mmc: s5p_sdhci: add Tizen specific bind codes
[platform/kernel/u-boot.git] / drivers / mmc / exynos_dw_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2012 SAMSUNG Electronics
4  * Jaehoon Chung <jh80.chung@samsung.com>
5  */
6
7 #include <common.h>
8 #include <dwmmc.h>
9 #include <fdtdec.h>
10 #include <linux/libfdt.h>
11 #include <malloc.h>
12 #include <errno.h>
13 #include <asm/arch/dwmmc.h>
14 #include <asm/arch/clk.h>
15 #include <asm/arch/pinmux.h>
16 #include <asm/arch/power.h>
17 #ifndef CONFIG_TPL_TM2
18 #include <asm/gpio.h>
19 #endif
20
21 #define DWMMC_MAX_CH_NUM                4
22 #define DWMMC_MAX_FREQ                  52000000
23 #define DWMMC_MIN_FREQ                  400000
24 #define DWMMC_MMC0_SDR_TIMING_VAL       0x03030001
25 #define DWMMC_MMC2_SDR_TIMING_VAL       0x03020001
26
27 #ifdef CONFIG_DM_MMC
28 #include <dm.h>
29 DECLARE_GLOBAL_DATA_PTR;
30
31 struct exynos_mmc_plat {
32         struct mmc_config cfg;
33         struct mmc mmc;
34 };
35 #endif
36
37 /* Exynos implmentation specific drver private data */
38 struct dwmci_exynos_priv_data {
39 #ifdef CONFIG_DM_MMC
40         struct dwmci_host host;
41 #endif
42         u32 sdr_timing;
43 };
44
45 /*
46  * Function used as callback function to initialise the
47  * CLKSEL register for every mmc channel.
48  */
49 static void exynos_dwmci_clksel(struct dwmci_host *host)
50 {
51 #ifdef CONFIG_DM_MMC
52         struct dwmci_exynos_priv_data *priv =
53                 container_of(host, struct dwmci_exynos_priv_data, host);
54 #else
55         struct dwmci_exynos_priv_data *priv = host->priv;
56 #endif
57         dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
58 }
59
60 unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
61 {
62         unsigned long sclk;
63         int8_t clk_div;
64
65         /*
66          * Since SDCLKIN is divided inside controller by the DIVRATIO
67          * value set in the CLKSEL register, we need to use the same output
68          * clock value to calculate the CLKDIV value.
69          * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
70          */
71         clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
72                         & DWMCI_DIVRATIO_MASK) + 1;
73         sclk = get_mmc_clk(host->dev_index);
74
75         /*
76          * Assume to know divider value.
77          * When clock unit is broken, need to set "host->div"
78          */
79         return sclk / clk_div / (host->div + 1);
80 }
81
82 static void exynos_dwmci_board_init(struct dwmci_host *host)
83 {
84         struct dwmci_exynos_priv_data *priv = host->priv;
85
86         if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
87                 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
88                 dwmci_writel(host, EMMCP_SEND0, 0);
89                 dwmci_writel(host, EMMCP_CTRL0,
90                              MPSCTRL_SECURE_READ_BIT |
91                              MPSCTRL_SECURE_WRITE_BIT |
92                              MPSCTRL_NON_SECURE_READ_BIT |
93                              MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
94         }
95
96         /* Set to timing value at initial time */
97         if (priv->sdr_timing)
98                 exynos_dwmci_clksel(host);
99 }
100
101 static int exynos_dwmci_core_init(struct dwmci_host *host)
102 {
103         int addr_config;
104
105 #ifndef CONFIG_TPL_TM2
106         unsigned long freq, sclk;
107         unsigned int div;
108
109         if (host->bus_hz)
110                 freq = host->bus_hz;
111         else
112                 freq = DWMMC_MAX_FREQ;
113
114         /* request mmc clock vlaue of 52MHz.  */
115         sclk = get_mmc_clk(host->dev_index);
116         div = DIV_ROUND_UP(sclk, freq);
117         /* set the clock divisor for mmc */
118         set_mmc_clk(host->dev_index, div);
119 #endif
120
121         host->name = "EXYNOS DWMMC";
122 #ifdef CONFIG_EXYNOS5420
123         host->quirks = DWMCI_QUIRK_DISABLE_SMU;
124 #endif
125         host->board_init = exynos_dwmci_board_init;
126
127         host->caps = MMC_MODE_DDR_52MHz;
128         host->clksel = exynos_dwmci_clksel;
129         host->get_mmc_clk = exynos_dwmci_get_clk;
130
131         addr_config = DWMCI_GET_ADDR_CONFIG(dwmci_readl(host, DWMCI_HCON));
132         if (addr_config == 1)
133                 /* host supports IDMAC in 64-bit address mode */
134                 host->dma_64bit_address = 1;
135
136 #ifndef CONFIG_DM_MMC
137         /* Add the mmc channel to be registered with mmc core */
138         if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
139                 printf("DWMMC%d registration failed\n", host->dev_index);
140                 return -1;
141         }
142 #endif
143         return 0;
144 }
145
146 static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
147
148 static int do_dwmci_init(struct dwmci_host *host)
149 {
150 #ifndef CONFIG_TPL_TM2
151         int flag, err;
152
153         flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
154         err = exynos_pinmux_config(host->dev_id, flag);
155         if (err) {
156                 printf("DWMMC%d not configure\n", host->dev_index);
157                 return err;
158         }
159 #endif
160         return exynos_dwmci_core_init(host);
161 }
162
163 static int exynos_dwmci_get_config(const void *blob, int node,
164                                    struct dwmci_host *host,
165                                    struct dwmci_exynos_priv_data *priv)
166 {
167         int err = 0;
168         u32 timing[3];
169         dma_addr_t base;
170
171         /* Extract device id for each mmc channel */
172 #ifndef CONFIG_TPL_TM2
173         host->dev_id = pinmux_decode_periph_id(blob, node);
174 #else
175         host->dev_id = 0;
176 #endif
177         host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
178         if (host->dev_index == host->dev_id)
179                 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
180
181         if (host->dev_index > 4) {
182                 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
183                 return -EINVAL;
184         }
185
186         /* Get the bus width from the device node (Default is 4bit buswidth) */
187         host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 4);
188
189         /* Set the base address from the device node */
190         base = fdtdec_get_addr(blob, node, "reg");
191         if (!base) {
192                 printf("DWMMC%d: Can't get base address\n", host->dev_index);
193                 return -EINVAL;
194         }
195         host->ioaddr = (void *)base;
196
197         /* Extract the timing info from the node */
198         err =  fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
199         if (err) {
200                 printf("DWMMC%d: Can't get sdr-timings for devider\n",
201                                 host->dev_index);
202                 return -EINVAL;
203         }
204
205         priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
206                         DWMCI_SET_DRV_CLK(timing[1]) |
207                         DWMCI_SET_DIV_RATIO(timing[2]));
208
209         /* sdr_timing didn't assigned anything, use the default value */
210         if (!priv->sdr_timing) {
211                 if (host->dev_index == 0)
212                         priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
213                 else if (host->dev_index == 2)
214                         priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
215         }
216
217         host->fifoth_val = fdtdec_get_int(blob, node, "fifoth_val", 0);
218         host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
219         host->div = fdtdec_get_int(blob, node, "div", 0);
220
221         return 0;
222 }
223
224 static int exynos_dwmci_process_node(const void *blob,
225                                         int node_list[], int count)
226 {
227         struct dwmci_exynos_priv_data *priv;
228         struct dwmci_host *host;
229         int i, node, err;
230
231         for (i = 0; i < count; i++) {
232                 node = node_list[i];
233                 if (node <= 0)
234                         continue;
235                 host = &dwmci_host[i];
236
237                 priv = malloc(sizeof(struct dwmci_exynos_priv_data));
238                 if (!priv) {
239                         pr_err("dwmci_exynos_priv_data malloc fail!\n");
240                         return -ENOMEM;
241                 }
242
243                 err = exynos_dwmci_get_config(blob, node, host, priv);
244                 if (err) {
245                         printf("%s: failed to decode dev %d\n", __func__, i);
246                         free(priv);
247                         return err;
248                 }
249                 host->priv = priv;
250
251                 do_dwmci_init(host);
252         }
253         return 0;
254 }
255
256 int exynos_dwmmc_init(const void *blob)
257 {
258         int node_list[DWMMC_MAX_CH_NUM];
259         int err = 0, count;
260
261         count = fdtdec_find_aliases_for_id(blob, "mmc",
262                         COMPAT_SAMSUNG_EXYNOS_DWMMC, node_list,
263                         DWMMC_MAX_CH_NUM);
264
265 #ifndef CONFIG_TPL_TM2
266         int boot_dev_node;
267         /* For DWMMC always set boot device as mmc 0 */
268         if (count >= 3 && get_boot_mode() == BOOT_MODE_SD) {
269                 boot_dev_node = node_list[2];
270                 node_list[2] = node_list[0];
271                 node_list[0] = boot_dev_node;
272         }
273 #endif
274         err = exynos_dwmci_process_node(blob, node_list, count);
275
276         return err;
277 }
278
279 #ifdef CONFIG_DM_MMC
280 static int exynos_dwmmc_probe(struct udevice *dev)
281 {
282         struct exynos_mmc_plat *plat = dev_get_platdata(dev);
283         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
284         struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
285         struct dwmci_host *host = &priv->host;
286         int err;
287
288         err = exynos_dwmci_get_config(gd->fdt_blob, dev_of_offset(dev), host,
289                                       priv);
290         if (err)
291                 return err;
292         err = do_dwmci_init(host);
293         if (err)
294                 return err;
295
296         dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
297         host->mmc = &plat->mmc;
298         host->mmc->priv = &priv->host;
299         host->priv = dev;
300         upriv->mmc = host->mmc;
301
302         return dwmci_probe(dev);
303 }
304
305 static int exynos_dwmmc_bind(struct udevice *dev)
306 {
307         struct exynos_mmc_plat *plat = dev_get_platdata(dev);
308 #if defined(CONFIG_TIZEN_XU3) || defined(CONFIG_TARGET_TIZEN)
309         struct blk_desc *bdesc;
310         struct udevice *bdev;
311         int ret, devnum = -1;
312
313         ret = dev_read_alias_seq(dev, &devnum);
314         if (get_boot_mode() == BOOT_MODE_SD) {
315                 if (devnum == 0)
316                         devnum = 1;
317                 else if (devnum == 2)
318                         devnum = 0;
319         }
320
321         ret = blk_create_devicef(dev, "mmc_blk", "blk", IF_TYPE_MMC,
322                         devnum, 512, 0, &bdev);
323         if (ret) {
324                 debug("Cannot create block device\n");
325                 return ret;
326         }
327         bdesc = dev_get_uclass_platdata(bdev);
328         plat->mmc.cfg = &plat->cfg;
329         plat->mmc.priv = dev;
330
331         /* the following chunk was from mmc_register() */
332
333         /* Setup dsr related values */
334         plat->mmc.dsr_imp = 0;
335         plat->mmc.dsr = 0xffffffff;
336         /* Setup the universal parts of the block interface just once */
337         bdesc->removable = 1;
338
339         /* setup initial part type */
340         bdesc->part_type = plat->cfg.part_type;
341         plat->mmc.dev = dev;
342         return 0;
343 #else
344         return dwmci_bind(dev, &plat->mmc, &plat->cfg);
345 #endif
346 }
347
348 static const struct udevice_id exynos_dwmmc_ids[] = {
349         { .compatible = "samsung,exynos4412-dw-mshc" },
350         { .compatible = "samsung,exynos-dwmmc" },
351         { }
352 };
353
354 U_BOOT_DRIVER(exynos_dwmmc_drv) = {
355         .name           = "exynos_dwmmc",
356         .id             = UCLASS_MMC,
357         .of_match       = exynos_dwmmc_ids,
358         .bind           = exynos_dwmmc_bind,
359         .ops            = &dm_dwmci_ops,
360         .probe          = exynos_dwmmc_probe,
361         .priv_auto_alloc_size   = sizeof(struct dwmci_exynos_priv_data),
362         .platdata_auto_alloc_size = sizeof(struct exynos_mmc_plat),
363 };
364 #endif