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