mmc.c 55 KB

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