mmc.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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/stat.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/mmc.h>
  19. #include "core.h"
  20. #include "bus.h"
  21. #include "mmc_ops.h"
  22. #include "sd_ops.h"
  23. static const unsigned int tran_exp[] = {
  24. 10000, 100000, 1000000, 10000000,
  25. 0, 0, 0, 0
  26. };
  27. static const unsigned char tran_mant[] = {
  28. 0, 10, 12, 13, 15, 20, 25, 30,
  29. 35, 40, 45, 50, 55, 60, 70, 80,
  30. };
  31. static const unsigned int tacc_exp[] = {
  32. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  33. };
  34. static const unsigned int tacc_mant[] = {
  35. 0, 10, 12, 13, 15, 20, 25, 30,
  36. 35, 40, 45, 50, 55, 60, 70, 80,
  37. };
  38. #define UNSTUFF_BITS(resp,start,size) \
  39. ({ \
  40. const int __size = size; \
  41. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  42. const int __off = 3 - ((start) / 32); \
  43. const int __shft = (start) & 31; \
  44. u32 __res; \
  45. \
  46. __res = resp[__off] >> __shft; \
  47. if (__size + __shft > 32) \
  48. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  49. __res & __mask; \
  50. })
  51. /*
  52. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  53. */
  54. static int mmc_decode_cid(struct mmc_card *card)
  55. {
  56. u32 *resp = card->raw_cid;
  57. /*
  58. * The selection of the format here is based upon published
  59. * specs from sandisk and from what people have reported.
  60. */
  61. switch (card->csd.mmca_vsn) {
  62. case 0: /* MMC v1.0 - v1.2 */
  63. case 1: /* MMC v1.4 */
  64. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  65. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  66. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  67. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  68. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  69. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  70. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  71. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  72. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  73. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  74. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  75. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  76. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  77. break;
  78. case 2: /* MMC v2.0 - v2.2 */
  79. case 3: /* MMC v3.1 - v3.3 */
  80. case 4: /* MMC v4 */
  81. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  82. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  83. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  84. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  85. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  86. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  87. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  88. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  89. card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
  90. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  91. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  92. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  93. break;
  94. default:
  95. pr_err("%s: card has unknown MMCA version %d\n",
  96. mmc_hostname(card->host), card->csd.mmca_vsn);
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. static void mmc_set_erase_size(struct mmc_card *card)
  102. {
  103. if (card->ext_csd.erase_group_def & 1)
  104. card->erase_size = card->ext_csd.hc_erase_size;
  105. else
  106. card->erase_size = card->csd.erase_size;
  107. mmc_init_erase(card);
  108. }
  109. /*
  110. * Given a 128-bit response, decode to our card CSD structure.
  111. */
  112. static int mmc_decode_csd(struct mmc_card *card)
  113. {
  114. struct mmc_csd *csd = &card->csd;
  115. unsigned int e, m, a, b;
  116. u32 *resp = card->raw_csd;
  117. /*
  118. * We only understand CSD structure v1.1 and v1.2.
  119. * v1.2 has extra information in bits 15, 11 and 10.
  120. * We also support eMMC v4.4 & v4.41.
  121. */
  122. csd->structure = UNSTUFF_BITS(resp, 126, 2);
  123. if (csd->structure == 0) {
  124. pr_err("%s: unrecognised CSD structure version %d\n",
  125. mmc_hostname(card->host), csd->structure);
  126. return -EINVAL;
  127. }
  128. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  129. m = UNSTUFF_BITS(resp, 115, 4);
  130. e = UNSTUFF_BITS(resp, 112, 3);
  131. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  132. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  133. m = UNSTUFF_BITS(resp, 99, 4);
  134. e = UNSTUFF_BITS(resp, 96, 3);
  135. csd->max_dtr = tran_exp[e] * tran_mant[m];
  136. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  137. e = UNSTUFF_BITS(resp, 47, 3);
  138. m = UNSTUFF_BITS(resp, 62, 12);
  139. csd->capacity = (1 + m) << (e + 2);
  140. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  141. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  142. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  143. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  144. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  145. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  146. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  147. if (csd->write_blkbits >= 9) {
  148. a = UNSTUFF_BITS(resp, 42, 5);
  149. b = UNSTUFF_BITS(resp, 37, 5);
  150. csd->erase_size = (a + 1) * (b + 1);
  151. csd->erase_size <<= csd->write_blkbits - 9;
  152. }
  153. return 0;
  154. }
  155. /*
  156. * Read extended CSD.
  157. */
  158. static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
  159. {
  160. int err;
  161. u8 *ext_csd;
  162. BUG_ON(!card);
  163. BUG_ON(!new_ext_csd);
  164. *new_ext_csd = NULL;
  165. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  166. return 0;
  167. /*
  168. * As the ext_csd is so large and mostly unused, we don't store the
  169. * raw block in mmc_card.
  170. */
  171. ext_csd = kmalloc(512, GFP_KERNEL);
  172. if (!ext_csd) {
  173. pr_err("%s: could not allocate a buffer to "
  174. "receive the ext_csd.\n", mmc_hostname(card->host));
  175. return -ENOMEM;
  176. }
  177. err = mmc_send_ext_csd(card, ext_csd);
  178. if (err) {
  179. kfree(ext_csd);
  180. *new_ext_csd = NULL;
  181. /* If the host or the card can't do the switch,
  182. * fail more gracefully. */
  183. if ((err != -EINVAL)
  184. && (err != -ENOSYS)
  185. && (err != -EFAULT))
  186. return err;
  187. /*
  188. * High capacity cards should have this "magic" size
  189. * stored in their CSD.
  190. */
  191. if (card->csd.capacity == (4096 * 512)) {
  192. pr_err("%s: unable to read EXT_CSD "
  193. "on a possible high capacity card. "
  194. "Card will be ignored.\n",
  195. mmc_hostname(card->host));
  196. } else {
  197. pr_warning("%s: unable to read "
  198. "EXT_CSD, performance might "
  199. "suffer.\n",
  200. mmc_hostname(card->host));
  201. err = 0;
  202. }
  203. } else
  204. *new_ext_csd = ext_csd;
  205. return err;
  206. }
  207. static void mmc_select_card_type(struct mmc_card *card)
  208. {
  209. struct mmc_host *host = card->host;
  210. u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
  211. u32 caps = host->caps, caps2 = host->caps2;
  212. unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
  213. unsigned int avail_type = 0;
  214. if (caps & MMC_CAP_MMC_HIGHSPEED &&
  215. card_type & EXT_CSD_CARD_TYPE_HS_26) {
  216. hs_max_dtr = MMC_HIGH_26_MAX_DTR;
  217. avail_type |= EXT_CSD_CARD_TYPE_HS_26;
  218. }
  219. if (caps & MMC_CAP_MMC_HIGHSPEED &&
  220. card_type & EXT_CSD_CARD_TYPE_HS_52) {
  221. hs_max_dtr = MMC_HIGH_52_MAX_DTR;
  222. avail_type |= EXT_CSD_CARD_TYPE_HS_52;
  223. }
  224. if (caps & MMC_CAP_1_8V_DDR &&
  225. card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
  226. hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
  227. avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
  228. }
  229. if (caps & MMC_CAP_1_2V_DDR &&
  230. card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
  231. hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
  232. avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
  233. }
  234. if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
  235. card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
  236. hs200_max_dtr = MMC_HS200_MAX_DTR;
  237. avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
  238. }
  239. if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
  240. card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
  241. hs200_max_dtr = MMC_HS200_MAX_DTR;
  242. avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
  243. }
  244. card->ext_csd.hs_max_dtr = hs_max_dtr;
  245. card->ext_csd.hs200_max_dtr = hs200_max_dtr;
  246. card->mmc_avail_type = avail_type;
  247. }
  248. /*
  249. * Decode extended CSD.
  250. */
  251. static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
  252. {
  253. int err = 0, idx;
  254. unsigned int part_size;
  255. u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
  256. BUG_ON(!card);
  257. if (!ext_csd)
  258. return 0;
  259. /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
  260. card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
  261. if (card->csd.structure == 3) {
  262. if (card->ext_csd.raw_ext_csd_structure > 2) {
  263. pr_err("%s: unrecognised EXT_CSD structure "
  264. "version %d\n", mmc_hostname(card->host),
  265. card->ext_csd.raw_ext_csd_structure);
  266. err = -EINVAL;
  267. goto out;
  268. }
  269. }
  270. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  271. if (card->ext_csd.rev > 7) {
  272. pr_err("%s: unrecognised EXT_CSD revision %d\n",
  273. mmc_hostname(card->host), card->ext_csd.rev);
  274. err = -EINVAL;
  275. goto out;
  276. }
  277. card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
  278. card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
  279. card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
  280. card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
  281. if (card->ext_csd.rev >= 2) {
  282. card->ext_csd.sectors =
  283. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  284. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  285. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  286. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  287. /* Cards with density > 2GiB are sector addressed */
  288. if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
  289. mmc_card_set_blockaddr(card);
  290. }
  291. card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
  292. mmc_select_card_type(card);
  293. card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
  294. card->ext_csd.raw_erase_timeout_mult =
  295. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  296. card->ext_csd.raw_hc_erase_grp_size =
  297. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  298. if (card->ext_csd.rev >= 3) {
  299. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  300. card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
  301. /* EXT_CSD value is in units of 10ms, but we store in ms */
  302. card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
  303. /* Sleep / awake timeout in 100ns units */
  304. if (sa_shift > 0 && sa_shift <= 0x17)
  305. card->ext_csd.sa_timeout =
  306. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  307. card->ext_csd.erase_group_def =
  308. ext_csd[EXT_CSD_ERASE_GROUP_DEF];
  309. card->ext_csd.hc_erase_timeout = 300 *
  310. ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
  311. card->ext_csd.hc_erase_size =
  312. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
  313. card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
  314. /*
  315. * There are two boot regions of equal size, defined in
  316. * multiples of 128K.
  317. */
  318. if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
  319. for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
  320. part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  321. mmc_part_add(card, part_size,
  322. EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
  323. "boot%d", idx, true,
  324. MMC_BLK_DATA_AREA_BOOT);
  325. }
  326. }
  327. }
  328. card->ext_csd.raw_hc_erase_gap_size =
  329. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  330. card->ext_csd.raw_sec_trim_mult =
  331. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  332. card->ext_csd.raw_sec_erase_mult =
  333. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  334. card->ext_csd.raw_sec_feature_support =
  335. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  336. card->ext_csd.raw_trim_mult =
  337. ext_csd[EXT_CSD_TRIM_MULT];
  338. card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
  339. if (card->ext_csd.rev >= 4) {
  340. /*
  341. * Enhanced area feature support -- check whether the eMMC
  342. * card has the Enhanced area enabled. If so, export enhanced
  343. * area offset and size to user by adding sysfs interface.
  344. */
  345. if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
  346. (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
  347. hc_erase_grp_sz =
  348. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  349. hc_wp_grp_sz =
  350. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  351. card->ext_csd.enhanced_area_en = 1;
  352. /*
  353. * calculate the enhanced data area offset, in bytes
  354. */
  355. card->ext_csd.enhanced_area_offset =
  356. (ext_csd[139] << 24) + (ext_csd[138] << 16) +
  357. (ext_csd[137] << 8) + ext_csd[136];
  358. if (mmc_card_blockaddr(card))
  359. card->ext_csd.enhanced_area_offset <<= 9;
  360. /*
  361. * calculate the enhanced data area size, in kilobytes
  362. */
  363. card->ext_csd.enhanced_area_size =
  364. (ext_csd[142] << 16) + (ext_csd[141] << 8) +
  365. ext_csd[140];
  366. card->ext_csd.enhanced_area_size *=
  367. (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
  368. card->ext_csd.enhanced_area_size <<= 9;
  369. } else {
  370. /*
  371. * If the enhanced area is not enabled, disable these
  372. * device attributes.
  373. */
  374. card->ext_csd.enhanced_area_offset = -EINVAL;
  375. card->ext_csd.enhanced_area_size = -EINVAL;
  376. }
  377. /*
  378. * General purpose partition feature support --
  379. * If ext_csd has the size of general purpose partitions,
  380. * set size, part_cfg, partition name in mmc_part.
  381. */
  382. if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
  383. EXT_CSD_PART_SUPPORT_PART_EN) {
  384. if (card->ext_csd.enhanced_area_en != 1) {
  385. hc_erase_grp_sz =
  386. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  387. hc_wp_grp_sz =
  388. ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  389. card->ext_csd.enhanced_area_en = 1;
  390. }
  391. for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
  392. if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
  393. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
  394. !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
  395. continue;
  396. part_size =
  397. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
  398. << 16) +
  399. (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
  400. << 8) +
  401. ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
  402. part_size *= (size_t)(hc_erase_grp_sz *
  403. hc_wp_grp_sz);
  404. mmc_part_add(card, part_size << 19,
  405. EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
  406. "gp%d", idx, false,
  407. MMC_BLK_DATA_AREA_GP);
  408. }
  409. }
  410. card->ext_csd.sec_trim_mult =
  411. ext_csd[EXT_CSD_SEC_TRIM_MULT];
  412. card->ext_csd.sec_erase_mult =
  413. ext_csd[EXT_CSD_SEC_ERASE_MULT];
  414. card->ext_csd.sec_feature_support =
  415. ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  416. card->ext_csd.trim_timeout = 300 *
  417. ext_csd[EXT_CSD_TRIM_MULT];
  418. /*
  419. * Note that the call to mmc_part_add above defaults to read
  420. * only. If this default assumption is changed, the call must
  421. * take into account the value of boot_locked below.
  422. */
  423. card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
  424. card->ext_csd.boot_ro_lockable = true;
  425. /* Save power class values */
  426. card->ext_csd.raw_pwr_cl_52_195 =
  427. ext_csd[EXT_CSD_PWR_CL_52_195];
  428. card->ext_csd.raw_pwr_cl_26_195 =
  429. ext_csd[EXT_CSD_PWR_CL_26_195];
  430. card->ext_csd.raw_pwr_cl_52_360 =
  431. ext_csd[EXT_CSD_PWR_CL_52_360];
  432. card->ext_csd.raw_pwr_cl_26_360 =
  433. ext_csd[EXT_CSD_PWR_CL_26_360];
  434. card->ext_csd.raw_pwr_cl_200_195 =
  435. ext_csd[EXT_CSD_PWR_CL_200_195];
  436. card->ext_csd.raw_pwr_cl_200_360 =
  437. ext_csd[EXT_CSD_PWR_CL_200_360];
  438. card->ext_csd.raw_pwr_cl_ddr_52_195 =
  439. ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
  440. card->ext_csd.raw_pwr_cl_ddr_52_360 =
  441. ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
  442. }
  443. if (card->ext_csd.rev >= 5) {
  444. /* Adjust production date as per JEDEC JESD84-B451 */
  445. if (card->cid.year < 2010)
  446. card->cid.year += 16;
  447. /* check whether the eMMC card supports BKOPS */
  448. if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
  449. card->ext_csd.bkops = 1;
  450. card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
  451. card->ext_csd.raw_bkops_status =
  452. ext_csd[EXT_CSD_BKOPS_STATUS];
  453. if (!card->ext_csd.bkops_en)
  454. pr_info("%s: BKOPS_EN bit is not set\n",
  455. mmc_hostname(card->host));
  456. }
  457. /* check whether the eMMC card supports HPI */
  458. if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
  459. card->ext_csd.hpi = 1;
  460. if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
  461. card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
  462. else
  463. card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
  464. /*
  465. * Indicate the maximum timeout to close
  466. * a command interrupted by HPI
  467. */
  468. card->ext_csd.out_of_int_time =
  469. ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
  470. }
  471. card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
  472. card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
  473. /*
  474. * RPMB regions are defined in multiples of 128K.
  475. */
  476. card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
  477. if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
  478. mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
  479. EXT_CSD_PART_CONFIG_ACC_RPMB,
  480. "rpmb", 0, false,
  481. MMC_BLK_DATA_AREA_RPMB);
  482. }
  483. }
  484. card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
  485. if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
  486. card->erased_byte = 0xFF;
  487. else
  488. card->erased_byte = 0x0;
  489. /* eMMC v4.5 or later */
  490. if (card->ext_csd.rev >= 6) {
  491. card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
  492. card->ext_csd.generic_cmd6_time = 10 *
  493. ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
  494. card->ext_csd.power_off_longtime = 10 *
  495. ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
  496. card->ext_csd.cache_size =
  497. ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
  498. ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
  499. ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
  500. ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
  501. if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
  502. card->ext_csd.data_sector_size = 4096;
  503. else
  504. card->ext_csd.data_sector_size = 512;
  505. if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
  506. (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
  507. card->ext_csd.data_tag_unit_size =
  508. ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
  509. (card->ext_csd.data_sector_size);
  510. } else {
  511. card->ext_csd.data_tag_unit_size = 0;
  512. }
  513. card->ext_csd.max_packed_writes =
  514. ext_csd[EXT_CSD_MAX_PACKED_WRITES];
  515. card->ext_csd.max_packed_reads =
  516. ext_csd[EXT_CSD_MAX_PACKED_READS];
  517. } else {
  518. card->ext_csd.data_sector_size = 512;
  519. }
  520. out:
  521. return err;
  522. }
  523. static inline void mmc_free_ext_csd(u8 *ext_csd)
  524. {
  525. kfree(ext_csd);
  526. }
  527. static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
  528. {
  529. u8 *bw_ext_csd;
  530. int err;
  531. if (bus_width == MMC_BUS_WIDTH_1)
  532. return 0;
  533. err = mmc_get_ext_csd(card, &bw_ext_csd);
  534. if (err || bw_ext_csd == NULL) {
  535. err = -EINVAL;
  536. goto out;
  537. }
  538. /* only compare read only fields */
  539. err = !((card->ext_csd.raw_partition_support ==
  540. bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  541. (card->ext_csd.raw_erased_mem_count ==
  542. bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  543. (card->ext_csd.rev ==
  544. bw_ext_csd[EXT_CSD_REV]) &&
  545. (card->ext_csd.raw_ext_csd_structure ==
  546. bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  547. (card->ext_csd.raw_card_type ==
  548. bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  549. (card->ext_csd.raw_s_a_timeout ==
  550. bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  551. (card->ext_csd.raw_hc_erase_gap_size ==
  552. bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  553. (card->ext_csd.raw_erase_timeout_mult ==
  554. bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  555. (card->ext_csd.raw_hc_erase_grp_size ==
  556. bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  557. (card->ext_csd.raw_sec_trim_mult ==
  558. bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  559. (card->ext_csd.raw_sec_erase_mult ==
  560. bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  561. (card->ext_csd.raw_sec_feature_support ==
  562. bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  563. (card->ext_csd.raw_trim_mult ==
  564. bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  565. (card->ext_csd.raw_sectors[0] ==
  566. bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  567. (card->ext_csd.raw_sectors[1] ==
  568. bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  569. (card->ext_csd.raw_sectors[2] ==
  570. bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  571. (card->ext_csd.raw_sectors[3] ==
  572. bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
  573. (card->ext_csd.raw_pwr_cl_52_195 ==
  574. bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
  575. (card->ext_csd.raw_pwr_cl_26_195 ==
  576. bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
  577. (card->ext_csd.raw_pwr_cl_52_360 ==
  578. bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
  579. (card->ext_csd.raw_pwr_cl_26_360 ==
  580. bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
  581. (card->ext_csd.raw_pwr_cl_200_195 ==
  582. bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
  583. (card->ext_csd.raw_pwr_cl_200_360 ==
  584. bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
  585. (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
  586. bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
  587. (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
  588. bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]));
  589. if (err)
  590. err = -EINVAL;
  591. out:
  592. mmc_free_ext_csd(bw_ext_csd);
  593. return err;
  594. }
  595. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  596. card->raw_cid[2], card->raw_cid[3]);
  597. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  598. card->raw_csd[2], card->raw_csd[3]);
  599. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  600. MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
  601. MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
  602. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  603. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  604. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  605. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  606. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  607. MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
  608. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  609. MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
  610. card->ext_csd.enhanced_area_offset);
  611. MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
  612. MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
  613. MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
  614. static struct attribute *mmc_std_attrs[] = {
  615. &dev_attr_cid.attr,
  616. &dev_attr_csd.attr,
  617. &dev_attr_date.attr,
  618. &dev_attr_erase_size.attr,
  619. &dev_attr_preferred_erase_size.attr,
  620. &dev_attr_fwrev.attr,
  621. &dev_attr_hwrev.attr,
  622. &dev_attr_manfid.attr,
  623. &dev_attr_name.attr,
  624. &dev_attr_oemid.attr,
  625. &dev_attr_prv.attr,
  626. &dev_attr_serial.attr,
  627. &dev_attr_enhanced_area_offset.attr,
  628. &dev_attr_enhanced_area_size.attr,
  629. &dev_attr_raw_rpmb_size_mult.attr,
  630. &dev_attr_rel_sectors.attr,
  631. NULL,
  632. };
  633. ATTRIBUTE_GROUPS(mmc_std);
  634. static struct device_type mmc_type = {
  635. .groups = mmc_std_groups,
  636. };
  637. /*
  638. * Select the PowerClass for the current bus width
  639. * If power class is defined for 4/8 bit bus in the
  640. * extended CSD register, select it by executing the
  641. * mmc_switch command.
  642. */
  643. static int __mmc_select_powerclass(struct mmc_card *card,
  644. unsigned int bus_width)
  645. {
  646. struct mmc_host *host = card->host;
  647. struct mmc_ext_csd *ext_csd = &card->ext_csd;
  648. unsigned int pwrclass_val = 0;
  649. int err = 0;
  650. /* Power class selection is supported for versions >= 4.0 */
  651. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  652. return 0;
  653. /* Power class values are defined only for 4/8 bit bus */
  654. if (bus_width == EXT_CSD_BUS_WIDTH_1)
  655. return 0;
  656. switch (1 << host->ios.vdd) {
  657. case MMC_VDD_165_195:
  658. if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
  659. pwrclass_val = ext_csd->raw_pwr_cl_26_195;
  660. else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
  661. pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  662. ext_csd->raw_pwr_cl_52_195 :
  663. ext_csd->raw_pwr_cl_ddr_52_195;
  664. else if (host->ios.clock <= MMC_HS200_MAX_DTR)
  665. pwrclass_val = ext_csd->raw_pwr_cl_200_195;
  666. break;
  667. case MMC_VDD_27_28:
  668. case MMC_VDD_28_29:
  669. case MMC_VDD_29_30:
  670. case MMC_VDD_30_31:
  671. case MMC_VDD_31_32:
  672. case MMC_VDD_32_33:
  673. case MMC_VDD_33_34:
  674. case MMC_VDD_34_35:
  675. case MMC_VDD_35_36:
  676. if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
  677. pwrclass_val = ext_csd->raw_pwr_cl_26_360;
  678. else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
  679. pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
  680. ext_csd->raw_pwr_cl_52_360 :
  681. ext_csd->raw_pwr_cl_ddr_52_360;
  682. else if (host->ios.clock <= MMC_HS200_MAX_DTR)
  683. pwrclass_val = ext_csd->raw_pwr_cl_200_360;
  684. break;
  685. default:
  686. pr_warning("%s: Voltage range not supported "
  687. "for power class.\n", mmc_hostname(host));
  688. return -EINVAL;
  689. }
  690. if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
  691. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
  692. EXT_CSD_PWR_CL_8BIT_SHIFT;
  693. else
  694. pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
  695. EXT_CSD_PWR_CL_4BIT_SHIFT;
  696. /* If the power class is different from the default value */
  697. if (pwrclass_val > 0) {
  698. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  699. EXT_CSD_POWER_CLASS,
  700. pwrclass_val,
  701. card->ext_csd.generic_cmd6_time);
  702. }
  703. return err;
  704. }
  705. static int mmc_select_powerclass(struct mmc_card *card)
  706. {
  707. struct mmc_host *host = card->host;
  708. u32 bus_width, ext_csd_bits;
  709. int err, ddr;
  710. /* Power class selection is supported for versions >= 4.0 */
  711. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  712. return 0;
  713. bus_width = host->ios.bus_width;
  714. /* Power class values are defined only for 4/8 bit bus */
  715. if (bus_width == MMC_BUS_WIDTH_1)
  716. return 0;
  717. ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
  718. if (ddr)
  719. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  720. EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
  721. else
  722. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  723. EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
  724. err = __mmc_select_powerclass(card, ext_csd_bits);
  725. if (err)
  726. pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
  727. mmc_hostname(host), 1 << bus_width, ddr);
  728. return err;
  729. }
  730. /*
  731. * Set the bus speed for the selected speed mode.
  732. */
  733. static void mmc_set_bus_speed(struct mmc_card *card)
  734. {
  735. unsigned int max_dtr = (unsigned int)-1;
  736. if (mmc_card_hs200(card) && max_dtr > card->ext_csd.hs200_max_dtr)
  737. max_dtr = card->ext_csd.hs200_max_dtr;
  738. else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
  739. max_dtr = card->ext_csd.hs_max_dtr;
  740. else if (max_dtr > card->csd.max_dtr)
  741. max_dtr = card->csd.max_dtr;
  742. mmc_set_clock(card->host, max_dtr);
  743. }
  744. /*
  745. * Select the bus width amoung 4-bit and 8-bit(SDR).
  746. * If the bus width is changed successfully, return the selected width value.
  747. * Zero is returned instead of error value if the wide width is not supported.
  748. */
  749. static int mmc_select_bus_width(struct mmc_card *card)
  750. {
  751. static unsigned ext_csd_bits[] = {
  752. EXT_CSD_BUS_WIDTH_8,
  753. EXT_CSD_BUS_WIDTH_4,
  754. };
  755. static unsigned bus_widths[] = {
  756. MMC_BUS_WIDTH_8,
  757. MMC_BUS_WIDTH_4,
  758. };
  759. struct mmc_host *host = card->host;
  760. unsigned idx, bus_width = 0;
  761. int err = 0;
  762. if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
  763. !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
  764. return 0;
  765. idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
  766. /*
  767. * Unlike SD, MMC cards dont have a configuration register to notify
  768. * supported bus width. So bus test command should be run to identify
  769. * the supported bus width or compare the ext csd values of current
  770. * bus width and ext csd values of 1 bit mode read earlier.
  771. */
  772. for (; idx < ARRAY_SIZE(bus_widths); idx++) {
  773. /*
  774. * Host is capable of 8bit transfer, then switch
  775. * the device to work in 8bit transfer mode. If the
  776. * mmc switch command returns error then switch to
  777. * 4bit transfer mode. On success set the corresponding
  778. * bus width on the host.
  779. */
  780. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  781. EXT_CSD_BUS_WIDTH,
  782. ext_csd_bits[idx],
  783. card->ext_csd.generic_cmd6_time);
  784. if (err)
  785. continue;
  786. bus_width = bus_widths[idx];
  787. mmc_set_bus_width(host, bus_width);
  788. /*
  789. * If controller can't handle bus width test,
  790. * compare ext_csd previously read in 1 bit mode
  791. * against ext_csd at new bus width
  792. */
  793. if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
  794. err = mmc_compare_ext_csds(card, bus_width);
  795. else
  796. err = mmc_bus_test(card, bus_width);
  797. if (!err) {
  798. err = bus_width;
  799. break;
  800. } else {
  801. pr_warn("%s: switch to bus width %d failed\n",
  802. mmc_hostname(host), ext_csd_bits[idx]);
  803. }
  804. }
  805. return err;
  806. }
  807. /*
  808. * Switch to the high-speed mode
  809. */
  810. static int mmc_select_hs(struct mmc_card *card)
  811. {
  812. int err;
  813. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  814. EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
  815. card->ext_csd.generic_cmd6_time,
  816. true, true, true);
  817. if (!err)
  818. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  819. return err;
  820. }
  821. /*
  822. * Activate wide bus and DDR if supported.
  823. */
  824. static int mmc_select_hs_ddr(struct mmc_card *card)
  825. {
  826. struct mmc_host *host = card->host;
  827. u32 bus_width, ext_csd_bits;
  828. int err = 0;
  829. if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
  830. return 0;
  831. bus_width = host->ios.bus_width;
  832. if (bus_width == MMC_BUS_WIDTH_1)
  833. return 0;
  834. ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
  835. EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
  836. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  837. EXT_CSD_BUS_WIDTH,
  838. ext_csd_bits,
  839. card->ext_csd.generic_cmd6_time);
  840. if (err) {
  841. pr_warn("%s: switch to bus width %d ddr failed\n",
  842. mmc_hostname(host), 1 << bus_width);
  843. return err;
  844. }
  845. /*
  846. * eMMC cards can support 3.3V to 1.2V i/o (vccq)
  847. * signaling.
  848. *
  849. * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
  850. *
  851. * 1.8V vccq at 3.3V core voltage (vcc) is not required
  852. * in the JEDEC spec for DDR.
  853. *
  854. * Do not force change in vccq since we are obviously
  855. * working and no change to vccq is needed.
  856. *
  857. * WARNING: eMMC rules are NOT the same as SD DDR
  858. */
  859. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
  860. err = __mmc_set_signal_voltage(host,
  861. MMC_SIGNAL_VOLTAGE_120);
  862. if (err)
  863. return err;
  864. }
  865. mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
  866. return err;
  867. }
  868. /*
  869. * For device supporting HS200 mode, the following sequence
  870. * should be done before executing the tuning process.
  871. * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
  872. * 2. switch to HS200 mode
  873. * 3. set the clock to > 52Mhz and <=200MHz
  874. */
  875. static int mmc_select_hs200(struct mmc_card *card)
  876. {
  877. struct mmc_host *host = card->host;
  878. int err = -EINVAL;
  879. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
  880. err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
  881. if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
  882. err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
  883. /* If fails try again during next card power cycle */
  884. if (err)
  885. goto err;
  886. /*
  887. * Set the bus width(4 or 8) with host's support and
  888. * switch to HS200 mode if bus width is set successfully.
  889. */
  890. err = mmc_select_bus_width(card);
  891. if (!IS_ERR_VALUE(err)) {
  892. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  893. EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
  894. card->ext_csd.generic_cmd6_time,
  895. true, true, true);
  896. if (!err)
  897. mmc_set_timing(host, MMC_TIMING_MMC_HS200);
  898. }
  899. err:
  900. return err;
  901. }
  902. /*
  903. * Activate High Speed or HS200 mode if supported.
  904. */
  905. static int mmc_select_timing(struct mmc_card *card)
  906. {
  907. int err = 0;
  908. if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
  909. card->ext_csd.hs_max_dtr == 0))
  910. goto bus_speed;
  911. if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
  912. err = mmc_select_hs200(card);
  913. else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
  914. err = mmc_select_hs(card);
  915. if (err && err != -EBADMSG)
  916. return err;
  917. if (err) {
  918. pr_warn("%s: switch to %s failed\n",
  919. mmc_card_hs(card) ? "high-speed" :
  920. (mmc_card_hs200(card) ? "hs200" : ""),
  921. mmc_hostname(card->host));
  922. err = 0;
  923. }
  924. bus_speed:
  925. /*
  926. * Set the bus speed to the selected bus timing.
  927. * If timing is not selected, backward compatible is the default.
  928. */
  929. mmc_set_bus_speed(card);
  930. return err;
  931. }
  932. /*
  933. * Execute tuning sequence to seek the proper bus operating
  934. * conditions for HS200, which sends CMD21 to the device.
  935. */
  936. static int mmc_hs200_tuning(struct mmc_card *card)
  937. {
  938. struct mmc_host *host = card->host;
  939. int err = 0;
  940. if (host->ops->execute_tuning) {
  941. mmc_host_clk_hold(host);
  942. err = host->ops->execute_tuning(host,
  943. MMC_SEND_TUNING_BLOCK_HS200);
  944. mmc_host_clk_release(host);
  945. if (err)
  946. pr_warn("%s: tuning execution failed\n",
  947. mmc_hostname(host));
  948. }
  949. return err;
  950. }
  951. /*
  952. * Handle the detection and initialisation of a card.
  953. *
  954. * In the case of a resume, "oldcard" will contain the card
  955. * we're trying to reinitialise.
  956. */
  957. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  958. struct mmc_card *oldcard)
  959. {
  960. struct mmc_card *card;
  961. int err;
  962. u32 cid[4];
  963. u32 rocr;
  964. u8 *ext_csd = NULL;
  965. BUG_ON(!host);
  966. WARN_ON(!host->claimed);
  967. /* Set correct bus mode for MMC before attempting init */
  968. if (!mmc_host_is_spi(host))
  969. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  970. /*
  971. * Since we're changing the OCR value, we seem to
  972. * need to tell some cards to go back to the idle
  973. * state. We wait 1ms to give cards time to
  974. * respond.
  975. * mmc_go_idle is needed for eMMC that are asleep
  976. */
  977. mmc_go_idle(host);
  978. /* The extra bit indicates that we support high capacity */
  979. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  980. if (err)
  981. goto err;
  982. /*
  983. * For SPI, enable CRC as appropriate.
  984. */
  985. if (mmc_host_is_spi(host)) {
  986. err = mmc_spi_set_crc(host, use_spi_crc);
  987. if (err)
  988. goto err;
  989. }
  990. /*
  991. * Fetch CID from card.
  992. */
  993. if (mmc_host_is_spi(host))
  994. err = mmc_send_cid(host, cid);
  995. else
  996. err = mmc_all_send_cid(host, cid);
  997. if (err)
  998. goto err;
  999. if (oldcard) {
  1000. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  1001. err = -ENOENT;
  1002. goto err;
  1003. }
  1004. card = oldcard;
  1005. } else {
  1006. /*
  1007. * Allocate card structure.
  1008. */
  1009. card = mmc_alloc_card(host, &mmc_type);
  1010. if (IS_ERR(card)) {
  1011. err = PTR_ERR(card);
  1012. goto err;
  1013. }
  1014. card->ocr = ocr;
  1015. card->type = MMC_TYPE_MMC;
  1016. card->rca = 1;
  1017. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  1018. }
  1019. /*
  1020. * For native busses: set card RCA and quit open drain mode.
  1021. */
  1022. if (!mmc_host_is_spi(host)) {
  1023. err = mmc_set_relative_addr(card);
  1024. if (err)
  1025. goto free_card;
  1026. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  1027. }
  1028. if (!oldcard) {
  1029. /*
  1030. * Fetch CSD from card.
  1031. */
  1032. err = mmc_send_csd(card, card->raw_csd);
  1033. if (err)
  1034. goto free_card;
  1035. err = mmc_decode_csd(card);
  1036. if (err)
  1037. goto free_card;
  1038. err = mmc_decode_cid(card);
  1039. if (err)
  1040. goto free_card;
  1041. }
  1042. /*
  1043. * Select card, as all following commands rely on that.
  1044. */
  1045. if (!mmc_host_is_spi(host)) {
  1046. err = mmc_select_card(card);
  1047. if (err)
  1048. goto free_card;
  1049. }
  1050. if (!oldcard) {
  1051. /*
  1052. * Fetch and process extended CSD.
  1053. */
  1054. err = mmc_get_ext_csd(card, &ext_csd);
  1055. if (err)
  1056. goto free_card;
  1057. err = mmc_read_ext_csd(card, ext_csd);
  1058. if (err)
  1059. goto free_card;
  1060. /* If doing byte addressing, check if required to do sector
  1061. * addressing. Handle the case of <2GB cards needing sector
  1062. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  1063. * ocr register has bit 30 set for sector addressing.
  1064. */
  1065. if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
  1066. mmc_card_set_blockaddr(card);
  1067. /* Erase size depends on CSD and Extended CSD */
  1068. mmc_set_erase_size(card);
  1069. }
  1070. /*
  1071. * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
  1072. * bit. This bit will be lost every time after a reset or power off.
  1073. */
  1074. if (card->ext_csd.enhanced_area_en ||
  1075. (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
  1076. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1077. EXT_CSD_ERASE_GROUP_DEF, 1,
  1078. card->ext_csd.generic_cmd6_time);
  1079. if (err && err != -EBADMSG)
  1080. goto free_card;
  1081. if (err) {
  1082. err = 0;
  1083. /*
  1084. * Just disable enhanced area off & sz
  1085. * will try to enable ERASE_GROUP_DEF
  1086. * during next time reinit
  1087. */
  1088. card->ext_csd.enhanced_area_offset = -EINVAL;
  1089. card->ext_csd.enhanced_area_size = -EINVAL;
  1090. } else {
  1091. card->ext_csd.erase_group_def = 1;
  1092. /*
  1093. * enable ERASE_GRP_DEF successfully.
  1094. * This will affect the erase size, so
  1095. * here need to reset erase size
  1096. */
  1097. mmc_set_erase_size(card);
  1098. }
  1099. }
  1100. /*
  1101. * Ensure eMMC user default partition is enabled
  1102. */
  1103. if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
  1104. card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
  1105. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
  1106. card->ext_csd.part_config,
  1107. card->ext_csd.part_time);
  1108. if (err && err != -EBADMSG)
  1109. goto free_card;
  1110. }
  1111. /*
  1112. * Enable power_off_notification byte in the ext_csd register
  1113. */
  1114. if (card->ext_csd.rev >= 6) {
  1115. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1116. EXT_CSD_POWER_OFF_NOTIFICATION,
  1117. EXT_CSD_POWER_ON,
  1118. card->ext_csd.generic_cmd6_time);
  1119. if (err && err != -EBADMSG)
  1120. goto free_card;
  1121. /*
  1122. * The err can be -EBADMSG or 0,
  1123. * so check for success and update the flag
  1124. */
  1125. if (!err)
  1126. card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
  1127. }
  1128. /*
  1129. * Select timing interface
  1130. */
  1131. err = mmc_select_timing(card);
  1132. if (err)
  1133. goto free_card;
  1134. if (mmc_card_hs200(card)) {
  1135. err = mmc_hs200_tuning(card);
  1136. if (err)
  1137. goto err;
  1138. } else if (mmc_card_hs(card)) {
  1139. /* Select the desired bus width optionally */
  1140. err = mmc_select_bus_width(card);
  1141. if (!IS_ERR_VALUE(err)) {
  1142. err = mmc_select_hs_ddr(card);
  1143. if (err)
  1144. goto err;
  1145. }
  1146. }
  1147. /*
  1148. * Choose the power class with selected bus interface
  1149. */
  1150. mmc_select_powerclass(card);
  1151. /*
  1152. * Enable HPI feature (if supported)
  1153. */
  1154. if (card->ext_csd.hpi) {
  1155. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1156. EXT_CSD_HPI_MGMT, 1,
  1157. card->ext_csd.generic_cmd6_time);
  1158. if (err && err != -EBADMSG)
  1159. goto free_card;
  1160. if (err) {
  1161. pr_warning("%s: Enabling HPI failed\n",
  1162. mmc_hostname(card->host));
  1163. err = 0;
  1164. } else
  1165. card->ext_csd.hpi_en = 1;
  1166. }
  1167. /*
  1168. * If cache size is higher than 0, this indicates
  1169. * the existence of cache and it can be turned on.
  1170. */
  1171. if (card->ext_csd.cache_size > 0) {
  1172. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1173. EXT_CSD_CACHE_CTRL, 1,
  1174. card->ext_csd.generic_cmd6_time);
  1175. if (err && err != -EBADMSG)
  1176. goto free_card;
  1177. /*
  1178. * Only if no error, cache is turned on successfully.
  1179. */
  1180. if (err) {
  1181. pr_warning("%s: Cache is supported, "
  1182. "but failed to turn on (%d)\n",
  1183. mmc_hostname(card->host), err);
  1184. card->ext_csd.cache_ctrl = 0;
  1185. err = 0;
  1186. } else {
  1187. card->ext_csd.cache_ctrl = 1;
  1188. }
  1189. }
  1190. /*
  1191. * The mandatory minimum values are defined for packed command.
  1192. * read: 5, write: 3
  1193. */
  1194. if (card->ext_csd.max_packed_writes >= 3 &&
  1195. card->ext_csd.max_packed_reads >= 5 &&
  1196. host->caps2 & MMC_CAP2_PACKED_CMD) {
  1197. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1198. EXT_CSD_EXP_EVENTS_CTRL,
  1199. EXT_CSD_PACKED_EVENT_EN,
  1200. card->ext_csd.generic_cmd6_time);
  1201. if (err && err != -EBADMSG)
  1202. goto free_card;
  1203. if (err) {
  1204. pr_warn("%s: Enabling packed event failed\n",
  1205. mmc_hostname(card->host));
  1206. card->ext_csd.packed_event_en = 0;
  1207. err = 0;
  1208. } else {
  1209. card->ext_csd.packed_event_en = 1;
  1210. }
  1211. }
  1212. if (!oldcard)
  1213. host->card = card;
  1214. mmc_free_ext_csd(ext_csd);
  1215. return 0;
  1216. free_card:
  1217. if (!oldcard)
  1218. mmc_remove_card(card);
  1219. err:
  1220. mmc_free_ext_csd(ext_csd);
  1221. return err;
  1222. }
  1223. static int mmc_can_sleep(struct mmc_card *card)
  1224. {
  1225. return (card && card->ext_csd.rev >= 3);
  1226. }
  1227. static int mmc_sleep(struct mmc_host *host)
  1228. {
  1229. struct mmc_command cmd = {0};
  1230. struct mmc_card *card = host->card;
  1231. unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
  1232. int err;
  1233. err = mmc_deselect_cards(host);
  1234. if (err)
  1235. return err;
  1236. cmd.opcode = MMC_SLEEP_AWAKE;
  1237. cmd.arg = card->rca << 16;
  1238. cmd.arg |= 1 << 15;
  1239. /*
  1240. * If the max_busy_timeout of the host is specified, validate it against
  1241. * the sleep cmd timeout. A failure means we need to prevent the host
  1242. * from doing hw busy detection, which is done by converting to a R1
  1243. * response instead of a R1B.
  1244. */
  1245. if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
  1246. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1247. } else {
  1248. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  1249. cmd.busy_timeout = timeout_ms;
  1250. }
  1251. err = mmc_wait_for_cmd(host, &cmd, 0);
  1252. if (err)
  1253. return err;
  1254. /*
  1255. * If the host does not wait while the card signals busy, then we will
  1256. * will have to wait the sleep/awake timeout. Note, we cannot use the
  1257. * SEND_STATUS command to poll the status because that command (and most
  1258. * others) is invalid while the card sleeps.
  1259. */
  1260. if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
  1261. mmc_delay(timeout_ms);
  1262. return err;
  1263. }
  1264. static int mmc_can_poweroff_notify(const struct mmc_card *card)
  1265. {
  1266. return card &&
  1267. mmc_card_mmc(card) &&
  1268. (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
  1269. }
  1270. static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
  1271. {
  1272. unsigned int timeout = card->ext_csd.generic_cmd6_time;
  1273. int err;
  1274. /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
  1275. if (notify_type == EXT_CSD_POWER_OFF_LONG)
  1276. timeout = card->ext_csd.power_off_longtime;
  1277. err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  1278. EXT_CSD_POWER_OFF_NOTIFICATION,
  1279. notify_type, timeout, true, false, false);
  1280. if (err)
  1281. pr_err("%s: Power Off Notification timed out, %u\n",
  1282. mmc_hostname(card->host), timeout);
  1283. /* Disable the power off notification after the switch operation. */
  1284. card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
  1285. return err;
  1286. }
  1287. /*
  1288. * Host is being removed. Free up the current card.
  1289. */
  1290. static void mmc_remove(struct mmc_host *host)
  1291. {
  1292. BUG_ON(!host);
  1293. BUG_ON(!host->card);
  1294. mmc_remove_card(host->card);
  1295. host->card = NULL;
  1296. }
  1297. /*
  1298. * Card detection - card is alive.
  1299. */
  1300. static int mmc_alive(struct mmc_host *host)
  1301. {
  1302. return mmc_send_status(host->card, NULL);
  1303. }
  1304. /*
  1305. * Card detection callback from host.
  1306. */
  1307. static void mmc_detect(struct mmc_host *host)
  1308. {
  1309. int err;
  1310. BUG_ON(!host);
  1311. BUG_ON(!host->card);
  1312. mmc_get_card(host->card);
  1313. /*
  1314. * Just check if our card has been removed.
  1315. */
  1316. err = _mmc_detect_card_removed(host);
  1317. mmc_put_card(host->card);
  1318. if (err) {
  1319. mmc_remove(host);
  1320. mmc_claim_host(host);
  1321. mmc_detach_bus(host);
  1322. mmc_power_off(host);
  1323. mmc_release_host(host);
  1324. }
  1325. }
  1326. static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
  1327. {
  1328. int err = 0;
  1329. unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
  1330. EXT_CSD_POWER_OFF_LONG;
  1331. BUG_ON(!host);
  1332. BUG_ON(!host->card);
  1333. mmc_claim_host(host);
  1334. if (mmc_card_suspended(host->card))
  1335. goto out;
  1336. if (mmc_card_doing_bkops(host->card)) {
  1337. err = mmc_stop_bkops(host->card);
  1338. if (err)
  1339. goto out;
  1340. }
  1341. err = mmc_flush_cache(host->card);
  1342. if (err)
  1343. goto out;
  1344. if (mmc_can_poweroff_notify(host->card) &&
  1345. ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
  1346. err = mmc_poweroff_notify(host->card, notify_type);
  1347. else if (mmc_can_sleep(host->card))
  1348. err = mmc_sleep(host);
  1349. else if (!mmc_host_is_spi(host))
  1350. err = mmc_deselect_cards(host);
  1351. if (!err) {
  1352. mmc_power_off(host);
  1353. mmc_card_set_suspended(host->card);
  1354. }
  1355. out:
  1356. mmc_release_host(host);
  1357. return err;
  1358. }
  1359. /*
  1360. * Suspend callback
  1361. */
  1362. static int mmc_suspend(struct mmc_host *host)
  1363. {
  1364. int err;
  1365. err = _mmc_suspend(host, true);
  1366. if (!err) {
  1367. pm_runtime_disable(&host->card->dev);
  1368. pm_runtime_set_suspended(&host->card->dev);
  1369. }
  1370. return err;
  1371. }
  1372. /*
  1373. * This function tries to determine if the same card is still present
  1374. * and, if so, restore all state to it.
  1375. */
  1376. static int _mmc_resume(struct mmc_host *host)
  1377. {
  1378. int err = 0;
  1379. BUG_ON(!host);
  1380. BUG_ON(!host->card);
  1381. mmc_claim_host(host);
  1382. if (!mmc_card_suspended(host->card))
  1383. goto out;
  1384. mmc_power_up(host, host->card->ocr);
  1385. err = mmc_init_card(host, host->card->ocr, host->card);
  1386. mmc_card_clr_suspended(host->card);
  1387. out:
  1388. mmc_release_host(host);
  1389. return err;
  1390. }
  1391. /*
  1392. * Shutdown callback
  1393. */
  1394. static int mmc_shutdown(struct mmc_host *host)
  1395. {
  1396. int err = 0;
  1397. /*
  1398. * In a specific case for poweroff notify, we need to resume the card
  1399. * before we can shutdown it properly.
  1400. */
  1401. if (mmc_can_poweroff_notify(host->card) &&
  1402. !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
  1403. err = _mmc_resume(host);
  1404. if (!err)
  1405. err = _mmc_suspend(host, false);
  1406. return err;
  1407. }
  1408. /*
  1409. * Callback for resume.
  1410. */
  1411. static int mmc_resume(struct mmc_host *host)
  1412. {
  1413. int err = 0;
  1414. if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
  1415. err = _mmc_resume(host);
  1416. pm_runtime_set_active(&host->card->dev);
  1417. pm_runtime_mark_last_busy(&host->card->dev);
  1418. }
  1419. pm_runtime_enable(&host->card->dev);
  1420. return err;
  1421. }
  1422. /*
  1423. * Callback for runtime_suspend.
  1424. */
  1425. static int mmc_runtime_suspend(struct mmc_host *host)
  1426. {
  1427. int err;
  1428. if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
  1429. return 0;
  1430. err = _mmc_suspend(host, true);
  1431. if (err)
  1432. pr_err("%s: error %d doing aggessive suspend\n",
  1433. mmc_hostname(host), err);
  1434. return err;
  1435. }
  1436. /*
  1437. * Callback for runtime_resume.
  1438. */
  1439. static int mmc_runtime_resume(struct mmc_host *host)
  1440. {
  1441. int err;
  1442. if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
  1443. return 0;
  1444. err = _mmc_resume(host);
  1445. if (err)
  1446. pr_err("%s: error %d doing aggessive resume\n",
  1447. mmc_hostname(host), err);
  1448. return 0;
  1449. }
  1450. static int mmc_power_restore(struct mmc_host *host)
  1451. {
  1452. int ret;
  1453. mmc_claim_host(host);
  1454. ret = mmc_init_card(host, host->card->ocr, host->card);
  1455. mmc_release_host(host);
  1456. return ret;
  1457. }
  1458. static const struct mmc_bus_ops mmc_ops = {
  1459. .remove = mmc_remove,
  1460. .detect = mmc_detect,
  1461. .suspend = mmc_suspend,
  1462. .resume = mmc_resume,
  1463. .runtime_suspend = mmc_runtime_suspend,
  1464. .runtime_resume = mmc_runtime_resume,
  1465. .power_restore = mmc_power_restore,
  1466. .alive = mmc_alive,
  1467. .shutdown = mmc_shutdown,
  1468. };
  1469. /*
  1470. * Starting point for MMC card init.
  1471. */
  1472. int mmc_attach_mmc(struct mmc_host *host)
  1473. {
  1474. int err;
  1475. u32 ocr, rocr;
  1476. BUG_ON(!host);
  1477. WARN_ON(!host->claimed);
  1478. /* Set correct bus mode for MMC before attempting attach */
  1479. if (!mmc_host_is_spi(host))
  1480. mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1481. err = mmc_send_op_cond(host, 0, &ocr);
  1482. if (err)
  1483. return err;
  1484. mmc_attach_bus(host, &mmc_ops);
  1485. if (host->ocr_avail_mmc)
  1486. host->ocr_avail = host->ocr_avail_mmc;
  1487. /*
  1488. * We need to get OCR a different way for SPI.
  1489. */
  1490. if (mmc_host_is_spi(host)) {
  1491. err = mmc_spi_read_ocr(host, 1, &ocr);
  1492. if (err)
  1493. goto err;
  1494. }
  1495. rocr = mmc_select_voltage(host, ocr);
  1496. /*
  1497. * Can we support the voltage of the card?
  1498. */
  1499. if (!rocr) {
  1500. err = -EINVAL;
  1501. goto err;
  1502. }
  1503. /*
  1504. * Detect and init the card.
  1505. */
  1506. err = mmc_init_card(host, rocr, NULL);
  1507. if (err)
  1508. goto err;
  1509. mmc_release_host(host);
  1510. err = mmc_add_card(host->card);
  1511. mmc_claim_host(host);
  1512. if (err)
  1513. goto remove_card;
  1514. return 0;
  1515. remove_card:
  1516. mmc_release_host(host);
  1517. mmc_remove_card(host->card);
  1518. mmc_claim_host(host);
  1519. host->card = NULL;
  1520. err:
  1521. mmc_detach_bus(host);
  1522. pr_err("%s: error %d whilst initialising MMC card\n",
  1523. mmc_hostname(host), err);
  1524. return err;
  1525. }