mmc.c 58 KB

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