blob: 184c93f7763d15cb4a13ff4036d8c481067140c2 [file] [log] [blame]
Kever Yang6fc9ebf2018-12-20 11:33:42 +08001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Kever Yang13856742017-06-23 16:11:07 +08002/*
3 * (C) Copyright 2017 Rockchip Electronics Co., Ltd.
Kever Yang13856742017-06-23 16:11:07 +08004 */
Kever Yang13856742017-06-23 16:11:07 +08005#include <common.h>
Kever Yang9228f282019-08-02 10:39:59 +03006#include <clk.h>
7#include <debug_uart.h>
Kever Yang13856742017-06-23 16:11:07 +08008#include <dm.h>
Kever Yang9228f282019-08-02 10:39:59 +03009#include <dt-structs.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Kever Yang13856742017-06-23 16:11:07 +080012#include <ram.h>
Kever Yang9228f282019-08-02 10:39:59 +030013#include <regmap.h>
Kever Yang13856742017-06-23 16:11:07 +080014#include <syscon.h>
Kever Yang9228f282019-08-02 10:39:59 +030015#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/clock.h>
Kever Yang9228f282019-08-02 10:39:59 +030017#include <asm/arch-rockchip/cru_rk3328.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/grf_rk3328.h>
Kever Yange47db832019-11-15 11:04:33 +080019#include <asm/arch-rockchip/sdram.h>
Kever Yang9228f282019-08-02 10:39:59 +030020#include <asm/arch-rockchip/sdram_rk3328.h>
21#include <asm/arch-rockchip/uart.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Kever Yang13856742017-06-23 16:11:07 +080023
Kever Yang13856742017-06-23 16:11:07 +080024struct dram_info {
Kever Yang9228f282019-08-02 10:39:59 +030025#ifdef CONFIG_TPL_BUILD
YouMin Chenb9f7df32019-11-15 11:04:44 +080026 struct ddr_pctl_regs *pctl;
27 struct ddr_phy_regs *phy;
Kever Yang9228f282019-08-02 10:39:59 +030028 struct clk ddr_clk;
29 struct rk3328_cru *cru;
YouMin Chenb9f7df32019-11-15 11:04:44 +080030 struct msch_regs *msch;
Kever Yang9228f282019-08-02 10:39:59 +030031 struct rk3328_ddr_grf_regs *ddr_grf;
32#endif
Kever Yang13856742017-06-23 16:11:07 +080033 struct ram_info info;
34 struct rk3328_grf_regs *grf;
35};
36
Kever Yang9228f282019-08-02 10:39:59 +030037#ifdef CONFIG_TPL_BUILD
38
39struct rk3328_sdram_channel sdram_ch;
40
41struct rockchip_dmc_plat {
42#if CONFIG_IS_ENABLED(OF_PLATDATA)
43 struct dtd_rockchip_rk3328_dmc dtplat;
44#else
45 struct rk3328_sdram_params sdram_params;
46#endif
47 struct regmap *map;
48};
49
50#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -070051static int conv_of_plat(struct udevice *dev)
Kever Yang9228f282019-08-02 10:39:59 +030052{
Simon Glassfa20e932020-12-03 16:55:20 -070053 struct rockchip_dmc_plat *plat = dev_get_plat(dev);
Kever Yang9228f282019-08-02 10:39:59 +030054 struct dtd_rockchip_rk3328_dmc *dtplat = &plat->dtplat;
55 int ret;
56
Simon Glassb75b15b2020-12-03 16:55:23 -070057 ret = regmap_init_mem_plat(dev, dtplat->reg,
58 ARRAY_SIZE(dtplat->reg) / 2, &plat->map);
Kever Yang9228f282019-08-02 10:39:59 +030059 if (ret)
60 return ret;
61
62 return 0;
63}
64#endif
65
66static void rkclk_ddr_reset(struct dram_info *dram,
67 u32 ctl_srstn, u32 ctl_psrstn,
68 u32 phy_srstn, u32 phy_psrstn)
69{
70 writel(ddrctrl_srstn_req(ctl_srstn) | ddrctrl_psrstn_req(ctl_psrstn) |
71 ddrphy_srstn_req(phy_srstn) | ddrphy_psrstn_req(phy_psrstn),
72 &dram->cru->softrst_con[5]);
73 writel(ddrctrl_asrstn_req(ctl_srstn), &dram->cru->softrst_con[9]);
74}
75
YouMin Chenb9f7df32019-11-15 11:04:44 +080076static void rkclk_set_dpll(struct dram_info *dram, unsigned int hz)
Kever Yang9228f282019-08-02 10:39:59 +030077{
78 unsigned int refdiv, postdiv1, postdiv2, fbdiv;
79 int delay = 1000;
YouMin Chenb9f7df32019-11-15 11:04:44 +080080 u32 mhz = hz / MHZ;
Kever Yang9228f282019-08-02 10:39:59 +030081
82 refdiv = 1;
83 if (mhz <= 300) {
84 postdiv1 = 4;
85 postdiv2 = 2;
86 } else if (mhz <= 400) {
87 postdiv1 = 6;
88 postdiv2 = 1;
89 } else if (mhz <= 600) {
90 postdiv1 = 4;
91 postdiv2 = 1;
92 } else if (mhz <= 800) {
93 postdiv1 = 3;
94 postdiv2 = 1;
95 } else if (mhz <= 1600) {
96 postdiv1 = 2;
97 postdiv2 = 1;
98 } else {
99 postdiv1 = 1;
100 postdiv2 = 1;
101 }
102 fbdiv = (mhz * refdiv * postdiv1 * postdiv2) / 24;
103
104 writel(((0x1 << 4) << 16) | (0 << 4), &dram->cru->mode_con);
105 writel(POSTDIV1(postdiv1) | FBDIV(fbdiv), &dram->cru->dpll_con[0]);
106 writel(DSMPD(1) | POSTDIV2(postdiv2) | REFDIV(refdiv),
107 &dram->cru->dpll_con[1]);
108
109 while (delay > 0) {
110 udelay(1);
111 if (LOCK(readl(&dram->cru->dpll_con[1])))
112 break;
113 delay--;
114 }
115
116 writel(((0x1 << 4) << 16) | (1 << 4), &dram->cru->mode_con);
117}
118
119static void rkclk_configure_ddr(struct dram_info *dram,
120 struct rk3328_sdram_params *sdram_params)
121{
122 void __iomem *phy_base = dram->phy;
123
124 /* choose DPLL for ddr clk source */
125 clrbits_le32(PHY_REG(phy_base, 0xef), 1 << 7);
126
127 /* for inno ddr phy need 2*freq */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800128 rkclk_set_dpll(dram, sdram_params->base.ddr_freq * MHZ * 2);
Kever Yang9228f282019-08-02 10:39:59 +0300129}
130
Kever Yang9228f282019-08-02 10:39:59 +0300131/* return ddrconfig value
132 * (-1), find ddrconfig fail
133 * other, the ddrconfig value
134 * only support cs0_row >= cs1_row
135 */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800136static u32 calculate_ddrconfig(struct rk3328_sdram_params *sdram_params)
Kever Yang9228f282019-08-02 10:39:59 +0300137{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800138 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
Kever Yang9228f282019-08-02 10:39:59 +0300139 u32 cs, bw, die_bw, col, row, bank;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800140 u32 cs1_row;
Kever Yang9228f282019-08-02 10:39:59 +0300141 u32 i, tmp;
142 u32 ddrconf = -1;
143
YouMin Chenb9f7df32019-11-15 11:04:44 +0800144 cs = cap_info->rank;
145 bw = cap_info->bw;
146 die_bw = cap_info->dbw;
147 col = cap_info->col;
148 row = cap_info->cs0_row;
149 cs1_row = cap_info->cs1_row;
150 bank = cap_info->bk;
151
152 if (sdram_params->base.dramtype == DDR4) {
153 /* when DDR_TEST, CS always at MSB position for easy test */
154 if (cs == 2 && row == cs1_row) {
155 /* include 2cs cap both 2^n or both (2^n - 2^(n-2)) */
156 tmp = ((row - 13) << 3) | (1 << 2) | (bw & 0x2) |
157 die_bw;
158 for (i = 17; i < 21; i++) {
159 if (((tmp & 0x7) ==
160 (ddr4_cfg_2_rbc[i - 10] & 0x7)) &&
161 ((tmp & 0x3c) <=
162 (ddr4_cfg_2_rbc[i - 10] & 0x3c))) {
163 ddrconf = i;
164 goto out;
165 }
166 }
167 }
Kever Yang9228f282019-08-02 10:39:59 +0300168
Kever Yang9228f282019-08-02 10:39:59 +0300169 tmp = ((cs - 1) << 6) | ((row - 13) << 3) | (bw & 0x2) | die_bw;
170 for (i = 10; i < 17; i++) {
171 if (((tmp & 0x7) == (ddr4_cfg_2_rbc[i - 10] & 0x7)) &&
172 ((tmp & 0x3c) <= (ddr4_cfg_2_rbc[i - 10] & 0x3c)) &&
173 ((tmp & 0x40) <= (ddr4_cfg_2_rbc[i - 10] & 0x40))) {
174 ddrconf = i;
175 goto out;
176 }
177 }
178 } else {
179 if (bank == 2) {
180 ddrconf = 8;
181 goto out;
182 }
183
YouMin Chenb9f7df32019-11-15 11:04:44 +0800184 /* when DDR_TEST, CS always at MSB position for easy test */
185 if (cs == 2 && row == cs1_row) {
186 /* include 2cs cap both 2^n or both (2^n - 2^(n-2)) */
187 for (i = 5; i < 8; i++) {
188 if ((bw + col - 11) == (ddr_cfg_2_rbc[i] &
189 0x3)) {
190 ddrconf = i;
191 goto out;
192 }
193 }
194 }
195
Kever Yang9228f282019-08-02 10:39:59 +0300196 tmp = ((row - 13) << 4) | (1 << 2) | ((bw + col - 11) << 0);
197 for (i = 0; i < 5; i++)
198 if (((tmp & 0xf) == (ddr_cfg_2_rbc[i] & 0xf)) &&
199 ((tmp & 0x30) <= (ddr_cfg_2_rbc[i] & 0x30))) {
200 ddrconf = i;
201 goto out;
202 }
203 }
204
205out:
206 if (ddrconf > 20)
YouMin Chenb9f7df32019-11-15 11:04:44 +0800207 printf("calculate ddrconfig error\n");
Kever Yang9228f282019-08-02 10:39:59 +0300208
209 return ddrconf;
210}
211
Kever Yang9228f282019-08-02 10:39:59 +0300212/*******
213 * calculate controller dram address map, and setting to register.
214 * argument sdram_ch.ddrconf must be right value before
215 * call this function.
216 *******/
217static void set_ctl_address_map(struct dram_info *dram,
218 struct rk3328_sdram_params *sdram_params)
219{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800220 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
Kever Yang9228f282019-08-02 10:39:59 +0300221 void __iomem *pctl_base = dram->pctl;
222
YouMin Chenb9f7df32019-11-15 11:04:44 +0800223 sdram_copy_to_reg((u32 *)(pctl_base + DDR_PCTL2_ADDRMAP0),
224 &addrmap[cap_info->ddrconfig][0], 9 * 4);
225 if (sdram_params->base.dramtype == LPDDR3 && cap_info->row_3_4)
Kever Yang9228f282019-08-02 10:39:59 +0300226 setbits_le32(pctl_base + DDR_PCTL2_ADDRMAP6, 1 << 31);
YouMin Chenb9f7df32019-11-15 11:04:44 +0800227 if (sdram_params->base.dramtype == DDR4 && cap_info->bw == 0x1)
Kever Yang9228f282019-08-02 10:39:59 +0300228 setbits_le32(pctl_base + DDR_PCTL2_PCCFG, 1 << 8);
229
YouMin Chenb9f7df32019-11-15 11:04:44 +0800230 if (cap_info->rank == 1)
Kever Yang9228f282019-08-02 10:39:59 +0300231 clrsetbits_le32(pctl_base + DDR_PCTL2_ADDRMAP0, 0x1f, 0x1f);
232}
233
Kever Yang9228f282019-08-02 10:39:59 +0300234static int data_training(struct dram_info *dram, u32 cs, u32 dramtype)
235{
Kever Yang9228f282019-08-02 10:39:59 +0300236 void __iomem *pctl_base = dram->pctl;
Kever Yang9228f282019-08-02 10:39:59 +0300237 u32 dis_auto_zq = 0;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800238 u32 pwrctl;
239 u32 ret;
Kever Yang9228f282019-08-02 10:39:59 +0300240
YouMin Chenb9f7df32019-11-15 11:04:44 +0800241 /* disable auto low-power */
242 pwrctl = readl(pctl_base + DDR_PCTL2_PWRCTL);
243 writel(0, pctl_base + DDR_PCTL2_PWRCTL);
Kever Yang9228f282019-08-02 10:39:59 +0300244
YouMin Chenb9f7df32019-11-15 11:04:44 +0800245 dis_auto_zq = pctl_dis_zqcs_aref(dram->pctl);
Kever Yang9228f282019-08-02 10:39:59 +0300246
YouMin Chenb9f7df32019-11-15 11:04:44 +0800247 ret = phy_data_training(dram->phy, cs, dramtype);
Kever Yang9228f282019-08-02 10:39:59 +0300248
YouMin Chenb9f7df32019-11-15 11:04:44 +0800249 pctl_rest_zqcs_aref(dram->pctl, dis_auto_zq);
Kever Yang9228f282019-08-02 10:39:59 +0300250
YouMin Chenb9f7df32019-11-15 11:04:44 +0800251 /* restore auto low-power */
252 writel(pwrctl, pctl_base + DDR_PCTL2_PWRCTL);
Kever Yang9228f282019-08-02 10:39:59 +0300253
YouMin Chenb9f7df32019-11-15 11:04:44 +0800254 return ret;
Kever Yang9228f282019-08-02 10:39:59 +0300255}
256
Kever Yang9228f282019-08-02 10:39:59 +0300257static void rx_deskew_switch_adjust(struct dram_info *dram)
258{
259 u32 i, deskew_val;
260 u32 gate_val = 0;
261 void __iomem *phy_base = dram->phy;
262
263 for (i = 0; i < 4; i++)
YouMin Chenb9f7df32019-11-15 11:04:44 +0800264 gate_val = MAX(readl(PHY_REG(phy_base, 0xfb + i)), gate_val);
Kever Yang9228f282019-08-02 10:39:59 +0300265
266 deskew_val = (gate_val >> 3) + 1;
267 deskew_val = (deskew_val > 0x1f) ? 0x1f : deskew_val;
268 clrsetbits_le32(PHY_REG(phy_base, 0x6e), 0xc, (deskew_val & 0x3) << 2);
269 clrsetbits_le32(PHY_REG(phy_base, 0x6f), 0x7 << 4,
270 (deskew_val & 0x1c) << 2);
271}
272
Kever Yang9228f282019-08-02 10:39:59 +0300273static void tx_deskew_switch_adjust(struct dram_info *dram)
274{
275 void __iomem *phy_base = dram->phy;
276
277 clrsetbits_le32(PHY_REG(phy_base, 0x6e), 0x3, 1);
278}
279
280static void set_ddrconfig(struct dram_info *dram, u32 ddrconfig)
281{
282 writel(ddrconfig, &dram->msch->ddrconf);
283}
284
YouMin Chenb9f7df32019-11-15 11:04:44 +0800285static void sdram_msch_config(struct msch_regs *msch,
286 struct sdram_msch_timings *noc_timings)
Kever Yang9228f282019-08-02 10:39:59 +0300287{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800288 writel(noc_timings->ddrtiming.d32, &msch->ddrtiming);
Kever Yang9228f282019-08-02 10:39:59 +0300289
YouMin Chenb9f7df32019-11-15 11:04:44 +0800290 writel(noc_timings->ddrmode.d32, &msch->ddrmode);
291 writel(noc_timings->readlatency, &msch->readlatency);
Kever Yang9228f282019-08-02 10:39:59 +0300292
YouMin Chenb9f7df32019-11-15 11:04:44 +0800293 writel(noc_timings->activate.d32, &msch->activate);
294 writel(noc_timings->devtodev.d32, &msch->devtodev);
295 writel(noc_timings->ddr4timing.d32, &msch->ddr4_timing);
296 writel(noc_timings->agingx0, &msch->aging0);
297 writel(noc_timings->agingx0, &msch->aging1);
298 writel(noc_timings->agingx0, &msch->aging2);
299 writel(noc_timings->agingx0, &msch->aging3);
300 writel(noc_timings->agingx0, &msch->aging4);
301 writel(noc_timings->agingx0, &msch->aging5);
302}
Kever Yang9228f282019-08-02 10:39:59 +0300303
YouMin Chenb9f7df32019-11-15 11:04:44 +0800304static void dram_all_config(struct dram_info *dram,
305 struct rk3328_sdram_params *sdram_params)
306{
307 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
308 u32 sys_reg2 = 0;
309 u32 sys_reg3 = 0;
Kever Yang9228f282019-08-02 10:39:59 +0300310
YouMin Chenb9f7df32019-11-15 11:04:44 +0800311 set_ddrconfig(dram, cap_info->ddrconfig);
312 sdram_org_config(cap_info, &sdram_params->base, &sys_reg2,
313 &sys_reg3, 0);
314 writel(sys_reg2, &dram->grf->os_reg[2]);
315 writel(sys_reg3, &dram->grf->os_reg[3]);
Kever Yang9228f282019-08-02 10:39:59 +0300316
YouMin Chenb9f7df32019-11-15 11:04:44 +0800317 sdram_msch_config(dram->msch, &sdram_ch.noc_timings);
Kever Yang9228f282019-08-02 10:39:59 +0300318}
319
320static void enable_low_power(struct dram_info *dram,
321 struct rk3328_sdram_params *sdram_params)
322{
323 void __iomem *pctl_base = dram->pctl;
324
325 /* enable upctl2 axi clock auto gating */
326 writel(0x00800000, &dram->ddr_grf->ddr_grf_con[0]);
327 writel(0x20012001, &dram->ddr_grf->ddr_grf_con[2]);
328 /* enable upctl2 core clock auto gating */
329 writel(0x001e001a, &dram->ddr_grf->ddr_grf_con[2]);
330 /* enable sr, pd */
331 if (PD_IDLE == 0)
332 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
333 else
334 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 1));
335 if (SR_IDLE == 0)
336 clrbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
337 else
338 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, 1);
339 setbits_le32(pctl_base + DDR_PCTL2_PWRCTL, (1 << 3));
340}
341
342static int sdram_init(struct dram_info *dram,
343 struct rk3328_sdram_params *sdram_params, u32 pre_init)
344{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800345 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
Kever Yang9228f282019-08-02 10:39:59 +0300346 void __iomem *pctl_base = dram->pctl;
347
348 rkclk_ddr_reset(dram, 1, 1, 1, 1);
349 udelay(10);
350 /*
351 * dereset ddr phy psrstn to config pll,
352 * if using phy pll psrstn must be dereset
353 * before config pll
354 */
355 rkclk_ddr_reset(dram, 1, 1, 1, 0);
356 rkclk_configure_ddr(dram, sdram_params);
YouMin Chenb9f7df32019-11-15 11:04:44 +0800357
Kever Yang9228f282019-08-02 10:39:59 +0300358 /* release phy srst to provide clk to ctrl */
359 rkclk_ddr_reset(dram, 1, 1, 0, 0);
360 udelay(10);
YouMin Chenb9f7df32019-11-15 11:04:44 +0800361 phy_soft_reset(dram->phy);
Kever Yang9228f282019-08-02 10:39:59 +0300362 /* release ctrl presetn, and config ctl registers */
363 rkclk_ddr_reset(dram, 1, 0, 0, 0);
YouMin Chenb9f7df32019-11-15 11:04:44 +0800364 pctl_cfg(dram->pctl, &sdram_params->pctl_regs, SR_IDLE, PD_IDLE);
365 cap_info->ddrconfig = calculate_ddrconfig(sdram_params);
Kever Yang9228f282019-08-02 10:39:59 +0300366 set_ctl_address_map(dram, sdram_params);
YouMin Chenb9f7df32019-11-15 11:04:44 +0800367 phy_cfg(dram->phy, &sdram_params->phy_regs, &sdram_params->skew,
368 &sdram_params->base, cap_info->bw);
Kever Yang9228f282019-08-02 10:39:59 +0300369
370 /* enable dfi_init_start to init phy after ctl srstn deassert */
371 setbits_le32(pctl_base + DDR_PCTL2_DFIMISC, (1 << 5) | (1 << 4));
372 rkclk_ddr_reset(dram, 0, 0, 0, 0);
373 /* wait for dfi_init_done and dram init complete */
374 while ((readl(pctl_base + DDR_PCTL2_STAT) & 0x7) == 0)
375 continue;
376
377 /* do ddr gate training */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800378 if (data_training(dram, 0, sdram_params->base.dramtype) != 0) {
379 printf("data training error\n");
380 return -1;
381 }
Kever Yang9228f282019-08-02 10:39:59 +0300382
YouMin Chenb9f7df32019-11-15 11:04:44 +0800383 if (sdram_params->base.dramtype == DDR4)
384 pctl_write_vrefdq(dram->pctl, 0x3, 5670,
385 sdram_params->base.dramtype);
Kever Yang9228f282019-08-02 10:39:59 +0300386
Kever Yang95b1e392020-01-07 15:15:20 +0800387 if (pre_init != 0) {
Kever Yang9228f282019-08-02 10:39:59 +0300388 rx_deskew_switch_adjust(dram);
389 tx_deskew_switch_adjust(dram);
390 }
391
392 dram_all_config(dram, sdram_params);
393 enable_low_power(dram, sdram_params);
394
395 return 0;
396}
397
398static u64 dram_detect_cap(struct dram_info *dram,
399 struct rk3328_sdram_params *sdram_params,
400 unsigned char channel)
401{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800402 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
Kever Yang9228f282019-08-02 10:39:59 +0300403
404 /*
405 * for ddr3: ddrconf = 3
406 * for ddr4: ddrconf = 12
407 * for lpddr3: ddrconf = 3
408 * default bw = 1
409 */
410 u32 bk, bktmp;
411 u32 col, coltmp;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800412 u32 rowtmp;
Kever Yang9228f282019-08-02 10:39:59 +0300413 u32 cs;
414 u32 bw = 1;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800415 u32 dram_type = sdram_params->base.dramtype;
Kever Yang9228f282019-08-02 10:39:59 +0300416
417 if (dram_type != DDR4) {
418 /* detect col and bk for ddr3/lpddr3 */
419 coltmp = 12;
420 bktmp = 3;
421 rowtmp = 16;
422
YouMin Chenb9f7df32019-11-15 11:04:44 +0800423 if (sdram_detect_col(cap_info, coltmp) != 0)
Kever Yang9228f282019-08-02 10:39:59 +0300424 goto cap_err;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800425 sdram_detect_bank(cap_info, coltmp, bktmp);
426 sdram_detect_dbw(cap_info, dram_type);
Kever Yang9228f282019-08-02 10:39:59 +0300427 } else {
428 /* detect bg for ddr4 */
429 coltmp = 10;
430 bktmp = 4;
431 rowtmp = 17;
432
433 col = 10;
434 bk = 2;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800435 cap_info->col = col;
436 cap_info->bk = bk;
437 sdram_detect_bg(cap_info, coltmp);
Kever Yang9228f282019-08-02 10:39:59 +0300438 }
YouMin Chenb9f7df32019-11-15 11:04:44 +0800439
Kever Yang9228f282019-08-02 10:39:59 +0300440 /* detect row */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800441 if (sdram_detect_row(cap_info, coltmp, bktmp, rowtmp) != 0)
Kever Yang9228f282019-08-02 10:39:59 +0300442 goto cap_err;
Kever Yang9228f282019-08-02 10:39:59 +0300443
YouMin Chenb9f7df32019-11-15 11:04:44 +0800444 /* detect row_3_4 */
445 sdram_detect_row_3_4(cap_info, coltmp, bktmp);
Kever Yang9228f282019-08-02 10:39:59 +0300446
YouMin Chenb9f7df32019-11-15 11:04:44 +0800447 /* bw and cs detect using data training */
Kever Yang9228f282019-08-02 10:39:59 +0300448 if (data_training(dram, 1, dram_type) == 0)
449 cs = 1;
450 else
451 cs = 0;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800452 cap_info->rank = cs + 1;
Kever Yang9228f282019-08-02 10:39:59 +0300453
454 bw = 2;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800455 cap_info->bw = bw;
Kever Yang9228f282019-08-02 10:39:59 +0300456
YouMin Chenb9f7df32019-11-15 11:04:44 +0800457 cap_info->cs0_high16bit_row = cap_info->cs0_row;
458 if (cs) {
459 cap_info->cs1_row = cap_info->cs0_row;
460 cap_info->cs1_high16bit_row = cap_info->cs0_row;
461 } else {
462 cap_info->cs1_row = 0;
463 cap_info->cs1_high16bit_row = 0;
Kever Yang9228f282019-08-02 10:39:59 +0300464 }
465
Kever Yang9228f282019-08-02 10:39:59 +0300466 return 0;
YouMin Chenb9f7df32019-11-15 11:04:44 +0800467cap_err:
468 return -1;
Kever Yang9228f282019-08-02 10:39:59 +0300469}
470
471static int sdram_init_detect(struct dram_info *dram,
472 struct rk3328_sdram_params *sdram_params)
473{
YouMin Chenb9f7df32019-11-15 11:04:44 +0800474 u32 sys_reg = 0;
475 u32 sys_reg3 = 0;
476 struct sdram_cap_info *cap_info = &sdram_params->ch.cap_info;
477
Kever Yang9228f282019-08-02 10:39:59 +0300478 debug("Starting SDRAM initialization...\n");
479
480 memcpy(&sdram_ch, &sdram_params->ch,
481 sizeof(struct rk3328_sdram_channel));
482
Kever Yang95b1e392020-01-07 15:15:20 +0800483 sdram_init(dram, sdram_params, 0);
Kever Yang9228f282019-08-02 10:39:59 +0300484 dram_detect_cap(dram, sdram_params, 0);
485
486 /* modify bw, cs related timing */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800487 pctl_remodify_sdram_params(&sdram_params->pctl_regs, cap_info,
488 sdram_params->base.dramtype);
489
490 if (cap_info->bw == 2)
491 sdram_ch.noc_timings.ddrtiming.b.bwratio = 0;
492 else
493 sdram_ch.noc_timings.ddrtiming.b.bwratio = 1;
494
Kever Yang9228f282019-08-02 10:39:59 +0300495 /* reinit sdram by real dram cap */
Kever Yang95b1e392020-01-07 15:15:20 +0800496 sdram_init(dram, sdram_params, 1);
Kever Yang9228f282019-08-02 10:39:59 +0300497
498 /* redetect cs1 row */
YouMin Chenb9f7df32019-11-15 11:04:44 +0800499 sdram_detect_cs1_row(cap_info, sdram_params->base.dramtype);
500 if (cap_info->cs1_row) {
501 sys_reg = readl(&dram->grf->os_reg[2]);
502 sys_reg3 = readl(&dram->grf->os_reg[3]);
503 SYS_REG_ENC_CS1_ROW(cap_info->cs1_row,
504 sys_reg, sys_reg3, 0);
505 writel(sys_reg, &dram->grf->os_reg[2]);
506 writel(sys_reg3, &dram->grf->os_reg[3]);
507 }
508
Jagan Teki95bf5872022-12-14 23:20:49 +0530509 sdram_print_ddr_info(&sdram_params->ch.cap_info, &sdram_params->base, 0);
Kever Yang9228f282019-08-02 10:39:59 +0300510
511 return 0;
512}
513
514static int rk3328_dmc_init(struct udevice *dev)
515{
516 struct dram_info *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700517 struct rockchip_dmc_plat *plat = dev_get_plat(dev);
Kever Yang9228f282019-08-02 10:39:59 +0300518 int ret;
519
Simon Glass92882652021-08-07 07:24:04 -0600520#if CONFIG_IS_ENABLED(OF_REAL)
Kever Yang9228f282019-08-02 10:39:59 +0300521 struct rk3328_sdram_params *params = &plat->sdram_params;
522#else
523 struct dtd_rockchip_rk3328_dmc *dtplat = &plat->dtplat;
524 struct rk3328_sdram_params *params =
525 (void *)dtplat->rockchip_sdram_params;
526
Simon Glassb75b15b2020-12-03 16:55:23 -0700527 ret = conv_of_plat(dev);
Kever Yang9228f282019-08-02 10:39:59 +0300528 if (ret)
529 return ret;
530#endif
531 priv->phy = regmap_get_range(plat->map, 0);
532 priv->pctl = regmap_get_range(plat->map, 1);
533 priv->grf = regmap_get_range(plat->map, 2);
534 priv->cru = regmap_get_range(plat->map, 3);
535 priv->msch = regmap_get_range(plat->map, 4);
536 priv->ddr_grf = regmap_get_range(plat->map, 5);
537
538 debug("%s phy %p pctrl %p grf %p cru %p msch %p ddr_grf %p\n",
539 __func__, priv->phy, priv->pctl, priv->grf, priv->cru,
540 priv->msch, priv->ddr_grf);
541 ret = sdram_init_detect(priv, params);
542 if (ret < 0) {
543 printf("%s DRAM init failed%d\n", __func__, ret);
544 return ret;
545 }
546
547 return 0;
548}
549
Simon Glassaad29ae2020-12-03 16:55:21 -0700550static int rk3328_dmc_of_to_plat(struct udevice *dev)
Kever Yang9228f282019-08-02 10:39:59 +0300551{
Simon Glass92882652021-08-07 07:24:04 -0600552#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassfa20e932020-12-03 16:55:20 -0700553 struct rockchip_dmc_plat *plat = dev_get_plat(dev);
Kever Yang9228f282019-08-02 10:39:59 +0300554 int ret;
555
556 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
557 (u32 *)&plat->sdram_params,
558 sizeof(plat->sdram_params) / sizeof(u32));
559 if (ret) {
560 printf("%s: Cannot read rockchip,sdram-params %d\n",
561 __func__, ret);
562 return ret;
563 }
564 ret = regmap_init_mem(dev, &plat->map);
565 if (ret)
566 printf("%s: regmap failed %d\n", __func__, ret);
567#endif
568 return 0;
569}
570
571#endif
572
Kever Yang13856742017-06-23 16:11:07 +0800573static int rk3328_dmc_probe(struct udevice *dev)
574{
Kever Yang9228f282019-08-02 10:39:59 +0300575#ifdef CONFIG_TPL_BUILD
576 if (rk3328_dmc_init(dev))
577 return 0;
578#else
Kever Yang13856742017-06-23 16:11:07 +0800579 struct dram_info *priv = dev_get_priv(dev);
580
581 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
582 debug("%s: grf=%p\n", __func__, priv->grf);
Tom Rinibb4dd962022-11-16 13:10:37 -0500583 priv->info.base = CFG_SYS_SDRAM_BASE;
Kever Yang13856742017-06-23 16:11:07 +0800584 priv->info.size = rockchip_sdram_size(
585 (phys_addr_t)&priv->grf->os_reg[2]);
Kever Yang9228f282019-08-02 10:39:59 +0300586#endif
Kever Yang13856742017-06-23 16:11:07 +0800587 return 0;
588}
589
590static int rk3328_dmc_get_info(struct udevice *dev, struct ram_info *info)
591{
592 struct dram_info *priv = dev_get_priv(dev);
593
594 *info = priv->info;
595
596 return 0;
597}
598
599static struct ram_ops rk3328_dmc_ops = {
600 .get_info = rk3328_dmc_get_info,
601};
602
Kever Yang13856742017-06-23 16:11:07 +0800603static const struct udevice_id rk3328_dmc_ids[] = {
604 { .compatible = "rockchip,rk3328-dmc" },
605 { }
606};
607
Walter Lozano2901ac62020-06-25 01:10:04 -0300608U_BOOT_DRIVER(rockchip_rk3328_dmc) = {
Kever Yang13856742017-06-23 16:11:07 +0800609 .name = "rockchip_rk3328_dmc",
610 .id = UCLASS_RAM,
611 .of_match = rk3328_dmc_ids,
612 .ops = &rk3328_dmc_ops,
Kever Yang9228f282019-08-02 10:39:59 +0300613#ifdef CONFIG_TPL_BUILD
Simon Glassaad29ae2020-12-03 16:55:21 -0700614 .of_to_plat = rk3328_dmc_of_to_plat,
Kever Yang9228f282019-08-02 10:39:59 +0300615#endif
Kever Yang13856742017-06-23 16:11:07 +0800616 .probe = rk3328_dmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700617 .priv_auto = sizeof(struct dram_info),
Kever Yang9228f282019-08-02 10:39:59 +0300618#ifdef CONFIG_TPL_BUILD
Simon Glass71fa5b42020-12-03 16:55:18 -0700619 .plat_auto = sizeof(struct rockchip_dmc_plat),
Kever Yang9228f282019-08-02 10:39:59 +0300620#endif
Kever Yang13856742017-06-23 16:11:07 +0800621};