blob: b9d655c0d5c89779b49f7917cf4d2ce422b7d251 [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 Protsenko60b63e42024-08-07 22:14:30 -050055 const struct exynos_dwmmc_variant *chip;
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +090056};
Jaehoon Chung7aff9672012-10-15 19:10:31 +000057
Sam Protsenko3192a642024-08-07 22:14:24 -050058static struct dwmci_exynos_priv_data *exynos_dwmmc_get_priv(
59 struct dwmci_host *host)
60{
61#ifdef CONFIG_DM_MMC
62 return container_of(host, struct dwmci_exynos_priv_data, host);
63#else
64 return host->priv;
65#endif
66}
67
Sam Protsenko57ddb372024-08-07 22:14:26 -050068/**
69 * exynos_dwmmc_get_sclk - Get source clock (SDCLKIN) rate
70 * @host: MMC controller object
71 * @rate: Will contain clock rate, Hz
72 *
73 * Return: 0 on success or negative value on error
74 */
75static int exynos_dwmmc_get_sclk(struct dwmci_host *host, unsigned long *rate)
76{
77#ifdef CONFIG_CPU_V7A
78 *rate = get_mmc_clk(host->dev_index);
79#else
80 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
81
82 *rate = clk_get_rate(&priv->clk);
83#endif
84
85 if (IS_ERR_VALUE(*rate))
86 return *rate;
87
88 return 0;
89}
90
91/**
92 * exynos_dwmmc_set_sclk - Set source clock (SDCLKIN) rate
93 * @host: MMC controller object
94 * @rate: Desired clock rate, Hz
95 *
96 * Return: 0 on success or negative value on error
97 */
98static int exynos_dwmmc_set_sclk(struct dwmci_host *host, unsigned long rate)
99{
100 int err;
101
102#ifdef CONFIG_CPU_V7A
103 unsigned long sclk;
104 unsigned int div;
105
106 err = exynos_dwmmc_get_sclk(host, &sclk);
107 if (err)
108 return err;
109
110 div = DIV_ROUND_UP(sclk, rate);
111 set_mmc_clk(host->dev_index, div);
112#else
113 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
114
115 err = clk_set_rate(&priv->clk, rate);
116 if (err < 0)
117 return err;
118#endif
119
120 return 0;
121}
122
Amard8501212013-04-27 11:42:55 +0530123/*
124 * Function used as callback function to initialise the
125 * CLKSEL register for every mmc channel.
126 */
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800127static int exynos_dwmci_clksel(struct dwmci_host *host)
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000128{
Sam Protsenko3192a642024-08-07 22:14:24 -0500129 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
130
Sam Protsenko60b63e42024-08-07 22:14:30 -0500131 dwmci_writel(host, priv->chip->clksel, priv->sdr_timing);
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800132
133 return 0;
Amard8501212013-04-27 11:42:55 +0530134}
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000135
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500136/**
137 * exynos_dwmmc_get_ciu_div - Get internal clock divider value
138 * @host: MMC controller object
139 *
140 * Returns: Divider value, in range of 1..8
141 */
142static u8 exynos_dwmmc_get_ciu_div(struct dwmci_host *host)
Amard8501212013-04-27 11:42:55 +0530143{
Sam Protsenko60b63e42024-08-07 22:14:30 -0500144 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500145
146 if (priv->chip->div)
147 return priv->chip->div + 1;
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530148
149 /*
150 * Since SDCLKIN is divided inside controller by the DIVRATIO
151 * value set in the CLKSEL register, we need to use the same output
152 * clock value to calculate the CLKDIV value.
153 * as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
154 */
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500155 return ((dwmci_readl(host, priv->chip->clksel) >> DWMCI_DIVRATIO_BIT)
156 & DWMCI_DIVRATIO_MASK) + 1;
157}
Sam Protsenko57ddb372024-08-07 22:14:26 -0500158
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500159unsigned int exynos_dwmci_get_clk(struct dwmci_host *host, uint freq)
160{
161 unsigned long sclk;
162 u8 clk_div;
163 int err;
164
165 clk_div = exynos_dwmmc_get_ciu_div(host);
Sam Protsenko57ddb372024-08-07 22:14:26 -0500166 err = exynos_dwmmc_get_sclk(host, &sclk);
167 if (err) {
168 printf("DWMMC%d: failed to get clock rate (%d)\n",
169 host->dev_index, err);
170 return 0;
171 }
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530172
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500173 return sclk / clk_div;
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000174}
175
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900176static void exynos_dwmci_board_init(struct dwmci_host *host)
177{
Sam Protsenko3192a642024-08-07 22:14:24 -0500178 struct dwmci_exynos_priv_data *priv = exynos_dwmmc_get_priv(host);
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900179
Sam Protsenko3b264402024-08-07 22:14:34 -0500180 if (priv->chip->quirks & DWMCI_QUIRK_DISABLE_SMU) {
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900181 dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
182 dwmci_writel(host, EMMCP_SEND0, 0);
183 dwmci_writel(host, EMMCP_CTRL0,
184 MPSCTRL_SECURE_READ_BIT |
185 MPSCTRL_SECURE_WRITE_BIT |
186 MPSCTRL_NON_SECURE_READ_BIT |
187 MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
188 }
Jaehoon Chung3d12e552015-02-04 15:48:39 +0900189
Jaehoon Chungbcb03eab2015-02-04 15:48:40 +0900190 /* Set to timing value at initial time */
191 if (priv->sdr_timing)
Jaehoon Chung3d12e552015-02-04 15:48:39 +0900192 exynos_dwmci_clksel(host);
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900193}
194
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900195static int exynos_dwmci_core_init(struct dwmci_host *host)
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000196{
Sam Protsenko57ddb372024-08-07 22:14:26 -0500197 unsigned long freq;
198 int err;
Jaehoon Chung62811102014-05-16 13:59:52 +0900199
200 if (host->bus_hz)
201 freq = host->bus_hz;
202 else
203 freq = DWMMC_MAX_FREQ;
204
Sam Protsenko57ddb372024-08-07 22:14:26 -0500205 err = exynos_dwmmc_set_sclk(host, freq);
206 if (err) {
207 printf("DWMMC%d: failed to set clock rate on probe (%d); "
208 "continue anyway\n", host->dev_index, err);
209 }
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000210
Amard8501212013-04-27 11:42:55 +0530211 host->name = "EXYNOS DWMMC";
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900212 host->board_init = exynos_dwmci_board_init;
Jaehoon Chungef91dd52014-05-16 13:59:57 +0900213 host->caps = MMC_MODE_DDR_52MHz;
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000214 host->clksel = exynos_dwmci_clksel;
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900215 host->get_mmc_clk = exynos_dwmci_get_clk;
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900216
217#ifndef CONFIG_DM_MMC
Amard8501212013-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 Chungde61aaee2016-06-29 19:46:17 +0900220 printf("DWMMC%d registration failed\n", host->dev_index);
Amard8501212013-04-27 11:42:55 +0530221 return -1;
222 }
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900223#endif
224
Amard8501212013-04-27 11:42:55 +0530225 return 0;
226}
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000227
Jaehoon Chung62811102014-05-16 13:59:52 +0900228static int do_dwmci_init(struct dwmci_host *host)
Amard8501212013-04-27 11:42:55 +0530229{
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500230#ifdef CONFIG_CPU_V7A
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900231 int flag, err;
Amard8501212013-04-27 11:42:55 +0530232
Jaehoon Chung62811102014-05-16 13:59:52 +0900233 flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
234 err = exynos_pinmux_config(host->dev_id, flag);
235 if (err) {
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900236 printf("DWMMC%d not configure\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900237 return err;
238 }
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500239#endif
Amard8501212013-04-27 11:42:55 +0530240
Jaehoon Chungde61aaee2016-06-29 19:46:17 +0900241 return exynos_dwmci_core_init(host);
Jaehoon Chung62811102014-05-16 13:59:52 +0900242}
Amard8501212013-04-27 11:42:55 +0530243
Sam Protsenkof78333a2024-08-07 22:14:27 -0500244#ifdef CONFIG_DM_MMC
245static int exynos_dwmmc_of_to_plat(struct udevice *dev)
Jaehoon Chung62811102014-05-16 13:59:52 +0900246{
Sam Protsenkof78333a2024-08-07 22:14:27 -0500247 struct dwmci_exynos_priv_data *priv = dev_get_priv(dev);
248 struct dwmci_host *host = &priv->host;
Jaehoon Chung62811102014-05-16 13:59:52 +0900249 int err = 0;
Sam Protsenkobab187c2024-08-07 22:14:29 -0500250 u32 div, timing[2];
Amard8501212013-04-27 11:42:55 +0530251
Sam Protsenko60b63e42024-08-07 22:14:30 -0500252 priv->chip = (struct exynos_dwmmc_variant *)dev_get_driver_data(dev);
253
Sam Protsenko0e28aa02024-08-07 22:14:25 -0500254#ifdef CONFIG_CPU_V7A
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500255 const void *blob = gd->fdt_blob;
256 int node = dev_of_offset(dev);
257
Jaehoon Chung62811102014-05-16 13:59:52 +0900258 /* Extract device id for each mmc channel */
259 host->dev_id = pinmux_decode_periph_id(blob, node);
Amard8501212013-04-27 11:42:55 +0530260
Sam Protsenko6002ceb2024-08-07 22:14:28 -0500261 host->dev_index = dev_read_u32_default(dev, "index", host->dev_id);
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900262 if (host->dev_index == host->dev_id)
263 host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
264
Jaehoon Chunge0303c72016-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 Protsenko0e28aa02024-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 Chunge0303c72016-06-29 19:46:16 +0900275
Jaehoon Chung865ecd92016-06-29 19:46:18 +0900276 /* Get the bus width from the device node (Default is 4bit buswidth) */
Sam Protsenko38d8d5d2024-08-07 22:14:32 -0500277 host->buswidth = dev_read_u32_default(dev, "bus-width", 4);
Amard8501212013-04-27 11:42:55 +0530278
Jaehoon Chung62811102014-05-16 13:59:52 +0900279 /* Set the base address from the device node */
Sam Protsenko745edd62024-08-07 22:14:23 -0500280 host->ioaddr = dev_read_addr_ptr(dev);
281 if (!host->ioaddr) {
Jaehoon Chungdb313bf2014-11-28 20:42:33 +0900282 printf("DWMMC%d: Can't get base address\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900283 return -EINVAL;
284 }
Jaehoon Chung62811102014-05-16 13:59:52 +0900285
Sam Protsenko40ad20d2024-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 Protsenkobab187c2024-08-07 22:14:29 -0500290 err = dev_read_u32_array(dev, "samsung,dw-mshc-sdr-timing", timing, 2);
Jaehoon Chung62811102014-05-16 13:59:52 +0900291 if (err) {
Sam Protsenkobab187c2024-08-07 22:14:29 -0500292 printf("DWMMC%d: Can't get sdr-timings\n", host->dev_index);
Jaehoon Chung62811102014-05-16 13:59:52 +0900293 return -EINVAL;
294 }
295
Sam Protsenkobab187c2024-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 Chungbcb03eab2015-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 Chung62811102014-05-16 13:59:52 +0900307
Sam Protsenko751fdf12024-08-07 22:14:17 -0500308 host->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
Sam Protsenkob4dbd282024-08-07 22:14:33 -0500309 host->bus_hz = dev_read_u32_default(dev, "clock-frequency", 0);
Jaehoon Chung62811102014-05-16 13:59:52 +0900310
Jaehoon Chung7aff9672012-10-15 19:10:31 +0000311 return 0;
312}
Jaehoon Chung62811102014-05-16 13:59:52 +0900313
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900314static int exynos_dwmmc_probe(struct udevice *dev)
315{
Simon Glassfa20e932020-12-03 16:55:20 -0700316 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-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 Protsenko57ddb372024-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 Chung98d18e92016-06-30 20:57:37 +0900328 err = do_dwmci_init(host);
329 if (err)
330 return err;
331
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900332 dwmci_setup_cfg(&plat->cfg, host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ);
Jaehoon Chung98d18e92016-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 Glassfa20e932020-12-03 16:55:20 -0700343 struct exynos_mmc_plat *plat = dev_get_plat(dev);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900344
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900345 return dwmci_bind(dev, &plat->mmc, &plat->cfg);
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900346}
347
Sam Protsenko60b63e42024-08-07 22:14:30 -0500348static const struct exynos_dwmmc_variant exynos4_drv_data = {
349 .clksel = DWMCI_CLKSEL,
Sam Protsenko40ad20d2024-08-07 22:14:31 -0500350 .div = EXYNOS4412_FIXED_CIU_CLK_DIV - 1,
Sam Protsenko60b63e42024-08-07 22:14:30 -0500351};
352
353static const struct exynos_dwmmc_variant exynos5_drv_data = {
354 .clksel = DWMCI_CLKSEL,
Sam Protsenko3b264402024-08-07 22:14:34 -0500355#ifdef CONFIG_EXYNOS5420
356 .quirks = DWMCI_QUIRK_DISABLE_SMU,
357#endif
Sam Protsenko60b63e42024-08-07 22:14:30 -0500358};
359
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900360static const struct udevice_id exynos_dwmmc_ids[] = {
Sam Protsenko60b63e42024-08-07 22:14:30 -0500361 {
362 .compatible = "samsung,exynos4412-dw-mshc",
363 .data = (ulong)&exynos4_drv_data,
364 }, {
365 .compatible = "samsung,exynos-dwmmc",
366 .data = (ulong)&exynos5_drv_data,
367 },
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900368 { }
369};
370
371U_BOOT_DRIVER(exynos_dwmmc_drv) = {
372 .name = "exynos_dwmmc",
373 .id = UCLASS_MMC,
374 .of_match = exynos_dwmmc_ids,
Sam Protsenkof78333a2024-08-07 22:14:27 -0500375 .of_to_plat = exynos_dwmmc_of_to_plat,
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900376 .bind = exynos_dwmmc_bind,
377 .ops = &dm_dwmci_ops,
378 .probe = exynos_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700379 .priv_auto = sizeof(struct dwmci_exynos_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700380 .plat_auto = sizeof(struct exynos_mmc_plat),
Jaehoon Chung98d18e92016-06-30 20:57:37 +0900381};
382#endif