blob: 41fb32c579efce78f68b9da0596baa414b1a833d [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
Sam Protsenko40ad20d2024-08-07 22:14:31 -050026#define EXYNOS4412_FIXED_CIU_CLK_DIV 4
27
Sam Protsenko3b264402024-08-07 22:14:34 -050028/* Quirks */
29#define DWMCI_QUIRK_DISABLE_SMU BIT(0)
30
Jaehoon Chung98d18e92016-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 Protsenko60b63e42024-08-07 22:14:30 -050041/* Chip specific data */
42struct exynos_dwmmc_variant {
43 u32 clksel; /* CLKSEL register offset */
Sam Protsenko40ad20d2024-08-07 22:14:31 -050044 u8 div; /* (optional) fixed clock divider value: 0..7 */
Sam Protsenko3b264402024-08-07 22:14:34 -050045 u32 quirks; /* quirk flags - see DWMCI_QUIRK_... */
Sam Protsenko60b63e42024-08-07 22:14:30 -050046};
47
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090048/* Exynos implmentation specific drver private data */
49struct dwmci_exynos_priv_data {
Jaehoon Chung98d18e92016-06-30 20:57:37 +090050#ifdef CONFIG_DM_MMC
51 struct dwmci_host host;
52#endif
Sam Protsenko57ddb372024-08-07 22:14:26 -050053 struct clk clk;
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090054 u32 sdr_timing;
Sam Protsenko4fe61da2024-08-07 22:14:35 -050055 u32 ddr_timing;
Sam Protsenko60b63e42024-08-07 22:14:30 -050056 const struct exynos_dwmmc_variant *chip;
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090057};
Jaehoon Chung7aff9672012-10-15 19:10:31 +000058
Sam Protsenko3192a642024-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 Protsenko57ddb372024-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
Amard8501212013-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 Limc51e7e12020-12-24 18:21:03 +0800128static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000129{
Sam Protsenko3192a642024-08-07 22:14:24 -0500130 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko4fe61da2024-08-07 22:14:35 -0500131 u32 timing;
Sam Protsenko3192a642024-08-07 22:14:24 -0500132
Sam Protsenko4fe61da2024-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 Limc51e7e12020-12-24 18:21:03 +0800139
140 return 0;
Amard8501212013-04-27 11:42:55 +0530141}
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000142
Sam Protsenko40ad20d2024-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)
Amard8501212013-04-27 11:42:55 +0530150{
Sam Protsenko60b63e42024-08-07 22:14:30 -0500151 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500152
153 if (priv->chip->div)
154 return priv->chip->div + 1;
Rajeshwari S Shindeccfa20b2014-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 Protsenko40ad20d2024-08-07 22:14:31 -0500162 return ((dwmci_readl(host, priv->chip->clksel) >> DWMCI_DIVRATIO_BIT)
163 & DWMCI_DIVRATIO_MASK) + 1;
164}
Sam Protsenko57ddb372024-08-07 22:14:26 -0500165
Sam Protsenko40ad20d2024-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 Protsenkof35cd262024-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 Protsenko40ad20d2024-08-07 22:14:31 -0500176 clk_div = exynos_dwmmc_get_ciu_div(host);
Sam Protsenkof35cd262024-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 Protsenko57ddb372024-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 Shindeccfa20b2014-02-05 10:48:15 +0530189
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500190 return sclk / clk_div;
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000191}
192
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900193static void exynos_dwmci_board_init(struct dwmci_host *host)
194{
Sam Protsenko3192a642024-08-07 22:14:24 -0500195 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900196
Sam Protsenko3b264402024-08-07 22:14:34 -0500197 if (priv->chip->quirks & DWMCI_QUIRK_DISABLE_SMU) {
Jaehoon Chung42f81a82013-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 Chung3d12e552015-02-04 15:48:39 +0900206
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900207 /* Set to timing value at initial time */
208 if (priv->sdr_timing)
Jaehoon Chung3d12e552015-02-04 15:48:39 +0900209 exynos_dwmci_clksel(host);
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900210}
211
Sam Protsenkof78333a2024-08-07 22:14:27 -0500212#ifdef CONFIG_DM_MMC
213static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung62811102014-05-16 13:59:52 +0900214{
Sam Protsenkof78333a2024-08-07 22:14:27 -0500215 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
216 struct dwmci_host *host = &priv->host;
Jaehoon Chung62811102014-05-16 13:59:52 +0900217 int err = 0;
Sam Protsenkobab187c2024-08-07 22:14:29 -0500218 u32 div, timing[2];
Amard8501212013-04-27 11:42:55 +0530219
Sam Protsenko60b63e42024-08-07 22:14:30 -0500220 priv->chip = (struct exynos_dwmmc_variant *)dev_get_driver_data(dev);
221
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500222#ifdef CONFIG_CPU_V7A
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500223 const void *blob = gd->fdt_blob;
224 int node = dev_of_offset(dev);
225
Jaehoon Chung62811102014-05-16 13:59:52 +0900226 /* Extract device id for each mmc channel */
227 host->dev_id = pinmux_decode_periph_id(blob, node);
Amard8501212013-04-27 11:42:55 +0530228
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500229 host->dev_index = dev_read_u32_default(dev, "index", host->dev_id);
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900230 if (host->dev_index == host->dev_id)
231 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
232
Jaehoon Chunge0303c72016-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 Protsenko0e28aa02024-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 Chunge0303c72016-06-29 19:46:16 +0900243
Jaehoon Chung865ecd92016-06-29 19:46:18 +0900244 /* Get the bus width from the device node (Default is 4bit buswidth) */
Sam Protsenko38d8d5d2024-08-07 22:14:32 -0500245 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Amard8501212013-04-27 11:42:55 +0530246
Jaehoon Chung62811102014-05-16 13:59:52 +0900247 /* Set the base address from the device node */
Sam Protsenko745edd62024-08-07 22:14:23 -0500248 host->ioaddr = dev_read_addr_ptr(dev);
249 if (!host->ioaddr) {
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900250 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900251 return -EINVAL;
252 }
Jaehoon Chung62811102014-05-16 13:59:52 +0900253
Sam Protsenko40ad20d2024-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 Protsenkobab187c2024-08-07 22:14:29 -0500258 err = dev_read_u32_array(dev, "samsung,dw-mshc-sdr-timing", timing, 2);
Jaehoon Chung62811102014-05-16 13:59:52 +0900259 if (err) {
Sam Protsenkobab187c2024-08-07 22:14:29 -0500260 printf("DWMMC%d: Can't get sdr-timings\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900261 return -EINVAL;
262 }
263
Sam Protsenkobab187c2024-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 Chungbcb03eab2015-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 Chung62811102014-05-16 13:59:52 +0900275
Sam Protsenko4fe61da2024-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 Protsenko751fdf12024-08-07 22:14:17 -0500287 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Sam Protsenkob4dbd282024-08-07 22:14:33 -0500288 host->bus_hz = dev_read_u32_default(dev, "clock-frequency", 0);
Jaehoon Chung62811102014-05-16 13:59:52 +0900289
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000290 return 0;
291}
Jaehoon Chung62811102014-05-16 13:59:52 +0900292
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900293static int exynos_dwmmc_probe(struct udevice *dev)
294{
Simon Glassfa20e932020-12-03 16:55:20 -0700295 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-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 Protsenkobb875e82024-08-07 22:14:38 -0500299 unsigned long freq;
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900300 int err;
301
Sam Protsenko57ddb372024-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 Protsenkobb875e82024-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 Chung98d18e92016-06-30 20:57:37 +0900315 return err;
Sam Protsenkobb875e82024-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
330 host->name = "EXYNOS DWMMC";
331 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
336#ifndef CONFIG_DM_MMC
337 /* Add the mmc channel to be registered with mmc core */
338 if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
339 printf("DWMMC%d registration failed\n", host->dev_index);
340 return -1;
341 }
342#endif
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900343
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900344 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900345 host->mmc = &plat->mmc;
346 host->mmc->priv = &priv->host;
347 host->priv = dev;
348 upriv->mmc = host->mmc;
349
350 return dwmci_probe(dev);
351}
352
353static int exynos_dwmmc_bind(struct udevice *dev)
354{
Simon Glassfa20e932020-12-03 16:55:20 -0700355 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900356
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900357 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900358}
359
Sam Protsenko60b63e42024-08-07 22:14:30 -0500360static const struct exynos_dwmmc_variant exynos4_drv_data = {
361 .clksel = DWMCI_CLKSEL,
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500362 .div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1,
Sam Protsenko60b63e42024-08-07 22:14:30 -0500363};
364
365static const struct exynos_dwmmc_variant exynos5_drv_data = {
366 .clksel = DWMCI_CLKSEL,
Sam Protsenko3b264402024-08-07 22:14:34 -0500367#ifdef CONFIG_EXYNOS5420
368 .quirks = DWMCI_QUIRK_DISABLE_SMU,
369#endif
Sam Protsenko60b63e42024-08-07 22:14:30 -0500370};
371
Sam Protsenkoaf9dcff2024-08-07 22:14:37 -0500372static const struct exynos_dwmmc_variant exynos7_smu_drv_data = {
373 .clksel = DWMCI_CLKSEL64,
374 .quirks = DWMCI_QUIRK_DISABLE_SMU,
375};
376
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900377static const struct udevice_id exynos_dwmmc_ids[] = {
Sam Protsenko60b63e42024-08-07 22:14:30 -0500378 {
379 .compatible = "samsung,exynos4412-dw-mshc",
380 .data = (ulong)&exynos4_drv_data,
381 }, {
382 .compatible = "samsung,exynos-dwmmc",
383 .data = (ulong)&exynos5_drv_data,
Sam Protsenkoaf9dcff2024-08-07 22:14:37 -0500384 }, {
385 .compatible = "samsung,exynos7-dw-mshc-smu",
386 .data = (ulong)&exynos7_smu_drv_data,
Sam Protsenko60b63e42024-08-07 22:14:30 -0500387 },
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900388 { }
389};
390
391U_BOOT_DRIVER(exynos_dwmmc_drv) = {
392 .name = "exynos_dwmmc",
393 .id = UCLASS_MMC,
394 .of_match = exynos_dwmmc_ids,
Sam Protsenkof78333a2024-08-07 22:14:27 -0500395 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900396 .bind = exynos_dwmmc_bind,
397 .ops = &dm_dwmci_ops,
398 .probe = exynos_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700399 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700400 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900401};
402#endif