mmc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. /* Sleep / awake timeout in 100ns units */
  256. if (sa_shift > 0 && sa_shift <= 0x17)
  257. card->ext_csd.sa_timeout =
  258. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  259. card->ext_csd.erase_group_def =
  260. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  261. card->ext_csd.hc_erase_timeout = 300 *
  262. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  263. card->ext_csd.hc_erase_size =
  264. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  265. }
  266. if (card->ext_csd.rev >= 4) {
  267. /*
  268. * Enhanced area feature support -- check whether the eMMC
  269. * card has the Enhanced area enabled. If so, export enhanced
  270. * area offset and size to user by adding sysfs interface.
  271. */
  272. if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
  273. (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
  274. u8 hc_erase_grp_sz =
  275. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  276. u8 hc_wp_grp_sz =
  277. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  278. card->ext_csd.enhanced_area_en = 1;
  279. /*
  280. * calculate the enhanced data area offset, in bytes
  281. */
  282. card->ext_csd.enhanced_area_offset =
  283. (ext_csd[139] << 24) + (ext_csd[138] << 16) +
  284. (ext_csd[137] << 8) + ext_csd[136];
  285. if (mmc_card_blockaddr(card))
  286. card->ext_csd.enhanced_area_offset <<= 9;
  287. /*
  288. * calculate the enhanced data area size, in kilobytes
  289. */
  290. card->ext_csd.enhanced_area_size =
  291. (ext_csd[142] << 16) + (ext_csd[141] << 8) +
  292. ext_csd[140];
  293. card->ext_csd.enhanced_area_size *=
  294. (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
  295. card->ext_csd.enhanced_area_size <<= 9;
  296. } else {
  297. /*
  298. * If the enhanced area is not enabled, disable these
  299. * device attributes.
  300. */
  301. card->ext_csd.enhanced_area_offset = -EINVAL;
  302. card->ext_csd.enhanced_area_size = -EINVAL;
  303. }
  304. card->ext_csd.sec_trim_mult =
  305. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  306. card->ext_csd.sec_erase_mult =
  307. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  308. card->ext_csd.sec_feature_support =
  309. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  310. card->ext_csd.trim_timeout = 300 *
  311. ext_csd[EXT_CSD_TRIM_MULT];
  312. }
  313. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  314. card->erased_byte = 0xFF;
  315. else
  316. card->erased_byte = 0x0;
  317. out:
  318. kfree(ext_csd);
  319. return err;
  320. }
  321. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  322. card->raw_cid[2], card->raw_cid[3]);
  323. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  324. card->raw_csd[2], card->raw_csd[3]);
  325. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  326. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  327. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  328. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  329. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  330. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  331. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  332. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  333. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  334. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  335. card->ext_csd.enhanced_area_offset);
  336. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  337. static struct attribute *mmc_std_attrs[] = {
  338. &dev_attr_cid.attr,
  339. &dev_attr_csd.attr,
  340. &dev_attr_date.attr,
  341. &dev_attr_erase_size.attr,
  342. &dev_attr_preferred_erase_size.attr,
  343. &dev_attr_fwrev.attr,
  344. &dev_attr_hwrev.attr,
  345. &dev_attr_manfid.attr,
  346. &dev_attr_name.attr,
  347. &dev_attr_oemid.attr,
  348. &dev_attr_serial.attr,
  349. &dev_attr_enhanced_area_offset.attr,
  350. &dev_attr_enhanced_area_size.attr,
  351. NULL,
  352. };
  353. static struct attribute_group mmc_std_attr_group = {
  354. .attrs = mmc_std_attrs,
  355. };
  356. static const struct attribute_group *mmc_attr_groups[] = {
  357. &mmc_std_attr_group,
  358. NULL,
  359. };
  360. static struct device_type mmc_type = {
  361. .groups = mmc_attr_groups,
  362. };
  363. /*
  364. * Handle the detection and initialisation of a card.
  365. *
  366. * In the case of a resume, "oldcard" will contain the card
  367. * we're trying to reinitialise.
  368. */
  369. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  370. struct mmc_card *oldcard)
  371. {
  372. struct mmc_card *card;
  373. int err, ddr = 0;
  374. u32 cid[4];
  375. unsigned int max_dtr;
  376. u32 rocr;
  377. BUG_ON(!host);
  378. WARN_ON(!host->claimed);
  379. /*
  380. * Since we're changing the OCR value, we seem to
  381. * need to tell some cards to go back to the idle
  382. * state. We wait 1ms to give cards time to
  383. * respond.
  384. */
  385. mmc_go_idle(host);
  386. /* The extra bit indicates that we support high capacity */
  387. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  388. if (err)
  389. goto err;
  390. /*
  391. * For SPI, enable CRC as appropriate.
  392. */
  393. if (mmc_host_is_spi(host)) {
  394. err = mmc_spi_set_crc(host, use_spi_crc);
  395. if (err)
  396. goto err;
  397. }
  398. /*
  399. * Fetch CID from card.
  400. */
  401. if (mmc_host_is_spi(host))
  402. err = mmc_send_cid(host, cid);
  403. else
  404. err = mmc_all_send_cid(host, cid);
  405. if (err)
  406. goto err;
  407. if (oldcard) {
  408. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  409. err = -ENOENT;
  410. goto err;
  411. }
  412. card = oldcard;
  413. } else {
  414. /*
  415. * Allocate card structure.
  416. */
  417. card = mmc_alloc_card(host, &mmc_type);
  418. if (IS_ERR(card)) {
  419. err = PTR_ERR(card);
  420. goto err;
  421. }
  422. card->type = MMC_TYPE_MMC;
  423. card->rca = 1;
  424. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  425. }
  426. /*
  427. * For native busses: set card RCA and quit open drain mode.
  428. */
  429. if (!mmc_host_is_spi(host)) {
  430. err = mmc_set_relative_addr(card);
  431. if (err)
  432. goto free_card;
  433. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  434. }
  435. if (!oldcard) {
  436. /*
  437. * Fetch CSD from card.
  438. */
  439. err = mmc_send_csd(card, card->raw_csd);
  440. if (err)
  441. goto free_card;
  442. err = mmc_decode_csd(card);
  443. if (err)
  444. goto free_card;
  445. err = mmc_decode_cid(card);
  446. if (err)
  447. goto free_card;
  448. }
  449. /*
  450. * Select card, as all following commands rely on that.
  451. */
  452. if (!mmc_host_is_spi(host)) {
  453. err = mmc_select_card(card);
  454. if (err)
  455. goto free_card;
  456. }
  457. if (!oldcard) {
  458. /*
  459. * Fetch and process extended CSD.
  460. */
  461. err = mmc_read_ext_csd(card);
  462. if (err)
  463. goto free_card;
  464. /* If doing byte addressing, check if required to do sector
  465. * addressing. Handle the case of <2GB cards needing sector
  466. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  467. * ocr register has bit 30 set for sector addressing.
  468. */
  469. if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
  470. mmc_card_set_blockaddr(card);
  471. /* Erase size depends on CSD and Extended CSD */
  472. mmc_set_erase_size(card);
  473. }
  474. /*
  475. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  476. * bit. This bit will be lost every time after a reset or power off.
  477. */
  478. if (card->ext_csd.enhanced_area_en) {
  479. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  480. EXT_CSD_ERASE_GROUP_DEF, 1);
  481. if (err && err != -EBADMSG)
  482. goto free_card;
  483. if (err) {
  484. err = 0;
  485. /*
  486. * Just disable enhanced area off & sz
  487. * will try to enable ERASE_GROUP_DEF
  488. * during next time reinit
  489. */
  490. card->ext_csd.enhanced_area_offset = -EINVAL;
  491. card->ext_csd.enhanced_area_size = -EINVAL;
  492. } else {
  493. card->ext_csd.erase_group_def = 1;
  494. /*
  495. * enable ERASE_GRP_DEF successfully.
  496. * This will affect the erase size, so
  497. * here need to reset erase size
  498. */
  499. mmc_set_erase_size(card);
  500. }
  501. }
  502. /*
  503. * Activate high speed (if supported)
  504. */
  505. if ((card->ext_csd.hs_max_dtr != 0) &&
  506. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  507. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  508. EXT_CSD_HS_TIMING, 1);
  509. if (err && err != -EBADMSG)
  510. goto free_card;
  511. if (err) {
  512. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  513. mmc_hostname(card->host));
  514. err = 0;
  515. } else {
  516. mmc_card_set_highspeed(card);
  517. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  518. }
  519. }
  520. /*
  521. * Compute bus speed.
  522. */
  523. max_dtr = (unsigned int)-1;
  524. if (mmc_card_highspeed(card)) {
  525. if (max_dtr > card->ext_csd.hs_max_dtr)
  526. max_dtr = card->ext_csd.hs_max_dtr;
  527. } else if (max_dtr > card->csd.max_dtr) {
  528. max_dtr = card->csd.max_dtr;
  529. }
  530. mmc_set_clock(host, max_dtr);
  531. /*
  532. * Indicate DDR mode (if supported).
  533. */
  534. if (mmc_card_highspeed(card)) {
  535. if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
  536. && (host->caps & (MMC_CAP_1_8V_DDR)))
  537. ddr = MMC_1_8V_DDR_MODE;
  538. else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
  539. && (host->caps & (MMC_CAP_1_2V_DDR)))
  540. ddr = MMC_1_2V_DDR_MODE;
  541. }
  542. /*
  543. * Activate wide bus and DDR (if supported).
  544. */
  545. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  546. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  547. static unsigned ext_csd_bits[][2] = {
  548. { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
  549. { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
  550. { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
  551. };
  552. static unsigned bus_widths[] = {
  553. MMC_BUS_WIDTH_8,
  554. MMC_BUS_WIDTH_4,
  555. MMC_BUS_WIDTH_1
  556. };
  557. unsigned idx, bus_width = 0;
  558. if (host->caps & MMC_CAP_8_BIT_DATA)
  559. idx = 0;
  560. else
  561. idx = 1;
  562. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  563. bus_width = bus_widths[idx];
  564. if (bus_width == MMC_BUS_WIDTH_1)
  565. ddr = 0; /* no DDR for 1-bit width */
  566. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  567. EXT_CSD_BUS_WIDTH,
  568. ext_csd_bits[idx][0]);
  569. if (!err) {
  570. mmc_set_bus_width_ddr(card->host,
  571. bus_width, MMC_SDR_MODE);
  572. /*
  573. * If controller can't handle bus width test,
  574. * use the highest bus width to maintain
  575. * compatibility with previous MMC behavior.
  576. */
  577. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  578. break;
  579. err = mmc_bus_test(card, bus_width);
  580. if (!err)
  581. break;
  582. }
  583. }
  584. if (!err && ddr) {
  585. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  586. EXT_CSD_BUS_WIDTH,
  587. ext_csd_bits[idx][1]);
  588. }
  589. if (err) {
  590. printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
  591. "failed\n", mmc_hostname(card->host),
  592. 1 << bus_width, ddr);
  593. goto free_card;
  594. } else if (ddr) {
  595. mmc_card_set_ddr_mode(card);
  596. mmc_set_bus_width_ddr(card->host, bus_width, ddr);
  597. }
  598. }
  599. if (!oldcard)
  600. host->card = card;
  601. return 0;
  602. free_card:
  603. if (!oldcard)
  604. mmc_remove_card(card);
  605. err:
  606. return err;
  607. }
  608. /*
  609. * Host is being removed. Free up the current card.
  610. */
  611. static void mmc_remove(struct mmc_host *host)
  612. {
  613. BUG_ON(!host);
  614. BUG_ON(!host->card);
  615. mmc_remove_card(host->card);
  616. host->card = NULL;
  617. }
  618. /*
  619. * Card detection callback from host.
  620. */
  621. static void mmc_detect(struct mmc_host *host)
  622. {
  623. int err;
  624. BUG_ON(!host);
  625. BUG_ON(!host->card);
  626. mmc_claim_host(host);
  627. /*
  628. * Just check if our card has been removed.
  629. */
  630. err = mmc_send_status(host->card, NULL);
  631. mmc_release_host(host);
  632. if (err) {
  633. mmc_remove(host);
  634. mmc_claim_host(host);
  635. mmc_detach_bus(host);
  636. mmc_release_host(host);
  637. }
  638. }
  639. /*
  640. * Suspend callback from host.
  641. */
  642. static int mmc_suspend(struct mmc_host *host)
  643. {
  644. BUG_ON(!host);
  645. BUG_ON(!host->card);
  646. mmc_claim_host(host);
  647. if (!mmc_host_is_spi(host))
  648. mmc_deselect_cards(host);
  649. host->card->state &= ~MMC_STATE_HIGHSPEED;
  650. mmc_release_host(host);
  651. return 0;
  652. }
  653. /*
  654. * Resume callback from host.
  655. *
  656. * This function tries to determine if the same card is still present
  657. * and, if so, restore all state to it.
  658. */
  659. static int mmc_resume(struct mmc_host *host)
  660. {
  661. int err;
  662. BUG_ON(!host);
  663. BUG_ON(!host->card);
  664. mmc_claim_host(host);
  665. err = mmc_init_card(host, host->ocr, host->card);
  666. mmc_release_host(host);
  667. return err;
  668. }
  669. static int mmc_power_restore(struct mmc_host *host)
  670. {
  671. int ret;
  672. host->card->state &= ~MMC_STATE_HIGHSPEED;
  673. mmc_claim_host(host);
  674. ret = mmc_init_card(host, host->ocr, host->card);
  675. mmc_release_host(host);
  676. return ret;
  677. }
  678. static int mmc_sleep(struct mmc_host *host)
  679. {
  680. struct mmc_card *card = host->card;
  681. int err = -ENOSYS;
  682. if (card && card->ext_csd.rev >= 3) {
  683. err = mmc_card_sleepawake(host, 1);
  684. if (err < 0)
  685. pr_debug("%s: Error %d while putting card into sleep",
  686. mmc_hostname(host), err);
  687. }
  688. return err;
  689. }
  690. static int mmc_awake(struct mmc_host *host)
  691. {
  692. struct mmc_card *card = host->card;
  693. int err = -ENOSYS;
  694. if (card && card->ext_csd.rev >= 3) {
  695. err = mmc_card_sleepawake(host, 0);
  696. if (err < 0)
  697. pr_debug("%s: Error %d while awaking sleeping card",
  698. mmc_hostname(host), err);
  699. }
  700. return err;
  701. }
  702. static const struct mmc_bus_ops mmc_ops = {
  703. .awake = mmc_awake,
  704. .sleep = mmc_sleep,
  705. .remove = mmc_remove,
  706. .detect = mmc_detect,
  707. .suspend = NULL,
  708. .resume = NULL,
  709. .power_restore = mmc_power_restore,
  710. };
  711. static const struct mmc_bus_ops mmc_ops_unsafe = {
  712. .awake = mmc_awake,
  713. .sleep = mmc_sleep,
  714. .remove = mmc_remove,
  715. .detect = mmc_detect,
  716. .suspend = mmc_suspend,
  717. .resume = mmc_resume,
  718. .power_restore = mmc_power_restore,
  719. };
  720. static void mmc_attach_bus_ops(struct mmc_host *host)
  721. {
  722. const struct mmc_bus_ops *bus_ops;
  723. if (!mmc_card_is_removable(host))
  724. bus_ops = &mmc_ops_unsafe;
  725. else
  726. bus_ops = &mmc_ops;
  727. mmc_attach_bus(host, bus_ops);
  728. }
  729. /*
  730. * Starting point for MMC card init.
  731. */
  732. int mmc_attach_mmc(struct mmc_host *host)
  733. {
  734. int err;
  735. u32 ocr;
  736. BUG_ON(!host);
  737. WARN_ON(!host->claimed);
  738. err = mmc_send_op_cond(host, 0, &ocr);
  739. if (err)
  740. return err;
  741. mmc_attach_bus_ops(host);
  742. if (host->ocr_avail_mmc)
  743. host->ocr_avail = host->ocr_avail_mmc;
  744. /*
  745. * We need to get OCR a different way for SPI.
  746. */
  747. if (mmc_host_is_spi(host)) {
  748. err = mmc_spi_read_ocr(host, 1, &ocr);
  749. if (err)
  750. goto err;
  751. }
  752. /*
  753. * Sanity check the voltages that the card claims to
  754. * support.
  755. */
  756. if (ocr & 0x7F) {
  757. printk(KERN_WARNING "%s: card claims to support voltages "
  758. "below the defined range. These will be ignored.\n",
  759. mmc_hostname(host));
  760. ocr &= ~0x7F;
  761. }
  762. host->ocr = mmc_select_voltage(host, ocr);
  763. /*
  764. * Can we support the voltage of the card?
  765. */
  766. if (!host->ocr) {
  767. err = -EINVAL;
  768. goto err;
  769. }
  770. /*
  771. * Detect and init the card.
  772. */
  773. err = mmc_init_card(host, host->ocr, NULL);
  774. if (err)
  775. goto err;
  776. mmc_release_host(host);
  777. err = mmc_add_card(host->card);
  778. mmc_claim_host(host);
  779. if (err)
  780. goto remove_card;
  781. return 0;
  782. remove_card:
  783. mmc_release_host(host);
  784. mmc_remove_card(host->card);
  785. mmc_claim_host(host);
  786. host->card = NULL;
  787. err:
  788. mmc_detach_bus(host);
  789. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  790. mmc_hostname(host), err);
  791. return err;
  792. }