mmc.c 61 KB

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