sd.c 28 KB

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