fsl_esdhc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
  4. * Copyright 2019-2021 NXP
  5. * Andy Fleming
  6. *
  7. * Based vaguely on the pxa mmc code:
  8. * (C) Copyright 2003
  9. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <command.h>
  14. #include <cpu_func.h>
  15. #include <errno.h>
  16. #include <hwconfig.h>
  17. #include <mmc.h>
  18. #include <part.h>
  19. #include <malloc.h>
  20. #include <fsl_esdhc.h>
  21. #include <fdt_support.h>
  22. #include <asm/cache.h>
  23. #include <asm/global_data.h>
  24. #include <asm/io.h>
  25. #include <dm.h>
  26. #include <dm/device_compat.h>
  27. #include <linux/bitops.h>
  28. #include <linux/delay.h>
  29. #include <linux/iopoll.h>
  30. #include <linux/dma-mapping.h>
  31. #include <sdhci.h>
  32. #include "../../board/freescale/common/qixis.h"
  33. DECLARE_GLOBAL_DATA_PTR;
  34. struct fsl_esdhc {
  35. uint dsaddr; /* SDMA system address register */
  36. uint blkattr; /* Block attributes register */
  37. uint cmdarg; /* Command argument register */
  38. uint xfertyp; /* Transfer type register */
  39. uint cmdrsp0; /* Command response 0 register */
  40. uint cmdrsp1; /* Command response 1 register */
  41. uint cmdrsp2; /* Command response 2 register */
  42. uint cmdrsp3; /* Command response 3 register */
  43. uint datport; /* Buffer data port register */
  44. uint prsstat; /* Present state register */
  45. uint proctl; /* Protocol control register */
  46. uint sysctl; /* System Control Register */
  47. uint irqstat; /* Interrupt status register */
  48. uint irqstaten; /* Interrupt status enable register */
  49. uint irqsigen; /* Interrupt signal enable register */
  50. uint autoc12err; /* Auto CMD error status register */
  51. uint hostcapblt; /* Host controller capabilities register */
  52. uint wml; /* Watermark level register */
  53. char reserved1[8]; /* reserved */
  54. uint fevt; /* Force event register */
  55. uint admaes; /* ADMA error status register */
  56. uint adsaddrl; /* ADMA system address low register */
  57. uint adsaddrh; /* ADMA system address high register */
  58. char reserved2[156];
  59. uint hostver; /* Host controller version register */
  60. char reserved3[4]; /* reserved */
  61. uint dmaerraddr; /* DMA error address register */
  62. char reserved4[4]; /* reserved */
  63. uint dmaerrattr; /* DMA error attribute register */
  64. char reserved5[4]; /* reserved */
  65. uint hostcapblt2; /* Host controller capabilities register 2 */
  66. char reserved6[8]; /* reserved */
  67. uint tbctl; /* Tuning block control register */
  68. char reserved7[32]; /* reserved */
  69. uint sdclkctl; /* SD clock control register */
  70. uint sdtimingctl; /* SD timing control register */
  71. char reserved8[20]; /* reserved */
  72. uint dllcfg0; /* DLL config 0 register */
  73. uint dllcfg1; /* DLL config 1 register */
  74. char reserved9[8]; /* reserved */
  75. uint dllstat0; /* DLL status 0 register */
  76. char reserved10[664];/* reserved */
  77. uint esdhcctl; /* eSDHC control register */
  78. };
  79. struct fsl_esdhc_plat {
  80. struct mmc_config cfg;
  81. struct mmc mmc;
  82. };
  83. /**
  84. * struct fsl_esdhc_priv
  85. *
  86. * @esdhc_regs: registers of the sdhc controller
  87. * @sdhc_clk: Current clk of the sdhc controller
  88. * @bus_width: bus width, 1bit, 4bit or 8bit
  89. * @cfg: mmc config
  90. * @mmc: mmc
  91. * Following is used when Driver Model is enabled for MMC
  92. * @dev: pointer for the device
  93. * @cd_gpio: gpio for card detection
  94. * @wp_gpio: gpio for write protection
  95. */
  96. struct fsl_esdhc_priv {
  97. struct fsl_esdhc *esdhc_regs;
  98. unsigned int sdhc_clk;
  99. bool is_sdhc_per_clk;
  100. unsigned int clock;
  101. #if !CONFIG_IS_ENABLED(DM_MMC)
  102. struct mmc *mmc;
  103. #endif
  104. struct udevice *dev;
  105. struct sdhci_adma_desc *adma_desc_table;
  106. dma_addr_t dma_addr;
  107. };
  108. /* Return the XFERTYP flags for a given command and data packet */
  109. static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
  110. {
  111. uint xfertyp = 0;
  112. if (data) {
  113. xfertyp |= XFERTYP_DPSEL;
  114. if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) &&
  115. cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
  116. cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
  117. xfertyp |= XFERTYP_DMAEN;
  118. if (data->blocks > 1) {
  119. xfertyp |= XFERTYP_MSBSEL;
  120. xfertyp |= XFERTYP_BCEN;
  121. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111))
  122. xfertyp |= XFERTYP_AC12EN;
  123. }
  124. if (data->flags & MMC_DATA_READ)
  125. xfertyp |= XFERTYP_DTDSEL;
  126. }
  127. if (cmd->resp_type & MMC_RSP_CRC)
  128. xfertyp |= XFERTYP_CCCEN;
  129. if (cmd->resp_type & MMC_RSP_OPCODE)
  130. xfertyp |= XFERTYP_CICEN;
  131. if (cmd->resp_type & MMC_RSP_136)
  132. xfertyp |= XFERTYP_RSPTYP_136;
  133. else if (cmd->resp_type & MMC_RSP_BUSY)
  134. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  135. else if (cmd->resp_type & MMC_RSP_PRESENT)
  136. xfertyp |= XFERTYP_RSPTYP_48;
  137. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  138. xfertyp |= XFERTYP_CMDTYP_ABORT;
  139. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  140. }
  141. /*
  142. * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
  143. */
  144. static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
  145. struct mmc_data *data)
  146. {
  147. struct fsl_esdhc *regs = priv->esdhc_regs;
  148. uint blocks;
  149. char *buffer;
  150. uint databuf;
  151. uint size;
  152. uint irqstat;
  153. ulong start;
  154. if (data->flags & MMC_DATA_READ) {
  155. blocks = data->blocks;
  156. buffer = data->dest;
  157. while (blocks) {
  158. start = get_timer(0);
  159. size = data->blocksize;
  160. irqstat = esdhc_read32(&regs->irqstat);
  161. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
  162. if (get_timer(start) > PIO_TIMEOUT) {
  163. printf("\nData Read Failed in PIO Mode.");
  164. return;
  165. }
  166. }
  167. while (size && (!(irqstat & IRQSTAT_TC))) {
  168. udelay(100); /* Wait before last byte transfer complete */
  169. irqstat = esdhc_read32(&regs->irqstat);
  170. databuf = in_le32(&regs->datport);
  171. *((uint *)buffer) = databuf;
  172. buffer += 4;
  173. size -= 4;
  174. }
  175. blocks--;
  176. }
  177. } else {
  178. blocks = data->blocks;
  179. buffer = (char *)data->src;
  180. while (blocks) {
  181. start = get_timer(0);
  182. size = data->blocksize;
  183. irqstat = esdhc_read32(&regs->irqstat);
  184. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
  185. if (get_timer(start) > PIO_TIMEOUT) {
  186. printf("\nData Write Failed in PIO Mode.");
  187. return;
  188. }
  189. }
  190. while (size && (!(irqstat & IRQSTAT_TC))) {
  191. udelay(100); /* Wait before last byte transfer complete */
  192. databuf = *((uint *)buffer);
  193. buffer += 4;
  194. size -= 4;
  195. irqstat = esdhc_read32(&regs->irqstat);
  196. out_le32(&regs->datport, databuf);
  197. }
  198. blocks--;
  199. }
  200. }
  201. }
  202. static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv,
  203. struct mmc_data *data)
  204. {
  205. struct fsl_esdhc *regs = priv->esdhc_regs;
  206. uint wml_value = data->blocksize / 4;
  207. if (data->flags & MMC_DATA_READ) {
  208. if (wml_value > WML_RD_WML_MAX)
  209. wml_value = WML_RD_WML_MAX_VAL;
  210. esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
  211. } else {
  212. if (wml_value > WML_WR_WML_MAX)
  213. wml_value = WML_WR_WML_MAX_VAL;
  214. esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
  215. wml_value << 16);
  216. }
  217. }
  218. static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data)
  219. {
  220. uint trans_bytes = data->blocksize * data->blocks;
  221. struct fsl_esdhc *regs = priv->esdhc_regs;
  222. phys_addr_t adma_addr;
  223. void *buf;
  224. if (data->flags & MMC_DATA_WRITE)
  225. buf = (void *)data->src;
  226. else
  227. buf = data->dest;
  228. priv->dma_addr = dma_map_single(buf, trans_bytes,
  229. mmc_get_dma_dir(data));
  230. if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) &&
  231. priv->adma_desc_table) {
  232. debug("Using ADMA2\n");
  233. /* prefer ADMA2 if it is available */
  234. sdhci_prepare_adma_table(priv->adma_desc_table, data,
  235. priv->dma_addr);
  236. adma_addr = virt_to_phys(priv->adma_desc_table);
  237. esdhc_write32(&regs->adsaddrl, lower_32_bits(adma_addr));
  238. if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT))
  239. esdhc_write32(&regs->adsaddrh, upper_32_bits(adma_addr));
  240. esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
  241. PROCTL_DMAS_ADMA2);
  242. } else {
  243. debug("Using SDMA\n");
  244. if (upper_32_bits(priv->dma_addr))
  245. printf("Cannot use 64 bit addresses with SDMA\n");
  246. esdhc_write32(&regs->dsaddr, lower_32_bits(priv->dma_addr));
  247. esdhc_clrsetbits32(&regs->proctl, PROCTL_DMAS_MASK,
  248. PROCTL_DMAS_SDMA);
  249. }
  250. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  251. }
  252. static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
  253. struct mmc_data *data)
  254. {
  255. int timeout;
  256. bool is_write = data->flags & MMC_DATA_WRITE;
  257. struct fsl_esdhc *regs = priv->esdhc_regs;
  258. if (is_write && !(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
  259. printf("Can not write to locked SD card.\n");
  260. return -EINVAL;
  261. }
  262. if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO))
  263. esdhc_setup_watermark_level(priv, data);
  264. else
  265. esdhc_setup_dma(priv, data);
  266. /* Calculate the timeout period for data transactions */
  267. /*
  268. * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
  269. * 2)Timeout period should be minimum 0.250sec as per SD Card spec
  270. * So, Number of SD Clock cycles for 0.25sec should be minimum
  271. * (SD Clock/sec * 0.25 sec) SD Clock cycles
  272. * = (mmc->clock * 1/4) SD Clock cycles
  273. * As 1) >= 2)
  274. * => (2^(timeout+13)) >= mmc->clock * 1/4
  275. * Taking log2 both the sides
  276. * => timeout + 13 >= log2(mmc->clock/4)
  277. * Rounding up to next power of 2
  278. * => timeout + 13 = log2(mmc->clock/4) + 1
  279. * => timeout + 13 = fls(mmc->clock/4)
  280. *
  281. * However, the MMC spec "It is strongly recommended for hosts to
  282. * implement more than 500ms timeout value even if the card
  283. * indicates the 250ms maximum busy length." Even the previous
  284. * value of 300ms is known to be insufficient for some cards.
  285. * So, we use
  286. * => timeout + 13 = fls(mmc->clock/2)
  287. */
  288. timeout = fls(mmc->clock/2);
  289. timeout -= 13;
  290. if (timeout > 14)
  291. timeout = 14;
  292. if (timeout < 0)
  293. timeout = 0;
  294. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) &&
  295. (timeout == 4 || timeout == 8 || timeout == 12))
  296. timeout++;
  297. if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE))
  298. timeout = 0xE;
  299. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  300. return 0;
  301. }
  302. /*
  303. * Sends a command out on the bus. Takes the mmc pointer,
  304. * a command pointer, and an optional data pointer.
  305. */
  306. static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
  307. struct mmc_cmd *cmd, struct mmc_data *data)
  308. {
  309. int err = 0;
  310. uint xfertyp;
  311. uint irqstat;
  312. u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
  313. struct fsl_esdhc *regs = priv->esdhc_regs;
  314. unsigned long start;
  315. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) &&
  316. cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  317. return 0;
  318. esdhc_write32(&regs->irqstat, -1);
  319. sync();
  320. /* Wait for the bus to be idle */
  321. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  322. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  323. ;
  324. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  325. ;
  326. /* Set up for a data transfer if we have one */
  327. if (data) {
  328. err = esdhc_setup_data(priv, mmc, data);
  329. if(err)
  330. return err;
  331. }
  332. /* Figure out the transfer arguments */
  333. xfertyp = esdhc_xfertyp(cmd, data);
  334. /* Mask all irqs */
  335. esdhc_write32(&regs->irqsigen, 0);
  336. /* Send the command */
  337. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  338. esdhc_write32(&regs->xfertyp, xfertyp);
  339. if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
  340. cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
  341. flags = IRQSTAT_BRR;
  342. /* Wait for the command to complete */
  343. start = get_timer(0);
  344. while (!(esdhc_read32(&regs->irqstat) & flags)) {
  345. if (get_timer(start) > 1000) {
  346. err = -ETIMEDOUT;
  347. goto out;
  348. }
  349. }
  350. irqstat = esdhc_read32(&regs->irqstat);
  351. if (irqstat & CMD_ERR) {
  352. err = -ECOMM;
  353. goto out;
  354. }
  355. if (irqstat & IRQSTAT_CTOE) {
  356. err = -ETIMEDOUT;
  357. goto out;
  358. }
  359. /* Workaround for ESDHC errata ENGcm03648 */
  360. if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
  361. int timeout = 6000;
  362. /* Poll on DATA0 line for cmd with busy signal for 600 ms */
  363. while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
  364. PRSSTAT_DAT0)) {
  365. udelay(100);
  366. timeout--;
  367. }
  368. if (timeout <= 0) {
  369. printf("Timeout waiting for DAT0 to go high!\n");
  370. err = -ETIMEDOUT;
  371. goto out;
  372. }
  373. }
  374. /* Copy the response to the response buffer */
  375. if (cmd->resp_type & MMC_RSP_136) {
  376. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  377. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  378. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  379. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  380. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  381. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  382. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  383. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  384. cmd->response[3] = (cmdrsp0 << 8);
  385. } else
  386. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  387. /* Wait until all of the blocks are transferred */
  388. if (data) {
  389. if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) {
  390. esdhc_pio_read_write(priv, data);
  391. } else {
  392. flags = DATA_COMPLETE;
  393. if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
  394. cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
  395. flags = IRQSTAT_BRR;
  396. do {
  397. irqstat = esdhc_read32(&regs->irqstat);
  398. if (irqstat & IRQSTAT_DTOE) {
  399. err = -ETIMEDOUT;
  400. goto out;
  401. }
  402. if (irqstat & DATA_ERR) {
  403. err = -ECOMM;
  404. goto out;
  405. }
  406. } while ((irqstat & flags) != flags);
  407. /*
  408. * Need invalidate the dcache here again to avoid any
  409. * cache-fill during the DMA operations such as the
  410. * speculative pre-fetching etc.
  411. */
  412. dma_unmap_single(priv->dma_addr,
  413. data->blocks * data->blocksize,
  414. mmc_get_dma_dir(data));
  415. }
  416. }
  417. out:
  418. /* Reset CMD and DATA portions on error */
  419. if (err) {
  420. esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
  421. SYSCTL_RSTC);
  422. while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
  423. ;
  424. if (data) {
  425. esdhc_write32(&regs->sysctl,
  426. esdhc_read32(&regs->sysctl) |
  427. SYSCTL_RSTD);
  428. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
  429. ;
  430. }
  431. }
  432. esdhc_write32(&regs->irqstat, -1);
  433. return err;
  434. }
  435. static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
  436. {
  437. struct fsl_esdhc *regs = priv->esdhc_regs;
  438. int div = 1;
  439. int pre_div = 2;
  440. unsigned int sdhc_clk = priv->sdhc_clk;
  441. u32 time_out;
  442. u32 value;
  443. uint clk;
  444. u32 hostver;
  445. if (clock < mmc->cfg->f_min)
  446. clock = mmc->cfg->f_min;
  447. while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
  448. pre_div *= 2;
  449. while (sdhc_clk / (div * pre_div) > clock && div < 16)
  450. div++;
  451. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
  452. clock == 200000000 && mmc->selected_mode == MMC_HS_400) {
  453. u32 div_ratio = pre_div * div;
  454. if (div_ratio <= 4) {
  455. pre_div = 4;
  456. div = 1;
  457. } else if (div_ratio <= 8) {
  458. pre_div = 4;
  459. div = 2;
  460. } else if (div_ratio <= 12) {
  461. pre_div = 4;
  462. div = 3;
  463. } else {
  464. printf("unsupported clock division.\n");
  465. }
  466. }
  467. mmc->clock = sdhc_clk / pre_div / div;
  468. priv->clock = mmc->clock;
  469. pre_div >>= 1;
  470. div -= 1;
  471. clk = (pre_div << 8) | (div << 4);
  472. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  473. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  474. /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */
  475. hostver = esdhc_read32(&priv->esdhc_regs->hostver);
  476. if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) {
  477. udelay(10000);
  478. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
  479. return;
  480. }
  481. time_out = 20;
  482. value = PRSSTAT_SDSTB;
  483. while (!(esdhc_read32(&regs->prsstat) & value)) {
  484. if (time_out == 0) {
  485. printf("fsl_esdhc: Internal clock never stabilised.\n");
  486. break;
  487. }
  488. time_out--;
  489. mdelay(1);
  490. }
  491. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
  492. }
  493. static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
  494. {
  495. struct fsl_esdhc *regs = priv->esdhc_regs;
  496. u32 value;
  497. u32 time_out;
  498. u32 hostver;
  499. value = esdhc_read32(&regs->sysctl);
  500. if (enable)
  501. value |= SYSCTL_CKEN;
  502. else
  503. value &= ~SYSCTL_CKEN;
  504. esdhc_write32(&regs->sysctl, value);
  505. /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */
  506. hostver = esdhc_read32(&priv->esdhc_regs->hostver);
  507. if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) {
  508. udelay(10000);
  509. return;
  510. }
  511. time_out = 20;
  512. value = PRSSTAT_SDSTB;
  513. while (!(esdhc_read32(&regs->prsstat) & value)) {
  514. if (time_out == 0) {
  515. printf("fsl_esdhc: Internal clock never stabilised.\n");
  516. break;
  517. }
  518. time_out--;
  519. mdelay(1);
  520. }
  521. }
  522. static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
  523. {
  524. struct fsl_esdhc *regs = priv->esdhc_regs;
  525. u32 time_out;
  526. esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_FAF);
  527. time_out = 20;
  528. while (esdhc_read32(&regs->esdhcctl) & ESDHCCTL_FAF) {
  529. if (time_out == 0) {
  530. printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
  531. break;
  532. }
  533. time_out--;
  534. mdelay(1);
  535. }
  536. }
  537. static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
  538. bool en)
  539. {
  540. struct fsl_esdhc *regs = priv->esdhc_regs;
  541. esdhc_clock_control(priv, false);
  542. esdhc_flush_async_fifo(priv);
  543. if (en)
  544. esdhc_setbits32(&regs->tbctl, TBCTL_TB_EN);
  545. else
  546. esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
  547. esdhc_clock_control(priv, true);
  548. }
  549. static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv)
  550. {
  551. struct fsl_esdhc *regs = priv->esdhc_regs;
  552. esdhc_clrbits32(&regs->sdtimingctl, FLW_CTL_BG);
  553. esdhc_clrbits32(&regs->sdclkctl, CMD_CLK_CTL);
  554. esdhc_clock_control(priv, false);
  555. esdhc_clrbits32(&regs->tbctl, HS400_MODE);
  556. esdhc_clock_control(priv, true);
  557. esdhc_clrbits32(&regs->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE);
  558. esdhc_clrbits32(&regs->tbctl, HS400_WNDW_ADJUST);
  559. esdhc_tuning_block_enable(priv, false);
  560. }
  561. static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
  562. {
  563. struct fsl_esdhc *regs = priv->esdhc_regs;
  564. ulong start;
  565. u32 val;
  566. /* Exit HS400 mode before setting any other mode */
  567. if (esdhc_read32(&regs->tbctl) & HS400_MODE &&
  568. mode != MMC_HS_400)
  569. esdhc_exit_hs400(priv);
  570. esdhc_clock_control(priv, false);
  571. if (mode == MMC_HS_200)
  572. esdhc_clrsetbits32(&regs->autoc12err, UHSM_MASK,
  573. UHSM_SDR104_HS200);
  574. if (mode == MMC_HS_400) {
  575. esdhc_setbits32(&regs->tbctl, HS400_MODE);
  576. esdhc_setbits32(&regs->sdclkctl, CMD_CLK_CTL);
  577. esdhc_clock_control(priv, true);
  578. if (priv->clock == 200000000)
  579. esdhc_setbits32(&regs->dllcfg0, DLL_FREQ_SEL);
  580. esdhc_setbits32(&regs->dllcfg0, DLL_ENABLE);
  581. esdhc_setbits32(&regs->dllcfg0, DLL_RESET);
  582. udelay(1);
  583. esdhc_clrbits32(&regs->dllcfg0, DLL_RESET);
  584. start = get_timer(0);
  585. val = DLL_STS_SLV_LOCK;
  586. while (!(esdhc_read32(&regs->dllstat0) & val)) {
  587. if (get_timer(start) > 1000) {
  588. printf("fsl_esdhc: delay chain lock timeout\n");
  589. return -ETIMEDOUT;
  590. }
  591. }
  592. esdhc_setbits32(&regs->tbctl, HS400_WNDW_ADJUST);
  593. esdhc_clock_control(priv, false);
  594. esdhc_flush_async_fifo(priv);
  595. }
  596. esdhc_clock_control(priv, true);
  597. return 0;
  598. }
  599. static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
  600. {
  601. struct fsl_esdhc *regs = priv->esdhc_regs;
  602. int ret;
  603. if (priv->is_sdhc_per_clk) {
  604. /* Select to use peripheral clock */
  605. esdhc_clock_control(priv, false);
  606. esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
  607. esdhc_clock_control(priv, true);
  608. }
  609. if (mmc->selected_mode == MMC_HS_400)
  610. esdhc_tuning_block_enable(priv, true);
  611. /* Set the clock speed */
  612. if (priv->clock != mmc->clock)
  613. set_sysctl(priv, mmc, mmc->clock);
  614. /* Set timing */
  615. ret = esdhc_set_timing(priv, mmc->selected_mode);
  616. if (ret)
  617. return ret;
  618. /* Set the bus width */
  619. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  620. if (mmc->bus_width == 4)
  621. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  622. else if (mmc->bus_width == 8)
  623. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  624. return 0;
  625. }
  626. static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
  627. {
  628. #ifdef CONFIG_ARCH_MPC830X
  629. immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  630. sysconf83xx_t *sysconf = &immr->sysconf;
  631. setbits_be32(&sysconf->sdhccr, 0x02000000);
  632. #else
  633. esdhc_write32(&regs->esdhcctl, ESDHCCTL_SNOOP);
  634. #endif
  635. }
  636. static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
  637. {
  638. struct fsl_esdhc *regs = priv->esdhc_regs;
  639. ulong start;
  640. /* Reset the entire host controller */
  641. esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
  642. /* Wait until the controller is available */
  643. start = get_timer(0);
  644. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
  645. if (get_timer(start) > 1000)
  646. return -ETIMEDOUT;
  647. }
  648. /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */
  649. esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
  650. esdhc_enable_cache_snooping(regs);
  651. esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  652. /* Set the initial clock speed */
  653. set_sysctl(priv, mmc, 400000);
  654. /* Disable the BRR and BWR bits in IRQSTAT */
  655. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  656. /* Put the PROCTL reg back to the default */
  657. esdhc_write32(&regs->proctl, PROCTL_INIT);
  658. /* Set timout to the maximum value */
  659. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  660. if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_UNRELIABLE_PULSE_DETECTION_WORKAROUND))
  661. esdhc_clrbits32(&regs->dllcfg1, DLL_PD_PULSE_STRETCH_SEL);
  662. return 0;
  663. }
  664. static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
  665. {
  666. struct fsl_esdhc *regs = priv->esdhc_regs;
  667. #ifdef CONFIG_ESDHC_DETECT_QUIRK
  668. if (qixis_esdhc_detect_quirk())
  669. return 1;
  670. #endif
  671. if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
  672. return 1;
  673. return 0;
  674. }
  675. static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
  676. struct mmc_config *cfg)
  677. {
  678. struct fsl_esdhc *regs = priv->esdhc_regs;
  679. u32 caps;
  680. caps = esdhc_read32(&regs->hostcapblt);
  681. /*
  682. * For eSDHC, power supply is through peripheral circuit. Some eSDHC
  683. * versions have value 0 of the bit but that does not reflect the
  684. * truth. 3.3V is common for SD/MMC, and is supported for all boards
  685. * with eSDHC in current u-boot. So, make 3.3V is supported in
  686. * default in code. CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT can be enabled
  687. * if future board does not support 3.3V.
  688. */
  689. caps |= HOSTCAPBLT_VS33;
  690. if (IS_ENABLED(CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT))
  691. caps &= ~HOSTCAPBLT_VS33;
  692. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135))
  693. caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
  694. if (caps & HOSTCAPBLT_VS18)
  695. cfg->voltages |= MMC_VDD_165_195;
  696. if (caps & HOSTCAPBLT_VS30)
  697. cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
  698. if (caps & HOSTCAPBLT_VS33)
  699. cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
  700. cfg->name = "FSL_SDHC";
  701. if (caps & HOSTCAPBLT_HSS)
  702. cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  703. cfg->f_min = 400000;
  704. cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
  705. cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  706. }
  707. #ifdef CONFIG_OF_LIBFDT
  708. __weak int esdhc_status_fixup(void *blob, const char *compat)
  709. {
  710. if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) {
  711. do_fixup_by_compat(blob, compat, "status", "disabled",
  712. sizeof("disabled"), 1);
  713. return 1;
  714. }
  715. return 0;
  716. }
  717. #if CONFIG_IS_ENABLED(DM_MMC)
  718. static int fsl_esdhc_get_cd(struct udevice *dev);
  719. static void esdhc_disable_for_no_card(void *blob)
  720. {
  721. struct udevice *dev;
  722. for (uclass_first_device(UCLASS_MMC, &dev);
  723. dev;
  724. uclass_next_device(&dev)) {
  725. char esdhc_path[50];
  726. if (fsl_esdhc_get_cd(dev))
  727. continue;
  728. snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
  729. (unsigned long)dev_read_addr(dev));
  730. do_fixup_by_path(blob, esdhc_path, "status", "disabled",
  731. sizeof("disabled"), 1);
  732. }
  733. }
  734. #else
  735. static void esdhc_disable_for_no_card(void *blob)
  736. {
  737. }
  738. #endif
  739. void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
  740. {
  741. const char *compat = "fsl,esdhc";
  742. if (esdhc_status_fixup(blob, compat))
  743. return;
  744. if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND))
  745. esdhc_disable_for_no_card(blob);
  746. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  747. gd->arch.sdhc_clk, 1);
  748. }
  749. #endif
  750. #if !CONFIG_IS_ENABLED(DM_MMC)
  751. static int esdhc_getcd(struct mmc *mmc)
  752. {
  753. struct fsl_esdhc_priv *priv = mmc->priv;
  754. return esdhc_getcd_common(priv);
  755. }
  756. static int esdhc_init(struct mmc *mmc)
  757. {
  758. struct fsl_esdhc_priv *priv = mmc->priv;
  759. return esdhc_init_common(priv, mmc);
  760. }
  761. static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  762. struct mmc_data *data)
  763. {
  764. struct fsl_esdhc_priv *priv = mmc->priv;
  765. return esdhc_send_cmd_common(priv, mmc, cmd, data);
  766. }
  767. static int esdhc_set_ios(struct mmc *mmc)
  768. {
  769. struct fsl_esdhc_priv *priv = mmc->priv;
  770. return esdhc_set_ios_common(priv, mmc);
  771. }
  772. static const struct mmc_ops esdhc_ops = {
  773. .getcd = esdhc_getcd,
  774. .init = esdhc_init,
  775. .send_cmd = esdhc_send_cmd,
  776. .set_ios = esdhc_set_ios,
  777. };
  778. int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
  779. {
  780. struct fsl_esdhc_plat *plat;
  781. struct fsl_esdhc_priv *priv;
  782. struct mmc_config *mmc_cfg;
  783. struct mmc *mmc;
  784. if (!cfg)
  785. return -EINVAL;
  786. priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
  787. if (!priv)
  788. return -ENOMEM;
  789. plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
  790. if (!plat) {
  791. free(priv);
  792. return -ENOMEM;
  793. }
  794. priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
  795. priv->sdhc_clk = cfg->sdhc_clk;
  796. if (gd->arch.sdhc_per_clk)
  797. priv->is_sdhc_per_clk = true;
  798. mmc_cfg = &plat->cfg;
  799. if (cfg->max_bus_width == 8) {
  800. mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
  801. MMC_MODE_8BIT;
  802. } else if (cfg->max_bus_width == 4) {
  803. mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
  804. } else if (cfg->max_bus_width == 1) {
  805. mmc_cfg->host_caps |= MMC_MODE_1BIT;
  806. } else {
  807. mmc_cfg->host_caps |= MMC_MODE_1BIT;
  808. printf("No max bus width provided. Fallback to 1-bit mode.\n");
  809. }
  810. if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK))
  811. mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
  812. mmc_cfg->ops = &esdhc_ops;
  813. fsl_esdhc_get_cfg_common(priv, mmc_cfg);
  814. mmc = mmc_create(mmc_cfg, priv);
  815. if (!mmc)
  816. return -EIO;
  817. priv->mmc = mmc;
  818. return 0;
  819. }
  820. int fsl_esdhc_mmc_init(struct bd_info *bis)
  821. {
  822. struct fsl_esdhc_cfg *cfg;
  823. cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
  824. cfg->esdhc_base = CFG_SYS_FSL_ESDHC_ADDR;
  825. cfg->max_bus_width = CONFIG_SYS_FSL_ESDHC_DEFAULT_BUS_WIDTH;
  826. /* Prefer peripheral clock which provides higher frequency. */
  827. if (gd->arch.sdhc_per_clk)
  828. cfg->sdhc_clk = gd->arch.sdhc_per_clk;
  829. else
  830. cfg->sdhc_clk = gd->arch.sdhc_clk;
  831. return fsl_esdhc_initialize(bis, cfg);
  832. }
  833. #else /* DM_MMC */
  834. static int fsl_esdhc_probe(struct udevice *dev)
  835. {
  836. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  837. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  838. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  839. u32 caps, hostver;
  840. fdt_addr_t addr;
  841. struct mmc *mmc;
  842. int ret;
  843. addr = dev_read_addr(dev);
  844. if (addr == FDT_ADDR_T_NONE)
  845. return -EINVAL;
  846. #ifdef CONFIG_PPC
  847. priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
  848. #else
  849. priv->esdhc_regs = (struct fsl_esdhc *)addr;
  850. #endif
  851. priv->dev = dev;
  852. if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) {
  853. /*
  854. * Only newer eSDHC controllers can do ADMA2 if the ADMA flag
  855. * is set in the host capabilities register.
  856. */
  857. caps = esdhc_read32(&priv->esdhc_regs->hostcapblt);
  858. hostver = esdhc_read32(&priv->esdhc_regs->hostver);
  859. if (caps & HOSTCAPBLT_DMAS &&
  860. HOSTVER_VENDOR(hostver) > VENDOR_V_22) {
  861. priv->adma_desc_table = sdhci_adma_init();
  862. if (!priv->adma_desc_table)
  863. debug("Could not allocate ADMA tables, falling back to SDMA\n");
  864. }
  865. }
  866. if (gd->arch.sdhc_per_clk) {
  867. priv->sdhc_clk = gd->arch.sdhc_per_clk;
  868. priv->is_sdhc_per_clk = true;
  869. } else {
  870. priv->sdhc_clk = gd->arch.sdhc_clk;
  871. }
  872. if (priv->sdhc_clk <= 0) {
  873. dev_err(dev, "Unable to get clk for %s\n", dev->name);
  874. return -EINVAL;
  875. }
  876. fsl_esdhc_get_cfg_common(priv, &plat->cfg);
  877. mmc_of_parse(dev, &plat->cfg);
  878. mmc = &plat->mmc;
  879. mmc->cfg = &plat->cfg;
  880. mmc->dev = dev;
  881. upriv->mmc = mmc;
  882. ret = esdhc_init_common(priv, mmc);
  883. if (ret)
  884. return ret;
  885. if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) &&
  886. !fsl_esdhc_get_cd(dev))
  887. esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
  888. return 0;
  889. }
  890. static int fsl_esdhc_get_cd(struct udevice *dev)
  891. {
  892. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  893. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  894. if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
  895. return 1;
  896. return esdhc_getcd_common(priv);
  897. }
  898. static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  899. struct mmc_data *data)
  900. {
  901. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  902. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  903. return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
  904. }
  905. static int fsl_esdhc_set_ios(struct udevice *dev)
  906. {
  907. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  908. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  909. return esdhc_set_ios_common(priv, &plat->mmc);
  910. }
  911. static int fsl_esdhc_reinit(struct udevice *dev)
  912. {
  913. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  914. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  915. return esdhc_init_common(priv, &plat->mmc);
  916. }
  917. #ifdef MMC_SUPPORTS_TUNING
  918. static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
  919. {
  920. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  921. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  922. struct fsl_esdhc *regs = priv->esdhc_regs;
  923. struct mmc *mmc = &plat->mmc;
  924. u32 val, irqstaten;
  925. int i;
  926. if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) &&
  927. plat->mmc.hs400_tuning)
  928. set_sysctl(priv, mmc, mmc->clock);
  929. esdhc_tuning_block_enable(priv, true);
  930. esdhc_setbits32(&regs->autoc12err, EXECUTE_TUNING);
  931. irqstaten = esdhc_read32(&regs->irqstaten);
  932. esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
  933. for (i = 0; i < MAX_TUNING_LOOP; i++) {
  934. mmc_send_tuning(mmc, opcode, NULL);
  935. mdelay(1);
  936. val = esdhc_read32(&regs->autoc12err);
  937. if (!(val & EXECUTE_TUNING)) {
  938. if (val & SMPCLKSEL)
  939. break;
  940. }
  941. }
  942. esdhc_write32(&regs->irqstaten, irqstaten);
  943. if (i != MAX_TUNING_LOOP) {
  944. if (plat->mmc.hs400_tuning)
  945. esdhc_setbits32(&regs->sdtimingctl, FLW_CTL_BG);
  946. return 0;
  947. }
  948. printf("fsl_esdhc: tuning failed!\n");
  949. esdhc_clrbits32(&regs->autoc12err, SMPCLKSEL);
  950. esdhc_clrbits32(&regs->autoc12err, EXECUTE_TUNING);
  951. esdhc_tuning_block_enable(priv, false);
  952. return -ETIMEDOUT;
  953. }
  954. #endif
  955. int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev)
  956. {
  957. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  958. esdhc_tuning_block_enable(priv, false);
  959. return 0;
  960. }
  961. static int fsl_esdhc_wait_dat0(struct udevice *dev, int state,
  962. int timeout_us)
  963. {
  964. int ret;
  965. u32 tmp;
  966. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  967. struct fsl_esdhc *regs = priv->esdhc_regs;
  968. ret = readx_poll_timeout(esdhc_read32, &regs->prsstat, tmp,
  969. !!(tmp & PRSSTAT_DAT0) == !!state,
  970. timeout_us);
  971. return ret;
  972. }
  973. static const struct dm_mmc_ops fsl_esdhc_ops = {
  974. .get_cd = fsl_esdhc_get_cd,
  975. .send_cmd = fsl_esdhc_send_cmd,
  976. .set_ios = fsl_esdhc_set_ios,
  977. #ifdef MMC_SUPPORTS_TUNING
  978. .execute_tuning = fsl_esdhc_execute_tuning,
  979. #endif
  980. .reinit = fsl_esdhc_reinit,
  981. .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr,
  982. .wait_dat0 = fsl_esdhc_wait_dat0,
  983. };
  984. static const struct udevice_id fsl_esdhc_ids[] = {
  985. { .compatible = "fsl,esdhc", },
  986. { /* sentinel */ }
  987. };
  988. static int fsl_esdhc_bind(struct udevice *dev)
  989. {
  990. struct fsl_esdhc_plat *plat = dev_get_plat(dev);
  991. return mmc_bind(dev, &plat->mmc, &plat->cfg);
  992. }
  993. U_BOOT_DRIVER(fsl_esdhc) = {
  994. .name = "fsl-esdhc-mmc",
  995. .id = UCLASS_MMC,
  996. .of_match = fsl_esdhc_ids,
  997. .ops = &fsl_esdhc_ops,
  998. .bind = fsl_esdhc_bind,
  999. .probe = fsl_esdhc_probe,
  1000. .plat_auto = sizeof(struct fsl_esdhc_plat),
  1001. .priv_auto = sizeof(struct fsl_esdhc_priv),
  1002. };
  1003. #endif