mmc.c 60 KB

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