mmc.c 57 KB

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