fsl_esdhc.c 27 KB

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