blob: f4d5a59036a22457ab9f25f68d4f6b998be1739f [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
25/* 400KHz is max freq for card ID etc. Use that as min */
26#define EMMC_MIN_FREQ 400000
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080027#define KHz (1000)
28#define MHz (1000 * KHz)
29#define SDHCI_TUNING_LOOP_COUNT 40
30
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) ==\
43 PHYCTRL_DLLRDY_DONE)
Kever Yang65922e02016-07-18 17:00:58 +080044
Alper Nebi Yasak9099d032022-03-15 20:46:27 +030045#define ARASAN_VENDOR_REGISTER 0x78
46#define ARASAN_VENDOR_ENHANCED_STROBE BIT(0)
47
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080048/* Rockchip specific Registers */
49#define DWCMSHC_EMMC_DLL_CTRL 0x800
50#define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
51#define DWCMSHC_EMMC_DLL_RXCLK 0x804
52#define DWCMSHC_EMMC_DLL_TXCLK 0x808
53#define DWCMSHC_EMMC_DLL_STRBIN 0x80c
54#define DWCMSHC_EMMC_DLL_STATUS0 0x840
55#define DWCMSHC_EMMC_DLL_STATUS1 0x844
56#define DWCMSHC_EMMC_DLL_START BIT(0)
57#define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29
58#define DWCMSHC_EMMC_DLL_START_POINT 16
59#define DWCMSHC_EMMC_DLL_START_DEFAULT 5
60#define DWCMSHC_EMMC_DLL_INC_VALUE 2
61#define DWCMSHC_EMMC_DLL_INC 8
62#define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
63#define DLL_TXCLK_TAPNUM_DEFAULT 0x10
64#define DLL_STRBIN_TAPNUM_DEFAULT 0x3
65#define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
66#define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
67#define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
68#define DLL_RXCLK_NO_INVERTER 1
69#define DLL_RXCLK_INVERTER 0
70#define DWCMSHC_ENHANCED_STROBE BIT(8)
71#define DLL_LOCK_WO_TMOUT(x) \
72 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
73 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
74#define ROCKCHIP_MAX_CLKS 3
75
Kever Yang65922e02016-07-18 17:00:58 +080076struct rockchip_sdhc_plat {
77 struct mmc_config cfg;
78 struct mmc mmc;
79};
80
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080081struct rockchip_emmc_phy {
82 u32 emmcphy_con[7];
83 u32 reserved;
84 u32 emmcphy_status;
85};
86
Kever Yang65922e02016-07-18 17:00:58 +080087struct rockchip_sdhc {
88 struct sdhci_host host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080089 struct udevice *dev;
Kever Yang65922e02016-07-18 17:00:58 +080090 void *base;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080091 struct rockchip_emmc_phy *phy;
92 struct clk emmc_clk;
93};
94
95struct sdhci_data {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080096 int (*emmc_phy_init)(struct udevice *dev);
97 int (*get_phy)(struct udevice *dev);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +030098
99 /**
100 * set_control_reg() - Set SDHCI control registers
101 *
102 * This is the set_control_reg() SDHCI operation that should be
103 * used for the hardware this driver data is associated with.
104 * Normally, this is used to set up control registers for
105 * voltage level and UHS speed mode.
106 *
107 * @host: SDHCI host structure
108 */
109 void (*set_control_reg)(struct sdhci_host *host);
110
111 /**
112 * set_ios_post() - Host specific hook after set_ios() calls
113 *
114 * This is the set_ios_post() SDHCI operation that should be
115 * used for the hardware this driver data is associated with.
116 * Normally, this is a hook that is called after sdhci_set_ios()
117 * that does any necessary host-specific configuration.
118 *
119 * @host: SDHCI host structure
120 * Return: 0 if successful, -ve on error
121 */
122 int (*set_ios_post)(struct sdhci_host *host);
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300123
124 /**
125 * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
126 *
127 * This is the set_enhanced_strobe() SDHCI operation that should
128 * be used for the hardware this driver data is associated with.
129 * Normally, this is used to set any host-specific configuration
130 * necessary for HS400 ES.
131 *
132 * @host: SDHCI host structure
133 * Return: 0 if successful, -ve on error
134 */
135 int (*set_enhanced_strobe)(struct sdhci_host *host);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800136};
137
138static int rk3399_emmc_phy_init(struct udevice *dev)
139{
140 return 0;
141}
142
143static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
144{
145 u32 caldone, dllrdy, freqsel;
146
147 writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
148 writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
149 writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
150
151 /*
152 * According to the user manual, calpad calibration
153 * cycle takes more than 2us without the minimal recommended
154 * value, so we may need a little margin here
155 */
156 udelay(3);
157 writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
158
159 /*
160 * According to the user manual, it asks driver to
161 * wait 5us for calpad busy trimming. But it seems that
162 * 5us of caldone isn't enough for all cases.
163 */
164 udelay(500);
165 caldone = readl(&phy->emmcphy_status);
166 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
167 if (caldone != PHYCTRL_CALDONE_DONE) {
168 printf("%s: caldone timeout.\n", __func__);
169 return;
170 }
171
172 /* Set the frequency of the DLL operation */
173 if (clock < 75 * MHz)
174 freqsel = PHYCTRL_FREQSEL_50M;
175 else if (clock < 125 * MHz)
176 freqsel = PHYCTRL_FREQSEL_100M;
177 else if (clock < 175 * MHz)
178 freqsel = PHYCTRL_FREQSEL_150M;
179 else
180 freqsel = PHYCTRL_FREQSEL_200M;
181
182 /* Set the frequency of the DLL operation */
183 writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
184 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
185
Yifeng Zhao58ec23b2021-10-15 16:41:27 +0800186 /* REN Enable on STRB Line for HS400 */
187 writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
188
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800189 read_poll_timeout(readl, &phy->emmcphy_status, dllrdy,
190 PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1, 5000);
191}
192
193static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
194{
195 writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
196 writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
197}
198
199static int rk3399_emmc_get_phy(struct udevice *dev)
200{
201 struct rockchip_sdhc *priv = dev_get_priv(dev);
202 ofnode phy_node;
203 void *grf_base;
204 u32 grf_phy_offset, phandle;
205
206 phandle = dev_read_u32_default(dev, "phys", 0);
207 phy_node = ofnode_get_by_phandle(phandle);
208 if (!ofnode_valid(phy_node)) {
209 debug("Not found emmc phy device\n");
210 return -ENODEV;
211 }
212
213 grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
214 if (grf_base < 0) {
215 printf("%s Get syscon grf failed", __func__);
216 return -ENODEV;
217 }
218 grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
219
220 priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
221
222 return 0;
223}
224
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300225static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host)
226{
227 struct mmc *mmc = host->mmc;
228 u32 vendor;
229
230 vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER);
231 if (mmc->selected_mode == MMC_HS_400_ES)
232 vendor |= ARASAN_VENDOR_ENHANCED_STROBE;
233 else
234 vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE;
235 sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER);
236
237 return 0;
238}
239
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300240static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800241{
242 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300243 struct mmc *mmc = host->mmc;
244 uint clock = mmc->tran_speed;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800245 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
246
247 if (cycle_phy)
248 rk3399_emmc_phy_power_off(priv->phy);
249
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300250 sdhci_set_control_reg(host);
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300251
252 /*
253 * Reinitializing the device tries to set it to lower-speed modes
254 * first, which fails if the Enhanced Strobe bit is set, making
255 * the device impossible to use. Set the correct value here to
256 * let reinitialization attempts succeed.
257 */
258 if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT))
259 rk3399_sdhci_set_enhanced_strobe(host);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300260};
261
262static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
263{
264 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
265 struct mmc *mmc = host->mmc;
266 uint clock = mmc->tran_speed;
267 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
268
269 if (!clock)
270 clock = mmc->clock;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800271
272 if (cycle_phy)
273 rk3399_emmc_phy_power_on(priv->phy, clock);
274
275 return 0;
276}
277
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800278static int rk3568_emmc_phy_init(struct udevice *dev)
279{
280 struct rockchip_sdhc *prv = dev_get_priv(dev);
281 struct sdhci_host *host = &prv->host;
282 u32 extra;
283
284 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
285 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
286
287 return 0;
288}
289
290static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock)
291{
292 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
293 int val, ret;
294 u32 extra;
295
296 if (clock > host->max_clk)
297 clock = host->max_clk;
298 if (clock)
299 clk_set_rate(&priv->emmc_clk, clock);
300
301 sdhci_set_clock(host->mmc, clock);
302
303 if (clock >= 100 * MHz) {
304 /* reset DLL */
305 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
306 udelay(1);
307 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
308
309 /* Init DLL settings */
310 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
311 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
312 DWCMSHC_EMMC_DLL_START;
313 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
314
315 ret = read_poll_timeout(readl, host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
316 val, DLL_LOCK_WO_TMOUT(val), 1, 500);
317 if (ret)
318 return ret;
319
320 extra = DWCMSHC_EMMC_DLL_DLYENA |
321 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
322 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
323
324 extra = DWCMSHC_EMMC_DLL_DLYENA |
325 DLL_TXCLK_TAPNUM_DEFAULT |
326 DLL_TXCLK_TAPNUM_FROM_SW;
327 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
328
329 extra = DWCMSHC_EMMC_DLL_DLYENA |
330 DLL_STRBIN_TAPNUM_DEFAULT;
331 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
332 } else {
333 /* reset the clock phase when the frequency is lower than 100MHz */
334 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
335 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
336 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
337 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
338 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
339 }
340
341 return 0;
342}
343
344static int rk3568_emmc_get_phy(struct udevice *dev)
345{
346 return 0;
347}
348
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300349static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800350{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800351 struct mmc *mmc = host->mmc;
352 uint clock = mmc->tran_speed;
353 u32 reg;
354
355 if (!clock)
356 clock = mmc->clock;
357
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300358 rk3568_sdhci_emmc_set_clock(host, clock);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800359
360 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) {
361 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
362 reg &= ~SDHCI_CTRL_UHS_MASK;
363 reg |= SDHCI_CTRL_HS400;
364 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
365 } else {
366 sdhci_set_uhs_timing(host);
367 }
368
369 return 0;
370}
371
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300372static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
373{
374 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
375 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
376
377 if (data->set_control_reg)
378 data->set_control_reg(host);
379}
380
381static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
382{
383 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
384 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
385
386 if (data->set_ios_post)
387 return data->set_ios_post(host);
388
389 return 0;
390}
391
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800392static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
393{
394 struct sdhci_host *host = dev_get_priv(mmc->dev);
395 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
396 struct mmc_cmd cmd;
397 u32 ctrl, blk_size;
398 int ret = 0;
399
400 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
401 ctrl |= SDHCI_CTRL_EXEC_TUNING;
402 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
403
404 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
405 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
406
407 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
408 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8)
409 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
410 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
411 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
412
413 cmd.cmdidx = opcode;
414 cmd.resp_type = MMC_RSP_R1;
415 cmd.cmdarg = 0;
416
417 do {
418 if (tuning_loop_counter-- == 0)
419 break;
420
421 mmc_send_cmd(mmc, &cmd, NULL);
422
423 if (opcode == MMC_CMD_SEND_TUNING_BLOCK)
424 /*
425 * For tuning command, do not do busy loop. As tuning
426 * is happening (CLK-DATA latching for setup/hold time
427 * requirements), give time to complete
428 */
429 udelay(1);
430
431 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
432 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
433
434 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
435 printf("%s:Tuning failed\n", __func__);
436 ret = -EIO;
437 }
438
439 if (tuning_loop_counter < 0) {
440 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
441 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
442 }
443
444 /* Enable only interrupts served by the SD controller */
445 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
446 /* Mask all sdhci interrupt sources */
447 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
448
449 return ret;
450}
451
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300452static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
453{
454 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
455 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
456
457 if (data->set_enhanced_strobe)
458 return data->set_enhanced_strobe(host);
459
460 return -ENOTSUPP;
461}
462
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800463static struct sdhci_ops rockchip_sdhci_ops = {
464 .set_ios_post = rockchip_sdhci_set_ios_post,
465 .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300466 .set_control_reg = rockchip_sdhci_set_control_reg,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300467 .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
Kever Yang65922e02016-07-18 17:00:58 +0800468};
469
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800470static int rockchip_sdhci_probe(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800471{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800472 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800473 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700474 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800475 struct rockchip_sdhc *prv = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800476 struct mmc_config *cfg = &plat->cfg;
Kever Yang65922e02016-07-18 17:00:58 +0800477 struct sdhci_host *host = &prv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800478 struct clk clk;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800479 int ret;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800480
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800481 host->max_clk = cfg->f_max;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800482 ret = clk_get_by_index(dev, 0, &clk);
483 if (!ret) {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800484 ret = clk_set_rate(&clk, host->max_clk);
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800485 if (IS_ERR_VALUE(ret))
486 printf("%s clk set rate fail!\n", __func__);
487 } else {
488 printf("%s fail to get clk\n", __func__);
489 }
Kever Yang65922e02016-07-18 17:00:58 +0800490
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800491 prv->emmc_clk = clk;
492 prv->dev = dev;
493
494 if (data->get_phy) {
495 ret = data->get_phy(dev);
496 if (ret)
497 return ret;
498 }
499
500 if (data->emmc_phy_init) {
501 ret = data->emmc_phy_init(dev);
502 if (ret)
503 return ret;
504 }
505
506 host->ops = &rockchip_sdhci_ops;
Kever Yang65922e02016-07-18 17:00:58 +0800507 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
508
Kever Yang65922e02016-07-18 17:00:58 +0800509 host->mmc = &plat->mmc;
Kever Yang65922e02016-07-18 17:00:58 +0800510 host->mmc->priv = &prv->host;
511 host->mmc->dev = dev;
512 upriv->mmc = host->mmc;
513
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800514 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
Kever Yang36d9bf82019-07-19 18:01:11 +0800515 if (ret)
516 return ret;
517
Kever Yang65922e02016-07-18 17:00:58 +0800518 return sdhci_probe(dev);
519}
520
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800521static int rockchip_sdhci_of_to_plat(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800522{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800523 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800524 struct sdhci_host *host = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800525 struct mmc_config *cfg = &plat->cfg;
526 int ret;
Kever Yang65922e02016-07-18 17:00:58 +0800527
528 host->name = dev->name;
Philipp Tomsichdbb28282017-09-11 22:04:21 +0200529 host->ioaddr = dev_read_addr_ptr(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800530
531 ret = mmc_of_parse(dev, cfg);
532 if (ret)
533 return ret;
Kever Yang65922e02016-07-18 17:00:58 +0800534
535 return 0;
536}
537
538static int rockchip_sdhci_bind(struct udevice *dev)
539{
Simon Glassfa20e932020-12-03 16:55:20 -0700540 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800541
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900542 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +0800543}
544
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800545static const struct sdhci_data rk3399_data = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800546 .get_phy = rk3399_emmc_get_phy,
547 .emmc_phy_init = rk3399_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300548 .set_control_reg = rk3399_sdhci_set_control_reg,
549 .set_ios_post = rk3399_sdhci_set_ios_post,
Alper Nebi Yasak9099d032022-03-15 20:46:27 +0300550 .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800551};
552
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800553static const struct sdhci_data rk3568_data = {
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800554 .get_phy = rk3568_emmc_get_phy,
555 .emmc_phy_init = rk3568_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300556 .set_ios_post = rk3568_sdhci_set_ios_post,
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800557};
558
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800559static const struct udevice_id sdhci_ids[] = {
560 {
561 .compatible = "arasan,sdhci-5.1",
562 .data = (ulong)&rk3399_data,
563 },
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800564 {
565 .compatible = "rockchip,rk3568-dwcmshc",
566 .data = (ulong)&rk3568_data,
567 },
Kever Yang65922e02016-07-18 17:00:58 +0800568 { }
569};
570
571U_BOOT_DRIVER(arasan_sdhci_drv) = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800572 .name = "rockchip_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800573 .id = UCLASS_MMC,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800574 .of_match = sdhci_ids,
575 .of_to_plat = rockchip_sdhci_of_to_plat,
Kever Yang65922e02016-07-18 17:00:58 +0800576 .ops = &sdhci_ops,
577 .bind = rockchip_sdhci_bind,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800578 .probe = rockchip_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700579 .priv_auto = sizeof(struct rockchip_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700580 .plat_auto = sizeof(struct rockchip_sdhc_plat),
Kever Yang65922e02016-07-18 17:00:58 +0800581};