1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
9 #include <dt-structs.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/periph.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
22 struct rockchip_mmc_plat {
23 #if CONFIG_IS_ENABLED(OF_PLATDATA)
24 struct dtd_rockchip_rk3288_dw_mshc dtplat;
26 struct mmc_config cfg;
30 struct rockchip_dwmmc_priv {
32 struct dwmci_host host;
38 static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
40 struct udevice *dev = host->priv;
41 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
45 * The clock frequency chosen here affects CLKDIV in the dw_mmc core.
46 * That can be either 0 or 1, but it must be set to 1 for eMMC DDR52
47 * 8-bit mode. It will be set to 0 for all other modes.
49 if (host->mmc->selected_mode == MMC_DDR_52 && host->mmc->bus_width == 8)
52 ret = clk_set_rate(&priv->clk, freq);
54 debug("%s: err=%d\n", __func__, ret);
61 static int rockchip_dwmmc_of_to_plat(struct udevice *dev)
63 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
64 struct dwmci_host *host = &priv->host;
66 if (!CONFIG_IS_ENABLED(OF_REAL))
69 host->name = dev->name;
70 host->ioaddr = dev_read_addr_ptr(dev);
71 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
72 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
75 /* use non-removeable as sdcard and emmc as judgement */
76 if (dev_read_bool(dev, "non-removable"))
81 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
83 if (priv->fifo_depth < 0)
85 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
87 #ifdef CONFIG_SPL_BUILD
89 priv->fifo_mode = dev_read_bool(dev, "u-boot,spl-fifo-mode");
93 * 'clock-freq-min-max' is deprecated
94 * (see https://github.com/torvalds/linux/commit/b023030f10573de738bbe8df63d43acab64c9f7b)
96 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
97 int val = dev_read_u32_default(dev, "max-frequency", -EINVAL);
102 priv->minmax[0] = 400000; /* 400 kHz */
103 priv->minmax[1] = val;
105 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
112 static int rockchip_dwmmc_probe(struct udevice *dev)
114 struct rockchip_mmc_plat *plat = dev_get_plat(dev);
115 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
116 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
117 struct dwmci_host *host = &priv->host;
120 #if CONFIG_IS_ENABLED(OF_PLATDATA)
121 struct dtd_rockchip_rk3288_dw_mshc *dtplat = &plat->dtplat;
123 host->name = dev->name;
124 host->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
125 host->buswidth = dtplat->bus_width;
126 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
129 priv->fifo_depth = dtplat->fifo_depth;
130 priv->fifo_mode = dtplat->u_boot_spl_fifo_mode;
131 priv->minmax[0] = 400000; /* 400 kHz */
132 priv->minmax[1] = dtplat->max_frequency;
134 ret = clk_get_by_phandle(dev, &dtplat->clocks[1], &priv->clk);
138 ret = clk_get_by_index(dev, 1, &priv->clk);
142 host->fifoth_val = MSIZE(0x2) |
143 RX_WMARK(priv->fifo_depth / 2 - 1) |
144 TX_WMARK(priv->fifo_depth / 2);
146 host->fifo_mode = priv->fifo_mode;
148 #ifdef CONFIG_MMC_PWRSEQ
149 /* Enable power if needed */
150 ret = mmc_pwrseq_get_power(dev, &plat->cfg);
152 ret = pwrseq_set_power(plat->cfg.pwr_dev, true);
157 dwmci_setup_cfg(&plat->cfg, host, priv->minmax[1], priv->minmax[0]);
158 host->mmc = &plat->mmc;
159 host->mmc->priv = &priv->host;
160 host->mmc->dev = dev;
161 upriv->mmc = host->mmc;
163 return dwmci_probe(dev);
166 static int rockchip_dwmmc_bind(struct udevice *dev)
168 struct rockchip_mmc_plat *plat = dev_get_plat(dev);
170 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
173 static const struct udevice_id rockchip_dwmmc_ids[] = {
174 { .compatible = "rockchip,rk2928-dw-mshc" },
175 { .compatible = "rockchip,rk3288-dw-mshc" },
179 U_BOOT_DRIVER(rockchip_rk3288_dw_mshc) = {
180 .name = "rockchip_rk3288_dw_mshc",
182 .of_match = rockchip_dwmmc_ids,
183 .of_to_plat = rockchip_dwmmc_of_to_plat,
184 .ops = &dm_dwmci_ops,
185 .bind = rockchip_dwmmc_bind,
186 .probe = rockchip_dwmmc_probe,
187 .priv_auto = sizeof(struct rockchip_dwmmc_priv),
188 .plat_auto = sizeof(struct rockchip_mmc_plat),
191 DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk2928_dw_mshc)
192 DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3328_dw_mshc)
193 DM_DRIVER_ALIAS(rockchip_rk3288_dw_mshc, rockchip_rk3368_dw_mshc)