mmc.c 63 KB

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