common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / ram / rockchip / dmc-rk3368.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4  */
5
6 #include <common.h>
7 #include <clk.h>
8 #include <dm.h>
9 #include <hang.h>
10 #include <log.h>
11 #include <dt-bindings/memory/rk3368-dmc.h>
12 #include <dt-structs.h>
13 #include <ram.h>
14 #include <regmap.h>
15 #include <syscon.h>
16 #include <asm/io.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/cru_rk3368.h>
19 #include <asm/arch-rockchip/grf_rk3368.h>
20 #include <asm/arch-rockchip/ddr_rk3368.h>
21 #include <asm/arch-rockchip/sdram.h>
22 #include <asm/arch-rockchip/sdram_rk3288.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25
26 struct dram_info {
27         struct ram_info info;
28         struct clk ddr_clk;
29         struct rk3368_cru *cru;
30         struct rk3368_grf *grf;
31         struct rk3368_ddr_pctl *pctl;
32         struct rk3368_ddrphy *phy;
33         struct rk3368_pmu_grf *pmugrf;
34         struct rk3368_msch *msch;
35 };
36
37 struct rk3368_sdram_params {
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39         struct dtd_rockchip_rk3368_dmc of_plat;
40 #endif
41         struct rk3288_sdram_pctl_timing pctl_timing;
42         u32 trefi_mem_ddr3;
43         struct rk3288_sdram_channel chan;
44         struct regmap *map;
45         u32 ddr_freq;
46         u32 memory_schedule;
47         u32 ddr_speed_bin;
48         u32 tfaw_mult;
49 };
50
51 /* PTCL bits */
52 enum {
53         /* PCTL_DFISTCFG0 */
54         DFI_INIT_START = BIT(0),
55         DFI_DATA_BYTE_DISABLE_EN = BIT(2),
56
57         /* PCTL_DFISTCFG1 */
58         DFI_DRAM_CLK_SR_EN = BIT(0),
59         DFI_DRAM_CLK_DPD_EN = BIT(1),
60         ODT_LEN_BL8_W_SHIFT = 16,
61
62         /* PCTL_DFISTCFG2 */
63         DFI_PARITY_INTR_EN = BIT(0),
64         DFI_PARITY_EN = BIT(1),
65
66         /* PCTL_DFILPCFG0 */
67         TLP_RESP_TIME_SHIFT = 16,
68         LP_SR_EN = BIT(8),
69         LP_PD_EN = BIT(0),
70
71         /* PCTL_DFIODTCFG */
72         RANK0_ODT_WRITE_SEL = BIT(3),
73         RANK1_ODT_WRITE_SEL = BIT(11),
74
75         /* PCTL_SCFG */
76         HW_LOW_POWER_EN = BIT(0),
77
78         /* PCTL_MCMD */
79         START_CMD = BIT(31),
80         MCMD_RANK0 = BIT(20),
81         MCMD_RANK1 = BIT(21),
82         DESELECT_CMD = 0,
83         PREA_CMD,
84         REF_CMD,
85         MRS_CMD,
86         ZQCS_CMD,
87         ZQCL_CMD,
88         RSTL_CMD,
89         MRR_CMD = 8,
90         DPDE_CMD,
91
92         /* PCTL_POWCTL */
93         POWER_UP_START = BIT(0),
94
95         /* PCTL_POWSTAT */
96         POWER_UP_DONE = BIT(0),
97
98         /* PCTL_SCTL */
99         INIT_STATE = 0,
100         CFG_STATE,
101         GO_STATE,
102         SLEEP_STATE,
103         WAKEUP_STATE,
104
105         /* PCTL_STAT */
106         LP_TRIG_SHIFT = 4,
107         LP_TRIG_MASK = 7,
108         PCTL_STAT_MSK = 7,
109         INIT_MEM = 0,
110         CONFIG,
111         CONFIG_REQ,
112         ACCESS,
113         ACCESS_REQ,
114         LOW_POWER,
115         LOW_POWER_ENTRY_REQ,
116         LOW_POWER_EXIT_REQ,
117
118         /* PCTL_MCFG */
119         DDR2_DDR3_BL_8 = BIT(0),
120         DDR3_EN = BIT(5),
121         TFAW_TRRD_MULT4 = (0 << 18),
122         TFAW_TRRD_MULT5 = (1 << 18),
123         TFAW_TRRD_MULT6 = (2 << 18),
124 };
125
126 #define DDR3_MR0_WR(n) \
127         ((n <= 8) ? ((n - 4) << 9) : (((n >> 1) & 0x7) << 9))
128 #define DDR3_MR0_CL(n) \
129         ((((n - 4) & 0x7) << 4) | (((n - 4) & 0x8) >> 2))
130 #define DDR3_MR0_BL8 \
131         (0 << 0)
132 #define DDR3_MR0_DLL_RESET \
133         (1 << 8)
134 #define DDR3_MR1_RTT120OHM \
135         ((0 << 9) | (1 << 6) | (0 << 2))
136 #define DDR3_MR2_TWL(n) \
137         (((n - 5) & 0x7) << 3)
138
139
140 #ifdef CONFIG_TPL_BUILD
141
142 static void ddr_set_noc_spr_err_stall(struct rk3368_grf *grf, bool enable)
143 {
144         if (enable)
145                 rk_setreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
146         else
147                 rk_clrreg(&grf->ddrc0_con0, NOC_RSP_ERR_STALL);
148 }
149
150 static void ddr_set_ddr3_mode(struct rk3368_grf *grf, bool ddr3_mode)
151 {
152         if (ddr3_mode)
153                 rk_setreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
154         else
155                 rk_clrreg(&grf->ddrc0_con0, MSCH0_MAINDDR3_DDR3);
156 }
157
158 static void ddrphy_config(struct rk3368_ddrphy *phy,
159                           u32 tcl, u32 tal, u32 tcwl)
160 {
161         int i;
162
163         /* Set to DDR3 mode */
164         clrsetbits_le32(&phy->reg[1], 0x3, 0x0);
165
166         /* DDRPHY_REGB: CL, AL */
167         clrsetbits_le32(&phy->reg[0xb], 0xff, tcl << 4 | tal);
168         /* DDRPHY_REGC: CWL */
169         clrsetbits_le32(&phy->reg[0xc], 0x0f, tcwl);
170
171         /* Update drive-strength */
172         writel(0xcc, &phy->reg[0x11]);
173         writel(0xaa, &phy->reg[0x16]);
174         /*
175          * Update NRCOMP/PRCOMP for all 4 channels (for details of all
176          * affected registers refer to the documentation of DDRPHY_REG20
177          * and DDRPHY_REG21 in the RK3368 TRM.
178          */
179         for (i = 0; i < 4; ++i) {
180                 writel(0xcc, &phy->reg[0x20 + i * 0x10]);
181                 writel(0x44, &phy->reg[0x21 + i * 0x10]);
182         }
183
184         /* Enable write-leveling calibration bypass */
185         setbits_le32(&phy->reg[2], BIT(3));
186 }
187
188 static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
189 {
190         int i;
191
192         for (i = 0; i < n / sizeof(u32); i++)
193                 writel(*src++, dest++);
194 }
195
196 static void send_command(struct rk3368_ddr_pctl *pctl, u32 rank, u32 cmd)
197 {
198         u32 mcmd = START_CMD | cmd | rank;
199
200         debug("%s: writing %x to MCMD\n", __func__, mcmd);
201         writel(mcmd, &pctl->mcmd);
202         while (readl(&pctl->mcmd) & START_CMD)
203                 /* spin */;
204 }
205
206 static void send_mrs(struct rk3368_ddr_pctl *pctl,
207                             u32 rank, u32 mr_num, u32 mr_data)
208 {
209         u32 mcmd = START_CMD | MRS_CMD | rank | (mr_num << 17) | (mr_data << 4);
210
211         debug("%s: writing %x to MCMD\n", __func__, mcmd);
212         writel(mcmd, &pctl->mcmd);
213         while (readl(&pctl->mcmd) & START_CMD)
214                 /* spin */;
215 }
216
217 static int memory_init(struct rk3368_ddr_pctl *pctl,
218                        struct rk3368_sdram_params *params)
219 {
220         u32 mr[4];
221         const ulong timeout_ms = 500;
222         ulong tmp;
223
224         /*
225          * Power up DRAM by DDR_PCTL_POWCTL[0] register of PCTL and
226          * wait power up DRAM finish with DDR_PCTL_POWSTAT[0] register
227          * of PCTL.
228          */
229         writel(POWER_UP_START, &pctl->powctl);
230
231         tmp = get_timer(0);
232         do {
233                 if (get_timer(tmp) > timeout_ms) {
234                         pr_err("%s: POWER_UP_START did not complete in %ld ms\n",
235                               __func__, timeout_ms);
236                         return -ETIME;
237                 }
238         } while (!(readl(&pctl->powstat) & POWER_UP_DONE));
239
240         /* Configure MR0 through MR3 */
241         mr[0] = DDR3_MR0_WR(params->pctl_timing.twr) |
242                 DDR3_MR0_CL(params->pctl_timing.tcl) |
243                 DDR3_MR0_DLL_RESET;
244         mr[1] = DDR3_MR1_RTT120OHM;
245         mr[2] = DDR3_MR2_TWL(params->pctl_timing.tcwl);
246         mr[3] = 0;
247
248         /*
249          * Also see RK3368 Technical Reference Manual:
250          *   "16.6.2 Initialization (DDR3 Initialization Sequence)"
251          */
252         send_command(pctl, MCMD_RANK0 | MCMD_RANK1, DESELECT_CMD);
253         udelay(1);
254         send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
255         send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 2, mr[2]);
256         send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 3, mr[3]);
257         send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 1, mr[1]);
258         send_mrs(pctl, MCMD_RANK0 | MCMD_RANK1, 0, mr[0]);
259         send_command(pctl, MCMD_RANK0 | MCMD_RANK1, ZQCL_CMD);
260
261         return 0;
262 }
263
264 static void move_to_config_state(struct rk3368_ddr_pctl *pctl)
265 {
266         /*
267          * Also see RK3368 Technical Reference Manual:
268          *   "16.6.1 State transition of PCTL (Moving to Config State)"
269          */
270         u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
271
272         switch (state) {
273         case LOW_POWER:
274                 writel(WAKEUP_STATE, &pctl->sctl);
275                 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
276                         /* spin */;
277
278                 /* fall-through */
279         case ACCESS:
280         case INIT_MEM:
281                 writel(CFG_STATE, &pctl->sctl);
282                 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
283                         /* spin */;
284                 break;
285
286         case CONFIG:
287                 return;
288
289         default:
290                 break;
291         }
292 }
293
294 static void move_to_access_state(struct rk3368_ddr_pctl *pctl)
295 {
296         /*
297          * Also see RK3368 Technical Reference Manual:
298          *   "16.6.1 State transition of PCTL (Moving to Access State)"
299          */
300         u32 state = readl(&pctl->stat) & PCTL_STAT_MSK;
301
302         switch (state) {
303         case LOW_POWER:
304                 if (((readl(&pctl->stat) >> LP_TRIG_SHIFT) &
305                      LP_TRIG_MASK) == 1)
306                         return;
307
308                 writel(WAKEUP_STATE, &pctl->sctl);
309                 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
310                         /* spin */;
311
312                 /* fall-through */
313         case INIT_MEM:
314                 writel(CFG_STATE, &pctl->sctl);
315                 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
316                         /* spin */;
317
318                 /* fall-through */
319         case CONFIG:
320                 writel(GO_STATE, &pctl->sctl);
321                 while ((readl(&pctl->stat) & PCTL_STAT_MSK) == CONFIG)
322                         /* spin */;
323                 break;
324
325         case ACCESS:
326                 return;
327
328         default:
329                 break;
330         }
331 }
332
333 static void ddrctl_reset(struct rk3368_cru *cru)
334 {
335         const u32 ctl_reset = BIT(3) | BIT(2);
336         const u32 phy_reset = BIT(1) | BIT(0);
337
338         /*
339          * The PHY reset should be released before the PCTL reset.
340          *
341          * Note that the following sequence (including the number of
342          * us to delay between releasing the PHY and PCTL reset) has
343          * been adapted per feedback received from Rockchips, so do
344          * not try to optimise.
345          */
346         rk_setreg(&cru->softrst_con[10], ctl_reset | phy_reset);
347         udelay(1);
348         rk_clrreg(&cru->softrst_con[10], phy_reset);
349         udelay(5);
350         rk_clrreg(&cru->softrst_con[10], ctl_reset);
351 }
352
353 static void ddrphy_reset(struct rk3368_ddrphy *ddrphy)
354 {
355         /*
356          * The analog part of the PHY should be release at least 1000
357          * DRAM cycles before the digital part of the PHY (waiting for
358          * 5us will ensure this for a DRAM clock as low as 200MHz).
359          */
360         clrbits_le32(&ddrphy->reg[0], BIT(3) | BIT(2));
361         udelay(1);
362         setbits_le32(&ddrphy->reg[0], BIT(2));
363         udelay(5);
364         setbits_le32(&ddrphy->reg[0], BIT(3));
365 }
366
367 static void ddrphy_config_delays(struct rk3368_ddrphy *ddrphy, u32 freq)
368 {
369         u32 dqs_dll_delay;
370
371         setbits_le32(&ddrphy->reg[0x13], BIT(4));
372         clrbits_le32(&ddrphy->reg[0x14], BIT(3));
373
374         setbits_le32(&ddrphy->reg[0x26], BIT(4));
375         clrbits_le32(&ddrphy->reg[0x27], BIT(3));
376
377         setbits_le32(&ddrphy->reg[0x36], BIT(4));
378         clrbits_le32(&ddrphy->reg[0x37], BIT(3));
379
380         setbits_le32(&ddrphy->reg[0x46], BIT(4));
381         clrbits_le32(&ddrphy->reg[0x47], BIT(3));
382
383         setbits_le32(&ddrphy->reg[0x56], BIT(4));
384         clrbits_le32(&ddrphy->reg[0x57], BIT(3));
385
386         if (freq <= 400000000)
387                 setbits_le32(&ddrphy->reg[0xa4], 0x1f);
388         else
389                 clrbits_le32(&ddrphy->reg[0xa4], 0x1f);
390
391         if (freq < 681000000)
392                 dqs_dll_delay = 3; /* 67.5 degree delay */
393         else
394                 dqs_dll_delay = 2; /* 45 degree delay */
395
396         writel(dqs_dll_delay, &ddrphy->reg[0x28]);
397         writel(dqs_dll_delay, &ddrphy->reg[0x38]);
398         writel(dqs_dll_delay, &ddrphy->reg[0x48]);
399         writel(dqs_dll_delay, &ddrphy->reg[0x58]);
400 }
401
402 static int dfi_cfg(struct rk3368_ddr_pctl *pctl)
403 {
404         const ulong timeout_ms = 200;
405         ulong tmp;
406
407         writel(DFI_DATA_BYTE_DISABLE_EN, &pctl->dfistcfg0);
408
409         writel(DFI_DRAM_CLK_SR_EN | DFI_DRAM_CLK_DPD_EN,
410                &pctl->dfistcfg1);
411         writel(DFI_PARITY_INTR_EN | DFI_PARITY_EN, &pctl->dfistcfg2);
412         writel(7 << TLP_RESP_TIME_SHIFT | LP_SR_EN | LP_PD_EN,
413                &pctl->dfilpcfg0);
414
415         writel(1, &pctl->dfitphyupdtype0);
416
417         writel(0x1f, &pctl->dfitphyrdlat);
418         writel(0, &pctl->dfitphywrdata);
419         writel(0, &pctl->dfiupdcfg);  /* phyupd and ctrlupd disabled */
420
421         setbits_le32(&pctl->dfistcfg0, DFI_INIT_START);
422
423         tmp = get_timer(0);
424         do {
425                 if (get_timer(tmp) > timeout_ms) {
426                         pr_err("%s: DFI init did not complete within %ld ms\n",
427                               __func__, timeout_ms);
428                         return -ETIME;
429                 }
430         } while ((readl(&pctl->dfiststat0) & 1) == 0);
431
432         return 0;
433 }
434
435 static inline u32 ps_to_tCK(const u32 ps, const ulong freq)
436 {
437         const ulong MHz = 1000000;
438         return DIV_ROUND_UP(ps * freq, 1000000 * MHz);
439 }
440
441 static inline u32 ns_to_tCK(const u32 ns, const ulong freq)
442 {
443         return ps_to_tCK(ns * 1000, freq);
444 }
445
446 static inline u32 tCK_to_ps(const ulong tCK, const ulong freq)
447 {
448         const ulong MHz = 1000000;
449         return DIV_ROUND_UP(tCK * 1000000 * MHz, freq);
450 }
451
452 static int pctl_calc_timings(struct rk3368_sdram_params *params,
453                               ulong freq)
454 {
455         struct rk3288_sdram_pctl_timing *pctl_timing = &params->pctl_timing;
456         const ulong MHz = 1000000;
457         u32 tccd;
458         u32 tfaw_as_ps;
459
460         if (params->ddr_speed_bin != DDR3_1600K) {
461                 pr_err("%s: unimplemented DDR3 speed bin %d\n",
462                       __func__, params->ddr_speed_bin);
463                 return -1;
464         }
465
466         /* PCTL is clocked at 1/2 the DRAM clock; err on the side of caution */
467         pctl_timing->togcnt1u = DIV_ROUND_UP(freq, 2 * MHz);
468         pctl_timing->togcnt100n = DIV_ROUND_UP(freq / 10, 2 * MHz);
469
470         pctl_timing->tinit = 200;                 /* 200 usec                */
471         pctl_timing->trsth = 500;                 /* 500 usec                */
472         pctl_timing->trefi = 78;                  /* 7.8usec = 78 * 100ns    */
473         params->trefi_mem_ddr3 = ns_to_tCK(pctl_timing->trefi * 100, freq);
474
475         if (freq <= (400 * MHz)) {
476                 pctl_timing->tcl = 6;
477                 pctl_timing->tcwl = 10;
478         } else if (freq <= (533 * MHz)) {
479                 pctl_timing->tcl = 8;
480                 pctl_timing->tcwl = 6;
481         } else if (freq <= (666 * MHz)) {
482                 pctl_timing->tcl = 10;
483                 pctl_timing->tcwl = 7;
484         } else {
485                 pctl_timing->tcl = 11;
486                 pctl_timing->tcwl = 8;
487         }
488
489         pctl_timing->tmrd = 4;                    /* 4 tCK (all speed bins)  */
490         pctl_timing->trfc = ns_to_tCK(350, freq); /* tRFC: 350 (max) @ 8GBit */
491         pctl_timing->trp = max(4u, ps_to_tCK(13750, freq));
492         /*
493          * JESD-79:
494          *   READ to WRITE Command Delay = RL + tCCD / 2 + 2tCK - WL
495          */
496         tccd = 4;
497         pctl_timing->trtw = pctl_timing->tcl + tccd/2 + 2 - pctl_timing->tcwl;
498         pctl_timing->tal = 0;
499         pctl_timing->tras = ps_to_tCK(35000, freq);
500         pctl_timing->trc = ps_to_tCK(48750, freq);
501         pctl_timing->trcd = ps_to_tCK(13750, freq);
502         pctl_timing->trrd = max(4u, ps_to_tCK(7500, freq));
503         pctl_timing->trtp = max(4u, ps_to_tCK(7500, freq));
504         pctl_timing->twr = ps_to_tCK(15000, freq);
505         /* The DDR3 mode-register does only support even values for tWR > 8. */
506         if (pctl_timing->twr > 8)
507                 pctl_timing->twr = (pctl_timing->twr + 1) & ~1;
508         pctl_timing->twtr = max(4u, ps_to_tCK(7500, freq));
509         pctl_timing->texsr = 512;                 /* tEXSR(max) is tDLLLK    */
510         pctl_timing->txp = max(3u, ps_to_tCK(6000, freq));
511         pctl_timing->txpdll = max(10u, ps_to_tCK(24000, freq));
512         pctl_timing->tzqcs = max(64u, ps_to_tCK(80000, freq));
513         pctl_timing->tzqcsi = 10000;               /* as used by Rockchip    */
514         pctl_timing->tdqs = 1;                     /* fixed for DDR3         */
515         pctl_timing->tcksre = max(5u, ps_to_tCK(10000, freq));
516         pctl_timing->tcksrx = max(5u, ps_to_tCK(10000, freq));
517         pctl_timing->tcke = max(3u, ps_to_tCK(5000, freq));
518         pctl_timing->tmod = max(12u, ps_to_tCK(15000, freq));
519         pctl_timing->trstl = ns_to_tCK(100, freq);
520         pctl_timing->tzqcl = max(256u, ps_to_tCK(320000, freq));   /* tZQoper */
521         pctl_timing->tmrr = 0;
522         pctl_timing->tckesr = pctl_timing->tcke + 1;  /* JESD-79: tCKE + 1tCK */
523         pctl_timing->tdpd = 0;    /* RK3368 TRM: "allowed values for DDR3: 0" */
524
525
526         /*
527          * The controller can represent tFAW as 4x, 5x or 6x tRRD only.
528          * We want to use the smallest multiplier that satisfies the tFAW
529          * requirements of the given speed-bin.  If necessary, we stretch out
530          * tRRD to allow us to operate on a 6x multiplier for tFAW.
531          */
532         tfaw_as_ps = 40000;      /* 40ns: tFAW for DDR3-1600K, 2KB page-size */
533         if (tCK_to_ps(pctl_timing->trrd * 6, freq) < tfaw_as_ps) {
534                 /* If tFAW is > 6 x tRRD, we need to stretch tRRD */
535                 pctl_timing->trrd = ps_to_tCK(DIV_ROUND_UP(40000, 6), freq);
536                 params->tfaw_mult = TFAW_TRRD_MULT6;
537         } else if (tCK_to_ps(pctl_timing->trrd * 5, freq) < tfaw_as_ps) {
538                 params->tfaw_mult = TFAW_TRRD_MULT6;
539         } else if (tCK_to_ps(pctl_timing->trrd * 4, freq) < tfaw_as_ps) {
540                 params->tfaw_mult = TFAW_TRRD_MULT5;
541         } else {
542                 params->tfaw_mult = TFAW_TRRD_MULT4;
543         }
544
545         return 0;
546 }
547
548 static void pctl_cfg(struct rk3368_ddr_pctl *pctl,
549                      struct rk3368_sdram_params *params,
550                      struct rk3368_grf *grf)
551 {
552         /* Configure PCTL timing registers */
553         params->pctl_timing.trefi |= BIT(31);   /* see PCTL_TREFI */
554         copy_to_reg(&pctl->togcnt1u, &params->pctl_timing.togcnt1u,
555                     sizeof(params->pctl_timing));
556         writel(params->trefi_mem_ddr3, &pctl->trefi_mem_ddr3);
557
558         /* Set up ODT write selector and ODT write length */
559         writel((RANK0_ODT_WRITE_SEL | RANK1_ODT_WRITE_SEL), &pctl->dfiodtcfg);
560         writel(7 << ODT_LEN_BL8_W_SHIFT, &pctl->dfiodtcfg1);
561
562         /* Set up the CL/CWL-dependent timings of DFI */
563         writel((params->pctl_timing.tcl - 1) / 2 - 1, &pctl->dfitrddataen);
564         writel((params->pctl_timing.tcwl - 1) / 2 - 1, &pctl->dfitphywrlat);
565
566         /* DDR3 */
567         writel(params->tfaw_mult | DDR3_EN | DDR2_DDR3_BL_8, &pctl->mcfg);
568         writel(0x001c0004, &grf->ddrc0_con0);
569
570         setbits_le32(&pctl->scfg, HW_LOW_POWER_EN);
571 }
572
573 static int ddrphy_data_training(struct rk3368_ddr_pctl *pctl,
574                                 struct rk3368_ddrphy *ddrphy)
575 {
576         const u32 trefi = readl(&pctl->trefi);
577         const ulong timeout_ms = 500;
578         ulong tmp;
579
580         /* disable auto-refresh */
581         writel(0 | BIT(31), &pctl->trefi);
582
583         clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
584         clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x21);
585
586         tmp = get_timer(0);
587         do {
588                 if (get_timer(tmp) > timeout_ms) {
589                         pr_err("%s: did not complete within %ld ms\n",
590                               __func__, timeout_ms);
591                         return -ETIME;
592                 }
593         } while ((readl(&ddrphy->reg[0xff]) & 0xf) != 0xf);
594
595         send_command(pctl, MCMD_RANK0 | MCMD_RANK1, PREA_CMD);
596         clrsetbits_le32(&ddrphy->reg[2], 0x33, 0x20);
597         /* resume auto-refresh */
598         writel(trefi | BIT(31), &pctl->trefi);
599
600         return 0;
601 }
602
603 static int sdram_col_row_detect(struct udevice *dev)
604 {
605         struct dram_info *priv = dev_get_priv(dev);
606         struct rk3368_sdram_params *params = dev_get_platdata(dev);
607         struct rk3368_ddr_pctl *pctl = priv->pctl;
608         struct rk3368_msch *msch = priv->msch;
609         const u32 test_pattern = 0x5aa5f00f;
610         int row, col;
611         uintptr_t addr;
612
613         move_to_config_state(pctl);
614         writel(6, &msch->ddrconf);
615         move_to_access_state(pctl);
616
617         /* Detect col */
618         for (col = 11; col >= 9; col--) {
619                 writel(0, CONFIG_SYS_SDRAM_BASE);
620                 addr = CONFIG_SYS_SDRAM_BASE +
621                         (1 << (col + params->chan.bw - 1));
622                 writel(test_pattern, addr);
623                 if ((readl(addr) == test_pattern) &&
624                     (readl(CONFIG_SYS_SDRAM_BASE) == 0))
625                         break;
626         }
627
628         if (col == 8) {
629                 pr_err("%s: col detect error\n", __func__);
630                 return -EINVAL;
631         }
632
633         move_to_config_state(pctl);
634         writel(15, &msch->ddrconf);
635         move_to_access_state(pctl);
636
637         /* Detect row*/
638         for (row = 16; row >= 12; row--) {
639                 writel(0, CONFIG_SYS_SDRAM_BASE);
640                 addr = CONFIG_SYS_SDRAM_BASE + (1 << (row + 15 - 1));
641                 writel(test_pattern, addr);
642                 if ((readl(addr) == test_pattern) &&
643                     (readl(CONFIG_SYS_SDRAM_BASE) == 0))
644                         break;
645         }
646
647         if (row == 11) {
648                 pr_err("%s: row detect error\n", __func__);
649                 return -EINVAL;
650         }
651
652         /* Record results */
653         debug("%s: col %d, row %d\n", __func__, col, row);
654         params->chan.col = col;
655         params->chan.cs0_row = row;
656         params->chan.cs1_row = row;
657         params->chan.row_3_4 = 0;
658
659         return 0;
660 }
661
662 static int msch_niu_config(struct rk3368_msch *msch,
663                            struct rk3368_sdram_params *params)
664 {
665         int i;
666         const u8 cols = params->chan.col - ((params->chan.bw == 2) ? 0 : 1);
667         const u8 rows = params->chan.cs0_row;
668
669         /*
670          * The DDR address-translation table always assumes a 32bit
671          * bus and the comparison below takes care of adjusting for
672          * a 16bit bus (i.e. one column-address is consumed).
673          */
674         const struct {
675                 u8 rows;
676                 u8 columns;
677                 u8 type;
678         } ddrconf_table[] = {
679                 /*
680                  * C-B-R-D patterns are first. For these we require an
681                  * exact match for the columns and rows (as there's
682                  * one entry per possible configuration).
683                  */
684                 [0] =  { .rows = 13, .columns = 10, .type = DMC_MSCH_CBRD },
685                 [1] =  { .rows = 14, .columns = 10, .type = DMC_MSCH_CBRD },
686                 [2] =  { .rows = 15, .columns = 10, .type = DMC_MSCH_CBRD },
687                 [3] =  { .rows = 16, .columns = 10, .type = DMC_MSCH_CBRD },
688                 [4] =  { .rows = 14, .columns = 11, .type = DMC_MSCH_CBRD },
689                 [5] =  { .rows = 15, .columns = 11, .type = DMC_MSCH_CBRD },
690                 [6] =  { .rows = 16, .columns = 11, .type = DMC_MSCH_CBRD },
691                 [7] =  { .rows = 13, .columns = 9, .type = DMC_MSCH_CBRD },
692                 [8] =  { .rows = 14, .columns = 9, .type = DMC_MSCH_CBRD },
693                 [9] =  { .rows = 15, .columns = 9, .type = DMC_MSCH_CBRD },
694                 [10] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBRD },
695                 /*
696                  * 11 through 13 are C-R-B-D patterns. These are
697                  * matched for an exact number of columns and to
698                  * ensure that the hardware uses at least as many rows
699                  * as the pattern requires (i.e. we make sure that
700                  * there's no gaps up until we hit the device/chip-select;
701                  * however, these patterns can accept up to 16 rows,
702                  * as the row-address continues right after the CS
703                  * switching)
704                  */
705                 [11] = { .rows = 15, .columns = 10, .type = DMC_MSCH_CRBD },
706                 [12] = { .rows = 14, .columns = 11, .type = DMC_MSCH_CRBD },
707                 [13] = { .rows = 13, .columns = 10, .type = DMC_MSCH_CRBD },
708                 /*
709                  * 14 and 15 are catch-all variants using a C-B-D-R
710                  * scheme (i.e. alternating the chip-select every time
711                  * C-B overflows) and stuffing the remaining C-bits
712                  * into the top. Matching needs to make sure that the
713                  * number of columns is either an exact match (i.e. we
714                  * can use less the the maximum number of rows) -or-
715                  * that the columns exceed what is given in this table
716                  * and the rows are an exact match (in which case the
717                  * remaining C-bits will be stuffed onto the top after
718                  * the device/chip-select switches).
719                  */
720                 [14] = { .rows = 16, .columns = 10, .type = DMC_MSCH_CBDR },
721                 [15] = { .rows = 16, .columns = 9, .type = DMC_MSCH_CBDR },
722         };
723
724         /*
725          * For C-B-R-D, we need an exact match (i.e. both for the number of
726          * columns and rows), while for C-B-D-R, only the the number of
727          * columns needs to match.
728          */
729         for (i = 0; i < ARRAY_SIZE(ddrconf_table); i++) {
730                 bool match = false;
731
732                 /* If this entry if for a different matcher, then skip it */
733                 if (ddrconf_table[i].type != params->memory_schedule)
734                         continue;
735
736                 /*
737                  * Match according to the rules (exact/inexact/at-least)
738                  * documented in the ddrconf_table above.
739                  */
740                 switch (params->memory_schedule) {
741                 case DMC_MSCH_CBRD:
742                         match = (ddrconf_table[i].columns == cols) &&
743                                 (ddrconf_table[i].rows == rows);
744                         break;
745
746                 case DMC_MSCH_CRBD:
747                         match = (ddrconf_table[i].columns == cols) &&
748                                 (ddrconf_table[i].rows <= rows);
749                         break;
750
751                 case DMC_MSCH_CBDR:
752                         match = (ddrconf_table[i].columns == cols) ||
753                                 ((ddrconf_table[i].columns <= cols) &&
754                                  (ddrconf_table[i].rows == rows));
755                         break;
756
757                 default:
758                         break;
759                 }
760
761                 if (match) {
762                         debug("%s: setting ddrconf 0x%x\n", __func__, i);
763                         writel(i, &msch->ddrconf);
764                         return 0;
765                 }
766         }
767
768         pr_err("%s: ddrconf (NIU config) not found\n", __func__);
769         return -EINVAL;
770 }
771
772 static void dram_all_config(struct udevice *dev)
773 {
774         struct dram_info *priv = dev_get_priv(dev);
775         struct rk3368_pmu_grf *pmugrf = priv->pmugrf;
776         struct rk3368_sdram_params *params = dev_get_platdata(dev);
777         const struct rk3288_sdram_channel *info = &params->chan;
778         u32 sys_reg = 0;
779         const int chan = 0;
780
781         sys_reg |= DDR3 << SYS_REG_DDRTYPE_SHIFT;
782         sys_reg |= 0 << SYS_REG_NUM_CH_SHIFT;
783
784         sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(chan);
785         sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(chan);
786         sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(chan);
787         sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(chan);
788         sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(chan);
789         sys_reg |= (info->cs0_row - 13) << SYS_REG_CS0_ROW_SHIFT(chan);
790         sys_reg |= (info->cs1_row - 13) << SYS_REG_CS1_ROW_SHIFT(chan);
791         sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(chan);
792         sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(chan);
793
794         writel(sys_reg, &pmugrf->os_reg[2]);
795 }
796
797 static int setup_sdram(struct udevice *dev)
798 {
799         struct dram_info *priv = dev_get_priv(dev);
800         struct rk3368_sdram_params *params = dev_get_platdata(dev);
801
802         struct rk3368_ddr_pctl *pctl = priv->pctl;
803         struct rk3368_ddrphy *ddrphy = priv->phy;
804         struct rk3368_cru *cru = priv->cru;
805         struct rk3368_grf *grf = priv->grf;
806         struct rk3368_msch *msch = priv->msch;
807
808         int ret;
809
810         /* The input clock (i.e. DPLL) needs to be 2x the DRAM frequency */
811         ret = clk_set_rate(&priv->ddr_clk, 2 * params->ddr_freq);
812         if (ret < 0) {
813                 debug("%s: could not set DDR clock: %d\n", __func__, ret);
814                 return ret;
815         }
816
817         /* Update the read-latency for the RK3368 */
818         writel(0x32, &msch->readlatency);
819
820         /* Initialise the DDR PCTL and DDR PHY */
821         ddrctl_reset(cru);
822         ddrphy_reset(ddrphy);
823         ddrphy_config_delays(ddrphy, params->ddr_freq);
824         dfi_cfg(pctl);
825         /* Configure relative system information of grf_ddrc0_con0 register */
826         ddr_set_ddr3_mode(grf, true);
827         ddr_set_noc_spr_err_stall(grf, true);
828         /* Calculate timings */
829         pctl_calc_timings(params, params->ddr_freq);
830         /* Initialise the device timings in protocol controller */
831         pctl_cfg(pctl, params, grf);
832         /* Configure AL, CL ... information of PHY registers */
833         ddrphy_config(ddrphy,
834                       params->pctl_timing.tcl,
835                       params->pctl_timing.tal,
836                       params->pctl_timing.tcwl);
837
838         /* Initialize DRAM and configure with mode-register values */
839         ret = memory_init(pctl, params);
840         if (ret)
841                 goto error;
842
843         move_to_config_state(pctl);
844         /* Perform data-training */
845         ddrphy_data_training(pctl, ddrphy);
846         move_to_access_state(pctl);
847
848         /* TODO(prt): could detect rank in training... */
849 #ifdef CONFIG_TARGET_EVB_PX5
850         params->chan.rank = 1;
851 #else
852         params->chan.rank = 2;
853 #endif
854         /* TODO(prt): bus width is not auto-detected (yet)... */
855         params->chan.bw = 2;  /* 32bit wide bus */
856         params->chan.dbw = params->chan.dbw;  /* 32bit wide bus */
857
858         /* DDR3 is always 8 bank */
859         params->chan.bk = 3;
860         /* Detect col and row number */
861         ret = sdram_col_row_detect(dev);
862         if (ret)
863                 goto error;
864
865         /* Configure NIU DDR configuration */
866         ret = msch_niu_config(msch, params);
867         if (ret)
868                 goto error;
869
870         /* set up OS_REG to communicate w/ next stage and OS */
871         dram_all_config(dev);
872
873         return 0;
874
875 error:
876         printf("DRAM init failed!\n");
877         hang();
878 }
879 #endif
880
881 static int rk3368_dmc_ofdata_to_platdata(struct udevice *dev)
882 {
883         int ret = 0;
884
885 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
886         struct rk3368_sdram_params *plat = dev_get_platdata(dev);
887
888         ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
889         if (ret)
890                 return ret;
891 #endif
892
893         return ret;
894 }
895
896 #if CONFIG_IS_ENABLED(OF_PLATDATA)
897 static int conv_of_platdata(struct udevice *dev)
898 {
899         struct rk3368_sdram_params *plat = dev_get_platdata(dev);
900         struct dtd_rockchip_rk3368_dmc *of_plat = &plat->of_plat;
901
902         plat->ddr_freq = of_plat->rockchip_ddr_frequency;
903         plat->ddr_speed_bin = of_plat->rockchip_ddr_speed_bin;
904         plat->memory_schedule = of_plat->rockchip_memory_schedule;
905
906         return 0;
907 }
908 #endif
909
910 static int rk3368_dmc_probe(struct udevice *dev)
911 {
912 #ifdef CONFIG_TPL_BUILD
913         struct rk3368_sdram_params *plat = dev_get_platdata(dev);
914         struct rk3368_ddr_pctl *pctl;
915         struct rk3368_ddrphy *ddrphy;
916         struct rk3368_cru *cru;
917         struct rk3368_grf *grf;
918         struct rk3368_msch *msch;
919         int ret;
920         struct udevice *dev_clk;
921 #endif
922         struct dram_info *priv = dev_get_priv(dev);
923
924 #if CONFIG_IS_ENABLED(OF_PLATDATA)
925         ret = conv_of_platdata(dev);
926         if (ret)
927                 return ret;
928 #endif
929
930         priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
931         debug("%s: pmugrf=%p\n", __func__, priv->pmugrf);
932
933 #ifdef CONFIG_TPL_BUILD
934         pctl = (struct rk3368_ddr_pctl *)plat->of_plat.reg[0];
935         ddrphy = (struct rk3368_ddrphy *)plat->of_plat.reg[2];
936         msch = syscon_get_first_range(ROCKCHIP_SYSCON_MSCH);
937         grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
938
939         priv->pctl = pctl;
940         priv->phy = ddrphy;
941         priv->msch = msch;
942         priv->grf = grf;
943
944         ret = rockchip_get_clk(&dev_clk);
945         if (ret)
946                 return ret;
947         priv->ddr_clk.id = CLK_DDR;
948         ret = clk_request(dev_clk, &priv->ddr_clk);
949         if (ret)
950                 return ret;
951
952         cru = rockchip_get_cru();
953         priv->cru = cru;
954         if (IS_ERR(priv->cru))
955                 return PTR_ERR(priv->cru);
956
957         ret = setup_sdram(dev);
958         if (ret)
959                 return ret;
960 #endif
961
962         priv->info.base = 0;
963         priv->info.size =
964                 rockchip_sdram_size((phys_addr_t)&priv->pmugrf->os_reg[2]);
965
966         /*
967         * we use the 0x00000000~0xfdffffff space since 0xff000000~0xffffffff
968         * is SoC register space (i.e. reserved), and 0xfe000000~0xfeffffff is
969         * inaccessible for some IP controller.
970         */
971         priv->info.size = min(priv->info.size, (size_t)0xfe000000);
972
973         return 0;
974 }
975
976 static int rk3368_dmc_get_info(struct udevice *dev, struct ram_info *info)
977 {
978         struct dram_info *priv = dev_get_priv(dev);
979
980         *info = priv->info;
981         return 0;
982 }
983
984 static struct ram_ops rk3368_dmc_ops = {
985         .get_info = rk3368_dmc_get_info,
986 };
987
988
989 static const struct udevice_id rk3368_dmc_ids[] = {
990         { .compatible = "rockchip,rk3368-dmc" },
991         { }
992 };
993
994 U_BOOT_DRIVER(dmc_rk3368) = {
995         .name = "rockchip_rk3368_dmc",
996         .id = UCLASS_RAM,
997         .of_match = rk3368_dmc_ids,
998         .ops = &rk3368_dmc_ops,
999         .probe = rk3368_dmc_probe,
1000         .priv_auto_alloc_size = sizeof(struct dram_info),
1001         .ofdata_to_platdata = rk3368_dmc_ofdata_to_platdata,
1002         .probe = rk3368_dmc_probe,
1003         .priv_auto_alloc_size = sizeof(struct dram_info),
1004         .platdata_auto_alloc_size = sizeof(struct rk3368_sdram_params),
1005 };