sd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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/slab.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/mmc/sd.h>
  18. #include "core.h"
  19. #include "bus.h"
  20. #include "mmc_ops.h"
  21. #include "sd.h"
  22. #include "sd_ops.h"
  23. static const unsigned int tran_exp[] = {
  24. 10000, 100000, 1000000, 10000000,
  25. 0, 0, 0, 0
  26. };
  27. static const unsigned char tran_mant[] = {
  28. 0, 10, 12, 13, 15, 20, 25, 30,
  29. 35, 40, 45, 50, 55, 60, 70, 80,
  30. };
  31. static const unsigned int tacc_exp[] = {
  32. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  33. };
  34. static const unsigned int tacc_mant[] = {
  35. 0, 10, 12, 13, 15, 20, 25, 30,
  36. 35, 40, 45, 50, 55, 60, 70, 80,
  37. };
  38. #define UNSTUFF_BITS(resp,start,size) \
  39. ({ \
  40. const int __size = size; \
  41. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  42. const int __off = 3 - ((start) / 32); \
  43. const int __shft = (start) & 31; \
  44. u32 __res; \
  45. \
  46. __res = resp[__off] >> __shft; \
  47. if (__size + __shft > 32) \
  48. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  49. __res & __mask; \
  50. })
  51. /*
  52. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  53. */
  54. void mmc_decode_cid(struct mmc_card *card)
  55. {
  56. u32 *resp = card->raw_cid;
  57. memset(&card->cid, 0, sizeof(struct mmc_cid));
  58. /*
  59. * SD doesn't currently have a version field so we will
  60. * have to assume we can parse this.
  61. */
  62. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  63. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  64. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  65. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  66. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  67. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  68. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  69. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  70. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  71. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  72. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  73. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  74. card->cid.year += 2000; /* SD cards year offset */
  75. }
  76. /*
  77. * Given a 128-bit response, decode to our card CSD structure.
  78. */
  79. static int mmc_decode_csd(struct mmc_card *card)
  80. {
  81. struct mmc_csd *csd = &card->csd;
  82. unsigned int e, m, csd_struct;
  83. u32 *resp = card->raw_csd;
  84. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  85. switch (csd_struct) {
  86. case 0:
  87. m = UNSTUFF_BITS(resp, 115, 4);
  88. e = UNSTUFF_BITS(resp, 112, 3);
  89. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  90. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  91. m = UNSTUFF_BITS(resp, 99, 4);
  92. e = UNSTUFF_BITS(resp, 96, 3);
  93. csd->max_dtr = tran_exp[e] * tran_mant[m];
  94. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  95. e = UNSTUFF_BITS(resp, 47, 3);
  96. m = UNSTUFF_BITS(resp, 62, 12);
  97. csd->capacity = (1 + m) << (e + 2);
  98. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  99. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  100. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  101. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  102. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  103. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  104. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  105. if (UNSTUFF_BITS(resp, 46, 1)) {
  106. csd->erase_size = 1;
  107. } else if (csd->write_blkbits >= 9) {
  108. csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
  109. csd->erase_size <<= csd->write_blkbits - 9;
  110. }
  111. break;
  112. case 1:
  113. /*
  114. * This is a block-addressed SDHC or SDXC card. Most
  115. * interesting fields are unused and have fixed
  116. * values. To avoid getting tripped by buggy cards,
  117. * we assume those fixed values ourselves.
  118. */
  119. mmc_card_set_blockaddr(card);
  120. csd->tacc_ns = 0; /* Unused */
  121. csd->tacc_clks = 0; /* Unused */
  122. m = UNSTUFF_BITS(resp, 99, 4);
  123. e = UNSTUFF_BITS(resp, 96, 3);
  124. csd->max_dtr = tran_exp[e] * tran_mant[m];
  125. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  126. csd->c_size = UNSTUFF_BITS(resp, 48, 22);
  127. /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
  128. if (csd->c_size >= 0xFFFF)
  129. mmc_card_set_ext_capacity(card);
  130. m = UNSTUFF_BITS(resp, 48, 22);
  131. csd->capacity = (1 + m) << 10;
  132. csd->read_blkbits = 9;
  133. csd->read_partial = 0;
  134. csd->write_misalign = 0;
  135. csd->read_misalign = 0;
  136. csd->r2w_factor = 4; /* Unused */
  137. csd->write_blkbits = 9;
  138. csd->write_partial = 0;
  139. csd->erase_size = 1;
  140. break;
  141. default:
  142. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  143. mmc_hostname(card->host), csd_struct);
  144. return -EINVAL;
  145. }
  146. card->erase_size = csd->erase_size;
  147. return 0;
  148. }
  149. /*
  150. * Given a 64-bit response, decode to our card SCR structure.
  151. */
  152. static int mmc_decode_scr(struct mmc_card *card)
  153. {
  154. struct sd_scr *scr = &card->scr;
  155. unsigned int scr_struct;
  156. u32 resp[4];
  157. resp[3] = card->raw_scr[1];
  158. resp[2] = card->raw_scr[0];
  159. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  160. if (scr_struct != 0) {
  161. printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
  162. mmc_hostname(card->host), scr_struct);
  163. return -EINVAL;
  164. }
  165. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  166. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  167. if (scr->sda_vsn == SCR_SPEC_VER_2)
  168. /* Check if Physical Layer Spec v3.0 is supported */
  169. scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
  170. if (UNSTUFF_BITS(resp, 55, 1))
  171. card->erased_byte = 0xFF;
  172. else
  173. card->erased_byte = 0x0;
  174. if (scr->sda_spec3)
  175. scr->cmds = UNSTUFF_BITS(resp, 32, 2);
  176. return 0;
  177. }
  178. /*
  179. * Fetch and process SD Status register.
  180. */
  181. static int mmc_read_ssr(struct mmc_card *card)
  182. {
  183. unsigned int au, es, et, eo;
  184. int err, i;
  185. u32 *ssr;
  186. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  187. printk(KERN_WARNING "%s: card lacks mandatory SD Status "
  188. "function.\n", mmc_hostname(card->host));
  189. return 0;
  190. }
  191. ssr = kmalloc(64, GFP_KERNEL);
  192. if (!ssr)
  193. return -ENOMEM;
  194. err = mmc_app_sd_status(card, ssr);
  195. if (err) {
  196. printk(KERN_WARNING "%s: problem reading SD Status "
  197. "register.\n", mmc_hostname(card->host));
  198. err = 0;
  199. goto out;
  200. }
  201. for (i = 0; i < 16; i++)
  202. ssr[i] = be32_to_cpu(ssr[i]);
  203. /*
  204. * UNSTUFF_BITS only works with four u32s so we have to offset the
  205. * bitfield positions accordingly.
  206. */
  207. au = UNSTUFF_BITS(ssr, 428 - 384, 4);
  208. if (au > 0 || au <= 9) {
  209. card->ssr.au = 1 << (au + 4);
  210. es = UNSTUFF_BITS(ssr, 408 - 384, 16);
  211. et = UNSTUFF_BITS(ssr, 402 - 384, 6);
  212. eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
  213. if (es && et) {
  214. card->ssr.erase_timeout = (et * 1000) / es;
  215. card->ssr.erase_offset = eo * 1000;
  216. }
  217. } else {
  218. printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
  219. "size.\n", mmc_hostname(card->host));
  220. }
  221. out:
  222. kfree(ssr);
  223. return err;
  224. }
  225. /*
  226. * Fetches and decodes switch information
  227. */
  228. static int mmc_read_switch(struct mmc_card *card)
  229. {
  230. int err;
  231. u8 *status;
  232. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  233. return 0;
  234. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  235. printk(KERN_WARNING "%s: card lacks mandatory switch "
  236. "function, performance might suffer.\n",
  237. mmc_hostname(card->host));
  238. return 0;
  239. }
  240. err = -EIO;
  241. status = kmalloc(64, GFP_KERNEL);
  242. if (!status) {
  243. printk(KERN_ERR "%s: could not allocate a buffer for "
  244. "switch capabilities.\n",
  245. mmc_hostname(card->host));
  246. return -ENOMEM;
  247. }
  248. /* Find out the supported Bus Speed Modes. */
  249. err = mmc_sd_switch(card, 0, 0, 1, status);
  250. if (err) {
  251. /*
  252. * If the host or the card can't do the switch,
  253. * fail more gracefully.
  254. */
  255. if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
  256. goto out;
  257. printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n",
  258. mmc_hostname(card->host));
  259. err = 0;
  260. goto out;
  261. }
  262. if (card->scr.sda_spec3) {
  263. card->sw_caps.sd3_bus_mode = status[13];
  264. /* Find out Driver Strengths supported by the card */
  265. err = mmc_sd_switch(card, 0, 2, 1, status);
  266. if (err) {
  267. /*
  268. * If the host or the card can't do the switch,
  269. * fail more gracefully.
  270. */
  271. if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
  272. goto out;
  273. printk(KERN_WARNING "%s: problem reading "
  274. "Driver Strength.\n",
  275. mmc_hostname(card->host));
  276. err = 0;
  277. goto out;
  278. }
  279. card->sw_caps.sd3_drv_type = status[9];
  280. /* Find out Current Limits supported by the card */
  281. err = mmc_sd_switch(card, 0, 3, 1, status);
  282. if (err) {
  283. /*
  284. * If the host or the card can't do the switch,
  285. * fail more gracefully.
  286. */
  287. if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
  288. goto out;
  289. printk(KERN_WARNING "%s: problem reading "
  290. "Current Limit.\n",
  291. mmc_hostname(card->host));
  292. err = 0;
  293. goto out;
  294. }
  295. card->sw_caps.sd3_curr_limit = status[7];
  296. } else {
  297. if (status[13] & 0x02)
  298. card->sw_caps.hs_max_dtr = 50000000;
  299. }
  300. out:
  301. kfree(status);
  302. return err;
  303. }
  304. /*
  305. * Test if the card supports high-speed mode and, if so, switch to it.
  306. */
  307. int mmc_sd_switch_hs(struct mmc_card *card)
  308. {
  309. int err;
  310. u8 *status;
  311. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  312. return 0;
  313. if (!(card->csd.cmdclass & CCC_SWITCH))
  314. return 0;
  315. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  316. return 0;
  317. if (card->sw_caps.hs_max_dtr == 0)
  318. return 0;
  319. err = -EIO;
  320. status = kmalloc(64, GFP_KERNEL);
  321. if (!status) {
  322. printk(KERN_ERR "%s: could not allocate a buffer for "
  323. "switch capabilities.\n", mmc_hostname(card->host));
  324. return -ENOMEM;
  325. }
  326. err = mmc_sd_switch(card, 1, 0, 1, status);
  327. if (err)
  328. goto out;
  329. if ((status[16] & 0xF) != 1) {
  330. printk(KERN_WARNING "%s: Problem switching card "
  331. "into high-speed mode!\n",
  332. mmc_hostname(card->host));
  333. err = 0;
  334. } else {
  335. err = 1;
  336. }
  337. out:
  338. kfree(status);
  339. return err;
  340. }
  341. static int sd_select_driver_type(struct mmc_card *card, u8 *status)
  342. {
  343. int host_drv_type = SD_DRIVER_TYPE_B;
  344. int card_drv_type = SD_DRIVER_TYPE_B;
  345. int drive_strength;
  346. int err;
  347. /*
  348. * If the host doesn't support any of the Driver Types A,C or D,
  349. * or there is no board specific handler then default Driver
  350. * Type B is used.
  351. */
  352. if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
  353. | MMC_CAP_DRIVER_TYPE_D)))
  354. return 0;
  355. if (!card->host->ops->select_drive_strength)
  356. return 0;
  357. if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
  358. host_drv_type |= SD_DRIVER_TYPE_A;
  359. if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
  360. host_drv_type |= SD_DRIVER_TYPE_C;
  361. if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
  362. host_drv_type |= SD_DRIVER_TYPE_D;
  363. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
  364. card_drv_type |= SD_DRIVER_TYPE_A;
  365. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
  366. card_drv_type |= SD_DRIVER_TYPE_C;
  367. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
  368. card_drv_type |= SD_DRIVER_TYPE_D;
  369. /*
  370. * The drive strength that the hardware can support
  371. * depends on the board design. Pass the appropriate
  372. * information and let the hardware specific code
  373. * return what is possible given the options
  374. */
  375. drive_strength = card->host->ops->select_drive_strength(
  376. card->sw_caps.uhs_max_dtr,
  377. host_drv_type, card_drv_type);
  378. err = mmc_sd_switch(card, 1, 2, drive_strength, status);
  379. if (err)
  380. return err;
  381. if ((status[15] & 0xF) != drive_strength) {
  382. printk(KERN_WARNING "%s: Problem setting drive strength!\n",
  383. mmc_hostname(card->host));
  384. return 0;
  385. }
  386. mmc_set_driver_type(card->host, drive_strength);
  387. return 0;
  388. }
  389. static void sd_update_bus_speed_mode(struct mmc_card *card)
  390. {
  391. /*
  392. * If the host doesn't support any of the UHS-I modes, fallback on
  393. * default speed.
  394. */
  395. if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  396. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
  397. card->sd_bus_speed = 0;
  398. return;
  399. }
  400. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  401. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  402. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  403. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  404. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  405. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  406. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  407. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  408. SD_MODE_UHS_SDR50)) {
  409. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  410. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  411. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  412. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  413. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  414. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  415. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  416. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  417. SD_MODE_UHS_SDR12)) {
  418. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  419. }
  420. }
  421. static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
  422. {
  423. int err;
  424. unsigned int timing = 0;
  425. switch (card->sd_bus_speed) {
  426. case UHS_SDR104_BUS_SPEED:
  427. timing = MMC_TIMING_UHS_SDR104;
  428. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  429. break;
  430. case UHS_DDR50_BUS_SPEED:
  431. timing = MMC_TIMING_UHS_DDR50;
  432. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  433. break;
  434. case UHS_SDR50_BUS_SPEED:
  435. timing = MMC_TIMING_UHS_SDR50;
  436. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  437. break;
  438. case UHS_SDR25_BUS_SPEED:
  439. timing = MMC_TIMING_UHS_SDR25;
  440. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  441. break;
  442. case UHS_SDR12_BUS_SPEED:
  443. timing = MMC_TIMING_UHS_SDR12;
  444. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  445. break;
  446. default:
  447. return 0;
  448. }
  449. err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
  450. if (err)
  451. return err;
  452. if ((status[16] & 0xF) != card->sd_bus_speed)
  453. printk(KERN_WARNING "%s: Problem setting bus speed mode!\n",
  454. mmc_hostname(card->host));
  455. else {
  456. mmc_set_timing(card->host, timing);
  457. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  458. }
  459. return 0;
  460. }
  461. static int sd_set_current_limit(struct mmc_card *card, u8 *status)
  462. {
  463. int current_limit = 0;
  464. int err;
  465. /*
  466. * Current limit switch is only defined for SDR50, SDR104, and DDR50
  467. * bus speed modes. For other bus speed modes, we set the default
  468. * current limit of 200mA.
  469. */
  470. if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
  471. (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
  472. (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
  473. if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
  474. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
  475. current_limit = SD_SET_CURRENT_LIMIT_800;
  476. else if (card->sw_caps.sd3_curr_limit &
  477. SD_MAX_CURRENT_600)
  478. current_limit = SD_SET_CURRENT_LIMIT_600;
  479. else if (card->sw_caps.sd3_curr_limit &
  480. SD_MAX_CURRENT_400)
  481. current_limit = SD_SET_CURRENT_LIMIT_400;
  482. else if (card->sw_caps.sd3_curr_limit &
  483. SD_MAX_CURRENT_200)
  484. current_limit = SD_SET_CURRENT_LIMIT_200;
  485. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
  486. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
  487. current_limit = SD_SET_CURRENT_LIMIT_600;
  488. else if (card->sw_caps.sd3_curr_limit &
  489. SD_MAX_CURRENT_400)
  490. current_limit = SD_SET_CURRENT_LIMIT_400;
  491. else if (card->sw_caps.sd3_curr_limit &
  492. SD_MAX_CURRENT_200)
  493. current_limit = SD_SET_CURRENT_LIMIT_200;
  494. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
  495. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
  496. current_limit = SD_SET_CURRENT_LIMIT_400;
  497. else if (card->sw_caps.sd3_curr_limit &
  498. SD_MAX_CURRENT_200)
  499. current_limit = SD_SET_CURRENT_LIMIT_200;
  500. } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
  501. if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
  502. current_limit = SD_SET_CURRENT_LIMIT_200;
  503. }
  504. } else
  505. current_limit = SD_SET_CURRENT_LIMIT_200;
  506. err = mmc_sd_switch(card, 1, 3, current_limit, status);
  507. if (err)
  508. return err;
  509. if (((status[15] >> 4) & 0x0F) != current_limit)
  510. printk(KERN_WARNING "%s: Problem setting current limit!\n",
  511. mmc_hostname(card->host));
  512. return 0;
  513. }
  514. /*
  515. * UHS-I specific initialization procedure
  516. */
  517. static int mmc_sd_init_uhs_card(struct mmc_card *card)
  518. {
  519. int err;
  520. u8 *status;
  521. if (!card->scr.sda_spec3)
  522. return 0;
  523. if (!(card->csd.cmdclass & CCC_SWITCH))
  524. return 0;
  525. status = kmalloc(64, GFP_KERNEL);
  526. if (!status) {
  527. printk(KERN_ERR "%s: could not allocate a buffer for "
  528. "switch capabilities.\n", mmc_hostname(card->host));
  529. return -ENOMEM;
  530. }
  531. /* Set 4-bit bus width */
  532. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  533. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  534. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  535. if (err)
  536. goto out;
  537. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  538. }
  539. /*
  540. * Select the bus speed mode depending on host
  541. * and card capability.
  542. */
  543. sd_update_bus_speed_mode(card);
  544. /* Set the driver strength for the card */
  545. err = sd_select_driver_type(card, status);
  546. if (err)
  547. goto out;
  548. /* Set current limit for the card */
  549. err = sd_set_current_limit(card, status);
  550. if (err)
  551. goto out;
  552. /* Set bus speed mode of the card */
  553. err = sd_set_bus_speed_mode(card, status);
  554. if (err)
  555. goto out;
  556. /* SPI mode doesn't define CMD19 */
  557. if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
  558. err = card->host->ops->execute_tuning(card->host);
  559. out:
  560. kfree(status);
  561. return err;
  562. }
  563. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  564. card->raw_cid[2], card->raw_cid[3]);
  565. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  566. card->raw_csd[2], card->raw_csd[3]);
  567. MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  568. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  569. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  570. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  571. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  572. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  573. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  574. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  575. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  576. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  577. static struct attribute *sd_std_attrs[] = {
  578. &dev_attr_cid.attr,
  579. &dev_attr_csd.attr,
  580. &dev_attr_scr.attr,
  581. &dev_attr_date.attr,
  582. &dev_attr_erase_size.attr,
  583. &dev_attr_preferred_erase_size.attr,
  584. &dev_attr_fwrev.attr,
  585. &dev_attr_hwrev.attr,
  586. &dev_attr_manfid.attr,
  587. &dev_attr_name.attr,
  588. &dev_attr_oemid.attr,
  589. &dev_attr_serial.attr,
  590. NULL,
  591. };
  592. static struct attribute_group sd_std_attr_group = {
  593. .attrs = sd_std_attrs,
  594. };
  595. static const struct attribute_group *sd_attr_groups[] = {
  596. &sd_std_attr_group,
  597. NULL,
  598. };
  599. struct device_type sd_type = {
  600. .groups = sd_attr_groups,
  601. };
  602. /*
  603. * Fetch CID from card.
  604. */
  605. int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
  606. {
  607. int err;
  608. /*
  609. * Since we're changing the OCR value, we seem to
  610. * need to tell some cards to go back to the idle
  611. * state. We wait 1ms to give cards time to
  612. * respond.
  613. */
  614. mmc_go_idle(host);
  615. /*
  616. * If SD_SEND_IF_COND indicates an SD 2.0
  617. * compliant card and we should set bit 30
  618. * of the ocr to indicate that we can handle
  619. * block-addressed SDHC cards.
  620. */
  621. err = mmc_send_if_cond(host, ocr);
  622. if (!err)
  623. ocr |= SD_OCR_CCS;
  624. /*
  625. * If the host supports one of UHS-I modes, request the card
  626. * to switch to 1.8V signaling level.
  627. */
  628. if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  629. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
  630. ocr |= SD_OCR_S18R;
  631. /* If the host can supply more than 150mA, XPC should be set to 1. */
  632. if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
  633. MMC_CAP_SET_XPC_180))
  634. ocr |= SD_OCR_XPC;
  635. try_again:
  636. err = mmc_send_app_op_cond(host, ocr, rocr);
  637. if (err)
  638. return err;
  639. /*
  640. * In case CCS and S18A in the response is set, start Signal Voltage
  641. * Switch procedure. SPI mode doesn't support CMD11.
  642. */
  643. if (!mmc_host_is_spi(host) && rocr &&
  644. ((*rocr & 0x41000000) == 0x41000000)) {
  645. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
  646. if (err) {
  647. ocr &= ~SD_OCR_S18R;
  648. goto try_again;
  649. }
  650. }
  651. if (mmc_host_is_spi(host))
  652. err = mmc_send_cid(host, cid);
  653. else
  654. err = mmc_all_send_cid(host, cid);
  655. return err;
  656. }
  657. int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
  658. {
  659. int err;
  660. /*
  661. * Fetch CSD from card.
  662. */
  663. err = mmc_send_csd(card, card->raw_csd);
  664. if (err)
  665. return err;
  666. err = mmc_decode_csd(card);
  667. if (err)
  668. return err;
  669. return 0;
  670. }
  671. int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
  672. bool reinit)
  673. {
  674. int err;
  675. if (!reinit) {
  676. /*
  677. * Fetch SCR from card.
  678. */
  679. err = mmc_app_send_scr(card, card->raw_scr);
  680. if (err)
  681. return err;
  682. err = mmc_decode_scr(card);
  683. if (err)
  684. return err;
  685. /*
  686. * Fetch and process SD Status register.
  687. */
  688. err = mmc_read_ssr(card);
  689. if (err)
  690. return err;
  691. /* Erase init depends on CSD and SSR */
  692. mmc_init_erase(card);
  693. /*
  694. * Fetch switch information from card.
  695. */
  696. err = mmc_read_switch(card);
  697. if (err)
  698. return err;
  699. }
  700. /*
  701. * For SPI, enable CRC as appropriate.
  702. * This CRC enable is located AFTER the reading of the
  703. * card registers because some SDHC cards are not able
  704. * to provide valid CRCs for non-512-byte blocks.
  705. */
  706. if (mmc_host_is_spi(host)) {
  707. err = mmc_spi_set_crc(host, use_spi_crc);
  708. if (err)
  709. return err;
  710. }
  711. /*
  712. * Check if read-only switch is active.
  713. */
  714. if (!reinit) {
  715. int ro = -1;
  716. if (host->ops->get_ro)
  717. ro = host->ops->get_ro(host);
  718. if (ro < 0) {
  719. printk(KERN_WARNING "%s: host does not "
  720. "support reading read-only "
  721. "switch. assuming write-enable.\n",
  722. mmc_hostname(host));
  723. } else if (ro > 0) {
  724. mmc_card_set_readonly(card);
  725. }
  726. }
  727. return 0;
  728. }
  729. unsigned mmc_sd_get_max_clock(struct mmc_card *card)
  730. {
  731. unsigned max_dtr = (unsigned int)-1;
  732. if (mmc_card_highspeed(card)) {
  733. if (max_dtr > card->sw_caps.hs_max_dtr)
  734. max_dtr = card->sw_caps.hs_max_dtr;
  735. } else if (max_dtr > card->csd.max_dtr) {
  736. max_dtr = card->csd.max_dtr;
  737. }
  738. return max_dtr;
  739. }
  740. void mmc_sd_go_highspeed(struct mmc_card *card)
  741. {
  742. mmc_card_set_highspeed(card);
  743. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  744. }
  745. /*
  746. * Handle the detection and initialisation of a card.
  747. *
  748. * In the case of a resume, "oldcard" will contain the card
  749. * we're trying to reinitialise.
  750. */
  751. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  752. struct mmc_card *oldcard)
  753. {
  754. struct mmc_card *card;
  755. int err;
  756. u32 cid[4];
  757. u32 rocr = 0;
  758. BUG_ON(!host);
  759. WARN_ON(!host->claimed);
  760. err = mmc_sd_get_cid(host, ocr, cid, &rocr);
  761. if (err)
  762. return err;
  763. if (oldcard) {
  764. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
  765. return -ENOENT;
  766. card = oldcard;
  767. } else {
  768. /*
  769. * Allocate card structure.
  770. */
  771. card = mmc_alloc_card(host, &sd_type);
  772. if (IS_ERR(card))
  773. return PTR_ERR(card);
  774. card->type = MMC_TYPE_SD;
  775. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  776. }
  777. /*
  778. * For native busses: get card RCA and quit open drain mode.
  779. */
  780. if (!mmc_host_is_spi(host)) {
  781. err = mmc_send_relative_addr(host, &card->rca);
  782. if (err)
  783. return err;
  784. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  785. }
  786. if (!oldcard) {
  787. err = mmc_sd_get_csd(host, card);
  788. if (err)
  789. return err;
  790. mmc_decode_cid(card);
  791. }
  792. /*
  793. * Select card, as all following commands rely on that.
  794. */
  795. if (!mmc_host_is_spi(host)) {
  796. err = mmc_select_card(card);
  797. if (err)
  798. return err;
  799. }
  800. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  801. if (err)
  802. goto free_card;
  803. /* Initialization sequence for UHS-I cards */
  804. if (rocr & SD_ROCR_S18A) {
  805. err = mmc_sd_init_uhs_card(card);
  806. if (err)
  807. goto free_card;
  808. /* Card is an ultra-high-speed card */
  809. mmc_sd_card_set_uhs(card);
  810. /*
  811. * Since initialization is now complete, enable preset
  812. * value registers for UHS-I cards.
  813. */
  814. if (host->ops->enable_preset_value)
  815. host->ops->enable_preset_value(host, true);
  816. } else {
  817. /*
  818. * Attempt to change to high-speed (if supported)
  819. */
  820. err = mmc_sd_switch_hs(card);
  821. if (err > 0)
  822. mmc_sd_go_highspeed(card);
  823. else if (err)
  824. goto free_card;
  825. /*
  826. * Set bus speed.
  827. */
  828. mmc_set_clock(host, mmc_sd_get_max_clock(card));
  829. /*
  830. * Switch to wider bus (if supported).
  831. */
  832. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  833. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  834. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  835. if (err)
  836. goto free_card;
  837. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  838. }
  839. }
  840. host->card = card;
  841. return 0;
  842. free_card:
  843. if (!oldcard)
  844. mmc_remove_card(card);
  845. return err;
  846. }
  847. /*
  848. * Host is being removed. Free up the current card.
  849. */
  850. static void mmc_sd_remove(struct mmc_host *host)
  851. {
  852. BUG_ON(!host);
  853. BUG_ON(!host->card);
  854. mmc_remove_card(host->card);
  855. host->card = NULL;
  856. }
  857. /*
  858. * Card detection callback from host.
  859. */
  860. static void mmc_sd_detect(struct mmc_host *host)
  861. {
  862. int err;
  863. BUG_ON(!host);
  864. BUG_ON(!host->card);
  865. mmc_claim_host(host);
  866. /*
  867. * Just check if our card has been removed.
  868. */
  869. err = mmc_send_status(host->card, NULL);
  870. mmc_release_host(host);
  871. if (err) {
  872. mmc_sd_remove(host);
  873. mmc_claim_host(host);
  874. mmc_detach_bus(host);
  875. mmc_release_host(host);
  876. }
  877. }
  878. /*
  879. * Suspend callback from host.
  880. */
  881. static int mmc_sd_suspend(struct mmc_host *host)
  882. {
  883. BUG_ON(!host);
  884. BUG_ON(!host->card);
  885. mmc_claim_host(host);
  886. if (!mmc_host_is_spi(host))
  887. mmc_deselect_cards(host);
  888. host->card->state &= ~MMC_STATE_HIGHSPEED;
  889. mmc_release_host(host);
  890. return 0;
  891. }
  892. /*
  893. * Resume callback from host.
  894. *
  895. * This function tries to determine if the same card is still present
  896. * and, if so, restore all state to it.
  897. */
  898. static int mmc_sd_resume(struct mmc_host *host)
  899. {
  900. int err;
  901. BUG_ON(!host);
  902. BUG_ON(!host->card);
  903. mmc_claim_host(host);
  904. err = mmc_sd_init_card(host, host->ocr, host->card);
  905. mmc_release_host(host);
  906. return err;
  907. }
  908. static int mmc_sd_power_restore(struct mmc_host *host)
  909. {
  910. int ret;
  911. host->card->state &= ~MMC_STATE_HIGHSPEED;
  912. mmc_claim_host(host);
  913. ret = mmc_sd_init_card(host, host->ocr, host->card);
  914. mmc_release_host(host);
  915. return ret;
  916. }
  917. static const struct mmc_bus_ops mmc_sd_ops = {
  918. .remove = mmc_sd_remove,
  919. .detect = mmc_sd_detect,
  920. .suspend = NULL,
  921. .resume = NULL,
  922. .power_restore = mmc_sd_power_restore,
  923. };
  924. static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
  925. .remove = mmc_sd_remove,
  926. .detect = mmc_sd_detect,
  927. .suspend = mmc_sd_suspend,
  928. .resume = mmc_sd_resume,
  929. .power_restore = mmc_sd_power_restore,
  930. };
  931. static void mmc_sd_attach_bus_ops(struct mmc_host *host)
  932. {
  933. const struct mmc_bus_ops *bus_ops;
  934. if (!mmc_card_is_removable(host))
  935. bus_ops = &mmc_sd_ops_unsafe;
  936. else
  937. bus_ops = &mmc_sd_ops;
  938. mmc_attach_bus(host, bus_ops);
  939. }
  940. /*
  941. * Starting point for SD card init.
  942. */
  943. int mmc_attach_sd(struct mmc_host *host)
  944. {
  945. int err;
  946. u32 ocr;
  947. BUG_ON(!host);
  948. WARN_ON(!host->claimed);
  949. /* Make sure we are at 3.3V signalling voltage */
  950. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false);
  951. if (err)
  952. return err;
  953. /* Disable preset value enable if already set since last time */
  954. if (host->ops->enable_preset_value)
  955. host->ops->enable_preset_value(host, false);
  956. err = mmc_send_app_op_cond(host, 0, &ocr);
  957. if (err)
  958. return err;
  959. mmc_sd_attach_bus_ops(host);
  960. if (host->ocr_avail_sd)
  961. host->ocr_avail = host->ocr_avail_sd;
  962. /*
  963. * We need to get OCR a different way for SPI.
  964. */
  965. if (mmc_host_is_spi(host)) {
  966. mmc_go_idle(host);
  967. err = mmc_spi_read_ocr(host, 0, &ocr);
  968. if (err)
  969. goto err;
  970. }
  971. /*
  972. * Sanity check the voltages that the card claims to
  973. * support.
  974. */
  975. if (ocr & 0x7F) {
  976. printk(KERN_WARNING "%s: card claims to support voltages "
  977. "below the defined range. These will be ignored.\n",
  978. mmc_hostname(host));
  979. ocr &= ~0x7F;
  980. }
  981. if ((ocr & MMC_VDD_165_195) &&
  982. !(host->ocr_avail_sd & MMC_VDD_165_195)) {
  983. printk(KERN_WARNING "%s: SD card claims to support the "
  984. "incompletely defined 'low voltage range'. This "
  985. "will be ignored.\n", mmc_hostname(host));
  986. ocr &= ~MMC_VDD_165_195;
  987. }
  988. host->ocr = mmc_select_voltage(host, ocr);
  989. /*
  990. * Can we support the voltage(s) of the card(s)?
  991. */
  992. if (!host->ocr) {
  993. err = -EINVAL;
  994. goto err;
  995. }
  996. /*
  997. * Detect and init the card.
  998. */
  999. err = mmc_sd_init_card(host, host->ocr, NULL);
  1000. if (err)
  1001. goto err;
  1002. mmc_release_host(host);
  1003. err = mmc_add_card(host->card);
  1004. mmc_claim_host(host);
  1005. if (err)
  1006. goto remove_card;
  1007. return 0;
  1008. remove_card:
  1009. mmc_release_host(host);
  1010. mmc_remove_card(host->card);
  1011. host->card = NULL;
  1012. mmc_claim_host(host);
  1013. err:
  1014. mmc_detach_bus(host);
  1015. printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
  1016. mmc_hostname(host), err);
  1017. return err;
  1018. }