blob: 50d0208b29fd4aaad3bc393c157d2fd10a29667b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung7aff9672012-10-15 19:10:31 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
Jaehoon Chung7aff9672012-10-15 19:10:31 +00005 */
6
Sam Protsenko57ddb372024-08-07 22:14:26 -05007#include <clk.h>
Jaehoon Chung7aff9672012-10-15 19:10:31 +00008#include <dwmmc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Amard8501212013-04-27 11:42:55 +053010#include <malloc.h>
Jaehoon Chungedd9d1dc2016-07-19 16:33:34 +090011#include <errno.h>
Jaehoon Chung7aff9672012-10-15 19:10:31 +000012#include <asm/arch/dwmmc.h>
13#include <asm/arch/clk.h>
Amard8501212013-04-27 11:42:55 +053014#include <asm/arch/pinmux.h>
Przemyslaw Marczakc3885b82015-02-20 12:29:26 +010015#include <asm/arch/power.h>
Jaehoon Chung62811102014-05-16 13:59:52 +090016#include <asm/gpio.h>
Sam Protsenko57ddb372024-08-07 22:14:26 -050017#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060018#include <linux/printk.h>
Jaehoon Chung7aff9672012-10-15 19:10:31 +000019
Amard8501212013-04-27 11:42:55 +053020#define DWMMC_MAX_CH_NUM 4
21#define DWMMC_MAX_FREQ 52000000
22#define DWMMC_MIN_FREQ 400000
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090023#define DWMMC_MMC0_SDR_TIMING_VAL 0x03030001
24#define DWMMC_MMC2_SDR_TIMING_VAL 0x03020001
25
Jaehoon Chung98d18e92016-06-30 20:57:37 +090026#ifdef CONFIG_DM_MMC
27#include <dm.h>
28DECLARE_GLOBAL_DATA_PTR;
29
30struct exynos_mmc_plat {
31 struct mmc_config cfg;
32 struct mmc mmc;
33};
34#endif
35
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090036/* Exynos implmentation specific drver private data */
37struct dwmci_exynos_priv_data {
Jaehoon Chung98d18e92016-06-30 20:57:37 +090038#ifdef CONFIG_DM_MMC
39 struct dwmci_host host;
40#endif
Sam Protsenko57ddb372024-08-07 22:14:26 -050041 struct clk clk;
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090042 u32 sdr_timing;
43};
Jaehoon Chung7aff9672012-10-15 19:10:31 +000044
Sam Protsenko3192a642024-08-07 22:14:24 -050045static struct dwmci_exynos_priv_data *exynos_dwmmc_get_priv(
46 struct dwmci_host *host)
47{
48#ifdef CONFIG_DM_MMC
49 return container_of(host, struct dwmci_exynos_priv_data, host);
50#else
51 return host->priv;
52#endif
53}
54
Sam Protsenko57ddb372024-08-07 22:14:26 -050055/**
56 * exynos_dwmmc_get_sclk - Get source clock (SDCLKIN) rate
57 * @host: MMC controller object
58 * @rate: Will contain clock rate, Hz
59 *
60 * Return: 0 on success or negative value on error
61 */
62static int exynos_dwmmc_get_sclk(struct dwmci_host *host, unsigned long *rate)
63{
64#ifdef CONFIG_CPU_V7A
65 *rate = get_mmc_clk(host->dev_index);
66#else
67 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
68
69 *rate = clk_get_rate(&priv->clk);
70#endif
71
72 if (IS_ERR_VALUE(*rate))
73 return *rate;
74
75 return 0;
76}
77
78/**
79 * exynos_dwmmc_set_sclk - Set source clock (SDCLKIN) rate
80 * @host: MMC controller object
81 * @rate: Desired clock rate, Hz
82 *
83 * Return: 0 on success or negative value on error
84 */
85static int exynos_dwmmc_set_sclk(struct dwmci_host *host, unsigned long rate)
86{
87 int err;
88
89#ifdef CONFIG_CPU_V7A
90 unsigned long sclk;
91 unsigned int div;
92
93 err = exynos_dwmmc_get_sclk(host, &sclk);
94 if (err)
95 return err;
96
97 div = DIV_ROUND_UP(sclk, rate);
98 set_mmc_clk(host->dev_index, div);
99#else
100 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
101
102 err = clk_set_rate(&priv->clk, rate);
103 if (err < 0)
104 return err;
105#endif
106
107 return 0;
108}
109
Amard8501212013-04-27 11:42:55 +0530110/*
111 * Function used as callback function to initialise the
112 * CLKSEL register for every mmc channel.
113 */
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800114static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000115{
Sam Protsenko3192a642024-08-07 22:14:24 -0500116 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
117
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900118 dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800119
120 return 0;
Amard8501212013-04-27 11:42:55 +0530121}
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000122
Simon Glasseff76682015-08-30 16:55:15 -0600123unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
Amard8501212013-04-27 11:42:55 +0530124{
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530125 unsigned long sclk;
126 int8_t clk_div;
Sam Protsenko57ddb372024-08-07 22:14:26 -0500127 int err;
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530128
129 /*
130 * Since SDCLKIN is divided inside controller by the DIVRATIO
131 * value set in the CLKSEL register, we need to use the same output
132 * clock value to calculate the CLKDIV value.
133 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
134 */
135 clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
136 & DWMCI_DIVRATIO_MASK) + 1;
Sam Protsenko57ddb372024-08-07 22:14:26 -0500137
138 err = exynos_dwmmc_get_sclk(host, &sclk);
139 if (err) {
140 printf("DWMMC%d: failed to get clock rate (%d)\n",
141 host->dev_index, err);
142 return 0;
143 }
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530144
Jaehoon Chung62811102014-05-16 13:59:52 +0900145 /*
146 * Assume to know divider value.
147 * When clock unit is broken, need to set "host->div"
148 */
149 return sclk / clk_div / (host->div + 1);
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000150}
151
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900152static void exynos_dwmci_board_init(struct dwmci_host *host)
153{
Sam Protsenko3192a642024-08-07 22:14:24 -0500154 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900155
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900156 if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
157 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
158 dwmci_writel(host, EMMCP_SEND0, 0);
159 dwmci_writel(host, EMMCP_CTRL0,
160 MPSCTRL_SECURE_READ_BIT |
161 MPSCTRL_SECURE_WRITE_BIT |
162 MPSCTRL_NON_SECURE_READ_BIT |
163 MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
164 }
Jaehoon Chung3d12e552015-02-04 15:48:39 +0900165
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900166 /* Set to timing value at initial time */
167 if (priv->sdr_timing)
Jaehoon Chung3d12e552015-02-04 15:48:39 +0900168 exynos_dwmci_clksel(host);
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900169}
170
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900171static int exynos_dwmci_core_init(struct dwmci_host *host)
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000172{
Sam Protsenko57ddb372024-08-07 22:14:26 -0500173 unsigned long freq;
174 int err;
Jaehoon Chung62811102014-05-16 13:59:52 +0900175
176 if (host->bus_hz)
177 freq = host->bus_hz;
178 else
179 freq = DWMMC_MAX_FREQ;
180
Sam Protsenko57ddb372024-08-07 22:14:26 -0500181 err = exynos_dwmmc_set_sclk(host, freq);
182 if (err) {
183 printf("DWMMC%d: failed to set clock rate on probe (%d); "
184 "continue anyway\n", host->dev_index, err);
185 }
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000186
Amard8501212013-04-27 11:42:55 +0530187 host->name = "EXYNOS DWMMC";
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530188#ifdef CONFIG_EXYNOS5420
189 host->quirks = DWMCI_QUIRK_DISABLE_SMU;
190#endif
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900191 host->board_init = exynos_dwmci_board_init;
Amard8501212013-04-27 11:42:55 +0530192
Jaehoon Chungef91dd52014-05-16 13:59:57 +0900193 host->caps = MMC_MODE_DDR_52MHz;
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000194 host->clksel = exynos_dwmci_clksel;
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900195 host->get_mmc_clk = exynos_dwmci_get_clk;
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900196
197#ifndef CONFIG_DM_MMC
Amard8501212013-04-27 11:42:55 +0530198 /* Add the mmc channel to be registered with mmc core */
199 if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900200 printf("DWMMC%d registration failed\n", host->dev_index);
Amard8501212013-04-27 11:42:55 +0530201 return -1;
202 }
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900203#endif
204
Amard8501212013-04-27 11:42:55 +0530205 return 0;
206}
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000207
Jaehoon Chung62811102014-05-16 13:59:52 +0900208static int do_dwmci_init(struct dwmci_host *host)
Amard8501212013-04-27 11:42:55 +0530209{
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500210#ifdef CONFIG_CPU_V7A
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900211 int flag, err;
Amard8501212013-04-27 11:42:55 +0530212
Jaehoon Chung62811102014-05-16 13:59:52 +0900213 flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
214 err = exynos_pinmux_config(host->dev_id, flag);
215 if (err) {
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900216 printf("DWMMC%d not configure\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900217 return err;
218 }
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500219#endif
Amard8501212013-04-27 11:42:55 +0530220
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900221 return exynos_dwmci_core_init(host);
Jaehoon Chung62811102014-05-16 13:59:52 +0900222}
Amard8501212013-04-27 11:42:55 +0530223
Sam Protsenkof78333a2024-08-07 22:14:27 -0500224#ifdef CONFIG_DM_MMC
225static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung62811102014-05-16 13:59:52 +0900226{
Sam Protsenkof78333a2024-08-07 22:14:27 -0500227 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
228 struct dwmci_host *host = &priv->host;
Jaehoon Chung62811102014-05-16 13:59:52 +0900229 int err = 0;
Sam Protsenko745edd62024-08-07 22:14:23 -0500230 u32 timing[3];
Amard8501212013-04-27 11:42:55 +0530231
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500232#ifdef CONFIG_CPU_V7A
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500233 const void *blob = gd->fdt_blob;
234 int node = dev_of_offset(dev);
235
Jaehoon Chung62811102014-05-16 13:59:52 +0900236 /* Extract device id for each mmc channel */
237 host->dev_id = pinmux_decode_periph_id(blob, node);
Amard8501212013-04-27 11:42:55 +0530238
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500239 host->dev_index = dev_read_u32_default(dev, "index", host->dev_id);
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900240 if (host->dev_index == host->dev_id)
241 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
242
Jaehoon Chunge0303c72016-06-29 19:46:16 +0900243 if (host->dev_index > 4) {
244 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
245 return -EINVAL;
246 }
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500247#else
248 if (dev_read_bool(dev, "non-removable"))
249 host->dev_index = 0; /* eMMC */
250 else
251 host->dev_index = 2; /* SD card */
252#endif
Jaehoon Chunge0303c72016-06-29 19:46:16 +0900253
Jaehoon Chung865ecd92016-06-29 19:46:18 +0900254 /* Get the bus width from the device node (Default is 4bit buswidth) */
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500255 host->buswidth = dev_read_u32_default(dev, "samsung,bus-width", 4);
Amard8501212013-04-27 11:42:55 +0530256
Jaehoon Chung62811102014-05-16 13:59:52 +0900257 /* Set the base address from the device node */
Sam Protsenko745edd62024-08-07 22:14:23 -0500258 host->ioaddr = dev_read_addr_ptr(dev);
259 if (!host->ioaddr) {
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900260 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900261 return -EINVAL;
262 }
Jaehoon Chung62811102014-05-16 13:59:52 +0900263
264 /* Extract the timing info from the node */
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500265 err = dev_read_u32_array(dev, "samsung,timing", timing, 3);
Jaehoon Chung62811102014-05-16 13:59:52 +0900266 if (err) {
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900267 printf("DWMMC%d: Can't get sdr-timings for devider\n",
268 host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900269 return -EINVAL;
270 }
271
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900272 priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
Jaehoon Chung62811102014-05-16 13:59:52 +0900273 DWMCI_SET_DRV_CLK(timing[1]) |
274 DWMCI_SET_DIV_RATIO(timing[2]));
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900275
276 /* sdr_timing didn't assigned anything, use the default value */
277 if (!priv->sdr_timing) {
278 if (host->dev_index == 0)
279 priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
280 else if (host->dev_index == 2)
281 priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
282 }
Jaehoon Chung62811102014-05-16 13:59:52 +0900283
Sam Protsenko751fdf12024-08-07 22:14:17 -0500284 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500285 host->bus_hz = dev_read_u32_default(dev, "bus_hz", 0);
286 host->div = dev_read_u32_default(dev, "div", 0);
Jaehoon Chung62811102014-05-16 13:59:52 +0900287
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000288 return 0;
289}
Jaehoon Chung62811102014-05-16 13:59:52 +0900290
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900291static int exynos_dwmmc_probe(struct udevice *dev)
292{
Simon Glassfa20e932020-12-03 16:55:20 -0700293 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900294 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
295 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
296 struct dwmci_host *host = &priv->host;
297 int err;
298
Sam Protsenko57ddb372024-08-07 22:14:26 -0500299#ifndef CONFIG_CPU_V7A
300 err = clk_get_by_index(dev, 1, &priv->clk); /* ciu */
301 if (err)
302 return err;
303#endif
304
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900305 err = do_dwmci_init(host);
306 if (err)
307 return err;
308
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900309 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900310 host->mmc = &plat->mmc;
311 host->mmc->priv = &priv->host;
312 host->priv = dev;
313 upriv->mmc = host->mmc;
314
315 return dwmci_probe(dev);
316}
317
318static int exynos_dwmmc_bind(struct udevice *dev)
319{
Simon Glassfa20e932020-12-03 16:55:20 -0700320 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900321
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900322 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900323}
324
325static const struct udevice_id exynos_dwmmc_ids[] = {
326 { .compatible = "samsung,exynos4412-dw-mshc" },
Lukasz Majewski03cf3af2018-08-01 14:49:00 +0200327 { .compatible = "samsung,exynos-dwmmc" },
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900328 { }
329};
330
331U_BOOT_DRIVER(exynos_dwmmc_drv) = {
332 .name = "exynos_dwmmc",
333 .id = UCLASS_MMC,
334 .of_match = exynos_dwmmc_ids,
Sam Protsenkof78333a2024-08-07 22:14:27 -0500335 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900336 .bind = exynos_dwmmc_bind,
337 .ops = &dm_dwmci_ops,
338 .probe = exynos_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700339 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700340 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900341};
342#endif