mmc.c 65 KB

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