blob: a9e240fd91d6fe5cb7bf903ace2f28b33aca9e55 [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
Sam Protsenkob8ea3812024-08-07 22:14:34 -050028/* Quirks */
29#define DWMCI_QUIRK_DISABLE_SMU BIT(0)
30
Jaehoon Chung3537ee82016-06-30 20:57:37 +090031#ifdef CONFIG_DM_MMC
32#include <dm.h>
33DECLARE_GLOBAL_DATA_PTR;
34
35struct exynos_mmc_plat {
36 struct mmc_config cfg;
37 struct mmc mmc;
38};
39#endif
40
Sam Protsenkoa95b7262024-08-07 22:14:30 -050041/* Chip specific data */
42struct exynos_dwmmc_variant {
43 u32 clksel; /* CLKSEL register offset */
Sam Protsenko56ba9452024-08-07 22:14:31 -050044 u8 div; /* (optional) fixed clock divider value: 0..7 */
Sam Protsenkob8ea3812024-08-07 22:14:34 -050045 u32 quirks; /* quirk flags - see DWMCI_QUIRK_... */
Sam Protsenkoa95b7262024-08-07 22:14:30 -050046};
47
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090048/* Exynos implmentation specific drver private data */
49struct dwmci_exynos_priv_data {
Jaehoon Chung3537ee82016-06-30 20:57:37 +090050#ifdef CONFIG_DM_MMC
51 struct dwmci_host host;
52#endif
Sam Protsenko8303fd62024-08-07 22:14:26 -050053 struct clk clk;
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090054 u32 sdr_timing;
Sam Protsenko4f89f602024-08-07 22:14:35 -050055 u32 ddr_timing;
Sam Protsenkoa95b7262024-08-07 22:14:30 -050056 const struct exynos_dwmmc_variant *chip;
Jaehoon Chung5dab81c2015-02-04 15:48:40 +090057};
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +000058
Sam Protsenkoc61f92e2024-08-07 22:14:24 -050059static struct dwmci_exynos_priv_data *exynos_dwmmc_get_priv(
60 struct dwmci_host *host)
61{
62#ifdef CONFIG_DM_MMC
63 return container_of(host, struct dwmci_exynos_priv_data, host);
64#else
65 return host->priv;
66#endif
67}
68
Sam Protsenko8303fd62024-08-07 22:14:26 -050069/**
70 * exynos_dwmmc_get_sclk - Get source clock (SDCLKIN) rate
71 * @host: MMC controller object
72 * @rate: Will contain clock rate, Hz
73 *
74 * Return: 0 on success or negative value on error
75 */
76static int exynos_dwmmc_get_sclk(struct dwmci_host *host, unsigned long *rate)
77{
78#ifdef CONFIG_CPU_V7A
79 *rate = get_mmc_clk(host->dev_index);
80#else
81 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
82
83 *rate = clk_get_rate(&priv->clk);
84#endif
85
86 if (IS_ERR_VALUE(*rate))
87 return *rate;
88
89 return 0;
90}
91
92/**
93 * exynos_dwmmc_set_sclk - Set source clock (SDCLKIN) rate
94 * @host: MMC controller object
95 * @rate: Desired clock rate, Hz
96 *
97 * Return: 0 on success or negative value on error
98 */
99static int exynos_dwmmc_set_sclk(struct dwmci_host *host, unsigned long rate)
100{
101 int err;
102
103#ifdef CONFIG_CPU_V7A
104 unsigned long sclk;
105 unsigned int div;
106
107 err = exynos_dwmmc_get_sclk(host, &sclk);
108 if (err)
109 return err;
110
111 div = DIV_ROUND_UP(sclk, rate);
112 set_mmc_clk(host->dev_index, div);
113#else
114 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
115
116 err = clk_set_rate(&priv->clk, rate);
117 if (err < 0)
118 return err;
119#endif
120
121 return 0;
122}
123
Amara082a2d2013-04-27 11:42:55 +0530124/*
125 * Function used as callback function to initialise the
126 * CLKSEL register for every mmc channel.
127 */
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800128static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000129{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500130 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko4f89f602024-08-07 22:14:35 -0500131 u32 timing;
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500132
Sam Protsenko4f89f602024-08-07 22:14:35 -0500133 if (host->mmc->selected_mode == MMC_DDR_52)
134 timing = priv->ddr_timing;
135 else
136 timing = priv->sdr_timing;
137
138 dwmci_writel(host, priv->chip->clksel, timing);
Siew Chin Limd456dfb2020-12-24 18:21:03 +0800139
140 return 0;
Jaehoon Chungd0ebbb82012-10-15 19:10:31 +0000141}
142
Sam Protsenko56ba9452024-08-07 22:14:31 -0500143/**
144 * exynos_dwmmc_get_ciu_div - Get internal clock divider value
145 * @host: MMC controller object
146 *
147 * Returns: Divider value, in range of 1..8
148 */
149static u8 exynos_dwmmc_get_ciu_div(struct dwmci_host *host)
Amara082a2d2013-04-27 11:42:55 +0530150{
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500151 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko56ba9452024-08-07 22:14:31 -0500152
153 if (priv->chip->div)
154 return priv->chip->div + 1;
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530155
156 /*
157 * Since SDCLKIN is divided inside controller by the DIVRATIO
158 * value set in the CLKSEL register, we need to use the same output
159 * clock value to calculate the CLKDIV value.
160 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
161 */
Sam Protsenko56ba9452024-08-07 22:14:31 -0500162 return ((dwmci_readl(host, priv->chip->clksel) >> DWMCI_DIVRATIO_BIT)
163 & DWMCI_DIVRATIO_MASK) + 1;
164}
Sam Protsenko8303fd62024-08-07 22:14:26 -0500165
Sam Protsenko56ba9452024-08-07 22:14:31 -0500166unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
167{
168 unsigned long sclk;
169 u8 clk_div;
170 int err;
171
Sam Protsenko97e7d1c2024-08-07 22:14:36 -0500172 /* Should be double rate for DDR mode */
173 if (host->mmc->selected_mode == MMC_DDR_52 && host->mmc->bus_width == 8)
174 freq *= 2;
175
Sam Protsenko56ba9452024-08-07 22:14:31 -0500176 clk_div = exynos_dwmmc_get_ciu_div(host);
Sam Protsenko97e7d1c2024-08-07 22:14:36 -0500177 err = exynos_dwmmc_set_sclk(host, freq * clk_div);
178 if (err) {
179 printf("DWMMC%d: failed to set clock rate (%d); "
180 "continue anyway\n", host->dev_index, err);
181 }
182
Sam Protsenko8303fd62024-08-07 22:14:26 -0500183 err = exynos_dwmmc_get_sclk(host, &sclk);
184 if (err) {
185 printf("DWMMC%d: failed to get clock rate (%d)\n",
186 host->dev_index, err);
187 return 0;
188 }
Rajeshwari S Shinded3e016c2014-02-05 10:48:15 +0530189
Sam Protsenko56ba9452024-08-07 22:14:31 -0500190 return sclk / clk_div;
Amara082a2d2013-04-27 11:42:55 +0530191}
192
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900193static void exynos_dwmci_board_init(struct dwmci_host *host)
194{
Sam Protsenkoc61f92e2024-08-07 22:14:24 -0500195 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900196
Sam Protsenkob8ea3812024-08-07 22:14:34 -0500197 if (priv->chip->quirks & DWMCI_QUIRK_DISABLE_SMU) {
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900198 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
199 dwmci_writel(host, EMMCP_SEND0, 0);
200 dwmci_writel(host, EMMCP_CTRL0,
201 MPSCTRL_SECURE_READ_BIT |
202 MPSCTRL_SECURE_WRITE_BIT |
203 MPSCTRL_NON_SECURE_READ_BIT |
204 MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
205 }
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900206
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900207 /* Set to timing value at initial time */
208 if (priv->sdr_timing)
Jaehoon Chung3a33bb12015-02-04 15:48:39 +0900209 exynos_dwmci_clksel(host);
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900210}
211
Sam Protsenko658a1b82024-08-07 22:14:27 -0500212#ifdef CONFIG_DM_MMC
213static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung959198f2014-05-16 13:59:52 +0900214{
Sam Protsenko658a1b82024-08-07 22:14:27 -0500215 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
216 struct dwmci_host *host = &priv->host;
Jaehoon Chung959198f2014-05-16 13:59:52 +0900217 int err = 0;
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500218 u32 div, timing[2];
Jaehoon Chung959198f2014-05-16 13:59:52 +0900219
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500220 priv->chip = (struct exynos_dwmmc_variant *)dev_get_driver_data(dev);
221
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500222#ifdef CONFIG_CPU_V7A
Sam Protsenko516f1522024-08-07 22:14:28 -0500223 const void *blob = gd->fdt_blob;
224 int node = dev_of_offset(dev);
225
Jaehoon Chung959198f2014-05-16 13:59:52 +0900226 /* Extract device id for each mmc channel */
227 host->dev_id = pinmux_decode_periph_id(blob, node);
228
Sam Protsenko516f1522024-08-07 22:14:28 -0500229 host->dev_index = dev_read_u32_default(dev, "index", host->dev_id);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900230 if (host->dev_index == host->dev_id)
231 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
232
Jaehoon Chungce757b12016-06-29 19:46:16 +0900233 if (host->dev_index > 4) {
234 printf("DWMMC%d: Can't get the dev index\n", host->dev_index);
235 return -EINVAL;
236 }
Sam Protsenkof6b7f9e2024-08-07 22:14:25 -0500237#else
238 if (dev_read_bool(dev, "non-removable"))
239 host->dev_index = 0; /* eMMC */
240 else
241 host->dev_index = 2; /* SD card */
242#endif
Jaehoon Chungce757b12016-06-29 19:46:16 +0900243
Jaehoon Chung70f6d392016-06-29 19:46:18 +0900244 /* Get the bus width from the device node (Default is 4bit buswidth) */
Sam Protsenkod355c1c2024-08-07 22:14:32 -0500245 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900246
Jaehoon Chung959198f2014-05-16 13:59:52 +0900247 /* Set the base address from the device node */
Sam Protsenkoff2b8832024-08-07 22:14:23 -0500248 host->ioaddr = dev_read_addr_ptr(dev);
249 if (!host->ioaddr) {
Jaehoon Chungdfcb6832014-11-28 20:42:33 +0900250 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900251 return -EINVAL;
252 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900253
Sam Protsenko56ba9452024-08-07 22:14:31 -0500254 if (priv->chip->div)
255 div = priv->chip->div;
256 else
257 div = dev_read_u32_default(dev, "samsung,dw-mshc-ciu-div", 0);
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500258 err = dev_read_u32_array(dev, "samsung,dw-mshc-sdr-timing", timing, 2);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900259 if (err) {
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500260 printf("DWMMC%d: Can't get sdr-timings\n", host->dev_index);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900261 return -EINVAL;
262 }
263
Sam Protsenkob55f03e2024-08-07 22:14:29 -0500264 priv->sdr_timing = DWMCI_SET_SAMPLE_CLK(timing[0]) |
265 DWMCI_SET_DRV_CLK(timing[1]) |
266 DWMCI_SET_DIV_RATIO(div);
Jaehoon Chung5dab81c2015-02-04 15:48:40 +0900267
268 /* sdr_timing didn't assigned anything, use the default value */
269 if (!priv->sdr_timing) {
270 if (host->dev_index == 0)
271 priv->sdr_timing = DWMMC_MMC0_SDR_TIMING_VAL;
272 else if (host->dev_index == 2)
273 priv->sdr_timing = DWMMC_MMC2_SDR_TIMING_VAL;
274 }
Jaehoon Chung959198f2014-05-16 13:59:52 +0900275
Sam Protsenko4f89f602024-08-07 22:14:35 -0500276 err = dev_read_u32_array(dev, "samsung,dw-mshc-ddr-timing", timing, 2);
277 if (err) {
278 debug("DWMMC%d: Can't get ddr-timings, using sdr-timings\n",
279 host->dev_index);
280 priv->ddr_timing = priv->sdr_timing;
281 } else {
282 priv->ddr_timing = DWMCI_SET_SAMPLE_CLK(timing[0]) |
283 DWMCI_SET_DRV_CLK(timing[1]) |
284 DWMCI_SET_DIV_RATIO(div);
285 }
286
Sam Protsenkoffd62e02024-08-07 22:14:17 -0500287 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Sam Protsenko897e96c2024-08-07 22:14:33 -0500288 host->bus_hz = dev_read_u32_default(dev, "clock-frequency", 0);
Jaehoon Chung959198f2014-05-16 13:59:52 +0900289
290 return 0;
291}
292
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900293static int exynos_dwmmc_probe(struct udevice *dev)
294{
Simon Glassc69cda22020-12-03 16:55:20 -0700295 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900296 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
297 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
298 struct dwmci_host *host = &priv->host;
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500299 unsigned long freq;
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900300 int err;
301
Sam Protsenko8303fd62024-08-07 22:14:26 -0500302#ifndef CONFIG_CPU_V7A
303 err = clk_get_by_index(dev, 1, &priv->clk); /* ciu */
304 if (err)
305 return err;
306#endif
307
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500308#ifdef CONFIG_CPU_V7A
309 int flag;
310
311 flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
312 err = exynos_pinmux_config(host->dev_id, flag);
313 if (err) {
314 printf("DWMMC%d not configure\n", host->dev_index);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900315 return err;
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500316 }
317#endif
318
319 if (host->bus_hz)
320 freq = host->bus_hz;
321 else
322 freq = DWMMC_MAX_FREQ;
323
324 err = exynos_dwmmc_set_sclk(host, freq);
325 if (err) {
326 printf("DWMMC%d: failed to set clock rate on probe (%d); "
327 "continue anyway\n", host->dev_index, err);
328 }
329
Sam Protsenko549afd72024-08-07 22:14:40 -0500330 host->name = dev->name;
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500331 host->board_init = exynos_dwmci_board_init;
332 host->caps = MMC_MODE_DDR_52MHz;
333 host->clksel = exynos_dwmci_clksel;
334 host->get_mmc_clk = exynos_dwmci_get_clk;
335
Sam Protsenko06663c92024-08-07 22:14:39 -0500336#ifdef CONFIG_BLK
337 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
338 host->mmc = &plat->mmc;
339#else
340 err = add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
341 if (err) {
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500342 printf("DWMMC%d registration failed\n", host->dev_index);
Sam Protsenko06663c92024-08-07 22:14:39 -0500343 return err;
Sam Protsenko0d5e8172024-08-07 22:14:38 -0500344 }
345#endif
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900346
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900347 host->mmc->priv = &priv->host;
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900348 upriv->mmc = host->mmc;
Sam Protsenko06663c92024-08-07 22:14:39 -0500349 host->mmc->dev = dev;
350 host->priv = dev;
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900351
352 return dwmci_probe(dev);
353}
354
355static int exynos_dwmmc_bind(struct udevice *dev)
356{
Simon Glassc69cda22020-12-03 16:55:20 -0700357 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900358
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900359 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900360}
361
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500362static const struct exynos_dwmmc_variant exynos4_drv_data = {
363 .clksel = DWMCI_CLKSEL,
Sam Protsenko56ba9452024-08-07 22:14:31 -0500364 .div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1,
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500365};
366
367static const struct exynos_dwmmc_variant exynos5_drv_data = {
368 .clksel = DWMCI_CLKSEL,
Sam Protsenkob8ea3812024-08-07 22:14:34 -0500369#ifdef CONFIG_EXYNOS5420
370 .quirks = DWMCI_QUIRK_DISABLE_SMU,
371#endif
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500372};
373
Sam Protsenko9780ca62024-08-07 22:14:37 -0500374static const struct exynos_dwmmc_variant exynos7_smu_drv_data = {
375 .clksel = DWMCI_CLKSEL64,
376 .quirks = DWMCI_QUIRK_DISABLE_SMU,
377};
378
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900379static const struct udevice_id exynos_dwmmc_ids[] = {
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500380 {
381 .compatible = "samsung,exynos4412-dw-mshc",
382 .data = (ulong)&exynos4_drv_data,
383 }, {
384 .compatible = "samsung,exynos-dwmmc",
385 .data = (ulong)&exynos5_drv_data,
Sam Protsenko9780ca62024-08-07 22:14:37 -0500386 }, {
387 .compatible = "samsung,exynos7-dw-mshc-smu",
388 .data = (ulong)&exynos7_smu_drv_data,
Sam Protsenkoa95b7262024-08-07 22:14:30 -0500389 },
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900390 { }
391};
392
393U_BOOT_DRIVER(exynos_dwmmc_drv) = {
394 .name = "exynos_dwmmc",
395 .id = UCLASS_MMC,
396 .of_match = exynos_dwmmc_ids,
Sam Protsenko658a1b82024-08-07 22:14:27 -0500397 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900398 .bind = exynos_dwmmc_bind,
399 .ops = &dm_dwmci_ops,
400 .probe = exynos_dwmmc_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700401 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700402 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung3537ee82016-06-30 20:57:37 +0900403};
404#endif