blob: c889c7bc9855fab4cf35811a7ceea2b351a9863d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang65922e02016-07-18 17:00:58 +08002/*
3 * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
4 *
5 * Rockchip SD Host Controller Interface
Kever Yang65922e02016-07-18 17:00:58 +08006 */
7
8#include <common.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +08009#include <clk.h>
Kever Yang65922e02016-07-18 17:00:58 +080010#include <dm.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080011#include <dm/ofnode.h>
Kever Yangdd99a022017-02-13 17:38:57 +080012#include <dt-structs.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080013#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <linux/err.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080016#include <linux/iopoll.h>
Kever Yang65922e02016-07-18 17:00:58 +080017#include <malloc.h>
Kever Yangdd99a022017-02-13 17:38:57 +080018#include <mapmem.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080019#include "mmc_private.h"
Kever Yang65922e02016-07-18 17:00:58 +080020#include <sdhci.h>
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080021#include <syscon.h>
22#include <asm/arch-rockchip/clock.h>
23#include <asm/arch-rockchip/hardware.h>
Kever Yang65922e02016-07-18 17:00:58 +080024
Alper Nebi Yasak6f198692022-03-15 20:46:28 +030025/* DWCMSHC specific Mode Select value */
26#define DWCMSHC_CTRL_HS400 0x7
Kever Yang65922e02016-07-18 17:00:58 +080027/* 400KHz is max freq for card ID etc. Use that as min */
28#define EMMC_MIN_FREQ 400000
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080029#define KHz (1000)
30#define MHz (1000 * KHz)
31#define SDHCI_TUNING_LOOP_COUNT 40
32
33#define PHYCTRL_CALDONE_MASK 0x1
34#define PHYCTRL_CALDONE_SHIFT 0x6
35#define PHYCTRL_CALDONE_DONE 0x1
36#define PHYCTRL_DLLRDY_MASK 0x1
37#define PHYCTRL_DLLRDY_SHIFT 0x5
38#define PHYCTRL_DLLRDY_DONE 0x1
39#define PHYCTRL_FREQSEL_200M 0x0
40#define PHYCTRL_FREQSEL_50M 0x1
41#define PHYCTRL_FREQSEL_100M 0x2
42#define PHYCTRL_FREQSEL_150M 0x3
43#define PHYCTRL_DLL_LOCK_WO_TMOUT(x) \
44 ((((x) >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK) ==\
45 PHYCTRL_DLLRDY_DONE)
Kever Yang65922e02016-07-18 17:00:58 +080046
Alper Nebi Yasak9099d032022-03-15 20:46:27 +030047#define ARASAN_VENDOR_REGISTER 0x78
48#define ARASAN_VENDOR_ENHANCED_STROBE BIT(0)
49
Jonas Karlman9168fbf2023-04-18 16:46:35 +000050/* Rockchip specific Registers */
51#define DWCMSHC_EMMC_EMMC_CTRL 0x52c
Alper Nebi Yasak6f198692022-03-15 20:46:28 +030052#define DWCMSHC_CARD_IS_EMMC BIT(0)
53#define DWCMSHC_ENHANCED_STROBE BIT(8)
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080054#define DWCMSHC_EMMC_DLL_CTRL 0x800
55#define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
56#define DWCMSHC_EMMC_DLL_RXCLK 0x804
57#define DWCMSHC_EMMC_DLL_TXCLK 0x808
58#define DWCMSHC_EMMC_DLL_STRBIN 0x80c
Jonas Karlmanee7115f2023-04-18 16:46:39 +000059#define DWCMSHC_EMMC_DLL_CMDOUT 0x810
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080060#define DWCMSHC_EMMC_DLL_STATUS0 0x840
61#define DWCMSHC_EMMC_DLL_STATUS1 0x844
62#define DWCMSHC_EMMC_DLL_START BIT(0)
Jonas Karlman9168fbf2023-04-18 16:46:35 +000063#define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
64#define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080065#define DWCMSHC_EMMC_DLL_START_POINT 16
66#define DWCMSHC_EMMC_DLL_START_DEFAULT 5
67#define DWCMSHC_EMMC_DLL_INC_VALUE 2
68#define DWCMSHC_EMMC_DLL_INC 8
Vasily Khoruzhickb58c6832023-03-08 17:28:30 -080069#define DWCMSHC_EMMC_DLL_BYPASS BIT(24)
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080070#define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
Jonas Karlman9168fbf2023-04-18 16:46:35 +000071#define DLL_RXCLK_NO_INVERTER BIT(29)
72#define DLL_RXCLK_ORI_GATE BIT(31)
Jonas Karlmanf4f60052023-04-18 16:46:37 +000073#define DLL_TXCLK_TAPNUM_DEFAULT 0x10
Jonas Karlman9168fbf2023-04-18 16:46:35 +000074#define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
Jonas Karlmanee7115f2023-04-18 16:46:39 +000075#define DLL_TXCLK_NO_INVERTER BIT(29)
Jonas Karlmanf4f60052023-04-18 16:46:37 +000076#define DLL_STRBIN_TAPNUM_DEFAULT 0x4
Alper Nebi Yasak6f198692022-03-15 20:46:28 +030077#define DLL_STRBIN_TAPNUM_FROM_SW BIT(24)
78#define DLL_STRBIN_DELAY_NUM_SEL BIT(26)
79#define DLL_STRBIN_DELAY_NUM_OFFSET 16
Jonas Karlmanf4f60052023-04-18 16:46:37 +000080#define DLL_STRBIN_DELAY_NUM_DEFAULT 0x10
Jonas Karlmanee7115f2023-04-18 16:46:39 +000081#define DLL_CMDOUT_TAPNUM_90_DEGREES 0x8
82#define DLL_CMDOUT_TAPNUM_FROM_SW BIT(24)
83#define DLL_CMDOUT_SRC_CLK_NEG BIT(28)
84#define DLL_CMDOUT_EN_SRC_CLK_NEG BIT(29)
85#define DLL_CMDOUT_BOTH_CLK_EDGE BIT(30)
Alper Nebi Yasak6f198692022-03-15 20:46:28 +030086
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080087#define DLL_LOCK_WO_TMOUT(x) \
88 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
89 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
90#define ROCKCHIP_MAX_CLKS 3
91
Jonas Karlmanee7115f2023-04-18 16:46:39 +000092#define FLAG_INVERTER_FLAG_IN_RXCLK BIT(0)
93
Kever Yang65922e02016-07-18 17:00:58 +080094struct rockchip_sdhc_plat {
95 struct mmc_config cfg;
96 struct mmc mmc;
97};
98
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080099struct rockchip_emmc_phy {
100 u32 emmcphy_con[7];
101 u32 reserved;
102 u32 emmcphy_status;
103};
104
Kever Yang65922e02016-07-18 17:00:58 +0800105struct rockchip_sdhc {
106 struct sdhci_host host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800107 struct udevice *dev;
Kever Yang65922e02016-07-18 17:00:58 +0800108 void *base;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800109 struct rockchip_emmc_phy *phy;
110 struct clk emmc_clk;
111};
112
113struct sdhci_data {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800114 int (*get_phy)(struct udevice *dev);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300115
116 /**
117 * set_control_reg() - Set SDHCI control registers
118 *
119 * This is the set_control_reg() SDHCI operation that should be
120 * used for the hardware this driver data is associated with.
121 * Normally, this is used to set up control registers for
122 * voltage level and UHS speed mode.
123 *
124 * @host: SDHCI host structure
125 */
126 void (*set_control_reg)(struct sdhci_host *host);
127
128 /**
129 * set_ios_post() - Host specific hook after set_ios() calls
130 *
131 * This is the set_ios_post() SDHCI operation that should be
132 * used for the hardware this driver data is associated with.
133 * Normally, this is a hook that is called after sdhci_set_ios()
134 * that does any necessary host-specific configuration.
135 *
136 * @host: SDHCI host structure
137 * Return: 0 if successful, -ve on error
138 */
139 int (*set_ios_post)(struct sdhci_host *host);
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300140
Jonas Karlmanea312462023-04-18 16:46:29 +0000141 void (*set_clock)(struct sdhci_host *host, u32 div);
142 int (*config_dll)(struct sdhci_host *host, u32 clock, bool enable);
143
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300144 /**
145 * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
146 *
147 * This is the set_enhanced_strobe() SDHCI operation that should
148 * be used for the hardware this driver data is associated with.
149 * Normally, this is used to set any host-specific configuration
150 * necessary for HS400 ES.
151 *
152 * @host: SDHCI host structure
153 * Return: 0 if successful, -ve on error
154 */
155 int (*set_enhanced_strobe)(struct sdhci_host *host);
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000156
157 u32 flags;
158 u8 hs200_txclk_tapnum;
159 u8 hs400_txclk_tapnum;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800160};
161
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800162static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
163{
164 u32 caldone, dllrdy, freqsel;
165
166 writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
167 writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
168 writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
169
170 /*
171 * According to the user manual, calpad calibration
172 * cycle takes more than 2us without the minimal recommended
173 * value, so we may need a little margin here
174 */
175 udelay(3);
176 writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
177
178 /*
179 * According to the user manual, it asks driver to
180 * wait 5us for calpad busy trimming. But it seems that
181 * 5us of caldone isn't enough for all cases.
182 */
183 udelay(500);
184 caldone = readl(&phy->emmcphy_status);
185 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
186 if (caldone != PHYCTRL_CALDONE_DONE) {
187 printf("%s: caldone timeout.\n", __func__);
188 return;
189 }
190
191 /* Set the frequency of the DLL operation */
192 if (clock < 75 * MHz)
193 freqsel = PHYCTRL_FREQSEL_50M;
194 else if (clock < 125 * MHz)
195 freqsel = PHYCTRL_FREQSEL_100M;
196 else if (clock < 175 * MHz)
197 freqsel = PHYCTRL_FREQSEL_150M;
198 else
199 freqsel = PHYCTRL_FREQSEL_200M;
200
201 /* Set the frequency of the DLL operation */
202 writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
203 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
204
Yifeng Zhao58ec23b2021-10-15 16:41:27 +0800205 /* REN Enable on STRB Line for HS400 */
206 writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
207
Ariel D'Alessandro85573612022-04-12 10:31:35 -0300208 read_poll_timeout(readl, dllrdy, PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1,
209 5000, &phy->emmcphy_status);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800210}
211
212static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
213{
214 writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
215 writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
216}
217
218static int rk3399_emmc_get_phy(struct udevice *dev)
219{
220 struct rockchip_sdhc *priv = dev_get_priv(dev);
221 ofnode phy_node;
222 void *grf_base;
223 u32 grf_phy_offset, phandle;
224
225 phandle = dev_read_u32_default(dev, "phys", 0);
226 phy_node = ofnode_get_by_phandle(phandle);
227 if (!ofnode_valid(phy_node)) {
228 debug("Not found emmc phy device\n");
229 return -ENODEV;
230 }
231
232 grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Haolin Li2ecb7492022-03-22 05:58:02 -0700233 if (IS_ERR_OR_NULL(grf_base)) {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800234 printf("%s Get syscon grf failed", __func__);
235 return -ENODEV;
236 }
237 grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
238
239 priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
240
241 return 0;
242}
243
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300244static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host)
245{
246 struct mmc *mmc = host->mmc;
247 u32 vendor;
248
249 vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER);
250 if (mmc->selected_mode == MMC_HS_400_ES)
251 vendor |= ARASAN_VENDOR_ENHANCED_STROBE;
252 else
253 vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE;
254 sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER);
255
256 return 0;
257}
258
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300259static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800260{
261 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300262 struct mmc *mmc = host->mmc;
263 uint clock = mmc->tran_speed;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800264 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
265
266 if (cycle_phy)
267 rk3399_emmc_phy_power_off(priv->phy);
268
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300269 sdhci_set_control_reg(host);
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300270
271 /*
272 * Reinitializing the device tries to set it to lower-speed modes
273 * first, which fails if the Enhanced Strobe bit is set, making
274 * the device impossible to use. Set the correct value here to
275 * let reinitialization attempts succeed.
276 */
277 if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT))
278 rk3399_sdhci_set_enhanced_strobe(host);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300279};
280
281static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
282{
283 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
284 struct mmc *mmc = host->mmc;
285 uint clock = mmc->tran_speed;
286 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
287
288 if (!clock)
289 clock = mmc->clock;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800290
291 if (cycle_phy)
292 rk3399_emmc_phy_power_on(priv->phy, clock);
293
294 return 0;
295}
296
Jonas Karlman78df6352023-04-20 15:55:15 +0000297static void rk3568_sdhci_set_clock(struct sdhci_host *host, u32 div)
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800298{
299 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
Jonas Karlman78df6352023-04-20 15:55:15 +0000300 struct mmc *mmc = host->mmc;
301 ulong rate;
302
303 rate = clk_set_rate(&priv->emmc_clk, mmc->clock);
304 if (IS_ERR_VALUE(rate))
305 printf("%s: Set clock rate failed: %ld\n", __func__, (long)rate);
306}
307
308static int rk3568_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
309{
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000310 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
311 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
312 struct mmc *mmc = host->mmc;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800313 int val, ret;
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000314 u32 extra, txclk_tapnum;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800315
Jonas Karlman68bc7022024-02-04 20:53:07 +0000316 if (!enable) {
317 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
Jonas Karlman78df6352023-04-20 15:55:15 +0000318 return 0;
Jonas Karlman68bc7022024-02-04 20:53:07 +0000319 }
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800320
321 if (clock >= 100 * MHz) {
322 /* reset DLL */
323 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
324 udelay(1);
325 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
326
327 /* Init DLL settings */
328 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
329 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
330 DWCMSHC_EMMC_DLL_START;
331 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
332
Ariel D'Alessandro85573612022-04-12 10:31:35 -0300333 ret = read_poll_timeout(readl, val, DLL_LOCK_WO_TMOUT(val), 1,
334 500,
335 host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800336 if (ret)
337 return ret;
338
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000339 extra = DWCMSHC_EMMC_DLL_DLYENA | DLL_RXCLK_ORI_GATE;
340 if (data->flags & FLAG_INVERTER_FLAG_IN_RXCLK)
341 extra |= DLL_RXCLK_NO_INVERTER;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800342 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
343
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000344 txclk_tapnum = data->hs200_txclk_tapnum;
345 if (mmc->selected_mode == MMC_HS_400 ||
346 mmc->selected_mode == MMC_HS_400_ES) {
347 txclk_tapnum = data->hs400_txclk_tapnum;
348
349 extra = DLL_CMDOUT_SRC_CLK_NEG |
350 DLL_CMDOUT_BOTH_CLK_EDGE |
351 DWCMSHC_EMMC_DLL_DLYENA |
352 DLL_CMDOUT_TAPNUM_90_DEGREES |
353 DLL_CMDOUT_TAPNUM_FROM_SW;
354 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CMDOUT);
355 }
356
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800357 extra = DWCMSHC_EMMC_DLL_DLYENA |
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000358 DLL_TXCLK_TAPNUM_FROM_SW |
359 DLL_TXCLK_NO_INVERTER |
360 txclk_tapnum;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800361 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
362
363 extra = DWCMSHC_EMMC_DLL_DLYENA |
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300364 DLL_STRBIN_TAPNUM_DEFAULT |
365 DLL_STRBIN_TAPNUM_FROM_SW;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800366 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
367 } else {
Vasily Khoruzhickb58c6832023-03-08 17:28:30 -0800368 /*
369 * Disable DLL and reset both of sample and drive clock.
370 * The bypass bit and start bit need to be set if DLL is not locked.
371 */
Jonas Karlman9168fbf2023-04-18 16:46:35 +0000372 extra = DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START;
373 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
Vasily Khoruzhickb58c6832023-03-08 17:28:30 -0800374 sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800375 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000376 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CMDOUT);
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300377 /*
378 * Before switching to hs400es mode, the driver will enable
379 * enhanced strobe first. PHY needs to configure the parameters
380 * of enhanced strobe first.
381 */
382 extra = DWCMSHC_EMMC_DLL_DLYENA |
383 DLL_STRBIN_DELAY_NUM_SEL |
384 DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
385 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800386 }
387
388 return 0;
389}
390
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300391static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800392{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800393 struct mmc *mmc = host->mmc;
Jonas Karlmanb42bdde2024-04-10 14:30:50 +0000394 struct rockchip_sdhc_plat *plat = dev_get_plat(mmc->dev);
395 struct mmc_config *cfg = &plat->cfg;
Jonas Karlman9168fbf2023-04-18 16:46:35 +0000396 u32 reg;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800397
Jonas Karlmandbe75fbe2023-04-18 16:46:33 +0000398 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
399 reg &= ~SDHCI_CTRL_UHS_MASK;
400
401 switch (mmc->selected_mode) {
402 case UHS_SDR25:
403 case MMC_HS:
404 case MMC_HS_52:
405 reg |= SDHCI_CTRL_UHS_SDR25;
406 break;
407 case UHS_SDR50:
408 reg |= SDHCI_CTRL_UHS_SDR50;
409 break;
410 case UHS_DDR50:
411 case MMC_DDR_52:
412 reg |= SDHCI_CTRL_UHS_DDR50;
413 break;
414 case UHS_SDR104:
415 case MMC_HS_200:
416 reg |= SDHCI_CTRL_UHS_SDR104;
417 break;
418 case MMC_HS_400:
419 case MMC_HS_400_ES:
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300420 reg |= DWCMSHC_CTRL_HS400;
Jonas Karlmandbe75fbe2023-04-18 16:46:33 +0000421 break;
422 default:
423 reg |= SDHCI_CTRL_UHS_SDR12;
424 }
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300425
Jonas Karlmandbe75fbe2023-04-18 16:46:33 +0000426 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
427
Jonas Karlman9168fbf2023-04-18 16:46:35 +0000428 reg = sdhci_readw(host, DWCMSHC_EMMC_EMMC_CTRL);
Jonas Karlmandbe75fbe2023-04-18 16:46:33 +0000429
430 if (IS_MMC(mmc))
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300431 reg |= DWCMSHC_CARD_IS_EMMC;
Jonas Karlmandbe75fbe2023-04-18 16:46:33 +0000432 else
433 reg &= ~DWCMSHC_CARD_IS_EMMC;
434
435 if (mmc->selected_mode == MMC_HS_400_ES)
436 reg |= DWCMSHC_ENHANCED_STROBE;
437 else
438 reg &= ~DWCMSHC_ENHANCED_STROBE;
439
Jonas Karlman9168fbf2023-04-18 16:46:35 +0000440 sdhci_writew(host, reg, DWCMSHC_EMMC_EMMC_CTRL);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800441
Jonas Karlmanb42bdde2024-04-10 14:30:50 +0000442 /*
443 * Reading more than 4 blocks with a single CMD18 command in PIO mode
444 * triggers Data End Bit Error using a slower mode than HS200. Limit to
445 * reading max 4 blocks in one command when using PIO mode.
446 */
447 if (!(host->flags & USE_DMA)) {
448 if (mmc->selected_mode == MMC_HS_200 ||
449 mmc->selected_mode == MMC_HS_400 ||
450 mmc->selected_mode == MMC_HS_400_ES)
451 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
452 else
453 cfg->b_max = 4;
454 }
455
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800456 return 0;
457}
458
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300459static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
460{
461 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
462 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
463
464 if (data->set_control_reg)
465 data->set_control_reg(host);
466}
467
468static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
469{
470 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
471 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
472
473 if (data->set_ios_post)
474 return data->set_ios_post(host);
475
476 return 0;
477}
478
Jonas Karlmanea312462023-04-18 16:46:29 +0000479static void rockchip_sdhci_set_clock(struct sdhci_host *host, u32 div)
480{
481 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
482 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
483
484 if (data->set_clock)
485 data->set_clock(host, div);
486}
487
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800488static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
489{
Jonas Karlman99f38882023-04-18 16:46:26 +0000490 struct rockchip_sdhc *priv = dev_get_priv(mmc->dev);
491 struct sdhci_host *host = &priv->host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800492 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
493 struct mmc_cmd cmd;
494 u32 ctrl, blk_size;
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000495 int ret;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800496
497 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
498 ctrl |= SDHCI_CTRL_EXEC_TUNING;
499 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
500
501 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800502
503 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000504 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && mmc->bus_width == 8)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800505 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
506 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
507 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
508
509 cmd.cmdidx = opcode;
510 cmd.resp_type = MMC_RSP_R1;
511 cmd.cmdarg = 0;
512
513 do {
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000514 ret = mmc_send_cmd(mmc, &cmd, NULL);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800515 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000516 if (ret || tuning_loop_counter-- == 0)
517 break;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800518 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
519
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000520 if (ret || tuning_loop_counter < 0 || !(ctrl & SDHCI_CTRL_TUNED_CLK)) {
521 if (!ret)
522 ret = -EIO;
523 printf("%s: Tuning failed: %d\n", __func__, ret);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800524
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800525 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
Jonas Karlmanfdea3712023-04-18 16:46:31 +0000526 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
527 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800528 }
529
530 /* Enable only interrupts served by the SD controller */
531 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800532
533 return ret;
534}
535
Jonas Karlmanea312462023-04-18 16:46:29 +0000536static int rockchip_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
537{
538 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
539 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
540
541 if (data->config_dll)
542 return data->config_dll(host, clock, enable);
543
544 return 0;
545}
546
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300547static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
548{
549 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
550 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
551
552 if (data->set_enhanced_strobe)
553 return data->set_enhanced_strobe(host);
554
Jonas Karlmandd2707f2023-04-18 16:46:34 +0000555 return 0;
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300556}
557
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800558static struct sdhci_ops rockchip_sdhci_ops = {
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300559 .set_control_reg = rockchip_sdhci_set_control_reg,
Jonas Karlmanea312462023-04-18 16:46:29 +0000560 .set_ios_post = rockchip_sdhci_set_ios_post,
561 .set_clock = rockchip_sdhci_set_clock,
562 .platform_execute_tuning = rockchip_sdhci_execute_tuning,
563 .config_dll = rockchip_sdhci_config_dll,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300564 .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
Kever Yang65922e02016-07-18 17:00:58 +0800565};
566
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800567static int rockchip_sdhci_probe(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800568{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800569 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800570 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700571 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Jonas Karlman99f38882023-04-18 16:46:26 +0000572 struct rockchip_sdhc *priv = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800573 struct mmc_config *cfg = &plat->cfg;
Jonas Karlman99f38882023-04-18 16:46:26 +0000574 struct sdhci_host *host = &priv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800575 struct clk clk;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800576 int ret;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800577
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800578 host->max_clk = cfg->f_max;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800579 ret = clk_get_by_index(dev, 0, &clk);
580 if (!ret) {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800581 ret = clk_set_rate(&clk, host->max_clk);
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800582 if (IS_ERR_VALUE(ret))
583 printf("%s clk set rate fail!\n", __func__);
584 } else {
585 printf("%s fail to get clk\n", __func__);
586 }
Kever Yang65922e02016-07-18 17:00:58 +0800587
Jonas Karlman99f38882023-04-18 16:46:26 +0000588 priv->emmc_clk = clk;
589 priv->dev = dev;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800590
591 if (data->get_phy) {
592 ret = data->get_phy(dev);
593 if (ret)
594 return ret;
595 }
596
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800597 host->ops = &rockchip_sdhci_ops;
Kever Yang65922e02016-07-18 17:00:58 +0800598 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
599
Kever Yang65922e02016-07-18 17:00:58 +0800600 host->mmc = &plat->mmc;
Jonas Karlman99f38882023-04-18 16:46:26 +0000601 host->mmc->priv = &priv->host;
Kever Yang65922e02016-07-18 17:00:58 +0800602 host->mmc->dev = dev;
603 upriv->mmc = host->mmc;
604
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800605 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
Kever Yang36d9bf82019-07-19 18:01:11 +0800606 if (ret)
607 return ret;
608
Jonas Karlman5d259362023-04-18 16:46:45 +0000609 /*
Jonas Karlmanf79c5372023-05-06 17:41:11 +0000610 * Disable use of DMA and force use of PIO mode in SPL to fix an issue
611 * where loading part of TF-A into SRAM using DMA silently fails.
612 */
613 if (IS_ENABLED(CONFIG_SPL_BUILD) &&
614 dev_read_bool(dev, "u-boot,spl-fifo-mode"))
615 host->flags &= ~USE_DMA;
616
Kever Yang65922e02016-07-18 17:00:58 +0800617 return sdhci_probe(dev);
618}
619
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800620static int rockchip_sdhci_of_to_plat(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800621{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800622 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Jonas Karlman99f38882023-04-18 16:46:26 +0000623 struct rockchip_sdhc *priv = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800624 struct mmc_config *cfg = &plat->cfg;
Jonas Karlman99f38882023-04-18 16:46:26 +0000625 struct sdhci_host *host = &priv->host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800626 int ret;
Kever Yang65922e02016-07-18 17:00:58 +0800627
628 host->name = dev->name;
Philipp Tomsichdbb28282017-09-11 22:04:21 +0200629 host->ioaddr = dev_read_addr_ptr(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800630
631 ret = mmc_of_parse(dev, cfg);
632 if (ret)
633 return ret;
Kever Yang65922e02016-07-18 17:00:58 +0800634
635 return 0;
636}
637
638static int rockchip_sdhci_bind(struct udevice *dev)
639{
Simon Glassfa20e932020-12-03 16:55:20 -0700640 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800641
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900642 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +0800643}
644
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800645static const struct sdhci_data rk3399_data = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800646 .get_phy = rk3399_emmc_get_phy,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300647 .set_control_reg = rk3399_sdhci_set_control_reg,
648 .set_ios_post = rk3399_sdhci_set_ios_post,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300649 .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800650};
651
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800652static const struct sdhci_data rk3568_data = {
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300653 .set_ios_post = rk3568_sdhci_set_ios_post,
Jonas Karlman78df6352023-04-20 15:55:15 +0000654 .set_clock = rk3568_sdhci_set_clock,
655 .config_dll = rk3568_sdhci_config_dll,
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000656 .flags = FLAG_INVERTER_FLAG_IN_RXCLK,
657 .hs200_txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT,
Jonas Karlman68bc7022024-02-04 20:53:07 +0000658 .hs400_txclk_tapnum = 0x8,
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800659};
660
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000661static const struct sdhci_data rk3588_data = {
662 .set_ios_post = rk3568_sdhci_set_ios_post,
663 .set_clock = rk3568_sdhci_set_clock,
664 .config_dll = rk3568_sdhci_config_dll,
665 .hs200_txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT,
Jonas Karlman68bc7022024-02-04 20:53:07 +0000666 .hs400_txclk_tapnum = 0x9,
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000667};
668
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800669static const struct udevice_id sdhci_ids[] = {
670 {
671 .compatible = "arasan,sdhci-5.1",
672 .data = (ulong)&rk3399_data,
673 },
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800674 {
675 .compatible = "rockchip,rk3568-dwcmshc",
676 .data = (ulong)&rk3568_data,
677 },
Jonas Karlmanee7115f2023-04-18 16:46:39 +0000678 {
679 .compatible = "rockchip,rk3588-dwcmshc",
680 .data = (ulong)&rk3588_data,
681 },
Kever Yang65922e02016-07-18 17:00:58 +0800682 { }
683};
684
685U_BOOT_DRIVER(arasan_sdhci_drv) = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800686 .name = "rockchip_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800687 .id = UCLASS_MMC,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800688 .of_match = sdhci_ids,
689 .of_to_plat = rockchip_sdhci_of_to_plat,
Kever Yang65922e02016-07-18 17:00:58 +0800690 .ops = &sdhci_ops,
691 .bind = rockchip_sdhci_bind,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800692 .probe = rockchip_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700693 .priv_auto = sizeof(struct rockchip_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700694 .plat_auto = sizeof(struct rockchip_sdhc_plat),
Kever Yang65922e02016-07-18 17:00:58 +0800695};