blob: 11213c6eecce5ef945e509d3e8eceb968c8c38b6 [file] [log] [blame]
Dinh Nguyen135cc7f2015-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 Nguyen135cc7f2015-06-02 22:52:49 -050016static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut0dcb9e82015-07-12 18:46:52 +020017 (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050018
19static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut0dcb9e82015-07-12 18:46:52 +020020 (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050021
22static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasut341ceec2015-07-12 18:31:05 +020023 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050024
25static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasut81df0a22015-07-12 18:42:34 +020026 (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050027
28static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasutc3b9b0f2015-07-12 18:54:37 +020029 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050030
31static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasutc3b9b0f2015-07-12 18:54:37 +020032 (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050033
34static struct socfpga_data_mgr *data_mgr =
Marek Vasuta3340102015-07-12 19:03:33 +020035 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050036
Marek Vasutcd5d38e2015-07-12 20:49:39 +020037static struct socfpga_sdr_ctrl *sdr_ctrl =
38 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
39
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050040#define DELTA_D 1
Dinh Nguyen135cc7f2015-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 Nguyen135cc7f2015-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 Vasut6eeb7472015-07-12 21:10:24 +0200100static void reg_file_set_group(u16 set_group)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500101{
Marek Vasut6eeb7472015-07-12 21:10:24 +0200102 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500103}
104
Marek Vasut6eeb7472015-07-12 21:10:24 +0200105static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500106{
Marek Vasut6eeb7472015-07-12 21:10:24 +0200107 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500108}
109
Marek Vasut6eeb7472015-07-12 21:10:24 +0200110static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500111{
Marek Vasut6eeb7472015-07-12 21:10:24 +0200112 set_sub_stage &= 0xff;
113 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500114}
115
116static void initialize(void)
117{
Dinh Nguyen135cc7f2015-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 Vasutb5450962015-07-12 21:05:08 +0200125 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500126
127 /* USER memory clock is not stable we begin initialization */
Marek Vasutb5450962015-07-12 21:05:08 +0200128 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500129
130 /* USER calibration status all set to zero */
Marek Vasutb5450962015-07-12 21:05:08 +0200131 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500132
Marek Vasutb5450962015-07-12 21:05:08 +0200133 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen135cc7f2015-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 Nguyen135cc7f2015-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 Vasutf84348d2015-07-18 02:23:29 +0200193 } else {
Dinh Nguyen135cc7f2015-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 Vasutb5450962015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500251}
252
Marek Vasut303a3dc2015-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)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500262{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200263 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
264}
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500265
Marek Vasut8957b492015-07-20 07:16:42 +0200266/**
267 * scc_mgr_initialize() - Initialize SCC Manager registers
268 *
269 * Initialize SCC Manager registers.
270 */
Marek Vasut303a3dc2015-07-12 22:28:33 +0200271static void scc_mgr_initialize(void)
272{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500273 /*
Marek Vasut8957b492015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500278 */
Marek Vasut303a3dc2015-07-12 22:28:33 +0200279 int i;
Marek Vasut8957b492015-07-20 07:16:42 +0200280
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500281 for (i = 0; i < 16; i++) {
Marek Vasut0eacf7e2015-06-26 18:56:54 +0200282 debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500283 __func__, __LINE__, i);
Marek Vasut303a3dc2015-07-12 22:28:33 +0200284 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, 0, i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500285 }
286}
287
Marek Vasut7481b692015-07-12 22:11:55 +0200288static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
289{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200290 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut7481b692015-07-12 22:11:55 +0200291}
292
293static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500294{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200295 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500296}
297
Marek Vasut7481b692015-07-12 22:11:55 +0200298static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
299{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200300 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Marek Vasut7481b692015-07-12 22:11:55 +0200301}
302
303static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
304{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200305 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200306}
307
Marek Vasut122e1f32015-07-17 06:07:13 +0200308static void scc_mgr_set_dqs_io_in_delay(uint32_t delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500309{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200310 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
311 delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500312}
313
Marek Vasut7481b692015-07-12 22:11:55 +0200314static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500315{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200316 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut7481b692015-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 Vasut303a3dc2015-07-12 22:28:33 +0200321 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200322}
323
Marek Vasut122e1f32015-07-17 06:07:13 +0200324static void scc_mgr_set_dqs_out1_delay(uint32_t delay)
Marek Vasut7481b692015-07-12 22:11:55 +0200325{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200326 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
327 delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500328}
329
Marek Vasut7481b692015-07-12 22:11:55 +0200330static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
331{
Marek Vasut303a3dc2015-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 Vasut7481b692015-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 Vasut1d3cde32015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500373{
Marek Vasut1d3cde32015-07-12 23:25:21 +0200374 u32 r;
Dinh Nguyen135cc7f2015-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 Vasut1d3cde32015-07-12 23:25:21 +0200378 scc_mgr_set(off, grp, val);
Marek Vasut49722822015-07-12 23:14:33 +0200379
Marek Vasut1d3cde32015-07-12 23:25:21 +0200380 if (update || (r == 0)) {
381 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasutb5450962015-07-12 21:05:08 +0200382 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500383 }
384 }
385}
386
Marek Vasut1d3cde32015-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 Nguyen135cc7f2015-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 Vasut1d3cde32015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500414}
415
Dinh Nguyen135cc7f2015-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 Nguyen135cc7f2015-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 Vasut1d3cde32015-07-12 23:25:21 +0200427 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
428 read_group, delay, 1);
Marek Vasutb5450962015-07-12 21:05:08 +0200429 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500430}
431
Marek Vasute62f6912015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500440{
Marek Vasute62f6912015-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 Nguyen135cc7f2015-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 Vasute62f6912015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500454}
455
Marek Vasut3b8e5b02015-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 Nguyen135cc7f2015-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 Vasut3b8e5b02015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500471 */
Marek Vasut3b8e5b02015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500477
Marek Vasut3b8e5b02015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500483}
484
Marek Vasut08bcb982015-07-20 04:41:53 +0200485/**
486 * scc_mgr_zero_all() - Zero all DQS config
487 *
488 * Zero all DQS config.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500489 */
490static void scc_mgr_zero_all(void)
491{
Marek Vasut08bcb982015-07-20 04:41:53 +0200492 int i, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500493
494 /*
495 * USER Zero all DQS config settings, across all groups and all
496 * shadow registers
497 */
Marek Vasut08bcb982015-07-20 04:41:53 +0200498 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
499 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen135cc7f2015-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 Vasut08bcb982015-07-20 04:41:53 +0200513 /* Arria V/Cyclone V don't have out2. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500514 scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
515 }
516 }
517
Marek Vasut08bcb982015-07-20 04:41:53 +0200518 /* Multicast to all DQS group enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200519 writel(0xff, &sdr_scc_mgr->dqs_ena);
520 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500521}
522
Marek Vasut0341de42015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500530{
Marek Vasut0341de42015-07-17 02:06:20 +0200531 /* Multicast to all DQ enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200532 writel(0xff, &sdr_scc_mgr->dq_ena);
533 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500534
Marek Vasut0341de42015-07-17 02:06:20 +0200535 /* Update current DQS IO enable. */
Marek Vasutb5450962015-07-12 21:05:08 +0200536 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500537
Marek Vasut0341de42015-07-17 02:06:20 +0200538 /* Update the DQS logic. */
Marek Vasutb5450962015-07-12 21:05:08 +0200539 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500540
Marek Vasut0341de42015-07-17 02:06:20 +0200541 /* Hit update. */
Marek Vasutb5450962015-07-12 21:05:08 +0200542 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500543}
544
Marek Vasut5a4379e2015-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 Vasut7481b692015-07-12 22:11:55 +0200552{
Marek Vasut5a4379e2015-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 Vasut7481b692015-07-12 22:11:55 +0200557 /*
Marek Vasut5a4379e2015-07-13 00:30:09 +0200558 * Load the setting in the SCC manager
Marek Vasut7481b692015-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 Vasut5a4379e2015-07-13 00:30:09 +0200562 * the setting must be set multiple times.
Marek Vasut7481b692015-07-12 22:11:55 +0200563 */
Marek Vasut5a4379e2015-07-13 00:30:09 +0200564 for (i = 0; i < ratio; i++)
565 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut7481b692015-07-12 22:11:55 +0200566}
567
Marek Vasut62d3c692015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500574{
Marek Vasut62d3c692015-07-20 08:41:04 +0200575 int i, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500576
Marek Vasut62d3c692015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500580 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
Marek Vasutcab80792015-07-12 22:07:33 +0200581 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500582 if (!out_only)
Marek Vasutcab80792015-07-12 22:07:33 +0200583 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500584 }
585
Marek Vasut62d3c692015-07-20 08:41:04 +0200586 /* Multicast to all DQ enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200587 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500588
Marek Vasut62d3c692015-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 Vasutcab80792015-07-12 22:07:33 +0200591 scc_mgr_set_dm_out1_delay(i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500592
Marek Vasut62d3c692015-07-20 08:41:04 +0200593 /* Multicast to all DM enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200594 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500595
Marek Vasut62d3c692015-07-20 08:41:04 +0200596 /* Zero all DQS IO settings. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500597 if (!out_only)
Marek Vasut122e1f32015-07-17 06:07:13 +0200598 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasut62d3c692015-07-20 08:41:04 +0200599
600 /* Arria V/Cyclone V don't have out2. */
Marek Vasut122e1f32015-07-17 06:07:13 +0200601 scc_mgr_set_dqs_out1_delay(IO_DQS_OUT_RESERVE);
Dinh Nguyen135cc7f2015-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 Vasut62d3c692015-07-20 08:41:04 +0200605 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasutb5450962015-07-12 21:05:08 +0200606 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500607
Marek Vasut62d3c692015-07-20 08:41:04 +0200608 /* Hit update to zero everything. */
Marek Vasutb5450962015-07-12 21:05:08 +0200609 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500610 }
611}
612
Dinh Nguyen135cc7f2015-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 Vasut122e1f32015-07-17 06:07:13 +0200617static void scc_mgr_apply_group_dq_in_delay(uint32_t group_bgn, uint32_t delay)
Dinh Nguyen135cc7f2015-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 Vasutcab80792015-07-12 22:07:33 +0200622 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500623 scc_mgr_load_dq(p);
624 }
625}
626
Marek Vasutcd649502015-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 Nguyen135cc7f2015-06-02 22:52:49 -0500634{
Marek Vasutcd649502015-07-17 05:42:49 +0200635 int i;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500636
Marek Vasutcd649502015-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 Nguyen135cc7f2015-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 Vasut122e1f32015-07-17 06:07:13 +0200644static void scc_mgr_apply_group_dm_out1_delay(uint32_t delay1)
Dinh Nguyen135cc7f2015-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 Vasutcab80792015-07-12 22:07:33 +0200649 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen135cc7f2015-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 Vasut122e1f32015-07-17 06:07:13 +0200659 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen135cc7f2015-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
666/* apply a delay to the entire output side: DQ, DM, DQS, OCT */
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200667static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
668 const u32 group_bgn,
669 const u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500670{
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200671 u32 i, new_delay;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500672
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200673 /* DQ shift */
674 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500675 scc_mgr_load_dq(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500676
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200677 /* DM shift */
678 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500679 scc_mgr_load_dm(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500680
681 /* dqs shift */
682 new_delay = READ_SCC_DQS_IO_OUT2_DELAY;
683 new_delay += delay;
684
685 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
686 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
687 " adding %u to OUT1\n", __func__, __LINE__,
688 write_group, group_bgn, delay, new_delay,
689 IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
690 new_delay - IO_IO_OUT2_DELAY_MAX);
Marek Vasut122e1f32015-07-17 06:07:13 +0200691 scc_mgr_set_dqs_out1_delay(new_delay -
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500692 IO_IO_OUT2_DELAY_MAX);
693 new_delay = IO_IO_OUT2_DELAY_MAX;
694 }
695
696 scc_mgr_load_dqs_io();
697
698 /* oct shift */
699 new_delay = READ_SCC_OCT_OUT2_DELAY;
700 new_delay += delay;
701
702 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
703 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
704 " adding %u to OUT1\n", __func__, __LINE__,
705 write_group, group_bgn, delay, new_delay,
706 IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
707 new_delay - IO_IO_OUT2_DELAY_MAX);
708 scc_mgr_set_oct_out1_delay(write_group, new_delay -
709 IO_IO_OUT2_DELAY_MAX);
710 new_delay = IO_IO_OUT2_DELAY_MAX;
711 }
712
713 scc_mgr_load_dqs_for_write_group(write_group);
714}
715
716/*
717 * USER apply a delay to the entire output side (DQ, DM, DQS, OCT)
718 * and to all ranks
719 */
720static void scc_mgr_apply_group_all_out_delay_add_all_ranks(
721 uint32_t write_group, uint32_t group_bgn, uint32_t delay)
722{
723 uint32_t r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500724
725 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
726 r += NUM_RANKS_PER_SHADOW_REG) {
727 scc_mgr_apply_group_all_out_delay_add(write_group,
728 group_bgn, delay);
Marek Vasutb5450962015-07-12 21:05:08 +0200729 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500730 }
731}
732
733/* optimization used to recover some slots in ddr3 inst_rom */
734/* could be applied to other protocols if we wanted to */
735static void set_jump_as_return(void)
736{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500737 /*
738 * to save space, we replace return with jump to special shared
739 * RETURN instruction so we set the counter to large value so that
740 * we always jump
741 */
Marek Vasutb5450962015-07-12 21:05:08 +0200742 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
743 writel(RW_MGR_RETURN, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500744}
745
746/*
747 * should always use constants as argument to ensure all computations are
748 * performed at compile time
749 */
750static void delay_for_n_mem_clocks(const uint32_t clocks)
751{
752 uint32_t afi_clocks;
753 uint8_t inner = 0;
754 uint8_t outer = 0;
755 uint16_t c_loop = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500756
757 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
758
759
760 afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
761 /* scale (rounding up) to get afi clocks */
762
763 /*
764 * Note, we don't bother accounting for being off a little bit
765 * because of a few extra instructions in outer loops
766 * Note, the loops have a test at the end, and do the test before
767 * the decrement, and so always perform the loop
768 * 1 time more than the counter value
769 */
770 if (afi_clocks == 0) {
771 ;
772 } else if (afi_clocks <= 0x100) {
773 inner = afi_clocks-1;
774 outer = 0;
775 c_loop = 0;
776 } else if (afi_clocks <= 0x10000) {
777 inner = 0xff;
778 outer = (afi_clocks-1) >> 8;
779 c_loop = 0;
780 } else {
781 inner = 0xff;
782 outer = 0xff;
783 c_loop = (afi_clocks-1) >> 16;
784 }
785
786 /*
787 * rom instructions are structured as follows:
788 *
789 * IDLE_LOOP2: jnz cntr0, TARGET_A
790 * IDLE_LOOP1: jnz cntr1, TARGET_B
791 * return
792 *
793 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
794 * TARGET_B is set to IDLE_LOOP2 as well
795 *
796 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
797 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
798 *
799 * a little confusing, but it helps save precious space in the inst_rom
800 * and sequencer rom and keeps the delays more accurate and reduces
801 * overhead
802 */
803 if (afi_clocks <= 0x100) {
Marek Vasutb5450962015-07-12 21:05:08 +0200804 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
805 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500806
Marek Vasutb5450962015-07-12 21:05:08 +0200807 writel(RW_MGR_IDLE_LOOP1,
808 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500809
Marek Vasutb5450962015-07-12 21:05:08 +0200810 writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
811 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500812 } else {
Marek Vasutb5450962015-07-12 21:05:08 +0200813 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
814 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500815
Marek Vasutb5450962015-07-12 21:05:08 +0200816 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
817 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500818
Marek Vasutb5450962015-07-12 21:05:08 +0200819 writel(RW_MGR_IDLE_LOOP2,
820 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500821
Marek Vasutb5450962015-07-12 21:05:08 +0200822 writel(RW_MGR_IDLE_LOOP2,
823 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500824
825 /* hack to get around compiler not being smart enough */
826 if (afi_clocks <= 0x10000) {
827 /* only need to run once */
Marek Vasutb5450962015-07-12 21:05:08 +0200828 writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
829 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500830 } else {
831 do {
Marek Vasutb5450962015-07-12 21:05:08 +0200832 writel(RW_MGR_IDLE_LOOP2,
833 SDR_PHYGRP_RWMGRGRP_ADDRESS |
834 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500835 } while (c_loop-- != 0);
836 }
837 }
838 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
839}
840
841static void rw_mgr_mem_initialize(void)
842{
843 uint32_t r;
Marek Vasutb5450962015-07-12 21:05:08 +0200844 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
845 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500846
847 debug("%s:%d\n", __func__, __LINE__);
848
849 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasutb5450962015-07-12 21:05:08 +0200850 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
851 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500852
853 /*
854 * Here's how you load register for a loop
855 * Counters are located @ 0x800
856 * Jump address are located @ 0xC00
857 * For both, registers 0 to 3 are selected using bits 3 and 2, like
858 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
859 * I know this ain't pretty, but Avalon bus throws away the 2 least
860 * significant bits
861 */
862
863 /* start with memory RESET activated */
864
865 /* tINIT = 200us */
866
867 /*
868 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
869 * If a and b are the number of iteration in 2 nested loops
870 * it takes the following number of cycles to complete the operation:
871 * number_of_cycles = ((2 + n) * a + 2) * b
872 * where n is the number of instruction in the inner loop
873 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
874 * b = 6A
875 */
876
877 /* Load counters */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500878 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR0_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200879 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500880 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR1_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200881 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500882 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR2_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200883 &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500884
885 /* Load jump address */
Marek Vasutb5450962015-07-12 21:05:08 +0200886 writel(RW_MGR_INIT_RESET_0_CKE_0,
887 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
888 writel(RW_MGR_INIT_RESET_0_CKE_0,
889 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
890 writel(RW_MGR_INIT_RESET_0_CKE_0,
891 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500892
893 /* Execute count instruction */
Marek Vasutb5450962015-07-12 21:05:08 +0200894 writel(RW_MGR_INIT_RESET_0_CKE_0, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500895
896 /* indicate that memory is stable */
Marek Vasutb5450962015-07-12 21:05:08 +0200897 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500898
899 /*
900 * transition the RESET to high
901 * Wait for 500us
902 */
903
904 /*
905 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
906 * If a and b are the number of iteration in 2 nested loops
907 * it takes the following number of cycles to complete the operation
908 * number_of_cycles = ((2 + n) * a + 2) * b
909 * where n is the number of instruction in the inner loop
910 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
911 * b = FF
912 */
913
914 /* Load counters */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500915 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR0_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200916 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500917 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR1_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200918 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500919 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR2_VAL),
Marek Vasutb5450962015-07-12 21:05:08 +0200920 &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500921
922 /* Load jump address */
Marek Vasutb5450962015-07-12 21:05:08 +0200923 writel(RW_MGR_INIT_RESET_1_CKE_0,
924 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
925 writel(RW_MGR_INIT_RESET_1_CKE_0,
926 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
927 writel(RW_MGR_INIT_RESET_1_CKE_0,
928 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500929
Marek Vasutb5450962015-07-12 21:05:08 +0200930 writel(RW_MGR_INIT_RESET_1_CKE_0, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500931
932 /* bring up clock enable */
933
934 /* tXRP < 250 ck cycles */
935 delay_for_n_mem_clocks(250);
936
937 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
938 if (param->skip_ranks[r]) {
939 /* request to skip the rank */
940 continue;
941 }
942
943 /* set rank */
944 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
945
946 /*
947 * USER Use Mirror-ed commands for odd ranks if address
948 * mirrorring is on
949 */
950 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
951 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200952 writel(RW_MGR_MRS2_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500953 delay_for_n_mem_clocks(4);
954 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200955 writel(RW_MGR_MRS3_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500956 delay_for_n_mem_clocks(4);
957 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200958 writel(RW_MGR_MRS1_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500959 delay_for_n_mem_clocks(4);
960 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200961 writel(RW_MGR_MRS0_DLL_RESET_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500962 } else {
963 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200964 writel(RW_MGR_MRS2, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500965 delay_for_n_mem_clocks(4);
966 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200967 writel(RW_MGR_MRS3, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500968 delay_for_n_mem_clocks(4);
969 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200970 writel(RW_MGR_MRS1, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500971 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200972 writel(RW_MGR_MRS0_DLL_RESET, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500973 }
974 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +0200975 writel(RW_MGR_ZQCL, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500976
977 /* tZQinit = tDLLK = 512 ck cycles */
978 delay_for_n_mem_clocks(512);
979 }
980}
981
982/*
983 * At the end of calibration we have to program the user settings in, and
984 * USER hand off the memory to the user.
985 */
986static void rw_mgr_mem_handoff(void)
987{
988 uint32_t r;
Marek Vasutb5450962015-07-12 21:05:08 +0200989 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
990 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500991
992 debug("%s:%d\n", __func__, __LINE__);
993 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
994 if (param->skip_ranks[r])
995 /* request to skip the rank */
996 continue;
997 /* set rank */
998 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
999
1000 /* precharge all banks ... */
Marek Vasutb5450962015-07-12 21:05:08 +02001001 writel(RW_MGR_PRECHARGE_ALL, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001002
1003 /* load up MR settings specified by user */
1004
1005 /*
1006 * Use Mirror-ed commands for odd ranks if address
1007 * mirrorring is on
1008 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001009 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
1010 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001011 writel(RW_MGR_MRS2_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001012 delay_for_n_mem_clocks(4);
1013 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001014 writel(RW_MGR_MRS3_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001015 delay_for_n_mem_clocks(4);
1016 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001017 writel(RW_MGR_MRS1_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001018 delay_for_n_mem_clocks(4);
1019 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001020 writel(RW_MGR_MRS0_USER_MIRR, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001021 } else {
1022 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001023 writel(RW_MGR_MRS2, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001024 delay_for_n_mem_clocks(4);
1025 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001026 writel(RW_MGR_MRS3, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001027 delay_for_n_mem_clocks(4);
1028 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001029 writel(RW_MGR_MRS1, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001030 delay_for_n_mem_clocks(4);
1031 set_jump_as_return();
Marek Vasutb5450962015-07-12 21:05:08 +02001032 writel(RW_MGR_MRS0_USER, grpaddr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001033 }
1034 /*
1035 * USER need to wait tMOD (12CK or 15ns) time before issuing
1036 * other commands, but we will have plenty of NIOS cycles before
1037 * actual handoff so its okay.
1038 */
1039 }
1040}
1041
1042/*
1043 * performs a guaranteed read on the patterns we are going to use during a
1044 * read test to ensure memory works
1045 */
1046static uint32_t rw_mgr_mem_calibrate_read_test_patterns(uint32_t rank_bgn,
1047 uint32_t group, uint32_t num_tries, uint32_t *bit_chk,
1048 uint32_t all_ranks)
1049{
1050 uint32_t r, vg;
1051 uint32_t correct_mask_vg;
1052 uint32_t tmp_bit_chk;
1053 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1054 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1055 uint32_t addr;
1056 uint32_t base_rw_mgr;
1057
1058 *bit_chk = param->read_correct_mask;
1059 correct_mask_vg = param->read_correct_mask_vg;
1060
1061 for (r = rank_bgn; r < rank_end; r++) {
1062 if (param->skip_ranks[r])
1063 /* request to skip the rank */
1064 continue;
1065
1066 /* set rank */
1067 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1068
1069 /* Load up a constant bursts of read commands */
Marek Vasutb5450962015-07-12 21:05:08 +02001070 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1071 writel(RW_MGR_GUARANTEED_READ,
1072 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001073
Marek Vasutb5450962015-07-12 21:05:08 +02001074 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1075 writel(RW_MGR_GUARANTEED_READ_CONT,
1076 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001077
1078 tmp_bit_chk = 0;
1079 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1080 /* reset the fifos to get pointers to known state */
1081
Marek Vasutb5450962015-07-12 21:05:08 +02001082 writel(0, &phy_mgr_cmd->fifo_reset);
1083 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1084 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001085
1086 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1087 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1088
Marek Vasuta3340102015-07-12 19:03:33 +02001089 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02001090 writel(RW_MGR_GUARANTEED_READ, addr +
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001091 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1092 vg) << 2));
1093
Marek Vasutb5450962015-07-12 21:05:08 +02001094 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001095 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & (~base_rw_mgr));
1096
1097 if (vg == 0)
1098 break;
1099 }
1100 *bit_chk &= tmp_bit_chk;
1101 }
1102
Marek Vasuta3340102015-07-12 19:03:33 +02001103 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02001104 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001105
1106 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1107 debug_cond(DLEVEL == 1, "%s:%d test_load_patterns(%u,ALL) => (%u == %u) =>\
1108 %lu\n", __func__, __LINE__, group, *bit_chk, param->read_correct_mask,
1109 (long unsigned int)(*bit_chk == param->read_correct_mask));
1110 return *bit_chk == param->read_correct_mask;
1111}
1112
1113static uint32_t rw_mgr_mem_calibrate_read_test_patterns_all_ranks
1114 (uint32_t group, uint32_t num_tries, uint32_t *bit_chk)
1115{
1116 return rw_mgr_mem_calibrate_read_test_patterns(0, group,
1117 num_tries, bit_chk, 1);
1118}
1119
1120/* load up the patterns we are going to use during a read test */
1121static void rw_mgr_mem_calibrate_read_load_patterns(uint32_t rank_bgn,
1122 uint32_t all_ranks)
1123{
1124 uint32_t r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001125 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1126 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1127
1128 debug("%s:%d\n", __func__, __LINE__);
1129 for (r = rank_bgn; r < rank_end; r++) {
1130 if (param->skip_ranks[r])
1131 /* request to skip the rank */
1132 continue;
1133
1134 /* set rank */
1135 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1136
1137 /* Load up a constant bursts */
Marek Vasutb5450962015-07-12 21:05:08 +02001138 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001139
Marek Vasutb5450962015-07-12 21:05:08 +02001140 writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1141 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001142
Marek Vasutb5450962015-07-12 21:05:08 +02001143 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001144
Marek Vasutb5450962015-07-12 21:05:08 +02001145 writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1146 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001147
Marek Vasutb5450962015-07-12 21:05:08 +02001148 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001149
Marek Vasutb5450962015-07-12 21:05:08 +02001150 writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1151 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001152
Marek Vasutb5450962015-07-12 21:05:08 +02001153 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001154
Marek Vasutb5450962015-07-12 21:05:08 +02001155 writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1156 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001157
Marek Vasutb5450962015-07-12 21:05:08 +02001158 writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1159 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001160 }
1161
1162 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1163}
1164
1165/*
1166 * try a read and see if it returns correct data back. has dummy reads
1167 * inserted into the mix used to align dqs enable. has more thorough checks
1168 * than the regular read test.
1169 */
1170static uint32_t rw_mgr_mem_calibrate_read_test(uint32_t rank_bgn, uint32_t group,
1171 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1172 uint32_t all_groups, uint32_t all_ranks)
1173{
1174 uint32_t r, vg;
1175 uint32_t correct_mask_vg;
1176 uint32_t tmp_bit_chk;
1177 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1178 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1179 uint32_t addr;
1180 uint32_t base_rw_mgr;
1181
1182 *bit_chk = param->read_correct_mask;
1183 correct_mask_vg = param->read_correct_mask_vg;
1184
1185 uint32_t quick_read_mode = (((STATIC_CALIB_STEPS) &
1186 CALIB_SKIP_DELAY_SWEEPS) && ENABLE_SUPER_QUICK_CALIBRATION);
1187
1188 for (r = rank_bgn; r < rank_end; r++) {
1189 if (param->skip_ranks[r])
1190 /* request to skip the rank */
1191 continue;
1192
1193 /* set rank */
1194 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1195
Marek Vasutb5450962015-07-12 21:05:08 +02001196 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001197
Marek Vasutb5450962015-07-12 21:05:08 +02001198 writel(RW_MGR_READ_B2B_WAIT1,
1199 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001200
Marek Vasutb5450962015-07-12 21:05:08 +02001201 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1202 writel(RW_MGR_READ_B2B_WAIT2,
1203 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001204
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001205 if (quick_read_mode)
Marek Vasutb5450962015-07-12 21:05:08 +02001206 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001207 /* need at least two (1+1) reads to capture failures */
1208 else if (all_groups)
Marek Vasutb5450962015-07-12 21:05:08 +02001209 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001210 else
Marek Vasutb5450962015-07-12 21:05:08 +02001211 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001212
Marek Vasutb5450962015-07-12 21:05:08 +02001213 writel(RW_MGR_READ_B2B,
1214 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001215 if (all_groups)
1216 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1217 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
Marek Vasutb5450962015-07-12 21:05:08 +02001218 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001219 else
Marek Vasutb5450962015-07-12 21:05:08 +02001220 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001221
Marek Vasutb5450962015-07-12 21:05:08 +02001222 writel(RW_MGR_READ_B2B,
1223 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001224
1225 tmp_bit_chk = 0;
1226 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1227 /* reset the fifos to get pointers to known state */
Marek Vasutb5450962015-07-12 21:05:08 +02001228 writel(0, &phy_mgr_cmd->fifo_reset);
1229 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1230 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001231
1232 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1233 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1234
Marek Vasuta3340102015-07-12 19:03:33 +02001235 if (all_groups)
1236 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_ALL_GROUPS_OFFSET;
1237 else
1238 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1239
Marek Vasut33acf0f2015-07-12 20:05:54 +02001240 writel(RW_MGR_READ_B2B, addr +
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001241 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1242 vg) << 2));
1243
Marek Vasutb5450962015-07-12 21:05:08 +02001244 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001245 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
1246
1247 if (vg == 0)
1248 break;
1249 }
1250 *bit_chk &= tmp_bit_chk;
1251 }
1252
Marek Vasuta3340102015-07-12 19:03:33 +02001253 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02001254 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001255
1256 if (all_correct) {
1257 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1258 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ALL,%u) =>\
1259 (%u == %u) => %lu", __func__, __LINE__, group,
1260 all_groups, *bit_chk, param->read_correct_mask,
1261 (long unsigned int)(*bit_chk ==
1262 param->read_correct_mask));
1263 return *bit_chk == param->read_correct_mask;
1264 } else {
1265 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1266 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ONE,%u) =>\
1267 (%u != %lu) => %lu\n", __func__, __LINE__,
1268 group, all_groups, *bit_chk, (long unsigned int)0,
1269 (long unsigned int)(*bit_chk != 0x00));
1270 return *bit_chk != 0x00;
1271 }
1272}
1273
1274static uint32_t rw_mgr_mem_calibrate_read_test_all_ranks(uint32_t group,
1275 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1276 uint32_t all_groups)
1277{
1278 return rw_mgr_mem_calibrate_read_test(0, group, num_tries, all_correct,
1279 bit_chk, all_groups, 1);
1280}
1281
1282static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
1283{
Marek Vasutb5450962015-07-12 21:05:08 +02001284 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001285 (*v)++;
1286}
1287
1288static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
1289{
1290 uint32_t i;
1291
1292 for (i = 0; i < VFIFO_SIZE-1; i++)
1293 rw_mgr_incr_vfifo(grp, v);
1294}
1295
1296static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
1297{
1298 uint32_t v;
1299 uint32_t fail_cnt = 0;
1300 uint32_t test_status;
1301
1302 for (v = 0; v < VFIFO_SIZE; ) {
1303 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
1304 __func__, __LINE__, v);
1305 test_status = rw_mgr_mem_calibrate_read_test_all_ranks
1306 (grp, 1, PASS_ONE_BIT, bit_chk, 0);
1307 if (!test_status) {
1308 fail_cnt++;
1309
1310 if (fail_cnt == 2)
1311 break;
1312 }
1313
1314 /* fiddle with FIFO */
1315 rw_mgr_incr_vfifo(grp, &v);
1316 }
1317
1318 if (v >= VFIFO_SIZE) {
1319 /* no failing read found!! Something must have gone wrong */
1320 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
1321 __func__, __LINE__);
1322 return 0;
1323 } else {
1324 return v;
1325 }
1326}
1327
1328static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
1329 uint32_t dtaps_per_ptap, uint32_t *work_bgn,
1330 uint32_t *v, uint32_t *d, uint32_t *p,
1331 uint32_t *i, uint32_t *max_working_cnt)
1332{
1333 uint32_t found_begin = 0;
1334 uint32_t tmp_delay = 0;
1335 uint32_t test_status;
1336
1337 for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
1338 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1339 *work_bgn = tmp_delay;
1340 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1341
1342 for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
1343 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
1344 IO_DELAY_PER_OPA_TAP) {
1345 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1346
1347 test_status =
1348 rw_mgr_mem_calibrate_read_test_all_ranks
1349 (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
1350
1351 if (test_status) {
1352 *max_working_cnt = 1;
1353 found_begin = 1;
1354 break;
1355 }
1356 }
1357
1358 if (found_begin)
1359 break;
1360
1361 if (*p > IO_DQS_EN_PHASE_MAX)
1362 /* fiddle with FIFO */
1363 rw_mgr_incr_vfifo(*grp, v);
1364 }
1365
1366 if (found_begin)
1367 break;
1368 }
1369
1370 if (*i >= VFIFO_SIZE) {
1371 /* cannot find working solution */
1372 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
1373 ptap/dtap\n", __func__, __LINE__);
1374 return 0;
1375 } else {
1376 return 1;
1377 }
1378}
1379
1380static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
1381 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1382 uint32_t *p, uint32_t *max_working_cnt)
1383{
1384 uint32_t found_begin = 0;
1385 uint32_t tmp_delay;
1386
1387 /* Special case code for backing up a phase */
1388 if (*p == 0) {
1389 *p = IO_DQS_EN_PHASE_MAX;
1390 rw_mgr_decr_vfifo(*grp, v);
1391 } else {
1392 (*p)--;
1393 }
1394 tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
1395 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1396
1397 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
1398 (*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1399 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1400
1401 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1402 PASS_ONE_BIT,
1403 bit_chk, 0)) {
1404 found_begin = 1;
1405 *work_bgn = tmp_delay;
1406 break;
1407 }
1408 }
1409
1410 /* We have found a working dtap before the ptap found above */
1411 if (found_begin == 1)
1412 (*max_working_cnt)++;
1413
1414 /*
1415 * Restore VFIFO to old state before we decremented it
1416 * (if needed).
1417 */
1418 (*p)++;
1419 if (*p > IO_DQS_EN_PHASE_MAX) {
1420 *p = 0;
1421 rw_mgr_incr_vfifo(*grp, v);
1422 }
1423
1424 scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
1425}
1426
1427static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
1428 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1429 uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
1430 uint32_t *work_end)
1431{
1432 uint32_t found_end = 0;
1433
1434 (*p)++;
1435 *work_end += IO_DELAY_PER_OPA_TAP;
1436 if (*p > IO_DQS_EN_PHASE_MAX) {
1437 /* fiddle with FIFO */
1438 *p = 0;
1439 rw_mgr_incr_vfifo(*grp, v);
1440 }
1441
1442 for (; *i < VFIFO_SIZE + 1; (*i)++) {
1443 for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
1444 += IO_DELAY_PER_OPA_TAP) {
1445 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1446
1447 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1448 (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
1449 found_end = 1;
1450 break;
1451 } else {
1452 (*max_working_cnt)++;
1453 }
1454 }
1455
1456 if (found_end)
1457 break;
1458
1459 if (*p > IO_DQS_EN_PHASE_MAX) {
1460 /* fiddle with FIFO */
1461 rw_mgr_incr_vfifo(*grp, v);
1462 *p = 0;
1463 }
1464 }
1465
1466 if (*i >= VFIFO_SIZE + 1) {
1467 /* cannot see edge of failing read */
1468 debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
1469 failed\n", __func__, __LINE__);
1470 return 0;
1471 } else {
1472 return 1;
1473 }
1474}
1475
1476static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
1477 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1478 uint32_t *p, uint32_t *work_mid,
1479 uint32_t *work_end)
1480{
1481 int i;
1482 int tmp_delay = 0;
1483
1484 *work_mid = (*work_bgn + *work_end) / 2;
1485
1486 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
1487 *work_bgn, *work_end, *work_mid);
1488 /* Get the middle delay to be less than a VFIFO delay */
1489 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
1490 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1491 ;
1492 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
1493 while (*work_mid > tmp_delay)
1494 *work_mid -= tmp_delay;
1495 debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
1496
1497 tmp_delay = 0;
1498 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
1499 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1500 ;
1501 tmp_delay -= IO_DELAY_PER_OPA_TAP;
1502 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
1503 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
1504 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
1505 ;
1506 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
1507
1508 scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
1509 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1510
1511 /*
1512 * push vfifo until we can successfully calibrate. We can do this
1513 * because the largest possible margin in 1 VFIFO cycle.
1514 */
1515 for (i = 0; i < VFIFO_SIZE; i++) {
1516 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
1517 *v);
1518 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1519 PASS_ONE_BIT,
1520 bit_chk, 0)) {
1521 break;
1522 }
1523
1524 /* fiddle with FIFO */
1525 rw_mgr_incr_vfifo(*grp, v);
1526 }
1527
1528 if (i >= VFIFO_SIZE) {
1529 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
1530 failed\n", __func__, __LINE__);
1531 return 0;
1532 } else {
1533 return 1;
1534 }
1535}
1536
1537/* find a good dqs enable to use */
1538static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
1539{
1540 uint32_t v, d, p, i;
1541 uint32_t max_working_cnt;
1542 uint32_t bit_chk;
1543 uint32_t dtaps_per_ptap;
1544 uint32_t work_bgn, work_mid, work_end;
1545 uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001546
1547 debug("%s:%d %u\n", __func__, __LINE__, grp);
1548
1549 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1550
1551 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1552 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1553
1554 /* ************************************************************** */
1555 /* * Step 0 : Determine number of delay taps for each phase tap * */
1556 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1557
1558 /* ********************************************************* */
1559 /* * Step 1 : First push vfifo until we get a failing read * */
1560 v = find_vfifo_read(grp, &bit_chk);
1561
1562 max_working_cnt = 0;
1563
1564 /* ******************************************************** */
1565 /* * step 2: find first working phase, increment in ptaps * */
1566 work_bgn = 0;
1567 if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
1568 &p, &i, &max_working_cnt) == 0)
1569 return 0;
1570
1571 work_end = work_bgn;
1572
1573 /*
1574 * If d is 0 then the working window covers a phase tap and
1575 * we can follow the old procedure otherwise, we've found the beginning,
1576 * and we need to increment the dtaps until we find the end.
1577 */
1578 if (d == 0) {
1579 /* ********************************************************* */
1580 /* * step 3a: if we have room, back off by one and
1581 increment in dtaps * */
1582
1583 sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1584 &max_working_cnt);
1585
1586 /* ********************************************************* */
1587 /* * step 4a: go forward from working phase to non working
1588 phase, increment in ptaps * */
1589 if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1590 &i, &max_working_cnt, &work_end) == 0)
1591 return 0;
1592
1593 /* ********************************************************* */
1594 /* * step 5a: back off one from last, increment in dtaps * */
1595
1596 /* Special case code for backing up a phase */
1597 if (p == 0) {
1598 p = IO_DQS_EN_PHASE_MAX;
1599 rw_mgr_decr_vfifo(grp, &v);
1600 } else {
1601 p = p - 1;
1602 }
1603
1604 work_end -= IO_DELAY_PER_OPA_TAP;
1605 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1606
1607 /* * The actual increment of dtaps is done outside of
1608 the if/else loop to share code */
1609 d = 0;
1610
1611 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
1612 vfifo=%u ptap=%u\n", __func__, __LINE__,
1613 v, p);
1614 } else {
1615 /* ******************************************************* */
1616 /* * step 3-5b: Find the right edge of the window using
1617 delay taps * */
1618 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
1619 ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
1620 v, p, d, work_bgn);
1621
1622 work_end = work_bgn;
1623
1624 /* * The actual increment of dtaps is done outside of the
1625 if/else loop to share code */
1626
1627 /* Only here to counterbalance a subtract later on which is
1628 not needed if this branch of the algorithm is taken */
1629 max_working_cnt++;
1630 }
1631
1632 /* The dtap increment to find the failing edge is done here */
1633 for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
1634 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1635 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1636 end-2: dtap=%u\n", __func__, __LINE__, d);
1637 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1638
1639 if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1640 PASS_ONE_BIT,
1641 &bit_chk, 0)) {
1642 break;
1643 }
1644 }
1645
1646 /* Go back to working dtap */
1647 if (d != 0)
1648 work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1649
1650 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
1651 ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
1652 v, p, d-1, work_end);
1653
1654 if (work_end < work_bgn) {
1655 /* nil range */
1656 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
1657 failed\n", __func__, __LINE__);
1658 return 0;
1659 }
1660
1661 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
1662 __func__, __LINE__, work_bgn, work_end);
1663
1664 /* *************************************************************** */
1665 /*
1666 * * We need to calculate the number of dtaps that equal a ptap
1667 * * To do that we'll back up a ptap and re-find the edge of the
1668 * * window using dtaps
1669 */
1670
1671 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
1672 for tracking\n", __func__, __LINE__);
1673
1674 /* Special case code for backing up a phase */
1675 if (p == 0) {
1676 p = IO_DQS_EN_PHASE_MAX;
1677 rw_mgr_decr_vfifo(grp, &v);
1678 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1679 cycle/phase: v=%u p=%u\n", __func__, __LINE__,
1680 v, p);
1681 } else {
1682 p = p - 1;
1683 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1684 phase only: v=%u p=%u", __func__, __LINE__,
1685 v, p);
1686 }
1687
1688 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1689
1690 /*
1691 * Increase dtap until we first see a passing read (in case the
1692 * window is smaller than a ptap),
1693 * and then a failing read to mark the edge of the window again
1694 */
1695
1696 /* Find a passing read */
1697 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
1698 __func__, __LINE__);
1699 found_passing_read = 0;
1700 found_failing_read = 0;
1701 initial_failing_dtap = d;
1702 for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
1703 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
1704 read d=%u\n", __func__, __LINE__, d);
1705 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1706
1707 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1708 PASS_ONE_BIT,
1709 &bit_chk, 0)) {
1710 found_passing_read = 1;
1711 break;
1712 }
1713 }
1714
1715 if (found_passing_read) {
1716 /* Find a failing read */
1717 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
1718 read\n", __func__, __LINE__);
1719 for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
1720 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1721 testing read d=%u\n", __func__, __LINE__, d);
1722 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1723
1724 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1725 (grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
1726 found_failing_read = 1;
1727 break;
1728 }
1729 }
1730 } else {
1731 debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
1732 calculate dtaps", __func__, __LINE__);
1733 debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
1734 }
1735
1736 /*
1737 * The dynamically calculated dtaps_per_ptap is only valid if we
1738 * found a passing/failing read. If we didn't, it means d hit the max
1739 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1740 * statically calculated value.
1741 */
1742 if (found_passing_read && found_failing_read)
1743 dtaps_per_ptap = d - initial_failing_dtap;
1744
Marek Vasutb5450962015-07-12 21:05:08 +02001745 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001746 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
1747 - %u = %u", __func__, __LINE__, d,
1748 initial_failing_dtap, dtaps_per_ptap);
1749
1750 /* ******************************************** */
1751 /* * step 6: Find the centre of the window * */
1752 if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1753 &work_mid, &work_end) == 0)
1754 return 0;
1755
1756 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
1757 vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
1758 v, p-1, d);
1759 return 1;
1760}
1761
1762/*
1763 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
1764 * dq_in_delay values
1765 */
1766static uint32_t
1767rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
1768(uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
1769{
1770 uint32_t found;
1771 uint32_t i;
1772 uint32_t p;
1773 uint32_t d;
1774 uint32_t r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001775
1776 const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
1777 (RW_MGR_MEM_DQ_PER_READ_DQS-1);
1778 /* we start at zero, so have one less dq to devide among */
1779
1780 debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
1781 test_bgn);
1782
1783 /* try different dq_in_delays since the dq path is shorter than dqs */
1784
1785 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1786 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut122e1f32015-07-17 06:07:13 +02001787 for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++, d += delay_step) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001788 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
1789 vfifo_find_dqs_", __func__, __LINE__);
1790 debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
1791 write_group, read_group);
1792 debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
Marek Vasutcab80792015-07-12 22:07:33 +02001793 scc_mgr_set_dq_in_delay(p, d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001794 scc_mgr_load_dq(p);
1795 }
Marek Vasutb5450962015-07-12 21:05:08 +02001796 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001797 }
1798
1799 found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
1800
1801 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
1802 en_phase_sweep_dq", __func__, __LINE__);
1803 debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
1804 chain to zero\n", write_group, read_group, found);
1805
1806 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1807 r += NUM_RANKS_PER_SHADOW_REG) {
1808 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1809 i++, p++) {
Marek Vasutcab80792015-07-12 22:07:33 +02001810 scc_mgr_set_dq_in_delay(p, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001811 scc_mgr_load_dq(p);
1812 }
Marek Vasutb5450962015-07-12 21:05:08 +02001813 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001814 }
1815
1816 return found;
1817}
1818
1819/* per-bit deskew DQ and center */
1820static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
1821 uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
1822 uint32_t use_read_test, uint32_t update_fom)
1823{
1824 uint32_t i, p, d, min_index;
1825 /*
1826 * Store these as signed since there are comparisons with
1827 * signed numbers.
1828 */
1829 uint32_t bit_chk;
1830 uint32_t sticky_bit_chk;
1831 int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1832 int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1833 int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
1834 int32_t mid;
1835 int32_t orig_mid_min, mid_min;
1836 int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
1837 final_dqs_en;
1838 int32_t dq_margin, dqs_margin;
1839 uint32_t stop;
1840 uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
1841 uint32_t addr;
1842
1843 debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
1844
Marek Vasuta3340102015-07-12 19:03:33 +02001845 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02001846 start_dqs = readl(addr + (read_group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001847 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut33acf0f2015-07-12 20:05:54 +02001848 start_dqs_en = readl(addr + ((read_group << 2)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001849 - IO_DQS_EN_DELAY_OFFSET));
1850
1851 /* set the left and right edge of each bit to an illegal value */
1852 /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
1853 sticky_bit_chk = 0;
1854 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1855 left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1856 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1857 }
1858
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001859 /* Search for the left edge of the window for each bit */
1860 for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
1861 scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
1862
Marek Vasutb5450962015-07-12 21:05:08 +02001863 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001864
1865 /*
1866 * Stop searching when the read test doesn't pass AND when
1867 * we've seen a passing read on every bit.
1868 */
1869 if (use_read_test) {
1870 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1871 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1872 &bit_chk, 0, 0);
1873 } else {
1874 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1875 0, PASS_ONE_BIT,
1876 &bit_chk, 0);
1877 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1878 (read_group - (write_group *
1879 RW_MGR_MEM_IF_READ_DQS_WIDTH /
1880 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1881 stop = (bit_chk == 0);
1882 }
1883 sticky_bit_chk = sticky_bit_chk | bit_chk;
1884 stop = stop && (sticky_bit_chk == param->read_correct_mask);
1885 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
1886 && %u", __func__, __LINE__, d,
1887 sticky_bit_chk,
1888 param->read_correct_mask, stop);
1889
1890 if (stop == 1) {
1891 break;
1892 } else {
1893 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1894 if (bit_chk & 1) {
1895 /* Remember a passing test as the
1896 left_edge */
1897 left_edge[i] = d;
1898 } else {
1899 /* If a left edge has not been seen yet,
1900 then a future passing test will mark
1901 this edge as the right edge */
1902 if (left_edge[i] ==
1903 IO_IO_IN_DELAY_MAX + 1) {
1904 right_edge[i] = -(d + 1);
1905 }
1906 }
1907 bit_chk = bit_chk >> 1;
1908 }
1909 }
1910 }
1911
1912 /* Reset DQ delay chains to 0 */
Marek Vasut122e1f32015-07-17 06:07:13 +02001913 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001914 sticky_bit_chk = 0;
1915 for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
1916 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
1917 %d right_edge[%u]: %d\n", __func__, __LINE__,
1918 i, left_edge[i], i, right_edge[i]);
1919
1920 /*
1921 * Check for cases where we haven't found the left edge,
1922 * which makes our assignment of the the right edge invalid.
1923 * Reset it to the illegal value.
1924 */
1925 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
1926 right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1927 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1928 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
1929 right_edge[%u]: %d\n", __func__, __LINE__,
1930 i, right_edge[i]);
1931 }
1932
1933 /*
1934 * Reset sticky bit (except for bits where we have seen
1935 * both the left and right edge).
1936 */
1937 sticky_bit_chk = sticky_bit_chk << 1;
1938 if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
1939 (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1940 sticky_bit_chk = sticky_bit_chk | 1;
1941 }
1942
1943 if (i == 0)
1944 break;
1945 }
1946
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001947 /* Search for the right edge of the window for each bit */
1948 for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
1949 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
1950 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
1951 uint32_t delay = d + start_dqs_en;
1952 if (delay > IO_DQS_EN_DELAY_MAX)
1953 delay = IO_DQS_EN_DELAY_MAX;
1954 scc_mgr_set_dqs_en_delay(read_group, delay);
1955 }
1956 scc_mgr_load_dqs(read_group);
1957
Marek Vasutb5450962015-07-12 21:05:08 +02001958 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001959
1960 /*
1961 * Stop searching when the read test doesn't pass AND when
1962 * we've seen a passing read on every bit.
1963 */
1964 if (use_read_test) {
1965 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1966 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1967 &bit_chk, 0, 0);
1968 } else {
1969 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1970 0, PASS_ONE_BIT,
1971 &bit_chk, 0);
1972 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1973 (read_group - (write_group *
1974 RW_MGR_MEM_IF_READ_DQS_WIDTH /
1975 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1976 stop = (bit_chk == 0);
1977 }
1978 sticky_bit_chk = sticky_bit_chk | bit_chk;
1979 stop = stop && (sticky_bit_chk == param->read_correct_mask);
1980
1981 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
1982 %u && %u", __func__, __LINE__, d,
1983 sticky_bit_chk, param->read_correct_mask, stop);
1984
1985 if (stop == 1) {
1986 break;
1987 } else {
1988 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1989 if (bit_chk & 1) {
1990 /* Remember a passing test as
1991 the right_edge */
1992 right_edge[i] = d;
1993 } else {
1994 if (d != 0) {
1995 /* If a right edge has not been
1996 seen yet, then a future passing
1997 test will mark this edge as the
1998 left edge */
1999 if (right_edge[i] ==
2000 IO_IO_IN_DELAY_MAX + 1) {
2001 left_edge[i] = -(d + 1);
2002 }
2003 } else {
2004 /* d = 0 failed, but it passed
2005 when testing the left edge,
2006 so it must be marginal,
2007 set it to -1 */
2008 if (right_edge[i] ==
2009 IO_IO_IN_DELAY_MAX + 1 &&
2010 left_edge[i] !=
2011 IO_IO_IN_DELAY_MAX
2012 + 1) {
2013 right_edge[i] = -1;
2014 }
2015 /* If a right edge has not been
2016 seen yet, then a future passing
2017 test will mark this edge as the
2018 left edge */
2019 else if (right_edge[i] ==
2020 IO_IO_IN_DELAY_MAX +
2021 1) {
2022 left_edge[i] = -(d + 1);
2023 }
2024 }
2025 }
2026
2027 debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
2028 d=%u]: ", __func__, __LINE__, d);
2029 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
2030 (int)(bit_chk & 1), i, left_edge[i]);
2031 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2032 right_edge[i]);
2033 bit_chk = bit_chk >> 1;
2034 }
2035 }
2036 }
2037
2038 /* Check that all bits have a window */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002039 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2040 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
2041 %d right_edge[%u]: %d", __func__, __LINE__,
2042 i, left_edge[i], i, right_edge[i]);
2043 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
2044 == IO_IO_IN_DELAY_MAX + 1)) {
2045 /*
2046 * Restore delay chain settings before letting the loop
2047 * in rw_mgr_mem_calibrate_vfifo to retry different
2048 * dqs/ck relationships.
2049 */
2050 scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
2051 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2052 scc_mgr_set_dqs_en_delay(read_group,
2053 start_dqs_en);
2054 }
2055 scc_mgr_load_dqs(read_group);
Marek Vasutb5450962015-07-12 21:05:08 +02002056 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002057
2058 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
2059 find edge [%u]: %d %d", __func__, __LINE__,
2060 i, left_edge[i], right_edge[i]);
2061 if (use_read_test) {
2062 set_failing_group_stage(read_group *
2063 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2064 CAL_STAGE_VFIFO,
2065 CAL_SUBSTAGE_VFIFO_CENTER);
2066 } else {
2067 set_failing_group_stage(read_group *
2068 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2069 CAL_STAGE_VFIFO_AFTER_WRITES,
2070 CAL_SUBSTAGE_VFIFO_CENTER);
2071 }
2072 return 0;
2073 }
2074 }
2075
2076 /* Find middle of window for each DQ bit */
2077 mid_min = left_edge[0] - right_edge[0];
2078 min_index = 0;
2079 for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2080 mid = left_edge[i] - right_edge[i];
2081 if (mid < mid_min) {
2082 mid_min = mid;
2083 min_index = i;
2084 }
2085 }
2086
2087 /*
2088 * -mid_min/2 represents the amount that we need to move DQS.
2089 * If mid_min is odd and positive we'll need to add one to
2090 * make sure the rounding in further calculations is correct
2091 * (always bias to the right), so just add 1 for all positive values.
2092 */
2093 if (mid_min > 0)
2094 mid_min++;
2095
2096 mid_min = mid_min / 2;
2097
2098 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
2099 __func__, __LINE__, mid_min, min_index);
2100
2101 /* Determine the amount we can change DQS (which is -mid_min) */
2102 orig_mid_min = mid_min;
2103 new_dqs = start_dqs - mid_min;
2104 if (new_dqs > IO_DQS_IN_DELAY_MAX)
2105 new_dqs = IO_DQS_IN_DELAY_MAX;
2106 else if (new_dqs < 0)
2107 new_dqs = 0;
2108
2109 mid_min = start_dqs - new_dqs;
2110 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2111 mid_min, new_dqs);
2112
2113 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2114 if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2115 mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2116 else if (start_dqs_en - mid_min < 0)
2117 mid_min += start_dqs_en - mid_min;
2118 }
2119 new_dqs = start_dqs - mid_min;
2120
2121 debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
2122 new_dqs=%d mid_min=%d\n", start_dqs,
2123 IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2124 new_dqs, mid_min);
2125
2126 /* Initialize data for export structures */
2127 dqs_margin = IO_IO_IN_DELAY_MAX + 1;
2128 dq_margin = IO_IO_IN_DELAY_MAX + 1;
2129
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002130 /* add delay to bring centre of all DQ windows to the same "level" */
2131 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
2132 /* Use values before divide by 2 to reduce round off error */
2133 shift_dq = (left_edge[i] - right_edge[i] -
2134 (left_edge[min_index] - right_edge[min_index]))/2 +
2135 (orig_mid_min - mid_min);
2136
2137 debug_cond(DLEVEL == 2, "vfifo_center: before: \
2138 shift_dq[%u]=%d\n", i, shift_dq);
2139
Marek Vasutb5450962015-07-12 21:05:08 +02002140 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02002141 temp_dq_in_delay1 = readl(addr + (p << 2));
2142 temp_dq_in_delay2 = readl(addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002143
2144 if (shift_dq + (int32_t)temp_dq_in_delay1 >
2145 (int32_t)IO_IO_IN_DELAY_MAX) {
2146 shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
2147 } else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
2148 shift_dq = -(int32_t)temp_dq_in_delay1;
2149 }
2150 debug_cond(DLEVEL == 2, "vfifo_center: after: \
2151 shift_dq[%u]=%d\n", i, shift_dq);
2152 final_dq[i] = temp_dq_in_delay1 + shift_dq;
Marek Vasutcab80792015-07-12 22:07:33 +02002153 scc_mgr_set_dq_in_delay(p, final_dq[i]);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002154 scc_mgr_load_dq(p);
2155
2156 debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
2157 left_edge[i] - shift_dq + (-mid_min),
2158 right_edge[i] + shift_dq - (-mid_min));
2159 /* To determine values for export structures */
2160 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2161 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2162
2163 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2164 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2165 }
2166
2167 final_dqs = new_dqs;
2168 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
2169 final_dqs_en = start_dqs_en - mid_min;
2170
2171 /* Move DQS-en */
2172 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2173 scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
2174 scc_mgr_load_dqs(read_group);
2175 }
2176
2177 /* Move DQS */
2178 scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
2179 scc_mgr_load_dqs(read_group);
2180 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
2181 dqs_margin=%d", __func__, __LINE__,
2182 dq_margin, dqs_margin);
2183
2184 /*
2185 * Do not remove this line as it makes sure all of our decisions
2186 * have been applied. Apply the update bit.
2187 */
Marek Vasutb5450962015-07-12 21:05:08 +02002188 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002189
2190 return (dq_margin >= 0) && (dqs_margin >= 0);
2191}
2192
2193/*
2194 * calibrate the read valid prediction FIFO.
2195 *
2196 * - read valid prediction will consist of finding a good DQS enable phase,
2197 * DQS enable delay, DQS input phase, and DQS input delay.
2198 * - we also do a per-bit deskew on the DQ lines.
2199 */
2200static uint32_t rw_mgr_mem_calibrate_vfifo(uint32_t read_group,
2201 uint32_t test_bgn)
2202{
2203 uint32_t p, d, rank_bgn, sr;
2204 uint32_t dtaps_per_ptap;
2205 uint32_t tmp_delay;
2206 uint32_t bit_chk;
2207 uint32_t grp_calibrated;
2208 uint32_t write_group, write_test_bgn;
2209 uint32_t failed_substage;
2210
Marek Vasut0eacf7e2015-06-26 18:56:54 +02002211 debug("%s:%d: %u %u\n", __func__, __LINE__, read_group, test_bgn);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002212
2213 /* update info for sims */
2214 reg_file_set_stage(CAL_STAGE_VFIFO);
2215
2216 write_group = read_group;
2217 write_test_bgn = test_bgn;
2218
2219 /* USER Determine number of delay taps for each phase tap */
2220 dtaps_per_ptap = 0;
2221 tmp_delay = 0;
2222 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
2223 dtaps_per_ptap++;
2224 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
2225 }
2226 dtaps_per_ptap--;
2227 tmp_delay = 0;
2228
2229 /* update info for sims */
2230 reg_file_set_group(read_group);
2231
2232 grp_calibrated = 0;
2233
2234 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
2235 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2236
2237 for (d = 0; d <= dtaps_per_ptap && grp_calibrated == 0; d += 2) {
2238 /*
2239 * In RLDRAMX we may be messing the delay of pins in
2240 * the same write group but outside of the current read
2241 * the group, but that's ok because we haven't
2242 * calibrated output side yet.
2243 */
2244 if (d > 0) {
2245 scc_mgr_apply_group_all_out_delay_add_all_ranks
2246 (write_group, write_test_bgn, d);
2247 }
2248
2249 for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX && grp_calibrated == 0;
2250 p++) {
2251 /* set a particular dqdqs phase */
2252 scc_mgr_set_dqdqs_output_phase_all_ranks(read_group, p);
2253
2254 debug_cond(DLEVEL == 1, "%s:%d calibrate_vfifo: g=%u \
2255 p=%u d=%u\n", __func__, __LINE__,
2256 read_group, p, d);
2257
2258 /*
2259 * Load up the patterns used by read calibration
2260 * using current DQDQS phase.
2261 */
2262 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2263 if (!(gbl->phy_debug_mode_flags &
2264 PHY_DEBUG_DISABLE_GUARANTEED_READ)) {
2265 if (!rw_mgr_mem_calibrate_read_test_patterns_all_ranks
2266 (read_group, 1, &bit_chk)) {
2267 debug_cond(DLEVEL == 1, "%s:%d Guaranteed read test failed:",
2268 __func__, __LINE__);
2269 debug_cond(DLEVEL == 1, " g=%u p=%u d=%u\n",
2270 read_group, p, d);
2271 break;
2272 }
2273 }
2274
2275/* case:56390 */
2276 grp_calibrated = 1;
2277 if (rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
2278 (write_group, read_group, test_bgn)) {
2279 /*
2280 * USER Read per-bit deskew can be done on a
2281 * per shadow register basis.
2282 */
2283 for (rank_bgn = 0, sr = 0;
2284 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2285 rank_bgn += NUM_RANKS_PER_SHADOW_REG,
2286 ++sr) {
2287 /*
2288 * Determine if this set of ranks
2289 * should be skipped entirely.
2290 */
2291 if (!param->skip_shadow_regs[sr]) {
2292 /*
2293 * If doing read after write
2294 * calibration, do not update
2295 * FOM, now - do it then.
2296 */
2297 if (!rw_mgr_mem_calibrate_vfifo_center
2298 (rank_bgn, write_group,
2299 read_group, test_bgn, 1, 0)) {
2300 grp_calibrated = 0;
2301 failed_substage =
2302 CAL_SUBSTAGE_VFIFO_CENTER;
2303 }
2304 }
2305 }
2306 } else {
2307 grp_calibrated = 0;
2308 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2309 }
2310 }
2311 }
2312
2313 if (grp_calibrated == 0) {
2314 set_failing_group_stage(write_group, CAL_STAGE_VFIFO,
2315 failed_substage);
2316 return 0;
2317 }
2318
2319 /*
2320 * Reset the delay chains back to zero if they have moved > 1
2321 * (check for > 1 because loop will increase d even when pass in
2322 * first case).
2323 */
2324 if (d > 2)
Marek Vasut62d3c692015-07-20 08:41:04 +02002325 scc_mgr_zero_group(write_group, 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002326
2327 return 1;
2328}
2329
2330/* VFIFO Calibration -- Read Deskew Calibration after write deskew */
2331static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
2332 uint32_t test_bgn)
2333{
2334 uint32_t rank_bgn, sr;
2335 uint32_t grp_calibrated;
2336 uint32_t write_group;
2337
2338 debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
2339
2340 /* update info for sims */
2341
2342 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2343 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2344
2345 write_group = read_group;
2346
2347 /* update info for sims */
2348 reg_file_set_group(read_group);
2349
2350 grp_calibrated = 1;
2351 /* Read per-bit deskew can be done on a per shadow register basis */
2352 for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2353 rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
2354 /* Determine if this set of ranks should be skipped entirely */
2355 if (!param->skip_shadow_regs[sr]) {
2356 /* This is the last calibration round, update FOM here */
2357 if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
2358 write_group,
2359 read_group,
2360 test_bgn, 0,
2361 1)) {
2362 grp_calibrated = 0;
2363 }
2364 }
2365 }
2366
2367
2368 if (grp_calibrated == 0) {
2369 set_failing_group_stage(write_group,
2370 CAL_STAGE_VFIFO_AFTER_WRITES,
2371 CAL_SUBSTAGE_VFIFO_CENTER);
2372 return 0;
2373 }
2374
2375 return 1;
2376}
2377
2378/* Calibrate LFIFO to find smallest read latency */
2379static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2380{
2381 uint32_t found_one;
2382 uint32_t bit_chk;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002383
2384 debug("%s:%d\n", __func__, __LINE__);
2385
2386 /* update info for sims */
2387 reg_file_set_stage(CAL_STAGE_LFIFO);
2388 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2389
2390 /* Load up the patterns used by read calibration for all ranks */
2391 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2392 found_one = 0;
2393
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002394 do {
Marek Vasutb5450962015-07-12 21:05:08 +02002395 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002396 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2397 __func__, __LINE__, gbl->curr_read_lat);
2398
2399 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
2400 NUM_READ_TESTS,
2401 PASS_ALL_BITS,
2402 &bit_chk, 1)) {
2403 break;
2404 }
2405
2406 found_one = 1;
2407 /* reduce read latency and see if things are working */
2408 /* correctly */
2409 gbl->curr_read_lat--;
2410 } while (gbl->curr_read_lat > 0);
2411
2412 /* reset the fifos to get pointers to known state */
2413
Marek Vasutb5450962015-07-12 21:05:08 +02002414 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002415
2416 if (found_one) {
2417 /* add a fudge factor to the read latency that was determined */
2418 gbl->curr_read_lat += 2;
Marek Vasutb5450962015-07-12 21:05:08 +02002419 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002420 debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
2421 read_lat=%u\n", __func__, __LINE__,
2422 gbl->curr_read_lat);
2423 return 1;
2424 } else {
2425 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2426 CAL_SUBSTAGE_READ_LATENCY);
2427
2428 debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
2429 read_lat=%u\n", __func__, __LINE__,
2430 gbl->curr_read_lat);
2431 return 0;
2432 }
2433}
2434
2435/*
2436 * issue write test command.
2437 * two variants are provided. one that just tests a write pattern and
2438 * another that tests datamask functionality.
2439 */
2440static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
2441 uint32_t test_dm)
2442{
2443 uint32_t mcc_instruction;
2444 uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
2445 ENABLE_SUPER_QUICK_CALIBRATION);
2446 uint32_t rw_wl_nop_cycles;
2447 uint32_t addr;
2448
2449 /*
2450 * Set counter and jump addresses for the right
2451 * number of NOP cycles.
2452 * The number of supported NOP cycles can range from -1 to infinity
2453 * Three different cases are handled:
2454 *
2455 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
2456 * mechanism will be used to insert the right number of NOPs
2457 *
2458 * 2. For a number of NOP cycles equals to 0, the micro-instruction
2459 * issuing the write command will jump straight to the
2460 * micro-instruction that turns on DQS (for DDRx), or outputs write
2461 * data (for RLD), skipping
2462 * the NOP micro-instruction all together
2463 *
2464 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
2465 * turned on in the same micro-instruction that issues the write
2466 * command. Then we need
2467 * to directly jump to the micro-instruction that sends out the data
2468 *
2469 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
2470 * (2 and 3). One jump-counter (0) is used to perform multiple
2471 * write-read operations.
2472 * one counter left to issue this command in "multiple-group" mode
2473 */
2474
2475 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
2476
2477 if (rw_wl_nop_cycles == -1) {
2478 /*
2479 * CNTR 2 - We want to execute the special write operation that
2480 * turns on DQS right away and then skip directly to the
2481 * instruction that sends out the data. We set the counter to a
2482 * large number so that the jump is always taken.
2483 */
Marek Vasutb5450962015-07-12 21:05:08 +02002484 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002485
2486 /* CNTR 3 - Not used */
2487 if (test_dm) {
2488 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002489 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
Marek Vasutb5450962015-07-12 21:05:08 +02002490 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002491 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
Marek Vasutb5450962015-07-12 21:05:08 +02002492 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002493 } else {
2494 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
Marek Vasutb5450962015-07-12 21:05:08 +02002495 writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
2496 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2497 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2498 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002499 }
2500 } else if (rw_wl_nop_cycles == 0) {
2501 /*
2502 * CNTR 2 - We want to skip the NOP operation and go straight
2503 * to the DQS enable instruction. We set the counter to a large
2504 * number so that the jump is always taken.
2505 */
Marek Vasutb5450962015-07-12 21:05:08 +02002506 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002507
2508 /* CNTR 3 - Not used */
2509 if (test_dm) {
2510 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002511 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
Marek Vasutb5450962015-07-12 21:05:08 +02002512 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002513 } else {
2514 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
Marek Vasutb5450962015-07-12 21:05:08 +02002515 writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
2516 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002517 }
2518 } else {
2519 /*
2520 * CNTR 2 - In this case we want to execute the next instruction
2521 * and NOT take the jump. So we set the counter to 0. The jump
2522 * address doesn't count.
2523 */
Marek Vasutb5450962015-07-12 21:05:08 +02002524 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
2525 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002526
2527 /*
2528 * CNTR 3 - Set the nop counter to the number of cycles we
2529 * need to loop for, minus 1.
2530 */
Marek Vasutb5450962015-07-12 21:05:08 +02002531 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002532 if (test_dm) {
2533 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
Marek Vasutb5450962015-07-12 21:05:08 +02002534 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2535 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002536 } else {
2537 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
Marek Vasutb5450962015-07-12 21:05:08 +02002538 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2539 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002540 }
2541 }
2542
Marek Vasutb5450962015-07-12 21:05:08 +02002543 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
2544 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002545
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002546 if (quick_write_mode)
Marek Vasutb5450962015-07-12 21:05:08 +02002547 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002548 else
Marek Vasutb5450962015-07-12 21:05:08 +02002549 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002550
Marek Vasutb5450962015-07-12 21:05:08 +02002551 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002552
2553 /*
2554 * CNTR 1 - This is used to ensure enough time elapses
2555 * for read data to come back.
2556 */
Marek Vasutb5450962015-07-12 21:05:08 +02002557 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002558
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002559 if (test_dm) {
Marek Vasutb5450962015-07-12 21:05:08 +02002560 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
2561 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002562 } else {
Marek Vasutb5450962015-07-12 21:05:08 +02002563 writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
2564 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002565 }
2566
Marek Vasuta3340102015-07-12 19:03:33 +02002567 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02002568 writel(mcc_instruction, addr + (group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002569}
2570
2571/* Test writes, can check for a single bit pass or multiple bit pass */
2572static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
2573 uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
2574 uint32_t *bit_chk, uint32_t all_ranks)
2575{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002576 uint32_t r;
2577 uint32_t correct_mask_vg;
2578 uint32_t tmp_bit_chk;
2579 uint32_t vg;
2580 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
2581 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
2582 uint32_t addr_rw_mgr;
2583 uint32_t base_rw_mgr;
2584
2585 *bit_chk = param->write_correct_mask;
2586 correct_mask_vg = param->write_correct_mask_vg;
2587
2588 for (r = rank_bgn; r < rank_end; r++) {
2589 if (param->skip_ranks[r]) {
2590 /* request to skip the rank */
2591 continue;
2592 }
2593
2594 /* set rank */
2595 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
2596
2597 tmp_bit_chk = 0;
Marek Vasut1fa95892015-07-12 17:52:36 +02002598 addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002599 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
2600 /* reset the fifos to get pointers to known state */
Marek Vasutb5450962015-07-12 21:05:08 +02002601 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002602
2603 tmp_bit_chk = tmp_bit_chk <<
2604 (RW_MGR_MEM_DQ_PER_WRITE_DQS /
2605 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
2606 rw_mgr_mem_calibrate_write_test_issue(write_group *
2607 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
2608 use_dm);
2609
Marek Vasut33acf0f2015-07-12 20:05:54 +02002610 base_rw_mgr = readl(addr_rw_mgr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002611 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
2612 if (vg == 0)
2613 break;
2614 }
2615 *bit_chk &= tmp_bit_chk;
2616 }
2617
2618 if (all_correct) {
2619 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2620 debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
2621 %u => %lu", write_group, use_dm,
2622 *bit_chk, param->write_correct_mask,
2623 (long unsigned int)(*bit_chk ==
2624 param->write_correct_mask));
2625 return *bit_chk == param->write_correct_mask;
2626 } else {
2627 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2628 debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
2629 write_group, use_dm, *bit_chk);
2630 debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
2631 (long unsigned int)(*bit_chk != 0));
2632 return *bit_chk != 0x00;
2633 }
2634}
2635
2636/*
2637 * center all windows. do per-bit-deskew to possibly increase size of
2638 * certain windows.
2639 */
2640static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
2641 uint32_t write_group, uint32_t test_bgn)
2642{
2643 uint32_t i, p, min_index;
2644 int32_t d;
2645 /*
2646 * Store these as signed since there are comparisons with
2647 * signed numbers.
2648 */
2649 uint32_t bit_chk;
2650 uint32_t sticky_bit_chk;
2651 int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2652 int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2653 int32_t mid;
2654 int32_t mid_min, orig_mid_min;
2655 int32_t new_dqs, start_dqs, shift_dq;
2656 int32_t dq_margin, dqs_margin, dm_margin;
2657 uint32_t stop;
2658 uint32_t temp_dq_out1_delay;
2659 uint32_t addr;
2660
2661 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2662
2663 dm_margin = 0;
2664
Marek Vasuta3340102015-07-12 19:03:33 +02002665 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02002666 start_dqs = readl(addr +
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002667 (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2668
2669 /* per-bit deskew */
2670
2671 /*
2672 * set the left and right edge of each bit to an illegal value
2673 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
2674 */
2675 sticky_bit_chk = 0;
2676 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2677 left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2678 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2679 }
2680
2681 /* Search for the left edge of the window for each bit */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002682 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
Marek Vasutcd649502015-07-17 05:42:49 +02002683 scc_mgr_apply_group_dq_out1_delay(write_group, d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002684
Marek Vasutb5450962015-07-12 21:05:08 +02002685 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002686
2687 /*
2688 * Stop searching when the read test doesn't pass AND when
2689 * we've seen a passing read on every bit.
2690 */
2691 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2692 0, PASS_ONE_BIT, &bit_chk, 0);
2693 sticky_bit_chk = sticky_bit_chk | bit_chk;
2694 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2695 debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
2696 == %u && %u [bit_chk= %u ]\n",
2697 d, sticky_bit_chk, param->write_correct_mask,
2698 stop, bit_chk);
2699
2700 if (stop == 1) {
2701 break;
2702 } else {
2703 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2704 if (bit_chk & 1) {
2705 /*
2706 * Remember a passing test as the
2707 * left_edge.
2708 */
2709 left_edge[i] = d;
2710 } else {
2711 /*
2712 * If a left edge has not been seen
2713 * yet, then a future passing test will
2714 * mark this edge as the right edge.
2715 */
2716 if (left_edge[i] ==
2717 IO_IO_OUT1_DELAY_MAX + 1) {
2718 right_edge[i] = -(d + 1);
2719 }
2720 }
2721 debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
2722 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2723 (int)(bit_chk & 1), i, left_edge[i]);
2724 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2725 right_edge[i]);
2726 bit_chk = bit_chk >> 1;
2727 }
2728 }
2729 }
2730
2731 /* Reset DQ delay chains to 0 */
Marek Vasut122e1f32015-07-17 06:07:13 +02002732 scc_mgr_apply_group_dq_out1_delay(0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002733 sticky_bit_chk = 0;
2734 for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
2735 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2736 %d right_edge[%u]: %d\n", __func__, __LINE__,
2737 i, left_edge[i], i, right_edge[i]);
2738
2739 /*
2740 * Check for cases where we haven't found the left edge,
2741 * which makes our assignment of the the right edge invalid.
2742 * Reset it to the illegal value.
2743 */
2744 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
2745 (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
2746 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2747 debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
2748 right_edge[%u]: %d\n", __func__, __LINE__,
2749 i, right_edge[i]);
2750 }
2751
2752 /*
2753 * Reset sticky bit (except for bits where we have
2754 * seen the left edge).
2755 */
2756 sticky_bit_chk = sticky_bit_chk << 1;
2757 if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
2758 sticky_bit_chk = sticky_bit_chk | 1;
2759
2760 if (i == 0)
2761 break;
2762 }
2763
2764 /* Search for the right edge of the window for each bit */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002765 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
2766 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2767 d + start_dqs);
2768
Marek Vasutb5450962015-07-12 21:05:08 +02002769 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002770
2771 /*
2772 * Stop searching when the read test doesn't pass AND when
2773 * we've seen a passing read on every bit.
2774 */
2775 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2776 0, PASS_ONE_BIT, &bit_chk, 0);
2777
2778 sticky_bit_chk = sticky_bit_chk | bit_chk;
2779 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2780
2781 debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
2782 %u && %u\n", d, sticky_bit_chk,
2783 param->write_correct_mask, stop);
2784
2785 if (stop == 1) {
2786 if (d == 0) {
2787 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
2788 i++) {
2789 /* d = 0 failed, but it passed when
2790 testing the left edge, so it must be
2791 marginal, set it to -1 */
2792 if (right_edge[i] ==
2793 IO_IO_OUT1_DELAY_MAX + 1 &&
2794 left_edge[i] !=
2795 IO_IO_OUT1_DELAY_MAX + 1) {
2796 right_edge[i] = -1;
2797 }
2798 }
2799 }
2800 break;
2801 } else {
2802 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2803 if (bit_chk & 1) {
2804 /*
2805 * Remember a passing test as
2806 * the right_edge.
2807 */
2808 right_edge[i] = d;
2809 } else {
2810 if (d != 0) {
2811 /*
2812 * If a right edge has not
2813 * been seen yet, then a future
2814 * passing test will mark this
2815 * edge as the left edge.
2816 */
2817 if (right_edge[i] ==
2818 IO_IO_OUT1_DELAY_MAX + 1)
2819 left_edge[i] = -(d + 1);
2820 } else {
2821 /*
2822 * d = 0 failed, but it passed
2823 * when testing the left edge,
2824 * so it must be marginal, set
2825 * it to -1.
2826 */
2827 if (right_edge[i] ==
2828 IO_IO_OUT1_DELAY_MAX + 1 &&
2829 left_edge[i] !=
2830 IO_IO_OUT1_DELAY_MAX + 1)
2831 right_edge[i] = -1;
2832 /*
2833 * If a right edge has not been
2834 * seen yet, then a future
2835 * passing test will mark this
2836 * edge as the left edge.
2837 */
2838 else if (right_edge[i] ==
2839 IO_IO_OUT1_DELAY_MAX +
2840 1)
2841 left_edge[i] = -(d + 1);
2842 }
2843 }
2844 debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
2845 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2846 (int)(bit_chk & 1), i, left_edge[i]);
2847 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2848 right_edge[i]);
2849 bit_chk = bit_chk >> 1;
2850 }
2851 }
2852 }
2853
2854 /* Check that all bits have a window */
2855 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2856 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2857 %d right_edge[%u]: %d", __func__, __LINE__,
2858 i, left_edge[i], i, right_edge[i]);
2859 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
2860 (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
2861 set_failing_group_stage(test_bgn + i,
2862 CAL_STAGE_WRITES,
2863 CAL_SUBSTAGE_WRITES_CENTER);
2864 return 0;
2865 }
2866 }
2867
2868 /* Find middle of window for each DQ bit */
2869 mid_min = left_edge[0] - right_edge[0];
2870 min_index = 0;
2871 for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2872 mid = left_edge[i] - right_edge[i];
2873 if (mid < mid_min) {
2874 mid_min = mid;
2875 min_index = i;
2876 }
2877 }
2878
2879 /*
2880 * -mid_min/2 represents the amount that we need to move DQS.
2881 * If mid_min is odd and positive we'll need to add one to
2882 * make sure the rounding in further calculations is correct
2883 * (always bias to the right), so just add 1 for all positive values.
2884 */
2885 if (mid_min > 0)
2886 mid_min++;
2887 mid_min = mid_min / 2;
2888 debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
2889 __LINE__, mid_min);
2890
2891 /* Determine the amount we can change DQS (which is -mid_min) */
2892 orig_mid_min = mid_min;
2893 new_dqs = start_dqs;
2894 mid_min = 0;
2895 debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
2896 mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
2897 /* Initialize data for export structures */
2898 dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
2899 dq_margin = IO_IO_OUT1_DELAY_MAX + 1;
2900
2901 /* add delay to bring centre of all DQ windows to the same "level" */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002902 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
2903 /* Use values before divide by 2 to reduce round off error */
2904 shift_dq = (left_edge[i] - right_edge[i] -
2905 (left_edge[min_index] - right_edge[min_index]))/2 +
2906 (orig_mid_min - mid_min);
2907
2908 debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
2909 [%u]=%d\n", __func__, __LINE__, i, shift_dq);
2910
Marek Vasutb5450962015-07-12 21:05:08 +02002911 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
Marek Vasut33acf0f2015-07-12 20:05:54 +02002912 temp_dq_out1_delay = readl(addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002913 if (shift_dq + (int32_t)temp_dq_out1_delay >
2914 (int32_t)IO_IO_OUT1_DELAY_MAX) {
2915 shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
2916 } else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
2917 shift_dq = -(int32_t)temp_dq_out1_delay;
2918 }
2919 debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
2920 i, shift_dq);
Marek Vasutcab80792015-07-12 22:07:33 +02002921 scc_mgr_set_dq_out1_delay(i, temp_dq_out1_delay + shift_dq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002922 scc_mgr_load_dq(i);
2923
2924 debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
2925 left_edge[i] - shift_dq + (-mid_min),
2926 right_edge[i] + shift_dq - (-mid_min));
2927 /* To determine values for export structures */
2928 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2929 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2930
2931 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2932 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2933 }
2934
2935 /* Move DQS */
2936 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasutb5450962015-07-12 21:05:08 +02002937 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002938
2939 /* Centre DM */
2940 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
2941
2942 /*
2943 * set the left and right edge of each bit to an illegal value,
2944 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
2945 */
2946 left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2947 right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2948 int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2949 int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2950 int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2951 int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
2952 int32_t win_best = 0;
2953
2954 /* Search for the/part of the window with DM shift */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002955 for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
Marek Vasut122e1f32015-07-17 06:07:13 +02002956 scc_mgr_apply_group_dm_out1_delay(d);
Marek Vasutb5450962015-07-12 21:05:08 +02002957 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002958
2959 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2960 PASS_ALL_BITS, &bit_chk,
2961 0)) {
2962 /* USE Set current end of the window */
2963 end_curr = -d;
2964 /*
2965 * If a starting edge of our window has not been seen
2966 * this is our current start of the DM window.
2967 */
2968 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2969 bgn_curr = -d;
2970
2971 /*
2972 * If current window is bigger than best seen.
2973 * Set best seen to be current window.
2974 */
2975 if ((end_curr-bgn_curr+1) > win_best) {
2976 win_best = end_curr-bgn_curr+1;
2977 bgn_best = bgn_curr;
2978 end_best = end_curr;
2979 }
2980 } else {
2981 /* We just saw a failing test. Reset temp edge */
2982 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2983 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2984 }
2985 }
2986
2987
2988 /* Reset DM delay chains to 0 */
Marek Vasut122e1f32015-07-17 06:07:13 +02002989 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002990
2991 /*
2992 * Check to see if the current window nudges up aganist 0 delay.
2993 * If so we need to continue the search by shifting DQS otherwise DQS
2994 * search begins as a new search. */
2995 if (end_curr != 0) {
2996 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2997 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2998 }
2999
3000 /* Search for the/part of the window with DQS shifts */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003001 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
3002 /*
3003 * Note: This only shifts DQS, so are we limiting ourselve to
3004 * width of DQ unnecessarily.
3005 */
3006 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
3007 d + new_dqs);
3008
Marek Vasutb5450962015-07-12 21:05:08 +02003009 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003010 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
3011 PASS_ALL_BITS, &bit_chk,
3012 0)) {
3013 /* USE Set current end of the window */
3014 end_curr = d;
3015 /*
3016 * If a beginning edge of our window has not been seen
3017 * this is our current begin of the DM window.
3018 */
3019 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
3020 bgn_curr = d;
3021
3022 /*
3023 * If current window is bigger than best seen. Set best
3024 * seen to be current window.
3025 */
3026 if ((end_curr-bgn_curr+1) > win_best) {
3027 win_best = end_curr-bgn_curr+1;
3028 bgn_best = bgn_curr;
3029 end_best = end_curr;
3030 }
3031 } else {
3032 /* We just saw a failing test. Reset temp edge */
3033 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3034 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3035
3036 /* Early exit optimization: if ther remaining delay
3037 chain space is less than already seen largest window
3038 we can exit */
3039 if ((win_best-1) >
3040 (IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
3041 break;
3042 }
3043 }
3044 }
3045
3046 /* assign left and right edge for cal and reporting; */
3047 left_edge[0] = -1*bgn_best;
3048 right_edge[0] = end_best;
3049
3050 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
3051 __LINE__, left_edge[0], right_edge[0]);
3052
3053 /* Move DQS (back to orig) */
3054 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3055
3056 /* Move DM */
3057
3058 /* Find middle of window for the DM bit */
3059 mid = (left_edge[0] - right_edge[0]) / 2;
3060
3061 /* only move right, since we are not moving DQS/DQ */
3062 if (mid < 0)
3063 mid = 0;
3064
3065 /* dm_marign should fail if we never find a window */
3066 if (win_best == 0)
3067 dm_margin = -1;
3068 else
3069 dm_margin = left_edge[0] - mid;
3070
Marek Vasut122e1f32015-07-17 06:07:13 +02003071 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasutb5450962015-07-12 21:05:08 +02003072 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003073
3074 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
3075 dm_margin=%d\n", __func__, __LINE__, left_edge[0],
3076 right_edge[0], mid, dm_margin);
3077 /* Export values */
3078 gbl->fom_out += dq_margin + dqs_margin;
3079
3080 debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
3081 dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
3082 dq_margin, dqs_margin, dm_margin);
3083
3084 /*
3085 * Do not remove this line as it makes sure all of our
3086 * decisions have been applied.
3087 */
Marek Vasutb5450962015-07-12 21:05:08 +02003088 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003089 return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
3090}
3091
3092/* calibrate the write operations */
3093static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
3094 uint32_t test_bgn)
3095{
3096 /* update info for sims */
3097 debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
3098
3099 reg_file_set_stage(CAL_STAGE_WRITES);
3100 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3101
3102 reg_file_set_group(g);
3103
3104 if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
3105 set_failing_group_stage(g, CAL_STAGE_WRITES,
3106 CAL_SUBSTAGE_WRITES_CENTER);
3107 return 0;
3108 }
3109
3110 return 1;
3111}
3112
3113/* precharge all banks and activate row 0 in bank "000..." and bank "111..." */
3114static void mem_precharge_and_activate(void)
3115{
3116 uint32_t r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003117
3118 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
3119 if (param->skip_ranks[r]) {
3120 /* request to skip the rank */
3121 continue;
3122 }
3123
3124 /* set rank */
3125 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3126
3127 /* precharge all banks ... */
Marek Vasutb5450962015-07-12 21:05:08 +02003128 writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3129 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003130
Marek Vasutb5450962015-07-12 21:05:08 +02003131 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3132 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3133 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003134
Marek Vasutb5450962015-07-12 21:05:08 +02003135 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3136 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3137 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003138
3139 /* activate rows */
Marek Vasutb5450962015-07-12 21:05:08 +02003140 writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3141 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003142 }
3143}
3144
3145/* Configure various memory related parameters. */
3146static void mem_config(void)
3147{
3148 uint32_t rlat, wlat;
3149 uint32_t rw_wl_nop_cycles;
3150 uint32_t max_latency;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003151
3152 debug("%s:%d\n", __func__, __LINE__);
3153 /* read in write and read latency */
Marek Vasutb5450962015-07-12 21:05:08 +02003154 wlat = readl(&data_mgr->t_wl_add);
3155 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003156
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003157 /* WL for hard phy does not include additive latency */
3158
3159 /*
3160 * add addtional write latency to offset the address/command extra
3161 * clock cycle. We change the AC mux setting causing AC to be delayed
3162 * by one mem clock cycle. Only do this for DDR3
3163 */
3164 wlat = wlat + 1;
3165
Marek Vasutb5450962015-07-12 21:05:08 +02003166 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003167
3168 rw_wl_nop_cycles = wlat - 2;
3169 gbl->rw_wl_nop_cycles = rw_wl_nop_cycles;
3170
3171 /*
3172 * For AV/CV, lfifo is hardened and always runs at full rate so
3173 * max latency in AFI clocks, used here, is correspondingly smaller.
3174 */
3175 max_latency = (1<<MAX_LATENCY_COUNT_WIDTH)/1 - 1;
3176 /* configure for a burst length of 8 */
3177
3178 /* write latency */
3179 /* Adjust Write Latency for Hard PHY */
3180 wlat = wlat + 1;
3181
3182 /* set a pretty high read latency initially */
3183 gbl->curr_read_lat = rlat + 16;
3184
3185 if (gbl->curr_read_lat > max_latency)
3186 gbl->curr_read_lat = max_latency;
3187
Marek Vasutb5450962015-07-12 21:05:08 +02003188 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003189
3190 /* advertise write latency */
3191 gbl->curr_write_lat = wlat;
Marek Vasutb5450962015-07-12 21:05:08 +02003192 writel(wlat - 2, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003193
3194 /* initialize bit slips */
3195 mem_precharge_and_activate();
3196}
3197
3198/* Set VFIFO and LFIFO to instant-on settings in skip calibration mode */
3199static void mem_skip_calibrate(void)
3200{
3201 uint32_t vfifo_offset;
3202 uint32_t i, j, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003203
3204 debug("%s:%d\n", __func__, __LINE__);
3205 /* Need to update every shadow register set used by the interface */
3206 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
3207 r += NUM_RANKS_PER_SHADOW_REG) {
3208 /*
3209 * Set output phase alignment settings appropriate for
3210 * skip calibration.
3211 */
3212 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3213 scc_mgr_set_dqs_en_phase(i, 0);
3214#if IO_DLL_CHAIN_LENGTH == 6
3215 scc_mgr_set_dqdqs_output_phase(i, 6);
3216#else
3217 scc_mgr_set_dqdqs_output_phase(i, 7);
3218#endif
3219 /*
3220 * Case:33398
3221 *
3222 * Write data arrives to the I/O two cycles before write
3223 * latency is reached (720 deg).
3224 * -> due to bit-slip in a/c bus
3225 * -> to allow board skew where dqs is longer than ck
3226 * -> how often can this happen!?
3227 * -> can claim back some ptaps for high freq
3228 * support if we can relax this, but i digress...
3229 *
3230 * The write_clk leads mem_ck by 90 deg
3231 * The minimum ptap of the OPA is 180 deg
3232 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3233 * The write_clk is always delayed by 2 ptaps
3234 *
3235 * Hence, to make DQS aligned to CK, we need to delay
3236 * DQS by:
3237 * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3238 *
3239 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3240 * gives us the number of ptaps, which simplies to:
3241 *
3242 * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3243 */
3244 scc_mgr_set_dqdqs_output_phase(i, (1.25 *
3245 IO_DLL_CHAIN_LENGTH - 2));
3246 }
Marek Vasutb5450962015-07-12 21:05:08 +02003247 writel(0xff, &sdr_scc_mgr->dqs_ena);
3248 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003249
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003250 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
Marek Vasutb5450962015-07-12 21:05:08 +02003251 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3252 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003253 }
Marek Vasutb5450962015-07-12 21:05:08 +02003254 writel(0xff, &sdr_scc_mgr->dq_ena);
3255 writel(0xff, &sdr_scc_mgr->dm_ena);
3256 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003257 }
3258
3259 /* Compensate for simulation model behaviour */
3260 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3261 scc_mgr_set_dqs_bus_in_delay(i, 10);
3262 scc_mgr_load_dqs(i);
3263 }
Marek Vasutb5450962015-07-12 21:05:08 +02003264 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003265
3266 /*
3267 * ArriaV has hard FIFOs that can only be initialized by incrementing
3268 * in sequencer.
3269 */
3270 vfifo_offset = CALIB_VFIFO_OFFSET;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003271 for (j = 0; j < vfifo_offset; j++) {
Marek Vasutb5450962015-07-12 21:05:08 +02003272 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003273 }
Marek Vasutb5450962015-07-12 21:05:08 +02003274 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003275
3276 /*
3277 * For ACV with hard lfifo, we get the skip-cal setting from
3278 * generation-time constant.
3279 */
3280 gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
Marek Vasutb5450962015-07-12 21:05:08 +02003281 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003282}
3283
3284/* Memory calibration entry point */
3285static uint32_t mem_calibrate(void)
3286{
3287 uint32_t i;
3288 uint32_t rank_bgn, sr;
3289 uint32_t write_group, write_test_bgn;
3290 uint32_t read_group, read_test_bgn;
3291 uint32_t run_groups, current_run;
3292 uint32_t failing_groups = 0;
3293 uint32_t group_failed = 0;
3294 uint32_t sr_failed = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003295
3296 debug("%s:%d\n", __func__, __LINE__);
3297 /* Initialize the data settings */
3298
3299 gbl->error_substage = CAL_SUBSTAGE_NIL;
3300 gbl->error_stage = CAL_STAGE_NIL;
3301 gbl->error_group = 0xff;
3302 gbl->fom_in = 0;
3303 gbl->fom_out = 0;
3304
3305 mem_config();
3306
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003307 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
Marek Vasutb5450962015-07-12 21:05:08 +02003308 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3309 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutd4d3de22015-07-19 01:34:43 +02003310 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3311 if (i == 0)
3312 scc_mgr_set_hhp_extras();
3313
Marek Vasut0341de42015-07-17 02:06:20 +02003314 scc_set_bypass_mode(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003315 }
3316
3317 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3318 /*
3319 * Set VFIFO and LFIFO to instant-on settings in skip
3320 * calibration mode.
3321 */
3322 mem_skip_calibrate();
3323 } else {
3324 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3325 /*
3326 * Zero all delay chain/phase settings for all
3327 * groups and all shadow register sets.
3328 */
3329 scc_mgr_zero_all();
3330
3331 run_groups = ~param->skip_groups;
3332
3333 for (write_group = 0, write_test_bgn = 0; write_group
3334 < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3335 write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
3336 /* Initialized the group failure */
3337 group_failed = 0;
3338
3339 current_run = run_groups & ((1 <<
3340 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3341 run_groups = run_groups >>
3342 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
3343
3344 if (current_run == 0)
3345 continue;
3346
Marek Vasutb5450962015-07-12 21:05:08 +02003347 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3348 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasut62d3c692015-07-20 08:41:04 +02003349 scc_mgr_zero_group(write_group, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003350
3351 for (read_group = write_group *
3352 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3353 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3354 read_test_bgn = 0;
3355 read_group < (write_group + 1) *
3356 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3357 RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3358 group_failed == 0;
3359 read_group++, read_test_bgn +=
3360 RW_MGR_MEM_DQ_PER_READ_DQS) {
3361 /* Calibrate the VFIFO */
3362 if (!((STATIC_CALIB_STEPS) &
3363 CALIB_SKIP_VFIFO)) {
3364 if (!rw_mgr_mem_calibrate_vfifo
3365 (read_group,
3366 read_test_bgn)) {
3367 group_failed = 1;
3368
3369 if (!(gbl->
3370 phy_debug_mode_flags &
3371 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3372 return 0;
3373 }
3374 }
3375 }
3376 }
3377
3378 /* Calibrate the output side */
3379 if (group_failed == 0) {
3380 for (rank_bgn = 0, sr = 0; rank_bgn
3381 < RW_MGR_MEM_NUMBER_OF_RANKS;
3382 rank_bgn +=
3383 NUM_RANKS_PER_SHADOW_REG,
3384 ++sr) {
3385 sr_failed = 0;
3386 if (!((STATIC_CALIB_STEPS) &
3387 CALIB_SKIP_WRITES)) {
3388 if ((STATIC_CALIB_STEPS)
3389 & CALIB_SKIP_DELAY_SWEEPS) {
3390 /* not needed in quick mode! */
3391 } else {
3392 /*
3393 * Determine if this set of
3394 * ranks should be skipped
3395 * entirely.
3396 */
3397 if (!param->skip_shadow_regs[sr]) {
3398 if (!rw_mgr_mem_calibrate_writes
3399 (rank_bgn, write_group,
3400 write_test_bgn)) {
3401 sr_failed = 1;
3402 if (!(gbl->
3403 phy_debug_mode_flags &
3404 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3405 return 0;
3406 }
3407 }
3408 }
3409 }
3410 }
3411 if (sr_failed != 0)
3412 group_failed = 1;
3413 }
3414 }
3415
3416 if (group_failed == 0) {
3417 for (read_group = write_group *
3418 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3419 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3420 read_test_bgn = 0;
3421 read_group < (write_group + 1)
3422 * RW_MGR_MEM_IF_READ_DQS_WIDTH
3423 / RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3424 group_failed == 0;
3425 read_group++, read_test_bgn +=
3426 RW_MGR_MEM_DQ_PER_READ_DQS) {
3427 if (!((STATIC_CALIB_STEPS) &
3428 CALIB_SKIP_WRITES)) {
3429 if (!rw_mgr_mem_calibrate_vfifo_end
3430 (read_group, read_test_bgn)) {
3431 group_failed = 1;
3432
3433 if (!(gbl->phy_debug_mode_flags
3434 & PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3435 return 0;
3436 }
3437 }
3438 }
3439 }
3440 }
3441
3442 if (group_failed != 0)
3443 failing_groups++;
3444 }
3445
3446 /*
3447 * USER If there are any failing groups then report
3448 * the failure.
3449 */
3450 if (failing_groups != 0)
3451 return 0;
3452
3453 /* Calibrate the LFIFO */
3454 if (!((STATIC_CALIB_STEPS) & CALIB_SKIP_LFIFO)) {
3455 /*
3456 * If we're skipping groups as part of debug,
3457 * don't calibrate LFIFO.
3458 */
3459 if (param->skip_groups == 0) {
3460 if (!rw_mgr_mem_calibrate_lfifo())
3461 return 0;
3462 }
3463 }
3464 }
3465 }
3466
3467 /*
3468 * Do not remove this line as it makes sure all of our decisions
3469 * have been applied.
3470 */
Marek Vasutb5450962015-07-12 21:05:08 +02003471 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003472 return 1;
3473}
3474
3475static uint32_t run_mem_calibrate(void)
3476{
3477 uint32_t pass;
3478 uint32_t debug_info;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003479
3480 debug("%s:%d\n", __func__, __LINE__);
3481
3482 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasutb5450962015-07-12 21:05:08 +02003483 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003484
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003485 /* stop tracking manger */
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003486 uint32_t ctrlcfg = readl(&sdr_ctrl->ctrl_cfg);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003487
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003488 writel(ctrlcfg & 0xFFBFFFFF, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003489
3490 initialize();
3491 rw_mgr_mem_initialize();
3492
3493 pass = mem_calibrate();
3494
3495 mem_precharge_and_activate();
Marek Vasutb5450962015-07-12 21:05:08 +02003496 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003497
3498 /*
3499 * Handoff:
3500 * Don't return control of the PHY back to AFI when in debug mode.
3501 */
3502 if ((gbl->phy_debug_mode_flags & PHY_DEBUG_IN_DEBUG_MODE) == 0) {
3503 rw_mgr_mem_handoff();
3504 /*
3505 * In Hard PHY this is a 2-bit control:
3506 * 0: AFI Mux Select
3507 * 1: DDIO Mux Select
3508 */
Marek Vasutb5450962015-07-12 21:05:08 +02003509 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003510 }
3511
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003512 writel(ctrlcfg, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003513
3514 if (pass) {
3515 printf("%s: CALIBRATION PASSED\n", __FILE__);
3516
3517 gbl->fom_in /= 2;
3518 gbl->fom_out /= 2;
3519
3520 if (gbl->fom_in > 0xff)
3521 gbl->fom_in = 0xff;
3522
3523 if (gbl->fom_out > 0xff)
3524 gbl->fom_out = 0xff;
3525
3526 /* Update the FOM in the register file */
3527 debug_info = gbl->fom_in;
3528 debug_info |= gbl->fom_out << 8;
Marek Vasutb5450962015-07-12 21:05:08 +02003529 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003530
Marek Vasutb5450962015-07-12 21:05:08 +02003531 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3532 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003533 } else {
3534 printf("%s: CALIBRATION FAILED\n", __FILE__);
3535
3536 debug_info = gbl->error_stage;
3537 debug_info |= gbl->error_substage << 8;
3538 debug_info |= gbl->error_group << 16;
3539
Marek Vasutb5450962015-07-12 21:05:08 +02003540 writel(debug_info, &sdr_reg_file->failing_stage);
3541 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3542 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003543
3544 /* Update the failing group/stage in the register file */
3545 debug_info = gbl->error_stage;
3546 debug_info |= gbl->error_substage << 8;
3547 debug_info |= gbl->error_group << 16;
Marek Vasutb5450962015-07-12 21:05:08 +02003548 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003549 }
3550
3551 return pass;
3552}
3553
Marek Vasutea9771b2015-07-19 06:12:42 +02003554/**
3555 * hc_initialize_rom_data() - Initialize ROM data
3556 *
3557 * Initialize ROM data.
3558 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003559static void hc_initialize_rom_data(void)
3560{
Marek Vasutea9771b2015-07-19 06:12:42 +02003561 u32 i, addr;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003562
Marek Vasuta3340102015-07-12 19:03:33 +02003563 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasutea9771b2015-07-19 06:12:42 +02003564 for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3565 writel(inst_rom_init[i], addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003566
Marek Vasuta3340102015-07-12 19:03:33 +02003567 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasutea9771b2015-07-19 06:12:42 +02003568 for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3569 writel(ac_rom_init[i], addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003570}
3571
Marek Vasuta17ae0f2015-07-19 06:13:37 +02003572/**
3573 * initialize_reg_file() - Initialize SDR register file
3574 *
3575 * Initialize SDR register file.
3576 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003577static void initialize_reg_file(void)
3578{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003579 /* Initialize the register file with the correct data */
Marek Vasutb5450962015-07-12 21:05:08 +02003580 writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3581 writel(0, &sdr_reg_file->debug_data_addr);
3582 writel(0, &sdr_reg_file->cur_stage);
3583 writel(0, &sdr_reg_file->fom);
3584 writel(0, &sdr_reg_file->failing_stage);
3585 writel(0, &sdr_reg_file->debug1);
3586 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003587}
3588
Marek Vasut0c9f3cb2015-07-19 06:14:04 +02003589/**
3590 * initialize_hps_phy() - Initialize HPS PHY
3591 *
3592 * Initialize HPS PHY.
3593 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003594static void initialize_hps_phy(void)
3595{
3596 uint32_t reg;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003597 /*
3598 * Tracking also gets configured here because it's in the
3599 * same register.
3600 */
3601 uint32_t trk_sample_count = 7500;
3602 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3603 /*
3604 * Format is number of outer loops in the 16 MSB, sample
3605 * count in 16 LSB.
3606 */
3607
3608 reg = 0;
3609 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3610 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3611 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3612 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3613 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3614 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3615 /*
3616 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3617 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3618 */
3619 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3620 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3621 trk_sample_count);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003622 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003623
3624 reg = 0;
3625 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3626 trk_sample_count >>
3627 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3628 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3629 trk_long_idle_sample_count);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003630 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003631
3632 reg = 0;
3633 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3634 trk_long_idle_sample_count >>
3635 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003636 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003637}
3638
3639static void initialize_tracking(void)
3640{
3641 uint32_t concatenated_longidle = 0x0;
3642 uint32_t concatenated_delays = 0x0;
3643 uint32_t concatenated_rw_addr = 0x0;
3644 uint32_t concatenated_refresh = 0x0;
3645 uint32_t trk_sample_count = 7500;
3646 uint32_t dtaps_per_ptap;
3647 uint32_t tmp_delay;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003648
3649 /*
3650 * compute usable version of value in case we skip full
3651 * computation later
3652 */
3653 dtaps_per_ptap = 0;
3654 tmp_delay = 0;
3655 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
3656 dtaps_per_ptap++;
3657 tmp_delay += IO_DELAY_PER_DCHAIN_TAP;
3658 }
3659 dtaps_per_ptap--;
3660
3661 concatenated_longidle = concatenated_longidle ^ 10;
3662 /*longidle outer loop */
3663 concatenated_longidle = concatenated_longidle << 16;
3664 concatenated_longidle = concatenated_longidle ^ 100;
3665 /*longidle sample count */
3666 concatenated_delays = concatenated_delays ^ 243;
3667 /* trfc, worst case of 933Mhz 4Gb */
3668 concatenated_delays = concatenated_delays << 8;
3669 concatenated_delays = concatenated_delays ^ 14;
3670 /* trcd, worst case */
3671 concatenated_delays = concatenated_delays << 8;
3672 concatenated_delays = concatenated_delays ^ 10;
3673 /* vfifo wait */
3674 concatenated_delays = concatenated_delays << 8;
3675 concatenated_delays = concatenated_delays ^ 4;
3676 /* mux delay */
3677
3678 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_IDLE;
3679 concatenated_rw_addr = concatenated_rw_addr << 8;
3680 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_ACTIVATE_1;
3681 concatenated_rw_addr = concatenated_rw_addr << 8;
3682 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_SGLE_READ;
3683 concatenated_rw_addr = concatenated_rw_addr << 8;
3684 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_PRECHARGE_ALL;
3685
3686 concatenated_refresh = concatenated_refresh ^ RW_MGR_REFRESH_ALL;
3687 concatenated_refresh = concatenated_refresh << 24;
3688 concatenated_refresh = concatenated_refresh ^ 1000; /* trefi */
3689
3690 /* Initialize the register file with the correct data */
Marek Vasutb5450962015-07-12 21:05:08 +02003691 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
3692 writel(trk_sample_count, &sdr_reg_file->trk_sample_count);
3693 writel(concatenated_longidle, &sdr_reg_file->trk_longidle);
3694 writel(concatenated_delays, &sdr_reg_file->delays);
3695 writel(concatenated_rw_addr, &sdr_reg_file->trk_rw_mgr_addr);
3696 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH, &sdr_reg_file->trk_read_dqs_width);
3697 writel(concatenated_refresh, &sdr_reg_file->trk_rfsh);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003698}
3699
3700int sdram_calibration_full(void)
3701{
3702 struct param_type my_param;
3703 struct gbl_type my_gbl;
3704 uint32_t pass;
3705 uint32_t i;
3706
3707 param = &my_param;
3708 gbl = &my_gbl;
3709
3710 /* Initialize the debug mode flags */
3711 gbl->phy_debug_mode_flags = 0;
3712 /* Set the calibration enabled by default */
3713 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3714 /*
3715 * Only sweep all groups (regardless of fail state) by default
3716 * Set enabled read test by default.
3717 */
3718#if DISABLE_GUARANTEED_READ
3719 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3720#endif
3721 /* Initialize the register file */
3722 initialize_reg_file();
3723
3724 /* Initialize any PHY CSR */
3725 initialize_hps_phy();
3726
3727 scc_mgr_initialize();
3728
3729 initialize_tracking();
3730
3731 /* USER Enable all ranks, groups */
3732 for (i = 0; i < RW_MGR_MEM_NUMBER_OF_RANKS; i++)
3733 param->skip_ranks[i] = 0;
3734 for (i = 0; i < NUM_SHADOW_REGS; ++i)
3735 param->skip_shadow_regs[i] = 0;
3736 param->skip_groups = 0;
3737
3738 printf("%s: Preparing to start memory calibration\n", __FILE__);
3739
3740 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut6283b4c2015-07-13 01:05:27 +02003741 debug_cond(DLEVEL == 1,
3742 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3743 RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3744 RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3745 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3746 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3747 debug_cond(DLEVEL == 1,
3748 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3749 RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3750 RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3751 IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3752 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3753 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3754 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3755 IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3756 IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3757 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3758 IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3759 IO_IO_OUT2_DELAY_MAX);
3760 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3761 IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003762
3763 hc_initialize_rom_data();
3764
3765 /* update info for sims */
3766 reg_file_set_stage(CAL_STAGE_NIL);
3767 reg_file_set_group(0);
3768
3769 /*
3770 * Load global needed for those actions that require
3771 * some dynamic calibration support.
3772 */
3773 dyn_calib_steps = STATIC_CALIB_STEPS;
3774 /*
3775 * Load global to allow dynamic selection of delay loop settings
3776 * based on calibration mode.
3777 */
3778 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3779 skip_delay_mask = 0xff;
3780 else
3781 skip_delay_mask = 0x0;
3782
3783 pass = run_mem_calibrate();
3784
3785 printf("%s: Calibration complete\n", __FILE__);
3786 return pass;
3787}