3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation's version 2 of
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
36 #include <asm/arch/mmc_host_def.h>
37 #ifdef CONFIG_OMAP54XX
38 #include <asm/arch/mux_dra7xx.h>
39 #include <asm/arch/dra7xx_iodelay.h>
41 #if !defined(CONFIG_SOC_KEYSTONE)
43 #include <asm/arch/sys_proto.h>
45 #ifdef CONFIG_MMC_OMAP36XX_PINS
46 #include <asm/arch/mux.h>
49 #include <power/regulator.h>
51 DECLARE_GLOBAL_DATA_PTR;
53 /* simplify defines to OMAP_HSMMC_USE_GPIO */
54 #if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
55 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
56 #define OMAP_HSMMC_USE_GPIO
58 #undef OMAP_HSMMC_USE_GPIO
61 /* common definitions for all OMAPs */
62 #define SYSCTL_SRC (1 << 25)
63 #define SYSCTL_SRD (1 << 26)
65 #ifdef CONFIG_IODELAY_RECALIBRATION
66 struct omap_hsmmc_pinctrl_state {
67 struct pad_conf_entry *padconf;
69 struct iodelay_cfg_entry *iodelay;
74 struct omap_hsmmc_data {
75 struct hsmmc *base_addr;
76 #if !CONFIG_IS_ENABLED(DM_MMC)
77 struct mmc_config cfg;
82 #ifdef OMAP_HSMMC_USE_GPIO
83 #if CONFIG_IS_ENABLED(DM_MMC)
84 struct gpio_desc cd_gpio; /* Change Detect GPIO */
85 struct gpio_desc wp_gpio; /* Write Protect GPIO */
91 #if CONFIG_IS_ENABLED(DM_MMC)
95 #ifdef CONFIG_MMC_OMAP_HS_ADMA
96 struct omap_hsmmc_adma_desc *adma_desc_table;
100 struct udevice *pbias_supply;
102 #ifdef CONFIG_IODELAY_RECALIBRATION
103 struct omap_hsmmc_pinctrl_state *default_pinctrl_state;
104 struct omap_hsmmc_pinctrl_state *hs_pinctrl_state;
105 struct omap_hsmmc_pinctrl_state *hs200_1_8v_pinctrl_state;
106 struct omap_hsmmc_pinctrl_state *ddr_1_8v_pinctrl_state;
107 struct omap_hsmmc_pinctrl_state *sdr12_pinctrl_state;
108 struct omap_hsmmc_pinctrl_state *sdr25_pinctrl_state;
109 struct omap_hsmmc_pinctrl_state *ddr50_pinctrl_state;
110 struct omap_hsmmc_pinctrl_state *sdr50_pinctrl_state;
111 struct omap_hsmmc_pinctrl_state *sdr104_pinctrl_state;
115 struct omap_mmc_of_data {
119 #ifdef CONFIG_MMC_OMAP_HS_ADMA
120 struct omap_hsmmc_adma_desc {
127 #define ADMA_MAX_LEN 63488
129 /* Decriptor table defines */
130 #define ADMA_DESC_ATTR_VALID BIT(0)
131 #define ADMA_DESC_ATTR_END BIT(1)
132 #define ADMA_DESC_ATTR_INT BIT(2)
133 #define ADMA_DESC_ATTR_ACT1 BIT(4)
134 #define ADMA_DESC_ATTR_ACT2 BIT(5)
136 #define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
137 #define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
140 /* If we fail after 1 second wait, something is really bad */
141 #define MAX_RETRY_MS 1000
142 #define MMC_TIMEOUT_MS 20
144 /* DMA transfers can take a long time if a lot a data is transferred.
145 * The timeout must take in account the amount of data. Let's assume
146 * that the time will never exceed 333 ms per MB (in other word we assume
147 * that the bandwidth is always above 3MB/s).
149 #define DMA_TIMEOUT_PER_MB 333
150 #define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
151 #define OMAP_HSMMC_NO_1_8_V BIT(1)
152 #define OMAP_HSMMC_USE_ADMA BIT(2)
153 #define OMAP_HSMMC_REQUIRE_IODELAY BIT(3)
155 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
156 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
158 static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
159 static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
160 static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
162 static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
164 #if CONFIG_IS_ENABLED(DM_MMC)
165 return dev_get_priv(mmc->dev);
167 return (struct omap_hsmmc_data *)mmc->priv;
170 static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
172 #if CONFIG_IS_ENABLED(DM_MMC)
173 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
176 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
180 #if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
181 static int omap_mmc_setup_gpio_in(int gpio, const char *label)
185 #ifndef CONFIG_DM_GPIO
186 if (!gpio_is_valid(gpio))
189 ret = gpio_request(gpio, label);
193 ret = gpio_direction_input(gpio);
201 static unsigned char mmc_board_init(struct mmc *mmc)
203 #if defined(CONFIG_OMAP34XX)
204 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
205 t2_t *t2_base = (t2_t *)T2_BASE;
206 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
208 #ifdef CONFIG_MMC_OMAP36XX_PINS
209 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
212 pbias_lite = readl(&t2_base->pbias_lite);
213 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
214 #ifdef CONFIG_TARGET_OMAP3_CAIRO
215 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
216 pbias_lite &= ~PBIASLITEVMODE0;
218 #ifdef CONFIG_TARGET_OMAP3_LOGIC
219 /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
220 pbias_lite &= ~PBIASLITEVMODE1;
222 #ifdef CONFIG_MMC_OMAP36XX_PINS
223 if (get_cpu_family() == CPU_OMAP36XX) {
224 /* Disable extended drain IO before changing PBIAS */
225 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
226 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
229 writel(pbias_lite, &t2_base->pbias_lite);
231 writel(pbias_lite | PBIASLITEPWRDNZ1 |
232 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
233 &t2_base->pbias_lite);
235 #ifdef CONFIG_MMC_OMAP36XX_PINS
236 if (get_cpu_family() == CPU_OMAP36XX)
237 /* Enable extended drain IO after changing PBIAS */
239 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
240 OMAP34XX_CTRL_WKUP_CTRL);
242 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
245 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
248 /* Change from default of 52MHz to 26MHz if necessary */
249 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
250 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
251 &t2_base->ctl_prog_io1);
253 writel(readl(&prcm_base->fclken1_core) |
254 EN_MMC1 | EN_MMC2 | EN_MMC3,
255 &prcm_base->fclken1_core);
257 writel(readl(&prcm_base->iclken1_core) |
258 EN_MMC1 | EN_MMC2 | EN_MMC3,
259 &prcm_base->iclken1_core);
262 #if (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) &&\
263 !CONFIG_IS_ENABLED(DM_REGULATOR)
264 /* PBIAS config needed for MMC1 only */
265 if (mmc_get_blk_desc(mmc)->devnum == 0)
266 vmmc_pbias_config(LDO_VOLT_3V0);
272 void mmc_init_stream(struct hsmmc *mmc_base)
276 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
278 writel(MMC_CMD0, &mmc_base->cmd);
279 start = get_timer(0);
280 while (!(readl(&mmc_base->stat) & CC_MASK)) {
281 if (get_timer(0) - start > MAX_RETRY_MS) {
282 printf("%s: timedout waiting for cc!\n", __func__);
286 writel(CC_MASK, &mmc_base->stat)
288 writel(MMC_CMD0, &mmc_base->cmd)
290 start = get_timer(0);
291 while (!(readl(&mmc_base->stat) & CC_MASK)) {
292 if (get_timer(0) - start > MAX_RETRY_MS) {
293 printf("%s: timedout waiting for cc2!\n", __func__);
297 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
300 #if CONFIG_IS_ENABLED(DM_MMC)
301 #ifdef CONFIG_IODELAY_RECALIBRATION
302 static void omap_hsmmc_io_recalibrate(struct mmc *mmc)
304 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
305 struct omap_hsmmc_pinctrl_state *pinctrl_state;
307 switch (priv->mode) {
309 pinctrl_state = priv->hs200_1_8v_pinctrl_state;
312 pinctrl_state = priv->sdr104_pinctrl_state;
315 pinctrl_state = priv->sdr50_pinctrl_state;
318 pinctrl_state = priv->ddr50_pinctrl_state;
321 pinctrl_state = priv->sdr25_pinctrl_state;
324 pinctrl_state = priv->sdr12_pinctrl_state;
329 pinctrl_state = priv->hs_pinctrl_state;
332 pinctrl_state = priv->ddr_1_8v_pinctrl_state;
334 pinctrl_state = priv->default_pinctrl_state;
339 pinctrl_state = priv->default_pinctrl_state;
341 if (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
342 if (pinctrl_state->iodelay)
343 late_recalibrate_iodelay(pinctrl_state->padconf,
344 pinctrl_state->npads,
345 pinctrl_state->iodelay,
346 pinctrl_state->niodelays);
348 do_set_mux32((*ctrl)->control_padconf_core_base,
349 pinctrl_state->padconf,
350 pinctrl_state->npads);
354 static void omap_hsmmc_set_timing(struct mmc *mmc)
357 struct hsmmc *mmc_base;
358 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
360 mmc_base = priv->base_addr;
362 omap_hsmmc_stop_clock(mmc_base);
363 val = readl(&mmc_base->ac12);
364 val &= ~AC12_UHSMC_MASK;
365 priv->mode = mmc->selected_mode;
367 if (mmc_is_mode_ddr(priv->mode))
368 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
370 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
372 switch (priv->mode) {
375 val |= AC12_UHSMC_SDR104;
378 val |= AC12_UHSMC_SDR50;
382 val |= AC12_UHSMC_DDR50;
387 val |= AC12_UHSMC_SDR25;
393 val |= AC12_UHSMC_SDR12;
396 val |= AC12_UHSMC_RES;
399 writel(val, &mmc_base->ac12);
401 #ifdef CONFIG_IODELAY_RECALIBRATION
402 omap_hsmmc_io_recalibrate(mmc);
404 omap_hsmmc_start_clock(mmc_base);
407 static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
409 struct hsmmc *mmc_base;
410 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
413 mmc_base = priv->base_addr;
415 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
416 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
418 switch (signal_voltage) {
419 case MMC_SIGNAL_VOLTAGE_330:
422 case MMC_SIGNAL_VOLTAGE_180:
424 ac12 |= AC12_V1V8_SIGEN;
428 writel(hctl, &mmc_base->hctl);
429 writel(ac12, &mmc_base->ac12);
432 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
433 static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout)
435 int ret = -ETIMEDOUT;
438 bool target_dat0_high = !!state;
439 struct omap_hsmmc_data *priv = dev_get_priv(dev);
440 struct hsmmc *mmc_base = priv->base_addr;
442 con = readl(&mmc_base->con);
443 writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
445 timeout = DIV_ROUND_UP(timeout, 10); /* check every 10 us. */
447 dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
448 if (dat0_high == target_dat0_high) {
454 writel(con, &mmc_base->con);
460 #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
461 #if CONFIG_IS_ENABLED(DM_REGULATOR)
462 static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
467 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
469 if (!mmc->vqmmc_supply)
473 ret = regulator_set_enable(priv->pbias_supply, false);
474 if (ret && ret != -ENOSYS)
477 /* Turn off IO voltage */
478 ret = regulator_set_enable(mmc->vqmmc_supply, false);
479 if (ret && ret != -ENOSYS)
481 /* Program a new IO voltage value */
482 ret = regulator_set_value(mmc->vqmmc_supply, uV);
485 /* Turn on IO voltage */
486 ret = regulator_set_enable(mmc->vqmmc_supply, true);
487 if (ret && ret != -ENOSYS)
490 /* Program PBIAS voltage*/
491 ret = regulator_set_value(priv->pbias_supply, uV);
492 if (ret && ret != -ENOSYS)
495 ret = regulator_set_enable(priv->pbias_supply, true);
496 if (ret && ret != -ENOSYS)
503 static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
505 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
506 struct hsmmc *mmc_base = priv->base_addr;
507 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
509 __maybe_unused u8 palmas_ldo_volt;
515 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
516 /* Use 3.0V rather than 3.3V */
518 capa_mask = VS30_3V0SUP;
519 palmas_ldo_volt = LDO_VOLT_3V0;
520 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
521 capa_mask = VS18_1V8SUP;
522 palmas_ldo_volt = LDO_VOLT_1V8;
527 val = readl(&mmc_base->capa);
528 if (!(val & capa_mask))
531 priv->signal_voltage = mmc->signal_voltage;
533 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
535 #if CONFIG_IS_ENABLED(DM_REGULATOR)
536 return omap_hsmmc_set_io_regulator(mmc, mv);
537 #elif (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) && \
538 defined(CONFIG_PALMAS_POWER)
539 if (mmc_get_blk_desc(mmc)->devnum == 0)
540 vmmc_pbias_config(palmas_ldo_volt);
548 static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
550 struct hsmmc *mmc_base;
551 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
554 mmc_base = priv->base_addr;
555 val = readl(&mmc_base->capa);
557 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
558 val |= (VS30_3V0SUP | VS18_1V8SUP);
559 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
567 writel(val, &mmc_base->capa);
572 #ifdef MMC_SUPPORTS_TUNING
573 static void omap_hsmmc_disable_tuning(struct mmc *mmc)
575 struct hsmmc *mmc_base;
576 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
579 mmc_base = priv->base_addr;
580 val = readl(&mmc_base->ac12);
581 val &= ~(AC12_SCLK_SEL);
582 writel(val, &mmc_base->ac12);
584 val = readl(&mmc_base->dll);
585 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
586 writel(val, &mmc_base->dll);
589 static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
592 struct hsmmc *mmc_base;
593 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
596 mmc_base = priv->base_addr;
597 val = readl(&mmc_base->dll);
598 val |= DLL_FORCE_VALUE;
599 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
600 val |= (count << DLL_FORCE_SR_C_SHIFT);
601 writel(val, &mmc_base->dll);
604 writel(val, &mmc_base->dll);
605 for (i = 0; i < 1000; i++) {
606 if (readl(&mmc_base->dll) & DLL_CALIB)
610 writel(val, &mmc_base->dll);
613 static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
615 struct omap_hsmmc_data *priv = dev_get_priv(dev);
616 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
617 struct mmc *mmc = upriv->mmc;
618 struct hsmmc *mmc_base;
620 u8 cur_match, prev_match = 0;
623 u32 start_window = 0, max_window = 0;
624 u32 length = 0, max_len = 0;
626 mmc_base = priv->base_addr;
627 val = readl(&mmc_base->capa2);
629 /* clock tuning is not needed for upto 52MHz */
630 if (!((mmc->selected_mode == MMC_HS_200) ||
631 (mmc->selected_mode == UHS_SDR104) ||
632 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
635 val = readl(&mmc_base->dll);
637 writel(val, &mmc_base->dll);
638 while (phase_delay <= MAX_PHASE_DELAY) {
639 omap_hsmmc_set_dll(mmc, phase_delay);
641 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
647 start_window = phase_delay;
652 if (length > max_len) {
653 max_window = start_window;
657 prev_match = cur_match;
666 val = readl(&mmc_base->ac12);
667 if (!(val & AC12_SCLK_SEL)) {
672 phase_delay = max_window + 4 * ((3 * max_len) >> 2);
673 omap_hsmmc_set_dll(mmc, phase_delay);
675 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
676 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
682 omap_hsmmc_disable_tuning(mmc);
683 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
684 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
690 static void omap_hsmmc_send_init_stream(struct udevice *dev)
692 struct omap_hsmmc_data *priv = dev_get_priv(dev);
693 struct hsmmc *mmc_base = priv->base_addr;
695 mmc_init_stream(mmc_base);
699 static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
701 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
702 struct hsmmc *mmc_base = priv->base_addr;
703 u32 irq_mask = INT_EN_MASK;
706 * TODO: Errata i802 indicates only DCRC interrupts can occur during
707 * tuning procedure and DCRC should be disabled. But see occurences
708 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
709 * interrupts occur along with BRR, so the data is actually in the
710 * buffer. It has to be debugged why these interrutps occur
712 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
713 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
715 writel(irq_mask, &mmc_base->ie);
718 static int omap_hsmmc_init_setup(struct mmc *mmc)
720 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
721 struct hsmmc *mmc_base;
722 unsigned int reg_val;
726 mmc_base = priv->base_addr;
729 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
730 &mmc_base->sysconfig);
731 start = get_timer(0);
732 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
733 if (get_timer(0) - start > MAX_RETRY_MS) {
734 printf("%s: timedout waiting for cc2!\n", __func__);
738 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
739 start = get_timer(0);
740 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
741 if (get_timer(0) - start > MAX_RETRY_MS) {
742 printf("%s: timedout waiting for softresetall!\n",
747 #ifdef CONFIG_MMC_OMAP_HS_ADMA
748 reg_val = readl(&mmc_base->hl_hwinfo);
749 if (reg_val & MADMA_EN)
750 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
753 #if CONFIG_IS_ENABLED(DM_MMC)
754 reg_val = omap_hsmmc_set_capabilities(mmc);
755 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS30_3V0SUP) ?
756 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
758 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
759 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
763 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
765 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
766 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
767 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
770 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
771 (ICE_STOP | DTO_15THDTO));
772 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
773 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
774 start = get_timer(0);
775 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
776 if (get_timer(0) - start > MAX_RETRY_MS) {
777 printf("%s: timedout waiting for ics!\n", __func__);
781 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
783 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
785 mmc_enable_irq(mmc, NULL);
787 #if !CONFIG_IS_ENABLED(DM_MMC)
788 mmc_init_stream(mmc_base);
795 * MMC controller internal finite state machine reset
797 * Used to reset command or data internal state machines, using respectively
798 * SRC or SRD bit of SYSCTL register
800 static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
804 mmc_reg_out(&mmc_base->sysctl, bit, bit);
807 * CMD(DAT) lines reset procedures are slightly different
808 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
809 * According to OMAP3 TRM:
810 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
812 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
813 * procedure steps must be as follows:
814 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
815 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
816 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
817 * 3. Wait until the SRC (SRD) bit returns to 0x0
818 * (reset procedure is completed).
820 #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
821 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
822 if (!(readl(&mmc_base->sysctl) & bit)) {
823 start = get_timer(0);
824 while (!(readl(&mmc_base->sysctl) & bit)) {
825 if (get_timer(0) - start > MMC_TIMEOUT_MS)
830 start = get_timer(0);
831 while ((readl(&mmc_base->sysctl) & bit) != 0) {
832 if (get_timer(0) - start > MAX_RETRY_MS) {
833 printf("%s: timedout waiting for sysctl %x to clear\n",
840 #ifdef CONFIG_MMC_OMAP_HS_ADMA
841 static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
843 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
844 struct omap_hsmmc_adma_desc *desc;
847 desc = &priv->adma_desc_table[priv->desc_slot];
849 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
853 attr |= ADMA_DESC_ATTR_END;
856 desc->addr = (u32)buf;
861 static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
862 struct mmc_data *data)
864 uint total_len = data->blocksize * data->blocks;
865 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
866 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
871 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
872 memalign(ARCH_DMA_MINALIGN, desc_count *
873 sizeof(struct omap_hsmmc_adma_desc));
875 if (data->flags & MMC_DATA_READ)
878 buf = (char *)data->src;
881 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
883 total_len -= ADMA_MAX_LEN;
886 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
888 flush_dcache_range((long)priv->adma_desc_table,
889 (long)priv->adma_desc_table +
891 sizeof(struct omap_hsmmc_adma_desc),
895 static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
897 struct hsmmc *mmc_base;
898 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
902 mmc_base = priv->base_addr;
903 omap_hsmmc_prepare_adma_table(mmc, data);
905 if (data->flags & MMC_DATA_READ)
908 buf = (char *)data->src;
910 val = readl(&mmc_base->hctl);
912 writel(val, &mmc_base->hctl);
914 val = readl(&mmc_base->con);
916 writel(val, &mmc_base->con);
918 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
920 flush_dcache_range((u32)buf,
922 ROUND(data->blocksize * data->blocks,
926 static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
928 struct hsmmc *mmc_base;
929 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
932 mmc_base = priv->base_addr;
934 val = readl(&mmc_base->con);
936 writel(val, &mmc_base->con);
938 val = readl(&mmc_base->hctl);
940 writel(val, &mmc_base->hctl);
942 kfree(priv->adma_desc_table);
945 #define omap_hsmmc_adma_desc
946 #define omap_hsmmc_prepare_adma_table
947 #define omap_hsmmc_prepare_data
948 #define omap_hsmmc_dma_cleanup
951 #if !CONFIG_IS_ENABLED(DM_MMC)
952 static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
953 struct mmc_data *data)
955 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
957 static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
958 struct mmc_data *data)
960 struct omap_hsmmc_data *priv = dev_get_priv(dev);
961 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
962 struct mmc *mmc = upriv->mmc;
964 struct hsmmc *mmc_base;
965 unsigned int flags, mmc_stat;
967 priv->last_cmd = cmd->cmdidx;
969 mmc_base = priv->base_addr;
971 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
974 start = get_timer(0);
975 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
976 if (get_timer(0) - start > MAX_RETRY_MS) {
977 printf("%s: timedout waiting on cmd inhibit to clear\n",
982 writel(0xFFFFFFFF, &mmc_base->stat);
983 start = get_timer(0);
984 while (readl(&mmc_base->stat)) {
985 if (get_timer(0) - start > MAX_RETRY_MS) {
986 printf("%s: timedout waiting for STAT (%x) to clear\n",
987 __func__, readl(&mmc_base->stat));
993 * CMDIDX[13:8] : Command index
994 * DATAPRNT[5] : Data Present Select
995 * ENCMDIDX[4] : Command Index Check Enable
996 * ENCMDCRC[3] : Command CRC Check Enable
1001 * 11 = Length 48 Check busy after response
1003 /* Delay added before checking the status of frq change
1004 * retry not supported by mmc.c(core file)
1006 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1007 udelay(50000); /* wait 50 ms */
1009 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1011 else if (cmd->resp_type & MMC_RSP_136)
1012 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1013 else if (cmd->resp_type & MMC_RSP_BUSY)
1014 flags = RSP_TYPE_LGHT48B;
1016 flags = RSP_TYPE_LGHT48;
1018 /* enable default flags */
1019 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
1021 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
1023 if (cmd->resp_type & MMC_RSP_CRC)
1024 flags |= CCCE_CHECK;
1025 if (cmd->resp_type & MMC_RSP_OPCODE)
1026 flags |= CICE_CHECK;
1029 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1030 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
1031 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
1032 data->blocksize = 512;
1033 writel(data->blocksize | (data->blocks << 16),
1036 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1038 if (data->flags & MMC_DATA_READ)
1039 flags |= (DP_DATA | DDIR_READ);
1041 flags |= (DP_DATA | DDIR_WRITE);
1043 #ifdef CONFIG_MMC_OMAP_HS_ADMA
1044 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1045 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1046 omap_hsmmc_prepare_data(mmc, data);
1052 mmc_enable_irq(mmc, cmd);
1054 writel(cmd->cmdarg, &mmc_base->arg);
1055 udelay(20); /* To fix "No status update" error on eMMC */
1056 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1058 start = get_timer(0);
1060 mmc_stat = readl(&mmc_base->stat);
1061 if (get_timer(start) > MAX_RETRY_MS) {
1062 printf("%s : timeout: No status update\n", __func__);
1065 } while (!mmc_stat);
1067 if ((mmc_stat & IE_CTO) != 0) {
1068 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
1070 } else if ((mmc_stat & ERRI_MASK) != 0)
1073 if (mmc_stat & CC_MASK) {
1074 writel(CC_MASK, &mmc_base->stat);
1075 if (cmd->resp_type & MMC_RSP_PRESENT) {
1076 if (cmd->resp_type & MMC_RSP_136) {
1077 /* response type 2 */
1078 cmd->response[3] = readl(&mmc_base->rsp10);
1079 cmd->response[2] = readl(&mmc_base->rsp32);
1080 cmd->response[1] = readl(&mmc_base->rsp54);
1081 cmd->response[0] = readl(&mmc_base->rsp76);
1083 /* response types 1, 1b, 3, 4, 5, 6 */
1084 cmd->response[0] = readl(&mmc_base->rsp10);
1088 #ifdef CONFIG_MMC_OMAP_HS_ADMA
1089 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1090 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1093 if (mmc_stat & IE_ADMAE) {
1094 omap_hsmmc_dma_cleanup(mmc);
1098 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1099 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1100 if (timeout < MAX_RETRY_MS)
1101 timeout = MAX_RETRY_MS;
1103 start = get_timer(0);
1105 mmc_stat = readl(&mmc_base->stat);
1106 if (mmc_stat & TC_MASK) {
1107 writel(readl(&mmc_base->stat) | TC_MASK,
1111 if (get_timer(start) > timeout) {
1112 printf("%s : DMA timeout: No status update\n",
1118 omap_hsmmc_dma_cleanup(mmc);
1123 if (data && (data->flags & MMC_DATA_READ)) {
1124 mmc_read_data(mmc_base, data->dest,
1125 data->blocksize * data->blocks);
1126 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1127 mmc_write_data(mmc_base, data->src,
1128 data->blocksize * data->blocks);
1133 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
1135 unsigned int *output_buf = (unsigned int *)buf;
1136 unsigned int mmc_stat;
1142 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1146 ulong start = get_timer(0);
1148 mmc_stat = readl(&mmc_base->stat);
1149 if (get_timer(0) - start > MAX_RETRY_MS) {
1150 printf("%s: timedout waiting for status!\n",
1154 } while (mmc_stat == 0);
1156 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1157 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1159 if ((mmc_stat & ERRI_MASK) != 0)
1162 if (mmc_stat & BRR_MASK) {
1165 writel(readl(&mmc_base->stat) | BRR_MASK,
1167 for (k = 0; k < count; k++) {
1168 *output_buf = readl(&mmc_base->data);
1174 if (mmc_stat & BWR_MASK)
1175 writel(readl(&mmc_base->stat) | BWR_MASK,
1178 if (mmc_stat & TC_MASK) {
1179 writel(readl(&mmc_base->stat) | TC_MASK,
1187 #if CONFIG_IS_ENABLED(MMC_WRITE)
1188 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1191 unsigned int *input_buf = (unsigned int *)buf;
1192 unsigned int mmc_stat;
1196 * Start Polled Write
1198 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1202 ulong start = get_timer(0);
1204 mmc_stat = readl(&mmc_base->stat);
1205 if (get_timer(0) - start > MAX_RETRY_MS) {
1206 printf("%s: timedout waiting for status!\n",
1210 } while (mmc_stat == 0);
1212 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1213 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1215 if ((mmc_stat & ERRI_MASK) != 0)
1218 if (mmc_stat & BWR_MASK) {
1221 writel(readl(&mmc_base->stat) | BWR_MASK,
1223 for (k = 0; k < count; k++) {
1224 writel(*input_buf, &mmc_base->data);
1230 if (mmc_stat & BRR_MASK)
1231 writel(readl(&mmc_base->stat) | BRR_MASK,
1234 if (mmc_stat & TC_MASK) {
1235 writel(readl(&mmc_base->stat) | TC_MASK,
1243 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1249 static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1251 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1254 static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1256 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1259 static void omap_hsmmc_set_clock(struct mmc *mmc)
1261 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1262 struct hsmmc *mmc_base;
1263 unsigned int dsor = 0;
1266 mmc_base = priv->base_addr;
1267 omap_hsmmc_stop_clock(mmc_base);
1269 /* TODO: Is setting DTO required here? */
1270 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1271 (ICE_STOP | DTO_15THDTO));
1273 if (mmc->clock != 0) {
1274 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1275 if (dsor > CLKD_MAX)
1281 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1282 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1284 start = get_timer(0);
1285 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1286 if (get_timer(0) - start > MAX_RETRY_MS) {
1287 printf("%s: timedout waiting for ics!\n", __func__);
1292 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1293 mmc->clock = priv->clock;
1294 omap_hsmmc_start_clock(mmc_base);
1297 static void omap_hsmmc_set_bus_width(struct mmc *mmc)
1299 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1300 struct hsmmc *mmc_base;
1302 mmc_base = priv->base_addr;
1303 /* configue bus width */
1304 switch (mmc->bus_width) {
1306 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1311 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1313 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1319 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1321 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1326 priv->bus_width = mmc->bus_width;
1329 #if !CONFIG_IS_ENABLED(DM_MMC)
1330 static int omap_hsmmc_set_ios(struct mmc *mmc)
1332 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1334 static int omap_hsmmc_set_ios(struct udevice *dev)
1336 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1337 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1338 struct mmc *mmc = upriv->mmc;
1340 struct hsmmc *mmc_base = priv->base_addr;
1343 if (priv->bus_width != mmc->bus_width)
1344 omap_hsmmc_set_bus_width(mmc);
1346 if (priv->clock != mmc->clock)
1347 omap_hsmmc_set_clock(mmc);
1349 if (mmc->clk_disable)
1350 omap_hsmmc_stop_clock(mmc_base);
1352 omap_hsmmc_start_clock(mmc_base);
1354 #if CONFIG_IS_ENABLED(DM_MMC)
1355 if (priv->mode != mmc->selected_mode)
1356 omap_hsmmc_set_timing(mmc);
1358 #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1359 if (priv->signal_voltage != mmc->signal_voltage)
1360 ret = omap_hsmmc_set_signal_voltage(mmc);
1366 #ifdef OMAP_HSMMC_USE_GPIO
1367 #if CONFIG_IS_ENABLED(DM_MMC)
1368 static int omap_hsmmc_getcd(struct udevice *dev)
1371 #if CONFIG_IS_ENABLED(DM_GPIO)
1372 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1373 value = dm_gpio_get_value(&priv->cd_gpio);
1375 /* if no CD return as 1 */
1382 static int omap_hsmmc_getwp(struct udevice *dev)
1385 #if CONFIG_IS_ENABLED(DM_GPIO)
1386 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1387 value = dm_gpio_get_value(&priv->wp_gpio);
1389 /* if no WP return as 0 */
1395 static int omap_hsmmc_getcd(struct mmc *mmc)
1397 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1400 /* if no CD return as 1 */
1401 cd_gpio = priv->cd_gpio;
1405 /* NOTE: assumes card detect signal is active-low */
1406 return !gpio_get_value(cd_gpio);
1409 static int omap_hsmmc_getwp(struct mmc *mmc)
1411 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1414 /* if no WP return as 0 */
1415 wp_gpio = priv->wp_gpio;
1419 /* NOTE: assumes write protect signal is active-high */
1420 return gpio_get_value(wp_gpio);
1425 #if CONFIG_IS_ENABLED(DM_MMC)
1426 static const struct dm_mmc_ops omap_hsmmc_ops = {
1427 .send_cmd = omap_hsmmc_send_cmd,
1428 .set_ios = omap_hsmmc_set_ios,
1429 #ifdef OMAP_HSMMC_USE_GPIO
1430 .get_cd = omap_hsmmc_getcd,
1431 .get_wp = omap_hsmmc_getwp,
1433 #ifdef MMC_SUPPORTS_TUNING
1434 .execute_tuning = omap_hsmmc_execute_tuning,
1436 .send_init_stream = omap_hsmmc_send_init_stream,
1437 #if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
1438 .wait_dat0 = omap_hsmmc_wait_dat0,
1442 static const struct mmc_ops omap_hsmmc_ops = {
1443 .send_cmd = omap_hsmmc_send_cmd,
1444 .set_ios = omap_hsmmc_set_ios,
1445 .init = omap_hsmmc_init_setup,
1446 #ifdef OMAP_HSMMC_USE_GPIO
1447 .getcd = omap_hsmmc_getcd,
1448 .getwp = omap_hsmmc_getwp,
1453 #if !CONFIG_IS_ENABLED(DM_MMC)
1454 int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1458 struct omap_hsmmc_data *priv;
1459 struct mmc_config *cfg;
1462 priv = calloc(1, sizeof(*priv));
1466 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
1468 switch (dev_index) {
1470 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
1472 #ifdef OMAP_HSMMC2_BASE
1474 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
1475 #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
1476 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
1477 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1478 defined(CONFIG_HSMMC2_8BIT)
1479 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1480 host_caps_val |= MMC_MODE_8BIT;
1484 #ifdef OMAP_HSMMC3_BASE
1486 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
1487 #if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
1488 /* Enable 8-bit interface for eMMC on DRA7XX */
1489 host_caps_val |= MMC_MODE_8BIT;
1494 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
1497 #ifdef OMAP_HSMMC_USE_GPIO
1498 /* on error gpio values are set to -1, which is what we want */
1499 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1500 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
1505 cfg->name = "OMAP SD/MMC";
1506 cfg->ops = &omap_hsmmc_ops;
1508 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1509 cfg->host_caps = host_caps_val & ~host_caps_mask;
1511 cfg->f_min = 400000;
1516 if (cfg->host_caps & MMC_MODE_HS) {
1517 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1518 cfg->f_max = 52000000;
1520 cfg->f_max = 26000000;
1522 cfg->f_max = 20000000;
1525 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1527 #if defined(CONFIG_OMAP34XX)
1529 * Silicon revs 2.1 and older do not support multiblock transfers.
1531 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
1535 mmc = mmc_create(cfg, priv);
1543 #ifdef CONFIG_IODELAY_RECALIBRATION
1544 static struct pad_conf_entry *
1545 omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1548 struct pad_conf_entry *padconf;
1550 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1552 debug("failed to allocate memory\n");
1556 while (index < count) {
1557 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1558 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1565 static struct iodelay_cfg_entry *
1566 omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1569 struct iodelay_cfg_entry *iodelay;
1571 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1573 debug("failed to allocate memory\n");
1577 while (index < count) {
1578 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1579 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1580 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1587 static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1588 const char *name, int *len)
1590 const void *fdt = gd->fdt_blob;
1592 const fdt32_t *pinctrl;
1594 offset = fdt_node_offset_by_phandle(fdt, phandle);
1596 debug("failed to get pinctrl node %s.\n",
1597 fdt_strerror(offset));
1601 pinctrl = fdt_getprop(fdt, offset, name, len);
1603 debug("failed to get property %s\n", name);
1610 static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1613 const void *fdt = gd->fdt_blob;
1614 const __be32 *phandle;
1615 int node = dev_of_offset(mmc->dev);
1617 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1619 debug("failed to get property %s\n", prop_name);
1623 return fdt32_to_cpu(*phandle);
1626 static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1629 const void *fdt = gd->fdt_blob;
1630 const __be32 *phandle;
1633 int node = dev_of_offset(mmc->dev);
1635 phandle = fdt_getprop(fdt, node, prop_name, &len);
1637 debug("failed to get property %s\n", prop_name);
1641 /* No manual mode iodelay values if count < 2 */
1642 count = len / sizeof(*phandle);
1646 return fdt32_to_cpu(*(phandle + 1));
1649 static struct pad_conf_entry *
1650 omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1654 struct pad_conf_entry *padconf;
1656 const fdt32_t *pinctrl;
1658 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1660 return ERR_PTR(-EINVAL);
1662 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1665 return ERR_PTR(-EINVAL);
1667 count = (len / sizeof(*pinctrl)) / 2;
1668 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1670 return ERR_PTR(-EINVAL);
1677 static struct iodelay_cfg_entry *
1678 omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1682 struct iodelay_cfg_entry *iodelay;
1684 const fdt32_t *pinctrl;
1686 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1687 /* Not all modes have manual mode iodelay values. So its not fatal */
1691 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1694 return ERR_PTR(-EINVAL);
1696 count = (len / sizeof(*pinctrl)) / 3;
1697 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1699 return ERR_PTR(-EINVAL);
1706 static struct omap_hsmmc_pinctrl_state *
1707 omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1712 const void *fdt = gd->fdt_blob;
1713 int node = dev_of_offset(mmc->dev);
1715 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1717 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1718 malloc(sizeof(*pinctrl_state));
1719 if (!pinctrl_state) {
1720 debug("failed to allocate memory\n");
1724 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1726 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1727 goto err_pinctrl_state;
1730 sprintf(prop_name, "pinctrl-%d", index);
1732 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1734 if (IS_ERR(pinctrl_state->padconf))
1735 goto err_pinctrl_state;
1736 pinctrl_state->npads = npads;
1738 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1740 if (IS_ERR(pinctrl_state->iodelay))
1742 pinctrl_state->niodelays = niodelays;
1744 return pinctrl_state;
1747 kfree(pinctrl_state->padconf);
1750 kfree(pinctrl_state);
1754 #define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
1756 struct omap_hsmmc_pinctrl_state *s = NULL; \
1758 if (!(cfg->host_caps & capmask)) \
1761 if (priv->hw_rev) { \
1762 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1763 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1767 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1769 if (!s && !optional) { \
1770 debug("%s: no pinctrl for %s\n", \
1771 mmc->dev->name, #mode); \
1772 cfg->host_caps &= ~(capmask); \
1774 priv->mode##_pinctrl_state = s; \
1778 static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1780 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1781 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1782 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1784 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1787 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1788 if (!default_pinctrl) {
1789 printf("no pinctrl state for default mode\n");
1793 priv->default_pinctrl_state = default_pinctrl;
1795 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1796 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1797 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1798 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1799 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
1801 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1802 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1803 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
1809 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1810 #ifdef CONFIG_OMAP54XX
1811 __weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1817 static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1819 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1820 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1822 struct mmc_config *cfg = &plat->cfg;
1823 #ifdef CONFIG_OMAP54XX
1824 const struct mmc_platform_fixups *fixups;
1826 const void *fdt = gd->fdt_blob;
1827 int node = dev_of_offset(dev);
1830 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1831 sizeof(struct hsmmc *),
1834 ret = mmc_of_parse(dev, cfg);
1839 cfg->f_max = 52000000;
1840 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1841 cfg->f_min = 400000;
1842 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1843 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1844 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1845 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1846 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1847 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
1849 plat->controller_flags |= of_data->controller_flags;
1851 #ifdef CONFIG_OMAP54XX
1852 fixups = platform_fixups_mmc(devfdt_get_addr(dev));
1854 plat->hw_rev = fixups->hw_rev;
1855 cfg->host_caps &= ~fixups->unsupported_caps;
1856 cfg->f_max = fixups->max_freq;
1866 static int omap_hsmmc_bind(struct udevice *dev)
1868 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1869 plat->mmc = calloc(1, sizeof(struct mmc));
1870 return mmc_bind(dev, plat->mmc, &plat->cfg);
1873 static int omap_hsmmc_probe(struct udevice *dev)
1875 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1876 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1877 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1878 struct mmc_config *cfg = &plat->cfg;
1880 #ifdef CONFIG_IODELAY_RECALIBRATION
1884 cfg->name = "OMAP SD/MMC";
1885 priv->base_addr = plat->base_addr;
1886 priv->controller_flags = plat->controller_flags;
1887 priv->hw_rev = plat->hw_rev;
1892 mmc = mmc_create(cfg, priv);
1896 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1897 device_get_supply_regulator(dev, "pbias-supply",
1898 &priv->pbias_supply);
1900 #if defined(OMAP_HSMMC_USE_GPIO)
1901 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
1902 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1903 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1910 #ifdef CONFIG_IODELAY_RECALIBRATION
1911 ret = omap_hsmmc_get_pinctrl_state(mmc);
1913 * disable high speed modes for the platforms that require IO delay
1914 * and for which we don't have this information
1917 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1918 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
1919 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
1924 return omap_hsmmc_init_setup(mmc);
1927 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1929 static const struct omap_mmc_of_data dra7_mmc_of_data = {
1930 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
1933 static const struct udevice_id omap_hsmmc_ids[] = {
1934 { .compatible = "ti,omap3-hsmmc" },
1935 { .compatible = "ti,omap4-hsmmc" },
1936 { .compatible = "ti,am33xx-hsmmc" },
1937 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
1942 U_BOOT_DRIVER(omap_hsmmc) = {
1943 .name = "omap_hsmmc",
1945 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1946 .of_match = omap_hsmmc_ids,
1947 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
1948 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
1951 .bind = omap_hsmmc_bind,
1953 .ops = &omap_hsmmc_ops,
1954 .probe = omap_hsmmc_probe,
1955 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
1956 #if !CONFIG_IS_ENABLED(OF_CONTROL)
1957 .flags = DM_FLAG_PRE_RELOC,