mmc.c 50 KB

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