sd.c 28 KB

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