mmc.c 72 KB

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