sd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 "core.h"
  17. #include "sysfs.h"
  18. #include "mmc_ops.h"
  19. #include "sd_ops.h"
  20. #include "core.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("%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. BUG_ON(!mmc_card_sd(card));
  144. resp[3] = card->raw_scr[1];
  145. resp[2] = card->raw_scr[0];
  146. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  147. if (scr_struct != 0) {
  148. printk("%s: unrecognised SCR structure version %d\n",
  149. mmc_hostname(card->host), scr_struct);
  150. return -EINVAL;
  151. }
  152. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  153. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  154. return 0;
  155. }
  156. /*
  157. * Fetches and decodes switch information
  158. */
  159. static int mmc_read_switch(struct mmc_card *card)
  160. {
  161. int err;
  162. u8 *status;
  163. err = MMC_ERR_FAILED;
  164. status = kmalloc(64, GFP_KERNEL);
  165. if (!status) {
  166. printk("%s: could not allocate a buffer for switch "
  167. "capabilities.\n",
  168. mmc_hostname(card->host));
  169. return err;
  170. }
  171. err = mmc_sd_switch(card, 0, 0, 1, status);
  172. if (err != MMC_ERR_NONE) {
  173. /*
  174. * Card not supporting high-speed will ignore the
  175. * command.
  176. */
  177. err = MMC_ERR_NONE;
  178. goto out;
  179. }
  180. if (status[13] & 0x02)
  181. card->sw_caps.hs_max_dtr = 50000000;
  182. out:
  183. kfree(status);
  184. return err;
  185. }
  186. /*
  187. * Test if the card supports high-speed mode and, if so, switch to it.
  188. */
  189. static int mmc_switch_hs(struct mmc_card *card)
  190. {
  191. int err;
  192. u8 *status;
  193. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  194. return MMC_ERR_NONE;
  195. if (card->sw_caps.hs_max_dtr == 0)
  196. return MMC_ERR_NONE;
  197. err = MMC_ERR_FAILED;
  198. status = kmalloc(64, GFP_KERNEL);
  199. if (!status) {
  200. printk("%s: could not allocate a buffer for switch "
  201. "capabilities.\n",
  202. mmc_hostname(card->host));
  203. return err;
  204. }
  205. err = mmc_sd_switch(card, 1, 0, 1, status);
  206. if (err != MMC_ERR_NONE)
  207. goto out;
  208. if ((status[16] & 0xF) != 1) {
  209. printk(KERN_WARNING "%s: Problem switching card "
  210. "into high-speed mode!\n",
  211. mmc_hostname(card->host));
  212. } else {
  213. mmc_card_set_highspeed(card);
  214. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  215. }
  216. out:
  217. kfree(status);
  218. return err;
  219. }
  220. /*
  221. * Handle the detection and initialisation of a card.
  222. *
  223. * In the case of a resume, "curcard" will contain the card
  224. * we're trying to reinitialise.
  225. */
  226. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  227. struct mmc_card *oldcard)
  228. {
  229. struct mmc_card *card;
  230. int err;
  231. u32 cid[4];
  232. unsigned int max_dtr;
  233. BUG_ON(!host);
  234. BUG_ON(!host->claimed);
  235. /*
  236. * Since we're changing the OCR value, we seem to
  237. * need to tell some cards to go back to the idle
  238. * state. We wait 1ms to give cards time to
  239. * respond.
  240. */
  241. mmc_go_idle(host);
  242. /*
  243. * If SD_SEND_IF_COND indicates an SD 2.0
  244. * compliant card and we should set bit 30
  245. * of the ocr to indicate that we can handle
  246. * block-addressed SDHC cards.
  247. */
  248. err = mmc_send_if_cond(host, ocr);
  249. if (err == MMC_ERR_NONE)
  250. ocr |= 1 << 30;
  251. err = mmc_send_app_op_cond(host, ocr, NULL);
  252. if (err != MMC_ERR_NONE)
  253. goto err;
  254. /*
  255. * Fetch CID from card.
  256. */
  257. err = mmc_all_send_cid(host, cid);
  258. if (err != MMC_ERR_NONE)
  259. goto err;
  260. if (oldcard) {
  261. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
  262. goto err;
  263. card = oldcard;
  264. } else {
  265. /*
  266. * Allocate card structure.
  267. */
  268. card = mmc_alloc_card(host);
  269. if (IS_ERR(card))
  270. goto err;
  271. card->type = MMC_TYPE_SD;
  272. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  273. }
  274. /*
  275. * Set card RCA.
  276. */
  277. err = mmc_send_relative_addr(host, &card->rca);
  278. if (err != MMC_ERR_NONE)
  279. goto free_card;
  280. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  281. if (!oldcard) {
  282. /*
  283. * Fetch CSD from card.
  284. */
  285. err = mmc_send_csd(card, card->raw_csd);
  286. if (err != MMC_ERR_NONE)
  287. goto free_card;
  288. err = mmc_decode_csd(card);
  289. if (err < 0)
  290. goto free_card;
  291. mmc_decode_cid(card);
  292. }
  293. /*
  294. * Select card, as all following commands rely on that.
  295. */
  296. err = mmc_select_card(card);
  297. if (err != MMC_ERR_NONE)
  298. goto free_card;
  299. if (!oldcard) {
  300. /*
  301. * Fetch SCR from card.
  302. */
  303. err = mmc_app_send_scr(card, card->raw_scr);
  304. if (err != MMC_ERR_NONE)
  305. goto free_card;
  306. err = mmc_decode_scr(card);
  307. if (err < 0)
  308. goto free_card;
  309. /*
  310. * Fetch switch information from card.
  311. */
  312. err = mmc_read_switch(card);
  313. if (err != MMC_ERR_NONE)
  314. goto free_card;
  315. }
  316. /*
  317. * Attempt to change to high-speed (if supported)
  318. */
  319. err = mmc_switch_hs(card);
  320. if (err != MMC_ERR_NONE)
  321. goto free_card;
  322. /*
  323. * Compute bus speed.
  324. */
  325. max_dtr = (unsigned int)-1;
  326. if (mmc_card_highspeed(card)) {
  327. if (max_dtr > card->sw_caps.hs_max_dtr)
  328. max_dtr = card->sw_caps.hs_max_dtr;
  329. } else if (max_dtr > card->csd.max_dtr) {
  330. max_dtr = card->csd.max_dtr;
  331. }
  332. mmc_set_clock(host, max_dtr);
  333. /*
  334. * Switch to wider bus (if supported).
  335. */
  336. if ((host->caps && MMC_CAP_4_BIT_DATA) &&
  337. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  338. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  339. if (err != MMC_ERR_NONE)
  340. goto free_card;
  341. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  342. }
  343. if (!oldcard)
  344. host->card = card;
  345. return MMC_ERR_NONE;
  346. free_card:
  347. if (!oldcard)
  348. mmc_remove_card(card);
  349. err:
  350. return MMC_ERR_FAILED;
  351. }
  352. /*
  353. * Host is being removed. Free up the current card.
  354. */
  355. static void mmc_sd_remove(struct mmc_host *host)
  356. {
  357. BUG_ON(!host);
  358. BUG_ON(!host->card);
  359. mmc_remove_card(host->card);
  360. host->card = NULL;
  361. }
  362. /*
  363. * Card detection callback from host.
  364. */
  365. static void mmc_sd_detect(struct mmc_host *host)
  366. {
  367. int err;
  368. BUG_ON(!host);
  369. BUG_ON(!host->card);
  370. mmc_claim_host(host);
  371. /*
  372. * Just check if our card has been removed.
  373. */
  374. err = mmc_send_status(host->card, NULL);
  375. mmc_release_host(host);
  376. if (err != MMC_ERR_NONE) {
  377. mmc_remove_card(host->card);
  378. host->card = NULL;
  379. mmc_claim_host(host);
  380. mmc_detach_bus(host);
  381. mmc_release_host(host);
  382. }
  383. }
  384. #ifdef CONFIG_MMC_UNSAFE_RESUME
  385. /*
  386. * Suspend callback from host.
  387. */
  388. static void mmc_sd_suspend(struct mmc_host *host)
  389. {
  390. BUG_ON(!host);
  391. BUG_ON(!host->card);
  392. mmc_claim_host(host);
  393. mmc_deselect_cards(host);
  394. host->card->state &= ~MMC_STATE_HIGHSPEED;
  395. mmc_release_host(host);
  396. }
  397. /*
  398. * Resume callback from host.
  399. *
  400. * This function tries to determine if the same card is still present
  401. * and, if so, restore all state to it.
  402. */
  403. static void mmc_sd_resume(struct mmc_host *host)
  404. {
  405. int err;
  406. BUG_ON(!host);
  407. BUG_ON(!host->card);
  408. mmc_claim_host(host);
  409. err = mmc_sd_init_card(host, host->ocr, host->card);
  410. if (err != MMC_ERR_NONE) {
  411. mmc_remove_card(host->card);
  412. host->card = NULL;
  413. mmc_detach_bus(host);
  414. }
  415. mmc_release_host(host);
  416. }
  417. #else
  418. #define mmc_sd_suspend NULL
  419. #define mmc_sd_resume NULL
  420. #endif
  421. static const struct mmc_bus_ops mmc_sd_ops = {
  422. .remove = mmc_sd_remove,
  423. .detect = mmc_sd_detect,
  424. .suspend = mmc_sd_suspend,
  425. .resume = mmc_sd_resume,
  426. };
  427. /*
  428. * Starting point for SD card init.
  429. */
  430. int mmc_attach_sd(struct mmc_host *host, u32 ocr)
  431. {
  432. int err;
  433. BUG_ON(!host);
  434. BUG_ON(!host->claimed);
  435. mmc_attach_bus(host, &mmc_sd_ops);
  436. /*
  437. * Sanity check the voltages that the card claims to
  438. * support.
  439. */
  440. if (ocr & 0x7F) {
  441. printk(KERN_WARNING "%s: card claims to support voltages "
  442. "below the defined range. These will be ignored.\n",
  443. mmc_hostname(host));
  444. ocr &= ~0x7F;
  445. }
  446. if (ocr & MMC_VDD_165_195) {
  447. printk(KERN_WARNING "%s: SD card claims to support the "
  448. "incompletely defined 'low voltage range'. This "
  449. "will be ignored.\n", mmc_hostname(host));
  450. ocr &= ~MMC_VDD_165_195;
  451. }
  452. host->ocr = mmc_select_voltage(host, ocr);
  453. /*
  454. * Can we support the voltage(s) of the card(s)?
  455. */
  456. if (!host->ocr)
  457. goto err;
  458. /*
  459. * Detect and init the card.
  460. */
  461. err = mmc_sd_init_card(host, host->ocr, NULL);
  462. if (err != MMC_ERR_NONE)
  463. goto err;
  464. mmc_release_host(host);
  465. err = mmc_register_card(host->card);
  466. if (err)
  467. goto reclaim_host;
  468. return 0;
  469. reclaim_host:
  470. mmc_claim_host(host);
  471. mmc_remove_card(host->card);
  472. host->card = NULL;
  473. err:
  474. mmc_detach_bus(host);
  475. mmc_release_host(host);
  476. return 0;
  477. }