sd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. * For SPI, enable CRC as appropriate.
  305. */
  306. if (mmc_host_is_spi(host)) {
  307. err = mmc_spi_set_crc(host, use_spi_crc);
  308. if (err)
  309. goto err;
  310. }
  311. /*
  312. * Fetch CID from card.
  313. */
  314. if (mmc_host_is_spi(host))
  315. err = mmc_send_cid(host, cid);
  316. else
  317. err = mmc_all_send_cid(host, cid);
  318. if (err)
  319. goto err;
  320. if (oldcard) {
  321. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  322. err = -ENOENT;
  323. goto err;
  324. }
  325. card = oldcard;
  326. } else {
  327. /*
  328. * Allocate card structure.
  329. */
  330. card = mmc_alloc_card(host, &sd_type);
  331. if (IS_ERR(card)) {
  332. err = PTR_ERR(card);
  333. goto err;
  334. }
  335. card->type = MMC_TYPE_SD;
  336. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  337. }
  338. /*
  339. * For native busses: get card RCA and quit open drain mode.
  340. */
  341. if (!mmc_host_is_spi(host)) {
  342. err = mmc_send_relative_addr(host, &card->rca);
  343. if (err)
  344. goto free_card;
  345. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  346. }
  347. if (!oldcard) {
  348. /*
  349. * Fetch CSD from card.
  350. */
  351. err = mmc_send_csd(card, card->raw_csd);
  352. if (err)
  353. goto free_card;
  354. err = mmc_decode_csd(card);
  355. if (err)
  356. goto free_card;
  357. mmc_decode_cid(card);
  358. }
  359. /*
  360. * Select card, as all following commands rely on that.
  361. */
  362. if (!mmc_host_is_spi(host)) {
  363. err = mmc_select_card(card);
  364. if (err)
  365. goto free_card;
  366. }
  367. if (!oldcard) {
  368. /*
  369. * Fetch SCR from card.
  370. */
  371. err = mmc_app_send_scr(card, card->raw_scr);
  372. if (err)
  373. goto free_card;
  374. err = mmc_decode_scr(card);
  375. if (err < 0)
  376. goto free_card;
  377. /*
  378. * Fetch switch information from card.
  379. */
  380. err = mmc_read_switch(card);
  381. if (err)
  382. goto free_card;
  383. }
  384. /*
  385. * Attempt to change to high-speed (if supported)
  386. */
  387. err = mmc_switch_hs(card);
  388. if (err)
  389. goto free_card;
  390. /*
  391. * Compute bus speed.
  392. */
  393. max_dtr = (unsigned int)-1;
  394. if (mmc_card_highspeed(card)) {
  395. if (max_dtr > card->sw_caps.hs_max_dtr)
  396. max_dtr = card->sw_caps.hs_max_dtr;
  397. } else if (max_dtr > card->csd.max_dtr) {
  398. max_dtr = card->csd.max_dtr;
  399. }
  400. mmc_set_clock(host, max_dtr);
  401. /*
  402. * Switch to wider bus (if supported).
  403. */
  404. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  405. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  406. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  407. if (err)
  408. goto free_card;
  409. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  410. }
  411. /*
  412. * Check if read-only switch is active.
  413. */
  414. if (!oldcard) {
  415. if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
  416. printk(KERN_WARNING "%s: host does not "
  417. "support reading read-only "
  418. "switch. assuming write-enable.\n",
  419. mmc_hostname(host));
  420. } else {
  421. if (host->ops->get_ro(host) > 0)
  422. mmc_card_set_readonly(card);
  423. }
  424. }
  425. if (!oldcard)
  426. host->card = card;
  427. return 0;
  428. free_card:
  429. if (!oldcard)
  430. mmc_remove_card(card);
  431. err:
  432. return err;
  433. }
  434. /*
  435. * Host is being removed. Free up the current card.
  436. */
  437. static void mmc_sd_remove(struct mmc_host *host)
  438. {
  439. BUG_ON(!host);
  440. BUG_ON(!host->card);
  441. mmc_remove_card(host->card);
  442. host->card = NULL;
  443. }
  444. /*
  445. * Card detection callback from host.
  446. */
  447. static void mmc_sd_detect(struct mmc_host *host)
  448. {
  449. int err;
  450. BUG_ON(!host);
  451. BUG_ON(!host->card);
  452. mmc_claim_host(host);
  453. /*
  454. * Just check if our card has been removed.
  455. */
  456. err = mmc_send_status(host->card, NULL);
  457. mmc_release_host(host);
  458. if (err) {
  459. mmc_sd_remove(host);
  460. mmc_claim_host(host);
  461. mmc_detach_bus(host);
  462. mmc_release_host(host);
  463. }
  464. }
  465. #ifdef CONFIG_MMC_UNSAFE_RESUME
  466. /*
  467. * Suspend callback from host.
  468. */
  469. static void mmc_sd_suspend(struct mmc_host *host)
  470. {
  471. BUG_ON(!host);
  472. BUG_ON(!host->card);
  473. mmc_claim_host(host);
  474. if (!mmc_host_is_spi(host))
  475. mmc_deselect_cards(host);
  476. host->card->state &= ~MMC_STATE_HIGHSPEED;
  477. mmc_release_host(host);
  478. }
  479. /*
  480. * Resume callback from host.
  481. *
  482. * This function tries to determine if the same card is still present
  483. * and, if so, restore all state to it.
  484. */
  485. static void mmc_sd_resume(struct mmc_host *host)
  486. {
  487. int err;
  488. BUG_ON(!host);
  489. BUG_ON(!host->card);
  490. mmc_claim_host(host);
  491. err = mmc_sd_init_card(host, host->ocr, host->card);
  492. mmc_release_host(host);
  493. if (err) {
  494. mmc_sd_remove(host);
  495. mmc_claim_host(host);
  496. mmc_detach_bus(host);
  497. mmc_release_host(host);
  498. }
  499. }
  500. #else
  501. #define mmc_sd_suspend NULL
  502. #define mmc_sd_resume NULL
  503. #endif
  504. static const struct mmc_bus_ops mmc_sd_ops = {
  505. .remove = mmc_sd_remove,
  506. .detect = mmc_sd_detect,
  507. .suspend = mmc_sd_suspend,
  508. .resume = mmc_sd_resume,
  509. };
  510. /*
  511. * Starting point for SD card init.
  512. */
  513. int mmc_attach_sd(struct mmc_host *host, u32 ocr)
  514. {
  515. int err;
  516. BUG_ON(!host);
  517. WARN_ON(!host->claimed);
  518. mmc_attach_bus(host, &mmc_sd_ops);
  519. /*
  520. * We need to get OCR a different way for SPI.
  521. */
  522. if (mmc_host_is_spi(host)) {
  523. mmc_go_idle(host);
  524. err = mmc_spi_read_ocr(host, 0, &ocr);
  525. if (err)
  526. goto err;
  527. }
  528. /*
  529. * Sanity check the voltages that the card claims to
  530. * support.
  531. */
  532. if (ocr & 0x7F) {
  533. printk(KERN_WARNING "%s: card claims to support voltages "
  534. "below the defined range. These will be ignored.\n",
  535. mmc_hostname(host));
  536. ocr &= ~0x7F;
  537. }
  538. if (ocr & MMC_VDD_165_195) {
  539. printk(KERN_WARNING "%s: SD card claims to support the "
  540. "incompletely defined 'low voltage range'. This "
  541. "will be ignored.\n", mmc_hostname(host));
  542. ocr &= ~MMC_VDD_165_195;
  543. }
  544. host->ocr = mmc_select_voltage(host, ocr);
  545. /*
  546. * Can we support the voltage(s) of the card(s)?
  547. */
  548. if (!host->ocr) {
  549. err = -EINVAL;
  550. goto err;
  551. }
  552. /*
  553. * Detect and init the card.
  554. */
  555. err = mmc_sd_init_card(host, host->ocr, NULL);
  556. if (err)
  557. goto err;
  558. mmc_release_host(host);
  559. err = mmc_add_card(host->card);
  560. if (err)
  561. goto remove_card;
  562. return 0;
  563. remove_card:
  564. mmc_remove_card(host->card);
  565. host->card = NULL;
  566. mmc_claim_host(host);
  567. err:
  568. mmc_detach_bus(host);
  569. mmc_release_host(host);
  570. printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
  571. mmc_hostname(host), err);
  572. return err;
  573. }