blob: 3b26726dcffede77dda4c1d335ed0f4bad23e686 [file] [log] [blame]
Dinh Nguyen3da42852015-06-02 22:52:49 -05001/*
2 * Copyright Altera Corporation (C) 2012-2015
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/sdram.h>
10#include "sequencer.h"
11#include "sequencer_auto.h"
12#include "sequencer_auto_ac_init.h"
13#include "sequencer_auto_inst_init.h"
14#include "sequencer_defines.h"
15
Dinh Nguyen3da42852015-06-02 22:52:49 -050016static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020017 (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen3da42852015-06-02 22:52:49 -050018
19static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020020 (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050021
22static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasuta1c654a2015-07-12 18:31:05 +020023 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050024
25static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasute79025a2015-07-12 18:42:34 +020026 (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050027
28static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasut1bc6f142015-07-12 18:54:37 +020029 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050030
31static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasut1bc6f142015-07-12 18:54:37 +020032 (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen3da42852015-06-02 22:52:49 -050033
34static struct socfpga_data_mgr *data_mgr =
Marek Vasutc4815f72015-07-12 19:03:33 +020035 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050036
Marek Vasut6cb9f162015-07-12 20:49:39 +020037static struct socfpga_sdr_ctrl *sdr_ctrl =
38 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
39
Dinh Nguyen3da42852015-06-02 22:52:49 -050040#define DELTA_D 1
Dinh Nguyen3da42852015-06-02 22:52:49 -050041
42/*
43 * In order to reduce ROM size, most of the selectable calibration steps are
44 * decided at compile time based on the user's calibration mode selection,
45 * as captured by the STATIC_CALIB_STEPS selection below.
46 *
47 * However, to support simulation-time selection of fast simulation mode, where
48 * we skip everything except the bare minimum, we need a few of the steps to
49 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
50 * check, which is based on the rtl-supplied value, or we dynamically compute
51 * the value to use based on the dynamically-chosen calibration mode
52 */
53
54#define DLEVEL 0
55#define STATIC_IN_RTL_SIM 0
56#define STATIC_SKIP_DELAY_LOOPS 0
57
58#define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
59 STATIC_SKIP_DELAY_LOOPS)
60
61/* calibration steps requested by the rtl */
62uint16_t dyn_calib_steps;
63
64/*
65 * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
66 * instead of static, we use boolean logic to select between
67 * non-skip and skip values
68 *
69 * The mask is set to include all bits when not-skipping, but is
70 * zero when skipping
71 */
72
73uint16_t skip_delay_mask; /* mask off bits when skipping/not-skipping */
74
75#define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
76 ((non_skip_value) & skip_delay_mask)
77
78struct gbl_type *gbl;
79struct param_type *param;
80uint32_t curr_shadow_reg;
81
82static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
83 uint32_t write_group, uint32_t use_dm,
84 uint32_t all_correct, uint32_t *bit_chk, uint32_t all_ranks);
85
Dinh Nguyen3da42852015-06-02 22:52:49 -050086static void set_failing_group_stage(uint32_t group, uint32_t stage,
87 uint32_t substage)
88{
89 /*
90 * Only set the global stage if there was not been any other
91 * failing group
92 */
93 if (gbl->error_stage == CAL_STAGE_NIL) {
94 gbl->error_substage = substage;
95 gbl->error_stage = stage;
96 gbl->error_group = group;
97 }
98}
99
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200100static void reg_file_set_group(u16 set_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500101{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200102 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500103}
104
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200105static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500106{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200107 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500108}
109
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200110static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500111{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200112 set_sub_stage &= 0xff;
113 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500114}
115
116static void initialize(void)
117{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500118 debug("%s:%d\n", __func__, __LINE__);
119 /* USER calibration has control over path to memory */
120 /*
121 * In Hard PHY this is a 2-bit control:
122 * 0: AFI Mux Select
123 * 1: DDIO Mux Select
124 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200125 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500126
127 /* USER memory clock is not stable we begin initialization */
Marek Vasut1273dd92015-07-12 21:05:08 +0200128 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500129
130 /* USER calibration status all set to zero */
Marek Vasut1273dd92015-07-12 21:05:08 +0200131 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500132
Marek Vasut1273dd92015-07-12 21:05:08 +0200133 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500134
135 if ((dyn_calib_steps & CALIB_SKIP_ALL) != CALIB_SKIP_ALL) {
136 param->read_correct_mask_vg = ((uint32_t)1 <<
137 (RW_MGR_MEM_DQ_PER_READ_DQS /
138 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
139 param->write_correct_mask_vg = ((uint32_t)1 <<
140 (RW_MGR_MEM_DQ_PER_READ_DQS /
141 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
142 param->read_correct_mask = ((uint32_t)1 <<
143 RW_MGR_MEM_DQ_PER_READ_DQS) - 1;
144 param->write_correct_mask = ((uint32_t)1 <<
145 RW_MGR_MEM_DQ_PER_WRITE_DQS) - 1;
146 param->dm_correct_mask = ((uint32_t)1 <<
147 (RW_MGR_MEM_DATA_WIDTH / RW_MGR_MEM_DATA_MASK_WIDTH))
148 - 1;
149 }
150}
151
152static void set_rank_and_odt_mask(uint32_t rank, uint32_t odt_mode)
153{
154 uint32_t odt_mask_0 = 0;
155 uint32_t odt_mask_1 = 0;
156 uint32_t cs_and_odt_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500157
158 if (odt_mode == RW_MGR_ODT_MODE_READ_WRITE) {
159 if (RW_MGR_MEM_NUMBER_OF_RANKS == 1) {
160 /*
161 * 1 Rank
162 * Read: ODT = 0
163 * Write: ODT = 1
164 */
165 odt_mask_0 = 0x0;
166 odt_mask_1 = 0x1;
167 } else if (RW_MGR_MEM_NUMBER_OF_RANKS == 2) {
168 /* 2 Ranks */
169 if (RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM == 1) {
170 /* - Dual-Slot , Single-Rank
171 * (1 chip-select per DIMM)
172 * OR
173 * - RDIMM, 4 total CS (2 CS per DIMM)
174 * means 2 DIMM
175 * Since MEM_NUMBER_OF_RANKS is 2 they are
176 * both single rank
177 * with 2 CS each (special for RDIMM)
178 * Read: Turn on ODT on the opposite rank
179 * Write: Turn on ODT on all ranks
180 */
181 odt_mask_0 = 0x3 & ~(1 << rank);
182 odt_mask_1 = 0x3;
183 } else {
184 /*
185 * USER - Single-Slot , Dual-rank DIMMs
186 * (2 chip-selects per DIMM)
187 * USER Read: Turn on ODT off on all ranks
188 * USER Write: Turn on ODT on active rank
189 */
190 odt_mask_0 = 0x0;
191 odt_mask_1 = 0x3 & (1 << rank);
192 }
Marek Vasut963bca62015-07-18 02:23:29 +0200193 } else {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500194 /* 4 Ranks
195 * Read:
196 * ----------+-----------------------+
197 * | |
198 * | ODT |
199 * Read From +-----------------------+
200 * Rank | 3 | 2 | 1 | 0 |
201 * ----------+-----+-----+-----+-----+
202 * 0 | 0 | 1 | 0 | 0 |
203 * 1 | 1 | 0 | 0 | 0 |
204 * 2 | 0 | 0 | 0 | 1 |
205 * 3 | 0 | 0 | 1 | 0 |
206 * ----------+-----+-----+-----+-----+
207 *
208 * Write:
209 * ----------+-----------------------+
210 * | |
211 * | ODT |
212 * Write To +-----------------------+
213 * Rank | 3 | 2 | 1 | 0 |
214 * ----------+-----+-----+-----+-----+
215 * 0 | 0 | 1 | 0 | 1 |
216 * 1 | 1 | 0 | 1 | 0 |
217 * 2 | 0 | 1 | 0 | 1 |
218 * 3 | 1 | 0 | 1 | 0 |
219 * ----------+-----+-----+-----+-----+
220 */
221 switch (rank) {
222 case 0:
223 odt_mask_0 = 0x4;
224 odt_mask_1 = 0x5;
225 break;
226 case 1:
227 odt_mask_0 = 0x8;
228 odt_mask_1 = 0xA;
229 break;
230 case 2:
231 odt_mask_0 = 0x1;
232 odt_mask_1 = 0x5;
233 break;
234 case 3:
235 odt_mask_0 = 0x2;
236 odt_mask_1 = 0xA;
237 break;
238 }
239 }
240 } else {
241 odt_mask_0 = 0x0;
242 odt_mask_1 = 0x0;
243 }
244
245 cs_and_odt_mask =
246 (0xFF & ~(1 << rank)) |
247 ((0xFF & odt_mask_0) << 8) |
248 ((0xFF & odt_mask_1) << 16);
Marek Vasut1273dd92015-07-12 21:05:08 +0200249 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
250 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500251}
252
Marek Vasutc76976d2015-07-12 22:28:33 +0200253/**
254 * scc_mgr_set() - Set SCC Manager register
255 * @off: Base offset in SCC Manager space
256 * @grp: Read/Write group
257 * @val: Value to be set
258 *
259 * This function sets the SCC Manager (Scan Chain Control Manager) register.
260 */
261static void scc_mgr_set(u32 off, u32 grp, u32 val)
262{
263 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
264}
265
Marek Vasute893f4d2015-07-20 07:16:42 +0200266/**
267 * scc_mgr_initialize() - Initialize SCC Manager registers
268 *
269 * Initialize SCC Manager registers.
270 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500271static void scc_mgr_initialize(void)
272{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500273 /*
Marek Vasute893f4d2015-07-20 07:16:42 +0200274 * Clear register file for HPS. 16 (2^4) is the size of the
275 * full register file in the scc mgr:
276 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
277 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500278 */
Marek Vasutc76976d2015-07-12 22:28:33 +0200279 int i;
Marek Vasute893f4d2015-07-20 07:16:42 +0200280
Dinh Nguyen3da42852015-06-02 22:52:49 -0500281 for (i = 0; i < 16; i++) {
Marek Vasut7ac40d22015-06-26 18:56:54 +0200282 debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -0500283 __func__, __LINE__, i);
Marek Vasutc76976d2015-07-12 22:28:33 +0200284 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, 0, i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500285 }
286}
287
Marek Vasut5ff825b2015-07-12 22:11:55 +0200288static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
289{
Marek Vasutc76976d2015-07-12 22:28:33 +0200290 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200291}
292
293static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500294{
Marek Vasutc76976d2015-07-12 22:28:33 +0200295 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500296}
297
Dinh Nguyen3da42852015-06-02 22:52:49 -0500298static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
299{
Marek Vasutc76976d2015-07-12 22:28:33 +0200300 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500301}
302
Marek Vasut5ff825b2015-07-12 22:11:55 +0200303static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
304{
Marek Vasutc76976d2015-07-12 22:28:33 +0200305 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200306}
307
Marek Vasut32675242015-07-17 06:07:13 +0200308static void scc_mgr_set_dqs_io_in_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200309{
Marek Vasutc76976d2015-07-12 22:28:33 +0200310 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
311 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200312}
313
314static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
315{
Marek Vasutc76976d2015-07-12 22:28:33 +0200316 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200317}
318
319static void scc_mgr_set_dq_out1_delay(uint32_t dq_in_group, uint32_t delay)
320{
Marek Vasutc76976d2015-07-12 22:28:33 +0200321 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200322}
323
Marek Vasut32675242015-07-17 06:07:13 +0200324static void scc_mgr_set_dqs_out1_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200325{
Marek Vasutc76976d2015-07-12 22:28:33 +0200326 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
327 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200328}
329
330static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
331{
Marek Vasutc76976d2015-07-12 22:28:33 +0200332 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
333 RW_MGR_MEM_DQ_PER_WRITE_DQS + 1 + dm,
334 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200335}
336
337/* load up dqs config settings */
338static void scc_mgr_load_dqs(uint32_t dqs)
339{
340 writel(dqs, &sdr_scc_mgr->dqs_ena);
341}
342
343/* load up dqs io config settings */
344static void scc_mgr_load_dqs_io(void)
345{
346 writel(0, &sdr_scc_mgr->dqs_io_ena);
347}
348
349/* load up dq config settings */
350static void scc_mgr_load_dq(uint32_t dq_in_group)
351{
352 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
353}
354
355/* load up dm config settings */
356static void scc_mgr_load_dm(uint32_t dm)
357{
358 writel(dm, &sdr_scc_mgr->dm_ena);
359}
360
Marek Vasut0b69b802015-07-12 23:25:21 +0200361/**
362 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
363 * @off: Base offset in SCC Manager space
364 * @grp: Read/Write group
365 * @val: Value to be set
366 * @update: If non-zero, trigger SCC Manager update for all ranks
367 *
368 * This function sets the SCC Manager (Scan Chain Control Manager) register
369 * and optionally triggers the SCC update for all ranks.
370 */
371static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
372 const int update)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500373{
Marek Vasut0b69b802015-07-12 23:25:21 +0200374 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500375
376 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
377 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut0b69b802015-07-12 23:25:21 +0200378 scc_mgr_set(off, grp, val);
Marek Vasut162d60e2015-07-12 23:14:33 +0200379
Marek Vasut0b69b802015-07-12 23:25:21 +0200380 if (update || (r == 0)) {
381 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasut1273dd92015-07-12 21:05:08 +0200382 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500383 }
384 }
385}
386
Marek Vasut0b69b802015-07-12 23:25:21 +0200387static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
388{
389 /*
390 * USER although the h/w doesn't support different phases per
391 * shadow register, for simplicity our scc manager modeling
392 * keeps different phase settings per shadow reg, and it's
393 * important for us to keep them in sync to match h/w.
394 * for efficiency, the scan chain update should occur only
395 * once to sr0.
396 */
397 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
398 read_group, phase, 0);
399}
400
Dinh Nguyen3da42852015-06-02 22:52:49 -0500401static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
402 uint32_t phase)
403{
Marek Vasut0b69b802015-07-12 23:25:21 +0200404 /*
405 * USER although the h/w doesn't support different phases per
406 * shadow register, for simplicity our scc manager modeling
407 * keeps different phase settings per shadow reg, and it's
408 * important for us to keep them in sync to match h/w.
409 * for efficiency, the scan chain update should occur only
410 * once to sr0.
411 */
412 scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
413 write_group, phase, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500414}
415
Dinh Nguyen3da42852015-06-02 22:52:49 -0500416static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
417 uint32_t delay)
418{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500419 /*
420 * In shadow register mode, the T11 settings are stored in
421 * registers in the core, which are updated by the DQS_ENA
422 * signals. Not issuing the SCC_MGR_UPD command allows us to
423 * save lots of rank switching overhead, by calling
424 * select_shadow_regs_for_update with update_scan_chains
425 * set to 0.
426 */
Marek Vasut0b69b802015-07-12 23:25:21 +0200427 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
428 read_group, delay, 1);
Marek Vasut1273dd92015-07-12 21:05:08 +0200429 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500430}
431
Marek Vasut5be355c2015-07-12 23:39:06 +0200432/**
433 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
434 * @write_group: Write group
435 * @delay: Delay value
436 *
437 * This function sets the OCT output delay in SCC manager.
438 */
439static void scc_mgr_set_oct_out1_delay(const u32 write_group, const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500440{
Marek Vasut5be355c2015-07-12 23:39:06 +0200441 const int ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
442 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
443 const int base = write_group * ratio;
444 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500445 /*
446 * Load the setting in the SCC manager
447 * Although OCT affects only write data, the OCT delay is controlled
448 * by the DQS logic block which is instantiated once per read group.
449 * For protocols where a write group consists of multiple read groups,
450 * the setting must be set multiple times.
451 */
Marek Vasut5be355c2015-07-12 23:39:06 +0200452 for (i = 0; i < ratio; i++)
453 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500454}
455
Marek Vasut37a37ca2015-07-19 01:32:55 +0200456/**
457 * scc_mgr_set_hhp_extras() - Set HHP extras.
458 *
459 * Load the fixed setting in the SCC manager HHP extras.
460 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500461static void scc_mgr_set_hhp_extras(void)
462{
463 /*
464 * Load the fixed setting in the SCC manager
Marek Vasut37a37ca2015-07-19 01:32:55 +0200465 * bits: 0:0 = 1'b1 - DQS bypass
466 * bits: 1:1 = 1'b1 - DQ bypass
467 * bits: 4:2 = 3'b001 - rfifo_mode
468 * bits: 6:5 = 2'b01 - rfifo clock_select
469 * bits: 7:7 = 1'b0 - separate gating from ungating setting
470 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen3da42852015-06-02 22:52:49 -0500471 */
Marek Vasut37a37ca2015-07-19 01:32:55 +0200472 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
473 (1 << 2) | (1 << 1) | (1 << 0);
474 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
475 SCC_MGR_HHP_GLOBALS_OFFSET |
476 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500477
Marek Vasut37a37ca2015-07-19 01:32:55 +0200478 debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n",
479 __func__, __LINE__);
480 writel(value, addr);
481 debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
482 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500483}
484
Marek Vasutf42af352015-07-20 04:41:53 +0200485/**
486 * scc_mgr_zero_all() - Zero all DQS config
487 *
488 * Zero all DQS config.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500489 */
490static void scc_mgr_zero_all(void)
491{
Marek Vasutf42af352015-07-20 04:41:53 +0200492 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500493
494 /*
495 * USER Zero all DQS config settings, across all groups and all
496 * shadow registers
497 */
Marek Vasutf42af352015-07-20 04:41:53 +0200498 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
499 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500500 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
501 /*
502 * The phases actually don't exist on a per-rank basis,
503 * but there's no harm updating them several times, so
504 * let's keep the code simple.
505 */
506 scc_mgr_set_dqs_bus_in_delay(i, IO_DQS_IN_RESERVE);
507 scc_mgr_set_dqs_en_phase(i, 0);
508 scc_mgr_set_dqs_en_delay(i, 0);
509 }
510
511 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
512 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasutf42af352015-07-20 04:41:53 +0200513 /* Arria V/Cyclone V don't have out2. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500514 scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
515 }
516 }
517
Marek Vasutf42af352015-07-20 04:41:53 +0200518 /* Multicast to all DQS group enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200519 writel(0xff, &sdr_scc_mgr->dqs_ena);
520 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500521}
522
Marek Vasutc5c5f532015-07-17 02:06:20 +0200523/**
524 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
525 * @write_group: Write group
526 *
527 * Set bypass mode and trigger SCC update.
528 */
529static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500530{
Marek Vasutc5c5f532015-07-17 02:06:20 +0200531 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200532 writel(0xff, &sdr_scc_mgr->dq_ena);
533 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500534
Marek Vasutc5c5f532015-07-17 02:06:20 +0200535 /* Update current DQS IO enable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200536 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500537
Marek Vasutc5c5f532015-07-17 02:06:20 +0200538 /* Update the DQS logic. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200539 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500540
Marek Vasutc5c5f532015-07-17 02:06:20 +0200541 /* Hit update. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200542 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500543}
544
Marek Vasut5e837892015-07-13 00:30:09 +0200545/**
546 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
547 * @write_group: Write group
548 *
549 * Load DQS settings for Write Group, do not trigger SCC update.
550 */
551static void scc_mgr_load_dqs_for_write_group(const u32 write_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200552{
Marek Vasut5e837892015-07-13 00:30:09 +0200553 const int ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
554 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
555 const int base = write_group * ratio;
556 int i;
Marek Vasut5ff825b2015-07-12 22:11:55 +0200557 /*
Marek Vasut5e837892015-07-13 00:30:09 +0200558 * Load the setting in the SCC manager
Marek Vasut5ff825b2015-07-12 22:11:55 +0200559 * Although OCT affects only write data, the OCT delay is controlled
560 * by the DQS logic block which is instantiated once per read group.
561 * For protocols where a write group consists of multiple read groups,
Marek Vasut5e837892015-07-13 00:30:09 +0200562 * the setting must be set multiple times.
Marek Vasut5ff825b2015-07-12 22:11:55 +0200563 */
Marek Vasut5e837892015-07-13 00:30:09 +0200564 for (i = 0; i < ratio; i++)
565 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200566}
567
Marek Vasutd41ea932015-07-20 08:41:04 +0200568/**
569 * scc_mgr_zero_group() - Zero all configs for a group
570 *
571 * Zero DQ, DM, DQS and OCT configs for a group.
572 */
573static void scc_mgr_zero_group(const u32 write_group, const int out_only)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500574{
Marek Vasutd41ea932015-07-20 08:41:04 +0200575 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500576
Marek Vasutd41ea932015-07-20 08:41:04 +0200577 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
578 r += NUM_RANKS_PER_SHADOW_REG) {
579 /* Zero all DQ config settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500580 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200581 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500582 if (!out_only)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200583 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500584 }
585
Marek Vasutd41ea932015-07-20 08:41:04 +0200586 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200587 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500588
Marek Vasutd41ea932015-07-20 08:41:04 +0200589 /* Zero all DM config settings. */
590 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200591 scc_mgr_set_dm_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500592
Marek Vasutd41ea932015-07-20 08:41:04 +0200593 /* Multicast to all DM enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200594 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500595
Marek Vasutd41ea932015-07-20 08:41:04 +0200596 /* Zero all DQS IO settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500597 if (!out_only)
Marek Vasut32675242015-07-17 06:07:13 +0200598 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasutd41ea932015-07-20 08:41:04 +0200599
600 /* Arria V/Cyclone V don't have out2. */
Marek Vasut32675242015-07-17 06:07:13 +0200601 scc_mgr_set_dqs_out1_delay(IO_DQS_OUT_RESERVE);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500602 scc_mgr_set_oct_out1_delay(write_group, IO_DQS_OUT_RESERVE);
603 scc_mgr_load_dqs_for_write_group(write_group);
604
Marek Vasutd41ea932015-07-20 08:41:04 +0200605 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasut1273dd92015-07-12 21:05:08 +0200606 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500607
Marek Vasutd41ea932015-07-20 08:41:04 +0200608 /* Hit update to zero everything. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200609 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500610 }
611}
612
Dinh Nguyen3da42852015-06-02 22:52:49 -0500613/*
614 * apply and load a particular input delay for the DQ pins in a group
615 * group_bgn is the index of the first dq pin (in the write group)
616 */
Marek Vasut32675242015-07-17 06:07:13 +0200617static void scc_mgr_apply_group_dq_in_delay(uint32_t group_bgn, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500618{
619 uint32_t i, p;
620
621 for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200622 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500623 scc_mgr_load_dq(p);
624 }
625}
626
Marek Vasut300c2e62015-07-17 05:42:49 +0200627/**
628 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the DQ pins in a group
629 * @delay: Delay value
630 *
631 * Apply and load a particular output delay for the DQ pins in a group.
632 */
633static void scc_mgr_apply_group_dq_out1_delay(const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500634{
Marek Vasut300c2e62015-07-17 05:42:49 +0200635 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500636
Marek Vasut300c2e62015-07-17 05:42:49 +0200637 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
638 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500639 scc_mgr_load_dq(i);
640 }
641}
642
643/* apply and load a particular output delay for the DM pins in a group */
Marek Vasut32675242015-07-17 06:07:13 +0200644static void scc_mgr_apply_group_dm_out1_delay(uint32_t delay1)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500645{
646 uint32_t i;
647
648 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200649 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500650 scc_mgr_load_dm(i);
651 }
652}
653
654
655/* apply and load delay on both DQS and OCT out1 */
656static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
657 uint32_t delay)
658{
Marek Vasut32675242015-07-17 06:07:13 +0200659 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500660 scc_mgr_load_dqs_io();
661
662 scc_mgr_set_oct_out1_delay(write_group, delay);
663 scc_mgr_load_dqs_for_write_group(write_group);
664}
665
Marek Vasut5cb1b502015-07-17 05:33:28 +0200666/**
667 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side: DQ, DM, DQS, OCT
668 * @write_group: Write group
669 * @delay: Delay value
670 *
671 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
672 */
Marek Vasut8eccde32015-07-17 05:30:14 +0200673static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
Marek Vasut8eccde32015-07-17 05:30:14 +0200674 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500675{
Marek Vasut8eccde32015-07-17 05:30:14 +0200676 u32 i, new_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500677
Marek Vasut8eccde32015-07-17 05:30:14 +0200678 /* DQ shift */
679 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500680 scc_mgr_load_dq(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500681
Marek Vasut8eccde32015-07-17 05:30:14 +0200682 /* DM shift */
683 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500684 scc_mgr_load_dm(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500685
Marek Vasut5cb1b502015-07-17 05:33:28 +0200686 /* DQS shift */
687 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500688 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200689 debug_cond(DLEVEL == 1,
690 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
691 __func__, __LINE__, write_group, delay, new_delay,
692 IO_IO_OUT2_DELAY_MAX,
Dinh Nguyen3da42852015-06-02 22:52:49 -0500693 new_delay - IO_IO_OUT2_DELAY_MAX);
Marek Vasut5cb1b502015-07-17 05:33:28 +0200694 new_delay -= IO_IO_OUT2_DELAY_MAX;
695 scc_mgr_set_dqs_out1_delay(new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500696 }
697
698 scc_mgr_load_dqs_io();
699
Marek Vasut5cb1b502015-07-17 05:33:28 +0200700 /* OCT shift */
701 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500702 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200703 debug_cond(DLEVEL == 1,
704 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
705 __func__, __LINE__, write_group, delay,
706 new_delay, IO_IO_OUT2_DELAY_MAX,
Dinh Nguyen3da42852015-06-02 22:52:49 -0500707 new_delay - IO_IO_OUT2_DELAY_MAX);
Marek Vasut5cb1b502015-07-17 05:33:28 +0200708 new_delay -= IO_IO_OUT2_DELAY_MAX;
709 scc_mgr_set_oct_out1_delay(write_group, new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500710 }
711
712 scc_mgr_load_dqs_for_write_group(write_group);
713}
714
715/*
716 * USER apply a delay to the entire output side (DQ, DM, DQS, OCT)
717 * and to all ranks
718 */
719static void scc_mgr_apply_group_all_out_delay_add_all_ranks(
720 uint32_t write_group, uint32_t group_bgn, uint32_t delay)
721{
722 uint32_t r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500723
724 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
725 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200726 scc_mgr_apply_group_all_out_delay_add(write_group, delay);
Marek Vasut1273dd92015-07-12 21:05:08 +0200727 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500728 }
729}
730
731/* optimization used to recover some slots in ddr3 inst_rom */
732/* could be applied to other protocols if we wanted to */
733static void set_jump_as_return(void)
734{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500735 /*
736 * to save space, we replace return with jump to special shared
737 * RETURN instruction so we set the counter to large value so that
738 * we always jump
739 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200740 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
741 writel(RW_MGR_RETURN, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500742}
743
744/*
745 * should always use constants as argument to ensure all computations are
746 * performed at compile time
747 */
748static void delay_for_n_mem_clocks(const uint32_t clocks)
749{
750 uint32_t afi_clocks;
751 uint8_t inner = 0;
752 uint8_t outer = 0;
753 uint16_t c_loop = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500754
755 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
756
757
758 afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
759 /* scale (rounding up) to get afi clocks */
760
761 /*
762 * Note, we don't bother accounting for being off a little bit
763 * because of a few extra instructions in outer loops
764 * Note, the loops have a test at the end, and do the test before
765 * the decrement, and so always perform the loop
766 * 1 time more than the counter value
767 */
768 if (afi_clocks == 0) {
769 ;
770 } else if (afi_clocks <= 0x100) {
771 inner = afi_clocks-1;
772 outer = 0;
773 c_loop = 0;
774 } else if (afi_clocks <= 0x10000) {
775 inner = 0xff;
776 outer = (afi_clocks-1) >> 8;
777 c_loop = 0;
778 } else {
779 inner = 0xff;
780 outer = 0xff;
781 c_loop = (afi_clocks-1) >> 16;
782 }
783
784 /*
785 * rom instructions are structured as follows:
786 *
787 * IDLE_LOOP2: jnz cntr0, TARGET_A
788 * IDLE_LOOP1: jnz cntr1, TARGET_B
789 * return
790 *
791 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
792 * TARGET_B is set to IDLE_LOOP2 as well
793 *
794 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
795 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
796 *
797 * a little confusing, but it helps save precious space in the inst_rom
798 * and sequencer rom and keeps the delays more accurate and reduces
799 * overhead
800 */
801 if (afi_clocks <= 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200802 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
803 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500804
Marek Vasut1273dd92015-07-12 21:05:08 +0200805 writel(RW_MGR_IDLE_LOOP1,
806 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500807
Marek Vasut1273dd92015-07-12 21:05:08 +0200808 writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
809 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500810 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200811 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
812 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500813
Marek Vasut1273dd92015-07-12 21:05:08 +0200814 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
815 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500816
Marek Vasut1273dd92015-07-12 21:05:08 +0200817 writel(RW_MGR_IDLE_LOOP2,
818 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500819
Marek Vasut1273dd92015-07-12 21:05:08 +0200820 writel(RW_MGR_IDLE_LOOP2,
821 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500822
823 /* hack to get around compiler not being smart enough */
824 if (afi_clocks <= 0x10000) {
825 /* only need to run once */
Marek Vasut1273dd92015-07-12 21:05:08 +0200826 writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
827 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500828 } else {
829 do {
Marek Vasut1273dd92015-07-12 21:05:08 +0200830 writel(RW_MGR_IDLE_LOOP2,
831 SDR_PHYGRP_RWMGRGRP_ADDRESS |
832 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500833 } while (c_loop-- != 0);
834 }
835 }
836 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
837}
838
839static void rw_mgr_mem_initialize(void)
840{
841 uint32_t r;
Marek Vasut1273dd92015-07-12 21:05:08 +0200842 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
843 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500844
845 debug("%s:%d\n", __func__, __LINE__);
846
847 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200848 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
849 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500850
851 /*
852 * Here's how you load register for a loop
853 * Counters are located @ 0x800
854 * Jump address are located @ 0xC00
855 * For both, registers 0 to 3 are selected using bits 3 and 2, like
856 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
857 * I know this ain't pretty, but Avalon bus throws away the 2 least
858 * significant bits
859 */
860
861 /* start with memory RESET activated */
862
863 /* tINIT = 200us */
864
865 /*
866 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
867 * If a and b are the number of iteration in 2 nested loops
868 * it takes the following number of cycles to complete the operation:
869 * number_of_cycles = ((2 + n) * a + 2) * b
870 * where n is the number of instruction in the inner loop
871 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
872 * b = 6A
873 */
874
875 /* Load counters */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500876 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR0_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200877 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500878 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR1_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200879 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500880 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR2_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200881 &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500882
883 /* Load jump address */
Marek Vasut1273dd92015-07-12 21:05:08 +0200884 writel(RW_MGR_INIT_RESET_0_CKE_0,
885 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
886 writel(RW_MGR_INIT_RESET_0_CKE_0,
887 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
888 writel(RW_MGR_INIT_RESET_0_CKE_0,
889 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500890
891 /* Execute count instruction */
Marek Vasut1273dd92015-07-12 21:05:08 +0200892 writel(RW_MGR_INIT_RESET_0_CKE_0, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500893
894 /* indicate that memory is stable */
Marek Vasut1273dd92015-07-12 21:05:08 +0200895 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500896
897 /*
898 * transition the RESET to high
899 * Wait for 500us
900 */
901
902 /*
903 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
904 * If a and b are the number of iteration in 2 nested loops
905 * it takes the following number of cycles to complete the operation
906 * number_of_cycles = ((2 + n) * a + 2) * b
907 * where n is the number of instruction in the inner loop
908 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
909 * b = FF
910 */
911
912 /* Load counters */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500913 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR0_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200914 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500915 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR1_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200916 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500917 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR2_VAL),
Marek Vasut1273dd92015-07-12 21:05:08 +0200918 &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500919
920 /* Load jump address */
Marek Vasut1273dd92015-07-12 21:05:08 +0200921 writel(RW_MGR_INIT_RESET_1_CKE_0,
922 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
923 writel(RW_MGR_INIT_RESET_1_CKE_0,
924 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
925 writel(RW_MGR_INIT_RESET_1_CKE_0,
926 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500927
Marek Vasut1273dd92015-07-12 21:05:08 +0200928 writel(RW_MGR_INIT_RESET_1_CKE_0, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500929
930 /* bring up clock enable */
931
932 /* tXRP < 250 ck cycles */
933 delay_for_n_mem_clocks(250);
934
935 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
936 if (param->skip_ranks[r]) {
937 /* request to skip the rank */
938 continue;
939 }
940
941 /* set rank */
942 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
943
944 /*
945 * USER Use Mirror-ed commands for odd ranks if address
946 * mirrorring is on
947 */
948 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
949 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200950 writel(RW_MGR_MRS2_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500951 delay_for_n_mem_clocks(4);
952 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200953 writel(RW_MGR_MRS3_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500954 delay_for_n_mem_clocks(4);
955 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200956 writel(RW_MGR_MRS1_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500957 delay_for_n_mem_clocks(4);
958 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200959 writel(RW_MGR_MRS0_DLL_RESET_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500960 } else {
961 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200962 writel(RW_MGR_MRS2, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500963 delay_for_n_mem_clocks(4);
964 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200965 writel(RW_MGR_MRS3, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500966 delay_for_n_mem_clocks(4);
967 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200968 writel(RW_MGR_MRS1, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500969 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200970 writel(RW_MGR_MRS0_DLL_RESET, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500971 }
972 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +0200973 writel(RW_MGR_ZQCL, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500974
975 /* tZQinit = tDLLK = 512 ck cycles */
976 delay_for_n_mem_clocks(512);
977 }
978}
979
980/*
981 * At the end of calibration we have to program the user settings in, and
982 * USER hand off the memory to the user.
983 */
984static void rw_mgr_mem_handoff(void)
985{
986 uint32_t r;
Marek Vasut1273dd92015-07-12 21:05:08 +0200987 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
988 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500989
990 debug("%s:%d\n", __func__, __LINE__);
991 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
992 if (param->skip_ranks[r])
993 /* request to skip the rank */
994 continue;
995 /* set rank */
996 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
997
998 /* precharge all banks ... */
Marek Vasut1273dd92015-07-12 21:05:08 +0200999 writel(RW_MGR_PRECHARGE_ALL, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001000
1001 /* load up MR settings specified by user */
1002
1003 /*
1004 * Use Mirror-ed commands for odd ranks if address
1005 * mirrorring is on
1006 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001007 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
1008 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001009 writel(RW_MGR_MRS2_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001010 delay_for_n_mem_clocks(4);
1011 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001012 writel(RW_MGR_MRS3_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001013 delay_for_n_mem_clocks(4);
1014 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001015 writel(RW_MGR_MRS1_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001016 delay_for_n_mem_clocks(4);
1017 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001018 writel(RW_MGR_MRS0_USER_MIRR, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001019 } else {
1020 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001021 writel(RW_MGR_MRS2, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001022 delay_for_n_mem_clocks(4);
1023 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001024 writel(RW_MGR_MRS3, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001025 delay_for_n_mem_clocks(4);
1026 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001027 writel(RW_MGR_MRS1, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001028 delay_for_n_mem_clocks(4);
1029 set_jump_as_return();
Marek Vasut1273dd92015-07-12 21:05:08 +02001030 writel(RW_MGR_MRS0_USER, grpaddr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001031 }
1032 /*
1033 * USER need to wait tMOD (12CK or 15ns) time before issuing
1034 * other commands, but we will have plenty of NIOS cycles before
1035 * actual handoff so its okay.
1036 */
1037 }
1038}
1039
1040/*
1041 * performs a guaranteed read on the patterns we are going to use during a
1042 * read test to ensure memory works
1043 */
1044static uint32_t rw_mgr_mem_calibrate_read_test_patterns(uint32_t rank_bgn,
1045 uint32_t group, uint32_t num_tries, uint32_t *bit_chk,
1046 uint32_t all_ranks)
1047{
1048 uint32_t r, vg;
1049 uint32_t correct_mask_vg;
1050 uint32_t tmp_bit_chk;
1051 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1052 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1053 uint32_t addr;
1054 uint32_t base_rw_mgr;
1055
1056 *bit_chk = param->read_correct_mask;
1057 correct_mask_vg = param->read_correct_mask_vg;
1058
1059 for (r = rank_bgn; r < rank_end; r++) {
1060 if (param->skip_ranks[r])
1061 /* request to skip the rank */
1062 continue;
1063
1064 /* set rank */
1065 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1066
1067 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001068 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1069 writel(RW_MGR_GUARANTEED_READ,
1070 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001071
Marek Vasut1273dd92015-07-12 21:05:08 +02001072 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1073 writel(RW_MGR_GUARANTEED_READ_CONT,
1074 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001075
1076 tmp_bit_chk = 0;
1077 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1078 /* reset the fifos to get pointers to known state */
1079
Marek Vasut1273dd92015-07-12 21:05:08 +02001080 writel(0, &phy_mgr_cmd->fifo_reset);
1081 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1082 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001083
1084 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1085 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1086
Marek Vasutc4815f72015-07-12 19:03:33 +02001087 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001088 writel(RW_MGR_GUARANTEED_READ, addr +
Dinh Nguyen3da42852015-06-02 22:52:49 -05001089 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1090 vg) << 2));
1091
Marek Vasut1273dd92015-07-12 21:05:08 +02001092 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001093 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & (~base_rw_mgr));
1094
1095 if (vg == 0)
1096 break;
1097 }
1098 *bit_chk &= tmp_bit_chk;
1099 }
1100
Marek Vasutc4815f72015-07-12 19:03:33 +02001101 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001102 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001103
1104 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1105 debug_cond(DLEVEL == 1, "%s:%d test_load_patterns(%u,ALL) => (%u == %u) =>\
1106 %lu\n", __func__, __LINE__, group, *bit_chk, param->read_correct_mask,
1107 (long unsigned int)(*bit_chk == param->read_correct_mask));
1108 return *bit_chk == param->read_correct_mask;
1109}
1110
1111static uint32_t rw_mgr_mem_calibrate_read_test_patterns_all_ranks
1112 (uint32_t group, uint32_t num_tries, uint32_t *bit_chk)
1113{
1114 return rw_mgr_mem_calibrate_read_test_patterns(0, group,
1115 num_tries, bit_chk, 1);
1116}
1117
1118/* load up the patterns we are going to use during a read test */
1119static void rw_mgr_mem_calibrate_read_load_patterns(uint32_t rank_bgn,
1120 uint32_t all_ranks)
1121{
1122 uint32_t r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001123 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1124 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1125
1126 debug("%s:%d\n", __func__, __LINE__);
1127 for (r = rank_bgn; r < rank_end; r++) {
1128 if (param->skip_ranks[r])
1129 /* request to skip the rank */
1130 continue;
1131
1132 /* set rank */
1133 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1134
1135 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001136 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001137
Marek Vasut1273dd92015-07-12 21:05:08 +02001138 writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1139 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001140
Marek Vasut1273dd92015-07-12 21:05:08 +02001141 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001142
Marek Vasut1273dd92015-07-12 21:05:08 +02001143 writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1144 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001145
Marek Vasut1273dd92015-07-12 21:05:08 +02001146 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001147
Marek Vasut1273dd92015-07-12 21:05:08 +02001148 writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1149 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001150
Marek Vasut1273dd92015-07-12 21:05:08 +02001151 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001152
Marek Vasut1273dd92015-07-12 21:05:08 +02001153 writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1154 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001155
Marek Vasut1273dd92015-07-12 21:05:08 +02001156 writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1157 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001158 }
1159
1160 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1161}
1162
1163/*
1164 * try a read and see if it returns correct data back. has dummy reads
1165 * inserted into the mix used to align dqs enable. has more thorough checks
1166 * than the regular read test.
1167 */
1168static uint32_t rw_mgr_mem_calibrate_read_test(uint32_t rank_bgn, uint32_t group,
1169 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1170 uint32_t all_groups, uint32_t all_ranks)
1171{
1172 uint32_t r, vg;
1173 uint32_t correct_mask_vg;
1174 uint32_t tmp_bit_chk;
1175 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1176 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1177 uint32_t addr;
1178 uint32_t base_rw_mgr;
1179
1180 *bit_chk = param->read_correct_mask;
1181 correct_mask_vg = param->read_correct_mask_vg;
1182
1183 uint32_t quick_read_mode = (((STATIC_CALIB_STEPS) &
1184 CALIB_SKIP_DELAY_SWEEPS) && ENABLE_SUPER_QUICK_CALIBRATION);
1185
1186 for (r = rank_bgn; r < rank_end; r++) {
1187 if (param->skip_ranks[r])
1188 /* request to skip the rank */
1189 continue;
1190
1191 /* set rank */
1192 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1193
Marek Vasut1273dd92015-07-12 21:05:08 +02001194 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001195
Marek Vasut1273dd92015-07-12 21:05:08 +02001196 writel(RW_MGR_READ_B2B_WAIT1,
1197 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001198
Marek Vasut1273dd92015-07-12 21:05:08 +02001199 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1200 writel(RW_MGR_READ_B2B_WAIT2,
1201 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001202
Dinh Nguyen3da42852015-06-02 22:52:49 -05001203 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001204 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001205 /* need at least two (1+1) reads to capture failures */
1206 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001207 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001208 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001209 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001210
Marek Vasut1273dd92015-07-12 21:05:08 +02001211 writel(RW_MGR_READ_B2B,
1212 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001213 if (all_groups)
1214 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1215 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001216 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001217 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001218 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001219
Marek Vasut1273dd92015-07-12 21:05:08 +02001220 writel(RW_MGR_READ_B2B,
1221 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001222
1223 tmp_bit_chk = 0;
1224 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1225 /* reset the fifos to get pointers to known state */
Marek Vasut1273dd92015-07-12 21:05:08 +02001226 writel(0, &phy_mgr_cmd->fifo_reset);
1227 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1228 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001229
1230 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1231 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1232
Marek Vasutc4815f72015-07-12 19:03:33 +02001233 if (all_groups)
1234 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_ALL_GROUPS_OFFSET;
1235 else
1236 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1237
Marek Vasut17fdc912015-07-12 20:05:54 +02001238 writel(RW_MGR_READ_B2B, addr +
Dinh Nguyen3da42852015-06-02 22:52:49 -05001239 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1240 vg) << 2));
1241
Marek Vasut1273dd92015-07-12 21:05:08 +02001242 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001243 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
1244
1245 if (vg == 0)
1246 break;
1247 }
1248 *bit_chk &= tmp_bit_chk;
1249 }
1250
Marek Vasutc4815f72015-07-12 19:03:33 +02001251 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001252 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001253
1254 if (all_correct) {
1255 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1256 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ALL,%u) =>\
1257 (%u == %u) => %lu", __func__, __LINE__, group,
1258 all_groups, *bit_chk, param->read_correct_mask,
1259 (long unsigned int)(*bit_chk ==
1260 param->read_correct_mask));
1261 return *bit_chk == param->read_correct_mask;
1262 } else {
1263 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1264 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ONE,%u) =>\
1265 (%u != %lu) => %lu\n", __func__, __LINE__,
1266 group, all_groups, *bit_chk, (long unsigned int)0,
1267 (long unsigned int)(*bit_chk != 0x00));
1268 return *bit_chk != 0x00;
1269 }
1270}
1271
1272static uint32_t rw_mgr_mem_calibrate_read_test_all_ranks(uint32_t group,
1273 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1274 uint32_t all_groups)
1275{
1276 return rw_mgr_mem_calibrate_read_test(0, group, num_tries, all_correct,
1277 bit_chk, all_groups, 1);
1278}
1279
1280static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
1281{
Marek Vasut1273dd92015-07-12 21:05:08 +02001282 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001283 (*v)++;
1284}
1285
1286static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
1287{
1288 uint32_t i;
1289
1290 for (i = 0; i < VFIFO_SIZE-1; i++)
1291 rw_mgr_incr_vfifo(grp, v);
1292}
1293
1294static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
1295{
1296 uint32_t v;
1297 uint32_t fail_cnt = 0;
1298 uint32_t test_status;
1299
1300 for (v = 0; v < VFIFO_SIZE; ) {
1301 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
1302 __func__, __LINE__, v);
1303 test_status = rw_mgr_mem_calibrate_read_test_all_ranks
1304 (grp, 1, PASS_ONE_BIT, bit_chk, 0);
1305 if (!test_status) {
1306 fail_cnt++;
1307
1308 if (fail_cnt == 2)
1309 break;
1310 }
1311
1312 /* fiddle with FIFO */
1313 rw_mgr_incr_vfifo(grp, &v);
1314 }
1315
1316 if (v >= VFIFO_SIZE) {
1317 /* no failing read found!! Something must have gone wrong */
1318 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
1319 __func__, __LINE__);
1320 return 0;
1321 } else {
1322 return v;
1323 }
1324}
1325
1326static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
1327 uint32_t dtaps_per_ptap, uint32_t *work_bgn,
1328 uint32_t *v, uint32_t *d, uint32_t *p,
1329 uint32_t *i, uint32_t *max_working_cnt)
1330{
1331 uint32_t found_begin = 0;
1332 uint32_t tmp_delay = 0;
1333 uint32_t test_status;
1334
1335 for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
1336 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1337 *work_bgn = tmp_delay;
1338 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1339
1340 for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
1341 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
1342 IO_DELAY_PER_OPA_TAP) {
1343 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1344
1345 test_status =
1346 rw_mgr_mem_calibrate_read_test_all_ranks
1347 (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
1348
1349 if (test_status) {
1350 *max_working_cnt = 1;
1351 found_begin = 1;
1352 break;
1353 }
1354 }
1355
1356 if (found_begin)
1357 break;
1358
1359 if (*p > IO_DQS_EN_PHASE_MAX)
1360 /* fiddle with FIFO */
1361 rw_mgr_incr_vfifo(*grp, v);
1362 }
1363
1364 if (found_begin)
1365 break;
1366 }
1367
1368 if (*i >= VFIFO_SIZE) {
1369 /* cannot find working solution */
1370 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
1371 ptap/dtap\n", __func__, __LINE__);
1372 return 0;
1373 } else {
1374 return 1;
1375 }
1376}
1377
1378static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
1379 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1380 uint32_t *p, uint32_t *max_working_cnt)
1381{
1382 uint32_t found_begin = 0;
1383 uint32_t tmp_delay;
1384
1385 /* Special case code for backing up a phase */
1386 if (*p == 0) {
1387 *p = IO_DQS_EN_PHASE_MAX;
1388 rw_mgr_decr_vfifo(*grp, v);
1389 } else {
1390 (*p)--;
1391 }
1392 tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
1393 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1394
1395 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
1396 (*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1397 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1398
1399 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1400 PASS_ONE_BIT,
1401 bit_chk, 0)) {
1402 found_begin = 1;
1403 *work_bgn = tmp_delay;
1404 break;
1405 }
1406 }
1407
1408 /* We have found a working dtap before the ptap found above */
1409 if (found_begin == 1)
1410 (*max_working_cnt)++;
1411
1412 /*
1413 * Restore VFIFO to old state before we decremented it
1414 * (if needed).
1415 */
1416 (*p)++;
1417 if (*p > IO_DQS_EN_PHASE_MAX) {
1418 *p = 0;
1419 rw_mgr_incr_vfifo(*grp, v);
1420 }
1421
1422 scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
1423}
1424
1425static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
1426 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1427 uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
1428 uint32_t *work_end)
1429{
1430 uint32_t found_end = 0;
1431
1432 (*p)++;
1433 *work_end += IO_DELAY_PER_OPA_TAP;
1434 if (*p > IO_DQS_EN_PHASE_MAX) {
1435 /* fiddle with FIFO */
1436 *p = 0;
1437 rw_mgr_incr_vfifo(*grp, v);
1438 }
1439
1440 for (; *i < VFIFO_SIZE + 1; (*i)++) {
1441 for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
1442 += IO_DELAY_PER_OPA_TAP) {
1443 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1444
1445 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1446 (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
1447 found_end = 1;
1448 break;
1449 } else {
1450 (*max_working_cnt)++;
1451 }
1452 }
1453
1454 if (found_end)
1455 break;
1456
1457 if (*p > IO_DQS_EN_PHASE_MAX) {
1458 /* fiddle with FIFO */
1459 rw_mgr_incr_vfifo(*grp, v);
1460 *p = 0;
1461 }
1462 }
1463
1464 if (*i >= VFIFO_SIZE + 1) {
1465 /* cannot see edge of failing read */
1466 debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
1467 failed\n", __func__, __LINE__);
1468 return 0;
1469 } else {
1470 return 1;
1471 }
1472}
1473
1474static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
1475 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1476 uint32_t *p, uint32_t *work_mid,
1477 uint32_t *work_end)
1478{
1479 int i;
1480 int tmp_delay = 0;
1481
1482 *work_mid = (*work_bgn + *work_end) / 2;
1483
1484 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
1485 *work_bgn, *work_end, *work_mid);
1486 /* Get the middle delay to be less than a VFIFO delay */
1487 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
1488 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1489 ;
1490 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
1491 while (*work_mid > tmp_delay)
1492 *work_mid -= tmp_delay;
1493 debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
1494
1495 tmp_delay = 0;
1496 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
1497 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1498 ;
1499 tmp_delay -= IO_DELAY_PER_OPA_TAP;
1500 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
1501 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
1502 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
1503 ;
1504 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
1505
1506 scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
1507 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1508
1509 /*
1510 * push vfifo until we can successfully calibrate. We can do this
1511 * because the largest possible margin in 1 VFIFO cycle.
1512 */
1513 for (i = 0; i < VFIFO_SIZE; i++) {
1514 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
1515 *v);
1516 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1517 PASS_ONE_BIT,
1518 bit_chk, 0)) {
1519 break;
1520 }
1521
1522 /* fiddle with FIFO */
1523 rw_mgr_incr_vfifo(*grp, v);
1524 }
1525
1526 if (i >= VFIFO_SIZE) {
1527 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
1528 failed\n", __func__, __LINE__);
1529 return 0;
1530 } else {
1531 return 1;
1532 }
1533}
1534
1535/* find a good dqs enable to use */
1536static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
1537{
1538 uint32_t v, d, p, i;
1539 uint32_t max_working_cnt;
1540 uint32_t bit_chk;
1541 uint32_t dtaps_per_ptap;
1542 uint32_t work_bgn, work_mid, work_end;
1543 uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001544
1545 debug("%s:%d %u\n", __func__, __LINE__, grp);
1546
1547 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1548
1549 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1550 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1551
1552 /* ************************************************************** */
1553 /* * Step 0 : Determine number of delay taps for each phase tap * */
1554 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1555
1556 /* ********************************************************* */
1557 /* * Step 1 : First push vfifo until we get a failing read * */
1558 v = find_vfifo_read(grp, &bit_chk);
1559
1560 max_working_cnt = 0;
1561
1562 /* ******************************************************** */
1563 /* * step 2: find first working phase, increment in ptaps * */
1564 work_bgn = 0;
1565 if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
1566 &p, &i, &max_working_cnt) == 0)
1567 return 0;
1568
1569 work_end = work_bgn;
1570
1571 /*
1572 * If d is 0 then the working window covers a phase tap and
1573 * we can follow the old procedure otherwise, we've found the beginning,
1574 * and we need to increment the dtaps until we find the end.
1575 */
1576 if (d == 0) {
1577 /* ********************************************************* */
1578 /* * step 3a: if we have room, back off by one and
1579 increment in dtaps * */
1580
1581 sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1582 &max_working_cnt);
1583
1584 /* ********************************************************* */
1585 /* * step 4a: go forward from working phase to non working
1586 phase, increment in ptaps * */
1587 if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1588 &i, &max_working_cnt, &work_end) == 0)
1589 return 0;
1590
1591 /* ********************************************************* */
1592 /* * step 5a: back off one from last, increment in dtaps * */
1593
1594 /* Special case code for backing up a phase */
1595 if (p == 0) {
1596 p = IO_DQS_EN_PHASE_MAX;
1597 rw_mgr_decr_vfifo(grp, &v);
1598 } else {
1599 p = p - 1;
1600 }
1601
1602 work_end -= IO_DELAY_PER_OPA_TAP;
1603 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1604
1605 /* * The actual increment of dtaps is done outside of
1606 the if/else loop to share code */
1607 d = 0;
1608
1609 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
1610 vfifo=%u ptap=%u\n", __func__, __LINE__,
1611 v, p);
1612 } else {
1613 /* ******************************************************* */
1614 /* * step 3-5b: Find the right edge of the window using
1615 delay taps * */
1616 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
1617 ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
1618 v, p, d, work_bgn);
1619
1620 work_end = work_bgn;
1621
1622 /* * The actual increment of dtaps is done outside of the
1623 if/else loop to share code */
1624
1625 /* Only here to counterbalance a subtract later on which is
1626 not needed if this branch of the algorithm is taken */
1627 max_working_cnt++;
1628 }
1629
1630 /* The dtap increment to find the failing edge is done here */
1631 for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
1632 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1633 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1634 end-2: dtap=%u\n", __func__, __LINE__, d);
1635 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1636
1637 if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1638 PASS_ONE_BIT,
1639 &bit_chk, 0)) {
1640 break;
1641 }
1642 }
1643
1644 /* Go back to working dtap */
1645 if (d != 0)
1646 work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1647
1648 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
1649 ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
1650 v, p, d-1, work_end);
1651
1652 if (work_end < work_bgn) {
1653 /* nil range */
1654 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
1655 failed\n", __func__, __LINE__);
1656 return 0;
1657 }
1658
1659 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
1660 __func__, __LINE__, work_bgn, work_end);
1661
1662 /* *************************************************************** */
1663 /*
1664 * * We need to calculate the number of dtaps that equal a ptap
1665 * * To do that we'll back up a ptap and re-find the edge of the
1666 * * window using dtaps
1667 */
1668
1669 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
1670 for tracking\n", __func__, __LINE__);
1671
1672 /* Special case code for backing up a phase */
1673 if (p == 0) {
1674 p = IO_DQS_EN_PHASE_MAX;
1675 rw_mgr_decr_vfifo(grp, &v);
1676 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1677 cycle/phase: v=%u p=%u\n", __func__, __LINE__,
1678 v, p);
1679 } else {
1680 p = p - 1;
1681 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1682 phase only: v=%u p=%u", __func__, __LINE__,
1683 v, p);
1684 }
1685
1686 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1687
1688 /*
1689 * Increase dtap until we first see a passing read (in case the
1690 * window is smaller than a ptap),
1691 * and then a failing read to mark the edge of the window again
1692 */
1693
1694 /* Find a passing read */
1695 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
1696 __func__, __LINE__);
1697 found_passing_read = 0;
1698 found_failing_read = 0;
1699 initial_failing_dtap = d;
1700 for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
1701 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
1702 read d=%u\n", __func__, __LINE__, d);
1703 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1704
1705 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1706 PASS_ONE_BIT,
1707 &bit_chk, 0)) {
1708 found_passing_read = 1;
1709 break;
1710 }
1711 }
1712
1713 if (found_passing_read) {
1714 /* Find a failing read */
1715 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
1716 read\n", __func__, __LINE__);
1717 for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
1718 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1719 testing read d=%u\n", __func__, __LINE__, d);
1720 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1721
1722 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1723 (grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
1724 found_failing_read = 1;
1725 break;
1726 }
1727 }
1728 } else {
1729 debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
1730 calculate dtaps", __func__, __LINE__);
1731 debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
1732 }
1733
1734 /*
1735 * The dynamically calculated dtaps_per_ptap is only valid if we
1736 * found a passing/failing read. If we didn't, it means d hit the max
1737 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1738 * statically calculated value.
1739 */
1740 if (found_passing_read && found_failing_read)
1741 dtaps_per_ptap = d - initial_failing_dtap;
1742
Marek Vasut1273dd92015-07-12 21:05:08 +02001743 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001744 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
1745 - %u = %u", __func__, __LINE__, d,
1746 initial_failing_dtap, dtaps_per_ptap);
1747
1748 /* ******************************************** */
1749 /* * step 6: Find the centre of the window * */
1750 if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1751 &work_mid, &work_end) == 0)
1752 return 0;
1753
1754 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
1755 vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
1756 v, p-1, d);
1757 return 1;
1758}
1759
1760/*
1761 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
1762 * dq_in_delay values
1763 */
1764static uint32_t
1765rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
1766(uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
1767{
1768 uint32_t found;
1769 uint32_t i;
1770 uint32_t p;
1771 uint32_t d;
1772 uint32_t r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001773
1774 const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
1775 (RW_MGR_MEM_DQ_PER_READ_DQS-1);
1776 /* we start at zero, so have one less dq to devide among */
1777
1778 debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
1779 test_bgn);
1780
1781 /* try different dq_in_delays since the dq path is shorter than dqs */
1782
1783 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1784 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut32675242015-07-17 06:07:13 +02001785 for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++, d += delay_step) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001786 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
1787 vfifo_find_dqs_", __func__, __LINE__);
1788 debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
1789 write_group, read_group);
1790 debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
Marek Vasut07aee5b2015-07-12 22:07:33 +02001791 scc_mgr_set_dq_in_delay(p, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001792 scc_mgr_load_dq(p);
1793 }
Marek Vasut1273dd92015-07-12 21:05:08 +02001794 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001795 }
1796
1797 found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
1798
1799 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
1800 en_phase_sweep_dq", __func__, __LINE__);
1801 debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
1802 chain to zero\n", write_group, read_group, found);
1803
1804 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1805 r += NUM_RANKS_PER_SHADOW_REG) {
1806 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1807 i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +02001808 scc_mgr_set_dq_in_delay(p, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001809 scc_mgr_load_dq(p);
1810 }
Marek Vasut1273dd92015-07-12 21:05:08 +02001811 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001812 }
1813
1814 return found;
1815}
1816
1817/* per-bit deskew DQ and center */
1818static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
1819 uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
1820 uint32_t use_read_test, uint32_t update_fom)
1821{
1822 uint32_t i, p, d, min_index;
1823 /*
1824 * Store these as signed since there are comparisons with
1825 * signed numbers.
1826 */
1827 uint32_t bit_chk;
1828 uint32_t sticky_bit_chk;
1829 int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1830 int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1831 int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
1832 int32_t mid;
1833 int32_t orig_mid_min, mid_min;
1834 int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
1835 final_dqs_en;
1836 int32_t dq_margin, dqs_margin;
1837 uint32_t stop;
1838 uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
1839 uint32_t addr;
1840
1841 debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
1842
Marek Vasutc4815f72015-07-12 19:03:33 +02001843 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001844 start_dqs = readl(addr + (read_group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001845 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut17fdc912015-07-12 20:05:54 +02001846 start_dqs_en = readl(addr + ((read_group << 2)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001847 - IO_DQS_EN_DELAY_OFFSET));
1848
1849 /* set the left and right edge of each bit to an illegal value */
1850 /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
1851 sticky_bit_chk = 0;
1852 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1853 left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1854 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1855 }
1856
Dinh Nguyen3da42852015-06-02 22:52:49 -05001857 /* Search for the left edge of the window for each bit */
1858 for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
1859 scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
1860
Marek Vasut1273dd92015-07-12 21:05:08 +02001861 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001862
1863 /*
1864 * Stop searching when the read test doesn't pass AND when
1865 * we've seen a passing read on every bit.
1866 */
1867 if (use_read_test) {
1868 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1869 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1870 &bit_chk, 0, 0);
1871 } else {
1872 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1873 0, PASS_ONE_BIT,
1874 &bit_chk, 0);
1875 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1876 (read_group - (write_group *
1877 RW_MGR_MEM_IF_READ_DQS_WIDTH /
1878 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1879 stop = (bit_chk == 0);
1880 }
1881 sticky_bit_chk = sticky_bit_chk | bit_chk;
1882 stop = stop && (sticky_bit_chk == param->read_correct_mask);
1883 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
1884 && %u", __func__, __LINE__, d,
1885 sticky_bit_chk,
1886 param->read_correct_mask, stop);
1887
1888 if (stop == 1) {
1889 break;
1890 } else {
1891 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1892 if (bit_chk & 1) {
1893 /* Remember a passing test as the
1894 left_edge */
1895 left_edge[i] = d;
1896 } else {
1897 /* If a left edge has not been seen yet,
1898 then a future passing test will mark
1899 this edge as the right edge */
1900 if (left_edge[i] ==
1901 IO_IO_IN_DELAY_MAX + 1) {
1902 right_edge[i] = -(d + 1);
1903 }
1904 }
1905 bit_chk = bit_chk >> 1;
1906 }
1907 }
1908 }
1909
1910 /* Reset DQ delay chains to 0 */
Marek Vasut32675242015-07-17 06:07:13 +02001911 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001912 sticky_bit_chk = 0;
1913 for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
1914 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
1915 %d right_edge[%u]: %d\n", __func__, __LINE__,
1916 i, left_edge[i], i, right_edge[i]);
1917
1918 /*
1919 * Check for cases where we haven't found the left edge,
1920 * which makes our assignment of the the right edge invalid.
1921 * Reset it to the illegal value.
1922 */
1923 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
1924 right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1925 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1926 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
1927 right_edge[%u]: %d\n", __func__, __LINE__,
1928 i, right_edge[i]);
1929 }
1930
1931 /*
1932 * Reset sticky bit (except for bits where we have seen
1933 * both the left and right edge).
1934 */
1935 sticky_bit_chk = sticky_bit_chk << 1;
1936 if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
1937 (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1938 sticky_bit_chk = sticky_bit_chk | 1;
1939 }
1940
1941 if (i == 0)
1942 break;
1943 }
1944
Dinh Nguyen3da42852015-06-02 22:52:49 -05001945 /* Search for the right edge of the window for each bit */
1946 for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
1947 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
1948 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
1949 uint32_t delay = d + start_dqs_en;
1950 if (delay > IO_DQS_EN_DELAY_MAX)
1951 delay = IO_DQS_EN_DELAY_MAX;
1952 scc_mgr_set_dqs_en_delay(read_group, delay);
1953 }
1954 scc_mgr_load_dqs(read_group);
1955
Marek Vasut1273dd92015-07-12 21:05:08 +02001956 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001957
1958 /*
1959 * Stop searching when the read test doesn't pass AND when
1960 * we've seen a passing read on every bit.
1961 */
1962 if (use_read_test) {
1963 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1964 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1965 &bit_chk, 0, 0);
1966 } else {
1967 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1968 0, PASS_ONE_BIT,
1969 &bit_chk, 0);
1970 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1971 (read_group - (write_group *
1972 RW_MGR_MEM_IF_READ_DQS_WIDTH /
1973 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1974 stop = (bit_chk == 0);
1975 }
1976 sticky_bit_chk = sticky_bit_chk | bit_chk;
1977 stop = stop && (sticky_bit_chk == param->read_correct_mask);
1978
1979 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
1980 %u && %u", __func__, __LINE__, d,
1981 sticky_bit_chk, param->read_correct_mask, stop);
1982
1983 if (stop == 1) {
1984 break;
1985 } else {
1986 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1987 if (bit_chk & 1) {
1988 /* Remember a passing test as
1989 the right_edge */
1990 right_edge[i] = d;
1991 } else {
1992 if (d != 0) {
1993 /* If a right edge has not been
1994 seen yet, then a future passing
1995 test will mark this edge as the
1996 left edge */
1997 if (right_edge[i] ==
1998 IO_IO_IN_DELAY_MAX + 1) {
1999 left_edge[i] = -(d + 1);
2000 }
2001 } else {
2002 /* d = 0 failed, but it passed
2003 when testing the left edge,
2004 so it must be marginal,
2005 set it to -1 */
2006 if (right_edge[i] ==
2007 IO_IO_IN_DELAY_MAX + 1 &&
2008 left_edge[i] !=
2009 IO_IO_IN_DELAY_MAX
2010 + 1) {
2011 right_edge[i] = -1;
2012 }
2013 /* If a right edge has not been
2014 seen yet, then a future passing
2015 test will mark this edge as the
2016 left edge */
2017 else if (right_edge[i] ==
2018 IO_IO_IN_DELAY_MAX +
2019 1) {
2020 left_edge[i] = -(d + 1);
2021 }
2022 }
2023 }
2024
2025 debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
2026 d=%u]: ", __func__, __LINE__, d);
2027 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
2028 (int)(bit_chk & 1), i, left_edge[i]);
2029 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2030 right_edge[i]);
2031 bit_chk = bit_chk >> 1;
2032 }
2033 }
2034 }
2035
2036 /* Check that all bits have a window */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002037 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2038 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
2039 %d right_edge[%u]: %d", __func__, __LINE__,
2040 i, left_edge[i], i, right_edge[i]);
2041 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
2042 == IO_IO_IN_DELAY_MAX + 1)) {
2043 /*
2044 * Restore delay chain settings before letting the loop
2045 * in rw_mgr_mem_calibrate_vfifo to retry different
2046 * dqs/ck relationships.
2047 */
2048 scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
2049 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2050 scc_mgr_set_dqs_en_delay(read_group,
2051 start_dqs_en);
2052 }
2053 scc_mgr_load_dqs(read_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002054 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002055
2056 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
2057 find edge [%u]: %d %d", __func__, __LINE__,
2058 i, left_edge[i], right_edge[i]);
2059 if (use_read_test) {
2060 set_failing_group_stage(read_group *
2061 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2062 CAL_STAGE_VFIFO,
2063 CAL_SUBSTAGE_VFIFO_CENTER);
2064 } else {
2065 set_failing_group_stage(read_group *
2066 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2067 CAL_STAGE_VFIFO_AFTER_WRITES,
2068 CAL_SUBSTAGE_VFIFO_CENTER);
2069 }
2070 return 0;
2071 }
2072 }
2073
2074 /* Find middle of window for each DQ bit */
2075 mid_min = left_edge[0] - right_edge[0];
2076 min_index = 0;
2077 for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2078 mid = left_edge[i] - right_edge[i];
2079 if (mid < mid_min) {
2080 mid_min = mid;
2081 min_index = i;
2082 }
2083 }
2084
2085 /*
2086 * -mid_min/2 represents the amount that we need to move DQS.
2087 * If mid_min is odd and positive we'll need to add one to
2088 * make sure the rounding in further calculations is correct
2089 * (always bias to the right), so just add 1 for all positive values.
2090 */
2091 if (mid_min > 0)
2092 mid_min++;
2093
2094 mid_min = mid_min / 2;
2095
2096 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
2097 __func__, __LINE__, mid_min, min_index);
2098
2099 /* Determine the amount we can change DQS (which is -mid_min) */
2100 orig_mid_min = mid_min;
2101 new_dqs = start_dqs - mid_min;
2102 if (new_dqs > IO_DQS_IN_DELAY_MAX)
2103 new_dqs = IO_DQS_IN_DELAY_MAX;
2104 else if (new_dqs < 0)
2105 new_dqs = 0;
2106
2107 mid_min = start_dqs - new_dqs;
2108 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2109 mid_min, new_dqs);
2110
2111 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2112 if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2113 mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2114 else if (start_dqs_en - mid_min < 0)
2115 mid_min += start_dqs_en - mid_min;
2116 }
2117 new_dqs = start_dqs - mid_min;
2118
2119 debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
2120 new_dqs=%d mid_min=%d\n", start_dqs,
2121 IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2122 new_dqs, mid_min);
2123
2124 /* Initialize data for export structures */
2125 dqs_margin = IO_IO_IN_DELAY_MAX + 1;
2126 dq_margin = IO_IO_IN_DELAY_MAX + 1;
2127
Dinh Nguyen3da42852015-06-02 22:52:49 -05002128 /* add delay to bring centre of all DQ windows to the same "level" */
2129 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
2130 /* Use values before divide by 2 to reduce round off error */
2131 shift_dq = (left_edge[i] - right_edge[i] -
2132 (left_edge[min_index] - right_edge[min_index]))/2 +
2133 (orig_mid_min - mid_min);
2134
2135 debug_cond(DLEVEL == 2, "vfifo_center: before: \
2136 shift_dq[%u]=%d\n", i, shift_dq);
2137
Marek Vasut1273dd92015-07-12 21:05:08 +02002138 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02002139 temp_dq_in_delay1 = readl(addr + (p << 2));
2140 temp_dq_in_delay2 = readl(addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002141
2142 if (shift_dq + (int32_t)temp_dq_in_delay1 >
2143 (int32_t)IO_IO_IN_DELAY_MAX) {
2144 shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
2145 } else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
2146 shift_dq = -(int32_t)temp_dq_in_delay1;
2147 }
2148 debug_cond(DLEVEL == 2, "vfifo_center: after: \
2149 shift_dq[%u]=%d\n", i, shift_dq);
2150 final_dq[i] = temp_dq_in_delay1 + shift_dq;
Marek Vasut07aee5b2015-07-12 22:07:33 +02002151 scc_mgr_set_dq_in_delay(p, final_dq[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002152 scc_mgr_load_dq(p);
2153
2154 debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
2155 left_edge[i] - shift_dq + (-mid_min),
2156 right_edge[i] + shift_dq - (-mid_min));
2157 /* To determine values for export structures */
2158 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2159 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2160
2161 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2162 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2163 }
2164
2165 final_dqs = new_dqs;
2166 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
2167 final_dqs_en = start_dqs_en - mid_min;
2168
2169 /* Move DQS-en */
2170 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2171 scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
2172 scc_mgr_load_dqs(read_group);
2173 }
2174
2175 /* Move DQS */
2176 scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
2177 scc_mgr_load_dqs(read_group);
2178 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
2179 dqs_margin=%d", __func__, __LINE__,
2180 dq_margin, dqs_margin);
2181
2182 /*
2183 * Do not remove this line as it makes sure all of our decisions
2184 * have been applied. Apply the update bit.
2185 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002186 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002187
2188 return (dq_margin >= 0) && (dqs_margin >= 0);
2189}
2190
2191/*
2192 * calibrate the read valid prediction FIFO.
2193 *
2194 * - read valid prediction will consist of finding a good DQS enable phase,
2195 * DQS enable delay, DQS input phase, and DQS input delay.
2196 * - we also do a per-bit deskew on the DQ lines.
2197 */
2198static uint32_t rw_mgr_mem_calibrate_vfifo(uint32_t read_group,
2199 uint32_t test_bgn)
2200{
2201 uint32_t p, d, rank_bgn, sr;
2202 uint32_t dtaps_per_ptap;
2203 uint32_t tmp_delay;
2204 uint32_t bit_chk;
2205 uint32_t grp_calibrated;
2206 uint32_t write_group, write_test_bgn;
2207 uint32_t failed_substage;
2208
Marek Vasut7ac40d22015-06-26 18:56:54 +02002209 debug("%s:%d: %u %u\n", __func__, __LINE__, read_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002210
2211 /* update info for sims */
2212 reg_file_set_stage(CAL_STAGE_VFIFO);
2213
2214 write_group = read_group;
2215 write_test_bgn = test_bgn;
2216
2217 /* USER Determine number of delay taps for each phase tap */
2218 dtaps_per_ptap = 0;
2219 tmp_delay = 0;
2220 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
2221 dtaps_per_ptap++;
2222 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
2223 }
2224 dtaps_per_ptap--;
2225 tmp_delay = 0;
2226
2227 /* update info for sims */
2228 reg_file_set_group(read_group);
2229
2230 grp_calibrated = 0;
2231
2232 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
2233 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2234
2235 for (d = 0; d <= dtaps_per_ptap && grp_calibrated == 0; d += 2) {
2236 /*
2237 * In RLDRAMX we may be messing the delay of pins in
2238 * the same write group but outside of the current read
2239 * the group, but that's ok because we haven't
2240 * calibrated output side yet.
2241 */
2242 if (d > 0) {
2243 scc_mgr_apply_group_all_out_delay_add_all_ranks
2244 (write_group, write_test_bgn, d);
2245 }
2246
2247 for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX && grp_calibrated == 0;
2248 p++) {
2249 /* set a particular dqdqs phase */
2250 scc_mgr_set_dqdqs_output_phase_all_ranks(read_group, p);
2251
2252 debug_cond(DLEVEL == 1, "%s:%d calibrate_vfifo: g=%u \
2253 p=%u d=%u\n", __func__, __LINE__,
2254 read_group, p, d);
2255
2256 /*
2257 * Load up the patterns used by read calibration
2258 * using current DQDQS phase.
2259 */
2260 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2261 if (!(gbl->phy_debug_mode_flags &
2262 PHY_DEBUG_DISABLE_GUARANTEED_READ)) {
2263 if (!rw_mgr_mem_calibrate_read_test_patterns_all_ranks
2264 (read_group, 1, &bit_chk)) {
2265 debug_cond(DLEVEL == 1, "%s:%d Guaranteed read test failed:",
2266 __func__, __LINE__);
2267 debug_cond(DLEVEL == 1, " g=%u p=%u d=%u\n",
2268 read_group, p, d);
2269 break;
2270 }
2271 }
2272
2273/* case:56390 */
2274 grp_calibrated = 1;
2275 if (rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
2276 (write_group, read_group, test_bgn)) {
2277 /*
2278 * USER Read per-bit deskew can be done on a
2279 * per shadow register basis.
2280 */
2281 for (rank_bgn = 0, sr = 0;
2282 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2283 rank_bgn += NUM_RANKS_PER_SHADOW_REG,
2284 ++sr) {
2285 /*
2286 * Determine if this set of ranks
2287 * should be skipped entirely.
2288 */
2289 if (!param->skip_shadow_regs[sr]) {
2290 /*
2291 * If doing read after write
2292 * calibration, do not update
2293 * FOM, now - do it then.
2294 */
2295 if (!rw_mgr_mem_calibrate_vfifo_center
2296 (rank_bgn, write_group,
2297 read_group, test_bgn, 1, 0)) {
2298 grp_calibrated = 0;
2299 failed_substage =
2300 CAL_SUBSTAGE_VFIFO_CENTER;
2301 }
2302 }
2303 }
2304 } else {
2305 grp_calibrated = 0;
2306 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2307 }
2308 }
2309 }
2310
2311 if (grp_calibrated == 0) {
2312 set_failing_group_stage(write_group, CAL_STAGE_VFIFO,
2313 failed_substage);
2314 return 0;
2315 }
2316
2317 /*
2318 * Reset the delay chains back to zero if they have moved > 1
2319 * (check for > 1 because loop will increase d even when pass in
2320 * first case).
2321 */
2322 if (d > 2)
Marek Vasutd41ea932015-07-20 08:41:04 +02002323 scc_mgr_zero_group(write_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002324
2325 return 1;
2326}
2327
2328/* VFIFO Calibration -- Read Deskew Calibration after write deskew */
2329static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
2330 uint32_t test_bgn)
2331{
2332 uint32_t rank_bgn, sr;
2333 uint32_t grp_calibrated;
2334 uint32_t write_group;
2335
2336 debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
2337
2338 /* update info for sims */
2339
2340 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2341 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2342
2343 write_group = read_group;
2344
2345 /* update info for sims */
2346 reg_file_set_group(read_group);
2347
2348 grp_calibrated = 1;
2349 /* Read per-bit deskew can be done on a per shadow register basis */
2350 for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2351 rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
2352 /* Determine if this set of ranks should be skipped entirely */
2353 if (!param->skip_shadow_regs[sr]) {
2354 /* This is the last calibration round, update FOM here */
2355 if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
2356 write_group,
2357 read_group,
2358 test_bgn, 0,
2359 1)) {
2360 grp_calibrated = 0;
2361 }
2362 }
2363 }
2364
2365
2366 if (grp_calibrated == 0) {
2367 set_failing_group_stage(write_group,
2368 CAL_STAGE_VFIFO_AFTER_WRITES,
2369 CAL_SUBSTAGE_VFIFO_CENTER);
2370 return 0;
2371 }
2372
2373 return 1;
2374}
2375
2376/* Calibrate LFIFO to find smallest read latency */
2377static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2378{
2379 uint32_t found_one;
2380 uint32_t bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002381
2382 debug("%s:%d\n", __func__, __LINE__);
2383
2384 /* update info for sims */
2385 reg_file_set_stage(CAL_STAGE_LFIFO);
2386 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2387
2388 /* Load up the patterns used by read calibration for all ranks */
2389 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2390 found_one = 0;
2391
Dinh Nguyen3da42852015-06-02 22:52:49 -05002392 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002393 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002394 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2395 __func__, __LINE__, gbl->curr_read_lat);
2396
2397 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
2398 NUM_READ_TESTS,
2399 PASS_ALL_BITS,
2400 &bit_chk, 1)) {
2401 break;
2402 }
2403
2404 found_one = 1;
2405 /* reduce read latency and see if things are working */
2406 /* correctly */
2407 gbl->curr_read_lat--;
2408 } while (gbl->curr_read_lat > 0);
2409
2410 /* reset the fifos to get pointers to known state */
2411
Marek Vasut1273dd92015-07-12 21:05:08 +02002412 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002413
2414 if (found_one) {
2415 /* add a fudge factor to the read latency that was determined */
2416 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002417 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002418 debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
2419 read_lat=%u\n", __func__, __LINE__,
2420 gbl->curr_read_lat);
2421 return 1;
2422 } else {
2423 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2424 CAL_SUBSTAGE_READ_LATENCY);
2425
2426 debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
2427 read_lat=%u\n", __func__, __LINE__,
2428 gbl->curr_read_lat);
2429 return 0;
2430 }
2431}
2432
2433/*
2434 * issue write test command.
2435 * two variants are provided. one that just tests a write pattern and
2436 * another that tests datamask functionality.
2437 */
2438static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
2439 uint32_t test_dm)
2440{
2441 uint32_t mcc_instruction;
2442 uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
2443 ENABLE_SUPER_QUICK_CALIBRATION);
2444 uint32_t rw_wl_nop_cycles;
2445 uint32_t addr;
2446
2447 /*
2448 * Set counter and jump addresses for the right
2449 * number of NOP cycles.
2450 * The number of supported NOP cycles can range from -1 to infinity
2451 * Three different cases are handled:
2452 *
2453 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
2454 * mechanism will be used to insert the right number of NOPs
2455 *
2456 * 2. For a number of NOP cycles equals to 0, the micro-instruction
2457 * issuing the write command will jump straight to the
2458 * micro-instruction that turns on DQS (for DDRx), or outputs write
2459 * data (for RLD), skipping
2460 * the NOP micro-instruction all together
2461 *
2462 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
2463 * turned on in the same micro-instruction that issues the write
2464 * command. Then we need
2465 * to directly jump to the micro-instruction that sends out the data
2466 *
2467 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
2468 * (2 and 3). One jump-counter (0) is used to perform multiple
2469 * write-read operations.
2470 * one counter left to issue this command in "multiple-group" mode
2471 */
2472
2473 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
2474
2475 if (rw_wl_nop_cycles == -1) {
2476 /*
2477 * CNTR 2 - We want to execute the special write operation that
2478 * turns on DQS right away and then skip directly to the
2479 * instruction that sends out the data. We set the counter to a
2480 * large number so that the jump is always taken.
2481 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002482 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002483
2484 /* CNTR 3 - Not used */
2485 if (test_dm) {
2486 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002487 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
Marek Vasut1273dd92015-07-12 21:05:08 +02002488 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002489 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
Marek Vasut1273dd92015-07-12 21:05:08 +02002490 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002491 } else {
2492 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
Marek Vasut1273dd92015-07-12 21:05:08 +02002493 writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
2494 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2495 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2496 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002497 }
2498 } else if (rw_wl_nop_cycles == 0) {
2499 /*
2500 * CNTR 2 - We want to skip the NOP operation and go straight
2501 * to the DQS enable instruction. We set the counter to a large
2502 * number so that the jump is always taken.
2503 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002504 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002505
2506 /* CNTR 3 - Not used */
2507 if (test_dm) {
2508 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002509 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
Marek Vasut1273dd92015-07-12 21:05:08 +02002510 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002511 } else {
2512 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
Marek Vasut1273dd92015-07-12 21:05:08 +02002513 writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
2514 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002515 }
2516 } else {
2517 /*
2518 * CNTR 2 - In this case we want to execute the next instruction
2519 * and NOT take the jump. So we set the counter to 0. The jump
2520 * address doesn't count.
2521 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002522 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
2523 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002524
2525 /*
2526 * CNTR 3 - Set the nop counter to the number of cycles we
2527 * need to loop for, minus 1.
2528 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002529 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002530 if (test_dm) {
2531 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
Marek Vasut1273dd92015-07-12 21:05:08 +02002532 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2533 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002534 } else {
2535 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
Marek Vasut1273dd92015-07-12 21:05:08 +02002536 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2537 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002538 }
2539 }
2540
Marek Vasut1273dd92015-07-12 21:05:08 +02002541 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
2542 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002543
Dinh Nguyen3da42852015-06-02 22:52:49 -05002544 if (quick_write_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02002545 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002546 else
Marek Vasut1273dd92015-07-12 21:05:08 +02002547 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002548
Marek Vasut1273dd92015-07-12 21:05:08 +02002549 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002550
2551 /*
2552 * CNTR 1 - This is used to ensure enough time elapses
2553 * for read data to come back.
2554 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002555 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002556
Dinh Nguyen3da42852015-06-02 22:52:49 -05002557 if (test_dm) {
Marek Vasut1273dd92015-07-12 21:05:08 +02002558 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
2559 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002560 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +02002561 writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
2562 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002563 }
2564
Marek Vasutc4815f72015-07-12 19:03:33 +02002565 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02002566 writel(mcc_instruction, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002567}
2568
2569/* Test writes, can check for a single bit pass or multiple bit pass */
2570static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
2571 uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
2572 uint32_t *bit_chk, uint32_t all_ranks)
2573{
Dinh Nguyen3da42852015-06-02 22:52:49 -05002574 uint32_t r;
2575 uint32_t correct_mask_vg;
2576 uint32_t tmp_bit_chk;
2577 uint32_t vg;
2578 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
2579 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
2580 uint32_t addr_rw_mgr;
2581 uint32_t base_rw_mgr;
2582
2583 *bit_chk = param->write_correct_mask;
2584 correct_mask_vg = param->write_correct_mask_vg;
2585
2586 for (r = rank_bgn; r < rank_end; r++) {
2587 if (param->skip_ranks[r]) {
2588 /* request to skip the rank */
2589 continue;
2590 }
2591
2592 /* set rank */
2593 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
2594
2595 tmp_bit_chk = 0;
Marek Vasuta4bfa462015-07-12 17:52:36 +02002596 addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002597 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
2598 /* reset the fifos to get pointers to known state */
Marek Vasut1273dd92015-07-12 21:05:08 +02002599 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002600
2601 tmp_bit_chk = tmp_bit_chk <<
2602 (RW_MGR_MEM_DQ_PER_WRITE_DQS /
2603 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
2604 rw_mgr_mem_calibrate_write_test_issue(write_group *
2605 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
2606 use_dm);
2607
Marek Vasut17fdc912015-07-12 20:05:54 +02002608 base_rw_mgr = readl(addr_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002609 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
2610 if (vg == 0)
2611 break;
2612 }
2613 *bit_chk &= tmp_bit_chk;
2614 }
2615
2616 if (all_correct) {
2617 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2618 debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
2619 %u => %lu", write_group, use_dm,
2620 *bit_chk, param->write_correct_mask,
2621 (long unsigned int)(*bit_chk ==
2622 param->write_correct_mask));
2623 return *bit_chk == param->write_correct_mask;
2624 } else {
2625 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2626 debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
2627 write_group, use_dm, *bit_chk);
2628 debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
2629 (long unsigned int)(*bit_chk != 0));
2630 return *bit_chk != 0x00;
2631 }
2632}
2633
2634/*
2635 * center all windows. do per-bit-deskew to possibly increase size of
2636 * certain windows.
2637 */
2638static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
2639 uint32_t write_group, uint32_t test_bgn)
2640{
2641 uint32_t i, p, min_index;
2642 int32_t d;
2643 /*
2644 * Store these as signed since there are comparisons with
2645 * signed numbers.
2646 */
2647 uint32_t bit_chk;
2648 uint32_t sticky_bit_chk;
2649 int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2650 int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2651 int32_t mid;
2652 int32_t mid_min, orig_mid_min;
2653 int32_t new_dqs, start_dqs, shift_dq;
2654 int32_t dq_margin, dqs_margin, dm_margin;
2655 uint32_t stop;
2656 uint32_t temp_dq_out1_delay;
2657 uint32_t addr;
2658
2659 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2660
2661 dm_margin = 0;
2662
Marek Vasutc4815f72015-07-12 19:03:33 +02002663 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02002664 start_dqs = readl(addr +
Dinh Nguyen3da42852015-06-02 22:52:49 -05002665 (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2666
2667 /* per-bit deskew */
2668
2669 /*
2670 * set the left and right edge of each bit to an illegal value
2671 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
2672 */
2673 sticky_bit_chk = 0;
2674 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2675 left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2676 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2677 }
2678
2679 /* Search for the left edge of the window for each bit */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002680 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
Marek Vasut300c2e62015-07-17 05:42:49 +02002681 scc_mgr_apply_group_dq_out1_delay(write_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002682
Marek Vasut1273dd92015-07-12 21:05:08 +02002683 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002684
2685 /*
2686 * Stop searching when the read test doesn't pass AND when
2687 * we've seen a passing read on every bit.
2688 */
2689 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2690 0, PASS_ONE_BIT, &bit_chk, 0);
2691 sticky_bit_chk = sticky_bit_chk | bit_chk;
2692 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2693 debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
2694 == %u && %u [bit_chk= %u ]\n",
2695 d, sticky_bit_chk, param->write_correct_mask,
2696 stop, bit_chk);
2697
2698 if (stop == 1) {
2699 break;
2700 } else {
2701 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2702 if (bit_chk & 1) {
2703 /*
2704 * Remember a passing test as the
2705 * left_edge.
2706 */
2707 left_edge[i] = d;
2708 } else {
2709 /*
2710 * If a left edge has not been seen
2711 * yet, then a future passing test will
2712 * mark this edge as the right edge.
2713 */
2714 if (left_edge[i] ==
2715 IO_IO_OUT1_DELAY_MAX + 1) {
2716 right_edge[i] = -(d + 1);
2717 }
2718 }
2719 debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
2720 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2721 (int)(bit_chk & 1), i, left_edge[i]);
2722 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2723 right_edge[i]);
2724 bit_chk = bit_chk >> 1;
2725 }
2726 }
2727 }
2728
2729 /* Reset DQ delay chains to 0 */
Marek Vasut32675242015-07-17 06:07:13 +02002730 scc_mgr_apply_group_dq_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002731 sticky_bit_chk = 0;
2732 for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
2733 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2734 %d right_edge[%u]: %d\n", __func__, __LINE__,
2735 i, left_edge[i], i, right_edge[i]);
2736
2737 /*
2738 * Check for cases where we haven't found the left edge,
2739 * which makes our assignment of the the right edge invalid.
2740 * Reset it to the illegal value.
2741 */
2742 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
2743 (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
2744 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2745 debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
2746 right_edge[%u]: %d\n", __func__, __LINE__,
2747 i, right_edge[i]);
2748 }
2749
2750 /*
2751 * Reset sticky bit (except for bits where we have
2752 * seen the left edge).
2753 */
2754 sticky_bit_chk = sticky_bit_chk << 1;
2755 if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
2756 sticky_bit_chk = sticky_bit_chk | 1;
2757
2758 if (i == 0)
2759 break;
2760 }
2761
2762 /* Search for the right edge of the window for each bit */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002763 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
2764 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2765 d + start_dqs);
2766
Marek Vasut1273dd92015-07-12 21:05:08 +02002767 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002768
2769 /*
2770 * Stop searching when the read test doesn't pass AND when
2771 * we've seen a passing read on every bit.
2772 */
2773 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2774 0, PASS_ONE_BIT, &bit_chk, 0);
2775
2776 sticky_bit_chk = sticky_bit_chk | bit_chk;
2777 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2778
2779 debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
2780 %u && %u\n", d, sticky_bit_chk,
2781 param->write_correct_mask, stop);
2782
2783 if (stop == 1) {
2784 if (d == 0) {
2785 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
2786 i++) {
2787 /* d = 0 failed, but it passed when
2788 testing the left edge, so it must be
2789 marginal, set it to -1 */
2790 if (right_edge[i] ==
2791 IO_IO_OUT1_DELAY_MAX + 1 &&
2792 left_edge[i] !=
2793 IO_IO_OUT1_DELAY_MAX + 1) {
2794 right_edge[i] = -1;
2795 }
2796 }
2797 }
2798 break;
2799 } else {
2800 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2801 if (bit_chk & 1) {
2802 /*
2803 * Remember a passing test as
2804 * the right_edge.
2805 */
2806 right_edge[i] = d;
2807 } else {
2808 if (d != 0) {
2809 /*
2810 * If a right edge has not
2811 * been seen yet, then a future
2812 * passing test will mark this
2813 * edge as the left edge.
2814 */
2815 if (right_edge[i] ==
2816 IO_IO_OUT1_DELAY_MAX + 1)
2817 left_edge[i] = -(d + 1);
2818 } else {
2819 /*
2820 * d = 0 failed, but it passed
2821 * when testing the left edge,
2822 * so it must be marginal, set
2823 * it to -1.
2824 */
2825 if (right_edge[i] ==
2826 IO_IO_OUT1_DELAY_MAX + 1 &&
2827 left_edge[i] !=
2828 IO_IO_OUT1_DELAY_MAX + 1)
2829 right_edge[i] = -1;
2830 /*
2831 * If a right edge has not been
2832 * seen yet, then a future
2833 * passing test will mark this
2834 * edge as the left edge.
2835 */
2836 else if (right_edge[i] ==
2837 IO_IO_OUT1_DELAY_MAX +
2838 1)
2839 left_edge[i] = -(d + 1);
2840 }
2841 }
2842 debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
2843 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2844 (int)(bit_chk & 1), i, left_edge[i]);
2845 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2846 right_edge[i]);
2847 bit_chk = bit_chk >> 1;
2848 }
2849 }
2850 }
2851
2852 /* Check that all bits have a window */
2853 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2854 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2855 %d right_edge[%u]: %d", __func__, __LINE__,
2856 i, left_edge[i], i, right_edge[i]);
2857 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
2858 (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
2859 set_failing_group_stage(test_bgn + i,
2860 CAL_STAGE_WRITES,
2861 CAL_SUBSTAGE_WRITES_CENTER);
2862 return 0;
2863 }
2864 }
2865
2866 /* Find middle of window for each DQ bit */
2867 mid_min = left_edge[0] - right_edge[0];
2868 min_index = 0;
2869 for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2870 mid = left_edge[i] - right_edge[i];
2871 if (mid < mid_min) {
2872 mid_min = mid;
2873 min_index = i;
2874 }
2875 }
2876
2877 /*
2878 * -mid_min/2 represents the amount that we need to move DQS.
2879 * If mid_min is odd and positive we'll need to add one to
2880 * make sure the rounding in further calculations is correct
2881 * (always bias to the right), so just add 1 for all positive values.
2882 */
2883 if (mid_min > 0)
2884 mid_min++;
2885 mid_min = mid_min / 2;
2886 debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
2887 __LINE__, mid_min);
2888
2889 /* Determine the amount we can change DQS (which is -mid_min) */
2890 orig_mid_min = mid_min;
2891 new_dqs = start_dqs;
2892 mid_min = 0;
2893 debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
2894 mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
2895 /* Initialize data for export structures */
2896 dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
2897 dq_margin = IO_IO_OUT1_DELAY_MAX + 1;
2898
2899 /* add delay to bring centre of all DQ windows to the same "level" */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002900 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
2901 /* Use values before divide by 2 to reduce round off error */
2902 shift_dq = (left_edge[i] - right_edge[i] -
2903 (left_edge[min_index] - right_edge[min_index]))/2 +
2904 (orig_mid_min - mid_min);
2905
2906 debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
2907 [%u]=%d\n", __func__, __LINE__, i, shift_dq);
2908
Marek Vasut1273dd92015-07-12 21:05:08 +02002909 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02002910 temp_dq_out1_delay = readl(addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002911 if (shift_dq + (int32_t)temp_dq_out1_delay >
2912 (int32_t)IO_IO_OUT1_DELAY_MAX) {
2913 shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
2914 } else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
2915 shift_dq = -(int32_t)temp_dq_out1_delay;
2916 }
2917 debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
2918 i, shift_dq);
Marek Vasut07aee5b2015-07-12 22:07:33 +02002919 scc_mgr_set_dq_out1_delay(i, temp_dq_out1_delay + shift_dq);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002920 scc_mgr_load_dq(i);
2921
2922 debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
2923 left_edge[i] - shift_dq + (-mid_min),
2924 right_edge[i] + shift_dq - (-mid_min));
2925 /* To determine values for export structures */
2926 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2927 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2928
2929 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2930 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2931 }
2932
2933 /* Move DQS */
2934 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02002935 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002936
2937 /* Centre DM */
2938 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
2939
2940 /*
2941 * set the left and right edge of each bit to an illegal value,
2942 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
2943 */
2944 left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2945 right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2946 int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2947 int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2948 int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2949 int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
2950 int32_t win_best = 0;
2951
2952 /* Search for the/part of the window with DM shift */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002953 for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
Marek Vasut32675242015-07-17 06:07:13 +02002954 scc_mgr_apply_group_dm_out1_delay(d);
Marek Vasut1273dd92015-07-12 21:05:08 +02002955 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002956
2957 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2958 PASS_ALL_BITS, &bit_chk,
2959 0)) {
2960 /* USE Set current end of the window */
2961 end_curr = -d;
2962 /*
2963 * If a starting edge of our window has not been seen
2964 * this is our current start of the DM window.
2965 */
2966 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2967 bgn_curr = -d;
2968
2969 /*
2970 * If current window is bigger than best seen.
2971 * Set best seen to be current window.
2972 */
2973 if ((end_curr-bgn_curr+1) > win_best) {
2974 win_best = end_curr-bgn_curr+1;
2975 bgn_best = bgn_curr;
2976 end_best = end_curr;
2977 }
2978 } else {
2979 /* We just saw a failing test. Reset temp edge */
2980 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2981 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2982 }
2983 }
2984
2985
2986 /* Reset DM delay chains to 0 */
Marek Vasut32675242015-07-17 06:07:13 +02002987 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002988
2989 /*
2990 * Check to see if the current window nudges up aganist 0 delay.
2991 * If so we need to continue the search by shifting DQS otherwise DQS
2992 * search begins as a new search. */
2993 if (end_curr != 0) {
2994 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2995 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2996 }
2997
2998 /* Search for the/part of the window with DQS shifts */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002999 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
3000 /*
3001 * Note: This only shifts DQS, so are we limiting ourselve to
3002 * width of DQ unnecessarily.
3003 */
3004 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
3005 d + new_dqs);
3006
Marek Vasut1273dd92015-07-12 21:05:08 +02003007 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003008 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
3009 PASS_ALL_BITS, &bit_chk,
3010 0)) {
3011 /* USE Set current end of the window */
3012 end_curr = d;
3013 /*
3014 * If a beginning edge of our window has not been seen
3015 * this is our current begin of the DM window.
3016 */
3017 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
3018 bgn_curr = d;
3019
3020 /*
3021 * If current window is bigger than best seen. Set best
3022 * seen to be current window.
3023 */
3024 if ((end_curr-bgn_curr+1) > win_best) {
3025 win_best = end_curr-bgn_curr+1;
3026 bgn_best = bgn_curr;
3027 end_best = end_curr;
3028 }
3029 } else {
3030 /* We just saw a failing test. Reset temp edge */
3031 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3032 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3033
3034 /* Early exit optimization: if ther remaining delay
3035 chain space is less than already seen largest window
3036 we can exit */
3037 if ((win_best-1) >
3038 (IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
3039 break;
3040 }
3041 }
3042 }
3043
3044 /* assign left and right edge for cal and reporting; */
3045 left_edge[0] = -1*bgn_best;
3046 right_edge[0] = end_best;
3047
3048 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
3049 __LINE__, left_edge[0], right_edge[0]);
3050
3051 /* Move DQS (back to orig) */
3052 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3053
3054 /* Move DM */
3055
3056 /* Find middle of window for the DM bit */
3057 mid = (left_edge[0] - right_edge[0]) / 2;
3058
3059 /* only move right, since we are not moving DQS/DQ */
3060 if (mid < 0)
3061 mid = 0;
3062
3063 /* dm_marign should fail if we never find a window */
3064 if (win_best == 0)
3065 dm_margin = -1;
3066 else
3067 dm_margin = left_edge[0] - mid;
3068
Marek Vasut32675242015-07-17 06:07:13 +02003069 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003070 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003071
3072 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
3073 dm_margin=%d\n", __func__, __LINE__, left_edge[0],
3074 right_edge[0], mid, dm_margin);
3075 /* Export values */
3076 gbl->fom_out += dq_margin + dqs_margin;
3077
3078 debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
3079 dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
3080 dq_margin, dqs_margin, dm_margin);
3081
3082 /*
3083 * Do not remove this line as it makes sure all of our
3084 * decisions have been applied.
3085 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003086 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003087 return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
3088}
3089
3090/* calibrate the write operations */
3091static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
3092 uint32_t test_bgn)
3093{
3094 /* update info for sims */
3095 debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
3096
3097 reg_file_set_stage(CAL_STAGE_WRITES);
3098 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3099
3100 reg_file_set_group(g);
3101
3102 if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
3103 set_failing_group_stage(g, CAL_STAGE_WRITES,
3104 CAL_SUBSTAGE_WRITES_CENTER);
3105 return 0;
3106 }
3107
3108 return 1;
3109}
3110
3111/* precharge all banks and activate row 0 in bank "000..." and bank "111..." */
3112static void mem_precharge_and_activate(void)
3113{
3114 uint32_t r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003115
3116 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
3117 if (param->skip_ranks[r]) {
3118 /* request to skip the rank */
3119 continue;
3120 }
3121
3122 /* set rank */
3123 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3124
3125 /* precharge all banks ... */
Marek Vasut1273dd92015-07-12 21:05:08 +02003126 writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3127 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003128
Marek Vasut1273dd92015-07-12 21:05:08 +02003129 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3130 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3131 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003132
Marek Vasut1273dd92015-07-12 21:05:08 +02003133 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3134 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3135 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003136
3137 /* activate rows */
Marek Vasut1273dd92015-07-12 21:05:08 +02003138 writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3139 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003140 }
3141}
3142
3143/* Configure various memory related parameters. */
3144static void mem_config(void)
3145{
3146 uint32_t rlat, wlat;
3147 uint32_t rw_wl_nop_cycles;
3148 uint32_t max_latency;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003149
3150 debug("%s:%d\n", __func__, __LINE__);
3151 /* read in write and read latency */
Marek Vasut1273dd92015-07-12 21:05:08 +02003152 wlat = readl(&data_mgr->t_wl_add);
3153 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003154
Dinh Nguyen3da42852015-06-02 22:52:49 -05003155 /* WL for hard phy does not include additive latency */
3156
3157 /*
3158 * add addtional write latency to offset the address/command extra
3159 * clock cycle. We change the AC mux setting causing AC to be delayed
3160 * by one mem clock cycle. Only do this for DDR3
3161 */
3162 wlat = wlat + 1;
3163
Marek Vasut1273dd92015-07-12 21:05:08 +02003164 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003165
3166 rw_wl_nop_cycles = wlat - 2;
3167 gbl->rw_wl_nop_cycles = rw_wl_nop_cycles;
3168
3169 /*
3170 * For AV/CV, lfifo is hardened and always runs at full rate so
3171 * max latency in AFI clocks, used here, is correspondingly smaller.
3172 */
3173 max_latency = (1<<MAX_LATENCY_COUNT_WIDTH)/1 - 1;
3174 /* configure for a burst length of 8 */
3175
3176 /* write latency */
3177 /* Adjust Write Latency for Hard PHY */
3178 wlat = wlat + 1;
3179
3180 /* set a pretty high read latency initially */
3181 gbl->curr_read_lat = rlat + 16;
3182
3183 if (gbl->curr_read_lat > max_latency)
3184 gbl->curr_read_lat = max_latency;
3185
Marek Vasut1273dd92015-07-12 21:05:08 +02003186 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003187
3188 /* advertise write latency */
3189 gbl->curr_write_lat = wlat;
Marek Vasut1273dd92015-07-12 21:05:08 +02003190 writel(wlat - 2, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003191
3192 /* initialize bit slips */
3193 mem_precharge_and_activate();
3194}
3195
3196/* Set VFIFO and LFIFO to instant-on settings in skip calibration mode */
3197static void mem_skip_calibrate(void)
3198{
3199 uint32_t vfifo_offset;
3200 uint32_t i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003201
3202 debug("%s:%d\n", __func__, __LINE__);
3203 /* Need to update every shadow register set used by the interface */
3204 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
3205 r += NUM_RANKS_PER_SHADOW_REG) {
3206 /*
3207 * Set output phase alignment settings appropriate for
3208 * skip calibration.
3209 */
3210 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3211 scc_mgr_set_dqs_en_phase(i, 0);
3212#if IO_DLL_CHAIN_LENGTH == 6
3213 scc_mgr_set_dqdqs_output_phase(i, 6);
3214#else
3215 scc_mgr_set_dqdqs_output_phase(i, 7);
3216#endif
3217 /*
3218 * Case:33398
3219 *
3220 * Write data arrives to the I/O two cycles before write
3221 * latency is reached (720 deg).
3222 * -> due to bit-slip in a/c bus
3223 * -> to allow board skew where dqs is longer than ck
3224 * -> how often can this happen!?
3225 * -> can claim back some ptaps for high freq
3226 * support if we can relax this, but i digress...
3227 *
3228 * The write_clk leads mem_ck by 90 deg
3229 * The minimum ptap of the OPA is 180 deg
3230 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3231 * The write_clk is always delayed by 2 ptaps
3232 *
3233 * Hence, to make DQS aligned to CK, we need to delay
3234 * DQS by:
3235 * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3236 *
3237 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3238 * gives us the number of ptaps, which simplies to:
3239 *
3240 * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3241 */
3242 scc_mgr_set_dqdqs_output_phase(i, (1.25 *
3243 IO_DLL_CHAIN_LENGTH - 2));
3244 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003245 writel(0xff, &sdr_scc_mgr->dqs_ena);
3246 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003247
Dinh Nguyen3da42852015-06-02 22:52:49 -05003248 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003249 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3250 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003251 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003252 writel(0xff, &sdr_scc_mgr->dq_ena);
3253 writel(0xff, &sdr_scc_mgr->dm_ena);
3254 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003255 }
3256
3257 /* Compensate for simulation model behaviour */
3258 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3259 scc_mgr_set_dqs_bus_in_delay(i, 10);
3260 scc_mgr_load_dqs(i);
3261 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003262 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003263
3264 /*
3265 * ArriaV has hard FIFOs that can only be initialized by incrementing
3266 * in sequencer.
3267 */
3268 vfifo_offset = CALIB_VFIFO_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003269 for (j = 0; j < vfifo_offset; j++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003270 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003271 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003272 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003273
3274 /*
3275 * For ACV with hard lfifo, we get the skip-cal setting from
3276 * generation-time constant.
3277 */
3278 gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
Marek Vasut1273dd92015-07-12 21:05:08 +02003279 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003280}
3281
3282/* Memory calibration entry point */
3283static uint32_t mem_calibrate(void)
3284{
3285 uint32_t i;
3286 uint32_t rank_bgn, sr;
3287 uint32_t write_group, write_test_bgn;
3288 uint32_t read_group, read_test_bgn;
3289 uint32_t run_groups, current_run;
3290 uint32_t failing_groups = 0;
3291 uint32_t group_failed = 0;
3292 uint32_t sr_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003293
3294 debug("%s:%d\n", __func__, __LINE__);
3295 /* Initialize the data settings */
3296
3297 gbl->error_substage = CAL_SUBSTAGE_NIL;
3298 gbl->error_stage = CAL_STAGE_NIL;
3299 gbl->error_group = 0xff;
3300 gbl->fom_in = 0;
3301 gbl->fom_out = 0;
3302
3303 mem_config();
3304
Dinh Nguyen3da42852015-06-02 22:52:49 -05003305 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003306 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3307 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003308 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3309 if (i == 0)
3310 scc_mgr_set_hhp_extras();
3311
Marek Vasutc5c5f532015-07-17 02:06:20 +02003312 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003313 }
3314
3315 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3316 /*
3317 * Set VFIFO and LFIFO to instant-on settings in skip
3318 * calibration mode.
3319 */
3320 mem_skip_calibrate();
3321 } else {
3322 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3323 /*
3324 * Zero all delay chain/phase settings for all
3325 * groups and all shadow register sets.
3326 */
3327 scc_mgr_zero_all();
3328
3329 run_groups = ~param->skip_groups;
3330
3331 for (write_group = 0, write_test_bgn = 0; write_group
3332 < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3333 write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
3334 /* Initialized the group failure */
3335 group_failed = 0;
3336
3337 current_run = run_groups & ((1 <<
3338 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3339 run_groups = run_groups >>
3340 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
3341
3342 if (current_run == 0)
3343 continue;
3344
Marek Vasut1273dd92015-07-12 21:05:08 +02003345 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3346 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutd41ea932015-07-20 08:41:04 +02003347 scc_mgr_zero_group(write_group, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003348
3349 for (read_group = write_group *
3350 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3351 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3352 read_test_bgn = 0;
3353 read_group < (write_group + 1) *
3354 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3355 RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3356 group_failed == 0;
3357 read_group++, read_test_bgn +=
3358 RW_MGR_MEM_DQ_PER_READ_DQS) {
3359 /* Calibrate the VFIFO */
3360 if (!((STATIC_CALIB_STEPS) &
3361 CALIB_SKIP_VFIFO)) {
3362 if (!rw_mgr_mem_calibrate_vfifo
3363 (read_group,
3364 read_test_bgn)) {
3365 group_failed = 1;
3366
3367 if (!(gbl->
3368 phy_debug_mode_flags &
3369 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3370 return 0;
3371 }
3372 }
3373 }
3374 }
3375
3376 /* Calibrate the output side */
3377 if (group_failed == 0) {
3378 for (rank_bgn = 0, sr = 0; rank_bgn
3379 < RW_MGR_MEM_NUMBER_OF_RANKS;
3380 rank_bgn +=
3381 NUM_RANKS_PER_SHADOW_REG,
3382 ++sr) {
3383 sr_failed = 0;
3384 if (!((STATIC_CALIB_STEPS) &
3385 CALIB_SKIP_WRITES)) {
3386 if ((STATIC_CALIB_STEPS)
3387 & CALIB_SKIP_DELAY_SWEEPS) {
3388 /* not needed in quick mode! */
3389 } else {
3390 /*
3391 * Determine if this set of
3392 * ranks should be skipped
3393 * entirely.
3394 */
3395 if (!param->skip_shadow_regs[sr]) {
3396 if (!rw_mgr_mem_calibrate_writes
3397 (rank_bgn, write_group,
3398 write_test_bgn)) {
3399 sr_failed = 1;
3400 if (!(gbl->
3401 phy_debug_mode_flags &
3402 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3403 return 0;
3404 }
3405 }
3406 }
3407 }
3408 }
3409 if (sr_failed != 0)
3410 group_failed = 1;
3411 }
3412 }
3413
3414 if (group_failed == 0) {
3415 for (read_group = write_group *
3416 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3417 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3418 read_test_bgn = 0;
3419 read_group < (write_group + 1)
3420 * RW_MGR_MEM_IF_READ_DQS_WIDTH
3421 / RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3422 group_failed == 0;
3423 read_group++, read_test_bgn +=
3424 RW_MGR_MEM_DQ_PER_READ_DQS) {
3425 if (!((STATIC_CALIB_STEPS) &
3426 CALIB_SKIP_WRITES)) {
3427 if (!rw_mgr_mem_calibrate_vfifo_end
3428 (read_group, read_test_bgn)) {
3429 group_failed = 1;
3430
3431 if (!(gbl->phy_debug_mode_flags
3432 & PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3433 return 0;
3434 }
3435 }
3436 }
3437 }
3438 }
3439
3440 if (group_failed != 0)
3441 failing_groups++;
3442 }
3443
3444 /*
3445 * USER If there are any failing groups then report
3446 * the failure.
3447 */
3448 if (failing_groups != 0)
3449 return 0;
3450
3451 /* Calibrate the LFIFO */
3452 if (!((STATIC_CALIB_STEPS) & CALIB_SKIP_LFIFO)) {
3453 /*
3454 * If we're skipping groups as part of debug,
3455 * don't calibrate LFIFO.
3456 */
3457 if (param->skip_groups == 0) {
3458 if (!rw_mgr_mem_calibrate_lfifo())
3459 return 0;
3460 }
3461 }
3462 }
3463 }
3464
3465 /*
3466 * Do not remove this line as it makes sure all of our decisions
3467 * have been applied.
3468 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003469 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003470 return 1;
3471}
3472
3473static uint32_t run_mem_calibrate(void)
3474{
3475 uint32_t pass;
3476 uint32_t debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003477
3478 debug("%s:%d\n", __func__, __LINE__);
3479
3480 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003481 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003482
Dinh Nguyen3da42852015-06-02 22:52:49 -05003483 /* stop tracking manger */
Marek Vasut6cb9f162015-07-12 20:49:39 +02003484 uint32_t ctrlcfg = readl(&sdr_ctrl->ctrl_cfg);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003485
Marek Vasut6cb9f162015-07-12 20:49:39 +02003486 writel(ctrlcfg & 0xFFBFFFFF, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003487
3488 initialize();
3489 rw_mgr_mem_initialize();
3490
3491 pass = mem_calibrate();
3492
3493 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003494 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003495
3496 /*
3497 * Handoff:
3498 * Don't return control of the PHY back to AFI when in debug mode.
3499 */
3500 if ((gbl->phy_debug_mode_flags & PHY_DEBUG_IN_DEBUG_MODE) == 0) {
3501 rw_mgr_mem_handoff();
3502 /*
3503 * In Hard PHY this is a 2-bit control:
3504 * 0: AFI Mux Select
3505 * 1: DDIO Mux Select
3506 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003507 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003508 }
3509
Marek Vasut6cb9f162015-07-12 20:49:39 +02003510 writel(ctrlcfg, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003511
3512 if (pass) {
3513 printf("%s: CALIBRATION PASSED\n", __FILE__);
3514
3515 gbl->fom_in /= 2;
3516 gbl->fom_out /= 2;
3517
3518 if (gbl->fom_in > 0xff)
3519 gbl->fom_in = 0xff;
3520
3521 if (gbl->fom_out > 0xff)
3522 gbl->fom_out = 0xff;
3523
3524 /* Update the FOM in the register file */
3525 debug_info = gbl->fom_in;
3526 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003527 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003528
Marek Vasut1273dd92015-07-12 21:05:08 +02003529 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3530 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003531 } else {
3532 printf("%s: CALIBRATION FAILED\n", __FILE__);
3533
3534 debug_info = gbl->error_stage;
3535 debug_info |= gbl->error_substage << 8;
3536 debug_info |= gbl->error_group << 16;
3537
Marek Vasut1273dd92015-07-12 21:05:08 +02003538 writel(debug_info, &sdr_reg_file->failing_stage);
3539 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3540 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003541
3542 /* Update the failing group/stage in the register file */
3543 debug_info = gbl->error_stage;
3544 debug_info |= gbl->error_substage << 8;
3545 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003546 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003547 }
3548
3549 return pass;
3550}
3551
Marek Vasutbb064342015-07-19 06:12:42 +02003552/**
3553 * hc_initialize_rom_data() - Initialize ROM data
3554 *
3555 * Initialize ROM data.
3556 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003557static void hc_initialize_rom_data(void)
3558{
Marek Vasutbb064342015-07-19 06:12:42 +02003559 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003560
Marek Vasutc4815f72015-07-12 19:03:33 +02003561 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003562 for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3563 writel(inst_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003564
Marek Vasutc4815f72015-07-12 19:03:33 +02003565 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003566 for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3567 writel(ac_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003568}
3569
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003570/**
3571 * initialize_reg_file() - Initialize SDR register file
3572 *
3573 * Initialize SDR register file.
3574 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003575static void initialize_reg_file(void)
3576{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003577 /* Initialize the register file with the correct data */
Marek Vasut1273dd92015-07-12 21:05:08 +02003578 writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3579 writel(0, &sdr_reg_file->debug_data_addr);
3580 writel(0, &sdr_reg_file->cur_stage);
3581 writel(0, &sdr_reg_file->fom);
3582 writel(0, &sdr_reg_file->failing_stage);
3583 writel(0, &sdr_reg_file->debug1);
3584 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003585}
3586
Marek Vasut2ca151f2015-07-19 06:14:04 +02003587/**
3588 * initialize_hps_phy() - Initialize HPS PHY
3589 *
3590 * Initialize HPS PHY.
3591 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003592static void initialize_hps_phy(void)
3593{
3594 uint32_t reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003595 /*
3596 * Tracking also gets configured here because it's in the
3597 * same register.
3598 */
3599 uint32_t trk_sample_count = 7500;
3600 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3601 /*
3602 * Format is number of outer loops in the 16 MSB, sample
3603 * count in 16 LSB.
3604 */
3605
3606 reg = 0;
3607 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3608 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3609 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3610 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3611 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3612 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3613 /*
3614 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3615 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3616 */
3617 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3618 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3619 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003620 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003621
3622 reg = 0;
3623 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3624 trk_sample_count >>
3625 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3626 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3627 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003628 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003629
3630 reg = 0;
3631 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3632 trk_long_idle_sample_count >>
3633 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003634 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003635}
3636
3637static void initialize_tracking(void)
3638{
3639 uint32_t concatenated_longidle = 0x0;
3640 uint32_t concatenated_delays = 0x0;
3641 uint32_t concatenated_rw_addr = 0x0;
3642 uint32_t concatenated_refresh = 0x0;
3643 uint32_t trk_sample_count = 7500;
3644 uint32_t dtaps_per_ptap;
3645 uint32_t tmp_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003646
3647 /*
3648 * compute usable version of value in case we skip full
3649 * computation later
3650 */
3651 dtaps_per_ptap = 0;
3652 tmp_delay = 0;
3653 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
3654 dtaps_per_ptap++;
3655 tmp_delay += IO_DELAY_PER_DCHAIN_TAP;
3656 }
3657 dtaps_per_ptap--;
3658
3659 concatenated_longidle = concatenated_longidle ^ 10;
3660 /*longidle outer loop */
3661 concatenated_longidle = concatenated_longidle << 16;
3662 concatenated_longidle = concatenated_longidle ^ 100;
3663 /*longidle sample count */
3664 concatenated_delays = concatenated_delays ^ 243;
3665 /* trfc, worst case of 933Mhz 4Gb */
3666 concatenated_delays = concatenated_delays << 8;
3667 concatenated_delays = concatenated_delays ^ 14;
3668 /* trcd, worst case */
3669 concatenated_delays = concatenated_delays << 8;
3670 concatenated_delays = concatenated_delays ^ 10;
3671 /* vfifo wait */
3672 concatenated_delays = concatenated_delays << 8;
3673 concatenated_delays = concatenated_delays ^ 4;
3674 /* mux delay */
3675
3676 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_IDLE;
3677 concatenated_rw_addr = concatenated_rw_addr << 8;
3678 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_ACTIVATE_1;
3679 concatenated_rw_addr = concatenated_rw_addr << 8;
3680 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_SGLE_READ;
3681 concatenated_rw_addr = concatenated_rw_addr << 8;
3682 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_PRECHARGE_ALL;
3683
3684 concatenated_refresh = concatenated_refresh ^ RW_MGR_REFRESH_ALL;
3685 concatenated_refresh = concatenated_refresh << 24;
3686 concatenated_refresh = concatenated_refresh ^ 1000; /* trefi */
3687
3688 /* Initialize the register file with the correct data */
Marek Vasut1273dd92015-07-12 21:05:08 +02003689 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
3690 writel(trk_sample_count, &sdr_reg_file->trk_sample_count);
3691 writel(concatenated_longidle, &sdr_reg_file->trk_longidle);
3692 writel(concatenated_delays, &sdr_reg_file->delays);
3693 writel(concatenated_rw_addr, &sdr_reg_file->trk_rw_mgr_addr);
3694 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH, &sdr_reg_file->trk_read_dqs_width);
3695 writel(concatenated_refresh, &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003696}
3697
3698int sdram_calibration_full(void)
3699{
3700 struct param_type my_param;
3701 struct gbl_type my_gbl;
3702 uint32_t pass;
3703 uint32_t i;
3704
3705 param = &my_param;
3706 gbl = &my_gbl;
3707
3708 /* Initialize the debug mode flags */
3709 gbl->phy_debug_mode_flags = 0;
3710 /* Set the calibration enabled by default */
3711 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3712 /*
3713 * Only sweep all groups (regardless of fail state) by default
3714 * Set enabled read test by default.
3715 */
3716#if DISABLE_GUARANTEED_READ
3717 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3718#endif
3719 /* Initialize the register file */
3720 initialize_reg_file();
3721
3722 /* Initialize any PHY CSR */
3723 initialize_hps_phy();
3724
3725 scc_mgr_initialize();
3726
3727 initialize_tracking();
3728
3729 /* USER Enable all ranks, groups */
3730 for (i = 0; i < RW_MGR_MEM_NUMBER_OF_RANKS; i++)
3731 param->skip_ranks[i] = 0;
3732 for (i = 0; i < NUM_SHADOW_REGS; ++i)
3733 param->skip_shadow_regs[i] = 0;
3734 param->skip_groups = 0;
3735
3736 printf("%s: Preparing to start memory calibration\n", __FILE__);
3737
3738 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut23f62b32015-07-13 01:05:27 +02003739 debug_cond(DLEVEL == 1,
3740 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3741 RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3742 RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3743 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3744 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3745 debug_cond(DLEVEL == 1,
3746 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3747 RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3748 RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3749 IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3750 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3751 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3752 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3753 IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3754 IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3755 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3756 IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3757 IO_IO_OUT2_DELAY_MAX);
3758 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3759 IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003760
3761 hc_initialize_rom_data();
3762
3763 /* update info for sims */
3764 reg_file_set_stage(CAL_STAGE_NIL);
3765 reg_file_set_group(0);
3766
3767 /*
3768 * Load global needed for those actions that require
3769 * some dynamic calibration support.
3770 */
3771 dyn_calib_steps = STATIC_CALIB_STEPS;
3772 /*
3773 * Load global to allow dynamic selection of delay loop settings
3774 * based on calibration mode.
3775 */
3776 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3777 skip_delay_mask = 0xff;
3778 else
3779 skip_delay_mask = 0x0;
3780
3781 pass = run_mem_calibrate();
3782
3783 printf("%s: Calibration complete\n", __FILE__);
3784 return pass;
3785}