sd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * linux/drivers/mmc/core/sd.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2007 Pierre Ossman, 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 <linux/mmc/sd.h>
  17. #include "core.h"
  18. #include "bus.h"
  19. #include "mmc_ops.h"
  20. #include "sd_ops.h"
  21. static const unsigned int tran_exp[] = {
  22. 10000, 100000, 1000000, 10000000,
  23. 0, 0, 0, 0
  24. };
  25. static const unsigned char tran_mant[] = {
  26. 0, 10, 12, 13, 15, 20, 25, 30,
  27. 35, 40, 45, 50, 55, 60, 70, 80,
  28. };
  29. static const unsigned int tacc_exp[] = {
  30. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  31. };
  32. static const unsigned int tacc_mant[] = {
  33. 0, 10, 12, 13, 15, 20, 25, 30,
  34. 35, 40, 45, 50, 55, 60, 70, 80,
  35. };
  36. #define UNSTUFF_BITS(resp,start,size) \
  37. ({ \
  38. const int __size = size; \
  39. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  40. const int __off = 3 - ((start) / 32); \
  41. const int __shft = (start) & 31; \
  42. u32 __res; \
  43. \
  44. __res = resp[__off] >> __shft; \
  45. if (__size + __shft > 32) \
  46. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  47. __res & __mask; \
  48. })
  49. /*
  50. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  51. */
  52. static void mmc_decode_cid(struct mmc_card *card)
  53. {
  54. u32 *resp = card->raw_cid;
  55. memset(&card->cid, 0, sizeof(struct mmc_cid));
  56. /*
  57. * SD doesn't currently have a version field so we will
  58. * have to assume we can parse this.
  59. */
  60. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  61. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  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.hwrev = UNSTUFF_BITS(resp, 60, 4);
  68. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  69. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  70. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  71. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  72. card->cid.year += 2000; /* SD cards year offset */
  73. }
  74. /*
  75. * Given a 128-bit response, decode to our card CSD structure.
  76. */
  77. static int mmc_decode_csd(struct mmc_card *card)
  78. {
  79. struct mmc_csd *csd = &card->csd;
  80. unsigned int e, m, csd_struct;
  81. u32 *resp = card->raw_csd;
  82. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  83. switch (csd_struct) {
  84. case 0:
  85. m = UNSTUFF_BITS(resp, 115, 4);
  86. e = UNSTUFF_BITS(resp, 112, 3);
  87. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  88. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  89. m = UNSTUFF_BITS(resp, 99, 4);
  90. e = UNSTUFF_BITS(resp, 96, 3);
  91. csd->max_dtr = tran_exp[e] * tran_mant[m];
  92. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  93. e = UNSTUFF_BITS(resp, 47, 3);
  94. m = UNSTUFF_BITS(resp, 62, 12);
  95. csd->capacity = (1 + m) << (e + 2);
  96. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  97. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  98. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  99. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  100. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  101. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  102. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  103. break;
  104. case 1:
  105. /*
  106. * This is a block-addressed SDHC card. Most
  107. * interesting fields are unused and have fixed
  108. * values. To avoid getting tripped by buggy cards,
  109. * we assume those fixed values ourselves.
  110. */
  111. mmc_card_set_blockaddr(card);
  112. csd->tacc_ns = 0; /* Unused */
  113. csd->tacc_clks = 0; /* Unused */
  114. m = UNSTUFF_BITS(resp, 99, 4);
  115. e = UNSTUFF_BITS(resp, 96, 3);
  116. csd->max_dtr = tran_exp[e] * tran_mant[m];
  117. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  118. m = UNSTUFF_BITS(resp, 48, 22);
  119. csd->capacity = (1 + m) << 10;
  120. csd->read_blkbits = 9;
  121. csd->read_partial = 0;
  122. csd->write_misalign = 0;
  123. csd->read_misalign = 0;
  124. csd->r2w_factor = 4; /* Unused */
  125. csd->write_blkbits = 9;
  126. csd->write_partial = 0;
  127. break;
  128. default:
  129. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  130. mmc_hostname(card->host), csd_struct);
  131. return -EINVAL;
  132. }
  133. return 0;
  134. }
  135. /*
  136. * Given a 64-bit response, decode to our card SCR structure.
  137. */
  138. static int mmc_decode_scr(struct mmc_card *card)
  139. {
  140. struct sd_scr *scr = &card->scr;
  141. unsigned int scr_struct;
  142. u32 resp[4];
  143. resp[3] = card->raw_scr[1];
  144. resp[2] = card->raw_scr[0];
  145. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  146. if (scr_struct != 0) {
  147. printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
  148. mmc_hostname(card->host), scr_struct);
  149. return -EINVAL;
  150. }
  151. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  152. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  153. return 0;
  154. }
  155. /*
  156. * Fetches and decodes switch information
  157. */
  158. static int mmc_read_switch(struct mmc_card *card)
  159. {
  160. int err;
  161. u8 *status;
  162. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  163. return 0;
  164. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  165. printk(KERN_WARNING "%s: card lacks mandatory switch "
  166. "function, performance might suffer.\n",
  167. mmc_hostname(card->host));
  168. return 0;
  169. }
  170. err = -EIO;
  171. status = kmalloc(64, GFP_KERNEL);
  172. if (!status) {
  173. printk(KERN_ERR "%s: could not allocate a buffer for "
  174. "switch capabilities.\n", mmc_hostname(card->host));
  175. return -ENOMEM;
  176. }
  177. err = mmc_sd_switch(card, 0, 0, 1, status);
  178. if (err) {
  179. /*
  180. * We all hosts that cannot perform the command
  181. * to fail more gracefully
  182. */
  183. if (err != -EINVAL)
  184. goto out;
  185. printk(KERN_WARNING "%s: problem reading switch "
  186. "capabilities, performance might suffer.\n",
  187. mmc_hostname(card->host));
  188. err = 0;
  189. goto out;
  190. }
  191. if (status[13] & 0x02)
  192. card->sw_caps.hs_max_dtr = 50000000;
  193. out:
  194. kfree(status);
  195. return err;
  196. }
  197. /*
  198. * Test if the card supports high-speed mode and, if so, switch to it.
  199. */
  200. static int mmc_switch_hs(struct mmc_card *card)
  201. {
  202. int err;
  203. u8 *status;
  204. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  205. return 0;
  206. if (!(card->csd.cmdclass & CCC_SWITCH))
  207. return 0;
  208. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  209. return 0;
  210. if (card->sw_caps.hs_max_dtr == 0)
  211. return 0;
  212. err = -EIO;
  213. status = kmalloc(64, GFP_KERNEL);
  214. if (!status) {
  215. printk(KERN_ERR "%s: could not allocate a buffer for "
  216. "switch capabilities.\n", mmc_hostname(card->host));
  217. return -ENOMEM;
  218. }
  219. err = mmc_sd_switch(card, 1, 0, 1, status);
  220. if (err)
  221. goto out;
  222. if ((status[16] & 0xF) != 1) {
  223. printk(KERN_WARNING "%s: Problem switching card "
  224. "into high-speed mode!\n",
  225. mmc_hostname(card->host));
  226. } else {
  227. mmc_card_set_highspeed(card);
  228. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  229. }
  230. out:
  231. kfree(status);
  232. return err;
  233. }
  234. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  235. card->raw_cid[2], card->raw_cid[3]);
  236. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  237. card->raw_csd[2], card->raw_csd[3]);
  238. MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  239. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  240. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  241. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  242. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  243. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  244. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  245. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  246. static struct attribute *sd_std_attrs[] = {
  247. &dev_attr_cid.attr,
  248. &dev_attr_csd.attr,
  249. &dev_attr_scr.attr,
  250. &dev_attr_date.attr,
  251. &dev_attr_fwrev.attr,
  252. &dev_attr_hwrev.attr,
  253. &dev_attr_manfid.attr,
  254. &dev_attr_name.attr,
  255. &dev_attr_oemid.attr,
  256. &dev_attr_serial.attr,
  257. NULL,
  258. };
  259. static struct attribute_group sd_std_attr_group = {
  260. .attrs = sd_std_attrs,
  261. };
  262. static struct attribute_group *sd_attr_groups[] = {
  263. &sd_std_attr_group,
  264. NULL,
  265. };
  266. static struct device_type sd_type = {
  267. .groups = sd_attr_groups,
  268. };
  269. /*
  270. * Handle the detection and initialisation of a card.
  271. *
  272. * In the case of a resume, "oldcard" will contain the card
  273. * we're trying to reinitialise.
  274. */
  275. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  276. struct mmc_card *oldcard)
  277. {
  278. struct mmc_card *card;
  279. int err;
  280. u32 cid[4];
  281. unsigned int max_dtr;
  282. BUG_ON(!host);
  283. WARN_ON(!host->claimed);
  284. /*
  285. * Since we're changing the OCR value, we seem to
  286. * need to tell some cards to go back to the idle
  287. * state. We wait 1ms to give cards time to
  288. * respond.
  289. */
  290. mmc_go_idle(host);
  291. /*
  292. * If SD_SEND_IF_COND indicates an SD 2.0
  293. * compliant card and we should set bit 30
  294. * of the ocr to indicate that we can handle
  295. * block-addressed SDHC cards.
  296. */
  297. err = mmc_send_if_cond(host, ocr);
  298. if (!err)
  299. ocr |= 1 << 30;
  300. err = mmc_send_app_op_cond(host, ocr, NULL);
  301. if (err)
  302. goto err;
  303. /*
  304. * Fetch CID from card.
  305. */
  306. if (mmc_host_is_spi(host))
  307. err = mmc_send_cid(host, cid);
  308. else
  309. err = mmc_all_send_cid(host, cid);
  310. if (err)
  311. goto err;
  312. if (oldcard) {
  313. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  314. err = -ENOENT;
  315. goto err;
  316. }
  317. card = oldcard;
  318. } else {
  319. /*
  320. * Allocate card structure.
  321. */
  322. card = mmc_alloc_card(host, &sd_type);
  323. if (IS_ERR(card)) {
  324. err = PTR_ERR(card);
  325. goto err;
  326. }
  327. card->type = MMC_TYPE_SD;
  328. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  329. }
  330. /*
  331. * For native busses: get card RCA and quit open drain mode.
  332. */
  333. if (!mmc_host_is_spi(host)) {
  334. err = mmc_send_relative_addr(host, &card->rca);
  335. if (err)
  336. goto free_card;
  337. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  338. }
  339. if (!oldcard) {
  340. /*
  341. * Fetch CSD from card.
  342. */
  343. err = mmc_send_csd(card, card->raw_csd);
  344. if (err)
  345. goto free_card;
  346. err = mmc_decode_csd(card);
  347. if (err)
  348. goto free_card;
  349. mmc_decode_cid(card);
  350. }
  351. /*
  352. * Select card, as all following commands rely on that.
  353. */
  354. if (!mmc_host_is_spi(host)) {
  355. err = mmc_select_card(card);
  356. if (err)
  357. goto free_card;
  358. }
  359. if (!oldcard) {
  360. /*
  361. * Fetch SCR from card.
  362. */
  363. err = mmc_app_send_scr(card, card->raw_scr);
  364. if (err)
  365. goto free_card;
  366. err = mmc_decode_scr(card);
  367. if (err < 0)
  368. goto free_card;
  369. /*
  370. * Fetch switch information from card.
  371. */
  372. err = mmc_read_switch(card);
  373. if (err)
  374. goto free_card;
  375. }
  376. /*
  377. * For SPI, enable CRC as appropriate.
  378. * This CRC enable is located AFTER the reading of the
  379. * card registers because some SDHC cards are not able
  380. * to provide valid CRCs for non-512-byte blocks.
  381. */
  382. if (mmc_host_is_spi(host)) {
  383. err = mmc_spi_set_crc(host, use_spi_crc);
  384. if (err)
  385. goto free_card;
  386. }
  387. /*
  388. * Attempt to change to high-speed (if supported)
  389. */
  390. err = mmc_switch_hs(card);
  391. if (err)
  392. goto free_card;
  393. /*
  394. * Compute bus speed.
  395. */
  396. max_dtr = (unsigned int)-1;
  397. if (mmc_card_highspeed(card)) {
  398. if (max_dtr > card->sw_caps.hs_max_dtr)
  399. max_dtr = card->sw_caps.hs_max_dtr;
  400. } else if (max_dtr > card->csd.max_dtr) {
  401. max_dtr = card->csd.max_dtr;
  402. }
  403. mmc_set_clock(host, max_dtr);
  404. /*
  405. * Switch to wider bus (if supported).
  406. */
  407. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  408. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  409. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  410. if (err)
  411. goto free_card;
  412. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  413. }
  414. /*
  415. * Check if read-only switch is active.
  416. */
  417. if (!oldcard) {
  418. if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
  419. printk(KERN_WARNING "%s: host does not "
  420. "support reading read-only "
  421. "switch. assuming write-enable.\n",
  422. mmc_hostname(host));
  423. } else {
  424. if (host->ops->get_ro(host) > 0)
  425. mmc_card_set_readonly(card);
  426. }
  427. }
  428. if (!oldcard)
  429. host->card = card;
  430. return 0;
  431. free_card:
  432. if (!oldcard)
  433. mmc_remove_card(card);
  434. err:
  435. return err;
  436. }
  437. /*
  438. * Host is being removed. Free up the current card.
  439. */
  440. static void mmc_sd_remove(struct mmc_host *host)
  441. {
  442. BUG_ON(!host);
  443. BUG_ON(!host->card);
  444. mmc_remove_card(host->card);
  445. host->card = NULL;
  446. }
  447. /*
  448. * Card detection callback from host.
  449. */
  450. static void mmc_sd_detect(struct mmc_host *host)
  451. {
  452. int err;
  453. BUG_ON(!host);
  454. BUG_ON(!host->card);
  455. mmc_claim_host(host);
  456. /*
  457. * Just check if our card has been removed.
  458. */
  459. err = mmc_send_status(host->card, NULL);
  460. mmc_release_host(host);
  461. if (err) {
  462. mmc_sd_remove(host);
  463. mmc_claim_host(host);
  464. mmc_detach_bus(host);
  465. mmc_release_host(host);
  466. }
  467. }
  468. #ifdef CONFIG_MMC_UNSAFE_RESUME
  469. /*
  470. * Suspend callback from host.
  471. */
  472. static void mmc_sd_suspend(struct mmc_host *host)
  473. {
  474. BUG_ON(!host);
  475. BUG_ON(!host->card);
  476. mmc_claim_host(host);
  477. if (!mmc_host_is_spi(host))
  478. mmc_deselect_cards(host);
  479. host->card->state &= ~MMC_STATE_HIGHSPEED;
  480. mmc_release_host(host);
  481. }
  482. /*
  483. * Resume callback from host.
  484. *
  485. * This function tries to determine if the same card is still present
  486. * and, if so, restore all state to it.
  487. */
  488. static void mmc_sd_resume(struct mmc_host *host)
  489. {
  490. int err;
  491. BUG_ON(!host);
  492. BUG_ON(!host->card);
  493. mmc_claim_host(host);
  494. err = mmc_sd_init_card(host, host->ocr, host->card);
  495. mmc_release_host(host);
  496. if (err) {
  497. mmc_sd_remove(host);
  498. mmc_claim_host(host);
  499. mmc_detach_bus(host);
  500. mmc_release_host(host);
  501. }
  502. }
  503. #else
  504. #define mmc_sd_suspend NULL
  505. #define mmc_sd_resume NULL
  506. #endif
  507. static const struct mmc_bus_ops mmc_sd_ops = {
  508. .remove = mmc_sd_remove,
  509. .detect = mmc_sd_detect,
  510. .suspend = mmc_sd_suspend,
  511. .resume = mmc_sd_resume,
  512. };
  513. /*
  514. * Starting point for SD card init.
  515. */
  516. int mmc_attach_sd(struct mmc_host *host, u32 ocr)
  517. {
  518. int err;
  519. BUG_ON(!host);
  520. WARN_ON(!host->claimed);
  521. mmc_attach_bus(host, &mmc_sd_ops);
  522. /*
  523. * We need to get OCR a different way for SPI.
  524. */
  525. if (mmc_host_is_spi(host)) {
  526. mmc_go_idle(host);
  527. err = mmc_spi_read_ocr(host, 0, &ocr);
  528. if (err)
  529. goto err;
  530. }
  531. /*
  532. * Sanity check the voltages that the card claims to
  533. * support.
  534. */
  535. if (ocr & 0x7F) {
  536. printk(KERN_WARNING "%s: card claims to support voltages "
  537. "below the defined range. These will be ignored.\n",
  538. mmc_hostname(host));
  539. ocr &= ~0x7F;
  540. }
  541. if (ocr & MMC_VDD_165_195) {
  542. printk(KERN_WARNING "%s: SD card claims to support the "
  543. "incompletely defined 'low voltage range'. This "
  544. "will be ignored.\n", mmc_hostname(host));
  545. ocr &= ~MMC_VDD_165_195;
  546. }
  547. host->ocr = mmc_select_voltage(host, ocr);
  548. /*
  549. * Can we support the voltage(s) of the card(s)?
  550. */
  551. if (!host->ocr) {
  552. err = -EINVAL;
  553. goto err;
  554. }
  555. /*
  556. * Detect and init the card.
  557. */
  558. err = mmc_sd_init_card(host, host->ocr, NULL);
  559. if (err)
  560. goto err;
  561. mmc_release_host(host);
  562. err = mmc_add_card(host->card);
  563. if (err)
  564. goto remove_card;
  565. return 0;
  566. remove_card:
  567. mmc_remove_card(host->card);
  568. host->card = NULL;
  569. mmc_claim_host(host);
  570. err:
  571. mmc_detach_bus(host);
  572. mmc_release_host(host);
  573. printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
  574. mmc_hostname(host), err);
  575. return err;
  576. }