sd.c 29 KB

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