sd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. int err, i;
  194. u32 *ssr;
  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. ssr = kmalloc(64, GFP_KERNEL);
  201. if (!ssr)
  202. return -ENOMEM;
  203. err = mmc_app_sd_status(card, ssr);
  204. if (err) {
  205. pr_warn("%s: problem reading SD Status register\n",
  206. mmc_hostname(card->host));
  207. err = 0;
  208. goto out;
  209. }
  210. for (i = 0; i < 16; i++)
  211. ssr[i] = be32_to_cpu(ssr[i]);
  212. /*
  213. * UNSTUFF_BITS only works with four u32s so we have to offset the
  214. * bitfield positions accordingly.
  215. */
  216. au = UNSTUFF_BITS(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(ssr, 408 - 384, 16);
  221. et = UNSTUFF_BITS(ssr, 402 - 384, 6);
  222. if (es && et) {
  223. eo = UNSTUFF_BITS(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. out:
  233. kfree(ssr);
  234. return err;
  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. pr_err("%s: could not allocate a buffer for "
  254. "switch capabilities.\n",
  255. mmc_hostname(card->host));
  256. return -ENOMEM;
  257. }
  258. /*
  259. * Find out the card's support bits with a mode 0 operation.
  260. * The argument does not matter, as the support bits do not
  261. * change with the arguments.
  262. */
  263. err = mmc_sd_switch(card, 0, 0, 0, status);
  264. if (err) {
  265. /*
  266. * If the host or the card can't do the switch,
  267. * fail more gracefully.
  268. */
  269. if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
  270. goto out;
  271. pr_warn("%s: problem reading Bus Speed modes\n",
  272. mmc_hostname(card->host));
  273. err = 0;
  274. goto out;
  275. }
  276. if (status[13] & SD_MODE_HIGH_SPEED)
  277. card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
  278. if (card->scr.sda_spec3) {
  279. card->sw_caps.sd3_bus_mode = status[13];
  280. /* Driver Strengths supported by the card */
  281. card->sw_caps.sd3_drv_type = status[9];
  282. card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
  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. status = kmalloc(64, GFP_KERNEL);
  304. if (!status) {
  305. pr_err("%s: could not allocate a buffer for "
  306. "switch capabilities.\n", mmc_hostname(card->host));
  307. return -ENOMEM;
  308. }
  309. err = mmc_sd_switch(card, 1, 0, 1, status);
  310. if (err)
  311. goto out;
  312. if ((status[16] & 0xF) != 1) {
  313. pr_warn("%s: Problem switching card into high-speed mode!\n",
  314. mmc_hostname(card->host));
  315. err = 0;
  316. } else {
  317. err = 1;
  318. }
  319. out:
  320. kfree(status);
  321. return err;
  322. }
  323. static int sd_select_driver_type(struct mmc_card *card, u8 *status)
  324. {
  325. int card_drv_type, drive_strength, drv_type;
  326. int err;
  327. card->drive_strength = 0;
  328. card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
  329. drive_strength = mmc_select_drive_strength(card,
  330. card->sw_caps.uhs_max_dtr,
  331. card_drv_type, &drv_type);
  332. if (drive_strength) {
  333. err = mmc_sd_switch(card, 1, 2, drive_strength, status);
  334. if (err)
  335. return err;
  336. if ((status[15] & 0xF) != drive_strength) {
  337. pr_warn("%s: Problem setting drive strength!\n",
  338. mmc_hostname(card->host));
  339. return 0;
  340. }
  341. card->drive_strength = drive_strength;
  342. }
  343. if (drv_type)
  344. mmc_set_driver_type(card->host, drv_type);
  345. return 0;
  346. }
  347. static void sd_update_bus_speed_mode(struct mmc_card *card)
  348. {
  349. /*
  350. * If the host doesn't support any of the UHS-I modes, fallback on
  351. * default speed.
  352. */
  353. if (!mmc_host_uhs(card->host)) {
  354. card->sd_bus_speed = 0;
  355. return;
  356. }
  357. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  358. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  359. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  360. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  361. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  362. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  363. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  364. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  365. SD_MODE_UHS_SDR50)) {
  366. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  367. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  368. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  369. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  370. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  371. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  372. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  373. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  374. SD_MODE_UHS_SDR12)) {
  375. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  376. }
  377. }
  378. static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
  379. {
  380. int err;
  381. unsigned int timing = 0;
  382. switch (card->sd_bus_speed) {
  383. case UHS_SDR104_BUS_SPEED:
  384. timing = MMC_TIMING_UHS_SDR104;
  385. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  386. break;
  387. case UHS_DDR50_BUS_SPEED:
  388. timing = MMC_TIMING_UHS_DDR50;
  389. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  390. break;
  391. case UHS_SDR50_BUS_SPEED:
  392. timing = MMC_TIMING_UHS_SDR50;
  393. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  394. break;
  395. case UHS_SDR25_BUS_SPEED:
  396. timing = MMC_TIMING_UHS_SDR25;
  397. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  398. break;
  399. case UHS_SDR12_BUS_SPEED:
  400. timing = MMC_TIMING_UHS_SDR12;
  401. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  402. break;
  403. default:
  404. return 0;
  405. }
  406. err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
  407. if (err)
  408. return err;
  409. if ((status[16] & 0xF) != card->sd_bus_speed)
  410. pr_warn("%s: Problem setting bus speed mode!\n",
  411. mmc_hostname(card->host));
  412. else {
  413. mmc_set_timing(card->host, timing);
  414. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  415. }
  416. return 0;
  417. }
  418. /* Get host's max current setting at its current voltage */
  419. static u32 sd_get_host_max_current(struct mmc_host *host)
  420. {
  421. u32 voltage, max_current;
  422. voltage = 1 << host->ios.vdd;
  423. switch (voltage) {
  424. case MMC_VDD_165_195:
  425. max_current = host->max_current_180;
  426. break;
  427. case MMC_VDD_29_30:
  428. case MMC_VDD_30_31:
  429. max_current = host->max_current_300;
  430. break;
  431. case MMC_VDD_32_33:
  432. case MMC_VDD_33_34:
  433. max_current = host->max_current_330;
  434. break;
  435. default:
  436. max_current = 0;
  437. }
  438. return max_current;
  439. }
  440. static int sd_set_current_limit(struct mmc_card *card, u8 *status)
  441. {
  442. int current_limit = SD_SET_CURRENT_NO_CHANGE;
  443. int err;
  444. u32 max_current;
  445. /*
  446. * Current limit switch is only defined for SDR50, SDR104, and DDR50
  447. * bus speed modes. For other bus speed modes, we do not change the
  448. * current limit.
  449. */
  450. if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
  451. (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
  452. (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
  453. return 0;
  454. /*
  455. * Host has different current capabilities when operating at
  456. * different voltages, so find out its max current first.
  457. */
  458. max_current = sd_get_host_max_current(card->host);
  459. /*
  460. * We only check host's capability here, if we set a limit that is
  461. * higher than the card's maximum current, the card will be using its
  462. * maximum current, e.g. if the card's maximum current is 300ma, and
  463. * when we set current limit to 200ma, the card will draw 200ma, and
  464. * when we set current limit to 400/600/800ma, the card will draw its
  465. * maximum 300ma from the host.
  466. *
  467. * The above is incorrect: if we try to set a current limit that is
  468. * not supported by the card, the card can rightfully error out the
  469. * attempt, and remain at the default current limit. This results
  470. * in a 300mA card being limited to 200mA even though the host
  471. * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
  472. * an iMX6 host. --rmk
  473. */
  474. if (max_current >= 800 &&
  475. card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
  476. current_limit = SD_SET_CURRENT_LIMIT_800;
  477. else if (max_current >= 600 &&
  478. card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
  479. current_limit = SD_SET_CURRENT_LIMIT_600;
  480. else if (max_current >= 400 &&
  481. card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
  482. current_limit = SD_SET_CURRENT_LIMIT_400;
  483. else if (max_current >= 200 &&
  484. card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
  485. current_limit = SD_SET_CURRENT_LIMIT_200;
  486. if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
  487. err = mmc_sd_switch(card, 1, 3, current_limit, status);
  488. if (err)
  489. return err;
  490. if (((status[15] >> 4) & 0x0F) != current_limit)
  491. pr_warn("%s: Problem setting current limit!\n",
  492. mmc_hostname(card->host));
  493. }
  494. return 0;
  495. }
  496. /*
  497. * UHS-I specific initialization procedure
  498. */
  499. static int mmc_sd_init_uhs_card(struct mmc_card *card)
  500. {
  501. int err;
  502. u8 *status;
  503. if (!card->scr.sda_spec3)
  504. return 0;
  505. if (!(card->csd.cmdclass & CCC_SWITCH))
  506. return 0;
  507. status = kmalloc(64, GFP_KERNEL);
  508. if (!status) {
  509. pr_err("%s: could not allocate a buffer for "
  510. "switch capabilities.\n", mmc_hostname(card->host));
  511. return -ENOMEM;
  512. }
  513. /* Set 4-bit bus width */
  514. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  515. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  516. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  517. if (err)
  518. goto out;
  519. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  520. }
  521. /*
  522. * Select the bus speed mode depending on host
  523. * and card capability.
  524. */
  525. sd_update_bus_speed_mode(card);
  526. /* Set the driver strength for the card */
  527. err = sd_select_driver_type(card, status);
  528. if (err)
  529. goto out;
  530. /* Set current limit for the card */
  531. err = sd_set_current_limit(card, status);
  532. if (err)
  533. goto out;
  534. /* Set bus speed mode of the card */
  535. err = sd_set_bus_speed_mode(card, status);
  536. if (err)
  537. goto out;
  538. /*
  539. * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
  540. * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
  541. */
  542. if (!mmc_host_is_spi(card->host) &&
  543. (card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
  544. card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
  545. card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
  546. err = mmc_execute_tuning(card);
  547. /*
  548. * As SD Specifications Part1 Physical Layer Specification
  549. * Version 3.01 says, CMD19 tuning is available for unlocked
  550. * cards in transfer state of 1.8V signaling mode. The small
  551. * difference between v3.00 and 3.01 spec means that CMD19
  552. * tuning is also available for DDR50 mode.
  553. */
  554. if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
  555. pr_warn("%s: ddr50 tuning failed\n",
  556. mmc_hostname(card->host));
  557. err = 0;
  558. }
  559. }
  560. out:
  561. kfree(status);
  562. return err;
  563. }
  564. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  565. card->raw_cid[2], card->raw_cid[3]);
  566. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  567. card->raw_csd[2], card->raw_csd[3]);
  568. MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  569. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  570. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  571. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  572. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  573. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  574. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  575. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  576. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  577. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  578. MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
  579. static ssize_t mmc_dsr_show(struct device *dev,
  580. struct device_attribute *attr,
  581. char *buf)
  582. {
  583. struct mmc_card *card = mmc_dev_to_card(dev);
  584. struct mmc_host *host = card->host;
  585. if (card->csd.dsr_imp && host->dsr_req)
  586. return sprintf(buf, "0x%x\n", host->dsr);
  587. else
  588. /* return default DSR value */
  589. return sprintf(buf, "0x%x\n", 0x404);
  590. }
  591. static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
  592. static struct attribute *sd_std_attrs[] = {
  593. &dev_attr_cid.attr,
  594. &dev_attr_csd.attr,
  595. &dev_attr_scr.attr,
  596. &dev_attr_date.attr,
  597. &dev_attr_erase_size.attr,
  598. &dev_attr_preferred_erase_size.attr,
  599. &dev_attr_fwrev.attr,
  600. &dev_attr_hwrev.attr,
  601. &dev_attr_manfid.attr,
  602. &dev_attr_name.attr,
  603. &dev_attr_oemid.attr,
  604. &dev_attr_serial.attr,
  605. &dev_attr_ocr.attr,
  606. &dev_attr_dsr.attr,
  607. NULL,
  608. };
  609. ATTRIBUTE_GROUPS(sd_std);
  610. struct device_type sd_type = {
  611. .groups = sd_std_groups,
  612. };
  613. /*
  614. * Fetch CID from card.
  615. */
  616. int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
  617. {
  618. int err;
  619. u32 max_current;
  620. int retries = 10;
  621. u32 pocr = ocr;
  622. try_again:
  623. if (!retries) {
  624. ocr &= ~SD_OCR_S18R;
  625. pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
  626. }
  627. /*
  628. * Since we're changing the OCR value, we seem to
  629. * need to tell some cards to go back to the idle
  630. * state. We wait 1ms to give cards time to
  631. * respond.
  632. */
  633. mmc_go_idle(host);
  634. /*
  635. * If SD_SEND_IF_COND indicates an SD 2.0
  636. * compliant card and we should set bit 30
  637. * of the ocr to indicate that we can handle
  638. * block-addressed SDHC cards.
  639. */
  640. err = mmc_send_if_cond(host, ocr);
  641. if (!err)
  642. ocr |= SD_OCR_CCS;
  643. /*
  644. * If the host supports one of UHS-I modes, request the card
  645. * to switch to 1.8V signaling level. If the card has failed
  646. * repeatedly to switch however, skip this.
  647. */
  648. if (retries && mmc_host_uhs(host))
  649. ocr |= SD_OCR_S18R;
  650. /*
  651. * If the host can supply more than 150mA at current voltage,
  652. * XPC should be set to 1.
  653. */
  654. max_current = sd_get_host_max_current(host);
  655. if (max_current > 150)
  656. ocr |= SD_OCR_XPC;
  657. err = mmc_send_app_op_cond(host, ocr, rocr);
  658. if (err)
  659. return err;
  660. /*
  661. * In case CCS and S18A in the response is set, start Signal Voltage
  662. * Switch procedure. SPI mode doesn't support CMD11.
  663. */
  664. if (!mmc_host_is_spi(host) && rocr &&
  665. ((*rocr & 0x41000000) == 0x41000000)) {
  666. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
  667. pocr);
  668. if (err == -EAGAIN) {
  669. retries--;
  670. goto try_again;
  671. } else if (err) {
  672. retries = 0;
  673. goto try_again;
  674. }
  675. }
  676. if (mmc_host_is_spi(host))
  677. err = mmc_send_cid(host, cid);
  678. else
  679. err = mmc_all_send_cid(host, cid);
  680. return err;
  681. }
  682. int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
  683. {
  684. int err;
  685. /*
  686. * Fetch CSD from card.
  687. */
  688. err = mmc_send_csd(card, card->raw_csd);
  689. if (err)
  690. return err;
  691. err = mmc_decode_csd(card);
  692. if (err)
  693. return err;
  694. return 0;
  695. }
  696. static int mmc_sd_get_ro(struct mmc_host *host)
  697. {
  698. int ro;
  699. /*
  700. * Some systems don't feature a write-protect pin and don't need one.
  701. * E.g. because they only have micro-SD card slot. For those systems
  702. * assume that the SD card is always read-write.
  703. */
  704. if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
  705. return 0;
  706. if (!host->ops->get_ro)
  707. return -1;
  708. ro = host->ops->get_ro(host);
  709. return ro;
  710. }
  711. int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
  712. bool reinit)
  713. {
  714. int err;
  715. if (!reinit) {
  716. /*
  717. * Fetch SCR from card.
  718. */
  719. err = mmc_app_send_scr(card, card->raw_scr);
  720. if (err)
  721. return err;
  722. err = mmc_decode_scr(card);
  723. if (err)
  724. return err;
  725. /*
  726. * Fetch and process SD Status register.
  727. */
  728. err = mmc_read_ssr(card);
  729. if (err)
  730. return err;
  731. /* Erase init depends on CSD and SSR */
  732. mmc_init_erase(card);
  733. /*
  734. * Fetch switch information from card.
  735. */
  736. err = mmc_read_switch(card);
  737. if (err)
  738. return err;
  739. }
  740. /*
  741. * For SPI, enable CRC as appropriate.
  742. * This CRC enable is located AFTER the reading of the
  743. * card registers because some SDHC cards are not able
  744. * to provide valid CRCs for non-512-byte blocks.
  745. */
  746. if (mmc_host_is_spi(host)) {
  747. err = mmc_spi_set_crc(host, use_spi_crc);
  748. if (err)
  749. return err;
  750. }
  751. /*
  752. * Check if read-only switch is active.
  753. */
  754. if (!reinit) {
  755. int ro = mmc_sd_get_ro(host);
  756. if (ro < 0) {
  757. pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
  758. mmc_hostname(host));
  759. } else if (ro > 0) {
  760. mmc_card_set_readonly(card);
  761. }
  762. }
  763. return 0;
  764. }
  765. unsigned mmc_sd_get_max_clock(struct mmc_card *card)
  766. {
  767. unsigned max_dtr = (unsigned int)-1;
  768. if (mmc_card_hs(card)) {
  769. if (max_dtr > card->sw_caps.hs_max_dtr)
  770. max_dtr = card->sw_caps.hs_max_dtr;
  771. } else if (max_dtr > card->csd.max_dtr) {
  772. max_dtr = card->csd.max_dtr;
  773. }
  774. return max_dtr;
  775. }
  776. /*
  777. * Handle the detection and initialisation of a card.
  778. *
  779. * In the case of a resume, "oldcard" will contain the card
  780. * we're trying to reinitialise.
  781. */
  782. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  783. struct mmc_card *oldcard)
  784. {
  785. struct mmc_card *card;
  786. int err;
  787. u32 cid[4];
  788. u32 rocr = 0;
  789. BUG_ON(!host);
  790. WARN_ON(!host->claimed);
  791. err = mmc_sd_get_cid(host, ocr, cid, &rocr);
  792. if (err)
  793. return err;
  794. if (oldcard) {
  795. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
  796. return -ENOENT;
  797. card = oldcard;
  798. } else {
  799. /*
  800. * Allocate card structure.
  801. */
  802. card = mmc_alloc_card(host, &sd_type);
  803. if (IS_ERR(card))
  804. return PTR_ERR(card);
  805. card->ocr = ocr;
  806. card->type = MMC_TYPE_SD;
  807. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  808. }
  809. /*
  810. * Call the optional HC's init_card function to handle quirks.
  811. */
  812. if (host->ops->init_card)
  813. host->ops->init_card(host, card);
  814. /*
  815. * For native busses: get card RCA and quit open drain mode.
  816. */
  817. if (!mmc_host_is_spi(host)) {
  818. err = mmc_send_relative_addr(host, &card->rca);
  819. if (err)
  820. goto free_card;
  821. }
  822. if (!oldcard) {
  823. err = mmc_sd_get_csd(host, card);
  824. if (err)
  825. goto free_card;
  826. mmc_decode_cid(card);
  827. }
  828. /*
  829. * handling only for cards supporting DSR and hosts requesting
  830. * DSR configuration
  831. */
  832. if (card->csd.dsr_imp && host->dsr_req)
  833. mmc_set_dsr(host);
  834. /*
  835. * Select card, as all following commands rely on that.
  836. */
  837. if (!mmc_host_is_spi(host)) {
  838. err = mmc_select_card(card);
  839. if (err)
  840. goto free_card;
  841. }
  842. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  843. if (err)
  844. goto free_card;
  845. /* Initialization sequence for UHS-I cards */
  846. if (rocr & SD_ROCR_S18A) {
  847. err = mmc_sd_init_uhs_card(card);
  848. if (err)
  849. goto free_card;
  850. } else {
  851. /*
  852. * Attempt to change to high-speed (if supported)
  853. */
  854. err = mmc_sd_switch_hs(card);
  855. if (err > 0)
  856. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  857. else if (err)
  858. goto free_card;
  859. /*
  860. * Set bus speed.
  861. */
  862. mmc_set_clock(host, mmc_sd_get_max_clock(card));
  863. /*
  864. * Switch to wider bus (if supported).
  865. */
  866. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  867. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  868. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  869. if (err)
  870. goto free_card;
  871. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  872. }
  873. }
  874. host->card = card;
  875. return 0;
  876. free_card:
  877. if (!oldcard)
  878. mmc_remove_card(card);
  879. return err;
  880. }
  881. /*
  882. * Host is being removed. Free up the current card.
  883. */
  884. static void mmc_sd_remove(struct mmc_host *host)
  885. {
  886. BUG_ON(!host);
  887. BUG_ON(!host->card);
  888. mmc_remove_card(host->card);
  889. host->card = NULL;
  890. }
  891. /*
  892. * Card detection - card is alive.
  893. */
  894. static int mmc_sd_alive(struct mmc_host *host)
  895. {
  896. return mmc_send_status(host->card, NULL);
  897. }
  898. /*
  899. * Card detection callback from host.
  900. */
  901. static void mmc_sd_detect(struct mmc_host *host)
  902. {
  903. int err;
  904. BUG_ON(!host);
  905. BUG_ON(!host->card);
  906. mmc_get_card(host->card);
  907. /*
  908. * Just check if our card has been removed.
  909. */
  910. err = _mmc_detect_card_removed(host);
  911. mmc_put_card(host->card);
  912. if (err) {
  913. mmc_sd_remove(host);
  914. mmc_claim_host(host);
  915. mmc_detach_bus(host);
  916. mmc_power_off(host);
  917. mmc_release_host(host);
  918. }
  919. }
  920. static int _mmc_sd_suspend(struct mmc_host *host)
  921. {
  922. int err = 0;
  923. BUG_ON(!host);
  924. BUG_ON(!host->card);
  925. mmc_claim_host(host);
  926. if (mmc_card_suspended(host->card))
  927. goto out;
  928. if (!mmc_host_is_spi(host))
  929. err = mmc_deselect_cards(host);
  930. if (!err) {
  931. mmc_power_off(host);
  932. mmc_card_set_suspended(host->card);
  933. }
  934. out:
  935. mmc_release_host(host);
  936. return err;
  937. }
  938. /*
  939. * Callback for suspend
  940. */
  941. static int mmc_sd_suspend(struct mmc_host *host)
  942. {
  943. int err;
  944. err = _mmc_sd_suspend(host);
  945. if (!err) {
  946. pm_runtime_disable(&host->card->dev);
  947. pm_runtime_set_suspended(&host->card->dev);
  948. }
  949. return err;
  950. }
  951. /*
  952. * This function tries to determine if the same card is still present
  953. * and, if so, restore all state to it.
  954. */
  955. static int _mmc_sd_resume(struct mmc_host *host)
  956. {
  957. int err = 0;
  958. BUG_ON(!host);
  959. BUG_ON(!host->card);
  960. mmc_claim_host(host);
  961. if (!mmc_card_suspended(host->card))
  962. goto out;
  963. mmc_power_up(host, host->card->ocr);
  964. err = mmc_sd_init_card(host, host->card->ocr, host->card);
  965. mmc_card_clr_suspended(host->card);
  966. out:
  967. mmc_release_host(host);
  968. return err;
  969. }
  970. /*
  971. * Callback for resume
  972. */
  973. static int mmc_sd_resume(struct mmc_host *host)
  974. {
  975. pm_runtime_enable(&host->card->dev);
  976. return 0;
  977. }
  978. /*
  979. * Callback for runtime_suspend.
  980. */
  981. static int mmc_sd_runtime_suspend(struct mmc_host *host)
  982. {
  983. int err;
  984. if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
  985. return 0;
  986. err = _mmc_sd_suspend(host);
  987. if (err)
  988. pr_err("%s: error %d doing aggressive suspend\n",
  989. mmc_hostname(host), err);
  990. return err;
  991. }
  992. /*
  993. * Callback for runtime_resume.
  994. */
  995. static int mmc_sd_runtime_resume(struct mmc_host *host)
  996. {
  997. int err;
  998. err = _mmc_sd_resume(host);
  999. if (err && err != -ENOMEDIUM)
  1000. pr_err("%s: error %d doing runtime resume\n",
  1001. mmc_hostname(host), err);
  1002. return 0;
  1003. }
  1004. static int mmc_sd_reset(struct mmc_host *host)
  1005. {
  1006. mmc_power_cycle(host, host->card->ocr);
  1007. return mmc_sd_init_card(host, host->card->ocr, host->card);
  1008. }
  1009. static const struct mmc_bus_ops mmc_sd_ops = {
  1010. .remove = mmc_sd_remove,
  1011. .detect = mmc_sd_detect,
  1012. .runtime_suspend = mmc_sd_runtime_suspend,
  1013. .runtime_resume = mmc_sd_runtime_resume,
  1014. .suspend = mmc_sd_suspend,
  1015. .resume = mmc_sd_resume,
  1016. .alive = mmc_sd_alive,
  1017. .shutdown = mmc_sd_suspend,
  1018. .reset = mmc_sd_reset,
  1019. };
  1020. /*
  1021. * Starting point for SD card init.
  1022. */
  1023. int mmc_attach_sd(struct mmc_host *host)
  1024. {
  1025. int err;
  1026. u32 ocr, rocr;
  1027. BUG_ON(!host);
  1028. WARN_ON(!host->claimed);
  1029. err = mmc_send_app_op_cond(host, 0, &ocr);
  1030. if (err)
  1031. return err;
  1032. mmc_attach_bus(host, &mmc_sd_ops);
  1033. if (host->ocr_avail_sd)
  1034. host->ocr_avail = host->ocr_avail_sd;
  1035. /*
  1036. * We need to get OCR a different way for SPI.
  1037. */
  1038. if (mmc_host_is_spi(host)) {
  1039. mmc_go_idle(host);
  1040. err = mmc_spi_read_ocr(host, 0, &ocr);
  1041. if (err)
  1042. goto err;
  1043. }
  1044. rocr = mmc_select_voltage(host, ocr);
  1045. /*
  1046. * Can we support the voltage(s) of the card(s)?
  1047. */
  1048. if (!rocr) {
  1049. err = -EINVAL;
  1050. goto err;
  1051. }
  1052. /*
  1053. * Detect and init the card.
  1054. */
  1055. err = mmc_sd_init_card(host, rocr, NULL);
  1056. if (err)
  1057. goto err;
  1058. mmc_release_host(host);
  1059. err = mmc_add_card(host->card);
  1060. if (err)
  1061. goto remove_card;
  1062. mmc_claim_host(host);
  1063. return 0;
  1064. remove_card:
  1065. mmc_remove_card(host->card);
  1066. host->card = NULL;
  1067. mmc_claim_host(host);
  1068. err:
  1069. mmc_detach_bus(host);
  1070. pr_err("%s: error %d whilst initialising SD card\n",
  1071. mmc_hostname(host), err);
  1072. return err;
  1073. }