mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * linux/drivers/mmc/core/mmc.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
  6. * MMCv4 support Copyright (C) 2006 Philip Langdale, 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 "core.h"
  18. #include "bus.h"
  19. #include "mmc_ops.h"
  20. static const unsigned int tran_exp[] = {
  21. 10000, 100000, 1000000, 10000000,
  22. 0, 0, 0, 0
  23. };
  24. static const unsigned char tran_mant[] = {
  25. 0, 10, 12, 13, 15, 20, 25, 30,
  26. 35, 40, 45, 50, 55, 60, 70, 80,
  27. };
  28. static const unsigned int tacc_exp[] = {
  29. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  30. };
  31. static const unsigned int tacc_mant[] = {
  32. 0, 10, 12, 13, 15, 20, 25, 30,
  33. 35, 40, 45, 50, 55, 60, 70, 80,
  34. };
  35. #define UNSTUFF_BITS(resp,start,size) \
  36. ({ \
  37. const int __size = size; \
  38. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  39. const int __off = 3 - ((start) / 32); \
  40. const int __shft = (start) & 31; \
  41. u32 __res; \
  42. \
  43. __res = resp[__off] >> __shft; \
  44. if (__size + __shft > 32) \
  45. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  46. __res & __mask; \
  47. })
  48. /*
  49. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  50. */
  51. static int mmc_decode_cid(struct mmc_card *card)
  52. {
  53. u32 *resp = card->raw_cid;
  54. /*
  55. * The selection of the format here is based upon published
  56. * specs from sandisk and from what people have reported.
  57. */
  58. switch (card->csd.mmca_vsn) {
  59. case 0: /* MMC v1.0 - v1.2 */
  60. case 1: /* MMC v1.4 */
  61. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  62. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  63. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  64. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  65. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  66. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  67. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  68. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  69. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  70. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  71. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  72. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  73. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  74. break;
  75. case 2: /* MMC v2.0 - v2.2 */
  76. case 3: /* MMC v3.1 - v3.3 */
  77. case 4: /* MMC v4 */
  78. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  79. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  80. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  81. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  82. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  83. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  84. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  85. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  86. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  87. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  88. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  89. break;
  90. default:
  91. printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
  92. mmc_hostname(card->host), card->csd.mmca_vsn);
  93. return -EINVAL;
  94. }
  95. return 0;
  96. }
  97. static void mmc_set_erase_size(struct mmc_card *card)
  98. {
  99. if (card->ext_csd.erase_group_def & 1)
  100. card->erase_size = card->ext_csd.hc_erase_size;
  101. else
  102. card->erase_size = card->csd.erase_size;
  103. mmc_init_erase(card);
  104. }
  105. /*
  106. * Given a 128-bit response, decode to our card CSD structure.
  107. */
  108. static int mmc_decode_csd(struct mmc_card *card)
  109. {
  110. struct mmc_csd *csd = &card->csd;
  111. unsigned int e, m, a, b;
  112. u32 *resp = card->raw_csd;
  113. /*
  114. * We only understand CSD structure v1.1 and v1.2.
  115. * v1.2 has extra information in bits 15, 11 and 10.
  116. * We also support eMMC v4.4 & v4.41.
  117. */
  118. csd->structure = UNSTUFF_BITS(resp, 126, 2);
  119. if (csd->structure == 0) {
  120. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  121. mmc_hostname(card->host), csd->structure);
  122. return -EINVAL;
  123. }
  124. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  125. m = UNSTUFF_BITS(resp, 115, 4);
  126. e = UNSTUFF_BITS(resp, 112, 3);
  127. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  128. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  129. m = UNSTUFF_BITS(resp, 99, 4);
  130. e = UNSTUFF_BITS(resp, 96, 3);
  131. csd->max_dtr = tran_exp[e] * tran_mant[m];
  132. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  133. e = UNSTUFF_BITS(resp, 47, 3);
  134. m = UNSTUFF_BITS(resp, 62, 12);
  135. csd->capacity = (1 + m) << (e + 2);
  136. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  137. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  138. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  139. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  140. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  141. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  142. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  143. if (csd->write_blkbits >= 9) {
  144. a = UNSTUFF_BITS(resp, 42, 5);
  145. b = UNSTUFF_BITS(resp, 37, 5);
  146. csd->erase_size = (a + 1) * (b + 1);
  147. csd->erase_size <<= csd->write_blkbits - 9;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Read and decode extended CSD.
  153. */
  154. static int mmc_read_ext_csd(struct mmc_card *card)
  155. {
  156. int err;
  157. u8 *ext_csd;
  158. BUG_ON(!card);
  159. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  160. return 0;
  161. /*
  162. * As the ext_csd is so large and mostly unused, we don't store the
  163. * raw block in mmc_card.
  164. */
  165. ext_csd = kmalloc(512, GFP_KERNEL);
  166. if (!ext_csd) {
  167. printk(KERN_ERR "%s: could not allocate a buffer to "
  168. "receive the ext_csd.\n", mmc_hostname(card->host));
  169. return -ENOMEM;
  170. }
  171. err = mmc_send_ext_csd(card, ext_csd);
  172. if (err) {
  173. /* If the host or the card can't do the switch,
  174. * fail more gracefully. */
  175. if ((err != -EINVAL)
  176. && (err != -ENOSYS)
  177. && (err != -EFAULT))
  178. goto out;
  179. /*
  180. * High capacity cards should have this "magic" size
  181. * stored in their CSD.
  182. */
  183. if (card->csd.capacity == (4096 * 512)) {
  184. printk(KERN_ERR "%s: unable to read EXT_CSD "
  185. "on a possible high capacity card. "
  186. "Card will be ignored.\n",
  187. mmc_hostname(card->host));
  188. } else {
  189. printk(KERN_WARNING "%s: unable to read "
  190. "EXT_CSD, performance might "
  191. "suffer.\n",
  192. mmc_hostname(card->host));
  193. err = 0;
  194. }
  195. goto out;
  196. }
  197. /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
  198. if (card->csd.structure == 3) {
  199. int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE];
  200. if (ext_csd_struct > 2) {
  201. printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
  202. "version %d\n", mmc_hostname(card->host),
  203. ext_csd_struct);
  204. err = -EINVAL;
  205. goto out;
  206. }
  207. }
  208. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  209. if (card->ext_csd.rev > 5) {
  210. printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
  211. mmc_hostname(card->host), card->ext_csd.rev);
  212. err = -EINVAL;
  213. goto out;
  214. }
  215. if (card->ext_csd.rev >= 2) {
  216. card->ext_csd.sectors =
  217. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  218. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  219. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  220. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  221. /* Cards with density > 2GiB are sector addressed */
  222. if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
  223. mmc_card_set_blockaddr(card);
  224. }
  225. switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
  226. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  227. card->ext_csd.hs_max_dtr = 52000000;
  228. break;
  229. case EXT_CSD_CARD_TYPE_26:
  230. card->ext_csd.hs_max_dtr = 26000000;
  231. break;
  232. default:
  233. /* MMC v4 spec says this cannot happen */
  234. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  235. "support any high-speed modes.\n",
  236. mmc_hostname(card->host));
  237. }
  238. if (card->ext_csd.rev >= 3) {
  239. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  240. /* Sleep / awake timeout in 100ns units */
  241. if (sa_shift > 0 && sa_shift <= 0x17)
  242. card->ext_csd.sa_timeout =
  243. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  244. card->ext_csd.erase_group_def =
  245. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  246. card->ext_csd.hc_erase_timeout = 300 *
  247. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  248. card->ext_csd.hc_erase_size =
  249. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  250. }
  251. if (card->ext_csd.rev >= 4) {
  252. card->ext_csd.sec_trim_mult =
  253. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  254. card->ext_csd.sec_erase_mult =
  255. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  256. card->ext_csd.sec_feature_support =
  257. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  258. card->ext_csd.trim_timeout = 300 *
  259. ext_csd[EXT_CSD_TRIM_MULT];
  260. }
  261. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  262. card->erased_byte = 0xFF;
  263. else
  264. card->erased_byte = 0x0;
  265. out:
  266. kfree(ext_csd);
  267. return err;
  268. }
  269. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  270. card->raw_cid[2], card->raw_cid[3]);
  271. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  272. card->raw_csd[2], card->raw_csd[3]);
  273. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  274. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  275. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  276. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  277. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  278. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  279. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  280. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  281. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  282. static struct attribute *mmc_std_attrs[] = {
  283. &dev_attr_cid.attr,
  284. &dev_attr_csd.attr,
  285. &dev_attr_date.attr,
  286. &dev_attr_erase_size.attr,
  287. &dev_attr_preferred_erase_size.attr,
  288. &dev_attr_fwrev.attr,
  289. &dev_attr_hwrev.attr,
  290. &dev_attr_manfid.attr,
  291. &dev_attr_name.attr,
  292. &dev_attr_oemid.attr,
  293. &dev_attr_serial.attr,
  294. NULL,
  295. };
  296. static struct attribute_group mmc_std_attr_group = {
  297. .attrs = mmc_std_attrs,
  298. };
  299. static const struct attribute_group *mmc_attr_groups[] = {
  300. &mmc_std_attr_group,
  301. NULL,
  302. };
  303. static struct device_type mmc_type = {
  304. .groups = mmc_attr_groups,
  305. };
  306. /*
  307. * Handle the detection and initialisation of a card.
  308. *
  309. * In the case of a resume, "oldcard" will contain the card
  310. * we're trying to reinitialise.
  311. */
  312. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  313. struct mmc_card *oldcard)
  314. {
  315. struct mmc_card *card;
  316. int err;
  317. u32 cid[4];
  318. unsigned int max_dtr;
  319. BUG_ON(!host);
  320. WARN_ON(!host->claimed);
  321. /*
  322. * Since we're changing the OCR value, we seem to
  323. * need to tell some cards to go back to the idle
  324. * state. We wait 1ms to give cards time to
  325. * respond.
  326. */
  327. mmc_go_idle(host);
  328. /* The extra bit indicates that we support high capacity */
  329. err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
  330. if (err)
  331. goto err;
  332. /*
  333. * For SPI, enable CRC as appropriate.
  334. */
  335. if (mmc_host_is_spi(host)) {
  336. err = mmc_spi_set_crc(host, use_spi_crc);
  337. if (err)
  338. goto err;
  339. }
  340. /*
  341. * Fetch CID from card.
  342. */
  343. if (mmc_host_is_spi(host))
  344. err = mmc_send_cid(host, cid);
  345. else
  346. err = mmc_all_send_cid(host, cid);
  347. if (err)
  348. goto err;
  349. if (oldcard) {
  350. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  351. err = -ENOENT;
  352. goto err;
  353. }
  354. card = oldcard;
  355. } else {
  356. /*
  357. * Allocate card structure.
  358. */
  359. card = mmc_alloc_card(host, &mmc_type);
  360. if (IS_ERR(card)) {
  361. err = PTR_ERR(card);
  362. goto err;
  363. }
  364. card->type = MMC_TYPE_MMC;
  365. card->rca = 1;
  366. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  367. }
  368. /*
  369. * For native busses: set card RCA and quit open drain mode.
  370. */
  371. if (!mmc_host_is_spi(host)) {
  372. err = mmc_set_relative_addr(card);
  373. if (err)
  374. goto free_card;
  375. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  376. }
  377. if (!oldcard) {
  378. /*
  379. * Fetch CSD from card.
  380. */
  381. err = mmc_send_csd(card, card->raw_csd);
  382. if (err)
  383. goto free_card;
  384. err = mmc_decode_csd(card);
  385. if (err)
  386. goto free_card;
  387. err = mmc_decode_cid(card);
  388. if (err)
  389. goto free_card;
  390. }
  391. /*
  392. * Select card, as all following commands rely on that.
  393. */
  394. if (!mmc_host_is_spi(host)) {
  395. err = mmc_select_card(card);
  396. if (err)
  397. goto free_card;
  398. }
  399. if (!oldcard) {
  400. /*
  401. * Fetch and process extended CSD.
  402. */
  403. err = mmc_read_ext_csd(card);
  404. if (err)
  405. goto free_card;
  406. /* Erase size depends on CSD and Extended CSD */
  407. mmc_set_erase_size(card);
  408. }
  409. /*
  410. * Activate high speed (if supported)
  411. */
  412. if ((card->ext_csd.hs_max_dtr != 0) &&
  413. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  414. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  415. EXT_CSD_HS_TIMING, 1);
  416. if (err && err != -EBADMSG)
  417. goto free_card;
  418. if (err) {
  419. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  420. mmc_hostname(card->host));
  421. err = 0;
  422. } else {
  423. mmc_card_set_highspeed(card);
  424. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  425. }
  426. }
  427. /*
  428. * Compute bus speed.
  429. */
  430. max_dtr = (unsigned int)-1;
  431. if (mmc_card_highspeed(card)) {
  432. if (max_dtr > card->ext_csd.hs_max_dtr)
  433. max_dtr = card->ext_csd.hs_max_dtr;
  434. } else if (max_dtr > card->csd.max_dtr) {
  435. max_dtr = card->csd.max_dtr;
  436. }
  437. mmc_set_clock(host, max_dtr);
  438. /*
  439. * Activate wide bus (if supported).
  440. */
  441. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  442. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  443. unsigned ext_csd_bit, bus_width;
  444. if (host->caps & MMC_CAP_8_BIT_DATA) {
  445. ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
  446. bus_width = MMC_BUS_WIDTH_8;
  447. } else {
  448. ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
  449. bus_width = MMC_BUS_WIDTH_4;
  450. }
  451. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  452. EXT_CSD_BUS_WIDTH, ext_csd_bit);
  453. if (err && err != -EBADMSG)
  454. goto free_card;
  455. if (err) {
  456. printk(KERN_WARNING "%s: switch to bus width %d "
  457. "failed\n", mmc_hostname(card->host),
  458. 1 << bus_width);
  459. err = 0;
  460. } else {
  461. mmc_set_bus_width(card->host, bus_width);
  462. }
  463. }
  464. if (!oldcard)
  465. host->card = card;
  466. return 0;
  467. free_card:
  468. if (!oldcard)
  469. mmc_remove_card(card);
  470. err:
  471. return err;
  472. }
  473. /*
  474. * Host is being removed. Free up the current card.
  475. */
  476. static void mmc_remove(struct mmc_host *host)
  477. {
  478. BUG_ON(!host);
  479. BUG_ON(!host->card);
  480. mmc_remove_card(host->card);
  481. host->card = NULL;
  482. }
  483. /*
  484. * Card detection callback from host.
  485. */
  486. static void mmc_detect(struct mmc_host *host)
  487. {
  488. int err;
  489. BUG_ON(!host);
  490. BUG_ON(!host->card);
  491. mmc_claim_host(host);
  492. /*
  493. * Just check if our card has been removed.
  494. */
  495. err = mmc_send_status(host->card, NULL);
  496. mmc_release_host(host);
  497. if (err) {
  498. mmc_remove(host);
  499. mmc_claim_host(host);
  500. mmc_detach_bus(host);
  501. mmc_release_host(host);
  502. }
  503. }
  504. /*
  505. * Suspend callback from host.
  506. */
  507. static int mmc_suspend(struct mmc_host *host)
  508. {
  509. BUG_ON(!host);
  510. BUG_ON(!host->card);
  511. mmc_claim_host(host);
  512. if (!mmc_host_is_spi(host))
  513. mmc_deselect_cards(host);
  514. host->card->state &= ~MMC_STATE_HIGHSPEED;
  515. mmc_release_host(host);
  516. return 0;
  517. }
  518. /*
  519. * Resume callback from host.
  520. *
  521. * This function tries to determine if the same card is still present
  522. * and, if so, restore all state to it.
  523. */
  524. static int mmc_resume(struct mmc_host *host)
  525. {
  526. int err;
  527. BUG_ON(!host);
  528. BUG_ON(!host->card);
  529. mmc_claim_host(host);
  530. err = mmc_init_card(host, host->ocr, host->card);
  531. mmc_release_host(host);
  532. return err;
  533. }
  534. static void mmc_power_restore(struct mmc_host *host)
  535. {
  536. host->card->state &= ~MMC_STATE_HIGHSPEED;
  537. mmc_claim_host(host);
  538. mmc_init_card(host, host->ocr, host->card);
  539. mmc_release_host(host);
  540. }
  541. static int mmc_sleep(struct mmc_host *host)
  542. {
  543. struct mmc_card *card = host->card;
  544. int err = -ENOSYS;
  545. if (card && card->ext_csd.rev >= 3) {
  546. err = mmc_card_sleepawake(host, 1);
  547. if (err < 0)
  548. pr_debug("%s: Error %d while putting card into sleep",
  549. mmc_hostname(host), err);
  550. }
  551. return err;
  552. }
  553. static int mmc_awake(struct mmc_host *host)
  554. {
  555. struct mmc_card *card = host->card;
  556. int err = -ENOSYS;
  557. if (card && card->ext_csd.rev >= 3) {
  558. err = mmc_card_sleepawake(host, 0);
  559. if (err < 0)
  560. pr_debug("%s: Error %d while awaking sleeping card",
  561. mmc_hostname(host), err);
  562. }
  563. return err;
  564. }
  565. static const struct mmc_bus_ops mmc_ops = {
  566. .awake = mmc_awake,
  567. .sleep = mmc_sleep,
  568. .remove = mmc_remove,
  569. .detect = mmc_detect,
  570. .suspend = NULL,
  571. .resume = NULL,
  572. .power_restore = mmc_power_restore,
  573. };
  574. static const struct mmc_bus_ops mmc_ops_unsafe = {
  575. .awake = mmc_awake,
  576. .sleep = mmc_sleep,
  577. .remove = mmc_remove,
  578. .detect = mmc_detect,
  579. .suspend = mmc_suspend,
  580. .resume = mmc_resume,
  581. .power_restore = mmc_power_restore,
  582. };
  583. static void mmc_attach_bus_ops(struct mmc_host *host)
  584. {
  585. const struct mmc_bus_ops *bus_ops;
  586. if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
  587. bus_ops = &mmc_ops_unsafe;
  588. else
  589. bus_ops = &mmc_ops;
  590. mmc_attach_bus(host, bus_ops);
  591. }
  592. /*
  593. * Starting point for MMC card init.
  594. */
  595. int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
  596. {
  597. int err;
  598. BUG_ON(!host);
  599. WARN_ON(!host->claimed);
  600. mmc_attach_bus_ops(host);
  601. /*
  602. * We need to get OCR a different way for SPI.
  603. */
  604. if (mmc_host_is_spi(host)) {
  605. err = mmc_spi_read_ocr(host, 1, &ocr);
  606. if (err)
  607. goto err;
  608. }
  609. /*
  610. * Sanity check the voltages that the card claims to
  611. * support.
  612. */
  613. if (ocr & 0x7F) {
  614. printk(KERN_WARNING "%s: card claims to support voltages "
  615. "below the defined range. These will be ignored.\n",
  616. mmc_hostname(host));
  617. ocr &= ~0x7F;
  618. }
  619. host->ocr = mmc_select_voltage(host, ocr);
  620. /*
  621. * Can we support the voltage of the card?
  622. */
  623. if (!host->ocr) {
  624. err = -EINVAL;
  625. goto err;
  626. }
  627. /*
  628. * Detect and init the card.
  629. */
  630. err = mmc_init_card(host, host->ocr, NULL);
  631. if (err)
  632. goto err;
  633. mmc_release_host(host);
  634. err = mmc_add_card(host->card);
  635. if (err)
  636. goto remove_card;
  637. return 0;
  638. remove_card:
  639. mmc_remove_card(host->card);
  640. host->card = NULL;
  641. mmc_claim_host(host);
  642. err:
  643. mmc_detach_bus(host);
  644. mmc_release_host(host);
  645. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  646. mmc_hostname(host), err);
  647. return err;
  648. }