blob: e1409dd2c749ce2144fe4e2a6c42581769390ea0 [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
Alper Nebi Yasak6f198692022-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 Zhaoe5dddfa2021-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 Khoruzhickb58c6832023-03-08 17:28:30 -080064#define DECMSHC_EMMC_DLL_CMDOUT 0x810
Yifeng Zhaoe5dddfa2021-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 Khoruzhickb58c6832023-03-08 17:28:30 -080073#define DWCMSHC_EMMC_DLL_BYPASS BIT(24)
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080074#define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
Alper Nebi Yasak6f198692022-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 Zhaoe5dddfa2021-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 Khoruzhickb58c6832023-03-08 17:28:30 -080088#define DLL_RXCLK_ORI_GATE BIT(31)
Yifeng Zhaoe5dddfa2021-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 Yang65922e02016-07-18 17:00:58 +080095struct rockchip_sdhc_plat {
96 struct mmc_config cfg;
97 struct mmc mmc;
98};
99
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800100struct rockchip_emmc_phy {
101 u32 emmcphy_con[7];
102 u32 reserved;
103 u32 emmcphy_status;
104};
105
Kever Yang65922e02016-07-18 17:00:58 +0800106struct rockchip_sdhc {
107 struct sdhci_host host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800108 struct udevice *dev;
Kever Yang65922e02016-07-18 17:00:58 +0800109 void *base;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800110 struct rockchip_emmc_phy *phy;
111 struct clk emmc_clk;
112};
113
114struct sdhci_data {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800115 int (*emmc_phy_init)(struct udevice *dev);
116 int (*get_phy)(struct udevice *dev);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300117
118 /**
119 * set_control_reg() - Set SDHCI control registers
120 *
121 * This is the set_control_reg() SDHCI operation that should be
122 * used for the hardware this driver data is associated with.
123 * Normally, this is used to set up control registers for
124 * voltage level and UHS speed mode.
125 *
126 * @host: SDHCI host structure
127 */
128 void (*set_control_reg)(struct sdhci_host *host);
129
130 /**
131 * set_ios_post() - Host specific hook after set_ios() calls
132 *
133 * This is the set_ios_post() SDHCI operation that should be
134 * used for the hardware this driver data is associated with.
135 * Normally, this is a hook that is called after sdhci_set_ios()
136 * that does any necessary host-specific configuration.
137 *
138 * @host: SDHCI host structure
139 * Return: 0 if successful, -ve on error
140 */
141 int (*set_ios_post)(struct sdhci_host *host);
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300142
143 /**
144 * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
145 *
146 * This is the set_enhanced_strobe() SDHCI operation that should
147 * be used for the hardware this driver data is associated with.
148 * Normally, this is used to set any host-specific configuration
149 * necessary for HS400 ES.
150 *
151 * @host: SDHCI host structure
152 * Return: 0 if successful, -ve on error
153 */
154 int (*set_enhanced_strobe)(struct sdhci_host *host);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800155};
156
157static int rk3399_emmc_phy_init(struct udevice *dev)
158{
159 return 0;
160}
161
162static 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
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800297static int rk3568_emmc_phy_init(struct udevice *dev)
298{
299 struct rockchip_sdhc *prv = dev_get_priv(dev);
300 struct sdhci_host *host = &prv->host;
301 u32 extra;
302
303 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
304 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
305
306 return 0;
307}
308
309static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock)
310{
311 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
312 int val, ret;
313 u32 extra;
314
315 if (clock > host->max_clk)
316 clock = host->max_clk;
317 if (clock)
318 clk_set_rate(&priv->emmc_clk, clock);
319
320 sdhci_set_clock(host->mmc, clock);
321
322 if (clock >= 100 * MHz) {
323 /* reset DLL */
324 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
325 udelay(1);
326 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
327
328 /* Init DLL settings */
329 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
330 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
331 DWCMSHC_EMMC_DLL_START;
332 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
333
Ariel D'Alessandro85573612022-04-12 10:31:35 -0300334 ret = read_poll_timeout(readl, val, DLL_LOCK_WO_TMOUT(val), 1,
335 500,
336 host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800337 if (ret)
338 return ret;
339
340 extra = DWCMSHC_EMMC_DLL_DLYENA |
341 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
342 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
343
344 extra = DWCMSHC_EMMC_DLL_DLYENA |
345 DLL_TXCLK_TAPNUM_DEFAULT |
346 DLL_TXCLK_TAPNUM_FROM_SW;
347 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
348
349 extra = DWCMSHC_EMMC_DLL_DLYENA |
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300350 DLL_STRBIN_TAPNUM_DEFAULT |
351 DLL_STRBIN_TAPNUM_FROM_SW;
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800352 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
353 } else {
Vasily Khoruzhickb58c6832023-03-08 17:28:30 -0800354 /*
355 * Disable DLL and reset both of sample and drive clock.
356 * The bypass bit and start bit need to be set if DLL is not locked.
357 */
358 sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START,
359 DWCMSHC_EMMC_DLL_CTRL);
360 sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
361 sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800362 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300363 /*
364 * Before switching to hs400es mode, the driver will enable
365 * enhanced strobe first. PHY needs to configure the parameters
366 * of enhanced strobe first.
367 */
368 extra = DWCMSHC_EMMC_DLL_DLYENA |
369 DLL_STRBIN_DELAY_NUM_SEL |
370 DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
371 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800372 }
373
374 return 0;
375}
376
377static int rk3568_emmc_get_phy(struct udevice *dev)
378{
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300379 return 0;
380}
381
382static int rk3568_sdhci_set_enhanced_strobe(struct sdhci_host *host)
383{
384 struct mmc *mmc = host->mmc;
385 u32 vendor;
386 int reg;
387
388 reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK)
389 + DWCMSHC_EMMC_CONTROL;
390
391 vendor = sdhci_readl(host, reg);
392 if (mmc->selected_mode == MMC_HS_400_ES)
393 vendor |= DWCMSHC_ENHANCED_STROBE;
394 else
395 vendor &= ~DWCMSHC_ENHANCED_STROBE;
396 sdhci_writel(host, vendor, reg);
397
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800398 return 0;
399}
400
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300401static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800402{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800403 struct mmc *mmc = host->mmc;
404 uint clock = mmc->tran_speed;
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300405 u32 reg, vendor_reg;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800406
407 if (!clock)
408 clock = mmc->clock;
409
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300410 rk3568_sdhci_emmc_set_clock(host, clock);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800411
412 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) {
413 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
414 reg &= ~SDHCI_CTRL_UHS_MASK;
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300415 reg |= DWCMSHC_CTRL_HS400;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800416 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300417
418 vendor_reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK)
419 + DWCMSHC_EMMC_CONTROL;
420 /* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */
421 reg = sdhci_readw(host, vendor_reg);
422 reg |= DWCMSHC_CARD_IS_EMMC;
423 sdhci_writew(host, reg, vendor_reg);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800424 } else {
425 sdhci_set_uhs_timing(host);
426 }
427
428 return 0;
429}
430
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300431static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
432{
433 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
434 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
435
436 if (data->set_control_reg)
437 data->set_control_reg(host);
438}
439
440static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
441{
442 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
443 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
444
445 if (data->set_ios_post)
446 return data->set_ios_post(host);
447
448 return 0;
449}
450
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800451static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
452{
453 struct sdhci_host *host = dev_get_priv(mmc->dev);
454 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
455 struct mmc_cmd cmd;
456 u32 ctrl, blk_size;
457 int ret = 0;
458
459 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
460 ctrl |= SDHCI_CTRL_EXEC_TUNING;
461 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
462
463 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
464 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
465
466 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
467 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8)
468 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
469 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
470 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
471
472 cmd.cmdidx = opcode;
473 cmd.resp_type = MMC_RSP_R1;
474 cmd.cmdarg = 0;
475
476 do {
477 if (tuning_loop_counter-- == 0)
478 break;
479
480 mmc_send_cmd(mmc, &cmd, NULL);
481
482 if (opcode == MMC_CMD_SEND_TUNING_BLOCK)
483 /*
484 * For tuning command, do not do busy loop. As tuning
485 * is happening (CLK-DATA latching for setup/hold time
486 * requirements), give time to complete
487 */
488 udelay(1);
489
490 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
491 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
492
493 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
494 printf("%s:Tuning failed\n", __func__);
495 ret = -EIO;
496 }
497
498 if (tuning_loop_counter < 0) {
499 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
500 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
501 }
502
503 /* Enable only interrupts served by the SD controller */
504 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
505 /* Mask all sdhci interrupt sources */
506 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
507
508 return ret;
509}
510
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300511static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
512{
513 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
514 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
515
516 if (data->set_enhanced_strobe)
517 return data->set_enhanced_strobe(host);
518
519 return -ENOTSUPP;
520}
521
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800522static struct sdhci_ops rockchip_sdhci_ops = {
523 .set_ios_post = rockchip_sdhci_set_ios_post,
524 .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300525 .set_control_reg = rockchip_sdhci_set_control_reg,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300526 .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
Kever Yang65922e02016-07-18 17:00:58 +0800527};
528
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800529static int rockchip_sdhci_probe(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800530{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800531 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800532 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700533 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800534 struct rockchip_sdhc *prv = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800535 struct mmc_config *cfg = &plat->cfg;
Kever Yang65922e02016-07-18 17:00:58 +0800536 struct sdhci_host *host = &prv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800537 struct clk clk;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800538 int ret;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800539
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800540 host->max_clk = cfg->f_max;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800541 ret = clk_get_by_index(dev, 0, &clk);
542 if (!ret) {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800543 ret = clk_set_rate(&clk, host->max_clk);
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800544 if (IS_ERR_VALUE(ret))
545 printf("%s clk set rate fail!\n", __func__);
546 } else {
547 printf("%s fail to get clk\n", __func__);
548 }
Kever Yang65922e02016-07-18 17:00:58 +0800549
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800550 prv->emmc_clk = clk;
551 prv->dev = dev;
552
553 if (data->get_phy) {
554 ret = data->get_phy(dev);
555 if (ret)
556 return ret;
557 }
558
559 if (data->emmc_phy_init) {
560 ret = data->emmc_phy_init(dev);
561 if (ret)
562 return ret;
563 }
564
565 host->ops = &rockchip_sdhci_ops;
Kever Yang65922e02016-07-18 17:00:58 +0800566 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
567
Kever Yang65922e02016-07-18 17:00:58 +0800568 host->mmc = &plat->mmc;
Kever Yang65922e02016-07-18 17:00:58 +0800569 host->mmc->priv = &prv->host;
570 host->mmc->dev = dev;
571 upriv->mmc = host->mmc;
572
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800573 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
Kever Yang36d9bf82019-07-19 18:01:11 +0800574 if (ret)
575 return ret;
576
Kever Yang65922e02016-07-18 17:00:58 +0800577 return sdhci_probe(dev);
578}
579
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800580static int rockchip_sdhci_of_to_plat(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800581{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800582 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800583 struct sdhci_host *host = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800584 struct mmc_config *cfg = &plat->cfg;
585 int ret;
Kever Yang65922e02016-07-18 17:00:58 +0800586
587 host->name = dev->name;
Philipp Tomsichdbb28282017-09-11 22:04:21 +0200588 host->ioaddr = dev_read_addr_ptr(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800589
590 ret = mmc_of_parse(dev, cfg);
591 if (ret)
592 return ret;
Kever Yang65922e02016-07-18 17:00:58 +0800593
594 return 0;
595}
596
597static int rockchip_sdhci_bind(struct udevice *dev)
598{
Simon Glassfa20e932020-12-03 16:55:20 -0700599 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800600
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900601 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +0800602}
603
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800604static const struct sdhci_data rk3399_data = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800605 .get_phy = rk3399_emmc_get_phy,
606 .emmc_phy_init = rk3399_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300607 .set_control_reg = rk3399_sdhci_set_control_reg,
608 .set_ios_post = rk3399_sdhci_set_ios_post,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300609 .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800610};
611
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800612static const struct sdhci_data rk3568_data = {
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800613 .get_phy = rk3568_emmc_get_phy,
614 .emmc_phy_init = rk3568_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300615 .set_ios_post = rk3568_sdhci_set_ios_post,
Alper Nebi Yasak6f198692022-03-15 20:46:28 +0300616 .set_enhanced_strobe = rk3568_sdhci_set_enhanced_strobe,
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800617};
618
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800619static const struct udevice_id sdhci_ids[] = {
620 {
621 .compatible = "arasan,sdhci-5.1",
622 .data = (ulong)&rk3399_data,
623 },
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800624 {
625 .compatible = "rockchip,rk3568-dwcmshc",
626 .data = (ulong)&rk3568_data,
627 },
Kever Yang65922e02016-07-18 17:00:58 +0800628 { }
629};
630
631U_BOOT_DRIVER(arasan_sdhci_drv) = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800632 .name = "rockchip_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800633 .id = UCLASS_MMC,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800634 .of_match = sdhci_ids,
635 .of_to_plat = rockchip_sdhci_of_to_plat,
Kever Yang65922e02016-07-18 17:00:58 +0800636 .ops = &sdhci_ops,
637 .bind = rockchip_sdhci_bind,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800638 .probe = rockchip_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700639 .priv_auto = sizeof(struct rockchip_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700640 .plat_auto = sizeof(struct rockchip_sdhc_plat),
Kever Yang65922e02016-07-18 17:00:58 +0800641};