blob: 45270e2718484b38e2b2d3fada36629993760196 [file] [log] [blame]
Kever Yang6fc9ebf2018-12-20 11:33:42 +08001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Kever Yang50fb9982017-02-22 16:56:35 +08002/*
3 * (C) Copyright 2016-2017 Rockchip Inc.
4 *
Kever Yang50fb9982017-02-22 16:56:35 +08005 * Adapted from coreboot.
6 */
Philipp Tomsichc69b3092017-05-31 18:16:34 +02007
Tom Rinidec7ea02024-05-20 13:35:03 -06008#include <config.h>
Kever Yang50fb9982017-02-22 16:56:35 +08009#include <clk.h>
10#include <dm.h>
11#include <dt-structs.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Kever Yang50fb9982017-02-22 16:56:35 +080014#include <ram.h>
15#include <regmap.h>
Simon Glass08d4ba42024-08-01 06:47:23 -060016#include <spl.h>
Kever Yang50fb9982017-02-22 16:56:35 +080017#include <syscon.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/clock.h>
Jagan Teki783acfd2020-01-09 14:22:17 +053019#include <asm/arch-rockchip/cru.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080020#include <asm/arch-rockchip/grf_rk3399.h>
Jagan Teki6ea82692019-07-16 17:27:40 +053021#include <asm/arch-rockchip/pmu_rk3399.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080022#include <asm/arch-rockchip/hardware.h>
Kever Yange47db832019-11-15 11:04:33 +080023#include <asm/arch-rockchip/sdram.h>
Jagan Tekiacf8e0f2019-07-15 23:50:57 +053024#include <asm/arch-rockchip/sdram_rk3399.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Kever Yang50fb9982017-02-22 16:56:35 +080026#include <linux/err.h>
Philipp Tomsichc69b3092017-05-31 18:16:34 +020027#include <time.h>
Kever Yang50fb9982017-02-22 16:56:35 +080028
Jagan Tekiacf8e0f2019-07-15 23:50:57 +053029#define PRESET_SGRF_HOLD(n) ((0x1 << (6 + 16)) | ((n) << 6))
30#define PRESET_GPIO0_HOLD(n) ((0x1 << (7 + 16)) | ((n) << 7))
31#define PRESET_GPIO1_HOLD(n) ((0x1 << (8 + 16)) | ((n) << 8))
32
33#define PHY_DRV_ODT_HI_Z 0x0
34#define PHY_DRV_ODT_240 0x1
35#define PHY_DRV_ODT_120 0x8
36#define PHY_DRV_ODT_80 0x9
37#define PHY_DRV_ODT_60 0xc
38#define PHY_DRV_ODT_48 0xd
39#define PHY_DRV_ODT_40 0xe
40#define PHY_DRV_ODT_34_3 0xf
41
Jagan Teki5d152172019-07-16 17:27:15 +053042#define PHY_BOOSTP_EN 0x1
43#define PHY_BOOSTN_EN 0x1
Jagan Tekid8681842019-07-16 17:27:16 +053044#define PHY_SLEWP_EN 0x1
45#define PHY_SLEWN_EN 0x1
Jagan Teki65535a22019-07-16 17:27:17 +053046#define PHY_RX_CM_INPUT 0x1
Jagan Teki0cb31122019-07-16 17:27:24 +053047#define CS0_MR22_VAL 0
48#define CS1_MR22_VAL 3
Jagan Teki5d152172019-07-16 17:27:15 +053049
YouMin Chen79f4d912019-11-15 11:04:53 +080050/* LPDDR3 DRAM DS */
51#define LPDDR3_DS_34 0x1
52#define LPDDR3_DS_40 0x2
53#define LPDDR3_DS_48 0x3
54
Jagan Tekice75cfb2019-07-15 23:58:43 +053055#define CRU_SFTRST_DDR_CTRL(ch, n) ((0x1 << (8 + 16 + (ch) * 4)) | \
56 ((n) << (8 + (ch) * 4)))
57#define CRU_SFTRST_DDR_PHY(ch, n) ((0x1 << (9 + 16 + (ch) * 4)) | \
58 ((n) << (9 + (ch) * 4)))
Kever Yang50fb9982017-02-22 16:56:35 +080059struct chan_info {
60 struct rk3399_ddr_pctl_regs *pctl;
61 struct rk3399_ddr_pi_regs *pi;
62 struct rk3399_ddr_publ_regs *publ;
YouMin Chen23ae72e2019-11-15 11:04:45 +080063 struct msch_regs *msch;
Kever Yang50fb9982017-02-22 16:56:35 +080064};
65
66struct dram_info {
Jagan Tekic9151e22019-07-15 23:58:45 +053067 u32 pwrup_srefresh_exit[2];
Kever Yang50fb9982017-02-22 16:56:35 +080068 struct chan_info chan[2];
69 struct clk ddr_clk;
Jagan Teki783acfd2020-01-09 14:22:17 +053070 struct rockchip_cru *cru;
Jagan Tekic9151e22019-07-15 23:58:45 +053071 struct rk3399_grf_regs *grf;
Jagan Teki6ea82692019-07-16 17:27:40 +053072 struct rk3399_pmu_regs *pmu;
Kever Yang50fb9982017-02-22 16:56:35 +080073 struct rk3399_pmucru *pmucru;
74 struct rk3399_pmusgrf_regs *pmusgrf;
75 struct rk3399_ddr_cic_regs *cic;
Jagan Teki9eb935a2019-07-16 17:27:30 +053076 const struct sdram_rk3399_ops *ops;
Kever Yang50fb9982017-02-22 16:56:35 +080077 struct ram_info info;
78 struct rk3399_pmugrf_regs *pmugrf;
79};
80
Jagan Teki9eb935a2019-07-16 17:27:30 +053081struct sdram_rk3399_ops {
YouMin Chende57fbf2019-11-15 11:04:46 +080082 int (*data_training_first)(struct dram_info *dram, u32 channel, u8 rank,
83 struct rk3399_sdram_params *sdram);
84 int (*set_rate_index)(struct dram_info *dram,
Lee Jones29cbb302022-08-11 08:58:48 +010085 struct rk3399_sdram_params *params, u32 ctl_fn);
YouMin Chen99027372019-11-15 11:04:48 +080086 void (*modify_param)(const struct chan_info *chan,
87 struct rk3399_sdram_params *params);
88 struct rk3399_sdram_params *
89 (*get_phy_index_params)(u32 phy_fn,
90 struct rk3399_sdram_params *params);
Jagan Teki9eb935a2019-07-16 17:27:30 +053091};
92
Kever Yang50fb9982017-02-22 16:56:35 +080093struct rockchip_dmc_plat {
94#if CONFIG_IS_ENABLED(OF_PLATDATA)
95 struct dtd_rockchip_rk3399_dmc dtplat;
96#else
97 struct rk3399_sdram_params sdram_params;
98#endif
99 struct regmap *map;
100};
101
Jagan Tekie3619d12019-07-16 17:27:21 +0530102struct io_setting {
103 u32 mhz;
104 u32 mr5;
105 /* dram side */
106 u32 dq_odt;
107 u32 ca_odt;
108 u32 pdds;
109 u32 dq_vref;
110 u32 ca_vref;
111 /* phy side */
112 u32 rd_odt;
113 u32 wr_dq_drv;
114 u32 wr_ca_drv;
115 u32 wr_ckcs_drv;
116 u32 rd_odt_en;
117 u32 rd_vref;
118} lpddr4_io_setting[] = {
119 {
120 50 * MHz,
121 0,
122 /* dram side */
123 0, /* dq_odt; */
124 0, /* ca_odt; */
125 6, /* pdds; */
126 0x72, /* dq_vref; */
127 0x72, /* ca_vref; */
128 /* phy side */
129 PHY_DRV_ODT_HI_Z, /* rd_odt; */
130 PHY_DRV_ODT_40, /* wr_dq_drv; */
131 PHY_DRV_ODT_40, /* wr_ca_drv; */
132 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
133 0, /* rd_odt_en;*/
134 41, /* rd_vref; (unit %, range 3.3% - 48.7%) */
135 },
136 {
137 600 * MHz,
138 0,
139 /* dram side */
140 1, /* dq_odt; */
141 0, /* ca_odt; */
142 6, /* pdds; */
143 0x72, /* dq_vref; */
144 0x72, /* ca_vref; */
145 /* phy side */
146 PHY_DRV_ODT_HI_Z, /* rd_odt; */
147 PHY_DRV_ODT_48, /* wr_dq_drv; */
148 PHY_DRV_ODT_40, /* wr_ca_drv; */
149 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
150 0, /* rd_odt_en; */
151 32, /* rd_vref; (unit %, range 3.3% - 48.7%) */
152 },
153 {
Kever Yangbc9b1562019-11-15 11:04:51 +0800154 933 * MHz,
Jagan Tekie3619d12019-07-16 17:27:21 +0530155 0,
156 /* dram side */
157 1, /* dq_odt; */
158 0, /* ca_odt; */
Kever Yangbc9b1562019-11-15 11:04:51 +0800159 3, /* pdds; */
Jagan Tekie3619d12019-07-16 17:27:21 +0530160 0x72, /* dq_vref; */
161 0x72, /* ca_vref; */
162 /* phy side */
Kever Yangbc9b1562019-11-15 11:04:51 +0800163 PHY_DRV_ODT_80, /* rd_odt; */
164 PHY_DRV_ODT_40, /* wr_dq_drv; */
Jagan Tekie3619d12019-07-16 17:27:21 +0530165 PHY_DRV_ODT_40, /* wr_ca_drv; */
166 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
167 1, /* rd_odt_en; */
Kever Yangbc9b1562019-11-15 11:04:51 +0800168 20, /* rd_vref; (unit %, range 3.3% - 48.7%) */
Jagan Tekie3619d12019-07-16 17:27:21 +0530169 },
170 {
171 1066 * MHz,
172 0,
173 /* dram side */
174 6, /* dq_odt; */
175 0, /* ca_odt; */
Kever Yangbc9b1562019-11-15 11:04:51 +0800176 3, /* pdds; */
Jagan Tekie3619d12019-07-16 17:27:21 +0530177 0x10, /* dq_vref; */
178 0x72, /* ca_vref; */
179 /* phy side */
Kever Yangbc9b1562019-11-15 11:04:51 +0800180 PHY_DRV_ODT_80, /* rd_odt; */
Jagan Tekie3619d12019-07-16 17:27:21 +0530181 PHY_DRV_ODT_60, /* wr_dq_drv; */
182 PHY_DRV_ODT_40, /* wr_ca_drv; */
183 PHY_DRV_ODT_40, /* wr_ckcs_drv; */
184 1, /* rd_odt_en; */
Kever Yangbc9b1562019-11-15 11:04:51 +0800185 20, /* rd_vref; (unit %, range 3.3% - 48.7%) */
Jagan Tekie3619d12019-07-16 17:27:21 +0530186 },
187};
188
Simon Glass08d4ba42024-08-01 06:47:23 -0600189/**
190 * phase_sdram_init() - Check if this is the phase where SDRAM init happens
191 *
192 * Returns: true to do SDRAM init in this phase, false to not
193 */
194static bool phase_sdram_init(void)
195{
196 return spl_phase() == PHASE_TPL ||
197 (!IS_ENABLED(CONFIG_TPL) &&
198 !IS_ENABLED(CONFIG_ROCKCHIP_EXTERNAL_TPL) &&
199 !spl_in_proper());
200}
201
Jagan Tekid33056b2019-07-16 17:27:22 +0530202static struct io_setting *
203lpddr4_get_io_settings(const struct rk3399_sdram_params *params, u32 mr5)
204{
205 struct io_setting *io = NULL;
206 u32 n;
207
208 for (n = 0; n < ARRAY_SIZE(lpddr4_io_setting); n++) {
209 io = &lpddr4_io_setting[n];
210
211 if (io->mr5 != 0) {
212 if (io->mhz >= params->base.ddr_freq &&
213 io->mr5 == mr5)
214 break;
215 } else {
216 if (io->mhz >= params->base.ddr_freq)
217 break;
218 }
219 }
220
221 return io;
222}
223
YouMin Chen23ae72e2019-11-15 11:04:45 +0800224static void *get_denali_ctl(const struct chan_info *chan,
Jagan Teki6ea82692019-07-16 17:27:40 +0530225 struct rk3399_sdram_params *params, bool reg)
226{
YouMin Chen23ae72e2019-11-15 11:04:45 +0800227 return reg ? &chan->pctl->denali_ctl : &params->pctl_regs.denali_ctl;
Jagan Teki6ea82692019-07-16 17:27:40 +0530228}
229
YouMin Chen23ae72e2019-11-15 11:04:45 +0800230static void *get_denali_phy(const struct chan_info *chan,
Jagan Teki6ea82692019-07-16 17:27:40 +0530231 struct rk3399_sdram_params *params, bool reg)
232{
YouMin Chen23ae72e2019-11-15 11:04:45 +0800233 return reg ? &chan->publ->denali_phy : &params->phy_regs.denali_phy;
Jagan Teki6ea82692019-07-16 17:27:40 +0530234}
235
Jagan Tekic9151e22019-07-15 23:58:45 +0530236static void *get_ddrc0_con(struct dram_info *dram, u8 channel)
237{
YouMin Chencafbf9f2019-11-15 11:04:47 +0800238 return (channel == 0) ? &dram->grf->ddrc0_con0 : &dram->grf->ddrc1_con0;
Jagan Tekic9151e22019-07-15 23:58:45 +0530239}
240
Jagan Teki783acfd2020-01-09 14:22:17 +0530241static void rkclk_ddr_reset(struct rockchip_cru *cru, u32 channel, u32 ctl,
Jagan Tekice75cfb2019-07-15 23:58:43 +0530242 u32 phy)
243{
244 channel &= 0x1;
245 ctl &= 0x1;
246 phy &= 0x1;
247 writel(CRU_SFTRST_DDR_CTRL(channel, ctl) |
248 CRU_SFTRST_DDR_PHY(channel, phy),
249 &cru->softrst_con[4]);
250}
251
Jagan Teki783acfd2020-01-09 14:22:17 +0530252static void phy_pctrl_reset(struct rockchip_cru *cru, u32 channel)
Jagan Tekice75cfb2019-07-15 23:58:43 +0530253{
254 rkclk_ddr_reset(cru, channel, 1, 1);
255 udelay(10);
256
257 rkclk_ddr_reset(cru, channel, 1, 0);
258 udelay(10);
259
260 rkclk_ddr_reset(cru, channel, 0, 0);
261 udelay(10);
262}
263
Kever Yang50fb9982017-02-22 16:56:35 +0800264static void phy_dll_bypass_set(struct rk3399_ddr_publ_regs *ddr_publ_regs,
265 u32 freq)
266{
267 u32 *denali_phy = ddr_publ_regs->denali_phy;
268
269 /* From IP spec, only freq small than 125 can enter dll bypass mode */
270 if (freq <= 125) {
271 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
272 setbits_le32(&denali_phy[86], (0x3 << 2) << 8);
273 setbits_le32(&denali_phy[214], (0x3 << 2) << 8);
274 setbits_le32(&denali_phy[342], (0x3 << 2) << 8);
275 setbits_le32(&denali_phy[470], (0x3 << 2) << 8);
276
277 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
278 setbits_le32(&denali_phy[547], (0x3 << 2) << 16);
279 setbits_le32(&denali_phy[675], (0x3 << 2) << 16);
280 setbits_le32(&denali_phy[803], (0x3 << 2) << 16);
281 } else {
282 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
283 clrbits_le32(&denali_phy[86], (0x3 << 2) << 8);
284 clrbits_le32(&denali_phy[214], (0x3 << 2) << 8);
285 clrbits_le32(&denali_phy[342], (0x3 << 2) << 8);
286 clrbits_le32(&denali_phy[470], (0x3 << 2) << 8);
287
288 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
289 clrbits_le32(&denali_phy[547], (0x3 << 2) << 16);
290 clrbits_le32(&denali_phy[675], (0x3 << 2) << 16);
291 clrbits_le32(&denali_phy[803], (0x3 << 2) << 16);
292 }
293}
294
295static void set_memory_map(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +0530296 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800297{
Jagan Tekia58ff792019-07-15 23:50:58 +0530298 const struct rk3399_sdram_channel *sdram_ch = &params->ch[channel];
Kever Yang50fb9982017-02-22 16:56:35 +0800299 u32 *denali_ctl = chan->pctl->denali_ctl;
300 u32 *denali_pi = chan->pi->denali_pi;
301 u32 cs_map;
302 u32 reduc;
303 u32 row;
304
305 /* Get row number from ddrconfig setting */
Jagan Teki97867c82019-07-15 23:51:05 +0530306 if (sdram_ch->cap_info.ddrconfig < 2 ||
307 sdram_ch->cap_info.ddrconfig == 4)
Kever Yang50fb9982017-02-22 16:56:35 +0800308 row = 16;
YouMin Chen79f4d912019-11-15 11:04:53 +0800309 else if (sdram_ch->cap_info.ddrconfig == 3 ||
310 sdram_ch->cap_info.ddrconfig == 5)
Kever Yang50fb9982017-02-22 16:56:35 +0800311 row = 14;
312 else
313 row = 15;
314
Jagan Teki97867c82019-07-15 23:51:05 +0530315 cs_map = (sdram_ch->cap_info.rank > 1) ? 3 : 1;
316 reduc = (sdram_ch->cap_info.bw == 2) ? 0 : 1;
Kever Yang50fb9982017-02-22 16:56:35 +0800317
318 /* Set the dram configuration to ctrl */
Jagan Teki97867c82019-07-15 23:51:05 +0530319 clrsetbits_le32(&denali_ctl[191], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800320 clrsetbits_le32(&denali_ctl[190], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530321 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800322 ((16 - row) << 24));
323
324 clrsetbits_le32(&denali_ctl[196], 0x3 | (1 << 16),
325 cs_map | (reduc << 16));
326
327 /* PI_199 PI_COL_DIFF:RW:0:4 */
Jagan Teki97867c82019-07-15 23:51:05 +0530328 clrsetbits_le32(&denali_pi[199], 0xF, (12 - sdram_ch->cap_info.col));
Kever Yang50fb9982017-02-22 16:56:35 +0800329
330 /* PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2 */
331 clrsetbits_le32(&denali_pi[155], (0x3 << 16) | (0x7 << 24),
Jagan Teki97867c82019-07-15 23:51:05 +0530332 ((3 - sdram_ch->cap_info.bk) << 16) |
Kever Yang50fb9982017-02-22 16:56:35 +0800333 ((16 - row) << 24));
Jagan Teki9337cb32019-07-16 17:27:18 +0530334
YouMin Chende57fbf2019-11-15 11:04:46 +0800335 if (params->base.dramtype == LPDDR4) {
Jagan Teki9337cb32019-07-16 17:27:18 +0530336 if (cs_map == 1)
337 cs_map = 0x5;
338 else if (cs_map == 2)
339 cs_map = 0xa;
340 else
341 cs_map = 0xF;
342 }
343
Kever Yang50fb9982017-02-22 16:56:35 +0800344 /* PI_41 PI_CS_MAP:RW:24:4 */
345 clrsetbits_le32(&denali_pi[41], 0xf << 24, cs_map << 24);
Jagan Teki97867c82019-07-15 23:51:05 +0530346 if (sdram_ch->cap_info.rank == 1 && params->base.dramtype == DDR3)
Kever Yang50fb9982017-02-22 16:56:35 +0800347 writel(0x2EC7FFFF, &denali_pi[34]);
348}
349
Thomas Hebbd8105ab2019-12-20 12:28:15 -0800350static int phy_io_config(u32 *denali_phy, u32 *denali_ctl,
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530351 const struct rk3399_sdram_params *params, u32 mr5)
Jagan Tekib5d46632019-07-16 17:27:07 +0530352{
Jagan Tekib5d46632019-07-16 17:27:07 +0530353 u32 vref_mode_dq, vref_value_dq, vref_mode_ac, vref_value_ac;
354 u32 mode_sel;
Jagan Tekib5d46632019-07-16 17:27:07 +0530355 u32 speed;
YouMin Chen79f4d912019-11-15 11:04:53 +0800356 u32 reg_value;
357 u32 ds_value, odt_value;
Jagan Tekib5d46632019-07-16 17:27:07 +0530358
Jagan Teki59a9a572019-07-16 17:27:27 +0530359 /* vref setting & mode setting */
Jagan Tekib5d46632019-07-16 17:27:07 +0530360 if (params->base.dramtype == LPDDR4) {
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530361 struct io_setting *io = lpddr4_get_io_settings(params, mr5);
362 u32 rd_vref = io->rd_vref * 1000;
363
364 if (rd_vref < 36700) {
365 /* MODE_LV[2:0] = LPDDR4 (Range 2)*/
366 vref_mode_dq = 0x7;
Jagan Teki59a9a572019-07-16 17:27:27 +0530367 /* MODE[2:0]= LPDDR4 Range 2(0.4*VDDQ) */
368 mode_sel = 0x5;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530369 vref_value_dq = (rd_vref - 3300) / 521;
370 } else {
371 /* MODE_LV[2:0] = LPDDR4 (Range 1)*/
372 vref_mode_dq = 0x6;
Jagan Teki59a9a572019-07-16 17:27:27 +0530373 /* MODE[2:0]= LPDDR4 Range 1(0.33*VDDQ) */
374 mode_sel = 0x4;
Jagan Teki2dd3efc2019-07-16 17:27:26 +0530375 vref_value_dq = (rd_vref - 15300) / 521;
376 }
Jagan Tekib5d46632019-07-16 17:27:07 +0530377 vref_mode_ac = 0x6;
Jagan Tekia5b07192019-07-16 17:27:28 +0530378 /* VDDQ/3/2=16.8% */
379 vref_value_ac = 0x3;
Jagan Tekib5d46632019-07-16 17:27:07 +0530380 } else if (params->base.dramtype == LPDDR3) {
381 if (params->base.odt == 1) {
382 vref_mode_dq = 0x5; /* LPDDR3 ODT */
YouMin Chen79f4d912019-11-15 11:04:53 +0800383 ds_value = readl(&denali_ctl[138]) & 0xf;
Jagan Tekib5d46632019-07-16 17:27:07 +0530384 odt_value = (readl(&denali_phy[6]) >> 4) & 0xf;
YouMin Chen79f4d912019-11-15 11:04:53 +0800385 if (ds_value == LPDDR3_DS_48) {
Jagan Tekib5d46632019-07-16 17:27:07 +0530386 switch (odt_value) {
387 case PHY_DRV_ODT_240:
YouMin Chen79f4d912019-11-15 11:04:53 +0800388 vref_value_dq = 0x1B;
Jagan Tekib5d46632019-07-16 17:27:07 +0530389 break;
390 case PHY_DRV_ODT_120:
391 vref_value_dq = 0x26;
392 break;
393 case PHY_DRV_ODT_60:
394 vref_value_dq = 0x36;
395 break;
396 default:
397 debug("Invalid ODT value.\n");
398 return -EINVAL;
399 }
YouMin Chen79f4d912019-11-15 11:04:53 +0800400 } else if (ds_value == LPDDR3_DS_40) {
Jagan Tekib5d46632019-07-16 17:27:07 +0530401 switch (odt_value) {
402 case PHY_DRV_ODT_240:
403 vref_value_dq = 0x19;
404 break;
405 case PHY_DRV_ODT_120:
406 vref_value_dq = 0x23;
407 break;
408 case PHY_DRV_ODT_60:
409 vref_value_dq = 0x31;
410 break;
411 default:
412 debug("Invalid ODT value.\n");
413 return -EINVAL;
414 }
YouMin Chen79f4d912019-11-15 11:04:53 +0800415 } else if (ds_value == LPDDR3_DS_34) {
Jagan Tekib5d46632019-07-16 17:27:07 +0530416 switch (odt_value) {
417 case PHY_DRV_ODT_240:
418 vref_value_dq = 0x17;
419 break;
420 case PHY_DRV_ODT_120:
421 vref_value_dq = 0x20;
422 break;
423 case PHY_DRV_ODT_60:
424 vref_value_dq = 0x2e;
425 break;
426 default:
427 debug("Invalid ODT value.\n");
428 return -EINVAL;
429 }
430 } else {
431 debug("Invalid DRV value.\n");
432 return -EINVAL;
433 }
434 } else {
435 vref_mode_dq = 0x2; /* LPDDR3 */
436 vref_value_dq = 0x1f;
437 }
438 vref_mode_ac = 0x2;
439 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530440 mode_sel = 0x0;
Jagan Tekib5d46632019-07-16 17:27:07 +0530441 } else if (params->base.dramtype == DDR3) {
442 /* DDR3L */
443 vref_mode_dq = 0x1;
444 vref_value_dq = 0x1f;
445 vref_mode_ac = 0x1;
446 vref_value_ac = 0x1f;
Jagan Teki213b9ba2019-07-16 17:27:11 +0530447 mode_sel = 0x1;
Jagan Tekib5d46632019-07-16 17:27:07 +0530448 } else {
449 debug("Unknown DRAM type.\n");
450 return -EINVAL;
451 }
452
453 reg_value = (vref_mode_dq << 9) | (0x1 << 8) | vref_value_dq;
454
455 /* PHY_913 PHY_PAD_VREF_CTRL_DQ_0 12bits offset_8 */
456 clrsetbits_le32(&denali_phy[913], 0xfff << 8, reg_value << 8);
457 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_1 12bits offset_0 */
458 clrsetbits_le32(&denali_phy[914], 0xfff, reg_value);
459 /* PHY_914 PHY_PAD_VREF_CTRL_DQ_2 12bits offset_16 */
460 clrsetbits_le32(&denali_phy[914], 0xfff << 16, reg_value << 16);
461 /* PHY_915 PHY_PAD_VREF_CTRL_DQ_3 12bits offset_0 */
462 clrsetbits_le32(&denali_phy[915], 0xfff, reg_value);
463
464 reg_value = (vref_mode_ac << 9) | (0x1 << 8) | vref_value_ac;
465
466 /* PHY_915 PHY_PAD_VREF_CTRL_AC 12bits offset_16 */
467 clrsetbits_le32(&denali_phy[915], 0xfff << 16, reg_value << 16);
468
Jagan Tekib5d46632019-07-16 17:27:07 +0530469 /* PHY_924 PHY_PAD_FDBK_DRIVE */
470 clrsetbits_le32(&denali_phy[924], 0x7 << 15, mode_sel << 15);
471 /* PHY_926 PHY_PAD_DATA_DRIVE */
472 clrsetbits_le32(&denali_phy[926], 0x7 << 6, mode_sel << 6);
473 /* PHY_927 PHY_PAD_DQS_DRIVE */
474 clrsetbits_le32(&denali_phy[927], 0x7 << 6, mode_sel << 6);
475 /* PHY_928 PHY_PAD_ADDR_DRIVE */
476 clrsetbits_le32(&denali_phy[928], 0x7 << 14, mode_sel << 14);
477 /* PHY_929 PHY_PAD_CLK_DRIVE */
478 clrsetbits_le32(&denali_phy[929], 0x7 << 14, mode_sel << 14);
479 /* PHY_935 PHY_PAD_CKE_DRIVE */
480 clrsetbits_le32(&denali_phy[935], 0x7 << 14, mode_sel << 14);
481 /* PHY_937 PHY_PAD_RST_DRIVE */
482 clrsetbits_le32(&denali_phy[937], 0x7 << 14, mode_sel << 14);
483 /* PHY_939 PHY_PAD_CS_DRIVE */
484 clrsetbits_le32(&denali_phy[939], 0x7 << 14, mode_sel << 14);
485
YouMin Chende57fbf2019-11-15 11:04:46 +0800486 if (params->base.dramtype == LPDDR4) {
Jagan Teki5d152172019-07-16 17:27:15 +0530487 /* BOOSTP_EN & BOOSTN_EN */
488 reg_value = ((PHY_BOOSTP_EN << 4) | PHY_BOOSTN_EN);
489 /* PHY_925 PHY_PAD_FDBK_DRIVE2 */
490 clrsetbits_le32(&denali_phy[925], 0xff << 8, reg_value << 8);
491 /* PHY_926 PHY_PAD_DATA_DRIVE */
492 clrsetbits_le32(&denali_phy[926], 0xff << 12, reg_value << 12);
493 /* PHY_927 PHY_PAD_DQS_DRIVE */
494 clrsetbits_le32(&denali_phy[927], 0xff << 14, reg_value << 14);
495 /* PHY_928 PHY_PAD_ADDR_DRIVE */
496 clrsetbits_le32(&denali_phy[928], 0xff << 20, reg_value << 20);
497 /* PHY_929 PHY_PAD_CLK_DRIVE */
498 clrsetbits_le32(&denali_phy[929], 0xff << 22, reg_value << 22);
499 /* PHY_935 PHY_PAD_CKE_DRIVE */
500 clrsetbits_le32(&denali_phy[935], 0xff << 20, reg_value << 20);
501 /* PHY_937 PHY_PAD_RST_DRIVE */
502 clrsetbits_le32(&denali_phy[937], 0xff << 20, reg_value << 20);
503 /* PHY_939 PHY_PAD_CS_DRIVE */
504 clrsetbits_le32(&denali_phy[939], 0xff << 20, reg_value << 20);
Jagan Tekid8681842019-07-16 17:27:16 +0530505
506 /* SLEWP_EN & SLEWN_EN */
507 reg_value = ((PHY_SLEWP_EN << 3) | PHY_SLEWN_EN);
508 /* PHY_924 PHY_PAD_FDBK_DRIVE */
509 clrsetbits_le32(&denali_phy[924], 0x3f << 8, reg_value << 8);
510 /* PHY_926 PHY_PAD_DATA_DRIVE */
511 clrsetbits_le32(&denali_phy[926], 0x3f, reg_value);
512 /* PHY_927 PHY_PAD_DQS_DRIVE */
513 clrsetbits_le32(&denali_phy[927], 0x3f, reg_value);
514 /* PHY_928 PHY_PAD_ADDR_DRIVE */
515 clrsetbits_le32(&denali_phy[928], 0x3f << 8, reg_value << 8);
516 /* PHY_929 PHY_PAD_CLK_DRIVE */
517 clrsetbits_le32(&denali_phy[929], 0x3f << 8, reg_value << 8);
518 /* PHY_935 PHY_PAD_CKE_DRIVE */
519 clrsetbits_le32(&denali_phy[935], 0x3f << 8, reg_value << 8);
520 /* PHY_937 PHY_PAD_RST_DRIVE */
521 clrsetbits_le32(&denali_phy[937], 0x3f << 8, reg_value << 8);
522 /* PHY_939 PHY_PAD_CS_DRIVE */
523 clrsetbits_le32(&denali_phy[939], 0x3f << 8, reg_value << 8);
Jagan Teki5d152172019-07-16 17:27:15 +0530524 }
525
Jagan Tekib5d46632019-07-16 17:27:07 +0530526 /* speed setting */
YouMin Chen79f4d912019-11-15 11:04:53 +0800527 speed = 0x2;
Jagan Tekib5d46632019-07-16 17:27:07 +0530528
529 /* PHY_924 PHY_PAD_FDBK_DRIVE */
530 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
531 /* PHY_926 PHY_PAD_DATA_DRIVE */
532 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
533 /* PHY_927 PHY_PAD_DQS_DRIVE */
534 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
535 /* PHY_928 PHY_PAD_ADDR_DRIVE */
536 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
537 /* PHY_929 PHY_PAD_CLK_DRIVE */
538 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
539 /* PHY_935 PHY_PAD_CKE_DRIVE */
540 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
541 /* PHY_937 PHY_PAD_RST_DRIVE */
542 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
543 /* PHY_939 PHY_PAD_CS_DRIVE */
544 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
545
YouMin Chende57fbf2019-11-15 11:04:46 +0800546 if (params->base.dramtype == LPDDR4) {
Jagan Teki65535a22019-07-16 17:27:17 +0530547 /* RX_CM_INPUT */
548 reg_value = PHY_RX_CM_INPUT;
549 /* PHY_924 PHY_PAD_FDBK_DRIVE */
550 clrsetbits_le32(&denali_phy[924], 0x1 << 14, reg_value << 14);
551 /* PHY_926 PHY_PAD_DATA_DRIVE */
552 clrsetbits_le32(&denali_phy[926], 0x1 << 11, reg_value << 11);
553 /* PHY_927 PHY_PAD_DQS_DRIVE */
554 clrsetbits_le32(&denali_phy[927], 0x1 << 13, reg_value << 13);
555 /* PHY_928 PHY_PAD_ADDR_DRIVE */
556 clrsetbits_le32(&denali_phy[928], 0x1 << 19, reg_value << 19);
557 /* PHY_929 PHY_PAD_CLK_DRIVE */
558 clrsetbits_le32(&denali_phy[929], 0x1 << 21, reg_value << 21);
559 /* PHY_935 PHY_PAD_CKE_DRIVE */
560 clrsetbits_le32(&denali_phy[935], 0x1 << 19, reg_value << 19);
561 /* PHY_937 PHY_PAD_RST_DRIVE */
562 clrsetbits_le32(&denali_phy[937], 0x1 << 19, reg_value << 19);
563 /* PHY_939 PHY_PAD_CS_DRIVE */
564 clrsetbits_le32(&denali_phy[939], 0x1 << 19, reg_value << 19);
565 }
566
Jagan Tekib5d46632019-07-16 17:27:07 +0530567 return 0;
568}
569
Kever Yang50fb9982017-02-22 16:56:35 +0800570static void set_ds_odt(const struct chan_info *chan,
Jagan Teki6ea82692019-07-16 17:27:40 +0530571 struct rk3399_sdram_params *params,
572 bool ctl_phy_reg, u32 mr5)
Kever Yang50fb9982017-02-22 16:56:35 +0800573{
Jagan Teki6ea82692019-07-16 17:27:40 +0530574 u32 *denali_phy = get_denali_phy(chan, params, ctl_phy_reg);
575 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
Kever Yang50fb9982017-02-22 16:56:35 +0800576 u32 tsel_idle_en, tsel_wr_en, tsel_rd_en;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530577 u32 tsel_idle_select_p, tsel_rd_select_p;
578 u32 tsel_idle_select_n, tsel_rd_select_n;
579 u32 tsel_wr_select_dq_p, tsel_wr_select_ca_p;
580 u32 tsel_wr_select_dq_n, tsel_wr_select_ca_n;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530581 u32 tsel_ckcs_select_p, tsel_ckcs_select_n;
Jagan Tekid33056b2019-07-16 17:27:22 +0530582 struct io_setting *io = NULL;
Jagan Teki0cb31122019-07-16 17:27:24 +0530583 u32 soc_odt = 0;
Kever Yang50fb9982017-02-22 16:56:35 +0800584 u32 reg_value;
585
Jagan Tekia58ff792019-07-15 23:50:58 +0530586 if (params->base.dramtype == LPDDR4) {
Jagan Tekid33056b2019-07-16 17:27:22 +0530587 io = lpddr4_get_io_settings(params, mr5);
588
Jagan Tekif676c7c2019-07-15 23:50:56 +0530589 tsel_rd_select_p = PHY_DRV_ODT_HI_Z;
Jagan Tekid33056b2019-07-16 17:27:22 +0530590 tsel_rd_select_n = io->rd_odt;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530591
Jagan Tekif676c7c2019-07-15 23:50:56 +0530592 tsel_idle_select_p = PHY_DRV_ODT_HI_Z;
Kever Yangbc9b1562019-11-15 11:04:51 +0800593 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800594
Jagan Tekid33056b2019-07-16 17:27:22 +0530595 tsel_wr_select_dq_p = io->wr_dq_drv;
Kever Yangbc9b1562019-11-15 11:04:51 +0800596 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530597
Jagan Tekid33056b2019-07-16 17:27:22 +0530598 tsel_wr_select_ca_p = io->wr_ca_drv;
Kever Yangbc9b1562019-11-15 11:04:51 +0800599 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530600
601 tsel_ckcs_select_p = io->wr_ckcs_drv;
602 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yangbc9b1562019-11-15 11:04:51 +0800603
Jagan Teki0cb31122019-07-16 17:27:24 +0530604 switch (tsel_rd_select_n) {
605 case PHY_DRV_ODT_240:
606 soc_odt = 1;
607 break;
608 case PHY_DRV_ODT_120:
609 soc_odt = 2;
610 break;
611 case PHY_DRV_ODT_80:
612 soc_odt = 3;
613 break;
614 case PHY_DRV_ODT_60:
615 soc_odt = 4;
616 break;
617 case PHY_DRV_ODT_48:
618 soc_odt = 5;
619 break;
620 case PHY_DRV_ODT_40:
621 soc_odt = 6;
622 break;
623 case PHY_DRV_ODT_34_3:
624 soc_odt = 6;
625 printf("%s: Unable to support LPDDR4 MR22 Soc ODT\n",
626 __func__);
627 break;
628 case PHY_DRV_ODT_HI_Z:
629 default:
630 soc_odt = 0;
631 break;
632 }
Jagan Tekia58ff792019-07-15 23:50:58 +0530633 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +0800634 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530635 tsel_rd_select_n = PHY_DRV_ODT_HI_Z;
636
Kever Yang50fb9982017-02-22 16:56:35 +0800637 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530638 tsel_idle_select_n = PHY_DRV_ODT_HI_Z;
Kever Yang50fb9982017-02-22 16:56:35 +0800639
Jagan Teki5c3251f2019-07-15 23:51:04 +0530640 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530641 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530642
Kever Yangbc9b1562019-11-15 11:04:51 +0800643 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
644 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530645
646 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
647 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800648 } else {
649 tsel_rd_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530650 tsel_rd_select_n = PHY_DRV_ODT_240;
651
Kever Yang50fb9982017-02-22 16:56:35 +0800652 tsel_idle_select_p = PHY_DRV_ODT_240;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530653 tsel_idle_select_n = PHY_DRV_ODT_240;
Kever Yang50fb9982017-02-22 16:56:35 +0800654
Jagan Teki5c3251f2019-07-15 23:51:04 +0530655 tsel_wr_select_dq_p = PHY_DRV_ODT_34_3;
Jagan Teki36667142019-07-15 23:51:00 +0530656 tsel_wr_select_dq_n = PHY_DRV_ODT_34_3;
Jagan Teki5c3251f2019-07-15 23:51:04 +0530657
658 tsel_wr_select_ca_p = PHY_DRV_ODT_34_3;
Jagan Teki0fd5efb2019-07-15 23:51:02 +0530659 tsel_wr_select_ca_n = PHY_DRV_ODT_34_3;
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530660
661 tsel_ckcs_select_p = PHY_DRV_ODT_34_3;
662 tsel_ckcs_select_n = PHY_DRV_ODT_34_3;
Kever Yang50fb9982017-02-22 16:56:35 +0800663 }
664
Jagan Tekib9584172019-07-16 17:27:25 +0530665 if (params->base.odt == 1) {
Kever Yang50fb9982017-02-22 16:56:35 +0800666 tsel_rd_en = 1;
Jagan Tekib9584172019-07-16 17:27:25 +0530667
668 if (params->base.dramtype == LPDDR4)
669 tsel_rd_en = io->rd_odt_en;
670 } else {
Kever Yang50fb9982017-02-22 16:56:35 +0800671 tsel_rd_en = 0;
Jagan Tekib9584172019-07-16 17:27:25 +0530672 }
Kever Yang50fb9982017-02-22 16:56:35 +0800673
674 tsel_wr_en = 0;
675 tsel_idle_en = 0;
676
Jagan Teki0cb31122019-07-16 17:27:24 +0530677 /* F0_0 */
678 clrsetbits_le32(&denali_ctl[145], 0xFF << 16,
679 (soc_odt | (CS0_MR22_VAL << 3)) << 16);
680 /* F2_0, F1_0 */
681 clrsetbits_le32(&denali_ctl[146], 0xFF00FF,
682 ((soc_odt | (CS0_MR22_VAL << 3)) << 16) |
683 (soc_odt | (CS0_MR22_VAL << 3)));
684 /* F0_1 */
685 clrsetbits_le32(&denali_ctl[159], 0xFF << 16,
686 (soc_odt | (CS1_MR22_VAL << 3)) << 16);
687 /* F2_1, F1_1 */
688 clrsetbits_le32(&denali_ctl[160], 0xFF00FF,
689 ((soc_odt | (CS1_MR22_VAL << 3)) << 16) |
690 (soc_odt | (CS1_MR22_VAL << 3)));
691
Kever Yang50fb9982017-02-22 16:56:35 +0800692 /*
693 * phy_dq_tsel_select_X 24bits DENALI_PHY_6/134/262/390 offset_0
694 * sets termination values for read/idle cycles and drive strength
695 * for write cycles for DQ/DM
696 */
697 reg_value = tsel_rd_select_n | (tsel_rd_select_p << 0x4) |
Jagan Tekib3b34392019-07-15 23:51:01 +0530698 (tsel_wr_select_dq_n << 8) | (tsel_wr_select_dq_p << 12) |
Kever Yang50fb9982017-02-22 16:56:35 +0800699 (tsel_idle_select_n << 16) | (tsel_idle_select_p << 20);
700 clrsetbits_le32(&denali_phy[6], 0xffffff, reg_value);
701 clrsetbits_le32(&denali_phy[134], 0xffffff, reg_value);
702 clrsetbits_le32(&denali_phy[262], 0xffffff, reg_value);
703 clrsetbits_le32(&denali_phy[390], 0xffffff, reg_value);
704
705 /*
706 * phy_dqs_tsel_select_X 24bits DENALI_PHY_7/135/263/391 offset_0
707 * sets termination values for read/idle cycles and drive strength
708 * for write cycles for DQS
709 */
710 clrsetbits_le32(&denali_phy[7], 0xffffff, reg_value);
711 clrsetbits_le32(&denali_phy[135], 0xffffff, reg_value);
712 clrsetbits_le32(&denali_phy[263], 0xffffff, reg_value);
713 clrsetbits_le32(&denali_phy[391], 0xffffff, reg_value);
714
715 /* phy_adr_tsel_select_ 8bits DENALI_PHY_544/672/800 offset_0 */
Jagan Teki7caa3e92019-07-15 23:51:03 +0530716 reg_value = tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 0x4);
YouMin Chende57fbf2019-11-15 11:04:46 +0800717 if (params->base.dramtype == LPDDR4) {
Jagan Teki539ffed2019-07-16 17:27:19 +0530718 /* LPDDR4 these register read always return 0, so
719 * can not use clrsetbits_le32(), need to write32
720 */
721 writel((0x300 << 8) | reg_value, &denali_phy[544]);
722 writel((0x300 << 8) | reg_value, &denali_phy[672]);
723 writel((0x300 << 8) | reg_value, &denali_phy[800]);
724 } else {
725 clrsetbits_le32(&denali_phy[544], 0xff, reg_value);
726 clrsetbits_le32(&denali_phy[672], 0xff, reg_value);
727 clrsetbits_le32(&denali_phy[800], 0xff, reg_value);
728 }
Kever Yang50fb9982017-02-22 16:56:35 +0800729
730 /* phy_pad_addr_drive 8bits DENALI_PHY_928 offset_0 */
731 clrsetbits_le32(&denali_phy[928], 0xff, reg_value);
732
733 /* phy_pad_rst_drive 8bits DENALI_PHY_937 offset_0 */
Jagan Teki6ea82692019-07-16 17:27:40 +0530734 if (!ctl_phy_reg)
735 clrsetbits_le32(&denali_phy[937], 0xff, reg_value);
Kever Yang50fb9982017-02-22 16:56:35 +0800736
737 /* phy_pad_cke_drive 8bits DENALI_PHY_935 offset_0 */
738 clrsetbits_le32(&denali_phy[935], 0xff, reg_value);
739
740 /* phy_pad_cs_drive 8bits DENALI_PHY_939 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530741 clrsetbits_le32(&denali_phy[939], 0xff,
742 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800743
744 /* phy_pad_clk_drive 8bits DENALI_PHY_929 offset_0 */
Jagan Tekic7ffdb72019-07-16 17:27:23 +0530745 clrsetbits_le32(&denali_phy[929], 0xff,
746 tsel_ckcs_select_n | (tsel_ckcs_select_p << 0x4));
Kever Yang50fb9982017-02-22 16:56:35 +0800747
748 /* phy_pad_fdbk_drive 23bit DENALI_PHY_924/925 */
749 clrsetbits_le32(&denali_phy[924], 0xff,
YouMin Chen79f4d912019-11-15 11:04:53 +0800750 tsel_wr_select_ca_n | (tsel_wr_select_ca_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800751 clrsetbits_le32(&denali_phy[925], 0xff,
YouMin Chen79f4d912019-11-15 11:04:53 +0800752 tsel_wr_select_dq_n | (tsel_wr_select_dq_p << 4));
Kever Yang50fb9982017-02-22 16:56:35 +0800753
754 /* phy_dq_tsel_enable_X 3bits DENALI_PHY_5/133/261/389 offset_16 */
755 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
756 << 16;
757 clrsetbits_le32(&denali_phy[5], 0x7 << 16, reg_value);
758 clrsetbits_le32(&denali_phy[133], 0x7 << 16, reg_value);
759 clrsetbits_le32(&denali_phy[261], 0x7 << 16, reg_value);
760 clrsetbits_le32(&denali_phy[389], 0x7 << 16, reg_value);
761
762 /* phy_dqs_tsel_enable_X 3bits DENALI_PHY_6/134/262/390 offset_24 */
763 reg_value = (tsel_rd_en | (tsel_wr_en << 1) | (tsel_idle_en << 2))
764 << 24;
765 clrsetbits_le32(&denali_phy[6], 0x7 << 24, reg_value);
766 clrsetbits_le32(&denali_phy[134], 0x7 << 24, reg_value);
767 clrsetbits_le32(&denali_phy[262], 0x7 << 24, reg_value);
768 clrsetbits_le32(&denali_phy[390], 0x7 << 24, reg_value);
769
770 /* phy_adr_tsel_enable_ 1bit DENALI_PHY_518/646/774 offset_8 */
771 reg_value = tsel_wr_en << 8;
772 clrsetbits_le32(&denali_phy[518], 0x1 << 8, reg_value);
773 clrsetbits_le32(&denali_phy[646], 0x1 << 8, reg_value);
774 clrsetbits_le32(&denali_phy[774], 0x1 << 8, reg_value);
775
776 /* phy_pad_addr_term tsel 1bit DENALI_PHY_933 offset_17 */
777 reg_value = tsel_wr_en << 17;
778 clrsetbits_le32(&denali_phy[933], 0x1 << 17, reg_value);
779 /*
780 * pad_rst/cke/cs/clk_term tsel 1bits
781 * DENALI_PHY_938/936/940/934 offset_17
782 */
783 clrsetbits_le32(&denali_phy[938], 0x1 << 17, reg_value);
784 clrsetbits_le32(&denali_phy[936], 0x1 << 17, reg_value);
785 clrsetbits_le32(&denali_phy[940], 0x1 << 17, reg_value);
786 clrsetbits_le32(&denali_phy[934], 0x1 << 17, reg_value);
787
788 /* phy_pad_fdbk_term 1bit DENALI_PHY_930 offset_17 */
789 clrsetbits_le32(&denali_phy[930], 0x1 << 17, reg_value);
Jagan Tekib5d46632019-07-16 17:27:07 +0530790
Thomas Hebbd8105ab2019-12-20 12:28:15 -0800791 phy_io_config(denali_phy, denali_ctl, params, mr5);
Kever Yang50fb9982017-02-22 16:56:35 +0800792}
793
YouMin Chen99027372019-11-15 11:04:48 +0800794static void pctl_start(struct dram_info *dram,
795 struct rk3399_sdram_params *params,
796 u32 channel_mask)
Jagan Tekic9151e22019-07-15 23:58:45 +0530797{
YouMin Chen99027372019-11-15 11:04:48 +0800798 const struct chan_info *chan_0 = &dram->chan[0];
799 const struct chan_info *chan_1 = &dram->chan[1];
800
801 u32 *denali_ctl_0 = chan_0->pctl->denali_ctl;
802 u32 *denali_phy_0 = chan_0->publ->denali_phy;
803 u32 *ddrc0_con_0 = get_ddrc0_con(dram, 0);
804 u32 *denali_ctl_1 = chan_1->pctl->denali_ctl;
805 u32 *denali_phy_1 = chan_1->publ->denali_phy;
806 u32 *ddrc1_con_0 = get_ddrc0_con(dram, 1);
Jagan Tekic9151e22019-07-15 23:58:45 +0530807 u32 count = 0;
808 u32 byte, tmp;
809
YouMin Chen99027372019-11-15 11:04:48 +0800810 /* PHY_DLL_RST_EN */
811 if (channel_mask & 1) {
812 writel(0x01000000, &ddrc0_con_0);
813 clrsetbits_le32(&denali_phy_0[957], 0x3 << 24, 0x2 << 24);
814 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530815
YouMin Chen99027372019-11-15 11:04:48 +0800816 if (channel_mask & 1) {
817 count = 0;
818 while (!(readl(&denali_ctl_0[203]) & (1 << 3))) {
819 if (count > 1000) {
820 printf("%s: Failed to init pctl channel 0\n",
821 __func__);
822 while (1)
823 ;
824 }
825 udelay(1);
826 count++;
827 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530828
YouMin Chen99027372019-11-15 11:04:48 +0800829 writel(0x01000100, &ddrc0_con_0);
830 for (byte = 0; byte < 4; byte++) {
831 tmp = 0x820;
832 writel((tmp << 16) | tmp,
833 &denali_phy_0[53 + (128 * byte)]);
834 writel((tmp << 16) | tmp,
835 &denali_phy_0[54 + (128 * byte)]);
836 writel((tmp << 16) | tmp,
837 &denali_phy_0[55 + (128 * byte)]);
838 writel((tmp << 16) | tmp,
839 &denali_phy_0[56 + (128 * byte)]);
840 writel((tmp << 16) | tmp,
841 &denali_phy_0[57 + (128 * byte)]);
842 clrsetbits_le32(&denali_phy_0[58 + (128 * byte)],
843 0xffff, tmp);
Jagan Tekic9151e22019-07-15 23:58:45 +0530844 }
YouMin Chen99027372019-11-15 11:04:48 +0800845 clrsetbits_le32(&denali_ctl_0[68], PWRUP_SREFRESH_EXIT,
846 dram->pwrup_srefresh_exit[0]);
847 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530848
YouMin Chen99027372019-11-15 11:04:48 +0800849 if (channel_mask & 2) {
850 writel(0x01000000, &ddrc1_con_0);
851 clrsetbits_le32(&denali_phy_1[957], 0x3 << 24, 0x2 << 24);
Jagan Tekic9151e22019-07-15 23:58:45 +0530852 }
YouMin Chen99027372019-11-15 11:04:48 +0800853 if (channel_mask & 2) {
854 count = 0;
855 while (!(readl(&denali_ctl_1[203]) & (1 << 3))) {
856 if (count > 1000) {
857 printf("%s: Failed to init pctl channel 1\n",
858 __func__);
859 while (1)
860 ;
861 }
862 udelay(1);
863 count++;
864 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530865
YouMin Chen99027372019-11-15 11:04:48 +0800866 writel(0x01000100, &ddrc1_con_0);
867 for (byte = 0; byte < 4; byte++) {
868 tmp = 0x820;
869 writel((tmp << 16) | tmp,
870 &denali_phy_1[53 + (128 * byte)]);
871 writel((tmp << 16) | tmp,
872 &denali_phy_1[54 + (128 * byte)]);
873 writel((tmp << 16) | tmp,
874 &denali_phy_1[55 + (128 * byte)]);
875 writel((tmp << 16) | tmp,
876 &denali_phy_1[56 + (128 * byte)]);
877 writel((tmp << 16) | tmp,
878 &denali_phy_1[57 + (128 * byte)]);
879 clrsetbits_le32(&denali_phy_1[58 + (128 * byte)],
880 0xffff, tmp);
881 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530882
YouMin Chen99027372019-11-15 11:04:48 +0800883 clrsetbits_le32(&denali_ctl_1[68], PWRUP_SREFRESH_EXIT,
884 dram->pwrup_srefresh_exit[1]);
Jagan Tekic9151e22019-07-15 23:58:45 +0530885
YouMin Chen99027372019-11-15 11:04:48 +0800886 /*
887 * restore channel 1 RESET original setting
888 * to avoid 240ohm too weak to prevent ESD test
889 */
890 if (params->base.dramtype == LPDDR4)
891 clrsetbits_le32(&denali_phy_1[937], 0xff,
892 params->phy_regs.denali_phy[937] &
893 0xFF);
Jagan Tekic9151e22019-07-15 23:58:45 +0530894 }
Jagan Tekic9151e22019-07-15 23:58:45 +0530895}
896
Jagan Teki4ef5c012019-07-15 23:58:44 +0530897static int pctl_cfg(struct dram_info *dram, const struct chan_info *chan,
Jagan Tekid33056b2019-07-16 17:27:22 +0530898 u32 channel, struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +0800899{
900 u32 *denali_ctl = chan->pctl->denali_ctl;
901 u32 *denali_pi = chan->pi->denali_pi;
902 u32 *denali_phy = chan->publ->denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +0530903 const u32 *params_ctl = params->pctl_regs.denali_ctl;
904 const u32 *params_phy = params->phy_regs.denali_phy;
Kever Yang50fb9982017-02-22 16:56:35 +0800905 u32 tmp, tmp1, tmp2;
YouMin Chen99027372019-11-15 11:04:48 +0800906 struct rk3399_sdram_params *params_cfg;
907 u32 byte;
Kever Yang50fb9982017-02-22 16:56:35 +0800908
YouMin Chen99027372019-11-15 11:04:48 +0800909 dram->ops->modify_param(chan, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800910 /*
911 * work around controller bug:
912 * Do not program DRAM_CLASS until NO_PHY_IND_TRAIN_INT is programmed
913 */
YouMin Chen23ae72e2019-11-15 11:04:45 +0800914 sdram_copy_to_reg(&denali_ctl[1], &params_ctl[1],
915 sizeof(struct rk3399_ddr_pctl_regs) - 4);
Kever Yang50fb9982017-02-22 16:56:35 +0800916 writel(params_ctl[0], &denali_ctl[0]);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530917
Jagan Tekicc9da9a2019-07-16 17:27:13 +0530918 /*
919 * two channel init at the same time, then ZQ Cal Start
920 * at the same time, it will use the same RZQ, but cannot
921 * start at the same time.
922 *
923 * So, increase tINIT3 for channel 1, will avoid two
924 * channel ZQ Cal Start at the same time
925 */
926 if (params->base.dramtype == LPDDR4 && channel == 1) {
927 tmp = ((params->base.ddr_freq * MHz + 999) / 1000);
928 tmp1 = readl(&denali_ctl[14]);
929 writel(tmp + tmp1, &denali_ctl[14]);
930 }
931
YouMin Chen23ae72e2019-11-15 11:04:45 +0800932 sdram_copy_to_reg(denali_pi, &params->pi_regs.denali_pi[0],
933 sizeof(struct rk3399_ddr_pi_regs));
Jagan Tekiacf8e0f2019-07-15 23:50:57 +0530934
Kever Yang50fb9982017-02-22 16:56:35 +0800935 /* rank count need to set for init */
Jagan Tekia58ff792019-07-15 23:50:58 +0530936 set_memory_map(chan, channel, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800937
Jagan Tekia58ff792019-07-15 23:50:58 +0530938 writel(params->phy_regs.denali_phy[910], &denali_phy[910]);
939 writel(params->phy_regs.denali_phy[911], &denali_phy[911]);
940 writel(params->phy_regs.denali_phy[912], &denali_phy[912]);
Kever Yang50fb9982017-02-22 16:56:35 +0800941
YouMin Chende57fbf2019-11-15 11:04:46 +0800942 if (params->base.dramtype == LPDDR4) {
Jagan Tekib49b5dc2019-07-16 17:27:14 +0530943 writel(params->phy_regs.denali_phy[898], &denali_phy[898]);
944 writel(params->phy_regs.denali_phy[919], &denali_phy[919]);
945 }
946
Jagan Tekic9151e22019-07-15 23:58:45 +0530947 dram->pwrup_srefresh_exit[channel] = readl(&denali_ctl[68]) &
948 PWRUP_SREFRESH_EXIT;
Kever Yang50fb9982017-02-22 16:56:35 +0800949 clrbits_le32(&denali_ctl[68], PWRUP_SREFRESH_EXIT);
950
951 /* PHY_DLL_RST_EN */
952 clrsetbits_le32(&denali_phy[957], 0x3 << 24, 1 << 24);
953
954 setbits_le32(&denali_pi[0], START);
955 setbits_le32(&denali_ctl[0], START);
956
Jagan Teki5e927182019-07-16 17:27:12 +0530957 /**
958 * LPDDR4 use PLL bypass mode for init
959 * not need to wait for the PLL to lock
960 */
961 if (params->base.dramtype != LPDDR4) {
962 /* Waiting for phy DLL lock */
963 while (1) {
964 tmp = readl(&denali_phy[920]);
965 tmp1 = readl(&denali_phy[921]);
966 tmp2 = readl(&denali_phy[922]);
967 if ((((tmp >> 16) & 0x1) == 0x1) &&
968 (((tmp1 >> 16) & 0x1) == 0x1) &&
969 (((tmp1 >> 0) & 0x1) == 0x1) &&
970 (((tmp2 >> 0) & 0x1) == 0x1))
971 break;
972 }
Kever Yang50fb9982017-02-22 16:56:35 +0800973 }
974
YouMin Chen23ae72e2019-11-15 11:04:45 +0800975 sdram_copy_to_reg(&denali_phy[896], &params_phy[896], (958 - 895) * 4);
976 sdram_copy_to_reg(&denali_phy[0], &params_phy[0], (90 - 0 + 1) * 4);
977 sdram_copy_to_reg(&denali_phy[128], &params_phy[128],
978 (218 - 128 + 1) * 4);
979 sdram_copy_to_reg(&denali_phy[256], &params_phy[256],
980 (346 - 256 + 1) * 4);
981 sdram_copy_to_reg(&denali_phy[384], &params_phy[384],
982 (474 - 384 + 1) * 4);
983 sdram_copy_to_reg(&denali_phy[512], &params_phy[512],
984 (549 - 512 + 1) * 4);
985 sdram_copy_to_reg(&denali_phy[640], &params_phy[640],
986 (677 - 640 + 1) * 4);
987 sdram_copy_to_reg(&denali_phy[768], &params_phy[768],
988 (805 - 768 + 1) * 4);
989
YouMin Chen99027372019-11-15 11:04:48 +0800990 if (params->base.dramtype == LPDDR4)
991 params_cfg = dram->ops->get_phy_index_params(1, params);
992 else
993 params_cfg = dram->ops->get_phy_index_params(0, params);
Kever Yang50fb9982017-02-22 16:56:35 +0800994
YouMin Chen99027372019-11-15 11:04:48 +0800995 clrsetbits_le32(&params_cfg->phy_regs.denali_phy[896], 0x3 << 8,
996 0 << 8);
997 writel(params_cfg->phy_regs.denali_phy[896], &denali_phy[896]);
Kever Yang50fb9982017-02-22 16:56:35 +0800998
YouMin Chen99027372019-11-15 11:04:48 +0800999 writel(params->phy_regs.denali_phy[83] + (0x10 << 16),
1000 &denali_phy[83]);
1001 writel(params->phy_regs.denali_phy[84] + (0x10 << 8),
1002 &denali_phy[84]);
1003 writel(params->phy_regs.denali_phy[211] + (0x10 << 16),
1004 &denali_phy[211]);
1005 writel(params->phy_regs.denali_phy[212] + (0x10 << 8),
1006 &denali_phy[212]);
1007 writel(params->phy_regs.denali_phy[339] + (0x10 << 16),
1008 &denali_phy[339]);
1009 writel(params->phy_regs.denali_phy[340] + (0x10 << 8),
1010 &denali_phy[340]);
1011 writel(params->phy_regs.denali_phy[467] + (0x10 << 16),
1012 &denali_phy[467]);
1013 writel(params->phy_regs.denali_phy[468] + (0x10 << 8),
1014 &denali_phy[468]);
1015
1016 if (params->base.dramtype == LPDDR4) {
1017 /*
1018 * to improve write dqs and dq phase from 1.5ns to 3.5ns
1019 * at 50MHz. this's the measure result from oscilloscope
1020 * of dqs and dq write signal.
1021 */
1022 for (byte = 0; byte < 4; byte++) {
1023 tmp = 0x680;
1024 clrsetbits_le32(&denali_phy[1 + (128 * byte)],
1025 0xfff << 8, tmp << 8);
1026 }
1027 /*
1028 * to workaround 366ball two channel's RESET connect to
1029 * one RESET signal of die
1030 */
1031 if (channel == 1)
1032 clrsetbits_le32(&denali_phy[937], 0xff,
1033 PHY_DRV_ODT_240 |
1034 (PHY_DRV_ODT_240 << 0x4));
1035 }
Kever Yang50fb9982017-02-22 16:56:35 +08001036
Kever Yang50fb9982017-02-22 16:56:35 +08001037 return 0;
1038}
1039
1040static void select_per_cs_training_index(const struct chan_info *chan,
1041 u32 rank)
1042{
1043 u32 *denali_phy = chan->publ->denali_phy;
1044
1045 /* PHY_84 PHY_PER_CS_TRAINING_EN_0 1bit offset_16 */
Jagan Tekif676c7c2019-07-15 23:50:56 +05301046 if ((readl(&denali_phy[84]) >> 16) & 1) {
Kever Yang50fb9982017-02-22 16:56:35 +08001047 /*
1048 * PHY_8/136/264/392
1049 * phy_per_cs_training_index_X 1bit offset_24
1050 */
1051 clrsetbits_le32(&denali_phy[8], 0x1 << 24, rank << 24);
1052 clrsetbits_le32(&denali_phy[136], 0x1 << 24, rank << 24);
1053 clrsetbits_le32(&denali_phy[264], 0x1 << 24, rank << 24);
1054 clrsetbits_le32(&denali_phy[392], 0x1 << 24, rank << 24);
1055 }
1056}
1057
1058static void override_write_leveling_value(const struct chan_info *chan)
1059{
1060 u32 *denali_ctl = chan->pctl->denali_ctl;
1061 u32 *denali_phy = chan->publ->denali_phy;
1062 u32 byte;
1063
1064 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
1065 setbits_le32(&denali_phy[896], 1);
1066
1067 /*
1068 * PHY_8/136/264/392
1069 * phy_per_cs_training_multicast_en_X 1bit offset_16
1070 */
1071 clrsetbits_le32(&denali_phy[8], 0x1 << 16, 1 << 16);
1072 clrsetbits_le32(&denali_phy[136], 0x1 << 16, 1 << 16);
1073 clrsetbits_le32(&denali_phy[264], 0x1 << 16, 1 << 16);
1074 clrsetbits_le32(&denali_phy[392], 0x1 << 16, 1 << 16);
1075
1076 for (byte = 0; byte < 4; byte++)
1077 clrsetbits_le32(&denali_phy[63 + (128 * byte)], 0xffff << 16,
1078 0x200 << 16);
1079
1080 /* PHY_896 PHY_FREQ_SEL_MULTICAST_EN 1bit offset_0 */
1081 clrbits_le32(&denali_phy[896], 1);
1082
1083 /* CTL_200 ctrlupd_req 1bit offset_8 */
1084 clrsetbits_le32(&denali_ctl[200], 0x1 << 8, 0x1 << 8);
1085}
1086
1087static int data_training_ca(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301088 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001089{
1090 u32 *denali_pi = chan->pi->denali_pi;
1091 u32 *denali_phy = chan->publ->denali_phy;
1092 u32 i, tmp;
1093 u32 obs_0, obs_1, obs_2, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301094 u32 rank = params->ch[channel].cap_info.rank;
Jagan Tekibafcc142019-07-15 23:58:41 +05301095 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001096
Jagan Tekia6079612019-07-15 23:58:40 +05301097 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1098 writel(0x00003f7c, (&denali_pi[175]));
1099
Jagan Tekif05675e2019-07-16 17:27:09 +05301100 if (params->base.dramtype == LPDDR4)
1101 rank_mask = (rank == 1) ? 0x5 : 0xf;
1102 else
1103 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Tekibafcc142019-07-15 23:58:41 +05301104
1105 for (i = 0; i < 4; i++) {
1106 if (!(rank_mask & (1 << i)))
1107 continue;
1108
Kever Yang50fb9982017-02-22 16:56:35 +08001109 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301110
Kever Yang50fb9982017-02-22 16:56:35 +08001111 /* PI_100 PI_CALVL_EN:RW:8:2 */
1112 clrsetbits_le32(&denali_pi[100], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301113
Kever Yang50fb9982017-02-22 16:56:35 +08001114 /* PI_92 PI_CALVL_REQ:WR:16:1,PI_CALVL_CS:RW:24:2 */
1115 clrsetbits_le32(&denali_pi[92],
1116 (0x1 << 16) | (0x3 << 24),
1117 (0x1 << 16) | (i << 24));
1118
1119 /* Waiting for training complete */
1120 while (1) {
1121 /* PI_174 PI_INT_STATUS:RD:8:18 */
1122 tmp = readl(&denali_pi[174]) >> 8;
1123 /*
1124 * check status obs
1125 * PHY_532/660/789 phy_adr_calvl_obs1_:0:32
1126 */
1127 obs_0 = readl(&denali_phy[532]);
1128 obs_1 = readl(&denali_phy[660]);
1129 obs_2 = readl(&denali_phy[788]);
1130 if (((obs_0 >> 30) & 0x3) ||
1131 ((obs_1 >> 30) & 0x3) ||
1132 ((obs_2 >> 30) & 0x3))
1133 obs_err = 1;
1134 if ((((tmp >> 11) & 0x1) == 0x1) &&
1135 (((tmp >> 13) & 0x1) == 0x1) &&
1136 (((tmp >> 5) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301137 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001138 break;
1139 else if ((((tmp >> 5) & 0x1) == 0x1) ||
1140 (obs_err == 1))
1141 return -EIO;
1142 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301143
Kever Yang50fb9982017-02-22 16:56:35 +08001144 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1145 writel(0x00003f7c, (&denali_pi[175]));
1146 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301147
Kever Yang50fb9982017-02-22 16:56:35 +08001148 clrbits_le32(&denali_pi[100], 0x3 << 8);
1149
1150 return 0;
1151}
1152
1153static int data_training_wl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301154 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001155{
1156 u32 *denali_pi = chan->pi->denali_pi;
1157 u32 *denali_phy = chan->publ->denali_phy;
1158 u32 i, tmp;
1159 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301160 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001161
Jagan Tekia6079612019-07-15 23:58:40 +05301162 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1163 writel(0x00003f7c, (&denali_pi[175]));
1164
Kever Yang50fb9982017-02-22 16:56:35 +08001165 for (i = 0; i < rank; i++) {
1166 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301167
Kever Yang50fb9982017-02-22 16:56:35 +08001168 /* PI_60 PI_WRLVL_EN:RW:8:2 */
1169 clrsetbits_le32(&denali_pi[60], 0x3 << 8, 0x2 << 8);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301170
Kever Yang50fb9982017-02-22 16:56:35 +08001171 /* PI_59 PI_WRLVL_REQ:WR:8:1,PI_WRLVL_CS:RW:16:2 */
1172 clrsetbits_le32(&denali_pi[59],
1173 (0x1 << 8) | (0x3 << 16),
1174 (0x1 << 8) | (i << 16));
1175
1176 /* Waiting for training complete */
1177 while (1) {
1178 /* PI_174 PI_INT_STATUS:RD:8:18 */
1179 tmp = readl(&denali_pi[174]) >> 8;
1180
1181 /*
1182 * check status obs, if error maybe can not
1183 * get leveling done PHY_40/168/296/424
1184 * phy_wrlvl_status_obs_X:0:13
1185 */
1186 obs_0 = readl(&denali_phy[40]);
1187 obs_1 = readl(&denali_phy[168]);
1188 obs_2 = readl(&denali_phy[296]);
1189 obs_3 = readl(&denali_phy[424]);
1190 if (((obs_0 >> 12) & 0x1) ||
1191 ((obs_1 >> 12) & 0x1) ||
1192 ((obs_2 >> 12) & 0x1) ||
1193 ((obs_3 >> 12) & 0x1))
1194 obs_err = 1;
1195 if ((((tmp >> 10) & 0x1) == 0x1) &&
1196 (((tmp >> 13) & 0x1) == 0x1) &&
1197 (((tmp >> 4) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301198 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001199 break;
1200 else if ((((tmp >> 4) & 0x1) == 0x1) ||
1201 (obs_err == 1))
1202 return -EIO;
1203 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301204
Kever Yang50fb9982017-02-22 16:56:35 +08001205 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1206 writel(0x00003f7c, (&denali_pi[175]));
1207 }
1208
1209 override_write_leveling_value(chan);
1210 clrbits_le32(&denali_pi[60], 0x3 << 8);
1211
1212 return 0;
1213}
1214
1215static int data_training_rg(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301216 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001217{
1218 u32 *denali_pi = chan->pi->denali_pi;
1219 u32 *denali_phy = chan->publ->denali_phy;
1220 u32 i, tmp;
1221 u32 obs_0, obs_1, obs_2, obs_3, obs_err = 0;
Jagan Teki97867c82019-07-15 23:51:05 +05301222 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001223
Jagan Tekia6079612019-07-15 23:58:40 +05301224 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1225 writel(0x00003f7c, (&denali_pi[175]));
1226
Kever Yang50fb9982017-02-22 16:56:35 +08001227 for (i = 0; i < rank; i++) {
1228 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301229
Kever Yang50fb9982017-02-22 16:56:35 +08001230 /* PI_80 PI_RDLVL_GATE_EN:RW:24:2 */
1231 clrsetbits_le32(&denali_pi[80], 0x3 << 24, 0x2 << 24);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301232
Kever Yang50fb9982017-02-22 16:56:35 +08001233 /*
1234 * PI_74 PI_RDLVL_GATE_REQ:WR:16:1
1235 * PI_RDLVL_CS:RW:24:2
1236 */
1237 clrsetbits_le32(&denali_pi[74],
1238 (0x1 << 16) | (0x3 << 24),
1239 (0x1 << 16) | (i << 24));
1240
1241 /* Waiting for training complete */
1242 while (1) {
1243 /* PI_174 PI_INT_STATUS:RD:8:18 */
1244 tmp = readl(&denali_pi[174]) >> 8;
1245
1246 /*
1247 * check status obs
1248 * PHY_43/171/299/427
1249 * PHY_GTLVL_STATUS_OBS_x:16:8
1250 */
1251 obs_0 = readl(&denali_phy[43]);
1252 obs_1 = readl(&denali_phy[171]);
1253 obs_2 = readl(&denali_phy[299]);
1254 obs_3 = readl(&denali_phy[427]);
1255 if (((obs_0 >> (16 + 6)) & 0x3) ||
1256 ((obs_1 >> (16 + 6)) & 0x3) ||
1257 ((obs_2 >> (16 + 6)) & 0x3) ||
1258 ((obs_3 >> (16 + 6)) & 0x3))
1259 obs_err = 1;
1260 if ((((tmp >> 9) & 0x1) == 0x1) &&
1261 (((tmp >> 13) & 0x1) == 0x1) &&
1262 (((tmp >> 3) & 0x1) == 0x0) &&
Jagan Tekif676c7c2019-07-15 23:50:56 +05301263 obs_err == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001264 break;
1265 else if ((((tmp >> 3) & 0x1) == 0x1) ||
1266 (obs_err == 1))
1267 return -EIO;
1268 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301269
Kever Yang50fb9982017-02-22 16:56:35 +08001270 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1271 writel(0x00003f7c, (&denali_pi[175]));
1272 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301273
Kever Yang50fb9982017-02-22 16:56:35 +08001274 clrbits_le32(&denali_pi[80], 0x3 << 24);
1275
1276 return 0;
1277}
1278
1279static int data_training_rl(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301280 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001281{
1282 u32 *denali_pi = chan->pi->denali_pi;
1283 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301284 u32 rank = params->ch[channel].cap_info.rank;
Kever Yang50fb9982017-02-22 16:56:35 +08001285
Jagan Tekia6079612019-07-15 23:58:40 +05301286 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1287 writel(0x00003f7c, (&denali_pi[175]));
1288
Kever Yang50fb9982017-02-22 16:56:35 +08001289 for (i = 0; i < rank; i++) {
1290 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301291
Kever Yang50fb9982017-02-22 16:56:35 +08001292 /* PI_80 PI_RDLVL_EN:RW:16:2 */
1293 clrsetbits_le32(&denali_pi[80], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301294
Kever Yang50fb9982017-02-22 16:56:35 +08001295 /* PI_74 PI_RDLVL_REQ:WR:8:1,PI_RDLVL_CS:RW:24:2 */
1296 clrsetbits_le32(&denali_pi[74],
1297 (0x1 << 8) | (0x3 << 24),
1298 (0x1 << 8) | (i << 24));
1299
1300 /* Waiting for training complete */
1301 while (1) {
1302 /* PI_174 PI_INT_STATUS:RD:8:18 */
1303 tmp = readl(&denali_pi[174]) >> 8;
1304
1305 /*
1306 * make sure status obs not report error bit
1307 * PHY_46/174/302/430
1308 * phy_rdlvl_status_obs_X:16:8
1309 */
1310 if ((((tmp >> 8) & 0x1) == 0x1) &&
1311 (((tmp >> 13) & 0x1) == 0x1) &&
1312 (((tmp >> 2) & 0x1) == 0x0))
1313 break;
1314 else if (((tmp >> 2) & 0x1) == 0x1)
1315 return -EIO;
1316 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301317
Kever Yang50fb9982017-02-22 16:56:35 +08001318 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1319 writel(0x00003f7c, (&denali_pi[175]));
1320 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301321
Kever Yang50fb9982017-02-22 16:56:35 +08001322 clrbits_le32(&denali_pi[80], 0x3 << 16);
1323
1324 return 0;
1325}
1326
1327static int data_training_wdql(const struct chan_info *chan, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301328 const struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001329{
1330 u32 *denali_pi = chan->pi->denali_pi;
1331 u32 i, tmp;
Jagan Teki97867c82019-07-15 23:51:05 +05301332 u32 rank = params->ch[channel].cap_info.rank;
Jagan Teki87723592019-07-15 23:58:42 +05301333 u32 rank_mask;
Kever Yang50fb9982017-02-22 16:56:35 +08001334
Jagan Tekia6079612019-07-15 23:58:40 +05301335 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1336 writel(0x00003f7c, (&denali_pi[175]));
1337
Jagan Tekid7504c02019-07-16 17:27:10 +05301338 if (params->base.dramtype == LPDDR4)
1339 rank_mask = (rank == 1) ? 0x5 : 0xf;
1340 else
1341 rank_mask = (rank == 1) ? 0x1 : 0x3;
Jagan Teki87723592019-07-15 23:58:42 +05301342
1343 for (i = 0; i < 4; i++) {
1344 if (!(rank_mask & (1 << i)))
1345 continue;
1346
Kever Yang50fb9982017-02-22 16:56:35 +08001347 select_per_cs_training_index(chan, i);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301348
Kever Yang50fb9982017-02-22 16:56:35 +08001349 /*
1350 * disable PI_WDQLVL_VREF_EN before wdq leveling?
YouMin Chen79f4d912019-11-15 11:04:53 +08001351 * PI_117 PI_WDQLVL_VREF_EN:RW:8:1
Kever Yang50fb9982017-02-22 16:56:35 +08001352 */
YouMin Chen79f4d912019-11-15 11:04:53 +08001353 clrbits_le32(&denali_pi[117], 0x1 << 8);
Kever Yang50fb9982017-02-22 16:56:35 +08001354 /* PI_124 PI_WDQLVL_EN:RW:16:2 */
1355 clrsetbits_le32(&denali_pi[124], 0x3 << 16, 0x2 << 16);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301356
Kever Yang50fb9982017-02-22 16:56:35 +08001357 /* PI_121 PI_WDQLVL_REQ:WR:8:1,PI_WDQLVL_CS:RW:16:2 */
1358 clrsetbits_le32(&denali_pi[121],
1359 (0x1 << 8) | (0x3 << 16),
1360 (0x1 << 8) | (i << 16));
1361
1362 /* Waiting for training complete */
1363 while (1) {
1364 /* PI_174 PI_INT_STATUS:RD:8:18 */
1365 tmp = readl(&denali_pi[174]) >> 8;
1366 if ((((tmp >> 12) & 0x1) == 0x1) &&
1367 (((tmp >> 13) & 0x1) == 0x1) &&
1368 (((tmp >> 6) & 0x1) == 0x0))
1369 break;
1370 else if (((tmp >> 6) & 0x1) == 0x1)
1371 return -EIO;
1372 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301373
Kever Yang50fb9982017-02-22 16:56:35 +08001374 /* clear interrupt,PI_175 PI_INT_ACK:WR:0:17 */
1375 writel(0x00003f7c, (&denali_pi[175]));
1376 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05301377
Kever Yang50fb9982017-02-22 16:56:35 +08001378 clrbits_le32(&denali_pi[124], 0x3 << 16);
1379
1380 return 0;
1381}
1382
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301383static int data_training(struct dram_info *dram, u32 channel,
Jagan Tekia58ff792019-07-15 23:50:58 +05301384 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001385 u32 training_flag)
1386{
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301387 struct chan_info *chan = &dram->chan[channel];
Kever Yang50fb9982017-02-22 16:56:35 +08001388 u32 *denali_phy = chan->publ->denali_phy;
Jagan Teki6214ff22019-07-15 23:58:39 +05301389 int ret;
Kever Yang50fb9982017-02-22 16:56:35 +08001390
1391 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1392 setbits_le32(&denali_phy[927], (1 << 22));
1393
1394 if (training_flag == PI_FULL_TRAINING) {
Jagan Tekia58ff792019-07-15 23:50:58 +05301395 if (params->base.dramtype == LPDDR4) {
Jagan Teki6ea82692019-07-16 17:27:40 +05301396 training_flag = PI_WRITE_LEVELING |
Kever Yang50fb9982017-02-22 16:56:35 +08001397 PI_READ_GATE_TRAINING |
1398 PI_READ_LEVELING | PI_WDQ_LEVELING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301399 } else if (params->base.dramtype == LPDDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001400 training_flag = PI_CA_TRAINING | PI_WRITE_LEVELING |
1401 PI_READ_GATE_TRAINING;
Jagan Tekia58ff792019-07-15 23:50:58 +05301402 } else if (params->base.dramtype == DDR3) {
Kever Yang50fb9982017-02-22 16:56:35 +08001403 training_flag = PI_WRITE_LEVELING |
1404 PI_READ_GATE_TRAINING |
1405 PI_READ_LEVELING;
1406 }
1407 }
1408
1409 /* ca training(LPDDR4,LPDDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301410 if ((training_flag & PI_CA_TRAINING) == PI_CA_TRAINING) {
1411 ret = data_training_ca(chan, channel, params);
1412 if (ret < 0) {
1413 debug("%s: data training ca failed\n", __func__);
1414 return ret;
1415 }
1416 }
Kever Yang50fb9982017-02-22 16:56:35 +08001417
1418 /* write leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301419 if ((training_flag & PI_WRITE_LEVELING) == PI_WRITE_LEVELING) {
1420 ret = data_training_wl(chan, channel, params);
1421 if (ret < 0) {
1422 debug("%s: data training wl failed\n", __func__);
1423 return ret;
1424 }
1425 }
Kever Yang50fb9982017-02-22 16:56:35 +08001426
1427 /* read gate training(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301428 if ((training_flag & PI_READ_GATE_TRAINING) == PI_READ_GATE_TRAINING) {
1429 ret = data_training_rg(chan, channel, params);
1430 if (ret < 0) {
1431 debug("%s: data training rg failed\n", __func__);
1432 return ret;
1433 }
1434 }
Kever Yang50fb9982017-02-22 16:56:35 +08001435
1436 /* read leveling(LPDDR4,LPDDR3,DDR3 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301437 if ((training_flag & PI_READ_LEVELING) == PI_READ_LEVELING) {
1438 ret = data_training_rl(chan, channel, params);
1439 if (ret < 0) {
1440 debug("%s: data training rl failed\n", __func__);
1441 return ret;
1442 }
1443 }
Kever Yang50fb9982017-02-22 16:56:35 +08001444
1445 /* wdq leveling(LPDDR4 support) */
Jagan Teki6214ff22019-07-15 23:58:39 +05301446 if ((training_flag & PI_WDQ_LEVELING) == PI_WDQ_LEVELING) {
1447 ret = data_training_wdql(chan, channel, params);
1448 if (ret < 0) {
1449 debug("%s: data training wdql failed\n", __func__);
1450 return ret;
1451 }
1452 }
Kever Yang50fb9982017-02-22 16:56:35 +08001453
1454 /* PHY_927 PHY_PAD_DQS_DRIVE RPULL offset_22 */
1455 clrbits_le32(&denali_phy[927], (1 << 22));
1456
1457 return 0;
1458}
1459
1460static void set_ddrconfig(const struct chan_info *chan,
Jagan Tekia58ff792019-07-15 23:50:58 +05301461 const struct rk3399_sdram_params *params,
Kever Yang50fb9982017-02-22 16:56:35 +08001462 unsigned char channel, u32 ddrconfig)
1463{
1464 /* only need to set ddrconfig */
YouMin Chen23ae72e2019-11-15 11:04:45 +08001465 struct msch_regs *ddr_msch_regs = chan->msch;
Kever Yang50fb9982017-02-22 16:56:35 +08001466 unsigned int cs0_cap = 0;
1467 unsigned int cs1_cap = 0;
1468
Jagan Teki97867c82019-07-15 23:51:05 +05301469 cs0_cap = (1 << (params->ch[channel].cap_info.cs0_row
1470 + params->ch[channel].cap_info.col
1471 + params->ch[channel].cap_info.bk
1472 + params->ch[channel].cap_info.bw - 20));
1473 if (params->ch[channel].cap_info.rank > 1)
1474 cs1_cap = cs0_cap >> (params->ch[channel].cap_info.cs0_row
1475 - params->ch[channel].cap_info.cs1_row);
1476 if (params->ch[channel].cap_info.row_3_4) {
Kever Yang50fb9982017-02-22 16:56:35 +08001477 cs0_cap = cs0_cap * 3 / 4;
1478 cs1_cap = cs1_cap * 3 / 4;
1479 }
1480
1481 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
1482 writel(((cs0_cap / 32) & 0xff) | (((cs1_cap / 32) & 0xff) << 8),
1483 &ddr_msch_regs->ddrsize);
1484}
1485
YouMin Chen23ae72e2019-11-15 11:04:45 +08001486static void sdram_msch_config(struct msch_regs *msch,
1487 struct sdram_msch_timings *noc_timings)
1488{
1489 writel(noc_timings->ddrtiminga0.d32,
1490 &msch->ddrtiminga0.d32);
1491 writel(noc_timings->ddrtimingb0.d32,
1492 &msch->ddrtimingb0.d32);
1493 writel(noc_timings->ddrtimingc0.d32,
1494 &msch->ddrtimingc0.d32);
1495 writel(noc_timings->devtodev0.d32,
1496 &msch->devtodev0.d32);
1497 writel(noc_timings->ddrmode.d32,
1498 &msch->ddrmode.d32);
1499}
1500
Kever Yang50fb9982017-02-22 16:56:35 +08001501static void dram_all_config(struct dram_info *dram,
YouMin Chen23ae72e2019-11-15 11:04:45 +08001502 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08001503{
Jagan Teki2d337122019-07-16 17:27:00 +05301504 u32 sys_reg2 = 0;
Jagan Teki9d8769c2019-07-16 17:27:01 +05301505 u32 sys_reg3 = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08001506 unsigned int channel, idx;
1507
Kever Yang50fb9982017-02-22 16:56:35 +08001508 for (channel = 0, idx = 0;
Jagan Tekia58ff792019-07-15 23:50:58 +05301509 (idx < params->base.num_channels) && (channel < 2);
Kever Yang50fb9982017-02-22 16:56:35 +08001510 channel++) {
YouMin Chen23ae72e2019-11-15 11:04:45 +08001511 struct msch_regs *ddr_msch_regs;
1512 struct sdram_msch_timings *noc_timing;
Kever Yang50fb9982017-02-22 16:56:35 +08001513
Jagan Teki97867c82019-07-15 23:51:05 +05301514 if (params->ch[channel].cap_info.col == 0)
Kever Yang50fb9982017-02-22 16:56:35 +08001515 continue;
1516 idx++;
YouMin Chen23ae72e2019-11-15 11:04:45 +08001517 sdram_org_config(&params->ch[channel].cap_info,
1518 &params->base, &sys_reg2,
1519 &sys_reg3, channel);
Kever Yang50fb9982017-02-22 16:56:35 +08001520 ddr_msch_regs = dram->chan[channel].msch;
Jagan Tekia58ff792019-07-15 23:50:58 +05301521 noc_timing = &params->ch[channel].noc_timings;
YouMin Chen23ae72e2019-11-15 11:04:45 +08001522 sdram_msch_config(ddr_msch_regs, noc_timing);
Kever Yang50fb9982017-02-22 16:56:35 +08001523
Jagan Tekib02c5482019-07-16 17:27:20 +05301524 /**
1525 * rank 1 memory clock disable (dfi_dram_clk_disable = 1)
1526 *
1527 * The hardware for LPDDR4 with
1528 * - CLK0P/N connect to lower 16-bits
1529 * - CLK1P/N connect to higher 16-bits
1530 *
1531 * dfi dram clk is configured via CLK1P/N, so disabling
1532 * dfi dram clk will disable the CLK1P/N as well for lpddr4.
1533 */
1534 if (params->ch[channel].cap_info.rank == 1 &&
1535 params->base.dramtype != LPDDR4)
Kever Yang50fb9982017-02-22 16:56:35 +08001536 setbits_le32(&dram->chan[channel].pctl->denali_ctl[276],
1537 1 << 17);
1538 }
1539
Jagan Teki2d337122019-07-16 17:27:00 +05301540 writel(sys_reg2, &dram->pmugrf->os_reg2);
Jagan Teki9d8769c2019-07-16 17:27:01 +05301541 writel(sys_reg3, &dram->pmugrf->os_reg3);
Kever Yang50fb9982017-02-22 16:56:35 +08001542 rk_clrsetreg(&dram->pmusgrf->soc_con4, 0x1f << 10,
Jagan Tekia58ff792019-07-15 23:50:58 +05301543 params->base.stride << 10);
Kever Yang50fb9982017-02-22 16:56:35 +08001544
1545 /* reboot hold register set */
1546 writel(PRESET_SGRF_HOLD(0) | PRESET_GPIO0_HOLD(1) |
1547 PRESET_GPIO1_HOLD(1),
1548 &dram->pmucru->pmucru_rstnhold_con[1]);
1549 clrsetbits_le32(&dram->cru->glb_rst_con, 0x3, 0x3);
1550}
1551
Kever Yange723a552019-08-12 20:02:29 +08001552static void set_cap_relate_config(const struct chan_info *chan,
1553 struct rk3399_sdram_params *params,
1554 unsigned int channel)
1555{
1556 u32 *denali_ctl = chan->pctl->denali_ctl;
1557 u32 tmp;
YouMin Chen23ae72e2019-11-15 11:04:45 +08001558 struct sdram_msch_timings *noc_timing;
Kever Yange723a552019-08-12 20:02:29 +08001559
1560 if (params->base.dramtype == LPDDR3) {
1561 tmp = (8 << params->ch[channel].cap_info.bw) /
1562 (8 << params->ch[channel].cap_info.dbw);
1563
1564 /**
1565 * memdata_ratio
1566 * 1 -> 0, 2 -> 1, 4 -> 2
1567 */
1568 clrsetbits_le32(&denali_ctl[197], 0x7,
1569 (tmp >> 1));
1570 clrsetbits_le32(&denali_ctl[198], 0x7 << 8,
1571 (tmp >> 1) << 8);
1572 }
1573
1574 noc_timing = &params->ch[channel].noc_timings;
1575
1576 /*
1577 * noc timing bw relate timing is 32 bit, and real bw is 16bit
1578 * actually noc reg is setting at function dram_all_config
1579 */
1580 if (params->ch[channel].cap_info.bw == 16 &&
1581 noc_timing->ddrmode.b.mwrsize == 2) {
1582 if (noc_timing->ddrmode.b.burstsize)
1583 noc_timing->ddrmode.b.burstsize -= 1;
1584 noc_timing->ddrmode.b.mwrsize -= 1;
1585 noc_timing->ddrtimingc0.b.burstpenalty *= 2;
1586 noc_timing->ddrtimingc0.b.wrtomwr *= 2;
1587 }
1588}
1589
1590static u32 calculate_ddrconfig(struct rk3399_sdram_params *params, u32 channel)
1591{
1592 unsigned int cs0_row = params->ch[channel].cap_info.cs0_row;
1593 unsigned int col = params->ch[channel].cap_info.col;
1594 unsigned int bw = params->ch[channel].cap_info.bw;
1595 u16 ddr_cfg_2_rbc[] = {
1596 /*
1597 * [6] highest bit col
1598 * [5:3] max row(14+n)
1599 * [2] insertion row
1600 * [1:0] col(9+n),col, data bus 32bit
1601 *
1602 * highbitcol, max_row, insertion_row, col
1603 */
1604 ((0 << 6) | (2 << 3) | (0 << 2) | 0), /* 0 */
1605 ((0 << 6) | (2 << 3) | (0 << 2) | 1), /* 1 */
1606 ((0 << 6) | (1 << 3) | (0 << 2) | 2), /* 2 */
1607 ((0 << 6) | (0 << 3) | (0 << 2) | 3), /* 3 */
1608 ((0 << 6) | (2 << 3) | (1 << 2) | 1), /* 4 */
1609 ((0 << 6) | (1 << 3) | (1 << 2) | 2), /* 5 */
1610 ((1 << 6) | (0 << 3) | (0 << 2) | 2), /* 6 */
1611 ((1 << 6) | (1 << 3) | (0 << 2) | 2), /* 7 */
1612 };
1613 u32 i;
1614
1615 col -= (bw == 2) ? 0 : 1;
1616 col -= 9;
1617
1618 for (i = 0; i < 4; i++) {
1619 if ((col == (ddr_cfg_2_rbc[i] & 0x3)) &&
1620 (cs0_row <= (((ddr_cfg_2_rbc[i] >> 3) & 0x7) + 14)))
1621 break;
1622 }
1623
1624 if (i >= 4)
1625 i = -EINVAL;
1626
1627 return i;
1628}
1629
YouMin Chen6ba388f2019-11-15 11:04:49 +08001630static void set_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf, u32 stride)
1631{
1632 rk_clrsetreg(&pmusgrf->soc_con4, 0x1f << 10, stride << 10);
1633}
1634
Jagan Teki2da26d72022-12-14 23:20:48 +05301635#if !defined(CONFIG_RAM_ROCKCHIP_LPDDR4)
YouMin Chende57fbf2019-11-15 11:04:46 +08001636static int data_training_first(struct dram_info *dram, u32 channel, u8 rank,
1637 struct rk3399_sdram_params *params)
Jagan Teki9eb935a2019-07-16 17:27:30 +05301638{
1639 u8 training_flag = PI_READ_GATE_TRAINING;
1640
1641 /*
1642 * LPDDR3 CA training msut be trigger before
1643 * other training.
1644 * DDR3 is not have CA training.
1645 */
1646
1647 if (params->base.dramtype == LPDDR3)
1648 training_flag |= PI_CA_TRAINING;
1649
1650 return data_training(dram, channel, params, training_flag);
1651}
1652
Kever Yang50fb9982017-02-22 16:56:35 +08001653static int switch_to_phy_index1(struct dram_info *dram,
Lee Jones29cbb302022-08-11 08:58:48 +01001654 struct rk3399_sdram_params *params,
1655 u32 unused)
Kever Yang50fb9982017-02-22 16:56:35 +08001656{
1657 u32 channel;
1658 u32 *denali_phy;
Jagan Tekia58ff792019-07-15 23:50:58 +05301659 u32 ch_count = params->base.num_channels;
Kever Yang50fb9982017-02-22 16:56:35 +08001660 int ret;
1661 int i = 0;
1662
1663 writel(RK_CLRSETBITS(0x03 << 4 | 1 << 2 | 1,
1664 1 << 4 | 1 << 2 | 1),
1665 &dram->cic->cic_ctrl0);
1666 while (!(readl(&dram->cic->cic_status0) & (1 << 2))) {
1667 mdelay(10);
1668 i++;
1669 if (i > 10) {
1670 debug("index1 frequency change overtime\n");
1671 return -ETIME;
1672 }
1673 }
1674
1675 i = 0;
1676 writel(RK_CLRSETBITS(1 << 1, 1 << 1), &dram->cic->cic_ctrl0);
1677 while (!(readl(&dram->cic->cic_status0) & (1 << 0))) {
1678 mdelay(10);
Heinrich Schuchardt80516592018-03-18 12:10:55 +01001679 i++;
Kever Yang50fb9982017-02-22 16:56:35 +08001680 if (i > 10) {
1681 debug("index1 frequency done overtime\n");
1682 return -ETIME;
1683 }
1684 }
1685
1686 for (channel = 0; channel < ch_count; channel++) {
1687 denali_phy = dram->chan[channel].publ->denali_phy;
1688 clrsetbits_le32(&denali_phy[896], (0x3 << 8) | 1, 1 << 8);
Jagan Teki5ff7abe2019-07-16 17:27:29 +05301689 ret = data_training(dram, channel, params, PI_FULL_TRAINING);
Jagan Teki6214ff22019-07-15 23:58:39 +05301690 if (ret < 0) {
Kever Yang50fb9982017-02-22 16:56:35 +08001691 debug("index1 training failed\n");
1692 return ret;
1693 }
1694 }
1695
1696 return 0;
1697}
1698
YouMin Chen99027372019-11-15 11:04:48 +08001699struct rk3399_sdram_params
1700 *get_phy_index_params(u32 phy_fn,
1701 struct rk3399_sdram_params *params)
1702{
1703 if (phy_fn == 0)
1704 return params;
1705 else
1706 return NULL;
1707}
1708
1709void modify_param(const struct chan_info *chan,
1710 struct rk3399_sdram_params *params)
1711{
1712 struct rk3399_sdram_params *params_cfg;
1713 u32 *denali_pi_params;
1714
1715 denali_pi_params = params->pi_regs.denali_pi;
1716
1717 /* modify PHY F0/F1/F2 params */
1718 params_cfg = get_phy_index_params(0, params);
1719 set_ds_odt(chan, params_cfg, false, 0);
1720
1721 clrsetbits_le32(&denali_pi_params[45], 0x1 << 24, 0x1 << 24);
1722 clrsetbits_le32(&denali_pi_params[61], 0x1 << 24, 0x1 << 24);
1723 clrsetbits_le32(&denali_pi_params[76], 0x1 << 24, 0x1 << 24);
1724 clrsetbits_le32(&denali_pi_params[77], 0x1, 0x1);
1725}
Jagan Teki940d1252019-07-16 17:27:39 +05301726#else
1727
YouMin Chende57fbf2019-11-15 11:04:46 +08001728struct rk3399_sdram_params dfs_cfgs_lpddr4[] = {
1729#include "sdram-rk3399-lpddr4-400.inc"
1730#include "sdram-rk3399-lpddr4-800.inc"
Jagan Teki6ea82692019-07-16 17:27:40 +05301731};
1732
YouMin Chen99027372019-11-15 11:04:48 +08001733static struct rk3399_sdram_params
1734 *lpddr4_get_phy_index_params(u32 phy_fn,
1735 struct rk3399_sdram_params *params)
1736{
1737 if (phy_fn == 0)
1738 return params;
1739 else if (phy_fn == 1)
1740 return &dfs_cfgs_lpddr4[1];
1741 else if (phy_fn == 2)
1742 return &dfs_cfgs_lpddr4[0];
1743 else
1744 return NULL;
1745}
1746
Jagan Teki6ea82692019-07-16 17:27:40 +05301747static void *get_denali_pi(const struct chan_info *chan,
1748 struct rk3399_sdram_params *params, bool reg)
1749{
1750 return reg ? &chan->pi->denali_pi : &params->pi_regs.denali_pi;
1751}
1752
YouMin Chende57fbf2019-11-15 11:04:46 +08001753static u32 lpddr4_get_phy_fn(struct rk3399_sdram_params *params, u32 ctl_fn)
Jagan Teki6ea82692019-07-16 17:27:40 +05301754{
YouMin Chende57fbf2019-11-15 11:04:46 +08001755 u32 lpddr4_phy_fn[] = {1, 0, 0xb};
Jagan Teki6ea82692019-07-16 17:27:40 +05301756
YouMin Chende57fbf2019-11-15 11:04:46 +08001757 return lpddr4_phy_fn[ctl_fn];
Jagan Teki6ea82692019-07-16 17:27:40 +05301758}
1759
YouMin Chende57fbf2019-11-15 11:04:46 +08001760static u32 lpddr4_get_ctl_fn(struct rk3399_sdram_params *params, u32 phy_fn)
Jagan Teki6ea82692019-07-16 17:27:40 +05301761{
YouMin Chende57fbf2019-11-15 11:04:46 +08001762 u32 lpddr4_ctl_fn[] = {1, 0, 2};
Jagan Teki6ea82692019-07-16 17:27:40 +05301763
YouMin Chende57fbf2019-11-15 11:04:46 +08001764 return lpddr4_ctl_fn[phy_fn];
Jagan Teki6ea82692019-07-16 17:27:40 +05301765}
1766
Jagan Tekicc117bb2019-07-16 17:27:31 +05301767static u32 get_ddr_stride(struct rk3399_pmusgrf_regs *pmusgrf)
1768{
1769 return ((readl(&pmusgrf->soc_con4) >> 10) & 0x1F);
1770}
1771
YouMin Chen6ba388f2019-11-15 11:04:49 +08001772/*
Jagan Tekicc117bb2019-07-16 17:27:31 +05301773 * read mr_num mode register
1774 * rank = 1: cs0
1775 * rank = 2: cs1
1776 */
1777static int read_mr(struct rk3399_ddr_pctl_regs *ddr_pctl_regs, u32 rank,
1778 u32 mr_num, u32 *buf)
1779{
1780 s32 timeout = 100;
1781
1782 writel(((1 << 16) | (((rank == 2) ? 1 : 0) << 8) | mr_num) << 8,
1783 &ddr_pctl_regs->denali_ctl[118]);
1784
1785 while (0 == (readl(&ddr_pctl_regs->denali_ctl[203]) &
1786 ((1 << 21) | (1 << 12)))) {
1787 udelay(1);
1788
1789 if (timeout <= 0) {
1790 printf("%s: pctl timeout!\n", __func__);
1791 return -ETIMEDOUT;
1792 }
1793
1794 timeout--;
1795 }
1796
1797 if (!(readl(&ddr_pctl_regs->denali_ctl[203]) & (1 << 12))) {
1798 *buf = readl(&ddr_pctl_regs->denali_ctl[119]) & 0xFF;
1799 } else {
1800 printf("%s: read mr failed with 0x%x status\n", __func__,
1801 readl(&ddr_pctl_regs->denali_ctl[17]) & 0x3);
1802 *buf = 0;
1803 }
1804
1805 setbits_le32(&ddr_pctl_regs->denali_ctl[205], (1 << 21) | (1 << 12));
1806
1807 return 0;
1808}
1809
1810static int lpddr4_mr_detect(struct dram_info *dram, u32 channel, u8 rank,
1811 struct rk3399_sdram_params *params)
1812{
1813 u64 cs0_cap;
1814 u32 stride;
1815 u32 cs = 0, col = 0, bk = 0, bw = 0, row_3_4 = 0;
1816 u32 cs0_row = 0, cs1_row = 0, ddrconfig = 0;
1817 u32 mr5, mr12, mr14;
1818 struct chan_info *chan = &dram->chan[channel];
1819 struct rk3399_ddr_pctl_regs *ddr_pctl_regs = chan->pctl;
1820 void __iomem *addr = NULL;
1821 int ret = 0;
1822 u32 val;
1823
1824 stride = get_ddr_stride(dram->pmusgrf);
1825
1826 if (params->ch[channel].cap_info.col == 0) {
1827 ret = -EPERM;
1828 goto end;
1829 }
1830
1831 cs = params->ch[channel].cap_info.rank;
1832 col = params->ch[channel].cap_info.col;
1833 bk = params->ch[channel].cap_info.bk;
1834 bw = params->ch[channel].cap_info.bw;
1835 row_3_4 = params->ch[channel].cap_info.row_3_4;
1836 cs0_row = params->ch[channel].cap_info.cs0_row;
1837 cs1_row = params->ch[channel].cap_info.cs1_row;
1838 ddrconfig = params->ch[channel].cap_info.ddrconfig;
1839
1840 /* 2GB */
1841 params->ch[channel].cap_info.rank = 2;
1842 params->ch[channel].cap_info.col = 10;
1843 params->ch[channel].cap_info.bk = 3;
1844 params->ch[channel].cap_info.bw = 2;
1845 params->ch[channel].cap_info.row_3_4 = 0;
1846 params->ch[channel].cap_info.cs0_row = 15;
1847 params->ch[channel].cap_info.cs1_row = 15;
1848 params->ch[channel].cap_info.ddrconfig = 1;
1849
1850 set_memory_map(chan, channel, params);
1851 params->ch[channel].cap_info.ddrconfig =
1852 calculate_ddrconfig(params, channel);
1853 set_ddrconfig(chan, params, channel,
1854 params->ch[channel].cap_info.ddrconfig);
1855 set_cap_relate_config(chan, params, channel);
1856
1857 cs0_cap = (1 << (params->ch[channel].cap_info.bw
1858 + params->ch[channel].cap_info.col
1859 + params->ch[channel].cap_info.bk
1860 + params->ch[channel].cap_info.cs0_row));
1861
1862 if (params->ch[channel].cap_info.row_3_4)
1863 cs0_cap = cs0_cap * 3 / 4;
1864
1865 if (channel == 0)
1866 set_ddr_stride(dram->pmusgrf, 0x17);
1867 else
1868 set_ddr_stride(dram->pmusgrf, 0x18);
1869
1870 /* read and write data to DRAM, avoid be optimized by compiler. */
1871 if (rank == 1)
1872 addr = (void __iomem *)0x100;
1873 else if (rank == 2)
1874 addr = (void __iomem *)(cs0_cap + 0x100);
1875
1876 val = readl(addr);
1877 writel(val + 1, addr);
1878
1879 read_mr(ddr_pctl_regs, rank, 5, &mr5);
1880 read_mr(ddr_pctl_regs, rank, 12, &mr12);
1881 read_mr(ddr_pctl_regs, rank, 14, &mr14);
1882
1883 if (mr5 == 0 || mr12 != 0x4d || mr14 != 0x4d) {
1884 ret = -EINVAL;
1885 goto end;
1886 }
1887end:
1888 params->ch[channel].cap_info.rank = cs;
1889 params->ch[channel].cap_info.col = col;
1890 params->ch[channel].cap_info.bk = bk;
1891 params->ch[channel].cap_info.bw = bw;
1892 params->ch[channel].cap_info.row_3_4 = row_3_4;
1893 params->ch[channel].cap_info.cs0_row = cs0_row;
1894 params->ch[channel].cap_info.cs1_row = cs1_row;
1895 params->ch[channel].cap_info.ddrconfig = ddrconfig;
1896
1897 set_ddr_stride(dram->pmusgrf, stride);
1898
1899 return ret;
1900}
Jagan Teki6ea82692019-07-16 17:27:40 +05301901
1902static void set_lpddr4_dq_odt(const struct chan_info *chan,
YouMin Chende57fbf2019-11-15 11:04:46 +08001903 struct rk3399_sdram_params *params, u32 ctl_fn,
Jagan Teki6ea82692019-07-16 17:27:40 +05301904 bool en, bool ctl_phy_reg, u32 mr5)
1905{
1906 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
1907 u32 *denali_pi = get_denali_pi(chan, params, ctl_phy_reg);
1908 struct io_setting *io;
1909 u32 reg_value;
1910
Jagan Teki6ea82692019-07-16 17:27:40 +05301911 io = lpddr4_get_io_settings(params, mr5);
YouMin Chende57fbf2019-11-15 11:04:46 +08001912 if (en)
1913 reg_value = io->dq_odt;
1914 else
1915 reg_value = 0;
Jagan Teki6ea82692019-07-16 17:27:40 +05301916
YouMin Chende57fbf2019-11-15 11:04:46 +08001917 switch (ctl_fn) {
Jagan Teki6ea82692019-07-16 17:27:40 +05301918 case 0:
1919 clrsetbits_le32(&denali_ctl[139], 0x7 << 24, reg_value << 24);
1920 clrsetbits_le32(&denali_ctl[153], 0x7 << 24, reg_value << 24);
1921
1922 clrsetbits_le32(&denali_pi[132], 0x7 << 0, (reg_value << 0));
1923 clrsetbits_le32(&denali_pi[139], 0x7 << 16, (reg_value << 16));
1924 clrsetbits_le32(&denali_pi[147], 0x7 << 0, (reg_value << 0));
1925 clrsetbits_le32(&denali_pi[154], 0x7 << 16, (reg_value << 16));
1926 break;
1927 case 1:
1928 clrsetbits_le32(&denali_ctl[140], 0x7 << 0, reg_value << 0);
1929 clrsetbits_le32(&denali_ctl[154], 0x7 << 0, reg_value << 0);
1930
1931 clrsetbits_le32(&denali_pi[129], 0x7 << 16, (reg_value << 16));
1932 clrsetbits_le32(&denali_pi[137], 0x7 << 0, (reg_value << 0));
1933 clrsetbits_le32(&denali_pi[144], 0x7 << 16, (reg_value << 16));
1934 clrsetbits_le32(&denali_pi[152], 0x7 << 0, (reg_value << 0));
1935 break;
1936 case 2:
1937 default:
1938 clrsetbits_le32(&denali_ctl[140], 0x7 << 8, (reg_value << 8));
1939 clrsetbits_le32(&denali_ctl[154], 0x7 << 8, (reg_value << 8));
1940
1941 clrsetbits_le32(&denali_pi[127], 0x7 << 0, (reg_value << 0));
1942 clrsetbits_le32(&denali_pi[134], 0x7 << 16, (reg_value << 16));
1943 clrsetbits_le32(&denali_pi[142], 0x7 << 0, (reg_value << 0));
1944 clrsetbits_le32(&denali_pi[149], 0x7 << 16, (reg_value << 16));
1945 break;
1946 }
1947}
1948
1949static void set_lpddr4_ca_odt(const struct chan_info *chan,
YouMin Chende57fbf2019-11-15 11:04:46 +08001950 struct rk3399_sdram_params *params, u32 ctl_fn,
Jagan Teki6ea82692019-07-16 17:27:40 +05301951 bool en, bool ctl_phy_reg, u32 mr5)
1952{
1953 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
1954 u32 *denali_pi = get_denali_pi(chan, params, ctl_phy_reg);
1955 struct io_setting *io;
1956 u32 reg_value;
1957
Jagan Teki6ea82692019-07-16 17:27:40 +05301958 io = lpddr4_get_io_settings(params, mr5);
YouMin Chende57fbf2019-11-15 11:04:46 +08001959 if (en)
1960 reg_value = io->ca_odt;
1961 else
1962 reg_value = 0;
Jagan Teki6ea82692019-07-16 17:27:40 +05301963
YouMin Chende57fbf2019-11-15 11:04:46 +08001964 switch (ctl_fn) {
Jagan Teki6ea82692019-07-16 17:27:40 +05301965 case 0:
1966 clrsetbits_le32(&denali_ctl[139], 0x7 << 28, reg_value << 28);
1967 clrsetbits_le32(&denali_ctl[153], 0x7 << 28, reg_value << 28);
1968
1969 clrsetbits_le32(&denali_pi[132], 0x7 << 4, reg_value << 4);
1970 clrsetbits_le32(&denali_pi[139], 0x7 << 20, reg_value << 20);
1971 clrsetbits_le32(&denali_pi[147], 0x7 << 4, reg_value << 4);
1972 clrsetbits_le32(&denali_pi[154], 0x7 << 20, reg_value << 20);
1973 break;
1974 case 1:
1975 clrsetbits_le32(&denali_ctl[140], 0x7 << 4, reg_value << 4);
1976 clrsetbits_le32(&denali_ctl[154], 0x7 << 4, reg_value << 4);
1977
1978 clrsetbits_le32(&denali_pi[129], 0x7 << 20, reg_value << 20);
1979 clrsetbits_le32(&denali_pi[137], 0x7 << 4, reg_value << 4);
1980 clrsetbits_le32(&denali_pi[144], 0x7 << 20, reg_value << 20);
1981 clrsetbits_le32(&denali_pi[152], 0x7 << 4, reg_value << 4);
1982 break;
1983 case 2:
1984 default:
1985 clrsetbits_le32(&denali_ctl[140], 0x7 << 12, (reg_value << 12));
1986 clrsetbits_le32(&denali_ctl[154], 0x7 << 12, (reg_value << 12));
1987
1988 clrsetbits_le32(&denali_pi[127], 0x7 << 4, reg_value << 4);
1989 clrsetbits_le32(&denali_pi[134], 0x7 << 20, reg_value << 20);
1990 clrsetbits_le32(&denali_pi[142], 0x7 << 4, reg_value << 4);
1991 clrsetbits_le32(&denali_pi[149], 0x7 << 20, reg_value << 20);
1992 break;
1993 }
1994}
1995
1996static void set_lpddr4_MR3(const struct chan_info *chan,
YouMin Chende57fbf2019-11-15 11:04:46 +08001997 struct rk3399_sdram_params *params, u32 ctl_fn,
Jagan Teki6ea82692019-07-16 17:27:40 +05301998 bool ctl_phy_reg, u32 mr5)
1999{
2000 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
2001 u32 *denali_pi = get_denali_pi(chan, params, ctl_phy_reg);
2002 struct io_setting *io;
2003 u32 reg_value;
2004
2005 io = lpddr4_get_io_settings(params, mr5);
2006
2007 reg_value = ((io->pdds << 3) | 1);
2008
YouMin Chende57fbf2019-11-15 11:04:46 +08002009 switch (ctl_fn) {
Jagan Teki6ea82692019-07-16 17:27:40 +05302010 case 0:
2011 clrsetbits_le32(&denali_ctl[138], 0xFFFF, reg_value);
2012 clrsetbits_le32(&denali_ctl[152], 0xFFFF, reg_value);
2013
2014 clrsetbits_le32(&denali_pi[131], 0xFFFF << 16, reg_value << 16);
2015 clrsetbits_le32(&denali_pi[139], 0xFFFF, reg_value);
2016 clrsetbits_le32(&denali_pi[146], 0xFFFF << 16, reg_value << 16);
2017 clrsetbits_le32(&denali_pi[154], 0xFFFF, reg_value);
2018 break;
2019 case 1:
2020 clrsetbits_le32(&denali_ctl[138], 0xFFFF << 16,
2021 reg_value << 16);
2022 clrsetbits_le32(&denali_ctl[152], 0xFFFF << 16,
2023 reg_value << 16);
2024
2025 clrsetbits_le32(&denali_pi[129], 0xFFFF, reg_value);
2026 clrsetbits_le32(&denali_pi[136], 0xFFFF << 16, reg_value << 16);
2027 clrsetbits_le32(&denali_pi[144], 0xFFFF, reg_value);
2028 clrsetbits_le32(&denali_pi[151], 0xFFFF << 16, reg_value << 16);
2029 break;
2030 case 2:
2031 default:
2032 clrsetbits_le32(&denali_ctl[139], 0xFFFF, reg_value);
2033 clrsetbits_le32(&denali_ctl[153], 0xFFFF, reg_value);
2034
2035 clrsetbits_le32(&denali_pi[126], 0xFFFF << 16, reg_value << 16);
2036 clrsetbits_le32(&denali_pi[134], 0xFFFF, reg_value);
2037 clrsetbits_le32(&denali_pi[141], 0xFFFF << 16, reg_value << 16);
2038 clrsetbits_le32(&denali_pi[149], 0xFFFF, reg_value);
2039 break;
2040 }
2041}
2042
2043static void set_lpddr4_MR12(const struct chan_info *chan,
YouMin Chende57fbf2019-11-15 11:04:46 +08002044 struct rk3399_sdram_params *params, u32 ctl_fn,
Jagan Teki6ea82692019-07-16 17:27:40 +05302045 bool ctl_phy_reg, u32 mr5)
2046{
2047 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
2048 u32 *denali_pi = get_denali_pi(chan, params, ctl_phy_reg);
2049 struct io_setting *io;
2050 u32 reg_value;
2051
2052 io = lpddr4_get_io_settings(params, mr5);
2053
2054 reg_value = io->ca_vref;
2055
YouMin Chende57fbf2019-11-15 11:04:46 +08002056 switch (ctl_fn) {
Jagan Teki6ea82692019-07-16 17:27:40 +05302057 case 0:
2058 clrsetbits_le32(&denali_ctl[140], 0xFFFF << 16,
2059 reg_value << 16);
2060 clrsetbits_le32(&denali_ctl[154], 0xFFFF << 16,
2061 reg_value << 16);
2062
2063 clrsetbits_le32(&denali_pi[132], 0xFF << 8, reg_value << 8);
2064 clrsetbits_le32(&denali_pi[139], 0xFF << 24, reg_value << 24);
2065 clrsetbits_le32(&denali_pi[147], 0xFF << 8, reg_value << 8);
2066 clrsetbits_le32(&denali_pi[154], 0xFF << 24, reg_value << 24);
2067 break;
2068 case 1:
2069 clrsetbits_le32(&denali_ctl[141], 0xFFFF, reg_value);
2070 clrsetbits_le32(&denali_ctl[155], 0xFFFF, reg_value);
2071
2072 clrsetbits_le32(&denali_pi[129], 0xFF << 24, reg_value << 24);
2073 clrsetbits_le32(&denali_pi[137], 0xFF << 8, reg_value << 8);
2074 clrsetbits_le32(&denali_pi[144], 0xFF << 24, reg_value << 24);
2075 clrsetbits_le32(&denali_pi[152], 0xFF << 8, reg_value << 8);
2076 break;
2077 case 2:
2078 default:
2079 clrsetbits_le32(&denali_ctl[141], 0xFFFF << 16,
2080 reg_value << 16);
2081 clrsetbits_le32(&denali_ctl[155], 0xFFFF << 16,
2082 reg_value << 16);
2083
2084 clrsetbits_le32(&denali_pi[127], 0xFF << 8, reg_value << 8);
2085 clrsetbits_le32(&denali_pi[134], 0xFF << 24, reg_value << 24);
2086 clrsetbits_le32(&denali_pi[142], 0xFF << 8, reg_value << 8);
2087 clrsetbits_le32(&denali_pi[149], 0xFF << 24, reg_value << 24);
2088 break;
2089 }
2090}
2091
2092static void set_lpddr4_MR14(const struct chan_info *chan,
YouMin Chende57fbf2019-11-15 11:04:46 +08002093 struct rk3399_sdram_params *params, u32 ctl_fn,
Jagan Teki6ea82692019-07-16 17:27:40 +05302094 bool ctl_phy_reg, u32 mr5)
2095{
2096 u32 *denali_ctl = get_denali_ctl(chan, params, ctl_phy_reg);
2097 u32 *denali_pi = get_denali_pi(chan, params, ctl_phy_reg);
2098 struct io_setting *io;
2099 u32 reg_value;
2100
2101 io = lpddr4_get_io_settings(params, mr5);
2102
2103 reg_value = io->dq_vref;
2104
YouMin Chende57fbf2019-11-15 11:04:46 +08002105 switch (ctl_fn) {
Jagan Teki6ea82692019-07-16 17:27:40 +05302106 case 0:
2107 clrsetbits_le32(&denali_ctl[142], 0xFFFF << 16,
2108 reg_value << 16);
2109 clrsetbits_le32(&denali_ctl[156], 0xFFFF << 16,
2110 reg_value << 16);
2111
2112 clrsetbits_le32(&denali_pi[132], 0xFF << 16, reg_value << 16);
2113 clrsetbits_le32(&denali_pi[140], 0xFF << 0, reg_value << 0);
2114 clrsetbits_le32(&denali_pi[147], 0xFF << 16, reg_value << 16);
2115 clrsetbits_le32(&denali_pi[155], 0xFF << 0, reg_value << 0);
2116 break;
2117 case 1:
2118 clrsetbits_le32(&denali_ctl[143], 0xFFFF, reg_value);
2119 clrsetbits_le32(&denali_ctl[157], 0xFFFF, reg_value);
2120
2121 clrsetbits_le32(&denali_pi[130], 0xFF << 0, reg_value << 0);
2122 clrsetbits_le32(&denali_pi[137], 0xFF << 16, reg_value << 16);
2123 clrsetbits_le32(&denali_pi[145], 0xFF << 0, reg_value << 0);
2124 clrsetbits_le32(&denali_pi[152], 0xFF << 16, reg_value << 16);
2125 break;
2126 case 2:
2127 default:
2128 clrsetbits_le32(&denali_ctl[143], 0xFFFF << 16,
2129 reg_value << 16);
2130 clrsetbits_le32(&denali_ctl[157], 0xFFFF << 16,
2131 reg_value << 16);
2132
2133 clrsetbits_le32(&denali_pi[127], 0xFF << 16, reg_value << 16);
2134 clrsetbits_le32(&denali_pi[135], 0xFF << 0, reg_value << 0);
2135 clrsetbits_le32(&denali_pi[142], 0xFF << 16, reg_value << 16);
2136 clrsetbits_le32(&denali_pi[150], 0xFF << 0, reg_value << 0);
2137 break;
2138 }
2139}
2140
YouMin Chen99027372019-11-15 11:04:48 +08002141void lpddr4_modify_param(const struct chan_info *chan,
2142 struct rk3399_sdram_params *params)
2143{
2144 struct rk3399_sdram_params *params_cfg;
2145 u32 *denali_ctl_params;
2146 u32 *denali_pi_params;
2147 u32 *denali_phy_params;
2148
2149 denali_ctl_params = params->pctl_regs.denali_ctl;
2150 denali_pi_params = params->pi_regs.denali_pi;
2151 denali_phy_params = params->phy_regs.denali_phy;
2152
2153 set_lpddr4_dq_odt(chan, params, 2, true, false, 0);
2154 set_lpddr4_ca_odt(chan, params, 2, true, false, 0);
2155 set_lpddr4_MR3(chan, params, 2, false, 0);
2156 set_lpddr4_MR12(chan, params, 2, false, 0);
2157 set_lpddr4_MR14(chan, params, 2, false, 0);
2158 params_cfg = lpddr4_get_phy_index_params(0, params);
2159 set_ds_odt(chan, params_cfg, false, 0);
2160 /* read two cycle preamble */
2161 clrsetbits_le32(&denali_ctl_params[200], 0x3 << 24, 0x3 << 24);
2162 clrsetbits_le32(&denali_phy_params[7], 0x3 << 24, 0x3 << 24);
2163 clrsetbits_le32(&denali_phy_params[135], 0x3 << 24, 0x3 << 24);
2164 clrsetbits_le32(&denali_phy_params[263], 0x3 << 24, 0x3 << 24);
2165 clrsetbits_le32(&denali_phy_params[391], 0x3 << 24, 0x3 << 24);
2166
2167 /* boot frequency two cycle preamble */
2168 clrsetbits_le32(&denali_phy_params[2], 0x3 << 16, 0x3 << 16);
2169 clrsetbits_le32(&denali_phy_params[130], 0x3 << 16, 0x3 << 16);
2170 clrsetbits_le32(&denali_phy_params[258], 0x3 << 16, 0x3 << 16);
2171 clrsetbits_le32(&denali_phy_params[386], 0x3 << 16, 0x3 << 16);
2172
2173 clrsetbits_le32(&denali_pi_params[45], 0x3 << 8, 0x3 << 8);
2174 clrsetbits_le32(&denali_pi_params[58], 0x1, 0x1);
2175
2176 /*
2177 * bypass mode need PHY_SLICE_PWR_RDC_DISABLE_x = 1,
2178 * boot frequency mode use bypass mode
2179 */
2180 setbits_le32(&denali_phy_params[10], 1 << 16);
2181 setbits_le32(&denali_phy_params[138], 1 << 16);
2182 setbits_le32(&denali_phy_params[266], 1 << 16);
2183 setbits_le32(&denali_phy_params[394], 1 << 16);
2184
2185 clrsetbits_le32(&denali_pi_params[45], 0x1 << 24, 0x1 << 24);
2186 clrsetbits_le32(&denali_pi_params[61], 0x1 << 24, 0x1 << 24);
2187 clrsetbits_le32(&denali_pi_params[76], 0x1 << 24, 0x1 << 24);
2188 clrsetbits_le32(&denali_pi_params[77], 0x1, 0x1);
2189}
2190
Jagan Teki6ea82692019-07-16 17:27:40 +05302191static void lpddr4_copy_phy(struct dram_info *dram,
YouMin Chende57fbf2019-11-15 11:04:46 +08002192 struct rk3399_sdram_params *params, u32 phy_fn,
2193 struct rk3399_sdram_params *params_cfg,
Jagan Teki6ea82692019-07-16 17:27:40 +05302194 u32 channel)
2195{
2196 u32 *denali_ctl, *denali_phy;
2197 u32 *denali_phy_params;
2198 u32 speed = 0;
YouMin Chende57fbf2019-11-15 11:04:46 +08002199 u32 ctl_fn, mr5;
Jagan Teki6ea82692019-07-16 17:27:40 +05302200
2201 denali_ctl = dram->chan[channel].pctl->denali_ctl;
2202 denali_phy = dram->chan[channel].publ->denali_phy;
YouMin Chende57fbf2019-11-15 11:04:46 +08002203 denali_phy_params = params_cfg->phy_regs.denali_phy;
Jagan Teki6ea82692019-07-16 17:27:40 +05302204
2205 /* switch index */
YouMin Chende57fbf2019-11-15 11:04:46 +08002206 clrsetbits_le32(&denali_phy_params[896], 0x3 << 8,
2207 phy_fn << 8);
Jagan Teki6ea82692019-07-16 17:27:40 +05302208 writel(denali_phy_params[896], &denali_phy[896]);
2209
2210 /* phy_pll_ctrl_ca, phy_pll_ctrl */
2211 writel(denali_phy_params[911], &denali_phy[911]);
2212
2213 /* phy_low_freq_sel */
2214 clrsetbits_le32(&denali_phy[913], 0x1,
2215 denali_phy_params[913] & 0x1);
2216
2217 /* phy_grp_slave_delay_x, phy_cslvl_dly_step */
2218 writel(denali_phy_params[916], &denali_phy[916]);
2219 writel(denali_phy_params[917], &denali_phy[917]);
2220 writel(denali_phy_params[918], &denali_phy[918]);
2221
2222 /* phy_adrz_sw_wraddr_shift_x */
2223 writel(denali_phy_params[512], &denali_phy[512]);
2224 clrsetbits_le32(&denali_phy[513], 0xffff,
2225 denali_phy_params[513] & 0xffff);
2226 writel(denali_phy_params[640], &denali_phy[640]);
2227 clrsetbits_le32(&denali_phy[641], 0xffff,
2228 denali_phy_params[641] & 0xffff);
2229 writel(denali_phy_params[768], &denali_phy[768]);
2230 clrsetbits_le32(&denali_phy[769], 0xffff,
2231 denali_phy_params[769] & 0xffff);
2232
2233 writel(denali_phy_params[544], &denali_phy[544]);
2234 writel(denali_phy_params[545], &denali_phy[545]);
2235 writel(denali_phy_params[546], &denali_phy[546]);
2236 writel(denali_phy_params[547], &denali_phy[547]);
2237
2238 writel(denali_phy_params[672], &denali_phy[672]);
2239 writel(denali_phy_params[673], &denali_phy[673]);
2240 writel(denali_phy_params[674], &denali_phy[674]);
2241 writel(denali_phy_params[675], &denali_phy[675]);
2242
2243 writel(denali_phy_params[800], &denali_phy[800]);
2244 writel(denali_phy_params[801], &denali_phy[801]);
2245 writel(denali_phy_params[802], &denali_phy[802]);
2246 writel(denali_phy_params[803], &denali_phy[803]);
2247
2248 /*
2249 * phy_adr_master_delay_start_x
2250 * phy_adr_master_delay_step_x
2251 * phy_adr_master_delay_wait_x
2252 */
2253 writel(denali_phy_params[548], &denali_phy[548]);
2254 writel(denali_phy_params[676], &denali_phy[676]);
2255 writel(denali_phy_params[804], &denali_phy[804]);
2256
2257 /* phy_adr_calvl_dly_step_x */
2258 writel(denali_phy_params[549], &denali_phy[549]);
2259 writel(denali_phy_params[677], &denali_phy[677]);
2260 writel(denali_phy_params[805], &denali_phy[805]);
2261
2262 /*
2263 * phy_clk_wrdm_slave_delay_x
2264 * phy_clk_wrdqz_slave_delay_x
2265 * phy_clk_wrdqs_slave_delay_x
2266 */
YouMin Chen23ae72e2019-11-15 11:04:45 +08002267 sdram_copy_to_reg((u32 *)&denali_phy[59],
2268 (u32 *)&denali_phy_params[59], (63 - 58) * 4);
2269 sdram_copy_to_reg((u32 *)&denali_phy[187],
2270 (u32 *)&denali_phy_params[187], (191 - 186) * 4);
2271 sdram_copy_to_reg((u32 *)&denali_phy[315],
2272 (u32 *)&denali_phy_params[315], (319 - 314) * 4);
2273 sdram_copy_to_reg((u32 *)&denali_phy[443],
2274 (u32 *)&denali_phy_params[443], (447 - 442) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302275
2276 /*
2277 * phy_dqs_tsel_wr_timing_x 8bits denali_phy_84/212/340/468 offset_8
2278 * dqs_tsel_wr_end[7:4] add half cycle
2279 * phy_dq_tsel_wr_timing_x 8bits denali_phy_83/211/339/467 offset_8
2280 * dq_tsel_wr_end[7:4] add half cycle
2281 */
2282 writel(denali_phy_params[83] + (0x10 << 16), &denali_phy[83]);
2283 writel(denali_phy_params[84] + (0x10 << 8), &denali_phy[84]);
2284 writel(denali_phy_params[85], &denali_phy[85]);
2285
2286 writel(denali_phy_params[211] + (0x10 << 16), &denali_phy[211]);
2287 writel(denali_phy_params[212] + (0x10 << 8), &denali_phy[212]);
2288 writel(denali_phy_params[213], &denali_phy[213]);
2289
2290 writel(denali_phy_params[339] + (0x10 << 16), &denali_phy[339]);
2291 writel(denali_phy_params[340] + (0x10 << 8), &denali_phy[340]);
2292 writel(denali_phy_params[341], &denali_phy[341]);
2293
2294 writel(denali_phy_params[467] + (0x10 << 16), &denali_phy[467]);
2295 writel(denali_phy_params[468] + (0x10 << 8), &denali_phy[468]);
2296 writel(denali_phy_params[469], &denali_phy[469]);
2297
2298 /*
2299 * phy_gtlvl_resp_wait_cnt_x
2300 * phy_gtlvl_dly_step_x
2301 * phy_wrlvl_resp_wait_cnt_x
2302 * phy_gtlvl_final_step_x
2303 * phy_gtlvl_back_step_x
2304 * phy_rdlvl_dly_step_x
2305 *
2306 * phy_master_delay_step_x
2307 * phy_master_delay_wait_x
2308 * phy_wrlvl_dly_step_x
2309 * phy_rptr_update_x
2310 * phy_wdqlvl_dly_step_x
2311 */
2312 writel(denali_phy_params[87], &denali_phy[87]);
2313 writel(denali_phy_params[88], &denali_phy[88]);
2314 writel(denali_phy_params[89], &denali_phy[89]);
2315 writel(denali_phy_params[90], &denali_phy[90]);
2316
2317 writel(denali_phy_params[215], &denali_phy[215]);
2318 writel(denali_phy_params[216], &denali_phy[216]);
2319 writel(denali_phy_params[217], &denali_phy[217]);
2320 writel(denali_phy_params[218], &denali_phy[218]);
2321
2322 writel(denali_phy_params[343], &denali_phy[343]);
2323 writel(denali_phy_params[344], &denali_phy[344]);
2324 writel(denali_phy_params[345], &denali_phy[345]);
2325 writel(denali_phy_params[346], &denali_phy[346]);
2326
2327 writel(denali_phy_params[471], &denali_phy[471]);
2328 writel(denali_phy_params[472], &denali_phy[472]);
2329 writel(denali_phy_params[473], &denali_phy[473]);
2330 writel(denali_phy_params[474], &denali_phy[474]);
2331
2332 /*
2333 * phy_gtlvl_lat_adj_start_x
2334 * phy_gtlvl_rddqs_slv_dly_start_x
2335 * phy_rdlvl_rddqs_dq_slv_dly_start_x
2336 * phy_wdqlvl_dqdm_slv_dly_start_x
2337 */
2338 writel(denali_phy_params[80], &denali_phy[80]);
2339 writel(denali_phy_params[81], &denali_phy[81]);
2340
2341 writel(denali_phy_params[208], &denali_phy[208]);
2342 writel(denali_phy_params[209], &denali_phy[209]);
2343
2344 writel(denali_phy_params[336], &denali_phy[336]);
2345 writel(denali_phy_params[337], &denali_phy[337]);
2346
2347 writel(denali_phy_params[464], &denali_phy[464]);
2348 writel(denali_phy_params[465], &denali_phy[465]);
2349
2350 /*
2351 * phy_master_delay_start_x
2352 * phy_sw_master_mode_x
2353 * phy_rddata_en_tsel_dly_x
2354 */
2355 writel(denali_phy_params[86], &denali_phy[86]);
2356 writel(denali_phy_params[214], &denali_phy[214]);
2357 writel(denali_phy_params[342], &denali_phy[342]);
2358 writel(denali_phy_params[470], &denali_phy[470]);
2359
2360 /*
2361 * phy_rddqz_slave_delay_x
2362 * phy_rddqs_dqz_fall_slave_delay_x
2363 * phy_rddqs_dqz_rise_slave_delay_x
2364 * phy_rddqs_dm_fall_slave_delay_x
2365 * phy_rddqs_dm_rise_slave_delay_x
2366 * phy_rddqs_gate_slave_delay_x
2367 * phy_wrlvl_delay_early_threshold_x
2368 * phy_write_path_lat_add_x
2369 * phy_rddqs_latency_adjust_x
2370 * phy_wrlvl_delay_period_threshold_x
2371 * phy_wrlvl_early_force_zero_x
2372 */
YouMin Chen23ae72e2019-11-15 11:04:45 +08002373 sdram_copy_to_reg((u32 *)&denali_phy[64],
2374 (u32 *)&denali_phy_params[64], (67 - 63) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302375 clrsetbits_le32(&denali_phy[68], 0xfffffc00,
2376 denali_phy_params[68] & 0xfffffc00);
YouMin Chen23ae72e2019-11-15 11:04:45 +08002377 sdram_copy_to_reg((u32 *)&denali_phy[69],
2378 (u32 *)&denali_phy_params[69], (79 - 68) * 4);
2379 sdram_copy_to_reg((u32 *)&denali_phy[192],
2380 (u32 *)&denali_phy_params[192], (195 - 191) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302381 clrsetbits_le32(&denali_phy[196], 0xfffffc00,
2382 denali_phy_params[196] & 0xfffffc00);
YouMin Chen23ae72e2019-11-15 11:04:45 +08002383 sdram_copy_to_reg((u32 *)&denali_phy[197],
2384 (u32 *)&denali_phy_params[197], (207 - 196) * 4);
2385 sdram_copy_to_reg((u32 *)&denali_phy[320],
2386 (u32 *)&denali_phy_params[320], (323 - 319) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302387 clrsetbits_le32(&denali_phy[324], 0xfffffc00,
2388 denali_phy_params[324] & 0xfffffc00);
YouMin Chen23ae72e2019-11-15 11:04:45 +08002389 sdram_copy_to_reg((u32 *)&denali_phy[325],
2390 (u32 *)&denali_phy_params[325], (335 - 324) * 4);
2391 sdram_copy_to_reg((u32 *)&denali_phy[448],
2392 (u32 *)&denali_phy_params[448], (451 - 447) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302393 clrsetbits_le32(&denali_phy[452], 0xfffffc00,
2394 denali_phy_params[452] & 0xfffffc00);
YouMin Chen23ae72e2019-11-15 11:04:45 +08002395 sdram_copy_to_reg((u32 *)&denali_phy[453],
2396 (u32 *)&denali_phy_params[453], (463 - 452) * 4);
Jagan Teki6ea82692019-07-16 17:27:40 +05302397
2398 /* phy_two_cyc_preamble_x */
2399 clrsetbits_le32(&denali_phy[7], 0x3 << 24,
2400 denali_phy_params[7] & (0x3 << 24));
2401 clrsetbits_le32(&denali_phy[135], 0x3 << 24,
2402 denali_phy_params[135] & (0x3 << 24));
2403 clrsetbits_le32(&denali_phy[263], 0x3 << 24,
2404 denali_phy_params[263] & (0x3 << 24));
2405 clrsetbits_le32(&denali_phy[391], 0x3 << 24,
2406 denali_phy_params[391] & (0x3 << 24));
2407
2408 /* speed */
YouMin Chende57fbf2019-11-15 11:04:46 +08002409 if (params_cfg->base.ddr_freq < 400)
Jagan Teki6ea82692019-07-16 17:27:40 +05302410 speed = 0x0;
YouMin Chende57fbf2019-11-15 11:04:46 +08002411 else if (params_cfg->base.ddr_freq < 800)
Jagan Teki6ea82692019-07-16 17:27:40 +05302412 speed = 0x1;
YouMin Chende57fbf2019-11-15 11:04:46 +08002413 else if (params_cfg->base.ddr_freq < 1200)
Jagan Teki6ea82692019-07-16 17:27:40 +05302414 speed = 0x2;
2415
2416 /* phy_924 phy_pad_fdbk_drive */
2417 clrsetbits_le32(&denali_phy[924], 0x3 << 21, speed << 21);
2418 /* phy_926 phy_pad_data_drive */
2419 clrsetbits_le32(&denali_phy[926], 0x3 << 9, speed << 9);
2420 /* phy_927 phy_pad_dqs_drive */
2421 clrsetbits_le32(&denali_phy[927], 0x3 << 9, speed << 9);
2422 /* phy_928 phy_pad_addr_drive */
2423 clrsetbits_le32(&denali_phy[928], 0x3 << 17, speed << 17);
2424 /* phy_929 phy_pad_clk_drive */
2425 clrsetbits_le32(&denali_phy[929], 0x3 << 17, speed << 17);
2426 /* phy_935 phy_pad_cke_drive */
2427 clrsetbits_le32(&denali_phy[935], 0x3 << 17, speed << 17);
2428 /* phy_937 phy_pad_rst_drive */
2429 clrsetbits_le32(&denali_phy[937], 0x3 << 17, speed << 17);
2430 /* phy_939 phy_pad_cs_drive */
2431 clrsetbits_le32(&denali_phy[939], 0x3 << 17, speed << 17);
2432
YouMin Chen99027372019-11-15 11:04:48 +08002433 if (params_cfg->base.dramtype == LPDDR4) {
2434 read_mr(dram->chan[channel].pctl, 1, 5, &mr5);
2435 set_ds_odt(&dram->chan[channel], params_cfg, true, mr5);
Jagan Teki6ea82692019-07-16 17:27:40 +05302436
YouMin Chen99027372019-11-15 11:04:48 +08002437 ctl_fn = lpddr4_get_ctl_fn(params_cfg, phy_fn);
2438 set_lpddr4_dq_odt(&dram->chan[channel], params_cfg,
2439 ctl_fn, true, true, mr5);
2440 set_lpddr4_ca_odt(&dram->chan[channel], params_cfg,
2441 ctl_fn, true, true, mr5);
2442 set_lpddr4_MR3(&dram->chan[channel], params_cfg,
2443 ctl_fn, true, mr5);
2444 set_lpddr4_MR12(&dram->chan[channel], params_cfg,
2445 ctl_fn, true, mr5);
2446 set_lpddr4_MR14(&dram->chan[channel], params_cfg,
2447 ctl_fn, true, mr5);
Jagan Teki6ea82692019-07-16 17:27:40 +05302448
YouMin Chen99027372019-11-15 11:04:48 +08002449 /*
2450 * if phy_sw_master_mode_x not bypass mode,
2451 * clear phy_slice_pwr_rdc_disable.
2452 * note: need use timings, not ddr_publ_regs
2453 */
2454 if (!((denali_phy_params[86] >> 8) & (1 << 2))) {
2455 clrbits_le32(&denali_phy[10], 1 << 16);
2456 clrbits_le32(&denali_phy[138], 1 << 16);
2457 clrbits_le32(&denali_phy[266], 1 << 16);
2458 clrbits_le32(&denali_phy[394], 1 << 16);
2459 }
Jagan Teki6ea82692019-07-16 17:27:40 +05302460
YouMin Chen99027372019-11-15 11:04:48 +08002461 /*
2462 * when PHY_PER_CS_TRAINING_EN=1, W2W_DIFFCS_DLY_Fx can't
2463 * smaller than 8
2464 * NOTE: need use timings, not ddr_publ_regs
2465 */
2466 if ((denali_phy_params[84] >> 16) & 1) {
2467 if (((readl(&denali_ctl[217 + ctl_fn]) >>
2468 16) & 0x1f) < 8)
2469 clrsetbits_le32(&denali_ctl[217 + ctl_fn],
2470 0x1f << 16,
2471 8 << 16);
2472 }
Jagan Teki6ea82692019-07-16 17:27:40 +05302473 }
2474}
2475
2476static void lpddr4_set_phy(struct dram_info *dram,
YouMin Chende57fbf2019-11-15 11:04:46 +08002477 struct rk3399_sdram_params *params, u32 phy_fn,
2478 struct rk3399_sdram_params *params_cfg)
Jagan Teki6ea82692019-07-16 17:27:40 +05302479{
2480 u32 channel;
2481
2482 for (channel = 0; channel < 2; channel++)
YouMin Chende57fbf2019-11-15 11:04:46 +08002483 lpddr4_copy_phy(dram, params, phy_fn, params_cfg,
2484 channel);
Jagan Teki6ea82692019-07-16 17:27:40 +05302485}
2486
2487static int lpddr4_set_ctl(struct dram_info *dram,
YouMin Chende57fbf2019-11-15 11:04:46 +08002488 struct rk3399_sdram_params *params,
2489 u32 fn, u32 hz)
Jagan Teki6ea82692019-07-16 17:27:40 +05302490{
2491 u32 channel;
2492 int ret_clk, ret;
2493
2494 /* cci idle req stall */
2495 writel(0x70007, &dram->grf->soc_con0);
2496
2497 /* enable all clk */
2498 setbits_le32(&dram->pmu->pmu_noc_auto_ena, (0x3 << 7));
2499
2500 /* idle */
2501 setbits_le32(&dram->pmu->pmu_bus_idle_req, (0x3 << 18));
2502 while ((readl(&dram->pmu->pmu_bus_idle_st) & (0x3 << 18))
2503 != (0x3 << 18))
2504 ;
2505
2506 /* change freq */
2507 writel((((0x3 << 4) | (1 << 2) | 1) << 16) |
YouMin Chende57fbf2019-11-15 11:04:46 +08002508 (fn << 4) | (1 << 2) | 1, &dram->cic->cic_ctrl0);
Jagan Teki6ea82692019-07-16 17:27:40 +05302509 while (!(readl(&dram->cic->cic_status0) & (1 << 2)))
2510 ;
2511
2512 ret_clk = clk_set_rate(&dram->ddr_clk, hz);
2513 if (ret_clk < 0) {
2514 printf("%s clk set failed %d\n", __func__, ret_clk);
2515 return ret_clk;
2516 }
2517
2518 writel(0x20002, &dram->cic->cic_ctrl0);
2519 while (!(readl(&dram->cic->cic_status0) & (1 << 0)))
2520 ;
2521
2522 /* deidle */
2523 clrbits_le32(&dram->pmu->pmu_bus_idle_req, (0x3 << 18));
2524 while (readl(&dram->pmu->pmu_bus_idle_st) & (0x3 << 18))
2525 ;
2526
2527 /* clear enable all clk */
2528 clrbits_le32(&dram->pmu->pmu_noc_auto_ena, (0x3 << 7));
2529
2530 /* lpddr4 ctl2 can not do training, all training will fail */
YouMin Chende57fbf2019-11-15 11:04:46 +08002531 if (!(params->base.dramtype == LPDDR4 && fn == 2)) {
Jagan Teki6ea82692019-07-16 17:27:40 +05302532 for (channel = 0; channel < 2; channel++) {
2533 if (!(params->ch[channel].cap_info.col))
2534 continue;
2535 ret = data_training(dram, channel, params,
YouMin Chende57fbf2019-11-15 11:04:46 +08002536 PI_FULL_TRAINING);
Jagan Teki6ea82692019-07-16 17:27:40 +05302537 if (ret)
2538 printf("%s: channel %d training failed!\n",
2539 __func__, channel);
2540 else
2541 debug("%s: channel %d training pass\n",
2542 __func__, channel);
2543 }
2544 }
2545
2546 return 0;
2547}
2548
2549static int lpddr4_set_rate(struct dram_info *dram,
Lee Jones29cbb302022-08-11 08:58:48 +01002550 struct rk3399_sdram_params *params,
2551 u32 ctl_fn)
Jagan Teki6ea82692019-07-16 17:27:40 +05302552{
YouMin Chende57fbf2019-11-15 11:04:46 +08002553 u32 phy_fn;
Jagan Teki6ea82692019-07-16 17:27:40 +05302554
Lee Jones29cbb302022-08-11 08:58:48 +01002555 phy_fn = lpddr4_get_phy_fn(params, ctl_fn);
Jagan Teki6ea82692019-07-16 17:27:40 +05302556
Lee Jones29cbb302022-08-11 08:58:48 +01002557 lpddr4_set_phy(dram, params, phy_fn, &dfs_cfgs_lpddr4[ctl_fn]);
2558 lpddr4_set_ctl(dram, params, ctl_fn,
2559 dfs_cfgs_lpddr4[ctl_fn].base.ddr_freq);
Jagan Teki6ea82692019-07-16 17:27:40 +05302560
Lee Jones29cbb302022-08-11 08:58:48 +01002561 if (IS_ENABLED(CONFIG_RAM_ROCKCHIP_DEBUG))
2562 printf("%s: change freq to %dMHz %d, %d\n", __func__,
2563 dfs_cfgs_lpddr4[ctl_fn].base.ddr_freq / MHz,
2564 ctl_fn, phy_fn);
Jagan Teki6ea82692019-07-16 17:27:40 +05302565
2566 return 0;
2567}
Jagan Teki2da26d72022-12-14 23:20:48 +05302568#endif /* CONFIG_RAM_ROCKCHIP_LPDDR4 */
Jagan Tekicc117bb2019-07-16 17:27:31 +05302569
YouMin Chen6ba388f2019-11-15 11:04:49 +08002570/* CS0,n=1
2571 * CS1,n=2
2572 * CS0 & CS1, n=3
2573 * cs0_cap: MB unit
2574 */
2575static void dram_set_cs(const struct chan_info *chan, u32 cs_map, u32 cs0_cap,
2576 unsigned char dramtype)
2577{
2578 u32 *denali_ctl = chan->pctl->denali_ctl;
2579 u32 *denali_pi = chan->pi->denali_pi;
2580 struct msch_regs *ddr_msch_regs = chan->msch;
2581
2582 clrsetbits_le32(&denali_ctl[196], 0x3, cs_map);
2583 writel((cs0_cap / 32) | (((4096 - cs0_cap) / 32) << 8),
2584 &ddr_msch_regs->ddrsize);
2585 if (dramtype == LPDDR4) {
2586 if (cs_map == 1)
2587 cs_map = 0x5;
2588 else if (cs_map == 2)
2589 cs_map = 0xa;
2590 else
2591 cs_map = 0xF;
2592 }
2593 /*PI_41 PI_CS_MAP:RW:24:4*/
2594 clrsetbits_le32(&denali_pi[41],
2595 0xf << 24, cs_map << 24);
2596 if (cs_map == 1 && dramtype == DDR3)
2597 writel(0x2EC7FFFF, &denali_pi[34]);
2598}
2599
2600static void dram_set_bw(const struct chan_info *chan, u32 bw)
2601{
2602 u32 *denali_ctl = chan->pctl->denali_ctl;
2603
2604 if (bw == 2)
2605 clrbits_le32(&denali_ctl[196], 1 << 16);
2606 else
2607 setbits_le32(&denali_ctl[196], 1 << 16);
2608}
2609
2610static void dram_set_max_col(const struct chan_info *chan, u32 bw, u32 *pcol)
2611{
2612 u32 *denali_ctl = chan->pctl->denali_ctl;
2613 struct msch_regs *ddr_msch_regs = chan->msch;
2614 u32 *denali_pi = chan->pi->denali_pi;
2615 u32 ddrconfig;
2616
2617 clrbits_le32(&denali_ctl[191], 0xf);
2618 clrsetbits_le32(&denali_ctl[190],
2619 (7 << 24),
2620 ((16 - ((bw == 2) ? 14 : 15)) << 24));
2621 /*PI_199 PI_COL_DIFF:RW:0:4*/
2622 clrbits_le32(&denali_pi[199], 0xf);
2623 /*PI_155 PI_ROW_DIFF:RW:24:3*/
2624 clrsetbits_le32(&denali_pi[155],
2625 (7 << 24),
2626 ((16 - 12) << 24));
2627 ddrconfig = (bw == 2) ? 3 : 2;
2628 writel(ddrconfig | (ddrconfig << 8), &ddr_msch_regs->ddrconf);
2629 /* set max cs0 size */
2630 writel((4096 / 32) | ((0 / 32) << 8),
2631 &ddr_msch_regs->ddrsize);
2632
2633 *pcol = 12;
2634}
2635
2636static void dram_set_max_bank(const struct chan_info *chan, u32 bw, u32 *pbank,
2637 u32 *pcol)
2638{
2639 u32 *denali_ctl = chan->pctl->denali_ctl;
2640 u32 *denali_pi = chan->pi->denali_pi;
2641
2642 clrbits_le32(&denali_ctl[191], 0xf);
2643 clrbits_le32(&denali_ctl[190], (3 << 16));
2644 /*PI_199 PI_COL_DIFF:RW:0:4*/
2645 clrbits_le32(&denali_pi[199], 0xf);
2646 /*PI_155 PI_BANK_DIFF:RW:16:2*/
2647 clrbits_le32(&denali_pi[155], (3 << 16));
2648
2649 *pbank = 3;
2650 *pcol = 12;
2651}
2652
2653static void dram_set_max_row(const struct chan_info *chan, u32 bw, u32 *prow,
2654 u32 *pbank, u32 *pcol)
2655{
2656 u32 *denali_ctl = chan->pctl->denali_ctl;
2657 u32 *denali_pi = chan->pi->denali_pi;
2658 struct msch_regs *ddr_msch_regs = chan->msch;
2659
2660 clrsetbits_le32(&denali_ctl[191], 0xf, 12 - 10);
2661 clrbits_le32(&denali_ctl[190],
2662 (0x3 << 16) | (0x7 << 24));
2663 /*PI_199 PI_COL_DIFF:RW:0:4*/
2664 clrsetbits_le32(&denali_pi[199], 0xf, 12 - 10);
2665 /*PI_155 PI_ROW_DIFF:RW:24:3 PI_BANK_DIFF:RW:16:2*/
2666 clrbits_le32(&denali_pi[155],
2667 (0x3 << 16) | (0x7 << 24));
2668 writel(1 | (1 << 8), &ddr_msch_regs->ddrconf);
2669 /* set max cs0 size */
2670 writel((4096 / 32) | ((0 / 32) << 8),
2671 &ddr_msch_regs->ddrsize);
2672
2673 *prow = 16;
2674 *pbank = 3;
2675 *pcol = (bw == 2) ? 10 : 11;
2676}
2677
2678static u64 dram_detect_cap(struct dram_info *dram,
2679 struct rk3399_sdram_params *params,
2680 unsigned char channel)
2681{
2682 const struct chan_info *chan = &dram->chan[channel];
2683 struct sdram_cap_info *cap_info = &params->ch[channel].cap_info;
2684 u32 bw;
2685 u32 col_tmp;
2686 u32 bk_tmp;
2687 u32 row_tmp;
2688 u32 cs0_cap;
2689 u32 training_flag;
2690 u32 ddrconfig;
2691
2692 /* detect bw */
2693 bw = 2;
2694 if (params->base.dramtype != LPDDR4) {
2695 dram_set_bw(chan, bw);
2696 cap_info->bw = bw;
2697 if (data_training(dram, channel, params,
2698 PI_READ_GATE_TRAINING)) {
2699 bw = 1;
2700 dram_set_bw(chan, 1);
2701 cap_info->bw = bw;
2702 if (data_training(dram, channel, params,
2703 PI_READ_GATE_TRAINING)) {
2704 printf("16bit error!!!\n");
2705 goto error;
2706 }
2707 }
2708 }
2709 /*
2710 * LPDDR3 CA training msut be trigger before other training.
2711 * DDR3 is not have CA training.
2712 */
2713 if (params->base.dramtype == LPDDR3)
2714 training_flag = PI_WRITE_LEVELING;
2715 else
2716 training_flag = PI_FULL_TRAINING;
2717
2718 if (params->base.dramtype != LPDDR4) {
2719 if (data_training(dram, channel, params, training_flag)) {
2720 printf("full training error!!!\n");
2721 goto error;
2722 }
2723 }
2724
2725 /* detect col */
2726 dram_set_max_col(chan, bw, &col_tmp);
2727 if (sdram_detect_col(cap_info, col_tmp) != 0)
2728 goto error;
2729
2730 /* detect bank */
2731 dram_set_max_bank(chan, bw, &bk_tmp, &col_tmp);
2732 sdram_detect_bank(cap_info, col_tmp, bk_tmp);
2733
2734 /* detect row */
2735 dram_set_max_row(chan, bw, &row_tmp, &bk_tmp, &col_tmp);
2736 if (sdram_detect_row(cap_info, col_tmp, bk_tmp, row_tmp) != 0)
2737 goto error;
2738
2739 /* detect row_3_4 */
2740 sdram_detect_row_3_4(cap_info, col_tmp, bk_tmp);
2741
2742 /* set ddrconfig */
2743 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col + cap_info->bk +
2744 cap_info->bw - 20));
2745 if (cap_info->row_3_4)
2746 cs0_cap = cs0_cap * 3 / 4;
2747
2748 cap_info->cs1_row = cap_info->cs0_row;
2749 set_memory_map(chan, channel, params);
2750 ddrconfig = calculate_ddrconfig(params, channel);
2751 if (-1 == ddrconfig)
2752 goto error;
2753 set_ddrconfig(chan, params, channel,
2754 cap_info->ddrconfig);
2755
2756 /* detect cs1 row */
2757 sdram_detect_cs1_row(cap_info, params->base.dramtype);
2758
Jonathan Liu3c9997b2023-03-23 21:35:58 +11002759 sdram_detect_high_row(cap_info);
2760
YouMin Chen6ba388f2019-11-15 11:04:49 +08002761 /* detect die bw */
2762 sdram_detect_dbw(cap_info, params->base.dramtype);
2763
2764 return 0;
2765error:
2766 return (-1);
2767}
2768
Jagan Teki2525fae2019-07-15 23:58:52 +05302769static unsigned char calculate_stride(struct rk3399_sdram_params *params)
2770{
Kever Yange2b64fd2019-11-15 11:04:52 +08002771 unsigned int gstride_type;
2772 unsigned int channel;
2773 unsigned int chinfo = 0;
2774 unsigned int cap = 0;
2775 unsigned int stride = -1;
Jagan Teki2525fae2019-07-15 23:58:52 +05302776 unsigned int ch_cap[2] = {0, 0};
Kever Yange2b64fd2019-11-15 11:04:52 +08002777
2778 gstride_type = STRIDE_256B;
Jagan Teki2525fae2019-07-15 23:58:52 +05302779
2780 for (channel = 0; channel < 2; channel++) {
2781 unsigned int cs0_cap = 0;
2782 unsigned int cs1_cap = 0;
Kever Yange2b64fd2019-11-15 11:04:52 +08002783 struct sdram_cap_info *cap_info =
2784 &params->ch[channel].cap_info;
Jagan Teki2525fae2019-07-15 23:58:52 +05302785
2786 if (cap_info->col == 0)
2787 continue;
2788
2789 cs0_cap = (1 << (cap_info->cs0_row + cap_info->col +
2790 cap_info->bk + cap_info->bw - 20));
2791 if (cap_info->rank > 1)
2792 cs1_cap = cs0_cap >> (cap_info->cs0_row
2793 - cap_info->cs1_row);
2794 if (cap_info->row_3_4) {
2795 cs0_cap = cs0_cap * 3 / 4;
2796 cs1_cap = cs1_cap * 3 / 4;
2797 }
2798 ch_cap[channel] = cs0_cap + cs1_cap;
2799 chinfo |= 1 << channel;
2800 }
2801
Kever Yange2b64fd2019-11-15 11:04:52 +08002802 cap = ch_cap[0] + ch_cap[1];
2803 if (params->base.num_channels == 1) {
2804 if (chinfo & 1) /* channel a only */
2805 stride = 0x17;
2806 else /* channel b only */
2807 stride = 0x18;
2808 } else {/* 2 channel */
2809 if (ch_cap[0] == ch_cap[1]) {
2810 /* interleaved */
2811 if (gstride_type == PART_STRIDE) {
2812 /*
2813 * first 64MB no interleaved other 256B interleaved
2814 * if 786M+768M.useful space from 0-1280MB and
2815 * 1536MB-1792MB
2816 * if 1.5G+1.5G(continuous).useful space from 0-2560MB
2817 * and 3072MB-3584MB
2818 */
2819 stride = 0x1F;
2820 } else {
2821 switch (cap) {
2822 /* 512MB */
2823 case 512:
2824 stride = 0;
2825 break;
2826 /* 1GB unstride or 256B stride*/
2827 case 1024:
2828 stride = (gstride_type == UN_STRIDE) ?
2829 0x1 : 0x5;
2830 break;
2831 /*
2832 * 768MB + 768MB same as total 2GB memory
2833 * useful space: 0-768MB 1GB-1792MB
2834 */
2835 case 1536:
2836 /* 2GB unstride or 256B or 512B stride */
2837 case 2048:
2838 stride = (gstride_type == UN_STRIDE) ?
2839 0x2 :
2840 ((gstride_type == STRIDE_512B) ?
2841 0xA : 0x9);
2842 break;
2843 /* 1536MB + 1536MB */
2844 case 3072:
2845 stride = (gstride_type == UN_STRIDE) ?
2846 0x3 :
2847 ((gstride_type == STRIDE_512B) ?
2848 0x12 : 0x11);
2849 break;
2850 /* 4GB unstride or 128B,256B,512B,4KB stride */
2851 case 4096:
2852 stride = (gstride_type == UN_STRIDE) ?
2853 0x3 : (0xC + gstride_type);
2854 break;
2855 }
2856 }
2857 }
2858 if (ch_cap[0] == 2048 && ch_cap[1] == 1024) {
2859 /* 2GB + 1GB */
2860 stride = (gstride_type == UN_STRIDE) ? 0x3 : 0x19;
2861 }
Jagan Teki2525fae2019-07-15 23:58:52 +05302862 /*
Kever Yange2b64fd2019-11-15 11:04:52 +08002863 * remain two channel capability not equal OR capability
2864 * power function of 2
Jagan Teki2525fae2019-07-15 23:58:52 +05302865 */
Kever Yange2b64fd2019-11-15 11:04:52 +08002866 if (stride == (-1)) {
2867 switch ((ch_cap[0] > ch_cap[1]) ?
2868 ch_cap[0] : ch_cap[1]) {
2869 case 256: /* 256MB + 128MB */
2870 stride = 0;
2871 break;
2872 case 512: /* 512MB + 256MB */
2873 stride = 1;
2874 break;
2875 case 1024:/* 1GB + 128MB/256MB/384MB/512MB/768MB */
2876 stride = 2;
2877 break;
2878 case 2048: /* 2GB + 128MB/256MB/384MB/512MB/768MB/1GB */
2879 stride = 3;
2880 break;
2881 default:
2882 break;
2883 }
Jagan Teki2525fae2019-07-15 23:58:52 +05302884 }
Kever Yange2b64fd2019-11-15 11:04:52 +08002885 if (stride == (-1))
2886 goto error;
2887 }
Jagan Teki2525fae2019-07-15 23:58:52 +05302888
Jagan Teki8eed4a42019-07-15 23:58:55 +05302889 sdram_print_stride(stride);
2890
Jagan Teki2525fae2019-07-15 23:58:52 +05302891 return stride;
Kever Yange2b64fd2019-11-15 11:04:52 +08002892error:
2893 printf("Cap not support!\n");
2894 return (-1);
Jagan Teki2525fae2019-07-15 23:58:52 +05302895}
2896
Jagan Teki43485e12019-07-15 23:58:54 +05302897static void clear_channel_params(struct rk3399_sdram_params *params, u8 channel)
2898{
2899 params->ch[channel].cap_info.rank = 0;
2900 params->ch[channel].cap_info.col = 0;
2901 params->ch[channel].cap_info.bk = 0;
2902 params->ch[channel].cap_info.bw = 32;
2903 params->ch[channel].cap_info.dbw = 32;
2904 params->ch[channel].cap_info.row_3_4 = 0;
2905 params->ch[channel].cap_info.cs0_row = 0;
2906 params->ch[channel].cap_info.cs1_row = 0;
2907 params->ch[channel].cap_info.ddrconfig = 0;
2908}
2909
Kever Yang50fb9982017-02-22 16:56:35 +08002910static int sdram_init(struct dram_info *dram,
Jagan Teki2525fae2019-07-15 23:58:52 +05302911 struct rk3399_sdram_params *params)
Kever Yang50fb9982017-02-22 16:56:35 +08002912{
Jagan Tekia58ff792019-07-15 23:50:58 +05302913 unsigned char dramtype = params->base.dramtype;
2914 unsigned int ddr_freq = params->base.ddr_freq;
Jagan Teki43485e12019-07-15 23:58:54 +05302915 int channel, ch, rank;
YouMin Chen6ba388f2019-11-15 11:04:49 +08002916 u32 tmp, ret;
Kever Yang50fb9982017-02-22 16:56:35 +08002917
2918 debug("Starting SDRAM initialization...\n");
2919
Philipp Tomsich39dce4a2017-05-31 18:16:35 +02002920 if ((dramtype == DDR3 && ddr_freq > 933) ||
Kever Yang50fb9982017-02-22 16:56:35 +08002921 (dramtype == LPDDR3 && ddr_freq > 933) ||
2922 (dramtype == LPDDR4 && ddr_freq > 800)) {
2923 debug("SDRAM frequency is to high!");
2924 return -E2BIG;
2925 }
2926
YouMin Chen99027372019-11-15 11:04:48 +08002927 /* detect rank */
Jagan Teki43485e12019-07-15 23:58:54 +05302928 for (ch = 0; ch < 2; ch++) {
2929 params->ch[ch].cap_info.rank = 2;
2930 for (rank = 2; rank != 0; rank--) {
YouMin Chen99027372019-11-15 11:04:48 +08002931 for (channel = 0; channel < 2; channel++) {
2932 const struct chan_info *chan =
2933 &dram->chan[channel];
Jagan Teki783acfd2020-01-09 14:22:17 +05302934 struct rockchip_cru *cru = dram->cru;
YouMin Chen99027372019-11-15 11:04:48 +08002935 struct rk3399_ddr_publ_regs *publ = chan->publ;
2936
2937 phy_pctrl_reset(cru, channel);
2938 phy_dll_bypass_set(publ, ddr_freq);
2939 pctl_cfg(dram, chan, channel, params);
Jagan Teki43485e12019-07-15 23:58:54 +05302940 }
2941
YouMin Chen99027372019-11-15 11:04:48 +08002942 /* start to trigger initialization */
2943 pctl_start(dram, params, 3);
2944
Jagan Teki43485e12019-07-15 23:58:54 +05302945 /* LPDDR2/LPDDR3 need to wait DAI complete, max 10us */
2946 if (dramtype == LPDDR3)
2947 udelay(10);
2948
YouMin Chen6ba388f2019-11-15 11:04:49 +08002949 tmp = (rank == 2) ? 3 : 1;
2950 dram_set_cs(&dram->chan[ch], tmp, 2048,
2951 params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05302952 params->ch[ch].cap_info.rank = rank;
2953
YouMin Chende57fbf2019-11-15 11:04:46 +08002954 ret = dram->ops->data_training_first(dram, ch,
2955 rank, params);
Jagan Teki9eb935a2019-07-16 17:27:30 +05302956 if (!ret) {
2957 debug("%s: data trained for rank %d, ch %d\n",
2958 __func__, rank, ch);
Jagan Teki43485e12019-07-15 23:58:54 +05302959 break;
Jagan Teki9eb935a2019-07-16 17:27:30 +05302960 }
Jagan Teki43485e12019-07-15 23:58:54 +05302961 }
2962 /* Computed rank with associated channel number */
2963 params->ch[ch].cap_info.rank = rank;
2964 }
2965
David Sebek68cfcb82023-03-30 17:51:14 -04002966#if defined(CONFIG_RAM_ROCKCHIP_LPDDR4)
Lee Jones29cbb302022-08-11 08:58:48 +01002967 /* LPDDR4 needs to be trained at 400MHz */
2968 lpddr4_set_rate(dram, params, 0);
2969 params->base.ddr_freq = dfs_cfgs_lpddr4[0].base.ddr_freq / MHz;
2970#endif
2971
Jagan Teki43485e12019-07-15 23:58:54 +05302972 params->base.num_channels = 0;
Kever Yang50fb9982017-02-22 16:56:35 +08002973 for (channel = 0; channel < 2; channel++) {
2974 const struct chan_info *chan = &dram->chan[channel];
YouMin Chen99027372019-11-15 11:04:48 +08002975 struct sdram_cap_info *cap_info =
2976 &params->ch[channel].cap_info;
Kever Yang50fb9982017-02-22 16:56:35 +08002977
Jagan Teki43485e12019-07-15 23:58:54 +05302978 if (cap_info->rank == 0) {
YouMin Chen6ba388f2019-11-15 11:04:49 +08002979 clear_channel_params(params, 1);
Kever Yang50fb9982017-02-22 16:56:35 +08002980 continue;
Kever Yang50fb9982017-02-22 16:56:35 +08002981 }
2982
Jagan Teki57bd8872020-07-14 01:36:34 +05302983 if (IS_ENABLED(CONFIG_RAM_ROCKCHIP_DEBUG)) {
2984 printf("Channel ");
2985 printf(channel ? "1: " : "0: ");
2986 }
Jagan Tekic9151e22019-07-15 23:58:45 +05302987
YouMin Chen6ba388f2019-11-15 11:04:49 +08002988 if (channel == 0)
2989 set_ddr_stride(dram->pmusgrf, 0x17);
2990 else
2991 set_ddr_stride(dram->pmusgrf, 0x18);
Kever Yang50fb9982017-02-22 16:56:35 +08002992
YouMin Chen6ba388f2019-11-15 11:04:49 +08002993 if (dram_detect_cap(dram, params, channel)) {
2994 printf("Cap error!\n");
2995 continue;
Kever Yang50fb9982017-02-22 16:56:35 +08002996 }
2997
Jagan Teki95bf5872022-12-14 23:20:49 +05302998 sdram_print_ddr_info(cap_info, &params->base, 0);
Kever Yange723a552019-08-12 20:02:29 +08002999 set_memory_map(chan, channel, params);
YouMin Chen99027372019-11-15 11:04:48 +08003000 cap_info->ddrconfig =
3001 calculate_ddrconfig(params, channel);
3002 if (-1 == cap_info->ddrconfig) {
3003 printf("no ddrconfig find, Cap not support!\n");
3004 continue;
3005 }
Han Pengfeif6a99292022-05-15 14:11:59 +08003006
3007 params->base.num_channels++;
Jagan Teki43485e12019-07-15 23:58:54 +05303008 set_ddrconfig(chan, params, channel, cap_info->ddrconfig);
Kever Yange723a552019-08-12 20:02:29 +08003009 set_cap_relate_config(chan, params, channel);
Jagan Teki43485e12019-07-15 23:58:54 +05303010 }
3011
3012 if (params->base.num_channels == 0) {
3013 printf("%s: ", __func__);
Jagan Teki8eed4a42019-07-15 23:58:55 +05303014 sdram_print_dram_type(params->base.dramtype);
Jagan Teki43485e12019-07-15 23:58:54 +05303015 printf(" - %dMHz failed!\n", params->base.ddr_freq);
3016 return -EINVAL;
Kever Yang50fb9982017-02-22 16:56:35 +08003017 }
Jagan Teki2525fae2019-07-15 23:58:52 +05303018
3019 params->base.stride = calculate_stride(params);
Jagan Tekia58ff792019-07-15 23:50:58 +05303020 dram_all_config(dram, params);
YouMin Chende57fbf2019-11-15 11:04:46 +08003021
Lee Jones29cbb302022-08-11 08:58:48 +01003022 ret = dram->ops->set_rate_index(dram, params, 1);
Lee Jonesd3cb5132022-08-11 08:58:46 +01003023 if (ret)
3024 return ret;
Kever Yang50fb9982017-02-22 16:56:35 +08003025
3026 debug("Finish SDRAM initialization...\n");
3027 return 0;
3028}
3029
Simon Glassaad29ae2020-12-03 16:55:21 -07003030static int rk3399_dmc_of_to_plat(struct udevice *dev)
Kever Yang50fb9982017-02-22 16:56:35 +08003031{
Simon Glass08d4ba42024-08-01 06:47:23 -06003032 struct rockchip_dmc_plat *plat;
Kever Yang50fb9982017-02-22 16:56:35 +08003033 int ret;
3034
Simon Glass08d4ba42024-08-01 06:47:23 -06003035 if (!CONFIG_IS_ENABLED(OF_REAL) || !phase_sdram_init())
Simon Glass6d70ba02021-08-07 07:24:06 -06003036 return 0;
3037
Simon Glass08d4ba42024-08-01 06:47:23 -06003038 plat = dev_get_plat(dev);
Philipp Tomsich0250c232017-06-07 18:46:03 +02003039 ret = dev_read_u32_array(dev, "rockchip,sdram-params",
3040 (u32 *)&plat->sdram_params,
3041 sizeof(plat->sdram_params) / sizeof(u32));
Kever Yang50fb9982017-02-22 16:56:35 +08003042 if (ret) {
3043 printf("%s: Cannot read rockchip,sdram-params %d\n",
3044 __func__, ret);
3045 return ret;
3046 }
Masahiro Yamadae4873e32018-04-19 12:14:03 +09003047 ret = regmap_init_mem(dev_ofnode(dev), &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08003048 if (ret)
3049 printf("%s: regmap failed %d\n", __func__, ret);
3050
Kever Yang50fb9982017-02-22 16:56:35 +08003051 return 0;
3052}
3053
3054#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -07003055static int conv_of_plat(struct udevice *dev)
Kever Yang50fb9982017-02-22 16:56:35 +08003056{
Simon Glassfa20e932020-12-03 16:55:20 -07003057 struct rockchip_dmc_plat *plat = dev_get_plat(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08003058 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
3059 int ret;
3060
Johan Jonker2e304a22023-03-13 01:30:46 +01003061 ret = regmap_init_mem_plat(dev, dtplat->reg, sizeof(dtplat->reg[0]),
Simon Glassb75b15b2020-12-03 16:55:23 -07003062 ARRAY_SIZE(dtplat->reg) / 2, &plat->map);
Kever Yang50fb9982017-02-22 16:56:35 +08003063 if (ret)
3064 return ret;
3065
3066 return 0;
3067}
3068#endif
3069
Jagan Teki9eb935a2019-07-16 17:27:30 +05303070static const struct sdram_rk3399_ops rk3399_ops = {
Jagan Teki2da26d72022-12-14 23:20:48 +05303071#if !defined(CONFIG_RAM_ROCKCHIP_LPDDR4)
YouMin Chende57fbf2019-11-15 11:04:46 +08003072 .data_training_first = data_training_first,
3073 .set_rate_index = switch_to_phy_index1,
YouMin Chen99027372019-11-15 11:04:48 +08003074 .modify_param = modify_param,
3075 .get_phy_index_params = get_phy_index_params,
Jagan Tekicc117bb2019-07-16 17:27:31 +05303076#else
YouMin Chende57fbf2019-11-15 11:04:46 +08003077 .data_training_first = lpddr4_mr_detect,
3078 .set_rate_index = lpddr4_set_rate,
YouMin Chen99027372019-11-15 11:04:48 +08003079 .modify_param = lpddr4_modify_param,
3080 .get_phy_index_params = lpddr4_get_phy_index_params,
Jagan Tekicc117bb2019-07-16 17:27:31 +05303081#endif
Jagan Teki9eb935a2019-07-16 17:27:30 +05303082};
3083
Kever Yang50fb9982017-02-22 16:56:35 +08003084static int rk3399_dmc_init(struct udevice *dev)
3085{
3086 struct dram_info *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07003087 struct rockchip_dmc_plat *plat = dev_get_plat(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08003088 int ret;
Simon Glass92882652021-08-07 07:24:04 -06003089#if CONFIG_IS_ENABLED(OF_REAL)
Kever Yang50fb9982017-02-22 16:56:35 +08003090 struct rk3399_sdram_params *params = &plat->sdram_params;
3091#else
3092 struct dtd_rockchip_rk3399_dmc *dtplat = &plat->dtplat;
3093 struct rk3399_sdram_params *params =
3094 (void *)dtplat->rockchip_sdram_params;
3095
Simon Glassb75b15b2020-12-03 16:55:23 -07003096 ret = conv_of_plat(dev);
Kever Yang50fb9982017-02-22 16:56:35 +08003097 if (ret)
3098 return ret;
3099#endif
3100
Jagan Teki9eb935a2019-07-16 17:27:30 +05303101 priv->ops = &rk3399_ops;
Kever Yang50fb9982017-02-22 16:56:35 +08003102 priv->cic = syscon_get_first_range(ROCKCHIP_SYSCON_CIC);
Jagan Tekic9151e22019-07-15 23:58:45 +05303103 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Jagan Teki6ea82692019-07-16 17:27:40 +05303104 priv->pmu = syscon_get_first_range(ROCKCHIP_SYSCON_PMU);
Kever Yang50fb9982017-02-22 16:56:35 +08003105 priv->pmusgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
3106 priv->pmucru = rockchip_get_pmucru();
3107 priv->cru = rockchip_get_cru();
3108 priv->chan[0].pctl = regmap_get_range(plat->map, 0);
3109 priv->chan[0].pi = regmap_get_range(plat->map, 1);
3110 priv->chan[0].publ = regmap_get_range(plat->map, 2);
3111 priv->chan[0].msch = regmap_get_range(plat->map, 3);
3112 priv->chan[1].pctl = regmap_get_range(plat->map, 4);
3113 priv->chan[1].pi = regmap_get_range(plat->map, 5);
3114 priv->chan[1].publ = regmap_get_range(plat->map, 6);
3115 priv->chan[1].msch = regmap_get_range(plat->map, 7);
3116
3117 debug("con reg %p %p %p %p %p %p %p %p\n",
3118 priv->chan[0].pctl, priv->chan[0].pi,
3119 priv->chan[0].publ, priv->chan[0].msch,
3120 priv->chan[1].pctl, priv->chan[1].pi,
3121 priv->chan[1].publ, priv->chan[1].msch);
Jagan Teki6ea82692019-07-16 17:27:40 +05303122 debug("cru %p, cic %p, grf %p, sgrf %p, pmucru %p, pmu %p\n", priv->cru,
3123 priv->cic, priv->pmugrf, priv->pmusgrf, priv->pmucru, priv->pmu);
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05303124
Kever Yang50fb9982017-02-22 16:56:35 +08003125#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1257efc2021-08-07 07:24:09 -06003126 ret = clk_get_by_phandle(dev, dtplat->clocks, &priv->ddr_clk);
Kever Yang50fb9982017-02-22 16:56:35 +08003127#else
3128 ret = clk_get_by_index(dev, 0, &priv->ddr_clk);
3129#endif
3130 if (ret) {
3131 printf("%s clk get failed %d\n", __func__, ret);
3132 return ret;
3133 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05303134
Kever Yang50fb9982017-02-22 16:56:35 +08003135 ret = clk_set_rate(&priv->ddr_clk, params->base.ddr_freq * MHz);
3136 if (ret < 0) {
3137 printf("%s clk set failed %d\n", __func__, ret);
3138 return ret;
3139 }
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05303140
Kever Yang50fb9982017-02-22 16:56:35 +08003141 ret = sdram_init(priv, params);
3142 if (ret < 0) {
Jagan Tekiacf8e0f2019-07-15 23:50:57 +05303143 printf("%s DRAM init failed %d\n", __func__, ret);
Kever Yang50fb9982017-02-22 16:56:35 +08003144 return ret;
3145 }
3146
3147 return 0;
3148}
Kever Yang50fb9982017-02-22 16:56:35 +08003149
Kever Yang50fb9982017-02-22 16:56:35 +08003150static int rk3399_dmc_probe(struct udevice *dev)
3151{
Simon Glassc5c33be2024-08-01 06:47:22 -06003152 struct dram_info *priv = dev_get_priv(dev);
3153
Simon Glass08d4ba42024-08-01 06:47:23 -06003154 priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF);
3155 debug("%s: pmugrf = %p\n", __func__, priv->pmugrf);
3156 if (phase_sdram_init() && rk3399_dmc_init(dev))
Kever Yang50fb9982017-02-22 16:56:35 +08003157 return 0;
Simon Glass08d4ba42024-08-01 06:47:23 -06003158
Simon Glassc5c33be2024-08-01 06:47:22 -06003159 /*
3160 * There is no point in checking the SDRAM size in TPL as it is not
3161 * used, so avoid the code size increment.
3162 */
3163 if (!IS_ENABLED(CONFIG_TPL_BUILD)) {
Simon Glassc5c33be2024-08-01 06:47:22 -06003164 priv->info.base = CFG_SYS_SDRAM_BASE;
3165 priv->info.size = rockchip_sdram_size(
3166 (phys_addr_t)&priv->pmugrf->os_reg2);
3167 }
3168
Kever Yang50fb9982017-02-22 16:56:35 +08003169 return 0;
3170}
3171
3172static int rk3399_dmc_get_info(struct udevice *dev, struct ram_info *info)
3173{
3174 struct dram_info *priv = dev_get_priv(dev);
3175
Kever Yangea61d142017-04-19 16:01:14 +08003176 *info = priv->info;
Kever Yang50fb9982017-02-22 16:56:35 +08003177
3178 return 0;
3179}
3180
3181static struct ram_ops rk3399_dmc_ops = {
3182 .get_info = rk3399_dmc_get_info,
3183};
3184
Kever Yang50fb9982017-02-22 16:56:35 +08003185static const struct udevice_id rk3399_dmc_ids[] = {
3186 { .compatible = "rockchip,rk3399-dmc" },
3187 { }
3188};
3189
3190U_BOOT_DRIVER(dmc_rk3399) = {
3191 .name = "rockchip_rk3399_dmc",
3192 .id = UCLASS_RAM,
3193 .of_match = rk3399_dmc_ids,
3194 .ops = &rk3399_dmc_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07003195 .of_to_plat = rk3399_dmc_of_to_plat,
Kever Yang50fb9982017-02-22 16:56:35 +08003196 .probe = rk3399_dmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07003197 .priv_auto = sizeof(struct dram_info),
Kever Yang7f347842019-04-01 17:20:53 +08003198#if defined(CONFIG_TPL_BUILD) || \
3199 (!defined(CONFIG_TPL) && defined(CONFIG_SPL_BUILD))
Simon Glass71fa5b42020-12-03 16:55:18 -07003200 .plat_auto = sizeof(struct rockchip_dmc_plat),
Kever Yang50fb9982017-02-22 16:56:35 +08003201#endif
3202};