Merge tag 'xilinx-for-v2022.07-rc1' of https://source.denx.de/u-boot/custodians/u...
[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 /* 400KHz is max freq for card ID etc. Use that as min */
26 #define EMMC_MIN_FREQ   400000
27 #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)
44
45 /* 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
73 struct rockchip_sdhc_plat {
74         struct mmc_config cfg;
75         struct mmc mmc;
76 };
77
78 struct rockchip_emmc_phy {
79         u32 emmcphy_con[7];
80         u32 reserved;
81         u32 emmcphy_status;
82 };
83
84 struct rockchip_sdhc {
85         struct sdhci_host host;
86         struct udevice *dev;
87         void *base;
88         struct rockchip_emmc_phy *phy;
89         struct clk emmc_clk;
90 };
91
92 struct sdhci_data {
93         int (*emmc_phy_init)(struct udevice *dev);
94         int (*get_phy)(struct udevice *dev);
95
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);
120 };
121
122 static int rk3399_emmc_phy_init(struct udevice *dev)
123 {
124         return 0;
125 }
126
127 static 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
170         /* REN Enable on STRB Line for HS400 */
171         writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
172
173         read_poll_timeout(readl, &phy->emmcphy_status, dllrdy,
174                           PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1, 5000);
175 }
176
177 static 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
183 static 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
209 static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
210 {
211         struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
212         struct mmc *mmc = host->mmc;
213         uint clock = mmc->tran_speed;
214         int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
215
216         if (cycle_phy)
217                 rk3399_emmc_phy_power_off(priv->phy);
218
219         sdhci_set_control_reg(host);
220 };
221
222 static 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;
231
232         if (cycle_phy)
233                 rk3399_emmc_phy_power_on(priv->phy, clock);
234
235         return 0;
236 }
237
238 static 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
250 static 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
304 static int rk3568_emmc_get_phy(struct udevice *dev)
305 {
306         return 0;
307 }
308
309 static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
310 {
311         struct mmc *mmc = host->mmc;
312         uint clock = mmc->tran_speed;
313         u32 reg;
314
315         if (!clock)
316                 clock = mmc->clock;
317
318         rk3568_sdhci_emmc_set_clock(host, clock);
319
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
332 static 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
341 static 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
352 static 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
412 static struct sdhci_ops rockchip_sdhci_ops = {
413         .set_ios_post   = rockchip_sdhci_set_ios_post,
414         .platform_execute_tuning = &rockchip_sdhci_execute_tuning,
415         .set_control_reg = rockchip_sdhci_set_control_reg,
416 };
417
418 static int rockchip_sdhci_probe(struct udevice *dev)
419 {
420         struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
421         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
422         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
423         struct rockchip_sdhc *prv = dev_get_priv(dev);
424         struct mmc_config *cfg = &plat->cfg;
425         struct sdhci_host *host = &prv->host;
426         struct clk clk;
427         int ret;
428
429         host->max_clk = cfg->f_max;
430         ret = clk_get_by_index(dev, 0, &clk);
431         if (!ret) {
432                 ret = clk_set_rate(&clk, host->max_clk);
433                 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         }
438
439         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;
455         host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
456
457         host->mmc = &plat->mmc;
458         host->mmc->priv = &prv->host;
459         host->mmc->dev = dev;
460         upriv->mmc = host->mmc;
461
462         ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
463         if (ret)
464                 return ret;
465
466         return sdhci_probe(dev);
467 }
468
469 static int rockchip_sdhci_of_to_plat(struct udevice *dev)
470 {
471         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
472         struct sdhci_host *host = dev_get_priv(dev);
473         struct mmc_config *cfg = &plat->cfg;
474         int ret;
475
476         host->name = dev->name;
477         host->ioaddr = dev_read_addr_ptr(dev);
478
479         ret = mmc_of_parse(dev, cfg);
480         if (ret)
481                 return ret;
482
483         return 0;
484 }
485
486 static int rockchip_sdhci_bind(struct udevice *dev)
487 {
488         struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
489
490         return sdhci_bind(dev, &plat->mmc, &plat->cfg);
491 }
492
493 static const struct sdhci_data rk3399_data = {
494         .get_phy = rk3399_emmc_get_phy,
495         .emmc_phy_init = rk3399_emmc_phy_init,
496         .set_control_reg = rk3399_sdhci_set_control_reg,
497         .set_ios_post = rk3399_sdhci_set_ios_post,
498 };
499
500 static const struct sdhci_data rk3568_data = {
501         .get_phy = rk3568_emmc_get_phy,
502         .emmc_phy_init = rk3568_emmc_phy_init,
503         .set_ios_post = rk3568_sdhci_set_ios_post,
504 };
505
506 static const struct udevice_id sdhci_ids[] = {
507         {
508                 .compatible = "arasan,sdhci-5.1",
509                 .data = (ulong)&rk3399_data,
510         },
511         {
512                 .compatible = "rockchip,rk3568-dwcmshc",
513                 .data = (ulong)&rk3568_data,
514         },
515         { }
516 };
517
518 U_BOOT_DRIVER(arasan_sdhci_drv) = {
519         .name           = "rockchip_sdhci_5_1",
520         .id             = UCLASS_MMC,
521         .of_match       = sdhci_ids,
522         .of_to_plat     = rockchip_sdhci_of_to_plat,
523         .ops            = &sdhci_ops,
524         .bind           = rockchip_sdhci_bind,
525         .probe          = rockchip_sdhci_probe,
526         .priv_auto      = sizeof(struct rockchip_sdhc),
527         .plat_auto      = sizeof(struct rockchip_sdhc_plat),
528 };