mmc.c 22 KB

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