fsl_esdhc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
  3. * Andy Fleming
  4. *
  5. * Based vaguely on the pxa mmc code:
  6. * (C) Copyright 2003
  7. * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <command.h>
  14. #include <errno.h>
  15. #include <hwconfig.h>
  16. #include <mmc.h>
  17. #include <part.h>
  18. #include <malloc.h>
  19. #include <fsl_esdhc.h>
  20. #include <fdt_support.h>
  21. #include <asm/io.h>
  22. #include <dm.h>
  23. #include <asm-generic/gpio.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
  26. IRQSTATEN_CINT | \
  27. IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
  28. IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
  29. IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
  30. IRQSTATEN_DINT)
  31. struct fsl_esdhc {
  32. uint dsaddr; /* SDMA system address register */
  33. uint blkattr; /* Block attributes register */
  34. uint cmdarg; /* Command argument register */
  35. uint xfertyp; /* Transfer type register */
  36. uint cmdrsp0; /* Command response 0 register */
  37. uint cmdrsp1; /* Command response 1 register */
  38. uint cmdrsp2; /* Command response 2 register */
  39. uint cmdrsp3; /* Command response 3 register */
  40. uint datport; /* Buffer data port register */
  41. uint prsstat; /* Present state register */
  42. uint proctl; /* Protocol control register */
  43. uint sysctl; /* System Control Register */
  44. uint irqstat; /* Interrupt status register */
  45. uint irqstaten; /* Interrupt status enable register */
  46. uint irqsigen; /* Interrupt signal enable register */
  47. uint autoc12err; /* Auto CMD error status register */
  48. uint hostcapblt; /* Host controller capabilities register */
  49. uint wml; /* Watermark level register */
  50. uint mixctrl; /* For USDHC */
  51. char reserved1[4]; /* reserved */
  52. uint fevt; /* Force event register */
  53. uint admaes; /* ADMA error status register */
  54. uint adsaddr; /* ADMA system address register */
  55. char reserved2[4];
  56. uint dllctrl;
  57. uint dllstat;
  58. uint clktunectrlstatus;
  59. char reserved3[84];
  60. uint vendorspec;
  61. uint mmcboot;
  62. uint vendorspec2;
  63. char reserved4[48];
  64. uint hostver; /* Host controller version register */
  65. char reserved5[4]; /* reserved */
  66. uint dmaerraddr; /* DMA error address register */
  67. char reserved6[4]; /* reserved */
  68. uint dmaerrattr; /* DMA error attribute register */
  69. char reserved7[4]; /* reserved */
  70. uint hostcapblt2; /* Host controller capabilities register 2 */
  71. char reserved8[8]; /* reserved */
  72. uint tcr; /* Tuning control register */
  73. char reserved9[28]; /* reserved */
  74. uint sddirctl; /* SD direction control register */
  75. char reserved10[712];/* reserved */
  76. uint scr; /* eSDHC control register */
  77. };
  78. /**
  79. * struct fsl_esdhc_priv
  80. *
  81. * @esdhc_regs: registers of the sdhc controller
  82. * @sdhc_clk: Current clk of the sdhc controller
  83. * @bus_width: bus width, 1bit, 4bit or 8bit
  84. * @cfg: mmc config
  85. * @mmc: mmc
  86. * Following is used when Driver Model is enabled for MMC
  87. * @dev: pointer for the device
  88. * @non_removable: 0: removable; 1: non-removable
  89. * @wp_enable: 1: enable checking wp; 0: no check
  90. * @cd_gpio: gpio for card detection
  91. * @wp_gpio: gpio for write protection
  92. */
  93. struct fsl_esdhc_priv {
  94. struct fsl_esdhc *esdhc_regs;
  95. unsigned int sdhc_clk;
  96. unsigned int bus_width;
  97. struct mmc_config cfg;
  98. struct mmc *mmc;
  99. struct udevice *dev;
  100. int non_removable;
  101. int wp_enable;
  102. #ifdef CONFIG_DM_GPIO
  103. struct gpio_desc cd_gpio;
  104. struct gpio_desc wp_gpio;
  105. #endif
  106. };
  107. /* Return the XFERTYP flags for a given command and data packet */
  108. static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
  109. {
  110. uint xfertyp = 0;
  111. if (data) {
  112. xfertyp |= XFERTYP_DPSEL;
  113. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  114. xfertyp |= XFERTYP_DMAEN;
  115. #endif
  116. if (data->blocks > 1) {
  117. xfertyp |= XFERTYP_MSBSEL;
  118. xfertyp |= XFERTYP_BCEN;
  119. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  120. xfertyp |= XFERTYP_AC12EN;
  121. #endif
  122. }
  123. if (data->flags & MMC_DATA_READ)
  124. xfertyp |= XFERTYP_DTDSEL;
  125. }
  126. if (cmd->resp_type & MMC_RSP_CRC)
  127. xfertyp |= XFERTYP_CCCEN;
  128. if (cmd->resp_type & MMC_RSP_OPCODE)
  129. xfertyp |= XFERTYP_CICEN;
  130. if (cmd->resp_type & MMC_RSP_136)
  131. xfertyp |= XFERTYP_RSPTYP_136;
  132. else if (cmd->resp_type & MMC_RSP_BUSY)
  133. xfertyp |= XFERTYP_RSPTYP_48_BUSY;
  134. else if (cmd->resp_type & MMC_RSP_PRESENT)
  135. xfertyp |= XFERTYP_RSPTYP_48;
  136. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  137. xfertyp |= XFERTYP_CMDTYP_ABORT;
  138. return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
  139. }
  140. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  141. /*
  142. * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
  143. */
  144. static void
  145. esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
  146. {
  147. struct fsl_esdhc_priv *priv = mmc->priv;
  148. struct fsl_esdhc *regs = priv->esdhc_regs;
  149. uint blocks;
  150. char *buffer;
  151. uint databuf;
  152. uint size;
  153. uint irqstat;
  154. uint timeout;
  155. if (data->flags & MMC_DATA_READ) {
  156. blocks = data->blocks;
  157. buffer = data->dest;
  158. while (blocks) {
  159. timeout = PIO_TIMEOUT;
  160. size = data->blocksize;
  161. irqstat = esdhc_read32(&regs->irqstat);
  162. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
  163. && --timeout);
  164. if (timeout <= 0) {
  165. printf("\nData Read Failed in PIO Mode.");
  166. return;
  167. }
  168. while (size && (!(irqstat & IRQSTAT_TC))) {
  169. udelay(100); /* Wait before last byte transfer complete */
  170. irqstat = esdhc_read32(&regs->irqstat);
  171. databuf = in_le32(&regs->datport);
  172. *((uint *)buffer) = databuf;
  173. buffer += 4;
  174. size -= 4;
  175. }
  176. blocks--;
  177. }
  178. } else {
  179. blocks = data->blocks;
  180. buffer = (char *)data->src;
  181. while (blocks) {
  182. timeout = PIO_TIMEOUT;
  183. size = data->blocksize;
  184. irqstat = esdhc_read32(&regs->irqstat);
  185. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
  186. && --timeout);
  187. if (timeout <= 0) {
  188. printf("\nData Write Failed in PIO Mode.");
  189. return;
  190. }
  191. while (size && (!(irqstat & IRQSTAT_TC))) {
  192. udelay(100); /* Wait before last byte transfer complete */
  193. databuf = *((uint *)buffer);
  194. buffer += 4;
  195. size -= 4;
  196. irqstat = esdhc_read32(&regs->irqstat);
  197. out_le32(&regs->datport, databuf);
  198. }
  199. blocks--;
  200. }
  201. }
  202. }
  203. #endif
  204. static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
  205. {
  206. int timeout;
  207. struct fsl_esdhc_priv *priv = mmc->priv;
  208. struct fsl_esdhc *regs = priv->esdhc_regs;
  209. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  210. dma_addr_t addr;
  211. #endif
  212. uint wml_value;
  213. wml_value = data->blocksize/4;
  214. if (data->flags & MMC_DATA_READ) {
  215. if (wml_value > WML_RD_WML_MAX)
  216. wml_value = WML_RD_WML_MAX_VAL;
  217. esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
  218. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  219. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  220. addr = virt_to_phys((void *)(data->dest));
  221. if (upper_32_bits(addr))
  222. printf("Error found for upper 32 bits\n");
  223. else
  224. esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
  225. #else
  226. esdhc_write32(&regs->dsaddr, (u32)data->dest);
  227. #endif
  228. #endif
  229. } else {
  230. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  231. flush_dcache_range((ulong)data->src,
  232. (ulong)data->src+data->blocks
  233. *data->blocksize);
  234. #endif
  235. if (wml_value > WML_WR_WML_MAX)
  236. wml_value = WML_WR_WML_MAX_VAL;
  237. if (priv->wp_enable) {
  238. if ((esdhc_read32(&regs->prsstat) &
  239. PRSSTAT_WPSPL) == 0) {
  240. printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
  241. return -ETIMEDOUT;
  242. }
  243. }
  244. esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
  245. wml_value << 16);
  246. #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
  247. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  248. addr = virt_to_phys((void *)(data->src));
  249. if (upper_32_bits(addr))
  250. printf("Error found for upper 32 bits\n");
  251. else
  252. esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
  253. #else
  254. esdhc_write32(&regs->dsaddr, (u32)data->src);
  255. #endif
  256. #endif
  257. }
  258. esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
  259. /* Calculate the timeout period for data transactions */
  260. /*
  261. * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
  262. * 2)Timeout period should be minimum 0.250sec as per SD Card spec
  263. * So, Number of SD Clock cycles for 0.25sec should be minimum
  264. * (SD Clock/sec * 0.25 sec) SD Clock cycles
  265. * = (mmc->clock * 1/4) SD Clock cycles
  266. * As 1) >= 2)
  267. * => (2^(timeout+13)) >= mmc->clock * 1/4
  268. * Taking log2 both the sides
  269. * => timeout + 13 >= log2(mmc->clock/4)
  270. * Rounding up to next power of 2
  271. * => timeout + 13 = log2(mmc->clock/4) + 1
  272. * => timeout + 13 = fls(mmc->clock/4)
  273. *
  274. * However, the MMC spec "It is strongly recommended for hosts to
  275. * implement more than 500ms timeout value even if the card
  276. * indicates the 250ms maximum busy length." Even the previous
  277. * value of 300ms is known to be insufficient for some cards.
  278. * So, we use
  279. * => timeout + 13 = fls(mmc->clock/2)
  280. */
  281. timeout = fls(mmc->clock/2);
  282. timeout -= 13;
  283. if (timeout > 14)
  284. timeout = 14;
  285. if (timeout < 0)
  286. timeout = 0;
  287. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
  288. if ((timeout == 4) || (timeout == 8) || (timeout == 12))
  289. timeout++;
  290. #endif
  291. #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
  292. timeout = 0xE;
  293. #endif
  294. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
  295. return 0;
  296. }
  297. static void check_and_invalidate_dcache_range
  298. (struct mmc_cmd *cmd,
  299. struct mmc_data *data) {
  300. unsigned start = 0;
  301. unsigned end = 0;
  302. unsigned size = roundup(ARCH_DMA_MINALIGN,
  303. data->blocks*data->blocksize);
  304. #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
  305. dma_addr_t addr;
  306. addr = virt_to_phys((void *)(data->dest));
  307. if (upper_32_bits(addr))
  308. printf("Error found for upper 32 bits\n");
  309. else
  310. start = lower_32_bits(addr);
  311. #else
  312. start = (unsigned)data->dest;
  313. #endif
  314. end = start + size;
  315. invalidate_dcache_range(start, end);
  316. }
  317. /*
  318. * Sends a command out on the bus. Takes the mmc pointer,
  319. * a command pointer, and an optional data pointer.
  320. */
  321. static int
  322. esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
  323. {
  324. int err = 0;
  325. uint xfertyp;
  326. uint irqstat;
  327. struct fsl_esdhc_priv *priv = mmc->priv;
  328. struct fsl_esdhc *regs = priv->esdhc_regs;
  329. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
  330. if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
  331. return 0;
  332. #endif
  333. esdhc_write32(&regs->irqstat, -1);
  334. sync();
  335. /* Wait for the bus to be idle */
  336. while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
  337. (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
  338. ;
  339. while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
  340. ;
  341. /* Wait at least 8 SD clock cycles before the next command */
  342. /*
  343. * Note: This is way more than 8 cycles, but 1ms seems to
  344. * resolve timing issues with some cards
  345. */
  346. udelay(1000);
  347. /* Set up for a data transfer if we have one */
  348. if (data) {
  349. err = esdhc_setup_data(mmc, data);
  350. if(err)
  351. return err;
  352. if (data->flags & MMC_DATA_READ)
  353. check_and_invalidate_dcache_range(cmd, data);
  354. }
  355. /* Figure out the transfer arguments */
  356. xfertyp = esdhc_xfertyp(cmd, data);
  357. /* Mask all irqs */
  358. esdhc_write32(&regs->irqsigen, 0);
  359. /* Send the command */
  360. esdhc_write32(&regs->cmdarg, cmd->cmdarg);
  361. #if defined(CONFIG_FSL_USDHC)
  362. esdhc_write32(&regs->mixctrl,
  363. (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
  364. | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
  365. esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
  366. #else
  367. esdhc_write32(&regs->xfertyp, xfertyp);
  368. #endif
  369. /* Wait for the command to complete */
  370. while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
  371. ;
  372. irqstat = esdhc_read32(&regs->irqstat);
  373. if (irqstat & CMD_ERR) {
  374. err = -ECOMM;
  375. goto out;
  376. }
  377. if (irqstat & IRQSTAT_CTOE) {
  378. err = -ETIMEDOUT;
  379. goto out;
  380. }
  381. /* Switch voltage to 1.8V if CMD11 succeeded */
  382. if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
  383. esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
  384. printf("Run CMD11 1.8V switch\n");
  385. /* Sleep for 5 ms - max time for card to switch to 1.8V */
  386. udelay(5000);
  387. }
  388. /* Workaround for ESDHC errata ENGcm03648 */
  389. if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
  390. int timeout = 6000;
  391. /* Poll on DATA0 line for cmd with busy signal for 600 ms */
  392. while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
  393. PRSSTAT_DAT0)) {
  394. udelay(100);
  395. timeout--;
  396. }
  397. if (timeout <= 0) {
  398. printf("Timeout waiting for DAT0 to go high!\n");
  399. err = -ETIMEDOUT;
  400. goto out;
  401. }
  402. }
  403. /* Copy the response to the response buffer */
  404. if (cmd->resp_type & MMC_RSP_136) {
  405. u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
  406. cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
  407. cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
  408. cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
  409. cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
  410. cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
  411. cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
  412. cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
  413. cmd->response[3] = (cmdrsp0 << 8);
  414. } else
  415. cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
  416. /* Wait until all of the blocks are transferred */
  417. if (data) {
  418. #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
  419. esdhc_pio_read_write(mmc, data);
  420. #else
  421. do {
  422. irqstat = esdhc_read32(&regs->irqstat);
  423. if (irqstat & IRQSTAT_DTOE) {
  424. err = -ETIMEDOUT;
  425. goto out;
  426. }
  427. if (irqstat & DATA_ERR) {
  428. err = -ECOMM;
  429. goto out;
  430. }
  431. } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
  432. /*
  433. * Need invalidate the dcache here again to avoid any
  434. * cache-fill during the DMA operations such as the
  435. * speculative pre-fetching etc.
  436. */
  437. if (data->flags & MMC_DATA_READ)
  438. check_and_invalidate_dcache_range(cmd, data);
  439. #endif
  440. }
  441. out:
  442. /* Reset CMD and DATA portions on error */
  443. if (err) {
  444. esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
  445. SYSCTL_RSTC);
  446. while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
  447. ;
  448. if (data) {
  449. esdhc_write32(&regs->sysctl,
  450. esdhc_read32(&regs->sysctl) |
  451. SYSCTL_RSTD);
  452. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
  453. ;
  454. }
  455. /* If this was CMD11, then notify that power cycle is needed */
  456. if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
  457. printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
  458. }
  459. esdhc_write32(&regs->irqstat, -1);
  460. return err;
  461. }
  462. static void set_sysctl(struct mmc *mmc, uint clock)
  463. {
  464. int div, pre_div;
  465. struct fsl_esdhc_priv *priv = mmc->priv;
  466. struct fsl_esdhc *regs = priv->esdhc_regs;
  467. int sdhc_clk = priv->sdhc_clk;
  468. uint clk;
  469. if (clock < mmc->cfg->f_min)
  470. clock = mmc->cfg->f_min;
  471. if (sdhc_clk / 16 > clock) {
  472. for (pre_div = 2; pre_div < 256; pre_div *= 2)
  473. if ((sdhc_clk / pre_div) <= (clock * 16))
  474. break;
  475. } else
  476. pre_div = 2;
  477. for (div = 1; div <= 16; div++)
  478. if ((sdhc_clk / (div * pre_div)) <= clock)
  479. break;
  480. pre_div >>= mmc->ddr_mode ? 2 : 1;
  481. div -= 1;
  482. clk = (pre_div << 8) | (div << 4);
  483. #ifdef CONFIG_FSL_USDHC
  484. esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
  485. #else
  486. esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
  487. #endif
  488. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
  489. udelay(10000);
  490. #ifdef CONFIG_FSL_USDHC
  491. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
  492. #else
  493. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
  494. #endif
  495. }
  496. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  497. static void esdhc_clock_control(struct mmc *mmc, bool enable)
  498. {
  499. struct fsl_esdhc_priv *priv = mmc->priv;
  500. struct fsl_esdhc *regs = priv->esdhc_regs;
  501. u32 value;
  502. u32 time_out;
  503. value = esdhc_read32(&regs->sysctl);
  504. if (enable)
  505. value |= SYSCTL_CKEN;
  506. else
  507. value &= ~SYSCTL_CKEN;
  508. esdhc_write32(&regs->sysctl, value);
  509. time_out = 20;
  510. value = PRSSTAT_SDSTB;
  511. while (!(esdhc_read32(&regs->prsstat) & value)) {
  512. if (time_out == 0) {
  513. printf("fsl_esdhc: Internal clock never stabilised.\n");
  514. break;
  515. }
  516. time_out--;
  517. mdelay(1);
  518. }
  519. }
  520. #endif
  521. static int esdhc_set_ios(struct mmc *mmc)
  522. {
  523. struct fsl_esdhc_priv *priv = mmc->priv;
  524. struct fsl_esdhc *regs = priv->esdhc_regs;
  525. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  526. /* Select to use peripheral clock */
  527. esdhc_clock_control(mmc, false);
  528. esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
  529. esdhc_clock_control(mmc, true);
  530. #endif
  531. /* Set the clock speed */
  532. set_sysctl(mmc, mmc->clock);
  533. /* Set the bus width */
  534. esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
  535. if (mmc->bus_width == 4)
  536. esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
  537. else if (mmc->bus_width == 8)
  538. esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
  539. return 0;
  540. }
  541. static int esdhc_init(struct mmc *mmc)
  542. {
  543. struct fsl_esdhc_priv *priv = mmc->priv;
  544. struct fsl_esdhc *regs = priv->esdhc_regs;
  545. int timeout = 1000;
  546. /* Reset the entire host controller */
  547. esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
  548. /* Wait until the controller is available */
  549. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  550. udelay(1000);
  551. #if defined(CONFIG_FSL_USDHC)
  552. /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
  553. esdhc_write32(&regs->mmcboot, 0x0);
  554. /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
  555. esdhc_write32(&regs->mixctrl, 0x0);
  556. esdhc_write32(&regs->clktunectrlstatus, 0x0);
  557. /* Put VEND_SPEC to default value */
  558. esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
  559. /* Disable DLL_CTRL delay line */
  560. esdhc_write32(&regs->dllctrl, 0x0);
  561. #endif
  562. #ifndef ARCH_MXC
  563. /* Enable cache snooping */
  564. esdhc_write32(&regs->scr, 0x00000040);
  565. #endif
  566. #ifndef CONFIG_FSL_USDHC
  567. esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
  568. #else
  569. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
  570. #endif
  571. /* Set the initial clock speed */
  572. mmc_set_clock(mmc, 400000);
  573. /* Disable the BRR and BWR bits in IRQSTAT */
  574. esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
  575. /* Put the PROCTL reg back to the default */
  576. esdhc_write32(&regs->proctl, PROCTL_INIT);
  577. /* Set timout to the maximum value */
  578. esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
  579. #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT
  580. esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
  581. #endif
  582. return 0;
  583. }
  584. static int esdhc_getcd(struct mmc *mmc)
  585. {
  586. struct fsl_esdhc_priv *priv = mmc->priv;
  587. struct fsl_esdhc *regs = priv->esdhc_regs;
  588. int timeout = 1000;
  589. #ifdef CONFIG_ESDHC_DETECT_QUIRK
  590. if (CONFIG_ESDHC_DETECT_QUIRK)
  591. return 1;
  592. #endif
  593. #ifdef CONFIG_DM_MMC
  594. if (priv->non_removable)
  595. return 1;
  596. #ifdef CONFIG_DM_GPIO
  597. if (dm_gpio_is_valid(&priv->cd_gpio))
  598. return dm_gpio_get_value(&priv->cd_gpio);
  599. #endif
  600. #endif
  601. while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
  602. udelay(1000);
  603. return timeout > 0;
  604. }
  605. static void esdhc_reset(struct fsl_esdhc *regs)
  606. {
  607. unsigned long timeout = 100; /* wait max 100 ms */
  608. /* reset the controller */
  609. esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
  610. /* hardware clears the bit when it is done */
  611. while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
  612. udelay(1000);
  613. if (!timeout)
  614. printf("MMC/SD: Reset never completed.\n");
  615. }
  616. static const struct mmc_ops esdhc_ops = {
  617. .send_cmd = esdhc_send_cmd,
  618. .set_ios = esdhc_set_ios,
  619. .init = esdhc_init,
  620. .getcd = esdhc_getcd,
  621. };
  622. static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
  623. struct fsl_esdhc_priv *priv)
  624. {
  625. if (!cfg || !priv)
  626. return -EINVAL;
  627. priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
  628. priv->bus_width = cfg->max_bus_width;
  629. priv->sdhc_clk = cfg->sdhc_clk;
  630. priv->wp_enable = cfg->wp_enable;
  631. return 0;
  632. };
  633. static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
  634. {
  635. struct fsl_esdhc *regs;
  636. struct mmc *mmc;
  637. u32 caps, voltage_caps;
  638. if (!priv)
  639. return -EINVAL;
  640. regs = priv->esdhc_regs;
  641. /* First reset the eSDHC controller */
  642. esdhc_reset(regs);
  643. #ifndef CONFIG_FSL_USDHC
  644. esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
  645. | SYSCTL_IPGEN | SYSCTL_CKEN);
  646. #else
  647. esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
  648. VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
  649. #endif
  650. writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
  651. memset(&priv->cfg, 0, sizeof(priv->cfg));
  652. voltage_caps = 0;
  653. caps = esdhc_read32(&regs->hostcapblt);
  654. #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
  655. caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
  656. ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
  657. #endif
  658. /* T4240 host controller capabilities register should have VS33 bit */
  659. #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
  660. caps = caps | ESDHC_HOSTCAPBLT_VS33;
  661. #endif
  662. if (caps & ESDHC_HOSTCAPBLT_VS18)
  663. voltage_caps |= MMC_VDD_165_195;
  664. if (caps & ESDHC_HOSTCAPBLT_VS30)
  665. voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
  666. if (caps & ESDHC_HOSTCAPBLT_VS33)
  667. voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
  668. priv->cfg.name = "FSL_SDHC";
  669. priv->cfg.ops = &esdhc_ops;
  670. #ifdef CONFIG_SYS_SD_VOLTAGE
  671. priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
  672. #else
  673. priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  674. #endif
  675. if ((priv->cfg.voltages & voltage_caps) == 0) {
  676. printf("voltage not supported by controller\n");
  677. return -1;
  678. }
  679. if (priv->bus_width == 8)
  680. priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  681. else if (priv->bus_width == 4)
  682. priv->cfg.host_caps = MMC_MODE_4BIT;
  683. priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
  684. #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
  685. priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
  686. #endif
  687. if (priv->bus_width > 0) {
  688. if (priv->bus_width < 8)
  689. priv->cfg.host_caps &= ~MMC_MODE_8BIT;
  690. if (priv->bus_width < 4)
  691. priv->cfg.host_caps &= ~MMC_MODE_4BIT;
  692. }
  693. if (caps & ESDHC_HOSTCAPBLT_HSS)
  694. priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  695. #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
  696. if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
  697. priv->cfg.host_caps &= ~MMC_MODE_8BIT;
  698. #endif
  699. priv->cfg.f_min = 400000;
  700. priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
  701. priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  702. mmc = mmc_create(&priv->cfg, priv);
  703. if (mmc == NULL)
  704. return -1;
  705. priv->mmc = mmc;
  706. return 0;
  707. }
  708. int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
  709. {
  710. struct fsl_esdhc_priv *priv;
  711. int ret;
  712. if (!cfg)
  713. return -EINVAL;
  714. priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
  715. if (!priv)
  716. return -ENOMEM;
  717. ret = fsl_esdhc_cfg_to_priv(cfg, priv);
  718. if (ret) {
  719. debug("%s xlate failure\n", __func__);
  720. free(priv);
  721. return ret;
  722. }
  723. ret = fsl_esdhc_init(priv);
  724. if (ret) {
  725. debug("%s init failure\n", __func__);
  726. free(priv);
  727. return ret;
  728. }
  729. return 0;
  730. }
  731. int fsl_esdhc_mmc_init(bd_t *bis)
  732. {
  733. struct fsl_esdhc_cfg *cfg;
  734. cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
  735. cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
  736. cfg->sdhc_clk = gd->arch.sdhc_clk;
  737. return fsl_esdhc_initialize(bis, cfg);
  738. }
  739. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  740. void mmc_adapter_card_type_ident(void)
  741. {
  742. u8 card_id;
  743. u8 value;
  744. card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
  745. gd->arch.sdhc_adapter = card_id;
  746. switch (card_id) {
  747. case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
  748. value = QIXIS_READ(brdcfg[5]);
  749. value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
  750. QIXIS_WRITE(brdcfg[5], value);
  751. break;
  752. case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
  753. value = QIXIS_READ(pwr_ctl[1]);
  754. value |= QIXIS_EVDD_BY_SDHC_VS;
  755. QIXIS_WRITE(pwr_ctl[1], value);
  756. break;
  757. case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
  758. value = QIXIS_READ(brdcfg[5]);
  759. value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
  760. QIXIS_WRITE(brdcfg[5], value);
  761. break;
  762. case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
  763. break;
  764. case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
  765. break;
  766. case QIXIS_ESDHC_ADAPTER_TYPE_SD:
  767. break;
  768. case QIXIS_ESDHC_NO_ADAPTER:
  769. break;
  770. default:
  771. break;
  772. }
  773. }
  774. #endif
  775. #ifdef CONFIG_OF_LIBFDT
  776. __weak int esdhc_status_fixup(void *blob, const char *compat)
  777. {
  778. #ifdef CONFIG_FSL_ESDHC_PIN_MUX
  779. if (!hwconfig("esdhc")) {
  780. do_fixup_by_compat(blob, compat, "status", "disabled",
  781. sizeof("disabled"), 1);
  782. return 1;
  783. }
  784. #endif
  785. do_fixup_by_compat(blob, compat, "status", "okay",
  786. sizeof("okay"), 1);
  787. return 0;
  788. }
  789. void fdt_fixup_esdhc(void *blob, bd_t *bd)
  790. {
  791. const char *compat = "fsl,esdhc";
  792. if (esdhc_status_fixup(blob, compat))
  793. return;
  794. #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
  795. do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
  796. gd->arch.sdhc_clk, 1);
  797. #else
  798. do_fixup_by_compat_u32(blob, compat, "clock-frequency",
  799. gd->arch.sdhc_clk, 1);
  800. #endif
  801. #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
  802. do_fixup_by_compat_u32(blob, compat, "adapter-type",
  803. (u32)(gd->arch.sdhc_adapter), 1);
  804. #endif
  805. }
  806. #endif
  807. #ifdef CONFIG_DM_MMC
  808. #include <asm/arch/clock.h>
  809. __weak void init_clk_usdhc(u32 index)
  810. {
  811. }
  812. static int fsl_esdhc_probe(struct udevice *dev)
  813. {
  814. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  815. struct fsl_esdhc_priv *priv = dev_get_priv(dev);
  816. const void *fdt = gd->fdt_blob;
  817. int node = dev_of_offset(dev);
  818. fdt_addr_t addr;
  819. unsigned int val;
  820. int ret;
  821. addr = dev_get_addr(dev);
  822. if (addr == FDT_ADDR_T_NONE)
  823. return -EINVAL;
  824. priv->esdhc_regs = (struct fsl_esdhc *)addr;
  825. priv->dev = dev;
  826. val = fdtdec_get_int(fdt, node, "bus-width", -1);
  827. if (val == 8)
  828. priv->bus_width = 8;
  829. else if (val == 4)
  830. priv->bus_width = 4;
  831. else
  832. priv->bus_width = 1;
  833. if (fdt_get_property(fdt, node, "non-removable", NULL)) {
  834. priv->non_removable = 1;
  835. } else {
  836. priv->non_removable = 0;
  837. #ifdef CONFIG_DM_GPIO
  838. gpio_request_by_name_nodev(fdt, node, "cd-gpios", 0,
  839. &priv->cd_gpio, GPIOD_IS_IN);
  840. #endif
  841. }
  842. priv->wp_enable = 1;
  843. #ifdef CONFIG_DM_GPIO
  844. ret = gpio_request_by_name_nodev(fdt, node, "wp-gpios", 0,
  845. &priv->wp_gpio, GPIOD_IS_IN);
  846. if (ret)
  847. priv->wp_enable = 0;
  848. #endif
  849. /*
  850. * TODO:
  851. * Because lack of clk driver, if SDHC clk is not enabled,
  852. * need to enable it first before this driver is invoked.
  853. *
  854. * we use MXC_ESDHC_CLK to get clk freq.
  855. * If one would like to make this function work,
  856. * the aliases should be provided in dts as this:
  857. *
  858. * aliases {
  859. * mmc0 = &usdhc1;
  860. * mmc1 = &usdhc2;
  861. * mmc2 = &usdhc3;
  862. * mmc3 = &usdhc4;
  863. * };
  864. * Then if your board only supports mmc2 and mmc3, but we can
  865. * correctly get the seq as 2 and 3, then let mxc_get_clock
  866. * work as expected.
  867. */
  868. init_clk_usdhc(dev->seq);
  869. priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
  870. if (priv->sdhc_clk <= 0) {
  871. dev_err(dev, "Unable to get clk for %s\n", dev->name);
  872. return -EINVAL;
  873. }
  874. ret = fsl_esdhc_init(priv);
  875. if (ret) {
  876. dev_err(dev, "fsl_esdhc_init failure\n");
  877. return ret;
  878. }
  879. upriv->mmc = priv->mmc;
  880. priv->mmc->dev = dev;
  881. return 0;
  882. }
  883. static const struct udevice_id fsl_esdhc_ids[] = {
  884. { .compatible = "fsl,imx6ul-usdhc", },
  885. { .compatible = "fsl,imx6sx-usdhc", },
  886. { .compatible = "fsl,imx6sl-usdhc", },
  887. { .compatible = "fsl,imx6q-usdhc", },
  888. { .compatible = "fsl,imx7d-usdhc", },
  889. { .compatible = "fsl,imx7ulp-usdhc", },
  890. { .compatible = "fsl,esdhc", },
  891. { /* sentinel */ }
  892. };
  893. U_BOOT_DRIVER(fsl_esdhc) = {
  894. .name = "fsl-esdhc-mmc",
  895. .id = UCLASS_MMC,
  896. .of_match = fsl_esdhc_ids,
  897. .probe = fsl_esdhc_probe,
  898. .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
  899. };
  900. #endif