mmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/mmc.h>
  16. #include "core.h"
  17. #include "bus.h"
  18. #include "mmc_ops.h"
  19. static const unsigned int tran_exp[] = {
  20. 10000, 100000, 1000000, 10000000,
  21. 0, 0, 0, 0
  22. };
  23. static const unsigned char tran_mant[] = {
  24. 0, 10, 12, 13, 15, 20, 25, 30,
  25. 35, 40, 45, 50, 55, 60, 70, 80,
  26. };
  27. static const unsigned int tacc_exp[] = {
  28. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  29. };
  30. static const unsigned int tacc_mant[] = {
  31. 0, 10, 12, 13, 15, 20, 25, 30,
  32. 35, 40, 45, 50, 55, 60, 70, 80,
  33. };
  34. #define UNSTUFF_BITS(resp,start,size) \
  35. ({ \
  36. const int __size = size; \
  37. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  38. const int __off = 3 - ((start) / 32); \
  39. const int __shft = (start) & 31; \
  40. u32 __res; \
  41. \
  42. __res = resp[__off] >> __shft; \
  43. if (__size + __shft > 32) \
  44. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  45. __res & __mask; \
  46. })
  47. /*
  48. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  49. */
  50. static int mmc_decode_cid(struct mmc_card *card)
  51. {
  52. u32 *resp = card->raw_cid;
  53. /*
  54. * The selection of the format here is based upon published
  55. * specs from sandisk and from what people have reported.
  56. */
  57. switch (card->csd.mmca_vsn) {
  58. case 0: /* MMC v1.0 - v1.2 */
  59. case 1: /* MMC v1.4 */
  60. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  61. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  62. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  63. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  64. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  65. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  66. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  67. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  68. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  69. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  70. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  71. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  72. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  73. break;
  74. case 2: /* MMC v2.0 - v2.2 */
  75. case 3: /* MMC v3.1 - v3.3 */
  76. case 4: /* MMC v4 */
  77. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  78. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  79. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  80. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  81. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  82. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  83. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  84. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  85. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  86. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  87. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  88. break;
  89. default:
  90. printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
  91. mmc_hostname(card->host), card->csd.mmca_vsn);
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Given a 128-bit response, decode to our card CSD structure.
  98. */
  99. static int mmc_decode_csd(struct mmc_card *card)
  100. {
  101. struct mmc_csd *csd = &card->csd;
  102. unsigned int e, m, csd_struct;
  103. u32 *resp = card->raw_csd;
  104. /*
  105. * We only understand CSD structure v1.1 and v1.2.
  106. * v1.2 has extra information in bits 15, 11 and 10.
  107. */
  108. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  109. if (csd_struct != 1 && csd_struct != 2) {
  110. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  111. mmc_hostname(card->host), csd_struct);
  112. return -EINVAL;
  113. }
  114. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  115. m = UNSTUFF_BITS(resp, 115, 4);
  116. e = UNSTUFF_BITS(resp, 112, 3);
  117. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  118. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  119. m = UNSTUFF_BITS(resp, 99, 4);
  120. e = UNSTUFF_BITS(resp, 96, 3);
  121. csd->max_dtr = tran_exp[e] * tran_mant[m];
  122. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  123. e = UNSTUFF_BITS(resp, 47, 3);
  124. m = UNSTUFF_BITS(resp, 62, 12);
  125. csd->capacity = (1 + m) << (e + 2);
  126. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  127. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  128. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  129. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  130. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  131. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  132. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  133. return 0;
  134. }
  135. /*
  136. * Read and decode extended CSD.
  137. */
  138. static int mmc_read_ext_csd(struct mmc_card *card)
  139. {
  140. int err;
  141. u8 *ext_csd;
  142. unsigned int ext_csd_struct;
  143. BUG_ON(!card);
  144. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  145. return 0;
  146. /*
  147. * As the ext_csd is so large and mostly unused, we don't store the
  148. * raw block in mmc_card.
  149. */
  150. ext_csd = kmalloc(512, GFP_KERNEL);
  151. if (!ext_csd) {
  152. printk(KERN_ERR "%s: could not allocate a buffer to "
  153. "receive the ext_csd.\n", mmc_hostname(card->host));
  154. return -ENOMEM;
  155. }
  156. err = mmc_send_ext_csd(card, ext_csd);
  157. if (err) {
  158. /*
  159. * We all hosts that cannot perform the command
  160. * to fail more gracefully
  161. */
  162. if (err != -EINVAL)
  163. goto out;
  164. /*
  165. * High capacity cards should have this "magic" size
  166. * stored in their CSD.
  167. */
  168. if (card->csd.capacity == (4096 * 512)) {
  169. printk(KERN_ERR "%s: unable to read EXT_CSD "
  170. "on a possible high capacity card. "
  171. "Card will be ignored.\n",
  172. mmc_hostname(card->host));
  173. } else {
  174. printk(KERN_WARNING "%s: unable to read "
  175. "EXT_CSD, performance might "
  176. "suffer.\n",
  177. mmc_hostname(card->host));
  178. err = 0;
  179. }
  180. goto out;
  181. }
  182. ext_csd_struct = ext_csd[EXT_CSD_REV];
  183. if (ext_csd_struct > 3) {
  184. printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
  185. "version %d\n", mmc_hostname(card->host),
  186. ext_csd_struct);
  187. err = -EINVAL;
  188. goto out;
  189. }
  190. if (ext_csd_struct >= 2) {
  191. card->ext_csd.sectors =
  192. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  193. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  194. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  195. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  196. if (card->ext_csd.sectors)
  197. mmc_card_set_blockaddr(card);
  198. }
  199. switch (ext_csd[EXT_CSD_CARD_TYPE]) {
  200. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  201. card->ext_csd.hs_max_dtr = 52000000;
  202. break;
  203. case EXT_CSD_CARD_TYPE_26:
  204. card->ext_csd.hs_max_dtr = 26000000;
  205. break;
  206. default:
  207. /* MMC v4 spec says this cannot happen */
  208. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  209. "support any high-speed modes.\n",
  210. mmc_hostname(card->host));
  211. goto out;
  212. }
  213. out:
  214. kfree(ext_csd);
  215. return err;
  216. }
  217. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  218. card->raw_cid[2], card->raw_cid[3]);
  219. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  220. card->raw_csd[2], card->raw_csd[3]);
  221. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  222. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  223. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  224. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  225. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  226. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  227. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  228. static struct attribute *mmc_std_attrs[] = {
  229. &dev_attr_cid.attr,
  230. &dev_attr_csd.attr,
  231. &dev_attr_date.attr,
  232. &dev_attr_fwrev.attr,
  233. &dev_attr_hwrev.attr,
  234. &dev_attr_manfid.attr,
  235. &dev_attr_name.attr,
  236. &dev_attr_oemid.attr,
  237. &dev_attr_serial.attr,
  238. NULL,
  239. };
  240. static struct attribute_group mmc_std_attr_group = {
  241. .attrs = mmc_std_attrs,
  242. };
  243. static struct attribute_group *mmc_attr_groups[] = {
  244. &mmc_std_attr_group,
  245. NULL,
  246. };
  247. static struct device_type mmc_type = {
  248. .groups = mmc_attr_groups,
  249. };
  250. /*
  251. * Handle the detection and initialisation of a card.
  252. *
  253. * In the case of a resume, "oldcard" will contain the card
  254. * we're trying to reinitialise.
  255. */
  256. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  257. struct mmc_card *oldcard)
  258. {
  259. struct mmc_card *card;
  260. int err;
  261. u32 cid[4];
  262. unsigned int max_dtr;
  263. BUG_ON(!host);
  264. WARN_ON(!host->claimed);
  265. /*
  266. * Since we're changing the OCR value, we seem to
  267. * need to tell some cards to go back to the idle
  268. * state. We wait 1ms to give cards time to
  269. * respond.
  270. */
  271. mmc_go_idle(host);
  272. /* The extra bit indicates that we support high capacity */
  273. err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
  274. if (err)
  275. goto err;
  276. /*
  277. * For SPI, enable CRC as appropriate.
  278. */
  279. if (mmc_host_is_spi(host)) {
  280. err = mmc_spi_set_crc(host, use_spi_crc);
  281. if (err)
  282. goto err;
  283. }
  284. /*
  285. * Fetch CID from card.
  286. */
  287. if (mmc_host_is_spi(host))
  288. err = mmc_send_cid(host, cid);
  289. else
  290. err = mmc_all_send_cid(host, cid);
  291. if (err)
  292. goto err;
  293. if (oldcard) {
  294. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  295. err = -ENOENT;
  296. goto err;
  297. }
  298. card = oldcard;
  299. } else {
  300. /*
  301. * Allocate card structure.
  302. */
  303. card = mmc_alloc_card(host, &mmc_type);
  304. if (IS_ERR(card)) {
  305. err = PTR_ERR(card);
  306. goto err;
  307. }
  308. card->type = MMC_TYPE_MMC;
  309. card->rca = 1;
  310. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  311. }
  312. /*
  313. * For native busses: set card RCA and quit open drain mode.
  314. */
  315. if (!mmc_host_is_spi(host)) {
  316. err = mmc_set_relative_addr(card);
  317. if (err)
  318. goto free_card;
  319. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  320. }
  321. if (!oldcard) {
  322. /*
  323. * Fetch CSD from card.
  324. */
  325. err = mmc_send_csd(card, card->raw_csd);
  326. if (err)
  327. goto free_card;
  328. err = mmc_decode_csd(card);
  329. if (err)
  330. goto free_card;
  331. err = mmc_decode_cid(card);
  332. if (err)
  333. goto free_card;
  334. }
  335. /*
  336. * Select card, as all following commands rely on that.
  337. */
  338. if (!mmc_host_is_spi(host)) {
  339. err = mmc_select_card(card);
  340. if (err)
  341. goto free_card;
  342. }
  343. if (!oldcard) {
  344. /*
  345. * Fetch and process extended CSD.
  346. */
  347. err = mmc_read_ext_csd(card);
  348. if (err)
  349. goto free_card;
  350. }
  351. /*
  352. * Activate high speed (if supported)
  353. */
  354. if ((card->ext_csd.hs_max_dtr != 0) &&
  355. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  356. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  357. EXT_CSD_HS_TIMING, 1);
  358. if (err)
  359. goto free_card;
  360. mmc_card_set_highspeed(card);
  361. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  362. }
  363. /*
  364. * Compute bus speed.
  365. */
  366. max_dtr = (unsigned int)-1;
  367. if (mmc_card_highspeed(card)) {
  368. if (max_dtr > card->ext_csd.hs_max_dtr)
  369. max_dtr = card->ext_csd.hs_max_dtr;
  370. } else if (max_dtr > card->csd.max_dtr) {
  371. max_dtr = card->csd.max_dtr;
  372. }
  373. mmc_set_clock(host, max_dtr);
  374. /*
  375. * Activate wide bus (if supported).
  376. */
  377. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  378. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  379. unsigned ext_csd_bit, bus_width;
  380. if (host->caps & MMC_CAP_8_BIT_DATA) {
  381. ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
  382. bus_width = MMC_BUS_WIDTH_8;
  383. } else {
  384. ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
  385. bus_width = MMC_BUS_WIDTH_4;
  386. }
  387. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  388. EXT_CSD_BUS_WIDTH, ext_csd_bit);
  389. if (err)
  390. goto free_card;
  391. mmc_set_bus_width(card->host, bus_width);
  392. }
  393. if (!oldcard)
  394. host->card = card;
  395. return 0;
  396. free_card:
  397. if (!oldcard)
  398. mmc_remove_card(card);
  399. err:
  400. return err;
  401. }
  402. /*
  403. * Host is being removed. Free up the current card.
  404. */
  405. static void mmc_remove(struct mmc_host *host)
  406. {
  407. BUG_ON(!host);
  408. BUG_ON(!host->card);
  409. mmc_remove_card(host->card);
  410. host->card = NULL;
  411. }
  412. /*
  413. * Card detection callback from host.
  414. */
  415. static void mmc_detect(struct mmc_host *host)
  416. {
  417. int err;
  418. BUG_ON(!host);
  419. BUG_ON(!host->card);
  420. mmc_claim_host(host);
  421. /*
  422. * Just check if our card has been removed.
  423. */
  424. err = mmc_send_status(host->card, NULL);
  425. mmc_release_host(host);
  426. if (err) {
  427. mmc_remove(host);
  428. mmc_claim_host(host);
  429. mmc_detach_bus(host);
  430. mmc_release_host(host);
  431. }
  432. }
  433. #ifdef CONFIG_MMC_UNSAFE_RESUME
  434. /*
  435. * Suspend callback from host.
  436. */
  437. static void mmc_suspend(struct mmc_host *host)
  438. {
  439. BUG_ON(!host);
  440. BUG_ON(!host->card);
  441. mmc_claim_host(host);
  442. if (!mmc_host_is_spi(host))
  443. mmc_deselect_cards(host);
  444. host->card->state &= ~MMC_STATE_HIGHSPEED;
  445. mmc_release_host(host);
  446. }
  447. /*
  448. * Resume callback from host.
  449. *
  450. * This function tries to determine if the same card is still present
  451. * and, if so, restore all state to it.
  452. */
  453. static void mmc_resume(struct mmc_host *host)
  454. {
  455. int err;
  456. BUG_ON(!host);
  457. BUG_ON(!host->card);
  458. mmc_claim_host(host);
  459. err = mmc_init_card(host, host->ocr, host->card);
  460. mmc_release_host(host);
  461. if (err) {
  462. mmc_remove(host);
  463. mmc_claim_host(host);
  464. mmc_detach_bus(host);
  465. mmc_release_host(host);
  466. }
  467. }
  468. #else
  469. #define mmc_suspend NULL
  470. #define mmc_resume NULL
  471. #endif
  472. static const struct mmc_bus_ops mmc_ops = {
  473. .remove = mmc_remove,
  474. .detect = mmc_detect,
  475. .suspend = mmc_suspend,
  476. .resume = mmc_resume,
  477. };
  478. /*
  479. * Starting point for MMC card init.
  480. */
  481. int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
  482. {
  483. int err;
  484. BUG_ON(!host);
  485. WARN_ON(!host->claimed);
  486. mmc_attach_bus(host, &mmc_ops);
  487. /*
  488. * We need to get OCR a different way for SPI.
  489. */
  490. if (mmc_host_is_spi(host)) {
  491. err = mmc_spi_read_ocr(host, 1, &ocr);
  492. if (err)
  493. goto err;
  494. }
  495. /*
  496. * Sanity check the voltages that the card claims to
  497. * support.
  498. */
  499. if (ocr & 0x7F) {
  500. printk(KERN_WARNING "%s: card claims to support voltages "
  501. "below the defined range. These will be ignored.\n",
  502. mmc_hostname(host));
  503. ocr &= ~0x7F;
  504. }
  505. host->ocr = mmc_select_voltage(host, ocr);
  506. /*
  507. * Can we support the voltage of the card?
  508. */
  509. if (!host->ocr) {
  510. err = -EINVAL;
  511. goto err;
  512. }
  513. /*
  514. * Detect and init the card.
  515. */
  516. err = mmc_init_card(host, host->ocr, NULL);
  517. if (err)
  518. goto err;
  519. mmc_release_host(host);
  520. err = mmc_add_card(host->card);
  521. if (err)
  522. goto remove_card;
  523. return 0;
  524. remove_card:
  525. mmc_remove_card(host->card);
  526. host->card = NULL;
  527. mmc_claim_host(host);
  528. err:
  529. mmc_detach_bus(host);
  530. mmc_release_host(host);
  531. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  532. mmc_hostname(host), err);
  533. return err;
  534. }