blob: 5b8747cc681f87147747dad56d90d382cb779620 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese5ffceb82015-03-26 15:36:56 +01002/*
3 * Copyright (C) Marvell International Ltd. and its affiliates
Stefan Roese5ffceb82015-03-26 15:36:56 +01004 */
5
Stefan Roese5ffceb82015-03-26 15:36:56 +01006#include "ddr3_init.h"
Chris Packham1a07d212018-05-10 13:28:29 +12007#include "mv_ddr_common.h"
Chris Packham4bf81db2018-12-03 14:26:49 +13008#include "mv_ddr_training_db.h"
9#include "mv_ddr_regs.h"
Stefan Roese5ffceb82015-03-26 15:36:56 +010010
11#define GET_CS_FROM_MASK(mask) (cs_mask2_num[mask])
12#define CS_CBE_VALUE(cs_num) (cs_cbe_reg[cs_num])
13
14u32 window_mem_addr = 0;
15u32 phy_reg0_val = 0;
16u32 phy_reg1_val = 8;
17u32 phy_reg2_val = 0;
Chris Packham1a07d212018-05-10 13:28:29 +120018u32 phy_reg3_val = PARAM_UNDEFINED;
Chris Packham4bf81db2018-12-03 14:26:49 +130019enum mv_ddr_freq low_freq = MV_DDR_FREQ_LOW_FREQ;
20enum mv_ddr_freq medium_freq;
Stefan Roese5ffceb82015-03-26 15:36:56 +010021u32 debug_dunit = 0;
22u32 odt_additional = 1;
23u32 *dq_map_table = NULL;
Chris Packham1a07d212018-05-10 13:28:29 +120024
25/* in case of ddr4 do not run ddr3_tip_write_additional_odt_setting function - mc odt always 'on'
26 * in ddr4 case the terminations are rttWR and rttPARK and the odt must be always 'on' 0x1498 = 0xf
27 */
Tony Dinhe2c524b2023-01-18 19:03:04 -080028#if defined(CONFIG_DDR4)
29u32 odt_config = 0;
30#else
Stefan Roese5ffceb82015-03-26 15:36:56 +010031u32 odt_config = 1;
Tony Dinhe2c524b2023-01-18 19:03:04 -080032#endif
Stefan Roese5ffceb82015-03-26 15:36:56 +010033
Chris Packham1a07d212018-05-10 13:28:29 +120034u32 nominal_avs;
35u32 extension_avs;
36
Stefan Roese5ffceb82015-03-26 15:36:56 +010037u32 is_pll_before_init = 0, is_adll_calib_before_init = 1, is_dfs_in_init = 0;
Chris Packham1a07d212018-05-10 13:28:29 +120038u32 dfs_low_freq;
39
40u32 g_rtt_nom_cs0, g_rtt_nom_cs1;
Stefan Roese5ffceb82015-03-26 15:36:56 +010041u8 calibration_update_control; /* 2 external only, 1 is internal only */
42
43enum hws_result training_result[MAX_STAGE_LIMIT][MAX_INTERFACE_NUM];
44enum auto_tune_stage training_stage = INIT_CONTROLLER;
45u32 finger_test = 0, p_finger_start = 11, p_finger_end = 64,
46 n_finger_start = 11, n_finger_end = 64,
47 p_finger_step = 3, n_finger_step = 3;
48u32 clamp_tbl[] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
49
50/* Initiate to 0xff, this variable is define by user in debug mode */
Chris Packham1a07d212018-05-10 13:28:29 +120051u32 mode_2t = 0xff;
Stefan Roese5ffceb82015-03-26 15:36:56 +010052u32 xsb_validate_type = 0;
53u32 xsb_validation_base_address = 0xf000;
54u32 first_active_if = 0;
55u32 dfs_low_phy1 = 0x1f;
56u32 multicast_id = 0;
57int use_broadcast = 0;
58struct hws_tip_freq_config_info *freq_info_table = NULL;
59u8 is_cbe_required = 0;
60u32 debug_mode = 0;
61u32 delay_enable = 0;
62int rl_mid_freq_wa = 0;
63
64u32 effective_cs = 0;
65
Chris Packham1a07d212018-05-10 13:28:29 +120066u32 vref_init_val = 0x4;
67u32 ck_delay = PARAM_UNDEFINED;
68
69/* Design guidelines parameters */
70u32 g_zpri_data = PARAM_UNDEFINED; /* controller data - P drive strength */
71u32 g_znri_data = PARAM_UNDEFINED; /* controller data - N drive strength */
72u32 g_zpri_ctrl = PARAM_UNDEFINED; /* controller C/A - P drive strength */
73u32 g_znri_ctrl = PARAM_UNDEFINED; /* controller C/A - N drive strength */
74
75u32 g_zpodt_data = PARAM_UNDEFINED; /* controller data - P ODT */
76u32 g_znodt_data = PARAM_UNDEFINED; /* controller data - N ODT */
77u32 g_zpodt_ctrl = PARAM_UNDEFINED; /* controller data - P ODT */
78u32 g_znodt_ctrl = PARAM_UNDEFINED; /* controller data - N ODT */
79
80u32 g_odt_config = PARAM_UNDEFINED;
81u32 g_rtt_nom = PARAM_UNDEFINED;
82u32 g_rtt_wr = PARAM_UNDEFINED;
83u32 g_dic = PARAM_UNDEFINED;
84u32 g_rtt_park = PARAM_UNDEFINED;
85
Stefan Roese5ffceb82015-03-26 15:36:56 +010086u32 mask_tune_func = (SET_MEDIUM_FREQ_MASK_BIT |
87 WRITE_LEVELING_MASK_BIT |
88 LOAD_PATTERN_2_MASK_BIT |
89 READ_LEVELING_MASK_BIT |
Chris Packham1a07d212018-05-10 13:28:29 +120090 SET_TARGET_FREQ_MASK_BIT |
91 WRITE_LEVELING_TF_MASK_BIT |
Tony Dinhe2c524b2023-01-18 19:03:04 -080092#if defined(CONFIG_DDR4)
93 SW_READ_LEVELING_MASK_BIT |
94#else /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +010095 READ_LEVELING_TF_MASK_BIT |
Tony Dinhe2c524b2023-01-18 19:03:04 -080096#endif /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +120097 CENTRALIZATION_RX_MASK_BIT |
98 CENTRALIZATION_TX_MASK_BIT);
Stefan Roese5ffceb82015-03-26 15:36:56 +010099
100static int ddr3_tip_ddr3_training_main_flow(u32 dev_num);
101static int ddr3_tip_write_odt(u32 dev_num, enum hws_access_type access_type,
102 u32 if_id, u32 cl_value, u32 cwl_value);
103static int ddr3_tip_ddr3_auto_tune(u32 dev_num);
Chris Packham1a07d212018-05-10 13:28:29 +1200104
Stefan Roese5ffceb82015-03-26 15:36:56 +0100105#ifdef ODT_TEST_SUPPORT
106static int odt_test(u32 dev_num, enum hws_algo_type algo_type);
107#endif
108
109int adll_calibration(u32 dev_num, enum hws_access_type access_type,
Chris Packham4bf81db2018-12-03 14:26:49 +1300110 u32 if_id, enum mv_ddr_freq frequency);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100111static int ddr3_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
Chris Packham4bf81db2018-12-03 14:26:49 +1300112 u32 if_id, enum mv_ddr_freq frequency);
Tony Dinhe2c524b2023-01-18 19:03:04 -0800113#if defined(CONFIG_DDR4)
114static int ddr4_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
115 u32 if_id, enum mv_ddr_freq frequency);
116#endif /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +1200117
118static u8 mem_size_config[MV_DDR_DIE_CAP_LAST] = {
Stefan Roese5ffceb82015-03-26 15:36:56 +0100119 0x2, /* 512Mbit */
120 0x3, /* 1Gbit */
121 0x0, /* 2Gbit */
122 0x4, /* 4Gbit */
Chris Packham1a07d212018-05-10 13:28:29 +1200123 0x5, /* 8Gbit */
124 0x0, /* TODO: placeholder for 16-Mbit die capacity */
125 0x0, /* TODO: placeholder for 32-Mbit die capacity */
126 0x0, /* TODO: placeholder for 12-Mbit die capacity */
127 0x0 /* TODO: placeholder for 24-Mbit die capacity */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100128};
129
130static u8 cs_mask2_num[] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
131
132static struct reg_data odpg_default_value[] = {
133 {0x1034, 0x38000, MASK_ALL_BITS},
134 {0x1038, 0x0, MASK_ALL_BITS},
135 {0x10b0, 0x0, MASK_ALL_BITS},
136 {0x10b8, 0x0, MASK_ALL_BITS},
137 {0x10c0, 0x0, MASK_ALL_BITS},
138 {0x10f0, 0x0, MASK_ALL_BITS},
139 {0x10f4, 0x0, MASK_ALL_BITS},
140 {0x10f8, 0xff, MASK_ALL_BITS},
141 {0x10fc, 0xffff, MASK_ALL_BITS},
142 {0x1130, 0x0, MASK_ALL_BITS},
143 {0x1830, 0x2000000, MASK_ALL_BITS},
144 {0x14d0, 0x0, MASK_ALL_BITS},
145 {0x14d4, 0x0, MASK_ALL_BITS},
146 {0x14d8, 0x0, MASK_ALL_BITS},
147 {0x14dc, 0x0, MASK_ALL_BITS},
148 {0x1454, 0x0, MASK_ALL_BITS},
149 {0x1594, 0x0, MASK_ALL_BITS},
150 {0x1598, 0x0, MASK_ALL_BITS},
151 {0x159c, 0x0, MASK_ALL_BITS},
152 {0x15a0, 0x0, MASK_ALL_BITS},
153 {0x15a4, 0x0, MASK_ALL_BITS},
154 {0x15a8, 0x0, MASK_ALL_BITS},
155 {0x15ac, 0x0, MASK_ALL_BITS},
Marek Behúnb9eaf112021-02-19 17:11:23 +0100156 {0x1600, 0x0, MASK_ALL_BITS},
Stefan Roese5ffceb82015-03-26 15:36:56 +0100157 {0x1604, 0x0, MASK_ALL_BITS},
158 {0x1608, 0x0, MASK_ALL_BITS},
159 {0x160c, 0x0, MASK_ALL_BITS},
160 {0x1610, 0x0, MASK_ALL_BITS},
161 {0x1614, 0x0, MASK_ALL_BITS},
162 {0x1618, 0x0, MASK_ALL_BITS},
163 {0x1624, 0x0, MASK_ALL_BITS},
164 {0x1690, 0x0, MASK_ALL_BITS},
165 {0x1694, 0x0, MASK_ALL_BITS},
166 {0x1698, 0x0, MASK_ALL_BITS},
167 {0x169c, 0x0, MASK_ALL_BITS},
168 {0x14b8, 0x6f67, MASK_ALL_BITS},
169 {0x1630, 0x0, MASK_ALL_BITS},
170 {0x1634, 0x0, MASK_ALL_BITS},
171 {0x1638, 0x0, MASK_ALL_BITS},
172 {0x163c, 0x0, MASK_ALL_BITS},
173 {0x16b0, 0x0, MASK_ALL_BITS},
174 {0x16b4, 0x0, MASK_ALL_BITS},
175 {0x16b8, 0x0, MASK_ALL_BITS},
176 {0x16bc, 0x0, MASK_ALL_BITS},
177 {0x16c0, 0x0, MASK_ALL_BITS},
178 {0x16c4, 0x0, MASK_ALL_BITS},
179 {0x16c8, 0x0, MASK_ALL_BITS},
180 {0x16cc, 0x1, MASK_ALL_BITS},
181 {0x16f0, 0x1, MASK_ALL_BITS},
182 {0x16f4, 0x0, MASK_ALL_BITS},
183 {0x16f8, 0x0, MASK_ALL_BITS},
184 {0x16fc, 0x0, MASK_ALL_BITS}
185};
186
Chris Packham1a07d212018-05-10 13:28:29 +1200187/* MR cmd and addr definitions */
Tony Dinhe2c524b2023-01-18 19:03:04 -0800188#if defined(CONFIG_DDR4)
189struct mv_ddr_mr_data mr_data[] = {
190 {MRS0_CMD, DDR4_MR0_REG},
191 {MRS1_CMD, DDR4_MR1_REG},
192 {MRS2_CMD, DDR4_MR2_REG},
193 {MRS3_CMD, DDR4_MR3_REG},
194 {MRS4_CMD, DDR4_MR4_REG},
195 {MRS5_CMD, DDR4_MR5_REG},
196 {MRS6_CMD, DDR4_MR6_REG}
197};
198#else
Chris Packham1a07d212018-05-10 13:28:29 +1200199struct mv_ddr_mr_data mr_data[] = {
200 {MRS0_CMD, MR0_REG},
201 {MRS1_CMD, MR1_REG},
202 {MRS2_CMD, MR2_REG},
203 {MRS3_CMD, MR3_REG}
204};
Tony Dinhe2c524b2023-01-18 19:03:04 -0800205#endif
Chris Packham1a07d212018-05-10 13:28:29 +1200206
Chris Packham4bf81db2018-12-03 14:26:49 +1300207/* inverse pads */
208static int ddr3_tip_pad_inv(void)
209{
210 u32 sphy, data;
211 u32 sphy_max = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
212 u32 ck_swap_ctrl_sphy;
213 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
214
215 for (sphy = 0; sphy < sphy_max; sphy++) {
216 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, sphy);
217 if (tm->interface_params[0].
218 as_bus_params[sphy].is_dqs_swap == 1) {
219 data = (INVERT_PAD << INV_PAD4_OFFS |
220 INVERT_PAD << INV_PAD5_OFFS);
221 /* dqs swap */
222 ddr3_tip_bus_read_modify_write(0, ACCESS_TYPE_UNICAST,
223 0, sphy,
224 DDR_PHY_DATA,
225 PHY_CTRL_PHY_REG,
226 data, data);
227 }
228
229 if (tm->interface_params[0].as_bus_params[sphy].
230 is_ck_swap == 1 && sphy == 0) {
231/* TODO: move this code to per platform one */
Chris Packham4bf81db2018-12-03 14:26:49 +1300232 /* clock swap for both cs0 and cs1 */
233 data = (INVERT_PAD << INV_PAD2_OFFS |
234 INVERT_PAD << INV_PAD6_OFFS |
235 INVERT_PAD << INV_PAD4_OFFS |
236 INVERT_PAD << INV_PAD5_OFFS);
237 ck_swap_ctrl_sphy = CK_SWAP_CTRL_PHY_NUM;
238 ddr3_tip_bus_read_modify_write(0, ACCESS_TYPE_UNICAST,
239 0, ck_swap_ctrl_sphy,
240 DDR_PHY_CONTROL,
241 PHY_CTRL_PHY_REG,
242 data, data);
Chris Packham4bf81db2018-12-03 14:26:49 +1300243 }
244 }
245
246 return MV_OK;
247}
248
Stefan Roese5ffceb82015-03-26 15:36:56 +0100249static int ddr3_tip_rank_control(u32 dev_num, u32 if_id);
250
251/*
252 * Update global training parameters by data from user
253 */
254int ddr3_tip_tune_training_params(u32 dev_num,
255 struct tune_train_params *params)
256{
Chris Packham1a07d212018-05-10 13:28:29 +1200257 if (params->ck_delay != PARAM_UNDEFINED)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100258 ck_delay = params->ck_delay;
Chris Packham1a07d212018-05-10 13:28:29 +1200259 if (params->phy_reg3_val != PARAM_UNDEFINED)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100260 phy_reg3_val = params->phy_reg3_val;
Chris Packham1a07d212018-05-10 13:28:29 +1200261 if (params->g_rtt_nom != PARAM_UNDEFINED)
262 g_rtt_nom = params->g_rtt_nom;
263 if (params->g_rtt_wr != PARAM_UNDEFINED)
264 g_rtt_wr = params->g_rtt_wr;
265 if (params->g_dic != PARAM_UNDEFINED)
266 g_dic = params->g_dic;
267 if (params->g_odt_config != PARAM_UNDEFINED)
268 g_odt_config = params->g_odt_config;
269 if (params->g_zpri_data != PARAM_UNDEFINED)
270 g_zpri_data = params->g_zpri_data;
271 if (params->g_znri_data != PARAM_UNDEFINED)
272 g_znri_data = params->g_znri_data;
273 if (params->g_zpri_ctrl != PARAM_UNDEFINED)
274 g_zpri_ctrl = params->g_zpri_ctrl;
275 if (params->g_znri_ctrl != PARAM_UNDEFINED)
276 g_znri_ctrl = params->g_znri_ctrl;
277 if (params->g_zpodt_data != PARAM_UNDEFINED)
278 g_zpodt_data = params->g_zpodt_data;
279 if (params->g_znodt_data != PARAM_UNDEFINED)
280 g_znodt_data = params->g_znodt_data;
281 if (params->g_zpodt_ctrl != PARAM_UNDEFINED)
282 g_zpodt_ctrl = params->g_zpodt_ctrl;
283 if (params->g_znodt_ctrl != PARAM_UNDEFINED)
284 g_znodt_ctrl = params->g_znodt_ctrl;
285 if (params->g_rtt_park != PARAM_UNDEFINED)
286 g_rtt_park = params->g_rtt_park;
287
288 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
289 ("DGL parameters: 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X\n",
290 g_zpri_data, g_znri_data, g_zpri_ctrl, g_znri_ctrl, g_zpodt_data, g_znodt_data,
291 g_zpodt_ctrl, g_znodt_ctrl, g_rtt_nom, g_dic, g_odt_config, g_rtt_wr));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100292
293 return MV_OK;
294}
295
296/*
297 * Configure CS
298 */
299int ddr3_tip_configure_cs(u32 dev_num, u32 if_id, u32 cs_num, u32 enable)
300{
301 u32 data, addr_hi, data_high;
302 u32 mem_index;
Baruch Siach24a1d132020-01-20 14:20:06 +0200303 u32 clk_enable;
Chris Packham1a07d212018-05-10 13:28:29 +1200304 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100305
Baruch Siach24a1d132020-01-20 14:20:06 +0200306 if (tm->clk_enable & (1 << cs_num))
307 clk_enable = 1;
308 else
309 clk_enable = enable;
310
Stefan Roese5ffceb82015-03-26 15:36:56 +0100311 if (enable == 1) {
312 data = (tm->interface_params[if_id].bus_width ==
Chris Packham1a07d212018-05-10 13:28:29 +1200313 MV_DDR_DEV_WIDTH_8BIT) ? 0 : 1;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100314 CHECK_STATUS(ddr3_tip_if_write
315 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200316 SDRAM_ADDR_CTRL_REG, (data << (cs_num * 4)),
Stefan Roese5ffceb82015-03-26 15:36:56 +0100317 0x3 << (cs_num * 4)));
318 mem_index = tm->interface_params[if_id].memory_size;
319
320 addr_hi = mem_size_config[mem_index] & 0x3;
321 CHECK_STATUS(ddr3_tip_if_write
322 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200323 SDRAM_ADDR_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100324 (addr_hi << (2 + cs_num * 4)),
325 0x3 << (2 + cs_num * 4)));
326
327 data_high = (mem_size_config[mem_index] & 0x4) >> 2;
328 CHECK_STATUS(ddr3_tip_if_write
329 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200330 SDRAM_ADDR_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100331 data_high << (20 + cs_num), 1 << (20 + cs_num)));
332
333 /* Enable Address Select Mode */
334 CHECK_STATUS(ddr3_tip_if_write
335 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200336 SDRAM_ADDR_CTRL_REG, 1 << (16 + cs_num),
Stefan Roese5ffceb82015-03-26 15:36:56 +0100337 1 << (16 + cs_num)));
338 }
339 switch (cs_num) {
340 case 0:
341 case 1:
342 case 2:
343 CHECK_STATUS(ddr3_tip_if_write
344 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Baruch Siach24a1d132020-01-20 14:20:06 +0200345 DUNIT_CTRL_LOW_REG, (clk_enable << (cs_num + 11)),
Stefan Roese5ffceb82015-03-26 15:36:56 +0100346 1 << (cs_num + 11)));
347 break;
348 case 3:
349 CHECK_STATUS(ddr3_tip_if_write
350 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Baruch Siach24a1d132020-01-20 14:20:06 +0200351 DUNIT_CTRL_LOW_REG, (clk_enable << 15), 1 << 15));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100352 break;
353 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100354
355 return MV_OK;
356}
357
358/*
359 * Init Controller Flow
360 */
361int hws_ddr3_tip_init_controller(u32 dev_num, struct init_cntr_param *init_cntr_prm)
362{
363 u32 if_id;
364 u32 cs_num;
Chris Packham1a07d212018-05-10 13:28:29 +1200365 u32 t_ckclk = 0, t_wr = 0, t2t = 0;
366 u32 data_value = 0, cs_cnt = 0,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100367 mem_mask = 0, bus_index = 0;
Chris Packham4bf81db2018-12-03 14:26:49 +1300368 enum mv_ddr_speed_bin speed_bin_index = SPEED_BIN_DDR_2133N;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100369 u32 cs_mask = 0;
370 u32 cl_value = 0, cwl_val = 0;
Chris Packham1a07d212018-05-10 13:28:29 +1200371 u32 bus_cnt = 0, adll_tap = 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100372 enum hws_access_type access_type = ACCESS_TYPE_UNICAST;
373 u32 data_read[MAX_INTERFACE_NUM];
Chris Packham1a07d212018-05-10 13:28:29 +1200374 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
375 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Chris Packham3a09e132018-05-10 13:28:30 +1200376 enum mv_ddr_timing timing;
Chris Packham4bf81db2018-12-03 14:26:49 +1300377 enum mv_ddr_freq freq = tm->interface_params[0].memory_freq;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100378
379 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
380 ("Init_controller, do_mrs_phy=%d, is_ctrl64_bit=%d\n",
381 init_cntr_prm->do_mrs_phy,
382 init_cntr_prm->is_ctrl64_bit));
383
384 if (init_cntr_prm->init_phy == 1) {
385 CHECK_STATUS(ddr3_tip_configure_phy(dev_num));
386 }
387
388 if (generic_init_controller == 1) {
389 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +1200390 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100391 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
392 ("active IF %d\n", if_id));
393 mem_mask = 0;
394 for (bus_index = 0;
Chris Packham1a07d212018-05-10 13:28:29 +1200395 bus_index < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100396 bus_index++) {
Chris Packham1a07d212018-05-10 13:28:29 +1200397 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_index);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100398 mem_mask |=
399 tm->interface_params[if_id].
400 as_bus_params[bus_index].mirror_enable_bitmask;
401 }
402
403 if (mem_mask != 0) {
404 CHECK_STATUS(ddr3_tip_if_write
405 (dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +1200406 if_id, DUAL_DUNIT_CFG_REG, 0,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100407 0x8));
408 }
409
Stefan Roese5ffceb82015-03-26 15:36:56 +0100410 speed_bin_index =
411 tm->interface_params[if_id].
412 speed_bin_index;
Chris Packham1a07d212018-05-10 13:28:29 +1200413
Stefan Roese5ffceb82015-03-26 15:36:56 +0100414 /* t_ckclk is external clock */
Chris Packham4bf81db2018-12-03 14:26:49 +1300415 t_ckclk = (MEGA / mv_ddr_freq_get(freq));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100416
Chris Packham1a07d212018-05-10 13:28:29 +1200417 if (MV_DDR_IS_HALF_BUS_DRAM_MODE(tm->bus_act_mask, octets_per_if_num))
418 data_value = (0x4000 | 0 | 0x1000000) & ~(1 << 26);
419 else
420 data_value = (0x4000 | 0x8000 | 0x1000000) & ~(1 << 26);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100421
422 /* Interface Bus Width */
423 /* SRMode */
424 CHECK_STATUS(ddr3_tip_if_write
425 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200426 SDRAM_CFG_REG, data_value,
427 0x100c000));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100428
429 /* Interleave first command pre-charge enable (TBD) */
430 CHECK_STATUS(ddr3_tip_if_write
431 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200432 SDRAM_OPEN_PAGES_CTRL_REG, (1 << 10),
Stefan Roese5ffceb82015-03-26 15:36:56 +0100433 (1 << 10)));
434
Chris Packham1a07d212018-05-10 13:28:29 +1200435 /* Reset divider_b assert -> de-assert */
436 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
437 SDRAM_CFG_REG,
438 0x0 << PUP_RST_DIVIDER_OFFS,
439 PUP_RST_DIVIDER_MASK << PUP_RST_DIVIDER_OFFS));
440
441 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
442 SDRAM_CFG_REG,
443 0x1 << PUP_RST_DIVIDER_OFFS,
444 PUP_RST_DIVIDER_MASK << PUP_RST_DIVIDER_OFFS));
445
Stefan Roese5ffceb82015-03-26 15:36:56 +0100446 /* PHY configuration */
447 /*
448 * Postamble Length = 1.5cc, Addresscntl to clk skew
449 * \BD, Preamble length normal, parralal ADLL enable
450 */
451 CHECK_STATUS(ddr3_tip_if_write
452 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200453 DRAM_PHY_CFG_REG, 0x28, 0x3e));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100454 if (init_cntr_prm->is_ctrl64_bit) {
455 /* positive edge */
456 CHECK_STATUS(ddr3_tip_if_write
457 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200458 DRAM_PHY_CFG_REG, 0x0,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100459 0xff80));
460 }
461
462 /* calibration block disable */
463 /* Xbar Read buffer select (for Internal access) */
464 CHECK_STATUS(ddr3_tip_if_write
465 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200466 MAIN_PADS_CAL_MACH_CTRL_REG, 0x1200c,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100467 0x7dffe01c));
468 CHECK_STATUS(ddr3_tip_if_write
469 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200470 MAIN_PADS_CAL_MACH_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100471 calibration_update_control << 3, 0x3 << 3));
472
473 /* Pad calibration control - enable */
474 CHECK_STATUS(ddr3_tip_if_write
475 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200476 MAIN_PADS_CAL_MACH_CTRL_REG, 0x1, 0x1));
477 if (ddr3_tip_dev_attr_get(dev_num, MV_ATTR_TIP_REV) < MV_TIP_REV_3) {
478 /* DDR3 rank ctrl \96 part of the generic code */
479 /* CS1 mirroring enable + w/a for JIRA DUNIT-14581 */
480 CHECK_STATUS(ddr3_tip_if_write
481 (dev_num, access_type, if_id,
482 DDR3_RANK_CTRL_REG, 0x27, MASK_ALL_BITS));
483 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100484
485 cs_mask = 0;
486 data_value = 0x7;
487 /*
488 * Address ctrl \96 Part of the Generic code
489 * The next configuration is done:
490 * 1) Memory Size
491 * 2) Bus_width
492 * 3) CS#
493 * 4) Page Number
Stefan Roese5ffceb82015-03-26 15:36:56 +0100494 * Per Dunit get from the Map_topology the parameters:
495 * Bus_width
Stefan Roese5ffceb82015-03-26 15:36:56 +0100496 */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100497
498 data_value =
499 (tm->interface_params[if_id].
Chris Packham1a07d212018-05-10 13:28:29 +1200500 bus_width == MV_DDR_DEV_WIDTH_8BIT) ? 0 : 1;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100501
502 /* create merge cs mask for all cs available in dunit */
503 for (bus_cnt = 0;
Chris Packham1a07d212018-05-10 13:28:29 +1200504 bus_cnt < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100505 bus_cnt++) {
Chris Packham1a07d212018-05-10 13:28:29 +1200506 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100507 cs_mask |=
508 tm->interface_params[if_id].
509 as_bus_params[bus_cnt].cs_bitmask;
510 }
511 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
512 ("Init_controller IF %d cs_mask %d\n",
513 if_id, cs_mask));
514 /*
515 * Configure the next upon the Map Topology \96 If the
516 * Dunit is CS0 Configure CS0 if it is multi CS
517 * configure them both: The Bust_width it\92s the
518 * Memory Bus width \96 x8 or x16
519 */
Chris Packham4bf81db2018-12-03 14:26:49 +1300520 for (cs_cnt = 0; cs_cnt < MAX_CS_NUM; cs_cnt++) {
Stefan Roese5ffceb82015-03-26 15:36:56 +0100521 ddr3_tip_configure_cs(dev_num, if_id, cs_cnt,
522 ((cs_mask & (1 << cs_cnt)) ? 1
523 : 0));
524 }
525
526 if (init_cntr_prm->do_mrs_phy) {
527 /*
528 * MR0 \96 Part of the Generic code
529 * The next configuration is done:
530 * 1) Burst Length
531 * 2) CAS Latency
532 * get for each dunit what is it Speed_bin &
533 * Target Frequency. From those both parameters
534 * get the appropriate Cas_l from the CL table
535 */
536 cl_value =
537 tm->interface_params[if_id].
538 cas_l;
539 cwl_val =
540 tm->interface_params[if_id].
541 cas_wl;
542 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
543 ("cl_value 0x%x cwl_val 0x%x\n",
544 cl_value, cwl_val));
Chris Packham1a07d212018-05-10 13:28:29 +1200545
Chris Packham4bf81db2018-12-03 14:26:49 +1300546 t_wr = time_to_nclk(mv_ddr_speed_bin_timing_get
Chris Packham1a07d212018-05-10 13:28:29 +1200547 (speed_bin_index,
548 SPEED_BIN_TWR), t_ckclk);
549
Stefan Roese5ffceb82015-03-26 15:36:56 +0100550 data_value =
551 ((cl_mask_table[cl_value] & 0x1) << 2) |
552 ((cl_mask_table[cl_value] & 0xe) << 3);
553 CHECK_STATUS(ddr3_tip_if_write
554 (dev_num, access_type, if_id,
555 MR0_REG, data_value,
556 (0x7 << 4) | (1 << 2)));
557 CHECK_STATUS(ddr3_tip_if_write
558 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200559 MR0_REG, twr_mask_table[t_wr] << 9,
560 0x7 << 9));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100561
562 /*
563 * MR1: Set RTT and DIC Design GL values
564 * configured by user
565 */
566 CHECK_STATUS(ddr3_tip_if_write
567 (dev_num, ACCESS_TYPE_MULTICAST,
568 PARAM_NOT_CARE, MR1_REG,
569 g_dic | g_rtt_nom, 0x266));
570
571 /* MR2 - Part of the Generic code */
572 /*
573 * The next configuration is done:
574 * 1) SRT
575 * 2) CAS Write Latency
576 */
577 data_value = (cwl_mask_table[cwl_val] << 3);
578 data_value |=
579 ((tm->interface_params[if_id].
580 interface_temp ==
Chris Packham1a07d212018-05-10 13:28:29 +1200581 MV_DDR_TEMP_HIGH) ? (1 << 7) : 0);
582 data_value |= g_rtt_wr;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100583 CHECK_STATUS(ddr3_tip_if_write
584 (dev_num, access_type, if_id,
585 MR2_REG, data_value,
586 (0x7 << 3) | (0x1 << 7) | (0x3 <<
587 9)));
588 }
589
590 ddr3_tip_write_odt(dev_num, access_type, if_id,
591 cl_value, cwl_val);
592 ddr3_tip_set_timing(dev_num, access_type, if_id, freq);
593
Chris Packham1a07d212018-05-10 13:28:29 +1200594 if (ddr3_tip_dev_attr_get(dev_num, MV_ATTR_TIP_REV) < MV_TIP_REV_3) {
Stefan Roese5ffceb82015-03-26 15:36:56 +0100595 CHECK_STATUS(ddr3_tip_if_write
596 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200597 DUNIT_CTRL_HIGH_REG, 0x1000119,
598 0x100017F));
599 } else {
600 CHECK_STATUS(ddr3_tip_if_write
601 (dev_num, access_type, if_id,
602 DUNIT_CTRL_HIGH_REG, 0x600177 |
603 (init_cntr_prm->is_ctrl64_bit ?
604 CPU_INTERJECTION_ENA_SPLIT_ENA << CPU_INTERJECTION_ENA_OFFS :
605 CPU_INTERJECTION_ENA_SPLIT_DIS << CPU_INTERJECTION_ENA_OFFS),
606 0x1600177 | CPU_INTERJECTION_ENA_MASK <<
607 CPU_INTERJECTION_ENA_OFFS));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100608 }
609
610 /* reset bit 7 */
611 CHECK_STATUS(ddr3_tip_if_write
612 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200613 DUNIT_CTRL_HIGH_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100614 (init_cntr_prm->msys_init << 7), (1 << 7)));
615
Chris Packham3a09e132018-05-10 13:28:30 +1200616 timing = tm->interface_params[if_id].timing;
617
Chris Packham1a07d212018-05-10 13:28:29 +1200618 if (mode_2t != 0xff) {
619 t2t = mode_2t;
Chris Packham3a09e132018-05-10 13:28:30 +1200620 } else if (timing != MV_DDR_TIM_DEFAULT) {
621 t2t = (timing == MV_DDR_TIM_2T) ? 1 : 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100622 } else {
Chris Packham1a07d212018-05-10 13:28:29 +1200623 /* calculate number of CS (per interface) */
Chris Packham4bf81db2018-12-03 14:26:49 +1300624 cs_num = mv_ddr_cs_num_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100625 t2t = (cs_num == 1) ? 0 : 1;
626 }
627
628 CHECK_STATUS(ddr3_tip_if_write
629 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200630 DUNIT_CTRL_LOW_REG, t2t << 3,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100631 0x3 << 3));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100632 CHECK_STATUS(ddr3_tip_if_write
633 (dev_num, access_type, if_id,
634 DDR_TIMING_REG, 0x28 << 9, 0x3f << 9));
635 CHECK_STATUS(ddr3_tip_if_write
636 (dev_num, access_type, if_id,
637 DDR_TIMING_REG, 0xa << 21, 0xff << 21));
638
639 /* move the block to ddr3_tip_set_timing - end */
640 /* AUTO_ZQC_TIMING */
641 CHECK_STATUS(ddr3_tip_if_write
642 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200643 ZQC_CFG_REG, (AUTO_ZQC_TIMING | (2 << 20)),
Stefan Roese5ffceb82015-03-26 15:36:56 +0100644 0x3fffff));
645 CHECK_STATUS(ddr3_tip_if_read
646 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200647 DRAM_PHY_CFG_REG, data_read, 0x30));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100648 data_value =
649 (data_read[if_id] == 0) ? (1 << 11) : 0;
650 CHECK_STATUS(ddr3_tip_if_write
651 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200652 DUNIT_CTRL_HIGH_REG, data_value,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100653 (1 << 11)));
654
655 /* Set Active control for ODT write transactions */
656 CHECK_STATUS(ddr3_tip_if_write
657 (dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +1200658 PARAM_NOT_CARE, 0x1494, g_odt_config,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100659 MASK_ALL_BITS));
Chris Packham1a07d212018-05-10 13:28:29 +1200660
661 if (ddr3_tip_dev_attr_get(dev_num, MV_ATTR_TIP_REV) == MV_TIP_REV_3) {
662 CHECK_STATUS(ddr3_tip_if_write
663 (dev_num, access_type, if_id,
664 0x14a8, 0x900, 0x900));
665 /* wa: controls control sub-phy outputs floating during self-refresh */
666 CHECK_STATUS(ddr3_tip_if_write
667 (dev_num, access_type, if_id,
668 0x16d0, 0, 0x8000));
669 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100670 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100671 }
672
673 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +1200674 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100675 CHECK_STATUS(ddr3_tip_rank_control(dev_num, if_id));
676
Chris Packham4bf81db2018-12-03 14:26:49 +1300677 if (init_cntr_prm->do_mrs_phy)
678 ddr3_tip_pad_inv();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100679
680 /* Pad calibration control - disable */
681 CHECK_STATUS(ddr3_tip_if_write
682 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200683 MAIN_PADS_CAL_MACH_CTRL_REG, 0x0, 0x1));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100684 CHECK_STATUS(ddr3_tip_if_write
685 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +1200686 MAIN_PADS_CAL_MACH_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100687 calibration_update_control << 3, 0x3 << 3));
688 }
689
Tony Dinhe2c524b2023-01-18 19:03:04 -0800690#if defined(CONFIG_DDR4)
691 /* dev_num, vref_en, pod_only */
692 CHECK_STATUS(mv_ddr4_mode_regs_init(dev_num));
693 CHECK_STATUS(mv_ddr4_sdram_config(dev_num));
694#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100695
696 if (delay_enable != 0) {
Chris Packham4bf81db2018-12-03 14:26:49 +1300697 adll_tap = MEGA / (mv_ddr_freq_get(freq) * 64);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100698 ddr3_tip_cmd_addr_init_delay(dev_num, adll_tap);
699 }
700
701 return MV_OK;
702}
703
704/*
Chris Packham1a07d212018-05-10 13:28:29 +1200705 * Rank Control Flow
Stefan Roese5ffceb82015-03-26 15:36:56 +0100706 */
Chris Packham1a07d212018-05-10 13:28:29 +1200707static int ddr3_tip_rev2_rank_control(u32 dev_num, u32 if_id)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100708{
Chris Packham1a07d212018-05-10 13:28:29 +1200709 u32 data_value = 0, bus_cnt = 0;
710 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
711 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100712
Chris Packham1a07d212018-05-10 13:28:29 +1200713 for (bus_cnt = 0; bus_cnt < octets_per_if_num; bus_cnt++) {
714 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
715 data_value |= tm->interface_params[if_id].as_bus_params[bus_cnt].
716 cs_bitmask;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100717
Chris Packham1a07d212018-05-10 13:28:29 +1200718 if (tm->interface_params[if_id].as_bus_params[bus_cnt].
719 mirror_enable_bitmask == 1) {
720 /*
721 * Check mirror_enable_bitmask
722 * If it is enabled, CS + 4 bit in a word to be '1'
723 */
724 if ((tm->interface_params[if_id].as_bus_params[bus_cnt].
725 cs_bitmask & 0x1) != 0) {
726 data_value |= tm->interface_params[if_id].
727 as_bus_params[bus_cnt].
728 mirror_enable_bitmask << 4;
729 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100730
Chris Packham1a07d212018-05-10 13:28:29 +1200731 if ((tm->interface_params[if_id].as_bus_params[bus_cnt].
732 cs_bitmask & 0x2) != 0) {
733 data_value |= tm->interface_params[if_id].
734 as_bus_params[bus_cnt].
735 mirror_enable_bitmask << 5;
736 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100737
Chris Packham1a07d212018-05-10 13:28:29 +1200738 if ((tm->interface_params[if_id].as_bus_params[bus_cnt].
739 cs_bitmask & 0x4) != 0) {
740 data_value |= tm->interface_params[if_id].
741 as_bus_params[bus_cnt].
742 mirror_enable_bitmask << 6;
743 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100744
Chris Packham1a07d212018-05-10 13:28:29 +1200745 if ((tm->interface_params[if_id].as_bus_params[bus_cnt].
746 cs_bitmask & 0x8) != 0) {
747 data_value |= tm->interface_params[if_id].
748 as_bus_params[bus_cnt].
749 mirror_enable_bitmask << 7;
750 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100751 }
752 }
753
Chris Packham1a07d212018-05-10 13:28:29 +1200754 CHECK_STATUS(ddr3_tip_if_write
755 (dev_num, ACCESS_TYPE_UNICAST, if_id, DDR3_RANK_CTRL_REG,
756 data_value, 0xff));
757
Stefan Roese5ffceb82015-03-26 15:36:56 +0100758 return MV_OK;
759}
760
Chris Packham1a07d212018-05-10 13:28:29 +1200761static int ddr3_tip_rev3_rank_control(u32 dev_num, u32 if_id)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100762{
763 u32 data_value = 0, bus_cnt;
Chris Packham1a07d212018-05-10 13:28:29 +1200764 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
765 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100766
Chris Packham1a07d212018-05-10 13:28:29 +1200767 for (bus_cnt = 1; bus_cnt < octets_per_if_num; bus_cnt++) {
768 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100769 if ((tm->interface_params[if_id].
770 as_bus_params[0].cs_bitmask !=
771 tm->interface_params[if_id].
772 as_bus_params[bus_cnt].cs_bitmask) ||
773 (tm->interface_params[if_id].
774 as_bus_params[0].mirror_enable_bitmask !=
775 tm->interface_params[if_id].
776 as_bus_params[bus_cnt].mirror_enable_bitmask))
777 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
778 ("WARNING:Wrong configuration for pup #%d CS mask and CS mirroring for all pups should be the same\n",
779 bus_cnt));
780 }
781
782 data_value |= tm->interface_params[if_id].
783 as_bus_params[0].cs_bitmask;
784 data_value |= tm->interface_params[if_id].
785 as_bus_params[0].mirror_enable_bitmask << 4;
786
787 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +1200788 (dev_num, ACCESS_TYPE_UNICAST, if_id, DDR3_RANK_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +0100789 data_value, 0xff));
790
791 return MV_OK;
792}
793
Chris Packham1a07d212018-05-10 13:28:29 +1200794static int ddr3_tip_rank_control(u32 dev_num, u32 if_id)
795{
796 if (ddr3_tip_dev_attr_get(dev_num, MV_ATTR_TIP_REV) == MV_TIP_REV_2)
797 return ddr3_tip_rev2_rank_control(dev_num, if_id);
798 else
799 return ddr3_tip_rev3_rank_control(dev_num, if_id);
800}
801
Stefan Roese5ffceb82015-03-26 15:36:56 +0100802/*
Chris Packham1a07d212018-05-10 13:28:29 +1200803 * Algorithm Parameters Validation
804 */
805int ddr3_tip_validate_algo_var(u32 value, u32 fail_value, char *var_name)
806{
807 if (value == fail_value) {
808 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
809 ("Error: %s is not initialized (Algo Components Validation)\n",
810 var_name));
811 return 0;
812 }
813
814 return 1;
815}
816
817int ddr3_tip_validate_algo_ptr(void *ptr, void *fail_value, char *ptr_name)
818{
819 if (ptr == fail_value) {
820 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
821 ("Error: %s is not initialized (Algo Components Validation)\n",
822 ptr_name));
823 return 0;
824 }
825
826 return 1;
827}
828
829int ddr3_tip_validate_algo_components(u8 dev_num)
830{
831 int status = 1;
832
833 /* Check DGL parameters*/
834 status &= ddr3_tip_validate_algo_var(ck_delay, PARAM_UNDEFINED, "ck_delay");
835 status &= ddr3_tip_validate_algo_var(phy_reg3_val, PARAM_UNDEFINED, "phy_reg3_val");
836 status &= ddr3_tip_validate_algo_var(g_rtt_nom, PARAM_UNDEFINED, "g_rtt_nom");
837 status &= ddr3_tip_validate_algo_var(g_dic, PARAM_UNDEFINED, "g_dic");
838 status &= ddr3_tip_validate_algo_var(odt_config, PARAM_UNDEFINED, "odt_config");
839 status &= ddr3_tip_validate_algo_var(g_zpri_data, PARAM_UNDEFINED, "g_zpri_data");
840 status &= ddr3_tip_validate_algo_var(g_znri_data, PARAM_UNDEFINED, "g_znri_data");
841 status &= ddr3_tip_validate_algo_var(g_zpri_ctrl, PARAM_UNDEFINED, "g_zpri_ctrl");
842 status &= ddr3_tip_validate_algo_var(g_znri_ctrl, PARAM_UNDEFINED, "g_znri_ctrl");
843 status &= ddr3_tip_validate_algo_var(g_zpodt_data, PARAM_UNDEFINED, "g_zpodt_data");
844 status &= ddr3_tip_validate_algo_var(g_znodt_data, PARAM_UNDEFINED, "g_znodt_data");
845 status &= ddr3_tip_validate_algo_var(g_zpodt_ctrl, PARAM_UNDEFINED, "g_zpodt_ctrl");
846 status &= ddr3_tip_validate_algo_var(g_znodt_ctrl, PARAM_UNDEFINED, "g_znodt_ctrl");
847
848 /* Check functions pointers */
849 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].tip_dunit_mux_select_func,
850 NULL, "tip_dunit_mux_select_func");
851 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].mv_ddr_dunit_write,
852 NULL, "mv_ddr_dunit_write");
853 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].mv_ddr_dunit_read,
854 NULL, "mv_ddr_dunit_read");
855 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].mv_ddr_phy_write,
856 NULL, "mv_ddr_phy_write");
857 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].mv_ddr_phy_read,
858 NULL, "mv_ddr_phy_read");
859 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].tip_get_freq_config_info_func,
860 NULL, "tip_get_freq_config_info_func");
861 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].tip_set_freq_divider_func,
862 NULL, "tip_set_freq_divider_func");
863 status &= ddr3_tip_validate_algo_ptr(config_func_info[dev_num].tip_get_clock_ratio,
864 NULL, "tip_get_clock_ratio");
865
866 status &= ddr3_tip_validate_algo_ptr(dq_map_table, NULL, "dq_map_table");
867 status &= ddr3_tip_validate_algo_var(dfs_low_freq, 0, "dfs_low_freq");
868
869 return (status == 1) ? MV_OK : MV_NOT_INITIALIZED;
870}
871
Chris Packham1a07d212018-05-10 13:28:29 +1200872int ddr3_pre_algo_config(void)
873{
874 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
875
876 /* Set Bus3 ECC training mode */
877 if (DDR3_IS_ECC_PUP3_MODE(tm->bus_act_mask)) {
878 /* Set Bus3 ECC MUX */
879 CHECK_STATUS(ddr3_tip_if_write
880 (0, ACCESS_TYPE_UNICAST, PARAM_NOT_CARE,
881 DRAM_PINS_MUX_REG, 0x100, 0x100));
882 }
883
884 /* Set regular ECC training mode (bus4 and bus 3) */
885 if ((DDR3_IS_ECC_PUP4_MODE(tm->bus_act_mask)) ||
886 (DDR3_IS_ECC_PUP3_MODE(tm->bus_act_mask)) ||
887 (DDR3_IS_ECC_PUP8_MODE(tm->bus_act_mask))) {
888 /* Enable ECC Write MUX */
889 CHECK_STATUS(ddr3_tip_if_write
890 (0, ACCESS_TYPE_UNICAST, PARAM_NOT_CARE,
891 TRAINING_SW_2_REG, 0x100, 0x100));
892 /* General ECC enable */
893 CHECK_STATUS(ddr3_tip_if_write
894 (0, ACCESS_TYPE_UNICAST, PARAM_NOT_CARE,
895 SDRAM_CFG_REG, 0x40000, 0x40000));
896 /* Disable Read Data ECC MUX */
897 CHECK_STATUS(ddr3_tip_if_write
898 (0, ACCESS_TYPE_UNICAST, PARAM_NOT_CARE,
899 TRAINING_SW_2_REG, 0x0, 0x2));
900 }
901
902 return MV_OK;
903}
904
905int ddr3_post_algo_config(void)
906{
907 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
908 int status;
909
910 status = ddr3_post_run_alg();
911 if (MV_OK != status) {
912 printf("DDR3 Post Run Alg - FAILED 0x%x\n", status);
913 return status;
914 }
915
916 /* Un_set ECC training mode */
917 if ((DDR3_IS_ECC_PUP4_MODE(tm->bus_act_mask)) ||
918 (DDR3_IS_ECC_PUP3_MODE(tm->bus_act_mask)) ||
919 (DDR3_IS_ECC_PUP8_MODE(tm->bus_act_mask))) {
920 /* Disable ECC Write MUX */
921 CHECK_STATUS(ddr3_tip_if_write
922 (0, ACCESS_TYPE_UNICAST, PARAM_NOT_CARE,
923 TRAINING_SW_2_REG, 0x0, 0x100));
924 /* General ECC and Bus3 ECC MUX remains enabled */
925 }
926
927 return MV_OK;
928}
929
930/*
Stefan Roese5ffceb82015-03-26 15:36:56 +0100931 * Run Training Flow
932 */
933int hws_ddr3_tip_run_alg(u32 dev_num, enum hws_algo_type algo_type)
934{
Chris Packham1a07d212018-05-10 13:28:29 +1200935 int status = MV_OK;
936
937 status = ddr3_pre_algo_config();
938 if (MV_OK != status) {
939 printf("DDR3 Pre Algo Config - FAILED 0x%x\n", status);
940 return status;
941 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100942
943#ifdef ODT_TEST_SUPPORT
944 if (finger_test == 1)
945 return odt_test(dev_num, algo_type);
946#endif
947
948 if (algo_type == ALGO_TYPE_DYNAMIC) {
Chris Packham1a07d212018-05-10 13:28:29 +1200949 status = ddr3_tip_ddr3_auto_tune(dev_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100950 }
951
Chris Packham1a07d212018-05-10 13:28:29 +1200952 if (status != MV_OK) {
Stefan Roese5ffceb82015-03-26 15:36:56 +0100953 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
Chris Packham1a07d212018-05-10 13:28:29 +1200954 ("******** DRAM initialization Failed (res 0x%x) ********\n",
955 status));
956 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100957 }
958
Chris Packham1a07d212018-05-10 13:28:29 +1200959 status = ddr3_post_algo_config();
960 if (MV_OK != status) {
961 printf("DDR3 Post Algo Config - FAILED 0x%x\n", status);
962 return status;
963 }
964
965 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100966}
967
968#ifdef ODT_TEST_SUPPORT
969/*
970 * ODT Test
971 */
972static int odt_test(u32 dev_num, enum hws_algo_type algo_type)
973{
974 int ret = MV_OK, ret_tune = MV_OK;
975 int pfinger_val = 0, nfinger_val;
976
977 for (pfinger_val = p_finger_start; pfinger_val <= p_finger_end;
978 pfinger_val += p_finger_step) {
979 for (nfinger_val = n_finger_start; nfinger_val <= n_finger_end;
980 nfinger_val += n_finger_step) {
981 if (finger_test != 0) {
982 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
983 ("pfinger_val %d nfinger_val %d\n",
984 pfinger_val, nfinger_val));
Chris Packham1a07d212018-05-10 13:28:29 +1200985 /*
986 * TODO: need to check the correctness
987 * of the following two lines.
988 */
989 g_zpodt_data = pfinger_val;
990 g_znodt_data = nfinger_val;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100991 }
992
993 if (algo_type == ALGO_TYPE_DYNAMIC) {
994 ret = ddr3_tip_ddr3_auto_tune(dev_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100995 }
996 }
997 }
998
999 if (ret_tune != MV_OK) {
1000 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1001 ("Run_alg: tuning failed %d\n", ret_tune));
1002 ret = (ret == MV_OK) ? ret_tune : ret;
1003 }
1004
1005 return ret;
1006}
1007#endif
1008
1009/*
1010 * Select Controller
1011 */
1012int hws_ddr3_tip_select_ddr_controller(u32 dev_num, int enable)
1013{
Chris Packham1a07d212018-05-10 13:28:29 +12001014 return config_func_info[dev_num].
1015 tip_dunit_mux_select_func((u8)dev_num, enable);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001016}
1017
1018/*
1019 * Dunit Register Write
1020 */
1021int ddr3_tip_if_write(u32 dev_num, enum hws_access_type interface_access,
1022 u32 if_id, u32 reg_addr, u32 data_value, u32 mask)
1023{
Chris Packham1a07d212018-05-10 13:28:29 +12001024 config_func_info[dev_num].mv_ddr_dunit_write(reg_addr, mask, data_value);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001025
Chris Packham1a07d212018-05-10 13:28:29 +12001026 return MV_OK;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001027}
1028
1029/*
1030 * Dunit Register Read
1031 */
1032int ddr3_tip_if_read(u32 dev_num, enum hws_access_type interface_access,
1033 u32 if_id, u32 reg_addr, u32 *data, u32 mask)
1034{
Chris Packham1a07d212018-05-10 13:28:29 +12001035 config_func_info[dev_num].mv_ddr_dunit_read(reg_addr, mask, data);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001036
Chris Packham1a07d212018-05-10 13:28:29 +12001037 return MV_OK;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001038}
1039
1040/*
1041 * Dunit Register Polling
1042 */
1043int ddr3_tip_if_polling(u32 dev_num, enum hws_access_type access_type,
1044 u32 if_id, u32 exp_value, u32 mask, u32 offset,
1045 u32 poll_tries)
1046{
1047 u32 poll_cnt = 0, interface_num = 0, start_if, end_if;
1048 u32 read_data[MAX_INTERFACE_NUM];
1049 int ret;
1050 int is_fail = 0, is_if_fail;
Chris Packham1a07d212018-05-10 13:28:29 +12001051 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01001052
1053 if (access_type == ACCESS_TYPE_MULTICAST) {
1054 start_if = 0;
1055 end_if = MAX_INTERFACE_NUM - 1;
1056 } else {
1057 start_if = if_id;
1058 end_if = if_id;
1059 }
1060
1061 for (interface_num = start_if; interface_num <= end_if; interface_num++) {
1062 /* polling bit 3 for n times */
Chris Packham1a07d212018-05-10 13:28:29 +12001063 VALIDATE_IF_ACTIVE(tm->if_act_mask, interface_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001064
1065 is_if_fail = 0;
1066 for (poll_cnt = 0; poll_cnt < poll_tries; poll_cnt++) {
1067 ret =
1068 ddr3_tip_if_read(dev_num, ACCESS_TYPE_UNICAST,
1069 interface_num, offset, read_data,
1070 mask);
1071 if (ret != MV_OK)
1072 return ret;
1073
1074 if (read_data[interface_num] == exp_value)
1075 break;
1076 }
1077
1078 if (poll_cnt >= poll_tries) {
1079 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1080 ("max poll IF #%d\n", interface_num));
1081 is_fail = 1;
1082 is_if_fail = 1;
1083 }
1084
1085 training_result[training_stage][interface_num] =
1086 (is_if_fail == 1) ? TEST_FAILED : TEST_SUCCESS;
1087 }
1088
1089 return (is_fail == 0) ? MV_OK : MV_FAIL;
1090}
1091
1092/*
1093 * Bus read access
1094 */
1095int ddr3_tip_bus_read(u32 dev_num, u32 if_id,
1096 enum hws_access_type phy_access, u32 phy_id,
1097 enum hws_ddr_phy phy_type, u32 reg_addr, u32 *data)
1098{
Chris Packham1a07d212018-05-10 13:28:29 +12001099 return config_func_info[dev_num].
1100 mv_ddr_phy_read(phy_access, phy_id, phy_type, reg_addr, data);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001101}
1102
1103/*
1104 * Bus write access
1105 */
1106int ddr3_tip_bus_write(u32 dev_num, enum hws_access_type interface_access,
1107 u32 if_id, enum hws_access_type phy_access,
1108 u32 phy_id, enum hws_ddr_phy phy_type, u32 reg_addr,
1109 u32 data_value)
1110{
Chris Packham1a07d212018-05-10 13:28:29 +12001111 return config_func_info[dev_num].
1112 mv_ddr_phy_write(phy_access, phy_id, phy_type, reg_addr, data_value, OPERATION_WRITE);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001113}
1114
Stefan Roese5ffceb82015-03-26 15:36:56 +01001115/*
Stefan Roese5ffceb82015-03-26 15:36:56 +01001116 * Phy read-modify-write
1117 */
1118int ddr3_tip_bus_read_modify_write(u32 dev_num, enum hws_access_type access_type,
1119 u32 interface_id, u32 phy_id,
1120 enum hws_ddr_phy phy_type, u32 reg_addr,
1121 u32 data_value, u32 reg_mask)
1122{
1123 u32 data_val = 0, if_id, start_if, end_if;
Chris Packham1a07d212018-05-10 13:28:29 +12001124 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01001125
1126 if (access_type == ACCESS_TYPE_MULTICAST) {
1127 start_if = 0;
1128 end_if = MAX_INTERFACE_NUM - 1;
1129 } else {
1130 start_if = interface_id;
1131 end_if = interface_id;
1132 }
1133
1134 for (if_id = start_if; if_id <= end_if; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001135 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001136 CHECK_STATUS(ddr3_tip_bus_read
1137 (dev_num, if_id, ACCESS_TYPE_UNICAST, phy_id,
1138 phy_type, reg_addr, &data_val));
1139 data_value = (data_val & (~reg_mask)) | (data_value & reg_mask);
1140 CHECK_STATUS(ddr3_tip_bus_write
1141 (dev_num, ACCESS_TYPE_UNICAST, if_id,
1142 ACCESS_TYPE_UNICAST, phy_id, phy_type, reg_addr,
1143 data_value));
1144 }
1145
1146 return MV_OK;
1147}
1148
1149/*
1150 * ADLL Calibration
1151 */
1152int adll_calibration(u32 dev_num, enum hws_access_type access_type,
Chris Packham4bf81db2018-12-03 14:26:49 +13001153 u32 if_id, enum mv_ddr_freq frequency)
Stefan Roese5ffceb82015-03-26 15:36:56 +01001154{
1155 struct hws_tip_freq_config_info freq_config_info;
1156 u32 bus_cnt = 0;
Chris Packham1a07d212018-05-10 13:28:29 +12001157 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
1158 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01001159
1160 /* Reset Diver_b assert -> de-assert */
1161 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001162 (dev_num, access_type, if_id, SDRAM_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001163 0, 0x10000000));
1164 mdelay(10);
1165 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001166 (dev_num, access_type, if_id, SDRAM_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001167 0x10000000, 0x10000000));
1168
Chris Packham1a07d212018-05-10 13:28:29 +12001169 CHECK_STATUS(config_func_info[dev_num].
1170 tip_get_freq_config_info_func((u8)dev_num, frequency,
1171 &freq_config_info));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001172
Chris Packham1a07d212018-05-10 13:28:29 +12001173 for (bus_cnt = 0; bus_cnt < octets_per_if_num; bus_cnt++) {
1174 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001175 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1176 (dev_num, access_type, if_id, bus_cnt,
Chris Packham1a07d212018-05-10 13:28:29 +12001177 DDR_PHY_DATA, ADLL_CFG0_PHY_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001178 freq_config_info.bw_per_freq << 8, 0x700));
1179 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1180 (dev_num, access_type, if_id, bus_cnt,
Chris Packham1a07d212018-05-10 13:28:29 +12001181 DDR_PHY_DATA, ADLL_CFG2_PHY_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001182 freq_config_info.rate_per_freq, 0x7));
1183 }
1184
Chris Packham1a07d212018-05-10 13:28:29 +12001185 for (bus_cnt = 0; bus_cnt < DDR_IF_CTRL_SUBPHYS_NUM; bus_cnt++) {
1186 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1187 (dev_num, ACCESS_TYPE_UNICAST, if_id, bus_cnt,
1188 DDR_PHY_CONTROL, ADLL_CFG0_PHY_REG,
1189 freq_config_info.bw_per_freq << 8, 0x700));
1190 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1191 (dev_num, ACCESS_TYPE_UNICAST, if_id, bus_cnt,
1192 DDR_PHY_CONTROL, ADLL_CFG2_PHY_REG,
1193 freq_config_info.rate_per_freq, 0x7));
1194 }
1195
Stefan Roese5ffceb82015-03-26 15:36:56 +01001196 /* DUnit to Phy drive post edge, ADLL reset assert de-assert */
1197 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001198 (dev_num, access_type, if_id, DRAM_PHY_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001199 0, (0x80000000 | 0x40000000)));
Chris Packham4bf81db2018-12-03 14:26:49 +13001200 mdelay(100 / (mv_ddr_freq_get(frequency)) / mv_ddr_freq_get(MV_DDR_FREQ_LOW_FREQ));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001201 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001202 (dev_num, access_type, if_id, DRAM_PHY_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001203 (0x80000000 | 0x40000000), (0x80000000 | 0x40000000)));
1204
1205 /* polling for ADLL Done */
1206 if (ddr3_tip_if_polling(dev_num, access_type, if_id,
1207 0x3ff03ff, 0x3ff03ff, PHY_LOCK_STATUS_REG,
1208 MAX_POLLING_ITERATIONS) != MV_OK) {
1209 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1210 ("Freq_set: DDR3 poll failed(1)"));
1211 }
1212
1213 /* pup data_pup reset assert-> deassert */
1214 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001215 (dev_num, access_type, if_id, SDRAM_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001216 0, 0x60000000));
1217 mdelay(10);
1218 CHECK_STATUS(ddr3_tip_if_write
Chris Packham1a07d212018-05-10 13:28:29 +12001219 (dev_num, access_type, if_id, SDRAM_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001220 0x60000000, 0x60000000));
1221
1222 return MV_OK;
1223}
1224
1225int ddr3_tip_freq_set(u32 dev_num, enum hws_access_type access_type,
Chris Packham4bf81db2018-12-03 14:26:49 +13001226 u32 if_id, enum mv_ddr_freq frequency)
Stefan Roese5ffceb82015-03-26 15:36:56 +01001227{
1228 u32 cl_value = 0, cwl_value = 0, mem_mask = 0, val = 0,
Chris Packham1a07d212018-05-10 13:28:29 +12001229 bus_cnt = 0, t_wr = 0, t_ckclk = 0,
1230 cnt_id;
1231 u32 end_if, start_if;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001232 u32 bus_index = 0;
1233 int is_dll_off = 0;
Chris Packham4bf81db2018-12-03 14:26:49 +13001234 enum mv_ddr_speed_bin speed_bin_index = 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001235 struct hws_tip_freq_config_info freq_config_info;
1236 enum hws_result *flow_result = training_result[training_stage];
1237 u32 adll_tap = 0;
Chris Packham1a07d212018-05-10 13:28:29 +12001238 u32 cs_num;
1239 u32 t2t;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001240 u32 cs_mask[MAX_INTERFACE_NUM];
Chris Packham1a07d212018-05-10 13:28:29 +12001241 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
1242 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
1243 unsigned int tclk;
Chris Packham3a09e132018-05-10 13:28:30 +12001244 enum mv_ddr_timing timing = tm->interface_params[if_id].timing;
Chris Packham4bf81db2018-12-03 14:26:49 +13001245 u32 freq = mv_ddr_freq_get(frequency);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001246
1247 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
1248 ("dev %d access %d IF %d freq %d\n", dev_num,
1249 access_type, if_id, frequency));
1250
Chris Packham4bf81db2018-12-03 14:26:49 +13001251 if (frequency == MV_DDR_FREQ_LOW_FREQ)
Stefan Roese5ffceb82015-03-26 15:36:56 +01001252 is_dll_off = 1;
1253 if (access_type == ACCESS_TYPE_MULTICAST) {
1254 start_if = 0;
1255 end_if = MAX_INTERFACE_NUM - 1;
1256 } else {
1257 start_if = if_id;
1258 end_if = if_id;
1259 }
1260
1261 /* calculate interface cs mask - Oferb 4/11 */
1262 /* speed bin can be different for each interface */
1263 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
1264 /* cs enable is active low */
Chris Packham1a07d212018-05-10 13:28:29 +12001265 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001266 cs_mask[if_id] = CS_BIT_MASK;
1267 training_result[training_stage][if_id] = TEST_SUCCESS;
1268 ddr3_tip_calc_cs_mask(dev_num, if_id, effective_cs,
1269 &cs_mask[if_id]);
1270 }
1271
1272 /* speed bin can be different for each interface */
1273 /*
1274 * moti b - need to remove the loop for multicas access functions
1275 * and loop the unicast access functions
1276 */
1277 for (if_id = start_if; if_id <= end_if; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001278 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001279
1280 flow_result[if_id] = TEST_SUCCESS;
1281 speed_bin_index =
1282 tm->interface_params[if_id].speed_bin_index;
1283 if (tm->interface_params[if_id].memory_freq ==
1284 frequency) {
1285 cl_value =
1286 tm->interface_params[if_id].cas_l;
1287 cwl_value =
1288 tm->interface_params[if_id].cas_wl;
Chris Packham1a07d212018-05-10 13:28:29 +12001289 } else if (tm->cfg_src == MV_DDR_CFG_SPD) {
Chris Packham4bf81db2018-12-03 14:26:49 +13001290 tclk = 1000000 / freq;
Chris Packham1a07d212018-05-10 13:28:29 +12001291 cl_value = mv_ddr_cl_calc(tm->timing_data[MV_DDR_TAA_MIN], tclk);
1292 if (cl_value == 0) {
1293 printf("mv_ddr: unsupported cas latency value found\n");
1294 return MV_FAIL;
1295 }
1296 cwl_value = mv_ddr_cwl_calc(tclk);
1297 if (cwl_value == 0) {
1298 printf("mv_ddr: unsupported cas write latency value found\n");
1299 return MV_FAIL;
1300 }
Stefan Roese5ffceb82015-03-26 15:36:56 +01001301 } else {
Chris Packham4bf81db2018-12-03 14:26:49 +13001302 cl_value = mv_ddr_cl_val_get(speed_bin_index, frequency);
1303 cwl_value = mv_ddr_cwl_val_get(speed_bin_index, frequency);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001304 }
1305
1306 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
1307 ("Freq_set dev 0x%x access 0x%x if 0x%x freq 0x%x speed %d:\n\t",
1308 dev_num, access_type, if_id,
1309 frequency, speed_bin_index));
1310
Chris Packham4bf81db2018-12-03 14:26:49 +13001311 for (cnt_id = 0; cnt_id < MV_DDR_FREQ_LAST; cnt_id++) {
Stefan Roese5ffceb82015-03-26 15:36:56 +01001312 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
Chris Packham4bf81db2018-12-03 14:26:49 +13001313 ("%d ", mv_ddr_cl_val_get(speed_bin_index, cnt_id)));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001314 }
1315
1316 DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE, ("\n"));
1317 mem_mask = 0;
Chris Packham1a07d212018-05-10 13:28:29 +12001318 for (bus_index = 0; bus_index < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001319 bus_index++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001320 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_index);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001321 mem_mask |=
1322 tm->interface_params[if_id].
1323 as_bus_params[bus_index].mirror_enable_bitmask;
1324 }
1325
1326 if (mem_mask != 0) {
Chris Packham1a07d212018-05-10 13:28:29 +12001327 /* motib redundent in KW28 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001328 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
1329 if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001330 DUAL_DUNIT_CFG_REG, 0, 0x8));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001331 }
1332
1333 /* dll state after exiting SR */
1334 if (is_dll_off == 1) {
1335 CHECK_STATUS(ddr3_tip_if_write
1336 (dev_num, access_type, if_id,
1337 DFS_REG, 0x1, 0x1));
1338 } else {
1339 CHECK_STATUS(ddr3_tip_if_write
1340 (dev_num, access_type, if_id,
1341 DFS_REG, 0, 0x1));
1342 }
1343
1344 CHECK_STATUS(ddr3_tip_if_write
1345 (dev_num, access_type, if_id,
1346 DUNIT_MMASK_REG, 0, 0x1));
1347 /* DFS - block transactions */
1348 CHECK_STATUS(ddr3_tip_if_write
1349 (dev_num, access_type, if_id,
1350 DFS_REG, 0x2, 0x2));
1351
1352 /* disable ODT in case of dll off */
1353 if (is_dll_off == 1) {
Tony Dinhe2c524b2023-01-18 19:03:04 -08001354#if defined(CONFIG_DDR4)
1355 CHECK_STATUS(ddr3_tip_if_read
1356 (dev_num, access_type, PARAM_NOT_CARE,
1357 0x1974, &g_rtt_nom_cs0, MASK_ALL_BITS));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001358 CHECK_STATUS(ddr3_tip_if_write
1359 (dev_num, access_type, if_id,
Tony Dinhe2c524b2023-01-18 19:03:04 -08001360 0x1974, 0, (0x7 << 8)));
1361 CHECK_STATUS(ddr3_tip_if_read
1362 (dev_num, access_type, PARAM_NOT_CARE,
1363 0x1A74, &g_rtt_nom_cs1, MASK_ALL_BITS));
1364 CHECK_STATUS(ddr3_tip_if_write
1365 (dev_num, access_type, if_id,
1366 0x1A74, 0, (0x7 << 8)));
1367#else /* CONFIG_DDR4 */
1368 CHECK_STATUS(ddr3_tip_if_write
1369 (dev_num, access_type, if_id,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001370 0x1874, 0, 0x244));
1371 CHECK_STATUS(ddr3_tip_if_write
1372 (dev_num, access_type, if_id,
1373 0x1884, 0, 0x244));
1374 CHECK_STATUS(ddr3_tip_if_write
1375 (dev_num, access_type, if_id,
1376 0x1894, 0, 0x244));
1377 CHECK_STATUS(ddr3_tip_if_write
1378 (dev_num, access_type, if_id,
1379 0x18a4, 0, 0x244));
Tony Dinhe2c524b2023-01-18 19:03:04 -08001380#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001381 }
1382
1383 /* DFS - Enter Self-Refresh */
1384 CHECK_STATUS(ddr3_tip_if_write
1385 (dev_num, access_type, if_id, DFS_REG, 0x4,
1386 0x4));
1387 /* polling on self refresh entry */
1388 if (ddr3_tip_if_polling(dev_num, ACCESS_TYPE_UNICAST,
1389 if_id, 0x8, 0x8, DFS_REG,
1390 MAX_POLLING_ITERATIONS) != MV_OK) {
1391 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1392 ("Freq_set: DDR3 poll failed on SR entry\n"));
1393 }
1394
Chris Packham1a07d212018-05-10 13:28:29 +12001395 /* Calculate 2T mode */
1396 if (mode_2t != 0xff) {
1397 t2t = mode_2t;
Chris Packham3a09e132018-05-10 13:28:30 +12001398 } else if (timing != MV_DDR_TIM_DEFAULT) {
1399 t2t = (timing == MV_DDR_TIM_2T) ? 1 : 0;
Chris Packham1a07d212018-05-10 13:28:29 +12001400 } else {
1401 /* Calculate number of CS per interface */
Chris Packham4bf81db2018-12-03 14:26:49 +13001402 cs_num = mv_ddr_cs_num_get();
Chris Packham1a07d212018-05-10 13:28:29 +12001403 t2t = (cs_num == 1) ? 0 : 1;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001404 }
1405
Chris Packham1a07d212018-05-10 13:28:29 +12001406 if (ddr3_tip_dev_attr_get(dev_num, MV_ATTR_INTERLEAVE_WA) == 1) {
1407 /* Use 1T mode if 1:1 ratio configured */
1408 if (config_func_info[dev_num].tip_get_clock_ratio(frequency) == 1) {
1409 /* Low freq*/
1410 CHECK_STATUS(ddr3_tip_if_write
1411 (dev_num, access_type, if_id,
1412 SDRAM_OPEN_PAGES_CTRL_REG, 0x0, 0x3C0));
1413 t2t = 0;
1414 } else {
1415 /* Middle or target freq */
1416 CHECK_STATUS(ddr3_tip_if_write
1417 (dev_num, access_type, if_id,
1418 SDRAM_OPEN_PAGES_CTRL_REG, 0x3C0, 0x3C0));
1419 }
1420 }
1421 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
1422 DUNIT_CTRL_LOW_REG, t2t << 3, 0x3 << 3));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001423
Chris Packham1a07d212018-05-10 13:28:29 +12001424 /* PLL configuration */
1425 config_func_info[dev_num].tip_set_freq_divider_func(dev_num, if_id,
1426 frequency);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001427
1428 /* DFS - CL/CWL/WR parameters after exiting SR */
1429 CHECK_STATUS(ddr3_tip_if_write
1430 (dev_num, access_type, if_id, DFS_REG,
1431 (cl_mask_table[cl_value] << 8), 0xf00));
1432 CHECK_STATUS(ddr3_tip_if_write
1433 (dev_num, access_type, if_id, DFS_REG,
1434 (cwl_mask_table[cwl_value] << 12), 0x7000));
Chris Packham5450f0c2018-01-18 17:16:10 +13001435
Chris Packham4bf81db2018-12-03 14:26:49 +13001436 t_ckclk = (MEGA / freq);
1437 t_wr = time_to_nclk(mv_ddr_speed_bin_timing_get
Chris Packham1a07d212018-05-10 13:28:29 +12001438 (speed_bin_index,
1439 SPEED_BIN_TWR), t_ckclk);
Chris Packham5450f0c2018-01-18 17:16:10 +13001440
Stefan Roese5ffceb82015-03-26 15:36:56 +01001441 CHECK_STATUS(ddr3_tip_if_write
1442 (dev_num, access_type, if_id, DFS_REG,
Chris Packham1a07d212018-05-10 13:28:29 +12001443 (twr_mask_table[t_wr] << 16), 0x70000));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001444
1445 /* Restore original RTT values if returning from DLL OFF mode */
1446 if (is_dll_off == 1) {
Tony Dinhe2c524b2023-01-18 19:03:04 -08001447#if defined(CONFIG_DDR4)
1448 CHECK_STATUS(ddr3_tip_if_write
1449 (dev_num, access_type, if_id,
1450 0x1974, g_rtt_nom_cs0, (0x7 << 8)));
1451 CHECK_STATUS(ddr3_tip_if_write
1452 (dev_num, access_type, if_id,
1453 0x1A74, g_rtt_nom_cs1, (0x7 << 8)));
1454
1455 mv_ddr4_mode_regs_init(dev_num);
1456#else /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001457 CHECK_STATUS(ddr3_tip_if_write
1458 (dev_num, access_type, if_id, 0x1874,
1459 g_dic | g_rtt_nom, 0x266));
1460 CHECK_STATUS(ddr3_tip_if_write
1461 (dev_num, access_type, if_id, 0x1884,
1462 g_dic | g_rtt_nom, 0x266));
1463 CHECK_STATUS(ddr3_tip_if_write
1464 (dev_num, access_type, if_id, 0x1894,
1465 g_dic | g_rtt_nom, 0x266));
1466 CHECK_STATUS(ddr3_tip_if_write
1467 (dev_num, access_type, if_id, 0x18a4,
1468 g_dic | g_rtt_nom, 0x266));
Tony Dinhe2c524b2023-01-18 19:03:04 -08001469#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001470 }
1471
Chris Packham1a07d212018-05-10 13:28:29 +12001472 /* Reset divider_b assert -> de-assert */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001473 CHECK_STATUS(ddr3_tip_if_write
1474 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001475 SDRAM_CFG_REG, 0, 0x10000000));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001476 mdelay(10);
1477 CHECK_STATUS(ddr3_tip_if_write
1478 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001479 SDRAM_CFG_REG, 0x10000000, 0x10000000));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001480
Chris Packham1a07d212018-05-10 13:28:29 +12001481 /* ADLL configuration function of process and frequency */
1482 CHECK_STATUS(config_func_info[dev_num].
1483 tip_get_freq_config_info_func(dev_num, frequency,
1484 &freq_config_info));
1485
Stefan Roese5ffceb82015-03-26 15:36:56 +01001486 /* TBD check milo5 using device ID ? */
Chris Packham1a07d212018-05-10 13:28:29 +12001487 for (bus_cnt = 0; bus_cnt < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001488 bus_cnt++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001489 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001490 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1491 (dev_num, ACCESS_TYPE_UNICAST,
1492 if_id, bus_cnt, DDR_PHY_DATA,
1493 0x92,
1494 freq_config_info.
1495 bw_per_freq << 8
1496 /*freq_mask[dev_num][frequency] << 8 */
1497 , 0x700));
1498 CHECK_STATUS(ddr3_tip_bus_read_modify_write
1499 (dev_num, ACCESS_TYPE_UNICAST, if_id,
1500 bus_cnt, DDR_PHY_DATA, 0x94,
1501 freq_config_info.rate_per_freq, 0x7));
1502 }
1503
Chris Packham1a07d212018-05-10 13:28:29 +12001504 /* Dunit to PHY drive post edge, ADLL reset assert -> de-assert */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001505 CHECK_STATUS(ddr3_tip_if_write
1506 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001507 DRAM_PHY_CFG_REG, 0,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001508 (0x80000000 | 0x40000000)));
Chris Packham4bf81db2018-12-03 14:26:49 +13001509 mdelay(100 / (freq / mv_ddr_freq_get(MV_DDR_FREQ_LOW_FREQ)));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001510 CHECK_STATUS(ddr3_tip_if_write
1511 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001512 DRAM_PHY_CFG_REG, (0x80000000 | 0x40000000),
Stefan Roese5ffceb82015-03-26 15:36:56 +01001513 (0x80000000 | 0x40000000)));
1514
1515 /* polling for ADLL Done */
1516 if (ddr3_tip_if_polling
1517 (dev_num, ACCESS_TYPE_UNICAST, if_id, 0x3ff03ff,
1518 0x3ff03ff, PHY_LOCK_STATUS_REG,
1519 MAX_POLLING_ITERATIONS) != MV_OK) {
1520 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1521 ("Freq_set: DDR3 poll failed(1)\n"));
1522 }
1523
1524 /* pup data_pup reset assert-> deassert */
1525 CHECK_STATUS(ddr3_tip_if_write
1526 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001527 SDRAM_CFG_REG, 0, 0x60000000));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001528 mdelay(10);
1529 CHECK_STATUS(ddr3_tip_if_write
1530 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001531 SDRAM_CFG_REG, 0x60000000, 0x60000000));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001532
1533 /* Set proper timing params before existing Self-Refresh */
1534 ddr3_tip_set_timing(dev_num, access_type, if_id, frequency);
1535 if (delay_enable != 0) {
Chris Packham4bf81db2018-12-03 14:26:49 +13001536 adll_tap = (is_dll_off == 1) ? 1000 : (MEGA / (freq * 64));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001537 ddr3_tip_cmd_addr_init_delay(dev_num, adll_tap);
1538 }
1539
1540 /* Exit SR */
1541 CHECK_STATUS(ddr3_tip_if_write
1542 (dev_num, access_type, if_id, DFS_REG, 0,
1543 0x4));
1544 if (ddr3_tip_if_polling
1545 (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x8, DFS_REG,
1546 MAX_POLLING_ITERATIONS) != MV_OK) {
1547 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1548 ("Freq_set: DDR3 poll failed(2)"));
1549 }
1550
1551 /* Refresh Command */
1552 CHECK_STATUS(ddr3_tip_if_write
1553 (dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001554 SDRAM_OP_REG, 0x2, 0xf1f));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001555 if (ddr3_tip_if_polling
1556 (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x1f,
Chris Packham1a07d212018-05-10 13:28:29 +12001557 SDRAM_OP_REG, MAX_POLLING_ITERATIONS) != MV_OK) {
Stefan Roese5ffceb82015-03-26 15:36:56 +01001558 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1559 ("Freq_set: DDR3 poll failed(3)"));
1560 }
1561
1562 /* Release DFS Block */
1563 CHECK_STATUS(ddr3_tip_if_write
1564 (dev_num, access_type, if_id, DFS_REG, 0,
1565 0x2));
1566 /* Controller to MBUS Retry - normal */
1567 CHECK_STATUS(ddr3_tip_if_write
1568 (dev_num, access_type, if_id, DUNIT_MMASK_REG,
1569 0x1, 0x1));
1570
1571 /* MRO: Burst Length 8, CL , Auto_precharge 0x16cc */
1572 val =
1573 ((cl_mask_table[cl_value] & 0x1) << 2) |
1574 ((cl_mask_table[cl_value] & 0xe) << 3);
1575 CHECK_STATUS(ddr3_tip_if_write
1576 (dev_num, access_type, if_id, MR0_REG,
1577 val, (0x7 << 4) | (1 << 2)));
1578 /* MR2: CWL = 10 , Auto Self-Refresh - disable */
Chris Packham1a07d212018-05-10 13:28:29 +12001579 val = (cwl_mask_table[cwl_value] << 3) | g_rtt_wr;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001580 /*
1581 * nklein 24.10.13 - should not be here - leave value as set in
1582 * the init configuration val |= (1 << 9);
1583 * val |= ((tm->interface_params[if_id].
Chris Packham1a07d212018-05-10 13:28:29 +12001584 * interface_temp == MV_DDR_TEMP_HIGH) ? (1 << 7) : 0);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001585 */
1586 /* nklein 24.10.13 - see above comment */
1587 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
1588 if_id, MR2_REG,
Chris Packham1a07d212018-05-10 13:28:29 +12001589 val, (0x7 << 3) | (0x3 << 9)));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001590
1591 /* ODT TIMING */
1592 val = ((cl_value - cwl_value + 1) << 4) |
1593 ((cl_value - cwl_value + 6) << 8) |
1594 ((cl_value - 1) << 12) | ((cl_value + 6) << 16);
1595 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
Chris Packham1a07d212018-05-10 13:28:29 +12001596 if_id, DDR_ODT_TIMING_LOW_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001597 val, 0xffff0));
Chris Packhamae806142018-01-18 17:16:07 +13001598 val = 0x91 | ((cwl_value - 1) << 8) | ((cwl_value + 5) << 12);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001599 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
Chris Packham1a07d212018-05-10 13:28:29 +12001600 if_id, DDR_ODT_TIMING_HIGH_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001601 val, 0xffff));
1602
Chris Packham1a07d212018-05-10 13:28:29 +12001603 /* in case of ddr4 need to set the receiver to odt always 'on' (odt_config = '0')
1604 * in case of ddr3 configure the odt through the timing
1605 */
1606 if (odt_config != 0) {
1607 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id, DUNIT_ODT_CTRL_REG, 0xf, 0xf));
1608 }
1609 else {
1610 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id, DUNIT_ODT_CTRL_REG,
1611 0x30f, 0x30f));
1612 }
Stefan Roese5ffceb82015-03-26 15:36:56 +01001613
1614 /* re-write CL */
1615 val = ((cl_mask_table[cl_value] & 0x1) << 2) |
1616 ((cl_mask_table[cl_value] & 0xe) << 3);
Chris Packham1a07d212018-05-10 13:28:29 +12001617
Marek Behúnb9eaf112021-02-19 17:11:23 +01001618 cs_mask[0] = 0xc;
1619
Chris Packham1a07d212018-05-10 13:28:29 +12001620 CHECK_STATUS(ddr3_tip_write_mrs_cmd(dev_num, cs_mask, MR_CMD0,
1621 val, (0x7 << 4) | (0x1 << 2)));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001622
1623 /* re-write CWL */
Chris Packham1a07d212018-05-10 13:28:29 +12001624 val = (cwl_mask_table[cwl_value] << 3) | g_rtt_wr;
1625 CHECK_STATUS(ddr3_tip_write_mrs_cmd(dev_num, cs_mask, MR_CMD2,
1626 val, (0x7 << 3) | (0x3 << 9)));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001627
1628 if (mem_mask != 0) {
1629 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
1630 if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001631 DUAL_DUNIT_CFG_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001632 1 << 3, 0x8));
1633 }
1634 }
1635
1636 return MV_OK;
1637}
1638
1639/*
1640 * Set ODT values
1641 */
1642static int ddr3_tip_write_odt(u32 dev_num, enum hws_access_type access_type,
1643 u32 if_id, u32 cl_value, u32 cwl_value)
1644{
1645 /* ODT TIMING */
1646 u32 val = (cl_value - cwl_value + 6);
1647
1648 val = ((cl_value - cwl_value + 1) << 4) | ((val & 0xf) << 8) |
1649 (((cl_value - 1) & 0xf) << 12) |
1650 (((cl_value + 6) & 0xf) << 16) | (((val & 0x10) >> 4) << 21);
1651 val |= (((cl_value - 1) >> 4) << 22) | (((cl_value + 6) >> 4) << 23);
1652
1653 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001654 DDR_ODT_TIMING_LOW_REG, val, 0xffff0));
Chris Packhamae806142018-01-18 17:16:07 +13001655 val = 0x91 | ((cwl_value - 1) << 8) | ((cwl_value + 5) << 12);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001656 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001657 DDR_ODT_TIMING_HIGH_REG, val, 0xffff));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001658 if (odt_additional == 1) {
1659 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
1660 if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001661 SDRAM_ODT_CTRL_HIGH_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001662 0xf, 0xf));
1663 }
1664
1665 /* ODT Active */
1666 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001667 DUNIT_ODT_CTRL_REG, 0xf, 0xf));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001668
1669 return MV_OK;
1670}
1671
1672/*
1673 * Set Timing values for training
1674 */
1675static int ddr3_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
Chris Packham4bf81db2018-12-03 14:26:49 +13001676 u32 if_id, enum mv_ddr_freq frequency)
Stefan Roese5ffceb82015-03-26 15:36:56 +01001677{
1678 u32 t_ckclk = 0, t_ras = 0;
1679 u32 t_rcd = 0, t_rp = 0, t_wr = 0, t_wtr = 0, t_rrd = 0, t_rtp = 0,
Chris Packham1a07d212018-05-10 13:28:29 +12001680 t_rfc = 0, t_mod = 0, t_r2r = 0x3, t_r2r_high = 0,
1681 t_r2w_w2r = 0x3, t_r2w_w2r_high = 0x1, t_w2w = 0x3;
1682 u32 refresh_interval_cnt, t_hclk, t_refi, t_faw, t_pd, t_xpdll;
1683 u32 val = 0, page_size = 0, mask = 0;
Chris Packham4bf81db2018-12-03 14:26:49 +13001684 enum mv_ddr_speed_bin speed_bin_index;
Chris Packham1a07d212018-05-10 13:28:29 +12001685 enum mv_ddr_die_capacity memory_size = MV_DDR_DIE_CAP_2GBIT;
1686 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Chris Packham4bf81db2018-12-03 14:26:49 +13001687 u32 freq = mv_ddr_freq_get(frequency);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001688
1689 speed_bin_index = tm->interface_params[if_id].speed_bin_index;
1690 memory_size = tm->interface_params[if_id].memory_size;
Chris Packham4bf81db2018-12-03 14:26:49 +13001691 page_size = mv_ddr_page_size_get(tm->interface_params[if_id].bus_width, memory_size);
1692 t_ckclk = (MEGA / freq);
Chris Packham1a07d212018-05-10 13:28:29 +12001693 /* HCLK in[ps] */
Chris Packham4bf81db2018-12-03 14:26:49 +13001694 t_hclk = MEGA / (freq / config_func_info[dev_num].tip_get_clock_ratio(frequency));
Chris Packham1a07d212018-05-10 13:28:29 +12001695
1696 t_refi = (tm->interface_params[if_id].interface_temp == MV_DDR_TEMP_HIGH) ? TREFI_HIGH : TREFI_LOW;
1697 t_refi *= 1000; /* psec */
1698 refresh_interval_cnt = t_refi / t_hclk; /* no units */
1699
1700 if (page_size == 1) {
Chris Packham4bf81db2018-12-03 14:26:49 +13001701 t_faw = mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TFAW1K);
Chris Packham1a07d212018-05-10 13:28:29 +12001702 t_faw = time_to_nclk(t_faw, t_ckclk);
1703 t_faw = GET_MAX_VALUE(20, t_faw);
1704 } else { /* page size =2, we do not support page size 0.5k */
Chris Packham4bf81db2018-12-03 14:26:49 +13001705 t_faw = mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TFAW2K);
Chris Packham1a07d212018-05-10 13:28:29 +12001706 t_faw = time_to_nclk(t_faw, t_ckclk);
1707 t_faw = GET_MAX_VALUE(28, t_faw);
1708 }
1709
Chris Packham4bf81db2018-12-03 14:26:49 +13001710 t_pd = GET_MAX_VALUE(t_ckclk * 3, mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TPD));
Chris Packham1a07d212018-05-10 13:28:29 +12001711 t_pd = time_to_nclk(t_pd, t_ckclk);
1712
Chris Packham4bf81db2018-12-03 14:26:49 +13001713 t_xpdll = GET_MAX_VALUE(t_ckclk * 10, mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TXPDLL));
Chris Packham1a07d212018-05-10 13:28:29 +12001714 t_xpdll = time_to_nclk(t_xpdll, t_ckclk);
1715
Chris Packham4bf81db2018-12-03 14:26:49 +13001716 t_rrd = (page_size == 1) ? mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001717 SPEED_BIN_TRRD1K) :
Chris Packham4bf81db2018-12-03 14:26:49 +13001718 mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TRRD2K);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001719 t_rrd = GET_MAX_VALUE(t_ckclk * 4, t_rrd);
Chris Packham4bf81db2018-12-03 14:26:49 +13001720 t_rtp = GET_MAX_VALUE(t_ckclk * 4, mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001721 SPEED_BIN_TRTP));
Chris Packham1a07d212018-05-10 13:28:29 +12001722 t_mod = GET_MAX_VALUE(t_ckclk * 12, 15000);
Tony Dinhe2c524b2023-01-18 19:03:04 -08001723#if defined(CONFIG_DDR4)
1724 t_wtr = GET_MAX_VALUE(t_ckclk * 2, mv_ddr_speed_bin_timing_get(speed_bin_index,
1725 SPEED_BIN_TWTR));
1726#else /* CONFIG_DDR4 */
Chris Packham4bf81db2018-12-03 14:26:49 +13001727 t_wtr = GET_MAX_VALUE(t_ckclk * 4, mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001728 SPEED_BIN_TWTR));
Tony Dinhe2c524b2023-01-18 19:03:04 -08001729#endif /* CONFIG_DDR4 */
Chris Packham4bf81db2018-12-03 14:26:49 +13001730 t_ras = time_to_nclk(mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001731 SPEED_BIN_TRAS),
1732 t_ckclk);
Chris Packham4bf81db2018-12-03 14:26:49 +13001733 t_rcd = time_to_nclk(mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001734 SPEED_BIN_TRCD),
1735 t_ckclk);
Chris Packham4bf81db2018-12-03 14:26:49 +13001736 t_rp = time_to_nclk(mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001737 SPEED_BIN_TRP),
1738 t_ckclk);
Chris Packham4bf81db2018-12-03 14:26:49 +13001739 t_wr = time_to_nclk(mv_ddr_speed_bin_timing_get(speed_bin_index,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001740 SPEED_BIN_TWR),
1741 t_ckclk);
Chris Packham1a07d212018-05-10 13:28:29 +12001742 t_wtr = time_to_nclk(t_wtr, t_ckclk);
1743 t_rrd = time_to_nclk(t_rrd, t_ckclk);
1744 t_rtp = time_to_nclk(t_rtp, t_ckclk);
Chris Packham4bf81db2018-12-03 14:26:49 +13001745 t_rfc = time_to_nclk(mv_ddr_rfc_get(memory_size) * 1000, t_ckclk);
Chris Packham1a07d212018-05-10 13:28:29 +12001746 t_mod = time_to_nclk(t_mod, t_ckclk);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001747
1748 /* SDRAM Timing Low */
Chris Packham1a07d212018-05-10 13:28:29 +12001749 val = (((t_ras - 1) & SDRAM_TIMING_LOW_TRAS_MASK) << SDRAM_TIMING_LOW_TRAS_OFFS) |
1750 (((t_rcd - 1) & SDRAM_TIMING_LOW_TRCD_MASK) << SDRAM_TIMING_LOW_TRCD_OFFS) |
1751 (((t_rcd - 1) >> SDRAM_TIMING_LOW_TRCD_OFFS & SDRAM_TIMING_HIGH_TRCD_MASK)
1752 << SDRAM_TIMING_HIGH_TRCD_OFFS) |
1753 (((t_rp - 1) & SDRAM_TIMING_LOW_TRP_MASK) << SDRAM_TIMING_LOW_TRP_OFFS) |
1754 (((t_rp - 1) >> SDRAM_TIMING_LOW_TRP_MASK & SDRAM_TIMING_HIGH_TRP_MASK)
1755 << SDRAM_TIMING_HIGH_TRP_OFFS) |
1756 (((t_wr - 1) & SDRAM_TIMING_LOW_TWR_MASK) << SDRAM_TIMING_LOW_TWR_OFFS) |
1757 (((t_wtr - 1) & SDRAM_TIMING_LOW_TWTR_MASK) << SDRAM_TIMING_LOW_TWTR_OFFS) |
1758 ((((t_ras - 1) >> 4) & SDRAM_TIMING_LOW_TRAS_HIGH_MASK) << SDRAM_TIMING_LOW_TRAS_HIGH_OFFS) |
1759 (((t_rrd - 1) & SDRAM_TIMING_LOW_TRRD_MASK) << SDRAM_TIMING_LOW_TRRD_OFFS) |
1760 (((t_rtp - 1) & SDRAM_TIMING_LOW_TRTP_MASK) << SDRAM_TIMING_LOW_TRTP_OFFS);
1761
1762 mask = (SDRAM_TIMING_LOW_TRAS_MASK << SDRAM_TIMING_LOW_TRAS_OFFS) |
1763 (SDRAM_TIMING_LOW_TRCD_MASK << SDRAM_TIMING_LOW_TRCD_OFFS) |
1764 (SDRAM_TIMING_HIGH_TRCD_MASK << SDRAM_TIMING_HIGH_TRCD_OFFS) |
1765 (SDRAM_TIMING_LOW_TRP_MASK << SDRAM_TIMING_LOW_TRP_OFFS) |
1766 (SDRAM_TIMING_HIGH_TRP_MASK << SDRAM_TIMING_HIGH_TRP_OFFS) |
1767 (SDRAM_TIMING_LOW_TWR_MASK << SDRAM_TIMING_LOW_TWR_OFFS) |
1768 (SDRAM_TIMING_LOW_TWTR_MASK << SDRAM_TIMING_LOW_TWTR_OFFS) |
1769 (SDRAM_TIMING_LOW_TRAS_HIGH_MASK << SDRAM_TIMING_LOW_TRAS_HIGH_OFFS) |
1770 (SDRAM_TIMING_LOW_TRRD_MASK << SDRAM_TIMING_LOW_TRRD_OFFS) |
1771 (SDRAM_TIMING_LOW_TRTP_MASK << SDRAM_TIMING_LOW_TRTP_OFFS);
1772
Stefan Roese5ffceb82015-03-26 15:36:56 +01001773 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001774 SDRAM_TIMING_LOW_REG, val, mask));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001775
1776 /* SDRAM Timing High */
Chris Packham1a07d212018-05-10 13:28:29 +12001777 val = 0;
1778 mask = 0;
1779
1780 val = (((t_rfc - 1) & SDRAM_TIMING_HIGH_TRFC_MASK) << SDRAM_TIMING_HIGH_TRFC_OFFS) |
1781 ((t_r2r & SDRAM_TIMING_HIGH_TR2R_MASK) << SDRAM_TIMING_HIGH_TR2R_OFFS) |
1782 ((t_r2w_w2r & SDRAM_TIMING_HIGH_TR2W_W2R_MASK) << SDRAM_TIMING_HIGH_TR2W_W2R_OFFS) |
1783 ((t_w2w & SDRAM_TIMING_HIGH_TW2W_MASK) << SDRAM_TIMING_HIGH_TW2W_OFFS) |
1784 ((((t_rfc - 1) >> 7) & SDRAM_TIMING_HIGH_TRFC_HIGH_MASK) << SDRAM_TIMING_HIGH_TRFC_HIGH_OFFS) |
1785 ((t_r2r_high & SDRAM_TIMING_HIGH_TR2R_HIGH_MASK) << SDRAM_TIMING_HIGH_TR2R_HIGH_OFFS) |
1786 ((t_r2w_w2r_high & SDRAM_TIMING_HIGH_TR2W_W2R_HIGH_MASK) << SDRAM_TIMING_HIGH_TR2W_W2R_HIGH_OFFS) |
1787 (((t_mod - 1) & SDRAM_TIMING_HIGH_TMOD_MASK) << SDRAM_TIMING_HIGH_TMOD_OFFS) |
1788 ((((t_mod - 1) >> 4) & SDRAM_TIMING_HIGH_TMOD_HIGH_MASK) << SDRAM_TIMING_HIGH_TMOD_HIGH_OFFS);
1789
1790 mask = (SDRAM_TIMING_HIGH_TRFC_MASK << SDRAM_TIMING_HIGH_TRFC_OFFS) |
1791 (SDRAM_TIMING_HIGH_TR2R_MASK << SDRAM_TIMING_HIGH_TR2R_OFFS) |
1792 (SDRAM_TIMING_HIGH_TR2W_W2R_MASK << SDRAM_TIMING_HIGH_TR2W_W2R_OFFS) |
1793 (SDRAM_TIMING_HIGH_TW2W_MASK << SDRAM_TIMING_HIGH_TW2W_OFFS) |
1794 (SDRAM_TIMING_HIGH_TRFC_HIGH_MASK << SDRAM_TIMING_HIGH_TRFC_HIGH_OFFS) |
1795 (SDRAM_TIMING_HIGH_TR2R_HIGH_MASK << SDRAM_TIMING_HIGH_TR2R_HIGH_OFFS) |
1796 (SDRAM_TIMING_HIGH_TR2W_W2R_HIGH_MASK << SDRAM_TIMING_HIGH_TR2W_W2R_HIGH_OFFS) |
1797 (SDRAM_TIMING_HIGH_TMOD_MASK << SDRAM_TIMING_HIGH_TMOD_OFFS) |
1798 (SDRAM_TIMING_HIGH_TMOD_HIGH_MASK << SDRAM_TIMING_HIGH_TMOD_HIGH_OFFS);
1799
Stefan Roese5ffceb82015-03-26 15:36:56 +01001800 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001801 SDRAM_TIMING_HIGH_REG, val, mask));
1802
Stefan Roese5ffceb82015-03-26 15:36:56 +01001803 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001804 SDRAM_CFG_REG,
1805 refresh_interval_cnt << REFRESH_OFFS,
1806 REFRESH_MASK << REFRESH_OFFS));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001807 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001808 SDRAM_ADDR_CTRL_REG, (t_faw - 1) << T_FAW_OFFS,
1809 T_FAW_MASK << T_FAW_OFFS));
1810
1811 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id, DDR_TIMING_REG,
1812 (t_pd - 1) << DDR_TIMING_TPD_OFFS |
1813 (t_xpdll - 1) << DDR_TIMING_TXPDLL_OFFS,
1814 DDR_TIMING_TPD_MASK << DDR_TIMING_TPD_OFFS |
1815 DDR_TIMING_TXPDLL_MASK << DDR_TIMING_TXPDLL_OFFS));
1816
Tony Dinhe2c524b2023-01-18 19:03:04 -08001817#if defined(CONFIG_DDR4)
1818 ddr4_tip_set_timing(dev_num, access_type, if_id, frequency);
1819#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01001820
1821 return MV_OK;
1822}
1823
Tony Dinhe2c524b2023-01-18 19:03:04 -08001824#if defined(CONFIG_DDR4)
1825static int ddr4_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
1826 u32 if_id, enum mv_ddr_freq frequency)
1827{
1828 u32 t_rrd_l = 0, t_wtr_l = 0, t_ckclk = 0, t_mod = 0, t_ccd = 0;
1829 u32 page_size = 0, val = 0, mask = 0;
1830 enum mv_ddr_speed_bin speed_bin_index;
1831 enum mv_ddr_die_capacity memory_size;
1832 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
1833 u32 freq = mv_ddr_freq_get(frequency);
1834
1835 speed_bin_index = tm->interface_params[if_id].speed_bin_index;
1836 memory_size = tm->interface_params[if_id].memory_size;
1837 page_size = mv_ddr_page_size_get(tm->interface_params[if_id].bus_width, memory_size);
1838
1839 t_ckclk = (MEGA / freq);
1840
1841 t_rrd_l = (page_size == 1) ? mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TRRDL1K) :
1842 mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TRRDL2K);
1843 t_rrd_l = GET_MAX_VALUE(t_ckclk * 4, t_rrd_l);
1844
1845 t_wtr_l = mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TWTRL);
1846 t_wtr_l = GET_MAX_VALUE(t_ckclk * 4, t_wtr_l);
1847
1848 t_rrd_l = time_to_nclk(t_rrd_l, t_ckclk);
1849 t_wtr_l = time_to_nclk(t_wtr_l, t_ckclk);
1850
1851 val = (((t_rrd_l - 1) & DDR4_TRRD_L_MASK) << DDR4_TRRD_L_OFFS) |
1852 (((t_wtr_l - 1) & DDR4_TWTR_L_MASK) << DDR4_TWTR_L_OFFS);
1853 mask = (DDR4_TRRD_L_MASK << DDR4_TRRD_L_OFFS) |
1854 (DDR4_TWTR_L_MASK << DDR4_TWTR_L_OFFS);
1855 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
1856 DRAM_LONG_TIMING_REG, val, mask));
1857
1858 val = 0;
1859 mask = 0;
1860 t_mod = mv_ddr_speed_bin_timing_get(speed_bin_index, SPEED_BIN_TMOD);
1861 t_mod = GET_MAX_VALUE(t_ckclk * 24, t_mod);
1862 t_mod = time_to_nclk(t_mod, t_ckclk);
1863
1864 val = (((t_mod - 1) & SDRAM_TIMING_HIGH_TMOD_MASK) << SDRAM_TIMING_HIGH_TMOD_OFFS) |
1865 ((((t_mod - 1) >> 4) & SDRAM_TIMING_HIGH_TMOD_HIGH_MASK) << SDRAM_TIMING_HIGH_TMOD_HIGH_OFFS);
1866 mask = (SDRAM_TIMING_HIGH_TMOD_MASK << SDRAM_TIMING_HIGH_TMOD_OFFS) |
1867 (SDRAM_TIMING_HIGH_TMOD_HIGH_MASK << SDRAM_TIMING_HIGH_TMOD_HIGH_OFFS);
1868 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
1869 SDRAM_TIMING_HIGH_REG, val, mask));
1870
1871 t_ccd = 6;
1872
1873 CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
1874 DDR_TIMING_REG,
1875 ((t_ccd - 1) & DDR_TIMING_TCCD_MASK) << DDR_TIMING_TCCD_OFFS,
1876 DDR_TIMING_TCCD_MASK << DDR_TIMING_TCCD_OFFS));
1877
1878 return MV_OK;
1879}
1880#endif /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +12001881
Stefan Roese5ffceb82015-03-26 15:36:56 +01001882/*
Stefan Roese5ffceb82015-03-26 15:36:56 +01001883 * Write CS Result
1884 */
1885int ddr3_tip_write_cs_result(u32 dev_num, u32 offset)
1886{
1887 u32 if_id, bus_num, cs_bitmask, data_val, cs_num;
Chris Packham1a07d212018-05-10 13:28:29 +12001888 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
1889 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01001890
1891 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001892 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
1893 for (bus_num = 0; bus_num < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +01001894 bus_num++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001895 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001896 cs_bitmask =
1897 tm->interface_params[if_id].
1898 as_bus_params[bus_num].cs_bitmask;
1899 if (cs_bitmask != effective_cs) {
1900 cs_num = GET_CS_FROM_MASK(cs_bitmask);
1901 ddr3_tip_bus_read(dev_num, if_id,
1902 ACCESS_TYPE_UNICAST, bus_num,
1903 DDR_PHY_DATA,
1904 offset +
Chris Packham1a07d212018-05-10 13:28:29 +12001905 (effective_cs * 0x4),
Stefan Roese5ffceb82015-03-26 15:36:56 +01001906 &data_val);
1907 ddr3_tip_bus_write(dev_num,
1908 ACCESS_TYPE_UNICAST,
1909 if_id,
1910 ACCESS_TYPE_UNICAST,
1911 bus_num, DDR_PHY_DATA,
1912 offset +
Chris Packham1a07d212018-05-10 13:28:29 +12001913 (cs_num * 0x4),
Stefan Roese5ffceb82015-03-26 15:36:56 +01001914 data_val);
1915 }
1916 }
1917 }
1918
1919 return MV_OK;
1920}
1921
1922/*
1923 * Write MRS
1924 */
Chris Packham1a07d212018-05-10 13:28:29 +12001925int ddr3_tip_write_mrs_cmd(u32 dev_num, u32 *cs_mask_arr, enum mr_number mr_num, u32 data, u32 mask)
Stefan Roese5ffceb82015-03-26 15:36:56 +01001926{
Chris Packham1a07d212018-05-10 13:28:29 +12001927 u32 if_id;
1928 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01001929
Stefan Roese5ffceb82015-03-26 15:36:56 +01001930 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +12001931 PARAM_NOT_CARE, mr_data[mr_num].reg_addr, data, mask));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001932 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001933 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001934 CHECK_STATUS(ddr3_tip_if_write
1935 (dev_num, ACCESS_TYPE_UNICAST, if_id,
Chris Packham1a07d212018-05-10 13:28:29 +12001936 SDRAM_OP_REG,
1937 (cs_mask_arr[if_id] << 8) | mr_data[mr_num].cmd, 0xf1f));
Stefan Roese5ffceb82015-03-26 15:36:56 +01001938 }
1939
1940 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12001941 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01001942 if (ddr3_tip_if_polling(dev_num, ACCESS_TYPE_UNICAST, if_id, 0,
Chris Packham1a07d212018-05-10 13:28:29 +12001943 0x1f, SDRAM_OP_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01001944 MAX_POLLING_ITERATIONS) != MV_OK) {
1945 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
1946 ("write_mrs_cmd: Poll cmd fail"));
1947 }
1948 }
1949
1950 return MV_OK;
1951}
1952
1953/*
1954 * Reset XSB Read FIFO
1955 */
1956int ddr3_tip_reset_fifo_ptr(u32 dev_num)
1957{
1958 u32 if_id = 0;
1959
1960 /* Configure PHY reset value to 0 in order to "clean" the FIFO */
1961 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1962 if_id, 0x15c8, 0, 0xff000000));
1963 /*
1964 * Move PHY to RL mode (only in RL mode the PHY overrides FIFO values
1965 * during FIFO reset)
1966 */
1967 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1968 if_id, TRAINING_SW_2_REG,
1969 0x1, 0x9));
1970 /* In order that above configuration will influence the PHY */
1971 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1972 if_id, 0x15b0,
1973 0x80000000, 0x80000000));
1974 /* Reset read fifo assertion */
1975 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1976 if_id, 0x1400, 0, 0x40000000));
1977 /* Reset read fifo deassertion */
1978 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1979 if_id, 0x1400,
1980 0x40000000, 0x40000000));
1981 /* Move PHY back to functional mode */
1982 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1983 if_id, TRAINING_SW_2_REG,
1984 0x8, 0x9));
1985 /* Stop training machine */
1986 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
1987 if_id, 0x15b4, 0x10000, 0x10000));
1988
1989 return MV_OK;
1990}
1991
1992/*
1993 * Reset Phy registers
1994 */
1995int ddr3_tip_ddr3_reset_phy_regs(u32 dev_num)
1996{
1997 u32 if_id, phy_id, cs;
Chris Packham1a07d212018-05-10 13:28:29 +12001998 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
1999 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002000
2001 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12002002 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
2003 for (phy_id = 0; phy_id < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +01002004 phy_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12002005 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, phy_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002006 CHECK_STATUS(ddr3_tip_bus_write
2007 (dev_num, ACCESS_TYPE_UNICAST,
2008 if_id, ACCESS_TYPE_UNICAST,
2009 phy_id, DDR_PHY_DATA,
Chris Packham1a07d212018-05-10 13:28:29 +12002010 WL_PHY_REG(effective_cs),
Stefan Roese5ffceb82015-03-26 15:36:56 +01002011 phy_reg0_val));
2012 CHECK_STATUS(ddr3_tip_bus_write
2013 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2014 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
Chris Packham1a07d212018-05-10 13:28:29 +12002015 RL_PHY_REG(effective_cs),
Stefan Roese5ffceb82015-03-26 15:36:56 +01002016 phy_reg2_val));
2017 CHECK_STATUS(ddr3_tip_bus_write
2018 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2019 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
Chris Packham1a07d212018-05-10 13:28:29 +12002020 CRX_PHY_REG(effective_cs), phy_reg3_val));
2021 CHECK_STATUS(ddr3_tip_bus_write
2022 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2023 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2024 CTX_PHY_REG(effective_cs), phy_reg1_val));
2025 CHECK_STATUS(ddr3_tip_bus_write
2026 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2027 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2028 PBS_TX_BCAST_PHY_REG(effective_cs), 0x0));
2029 CHECK_STATUS(ddr3_tip_bus_write
2030 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2031 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2032 PBS_RX_BCAST_PHY_REG(effective_cs), 0));
2033 CHECK_STATUS(ddr3_tip_bus_write
2034 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2035 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2036 PBS_TX_PHY_REG(effective_cs, DQSP_PAD), 0));
2037 CHECK_STATUS(ddr3_tip_bus_write
2038 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2039 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2040 PBS_RX_PHY_REG(effective_cs, DQSP_PAD), 0));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002041 CHECK_STATUS(ddr3_tip_bus_write
2042 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2043 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
Chris Packham1a07d212018-05-10 13:28:29 +12002044 PBS_TX_PHY_REG(effective_cs, DQSN_PAD), 0));
2045 CHECK_STATUS(ddr3_tip_bus_write
2046 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2047 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2048 PBS_RX_PHY_REG(effective_cs, DQSN_PAD), 0));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002049 }
2050 }
2051
2052 /* Set Receiver Calibration value */
2053 for (cs = 0; cs < MAX_CS_NUM; cs++) {
2054 /* PHY register 0xdb bits[5:0] - configure to 63 */
2055 CHECK_STATUS(ddr3_tip_bus_write
2056 (dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
2057 ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
Chris Packham1a07d212018-05-10 13:28:29 +12002058 DDR_PHY_DATA, VREF_BCAST_PHY_REG(cs), 63));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002059 }
2060
2061 return MV_OK;
2062}
2063
2064/*
2065 * Restore Dunit registers
2066 */
2067int ddr3_tip_restore_dunit_regs(u32 dev_num)
2068{
2069 u32 index_cnt;
2070
Chris Packham1a07d212018-05-10 13:28:29 +12002071 mv_ddr_set_calib_controller();
2072
Stefan Roese5ffceb82015-03-26 15:36:56 +01002073 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +12002074 PARAM_NOT_CARE, MAIN_PADS_CAL_MACH_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01002075 0x1, 0x1));
2076 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +12002077 PARAM_NOT_CARE, MAIN_PADS_CAL_MACH_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01002078 calibration_update_control << 3,
2079 0x3 << 3));
2080 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
2081 PARAM_NOT_CARE,
Chris Packham1a07d212018-05-10 13:28:29 +12002082 ODPG_WR_RD_MODE_ENA_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01002083 0xffff, MASK_ALL_BITS));
2084
2085 for (index_cnt = 0; index_cnt < ARRAY_SIZE(odpg_default_value);
2086 index_cnt++) {
2087 CHECK_STATUS(ddr3_tip_if_write
2088 (dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
2089 odpg_default_value[index_cnt].reg_addr,
2090 odpg_default_value[index_cnt].reg_data,
2091 odpg_default_value[index_cnt].reg_mask));
2092 }
2093
2094 return MV_OK;
2095}
2096
Chris Packham1a07d212018-05-10 13:28:29 +12002097int ddr3_tip_adll_regs_bypass(u32 dev_num, u32 reg_val1, u32 reg_val2)
2098{
2099 u32 if_id, phy_id;
2100 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
2101 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2102
2103 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
2104 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
2105 for (phy_id = 0; phy_id < octets_per_if_num; phy_id++) {
2106 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, phy_id);
2107 CHECK_STATUS(ddr3_tip_bus_write
2108 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2109 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2110 CTX_PHY_REG(effective_cs), reg_val1));
2111 CHECK_STATUS(ddr3_tip_bus_write
2112 (dev_num, ACCESS_TYPE_UNICAST, if_id,
2113 ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
2114 PBS_TX_BCAST_PHY_REG(effective_cs), reg_val2));
2115 }
2116 }
2117
2118 return MV_OK;
2119}
2120
Stefan Roese5ffceb82015-03-26 15:36:56 +01002121/*
2122 * Auto tune main flow
2123 */
2124static int ddr3_tip_ddr3_training_main_flow(u32 dev_num)
2125{
Chris Packham1a07d212018-05-10 13:28:29 +12002126/* TODO: enable this functionality for other platforms */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002127 struct init_cntr_param init_cntr_prm;
2128 int ret = MV_OK;
Chris Packham1a07d212018-05-10 13:28:29 +12002129 int adll_bypass_flag = 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +01002130 u32 if_id;
Chris Packham4bf81db2018-12-03 14:26:49 +13002131 unsigned int max_cs = mv_ddr_cs_num_get();
Chris Packham1a07d212018-05-10 13:28:29 +12002132 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Chris Packham4bf81db2018-12-03 14:26:49 +13002133 enum mv_ddr_freq freq = tm->interface_params[0].memory_freq;
2134 unsigned int *freq_tbl = mv_ddr_freq_tbl_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002135
Chris Packham1a07d212018-05-10 13:28:29 +12002136#ifdef DDR_VIEWER_TOOL
Stefan Roese5ffceb82015-03-26 15:36:56 +01002137 if (debug_training == DEBUG_LEVEL_TRACE) {
2138 CHECK_STATUS(print_device_info((u8)dev_num));
2139 }
2140#endif
2141
Chris Packham1a07d212018-05-10 13:28:29 +12002142 ddr3_tip_validate_algo_components(dev_num);
2143
Stefan Roese5ffceb82015-03-26 15:36:56 +01002144 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2145 CHECK_STATUS(ddr3_tip_ddr3_reset_phy_regs(dev_num));
2146 }
2147 /* Set to 0 after each loop to avoid illegal value may be used */
2148 effective_cs = 0;
2149
Chris Packham4bf81db2018-12-03 14:26:49 +13002150 freq_tbl[MV_DDR_FREQ_LOW_FREQ] = dfs_low_freq;
Chris Packham1a07d212018-05-10 13:28:29 +12002151
Stefan Roese5ffceb82015-03-26 15:36:56 +01002152 if (is_pll_before_init != 0) {
2153 for (if_id = 0; if_id < MAX_INTERFACE_NUM; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12002154 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002155 config_func_info[dev_num].tip_set_freq_divider_func(
2156 (u8)dev_num, if_id, freq);
2157 }
2158 }
2159
Chris Packham1a07d212018-05-10 13:28:29 +12002160/* TODO: enable this functionality for other platforms */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002161 if (is_adll_calib_before_init != 0) {
2162 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2163 ("with adll calib before init\n"));
2164 adll_calibration(dev_num, ACCESS_TYPE_MULTICAST, 0, freq);
2165 }
2166
2167 if (is_reg_dump != 0) {
2168 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2169 ("Dump before init controller\n"));
2170 ddr3_tip_reg_dump(dev_num);
2171 }
2172
2173 if (mask_tune_func & INIT_CONTROLLER_MASK_BIT) {
2174 training_stage = INIT_CONTROLLER;
2175 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2176 ("INIT_CONTROLLER_MASK_BIT\n"));
2177 init_cntr_prm.do_mrs_phy = 1;
2178 init_cntr_prm.is_ctrl64_bit = 0;
2179 init_cntr_prm.init_phy = 1;
2180 init_cntr_prm.msys_init = 0;
2181 ret = hws_ddr3_tip_init_controller(dev_num, &init_cntr_prm);
2182 if (is_reg_dump != 0)
2183 ddr3_tip_reg_dump(dev_num);
2184 if (ret != MV_OK) {
2185 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2186 ("hws_ddr3_tip_init_controller failure\n"));
2187 if (debug_mode == 0)
2188 return MV_FAIL;
2189 }
2190 }
2191
Chris Packham1a07d212018-05-10 13:28:29 +12002192 ret = adll_calibration(dev_num, ACCESS_TYPE_MULTICAST, 0, freq);
2193 if (ret != MV_OK) {
2194 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2195 ("adll_calibration failure\n"));
2196 if (debug_mode == 0)
2197 return MV_FAIL;
2198 }
Stefan Roese5ffceb82015-03-26 15:36:56 +01002199
2200 if (mask_tune_func & SET_LOW_FREQ_MASK_BIT) {
2201 training_stage = SET_LOW_FREQ;
Chris Packham1a07d212018-05-10 13:28:29 +12002202
2203 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2204 ddr3_tip_adll_regs_bypass(dev_num, 0, 0x1f);
2205 adll_bypass_flag = 1;
2206 }
2207 effective_cs = 0;
2208
Stefan Roese5ffceb82015-03-26 15:36:56 +01002209 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2210 ("SET_LOW_FREQ_MASK_BIT %d\n",
Chris Packham4bf81db2018-12-03 14:26:49 +13002211 freq_tbl[low_freq]));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002212 ret = ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
2213 PARAM_NOT_CARE, low_freq);
2214 if (is_reg_dump != 0)
2215 ddr3_tip_reg_dump(dev_num);
2216 if (ret != MV_OK) {
2217 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2218 ("ddr3_tip_freq_set failure\n"));
2219 if (debug_mode == 0)
2220 return MV_FAIL;
2221 }
2222 }
2223
Chris Packham1a07d212018-05-10 13:28:29 +12002224 if (mask_tune_func & WRITE_LEVELING_LF_MASK_BIT) {
2225 training_stage = WRITE_LEVELING_LF;
2226 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2227 ("WRITE_LEVELING_LF_MASK_BIT\n"));
2228 ret = ddr3_tip_dynamic_write_leveling(dev_num, 1);
2229 if (is_reg_dump != 0)
2230 ddr3_tip_reg_dump(dev_num);
2231 if (ret != MV_OK) {
2232 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2233 ("ddr3_tip_dynamic_write_leveling LF failure\n"));
2234 if (debug_mode == 0)
2235 return MV_FAIL;
2236 }
2237 }
2238
Stefan Roese5ffceb82015-03-26 15:36:56 +01002239 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2240 if (mask_tune_func & LOAD_PATTERN_MASK_BIT) {
2241 training_stage = LOAD_PATTERN;
2242 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2243 ("LOAD_PATTERN_MASK_BIT #%d\n",
2244 effective_cs));
2245 ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
2246 if (is_reg_dump != 0)
2247 ddr3_tip_reg_dump(dev_num);
2248 if (ret != MV_OK) {
2249 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2250 ("ddr3_tip_load_all_pattern_to_mem failure CS #%d\n",
2251 effective_cs));
2252 if (debug_mode == 0)
2253 return MV_FAIL;
2254 }
2255 }
2256 }
Chris Packham1a07d212018-05-10 13:28:29 +12002257
2258 if (adll_bypass_flag == 1) {
2259 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2260 ddr3_tip_adll_regs_bypass(dev_num, phy_reg1_val, 0);
2261 adll_bypass_flag = 0;
2262 }
2263 }
2264
Stefan Roese5ffceb82015-03-26 15:36:56 +01002265 /* Set to 0 after each loop to avoid illegal value may be used */
2266 effective_cs = 0;
2267
2268 if (mask_tune_func & SET_MEDIUM_FREQ_MASK_BIT) {
2269 training_stage = SET_MEDIUM_FREQ;
2270 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2271 ("SET_MEDIUM_FREQ_MASK_BIT %d\n",
Chris Packham4bf81db2018-12-03 14:26:49 +13002272 freq_tbl[medium_freq]));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002273 ret =
2274 ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
2275 PARAM_NOT_CARE, medium_freq);
2276 if (is_reg_dump != 0)
2277 ddr3_tip_reg_dump(dev_num);
2278 if (ret != MV_OK) {
2279 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2280 ("ddr3_tip_freq_set failure\n"));
2281 if (debug_mode == 0)
2282 return MV_FAIL;
2283 }
2284 }
2285
2286 if (mask_tune_func & WRITE_LEVELING_MASK_BIT) {
2287 training_stage = WRITE_LEVELING;
2288 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2289 ("WRITE_LEVELING_MASK_BIT\n"));
Chris Packham4bf81db2018-12-03 14:26:49 +13002290 if ((rl_mid_freq_wa == 0) || (freq_tbl[medium_freq] == 533)) {
Chris Packham1a07d212018-05-10 13:28:29 +12002291 ret = ddr3_tip_dynamic_write_leveling(dev_num, 0);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002292 } else {
2293 /* Use old WL */
2294 ret = ddr3_tip_legacy_dynamic_write_leveling(dev_num);
2295 }
2296
2297 if (is_reg_dump != 0)
2298 ddr3_tip_reg_dump(dev_num);
2299 if (ret != MV_OK) {
2300 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2301 ("ddr3_tip_dynamic_write_leveling failure\n"));
2302 if (debug_mode == 0)
2303 return MV_FAIL;
2304 }
2305 }
2306
2307 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2308 if (mask_tune_func & LOAD_PATTERN_2_MASK_BIT) {
2309 training_stage = LOAD_PATTERN_2;
2310 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2311 ("LOAD_PATTERN_2_MASK_BIT CS #%d\n",
2312 effective_cs));
2313 ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
2314 if (is_reg_dump != 0)
2315 ddr3_tip_reg_dump(dev_num);
2316 if (ret != MV_OK) {
2317 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2318 ("ddr3_tip_load_all_pattern_to_mem failure CS #%d\n",
2319 effective_cs));
2320 if (debug_mode == 0)
2321 return MV_FAIL;
2322 }
2323 }
2324 }
2325 /* Set to 0 after each loop to avoid illegal value may be used */
2326 effective_cs = 0;
2327
2328 if (mask_tune_func & READ_LEVELING_MASK_BIT) {
2329 training_stage = READ_LEVELING;
2330 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2331 ("READ_LEVELING_MASK_BIT\n"));
Chris Packham4bf81db2018-12-03 14:26:49 +13002332 if ((rl_mid_freq_wa == 0) || (freq_tbl[medium_freq] == 533)) {
Stefan Roese5ffceb82015-03-26 15:36:56 +01002333 ret = ddr3_tip_dynamic_read_leveling(dev_num, medium_freq);
2334 } else {
2335 /* Use old RL */
2336 ret = ddr3_tip_legacy_dynamic_read_leveling(dev_num);
2337 }
2338
2339 if (is_reg_dump != 0)
2340 ddr3_tip_reg_dump(dev_num);
2341 if (ret != MV_OK) {
2342 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2343 ("ddr3_tip_dynamic_read_leveling failure\n"));
2344 if (debug_mode == 0)
2345 return MV_FAIL;
2346 }
2347 }
2348
2349 if (mask_tune_func & WRITE_LEVELING_SUPP_MASK_BIT) {
2350 training_stage = WRITE_LEVELING_SUPP;
2351 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2352 ("WRITE_LEVELING_SUPP_MASK_BIT\n"));
2353 ret = ddr3_tip_dynamic_write_leveling_supp(dev_num);
2354 if (is_reg_dump != 0)
2355 ddr3_tip_reg_dump(dev_num);
2356 if (ret != MV_OK) {
2357 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2358 ("ddr3_tip_dynamic_write_leveling_supp failure\n"));
2359 if (debug_mode == 0)
2360 return MV_FAIL;
2361 }
2362 }
2363
Tony Dinhe2c524b2023-01-18 19:03:04 -08002364#if !defined(CONFIG_DDR4)
Stefan Roese5ffceb82015-03-26 15:36:56 +01002365 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2366 if (mask_tune_func & PBS_RX_MASK_BIT) {
2367 training_stage = PBS_RX;
2368 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2369 ("PBS_RX_MASK_BIT CS #%d\n",
2370 effective_cs));
2371 ret = ddr3_tip_pbs_rx(dev_num);
2372 if (is_reg_dump != 0)
2373 ddr3_tip_reg_dump(dev_num);
2374 if (ret != MV_OK) {
2375 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2376 ("ddr3_tip_pbs_rx failure CS #%d\n",
2377 effective_cs));
2378 if (debug_mode == 0)
2379 return MV_FAIL;
2380 }
2381 }
2382 }
2383
2384 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2385 if (mask_tune_func & PBS_TX_MASK_BIT) {
2386 training_stage = PBS_TX;
2387 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2388 ("PBS_TX_MASK_BIT CS #%d\n",
2389 effective_cs));
2390 ret = ddr3_tip_pbs_tx(dev_num);
2391 if (is_reg_dump != 0)
2392 ddr3_tip_reg_dump(dev_num);
2393 if (ret != MV_OK) {
2394 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2395 ("ddr3_tip_pbs_tx failure CS #%d\n",
2396 effective_cs));
2397 if (debug_mode == 0)
2398 return MV_FAIL;
2399 }
2400 }
2401 }
2402 /* Set to 0 after each loop to avoid illegal value may be used */
2403 effective_cs = 0;
Tony Dinhe2c524b2023-01-18 19:03:04 -08002404#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002405
2406 if (mask_tune_func & SET_TARGET_FREQ_MASK_BIT) {
2407 training_stage = SET_TARGET_FREQ;
2408 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2409 ("SET_TARGET_FREQ_MASK_BIT %d\n",
Chris Packham4bf81db2018-12-03 14:26:49 +13002410 freq_tbl[tm->
Stefan Roese5ffceb82015-03-26 15:36:56 +01002411 interface_params[first_active_if].
2412 memory_freq]));
2413 ret = ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
2414 PARAM_NOT_CARE,
2415 tm->interface_params[first_active_if].
2416 memory_freq);
2417 if (is_reg_dump != 0)
2418 ddr3_tip_reg_dump(dev_num);
2419 if (ret != MV_OK) {
2420 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2421 ("ddr3_tip_freq_set failure\n"));
2422 if (debug_mode == 0)
2423 return MV_FAIL;
2424 }
2425 }
2426
2427 if (mask_tune_func & WRITE_LEVELING_TF_MASK_BIT) {
2428 training_stage = WRITE_LEVELING_TF;
2429 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2430 ("WRITE_LEVELING_TF_MASK_BIT\n"));
Chris Packham1a07d212018-05-10 13:28:29 +12002431 ret = ddr3_tip_dynamic_write_leveling(dev_num, 0);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002432 if (is_reg_dump != 0)
2433 ddr3_tip_reg_dump(dev_num);
2434 if (ret != MV_OK) {
2435 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2436 ("ddr3_tip_dynamic_write_leveling TF failure\n"));
2437 if (debug_mode == 0)
2438 return MV_FAIL;
2439 }
2440 }
2441
2442 if (mask_tune_func & LOAD_PATTERN_HIGH_MASK_BIT) {
2443 training_stage = LOAD_PATTERN_HIGH;
2444 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("LOAD_PATTERN_HIGH\n"));
2445 ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
2446 if (is_reg_dump != 0)
2447 ddr3_tip_reg_dump(dev_num);
2448 if (ret != MV_OK) {
2449 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2450 ("ddr3_tip_load_all_pattern_to_mem failure\n"));
2451 if (debug_mode == 0)
2452 return MV_FAIL;
2453 }
2454 }
2455
2456 if (mask_tune_func & READ_LEVELING_TF_MASK_BIT) {
2457 training_stage = READ_LEVELING_TF;
2458 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2459 ("READ_LEVELING_TF_MASK_BIT\n"));
2460 ret = ddr3_tip_dynamic_read_leveling(dev_num, tm->
2461 interface_params[first_active_if].
2462 memory_freq);
2463 if (is_reg_dump != 0)
2464 ddr3_tip_reg_dump(dev_num);
2465 if (ret != MV_OK) {
2466 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2467 ("ddr3_tip_dynamic_read_leveling TF failure\n"));
2468 if (debug_mode == 0)
2469 return MV_FAIL;
2470 }
2471 }
2472
Chris Packham1a07d212018-05-10 13:28:29 +12002473 if (mask_tune_func & RL_DQS_BURST_MASK_BIT) {
2474 training_stage = READ_LEVELING_TF;
2475 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2476 ("RL_DQS_BURST_MASK_BIT\n"));
2477 ret = mv_ddr_rl_dqs_burst(0, 0, tm->interface_params[0].memory_freq);
2478 if (is_reg_dump != 0)
2479 ddr3_tip_reg_dump(dev_num);
2480 if (ret != MV_OK) {
2481 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2482 ("mv_ddr_rl_dqs_burst TF failure\n"));
2483 if (debug_mode == 0)
2484 return MV_FAIL;
2485 }
2486 }
2487
Tony Dinhe2c524b2023-01-18 19:03:04 -08002488#if !defined(CONFIG_DDR4)
Stefan Roese5ffceb82015-03-26 15:36:56 +01002489 if (mask_tune_func & DM_PBS_TX_MASK_BIT) {
2490 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("DM_PBS_TX_MASK_BIT\n"));
2491 }
2492
2493 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2494 if (mask_tune_func & VREF_CALIBRATION_MASK_BIT) {
2495 training_stage = VREF_CALIBRATION;
2496 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("VREF\n"));
2497 ret = ddr3_tip_vref(dev_num);
2498 if (is_reg_dump != 0) {
2499 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2500 ("VREF Dump\n"));
2501 ddr3_tip_reg_dump(dev_num);
2502 }
2503 if (ret != MV_OK) {
2504 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2505 ("ddr3_tip_vref failure\n"));
2506 if (debug_mode == 0)
2507 return MV_FAIL;
2508 }
2509 }
2510 }
2511 /* Set to 0 after each loop to avoid illegal value may be used */
2512 effective_cs = 0;
2513
2514 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2515 if (mask_tune_func & CENTRALIZATION_RX_MASK_BIT) {
2516 training_stage = CENTRALIZATION_RX;
2517 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2518 ("CENTRALIZATION_RX_MASK_BIT CS #%d\n",
2519 effective_cs));
2520 ret = ddr3_tip_centralization_rx(dev_num);
2521 if (is_reg_dump != 0)
2522 ddr3_tip_reg_dump(dev_num);
2523 if (ret != MV_OK) {
2524 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2525 ("ddr3_tip_centralization_rx failure CS #%d\n",
2526 effective_cs));
2527 if (debug_mode == 0)
2528 return MV_FAIL;
2529 }
2530 }
2531 }
2532 /* Set to 0 after each loop to avoid illegal value may be used */
2533 effective_cs = 0;
Tony Dinhe2c524b2023-01-18 19:03:04 -08002534#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002535
2536 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2537 if (mask_tune_func & WRITE_LEVELING_SUPP_TF_MASK_BIT) {
2538 training_stage = WRITE_LEVELING_SUPP_TF;
2539 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2540 ("WRITE_LEVELING_SUPP_TF_MASK_BIT CS #%d\n",
2541 effective_cs));
2542 ret = ddr3_tip_dynamic_write_leveling_supp(dev_num);
2543 if (is_reg_dump != 0)
2544 ddr3_tip_reg_dump(dev_num);
2545 if (ret != MV_OK) {
2546 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2547 ("ddr3_tip_dynamic_write_leveling_supp TF failure CS #%d\n",
2548 effective_cs));
2549 if (debug_mode == 0)
2550 return MV_FAIL;
2551 }
2552 }
2553 }
2554 /* Set to 0 after each loop to avoid illegal value may be used */
2555 effective_cs = 0;
2556
Tony Dinhe2c524b2023-01-18 19:03:04 -08002557#if defined(CONFIG_DDR4)
2558 for (effective_cs = 0; effective_cs < max_cs; effective_cs++)
2559 CHECK_STATUS(mv_ddr4_training_main_flow(dev_num));
2560#endif /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +12002561
Tony Dinhe2c524b2023-01-18 19:03:04 -08002562#if !defined(CONFIG_DDR4)
Stefan Roese5ffceb82015-03-26 15:36:56 +01002563 for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
2564 if (mask_tune_func & CENTRALIZATION_TX_MASK_BIT) {
2565 training_stage = CENTRALIZATION_TX;
2566 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2567 ("CENTRALIZATION_TX_MASK_BIT CS #%d\n",
2568 effective_cs));
2569 ret = ddr3_tip_centralization_tx(dev_num);
2570 if (is_reg_dump != 0)
2571 ddr3_tip_reg_dump(dev_num);
2572 if (ret != MV_OK) {
2573 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2574 ("ddr3_tip_centralization_tx failure CS #%d\n",
2575 effective_cs));
2576 if (debug_mode == 0)
2577 return MV_FAIL;
2578 }
2579 }
2580 }
2581 /* Set to 0 after each loop to avoid illegal value may be used */
2582 effective_cs = 0;
Tony Dinhe2c524b2023-01-18 19:03:04 -08002583#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002584
2585 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("restore registers to default\n"));
2586 /* restore register values */
2587 CHECK_STATUS(ddr3_tip_restore_dunit_regs(dev_num));
2588
2589 if (is_reg_dump != 0)
2590 ddr3_tip_reg_dump(dev_num);
2591
2592 return MV_OK;
2593}
2594
2595/*
2596 * DDR3 Dynamic training flow
2597 */
2598static int ddr3_tip_ddr3_auto_tune(u32 dev_num)
2599{
Chris Packham1a07d212018-05-10 13:28:29 +12002600 int status;
2601 u32 if_id, stage;
Stefan Roese5ffceb82015-03-26 15:36:56 +01002602 int is_if_fail = 0, is_auto_tune_fail = 0;
2603
2604 training_stage = INIT_CONTROLLER;
2605
2606 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
2607 for (stage = 0; stage < MAX_STAGE_LIMIT; stage++)
2608 training_result[stage][if_id] = NO_TEST_DONE;
2609 }
2610
Chris Packham1a07d212018-05-10 13:28:29 +12002611 status = ddr3_tip_ddr3_training_main_flow(dev_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002612
2613 /* activate XSB test */
2614 if (xsb_validate_type != 0) {
2615 run_xsb_test(dev_num, xsb_validation_base_address, 1, 1,
2616 0x1024);
2617 }
2618
2619 if (is_reg_dump != 0)
2620 ddr3_tip_reg_dump(dev_num);
2621
2622 /* print log */
2623 CHECK_STATUS(ddr3_tip_print_log(dev_num, window_mem_addr));
2624
Chris Packham1a07d212018-05-10 13:28:29 +12002625#ifndef EXCLUDE_DEBUG_PRINTS
2626 if (status != MV_OK) {
Stefan Roese5ffceb82015-03-26 15:36:56 +01002627 CHECK_STATUS(ddr3_tip_print_stability_log(dev_num));
2628 }
Chris Packham1a07d212018-05-10 13:28:29 +12002629#endif /* EXCLUDE_DEBUG_PRINTS */
Stefan Roese5ffceb82015-03-26 15:36:56 +01002630
2631 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
2632 is_if_fail = 0;
2633 for (stage = 0; stage < MAX_STAGE_LIMIT; stage++) {
2634 if (training_result[stage][if_id] == TEST_FAILED)
2635 is_if_fail = 1;
2636 }
2637 if (is_if_fail == 1) {
2638 is_auto_tune_fail = 1;
2639 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2640 ("Auto Tune failed for IF %d\n",
2641 if_id));
2642 }
2643 }
2644
Chris Packham1a07d212018-05-10 13:28:29 +12002645 if (((status == MV_FAIL) && (is_auto_tune_fail == 0)) ||
2646 ((status == MV_OK) && (is_auto_tune_fail == 1))) {
2647 /*
2648 * If MainFlow result and trainingResult DB not in sync,
2649 * issue warning (caused by no update of trainingResult DB
2650 * when failed)
2651 */
2652 DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
2653 ("Warning: Algorithm return value and Result DB"
2654 "are not synced (status 0x%x result DB %d)\n",
2655 status, is_auto_tune_fail));
2656 }
2657
2658 if ((status != MV_OK) || (is_auto_tune_fail == 1))
Stefan Roese5ffceb82015-03-26 15:36:56 +01002659 return MV_FAIL;
2660 else
2661 return MV_OK;
2662}
2663
2664/*
2665 * Enable init sequence
2666 */
2667int ddr3_tip_enable_init_sequence(u32 dev_num)
2668{
2669 int is_fail = 0;
2670 u32 if_id = 0, mem_mask = 0, bus_index = 0;
Chris Packham1a07d212018-05-10 13:28:29 +12002671 u32 octets_per_if_num = ddr3_tip_dev_attr_get(dev_num, MV_ATTR_OCTET_PER_INTERFACE);
2672 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002673
2674 /* Enable init sequence */
2675 CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST, 0,
Chris Packham1a07d212018-05-10 13:28:29 +12002676 SDRAM_INIT_CTRL_REG, 0x1, 0x1));
Stefan Roese5ffceb82015-03-26 15:36:56 +01002677
2678 for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
Chris Packham1a07d212018-05-10 13:28:29 +12002679 VALIDATE_IF_ACTIVE(tm->if_act_mask, if_id);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002680
2681 if (ddr3_tip_if_polling
2682 (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x1,
Chris Packham1a07d212018-05-10 13:28:29 +12002683 SDRAM_INIT_CTRL_REG,
Stefan Roese5ffceb82015-03-26 15:36:56 +01002684 MAX_POLLING_ITERATIONS) != MV_OK) {
2685 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2686 ("polling failed IF %d\n",
2687 if_id));
2688 is_fail = 1;
2689 continue;
2690 }
2691
2692 mem_mask = 0;
Chris Packham1a07d212018-05-10 13:28:29 +12002693 for (bus_index = 0; bus_index < octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +01002694 bus_index++) {
Chris Packham1a07d212018-05-10 13:28:29 +12002695 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_index);
Stefan Roese5ffceb82015-03-26 15:36:56 +01002696 mem_mask |=
2697 tm->interface_params[if_id].
2698 as_bus_params[bus_index].mirror_enable_bitmask;
2699 }
2700
2701 if (mem_mask != 0) {
2702 /* Disable Multi CS */
2703 CHECK_STATUS(ddr3_tip_if_write
2704 (dev_num, ACCESS_TYPE_MULTICAST,
Chris Packham1a07d212018-05-10 13:28:29 +12002705 if_id, DUAL_DUNIT_CFG_REG, 1 << 3,
Stefan Roese5ffceb82015-03-26 15:36:56 +01002706 1 << 3));
2707 }
2708 }
2709
2710 return (is_fail == 0) ? MV_OK : MV_FAIL;
2711}
2712
2713int ddr3_tip_register_dq_table(u32 dev_num, u32 *table)
2714{
2715 dq_map_table = table;
2716
2717 return MV_OK;
2718}
2719
2720/*
2721 * Check if pup search is locked
2722 */
2723int ddr3_tip_is_pup_lock(u32 *pup_buf, enum hws_training_result read_mode)
2724{
2725 u32 bit_start = 0, bit_end = 0, bit_id;
2726
2727 if (read_mode == RESULT_PER_BIT) {
2728 bit_start = 0;
2729 bit_end = BUS_WIDTH_IN_BITS - 1;
2730 } else {
2731 bit_start = 0;
2732 bit_end = 0;
2733 }
2734
2735 for (bit_id = bit_start; bit_id <= bit_end; bit_id++) {
2736 if (GET_LOCK_RESULT(pup_buf[bit_id]) == 0)
2737 return 0;
2738 }
2739
2740 return 1;
2741}
2742
2743/*
2744 * Get minimum buffer value
2745 */
2746u8 ddr3_tip_get_buf_min(u8 *buf_ptr)
2747{
2748 u8 min_val = 0xff;
2749 u8 cnt = 0;
2750
2751 for (cnt = 0; cnt < BUS_WIDTH_IN_BITS; cnt++) {
2752 if (buf_ptr[cnt] < min_val)
2753 min_val = buf_ptr[cnt];
2754 }
2755
2756 return min_val;
2757}
2758
2759/*
2760 * Get maximum buffer value
2761 */
2762u8 ddr3_tip_get_buf_max(u8 *buf_ptr)
2763{
2764 u8 max_val = 0;
2765 u8 cnt = 0;
2766
2767 for (cnt = 0; cnt < BUS_WIDTH_IN_BITS; cnt++) {
2768 if (buf_ptr[cnt] > max_val)
2769 max_val = buf_ptr[cnt];
2770 }
2771
2772 return max_val;
2773}
2774
2775/*
2776 * The following functions return memory parameters:
2777 * bus and device width, device size
2778 */
2779
2780u32 hws_ddr3_get_bus_width(void)
2781{
Chris Packham1a07d212018-05-10 13:28:29 +12002782 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002783
2784 return (DDR3_IS_16BIT_DRAM_MODE(tm->bus_act_mask) ==
2785 1) ? 16 : 32;
2786}
2787
2788u32 hws_ddr3_get_device_width(u32 if_id)
2789{
Chris Packham1a07d212018-05-10 13:28:29 +12002790 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002791
2792 return (tm->interface_params[if_id].bus_width ==
Chris Packham1a07d212018-05-10 13:28:29 +12002793 MV_DDR_DEV_WIDTH_8BIT) ? 8 : 16;
Stefan Roese5ffceb82015-03-26 15:36:56 +01002794}
2795
2796u32 hws_ddr3_get_device_size(u32 if_id)
2797{
Chris Packham1a07d212018-05-10 13:28:29 +12002798 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002799
2800 if (tm->interface_params[if_id].memory_size >=
Chris Packham1a07d212018-05-10 13:28:29 +12002801 MV_DDR_DIE_CAP_LAST) {
Stefan Roese5ffceb82015-03-26 15:36:56 +01002802 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2803 ("Error: Wrong device size of Cs: %d",
2804 tm->interface_params[if_id].memory_size));
2805 return 0;
2806 } else {
2807 return 1 << tm->interface_params[if_id].memory_size;
2808 }
2809}
2810
2811int hws_ddr3_calc_mem_cs_size(u32 if_id, u32 cs, u32 *cs_size)
2812{
2813 u32 cs_mem_size, dev_size;
2814
2815 dev_size = hws_ddr3_get_device_size(if_id);
2816 if (dev_size != 0) {
2817 cs_mem_size = ((hws_ddr3_get_bus_width() /
2818 hws_ddr3_get_device_width(if_id)) * dev_size);
2819
2820 /* the calculated result in Gbytex16 to avoid float using */
2821
2822 if (cs_mem_size == 2) {
2823 *cs_size = _128M;
2824 } else if (cs_mem_size == 4) {
2825 *cs_size = _256M;
2826 } else if (cs_mem_size == 8) {
2827 *cs_size = _512M;
2828 } else if (cs_mem_size == 16) {
2829 *cs_size = _1G;
2830 } else if (cs_mem_size == 32) {
2831 *cs_size = _2G;
2832 } else {
2833 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2834 ("Error: Wrong Memory size of Cs: %d", cs));
2835 return MV_FAIL;
2836 }
2837 return MV_OK;
2838 } else {
2839 return MV_FAIL;
2840 }
2841}
2842
2843int hws_ddr3_cs_base_adr_calc(u32 if_id, u32 cs, u32 *cs_base_addr)
2844{
2845 u32 cs_mem_size = 0;
2846#ifdef DEVICE_MAX_DRAM_ADDRESS_SIZE
2847 u32 physical_mem_size;
2848 u32 max_mem_size = DEVICE_MAX_DRAM_ADDRESS_SIZE;
2849#endif
2850
2851 if (hws_ddr3_calc_mem_cs_size(if_id, cs, &cs_mem_size) != MV_OK)
2852 return MV_FAIL;
2853
2854#ifdef DEVICE_MAX_DRAM_ADDRESS_SIZE
Chris Packham1a07d212018-05-10 13:28:29 +12002855 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +01002856 /*
2857 * if number of address pins doesn't allow to use max mem size that
2858 * is defined in topology mem size is defined by
2859 * DEVICE_MAX_DRAM_ADDRESS_SIZE
2860 */
Chris Packham1a07d212018-05-10 13:28:29 +12002861 physical_mem_size = mem_size[tm->interface_params[0].memory_size];
Stefan Roese5ffceb82015-03-26 15:36:56 +01002862
2863 if (hws_ddr3_get_device_width(cs) == 16) {
2864 /*
2865 * 16bit mem device can be twice more - no need in less
2866 * significant pin
2867 */
2868 max_mem_size = DEVICE_MAX_DRAM_ADDRESS_SIZE * 2;
2869 }
2870
2871 if (physical_mem_size > max_mem_size) {
2872 cs_mem_size = max_mem_size *
2873 (hws_ddr3_get_bus_width() /
2874 hws_ddr3_get_device_width(if_id));
2875 DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
2876 ("Updated Physical Mem size is from 0x%x to %x\n",
2877 physical_mem_size,
2878 DEVICE_MAX_DRAM_ADDRESS_SIZE));
2879 }
2880#endif
2881
2882 /* calculate CS base addr */
2883 *cs_base_addr = ((cs_mem_size) * cs) & 0xffff0000;
2884
2885 return MV_OK;
2886}
Chris Packham4bf81db2018-12-03 14:26:49 +13002887
2888/* TODO: consider to move to misl phy driver */
2889enum {
2890 MISL_PHY_DRV_OHM_30 = 0xf,
2891 MISL_PHY_DRV_OHM_48 = 0xa,
2892 MISL_PHY_DRV_OHM_80 = 0x6,
2893 MISL_PHY_DRV_OHM_120 = 0x4
2894};
2895
2896enum {
2897 MISL_PHY_ODT_OHM_60 = 0x8,
2898 MISL_PHY_ODT_OHM_80 = 0x6,
2899 MISL_PHY_ODT_OHM_120 = 0x4,
2900 MISL_PHY_ODT_OHM_240 = 0x2
2901};
2902
2903static unsigned int mv_ddr_misl_phy_drv_calc(unsigned int cfg)
2904{
2905 unsigned int val;
2906
2907 switch (cfg) {
2908 case MV_DDR_OHM_30:
2909 val = MISL_PHY_DRV_OHM_30;
2910 break;
2911 case MV_DDR_OHM_48:
2912 val = MISL_PHY_DRV_OHM_48;
2913 break;
2914 case MV_DDR_OHM_80:
2915 val = MISL_PHY_DRV_OHM_80;
2916 break;
2917 case MV_DDR_OHM_120:
2918 val = MISL_PHY_DRV_OHM_120;
2919 break;
2920 default:
2921 val = PARAM_UNDEFINED;
2922 }
2923
2924 return val;
2925}
2926
2927static unsigned int mv_ddr_misl_phy_odt_calc(unsigned int cfg)
2928{
2929 unsigned int val;
2930
2931 switch (cfg) {
2932 case MV_DDR_OHM_60:
2933 val = MISL_PHY_ODT_OHM_60;
2934 break;
2935 case MV_DDR_OHM_80:
2936 val = MISL_PHY_ODT_OHM_80;
2937 break;
2938 case MV_DDR_OHM_120:
2939 val = MISL_PHY_ODT_OHM_120;
2940 break;
2941 case MV_DDR_OHM_240:
2942 val = MISL_PHY_ODT_OHM_240;
2943 break;
2944 default:
2945 val = PARAM_UNDEFINED;
2946 }
2947
2948 return val;
2949}
2950
2951unsigned int mv_ddr_misl_phy_drv_data_p_get(void)
2952{
2953 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2954 unsigned int drv_data_p = mv_ddr_misl_phy_drv_calc(tm->edata.phy_edata.drv_data_p);
2955
2956 if (drv_data_p == PARAM_UNDEFINED)
2957 printf("error: %s: unsupported drv_data_p parameter found\n", __func__);
2958
2959 return drv_data_p;
2960}
2961
2962unsigned int mv_ddr_misl_phy_drv_data_n_get(void)
2963{
2964 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2965 unsigned int drv_data_n = mv_ddr_misl_phy_drv_calc(tm->edata.phy_edata.drv_data_n);
2966
2967 if (drv_data_n == PARAM_UNDEFINED)
2968 printf("error: %s: unsupported drv_data_n parameter found\n", __func__);
2969
2970 return drv_data_n;
2971}
2972
2973unsigned int mv_ddr_misl_phy_drv_ctrl_p_get(void)
2974{
2975 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2976 unsigned int drv_ctrl_p = mv_ddr_misl_phy_drv_calc(tm->edata.phy_edata.drv_ctrl_p);
2977
2978 if (drv_ctrl_p == PARAM_UNDEFINED)
2979 printf("error: %s: unsupported drv_ctrl_p parameter found\n", __func__);
2980
2981 return drv_ctrl_p;
2982}
2983
2984unsigned int mv_ddr_misl_phy_drv_ctrl_n_get(void)
2985{
2986 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2987 unsigned int drv_ctrl_n = mv_ddr_misl_phy_drv_calc(tm->edata.phy_edata.drv_ctrl_n);
2988
2989 if (drv_ctrl_n == PARAM_UNDEFINED)
2990 printf("error: %s: unsupported drv_ctrl_n parameter found\n", __func__);
2991
2992 return drv_ctrl_n;
2993}
2994
2995unsigned int mv_ddr_misl_phy_odt_p_get(void)
2996{
2997 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
2998 unsigned int cs_num = mv_ddr_cs_num_get();
2999 unsigned int odt_p = PARAM_UNDEFINED;
3000
3001 if (cs_num > 0 && cs_num <= MAX_CS_NUM)
3002 odt_p = mv_ddr_misl_phy_odt_calc(tm->edata.phy_edata.odt_p[cs_num - 1]);
3003
3004 if (odt_p == PARAM_UNDEFINED)
3005 printf("error: %s: unsupported odt_p parameter found\n", __func__);
3006
3007 return odt_p;
3008}
3009
3010unsigned int mv_ddr_misl_phy_odt_n_get(void)
3011{
3012 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
3013 unsigned int cs_num = mv_ddr_cs_num_get();
3014 unsigned int odt_n = PARAM_UNDEFINED;
3015
3016 if (cs_num > 0 && cs_num <= MAX_CS_NUM)
3017 odt_n = mv_ddr_misl_phy_odt_calc(tm->edata.phy_edata.odt_n[cs_num - 1]);
3018
3019 if (odt_n == PARAM_UNDEFINED)
3020 printf("error: %s: unsupported odt_n parameter found\n", __func__);
3021
3022 return odt_n;
3023}
Tony Dinhe2c524b2023-01-18 19:03:04 -08003024