mmc.c 56 KB

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