1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Nexell
4 * Youngbok, Park <park@nexell.co.kr>
6 * (C) Copyright 2019 Stefan Bosch <stefan_b@posteo.net>
11 #include <dt-structs.h>
15 #include <asm/arch/reset.h>
16 #include <asm/arch/clk.h>
18 #define DWMCI_CLKSEL 0x09C
19 #define DWMCI_SHIFT_0 0x0
20 #define DWMCI_SHIFT_1 0x1
21 #define DWMCI_SHIFT_2 0x2
22 #define DWMCI_SHIFT_3 0x3
23 #define DWMCI_SET_SAMPLE_CLK(x) (x)
24 #define DWMCI_SET_DRV_CLK(x) ((x) << 16)
25 #define DWMCI_SET_DIV_RATIO(x) ((x) << 24)
26 #define DWMCI_CLKCTRL 0x114
27 #define NX_MMC_CLK_DELAY(x, y, a, b) ((((x) & 0xFF) << 0) |\
28 (((y) & 0x03) << 16) |\
29 (((a) & 0xFF) << 8) |\
32 struct nexell_mmc_plat {
33 struct mmc_config cfg;
37 struct nexell_dwmmc_priv {
39 struct dwmci_host host;
52 struct clk *clk_get(const char *id);
54 static void nx_dw_mmc_clksel(struct dwmci_host *host)
56 /* host->priv is pointer to "struct udevice" */
57 struct nexell_dwmmc_priv *priv = dev_get_priv(host->priv);
61 val = DWMCI_SET_SAMPLE_CLK(DWMCI_SHIFT_0) |
62 DWMCI_SET_DRV_CLK(DWMCI_SHIFT_0) | DWMCI_SET_DIV_RATIO(1);
64 val = DWMCI_SET_SAMPLE_CLK(DWMCI_SHIFT_0) |
65 DWMCI_SET_DRV_CLK(DWMCI_SHIFT_0) | DWMCI_SET_DIV_RATIO(3);
67 dwmci_writel(host, DWMCI_CLKSEL, val);
70 static void nx_dw_mmc_reset(int ch)
72 int rst_id = RESET_ID_SDMMC0 + ch;
74 nx_rstcon_setrst(rst_id, 0);
75 nx_rstcon_setrst(rst_id, 1);
78 static void nx_dw_mmc_clk_delay(struct udevice *dev)
81 struct nexell_dwmmc_priv *priv = dev_get_priv(dev);
82 struct dwmci_host *host = &priv->host;
84 delay = NX_MMC_CLK_DELAY(priv->d_delay,
85 priv->d_shift, priv->s_delay, priv->s_shift);
87 writel(delay, (host->ioaddr + DWMCI_CLKCTRL));
88 debug("%s: Values set: d_delay==%d, d_shift==%d, s_delay==%d, "
89 "s_shift==%d\n", __func__, priv->d_delay, priv->d_shift,
90 priv->s_delay, priv->s_shift);
93 static unsigned int nx_dw_mmc_get_clk(struct dwmci_host *host, uint freq)
96 struct udevice *dev = host->priv;
97 struct nexell_dwmmc_priv *priv = dev_get_priv(dev);
99 int index = host->dev_index;
100 char name[50] = { 0, };
104 sprintf(name, "%s.%d", DEV_NAME_SDHC, index);
105 clk = clk_get((const char *)name);
111 return clk_get_rate(clk) / 2;
114 static unsigned long nx_dw_mmc_set_clk(struct dwmci_host *host,
118 char name[50] = { 0, };
119 struct udevice *dev = host->priv;
120 struct nexell_dwmmc_priv *priv = dev_get_priv(dev);
122 int index = host->dev_index;
126 sprintf(name, "%s.%d", DEV_NAME_SDHC, index);
127 clk = clk_get((const char *)name);
129 debug("%s: clk_get(\"%s\") failed!\n", __func__, name);
136 rate = clk_set_rate(clk, rate);
142 static int nexell_dwmmc_of_to_plat(struct udevice *dev)
144 struct nexell_dwmmc_priv *priv = dev_get_priv(dev);
145 struct dwmci_host *host = &priv->host;
148 debug("%s\n", __func__);
150 host->name = dev->name;
151 host->ioaddr = dev_read_addr_ptr(dev);
152 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
153 host->get_mmc_clk = nx_dw_mmc_get_clk;
154 host->clksel = nx_dw_mmc_clksel;
157 val = dev_read_u32_default(dev, "index", -1);
158 if (val < 0 || val > 2) {
159 debug(" 'index' missing/invalid!\n");
162 host->dev_index = val;
164 priv->fifo_size = dev_read_u32_default(dev, "fifo-size", 0x20);
165 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
166 priv->frequency = dev_read_u32_default(dev, "frequency", 50000000);
167 priv->max_freq = dev_read_u32_default(dev, "max-frequency", 50000000);
168 priv->min_freq = 400000; /* 400 kHz */
169 priv->d_delay = dev_read_u32_default(dev, "drive_dly", 0);
170 priv->d_shift = dev_read_u32_default(dev, "drive_shift", 3);
171 priv->s_delay = dev_read_u32_default(dev, "sample_dly", 0);
172 priv->s_shift = dev_read_u32_default(dev, "sample_shift", 2);
173 priv->mmcboost = dev_read_u32_default(dev, "mmcboost", 0);
175 debug(" index==%d, name==%s, ioaddr==0x%08x\n",
176 host->dev_index, host->name, (u32)host->ioaddr);
180 static int nexell_dwmmc_probe(struct udevice *dev)
182 struct nexell_mmc_plat *plat = dev_get_plat(dev);
183 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
184 struct nexell_dwmmc_priv *priv = dev_get_priv(dev);
185 struct dwmci_host *host = &priv->host;
186 struct udevice *pwr_dev __maybe_unused;
188 host->fifoth_val = MSIZE(0x2) |
189 RX_WMARK(priv->fifo_size / 2 - 1) |
190 TX_WMARK(priv->fifo_size / 2);
192 host->fifo_mode = priv->fifo_mode;
194 dwmci_setup_cfg(&plat->cfg, host, priv->max_freq, priv->min_freq);
195 host->mmc = &plat->mmc;
196 host->mmc->priv = &priv->host;
197 host->mmc->dev = dev;
198 upriv->mmc = host->mmc;
200 if (nx_dw_mmc_set_clk(host, priv->frequency * 4) !=
201 priv->frequency * 4) {
202 debug("%s: nx_dw_mmc_set_clk(host, %d) failed!\n",
203 __func__, priv->frequency * 4);
206 debug("%s: nx_dw_mmc_set_clk(host, %d) OK\n",
207 __func__, priv->frequency * 4);
209 nx_dw_mmc_reset(host->dev_index);
210 nx_dw_mmc_clk_delay(dev);
212 return dwmci_probe(dev);
215 static int nexell_dwmmc_bind(struct udevice *dev)
217 struct nexell_mmc_plat *plat = dev_get_plat(dev);
219 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
222 static const struct udevice_id nexell_dwmmc_ids[] = {
223 { .compatible = "nexell,nexell-dwmmc" },
227 U_BOOT_DRIVER(nexell_dwmmc_drv) = {
228 .name = "nexell_dwmmc",
230 .of_match = nexell_dwmmc_ids,
231 .of_to_plat = nexell_dwmmc_of_to_plat,
232 .ops = &dm_dwmci_ops,
233 .bind = nexell_dwmmc_bind,
234 .probe = nexell_dwmmc_probe,
235 .priv_auto = sizeof(struct nexell_dwmmc_priv),
236 .plat_auto = sizeof(struct nexell_mmc_plat),