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 #if !defined(CONFIG_SOC_KEYSTONE)
39 #include <asm/arch/sys_proto.h>
41 #ifdef CONFIG_MMC_OMAP36XX_PINS
42 #include <asm/arch/mux.h>
46 DECLARE_GLOBAL_DATA_PTR
;
48 /* simplify defines to OMAP_HSMMC_USE_GPIO */
49 #if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
50 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
51 #define OMAP_HSMMC_USE_GPIO
53 #undef OMAP_HSMMC_USE_GPIO
56 /* common definitions for all OMAPs */
57 #define SYSCTL_SRC (1 << 25)
58 #define SYSCTL_SRD (1 << 26)
60 struct omap_hsmmc_data
{
61 struct hsmmc
*base_addr
;
62 #if !CONFIG_IS_ENABLED(DM_MMC)
63 struct mmc_config cfg
;
67 #ifdef OMAP_HSMMC_USE_GPIO
68 #if CONFIG_IS_ENABLED(DM_MMC)
69 struct gpio_desc cd_gpio
; /* Change Detect GPIO */
70 struct gpio_desc wp_gpio
; /* Write Protect GPIO */
77 #if CONFIG_IS_ENABLED(DM_MMC)
82 #ifndef CONFIG_OMAP34XX
83 struct omap_hsmmc_adma_desc
*adma_desc_table
;
88 #ifndef CONFIG_OMAP34XX
89 struct omap_hsmmc_adma_desc
{
96 #define ADMA_MAX_LEN 63488
98 /* Decriptor table defines */
99 #define ADMA_DESC_ATTR_VALID BIT(0)
100 #define ADMA_DESC_ATTR_END BIT(1)
101 #define ADMA_DESC_ATTR_INT BIT(2)
102 #define ADMA_DESC_ATTR_ACT1 BIT(4)
103 #define ADMA_DESC_ATTR_ACT2 BIT(5)
105 #define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
106 #define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
109 /* If we fail after 1 second wait, something is really bad */
110 #define MAX_RETRY_MS 1000
112 /* DMA transfers can take a long time if a lot a data is transferred.
113 * The timeout must take in account the amount of data. Let's assume
114 * that the time will never exceed 333 ms per MB (in other word we assume
115 * that the bandwidth is always above 3MB/s).
117 #define DMA_TIMEOUT_PER_MB 333
118 #define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
119 #define OMAP_HSMMC_NO_1_8_V BIT(1)
120 #define OMAP_HSMMC_USE_ADMA BIT(2)
122 static int mmc_read_data(struct hsmmc
*mmc_base
, char *buf
, unsigned int size
);
123 static int mmc_write_data(struct hsmmc
*mmc_base
, const char *buf
,
125 static void omap_hsmmc_start_clock(struct hsmmc
*mmc_base
);
126 static void omap_hsmmc_stop_clock(struct hsmmc
*mmc_base
);
127 static void mmc_reset_controller_fsm(struct hsmmc
*mmc_base
, u32 bit
);
129 static inline struct omap_hsmmc_data
*omap_hsmmc_get_data(struct mmc
*mmc
)
131 #if CONFIG_IS_ENABLED(DM_MMC)
132 return dev_get_priv(mmc
->dev
);
134 return (struct omap_hsmmc_data
*)mmc
->priv
;
137 static inline struct mmc_config
*omap_hsmmc_get_cfg(struct mmc
*mmc
)
139 #if CONFIG_IS_ENABLED(DM_MMC)
140 struct omap_hsmmc_plat
*plat
= dev_get_platdata(mmc
->dev
);
143 return &((struct omap_hsmmc_data
*)mmc
->priv
)->cfg
;
147 #if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
148 static int omap_mmc_setup_gpio_in(int gpio
, const char *label
)
152 #ifndef CONFIG_DM_GPIO
153 if (!gpio_is_valid(gpio
))
156 ret
= gpio_request(gpio
, label
);
160 ret
= gpio_direction_input(gpio
);
168 static unsigned char mmc_board_init(struct mmc
*mmc
)
170 #if defined(CONFIG_OMAP34XX)
171 struct mmc_config
*cfg
= omap_hsmmc_get_cfg(mmc
);
172 t2_t
*t2_base
= (t2_t
*)T2_BASE
;
173 struct prcm
*prcm_base
= (struct prcm
*)PRCM_BASE
;
175 #ifdef CONFIG_MMC_OMAP36XX_PINS
176 u32 wkup_ctrl
= readl(OMAP34XX_CTRL_WKUP_CTRL
);
179 pbias_lite
= readl(&t2_base
->pbias_lite
);
180 pbias_lite
&= ~(PBIASLITEPWRDNZ1
| PBIASLITEPWRDNZ0
);
181 #ifdef CONFIG_TARGET_OMAP3_CAIRO
182 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
183 pbias_lite
&= ~PBIASLITEVMODE0
;
185 #ifdef CONFIG_MMC_OMAP36XX_PINS
186 if (get_cpu_family() == CPU_OMAP36XX
) {
187 /* Disable extended drain IO before changing PBIAS */
188 wkup_ctrl
&= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ
;
189 writel(wkup_ctrl
, OMAP34XX_CTRL_WKUP_CTRL
);
192 writel(pbias_lite
, &t2_base
->pbias_lite
);
194 writel(pbias_lite
| PBIASLITEPWRDNZ1
|
195 PBIASSPEEDCTRL0
| PBIASLITEPWRDNZ0
,
196 &t2_base
->pbias_lite
);
198 #ifdef CONFIG_MMC_OMAP36XX_PINS
199 if (get_cpu_family() == CPU_OMAP36XX
)
200 /* Enable extended drain IO after changing PBIAS */
202 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ
,
203 OMAP34XX_CTRL_WKUP_CTRL
);
205 writel(readl(&t2_base
->devconf0
) | MMCSDIO1ADPCLKISEL
,
208 writel(readl(&t2_base
->devconf1
) | MMCSDIO2ADPCLKISEL
,
211 /* Change from default of 52MHz to 26MHz if necessary */
212 if (!(cfg
->host_caps
& MMC_MODE_HS_52MHz
))
213 writel(readl(&t2_base
->ctl_prog_io1
) & ~CTLPROGIO1SPEEDCTRL
,
214 &t2_base
->ctl_prog_io1
);
216 writel(readl(&prcm_base
->fclken1_core
) |
217 EN_MMC1
| EN_MMC2
| EN_MMC3
,
218 &prcm_base
->fclken1_core
);
220 writel(readl(&prcm_base
->iclken1_core
) |
221 EN_MMC1
| EN_MMC2
| EN_MMC3
,
222 &prcm_base
->iclken1_core
);
225 #if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
226 /* PBIAS config needed for MMC1 only */
227 if (mmc_get_blk_desc(mmc
)->devnum
== 0)
228 vmmc_pbias_config(LDO_VOLT_3V0
);
234 void mmc_init_stream(struct hsmmc
*mmc_base
)
238 writel(readl(&mmc_base
->con
) | INIT_INITSTREAM
, &mmc_base
->con
);
240 writel(MMC_CMD0
, &mmc_base
->cmd
);
241 start
= get_timer(0);
242 while (!(readl(&mmc_base
->stat
) & CC_MASK
)) {
243 if (get_timer(0) - start
> MAX_RETRY_MS
) {
244 printf("%s: timedout waiting for cc!\n", __func__
);
248 writel(CC_MASK
, &mmc_base
->stat
)
250 writel(MMC_CMD0
, &mmc_base
->cmd
)
252 start
= get_timer(0);
253 while (!(readl(&mmc_base
->stat
) & CC_MASK
)) {
254 if (get_timer(0) - start
> MAX_RETRY_MS
) {
255 printf("%s: timedout waiting for cc2!\n", __func__
);
259 writel(readl(&mmc_base
->con
) & ~INIT_INITSTREAM
, &mmc_base
->con
);
262 #if CONFIG_IS_ENABLED(DM_MMC)
263 static void omap_hsmmc_set_timing(struct mmc
*mmc
)
266 struct hsmmc
*mmc_base
;
267 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
269 mmc_base
= priv
->base_addr
;
271 val
= readl(&mmc_base
->ac12
);
272 val
&= ~AC12_UHSMC_MASK
;
273 priv
->mode
= mmc
->selected_mode
;
275 if (mmc_is_mode_ddr(priv
->mode
))
276 writel(readl(&mmc_base
->con
) | DDR
, &mmc_base
->con
);
278 writel(readl(&mmc_base
->con
) & ~DDR
, &mmc_base
->con
);
280 switch (priv
->mode
) {
283 val
|= AC12_UHSMC_SDR104
;
286 val
|= AC12_UHSMC_SDR50
;
290 val
|= AC12_UHSMC_DDR50
;
295 val
|= AC12_UHSMC_SDR25
;
301 val
|= AC12_UHSMC_SDR12
;
304 val
|= AC12_UHSMC_RES
;
307 writel(val
, &mmc_base
->ac12
);
310 static void omap_hsmmc_conf_bus_power(struct mmc
*mmc
)
312 struct hsmmc
*mmc_base
;
313 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
316 mmc_base
= priv
->base_addr
;
318 val
= readl(&mmc_base
->hctl
) & ~SDVS_MASK
;
332 writel(val
, &mmc_base
->hctl
);
335 static void omap_hsmmc_set_capabilities(struct mmc
*mmc
)
337 struct hsmmc
*mmc_base
;
338 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
341 mmc_base
= priv
->base_addr
;
342 val
= readl(&mmc_base
->capa
);
344 if (priv
->controller_flags
& OMAP_HSMMC_SUPPORTS_DUAL_VOLT
) {
345 val
|= (VS30_3V0SUP
| VS18_1V8SUP
);
347 } else if (priv
->controller_flags
& OMAP_HSMMC_NO_1_8_V
) {
357 writel(val
, &mmc_base
->capa
);
360 #ifdef MMC_SUPPORTS_TUNING
361 static void omap_hsmmc_disable_tuning(struct mmc
*mmc
)
363 struct hsmmc
*mmc_base
;
364 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
367 mmc_base
= priv
->base_addr
;
368 val
= readl(&mmc_base
->ac12
);
369 val
&= ~(AC12_SCLK_SEL
);
370 writel(val
, &mmc_base
->ac12
);
372 val
= readl(&mmc_base
->dll
);
373 val
&= ~(DLL_FORCE_VALUE
| DLL_SWT
);
374 writel(val
, &mmc_base
->dll
);
377 static void omap_hsmmc_set_dll(struct mmc
*mmc
, int count
)
380 struct hsmmc
*mmc_base
;
381 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
384 mmc_base
= priv
->base_addr
;
385 val
= readl(&mmc_base
->dll
);
386 val
|= DLL_FORCE_VALUE
;
387 val
&= ~(DLL_FORCE_SR_C_MASK
<< DLL_FORCE_SR_C_SHIFT
);
388 val
|= (count
<< DLL_FORCE_SR_C_SHIFT
);
389 writel(val
, &mmc_base
->dll
);
392 writel(val
, &mmc_base
->dll
);
393 for (i
= 0; i
< 1000; i
++) {
394 if (readl(&mmc_base
->dll
) & DLL_CALIB
)
398 writel(val
, &mmc_base
->dll
);
401 static int omap_hsmmc_execute_tuning(struct udevice
*dev
, uint opcode
)
403 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
404 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
405 struct mmc
*mmc
= upriv
->mmc
;
406 struct hsmmc
*mmc_base
;
408 u8 cur_match
, prev_match
= 0;
411 u32 start_window
= 0, max_window
= 0;
412 u32 length
= 0, max_len
= 0;
414 mmc_base
= priv
->base_addr
;
415 val
= readl(&mmc_base
->capa2
);
417 /* clock tuning is not needed for upto 52MHz */
418 if (!((mmc
->selected_mode
== MMC_HS_200
) ||
419 (mmc
->selected_mode
== UHS_SDR104
) ||
420 ((mmc
->selected_mode
== UHS_SDR50
) && (val
& CAPA2_TSDR50
))))
423 val
= readl(&mmc_base
->dll
);
425 writel(val
, &mmc_base
->dll
);
426 while (phase_delay
<= MAX_PHASE_DELAY
) {
427 omap_hsmmc_set_dll(mmc
, phase_delay
);
429 cur_match
= !mmc_send_tuning(mmc
, opcode
, NULL
);
435 start_window
= phase_delay
;
440 if (length
> max_len
) {
441 max_window
= start_window
;
445 prev_match
= cur_match
;
454 val
= readl(&mmc_base
->ac12
);
455 if (!(val
& AC12_SCLK_SEL
)) {
460 phase_delay
= max_window
+ 4 * ((3 * max_len
) >> 2);
461 omap_hsmmc_set_dll(mmc
, phase_delay
);
463 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
464 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRC
);
470 omap_hsmmc_disable_tuning(mmc
);
471 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
472 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRC
);
479 static void mmc_enable_irq(struct mmc
*mmc
, struct mmc_cmd
*cmd
)
481 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
482 struct hsmmc
*mmc_base
= priv
->base_addr
;
483 u32 irq_mask
= INT_EN_MASK
;
486 * TODO: Errata i802 indicates only DCRC interrupts can occur during
487 * tuning procedure and DCRC should be disabled. But see occurences
488 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
489 * interrupts occur along with BRR, so the data is actually in the
490 * buffer. It has to be debugged why these interrutps occur
492 if (cmd
&& mmc_is_tuning_cmd(cmd
->cmdidx
))
493 irq_mask
&= ~(IE_DEB
| IE_DCRC
| IE_CIE
| IE_CEB
| IE_CCRC
);
495 writel(irq_mask
, &mmc_base
->ie
);
498 static int omap_hsmmc_init_setup(struct mmc
*mmc
)
500 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
501 struct hsmmc
*mmc_base
;
502 unsigned int reg_val
;
506 mmc_base
= priv
->base_addr
;
509 writel(readl(&mmc_base
->sysconfig
) | MMC_SOFTRESET
,
510 &mmc_base
->sysconfig
);
511 start
= get_timer(0);
512 while ((readl(&mmc_base
->sysstatus
) & RESETDONE
) == 0) {
513 if (get_timer(0) - start
> MAX_RETRY_MS
) {
514 printf("%s: timedout waiting for cc2!\n", __func__
);
518 writel(readl(&mmc_base
->sysctl
) | SOFTRESETALL
, &mmc_base
->sysctl
);
519 start
= get_timer(0);
520 while ((readl(&mmc_base
->sysctl
) & SOFTRESETALL
) != 0x0) {
521 if (get_timer(0) - start
> MAX_RETRY_MS
) {
522 printf("%s: timedout waiting for softresetall!\n",
527 #ifndef CONFIG_OMAP34XX
528 reg_val
= readl(&mmc_base
->hl_hwinfo
);
529 if (reg_val
& MADMA_EN
)
530 priv
->controller_flags
|= OMAP_HSMMC_USE_ADMA
;
533 #if CONFIG_IS_ENABLED(DM_MMC)
534 omap_hsmmc_set_capabilities(mmc
);
535 omap_hsmmc_conf_bus_power(mmc
);
537 writel(DTW_1_BITMODE
| SDBP_PWROFF
| SDVS_3V0
, &mmc_base
->hctl
);
538 writel(readl(&mmc_base
->capa
) | VS30_3V0SUP
| VS18_1V8SUP
,
542 reg_val
= readl(&mmc_base
->con
) & RESERVED_MASK
;
544 writel(CTPL_MMC_SD
| reg_val
| WPP_ACTIVEHIGH
| CDP_ACTIVEHIGH
|
545 MIT_CTO
| DW8_1_4BITMODE
| MODE_FUNC
| STR_BLOCK
|
546 HR_NOHOSTRESP
| INIT_NOINIT
| NOOPENDRAIN
, &mmc_base
->con
);
549 mmc_reg_out(&mmc_base
->sysctl
, (ICE_MASK
| DTO_MASK
| CEN_MASK
),
550 (ICE_STOP
| DTO_15THDTO
));
551 mmc_reg_out(&mmc_base
->sysctl
, ICE_MASK
| CLKD_MASK
,
552 (dsor
<< CLKD_OFFSET
) | ICE_OSCILLATE
);
553 start
= get_timer(0);
554 while ((readl(&mmc_base
->sysctl
) & ICS_MASK
) == ICS_NOTREADY
) {
555 if (get_timer(0) - start
> MAX_RETRY_MS
) {
556 printf("%s: timedout waiting for ics!\n", __func__
);
560 writel(readl(&mmc_base
->sysctl
) | CEN_ENABLE
, &mmc_base
->sysctl
);
562 writel(readl(&mmc_base
->hctl
) | SDBP_PWRON
, &mmc_base
->hctl
);
564 mmc_enable_irq(mmc
, NULL
);
565 mmc_init_stream(mmc_base
);
571 * MMC controller internal finite state machine reset
573 * Used to reset command or data internal state machines, using respectively
574 * SRC or SRD bit of SYSCTL register
576 static void mmc_reset_controller_fsm(struct hsmmc
*mmc_base
, u32 bit
)
580 mmc_reg_out(&mmc_base
->sysctl
, bit
, bit
);
583 * CMD(DAT) lines reset procedures are slightly different
584 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
585 * According to OMAP3 TRM:
586 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
588 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
589 * procedure steps must be as follows:
590 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
591 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
592 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
593 * 3. Wait until the SRC (SRD) bit returns to 0x0
594 * (reset procedure is completed).
596 #if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
597 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
598 if (!(readl(&mmc_base
->sysctl
) & bit
)) {
599 start
= get_timer(0);
600 while (!(readl(&mmc_base
->sysctl
) & bit
)) {
601 if (get_timer(0) - start
> MAX_RETRY_MS
)
606 start
= get_timer(0);
607 while ((readl(&mmc_base
->sysctl
) & bit
) != 0) {
608 if (get_timer(0) - start
> MAX_RETRY_MS
) {
609 printf("%s: timedout waiting for sysctl %x to clear\n",
616 #ifndef CONFIG_OMAP34XX
617 static void omap_hsmmc_adma_desc(struct mmc
*mmc
, char *buf
, u16 len
, bool end
)
619 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
620 struct omap_hsmmc_adma_desc
*desc
;
623 desc
= &priv
->adma_desc_table
[priv
->desc_slot
];
625 attr
= ADMA_DESC_ATTR_VALID
| ADMA_DESC_TRANSFER_DATA
;
629 attr
|= ADMA_DESC_ATTR_END
;
632 desc
->addr
= (u32
)buf
;
637 static void omap_hsmmc_prepare_adma_table(struct mmc
*mmc
,
638 struct mmc_data
*data
)
640 uint total_len
= data
->blocksize
* data
->blocks
;
641 uint desc_count
= DIV_ROUND_UP(total_len
, ADMA_MAX_LEN
);
642 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
647 priv
->adma_desc_table
= (struct omap_hsmmc_adma_desc
*)
648 memalign(ARCH_DMA_MINALIGN
, desc_count
*
649 sizeof(struct omap_hsmmc_adma_desc
));
651 if (data
->flags
& MMC_DATA_READ
)
654 buf
= (char *)data
->src
;
657 omap_hsmmc_adma_desc(mmc
, buf
, ADMA_MAX_LEN
, false);
659 total_len
-= ADMA_MAX_LEN
;
662 omap_hsmmc_adma_desc(mmc
, buf
, total_len
, true);
664 flush_dcache_range((long)priv
->adma_desc_table
,
665 (long)priv
->adma_desc_table
+
667 sizeof(struct omap_hsmmc_adma_desc
),
671 static void omap_hsmmc_prepare_data(struct mmc
*mmc
, struct mmc_data
*data
)
673 struct hsmmc
*mmc_base
;
674 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
678 mmc_base
= priv
->base_addr
;
679 omap_hsmmc_prepare_adma_table(mmc
, data
);
681 if (data
->flags
& MMC_DATA_READ
)
684 buf
= (char *)data
->src
;
686 val
= readl(&mmc_base
->hctl
);
688 writel(val
, &mmc_base
->hctl
);
690 val
= readl(&mmc_base
->con
);
692 writel(val
, &mmc_base
->con
);
694 writel((u32
)priv
->adma_desc_table
, &mmc_base
->admasal
);
696 flush_dcache_range((u32
)buf
,
698 ROUND(data
->blocksize
* data
->blocks
,
702 static void omap_hsmmc_dma_cleanup(struct mmc
*mmc
)
704 struct hsmmc
*mmc_base
;
705 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
708 mmc_base
= priv
->base_addr
;
710 val
= readl(&mmc_base
->con
);
712 writel(val
, &mmc_base
->con
);
714 val
= readl(&mmc_base
->hctl
);
716 writel(val
, &mmc_base
->hctl
);
718 kfree(priv
->adma_desc_table
);
721 #define omap_hsmmc_adma_desc
722 #define omap_hsmmc_prepare_adma_table
723 #define omap_hsmmc_prepare_data
724 #define omap_hsmmc_dma_cleanup
727 #if !CONFIG_IS_ENABLED(DM_MMC)
728 static int omap_hsmmc_send_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
729 struct mmc_data
*data
)
731 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
733 static int omap_hsmmc_send_cmd(struct udevice
*dev
, struct mmc_cmd
*cmd
,
734 struct mmc_data
*data
)
736 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
737 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
738 struct mmc
*mmc
= upriv
->mmc
;
740 struct hsmmc
*mmc_base
;
741 unsigned int flags
, mmc_stat
;
744 mmc_base
= priv
->base_addr
;
746 if (cmd
->cmdidx
== MMC_CMD_STOP_TRANSMISSION
)
749 start
= get_timer(0);
750 while ((readl(&mmc_base
->pstate
) & (DATI_MASK
| CMDI_MASK
)) != 0) {
751 if (get_timer(0) - start
> MAX_RETRY_MS
) {
752 printf("%s: timedout waiting on cmd inhibit to clear\n",
757 writel(0xFFFFFFFF, &mmc_base
->stat
);
758 start
= get_timer(0);
759 while (readl(&mmc_base
->stat
)) {
760 if (get_timer(0) - start
> MAX_RETRY_MS
) {
761 printf("%s: timedout waiting for STAT (%x) to clear\n",
762 __func__
, readl(&mmc_base
->stat
));
768 * CMDIDX[13:8] : Command index
769 * DATAPRNT[5] : Data Present Select
770 * ENCMDIDX[4] : Command Index Check Enable
771 * ENCMDCRC[3] : Command CRC Check Enable
776 * 11 = Length 48 Check busy after response
778 /* Delay added before checking the status of frq change
779 * retry not supported by mmc.c(core file)
781 if (cmd
->cmdidx
== SD_CMD_APP_SEND_SCR
)
782 udelay(50000); /* wait 50 ms */
784 if (!(cmd
->resp_type
& MMC_RSP_PRESENT
))
786 else if (cmd
->resp_type
& MMC_RSP_136
)
787 flags
= RSP_TYPE_LGHT136
| CICE_NOCHECK
;
788 else if (cmd
->resp_type
& MMC_RSP_BUSY
)
789 flags
= RSP_TYPE_LGHT48B
;
791 flags
= RSP_TYPE_LGHT48
;
793 /* enable default flags */
794 flags
= flags
| (CMD_TYPE_NORMAL
| CICE_NOCHECK
| CCCE_NOCHECK
|
796 flags
&= ~(ACEN_ENABLE
| BCE_ENABLE
| DE_ENABLE
);
798 if (cmd
->resp_type
& MMC_RSP_CRC
)
800 if (cmd
->resp_type
& MMC_RSP_OPCODE
)
804 if ((cmd
->cmdidx
== MMC_CMD_READ_MULTIPLE_BLOCK
) ||
805 (cmd
->cmdidx
== MMC_CMD_WRITE_MULTIPLE_BLOCK
)) {
806 flags
|= (MSBS_MULTIBLK
| BCE_ENABLE
| ACEN_ENABLE
);
807 data
->blocksize
= 512;
808 writel(data
->blocksize
| (data
->blocks
<< 16),
811 writel(data
->blocksize
| NBLK_STPCNT
, &mmc_base
->blk
);
813 if (data
->flags
& MMC_DATA_READ
)
814 flags
|= (DP_DATA
| DDIR_READ
);
816 flags
|= (DP_DATA
| DDIR_WRITE
);
818 #ifndef CONFIG_OMAP34XX
819 if ((priv
->controller_flags
& OMAP_HSMMC_USE_ADMA
) &&
820 !mmc_is_tuning_cmd(cmd
->cmdidx
)) {
821 omap_hsmmc_prepare_data(mmc
, data
);
827 mmc_enable_irq(mmc
, cmd
);
829 writel(cmd
->cmdarg
, &mmc_base
->arg
);
830 udelay(20); /* To fix "No status update" error on eMMC */
831 writel((cmd
->cmdidx
<< 24) | flags
, &mmc_base
->cmd
);
833 start
= get_timer(0);
835 mmc_stat
= readl(&mmc_base
->stat
);
836 if (get_timer(start
) > MAX_RETRY_MS
) {
837 printf("%s : timeout: No status update\n", __func__
);
842 if ((mmc_stat
& IE_CTO
) != 0) {
843 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRC
);
845 } else if ((mmc_stat
& ERRI_MASK
) != 0)
848 if (mmc_stat
& CC_MASK
) {
849 writel(CC_MASK
, &mmc_base
->stat
);
850 if (cmd
->resp_type
& MMC_RSP_PRESENT
) {
851 if (cmd
->resp_type
& MMC_RSP_136
) {
852 /* response type 2 */
853 cmd
->response
[3] = readl(&mmc_base
->rsp10
);
854 cmd
->response
[2] = readl(&mmc_base
->rsp32
);
855 cmd
->response
[1] = readl(&mmc_base
->rsp54
);
856 cmd
->response
[0] = readl(&mmc_base
->rsp76
);
858 /* response types 1, 1b, 3, 4, 5, 6 */
859 cmd
->response
[0] = readl(&mmc_base
->rsp10
);
863 #ifndef CONFIG_OMAP34XX
864 if ((priv
->controller_flags
& OMAP_HSMMC_USE_ADMA
) && data
&&
865 !mmc_is_tuning_cmd(cmd
->cmdidx
)) {
868 if (mmc_stat
& IE_ADMAE
) {
869 omap_hsmmc_dma_cleanup(mmc
);
873 sz_mb
= DIV_ROUND_UP(data
->blocksize
* data
->blocks
, 1 << 20);
874 timeout
= sz_mb
* DMA_TIMEOUT_PER_MB
;
875 if (timeout
< MAX_RETRY_MS
)
876 timeout
= MAX_RETRY_MS
;
878 start
= get_timer(0);
880 mmc_stat
= readl(&mmc_base
->stat
);
881 if (mmc_stat
& TC_MASK
) {
882 writel(readl(&mmc_base
->stat
) | TC_MASK
,
886 if (get_timer(start
) > timeout
) {
887 printf("%s : DMA timeout: No status update\n",
893 omap_hsmmc_dma_cleanup(mmc
);
898 if (data
&& (data
->flags
& MMC_DATA_READ
)) {
899 mmc_read_data(mmc_base
, data
->dest
,
900 data
->blocksize
* data
->blocks
);
901 } else if (data
&& (data
->flags
& MMC_DATA_WRITE
)) {
902 mmc_write_data(mmc_base
, data
->src
,
903 data
->blocksize
* data
->blocks
);
908 static int mmc_read_data(struct hsmmc
*mmc_base
, char *buf
, unsigned int size
)
910 unsigned int *output_buf
= (unsigned int *)buf
;
911 unsigned int mmc_stat
;
917 count
= (size
> MMCSD_SECTOR_SIZE
) ? MMCSD_SECTOR_SIZE
: size
;
921 ulong start
= get_timer(0);
923 mmc_stat
= readl(&mmc_base
->stat
);
924 if (get_timer(0) - start
> MAX_RETRY_MS
) {
925 printf("%s: timedout waiting for status!\n",
929 } while (mmc_stat
== 0);
931 if ((mmc_stat
& (IE_DTO
| IE_DCRC
| IE_DEB
)) != 0)
932 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
934 if ((mmc_stat
& ERRI_MASK
) != 0)
937 if (mmc_stat
& BRR_MASK
) {
940 writel(readl(&mmc_base
->stat
) | BRR_MASK
,
942 for (k
= 0; k
< count
; k
++) {
943 *output_buf
= readl(&mmc_base
->data
);
949 if (mmc_stat
& BWR_MASK
)
950 writel(readl(&mmc_base
->stat
) | BWR_MASK
,
953 if (mmc_stat
& TC_MASK
) {
954 writel(readl(&mmc_base
->stat
) | TC_MASK
,
962 static int mmc_write_data(struct hsmmc
*mmc_base
, const char *buf
,
965 unsigned int *input_buf
= (unsigned int *)buf
;
966 unsigned int mmc_stat
;
972 count
= (size
> MMCSD_SECTOR_SIZE
) ? MMCSD_SECTOR_SIZE
: size
;
976 ulong start
= get_timer(0);
978 mmc_stat
= readl(&mmc_base
->stat
);
979 if (get_timer(0) - start
> MAX_RETRY_MS
) {
980 printf("%s: timedout waiting for status!\n",
984 } while (mmc_stat
== 0);
986 if ((mmc_stat
& (IE_DTO
| IE_DCRC
| IE_DEB
)) != 0)
987 mmc_reset_controller_fsm(mmc_base
, SYSCTL_SRD
);
989 if ((mmc_stat
& ERRI_MASK
) != 0)
992 if (mmc_stat
& BWR_MASK
) {
995 writel(readl(&mmc_base
->stat
) | BWR_MASK
,
997 for (k
= 0; k
< count
; k
++) {
998 writel(*input_buf
, &mmc_base
->data
);
1004 if (mmc_stat
& BRR_MASK
)
1005 writel(readl(&mmc_base
->stat
) | BRR_MASK
,
1008 if (mmc_stat
& TC_MASK
) {
1009 writel(readl(&mmc_base
->stat
) | TC_MASK
,
1017 static void omap_hsmmc_stop_clock(struct hsmmc
*mmc_base
)
1019 writel(readl(&mmc_base
->sysctl
) & ~CEN_ENABLE
, &mmc_base
->sysctl
);
1022 static void omap_hsmmc_start_clock(struct hsmmc
*mmc_base
)
1024 writel(readl(&mmc_base
->sysctl
) | CEN_ENABLE
, &mmc_base
->sysctl
);
1027 static void omap_hsmmc_set_clock(struct mmc
*mmc
)
1029 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
1030 struct hsmmc
*mmc_base
;
1031 unsigned int dsor
= 0;
1034 mmc_base
= priv
->base_addr
;
1035 omap_hsmmc_stop_clock(mmc_base
);
1037 /* TODO: Is setting DTO required here? */
1038 mmc_reg_out(&mmc_base
->sysctl
, (ICE_MASK
| DTO_MASK
),
1039 (ICE_STOP
| DTO_15THDTO
));
1041 if (mmc
->clock
!= 0) {
1042 dsor
= DIV_ROUND_UP(MMC_CLOCK_REFERENCE
* 1000000, mmc
->clock
);
1043 if (dsor
> CLKD_MAX
)
1049 mmc_reg_out(&mmc_base
->sysctl
, ICE_MASK
| CLKD_MASK
,
1050 (dsor
<< CLKD_OFFSET
) | ICE_OSCILLATE
);
1052 start
= get_timer(0);
1053 while ((readl(&mmc_base
->sysctl
) & ICS_MASK
) == ICS_NOTREADY
) {
1054 if (get_timer(0) - start
> MAX_RETRY_MS
) {
1055 printf("%s: timedout waiting for ics!\n", __func__
);
1060 priv
->clock
= mmc
->clock
;
1061 omap_hsmmc_start_clock(mmc_base
);
1064 static void omap_hsmmc_set_bus_width(struct mmc
*mmc
)
1066 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
1067 struct hsmmc
*mmc_base
;
1069 mmc_base
= priv
->base_addr
;
1070 /* configue bus width */
1071 switch (mmc
->bus_width
) {
1073 writel(readl(&mmc_base
->con
) | DTW_8_BITMODE
,
1078 writel(readl(&mmc_base
->con
) & ~DTW_8_BITMODE
,
1080 writel(readl(&mmc_base
->hctl
) | DTW_4_BITMODE
,
1086 writel(readl(&mmc_base
->con
) & ~DTW_8_BITMODE
,
1088 writel(readl(&mmc_base
->hctl
) & ~DTW_4_BITMODE
,
1093 priv
->bus_width
= mmc
->bus_width
;
1096 #if !CONFIG_IS_ENABLED(DM_MMC)
1097 static int omap_hsmmc_set_ios(struct mmc
*mmc
)
1099 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
1101 static int omap_hsmmc_set_ios(struct udevice
*dev
)
1103 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
1104 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
1105 struct mmc
*mmc
= upriv
->mmc
;
1108 if (priv
->bus_width
!= mmc
->bus_width
)
1109 omap_hsmmc_set_bus_width(mmc
);
1111 if (priv
->clock
!= mmc
->clock
)
1112 omap_hsmmc_set_clock(mmc
);
1114 #if CONFIG_IS_ENABLED(DM_MMC)
1115 if (priv
->mode
!= mmc
->selected_mode
)
1116 omap_hsmmc_set_timing(mmc
);
1121 #ifdef OMAP_HSMMC_USE_GPIO
1122 #if CONFIG_IS_ENABLED(DM_MMC)
1123 static int omap_hsmmc_getcd(struct udevice
*dev
)
1125 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
1128 value
= dm_gpio_get_value(&priv
->cd_gpio
);
1129 /* if no CD return as 1 */
1133 if (priv
->cd_inverted
)
1138 static int omap_hsmmc_getwp(struct udevice
*dev
)
1140 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
1143 value
= dm_gpio_get_value(&priv
->wp_gpio
);
1144 /* if no WP return as 0 */
1150 static int omap_hsmmc_getcd(struct mmc
*mmc
)
1152 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
1155 /* if no CD return as 1 */
1156 cd_gpio
= priv
->cd_gpio
;
1160 /* NOTE: assumes card detect signal is active-low */
1161 return !gpio_get_value(cd_gpio
);
1164 static int omap_hsmmc_getwp(struct mmc
*mmc
)
1166 struct omap_hsmmc_data
*priv
= omap_hsmmc_get_data(mmc
);
1169 /* if no WP return as 0 */
1170 wp_gpio
= priv
->wp_gpio
;
1174 /* NOTE: assumes write protect signal is active-high */
1175 return gpio_get_value(wp_gpio
);
1180 #if CONFIG_IS_ENABLED(DM_MMC)
1181 static const struct dm_mmc_ops omap_hsmmc_ops
= {
1182 .send_cmd
= omap_hsmmc_send_cmd
,
1183 .set_ios
= omap_hsmmc_set_ios
,
1184 #ifdef OMAP_HSMMC_USE_GPIO
1185 .get_cd
= omap_hsmmc_getcd
,
1186 .get_wp
= omap_hsmmc_getwp
,
1188 #ifdef MMC_SUPPORTS_TUNING
1189 .execute_tuning
= omap_hsmmc_execute_tuning
,
1193 static const struct mmc_ops omap_hsmmc_ops
= {
1194 .send_cmd
= omap_hsmmc_send_cmd
,
1195 .set_ios
= omap_hsmmc_set_ios
,
1196 .init
= omap_hsmmc_init_setup
,
1197 #ifdef OMAP_HSMMC_USE_GPIO
1198 .getcd
= omap_hsmmc_getcd
,
1199 .getwp
= omap_hsmmc_getwp
,
1204 #if !CONFIG_IS_ENABLED(DM_MMC)
1205 int omap_mmc_init(int dev_index
, uint host_caps_mask
, uint f_max
, int cd_gpio
,
1209 struct omap_hsmmc_data
*priv
;
1210 struct mmc_config
*cfg
;
1213 priv
= malloc(sizeof(*priv
));
1217 host_caps_val
= MMC_MODE_4BIT
| MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
1219 switch (dev_index
) {
1221 priv
->base_addr
= (struct hsmmc
*)OMAP_HSMMC1_BASE
;
1223 #ifdef OMAP_HSMMC2_BASE
1225 priv
->base_addr
= (struct hsmmc
*)OMAP_HSMMC2_BASE
;
1226 #if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
1227 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
1228 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1229 defined(CONFIG_HSMMC2_8BIT)
1230 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1231 host_caps_val
|= MMC_MODE_8BIT
;
1235 #ifdef OMAP_HSMMC3_BASE
1237 priv
->base_addr
= (struct hsmmc
*)OMAP_HSMMC3_BASE
;
1238 #if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
1239 /* Enable 8-bit interface for eMMC on DRA7XX */
1240 host_caps_val
|= MMC_MODE_8BIT
;
1245 priv
->base_addr
= (struct hsmmc
*)OMAP_HSMMC1_BASE
;
1248 #ifdef OMAP_HSMMC_USE_GPIO
1249 /* on error gpio values are set to -1, which is what we want */
1250 priv
->cd_gpio
= omap_mmc_setup_gpio_in(cd_gpio
, "mmc_cd");
1251 priv
->wp_gpio
= omap_mmc_setup_gpio_in(wp_gpio
, "mmc_wp");
1256 cfg
->name
= "OMAP SD/MMC";
1257 cfg
->ops
= &omap_hsmmc_ops
;
1259 cfg
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1260 cfg
->host_caps
= host_caps_val
& ~host_caps_mask
;
1262 cfg
->f_min
= 400000;
1267 if (cfg
->host_caps
& MMC_MODE_HS
) {
1268 if (cfg
->host_caps
& MMC_MODE_HS_52MHz
)
1269 cfg
->f_max
= 52000000;
1271 cfg
->f_max
= 26000000;
1273 cfg
->f_max
= 20000000;
1276 cfg
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1278 #if defined(CONFIG_OMAP34XX)
1280 * Silicon revs 2.1 and older do not support multiblock transfers.
1282 if ((get_cpu_family() == CPU_OMAP34XX
) && (get_cpu_rev() <= CPU_3XX_ES21
))
1285 mmc
= mmc_create(cfg
, priv
);
1292 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1293 static int omap_hsmmc_ofdata_to_platdata(struct udevice
*dev
)
1295 struct omap_hsmmc_plat
*plat
= dev_get_platdata(dev
);
1296 struct mmc_config
*cfg
= &plat
->cfg
;
1297 const void *fdt
= gd
->fdt_blob
;
1298 int node
= dev_of_offset(dev
);
1301 plat
->base_addr
= map_physmem(devfdt_get_addr(dev
),
1302 sizeof(struct hsmmc
*),
1305 cfg
->host_caps
= MMC_MODE_HS_52MHz
| MMC_MODE_HS
;
1306 val
= fdtdec_get_int(fdt
, node
, "bus-width", -1);
1308 printf("error: bus-width property missing\n");
1314 cfg
->host_caps
|= MMC_MODE_8BIT
;
1316 cfg
->host_caps
|= MMC_MODE_4BIT
;
1319 printf("error: invalid bus-width property\n");
1323 cfg
->f_min
= 400000;
1324 cfg
->f_max
= fdtdec_get_int(fdt
, node
, "max-frequency", 52000000);
1325 cfg
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
| MMC_VDD_165_195
;
1326 cfg
->b_max
= CONFIG_SYS_MMC_MAX_BLK_COUNT
;
1327 if (fdtdec_get_bool(fdt
, node
, "ti,dual-volt"))
1328 plat
->controller_flags
|= OMAP_HSMMC_SUPPORTS_DUAL_VOLT
;
1329 if (fdtdec_get_bool(fdt
, node
, "no-1-8-v"))
1330 plat
->controller_flags
|= OMAP_HSMMC_NO_1_8_V
;
1332 #ifdef OMAP_HSMMC_USE_GPIO
1333 plat
->cd_inverted
= fdtdec_get_bool(fdt
, node
, "cd-inverted");
1342 static int omap_hsmmc_bind(struct udevice
*dev
)
1344 struct omap_hsmmc_plat
*plat
= dev_get_platdata(dev
);
1346 return mmc_bind(dev
, &plat
->mmc
, &plat
->cfg
);
1349 static int omap_hsmmc_probe(struct udevice
*dev
)
1351 struct omap_hsmmc_plat
*plat
= dev_get_platdata(dev
);
1352 struct mmc_uclass_priv
*upriv
= dev_get_uclass_priv(dev
);
1353 struct omap_hsmmc_data
*priv
= dev_get_priv(dev
);
1354 struct mmc_config
*cfg
= &plat
->cfg
;
1357 cfg
->name
= "OMAP SD/MMC";
1358 priv
->base_addr
= plat
->base_addr
;
1359 #ifdef OMAP_HSMMC_USE_GPIO
1360 priv
->cd_inverted
= plat
->cd_inverted
;
1366 mmc
= mmc_create(cfg
, priv
);
1371 #if defined(OMAP_HSMMC_USE_GPIO) && CONFIG_IS_ENABLED(OF_CONTROL)
1372 gpio_request_by_name(dev
, "cd-gpios", 0, &priv
->cd_gpio
, GPIOD_IS_IN
);
1373 gpio_request_by_name(dev
, "wp-gpios", 0, &priv
->wp_gpio
, GPIOD_IS_IN
);
1379 return omap_hsmmc_init_setup(mmc
);
1382 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1383 static const struct udevice_id omap_hsmmc_ids
[] = {
1384 { .compatible
= "ti,omap3-hsmmc" },
1385 { .compatible
= "ti,omap4-hsmmc" },
1386 { .compatible
= "ti,am33xx-hsmmc" },
1391 U_BOOT_DRIVER(omap_hsmmc
) = {
1392 .name
= "omap_hsmmc",
1394 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1395 .of_match
= omap_hsmmc_ids
,
1396 .ofdata_to_platdata
= omap_hsmmc_ofdata_to_platdata
,
1397 .platdata_auto_alloc_size
= sizeof(struct omap_hsmmc_plat
),
1400 .bind
= omap_hsmmc_bind
,
1402 .ops
= &omap_hsmmc_ops
,
1403 .probe
= omap_hsmmc_probe
,
1404 .priv_auto_alloc_size
= sizeof(struct omap_hsmmc_data
),
1405 .flags
= DM_FLAG_PRE_RELOC
,