mmc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/mmc.h>
  17. #include "core.h"
  18. #include "bus.h"
  19. #include "mmc_ops.h"
  20. #include "sd_ops.h"
  21. static const unsigned int tran_exp[] = {
  22. 10000, 100000, 1000000, 10000000,
  23. 0, 0, 0, 0
  24. };
  25. static const unsigned char tran_mant[] = {
  26. 0, 10, 12, 13, 15, 20, 25, 30,
  27. 35, 40, 45, 50, 55, 60, 70, 80,
  28. };
  29. static const unsigned int tacc_exp[] = {
  30. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  31. };
  32. static const unsigned int tacc_mant[] = {
  33. 0, 10, 12, 13, 15, 20, 25, 30,
  34. 35, 40, 45, 50, 55, 60, 70, 80,
  35. };
  36. #define UNSTUFF_BITS(resp,start,size) \
  37. ({ \
  38. const int __size = size; \
  39. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  40. const int __off = 3 - ((start) / 32); \
  41. const int __shft = (start) & 31; \
  42. u32 __res; \
  43. \
  44. __res = resp[__off] >> __shft; \
  45. if (__size + __shft > 32) \
  46. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  47. __res & __mask; \
  48. })
  49. /*
  50. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  51. */
  52. static int mmc_decode_cid(struct mmc_card *card)
  53. {
  54. u32 *resp = card->raw_cid;
  55. /*
  56. * The selection of the format here is based upon published
  57. * specs from sandisk and from what people have reported.
  58. */
  59. switch (card->csd.mmca_vsn) {
  60. case 0: /* MMC v1.0 - v1.2 */
  61. case 1: /* MMC v1.4 */
  62. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  63. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  64. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  65. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  66. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  67. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  68. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  69. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  70. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  71. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  72. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  73. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  74. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  75. break;
  76. case 2: /* MMC v2.0 - v2.2 */
  77. case 3: /* MMC v3.1 - v3.3 */
  78. case 4: /* MMC v4 */
  79. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  80. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  81. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  82. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  83. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  84. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  85. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  86. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  87. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  88. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  89. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  90. break;
  91. default:
  92. printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
  93. mmc_hostname(card->host), card->csd.mmca_vsn);
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static void mmc_set_erase_size(struct mmc_card *card)
  99. {
  100. if (card->ext_csd.erase_group_def & 1)
  101. card->erase_size = card->ext_csd.hc_erase_size;
  102. else
  103. card->erase_size = card->csd.erase_size;
  104. mmc_init_erase(card);
  105. }
  106. /*
  107. * Given a 128-bit response, decode to our card CSD structure.
  108. */
  109. static int mmc_decode_csd(struct mmc_card *card)
  110. {
  111. struct mmc_csd *csd = &card->csd;
  112. unsigned int e, m, a, b;
  113. u32 *resp = card->raw_csd;
  114. /*
  115. * We only understand CSD structure v1.1 and v1.2.
  116. * v1.2 has extra information in bits 15, 11 and 10.
  117. * We also support eMMC v4.4 & v4.41.
  118. */
  119. csd->structure = UNSTUFF_BITS(resp, 126, 2);
  120. if (csd->structure == 0) {
  121. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  122. mmc_hostname(card->host), csd->structure);
  123. return -EINVAL;
  124. }
  125. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  126. m = UNSTUFF_BITS(resp, 115, 4);
  127. e = UNSTUFF_BITS(resp, 112, 3);
  128. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  129. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  130. m = UNSTUFF_BITS(resp, 99, 4);
  131. e = UNSTUFF_BITS(resp, 96, 3);
  132. csd->max_dtr = tran_exp[e] * tran_mant[m];
  133. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  134. e = UNSTUFF_BITS(resp, 47, 3);
  135. m = UNSTUFF_BITS(resp, 62, 12);
  136. csd->capacity = (1 + m) << (e + 2);
  137. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  138. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  139. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  140. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  141. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  142. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  143. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  144. if (csd->write_blkbits >= 9) {
  145. a = UNSTUFF_BITS(resp, 42, 5);
  146. b = UNSTUFF_BITS(resp, 37, 5);
  147. csd->erase_size = (a + 1) * (b + 1);
  148. csd->erase_size <<= csd->write_blkbits - 9;
  149. }
  150. return 0;
  151. }
  152. /*
  153. * Read extended CSD.
  154. */
  155. static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
  156. {
  157. int err;
  158. u8 *ext_csd;
  159. BUG_ON(!card);
  160. BUG_ON(!new_ext_csd);
  161. *new_ext_csd = NULL;
  162. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  163. return 0;
  164. /*
  165. * As the ext_csd is so large and mostly unused, we don't store the
  166. * raw block in mmc_card.
  167. */
  168. ext_csd = kmalloc(512, GFP_KERNEL);
  169. if (!ext_csd) {
  170. printk(KERN_ERR "%s: could not allocate a buffer to "
  171. "receive the ext_csd.\n", mmc_hostname(card->host));
  172. return -ENOMEM;
  173. }
  174. err = mmc_send_ext_csd(card, ext_csd);
  175. if (err) {
  176. kfree(ext_csd);
  177. *new_ext_csd = NULL;
  178. /* If the host or the card can't do the switch,
  179. * fail more gracefully. */
  180. if ((err != -EINVAL)
  181. && (err != -ENOSYS)
  182. && (err != -EFAULT))
  183. return err;
  184. /*
  185. * High capacity cards should have this "magic" size
  186. * stored in their CSD.
  187. */
  188. if (card->csd.capacity == (4096 * 512)) {
  189. printk(KERN_ERR "%s: unable to read EXT_CSD "
  190. "on a possible high capacity card. "
  191. "Card will be ignored.\n",
  192. mmc_hostname(card->host));
  193. } else {
  194. printk(KERN_WARNING "%s: unable to read "
  195. "EXT_CSD, performance might "
  196. "suffer.\n",
  197. mmc_hostname(card->host));
  198. err = 0;
  199. }
  200. } else
  201. *new_ext_csd = ext_csd;
  202. return err;
  203. }
  204. /*
  205. * Decode extended CSD.
  206. */
  207. static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
  208. {
  209. int err = 0;
  210. BUG_ON(!card);
  211. if (!ext_csd)
  212. return 0;
  213. /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
  214. card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
  215. if (card->csd.structure == 3) {
  216. if (card->ext_csd.raw_ext_csd_structure > 2) {
  217. printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
  218. "version %d\n", mmc_hostname(card->host),
  219. card->ext_csd.raw_ext_csd_structure);
  220. err = -EINVAL;
  221. goto out;
  222. }
  223. }
  224. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  225. if (card->ext_csd.rev > 6) {
  226. printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
  227. mmc_hostname(card->host), card->ext_csd.rev);
  228. err = -EINVAL;
  229. goto out;
  230. }
  231. card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
  232. card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
  233. card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
  234. card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
  235. if (card->ext_csd.rev >= 2) {
  236. card->ext_csd.sectors =
  237. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  238. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  239. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  240. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  241. /* Cards with density > 2GiB are sector addressed */
  242. if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
  243. mmc_card_set_blockaddr(card);
  244. }
  245. card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
  246. switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
  247. case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
  248. EXT_CSD_CARD_TYPE_26:
  249. card->ext_csd.hs_max_dtr = 52000000;
  250. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
  251. break;
  252. case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
  253. EXT_CSD_CARD_TYPE_26:
  254. card->ext_csd.hs_max_dtr = 52000000;
  255. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
  256. break;
  257. case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
  258. EXT_CSD_CARD_TYPE_26:
  259. card->ext_csd.hs_max_dtr = 52000000;
  260. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
  261. break;
  262. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  263. card->ext_csd.hs_max_dtr = 52000000;
  264. break;
  265. case EXT_CSD_CARD_TYPE_26:
  266. card->ext_csd.hs_max_dtr = 26000000;
  267. break;
  268. default:
  269. /* MMC v4 spec says this cannot happen */
  270. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  271. "support any high-speed modes.\n",
  272. mmc_hostname(card->host));
  273. }
  274. card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
  275. card->ext_csd.raw_erase_timeout_mult =
  276. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  277. card->ext_csd.raw_hc_erase_grp_size =
  278. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  279. if (card->ext_csd.rev >= 3) {
  280. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  281. card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
  282. /* EXT_CSD value is in units of 10ms, but we store in ms */
  283. card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
  284. /* Sleep / awake timeout in 100ns units */
  285. if (sa_shift > 0 && sa_shift <= 0x17)
  286. card->ext_csd.sa_timeout =
  287. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  288. card->ext_csd.erase_group_def =
  289. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  290. card->ext_csd.hc_erase_timeout = 300 *
  291. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  292. card->ext_csd.hc_erase_size =
  293. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  294. card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
  295. /*
  296. * There are two boot regions of equal size, defined in
  297. * multiples of 128K.
  298. */
  299. card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  300. }
  301. card->ext_csd.raw_hc_erase_gap_size =
  302. ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
  303. card->ext_csd.raw_sec_trim_mult =
  304. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  305. card->ext_csd.raw_sec_erase_mult =
  306. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  307. card->ext_csd.raw_sec_feature_support =
  308. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  309. card->ext_csd.raw_trim_mult =
  310. ext_csd[EXT_CSD_TRIM_MULT];
  311. if (card->ext_csd.rev >= 4) {
  312. /*
  313. * Enhanced area feature support -- check whether the eMMC
  314. * card has the Enhanced area enabled. If so, export enhanced
  315. * area offset and size to user by adding sysfs interface.
  316. */
  317. if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
  318. (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
  319. u8 hc_erase_grp_sz =
  320. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  321. u8 hc_wp_grp_sz =
  322. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  323. card->ext_csd.enhanced_area_en = 1;
  324. /*
  325. * calculate the enhanced data area offset, in bytes
  326. */
  327. card->ext_csd.enhanced_area_offset =
  328. (ext_csd[139] << 24) + (ext_csd[138] << 16) +
  329. (ext_csd[137] << 8) + ext_csd[136];
  330. if (mmc_card_blockaddr(card))
  331. card->ext_csd.enhanced_area_offset <<= 9;
  332. /*
  333. * calculate the enhanced data area size, in kilobytes
  334. */
  335. card->ext_csd.enhanced_area_size =
  336. (ext_csd[142] << 16) + (ext_csd[141] << 8) +
  337. ext_csd[140];
  338. card->ext_csd.enhanced_area_size *=
  339. (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
  340. card->ext_csd.enhanced_area_size <<= 9;
  341. } else {
  342. /*
  343. * If the enhanced area is not enabled, disable these
  344. * device attributes.
  345. */
  346. card->ext_csd.enhanced_area_offset = -EINVAL;
  347. card->ext_csd.enhanced_area_size = -EINVAL;
  348. }
  349. card->ext_csd.sec_trim_mult =
  350. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  351. card->ext_csd.sec_erase_mult =
  352. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  353. card->ext_csd.sec_feature_support =
  354. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  355. card->ext_csd.trim_timeout = 300 *
  356. ext_csd[EXT_CSD_TRIM_MULT];
  357. }
  358. if (card->ext_csd.rev >= 5)
  359. card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
  360. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  361. card->erased_byte = 0xFF;
  362. else
  363. card->erased_byte = 0x0;
  364. out:
  365. return err;
  366. }
  367. static inline void mmc_free_ext_csd(u8 *ext_csd)
  368. {
  369. kfree(ext_csd);
  370. }
  371. static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
  372. {
  373. u8 *bw_ext_csd;
  374. int err;
  375. if (bus_width == MMC_BUS_WIDTH_1)
  376. return 0;
  377. err = mmc_get_ext_csd(card, &bw_ext_csd);
  378. if (err || bw_ext_csd == NULL) {
  379. if (bus_width != MMC_BUS_WIDTH_1)
  380. err = -EINVAL;
  381. goto out;
  382. }
  383. if (bus_width == MMC_BUS_WIDTH_1)
  384. goto out;
  385. /* only compare read only fields */
  386. err = (!(card->ext_csd.raw_partition_support ==
  387. bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  388. (card->ext_csd.raw_erased_mem_count ==
  389. bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  390. (card->ext_csd.rev ==
  391. bw_ext_csd[EXT_CSD_REV]) &&
  392. (card->ext_csd.raw_ext_csd_structure ==
  393. bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  394. (card->ext_csd.raw_card_type ==
  395. bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  396. (card->ext_csd.raw_s_a_timeout ==
  397. bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  398. (card->ext_csd.raw_hc_erase_gap_size ==
  399. bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  400. (card->ext_csd.raw_erase_timeout_mult ==
  401. bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  402. (card->ext_csd.raw_hc_erase_grp_size ==
  403. bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  404. (card->ext_csd.raw_sec_trim_mult ==
  405. bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  406. (card->ext_csd.raw_sec_erase_mult ==
  407. bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  408. (card->ext_csd.raw_sec_feature_support ==
  409. bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  410. (card->ext_csd.raw_trim_mult ==
  411. bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  412. (card->ext_csd.raw_sectors[0] ==
  413. bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  414. (card->ext_csd.raw_sectors[1] ==
  415. bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  416. (card->ext_csd.raw_sectors[2] ==
  417. bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  418. (card->ext_csd.raw_sectors[3] ==
  419. bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
  420. if (err)
  421. err = -EINVAL;
  422. out:
  423. mmc_free_ext_csd(bw_ext_csd);
  424. return err;
  425. }
  426. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  427. card->raw_cid[2], card->raw_cid[3]);
  428. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  429. card->raw_csd[2], card->raw_csd[3]);
  430. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  431. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  432. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  433. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  434. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  435. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  436. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  437. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  438. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  439. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  440. card->ext_csd.enhanced_area_offset);
  441. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  442. static struct attribute *mmc_std_attrs[] = {
  443. &dev_attr_cid.attr,
  444. &dev_attr_csd.attr,
  445. &dev_attr_date.attr,
  446. &dev_attr_erase_size.attr,
  447. &dev_attr_preferred_erase_size.attr,
  448. &dev_attr_fwrev.attr,
  449. &dev_attr_hwrev.attr,
  450. &dev_attr_manfid.attr,
  451. &dev_attr_name.attr,
  452. &dev_attr_oemid.attr,
  453. &dev_attr_serial.attr,
  454. &dev_attr_enhanced_area_offset.attr,
  455. &dev_attr_enhanced_area_size.attr,
  456. NULL,
  457. };
  458. static struct attribute_group mmc_std_attr_group = {
  459. .attrs = mmc_std_attrs,
  460. };
  461. static const struct attribute_group *mmc_attr_groups[] = {
  462. &mmc_std_attr_group,
  463. NULL,
  464. };
  465. static struct device_type mmc_type = {
  466. .groups = mmc_attr_groups,
  467. };
  468. /*
  469. * Handle the detection and initialisation of a card.
  470. *
  471. * In the case of a resume, "oldcard" will contain the card
  472. * we're trying to reinitialise.
  473. */
  474. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  475. struct mmc_card *oldcard)
  476. {
  477. struct mmc_card *card;
  478. int err, ddr = 0;
  479. u32 cid[4];
  480. unsigned int max_dtr;
  481. u32 rocr;
  482. u8 *ext_csd = NULL;
  483. BUG_ON(!host);
  484. WARN_ON(!host->claimed);
  485. /* Set correct bus mode for MMC before attempting init */
  486. if (!mmc_host_is_spi(host))
  487. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  488. /*
  489. * Since we're changing the OCR value, we seem to
  490. * need to tell some cards to go back to the idle
  491. * state. We wait 1ms to give cards time to
  492. * respond.
  493. * mmc_go_idle is needed for eMMC that are asleep
  494. */
  495. mmc_go_idle(host);
  496. /* The extra bit indicates that we support high capacity */
  497. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  498. if (err)
  499. goto err;
  500. /*
  501. * For SPI, enable CRC as appropriate.
  502. */
  503. if (mmc_host_is_spi(host)) {
  504. err = mmc_spi_set_crc(host, use_spi_crc);
  505. if (err)
  506. goto err;
  507. }
  508. /*
  509. * Fetch CID from card.
  510. */
  511. if (mmc_host_is_spi(host))
  512. err = mmc_send_cid(host, cid);
  513. else
  514. err = mmc_all_send_cid(host, cid);
  515. if (err)
  516. goto err;
  517. if (oldcard) {
  518. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  519. err = -ENOENT;
  520. goto err;
  521. }
  522. card = oldcard;
  523. } else {
  524. /*
  525. * Allocate card structure.
  526. */
  527. card = mmc_alloc_card(host, &mmc_type);
  528. if (IS_ERR(card)) {
  529. err = PTR_ERR(card);
  530. goto err;
  531. }
  532. card->type = MMC_TYPE_MMC;
  533. card->rca = 1;
  534. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  535. }
  536. /*
  537. * For native busses: set card RCA and quit open drain mode.
  538. */
  539. if (!mmc_host_is_spi(host)) {
  540. err = mmc_set_relative_addr(card);
  541. if (err)
  542. goto free_card;
  543. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  544. }
  545. if (!oldcard) {
  546. /*
  547. * Fetch CSD from card.
  548. */
  549. err = mmc_send_csd(card, card->raw_csd);
  550. if (err)
  551. goto free_card;
  552. err = mmc_decode_csd(card);
  553. if (err)
  554. goto free_card;
  555. err = mmc_decode_cid(card);
  556. if (err)
  557. goto free_card;
  558. }
  559. /*
  560. * Select card, as all following commands rely on that.
  561. */
  562. if (!mmc_host_is_spi(host)) {
  563. err = mmc_select_card(card);
  564. if (err)
  565. goto free_card;
  566. }
  567. if (!oldcard) {
  568. /*
  569. * Fetch and process extended CSD.
  570. */
  571. err = mmc_get_ext_csd(card, &ext_csd);
  572. if (err)
  573. goto free_card;
  574. err = mmc_read_ext_csd(card, ext_csd);
  575. if (err)
  576. goto free_card;
  577. /* If doing byte addressing, check if required to do sector
  578. * addressing. Handle the case of <2GB cards needing sector
  579. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  580. * ocr register has bit 30 set for sector addressing.
  581. */
  582. if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
  583. mmc_card_set_blockaddr(card);
  584. /* Erase size depends on CSD and Extended CSD */
  585. mmc_set_erase_size(card);
  586. }
  587. /*
  588. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  589. * bit. This bit will be lost every time after a reset or power off.
  590. */
  591. if (card->ext_csd.enhanced_area_en) {
  592. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  593. EXT_CSD_ERASE_GROUP_DEF, 1, 0);
  594. if (err && err != -EBADMSG)
  595. goto free_card;
  596. if (err) {
  597. err = 0;
  598. /*
  599. * Just disable enhanced area off & sz
  600. * will try to enable ERASE_GROUP_DEF
  601. * during next time reinit
  602. */
  603. card->ext_csd.enhanced_area_offset = -EINVAL;
  604. card->ext_csd.enhanced_area_size = -EINVAL;
  605. } else {
  606. card->ext_csd.erase_group_def = 1;
  607. /*
  608. * enable ERASE_GRP_DEF successfully.
  609. * This will affect the erase size, so
  610. * here need to reset erase size
  611. */
  612. mmc_set_erase_size(card);
  613. }
  614. }
  615. /*
  616. * Ensure eMMC user default partition is enabled
  617. */
  618. if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
  619. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  620. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
  621. card->ext_csd.part_config,
  622. card->ext_csd.part_time);
  623. if (err && err != -EBADMSG)
  624. goto free_card;
  625. }
  626. /*
  627. * Activate high speed (if supported)
  628. */
  629. if ((card->ext_csd.hs_max_dtr != 0) &&
  630. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  631. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  632. EXT_CSD_HS_TIMING, 1, 0);
  633. if (err && err != -EBADMSG)
  634. goto free_card;
  635. if (err) {
  636. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  637. mmc_hostname(card->host));
  638. err = 0;
  639. } else {
  640. mmc_card_set_highspeed(card);
  641. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  642. }
  643. }
  644. /*
  645. * Compute bus speed.
  646. */
  647. max_dtr = (unsigned int)-1;
  648. if (mmc_card_highspeed(card)) {
  649. if (max_dtr > card->ext_csd.hs_max_dtr)
  650. max_dtr = card->ext_csd.hs_max_dtr;
  651. } else if (max_dtr > card->csd.max_dtr) {
  652. max_dtr = card->csd.max_dtr;
  653. }
  654. mmc_set_clock(host, max_dtr);
  655. /*
  656. * Indicate DDR mode (if supported).
  657. */
  658. if (mmc_card_highspeed(card)) {
  659. if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
  660. && ((host->caps & (MMC_CAP_1_8V_DDR |
  661. MMC_CAP_UHS_DDR50))
  662. == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
  663. ddr = MMC_1_8V_DDR_MODE;
  664. else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
  665. && ((host->caps & (MMC_CAP_1_2V_DDR |
  666. MMC_CAP_UHS_DDR50))
  667. == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
  668. ddr = MMC_1_2V_DDR_MODE;
  669. }
  670. /*
  671. * Activate wide bus and DDR (if supported).
  672. */
  673. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  674. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  675. static unsigned ext_csd_bits[][2] = {
  676. { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
  677. { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
  678. { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
  679. };
  680. static unsigned bus_widths[] = {
  681. MMC_BUS_WIDTH_8,
  682. MMC_BUS_WIDTH_4,
  683. MMC_BUS_WIDTH_1
  684. };
  685. unsigned idx, bus_width = 0;
  686. if (host->caps & MMC_CAP_8_BIT_DATA)
  687. idx = 0;
  688. else
  689. idx = 1;
  690. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  691. bus_width = bus_widths[idx];
  692. if (bus_width == MMC_BUS_WIDTH_1)
  693. ddr = 0; /* no DDR for 1-bit width */
  694. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  695. EXT_CSD_BUS_WIDTH,
  696. ext_csd_bits[idx][0],
  697. 0);
  698. if (!err) {
  699. mmc_set_bus_width(card->host, bus_width);
  700. /*
  701. * If controller can't handle bus width test,
  702. * compare ext_csd previously read in 1 bit mode
  703. * against ext_csd at new bus width
  704. */
  705. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  706. err = mmc_compare_ext_csds(card,
  707. bus_width);
  708. else
  709. err = mmc_bus_test(card, bus_width);
  710. if (!err)
  711. break;
  712. }
  713. }
  714. if (!err && ddr) {
  715. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  716. EXT_CSD_BUS_WIDTH,
  717. ext_csd_bits[idx][1],
  718. 0);
  719. }
  720. if (err) {
  721. printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
  722. "failed\n", mmc_hostname(card->host),
  723. 1 << bus_width, ddr);
  724. goto free_card;
  725. } else if (ddr) {
  726. /*
  727. * eMMC cards can support 3.3V to 1.2V i/o (vccq)
  728. * signaling.
  729. *
  730. * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
  731. *
  732. * 1.8V vccq at 3.3V core voltage (vcc) is not required
  733. * in the JEDEC spec for DDR.
  734. *
  735. * Do not force change in vccq since we are obviously
  736. * working and no change to vccq is needed.
  737. *
  738. * WARNING: eMMC rules are NOT the same as SD DDR
  739. */
  740. if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
  741. err = mmc_set_signal_voltage(host,
  742. MMC_SIGNAL_VOLTAGE_120, 0);
  743. if (err)
  744. goto err;
  745. }
  746. mmc_card_set_ddr_mode(card);
  747. mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
  748. mmc_set_bus_width(card->host, bus_width);
  749. }
  750. }
  751. if (!oldcard)
  752. host->card = card;
  753. mmc_free_ext_csd(ext_csd);
  754. return 0;
  755. free_card:
  756. if (!oldcard)
  757. mmc_remove_card(card);
  758. err:
  759. mmc_free_ext_csd(ext_csd);
  760. return err;
  761. }
  762. /*
  763. * Host is being removed. Free up the current card.
  764. */
  765. static void mmc_remove(struct mmc_host *host)
  766. {
  767. BUG_ON(!host);
  768. BUG_ON(!host->card);
  769. mmc_remove_card(host->card);
  770. host->card = NULL;
  771. }
  772. /*
  773. * Card detection callback from host.
  774. */
  775. static void mmc_detect(struct mmc_host *host)
  776. {
  777. int err;
  778. BUG_ON(!host);
  779. BUG_ON(!host->card);
  780. mmc_claim_host(host);
  781. /*
  782. * Just check if our card has been removed.
  783. */
  784. err = mmc_send_status(host->card, NULL);
  785. mmc_release_host(host);
  786. if (err) {
  787. mmc_remove(host);
  788. mmc_claim_host(host);
  789. mmc_detach_bus(host);
  790. mmc_power_off(host);
  791. mmc_release_host(host);
  792. }
  793. }
  794. /*
  795. * Suspend callback from host.
  796. */
  797. static int mmc_suspend(struct mmc_host *host)
  798. {
  799. int err = 0;
  800. BUG_ON(!host);
  801. BUG_ON(!host->card);
  802. mmc_claim_host(host);
  803. if (mmc_card_can_sleep(host))
  804. err = mmc_card_sleep(host);
  805. else if (!mmc_host_is_spi(host))
  806. mmc_deselect_cards(host);
  807. host->card->state &= ~MMC_STATE_HIGHSPEED;
  808. mmc_release_host(host);
  809. return err;
  810. }
  811. /*
  812. * Resume callback from host.
  813. *
  814. * This function tries to determine if the same card is still present
  815. * and, if so, restore all state to it.
  816. */
  817. static int mmc_resume(struct mmc_host *host)
  818. {
  819. int err;
  820. BUG_ON(!host);
  821. BUG_ON(!host->card);
  822. mmc_claim_host(host);
  823. err = mmc_init_card(host, host->ocr, host->card);
  824. mmc_release_host(host);
  825. return err;
  826. }
  827. static int mmc_power_restore(struct mmc_host *host)
  828. {
  829. int ret;
  830. host->card->state &= ~MMC_STATE_HIGHSPEED;
  831. mmc_claim_host(host);
  832. ret = mmc_init_card(host, host->ocr, host->card);
  833. mmc_release_host(host);
  834. return ret;
  835. }
  836. static int mmc_sleep(struct mmc_host *host)
  837. {
  838. struct mmc_card *card = host->card;
  839. int err = -ENOSYS;
  840. if (card && card->ext_csd.rev >= 3) {
  841. err = mmc_card_sleepawake(host, 1);
  842. if (err < 0)
  843. pr_debug("%s: Error %d while putting card into sleep",
  844. mmc_hostname(host), err);
  845. }
  846. return err;
  847. }
  848. static int mmc_awake(struct mmc_host *host)
  849. {
  850. struct mmc_card *card = host->card;
  851. int err = -ENOSYS;
  852. if (card && card->ext_csd.rev >= 3) {
  853. err = mmc_card_sleepawake(host, 0);
  854. if (err < 0)
  855. pr_debug("%s: Error %d while awaking sleeping card",
  856. mmc_hostname(host), err);
  857. }
  858. return err;
  859. }
  860. static const struct mmc_bus_ops mmc_ops = {
  861. .awake = mmc_awake,
  862. .sleep = mmc_sleep,
  863. .remove = mmc_remove,
  864. .detect = mmc_detect,
  865. .suspend = NULL,
  866. .resume = NULL,
  867. .power_restore = mmc_power_restore,
  868. };
  869. static const struct mmc_bus_ops mmc_ops_unsafe = {
  870. .awake = mmc_awake,
  871. .sleep = mmc_sleep,
  872. .remove = mmc_remove,
  873. .detect = mmc_detect,
  874. .suspend = mmc_suspend,
  875. .resume = mmc_resume,
  876. .power_restore = mmc_power_restore,
  877. };
  878. static void mmc_attach_bus_ops(struct mmc_host *host)
  879. {
  880. const struct mmc_bus_ops *bus_ops;
  881. if (!mmc_card_is_removable(host))
  882. bus_ops = &mmc_ops_unsafe;
  883. else
  884. bus_ops = &mmc_ops;
  885. mmc_attach_bus(host, bus_ops);
  886. }
  887. /*
  888. * Starting point for MMC card init.
  889. */
  890. int mmc_attach_mmc(struct mmc_host *host)
  891. {
  892. int err;
  893. u32 ocr;
  894. BUG_ON(!host);
  895. WARN_ON(!host->claimed);
  896. /* Set correct bus mode for MMC before attempting attach */
  897. if (!mmc_host_is_spi(host))
  898. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  899. err = mmc_send_op_cond(host, 0, &ocr);
  900. if (err)
  901. return err;
  902. mmc_attach_bus_ops(host);
  903. if (host->ocr_avail_mmc)
  904. host->ocr_avail = host->ocr_avail_mmc;
  905. /*
  906. * We need to get OCR a different way for SPI.
  907. */
  908. if (mmc_host_is_spi(host)) {
  909. err = mmc_spi_read_ocr(host, 1, &ocr);
  910. if (err)
  911. goto err;
  912. }
  913. /*
  914. * Sanity check the voltages that the card claims to
  915. * support.
  916. */
  917. if (ocr & 0x7F) {
  918. printk(KERN_WARNING "%s: card claims to support voltages "
  919. "below the defined range. These will be ignored.\n",
  920. mmc_hostname(host));
  921. ocr &= ~0x7F;
  922. }
  923. host->ocr = mmc_select_voltage(host, ocr);
  924. /*
  925. * Can we support the voltage of the card?
  926. */
  927. if (!host->ocr) {
  928. err = -EINVAL;
  929. goto err;
  930. }
  931. /*
  932. * Detect and init the card.
  933. */
  934. err = mmc_init_card(host, host->ocr, NULL);
  935. if (err)
  936. goto err;
  937. mmc_release_host(host);
  938. err = mmc_add_card(host->card);
  939. mmc_claim_host(host);
  940. if (err)
  941. goto remove_card;
  942. return 0;
  943. remove_card:
  944. mmc_release_host(host);
  945. mmc_remove_card(host->card);
  946. mmc_claim_host(host);
  947. host->card = NULL;
  948. err:
  949. mmc_detach_bus(host);
  950. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  951. mmc_hostname(host), err);
  952. return err;
  953. }