mmc.c 56 KB

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