sd.c 29 KB

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