sd.c 33 KB

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