mmc.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  1. /*
  2. * Copyright 2008, Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based vaguely on the Linux code
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <config.h>
  10. #include <common.h>
  11. #include <command.h>
  12. #include <dm.h>
  13. #include <dm/device-internal.h>
  14. #include <errno.h>
  15. #include <mmc.h>
  16. #include <part.h>
  17. #include <power/regulator.h>
  18. #include <malloc.h>
  19. #include <memalign.h>
  20. #include <linux/list.h>
  21. #include <div64.h>
  22. #include "mmc_private.h"
  23. static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage);
  24. static int mmc_power_cycle(struct mmc *mmc);
  25. static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps);
  26. #if CONFIG_IS_ENABLED(MMC_TINY)
  27. static struct mmc mmc_static;
  28. struct mmc *find_mmc_device(int dev_num)
  29. {
  30. return &mmc_static;
  31. }
  32. void mmc_do_preinit(void)
  33. {
  34. struct mmc *m = &mmc_static;
  35. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  36. mmc_set_preinit(m, 1);
  37. #endif
  38. if (m->preinit)
  39. mmc_start_init(m);
  40. }
  41. struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
  42. {
  43. return &mmc->block_dev;
  44. }
  45. #endif
  46. #if !CONFIG_IS_ENABLED(DM_MMC)
  47. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  48. static int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
  49. {
  50. return -ENOSYS;
  51. }
  52. #endif
  53. __weak int board_mmc_getwp(struct mmc *mmc)
  54. {
  55. return -1;
  56. }
  57. int mmc_getwp(struct mmc *mmc)
  58. {
  59. int wp;
  60. wp = board_mmc_getwp(mmc);
  61. if (wp < 0) {
  62. if (mmc->cfg->ops->getwp)
  63. wp = mmc->cfg->ops->getwp(mmc);
  64. else
  65. wp = 0;
  66. }
  67. return wp;
  68. }
  69. __weak int board_mmc_getcd(struct mmc *mmc)
  70. {
  71. return -1;
  72. }
  73. #endif
  74. #ifdef CONFIG_MMC_TRACE
  75. void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
  76. {
  77. printf("CMD_SEND:%d\n", cmd->cmdidx);
  78. printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
  79. }
  80. void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
  81. {
  82. int i;
  83. u8 *ptr;
  84. if (ret) {
  85. printf("\t\tRET\t\t\t %d\n", ret);
  86. } else {
  87. switch (cmd->resp_type) {
  88. case MMC_RSP_NONE:
  89. printf("\t\tMMC_RSP_NONE\n");
  90. break;
  91. case MMC_RSP_R1:
  92. printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
  93. cmd->response[0]);
  94. break;
  95. case MMC_RSP_R1b:
  96. printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
  97. cmd->response[0]);
  98. break;
  99. case MMC_RSP_R2:
  100. printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
  101. cmd->response[0]);
  102. printf("\t\t \t\t 0x%08X \n",
  103. cmd->response[1]);
  104. printf("\t\t \t\t 0x%08X \n",
  105. cmd->response[2]);
  106. printf("\t\t \t\t 0x%08X \n",
  107. cmd->response[3]);
  108. printf("\n");
  109. printf("\t\t\t\t\tDUMPING DATA\n");
  110. for (i = 0; i < 4; i++) {
  111. int j;
  112. printf("\t\t\t\t\t%03d - ", i*4);
  113. ptr = (u8 *)&cmd->response[i];
  114. ptr += 3;
  115. for (j = 0; j < 4; j++)
  116. printf("%02X ", *ptr--);
  117. printf("\n");
  118. }
  119. break;
  120. case MMC_RSP_R3:
  121. printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
  122. cmd->response[0]);
  123. break;
  124. default:
  125. printf("\t\tERROR MMC rsp not supported\n");
  126. break;
  127. }
  128. }
  129. }
  130. void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
  131. {
  132. int status;
  133. status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
  134. printf("CURR STATE:%d\n", status);
  135. }
  136. #endif
  137. #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
  138. const char *mmc_mode_name(enum bus_mode mode)
  139. {
  140. static const char *const names[] = {
  141. [MMC_LEGACY] = "MMC legacy",
  142. [SD_LEGACY] = "SD Legacy",
  143. [MMC_HS] = "MMC High Speed (26MHz)",
  144. [SD_HS] = "SD High Speed (50MHz)",
  145. [UHS_SDR12] = "UHS SDR12 (25MHz)",
  146. [UHS_SDR25] = "UHS SDR25 (50MHz)",
  147. [UHS_SDR50] = "UHS SDR50 (100MHz)",
  148. [UHS_SDR104] = "UHS SDR104 (208MHz)",
  149. [UHS_DDR50] = "UHS DDR50 (50MHz)",
  150. [MMC_HS_52] = "MMC High Speed (52MHz)",
  151. [MMC_DDR_52] = "MMC DDR52 (52MHz)",
  152. [MMC_HS_200] = "HS200 (200MHz)",
  153. };
  154. if (mode >= MMC_MODES_END)
  155. return "Unknown mode";
  156. else
  157. return names[mode];
  158. }
  159. #endif
  160. static uint mmc_mode2freq(struct mmc *mmc, enum bus_mode mode)
  161. {
  162. static const int freqs[] = {
  163. [SD_LEGACY] = 25000000,
  164. [MMC_HS] = 26000000,
  165. [SD_HS] = 50000000,
  166. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  167. [UHS_SDR12] = 25000000,
  168. [UHS_SDR25] = 50000000,
  169. [UHS_SDR50] = 100000000,
  170. [UHS_DDR50] = 50000000,
  171. #ifdef MMC_SUPPORTS_TUNING
  172. [UHS_SDR104] = 208000000,
  173. #endif
  174. #endif
  175. [MMC_HS_52] = 52000000,
  176. [MMC_DDR_52] = 52000000,
  177. #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
  178. [MMC_HS_200] = 200000000,
  179. #endif
  180. };
  181. if (mode == MMC_LEGACY)
  182. return mmc->legacy_speed;
  183. else if (mode >= MMC_MODES_END)
  184. return 0;
  185. else
  186. return freqs[mode];
  187. }
  188. static int mmc_select_mode(struct mmc *mmc, enum bus_mode mode)
  189. {
  190. mmc->selected_mode = mode;
  191. mmc->tran_speed = mmc_mode2freq(mmc, mode);
  192. mmc->ddr_mode = mmc_is_mode_ddr(mode);
  193. debug("selecting mode %s (freq : %d MHz)\n", mmc_mode_name(mode),
  194. mmc->tran_speed / 1000000);
  195. return 0;
  196. }
  197. #if !CONFIG_IS_ENABLED(DM_MMC)
  198. int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  199. {
  200. int ret;
  201. mmmc_trace_before_send(mmc, cmd);
  202. ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
  203. mmmc_trace_after_send(mmc, cmd, ret);
  204. return ret;
  205. }
  206. #endif
  207. int mmc_send_status(struct mmc *mmc, int timeout)
  208. {
  209. struct mmc_cmd cmd;
  210. int err, retries = 5;
  211. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  212. cmd.resp_type = MMC_RSP_R1;
  213. if (!mmc_host_is_spi(mmc))
  214. cmd.cmdarg = mmc->rca << 16;
  215. while (1) {
  216. err = mmc_send_cmd(mmc, &cmd, NULL);
  217. if (!err) {
  218. if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
  219. (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
  220. MMC_STATE_PRG)
  221. break;
  222. if (cmd.response[0] & MMC_STATUS_MASK) {
  223. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  224. pr_err("Status Error: 0x%08X\n",
  225. cmd.response[0]);
  226. #endif
  227. return -ECOMM;
  228. }
  229. } else if (--retries < 0)
  230. return err;
  231. if (timeout-- <= 0)
  232. break;
  233. udelay(1000);
  234. }
  235. mmc_trace_state(mmc, &cmd);
  236. if (timeout <= 0) {
  237. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  238. pr_err("Timeout waiting card ready\n");
  239. #endif
  240. return -ETIMEDOUT;
  241. }
  242. return 0;
  243. }
  244. int mmc_set_blocklen(struct mmc *mmc, int len)
  245. {
  246. struct mmc_cmd cmd;
  247. int err;
  248. if (mmc->ddr_mode)
  249. return 0;
  250. cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
  251. cmd.resp_type = MMC_RSP_R1;
  252. cmd.cmdarg = len;
  253. err = mmc_send_cmd(mmc, &cmd, NULL);
  254. #ifdef CONFIG_MMC_QUIRKS
  255. if (err && (mmc->quirks & MMC_QUIRK_RETRY_SET_BLOCKLEN)) {
  256. int retries = 4;
  257. /*
  258. * It has been seen that SET_BLOCKLEN may fail on the first
  259. * attempt, let's try a few more time
  260. */
  261. do {
  262. err = mmc_send_cmd(mmc, &cmd, NULL);
  263. if (!err)
  264. break;
  265. } while (retries--);
  266. }
  267. #endif
  268. return err;
  269. }
  270. #ifdef MMC_SUPPORTS_TUNING
  271. static const u8 tuning_blk_pattern_4bit[] = {
  272. 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc,
  273. 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
  274. 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
  275. 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
  276. 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
  277. 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
  278. 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
  279. 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
  280. };
  281. static const u8 tuning_blk_pattern_8bit[] = {
  282. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  283. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  284. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  285. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  286. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  287. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  288. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  289. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  290. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  291. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  292. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  293. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  294. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  295. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  296. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  297. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  298. };
  299. int mmc_send_tuning(struct mmc *mmc, u32 opcode, int *cmd_error)
  300. {
  301. struct mmc_cmd cmd;
  302. struct mmc_data data;
  303. const u8 *tuning_block_pattern;
  304. int size, err;
  305. if (mmc->bus_width == 8) {
  306. tuning_block_pattern = tuning_blk_pattern_8bit;
  307. size = sizeof(tuning_blk_pattern_8bit);
  308. } else if (mmc->bus_width == 4) {
  309. tuning_block_pattern = tuning_blk_pattern_4bit;
  310. size = sizeof(tuning_blk_pattern_4bit);
  311. } else {
  312. return -EINVAL;
  313. }
  314. ALLOC_CACHE_ALIGN_BUFFER(u8, data_buf, size);
  315. cmd.cmdidx = opcode;
  316. cmd.cmdarg = 0;
  317. cmd.resp_type = MMC_RSP_R1;
  318. data.dest = (void *)data_buf;
  319. data.blocks = 1;
  320. data.blocksize = size;
  321. data.flags = MMC_DATA_READ;
  322. err = mmc_send_cmd(mmc, &cmd, &data);
  323. if (err)
  324. return err;
  325. if (memcmp(data_buf, tuning_block_pattern, size))
  326. return -EIO;
  327. return 0;
  328. }
  329. #endif
  330. static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
  331. lbaint_t blkcnt)
  332. {
  333. struct mmc_cmd cmd;
  334. struct mmc_data data;
  335. if (blkcnt > 1)
  336. cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
  337. else
  338. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  339. if (mmc->high_capacity)
  340. cmd.cmdarg = start;
  341. else
  342. cmd.cmdarg = start * mmc->read_bl_len;
  343. cmd.resp_type = MMC_RSP_R1;
  344. data.dest = dst;
  345. data.blocks = blkcnt;
  346. data.blocksize = mmc->read_bl_len;
  347. data.flags = MMC_DATA_READ;
  348. if (mmc_send_cmd(mmc, &cmd, &data))
  349. return 0;
  350. if (blkcnt > 1) {
  351. cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
  352. cmd.cmdarg = 0;
  353. cmd.resp_type = MMC_RSP_R1b;
  354. if (mmc_send_cmd(mmc, &cmd, NULL)) {
  355. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  356. pr_err("mmc fail to send stop cmd\n");
  357. #endif
  358. return 0;
  359. }
  360. }
  361. return blkcnt;
  362. }
  363. #if CONFIG_IS_ENABLED(BLK)
  364. ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
  365. #else
  366. ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
  367. void *dst)
  368. #endif
  369. {
  370. #if CONFIG_IS_ENABLED(BLK)
  371. struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
  372. #endif
  373. int dev_num = block_dev->devnum;
  374. int err;
  375. lbaint_t cur, blocks_todo = blkcnt;
  376. if (blkcnt == 0)
  377. return 0;
  378. struct mmc *mmc = find_mmc_device(dev_num);
  379. if (!mmc)
  380. return 0;
  381. if (CONFIG_IS_ENABLED(MMC_TINY))
  382. err = mmc_switch_part(mmc, block_dev->hwpart);
  383. else
  384. err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
  385. if (err < 0)
  386. return 0;
  387. if ((start + blkcnt) > block_dev->lba) {
  388. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  389. pr_err("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
  390. start + blkcnt, block_dev->lba);
  391. #endif
  392. return 0;
  393. }
  394. if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
  395. debug("%s: Failed to set blocklen\n", __func__);
  396. return 0;
  397. }
  398. do {
  399. cur = (blocks_todo > mmc->cfg->b_max) ?
  400. mmc->cfg->b_max : blocks_todo;
  401. if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
  402. debug("%s: Failed to read blocks\n", __func__);
  403. return 0;
  404. }
  405. blocks_todo -= cur;
  406. start += cur;
  407. dst += cur * mmc->read_bl_len;
  408. } while (blocks_todo > 0);
  409. return blkcnt;
  410. }
  411. static int mmc_go_idle(struct mmc *mmc)
  412. {
  413. struct mmc_cmd cmd;
  414. int err;
  415. udelay(1000);
  416. cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
  417. cmd.cmdarg = 0;
  418. cmd.resp_type = MMC_RSP_NONE;
  419. err = mmc_send_cmd(mmc, &cmd, NULL);
  420. if (err)
  421. return err;
  422. udelay(2000);
  423. return 0;
  424. }
  425. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  426. static int mmc_switch_voltage(struct mmc *mmc, int signal_voltage)
  427. {
  428. struct mmc_cmd cmd;
  429. int err = 0;
  430. /*
  431. * Send CMD11 only if the request is to switch the card to
  432. * 1.8V signalling.
  433. */
  434. if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  435. return mmc_set_signal_voltage(mmc, signal_voltage);
  436. cmd.cmdidx = SD_CMD_SWITCH_UHS18V;
  437. cmd.cmdarg = 0;
  438. cmd.resp_type = MMC_RSP_R1;
  439. err = mmc_send_cmd(mmc, &cmd, NULL);
  440. if (err)
  441. return err;
  442. if (!mmc_host_is_spi(mmc) && (cmd.response[0] & MMC_STATUS_ERROR))
  443. return -EIO;
  444. /*
  445. * The card should drive cmd and dat[0:3] low immediately
  446. * after the response of cmd11, but wait 100 us to be sure
  447. */
  448. err = mmc_wait_dat0(mmc, 0, 100);
  449. if (err == -ENOSYS)
  450. udelay(100);
  451. else if (err)
  452. return -ETIMEDOUT;
  453. /*
  454. * During a signal voltage level switch, the clock must be gated
  455. * for 5 ms according to the SD spec
  456. */
  457. mmc_set_clock(mmc, mmc->clock, true);
  458. err = mmc_set_signal_voltage(mmc, signal_voltage);
  459. if (err)
  460. return err;
  461. /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
  462. mdelay(10);
  463. mmc_set_clock(mmc, mmc->clock, false);
  464. /*
  465. * Failure to switch is indicated by the card holding
  466. * dat[0:3] low. Wait for at least 1 ms according to spec
  467. */
  468. err = mmc_wait_dat0(mmc, 1, 1000);
  469. if (err == -ENOSYS)
  470. udelay(1000);
  471. else if (err)
  472. return -ETIMEDOUT;
  473. return 0;
  474. }
  475. #endif
  476. static int sd_send_op_cond(struct mmc *mmc, bool uhs_en)
  477. {
  478. int timeout = 1000;
  479. int err;
  480. struct mmc_cmd cmd;
  481. while (1) {
  482. cmd.cmdidx = MMC_CMD_APP_CMD;
  483. cmd.resp_type = MMC_RSP_R1;
  484. cmd.cmdarg = 0;
  485. err = mmc_send_cmd(mmc, &cmd, NULL);
  486. if (err)
  487. return err;
  488. cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
  489. cmd.resp_type = MMC_RSP_R3;
  490. /*
  491. * Most cards do not answer if some reserved bits
  492. * in the ocr are set. However, Some controller
  493. * can set bit 7 (reserved for low voltages), but
  494. * how to manage low voltages SD card is not yet
  495. * specified.
  496. */
  497. cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
  498. (mmc->cfg->voltages & 0xff8000);
  499. if (mmc->version == SD_VERSION_2)
  500. cmd.cmdarg |= OCR_HCS;
  501. if (uhs_en)
  502. cmd.cmdarg |= OCR_S18R;
  503. err = mmc_send_cmd(mmc, &cmd, NULL);
  504. if (err)
  505. return err;
  506. if (cmd.response[0] & OCR_BUSY)
  507. break;
  508. if (timeout-- <= 0)
  509. return -EOPNOTSUPP;
  510. udelay(1000);
  511. }
  512. if (mmc->version != SD_VERSION_2)
  513. mmc->version = SD_VERSION_1_0;
  514. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  515. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  516. cmd.resp_type = MMC_RSP_R3;
  517. cmd.cmdarg = 0;
  518. err = mmc_send_cmd(mmc, &cmd, NULL);
  519. if (err)
  520. return err;
  521. }
  522. mmc->ocr = cmd.response[0];
  523. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  524. if (uhs_en && !(mmc_host_is_spi(mmc)) && (cmd.response[0] & 0x41000000)
  525. == 0x41000000) {
  526. err = mmc_switch_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
  527. if (err)
  528. return err;
  529. }
  530. #endif
  531. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  532. mmc->rca = 0;
  533. return 0;
  534. }
  535. static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
  536. {
  537. struct mmc_cmd cmd;
  538. int err;
  539. cmd.cmdidx = MMC_CMD_SEND_OP_COND;
  540. cmd.resp_type = MMC_RSP_R3;
  541. cmd.cmdarg = 0;
  542. if (use_arg && !mmc_host_is_spi(mmc))
  543. cmd.cmdarg = OCR_HCS |
  544. (mmc->cfg->voltages &
  545. (mmc->ocr & OCR_VOLTAGE_MASK)) |
  546. (mmc->ocr & OCR_ACCESS_MODE);
  547. err = mmc_send_cmd(mmc, &cmd, NULL);
  548. if (err)
  549. return err;
  550. mmc->ocr = cmd.response[0];
  551. return 0;
  552. }
  553. static int mmc_send_op_cond(struct mmc *mmc)
  554. {
  555. int err, i;
  556. /* Some cards seem to need this */
  557. mmc_go_idle(mmc);
  558. /* Asking to the card its capabilities */
  559. for (i = 0; i < 2; i++) {
  560. err = mmc_send_op_cond_iter(mmc, i != 0);
  561. if (err)
  562. return err;
  563. /* exit if not busy (flag seems to be inverted) */
  564. if (mmc->ocr & OCR_BUSY)
  565. break;
  566. }
  567. mmc->op_cond_pending = 1;
  568. return 0;
  569. }
  570. static int mmc_complete_op_cond(struct mmc *mmc)
  571. {
  572. struct mmc_cmd cmd;
  573. int timeout = 1000;
  574. uint start;
  575. int err;
  576. mmc->op_cond_pending = 0;
  577. if (!(mmc->ocr & OCR_BUSY)) {
  578. /* Some cards seem to need this */
  579. mmc_go_idle(mmc);
  580. start = get_timer(0);
  581. while (1) {
  582. err = mmc_send_op_cond_iter(mmc, 1);
  583. if (err)
  584. return err;
  585. if (mmc->ocr & OCR_BUSY)
  586. break;
  587. if (get_timer(start) > timeout)
  588. return -EOPNOTSUPP;
  589. udelay(100);
  590. }
  591. }
  592. if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
  593. cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
  594. cmd.resp_type = MMC_RSP_R3;
  595. cmd.cmdarg = 0;
  596. err = mmc_send_cmd(mmc, &cmd, NULL);
  597. if (err)
  598. return err;
  599. mmc->ocr = cmd.response[0];
  600. }
  601. mmc->version = MMC_VERSION_UNKNOWN;
  602. mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
  603. mmc->rca = 1;
  604. return 0;
  605. }
  606. static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
  607. {
  608. struct mmc_cmd cmd;
  609. struct mmc_data data;
  610. int err;
  611. /* Get the Card Status Register */
  612. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  613. cmd.resp_type = MMC_RSP_R1;
  614. cmd.cmdarg = 0;
  615. data.dest = (char *)ext_csd;
  616. data.blocks = 1;
  617. data.blocksize = MMC_MAX_BLOCK_LEN;
  618. data.flags = MMC_DATA_READ;
  619. err = mmc_send_cmd(mmc, &cmd, &data);
  620. return err;
  621. }
  622. int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
  623. {
  624. struct mmc_cmd cmd;
  625. int timeout = 1000;
  626. int retries = 3;
  627. int ret;
  628. cmd.cmdidx = MMC_CMD_SWITCH;
  629. cmd.resp_type = MMC_RSP_R1b;
  630. cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  631. (index << 16) |
  632. (value << 8);
  633. while (retries > 0) {
  634. ret = mmc_send_cmd(mmc, &cmd, NULL);
  635. /* Waiting for the ready status */
  636. if (!ret) {
  637. ret = mmc_send_status(mmc, timeout);
  638. return ret;
  639. }
  640. retries--;
  641. }
  642. return ret;
  643. }
  644. static int mmc_set_card_speed(struct mmc *mmc, enum bus_mode mode)
  645. {
  646. int err;
  647. int speed_bits;
  648. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  649. switch (mode) {
  650. case MMC_HS:
  651. case MMC_HS_52:
  652. case MMC_DDR_52:
  653. speed_bits = EXT_CSD_TIMING_HS;
  654. break;
  655. #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
  656. case MMC_HS_200:
  657. speed_bits = EXT_CSD_TIMING_HS200;
  658. break;
  659. #endif
  660. case MMC_LEGACY:
  661. speed_bits = EXT_CSD_TIMING_LEGACY;
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
  667. speed_bits);
  668. if (err)
  669. return err;
  670. if ((mode == MMC_HS) || (mode == MMC_HS_52)) {
  671. /* Now check to see that it worked */
  672. err = mmc_send_ext_csd(mmc, test_csd);
  673. if (err)
  674. return err;
  675. /* No high-speed support */
  676. if (!test_csd[EXT_CSD_HS_TIMING])
  677. return -ENOTSUPP;
  678. }
  679. return 0;
  680. }
  681. static int mmc_get_capabilities(struct mmc *mmc)
  682. {
  683. u8 *ext_csd = mmc->ext_csd;
  684. char cardtype;
  685. mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(MMC_LEGACY);
  686. if (mmc_host_is_spi(mmc))
  687. return 0;
  688. /* Only version 4 supports high-speed */
  689. if (mmc->version < MMC_VERSION_4)
  690. return 0;
  691. if (!ext_csd) {
  692. pr_err("No ext_csd found!\n"); /* this should enver happen */
  693. return -ENOTSUPP;
  694. }
  695. mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
  696. cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0x3f;
  697. mmc->cardtype = cardtype;
  698. #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
  699. if (cardtype & (EXT_CSD_CARD_TYPE_HS200_1_2V |
  700. EXT_CSD_CARD_TYPE_HS200_1_8V)) {
  701. mmc->card_caps |= MMC_MODE_HS200;
  702. }
  703. #endif
  704. if (cardtype & EXT_CSD_CARD_TYPE_52) {
  705. if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
  706. mmc->card_caps |= MMC_MODE_DDR_52MHz;
  707. mmc->card_caps |= MMC_MODE_HS_52MHz;
  708. }
  709. if (cardtype & EXT_CSD_CARD_TYPE_26)
  710. mmc->card_caps |= MMC_MODE_HS;
  711. return 0;
  712. }
  713. static int mmc_set_capacity(struct mmc *mmc, int part_num)
  714. {
  715. switch (part_num) {
  716. case 0:
  717. mmc->capacity = mmc->capacity_user;
  718. break;
  719. case 1:
  720. case 2:
  721. mmc->capacity = mmc->capacity_boot;
  722. break;
  723. case 3:
  724. mmc->capacity = mmc->capacity_rpmb;
  725. break;
  726. case 4:
  727. case 5:
  728. case 6:
  729. case 7:
  730. mmc->capacity = mmc->capacity_gp[part_num - 4];
  731. break;
  732. default:
  733. return -1;
  734. }
  735. mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
  736. return 0;
  737. }
  738. #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
  739. static int mmc_boot_part_access_chk(struct mmc *mmc, unsigned int part_num)
  740. {
  741. int forbidden = 0;
  742. bool change = false;
  743. if (part_num & PART_ACCESS_MASK)
  744. forbidden = MMC_CAP(MMC_HS_200);
  745. if (MMC_CAP(mmc->selected_mode) & forbidden) {
  746. debug("selected mode (%s) is forbidden for part %d\n",
  747. mmc_mode_name(mmc->selected_mode), part_num);
  748. change = true;
  749. } else if (mmc->selected_mode != mmc->best_mode) {
  750. debug("selected mode is not optimal\n");
  751. change = true;
  752. }
  753. if (change)
  754. return mmc_select_mode_and_width(mmc,
  755. mmc->card_caps & ~forbidden);
  756. return 0;
  757. }
  758. #else
  759. static inline int mmc_boot_part_access_chk(struct mmc *mmc,
  760. unsigned int part_num)
  761. {
  762. return 0;
  763. }
  764. #endif
  765. int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
  766. {
  767. int ret;
  768. ret = mmc_boot_part_access_chk(mmc, part_num);
  769. if (ret)
  770. return ret;
  771. ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
  772. (mmc->part_config & ~PART_ACCESS_MASK)
  773. | (part_num & PART_ACCESS_MASK));
  774. /*
  775. * Set the capacity if the switch succeeded or was intended
  776. * to return to representing the raw device.
  777. */
  778. if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
  779. ret = mmc_set_capacity(mmc, part_num);
  780. mmc_get_blk_desc(mmc)->hwpart = part_num;
  781. }
  782. return ret;
  783. }
  784. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  785. int mmc_hwpart_config(struct mmc *mmc,
  786. const struct mmc_hwpart_conf *conf,
  787. enum mmc_hwpart_conf_mode mode)
  788. {
  789. u8 part_attrs = 0;
  790. u32 enh_size_mult;
  791. u32 enh_start_addr;
  792. u32 gp_size_mult[4];
  793. u32 max_enh_size_mult;
  794. u32 tot_enh_size_mult = 0;
  795. u8 wr_rel_set;
  796. int i, pidx, err;
  797. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  798. if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
  799. return -EINVAL;
  800. if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
  801. pr_err("eMMC >= 4.4 required for enhanced user data area\n");
  802. return -EMEDIUMTYPE;
  803. }
  804. if (!(mmc->part_support & PART_SUPPORT)) {
  805. pr_err("Card does not support partitioning\n");
  806. return -EMEDIUMTYPE;
  807. }
  808. if (!mmc->hc_wp_grp_size) {
  809. pr_err("Card does not define HC WP group size\n");
  810. return -EMEDIUMTYPE;
  811. }
  812. /* check partition alignment and total enhanced size */
  813. if (conf->user.enh_size) {
  814. if (conf->user.enh_size % mmc->hc_wp_grp_size ||
  815. conf->user.enh_start % mmc->hc_wp_grp_size) {
  816. pr_err("User data enhanced area not HC WP group "
  817. "size aligned\n");
  818. return -EINVAL;
  819. }
  820. part_attrs |= EXT_CSD_ENH_USR;
  821. enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
  822. if (mmc->high_capacity) {
  823. enh_start_addr = conf->user.enh_start;
  824. } else {
  825. enh_start_addr = (conf->user.enh_start << 9);
  826. }
  827. } else {
  828. enh_size_mult = 0;
  829. enh_start_addr = 0;
  830. }
  831. tot_enh_size_mult += enh_size_mult;
  832. for (pidx = 0; pidx < 4; pidx++) {
  833. if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
  834. pr_err("GP%i partition not HC WP group size "
  835. "aligned\n", pidx+1);
  836. return -EINVAL;
  837. }
  838. gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
  839. if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
  840. part_attrs |= EXT_CSD_ENH_GP(pidx);
  841. tot_enh_size_mult += gp_size_mult[pidx];
  842. }
  843. }
  844. if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
  845. pr_err("Card does not support enhanced attribute\n");
  846. return -EMEDIUMTYPE;
  847. }
  848. err = mmc_send_ext_csd(mmc, ext_csd);
  849. if (err)
  850. return err;
  851. max_enh_size_mult =
  852. (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
  853. (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
  854. ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
  855. if (tot_enh_size_mult > max_enh_size_mult) {
  856. pr_err("Total enhanced size exceeds maximum (%u > %u)\n",
  857. tot_enh_size_mult, max_enh_size_mult);
  858. return -EMEDIUMTYPE;
  859. }
  860. /* The default value of EXT_CSD_WR_REL_SET is device
  861. * dependent, the values can only be changed if the
  862. * EXT_CSD_HS_CTRL_REL bit is set. The values can be
  863. * changed only once and before partitioning is completed. */
  864. wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
  865. if (conf->user.wr_rel_change) {
  866. if (conf->user.wr_rel_set)
  867. wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
  868. else
  869. wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
  870. }
  871. for (pidx = 0; pidx < 4; pidx++) {
  872. if (conf->gp_part[pidx].wr_rel_change) {
  873. if (conf->gp_part[pidx].wr_rel_set)
  874. wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
  875. else
  876. wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
  877. }
  878. }
  879. if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
  880. !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
  881. puts("Card does not support host controlled partition write "
  882. "reliability settings\n");
  883. return -EMEDIUMTYPE;
  884. }
  885. if (ext_csd[EXT_CSD_PARTITION_SETTING] &
  886. EXT_CSD_PARTITION_SETTING_COMPLETED) {
  887. pr_err("Card already partitioned\n");
  888. return -EPERM;
  889. }
  890. if (mode == MMC_HWPART_CONF_CHECK)
  891. return 0;
  892. /* Partitioning requires high-capacity size definitions */
  893. if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
  894. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  895. EXT_CSD_ERASE_GROUP_DEF, 1);
  896. if (err)
  897. return err;
  898. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  899. /* update erase group size to be high-capacity */
  900. mmc->erase_grp_size =
  901. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
  902. }
  903. /* all OK, write the configuration */
  904. for (i = 0; i < 4; i++) {
  905. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  906. EXT_CSD_ENH_START_ADDR+i,
  907. (enh_start_addr >> (i*8)) & 0xFF);
  908. if (err)
  909. return err;
  910. }
  911. for (i = 0; i < 3; i++) {
  912. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  913. EXT_CSD_ENH_SIZE_MULT+i,
  914. (enh_size_mult >> (i*8)) & 0xFF);
  915. if (err)
  916. return err;
  917. }
  918. for (pidx = 0; pidx < 4; pidx++) {
  919. for (i = 0; i < 3; i++) {
  920. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  921. EXT_CSD_GP_SIZE_MULT+pidx*3+i,
  922. (gp_size_mult[pidx] >> (i*8)) & 0xFF);
  923. if (err)
  924. return err;
  925. }
  926. }
  927. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  928. EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
  929. if (err)
  930. return err;
  931. if (mode == MMC_HWPART_CONF_SET)
  932. return 0;
  933. /* The WR_REL_SET is a write-once register but shall be
  934. * written before setting PART_SETTING_COMPLETED. As it is
  935. * write-once we can only write it when completing the
  936. * partitioning. */
  937. if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
  938. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  939. EXT_CSD_WR_REL_SET, wr_rel_set);
  940. if (err)
  941. return err;
  942. }
  943. /* Setting PART_SETTING_COMPLETED confirms the partition
  944. * configuration but it only becomes effective after power
  945. * cycle, so we do not adjust the partition related settings
  946. * in the mmc struct. */
  947. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  948. EXT_CSD_PARTITION_SETTING,
  949. EXT_CSD_PARTITION_SETTING_COMPLETED);
  950. if (err)
  951. return err;
  952. return 0;
  953. }
  954. #endif
  955. #if !CONFIG_IS_ENABLED(DM_MMC)
  956. int mmc_getcd(struct mmc *mmc)
  957. {
  958. int cd;
  959. cd = board_mmc_getcd(mmc);
  960. if (cd < 0) {
  961. if (mmc->cfg->ops->getcd)
  962. cd = mmc->cfg->ops->getcd(mmc);
  963. else
  964. cd = 1;
  965. }
  966. return cd;
  967. }
  968. #endif
  969. static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
  970. {
  971. struct mmc_cmd cmd;
  972. struct mmc_data data;
  973. /* Switch the frequency */
  974. cmd.cmdidx = SD_CMD_SWITCH_FUNC;
  975. cmd.resp_type = MMC_RSP_R1;
  976. cmd.cmdarg = (mode << 31) | 0xffffff;
  977. cmd.cmdarg &= ~(0xf << (group * 4));
  978. cmd.cmdarg |= value << (group * 4);
  979. data.dest = (char *)resp;
  980. data.blocksize = 64;
  981. data.blocks = 1;
  982. data.flags = MMC_DATA_READ;
  983. return mmc_send_cmd(mmc, &cmd, &data);
  984. }
  985. static int sd_get_capabilities(struct mmc *mmc)
  986. {
  987. int err;
  988. struct mmc_cmd cmd;
  989. ALLOC_CACHE_ALIGN_BUFFER(__be32, scr, 2);
  990. ALLOC_CACHE_ALIGN_BUFFER(__be32, switch_status, 16);
  991. struct mmc_data data;
  992. int timeout;
  993. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  994. u32 sd3_bus_mode;
  995. #endif
  996. mmc->card_caps = MMC_MODE_1BIT | MMC_CAP(SD_LEGACY);
  997. if (mmc_host_is_spi(mmc))
  998. return 0;
  999. /* Read the SCR to find out if this card supports higher speeds */
  1000. cmd.cmdidx = MMC_CMD_APP_CMD;
  1001. cmd.resp_type = MMC_RSP_R1;
  1002. cmd.cmdarg = mmc->rca << 16;
  1003. err = mmc_send_cmd(mmc, &cmd, NULL);
  1004. if (err)
  1005. return err;
  1006. cmd.cmdidx = SD_CMD_APP_SEND_SCR;
  1007. cmd.resp_type = MMC_RSP_R1;
  1008. cmd.cmdarg = 0;
  1009. timeout = 3;
  1010. retry_scr:
  1011. data.dest = (char *)scr;
  1012. data.blocksize = 8;
  1013. data.blocks = 1;
  1014. data.flags = MMC_DATA_READ;
  1015. err = mmc_send_cmd(mmc, &cmd, &data);
  1016. if (err) {
  1017. if (timeout--)
  1018. goto retry_scr;
  1019. return err;
  1020. }
  1021. mmc->scr[0] = __be32_to_cpu(scr[0]);
  1022. mmc->scr[1] = __be32_to_cpu(scr[1]);
  1023. switch ((mmc->scr[0] >> 24) & 0xf) {
  1024. case 0:
  1025. mmc->version = SD_VERSION_1_0;
  1026. break;
  1027. case 1:
  1028. mmc->version = SD_VERSION_1_10;
  1029. break;
  1030. case 2:
  1031. mmc->version = SD_VERSION_2;
  1032. if ((mmc->scr[0] >> 15) & 0x1)
  1033. mmc->version = SD_VERSION_3;
  1034. break;
  1035. default:
  1036. mmc->version = SD_VERSION_1_0;
  1037. break;
  1038. }
  1039. if (mmc->scr[0] & SD_DATA_4BIT)
  1040. mmc->card_caps |= MMC_MODE_4BIT;
  1041. /* Version 1.0 doesn't support switching */
  1042. if (mmc->version == SD_VERSION_1_0)
  1043. return 0;
  1044. timeout = 4;
  1045. while (timeout--) {
  1046. err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
  1047. (u8 *)switch_status);
  1048. if (err)
  1049. return err;
  1050. /* The high-speed function is busy. Try again */
  1051. if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
  1052. break;
  1053. }
  1054. /* If high-speed isn't supported, we return */
  1055. if (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)
  1056. mmc->card_caps |= MMC_CAP(SD_HS);
  1057. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  1058. /* Version before 3.0 don't support UHS modes */
  1059. if (mmc->version < SD_VERSION_3)
  1060. return 0;
  1061. sd3_bus_mode = __be32_to_cpu(switch_status[3]) >> 16 & 0x1f;
  1062. if (sd3_bus_mode & SD_MODE_UHS_SDR104)
  1063. mmc->card_caps |= MMC_CAP(UHS_SDR104);
  1064. if (sd3_bus_mode & SD_MODE_UHS_SDR50)
  1065. mmc->card_caps |= MMC_CAP(UHS_SDR50);
  1066. if (sd3_bus_mode & SD_MODE_UHS_SDR25)
  1067. mmc->card_caps |= MMC_CAP(UHS_SDR25);
  1068. if (sd3_bus_mode & SD_MODE_UHS_SDR12)
  1069. mmc->card_caps |= MMC_CAP(UHS_SDR12);
  1070. if (sd3_bus_mode & SD_MODE_UHS_DDR50)
  1071. mmc->card_caps |= MMC_CAP(UHS_DDR50);
  1072. #endif
  1073. return 0;
  1074. }
  1075. static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)
  1076. {
  1077. int err;
  1078. ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
  1079. int speed;
  1080. switch (mode) {
  1081. case SD_LEGACY:
  1082. speed = UHS_SDR12_BUS_SPEED;
  1083. break;
  1084. case SD_HS:
  1085. speed = HIGH_SPEED_BUS_SPEED;
  1086. break;
  1087. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  1088. case UHS_SDR12:
  1089. speed = UHS_SDR12_BUS_SPEED;
  1090. break;
  1091. case UHS_SDR25:
  1092. speed = UHS_SDR25_BUS_SPEED;
  1093. break;
  1094. case UHS_SDR50:
  1095. speed = UHS_SDR50_BUS_SPEED;
  1096. break;
  1097. case UHS_DDR50:
  1098. speed = UHS_DDR50_BUS_SPEED;
  1099. break;
  1100. case UHS_SDR104:
  1101. speed = UHS_SDR104_BUS_SPEED;
  1102. break;
  1103. #endif
  1104. default:
  1105. return -EINVAL;
  1106. }
  1107. err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, speed, (u8 *)switch_status);
  1108. if (err)
  1109. return err;
  1110. if ((__be32_to_cpu(switch_status[4]) >> 24) != speed)
  1111. return -ENOTSUPP;
  1112. return 0;
  1113. }
  1114. int sd_select_bus_width(struct mmc *mmc, int w)
  1115. {
  1116. int err;
  1117. struct mmc_cmd cmd;
  1118. if ((w != 4) && (w != 1))
  1119. return -EINVAL;
  1120. cmd.cmdidx = MMC_CMD_APP_CMD;
  1121. cmd.resp_type = MMC_RSP_R1;
  1122. cmd.cmdarg = mmc->rca << 16;
  1123. err = mmc_send_cmd(mmc, &cmd, NULL);
  1124. if (err)
  1125. return err;
  1126. cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
  1127. cmd.resp_type = MMC_RSP_R1;
  1128. if (w == 4)
  1129. cmd.cmdarg = 2;
  1130. else if (w == 1)
  1131. cmd.cmdarg = 0;
  1132. err = mmc_send_cmd(mmc, &cmd, NULL);
  1133. if (err)
  1134. return err;
  1135. return 0;
  1136. }
  1137. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1138. static int sd_read_ssr(struct mmc *mmc)
  1139. {
  1140. static const unsigned int sd_au_size[] = {
  1141. 0, SZ_16K / 512, SZ_32K / 512,
  1142. SZ_64K / 512, SZ_128K / 512, SZ_256K / 512,
  1143. SZ_512K / 512, SZ_1M / 512, SZ_2M / 512,
  1144. SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
  1145. SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512,
  1146. SZ_64M / 512,
  1147. };
  1148. int err, i;
  1149. struct mmc_cmd cmd;
  1150. ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
  1151. struct mmc_data data;
  1152. int timeout = 3;
  1153. unsigned int au, eo, et, es;
  1154. cmd.cmdidx = MMC_CMD_APP_CMD;
  1155. cmd.resp_type = MMC_RSP_R1;
  1156. cmd.cmdarg = mmc->rca << 16;
  1157. err = mmc_send_cmd(mmc, &cmd, NULL);
  1158. if (err)
  1159. return err;
  1160. cmd.cmdidx = SD_CMD_APP_SD_STATUS;
  1161. cmd.resp_type = MMC_RSP_R1;
  1162. cmd.cmdarg = 0;
  1163. retry_ssr:
  1164. data.dest = (char *)ssr;
  1165. data.blocksize = 64;
  1166. data.blocks = 1;
  1167. data.flags = MMC_DATA_READ;
  1168. err = mmc_send_cmd(mmc, &cmd, &data);
  1169. if (err) {
  1170. if (timeout--)
  1171. goto retry_ssr;
  1172. return err;
  1173. }
  1174. for (i = 0; i < 16; i++)
  1175. ssr[i] = be32_to_cpu(ssr[i]);
  1176. au = (ssr[2] >> 12) & 0xF;
  1177. if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
  1178. mmc->ssr.au = sd_au_size[au];
  1179. es = (ssr[3] >> 24) & 0xFF;
  1180. es |= (ssr[2] & 0xFF) << 8;
  1181. et = (ssr[3] >> 18) & 0x3F;
  1182. if (es && et) {
  1183. eo = (ssr[3] >> 16) & 0x3;
  1184. mmc->ssr.erase_timeout = (et * 1000) / es;
  1185. mmc->ssr.erase_offset = eo * 1000;
  1186. }
  1187. } else {
  1188. debug("Invalid Allocation Unit Size.\n");
  1189. }
  1190. return 0;
  1191. }
  1192. #endif
  1193. /* frequency bases */
  1194. /* divided by 10 to be nice to platforms without floating point */
  1195. static const int fbase[] = {
  1196. 10000,
  1197. 100000,
  1198. 1000000,
  1199. 10000000,
  1200. };
  1201. /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice
  1202. * to platforms without floating point.
  1203. */
  1204. static const u8 multipliers[] = {
  1205. 0, /* reserved */
  1206. 10,
  1207. 12,
  1208. 13,
  1209. 15,
  1210. 20,
  1211. 25,
  1212. 30,
  1213. 35,
  1214. 40,
  1215. 45,
  1216. 50,
  1217. 55,
  1218. 60,
  1219. 70,
  1220. 80,
  1221. };
  1222. static inline int bus_width(uint cap)
  1223. {
  1224. if (cap == MMC_MODE_8BIT)
  1225. return 8;
  1226. if (cap == MMC_MODE_4BIT)
  1227. return 4;
  1228. if (cap == MMC_MODE_1BIT)
  1229. return 1;
  1230. pr_warn("invalid bus witdh capability 0x%x\n", cap);
  1231. return 0;
  1232. }
  1233. #if !CONFIG_IS_ENABLED(DM_MMC)
  1234. #ifdef MMC_SUPPORTS_TUNING
  1235. static int mmc_execute_tuning(struct mmc *mmc, uint opcode)
  1236. {
  1237. return -ENOTSUPP;
  1238. }
  1239. #endif
  1240. static void mmc_send_init_stream(struct mmc *mmc)
  1241. {
  1242. }
  1243. static int mmc_set_ios(struct mmc *mmc)
  1244. {
  1245. int ret = 0;
  1246. if (mmc->cfg->ops->set_ios)
  1247. ret = mmc->cfg->ops->set_ios(mmc);
  1248. return ret;
  1249. }
  1250. #endif
  1251. int mmc_set_clock(struct mmc *mmc, uint clock, bool disable)
  1252. {
  1253. if (!disable) {
  1254. if (clock > mmc->cfg->f_max)
  1255. clock = mmc->cfg->f_max;
  1256. if (clock < mmc->cfg->f_min)
  1257. clock = mmc->cfg->f_min;
  1258. }
  1259. mmc->clock = clock;
  1260. mmc->clk_disable = disable;
  1261. return mmc_set_ios(mmc);
  1262. }
  1263. static int mmc_set_bus_width(struct mmc *mmc, uint width)
  1264. {
  1265. mmc->bus_width = width;
  1266. return mmc_set_ios(mmc);
  1267. }
  1268. #if CONFIG_IS_ENABLED(MMC_VERBOSE) || defined(DEBUG)
  1269. /*
  1270. * helper function to display the capabilities in a human
  1271. * friendly manner. The capabilities include bus width and
  1272. * supported modes.
  1273. */
  1274. void mmc_dump_capabilities(const char *text, uint caps)
  1275. {
  1276. enum bus_mode mode;
  1277. printf("%s: widths [", text);
  1278. if (caps & MMC_MODE_8BIT)
  1279. printf("8, ");
  1280. if (caps & MMC_MODE_4BIT)
  1281. printf("4, ");
  1282. if (caps & MMC_MODE_1BIT)
  1283. printf("1, ");
  1284. printf("\b\b] modes [");
  1285. for (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)
  1286. if (MMC_CAP(mode) & caps)
  1287. printf("%s, ", mmc_mode_name(mode));
  1288. printf("\b\b]\n");
  1289. }
  1290. #endif
  1291. struct mode_width_tuning {
  1292. enum bus_mode mode;
  1293. uint widths;
  1294. #ifdef MMC_SUPPORTS_TUNING
  1295. uint tuning;
  1296. #endif
  1297. };
  1298. #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
  1299. int mmc_voltage_to_mv(enum mmc_voltage voltage)
  1300. {
  1301. switch (voltage) {
  1302. case MMC_SIGNAL_VOLTAGE_000: return 0;
  1303. case MMC_SIGNAL_VOLTAGE_330: return 3300;
  1304. case MMC_SIGNAL_VOLTAGE_180: return 1800;
  1305. case MMC_SIGNAL_VOLTAGE_120: return 1200;
  1306. }
  1307. return -EINVAL;
  1308. }
  1309. static int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
  1310. {
  1311. int err;
  1312. if (mmc->signal_voltage == signal_voltage)
  1313. return 0;
  1314. mmc->signal_voltage = signal_voltage;
  1315. err = mmc_set_ios(mmc);
  1316. if (err)
  1317. debug("unable to set voltage (err %d)\n", err);
  1318. return err;
  1319. }
  1320. #else
  1321. static inline int mmc_set_signal_voltage(struct mmc *mmc, uint signal_voltage)
  1322. {
  1323. return 0;
  1324. }
  1325. #endif
  1326. static const struct mode_width_tuning sd_modes_by_pref[] = {
  1327. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  1328. #ifdef MMC_SUPPORTS_TUNING
  1329. {
  1330. .mode = UHS_SDR104,
  1331. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1332. .tuning = MMC_CMD_SEND_TUNING_BLOCK
  1333. },
  1334. #endif
  1335. {
  1336. .mode = UHS_SDR50,
  1337. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1338. },
  1339. {
  1340. .mode = UHS_DDR50,
  1341. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1342. },
  1343. {
  1344. .mode = UHS_SDR25,
  1345. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1346. },
  1347. #endif
  1348. {
  1349. .mode = SD_HS,
  1350. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1351. },
  1352. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  1353. {
  1354. .mode = UHS_SDR12,
  1355. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1356. },
  1357. #endif
  1358. {
  1359. .mode = SD_LEGACY,
  1360. .widths = MMC_MODE_4BIT | MMC_MODE_1BIT,
  1361. }
  1362. };
  1363. #define for_each_sd_mode_by_pref(caps, mwt) \
  1364. for (mwt = sd_modes_by_pref;\
  1365. mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\
  1366. mwt++) \
  1367. if (caps & MMC_CAP(mwt->mode))
  1368. static int sd_select_mode_and_width(struct mmc *mmc, uint card_caps)
  1369. {
  1370. int err;
  1371. uint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};
  1372. const struct mode_width_tuning *mwt;
  1373. #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
  1374. bool uhs_en = (mmc->ocr & OCR_S18R) ? true : false;
  1375. #else
  1376. bool uhs_en = false;
  1377. #endif
  1378. uint caps;
  1379. #ifdef DEBUG
  1380. mmc_dump_capabilities("sd card", card_caps);
  1381. mmc_dump_capabilities("host", mmc->host_caps);
  1382. #endif
  1383. /* Restrict card's capabilities by what the host can do */
  1384. caps = card_caps & mmc->host_caps;
  1385. if (!uhs_en)
  1386. caps &= ~UHS_CAPS;
  1387. for_each_sd_mode_by_pref(caps, mwt) {
  1388. uint *w;
  1389. for (w = widths; w < widths + ARRAY_SIZE(widths); w++) {
  1390. if (*w & caps & mwt->widths) {
  1391. debug("trying mode %s width %d (at %d MHz)\n",
  1392. mmc_mode_name(mwt->mode),
  1393. bus_width(*w),
  1394. mmc_mode2freq(mmc, mwt->mode) / 1000000);
  1395. /* configure the bus width (card + host) */
  1396. err = sd_select_bus_width(mmc, bus_width(*w));
  1397. if (err)
  1398. goto error;
  1399. mmc_set_bus_width(mmc, bus_width(*w));
  1400. /* configure the bus mode (card) */
  1401. err = sd_set_card_speed(mmc, mwt->mode);
  1402. if (err)
  1403. goto error;
  1404. /* configure the bus mode (host) */
  1405. mmc_select_mode(mmc, mwt->mode);
  1406. mmc_set_clock(mmc, mmc->tran_speed, false);
  1407. #ifdef MMC_SUPPORTS_TUNING
  1408. /* execute tuning if needed */
  1409. if (mwt->tuning && !mmc_host_is_spi(mmc)) {
  1410. err = mmc_execute_tuning(mmc,
  1411. mwt->tuning);
  1412. if (err) {
  1413. debug("tuning failed\n");
  1414. goto error;
  1415. }
  1416. }
  1417. #endif
  1418. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1419. err = sd_read_ssr(mmc);
  1420. if (!err)
  1421. pr_warn("unable to read ssr\n");
  1422. #endif
  1423. if (!err)
  1424. return 0;
  1425. error:
  1426. /* revert to a safer bus speed */
  1427. mmc_select_mode(mmc, SD_LEGACY);
  1428. mmc_set_clock(mmc, mmc->tran_speed, false);
  1429. }
  1430. }
  1431. }
  1432. printf("unable to select a mode\n");
  1433. return -ENOTSUPP;
  1434. }
  1435. /*
  1436. * read the compare the part of ext csd that is constant.
  1437. * This can be used to check that the transfer is working
  1438. * as expected.
  1439. */
  1440. static int mmc_read_and_compare_ext_csd(struct mmc *mmc)
  1441. {
  1442. int err;
  1443. const u8 *ext_csd = mmc->ext_csd;
  1444. ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
  1445. if (mmc->version < MMC_VERSION_4)
  1446. return 0;
  1447. err = mmc_send_ext_csd(mmc, test_csd);
  1448. if (err)
  1449. return err;
  1450. /* Only compare read only fields */
  1451. if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
  1452. == test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
  1453. ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
  1454. == test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
  1455. ext_csd[EXT_CSD_REV]
  1456. == test_csd[EXT_CSD_REV] &&
  1457. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  1458. == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
  1459. memcmp(&ext_csd[EXT_CSD_SEC_CNT],
  1460. &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
  1461. return 0;
  1462. return -EBADMSG;
  1463. }
  1464. #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
  1465. static int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
  1466. uint32_t allowed_mask)
  1467. {
  1468. u32 card_mask = 0;
  1469. switch (mode) {
  1470. case MMC_HS_200:
  1471. if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_8V)
  1472. card_mask |= MMC_SIGNAL_VOLTAGE_180;
  1473. if (mmc->cardtype & EXT_CSD_CARD_TYPE_HS200_1_2V)
  1474. card_mask |= MMC_SIGNAL_VOLTAGE_120;
  1475. break;
  1476. case MMC_DDR_52:
  1477. if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
  1478. card_mask |= MMC_SIGNAL_VOLTAGE_330 |
  1479. MMC_SIGNAL_VOLTAGE_180;
  1480. if (mmc->cardtype & EXT_CSD_CARD_TYPE_DDR_1_2V)
  1481. card_mask |= MMC_SIGNAL_VOLTAGE_120;
  1482. break;
  1483. default:
  1484. card_mask |= MMC_SIGNAL_VOLTAGE_330;
  1485. break;
  1486. }
  1487. while (card_mask & allowed_mask) {
  1488. enum mmc_voltage best_match;
  1489. best_match = 1 << (ffs(card_mask & allowed_mask) - 1);
  1490. if (!mmc_set_signal_voltage(mmc, best_match))
  1491. return 0;
  1492. allowed_mask &= ~best_match;
  1493. }
  1494. return -ENOTSUPP;
  1495. }
  1496. #else
  1497. static inline int mmc_set_lowest_voltage(struct mmc *mmc, enum bus_mode mode,
  1498. uint32_t allowed_mask)
  1499. {
  1500. return 0;
  1501. }
  1502. #endif
  1503. static const struct mode_width_tuning mmc_modes_by_pref[] = {
  1504. #if CONFIG_IS_ENABLED(MMC_HS200_SUPPORT)
  1505. {
  1506. .mode = MMC_HS_200,
  1507. .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
  1508. .tuning = MMC_CMD_SEND_TUNING_BLOCK_HS200
  1509. },
  1510. #endif
  1511. {
  1512. .mode = MMC_DDR_52,
  1513. .widths = MMC_MODE_8BIT | MMC_MODE_4BIT,
  1514. },
  1515. {
  1516. .mode = MMC_HS_52,
  1517. .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
  1518. },
  1519. {
  1520. .mode = MMC_HS,
  1521. .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
  1522. },
  1523. {
  1524. .mode = MMC_LEGACY,
  1525. .widths = MMC_MODE_8BIT | MMC_MODE_4BIT | MMC_MODE_1BIT,
  1526. }
  1527. };
  1528. #define for_each_mmc_mode_by_pref(caps, mwt) \
  1529. for (mwt = mmc_modes_by_pref;\
  1530. mwt < mmc_modes_by_pref + ARRAY_SIZE(mmc_modes_by_pref);\
  1531. mwt++) \
  1532. if (caps & MMC_CAP(mwt->mode))
  1533. static const struct ext_csd_bus_width {
  1534. uint cap;
  1535. bool is_ddr;
  1536. uint ext_csd_bits;
  1537. } ext_csd_bus_width[] = {
  1538. {MMC_MODE_8BIT, true, EXT_CSD_DDR_BUS_WIDTH_8},
  1539. {MMC_MODE_4BIT, true, EXT_CSD_DDR_BUS_WIDTH_4},
  1540. {MMC_MODE_8BIT, false, EXT_CSD_BUS_WIDTH_8},
  1541. {MMC_MODE_4BIT, false, EXT_CSD_BUS_WIDTH_4},
  1542. {MMC_MODE_1BIT, false, EXT_CSD_BUS_WIDTH_1},
  1543. };
  1544. #define for_each_supported_width(caps, ddr, ecbv) \
  1545. for (ecbv = ext_csd_bus_width;\
  1546. ecbv < ext_csd_bus_width + ARRAY_SIZE(ext_csd_bus_width);\
  1547. ecbv++) \
  1548. if ((ddr == ecbv->is_ddr) && (caps & ecbv->cap))
  1549. static int mmc_select_mode_and_width(struct mmc *mmc, uint card_caps)
  1550. {
  1551. int err;
  1552. const struct mode_width_tuning *mwt;
  1553. const struct ext_csd_bus_width *ecbw;
  1554. #ifdef DEBUG
  1555. mmc_dump_capabilities("mmc", card_caps);
  1556. mmc_dump_capabilities("host", mmc->host_caps);
  1557. #endif
  1558. /* Restrict card's capabilities by what the host can do */
  1559. card_caps &= mmc->host_caps;
  1560. /* Only version 4 of MMC supports wider bus widths */
  1561. if (mmc->version < MMC_VERSION_4)
  1562. return 0;
  1563. if (!mmc->ext_csd) {
  1564. debug("No ext_csd found!\n"); /* this should enver happen */
  1565. return -ENOTSUPP;
  1566. }
  1567. mmc_set_clock(mmc, mmc->legacy_speed, false);
  1568. for_each_mmc_mode_by_pref(card_caps, mwt) {
  1569. for_each_supported_width(card_caps & mwt->widths,
  1570. mmc_is_mode_ddr(mwt->mode), ecbw) {
  1571. enum mmc_voltage old_voltage;
  1572. debug("trying mode %s width %d (at %d MHz)\n",
  1573. mmc_mode_name(mwt->mode),
  1574. bus_width(ecbw->cap),
  1575. mmc_mode2freq(mmc, mwt->mode) / 1000000);
  1576. old_voltage = mmc->signal_voltage;
  1577. err = mmc_set_lowest_voltage(mmc, mwt->mode,
  1578. MMC_ALL_SIGNAL_VOLTAGE);
  1579. if (err)
  1580. continue;
  1581. /* configure the bus width (card + host) */
  1582. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1583. EXT_CSD_BUS_WIDTH,
  1584. ecbw->ext_csd_bits & ~EXT_CSD_DDR_FLAG);
  1585. if (err)
  1586. goto error;
  1587. mmc_set_bus_width(mmc, bus_width(ecbw->cap));
  1588. /* configure the bus speed (card) */
  1589. err = mmc_set_card_speed(mmc, mwt->mode);
  1590. if (err)
  1591. goto error;
  1592. /*
  1593. * configure the bus width AND the ddr mode (card)
  1594. * The host side will be taken care of in the next step
  1595. */
  1596. if (ecbw->ext_csd_bits & EXT_CSD_DDR_FLAG) {
  1597. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1598. EXT_CSD_BUS_WIDTH,
  1599. ecbw->ext_csd_bits);
  1600. if (err)
  1601. goto error;
  1602. }
  1603. /* configure the bus mode (host) */
  1604. mmc_select_mode(mmc, mwt->mode);
  1605. mmc_set_clock(mmc, mmc->tran_speed, false);
  1606. #ifdef MMC_SUPPORTS_TUNING
  1607. /* execute tuning if needed */
  1608. if (mwt->tuning) {
  1609. err = mmc_execute_tuning(mmc, mwt->tuning);
  1610. if (err) {
  1611. debug("tuning failed\n");
  1612. goto error;
  1613. }
  1614. }
  1615. #endif
  1616. /* do a transfer to check the configuration */
  1617. err = mmc_read_and_compare_ext_csd(mmc);
  1618. if (!err)
  1619. return 0;
  1620. error:
  1621. mmc_set_signal_voltage(mmc, old_voltage);
  1622. /* if an error occured, revert to a safer bus mode */
  1623. mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1624. EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_1);
  1625. mmc_select_mode(mmc, MMC_LEGACY);
  1626. mmc_set_bus_width(mmc, 1);
  1627. }
  1628. }
  1629. pr_err("unable to select a mode\n");
  1630. return -ENOTSUPP;
  1631. }
  1632. static int mmc_startup_v4(struct mmc *mmc)
  1633. {
  1634. int err, i;
  1635. u64 capacity;
  1636. bool has_parts = false;
  1637. bool part_completed;
  1638. static const u32 mmc_versions[] = {
  1639. MMC_VERSION_4,
  1640. MMC_VERSION_4_1,
  1641. MMC_VERSION_4_2,
  1642. MMC_VERSION_4_3,
  1643. MMC_VERSION_4_41,
  1644. MMC_VERSION_4_5,
  1645. MMC_VERSION_5_0,
  1646. MMC_VERSION_5_1
  1647. };
  1648. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  1649. if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4))
  1650. return 0;
  1651. /* check ext_csd version and capacity */
  1652. err = mmc_send_ext_csd(mmc, ext_csd);
  1653. if (err)
  1654. goto error;
  1655. /* store the ext csd for future reference */
  1656. if (!mmc->ext_csd)
  1657. mmc->ext_csd = malloc(MMC_MAX_BLOCK_LEN);
  1658. if (!mmc->ext_csd)
  1659. return -ENOMEM;
  1660. memcpy(mmc->ext_csd, ext_csd, MMC_MAX_BLOCK_LEN);
  1661. if (ext_csd[EXT_CSD_REV] > ARRAY_SIZE(mmc_versions))
  1662. return -EINVAL;
  1663. mmc->version = mmc_versions[ext_csd[EXT_CSD_REV]];
  1664. if (mmc->version >= MMC_VERSION_4_2) {
  1665. /*
  1666. * According to the JEDEC Standard, the value of
  1667. * ext_csd's capacity is valid if the value is more
  1668. * than 2GB
  1669. */
  1670. capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
  1671. | ext_csd[EXT_CSD_SEC_CNT + 1] << 8
  1672. | ext_csd[EXT_CSD_SEC_CNT + 2] << 16
  1673. | ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  1674. capacity *= MMC_MAX_BLOCK_LEN;
  1675. if ((capacity >> 20) > 2 * 1024)
  1676. mmc->capacity_user = capacity;
  1677. }
  1678. /* The partition data may be non-zero but it is only
  1679. * effective if PARTITION_SETTING_COMPLETED is set in
  1680. * EXT_CSD, so ignore any data if this bit is not set,
  1681. * except for enabling the high-capacity group size
  1682. * definition (see below).
  1683. */
  1684. part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
  1685. EXT_CSD_PARTITION_SETTING_COMPLETED);
  1686. /* store the partition info of emmc */
  1687. mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
  1688. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
  1689. ext_csd[EXT_CSD_BOOT_MULT])
  1690. mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
  1691. if (part_completed &&
  1692. (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
  1693. mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
  1694. mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
  1695. mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
  1696. for (i = 0; i < 4; i++) {
  1697. int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
  1698. uint mult = (ext_csd[idx + 2] << 16) +
  1699. (ext_csd[idx + 1] << 8) + ext_csd[idx];
  1700. if (mult)
  1701. has_parts = true;
  1702. if (!part_completed)
  1703. continue;
  1704. mmc->capacity_gp[i] = mult;
  1705. mmc->capacity_gp[i] *=
  1706. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  1707. mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1708. mmc->capacity_gp[i] <<= 19;
  1709. }
  1710. #ifndef CONFIG_SPL_BUILD
  1711. if (part_completed) {
  1712. mmc->enh_user_size =
  1713. (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16) +
  1714. (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
  1715. ext_csd[EXT_CSD_ENH_SIZE_MULT];
  1716. mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
  1717. mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1718. mmc->enh_user_size <<= 19;
  1719. mmc->enh_user_start =
  1720. (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24) +
  1721. (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
  1722. (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
  1723. ext_csd[EXT_CSD_ENH_START_ADDR];
  1724. if (mmc->high_capacity)
  1725. mmc->enh_user_start <<= 9;
  1726. }
  1727. #endif
  1728. /*
  1729. * Host needs to enable ERASE_GRP_DEF bit if device is
  1730. * partitioned. This bit will be lost every time after a reset
  1731. * or power off. This will affect erase size.
  1732. */
  1733. if (part_completed)
  1734. has_parts = true;
  1735. if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
  1736. (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
  1737. has_parts = true;
  1738. if (has_parts) {
  1739. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
  1740. EXT_CSD_ERASE_GROUP_DEF, 1);
  1741. if (err)
  1742. goto error;
  1743. ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
  1744. }
  1745. if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
  1746. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1747. /* Read out group size from ext_csd */
  1748. mmc->erase_grp_size =
  1749. ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
  1750. #endif
  1751. /*
  1752. * if high capacity and partition setting completed
  1753. * SEC_COUNT is valid even if it is smaller than 2 GiB
  1754. * JEDEC Standard JESD84-B45, 6.2.4
  1755. */
  1756. if (mmc->high_capacity && part_completed) {
  1757. capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
  1758. (ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
  1759. (ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
  1760. (ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
  1761. capacity *= MMC_MAX_BLOCK_LEN;
  1762. mmc->capacity_user = capacity;
  1763. }
  1764. }
  1765. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1766. else {
  1767. /* Calculate the group size from the csd value. */
  1768. int erase_gsz, erase_gmul;
  1769. erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
  1770. erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
  1771. mmc->erase_grp_size = (erase_gsz + 1)
  1772. * (erase_gmul + 1);
  1773. }
  1774. #endif
  1775. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  1776. mmc->hc_wp_grp_size = 1024
  1777. * ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
  1778. * ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
  1779. #endif
  1780. mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
  1781. return 0;
  1782. error:
  1783. if (mmc->ext_csd) {
  1784. free(mmc->ext_csd);
  1785. mmc->ext_csd = NULL;
  1786. }
  1787. return err;
  1788. }
  1789. static int mmc_startup(struct mmc *mmc)
  1790. {
  1791. int err, i;
  1792. uint mult, freq;
  1793. u64 cmult, csize;
  1794. struct mmc_cmd cmd;
  1795. struct blk_desc *bdesc;
  1796. #ifdef CONFIG_MMC_SPI_CRC_ON
  1797. if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
  1798. cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
  1799. cmd.resp_type = MMC_RSP_R1;
  1800. cmd.cmdarg = 1;
  1801. err = mmc_send_cmd(mmc, &cmd, NULL);
  1802. if (err)
  1803. return err;
  1804. }
  1805. #endif
  1806. /* Put the Card in Identify Mode */
  1807. cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
  1808. MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
  1809. cmd.resp_type = MMC_RSP_R2;
  1810. cmd.cmdarg = 0;
  1811. err = mmc_send_cmd(mmc, &cmd, NULL);
  1812. #ifdef CONFIG_MMC_QUIRKS
  1813. if (err && (mmc->quirks & MMC_QUIRK_RETRY_SEND_CID)) {
  1814. int retries = 4;
  1815. /*
  1816. * It has been seen that SEND_CID may fail on the first
  1817. * attempt, let's try a few more time
  1818. */
  1819. do {
  1820. err = mmc_send_cmd(mmc, &cmd, NULL);
  1821. if (!err)
  1822. break;
  1823. } while (retries--);
  1824. }
  1825. #endif
  1826. if (err)
  1827. return err;
  1828. memcpy(mmc->cid, cmd.response, 16);
  1829. /*
  1830. * For MMC cards, set the Relative Address.
  1831. * For SD cards, get the Relatvie Address.
  1832. * This also puts the cards into Standby State
  1833. */
  1834. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  1835. cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
  1836. cmd.cmdarg = mmc->rca << 16;
  1837. cmd.resp_type = MMC_RSP_R6;
  1838. err = mmc_send_cmd(mmc, &cmd, NULL);
  1839. if (err)
  1840. return err;
  1841. if (IS_SD(mmc))
  1842. mmc->rca = (cmd.response[0] >> 16) & 0xffff;
  1843. }
  1844. /* Get the Card-Specific Data */
  1845. cmd.cmdidx = MMC_CMD_SEND_CSD;
  1846. cmd.resp_type = MMC_RSP_R2;
  1847. cmd.cmdarg = mmc->rca << 16;
  1848. err = mmc_send_cmd(mmc, &cmd, NULL);
  1849. if (err)
  1850. return err;
  1851. mmc->csd[0] = cmd.response[0];
  1852. mmc->csd[1] = cmd.response[1];
  1853. mmc->csd[2] = cmd.response[2];
  1854. mmc->csd[3] = cmd.response[3];
  1855. if (mmc->version == MMC_VERSION_UNKNOWN) {
  1856. int version = (cmd.response[0] >> 26) & 0xf;
  1857. switch (version) {
  1858. case 0:
  1859. mmc->version = MMC_VERSION_1_2;
  1860. break;
  1861. case 1:
  1862. mmc->version = MMC_VERSION_1_4;
  1863. break;
  1864. case 2:
  1865. mmc->version = MMC_VERSION_2_2;
  1866. break;
  1867. case 3:
  1868. mmc->version = MMC_VERSION_3;
  1869. break;
  1870. case 4:
  1871. mmc->version = MMC_VERSION_4;
  1872. break;
  1873. default:
  1874. mmc->version = MMC_VERSION_1_2;
  1875. break;
  1876. }
  1877. }
  1878. /* divide frequency by 10, since the mults are 10x bigger */
  1879. freq = fbase[(cmd.response[0] & 0x7)];
  1880. mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
  1881. mmc->legacy_speed = freq * mult;
  1882. mmc_select_mode(mmc, MMC_LEGACY);
  1883. mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
  1884. mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
  1885. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1886. if (IS_SD(mmc))
  1887. mmc->write_bl_len = mmc->read_bl_len;
  1888. else
  1889. mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
  1890. #endif
  1891. if (mmc->high_capacity) {
  1892. csize = (mmc->csd[1] & 0x3f) << 16
  1893. | (mmc->csd[2] & 0xffff0000) >> 16;
  1894. cmult = 8;
  1895. } else {
  1896. csize = (mmc->csd[1] & 0x3ff) << 2
  1897. | (mmc->csd[2] & 0xc0000000) >> 30;
  1898. cmult = (mmc->csd[2] & 0x00038000) >> 15;
  1899. }
  1900. mmc->capacity_user = (csize + 1) << (cmult + 2);
  1901. mmc->capacity_user *= mmc->read_bl_len;
  1902. mmc->capacity_boot = 0;
  1903. mmc->capacity_rpmb = 0;
  1904. for (i = 0; i < 4; i++)
  1905. mmc->capacity_gp[i] = 0;
  1906. if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
  1907. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  1908. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1909. if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
  1910. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  1911. #endif
  1912. if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
  1913. cmd.cmdidx = MMC_CMD_SET_DSR;
  1914. cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
  1915. cmd.resp_type = MMC_RSP_NONE;
  1916. if (mmc_send_cmd(mmc, &cmd, NULL))
  1917. pr_warn("MMC: SET_DSR failed\n");
  1918. }
  1919. /* Select the card, and put it into Transfer Mode */
  1920. if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
  1921. cmd.cmdidx = MMC_CMD_SELECT_CARD;
  1922. cmd.resp_type = MMC_RSP_R1;
  1923. cmd.cmdarg = mmc->rca << 16;
  1924. err = mmc_send_cmd(mmc, &cmd, NULL);
  1925. if (err)
  1926. return err;
  1927. }
  1928. /*
  1929. * For SD, its erase group is always one sector
  1930. */
  1931. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1932. mmc->erase_grp_size = 1;
  1933. #endif
  1934. mmc->part_config = MMCPART_NOAVAILABLE;
  1935. err = mmc_startup_v4(mmc);
  1936. if (err)
  1937. return err;
  1938. err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
  1939. if (err)
  1940. return err;
  1941. if (IS_SD(mmc)) {
  1942. err = sd_get_capabilities(mmc);
  1943. if (err)
  1944. return err;
  1945. err = sd_select_mode_and_width(mmc, mmc->card_caps);
  1946. } else {
  1947. err = mmc_get_capabilities(mmc);
  1948. if (err)
  1949. return err;
  1950. mmc_select_mode_and_width(mmc, mmc->card_caps);
  1951. }
  1952. if (err)
  1953. return err;
  1954. mmc->best_mode = mmc->selected_mode;
  1955. /* Fix the block length for DDR mode */
  1956. if (mmc->ddr_mode) {
  1957. mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
  1958. #if CONFIG_IS_ENABLED(MMC_WRITE)
  1959. mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
  1960. #endif
  1961. }
  1962. /* fill in device description */
  1963. bdesc = mmc_get_blk_desc(mmc);
  1964. bdesc->lun = 0;
  1965. bdesc->hwpart = 0;
  1966. bdesc->type = 0;
  1967. bdesc->blksz = mmc->read_bl_len;
  1968. bdesc->log2blksz = LOG2(bdesc->blksz);
  1969. bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
  1970. #if !defined(CONFIG_SPL_BUILD) || \
  1971. (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
  1972. !defined(CONFIG_USE_TINY_PRINTF))
  1973. sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
  1974. mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
  1975. (mmc->cid[3] >> 16) & 0xffff);
  1976. sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
  1977. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  1978. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
  1979. (mmc->cid[2] >> 24) & 0xff);
  1980. sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
  1981. (mmc->cid[2] >> 16) & 0xf);
  1982. #else
  1983. bdesc->vendor[0] = 0;
  1984. bdesc->product[0] = 0;
  1985. bdesc->revision[0] = 0;
  1986. #endif
  1987. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
  1988. part_init(bdesc);
  1989. #endif
  1990. return 0;
  1991. }
  1992. static int mmc_send_if_cond(struct mmc *mmc)
  1993. {
  1994. struct mmc_cmd cmd;
  1995. int err;
  1996. cmd.cmdidx = SD_CMD_SEND_IF_COND;
  1997. /* We set the bit if the host supports voltages between 2.7 and 3.6 V */
  1998. cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
  1999. cmd.resp_type = MMC_RSP_R7;
  2000. err = mmc_send_cmd(mmc, &cmd, NULL);
  2001. if (err)
  2002. return err;
  2003. if ((cmd.response[0] & 0xff) != 0xaa)
  2004. return -EOPNOTSUPP;
  2005. else
  2006. mmc->version = SD_VERSION_2;
  2007. return 0;
  2008. }
  2009. #if !CONFIG_IS_ENABLED(DM_MMC)
  2010. /* board-specific MMC power initializations. */
  2011. __weak void board_mmc_power_init(void)
  2012. {
  2013. }
  2014. #endif
  2015. static int mmc_power_init(struct mmc *mmc)
  2016. {
  2017. #if CONFIG_IS_ENABLED(DM_MMC)
  2018. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  2019. int ret;
  2020. ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
  2021. &mmc->vmmc_supply);
  2022. if (ret)
  2023. debug("%s: No vmmc supply\n", mmc->dev->name);
  2024. ret = device_get_supply_regulator(mmc->dev, "vqmmc-supply",
  2025. &mmc->vqmmc_supply);
  2026. if (ret)
  2027. debug("%s: No vqmmc supply\n", mmc->dev->name);
  2028. #endif
  2029. #else /* !CONFIG_DM_MMC */
  2030. /*
  2031. * Driver model should use a regulator, as above, rather than calling
  2032. * out to board code.
  2033. */
  2034. board_mmc_power_init();
  2035. #endif
  2036. return 0;
  2037. }
  2038. /*
  2039. * put the host in the initial state:
  2040. * - turn on Vdd (card power supply)
  2041. * - configure the bus width and clock to minimal values
  2042. */
  2043. static void mmc_set_initial_state(struct mmc *mmc)
  2044. {
  2045. int err;
  2046. /* First try to set 3.3V. If it fails set to 1.8V */
  2047. err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_330);
  2048. if (err != 0)
  2049. err = mmc_set_signal_voltage(mmc, MMC_SIGNAL_VOLTAGE_180);
  2050. if (err != 0)
  2051. pr_warn("mmc: failed to set signal voltage\n");
  2052. mmc_select_mode(mmc, MMC_LEGACY);
  2053. mmc_set_bus_width(mmc, 1);
  2054. mmc_set_clock(mmc, 0, false);
  2055. }
  2056. static int mmc_power_on(struct mmc *mmc)
  2057. {
  2058. #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
  2059. if (mmc->vmmc_supply) {
  2060. int ret = regulator_set_enable(mmc->vmmc_supply, true);
  2061. if (ret) {
  2062. puts("Error enabling VMMC supply\n");
  2063. return ret;
  2064. }
  2065. }
  2066. #endif
  2067. return 0;
  2068. }
  2069. static int mmc_power_off(struct mmc *mmc)
  2070. {
  2071. mmc_set_clock(mmc, 0, true);
  2072. #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(DM_REGULATOR)
  2073. if (mmc->vmmc_supply) {
  2074. int ret = regulator_set_enable(mmc->vmmc_supply, false);
  2075. if (ret) {
  2076. debug("Error disabling VMMC supply\n");
  2077. return ret;
  2078. }
  2079. }
  2080. #endif
  2081. return 0;
  2082. }
  2083. static int mmc_power_cycle(struct mmc *mmc)
  2084. {
  2085. int ret;
  2086. ret = mmc_power_off(mmc);
  2087. if (ret)
  2088. return ret;
  2089. /*
  2090. * SD spec recommends at least 1ms of delay. Let's wait for 2ms
  2091. * to be on the safer side.
  2092. */
  2093. udelay(2000);
  2094. return mmc_power_on(mmc);
  2095. }
  2096. int mmc_start_init(struct mmc *mmc)
  2097. {
  2098. bool no_card;
  2099. bool uhs_en = supports_uhs(mmc->cfg->host_caps);
  2100. int err;
  2101. /*
  2102. * all hosts are capable of 1 bit bus-width and able to use the legacy
  2103. * timings.
  2104. */
  2105. mmc->host_caps = mmc->cfg->host_caps | MMC_CAP(SD_LEGACY) |
  2106. MMC_CAP(MMC_LEGACY) | MMC_MODE_1BIT;
  2107. #if !defined(CONFIG_MMC_BROKEN_CD)
  2108. /* we pretend there's no card when init is NULL */
  2109. no_card = mmc_getcd(mmc) == 0;
  2110. #else
  2111. no_card = 0;
  2112. #endif
  2113. #if !CONFIG_IS_ENABLED(DM_MMC)
  2114. no_card = no_card || (mmc->cfg->ops->init == NULL);
  2115. #endif
  2116. if (no_card) {
  2117. mmc->has_init = 0;
  2118. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  2119. printf("MMC: no card present\n");
  2120. #endif
  2121. return -ENOMEDIUM;
  2122. }
  2123. if (mmc->has_init)
  2124. return 0;
  2125. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  2126. mmc_adapter_card_type_ident();
  2127. #endif
  2128. err = mmc_power_init(mmc);
  2129. if (err)
  2130. return err;
  2131. #ifdef CONFIG_MMC_QUIRKS
  2132. mmc->quirks = MMC_QUIRK_RETRY_SET_BLOCKLEN |
  2133. MMC_QUIRK_RETRY_SEND_CID;
  2134. #endif
  2135. err = mmc_power_cycle(mmc);
  2136. if (err) {
  2137. /*
  2138. * if power cycling is not supported, we should not try
  2139. * to use the UHS modes, because we wouldn't be able to
  2140. * recover from an error during the UHS initialization.
  2141. */
  2142. debug("Unable to do a full power cycle. Disabling the UHS modes for safety\n");
  2143. uhs_en = false;
  2144. mmc->host_caps &= ~UHS_CAPS;
  2145. err = mmc_power_on(mmc);
  2146. }
  2147. if (err)
  2148. return err;
  2149. #if CONFIG_IS_ENABLED(DM_MMC)
  2150. /* The device has already been probed ready for use */
  2151. #else
  2152. /* made sure it's not NULL earlier */
  2153. err = mmc->cfg->ops->init(mmc);
  2154. if (err)
  2155. return err;
  2156. #endif
  2157. mmc->ddr_mode = 0;
  2158. retry:
  2159. mmc_set_initial_state(mmc);
  2160. mmc_send_init_stream(mmc);
  2161. /* Reset the Card */
  2162. err = mmc_go_idle(mmc);
  2163. if (err)
  2164. return err;
  2165. /* The internal partition reset to user partition(0) at every CMD0*/
  2166. mmc_get_blk_desc(mmc)->hwpart = 0;
  2167. /* Test for SD version 2 */
  2168. err = mmc_send_if_cond(mmc);
  2169. /* Now try to get the SD card's operating condition */
  2170. err = sd_send_op_cond(mmc, uhs_en);
  2171. if (err && uhs_en) {
  2172. uhs_en = false;
  2173. mmc_power_cycle(mmc);
  2174. goto retry;
  2175. }
  2176. /* If the command timed out, we check for an MMC card */
  2177. if (err == -ETIMEDOUT) {
  2178. err = mmc_send_op_cond(mmc);
  2179. if (err) {
  2180. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
  2181. pr_err("Card did not respond to voltage select!\n");
  2182. #endif
  2183. return -EOPNOTSUPP;
  2184. }
  2185. }
  2186. if (!err)
  2187. mmc->init_in_progress = 1;
  2188. return err;
  2189. }
  2190. static int mmc_complete_init(struct mmc *mmc)
  2191. {
  2192. int err = 0;
  2193. mmc->init_in_progress = 0;
  2194. if (mmc->op_cond_pending)
  2195. err = mmc_complete_op_cond(mmc);
  2196. if (!err)
  2197. err = mmc_startup(mmc);
  2198. if (err)
  2199. mmc->has_init = 0;
  2200. else
  2201. mmc->has_init = 1;
  2202. return err;
  2203. }
  2204. int mmc_init(struct mmc *mmc)
  2205. {
  2206. int err = 0;
  2207. __maybe_unused unsigned start;
  2208. #if CONFIG_IS_ENABLED(DM_MMC)
  2209. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
  2210. upriv->mmc = mmc;
  2211. #endif
  2212. if (mmc->has_init)
  2213. return 0;
  2214. start = get_timer(0);
  2215. if (!mmc->init_in_progress)
  2216. err = mmc_start_init(mmc);
  2217. if (!err)
  2218. err = mmc_complete_init(mmc);
  2219. if (err)
  2220. printf("%s: %d, time %lu\n", __func__, err, get_timer(start));
  2221. return err;
  2222. }
  2223. int mmc_set_dsr(struct mmc *mmc, u16 val)
  2224. {
  2225. mmc->dsr = val;
  2226. return 0;
  2227. }
  2228. /* CPU-specific MMC initializations */
  2229. __weak int cpu_mmc_init(bd_t *bis)
  2230. {
  2231. return -1;
  2232. }
  2233. /* board-specific MMC initializations. */
  2234. __weak int board_mmc_init(bd_t *bis)
  2235. {
  2236. return -1;
  2237. }
  2238. void mmc_set_preinit(struct mmc *mmc, int preinit)
  2239. {
  2240. mmc->preinit = preinit;
  2241. }
  2242. #if CONFIG_IS_ENABLED(DM_MMC) && defined(CONFIG_SPL_BUILD)
  2243. static int mmc_probe(bd_t *bis)
  2244. {
  2245. return 0;
  2246. }
  2247. #elif CONFIG_IS_ENABLED(DM_MMC)
  2248. static int mmc_probe(bd_t *bis)
  2249. {
  2250. int ret, i;
  2251. struct uclass *uc;
  2252. struct udevice *dev;
  2253. ret = uclass_get(UCLASS_MMC, &uc);
  2254. if (ret)
  2255. return ret;
  2256. /*
  2257. * Try to add them in sequence order. Really with driver model we
  2258. * should allow holes, but the current MMC list does not allow that.
  2259. * So if we request 0, 1, 3 we will get 0, 1, 2.
  2260. */
  2261. for (i = 0; ; i++) {
  2262. ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
  2263. if (ret == -ENODEV)
  2264. break;
  2265. }
  2266. uclass_foreach_dev(dev, uc) {
  2267. ret = device_probe(dev);
  2268. if (ret)
  2269. pr_err("%s - probe failed: %d\n", dev->name, ret);
  2270. }
  2271. return 0;
  2272. }
  2273. #else
  2274. static int mmc_probe(bd_t *bis)
  2275. {
  2276. if (board_mmc_init(bis) < 0)
  2277. cpu_mmc_init(bis);
  2278. return 0;
  2279. }
  2280. #endif
  2281. int mmc_initialize(bd_t *bis)
  2282. {
  2283. static int initialized = 0;
  2284. int ret;
  2285. if (initialized) /* Avoid initializing mmc multiple times */
  2286. return 0;
  2287. initialized = 1;
  2288. #if !CONFIG_IS_ENABLED(BLK)
  2289. #if !CONFIG_IS_ENABLED(MMC_TINY)
  2290. mmc_list_init();
  2291. #endif
  2292. #endif
  2293. ret = mmc_probe(bis);
  2294. if (ret)
  2295. return ret;
  2296. #ifndef CONFIG_SPL_BUILD
  2297. print_mmc_devices(',');
  2298. #endif
  2299. mmc_do_preinit();
  2300. return 0;
  2301. }
  2302. #ifdef CONFIG_CMD_BKOPS_ENABLE
  2303. int mmc_set_bkops_enable(struct mmc *mmc)
  2304. {
  2305. int err;
  2306. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  2307. err = mmc_send_ext_csd(mmc, ext_csd);
  2308. if (err) {
  2309. puts("Could not get ext_csd register values\n");
  2310. return err;
  2311. }
  2312. if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
  2313. puts("Background operations not supported on device\n");
  2314. return -EMEDIUMTYPE;
  2315. }
  2316. if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
  2317. puts("Background operations already enabled\n");
  2318. return 0;
  2319. }
  2320. err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
  2321. if (err) {
  2322. puts("Failed to enable manual background operations\n");
  2323. return err;
  2324. }
  2325. puts("Enabled manual background operations\n");
  2326. return 0;
  2327. }
  2328. #endif