sd.c 31 KB

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