sd.c 13 KB

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