mmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * linux/drivers/mmc/mmc.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
  6. * MMCv4 support Copyright (C) 2006 Philip Langdale, 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. static const unsigned int tran_exp[] = {
  20. 10000, 100000, 1000000, 10000000,
  21. 0, 0, 0, 0
  22. };
  23. static const unsigned char tran_mant[] = {
  24. 0, 10, 12, 13, 15, 20, 25, 30,
  25. 35, 40, 45, 50, 55, 60, 70, 80,
  26. };
  27. static const unsigned int tacc_exp[] = {
  28. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  29. };
  30. static const unsigned int tacc_mant[] = {
  31. 0, 10, 12, 13, 15, 20, 25, 30,
  32. 35, 40, 45, 50, 55, 60, 70, 80,
  33. };
  34. #define UNSTUFF_BITS(resp,start,size) \
  35. ({ \
  36. const int __size = size; \
  37. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  38. const int __off = 3 - ((start) / 32); \
  39. const int __shft = (start) & 31; \
  40. u32 __res; \
  41. \
  42. __res = resp[__off] >> __shft; \
  43. if (__size + __shft > 32) \
  44. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  45. __res & __mask; \
  46. })
  47. /*
  48. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  49. */
  50. static void mmc_decode_cid(struct mmc_card *card)
  51. {
  52. u32 *resp = card->raw_cid;
  53. /*
  54. * The selection of the format here is based upon published
  55. * specs from sandisk and from what people have reported.
  56. */
  57. switch (card->csd.mmca_vsn) {
  58. case 0: /* MMC v1.0 - v1.2 */
  59. case 1: /* MMC v1.4 */
  60. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  61. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  62. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  63. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  64. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  65. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  66. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  67. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  68. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  69. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  70. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  71. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  72. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  73. break;
  74. case 2: /* MMC v2.0 - v2.2 */
  75. case 3: /* MMC v3.1 - v3.3 */
  76. case 4: /* MMC v4 */
  77. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  78. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  79. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  80. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  81. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  82. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  83. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  84. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  85. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  86. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  87. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  88. break;
  89. default:
  90. printk("%s: card has unknown MMCA version %d\n",
  91. mmc_hostname(card->host), card->csd.mmca_vsn);
  92. mmc_card_set_bad(card);
  93. break;
  94. }
  95. }
  96. /*
  97. * Given a 128-bit response, decode to our card CSD structure.
  98. */
  99. static void mmc_decode_csd(struct mmc_card *card)
  100. {
  101. struct mmc_csd *csd = &card->csd;
  102. unsigned int e, m, csd_struct;
  103. u32 *resp = card->raw_csd;
  104. /*
  105. * We only understand CSD structure v1.1 and v1.2.
  106. * v1.2 has extra information in bits 15, 11 and 10.
  107. */
  108. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  109. if (csd_struct != 1 && csd_struct != 2) {
  110. printk("%s: unrecognised CSD structure version %d\n",
  111. mmc_hostname(card->host), csd_struct);
  112. mmc_card_set_bad(card);
  113. return;
  114. }
  115. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  116. m = UNSTUFF_BITS(resp, 115, 4);
  117. e = UNSTUFF_BITS(resp, 112, 3);
  118. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  119. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  120. m = UNSTUFF_BITS(resp, 99, 4);
  121. e = UNSTUFF_BITS(resp, 96, 3);
  122. csd->max_dtr = tran_exp[e] * tran_mant[m];
  123. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  124. e = UNSTUFF_BITS(resp, 47, 3);
  125. m = UNSTUFF_BITS(resp, 62, 12);
  126. csd->capacity = (1 + m) << (e + 2);
  127. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  128. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  129. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  130. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  131. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  132. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  133. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  134. }
  135. /*
  136. * Read and decode extended CSD. Switch to high-speed and wide bus
  137. * if supported.
  138. */
  139. static int mmc_process_ext_csd(struct mmc_card *card)
  140. {
  141. int err;
  142. u8 *ext_csd;
  143. BUG_ON(!card);
  144. err = MMC_ERR_FAILED;
  145. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  146. return MMC_ERR_NONE;
  147. /*
  148. * As the ext_csd is so large and mostly unused, we don't store the
  149. * raw block in mmc_card.
  150. */
  151. ext_csd = kmalloc(512, GFP_KERNEL);
  152. if (!ext_csd) {
  153. printk(KERN_ERR "%s: could not allocate a buffer to "
  154. "receive the ext_csd. mmc v4 cards will be "
  155. "treated as v3.\n", mmc_hostname(card->host));
  156. return MMC_ERR_FAILED;
  157. }
  158. err = mmc_send_ext_csd(card, ext_csd);
  159. if (err != MMC_ERR_NONE) {
  160. /*
  161. * High capacity cards should have this "magic" size
  162. * stored in their CSD.
  163. */
  164. if (card->csd.capacity == (4096 * 512)) {
  165. printk(KERN_ERR "%s: unable to read EXT_CSD "
  166. "on a possible high capacity card. "
  167. "Card will be ignored.\n",
  168. mmc_hostname(card->host));
  169. } else {
  170. printk(KERN_WARNING "%s: unable to read "
  171. "EXT_CSD, performance might "
  172. "suffer.\n",
  173. mmc_hostname(card->host));
  174. err = MMC_ERR_NONE;
  175. }
  176. goto out;
  177. }
  178. card->ext_csd.sectors =
  179. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  180. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  181. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  182. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  183. if (card->ext_csd.sectors)
  184. mmc_card_set_blockaddr(card);
  185. switch (ext_csd[EXT_CSD_CARD_TYPE]) {
  186. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  187. card->ext_csd.hs_max_dtr = 52000000;
  188. break;
  189. case EXT_CSD_CARD_TYPE_26:
  190. card->ext_csd.hs_max_dtr = 26000000;
  191. break;
  192. default:
  193. /* MMC v4 spec says this cannot happen */
  194. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  195. "support any high-speed modes.\n",
  196. mmc_hostname(card->host));
  197. goto out;
  198. }
  199. if (card->host->caps & MMC_CAP_MMC_HIGHSPEED) {
  200. /* Activate highspeed support. */
  201. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  202. EXT_CSD_HS_TIMING, 1);
  203. if (err != MMC_ERR_NONE) {
  204. printk(KERN_WARNING "%s: failed to switch "
  205. "card to mmc v4 high-speed mode.\n",
  206. mmc_hostname(card->host));
  207. err = MMC_ERR_NONE;
  208. goto out;
  209. }
  210. mmc_card_set_highspeed(card);
  211. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  212. }
  213. /* Check for host support for wide-bus modes. */
  214. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  215. /* Activate 4-bit support. */
  216. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  217. EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
  218. if (err != MMC_ERR_NONE) {
  219. printk(KERN_WARNING "%s: failed to switch "
  220. "card to mmc v4 4-bit bus mode.\n",
  221. mmc_hostname(card->host));
  222. err = MMC_ERR_NONE;
  223. goto out;
  224. }
  225. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  226. }
  227. out:
  228. kfree(ext_csd);
  229. return err;
  230. }
  231. /*
  232. * Host is being removed. Free up the current card.
  233. */
  234. static void mmc_remove(struct mmc_host *host)
  235. {
  236. BUG_ON(!host);
  237. BUG_ON(!host->card);
  238. mmc_remove_card(host->card);
  239. host->card = NULL;
  240. }
  241. /*
  242. * Card detection callback from host.
  243. */
  244. static void mmc_detect(struct mmc_host *host)
  245. {
  246. int err;
  247. BUG_ON(!host);
  248. BUG_ON(!host->card);
  249. mmc_claim_host(host);
  250. /*
  251. * Just check if our card has been removed.
  252. */
  253. err = mmc_send_status(host->card, NULL);
  254. mmc_release_host(host);
  255. if (err != MMC_ERR_NONE) {
  256. mmc_remove_card(host->card);
  257. host->card = NULL;
  258. mmc_claim_host(host);
  259. mmc_detach_bus(host);
  260. mmc_release_host(host);
  261. }
  262. }
  263. static const struct mmc_bus_ops mmc_ops = {
  264. .remove = mmc_remove,
  265. .detect = mmc_detect,
  266. };
  267. /*
  268. * Starting point for MMC card init.
  269. */
  270. int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
  271. {
  272. struct mmc_card *card;
  273. int err;
  274. u32 cid[4];
  275. unsigned int max_dtr;
  276. BUG_ON(!host);
  277. BUG_ON(!host->claimed);
  278. mmc_attach_bus(host, &mmc_ops);
  279. /*
  280. * Sanity check the voltages that the card claims to
  281. * support.
  282. */
  283. if (ocr & 0x7F) {
  284. printk(KERN_WARNING "%s: card claims to support voltages "
  285. "below the defined range. These will be ignored.\n",
  286. mmc_hostname(host));
  287. ocr &= ~0x7F;
  288. }
  289. host->ocr = mmc_select_voltage(host, ocr);
  290. /*
  291. * Can we support the voltage of the card?
  292. */
  293. if (!host->ocr)
  294. goto err;
  295. /*
  296. * Since we're changing the OCR value, we seem to
  297. * need to tell some cards to go back to the idle
  298. * state. We wait 1ms to give cards time to
  299. * respond.
  300. */
  301. mmc_go_idle(host);
  302. /* The extra bit indicates that we support high capacity */
  303. mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
  304. /*
  305. * Fetch CID from card.
  306. */
  307. err = mmc_all_send_cid(host, cid);
  308. if (err != MMC_ERR_NONE)
  309. goto err;
  310. /*
  311. * Allocate card structure.
  312. */
  313. card = mmc_alloc_card(host);
  314. if (IS_ERR(card))
  315. goto err;
  316. card->type = MMC_TYPE_MMC;
  317. card->rca = 1;
  318. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  319. /*
  320. * Set card RCA.
  321. */
  322. err = mmc_set_relative_addr(card);
  323. if (err != MMC_ERR_NONE)
  324. goto free_card;
  325. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  326. /*
  327. * Fetch CSD from card.
  328. */
  329. err = mmc_send_csd(card, card->raw_csd);
  330. if (err != MMC_ERR_NONE)
  331. goto free_card;
  332. mmc_decode_csd(card);
  333. mmc_decode_cid(card);
  334. /*
  335. * Fetch and process extened CSD.
  336. * This will switch into high-speed and wide bus modes,
  337. * as available.
  338. */
  339. err = mmc_select_card(card);
  340. if (err != MMC_ERR_NONE)
  341. goto free_card;
  342. err = mmc_process_ext_csd(card);
  343. if (err != MMC_ERR_NONE)
  344. goto free_card;
  345. /*
  346. * Compute bus speed.
  347. */
  348. max_dtr = (unsigned int)-1;
  349. if (mmc_card_highspeed(card)) {
  350. if (max_dtr > card->ext_csd.hs_max_dtr)
  351. max_dtr = card->ext_csd.hs_max_dtr;
  352. } else if (max_dtr > card->csd.max_dtr) {
  353. max_dtr = card->csd.max_dtr;
  354. }
  355. mmc_set_clock(host, max_dtr);
  356. host->card = card;
  357. mmc_release_host(host);
  358. err = mmc_register_card(card);
  359. if (err)
  360. goto reclaim_host;
  361. return 0;
  362. reclaim_host:
  363. mmc_claim_host(host);
  364. free_card:
  365. mmc_remove_card(card);
  366. host->card = NULL;
  367. err:
  368. mmc_detach_bus(host);
  369. mmc_release_host(host);
  370. return 0;
  371. }