mmc.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/core/mmc.c
  4. *
  5. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  6. * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
  7. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. */
  9. #include <linux/err.h>
  10. #include <linux/of.h>
  11. #include <linux/slab.h>
  12. #include <linux/stat.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/random.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/mmc.h>
  19. #include "core.h"
  20. #include "card.h"
  21. #include "host.h"
  22. #include "bus.h"
  23. #include "mmc_ops.h"
  24. #include "quirks.h"
  25. #include "sd_ops.h"
  26. #include "pwrseq.h"
  27. #define DEFAULT_CMD6_TIMEOUT_MS 500
  28. #define MIN_CACHE_EN_TIMEOUT_MS 1600
  29. #define CACHE_FLUSH_TIMEOUT_MS 30000 /* 30s */
  30. static const unsigned int tran_exp[] = {
  31. 10000, 100000, 1000000, 10000000,
  32. 0, 0, 0, 0
  33. };
  34. static const unsigned char tran_mant[] = {
  35. 0, 10, 12, 13, 15, 20, 25, 30,
  36. 35, 40, 45, 50, 55, 60, 70, 80,
  37. };
  38. static const unsigned int taac_exp[] = {
  39. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  40. };
  41. static const unsigned int taac_mant[] = {
  42. 0, 10, 12, 13, 15, 20, 25, 30,
  43. 35, 40, 45, 50, 55, 60, 70, 80,
  44. };
  45. /*
  46. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  47. */
  48. static int mmc_decode_cid(struct mmc_card *card)
  49. {
  50. u32 *resp = card->raw_cid;
  51. /*
  52. * Add the raw card ID (cid) data to the entropy pool. It doesn't
  53. * matter that not all of it is unique, it's just bonus entropy.
  54. */
  55. add_device_randomness(&card->raw_cid, sizeof(card->raw_cid));
  56. /*
  57. * The selection of the format here is based upon published
  58. * specs from sandisk and from what people have reported.
  59. */
  60. switch (card->csd.mmca_vsn) {
  61. case 0: /* MMC v1.0 - v1.2 */
  62. case 1: /* MMC v1.4 */
  63. card->cid.manfid = unstuff_bits(resp, 104, 24);
  64. card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
  65. card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
  66. card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
  67. card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
  68. card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
  69. card->cid.prod_name[5] = unstuff_bits(resp, 56, 8);
  70. card->cid.prod_name[6] = unstuff_bits(resp, 48, 8);
  71. card->cid.hwrev = unstuff_bits(resp, 44, 4);
  72. card->cid.fwrev = unstuff_bits(resp, 40, 4);
  73. card->cid.serial = unstuff_bits(resp, 16, 24);
  74. card->cid.month = unstuff_bits(resp, 12, 4);
  75. card->cid.year = unstuff_bits(resp, 8, 4) + 1997;
  76. break;
  77. case 2: /* MMC v2.0 - v2.2 */
  78. case 3: /* MMC v3.1 - v3.3 */
  79. case 4: /* MMC v4 */
  80. card->cid.manfid = unstuff_bits(resp, 120, 8);
  81. card->cid.oemid = unstuff_bits(resp, 104, 16);
  82. card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
  83. card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
  84. card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
  85. card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
  86. card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
  87. card->cid.prod_name[5] = unstuff_bits(resp, 56, 8);
  88. card->cid.prv = unstuff_bits(resp, 48, 8);
  89. card->cid.serial = unstuff_bits(resp, 16, 32);
  90. card->cid.month = unstuff_bits(resp, 12, 4);
  91. card->cid.year = unstuff_bits(resp, 8, 4) + 1997;
  92. break;
  93. default:
  94. pr_err("%s: card has unknown MMCA version %d\n",
  95. mmc_hostname(card->host), card->csd.mmca_vsn);
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static void mmc_set_erase_size(struct mmc_card *card)
  101. {
  102. if (card->ext_csd.erase_group_def & 1)
  103. card->erase_size = card->ext_csd.hc_erase_size;
  104. else
  105. card->erase_size = card->csd.erase_size;
  106. mmc_init_erase(card);
  107. }
  108. static void mmc_set_wp_grp_size(struct mmc_card *card)
  109. {
  110. if (card->ext_csd.erase_group_def & 1)
  111. card->wp_grp_size = card->ext_csd.hc_erase_size *
  112. card->ext_csd.raw_hc_erase_gap_size;
  113. else
  114. card->wp_grp_size = card->csd.erase_size *
  115. (card->csd.wp_grp_size + 1);
  116. }
  117. /*
  118. * Given a 128-bit response, decode to our card CSD structure.
  119. */
  120. static int mmc_decode_csd(struct mmc_card *card)
  121. {
  122. struct mmc_csd *csd = &card->csd;
  123. unsigned int e, m, a, b;
  124. u32 *resp = card->raw_csd;
  125. /*
  126. * We only understand CSD structure v1.1 and v1.2.
  127. * v1.2 has extra information in bits 15, 11 and 10.
  128. * We also support eMMC v4.4 & v4.41.
  129. */
  130. csd->structure = unstuff_bits(resp, 126, 2);
  131. if (csd->structure == 0) {
  132. pr_err("%s: unrecognised CSD structure version %d\n",
  133. mmc_hostname(card->host), csd->structure);
  134. return -EINVAL;
  135. }
  136. csd->mmca_vsn = unstuff_bits(resp, 122, 4);
  137. m = unstuff_bits(resp, 115, 4);
  138. e = unstuff_bits(resp, 112, 3);
  139. csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
  140. csd->taac_clks = unstuff_bits(resp, 104, 8) * 100;
  141. m = unstuff_bits(resp, 99, 4);
  142. e = unstuff_bits(resp, 96, 3);
  143. csd->max_dtr = tran_exp[e] * tran_mant[m];
  144. csd->cmdclass = unstuff_bits(resp, 84, 12);
  145. e = unstuff_bits(resp, 47, 3);
  146. m = unstuff_bits(resp, 62, 12);
  147. csd->capacity = (1 + m) << (e + 2);
  148. csd->read_blkbits = unstuff_bits(resp, 80, 4);
  149. csd->read_partial = unstuff_bits(resp, 79, 1);
  150. csd->write_misalign = unstuff_bits(resp, 78, 1);
  151. csd->read_misalign = unstuff_bits(resp, 77, 1);
  152. csd->dsr_imp = unstuff_bits(resp, 76, 1);
  153. csd->r2w_factor = unstuff_bits(resp, 26, 3);
  154. csd->write_blkbits = unstuff_bits(resp, 22, 4);
  155. csd->write_partial = unstuff_bits(resp, 21, 1);
  156. if (csd->write_blkbits >= 9) {
  157. a = unstuff_bits(resp, 42, 5);
  158. b = unstuff_bits(resp, 37, 5);
  159. csd->erase_size = (a + 1) * (b + 1);
  160. csd->erase_size <<= csd->write_blkbits - 9;
  161. csd->wp_grp_size = unstuff_bits(resp, 32, 5);
  162. }
  163. return 0;
  164. }
  165. static void mmc_select_card_type(struct mmc_card *card)
  166. {
  167. struct mmc_host *host = card->host;
  168. u8 card_type = card->ext_csd.raw_card_type;
  169. u32 caps = host->caps, caps2 = host->caps2;
  170. unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
  171. unsigned int avail_type = 0;
  172. if (caps & MMC_CAP_MMC_HIGHSPEED &&
  173. card_type & EXT_CSD_CARD_TYPE_HS_26) {
  174. hs_max_dtr = MMC_HIGH_26_MAX_DTR;
  175. avail_type |= EXT_CSD_CARD_TYPE_HS_26;
  176. }
  177. if (caps & MMC_CAP_MMC_HIGHSPEED &&
  178. card_type & EXT_CSD_CARD_TYPE_HS_52) {
  179. hs_max_dtr = MMC_HIGH_52_MAX_DTR;
  180. avail_type |= EXT_CSD_CARD_TYPE_HS_52;
  181. }
  182. if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
  183. card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
  184. hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
  185. avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
  186. }
  187. if (caps & MMC_CAP_1_2V_DDR &&
  188. card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
  189. hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
  190. avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
  191. }
  192. if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
  193. card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
  194. hs200_max_dtr = MMC_HS200_MAX_DTR;
  195. avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
  196. }
  197. if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
  198. card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
  199. hs200_max_dtr = MMC_HS200_MAX_DTR;
  200. avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
  201. }
  202. if (caps2 & MMC_CAP2_HS400_1_8V &&
  203. card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
  204. hs200_max_dtr = MMC_HS200_MAX_DTR;
  205. avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
  206. }
  207. if (caps2 & MMC_CAP2_HS400_1_2V &&
  208. card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
  209. hs200_max_dtr = MMC_HS200_MAX_DTR;
  210. avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
  211. }
  212. if ((caps2 & MMC_CAP2_HS400_ES) &&
  213. card->ext_csd.strobe_support &&
  214. (avail_type & EXT_CSD_CARD_TYPE_HS400))
  215. avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
  216. card->ext_csd.hs_max_dtr = hs_max_dtr;
  217. card->ext_csd.hs200_max_dtr = hs200_max_dtr;
  218. card->mmc_avail_type = avail_type;
  219. }
  220. static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
  221. {
  222. u8 hc_erase_grp_sz, hc_wp_grp_sz;
  223. /*
  224. * Disable these attributes by default
  225. */
  226. card->ext_csd.enhanced_area_offset = -EINVAL;
  227. card->ext_csd.enhanced_area_size = -EINVAL;
  228. /*
  229. * Enhanced area feature support -- check whether the eMMC
  230. * card has the Enhanced area enabled. If so, export enhanced
  231. * area offset and size to user by adding sysfs interface.
  232. */
  233. if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
  234. (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
  235. if (card->ext_csd.partition_setting_completed) {
  236. hc_erase_grp_sz =
  237. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  238. hc_wp_grp_sz =
  239. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  240. /*
  241. * calculate the enhanced data area offset, in bytes
  242. */
  243. card->ext_csd.enhanced_area_offset =
  244. (((unsigned long long)ext_csd[139]) << 24) +
  245. (((unsigned long long)ext_csd[138]) << 16) +
  246. (((unsigned long long)ext_csd[137]) << 8) +
  247. (((unsigned long long)ext_csd[136]));
  248. if (mmc_card_blockaddr(card))
  249. card->ext_csd.enhanced_area_offset <<= 9;
  250. /*
  251. * calculate the enhanced data area size, in kilobytes
  252. */
  253. card->ext_csd.enhanced_area_size =
  254. (ext_csd[142] << 16) + (ext_csd[141] << 8) +
  255. ext_csd[140];
  256. card->ext_csd.enhanced_area_size *=
  257. (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
  258. card->ext_csd.enhanced_area_size <<= 9;
  259. } else {
  260. pr_warn("%s: defines enhanced area without partition setting complete\n",
  261. mmc_hostname(card->host));
  262. }
  263. }
  264. }
  265. static void mmc_part_add(struct mmc_card *card, u64 size,
  266. unsigned int part_cfg, char *name, int idx, bool ro,
  267. int area_type)
  268. {
  269. card->part[card->nr_parts].size = size;
  270. card->part[card->nr_parts].part_cfg = part_cfg;
  271. sprintf(card->part[card->nr_parts].name, name, idx);
  272. card->part[card->nr_parts].force_ro = ro;
  273. card->part[card->nr_parts].area_type = area_type;
  274. card->nr_parts++;
  275. }
  276. static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
  277. {
  278. int idx;
  279. u8 hc_erase_grp_sz, hc_wp_grp_sz;
  280. u64 part_size;
  281. /*
  282. * General purpose partition feature support --
  283. * If ext_csd has the size of general purpose partitions,
  284. * set size, part_cfg, partition name in mmc_part.
  285. */
  286. if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
  287. EXT_CSD_PART_SUPPORT_PART_EN) {
  288. hc_erase_grp_sz =
  289. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  290. hc_wp_grp_sz =
  291. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  292. for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
  293. if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
  294. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
  295. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
  296. continue;
  297. if (card->ext_csd.partition_setting_completed == 0) {
  298. pr_warn("%s: has partition size defined without partition complete\n",
  299. mmc_hostname(card->host));
  300. break;
  301. }
  302. part_size =
  303. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
  304. << 16) +
  305. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
  306. << 8) +
  307. ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
  308. part_size *= (hc_erase_grp_sz * hc_wp_grp_sz);
  309. mmc_part_add(card, part_size << 19,
  310. EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
  311. "gp%d", idx, false,
  312. MMC_BLK_DATA_AREA_GP);
  313. }
  314. }
  315. }
  316. /* Minimum partition switch timeout in milliseconds */
  317. #define MMC_MIN_PART_SWITCH_TIME 300
  318. /*
  319. * Decode extended CSD.
  320. */
  321. static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
  322. {
  323. int err = 0, idx;
  324. u64 part_size;
  325. struct device_node *np;
  326. bool broken_hpi = false;
  327. /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
  328. card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
  329. if (card->csd.structure == 3) {
  330. if (card->ext_csd.raw_ext_csd_structure > 2) {
  331. pr_err("%s: unrecognised EXT_CSD structure "
  332. "version %d\n", mmc_hostname(card->host),
  333. card->ext_csd.raw_ext_csd_structure);
  334. err = -EINVAL;
  335. goto out;
  336. }
  337. }
  338. np = mmc_of_find_child_device(card->host, 0);
  339. if (np && of_device_is_compatible(np, "mmc-card"))
  340. broken_hpi = of_property_read_bool(np, "broken-hpi");
  341. of_node_put(np);
  342. /*
  343. * The EXT_CSD format is meant to be forward compatible. As long
  344. * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
  345. * are authorized, see JEDEC JESD84-B50 section B.8.
  346. */
  347. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  348. /* fixup device after ext_csd revision field is updated */
  349. mmc_fixup_device(card, mmc_ext_csd_fixups);
  350. card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
  351. card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
  352. card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
  353. card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
  354. if (card->ext_csd.rev >= 2) {
  355. card->ext_csd.sectors =
  356. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  357. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  358. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  359. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  360. /* Cards with density > 2GiB are sector addressed */
  361. if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
  362. mmc_card_set_blockaddr(card);
  363. }
  364. card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
  365. card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
  366. card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
  367. card->ext_csd.raw_erase_timeout_mult =
  368. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  369. card->ext_csd.raw_hc_erase_grp_size =
  370. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  371. card->ext_csd.raw_boot_mult =
  372. ext_csd[EXT_CSD_BOOT_MULT];
  373. if (card->ext_csd.rev >= 3) {
  374. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  375. card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
  376. /* EXT_CSD value is in units of 10ms, but we store in ms */
  377. card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
  378. /* Sleep / awake timeout in 100ns units */
  379. if (sa_shift > 0 && sa_shift <= 0x17)
  380. card->ext_csd.sa_timeout =
  381. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  382. card->ext_csd.erase_group_def =
  383. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  384. card->ext_csd.hc_erase_timeout = 300 *
  385. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  386. card->ext_csd.hc_erase_size =
  387. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  388. card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
  389. /*
  390. * There are two boot regions of equal size, defined in
  391. * multiples of 128K.
  392. */
  393. if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
  394. for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
  395. part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  396. mmc_part_add(card, part_size,
  397. EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
  398. "boot%d", idx, true,
  399. MMC_BLK_DATA_AREA_BOOT);
  400. }
  401. }
  402. }
  403. card->ext_csd.raw_hc_erase_gap_size =
  404. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  405. card->ext_csd.raw_sec_trim_mult =
  406. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  407. card->ext_csd.raw_sec_erase_mult =
  408. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  409. card->ext_csd.raw_sec_feature_support =
  410. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  411. card->ext_csd.raw_trim_mult =
  412. ext_csd[EXT_CSD_TRIM_MULT];
  413. card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
  414. card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
  415. if (card->ext_csd.rev >= 4) {
  416. if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
  417. EXT_CSD_PART_SETTING_COMPLETED)
  418. card->ext_csd.partition_setting_completed = 1;
  419. else
  420. card->ext_csd.partition_setting_completed = 0;
  421. mmc_manage_enhanced_area(card, ext_csd);
  422. mmc_manage_gp_partitions(card, ext_csd);
  423. card->ext_csd.sec_trim_mult =
  424. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  425. card->ext_csd.sec_erase_mult =
  426. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  427. card->ext_csd.sec_feature_support =
  428. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  429. card->ext_csd.trim_timeout = 300 *
  430. ext_csd[EXT_CSD_TRIM_MULT];
  431. /*
  432. * Note that the call to mmc_part_add above defaults to read
  433. * only. If this default assumption is changed, the call must
  434. * take into account the value of boot_locked below.
  435. */
  436. card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
  437. card->ext_csd.boot_ro_lockable = true;
  438. /* Save power class values */
  439. card->ext_csd.raw_pwr_cl_52_195 =
  440. ext_csd[EXT_CSD_PWR_CL_52_195];
  441. card->ext_csd.raw_pwr_cl_26_195 =
  442. ext_csd[EXT_CSD_PWR_CL_26_195];
  443. card->ext_csd.raw_pwr_cl_52_360 =
  444. ext_csd[EXT_CSD_PWR_CL_52_360];
  445. card->ext_csd.raw_pwr_cl_26_360 =
  446. ext_csd[EXT_CSD_PWR_CL_26_360];
  447. card->ext_csd.raw_pwr_cl_200_195 =
  448. ext_csd[EXT_CSD_PWR_CL_200_195];
  449. card->ext_csd.raw_pwr_cl_200_360 =
  450. ext_csd[EXT_CSD_PWR_CL_200_360];
  451. card->ext_csd.raw_pwr_cl_ddr_52_195 =
  452. ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
  453. card->ext_csd.raw_pwr_cl_ddr_52_360 =
  454. ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
  455. card->ext_csd.raw_pwr_cl_ddr_200_360 =
  456. ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
  457. }
  458. if (card->ext_csd.rev >= 5) {
  459. /* Adjust production date as per JEDEC JESD84-B451 */
  460. if (card->cid.year < 2010)
  461. card->cid.year += 16;
  462. /* check whether the eMMC card supports BKOPS */
  463. if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
  464. card->ext_csd.bkops = 1;
  465. card->ext_csd.man_bkops_en =
  466. (ext_csd[EXT_CSD_BKOPS_EN] &
  467. EXT_CSD_MANUAL_BKOPS_MASK);
  468. card->ext_csd.raw_bkops_status =
  469. ext_csd[EXT_CSD_BKOPS_STATUS];
  470. if (card->ext_csd.man_bkops_en)
  471. pr_debug("%s: MAN_BKOPS_EN bit is set\n",
  472. mmc_hostname(card->host));
  473. card->ext_csd.auto_bkops_en =
  474. (ext_csd[EXT_CSD_BKOPS_EN] &
  475. EXT_CSD_AUTO_BKOPS_MASK);
  476. if (card->ext_csd.auto_bkops_en)
  477. pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
  478. mmc_hostname(card->host));
  479. }
  480. /* check whether the eMMC card supports HPI */
  481. if (!mmc_card_broken_hpi(card) &&
  482. !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
  483. card->ext_csd.hpi = 1;
  484. if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
  485. card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
  486. else
  487. card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
  488. /*
  489. * Indicate the maximum timeout to close
  490. * a command interrupted by HPI
  491. */
  492. card->ext_csd.out_of_int_time =
  493. ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
  494. }
  495. card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
  496. card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
  497. /*
  498. * RPMB regions are defined in multiples of 128K.
  499. */
  500. card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
  501. if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
  502. mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
  503. EXT_CSD_PART_CONFIG_ACC_RPMB,
  504. "rpmb", 0, false,
  505. MMC_BLK_DATA_AREA_RPMB);
  506. }
  507. }
  508. card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
  509. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  510. card->erased_byte = 0xFF;
  511. else
  512. card->erased_byte = 0x0;
  513. /* eMMC v4.5 or later */
  514. card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
  515. if (card->ext_csd.rev >= 6) {
  516. card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
  517. card->ext_csd.generic_cmd6_time = 10 *
  518. ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
  519. card->ext_csd.power_off_longtime = 10 *
  520. ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
  521. card->ext_csd.cache_size =
  522. ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
  523. ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
  524. ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
  525. ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
  526. if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
  527. card->ext_csd.data_sector_size = 4096;
  528. else
  529. card->ext_csd.data_sector_size = 512;
  530. if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
  531. (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
  532. card->ext_csd.data_tag_unit_size =
  533. ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
  534. (card->ext_csd.data_sector_size);
  535. } else {
  536. card->ext_csd.data_tag_unit_size = 0;
  537. }
  538. } else {
  539. card->ext_csd.data_sector_size = 512;
  540. }
  541. /*
  542. * GENERIC_CMD6_TIME is to be used "unless a specific timeout is defined
  543. * when accessing a specific field", so use it here if there is no
  544. * PARTITION_SWITCH_TIME.
  545. */
  546. if (!card->ext_csd.part_time)
  547. card->ext_csd.part_time = card->ext_csd.generic_cmd6_time;
  548. /* Some eMMC set the value too low so set a minimum */
  549. if (card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
  550. card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
  551. /* eMMC v5 or later */
  552. if (card->ext_csd.rev >= 7) {
  553. memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
  554. MMC_FIRMWARE_LEN);
  555. card->ext_csd.ffu_capable =
  556. (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
  557. !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
  558. card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
  559. card->ext_csd.device_life_time_est_typ_a =
  560. ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
  561. card->ext_csd.device_life_time_est_typ_b =
  562. ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
  563. }
  564. /* eMMC v5.1 or later */
  565. if (card->ext_csd.rev >= 8) {
  566. card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
  567. EXT_CSD_CMDQ_SUPPORTED;
  568. card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
  569. EXT_CSD_CMDQ_DEPTH_MASK) + 1;
  570. /* Exclude inefficiently small queue depths */
  571. if (card->ext_csd.cmdq_depth <= 2) {
  572. card->ext_csd.cmdq_support = false;
  573. card->ext_csd.cmdq_depth = 0;
  574. }
  575. if (card->ext_csd.cmdq_support) {
  576. pr_debug("%s: Command Queue supported depth %u\n",
  577. mmc_hostname(card->host),
  578. card->ext_csd.cmdq_depth);
  579. }
  580. card->ext_csd.enhanced_rpmb_supported =
  581. (card->ext_csd.rel_param &
  582. EXT_CSD_WR_REL_PARAM_EN_RPMB_REL_WR);
  583. }
  584. out:
  585. return err;
  586. }
  587. static int mmc_read_ext_csd(struct mmc_card *card)
  588. {
  589. u8 *ext_csd;
  590. int err;
  591. if (!mmc_can_ext_csd(card))
  592. return 0;
  593. err = mmc_get_ext_csd(card, &ext_csd);
  594. if (err) {
  595. /* If the host or the card can't do the switch,
  596. * fail more gracefully. */
  597. if ((err != -EINVAL)
  598. && (err != -ENOSYS)
  599. && (err != -EFAULT))
  600. return err;
  601. /*
  602. * High capacity cards should have this "magic" size
  603. * stored in their CSD.
  604. */
  605. if (card->csd.capacity == (4096 * 512)) {
  606. pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
  607. mmc_hostname(card->host));
  608. } else {
  609. pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
  610. mmc_hostname(card->host));
  611. err = 0;
  612. }
  613. return err;
  614. }
  615. err = mmc_decode_ext_csd(card, ext_csd);
  616. kfree(ext_csd);
  617. return err;
  618. }
  619. static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
  620. {
  621. u8 *bw_ext_csd;
  622. int err;
  623. if (bus_width == MMC_BUS_WIDTH_1)
  624. return 0;
  625. err = mmc_get_ext_csd(card, &bw_ext_csd);
  626. if (err)
  627. return err;
  628. /* only compare read only fields */
  629. err = !((card->ext_csd.raw_partition_support ==
  630. bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  631. (card->ext_csd.raw_erased_mem_count ==
  632. bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  633. (card->ext_csd.rev ==
  634. bw_ext_csd[EXT_CSD_REV]) &&
  635. (card->ext_csd.raw_ext_csd_structure ==
  636. bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  637. (card->ext_csd.raw_card_type ==
  638. bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  639. (card->ext_csd.raw_s_a_timeout ==
  640. bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  641. (card->ext_csd.raw_hc_erase_gap_size ==
  642. bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  643. (card->ext_csd.raw_erase_timeout_mult ==
  644. bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  645. (card->ext_csd.raw_hc_erase_grp_size ==
  646. bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  647. (card->ext_csd.raw_sec_trim_mult ==
  648. bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  649. (card->ext_csd.raw_sec_erase_mult ==
  650. bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  651. (card->ext_csd.raw_sec_feature_support ==
  652. bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  653. (card->ext_csd.raw_trim_mult ==
  654. bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  655. (card->ext_csd.raw_sectors[0] ==
  656. bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  657. (card->ext_csd.raw_sectors[1] ==
  658. bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  659. (card->ext_csd.raw_sectors[2] ==
  660. bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  661. (card->ext_csd.raw_sectors[3] ==
  662. bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
  663. (card->ext_csd.raw_pwr_cl_52_195 ==
  664. bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
  665. (card->ext_csd.raw_pwr_cl_26_195 ==
  666. bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
  667. (card->ext_csd.raw_pwr_cl_52_360 ==
  668. bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
  669. (card->ext_csd.raw_pwr_cl_26_360 ==
  670. bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
  671. (card->ext_csd.raw_pwr_cl_200_195 ==
  672. bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
  673. (card->ext_csd.raw_pwr_cl_200_360 ==
  674. bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
  675. (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
  676. bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
  677. (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
  678. bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
  679. (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
  680. bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
  681. if (err)
  682. err = -EINVAL;
  683. kfree(bw_ext_csd);
  684. return err;
  685. }
  686. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  687. card->raw_cid[2], card->raw_cid[3]);
  688. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  689. card->raw_csd[2], card->raw_csd[3]);
  690. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  691. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  692. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  693. MMC_DEV_ATTR(wp_grp_size, "%u\n", card->wp_grp_size << 9);
  694. MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
  695. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  696. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  697. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  698. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  699. MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
  700. MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev);
  701. MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
  702. MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
  703. card->ext_csd.device_life_time_est_typ_a,
  704. card->ext_csd.device_life_time_est_typ_b);
  705. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  706. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  707. card->ext_csd.enhanced_area_offset);
  708. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  709. MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
  710. MMC_DEV_ATTR(enhanced_rpmb_supported, "%#x\n",
  711. card->ext_csd.enhanced_rpmb_supported);
  712. MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
  713. MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
  714. MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
  715. MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
  716. static ssize_t mmc_fwrev_show(struct device *dev,
  717. struct device_attribute *attr,
  718. char *buf)
  719. {
  720. struct mmc_card *card = mmc_dev_to_card(dev);
  721. if (card->ext_csd.rev < 7)
  722. return sysfs_emit(buf, "0x%x\n", card->cid.fwrev);
  723. else
  724. return sysfs_emit(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
  725. card->ext_csd.fwrev);
  726. }
  727. static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
  728. static ssize_t mmc_dsr_show(struct device *dev,
  729. struct device_attribute *attr,
  730. char *buf)
  731. {
  732. struct mmc_card *card = mmc_dev_to_card(dev);
  733. struct mmc_host *host = card->host;
  734. if (card->csd.dsr_imp && host->dsr_req)
  735. return sysfs_emit(buf, "0x%x\n", host->dsr);
  736. else
  737. /* return default DSR value */
  738. return sysfs_emit(buf, "0x%x\n", 0x404);
  739. }
  740. static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
  741. static struct attribute *mmc_std_attrs[] = {
  742. &dev_attr_cid.attr,
  743. &dev_attr_csd.attr,
  744. &dev_attr_date.attr,
  745. &dev_attr_erase_size.attr,
  746. &dev_attr_preferred_erase_size.attr,
  747. &dev_attr_wp_grp_size.attr,
  748. &dev_attr_fwrev.attr,
  749. &dev_attr_ffu_capable.attr,
  750. &dev_attr_hwrev.attr,
  751. &dev_attr_manfid.attr,
  752. &dev_attr_name.attr,
  753. &dev_attr_oemid.attr,
  754. &dev_attr_prv.attr,
  755. &dev_attr_rev.attr,
  756. &dev_attr_pre_eol_info.attr,
  757. &dev_attr_life_time.attr,
  758. &dev_attr_serial.attr,
  759. &dev_attr_enhanced_area_offset.attr,
  760. &dev_attr_enhanced_area_size.attr,
  761. &dev_attr_raw_rpmb_size_mult.attr,
  762. &dev_attr_enhanced_rpmb_supported.attr,
  763. &dev_attr_rel_sectors.attr,
  764. &dev_attr_ocr.attr,
  765. &dev_attr_rca.attr,
  766. &dev_attr_dsr.attr,
  767. &dev_attr_cmdq_en.attr,
  768. NULL,
  769. };
  770. ATTRIBUTE_GROUPS(mmc_std);
  771. static const struct device_type mmc_type = {
  772. .groups = mmc_std_groups,
  773. };
  774. /*
  775. * Select the PowerClass for the current bus width
  776. * If power class is defined for 4/8 bit bus in the
  777. * extended CSD register, select it by executing the
  778. * mmc_switch command.
  779. */
  780. static int __mmc_select_powerclass(struct mmc_card *card,
  781. unsigned int bus_width)
  782. {
  783. struct mmc_host *host = card->host;
  784. struct mmc_ext_csd *ext_csd = &card->ext_csd;
  785. unsigned int pwrclass_val = 0;
  786. int err = 0;
  787. switch (1 << host->ios.vdd) {
  788. case MMC_VDD_165_195:
  789. if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
  790. pwrclass_val = ext_csd->raw_pwr_cl_26_195;
  791. else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
  792. pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  793. ext_csd->raw_pwr_cl_52_195 :
  794. ext_csd->raw_pwr_cl_ddr_52_195;
  795. else if (host->ios.clock <= MMC_HS200_MAX_DTR)
  796. pwrclass_val = ext_csd->raw_pwr_cl_200_195;
  797. break;
  798. case MMC_VDD_27_28:
  799. case MMC_VDD_28_29:
  800. case MMC_VDD_29_30:
  801. case MMC_VDD_30_31:
  802. case MMC_VDD_31_32:
  803. case MMC_VDD_32_33:
  804. case MMC_VDD_33_34:
  805. case MMC_VDD_34_35:
  806. case MMC_VDD_35_36:
  807. if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
  808. pwrclass_val = ext_csd->raw_pwr_cl_26_360;
  809. else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
  810. pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  811. ext_csd->raw_pwr_cl_52_360 :
  812. ext_csd->raw_pwr_cl_ddr_52_360;
  813. else if (host->ios.clock <= MMC_HS200_MAX_DTR)
  814. pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
  815. ext_csd->raw_pwr_cl_ddr_200_360 :
  816. ext_csd->raw_pwr_cl_200_360;
  817. break;
  818. default:
  819. pr_warn("%s: Voltage range not supported for power class\n",
  820. mmc_hostname(host));
  821. return -EINVAL;
  822. }
  823. if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
  824. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
  825. EXT_CSD_PWR_CL_8BIT_SHIFT;
  826. else
  827. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
  828. EXT_CSD_PWR_CL_4BIT_SHIFT;
  829. /* If the power class is different from the default value */
  830. if (pwrclass_val > 0) {
  831. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  832. EXT_CSD_POWER_CLASS,
  833. pwrclass_val,
  834. card->ext_csd.generic_cmd6_time);
  835. }
  836. return err;
  837. }
  838. static int mmc_select_powerclass(struct mmc_card *card)
  839. {
  840. struct mmc_host *host = card->host;
  841. u32 bus_width, ext_csd_bits;
  842. int err, ddr;
  843. /* Power class selection is supported for versions >= 4.0 */
  844. if (!mmc_can_ext_csd(card))
  845. return 0;
  846. bus_width = host->ios.bus_width;
  847. /* Power class values are defined only for 4/8 bit bus */
  848. if (bus_width == MMC_BUS_WIDTH_1)
  849. return 0;
  850. ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
  851. if (ddr)
  852. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  853. EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
  854. else
  855. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  856. EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
  857. err = __mmc_select_powerclass(card, ext_csd_bits);
  858. if (err)
  859. pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
  860. mmc_hostname(host), 1 << bus_width, ddr);
  861. return err;
  862. }
  863. /*
  864. * Set the bus speed for the selected speed mode.
  865. */
  866. static void mmc_set_bus_speed(struct mmc_card *card)
  867. {
  868. unsigned int max_dtr = (unsigned int)-1;
  869. if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
  870. max_dtr > card->ext_csd.hs200_max_dtr)
  871. max_dtr = card->ext_csd.hs200_max_dtr;
  872. else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
  873. max_dtr = card->ext_csd.hs_max_dtr;
  874. else if (max_dtr > card->csd.max_dtr)
  875. max_dtr = card->csd.max_dtr;
  876. mmc_set_clock(card->host, max_dtr);
  877. }
  878. /*
  879. * Select the bus width amoung 4-bit and 8-bit(SDR).
  880. * If the bus width is changed successfully, return the selected width value.
  881. * Zero is returned instead of error value if the wide width is not supported.
  882. */
  883. static int mmc_select_bus_width(struct mmc_card *card)
  884. {
  885. static unsigned ext_csd_bits[] = {
  886. EXT_CSD_BUS_WIDTH_8,
  887. EXT_CSD_BUS_WIDTH_4,
  888. EXT_CSD_BUS_WIDTH_1,
  889. };
  890. static unsigned bus_widths[] = {
  891. MMC_BUS_WIDTH_8,
  892. MMC_BUS_WIDTH_4,
  893. MMC_BUS_WIDTH_1,
  894. };
  895. struct mmc_host *host = card->host;
  896. unsigned idx, bus_width = 0;
  897. int err = 0;
  898. if (!mmc_can_ext_csd(card) ||
  899. !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
  900. return 0;
  901. idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
  902. /*
  903. * Unlike SD, MMC cards dont have a configuration register to notify
  904. * supported bus width. So bus test command should be run to identify
  905. * the supported bus width or compare the ext csd values of current
  906. * bus width and ext csd values of 1 bit mode read earlier.
  907. */
  908. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  909. /*
  910. * Host is capable of 8bit transfer, then switch
  911. * the device to work in 8bit transfer mode. If the
  912. * mmc switch command returns error then switch to
  913. * 4bit transfer mode. On success set the corresponding
  914. * bus width on the host.
  915. */
  916. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  917. EXT_CSD_BUS_WIDTH,
  918. ext_csd_bits[idx],
  919. card->ext_csd.generic_cmd6_time);
  920. if (err)
  921. continue;
  922. bus_width = bus_widths[idx];
  923. mmc_set_bus_width(host, bus_width);
  924. /*
  925. * If controller can't handle bus width test,
  926. * compare ext_csd previously read in 1 bit mode
  927. * against ext_csd at new bus width
  928. */
  929. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  930. err = mmc_compare_ext_csds(card, bus_width);
  931. else
  932. err = mmc_bus_test(card, bus_width);
  933. if (!err) {
  934. err = bus_width;
  935. break;
  936. } else {
  937. pr_warn("%s: switch to bus width %d failed\n",
  938. mmc_hostname(host), 1 << bus_width);
  939. }
  940. }
  941. return err;
  942. }
  943. /*
  944. * Switch to the high-speed mode
  945. */
  946. static int mmc_select_hs(struct mmc_card *card)
  947. {
  948. int err;
  949. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  950. EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
  951. card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
  952. true, true, MMC_CMD_RETRIES);
  953. if (err)
  954. pr_warn("%s: switch to high-speed failed, err:%d\n",
  955. mmc_hostname(card->host), err);
  956. return err;
  957. }
  958. /*
  959. * Activate wide bus and DDR if supported.
  960. */
  961. static int mmc_select_hs_ddr(struct mmc_card *card)
  962. {
  963. struct mmc_host *host = card->host;
  964. u32 bus_width, ext_csd_bits;
  965. int err = 0;
  966. if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
  967. return 0;
  968. bus_width = host->ios.bus_width;
  969. if (bus_width == MMC_BUS_WIDTH_1)
  970. return 0;
  971. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  972. EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
  973. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  974. EXT_CSD_BUS_WIDTH,
  975. ext_csd_bits,
  976. card->ext_csd.generic_cmd6_time,
  977. MMC_TIMING_MMC_DDR52,
  978. true, true, MMC_CMD_RETRIES);
  979. if (err) {
  980. pr_err("%s: switch to bus width %d ddr failed\n",
  981. mmc_hostname(host), 1 << bus_width);
  982. return err;
  983. }
  984. /*
  985. * eMMC cards can support 3.3V to 1.2V i/o (vccq)
  986. * signaling.
  987. *
  988. * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
  989. *
  990. * 1.8V vccq at 3.3V core voltage (vcc) is not required
  991. * in the JEDEC spec for DDR.
  992. *
  993. * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
  994. * host controller can support this, like some of the SDHCI
  995. * controller which connect to an eMMC device. Some of these
  996. * host controller still needs to use 1.8v vccq for supporting
  997. * DDR mode.
  998. *
  999. * So the sequence will be:
  1000. * if (host and device can both support 1.2v IO)
  1001. * use 1.2v IO;
  1002. * else if (host and device can both support 1.8v IO)
  1003. * use 1.8v IO;
  1004. * so if host and device can only support 3.3v IO, this is the
  1005. * last choice.
  1006. *
  1007. * WARNING: eMMC rules are NOT the same as SD DDR
  1008. */
  1009. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
  1010. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
  1011. if (!err)
  1012. return 0;
  1013. }
  1014. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
  1015. host->caps & MMC_CAP_1_8V_DDR)
  1016. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
  1017. /* make sure vccq is 3.3v after switching disaster */
  1018. if (err)
  1019. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
  1020. return err;
  1021. }
  1022. static int mmc_select_hs400(struct mmc_card *card)
  1023. {
  1024. struct mmc_host *host = card->host;
  1025. unsigned int max_dtr;
  1026. int err = 0;
  1027. u8 val;
  1028. /*
  1029. * HS400 mode requires 8-bit bus width
  1030. */
  1031. if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
  1032. host->ios.bus_width == MMC_BUS_WIDTH_8))
  1033. return 0;
  1034. /* Switch card to HS mode */
  1035. val = EXT_CSD_TIMING_HS;
  1036. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1037. EXT_CSD_HS_TIMING, val,
  1038. card->ext_csd.generic_cmd6_time, 0,
  1039. false, true, MMC_CMD_RETRIES);
  1040. if (err) {
  1041. pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
  1042. mmc_hostname(host), err);
  1043. return err;
  1044. }
  1045. /* Prepare host to downgrade to HS timing */
  1046. if (host->ops->hs400_downgrade)
  1047. host->ops->hs400_downgrade(host);
  1048. /* Set host controller to HS timing */
  1049. mmc_set_timing(host, MMC_TIMING_MMC_HS);
  1050. /* Reduce frequency to HS frequency */
  1051. max_dtr = card->ext_csd.hs_max_dtr;
  1052. mmc_set_clock(host, max_dtr);
  1053. err = mmc_switch_status(card, true);
  1054. if (err)
  1055. goto out_err;
  1056. if (host->ops->hs400_prepare_ddr)
  1057. host->ops->hs400_prepare_ddr(host);
  1058. /* Switch card to DDR */
  1059. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1060. EXT_CSD_BUS_WIDTH,
  1061. EXT_CSD_DDR_BUS_WIDTH_8,
  1062. card->ext_csd.generic_cmd6_time);
  1063. if (err) {
  1064. pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
  1065. mmc_hostname(host), err);
  1066. return err;
  1067. }
  1068. /* Switch card to HS400 */
  1069. val = EXT_CSD_TIMING_HS400 |
  1070. card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
  1071. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1072. EXT_CSD_HS_TIMING, val,
  1073. card->ext_csd.generic_cmd6_time, 0,
  1074. false, true, MMC_CMD_RETRIES);
  1075. if (err) {
  1076. pr_err("%s: switch to hs400 failed, err:%d\n",
  1077. mmc_hostname(host), err);
  1078. return err;
  1079. }
  1080. /* Set host controller to HS400 timing and frequency */
  1081. mmc_set_timing(host, MMC_TIMING_MMC_HS400);
  1082. mmc_set_bus_speed(card);
  1083. if (host->ops->execute_hs400_tuning) {
  1084. mmc_retune_disable(host);
  1085. err = host->ops->execute_hs400_tuning(host, card);
  1086. mmc_retune_enable(host);
  1087. if (err)
  1088. goto out_err;
  1089. }
  1090. if (host->ops->hs400_complete)
  1091. host->ops->hs400_complete(host);
  1092. err = mmc_switch_status(card, true);
  1093. if (err)
  1094. goto out_err;
  1095. return 0;
  1096. out_err:
  1097. pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
  1098. __func__, err);
  1099. return err;
  1100. }
  1101. int mmc_hs200_to_hs400(struct mmc_card *card)
  1102. {
  1103. return mmc_select_hs400(card);
  1104. }
  1105. int mmc_hs400_to_hs200(struct mmc_card *card)
  1106. {
  1107. struct mmc_host *host = card->host;
  1108. unsigned int max_dtr;
  1109. int err;
  1110. u8 val;
  1111. /* Reduce frequency to HS */
  1112. max_dtr = card->ext_csd.hs_max_dtr;
  1113. mmc_set_clock(host, max_dtr);
  1114. /* Switch HS400 to HS DDR */
  1115. val = EXT_CSD_TIMING_HS;
  1116. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
  1117. val, card->ext_csd.generic_cmd6_time, 0,
  1118. false, true, MMC_CMD_RETRIES);
  1119. if (err)
  1120. goto out_err;
  1121. if (host->ops->hs400_downgrade)
  1122. host->ops->hs400_downgrade(host);
  1123. mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
  1124. err = mmc_switch_status(card, true);
  1125. if (err)
  1126. goto out_err;
  1127. /* Switch HS DDR to HS */
  1128. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
  1129. EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
  1130. 0, false, true, MMC_CMD_RETRIES);
  1131. if (err)
  1132. goto out_err;
  1133. mmc_set_timing(host, MMC_TIMING_MMC_HS);
  1134. err = mmc_switch_status(card, true);
  1135. if (err)
  1136. goto out_err;
  1137. /* Switch HS to HS200 */
  1138. val = EXT_CSD_TIMING_HS200 |
  1139. card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
  1140. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
  1141. val, card->ext_csd.generic_cmd6_time, 0,
  1142. false, true, MMC_CMD_RETRIES);
  1143. if (err)
  1144. goto out_err;
  1145. mmc_set_timing(host, MMC_TIMING_MMC_HS200);
  1146. /*
  1147. * For HS200, CRC errors are not a reliable way to know the switch
  1148. * failed. If there really is a problem, we would expect tuning will
  1149. * fail and the result ends up the same.
  1150. */
  1151. err = mmc_switch_status(card, false);
  1152. if (err)
  1153. goto out_err;
  1154. mmc_set_bus_speed(card);
  1155. /* Prepare tuning for HS400 mode. */
  1156. if (host->ops->prepare_hs400_tuning)
  1157. host->ops->prepare_hs400_tuning(host, &host->ios);
  1158. return 0;
  1159. out_err:
  1160. pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
  1161. __func__, err);
  1162. return err;
  1163. }
  1164. static void mmc_select_driver_type(struct mmc_card *card)
  1165. {
  1166. int card_drv_type, drive_strength, drv_type = 0;
  1167. int fixed_drv_type = card->host->fixed_drv_type;
  1168. card_drv_type = card->ext_csd.raw_driver_strength |
  1169. mmc_driver_type_mask(0);
  1170. if (fixed_drv_type >= 0)
  1171. drive_strength = card_drv_type & mmc_driver_type_mask(fixed_drv_type)
  1172. ? fixed_drv_type : 0;
  1173. else
  1174. drive_strength = mmc_select_drive_strength(card,
  1175. card->ext_csd.hs200_max_dtr,
  1176. card_drv_type, &drv_type);
  1177. card->drive_strength = drive_strength;
  1178. if (drv_type)
  1179. mmc_set_driver_type(card->host, drv_type);
  1180. }
  1181. static int mmc_select_hs400es(struct mmc_card *card)
  1182. {
  1183. struct mmc_host *host = card->host;
  1184. int err = -EINVAL;
  1185. u8 val;
  1186. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
  1187. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
  1188. if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
  1189. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
  1190. /* If fails try again during next card power cycle */
  1191. if (err)
  1192. goto out_err;
  1193. err = mmc_select_bus_width(card);
  1194. if (err != MMC_BUS_WIDTH_8) {
  1195. pr_err("%s: switch to 8bit bus width failed, err:%d\n",
  1196. mmc_hostname(host), err);
  1197. err = err < 0 ? err : -ENOTSUPP;
  1198. goto out_err;
  1199. }
  1200. /* Switch card to HS mode */
  1201. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1202. EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
  1203. card->ext_csd.generic_cmd6_time, 0,
  1204. false, true, MMC_CMD_RETRIES);
  1205. if (err) {
  1206. pr_err("%s: switch to hs for hs400es failed, err:%d\n",
  1207. mmc_hostname(host), err);
  1208. goto out_err;
  1209. }
  1210. /*
  1211. * Bump to HS timing and frequency. Some cards don't handle
  1212. * SEND_STATUS reliably at the initial frequency.
  1213. */
  1214. mmc_set_timing(host, MMC_TIMING_MMC_HS);
  1215. mmc_set_bus_speed(card);
  1216. err = mmc_switch_status(card, true);
  1217. if (err)
  1218. goto out_err;
  1219. /* Switch card to DDR with strobe bit */
  1220. val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
  1221. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1222. EXT_CSD_BUS_WIDTH,
  1223. val,
  1224. card->ext_csd.generic_cmd6_time);
  1225. if (err) {
  1226. pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
  1227. mmc_hostname(host), err);
  1228. goto out_err;
  1229. }
  1230. mmc_select_driver_type(card);
  1231. /* Switch card to HS400 */
  1232. val = EXT_CSD_TIMING_HS400 |
  1233. card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
  1234. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1235. EXT_CSD_HS_TIMING, val,
  1236. card->ext_csd.generic_cmd6_time, 0,
  1237. false, true, MMC_CMD_RETRIES);
  1238. if (err) {
  1239. pr_err("%s: switch to hs400es failed, err:%d\n",
  1240. mmc_hostname(host), err);
  1241. goto out_err;
  1242. }
  1243. /* Set host controller to HS400 timing and frequency */
  1244. mmc_set_timing(host, MMC_TIMING_MMC_HS400);
  1245. /* Controller enable enhanced strobe function */
  1246. host->ios.enhanced_strobe = true;
  1247. if (host->ops->hs400_enhanced_strobe)
  1248. host->ops->hs400_enhanced_strobe(host, &host->ios);
  1249. err = mmc_switch_status(card, true);
  1250. if (err)
  1251. goto out_err;
  1252. return 0;
  1253. out_err:
  1254. pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
  1255. __func__, err);
  1256. return err;
  1257. }
  1258. /*
  1259. * For device supporting HS200 mode, the following sequence
  1260. * should be done before executing the tuning process.
  1261. * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
  1262. * 2. switch to HS200 mode
  1263. * 3. set the clock to > 52Mhz and <=200MHz
  1264. */
  1265. static int mmc_select_hs200(struct mmc_card *card)
  1266. {
  1267. struct mmc_host *host = card->host;
  1268. unsigned int old_timing, old_signal_voltage, old_clock;
  1269. int err = -EINVAL;
  1270. u8 val;
  1271. old_signal_voltage = host->ios.signal_voltage;
  1272. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
  1273. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
  1274. if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
  1275. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
  1276. /* If fails try again during next card power cycle */
  1277. if (err)
  1278. return err;
  1279. mmc_select_driver_type(card);
  1280. /*
  1281. * Set the bus width(4 or 8) with host's support and
  1282. * switch to HS200 mode if bus width is set successfully.
  1283. */
  1284. err = mmc_select_bus_width(card);
  1285. if (err > 0) {
  1286. val = EXT_CSD_TIMING_HS200 |
  1287. card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
  1288. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1289. EXT_CSD_HS_TIMING, val,
  1290. card->ext_csd.generic_cmd6_time, 0,
  1291. false, true, MMC_CMD_RETRIES);
  1292. if (err)
  1293. goto err;
  1294. /*
  1295. * Bump to HS timing and frequency. Some cards don't handle
  1296. * SEND_STATUS reliably at the initial frequency.
  1297. * NB: We can't move to full (HS200) speeds until after we've
  1298. * successfully switched over.
  1299. */
  1300. old_timing = host->ios.timing;
  1301. old_clock = host->ios.clock;
  1302. mmc_set_timing(host, MMC_TIMING_MMC_HS200);
  1303. mmc_set_clock(card->host, card->ext_csd.hs_max_dtr);
  1304. /*
  1305. * For HS200, CRC errors are not a reliable way to know the
  1306. * switch failed. If there really is a problem, we would expect
  1307. * tuning will fail and the result ends up the same.
  1308. */
  1309. err = mmc_switch_status(card, false);
  1310. /*
  1311. * mmc_select_timing() assumes timing has not changed if
  1312. * it is a switch error.
  1313. */
  1314. if (err == -EBADMSG) {
  1315. mmc_set_clock(host, old_clock);
  1316. mmc_set_timing(host, old_timing);
  1317. }
  1318. }
  1319. err:
  1320. if (err) {
  1321. /* fall back to the old signal voltage, if fails report error */
  1322. if (mmc_set_signal_voltage(host, old_signal_voltage))
  1323. err = -EIO;
  1324. pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
  1325. __func__, err);
  1326. }
  1327. return err;
  1328. }
  1329. /*
  1330. * Activate High Speed, HS200 or HS400ES mode if supported.
  1331. */
  1332. static int mmc_select_timing(struct mmc_card *card)
  1333. {
  1334. int err = 0;
  1335. if (!mmc_can_ext_csd(card))
  1336. goto bus_speed;
  1337. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES) {
  1338. err = mmc_select_hs400es(card);
  1339. goto out;
  1340. }
  1341. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200) {
  1342. err = mmc_select_hs200(card);
  1343. if (err == -EBADMSG)
  1344. card->mmc_avail_type &= ~EXT_CSD_CARD_TYPE_HS200;
  1345. else
  1346. goto out;
  1347. }
  1348. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
  1349. err = mmc_select_hs(card);
  1350. out:
  1351. if (err && err != -EBADMSG)
  1352. return err;
  1353. bus_speed:
  1354. /*
  1355. * Set the bus speed to the selected bus timing.
  1356. * If timing is not selected, backward compatible is the default.
  1357. */
  1358. mmc_set_bus_speed(card);
  1359. return 0;
  1360. }
  1361. /*
  1362. * Execute tuning sequence to seek the proper bus operating
  1363. * conditions for HS200 and HS400, which sends CMD21 to the device.
  1364. */
  1365. static int mmc_hs200_tuning(struct mmc_card *card)
  1366. {
  1367. struct mmc_host *host = card->host;
  1368. /*
  1369. * Timing should be adjusted to the HS400 target
  1370. * operation frequency for tuning process
  1371. */
  1372. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
  1373. host->ios.bus_width == MMC_BUS_WIDTH_8)
  1374. if (host->ops->prepare_hs400_tuning)
  1375. host->ops->prepare_hs400_tuning(host, &host->ios);
  1376. return mmc_execute_tuning(card);
  1377. }
  1378. /*
  1379. * Handle the detection and initialisation of a card.
  1380. *
  1381. * In the case of a resume, "oldcard" will contain the card
  1382. * we're trying to reinitialise.
  1383. */
  1384. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  1385. struct mmc_card *oldcard)
  1386. {
  1387. struct mmc_card *card;
  1388. int err;
  1389. u32 cid[4];
  1390. u32 rocr;
  1391. WARN_ON(!host->claimed);
  1392. /* Set correct bus mode for MMC before attempting init */
  1393. if (!mmc_host_is_spi(host))
  1394. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1395. /*
  1396. * Since we're changing the OCR value, we seem to
  1397. * need to tell some cards to go back to the idle
  1398. * state. We wait 1ms to give cards time to
  1399. * respond.
  1400. * mmc_go_idle is needed for eMMC that are asleep
  1401. */
  1402. mmc_go_idle(host);
  1403. /* The extra bit indicates that we support high capacity */
  1404. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  1405. if (err)
  1406. goto err;
  1407. /*
  1408. * For SPI, enable CRC as appropriate.
  1409. */
  1410. if (mmc_host_is_spi(host)) {
  1411. err = mmc_spi_set_crc(host, use_spi_crc);
  1412. if (err)
  1413. goto err;
  1414. }
  1415. /*
  1416. * Fetch CID from card.
  1417. */
  1418. err = mmc_send_cid(host, cid);
  1419. if (err)
  1420. goto err;
  1421. if (oldcard) {
  1422. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  1423. pr_debug("%s: Perhaps the card was replaced\n",
  1424. mmc_hostname(host));
  1425. err = -ENOENT;
  1426. goto err;
  1427. }
  1428. card = oldcard;
  1429. } else {
  1430. /*
  1431. * Allocate card structure.
  1432. */
  1433. card = mmc_alloc_card(host, &mmc_type);
  1434. if (IS_ERR(card)) {
  1435. err = PTR_ERR(card);
  1436. goto err;
  1437. }
  1438. card->ocr = ocr;
  1439. card->type = MMC_TYPE_MMC;
  1440. card->rca = 1;
  1441. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  1442. }
  1443. /*
  1444. * Call the optional HC's init_card function to handle quirks.
  1445. */
  1446. if (host->ops->init_card)
  1447. host->ops->init_card(host, card);
  1448. /*
  1449. * For native busses: set card RCA and quit open drain mode.
  1450. */
  1451. if (!mmc_host_is_spi(host)) {
  1452. err = mmc_set_relative_addr(card);
  1453. if (err)
  1454. goto free_card;
  1455. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  1456. }
  1457. if (!oldcard) {
  1458. /*
  1459. * Fetch CSD from card.
  1460. */
  1461. err = mmc_send_csd(card, card->raw_csd);
  1462. if (err)
  1463. goto free_card;
  1464. err = mmc_decode_csd(card);
  1465. if (err)
  1466. goto free_card;
  1467. err = mmc_decode_cid(card);
  1468. if (err)
  1469. goto free_card;
  1470. }
  1471. /*
  1472. * handling only for cards supporting DSR and hosts requesting
  1473. * DSR configuration
  1474. */
  1475. if (card->csd.dsr_imp && host->dsr_req)
  1476. mmc_set_dsr(host);
  1477. /*
  1478. * Select card, as all following commands rely on that.
  1479. */
  1480. if (!mmc_host_is_spi(host)) {
  1481. err = mmc_select_card(card);
  1482. if (err)
  1483. goto free_card;
  1484. }
  1485. if (!oldcard) {
  1486. /* Read extended CSD. */
  1487. err = mmc_read_ext_csd(card);
  1488. if (err)
  1489. goto free_card;
  1490. /*
  1491. * If doing byte addressing, check if required to do sector
  1492. * addressing. Handle the case of <2GB cards needing sector
  1493. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  1494. * ocr register has bit 30 set for sector addressing.
  1495. */
  1496. if (rocr & BIT(30))
  1497. mmc_card_set_blockaddr(card);
  1498. /* Erase size depends on CSD and Extended CSD */
  1499. mmc_set_erase_size(card);
  1500. }
  1501. /*
  1502. * Reselect the card type since host caps could have been changed when
  1503. * debugging even if the card is not new.
  1504. */
  1505. mmc_select_card_type(card);
  1506. /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
  1507. if (card->ext_csd.rev >= 3) {
  1508. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1509. EXT_CSD_ERASE_GROUP_DEF, 1,
  1510. card->ext_csd.generic_cmd6_time);
  1511. if (err && err != -EBADMSG)
  1512. goto free_card;
  1513. if (err) {
  1514. /*
  1515. * Just disable enhanced area off & sz
  1516. * will try to enable ERASE_GROUP_DEF
  1517. * during next time reinit
  1518. */
  1519. card->ext_csd.enhanced_area_offset = -EINVAL;
  1520. card->ext_csd.enhanced_area_size = -EINVAL;
  1521. } else {
  1522. card->ext_csd.erase_group_def = 1;
  1523. /*
  1524. * enable ERASE_GRP_DEF successfully.
  1525. * This will affect the erase size, so
  1526. * here need to reset erase size
  1527. */
  1528. mmc_set_erase_size(card);
  1529. }
  1530. }
  1531. mmc_set_wp_grp_size(card);
  1532. /*
  1533. * Ensure eMMC user default partition is enabled
  1534. */
  1535. if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
  1536. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  1537. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
  1538. card->ext_csd.part_config,
  1539. card->ext_csd.part_time);
  1540. if (err && err != -EBADMSG)
  1541. goto free_card;
  1542. }
  1543. /*
  1544. * Enable power_off_notification byte in the ext_csd register
  1545. */
  1546. if (card->ext_csd.rev >= 6) {
  1547. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1548. EXT_CSD_POWER_OFF_NOTIFICATION,
  1549. EXT_CSD_POWER_ON,
  1550. card->ext_csd.generic_cmd6_time);
  1551. if (err && err != -EBADMSG)
  1552. goto free_card;
  1553. /*
  1554. * The err can be -EBADMSG or 0,
  1555. * so check for success and update the flag
  1556. */
  1557. if (!err)
  1558. card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
  1559. }
  1560. /* set erase_arg */
  1561. if (mmc_can_discard(card))
  1562. card->erase_arg = MMC_DISCARD_ARG;
  1563. else if (mmc_can_trim(card))
  1564. card->erase_arg = MMC_TRIM_ARG;
  1565. else
  1566. card->erase_arg = MMC_ERASE_ARG;
  1567. /*
  1568. * Select timing interface
  1569. */
  1570. err = mmc_select_timing(card);
  1571. if (err)
  1572. goto free_card;
  1573. if (mmc_card_hs200(card)) {
  1574. host->doing_init_tune = 1;
  1575. err = mmc_hs200_tuning(card);
  1576. if (!err)
  1577. err = mmc_select_hs400(card);
  1578. host->doing_init_tune = 0;
  1579. if (err)
  1580. goto free_card;
  1581. } else if (mmc_card_hs400es(card)) {
  1582. if (host->ops->execute_hs400_tuning) {
  1583. err = host->ops->execute_hs400_tuning(host, card);
  1584. if (err)
  1585. goto free_card;
  1586. }
  1587. } else {
  1588. /* Select the desired bus width optionally */
  1589. err = mmc_select_bus_width(card);
  1590. if (err > 0 && mmc_card_hs(card)) {
  1591. err = mmc_select_hs_ddr(card);
  1592. if (err)
  1593. goto free_card;
  1594. }
  1595. }
  1596. /*
  1597. * Choose the power class with selected bus interface
  1598. */
  1599. mmc_select_powerclass(card);
  1600. /*
  1601. * Enable HPI feature (if supported)
  1602. */
  1603. if (card->ext_csd.hpi) {
  1604. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1605. EXT_CSD_HPI_MGMT, 1,
  1606. card->ext_csd.generic_cmd6_time);
  1607. if (err && err != -EBADMSG)
  1608. goto free_card;
  1609. if (err) {
  1610. pr_warn("%s: Enabling HPI failed\n",
  1611. mmc_hostname(card->host));
  1612. card->ext_csd.hpi_en = 0;
  1613. } else {
  1614. card->ext_csd.hpi_en = 1;
  1615. }
  1616. }
  1617. /*
  1618. * If cache size is higher than 0, this indicates the existence of cache
  1619. * and it can be turned on. Note that some eMMCs from Micron has been
  1620. * reported to need ~800 ms timeout, while enabling the cache after
  1621. * sudden power failure tests. Let's extend the timeout to a minimum of
  1622. * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
  1623. */
  1624. if (card->ext_csd.cache_size > 0) {
  1625. unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
  1626. timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
  1627. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1628. EXT_CSD_CACHE_CTRL, 1, timeout_ms);
  1629. if (err && err != -EBADMSG)
  1630. goto free_card;
  1631. /*
  1632. * Only if no error, cache is turned on successfully.
  1633. */
  1634. if (err) {
  1635. pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
  1636. mmc_hostname(card->host), err);
  1637. card->ext_csd.cache_ctrl = 0;
  1638. } else {
  1639. card->ext_csd.cache_ctrl = 1;
  1640. }
  1641. }
  1642. /*
  1643. * Enable Command Queue if supported. Note that Packed Commands cannot
  1644. * be used with Command Queue.
  1645. */
  1646. card->ext_csd.cmdq_en = false;
  1647. if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) {
  1648. err = mmc_cmdq_enable(card);
  1649. if (err && err != -EBADMSG)
  1650. goto free_card;
  1651. if (err) {
  1652. pr_warn("%s: Enabling CMDQ failed\n",
  1653. mmc_hostname(card->host));
  1654. card->ext_csd.cmdq_support = false;
  1655. card->ext_csd.cmdq_depth = 0;
  1656. }
  1657. }
  1658. /*
  1659. * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
  1660. * disabled for a time, so a flag is needed to indicate to re-enable the
  1661. * Command Queue.
  1662. */
  1663. card->reenable_cmdq = card->ext_csd.cmdq_en;
  1664. if (host->cqe_ops && !host->cqe_enabled) {
  1665. err = host->cqe_ops->cqe_enable(host, card);
  1666. if (!err) {
  1667. host->cqe_enabled = true;
  1668. if (card->ext_csd.cmdq_en) {
  1669. pr_info("%s: Command Queue Engine enabled\n",
  1670. mmc_hostname(host));
  1671. } else {
  1672. host->hsq_enabled = true;
  1673. pr_info("%s: Host Software Queue enabled\n",
  1674. mmc_hostname(host));
  1675. }
  1676. }
  1677. }
  1678. if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
  1679. host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1680. pr_err("%s: Host failed to negotiate down from 3.3V\n",
  1681. mmc_hostname(host));
  1682. err = -EINVAL;
  1683. goto free_card;
  1684. }
  1685. if (!oldcard)
  1686. host->card = card;
  1687. return 0;
  1688. free_card:
  1689. if (!oldcard)
  1690. mmc_remove_card(card);
  1691. err:
  1692. return err;
  1693. }
  1694. static int mmc_can_sleep(struct mmc_card *card)
  1695. {
  1696. return card->ext_csd.rev >= 3;
  1697. }
  1698. static int mmc_sleep_busy_cb(void *cb_data, bool *busy)
  1699. {
  1700. struct mmc_host *host = cb_data;
  1701. *busy = host->ops->card_busy(host);
  1702. return 0;
  1703. }
  1704. static int mmc_sleep(struct mmc_host *host)
  1705. {
  1706. struct mmc_command cmd = {};
  1707. struct mmc_card *card = host->card;
  1708. unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
  1709. bool use_r1b_resp;
  1710. int err;
  1711. /* Re-tuning can't be done once the card is deselected */
  1712. mmc_retune_hold(host);
  1713. err = mmc_deselect_cards(host);
  1714. if (err)
  1715. goto out_release;
  1716. cmd.opcode = MMC_SLEEP_AWAKE;
  1717. cmd.arg = card->rca << 16;
  1718. cmd.arg |= 1 << 15;
  1719. use_r1b_resp = mmc_prepare_busy_cmd(host, &cmd, timeout_ms);
  1720. err = mmc_wait_for_cmd(host, &cmd, 0);
  1721. if (err)
  1722. goto out_release;
  1723. /*
  1724. * If the host does not wait while the card signals busy, then we can
  1725. * try to poll, but only if the host supports HW polling, as the
  1726. * SEND_STATUS cmd is not allowed. If we can't poll, then we simply need
  1727. * to wait the sleep/awake timeout.
  1728. */
  1729. if (host->caps & MMC_CAP_WAIT_WHILE_BUSY && use_r1b_resp)
  1730. goto out_release;
  1731. if (!host->ops->card_busy) {
  1732. mmc_delay(timeout_ms);
  1733. goto out_release;
  1734. }
  1735. err = __mmc_poll_for_busy(host, 0, timeout_ms, &mmc_sleep_busy_cb, host);
  1736. out_release:
  1737. mmc_retune_release(host);
  1738. return err;
  1739. }
  1740. static int mmc_can_poweroff_notify(const struct mmc_card *card)
  1741. {
  1742. return card &&
  1743. mmc_card_mmc(card) &&
  1744. (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
  1745. }
  1746. static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
  1747. {
  1748. unsigned int timeout = card->ext_csd.generic_cmd6_time;
  1749. int err;
  1750. /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
  1751. if (notify_type == EXT_CSD_POWER_OFF_LONG)
  1752. timeout = card->ext_csd.power_off_longtime;
  1753. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1754. EXT_CSD_POWER_OFF_NOTIFICATION,
  1755. notify_type, timeout, 0, false, false, MMC_CMD_RETRIES);
  1756. if (err)
  1757. pr_err("%s: Power Off Notification timed out, %u\n",
  1758. mmc_hostname(card->host), timeout);
  1759. /* Disable the power off notification after the switch operation. */
  1760. card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
  1761. return err;
  1762. }
  1763. /*
  1764. * Host is being removed. Free up the current card.
  1765. */
  1766. static void mmc_remove(struct mmc_host *host)
  1767. {
  1768. mmc_remove_card(host->card);
  1769. host->card = NULL;
  1770. }
  1771. /*
  1772. * Card detection - card is alive.
  1773. */
  1774. static int mmc_alive(struct mmc_host *host)
  1775. {
  1776. return mmc_send_status(host->card, NULL);
  1777. }
  1778. /*
  1779. * Card detection callback from host.
  1780. */
  1781. static void mmc_detect(struct mmc_host *host)
  1782. {
  1783. int err;
  1784. mmc_get_card(host->card, NULL);
  1785. /*
  1786. * Just check if our card has been removed.
  1787. */
  1788. err = _mmc_detect_card_removed(host);
  1789. mmc_put_card(host->card, NULL);
  1790. if (err) {
  1791. mmc_remove(host);
  1792. mmc_claim_host(host);
  1793. mmc_detach_bus(host);
  1794. mmc_power_off(host);
  1795. mmc_release_host(host);
  1796. }
  1797. }
  1798. static bool _mmc_cache_enabled(struct mmc_host *host)
  1799. {
  1800. return host->card->ext_csd.cache_size > 0 &&
  1801. host->card->ext_csd.cache_ctrl & 1;
  1802. }
  1803. /*
  1804. * Flush the internal cache of the eMMC to non-volatile storage.
  1805. */
  1806. static int _mmc_flush_cache(struct mmc_host *host)
  1807. {
  1808. int err = 0;
  1809. if (mmc_card_broken_cache_flush(host->card) && !host->card->written_flag)
  1810. return 0;
  1811. if (_mmc_cache_enabled(host)) {
  1812. err = mmc_switch(host->card, EXT_CSD_CMD_SET_NORMAL,
  1813. EXT_CSD_FLUSH_CACHE, 1,
  1814. CACHE_FLUSH_TIMEOUT_MS);
  1815. if (err)
  1816. pr_err("%s: cache flush error %d\n", mmc_hostname(host), err);
  1817. else
  1818. host->card->written_flag = false;
  1819. }
  1820. return err;
  1821. }
  1822. static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
  1823. {
  1824. int err = 0;
  1825. unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
  1826. EXT_CSD_POWER_OFF_LONG;
  1827. mmc_claim_host(host);
  1828. if (mmc_card_suspended(host->card))
  1829. goto out;
  1830. err = _mmc_flush_cache(host);
  1831. if (err)
  1832. goto out;
  1833. if (mmc_can_poweroff_notify(host->card) &&
  1834. ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend ||
  1835. (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND)))
  1836. err = mmc_poweroff_notify(host->card, notify_type);
  1837. else if (mmc_can_sleep(host->card))
  1838. err = mmc_sleep(host);
  1839. else if (!mmc_host_is_spi(host))
  1840. err = mmc_deselect_cards(host);
  1841. if (!err) {
  1842. mmc_power_off(host);
  1843. mmc_card_set_suspended(host->card);
  1844. }
  1845. out:
  1846. mmc_release_host(host);
  1847. return err;
  1848. }
  1849. /*
  1850. * Suspend callback
  1851. */
  1852. static int mmc_suspend(struct mmc_host *host)
  1853. {
  1854. int err;
  1855. err = _mmc_suspend(host, true);
  1856. if (!err) {
  1857. pm_runtime_disable(&host->card->dev);
  1858. pm_runtime_set_suspended(&host->card->dev);
  1859. }
  1860. return err;
  1861. }
  1862. /*
  1863. * This function tries to determine if the same card is still present
  1864. * and, if so, restore all state to it.
  1865. */
  1866. static int _mmc_resume(struct mmc_host *host)
  1867. {
  1868. int err = 0;
  1869. mmc_claim_host(host);
  1870. if (!mmc_card_suspended(host->card))
  1871. goto out;
  1872. mmc_power_up(host, host->card->ocr);
  1873. err = mmc_init_card(host, host->card->ocr, host->card);
  1874. mmc_card_clr_suspended(host->card);
  1875. out:
  1876. mmc_release_host(host);
  1877. return err;
  1878. }
  1879. /*
  1880. * Shutdown callback
  1881. */
  1882. static int mmc_shutdown(struct mmc_host *host)
  1883. {
  1884. int err = 0;
  1885. /*
  1886. * In a specific case for poweroff notify, we need to resume the card
  1887. * before we can shutdown it properly.
  1888. */
  1889. if (mmc_can_poweroff_notify(host->card) &&
  1890. !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
  1891. err = _mmc_resume(host);
  1892. if (!err)
  1893. err = _mmc_suspend(host, false);
  1894. return err;
  1895. }
  1896. /*
  1897. * Callback for resume.
  1898. */
  1899. static int mmc_resume(struct mmc_host *host)
  1900. {
  1901. pm_runtime_enable(&host->card->dev);
  1902. return 0;
  1903. }
  1904. /*
  1905. * Callback for runtime_suspend.
  1906. */
  1907. static int mmc_runtime_suspend(struct mmc_host *host)
  1908. {
  1909. int err;
  1910. if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
  1911. return 0;
  1912. err = _mmc_suspend(host, true);
  1913. if (err)
  1914. pr_err("%s: error %d doing aggressive suspend\n",
  1915. mmc_hostname(host), err);
  1916. return err;
  1917. }
  1918. /*
  1919. * Callback for runtime_resume.
  1920. */
  1921. static int mmc_runtime_resume(struct mmc_host *host)
  1922. {
  1923. int err;
  1924. err = _mmc_resume(host);
  1925. if (err && err != -ENOMEDIUM)
  1926. pr_err("%s: error %d doing runtime resume\n",
  1927. mmc_hostname(host), err);
  1928. return 0;
  1929. }
  1930. static int mmc_can_reset(struct mmc_card *card)
  1931. {
  1932. u8 rst_n_function;
  1933. rst_n_function = card->ext_csd.rst_n_function;
  1934. if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
  1935. return 0;
  1936. return 1;
  1937. }
  1938. static int _mmc_hw_reset(struct mmc_host *host)
  1939. {
  1940. struct mmc_card *card = host->card;
  1941. /*
  1942. * In the case of recovery, we can't expect flushing the cache to work
  1943. * always, but we have a go and ignore errors.
  1944. */
  1945. _mmc_flush_cache(host);
  1946. if ((host->caps & MMC_CAP_HW_RESET) && host->ops->card_hw_reset &&
  1947. mmc_can_reset(card)) {
  1948. /* If the card accept RST_n signal, send it. */
  1949. mmc_set_clock(host, host->f_init);
  1950. host->ops->card_hw_reset(host);
  1951. /* Set initial state and call mmc_set_ios */
  1952. mmc_set_initial_state(host);
  1953. } else {
  1954. /* Do a brute force power cycle */
  1955. mmc_power_cycle(host, card->ocr);
  1956. mmc_pwrseq_reset(host);
  1957. }
  1958. return mmc_init_card(host, card->ocr, card);
  1959. }
  1960. static const struct mmc_bus_ops mmc_ops = {
  1961. .remove = mmc_remove,
  1962. .detect = mmc_detect,
  1963. .suspend = mmc_suspend,
  1964. .resume = mmc_resume,
  1965. .runtime_suspend = mmc_runtime_suspend,
  1966. .runtime_resume = mmc_runtime_resume,
  1967. .alive = mmc_alive,
  1968. .shutdown = mmc_shutdown,
  1969. .hw_reset = _mmc_hw_reset,
  1970. .cache_enabled = _mmc_cache_enabled,
  1971. .flush_cache = _mmc_flush_cache,
  1972. };
  1973. /*
  1974. * Starting point for MMC card init.
  1975. */
  1976. int mmc_attach_mmc(struct mmc_host *host)
  1977. {
  1978. int err;
  1979. u32 ocr, rocr;
  1980. WARN_ON(!host->claimed);
  1981. /* Set correct bus mode for MMC before attempting attach */
  1982. if (!mmc_host_is_spi(host))
  1983. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1984. err = mmc_send_op_cond(host, 0, &ocr);
  1985. if (err)
  1986. return err;
  1987. mmc_attach_bus(host, &mmc_ops);
  1988. if (host->ocr_avail_mmc)
  1989. host->ocr_avail = host->ocr_avail_mmc;
  1990. /*
  1991. * We need to get OCR a different way for SPI.
  1992. */
  1993. if (mmc_host_is_spi(host)) {
  1994. err = mmc_spi_read_ocr(host, 1, &ocr);
  1995. if (err)
  1996. goto err;
  1997. }
  1998. rocr = mmc_select_voltage(host, ocr);
  1999. /*
  2000. * Can we support the voltage of the card?
  2001. */
  2002. if (!rocr) {
  2003. err = -EINVAL;
  2004. goto err;
  2005. }
  2006. /*
  2007. * Detect and init the card.
  2008. */
  2009. err = mmc_init_card(host, rocr, NULL);
  2010. if (err)
  2011. goto err;
  2012. mmc_release_host(host);
  2013. err = mmc_add_card(host->card);
  2014. if (err)
  2015. goto remove_card;
  2016. mmc_claim_host(host);
  2017. return 0;
  2018. remove_card:
  2019. mmc_remove_card(host->card);
  2020. mmc_claim_host(host);
  2021. host->card = NULL;
  2022. err:
  2023. mmc_detach_bus(host);
  2024. pr_err("%s: error %d whilst initialising MMC card\n",
  2025. mmc_hostname(host), err);
  2026. return err;
  2027. }