1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
6 * Adapted from the very similar rk3288 ddr init.
12 #include <dt-structs.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/cru_rk3188.h>
20 #include <asm/arch/ddr_rk3188.h>
21 #include <asm/arch/grf_rk3188.h>
22 #include <asm/arch/pmu_rk3188.h>
23 #include <asm/arch/sdram.h>
24 #include <asm/arch/sdram_common.h>
25 #include <linux/err.h>
28 struct rk3288_ddr_pctl
*pctl
;
29 struct rk3288_ddr_publ
*publ
;
30 struct rk3188_msch
*msch
;
34 struct chan_info chan
[1];
37 struct rk3188_cru
*cru
;
38 struct rk3188_grf
*grf
;
39 struct rk3188_sgrf
*sgrf
;
40 struct rk3188_pmu
*pmu
;
43 struct rk3188_sdram_params
{
44 #if CONFIG_IS_ENABLED(OF_PLATDATA)
45 struct dtd_rockchip_rk3188_dmc of_plat
;
47 struct rk3288_sdram_channel ch
[2];
48 struct rk3288_sdram_pctl_timing pctl_timing
;
49 struct rk3288_sdram_phy_timing phy_timing
;
50 struct rk3288_base_params base
;
55 const int ddrconf_table
[] = {
58 * [1:0] col(9+n), assume bw=2
62 ((2 << DDRCONF_ROW_SHIFT
) | 1 << DDRCONF_COL_SHIFT
),
63 ((1 << DDRCONF_ROW_SHIFT
) | 1 << DDRCONF_COL_SHIFT
),
64 ((0 << DDRCONF_ROW_SHIFT
) | 1 << DDRCONF_COL_SHIFT
),
65 ((2 << DDRCONF_ROW_SHIFT
) | 2 << DDRCONF_COL_SHIFT
),
66 ((1 << DDRCONF_ROW_SHIFT
) | 2 << DDRCONF_COL_SHIFT
),
67 ((0 << DDRCONF_ROW_SHIFT
) | 2 << DDRCONF_COL_SHIFT
),
68 ((1 << DDRCONF_ROW_SHIFT
) | 0 << DDRCONF_COL_SHIFT
),
69 ((0 << DDRCONF_ROW_SHIFT
) | 0 << DDRCONF_COL_SHIFT
),
79 #define TEST_PATTEN 0x5aa5f00f
80 #define DQS_GATE_TRAINING_ERROR_RANK0 (1 << 4)
81 #define DQS_GATE_TRAINING_ERROR_RANK1 (2 << 4)
83 #ifdef CONFIG_SPL_BUILD
84 static void copy_to_reg(u32
*dest
, const u32
*src
, u32 n
)
88 for (i
= 0; i
< n
/ sizeof(u32
); i
++) {
95 static void ddr_reset(struct rk3188_cru
*cru
, u32 ch
, u32 ctl
, u32 phy
)
97 u32 phy_ctl_srstn_shift
= 13;
98 u32 ctl_psrstn_shift
= 11;
99 u32 ctl_srstn_shift
= 10;
100 u32 phy_psrstn_shift
= 9;
101 u32 phy_srstn_shift
= 8;
103 rk_clrsetreg(&cru
->cru_softrst_con
[5],
104 1 << phy_ctl_srstn_shift
| 1 << ctl_psrstn_shift
|
105 1 << ctl_srstn_shift
| 1 << phy_psrstn_shift
|
106 1 << phy_srstn_shift
,
107 phy
<< phy_ctl_srstn_shift
| ctl
<< ctl_psrstn_shift
|
108 ctl
<< ctl_srstn_shift
| phy
<< phy_psrstn_shift
|
109 phy
<< phy_srstn_shift
);
112 static void ddr_phy_ctl_reset(struct rk3188_cru
*cru
, u32 ch
, u32 n
)
114 u32 phy_ctl_srstn_shift
= 13;
116 rk_clrsetreg(&cru
->cru_softrst_con
[5],
117 1 << phy_ctl_srstn_shift
, n
<< phy_ctl_srstn_shift
);
120 static void phy_pctrl_reset(struct rk3188_cru
*cru
,
121 struct rk3288_ddr_publ
*publ
,
126 ddr_reset(cru
, channel
, 1, 1);
128 clrbits_le32(&publ
->acdllcr
, ACDLLCR_DLLSRST
);
129 for (i
= 0; i
< 4; i
++)
130 clrbits_le32(&publ
->datx8
[i
].dxdllcr
, DXDLLCR_DLLSRST
);
133 setbits_le32(&publ
->acdllcr
, ACDLLCR_DLLSRST
);
134 for (i
= 0; i
< 4; i
++)
135 setbits_le32(&publ
->datx8
[i
].dxdllcr
, DXDLLCR_DLLSRST
);
138 ddr_reset(cru
, channel
, 1, 0);
140 ddr_reset(cru
, channel
, 0, 0);
144 static void phy_dll_bypass_set(struct rk3288_ddr_publ
*publ
,
149 if (freq
<= 250000000) {
150 if (freq
<= 150000000)
151 clrbits_le32(&publ
->dllgcr
, SBIAS_BYPASS
);
153 setbits_le32(&publ
->dllgcr
, SBIAS_BYPASS
);
154 setbits_le32(&publ
->acdllcr
, ACDLLCR_DLLDIS
);
155 for (i
= 0; i
< 4; i
++)
156 setbits_le32(&publ
->datx8
[i
].dxdllcr
,
159 setbits_le32(&publ
->pir
, PIR_DLLBYP
);
161 clrbits_le32(&publ
->dllgcr
, SBIAS_BYPASS
);
162 clrbits_le32(&publ
->acdllcr
, ACDLLCR_DLLDIS
);
163 for (i
= 0; i
< 4; i
++) {
164 clrbits_le32(&publ
->datx8
[i
].dxdllcr
,
168 clrbits_le32(&publ
->pir
, PIR_DLLBYP
);
172 static void dfi_cfg(struct rk3288_ddr_pctl
*pctl
, u32 dramtype
)
174 writel(DFI_INIT_START
, &pctl
->dfistcfg0
);
175 writel(DFI_DRAM_CLK_SR_EN
| DFI_DRAM_CLK_DPD_EN
,
177 writel(DFI_PARITY_INTR_EN
| DFI_PARITY_EN
, &pctl
->dfistcfg2
);
178 writel(7 << TLP_RESP_TIME_SHIFT
| LP_SR_EN
| LP_PD_EN
,
181 writel(2 << TCTRL_DELAY_TIME_SHIFT
, &pctl
->dfitctrldelay
);
182 writel(1 << TPHY_WRDATA_TIME_SHIFT
, &pctl
->dfitphywrdata
);
183 writel(0xf << TPHY_RDLAT_TIME_SHIFT
, &pctl
->dfitphyrdlat
);
184 writel(2 << TDRAM_CLK_DIS_TIME_SHIFT
, &pctl
->dfitdramclkdis
);
185 writel(2 << TDRAM_CLK_EN_TIME_SHIFT
, &pctl
->dfitdramclken
);
186 writel(1, &pctl
->dfitphyupdtype0
);
188 /* cs0 and cs1 write odt enable */
189 writel((RANK0_ODT_WRITE_SEL
| RANK1_ODT_WRITE_SEL
),
191 /* odt write length */
192 writel(7 << ODT_LEN_BL8_W_SHIFT
, &pctl
->dfiodtcfg1
);
193 /* phyupd and ctrlupd disabled */
194 writel(0, &pctl
->dfiupdcfg
);
197 static void ddr_set_enable(struct rk3188_grf
*grf
, uint channel
, bool enable
)
202 val
= 1 << DDR_16BIT_EN_SHIFT
;
204 rk_clrsetreg(&grf
->ddrc_con0
, 1 << DDR_16BIT_EN_SHIFT
, val
);
207 static void ddr_set_ddr3_mode(struct rk3188_grf
*grf
, uint channel
,
212 mask
= MSCH4_MAINDDR3_MASK
<< MSCH4_MAINDDR3_SHIFT
;
213 val
= ddr3_mode
<< MSCH4_MAINDDR3_SHIFT
;
214 rk_clrsetreg(&grf
->soc_con2
, mask
, val
);
217 static void ddr_rank_2_row15en(struct rk3188_grf
*grf
, bool enable
)
221 mask
= RANK_TO_ROW15_EN_MASK
<< RANK_TO_ROW15_EN_SHIFT
;
222 val
= enable
<< RANK_TO_ROW15_EN_SHIFT
;
223 rk_clrsetreg(&grf
->soc_con2
, mask
, val
);
226 static void pctl_cfg(int channel
, struct rk3288_ddr_pctl
*pctl
,
227 struct rk3188_sdram_params
*sdram_params
,
228 struct rk3188_grf
*grf
)
230 copy_to_reg(&pctl
->togcnt1u
, &sdram_params
->pctl_timing
.togcnt1u
,
231 sizeof(sdram_params
->pctl_timing
));
232 switch (sdram_params
->base
.dramtype
) {
234 if (sdram_params
->phy_timing
.mr
[1] & DDR3_DLL_DISABLE
) {
235 writel(sdram_params
->pctl_timing
.tcl
- 3,
236 &pctl
->dfitrddataen
);
238 writel(sdram_params
->pctl_timing
.tcl
- 2,
239 &pctl
->dfitrddataen
);
241 writel(sdram_params
->pctl_timing
.tcwl
- 1,
242 &pctl
->dfitphywrlat
);
243 writel(0 << MDDR_LPDDR2_CLK_STOP_IDLE_SHIFT
| DDR3_EN
|
244 DDR2_DDR3_BL_8
| (6 - 4) << TFAW_SHIFT
| PD_EXIT_SLOW
|
245 1 << PD_TYPE_SHIFT
| 0 << PD_IDLE_SHIFT
,
247 ddr_set_ddr3_mode(grf
, channel
, true);
248 ddr_set_enable(grf
, channel
, true);
252 setbits_le32(&pctl
->scfg
, 1);
255 static void phy_cfg(const struct chan_info
*chan
, int channel
,
256 struct rk3188_sdram_params
*sdram_params
)
258 struct rk3288_ddr_publ
*publ
= chan
->publ
;
259 struct rk3188_msch
*msch
= chan
->msch
;
260 uint ddr_freq_mhz
= sdram_params
->base
.ddr_freq
/ 1000000;
264 dinit2
= DIV_ROUND_UP(ddr_freq_mhz
* 200000, 1000);
266 copy_to_reg(&publ
->dtpr
[0], &sdram_params
->phy_timing
.dtpr0
,
267 sizeof(sdram_params
->phy_timing
));
268 writel(sdram_params
->base
.noc_timing
, &msch
->ddrtiming
);
269 writel(0x3f, &msch
->readlatency
);
270 writel(DIV_ROUND_UP(ddr_freq_mhz
* 5120, 1000) << PRT_DLLLOCK_SHIFT
|
271 DIV_ROUND_UP(ddr_freq_mhz
* 50, 1000) << PRT_DLLSRST_SHIFT
|
272 8 << PRT_ITMSRST_SHIFT
, &publ
->ptr
[0]);
273 writel(DIV_ROUND_UP(ddr_freq_mhz
* 500000, 1000) << PRT_DINIT0_SHIFT
|
274 DIV_ROUND_UP(ddr_freq_mhz
* 400, 1000) << PRT_DINIT1_SHIFT
,
276 writel(min(dinit2
, 0x1ffffU
) << PRT_DINIT2_SHIFT
|
277 DIV_ROUND_UP(ddr_freq_mhz
* 1000, 1000) << PRT_DINIT3_SHIFT
,
280 switch (sdram_params
->base
.dramtype
) {
282 clrbits_le32(&publ
->pgcr
, 0x1f);
283 clrsetbits_le32(&publ
->dcr
, DDRMD_MASK
<< DDRMD_SHIFT
,
284 DDRMD_DDR3
<< DDRMD_SHIFT
);
287 if (sdram_params
->base
.odt
) {
288 /*dynamic RTT enable */
289 for (i
= 0; i
< 4; i
++)
290 setbits_le32(&publ
->datx8
[i
].dxgcr
, DQSRTT
| DQRTT
);
292 /*dynamic RTT disable */
293 for (i
= 0; i
< 4; i
++)
294 clrbits_le32(&publ
->datx8
[i
].dxgcr
, DQSRTT
| DQRTT
);
298 static void phy_init(struct rk3288_ddr_publ
*publ
)
300 setbits_le32(&publ
->pir
, PIR_INIT
| PIR_DLLSRST
301 | PIR_DLLLOCK
| PIR_ZCAL
| PIR_ITMSRST
| PIR_CLRSR
);
303 while ((readl(&publ
->pgsr
) &
304 (PGSR_IDONE
| PGSR_DLDONE
| PGSR_ZCDONE
)) !=
305 (PGSR_IDONE
| PGSR_DLDONE
| PGSR_ZCDONE
))
309 static void send_command(struct rk3288_ddr_pctl
*pctl
, u32 rank
,
312 writel((START_CMD
| (rank
<< 20) | arg
| cmd
), &pctl
->mcmd
);
314 while (readl(&pctl
->mcmd
) & START_CMD
)
318 static inline void send_command_op(struct rk3288_ddr_pctl
*pctl
,
319 u32 rank
, u32 cmd
, u32 ma
, u32 op
)
321 send_command(pctl
, rank
, cmd
, (ma
& LPDDR2_MA_MASK
) << LPDDR2_MA_SHIFT
|
322 (op
& LPDDR2_OP_MASK
) << LPDDR2_OP_SHIFT
);
325 static void memory_init(struct rk3288_ddr_publ
*publ
,
328 setbits_le32(&publ
->pir
,
329 (PIR_INIT
| PIR_DRAMINIT
| PIR_LOCKBYP
330 | PIR_ZCALBYP
| PIR_CLRSR
| PIR_ICPC
331 | (dramtype
== DDR3
? PIR_DRAMRST
: 0)));
333 while ((readl(&publ
->pgsr
) & (PGSR_IDONE
| PGSR_DLDONE
))
334 != (PGSR_IDONE
| PGSR_DLDONE
))
338 static void move_to_config_state(struct rk3288_ddr_publ
*publ
,
339 struct rk3288_ddr_pctl
*pctl
)
344 state
= readl(&pctl
->stat
) & PCTL_STAT_MSK
;
348 writel(WAKEUP_STATE
, &pctl
->sctl
);
349 while ((readl(&pctl
->stat
) & PCTL_STAT_MSK
)
353 while ((readl(&publ
->pgsr
) & PGSR_DLDONE
)
357 * if at low power state,need wakeup first,
358 * and then enter the config, so
364 writel(CFG_STATE
, &pctl
->sctl
);
365 while ((readl(&pctl
->stat
) & PCTL_STAT_MSK
) != CONFIG
)
376 static void set_bandwidth_ratio(const struct chan_info
*chan
, int channel
,
377 u32 n
, struct rk3188_grf
*grf
)
379 struct rk3288_ddr_pctl
*pctl
= chan
->pctl
;
380 struct rk3288_ddr_publ
*publ
= chan
->publ
;
381 struct rk3188_msch
*msch
= chan
->msch
;
384 setbits_le32(&pctl
->ppcfg
, 1);
385 ddr_set_enable(grf
, channel
, 1);
386 setbits_le32(&msch
->ddrtiming
, 1 << 31);
387 /* Data Byte disable*/
388 clrbits_le32(&publ
->datx8
[2].dxgcr
, 1);
389 clrbits_le32(&publ
->datx8
[3].dxgcr
, 1);
391 setbits_le32(&publ
->datx8
[2].dxdllcr
, DXDLLCR_DLLDIS
);
392 setbits_le32(&publ
->datx8
[3].dxdllcr
, DXDLLCR_DLLDIS
);
394 clrbits_le32(&pctl
->ppcfg
, 1);
395 ddr_set_enable(grf
, channel
, 0);
396 clrbits_le32(&msch
->ddrtiming
, 1 << 31);
397 /* Data Byte enable*/
398 setbits_le32(&publ
->datx8
[2].dxgcr
, 1);
399 setbits_le32(&publ
->datx8
[3].dxgcr
, 1);
402 clrbits_le32(&publ
->datx8
[2].dxdllcr
, DXDLLCR_DLLDIS
);
403 clrbits_le32(&publ
->datx8
[3].dxdllcr
, DXDLLCR_DLLDIS
);
405 clrbits_le32(&publ
->datx8
[2].dxdllcr
, DXDLLCR_DLLSRST
);
406 clrbits_le32(&publ
->datx8
[3].dxdllcr
, DXDLLCR_DLLSRST
);
408 setbits_le32(&publ
->datx8
[2].dxdllcr
, DXDLLCR_DLLSRST
);
409 setbits_le32(&publ
->datx8
[3].dxdllcr
, DXDLLCR_DLLSRST
);
411 setbits_le32(&pctl
->dfistcfg0
, 1 << 2);
414 static int data_training(const struct chan_info
*chan
, int channel
,
415 struct rk3188_sdram_params
*sdram_params
)
421 u32 step
[2] = { PIR_QSTRN
, PIR_RVTRN
};
422 struct rk3288_ddr_publ
*publ
= chan
->publ
;
423 struct rk3288_ddr_pctl
*pctl
= chan
->pctl
;
425 /* disable auto refresh */
426 writel(0, &pctl
->trefi
);
428 if (sdram_params
->base
.dramtype
!= LPDDR3
)
429 setbits_le32(&publ
->pgcr
, 1 << PGCR_DQSCFG_SHIFT
);
430 rank
= sdram_params
->ch
[channel
].rank
| 1;
431 for (j
= 0; j
< ARRAY_SIZE(step
); j
++) {
433 * trigger QSTRN and RVTRN
434 * clear DTDONE status
436 setbits_le32(&publ
->pir
, PIR_CLRSR
);
439 setbits_le32(&publ
->pir
,
440 PIR_INIT
| step
[j
] | PIR_LOCKBYP
| PIR_ZCALBYP
|
443 /* wait echo byte DTDONE */
444 while ((readl(&publ
->datx8
[0].dxgsr
[0]) & rank
)
447 while ((readl(&publ
->datx8
[1].dxgsr
[0]) & rank
)
450 if (!(readl(&pctl
->ppcfg
) & 1)) {
451 while ((readl(&publ
->datx8
[2].dxgsr
[0])
454 while ((readl(&publ
->datx8
[3].dxgsr
[0])
458 if (readl(&publ
->pgsr
) &
459 (PGSR_DTERR
| PGSR_RVERR
| PGSR_RVEIRR
)) {
464 /* send some auto refresh to complement the lost while DTT */
465 for (i
= 0; i
< (rank
> 1 ? 8 : 4); i
++)
466 send_command(pctl
, rank
, REF_CMD
, 0);
468 if (sdram_params
->base
.dramtype
!= LPDDR3
)
469 clrbits_le32(&publ
->pgcr
, 1 << PGCR_DQSCFG_SHIFT
);
471 /* resume auto refresh */
472 writel(sdram_params
->pctl_timing
.trefi
, &pctl
->trefi
);
477 static void move_to_access_state(const struct chan_info
*chan
)
479 struct rk3288_ddr_publ
*publ
= chan
->publ
;
480 struct rk3288_ddr_pctl
*pctl
= chan
->pctl
;
484 state
= readl(&pctl
->stat
) & PCTL_STAT_MSK
;
488 if (((readl(&pctl
->stat
) >> LP_TRIG_SHIFT
) &
492 writel(WAKEUP_STATE
, &pctl
->sctl
);
493 while ((readl(&pctl
->stat
) & PCTL_STAT_MSK
) != ACCESS
)
496 while ((readl(&publ
->pgsr
) & PGSR_DLDONE
)
501 writel(CFG_STATE
, &pctl
->sctl
);
502 while ((readl(&pctl
->stat
) & PCTL_STAT_MSK
) != CONFIG
)
506 writel(GO_STATE
, &pctl
->sctl
);
507 while ((readl(&pctl
->stat
) & PCTL_STAT_MSK
) == CONFIG
)
518 static void dram_cfg_rbc(const struct chan_info
*chan
, u32 chnum
,
519 struct rk3188_sdram_params
*sdram_params
)
521 struct rk3288_ddr_publ
*publ
= chan
->publ
;
523 if (sdram_params
->ch
[chnum
].bk
== 3)
524 clrsetbits_le32(&publ
->dcr
, PDQ_MASK
<< PDQ_SHIFT
,
527 clrbits_le32(&publ
->dcr
, PDQ_MASK
<< PDQ_SHIFT
);
529 writel(sdram_params
->base
.ddrconfig
, &chan
->msch
->ddrconf
);
532 static void dram_all_config(const struct dram_info
*dram
,
533 struct rk3188_sdram_params
*sdram_params
)
538 sys_reg
|= sdram_params
->base
.dramtype
<< SYS_REG_DDRTYPE_SHIFT
;
539 sys_reg
|= (sdram_params
->num_channels
- 1) << SYS_REG_NUM_CH_SHIFT
;
540 for (chan
= 0; chan
< sdram_params
->num_channels
; chan
++) {
541 const struct rk3288_sdram_channel
*info
=
542 &sdram_params
->ch
[chan
];
544 sys_reg
|= info
->row_3_4
<< SYS_REG_ROW_3_4_SHIFT(chan
);
545 sys_reg
|= 1 << SYS_REG_CHINFO_SHIFT(chan
);
546 sys_reg
|= (info
->rank
- 1) << SYS_REG_RANK_SHIFT(chan
);
547 sys_reg
|= (info
->col
- 9) << SYS_REG_COL_SHIFT(chan
);
548 sys_reg
|= info
->bk
== 3 ? 0 : 1 << SYS_REG_BK_SHIFT(chan
);
549 sys_reg
|= (info
->cs0_row
- 13) << SYS_REG_CS0_ROW_SHIFT(chan
);
550 sys_reg
|= (info
->cs1_row
- 13) << SYS_REG_CS1_ROW_SHIFT(chan
);
551 sys_reg
|= (2 >> info
->bw
) << SYS_REG_BW_SHIFT(chan
);
552 sys_reg
|= (2 >> info
->dbw
) << SYS_REG_DBW_SHIFT(chan
);
554 dram_cfg_rbc(&dram
->chan
[chan
], chan
, sdram_params
);
556 if (sdram_params
->ch
[0].rank
== 2)
557 ddr_rank_2_row15en(dram
->grf
, 0);
559 ddr_rank_2_row15en(dram
->grf
, 1);
561 writel(sys_reg
, &dram
->pmu
->sys_reg
[2]);
564 static int sdram_rank_bw_detect(struct dram_info
*dram
, int channel
,
565 struct rk3188_sdram_params
*sdram_params
)
568 int need_trainig
= 0;
569 const struct chan_info
*chan
= &dram
->chan
[channel
];
570 struct rk3288_ddr_publ
*publ
= chan
->publ
;
572 ddr_rank_2_row15en(dram
->grf
, 0);
574 if (data_training(chan
, channel
, sdram_params
) < 0) {
575 printf("first data training fail!\n");
576 reg
= readl(&publ
->datx8
[0].dxgsr
[0]);
577 /* Check the result for rank 0 */
578 if ((channel
== 0) && (reg
& DQS_GATE_TRAINING_ERROR_RANK0
)) {
579 printf("data training fail!\n");
583 /* Check the result for rank 1 */
584 if (reg
& DQS_GATE_TRAINING_ERROR_RANK1
) {
585 sdram_params
->ch
[channel
].rank
= 1;
586 clrsetbits_le32(&publ
->pgcr
, 0xF << 18,
587 sdram_params
->ch
[channel
].rank
<< 18);
590 reg
= readl(&publ
->datx8
[2].dxgsr
[0]);
591 if (reg
& (1 << 4)) {
592 sdram_params
->ch
[channel
].bw
= 1;
593 set_bandwidth_ratio(chan
, channel
,
594 sdram_params
->ch
[channel
].bw
,
599 /* Assume the Die bit width are the same with the chip bit width */
600 sdram_params
->ch
[channel
].dbw
= sdram_params
->ch
[channel
].bw
;
603 (data_training(chan
, channel
, sdram_params
) < 0)) {
604 if (sdram_params
->base
.dramtype
== LPDDR3
) {
605 ddr_phy_ctl_reset(dram
->cru
, channel
, 1);
607 ddr_phy_ctl_reset(dram
->cru
, channel
, 0);
610 printf("2nd data training failed!");
618 * Detect ram columns and rows.
619 * @dram: dram info struct
620 * @channel: channel number to handle
621 * @sdram_params: sdram parameters, function will fill in col and row values
623 * Returns 0 or negative on error.
625 static int sdram_col_row_detect(struct dram_info
*dram
, int channel
,
626 struct rk3188_sdram_params
*sdram_params
)
630 const struct chan_info
*chan
= &dram
->chan
[channel
];
631 struct rk3288_ddr_pctl
*pctl
= chan
->pctl
;
632 struct rk3288_ddr_publ
*publ
= chan
->publ
;
636 for (col
= 11; col
>= 9; col
--) {
637 writel(0, CONFIG_SYS_SDRAM_BASE
);
638 addr
= CONFIG_SYS_SDRAM_BASE
+
639 (1 << (col
+ sdram_params
->ch
[channel
].bw
- 1));
640 writel(TEST_PATTEN
, addr
);
641 if ((readl(addr
) == TEST_PATTEN
) &&
642 (readl(CONFIG_SYS_SDRAM_BASE
) == 0))
646 printf("Col detect error\n");
650 sdram_params
->ch
[channel
].col
= col
;
653 ddr_rank_2_row15en(dram
->grf
, 1);
654 move_to_config_state(publ
, pctl
);
655 writel(1, &chan
->msch
->ddrconf
);
656 move_to_access_state(chan
);
657 /* Detect row, max 15,min13 in rk3188*/
658 for (row
= 16; row
>= 13; row
--) {
659 writel(0, CONFIG_SYS_SDRAM_BASE
);
660 addr
= CONFIG_SYS_SDRAM_BASE
+ (1 << (row
+ 15 - 1));
661 writel(TEST_PATTEN
, addr
);
662 if ((readl(addr
) == TEST_PATTEN
) &&
663 (readl(CONFIG_SYS_SDRAM_BASE
) == 0))
667 printf("Row detect error\n");
670 sdram_params
->ch
[channel
].cs1_row
= row
;
671 sdram_params
->ch
[channel
].row_3_4
= 0;
672 debug("chn %d col %d, row %d\n", channel
, col
, row
);
673 sdram_params
->ch
[channel
].cs0_row
= row
;
680 static int sdram_get_niu_config(struct rk3188_sdram_params
*sdram_params
)
682 int i
, tmp
, size
, row
, ret
= 0;
684 row
= sdram_params
->ch
[0].cs0_row
;
686 * RK3188 share the rank and row bit15, we use same ddr config for 15bit
691 tmp
= sdram_params
->ch
[0].col
- 9;
692 tmp
-= (sdram_params
->ch
[0].bw
== 2) ? 0 : 1;
693 tmp
|= ((row
- 13) << 4);
694 size
= sizeof(ddrconf_table
)/sizeof(ddrconf_table
[0]);
695 for (i
= 0; i
< size
; i
++)
696 if (tmp
== ddrconf_table
[i
])
699 printf("niu config not found\n");
702 debug("niu config %d\n", i
);
703 sdram_params
->base
.ddrconfig
= i
;
709 static int sdram_init(struct dram_info
*dram
,
710 struct rk3188_sdram_params
*sdram_params
)
716 if ((sdram_params
->base
.dramtype
== DDR3
&&
717 sdram_params
->base
.ddr_freq
> 800000000)) {
718 printf("SDRAM frequency is too high!");
722 ret
= clk_set_rate(&dram
->ddr_clk
, sdram_params
->base
.ddr_freq
);
724 printf("Could not set DDR clock\n");
728 for (channel
= 0; channel
< 1; channel
++) {
729 const struct chan_info
*chan
= &dram
->chan
[channel
];
730 struct rk3288_ddr_pctl
*pctl
= chan
->pctl
;
731 struct rk3288_ddr_publ
*publ
= chan
->publ
;
733 phy_pctrl_reset(dram
->cru
, publ
, channel
);
734 phy_dll_bypass_set(publ
, sdram_params
->base
.ddr_freq
);
736 dfi_cfg(pctl
, sdram_params
->base
.dramtype
);
738 pctl_cfg(channel
, pctl
, sdram_params
, dram
->grf
);
740 phy_cfg(chan
, channel
, sdram_params
);
744 writel(POWER_UP_START
, &pctl
->powctl
);
745 while (!(readl(&pctl
->powstat
) & POWER_UP_DONE
))
748 memory_init(publ
, sdram_params
->base
.dramtype
);
749 move_to_config_state(publ
, pctl
);
751 /* Using 32bit bus width for detect */
752 sdram_params
->ch
[channel
].bw
= 2;
753 set_bandwidth_ratio(chan
, channel
,
754 sdram_params
->ch
[channel
].bw
, dram
->grf
);
756 * set cs, using n=3 for detect
761 sdram_params
->ch
[channel
].rank
= 2,
762 clrsetbits_le32(&publ
->pgcr
, 0xF << 18,
763 (sdram_params
->ch
[channel
].rank
| 1) << 18);
765 /* DS=40ohm,ODT=155ohm */
766 zqcr
= 1 << ZDEN_SHIFT
| 2 << PU_ONDIE_SHIFT
|
767 2 << PD_ONDIE_SHIFT
| 0x19 << PU_OUTPUT_SHIFT
|
768 0x19 << PD_OUTPUT_SHIFT
;
769 writel(zqcr
, &publ
->zq1cr
[0]);
770 writel(zqcr
, &publ
->zq0cr
[0]);
772 /* Detect the rank and bit-width with data-training */
773 writel(1, &chan
->msch
->ddrconf
);
774 sdram_rank_bw_detect(dram
, channel
, sdram_params
);
776 if (sdram_params
->base
.dramtype
== LPDDR3
) {
778 writel(0, &pctl
->mrrcfg0
);
779 for (i
= 0; i
< 17; i
++)
780 send_command_op(pctl
, 1, MRR_CMD
, i
, 0);
782 writel(4, &chan
->msch
->ddrconf
);
783 move_to_access_state(chan
);
784 /* DDR3 and LPDDR3 are always 8 bank, no need detect */
785 sdram_params
->ch
[channel
].bk
= 3;
786 /* Detect Col and Row number*/
787 ret
= sdram_col_row_detect(dram
, channel
, sdram_params
);
791 /* Find NIU DDR configuration */
792 ret
= sdram_get_niu_config(sdram_params
);
796 dram_all_config(dram
, sdram_params
);
797 debug("%s done\n", __func__
);
801 printf("DRAM init failed!\n");
805 static int setup_sdram(struct udevice
*dev
)
807 struct dram_info
*priv
= dev_get_priv(dev
);
808 struct rk3188_sdram_params
*params
= dev_get_platdata(dev
);
810 return sdram_init(priv
, params
);
813 static int rk3188_dmc_ofdata_to_platdata(struct udevice
*dev
)
815 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
816 struct rk3188_sdram_params
*params
= dev_get_platdata(dev
);
819 /* rk3188 supports only one-channel */
820 params
->num_channels
= 1;
821 ret
= dev_read_u32_array(dev
, "rockchip,pctl-timing",
822 (u32
*)¶ms
->pctl_timing
,
823 sizeof(params
->pctl_timing
) / sizeof(u32
));
825 printf("%s: Cannot read rockchip,pctl-timing\n", __func__
);
828 ret
= dev_read_u32_array(dev
, "rockchip,phy-timing",
829 (u32
*)¶ms
->phy_timing
,
830 sizeof(params
->phy_timing
) / sizeof(u32
));
832 printf("%s: Cannot read rockchip,phy-timing\n", __func__
);
835 ret
= dev_read_u32_array(dev
, "rockchip,sdram-params",
836 (u32
*)¶ms
->base
,
837 sizeof(params
->base
) / sizeof(u32
));
839 printf("%s: Cannot read rockchip,sdram-params\n", __func__
);
842 ret
= regmap_init_mem(dev
, ¶ms
->map
);
849 #endif /* CONFIG_SPL_BUILD */
851 #if CONFIG_IS_ENABLED(OF_PLATDATA)
852 static int conv_of_platdata(struct udevice
*dev
)
854 struct rk3188_sdram_params
*plat
= dev_get_platdata(dev
);
855 struct dtd_rockchip_rk3188_dmc
*of_plat
= &plat
->of_plat
;
858 memcpy(&plat
->pctl_timing
, of_plat
->rockchip_pctl_timing
,
859 sizeof(plat
->pctl_timing
));
860 memcpy(&plat
->phy_timing
, of_plat
->rockchip_phy_timing
,
861 sizeof(plat
->phy_timing
));
862 memcpy(&plat
->base
, of_plat
->rockchip_sdram_params
, sizeof(plat
->base
));
863 /* rk3188 supports dual-channel, set default channel num to 2 */
864 plat
->num_channels
= 1;
865 ret
= regmap_init_mem_platdata(dev
, of_plat
->reg
,
866 ARRAY_SIZE(of_plat
->reg
) / 2,
875 static int rk3188_dmc_probe(struct udevice
*dev
)
877 #ifdef CONFIG_SPL_BUILD
878 struct rk3188_sdram_params
*plat
= dev_get_platdata(dev
);
880 struct udevice
*dev_clk
;
883 struct dram_info
*priv
= dev_get_priv(dev
);
885 priv
->pmu
= syscon_get_first_range(ROCKCHIP_SYSCON_PMU
);
887 #ifdef CONFIG_SPL_BUILD
888 #if CONFIG_IS_ENABLED(OF_PLATDATA)
889 ret
= conv_of_platdata(dev
);
893 map
= syscon_get_regmap_by_driver_data(ROCKCHIP_SYSCON_NOC
);
896 priv
->chan
[0].msch
= regmap_get_range(map
, 0);
898 priv
->grf
= syscon_get_first_range(ROCKCHIP_SYSCON_GRF
);
900 priv
->chan
[0].pctl
= regmap_get_range(plat
->map
, 0);
901 priv
->chan
[0].publ
= regmap_get_range(plat
->map
, 1);
903 ret
= rockchip_get_clk(&dev_clk
);
906 priv
->ddr_clk
.id
= CLK_DDR
;
907 ret
= clk_request(dev_clk
, &priv
->ddr_clk
);
911 priv
->cru
= rockchip_get_cru();
912 if (IS_ERR(priv
->cru
))
913 return PTR_ERR(priv
->cru
);
914 ret
= setup_sdram(dev
);
918 priv
->info
.base
= CONFIG_SYS_SDRAM_BASE
;
919 priv
->info
.size
= rockchip_sdram_size(
920 (phys_addr_t
)&priv
->pmu
->sys_reg
[2]);
926 static int rk3188_dmc_get_info(struct udevice
*dev
, struct ram_info
*info
)
928 struct dram_info
*priv
= dev_get_priv(dev
);
935 static struct ram_ops rk3188_dmc_ops
= {
936 .get_info
= rk3188_dmc_get_info
,
939 static const struct udevice_id rk3188_dmc_ids
[] = {
940 { .compatible
= "rockchip,rk3188-dmc" },
944 U_BOOT_DRIVER(dmc_rk3188
) = {
945 .name
= "rockchip_rk3188_dmc",
947 .of_match
= rk3188_dmc_ids
,
948 .ops
= &rk3188_dmc_ops
,
949 #ifdef CONFIG_SPL_BUILD
950 .ofdata_to_platdata
= rk3188_dmc_ofdata_to_platdata
,
952 .probe
= rk3188_dmc_probe
,
953 .priv_auto_alloc_size
= sizeof(struct dram_info
),
954 #ifdef CONFIG_SPL_BUILD
955 .platdata_auto_alloc_size
= sizeof(struct rk3188_sdram_params
),