sd.c 27 KB

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