mmc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. BUG_ON(!host);
  377. WARN_ON(!host->claimed);
  378. /*
  379. * Since we're changing the OCR value, we seem to
  380. * need to tell some cards to go back to the idle
  381. * state. We wait 1ms to give cards time to
  382. * respond.
  383. */
  384. mmc_go_idle(host);
  385. /* The extra bit indicates that we support high capacity */
  386. err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
  387. if (err)
  388. goto err;
  389. /*
  390. * For SPI, enable CRC as appropriate.
  391. */
  392. if (mmc_host_is_spi(host)) {
  393. err = mmc_spi_set_crc(host, use_spi_crc);
  394. if (err)
  395. goto err;
  396. }
  397. /*
  398. * Fetch CID from card.
  399. */
  400. if (mmc_host_is_spi(host))
  401. err = mmc_send_cid(host, cid);
  402. else
  403. err = mmc_all_send_cid(host, cid);
  404. if (err)
  405. goto err;
  406. if (oldcard) {
  407. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  408. err = -ENOENT;
  409. goto err;
  410. }
  411. card = oldcard;
  412. } else {
  413. /*
  414. * Allocate card structure.
  415. */
  416. card = mmc_alloc_card(host, &mmc_type);
  417. if (IS_ERR(card)) {
  418. err = PTR_ERR(card);
  419. goto err;
  420. }
  421. card->type = MMC_TYPE_MMC;
  422. card->rca = 1;
  423. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  424. }
  425. /*
  426. * For native busses: set card RCA and quit open drain mode.
  427. */
  428. if (!mmc_host_is_spi(host)) {
  429. err = mmc_set_relative_addr(card);
  430. if (err)
  431. goto free_card;
  432. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  433. }
  434. if (!oldcard) {
  435. /*
  436. * Fetch CSD from card.
  437. */
  438. err = mmc_send_csd(card, card->raw_csd);
  439. if (err)
  440. goto free_card;
  441. err = mmc_decode_csd(card);
  442. if (err)
  443. goto free_card;
  444. err = mmc_decode_cid(card);
  445. if (err)
  446. goto free_card;
  447. }
  448. /*
  449. * Select card, as all following commands rely on that.
  450. */
  451. if (!mmc_host_is_spi(host)) {
  452. err = mmc_select_card(card);
  453. if (err)
  454. goto free_card;
  455. }
  456. if (!oldcard) {
  457. /*
  458. * Fetch and process extended CSD.
  459. */
  460. err = mmc_read_ext_csd(card);
  461. if (err)
  462. goto free_card;
  463. /* Erase size depends on CSD and Extended CSD */
  464. mmc_set_erase_size(card);
  465. }
  466. /*
  467. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  468. * bit. This bit will be lost everytime after a reset or power off.
  469. */
  470. if (card->ext_csd.enhanced_area_en) {
  471. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  472. EXT_CSD_ERASE_GROUP_DEF, 1);
  473. if (err && err != -EBADMSG)
  474. goto free_card;
  475. if (err) {
  476. err = 0;
  477. /*
  478. * Just disable enhanced area off & sz
  479. * will try to enable ERASE_GROUP_DEF
  480. * during next time reinit
  481. */
  482. card->ext_csd.enhanced_area_offset = -EINVAL;
  483. card->ext_csd.enhanced_area_size = -EINVAL;
  484. } else {
  485. card->ext_csd.erase_group_def = 1;
  486. /*
  487. * enable ERASE_GRP_DEF successfully.
  488. * This will affect the erase size, so
  489. * here need to reset erase size
  490. */
  491. mmc_set_erase_size(card);
  492. }
  493. }
  494. /*
  495. * Activate high speed (if supported)
  496. */
  497. if ((card->ext_csd.hs_max_dtr != 0) &&
  498. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  499. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  500. EXT_CSD_HS_TIMING, 1);
  501. if (err && err != -EBADMSG)
  502. goto free_card;
  503. if (err) {
  504. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  505. mmc_hostname(card->host));
  506. err = 0;
  507. } else {
  508. mmc_card_set_highspeed(card);
  509. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  510. }
  511. }
  512. /*
  513. * Compute bus speed.
  514. */
  515. max_dtr = (unsigned int)-1;
  516. if (mmc_card_highspeed(card)) {
  517. if (max_dtr > card->ext_csd.hs_max_dtr)
  518. max_dtr = card->ext_csd.hs_max_dtr;
  519. } else if (max_dtr > card->csd.max_dtr) {
  520. max_dtr = card->csd.max_dtr;
  521. }
  522. mmc_set_clock(host, max_dtr);
  523. /*
  524. * Indicate DDR mode (if supported).
  525. */
  526. if (mmc_card_highspeed(card)) {
  527. if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
  528. && (host->caps & (MMC_CAP_1_8V_DDR)))
  529. ddr = MMC_1_8V_DDR_MODE;
  530. else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
  531. && (host->caps & (MMC_CAP_1_2V_DDR)))
  532. ddr = MMC_1_2V_DDR_MODE;
  533. }
  534. /*
  535. * Activate wide bus and DDR (if supported).
  536. */
  537. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  538. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  539. static unsigned ext_csd_bits[][2] = {
  540. { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
  541. { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
  542. { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
  543. };
  544. static unsigned bus_widths[] = {
  545. MMC_BUS_WIDTH_8,
  546. MMC_BUS_WIDTH_4,
  547. MMC_BUS_WIDTH_1
  548. };
  549. unsigned idx, bus_width = 0;
  550. if (host->caps & MMC_CAP_8_BIT_DATA)
  551. idx = 0;
  552. else
  553. idx = 1;
  554. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  555. bus_width = bus_widths[idx];
  556. if (bus_width == MMC_BUS_WIDTH_1)
  557. ddr = 0; /* no DDR for 1-bit width */
  558. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  559. EXT_CSD_BUS_WIDTH,
  560. ext_csd_bits[idx][0]);
  561. if (!err) {
  562. mmc_set_bus_width_ddr(card->host,
  563. bus_width, MMC_SDR_MODE);
  564. /*
  565. * If controller can't handle bus width test,
  566. * use the highest bus width to maintain
  567. * compatibility with previous MMC behavior.
  568. */
  569. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  570. break;
  571. err = mmc_bus_test(card, bus_width);
  572. if (!err)
  573. break;
  574. }
  575. }
  576. if (!err && ddr) {
  577. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  578. EXT_CSD_BUS_WIDTH,
  579. ext_csd_bits[idx][1]);
  580. }
  581. if (err) {
  582. printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
  583. "failed\n", mmc_hostname(card->host),
  584. 1 << bus_width, ddr);
  585. goto free_card;
  586. } else if (ddr) {
  587. mmc_card_set_ddr_mode(card);
  588. mmc_set_bus_width_ddr(card->host, bus_width, ddr);
  589. }
  590. }
  591. if (!oldcard)
  592. host->card = card;
  593. return 0;
  594. free_card:
  595. if (!oldcard)
  596. mmc_remove_card(card);
  597. err:
  598. return err;
  599. }
  600. /*
  601. * Host is being removed. Free up the current card.
  602. */
  603. static void mmc_remove(struct mmc_host *host)
  604. {
  605. BUG_ON(!host);
  606. BUG_ON(!host->card);
  607. mmc_remove_card(host->card);
  608. host->card = NULL;
  609. }
  610. /*
  611. * Card detection callback from host.
  612. */
  613. static void mmc_detect(struct mmc_host *host)
  614. {
  615. int err;
  616. BUG_ON(!host);
  617. BUG_ON(!host->card);
  618. mmc_claim_host(host);
  619. /*
  620. * Just check if our card has been removed.
  621. */
  622. err = mmc_send_status(host->card, NULL);
  623. mmc_release_host(host);
  624. if (err) {
  625. mmc_remove(host);
  626. mmc_claim_host(host);
  627. mmc_detach_bus(host);
  628. mmc_release_host(host);
  629. }
  630. }
  631. /*
  632. * Suspend callback from host.
  633. */
  634. static int mmc_suspend(struct mmc_host *host)
  635. {
  636. BUG_ON(!host);
  637. BUG_ON(!host->card);
  638. mmc_claim_host(host);
  639. if (!mmc_host_is_spi(host))
  640. mmc_deselect_cards(host);
  641. host->card->state &= ~MMC_STATE_HIGHSPEED;
  642. mmc_release_host(host);
  643. return 0;
  644. }
  645. /*
  646. * Resume callback from host.
  647. *
  648. * This function tries to determine if the same card is still present
  649. * and, if so, restore all state to it.
  650. */
  651. static int mmc_resume(struct mmc_host *host)
  652. {
  653. int err;
  654. BUG_ON(!host);
  655. BUG_ON(!host->card);
  656. mmc_claim_host(host);
  657. err = mmc_init_card(host, host->ocr, host->card);
  658. mmc_release_host(host);
  659. return err;
  660. }
  661. static int mmc_power_restore(struct mmc_host *host)
  662. {
  663. int ret;
  664. host->card->state &= ~MMC_STATE_HIGHSPEED;
  665. mmc_claim_host(host);
  666. ret = mmc_init_card(host, host->ocr, host->card);
  667. mmc_release_host(host);
  668. return ret;
  669. }
  670. static int mmc_sleep(struct mmc_host *host)
  671. {
  672. struct mmc_card *card = host->card;
  673. int err = -ENOSYS;
  674. if (card && card->ext_csd.rev >= 3) {
  675. err = mmc_card_sleepawake(host, 1);
  676. if (err < 0)
  677. pr_debug("%s: Error %d while putting card into sleep",
  678. mmc_hostname(host), err);
  679. }
  680. return err;
  681. }
  682. static int mmc_awake(struct mmc_host *host)
  683. {
  684. struct mmc_card *card = host->card;
  685. int err = -ENOSYS;
  686. if (card && card->ext_csd.rev >= 3) {
  687. err = mmc_card_sleepawake(host, 0);
  688. if (err < 0)
  689. pr_debug("%s: Error %d while awaking sleeping card",
  690. mmc_hostname(host), err);
  691. }
  692. return err;
  693. }
  694. static const struct mmc_bus_ops mmc_ops = {
  695. .awake = mmc_awake,
  696. .sleep = mmc_sleep,
  697. .remove = mmc_remove,
  698. .detect = mmc_detect,
  699. .suspend = NULL,
  700. .resume = NULL,
  701. .power_restore = mmc_power_restore,
  702. };
  703. static const struct mmc_bus_ops mmc_ops_unsafe = {
  704. .awake = mmc_awake,
  705. .sleep = mmc_sleep,
  706. .remove = mmc_remove,
  707. .detect = mmc_detect,
  708. .suspend = mmc_suspend,
  709. .resume = mmc_resume,
  710. .power_restore = mmc_power_restore,
  711. };
  712. static void mmc_attach_bus_ops(struct mmc_host *host)
  713. {
  714. const struct mmc_bus_ops *bus_ops;
  715. if (!mmc_card_is_removable(host))
  716. bus_ops = &mmc_ops_unsafe;
  717. else
  718. bus_ops = &mmc_ops;
  719. mmc_attach_bus(host, bus_ops);
  720. }
  721. /*
  722. * Starting point for MMC card init.
  723. */
  724. int mmc_attach_mmc(struct mmc_host *host)
  725. {
  726. int err;
  727. u32 ocr;
  728. BUG_ON(!host);
  729. WARN_ON(!host->claimed);
  730. err = mmc_send_op_cond(host, 0, &ocr);
  731. if (err)
  732. return err;
  733. mmc_attach_bus_ops(host);
  734. if (host->ocr_avail_mmc)
  735. host->ocr_avail = host->ocr_avail_mmc;
  736. /*
  737. * We need to get OCR a different way for SPI.
  738. */
  739. if (mmc_host_is_spi(host)) {
  740. err = mmc_spi_read_ocr(host, 1, &ocr);
  741. if (err)
  742. goto err;
  743. }
  744. /*
  745. * Sanity check the voltages that the card claims to
  746. * support.
  747. */
  748. if (ocr & 0x7F) {
  749. printk(KERN_WARNING "%s: card claims to support voltages "
  750. "below the defined range. These will be ignored.\n",
  751. mmc_hostname(host));
  752. ocr &= ~0x7F;
  753. }
  754. host->ocr = mmc_select_voltage(host, ocr);
  755. /*
  756. * Can we support the voltage of the card?
  757. */
  758. if (!host->ocr) {
  759. err = -EINVAL;
  760. goto err;
  761. }
  762. /*
  763. * Detect and init the card.
  764. */
  765. err = mmc_init_card(host, host->ocr, NULL);
  766. if (err)
  767. goto err;
  768. mmc_release_host(host);
  769. err = mmc_add_card(host->card);
  770. mmc_claim_host(host);
  771. if (err)
  772. goto remove_card;
  773. return 0;
  774. remove_card:
  775. mmc_release_host(host);
  776. mmc_remove_card(host->card);
  777. mmc_claim_host(host);
  778. host->card = NULL;
  779. err:
  780. mmc_detach_bus(host);
  781. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  782. mmc_hostname(host), err);
  783. return err;
  784. }