sd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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/slab.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 "core.h"
  19. #include "bus.h"
  20. #include "mmc_ops.h"
  21. #include "sd.h"
  22. #include "sd_ops.h"
  23. static const unsigned int tran_exp[] = {
  24. 10000, 100000, 1000000, 10000000,
  25. 0, 0, 0, 0
  26. };
  27. static const unsigned char tran_mant[] = {
  28. 0, 10, 12, 13, 15, 20, 25, 30,
  29. 35, 40, 45, 50, 55, 60, 70, 80,
  30. };
  31. static const unsigned int tacc_exp[] = {
  32. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  33. };
  34. static const unsigned int tacc_mant[] = {
  35. 0, 10, 12, 13, 15, 20, 25, 30,
  36. 35, 40, 45, 50, 55, 60, 70, 80,
  37. };
  38. #define UNSTUFF_BITS(resp,start,size) \
  39. ({ \
  40. const int __size = size; \
  41. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  42. const int __off = 3 - ((start) / 32); \
  43. const int __shft = (start) & 31; \
  44. u32 __res; \
  45. \
  46. __res = resp[__off] >> __shft; \
  47. if (__size + __shft > 32) \
  48. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  49. __res & __mask; \
  50. })
  51. /*
  52. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  53. */
  54. void mmc_decode_cid(struct mmc_card *card)
  55. {
  56. u32 *resp = card->raw_cid;
  57. memset(&card->cid, 0, sizeof(struct mmc_cid));
  58. /*
  59. * SD doesn't currently have a version field so we will
  60. * have to assume we can parse this.
  61. */
  62. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  63. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  64. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  65. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  66. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  67. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  68. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  69. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  70. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  71. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  72. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  73. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  74. card->cid.year += 2000; /* SD cards year offset */
  75. }
  76. /*
  77. * Given a 128-bit response, decode to our card CSD structure.
  78. */
  79. static int mmc_decode_csd(struct mmc_card *card)
  80. {
  81. struct mmc_csd *csd = &card->csd;
  82. unsigned int e, m, csd_struct;
  83. u32 *resp = card->raw_csd;
  84. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  85. switch (csd_struct) {
  86. case 0:
  87. m = UNSTUFF_BITS(resp, 115, 4);
  88. e = UNSTUFF_BITS(resp, 112, 3);
  89. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  90. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  91. m = UNSTUFF_BITS(resp, 99, 4);
  92. e = UNSTUFF_BITS(resp, 96, 3);
  93. csd->max_dtr = tran_exp[e] * tran_mant[m];
  94. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  95. e = UNSTUFF_BITS(resp, 47, 3);
  96. m = UNSTUFF_BITS(resp, 62, 12);
  97. csd->capacity = (1 + m) << (e + 2);
  98. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  99. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  100. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  101. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  102. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  103. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  104. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  105. if (UNSTUFF_BITS(resp, 46, 1)) {
  106. csd->erase_size = 1;
  107. } else if (csd->write_blkbits >= 9) {
  108. csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
  109. csd->erase_size <<= csd->write_blkbits - 9;
  110. }
  111. break;
  112. case 1:
  113. /*
  114. * This is a block-addressed SDHC card. Most
  115. * interesting fields are unused and have fixed
  116. * values. To avoid getting tripped by buggy cards,
  117. * we assume those fixed values ourselves.
  118. */
  119. mmc_card_set_blockaddr(card);
  120. csd->tacc_ns = 0; /* Unused */
  121. csd->tacc_clks = 0; /* Unused */
  122. m = UNSTUFF_BITS(resp, 99, 4);
  123. e = UNSTUFF_BITS(resp, 96, 3);
  124. csd->max_dtr = tran_exp[e] * tran_mant[m];
  125. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  126. m = UNSTUFF_BITS(resp, 48, 22);
  127. csd->capacity = (1 + m) << 10;
  128. csd->read_blkbits = 9;
  129. csd->read_partial = 0;
  130. csd->write_misalign = 0;
  131. csd->read_misalign = 0;
  132. csd->r2w_factor = 4; /* Unused */
  133. csd->write_blkbits = 9;
  134. csd->write_partial = 0;
  135. csd->erase_size = 1;
  136. break;
  137. default:
  138. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  139. mmc_hostname(card->host), csd_struct);
  140. return -EINVAL;
  141. }
  142. card->erase_size = csd->erase_size;
  143. return 0;
  144. }
  145. /*
  146. * Given a 64-bit response, decode to our card SCR structure.
  147. */
  148. static int mmc_decode_scr(struct mmc_card *card)
  149. {
  150. struct sd_scr *scr = &card->scr;
  151. unsigned int scr_struct;
  152. u32 resp[4];
  153. resp[3] = card->raw_scr[1];
  154. resp[2] = card->raw_scr[0];
  155. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  156. if (scr_struct != 0) {
  157. printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
  158. mmc_hostname(card->host), scr_struct);
  159. return -EINVAL;
  160. }
  161. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  162. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  163. if (UNSTUFF_BITS(resp, 55, 1))
  164. card->erased_byte = 0xFF;
  165. else
  166. card->erased_byte = 0x0;
  167. return 0;
  168. }
  169. /*
  170. * Fetch and process SD Status register.
  171. */
  172. static int mmc_read_ssr(struct mmc_card *card)
  173. {
  174. unsigned int au, es, et, eo;
  175. int err, i;
  176. u32 *ssr;
  177. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  178. printk(KERN_WARNING "%s: card lacks mandatory SD Status "
  179. "function.\n", mmc_hostname(card->host));
  180. return 0;
  181. }
  182. ssr = kmalloc(64, GFP_KERNEL);
  183. if (!ssr)
  184. return -ENOMEM;
  185. err = mmc_app_sd_status(card, ssr);
  186. if (err) {
  187. printk(KERN_WARNING "%s: problem reading SD Status "
  188. "register.\n", mmc_hostname(card->host));
  189. err = 0;
  190. goto out;
  191. }
  192. for (i = 0; i < 16; i++)
  193. ssr[i] = be32_to_cpu(ssr[i]);
  194. /*
  195. * UNSTUFF_BITS only works with four u32s so we have to offset the
  196. * bitfield positions accordingly.
  197. */
  198. au = UNSTUFF_BITS(ssr, 428 - 384, 4);
  199. if (au > 0 || au <= 9) {
  200. card->ssr.au = 1 << (au + 4);
  201. es = UNSTUFF_BITS(ssr, 408 - 384, 16);
  202. et = UNSTUFF_BITS(ssr, 402 - 384, 6);
  203. eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
  204. if (es && et) {
  205. card->ssr.erase_timeout = (et * 1000) / es;
  206. card->ssr.erase_offset = eo * 1000;
  207. }
  208. } else {
  209. printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
  210. "size.\n", mmc_hostname(card->host));
  211. }
  212. out:
  213. kfree(ssr);
  214. return err;
  215. }
  216. /*
  217. * Fetches and decodes switch information
  218. */
  219. static int mmc_read_switch(struct mmc_card *card)
  220. {
  221. int err;
  222. u8 *status;
  223. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  224. return 0;
  225. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  226. printk(KERN_WARNING "%s: card lacks mandatory switch "
  227. "function, performance might suffer.\n",
  228. mmc_hostname(card->host));
  229. return 0;
  230. }
  231. err = -EIO;
  232. status = kmalloc(64, GFP_KERNEL);
  233. if (!status) {
  234. printk(KERN_ERR "%s: could not allocate a buffer for "
  235. "switch capabilities.\n", mmc_hostname(card->host));
  236. return -ENOMEM;
  237. }
  238. err = mmc_sd_switch(card, 0, 0, 1, status);
  239. if (err) {
  240. /* If the host or the card can't do the switch,
  241. * fail more gracefully. */
  242. if ((err != -EINVAL)
  243. && (err != -ENOSYS)
  244. && (err != -EFAULT))
  245. goto out;
  246. printk(KERN_WARNING "%s: problem reading switch "
  247. "capabilities, performance might suffer.\n",
  248. mmc_hostname(card->host));
  249. err = 0;
  250. goto out;
  251. }
  252. if (status[13] & 0x02)
  253. card->sw_caps.hs_max_dtr = 50000000;
  254. out:
  255. kfree(status);
  256. return err;
  257. }
  258. /*
  259. * Test if the card supports high-speed mode and, if so, switch to it.
  260. */
  261. int mmc_sd_switch_hs(struct mmc_card *card)
  262. {
  263. int err;
  264. u8 *status;
  265. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  266. return 0;
  267. if (!(card->csd.cmdclass & CCC_SWITCH))
  268. return 0;
  269. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  270. return 0;
  271. if (card->sw_caps.hs_max_dtr == 0)
  272. return 0;
  273. err = -EIO;
  274. status = kmalloc(64, GFP_KERNEL);
  275. if (!status) {
  276. printk(KERN_ERR "%s: could not allocate a buffer for "
  277. "switch capabilities.\n", mmc_hostname(card->host));
  278. return -ENOMEM;
  279. }
  280. err = mmc_sd_switch(card, 1, 0, 1, status);
  281. if (err)
  282. goto out;
  283. if ((status[16] & 0xF) != 1) {
  284. printk(KERN_WARNING "%s: Problem switching card "
  285. "into high-speed mode!\n",
  286. mmc_hostname(card->host));
  287. err = 0;
  288. } else {
  289. err = 1;
  290. }
  291. out:
  292. kfree(status);
  293. return err;
  294. }
  295. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  296. card->raw_cid[2], card->raw_cid[3]);
  297. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  298. card->raw_csd[2], card->raw_csd[3]);
  299. MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  300. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  301. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  302. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  303. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  304. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  305. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  306. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  307. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  308. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  309. static struct attribute *sd_std_attrs[] = {
  310. &dev_attr_cid.attr,
  311. &dev_attr_csd.attr,
  312. &dev_attr_scr.attr,
  313. &dev_attr_date.attr,
  314. &dev_attr_erase_size.attr,
  315. &dev_attr_preferred_erase_size.attr,
  316. &dev_attr_fwrev.attr,
  317. &dev_attr_hwrev.attr,
  318. &dev_attr_manfid.attr,
  319. &dev_attr_name.attr,
  320. &dev_attr_oemid.attr,
  321. &dev_attr_serial.attr,
  322. NULL,
  323. };
  324. static struct attribute_group sd_std_attr_group = {
  325. .attrs = sd_std_attrs,
  326. };
  327. static const struct attribute_group *sd_attr_groups[] = {
  328. &sd_std_attr_group,
  329. NULL,
  330. };
  331. struct device_type sd_type = {
  332. .groups = sd_attr_groups,
  333. };
  334. /*
  335. * Fetch CID from card.
  336. */
  337. int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
  338. {
  339. int err;
  340. u32 rocr;
  341. /*
  342. * Since we're changing the OCR value, we seem to
  343. * need to tell some cards to go back to the idle
  344. * state. We wait 1ms to give cards time to
  345. * respond.
  346. */
  347. mmc_go_idle(host);
  348. /*
  349. * If SD_SEND_IF_COND indicates an SD 2.0
  350. * compliant card and we should set bit 30
  351. * of the ocr to indicate that we can handle
  352. * block-addressed SDHC cards.
  353. */
  354. err = mmc_send_if_cond(host, ocr);
  355. if (!err)
  356. ocr |= SD_OCR_CCS;
  357. /*
  358. * If the host supports one of UHS-I modes, request the card
  359. * to switch to 1.8V signaling level.
  360. */
  361. if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  362. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
  363. ocr |= SD_OCR_S18R;
  364. /* If the host can supply more than 150mA, XPC should be set to 1. */
  365. if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
  366. MMC_CAP_SET_XPC_180))
  367. ocr |= SD_OCR_XPC;
  368. try_again:
  369. err = mmc_send_app_op_cond(host, ocr, &rocr);
  370. if (err)
  371. return err;
  372. /*
  373. * In case CCS and S18A in the response is set, start Signal Voltage
  374. * Switch procedure. SPI mode doesn't support CMD11.
  375. */
  376. if (!mmc_host_is_spi(host) && ((rocr & 0x41000000) == 0x41000000)) {
  377. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
  378. if (err) {
  379. ocr &= ~SD_OCR_S18R;
  380. goto try_again;
  381. }
  382. }
  383. if (mmc_host_is_spi(host))
  384. err = mmc_send_cid(host, cid);
  385. else
  386. err = mmc_all_send_cid(host, cid);
  387. return err;
  388. }
  389. int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
  390. {
  391. int err;
  392. /*
  393. * Fetch CSD from card.
  394. */
  395. err = mmc_send_csd(card, card->raw_csd);
  396. if (err)
  397. return err;
  398. err = mmc_decode_csd(card);
  399. if (err)
  400. return err;
  401. return 0;
  402. }
  403. int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
  404. bool reinit)
  405. {
  406. int err;
  407. if (!reinit) {
  408. /*
  409. * Fetch SCR from card.
  410. */
  411. err = mmc_app_send_scr(card, card->raw_scr);
  412. if (err)
  413. return err;
  414. err = mmc_decode_scr(card);
  415. if (err)
  416. return err;
  417. /*
  418. * Fetch and process SD Status register.
  419. */
  420. err = mmc_read_ssr(card);
  421. if (err)
  422. return err;
  423. /* Erase init depends on CSD and SSR */
  424. mmc_init_erase(card);
  425. /*
  426. * Fetch switch information from card.
  427. */
  428. err = mmc_read_switch(card);
  429. if (err)
  430. return err;
  431. }
  432. /*
  433. * For SPI, enable CRC as appropriate.
  434. * This CRC enable is located AFTER the reading of the
  435. * card registers because some SDHC cards are not able
  436. * to provide valid CRCs for non-512-byte blocks.
  437. */
  438. if (mmc_host_is_spi(host)) {
  439. err = mmc_spi_set_crc(host, use_spi_crc);
  440. if (err)
  441. return err;
  442. }
  443. /*
  444. * Check if read-only switch is active.
  445. */
  446. if (!reinit) {
  447. int ro = -1;
  448. if (host->ops->get_ro)
  449. ro = host->ops->get_ro(host);
  450. if (ro < 0) {
  451. printk(KERN_WARNING "%s: host does not "
  452. "support reading read-only "
  453. "switch. assuming write-enable.\n",
  454. mmc_hostname(host));
  455. } else if (ro > 0) {
  456. mmc_card_set_readonly(card);
  457. }
  458. }
  459. return 0;
  460. }
  461. unsigned mmc_sd_get_max_clock(struct mmc_card *card)
  462. {
  463. unsigned max_dtr = (unsigned int)-1;
  464. if (mmc_card_highspeed(card)) {
  465. if (max_dtr > card->sw_caps.hs_max_dtr)
  466. max_dtr = card->sw_caps.hs_max_dtr;
  467. } else if (max_dtr > card->csd.max_dtr) {
  468. max_dtr = card->csd.max_dtr;
  469. }
  470. return max_dtr;
  471. }
  472. void mmc_sd_go_highspeed(struct mmc_card *card)
  473. {
  474. mmc_card_set_highspeed(card);
  475. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  476. }
  477. /*
  478. * Handle the detection and initialisation of a card.
  479. *
  480. * In the case of a resume, "oldcard" will contain the card
  481. * we're trying to reinitialise.
  482. */
  483. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  484. struct mmc_card *oldcard)
  485. {
  486. struct mmc_card *card;
  487. int err;
  488. u32 cid[4];
  489. BUG_ON(!host);
  490. WARN_ON(!host->claimed);
  491. err = mmc_sd_get_cid(host, ocr, cid);
  492. if (err)
  493. return err;
  494. if (oldcard) {
  495. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
  496. return -ENOENT;
  497. card = oldcard;
  498. } else {
  499. /*
  500. * Allocate card structure.
  501. */
  502. card = mmc_alloc_card(host, &sd_type);
  503. if (IS_ERR(card))
  504. return PTR_ERR(card);
  505. card->type = MMC_TYPE_SD;
  506. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  507. }
  508. /*
  509. * For native busses: get card RCA and quit open drain mode.
  510. */
  511. if (!mmc_host_is_spi(host)) {
  512. err = mmc_send_relative_addr(host, &card->rca);
  513. if (err)
  514. return err;
  515. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  516. }
  517. if (!oldcard) {
  518. err = mmc_sd_get_csd(host, card);
  519. if (err)
  520. return err;
  521. mmc_decode_cid(card);
  522. }
  523. /*
  524. * Select card, as all following commands rely on that.
  525. */
  526. if (!mmc_host_is_spi(host)) {
  527. err = mmc_select_card(card);
  528. if (err)
  529. return err;
  530. }
  531. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  532. if (err)
  533. goto free_card;
  534. /*
  535. * Attempt to change to high-speed (if supported)
  536. */
  537. err = mmc_sd_switch_hs(card);
  538. if (err > 0)
  539. mmc_sd_go_highspeed(card);
  540. else if (err)
  541. goto free_card;
  542. /*
  543. * Set bus speed.
  544. */
  545. mmc_set_clock(host, mmc_sd_get_max_clock(card));
  546. /*
  547. * Switch to wider bus (if supported).
  548. */
  549. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  550. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  551. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  552. if (err)
  553. goto free_card;
  554. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  555. }
  556. host->card = card;
  557. return 0;
  558. free_card:
  559. if (!oldcard)
  560. mmc_remove_card(card);
  561. return err;
  562. }
  563. /*
  564. * Host is being removed. Free up the current card.
  565. */
  566. static void mmc_sd_remove(struct mmc_host *host)
  567. {
  568. BUG_ON(!host);
  569. BUG_ON(!host->card);
  570. mmc_remove_card(host->card);
  571. host->card = NULL;
  572. }
  573. /*
  574. * Card detection callback from host.
  575. */
  576. static void mmc_sd_detect(struct mmc_host *host)
  577. {
  578. int err;
  579. BUG_ON(!host);
  580. BUG_ON(!host->card);
  581. mmc_claim_host(host);
  582. /*
  583. * Just check if our card has been removed.
  584. */
  585. err = mmc_send_status(host->card, NULL);
  586. mmc_release_host(host);
  587. if (err) {
  588. mmc_sd_remove(host);
  589. mmc_claim_host(host);
  590. mmc_detach_bus(host);
  591. mmc_release_host(host);
  592. }
  593. }
  594. /*
  595. * Suspend callback from host.
  596. */
  597. static int mmc_sd_suspend(struct mmc_host *host)
  598. {
  599. BUG_ON(!host);
  600. BUG_ON(!host->card);
  601. mmc_claim_host(host);
  602. if (!mmc_host_is_spi(host))
  603. mmc_deselect_cards(host);
  604. host->card->state &= ~MMC_STATE_HIGHSPEED;
  605. mmc_release_host(host);
  606. return 0;
  607. }
  608. /*
  609. * Resume callback from host.
  610. *
  611. * This function tries to determine if the same card is still present
  612. * and, if so, restore all state to it.
  613. */
  614. static int mmc_sd_resume(struct mmc_host *host)
  615. {
  616. int err;
  617. BUG_ON(!host);
  618. BUG_ON(!host->card);
  619. mmc_claim_host(host);
  620. err = mmc_sd_init_card(host, host->ocr, host->card);
  621. mmc_release_host(host);
  622. return err;
  623. }
  624. static int mmc_sd_power_restore(struct mmc_host *host)
  625. {
  626. int ret;
  627. host->card->state &= ~MMC_STATE_HIGHSPEED;
  628. mmc_claim_host(host);
  629. ret = mmc_sd_init_card(host, host->ocr, host->card);
  630. mmc_release_host(host);
  631. return ret;
  632. }
  633. static const struct mmc_bus_ops mmc_sd_ops = {
  634. .remove = mmc_sd_remove,
  635. .detect = mmc_sd_detect,
  636. .suspend = NULL,
  637. .resume = NULL,
  638. .power_restore = mmc_sd_power_restore,
  639. };
  640. static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
  641. .remove = mmc_sd_remove,
  642. .detect = mmc_sd_detect,
  643. .suspend = mmc_sd_suspend,
  644. .resume = mmc_sd_resume,
  645. .power_restore = mmc_sd_power_restore,
  646. };
  647. static void mmc_sd_attach_bus_ops(struct mmc_host *host)
  648. {
  649. const struct mmc_bus_ops *bus_ops;
  650. if (!mmc_card_is_removable(host))
  651. bus_ops = &mmc_sd_ops_unsafe;
  652. else
  653. bus_ops = &mmc_sd_ops;
  654. mmc_attach_bus(host, bus_ops);
  655. }
  656. /*
  657. * Starting point for SD card init.
  658. */
  659. int mmc_attach_sd(struct mmc_host *host)
  660. {
  661. int err;
  662. u32 ocr;
  663. BUG_ON(!host);
  664. WARN_ON(!host->claimed);
  665. /* Make sure we are at 3.3V signalling voltage */
  666. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
  667. if (err)
  668. return err;
  669. err = mmc_send_app_op_cond(host, 0, &ocr);
  670. if (err)
  671. return err;
  672. mmc_sd_attach_bus_ops(host);
  673. if (host->ocr_avail_sd)
  674. host->ocr_avail = host->ocr_avail_sd;
  675. /*
  676. * We need to get OCR a different way for SPI.
  677. */
  678. if (mmc_host_is_spi(host)) {
  679. mmc_go_idle(host);
  680. err = mmc_spi_read_ocr(host, 0, &ocr);
  681. if (err)
  682. goto err;
  683. }
  684. /*
  685. * Sanity check the voltages that the card claims to
  686. * support.
  687. */
  688. if (ocr & 0x7F) {
  689. printk(KERN_WARNING "%s: card claims to support voltages "
  690. "below the defined range. These will be ignored.\n",
  691. mmc_hostname(host));
  692. ocr &= ~0x7F;
  693. }
  694. if ((ocr & MMC_VDD_165_195) &&
  695. !(host->ocr_avail_sd & MMC_VDD_165_195)) {
  696. printk(KERN_WARNING "%s: SD card claims to support the "
  697. "incompletely defined 'low voltage range'. This "
  698. "will be ignored.\n", mmc_hostname(host));
  699. ocr &= ~MMC_VDD_165_195;
  700. }
  701. host->ocr = mmc_select_voltage(host, ocr);
  702. /*
  703. * Can we support the voltage(s) of the card(s)?
  704. */
  705. if (!host->ocr) {
  706. err = -EINVAL;
  707. goto err;
  708. }
  709. /*
  710. * Detect and init the card.
  711. */
  712. err = mmc_sd_init_card(host, host->ocr, NULL);
  713. if (err)
  714. goto err;
  715. mmc_release_host(host);
  716. err = mmc_add_card(host->card);
  717. mmc_claim_host(host);
  718. if (err)
  719. goto remove_card;
  720. return 0;
  721. remove_card:
  722. mmc_release_host(host);
  723. mmc_remove_card(host->card);
  724. host->card = NULL;
  725. mmc_claim_host(host);
  726. err:
  727. mmc_detach_bus(host);
  728. printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
  729. mmc_hostname(host), err);
  730. return err;
  731. }