blob: dd1ce8535f2a194bd0fc2c33379a4cacb8063212 [file] [log] [blame]
Dinh Nguyen3da42852015-06-02 22:52:49 -05001/*
2 * Copyright Altera Corporation (C) 2012-2015
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/sdram.h>
Marek Vasut04372fb2015-07-18 02:46:56 +020010#include <errno.h>
Dinh Nguyen3da42852015-06-02 22:52:49 -050011#include "sequencer.h"
12#include "sequencer_auto.h"
13#include "sequencer_auto_ac_init.h"
14#include "sequencer_auto_inst_init.h"
15#include "sequencer_defines.h"
16
Dinh Nguyen3da42852015-06-02 22:52:49 -050017static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020018 (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen3da42852015-06-02 22:52:49 -050019
20static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020021 (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050022
23static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasuta1c654a2015-07-12 18:31:05 +020024 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050025
26static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasute79025a2015-07-12 18:42:34 +020027 (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050028
29static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasut1bc6f142015-07-12 18:54:37 +020030 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050031
32static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasut1bc6f142015-07-12 18:54:37 +020033 (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen3da42852015-06-02 22:52:49 -050034
35static struct socfpga_data_mgr *data_mgr =
Marek Vasutc4815f72015-07-12 19:03:33 +020036 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050037
Marek Vasut6cb9f162015-07-12 20:49:39 +020038static struct socfpga_sdr_ctrl *sdr_ctrl =
39 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
40
Dinh Nguyen3da42852015-06-02 22:52:49 -050041#define DELTA_D 1
Dinh Nguyen3da42852015-06-02 22:52:49 -050042
43/*
44 * In order to reduce ROM size, most of the selectable calibration steps are
45 * decided at compile time based on the user's calibration mode selection,
46 * as captured by the STATIC_CALIB_STEPS selection below.
47 *
48 * However, to support simulation-time selection of fast simulation mode, where
49 * we skip everything except the bare minimum, we need a few of the steps to
50 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
51 * check, which is based on the rtl-supplied value, or we dynamically compute
52 * the value to use based on the dynamically-chosen calibration mode
53 */
54
55#define DLEVEL 0
56#define STATIC_IN_RTL_SIM 0
57#define STATIC_SKIP_DELAY_LOOPS 0
58
59#define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
60 STATIC_SKIP_DELAY_LOOPS)
61
62/* calibration steps requested by the rtl */
63uint16_t dyn_calib_steps;
64
65/*
66 * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
67 * instead of static, we use boolean logic to select between
68 * non-skip and skip values
69 *
70 * The mask is set to include all bits when not-skipping, but is
71 * zero when skipping
72 */
73
74uint16_t skip_delay_mask; /* mask off bits when skipping/not-skipping */
75
76#define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
77 ((non_skip_value) & skip_delay_mask)
78
79struct gbl_type *gbl;
80struct param_type *param;
81uint32_t curr_shadow_reg;
82
Dinh Nguyen3da42852015-06-02 22:52:49 -050083static void set_failing_group_stage(uint32_t group, uint32_t stage,
84 uint32_t substage)
85{
86 /*
87 * Only set the global stage if there was not been any other
88 * failing group
89 */
90 if (gbl->error_stage == CAL_STAGE_NIL) {
91 gbl->error_substage = substage;
92 gbl->error_stage = stage;
93 gbl->error_group = group;
94 }
95}
96
Marek Vasut2c0d2d92015-07-12 21:10:24 +020097static void reg_file_set_group(u16 set_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -050098{
Marek Vasut2c0d2d92015-07-12 21:10:24 +020099 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500100}
101
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200102static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500103{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200104 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500105}
106
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200107static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500108{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200109 set_sub_stage &= 0xff;
110 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500111}
112
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200113/**
114 * phy_mgr_initialize() - Initialize PHY Manager
115 *
116 * Initialize PHY Manager.
117 */
Marek Vasut9fa9c902015-07-17 01:12:07 +0200118static void phy_mgr_initialize(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500119{
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200120 u32 ratio;
121
Dinh Nguyen3da42852015-06-02 22:52:49 -0500122 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200123 /* Calibration has control over path to memory */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500124 /*
125 * In Hard PHY this is a 2-bit control:
126 * 0: AFI Mux Select
127 * 1: DDIO Mux Select
128 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200129 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500130
131 /* USER memory clock is not stable we begin initialization */
Marek Vasut1273dd92015-07-12 21:05:08 +0200132 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500133
134 /* USER calibration status all set to zero */
Marek Vasut1273dd92015-07-12 21:05:08 +0200135 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500136
Marek Vasut1273dd92015-07-12 21:05:08 +0200137 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500138
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200139 /* Init params only if we do NOT skip calibration. */
140 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
141 return;
142
143 ratio = RW_MGR_MEM_DQ_PER_READ_DQS /
144 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS;
145 param->read_correct_mask_vg = (1 << ratio) - 1;
146 param->write_correct_mask_vg = (1 << ratio) - 1;
147 param->read_correct_mask = (1 << RW_MGR_MEM_DQ_PER_READ_DQS) - 1;
148 param->write_correct_mask = (1 << RW_MGR_MEM_DQ_PER_WRITE_DQS) - 1;
149 ratio = RW_MGR_MEM_DATA_WIDTH /
150 RW_MGR_MEM_DATA_MASK_WIDTH;
151 param->dm_correct_mask = (1 << ratio) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500152}
153
Marek Vasut080bf642015-07-20 08:15:57 +0200154/**
155 * set_rank_and_odt_mask() - Set Rank and ODT mask
156 * @rank: Rank mask
157 * @odt_mode: ODT mode, OFF or READ_WRITE
158 *
159 * Set Rank and ODT mask (On-Die Termination).
160 */
Marek Vasutb2dfd102015-07-20 08:03:11 +0200161static void set_rank_and_odt_mask(const u32 rank, const u32 odt_mode)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500162{
Marek Vasutb2dfd102015-07-20 08:03:11 +0200163 u32 odt_mask_0 = 0;
164 u32 odt_mask_1 = 0;
165 u32 cs_and_odt_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500166
Marek Vasutb2dfd102015-07-20 08:03:11 +0200167 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
168 odt_mask_0 = 0x0;
169 odt_mask_1 = 0x0;
170 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
Marek Vasut287cdf62015-07-20 08:09:05 +0200171 switch (RW_MGR_MEM_NUMBER_OF_RANKS) {
172 case 1: /* 1 Rank */
173 /* Read: ODT = 0 ; Write: ODT = 1 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500174 odt_mask_0 = 0x0;
175 odt_mask_1 = 0x1;
Marek Vasut287cdf62015-07-20 08:09:05 +0200176 break;
177 case 2: /* 2 Ranks */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500178 if (RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM == 1) {
Marek Vasut080bf642015-07-20 08:15:57 +0200179 /*
180 * - Dual-Slot , Single-Rank (1 CS per DIMM)
181 * OR
182 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
183 *
184 * Since MEM_NUMBER_OF_RANKS is 2, they
185 * are both single rank with 2 CS each
186 * (special for RDIMM).
187 *
Dinh Nguyen3da42852015-06-02 22:52:49 -0500188 * Read: Turn on ODT on the opposite rank
189 * Write: Turn on ODT on all ranks
190 */
191 odt_mask_0 = 0x3 & ~(1 << rank);
192 odt_mask_1 = 0x3;
193 } else {
194 /*
Marek Vasut080bf642015-07-20 08:15:57 +0200195 * - Single-Slot , Dual-Rank (2 CS per DIMM)
196 *
197 * Read: Turn on ODT off on all ranks
198 * Write: Turn on ODT on active rank
Dinh Nguyen3da42852015-06-02 22:52:49 -0500199 */
200 odt_mask_0 = 0x0;
201 odt_mask_1 = 0x3 & (1 << rank);
202 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200203 break;
204 case 4: /* 4 Ranks */
205 /* Read:
Dinh Nguyen3da42852015-06-02 22:52:49 -0500206 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500207 * | ODT |
208 * Read From +-----------------------+
209 * Rank | 3 | 2 | 1 | 0 |
210 * ----------+-----+-----+-----+-----+
211 * 0 | 0 | 1 | 0 | 0 |
212 * 1 | 1 | 0 | 0 | 0 |
213 * 2 | 0 | 0 | 0 | 1 |
214 * 3 | 0 | 0 | 1 | 0 |
215 * ----------+-----+-----+-----+-----+
216 *
217 * Write:
218 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500219 * | ODT |
220 * Write To +-----------------------+
221 * Rank | 3 | 2 | 1 | 0 |
222 * ----------+-----+-----+-----+-----+
223 * 0 | 0 | 1 | 0 | 1 |
224 * 1 | 1 | 0 | 1 | 0 |
225 * 2 | 0 | 1 | 0 | 1 |
226 * 3 | 1 | 0 | 1 | 0 |
227 * ----------+-----+-----+-----+-----+
228 */
229 switch (rank) {
230 case 0:
231 odt_mask_0 = 0x4;
232 odt_mask_1 = 0x5;
233 break;
234 case 1:
235 odt_mask_0 = 0x8;
236 odt_mask_1 = 0xA;
237 break;
238 case 2:
239 odt_mask_0 = 0x1;
240 odt_mask_1 = 0x5;
241 break;
242 case 3:
243 odt_mask_0 = 0x2;
244 odt_mask_1 = 0xA;
245 break;
246 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200247 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500248 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500249 }
250
Marek Vasutb2dfd102015-07-20 08:03:11 +0200251 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
252 ((0xFF & odt_mask_0) << 8) |
253 ((0xFF & odt_mask_1) << 16);
Marek Vasut1273dd92015-07-12 21:05:08 +0200254 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
255 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500256}
257
Marek Vasutc76976d2015-07-12 22:28:33 +0200258/**
259 * scc_mgr_set() - Set SCC Manager register
260 * @off: Base offset in SCC Manager space
261 * @grp: Read/Write group
262 * @val: Value to be set
263 *
264 * This function sets the SCC Manager (Scan Chain Control Manager) register.
265 */
266static void scc_mgr_set(u32 off, u32 grp, u32 val)
267{
268 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
269}
270
Marek Vasute893f4d2015-07-20 07:16:42 +0200271/**
272 * scc_mgr_initialize() - Initialize SCC Manager registers
273 *
274 * Initialize SCC Manager registers.
275 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500276static void scc_mgr_initialize(void)
277{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500278 /*
Marek Vasute893f4d2015-07-20 07:16:42 +0200279 * Clear register file for HPS. 16 (2^4) is the size of the
280 * full register file in the scc mgr:
281 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
282 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500283 */
Marek Vasutc76976d2015-07-12 22:28:33 +0200284 int i;
Marek Vasute893f4d2015-07-20 07:16:42 +0200285
Dinh Nguyen3da42852015-06-02 22:52:49 -0500286 for (i = 0; i < 16; i++) {
Marek Vasut7ac40d22015-06-26 18:56:54 +0200287 debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -0500288 __func__, __LINE__, i);
Marek Vasutc76976d2015-07-12 22:28:33 +0200289 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, 0, i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500290 }
291}
292
Marek Vasut5ff825b2015-07-12 22:11:55 +0200293static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
294{
Marek Vasutc76976d2015-07-12 22:28:33 +0200295 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200296}
297
298static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500299{
Marek Vasutc76976d2015-07-12 22:28:33 +0200300 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500301}
302
Dinh Nguyen3da42852015-06-02 22:52:49 -0500303static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
304{
Marek Vasutc76976d2015-07-12 22:28:33 +0200305 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500306}
307
Marek Vasut5ff825b2015-07-12 22:11:55 +0200308static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
309{
Marek Vasutc76976d2015-07-12 22:28:33 +0200310 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200311}
312
Marek Vasut32675242015-07-17 06:07:13 +0200313static void scc_mgr_set_dqs_io_in_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200314{
Marek Vasutc76976d2015-07-12 22:28:33 +0200315 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
316 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200317}
318
319static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
320{
Marek Vasutc76976d2015-07-12 22:28:33 +0200321 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200322}
323
324static void scc_mgr_set_dq_out1_delay(uint32_t dq_in_group, uint32_t delay)
325{
Marek Vasutc76976d2015-07-12 22:28:33 +0200326 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200327}
328
Marek Vasut32675242015-07-17 06:07:13 +0200329static void scc_mgr_set_dqs_out1_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200330{
Marek Vasutc76976d2015-07-12 22:28:33 +0200331 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, RW_MGR_MEM_DQ_PER_WRITE_DQS,
332 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200333}
334
335static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
336{
Marek Vasutc76976d2015-07-12 22:28:33 +0200337 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
338 RW_MGR_MEM_DQ_PER_WRITE_DQS + 1 + dm,
339 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200340}
341
342/* load up dqs config settings */
343static void scc_mgr_load_dqs(uint32_t dqs)
344{
345 writel(dqs, &sdr_scc_mgr->dqs_ena);
346}
347
348/* load up dqs io config settings */
349static void scc_mgr_load_dqs_io(void)
350{
351 writel(0, &sdr_scc_mgr->dqs_io_ena);
352}
353
354/* load up dq config settings */
355static void scc_mgr_load_dq(uint32_t dq_in_group)
356{
357 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
358}
359
360/* load up dm config settings */
361static void scc_mgr_load_dm(uint32_t dm)
362{
363 writel(dm, &sdr_scc_mgr->dm_ena);
364}
365
Marek Vasut0b69b802015-07-12 23:25:21 +0200366/**
367 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
368 * @off: Base offset in SCC Manager space
369 * @grp: Read/Write group
370 * @val: Value to be set
371 * @update: If non-zero, trigger SCC Manager update for all ranks
372 *
373 * This function sets the SCC Manager (Scan Chain Control Manager) register
374 * and optionally triggers the SCC update for all ranks.
375 */
376static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
377 const int update)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500378{
Marek Vasut0b69b802015-07-12 23:25:21 +0200379 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500380
381 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
382 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut0b69b802015-07-12 23:25:21 +0200383 scc_mgr_set(off, grp, val);
Marek Vasut162d60e2015-07-12 23:14:33 +0200384
Marek Vasut0b69b802015-07-12 23:25:21 +0200385 if (update || (r == 0)) {
386 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasut1273dd92015-07-12 21:05:08 +0200387 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500388 }
389 }
390}
391
Marek Vasut0b69b802015-07-12 23:25:21 +0200392static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
393{
394 /*
395 * USER although the h/w doesn't support different phases per
396 * shadow register, for simplicity our scc manager modeling
397 * keeps different phase settings per shadow reg, and it's
398 * important for us to keep them in sync to match h/w.
399 * for efficiency, the scan chain update should occur only
400 * once to sr0.
401 */
402 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
403 read_group, phase, 0);
404}
405
Dinh Nguyen3da42852015-06-02 22:52:49 -0500406static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
407 uint32_t phase)
408{
Marek Vasut0b69b802015-07-12 23:25:21 +0200409 /*
410 * USER although the h/w doesn't support different phases per
411 * shadow register, for simplicity our scc manager modeling
412 * keeps different phase settings per shadow reg, and it's
413 * important for us to keep them in sync to match h/w.
414 * for efficiency, the scan chain update should occur only
415 * once to sr0.
416 */
417 scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
418 write_group, phase, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500419}
420
Dinh Nguyen3da42852015-06-02 22:52:49 -0500421static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
422 uint32_t delay)
423{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500424 /*
425 * In shadow register mode, the T11 settings are stored in
426 * registers in the core, which are updated by the DQS_ENA
427 * signals. Not issuing the SCC_MGR_UPD command allows us to
428 * save lots of rank switching overhead, by calling
429 * select_shadow_regs_for_update with update_scan_chains
430 * set to 0.
431 */
Marek Vasut0b69b802015-07-12 23:25:21 +0200432 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
433 read_group, delay, 1);
Marek Vasut1273dd92015-07-12 21:05:08 +0200434 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500435}
436
Marek Vasut5be355c2015-07-12 23:39:06 +0200437/**
438 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
439 * @write_group: Write group
440 * @delay: Delay value
441 *
442 * This function sets the OCT output delay in SCC manager.
443 */
444static void scc_mgr_set_oct_out1_delay(const u32 write_group, const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500445{
Marek Vasut5be355c2015-07-12 23:39:06 +0200446 const int ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
447 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
448 const int base = write_group * ratio;
449 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500450 /*
451 * Load the setting in the SCC manager
452 * Although OCT affects only write data, the OCT delay is controlled
453 * by the DQS logic block which is instantiated once per read group.
454 * For protocols where a write group consists of multiple read groups,
455 * the setting must be set multiple times.
456 */
Marek Vasut5be355c2015-07-12 23:39:06 +0200457 for (i = 0; i < ratio; i++)
458 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500459}
460
Marek Vasut37a37ca2015-07-19 01:32:55 +0200461/**
462 * scc_mgr_set_hhp_extras() - Set HHP extras.
463 *
464 * Load the fixed setting in the SCC manager HHP extras.
465 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500466static void scc_mgr_set_hhp_extras(void)
467{
468 /*
469 * Load the fixed setting in the SCC manager
Marek Vasut37a37ca2015-07-19 01:32:55 +0200470 * bits: 0:0 = 1'b1 - DQS bypass
471 * bits: 1:1 = 1'b1 - DQ bypass
472 * bits: 4:2 = 3'b001 - rfifo_mode
473 * bits: 6:5 = 2'b01 - rfifo clock_select
474 * bits: 7:7 = 1'b0 - separate gating from ungating setting
475 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen3da42852015-06-02 22:52:49 -0500476 */
Marek Vasut37a37ca2015-07-19 01:32:55 +0200477 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
478 (1 << 2) | (1 << 1) | (1 << 0);
479 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
480 SCC_MGR_HHP_GLOBALS_OFFSET |
481 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500482
Marek Vasut37a37ca2015-07-19 01:32:55 +0200483 debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n",
484 __func__, __LINE__);
485 writel(value, addr);
486 debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
487 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500488}
489
Marek Vasutf42af352015-07-20 04:41:53 +0200490/**
491 * scc_mgr_zero_all() - Zero all DQS config
492 *
493 * Zero all DQS config.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500494 */
495static void scc_mgr_zero_all(void)
496{
Marek Vasutf42af352015-07-20 04:41:53 +0200497 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500498
499 /*
500 * USER Zero all DQS config settings, across all groups and all
501 * shadow registers
502 */
Marek Vasutf42af352015-07-20 04:41:53 +0200503 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
504 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500505 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
506 /*
507 * The phases actually don't exist on a per-rank basis,
508 * but there's no harm updating them several times, so
509 * let's keep the code simple.
510 */
511 scc_mgr_set_dqs_bus_in_delay(i, IO_DQS_IN_RESERVE);
512 scc_mgr_set_dqs_en_phase(i, 0);
513 scc_mgr_set_dqs_en_delay(i, 0);
514 }
515
516 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
517 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasutf42af352015-07-20 04:41:53 +0200518 /* Arria V/Cyclone V don't have out2. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500519 scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
520 }
521 }
522
Marek Vasutf42af352015-07-20 04:41:53 +0200523 /* Multicast to all DQS group enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200524 writel(0xff, &sdr_scc_mgr->dqs_ena);
525 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500526}
527
Marek Vasutc5c5f532015-07-17 02:06:20 +0200528/**
529 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
530 * @write_group: Write group
531 *
532 * Set bypass mode and trigger SCC update.
533 */
534static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500535{
Marek Vasutc5c5f532015-07-17 02:06:20 +0200536 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200537 writel(0xff, &sdr_scc_mgr->dq_ena);
538 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500539
Marek Vasutc5c5f532015-07-17 02:06:20 +0200540 /* Update current DQS IO enable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200541 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500542
Marek Vasutc5c5f532015-07-17 02:06:20 +0200543 /* Update the DQS logic. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200544 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500545
Marek Vasutc5c5f532015-07-17 02:06:20 +0200546 /* Hit update. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200547 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500548}
549
Marek Vasut5e837892015-07-13 00:30:09 +0200550/**
551 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
552 * @write_group: Write group
553 *
554 * Load DQS settings for Write Group, do not trigger SCC update.
555 */
556static void scc_mgr_load_dqs_for_write_group(const u32 write_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200557{
Marek Vasut5e837892015-07-13 00:30:09 +0200558 const int ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
559 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
560 const int base = write_group * ratio;
561 int i;
Marek Vasut5ff825b2015-07-12 22:11:55 +0200562 /*
Marek Vasut5e837892015-07-13 00:30:09 +0200563 * Load the setting in the SCC manager
Marek Vasut5ff825b2015-07-12 22:11:55 +0200564 * Although OCT affects only write data, the OCT delay is controlled
565 * by the DQS logic block which is instantiated once per read group.
566 * For protocols where a write group consists of multiple read groups,
Marek Vasut5e837892015-07-13 00:30:09 +0200567 * the setting must be set multiple times.
Marek Vasut5ff825b2015-07-12 22:11:55 +0200568 */
Marek Vasut5e837892015-07-13 00:30:09 +0200569 for (i = 0; i < ratio; i++)
570 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200571}
572
Marek Vasutd41ea932015-07-20 08:41:04 +0200573/**
574 * scc_mgr_zero_group() - Zero all configs for a group
575 *
576 * Zero DQ, DM, DQS and OCT configs for a group.
577 */
578static void scc_mgr_zero_group(const u32 write_group, const int out_only)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500579{
Marek Vasutd41ea932015-07-20 08:41:04 +0200580 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500581
Marek Vasutd41ea932015-07-20 08:41:04 +0200582 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
583 r += NUM_RANKS_PER_SHADOW_REG) {
584 /* Zero all DQ config settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500585 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200586 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500587 if (!out_only)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200588 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500589 }
590
Marek Vasutd41ea932015-07-20 08:41:04 +0200591 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200592 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500593
Marek Vasutd41ea932015-07-20 08:41:04 +0200594 /* Zero all DM config settings. */
595 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200596 scc_mgr_set_dm_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500597
Marek Vasutd41ea932015-07-20 08:41:04 +0200598 /* Multicast to all DM enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200599 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500600
Marek Vasutd41ea932015-07-20 08:41:04 +0200601 /* Zero all DQS IO settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500602 if (!out_only)
Marek Vasut32675242015-07-17 06:07:13 +0200603 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasutd41ea932015-07-20 08:41:04 +0200604
605 /* Arria V/Cyclone V don't have out2. */
Marek Vasut32675242015-07-17 06:07:13 +0200606 scc_mgr_set_dqs_out1_delay(IO_DQS_OUT_RESERVE);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500607 scc_mgr_set_oct_out1_delay(write_group, IO_DQS_OUT_RESERVE);
608 scc_mgr_load_dqs_for_write_group(write_group);
609
Marek Vasutd41ea932015-07-20 08:41:04 +0200610 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasut1273dd92015-07-12 21:05:08 +0200611 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500612
Marek Vasutd41ea932015-07-20 08:41:04 +0200613 /* Hit update to zero everything. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200614 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500615 }
616}
617
Dinh Nguyen3da42852015-06-02 22:52:49 -0500618/*
619 * apply and load a particular input delay for the DQ pins in a group
620 * group_bgn is the index of the first dq pin (in the write group)
621 */
Marek Vasut32675242015-07-17 06:07:13 +0200622static void scc_mgr_apply_group_dq_in_delay(uint32_t group_bgn, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500623{
624 uint32_t i, p;
625
626 for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200627 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500628 scc_mgr_load_dq(p);
629 }
630}
631
Marek Vasut300c2e62015-07-17 05:42:49 +0200632/**
633 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the DQ pins in a group
634 * @delay: Delay value
635 *
636 * Apply and load a particular output delay for the DQ pins in a group.
637 */
638static void scc_mgr_apply_group_dq_out1_delay(const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500639{
Marek Vasut300c2e62015-07-17 05:42:49 +0200640 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500641
Marek Vasut300c2e62015-07-17 05:42:49 +0200642 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
643 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500644 scc_mgr_load_dq(i);
645 }
646}
647
648/* apply and load a particular output delay for the DM pins in a group */
Marek Vasut32675242015-07-17 06:07:13 +0200649static void scc_mgr_apply_group_dm_out1_delay(uint32_t delay1)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500650{
651 uint32_t i;
652
653 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200654 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500655 scc_mgr_load_dm(i);
656 }
657}
658
659
660/* apply and load delay on both DQS and OCT out1 */
661static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
662 uint32_t delay)
663{
Marek Vasut32675242015-07-17 06:07:13 +0200664 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500665 scc_mgr_load_dqs_io();
666
667 scc_mgr_set_oct_out1_delay(write_group, delay);
668 scc_mgr_load_dqs_for_write_group(write_group);
669}
670
Marek Vasut5cb1b502015-07-17 05:33:28 +0200671/**
672 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side: DQ, DM, DQS, OCT
673 * @write_group: Write group
674 * @delay: Delay value
675 *
676 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
677 */
Marek Vasut8eccde32015-07-17 05:30:14 +0200678static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
Marek Vasut8eccde32015-07-17 05:30:14 +0200679 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500680{
Marek Vasut8eccde32015-07-17 05:30:14 +0200681 u32 i, new_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500682
Marek Vasut8eccde32015-07-17 05:30:14 +0200683 /* DQ shift */
684 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500685 scc_mgr_load_dq(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500686
Marek Vasut8eccde32015-07-17 05:30:14 +0200687 /* DM shift */
688 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500689 scc_mgr_load_dm(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500690
Marek Vasut5cb1b502015-07-17 05:33:28 +0200691 /* DQS shift */
692 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500693 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200694 debug_cond(DLEVEL == 1,
695 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
696 __func__, __LINE__, write_group, delay, new_delay,
697 IO_IO_OUT2_DELAY_MAX,
Dinh Nguyen3da42852015-06-02 22:52:49 -0500698 new_delay - IO_IO_OUT2_DELAY_MAX);
Marek Vasut5cb1b502015-07-17 05:33:28 +0200699 new_delay -= IO_IO_OUT2_DELAY_MAX;
700 scc_mgr_set_dqs_out1_delay(new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500701 }
702
703 scc_mgr_load_dqs_io();
704
Marek Vasut5cb1b502015-07-17 05:33:28 +0200705 /* OCT shift */
706 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500707 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200708 debug_cond(DLEVEL == 1,
709 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
710 __func__, __LINE__, write_group, delay,
711 new_delay, IO_IO_OUT2_DELAY_MAX,
Dinh Nguyen3da42852015-06-02 22:52:49 -0500712 new_delay - IO_IO_OUT2_DELAY_MAX);
Marek Vasut5cb1b502015-07-17 05:33:28 +0200713 new_delay -= IO_IO_OUT2_DELAY_MAX;
714 scc_mgr_set_oct_out1_delay(write_group, new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500715 }
716
717 scc_mgr_load_dqs_for_write_group(write_group);
718}
719
Marek Vasutf51a7d32015-07-19 02:18:21 +0200720/**
721 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side to all ranks
722 * @write_group: Write group
723 * @delay: Delay value
724 *
725 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500726 */
Marek Vasutf51a7d32015-07-19 02:18:21 +0200727static void
728scc_mgr_apply_group_all_out_delay_add_all_ranks(const u32 write_group,
729 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500730{
Marek Vasutf51a7d32015-07-19 02:18:21 +0200731 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500732
733 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
Marek Vasutf51a7d32015-07-19 02:18:21 +0200734 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200735 scc_mgr_apply_group_all_out_delay_add(write_group, delay);
Marek Vasut1273dd92015-07-12 21:05:08 +0200736 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500737 }
738}
739
Marek Vasutf936f942015-07-26 11:07:19 +0200740/**
741 * set_jump_as_return() - Return instruction optimization
742 *
743 * Optimization used to recover some slots in ddr3 inst_rom could be
744 * applied to other protocols if we wanted to
745 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500746static void set_jump_as_return(void)
747{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500748 /*
Marek Vasutf936f942015-07-26 11:07:19 +0200749 * To save space, we replace return with jump to special shared
Dinh Nguyen3da42852015-06-02 22:52:49 -0500750 * RETURN instruction so we set the counter to large value so that
Marek Vasutf936f942015-07-26 11:07:19 +0200751 * we always jump.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500752 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200753 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
754 writel(RW_MGR_RETURN, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500755}
756
757/*
758 * should always use constants as argument to ensure all computations are
759 * performed at compile time
760 */
761static void delay_for_n_mem_clocks(const uint32_t clocks)
762{
763 uint32_t afi_clocks;
764 uint8_t inner = 0;
765 uint8_t outer = 0;
766 uint16_t c_loop = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500767
768 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
769
770
771 afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
772 /* scale (rounding up) to get afi clocks */
773
774 /*
775 * Note, we don't bother accounting for being off a little bit
776 * because of a few extra instructions in outer loops
777 * Note, the loops have a test at the end, and do the test before
778 * the decrement, and so always perform the loop
779 * 1 time more than the counter value
780 */
781 if (afi_clocks == 0) {
782 ;
783 } else if (afi_clocks <= 0x100) {
784 inner = afi_clocks-1;
785 outer = 0;
786 c_loop = 0;
787 } else if (afi_clocks <= 0x10000) {
788 inner = 0xff;
789 outer = (afi_clocks-1) >> 8;
790 c_loop = 0;
791 } else {
792 inner = 0xff;
793 outer = 0xff;
794 c_loop = (afi_clocks-1) >> 16;
795 }
796
797 /*
798 * rom instructions are structured as follows:
799 *
800 * IDLE_LOOP2: jnz cntr0, TARGET_A
801 * IDLE_LOOP1: jnz cntr1, TARGET_B
802 * return
803 *
804 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
805 * TARGET_B is set to IDLE_LOOP2 as well
806 *
807 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
808 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
809 *
810 * a little confusing, but it helps save precious space in the inst_rom
811 * and sequencer rom and keeps the delays more accurate and reduces
812 * overhead
813 */
814 if (afi_clocks <= 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200815 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
816 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500817
Marek Vasut1273dd92015-07-12 21:05:08 +0200818 writel(RW_MGR_IDLE_LOOP1,
819 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500820
Marek Vasut1273dd92015-07-12 21:05:08 +0200821 writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
822 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500823 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200824 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
825 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500826
Marek Vasut1273dd92015-07-12 21:05:08 +0200827 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
828 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500829
Marek Vasut1273dd92015-07-12 21:05:08 +0200830 writel(RW_MGR_IDLE_LOOP2,
831 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500832
Marek Vasut1273dd92015-07-12 21:05:08 +0200833 writel(RW_MGR_IDLE_LOOP2,
834 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500835
836 /* hack to get around compiler not being smart enough */
837 if (afi_clocks <= 0x10000) {
838 /* only need to run once */
Marek Vasut1273dd92015-07-12 21:05:08 +0200839 writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
840 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500841 } else {
842 do {
Marek Vasut1273dd92015-07-12 21:05:08 +0200843 writel(RW_MGR_IDLE_LOOP2,
844 SDR_PHYGRP_RWMGRGRP_ADDRESS |
845 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500846 } while (c_loop-- != 0);
847 }
848 }
849 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
850}
851
Marek Vasut944fe712015-07-13 00:44:30 +0200852/**
853 * rw_mgr_mem_init_load_regs() - Load instruction registers
854 * @cntr0: Counter 0 value
855 * @cntr1: Counter 1 value
856 * @cntr2: Counter 2 value
857 * @jump: Jump instruction value
858 *
859 * Load instruction registers.
860 */
861static void rw_mgr_mem_init_load_regs(u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
862{
863 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
864 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
865
866 /* Load counters */
867 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
868 &sdr_rw_load_mgr_regs->load_cntr0);
869 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
870 &sdr_rw_load_mgr_regs->load_cntr1);
871 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
872 &sdr_rw_load_mgr_regs->load_cntr2);
873
874 /* Load jump address */
875 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
876 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
877 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
878
879 /* Execute count instruction */
880 writel(jump, grpaddr);
881}
882
Marek Vasutecd23342015-07-13 00:51:05 +0200883/**
884 * rw_mgr_mem_load_user() - Load user calibration values
885 * @fin1: Final instruction 1
886 * @fin2: Final instruction 2
887 * @precharge: If 1, precharge the banks at the end
888 *
889 * Load user calibration values and optionally precharge the banks.
890 */
891static void rw_mgr_mem_load_user(const u32 fin1, const u32 fin2,
892 const int precharge)
893{
894 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
895 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
896 u32 r;
897
898 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
899 if (param->skip_ranks[r]) {
900 /* request to skip the rank */
901 continue;
902 }
903
904 /* set rank */
905 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
906
907 /* precharge all banks ... */
908 if (precharge)
909 writel(RW_MGR_PRECHARGE_ALL, grpaddr);
910
911 /*
912 * USER Use Mirror-ed commands for odd ranks if address
913 * mirrorring is on
914 */
915 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
916 set_jump_as_return();
917 writel(RW_MGR_MRS2_MIRR, grpaddr);
918 delay_for_n_mem_clocks(4);
919 set_jump_as_return();
920 writel(RW_MGR_MRS3_MIRR, grpaddr);
921 delay_for_n_mem_clocks(4);
922 set_jump_as_return();
923 writel(RW_MGR_MRS1_MIRR, grpaddr);
924 delay_for_n_mem_clocks(4);
925 set_jump_as_return();
926 writel(fin1, grpaddr);
927 } else {
928 set_jump_as_return();
929 writel(RW_MGR_MRS2, grpaddr);
930 delay_for_n_mem_clocks(4);
931 set_jump_as_return();
932 writel(RW_MGR_MRS3, grpaddr);
933 delay_for_n_mem_clocks(4);
934 set_jump_as_return();
935 writel(RW_MGR_MRS1, grpaddr);
936 set_jump_as_return();
937 writel(fin2, grpaddr);
938 }
939
940 if (precharge)
941 continue;
942
943 set_jump_as_return();
944 writel(RW_MGR_ZQCL, grpaddr);
945
946 /* tZQinit = tDLLK = 512 ck cycles */
947 delay_for_n_mem_clocks(512);
948 }
949}
950
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200951/**
952 * rw_mgr_mem_initialize() - Initialize RW Manager
953 *
954 * Initialize RW Manager.
955 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500956static void rw_mgr_mem_initialize(void)
957{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500958 debug("%s:%d\n", __func__, __LINE__);
959
960 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200961 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
962 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500963
964 /*
965 * Here's how you load register for a loop
966 * Counters are located @ 0x800
967 * Jump address are located @ 0xC00
968 * For both, registers 0 to 3 are selected using bits 3 and 2, like
969 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
970 * I know this ain't pretty, but Avalon bus throws away the 2 least
971 * significant bits
972 */
973
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200974 /* Start with memory RESET activated */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500975
976 /* tINIT = 200us */
977
978 /*
979 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
980 * If a and b are the number of iteration in 2 nested loops
981 * it takes the following number of cycles to complete the operation:
982 * number_of_cycles = ((2 + n) * a + 2) * b
983 * where n is the number of instruction in the inner loop
984 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
985 * b = 6A
986 */
Marek Vasut944fe712015-07-13 00:44:30 +0200987 rw_mgr_mem_init_load_regs(SEQ_TINIT_CNTR0_VAL, SEQ_TINIT_CNTR1_VAL,
988 SEQ_TINIT_CNTR2_VAL,
989 RW_MGR_INIT_RESET_0_CKE_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500990
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200991 /* Indicate that memory is stable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200992 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500993
994 /*
995 * transition the RESET to high
996 * Wait for 500us
997 */
998
999 /*
1000 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
1001 * If a and b are the number of iteration in 2 nested loops
1002 * it takes the following number of cycles to complete the operation
1003 * number_of_cycles = ((2 + n) * a + 2) * b
1004 * where n is the number of instruction in the inner loop
1005 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
1006 * b = FF
1007 */
Marek Vasut944fe712015-07-13 00:44:30 +02001008 rw_mgr_mem_init_load_regs(SEQ_TRESET_CNTR0_VAL, SEQ_TRESET_CNTR1_VAL,
1009 SEQ_TRESET_CNTR2_VAL,
1010 RW_MGR_INIT_RESET_1_CKE_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001011
Marek Vasut8e9d7d02015-07-26 10:57:06 +02001012 /* Bring up clock enable. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001013
1014 /* tXRP < 250 ck cycles */
1015 delay_for_n_mem_clocks(250);
1016
Marek Vasutecd23342015-07-13 00:51:05 +02001017 rw_mgr_mem_load_user(RW_MGR_MRS0_DLL_RESET_MIRR, RW_MGR_MRS0_DLL_RESET,
1018 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001019}
1020
1021/*
1022 * At the end of calibration we have to program the user settings in, and
1023 * USER hand off the memory to the user.
1024 */
1025static void rw_mgr_mem_handoff(void)
1026{
Marek Vasutecd23342015-07-13 00:51:05 +02001027 rw_mgr_mem_load_user(RW_MGR_MRS0_USER_MIRR, RW_MGR_MRS0_USER, 1);
1028 /*
1029 * USER need to wait tMOD (12CK or 15ns) time before issuing
1030 * other commands, but we will have plenty of NIOS cycles before
1031 * actual handoff so its okay.
1032 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001033}
1034
Marek Vasutad64769c2015-07-21 05:43:37 +02001035/*
1036 * issue write test command.
1037 * two variants are provided. one that just tests a write pattern and
1038 * another that tests datamask functionality.
1039 */
1040static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
1041 uint32_t test_dm)
1042{
1043 uint32_t mcc_instruction;
1044 uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
1045 ENABLE_SUPER_QUICK_CALIBRATION);
1046 uint32_t rw_wl_nop_cycles;
1047 uint32_t addr;
1048
1049 /*
1050 * Set counter and jump addresses for the right
1051 * number of NOP cycles.
1052 * The number of supported NOP cycles can range from -1 to infinity
1053 * Three different cases are handled:
1054 *
1055 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1056 * mechanism will be used to insert the right number of NOPs
1057 *
1058 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1059 * issuing the write command will jump straight to the
1060 * micro-instruction that turns on DQS (for DDRx), or outputs write
1061 * data (for RLD), skipping
1062 * the NOP micro-instruction all together
1063 *
1064 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1065 * turned on in the same micro-instruction that issues the write
1066 * command. Then we need
1067 * to directly jump to the micro-instruction that sends out the data
1068 *
1069 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1070 * (2 and 3). One jump-counter (0) is used to perform multiple
1071 * write-read operations.
1072 * one counter left to issue this command in "multiple-group" mode
1073 */
1074
1075 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
1076
1077 if (rw_wl_nop_cycles == -1) {
1078 /*
1079 * CNTR 2 - We want to execute the special write operation that
1080 * turns on DQS right away and then skip directly to the
1081 * instruction that sends out the data. We set the counter to a
1082 * large number so that the jump is always taken.
1083 */
1084 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1085
1086 /* CNTR 3 - Not used */
1087 if (test_dm) {
1088 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
1089 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
1090 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1091 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
1092 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1093 } else {
1094 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
1095 writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
1096 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1097 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
1098 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1099 }
1100 } else if (rw_wl_nop_cycles == 0) {
1101 /*
1102 * CNTR 2 - We want to skip the NOP operation and go straight
1103 * to the DQS enable instruction. We set the counter to a large
1104 * number so that the jump is always taken.
1105 */
1106 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1107
1108 /* CNTR 3 - Not used */
1109 if (test_dm) {
1110 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
1111 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
1112 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1113 } else {
1114 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
1115 writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
1116 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1117 }
1118 } else {
1119 /*
1120 * CNTR 2 - In this case we want to execute the next instruction
1121 * and NOT take the jump. So we set the counter to 0. The jump
1122 * address doesn't count.
1123 */
1124 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1125 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1126
1127 /*
1128 * CNTR 3 - Set the nop counter to the number of cycles we
1129 * need to loop for, minus 1.
1130 */
1131 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1132 if (test_dm) {
1133 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
1134 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
1135 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1136 } else {
1137 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
1138 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
1139 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1140 }
1141 }
1142
1143 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1144 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1145
1146 if (quick_write_mode)
1147 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1148 else
1149 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1150
1151 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1152
1153 /*
1154 * CNTR 1 - This is used to ensure enough time elapses
1155 * for read data to come back.
1156 */
1157 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1158
1159 if (test_dm) {
1160 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
1161 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1162 } else {
1163 writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
1164 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1165 }
1166
1167 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1168 writel(mcc_instruction, addr + (group << 2));
1169}
1170
1171/* Test writes, can check for a single bit pass or multiple bit pass */
Marek Vasutb9452ea2015-07-21 05:54:39 +02001172static int
1173rw_mgr_mem_calibrate_write_test(const u32 rank_bgn, const u32 write_group,
1174 const u32 use_dm, const u32 all_correct,
1175 u32 *bit_chk, const u32 all_ranks)
Marek Vasutad64769c2015-07-21 05:43:37 +02001176{
Marek Vasutb9452ea2015-07-21 05:54:39 +02001177 const u32 rank_end = all_ranks ?
1178 RW_MGR_MEM_NUMBER_OF_RANKS :
1179 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1180 const u32 shift_ratio = RW_MGR_MEM_DQ_PER_WRITE_DQS /
1181 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS;
1182 const u32 correct_mask_vg = param->write_correct_mask_vg;
1183
1184 u32 tmp_bit_chk, base_rw_mgr;
1185 int vg, r;
Marek Vasutad64769c2015-07-21 05:43:37 +02001186
1187 *bit_chk = param->write_correct_mask;
Marek Vasutad64769c2015-07-21 05:43:37 +02001188
1189 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001190 /* Request to skip the rank */
1191 if (param->skip_ranks[r])
Marek Vasutad64769c2015-07-21 05:43:37 +02001192 continue;
Marek Vasutad64769c2015-07-21 05:43:37 +02001193
Marek Vasutb9452ea2015-07-21 05:54:39 +02001194 /* Set rank */
Marek Vasutad64769c2015-07-21 05:43:37 +02001195 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1196
1197 tmp_bit_chk = 0;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001198 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS - 1;
1199 vg >= 0; vg--) {
1200 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutad64769c2015-07-21 05:43:37 +02001201 writel(0, &phy_mgr_cmd->fifo_reset);
1202
Marek Vasutb9452ea2015-07-21 05:54:39 +02001203 rw_mgr_mem_calibrate_write_test_issue(
1204 write_group *
1205 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS + vg,
Marek Vasutad64769c2015-07-21 05:43:37 +02001206 use_dm);
1207
Marek Vasutb9452ea2015-07-21 05:54:39 +02001208 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1209 tmp_bit_chk <<= shift_ratio;
1210 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasutad64769c2015-07-21 05:43:37 +02001211 }
Marek Vasutb9452ea2015-07-21 05:54:39 +02001212
Marek Vasutad64769c2015-07-21 05:43:37 +02001213 *bit_chk &= tmp_bit_chk;
1214 }
1215
Marek Vasutb9452ea2015-07-21 05:54:39 +02001216 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutad64769c2015-07-21 05:43:37 +02001217 if (all_correct) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001218 debug_cond(DLEVEL == 2,
1219 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1220 write_group, use_dm, *bit_chk,
1221 param->write_correct_mask,
1222 *bit_chk == param->write_correct_mask);
Marek Vasutad64769c2015-07-21 05:43:37 +02001223 return *bit_chk == param->write_correct_mask;
1224 } else {
1225 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutb9452ea2015-07-21 05:54:39 +02001226 debug_cond(DLEVEL == 2,
1227 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1228 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasutad64769c2015-07-21 05:43:37 +02001229 return *bit_chk != 0x00;
1230 }
1231}
1232
Marek Vasutd844c7d2015-07-18 03:55:07 +02001233/**
1234 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1235 * @rank_bgn: Rank number
1236 * @group: Read/Write Group
1237 * @all_ranks: Test all ranks
1238 *
1239 * Performs a guaranteed read on the patterns we are going to use during a
1240 * read test to ensure memory works.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001241 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02001242static int
1243rw_mgr_mem_calibrate_read_test_patterns(const u32 rank_bgn, const u32 group,
1244 const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001245{
Marek Vasutd844c7d2015-07-18 03:55:07 +02001246 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1247 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1248 const u32 addr_offset =
1249 (group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS) << 2;
1250 const u32 rank_end = all_ranks ?
1251 RW_MGR_MEM_NUMBER_OF_RANKS :
1252 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1253 const u32 shift_ratio = RW_MGR_MEM_DQ_PER_READ_DQS /
1254 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS;
1255 const u32 correct_mask_vg = param->read_correct_mask_vg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001256
Marek Vasutd844c7d2015-07-18 03:55:07 +02001257 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1258 int vg, r;
1259 int ret = 0;
1260
1261 bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001262
1263 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutd844c7d2015-07-18 03:55:07 +02001264 /* Request to skip the rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001265 if (param->skip_ranks[r])
Dinh Nguyen3da42852015-06-02 22:52:49 -05001266 continue;
1267
Marek Vasutd844c7d2015-07-18 03:55:07 +02001268 /* Set rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001269 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1270
1271 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001272 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1273 writel(RW_MGR_GUARANTEED_READ,
1274 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001275
Marek Vasut1273dd92015-07-12 21:05:08 +02001276 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1277 writel(RW_MGR_GUARANTEED_READ_CONT,
1278 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001279
1280 tmp_bit_chk = 0;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001281 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1;
1282 vg >= 0; vg--) {
1283 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001284 writel(0, &phy_mgr_cmd->fifo_reset);
1285 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1286 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001287 writel(RW_MGR_GUARANTEED_READ,
1288 addr + addr_offset + (vg << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001289
Marek Vasut1273dd92015-07-12 21:05:08 +02001290 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001291 tmp_bit_chk <<= shift_ratio;
1292 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001293 }
Marek Vasutd844c7d2015-07-18 03:55:07 +02001294
1295 bit_chk &= tmp_bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001296 }
1297
Marek Vasut17fdc912015-07-12 20:05:54 +02001298 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001299
1300 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001301
1302 if (bit_chk != param->read_correct_mask)
1303 ret = -EIO;
1304
1305 debug_cond(DLEVEL == 1,
1306 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1307 __func__, __LINE__, group, bit_chk,
1308 param->read_correct_mask, ret);
1309
1310 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001311}
1312
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001313/**
1314 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read test
1315 * @rank_bgn: Rank number
1316 * @all_ranks: Test all ranks
1317 *
1318 * Load up the patterns we are going to use during a read test.
1319 */
1320static void rw_mgr_mem_calibrate_read_load_patterns(const u32 rank_bgn,
1321 const int all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001322{
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001323 const u32 rank_end = all_ranks ?
1324 RW_MGR_MEM_NUMBER_OF_RANKS :
1325 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1326 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001327
1328 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001329
Dinh Nguyen3da42852015-06-02 22:52:49 -05001330 for (r = rank_bgn; r < rank_end; r++) {
1331 if (param->skip_ranks[r])
1332 /* request to skip the rank */
1333 continue;
1334
1335 /* set rank */
1336 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1337
1338 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001339 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001340
Marek Vasut1273dd92015-07-12 21:05:08 +02001341 writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1342 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001343
Marek Vasut1273dd92015-07-12 21:05:08 +02001344 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001345
Marek Vasut1273dd92015-07-12 21:05:08 +02001346 writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1347 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001348
Marek Vasut1273dd92015-07-12 21:05:08 +02001349 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001350
Marek Vasut1273dd92015-07-12 21:05:08 +02001351 writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1352 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001353
Marek Vasut1273dd92015-07-12 21:05:08 +02001354 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001355
Marek Vasut1273dd92015-07-12 21:05:08 +02001356 writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1357 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001358
Marek Vasut1273dd92015-07-12 21:05:08 +02001359 writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1360 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001361 }
1362
1363 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1364}
1365
Marek Vasut783fcf52015-07-20 03:26:05 +02001366/**
1367 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1368 * @rank_bgn: Rank number
1369 * @group: Read/Write group
1370 * @num_tries: Number of retries of the test
1371 * @all_correct: All bits must be correct in the mask
1372 * @bit_chk: Resulting bit mask after the test
1373 * @all_groups: Test all R/W groups
1374 * @all_ranks: Test all ranks
1375 *
1376 * Try a read and see if it returns correct data back. Test has dummy reads
1377 * inserted into the mix used to align DQS enable. Test has more thorough
1378 * checks than the regular read test.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001379 */
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001380static int
1381rw_mgr_mem_calibrate_read_test(const u32 rank_bgn, const u32 group,
1382 const u32 num_tries, const u32 all_correct,
1383 u32 *bit_chk,
1384 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001385{
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001386 const u32 rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
Dinh Nguyen3da42852015-06-02 22:52:49 -05001387 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001388 const u32 quick_read_mode =
1389 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
1390 ENABLE_SUPER_QUICK_CALIBRATION);
1391 u32 correct_mask_vg = param->read_correct_mask_vg;
1392 u32 tmp_bit_chk;
1393 u32 base_rw_mgr;
1394 u32 addr;
1395
1396 int r, vg, ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001397
1398 *bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001399
1400 for (r = rank_bgn; r < rank_end; r++) {
1401 if (param->skip_ranks[r])
1402 /* request to skip the rank */
1403 continue;
1404
1405 /* set rank */
1406 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1407
Marek Vasut1273dd92015-07-12 21:05:08 +02001408 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001409
Marek Vasut1273dd92015-07-12 21:05:08 +02001410 writel(RW_MGR_READ_B2B_WAIT1,
1411 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001412
Marek Vasut1273dd92015-07-12 21:05:08 +02001413 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1414 writel(RW_MGR_READ_B2B_WAIT2,
1415 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001416
Dinh Nguyen3da42852015-06-02 22:52:49 -05001417 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001418 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001419 /* need at least two (1+1) reads to capture failures */
1420 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001421 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001422 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001423 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001424
Marek Vasut1273dd92015-07-12 21:05:08 +02001425 writel(RW_MGR_READ_B2B,
1426 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001427 if (all_groups)
1428 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1429 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001430 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001431 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001432 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001433
Marek Vasut1273dd92015-07-12 21:05:08 +02001434 writel(RW_MGR_READ_B2B,
1435 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001436
1437 tmp_bit_chk = 0;
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001438 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1; vg >= 0;
1439 vg--) {
Marek Vasutba522c72015-07-19 07:57:28 +02001440 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001441 writel(0, &phy_mgr_cmd->fifo_reset);
1442 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1443 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001444
Marek Vasutba522c72015-07-19 07:57:28 +02001445 if (all_groups) {
1446 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1447 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1448 } else {
1449 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1450 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1451 }
Marek Vasutc4815f72015-07-12 19:03:33 +02001452
Marek Vasut17fdc912015-07-12 20:05:54 +02001453 writel(RW_MGR_READ_B2B, addr +
Dinh Nguyen3da42852015-06-02 22:52:49 -05001454 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1455 vg) << 2));
1456
Marek Vasut1273dd92015-07-12 21:05:08 +02001457 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutba522c72015-07-19 07:57:28 +02001458 tmp_bit_chk <<= RW_MGR_MEM_DQ_PER_READ_DQS /
1459 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS;
1460 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001461 }
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001462
Dinh Nguyen3da42852015-06-02 22:52:49 -05001463 *bit_chk &= tmp_bit_chk;
1464 }
1465
Marek Vasutc4815f72015-07-12 19:03:33 +02001466 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001467 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001468
Marek Vasut3853d652015-07-19 07:44:21 +02001469 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1470
Dinh Nguyen3da42852015-06-02 22:52:49 -05001471 if (all_correct) {
Marek Vasut3853d652015-07-19 07:44:21 +02001472 ret = (*bit_chk == param->read_correct_mask);
1473 debug_cond(DLEVEL == 2,
1474 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1475 __func__, __LINE__, group, all_groups, *bit_chk,
1476 param->read_correct_mask, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001477 } else {
Marek Vasut3853d652015-07-19 07:44:21 +02001478 ret = (*bit_chk != 0x00);
1479 debug_cond(DLEVEL == 2,
1480 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1481 __func__, __LINE__, group, all_groups, *bit_chk,
1482 0, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001483 }
Marek Vasut3853d652015-07-19 07:44:21 +02001484
1485 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001486}
1487
Marek Vasut96df6032015-07-19 07:35:36 +02001488/**
1489 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1490 * @grp: Read/Write group
1491 * @num_tries: Number of retries of the test
1492 * @all_correct: All bits must be correct in the mask
1493 * @all_groups: Test all R/W groups
1494 *
1495 * Perform a READ test across all memory ranks.
1496 */
1497static int
1498rw_mgr_mem_calibrate_read_test_all_ranks(const u32 grp, const u32 num_tries,
1499 const u32 all_correct,
1500 const u32 all_groups)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001501{
Marek Vasut96df6032015-07-19 07:35:36 +02001502 u32 bit_chk;
1503 return rw_mgr_mem_calibrate_read_test(0, grp, num_tries, all_correct,
1504 &bit_chk, all_groups, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001505}
1506
Marek Vasut60bb8a82015-07-19 06:25:27 +02001507/**
1508 * rw_mgr_incr_vfifo() - Increase VFIFO value
1509 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001510 *
1511 * Increase VFIFO value.
1512 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001513static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001514{
Marek Vasut1273dd92015-07-12 21:05:08 +02001515 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001516}
1517
Marek Vasut60bb8a82015-07-19 06:25:27 +02001518/**
1519 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1520 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001521 *
1522 * Decrease VFIFO value.
1523 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001524static void rw_mgr_decr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001525{
Marek Vasut60bb8a82015-07-19 06:25:27 +02001526 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001527
Marek Vasut60bb8a82015-07-19 06:25:27 +02001528 for (i = 0; i < VFIFO_SIZE - 1; i++)
Marek Vasut8c887b62015-07-19 06:37:51 +02001529 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001530}
1531
Marek Vasutd145ca92015-07-19 06:45:43 +02001532/**
1533 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1534 * @grp: Read/Write group
1535 *
1536 * Push VFIFO until a failing read happens.
1537 */
1538static int find_vfifo_failing_read(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001539{
Marek Vasut96df6032015-07-19 07:35:36 +02001540 u32 v, ret, fail_cnt = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001541
Marek Vasut8c887b62015-07-19 06:37:51 +02001542 for (v = 0; v < VFIFO_SIZE; v++) {
Marek Vasutd145ca92015-07-19 06:45:43 +02001543 debug_cond(DLEVEL == 2, "%s:%d: vfifo %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001544 __func__, __LINE__, v);
Marek Vasutd145ca92015-07-19 06:45:43 +02001545 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001546 PASS_ONE_BIT, 0);
Marek Vasutd145ca92015-07-19 06:45:43 +02001547 if (!ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001548 fail_cnt++;
1549
1550 if (fail_cnt == 2)
Marek Vasutd145ca92015-07-19 06:45:43 +02001551 return v;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001552 }
1553
Marek Vasutd145ca92015-07-19 06:45:43 +02001554 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001555 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001556 }
1557
Marek Vasutd145ca92015-07-19 06:45:43 +02001558 /* No failing read found! Something must have gone wrong. */
1559 debug_cond(DLEVEL == 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
1560 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001561}
1562
Marek Vasut192d6f92015-07-19 05:26:49 +02001563/**
Marek Vasut52e8f212015-07-19 07:27:06 +02001564 * sdr_find_phase_delay() - Find DQS enable phase or delay
1565 * @working: If 1, look for working phase/delay, if 0, look for non-working
1566 * @delay: If 1, look for delay, if 0, look for phase
1567 * @grp: Read/Write group
1568 * @work: Working window position
1569 * @work_inc: Working window increment
1570 * @pd: DQS Phase/Delay Iterator
1571 *
1572 * Find working or non-working DQS enable phase setting.
1573 */
1574static int sdr_find_phase_delay(int working, int delay, const u32 grp,
1575 u32 *work, const u32 work_inc, u32 *pd)
1576{
1577 const u32 max = delay ? IO_DQS_EN_DELAY_MAX : IO_DQS_EN_PHASE_MAX;
Marek Vasut96df6032015-07-19 07:35:36 +02001578 u32 ret;
Marek Vasut52e8f212015-07-19 07:27:06 +02001579
1580 for (; *pd <= max; (*pd)++) {
1581 if (delay)
1582 scc_mgr_set_dqs_en_delay_all_ranks(grp, *pd);
1583 else
1584 scc_mgr_set_dqs_en_phase_all_ranks(grp, *pd);
1585
1586 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001587 PASS_ONE_BIT, 0);
Marek Vasut52e8f212015-07-19 07:27:06 +02001588 if (!working)
1589 ret = !ret;
1590
1591 if (ret)
1592 return 0;
1593
1594 if (work)
1595 *work += work_inc;
1596 }
1597
1598 return -EINVAL;
1599}
1600/**
Marek Vasut192d6f92015-07-19 05:26:49 +02001601 * sdr_find_phase() - Find DQS enable phase
1602 * @working: If 1, look for working phase, if 0, look for non-working phase
1603 * @grp: Read/Write group
Marek Vasut192d6f92015-07-19 05:26:49 +02001604 * @work: Working window position
1605 * @i: Iterator
1606 * @p: DQS Phase Iterator
Marek Vasut192d6f92015-07-19 05:26:49 +02001607 *
1608 * Find working or non-working DQS enable phase setting.
1609 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001610static int sdr_find_phase(int working, const u32 grp, u32 *work,
Marek Vasut86a39dc2015-07-19 05:35:40 +02001611 u32 *i, u32 *p)
Marek Vasut192d6f92015-07-19 05:26:49 +02001612{
Marek Vasut192d6f92015-07-19 05:26:49 +02001613 const u32 end = VFIFO_SIZE + (working ? 0 : 1);
Marek Vasut52e8f212015-07-19 07:27:06 +02001614 int ret;
Marek Vasut192d6f92015-07-19 05:26:49 +02001615
1616 for (; *i < end; (*i)++) {
1617 if (working)
1618 *p = 0;
1619
Marek Vasut52e8f212015-07-19 07:27:06 +02001620 ret = sdr_find_phase_delay(working, 0, grp, work,
1621 IO_DELAY_PER_OPA_TAP, p);
1622 if (!ret)
1623 return 0;
Marek Vasut192d6f92015-07-19 05:26:49 +02001624
1625 if (*p > IO_DQS_EN_PHASE_MAX) {
1626 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001627 rw_mgr_incr_vfifo(grp);
Marek Vasut192d6f92015-07-19 05:26:49 +02001628 if (!working)
1629 *p = 0;
1630 }
1631 }
1632
1633 return -EINVAL;
1634}
1635
Marek Vasut4c5e5842015-07-19 06:04:00 +02001636/**
1637 * sdr_working_phase() - Find working DQS enable phase
1638 * @grp: Read/Write group
1639 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001640 * @d: dtaps output value
1641 * @p: DQS Phase Iterator
1642 * @i: Iterator
1643 *
1644 * Find working DQS enable phase setting.
1645 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001646static int sdr_working_phase(const u32 grp, u32 *work_bgn, u32 *d,
Marek Vasut4c5e5842015-07-19 06:04:00 +02001647 u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001648{
Marek Vasut35ee8672015-07-19 05:40:06 +02001649 const u32 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP /
1650 IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Marek Vasut192d6f92015-07-19 05:26:49 +02001651 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001652
Marek Vasut192d6f92015-07-19 05:26:49 +02001653 *work_bgn = 0;
1654
1655 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1656 *i = 0;
Marek Vasut521fe392015-07-19 04:34:12 +02001657 scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
Marek Vasut8c887b62015-07-19 06:37:51 +02001658 ret = sdr_find_phase(1, grp, work_bgn, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001659 if (!ret)
1660 return 0;
1661 *work_bgn += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001662 }
1663
Marek Vasut38ed6922015-07-19 05:01:12 +02001664 /* Cannot find working solution */
Marek Vasut192d6f92015-07-19 05:26:49 +02001665 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
1666 __func__, __LINE__);
1667 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001668}
1669
Marek Vasut4c5e5842015-07-19 06:04:00 +02001670/**
1671 * sdr_backup_phase() - Find DQS enable backup phase
1672 * @grp: Read/Write group
1673 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001674 * @p: DQS Phase Iterator
1675 *
1676 * Find DQS enable backup phase setting.
1677 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001678static void sdr_backup_phase(const u32 grp, u32 *work_bgn, u32 *p)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001679{
Marek Vasut96df6032015-07-19 07:35:36 +02001680 u32 tmp_delay, d;
Marek Vasut4c5e5842015-07-19 06:04:00 +02001681 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001682
1683 /* Special case code for backing up a phase */
1684 if (*p == 0) {
1685 *p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001686 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001687 } else {
1688 (*p)--;
1689 }
1690 tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
Marek Vasut521fe392015-07-19 04:34:12 +02001691 scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001692
Marek Vasut49891df62015-07-19 05:48:30 +02001693 for (d = 0; d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn; d++) {
1694 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001695
Marek Vasut4c5e5842015-07-19 06:04:00 +02001696 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001697 PASS_ONE_BIT, 0);
Marek Vasut4c5e5842015-07-19 06:04:00 +02001698 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001699 *work_bgn = tmp_delay;
1700 break;
1701 }
Marek Vasut49891df62015-07-19 05:48:30 +02001702
1703 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001704 }
1705
Marek Vasut4c5e5842015-07-19 06:04:00 +02001706 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001707 (*p)++;
1708 if (*p > IO_DQS_EN_PHASE_MAX) {
1709 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001710 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001711 }
1712
Marek Vasut521fe392015-07-19 04:34:12 +02001713 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001714}
1715
Marek Vasut4c5e5842015-07-19 06:04:00 +02001716/**
1717 * sdr_nonworking_phase() - Find non-working DQS enable phase
1718 * @grp: Read/Write group
1719 * @work_end: Working window end position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001720 * @p: DQS Phase Iterator
1721 * @i: Iterator
1722 *
1723 * Find non-working DQS enable phase setting.
1724 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001725static int sdr_nonworking_phase(const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001726{
Marek Vasut192d6f92015-07-19 05:26:49 +02001727 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001728
1729 (*p)++;
1730 *work_end += IO_DELAY_PER_OPA_TAP;
1731 if (*p > IO_DQS_EN_PHASE_MAX) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001732 /* Fiddle with FIFO. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001733 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001734 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001735 }
1736
Marek Vasut8c887b62015-07-19 06:37:51 +02001737 ret = sdr_find_phase(0, grp, work_end, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001738 if (ret) {
1739 /* Cannot see edge of failing read. */
1740 debug_cond(DLEVEL == 2, "%s:%d: end: failed\n",
1741 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001742 }
1743
Marek Vasut192d6f92015-07-19 05:26:49 +02001744 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001745}
1746
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001747/**
1748 * sdr_find_window_center() - Find center of the working DQS window.
1749 * @grp: Read/Write group
1750 * @work_bgn: First working settings
1751 * @work_end: Last working settings
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001752 *
1753 * Find center of the working DQS enable window.
1754 */
1755static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
Marek Vasut8c887b62015-07-19 06:37:51 +02001756 const u32 work_end)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001757{
Marek Vasut96df6032015-07-19 07:35:36 +02001758 u32 work_mid;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001759 int tmp_delay = 0;
Marek Vasut28fd2422015-07-19 02:56:59 +02001760 int i, p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001761
Marek Vasut28fd2422015-07-19 02:56:59 +02001762 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001763
1764 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasut28fd2422015-07-19 02:56:59 +02001765 work_bgn, work_end, work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001766 /* Get the middle delay to be less than a VFIFO delay */
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001767 tmp_delay = (IO_DQS_EN_PHASE_MAX + 1) * IO_DELAY_PER_OPA_TAP;
Marek Vasut28fd2422015-07-19 02:56:59 +02001768
Dinh Nguyen3da42852015-06-02 22:52:49 -05001769 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001770 work_mid %= tmp_delay;
Marek Vasut28fd2422015-07-19 02:56:59 +02001771 debug_cond(DLEVEL == 2, "new work_mid %d\n", work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001772
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001773 tmp_delay = rounddown(work_mid, IO_DELAY_PER_OPA_TAP);
1774 if (tmp_delay > IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP)
1775 tmp_delay = IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP;
1776 p = tmp_delay / IO_DELAY_PER_OPA_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001777
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001778 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
1779
1780 d = DIV_ROUND_UP(work_mid - tmp_delay, IO_DELAY_PER_DQS_EN_DCHAIN_TAP);
1781 if (d > IO_DQS_EN_DELAY_MAX)
1782 d = IO_DQS_EN_DELAY_MAX;
1783 tmp_delay += d * IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1784
Marek Vasut28fd2422015-07-19 02:56:59 +02001785 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
1786
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001787 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
Marek Vasut28fd2422015-07-19 02:56:59 +02001788 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001789
1790 /*
1791 * push vfifo until we can successfully calibrate. We can do this
1792 * because the largest possible margin in 1 VFIFO cycle.
1793 */
1794 for (i = 0; i < VFIFO_SIZE; i++) {
Marek Vasut8c887b62015-07-19 06:37:51 +02001795 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center\n");
Marek Vasut28fd2422015-07-19 02:56:59 +02001796 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05001797 PASS_ONE_BIT,
Marek Vasut96df6032015-07-19 07:35:36 +02001798 0)) {
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001799 debug_cond(DLEVEL == 2,
Marek Vasut8c887b62015-07-19 06:37:51 +02001800 "%s:%d center: found: ptap=%u dtap=%u\n",
1801 __func__, __LINE__, p, d);
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001802 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001803 }
1804
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001805 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001806 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001807 }
1808
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001809 debug_cond(DLEVEL == 2, "%s:%d center: failed.\n",
1810 __func__, __LINE__);
1811 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001812}
1813
Marek Vasut33756892015-07-20 09:11:09 +02001814/**
1815 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to use
1816 * @grp: Read/Write Group
1817 *
1818 * Find a good DQS enable to use.
1819 */
Marek Vasut914546e2015-07-20 09:20:42 +02001820static int rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001821{
Marek Vasut57355402015-07-20 09:20:20 +02001822 u32 d, p, i;
1823 u32 dtaps_per_ptap;
1824 u32 work_bgn, work_end;
1825 u32 found_passing_read, found_failing_read, initial_failing_dtap;
1826 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001827
1828 debug("%s:%d %u\n", __func__, __LINE__, grp);
1829
1830 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1831
1832 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1833 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1834
Marek Vasut2f3589c2015-07-19 02:42:21 +02001835 /* Step 0: Determine number of delay taps for each phase tap. */
1836 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP / IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001837
Marek Vasut2f3589c2015-07-19 02:42:21 +02001838 /* Step 1: First push vfifo until we get a failing read. */
Marek Vasutd145ca92015-07-19 06:45:43 +02001839 find_vfifo_failing_read(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001840
Marek Vasut2f3589c2015-07-19 02:42:21 +02001841 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001842 work_bgn = 0;
Marek Vasut914546e2015-07-20 09:20:42 +02001843 ret = sdr_working_phase(grp, &work_bgn, &d, &p, &i);
1844 if (ret)
1845 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001846
1847 work_end = work_bgn;
1848
1849 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001850 * If d is 0 then the working window covers a phase tap and we can
1851 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen3da42852015-06-02 22:52:49 -05001852 * and we need to increment the dtaps until we find the end.
1853 */
1854 if (d == 0) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001855 /*
1856 * Step 3a: If we have room, back off by one and
1857 * increment in dtaps.
1858 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001859 sdr_backup_phase(grp, &work_bgn, &p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001860
Marek Vasut2f3589c2015-07-19 02:42:21 +02001861 /*
1862 * Step 4a: go forward from working phase to non working
1863 * phase, increment in ptaps.
1864 */
Marek Vasut914546e2015-07-20 09:20:42 +02001865 ret = sdr_nonworking_phase(grp, &work_end, &p, &i);
1866 if (ret)
1867 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001868
Marek Vasut2f3589c2015-07-19 02:42:21 +02001869 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001870
1871 /* Special case code for backing up a phase */
1872 if (p == 0) {
1873 p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001874 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001875 } else {
1876 p = p - 1;
1877 }
1878
1879 work_end -= IO_DELAY_PER_OPA_TAP;
1880 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1881
Dinh Nguyen3da42852015-06-02 22:52:49 -05001882 d = 0;
1883
Marek Vasut2f3589c2015-07-19 02:42:21 +02001884 debug_cond(DLEVEL == 2, "%s:%d p: ptap=%u\n",
1885 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001886 }
1887
Marek Vasut2f3589c2015-07-19 02:42:21 +02001888 /* The dtap increment to find the failing edge is done here. */
Marek Vasut52e8f212015-07-19 07:27:06 +02001889 sdr_find_phase_delay(0, 1, grp, &work_end,
1890 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001891
1892 /* Go back to working dtap */
1893 if (d != 0)
1894 work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1895
Marek Vasut2f3589c2015-07-19 02:42:21 +02001896 debug_cond(DLEVEL == 2,
1897 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1898 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001899
1900 if (work_end < work_bgn) {
1901 /* nil range */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001902 debug_cond(DLEVEL == 2, "%s:%d end-2: failed\n",
1903 __func__, __LINE__);
Marek Vasut914546e2015-07-20 09:20:42 +02001904 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001905 }
1906
Marek Vasut2f3589c2015-07-19 02:42:21 +02001907 debug_cond(DLEVEL == 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001908 __func__, __LINE__, work_bgn, work_end);
1909
Dinh Nguyen3da42852015-06-02 22:52:49 -05001910 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001911 * We need to calculate the number of dtaps that equal a ptap.
1912 * To do that we'll back up a ptap and re-find the edge of the
1913 * window using dtaps
Dinh Nguyen3da42852015-06-02 22:52:49 -05001914 */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001915 debug_cond(DLEVEL == 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
1916 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001917
1918 /* Special case code for backing up a phase */
1919 if (p == 0) {
1920 p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001921 rw_mgr_decr_vfifo(grp);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001922 debug_cond(DLEVEL == 2, "%s:%d backedup cycle/phase: p=%u\n",
1923 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001924 } else {
1925 p = p - 1;
Marek Vasut2f3589c2015-07-19 02:42:21 +02001926 debug_cond(DLEVEL == 2, "%s:%d backedup phase only: p=%u",
1927 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001928 }
1929
1930 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1931
1932 /*
1933 * Increase dtap until we first see a passing read (in case the
Marek Vasut2f3589c2015-07-19 02:42:21 +02001934 * window is smaller than a ptap), and then a failing read to
1935 * mark the edge of the window again.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001936 */
1937
Marek Vasut2f3589c2015-07-19 02:42:21 +02001938 /* Find a passing read. */
1939 debug_cond(DLEVEL == 2, "%s:%d find passing read\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001940 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001941
Dinh Nguyen3da42852015-06-02 22:52:49 -05001942 initial_failing_dtap = d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001943
Marek Vasut52e8f212015-07-19 07:27:06 +02001944 found_passing_read = !sdr_find_phase_delay(1, 1, grp, NULL, 0, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001945 if (found_passing_read) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001946 /* Find a failing read. */
1947 debug_cond(DLEVEL == 2, "%s:%d find failing read\n",
1948 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001949 d++;
1950 found_failing_read = !sdr_find_phase_delay(0, 1, grp, NULL, 0,
1951 &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001952 } else {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001953 debug_cond(DLEVEL == 1,
1954 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1955 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001956 }
1957
1958 /*
1959 * The dynamically calculated dtaps_per_ptap is only valid if we
1960 * found a passing/failing read. If we didn't, it means d hit the max
1961 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1962 * statically calculated value.
1963 */
1964 if (found_passing_read && found_failing_read)
1965 dtaps_per_ptap = d - initial_failing_dtap;
1966
Marek Vasut1273dd92015-07-12 21:05:08 +02001967 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001968 debug_cond(DLEVEL == 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
1969 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001970
Marek Vasut2f3589c2015-07-19 02:42:21 +02001971 /* Step 6: Find the centre of the window. */
Marek Vasut914546e2015-07-20 09:20:42 +02001972 ret = sdr_find_window_center(grp, work_bgn, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001973
Marek Vasut914546e2015-07-20 09:20:42 +02001974 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001975}
1976
Marek Vasutc4907892015-07-13 02:11:02 +02001977/**
Marek Vasut901dc362015-07-13 02:48:34 +02001978 * search_stop_check() - Check if the detected edge is valid
1979 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1980 * @d: DQS delay
1981 * @rank_bgn: Rank number
1982 * @write_group: Write Group
1983 * @read_group: Read Group
1984 * @bit_chk: Resulting bit mask after the test
1985 * @sticky_bit_chk: Resulting sticky bit mask after the test
1986 * @use_read_test: Perform read test
1987 *
1988 * Test if the found edge is valid.
1989 */
1990static u32 search_stop_check(const int write, const int d, const int rank_bgn,
1991 const u32 write_group, const u32 read_group,
1992 u32 *bit_chk, u32 *sticky_bit_chk,
1993 const u32 use_read_test)
1994{
1995 const u32 ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
1996 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
1997 const u32 correct_mask = write ? param->write_correct_mask :
1998 param->read_correct_mask;
1999 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2000 RW_MGR_MEM_DQ_PER_READ_DQS;
2001 u32 ret;
2002 /*
2003 * Stop searching when the read test doesn't pass AND when
2004 * we've seen a passing read on every bit.
2005 */
2006 if (write) { /* WRITE-ONLY */
2007 ret = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2008 0, PASS_ONE_BIT,
2009 bit_chk, 0);
2010 } else if (use_read_test) { /* READ-ONLY */
2011 ret = !rw_mgr_mem_calibrate_read_test(rank_bgn, read_group,
2012 NUM_READ_PB_TESTS,
2013 PASS_ONE_BIT, bit_chk,
2014 0, 0);
2015 } else { /* READ-ONLY */
2016 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 0,
2017 PASS_ONE_BIT, bit_chk, 0);
2018 *bit_chk = *bit_chk >> (per_dqs *
2019 (read_group - (write_group * ratio)));
2020 ret = (*bit_chk == 0);
2021 }
2022 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2023 ret = ret && (*sticky_bit_chk == correct_mask);
2024 debug_cond(DLEVEL == 2,
2025 "%s:%d center(left): dtap=%u => %u == %u && %u",
2026 __func__, __LINE__, d,
2027 *sticky_bit_chk, correct_mask, ret);
2028 return ret;
2029}
2030
2031/**
Marek Vasut71120772015-07-13 02:38:15 +02002032 * search_left_edge() - Find left edge of DQ/DQS working phase
2033 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2034 * @rank_bgn: Rank number
2035 * @write_group: Write Group
2036 * @read_group: Read Group
2037 * @test_bgn: Rank number to begin the test
Marek Vasut71120772015-07-13 02:38:15 +02002038 * @sticky_bit_chk: Resulting sticky bit mask after the test
2039 * @left_edge: Left edge of the DQ/DQS phase
2040 * @right_edge: Right edge of the DQ/DQS phase
2041 * @use_read_test: Perform read test
2042 *
2043 * Find left edge of DQ/DQS working phase.
2044 */
2045static void search_left_edge(const int write, const int rank_bgn,
2046 const u32 write_group, const u32 read_group, const u32 test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002047 u32 *sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002048 int *left_edge, int *right_edge, const u32 use_read_test)
2049{
Marek Vasut71120772015-07-13 02:38:15 +02002050 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2051 const u32 dqs_max = write ? IO_IO_OUT1_DELAY_MAX : IO_DQS_IN_DELAY_MAX;
2052 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2053 RW_MGR_MEM_DQ_PER_READ_DQS;
Marek Vasut0c4be192015-07-18 20:34:00 +02002054 u32 stop, bit_chk;
Marek Vasut71120772015-07-13 02:38:15 +02002055 int i, d;
2056
2057 for (d = 0; d <= dqs_max; d++) {
2058 if (write)
2059 scc_mgr_apply_group_dq_out1_delay(d);
2060 else
2061 scc_mgr_apply_group_dq_in_delay(test_bgn, d);
2062
2063 writel(0, &sdr_scc_mgr->update);
2064
Marek Vasut901dc362015-07-13 02:48:34 +02002065 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002066 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002067 use_read_test);
Marek Vasut71120772015-07-13 02:38:15 +02002068 if (stop == 1)
2069 break;
2070
2071 /* stop != 1 */
2072 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002073 if (bit_chk & 1) {
Marek Vasut71120772015-07-13 02:38:15 +02002074 /*
2075 * Remember a passing test as
2076 * the left_edge.
2077 */
2078 left_edge[i] = d;
2079 } else {
2080 /*
2081 * If a left edge has not been seen
2082 * yet, then a future passing test
2083 * will mark this edge as the right
2084 * edge.
2085 */
2086 if (left_edge[i] == delay_max + 1)
2087 right_edge[i] = -(d + 1);
2088 }
Marek Vasut0c4be192015-07-18 20:34:00 +02002089 bit_chk >>= 1;
Marek Vasut71120772015-07-13 02:38:15 +02002090 }
2091 }
2092
2093 /* Reset DQ delay chains to 0 */
2094 if (write)
2095 scc_mgr_apply_group_dq_out1_delay(0);
2096 else
2097 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2098
2099 *sticky_bit_chk = 0;
2100 for (i = per_dqs - 1; i >= 0; i--) {
2101 debug_cond(DLEVEL == 2,
2102 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2103 __func__, __LINE__, i, left_edge[i],
2104 i, right_edge[i]);
2105
2106 /*
2107 * Check for cases where we haven't found the left edge,
2108 * which makes our assignment of the the right edge invalid.
2109 * Reset it to the illegal value.
2110 */
2111 if ((left_edge[i] == delay_max + 1) &&
2112 (right_edge[i] != delay_max + 1)) {
2113 right_edge[i] = delay_max + 1;
2114 debug_cond(DLEVEL == 2,
2115 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2116 __func__, __LINE__, i, right_edge[i]);
2117 }
2118
2119 /*
2120 * Reset sticky bit
2121 * READ: except for bits where we have seen both
2122 * the left and right edge.
2123 * WRITE: except for bits where we have seen the
2124 * left edge.
2125 */
2126 *sticky_bit_chk <<= 1;
2127 if (write) {
2128 if (left_edge[i] != delay_max + 1)
2129 *sticky_bit_chk |= 1;
2130 } else {
2131 if ((left_edge[i] != delay_max + 1) &&
2132 (right_edge[i] != delay_max + 1))
2133 *sticky_bit_chk |= 1;
2134 }
2135 }
2136
2137
2138}
2139
2140/**
Marek Vasutc4907892015-07-13 02:11:02 +02002141 * search_right_edge() - Find right edge of DQ/DQS working phase
2142 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2143 * @rank_bgn: Rank number
2144 * @write_group: Write Group
2145 * @read_group: Read Group
2146 * @start_dqs: DQS start phase
2147 * @start_dqs_en: DQS enable start phase
Marek Vasutc4907892015-07-13 02:11:02 +02002148 * @sticky_bit_chk: Resulting sticky bit mask after the test
2149 * @left_edge: Left edge of the DQ/DQS phase
2150 * @right_edge: Right edge of the DQ/DQS phase
2151 * @use_read_test: Perform read test
2152 *
2153 * Find right edge of DQ/DQS working phase.
2154 */
2155static int search_right_edge(const int write, const int rank_bgn,
2156 const u32 write_group, const u32 read_group,
2157 const int start_dqs, const int start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002158 u32 *sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002159 int *left_edge, int *right_edge, const u32 use_read_test)
2160{
Marek Vasutc4907892015-07-13 02:11:02 +02002161 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2162 const u32 dqs_max = write ? IO_IO_OUT1_DELAY_MAX : IO_DQS_IN_DELAY_MAX;
2163 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2164 RW_MGR_MEM_DQ_PER_READ_DQS;
Marek Vasut0c4be192015-07-18 20:34:00 +02002165 u32 stop, bit_chk;
Marek Vasutc4907892015-07-13 02:11:02 +02002166 int i, d;
2167
2168 for (d = 0; d <= dqs_max - start_dqs; d++) {
2169 if (write) { /* WRITE-ONLY */
2170 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2171 d + start_dqs);
2172 } else { /* READ-ONLY */
2173 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
2174 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2175 uint32_t delay = d + start_dqs_en;
2176 if (delay > IO_DQS_EN_DELAY_MAX)
2177 delay = IO_DQS_EN_DELAY_MAX;
2178 scc_mgr_set_dqs_en_delay(read_group, delay);
2179 }
2180 scc_mgr_load_dqs(read_group);
2181 }
2182
2183 writel(0, &sdr_scc_mgr->update);
2184
Marek Vasut901dc362015-07-13 02:48:34 +02002185 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002186 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002187 use_read_test);
Marek Vasutc4907892015-07-13 02:11:02 +02002188 if (stop == 1) {
2189 if (write && (d == 0)) { /* WRITE-ONLY */
2190 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2191 /*
2192 * d = 0 failed, but it passed when
2193 * testing the left edge, so it must be
2194 * marginal, set it to -1
2195 */
2196 if (right_edge[i] == delay_max + 1 &&
2197 left_edge[i] != delay_max + 1)
2198 right_edge[i] = -1;
2199 }
2200 }
2201 break;
2202 }
2203
2204 /* stop != 1 */
2205 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002206 if (bit_chk & 1) {
Marek Vasutc4907892015-07-13 02:11:02 +02002207 /*
2208 * Remember a passing test as
2209 * the right_edge.
2210 */
2211 right_edge[i] = d;
2212 } else {
2213 if (d != 0) {
2214 /*
2215 * If a right edge has not
2216 * been seen yet, then a future
2217 * passing test will mark this
2218 * edge as the left edge.
2219 */
2220 if (right_edge[i] == delay_max + 1)
2221 left_edge[i] = -(d + 1);
2222 } else {
2223 /*
2224 * d = 0 failed, but it passed
2225 * when testing the left edge,
2226 * so it must be marginal, set
2227 * it to -1
2228 */
2229 if (right_edge[i] == delay_max + 1 &&
2230 left_edge[i] != delay_max + 1)
2231 right_edge[i] = -1;
2232 /*
2233 * If a right edge has not been
2234 * seen yet, then a future
2235 * passing test will mark this
2236 * edge as the left edge.
2237 */
2238 else if (right_edge[i] == delay_max + 1)
2239 left_edge[i] = -(d + 1);
2240 }
2241 }
2242
2243 debug_cond(DLEVEL == 2, "%s:%d center[r,d=%u]: ",
2244 __func__, __LINE__, d);
2245 debug_cond(DLEVEL == 2,
2246 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasut0c4be192015-07-18 20:34:00 +02002247 bit_chk & 1, i, left_edge[i]);
Marek Vasutc4907892015-07-13 02:11:02 +02002248 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2249 right_edge[i]);
Marek Vasut0c4be192015-07-18 20:34:00 +02002250 bit_chk >>= 1;
Marek Vasutc4907892015-07-13 02:11:02 +02002251 }
2252 }
2253
2254 /* Check that all bits have a window */
2255 for (i = 0; i < per_dqs; i++) {
2256 debug_cond(DLEVEL == 2,
2257 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2258 __func__, __LINE__, i, left_edge[i],
2259 i, right_edge[i]);
2260 if ((left_edge[i] == dqs_max + 1) ||
2261 (right_edge[i] == dqs_max + 1))
2262 return i + 1; /* FIXME: If we fail, retval > 0 */
2263 }
2264
2265 return 0;
2266}
2267
Marek Vasutafb3eb82015-07-18 19:18:06 +02002268/**
2269 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2270 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2271 * @left_edge: Left edge of the DQ/DQS phase
2272 * @right_edge: Right edge of the DQ/DQS phase
2273 * @mid_min: Best DQ/DQS phase middle setting
2274 *
2275 * Find index and value of the middle of the DQ/DQS working phase.
2276 */
2277static int get_window_mid_index(const int write, int *left_edge,
2278 int *right_edge, int *mid_min)
2279{
2280 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2281 RW_MGR_MEM_DQ_PER_READ_DQS;
2282 int i, mid, min_index;
2283
2284 /* Find middle of window for each DQ bit */
2285 *mid_min = left_edge[0] - right_edge[0];
2286 min_index = 0;
2287 for (i = 1; i < per_dqs; i++) {
2288 mid = left_edge[i] - right_edge[i];
2289 if (mid < *mid_min) {
2290 *mid_min = mid;
2291 min_index = i;
2292 }
2293 }
2294
2295 /*
2296 * -mid_min/2 represents the amount that we need to move DQS.
2297 * If mid_min is odd and positive we'll need to add one to make
2298 * sure the rounding in further calculations is correct (always
2299 * bias to the right), so just add 1 for all positive values.
2300 */
2301 if (*mid_min > 0)
2302 (*mid_min)++;
2303 *mid_min = *mid_min / 2;
2304
2305 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
2306 __func__, __LINE__, *mid_min, min_index);
2307 return min_index;
2308}
2309
Marek Vasutffb8b662015-07-18 19:46:26 +02002310/**
2311 * center_dq_windows() - Center the DQ/DQS windows
2312 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2313 * @left_edge: Left edge of the DQ/DQS phase
2314 * @right_edge: Right edge of the DQ/DQS phase
2315 * @mid_min: Adjusted DQ/DQS phase middle setting
2316 * @orig_mid_min: Original DQ/DQS phase middle setting
2317 * @min_index: DQ/DQS phase middle setting index
2318 * @test_bgn: Rank number to begin the test
2319 * @dq_margin: Amount of shift for the DQ
2320 * @dqs_margin: Amount of shift for the DQS
2321 *
2322 * Align the DQ/DQS windows in each group.
2323 */
2324static void center_dq_windows(const int write, int *left_edge, int *right_edge,
2325 const int mid_min, const int orig_mid_min,
2326 const int min_index, const int test_bgn,
2327 int *dq_margin, int *dqs_margin)
2328{
2329 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2330 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2331 RW_MGR_MEM_DQ_PER_READ_DQS;
2332 const u32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
2333 SCC_MGR_IO_IN_DELAY_OFFSET;
2334 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
2335
2336 u32 temp_dq_io_delay1, temp_dq_io_delay2;
2337 int shift_dq, i, p;
2338
2339 /* Initialize data for export structures */
2340 *dqs_margin = delay_max + 1;
2341 *dq_margin = delay_max + 1;
2342
2343 /* add delay to bring centre of all DQ windows to the same "level" */
2344 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2345 /* Use values before divide by 2 to reduce round off error */
2346 shift_dq = (left_edge[i] - right_edge[i] -
2347 (left_edge[min_index] - right_edge[min_index]))/2 +
2348 (orig_mid_min - mid_min);
2349
2350 debug_cond(DLEVEL == 2,
2351 "vfifo_center: before: shift_dq[%u]=%d\n",
2352 i, shift_dq);
2353
2354 temp_dq_io_delay1 = readl(addr + (p << 2));
2355 temp_dq_io_delay2 = readl(addr + (i << 2));
2356
2357 if (shift_dq + temp_dq_io_delay1 > delay_max)
2358 shift_dq = delay_max - temp_dq_io_delay2;
2359 else if (shift_dq + temp_dq_io_delay1 < 0)
2360 shift_dq = -temp_dq_io_delay1;
2361
2362 debug_cond(DLEVEL == 2,
2363 "vfifo_center: after: shift_dq[%u]=%d\n",
2364 i, shift_dq);
2365
2366 if (write)
2367 scc_mgr_set_dq_out1_delay(i, temp_dq_io_delay1 + shift_dq);
2368 else
2369 scc_mgr_set_dq_in_delay(p, temp_dq_io_delay1 + shift_dq);
2370
2371 scc_mgr_load_dq(p);
2372
2373 debug_cond(DLEVEL == 2,
2374 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2375 left_edge[i] - shift_dq + (-mid_min),
2376 right_edge[i] + shift_dq - (-mid_min));
2377
2378 /* To determine values for export structures */
2379 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2380 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2381
2382 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2383 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2384 }
2385
2386}
2387
Marek Vasutac63b9a2015-07-21 04:27:32 +02002388/**
2389 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2390 * @rank_bgn: Rank number
2391 * @rw_group: Read/Write Group
2392 * @test_bgn: Rank at which the test begins
2393 * @use_read_test: Perform a read test
2394 * @update_fom: Update FOM
2395 *
2396 * Per-bit deskew DQ and centering.
2397 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002398static int rw_mgr_mem_calibrate_vfifo_center(const u32 rank_bgn,
2399 const u32 rw_group, const u32 test_bgn,
2400 const int use_read_test, const int update_fom)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002401{
Marek Vasut5d6db442015-07-18 19:57:12 +02002402 const u32 addr =
2403 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasut0113c3e2015-07-18 20:42:27 +02002404 (rw_group << 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002405 /*
2406 * Store these as signed since there are comparisons with
2407 * signed numbers.
2408 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002409 uint32_t sticky_bit_chk;
2410 int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
2411 int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
Dinh Nguyen3da42852015-06-02 22:52:49 -05002412 int32_t orig_mid_min, mid_min;
Marek Vasut5d6db442015-07-18 19:57:12 +02002413 int32_t new_dqs, start_dqs, start_dqs_en, final_dqs_en;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002414 int32_t dq_margin, dqs_margin;
Marek Vasut5d6db442015-07-18 19:57:12 +02002415 int i, min_index;
Marek Vasutc4907892015-07-13 02:11:02 +02002416 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002417
Marek Vasut0113c3e2015-07-18 20:42:27 +02002418 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002419
Marek Vasut5d6db442015-07-18 19:57:12 +02002420 start_dqs = readl(addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002421 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut5d6db442015-07-18 19:57:12 +02002422 start_dqs_en = readl(addr - IO_DQS_EN_DELAY_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002423
2424 /* set the left and right edge of each bit to an illegal value */
2425 /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
2426 sticky_bit_chk = 0;
2427 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2428 left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
2429 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
2430 }
2431
Dinh Nguyen3da42852015-06-02 22:52:49 -05002432 /* Search for the left edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002433 search_left_edge(0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002434 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002435 left_edge, right_edge, use_read_test);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002436
Marek Vasutf0712c32015-07-18 08:01:45 +02002437
Dinh Nguyen3da42852015-06-02 22:52:49 -05002438 /* Search for the right edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002439 ret = search_right_edge(0, rank_bgn, rw_group, rw_group,
Marek Vasutc4907892015-07-13 02:11:02 +02002440 start_dqs, start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002441 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002442 left_edge, right_edge, use_read_test);
2443 if (ret) {
2444 /*
2445 * Restore delay chain settings before letting the loop
2446 * in rw_mgr_mem_calibrate_vfifo to retry different
2447 * dqs/ck relationships.
2448 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002449 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Marek Vasutc4907892015-07-13 02:11:02 +02002450 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut0113c3e2015-07-18 20:42:27 +02002451 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002452
Marek Vasut0113c3e2015-07-18 20:42:27 +02002453 scc_mgr_load_dqs(rw_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002454 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002455
Marek Vasutc4907892015-07-13 02:11:02 +02002456 debug_cond(DLEVEL == 1,
2457 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2458 __func__, __LINE__, i, left_edge[i], right_edge[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002459 if (use_read_test) {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002460 set_failing_group_stage(rw_group *
Marek Vasutc4907892015-07-13 02:11:02 +02002461 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2462 CAL_STAGE_VFIFO,
2463 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002464 } else {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002465 set_failing_group_stage(rw_group *
Marek Vasutc4907892015-07-13 02:11:02 +02002466 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2467 CAL_STAGE_VFIFO_AFTER_WRITES,
2468 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002469 }
Marek Vasut98668242015-07-18 20:44:28 +02002470 return -EIO;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002471 }
2472
Marek Vasutafb3eb82015-07-18 19:18:06 +02002473 min_index = get_window_mid_index(0, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002474
2475 /* Determine the amount we can change DQS (which is -mid_min) */
2476 orig_mid_min = mid_min;
2477 new_dqs = start_dqs - mid_min;
2478 if (new_dqs > IO_DQS_IN_DELAY_MAX)
2479 new_dqs = IO_DQS_IN_DELAY_MAX;
2480 else if (new_dqs < 0)
2481 new_dqs = 0;
2482
2483 mid_min = start_dqs - new_dqs;
2484 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2485 mid_min, new_dqs);
2486
2487 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2488 if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2489 mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2490 else if (start_dqs_en - mid_min < 0)
2491 mid_min += start_dqs_en - mid_min;
2492 }
2493 new_dqs = start_dqs - mid_min;
2494
Marek Vasutf0712c32015-07-18 08:01:45 +02002495 debug_cond(DLEVEL == 1,
2496 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2497 start_dqs,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002498 IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2499 new_dqs, mid_min);
2500
Marek Vasutffb8b662015-07-18 19:46:26 +02002501 /* Add delay to bring centre of all DQ windows to the same "level". */
2502 center_dq_windows(0, left_edge, right_edge, mid_min, orig_mid_min,
2503 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002504
Dinh Nguyen3da42852015-06-02 22:52:49 -05002505 /* Move DQS-en */
2506 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
Marek Vasut5d6db442015-07-18 19:57:12 +02002507 final_dqs_en = start_dqs_en - mid_min;
Marek Vasut0113c3e2015-07-18 20:42:27 +02002508 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2509 scc_mgr_load_dqs(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002510 }
2511
2512 /* Move DQS */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002513 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2514 scc_mgr_load_dqs(rw_group);
Marek Vasutf0712c32015-07-18 08:01:45 +02002515 debug_cond(DLEVEL == 2,
2516 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2517 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002518
2519 /*
2520 * Do not remove this line as it makes sure all of our decisions
2521 * have been applied. Apply the update bit.
2522 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002523 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002524
Marek Vasut98668242015-07-18 20:44:28 +02002525 if ((dq_margin < 0) || (dqs_margin < 0))
2526 return -EINVAL;
2527
2528 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002529}
2530
Marek Vasutbce24ef2015-07-17 03:16:45 +02002531/**
Marek Vasut04372fb2015-07-18 02:46:56 +02002532 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
2533 * @rw_group: Read/Write Group
2534 * @phase: DQ/DQS phase
2535 *
2536 * Because initially no communication ca be reliably performed with the memory
2537 * device, the sequencer uses a guaranteed write mechanism to write data into
2538 * the memory device.
2539 */
2540static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
2541 const u32 phase)
2542{
Marek Vasut04372fb2015-07-18 02:46:56 +02002543 int ret;
2544
2545 /* Set a particular DQ/DQS phase. */
2546 scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
2547
2548 debug_cond(DLEVEL == 1, "%s:%d guaranteed write: g=%u p=%u\n",
2549 __func__, __LINE__, rw_group, phase);
2550
2551 /*
2552 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2553 * Load up the patterns used by read calibration using the
2554 * current DQDQS phase.
2555 */
2556 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2557
2558 if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2559 return 0;
2560
2561 /*
2562 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2563 * Back-to-Back reads of the patterns used for calibration.
2564 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02002565 ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
2566 if (ret)
Marek Vasut04372fb2015-07-18 02:46:56 +02002567 debug_cond(DLEVEL == 1,
2568 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2569 __func__, __LINE__, rw_group, phase);
Marek Vasutd844c7d2015-07-18 03:55:07 +02002570 return ret;
Marek Vasut04372fb2015-07-18 02:46:56 +02002571}
2572
2573/**
Marek Vasutf09da112015-07-18 02:57:32 +02002574 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2575 * @rw_group: Read/Write Group
2576 * @test_bgn: Rank at which the test begins
2577 *
2578 * DQS enable calibration ensures reliable capture of the DQ signal without
2579 * glitches on the DQS line.
2580 */
2581static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
2582 const u32 test_bgn)
2583{
Marek Vasutf09da112015-07-18 02:57:32 +02002584 /*
2585 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2586 * DQS and DQS Eanble Signal Relationships.
2587 */
Marek Vasut28ea8272015-07-18 04:28:42 +02002588
2589 /* We start at zero, so have one less dq to devide among */
2590 const u32 delay_step = IO_IO_IN_DELAY_MAX /
2591 (RW_MGR_MEM_DQ_PER_READ_DQS - 1);
Marek Vasut914546e2015-07-20 09:20:42 +02002592 int ret;
Marek Vasut28ea8272015-07-18 04:28:42 +02002593 u32 i, p, d, r;
2594
2595 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2596
2597 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
2598 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
2599 r += NUM_RANKS_PER_SHADOW_REG) {
2600 for (i = 0, p = test_bgn, d = 0;
2601 i < RW_MGR_MEM_DQ_PER_READ_DQS;
2602 i++, p++, d += delay_step) {
2603 debug_cond(DLEVEL == 1,
2604 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2605 __func__, __LINE__, rw_group, r, i, p, d);
2606
2607 scc_mgr_set_dq_in_delay(p, d);
2608 scc_mgr_load_dq(p);
2609 }
2610
2611 writel(0, &sdr_scc_mgr->update);
2612 }
2613
2614 /*
2615 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2616 * dq_in_delay values
2617 */
Marek Vasut914546e2015-07-20 09:20:42 +02002618 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(rw_group);
Marek Vasut28ea8272015-07-18 04:28:42 +02002619
2620 debug_cond(DLEVEL == 1,
2621 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut914546e2015-07-20 09:20:42 +02002622 __func__, __LINE__, rw_group, !ret);
Marek Vasut28ea8272015-07-18 04:28:42 +02002623
2624 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
2625 r += NUM_RANKS_PER_SHADOW_REG) {
2626 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2627 writel(0, &sdr_scc_mgr->update);
2628 }
2629
Marek Vasut914546e2015-07-20 09:20:42 +02002630 return ret;
Marek Vasutf09da112015-07-18 02:57:32 +02002631}
2632
2633/**
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002634 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2635 * @rw_group: Read/Write Group
2636 * @test_bgn: Rank at which the test begins
2637 * @use_read_test: Perform a read test
2638 * @update_fom: Update FOM
2639 *
2640 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2641 * within a group.
2642 */
2643static int
2644rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
2645 const int use_read_test,
2646 const int update_fom)
2647
2648{
2649 int ret, grp_calibrated;
2650 u32 rank_bgn, sr;
2651
2652 /*
2653 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2654 * Read per-bit deskew can be done on a per shadow register basis.
2655 */
2656 grp_calibrated = 1;
2657 for (rank_bgn = 0, sr = 0;
2658 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2659 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
2660 /* Check if this set of ranks should be skipped entirely. */
2661 if (param->skip_shadow_regs[sr])
2662 continue;
2663
2664 ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
Marek Vasut0113c3e2015-07-18 20:42:27 +02002665 test_bgn,
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002666 use_read_test,
2667 update_fom);
Marek Vasut98668242015-07-18 20:44:28 +02002668 if (!ret)
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002669 continue;
2670
2671 grp_calibrated = 0;
2672 }
2673
2674 if (!grp_calibrated)
2675 return -EIO;
2676
2677 return 0;
2678}
2679
2680/**
Marek Vasutbce24ef2015-07-17 03:16:45 +02002681 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2682 * @rw_group: Read/Write Group
2683 * @test_bgn: Rank at which the test begins
Dinh Nguyen3da42852015-06-02 22:52:49 -05002684 *
Marek Vasutbce24ef2015-07-17 03:16:45 +02002685 * Stage 1: Calibrate the read valid prediction FIFO.
2686 *
2687 * This function implements UniPHY calibration Stage 1, as explained in
2688 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2689 *
2690 * - read valid prediction will consist of finding:
2691 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2692 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002693 * - we also do a per-bit deskew on the DQ lines.
2694 */
Marek Vasutc336ca32015-07-17 04:24:18 +02002695static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002696{
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002697 uint32_t p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002698 uint32_t dtaps_per_ptap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002699 uint32_t failed_substage;
2700
Marek Vasut04372fb2015-07-18 02:46:56 +02002701 int ret;
2702
Marek Vasutc336ca32015-07-17 04:24:18 +02002703 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002704
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002705 /* Update info for sims */
2706 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002707 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002708 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002709
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002710 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2711
2712 /* USER Determine number of delay taps for each phase tap. */
Marek Vasutd32badb2015-07-17 03:11:06 +02002713 dtaps_per_ptap = DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP,
2714 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002715
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002716 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002717 /*
2718 * In RLDRAMX we may be messing the delay of pins in
Marek Vasutc336ca32015-07-17 04:24:18 +02002719 * the same write rw_group but outside of the current read
2720 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutac70d2f2015-07-17 03:44:26 +02002721 * output side yet.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002722 */
2723 if (d > 0) {
Marek Vasutf51a7d32015-07-19 02:18:21 +02002724 scc_mgr_apply_group_all_out_delay_add_all_ranks(
Marek Vasutc336ca32015-07-17 04:24:18 +02002725 rw_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002726 }
2727
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002728 for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX; p++) {
Marek Vasut04372fb2015-07-18 02:46:56 +02002729 /* 1) Guaranteed Write */
2730 ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
2731 if (ret)
2732 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002733
Marek Vasutf09da112015-07-18 02:57:32 +02002734 /* 2) DQS Enable Calibration */
2735 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
2736 test_bgn);
2737 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002738 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002739 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002740 }
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002741
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002742 /* 3) Centering DQ/DQS */
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002743 /*
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002744 * If doing read after write calibration, do not update
2745 * FOM now. Do it then.
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002746 */
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002747 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
2748 test_bgn, 1, 0);
2749 if (ret) {
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002750 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002751 continue;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002752 }
2753
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002754 /* All done. */
2755 goto cal_done_ok;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002756 }
2757 }
2758
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002759 /* Calibration Stage 1 failed. */
Marek Vasutc336ca32015-07-17 04:24:18 +02002760 set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002761 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002762
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002763 /* Calibration Stage 1 completed OK. */
2764cal_done_ok:
Dinh Nguyen3da42852015-06-02 22:52:49 -05002765 /*
2766 * Reset the delay chains back to zero if they have moved > 1
2767 * (check for > 1 because loop will increase d even when pass in
2768 * first case).
2769 */
2770 if (d > 2)
Marek Vasutc336ca32015-07-17 04:24:18 +02002771 scc_mgr_zero_group(rw_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002772
2773 return 1;
2774}
2775
2776/* VFIFO Calibration -- Read Deskew Calibration after write deskew */
2777static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
2778 uint32_t test_bgn)
2779{
2780 uint32_t rank_bgn, sr;
2781 uint32_t grp_calibrated;
2782 uint32_t write_group;
2783
2784 debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
2785
2786 /* update info for sims */
2787
2788 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2789 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2790
2791 write_group = read_group;
2792
2793 /* update info for sims */
2794 reg_file_set_group(read_group);
2795
2796 grp_calibrated = 1;
2797 /* Read per-bit deskew can be done on a per shadow register basis */
2798 for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2799 rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
2800 /* Determine if this set of ranks should be skipped entirely */
2801 if (!param->skip_shadow_regs[sr]) {
2802 /* This is the last calibration round, update FOM here */
Marek Vasut98668242015-07-18 20:44:28 +02002803 if (rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002804 read_group,
2805 test_bgn, 0,
2806 1)) {
2807 grp_calibrated = 0;
2808 }
2809 }
2810 }
2811
2812
2813 if (grp_calibrated == 0) {
2814 set_failing_group_stage(write_group,
2815 CAL_STAGE_VFIFO_AFTER_WRITES,
2816 CAL_SUBSTAGE_VFIFO_CENTER);
2817 return 0;
2818 }
2819
2820 return 1;
2821}
2822
2823/* Calibrate LFIFO to find smallest read latency */
2824static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2825{
2826 uint32_t found_one;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002827
2828 debug("%s:%d\n", __func__, __LINE__);
2829
2830 /* update info for sims */
2831 reg_file_set_stage(CAL_STAGE_LFIFO);
2832 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2833
2834 /* Load up the patterns used by read calibration for all ranks */
2835 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2836 found_one = 0;
2837
Dinh Nguyen3da42852015-06-02 22:52:49 -05002838 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002839 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002840 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2841 __func__, __LINE__, gbl->curr_read_lat);
2842
2843 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
2844 NUM_READ_TESTS,
2845 PASS_ALL_BITS,
Marek Vasut96df6032015-07-19 07:35:36 +02002846 1)) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002847 break;
2848 }
2849
2850 found_one = 1;
2851 /* reduce read latency and see if things are working */
2852 /* correctly */
2853 gbl->curr_read_lat--;
2854 } while (gbl->curr_read_lat > 0);
2855
2856 /* reset the fifos to get pointers to known state */
2857
Marek Vasut1273dd92015-07-12 21:05:08 +02002858 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002859
2860 if (found_one) {
2861 /* add a fudge factor to the read latency that was determined */
2862 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002863 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002864 debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
2865 read_lat=%u\n", __func__, __LINE__,
2866 gbl->curr_read_lat);
2867 return 1;
2868 } else {
2869 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2870 CAL_SUBSTAGE_READ_LATENCY);
2871
2872 debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
2873 read_lat=%u\n", __func__, __LINE__,
2874 gbl->curr_read_lat);
2875 return 0;
2876 }
2877}
2878
Marek Vasutc8570af2015-07-21 05:26:58 +02002879/**
2880 * search_window() - Search for the/part of the window with DM/DQS shift
2881 * @search_dm: If 1, search for the DM shift, if 0, search for DQS shift
2882 * @rank_bgn: Rank number
2883 * @write_group: Write Group
2884 * @bgn_curr: Current window begin
2885 * @end_curr: Current window end
2886 * @bgn_best: Current best window begin
2887 * @end_best: Current best window end
2888 * @win_best: Size of the best window
2889 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2890 *
2891 * Search for the/part of the window with DM/DQS shift.
2892 */
2893static void search_window(const int search_dm,
2894 const u32 rank_bgn, const u32 write_group,
2895 int *bgn_curr, int *end_curr, int *bgn_best,
2896 int *end_best, int *win_best, int new_dqs)
2897{
2898 u32 bit_chk;
2899 const int max = IO_IO_OUT1_DELAY_MAX - new_dqs;
2900 int d, di;
2901
2902 /* Search for the/part of the window with DM/DQS shift. */
2903 for (di = max; di >= 0; di -= DELTA_D) {
2904 if (search_dm) {
2905 d = di;
2906 scc_mgr_apply_group_dm_out1_delay(d);
2907 } else {
2908 /* For DQS, we go from 0...max */
2909 d = max - di;
2910 /*
2911 * Note: This only shifts DQS, so are we limiting ourselve to
2912 * width of DQ unnecessarily.
2913 */
2914 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2915 d + new_dqs);
2916 }
2917
2918 writel(0, &sdr_scc_mgr->update);
2919
2920 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2921 PASS_ALL_BITS, &bit_chk,
2922 0)) {
2923 /* Set current end of the window. */
2924 *end_curr = search_dm ? -d : d;
2925
2926 /*
2927 * If a starting edge of our window has not been seen
2928 * this is our current start of the DM window.
2929 */
2930 if (*bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2931 *bgn_curr = search_dm ? -d : d;
2932
2933 /*
2934 * If current window is bigger than best seen.
2935 * Set best seen to be current window.
2936 */
2937 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2938 *win_best = *end_curr - *bgn_curr + 1;
2939 *bgn_best = *bgn_curr;
2940 *end_best = *end_curr;
2941 }
2942 } else {
2943 /* We just saw a failing test. Reset temp edge. */
2944 *bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2945 *end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2946
2947 /* Early exit is only applicable to DQS. */
2948 if (search_dm)
2949 continue;
2950
2951 /*
2952 * Early exit optimization: if the remaining delay
2953 * chain space is less than already seen largest
2954 * window we can exit.
2955 */
2956 if (*win_best - 1 > IO_IO_OUT1_DELAY_MAX - new_dqs - d)
2957 break;
2958 }
2959 }
2960}
2961
Dinh Nguyen3da42852015-06-02 22:52:49 -05002962/*
Marek Vasuta386a502015-07-21 05:33:49 +02002963 * rw_mgr_mem_calibrate_writes_center() - Center all windows
2964 * @rank_bgn: Rank number
2965 * @write_group: Write group
2966 * @test_bgn: Rank at which the test begins
2967 *
2968 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen3da42852015-06-02 22:52:49 -05002969 * certain windows.
2970 */
Marek Vasut3b44f552015-07-21 05:00:42 +02002971static int
2972rw_mgr_mem_calibrate_writes_center(const u32 rank_bgn, const u32 write_group,
2973 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002974{
Marek Vasutc8570af2015-07-21 05:26:58 +02002975 int i;
Marek Vasut3b44f552015-07-21 05:00:42 +02002976 u32 sticky_bit_chk;
2977 u32 min_index;
Marek Vasut3b44f552015-07-21 05:00:42 +02002978 int left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2979 int right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2980 int mid;
2981 int mid_min, orig_mid_min;
2982 int new_dqs, start_dqs;
2983 int dq_margin, dqs_margin, dm_margin;
2984 int bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2985 int end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2986 int bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2987 int end_best = IO_IO_OUT1_DELAY_MAX + 1;
2988 int win_best = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002989
Marek Vasutc4907892015-07-13 02:11:02 +02002990 int ret;
2991
Dinh Nguyen3da42852015-06-02 22:52:49 -05002992 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2993
2994 dm_margin = 0;
2995
Marek Vasutc6540872015-07-21 05:29:05 +02002996 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
2997 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Dinh Nguyen3da42852015-06-02 22:52:49 -05002998 (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2999
Marek Vasut3b44f552015-07-21 05:00:42 +02003000 /* Per-bit deskew. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003001
3002 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003003 * Set the left and right edge of each bit to an illegal value.
3004 * Use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003005 */
3006 sticky_bit_chk = 0;
3007 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
3008 left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
3009 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
3010 }
3011
Marek Vasut3b44f552015-07-21 05:00:42 +02003012 /* Search for the left edge of the window for each bit. */
Marek Vasut71120772015-07-13 02:38:15 +02003013 search_left_edge(1, rank_bgn, write_group, 0, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02003014 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02003015 left_edge, right_edge, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003016
Marek Vasut3b44f552015-07-21 05:00:42 +02003017 /* Search for the right edge of the window for each bit. */
Marek Vasutc4907892015-07-13 02:11:02 +02003018 ret = search_right_edge(1, rank_bgn, write_group, 0,
3019 start_dqs, 0,
Marek Vasut0c4be192015-07-18 20:34:00 +02003020 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02003021 left_edge, right_edge, 0);
3022 if (ret) {
3023 set_failing_group_stage(test_bgn + ret - 1, CAL_STAGE_WRITES,
3024 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutd043ee52015-07-21 05:32:49 +02003025 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003026 }
3027
Marek Vasutafb3eb82015-07-18 19:18:06 +02003028 min_index = get_window_mid_index(1, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003029
Marek Vasut3b44f552015-07-21 05:00:42 +02003030 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003031 orig_mid_min = mid_min;
3032 new_dqs = start_dqs;
3033 mid_min = 0;
Marek Vasut3b44f552015-07-21 05:00:42 +02003034 debug_cond(DLEVEL == 1,
3035 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3036 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003037
Marek Vasutffb8b662015-07-18 19:46:26 +02003038 /* Add delay to bring centre of all DQ windows to the same "level". */
3039 center_dq_windows(1, left_edge, right_edge, mid_min, orig_mid_min,
3040 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003041
3042 /* Move DQS */
3043 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02003044 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003045
3046 /* Centre DM */
3047 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
3048
3049 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003050 * Set the left and right edge of each bit to an illegal value.
3051 * Use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003052 */
3053 left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
3054 right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003055
Marek Vasut3b44f552015-07-21 05:00:42 +02003056 /* Search for the/part of the window with DM shift. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003057 search_window(1, rank_bgn, write_group, &bgn_curr, &end_curr,
3058 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003059
Marek Vasut3b44f552015-07-21 05:00:42 +02003060 /* Reset DM delay chains to 0. */
Marek Vasut32675242015-07-17 06:07:13 +02003061 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003062
3063 /*
3064 * Check to see if the current window nudges up aganist 0 delay.
3065 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasut3b44f552015-07-21 05:00:42 +02003066 * search begins as a new search.
3067 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003068 if (end_curr != 0) {
3069 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3070 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3071 }
3072
Marek Vasut3b44f552015-07-21 05:00:42 +02003073 /* Search for the/part of the window with DQS shifts. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003074 search_window(0, rank_bgn, write_group, &bgn_curr, &end_curr,
3075 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003076
Marek Vasut3b44f552015-07-21 05:00:42 +02003077 /* Assign left and right edge for cal and reporting. */
3078 left_edge[0] = -1 * bgn_best;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003079 right_edge[0] = end_best;
3080
Marek Vasut3b44f552015-07-21 05:00:42 +02003081 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n",
3082 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003083
Marek Vasut3b44f552015-07-21 05:00:42 +02003084 /* Move DQS (back to orig). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003085 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3086
3087 /* Move DM */
3088
Marek Vasut3b44f552015-07-21 05:00:42 +02003089 /* Find middle of window for the DM bit. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003090 mid = (left_edge[0] - right_edge[0]) / 2;
3091
Marek Vasut3b44f552015-07-21 05:00:42 +02003092 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003093 if (mid < 0)
3094 mid = 0;
3095
Marek Vasut3b44f552015-07-21 05:00:42 +02003096 /* dm_marign should fail if we never find a window. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003097 if (win_best == 0)
3098 dm_margin = -1;
3099 else
3100 dm_margin = left_edge[0] - mid;
3101
Marek Vasut32675242015-07-17 06:07:13 +02003102 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003103 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003104
Marek Vasut3b44f552015-07-21 05:00:42 +02003105 debug_cond(DLEVEL == 2,
3106 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3107 __func__, __LINE__, left_edge[0], right_edge[0],
3108 mid, dm_margin);
3109 /* Export values. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003110 gbl->fom_out += dq_margin + dqs_margin;
3111
Marek Vasut3b44f552015-07-21 05:00:42 +02003112 debug_cond(DLEVEL == 2,
3113 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3114 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003115
3116 /*
3117 * Do not remove this line as it makes sure all of our
3118 * decisions have been applied.
3119 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003120 writel(0, &sdr_scc_mgr->update);
Marek Vasut3b44f552015-07-21 05:00:42 +02003121
Marek Vasutd043ee52015-07-21 05:32:49 +02003122 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3123 return -EINVAL;
3124
3125 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003126}
3127
Marek Vasutdb3a6062015-07-18 07:23:25 +02003128/**
3129 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3130 * @rank_bgn: Rank number
3131 * @group: Read/Write Group
3132 * @test_bgn: Rank at which the test begins
3133 *
3134 * Stage 2: Write Calibration Part One.
3135 *
3136 * This function implements UniPHY calibration Stage 2, as explained in
3137 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3138 */
3139static int rw_mgr_mem_calibrate_writes(const u32 rank_bgn, const u32 group,
3140 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003141{
Marek Vasutdb3a6062015-07-18 07:23:25 +02003142 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003143
Marek Vasutdb3a6062015-07-18 07:23:25 +02003144 /* Update info for sims */
3145 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3146
3147 reg_file_set_group(group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003148 reg_file_set_stage(CAL_STAGE_WRITES);
3149 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3150
Marek Vasutdb3a6062015-07-18 07:23:25 +02003151 ret = rw_mgr_mem_calibrate_writes_center(rank_bgn, group, test_bgn);
Marek Vasutd043ee52015-07-21 05:32:49 +02003152 if (ret)
Marek Vasutdb3a6062015-07-18 07:23:25 +02003153 set_failing_group_stage(group, CAL_STAGE_WRITES,
Dinh Nguyen3da42852015-06-02 22:52:49 -05003154 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003155
Marek Vasutd043ee52015-07-21 05:32:49 +02003156 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003157}
3158
Marek Vasut4b0ac262015-07-20 07:33:33 +02003159/**
3160 * mem_precharge_and_activate() - Precharge all banks and activate
3161 *
3162 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3163 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003164static void mem_precharge_and_activate(void)
3165{
Marek Vasut4b0ac262015-07-20 07:33:33 +02003166 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003167
3168 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
Marek Vasut4b0ac262015-07-20 07:33:33 +02003169 /* Test if the rank should be skipped. */
3170 if (param->skip_ranks[r])
Dinh Nguyen3da42852015-06-02 22:52:49 -05003171 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003172
Marek Vasut4b0ac262015-07-20 07:33:33 +02003173 /* Set rank. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003174 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3175
Marek Vasut4b0ac262015-07-20 07:33:33 +02003176 /* Precharge all banks. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003177 writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3178 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003179
Marek Vasut1273dd92015-07-12 21:05:08 +02003180 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3181 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3182 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003183
Marek Vasut1273dd92015-07-12 21:05:08 +02003184 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3185 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3186 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003187
Marek Vasut4b0ac262015-07-20 07:33:33 +02003188 /* Activate rows. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003189 writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3190 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003191 }
3192}
3193
Marek Vasut16502a02015-07-17 01:57:41 +02003194/**
3195 * mem_init_latency() - Configure memory RLAT and WLAT settings
3196 *
3197 * Configure memory RLAT and WLAT parameters.
3198 */
3199static void mem_init_latency(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003200{
Marek Vasut16502a02015-07-17 01:57:41 +02003201 /*
3202 * For AV/CV, LFIFO is hardened and always runs at full rate
3203 * so max latency in AFI clocks, used here, is correspondingly
3204 * smaller.
3205 */
3206 const u32 max_latency = (1 << MAX_LATENCY_COUNT_WIDTH) - 1;
3207 u32 rlat, wlat;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003208
3209 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut16502a02015-07-17 01:57:41 +02003210
3211 /*
3212 * Read in write latency.
3213 * WL for Hard PHY does not include additive latency.
3214 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003215 wlat = readl(&data_mgr->t_wl_add);
3216 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003217
Marek Vasut16502a02015-07-17 01:57:41 +02003218 gbl->rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003219
Marek Vasut16502a02015-07-17 01:57:41 +02003220 /* Read in readl latency. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003221 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003222
Marek Vasut16502a02015-07-17 01:57:41 +02003223 /* Set a pretty high read latency initially. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003224 gbl->curr_read_lat = rlat + 16;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003225 if (gbl->curr_read_lat > max_latency)
3226 gbl->curr_read_lat = max_latency;
3227
Marek Vasut1273dd92015-07-12 21:05:08 +02003228 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003229
Marek Vasut16502a02015-07-17 01:57:41 +02003230 /* Advertise write latency. */
3231 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003232}
3233
Marek Vasut51cea0b2015-07-26 10:54:15 +02003234/**
3235 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3236 *
3237 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3238 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003239static void mem_skip_calibrate(void)
3240{
3241 uint32_t vfifo_offset;
3242 uint32_t i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003243
3244 debug("%s:%d\n", __func__, __LINE__);
3245 /* Need to update every shadow register set used by the interface */
3246 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003247 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003248 /*
3249 * Set output phase alignment settings appropriate for
3250 * skip calibration.
3251 */
3252 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3253 scc_mgr_set_dqs_en_phase(i, 0);
3254#if IO_DLL_CHAIN_LENGTH == 6
3255 scc_mgr_set_dqdqs_output_phase(i, 6);
3256#else
3257 scc_mgr_set_dqdqs_output_phase(i, 7);
3258#endif
3259 /*
3260 * Case:33398
3261 *
3262 * Write data arrives to the I/O two cycles before write
3263 * latency is reached (720 deg).
3264 * -> due to bit-slip in a/c bus
3265 * -> to allow board skew where dqs is longer than ck
3266 * -> how often can this happen!?
3267 * -> can claim back some ptaps for high freq
3268 * support if we can relax this, but i digress...
3269 *
3270 * The write_clk leads mem_ck by 90 deg
3271 * The minimum ptap of the OPA is 180 deg
3272 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3273 * The write_clk is always delayed by 2 ptaps
3274 *
3275 * Hence, to make DQS aligned to CK, we need to delay
3276 * DQS by:
3277 * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3278 *
3279 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3280 * gives us the number of ptaps, which simplies to:
3281 *
3282 * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3283 */
Marek Vasut51cea0b2015-07-26 10:54:15 +02003284 scc_mgr_set_dqdqs_output_phase(i,
3285 1.25 * IO_DLL_CHAIN_LENGTH - 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003286 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003287 writel(0xff, &sdr_scc_mgr->dqs_ena);
3288 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003289
Dinh Nguyen3da42852015-06-02 22:52:49 -05003290 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003291 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3292 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003293 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003294 writel(0xff, &sdr_scc_mgr->dq_ena);
3295 writel(0xff, &sdr_scc_mgr->dm_ena);
3296 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003297 }
3298
3299 /* Compensate for simulation model behaviour */
3300 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3301 scc_mgr_set_dqs_bus_in_delay(i, 10);
3302 scc_mgr_load_dqs(i);
3303 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003304 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003305
3306 /*
3307 * ArriaV has hard FIFOs that can only be initialized by incrementing
3308 * in sequencer.
3309 */
3310 vfifo_offset = CALIB_VFIFO_OFFSET;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003311 for (j = 0; j < vfifo_offset; j++)
Marek Vasut1273dd92015-07-12 21:05:08 +02003312 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasut1273dd92015-07-12 21:05:08 +02003313 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003314
3315 /*
Marek Vasut51cea0b2015-07-26 10:54:15 +02003316 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3317 * setting from generation-time constant.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003318 */
3319 gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
Marek Vasut1273dd92015-07-12 21:05:08 +02003320 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003321}
3322
Marek Vasut3589fbf2015-07-20 04:34:51 +02003323/**
3324 * mem_calibrate() - Memory calibration entry point.
3325 *
3326 * Perform memory calibration.
3327 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003328static uint32_t mem_calibrate(void)
3329{
3330 uint32_t i;
3331 uint32_t rank_bgn, sr;
3332 uint32_t write_group, write_test_bgn;
3333 uint32_t read_group, read_test_bgn;
3334 uint32_t run_groups, current_run;
3335 uint32_t failing_groups = 0;
3336 uint32_t group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003337
Marek Vasut33c42bb2015-07-17 02:21:47 +02003338 const u32 rwdqs_ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
3339 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
3340
Dinh Nguyen3da42852015-06-02 22:52:49 -05003341 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003342
Marek Vasut16502a02015-07-17 01:57:41 +02003343 /* Initialize the data settings */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003344 gbl->error_substage = CAL_SUBSTAGE_NIL;
3345 gbl->error_stage = CAL_STAGE_NIL;
3346 gbl->error_group = 0xff;
3347 gbl->fom_in = 0;
3348 gbl->fom_out = 0;
3349
Marek Vasut16502a02015-07-17 01:57:41 +02003350 /* Initialize WLAT and RLAT. */
3351 mem_init_latency();
3352
3353 /* Initialize bit slips. */
3354 mem_precharge_and_activate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003355
Dinh Nguyen3da42852015-06-02 22:52:49 -05003356 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003357 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3358 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003359 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3360 if (i == 0)
3361 scc_mgr_set_hhp_extras();
3362
Marek Vasutc5c5f532015-07-17 02:06:20 +02003363 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003364 }
3365
Marek Vasut722c9682015-07-17 02:07:12 +02003366 /* Calibration is skipped. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003367 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3368 /*
3369 * Set VFIFO and LFIFO to instant-on settings in skip
3370 * calibration mode.
3371 */
3372 mem_skip_calibrate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003373
Marek Vasut722c9682015-07-17 02:07:12 +02003374 /*
3375 * Do not remove this line as it makes sure all of our
3376 * decisions have been applied.
3377 */
3378 writel(0, &sdr_scc_mgr->update);
3379 return 1;
3380 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003381
Marek Vasut722c9682015-07-17 02:07:12 +02003382 /* Calibration is not skipped. */
3383 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3384 /*
3385 * Zero all delay chain/phase settings for all
3386 * groups and all shadow register sets.
3387 */
3388 scc_mgr_zero_all();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003389
Marek Vasut722c9682015-07-17 02:07:12 +02003390 run_groups = ~param->skip_groups;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003391
Marek Vasut722c9682015-07-17 02:07:12 +02003392 for (write_group = 0, write_test_bgn = 0; write_group
3393 < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3394 write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003395
3396 /* Initialize the group failure */
Marek Vasut722c9682015-07-17 02:07:12 +02003397 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003398
Marek Vasut722c9682015-07-17 02:07:12 +02003399 current_run = run_groups & ((1 <<
3400 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3401 run_groups = run_groups >>
3402 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003403
Marek Vasut722c9682015-07-17 02:07:12 +02003404 if (current_run == 0)
3405 continue;
3406
3407 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3408 SCC_MGR_GROUP_COUNTER_OFFSET);
3409 scc_mgr_zero_group(write_group, 0);
3410
Marek Vasut33c42bb2015-07-17 02:21:47 +02003411 for (read_group = write_group * rwdqs_ratio,
3412 read_test_bgn = 0;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003413 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003414 read_group++,
3415 read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
3416 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3417 continue;
Marek Vasut722c9682015-07-17 02:07:12 +02003418
Marek Vasut33c42bb2015-07-17 02:21:47 +02003419 /* Calibrate the VFIFO */
3420 if (rw_mgr_mem_calibrate_vfifo(read_group,
3421 read_test_bgn))
3422 continue;
3423
Marek Vasutc452dcd2015-07-17 02:50:56 +02003424 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3425 return 0;
3426
3427 /* The group failed, we're done. */
3428 goto grp_failed;
3429 }
3430
3431 /* Calibrate the output side */
3432 for (rank_bgn = 0, sr = 0;
3433 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
3434 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3435 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3436 continue;
3437
3438 /* Not needed in quick mode! */
3439 if (STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS)
3440 continue;
3441
3442 /*
3443 * Determine if this set of ranks
3444 * should be skipped entirely.
3445 */
3446 if (param->skip_shadow_regs[sr])
3447 continue;
3448
3449 /* Calibrate WRITEs */
Marek Vasutdb3a6062015-07-18 07:23:25 +02003450 if (!rw_mgr_mem_calibrate_writes(rank_bgn,
Marek Vasutc452dcd2015-07-17 02:50:56 +02003451 write_group, write_test_bgn))
3452 continue;
3453
Marek Vasut33c42bb2015-07-17 02:21:47 +02003454 group_failed = 1;
3455 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3456 return 0;
Marek Vasut722c9682015-07-17 02:07:12 +02003457 }
3458
Marek Vasutc452dcd2015-07-17 02:50:56 +02003459 /* Some group failed, we're done. */
3460 if (group_failed)
3461 goto grp_failed;
Marek Vasut4ac21612015-07-17 02:31:04 +02003462
Marek Vasutc452dcd2015-07-17 02:50:56 +02003463 for (read_group = write_group * rwdqs_ratio,
3464 read_test_bgn = 0;
3465 read_group < (write_group + 1) * rwdqs_ratio;
3466 read_group++,
3467 read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
3468 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3469 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003470
Marek Vasutc452dcd2015-07-17 02:50:56 +02003471 if (rw_mgr_mem_calibrate_vfifo_end(read_group,
3472 read_test_bgn))
3473 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003474
Marek Vasutc452dcd2015-07-17 02:50:56 +02003475 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3476 return 0;
Marek Vasut4ac21612015-07-17 02:31:04 +02003477
Marek Vasutc452dcd2015-07-17 02:50:56 +02003478 /* The group failed, we're done. */
3479 goto grp_failed;
Marek Vasut722c9682015-07-17 02:07:12 +02003480 }
3481
Marek Vasutc452dcd2015-07-17 02:50:56 +02003482 /* No group failed, continue as usual. */
3483 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003484
Marek Vasutc452dcd2015-07-17 02:50:56 +02003485grp_failed: /* A group failed, increment the counter. */
3486 failing_groups++;
Marek Vasut722c9682015-07-17 02:07:12 +02003487 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003488
Marek Vasut722c9682015-07-17 02:07:12 +02003489 /*
3490 * USER If there are any failing groups then report
3491 * the failure.
3492 */
3493 if (failing_groups != 0)
3494 return 0;
3495
Marek Vasutc50ae302015-07-17 02:40:21 +02003496 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3497 continue;
3498
3499 /*
3500 * If we're skipping groups as part of debug,
3501 * don't calibrate LFIFO.
3502 */
3503 if (param->skip_groups != 0)
3504 continue;
3505
Marek Vasut722c9682015-07-17 02:07:12 +02003506 /* Calibrate the LFIFO */
Marek Vasutc50ae302015-07-17 02:40:21 +02003507 if (!rw_mgr_mem_calibrate_lfifo())
3508 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003509 }
3510
3511 /*
3512 * Do not remove this line as it makes sure all of our decisions
3513 * have been applied.
3514 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003515 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003516 return 1;
3517}
3518
Marek Vasut23a040c2015-07-17 01:20:21 +02003519/**
3520 * run_mem_calibrate() - Perform memory calibration
3521 *
3522 * This function triggers the entire memory calibration procedure.
3523 */
3524static int run_mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003525{
Marek Vasut23a040c2015-07-17 01:20:21 +02003526 int pass;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003527
3528 debug("%s:%d\n", __func__, __LINE__);
3529
3530 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003531 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003532
Marek Vasut23a040c2015-07-17 01:20:21 +02003533 /* Stop tracking manager. */
3534 clrbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003535
Marek Vasut9fa9c902015-07-17 01:12:07 +02003536 phy_mgr_initialize();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003537 rw_mgr_mem_initialize();
3538
Marek Vasut23a040c2015-07-17 01:20:21 +02003539 /* Perform the actual memory calibration. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003540 pass = mem_calibrate();
3541
3542 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003543 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003544
Marek Vasut23a040c2015-07-17 01:20:21 +02003545 /* Handoff. */
3546 rw_mgr_mem_handoff();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003547 /*
Marek Vasut23a040c2015-07-17 01:20:21 +02003548 * In Hard PHY this is a 2-bit control:
3549 * 0: AFI Mux Select
3550 * 1: DDIO Mux Select
Dinh Nguyen3da42852015-06-02 22:52:49 -05003551 */
Marek Vasut23a040c2015-07-17 01:20:21 +02003552 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003553
Marek Vasut23a040c2015-07-17 01:20:21 +02003554 /* Start tracking manager. */
3555 setbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
3556
3557 return pass;
3558}
3559
3560/**
3561 * debug_mem_calibrate() - Report result of memory calibration
3562 * @pass: Value indicating whether calibration passed or failed
3563 *
3564 * This function reports the results of the memory calibration
3565 * and writes debug information into the register file.
3566 */
3567static void debug_mem_calibrate(int pass)
3568{
3569 uint32_t debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003570
3571 if (pass) {
3572 printf("%s: CALIBRATION PASSED\n", __FILE__);
3573
3574 gbl->fom_in /= 2;
3575 gbl->fom_out /= 2;
3576
3577 if (gbl->fom_in > 0xff)
3578 gbl->fom_in = 0xff;
3579
3580 if (gbl->fom_out > 0xff)
3581 gbl->fom_out = 0xff;
3582
3583 /* Update the FOM in the register file */
3584 debug_info = gbl->fom_in;
3585 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003586 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003587
Marek Vasut1273dd92015-07-12 21:05:08 +02003588 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3589 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003590 } else {
3591 printf("%s: CALIBRATION FAILED\n", __FILE__);
3592
3593 debug_info = gbl->error_stage;
3594 debug_info |= gbl->error_substage << 8;
3595 debug_info |= gbl->error_group << 16;
3596
Marek Vasut1273dd92015-07-12 21:05:08 +02003597 writel(debug_info, &sdr_reg_file->failing_stage);
3598 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3599 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003600
3601 /* Update the failing group/stage in the register file */
3602 debug_info = gbl->error_stage;
3603 debug_info |= gbl->error_substage << 8;
3604 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003605 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003606 }
3607
Marek Vasut23a040c2015-07-17 01:20:21 +02003608 printf("%s: Calibration complete\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003609}
3610
Marek Vasutbb064342015-07-19 06:12:42 +02003611/**
3612 * hc_initialize_rom_data() - Initialize ROM data
3613 *
3614 * Initialize ROM data.
3615 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003616static void hc_initialize_rom_data(void)
3617{
Marek Vasutbb064342015-07-19 06:12:42 +02003618 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003619
Marek Vasutc4815f72015-07-12 19:03:33 +02003620 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003621 for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3622 writel(inst_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003623
Marek Vasutc4815f72015-07-12 19:03:33 +02003624 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003625 for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3626 writel(ac_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003627}
3628
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003629/**
3630 * initialize_reg_file() - Initialize SDR register file
3631 *
3632 * Initialize SDR register file.
3633 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003634static void initialize_reg_file(void)
3635{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003636 /* Initialize the register file with the correct data */
Marek Vasut1273dd92015-07-12 21:05:08 +02003637 writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3638 writel(0, &sdr_reg_file->debug_data_addr);
3639 writel(0, &sdr_reg_file->cur_stage);
3640 writel(0, &sdr_reg_file->fom);
3641 writel(0, &sdr_reg_file->failing_stage);
3642 writel(0, &sdr_reg_file->debug1);
3643 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003644}
3645
Marek Vasut2ca151f2015-07-19 06:14:04 +02003646/**
3647 * initialize_hps_phy() - Initialize HPS PHY
3648 *
3649 * Initialize HPS PHY.
3650 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003651static void initialize_hps_phy(void)
3652{
3653 uint32_t reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003654 /*
3655 * Tracking also gets configured here because it's in the
3656 * same register.
3657 */
3658 uint32_t trk_sample_count = 7500;
3659 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3660 /*
3661 * Format is number of outer loops in the 16 MSB, sample
3662 * count in 16 LSB.
3663 */
3664
3665 reg = 0;
3666 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3667 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3668 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3669 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3670 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3671 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3672 /*
3673 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3674 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3675 */
3676 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3677 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3678 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003679 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003680
3681 reg = 0;
3682 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3683 trk_sample_count >>
3684 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3685 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3686 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003687 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003688
3689 reg = 0;
3690 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3691 trk_long_idle_sample_count >>
3692 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003693 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003694}
3695
Marek Vasut880e46f2015-07-17 00:45:11 +02003696/**
3697 * initialize_tracking() - Initialize tracking
3698 *
3699 * Initialize the register file with usable initial data.
3700 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003701static void initialize_tracking(void)
3702{
Marek Vasut880e46f2015-07-17 00:45:11 +02003703 /*
3704 * Initialize the register file with the correct data.
3705 * Compute usable version of value in case we skip full
3706 * computation later.
3707 */
3708 writel(DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP) - 1,
3709 &sdr_reg_file->dtaps_per_ptap);
3710
3711 /* trk_sample_count */
3712 writel(7500, &sdr_reg_file->trk_sample_count);
3713
3714 /* longidle outer loop [15:0] */
3715 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003716
3717 /*
Marek Vasut880e46f2015-07-17 00:45:11 +02003718 * longidle sample count [31:24]
3719 * trfc, worst case of 933Mhz 4Gb [23:16]
3720 * trcd, worst case [15:8]
3721 * vfifo wait [7:0]
Dinh Nguyen3da42852015-06-02 22:52:49 -05003722 */
Marek Vasut880e46f2015-07-17 00:45:11 +02003723 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3724 &sdr_reg_file->delays);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003725
Marek Vasut880e46f2015-07-17 00:45:11 +02003726 /* mux delay */
3727 writel((RW_MGR_IDLE << 24) | (RW_MGR_ACTIVATE_1 << 16) |
3728 (RW_MGR_SGLE_READ << 8) | (RW_MGR_PRECHARGE_ALL << 0),
3729 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003730
Marek Vasut880e46f2015-07-17 00:45:11 +02003731 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH,
3732 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003733
Marek Vasut880e46f2015-07-17 00:45:11 +02003734 /* trefi [7:0] */
3735 writel((RW_MGR_REFRESH_ALL << 24) | (1000 << 0),
3736 &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003737}
3738
3739int sdram_calibration_full(void)
3740{
3741 struct param_type my_param;
3742 struct gbl_type my_gbl;
3743 uint32_t pass;
Marek Vasut84e0b0c2015-07-17 01:05:36 +02003744
3745 memset(&my_param, 0, sizeof(my_param));
3746 memset(&my_gbl, 0, sizeof(my_gbl));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003747
3748 param = &my_param;
3749 gbl = &my_gbl;
3750
Dinh Nguyen3da42852015-06-02 22:52:49 -05003751 /* Set the calibration enabled by default */
3752 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3753 /*
3754 * Only sweep all groups (regardless of fail state) by default
3755 * Set enabled read test by default.
3756 */
3757#if DISABLE_GUARANTEED_READ
3758 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3759#endif
3760 /* Initialize the register file */
3761 initialize_reg_file();
3762
3763 /* Initialize any PHY CSR */
3764 initialize_hps_phy();
3765
3766 scc_mgr_initialize();
3767
3768 initialize_tracking();
3769
Dinh Nguyen3da42852015-06-02 22:52:49 -05003770 printf("%s: Preparing to start memory calibration\n", __FILE__);
3771
3772 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut23f62b32015-07-13 01:05:27 +02003773 debug_cond(DLEVEL == 1,
3774 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3775 RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3776 RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3777 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3778 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3779 debug_cond(DLEVEL == 1,
3780 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3781 RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3782 RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3783 IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3784 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3785 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3786 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3787 IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3788 IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3789 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3790 IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3791 IO_IO_OUT2_DELAY_MAX);
3792 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3793 IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003794
3795 hc_initialize_rom_data();
3796
3797 /* update info for sims */
3798 reg_file_set_stage(CAL_STAGE_NIL);
3799 reg_file_set_group(0);
3800
3801 /*
3802 * Load global needed for those actions that require
3803 * some dynamic calibration support.
3804 */
3805 dyn_calib_steps = STATIC_CALIB_STEPS;
3806 /*
3807 * Load global to allow dynamic selection of delay loop settings
3808 * based on calibration mode.
3809 */
3810 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3811 skip_delay_mask = 0xff;
3812 else
3813 skip_delay_mask = 0x0;
3814
3815 pass = run_mem_calibrate();
Marek Vasut23a040c2015-07-17 01:20:21 +02003816 debug_mem_calibrate(pass);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003817 return pass;
3818}