mmc.c 52 KB

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