1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
5 * Rockchip SD Host Controller Interface
11 #include <dm/ofnode.h>
12 #include <dt-structs.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/libfdt.h>
16 #include <linux/iopoll.h>
19 #include "mmc_private.h"
22 #include <asm/arch-rockchip/clock.h>
23 #include <asm/arch-rockchip/hardware.h>
25 /* 400KHz is max freq for card ID etc. Use that as min */
26 #define EMMC_MIN_FREQ 400000
28 #define MHz (1000 * KHz)
29 #define SDHCI_TUNING_LOOP_COUNT 40
31 #define PHYCTRL_CALDONE_MASK 0x1
32 #define PHYCTRL_CALDONE_SHIFT 0x6
33 #define PHYCTRL_CALDONE_DONE 0x1
34 #define PHYCTRL_DLLRDY_MASK 0x1
35 #define PHYCTRL_DLLRDY_SHIFT 0x5
36 #define PHYCTRL_DLLRDY_DONE 0x1
37 #define PHYCTRL_FREQSEL_200M 0x0
38 #define PHYCTRL_FREQSEL_50M 0x1
39 #define PHYCTRL_FREQSEL_100M 0x2
40 #define PHYCTRL_FREQSEL_150M 0x3
41 #define PHYCTRL_DLL_LOCK_WO_TMOUT(x) \
42 ((((x) >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK) ==\
45 /* Rockchip specific Registers */
46 #define DWCMSHC_EMMC_DLL_CTRL 0x800
47 #define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
48 #define DWCMSHC_EMMC_DLL_RXCLK 0x804
49 #define DWCMSHC_EMMC_DLL_TXCLK 0x808
50 #define DWCMSHC_EMMC_DLL_STRBIN 0x80c
51 #define DWCMSHC_EMMC_DLL_STATUS0 0x840
52 #define DWCMSHC_EMMC_DLL_STATUS1 0x844
53 #define DWCMSHC_EMMC_DLL_START BIT(0)
54 #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29
55 #define DWCMSHC_EMMC_DLL_START_POINT 16
56 #define DWCMSHC_EMMC_DLL_START_DEFAULT 5
57 #define DWCMSHC_EMMC_DLL_INC_VALUE 2
58 #define DWCMSHC_EMMC_DLL_INC 8
59 #define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
60 #define DLL_TXCLK_TAPNUM_DEFAULT 0x10
61 #define DLL_STRBIN_TAPNUM_DEFAULT 0x3
62 #define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
63 #define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
64 #define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
65 #define DLL_RXCLK_NO_INVERTER 1
66 #define DLL_RXCLK_INVERTER 0
67 #define DWCMSHC_ENHANCED_STROBE BIT(8)
68 #define DLL_LOCK_WO_TMOUT(x) \
69 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
70 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
71 #define ROCKCHIP_MAX_CLKS 3
73 struct rockchip_sdhc_plat {
74 struct mmc_config cfg;
78 struct rockchip_emmc_phy {
84 struct rockchip_sdhc {
85 struct sdhci_host host;
88 struct rockchip_emmc_phy *phy;
93 int (*emmc_phy_init)(struct udevice *dev);
94 int (*get_phy)(struct udevice *dev);
97 * set_control_reg() - Set SDHCI control registers
99 * This is the set_control_reg() SDHCI operation that should be
100 * used for the hardware this driver data is associated with.
101 * Normally, this is used to set up control registers for
102 * voltage level and UHS speed mode.
104 * @host: SDHCI host structure
106 void (*set_control_reg)(struct sdhci_host *host);
109 * set_ios_post() - Host specific hook after set_ios() calls
111 * This is the set_ios_post() SDHCI operation that should be
112 * used for the hardware this driver data is associated with.
113 * Normally, this is a hook that is called after sdhci_set_ios()
114 * that does any necessary host-specific configuration.
116 * @host: SDHCI host structure
117 * Return: 0 if successful, -ve on error
119 int (*set_ios_post)(struct sdhci_host *host);
122 static int rk3399_emmc_phy_init(struct udevice *dev)
127 static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
129 u32 caldone, dllrdy, freqsel;
131 writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
132 writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
133 writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
136 * According to the user manual, calpad calibration
137 * cycle takes more than 2us without the minimal recommended
138 * value, so we may need a little margin here
141 writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
144 * According to the user manual, it asks driver to
145 * wait 5us for calpad busy trimming. But it seems that
146 * 5us of caldone isn't enough for all cases.
149 caldone = readl(&phy->emmcphy_status);
150 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
151 if (caldone != PHYCTRL_CALDONE_DONE) {
152 printf("%s: caldone timeout.\n", __func__);
156 /* Set the frequency of the DLL operation */
157 if (clock < 75 * MHz)
158 freqsel = PHYCTRL_FREQSEL_50M;
159 else if (clock < 125 * MHz)
160 freqsel = PHYCTRL_FREQSEL_100M;
161 else if (clock < 175 * MHz)
162 freqsel = PHYCTRL_FREQSEL_150M;
164 freqsel = PHYCTRL_FREQSEL_200M;
166 /* Set the frequency of the DLL operation */
167 writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
168 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
170 /* REN Enable on STRB Line for HS400 */
171 writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
173 read_poll_timeout(readl, &phy->emmcphy_status, dllrdy,
174 PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1, 5000);
177 static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
179 writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
180 writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
183 static int rk3399_emmc_get_phy(struct udevice *dev)
185 struct rockchip_sdhc *priv = dev_get_priv(dev);
188 u32 grf_phy_offset, phandle;
190 phandle = dev_read_u32_default(dev, "phys", 0);
191 phy_node = ofnode_get_by_phandle(phandle);
192 if (!ofnode_valid(phy_node)) {
193 debug("Not found emmc phy device\n");
197 grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
199 printf("%s Get syscon grf failed", __func__);
202 grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
204 priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
209 static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
211 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
212 struct mmc *mmc = host->mmc;
213 uint clock = mmc->tran_speed;
214 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
217 rk3399_emmc_phy_power_off(priv->phy);
219 sdhci_set_control_reg(host);
222 static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
224 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
225 struct mmc *mmc = host->mmc;
226 uint clock = mmc->tran_speed;
227 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
233 rk3399_emmc_phy_power_on(priv->phy, clock);
238 static int rk3568_emmc_phy_init(struct udevice *dev)
240 struct rockchip_sdhc *prv = dev_get_priv(dev);
241 struct sdhci_host *host = &prv->host;
244 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
245 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
250 static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock)
252 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
256 if (clock > host->max_clk)
257 clock = host->max_clk;
259 clk_set_rate(&priv->emmc_clk, clock);
261 sdhci_set_clock(host->mmc, clock);
263 if (clock >= 100 * MHz) {
265 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
267 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
269 /* Init DLL settings */
270 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
271 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
272 DWCMSHC_EMMC_DLL_START;
273 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
275 ret = read_poll_timeout(readl, host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
276 val, DLL_LOCK_WO_TMOUT(val), 1, 500);
280 extra = DWCMSHC_EMMC_DLL_DLYENA |
281 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
282 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
284 extra = DWCMSHC_EMMC_DLL_DLYENA |
285 DLL_TXCLK_TAPNUM_DEFAULT |
286 DLL_TXCLK_TAPNUM_FROM_SW;
287 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
289 extra = DWCMSHC_EMMC_DLL_DLYENA |
290 DLL_STRBIN_TAPNUM_DEFAULT;
291 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
293 /* reset the clock phase when the frequency is lower than 100MHz */
294 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
295 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
296 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
297 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
298 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
304 static int rk3568_emmc_get_phy(struct udevice *dev)
309 static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
311 struct mmc *mmc = host->mmc;
312 uint clock = mmc->tran_speed;
318 rk3568_sdhci_emmc_set_clock(host, clock);
320 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) {
321 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
322 reg &= ~SDHCI_CTRL_UHS_MASK;
323 reg |= SDHCI_CTRL_HS400;
324 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
326 sdhci_set_uhs_timing(host);
332 static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
334 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
335 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
337 if (data->set_control_reg)
338 data->set_control_reg(host);
341 static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
343 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
344 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
346 if (data->set_ios_post)
347 return data->set_ios_post(host);
352 static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
354 struct sdhci_host *host = dev_get_priv(mmc->dev);
355 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
360 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
361 ctrl |= SDHCI_CTRL_EXEC_TUNING;
362 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
364 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
365 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
367 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
368 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8)
369 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
370 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
371 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
374 cmd.resp_type = MMC_RSP_R1;
378 if (tuning_loop_counter-- == 0)
381 mmc_send_cmd(mmc, &cmd, NULL);
383 if (opcode == MMC_CMD_SEND_TUNING_BLOCK)
385 * For tuning command, do not do busy loop. As tuning
386 * is happening (CLK-DATA latching for setup/hold time
387 * requirements), give time to complete
391 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
392 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
394 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
395 printf("%s:Tuning failed\n", __func__);
399 if (tuning_loop_counter < 0) {
400 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
401 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
404 /* Enable only interrupts served by the SD controller */
405 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
406 /* Mask all sdhci interrupt sources */
407 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
412 static struct sdhci_ops rockchip_sdhci_ops = {
413 .set_ios_post = rockchip_sdhci_set_ios_post,
414 .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
415 .set_control_reg = rockchip_sdhci_set_control_reg,
418 static int rockchip_sdhci_probe(struct udevice *dev)
420 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
421 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
422 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
423 struct rockchip_sdhc *prv = dev_get_priv(dev);
424 struct mmc_config *cfg = &plat->cfg;
425 struct sdhci_host *host = &prv->host;
429 host->max_clk = cfg->f_max;
430 ret = clk_get_by_index(dev, 0, &clk);
432 ret = clk_set_rate(&clk, host->max_clk);
433 if (IS_ERR_VALUE(ret))
434 printf("%s clk set rate fail!\n", __func__);
436 printf("%s fail to get clk\n", __func__);
443 ret = data->get_phy(dev);
448 if (data->emmc_phy_init) {
449 ret = data->emmc_phy_init(dev);
454 host->ops = &rockchip_sdhci_ops;
455 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
457 host->mmc = &plat->mmc;
458 host->mmc->priv = &prv->host;
459 host->mmc->dev = dev;
460 upriv->mmc = host->mmc;
462 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
466 return sdhci_probe(dev);
469 static int rockchip_sdhci_of_to_plat(struct udevice *dev)
471 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
472 struct sdhci_host *host = dev_get_priv(dev);
473 struct mmc_config *cfg = &plat->cfg;
476 host->name = dev->name;
477 host->ioaddr = dev_read_addr_ptr(dev);
479 ret = mmc_of_parse(dev, cfg);
486 static int rockchip_sdhci_bind(struct udevice *dev)
488 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
490 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
493 static const struct sdhci_data rk3399_data = {
494 .get_phy = rk3399_emmc_get_phy,
495 .emmc_phy_init = rk3399_emmc_phy_init,
496 .set_control_reg = rk3399_sdhci_set_control_reg,
497 .set_ios_post = rk3399_sdhci_set_ios_post,
500 static const struct sdhci_data rk3568_data = {
501 .get_phy = rk3568_emmc_get_phy,
502 .emmc_phy_init = rk3568_emmc_phy_init,
503 .set_ios_post = rk3568_sdhci_set_ios_post,
506 static const struct udevice_id sdhci_ids[] = {
508 .compatible = "arasan,sdhci-5.1",
509 .data = (ulong)&rk3399_data,
512 .compatible = "rockchip,rk3568-dwcmshc",
513 .data = (ulong)&rk3568_data,
518 U_BOOT_DRIVER(arasan_sdhci_drv) = {
519 .name = "rockchip_sdhci_5_1",
521 .of_match = sdhci_ids,
522 .of_to_plat = rockchip_sdhci_of_to_plat,
524 .bind = rockchip_sdhci_bind,
525 .probe = rockchip_sdhci_probe,
526 .priv_auto = sizeof(struct rockchip_sdhc),
527 .plat_auto = sizeof(struct rockchip_sdhc_plat),