sd.c 30 KB

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