blob: e64c0fdb4cc81858bb64314c12a7457fdf69212d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang79c83062016-07-18 17:00:58 +08002/*
3 * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
4 *
5 * Rockchip SD Host Controller Interface
Kever Yang79c83062016-07-18 17:00:58 +08006 */
7
8#include <common.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +08009#include <clk.h>
Kever Yang79c83062016-07-18 17:00:58 +080010#include <dm.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +080011#include <dm/ofnode.h>
Kever Yangc2868212017-02-13 17:38:57 +080012#include <dt-structs.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +080013#include <linux/delay.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <linux/err.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +080016#include <linux/iopoll.h>
Kever Yang79c83062016-07-18 17:00:58 +080017#include <malloc.h>
Kever Yangc2868212017-02-13 17:38:57 +080018#include <mapmem.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +080019#include "mmc_private.h"
Kever Yang79c83062016-07-18 17:00:58 +080020#include <sdhci.h>
Yifeng Zhaoac804142021-06-29 16:24:41 +080021#include <syscon.h>
22#include <asm/arch-rockchip/clock.h>
23#include <asm/arch-rockchip/hardware.h>
Kever Yang79c83062016-07-18 17:00:58 +080024
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +030025/* DWCMSHC specific Mode Select value */
26#define DWCMSHC_CTRL_HS400 0x7
Kever Yang79c83062016-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 Zhaoac804142021-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 Yang79c83062016-07-18 17:00:58 +080046
Alper Nebi Yasakc35af782022-03-15 20:46:27 +030047#define ARASAN_VENDOR_REGISTER 0x78
48#define ARASAN_VENDOR_ENHANCED_STROBE BIT(0)
49
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +030050/* DWC IP vendor area 1 pointer */
51#define DWCMSHC_P_VENDOR_AREA1 0xe8
52#define DWCMSHC_AREA1_MASK GENMASK(11, 0)
53/* Offset inside the vendor area 1 */
54#define DWCMSHC_EMMC_CONTROL 0x2c
55#define DWCMSHC_CARD_IS_EMMC BIT(0)
56#define DWCMSHC_ENHANCED_STROBE BIT(8)
57
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +080058/* Rockchip specific Registers */
59#define DWCMSHC_EMMC_DLL_CTRL 0x800
60#define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
61#define DWCMSHC_EMMC_DLL_RXCLK 0x804
62#define DWCMSHC_EMMC_DLL_TXCLK 0x808
63#define DWCMSHC_EMMC_DLL_STRBIN 0x80c
Vasily Khoruzhick2321a992023-03-08 17:28:30 -080064#define DECMSHC_EMMC_DLL_CMDOUT 0x810
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +080065#define DWCMSHC_EMMC_DLL_STATUS0 0x840
66#define DWCMSHC_EMMC_DLL_STATUS1 0x844
67#define DWCMSHC_EMMC_DLL_START BIT(0)
68#define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29
69#define DWCMSHC_EMMC_DLL_START_POINT 16
70#define DWCMSHC_EMMC_DLL_START_DEFAULT 5
71#define DWCMSHC_EMMC_DLL_INC_VALUE 2
72#define DWCMSHC_EMMC_DLL_INC 8
Vasily Khoruzhick2321a992023-03-08 17:28:30 -080073#define DWCMSHC_EMMC_DLL_BYPASS BIT(24)
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +080074#define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +030075#define DLL_TXCLK_TAPNUM_DEFAULT 0xA
76
77#define DLL_STRBIN_TAPNUM_DEFAULT 0x8
78#define DLL_STRBIN_TAPNUM_FROM_SW BIT(24)
79#define DLL_STRBIN_DELAY_NUM_SEL BIT(26)
80#define DLL_STRBIN_DELAY_NUM_OFFSET 16
81#define DLL_STRBIN_DELAY_NUM_DEFAULT 0x16
82
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +080083#define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
84#define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
85#define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
86#define DLL_RXCLK_NO_INVERTER 1
87#define DLL_RXCLK_INVERTER 0
Vasily Khoruzhick2321a992023-03-08 17:28:30 -080088#define DLL_RXCLK_ORI_GATE BIT(31)
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +080089#define DWCMSHC_ENHANCED_STROBE BIT(8)
90#define DLL_LOCK_WO_TMOUT(x) \
91 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
92 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
93#define ROCKCHIP_MAX_CLKS 3
94
Kever Yang79c83062016-07-18 17:00:58 +080095struct rockchip_sdhc_plat {
96 struct mmc_config cfg;
97 struct mmc mmc;
98};
99
Yifeng Zhaoac804142021-06-29 16:24:41 +0800100struct rockchip_emmc_phy {
101 u32 emmcphy_con[7];
102 u32 reserved;
103 u32 emmcphy_status;
Kever Yang79c83062016-07-18 17:00:58 +0800104};
105
Yifeng Zhaoac804142021-06-29 16:24:41 +0800106struct rockchip_sdhc {
107 struct sdhci_host host;
108 struct udevice *dev;
109 void *base;
110 struct rockchip_emmc_phy *phy;
111 struct clk emmc_clk;
112};
113
114struct sdhci_data {
Yifeng Zhaoac804142021-06-29 16:24:41 +0800115 int (*get_phy)(struct udevice *dev);
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300116
117 /**
118 * set_control_reg() - Set SDHCI control registers
119 *
120 * This is the set_control_reg() SDHCI operation that should be
121 * used for the hardware this driver data is associated with.
122 * Normally, this is used to set up control registers for
123 * voltage level and UHS speed mode.
124 *
125 * @host: SDHCI host structure
126 */
127 void (*set_control_reg)(struct sdhci_host *host);
128
129 /**
130 * set_ios_post() - Host specific hook after set_ios() calls
131 *
132 * This is the set_ios_post() SDHCI operation that should be
133 * used for the hardware this driver data is associated with.
134 * Normally, this is a hook that is called after sdhci_set_ios()
135 * that does any necessary host-specific configuration.
136 *
137 * @host: SDHCI host structure
138 * Return: 0 if successful, -ve on error
139 */
140 int (*set_ios_post)(struct sdhci_host *host);
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300141
142 /**
143 * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
144 *
145 * This is the set_enhanced_strobe() SDHCI operation that should
146 * be used for the hardware this driver data is associated with.
147 * Normally, this is used to set any host-specific configuration
148 * necessary for HS400 ES.
149 *
150 * @host: SDHCI host structure
151 * Return: 0 if successful, -ve on error
152 */
153 int (*set_enhanced_strobe)(struct sdhci_host *host);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800154};
155
Yifeng Zhaoac804142021-06-29 16:24:41 +0800156static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
157{
158 u32 caldone, dllrdy, freqsel;
159
160 writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
161 writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
162 writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
163
164 /*
165 * According to the user manual, calpad calibration
166 * cycle takes more than 2us without the minimal recommended
167 * value, so we may need a little margin here
168 */
169 udelay(3);
170 writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
171
172 /*
173 * According to the user manual, it asks driver to
174 * wait 5us for calpad busy trimming. But it seems that
175 * 5us of caldone isn't enough for all cases.
176 */
177 udelay(500);
178 caldone = readl(&phy->emmcphy_status);
179 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
180 if (caldone != PHYCTRL_CALDONE_DONE) {
181 printf("%s: caldone timeout.\n", __func__);
182 return;
183 }
184
185 /* Set the frequency of the DLL operation */
186 if (clock < 75 * MHz)
187 freqsel = PHYCTRL_FREQSEL_50M;
188 else if (clock < 125 * MHz)
189 freqsel = PHYCTRL_FREQSEL_100M;
190 else if (clock < 175 * MHz)
191 freqsel = PHYCTRL_FREQSEL_150M;
192 else
193 freqsel = PHYCTRL_FREQSEL_200M;
194
195 /* Set the frequency of the DLL operation */
196 writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
197 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
198
Yifeng Zhao022f5522021-10-15 16:41:27 +0800199 /* REN Enable on STRB Line for HS400 */
200 writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
201
Ariel D'Alessandrod7db0e62022-04-12 10:31:35 -0300202 read_poll_timeout(readl, dllrdy, PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1,
203 5000, &phy->emmcphy_status);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800204}
205
206static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
207{
208 writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
209 writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
210}
211
212static int rk3399_emmc_get_phy(struct udevice *dev)
213{
214 struct rockchip_sdhc *priv = dev_get_priv(dev);
215 ofnode phy_node;
216 void *grf_base;
217 u32 grf_phy_offset, phandle;
218
219 phandle = dev_read_u32_default(dev, "phys", 0);
220 phy_node = ofnode_get_by_phandle(phandle);
221 if (!ofnode_valid(phy_node)) {
222 debug("Not found emmc phy device\n");
223 return -ENODEV;
224 }
225
226 grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Haolin Lifdda7902022-03-22 05:58:02 -0700227 if (IS_ERR_OR_NULL(grf_base)) {
Yifeng Zhaoac804142021-06-29 16:24:41 +0800228 printf("%s Get syscon grf failed", __func__);
229 return -ENODEV;
230 }
231 grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
232
233 priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
234
235 return 0;
236}
237
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300238static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host)
239{
240 struct mmc *mmc = host->mmc;
241 u32 vendor;
242
243 vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER);
244 if (mmc->selected_mode == MMC_HS_400_ES)
245 vendor |= ARASAN_VENDOR_ENHANCED_STROBE;
246 else
247 vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE;
248 sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER);
249
250 return 0;
251}
252
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300253static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
Yifeng Zhaoac804142021-06-29 16:24:41 +0800254{
255 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300256 struct mmc *mmc = host->mmc;
257 uint clock = mmc->tran_speed;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800258 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
259
260 if (cycle_phy)
261 rk3399_emmc_phy_power_off(priv->phy);
262
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300263 sdhci_set_control_reg(host);
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300264
265 /*
266 * Reinitializing the device tries to set it to lower-speed modes
267 * first, which fails if the Enhanced Strobe bit is set, making
268 * the device impossible to use. Set the correct value here to
269 * let reinitialization attempts succeed.
270 */
271 if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT))
272 rk3399_sdhci_set_enhanced_strobe(host);
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300273};
274
275static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
276{
277 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
278 struct mmc *mmc = host->mmc;
279 uint clock = mmc->tran_speed;
280 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
281
282 if (!clock)
283 clock = mmc->clock;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800284
285 if (cycle_phy)
286 rk3399_emmc_phy_power_on(priv->phy, clock);
287
288 return 0;
289}
290
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800291static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock)
292{
293 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
294 int val, ret;
295 u32 extra;
296
297 if (clock > host->max_clk)
298 clock = host->max_clk;
299 if (clock)
300 clk_set_rate(&priv->emmc_clk, clock);
301
302 sdhci_set_clock(host->mmc, clock);
303
304 if (clock >= 100 * MHz) {
305 /* reset DLL */
306 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
307 udelay(1);
308 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
309
310 /* Init DLL settings */
311 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
312 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
313 DWCMSHC_EMMC_DLL_START;
314 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
315
Ariel D'Alessandrod7db0e62022-04-12 10:31:35 -0300316 ret = read_poll_timeout(readl, val, DLL_LOCK_WO_TMOUT(val), 1,
317 500,
318 host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0);
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800319 if (ret)
320 return ret;
321
322 extra = DWCMSHC_EMMC_DLL_DLYENA |
323 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
324 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
325
326 extra = DWCMSHC_EMMC_DLL_DLYENA |
327 DLL_TXCLK_TAPNUM_DEFAULT |
328 DLL_TXCLK_TAPNUM_FROM_SW;
329 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
330
331 extra = DWCMSHC_EMMC_DLL_DLYENA |
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300332 DLL_STRBIN_TAPNUM_DEFAULT |
333 DLL_STRBIN_TAPNUM_FROM_SW;
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800334 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
335 } else {
Vasily Khoruzhick2321a992023-03-08 17:28:30 -0800336 /*
337 * Disable DLL and reset both of sample and drive clock.
338 * The bypass bit and start bit need to be set if DLL is not locked.
339 */
340 sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START,
341 DWCMSHC_EMMC_DLL_CTRL);
342 sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
343 sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800344 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300345 /*
346 * Before switching to hs400es mode, the driver will enable
347 * enhanced strobe first. PHY needs to configure the parameters
348 * of enhanced strobe first.
349 */
350 extra = DWCMSHC_EMMC_DLL_DLYENA |
351 DLL_STRBIN_DELAY_NUM_SEL |
352 DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
353 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800354 }
355
356 return 0;
357}
358
359static int rk3568_emmc_get_phy(struct udevice *dev)
360{
361 return 0;
362}
363
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300364static int rk3568_sdhci_set_enhanced_strobe(struct sdhci_host *host)
365{
366 struct mmc *mmc = host->mmc;
367 u32 vendor;
368 int reg;
369
370 reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK)
371 + DWCMSHC_EMMC_CONTROL;
372
373 vendor = sdhci_readl(host, reg);
374 if (mmc->selected_mode == MMC_HS_400_ES)
375 vendor |= DWCMSHC_ENHANCED_STROBE;
376 else
377 vendor &= ~DWCMSHC_ENHANCED_STROBE;
378 sdhci_writel(host, vendor, reg);
379
380 return 0;
381}
382
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300383static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
Yifeng Zhaoac804142021-06-29 16:24:41 +0800384{
Yifeng Zhaoac804142021-06-29 16:24:41 +0800385 struct mmc *mmc = host->mmc;
Vasily Khoruzhick77867102023-03-07 13:26:46 -0800386 uint clock = mmc->clock;
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300387 u32 reg, vendor_reg;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800388
Vasily Khoruzhick77867102023-03-07 13:26:46 -0800389 if (mmc->tran_speed && mmc->clock > mmc->tran_speed)
390 clock = mmc->tran_speed;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800391
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300392 rk3568_sdhci_emmc_set_clock(host, clock);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800393
394 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) {
395 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
396 reg &= ~SDHCI_CTRL_UHS_MASK;
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300397 reg |= DWCMSHC_CTRL_HS400;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800398 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300399
400 vendor_reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK)
401 + DWCMSHC_EMMC_CONTROL;
402 /* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */
403 reg = sdhci_readw(host, vendor_reg);
404 reg |= DWCMSHC_CARD_IS_EMMC;
405 sdhci_writew(host, reg, vendor_reg);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800406 } else {
407 sdhci_set_uhs_timing(host);
408 }
409
410 return 0;
411}
412
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300413static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
414{
415 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
416 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
417
418 if (data->set_control_reg)
419 data->set_control_reg(host);
420}
421
422static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
423{
424 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
425 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
426
427 if (data->set_ios_post)
428 return data->set_ios_post(host);
429
430 return 0;
431}
432
Yifeng Zhaoac804142021-06-29 16:24:41 +0800433static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
434{
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000435 struct rockchip_sdhc *priv = dev_get_priv(mmc->dev);
436 struct sdhci_host *host = &priv->host;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800437 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
438 struct mmc_cmd cmd;
439 u32 ctrl, blk_size;
440 int ret = 0;
441
442 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
443 ctrl |= SDHCI_CTRL_EXEC_TUNING;
444 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
445
446 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
447 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
448
449 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
450 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8)
451 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
452 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
453 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
454
455 cmd.cmdidx = opcode;
456 cmd.resp_type = MMC_RSP_R1;
457 cmd.cmdarg = 0;
458
459 do {
460 if (tuning_loop_counter-- == 0)
461 break;
462
463 mmc_send_cmd(mmc, &cmd, NULL);
464
465 if (opcode == MMC_CMD_SEND_TUNING_BLOCK)
466 /*
467 * For tuning command, do not do busy loop. As tuning
468 * is happening (CLK-DATA latching for setup/hold time
469 * requirements), give time to complete
470 */
471 udelay(1);
472
473 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
474 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
475
476 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
477 printf("%s:Tuning failed\n", __func__);
478 ret = -EIO;
479 }
480
481 if (tuning_loop_counter < 0) {
482 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
483 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
484 }
485
486 /* Enable only interrupts served by the SD controller */
487 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
488 /* Mask all sdhci interrupt sources */
489 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
490
491 return ret;
492}
493
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300494static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
495{
496 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
497 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
498
499 if (data->set_enhanced_strobe)
500 return data->set_enhanced_strobe(host);
501
502 return -ENOTSUPP;
503}
504
Yifeng Zhaoac804142021-06-29 16:24:41 +0800505static struct sdhci_ops rockchip_sdhci_ops = {
506 .set_ios_post = rockchip_sdhci_set_ios_post,
507 .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300508 .set_control_reg = rockchip_sdhci_set_control_reg,
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300509 .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
Yifeng Zhaoac804142021-06-29 16:24:41 +0800510};
511
512static int rockchip_sdhci_probe(struct udevice *dev)
513{
514 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
Kever Yang79c83062016-07-18 17:00:58 +0800515 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700516 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000517 struct rockchip_sdhc *priv = dev_get_priv(dev);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800518 struct mmc_config *cfg = &plat->cfg;
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000519 struct sdhci_host *host = &priv->host;
Kever Yang39fbb562016-12-28 11:32:35 +0800520 struct clk clk;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800521 int ret;
Kever Yang39fbb562016-12-28 11:32:35 +0800522
Yifeng Zhaoac804142021-06-29 16:24:41 +0800523 host->max_clk = cfg->f_max;
Kever Yang39fbb562016-12-28 11:32:35 +0800524 ret = clk_get_by_index(dev, 0, &clk);
525 if (!ret) {
Yifeng Zhaoac804142021-06-29 16:24:41 +0800526 ret = clk_set_rate(&clk, host->max_clk);
Kever Yang39fbb562016-12-28 11:32:35 +0800527 if (IS_ERR_VALUE(ret))
528 printf("%s clk set rate fail!\n", __func__);
529 } else {
530 printf("%s fail to get clk\n", __func__);
531 }
Kever Yang79c83062016-07-18 17:00:58 +0800532
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000533 priv->emmc_clk = clk;
534 priv->dev = dev;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800535
536 if (data->get_phy) {
537 ret = data->get_phy(dev);
538 if (ret)
539 return ret;
540 }
541
Yifeng Zhaoac804142021-06-29 16:24:41 +0800542 host->ops = &rockchip_sdhci_ops;
Kever Yang79c83062016-07-18 17:00:58 +0800543 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
544
Kever Yang79c83062016-07-18 17:00:58 +0800545 host->mmc = &plat->mmc;
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000546 host->mmc->priv = &priv->host;
Kever Yang79c83062016-07-18 17:00:58 +0800547 host->mmc->dev = dev;
548 upriv->mmc = host->mmc;
549
Yifeng Zhaoac804142021-06-29 16:24:41 +0800550 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
Kever Yang4dcdc5c2019-07-19 18:01:11 +0800551 if (ret)
552 return ret;
553
Kever Yang79c83062016-07-18 17:00:58 +0800554 return sdhci_probe(dev);
555}
556
Yifeng Zhaoac804142021-06-29 16:24:41 +0800557static int rockchip_sdhci_of_to_plat(struct udevice *dev)
Kever Yang79c83062016-07-18 17:00:58 +0800558{
Yifeng Zhaoac804142021-06-29 16:24:41 +0800559 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000560 struct rockchip_sdhc *priv = dev_get_priv(dev);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800561 struct mmc_config *cfg = &plat->cfg;
Jonas Karlmanb8c394b2023-04-18 16:46:26 +0000562 struct sdhci_host *host = &priv->host;
Yifeng Zhaoac804142021-06-29 16:24:41 +0800563 int ret;
Kever Yang79c83062016-07-18 17:00:58 +0800564
565 host->name = dev->name;
Philipp Tomsich327b2b32017-09-11 22:04:21 +0200566 host->ioaddr = dev_read_addr_ptr(dev);
Yifeng Zhaoac804142021-06-29 16:24:41 +0800567
568 ret = mmc_of_parse(dev, cfg);
569 if (ret)
570 return ret;
Kever Yang79c83062016-07-18 17:00:58 +0800571
572 return 0;
573}
574
575static int rockchip_sdhci_bind(struct udevice *dev)
576{
Simon Glassc69cda22020-12-03 16:55:20 -0700577 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang79c83062016-07-18 17:00:58 +0800578
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900579 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang79c83062016-07-18 17:00:58 +0800580}
581
Yifeng Zhaoac804142021-06-29 16:24:41 +0800582static const struct sdhci_data rk3399_data = {
Yifeng Zhaoac804142021-06-29 16:24:41 +0800583 .get_phy = rk3399_emmc_get_phy,
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300584 .set_control_reg = rk3399_sdhci_set_control_reg,
585 .set_ios_post = rk3399_sdhci_set_ios_post,
Alper Nebi Yasakc35af782022-03-15 20:46:27 +0300586 .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
Yifeng Zhaoac804142021-06-29 16:24:41 +0800587};
588
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800589static const struct sdhci_data rk3568_data = {
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800590 .get_phy = rk3568_emmc_get_phy,
Alper Nebi Yasakee5a2842022-01-29 01:42:37 +0300591 .set_ios_post = rk3568_sdhci_set_ios_post,
Alper Nebi Yasakc48021d2022-03-15 20:46:28 +0300592 .set_enhanced_strobe = rk3568_sdhci_set_enhanced_strobe,
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800593};
594
Yifeng Zhaoac804142021-06-29 16:24:41 +0800595static const struct udevice_id sdhci_ids[] = {
596 {
597 .compatible = "arasan,sdhci-5.1",
598 .data = (ulong)&rk3399_data,
599 },
Yifeng Zhaoa63a57e2021-06-29 16:24:42 +0800600 {
601 .compatible = "rockchip,rk3568-dwcmshc",
602 .data = (ulong)&rk3568_data,
603 },
Kever Yang79c83062016-07-18 17:00:58 +0800604 { }
605};
606
607U_BOOT_DRIVER(arasan_sdhci_drv) = {
Yifeng Zhaoac804142021-06-29 16:24:41 +0800608 .name = "rockchip_sdhci_5_1",
Kever Yang79c83062016-07-18 17:00:58 +0800609 .id = UCLASS_MMC,
Yifeng Zhaoac804142021-06-29 16:24:41 +0800610 .of_match = sdhci_ids,
611 .of_to_plat = rockchip_sdhci_of_to_plat,
Kever Yang79c83062016-07-18 17:00:58 +0800612 .ops = &sdhci_ops,
613 .bind = rockchip_sdhci_bind,
Yifeng Zhaoac804142021-06-29 16:24:41 +0800614 .probe = rockchip_sdhci_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700615 .priv_auto = sizeof(struct rockchip_sdhc),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700616 .plat_auto = sizeof(struct rockchip_sdhc_plat),
Kever Yang79c83062016-07-18 17:00:58 +0800617};