mmc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. #include "sd_ops.h"
  21. static const unsigned int tran_exp[] = {
  22. 10000, 100000, 1000000, 10000000,
  23. 0, 0, 0, 0
  24. };
  25. static const unsigned char tran_mant[] = {
  26. 0, 10, 12, 13, 15, 20, 25, 30,
  27. 35, 40, 45, 50, 55, 60, 70, 80,
  28. };
  29. static const unsigned int tacc_exp[] = {
  30. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  31. };
  32. static const unsigned int tacc_mant[] = {
  33. 0, 10, 12, 13, 15, 20, 25, 30,
  34. 35, 40, 45, 50, 55, 60, 70, 80,
  35. };
  36. #define UNSTUFF_BITS(resp,start,size) \
  37. ({ \
  38. const int __size = size; \
  39. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  40. const int __off = 3 - ((start) / 32); \
  41. const int __shft = (start) & 31; \
  42. u32 __res; \
  43. \
  44. __res = resp[__off] >> __shft; \
  45. if (__size + __shft > 32) \
  46. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  47. __res & __mask; \
  48. })
  49. /*
  50. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  51. */
  52. static int mmc_decode_cid(struct mmc_card *card)
  53. {
  54. u32 *resp = card->raw_cid;
  55. /*
  56. * The selection of the format here is based upon published
  57. * specs from sandisk and from what people have reported.
  58. */
  59. switch (card->csd.mmca_vsn) {
  60. case 0: /* MMC v1.0 - v1.2 */
  61. case 1: /* MMC v1.4 */
  62. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  63. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  64. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  65. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  66. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  67. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  68. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  69. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  70. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  71. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  72. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  73. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  74. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  75. break;
  76. case 2: /* MMC v2.0 - v2.2 */
  77. case 3: /* MMC v3.1 - v3.3 */
  78. case 4: /* MMC v4 */
  79. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  80. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  81. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  82. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  83. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  84. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  85. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  86. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  87. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  88. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  89. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  90. break;
  91. default:
  92. printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
  93. mmc_hostname(card->host), card->csd.mmca_vsn);
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static void mmc_set_erase_size(struct mmc_card *card)
  99. {
  100. if (card->ext_csd.erase_group_def & 1)
  101. card->erase_size = card->ext_csd.hc_erase_size;
  102. else
  103. card->erase_size = card->csd.erase_size;
  104. mmc_init_erase(card);
  105. }
  106. /*
  107. * Given a 128-bit response, decode to our card CSD structure.
  108. */
  109. static int mmc_decode_csd(struct mmc_card *card)
  110. {
  111. struct mmc_csd *csd = &card->csd;
  112. unsigned int e, m, a, b;
  113. u32 *resp = card->raw_csd;
  114. /*
  115. * We only understand CSD structure v1.1 and v1.2.
  116. * v1.2 has extra information in bits 15, 11 and 10.
  117. * We also support eMMC v4.4 & v4.41.
  118. */
  119. csd->structure = UNSTUFF_BITS(resp, 126, 2);
  120. if (csd->structure == 0) {
  121. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  122. mmc_hostname(card->host), csd->structure);
  123. return -EINVAL;
  124. }
  125. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  126. m = UNSTUFF_BITS(resp, 115, 4);
  127. e = UNSTUFF_BITS(resp, 112, 3);
  128. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  129. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  130. m = UNSTUFF_BITS(resp, 99, 4);
  131. e = UNSTUFF_BITS(resp, 96, 3);
  132. csd->max_dtr = tran_exp[e] * tran_mant[m];
  133. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  134. e = UNSTUFF_BITS(resp, 47, 3);
  135. m = UNSTUFF_BITS(resp, 62, 12);
  136. csd->capacity = (1 + m) << (e + 2);
  137. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  138. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  139. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  140. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  141. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  142. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  143. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  144. if (csd->write_blkbits >= 9) {
  145. a = UNSTUFF_BITS(resp, 42, 5);
  146. b = UNSTUFF_BITS(resp, 37, 5);
  147. csd->erase_size = (a + 1) * (b + 1);
  148. csd->erase_size <<= csd->write_blkbits - 9;
  149. }
  150. return 0;
  151. }
  152. /*
  153. * Read extended CSD.
  154. */
  155. static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
  156. {
  157. int err;
  158. u8 *ext_csd;
  159. BUG_ON(!card);
  160. BUG_ON(!new_ext_csd);
  161. *new_ext_csd = NULL;
  162. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  163. return 0;
  164. /*
  165. * As the ext_csd is so large and mostly unused, we don't store the
  166. * raw block in mmc_card.
  167. */
  168. ext_csd = kmalloc(512, GFP_KERNEL);
  169. if (!ext_csd) {
  170. printk(KERN_ERR "%s: could not allocate a buffer to "
  171. "receive the ext_csd.\n", mmc_hostname(card->host));
  172. return -ENOMEM;
  173. }
  174. err = mmc_send_ext_csd(card, ext_csd);
  175. if (err) {
  176. kfree(ext_csd);
  177. *new_ext_csd = NULL;
  178. /* If the host or the card can't do the switch,
  179. * fail more gracefully. */
  180. if ((err != -EINVAL)
  181. && (err != -ENOSYS)
  182. && (err != -EFAULT))
  183. return err;
  184. /*
  185. * High capacity cards should have this "magic" size
  186. * stored in their CSD.
  187. */
  188. if (card->csd.capacity == (4096 * 512)) {
  189. printk(KERN_ERR "%s: unable to read EXT_CSD "
  190. "on a possible high capacity card. "
  191. "Card will be ignored.\n",
  192. mmc_hostname(card->host));
  193. } else {
  194. printk(KERN_WARNING "%s: unable to read "
  195. "EXT_CSD, performance might "
  196. "suffer.\n",
  197. mmc_hostname(card->host));
  198. err = 0;
  199. }
  200. } else
  201. *new_ext_csd = ext_csd;
  202. return err;
  203. }
  204. /*
  205. * Decode extended CSD.
  206. */
  207. static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
  208. {
  209. int err = 0, idx;
  210. unsigned int part_size;
  211. u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
  212. BUG_ON(!card);
  213. if (!ext_csd)
  214. return 0;
  215. /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
  216. card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
  217. if (card->csd.structure == 3) {
  218. if (card->ext_csd.raw_ext_csd_structure > 2) {
  219. printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
  220. "version %d\n", mmc_hostname(card->host),
  221. card->ext_csd.raw_ext_csd_structure);
  222. err = -EINVAL;
  223. goto out;
  224. }
  225. }
  226. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  227. if (card->ext_csd.rev > 6) {
  228. printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
  229. mmc_hostname(card->host), card->ext_csd.rev);
  230. err = -EINVAL;
  231. goto out;
  232. }
  233. card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
  234. card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
  235. card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
  236. card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
  237. if (card->ext_csd.rev >= 2) {
  238. card->ext_csd.sectors =
  239. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  240. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  241. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  242. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  243. /* Cards with density > 2GiB are sector addressed */
  244. if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
  245. mmc_card_set_blockaddr(card);
  246. }
  247. card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
  248. switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
  249. case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
  250. EXT_CSD_CARD_TYPE_26:
  251. card->ext_csd.hs_max_dtr = 52000000;
  252. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
  253. break;
  254. case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
  255. EXT_CSD_CARD_TYPE_26:
  256. card->ext_csd.hs_max_dtr = 52000000;
  257. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
  258. break;
  259. case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
  260. EXT_CSD_CARD_TYPE_26:
  261. card->ext_csd.hs_max_dtr = 52000000;
  262. card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
  263. break;
  264. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  265. card->ext_csd.hs_max_dtr = 52000000;
  266. break;
  267. case EXT_CSD_CARD_TYPE_26:
  268. card->ext_csd.hs_max_dtr = 26000000;
  269. break;
  270. default:
  271. /* MMC v4 spec says this cannot happen */
  272. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  273. "support any high-speed modes.\n",
  274. mmc_hostname(card->host));
  275. }
  276. card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
  277. card->ext_csd.raw_erase_timeout_mult =
  278. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  279. card->ext_csd.raw_hc_erase_grp_size =
  280. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  281. if (card->ext_csd.rev >= 3) {
  282. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  283. card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
  284. /* EXT_CSD value is in units of 10ms, but we store in ms */
  285. card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
  286. /* Sleep / awake timeout in 100ns units */
  287. if (sa_shift > 0 && sa_shift <= 0x17)
  288. card->ext_csd.sa_timeout =
  289. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  290. card->ext_csd.erase_group_def =
  291. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  292. card->ext_csd.hc_erase_timeout = 300 *
  293. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  294. card->ext_csd.hc_erase_size =
  295. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  296. card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
  297. /*
  298. * There are two boot regions of equal size, defined in
  299. * multiples of 128K.
  300. */
  301. if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
  302. for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
  303. part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  304. mmc_part_add(card, part_size,
  305. EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
  306. "boot%d", idx, true);
  307. }
  308. }
  309. }
  310. card->ext_csd.raw_hc_erase_gap_size =
  311. ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
  312. card->ext_csd.raw_sec_trim_mult =
  313. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  314. card->ext_csd.raw_sec_erase_mult =
  315. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  316. card->ext_csd.raw_sec_feature_support =
  317. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  318. card->ext_csd.raw_trim_mult =
  319. ext_csd[EXT_CSD_TRIM_MULT];
  320. if (card->ext_csd.rev >= 4) {
  321. /*
  322. * Enhanced area feature support -- check whether the eMMC
  323. * card has the Enhanced area enabled. If so, export enhanced
  324. * area offset and size to user by adding sysfs interface.
  325. */
  326. card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
  327. if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
  328. (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
  329. hc_erase_grp_sz =
  330. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  331. hc_wp_grp_sz =
  332. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  333. card->ext_csd.enhanced_area_en = 1;
  334. /*
  335. * calculate the enhanced data area offset, in bytes
  336. */
  337. card->ext_csd.enhanced_area_offset =
  338. (ext_csd[139] << 24) + (ext_csd[138] << 16) +
  339. (ext_csd[137] << 8) + ext_csd[136];
  340. if (mmc_card_blockaddr(card))
  341. card->ext_csd.enhanced_area_offset <<= 9;
  342. /*
  343. * calculate the enhanced data area size, in kilobytes
  344. */
  345. card->ext_csd.enhanced_area_size =
  346. (ext_csd[142] << 16) + (ext_csd[141] << 8) +
  347. ext_csd[140];
  348. card->ext_csd.enhanced_area_size *=
  349. (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
  350. card->ext_csd.enhanced_area_size <<= 9;
  351. } else {
  352. /*
  353. * If the enhanced area is not enabled, disable these
  354. * device attributes.
  355. */
  356. card->ext_csd.enhanced_area_offset = -EINVAL;
  357. card->ext_csd.enhanced_area_size = -EINVAL;
  358. }
  359. /*
  360. * General purpose partition feature support --
  361. * If ext_csd has the size of general purpose partitions,
  362. * set size, part_cfg, partition name in mmc_part.
  363. */
  364. if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
  365. EXT_CSD_PART_SUPPORT_PART_EN) {
  366. if (card->ext_csd.enhanced_area_en != 1) {
  367. hc_erase_grp_sz =
  368. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  369. hc_wp_grp_sz =
  370. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  371. card->ext_csd.enhanced_area_en = 1;
  372. }
  373. for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
  374. if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
  375. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
  376. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
  377. continue;
  378. part_size =
  379. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
  380. << 16) +
  381. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
  382. << 8) +
  383. ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
  384. part_size *= (size_t)(hc_erase_grp_sz *
  385. hc_wp_grp_sz);
  386. mmc_part_add(card, part_size << 19,
  387. EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
  388. "gp%d", idx, false);
  389. }
  390. }
  391. card->ext_csd.sec_trim_mult =
  392. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  393. card->ext_csd.sec_erase_mult =
  394. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  395. card->ext_csd.sec_feature_support =
  396. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  397. card->ext_csd.trim_timeout = 300 *
  398. ext_csd[EXT_CSD_TRIM_MULT];
  399. }
  400. if (card->ext_csd.rev >= 5) {
  401. card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
  402. card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
  403. }
  404. card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
  405. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  406. card->erased_byte = 0xFF;
  407. else
  408. card->erased_byte = 0x0;
  409. out:
  410. return err;
  411. }
  412. static inline void mmc_free_ext_csd(u8 *ext_csd)
  413. {
  414. kfree(ext_csd);
  415. }
  416. static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
  417. {
  418. u8 *bw_ext_csd;
  419. int err;
  420. if (bus_width == MMC_BUS_WIDTH_1)
  421. return 0;
  422. err = mmc_get_ext_csd(card, &bw_ext_csd);
  423. if (err || bw_ext_csd == NULL) {
  424. if (bus_width != MMC_BUS_WIDTH_1)
  425. err = -EINVAL;
  426. goto out;
  427. }
  428. if (bus_width == MMC_BUS_WIDTH_1)
  429. goto out;
  430. /* only compare read only fields */
  431. err = (!(card->ext_csd.raw_partition_support ==
  432. bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  433. (card->ext_csd.raw_erased_mem_count ==
  434. bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  435. (card->ext_csd.rev ==
  436. bw_ext_csd[EXT_CSD_REV]) &&
  437. (card->ext_csd.raw_ext_csd_structure ==
  438. bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  439. (card->ext_csd.raw_card_type ==
  440. bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  441. (card->ext_csd.raw_s_a_timeout ==
  442. bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  443. (card->ext_csd.raw_hc_erase_gap_size ==
  444. bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  445. (card->ext_csd.raw_erase_timeout_mult ==
  446. bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  447. (card->ext_csd.raw_hc_erase_grp_size ==
  448. bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  449. (card->ext_csd.raw_sec_trim_mult ==
  450. bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  451. (card->ext_csd.raw_sec_erase_mult ==
  452. bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  453. (card->ext_csd.raw_sec_feature_support ==
  454. bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  455. (card->ext_csd.raw_trim_mult ==
  456. bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  457. (card->ext_csd.raw_sectors[0] ==
  458. bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  459. (card->ext_csd.raw_sectors[1] ==
  460. bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  461. (card->ext_csd.raw_sectors[2] ==
  462. bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  463. (card->ext_csd.raw_sectors[3] ==
  464. bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
  465. if (err)
  466. err = -EINVAL;
  467. out:
  468. mmc_free_ext_csd(bw_ext_csd);
  469. return err;
  470. }
  471. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  472. card->raw_cid[2], card->raw_cid[3]);
  473. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  474. card->raw_csd[2], card->raw_csd[3]);
  475. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  476. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  477. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  478. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  479. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  480. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  481. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  482. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  483. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  484. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  485. card->ext_csd.enhanced_area_offset);
  486. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  487. static struct attribute *mmc_std_attrs[] = {
  488. &dev_attr_cid.attr,
  489. &dev_attr_csd.attr,
  490. &dev_attr_date.attr,
  491. &dev_attr_erase_size.attr,
  492. &dev_attr_preferred_erase_size.attr,
  493. &dev_attr_fwrev.attr,
  494. &dev_attr_hwrev.attr,
  495. &dev_attr_manfid.attr,
  496. &dev_attr_name.attr,
  497. &dev_attr_oemid.attr,
  498. &dev_attr_serial.attr,
  499. &dev_attr_enhanced_area_offset.attr,
  500. &dev_attr_enhanced_area_size.attr,
  501. NULL,
  502. };
  503. static struct attribute_group mmc_std_attr_group = {
  504. .attrs = mmc_std_attrs,
  505. };
  506. static const struct attribute_group *mmc_attr_groups[] = {
  507. &mmc_std_attr_group,
  508. NULL,
  509. };
  510. static struct device_type mmc_type = {
  511. .groups = mmc_attr_groups,
  512. };
  513. /*
  514. * Select the PowerClass for the current bus width
  515. * If power class is defined for 4/8 bit bus in the
  516. * extended CSD register, select it by executing the
  517. * mmc_switch command.
  518. */
  519. static int mmc_select_powerclass(struct mmc_card *card,
  520. unsigned int bus_width, u8 *ext_csd)
  521. {
  522. int err = 0;
  523. unsigned int pwrclass_val;
  524. unsigned int index = 0;
  525. struct mmc_host *host;
  526. BUG_ON(!card);
  527. host = card->host;
  528. BUG_ON(!host);
  529. if (ext_csd == NULL)
  530. return 0;
  531. /* Power class selection is supported for versions >= 4.0 */
  532. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  533. return 0;
  534. /* Power class values are defined only for 4/8 bit bus */
  535. if (bus_width == EXT_CSD_BUS_WIDTH_1)
  536. return 0;
  537. switch (1 << host->ios.vdd) {
  538. case MMC_VDD_165_195:
  539. if (host->ios.clock <= 26000000)
  540. index = EXT_CSD_PWR_CL_26_195;
  541. else if (host->ios.clock <= 52000000)
  542. index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  543. EXT_CSD_PWR_CL_52_195 :
  544. EXT_CSD_PWR_CL_DDR_52_195;
  545. else if (host->ios.clock <= 200000000)
  546. index = EXT_CSD_PWR_CL_200_195;
  547. break;
  548. case MMC_VDD_32_33:
  549. case MMC_VDD_33_34:
  550. case MMC_VDD_34_35:
  551. case MMC_VDD_35_36:
  552. if (host->ios.clock <= 26000000)
  553. index = EXT_CSD_PWR_CL_26_360;
  554. else if (host->ios.clock <= 52000000)
  555. index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  556. EXT_CSD_PWR_CL_52_360 :
  557. EXT_CSD_PWR_CL_DDR_52_360;
  558. else if (host->ios.clock <= 200000000)
  559. index = EXT_CSD_PWR_CL_200_360;
  560. break;
  561. default:
  562. pr_warning("%s: Voltage range not supported "
  563. "for power class.\n", mmc_hostname(host));
  564. return -EINVAL;
  565. }
  566. pwrclass_val = ext_csd[index];
  567. if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
  568. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
  569. EXT_CSD_PWR_CL_8BIT_SHIFT;
  570. else
  571. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
  572. EXT_CSD_PWR_CL_4BIT_SHIFT;
  573. /* If the power class is different from the default value */
  574. if (pwrclass_val > 0) {
  575. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  576. EXT_CSD_POWER_CLASS,
  577. pwrclass_val,
  578. 0);
  579. }
  580. return err;
  581. }
  582. /*
  583. * Handle the detection and initialisation of a card.
  584. *
  585. * In the case of a resume, "oldcard" will contain the card
  586. * we're trying to reinitialise.
  587. */
  588. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  589. struct mmc_card *oldcard)
  590. {
  591. struct mmc_card *card;
  592. int err, ddr = 0;
  593. u32 cid[4];
  594. unsigned int max_dtr;
  595. u32 rocr;
  596. u8 *ext_csd = NULL;
  597. BUG_ON(!host);
  598. WARN_ON(!host->claimed);
  599. /* Set correct bus mode for MMC before attempting init */
  600. if (!mmc_host_is_spi(host))
  601. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  602. /*
  603. * Since we're changing the OCR value, we seem to
  604. * need to tell some cards to go back to the idle
  605. * state. We wait 1ms to give cards time to
  606. * respond.
  607. * mmc_go_idle is needed for eMMC that are asleep
  608. */
  609. mmc_go_idle(host);
  610. /* The extra bit indicates that we support high capacity */
  611. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  612. if (err)
  613. goto err;
  614. /*
  615. * For SPI, enable CRC as appropriate.
  616. */
  617. if (mmc_host_is_spi(host)) {
  618. err = mmc_spi_set_crc(host, use_spi_crc);
  619. if (err)
  620. goto err;
  621. }
  622. /*
  623. * Fetch CID from card.
  624. */
  625. if (mmc_host_is_spi(host))
  626. err = mmc_send_cid(host, cid);
  627. else
  628. err = mmc_all_send_cid(host, cid);
  629. if (err)
  630. goto err;
  631. if (oldcard) {
  632. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  633. err = -ENOENT;
  634. goto err;
  635. }
  636. card = oldcard;
  637. } else {
  638. /*
  639. * Allocate card structure.
  640. */
  641. card = mmc_alloc_card(host, &mmc_type);
  642. if (IS_ERR(card)) {
  643. err = PTR_ERR(card);
  644. goto err;
  645. }
  646. card->type = MMC_TYPE_MMC;
  647. card->rca = 1;
  648. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  649. }
  650. /*
  651. * For native busses: set card RCA and quit open drain mode.
  652. */
  653. if (!mmc_host_is_spi(host)) {
  654. err = mmc_set_relative_addr(card);
  655. if (err)
  656. goto free_card;
  657. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  658. }
  659. if (!oldcard) {
  660. /*
  661. * Fetch CSD from card.
  662. */
  663. err = mmc_send_csd(card, card->raw_csd);
  664. if (err)
  665. goto free_card;
  666. err = mmc_decode_csd(card);
  667. if (err)
  668. goto free_card;
  669. err = mmc_decode_cid(card);
  670. if (err)
  671. goto free_card;
  672. }
  673. /*
  674. * Select card, as all following commands rely on that.
  675. */
  676. if (!mmc_host_is_spi(host)) {
  677. err = mmc_select_card(card);
  678. if (err)
  679. goto free_card;
  680. }
  681. if (!oldcard) {
  682. /*
  683. * Fetch and process extended CSD.
  684. */
  685. err = mmc_get_ext_csd(card, &ext_csd);
  686. if (err)
  687. goto free_card;
  688. err = mmc_read_ext_csd(card, ext_csd);
  689. if (err)
  690. goto free_card;
  691. /* If doing byte addressing, check if required to do sector
  692. * addressing. Handle the case of <2GB cards needing sector
  693. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  694. * ocr register has bit 30 set for sector addressing.
  695. */
  696. if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
  697. mmc_card_set_blockaddr(card);
  698. /* Erase size depends on CSD and Extended CSD */
  699. mmc_set_erase_size(card);
  700. }
  701. /*
  702. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  703. * bit. This bit will be lost every time after a reset or power off.
  704. */
  705. if (card->ext_csd.enhanced_area_en) {
  706. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  707. EXT_CSD_ERASE_GROUP_DEF, 1, 0);
  708. if (err && err != -EBADMSG)
  709. goto free_card;
  710. if (err) {
  711. err = 0;
  712. /*
  713. * Just disable enhanced area off & sz
  714. * will try to enable ERASE_GROUP_DEF
  715. * during next time reinit
  716. */
  717. card->ext_csd.enhanced_area_offset = -EINVAL;
  718. card->ext_csd.enhanced_area_size = -EINVAL;
  719. } else {
  720. card->ext_csd.erase_group_def = 1;
  721. /*
  722. * enable ERASE_GRP_DEF successfully.
  723. * This will affect the erase size, so
  724. * here need to reset erase size
  725. */
  726. mmc_set_erase_size(card);
  727. }
  728. }
  729. /*
  730. * Ensure eMMC user default partition is enabled
  731. */
  732. if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
  733. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  734. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
  735. card->ext_csd.part_config,
  736. card->ext_csd.part_time);
  737. if (err && err != -EBADMSG)
  738. goto free_card;
  739. }
  740. /*
  741. * Activate high speed (if supported)
  742. */
  743. if ((card->ext_csd.hs_max_dtr != 0) &&
  744. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  745. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  746. EXT_CSD_HS_TIMING, 1, 0);
  747. if (err && err != -EBADMSG)
  748. goto free_card;
  749. if (err) {
  750. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  751. mmc_hostname(card->host));
  752. err = 0;
  753. } else {
  754. mmc_card_set_highspeed(card);
  755. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  756. }
  757. }
  758. /*
  759. * Compute bus speed.
  760. */
  761. max_dtr = (unsigned int)-1;
  762. if (mmc_card_highspeed(card)) {
  763. if (max_dtr > card->ext_csd.hs_max_dtr)
  764. max_dtr = card->ext_csd.hs_max_dtr;
  765. } else if (max_dtr > card->csd.max_dtr) {
  766. max_dtr = card->csd.max_dtr;
  767. }
  768. mmc_set_clock(host, max_dtr);
  769. /*
  770. * Indicate DDR mode (if supported).
  771. */
  772. if (mmc_card_highspeed(card)) {
  773. if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
  774. && ((host->caps & (MMC_CAP_1_8V_DDR |
  775. MMC_CAP_UHS_DDR50))
  776. == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
  777. ddr = MMC_1_8V_DDR_MODE;
  778. else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
  779. && ((host->caps & (MMC_CAP_1_2V_DDR |
  780. MMC_CAP_UHS_DDR50))
  781. == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
  782. ddr = MMC_1_2V_DDR_MODE;
  783. }
  784. /*
  785. * Activate wide bus and DDR (if supported).
  786. */
  787. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  788. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  789. static unsigned ext_csd_bits[][2] = {
  790. { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
  791. { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
  792. { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
  793. };
  794. static unsigned bus_widths[] = {
  795. MMC_BUS_WIDTH_8,
  796. MMC_BUS_WIDTH_4,
  797. MMC_BUS_WIDTH_1
  798. };
  799. unsigned idx, bus_width = 0;
  800. if (host->caps & MMC_CAP_8_BIT_DATA)
  801. idx = 0;
  802. else
  803. idx = 1;
  804. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  805. bus_width = bus_widths[idx];
  806. if (bus_width == MMC_BUS_WIDTH_1)
  807. ddr = 0; /* no DDR for 1-bit width */
  808. err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
  809. ext_csd);
  810. if (err)
  811. pr_err("%s: power class selection to "
  812. "bus width %d failed\n",
  813. mmc_hostname(card->host),
  814. 1 << bus_width);
  815. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  816. EXT_CSD_BUS_WIDTH,
  817. ext_csd_bits[idx][0],
  818. 0);
  819. if (!err) {
  820. mmc_set_bus_width(card->host, bus_width);
  821. /*
  822. * If controller can't handle bus width test,
  823. * compare ext_csd previously read in 1 bit mode
  824. * against ext_csd at new bus width
  825. */
  826. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  827. err = mmc_compare_ext_csds(card,
  828. bus_width);
  829. else
  830. err = mmc_bus_test(card, bus_width);
  831. if (!err)
  832. break;
  833. }
  834. }
  835. if (!err && ddr) {
  836. err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
  837. ext_csd);
  838. if (err)
  839. pr_err("%s: power class selection to "
  840. "bus width %d ddr %d failed\n",
  841. mmc_hostname(card->host),
  842. 1 << bus_width, ddr);
  843. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  844. EXT_CSD_BUS_WIDTH,
  845. ext_csd_bits[idx][1],
  846. 0);
  847. }
  848. if (err) {
  849. printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
  850. "failed\n", mmc_hostname(card->host),
  851. 1 << bus_width, ddr);
  852. goto free_card;
  853. } else if (ddr) {
  854. /*
  855. * eMMC cards can support 3.3V to 1.2V i/o (vccq)
  856. * signaling.
  857. *
  858. * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
  859. *
  860. * 1.8V vccq at 3.3V core voltage (vcc) is not required
  861. * in the JEDEC spec for DDR.
  862. *
  863. * Do not force change in vccq since we are obviously
  864. * working and no change to vccq is needed.
  865. *
  866. * WARNING: eMMC rules are NOT the same as SD DDR
  867. */
  868. if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
  869. err = mmc_set_signal_voltage(host,
  870. MMC_SIGNAL_VOLTAGE_120, 0);
  871. if (err)
  872. goto err;
  873. }
  874. mmc_card_set_ddr_mode(card);
  875. mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
  876. mmc_set_bus_width(card->host, bus_width);
  877. }
  878. }
  879. if (!oldcard)
  880. host->card = card;
  881. mmc_free_ext_csd(ext_csd);
  882. return 0;
  883. free_card:
  884. if (!oldcard)
  885. mmc_remove_card(card);
  886. err:
  887. mmc_free_ext_csd(ext_csd);
  888. return err;
  889. }
  890. /*
  891. * Host is being removed. Free up the current card.
  892. */
  893. static void mmc_remove(struct mmc_host *host)
  894. {
  895. BUG_ON(!host);
  896. BUG_ON(!host->card);
  897. mmc_remove_card(host->card);
  898. host->card = NULL;
  899. }
  900. /*
  901. * Card detection callback from host.
  902. */
  903. static void mmc_detect(struct mmc_host *host)
  904. {
  905. int err;
  906. BUG_ON(!host);
  907. BUG_ON(!host->card);
  908. mmc_claim_host(host);
  909. /*
  910. * Just check if our card has been removed.
  911. */
  912. err = mmc_send_status(host->card, NULL);
  913. mmc_release_host(host);
  914. if (err) {
  915. mmc_remove(host);
  916. mmc_claim_host(host);
  917. mmc_detach_bus(host);
  918. mmc_power_off(host);
  919. mmc_release_host(host);
  920. }
  921. }
  922. /*
  923. * Suspend callback from host.
  924. */
  925. static int mmc_suspend(struct mmc_host *host)
  926. {
  927. int err = 0;
  928. BUG_ON(!host);
  929. BUG_ON(!host->card);
  930. mmc_claim_host(host);
  931. if (mmc_card_can_sleep(host))
  932. err = mmc_card_sleep(host);
  933. else if (!mmc_host_is_spi(host))
  934. mmc_deselect_cards(host);
  935. host->card->state &= ~MMC_STATE_HIGHSPEED;
  936. mmc_release_host(host);
  937. return err;
  938. }
  939. /*
  940. * Resume callback from host.
  941. *
  942. * This function tries to determine if the same card is still present
  943. * and, if so, restore all state to it.
  944. */
  945. static int mmc_resume(struct mmc_host *host)
  946. {
  947. int err;
  948. BUG_ON(!host);
  949. BUG_ON(!host->card);
  950. mmc_claim_host(host);
  951. err = mmc_init_card(host, host->ocr, host->card);
  952. mmc_release_host(host);
  953. return err;
  954. }
  955. static int mmc_power_restore(struct mmc_host *host)
  956. {
  957. int ret;
  958. host->card->state &= ~MMC_STATE_HIGHSPEED;
  959. mmc_claim_host(host);
  960. ret = mmc_init_card(host, host->ocr, host->card);
  961. mmc_release_host(host);
  962. return ret;
  963. }
  964. static int mmc_sleep(struct mmc_host *host)
  965. {
  966. struct mmc_card *card = host->card;
  967. int err = -ENOSYS;
  968. if (card && card->ext_csd.rev >= 3) {
  969. err = mmc_card_sleepawake(host, 1);
  970. if (err < 0)
  971. pr_debug("%s: Error %d while putting card into sleep",
  972. mmc_hostname(host), err);
  973. }
  974. return err;
  975. }
  976. static int mmc_awake(struct mmc_host *host)
  977. {
  978. struct mmc_card *card = host->card;
  979. int err = -ENOSYS;
  980. if (card && card->ext_csd.rev >= 3) {
  981. err = mmc_card_sleepawake(host, 0);
  982. if (err < 0)
  983. pr_debug("%s: Error %d while awaking sleeping card",
  984. mmc_hostname(host), err);
  985. }
  986. return err;
  987. }
  988. static const struct mmc_bus_ops mmc_ops = {
  989. .awake = mmc_awake,
  990. .sleep = mmc_sleep,
  991. .remove = mmc_remove,
  992. .detect = mmc_detect,
  993. .suspend = NULL,
  994. .resume = NULL,
  995. .power_restore = mmc_power_restore,
  996. };
  997. static const struct mmc_bus_ops mmc_ops_unsafe = {
  998. .awake = mmc_awake,
  999. .sleep = mmc_sleep,
  1000. .remove = mmc_remove,
  1001. .detect = mmc_detect,
  1002. .suspend = mmc_suspend,
  1003. .resume = mmc_resume,
  1004. .power_restore = mmc_power_restore,
  1005. };
  1006. static void mmc_attach_bus_ops(struct mmc_host *host)
  1007. {
  1008. const struct mmc_bus_ops *bus_ops;
  1009. if (!mmc_card_is_removable(host))
  1010. bus_ops = &mmc_ops_unsafe;
  1011. else
  1012. bus_ops = &mmc_ops;
  1013. mmc_attach_bus(host, bus_ops);
  1014. }
  1015. /*
  1016. * Starting point for MMC card init.
  1017. */
  1018. int mmc_attach_mmc(struct mmc_host *host)
  1019. {
  1020. int err;
  1021. u32 ocr;
  1022. BUG_ON(!host);
  1023. WARN_ON(!host->claimed);
  1024. /* Set correct bus mode for MMC before attempting attach */
  1025. if (!mmc_host_is_spi(host))
  1026. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1027. err = mmc_send_op_cond(host, 0, &ocr);
  1028. if (err)
  1029. return err;
  1030. mmc_attach_bus_ops(host);
  1031. if (host->ocr_avail_mmc)
  1032. host->ocr_avail = host->ocr_avail_mmc;
  1033. /*
  1034. * We need to get OCR a different way for SPI.
  1035. */
  1036. if (mmc_host_is_spi(host)) {
  1037. err = mmc_spi_read_ocr(host, 1, &ocr);
  1038. if (err)
  1039. goto err;
  1040. }
  1041. /*
  1042. * Sanity check the voltages that the card claims to
  1043. * support.
  1044. */
  1045. if (ocr & 0x7F) {
  1046. printk(KERN_WARNING "%s: card claims to support voltages "
  1047. "below the defined range. These will be ignored.\n",
  1048. mmc_hostname(host));
  1049. ocr &= ~0x7F;
  1050. }
  1051. host->ocr = mmc_select_voltage(host, ocr);
  1052. /*
  1053. * Can we support the voltage of the card?
  1054. */
  1055. if (!host->ocr) {
  1056. err = -EINVAL;
  1057. goto err;
  1058. }
  1059. /*
  1060. * Detect and init the card.
  1061. */
  1062. err = mmc_init_card(host, host->ocr, NULL);
  1063. if (err)
  1064. goto err;
  1065. mmc_release_host(host);
  1066. err = mmc_add_card(host->card);
  1067. mmc_claim_host(host);
  1068. if (err)
  1069. goto remove_card;
  1070. return 0;
  1071. remove_card:
  1072. mmc_release_host(host);
  1073. mmc_remove_card(host->card);
  1074. mmc_claim_host(host);
  1075. host->card = NULL;
  1076. err:
  1077. mmc_detach_bus(host);
  1078. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  1079. mmc_hostname(host), err);
  1080. return err;
  1081. }