sd.c 30 KB

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