blob: b5c8f592c7e85f6ecad1f2ec8eb22f71c68f226e [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>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Amara082a2d2013-04-27 11:42:55 +053010#include <malloc.h>
Jaehoon Chungccd60a82016-07-19 16:33:34 +090011#include <errno.h>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000012#include <asm/arch/dwmmc.h>
13#include <asm/arch/clk.h>
Amara082a2d2013-04-27 11:42:55 +053014#include <asm/arch/pinmux.h>
Przemyslaw Marczak64029f72015-02-20 12:29:26 +010015#include <asm/arch/power.h>
Jaehoon Chung959198f2014-05-16 13:59:52 +090016#include <asm/gpio.h>
Sam Protsenko8303fd62024-08-07 22:14:26 -050017#include <linux/err.h>
Simon Glass1e94b462023-09-14 18:21:46 -060018#include <linux/printk.h>
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000019
Amara082a2d2013-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 Chung5dab81c2015-02-04 15:48:40 +090023#define DWMMC_MMC0_SDR_TIMING_VAL 0x03030001
24#define DWMMC_MMC2_SDR_TIMING_VAL 0x03020001
25
Sam Protsenko56ba9452024-08-07 22:14:31 -050026#define EXYNOS4412_FIXED_CIU_CLK_DIV 4
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
Sam Protsenkoa95b7262024-08-07 22:14:30 -050038/* Chip specific data */
39struct exynos_dwmmc_variant {
40 u32 clksel; /* CLKSEL register offset */
Sam Protsenko56ba9452024-08-07 22:14:31 -050041 u8 div; /* (optional) fixed clock divider value: 0..7 */
Sam Protsenkoa95b7262024-08-07 22:14:30 -050042};
43
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090044/* Exynos implmentation specific drver private data */
45struct dwmci_exynos_priv_data {
Jaehoon Chung3537ee82016-06-30 20:57:37 +090046#ifdef CONFIG_DM_MMC
47 struct dwmci_host host;
48#endif
Sam Protsenko8303fd62024-08-07 22:14:26 -050049 struct clk clk;
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090050 u32 sdr_timing;
Sam Protsenkoa95b7262024-08-07 22:14:30 -050051 const struct exynos_dwmmc_variant *chip;
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090052};
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000053
Sam Protsenkoc61f92e2024-08-07 22:14:24 -050054static struct dwmci_exynos_priv_data *exynos_dwmmc_get_priv(
55 struct dwmci_host *host)
56{
57#ifdef CONFIG_DM_MMC
58 return container_of(host, struct dwmci_exynos_priv_data, host);
59#else
60 return host->priv;
61#endif
62}
63
Sam Protsenko8303fd62024-08-07 22:14:26 -050064/**
65 * exynos_dwmmc_get_sclk - Get source clock (SDCLKIN) rate
66 * @host: MMC controller object
67 * @rate: Will contain clock rate, Hz
68 *
69 * Return: 0 on success or negative value on error
70 */
71static int exynos_dwmmc_get_sclk(struct dwmci_host *host, unsigned long *rate)
72{
73#ifdef CONFIG_CPU_V7A
74 *rate = get_mmc_clk(host->dev_index);
75#else
76 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
77
78 *rate = clk_get_rate(&priv->clk);
79#endif
80
81 if (IS_ERR_VALUE(*rate))
82 return *rate;
83
84 return 0;
85}
86
87/**
88 * exynos_dwmmc_set_sclk - Set source clock (SDCLKIN) rate
89 * @host: MMC controller object
90 * @rate: Desired clock rate, Hz
91 *
92 * Return: 0 on success or negative value on error
93 */
94static int exynos_dwmmc_set_sclk(struct dwmci_host *host, unsigned long rate)
95{
96 int err;
97
98#ifdef CONFIG_CPU_V7A
99 unsigned long sclk;
100 unsigned int div;
101
102 err = exynos_dwmmc_get_sclk(host, &sclk);
103 if (err)
104 return err;
105
106 div = DIV_ROUND_UP(sclk, rate);
107 set_mmc_clk(host->dev_index, div);
108#else
109 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
110
111 err = clk_set_rate(&priv->clk, rate);
112 if (err < 0)
113 return err;
114#endif
115
116 return 0;
117}
118
Amara082a2d2013-04-27 11:42:55 +0530119/*
120 * Function used as callback function to initialise the
121 * CLKSEL register for every mmc channel.
122 */
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800123static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000124{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500125 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
126
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500127 dwmci_writel(host, priv->chip->clksel, priv->sdr_timing);
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800128
129 return 0;
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000130}
131
Sam Protsenko56ba9452024-08-07 22:14:31 -0500132/**
133 * exynos_dwmmc_get_ciu_div - Get internal clock divider value
134 * @host: MMC controller object
135 *
136 * Returns: Divider value, in range of 1..8
137 */
138static u8 exynos_dwmmc_get_ciu_div(struct dwmci_host *host)
Amara082a2d2013-04-27 11:42:55 +0530139{
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500140 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko56ba9452024-08-07 22:14:31 -0500141
142 if (priv->chip->div)
143 return priv->chip->div + 1;
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530144
145 /*
146 * Since SDCLKIN is divided inside controller by the DIVRATIO
147 * value set in the CLKSEL register, we need to use the same output
148 * clock value to calculate the CLKDIV value.
149 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
150 */
Sam Protsenko56ba9452024-08-07 22:14:31 -0500151 return ((dwmci_readl(host, priv->chip->clksel) >> DWMCI_DIVRATIO_BIT)
152 & DWMCI_DIVRATIO_MASK) + 1;
153}
Sam Protsenko8303fd62024-08-07 22:14:26 -0500154
Sam Protsenko56ba9452024-08-07 22:14:31 -0500155unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
156{
157 unsigned long sclk;
158 u8 clk_div;
159 int err;
160
161 clk_div = exynos_dwmmc_get_ciu_div(host);
Sam Protsenko8303fd62024-08-07 22:14:26 -0500162 err = exynos_dwmmc_get_sclk(host, &sclk);
163 if (err) {
164 printf("DWMMC%d: failed to get clock rate (%d)\n",
165 host->dev_index, err);
166 return 0;
167 }
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530168
Sam Protsenko56ba9452024-08-07 22:14:31 -0500169 return sclk / clk_div;
Amara082a2d2013-04-27 11:42:55 +0530170}
171
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900172static void exynos_dwmci_board_init(struct dwmci_host *host)
173{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500174 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900175
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900176 if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
177 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
178 dwmci_writel(host, EMMCP_SEND0, 0);
179 dwmci_writel(host, EMMCP_CTRL0,
180 MPSCTRL_SECURE_READ_BIT |
181 MPSCTRL_SECURE_WRITE_BIT |
182 MPSCTRL_NON_SECURE_READ_BIT |
183 MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
184 }
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900185
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900186 /* Set to timing value at initial time */
187 if (priv->sdr_timing)
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900188 exynos_dwmci_clksel(host);
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900189}
190
Jaehoon Chungd956a672016-06-29 19:46:17 +0900191static int exynos_dwmci_core_init(struct dwmci_host *host)
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000192{
Sam Protsenko8303fd62024-08-07 22:14:26 -0500193 unsigned long freq;
194 int err;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900195
196 if (host->bus_hz)
197 freq = host->bus_hz;
198 else
199 freq = DWMMC_MAX_FREQ;
200
Sam Protsenko8303fd62024-08-07 22:14:26 -0500201 err = exynos_dwmmc_set_sclk(host, freq);
202 if (err) {
203 printf("DWMMC%d: failed to set clock rate on probe (%d); "
204 "continue anyway\n", host->dev_index, err);
205 }
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000206
Amara082a2d2013-04-27 11:42:55 +0530207 host->name = "EXYNOS DWMMC";
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530208#ifdef CONFIG_EXYNOS5420
209 host->quirks = DWMCI_QUIRK_DISABLE_SMU;
210#endif
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900211 host->board_init = exynos_dwmci_board_init;
Amara082a2d2013-04-27 11:42:55 +0530212
Jaehoon Chunge09bd852014-05-16 13:59:57 +0900213 host->caps = MMC_MODE_DDR_52MHz;
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000214 host->clksel = exynos_dwmci_clksel;
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900215 host->get_mmc_clk = exynos_dwmci_get_clk;
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900216
217#ifndef CONFIG_DM_MMC
Amara082a2d2013-04-27 11:42:55 +0530218 /* Add the mmc channel to be registered with mmc core */
219 if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
Jaehoon Chungd956a672016-06-29 19:46:17 +0900220 printf("DWMMC%d registration failed\n", host->dev_index);
Amara082a2d2013-04-27 11:42:55 +0530221 return -1;
222 }
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900223#endif
224
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000225 return 0;
226}
227
Jaehoon Chung959198f2014-05-16 13:59:52 +0900228static int do_dwmci_init(struct dwmci_host *host)
229{
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500230#ifdef CONFIG_CPU_V7A
Jaehoon Chungd956a672016-06-29 19:46:17 +0900231 int flag, err;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900232
233 flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
234 err = exynos_pinmux_config(host->dev_id, flag);
235 if (err) {
Jaehoon Chungd956a672016-06-29 19:46:17 +0900236 printf("DWMMC%d not configure\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900237 return err;
238 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500239#endif
Jaehoon Chung959198f2014-05-16 13:59:52 +0900240
Jaehoon Chungd956a672016-06-29 19:46:17 +0900241 return exynos_dwmci_core_init(host);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900242}
243
Sam Protsenko658a1b82024-08-07 22:14:27 -0500244#ifdef CONFIG_DM_MMC
245static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung959198f2014-05-16 13:59:52 +0900246{
Sam Protsenko658a1b82024-08-07 22:14:27 -0500247 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
248 struct dwmci_host *host = &priv->host;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900249 int err = 0;
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500250 u32 div, timing[2];
Jaehoon Chung959198f2014-05-16 13:59:52 +0900251
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500252 priv->chip = (struct exynos_dwmmc_variant *)dev_get_driver_data(dev);
253
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500254#ifdef CONFIG_CPU_V7A
Sam Protsenko516f1522024-08-07 22:14:28 -0500255 const void *blob = gd->fdt_blob;
256 int node = dev_of_offset(dev);
257
Jaehoon Chung959198f2014-05-16 13:59:52 +0900258 /* Extract device id for each mmc channel */
259 host->dev_id = pinmux_decode_periph_id(blob, node);
260
Sam Protsenko516f1522024-08-07 22:14:28 -0500261 host->dev_index = dev_read_u32_default(dev, "index", host->dev_id);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900262 if (host->dev_index == host->dev_id)
263 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
264
Jaehoon Chungce757b12016-06-29 19:46:16 +0900265 if (host->dev_index > 4) {
266 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
267 return -EINVAL;
268 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500269#else
270 if (dev_read_bool(dev, "non-removable"))
271 host->dev_index = 0; /* eMMC */
272 else
273 host->dev_index = 2; /* SD card */
274#endif
Jaehoon Chungce757b12016-06-29 19:46:16 +0900275
Jaehoon Chung70f6d392016-06-29 19:46:18 +0900276 /* Get the bus width from the device node (Default is 4bit buswidth) */
Sam Protsenko516f1522024-08-07 22:14:28 -0500277 host->buswidth = dev_read_u32_default(dev, "samsung,bus-width", 4);
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900278
Jaehoon Chung959198f2014-05-16 13:59:52 +0900279 /* Set the base address from the device node */
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500280 host->ioaddr = dev_read_addr_ptr(dev);
281 if (!host->ioaddr) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900282 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900283 return -EINVAL;
284 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900285
Sam Protsenko56ba9452024-08-07 22:14:31 -0500286 if (priv->chip->div)
287 div = priv->chip->div;
288 else
289 div = dev_read_u32_default(dev, "samsung,dw-mshc-ciu-div", 0);
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500290 err = dev_read_u32_array(dev, "samsung,dw-mshc-sdr-timing", timing, 2);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900291 if (err) {
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500292 printf("DWMMC%d: Can't get sdr-timings\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900293 return -EINVAL;
294 }
295
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500296 priv->sdr_timing = DWMCI_SET_SAMPLE_CLK(timing[0]) |
297 DWMCI_SET_DRV_CLK(timing[1]) |
298 DWMCI_SET_DIV_RATIO(div);
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900299
300 /* sdr_timing didn't assigned anything, use the default value */
301 if (!priv->sdr_timing) {
302 if (host->dev_index == 0)
303 priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
304 else if (host->dev_index == 2)
305 priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
306 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900307
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500308 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Sam Protsenko516f1522024-08-07 22:14:28 -0500309 host->bus_hz = dev_read_u32_default(dev, "bus_hz", 0);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900310
311 return 0;
312}
313
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900314static int exynos_dwmmc_probe(struct udevice *dev)
315{
Simon Glassc69cda22020-12-03 16:55:20 -0700316 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900317 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
318 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
319 struct dwmci_host *host = &priv->host;
320 int err;
321
Sam Protsenko8303fd62024-08-07 22:14:26 -0500322#ifndef CONFIG_CPU_V7A
323 err = clk_get_by_index(dev, 1, &priv->clk); /* ciu */
324 if (err)
325 return err;
326#endif
327
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900328 err = do_dwmci_init(host);
329 if (err)
330 return err;
331
Jaehoon Chunge5113c32016-09-23 19:13:16 +0900332 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900333 host->mmc = &plat->mmc;
334 host->mmc->priv = &priv->host;
335 host->priv = dev;
336 upriv->mmc = host->mmc;
337
338 return dwmci_probe(dev);
339}
340
341static int exynos_dwmmc_bind(struct udevice *dev)
342{
Simon Glassc69cda22020-12-03 16:55:20 -0700343 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900344
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900345 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900346}
347
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500348static const struct exynos_dwmmc_variant exynos4_drv_data = {
349 .clksel = DWMCI_CLKSEL,
Sam Protsenko56ba9452024-08-07 22:14:31 -0500350 .div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1,
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500351};
352
353static const struct exynos_dwmmc_variant exynos5_drv_data = {
354 .clksel = DWMCI_CLKSEL,
355};
356
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900357static const struct udevice_id exynos_dwmmc_ids[] = {
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500358 {
359 .compatible = "samsung,exynos4412-dw-mshc",
360 .data = (ulong)&exynos4_drv_data,
361 }, {
362 .compatible = "samsung,exynos-dwmmc",
363 .data = (ulong)&exynos5_drv_data,
364 },
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900365 { }
366};
367
368U_BOOT_DRIVER(exynos_dwmmc_drv) = {
369 .name = "exynos_dwmmc",
370 .id = UCLASS_MMC,
371 .of_match = exynos_dwmmc_ids,
Sam Protsenko658a1b82024-08-07 22:14:27 -0500372 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900373 .bind = exynos_dwmmc_bind,
374 .ops = &dm_dwmci_ops,
375 .probe = exynos_dwmmc_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700376 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700377 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900378};
379#endif