sd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 void 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. mmc_card_set_bad(card);
  132. return;
  133. }
  134. }
  135. /*
  136. * Given a 64-bit response, decode to our card SCR structure.
  137. */
  138. static void 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. mmc_card_set_bad(card);
  151. return;
  152. }
  153. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  154. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  155. }
  156. /*
  157. * Test if the card supports high-speed mode and, if so, switch to it.
  158. */
  159. static int mmc_switch_hs(struct mmc_card *card)
  160. {
  161. int err;
  162. u8 *status;
  163. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  164. return MMC_ERR_NONE;
  165. err = MMC_ERR_FAILED;
  166. status = kmalloc(64, GFP_KERNEL);
  167. if (!status) {
  168. printk("%s: could not allocate a buffer for switch "
  169. "capabilities.\n",
  170. mmc_hostname(card->host));
  171. return err;
  172. }
  173. err = mmc_sd_switch(card, 0, 0, 1, status);
  174. if (err != MMC_ERR_NONE) {
  175. /*
  176. * Card not supporting high-speed will ignore the
  177. * command.
  178. */
  179. if (err == MMC_ERR_TIMEOUT)
  180. err = MMC_ERR_NONE;
  181. goto out;
  182. }
  183. if (status[13] & 0x02)
  184. card->sw_caps.hs_max_dtr = 50000000;
  185. err = mmc_sd_switch(card, 1, 0, 1, status);
  186. if (err != MMC_ERR_NONE)
  187. goto out;
  188. if ((status[16] & 0xF) != 1) {
  189. printk(KERN_WARNING "%s: Problem switching card "
  190. "into high-speed mode!\n",
  191. mmc_hostname(card->host));
  192. } else {
  193. mmc_card_set_highspeed(card);
  194. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  195. }
  196. out:
  197. kfree(status);
  198. return err;
  199. }
  200. /*
  201. * Host is being removed. Free up the current card.
  202. */
  203. static void mmc_sd_remove(struct mmc_host *host)
  204. {
  205. BUG_ON(!host);
  206. BUG_ON(!host->card);
  207. mmc_remove_card(host->card);
  208. host->card = NULL;
  209. }
  210. /*
  211. * Card detection callback from host.
  212. */
  213. static void mmc_sd_detect(struct mmc_host *host)
  214. {
  215. int err;
  216. BUG_ON(!host);
  217. BUG_ON(!host->card);
  218. mmc_claim_host(host);
  219. /*
  220. * Just check if our card has been removed.
  221. */
  222. err = mmc_send_status(host->card, NULL);
  223. mmc_release_host(host);
  224. if (err != MMC_ERR_NONE) {
  225. mmc_remove_card(host->card);
  226. host->card = NULL;
  227. mmc_claim_host(host);
  228. mmc_detach_bus(host);
  229. mmc_release_host(host);
  230. }
  231. }
  232. static const struct mmc_bus_ops mmc_sd_ops = {
  233. .remove = mmc_sd_remove,
  234. .detect = mmc_sd_detect,
  235. };
  236. /*
  237. * Starting point for SD card init.
  238. */
  239. int mmc_attach_sd(struct mmc_host *host, u32 ocr)
  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. mmc_attach_bus(host, &mmc_sd_ops);
  248. /*
  249. * Sanity check the voltages that the card claims to
  250. * support.
  251. */
  252. if (ocr & 0x7F) {
  253. printk(KERN_WARNING "%s: card claims to support voltages "
  254. "below the defined range. These will be ignored.\n",
  255. mmc_hostname(host));
  256. ocr &= ~0x7F;
  257. }
  258. if (ocr & MMC_VDD_165_195) {
  259. printk(KERN_WARNING "%s: SD card claims to support the "
  260. "incompletely defined 'low voltage range'. This "
  261. "will be ignored.\n", mmc_hostname(host));
  262. ocr &= ~MMC_VDD_165_195;
  263. }
  264. host->ocr = mmc_select_voltage(host, ocr);
  265. /*
  266. * Can we support the voltage(s) of the card(s)?
  267. */
  268. if (!host->ocr)
  269. goto err;
  270. /*
  271. * Since we're changing the OCR value, we seem to
  272. * need to tell some cards to go back to the idle
  273. * state. We wait 1ms to give cards time to
  274. * respond.
  275. */
  276. mmc_go_idle(host);
  277. /*
  278. * If SD_SEND_IF_COND indicates an SD 2.0
  279. * compliant card and we should set bit 30
  280. * of the ocr to indicate that we can handle
  281. * block-addressed SDHC cards.
  282. */
  283. err = mmc_send_if_cond(host, host->ocr);
  284. if (err == MMC_ERR_NONE)
  285. ocr = host->ocr | (1 << 30);
  286. mmc_send_app_op_cond(host, ocr, NULL);
  287. /*
  288. * Fetch CID from card.
  289. */
  290. err = mmc_all_send_cid(host, cid);
  291. if (err != MMC_ERR_NONE)
  292. goto err;
  293. /*
  294. * Allocate card structure.
  295. */
  296. card = mmc_alloc_card(host);
  297. if (IS_ERR(card))
  298. goto err;
  299. card->type = MMC_TYPE_SD;
  300. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  301. /*
  302. * Set card RCA.
  303. */
  304. err = mmc_send_relative_addr(host, &card->rca);
  305. if (err != MMC_ERR_NONE)
  306. goto free_card;
  307. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  308. /*
  309. * Fetch CSD from card.
  310. */
  311. err = mmc_send_csd(card, card->raw_csd);
  312. if (err != MMC_ERR_NONE)
  313. goto free_card;
  314. mmc_decode_csd(card);
  315. mmc_decode_cid(card);
  316. /*
  317. * Fetch SCR from card.
  318. */
  319. err = mmc_select_card(card);
  320. if (err != MMC_ERR_NONE)
  321. goto free_card;
  322. err = mmc_app_send_scr(card, card->raw_scr);
  323. if (err != MMC_ERR_NONE)
  324. goto free_card;
  325. mmc_decode_scr(card);
  326. /*
  327. * Check if card can be switched into high-speed mode.
  328. */
  329. err = mmc_switch_hs(card);
  330. if (err != MMC_ERR_NONE)
  331. goto free_card;
  332. /*
  333. * Compute bus speed.
  334. */
  335. max_dtr = (unsigned int)-1;
  336. if (mmc_card_highspeed(card)) {
  337. if (max_dtr > card->sw_caps.hs_max_dtr)
  338. max_dtr = card->sw_caps.hs_max_dtr;
  339. } else if (max_dtr > card->csd.max_dtr) {
  340. max_dtr = card->csd.max_dtr;
  341. }
  342. mmc_set_clock(host, max_dtr);
  343. /*
  344. * Switch to wider bus (if supported).
  345. */
  346. if ((host->caps && MMC_CAP_4_BIT_DATA) &&
  347. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  348. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  349. if (err != MMC_ERR_NONE)
  350. goto free_card;
  351. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  352. }
  353. host->card = card;
  354. mmc_release_host(host);
  355. err = mmc_register_card(card);
  356. if (err)
  357. goto reclaim_host;
  358. return 0;
  359. reclaim_host:
  360. mmc_claim_host(host);
  361. free_card:
  362. mmc_remove_card(card);
  363. host->card = NULL;
  364. err:
  365. mmc_detach_bus(host);
  366. mmc_release_host(host);
  367. return 0;
  368. }