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