blob: b91df05de4ffd4a67bc0b0404c479728b137dba0 [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
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +080045/* Rockchip specific Registers */
46#define DWCMSHC_EMMC_DLL_CTRL 0x800
47#define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
48#define DWCMSHC_EMMC_DLL_RXCLK 0x804
49#define DWCMSHC_EMMC_DLL_TXCLK 0x808
50#define DWCMSHC_EMMC_DLL_STRBIN 0x80c
51#define DWCMSHC_EMMC_DLL_STATUS0 0x840
52#define DWCMSHC_EMMC_DLL_STATUS1 0x844
53#define DWCMSHC_EMMC_DLL_START BIT(0)
54#define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29
55#define DWCMSHC_EMMC_DLL_START_POINT 16
56#define DWCMSHC_EMMC_DLL_START_DEFAULT 5
57#define DWCMSHC_EMMC_DLL_INC_VALUE 2
58#define DWCMSHC_EMMC_DLL_INC 8
59#define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
60#define DLL_TXCLK_TAPNUM_DEFAULT 0x10
61#define DLL_STRBIN_TAPNUM_DEFAULT 0x3
62#define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
63#define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
64#define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
65#define DLL_RXCLK_NO_INVERTER 1
66#define DLL_RXCLK_INVERTER 0
67#define DWCMSHC_ENHANCED_STROBE BIT(8)
68#define DLL_LOCK_WO_TMOUT(x) \
69 ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
70 (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
71#define ROCKCHIP_MAX_CLKS 3
72
Kever Yang65922e02016-07-18 17:00:58 +080073struct rockchip_sdhc_plat {
74 struct mmc_config cfg;
75 struct mmc mmc;
76};
77
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080078struct rockchip_emmc_phy {
79 u32 emmcphy_con[7];
80 u32 reserved;
81 u32 emmcphy_status;
82};
83
Kever Yang65922e02016-07-18 17:00:58 +080084struct rockchip_sdhc {
85 struct sdhci_host host;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080086 struct udevice *dev;
Kever Yang65922e02016-07-18 17:00:58 +080087 void *base;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080088 struct rockchip_emmc_phy *phy;
89 struct clk emmc_clk;
90};
91
92struct sdhci_data {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +080093 int (*emmc_phy_init)(struct udevice *dev);
94 int (*get_phy)(struct udevice *dev);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +030095
96 /**
97 * set_control_reg() - Set SDHCI control registers
98 *
99 * This is the set_control_reg() SDHCI operation that should be
100 * used for the hardware this driver data is associated with.
101 * Normally, this is used to set up control registers for
102 * voltage level and UHS speed mode.
103 *
104 * @host: SDHCI host structure
105 */
106 void (*set_control_reg)(struct sdhci_host *host);
107
108 /**
109 * set_ios_post() - Host specific hook after set_ios() calls
110 *
111 * This is the set_ios_post() SDHCI operation that should be
112 * used for the hardware this driver data is associated with.
113 * Normally, this is a hook that is called after sdhci_set_ios()
114 * that does any necessary host-specific configuration.
115 *
116 * @host: SDHCI host structure
117 * Return: 0 if successful, -ve on error
118 */
119 int (*set_ios_post)(struct sdhci_host *host);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800120};
121
122static int rk3399_emmc_phy_init(struct udevice *dev)
123{
124 return 0;
125}
126
127static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
128{
129 u32 caldone, dllrdy, freqsel;
130
131 writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
132 writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
133 writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
134
135 /*
136 * According to the user manual, calpad calibration
137 * cycle takes more than 2us without the minimal recommended
138 * value, so we may need a little margin here
139 */
140 udelay(3);
141 writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
142
143 /*
144 * According to the user manual, it asks driver to
145 * wait 5us for calpad busy trimming. But it seems that
146 * 5us of caldone isn't enough for all cases.
147 */
148 udelay(500);
149 caldone = readl(&phy->emmcphy_status);
150 caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
151 if (caldone != PHYCTRL_CALDONE_DONE) {
152 printf("%s: caldone timeout.\n", __func__);
153 return;
154 }
155
156 /* Set the frequency of the DLL operation */
157 if (clock < 75 * MHz)
158 freqsel = PHYCTRL_FREQSEL_50M;
159 else if (clock < 125 * MHz)
160 freqsel = PHYCTRL_FREQSEL_100M;
161 else if (clock < 175 * MHz)
162 freqsel = PHYCTRL_FREQSEL_150M;
163 else
164 freqsel = PHYCTRL_FREQSEL_200M;
165
166 /* Set the frequency of the DLL operation */
167 writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
168 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
169
Yifeng Zhao58ec23b2021-10-15 16:41:27 +0800170 /* REN Enable on STRB Line for HS400 */
171 writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
172
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800173 read_poll_timeout(readl, &phy->emmcphy_status, dllrdy,
174 PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1, 5000);
175}
176
177static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
178{
179 writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
180 writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
181}
182
183static int rk3399_emmc_get_phy(struct udevice *dev)
184{
185 struct rockchip_sdhc *priv = dev_get_priv(dev);
186 ofnode phy_node;
187 void *grf_base;
188 u32 grf_phy_offset, phandle;
189
190 phandle = dev_read_u32_default(dev, "phys", 0);
191 phy_node = ofnode_get_by_phandle(phandle);
192 if (!ofnode_valid(phy_node)) {
193 debug("Not found emmc phy device\n");
194 return -ENODEV;
195 }
196
197 grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
198 if (grf_base < 0) {
199 printf("%s Get syscon grf failed", __func__);
200 return -ENODEV;
201 }
202 grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
203
204 priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
205
206 return 0;
207}
208
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300209static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800210{
211 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300212 struct mmc *mmc = host->mmc;
213 uint clock = mmc->tran_speed;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800214 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
215
216 if (cycle_phy)
217 rk3399_emmc_phy_power_off(priv->phy);
218
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300219 sdhci_set_control_reg(host);
220};
221
222static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
223{
224 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
225 struct mmc *mmc = host->mmc;
226 uint clock = mmc->tran_speed;
227 int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
228
229 if (!clock)
230 clock = mmc->clock;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800231
232 if (cycle_phy)
233 rk3399_emmc_phy_power_on(priv->phy, clock);
234
235 return 0;
236}
237
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800238static int rk3568_emmc_phy_init(struct udevice *dev)
239{
240 struct rockchip_sdhc *prv = dev_get_priv(dev);
241 struct sdhci_host *host = &prv->host;
242 u32 extra;
243
244 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
245 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
246
247 return 0;
248}
249
250static int rk3568_sdhci_emmc_set_clock(struct sdhci_host *host, unsigned int clock)
251{
252 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
253 int val, ret;
254 u32 extra;
255
256 if (clock > host->max_clk)
257 clock = host->max_clk;
258 if (clock)
259 clk_set_rate(&priv->emmc_clk, clock);
260
261 sdhci_set_clock(host->mmc, clock);
262
263 if (clock >= 100 * MHz) {
264 /* reset DLL */
265 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
266 udelay(1);
267 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
268
269 /* Init DLL settings */
270 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
271 DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
272 DWCMSHC_EMMC_DLL_START;
273 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
274
275 ret = read_poll_timeout(readl, host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
276 val, DLL_LOCK_WO_TMOUT(val), 1, 500);
277 if (ret)
278 return ret;
279
280 extra = DWCMSHC_EMMC_DLL_DLYENA |
281 DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
282 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
283
284 extra = DWCMSHC_EMMC_DLL_DLYENA |
285 DLL_TXCLK_TAPNUM_DEFAULT |
286 DLL_TXCLK_TAPNUM_FROM_SW;
287 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
288
289 extra = DWCMSHC_EMMC_DLL_DLYENA |
290 DLL_STRBIN_TAPNUM_DEFAULT;
291 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
292 } else {
293 /* reset the clock phase when the frequency is lower than 100MHz */
294 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
295 extra = DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
296 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
297 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
298 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
299 }
300
301 return 0;
302}
303
304static int rk3568_emmc_get_phy(struct udevice *dev)
305{
306 return 0;
307}
308
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300309static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800310{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800311 struct mmc *mmc = host->mmc;
312 uint clock = mmc->tran_speed;
313 u32 reg;
314
315 if (!clock)
316 clock = mmc->clock;
317
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300318 rk3568_sdhci_emmc_set_clock(host, clock);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800319
320 if (mmc->selected_mode == MMC_HS_400 || mmc->selected_mode == MMC_HS_400_ES) {
321 reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
322 reg &= ~SDHCI_CTRL_UHS_MASK;
323 reg |= SDHCI_CTRL_HS400;
324 sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
325 } else {
326 sdhci_set_uhs_timing(host);
327 }
328
329 return 0;
330}
331
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300332static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
333{
334 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
335 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
336
337 if (data->set_control_reg)
338 data->set_control_reg(host);
339}
340
341static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
342{
343 struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
344 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
345
346 if (data->set_ios_post)
347 return data->set_ios_post(host);
348
349 return 0;
350}
351
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800352static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
353{
354 struct sdhci_host *host = dev_get_priv(mmc->dev);
355 char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
356 struct mmc_cmd cmd;
357 u32 ctrl, blk_size;
358 int ret = 0;
359
360 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
361 ctrl |= SDHCI_CTRL_EXEC_TUNING;
362 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
363
364 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
365 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
366
367 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
368 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && host->mmc->bus_width == 8)
369 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
370 sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
371 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
372
373 cmd.cmdidx = opcode;
374 cmd.resp_type = MMC_RSP_R1;
375 cmd.cmdarg = 0;
376
377 do {
378 if (tuning_loop_counter-- == 0)
379 break;
380
381 mmc_send_cmd(mmc, &cmd, NULL);
382
383 if (opcode == MMC_CMD_SEND_TUNING_BLOCK)
384 /*
385 * For tuning command, do not do busy loop. As tuning
386 * is happening (CLK-DATA latching for setup/hold time
387 * requirements), give time to complete
388 */
389 udelay(1);
390
391 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
392 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
393
394 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
395 printf("%s:Tuning failed\n", __func__);
396 ret = -EIO;
397 }
398
399 if (tuning_loop_counter < 0) {
400 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
401 sdhci_writel(host, ctrl, SDHCI_HOST_CONTROL2);
402 }
403
404 /* Enable only interrupts served by the SD controller */
405 sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
406 /* Mask all sdhci interrupt sources */
407 sdhci_writel(host, 0x0, SDHCI_SIGNAL_ENABLE);
408
409 return ret;
410}
411
412static struct sdhci_ops rockchip_sdhci_ops = {
413 .set_ios_post = rockchip_sdhci_set_ios_post,
414 .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300415 .set_control_reg = rockchip_sdhci_set_control_reg,
Kever Yang65922e02016-07-18 17:00:58 +0800416};
417
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800418static int rockchip_sdhci_probe(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800419{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800420 struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800421 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700422 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800423 struct rockchip_sdhc *prv = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800424 struct mmc_config *cfg = &plat->cfg;
Kever Yang65922e02016-07-18 17:00:58 +0800425 struct sdhci_host *host = &prv->host;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800426 struct clk clk;
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800427 int ret;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800428
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800429 host->max_clk = cfg->f_max;
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800430 ret = clk_get_by_index(dev, 0, &clk);
431 if (!ret) {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800432 ret = clk_set_rate(&clk, host->max_clk);
Kever Yang9ea1fdf2016-12-28 11:32:35 +0800433 if (IS_ERR_VALUE(ret))
434 printf("%s clk set rate fail!\n", __func__);
435 } else {
436 printf("%s fail to get clk\n", __func__);
437 }
Kever Yang65922e02016-07-18 17:00:58 +0800438
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800439 prv->emmc_clk = clk;
440 prv->dev = dev;
441
442 if (data->get_phy) {
443 ret = data->get_phy(dev);
444 if (ret)
445 return ret;
446 }
447
448 if (data->emmc_phy_init) {
449 ret = data->emmc_phy_init(dev);
450 if (ret)
451 return ret;
452 }
453
454 host->ops = &rockchip_sdhci_ops;
Kever Yang65922e02016-07-18 17:00:58 +0800455 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
456
Kever Yang65922e02016-07-18 17:00:58 +0800457 host->mmc = &plat->mmc;
Kever Yang65922e02016-07-18 17:00:58 +0800458 host->mmc->priv = &prv->host;
459 host->mmc->dev = dev;
460 upriv->mmc = host->mmc;
461
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800462 ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
Kever Yang36d9bf82019-07-19 18:01:11 +0800463 if (ret)
464 return ret;
465
Kever Yang65922e02016-07-18 17:00:58 +0800466 return sdhci_probe(dev);
467}
468
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800469static int rockchip_sdhci_of_to_plat(struct udevice *dev)
Kever Yang65922e02016-07-18 17:00:58 +0800470{
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800471 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800472 struct sdhci_host *host = dev_get_priv(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800473 struct mmc_config *cfg = &plat->cfg;
474 int ret;
Kever Yang65922e02016-07-18 17:00:58 +0800475
476 host->name = dev->name;
Philipp Tomsichdbb28282017-09-11 22:04:21 +0200477 host->ioaddr = dev_read_addr_ptr(dev);
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800478
479 ret = mmc_of_parse(dev, cfg);
480 if (ret)
481 return ret;
Kever Yang65922e02016-07-18 17:00:58 +0800482
483 return 0;
484}
485
486static int rockchip_sdhci_bind(struct udevice *dev)
487{
Simon Glassfa20e932020-12-03 16:55:20 -0700488 struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
Kever Yang65922e02016-07-18 17:00:58 +0800489
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900490 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Kever Yang65922e02016-07-18 17:00:58 +0800491}
492
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800493static const struct sdhci_data rk3399_data = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800494 .get_phy = rk3399_emmc_get_phy,
495 .emmc_phy_init = rk3399_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300496 .set_control_reg = rk3399_sdhci_set_control_reg,
497 .set_ios_post = rk3399_sdhci_set_ios_post,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800498};
499
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800500static const struct sdhci_data rk3568_data = {
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800501 .get_phy = rk3568_emmc_get_phy,
502 .emmc_phy_init = rk3568_emmc_phy_init,
Alper Nebi Yasak676a5a52022-01-29 01:42:37 +0300503 .set_ios_post = rk3568_sdhci_set_ios_post,
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800504};
505
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800506static const struct udevice_id sdhci_ids[] = {
507 {
508 .compatible = "arasan,sdhci-5.1",
509 .data = (ulong)&rk3399_data,
510 },
Yifeng Zhaoe5dddfa2021-06-29 16:24:42 +0800511 {
512 .compatible = "rockchip,rk3568-dwcmshc",
513 .data = (ulong)&rk3568_data,
514 },
Kever Yang65922e02016-07-18 17:00:58 +0800515 { }
516};
517
518U_BOOT_DRIVER(arasan_sdhci_drv) = {
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800519 .name = "rockchip_sdhci_5_1",
Kever Yang65922e02016-07-18 17:00:58 +0800520 .id = UCLASS_MMC,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800521 .of_match = sdhci_ids,
522 .of_to_plat = rockchip_sdhci_of_to_plat,
Kever Yang65922e02016-07-18 17:00:58 +0800523 .ops = &sdhci_ops,
524 .bind = rockchip_sdhci_bind,
Yifeng Zhao5c2a5ab2021-06-29 16:24:41 +0800525 .probe = rockchip_sdhci_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700526 .priv_auto = sizeof(struct rockchip_sdhc),
Simon Glass71fa5b42020-12-03 16:55:18 -0700527 .plat_auto = sizeof(struct rockchip_sdhc_plat),
Kever Yang65922e02016-07-18 17:00:58 +0800528};