xref: /openbmc/u-boot/drivers/ddr/altera/sequencer.c (revision 0b69b807d841ae39d41f27b96542219211bcec4e)
1 /*
2  * Copyright Altera Corporation (C) 2012-2015
3  *
4  * SPDX-License-Identifier:    BSD-3-Clause
5  */
6 
7 #include <common.h>
8 #include <asm/io.h>
9 #include <asm/arch/sdram.h>
10 #include "sequencer.h"
11 #include "sequencer_auto.h"
12 #include "sequencer_auto_ac_init.h"
13 #include "sequencer_auto_inst_init.h"
14 #include "sequencer_defines.h"
15 
16 static void scc_mgr_load_dqs_for_write_group(uint32_t write_group);
17 
18 static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
19 	(struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
20 
21 static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
22 	(struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
23 
24 static struct socfpga_sdr_reg_file *sdr_reg_file =
25 	(struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
26 
27 static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
28 	(struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
29 
30 static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
31 	(struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
32 
33 static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
34 	(struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
35 
36 static struct socfpga_data_mgr *data_mgr =
37 	(struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
38 
39 static struct socfpga_sdr_ctrl *sdr_ctrl =
40 	(struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
41 
42 #define DELTA_D		1
43 
44 /*
45  * In order to reduce ROM size, most of the selectable calibration steps are
46  * decided at compile time based on the user's calibration mode selection,
47  * as captured by the STATIC_CALIB_STEPS selection below.
48  *
49  * However, to support simulation-time selection of fast simulation mode, where
50  * we skip everything except the bare minimum, we need a few of the steps to
51  * be dynamic.  In those cases, we either use the DYNAMIC_CALIB_STEPS for the
52  * check, which is based on the rtl-supplied value, or we dynamically compute
53  * the value to use based on the dynamically-chosen calibration mode
54  */
55 
56 #define DLEVEL 0
57 #define STATIC_IN_RTL_SIM 0
58 #define STATIC_SKIP_DELAY_LOOPS 0
59 
60 #define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
61 	STATIC_SKIP_DELAY_LOOPS)
62 
63 /* calibration steps requested by the rtl */
64 uint16_t dyn_calib_steps;
65 
66 /*
67  * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
68  * instead of static, we use boolean logic to select between
69  * non-skip and skip values
70  *
71  * The mask is set to include all bits when not-skipping, but is
72  * zero when skipping
73  */
74 
75 uint16_t skip_delay_mask;	/* mask off bits when skipping/not-skipping */
76 
77 #define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
78 	((non_skip_value) & skip_delay_mask)
79 
80 struct gbl_type *gbl;
81 struct param_type *param;
82 uint32_t curr_shadow_reg;
83 
84 static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
85 	uint32_t write_group, uint32_t use_dm,
86 	uint32_t all_correct, uint32_t *bit_chk, uint32_t all_ranks);
87 
88 static void set_failing_group_stage(uint32_t group, uint32_t stage,
89 	uint32_t substage)
90 {
91 	/*
92 	 * Only set the global stage if there was not been any other
93 	 * failing group
94 	 */
95 	if (gbl->error_stage == CAL_STAGE_NIL)	{
96 		gbl->error_substage = substage;
97 		gbl->error_stage = stage;
98 		gbl->error_group = group;
99 	}
100 }
101 
102 static void reg_file_set_group(u16 set_group)
103 {
104 	clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
105 }
106 
107 static void reg_file_set_stage(u8 set_stage)
108 {
109 	clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
110 }
111 
112 static void reg_file_set_sub_stage(u8 set_sub_stage)
113 {
114 	set_sub_stage &= 0xff;
115 	clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
116 }
117 
118 static void initialize(void)
119 {
120 	debug("%s:%d\n", __func__, __LINE__);
121 	/* USER calibration has control over path to memory */
122 	/*
123 	 * In Hard PHY this is a 2-bit control:
124 	 * 0: AFI Mux Select
125 	 * 1: DDIO Mux Select
126 	 */
127 	writel(0x3, &phy_mgr_cfg->mux_sel);
128 
129 	/* USER memory clock is not stable we begin initialization  */
130 	writel(0, &phy_mgr_cfg->reset_mem_stbl);
131 
132 	/* USER calibration status all set to zero */
133 	writel(0, &phy_mgr_cfg->cal_status);
134 
135 	writel(0, &phy_mgr_cfg->cal_debug_info);
136 
137 	if ((dyn_calib_steps & CALIB_SKIP_ALL) != CALIB_SKIP_ALL) {
138 		param->read_correct_mask_vg  = ((uint32_t)1 <<
139 			(RW_MGR_MEM_DQ_PER_READ_DQS /
140 			RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
141 		param->write_correct_mask_vg = ((uint32_t)1 <<
142 			(RW_MGR_MEM_DQ_PER_READ_DQS /
143 			RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
144 		param->read_correct_mask     = ((uint32_t)1 <<
145 			RW_MGR_MEM_DQ_PER_READ_DQS) - 1;
146 		param->write_correct_mask    = ((uint32_t)1 <<
147 			RW_MGR_MEM_DQ_PER_WRITE_DQS) - 1;
148 		param->dm_correct_mask       = ((uint32_t)1 <<
149 			(RW_MGR_MEM_DATA_WIDTH / RW_MGR_MEM_DATA_MASK_WIDTH))
150 			- 1;
151 	}
152 }
153 
154 static void set_rank_and_odt_mask(uint32_t rank, uint32_t odt_mode)
155 {
156 	uint32_t odt_mask_0 = 0;
157 	uint32_t odt_mask_1 = 0;
158 	uint32_t cs_and_odt_mask;
159 
160 	if (odt_mode == RW_MGR_ODT_MODE_READ_WRITE) {
161 		if (RW_MGR_MEM_NUMBER_OF_RANKS == 1) {
162 			/*
163 			 * 1 Rank
164 			 * Read: ODT = 0
165 			 * Write: ODT = 1
166 			 */
167 			odt_mask_0 = 0x0;
168 			odt_mask_1 = 0x1;
169 		} else if (RW_MGR_MEM_NUMBER_OF_RANKS == 2) {
170 			/* 2 Ranks */
171 			if (RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM == 1) {
172 				/* - Dual-Slot , Single-Rank
173 				 * (1 chip-select per DIMM)
174 				 * OR
175 				 * - RDIMM, 4 total CS (2 CS per DIMM)
176 				 * means 2 DIMM
177 				 * Since MEM_NUMBER_OF_RANKS is 2 they are
178 				 * both single rank
179 				 * with 2 CS each (special for RDIMM)
180 				 * Read: Turn on ODT on the opposite rank
181 				 * Write: Turn on ODT on all ranks
182 				 */
183 				odt_mask_0 = 0x3 & ~(1 << rank);
184 				odt_mask_1 = 0x3;
185 			} else {
186 				/*
187 				 * USER - Single-Slot , Dual-rank DIMMs
188 				 * (2 chip-selects per DIMM)
189 				 * USER Read: Turn on ODT off on all ranks
190 				 * USER Write: Turn on ODT on active rank
191 				 */
192 				odt_mask_0 = 0x0;
193 				odt_mask_1 = 0x3 & (1 << rank);
194 			}
195 		} else {
196 			/* 4 Ranks
197 			 * Read:
198 			 * ----------+-----------------------+
199 			 *           |                       |
200 			 *           |         ODT           |
201 			 * Read From +-----------------------+
202 			 *   Rank    |  3  |  2  |  1  |  0  |
203 			 * ----------+-----+-----+-----+-----+
204 			 *     0     |  0  |  1  |  0  |  0  |
205 			 *     1     |  1  |  0  |  0  |  0  |
206 			 *     2     |  0  |  0  |  0  |  1  |
207 			 *     3     |  0  |  0  |  1  |  0  |
208 			 * ----------+-----+-----+-----+-----+
209 			 *
210 			 * Write:
211 			 * ----------+-----------------------+
212 			 *           |                       |
213 			 *           |         ODT           |
214 			 * Write To  +-----------------------+
215 			 *   Rank    |  3  |  2  |  1  |  0  |
216 			 * ----------+-----+-----+-----+-----+
217 			 *     0     |  0  |  1  |  0  |  1  |
218 			 *     1     |  1  |  0  |  1  |  0  |
219 			 *     2     |  0  |  1  |  0  |  1  |
220 			 *     3     |  1  |  0  |  1  |  0  |
221 			 * ----------+-----+-----+-----+-----+
222 			 */
223 			switch (rank) {
224 			case 0:
225 				odt_mask_0 = 0x4;
226 				odt_mask_1 = 0x5;
227 				break;
228 			case 1:
229 				odt_mask_0 = 0x8;
230 				odt_mask_1 = 0xA;
231 				break;
232 			case 2:
233 				odt_mask_0 = 0x1;
234 				odt_mask_1 = 0x5;
235 				break;
236 			case 3:
237 				odt_mask_0 = 0x2;
238 				odt_mask_1 = 0xA;
239 				break;
240 			}
241 		}
242 	} else {
243 		odt_mask_0 = 0x0;
244 		odt_mask_1 = 0x0;
245 	}
246 
247 	cs_and_odt_mask =
248 		(0xFF & ~(1 << rank)) |
249 		((0xFF & odt_mask_0) << 8) |
250 		((0xFF & odt_mask_1) << 16);
251 	writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
252 				RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
253 }
254 
255 /**
256  * scc_mgr_set() - Set SCC Manager register
257  * @off:	Base offset in SCC Manager space
258  * @grp:	Read/Write group
259  * @val:	Value to be set
260  *
261  * This function sets the SCC Manager (Scan Chain Control Manager) register.
262  */
263 static void scc_mgr_set(u32 off, u32 grp, u32 val)
264 {
265 	writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
266 }
267 
268 /**
269  * scc_mgr_initialize() - Initialize SCC Manager registers
270  *
271  * Initialize SCC Manager registers.
272  */
273 static void scc_mgr_initialize(void)
274 {
275 	/*
276 	 * Clear register file for HPS. 16 (2^4) is the size of the
277 	 * full register file in the scc mgr:
278 	 *	RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
279 	 *                             MEM_IF_READ_DQS_WIDTH - 1);
280 	 */
281 	int i;
282 
283 	for (i = 0; i < 16; i++) {
284 		debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
285 			   __func__, __LINE__, i);
286 		scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, 0, i);
287 	}
288 }
289 
290 static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
291 {
292 	scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
293 }
294 
295 static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
296 {
297 	scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
298 }
299 
300 static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
301 {
302 	scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
303 }
304 
305 static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
306 {
307 	scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
308 }
309 
310 static void scc_mgr_set_dqs_io_in_delay(uint32_t write_group, uint32_t delay)
311 {
312 	scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
313 		    delay);
314 }
315 
316 static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
317 {
318 	scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
319 }
320 
321 static void scc_mgr_set_dq_out1_delay(uint32_t dq_in_group, uint32_t delay)
322 {
323 	scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
324 }
325 
326 static void scc_mgr_set_dqs_out1_delay(uint32_t write_group,
327 					      uint32_t delay)
328 {
329 	scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
330 		    delay);
331 }
332 
333 static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
334 {
335 	scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
336 		    RW_MGR_MEM_DQ_PER_WRITE_DQS + 1 + dm,
337 		    delay);
338 }
339 
340 /* load up dqs config settings */
341 static void scc_mgr_load_dqs(uint32_t dqs)
342 {
343 	writel(dqs, &sdr_scc_mgr->dqs_ena);
344 }
345 
346 /* load up dqs io config settings */
347 static void scc_mgr_load_dqs_io(void)
348 {
349 	writel(0, &sdr_scc_mgr->dqs_io_ena);
350 }
351 
352 /* load up dq config settings */
353 static void scc_mgr_load_dq(uint32_t dq_in_group)
354 {
355 	writel(dq_in_group, &sdr_scc_mgr->dq_ena);
356 }
357 
358 /* load up dm config settings */
359 static void scc_mgr_load_dm(uint32_t dm)
360 {
361 	writel(dm, &sdr_scc_mgr->dm_ena);
362 }
363 
364 /**
365  * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
366  * @off:	Base offset in SCC Manager space
367  * @grp:	Read/Write group
368  * @val:	Value to be set
369  * @update:	If non-zero, trigger SCC Manager update for all ranks
370  *
371  * This function sets the SCC Manager (Scan Chain Control Manager) register
372  * and optionally triggers the SCC update for all ranks.
373  */
374 static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
375 				  const int update)
376 {
377 	u32 r;
378 
379 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
380 	     r += NUM_RANKS_PER_SHADOW_REG) {
381 		scc_mgr_set(off, grp, val);
382 
383 		if (update || (r == 0)) {
384 			writel(grp, &sdr_scc_mgr->dqs_ena);
385 			writel(0, &sdr_scc_mgr->update);
386 		}
387 	}
388 }
389 
390 static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
391 {
392 	/*
393 	 * USER although the h/w doesn't support different phases per
394 	 * shadow register, for simplicity our scc manager modeling
395 	 * keeps different phase settings per shadow reg, and it's
396 	 * important for us to keep them in sync to match h/w.
397 	 * for efficiency, the scan chain update should occur only
398 	 * once to sr0.
399 	 */
400 	scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
401 			      read_group, phase, 0);
402 }
403 
404 static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
405 						     uint32_t phase)
406 {
407 	/*
408 	 * USER although the h/w doesn't support different phases per
409 	 * shadow register, for simplicity our scc manager modeling
410 	 * keeps different phase settings per shadow reg, and it's
411 	 * important for us to keep them in sync to match h/w.
412 	 * for efficiency, the scan chain update should occur only
413 	 * once to sr0.
414 	 */
415 	scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
416 			      write_group, phase, 0);
417 }
418 
419 static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
420 					       uint32_t delay)
421 {
422 	/*
423 	 * In shadow register mode, the T11 settings are stored in
424 	 * registers in the core, which are updated by the DQS_ENA
425 	 * signals. Not issuing the SCC_MGR_UPD command allows us to
426 	 * save lots of rank switching overhead, by calling
427 	 * select_shadow_regs_for_update with update_scan_chains
428 	 * set to 0.
429 	 */
430 	scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
431 			      read_group, delay, 1);
432 	writel(0, &sdr_scc_mgr->update);
433 }
434 
435 static void scc_mgr_set_oct_out1_delay(uint32_t write_group, uint32_t delay)
436 {
437 	uint32_t read_group;
438 	uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_OCT_OUT1_DELAY_OFFSET;
439 
440 	/*
441 	 * Load the setting in the SCC manager
442 	 * Although OCT affects only write data, the OCT delay is controlled
443 	 * by the DQS logic block which is instantiated once per read group.
444 	 * For protocols where a write group consists of multiple read groups,
445 	 * the setting must be set multiple times.
446 	 */
447 	for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
448 	     RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
449 	     read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
450 	     RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
451 		writel(delay, addr + (read_group << 2));
452 }
453 
454 static void scc_mgr_set_hhp_extras(void)
455 {
456 	/*
457 	 * Load the fixed setting in the SCC manager
458 	 * bits: 0:0 = 1'b1   - dqs bypass
459 	 * bits: 1:1 = 1'b1   - dq bypass
460 	 * bits: 4:2 = 3'b001   - rfifo_mode
461 	 * bits: 6:5 = 2'b01  - rfifo clock_select
462 	 * bits: 7:7 = 1'b0  - separate gating from ungating setting
463 	 * bits: 8:8 = 1'b0  - separate OE from Output delay setting
464 	 */
465 	uint32_t value = (0<<8) | (0<<7) | (1<<5) | (1<<2) | (1<<1) | (1<<0);
466 	uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_HHP_GLOBALS_OFFSET;
467 
468 	writel(value, addr + SCC_MGR_HHP_EXTRAS_OFFSET);
469 }
470 
471 /*
472  * USER Zero all DQS config
473  * TODO: maybe rename to scc_mgr_zero_dqs_config (or something)
474  */
475 static void scc_mgr_zero_all(void)
476 {
477 	uint32_t i, r;
478 
479 	/*
480 	 * USER Zero all DQS config settings, across all groups and all
481 	 * shadow registers
482 	 */
483 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
484 	     NUM_RANKS_PER_SHADOW_REG) {
485 		for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
486 			/*
487 			 * The phases actually don't exist on a per-rank basis,
488 			 * but there's no harm updating them several times, so
489 			 * let's keep the code simple.
490 			 */
491 			scc_mgr_set_dqs_bus_in_delay(i, IO_DQS_IN_RESERVE);
492 			scc_mgr_set_dqs_en_phase(i, 0);
493 			scc_mgr_set_dqs_en_delay(i, 0);
494 		}
495 
496 		for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
497 			scc_mgr_set_dqdqs_output_phase(i, 0);
498 			/* av/cv don't have out2 */
499 			scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
500 		}
501 	}
502 
503 	/* multicast to all DQS group enables */
504 	writel(0xff, &sdr_scc_mgr->dqs_ena);
505 	writel(0, &sdr_scc_mgr->update);
506 }
507 
508 static void scc_set_bypass_mode(uint32_t write_group, uint32_t mode)
509 {
510 	/* mode = 0 : Do NOT bypass - Half Rate Mode */
511 	/* mode = 1 : Bypass - Full Rate Mode */
512 
513 	/* only need to set once for all groups, pins, dq, dqs, dm */
514 	if (write_group == 0) {
515 		debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n", __func__,
516 			   __LINE__);
517 		scc_mgr_set_hhp_extras();
518 		debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
519 			  __func__, __LINE__);
520 	}
521 	/* multicast to all DQ enables */
522 	writel(0xff, &sdr_scc_mgr->dq_ena);
523 	writel(0xff, &sdr_scc_mgr->dm_ena);
524 
525 	/* update current DQS IO enable */
526 	writel(0, &sdr_scc_mgr->dqs_io_ena);
527 
528 	/* update the DQS logic */
529 	writel(write_group, &sdr_scc_mgr->dqs_ena);
530 
531 	/* hit update */
532 	writel(0, &sdr_scc_mgr->update);
533 }
534 
535 static void scc_mgr_load_dqs_for_write_group(uint32_t write_group)
536 {
537 	uint32_t read_group;
538 	uint32_t addr = (u32)&sdr_scc_mgr->dqs_ena;
539 	/*
540 	 * Although OCT affects only write data, the OCT delay is controlled
541 	 * by the DQS logic block which is instantiated once per read group.
542 	 * For protocols where a write group consists of multiple read groups,
543 	 * the setting must be scanned multiple times.
544 	 */
545 	for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
546 	     RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
547 	     read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
548 	     RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
549 		writel(read_group, addr);
550 }
551 
552 static void scc_mgr_zero_group(uint32_t write_group, uint32_t test_begin,
553 			       int32_t out_only)
554 {
555 	uint32_t i, r;
556 
557 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
558 		NUM_RANKS_PER_SHADOW_REG) {
559 		/* Zero all DQ config settings */
560 		for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
561 			scc_mgr_set_dq_out1_delay(i, 0);
562 			if (!out_only)
563 				scc_mgr_set_dq_in_delay(i, 0);
564 		}
565 
566 		/* multicast to all DQ enables */
567 		writel(0xff, &sdr_scc_mgr->dq_ena);
568 
569 		/* Zero all DM config settings */
570 		for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
571 			scc_mgr_set_dm_out1_delay(i, 0);
572 		}
573 
574 		/* multicast to all DM enables */
575 		writel(0xff, &sdr_scc_mgr->dm_ena);
576 
577 		/* zero all DQS io settings */
578 		if (!out_only)
579 			scc_mgr_set_dqs_io_in_delay(write_group, 0);
580 		/* av/cv don't have out2 */
581 		scc_mgr_set_dqs_out1_delay(write_group, IO_DQS_OUT_RESERVE);
582 		scc_mgr_set_oct_out1_delay(write_group, IO_DQS_OUT_RESERVE);
583 		scc_mgr_load_dqs_for_write_group(write_group);
584 
585 		/* multicast to all DQS IO enables (only 1) */
586 		writel(0, &sdr_scc_mgr->dqs_io_ena);
587 
588 		/* hit update to zero everything */
589 		writel(0, &sdr_scc_mgr->update);
590 	}
591 }
592 
593 /*
594  * apply and load a particular input delay for the DQ pins in a group
595  * group_bgn is the index of the first dq pin (in the write group)
596  */
597 static void scc_mgr_apply_group_dq_in_delay(uint32_t write_group,
598 					    uint32_t group_bgn, uint32_t delay)
599 {
600 	uint32_t i, p;
601 
602 	for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
603 		scc_mgr_set_dq_in_delay(p, delay);
604 		scc_mgr_load_dq(p);
605 	}
606 }
607 
608 /* apply and load a particular output delay for the DQ pins in a group */
609 static void scc_mgr_apply_group_dq_out1_delay(uint32_t write_group,
610 					      uint32_t group_bgn,
611 					      uint32_t delay1)
612 {
613 	uint32_t i, p;
614 
615 	for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
616 		scc_mgr_set_dq_out1_delay(i, delay1);
617 		scc_mgr_load_dq(i);
618 	}
619 }
620 
621 /* apply and load a particular output delay for the DM pins in a group */
622 static void scc_mgr_apply_group_dm_out1_delay(uint32_t write_group,
623 					      uint32_t delay1)
624 {
625 	uint32_t i;
626 
627 	for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
628 		scc_mgr_set_dm_out1_delay(i, delay1);
629 		scc_mgr_load_dm(i);
630 	}
631 }
632 
633 
634 /* apply and load delay on both DQS and OCT out1 */
635 static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
636 						    uint32_t delay)
637 {
638 	scc_mgr_set_dqs_out1_delay(write_group, delay);
639 	scc_mgr_load_dqs_io();
640 
641 	scc_mgr_set_oct_out1_delay(write_group, delay);
642 	scc_mgr_load_dqs_for_write_group(write_group);
643 }
644 
645 /* apply a delay to the entire output side: DQ, DM, DQS, OCT */
646 static void scc_mgr_apply_group_all_out_delay_add(uint32_t write_group,
647 						  uint32_t group_bgn,
648 						  uint32_t delay)
649 {
650 	uint32_t i, p, new_delay;
651 
652 	/* dq shift */
653 	for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
654 		new_delay = READ_SCC_DQ_OUT2_DELAY;
655 		new_delay += delay;
656 
657 		if (new_delay > IO_IO_OUT2_DELAY_MAX) {
658 			debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQ[%u,%u]:\
659 				   %u > %lu => %lu", __func__, __LINE__,
660 				   write_group, group_bgn, delay, i, p, new_delay,
661 				   (long unsigned int)IO_IO_OUT2_DELAY_MAX,
662 				   (long unsigned int)IO_IO_OUT2_DELAY_MAX);
663 			new_delay = IO_IO_OUT2_DELAY_MAX;
664 		}
665 
666 		scc_mgr_load_dq(i);
667 	}
668 
669 	/* dm shift */
670 	for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
671 		new_delay = READ_SCC_DM_IO_OUT2_DELAY;
672 		new_delay += delay;
673 
674 		if (new_delay > IO_IO_OUT2_DELAY_MAX) {
675 			debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DM[%u]:\
676 				   %u > %lu => %lu\n",  __func__, __LINE__,
677 				   write_group, group_bgn, delay, i, new_delay,
678 				   (long unsigned int)IO_IO_OUT2_DELAY_MAX,
679 				   (long unsigned int)IO_IO_OUT2_DELAY_MAX);
680 			new_delay = IO_IO_OUT2_DELAY_MAX;
681 		}
682 
683 		scc_mgr_load_dm(i);
684 	}
685 
686 	/* dqs shift */
687 	new_delay = READ_SCC_DQS_IO_OUT2_DELAY;
688 	new_delay += delay;
689 
690 	if (new_delay > IO_IO_OUT2_DELAY_MAX) {
691 		debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
692 			   " adding %u to OUT1\n", __func__, __LINE__,
693 			   write_group, group_bgn, delay, new_delay,
694 			   IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
695 			   new_delay - IO_IO_OUT2_DELAY_MAX);
696 		scc_mgr_set_dqs_out1_delay(write_group, new_delay -
697 					   IO_IO_OUT2_DELAY_MAX);
698 		new_delay = IO_IO_OUT2_DELAY_MAX;
699 	}
700 
701 	scc_mgr_load_dqs_io();
702 
703 	/* oct shift */
704 	new_delay = READ_SCC_OCT_OUT2_DELAY;
705 	new_delay += delay;
706 
707 	if (new_delay > IO_IO_OUT2_DELAY_MAX) {
708 		debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
709 			   " adding %u to OUT1\n", __func__, __LINE__,
710 			   write_group, group_bgn, delay, new_delay,
711 			   IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
712 			   new_delay - IO_IO_OUT2_DELAY_MAX);
713 		scc_mgr_set_oct_out1_delay(write_group, new_delay -
714 					   IO_IO_OUT2_DELAY_MAX);
715 		new_delay = IO_IO_OUT2_DELAY_MAX;
716 	}
717 
718 	scc_mgr_load_dqs_for_write_group(write_group);
719 }
720 
721 /*
722  * USER apply a delay to the entire output side (DQ, DM, DQS, OCT)
723  * and to all ranks
724  */
725 static void scc_mgr_apply_group_all_out_delay_add_all_ranks(
726 	uint32_t write_group, uint32_t group_bgn, uint32_t delay)
727 {
728 	uint32_t r;
729 
730 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
731 		r += NUM_RANKS_PER_SHADOW_REG) {
732 		scc_mgr_apply_group_all_out_delay_add(write_group,
733 						      group_bgn, delay);
734 		writel(0, &sdr_scc_mgr->update);
735 	}
736 }
737 
738 /* optimization used to recover some slots in ddr3 inst_rom */
739 /* could be applied to other protocols if we wanted to */
740 static void set_jump_as_return(void)
741 {
742 	/*
743 	 * to save space, we replace return with jump to special shared
744 	 * RETURN instruction so we set the counter to large value so that
745 	 * we always jump
746 	 */
747 	writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
748 	writel(RW_MGR_RETURN, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
749 }
750 
751 /*
752  * should always use constants as argument to ensure all computations are
753  * performed at compile time
754  */
755 static void delay_for_n_mem_clocks(const uint32_t clocks)
756 {
757 	uint32_t afi_clocks;
758 	uint8_t inner = 0;
759 	uint8_t outer = 0;
760 	uint16_t c_loop = 0;
761 
762 	debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
763 
764 
765 	afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
766 	/* scale (rounding up) to get afi clocks */
767 
768 	/*
769 	 * Note, we don't bother accounting for being off a little bit
770 	 * because of a few extra instructions in outer loops
771 	 * Note, the loops have a test at the end, and do the test before
772 	 * the decrement, and so always perform the loop
773 	 * 1 time more than the counter value
774 	 */
775 	if (afi_clocks == 0) {
776 		;
777 	} else if (afi_clocks <= 0x100) {
778 		inner = afi_clocks-1;
779 		outer = 0;
780 		c_loop = 0;
781 	} else if (afi_clocks <= 0x10000) {
782 		inner = 0xff;
783 		outer = (afi_clocks-1) >> 8;
784 		c_loop = 0;
785 	} else {
786 		inner = 0xff;
787 		outer = 0xff;
788 		c_loop = (afi_clocks-1) >> 16;
789 	}
790 
791 	/*
792 	 * rom instructions are structured as follows:
793 	 *
794 	 *    IDLE_LOOP2: jnz cntr0, TARGET_A
795 	 *    IDLE_LOOP1: jnz cntr1, TARGET_B
796 	 *                return
797 	 *
798 	 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
799 	 * TARGET_B is set to IDLE_LOOP2 as well
800 	 *
801 	 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
802 	 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
803 	 *
804 	 * a little confusing, but it helps save precious space in the inst_rom
805 	 * and sequencer rom and keeps the delays more accurate and reduces
806 	 * overhead
807 	 */
808 	if (afi_clocks <= 0x100) {
809 		writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
810 			&sdr_rw_load_mgr_regs->load_cntr1);
811 
812 		writel(RW_MGR_IDLE_LOOP1,
813 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
814 
815 		writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
816 					  RW_MGR_RUN_SINGLE_GROUP_OFFSET);
817 	} else {
818 		writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
819 			&sdr_rw_load_mgr_regs->load_cntr0);
820 
821 		writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
822 			&sdr_rw_load_mgr_regs->load_cntr1);
823 
824 		writel(RW_MGR_IDLE_LOOP2,
825 			&sdr_rw_load_jump_mgr_regs->load_jump_add0);
826 
827 		writel(RW_MGR_IDLE_LOOP2,
828 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
829 
830 		/* hack to get around compiler not being smart enough */
831 		if (afi_clocks <= 0x10000) {
832 			/* only need to run once */
833 			writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
834 						  RW_MGR_RUN_SINGLE_GROUP_OFFSET);
835 		} else {
836 			do {
837 				writel(RW_MGR_IDLE_LOOP2,
838 					SDR_PHYGRP_RWMGRGRP_ADDRESS |
839 					RW_MGR_RUN_SINGLE_GROUP_OFFSET);
840 			} while (c_loop-- != 0);
841 		}
842 	}
843 	debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
844 }
845 
846 static void rw_mgr_mem_initialize(void)
847 {
848 	uint32_t r;
849 	uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
850 			   RW_MGR_RUN_SINGLE_GROUP_OFFSET;
851 
852 	debug("%s:%d\n", __func__, __LINE__);
853 
854 	/* The reset / cke part of initialization is broadcasted to all ranks */
855 	writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
856 				RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
857 
858 	/*
859 	 * Here's how you load register for a loop
860 	 * Counters are located @ 0x800
861 	 * Jump address are located @ 0xC00
862 	 * For both, registers 0 to 3 are selected using bits 3 and 2, like
863 	 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
864 	 * I know this ain't pretty, but Avalon bus throws away the 2 least
865 	 * significant bits
866 	 */
867 
868 	/* start with memory RESET activated */
869 
870 	/* tINIT = 200us */
871 
872 	/*
873 	 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
874 	 * If a and b are the number of iteration in 2 nested loops
875 	 * it takes the following number of cycles to complete the operation:
876 	 * number_of_cycles = ((2 + n) * a + 2) * b
877 	 * where n is the number of instruction in the inner loop
878 	 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
879 	 * b = 6A
880 	 */
881 
882 	/* Load counters */
883 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR0_VAL),
884 	       &sdr_rw_load_mgr_regs->load_cntr0);
885 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR1_VAL),
886 	       &sdr_rw_load_mgr_regs->load_cntr1);
887 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR2_VAL),
888 	       &sdr_rw_load_mgr_regs->load_cntr2);
889 
890 	/* Load jump address */
891 	writel(RW_MGR_INIT_RESET_0_CKE_0,
892 		&sdr_rw_load_jump_mgr_regs->load_jump_add0);
893 	writel(RW_MGR_INIT_RESET_0_CKE_0,
894 		&sdr_rw_load_jump_mgr_regs->load_jump_add1);
895 	writel(RW_MGR_INIT_RESET_0_CKE_0,
896 		&sdr_rw_load_jump_mgr_regs->load_jump_add2);
897 
898 	/* Execute count instruction */
899 	writel(RW_MGR_INIT_RESET_0_CKE_0, grpaddr);
900 
901 	/* indicate that memory is stable */
902 	writel(1, &phy_mgr_cfg->reset_mem_stbl);
903 
904 	/*
905 	 * transition the RESET to high
906 	 * Wait for 500us
907 	 */
908 
909 	/*
910 	 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
911 	 * If a and b are the number of iteration in 2 nested loops
912 	 * it takes the following number of cycles to complete the operation
913 	 * number_of_cycles = ((2 + n) * a + 2) * b
914 	 * where n is the number of instruction in the inner loop
915 	 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
916 	 * b = FF
917 	 */
918 
919 	/* Load counters */
920 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR0_VAL),
921 	       &sdr_rw_load_mgr_regs->load_cntr0);
922 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR1_VAL),
923 	       &sdr_rw_load_mgr_regs->load_cntr1);
924 	writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR2_VAL),
925 	       &sdr_rw_load_mgr_regs->load_cntr2);
926 
927 	/* Load jump address */
928 	writel(RW_MGR_INIT_RESET_1_CKE_0,
929 		&sdr_rw_load_jump_mgr_regs->load_jump_add0);
930 	writel(RW_MGR_INIT_RESET_1_CKE_0,
931 		&sdr_rw_load_jump_mgr_regs->load_jump_add1);
932 	writel(RW_MGR_INIT_RESET_1_CKE_0,
933 		&sdr_rw_load_jump_mgr_regs->load_jump_add2);
934 
935 	writel(RW_MGR_INIT_RESET_1_CKE_0, grpaddr);
936 
937 	/* bring up clock enable */
938 
939 	/* tXRP < 250 ck cycles */
940 	delay_for_n_mem_clocks(250);
941 
942 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
943 		if (param->skip_ranks[r]) {
944 			/* request to skip the rank */
945 			continue;
946 		}
947 
948 		/* set rank */
949 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
950 
951 		/*
952 		 * USER Use Mirror-ed commands for odd ranks if address
953 		 * mirrorring is on
954 		 */
955 		if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
956 			set_jump_as_return();
957 			writel(RW_MGR_MRS2_MIRR, grpaddr);
958 			delay_for_n_mem_clocks(4);
959 			set_jump_as_return();
960 			writel(RW_MGR_MRS3_MIRR, grpaddr);
961 			delay_for_n_mem_clocks(4);
962 			set_jump_as_return();
963 			writel(RW_MGR_MRS1_MIRR, grpaddr);
964 			delay_for_n_mem_clocks(4);
965 			set_jump_as_return();
966 			writel(RW_MGR_MRS0_DLL_RESET_MIRR, grpaddr);
967 		} else {
968 			set_jump_as_return();
969 			writel(RW_MGR_MRS2, grpaddr);
970 			delay_for_n_mem_clocks(4);
971 			set_jump_as_return();
972 			writel(RW_MGR_MRS3, grpaddr);
973 			delay_for_n_mem_clocks(4);
974 			set_jump_as_return();
975 			writel(RW_MGR_MRS1, grpaddr);
976 			set_jump_as_return();
977 			writel(RW_MGR_MRS0_DLL_RESET, grpaddr);
978 		}
979 		set_jump_as_return();
980 		writel(RW_MGR_ZQCL, grpaddr);
981 
982 		/* tZQinit = tDLLK = 512 ck cycles */
983 		delay_for_n_mem_clocks(512);
984 	}
985 }
986 
987 /*
988  * At the end of calibration we have to program the user settings in, and
989  * USER  hand off the memory to the user.
990  */
991 static void rw_mgr_mem_handoff(void)
992 {
993 	uint32_t r;
994 	uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
995 			   RW_MGR_RUN_SINGLE_GROUP_OFFSET;
996 
997 	debug("%s:%d\n", __func__, __LINE__);
998 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
999 		if (param->skip_ranks[r])
1000 			/* request to skip the rank */
1001 			continue;
1002 		/* set rank */
1003 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
1004 
1005 		/* precharge all banks ... */
1006 		writel(RW_MGR_PRECHARGE_ALL, grpaddr);
1007 
1008 		/* load up MR settings specified by user */
1009 
1010 		/*
1011 		 * Use Mirror-ed commands for odd ranks if address
1012 		 * mirrorring is on
1013 		 */
1014 		if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
1015 			set_jump_as_return();
1016 			writel(RW_MGR_MRS2_MIRR, grpaddr);
1017 			delay_for_n_mem_clocks(4);
1018 			set_jump_as_return();
1019 			writel(RW_MGR_MRS3_MIRR, grpaddr);
1020 			delay_for_n_mem_clocks(4);
1021 			set_jump_as_return();
1022 			writel(RW_MGR_MRS1_MIRR, grpaddr);
1023 			delay_for_n_mem_clocks(4);
1024 			set_jump_as_return();
1025 			writel(RW_MGR_MRS0_USER_MIRR, grpaddr);
1026 		} else {
1027 			set_jump_as_return();
1028 			writel(RW_MGR_MRS2, grpaddr);
1029 			delay_for_n_mem_clocks(4);
1030 			set_jump_as_return();
1031 			writel(RW_MGR_MRS3, grpaddr);
1032 			delay_for_n_mem_clocks(4);
1033 			set_jump_as_return();
1034 			writel(RW_MGR_MRS1, grpaddr);
1035 			delay_for_n_mem_clocks(4);
1036 			set_jump_as_return();
1037 			writel(RW_MGR_MRS0_USER, grpaddr);
1038 		}
1039 		/*
1040 		 * USER  need to wait tMOD (12CK or 15ns) time before issuing
1041 		 * other commands, but we will have plenty of NIOS cycles before
1042 		 * actual handoff so its okay.
1043 		 */
1044 	}
1045 }
1046 
1047 /*
1048  * performs a guaranteed read on the patterns we are going to use during a
1049  * read test to ensure memory works
1050  */
1051 static uint32_t rw_mgr_mem_calibrate_read_test_patterns(uint32_t rank_bgn,
1052 	uint32_t group, uint32_t num_tries, uint32_t *bit_chk,
1053 	uint32_t all_ranks)
1054 {
1055 	uint32_t r, vg;
1056 	uint32_t correct_mask_vg;
1057 	uint32_t tmp_bit_chk;
1058 	uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1059 		(rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1060 	uint32_t addr;
1061 	uint32_t base_rw_mgr;
1062 
1063 	*bit_chk = param->read_correct_mask;
1064 	correct_mask_vg = param->read_correct_mask_vg;
1065 
1066 	for (r = rank_bgn; r < rank_end; r++) {
1067 		if (param->skip_ranks[r])
1068 			/* request to skip the rank */
1069 			continue;
1070 
1071 		/* set rank */
1072 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1073 
1074 		/* Load up a constant bursts of read commands */
1075 		writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1076 		writel(RW_MGR_GUARANTEED_READ,
1077 			&sdr_rw_load_jump_mgr_regs->load_jump_add0);
1078 
1079 		writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1080 		writel(RW_MGR_GUARANTEED_READ_CONT,
1081 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
1082 
1083 		tmp_bit_chk = 0;
1084 		for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1085 			/* reset the fifos to get pointers to known state */
1086 
1087 			writel(0, &phy_mgr_cmd->fifo_reset);
1088 			writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1089 				  RW_MGR_RESET_READ_DATAPATH_OFFSET);
1090 
1091 			tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1092 				/ RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1093 
1094 			addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1095 			writel(RW_MGR_GUARANTEED_READ, addr +
1096 			       ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1097 				vg) << 2));
1098 
1099 			base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1100 			tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & (~base_rw_mgr));
1101 
1102 			if (vg == 0)
1103 				break;
1104 		}
1105 		*bit_chk &= tmp_bit_chk;
1106 	}
1107 
1108 	addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1109 	writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
1110 
1111 	set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1112 	debug_cond(DLEVEL == 1, "%s:%d test_load_patterns(%u,ALL) => (%u == %u) =>\
1113 		   %lu\n", __func__, __LINE__, group, *bit_chk, param->read_correct_mask,
1114 		   (long unsigned int)(*bit_chk == param->read_correct_mask));
1115 	return *bit_chk == param->read_correct_mask;
1116 }
1117 
1118 static uint32_t rw_mgr_mem_calibrate_read_test_patterns_all_ranks
1119 	(uint32_t group, uint32_t num_tries, uint32_t *bit_chk)
1120 {
1121 	return rw_mgr_mem_calibrate_read_test_patterns(0, group,
1122 		num_tries, bit_chk, 1);
1123 }
1124 
1125 /* load up the patterns we are going to use during a read test */
1126 static void rw_mgr_mem_calibrate_read_load_patterns(uint32_t rank_bgn,
1127 	uint32_t all_ranks)
1128 {
1129 	uint32_t r;
1130 	uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1131 		(rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1132 
1133 	debug("%s:%d\n", __func__, __LINE__);
1134 	for (r = rank_bgn; r < rank_end; r++) {
1135 		if (param->skip_ranks[r])
1136 			/* request to skip the rank */
1137 			continue;
1138 
1139 		/* set rank */
1140 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1141 
1142 		/* Load up a constant bursts */
1143 		writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1144 
1145 		writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1146 			&sdr_rw_load_jump_mgr_regs->load_jump_add0);
1147 
1148 		writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1149 
1150 		writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1151 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
1152 
1153 		writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
1154 
1155 		writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1156 			&sdr_rw_load_jump_mgr_regs->load_jump_add2);
1157 
1158 		writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
1159 
1160 		writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1161 			&sdr_rw_load_jump_mgr_regs->load_jump_add3);
1162 
1163 		writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1164 						RW_MGR_RUN_SINGLE_GROUP_OFFSET);
1165 	}
1166 
1167 	set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1168 }
1169 
1170 /*
1171  * try a read and see if it returns correct data back. has dummy reads
1172  * inserted into the mix used to align dqs enable. has more thorough checks
1173  * than the regular read test.
1174  */
1175 static uint32_t rw_mgr_mem_calibrate_read_test(uint32_t rank_bgn, uint32_t group,
1176 	uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1177 	uint32_t all_groups, uint32_t all_ranks)
1178 {
1179 	uint32_t r, vg;
1180 	uint32_t correct_mask_vg;
1181 	uint32_t tmp_bit_chk;
1182 	uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1183 		(rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1184 	uint32_t addr;
1185 	uint32_t base_rw_mgr;
1186 
1187 	*bit_chk = param->read_correct_mask;
1188 	correct_mask_vg = param->read_correct_mask_vg;
1189 
1190 	uint32_t quick_read_mode = (((STATIC_CALIB_STEPS) &
1191 		CALIB_SKIP_DELAY_SWEEPS) && ENABLE_SUPER_QUICK_CALIBRATION);
1192 
1193 	for (r = rank_bgn; r < rank_end; r++) {
1194 		if (param->skip_ranks[r])
1195 			/* request to skip the rank */
1196 			continue;
1197 
1198 		/* set rank */
1199 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1200 
1201 		writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
1202 
1203 		writel(RW_MGR_READ_B2B_WAIT1,
1204 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
1205 
1206 		writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1207 		writel(RW_MGR_READ_B2B_WAIT2,
1208 			&sdr_rw_load_jump_mgr_regs->load_jump_add2);
1209 
1210 		if (quick_read_mode)
1211 			writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
1212 			/* need at least two (1+1) reads to capture failures */
1213 		else if (all_groups)
1214 			writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
1215 		else
1216 			writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
1217 
1218 		writel(RW_MGR_READ_B2B,
1219 			&sdr_rw_load_jump_mgr_regs->load_jump_add0);
1220 		if (all_groups)
1221 			writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1222 			       RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
1223 			       &sdr_rw_load_mgr_regs->load_cntr3);
1224 		else
1225 			writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
1226 
1227 		writel(RW_MGR_READ_B2B,
1228 			&sdr_rw_load_jump_mgr_regs->load_jump_add3);
1229 
1230 		tmp_bit_chk = 0;
1231 		for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1232 			/* reset the fifos to get pointers to known state */
1233 			writel(0, &phy_mgr_cmd->fifo_reset);
1234 			writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1235 				  RW_MGR_RESET_READ_DATAPATH_OFFSET);
1236 
1237 			tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1238 				/ RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1239 
1240 			if (all_groups)
1241 				addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_ALL_GROUPS_OFFSET;
1242 			else
1243 				addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1244 
1245 			writel(RW_MGR_READ_B2B, addr +
1246 			       ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1247 			       vg) << 2));
1248 
1249 			base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1250 			tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
1251 
1252 			if (vg == 0)
1253 				break;
1254 		}
1255 		*bit_chk &= tmp_bit_chk;
1256 	}
1257 
1258 	addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1259 	writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
1260 
1261 	if (all_correct) {
1262 		set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1263 		debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ALL,%u) =>\
1264 			   (%u == %u) => %lu", __func__, __LINE__, group,
1265 			   all_groups, *bit_chk, param->read_correct_mask,
1266 			   (long unsigned int)(*bit_chk ==
1267 			   param->read_correct_mask));
1268 		return *bit_chk == param->read_correct_mask;
1269 	} else	{
1270 		set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1271 		debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ONE,%u) =>\
1272 			   (%u != %lu) => %lu\n", __func__, __LINE__,
1273 			   group, all_groups, *bit_chk, (long unsigned int)0,
1274 			   (long unsigned int)(*bit_chk != 0x00));
1275 		return *bit_chk != 0x00;
1276 	}
1277 }
1278 
1279 static uint32_t rw_mgr_mem_calibrate_read_test_all_ranks(uint32_t group,
1280 	uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1281 	uint32_t all_groups)
1282 {
1283 	return rw_mgr_mem_calibrate_read_test(0, group, num_tries, all_correct,
1284 					      bit_chk, all_groups, 1);
1285 }
1286 
1287 static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
1288 {
1289 	writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
1290 	(*v)++;
1291 }
1292 
1293 static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
1294 {
1295 	uint32_t i;
1296 
1297 	for (i = 0; i < VFIFO_SIZE-1; i++)
1298 		rw_mgr_incr_vfifo(grp, v);
1299 }
1300 
1301 static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
1302 {
1303 	uint32_t  v;
1304 	uint32_t fail_cnt = 0;
1305 	uint32_t test_status;
1306 
1307 	for (v = 0; v < VFIFO_SIZE; ) {
1308 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
1309 			   __func__, __LINE__, v);
1310 		test_status = rw_mgr_mem_calibrate_read_test_all_ranks
1311 			(grp, 1, PASS_ONE_BIT, bit_chk, 0);
1312 		if (!test_status) {
1313 			fail_cnt++;
1314 
1315 			if (fail_cnt == 2)
1316 				break;
1317 		}
1318 
1319 		/* fiddle with FIFO */
1320 		rw_mgr_incr_vfifo(grp, &v);
1321 	}
1322 
1323 	if (v >= VFIFO_SIZE) {
1324 		/* no failing read found!! Something must have gone wrong */
1325 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
1326 			   __func__, __LINE__);
1327 		return 0;
1328 	} else {
1329 		return v;
1330 	}
1331 }
1332 
1333 static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
1334 			      uint32_t dtaps_per_ptap, uint32_t *work_bgn,
1335 			      uint32_t *v, uint32_t *d, uint32_t *p,
1336 			      uint32_t *i, uint32_t *max_working_cnt)
1337 {
1338 	uint32_t found_begin = 0;
1339 	uint32_t tmp_delay = 0;
1340 	uint32_t test_status;
1341 
1342 	for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
1343 		IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1344 		*work_bgn = tmp_delay;
1345 		scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1346 
1347 		for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
1348 			for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
1349 				IO_DELAY_PER_OPA_TAP) {
1350 				scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1351 
1352 				test_status =
1353 				rw_mgr_mem_calibrate_read_test_all_ranks
1354 				(*grp, 1, PASS_ONE_BIT, bit_chk, 0);
1355 
1356 				if (test_status) {
1357 					*max_working_cnt = 1;
1358 					found_begin = 1;
1359 					break;
1360 				}
1361 			}
1362 
1363 			if (found_begin)
1364 				break;
1365 
1366 			if (*p > IO_DQS_EN_PHASE_MAX)
1367 				/* fiddle with FIFO */
1368 				rw_mgr_incr_vfifo(*grp, v);
1369 		}
1370 
1371 		if (found_begin)
1372 			break;
1373 	}
1374 
1375 	if (*i >= VFIFO_SIZE) {
1376 		/* cannot find working solution */
1377 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
1378 			   ptap/dtap\n", __func__, __LINE__);
1379 		return 0;
1380 	} else {
1381 		return 1;
1382 	}
1383 }
1384 
1385 static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
1386 			     uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1387 			     uint32_t *p, uint32_t *max_working_cnt)
1388 {
1389 	uint32_t found_begin = 0;
1390 	uint32_t tmp_delay;
1391 
1392 	/* Special case code for backing up a phase */
1393 	if (*p == 0) {
1394 		*p = IO_DQS_EN_PHASE_MAX;
1395 		rw_mgr_decr_vfifo(*grp, v);
1396 	} else {
1397 		(*p)--;
1398 	}
1399 	tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
1400 	scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1401 
1402 	for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
1403 		(*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1404 		scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1405 
1406 		if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1407 							     PASS_ONE_BIT,
1408 							     bit_chk, 0)) {
1409 			found_begin = 1;
1410 			*work_bgn = tmp_delay;
1411 			break;
1412 		}
1413 	}
1414 
1415 	/* We have found a working dtap before the ptap found above */
1416 	if (found_begin == 1)
1417 		(*max_working_cnt)++;
1418 
1419 	/*
1420 	 * Restore VFIFO to old state before we decremented it
1421 	 * (if needed).
1422 	 */
1423 	(*p)++;
1424 	if (*p > IO_DQS_EN_PHASE_MAX) {
1425 		*p = 0;
1426 		rw_mgr_incr_vfifo(*grp, v);
1427 	}
1428 
1429 	scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
1430 }
1431 
1432 static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
1433 			     uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1434 			     uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
1435 			     uint32_t *work_end)
1436 {
1437 	uint32_t found_end = 0;
1438 
1439 	(*p)++;
1440 	*work_end += IO_DELAY_PER_OPA_TAP;
1441 	if (*p > IO_DQS_EN_PHASE_MAX) {
1442 		/* fiddle with FIFO */
1443 		*p = 0;
1444 		rw_mgr_incr_vfifo(*grp, v);
1445 	}
1446 
1447 	for (; *i < VFIFO_SIZE + 1; (*i)++) {
1448 		for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
1449 			+= IO_DELAY_PER_OPA_TAP) {
1450 			scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1451 
1452 			if (!rw_mgr_mem_calibrate_read_test_all_ranks
1453 				(*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
1454 				found_end = 1;
1455 				break;
1456 			} else {
1457 				(*max_working_cnt)++;
1458 			}
1459 		}
1460 
1461 		if (found_end)
1462 			break;
1463 
1464 		if (*p > IO_DQS_EN_PHASE_MAX) {
1465 			/* fiddle with FIFO */
1466 			rw_mgr_incr_vfifo(*grp, v);
1467 			*p = 0;
1468 		}
1469 	}
1470 
1471 	if (*i >= VFIFO_SIZE + 1) {
1472 		/* cannot see edge of failing read */
1473 		debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
1474 			   failed\n", __func__, __LINE__);
1475 		return 0;
1476 	} else {
1477 		return 1;
1478 	}
1479 }
1480 
1481 static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
1482 				  uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1483 				  uint32_t *p, uint32_t *work_mid,
1484 				  uint32_t *work_end)
1485 {
1486 	int i;
1487 	int tmp_delay = 0;
1488 
1489 	*work_mid = (*work_bgn + *work_end) / 2;
1490 
1491 	debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
1492 		   *work_bgn, *work_end, *work_mid);
1493 	/* Get the middle delay to be less than a VFIFO delay */
1494 	for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
1495 		(*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1496 		;
1497 	debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
1498 	while (*work_mid > tmp_delay)
1499 		*work_mid -= tmp_delay;
1500 	debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
1501 
1502 	tmp_delay = 0;
1503 	for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
1504 		(*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1505 		;
1506 	tmp_delay -= IO_DELAY_PER_OPA_TAP;
1507 	debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
1508 	for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
1509 		tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
1510 		;
1511 	debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
1512 
1513 	scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
1514 	scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1515 
1516 	/*
1517 	 * push vfifo until we can successfully calibrate. We can do this
1518 	 * because the largest possible margin in 1 VFIFO cycle.
1519 	 */
1520 	for (i = 0; i < VFIFO_SIZE; i++) {
1521 		debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
1522 			   *v);
1523 		if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1524 							     PASS_ONE_BIT,
1525 							     bit_chk, 0)) {
1526 			break;
1527 		}
1528 
1529 		/* fiddle with FIFO */
1530 		rw_mgr_incr_vfifo(*grp, v);
1531 	}
1532 
1533 	if (i >= VFIFO_SIZE) {
1534 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
1535 			   failed\n", __func__, __LINE__);
1536 		return 0;
1537 	} else {
1538 		return 1;
1539 	}
1540 }
1541 
1542 /* find a good dqs enable to use */
1543 static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
1544 {
1545 	uint32_t v, d, p, i;
1546 	uint32_t max_working_cnt;
1547 	uint32_t bit_chk;
1548 	uint32_t dtaps_per_ptap;
1549 	uint32_t work_bgn, work_mid, work_end;
1550 	uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
1551 
1552 	debug("%s:%d %u\n", __func__, __LINE__, grp);
1553 
1554 	reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1555 
1556 	scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1557 	scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1558 
1559 	/* ************************************************************** */
1560 	/* * Step 0 : Determine number of delay taps for each phase tap * */
1561 	dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1562 
1563 	/* ********************************************************* */
1564 	/* * Step 1 : First push vfifo until we get a failing read * */
1565 	v = find_vfifo_read(grp, &bit_chk);
1566 
1567 	max_working_cnt = 0;
1568 
1569 	/* ******************************************************** */
1570 	/* * step 2: find first working phase, increment in ptaps * */
1571 	work_bgn = 0;
1572 	if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
1573 				&p, &i, &max_working_cnt) == 0)
1574 		return 0;
1575 
1576 	work_end = work_bgn;
1577 
1578 	/*
1579 	 * If d is 0 then the working window covers a phase tap and
1580 	 * we can follow the old procedure otherwise, we've found the beginning,
1581 	 * and we need to increment the dtaps until we find the end.
1582 	 */
1583 	if (d == 0) {
1584 		/* ********************************************************* */
1585 		/* * step 3a: if we have room, back off by one and
1586 		increment in dtaps * */
1587 
1588 		sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1589 				 &max_working_cnt);
1590 
1591 		/* ********************************************************* */
1592 		/* * step 4a: go forward from working phase to non working
1593 		phase, increment in ptaps * */
1594 		if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1595 					 &i, &max_working_cnt, &work_end) == 0)
1596 			return 0;
1597 
1598 		/* ********************************************************* */
1599 		/* * step 5a:  back off one from last, increment in dtaps  * */
1600 
1601 		/* Special case code for backing up a phase */
1602 		if (p == 0) {
1603 			p = IO_DQS_EN_PHASE_MAX;
1604 			rw_mgr_decr_vfifo(grp, &v);
1605 		} else {
1606 			p = p - 1;
1607 		}
1608 
1609 		work_end -= IO_DELAY_PER_OPA_TAP;
1610 		scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1611 
1612 		/* * The actual increment of dtaps is done outside of
1613 		the if/else loop to share code */
1614 		d = 0;
1615 
1616 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
1617 			   vfifo=%u ptap=%u\n", __func__, __LINE__,
1618 			   v, p);
1619 	} else {
1620 		/* ******************************************************* */
1621 		/* * step 3-5b:  Find the right edge of the window using
1622 		delay taps   * */
1623 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
1624 			   ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
1625 			   v, p, d, work_bgn);
1626 
1627 		work_end = work_bgn;
1628 
1629 		/* * The actual increment of dtaps is done outside of the
1630 		if/else loop to share code */
1631 
1632 		/* Only here to counterbalance a subtract later on which is
1633 		not needed if this branch of the algorithm is taken */
1634 		max_working_cnt++;
1635 	}
1636 
1637 	/* The dtap increment to find the failing edge is done here */
1638 	for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
1639 		IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1640 			debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1641 				   end-2: dtap=%u\n", __func__, __LINE__, d);
1642 			scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1643 
1644 			if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1645 								      PASS_ONE_BIT,
1646 								      &bit_chk, 0)) {
1647 				break;
1648 			}
1649 	}
1650 
1651 	/* Go back to working dtap */
1652 	if (d != 0)
1653 		work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1654 
1655 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
1656 		   ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
1657 		   v, p, d-1, work_end);
1658 
1659 	if (work_end < work_bgn) {
1660 		/* nil range */
1661 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
1662 			   failed\n", __func__, __LINE__);
1663 		return 0;
1664 	}
1665 
1666 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
1667 		   __func__, __LINE__, work_bgn, work_end);
1668 
1669 	/* *************************************************************** */
1670 	/*
1671 	 * * We need to calculate the number of dtaps that equal a ptap
1672 	 * * To do that we'll back up a ptap and re-find the edge of the
1673 	 * * window using dtaps
1674 	 */
1675 
1676 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
1677 		   for tracking\n", __func__, __LINE__);
1678 
1679 	/* Special case code for backing up a phase */
1680 	if (p == 0) {
1681 		p = IO_DQS_EN_PHASE_MAX;
1682 		rw_mgr_decr_vfifo(grp, &v);
1683 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1684 			   cycle/phase: v=%u p=%u\n", __func__, __LINE__,
1685 			   v, p);
1686 	} else {
1687 		p = p - 1;
1688 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1689 			   phase only: v=%u p=%u", __func__, __LINE__,
1690 			   v, p);
1691 	}
1692 
1693 	scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1694 
1695 	/*
1696 	 * Increase dtap until we first see a passing read (in case the
1697 	 * window is smaller than a ptap),
1698 	 * and then a failing read to mark the edge of the window again
1699 	 */
1700 
1701 	/* Find a passing read */
1702 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
1703 		   __func__, __LINE__);
1704 	found_passing_read = 0;
1705 	found_failing_read = 0;
1706 	initial_failing_dtap = d;
1707 	for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
1708 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
1709 			   read d=%u\n", __func__, __LINE__, d);
1710 		scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1711 
1712 		if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1713 							     PASS_ONE_BIT,
1714 							     &bit_chk, 0)) {
1715 			found_passing_read = 1;
1716 			break;
1717 		}
1718 	}
1719 
1720 	if (found_passing_read) {
1721 		/* Find a failing read */
1722 		debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
1723 			   read\n", __func__, __LINE__);
1724 		for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
1725 			debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1726 				   testing read d=%u\n", __func__, __LINE__, d);
1727 			scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1728 
1729 			if (!rw_mgr_mem_calibrate_read_test_all_ranks
1730 				(grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
1731 				found_failing_read = 1;
1732 				break;
1733 			}
1734 		}
1735 	} else {
1736 		debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
1737 			   calculate dtaps", __func__, __LINE__);
1738 		debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
1739 	}
1740 
1741 	/*
1742 	 * The dynamically calculated dtaps_per_ptap is only valid if we
1743 	 * found a passing/failing read. If we didn't, it means d hit the max
1744 	 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1745 	 * statically calculated value.
1746 	 */
1747 	if (found_passing_read && found_failing_read)
1748 		dtaps_per_ptap = d - initial_failing_dtap;
1749 
1750 	writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
1751 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
1752 		   - %u = %u",  __func__, __LINE__, d,
1753 		   initial_failing_dtap, dtaps_per_ptap);
1754 
1755 	/* ******************************************** */
1756 	/* * step 6:  Find the centre of the window   * */
1757 	if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1758 				   &work_mid, &work_end) == 0)
1759 		return 0;
1760 
1761 	debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
1762 		   vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
1763 		   v, p-1, d);
1764 	return 1;
1765 }
1766 
1767 /*
1768  * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
1769  * dq_in_delay values
1770  */
1771 static uint32_t
1772 rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
1773 (uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
1774 {
1775 	uint32_t found;
1776 	uint32_t i;
1777 	uint32_t p;
1778 	uint32_t d;
1779 	uint32_t r;
1780 
1781 	const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
1782 		(RW_MGR_MEM_DQ_PER_READ_DQS-1);
1783 		/* we start at zero, so have one less dq to devide among */
1784 
1785 	debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
1786 	      test_bgn);
1787 
1788 	/* try different dq_in_delays since the dq path is shorter than dqs */
1789 
1790 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1791 	     r += NUM_RANKS_PER_SHADOW_REG) {
1792 		for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1793 			i++, p++, d += delay_step) {
1794 			debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
1795 				   vfifo_find_dqs_", __func__, __LINE__);
1796 			debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
1797 			       write_group, read_group);
1798 			debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
1799 			scc_mgr_set_dq_in_delay(p, d);
1800 			scc_mgr_load_dq(p);
1801 		}
1802 		writel(0, &sdr_scc_mgr->update);
1803 	}
1804 
1805 	found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
1806 
1807 	debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
1808 		   en_phase_sweep_dq", __func__, __LINE__);
1809 	debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
1810 		   chain to zero\n", write_group, read_group, found);
1811 
1812 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1813 	     r += NUM_RANKS_PER_SHADOW_REG) {
1814 		for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1815 			i++, p++) {
1816 			scc_mgr_set_dq_in_delay(p, 0);
1817 			scc_mgr_load_dq(p);
1818 		}
1819 		writel(0, &sdr_scc_mgr->update);
1820 	}
1821 
1822 	return found;
1823 }
1824 
1825 /* per-bit deskew DQ and center */
1826 static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
1827 	uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
1828 	uint32_t use_read_test, uint32_t update_fom)
1829 {
1830 	uint32_t i, p, d, min_index;
1831 	/*
1832 	 * Store these as signed since there are comparisons with
1833 	 * signed numbers.
1834 	 */
1835 	uint32_t bit_chk;
1836 	uint32_t sticky_bit_chk;
1837 	int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1838 	int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1839 	int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
1840 	int32_t mid;
1841 	int32_t orig_mid_min, mid_min;
1842 	int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
1843 		final_dqs_en;
1844 	int32_t dq_margin, dqs_margin;
1845 	uint32_t stop;
1846 	uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
1847 	uint32_t addr;
1848 
1849 	debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
1850 
1851 	addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
1852 	start_dqs = readl(addr + (read_group << 2));
1853 	if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
1854 		start_dqs_en = readl(addr + ((read_group << 2)
1855 				     - IO_DQS_EN_DELAY_OFFSET));
1856 
1857 	/* set the left and right edge of each bit to an illegal value */
1858 	/* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
1859 	sticky_bit_chk = 0;
1860 	for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1861 		left_edge[i]  = IO_IO_IN_DELAY_MAX + 1;
1862 		right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1863 	}
1864 
1865 	/* Search for the left edge of the window for each bit */
1866 	for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
1867 		scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
1868 
1869 		writel(0, &sdr_scc_mgr->update);
1870 
1871 		/*
1872 		 * Stop searching when the read test doesn't pass AND when
1873 		 * we've seen a passing read on every bit.
1874 		 */
1875 		if (use_read_test) {
1876 			stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1877 				read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1878 				&bit_chk, 0, 0);
1879 		} else {
1880 			rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1881 							0, PASS_ONE_BIT,
1882 							&bit_chk, 0);
1883 			bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1884 				(read_group - (write_group *
1885 					RW_MGR_MEM_IF_READ_DQS_WIDTH /
1886 					RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1887 			stop = (bit_chk == 0);
1888 		}
1889 		sticky_bit_chk = sticky_bit_chk | bit_chk;
1890 		stop = stop && (sticky_bit_chk == param->read_correct_mask);
1891 		debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
1892 			   && %u", __func__, __LINE__, d,
1893 			   sticky_bit_chk,
1894 			param->read_correct_mask, stop);
1895 
1896 		if (stop == 1) {
1897 			break;
1898 		} else {
1899 			for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1900 				if (bit_chk & 1) {
1901 					/* Remember a passing test as the
1902 					left_edge */
1903 					left_edge[i] = d;
1904 				} else {
1905 					/* If a left edge has not been seen yet,
1906 					then a future passing test will mark
1907 					this edge as the right edge */
1908 					if (left_edge[i] ==
1909 						IO_IO_IN_DELAY_MAX + 1) {
1910 						right_edge[i] = -(d + 1);
1911 					}
1912 				}
1913 				bit_chk = bit_chk >> 1;
1914 			}
1915 		}
1916 	}
1917 
1918 	/* Reset DQ delay chains to 0 */
1919 	scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, 0);
1920 	sticky_bit_chk = 0;
1921 	for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
1922 		debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
1923 			   %d right_edge[%u]: %d\n", __func__, __LINE__,
1924 			   i, left_edge[i], i, right_edge[i]);
1925 
1926 		/*
1927 		 * Check for cases where we haven't found the left edge,
1928 		 * which makes our assignment of the the right edge invalid.
1929 		 * Reset it to the illegal value.
1930 		 */
1931 		if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
1932 			right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1933 			right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1934 			debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
1935 				   right_edge[%u]: %d\n", __func__, __LINE__,
1936 				   i, right_edge[i]);
1937 		}
1938 
1939 		/*
1940 		 * Reset sticky bit (except for bits where we have seen
1941 		 * both the left and right edge).
1942 		 */
1943 		sticky_bit_chk = sticky_bit_chk << 1;
1944 		if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
1945 		    (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1946 			sticky_bit_chk = sticky_bit_chk | 1;
1947 		}
1948 
1949 		if (i == 0)
1950 			break;
1951 	}
1952 
1953 	/* Search for the right edge of the window for each bit */
1954 	for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
1955 		scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
1956 		if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
1957 			uint32_t delay = d + start_dqs_en;
1958 			if (delay > IO_DQS_EN_DELAY_MAX)
1959 				delay = IO_DQS_EN_DELAY_MAX;
1960 			scc_mgr_set_dqs_en_delay(read_group, delay);
1961 		}
1962 		scc_mgr_load_dqs(read_group);
1963 
1964 		writel(0, &sdr_scc_mgr->update);
1965 
1966 		/*
1967 		 * Stop searching when the read test doesn't pass AND when
1968 		 * we've seen a passing read on every bit.
1969 		 */
1970 		if (use_read_test) {
1971 			stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1972 				read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1973 				&bit_chk, 0, 0);
1974 		} else {
1975 			rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1976 							0, PASS_ONE_BIT,
1977 							&bit_chk, 0);
1978 			bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1979 				(read_group - (write_group *
1980 					RW_MGR_MEM_IF_READ_DQS_WIDTH /
1981 					RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1982 			stop = (bit_chk == 0);
1983 		}
1984 		sticky_bit_chk = sticky_bit_chk | bit_chk;
1985 		stop = stop && (sticky_bit_chk == param->read_correct_mask);
1986 
1987 		debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
1988 			   %u && %u", __func__, __LINE__, d,
1989 			   sticky_bit_chk, param->read_correct_mask, stop);
1990 
1991 		if (stop == 1) {
1992 			break;
1993 		} else {
1994 			for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1995 				if (bit_chk & 1) {
1996 					/* Remember a passing test as
1997 					the right_edge */
1998 					right_edge[i] = d;
1999 				} else {
2000 					if (d != 0) {
2001 						/* If a right edge has not been
2002 						seen yet, then a future passing
2003 						test will mark this edge as the
2004 						left edge */
2005 						if (right_edge[i] ==
2006 						IO_IO_IN_DELAY_MAX + 1) {
2007 							left_edge[i] = -(d + 1);
2008 						}
2009 					} else {
2010 						/* d = 0 failed, but it passed
2011 						when testing the left edge,
2012 						so it must be marginal,
2013 						set it to -1 */
2014 						if (right_edge[i] ==
2015 							IO_IO_IN_DELAY_MAX + 1 &&
2016 							left_edge[i] !=
2017 							IO_IO_IN_DELAY_MAX
2018 							+ 1) {
2019 							right_edge[i] = -1;
2020 						}
2021 						/* If a right edge has not been
2022 						seen yet, then a future passing
2023 						test will mark this edge as the
2024 						left edge */
2025 						else if (right_edge[i] ==
2026 							IO_IO_IN_DELAY_MAX +
2027 							1) {
2028 							left_edge[i] = -(d + 1);
2029 						}
2030 					}
2031 				}
2032 
2033 				debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
2034 					   d=%u]: ", __func__, __LINE__, d);
2035 				debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
2036 					   (int)(bit_chk & 1), i, left_edge[i]);
2037 				debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2038 					   right_edge[i]);
2039 				bit_chk = bit_chk >> 1;
2040 			}
2041 		}
2042 	}
2043 
2044 	/* Check that all bits have a window */
2045 	for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2046 		debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
2047 			   %d right_edge[%u]: %d", __func__, __LINE__,
2048 			   i, left_edge[i], i, right_edge[i]);
2049 		if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
2050 			== IO_IO_IN_DELAY_MAX + 1)) {
2051 			/*
2052 			 * Restore delay chain settings before letting the loop
2053 			 * in rw_mgr_mem_calibrate_vfifo to retry different
2054 			 * dqs/ck relationships.
2055 			 */
2056 			scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
2057 			if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2058 				scc_mgr_set_dqs_en_delay(read_group,
2059 							 start_dqs_en);
2060 			}
2061 			scc_mgr_load_dqs(read_group);
2062 			writel(0, &sdr_scc_mgr->update);
2063 
2064 			debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
2065 				   find edge [%u]: %d %d", __func__, __LINE__,
2066 				   i, left_edge[i], right_edge[i]);
2067 			if (use_read_test) {
2068 				set_failing_group_stage(read_group *
2069 					RW_MGR_MEM_DQ_PER_READ_DQS + i,
2070 					CAL_STAGE_VFIFO,
2071 					CAL_SUBSTAGE_VFIFO_CENTER);
2072 			} else {
2073 				set_failing_group_stage(read_group *
2074 					RW_MGR_MEM_DQ_PER_READ_DQS + i,
2075 					CAL_STAGE_VFIFO_AFTER_WRITES,
2076 					CAL_SUBSTAGE_VFIFO_CENTER);
2077 			}
2078 			return 0;
2079 		}
2080 	}
2081 
2082 	/* Find middle of window for each DQ bit */
2083 	mid_min = left_edge[0] - right_edge[0];
2084 	min_index = 0;
2085 	for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2086 		mid = left_edge[i] - right_edge[i];
2087 		if (mid < mid_min) {
2088 			mid_min = mid;
2089 			min_index = i;
2090 		}
2091 	}
2092 
2093 	/*
2094 	 * -mid_min/2 represents the amount that we need to move DQS.
2095 	 * If mid_min is odd and positive we'll need to add one to
2096 	 * make sure the rounding in further calculations is correct
2097 	 * (always bias to the right), so just add 1 for all positive values.
2098 	 */
2099 	if (mid_min > 0)
2100 		mid_min++;
2101 
2102 	mid_min = mid_min / 2;
2103 
2104 	debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
2105 		   __func__, __LINE__, mid_min, min_index);
2106 
2107 	/* Determine the amount we can change DQS (which is -mid_min) */
2108 	orig_mid_min = mid_min;
2109 	new_dqs = start_dqs - mid_min;
2110 	if (new_dqs > IO_DQS_IN_DELAY_MAX)
2111 		new_dqs = IO_DQS_IN_DELAY_MAX;
2112 	else if (new_dqs < 0)
2113 		new_dqs = 0;
2114 
2115 	mid_min = start_dqs - new_dqs;
2116 	debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2117 		   mid_min, new_dqs);
2118 
2119 	if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2120 		if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2121 			mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2122 		else if (start_dqs_en - mid_min < 0)
2123 			mid_min += start_dqs_en - mid_min;
2124 	}
2125 	new_dqs = start_dqs - mid_min;
2126 
2127 	debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
2128 		   new_dqs=%d mid_min=%d\n", start_dqs,
2129 		   IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2130 		   new_dqs, mid_min);
2131 
2132 	/* Initialize data for export structures */
2133 	dqs_margin = IO_IO_IN_DELAY_MAX + 1;
2134 	dq_margin  = IO_IO_IN_DELAY_MAX + 1;
2135 
2136 	/* add delay to bring centre of all DQ windows to the same "level" */
2137 	for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
2138 		/* Use values before divide by 2 to reduce round off error */
2139 		shift_dq = (left_edge[i] - right_edge[i] -
2140 			(left_edge[min_index] - right_edge[min_index]))/2  +
2141 			(orig_mid_min - mid_min);
2142 
2143 		debug_cond(DLEVEL == 2, "vfifo_center: before: \
2144 			   shift_dq[%u]=%d\n", i, shift_dq);
2145 
2146 		addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
2147 		temp_dq_in_delay1 = readl(addr + (p << 2));
2148 		temp_dq_in_delay2 = readl(addr + (i << 2));
2149 
2150 		if (shift_dq + (int32_t)temp_dq_in_delay1 >
2151 			(int32_t)IO_IO_IN_DELAY_MAX) {
2152 			shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
2153 		} else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
2154 			shift_dq = -(int32_t)temp_dq_in_delay1;
2155 		}
2156 		debug_cond(DLEVEL == 2, "vfifo_center: after: \
2157 			   shift_dq[%u]=%d\n", i, shift_dq);
2158 		final_dq[i] = temp_dq_in_delay1 + shift_dq;
2159 		scc_mgr_set_dq_in_delay(p, final_dq[i]);
2160 		scc_mgr_load_dq(p);
2161 
2162 		debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
2163 			   left_edge[i] - shift_dq + (-mid_min),
2164 			   right_edge[i] + shift_dq - (-mid_min));
2165 		/* To determine values for export structures */
2166 		if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2167 			dq_margin = left_edge[i] - shift_dq + (-mid_min);
2168 
2169 		if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2170 			dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2171 	}
2172 
2173 	final_dqs = new_dqs;
2174 	if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
2175 		final_dqs_en = start_dqs_en - mid_min;
2176 
2177 	/* Move DQS-en */
2178 	if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2179 		scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
2180 		scc_mgr_load_dqs(read_group);
2181 	}
2182 
2183 	/* Move DQS */
2184 	scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
2185 	scc_mgr_load_dqs(read_group);
2186 	debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
2187 		   dqs_margin=%d", __func__, __LINE__,
2188 		   dq_margin, dqs_margin);
2189 
2190 	/*
2191 	 * Do not remove this line as it makes sure all of our decisions
2192 	 * have been applied. Apply the update bit.
2193 	 */
2194 	writel(0, &sdr_scc_mgr->update);
2195 
2196 	return (dq_margin >= 0) && (dqs_margin >= 0);
2197 }
2198 
2199 /*
2200  * calibrate the read valid prediction FIFO.
2201  *
2202  *  - read valid prediction will consist of finding a good DQS enable phase,
2203  * DQS enable delay, DQS input phase, and DQS input delay.
2204  *  - we also do a per-bit deskew on the DQ lines.
2205  */
2206 static uint32_t rw_mgr_mem_calibrate_vfifo(uint32_t read_group,
2207 					   uint32_t test_bgn)
2208 {
2209 	uint32_t p, d, rank_bgn, sr;
2210 	uint32_t dtaps_per_ptap;
2211 	uint32_t tmp_delay;
2212 	uint32_t bit_chk;
2213 	uint32_t grp_calibrated;
2214 	uint32_t write_group, write_test_bgn;
2215 	uint32_t failed_substage;
2216 
2217 	debug("%s:%d: %u %u\n", __func__, __LINE__, read_group, test_bgn);
2218 
2219 	/* update info for sims */
2220 	reg_file_set_stage(CAL_STAGE_VFIFO);
2221 
2222 	write_group = read_group;
2223 	write_test_bgn = test_bgn;
2224 
2225 	/* USER Determine number of delay taps for each phase tap */
2226 	dtaps_per_ptap = 0;
2227 	tmp_delay = 0;
2228 	while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
2229 		dtaps_per_ptap++;
2230 		tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
2231 	}
2232 	dtaps_per_ptap--;
2233 	tmp_delay = 0;
2234 
2235 	/* update info for sims */
2236 	reg_file_set_group(read_group);
2237 
2238 	grp_calibrated = 0;
2239 
2240 	reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
2241 	failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2242 
2243 	for (d = 0; d <= dtaps_per_ptap && grp_calibrated == 0; d += 2) {
2244 		/*
2245 		 * In RLDRAMX we may be messing the delay of pins in
2246 		 * the same write group but outside of the current read
2247 		 * the group, but that's ok because we haven't
2248 		 * calibrated output side yet.
2249 		 */
2250 		if (d > 0) {
2251 			scc_mgr_apply_group_all_out_delay_add_all_ranks
2252 			(write_group, write_test_bgn, d);
2253 		}
2254 
2255 		for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX && grp_calibrated == 0;
2256 			p++) {
2257 			/* set a particular dqdqs phase */
2258 			scc_mgr_set_dqdqs_output_phase_all_ranks(read_group, p);
2259 
2260 			debug_cond(DLEVEL == 1, "%s:%d calibrate_vfifo: g=%u \
2261 				   p=%u d=%u\n", __func__, __LINE__,
2262 				   read_group, p, d);
2263 
2264 			/*
2265 			 * Load up the patterns used by read calibration
2266 			 * using current DQDQS phase.
2267 			 */
2268 			rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2269 			if (!(gbl->phy_debug_mode_flags &
2270 				PHY_DEBUG_DISABLE_GUARANTEED_READ)) {
2271 				if (!rw_mgr_mem_calibrate_read_test_patterns_all_ranks
2272 				    (read_group, 1, &bit_chk)) {
2273 					debug_cond(DLEVEL == 1, "%s:%d Guaranteed read test failed:",
2274 						   __func__, __LINE__);
2275 					debug_cond(DLEVEL == 1, " g=%u p=%u d=%u\n",
2276 						   read_group, p, d);
2277 					break;
2278 				}
2279 			}
2280 
2281 /* case:56390 */
2282 			grp_calibrated = 1;
2283 		if (rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
2284 		    (write_group, read_group, test_bgn)) {
2285 				/*
2286 				 * USER Read per-bit deskew can be done on a
2287 				 * per shadow register basis.
2288 				 */
2289 				for (rank_bgn = 0, sr = 0;
2290 					rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2291 					rank_bgn += NUM_RANKS_PER_SHADOW_REG,
2292 					++sr) {
2293 					/*
2294 					 * Determine if this set of ranks
2295 					 * should be skipped entirely.
2296 					 */
2297 					if (!param->skip_shadow_regs[sr]) {
2298 						/*
2299 						 * If doing read after write
2300 						 * calibration, do not update
2301 						 * FOM, now - do it then.
2302 						 */
2303 					if (!rw_mgr_mem_calibrate_vfifo_center
2304 						(rank_bgn, write_group,
2305 						read_group, test_bgn, 1, 0)) {
2306 							grp_calibrated = 0;
2307 							failed_substage =
2308 						CAL_SUBSTAGE_VFIFO_CENTER;
2309 						}
2310 					}
2311 				}
2312 			} else {
2313 				grp_calibrated = 0;
2314 				failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2315 			}
2316 		}
2317 	}
2318 
2319 	if (grp_calibrated == 0) {
2320 		set_failing_group_stage(write_group, CAL_STAGE_VFIFO,
2321 					failed_substage);
2322 		return 0;
2323 	}
2324 
2325 	/*
2326 	 * Reset the delay chains back to zero if they have moved > 1
2327 	 * (check for > 1 because loop will increase d even when pass in
2328 	 * first case).
2329 	 */
2330 	if (d > 2)
2331 		scc_mgr_zero_group(write_group, write_test_bgn, 1);
2332 
2333 	return 1;
2334 }
2335 
2336 /* VFIFO Calibration -- Read Deskew Calibration after write deskew */
2337 static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
2338 					       uint32_t test_bgn)
2339 {
2340 	uint32_t rank_bgn, sr;
2341 	uint32_t grp_calibrated;
2342 	uint32_t write_group;
2343 
2344 	debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
2345 
2346 	/* update info for sims */
2347 
2348 	reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2349 	reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2350 
2351 	write_group = read_group;
2352 
2353 	/* update info for sims */
2354 	reg_file_set_group(read_group);
2355 
2356 	grp_calibrated = 1;
2357 	/* Read per-bit deskew can be done on a per shadow register basis */
2358 	for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2359 		rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
2360 		/* Determine if this set of ranks should be skipped entirely */
2361 		if (!param->skip_shadow_regs[sr]) {
2362 		/* This is the last calibration round, update FOM here */
2363 			if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
2364 								write_group,
2365 								read_group,
2366 								test_bgn, 0,
2367 								1)) {
2368 				grp_calibrated = 0;
2369 			}
2370 		}
2371 	}
2372 
2373 
2374 	if (grp_calibrated == 0) {
2375 		set_failing_group_stage(write_group,
2376 					CAL_STAGE_VFIFO_AFTER_WRITES,
2377 					CAL_SUBSTAGE_VFIFO_CENTER);
2378 		return 0;
2379 	}
2380 
2381 	return 1;
2382 }
2383 
2384 /* Calibrate LFIFO to find smallest read latency */
2385 static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2386 {
2387 	uint32_t found_one;
2388 	uint32_t bit_chk;
2389 
2390 	debug("%s:%d\n", __func__, __LINE__);
2391 
2392 	/* update info for sims */
2393 	reg_file_set_stage(CAL_STAGE_LFIFO);
2394 	reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2395 
2396 	/* Load up the patterns used by read calibration for all ranks */
2397 	rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2398 	found_one = 0;
2399 
2400 	do {
2401 		writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
2402 		debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2403 			   __func__, __LINE__, gbl->curr_read_lat);
2404 
2405 		if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
2406 							      NUM_READ_TESTS,
2407 							      PASS_ALL_BITS,
2408 							      &bit_chk, 1)) {
2409 			break;
2410 		}
2411 
2412 		found_one = 1;
2413 		/* reduce read latency and see if things are working */
2414 		/* correctly */
2415 		gbl->curr_read_lat--;
2416 	} while (gbl->curr_read_lat > 0);
2417 
2418 	/* reset the fifos to get pointers to known state */
2419 
2420 	writel(0, &phy_mgr_cmd->fifo_reset);
2421 
2422 	if (found_one) {
2423 		/* add a fudge factor to the read latency that was determined */
2424 		gbl->curr_read_lat += 2;
2425 		writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
2426 		debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
2427 			   read_lat=%u\n", __func__, __LINE__,
2428 			   gbl->curr_read_lat);
2429 		return 1;
2430 	} else {
2431 		set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2432 					CAL_SUBSTAGE_READ_LATENCY);
2433 
2434 		debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
2435 			   read_lat=%u\n", __func__, __LINE__,
2436 			   gbl->curr_read_lat);
2437 		return 0;
2438 	}
2439 }
2440 
2441 /*
2442  * issue write test command.
2443  * two variants are provided. one that just tests a write pattern and
2444  * another that tests datamask functionality.
2445  */
2446 static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
2447 						  uint32_t test_dm)
2448 {
2449 	uint32_t mcc_instruction;
2450 	uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
2451 		ENABLE_SUPER_QUICK_CALIBRATION);
2452 	uint32_t rw_wl_nop_cycles;
2453 	uint32_t addr;
2454 
2455 	/*
2456 	 * Set counter and jump addresses for the right
2457 	 * number of NOP cycles.
2458 	 * The number of supported NOP cycles can range from -1 to infinity
2459 	 * Three different cases are handled:
2460 	 *
2461 	 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
2462 	 *    mechanism will be used to insert the right number of NOPs
2463 	 *
2464 	 * 2. For a number of NOP cycles equals to 0, the micro-instruction
2465 	 *    issuing the write command will jump straight to the
2466 	 *    micro-instruction that turns on DQS (for DDRx), or outputs write
2467 	 *    data (for RLD), skipping
2468 	 *    the NOP micro-instruction all together
2469 	 *
2470 	 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
2471 	 *    turned on in the same micro-instruction that issues the write
2472 	 *    command. Then we need
2473 	 *    to directly jump to the micro-instruction that sends out the data
2474 	 *
2475 	 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
2476 	 *       (2 and 3). One jump-counter (0) is used to perform multiple
2477 	 *       write-read operations.
2478 	 *       one counter left to issue this command in "multiple-group" mode
2479 	 */
2480 
2481 	rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
2482 
2483 	if (rw_wl_nop_cycles == -1) {
2484 		/*
2485 		 * CNTR 2 - We want to execute the special write operation that
2486 		 * turns on DQS right away and then skip directly to the
2487 		 * instruction that sends out the data. We set the counter to a
2488 		 * large number so that the jump is always taken.
2489 		 */
2490 		writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
2491 
2492 		/* CNTR 3 - Not used */
2493 		if (test_dm) {
2494 			mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
2495 			writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
2496 			       &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2497 			writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2498 			       &sdr_rw_load_jump_mgr_regs->load_jump_add3);
2499 		} else {
2500 			mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
2501 			writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
2502 				&sdr_rw_load_jump_mgr_regs->load_jump_add2);
2503 			writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2504 				&sdr_rw_load_jump_mgr_regs->load_jump_add3);
2505 		}
2506 	} else if (rw_wl_nop_cycles == 0) {
2507 		/*
2508 		 * CNTR 2 - We want to skip the NOP operation and go straight
2509 		 * to the DQS enable instruction. We set the counter to a large
2510 		 * number so that the jump is always taken.
2511 		 */
2512 		writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
2513 
2514 		/* CNTR 3 - Not used */
2515 		if (test_dm) {
2516 			mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
2517 			writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
2518 			       &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2519 		} else {
2520 			mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
2521 			writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
2522 				&sdr_rw_load_jump_mgr_regs->load_jump_add2);
2523 		}
2524 	} else {
2525 		/*
2526 		 * CNTR 2 - In this case we want to execute the next instruction
2527 		 * and NOT take the jump. So we set the counter to 0. The jump
2528 		 * address doesn't count.
2529 		 */
2530 		writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
2531 		writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2532 
2533 		/*
2534 		 * CNTR 3 - Set the nop counter to the number of cycles we
2535 		 * need to loop for, minus 1.
2536 		 */
2537 		writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
2538 		if (test_dm) {
2539 			mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
2540 			writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2541 				&sdr_rw_load_jump_mgr_regs->load_jump_add3);
2542 		} else {
2543 			mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
2544 			writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2545 				&sdr_rw_load_jump_mgr_regs->load_jump_add3);
2546 		}
2547 	}
2548 
2549 	writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
2550 		  RW_MGR_RESET_READ_DATAPATH_OFFSET);
2551 
2552 	if (quick_write_mode)
2553 		writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
2554 	else
2555 		writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
2556 
2557 	writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
2558 
2559 	/*
2560 	 * CNTR 1 - This is used to ensure enough time elapses
2561 	 * for read data to come back.
2562 	 */
2563 	writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
2564 
2565 	if (test_dm) {
2566 		writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
2567 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
2568 	} else {
2569 		writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
2570 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
2571 	}
2572 
2573 	addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
2574 	writel(mcc_instruction, addr + (group << 2));
2575 }
2576 
2577 /* Test writes, can check for a single bit pass or multiple bit pass */
2578 static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
2579 	uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
2580 	uint32_t *bit_chk, uint32_t all_ranks)
2581 {
2582 	uint32_t r;
2583 	uint32_t correct_mask_vg;
2584 	uint32_t tmp_bit_chk;
2585 	uint32_t vg;
2586 	uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
2587 		(rank_bgn + NUM_RANKS_PER_SHADOW_REG);
2588 	uint32_t addr_rw_mgr;
2589 	uint32_t base_rw_mgr;
2590 
2591 	*bit_chk = param->write_correct_mask;
2592 	correct_mask_vg = param->write_correct_mask_vg;
2593 
2594 	for (r = rank_bgn; r < rank_end; r++) {
2595 		if (param->skip_ranks[r]) {
2596 			/* request to skip the rank */
2597 			continue;
2598 		}
2599 
2600 		/* set rank */
2601 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
2602 
2603 		tmp_bit_chk = 0;
2604 		addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
2605 		for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
2606 			/* reset the fifos to get pointers to known state */
2607 			writel(0, &phy_mgr_cmd->fifo_reset);
2608 
2609 			tmp_bit_chk = tmp_bit_chk <<
2610 				(RW_MGR_MEM_DQ_PER_WRITE_DQS /
2611 				RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
2612 			rw_mgr_mem_calibrate_write_test_issue(write_group *
2613 				RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
2614 				use_dm);
2615 
2616 			base_rw_mgr = readl(addr_rw_mgr);
2617 			tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
2618 			if (vg == 0)
2619 				break;
2620 		}
2621 		*bit_chk &= tmp_bit_chk;
2622 	}
2623 
2624 	if (all_correct) {
2625 		set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2626 		debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
2627 			   %u => %lu", write_group, use_dm,
2628 			   *bit_chk, param->write_correct_mask,
2629 			   (long unsigned int)(*bit_chk ==
2630 			   param->write_correct_mask));
2631 		return *bit_chk == param->write_correct_mask;
2632 	} else {
2633 		set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2634 		debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
2635 		       write_group, use_dm, *bit_chk);
2636 		debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
2637 			(long unsigned int)(*bit_chk != 0));
2638 		return *bit_chk != 0x00;
2639 	}
2640 }
2641 
2642 /*
2643  * center all windows. do per-bit-deskew to possibly increase size of
2644  * certain windows.
2645  */
2646 static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
2647 	uint32_t write_group, uint32_t test_bgn)
2648 {
2649 	uint32_t i, p, min_index;
2650 	int32_t d;
2651 	/*
2652 	 * Store these as signed since there are comparisons with
2653 	 * signed numbers.
2654 	 */
2655 	uint32_t bit_chk;
2656 	uint32_t sticky_bit_chk;
2657 	int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2658 	int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2659 	int32_t mid;
2660 	int32_t mid_min, orig_mid_min;
2661 	int32_t new_dqs, start_dqs, shift_dq;
2662 	int32_t dq_margin, dqs_margin, dm_margin;
2663 	uint32_t stop;
2664 	uint32_t temp_dq_out1_delay;
2665 	uint32_t addr;
2666 
2667 	debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2668 
2669 	dm_margin = 0;
2670 
2671 	addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
2672 	start_dqs = readl(addr +
2673 			  (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2674 
2675 	/* per-bit deskew */
2676 
2677 	/*
2678 	 * set the left and right edge of each bit to an illegal value
2679 	 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
2680 	 */
2681 	sticky_bit_chk = 0;
2682 	for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2683 		left_edge[i]  = IO_IO_OUT1_DELAY_MAX + 1;
2684 		right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2685 	}
2686 
2687 	/* Search for the left edge of the window for each bit */
2688 	for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
2689 		scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, d);
2690 
2691 		writel(0, &sdr_scc_mgr->update);
2692 
2693 		/*
2694 		 * Stop searching when the read test doesn't pass AND when
2695 		 * we've seen a passing read on every bit.
2696 		 */
2697 		stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2698 			0, PASS_ONE_BIT, &bit_chk, 0);
2699 		sticky_bit_chk = sticky_bit_chk | bit_chk;
2700 		stop = stop && (sticky_bit_chk == param->write_correct_mask);
2701 		debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
2702 			   == %u && %u [bit_chk= %u ]\n",
2703 			d, sticky_bit_chk, param->write_correct_mask,
2704 			stop, bit_chk);
2705 
2706 		if (stop == 1) {
2707 			break;
2708 		} else {
2709 			for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2710 				if (bit_chk & 1) {
2711 					/*
2712 					 * Remember a passing test as the
2713 					 * left_edge.
2714 					 */
2715 					left_edge[i] = d;
2716 				} else {
2717 					/*
2718 					 * If a left edge has not been seen
2719 					 * yet, then a future passing test will
2720 					 * mark this edge as the right edge.
2721 					 */
2722 					if (left_edge[i] ==
2723 						IO_IO_OUT1_DELAY_MAX + 1) {
2724 						right_edge[i] = -(d + 1);
2725 					}
2726 				}
2727 				debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
2728 				debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2729 					   (int)(bit_chk & 1), i, left_edge[i]);
2730 				debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2731 				       right_edge[i]);
2732 				bit_chk = bit_chk >> 1;
2733 			}
2734 		}
2735 	}
2736 
2737 	/* Reset DQ delay chains to 0 */
2738 	scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, 0);
2739 	sticky_bit_chk = 0;
2740 	for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
2741 		debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2742 			   %d right_edge[%u]: %d\n", __func__, __LINE__,
2743 			   i, left_edge[i], i, right_edge[i]);
2744 
2745 		/*
2746 		 * Check for cases where we haven't found the left edge,
2747 		 * which makes our assignment of the the right edge invalid.
2748 		 * Reset it to the illegal value.
2749 		 */
2750 		if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
2751 		    (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
2752 			right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2753 			debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
2754 				   right_edge[%u]: %d\n", __func__, __LINE__,
2755 				   i, right_edge[i]);
2756 		}
2757 
2758 		/*
2759 		 * Reset sticky bit (except for bits where we have
2760 		 * seen the left edge).
2761 		 */
2762 		sticky_bit_chk = sticky_bit_chk << 1;
2763 		if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
2764 			sticky_bit_chk = sticky_bit_chk | 1;
2765 
2766 		if (i == 0)
2767 			break;
2768 	}
2769 
2770 	/* Search for the right edge of the window for each bit */
2771 	for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
2772 		scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2773 							d + start_dqs);
2774 
2775 		writel(0, &sdr_scc_mgr->update);
2776 
2777 		/*
2778 		 * Stop searching when the read test doesn't pass AND when
2779 		 * we've seen a passing read on every bit.
2780 		 */
2781 		stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2782 			0, PASS_ONE_BIT, &bit_chk, 0);
2783 
2784 		sticky_bit_chk = sticky_bit_chk | bit_chk;
2785 		stop = stop && (sticky_bit_chk == param->write_correct_mask);
2786 
2787 		debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
2788 			   %u && %u\n", d, sticky_bit_chk,
2789 			   param->write_correct_mask, stop);
2790 
2791 		if (stop == 1) {
2792 			if (d == 0) {
2793 				for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
2794 					i++) {
2795 					/* d = 0 failed, but it passed when
2796 					testing the left edge, so it must be
2797 					marginal, set it to -1 */
2798 					if (right_edge[i] ==
2799 						IO_IO_OUT1_DELAY_MAX + 1 &&
2800 						left_edge[i] !=
2801 						IO_IO_OUT1_DELAY_MAX + 1) {
2802 						right_edge[i] = -1;
2803 					}
2804 				}
2805 			}
2806 			break;
2807 		} else {
2808 			for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2809 				if (bit_chk & 1) {
2810 					/*
2811 					 * Remember a passing test as
2812 					 * the right_edge.
2813 					 */
2814 					right_edge[i] = d;
2815 				} else {
2816 					if (d != 0) {
2817 						/*
2818 						 * If a right edge has not
2819 						 * been seen yet, then a future
2820 						 * passing test will mark this
2821 						 * edge as the left edge.
2822 						 */
2823 						if (right_edge[i] ==
2824 						    IO_IO_OUT1_DELAY_MAX + 1)
2825 							left_edge[i] = -(d + 1);
2826 					} else {
2827 						/*
2828 						 * d = 0 failed, but it passed
2829 						 * when testing the left edge,
2830 						 * so it must be marginal, set
2831 						 * it to -1.
2832 						 */
2833 						if (right_edge[i] ==
2834 						    IO_IO_OUT1_DELAY_MAX + 1 &&
2835 						    left_edge[i] !=
2836 						    IO_IO_OUT1_DELAY_MAX + 1)
2837 							right_edge[i] = -1;
2838 						/*
2839 						 * If a right edge has not been
2840 						 * seen yet, then a future
2841 						 * passing test will mark this
2842 						 * edge as the left edge.
2843 						 */
2844 						else if (right_edge[i] ==
2845 							IO_IO_OUT1_DELAY_MAX +
2846 							1)
2847 							left_edge[i] = -(d + 1);
2848 					}
2849 				}
2850 				debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
2851 				debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2852 					   (int)(bit_chk & 1), i, left_edge[i]);
2853 				debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2854 					   right_edge[i]);
2855 				bit_chk = bit_chk >> 1;
2856 			}
2857 		}
2858 	}
2859 
2860 	/* Check that all bits have a window */
2861 	for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2862 		debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2863 			   %d right_edge[%u]: %d", __func__, __LINE__,
2864 			   i, left_edge[i], i, right_edge[i]);
2865 		if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
2866 		    (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
2867 			set_failing_group_stage(test_bgn + i,
2868 						CAL_STAGE_WRITES,
2869 						CAL_SUBSTAGE_WRITES_CENTER);
2870 			return 0;
2871 		}
2872 	}
2873 
2874 	/* Find middle of window for each DQ bit */
2875 	mid_min = left_edge[0] - right_edge[0];
2876 	min_index = 0;
2877 	for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2878 		mid = left_edge[i] - right_edge[i];
2879 		if (mid < mid_min) {
2880 			mid_min = mid;
2881 			min_index = i;
2882 		}
2883 	}
2884 
2885 	/*
2886 	 * -mid_min/2 represents the amount that we need to move DQS.
2887 	 * If mid_min is odd and positive we'll need to add one to
2888 	 * make sure the rounding in further calculations is correct
2889 	 * (always bias to the right), so just add 1 for all positive values.
2890 	 */
2891 	if (mid_min > 0)
2892 		mid_min++;
2893 	mid_min = mid_min / 2;
2894 	debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
2895 		   __LINE__, mid_min);
2896 
2897 	/* Determine the amount we can change DQS (which is -mid_min) */
2898 	orig_mid_min = mid_min;
2899 	new_dqs = start_dqs;
2900 	mid_min = 0;
2901 	debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
2902 		   mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
2903 	/* Initialize data for export structures */
2904 	dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
2905 	dq_margin  = IO_IO_OUT1_DELAY_MAX + 1;
2906 
2907 	/* add delay to bring centre of all DQ windows to the same "level" */
2908 	for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
2909 		/* Use values before divide by 2 to reduce round off error */
2910 		shift_dq = (left_edge[i] - right_edge[i] -
2911 			(left_edge[min_index] - right_edge[min_index]))/2  +
2912 		(orig_mid_min - mid_min);
2913 
2914 		debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
2915 			   [%u]=%d\n", __func__, __LINE__, i, shift_dq);
2916 
2917 		addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
2918 		temp_dq_out1_delay = readl(addr + (i << 2));
2919 		if (shift_dq + (int32_t)temp_dq_out1_delay >
2920 			(int32_t)IO_IO_OUT1_DELAY_MAX) {
2921 			shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
2922 		} else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
2923 			shift_dq = -(int32_t)temp_dq_out1_delay;
2924 		}
2925 		debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
2926 			   i, shift_dq);
2927 		scc_mgr_set_dq_out1_delay(i, temp_dq_out1_delay + shift_dq);
2928 		scc_mgr_load_dq(i);
2929 
2930 		debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
2931 			   left_edge[i] - shift_dq + (-mid_min),
2932 			   right_edge[i] + shift_dq - (-mid_min));
2933 		/* To determine values for export structures */
2934 		if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2935 			dq_margin = left_edge[i] - shift_dq + (-mid_min);
2936 
2937 		if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2938 			dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2939 	}
2940 
2941 	/* Move DQS */
2942 	scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
2943 	writel(0, &sdr_scc_mgr->update);
2944 
2945 	/* Centre DM */
2946 	debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
2947 
2948 	/*
2949 	 * set the left and right edge of each bit to an illegal value,
2950 	 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
2951 	 */
2952 	left_edge[0]  = IO_IO_OUT1_DELAY_MAX + 1;
2953 	right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2954 	int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2955 	int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2956 	int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2957 	int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
2958 	int32_t win_best = 0;
2959 
2960 	/* Search for the/part of the window with DM shift */
2961 	for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
2962 		scc_mgr_apply_group_dm_out1_delay(write_group, d);
2963 		writel(0, &sdr_scc_mgr->update);
2964 
2965 		if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2966 						    PASS_ALL_BITS, &bit_chk,
2967 						    0)) {
2968 			/* USE Set current end of the window */
2969 			end_curr = -d;
2970 			/*
2971 			 * If a starting edge of our window has not been seen
2972 			 * this is our current start of the DM window.
2973 			 */
2974 			if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2975 				bgn_curr = -d;
2976 
2977 			/*
2978 			 * If current window is bigger than best seen.
2979 			 * Set best seen to be current window.
2980 			 */
2981 			if ((end_curr-bgn_curr+1) > win_best) {
2982 				win_best = end_curr-bgn_curr+1;
2983 				bgn_best = bgn_curr;
2984 				end_best = end_curr;
2985 			}
2986 		} else {
2987 			/* We just saw a failing test. Reset temp edge */
2988 			bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2989 			end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2990 			}
2991 		}
2992 
2993 
2994 	/* Reset DM delay chains to 0 */
2995 	scc_mgr_apply_group_dm_out1_delay(write_group, 0);
2996 
2997 	/*
2998 	 * Check to see if the current window nudges up aganist 0 delay.
2999 	 * If so we need to continue the search by shifting DQS otherwise DQS
3000 	 * search begins as a new search. */
3001 	if (end_curr != 0) {
3002 		bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3003 		end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3004 	}
3005 
3006 	/* Search for the/part of the window with DQS shifts */
3007 	for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
3008 		/*
3009 		 * Note: This only shifts DQS, so are we limiting ourselve to
3010 		 * width of DQ unnecessarily.
3011 		 */
3012 		scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
3013 							d + new_dqs);
3014 
3015 		writel(0, &sdr_scc_mgr->update);
3016 		if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
3017 						    PASS_ALL_BITS, &bit_chk,
3018 						    0)) {
3019 			/* USE Set current end of the window */
3020 			end_curr = d;
3021 			/*
3022 			 * If a beginning edge of our window has not been seen
3023 			 * this is our current begin of the DM window.
3024 			 */
3025 			if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
3026 				bgn_curr = d;
3027 
3028 			/*
3029 			 * If current window is bigger than best seen. Set best
3030 			 * seen to be current window.
3031 			 */
3032 			if ((end_curr-bgn_curr+1) > win_best) {
3033 				win_best = end_curr-bgn_curr+1;
3034 				bgn_best = bgn_curr;
3035 				end_best = end_curr;
3036 			}
3037 		} else {
3038 			/* We just saw a failing test. Reset temp edge */
3039 			bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3040 			end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3041 
3042 			/* Early exit optimization: if ther remaining delay
3043 			chain space is less than already seen largest window
3044 			we can exit */
3045 			if ((win_best-1) >
3046 				(IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
3047 					break;
3048 				}
3049 			}
3050 		}
3051 
3052 	/* assign left and right edge for cal and reporting; */
3053 	left_edge[0] = -1*bgn_best;
3054 	right_edge[0] = end_best;
3055 
3056 	debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
3057 		   __LINE__, left_edge[0], right_edge[0]);
3058 
3059 	/* Move DQS (back to orig) */
3060 	scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3061 
3062 	/* Move DM */
3063 
3064 	/* Find middle of window for the DM bit */
3065 	mid = (left_edge[0] - right_edge[0]) / 2;
3066 
3067 	/* only move right, since we are not moving DQS/DQ */
3068 	if (mid < 0)
3069 		mid = 0;
3070 
3071 	/* dm_marign should fail if we never find a window */
3072 	if (win_best == 0)
3073 		dm_margin = -1;
3074 	else
3075 		dm_margin = left_edge[0] - mid;
3076 
3077 	scc_mgr_apply_group_dm_out1_delay(write_group, mid);
3078 	writel(0, &sdr_scc_mgr->update);
3079 
3080 	debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
3081 		   dm_margin=%d\n", __func__, __LINE__, left_edge[0],
3082 		   right_edge[0], mid, dm_margin);
3083 	/* Export values */
3084 	gbl->fom_out += dq_margin + dqs_margin;
3085 
3086 	debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
3087 		   dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
3088 		   dq_margin, dqs_margin, dm_margin);
3089 
3090 	/*
3091 	 * Do not remove this line as it makes sure all of our
3092 	 * decisions have been applied.
3093 	 */
3094 	writel(0, &sdr_scc_mgr->update);
3095 	return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
3096 }
3097 
3098 /* calibrate the write operations */
3099 static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
3100 	uint32_t test_bgn)
3101 {
3102 	/* update info for sims */
3103 	debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
3104 
3105 	reg_file_set_stage(CAL_STAGE_WRITES);
3106 	reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3107 
3108 	reg_file_set_group(g);
3109 
3110 	if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
3111 		set_failing_group_stage(g, CAL_STAGE_WRITES,
3112 					CAL_SUBSTAGE_WRITES_CENTER);
3113 		return 0;
3114 	}
3115 
3116 	return 1;
3117 }
3118 
3119 /* precharge all banks and activate row 0 in bank "000..." and bank "111..." */
3120 static void mem_precharge_and_activate(void)
3121 {
3122 	uint32_t r;
3123 
3124 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
3125 		if (param->skip_ranks[r]) {
3126 			/* request to skip the rank */
3127 			continue;
3128 		}
3129 
3130 		/* set rank */
3131 		set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3132 
3133 		/* precharge all banks ... */
3134 		writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3135 					     RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3136 
3137 		writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3138 		writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3139 			&sdr_rw_load_jump_mgr_regs->load_jump_add0);
3140 
3141 		writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3142 		writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3143 			&sdr_rw_load_jump_mgr_regs->load_jump_add1);
3144 
3145 		/* activate rows */
3146 		writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3147 						RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3148 	}
3149 }
3150 
3151 /* Configure various memory related parameters. */
3152 static void mem_config(void)
3153 {
3154 	uint32_t rlat, wlat;
3155 	uint32_t rw_wl_nop_cycles;
3156 	uint32_t max_latency;
3157 
3158 	debug("%s:%d\n", __func__, __LINE__);
3159 	/* read in write and read latency */
3160 	wlat = readl(&data_mgr->t_wl_add);
3161 	wlat += readl(&data_mgr->mem_t_add);
3162 
3163 	/* WL for hard phy does not include additive latency */
3164 
3165 	/*
3166 	 * add addtional write latency to offset the address/command extra
3167 	 * clock cycle. We change the AC mux setting causing AC to be delayed
3168 	 * by one mem clock cycle. Only do this for DDR3
3169 	 */
3170 	wlat = wlat + 1;
3171 
3172 	rlat = readl(&data_mgr->t_rl_add);
3173 
3174 	rw_wl_nop_cycles = wlat - 2;
3175 	gbl->rw_wl_nop_cycles = rw_wl_nop_cycles;
3176 
3177 	/*
3178 	 * For AV/CV, lfifo is hardened and always runs at full rate so
3179 	 * max latency in AFI clocks, used here, is correspondingly smaller.
3180 	 */
3181 	max_latency = (1<<MAX_LATENCY_COUNT_WIDTH)/1 - 1;
3182 	/* configure for a burst length of 8 */
3183 
3184 	/* write latency */
3185 	/* Adjust Write Latency for Hard PHY */
3186 	wlat = wlat + 1;
3187 
3188 	/* set a pretty high read latency initially */
3189 	gbl->curr_read_lat = rlat + 16;
3190 
3191 	if (gbl->curr_read_lat > max_latency)
3192 		gbl->curr_read_lat = max_latency;
3193 
3194 	writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
3195 
3196 	/* advertise write latency */
3197 	gbl->curr_write_lat = wlat;
3198 	writel(wlat - 2, &phy_mgr_cfg->afi_wlat);
3199 
3200 	/* initialize bit slips */
3201 	mem_precharge_and_activate();
3202 }
3203 
3204 /* Set VFIFO and LFIFO to instant-on settings in skip calibration mode */
3205 static void mem_skip_calibrate(void)
3206 {
3207 	uint32_t vfifo_offset;
3208 	uint32_t i, j, r;
3209 
3210 	debug("%s:%d\n", __func__, __LINE__);
3211 	/* Need to update every shadow register set used by the interface */
3212 	for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
3213 		r += NUM_RANKS_PER_SHADOW_REG) {
3214 		/*
3215 		 * Set output phase alignment settings appropriate for
3216 		 * skip calibration.
3217 		 */
3218 		for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3219 			scc_mgr_set_dqs_en_phase(i, 0);
3220 #if IO_DLL_CHAIN_LENGTH == 6
3221 			scc_mgr_set_dqdqs_output_phase(i, 6);
3222 #else
3223 			scc_mgr_set_dqdqs_output_phase(i, 7);
3224 #endif
3225 			/*
3226 			 * Case:33398
3227 			 *
3228 			 * Write data arrives to the I/O two cycles before write
3229 			 * latency is reached (720 deg).
3230 			 *   -> due to bit-slip in a/c bus
3231 			 *   -> to allow board skew where dqs is longer than ck
3232 			 *      -> how often can this happen!?
3233 			 *      -> can claim back some ptaps for high freq
3234 			 *       support if we can relax this, but i digress...
3235 			 *
3236 			 * The write_clk leads mem_ck by 90 deg
3237 			 * The minimum ptap of the OPA is 180 deg
3238 			 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3239 			 * The write_clk is always delayed by 2 ptaps
3240 			 *
3241 			 * Hence, to make DQS aligned to CK, we need to delay
3242 			 * DQS by:
3243 			 *    (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3244 			 *
3245 			 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3246 			 * gives us the number of ptaps, which simplies to:
3247 			 *
3248 			 *    (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3249 			 */
3250 			scc_mgr_set_dqdqs_output_phase(i, (1.25 *
3251 				IO_DLL_CHAIN_LENGTH - 2));
3252 		}
3253 		writel(0xff, &sdr_scc_mgr->dqs_ena);
3254 		writel(0xff, &sdr_scc_mgr->dqs_io_ena);
3255 
3256 		for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
3257 			writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3258 				  SCC_MGR_GROUP_COUNTER_OFFSET);
3259 		}
3260 		writel(0xff, &sdr_scc_mgr->dq_ena);
3261 		writel(0xff, &sdr_scc_mgr->dm_ena);
3262 		writel(0, &sdr_scc_mgr->update);
3263 	}
3264 
3265 	/* Compensate for simulation model behaviour */
3266 	for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3267 		scc_mgr_set_dqs_bus_in_delay(i, 10);
3268 		scc_mgr_load_dqs(i);
3269 	}
3270 	writel(0, &sdr_scc_mgr->update);
3271 
3272 	/*
3273 	 * ArriaV has hard FIFOs that can only be initialized by incrementing
3274 	 * in sequencer.
3275 	 */
3276 	vfifo_offset = CALIB_VFIFO_OFFSET;
3277 	for (j = 0; j < vfifo_offset; j++) {
3278 		writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
3279 	}
3280 	writel(0, &phy_mgr_cmd->fifo_reset);
3281 
3282 	/*
3283 	 * For ACV with hard lfifo, we get the skip-cal setting from
3284 	 * generation-time constant.
3285 	 */
3286 	gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
3287 	writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
3288 }
3289 
3290 /* Memory calibration entry point */
3291 static uint32_t mem_calibrate(void)
3292 {
3293 	uint32_t i;
3294 	uint32_t rank_bgn, sr;
3295 	uint32_t write_group, write_test_bgn;
3296 	uint32_t read_group, read_test_bgn;
3297 	uint32_t run_groups, current_run;
3298 	uint32_t failing_groups = 0;
3299 	uint32_t group_failed = 0;
3300 	uint32_t sr_failed = 0;
3301 
3302 	debug("%s:%d\n", __func__, __LINE__);
3303 	/* Initialize the data settings */
3304 
3305 	gbl->error_substage = CAL_SUBSTAGE_NIL;
3306 	gbl->error_stage = CAL_STAGE_NIL;
3307 	gbl->error_group = 0xff;
3308 	gbl->fom_in = 0;
3309 	gbl->fom_out = 0;
3310 
3311 	mem_config();
3312 
3313 	uint32_t bypass_mode = 0x1;
3314 	for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3315 		writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3316 			  SCC_MGR_GROUP_COUNTER_OFFSET);
3317 		scc_set_bypass_mode(i, bypass_mode);
3318 	}
3319 
3320 	if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3321 		/*
3322 		 * Set VFIFO and LFIFO to instant-on settings in skip
3323 		 * calibration mode.
3324 		 */
3325 		mem_skip_calibrate();
3326 	} else {
3327 		for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3328 			/*
3329 			 * Zero all delay chain/phase settings for all
3330 			 * groups and all shadow register sets.
3331 			 */
3332 			scc_mgr_zero_all();
3333 
3334 			run_groups = ~param->skip_groups;
3335 
3336 			for (write_group = 0, write_test_bgn = 0; write_group
3337 				< RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3338 				write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
3339 				/* Initialized the group failure */
3340 				group_failed = 0;
3341 
3342 				current_run = run_groups & ((1 <<
3343 					RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3344 				run_groups = run_groups >>
3345 					RW_MGR_NUM_DQS_PER_WRITE_GROUP;
3346 
3347 				if (current_run == 0)
3348 					continue;
3349 
3350 				writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3351 						    SCC_MGR_GROUP_COUNTER_OFFSET);
3352 				scc_mgr_zero_group(write_group, write_test_bgn,
3353 						   0);
3354 
3355 				for (read_group = write_group *
3356 					RW_MGR_MEM_IF_READ_DQS_WIDTH /
3357 					RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3358 					read_test_bgn = 0;
3359 					read_group < (write_group + 1) *
3360 					RW_MGR_MEM_IF_READ_DQS_WIDTH /
3361 					RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3362 					group_failed == 0;
3363 					read_group++, read_test_bgn +=
3364 					RW_MGR_MEM_DQ_PER_READ_DQS) {
3365 					/* Calibrate the VFIFO */
3366 					if (!((STATIC_CALIB_STEPS) &
3367 						CALIB_SKIP_VFIFO)) {
3368 						if (!rw_mgr_mem_calibrate_vfifo
3369 							(read_group,
3370 							read_test_bgn)) {
3371 							group_failed = 1;
3372 
3373 							if (!(gbl->
3374 							phy_debug_mode_flags &
3375 						PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3376 								return 0;
3377 							}
3378 						}
3379 					}
3380 				}
3381 
3382 				/* Calibrate the output side */
3383 				if (group_failed == 0)	{
3384 					for (rank_bgn = 0, sr = 0; rank_bgn
3385 						< RW_MGR_MEM_NUMBER_OF_RANKS;
3386 						rank_bgn +=
3387 						NUM_RANKS_PER_SHADOW_REG,
3388 						++sr) {
3389 						sr_failed = 0;
3390 						if (!((STATIC_CALIB_STEPS) &
3391 						CALIB_SKIP_WRITES)) {
3392 							if ((STATIC_CALIB_STEPS)
3393 						& CALIB_SKIP_DELAY_SWEEPS) {
3394 						/* not needed in quick mode! */
3395 							} else {
3396 						/*
3397 						 * Determine if this set of
3398 						 * ranks should be skipped
3399 						 * entirely.
3400 						 */
3401 					if (!param->skip_shadow_regs[sr]) {
3402 						if (!rw_mgr_mem_calibrate_writes
3403 						(rank_bgn, write_group,
3404 						write_test_bgn)) {
3405 							sr_failed = 1;
3406 							if (!(gbl->
3407 							phy_debug_mode_flags &
3408 						PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3409 								return 0;
3410 									}
3411 									}
3412 								}
3413 							}
3414 						}
3415 						if (sr_failed != 0)
3416 							group_failed = 1;
3417 					}
3418 				}
3419 
3420 				if (group_failed == 0) {
3421 					for (read_group = write_group *
3422 					RW_MGR_MEM_IF_READ_DQS_WIDTH /
3423 					RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3424 					read_test_bgn = 0;
3425 						read_group < (write_group + 1)
3426 						* RW_MGR_MEM_IF_READ_DQS_WIDTH
3427 						/ RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3428 						group_failed == 0;
3429 						read_group++, read_test_bgn +=
3430 						RW_MGR_MEM_DQ_PER_READ_DQS) {
3431 						if (!((STATIC_CALIB_STEPS) &
3432 							CALIB_SKIP_WRITES)) {
3433 					if (!rw_mgr_mem_calibrate_vfifo_end
3434 						(read_group, read_test_bgn)) {
3435 							group_failed = 1;
3436 
3437 						if (!(gbl->phy_debug_mode_flags
3438 						& PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3439 								return 0;
3440 								}
3441 							}
3442 						}
3443 					}
3444 				}
3445 
3446 				if (group_failed != 0)
3447 					failing_groups++;
3448 			}
3449 
3450 			/*
3451 			 * USER If there are any failing groups then report
3452 			 * the failure.
3453 			 */
3454 			if (failing_groups != 0)
3455 				return 0;
3456 
3457 			/* Calibrate the LFIFO */
3458 			if (!((STATIC_CALIB_STEPS) & CALIB_SKIP_LFIFO)) {
3459 				/*
3460 				 * If we're skipping groups as part of debug,
3461 				 * don't calibrate LFIFO.
3462 				 */
3463 				if (param->skip_groups == 0) {
3464 					if (!rw_mgr_mem_calibrate_lfifo())
3465 						return 0;
3466 				}
3467 			}
3468 		}
3469 	}
3470 
3471 	/*
3472 	 * Do not remove this line as it makes sure all of our decisions
3473 	 * have been applied.
3474 	 */
3475 	writel(0, &sdr_scc_mgr->update);
3476 	return 1;
3477 }
3478 
3479 static uint32_t run_mem_calibrate(void)
3480 {
3481 	uint32_t pass;
3482 	uint32_t debug_info;
3483 
3484 	debug("%s:%d\n", __func__, __LINE__);
3485 
3486 	/* Reset pass/fail status shown on afi_cal_success/fail */
3487 	writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
3488 
3489 	/* stop tracking manger */
3490 	uint32_t ctrlcfg = readl(&sdr_ctrl->ctrl_cfg);
3491 
3492 	writel(ctrlcfg & 0xFFBFFFFF, &sdr_ctrl->ctrl_cfg);
3493 
3494 	initialize();
3495 	rw_mgr_mem_initialize();
3496 
3497 	pass = mem_calibrate();
3498 
3499 	mem_precharge_and_activate();
3500 	writel(0, &phy_mgr_cmd->fifo_reset);
3501 
3502 	/*
3503 	 * Handoff:
3504 	 * Don't return control of the PHY back to AFI when in debug mode.
3505 	 */
3506 	if ((gbl->phy_debug_mode_flags & PHY_DEBUG_IN_DEBUG_MODE) == 0) {
3507 		rw_mgr_mem_handoff();
3508 		/*
3509 		 * In Hard PHY this is a 2-bit control:
3510 		 * 0: AFI Mux Select
3511 		 * 1: DDIO Mux Select
3512 		 */
3513 		writel(0x2, &phy_mgr_cfg->mux_sel);
3514 	}
3515 
3516 	writel(ctrlcfg, &sdr_ctrl->ctrl_cfg);
3517 
3518 	if (pass) {
3519 		printf("%s: CALIBRATION PASSED\n", __FILE__);
3520 
3521 		gbl->fom_in /= 2;
3522 		gbl->fom_out /= 2;
3523 
3524 		if (gbl->fom_in > 0xff)
3525 			gbl->fom_in = 0xff;
3526 
3527 		if (gbl->fom_out > 0xff)
3528 			gbl->fom_out = 0xff;
3529 
3530 		/* Update the FOM in the register file */
3531 		debug_info = gbl->fom_in;
3532 		debug_info |= gbl->fom_out << 8;
3533 		writel(debug_info, &sdr_reg_file->fom);
3534 
3535 		writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3536 		writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
3537 	} else {
3538 		printf("%s: CALIBRATION FAILED\n", __FILE__);
3539 
3540 		debug_info = gbl->error_stage;
3541 		debug_info |= gbl->error_substage << 8;
3542 		debug_info |= gbl->error_group << 16;
3543 
3544 		writel(debug_info, &sdr_reg_file->failing_stage);
3545 		writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3546 		writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
3547 
3548 		/* Update the failing group/stage in the register file */
3549 		debug_info = gbl->error_stage;
3550 		debug_info |= gbl->error_substage << 8;
3551 		debug_info |= gbl->error_group << 16;
3552 		writel(debug_info, &sdr_reg_file->failing_stage);
3553 	}
3554 
3555 	return pass;
3556 }
3557 
3558 /**
3559  * hc_initialize_rom_data() - Initialize ROM data
3560  *
3561  * Initialize ROM data.
3562  */
3563 static void hc_initialize_rom_data(void)
3564 {
3565 	u32 i, addr;
3566 
3567 	addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
3568 	for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3569 		writel(inst_rom_init[i], addr + (i << 2));
3570 
3571 	addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
3572 	for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3573 		writel(ac_rom_init[i], addr + (i << 2));
3574 }
3575 
3576 /**
3577  * initialize_reg_file() - Initialize SDR register file
3578  *
3579  * Initialize SDR register file.
3580  */
3581 static void initialize_reg_file(void)
3582 {
3583 	/* Initialize the register file with the correct data */
3584 	writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3585 	writel(0, &sdr_reg_file->debug_data_addr);
3586 	writel(0, &sdr_reg_file->cur_stage);
3587 	writel(0, &sdr_reg_file->fom);
3588 	writel(0, &sdr_reg_file->failing_stage);
3589 	writel(0, &sdr_reg_file->debug1);
3590 	writel(0, &sdr_reg_file->debug2);
3591 }
3592 
3593 /**
3594  * initialize_hps_phy() - Initialize HPS PHY
3595  *
3596  * Initialize HPS PHY.
3597  */
3598 static void initialize_hps_phy(void)
3599 {
3600 	uint32_t reg;
3601 	/*
3602 	 * Tracking also gets configured here because it's in the
3603 	 * same register.
3604 	 */
3605 	uint32_t trk_sample_count = 7500;
3606 	uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3607 	/*
3608 	 * Format is number of outer loops in the 16 MSB, sample
3609 	 * count in 16 LSB.
3610 	 */
3611 
3612 	reg = 0;
3613 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3614 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3615 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3616 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3617 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3618 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3619 	/*
3620 	 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3621 	 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3622 	 */
3623 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3624 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3625 		trk_sample_count);
3626 	writel(reg, &sdr_ctrl->phy_ctrl0);
3627 
3628 	reg = 0;
3629 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3630 		trk_sample_count >>
3631 		SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3632 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3633 		trk_long_idle_sample_count);
3634 	writel(reg, &sdr_ctrl->phy_ctrl1);
3635 
3636 	reg = 0;
3637 	reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3638 		trk_long_idle_sample_count >>
3639 		SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
3640 	writel(reg, &sdr_ctrl->phy_ctrl2);
3641 }
3642 
3643 static void initialize_tracking(void)
3644 {
3645 	uint32_t concatenated_longidle = 0x0;
3646 	uint32_t concatenated_delays = 0x0;
3647 	uint32_t concatenated_rw_addr = 0x0;
3648 	uint32_t concatenated_refresh = 0x0;
3649 	uint32_t trk_sample_count = 7500;
3650 	uint32_t dtaps_per_ptap;
3651 	uint32_t tmp_delay;
3652 
3653 	/*
3654 	 * compute usable version of value in case we skip full
3655 	 * computation later
3656 	 */
3657 	dtaps_per_ptap = 0;
3658 	tmp_delay = 0;
3659 	while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
3660 		dtaps_per_ptap++;
3661 		tmp_delay += IO_DELAY_PER_DCHAIN_TAP;
3662 	}
3663 	dtaps_per_ptap--;
3664 
3665 	concatenated_longidle = concatenated_longidle ^ 10;
3666 		/*longidle outer loop */
3667 	concatenated_longidle = concatenated_longidle << 16;
3668 	concatenated_longidle = concatenated_longidle ^ 100;
3669 		/*longidle sample count */
3670 	concatenated_delays = concatenated_delays ^ 243;
3671 		/* trfc, worst case of 933Mhz 4Gb */
3672 	concatenated_delays = concatenated_delays << 8;
3673 	concatenated_delays = concatenated_delays ^ 14;
3674 		/* trcd, worst case */
3675 	concatenated_delays = concatenated_delays << 8;
3676 	concatenated_delays = concatenated_delays ^ 10;
3677 		/* vfifo wait */
3678 	concatenated_delays = concatenated_delays << 8;
3679 	concatenated_delays = concatenated_delays ^ 4;
3680 		/* mux delay */
3681 
3682 	concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_IDLE;
3683 	concatenated_rw_addr = concatenated_rw_addr << 8;
3684 	concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_ACTIVATE_1;
3685 	concatenated_rw_addr = concatenated_rw_addr << 8;
3686 	concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_SGLE_READ;
3687 	concatenated_rw_addr = concatenated_rw_addr << 8;
3688 	concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_PRECHARGE_ALL;
3689 
3690 	concatenated_refresh = concatenated_refresh ^ RW_MGR_REFRESH_ALL;
3691 	concatenated_refresh = concatenated_refresh << 24;
3692 	concatenated_refresh = concatenated_refresh ^ 1000; /* trefi */
3693 
3694 	/* Initialize the register file with the correct data */
3695 	writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
3696 	writel(trk_sample_count, &sdr_reg_file->trk_sample_count);
3697 	writel(concatenated_longidle, &sdr_reg_file->trk_longidle);
3698 	writel(concatenated_delays, &sdr_reg_file->delays);
3699 	writel(concatenated_rw_addr, &sdr_reg_file->trk_rw_mgr_addr);
3700 	writel(RW_MGR_MEM_IF_READ_DQS_WIDTH, &sdr_reg_file->trk_read_dqs_width);
3701 	writel(concatenated_refresh, &sdr_reg_file->trk_rfsh);
3702 }
3703 
3704 int sdram_calibration_full(void)
3705 {
3706 	struct param_type my_param;
3707 	struct gbl_type my_gbl;
3708 	uint32_t pass;
3709 	uint32_t i;
3710 
3711 	param = &my_param;
3712 	gbl = &my_gbl;
3713 
3714 	/* Initialize the debug mode flags */
3715 	gbl->phy_debug_mode_flags = 0;
3716 	/* Set the calibration enabled by default */
3717 	gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3718 	/*
3719 	 * Only sweep all groups (regardless of fail state) by default
3720 	 * Set enabled read test by default.
3721 	 */
3722 #if DISABLE_GUARANTEED_READ
3723 	gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3724 #endif
3725 	/* Initialize the register file */
3726 	initialize_reg_file();
3727 
3728 	/* Initialize any PHY CSR */
3729 	initialize_hps_phy();
3730 
3731 	scc_mgr_initialize();
3732 
3733 	initialize_tracking();
3734 
3735 	/* USER Enable all ranks, groups */
3736 	for (i = 0; i < RW_MGR_MEM_NUMBER_OF_RANKS; i++)
3737 		param->skip_ranks[i] = 0;
3738 	for (i = 0; i < NUM_SHADOW_REGS; ++i)
3739 		param->skip_shadow_regs[i] = 0;
3740 	param->skip_groups = 0;
3741 
3742 	printf("%s: Preparing to start memory calibration\n", __FILE__);
3743 
3744 	debug("%s:%d\n", __func__, __LINE__);
3745 	debug_cond(DLEVEL == 1,
3746 		   "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3747 		   RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3748 		   RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3749 		   RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3750 		   RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3751 	debug_cond(DLEVEL == 1,
3752 		   "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3753 		   RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3754 		   RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3755 		   IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3756 	debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3757 		   IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3758 	debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3759 		   IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3760 		   IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3761 	debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3762 		   IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3763 		   IO_IO_OUT2_DELAY_MAX);
3764 	debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3765 		   IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
3766 
3767 	hc_initialize_rom_data();
3768 
3769 	/* update info for sims */
3770 	reg_file_set_stage(CAL_STAGE_NIL);
3771 	reg_file_set_group(0);
3772 
3773 	/*
3774 	 * Load global needed for those actions that require
3775 	 * some dynamic calibration support.
3776 	 */
3777 	dyn_calib_steps = STATIC_CALIB_STEPS;
3778 	/*
3779 	 * Load global to allow dynamic selection of delay loop settings
3780 	 * based on calibration mode.
3781 	 */
3782 	if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3783 		skip_delay_mask = 0xff;
3784 	else
3785 		skip_delay_mask = 0x0;
3786 
3787 	pass = run_mem_calibrate();
3788 
3789 	printf("%s: Calibration complete\n", __FILE__);
3790 	return pass;
3791 }
3792