mmc.c 19 KB

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