sd.c 30 KB

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