blob: 4d28ede0d75680d736d5c534f7414ea8fb7dd884 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +00005 */
6
Sam Protsenko8303fd62024-08-07 22:14:26 -05007#include <clk.h>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +00008#include <dwmmc.h>
Amara082a2d2013-04-27 11:42:55 +05309#include <fdtdec.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Amara082a2d2013-04-27 11:42:55 +053012#include <malloc.h>
Jaehoon Chungccd60a82016-07-19 16:33:34 +090013#include <errno.h>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000014#include <asm/arch/dwmmc.h>
15#include <asm/arch/clk.h>
Amara082a2d2013-04-27 11:42:55 +053016#include <asm/arch/pinmux.h>
Przemyslaw Marczak64029f72015-02-20 12:29:26 +010017#include <asm/arch/power.h>
Jaehoon Chung959198f2014-05-16 13:59:52 +090018#include <asm/gpio.h>
Sam Protsenko8303fd62024-08-07 22:14:26 -050019#include <linux/err.h>
Simon Glass1e94b462023-09-14 18:21:46 -060020#include <linux/printk.h>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000021
Amara082a2d2013-04-27 11:42:55 +053022#define DWMMC_MAX_CH_NUM 4
23#define DWMMC_MAX_FREQ 52000000
24#define DWMMC_MIN_FREQ 400000
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090025#define DWMMC_MMC0_SDR_TIMING_VAL 0x03030001
26#define DWMMC_MMC2_SDR_TIMING_VAL 0x03020001
27
Jaehoon Chung3537ee82016-06-30 20:57:37 +090028#ifdef CONFIG_DM_MMC
29#include <dm.h>
30DECLARE_GLOBAL_DATA_PTR;
31
32struct exynos_mmc_plat {
33 struct mmc_config cfg;
34 struct mmc mmc;
35};
36#endif
37
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090038/* Exynos implmentation specific drver private data */
39struct dwmci_exynos_priv_data {
Jaehoon Chung3537ee82016-06-30 20:57:37 +090040#ifdef CONFIG_DM_MMC
41 struct dwmci_host host;
42#endif
Sam Protsenko8303fd62024-08-07 22:14:26 -050043 struct clk clk;
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090044 u32 sdr_timing;
45};
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000046
Sam Protsenkoc61f92e2024-08-07 22:14:24 -050047static struct dwmci_exynos_priv_data *exynos_dwmmc_get_priv(
48 struct dwmci_host *host)
49{
50#ifdef CONFIG_DM_MMC
51 return container_of(host, struct dwmci_exynos_priv_data, host);
52#else
53 return host->priv;
54#endif
55}
56
Sam Protsenko8303fd62024-08-07 22:14:26 -050057/**
58 * exynos_dwmmc_get_sclk - Get source clock (SDCLKIN) rate
59 * @host: MMC controller object
60 * @rate: Will contain clock rate, Hz
61 *
62 * Return: 0 on success or negative value on error
63 */
64static int exynos_dwmmc_get_sclk(struct dwmci_host *host, unsigned long *rate)
65{
66#ifdef CONFIG_CPU_V7A
67 *rate = get_mmc_clk(host->dev_index);
68#else
69 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
70
71 *rate = clk_get_rate(&priv->clk);
72#endif
73
74 if (IS_ERR_VALUE(*rate))
75 return *rate;
76
77 return 0;
78}
79
80/**
81 * exynos_dwmmc_set_sclk - Set source clock (SDCLKIN) rate
82 * @host: MMC controller object
83 * @rate: Desired clock rate, Hz
84 *
85 * Return: 0 on success or negative value on error
86 */
87static int exynos_dwmmc_set_sclk(struct dwmci_host *host, unsigned long rate)
88{
89 int err;
90
91#ifdef CONFIG_CPU_V7A
92 unsigned long sclk;
93 unsigned int div;
94
95 err = exynos_dwmmc_get_sclk(host, &sclk);
96 if (err)
97 return err;
98
99 div = DIV_ROUND_UP(sclk, rate);
100 set_mmc_clk(host->dev_index, div);
101#else
102 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
103
104 err = clk_set_rate(&priv->clk, rate);
105 if (err < 0)
106 return err;
107#endif
108
109 return 0;
110}
111
Amara082a2d2013-04-27 11:42:55 +0530112/*
113 * Function used as callback function to initialise the
114 * CLKSEL register for every mmc channel.
115 */
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800116static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000117{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500118 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
119
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900120 dwmci_writel(host, DWMCI_CLKSEL, priv->sdr_timing);
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800121
122 return 0;
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000123}
124
Simon Glasse3563f22015-08-30 16:55:15 -0600125unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
Amara082a2d2013-04-27 11:42:55 +0530126{
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530127 unsigned long sclk;
128 int8_t clk_div;
Sam Protsenko8303fd62024-08-07 22:14:26 -0500129 int err;
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530130
131 /*
132 * Since SDCLKIN is divided inside controller by the DIVRATIO
133 * value set in the CLKSEL register, we need to use the same output
134 * clock value to calculate the CLKDIV value.
135 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
136 */
137 clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
138 & DWMCI_DIVRATIO_MASK) + 1;
Sam Protsenko8303fd62024-08-07 22:14:26 -0500139
140 err = exynos_dwmmc_get_sclk(host, &sclk);
141 if (err) {
142 printf("DWMMC%d: failed to get clock rate (%d)\n",
143 host->dev_index, err);
144 return 0;
145 }
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530146
Jaehoon Chung959198f2014-05-16 13:59:52 +0900147 /*
148 * Assume to know divider value.
149 * When clock unit is broken, need to set "host->div"
150 */
151 return sclk / clk_div / (host->div + 1);
Amara082a2d2013-04-27 11:42:55 +0530152}
153
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900154static void exynos_dwmci_board_init(struct dwmci_host *host)
155{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500156 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900157
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900158 if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
159 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
160 dwmci_writel(host, EMMCP_SEND0, 0);
161 dwmci_writel(host, EMMCP_CTRL0,
162 MPSCTRL_SECURE_READ_BIT |
163 MPSCTRL_SECURE_WRITE_BIT |
164 MPSCTRL_NON_SECURE_READ_BIT |
165 MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
166 }
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900167
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900168 /* Set to timing value at initial time */
169 if (priv->sdr_timing)
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900170 exynos_dwmci_clksel(host);
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900171}
172
Jaehoon Chungd956a672016-06-29 19:46:17 +0900173static int exynos_dwmci_core_init(struct dwmci_host *host)
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000174{
Sam Protsenko8303fd62024-08-07 22:14:26 -0500175 unsigned long freq;
176 int err;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900177
178 if (host->bus_hz)
179 freq = host->bus_hz;
180 else
181 freq = DWMMC_MAX_FREQ;
182
Sam Protsenko8303fd62024-08-07 22:14:26 -0500183 err = exynos_dwmmc_set_sclk(host, freq);
184 if (err) {
185 printf("DWMMC%d: failed to set clock rate on probe (%d); "
186 "continue anyway\n", host->dev_index, err);
187 }
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000188
Amara082a2d2013-04-27 11:42:55 +0530189 host->name = "EXYNOS DWMMC";
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530190#ifdef CONFIG_EXYNOS5420
191 host->quirks = DWMCI_QUIRK_DISABLE_SMU;
192#endif
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900193 host->board_init = exynos_dwmci_board_init;
Amara082a2d2013-04-27 11:42:55 +0530194
Jaehoon Chunge09bd852014-05-16 13:59:57 +0900195 host->caps = MMC_MODE_DDR_52MHz;
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000196 host->clksel = exynos_dwmci_clksel;
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900197 host->get_mmc_clk = exynos_dwmci_get_clk;
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900198
199#ifndef CONFIG_DM_MMC
Amara082a2d2013-04-27 11:42:55 +0530200 /* Add the mmc channel to be registered with mmc core */
201 if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
Jaehoon Chungd956a672016-06-29 19:46:17 +0900202 printf("DWMMC%d registration failed\n", host->dev_index);
Amara082a2d2013-04-27 11:42:55 +0530203 return -1;
204 }
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900205#endif
206
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000207 return 0;
208}
209
Jaehoon Chung959198f2014-05-16 13:59:52 +0900210static int do_dwmci_init(struct dwmci_host *host)
211{
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500212#ifdef CONFIG_CPU_V7A
Jaehoon Chungd956a672016-06-29 19:46:17 +0900213 int flag, err;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900214
215 flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
216 err = exynos_pinmux_config(host->dev_id, flag);
217 if (err) {
Jaehoon Chungd956a672016-06-29 19:46:17 +0900218 printf("DWMMC%d not configure\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900219 return err;
220 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500221#endif
Jaehoon Chung959198f2014-05-16 13:59:52 +0900222
Jaehoon Chungd956a672016-06-29 19:46:17 +0900223 return exynos_dwmci_core_init(host);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900224}
225
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500226static int exynos_dwmci_get_config(struct udevice *dev, const void *blob,
227 int node, struct dwmci_host *host,
Lukasz Majewskib88c1ef2018-08-01 14:48:53 +0200228 struct dwmci_exynos_priv_data *priv)
Jaehoon Chung959198f2014-05-16 13:59:52 +0900229{
230 int err = 0;
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500231 u32 timing[3];
Jaehoon Chung959198f2014-05-16 13:59:52 +0900232
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500233#ifdef CONFIG_CPU_V7A
Jaehoon Chung959198f2014-05-16 13:59:52 +0900234 /* Extract device id for each mmc channel */
235 host->dev_id = pinmux_decode_periph_id(blob, node);
236
Jaehoon Chung959198f2014-05-16 13:59:52 +0900237 host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
238 if (host->dev_index == host->dev_id)
239 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
240
Jaehoon Chungce757b12016-06-29 19:46:16 +0900241 if (host->dev_index > 4) {
242 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
243 return -EINVAL;
244 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500245#else
246 if (dev_read_bool(dev, "non-removable"))
247 host->dev_index = 0; /* eMMC */
248 else
249 host->dev_index = 2; /* SD card */
250#endif
Jaehoon Chungce757b12016-06-29 19:46:16 +0900251
Jaehoon Chung70f6d392016-06-29 19:46:18 +0900252 /* Get the bus width from the device node (Default is 4bit buswidth) */
253 host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 4);
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900254
Jaehoon Chung959198f2014-05-16 13:59:52 +0900255 /* Set the base address from the device node */
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500256 host->ioaddr = dev_read_addr_ptr(dev);
257 if (!host->ioaddr) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900258 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900259 return -EINVAL;
260 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900261
262 /* Extract the timing info from the node */
263 err = fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
264 if (err) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900265 printf("DWMMC%d: Can't get sdr-timings for devider\n",
266 host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900267 return -EINVAL;
268 }
269
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900270 priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
Jaehoon Chung959198f2014-05-16 13:59:52 +0900271 DWMCI_SET_DRV_CLK(timing[1]) |
272 DWMCI_SET_DIV_RATIO(timing[2]));
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900273
274 /* sdr_timing didn't assigned anything, use the default value */
275 if (!priv->sdr_timing) {
276 if (host->dev_index == 0)
277 priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
278 else if (host->dev_index == 2)
279 priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
280 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900281
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500282 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900283 host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
284 host->div = fdtdec_get_int(blob, node, "div", 0);
285
286 return 0;
287}
288
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900289#ifdef CONFIG_DM_MMC
290static int exynos_dwmmc_probe(struct udevice *dev)
291{
Simon Glassc69cda22020-12-03 16:55:20 -0700292 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900293 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
294 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
295 struct dwmci_host *host = &priv->host;
296 int err;
297
Sam Protsenko8303fd62024-08-07 22:14:26 -0500298#ifndef CONFIG_CPU_V7A
299 err = clk_get_by_index(dev, 1, &priv->clk); /* ciu */
300 if (err)
301 return err;
302#endif
303
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500304 err = exynos_dwmci_get_config(dev, gd->fdt_blob, dev_of_offset(dev),
305 host, priv);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900306 if (err)
307 return err;
308 err = do_dwmci_init(host);
309 if (err)
310 return err;
311
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900312 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900313 host->mmc = &plat->mmc;
314 host->mmc->priv = &priv->host;
315 host->priv = dev;
316 upriv->mmc = host->mmc;
317
318 return dwmci_probe(dev);
319}
320
321static int exynos_dwmmc_bind(struct udevice *dev)
322{
Simon Glassc69cda22020-12-03 16:55:20 -0700323 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900324
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900325 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900326}
327
328static const struct udevice_id exynos_dwmmc_ids[] = {
329 { .compatible = "samsung,exynos4412-dw-mshc" },
Lukasz Majewski0acdb2c2018-08-01 14:49:00 +0200330 { .compatible = "samsung,exynos-dwmmc" },
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900331 { }
332};
333
334U_BOOT_DRIVER(exynos_dwmmc_drv) = {
335 .name = "exynos_dwmmc",
336 .id = UCLASS_MMC,
337 .of_match = exynos_dwmmc_ids,
338 .bind = exynos_dwmmc_bind,
339 .ops = &dm_dwmci_ops,
340 .probe = exynos_dwmmc_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700341 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700342 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900343};
344#endif