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