mmc.c 55 KB

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