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