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>
52 DECLARE_GLOBAL_DATA_PTR;
54 /* simplify defines to OMAP_HSMMC_USE_GPIO */
55 #if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
56 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
57 #define OMAP_HSMMC_USE_GPIO
59 #undef OMAP_HSMMC_USE_GPIO
62 /* common definitions for all OMAPs */
63 #define SYSCTL_SRC (1 << 25)
64 #define SYSCTL_SRD (1 << 26)
66 #ifdef CONFIG_IODELAY_RECALIBRATION
67 struct omap_hsmmc_pinctrl_state {
68 struct pad_conf_entry *padconf;
70 struct iodelay_cfg_entry *iodelay;
75 struct omap_hsmmc_data {
76 struct hsmmc *base_addr;
77 #if !CONFIG_IS_ENABLED(DM_MMC)
78 struct mmc_config cfg;
83 #ifdef OMAP_HSMMC_USE_GPIO
84 #if CONFIG_IS_ENABLED(DM_MMC)
85 struct gpio_desc cd_gpio; /* Change Detect GPIO */
86 struct gpio_desc wp_gpio; /* Write Protect GPIO */
92 #if CONFIG_IS_ENABLED(DM_MMC)
96 #ifdef CONFIG_MMC_OMAP_HS_ADMA
97 struct omap_hsmmc_adma_desc *adma_desc_table;
101 struct udevice *pbias_supply;
103 #ifdef CONFIG_IODELAY_RECALIBRATION
104 struct omap_hsmmc_pinctrl_state *default_pinctrl_state;
105 struct omap_hsmmc_pinctrl_state *hs_pinctrl_state;
106 struct omap_hsmmc_pinctrl_state *hs200_1_8v_pinctrl_state;
107 struct omap_hsmmc_pinctrl_state *ddr_1_8v_pinctrl_state;
108 struct omap_hsmmc_pinctrl_state *sdr12_pinctrl_state;
109 struct omap_hsmmc_pinctrl_state *sdr25_pinctrl_state;
110 struct omap_hsmmc_pinctrl_state *ddr50_pinctrl_state;
111 struct omap_hsmmc_pinctrl_state *sdr50_pinctrl_state;
112 struct omap_hsmmc_pinctrl_state *sdr104_pinctrl_state;
116 struct omap_mmc_of_data {
120 #ifdef CONFIG_MMC_OMAP_HS_ADMA
121 struct omap_hsmmc_adma_desc {
128 #define ADMA_MAX_LEN 63488
130 /* Decriptor table defines */
131 #define ADMA_DESC_ATTR_VALID BIT(0)
132 #define ADMA_DESC_ATTR_END BIT(1)
133 #define ADMA_DESC_ATTR_INT BIT(2)
134 #define ADMA_DESC_ATTR_ACT1 BIT(4)
135 #define ADMA_DESC_ATTR_ACT2 BIT(5)
137 #define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
138 #define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
141 /* If we fail after 1 second wait, something is really bad */
142 #define MAX_RETRY_MS 1000
143 #define MMC_TIMEOUT_MS 20
145 /* DMA transfers can take a long time if a lot a data is transferred.
146 * The timeout must take in account the amount of data. Let's assume
147 * that the time will never exceed 333 ms per MB (in other word we assume
148 * that the bandwidth is always above 3MB/s).
150 #define DMA_TIMEOUT_PER_MB 333
151 #define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
152 #define OMAP_HSMMC_NO_1_8_V BIT(1)
153 #define OMAP_HSMMC_USE_ADMA BIT(2)
154 #define OMAP_HSMMC_REQUIRE_IODELAY BIT(3)
156 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
157 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
159 static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
160 static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
161 static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
163 static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
165 #if CONFIG_IS_ENABLED(DM_MMC)
166 return dev_get_priv(mmc->dev);
168 return (struct omap_hsmmc_data *)mmc->priv;
171 static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
173 #if CONFIG_IS_ENABLED(DM_MMC)
174 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
177 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
181 #if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
182 static int omap_mmc_setup_gpio_in(int gpio, const char *label)
186 #ifndef CONFIG_DM_GPIO
187 if (!gpio_is_valid(gpio))
190 ret = gpio_request(gpio, label);
194 ret = gpio_direction_input(gpio);
202 static unsigned char mmc_board_init(struct mmc *mmc)
204 #if defined(CONFIG_OMAP34XX)
205 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
206 t2_t *t2_base = (t2_t *)T2_BASE;
207 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
209 #ifdef CONFIG_MMC_OMAP36XX_PINS
210 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
213 pbias_lite = readl(&t2_base->pbias_lite);
214 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
215 #ifdef CONFIG_TARGET_OMAP3_CAIRO
216 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
217 pbias_lite &= ~PBIASLITEVMODE0;
219 #ifdef CONFIG_TARGET_OMAP3_LOGIC
220 /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
221 pbias_lite &= ~PBIASLITEVMODE1;
223 #ifdef CONFIG_MMC_OMAP36XX_PINS
224 if (get_cpu_family() == CPU_OMAP36XX) {
225 /* Disable extended drain IO before changing PBIAS */
226 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
227 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
230 writel(pbias_lite, &t2_base->pbias_lite);
232 writel(pbias_lite | PBIASLITEPWRDNZ1 |
233 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
234 &t2_base->pbias_lite);
236 #ifdef CONFIG_MMC_OMAP36XX_PINS
237 if (get_cpu_family() == CPU_OMAP36XX)
238 /* Enable extended drain IO after changing PBIAS */
240 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
241 OMAP34XX_CTRL_WKUP_CTRL);
243 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
246 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
249 /* Change from default of 52MHz to 26MHz if necessary */
250 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
251 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
252 &t2_base->ctl_prog_io1);
254 writel(readl(&prcm_base->fclken1_core) |
255 EN_MMC1 | EN_MMC2 | EN_MMC3,
256 &prcm_base->fclken1_core);
258 writel(readl(&prcm_base->iclken1_core) |
259 EN_MMC1 | EN_MMC2 | EN_MMC3,
260 &prcm_base->iclken1_core);
263 #if (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) &&\
264 !CONFIG_IS_ENABLED(DM_REGULATOR)
265 /* PBIAS config needed for MMC1 only */
266 if (mmc_get_blk_desc(mmc)->devnum == 0)
267 vmmc_pbias_config(LDO_VOLT_3V3);
273 void mmc_init_stream(struct hsmmc *mmc_base)
277 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
279 writel(MMC_CMD0, &mmc_base->cmd);
280 start = get_timer(0);
281 while (!(readl(&mmc_base->stat) & CC_MASK)) {
282 if (get_timer(0) - start > MAX_RETRY_MS) {
283 printf("%s: timedout waiting for cc!\n", __func__);
287 writel(CC_MASK, &mmc_base->stat)
289 writel(MMC_CMD0, &mmc_base->cmd)
291 start = get_timer(0);
292 while (!(readl(&mmc_base->stat) & CC_MASK)) {
293 if (get_timer(0) - start > MAX_RETRY_MS) {
294 printf("%s: timedout waiting for cc2!\n", __func__);
298 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
301 #if CONFIG_IS_ENABLED(DM_MMC)
302 #ifdef CONFIG_IODELAY_RECALIBRATION
303 static void omap_hsmmc_io_recalibrate(struct mmc *mmc)
305 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
306 struct omap_hsmmc_pinctrl_state *pinctrl_state;
308 switch (priv->mode) {
310 pinctrl_state = priv->hs200_1_8v_pinctrl_state;
313 pinctrl_state = priv->sdr104_pinctrl_state;
316 pinctrl_state = priv->sdr50_pinctrl_state;
319 pinctrl_state = priv->ddr50_pinctrl_state;
322 pinctrl_state = priv->sdr25_pinctrl_state;
325 pinctrl_state = priv->sdr12_pinctrl_state;
330 pinctrl_state = priv->hs_pinctrl_state;
333 pinctrl_state = priv->ddr_1_8v_pinctrl_state;
335 pinctrl_state = priv->default_pinctrl_state;
340 pinctrl_state = priv->default_pinctrl_state;
342 if (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
343 if (pinctrl_state->iodelay)
344 late_recalibrate_iodelay(pinctrl_state->padconf,
345 pinctrl_state->npads,
346 pinctrl_state->iodelay,
347 pinctrl_state->niodelays);
349 do_set_mux32((*ctrl)->control_padconf_core_base,
350 pinctrl_state->padconf,
351 pinctrl_state->npads);
355 static void omap_hsmmc_set_timing(struct mmc *mmc)
358 struct hsmmc *mmc_base;
359 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
361 mmc_base = priv->base_addr;
363 omap_hsmmc_stop_clock(mmc_base);
364 val = readl(&mmc_base->ac12);
365 val &= ~AC12_UHSMC_MASK;
366 priv->mode = mmc->selected_mode;
368 if (mmc_is_mode_ddr(priv->mode))
369 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
371 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
373 switch (priv->mode) {
376 val |= AC12_UHSMC_SDR104;
379 val |= AC12_UHSMC_SDR50;
383 val |= AC12_UHSMC_DDR50;
388 val |= AC12_UHSMC_SDR25;
394 val |= AC12_UHSMC_SDR12;
397 val |= AC12_UHSMC_RES;
400 writel(val, &mmc_base->ac12);
402 #ifdef CONFIG_IODELAY_RECALIBRATION
403 omap_hsmmc_io_recalibrate(mmc);
405 omap_hsmmc_start_clock(mmc_base);
408 static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
410 struct hsmmc *mmc_base;
411 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
414 mmc_base = priv->base_addr;
416 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
417 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
419 switch (signal_voltage) {
420 case MMC_SIGNAL_VOLTAGE_330:
423 case MMC_SIGNAL_VOLTAGE_180:
425 ac12 |= AC12_V1V8_SIGEN;
429 writel(hctl, &mmc_base->hctl);
430 writel(ac12, &mmc_base->ac12);
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);
459 #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
460 #if CONFIG_IS_ENABLED(DM_REGULATOR)
461 static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
466 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
468 if (!mmc->vqmmc_supply)
472 ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
476 /* Turn off IO voltage */
477 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
480 /* Program a new IO voltage value */
481 ret = regulator_set_value(mmc->vqmmc_supply, uV);
484 /* Turn on IO voltage */
485 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
489 /* Program PBIAS voltage*/
490 ret = regulator_set_value(priv->pbias_supply, uV);
491 if (ret && ret != -ENOSYS)
494 ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
502 static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
504 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
505 struct hsmmc *mmc_base = priv->base_addr;
506 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
508 __maybe_unused u8 palmas_ldo_volt;
514 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
516 capa_mask = VS33_3V3SUP;
517 palmas_ldo_volt = LDO_VOLT_3V3;
518 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
519 capa_mask = VS18_1V8SUP;
520 palmas_ldo_volt = LDO_VOLT_1V8;
525 val = readl(&mmc_base->capa);
526 if (!(val & capa_mask))
529 priv->signal_voltage = mmc->signal_voltage;
531 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
533 #if CONFIG_IS_ENABLED(DM_REGULATOR)
534 return omap_hsmmc_set_io_regulator(mmc, mv);
535 #elif (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) && \
536 defined(CONFIG_PALMAS_POWER)
537 if (mmc_get_blk_desc(mmc)->devnum == 0)
538 vmmc_pbias_config(palmas_ldo_volt);
546 static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
548 struct hsmmc *mmc_base;
549 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
552 mmc_base = priv->base_addr;
553 val = readl(&mmc_base->capa);
555 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
556 val |= (VS33_3V3SUP | VS18_1V8SUP);
557 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
565 writel(val, &mmc_base->capa);
570 #ifdef MMC_SUPPORTS_TUNING
571 static void omap_hsmmc_disable_tuning(struct mmc *mmc)
573 struct hsmmc *mmc_base;
574 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
577 mmc_base = priv->base_addr;
578 val = readl(&mmc_base->ac12);
579 val &= ~(AC12_SCLK_SEL);
580 writel(val, &mmc_base->ac12);
582 val = readl(&mmc_base->dll);
583 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
584 writel(val, &mmc_base->dll);
587 static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
590 struct hsmmc *mmc_base;
591 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
594 mmc_base = priv->base_addr;
595 val = readl(&mmc_base->dll);
596 val |= DLL_FORCE_VALUE;
597 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
598 val |= (count << DLL_FORCE_SR_C_SHIFT);
599 writel(val, &mmc_base->dll);
602 writel(val, &mmc_base->dll);
603 for (i = 0; i < 1000; i++) {
604 if (readl(&mmc_base->dll) & DLL_CALIB)
608 writel(val, &mmc_base->dll);
611 static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
613 struct omap_hsmmc_data *priv = dev_get_priv(dev);
614 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
615 struct mmc *mmc = upriv->mmc;
616 struct hsmmc *mmc_base;
618 u8 cur_match, prev_match = 0;
621 u32 start_window = 0, max_window = 0;
622 u32 length = 0, max_len = 0;
623 bool single_point_failure = false;
624 struct udevice *thermal_dev;
628 mmc_base = priv->base_addr;
629 val = readl(&mmc_base->capa2);
631 /* clock tuning is not needed for upto 52MHz */
632 if (!((mmc->selected_mode == MMC_HS_200) ||
633 (mmc->selected_mode == UHS_SDR104) ||
634 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
637 ret = uclass_first_device(UCLASS_THERMAL, &thermal_dev);
639 printf("Couldn't get thermal device for tuning\n");
642 ret = thermal_get_temp(thermal_dev, &temperature);
644 printf("Couldn't get temperature for tuning\n");
647 val = readl(&mmc_base->dll);
649 writel(val, &mmc_base->dll);
652 * Stage 1: Search for a maximum pass window ignoring any
653 * any single point failures. If the tuning value ends up
654 * near it, move away from it in stage 2 below
656 while (phase_delay <= MAX_PHASE_DELAY) {
657 omap_hsmmc_set_dll(mmc, phase_delay);
659 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
664 } else if (single_point_failure) {
665 /* ignore single point failure */
667 single_point_failure = false;
669 start_window = phase_delay;
673 single_point_failure = prev_match;
676 if (length > max_len) {
677 max_window = start_window;
681 prev_match = cur_match;
690 val = readl(&mmc_base->ac12);
691 if (!(val & AC12_SCLK_SEL)) {
696 * Assign tuning value as a ratio of maximum pass window based
699 if (temperature < -20000)
700 phase_delay = min(max_window + 4 * max_len - 24,
702 DIV_ROUND_UP(13 * max_len, 16) * 4);
703 else if (temperature < 20000)
704 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
705 else if (temperature < 40000)
706 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
707 else if (temperature < 70000)
708 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
709 else if (temperature < 90000)
710 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
711 else if (temperature < 120000)
712 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
714 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
717 * Stage 2: Search for a single point failure near the chosen tuning
718 * value in two steps. First in the +3 to +10 range and then in the
719 * +2 to -10 range. If found, move away from it in the appropriate
720 * direction by the appropriate amount depending on the temperature.
722 for (i = 3; i <= 10; i++) {
723 omap_hsmmc_set_dll(mmc, phase_delay + i);
724 if (mmc_send_tuning(mmc, opcode, NULL)) {
725 if (temperature < 10000)
726 phase_delay += i + 6;
727 else if (temperature < 20000)
728 phase_delay += i - 12;
729 else if (temperature < 70000)
730 phase_delay += i - 8;
731 else if (temperature < 90000)
732 phase_delay += i - 6;
734 phase_delay += i - 6;
736 goto single_failure_found;
740 for (i = 2; i >= -10; i--) {
741 omap_hsmmc_set_dll(mmc, phase_delay + i);
742 if (mmc_send_tuning(mmc, opcode, NULL)) {
743 if (temperature < 10000)
744 phase_delay += i + 12;
745 else if (temperature < 20000)
746 phase_delay += i + 8;
747 else if (temperature < 70000)
748 phase_delay += i + 8;
749 else if (temperature < 90000)
750 phase_delay += i + 10;
752 phase_delay += i + 12;
754 goto single_failure_found;
758 single_failure_found:
760 omap_hsmmc_set_dll(mmc, phase_delay);
762 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
763 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
769 omap_hsmmc_disable_tuning(mmc);
770 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
771 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
778 static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
780 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
781 struct hsmmc *mmc_base = priv->base_addr;
782 u32 irq_mask = INT_EN_MASK;
785 * TODO: Errata i802 indicates only DCRC interrupts can occur during
786 * tuning procedure and DCRC should be disabled. But see occurences
787 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
788 * interrupts occur along with BRR, so the data is actually in the
789 * buffer. It has to be debugged why these interrutps occur
791 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
792 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
794 writel(irq_mask, &mmc_base->ie);
797 static int omap_hsmmc_init_setup(struct mmc *mmc)
799 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
800 struct hsmmc *mmc_base;
801 unsigned int reg_val;
805 mmc_base = priv->base_addr;
808 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
809 &mmc_base->sysconfig);
810 start = get_timer(0);
811 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
812 if (get_timer(0) - start > MAX_RETRY_MS) {
813 printf("%s: timedout waiting for cc2!\n", __func__);
817 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
818 start = get_timer(0);
819 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
820 if (get_timer(0) - start > MAX_RETRY_MS) {
821 printf("%s: timedout waiting for softresetall!\n",
826 #ifdef CONFIG_MMC_OMAP_HS_ADMA
827 reg_val = readl(&mmc_base->hl_hwinfo);
828 if (reg_val & MADMA_EN)
829 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
832 #if CONFIG_IS_ENABLED(DM_MMC)
833 reg_val = omap_hsmmc_set_capabilities(mmc);
834 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
835 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
837 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
838 writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
842 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
844 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
845 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
846 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
849 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
850 (ICE_STOP | DTO_15THDTO));
851 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
852 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
853 start = get_timer(0);
854 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
855 if (get_timer(0) - start > MAX_RETRY_MS) {
856 printf("%s: timedout waiting for ics!\n", __func__);
860 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
862 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
864 mmc_enable_irq(mmc, NULL);
866 #if !CONFIG_IS_ENABLED(DM_MMC)
867 mmc_init_stream(mmc_base);
874 * MMC controller internal finite state machine reset
876 * Used to reset command or data internal state machines, using respectively
877 * SRC or SRD bit of SYSCTL register
879 static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
883 mmc_reg_out(&mmc_base->sysctl, bit, bit);
886 * CMD(DAT) lines reset procedures are slightly different
887 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
888 * According to OMAP3 TRM:
889 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
891 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
892 * procedure steps must be as follows:
893 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
894 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
895 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
896 * 3. Wait until the SRC (SRD) bit returns to 0x0
897 * (reset procedure is completed).
899 #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
900 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
901 if (!(readl(&mmc_base->sysctl) & bit)) {
902 start = get_timer(0);
903 while (!(readl(&mmc_base->sysctl) & bit)) {
904 if (get_timer(0) - start > MMC_TIMEOUT_MS)
909 start = get_timer(0);
910 while ((readl(&mmc_base->sysctl) & bit) != 0) {
911 if (get_timer(0) - start > MAX_RETRY_MS) {
912 printf("%s: timedout waiting for sysctl %x to clear\n",
919 #ifdef CONFIG_MMC_OMAP_HS_ADMA
920 static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
922 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
923 struct omap_hsmmc_adma_desc *desc;
926 desc = &priv->adma_desc_table[priv->desc_slot];
928 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
932 attr |= ADMA_DESC_ATTR_END;
935 desc->addr = (u32)buf;
940 static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
941 struct mmc_data *data)
943 uint total_len = data->blocksize * data->blocks;
944 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
945 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
950 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
951 memalign(ARCH_DMA_MINALIGN, desc_count *
952 sizeof(struct omap_hsmmc_adma_desc));
954 if (data->flags & MMC_DATA_READ)
957 buf = (char *)data->src;
960 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
962 total_len -= ADMA_MAX_LEN;
965 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
967 flush_dcache_range((long)priv->adma_desc_table,
968 (long)priv->adma_desc_table +
970 sizeof(struct omap_hsmmc_adma_desc),
974 static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
976 struct hsmmc *mmc_base;
977 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
981 mmc_base = priv->base_addr;
982 omap_hsmmc_prepare_adma_table(mmc, data);
984 if (data->flags & MMC_DATA_READ)
987 buf = (char *)data->src;
989 val = readl(&mmc_base->hctl);
991 writel(val, &mmc_base->hctl);
993 val = readl(&mmc_base->con);
995 writel(val, &mmc_base->con);
997 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
999 flush_dcache_range((u32)buf,
1001 ROUND(data->blocksize * data->blocks,
1002 ARCH_DMA_MINALIGN));
1005 static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
1007 struct hsmmc *mmc_base;
1008 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1011 mmc_base = priv->base_addr;
1013 val = readl(&mmc_base->con);
1015 writel(val, &mmc_base->con);
1017 val = readl(&mmc_base->hctl);
1019 writel(val, &mmc_base->hctl);
1021 kfree(priv->adma_desc_table);
1024 #define omap_hsmmc_adma_desc
1025 #define omap_hsmmc_prepare_adma_table
1026 #define omap_hsmmc_prepare_data
1027 #define omap_hsmmc_dma_cleanup
1030 #if !CONFIG_IS_ENABLED(DM_MMC)
1031 static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1032 struct mmc_data *data)
1034 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1036 static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1037 struct mmc_data *data)
1039 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1040 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1041 struct mmc *mmc = upriv->mmc;
1043 struct hsmmc *mmc_base;
1044 unsigned int flags, mmc_stat;
1046 priv->last_cmd = cmd->cmdidx;
1048 mmc_base = priv->base_addr;
1050 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
1053 start = get_timer(0);
1054 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
1055 if (get_timer(0) - start > MAX_RETRY_MS) {
1056 printf("%s: timedout waiting on cmd inhibit to clear\n",
1058 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1059 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
1063 writel(0xFFFFFFFF, &mmc_base->stat);
1064 if (readl(&mmc_base->stat)) {
1065 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1066 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
1071 * CMDIDX[13:8] : Command index
1072 * DATAPRNT[5] : Data Present Select
1073 * ENCMDIDX[4] : Command Index Check Enable
1074 * ENCMDCRC[3] : Command CRC Check Enable
1079 * 11 = Length 48 Check busy after response
1081 /* Delay added before checking the status of frq change
1082 * retry not supported by mmc.c(core file)
1084 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1085 udelay(50000); /* wait 50 ms */
1087 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1089 else if (cmd->resp_type & MMC_RSP_136)
1090 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1091 else if (cmd->resp_type & MMC_RSP_BUSY)
1092 flags = RSP_TYPE_LGHT48B;
1094 flags = RSP_TYPE_LGHT48;
1096 /* enable default flags */
1097 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
1099 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
1101 if (cmd->resp_type & MMC_RSP_CRC)
1102 flags |= CCCE_CHECK;
1103 if (cmd->resp_type & MMC_RSP_OPCODE)
1104 flags |= CICE_CHECK;
1107 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1108 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
1109 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
1110 data->blocksize = 512;
1111 writel(data->blocksize | (data->blocks << 16),
1114 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1116 if (data->flags & MMC_DATA_READ)
1117 flags |= (DP_DATA | DDIR_READ);
1119 flags |= (DP_DATA | DDIR_WRITE);
1121 #ifdef CONFIG_MMC_OMAP_HS_ADMA
1122 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1123 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1124 omap_hsmmc_prepare_data(mmc, data);
1130 mmc_enable_irq(mmc, cmd);
1132 writel(cmd->cmdarg, &mmc_base->arg);
1133 udelay(20); /* To fix "No status update" error on eMMC */
1134 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1136 start = get_timer(0);
1138 mmc_stat = readl(&mmc_base->stat);
1139 if (get_timer(start) > MAX_RETRY_MS) {
1140 printf("%s : timeout: No status update\n", __func__);
1143 } while (!mmc_stat);
1145 if ((mmc_stat & IE_CTO) != 0) {
1146 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
1148 } else if ((mmc_stat & ERRI_MASK) != 0)
1151 if (mmc_stat & CC_MASK) {
1152 writel(CC_MASK, &mmc_base->stat);
1153 if (cmd->resp_type & MMC_RSP_PRESENT) {
1154 if (cmd->resp_type & MMC_RSP_136) {
1155 /* response type 2 */
1156 cmd->response[3] = readl(&mmc_base->rsp10);
1157 cmd->response[2] = readl(&mmc_base->rsp32);
1158 cmd->response[1] = readl(&mmc_base->rsp54);
1159 cmd->response[0] = readl(&mmc_base->rsp76);
1161 /* response types 1, 1b, 3, 4, 5, 6 */
1162 cmd->response[0] = readl(&mmc_base->rsp10);
1166 #ifdef CONFIG_MMC_OMAP_HS_ADMA
1167 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1168 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1171 if (mmc_stat & IE_ADMAE) {
1172 omap_hsmmc_dma_cleanup(mmc);
1176 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1177 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1178 if (timeout < MAX_RETRY_MS)
1179 timeout = MAX_RETRY_MS;
1181 start = get_timer(0);
1183 mmc_stat = readl(&mmc_base->stat);
1184 if (mmc_stat & TC_MASK) {
1185 writel(readl(&mmc_base->stat) | TC_MASK,
1189 if (get_timer(start) > timeout) {
1190 printf("%s : DMA timeout: No status update\n",
1196 omap_hsmmc_dma_cleanup(mmc);
1201 if (data && (data->flags & MMC_DATA_READ)) {
1202 mmc_read_data(mmc_base, data->dest,
1203 data->blocksize * data->blocks);
1204 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1205 mmc_write_data(mmc_base, data->src,
1206 data->blocksize * data->blocks);
1211 static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
1213 unsigned int *output_buf = (unsigned int *)buf;
1214 unsigned int mmc_stat;
1220 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1224 ulong start = get_timer(0);
1226 mmc_stat = readl(&mmc_base->stat);
1227 if (get_timer(0) - start > MAX_RETRY_MS) {
1228 printf("%s: timedout waiting for status!\n",
1232 } while (mmc_stat == 0);
1234 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1235 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1237 if ((mmc_stat & ERRI_MASK) != 0)
1240 if (mmc_stat & BRR_MASK) {
1243 writel(readl(&mmc_base->stat) | BRR_MASK,
1245 for (k = 0; k < count; k++) {
1246 *output_buf = readl(&mmc_base->data);
1252 if (mmc_stat & BWR_MASK)
1253 writel(readl(&mmc_base->stat) | BWR_MASK,
1256 if (mmc_stat & TC_MASK) {
1257 writel(readl(&mmc_base->stat) | TC_MASK,
1265 #if CONFIG_IS_ENABLED(MMC_WRITE)
1266 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1269 unsigned int *input_buf = (unsigned int *)buf;
1270 unsigned int mmc_stat;
1274 * Start Polled Write
1276 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1280 ulong start = get_timer(0);
1282 mmc_stat = readl(&mmc_base->stat);
1283 if (get_timer(0) - start > MAX_RETRY_MS) {
1284 printf("%s: timedout waiting for status!\n",
1288 } while (mmc_stat == 0);
1290 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1291 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1293 if ((mmc_stat & ERRI_MASK) != 0)
1296 if (mmc_stat & BWR_MASK) {
1299 writel(readl(&mmc_base->stat) | BWR_MASK,
1301 for (k = 0; k < count; k++) {
1302 writel(*input_buf, &mmc_base->data);
1308 if (mmc_stat & BRR_MASK)
1309 writel(readl(&mmc_base->stat) | BRR_MASK,
1312 if (mmc_stat & TC_MASK) {
1313 writel(readl(&mmc_base->stat) | TC_MASK,
1321 static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1327 static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1329 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1332 static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1334 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1337 static void omap_hsmmc_set_clock(struct mmc *mmc)
1339 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1340 struct hsmmc *mmc_base;
1341 unsigned int dsor = 0;
1344 mmc_base = priv->base_addr;
1345 omap_hsmmc_stop_clock(mmc_base);
1347 /* TODO: Is setting DTO required here? */
1348 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1349 (ICE_STOP | DTO_15THDTO));
1351 if (mmc->clock != 0) {
1352 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1353 if (dsor > CLKD_MAX)
1359 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1360 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1362 start = get_timer(0);
1363 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1364 if (get_timer(0) - start > MAX_RETRY_MS) {
1365 printf("%s: timedout waiting for ics!\n", __func__);
1370 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1371 mmc->clock = priv->clock;
1372 omap_hsmmc_start_clock(mmc_base);
1375 static void omap_hsmmc_set_bus_width(struct mmc *mmc)
1377 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1378 struct hsmmc *mmc_base;
1380 mmc_base = priv->base_addr;
1381 /* configue bus width */
1382 switch (mmc->bus_width) {
1384 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1389 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1391 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1397 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1399 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1404 priv->bus_width = mmc->bus_width;
1407 #if !CONFIG_IS_ENABLED(DM_MMC)
1408 static int omap_hsmmc_set_ios(struct mmc *mmc)
1410 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1412 static int omap_hsmmc_set_ios(struct udevice *dev)
1414 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1415 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1416 struct mmc *mmc = upriv->mmc;
1418 struct hsmmc *mmc_base = priv->base_addr;
1421 if (priv->bus_width != mmc->bus_width)
1422 omap_hsmmc_set_bus_width(mmc);
1424 if (priv->clock != mmc->clock)
1425 omap_hsmmc_set_clock(mmc);
1427 if (mmc->clk_disable)
1428 omap_hsmmc_stop_clock(mmc_base);
1430 omap_hsmmc_start_clock(mmc_base);
1432 #if CONFIG_IS_ENABLED(DM_MMC)
1433 if (priv->mode != mmc->selected_mode)
1434 omap_hsmmc_set_timing(mmc);
1436 #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1437 if (priv->signal_voltage != mmc->signal_voltage)
1438 ret = omap_hsmmc_set_signal_voltage(mmc);
1444 #ifdef OMAP_HSMMC_USE_GPIO
1445 #if CONFIG_IS_ENABLED(DM_MMC)
1446 static int omap_hsmmc_getcd(struct udevice *dev)
1449 #if CONFIG_IS_ENABLED(DM_GPIO)
1450 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1451 value = dm_gpio_get_value(&priv->cd_gpio);
1453 /* if no CD return as 1 */
1460 static int omap_hsmmc_getwp(struct udevice *dev)
1463 #if CONFIG_IS_ENABLED(DM_GPIO)
1464 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1465 value = dm_gpio_get_value(&priv->wp_gpio);
1467 /* if no WP return as 0 */
1473 static int omap_hsmmc_getcd(struct mmc *mmc)
1475 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1478 /* if no CD return as 1 */
1479 cd_gpio = priv->cd_gpio;
1483 /* NOTE: assumes card detect signal is active-low */
1484 return !gpio_get_value(cd_gpio);
1487 static int omap_hsmmc_getwp(struct mmc *mmc)
1489 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1492 /* if no WP return as 0 */
1493 wp_gpio = priv->wp_gpio;
1497 /* NOTE: assumes write protect signal is active-high */
1498 return gpio_get_value(wp_gpio);
1503 #if CONFIG_IS_ENABLED(DM_MMC)
1504 static const struct dm_mmc_ops omap_hsmmc_ops = {
1505 .send_cmd = omap_hsmmc_send_cmd,
1506 .set_ios = omap_hsmmc_set_ios,
1507 #ifdef OMAP_HSMMC_USE_GPIO
1508 .get_cd = omap_hsmmc_getcd,
1509 .get_wp = omap_hsmmc_getwp,
1511 #ifdef MMC_SUPPORTS_TUNING
1512 .execute_tuning = omap_hsmmc_execute_tuning,
1514 .wait_dat0 = omap_hsmmc_wait_dat0,
1517 static const struct mmc_ops omap_hsmmc_ops = {
1518 .send_cmd = omap_hsmmc_send_cmd,
1519 .set_ios = omap_hsmmc_set_ios,
1520 .init = omap_hsmmc_init_setup,
1521 #ifdef OMAP_HSMMC_USE_GPIO
1522 .getcd = omap_hsmmc_getcd,
1523 .getwp = omap_hsmmc_getwp,
1528 #if !CONFIG_IS_ENABLED(DM_MMC)
1529 int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1533 struct omap_hsmmc_data *priv;
1534 struct mmc_config *cfg;
1537 priv = calloc(1, sizeof(*priv));
1541 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
1543 switch (dev_index) {
1545 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
1547 #ifdef OMAP_HSMMC2_BASE
1549 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
1550 #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
1551 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
1552 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1553 defined(CONFIG_HSMMC2_8BIT)
1554 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1555 host_caps_val |= MMC_MODE_8BIT;
1559 #ifdef OMAP_HSMMC3_BASE
1561 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
1562 #if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
1563 /* Enable 8-bit interface for eMMC on DRA7XX */
1564 host_caps_val |= MMC_MODE_8BIT;
1569 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
1572 #ifdef OMAP_HSMMC_USE_GPIO
1573 /* on error gpio values are set to -1, which is what we want */
1574 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1575 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
1580 cfg->name = "OMAP SD/MMC";
1581 cfg->ops = &omap_hsmmc_ops;
1583 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1584 cfg->host_caps = host_caps_val & ~host_caps_mask;
1586 cfg->f_min = 400000;
1591 if (cfg->host_caps & MMC_MODE_HS) {
1592 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1593 cfg->f_max = 52000000;
1595 cfg->f_max = 26000000;
1597 cfg->f_max = 20000000;
1600 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1602 #if defined(CONFIG_OMAP34XX)
1604 * Silicon revs 2.1 and older do not support multiblock transfers.
1606 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
1610 mmc = mmc_create(cfg, priv);
1618 #ifdef CONFIG_IODELAY_RECALIBRATION
1619 static struct pad_conf_entry *
1620 omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1623 struct pad_conf_entry *padconf;
1625 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1627 debug("failed to allocate memory\n");
1631 while (index < count) {
1632 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1633 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1640 static struct iodelay_cfg_entry *
1641 omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1644 struct iodelay_cfg_entry *iodelay;
1646 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1648 debug("failed to allocate memory\n");
1652 while (index < count) {
1653 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1654 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1655 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1662 static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1663 const char *name, int *len)
1665 const void *fdt = gd->fdt_blob;
1667 const fdt32_t *pinctrl;
1669 offset = fdt_node_offset_by_phandle(fdt, phandle);
1671 debug("failed to get pinctrl node %s.\n",
1672 fdt_strerror(offset));
1676 pinctrl = fdt_getprop(fdt, offset, name, len);
1678 debug("failed to get property %s\n", name);
1685 static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1688 const void *fdt = gd->fdt_blob;
1689 const __be32 *phandle;
1690 int node = dev_of_offset(mmc->dev);
1692 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1694 debug("failed to get property %s\n", prop_name);
1698 return fdt32_to_cpu(*phandle);
1701 static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1704 const void *fdt = gd->fdt_blob;
1705 const __be32 *phandle;
1708 int node = dev_of_offset(mmc->dev);
1710 phandle = fdt_getprop(fdt, node, prop_name, &len);
1712 debug("failed to get property %s\n", prop_name);
1716 /* No manual mode iodelay values if count < 2 */
1717 count = len / sizeof(*phandle);
1721 return fdt32_to_cpu(*(phandle + 1));
1724 static struct pad_conf_entry *
1725 omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1729 struct pad_conf_entry *padconf;
1731 const fdt32_t *pinctrl;
1733 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1735 return ERR_PTR(-EINVAL);
1737 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1740 return ERR_PTR(-EINVAL);
1742 count = (len / sizeof(*pinctrl)) / 2;
1743 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1745 return ERR_PTR(-EINVAL);
1752 static struct iodelay_cfg_entry *
1753 omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1757 struct iodelay_cfg_entry *iodelay;
1759 const fdt32_t *pinctrl;
1761 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1762 /* Not all modes have manual mode iodelay values. So its not fatal */
1766 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1769 return ERR_PTR(-EINVAL);
1771 count = (len / sizeof(*pinctrl)) / 3;
1772 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1774 return ERR_PTR(-EINVAL);
1781 static struct omap_hsmmc_pinctrl_state *
1782 omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1787 const void *fdt = gd->fdt_blob;
1788 int node = dev_of_offset(mmc->dev);
1790 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1792 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1793 malloc(sizeof(*pinctrl_state));
1794 if (!pinctrl_state) {
1795 debug("failed to allocate memory\n");
1799 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1801 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1802 goto err_pinctrl_state;
1805 sprintf(prop_name, "pinctrl-%d", index);
1807 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1809 if (IS_ERR(pinctrl_state->padconf))
1810 goto err_pinctrl_state;
1811 pinctrl_state->npads = npads;
1813 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1815 if (IS_ERR(pinctrl_state->iodelay))
1817 pinctrl_state->niodelays = niodelays;
1819 return pinctrl_state;
1822 kfree(pinctrl_state->padconf);
1825 kfree(pinctrl_state);
1829 #define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
1831 struct omap_hsmmc_pinctrl_state *s = NULL; \
1833 if (!(cfg->host_caps & capmask)) \
1836 if (priv->hw_rev) { \
1837 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1838 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1842 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1844 if (!s && !optional) { \
1845 debug("%s: no pinctrl for %s\n", \
1846 mmc->dev->name, #mode); \
1847 cfg->host_caps &= ~(capmask); \
1849 priv->mode##_pinctrl_state = s; \
1853 static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1855 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1856 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1857 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1859 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1862 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1863 if (!default_pinctrl) {
1864 printf("no pinctrl state for default mode\n");
1868 priv->default_pinctrl_state = default_pinctrl;
1870 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1871 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1872 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1873 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1874 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
1876 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1877 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1878 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
1884 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1885 #ifdef CONFIG_OMAP54XX
1886 __weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1892 static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1894 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1895 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1897 struct mmc_config *cfg = &plat->cfg;
1898 #ifdef CONFIG_OMAP54XX
1899 const struct mmc_platform_fixups *fixups;
1901 const void *fdt = gd->fdt_blob;
1902 int node = dev_of_offset(dev);
1905 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1906 sizeof(struct hsmmc *),
1909 ret = mmc_of_parse(dev, cfg);
1914 cfg->f_max = 52000000;
1915 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
1916 cfg->f_min = 400000;
1917 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1918 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1919 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1920 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1921 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1922 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
1924 plat->controller_flags |= of_data->controller_flags;
1926 #ifdef CONFIG_OMAP54XX
1927 fixups = platform_fixups_mmc(devfdt_get_addr(dev));
1929 plat->hw_rev = fixups->hw_rev;
1930 cfg->host_caps &= ~fixups->unsupported_caps;
1931 cfg->f_max = fixups->max_freq;
1941 static int omap_hsmmc_bind(struct udevice *dev)
1943 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1944 plat->mmc = calloc(1, sizeof(struct mmc));
1945 return mmc_bind(dev, plat->mmc, &plat->cfg);
1948 static int omap_hsmmc_probe(struct udevice *dev)
1950 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1951 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1952 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1953 struct mmc_config *cfg = &plat->cfg;
1955 #ifdef CONFIG_IODELAY_RECALIBRATION
1959 cfg->name = "OMAP SD/MMC";
1960 priv->base_addr = plat->base_addr;
1961 priv->controller_flags = plat->controller_flags;
1962 priv->hw_rev = plat->hw_rev;
1967 mmc = mmc_create(cfg, priv);
1971 #if CONFIG_IS_ENABLED(DM_REGULATOR)
1972 device_get_supply_regulator(dev, "pbias-supply",
1973 &priv->pbias_supply);
1975 #if defined(OMAP_HSMMC_USE_GPIO)
1976 #if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
1977 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1978 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1985 #ifdef CONFIG_IODELAY_RECALIBRATION
1986 ret = omap_hsmmc_get_pinctrl_state(mmc);
1988 * disable high speed modes for the platforms that require IO delay
1989 * and for which we don't have this information
1992 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1993 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
1994 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
1999 return omap_hsmmc_init_setup(mmc);
2002 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
2004 static const struct omap_mmc_of_data dra7_mmc_of_data = {
2005 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
2008 static const struct udevice_id omap_hsmmc_ids[] = {
2009 { .compatible = "ti,omap3-hsmmc" },
2010 { .compatible = "ti,omap4-hsmmc" },
2011 { .compatible = "ti,am33xx-hsmmc" },
2012 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
2017 U_BOOT_DRIVER(omap_hsmmc) = {
2018 .name = "omap_hsmmc",
2020 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
2021 .of_match = omap_hsmmc_ids,
2022 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
2023 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
2026 .bind = omap_hsmmc_bind,
2028 .ops = &omap_hsmmc_ops,
2029 .probe = omap_hsmmc_probe,
2030 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
2031 #if !CONFIG_IS_ENABLED(OF_CONTROL)
2032 .flags = DM_FLAG_PRE_RELOC,