mmc: rockchip_sdhci: Update speed mode controls in set_ios_post
[platform/kernel/u-boot.git] / drivers / mmc / rockchip_sdhci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
4  *
5  * Rockchip SD Host Controller Interface
6  */
7
8 #include <common.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <dm/ofnode.h>
12 #include <dt-structs.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/libfdt.h>
16 #include <linux/iopoll.h>
17 #include <malloc.h>
18 #include <mapmem.h>
19 #include "mmc_private.h"
20 #include <sdhci.h>
21 #include <syscon.h>
22 #include <asm/arch-rockchip/clock.h>
23 #include <asm/arch-rockchip/hardware.h>
24
25 /* DWCMSHC specific Mode Select value */
26 #define DWCMSHC_CTRL_HS400              0x7
27 /* 400KHz is max freq for card ID etc. Use that as min */
28 #define EMMC_MIN_FREQ   400000
29 #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)
46
47 #define ARASAN_VENDOR_REGISTER          0x78
48 #define ARASAN_VENDOR_ENHANCED_STROBE   BIT(0)
49
50 /* 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
58 /* 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
64 #define DECMSHC_EMMC_DLL_CMDOUT         0x810
65 #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
73 #define DWCMSHC_EMMC_DLL_BYPASS         BIT(24)
74 #define DWCMSHC_EMMC_DLL_DLYENA         BIT(27)
75 #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
83 #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
88 #define DLL_RXCLK_ORI_GATE              BIT(31)
89 #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
95 struct rockchip_sdhc_plat {
96         struct mmc_config cfg;
97         struct mmc mmc;
98 };
99
100 struct rockchip_emmc_phy {
101         u32 emmcphy_con[7];
102         u32 reserved;
103         u32 emmcphy_status;
104 };
105
106 struct 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
114 struct sdhci_data {
115         int (*get_phy)(struct udevice *dev);
116
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);
141
142         void (*set_clock)(struct sdhci_host *host, u32 div);
143         int (*config_dll)(struct sdhci_host *host, u32 clock, bool enable);
144
145         /**
146          * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
147          *
148          * This is the set_enhanced_strobe() SDHCI operation that should
149          * be used for the hardware this driver data is associated with.
150          * Normally, this is used to set any host-specific configuration
151          * necessary for HS400 ES.
152          *
153          * @host: SDHCI host structure
154          * Return: 0 if successful, -ve on error
155          */
156         int (*set_enhanced_strobe)(struct sdhci_host *host);
157 };
158
159 static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
160 {
161         u32 caldone, dllrdy, freqsel;
162
163         writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
164         writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
165         writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
166
167         /*
168          * According to the user manual, calpad calibration
169          * cycle takes more than 2us without the minimal recommended
170          * value, so we may need a little margin here
171          */
172         udelay(3);
173         writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
174
175         /*
176          * According to the user manual, it asks driver to
177          * wait 5us for calpad busy trimming. But it seems that
178          * 5us of caldone isn't enough for all cases.
179          */
180         udelay(500);
181         caldone = readl(&phy->emmcphy_status);
182         caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
183         if (caldone != PHYCTRL_CALDONE_DONE) {
184                 printf("%s: caldone timeout.\n", __func__);
185                 return;
186         }
187
188         /* Set the frequency of the DLL operation */
189         if (clock < 75 * MHz)
190                 freqsel = PHYCTRL_FREQSEL_50M;
191         else if (clock < 125 * MHz)
192                 freqsel = PHYCTRL_FREQSEL_100M;
193         else if (clock < 175 * MHz)
194                 freqsel = PHYCTRL_FREQSEL_150M;
195         else
196                 freqsel = PHYCTRL_FREQSEL_200M;
197
198         /* Set the frequency of the DLL operation */
199         writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
200         writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
201
202         /* REN Enable on STRB Line for HS400 */
203         writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
204
205         read_poll_timeout(readl, dllrdy, PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1,
206                           5000, &phy->emmcphy_status);
207 }
208
209 static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
210 {
211         writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
212         writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
213 }
214
215 static int rk3399_emmc_get_phy(struct udevice *dev)
216 {
217         struct rockchip_sdhc *priv = dev_get_priv(dev);
218         ofnode phy_node;
219         void *grf_base;
220         u32 grf_phy_offset, phandle;
221
222         phandle = dev_read_u32_default(dev, "phys", 0);
223         phy_node = ofnode_get_by_phandle(phandle);
224         if (!ofnode_valid(phy_node)) {
225                 debug("Not found emmc phy device\n");
226                 return -ENODEV;
227         }
228
229         grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
230         if (IS_ERR_OR_NULL(grf_base)) {
231                 printf("%s Get syscon grf failed", __func__);
232                 return -ENODEV;
233         }
234         grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
235
236         priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
237
238         return 0;
239 }
240
241 static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host)
242 {
243         struct mmc *mmc = host->mmc;
244         u32 vendor;
245
246         vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER);
247         if (mmc->selected_mode == MMC_HS_400_ES)
248                 vendor |= ARASAN_VENDOR_ENHANCED_STROBE;
249         else
250                 vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE;
251         sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER);
252
253         return 0;
254 }
255
256 static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
257 {
258         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
259         struct mmc *mmc = host->mmc;
260         uint clock = mmc->tran_speed;
261         int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
262
263         if (cycle_phy)
264                 rk3399_emmc_phy_power_off(priv->phy);
265
266         sdhci_set_control_reg(host);
267
268         /*
269          * Reinitializing the device tries to set it to lower-speed modes
270          * first, which fails if the Enhanced Strobe bit is set, making
271          * the device impossible to use. Set the correct value here to
272          * let reinitialization attempts succeed.
273          */
274         if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT))
275                 rk3399_sdhci_set_enhanced_strobe(host);
276 };
277
278 static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
279 {
280         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
281         struct mmc *mmc = host->mmc;
282         uint clock = mmc->tran_speed;
283         int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
284
285         if (!clock)
286                 clock = mmc->clock;
287
288         if (cycle_phy)
289                 rk3399_emmc_phy_power_on(priv->phy, clock);
290
291         return 0;
292 }
293
294 static void rk3568_sdhci_set_clock(struct sdhci_host *host, u32 div)
295 {
296         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
297         struct mmc *mmc = host->mmc;
298         ulong rate;
299
300         rate = clk_set_rate(&priv->emmc_clk, mmc->clock);
301         if (IS_ERR_VALUE(rate))
302                 printf("%s: Set clock rate failed: %ld\n", __func__, (long)rate);
303 }
304
305 static int rk3568_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
306 {
307         int val, ret;
308         u32 extra;
309
310         if (!enable)
311                 return 0;
312
313         if (clock >= 100 * MHz) {
314                 /* reset DLL */
315                 sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
316                 udelay(1);
317                 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
318
319                 /* Init DLL settings */
320                 extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
321                         DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
322                         DWCMSHC_EMMC_DLL_START;
323                 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
324
325                 ret = read_poll_timeout(readl, val, DLL_LOCK_WO_TMOUT(val), 1,
326                                         500,
327                                         host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0);
328                 if (ret)
329                         return ret;
330
331                 extra = DWCMSHC_EMMC_DLL_DLYENA |
332                         DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
333                 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
334
335                 extra = DWCMSHC_EMMC_DLL_DLYENA |
336                         DLL_TXCLK_TAPNUM_DEFAULT |
337                         DLL_TXCLK_TAPNUM_FROM_SW;
338                 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
339
340                 extra = DWCMSHC_EMMC_DLL_DLYENA |
341                         DLL_STRBIN_TAPNUM_DEFAULT |
342                         DLL_STRBIN_TAPNUM_FROM_SW;
343                 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
344         } else {
345                 /*
346                  * Disable DLL and reset both of sample and drive clock.
347                  * The bypass bit and start bit need to be set if DLL is not locked.
348                  */
349                 sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START,
350                              DWCMSHC_EMMC_DLL_CTRL);
351                 sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
352                 sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
353                 sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
354                 /*
355                  * Before switching to hs400es mode, the driver will enable
356                  * enhanced strobe first. PHY needs to configure the parameters
357                  * of enhanced strobe first.
358                  */
359                 extra = DWCMSHC_EMMC_DLL_DLYENA |
360                         DLL_STRBIN_DELAY_NUM_SEL |
361                         DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
362                 sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
363         }
364
365         return 0;
366 }
367
368 static int rk3568_emmc_get_phy(struct udevice *dev)
369 {
370         return 0;
371 }
372
373 static int rk3568_sdhci_set_enhanced_strobe(struct sdhci_host *host)
374 {
375         return 0;
376 }
377
378 static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
379 {
380         struct mmc *mmc = host->mmc;
381         u32 reg, vendor_reg;
382
383         reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
384         reg &= ~SDHCI_CTRL_UHS_MASK;
385
386         switch (mmc->selected_mode) {
387         case UHS_SDR25:
388         case MMC_HS:
389         case MMC_HS_52:
390                 reg |= SDHCI_CTRL_UHS_SDR25;
391                 break;
392         case UHS_SDR50:
393                 reg |= SDHCI_CTRL_UHS_SDR50;
394                 break;
395         case UHS_DDR50:
396         case MMC_DDR_52:
397                 reg |= SDHCI_CTRL_UHS_DDR50;
398                 break;
399         case UHS_SDR104:
400         case MMC_HS_200:
401                 reg |= SDHCI_CTRL_UHS_SDR104;
402                 break;
403         case MMC_HS_400:
404         case MMC_HS_400_ES:
405                 reg |= DWCMSHC_CTRL_HS400;
406                 break;
407         default:
408                 reg |= SDHCI_CTRL_UHS_SDR12;
409         }
410
411         sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
412
413         vendor_reg = (sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK)
414                      + DWCMSHC_EMMC_CONTROL;
415         reg = sdhci_readw(host, vendor_reg);
416
417         if (IS_MMC(mmc))
418                 reg |= DWCMSHC_CARD_IS_EMMC;
419         else
420                 reg &= ~DWCMSHC_CARD_IS_EMMC;
421
422         if (mmc->selected_mode == MMC_HS_400_ES)
423                 reg |= DWCMSHC_ENHANCED_STROBE;
424         else
425                 reg &= ~DWCMSHC_ENHANCED_STROBE;
426
427         sdhci_writew(host, reg, vendor_reg);
428
429         return 0;
430 }
431
432 static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
433 {
434         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
435         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
436
437         if (data->set_control_reg)
438                 data->set_control_reg(host);
439 }
440
441 static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
442 {
443         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
444         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
445
446         if (data->set_ios_post)
447                 return data->set_ios_post(host);
448
449         return 0;
450 }
451
452 static void rockchip_sdhci_set_clock(struct sdhci_host *host, u32 div)
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_clock)
458                 data->set_clock(host, div);
459 }
460
461 static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
462 {
463         struct rockchip_sdhc *priv = dev_get_priv(mmc->dev);
464         struct sdhci_host *host = &priv->host;
465         char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
466         struct mmc_cmd cmd;
467         u32 ctrl, blk_size;
468         int ret;
469
470         ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
471         ctrl |= SDHCI_CTRL_EXEC_TUNING;
472         sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
473
474         sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
475
476         blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
477         if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && mmc->bus_width == 8)
478                 blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
479         sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
480         sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
481
482         cmd.cmdidx = opcode;
483         cmd.resp_type = MMC_RSP_R1;
484         cmd.cmdarg = 0;
485
486         do {
487                 ret = mmc_send_cmd(mmc, &cmd, NULL);
488                 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
489                 if (ret || tuning_loop_counter-- == 0)
490                         break;
491         } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
492
493         if (ret || tuning_loop_counter < 0 || !(ctrl & SDHCI_CTRL_TUNED_CLK)) {
494                 if (!ret)
495                         ret = -EIO;
496                 printf("%s: Tuning failed: %d\n", __func__, ret);
497
498                 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
499                 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
500                 sdhci_writew(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
506         return ret;
507 }
508
509 static int rockchip_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
510 {
511         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
512         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
513
514         if (data->config_dll)
515                 return data->config_dll(host, clock, enable);
516
517         return 0;
518 }
519
520 static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
521 {
522         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
523         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
524
525         if (data->set_enhanced_strobe)
526                 return data->set_enhanced_strobe(host);
527
528         return -ENOTSUPP;
529 }
530
531 static struct sdhci_ops rockchip_sdhci_ops = {
532         .set_control_reg = rockchip_sdhci_set_control_reg,
533         .set_ios_post = rockchip_sdhci_set_ios_post,
534         .set_clock = rockchip_sdhci_set_clock,
535         .platform_execute_tuning = rockchip_sdhci_execute_tuning,
536         .config_dll = rockchip_sdhci_config_dll,
537         .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
538 };
539
540 static int rockchip_sdhci_probe(struct udevice *dev)
541 {
542         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
543         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
544         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
545         struct rockchip_sdhc *priv = dev_get_priv(dev);
546         struct mmc_config *cfg = &plat->cfg;
547         struct sdhci_host *host = &priv->host;
548         struct clk clk;
549         int ret;
550
551         host->max_clk = cfg->f_max;
552         ret = clk_get_by_index(dev, 0, &clk);
553         if (!ret) {
554                 ret = clk_set_rate(&clk, host->max_clk);
555                 if (IS_ERR_VALUE(ret))
556                         printf("%s clk set rate fail!\n", __func__);
557         } else {
558                 printf("%s fail to get clk\n", __func__);
559         }
560
561         priv->emmc_clk = clk;
562         priv->dev = dev;
563
564         if (data->get_phy) {
565                 ret = data->get_phy(dev);
566                 if (ret)
567                         return ret;
568         }
569
570         host->ops = &rockchip_sdhci_ops;
571         host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
572
573         host->mmc = &plat->mmc;
574         host->mmc->priv = &priv->host;
575         host->mmc->dev = dev;
576         upriv->mmc = host->mmc;
577
578         ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
579         if (ret)
580                 return ret;
581
582         return sdhci_probe(dev);
583 }
584
585 static int rockchip_sdhci_of_to_plat(struct udevice *dev)
586 {
587         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
588         struct rockchip_sdhc *priv = dev_get_priv(dev);
589         struct mmc_config *cfg = &plat->cfg;
590         struct sdhci_host *host = &priv->host;
591         int ret;
592
593         host->name = dev->name;
594         host->ioaddr = dev_read_addr_ptr(dev);
595
596         ret = mmc_of_parse(dev, cfg);
597         if (ret)
598                 return ret;
599
600         return 0;
601 }
602
603 static int rockchip_sdhci_bind(struct udevice *dev)
604 {
605         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
606
607         return sdhci_bind(dev, &plat->mmc, &plat->cfg);
608 }
609
610 static const struct sdhci_data rk3399_data = {
611         .get_phy = rk3399_emmc_get_phy,
612         .set_control_reg = rk3399_sdhci_set_control_reg,
613         .set_ios_post = rk3399_sdhci_set_ios_post,
614         .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
615 };
616
617 static const struct sdhci_data rk3568_data = {
618         .get_phy = rk3568_emmc_get_phy,
619         .set_ios_post = rk3568_sdhci_set_ios_post,
620         .set_clock = rk3568_sdhci_set_clock,
621         .config_dll = rk3568_sdhci_config_dll,
622         .set_enhanced_strobe = rk3568_sdhci_set_enhanced_strobe,
623 };
624
625 static const struct udevice_id sdhci_ids[] = {
626         {
627                 .compatible = "arasan,sdhci-5.1",
628                 .data = (ulong)&rk3399_data,
629         },
630         {
631                 .compatible = "rockchip,rk3568-dwcmshc",
632                 .data = (ulong)&rk3568_data,
633         },
634         { }
635 };
636
637 U_BOOT_DRIVER(arasan_sdhci_drv) = {
638         .name           = "rockchip_sdhci_5_1",
639         .id             = UCLASS_MMC,
640         .of_match       = sdhci_ids,
641         .of_to_plat     = rockchip_sdhci_of_to_plat,
642         .ops            = &sdhci_ops,
643         .bind           = rockchip_sdhci_bind,
644         .probe          = rockchip_sdhci_probe,
645         .priv_auto      = sizeof(struct rockchip_sdhc),
646         .plat_auto      = sizeof(struct rockchip_sdhc_plat),
647 };