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