2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/clock.h>
16 #include <asm/arch/periph.h>
17 #include <linux/err.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct rockchip_mmc_plat {
22 struct mmc_config cfg;
26 struct rockchip_dwmmc_priv {
28 struct dwmci_host host;
34 static uint rockchip_dwmmc_get_mmc_clk(struct dwmci_host *host, uint freq)
36 struct udevice *dev = host->priv;
37 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
40 ret = clk_set_rate(&priv->clk, freq);
42 debug("%s: err=%d\n", __func__, ret);
49 static int rockchip_dwmmc_ofdata_to_platdata(struct udevice *dev)
51 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
52 struct dwmci_host *host = &priv->host;
54 host->name = dev->name;
55 host->ioaddr = (void *)dev_get_addr(dev);
56 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
58 host->get_mmc_clk = rockchip_dwmmc_get_mmc_clk;
61 /* use non-removeable as sdcard and emmc as judgement */
62 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, "non-removable"))
67 priv->fifo_depth = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
69 if (priv->fifo_depth < 0)
71 priv->fifo_mode = fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
73 if (fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
74 "clock-freq-min-max", priv->minmax, 2))
80 static int rockchip_dwmmc_probe(struct udevice *dev)
82 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
83 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
84 struct rockchip_dwmmc_priv *priv = dev_get_priv(dev);
85 struct dwmci_host *host = &priv->host;
86 struct udevice *pwr_dev __maybe_unused;
89 ret = clk_get_by_index(dev, 0, &priv->clk);
93 host->fifoth_val = MSIZE(0x2) |
94 RX_WMARK(priv->fifo_depth / 2 - 1) |
95 TX_WMARK(priv->fifo_depth / 2);
97 host->fifo_mode = priv->fifo_mode;
100 /* Enable power if needed */
101 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
104 ret = pwrseq_set_power(pwr_dev, true);
109 dwmci_setup_cfg(&plat->cfg, dev->name, host->buswidth, host->caps,
110 priv->minmax[1], priv->minmax[0]);
111 host->mmc = &plat->mmc;
112 host->mmc->priv = &priv->host;
113 host->mmc->dev = dev;
114 upriv->mmc = host->mmc;
116 return dwmci_probe(dev);
119 static int rockchip_dwmmc_bind(struct udevice *dev)
121 struct rockchip_mmc_plat *plat = dev_get_platdata(dev);
124 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
131 static const struct udevice_id rockchip_dwmmc_ids[] = {
132 { .compatible = "rockchip,rk3288-dw-mshc" },
136 U_BOOT_DRIVER(rockchip_dwmmc_drv) = {
137 .name = "rockchip_dwmmc",
139 .of_match = rockchip_dwmmc_ids,
140 .ofdata_to_platdata = rockchip_dwmmc_ofdata_to_platdata,
141 .ops = &dm_dwmci_ops,
142 .bind = rockchip_dwmmc_bind,
143 .probe = rockchip_dwmmc_probe,
144 .priv_auto_alloc_size = sizeof(struct rockchip_dwmmc_priv),
145 .platdata_auto_alloc_size = sizeof(struct rockchip_mmc_plat),
149 static int rockchip_dwmmc_pwrseq_set_power(struct udevice *dev, bool enable)
151 struct gpio_desc reset;
154 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
157 dm_gpio_set_value(&reset, 1);
159 dm_gpio_set_value(&reset, 0);
165 static const struct pwrseq_ops rockchip_dwmmc_pwrseq_ops = {
166 .set_power = rockchip_dwmmc_pwrseq_set_power,
169 static const struct udevice_id rockchip_dwmmc_pwrseq_ids[] = {
170 { .compatible = "mmc-pwrseq-emmc" },
174 U_BOOT_DRIVER(rockchip_dwmmc_pwrseq_drv) = {
175 .name = "mmc_pwrseq_emmc",
177 .of_match = rockchip_dwmmc_pwrseq_ids,
178 .ops = &rockchip_dwmmc_pwrseq_ops,