mmc.c 49 KB

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