1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek SD/MMC Card Interface driver
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Weijie Gao <weijie.gao@mediatek.com>
17 #include <dm/pinctrl.h>
18 #include <linux/bitops.h>
20 #include <linux/iopoll.h>
23 #define MSDC_CFG_HS400_CK_MODE_EXT BIT(22)
24 #define MSDC_CFG_CKMOD_EXT_M 0x300000
25 #define MSDC_CFG_CKMOD_EXT_S 20
26 #define MSDC_CFG_CKDIV_EXT_M 0xfff00
27 #define MSDC_CFG_CKDIV_EXT_S 8
28 #define MSDC_CFG_HS400_CK_MODE BIT(18)
29 #define MSDC_CFG_CKMOD_M 0x30000
30 #define MSDC_CFG_CKMOD_S 16
31 #define MSDC_CFG_CKDIV_M 0xff00
32 #define MSDC_CFG_CKDIV_S 8
33 #define MSDC_CFG_CKSTB BIT(7)
34 #define MSDC_CFG_PIO BIT(3)
35 #define MSDC_CFG_RST BIT(2)
36 #define MSDC_CFG_CKPDN BIT(1)
37 #define MSDC_CFG_MODE BIT(0)
40 #define MSDC_IOCON_W_DSPL BIT(8)
41 #define MSDC_IOCON_DSPL BIT(2)
42 #define MSDC_IOCON_RSPL BIT(1)
45 #define MSDC_PS_DAT0 BIT(16)
46 #define MSDC_PS_CDDBCE_M 0xf000
47 #define MSDC_PS_CDDBCE_S 12
48 #define MSDC_PS_CDSTS BIT(1)
49 #define MSDC_PS_CDEN BIT(0)
51 /* #define MSDC_INT(EN) */
52 #define MSDC_INT_ACMDRDY BIT(3)
53 #define MSDC_INT_ACMDTMO BIT(4)
54 #define MSDC_INT_ACMDCRCERR BIT(5)
55 #define MSDC_INT_CMDRDY BIT(8)
56 #define MSDC_INT_CMDTMO BIT(9)
57 #define MSDC_INT_RSPCRCERR BIT(10)
58 #define MSDC_INT_XFER_COMPL BIT(12)
59 #define MSDC_INT_DATTMO BIT(14)
60 #define MSDC_INT_DATCRCERR BIT(15)
63 #define MSDC_FIFOCS_CLR BIT(31)
64 #define MSDC_FIFOCS_TXCNT_M 0xff0000
65 #define MSDC_FIFOCS_TXCNT_S 16
66 #define MSDC_FIFOCS_RXCNT_M 0xff
67 #define MSDC_FIFOCS_RXCNT_S 0
70 #define SDC_CFG_DTOC_M 0xff000000
71 #define SDC_CFG_DTOC_S 24
72 #define SDC_CFG_SDIOIDE BIT(20)
73 #define SDC_CFG_SDIO BIT(19)
74 #define SDC_CFG_BUSWIDTH_M 0x30000
75 #define SDC_CFG_BUSWIDTH_S 16
78 #define SDC_CMD_BLK_LEN_M 0xfff0000
79 #define SDC_CMD_BLK_LEN_S 16
80 #define SDC_CMD_STOP BIT(14)
81 #define SDC_CMD_WR BIT(13)
82 #define SDC_CMD_DTYPE_M 0x1800
83 #define SDC_CMD_DTYPE_S 11
84 #define SDC_CMD_RSPTYP_M 0x380
85 #define SDC_CMD_RSPTYP_S 7
86 #define SDC_CMD_CMD_M 0x3f
87 #define SDC_CMD_CMD_S 0
90 #define SDC_STS_CMDBUSY BIT(1)
91 #define SDC_STS_SDCBUSY BIT(0)
94 #define SDC_RX_ENHANCE_EN BIT(20)
97 #define MSDC_INT_DAT_LATCH_CK_SEL_M 0x380
98 #define MSDC_INT_DAT_LATCH_CK_SEL_S 7
101 #define MSDC_PB1_STOP_DLY_M 0xf00
102 #define MSDC_PB1_STOP_DLY_S 8
105 #define MSDC_PB2_CRCSTSENSEL_M 0xe0000000
106 #define MSDC_PB2_CRCSTSENSEL_S 29
107 #define MSDC_PB2_CFGCRCSTS BIT(28)
108 #define MSDC_PB2_RESPSTSENSEL_M 0x70000
109 #define MSDC_PB2_RESPSTSENSEL_S 16
110 #define MSDC_PB2_CFGRESP BIT(15)
111 #define MSDC_PB2_RESPWAIT_M 0x0c
112 #define MSDC_PB2_RESPWAIT_S 2
115 #define MSDC_PAD_TUNE_CMDRRDLY_M 0x7c00000
116 #define MSDC_PAD_TUNE_CMDRRDLY_S 22
117 #define MSDC_PAD_TUNE_CMD_SEL BIT(21)
118 #define MSDC_PAD_TUNE_CMDRDLY_M 0x1f0000
119 #define MSDC_PAD_TUNE_CMDRDLY_S 16
120 #define MSDC_PAD_TUNE_RXDLYSEL BIT(15)
121 #define MSDC_PAD_TUNE_RD_SEL BIT(13)
122 #define MSDC_PAD_TUNE_DATRRDLY_M 0x1f00
123 #define MSDC_PAD_TUNE_DATRRDLY_S 8
124 #define MSDC_PAD_TUNE_DATWRDLY_M 0x1f
125 #define MSDC_PAD_TUNE_DATWRDLY_S 0
128 #define EMMC50_CFG_CFCSTS_SEL BIT(4)
131 #define SDC_FIFO_CFG_WRVALIDSEL BIT(24)
132 #define SDC_FIFO_CFG_RDVALIDSEL BIT(25)
134 /* SDC_CFG_BUSWIDTH */
135 #define MSDC_BUS_1BITS 0x0
136 #define MSDC_BUS_4BITS 0x1
137 #define MSDC_BUS_8BITS 0x2
139 #define MSDC_FIFO_SIZE 128
141 #define PAD_DELAY_MAX 32
143 #define DEFAULT_CD_DEBOUNCE 8
145 #define CMD_INTS_MASK \
146 (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO)
148 #define DATA_INTS_MASK \
149 (MSDC_INT_XFER_COMPL | MSDC_INT_DATTMO | MSDC_INT_DATCRCERR)
151 /* Register offset */
217 struct msdc_compatible {
227 struct msdc_delay_phase {
234 struct mmc_config cfg;
238 struct msdc_tune_para {
244 struct mtk_sd_regs *base;
247 struct msdc_compatible *dev_comp;
249 struct clk src_clk; /* for SD/MMC bus clock */
250 struct clk h_clk; /* MSDC core clock */
252 u32 src_clk_freq; /* source clock */
253 u32 mclk; /* mmc framework required bus clock */
254 u32 sclk; /* actual calculated bus clock */
256 /* operation timeout clocks */
262 u32 hs200_cmd_int_delay;
263 u32 hs200_write_int_delay;
265 u32 r_smpl; /* sample edge */
268 /* whether to use gpio detection or built-in hw detection */
271 /* card detection / write protection GPIOs */
272 #ifdef CONFIG_DM_GPIO
273 struct gpio_desc gpio_wp;
274 struct gpio_desc gpio_cd;
278 uint last_data_write;
280 enum bus_mode timing;
282 struct msdc_tune_para def_tune_para;
283 struct msdc_tune_para saved_tune_para;
286 static void msdc_reset_hw(struct msdc_host *host)
290 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_RST);
292 readl_poll_timeout(&host->base->msdc_cfg, reg,
293 !(reg & MSDC_CFG_RST), 1000000);
296 static void msdc_fifo_clr(struct msdc_host *host)
300 setbits_le32(&host->base->msdc_fifocs, MSDC_FIFOCS_CLR);
302 readl_poll_timeout(&host->base->msdc_fifocs, reg,
303 !(reg & MSDC_FIFOCS_CLR), 1000000);
306 static u32 msdc_fifo_rx_bytes(struct msdc_host *host)
308 return (readl(&host->base->msdc_fifocs) &
309 MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S;
312 static u32 msdc_fifo_tx_bytes(struct msdc_host *host)
314 return (readl(&host->base->msdc_fifocs) &
315 MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S;
318 static u32 msdc_cmd_find_resp(struct msdc_host *host, struct mmc_cmd *cmd)
322 switch (cmd->resp_type) {
323 /* Actually, R1, R5, R6, R7 are the same */
345 static u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
347 struct mmc_data *data)
349 u32 opcode = cmd->cmdidx;
350 u32 resp_type = msdc_cmd_find_resp(host, cmd);
356 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
357 case MMC_CMD_READ_MULTIPLE_BLOCK:
360 case MMC_CMD_WRITE_SINGLE_BLOCK:
361 case MMC_CMD_READ_SINGLE_BLOCK:
362 case SD_CMD_APP_SEND_SCR:
365 case SD_CMD_SWITCH_FUNC: /* same as MMC_CMD_SWITCH */
366 case SD_CMD_SEND_IF_COND: /* same as MMC_CMD_SEND_EXT_CSD */
367 case SD_CMD_APP_SD_STATUS: /* same as MMC_CMD_SEND_STATUS */
373 if (data->flags == MMC_DATA_WRITE)
374 rawcmd |= SDC_CMD_WR;
376 if (data->blocks > 1)
379 blocksize = data->blocksize;
382 rawcmd |= ((opcode << SDC_CMD_CMD_S) & SDC_CMD_CMD_M) |
383 ((resp_type << SDC_CMD_RSPTYP_S) & SDC_CMD_RSPTYP_M) |
384 ((blocksize << SDC_CMD_BLK_LEN_S) & SDC_CMD_BLK_LEN_M) |
385 ((dtype << SDC_CMD_DTYPE_S) & SDC_CMD_DTYPE_M);
387 if (opcode == MMC_CMD_STOP_TRANSMISSION)
388 rawcmd |= SDC_CMD_STOP;
393 static int msdc_cmd_done(struct msdc_host *host, int events,
396 u32 *rsp = cmd->response;
399 if (cmd->resp_type & MMC_RSP_PRESENT) {
400 if (cmd->resp_type & MMC_RSP_136) {
401 rsp[0] = readl(&host->base->sdc_resp[3]);
402 rsp[1] = readl(&host->base->sdc_resp[2]);
403 rsp[2] = readl(&host->base->sdc_resp[1]);
404 rsp[3] = readl(&host->base->sdc_resp[0]);
406 rsp[0] = readl(&host->base->sdc_resp[0]);
410 if (!(events & MSDC_INT_CMDRDY)) {
411 if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
412 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
414 * should not clear fifo/interrupt as the tune data
415 * may have alreay come.
419 if (events & MSDC_INT_CMDTMO)
428 static bool msdc_cmd_is_ready(struct msdc_host *host)
433 /* The max busy time we can endure is 20ms */
434 ret = readl_poll_timeout(&host->base->sdc_sts, reg,
435 !(reg & SDC_STS_CMDBUSY), 20000);
438 pr_err("CMD bus busy detected\n");
443 if (host->last_resp_type == MMC_RSP_R1b && host->last_data_write) {
444 ret = readl_poll_timeout(&host->base->msdc_ps, reg,
445 reg & MSDC_PS_DAT0, 1000000);
448 pr_err("Card stuck in programming state!\n");
457 static int msdc_start_command(struct msdc_host *host, struct mmc_cmd *cmd,
458 struct mmc_data *data)
465 if (!msdc_cmd_is_ready(host))
470 host->last_resp_type = cmd->resp_type;
471 host->last_data_write = 0;
473 rawcmd = msdc_cmd_prepare_raw_cmd(host, cmd, data);
476 blocks = data->blocks;
478 writel(CMD_INTS_MASK, &host->base->msdc_int);
479 writel(blocks, &host->base->sdc_blk_num);
480 writel(cmd->cmdarg, &host->base->sdc_arg);
481 writel(rawcmd, &host->base->sdc_cmd);
483 ret = readl_poll_timeout(&host->base->msdc_int, status,
484 status & CMD_INTS_MASK, 1000000);
487 status = MSDC_INT_CMDTMO;
489 return msdc_cmd_done(host, status, cmd);
492 static void msdc_fifo_read(struct msdc_host *host, u8 *buf, u32 size)
496 while ((size_t)buf % 4) {
497 *buf++ = readb(&host->base->msdc_rxdata);
503 *wbuf++ = readl(&host->base->msdc_rxdata);
509 *buf++ = readb(&host->base->msdc_rxdata);
514 static void msdc_fifo_write(struct msdc_host *host, const u8 *buf, u32 size)
518 while ((size_t)buf % 4) {
519 writeb(*buf++, &host->base->msdc_txdata);
523 wbuf = (const u32 *)buf;
525 writel(*wbuf++, &host->base->msdc_txdata);
529 buf = (const u8 *)wbuf;
531 writeb(*buf++, &host->base->msdc_txdata);
536 static int msdc_pio_read(struct msdc_host *host, u8 *ptr, u32 size)
543 status = readl(&host->base->msdc_int);
544 writel(status, &host->base->msdc_int);
545 status &= DATA_INTS_MASK;
547 if (status & MSDC_INT_DATCRCERR) {
552 if (status & MSDC_INT_DATTMO) {
557 if (status & MSDC_INT_XFER_COMPL) {
559 pr_err("data not fully read\n");
566 chksz = min(size, (u32)MSDC_FIFO_SIZE);
568 if (msdc_fifo_rx_bytes(host) >= chksz) {
569 msdc_fifo_read(host, ptr, chksz);
578 static int msdc_pio_write(struct msdc_host *host, const u8 *ptr, u32 size)
585 status = readl(&host->base->msdc_int);
586 writel(status, &host->base->msdc_int);
587 status &= DATA_INTS_MASK;
589 if (status & MSDC_INT_DATCRCERR) {
594 if (status & MSDC_INT_DATTMO) {
599 if (status & MSDC_INT_XFER_COMPL) {
601 pr_err("data not fully written\n");
608 chksz = min(size, (u32)MSDC_FIFO_SIZE);
610 if (MSDC_FIFO_SIZE - msdc_fifo_tx_bytes(host) >= chksz) {
611 msdc_fifo_write(host, ptr, chksz);
620 static int msdc_start_data(struct msdc_host *host, struct mmc_data *data)
625 if (data->flags == MMC_DATA_WRITE)
626 host->last_data_write = 1;
628 writel(DATA_INTS_MASK, &host->base->msdc_int);
630 size = data->blocks * data->blocksize;
632 if (data->flags == MMC_DATA_WRITE)
633 ret = msdc_pio_write(host, (const u8 *)data->src, size);
635 ret = msdc_pio_read(host, (u8 *)data->dest, size);
645 static int msdc_ops_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
646 struct mmc_data *data)
648 struct msdc_host *host = dev_get_priv(dev);
651 ret = msdc_start_command(host, cmd, data);
656 return msdc_start_data(host, data);
661 static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
666 host->timeout_ns = ns;
667 host->timeout_clks = clks;
669 if (host->sclk == 0) {
672 clk_ns = 1000000000UL / host->sclk;
673 timeout = (ns + clk_ns - 1) / clk_ns + clks;
674 /* unit is 1048576 sclk cycles */
675 timeout = (timeout + (0x1 << 20) - 1) >> 20;
676 if (host->dev_comp->clk_div_bits == 8)
677 mode = (readl(&host->base->msdc_cfg) &
678 MSDC_CFG_CKMOD_M) >> MSDC_CFG_CKMOD_S;
680 mode = (readl(&host->base->msdc_cfg) &
681 MSDC_CFG_CKMOD_EXT_M) >> MSDC_CFG_CKMOD_EXT_S;
682 /* DDR mode will double the clk cycles for data timeout */
683 timeout = mode >= 2 ? timeout * 2 : timeout;
684 timeout = timeout > 1 ? timeout - 1 : 0;
685 timeout = timeout > 255 ? 255 : timeout;
688 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
689 timeout << SDC_CFG_DTOC_S);
692 static void msdc_set_buswidth(struct msdc_host *host, u32 width)
694 u32 val = readl(&host->base->sdc_cfg);
696 val &= ~SDC_CFG_BUSWIDTH_M;
701 val |= (MSDC_BUS_1BITS << SDC_CFG_BUSWIDTH_S);
704 val |= (MSDC_BUS_4BITS << SDC_CFG_BUSWIDTH_S);
707 val |= (MSDC_BUS_8BITS << SDC_CFG_BUSWIDTH_S);
711 writel(val, &host->base->sdc_cfg);
714 static void msdc_set_mclk(struct msdc_host *host, enum bus_mode timing, u32 hz)
723 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
727 if (host->dev_comp->clk_div_bits == 8)
728 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_HS400_CK_MODE);
730 clrbits_le32(&host->base->msdc_cfg,
731 MSDC_CFG_HS400_CK_MODE_EXT);
733 if (timing == UHS_DDR50 || timing == MMC_DDR_52 ||
734 timing == MMC_HS_400) {
735 if (timing == MMC_HS_400)
738 mode = 0x2; /* ddr mode and use divisor */
740 if (hz >= (host->src_clk_freq >> 2)) {
741 div = 0; /* mean div = 1/4 */
742 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
744 div = (host->src_clk_freq + ((hz << 2) - 1)) /
746 sclk = (host->src_clk_freq >> 2) / div;
750 if (timing == MMC_HS_400 && hz >= (host->src_clk_freq >> 1)) {
751 if (host->dev_comp->clk_div_bits == 8)
752 setbits_le32(&host->base->msdc_cfg,
753 MSDC_CFG_HS400_CK_MODE);
755 setbits_le32(&host->base->msdc_cfg,
756 MSDC_CFG_HS400_CK_MODE_EXT);
758 sclk = host->src_clk_freq >> 1;
759 div = 0; /* div is ignore when bit18 is set */
761 } else if (hz >= host->src_clk_freq) {
762 mode = 0x1; /* no divisor */
764 sclk = host->src_clk_freq;
766 mode = 0x0; /* use divisor */
767 if (hz >= (host->src_clk_freq >> 1)) {
768 div = 0; /* mean div = 1/2 */
769 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
771 div = (host->src_clk_freq + ((hz << 2) - 1)) /
773 sclk = (host->src_clk_freq >> 2) / div;
777 clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
779 if (host->dev_comp->clk_div_bits == 8) {
780 div = min(div, (u32)(MSDC_CFG_CKDIV_M >> MSDC_CFG_CKDIV_S));
781 clrsetbits_le32(&host->base->msdc_cfg,
782 MSDC_CFG_CKMOD_M | MSDC_CFG_CKDIV_M,
783 (mode << MSDC_CFG_CKMOD_S) |
784 (div << MSDC_CFG_CKDIV_S));
786 div = min(div, (u32)(MSDC_CFG_CKDIV_EXT_M >>
787 MSDC_CFG_CKDIV_EXT_S));
788 clrsetbits_le32(&host->base->msdc_cfg,
789 MSDC_CFG_CKMOD_EXT_M | MSDC_CFG_CKDIV_EXT_M,
790 (mode << MSDC_CFG_CKMOD_EXT_S) |
791 (div << MSDC_CFG_CKDIV_EXT_S));
794 readl_poll_timeout(&host->base->msdc_cfg, reg,
795 reg & MSDC_CFG_CKSTB, 1000000);
797 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN);
800 host->timing = timing;
802 /* needed because clk changed. */
803 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
806 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
807 * tune result of hs200/200Mhz is not suitable for 50Mhz
809 if (host->sclk <= 52000000) {
810 writel(host->def_tune_para.iocon, &host->base->msdc_iocon);
811 writel(host->def_tune_para.pad_tune,
812 &host->base->pad_tune);
814 writel(host->saved_tune_para.iocon, &host->base->msdc_iocon);
815 writel(host->saved_tune_para.pad_tune,
816 &host->base->pad_tune);
819 dev_dbg(dev, "sclk: %d, timing: %d\n", host->sclk, timing);
822 static int msdc_ops_set_ios(struct udevice *dev)
824 struct msdc_plat *plat = dev_get_platdata(dev);
825 struct msdc_host *host = dev_get_priv(dev);
826 struct mmc *mmc = &plat->mmc;
827 uint clock = mmc->clock;
829 msdc_set_buswidth(host, mmc->bus_width);
831 if (mmc->clk_disable)
833 else if (clock < mmc->cfg->f_min)
834 clock = mmc->cfg->f_min;
836 if (host->mclk != clock || host->timing != mmc->selected_mode)
837 msdc_set_mclk(host, mmc->selected_mode, clock);
842 static int msdc_ops_get_cd(struct udevice *dev)
844 struct msdc_host *host = dev_get_priv(dev);
847 if (host->builtin_cd) {
848 val = readl(&host->base->msdc_ps);
849 return !(val & MSDC_PS_CDSTS);
852 #ifdef CONFIG_DM_GPIO
853 if (!host->gpio_cd.dev)
856 return dm_gpio_get_value(&host->gpio_cd);
862 static int msdc_ops_get_wp(struct udevice *dev)
864 struct msdc_host *host = dev_get_priv(dev);
866 #ifdef CONFIG_DM_GPIO
867 if (!host->gpio_wp.dev)
870 return !dm_gpio_get_value(&host->gpio_wp);
876 #ifdef MMC_SUPPORTS_TUNING
877 static u32 test_delay_bit(u32 delay, u32 bit)
879 bit %= PAD_DELAY_MAX;
880 return delay & (1 << bit);
883 static int get_delay_len(u32 delay, u32 start_bit)
887 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
888 if (test_delay_bit(delay, start_bit + i) == 0)
892 return PAD_DELAY_MAX - start_bit;
895 static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay)
897 int start = 0, len = 0;
898 int start_final = 0, len_final = 0;
899 u8 final_phase = 0xff;
900 struct msdc_delay_phase delay_phase = { 0, };
903 dev_err(dev, "phase error: [map:%x]\n", delay);
904 delay_phase.final_phase = final_phase;
908 while (start < PAD_DELAY_MAX) {
909 len = get_delay_len(delay, start);
910 if (len_final < len) {
915 start += len ? len : 1;
916 if (len >= 12 && start_final < 4)
920 /* The rule is to find the smallest delay cell */
921 if (start_final == 0)
922 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
924 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
926 dev_info(dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
927 delay, len_final, final_phase);
929 delay_phase.maxlen = len_final;
930 delay_phase.start = start_final;
931 delay_phase.final_phase = final_phase;
935 static int msdc_tune_response(struct udevice *dev, u32 opcode)
937 struct msdc_plat *plat = dev_get_platdata(dev);
938 struct msdc_host *host = dev_get_priv(dev);
939 struct mmc *mmc = &plat->mmc;
940 u32 rise_delay = 0, fall_delay = 0;
941 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
942 struct msdc_delay_phase internal_delay_phase;
943 u8 final_delay, final_maxlen;
944 u32 internal_delay = 0;
945 void __iomem *tune_reg = &host->base->pad_tune;
949 if (host->dev_comp->pad_tune0)
950 tune_reg = &host->base->pad_tune0;
952 if (mmc->selected_mode == MMC_HS_200 ||
953 mmc->selected_mode == UHS_SDR104)
954 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
955 host->hs200_cmd_int_delay <<
956 MSDC_PAD_TUNE_CMDRRDLY_S);
958 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
960 for (i = 0; i < PAD_DELAY_MAX; i++) {
961 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
962 i << MSDC_PAD_TUNE_CMDRDLY_S);
964 for (j = 0; j < 3; j++) {
965 mmc_send_tuning(mmc, opcode, &cmd_err);
967 rise_delay |= (1 << i);
969 rise_delay &= ~(1 << i);
975 final_rise_delay = get_best_delay(host, rise_delay);
976 /* if rising edge has enough margin, do not scan falling edge */
977 if (final_rise_delay.maxlen >= 12 ||
978 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
981 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
982 for (i = 0; i < PAD_DELAY_MAX; i++) {
983 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
984 i << MSDC_PAD_TUNE_CMDRDLY_S);
986 for (j = 0; j < 3; j++) {
987 mmc_send_tuning(mmc, opcode, &cmd_err);
989 fall_delay |= (1 << i);
991 fall_delay &= ~(1 << i);
997 final_fall_delay = get_best_delay(host, fall_delay);
1000 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1001 if (final_maxlen == final_rise_delay.maxlen) {
1002 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1003 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1004 final_rise_delay.final_phase <<
1005 MSDC_PAD_TUNE_CMDRDLY_S);
1006 final_delay = final_rise_delay.final_phase;
1008 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1009 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M,
1010 final_fall_delay.final_phase <<
1011 MSDC_PAD_TUNE_CMDRDLY_S);
1012 final_delay = final_fall_delay.final_phase;
1015 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
1018 for (i = 0; i < PAD_DELAY_MAX; i++) {
1019 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1020 i << MSDC_PAD_TUNE_CMDRRDLY_S);
1022 mmc_send_tuning(mmc, opcode, &cmd_err);
1024 internal_delay |= (1 << i);
1027 dev_err(dev, "Final internal delay: 0x%x\n", internal_delay);
1029 internal_delay_phase = get_best_delay(host, internal_delay);
1030 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M,
1031 internal_delay_phase.final_phase <<
1032 MSDC_PAD_TUNE_CMDRRDLY_S);
1035 dev_err(dev, "Final cmd pad delay: %x\n", final_delay);
1036 return final_delay == 0xff ? -EIO : 0;
1039 static int msdc_tune_data(struct udevice *dev, u32 opcode)
1041 struct msdc_plat *plat = dev_get_platdata(dev);
1042 struct msdc_host *host = dev_get_priv(dev);
1043 struct mmc *mmc = &plat->mmc;
1044 u32 rise_delay = 0, fall_delay = 0;
1045 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, };
1046 u8 final_delay, final_maxlen;
1047 void __iomem *tune_reg = &host->base->pad_tune;
1051 if (host->dev_comp->pad_tune0)
1052 tune_reg = &host->base->pad_tune0;
1054 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1055 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1057 for (i = 0; i < PAD_DELAY_MAX; i++) {
1058 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1059 i << MSDC_PAD_TUNE_DATRRDLY_S);
1061 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1063 rise_delay |= (1 << i);
1064 } else if (cmd_err) {
1065 /* in this case, retune response is needed */
1066 ret = msdc_tune_response(dev, opcode);
1072 final_rise_delay = get_best_delay(host, rise_delay);
1073 if (final_rise_delay.maxlen >= 12 ||
1074 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1077 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1078 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1080 for (i = 0; i < PAD_DELAY_MAX; i++) {
1081 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1082 i << MSDC_PAD_TUNE_DATRRDLY_S);
1084 ret = mmc_send_tuning(mmc, opcode, &cmd_err);
1086 fall_delay |= (1 << i);
1087 } else if (cmd_err) {
1088 /* in this case, retune response is needed */
1089 ret = msdc_tune_response(dev, opcode);
1095 final_fall_delay = get_best_delay(host, fall_delay);
1098 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
1099 if (final_maxlen == final_rise_delay.maxlen) {
1100 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1101 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1102 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1103 final_rise_delay.final_phase <<
1104 MSDC_PAD_TUNE_DATRRDLY_S);
1105 final_delay = final_rise_delay.final_phase;
1107 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL);
1108 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL);
1109 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M,
1110 final_fall_delay.final_phase <<
1111 MSDC_PAD_TUNE_DATRRDLY_S);
1112 final_delay = final_fall_delay.final_phase;
1115 if (mmc->selected_mode == MMC_HS_200 ||
1116 mmc->selected_mode == UHS_SDR104)
1117 clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATWRDLY_M,
1118 host->hs200_write_int_delay <<
1119 MSDC_PAD_TUNE_DATWRDLY_S);
1121 dev_err(dev, "Final data pad delay: %x\n", final_delay);
1123 return final_delay == 0xff ? -EIO : 0;
1126 static int msdc_execute_tuning(struct udevice *dev, uint opcode)
1128 struct msdc_plat *plat = dev_get_platdata(dev);
1129 struct msdc_host *host = dev_get_priv(dev);
1130 struct mmc *mmc = &plat->mmc;
1133 if (mmc->selected_mode == MMC_HS_400) {
1134 writel(host->hs400_ds_delay, &host->base->pad_ds_tune);
1135 /* for hs400 mode it must be set to 0 */
1136 clrbits_le32(&host->base->patch_bit2, MSDC_PB2_CFGCRCSTS);
1137 host->hs400_mode = true;
1140 ret = msdc_tune_response(dev, opcode);
1142 dev_err(dev, "Tune response fail!\n");
1146 if (!host->hs400_mode) {
1147 ret = msdc_tune_data(dev, opcode);
1149 dev_err(dev, "Tune data fail!\n");
1152 host->saved_tune_para.iocon = readl(&host->base->msdc_iocon);
1153 host->saved_tune_para.pad_tune = readl(&host->base->pad_tune);
1159 static void msdc_init_hw(struct msdc_host *host)
1162 void __iomem *tune_reg = &host->base->pad_tune;
1164 if (host->dev_comp->pad_tune0)
1165 tune_reg = &host->base->pad_tune0;
1167 /* Configure to MMC/SD mode, clock free running */
1168 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_MODE);
1171 setbits_le32(&host->base->msdc_cfg, MSDC_CFG_PIO);
1174 msdc_reset_hw(host);
1176 /* Enable/disable hw card detection according to fdt option */
1177 if (host->builtin_cd)
1178 clrsetbits_le32(&host->base->msdc_ps,
1180 (DEFAULT_CD_DEBOUNCE << MSDC_PS_CDDBCE_S) |
1183 clrbits_le32(&host->base->msdc_ps, MSDC_PS_CDEN);
1185 /* Clear all interrupts */
1186 val = readl(&host->base->msdc_int);
1187 writel(val, &host->base->msdc_int);
1189 /* Enable data & cmd interrupts */
1190 writel(DATA_INTS_MASK | CMD_INTS_MASK, &host->base->msdc_inten);
1192 writel(0, tune_reg);
1193 writel(0, &host->base->msdc_iocon);
1196 setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1198 clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL);
1200 writel(0x403c0046, &host->base->patch_bit0);
1201 writel(0xffff4089, &host->base->patch_bit1);
1203 if (host->dev_comp->stop_clk_fix)
1204 clrsetbits_le32(&host->base->patch_bit1, MSDC_PB1_STOP_DLY_M,
1205 3 << MSDC_PB1_STOP_DLY_S);
1207 if (host->dev_comp->busy_check)
1208 clrbits_le32(&host->base->patch_bit1, (1 << 7));
1210 setbits_le32(&host->base->emmc50_cfg0, EMMC50_CFG_CFCSTS_SEL);
1212 if (host->dev_comp->async_fifo) {
1213 clrsetbits_le32(&host->base->patch_bit2, MSDC_PB2_RESPWAIT_M,
1214 3 << MSDC_PB2_RESPWAIT_S);
1216 if (host->dev_comp->enhance_rx) {
1217 setbits_le32(&host->base->sdc_adv_cfg0,
1220 clrsetbits_le32(&host->base->patch_bit2,
1221 MSDC_PB2_RESPSTSENSEL_M,
1222 2 << MSDC_PB2_RESPSTSENSEL_S);
1223 clrsetbits_le32(&host->base->patch_bit2,
1224 MSDC_PB2_CRCSTSENSEL_M,
1225 2 << MSDC_PB2_CRCSTSENSEL_S);
1228 /* use async fifo to avoid tune internal delay */
1229 clrbits_le32(&host->base->patch_bit2,
1231 clrbits_le32(&host->base->patch_bit2,
1232 MSDC_PB2_CFGCRCSTS);
1235 if (host->dev_comp->data_tune) {
1236 setbits_le32(tune_reg,
1237 MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL);
1238 clrsetbits_le32(&host->base->patch_bit0,
1239 MSDC_INT_DAT_LATCH_CK_SEL_M,
1241 MSDC_INT_DAT_LATCH_CK_SEL_S);
1243 /* choose clock tune */
1244 setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
1247 /* Configure to enable SDIO mode otherwise sdio cmd5 won't work */
1248 setbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIO);
1250 /* disable detecting SDIO device interrupt function */
1251 clrbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIOIDE);
1253 /* Configure to default data timeout */
1254 clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M,
1255 3 << SDC_CFG_DTOC_S);
1257 if (host->dev_comp->stop_clk_fix) {
1258 clrbits_le32(&host->base->sdc_fifo_cfg,
1259 SDC_FIFO_CFG_WRVALIDSEL);
1260 clrbits_le32(&host->base->sdc_fifo_cfg,
1261 SDC_FIFO_CFG_RDVALIDSEL);
1264 host->def_tune_para.iocon = readl(&host->base->msdc_iocon);
1265 host->def_tune_para.pad_tune = readl(&host->base->pad_tune);
1268 static void msdc_ungate_clock(struct msdc_host *host)
1270 clk_enable(&host->src_clk);
1271 clk_enable(&host->h_clk);
1274 static int msdc_drv_probe(struct udevice *dev)
1276 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1277 struct msdc_plat *plat = dev_get_platdata(dev);
1278 struct msdc_host *host = dev_get_priv(dev);
1279 struct mmc_config *cfg = &plat->cfg;
1281 cfg->name = dev->name;
1283 host->dev_comp = (struct msdc_compatible *)dev_get_driver_data(dev);
1285 host->src_clk_freq = clk_get_rate(&host->src_clk);
1287 if (host->dev_comp->clk_div_bits == 8)
1288 cfg->f_min = host->src_clk_freq / (4 * 255);
1290 cfg->f_min = host->src_clk_freq / (4 * 4095);
1291 cfg->f_max = host->src_clk_freq / 2;
1294 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
1296 host->mmc = &plat->mmc;
1297 host->timeout_ns = 100000000;
1298 host->timeout_clks = 3 * 1048576;
1300 #ifdef CONFIG_PINCTRL
1301 pinctrl_select_state(dev, "default");
1304 msdc_ungate_clock(host);
1307 upriv->mmc = &plat->mmc;
1312 static int msdc_ofdata_to_platdata(struct udevice *dev)
1314 struct msdc_plat *plat = dev_get_platdata(dev);
1315 struct msdc_host *host = dev_get_priv(dev);
1316 struct mmc_config *cfg = &plat->cfg;
1319 host->base = (void *)dev_read_addr(dev);
1323 ret = mmc_of_parse(dev, cfg);
1327 ret = clk_get_by_name(dev, "source", &host->src_clk);
1331 ret = clk_get_by_name(dev, "hclk", &host->h_clk);
1335 #ifdef CONFIG_DM_GPIO
1336 gpio_request_by_name(dev, "wp-gpios", 0, &host->gpio_wp, GPIOD_IS_IN);
1337 gpio_request_by_name(dev, "cd-gpios", 0, &host->gpio_cd, GPIOD_IS_IN);
1340 host->hs400_ds_delay = dev_read_u32_default(dev, "hs400-ds-delay", 0);
1341 host->hs200_cmd_int_delay =
1342 dev_read_u32_default(dev, "cmd_int_delay", 0);
1343 host->hs200_write_int_delay =
1344 dev_read_u32_default(dev, "write_int_delay", 0);
1345 host->latch_ck = dev_read_u32_default(dev, "latch-ck", 0);
1346 host->r_smpl = dev_read_u32_default(dev, "r_smpl", 0);
1347 host->builtin_cd = dev_read_u32_default(dev, "builtin-cd", 0);
1352 static int msdc_drv_bind(struct udevice *dev)
1354 struct msdc_plat *plat = dev_get_platdata(dev);
1356 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1359 static const struct dm_mmc_ops msdc_ops = {
1360 .send_cmd = msdc_ops_send_cmd,
1361 .set_ios = msdc_ops_set_ios,
1362 .get_cd = msdc_ops_get_cd,
1363 .get_wp = msdc_ops_get_wp,
1364 #ifdef MMC_SUPPORTS_TUNING
1365 .execute_tuning = msdc_execute_tuning,
1369 static const struct msdc_compatible mt7623_compat = {
1374 .busy_check = false,
1375 .stop_clk_fix = false,
1379 static const struct udevice_id msdc_ids[] = {
1380 { .compatible = "mediatek,mt7623-mmc", .data = (ulong)&mt7623_compat },
1384 U_BOOT_DRIVER(mtk_sd_drv) = {
1387 .of_match = msdc_ids,
1388 .ofdata_to_platdata = msdc_ofdata_to_platdata,
1389 .bind = msdc_drv_bind,
1390 .probe = msdc_drv_probe,
1392 .platdata_auto_alloc_size = sizeof(struct msdc_plat),
1393 .priv_auto_alloc_size = sizeof(struct msdc_host),