mmc.c 61 KB

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