1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007-2011
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Aaron <leafy.myeh@allwinnertech.com>
7 * MMC driver for allwinner sunxi platform.
20 #include <asm/arch/clock.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/mmc.h>
23 #include <linux/delay.h>
25 #ifndef CCM_MMC_CTRL_MODE_SEL_NEW
26 #define CCM_MMC_CTRL_MODE_SEL_NEW 0
29 struct sunxi_mmc_plat {
30 struct mmc_config cfg;
34 struct sunxi_mmc_priv {
38 struct gpio_desc cd_gpio; /* Change Detect GPIO */
39 struct sunxi_mmc *reg;
40 struct mmc_config cfg;
43 #if !CONFIG_IS_ENABLED(DM_MMC)
44 /* support 4 mmc hosts */
45 struct sunxi_mmc_priv mmc_host[4];
47 static int sunxi_mmc_getcd_gpio(int sdc_no)
50 case 0: return sunxi_name_to_gpio(CONFIG_MMC0_CD_PIN);
51 case 1: return sunxi_name_to_gpio(CONFIG_MMC1_CD_PIN);
52 case 2: return sunxi_name_to_gpio(CONFIG_MMC2_CD_PIN);
53 case 3: return sunxi_name_to_gpio(CONFIG_MMC3_CD_PIN);
58 static int mmc_resource_init(int sdc_no)
60 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
61 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
64 debug("init mmc %d resource\n", sdc_no);
68 priv->reg = (struct sunxi_mmc *)SUNXI_MMC0_BASE;
69 priv->mclkreg = &ccm->sd0_clk_cfg;
72 priv->reg = (struct sunxi_mmc *)SUNXI_MMC1_BASE;
73 priv->mclkreg = &ccm->sd1_clk_cfg;
76 priv->reg = (struct sunxi_mmc *)SUNXI_MMC2_BASE;
77 priv->mclkreg = &ccm->sd2_clk_cfg;
79 #ifdef SUNXI_MMC3_BASE
81 priv->reg = (struct sunxi_mmc *)SUNXI_MMC3_BASE;
82 priv->mclkreg = &ccm->sd3_clk_cfg;
86 printf("Wrong mmc number %d\n", sdc_no);
89 priv->mmc_no = sdc_no;
91 cd_pin = sunxi_mmc_getcd_gpio(sdc_no);
93 ret = gpio_request(cd_pin, "mmc_cd");
95 sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
96 ret = gpio_direction_input(cd_pin);
105 * All A64 and later MMC controllers feature auto-calibration. This would
106 * normally be detected via the compatible string, but we need something
107 * which works in the SPL as well.
109 static bool sunxi_mmc_can_calibrate(void)
111 return IS_ENABLED(CONFIG_MACH_SUN50I) ||
112 IS_ENABLED(CONFIG_MACH_SUN50I_H5) ||
113 IS_ENABLED(CONFIG_SUN50I_GEN_H6) ||
114 IS_ENABLED(CONFIG_MACH_SUN8I_R40);
117 static int mmc_set_mod_clk(struct sunxi_mmc_priv *priv, unsigned int hz)
119 unsigned int pll, pll_hz, div, n, oclk_dly, sclk_dly;
120 bool new_mode = IS_ENABLED(CONFIG_MMC_SUNXI_HAS_NEW_MODE);
123 /* A83T support new mode only on eMMC */
124 if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T) && priv->mmc_no != 2)
127 if (hz <= 24000000) {
128 pll = CCM_MMC_CTRL_OSCM24;
131 #ifdef CONFIG_MACH_SUN9I
132 pll = CCM_MMC_CTRL_PLL_PERIPH0;
133 pll_hz = clock_get_pll4_periph0();
136 * SoCs since the A64 (H5, H6, H616) actually use the doubled
137 * rate of PLL6/PERIPH0 as an input clock, but compensate for
138 * that with a fixed post-divider of 2 in the mod clock.
139 * This cancels each other out, so for simplicity we just
140 * pretend it's always PLL6 without a post divider here.
142 pll = CCM_MMC_CTRL_PLL6;
143 pll_hz = clock_get_pll6();
158 printf("mmc %u error cannot set clock to %u\n", priv->mmc_no,
163 /* determine delays */
167 } else if (hz <= 25000000) {
171 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
186 val |= CCM_MMC_CTRL_MODE_SEL_NEW;
187 setbits_le32(&priv->reg->ntsr, SUNXI_MMC_NTSR_MODE_SEL_NEW);
190 if (!sunxi_mmc_can_calibrate()) {
192 * Use hardcoded delay values if controller doesn't support
195 val = CCM_MMC_CTRL_OCLK_DLY(oclk_dly) |
196 CCM_MMC_CTRL_SCLK_DLY(sclk_dly);
199 writel(CCM_MMC_CTRL_ENABLE| pll | CCM_MMC_CTRL_N(n) |
200 CCM_MMC_CTRL_M(div) | val, priv->mclkreg);
202 debug("mmc %u set mod-clk req %u parent %u n %u m %u rate %u\n",
203 priv->mmc_no, hz, pll_hz, 1u << n, div, pll_hz / (1u << n) / div);
208 static int mmc_update_clk(struct sunxi_mmc_priv *priv)
211 unsigned timeout_msecs = 2000;
212 unsigned long start = get_timer(0);
214 cmd = SUNXI_MMC_CMD_START |
215 SUNXI_MMC_CMD_UPCLK_ONLY |
216 SUNXI_MMC_CMD_WAIT_PRE_OVER;
218 writel(cmd, &priv->reg->cmd);
219 while (readl(&priv->reg->cmd) & SUNXI_MMC_CMD_START) {
220 if (get_timer(start) > timeout_msecs)
224 /* clock update sets various irq status bits, clear these */
225 writel(readl(&priv->reg->rint), &priv->reg->rint);
230 static int mmc_config_clock(struct sunxi_mmc_priv *priv, struct mmc *mmc)
232 unsigned rval = readl(&priv->reg->clkcr);
235 rval &= ~SUNXI_MMC_CLK_ENABLE;
236 writel(rval, &priv->reg->clkcr);
237 if (mmc_update_clk(priv))
240 /* Set mod_clk to new rate */
241 if (mmc_set_mod_clk(priv, mmc->clock))
244 /* Clear internal divider */
245 rval &= ~SUNXI_MMC_CLK_DIVIDER_MASK;
246 writel(rval, &priv->reg->clkcr);
248 #if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
249 /* A64 supports calibration of delays on MMC controller and we
250 * have to set delay of zero before starting calibration.
251 * Allwinner BSP driver sets a delay only in the case of
252 * using HS400 which is not supported by mainline U-Boot or
253 * Linux at the moment
255 if (sunxi_mmc_can_calibrate())
256 writel(SUNXI_MMC_CAL_DL_SW_EN, &priv->reg->samp_dl);
259 /* Re-enable Clock */
260 rval |= SUNXI_MMC_CLK_ENABLE;
261 writel(rval, &priv->reg->clkcr);
262 if (mmc_update_clk(priv))
268 static int sunxi_mmc_set_ios_common(struct sunxi_mmc_priv *priv,
271 debug("set ios: bus_width: %x, clock: %d\n",
272 mmc->bus_width, mmc->clock);
274 /* Change clock first */
275 if (mmc->clock && mmc_config_clock(priv, mmc) != 0) {
280 /* Change bus width */
281 if (mmc->bus_width == 8)
282 writel(0x2, &priv->reg->width);
283 else if (mmc->bus_width == 4)
284 writel(0x1, &priv->reg->width);
286 writel(0x0, &priv->reg->width);
291 #if !CONFIG_IS_ENABLED(DM_MMC)
292 static int sunxi_mmc_core_init(struct mmc *mmc)
294 struct sunxi_mmc_priv *priv = mmc->priv;
296 /* Reset controller */
297 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
304 static int mmc_trans_data_by_cpu(struct sunxi_mmc_priv *priv, struct mmc *mmc,
305 struct mmc_data *data)
307 const int reading = !!(data->flags & MMC_DATA_READ);
308 const uint32_t status_bit = reading ? SUNXI_MMC_STATUS_FIFO_EMPTY :
309 SUNXI_MMC_STATUS_FIFO_FULL;
311 unsigned *buff = (unsigned int *)(reading ? data->dest : data->src);
312 unsigned word_cnt = (data->blocksize * data->blocks) >> 2;
313 unsigned timeout_msecs = word_cnt >> 6;
317 if (timeout_msecs < 2000)
318 timeout_msecs = 2000;
320 /* Always read / write data through the CPU */
321 setbits_le32(&priv->reg->gctrl, SUNXI_MMC_GCTRL_ACCESS_BY_AHB);
323 start = get_timer(0);
325 for (i = 0; i < word_cnt;) {
326 unsigned int in_fifo;
328 while ((status = readl(&priv->reg->status)) & status_bit) {
329 if (get_timer(start) > timeout_msecs)
334 * For writing we do not easily know the FIFO size, so have
335 * to check the FIFO status after every word written.
336 * TODO: For optimisation we could work out a minimum FIFO
337 * size across all SoCs, and use that together with the current
338 * fill level to write chunks of words.
341 writel(buff[i++], &priv->reg->fifo);
346 * The status register holds the current FIFO level, so we
347 * can be sure to collect as many words from the FIFO
348 * register without checking the status register after every
349 * read. That saves half of the costly MMIO reads, effectively
350 * doubling the read performance.
351 * Some SoCs (A20) report a level of 0 if the FIFO is
352 * completely full (value masked out?). Use a safe minimal
353 * FIFO size in this case.
355 in_fifo = SUNXI_MMC_STATUS_FIFO_LEVEL(status);
356 if (in_fifo == 0 && (status & SUNXI_MMC_STATUS_FIFO_FULL))
358 for (; in_fifo > 0; in_fifo--)
359 buff[i++] = readl_relaxed(&priv->reg->fifo);
366 static int mmc_rint_wait(struct sunxi_mmc_priv *priv, struct mmc *mmc,
367 uint timeout_msecs, uint done_bit, const char *what)
370 unsigned long start = get_timer(0);
373 status = readl(&priv->reg->rint);
374 if ((get_timer(start) > timeout_msecs) ||
375 (status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT)) {
376 debug("%s timeout %x\n", what,
377 status & SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT);
380 } while (!(status & done_bit));
385 static int sunxi_mmc_send_cmd_common(struct sunxi_mmc_priv *priv,
386 struct mmc *mmc, struct mmc_cmd *cmd,
387 struct mmc_data *data)
389 unsigned int cmdval = SUNXI_MMC_CMD_START;
390 unsigned int timeout_msecs;
392 unsigned int status = 0;
393 unsigned int bytecnt = 0;
397 if (cmd->resp_type & MMC_RSP_BUSY)
398 debug("mmc cmd %d check rsp busy\n", cmd->cmdidx);
399 if (cmd->cmdidx == 12)
403 cmdval |= SUNXI_MMC_CMD_SEND_INIT_SEQ;
404 if (cmd->resp_type & MMC_RSP_PRESENT)
405 cmdval |= SUNXI_MMC_CMD_RESP_EXPIRE;
406 if (cmd->resp_type & MMC_RSP_136)
407 cmdval |= SUNXI_MMC_CMD_LONG_RESPONSE;
408 if (cmd->resp_type & MMC_RSP_CRC)
409 cmdval |= SUNXI_MMC_CMD_CHK_RESPONSE_CRC;
412 if ((u32)(long)data->dest & 0x3) {
417 cmdval |= SUNXI_MMC_CMD_DATA_EXPIRE|SUNXI_MMC_CMD_WAIT_PRE_OVER;
418 if (data->flags & MMC_DATA_WRITE)
419 cmdval |= SUNXI_MMC_CMD_WRITE;
420 if (data->blocks > 1)
421 cmdval |= SUNXI_MMC_CMD_AUTO_STOP;
422 writel(data->blocksize, &priv->reg->blksz);
423 writel(data->blocks * data->blocksize, &priv->reg->bytecnt);
426 debug("mmc %d, cmd %d(0x%08x), arg 0x%08x\n", priv->mmc_no,
427 cmd->cmdidx, cmdval | cmd->cmdidx, cmd->cmdarg);
428 writel(cmd->cmdarg, &priv->reg->arg);
431 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
434 * transfer data and check status
435 * STATREG[2] : FIFO empty
436 * STATREG[3] : FIFO full
441 bytecnt = data->blocksize * data->blocks;
442 debug("trans data %d bytes\n", bytecnt);
443 writel(cmdval | cmd->cmdidx, &priv->reg->cmd);
444 ret = mmc_trans_data_by_cpu(priv, mmc, data);
446 error = readl(&priv->reg->rint) &
447 SUNXI_MMC_RINT_INTERRUPT_ERROR_BIT;
453 error = mmc_rint_wait(priv, mmc, 1000, SUNXI_MMC_RINT_COMMAND_DONE,
460 debug("cacl timeout %x msec\n", timeout_msecs);
461 error = mmc_rint_wait(priv, mmc, timeout_msecs,
463 SUNXI_MMC_RINT_AUTO_COMMAND_DONE :
464 SUNXI_MMC_RINT_DATA_OVER,
470 if (cmd->resp_type & MMC_RSP_BUSY) {
471 unsigned long start = get_timer(0);
472 timeout_msecs = 2000;
475 status = readl(&priv->reg->status);
476 if (get_timer(start) > timeout_msecs) {
477 debug("busy timeout\n");
481 } while (status & SUNXI_MMC_STATUS_CARD_DATA_BUSY);
484 if (cmd->resp_type & MMC_RSP_136) {
485 cmd->response[0] = readl(&priv->reg->resp3);
486 cmd->response[1] = readl(&priv->reg->resp2);
487 cmd->response[2] = readl(&priv->reg->resp1);
488 cmd->response[3] = readl(&priv->reg->resp0);
489 debug("mmc resp 0x%08x 0x%08x 0x%08x 0x%08x\n",
490 cmd->response[3], cmd->response[2],
491 cmd->response[1], cmd->response[0]);
493 cmd->response[0] = readl(&priv->reg->resp0);
494 debug("mmc resp 0x%08x\n", cmd->response[0]);
498 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
499 mmc_update_clk(priv);
501 writel(0xffffffff, &priv->reg->rint);
502 writel(readl(&priv->reg->gctrl) | SUNXI_MMC_GCTRL_FIFO_RESET,
508 #if !CONFIG_IS_ENABLED(DM_MMC)
509 static int sunxi_mmc_set_ios_legacy(struct mmc *mmc)
511 struct sunxi_mmc_priv *priv = mmc->priv;
513 return sunxi_mmc_set_ios_common(priv, mmc);
516 static int sunxi_mmc_send_cmd_legacy(struct mmc *mmc, struct mmc_cmd *cmd,
517 struct mmc_data *data)
519 struct sunxi_mmc_priv *priv = mmc->priv;
521 return sunxi_mmc_send_cmd_common(priv, mmc, cmd, data);
524 static int sunxi_mmc_getcd_legacy(struct mmc *mmc)
526 struct sunxi_mmc_priv *priv = mmc->priv;
529 cd_pin = sunxi_mmc_getcd_gpio(priv->mmc_no);
533 return !gpio_get_value(cd_pin);
536 static const struct mmc_ops sunxi_mmc_ops = {
537 .send_cmd = sunxi_mmc_send_cmd_legacy,
538 .set_ios = sunxi_mmc_set_ios_legacy,
539 .init = sunxi_mmc_core_init,
540 .getcd = sunxi_mmc_getcd_legacy,
543 struct mmc *sunxi_mmc_init(int sdc_no)
545 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
546 struct sunxi_mmc_priv *priv = &mmc_host[sdc_no];
547 struct mmc_config *cfg = &priv->cfg;
550 memset(priv, '\0', sizeof(struct sunxi_mmc_priv));
552 cfg->name = "SUNXI SD/MMC";
553 cfg->ops = &sunxi_mmc_ops;
555 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
556 cfg->host_caps = MMC_MODE_4BIT;
558 if ((IS_ENABLED(CONFIG_MACH_SUN50I) || IS_ENABLED(CONFIG_MACH_SUN8I) ||
559 IS_ENABLED(CONFIG_SUN50I_GEN_H6)) && (sdc_no == 2))
560 cfg->host_caps = MMC_MODE_8BIT;
562 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
563 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
566 cfg->f_max = 52000000;
568 if (mmc_resource_init(sdc_no) != 0)
571 /* config ahb clock */
572 debug("init mmc %d clock and io\n", sdc_no);
573 #if !defined(CONFIG_SUN50I_GEN_H6)
574 setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MMC(sdc_no));
576 #ifdef CONFIG_SUNXI_GEN_SUN6I
578 setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MMC(sdc_no));
580 #if defined(CONFIG_MACH_SUN9I)
581 /* sun9i has a mmc-common module, also set the gate and reset there */
582 writel(SUNXI_MMC_COMMON_CLK_GATE | SUNXI_MMC_COMMON_RESET,
583 SUNXI_MMC_COMMON_BASE + 4 * sdc_no);
585 #else /* CONFIG_SUN50I_GEN_H6 */
586 setbits_le32(&ccm->sd_gate_reset, 1 << sdc_no);
588 setbits_le32(&ccm->sd_gate_reset, 1 << (RESET_SHIFT + sdc_no));
590 ret = mmc_set_mod_clk(priv, 24000000);
594 return mmc_create(cfg, priv);
598 static int sunxi_mmc_set_ios(struct udevice *dev)
600 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
601 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
603 return sunxi_mmc_set_ios_common(priv, &plat->mmc);
606 static int sunxi_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
607 struct mmc_data *data)
609 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
610 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
612 return sunxi_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
615 static int sunxi_mmc_getcd(struct udevice *dev)
617 struct mmc *mmc = mmc_get_mmc_dev(dev);
618 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
620 /* If polling, assume that the card is always present. */
621 if ((mmc->cfg->host_caps & MMC_CAP_NONREMOVABLE) ||
622 (mmc->cfg->host_caps & MMC_CAP_NEEDS_POLL))
625 if (dm_gpio_is_valid(&priv->cd_gpio)) {
626 int cd_state = dm_gpio_get_value(&priv->cd_gpio);
628 if (mmc->cfg->host_caps & MMC_CAP_CD_ACTIVE_HIGH)
636 static const struct dm_mmc_ops sunxi_mmc_ops = {
637 .send_cmd = sunxi_mmc_send_cmd,
638 .set_ios = sunxi_mmc_set_ios,
639 .get_cd = sunxi_mmc_getcd,
642 static unsigned get_mclk_offset(void)
644 if (IS_ENABLED(CONFIG_MACH_SUN9I_A80))
647 if (IS_ENABLED(CONFIG_SUN50I_GEN_H6))
653 static int sunxi_mmc_probe(struct udevice *dev)
655 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
656 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
657 struct sunxi_mmc_priv *priv = dev_get_priv(dev);
658 struct reset_ctl_bulk reset_bulk;
660 struct mmc_config *cfg = &plat->cfg;
661 struct ofnode_phandle_args args;
665 cfg->name = dev->name;
667 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
668 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
669 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
672 cfg->f_max = 52000000;
674 ret = mmc_of_parse(dev, cfg);
678 priv->reg = dev_read_addr_ptr(dev);
680 /* We don't have a sunxi clock driver so find the clock address here */
681 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
685 ccu_reg = (u32 *)(uintptr_t)ofnode_get_addr(args.node);
687 priv->mmc_no = ((uintptr_t)priv->reg - SUNXI_MMC0_BASE) / 0x1000;
688 priv->mclkreg = (void *)ccu_reg + get_mclk_offset() + priv->mmc_no * 4;
690 ret = clk_get_by_name(dev, "ahb", &gate_clk);
692 clk_enable(&gate_clk);
694 ret = reset_get_bulk(dev, &reset_bulk);
696 reset_deassert_bulk(&reset_bulk);
698 ret = mmc_set_mod_clk(priv, 24000000);
702 /* This GPIO is optional */
703 if (!gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
705 int cd_pin = gpio_get_number(&priv->cd_gpio);
707 sunxi_gpio_set_pull(cd_pin, SUNXI_GPIO_PULL_UP);
710 upriv->mmc = &plat->mmc;
712 /* Reset controller */
713 writel(SUNXI_MMC_GCTRL_RESET, &priv->reg->gctrl);
719 static int sunxi_mmc_bind(struct udevice *dev)
721 struct sunxi_mmc_plat *plat = dev_get_plat(dev);
723 return mmc_bind(dev, &plat->mmc, &plat->cfg);
726 static const struct udevice_id sunxi_mmc_ids[] = {
727 { .compatible = "allwinner,sun4i-a10-mmc" },
728 { .compatible = "allwinner,sun5i-a13-mmc" },
729 { .compatible = "allwinner,sun7i-a20-mmc" },
730 { .compatible = "allwinner,sun8i-a83t-emmc" },
731 { .compatible = "allwinner,sun9i-a80-mmc" },
732 { .compatible = "allwinner,sun50i-a64-mmc" },
733 { .compatible = "allwinner,sun50i-a64-emmc" },
734 { .compatible = "allwinner,sun50i-h6-mmc" },
735 { .compatible = "allwinner,sun50i-h6-emmc" },
736 { .compatible = "allwinner,sun50i-a100-mmc" },
737 { .compatible = "allwinner,sun50i-a100-emmc" },
741 U_BOOT_DRIVER(sunxi_mmc_drv) = {
744 .of_match = sunxi_mmc_ids,
745 .bind = sunxi_mmc_bind,
746 .probe = sunxi_mmc_probe,
747 .ops = &sunxi_mmc_ops,
748 .plat_auto = sizeof(struct sunxi_mmc_plat),
749 .priv_auto = sizeof(struct sunxi_mmc_priv),