mmc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. pr_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. pr_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. pr_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. pr_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. pr_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. pr_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. pr_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. pr_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. if (card->ext_csd.rev >= 6)
  410. card->ext_csd.generic_cmd6_time = 10 *
  411. ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
  412. else
  413. card->ext_csd.generic_cmd6_time = 0;
  414. out:
  415. return err;
  416. }
  417. static inline void mmc_free_ext_csd(u8 *ext_csd)
  418. {
  419. kfree(ext_csd);
  420. }
  421. static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
  422. {
  423. u8 *bw_ext_csd;
  424. int err;
  425. if (bus_width == MMC_BUS_WIDTH_1)
  426. return 0;
  427. err = mmc_get_ext_csd(card, &bw_ext_csd);
  428. if (err || bw_ext_csd == NULL) {
  429. if (bus_width != MMC_BUS_WIDTH_1)
  430. err = -EINVAL;
  431. goto out;
  432. }
  433. if (bus_width == MMC_BUS_WIDTH_1)
  434. goto out;
  435. /* only compare read only fields */
  436. err = (!(card->ext_csd.raw_partition_support ==
  437. bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  438. (card->ext_csd.raw_erased_mem_count ==
  439. bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  440. (card->ext_csd.rev ==
  441. bw_ext_csd[EXT_CSD_REV]) &&
  442. (card->ext_csd.raw_ext_csd_structure ==
  443. bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  444. (card->ext_csd.raw_card_type ==
  445. bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  446. (card->ext_csd.raw_s_a_timeout ==
  447. bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  448. (card->ext_csd.raw_hc_erase_gap_size ==
  449. bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  450. (card->ext_csd.raw_erase_timeout_mult ==
  451. bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  452. (card->ext_csd.raw_hc_erase_grp_size ==
  453. bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  454. (card->ext_csd.raw_sec_trim_mult ==
  455. bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  456. (card->ext_csd.raw_sec_erase_mult ==
  457. bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  458. (card->ext_csd.raw_sec_feature_support ==
  459. bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  460. (card->ext_csd.raw_trim_mult ==
  461. bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  462. (card->ext_csd.raw_sectors[0] ==
  463. bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  464. (card->ext_csd.raw_sectors[1] ==
  465. bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  466. (card->ext_csd.raw_sectors[2] ==
  467. bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  468. (card->ext_csd.raw_sectors[3] ==
  469. bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
  470. if (err)
  471. err = -EINVAL;
  472. out:
  473. mmc_free_ext_csd(bw_ext_csd);
  474. return err;
  475. }
  476. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  477. card->raw_cid[2], card->raw_cid[3]);
  478. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  479. card->raw_csd[2], card->raw_csd[3]);
  480. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  481. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  482. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  483. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  484. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  485. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  486. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  487. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  488. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  489. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  490. card->ext_csd.enhanced_area_offset);
  491. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  492. static struct attribute *mmc_std_attrs[] = {
  493. &dev_attr_cid.attr,
  494. &dev_attr_csd.attr,
  495. &dev_attr_date.attr,
  496. &dev_attr_erase_size.attr,
  497. &dev_attr_preferred_erase_size.attr,
  498. &dev_attr_fwrev.attr,
  499. &dev_attr_hwrev.attr,
  500. &dev_attr_manfid.attr,
  501. &dev_attr_name.attr,
  502. &dev_attr_oemid.attr,
  503. &dev_attr_serial.attr,
  504. &dev_attr_enhanced_area_offset.attr,
  505. &dev_attr_enhanced_area_size.attr,
  506. NULL,
  507. };
  508. static struct attribute_group mmc_std_attr_group = {
  509. .attrs = mmc_std_attrs,
  510. };
  511. static const struct attribute_group *mmc_attr_groups[] = {
  512. &mmc_std_attr_group,
  513. NULL,
  514. };
  515. static struct device_type mmc_type = {
  516. .groups = mmc_attr_groups,
  517. };
  518. /*
  519. * Select the PowerClass for the current bus width
  520. * If power class is defined for 4/8 bit bus in the
  521. * extended CSD register, select it by executing the
  522. * mmc_switch command.
  523. */
  524. static int mmc_select_powerclass(struct mmc_card *card,
  525. unsigned int bus_width, u8 *ext_csd)
  526. {
  527. int err = 0;
  528. unsigned int pwrclass_val;
  529. unsigned int index = 0;
  530. struct mmc_host *host;
  531. BUG_ON(!card);
  532. host = card->host;
  533. BUG_ON(!host);
  534. if (ext_csd == NULL)
  535. return 0;
  536. /* Power class selection is supported for versions >= 4.0 */
  537. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  538. return 0;
  539. /* Power class values are defined only for 4/8 bit bus */
  540. if (bus_width == EXT_CSD_BUS_WIDTH_1)
  541. return 0;
  542. switch (1 << host->ios.vdd) {
  543. case MMC_VDD_165_195:
  544. if (host->ios.clock <= 26000000)
  545. index = EXT_CSD_PWR_CL_26_195;
  546. else if (host->ios.clock <= 52000000)
  547. index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  548. EXT_CSD_PWR_CL_52_195 :
  549. EXT_CSD_PWR_CL_DDR_52_195;
  550. else if (host->ios.clock <= 200000000)
  551. index = EXT_CSD_PWR_CL_200_195;
  552. break;
  553. case MMC_VDD_32_33:
  554. case MMC_VDD_33_34:
  555. case MMC_VDD_34_35:
  556. case MMC_VDD_35_36:
  557. if (host->ios.clock <= 26000000)
  558. index = EXT_CSD_PWR_CL_26_360;
  559. else if (host->ios.clock <= 52000000)
  560. index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  561. EXT_CSD_PWR_CL_52_360 :
  562. EXT_CSD_PWR_CL_DDR_52_360;
  563. else if (host->ios.clock <= 200000000)
  564. index = EXT_CSD_PWR_CL_200_360;
  565. break;
  566. default:
  567. pr_warning("%s: Voltage range not supported "
  568. "for power class.\n", mmc_hostname(host));
  569. return -EINVAL;
  570. }
  571. pwrclass_val = ext_csd[index];
  572. if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
  573. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
  574. EXT_CSD_PWR_CL_8BIT_SHIFT;
  575. else
  576. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
  577. EXT_CSD_PWR_CL_4BIT_SHIFT;
  578. /* If the power class is different from the default value */
  579. if (pwrclass_val > 0) {
  580. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  581. EXT_CSD_POWER_CLASS,
  582. pwrclass_val,
  583. 0);
  584. }
  585. return err;
  586. }
  587. /*
  588. * Handle the detection and initialisation of a card.
  589. *
  590. * In the case of a resume, "oldcard" will contain the card
  591. * we're trying to reinitialise.
  592. */
  593. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  594. struct mmc_card *oldcard)
  595. {
  596. struct mmc_card *card;
  597. int err, ddr = 0;
  598. u32 cid[4];
  599. unsigned int max_dtr;
  600. u32 rocr;
  601. u8 *ext_csd = NULL;
  602. BUG_ON(!host);
  603. WARN_ON(!host->claimed);
  604. /* Set correct bus mode for MMC before attempting init */
  605. if (!mmc_host_is_spi(host))
  606. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  607. /*
  608. * Since we're changing the OCR value, we seem to
  609. * need to tell some cards to go back to the idle
  610. * state. We wait 1ms to give cards time to
  611. * respond.
  612. * mmc_go_idle is needed for eMMC that are asleep
  613. */
  614. mmc_go_idle(host);
  615. /* The extra bit indicates that we support high capacity */
  616. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  617. if (err)
  618. goto err;
  619. /*
  620. * For SPI, enable CRC as appropriate.
  621. */
  622. if (mmc_host_is_spi(host)) {
  623. err = mmc_spi_set_crc(host, use_spi_crc);
  624. if (err)
  625. goto err;
  626. }
  627. /*
  628. * Fetch CID from card.
  629. */
  630. if (mmc_host_is_spi(host))
  631. err = mmc_send_cid(host, cid);
  632. else
  633. err = mmc_all_send_cid(host, cid);
  634. if (err)
  635. goto err;
  636. if (oldcard) {
  637. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  638. err = -ENOENT;
  639. goto err;
  640. }
  641. card = oldcard;
  642. } else {
  643. /*
  644. * Allocate card structure.
  645. */
  646. card = mmc_alloc_card(host, &mmc_type);
  647. if (IS_ERR(card)) {
  648. err = PTR_ERR(card);
  649. goto err;
  650. }
  651. card->type = MMC_TYPE_MMC;
  652. card->rca = 1;
  653. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  654. }
  655. /*
  656. * For native busses: set card RCA and quit open drain mode.
  657. */
  658. if (!mmc_host_is_spi(host)) {
  659. err = mmc_set_relative_addr(card);
  660. if (err)
  661. goto free_card;
  662. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  663. }
  664. if (!oldcard) {
  665. /*
  666. * Fetch CSD from card.
  667. */
  668. err = mmc_send_csd(card, card->raw_csd);
  669. if (err)
  670. goto free_card;
  671. err = mmc_decode_csd(card);
  672. if (err)
  673. goto free_card;
  674. err = mmc_decode_cid(card);
  675. if (err)
  676. goto free_card;
  677. }
  678. /*
  679. * Select card, as all following commands rely on that.
  680. */
  681. if (!mmc_host_is_spi(host)) {
  682. err = mmc_select_card(card);
  683. if (err)
  684. goto free_card;
  685. }
  686. if (!oldcard) {
  687. /*
  688. * Fetch and process extended CSD.
  689. */
  690. err = mmc_get_ext_csd(card, &ext_csd);
  691. if (err)
  692. goto free_card;
  693. err = mmc_read_ext_csd(card, ext_csd);
  694. if (err)
  695. goto free_card;
  696. /* If doing byte addressing, check if required to do sector
  697. * addressing. Handle the case of <2GB cards needing sector
  698. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  699. * ocr register has bit 30 set for sector addressing.
  700. */
  701. if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
  702. mmc_card_set_blockaddr(card);
  703. /* Erase size depends on CSD and Extended CSD */
  704. mmc_set_erase_size(card);
  705. }
  706. /*
  707. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  708. * bit. This bit will be lost every time after a reset or power off.
  709. */
  710. if (card->ext_csd.enhanced_area_en) {
  711. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  712. EXT_CSD_ERASE_GROUP_DEF, 1,
  713. card->ext_csd.generic_cmd6_time);
  714. if (err && err != -EBADMSG)
  715. goto free_card;
  716. if (err) {
  717. err = 0;
  718. /*
  719. * Just disable enhanced area off & sz
  720. * will try to enable ERASE_GROUP_DEF
  721. * during next time reinit
  722. */
  723. card->ext_csd.enhanced_area_offset = -EINVAL;
  724. card->ext_csd.enhanced_area_size = -EINVAL;
  725. } else {
  726. card->ext_csd.erase_group_def = 1;
  727. /*
  728. * enable ERASE_GRP_DEF successfully.
  729. * This will affect the erase size, so
  730. * here need to reset erase size
  731. */
  732. mmc_set_erase_size(card);
  733. }
  734. }
  735. /*
  736. * Ensure eMMC user default partition is enabled
  737. */
  738. if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
  739. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  740. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
  741. card->ext_csd.part_config,
  742. card->ext_csd.part_time);
  743. if (err && err != -EBADMSG)
  744. goto free_card;
  745. }
  746. /*
  747. * Activate high speed (if supported)
  748. */
  749. if ((card->ext_csd.hs_max_dtr != 0) &&
  750. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  751. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  752. EXT_CSD_HS_TIMING, 1,
  753. card->ext_csd.generic_cmd6_time);
  754. if (err && err != -EBADMSG)
  755. goto free_card;
  756. if (err) {
  757. pr_warning("%s: switch to highspeed failed\n",
  758. mmc_hostname(card->host));
  759. err = 0;
  760. } else {
  761. mmc_card_set_highspeed(card);
  762. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  763. }
  764. }
  765. /*
  766. * Compute bus speed.
  767. */
  768. max_dtr = (unsigned int)-1;
  769. if (mmc_card_highspeed(card)) {
  770. if (max_dtr > card->ext_csd.hs_max_dtr)
  771. max_dtr = card->ext_csd.hs_max_dtr;
  772. } else if (max_dtr > card->csd.max_dtr) {
  773. max_dtr = card->csd.max_dtr;
  774. }
  775. mmc_set_clock(host, max_dtr);
  776. /*
  777. * Indicate DDR mode (if supported).
  778. */
  779. if (mmc_card_highspeed(card)) {
  780. if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
  781. && ((host->caps & (MMC_CAP_1_8V_DDR |
  782. MMC_CAP_UHS_DDR50))
  783. == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
  784. ddr = MMC_1_8V_DDR_MODE;
  785. else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
  786. && ((host->caps & (MMC_CAP_1_2V_DDR |
  787. MMC_CAP_UHS_DDR50))
  788. == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
  789. ddr = MMC_1_2V_DDR_MODE;
  790. }
  791. /*
  792. * Activate wide bus and DDR (if supported).
  793. */
  794. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  795. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  796. static unsigned ext_csd_bits[][2] = {
  797. { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
  798. { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
  799. { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
  800. };
  801. static unsigned bus_widths[] = {
  802. MMC_BUS_WIDTH_8,
  803. MMC_BUS_WIDTH_4,
  804. MMC_BUS_WIDTH_1
  805. };
  806. unsigned idx, bus_width = 0;
  807. if (host->caps & MMC_CAP_8_BIT_DATA)
  808. idx = 0;
  809. else
  810. idx = 1;
  811. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  812. bus_width = bus_widths[idx];
  813. if (bus_width == MMC_BUS_WIDTH_1)
  814. ddr = 0; /* no DDR for 1-bit width */
  815. err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
  816. ext_csd);
  817. if (err)
  818. pr_err("%s: power class selection to "
  819. "bus width %d failed\n",
  820. mmc_hostname(card->host),
  821. 1 << bus_width);
  822. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  823. EXT_CSD_BUS_WIDTH,
  824. ext_csd_bits[idx][0],
  825. card->ext_csd.generic_cmd6_time);
  826. if (!err) {
  827. mmc_set_bus_width(card->host, bus_width);
  828. /*
  829. * If controller can't handle bus width test,
  830. * compare ext_csd previously read in 1 bit mode
  831. * against ext_csd at new bus width
  832. */
  833. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  834. err = mmc_compare_ext_csds(card,
  835. bus_width);
  836. else
  837. err = mmc_bus_test(card, bus_width);
  838. if (!err)
  839. break;
  840. }
  841. }
  842. if (!err && ddr) {
  843. err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
  844. ext_csd);
  845. if (err)
  846. pr_err("%s: power class selection to "
  847. "bus width %d ddr %d failed\n",
  848. mmc_hostname(card->host),
  849. 1 << bus_width, ddr);
  850. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  851. EXT_CSD_BUS_WIDTH,
  852. ext_csd_bits[idx][1],
  853. card->ext_csd.generic_cmd6_time);
  854. }
  855. if (err) {
  856. pr_warning("%s: switch to bus width %d ddr %d "
  857. "failed\n", mmc_hostname(card->host),
  858. 1 << bus_width, ddr);
  859. goto free_card;
  860. } else if (ddr) {
  861. /*
  862. * eMMC cards can support 3.3V to 1.2V i/o (vccq)
  863. * signaling.
  864. *
  865. * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
  866. *
  867. * 1.8V vccq at 3.3V core voltage (vcc) is not required
  868. * in the JEDEC spec for DDR.
  869. *
  870. * Do not force change in vccq since we are obviously
  871. * working and no change to vccq is needed.
  872. *
  873. * WARNING: eMMC rules are NOT the same as SD DDR
  874. */
  875. if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
  876. err = mmc_set_signal_voltage(host,
  877. MMC_SIGNAL_VOLTAGE_120, 0);
  878. if (err)
  879. goto err;
  880. }
  881. mmc_card_set_ddr_mode(card);
  882. mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
  883. mmc_set_bus_width(card->host, bus_width);
  884. }
  885. }
  886. if (!oldcard)
  887. host->card = card;
  888. mmc_free_ext_csd(ext_csd);
  889. return 0;
  890. free_card:
  891. if (!oldcard)
  892. mmc_remove_card(card);
  893. err:
  894. mmc_free_ext_csd(ext_csd);
  895. return err;
  896. }
  897. /*
  898. * Host is being removed. Free up the current card.
  899. */
  900. static void mmc_remove(struct mmc_host *host)
  901. {
  902. BUG_ON(!host);
  903. BUG_ON(!host->card);
  904. mmc_remove_card(host->card);
  905. host->card = NULL;
  906. }
  907. /*
  908. * Card detection callback from host.
  909. */
  910. static void mmc_detect(struct mmc_host *host)
  911. {
  912. int err;
  913. BUG_ON(!host);
  914. BUG_ON(!host->card);
  915. mmc_claim_host(host);
  916. /*
  917. * Just check if our card has been removed.
  918. */
  919. err = mmc_send_status(host->card, NULL);
  920. mmc_release_host(host);
  921. if (err) {
  922. mmc_remove(host);
  923. mmc_claim_host(host);
  924. mmc_detach_bus(host);
  925. mmc_power_off(host);
  926. mmc_release_host(host);
  927. }
  928. }
  929. /*
  930. * Suspend callback from host.
  931. */
  932. static int mmc_suspend(struct mmc_host *host)
  933. {
  934. int err = 0;
  935. BUG_ON(!host);
  936. BUG_ON(!host->card);
  937. mmc_claim_host(host);
  938. if (mmc_card_can_sleep(host))
  939. err = mmc_card_sleep(host);
  940. else if (!mmc_host_is_spi(host))
  941. mmc_deselect_cards(host);
  942. host->card->state &= ~MMC_STATE_HIGHSPEED;
  943. mmc_release_host(host);
  944. return err;
  945. }
  946. /*
  947. * Resume callback from host.
  948. *
  949. * This function tries to determine if the same card is still present
  950. * and, if so, restore all state to it.
  951. */
  952. static int mmc_resume(struct mmc_host *host)
  953. {
  954. int err;
  955. BUG_ON(!host);
  956. BUG_ON(!host->card);
  957. mmc_claim_host(host);
  958. err = mmc_init_card(host, host->ocr, host->card);
  959. mmc_release_host(host);
  960. return err;
  961. }
  962. static int mmc_power_restore(struct mmc_host *host)
  963. {
  964. int ret;
  965. host->card->state &= ~MMC_STATE_HIGHSPEED;
  966. mmc_claim_host(host);
  967. ret = mmc_init_card(host, host->ocr, host->card);
  968. mmc_release_host(host);
  969. return ret;
  970. }
  971. static int mmc_sleep(struct mmc_host *host)
  972. {
  973. struct mmc_card *card = host->card;
  974. int err = -ENOSYS;
  975. if (card && card->ext_csd.rev >= 3) {
  976. err = mmc_card_sleepawake(host, 1);
  977. if (err < 0)
  978. pr_debug("%s: Error %d while putting card into sleep",
  979. mmc_hostname(host), err);
  980. }
  981. return err;
  982. }
  983. static int mmc_awake(struct mmc_host *host)
  984. {
  985. struct mmc_card *card = host->card;
  986. int err = -ENOSYS;
  987. if (card && card->ext_csd.rev >= 3) {
  988. err = mmc_card_sleepawake(host, 0);
  989. if (err < 0)
  990. pr_debug("%s: Error %d while awaking sleeping card",
  991. mmc_hostname(host), err);
  992. }
  993. return err;
  994. }
  995. static const struct mmc_bus_ops mmc_ops = {
  996. .awake = mmc_awake,
  997. .sleep = mmc_sleep,
  998. .remove = mmc_remove,
  999. .detect = mmc_detect,
  1000. .suspend = NULL,
  1001. .resume = NULL,
  1002. .power_restore = mmc_power_restore,
  1003. };
  1004. static const struct mmc_bus_ops mmc_ops_unsafe = {
  1005. .awake = mmc_awake,
  1006. .sleep = mmc_sleep,
  1007. .remove = mmc_remove,
  1008. .detect = mmc_detect,
  1009. .suspend = mmc_suspend,
  1010. .resume = mmc_resume,
  1011. .power_restore = mmc_power_restore,
  1012. };
  1013. static void mmc_attach_bus_ops(struct mmc_host *host)
  1014. {
  1015. const struct mmc_bus_ops *bus_ops;
  1016. if (!mmc_card_is_removable(host))
  1017. bus_ops = &mmc_ops_unsafe;
  1018. else
  1019. bus_ops = &mmc_ops;
  1020. mmc_attach_bus(host, bus_ops);
  1021. }
  1022. /*
  1023. * Starting point for MMC card init.
  1024. */
  1025. int mmc_attach_mmc(struct mmc_host *host)
  1026. {
  1027. int err;
  1028. u32 ocr;
  1029. BUG_ON(!host);
  1030. WARN_ON(!host->claimed);
  1031. /* Set correct bus mode for MMC before attempting attach */
  1032. if (!mmc_host_is_spi(host))
  1033. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1034. err = mmc_send_op_cond(host, 0, &ocr);
  1035. if (err)
  1036. return err;
  1037. mmc_attach_bus_ops(host);
  1038. if (host->ocr_avail_mmc)
  1039. host->ocr_avail = host->ocr_avail_mmc;
  1040. /*
  1041. * We need to get OCR a different way for SPI.
  1042. */
  1043. if (mmc_host_is_spi(host)) {
  1044. err = mmc_spi_read_ocr(host, 1, &ocr);
  1045. if (err)
  1046. goto err;
  1047. }
  1048. /*
  1049. * Sanity check the voltages that the card claims to
  1050. * support.
  1051. */
  1052. if (ocr & 0x7F) {
  1053. pr_warning("%s: card claims to support voltages "
  1054. "below the defined range. These will be ignored.\n",
  1055. mmc_hostname(host));
  1056. ocr &= ~0x7F;
  1057. }
  1058. host->ocr = mmc_select_voltage(host, ocr);
  1059. /*
  1060. * Can we support the voltage of the card?
  1061. */
  1062. if (!host->ocr) {
  1063. err = -EINVAL;
  1064. goto err;
  1065. }
  1066. /*
  1067. * Detect and init the card.
  1068. */
  1069. err = mmc_init_card(host, host->ocr, NULL);
  1070. if (err)
  1071. goto err;
  1072. mmc_release_host(host);
  1073. err = mmc_add_card(host->card);
  1074. mmc_claim_host(host);
  1075. if (err)
  1076. goto remove_card;
  1077. return 0;
  1078. remove_card:
  1079. mmc_release_host(host);
  1080. mmc_remove_card(host->card);
  1081. mmc_claim_host(host);
  1082. host->card = NULL;
  1083. err:
  1084. mmc_detach_bus(host);
  1085. pr_err("%s: error %d whilst initialising MMC card\n",
  1086. mmc_hostname(host), err);
  1087. return err;
  1088. }