blob: 09a59fdf012009cc43c16cf230c701d143aad4a5 [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 Protsenko658a1b82024-08-07 22:14:27 -0500226#ifdef CONFIG_DM_MMC
227static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung959198f2014-05-16 13:59:52 +0900228{
Sam Protsenko658a1b82024-08-07 22:14:27 -0500229 const void *blob = gd->fdt_blob;
230 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
231 struct dwmci_host *host = &priv->host;
232 int node = dev_of_offset(dev);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900233 int err = 0;
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500234 u32 timing[3];
Jaehoon Chung959198f2014-05-16 13:59:52 +0900235
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500236#ifdef CONFIG_CPU_V7A
Jaehoon Chung959198f2014-05-16 13:59:52 +0900237 /* Extract device id for each mmc channel */
238 host->dev_id = pinmux_decode_periph_id(blob, node);
239
Jaehoon Chung959198f2014-05-16 13:59:52 +0900240 host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
241 if (host->dev_index == host->dev_id)
242 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
243
Jaehoon Chungce757b12016-06-29 19:46:16 +0900244 if (host->dev_index > 4) {
245 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
246 return -EINVAL;
247 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500248#else
249 if (dev_read_bool(dev, "non-removable"))
250 host->dev_index = 0; /* eMMC */
251 else
252 host->dev_index = 2; /* SD card */
253#endif
Jaehoon Chungce757b12016-06-29 19:46:16 +0900254
Jaehoon Chung70f6d392016-06-29 19:46:18 +0900255 /* Get the bus width from the device node (Default is 4bit buswidth) */
256 host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 4);
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900257
Jaehoon Chung959198f2014-05-16 13:59:52 +0900258 /* Set the base address from the device node */
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500259 host->ioaddr = dev_read_addr_ptr(dev);
260 if (!host->ioaddr) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900261 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900262 return -EINVAL;
263 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900264
265 /* Extract the timing info from the node */
266 err = fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
267 if (err) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900268 printf("DWMMC%d: Can't get sdr-timings for devider\n",
269 host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900270 return -EINVAL;
271 }
272
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900273 priv->sdr_timing = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
Jaehoon Chung959198f2014-05-16 13:59:52 +0900274 DWMCI_SET_DRV_CLK(timing[1]) |
275 DWMCI_SET_DIV_RATIO(timing[2]));
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900276
277 /* sdr_timing didn't assigned anything, use the default value */
278 if (!priv->sdr_timing) {
279 if (host->dev_index == 0)
280 priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
281 else if (host->dev_index == 2)
282 priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
283 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900284
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500285 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900286 host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
287 host->div = fdtdec_get_int(blob, node, "div", 0);
288
289 return 0;
290}
291
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900292static int exynos_dwmmc_probe(struct udevice *dev)
293{
Simon Glassc69cda22020-12-03 16:55:20 -0700294 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900295 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
296 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
297 struct dwmci_host *host = &priv->host;
298 int err;
299
Sam Protsenko8303fd62024-08-07 22:14:26 -0500300#ifndef CONFIG_CPU_V7A
301 err = clk_get_by_index(dev, 1, &priv->clk); /* ciu */
302 if (err)
303 return err;
304#endif
305
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900306 err = do_dwmci_init(host);
307 if (err)
308 return err;
309
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900310 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900311 host->mmc = &plat->mmc;
312 host->mmc->priv = &priv->host;
313 host->priv = dev;
314 upriv->mmc = host->mmc;
315
316 return dwmci_probe(dev);
317}
318
319static int exynos_dwmmc_bind(struct udevice *dev)
320{
Simon Glassc69cda22020-12-03 16:55:20 -0700321 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900322
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900323 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900324}
325
326static const struct udevice_id exynos_dwmmc_ids[] = {
327 { .compatible = "samsung,exynos4412-dw-mshc" },
Lukasz Majewski0acdb2c2018-08-01 14:49:00 +0200328 { .compatible = "samsung,exynos-dwmmc" },
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900329 { }
330};
331
332U_BOOT_DRIVER(exynos_dwmmc_drv) = {
333 .name = "exynos_dwmmc",
334 .id = UCLASS_MMC,
335 .of_match = exynos_dwmmc_ids,
Sam Protsenko658a1b82024-08-07 22:14:27 -0500336 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900337 .bind = exynos_dwmmc_bind,
338 .ops = &dm_dwmci_ops,
339 .probe = exynos_dwmmc_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700340 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700341 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900342};
343#endif