blob: 4f3ed739fbb22be3e14dbd890c27f1eb39e7fa61 [file] [log] [blame]
Kever Yangc43acfd2018-12-20 11:33:42 +08001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Heiko Stübner3e747192017-02-18 19:46:37 +01002/*
3 * (C) Copyright 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
5 *
Heiko Stübner3e747192017-02-18 19:46:37 +01006 * Adapted from the very similar rk3288 ddr init.
7 */
8
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <dt-structs.h>
13#include <errno.h>
Simon Glassdb41d652019-12-28 10:45:07 -070014#include <hang.h>
Simon Glass691d7192020-05-10 11:40:02 -060015#include <init.h>
Heiko Stübner3e747192017-02-18 19:46:37 +010016#include <ram.h>
17#include <regmap.h>
18#include <syscon.h>
19#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080020#include <asm/arch-rockchip/clock.h>
21#include <asm/arch-rockchip/cru_rk3188.h>
22#include <asm/arch-rockchip/ddr_rk3188.h>
23#include <asm/arch-rockchip/grf_rk3188.h>
24#include <asm/arch-rockchip/pmu_rk3188.h>
Kever Yang5d19ddf2019-11-15 11:04:33 +080025#include <asm/arch-rockchip/sdram.h>
Kever Yang2a2f0b12019-11-15 11:04:32 +080026#include <asm/arch-rockchip/sdram_rk3288.h>
Heiko Stübner3e747192017-02-18 19:46:37 +010027#include <linux/err.h>
28
Heiko Stübner3e747192017-02-18 19:46:37 +010029struct chan_info {
30 struct rk3288_ddr_pctl *pctl;
31 struct rk3288_ddr_publ *publ;
32 struct rk3188_msch *msch;
33};
34
35struct dram_info {
36 struct chan_info chan[1];
37 struct ram_info info;
38 struct clk ddr_clk;
39 struct rk3188_cru *cru;
40 struct rk3188_grf *grf;
41 struct rk3188_sgrf *sgrf;
42 struct rk3188_pmu *pmu;
43};
44
45struct rk3188_sdram_params {
46#if CONFIG_IS_ENABLED(OF_PLATDATA)
47 struct dtd_rockchip_rk3188_dmc of_plat;
48#endif
49 struct rk3288_sdram_channel ch[2];
50 struct rk3288_sdram_pctl_timing pctl_timing;
51 struct rk3288_sdram_phy_timing phy_timing;
52 struct rk3288_base_params base;
53 int num_channels;
54 struct regmap *map;
55};
56
57const int ddrconf_table[] = {
58 /*
59 * [5:4] row(13+n)
60 * [1:0] col(9+n), assume bw=2
61 * row col,bw
62 */
63 0,
64 ((2 << DDRCONF_ROW_SHIFT) | 1 << DDRCONF_COL_SHIFT),
65 ((1 << DDRCONF_ROW_SHIFT) | 1 << DDRCONF_COL_SHIFT),
66 ((0 << DDRCONF_ROW_SHIFT) | 1 << DDRCONF_COL_SHIFT),
67 ((2 << DDRCONF_ROW_SHIFT) | 2 << DDRCONF_COL_SHIFT),
68 ((1 << DDRCONF_ROW_SHIFT) | 2 << DDRCONF_COL_SHIFT),
69 ((0 << DDRCONF_ROW_SHIFT) | 2 << DDRCONF_COL_SHIFT),
70 ((1 << DDRCONF_ROW_SHIFT) | 0 << DDRCONF_COL_SHIFT),
71 ((0 << DDRCONF_ROW_SHIFT) | 0 << DDRCONF_COL_SHIFT),
72 0,
73 0,
74 0,
75 0,
76 0,
77 0,
78 0,
79};
80
81#define TEST_PATTEN 0x5aa5f00f
82#define DQS_GATE_TRAINING_ERROR_RANK0 (1 << 4)
83#define DQS_GATE_TRAINING_ERROR_RANK1 (2 << 4)
84
85#ifdef CONFIG_SPL_BUILD
86static void copy_to_reg(u32 *dest, const u32 *src, u32 n)
87{
88 int i;
89
90 for (i = 0; i < n / sizeof(u32); i++) {
91 writel(*src, dest);
92 src++;
93 dest++;
94 }
95}
96
97static void ddr_reset(struct rk3188_cru *cru, u32 ch, u32 ctl, u32 phy)
98{
99 u32 phy_ctl_srstn_shift = 13;
100 u32 ctl_psrstn_shift = 11;
101 u32 ctl_srstn_shift = 10;
102 u32 phy_psrstn_shift = 9;
103 u32 phy_srstn_shift = 8;
104
105 rk_clrsetreg(&cru->cru_softrst_con[5],
106 1 << phy_ctl_srstn_shift | 1 << ctl_psrstn_shift |
107 1 << ctl_srstn_shift | 1 << phy_psrstn_shift |
108 1 << phy_srstn_shift,
109 phy << phy_ctl_srstn_shift | ctl << ctl_psrstn_shift |
110 ctl << ctl_srstn_shift | phy << phy_psrstn_shift |
111 phy << phy_srstn_shift);
112}
113
114static void ddr_phy_ctl_reset(struct rk3188_cru *cru, u32 ch, u32 n)
115{
116 u32 phy_ctl_srstn_shift = 13;
117
118 rk_clrsetreg(&cru->cru_softrst_con[5],
119 1 << phy_ctl_srstn_shift, n << phy_ctl_srstn_shift);
120}
121
122static void phy_pctrl_reset(struct rk3188_cru *cru,
123 struct rk3288_ddr_publ *publ,
124 int channel)
125{
126 int i;
127
128 ddr_reset(cru, channel, 1, 1);
129 udelay(1);
130 clrbits_le32(&publ->acdllcr, ACDLLCR_DLLSRST);
131 for (i = 0; i < 4; i++)
132 clrbits_le32(&publ->datx8[i].dxdllcr, DXDLLCR_DLLSRST);
133
134 udelay(10);
135 setbits_le32(&publ->acdllcr, ACDLLCR_DLLSRST);
136 for (i = 0; i < 4; i++)
137 setbits_le32(&publ->datx8[i].dxdllcr, DXDLLCR_DLLSRST);
138
139 udelay(10);
140 ddr_reset(cru, channel, 1, 0);
141 udelay(10);
142 ddr_reset(cru, channel, 0, 0);
143 udelay(10);
144}
145
146static void phy_dll_bypass_set(struct rk3288_ddr_publ *publ,
147 u32 freq)
148{
149 int i;
150
151 if (freq <= 250000000) {
152 if (freq <= 150000000)
153 clrbits_le32(&publ->dllgcr, SBIAS_BYPASS);
154 else
155 setbits_le32(&publ->dllgcr, SBIAS_BYPASS);
156 setbits_le32(&publ->acdllcr, ACDLLCR_DLLDIS);
157 for (i = 0; i < 4; i++)
158 setbits_le32(&publ->datx8[i].dxdllcr,
159 DXDLLCR_DLLDIS);
160
161 setbits_le32(&publ->pir, PIR_DLLBYP);
162 } else {
163 clrbits_le32(&publ->dllgcr, SBIAS_BYPASS);
164 clrbits_le32(&publ->acdllcr, ACDLLCR_DLLDIS);
165 for (i = 0; i < 4; i++) {
166 clrbits_le32(&publ->datx8[i].dxdllcr,
167 DXDLLCR_DLLDIS);
168 }
169
170 clrbits_le32(&publ->pir, PIR_DLLBYP);
171 }
172}
173
174static void dfi_cfg(struct rk3288_ddr_pctl *pctl, u32 dramtype)
175{
176 writel(DFI_INIT_START, &pctl->dfistcfg0);
177 writel(DFI_DRAM_CLK_SR_EN | DFI_DRAM_CLK_DPD_EN,
178 &pctl->dfistcfg1);
179 writel(DFI_PARITY_INTR_EN | DFI_PARITY_EN, &pctl->dfistcfg2);
180 writel(7 << TLP_RESP_TIME_SHIFT | LP_SR_EN | LP_PD_EN,
181 &pctl->dfilpcfg0);
182
183 writel(2 << TCTRL_DELAY_TIME_SHIFT, &pctl->dfitctrldelay);
184 writel(1 << TPHY_WRDATA_TIME_SHIFT, &pctl->dfitphywrdata);
185 writel(0xf << TPHY_RDLAT_TIME_SHIFT, &pctl->dfitphyrdlat);
186 writel(2 << TDRAM_CLK_DIS_TIME_SHIFT, &pctl->dfitdramclkdis);
187 writel(2 << TDRAM_CLK_EN_TIME_SHIFT, &pctl->dfitdramclken);
188 writel(1, &pctl->dfitphyupdtype0);
189
190 /* cs0 and cs1 write odt enable */
191 writel((RANK0_ODT_WRITE_SEL | RANK1_ODT_WRITE_SEL),
192 &pctl->dfiodtcfg);
193 /* odt write length */
194 writel(7 << ODT_LEN_BL8_W_SHIFT, &pctl->dfiodtcfg1);
195 /* phyupd and ctrlupd disabled */
196 writel(0, &pctl->dfiupdcfg);
197}
198
199static void ddr_set_enable(struct rk3188_grf *grf, uint channel, bool enable)
200{
201 uint val = 0;
202
203 if (enable)
204 val = 1 << DDR_16BIT_EN_SHIFT;
205
206 rk_clrsetreg(&grf->ddrc_con0, 1 << DDR_16BIT_EN_SHIFT, val);
207}
208
209static void ddr_set_ddr3_mode(struct rk3188_grf *grf, uint channel,
210 bool ddr3_mode)
211{
212 uint mask, val;
213
214 mask = MSCH4_MAINDDR3_MASK << MSCH4_MAINDDR3_SHIFT;
215 val = ddr3_mode << MSCH4_MAINDDR3_SHIFT;
216 rk_clrsetreg(&grf->soc_con2, mask, val);
217}
218
219static void ddr_rank_2_row15en(struct rk3188_grf *grf, bool enable)
220{
221 uint mask, val;
222
223 mask = RANK_TO_ROW15_EN_MASK << RANK_TO_ROW15_EN_SHIFT;
224 val = enable << RANK_TO_ROW15_EN_SHIFT;
225 rk_clrsetreg(&grf->soc_con2, mask, val);
226}
227
228static void pctl_cfg(int channel, struct rk3288_ddr_pctl *pctl,
229 struct rk3188_sdram_params *sdram_params,
230 struct rk3188_grf *grf)
231{
232 copy_to_reg(&pctl->togcnt1u, &sdram_params->pctl_timing.togcnt1u,
233 sizeof(sdram_params->pctl_timing));
234 switch (sdram_params->base.dramtype) {
235 case DDR3:
236 if (sdram_params->phy_timing.mr[1] & DDR3_DLL_DISABLE) {
237 writel(sdram_params->pctl_timing.tcl - 3,
238 &pctl->dfitrddataen);
239 } else {
240 writel(sdram_params->pctl_timing.tcl - 2,
241 &pctl->dfitrddataen);
242 }
243 writel(sdram_params->pctl_timing.tcwl - 1,
244 &pctl->dfitphywrlat);
245 writel(0 << MDDR_LPDDR2_CLK_STOP_IDLE_SHIFT | DDR3_EN |
246 DDR2_DDR3_BL_8 | (6 - 4) << TFAW_SHIFT | PD_EXIT_SLOW |
247 1 << PD_TYPE_SHIFT | 0 << PD_IDLE_SHIFT,
248 &pctl->mcfg);
249 ddr_set_ddr3_mode(grf, channel, true);
250 ddr_set_enable(grf, channel, true);
251 break;
252 }
253
254 setbits_le32(&pctl->scfg, 1);
255}
256
257static void phy_cfg(const struct chan_info *chan, int channel,
258 struct rk3188_sdram_params *sdram_params)
259{
260 struct rk3288_ddr_publ *publ = chan->publ;
261 struct rk3188_msch *msch = chan->msch;
262 uint ddr_freq_mhz = sdram_params->base.ddr_freq / 1000000;
263 u32 dinit2;
264 int i;
265
266 dinit2 = DIV_ROUND_UP(ddr_freq_mhz * 200000, 1000);
267 /* DDR PHY Timing */
268 copy_to_reg(&publ->dtpr[0], &sdram_params->phy_timing.dtpr0,
269 sizeof(sdram_params->phy_timing));
270 writel(sdram_params->base.noc_timing, &msch->ddrtiming);
271 writel(0x3f, &msch->readlatency);
272 writel(DIV_ROUND_UP(ddr_freq_mhz * 5120, 1000) << PRT_DLLLOCK_SHIFT |
273 DIV_ROUND_UP(ddr_freq_mhz * 50, 1000) << PRT_DLLSRST_SHIFT |
274 8 << PRT_ITMSRST_SHIFT, &publ->ptr[0]);
275 writel(DIV_ROUND_UP(ddr_freq_mhz * 500000, 1000) << PRT_DINIT0_SHIFT |
276 DIV_ROUND_UP(ddr_freq_mhz * 400, 1000) << PRT_DINIT1_SHIFT,
277 &publ->ptr[1]);
278 writel(min(dinit2, 0x1ffffU) << PRT_DINIT2_SHIFT |
279 DIV_ROUND_UP(ddr_freq_mhz * 1000, 1000) << PRT_DINIT3_SHIFT,
280 &publ->ptr[2]);
281
282 switch (sdram_params->base.dramtype) {
283 case DDR3:
284 clrbits_le32(&publ->pgcr, 0x1f);
285 clrsetbits_le32(&publ->dcr, DDRMD_MASK << DDRMD_SHIFT,
286 DDRMD_DDR3 << DDRMD_SHIFT);
287 break;
288 }
289 if (sdram_params->base.odt) {
290 /*dynamic RTT enable */
291 for (i = 0; i < 4; i++)
292 setbits_le32(&publ->datx8[i].dxgcr, DQSRTT | DQRTT);
293 } else {
294 /*dynamic RTT disable */
295 for (i = 0; i < 4; i++)
296 clrbits_le32(&publ->datx8[i].dxgcr, DQSRTT | DQRTT);
297 }
298}
299
300static void phy_init(struct rk3288_ddr_publ *publ)
301{
302 setbits_le32(&publ->pir, PIR_INIT | PIR_DLLSRST
303 | PIR_DLLLOCK | PIR_ZCAL | PIR_ITMSRST | PIR_CLRSR);
304 udelay(1);
305 while ((readl(&publ->pgsr) &
306 (PGSR_IDONE | PGSR_DLDONE | PGSR_ZCDONE)) !=
307 (PGSR_IDONE | PGSR_DLDONE | PGSR_ZCDONE))
308 ;
309}
310
311static void send_command(struct rk3288_ddr_pctl *pctl, u32 rank,
312 u32 cmd, u32 arg)
313{
314 writel((START_CMD | (rank << 20) | arg | cmd), &pctl->mcmd);
315 udelay(1);
316 while (readl(&pctl->mcmd) & START_CMD)
317 ;
318}
319
320static inline void send_command_op(struct rk3288_ddr_pctl *pctl,
321 u32 rank, u32 cmd, u32 ma, u32 op)
322{
323 send_command(pctl, rank, cmd, (ma & LPDDR2_MA_MASK) << LPDDR2_MA_SHIFT |
324 (op & LPDDR2_OP_MASK) << LPDDR2_OP_SHIFT);
325}
326
327static void memory_init(struct rk3288_ddr_publ *publ,
328 u32 dramtype)
329{
330 setbits_le32(&publ->pir,
331 (PIR_INIT | PIR_DRAMINIT | PIR_LOCKBYP
332 | PIR_ZCALBYP | PIR_CLRSR | PIR_ICPC
333 | (dramtype == DDR3 ? PIR_DRAMRST : 0)));
334 udelay(1);
335 while ((readl(&publ->pgsr) & (PGSR_IDONE | PGSR_DLDONE))
336 != (PGSR_IDONE | PGSR_DLDONE))
337 ;
338}
339
340static void move_to_config_state(struct rk3288_ddr_publ *publ,
341 struct rk3288_ddr_pctl *pctl)
342{
343 unsigned int state;
344
345 while (1) {
346 state = readl(&pctl->stat) & PCTL_STAT_MSK;
347
348 switch (state) {
349 case LOW_POWER:
350 writel(WAKEUP_STATE, &pctl->sctl);
351 while ((readl(&pctl->stat) & PCTL_STAT_MSK)
352 != ACCESS)
353 ;
354 /* wait DLL lock */
355 while ((readl(&publ->pgsr) & PGSR_DLDONE)
356 != PGSR_DLDONE)
357 ;
358 /*
359 * if at low power state,need wakeup first,
360 * and then enter the config, so
361 * fallthrough
362 */
363 case ACCESS:
364 /* fallthrough */
365 case INIT_MEM:
366 writel(CFG_STATE, &pctl->sctl);
367 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
368 ;
369 break;
370 case CONFIG:
371 return;
372 default:
373 break;
374 }
375 }
376}
377
378static void set_bandwidth_ratio(const struct chan_info *chan, int channel,
379 u32 n, struct rk3188_grf *grf)
380{
381 struct rk3288_ddr_pctl *pctl = chan->pctl;
382 struct rk3288_ddr_publ *publ = chan->publ;
383 struct rk3188_msch *msch = chan->msch;
384
385 if (n == 1) {
386 setbits_le32(&pctl->ppcfg, 1);
387 ddr_set_enable(grf, channel, 1);
388 setbits_le32(&msch->ddrtiming, 1 << 31);
389 /* Data Byte disable*/
390 clrbits_le32(&publ->datx8[2].dxgcr, 1);
391 clrbits_le32(&publ->datx8[3].dxgcr, 1);
392 /* disable DLL */
393 setbits_le32(&publ->datx8[2].dxdllcr, DXDLLCR_DLLDIS);
394 setbits_le32(&publ->datx8[3].dxdllcr, DXDLLCR_DLLDIS);
395 } else {
396 clrbits_le32(&pctl->ppcfg, 1);
397 ddr_set_enable(grf, channel, 0);
398 clrbits_le32(&msch->ddrtiming, 1 << 31);
399 /* Data Byte enable*/
400 setbits_le32(&publ->datx8[2].dxgcr, 1);
401 setbits_le32(&publ->datx8[3].dxgcr, 1);
402
403 /* enable DLL */
404 clrbits_le32(&publ->datx8[2].dxdllcr, DXDLLCR_DLLDIS);
405 clrbits_le32(&publ->datx8[3].dxdllcr, DXDLLCR_DLLDIS);
406 /* reset DLL */
407 clrbits_le32(&publ->datx8[2].dxdllcr, DXDLLCR_DLLSRST);
408 clrbits_le32(&publ->datx8[3].dxdllcr, DXDLLCR_DLLSRST);
409 udelay(10);
410 setbits_le32(&publ->datx8[2].dxdllcr, DXDLLCR_DLLSRST);
411 setbits_le32(&publ->datx8[3].dxdllcr, DXDLLCR_DLLSRST);
412 }
413 setbits_le32(&pctl->dfistcfg0, 1 << 2);
414}
415
416static int data_training(const struct chan_info *chan, int channel,
417 struct rk3188_sdram_params *sdram_params)
418{
419 unsigned int j;
420 int ret = 0;
421 u32 rank;
422 int i;
423 u32 step[2] = { PIR_QSTRN, PIR_RVTRN };
424 struct rk3288_ddr_publ *publ = chan->publ;
425 struct rk3288_ddr_pctl *pctl = chan->pctl;
426
427 /* disable auto refresh */
428 writel(0, &pctl->trefi);
429
430 if (sdram_params->base.dramtype != LPDDR3)
431 setbits_le32(&publ->pgcr, 1 << PGCR_DQSCFG_SHIFT);
432 rank = sdram_params->ch[channel].rank | 1;
433 for (j = 0; j < ARRAY_SIZE(step); j++) {
434 /*
435 * trigger QSTRN and RVTRN
436 * clear DTDONE status
437 */
438 setbits_le32(&publ->pir, PIR_CLRSR);
439
440 /* trigger DTT */
441 setbits_le32(&publ->pir,
442 PIR_INIT | step[j] | PIR_LOCKBYP | PIR_ZCALBYP |
443 PIR_CLRSR);
444 udelay(1);
445 /* wait echo byte DTDONE */
446 while ((readl(&publ->datx8[0].dxgsr[0]) & rank)
447 != rank)
448 ;
449 while ((readl(&publ->datx8[1].dxgsr[0]) & rank)
450 != rank)
451 ;
452 if (!(readl(&pctl->ppcfg) & 1)) {
453 while ((readl(&publ->datx8[2].dxgsr[0])
454 & rank) != rank)
455 ;
456 while ((readl(&publ->datx8[3].dxgsr[0])
457 & rank) != rank)
458 ;
459 }
460 if (readl(&publ->pgsr) &
461 (PGSR_DTERR | PGSR_RVERR | PGSR_RVEIRR)) {
462 ret = -1;
463 break;
464 }
465 }
466 /* send some auto refresh to complement the lost while DTT */
467 for (i = 0; i < (rank > 1 ? 8 : 4); i++)
468 send_command(pctl, rank, REF_CMD, 0);
469
470 if (sdram_params->base.dramtype != LPDDR3)
471 clrbits_le32(&publ->pgcr, 1 << PGCR_DQSCFG_SHIFT);
472
473 /* resume auto refresh */
474 writel(sdram_params->pctl_timing.trefi, &pctl->trefi);
475
476 return ret;
477}
478
479static void move_to_access_state(const struct chan_info *chan)
480{
481 struct rk3288_ddr_publ *publ = chan->publ;
482 struct rk3288_ddr_pctl *pctl = chan->pctl;
483 unsigned int state;
484
485 while (1) {
486 state = readl(&pctl->stat) & PCTL_STAT_MSK;
487
488 switch (state) {
489 case LOW_POWER:
490 if (((readl(&pctl->stat) >> LP_TRIG_SHIFT) &
491 LP_TRIG_MASK) == 1)
492 return;
493
494 writel(WAKEUP_STATE, &pctl->sctl);
495 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != ACCESS)
496 ;
497 /* wait DLL lock */
498 while ((readl(&publ->pgsr) & PGSR_DLDONE)
499 != PGSR_DLDONE)
500 ;
501 break;
502 case INIT_MEM:
503 writel(CFG_STATE, &pctl->sctl);
504 while ((readl(&pctl->stat) & PCTL_STAT_MSK) != CONFIG)
505 ;
506 /* fallthrough */
507 case CONFIG:
508 writel(GO_STATE, &pctl->sctl);
509 while ((readl(&pctl->stat) & PCTL_STAT_MSK) == CONFIG)
510 ;
511 break;
512 case ACCESS:
513 return;
514 default:
515 break;
516 }
517 }
518}
519
520static void dram_cfg_rbc(const struct chan_info *chan, u32 chnum,
521 struct rk3188_sdram_params *sdram_params)
522{
523 struct rk3288_ddr_publ *publ = chan->publ;
524
525 if (sdram_params->ch[chnum].bk == 3)
526 clrsetbits_le32(&publ->dcr, PDQ_MASK << PDQ_SHIFT,
527 1 << PDQ_SHIFT);
528 else
529 clrbits_le32(&publ->dcr, PDQ_MASK << PDQ_SHIFT);
530
531 writel(sdram_params->base.ddrconfig, &chan->msch->ddrconf);
532}
533
534static void dram_all_config(const struct dram_info *dram,
535 struct rk3188_sdram_params *sdram_params)
536{
537 unsigned int chan;
538 u32 sys_reg = 0;
539
540 sys_reg |= sdram_params->base.dramtype << SYS_REG_DDRTYPE_SHIFT;
541 sys_reg |= (sdram_params->num_channels - 1) << SYS_REG_NUM_CH_SHIFT;
542 for (chan = 0; chan < sdram_params->num_channels; chan++) {
543 const struct rk3288_sdram_channel *info =
544 &sdram_params->ch[chan];
545
546 sys_reg |= info->row_3_4 << SYS_REG_ROW_3_4_SHIFT(chan);
547 sys_reg |= 1 << SYS_REG_CHINFO_SHIFT(chan);
548 sys_reg |= (info->rank - 1) << SYS_REG_RANK_SHIFT(chan);
549 sys_reg |= (info->col - 9) << SYS_REG_COL_SHIFT(chan);
550 sys_reg |= info->bk == 3 ? 0 : 1 << SYS_REG_BK_SHIFT(chan);
551 sys_reg |= (info->cs0_row - 13) << SYS_REG_CS0_ROW_SHIFT(chan);
552 sys_reg |= (info->cs1_row - 13) << SYS_REG_CS1_ROW_SHIFT(chan);
553 sys_reg |= (2 >> info->bw) << SYS_REG_BW_SHIFT(chan);
554 sys_reg |= (2 >> info->dbw) << SYS_REG_DBW_SHIFT(chan);
555
556 dram_cfg_rbc(&dram->chan[chan], chan, sdram_params);
557 }
558 if (sdram_params->ch[0].rank == 2)
559 ddr_rank_2_row15en(dram->grf, 0);
560 else
561 ddr_rank_2_row15en(dram->grf, 1);
562
563 writel(sys_reg, &dram->pmu->sys_reg[2]);
564}
565
566static int sdram_rank_bw_detect(struct dram_info *dram, int channel,
567 struct rk3188_sdram_params *sdram_params)
568{
569 int reg;
570 int need_trainig = 0;
571 const struct chan_info *chan = &dram->chan[channel];
572 struct rk3288_ddr_publ *publ = chan->publ;
573
574 ddr_rank_2_row15en(dram->grf, 0);
575
576 if (data_training(chan, channel, sdram_params) < 0) {
577 printf("first data training fail!\n");
578 reg = readl(&publ->datx8[0].dxgsr[0]);
579 /* Check the result for rank 0 */
580 if ((channel == 0) && (reg & DQS_GATE_TRAINING_ERROR_RANK0)) {
581 printf("data training fail!\n");
582 return -EIO;
583 }
584
585 /* Check the result for rank 1 */
586 if (reg & DQS_GATE_TRAINING_ERROR_RANK1) {
587 sdram_params->ch[channel].rank = 1;
588 clrsetbits_le32(&publ->pgcr, 0xF << 18,
589 sdram_params->ch[channel].rank << 18);
590 need_trainig = 1;
591 }
592 reg = readl(&publ->datx8[2].dxgsr[0]);
593 if (reg & (1 << 4)) {
594 sdram_params->ch[channel].bw = 1;
595 set_bandwidth_ratio(chan, channel,
596 sdram_params->ch[channel].bw,
597 dram->grf);
598 need_trainig = 1;
599 }
600 }
601 /* Assume the Die bit width are the same with the chip bit width */
602 sdram_params->ch[channel].dbw = sdram_params->ch[channel].bw;
603
604 if (need_trainig &&
605 (data_training(chan, channel, sdram_params) < 0)) {
606 if (sdram_params->base.dramtype == LPDDR3) {
607 ddr_phy_ctl_reset(dram->cru, channel, 1);
608 udelay(10);
609 ddr_phy_ctl_reset(dram->cru, channel, 0);
610 udelay(10);
611 }
612 printf("2nd data training failed!");
613 return -EIO;
614 }
615
616 return 0;
617}
618
619/*
620 * Detect ram columns and rows.
621 * @dram: dram info struct
622 * @channel: channel number to handle
623 * @sdram_params: sdram parameters, function will fill in col and row values
624 *
625 * Returns 0 or negative on error.
626 */
627static int sdram_col_row_detect(struct dram_info *dram, int channel,
628 struct rk3188_sdram_params *sdram_params)
629{
630 int row, col;
631 unsigned int addr;
632 const struct chan_info *chan = &dram->chan[channel];
633 struct rk3288_ddr_pctl *pctl = chan->pctl;
634 struct rk3288_ddr_publ *publ = chan->publ;
635 int ret = 0;
636
637 /* Detect col */
638 for (col = 11; col >= 9; col--) {
639 writel(0, CONFIG_SYS_SDRAM_BASE);
640 addr = CONFIG_SYS_SDRAM_BASE +
641 (1 << (col + sdram_params->ch[channel].bw - 1));
642 writel(TEST_PATTEN, addr);
643 if ((readl(addr) == TEST_PATTEN) &&
644 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
645 break;
646 }
647 if (col == 8) {
648 printf("Col detect error\n");
649 ret = -EINVAL;
650 goto out;
651 } else {
652 sdram_params->ch[channel].col = col;
653 }
654
655 ddr_rank_2_row15en(dram->grf, 1);
656 move_to_config_state(publ, pctl);
657 writel(1, &chan->msch->ddrconf);
658 move_to_access_state(chan);
659 /* Detect row, max 15,min13 in rk3188*/
660 for (row = 16; row >= 13; row--) {
661 writel(0, CONFIG_SYS_SDRAM_BASE);
662 addr = CONFIG_SYS_SDRAM_BASE + (1 << (row + 15 - 1));
663 writel(TEST_PATTEN, addr);
664 if ((readl(addr) == TEST_PATTEN) &&
665 (readl(CONFIG_SYS_SDRAM_BASE) == 0))
666 break;
667 }
668 if (row == 12) {
669 printf("Row detect error\n");
670 ret = -EINVAL;
671 } else {
672 sdram_params->ch[channel].cs1_row = row;
673 sdram_params->ch[channel].row_3_4 = 0;
674 debug("chn %d col %d, row %d\n", channel, col, row);
675 sdram_params->ch[channel].cs0_row = row;
676 }
677
678out:
679 return ret;
680}
681
682static int sdram_get_niu_config(struct rk3188_sdram_params *sdram_params)
683{
Kever Yanga27290a2017-09-25 16:33:22 +0800684 int i, tmp, size, row, ret = 0;
Heiko Stübner3e747192017-02-18 19:46:37 +0100685
Kever Yanga27290a2017-09-25 16:33:22 +0800686 row = sdram_params->ch[0].cs0_row;
687 /*
688 * RK3188 share the rank and row bit15, we use same ddr config for 15bit
689 * and 16bit row
690 */
691 if (row == 16)
692 row = 15;
Heiko Stübner3e747192017-02-18 19:46:37 +0100693 tmp = sdram_params->ch[0].col - 9;
694 tmp -= (sdram_params->ch[0].bw == 2) ? 0 : 1;
Kever Yanga27290a2017-09-25 16:33:22 +0800695 tmp |= ((row - 13) << 4);
Heiko Stübner3e747192017-02-18 19:46:37 +0100696 size = sizeof(ddrconf_table)/sizeof(ddrconf_table[0]);
697 for (i = 0; i < size; i++)
698 if (tmp == ddrconf_table[i])
699 break;
700 if (i >= size) {
701 printf("niu config not found\n");
702 ret = -EINVAL;
703 } else {
704 debug("niu config %d\n", i);
705 sdram_params->base.ddrconfig = i;
706 }
707
708 return ret;
709}
710
711static int sdram_init(struct dram_info *dram,
712 struct rk3188_sdram_params *sdram_params)
713{
714 int channel;
715 int zqcr;
716 int ret;
717
718 if ((sdram_params->base.dramtype == DDR3 &&
719 sdram_params->base.ddr_freq > 800000000)) {
720 printf("SDRAM frequency is too high!");
721 return -E2BIG;
722 }
723
724 ret = clk_set_rate(&dram->ddr_clk, sdram_params->base.ddr_freq);
725 if (ret) {
726 printf("Could not set DDR clock\n");
727 return ret;
728 }
729
730 for (channel = 0; channel < 1; channel++) {
731 const struct chan_info *chan = &dram->chan[channel];
732 struct rk3288_ddr_pctl *pctl = chan->pctl;
733 struct rk3288_ddr_publ *publ = chan->publ;
734
735 phy_pctrl_reset(dram->cru, publ, channel);
736 phy_dll_bypass_set(publ, sdram_params->base.ddr_freq);
737
738 dfi_cfg(pctl, sdram_params->base.dramtype);
739
740 pctl_cfg(channel, pctl, sdram_params, dram->grf);
741
742 phy_cfg(chan, channel, sdram_params);
743
744 phy_init(publ);
745
746 writel(POWER_UP_START, &pctl->powctl);
747 while (!(readl(&pctl->powstat) & POWER_UP_DONE))
748 ;
749
750 memory_init(publ, sdram_params->base.dramtype);
751 move_to_config_state(publ, pctl);
752
753 /* Using 32bit bus width for detect */
754 sdram_params->ch[channel].bw = 2;
755 set_bandwidth_ratio(chan, channel,
756 sdram_params->ch[channel].bw, dram->grf);
757 /*
758 * set cs, using n=3 for detect
759 * CS0, n=1
760 * CS1, n=2
761 * CS0 & CS1, n = 3
762 */
763 sdram_params->ch[channel].rank = 2,
764 clrsetbits_le32(&publ->pgcr, 0xF << 18,
765 (sdram_params->ch[channel].rank | 1) << 18);
766
767 /* DS=40ohm,ODT=155ohm */
768 zqcr = 1 << ZDEN_SHIFT | 2 << PU_ONDIE_SHIFT |
769 2 << PD_ONDIE_SHIFT | 0x19 << PU_OUTPUT_SHIFT |
770 0x19 << PD_OUTPUT_SHIFT;
771 writel(zqcr, &publ->zq1cr[0]);
772 writel(zqcr, &publ->zq0cr[0]);
773
774 /* Detect the rank and bit-width with data-training */
775 writel(1, &chan->msch->ddrconf);
776 sdram_rank_bw_detect(dram, channel, sdram_params);
777
778 if (sdram_params->base.dramtype == LPDDR3) {
779 u32 i;
780 writel(0, &pctl->mrrcfg0);
781 for (i = 0; i < 17; i++)
782 send_command_op(pctl, 1, MRR_CMD, i, 0);
783 }
784 writel(4, &chan->msch->ddrconf);
785 move_to_access_state(chan);
786 /* DDR3 and LPDDR3 are always 8 bank, no need detect */
787 sdram_params->ch[channel].bk = 3;
788 /* Detect Col and Row number*/
789 ret = sdram_col_row_detect(dram, channel, sdram_params);
790 if (ret)
791 goto error;
792 }
793 /* Find NIU DDR configuration */
794 ret = sdram_get_niu_config(sdram_params);
795 if (ret)
796 goto error;
797
798 dram_all_config(dram, sdram_params);
799 debug("%s done\n", __func__);
800
801 return 0;
802error:
803 printf("DRAM init failed!\n");
804 hang();
805}
Heiko Stübner3e747192017-02-18 19:46:37 +0100806
Heiko Stübner3e747192017-02-18 19:46:37 +0100807static int setup_sdram(struct udevice *dev)
808{
809 struct dram_info *priv = dev_get_priv(dev);
810 struct rk3188_sdram_params *params = dev_get_platdata(dev);
811
812 return sdram_init(priv, params);
813}
814
815static int rk3188_dmc_ofdata_to_platdata(struct udevice *dev)
816{
817#if !CONFIG_IS_ENABLED(OF_PLATDATA)
818 struct rk3188_sdram_params *params = dev_get_platdata(dev);
Heiko Stübner3e747192017-02-18 19:46:37 +0100819 int ret;
820
821 /* rk3188 supports only one-channel */
822 params->num_channels = 1;
Philipp Tomsich8f1034e2017-06-07 18:46:03 +0200823 ret = dev_read_u32_array(dev, "rockchip,pctl-timing",
824 (u32 *)&params->pctl_timing,
825 sizeof(params->pctl_timing) / sizeof(u32));
Heiko Stübner3e747192017-02-18 19:46:37 +0100826 if (ret) {
827 printf("%s: Cannot read rockchip,pctl-timing\n", __func__);
828 return -EINVAL;
829 }
Philipp Tomsich8f1034e2017-06-07 18:46:03 +0200830 ret = dev_read_u32_array(dev, "rockchip,phy-timing",
831 (u32 *)&params->phy_timing,
832 sizeof(params->phy_timing) / sizeof(u32));
Heiko Stübner3e747192017-02-18 19:46:37 +0100833 if (ret) {
834 printf("%s: Cannot read rockchip,phy-timing\n", __func__);
835 return -EINVAL;
836 }
Philipp Tomsich8f1034e2017-06-07 18:46:03 +0200837 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
838 (u32 *)&params->base,
839 sizeof(params->base) / sizeof(u32));
Heiko Stübner3e747192017-02-18 19:46:37 +0100840 if (ret) {
841 printf("%s: Cannot read rockchip,sdram-params\n", __func__);
842 return -EINVAL;
843 }
Masahiro Yamadad3581232018-04-19 12:14:03 +0900844 ret = regmap_init_mem(dev_ofnode(dev), &params->map);
Heiko Stübner3e747192017-02-18 19:46:37 +0100845 if (ret)
846 return ret;
847#endif
848
849 return 0;
850}
851#endif /* CONFIG_SPL_BUILD */
852
853#if CONFIG_IS_ENABLED(OF_PLATDATA)
854static int conv_of_platdata(struct udevice *dev)
855{
856 struct rk3188_sdram_params *plat = dev_get_platdata(dev);
857 struct dtd_rockchip_rk3188_dmc *of_plat = &plat->of_plat;
858 int ret;
859
860 memcpy(&plat->pctl_timing, of_plat->rockchip_pctl_timing,
861 sizeof(plat->pctl_timing));
862 memcpy(&plat->phy_timing, of_plat->rockchip_phy_timing,
863 sizeof(plat->phy_timing));
864 memcpy(&plat->base, of_plat->rockchip_sdram_params, sizeof(plat->base));
865 /* rk3188 supports dual-channel, set default channel num to 2 */
866 plat->num_channels = 1;
867 ret = regmap_init_mem_platdata(dev, of_plat->reg,
868 ARRAY_SIZE(of_plat->reg) / 2,
869 &plat->map);
870 if (ret)
871 return ret;
872
873 return 0;
874}
875#endif
876
877static int rk3188_dmc_probe(struct udevice *dev)
878{
879#ifdef CONFIG_SPL_BUILD
880 struct rk3188_sdram_params *plat = dev_get_platdata(dev);
Kever Yang7805cdf2017-06-23 16:11:06 +0800881 struct regmap *map;
882 struct udevice *dev_clk;
883 int ret;
Heiko Stübner3e747192017-02-18 19:46:37 +0100884#endif
885 struct dram_info *priv = dev_get_priv(dev);
Heiko Stübner3e747192017-02-18 19:46:37 +0100886
Kever Yang7805cdf2017-06-23 16:11:06 +0800887 priv->pmu = syscon_get_first_range(ROCKCHIP_SYSCON_PMU);
888
889#ifdef CONFIG_SPL_BUILD
Heiko Stübner3e747192017-02-18 19:46:37 +0100890#if CONFIG_IS_ENABLED(OF_PLATDATA)
891 ret = conv_of_platdata(dev);
892 if (ret)
893 return ret;
894#endif
895 map = syscon_get_regmap_by_driver_data(ROCKCHIP_SYSCON_NOC);
896 if (IS_ERR(map))
897 return PTR_ERR(map);
898 priv->chan[0].msch = regmap_get_range(map, 0);
899
900 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Heiko Stübner3e747192017-02-18 19:46:37 +0100901
Heiko Stübner3e747192017-02-18 19:46:37 +0100902 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
903 priv->chan[0].publ = regmap_get_range(plat->map, 1);
Heiko Stübner3e747192017-02-18 19:46:37 +0100904
905 ret = rockchip_get_clk(&dev_clk);
906 if (ret)
907 return ret;
908 priv->ddr_clk.id = CLK_DDR;
909 ret = clk_request(dev_clk, &priv->ddr_clk);
910 if (ret)
911 return ret;
912
913 priv->cru = rockchip_get_cru();
914 if (IS_ERR(priv->cru))
915 return PTR_ERR(priv->cru);
Heiko Stübner3e747192017-02-18 19:46:37 +0100916 ret = setup_sdram(dev);
917 if (ret)
918 return ret;
Kever Yang7805cdf2017-06-23 16:11:06 +0800919#else
Heiko Stübner34085092017-03-20 12:40:29 +0100920 priv->info.base = CONFIG_SYS_SDRAM_BASE;
Kever Yang7805cdf2017-06-23 16:11:06 +0800921 priv->info.size = rockchip_sdram_size(
922 (phys_addr_t)&priv->pmu->sys_reg[2]);
923#endif
Heiko Stübner3e747192017-02-18 19:46:37 +0100924
925 return 0;
926}
927
928static int rk3188_dmc_get_info(struct udevice *dev, struct ram_info *info)
929{
930 struct dram_info *priv = dev_get_priv(dev);
931
932 *info = priv->info;
933
934 return 0;
935}
936
937static struct ram_ops rk3188_dmc_ops = {
938 .get_info = rk3188_dmc_get_info,
939};
940
941static const struct udevice_id rk3188_dmc_ids[] = {
942 { .compatible = "rockchip,rk3188-dmc" },
943 { }
944};
945
946U_BOOT_DRIVER(dmc_rk3188) = {
947 .name = "rockchip_rk3188_dmc",
948 .id = UCLASS_RAM,
949 .of_match = rk3188_dmc_ids,
950 .ops = &rk3188_dmc_ops,
951#ifdef CONFIG_SPL_BUILD
952 .ofdata_to_platdata = rk3188_dmc_ofdata_to_platdata,
953#endif
954 .probe = rk3188_dmc_probe,
955 .priv_auto_alloc_size = sizeof(struct dram_info),
956#ifdef CONFIG_SPL_BUILD
957 .platdata_auto_alloc_size = sizeof(struct rk3188_sdram_params),
958#endif
959};