blob: ed4d791af0b1ab2da0cfc433a5b3d5bbbb417bc2 [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 */
Marek Vasut90a584b2015-07-26 11:11:28 +0200761static void delay_for_n_mem_clocks(const u32 clocks)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500762{
Marek Vasut90a584b2015-07-26 11:11:28 +0200763 u32 afi_clocks;
Marek Vasut6a39be62015-07-26 11:42:53 +0200764 u16 c_loop;
765 u8 inner;
766 u8 outer;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500767
768 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
769
Marek Vasutcbcaf462015-07-26 11:34:09 +0200770 /* Scale (rounding up) to get afi clocks. */
Marek Vasut90a584b2015-07-26 11:11:28 +0200771 afi_clocks = DIV_ROUND_UP(clocks, AFI_RATE_RATIO);
Marek Vasutcbcaf462015-07-26 11:34:09 +0200772 if (afi_clocks) /* Temporary underflow protection */
773 afi_clocks--;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500774
775 /*
Marek Vasut90a584b2015-07-26 11:11:28 +0200776 * Note, we don't bother accounting for being off a little
777 * bit because of a few extra instructions in outer loops.
778 * Note, the loops have a test at the end, and do the test
779 * before the decrement, and so always perform the loop
Dinh Nguyen3da42852015-06-02 22:52:49 -0500780 * 1 time more than the counter value
781 */
Marek Vasut6a39be62015-07-26 11:42:53 +0200782 c_loop = afi_clocks >> 16;
783 outer = c_loop ? 0xff : (afi_clocks >> 8);
784 inner = outer ? 0xff : afi_clocks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500785
786 /*
787 * rom instructions are structured as follows:
788 *
789 * IDLE_LOOP2: jnz cntr0, TARGET_A
790 * IDLE_LOOP1: jnz cntr1, TARGET_B
791 * return
792 *
793 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
794 * TARGET_B is set to IDLE_LOOP2 as well
795 *
796 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
797 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
798 *
799 * a little confusing, but it helps save precious space in the inst_rom
800 * and sequencer rom and keeps the delays more accurate and reduces
801 * overhead
802 */
Marek Vasutcbcaf462015-07-26 11:34:09 +0200803 if (afi_clocks < 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200804 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
805 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500806
Marek Vasut1273dd92015-07-12 21:05:08 +0200807 writel(RW_MGR_IDLE_LOOP1,
808 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500809
Marek Vasut1273dd92015-07-12 21:05:08 +0200810 writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
811 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500812 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200813 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
814 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500815
Marek Vasut1273dd92015-07-12 21:05:08 +0200816 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
817 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500818
Marek Vasut1273dd92015-07-12 21:05:08 +0200819 writel(RW_MGR_IDLE_LOOP2,
820 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500821
Marek Vasut1273dd92015-07-12 21:05:08 +0200822 writel(RW_MGR_IDLE_LOOP2,
823 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500824
825 /* hack to get around compiler not being smart enough */
826 if (afi_clocks <= 0x10000) {
827 /* only need to run once */
Marek Vasut1273dd92015-07-12 21:05:08 +0200828 writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
829 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500830 } else {
831 do {
Marek Vasut1273dd92015-07-12 21:05:08 +0200832 writel(RW_MGR_IDLE_LOOP2,
833 SDR_PHYGRP_RWMGRGRP_ADDRESS |
834 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500835 } while (c_loop-- != 0);
836 }
837 }
838 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
839}
840
Marek Vasut944fe712015-07-13 00:44:30 +0200841/**
842 * rw_mgr_mem_init_load_regs() - Load instruction registers
843 * @cntr0: Counter 0 value
844 * @cntr1: Counter 1 value
845 * @cntr2: Counter 2 value
846 * @jump: Jump instruction value
847 *
848 * Load instruction registers.
849 */
850static void rw_mgr_mem_init_load_regs(u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
851{
852 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
853 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
854
855 /* Load counters */
856 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
857 &sdr_rw_load_mgr_regs->load_cntr0);
858 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
859 &sdr_rw_load_mgr_regs->load_cntr1);
860 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
861 &sdr_rw_load_mgr_regs->load_cntr2);
862
863 /* Load jump address */
864 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
865 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
866 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
867
868 /* Execute count instruction */
869 writel(jump, grpaddr);
870}
871
Marek Vasutecd23342015-07-13 00:51:05 +0200872/**
873 * rw_mgr_mem_load_user() - Load user calibration values
874 * @fin1: Final instruction 1
875 * @fin2: Final instruction 2
876 * @precharge: If 1, precharge the banks at the end
877 *
878 * Load user calibration values and optionally precharge the banks.
879 */
880static void rw_mgr_mem_load_user(const u32 fin1, const u32 fin2,
881 const int precharge)
882{
883 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
884 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
885 u32 r;
886
887 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
888 if (param->skip_ranks[r]) {
889 /* request to skip the rank */
890 continue;
891 }
892
893 /* set rank */
894 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
895
896 /* precharge all banks ... */
897 if (precharge)
898 writel(RW_MGR_PRECHARGE_ALL, grpaddr);
899
900 /*
901 * USER Use Mirror-ed commands for odd ranks if address
902 * mirrorring is on
903 */
904 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
905 set_jump_as_return();
906 writel(RW_MGR_MRS2_MIRR, grpaddr);
907 delay_for_n_mem_clocks(4);
908 set_jump_as_return();
909 writel(RW_MGR_MRS3_MIRR, grpaddr);
910 delay_for_n_mem_clocks(4);
911 set_jump_as_return();
912 writel(RW_MGR_MRS1_MIRR, grpaddr);
913 delay_for_n_mem_clocks(4);
914 set_jump_as_return();
915 writel(fin1, grpaddr);
916 } else {
917 set_jump_as_return();
918 writel(RW_MGR_MRS2, grpaddr);
919 delay_for_n_mem_clocks(4);
920 set_jump_as_return();
921 writel(RW_MGR_MRS3, grpaddr);
922 delay_for_n_mem_clocks(4);
923 set_jump_as_return();
924 writel(RW_MGR_MRS1, grpaddr);
925 set_jump_as_return();
926 writel(fin2, grpaddr);
927 }
928
929 if (precharge)
930 continue;
931
932 set_jump_as_return();
933 writel(RW_MGR_ZQCL, grpaddr);
934
935 /* tZQinit = tDLLK = 512 ck cycles */
936 delay_for_n_mem_clocks(512);
937 }
938}
939
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200940/**
941 * rw_mgr_mem_initialize() - Initialize RW Manager
942 *
943 * Initialize RW Manager.
944 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500945static void rw_mgr_mem_initialize(void)
946{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500947 debug("%s:%d\n", __func__, __LINE__);
948
949 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200950 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
951 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500952
953 /*
954 * Here's how you load register for a loop
955 * Counters are located @ 0x800
956 * Jump address are located @ 0xC00
957 * For both, registers 0 to 3 are selected using bits 3 and 2, like
958 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
959 * I know this ain't pretty, but Avalon bus throws away the 2 least
960 * significant bits
961 */
962
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200963 /* Start with memory RESET activated */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500964
965 /* tINIT = 200us */
966
967 /*
968 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
969 * If a and b are the number of iteration in 2 nested loops
970 * it takes the following number of cycles to complete the operation:
971 * number_of_cycles = ((2 + n) * a + 2) * b
972 * where n is the number of instruction in the inner loop
973 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
974 * b = 6A
975 */
Marek Vasut944fe712015-07-13 00:44:30 +0200976 rw_mgr_mem_init_load_regs(SEQ_TINIT_CNTR0_VAL, SEQ_TINIT_CNTR1_VAL,
977 SEQ_TINIT_CNTR2_VAL,
978 RW_MGR_INIT_RESET_0_CKE_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500979
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200980 /* Indicate that memory is stable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200981 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500982
983 /*
984 * transition the RESET to high
985 * Wait for 500us
986 */
987
988 /*
989 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
990 * If a and b are the number of iteration in 2 nested loops
991 * it takes the following number of cycles to complete the operation
992 * number_of_cycles = ((2 + n) * a + 2) * b
993 * where n is the number of instruction in the inner loop
994 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
995 * b = FF
996 */
Marek Vasut944fe712015-07-13 00:44:30 +0200997 rw_mgr_mem_init_load_regs(SEQ_TRESET_CNTR0_VAL, SEQ_TRESET_CNTR1_VAL,
998 SEQ_TRESET_CNTR2_VAL,
999 RW_MGR_INIT_RESET_1_CKE_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001000
Marek Vasut8e9d7d02015-07-26 10:57:06 +02001001 /* Bring up clock enable. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001002
1003 /* tXRP < 250 ck cycles */
1004 delay_for_n_mem_clocks(250);
1005
Marek Vasutecd23342015-07-13 00:51:05 +02001006 rw_mgr_mem_load_user(RW_MGR_MRS0_DLL_RESET_MIRR, RW_MGR_MRS0_DLL_RESET,
1007 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001008}
1009
Marek Vasutf1f22f72015-07-26 10:59:19 +02001010/**
1011 * rw_mgr_mem_handoff() - Hand off the memory to user
1012 *
1013 * At the end of calibration we have to program the user settings in
1014 * and hand off the memory to the user.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001015 */
1016static void rw_mgr_mem_handoff(void)
1017{
Marek Vasutecd23342015-07-13 00:51:05 +02001018 rw_mgr_mem_load_user(RW_MGR_MRS0_USER_MIRR, RW_MGR_MRS0_USER, 1);
1019 /*
Marek Vasutf1f22f72015-07-26 10:59:19 +02001020 * Need to wait tMOD (12CK or 15ns) time before issuing other
1021 * commands, but we will have plenty of NIOS cycles before actual
1022 * handoff so its okay.
Marek Vasutecd23342015-07-13 00:51:05 +02001023 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001024}
1025
Marek Vasut8371c2e2015-07-21 06:00:36 +02001026/**
1027 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1028 * @group: Write Group
1029 * @use_dm: Use DM
1030 *
1031 * Issue write test command. Two variants are provided, one that just tests
1032 * a write pattern and another that tests datamask functionality.
Marek Vasutad64769c2015-07-21 05:43:37 +02001033 */
Marek Vasut8371c2e2015-07-21 06:00:36 +02001034static void rw_mgr_mem_calibrate_write_test_issue(u32 group,
1035 u32 test_dm)
Marek Vasutad64769c2015-07-21 05:43:37 +02001036{
Marek Vasut8371c2e2015-07-21 06:00:36 +02001037 const u32 quick_write_mode =
1038 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
1039 ENABLE_SUPER_QUICK_CALIBRATION;
1040 u32 mcc_instruction;
1041 u32 rw_wl_nop_cycles;
Marek Vasutad64769c2015-07-21 05:43:37 +02001042
1043 /*
1044 * Set counter and jump addresses for the right
1045 * number of NOP cycles.
1046 * The number of supported NOP cycles can range from -1 to infinity
1047 * Three different cases are handled:
1048 *
1049 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1050 * mechanism will be used to insert the right number of NOPs
1051 *
1052 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1053 * issuing the write command will jump straight to the
1054 * micro-instruction that turns on DQS (for DDRx), or outputs write
1055 * data (for RLD), skipping
1056 * the NOP micro-instruction all together
1057 *
1058 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1059 * turned on in the same micro-instruction that issues the write
1060 * command. Then we need
1061 * to directly jump to the micro-instruction that sends out the data
1062 *
1063 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1064 * (2 and 3). One jump-counter (0) is used to perform multiple
1065 * write-read operations.
1066 * one counter left to issue this command in "multiple-group" mode
1067 */
1068
1069 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
1070
1071 if (rw_wl_nop_cycles == -1) {
1072 /*
1073 * CNTR 2 - We want to execute the special write operation that
1074 * turns on DQS right away and then skip directly to the
1075 * instruction that sends out the data. We set the counter to a
1076 * large number so that the jump is always taken.
1077 */
1078 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1079
1080 /* CNTR 3 - Not used */
1081 if (test_dm) {
1082 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
1083 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
1084 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1085 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
1086 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1087 } else {
1088 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
1089 writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
1090 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1091 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
1092 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1093 }
1094 } else if (rw_wl_nop_cycles == 0) {
1095 /*
1096 * CNTR 2 - We want to skip the NOP operation and go straight
1097 * to the DQS enable instruction. We set the counter to a large
1098 * number so that the jump is always taken.
1099 */
1100 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1101
1102 /* CNTR 3 - Not used */
1103 if (test_dm) {
1104 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
1105 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
1106 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1107 } else {
1108 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
1109 writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
1110 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1111 }
1112 } else {
1113 /*
1114 * CNTR 2 - In this case we want to execute the next instruction
1115 * and NOT take the jump. So we set the counter to 0. The jump
1116 * address doesn't count.
1117 */
1118 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1119 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1120
1121 /*
1122 * CNTR 3 - Set the nop counter to the number of cycles we
1123 * need to loop for, minus 1.
1124 */
1125 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1126 if (test_dm) {
1127 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
1128 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
1129 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1130 } else {
1131 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
1132 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
1133 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1134 }
1135 }
1136
1137 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1138 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1139
1140 if (quick_write_mode)
1141 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1142 else
1143 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1144
1145 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1146
1147 /*
1148 * CNTR 1 - This is used to ensure enough time elapses
1149 * for read data to come back.
1150 */
1151 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1152
1153 if (test_dm) {
1154 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
1155 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1156 } else {
1157 writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
1158 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1159 }
1160
Marek Vasut8371c2e2015-07-21 06:00:36 +02001161 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1162 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1163 (group << 2));
Marek Vasutad64769c2015-07-21 05:43:37 +02001164}
1165
Marek Vasut4a82854b2015-07-21 05:57:11 +02001166/**
1167 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple pass
1168 * @rank_bgn: Rank number
1169 * @write_group: Write Group
1170 * @use_dm: Use DM
1171 * @all_correct: All bits must be correct in the mask
1172 * @bit_chk: Resulting bit mask after the test
1173 * @all_ranks: Test all ranks
1174 *
1175 * Test writes, can check for a single bit pass or multiple bit pass.
1176 */
Marek Vasutb9452ea2015-07-21 05:54:39 +02001177static int
1178rw_mgr_mem_calibrate_write_test(const u32 rank_bgn, const u32 write_group,
1179 const u32 use_dm, const u32 all_correct,
1180 u32 *bit_chk, const u32 all_ranks)
Marek Vasutad64769c2015-07-21 05:43:37 +02001181{
Marek Vasutb9452ea2015-07-21 05:54:39 +02001182 const u32 rank_end = all_ranks ?
1183 RW_MGR_MEM_NUMBER_OF_RANKS :
1184 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1185 const u32 shift_ratio = RW_MGR_MEM_DQ_PER_WRITE_DQS /
1186 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS;
1187 const u32 correct_mask_vg = param->write_correct_mask_vg;
1188
1189 u32 tmp_bit_chk, base_rw_mgr;
1190 int vg, r;
Marek Vasutad64769c2015-07-21 05:43:37 +02001191
1192 *bit_chk = param->write_correct_mask;
Marek Vasutad64769c2015-07-21 05:43:37 +02001193
1194 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001195 /* Request to skip the rank */
1196 if (param->skip_ranks[r])
Marek Vasutad64769c2015-07-21 05:43:37 +02001197 continue;
Marek Vasutad64769c2015-07-21 05:43:37 +02001198
Marek Vasutb9452ea2015-07-21 05:54:39 +02001199 /* Set rank */
Marek Vasutad64769c2015-07-21 05:43:37 +02001200 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1201
1202 tmp_bit_chk = 0;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001203 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS - 1;
1204 vg >= 0; vg--) {
1205 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutad64769c2015-07-21 05:43:37 +02001206 writel(0, &phy_mgr_cmd->fifo_reset);
1207
Marek Vasutb9452ea2015-07-21 05:54:39 +02001208 rw_mgr_mem_calibrate_write_test_issue(
1209 write_group *
1210 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS + vg,
Marek Vasutad64769c2015-07-21 05:43:37 +02001211 use_dm);
1212
Marek Vasutb9452ea2015-07-21 05:54:39 +02001213 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1214 tmp_bit_chk <<= shift_ratio;
1215 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasutad64769c2015-07-21 05:43:37 +02001216 }
Marek Vasutb9452ea2015-07-21 05:54:39 +02001217
Marek Vasutad64769c2015-07-21 05:43:37 +02001218 *bit_chk &= tmp_bit_chk;
1219 }
1220
Marek Vasutb9452ea2015-07-21 05:54:39 +02001221 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutad64769c2015-07-21 05:43:37 +02001222 if (all_correct) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001223 debug_cond(DLEVEL == 2,
1224 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1225 write_group, use_dm, *bit_chk,
1226 param->write_correct_mask,
1227 *bit_chk == param->write_correct_mask);
Marek Vasutad64769c2015-07-21 05:43:37 +02001228 return *bit_chk == param->write_correct_mask;
1229 } else {
1230 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutb9452ea2015-07-21 05:54:39 +02001231 debug_cond(DLEVEL == 2,
1232 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1233 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasutad64769c2015-07-21 05:43:37 +02001234 return *bit_chk != 0x00;
1235 }
1236}
1237
Marek Vasutd844c7d2015-07-18 03:55:07 +02001238/**
1239 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1240 * @rank_bgn: Rank number
1241 * @group: Read/Write Group
1242 * @all_ranks: Test all ranks
1243 *
1244 * Performs a guaranteed read on the patterns we are going to use during a
1245 * read test to ensure memory works.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001246 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02001247static int
1248rw_mgr_mem_calibrate_read_test_patterns(const u32 rank_bgn, const u32 group,
1249 const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001250{
Marek Vasutd844c7d2015-07-18 03:55:07 +02001251 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1252 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1253 const u32 addr_offset =
1254 (group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS) << 2;
1255 const u32 rank_end = all_ranks ?
1256 RW_MGR_MEM_NUMBER_OF_RANKS :
1257 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1258 const u32 shift_ratio = RW_MGR_MEM_DQ_PER_READ_DQS /
1259 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS;
1260 const u32 correct_mask_vg = param->read_correct_mask_vg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001261
Marek Vasutd844c7d2015-07-18 03:55:07 +02001262 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1263 int vg, r;
1264 int ret = 0;
1265
1266 bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001267
1268 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutd844c7d2015-07-18 03:55:07 +02001269 /* Request to skip the rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001270 if (param->skip_ranks[r])
Dinh Nguyen3da42852015-06-02 22:52:49 -05001271 continue;
1272
Marek Vasutd844c7d2015-07-18 03:55:07 +02001273 /* Set rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001274 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1275
1276 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001277 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1278 writel(RW_MGR_GUARANTEED_READ,
1279 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001280
Marek Vasut1273dd92015-07-12 21:05:08 +02001281 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1282 writel(RW_MGR_GUARANTEED_READ_CONT,
1283 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001284
1285 tmp_bit_chk = 0;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001286 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1;
1287 vg >= 0; vg--) {
1288 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001289 writel(0, &phy_mgr_cmd->fifo_reset);
1290 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1291 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001292 writel(RW_MGR_GUARANTEED_READ,
1293 addr + addr_offset + (vg << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001294
Marek Vasut1273dd92015-07-12 21:05:08 +02001295 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001296 tmp_bit_chk <<= shift_ratio;
1297 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001298 }
Marek Vasutd844c7d2015-07-18 03:55:07 +02001299
1300 bit_chk &= tmp_bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001301 }
1302
Marek Vasut17fdc912015-07-12 20:05:54 +02001303 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001304
1305 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001306
1307 if (bit_chk != param->read_correct_mask)
1308 ret = -EIO;
1309
1310 debug_cond(DLEVEL == 1,
1311 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1312 __func__, __LINE__, group, bit_chk,
1313 param->read_correct_mask, ret);
1314
1315 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001316}
1317
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001318/**
1319 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read test
1320 * @rank_bgn: Rank number
1321 * @all_ranks: Test all ranks
1322 *
1323 * Load up the patterns we are going to use during a read test.
1324 */
1325static void rw_mgr_mem_calibrate_read_load_patterns(const u32 rank_bgn,
1326 const int all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001327{
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001328 const u32 rank_end = all_ranks ?
1329 RW_MGR_MEM_NUMBER_OF_RANKS :
1330 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1331 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001332
1333 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001334
Dinh Nguyen3da42852015-06-02 22:52:49 -05001335 for (r = rank_bgn; r < rank_end; r++) {
1336 if (param->skip_ranks[r])
1337 /* request to skip the rank */
1338 continue;
1339
1340 /* set rank */
1341 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1342
1343 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001344 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001345
Marek Vasut1273dd92015-07-12 21:05:08 +02001346 writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1347 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001348
Marek Vasut1273dd92015-07-12 21:05:08 +02001349 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001350
Marek Vasut1273dd92015-07-12 21:05:08 +02001351 writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1352 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001353
Marek Vasut1273dd92015-07-12 21:05:08 +02001354 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001355
Marek Vasut1273dd92015-07-12 21:05:08 +02001356 writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1357 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001358
Marek Vasut1273dd92015-07-12 21:05:08 +02001359 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001360
Marek Vasut1273dd92015-07-12 21:05:08 +02001361 writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1362 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001363
Marek Vasut1273dd92015-07-12 21:05:08 +02001364 writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1365 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001366 }
1367
1368 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1369}
1370
Marek Vasut783fcf52015-07-20 03:26:05 +02001371/**
1372 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1373 * @rank_bgn: Rank number
1374 * @group: Read/Write group
1375 * @num_tries: Number of retries of the test
1376 * @all_correct: All bits must be correct in the mask
1377 * @bit_chk: Resulting bit mask after the test
1378 * @all_groups: Test all R/W groups
1379 * @all_ranks: Test all ranks
1380 *
1381 * Try a read and see if it returns correct data back. Test has dummy reads
1382 * inserted into the mix used to align DQS enable. Test has more thorough
1383 * checks than the regular read test.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001384 */
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001385static int
1386rw_mgr_mem_calibrate_read_test(const u32 rank_bgn, const u32 group,
1387 const u32 num_tries, const u32 all_correct,
1388 u32 *bit_chk,
1389 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001390{
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001391 const u32 rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
Dinh Nguyen3da42852015-06-02 22:52:49 -05001392 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001393 const u32 quick_read_mode =
1394 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
1395 ENABLE_SUPER_QUICK_CALIBRATION);
1396 u32 correct_mask_vg = param->read_correct_mask_vg;
1397 u32 tmp_bit_chk;
1398 u32 base_rw_mgr;
1399 u32 addr;
1400
1401 int r, vg, ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001402
1403 *bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001404
1405 for (r = rank_bgn; r < rank_end; r++) {
1406 if (param->skip_ranks[r])
1407 /* request to skip the rank */
1408 continue;
1409
1410 /* set rank */
1411 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1412
Marek Vasut1273dd92015-07-12 21:05:08 +02001413 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001414
Marek Vasut1273dd92015-07-12 21:05:08 +02001415 writel(RW_MGR_READ_B2B_WAIT1,
1416 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001417
Marek Vasut1273dd92015-07-12 21:05:08 +02001418 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1419 writel(RW_MGR_READ_B2B_WAIT2,
1420 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001421
Dinh Nguyen3da42852015-06-02 22:52:49 -05001422 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001423 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001424 /* need at least two (1+1) reads to capture failures */
1425 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001426 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001427 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001428 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001429
Marek Vasut1273dd92015-07-12 21:05:08 +02001430 writel(RW_MGR_READ_B2B,
1431 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001432 if (all_groups)
1433 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1434 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001435 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001436 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001437 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001438
Marek Vasut1273dd92015-07-12 21:05:08 +02001439 writel(RW_MGR_READ_B2B,
1440 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001441
1442 tmp_bit_chk = 0;
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001443 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1; vg >= 0;
1444 vg--) {
Marek Vasutba522c72015-07-19 07:57:28 +02001445 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001446 writel(0, &phy_mgr_cmd->fifo_reset);
1447 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1448 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001449
Marek Vasutba522c72015-07-19 07:57:28 +02001450 if (all_groups) {
1451 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1452 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1453 } else {
1454 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1455 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1456 }
Marek Vasutc4815f72015-07-12 19:03:33 +02001457
Marek Vasut17fdc912015-07-12 20:05:54 +02001458 writel(RW_MGR_READ_B2B, addr +
Dinh Nguyen3da42852015-06-02 22:52:49 -05001459 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1460 vg) << 2));
1461
Marek Vasut1273dd92015-07-12 21:05:08 +02001462 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutba522c72015-07-19 07:57:28 +02001463 tmp_bit_chk <<= RW_MGR_MEM_DQ_PER_READ_DQS /
1464 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS;
1465 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001466 }
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001467
Dinh Nguyen3da42852015-06-02 22:52:49 -05001468 *bit_chk &= tmp_bit_chk;
1469 }
1470
Marek Vasutc4815f72015-07-12 19:03:33 +02001471 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut17fdc912015-07-12 20:05:54 +02001472 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001473
Marek Vasut3853d652015-07-19 07:44:21 +02001474 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1475
Dinh Nguyen3da42852015-06-02 22:52:49 -05001476 if (all_correct) {
Marek Vasut3853d652015-07-19 07:44:21 +02001477 ret = (*bit_chk == param->read_correct_mask);
1478 debug_cond(DLEVEL == 2,
1479 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1480 __func__, __LINE__, group, all_groups, *bit_chk,
1481 param->read_correct_mask, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001482 } else {
Marek Vasut3853d652015-07-19 07:44:21 +02001483 ret = (*bit_chk != 0x00);
1484 debug_cond(DLEVEL == 2,
1485 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1486 __func__, __LINE__, group, all_groups, *bit_chk,
1487 0, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001488 }
Marek Vasut3853d652015-07-19 07:44:21 +02001489
1490 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001491}
1492
Marek Vasut96df6032015-07-19 07:35:36 +02001493/**
1494 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1495 * @grp: Read/Write group
1496 * @num_tries: Number of retries of the test
1497 * @all_correct: All bits must be correct in the mask
1498 * @all_groups: Test all R/W groups
1499 *
1500 * Perform a READ test across all memory ranks.
1501 */
1502static int
1503rw_mgr_mem_calibrate_read_test_all_ranks(const u32 grp, const u32 num_tries,
1504 const u32 all_correct,
1505 const u32 all_groups)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001506{
Marek Vasut96df6032015-07-19 07:35:36 +02001507 u32 bit_chk;
1508 return rw_mgr_mem_calibrate_read_test(0, grp, num_tries, all_correct,
1509 &bit_chk, all_groups, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001510}
1511
Marek Vasut60bb8a82015-07-19 06:25:27 +02001512/**
1513 * rw_mgr_incr_vfifo() - Increase VFIFO value
1514 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001515 *
1516 * Increase VFIFO value.
1517 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001518static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001519{
Marek Vasut1273dd92015-07-12 21:05:08 +02001520 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001521}
1522
Marek Vasut60bb8a82015-07-19 06:25:27 +02001523/**
1524 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1525 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001526 *
1527 * Decrease VFIFO value.
1528 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001529static void rw_mgr_decr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001530{
Marek Vasut60bb8a82015-07-19 06:25:27 +02001531 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001532
Marek Vasut60bb8a82015-07-19 06:25:27 +02001533 for (i = 0; i < VFIFO_SIZE - 1; i++)
Marek Vasut8c887b62015-07-19 06:37:51 +02001534 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001535}
1536
Marek Vasutd145ca92015-07-19 06:45:43 +02001537/**
1538 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1539 * @grp: Read/Write group
1540 *
1541 * Push VFIFO until a failing read happens.
1542 */
1543static int find_vfifo_failing_read(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001544{
Marek Vasut96df6032015-07-19 07:35:36 +02001545 u32 v, ret, fail_cnt = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001546
Marek Vasut8c887b62015-07-19 06:37:51 +02001547 for (v = 0; v < VFIFO_SIZE; v++) {
Marek Vasutd145ca92015-07-19 06:45:43 +02001548 debug_cond(DLEVEL == 2, "%s:%d: vfifo %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001549 __func__, __LINE__, v);
Marek Vasutd145ca92015-07-19 06:45:43 +02001550 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001551 PASS_ONE_BIT, 0);
Marek Vasutd145ca92015-07-19 06:45:43 +02001552 if (!ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001553 fail_cnt++;
1554
1555 if (fail_cnt == 2)
Marek Vasutd145ca92015-07-19 06:45:43 +02001556 return v;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001557 }
1558
Marek Vasutd145ca92015-07-19 06:45:43 +02001559 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001560 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001561 }
1562
Marek Vasutd145ca92015-07-19 06:45:43 +02001563 /* No failing read found! Something must have gone wrong. */
1564 debug_cond(DLEVEL == 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
1565 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001566}
1567
Marek Vasut192d6f92015-07-19 05:26:49 +02001568/**
Marek Vasut52e8f212015-07-19 07:27:06 +02001569 * sdr_find_phase_delay() - Find DQS enable phase or delay
1570 * @working: If 1, look for working phase/delay, if 0, look for non-working
1571 * @delay: If 1, look for delay, if 0, look for phase
1572 * @grp: Read/Write group
1573 * @work: Working window position
1574 * @work_inc: Working window increment
1575 * @pd: DQS Phase/Delay Iterator
1576 *
1577 * Find working or non-working DQS enable phase setting.
1578 */
1579static int sdr_find_phase_delay(int working, int delay, const u32 grp,
1580 u32 *work, const u32 work_inc, u32 *pd)
1581{
1582 const u32 max = delay ? IO_DQS_EN_DELAY_MAX : IO_DQS_EN_PHASE_MAX;
Marek Vasut96df6032015-07-19 07:35:36 +02001583 u32 ret;
Marek Vasut52e8f212015-07-19 07:27:06 +02001584
1585 for (; *pd <= max; (*pd)++) {
1586 if (delay)
1587 scc_mgr_set_dqs_en_delay_all_ranks(grp, *pd);
1588 else
1589 scc_mgr_set_dqs_en_phase_all_ranks(grp, *pd);
1590
1591 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001592 PASS_ONE_BIT, 0);
Marek Vasut52e8f212015-07-19 07:27:06 +02001593 if (!working)
1594 ret = !ret;
1595
1596 if (ret)
1597 return 0;
1598
1599 if (work)
1600 *work += work_inc;
1601 }
1602
1603 return -EINVAL;
1604}
1605/**
Marek Vasut192d6f92015-07-19 05:26:49 +02001606 * sdr_find_phase() - Find DQS enable phase
1607 * @working: If 1, look for working phase, if 0, look for non-working phase
1608 * @grp: Read/Write group
Marek Vasut192d6f92015-07-19 05:26:49 +02001609 * @work: Working window position
1610 * @i: Iterator
1611 * @p: DQS Phase Iterator
Marek Vasut192d6f92015-07-19 05:26:49 +02001612 *
1613 * Find working or non-working DQS enable phase setting.
1614 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001615static int sdr_find_phase(int working, const u32 grp, u32 *work,
Marek Vasut86a39dc2015-07-19 05:35:40 +02001616 u32 *i, u32 *p)
Marek Vasut192d6f92015-07-19 05:26:49 +02001617{
Marek Vasut192d6f92015-07-19 05:26:49 +02001618 const u32 end = VFIFO_SIZE + (working ? 0 : 1);
Marek Vasut52e8f212015-07-19 07:27:06 +02001619 int ret;
Marek Vasut192d6f92015-07-19 05:26:49 +02001620
1621 for (; *i < end; (*i)++) {
1622 if (working)
1623 *p = 0;
1624
Marek Vasut52e8f212015-07-19 07:27:06 +02001625 ret = sdr_find_phase_delay(working, 0, grp, work,
1626 IO_DELAY_PER_OPA_TAP, p);
1627 if (!ret)
1628 return 0;
Marek Vasut192d6f92015-07-19 05:26:49 +02001629
1630 if (*p > IO_DQS_EN_PHASE_MAX) {
1631 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001632 rw_mgr_incr_vfifo(grp);
Marek Vasut192d6f92015-07-19 05:26:49 +02001633 if (!working)
1634 *p = 0;
1635 }
1636 }
1637
1638 return -EINVAL;
1639}
1640
Marek Vasut4c5e5842015-07-19 06:04:00 +02001641/**
1642 * sdr_working_phase() - Find working DQS enable phase
1643 * @grp: Read/Write group
1644 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001645 * @d: dtaps output value
1646 * @p: DQS Phase Iterator
1647 * @i: Iterator
1648 *
1649 * Find working DQS enable phase setting.
1650 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001651static int sdr_working_phase(const u32 grp, u32 *work_bgn, u32 *d,
Marek Vasut4c5e5842015-07-19 06:04:00 +02001652 u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001653{
Marek Vasut35ee8672015-07-19 05:40:06 +02001654 const u32 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP /
1655 IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Marek Vasut192d6f92015-07-19 05:26:49 +02001656 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001657
Marek Vasut192d6f92015-07-19 05:26:49 +02001658 *work_bgn = 0;
1659
1660 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1661 *i = 0;
Marek Vasut521fe392015-07-19 04:34:12 +02001662 scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
Marek Vasut8c887b62015-07-19 06:37:51 +02001663 ret = sdr_find_phase(1, grp, work_bgn, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001664 if (!ret)
1665 return 0;
1666 *work_bgn += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001667 }
1668
Marek Vasut38ed6922015-07-19 05:01:12 +02001669 /* Cannot find working solution */
Marek Vasut192d6f92015-07-19 05:26:49 +02001670 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
1671 __func__, __LINE__);
1672 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001673}
1674
Marek Vasut4c5e5842015-07-19 06:04:00 +02001675/**
1676 * sdr_backup_phase() - Find DQS enable backup phase
1677 * @grp: Read/Write group
1678 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001679 * @p: DQS Phase Iterator
1680 *
1681 * Find DQS enable backup phase setting.
1682 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001683static void sdr_backup_phase(const u32 grp, u32 *work_bgn, u32 *p)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001684{
Marek Vasut96df6032015-07-19 07:35:36 +02001685 u32 tmp_delay, d;
Marek Vasut4c5e5842015-07-19 06:04:00 +02001686 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001687
1688 /* Special case code for backing up a phase */
1689 if (*p == 0) {
1690 *p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001691 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001692 } else {
1693 (*p)--;
1694 }
1695 tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
Marek Vasut521fe392015-07-19 04:34:12 +02001696 scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001697
Marek Vasut49891df62015-07-19 05:48:30 +02001698 for (d = 0; d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn; d++) {
1699 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001700
Marek Vasut4c5e5842015-07-19 06:04:00 +02001701 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001702 PASS_ONE_BIT, 0);
Marek Vasut4c5e5842015-07-19 06:04:00 +02001703 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001704 *work_bgn = tmp_delay;
1705 break;
1706 }
Marek Vasut49891df62015-07-19 05:48:30 +02001707
1708 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001709 }
1710
Marek Vasut4c5e5842015-07-19 06:04:00 +02001711 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001712 (*p)++;
1713 if (*p > IO_DQS_EN_PHASE_MAX) {
1714 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001715 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001716 }
1717
Marek Vasut521fe392015-07-19 04:34:12 +02001718 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001719}
1720
Marek Vasut4c5e5842015-07-19 06:04:00 +02001721/**
1722 * sdr_nonworking_phase() - Find non-working DQS enable phase
1723 * @grp: Read/Write group
1724 * @work_end: Working window end position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001725 * @p: DQS Phase Iterator
1726 * @i: Iterator
1727 *
1728 * Find non-working DQS enable phase setting.
1729 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001730static int sdr_nonworking_phase(const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001731{
Marek Vasut192d6f92015-07-19 05:26:49 +02001732 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001733
1734 (*p)++;
1735 *work_end += IO_DELAY_PER_OPA_TAP;
1736 if (*p > IO_DQS_EN_PHASE_MAX) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001737 /* Fiddle with FIFO. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001738 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001739 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001740 }
1741
Marek Vasut8c887b62015-07-19 06:37:51 +02001742 ret = sdr_find_phase(0, grp, work_end, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001743 if (ret) {
1744 /* Cannot see edge of failing read. */
1745 debug_cond(DLEVEL == 2, "%s:%d: end: failed\n",
1746 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001747 }
1748
Marek Vasut192d6f92015-07-19 05:26:49 +02001749 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001750}
1751
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001752/**
1753 * sdr_find_window_center() - Find center of the working DQS window.
1754 * @grp: Read/Write group
1755 * @work_bgn: First working settings
1756 * @work_end: Last working settings
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001757 *
1758 * Find center of the working DQS enable window.
1759 */
1760static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
Marek Vasut8c887b62015-07-19 06:37:51 +02001761 const u32 work_end)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001762{
Marek Vasut96df6032015-07-19 07:35:36 +02001763 u32 work_mid;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001764 int tmp_delay = 0;
Marek Vasut28fd2422015-07-19 02:56:59 +02001765 int i, p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001766
Marek Vasut28fd2422015-07-19 02:56:59 +02001767 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001768
1769 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasut28fd2422015-07-19 02:56:59 +02001770 work_bgn, work_end, work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001771 /* Get the middle delay to be less than a VFIFO delay */
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001772 tmp_delay = (IO_DQS_EN_PHASE_MAX + 1) * IO_DELAY_PER_OPA_TAP;
Marek Vasut28fd2422015-07-19 02:56:59 +02001773
Dinh Nguyen3da42852015-06-02 22:52:49 -05001774 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001775 work_mid %= tmp_delay;
Marek Vasut28fd2422015-07-19 02:56:59 +02001776 debug_cond(DLEVEL == 2, "new work_mid %d\n", work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001777
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001778 tmp_delay = rounddown(work_mid, IO_DELAY_PER_OPA_TAP);
1779 if (tmp_delay > IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP)
1780 tmp_delay = IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP;
1781 p = tmp_delay / IO_DELAY_PER_OPA_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001782
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001783 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
1784
1785 d = DIV_ROUND_UP(work_mid - tmp_delay, IO_DELAY_PER_DQS_EN_DCHAIN_TAP);
1786 if (d > IO_DQS_EN_DELAY_MAX)
1787 d = IO_DQS_EN_DELAY_MAX;
1788 tmp_delay += d * IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1789
Marek Vasut28fd2422015-07-19 02:56:59 +02001790 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
1791
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001792 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
Marek Vasut28fd2422015-07-19 02:56:59 +02001793 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001794
1795 /*
1796 * push vfifo until we can successfully calibrate. We can do this
1797 * because the largest possible margin in 1 VFIFO cycle.
1798 */
1799 for (i = 0; i < VFIFO_SIZE; i++) {
Marek Vasut8c887b62015-07-19 06:37:51 +02001800 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center\n");
Marek Vasut28fd2422015-07-19 02:56:59 +02001801 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05001802 PASS_ONE_BIT,
Marek Vasut96df6032015-07-19 07:35:36 +02001803 0)) {
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001804 debug_cond(DLEVEL == 2,
Marek Vasut8c887b62015-07-19 06:37:51 +02001805 "%s:%d center: found: ptap=%u dtap=%u\n",
1806 __func__, __LINE__, p, d);
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001807 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001808 }
1809
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001810 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001811 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001812 }
1813
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001814 debug_cond(DLEVEL == 2, "%s:%d center: failed.\n",
1815 __func__, __LINE__);
1816 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001817}
1818
Marek Vasut33756892015-07-20 09:11:09 +02001819/**
1820 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to use
1821 * @grp: Read/Write Group
1822 *
1823 * Find a good DQS enable to use.
1824 */
Marek Vasut914546e2015-07-20 09:20:42 +02001825static int rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001826{
Marek Vasut57355402015-07-20 09:20:20 +02001827 u32 d, p, i;
1828 u32 dtaps_per_ptap;
1829 u32 work_bgn, work_end;
1830 u32 found_passing_read, found_failing_read, initial_failing_dtap;
1831 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001832
1833 debug("%s:%d %u\n", __func__, __LINE__, grp);
1834
1835 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1836
1837 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1838 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1839
Marek Vasut2f3589c2015-07-19 02:42:21 +02001840 /* Step 0: Determine number of delay taps for each phase tap. */
1841 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP / IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001842
Marek Vasut2f3589c2015-07-19 02:42:21 +02001843 /* Step 1: First push vfifo until we get a failing read. */
Marek Vasutd145ca92015-07-19 06:45:43 +02001844 find_vfifo_failing_read(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001845
Marek Vasut2f3589c2015-07-19 02:42:21 +02001846 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001847 work_bgn = 0;
Marek Vasut914546e2015-07-20 09:20:42 +02001848 ret = sdr_working_phase(grp, &work_bgn, &d, &p, &i);
1849 if (ret)
1850 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001851
1852 work_end = work_bgn;
1853
1854 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001855 * If d is 0 then the working window covers a phase tap and we can
1856 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen3da42852015-06-02 22:52:49 -05001857 * and we need to increment the dtaps until we find the end.
1858 */
1859 if (d == 0) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001860 /*
1861 * Step 3a: If we have room, back off by one and
1862 * increment in dtaps.
1863 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001864 sdr_backup_phase(grp, &work_bgn, &p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001865
Marek Vasut2f3589c2015-07-19 02:42:21 +02001866 /*
1867 * Step 4a: go forward from working phase to non working
1868 * phase, increment in ptaps.
1869 */
Marek Vasut914546e2015-07-20 09:20:42 +02001870 ret = sdr_nonworking_phase(grp, &work_end, &p, &i);
1871 if (ret)
1872 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001873
Marek Vasut2f3589c2015-07-19 02:42:21 +02001874 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001875
1876 /* Special case code for backing up a phase */
1877 if (p == 0) {
1878 p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001879 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001880 } else {
1881 p = p - 1;
1882 }
1883
1884 work_end -= IO_DELAY_PER_OPA_TAP;
1885 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1886
Dinh Nguyen3da42852015-06-02 22:52:49 -05001887 d = 0;
1888
Marek Vasut2f3589c2015-07-19 02:42:21 +02001889 debug_cond(DLEVEL == 2, "%s:%d p: ptap=%u\n",
1890 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001891 }
1892
Marek Vasut2f3589c2015-07-19 02:42:21 +02001893 /* The dtap increment to find the failing edge is done here. */
Marek Vasut52e8f212015-07-19 07:27:06 +02001894 sdr_find_phase_delay(0, 1, grp, &work_end,
1895 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001896
1897 /* Go back to working dtap */
1898 if (d != 0)
1899 work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1900
Marek Vasut2f3589c2015-07-19 02:42:21 +02001901 debug_cond(DLEVEL == 2,
1902 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1903 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001904
1905 if (work_end < work_bgn) {
1906 /* nil range */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001907 debug_cond(DLEVEL == 2, "%s:%d end-2: failed\n",
1908 __func__, __LINE__);
Marek Vasut914546e2015-07-20 09:20:42 +02001909 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001910 }
1911
Marek Vasut2f3589c2015-07-19 02:42:21 +02001912 debug_cond(DLEVEL == 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001913 __func__, __LINE__, work_bgn, work_end);
1914
Dinh Nguyen3da42852015-06-02 22:52:49 -05001915 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001916 * We need to calculate the number of dtaps that equal a ptap.
1917 * To do that we'll back up a ptap and re-find the edge of the
1918 * window using dtaps
Dinh Nguyen3da42852015-06-02 22:52:49 -05001919 */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001920 debug_cond(DLEVEL == 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
1921 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001922
1923 /* Special case code for backing up a phase */
1924 if (p == 0) {
1925 p = IO_DQS_EN_PHASE_MAX;
Marek Vasut8c887b62015-07-19 06:37:51 +02001926 rw_mgr_decr_vfifo(grp);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001927 debug_cond(DLEVEL == 2, "%s:%d backedup cycle/phase: p=%u\n",
1928 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001929 } else {
1930 p = p - 1;
Marek Vasut2f3589c2015-07-19 02:42:21 +02001931 debug_cond(DLEVEL == 2, "%s:%d backedup phase only: p=%u",
1932 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001933 }
1934
1935 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1936
1937 /*
1938 * Increase dtap until we first see a passing read (in case the
Marek Vasut2f3589c2015-07-19 02:42:21 +02001939 * window is smaller than a ptap), and then a failing read to
1940 * mark the edge of the window again.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001941 */
1942
Marek Vasut2f3589c2015-07-19 02:42:21 +02001943 /* Find a passing read. */
1944 debug_cond(DLEVEL == 2, "%s:%d find passing read\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001945 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001946
Dinh Nguyen3da42852015-06-02 22:52:49 -05001947 initial_failing_dtap = d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001948
Marek Vasut52e8f212015-07-19 07:27:06 +02001949 found_passing_read = !sdr_find_phase_delay(1, 1, grp, NULL, 0, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001950 if (found_passing_read) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001951 /* Find a failing read. */
1952 debug_cond(DLEVEL == 2, "%s:%d find failing read\n",
1953 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001954 d++;
1955 found_failing_read = !sdr_find_phase_delay(0, 1, grp, NULL, 0,
1956 &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001957 } else {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001958 debug_cond(DLEVEL == 1,
1959 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1960 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001961 }
1962
1963 /*
1964 * The dynamically calculated dtaps_per_ptap is only valid if we
1965 * found a passing/failing read. If we didn't, it means d hit the max
1966 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1967 * statically calculated value.
1968 */
1969 if (found_passing_read && found_failing_read)
1970 dtaps_per_ptap = d - initial_failing_dtap;
1971
Marek Vasut1273dd92015-07-12 21:05:08 +02001972 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001973 debug_cond(DLEVEL == 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
1974 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001975
Marek Vasut2f3589c2015-07-19 02:42:21 +02001976 /* Step 6: Find the centre of the window. */
Marek Vasut914546e2015-07-20 09:20:42 +02001977 ret = sdr_find_window_center(grp, work_bgn, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001978
Marek Vasut914546e2015-07-20 09:20:42 +02001979 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001980}
1981
Marek Vasutc4907892015-07-13 02:11:02 +02001982/**
Marek Vasut901dc362015-07-13 02:48:34 +02001983 * search_stop_check() - Check if the detected edge is valid
1984 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1985 * @d: DQS delay
1986 * @rank_bgn: Rank number
1987 * @write_group: Write Group
1988 * @read_group: Read Group
1989 * @bit_chk: Resulting bit mask after the test
1990 * @sticky_bit_chk: Resulting sticky bit mask after the test
1991 * @use_read_test: Perform read test
1992 *
1993 * Test if the found edge is valid.
1994 */
1995static u32 search_stop_check(const int write, const int d, const int rank_bgn,
1996 const u32 write_group, const u32 read_group,
1997 u32 *bit_chk, u32 *sticky_bit_chk,
1998 const u32 use_read_test)
1999{
2000 const u32 ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
2001 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
2002 const u32 correct_mask = write ? param->write_correct_mask :
2003 param->read_correct_mask;
2004 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2005 RW_MGR_MEM_DQ_PER_READ_DQS;
2006 u32 ret;
2007 /*
2008 * Stop searching when the read test doesn't pass AND when
2009 * we've seen a passing read on every bit.
2010 */
2011 if (write) { /* WRITE-ONLY */
2012 ret = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2013 0, PASS_ONE_BIT,
2014 bit_chk, 0);
2015 } else if (use_read_test) { /* READ-ONLY */
2016 ret = !rw_mgr_mem_calibrate_read_test(rank_bgn, read_group,
2017 NUM_READ_PB_TESTS,
2018 PASS_ONE_BIT, bit_chk,
2019 0, 0);
2020 } else { /* READ-ONLY */
2021 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 0,
2022 PASS_ONE_BIT, bit_chk, 0);
2023 *bit_chk = *bit_chk >> (per_dqs *
2024 (read_group - (write_group * ratio)));
2025 ret = (*bit_chk == 0);
2026 }
2027 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2028 ret = ret && (*sticky_bit_chk == correct_mask);
2029 debug_cond(DLEVEL == 2,
2030 "%s:%d center(left): dtap=%u => %u == %u && %u",
2031 __func__, __LINE__, d,
2032 *sticky_bit_chk, correct_mask, ret);
2033 return ret;
2034}
2035
2036/**
Marek Vasut71120772015-07-13 02:38:15 +02002037 * search_left_edge() - Find left edge of DQ/DQS working phase
2038 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2039 * @rank_bgn: Rank number
2040 * @write_group: Write Group
2041 * @read_group: Read Group
2042 * @test_bgn: Rank number to begin the test
Marek Vasut71120772015-07-13 02:38:15 +02002043 * @sticky_bit_chk: Resulting sticky bit mask after the test
2044 * @left_edge: Left edge of the DQ/DQS phase
2045 * @right_edge: Right edge of the DQ/DQS phase
2046 * @use_read_test: Perform read test
2047 *
2048 * Find left edge of DQ/DQS working phase.
2049 */
2050static void search_left_edge(const int write, const int rank_bgn,
2051 const u32 write_group, const u32 read_group, const u32 test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002052 u32 *sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002053 int *left_edge, int *right_edge, const u32 use_read_test)
2054{
Marek Vasut71120772015-07-13 02:38:15 +02002055 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2056 const u32 dqs_max = write ? IO_IO_OUT1_DELAY_MAX : IO_DQS_IN_DELAY_MAX;
2057 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2058 RW_MGR_MEM_DQ_PER_READ_DQS;
Marek Vasut0c4be192015-07-18 20:34:00 +02002059 u32 stop, bit_chk;
Marek Vasut71120772015-07-13 02:38:15 +02002060 int i, d;
2061
2062 for (d = 0; d <= dqs_max; d++) {
2063 if (write)
2064 scc_mgr_apply_group_dq_out1_delay(d);
2065 else
2066 scc_mgr_apply_group_dq_in_delay(test_bgn, d);
2067
2068 writel(0, &sdr_scc_mgr->update);
2069
Marek Vasut901dc362015-07-13 02:48:34 +02002070 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002071 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002072 use_read_test);
Marek Vasut71120772015-07-13 02:38:15 +02002073 if (stop == 1)
2074 break;
2075
2076 /* stop != 1 */
2077 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002078 if (bit_chk & 1) {
Marek Vasut71120772015-07-13 02:38:15 +02002079 /*
2080 * Remember a passing test as
2081 * the left_edge.
2082 */
2083 left_edge[i] = d;
2084 } else {
2085 /*
2086 * If a left edge has not been seen
2087 * yet, then a future passing test
2088 * will mark this edge as the right
2089 * edge.
2090 */
2091 if (left_edge[i] == delay_max + 1)
2092 right_edge[i] = -(d + 1);
2093 }
Marek Vasut0c4be192015-07-18 20:34:00 +02002094 bit_chk >>= 1;
Marek Vasut71120772015-07-13 02:38:15 +02002095 }
2096 }
2097
2098 /* Reset DQ delay chains to 0 */
2099 if (write)
2100 scc_mgr_apply_group_dq_out1_delay(0);
2101 else
2102 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2103
2104 *sticky_bit_chk = 0;
2105 for (i = per_dqs - 1; i >= 0; i--) {
2106 debug_cond(DLEVEL == 2,
2107 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2108 __func__, __LINE__, i, left_edge[i],
2109 i, right_edge[i]);
2110
2111 /*
2112 * Check for cases where we haven't found the left edge,
2113 * which makes our assignment of the the right edge invalid.
2114 * Reset it to the illegal value.
2115 */
2116 if ((left_edge[i] == delay_max + 1) &&
2117 (right_edge[i] != delay_max + 1)) {
2118 right_edge[i] = delay_max + 1;
2119 debug_cond(DLEVEL == 2,
2120 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2121 __func__, __LINE__, i, right_edge[i]);
2122 }
2123
2124 /*
2125 * Reset sticky bit
2126 * READ: except for bits where we have seen both
2127 * the left and right edge.
2128 * WRITE: except for bits where we have seen the
2129 * left edge.
2130 */
2131 *sticky_bit_chk <<= 1;
2132 if (write) {
2133 if (left_edge[i] != delay_max + 1)
2134 *sticky_bit_chk |= 1;
2135 } else {
2136 if ((left_edge[i] != delay_max + 1) &&
2137 (right_edge[i] != delay_max + 1))
2138 *sticky_bit_chk |= 1;
2139 }
2140 }
2141
2142
2143}
2144
2145/**
Marek Vasutc4907892015-07-13 02:11:02 +02002146 * search_right_edge() - Find right edge of DQ/DQS working phase
2147 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2148 * @rank_bgn: Rank number
2149 * @write_group: Write Group
2150 * @read_group: Read Group
2151 * @start_dqs: DQS start phase
2152 * @start_dqs_en: DQS enable start phase
Marek Vasutc4907892015-07-13 02:11:02 +02002153 * @sticky_bit_chk: Resulting sticky bit mask after the test
2154 * @left_edge: Left edge of the DQ/DQS phase
2155 * @right_edge: Right edge of the DQ/DQS phase
2156 * @use_read_test: Perform read test
2157 *
2158 * Find right edge of DQ/DQS working phase.
2159 */
2160static int search_right_edge(const int write, const int rank_bgn,
2161 const u32 write_group, const u32 read_group,
2162 const int start_dqs, const int start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002163 u32 *sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002164 int *left_edge, int *right_edge, const u32 use_read_test)
2165{
Marek Vasutc4907892015-07-13 02:11:02 +02002166 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2167 const u32 dqs_max = write ? IO_IO_OUT1_DELAY_MAX : IO_DQS_IN_DELAY_MAX;
2168 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2169 RW_MGR_MEM_DQ_PER_READ_DQS;
Marek Vasut0c4be192015-07-18 20:34:00 +02002170 u32 stop, bit_chk;
Marek Vasutc4907892015-07-13 02:11:02 +02002171 int i, d;
2172
2173 for (d = 0; d <= dqs_max - start_dqs; d++) {
2174 if (write) { /* WRITE-ONLY */
2175 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2176 d + start_dqs);
2177 } else { /* READ-ONLY */
2178 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
2179 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2180 uint32_t delay = d + start_dqs_en;
2181 if (delay > IO_DQS_EN_DELAY_MAX)
2182 delay = IO_DQS_EN_DELAY_MAX;
2183 scc_mgr_set_dqs_en_delay(read_group, delay);
2184 }
2185 scc_mgr_load_dqs(read_group);
2186 }
2187
2188 writel(0, &sdr_scc_mgr->update);
2189
Marek Vasut901dc362015-07-13 02:48:34 +02002190 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002191 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002192 use_read_test);
Marek Vasutc4907892015-07-13 02:11:02 +02002193 if (stop == 1) {
2194 if (write && (d == 0)) { /* WRITE-ONLY */
2195 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2196 /*
2197 * d = 0 failed, but it passed when
2198 * testing the left edge, so it must be
2199 * marginal, set it to -1
2200 */
2201 if (right_edge[i] == delay_max + 1 &&
2202 left_edge[i] != delay_max + 1)
2203 right_edge[i] = -1;
2204 }
2205 }
2206 break;
2207 }
2208
2209 /* stop != 1 */
2210 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002211 if (bit_chk & 1) {
Marek Vasutc4907892015-07-13 02:11:02 +02002212 /*
2213 * Remember a passing test as
2214 * the right_edge.
2215 */
2216 right_edge[i] = d;
2217 } else {
2218 if (d != 0) {
2219 /*
2220 * If a right edge has not
2221 * been seen yet, then a future
2222 * passing test will mark this
2223 * edge as the left edge.
2224 */
2225 if (right_edge[i] == delay_max + 1)
2226 left_edge[i] = -(d + 1);
2227 } else {
2228 /*
2229 * d = 0 failed, but it passed
2230 * when testing the left edge,
2231 * so it must be marginal, set
2232 * it to -1
2233 */
2234 if (right_edge[i] == delay_max + 1 &&
2235 left_edge[i] != delay_max + 1)
2236 right_edge[i] = -1;
2237 /*
2238 * If a right edge has not been
2239 * seen yet, then a future
2240 * passing test will mark this
2241 * edge as the left edge.
2242 */
2243 else if (right_edge[i] == delay_max + 1)
2244 left_edge[i] = -(d + 1);
2245 }
2246 }
2247
2248 debug_cond(DLEVEL == 2, "%s:%d center[r,d=%u]: ",
2249 __func__, __LINE__, d);
2250 debug_cond(DLEVEL == 2,
2251 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasut0c4be192015-07-18 20:34:00 +02002252 bit_chk & 1, i, left_edge[i]);
Marek Vasutc4907892015-07-13 02:11:02 +02002253 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2254 right_edge[i]);
Marek Vasut0c4be192015-07-18 20:34:00 +02002255 bit_chk >>= 1;
Marek Vasutc4907892015-07-13 02:11:02 +02002256 }
2257 }
2258
2259 /* Check that all bits have a window */
2260 for (i = 0; i < per_dqs; i++) {
2261 debug_cond(DLEVEL == 2,
2262 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2263 __func__, __LINE__, i, left_edge[i],
2264 i, right_edge[i]);
2265 if ((left_edge[i] == dqs_max + 1) ||
2266 (right_edge[i] == dqs_max + 1))
2267 return i + 1; /* FIXME: If we fail, retval > 0 */
2268 }
2269
2270 return 0;
2271}
2272
Marek Vasutafb3eb82015-07-18 19:18:06 +02002273/**
2274 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2275 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2276 * @left_edge: Left edge of the DQ/DQS phase
2277 * @right_edge: Right edge of the DQ/DQS phase
2278 * @mid_min: Best DQ/DQS phase middle setting
2279 *
2280 * Find index and value of the middle of the DQ/DQS working phase.
2281 */
2282static int get_window_mid_index(const int write, int *left_edge,
2283 int *right_edge, int *mid_min)
2284{
2285 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2286 RW_MGR_MEM_DQ_PER_READ_DQS;
2287 int i, mid, min_index;
2288
2289 /* Find middle of window for each DQ bit */
2290 *mid_min = left_edge[0] - right_edge[0];
2291 min_index = 0;
2292 for (i = 1; i < per_dqs; i++) {
2293 mid = left_edge[i] - right_edge[i];
2294 if (mid < *mid_min) {
2295 *mid_min = mid;
2296 min_index = i;
2297 }
2298 }
2299
2300 /*
2301 * -mid_min/2 represents the amount that we need to move DQS.
2302 * If mid_min is odd and positive we'll need to add one to make
2303 * sure the rounding in further calculations is correct (always
2304 * bias to the right), so just add 1 for all positive values.
2305 */
2306 if (*mid_min > 0)
2307 (*mid_min)++;
2308 *mid_min = *mid_min / 2;
2309
2310 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
2311 __func__, __LINE__, *mid_min, min_index);
2312 return min_index;
2313}
2314
Marek Vasutffb8b662015-07-18 19:46:26 +02002315/**
2316 * center_dq_windows() - Center the DQ/DQS windows
2317 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2318 * @left_edge: Left edge of the DQ/DQS phase
2319 * @right_edge: Right edge of the DQ/DQS phase
2320 * @mid_min: Adjusted DQ/DQS phase middle setting
2321 * @orig_mid_min: Original DQ/DQS phase middle setting
2322 * @min_index: DQ/DQS phase middle setting index
2323 * @test_bgn: Rank number to begin the test
2324 * @dq_margin: Amount of shift for the DQ
2325 * @dqs_margin: Amount of shift for the DQS
2326 *
2327 * Align the DQ/DQS windows in each group.
2328 */
2329static void center_dq_windows(const int write, int *left_edge, int *right_edge,
2330 const int mid_min, const int orig_mid_min,
2331 const int min_index, const int test_bgn,
2332 int *dq_margin, int *dqs_margin)
2333{
2334 const u32 delay_max = write ? IO_IO_OUT1_DELAY_MAX : IO_IO_IN_DELAY_MAX;
2335 const u32 per_dqs = write ? RW_MGR_MEM_DQ_PER_WRITE_DQS :
2336 RW_MGR_MEM_DQ_PER_READ_DQS;
2337 const u32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
2338 SCC_MGR_IO_IN_DELAY_OFFSET;
2339 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
2340
2341 u32 temp_dq_io_delay1, temp_dq_io_delay2;
2342 int shift_dq, i, p;
2343
2344 /* Initialize data for export structures */
2345 *dqs_margin = delay_max + 1;
2346 *dq_margin = delay_max + 1;
2347
2348 /* add delay to bring centre of all DQ windows to the same "level" */
2349 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2350 /* Use values before divide by 2 to reduce round off error */
2351 shift_dq = (left_edge[i] - right_edge[i] -
2352 (left_edge[min_index] - right_edge[min_index]))/2 +
2353 (orig_mid_min - mid_min);
2354
2355 debug_cond(DLEVEL == 2,
2356 "vfifo_center: before: shift_dq[%u]=%d\n",
2357 i, shift_dq);
2358
2359 temp_dq_io_delay1 = readl(addr + (p << 2));
2360 temp_dq_io_delay2 = readl(addr + (i << 2));
2361
2362 if (shift_dq + temp_dq_io_delay1 > delay_max)
2363 shift_dq = delay_max - temp_dq_io_delay2;
2364 else if (shift_dq + temp_dq_io_delay1 < 0)
2365 shift_dq = -temp_dq_io_delay1;
2366
2367 debug_cond(DLEVEL == 2,
2368 "vfifo_center: after: shift_dq[%u]=%d\n",
2369 i, shift_dq);
2370
2371 if (write)
2372 scc_mgr_set_dq_out1_delay(i, temp_dq_io_delay1 + shift_dq);
2373 else
2374 scc_mgr_set_dq_in_delay(p, temp_dq_io_delay1 + shift_dq);
2375
2376 scc_mgr_load_dq(p);
2377
2378 debug_cond(DLEVEL == 2,
2379 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2380 left_edge[i] - shift_dq + (-mid_min),
2381 right_edge[i] + shift_dq - (-mid_min));
2382
2383 /* To determine values for export structures */
2384 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2385 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2386
2387 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2388 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2389 }
2390
2391}
2392
Marek Vasutac63b9a2015-07-21 04:27:32 +02002393/**
2394 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2395 * @rank_bgn: Rank number
2396 * @rw_group: Read/Write Group
2397 * @test_bgn: Rank at which the test begins
2398 * @use_read_test: Perform a read test
2399 * @update_fom: Update FOM
2400 *
2401 * Per-bit deskew DQ and centering.
2402 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002403static int rw_mgr_mem_calibrate_vfifo_center(const u32 rank_bgn,
2404 const u32 rw_group, const u32 test_bgn,
2405 const int use_read_test, const int update_fom)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002406{
Marek Vasut5d6db442015-07-18 19:57:12 +02002407 const u32 addr =
2408 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasut0113c3e2015-07-18 20:42:27 +02002409 (rw_group << 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002410 /*
2411 * Store these as signed since there are comparisons with
2412 * signed numbers.
2413 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002414 uint32_t sticky_bit_chk;
2415 int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
2416 int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
Dinh Nguyen3da42852015-06-02 22:52:49 -05002417 int32_t orig_mid_min, mid_min;
Marek Vasut5d6db442015-07-18 19:57:12 +02002418 int32_t new_dqs, start_dqs, start_dqs_en, final_dqs_en;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002419 int32_t dq_margin, dqs_margin;
Marek Vasut5d6db442015-07-18 19:57:12 +02002420 int i, min_index;
Marek Vasutc4907892015-07-13 02:11:02 +02002421 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002422
Marek Vasut0113c3e2015-07-18 20:42:27 +02002423 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002424
Marek Vasut5d6db442015-07-18 19:57:12 +02002425 start_dqs = readl(addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002426 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut5d6db442015-07-18 19:57:12 +02002427 start_dqs_en = readl(addr - IO_DQS_EN_DELAY_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002428
2429 /* set the left and right edge of each bit to an illegal value */
2430 /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
2431 sticky_bit_chk = 0;
2432 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2433 left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
2434 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
2435 }
2436
Dinh Nguyen3da42852015-06-02 22:52:49 -05002437 /* Search for the left edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002438 search_left_edge(0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002439 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002440 left_edge, right_edge, use_read_test);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002441
Marek Vasutf0712c32015-07-18 08:01:45 +02002442
Dinh Nguyen3da42852015-06-02 22:52:49 -05002443 /* Search for the right edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002444 ret = search_right_edge(0, rank_bgn, rw_group, rw_group,
Marek Vasutc4907892015-07-13 02:11:02 +02002445 start_dqs, start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002446 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002447 left_edge, right_edge, use_read_test);
2448 if (ret) {
2449 /*
2450 * Restore delay chain settings before letting the loop
2451 * in rw_mgr_mem_calibrate_vfifo to retry different
2452 * dqs/ck relationships.
2453 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002454 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Marek Vasutc4907892015-07-13 02:11:02 +02002455 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
Marek Vasut0113c3e2015-07-18 20:42:27 +02002456 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002457
Marek Vasut0113c3e2015-07-18 20:42:27 +02002458 scc_mgr_load_dqs(rw_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002459 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002460
Marek Vasutc4907892015-07-13 02:11:02 +02002461 debug_cond(DLEVEL == 1,
2462 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2463 __func__, __LINE__, i, left_edge[i], right_edge[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002464 if (use_read_test) {
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,
2468 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002469 } else {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002470 set_failing_group_stage(rw_group *
Marek Vasutc4907892015-07-13 02:11:02 +02002471 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2472 CAL_STAGE_VFIFO_AFTER_WRITES,
2473 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002474 }
Marek Vasut98668242015-07-18 20:44:28 +02002475 return -EIO;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002476 }
2477
Marek Vasutafb3eb82015-07-18 19:18:06 +02002478 min_index = get_window_mid_index(0, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002479
2480 /* Determine the amount we can change DQS (which is -mid_min) */
2481 orig_mid_min = mid_min;
2482 new_dqs = start_dqs - mid_min;
2483 if (new_dqs > IO_DQS_IN_DELAY_MAX)
2484 new_dqs = IO_DQS_IN_DELAY_MAX;
2485 else if (new_dqs < 0)
2486 new_dqs = 0;
2487
2488 mid_min = start_dqs - new_dqs;
2489 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2490 mid_min, new_dqs);
2491
2492 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2493 if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2494 mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2495 else if (start_dqs_en - mid_min < 0)
2496 mid_min += start_dqs_en - mid_min;
2497 }
2498 new_dqs = start_dqs - mid_min;
2499
Marek Vasutf0712c32015-07-18 08:01:45 +02002500 debug_cond(DLEVEL == 1,
2501 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2502 start_dqs,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002503 IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2504 new_dqs, mid_min);
2505
Marek Vasutffb8b662015-07-18 19:46:26 +02002506 /* Add delay to bring centre of all DQ windows to the same "level". */
2507 center_dq_windows(0, left_edge, right_edge, mid_min, orig_mid_min,
2508 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002509
Dinh Nguyen3da42852015-06-02 22:52:49 -05002510 /* Move DQS-en */
2511 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
Marek Vasut5d6db442015-07-18 19:57:12 +02002512 final_dqs_en = start_dqs_en - mid_min;
Marek Vasut0113c3e2015-07-18 20:42:27 +02002513 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2514 scc_mgr_load_dqs(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002515 }
2516
2517 /* Move DQS */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002518 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2519 scc_mgr_load_dqs(rw_group);
Marek Vasutf0712c32015-07-18 08:01:45 +02002520 debug_cond(DLEVEL == 2,
2521 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2522 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002523
2524 /*
2525 * Do not remove this line as it makes sure all of our decisions
2526 * have been applied. Apply the update bit.
2527 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002528 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002529
Marek Vasut98668242015-07-18 20:44:28 +02002530 if ((dq_margin < 0) || (dqs_margin < 0))
2531 return -EINVAL;
2532
2533 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002534}
2535
Marek Vasutbce24ef2015-07-17 03:16:45 +02002536/**
Marek Vasut04372fb2015-07-18 02:46:56 +02002537 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
2538 * @rw_group: Read/Write Group
2539 * @phase: DQ/DQS phase
2540 *
2541 * Because initially no communication ca be reliably performed with the memory
2542 * device, the sequencer uses a guaranteed write mechanism to write data into
2543 * the memory device.
2544 */
2545static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
2546 const u32 phase)
2547{
Marek Vasut04372fb2015-07-18 02:46:56 +02002548 int ret;
2549
2550 /* Set a particular DQ/DQS phase. */
2551 scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
2552
2553 debug_cond(DLEVEL == 1, "%s:%d guaranteed write: g=%u p=%u\n",
2554 __func__, __LINE__, rw_group, phase);
2555
2556 /*
2557 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2558 * Load up the patterns used by read calibration using the
2559 * current DQDQS phase.
2560 */
2561 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2562
2563 if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2564 return 0;
2565
2566 /*
2567 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2568 * Back-to-Back reads of the patterns used for calibration.
2569 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02002570 ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
2571 if (ret)
Marek Vasut04372fb2015-07-18 02:46:56 +02002572 debug_cond(DLEVEL == 1,
2573 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2574 __func__, __LINE__, rw_group, phase);
Marek Vasutd844c7d2015-07-18 03:55:07 +02002575 return ret;
Marek Vasut04372fb2015-07-18 02:46:56 +02002576}
2577
2578/**
Marek Vasutf09da112015-07-18 02:57:32 +02002579 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2580 * @rw_group: Read/Write Group
2581 * @test_bgn: Rank at which the test begins
2582 *
2583 * DQS enable calibration ensures reliable capture of the DQ signal without
2584 * glitches on the DQS line.
2585 */
2586static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
2587 const u32 test_bgn)
2588{
Marek Vasutf09da112015-07-18 02:57:32 +02002589 /*
2590 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2591 * DQS and DQS Eanble Signal Relationships.
2592 */
Marek Vasut28ea8272015-07-18 04:28:42 +02002593
2594 /* We start at zero, so have one less dq to devide among */
2595 const u32 delay_step = IO_IO_IN_DELAY_MAX /
2596 (RW_MGR_MEM_DQ_PER_READ_DQS - 1);
Marek Vasut914546e2015-07-20 09:20:42 +02002597 int ret;
Marek Vasut28ea8272015-07-18 04:28:42 +02002598 u32 i, p, d, r;
2599
2600 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2601
2602 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
2603 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
2604 r += NUM_RANKS_PER_SHADOW_REG) {
2605 for (i = 0, p = test_bgn, d = 0;
2606 i < RW_MGR_MEM_DQ_PER_READ_DQS;
2607 i++, p++, d += delay_step) {
2608 debug_cond(DLEVEL == 1,
2609 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2610 __func__, __LINE__, rw_group, r, i, p, d);
2611
2612 scc_mgr_set_dq_in_delay(p, d);
2613 scc_mgr_load_dq(p);
2614 }
2615
2616 writel(0, &sdr_scc_mgr->update);
2617 }
2618
2619 /*
2620 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2621 * dq_in_delay values
2622 */
Marek Vasut914546e2015-07-20 09:20:42 +02002623 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(rw_group);
Marek Vasut28ea8272015-07-18 04:28:42 +02002624
2625 debug_cond(DLEVEL == 1,
2626 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut914546e2015-07-20 09:20:42 +02002627 __func__, __LINE__, rw_group, !ret);
Marek Vasut28ea8272015-07-18 04:28:42 +02002628
2629 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
2630 r += NUM_RANKS_PER_SHADOW_REG) {
2631 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2632 writel(0, &sdr_scc_mgr->update);
2633 }
2634
Marek Vasut914546e2015-07-20 09:20:42 +02002635 return ret;
Marek Vasutf09da112015-07-18 02:57:32 +02002636}
2637
2638/**
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002639 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2640 * @rw_group: Read/Write Group
2641 * @test_bgn: Rank at which the test begins
2642 * @use_read_test: Perform a read test
2643 * @update_fom: Update FOM
2644 *
2645 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2646 * within a group.
2647 */
2648static int
2649rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
2650 const int use_read_test,
2651 const int update_fom)
2652
2653{
2654 int ret, grp_calibrated;
2655 u32 rank_bgn, sr;
2656
2657 /*
2658 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2659 * Read per-bit deskew can be done on a per shadow register basis.
2660 */
2661 grp_calibrated = 1;
2662 for (rank_bgn = 0, sr = 0;
2663 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2664 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
2665 /* Check if this set of ranks should be skipped entirely. */
2666 if (param->skip_shadow_regs[sr])
2667 continue;
2668
2669 ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
Marek Vasut0113c3e2015-07-18 20:42:27 +02002670 test_bgn,
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002671 use_read_test,
2672 update_fom);
Marek Vasut98668242015-07-18 20:44:28 +02002673 if (!ret)
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002674 continue;
2675
2676 grp_calibrated = 0;
2677 }
2678
2679 if (!grp_calibrated)
2680 return -EIO;
2681
2682 return 0;
2683}
2684
2685/**
Marek Vasutbce24ef2015-07-17 03:16:45 +02002686 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2687 * @rw_group: Read/Write Group
2688 * @test_bgn: Rank at which the test begins
Dinh Nguyen3da42852015-06-02 22:52:49 -05002689 *
Marek Vasutbce24ef2015-07-17 03:16:45 +02002690 * Stage 1: Calibrate the read valid prediction FIFO.
2691 *
2692 * This function implements UniPHY calibration Stage 1, as explained in
2693 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2694 *
2695 * - read valid prediction will consist of finding:
2696 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2697 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002698 * - we also do a per-bit deskew on the DQ lines.
2699 */
Marek Vasutc336ca32015-07-17 04:24:18 +02002700static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002701{
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002702 uint32_t p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002703 uint32_t dtaps_per_ptap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002704 uint32_t failed_substage;
2705
Marek Vasut04372fb2015-07-18 02:46:56 +02002706 int ret;
2707
Marek Vasutc336ca32015-07-17 04:24:18 +02002708 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002709
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002710 /* Update info for sims */
2711 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002712 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002713 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002714
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002715 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2716
2717 /* USER Determine number of delay taps for each phase tap. */
Marek Vasutd32badb2015-07-17 03:11:06 +02002718 dtaps_per_ptap = DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP,
2719 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002720
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002721 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002722 /*
2723 * In RLDRAMX we may be messing the delay of pins in
Marek Vasutc336ca32015-07-17 04:24:18 +02002724 * the same write rw_group but outside of the current read
2725 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutac70d2f2015-07-17 03:44:26 +02002726 * output side yet.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002727 */
2728 if (d > 0) {
Marek Vasutf51a7d32015-07-19 02:18:21 +02002729 scc_mgr_apply_group_all_out_delay_add_all_ranks(
Marek Vasutc336ca32015-07-17 04:24:18 +02002730 rw_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002731 }
2732
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002733 for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX; p++) {
Marek Vasut04372fb2015-07-18 02:46:56 +02002734 /* 1) Guaranteed Write */
2735 ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
2736 if (ret)
2737 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002738
Marek Vasutf09da112015-07-18 02:57:32 +02002739 /* 2) DQS Enable Calibration */
2740 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
2741 test_bgn);
2742 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002743 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002744 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002745 }
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002746
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002747 /* 3) Centering DQ/DQS */
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002748 /*
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002749 * If doing read after write calibration, do not update
2750 * FOM now. Do it then.
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002751 */
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002752 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
2753 test_bgn, 1, 0);
2754 if (ret) {
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002755 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002756 continue;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002757 }
2758
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002759 /* All done. */
2760 goto cal_done_ok;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002761 }
2762 }
2763
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002764 /* Calibration Stage 1 failed. */
Marek Vasutc336ca32015-07-17 04:24:18 +02002765 set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002766 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002767
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002768 /* Calibration Stage 1 completed OK. */
2769cal_done_ok:
Dinh Nguyen3da42852015-06-02 22:52:49 -05002770 /*
2771 * Reset the delay chains back to zero if they have moved > 1
2772 * (check for > 1 because loop will increase d even when pass in
2773 * first case).
2774 */
2775 if (d > 2)
Marek Vasutc336ca32015-07-17 04:24:18 +02002776 scc_mgr_zero_group(rw_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002777
2778 return 1;
2779}
2780
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002781/**
2782 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2783 * @rw_group: Read/Write Group
2784 * @test_bgn: Rank at which the test begins
2785 *
2786 * Stage 3: DQ/DQS Centering.
2787 *
2788 * This function implements UniPHY calibration Stage 3, as explained in
2789 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2790 */
2791static int rw_mgr_mem_calibrate_vfifo_end(const u32 rw_group,
2792 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002793{
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002794 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002795
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002796 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002797
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002798 /* Update info for sims. */
2799 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002800 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2801 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2802
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002803 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group, test_bgn, 0, 1);
2804 if (ret)
2805 set_failing_group_stage(rw_group,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002806 CAL_STAGE_VFIFO_AFTER_WRITES,
2807 CAL_SUBSTAGE_VFIFO_CENTER);
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002808 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002809}
2810
Marek Vasutc9842782015-07-21 06:18:57 +02002811/**
2812 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2813 *
2814 * Stage 4: Minimize latency.
2815 *
2816 * This function implements UniPHY calibration Stage 4, as explained in
2817 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2818 * Calibrate LFIFO to find smallest read latency.
2819 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002820static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2821{
Marek Vasutc9842782015-07-21 06:18:57 +02002822 int found_one = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002823
2824 debug("%s:%d\n", __func__, __LINE__);
2825
Marek Vasutc9842782015-07-21 06:18:57 +02002826 /* Update info for sims. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002827 reg_file_set_stage(CAL_STAGE_LFIFO);
2828 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2829
2830 /* Load up the patterns used by read calibration for all ranks */
2831 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002832
Dinh Nguyen3da42852015-06-02 22:52:49 -05002833 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002834 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002835 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2836 __func__, __LINE__, gbl->curr_read_lat);
2837
Marek Vasutc9842782015-07-21 06:18:57 +02002838 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0, NUM_READ_TESTS,
2839 PASS_ALL_BITS, 1))
Dinh Nguyen3da42852015-06-02 22:52:49 -05002840 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002841
2842 found_one = 1;
Marek Vasutc9842782015-07-21 06:18:57 +02002843 /*
2844 * Reduce read latency and see if things are
2845 * working correctly.
2846 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002847 gbl->curr_read_lat--;
2848 } while (gbl->curr_read_lat > 0);
2849
Marek Vasutc9842782015-07-21 06:18:57 +02002850 /* Reset the fifos to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02002851 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002852
2853 if (found_one) {
Marek Vasutc9842782015-07-21 06:18:57 +02002854 /* Add a fudge factor to the read latency that was determined */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002855 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002856 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutc9842782015-07-21 06:18:57 +02002857 debug_cond(DLEVEL == 2,
2858 "%s:%d lfifo: success: using read_lat=%u\n",
2859 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002860 } else {
2861 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2862 CAL_SUBSTAGE_READ_LATENCY);
2863
Marek Vasutc9842782015-07-21 06:18:57 +02002864 debug_cond(DLEVEL == 2,
2865 "%s:%d lfifo: failed at initial read_lat=%u\n",
2866 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002867 }
Marek Vasutc9842782015-07-21 06:18:57 +02002868
2869 return found_one;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002870}
2871
Marek Vasutc8570af2015-07-21 05:26:58 +02002872/**
2873 * search_window() - Search for the/part of the window with DM/DQS shift
2874 * @search_dm: If 1, search for the DM shift, if 0, search for DQS shift
2875 * @rank_bgn: Rank number
2876 * @write_group: Write Group
2877 * @bgn_curr: Current window begin
2878 * @end_curr: Current window end
2879 * @bgn_best: Current best window begin
2880 * @end_best: Current best window end
2881 * @win_best: Size of the best window
2882 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2883 *
2884 * Search for the/part of the window with DM/DQS shift.
2885 */
2886static void search_window(const int search_dm,
2887 const u32 rank_bgn, const u32 write_group,
2888 int *bgn_curr, int *end_curr, int *bgn_best,
2889 int *end_best, int *win_best, int new_dqs)
2890{
2891 u32 bit_chk;
2892 const int max = IO_IO_OUT1_DELAY_MAX - new_dqs;
2893 int d, di;
2894
2895 /* Search for the/part of the window with DM/DQS shift. */
2896 for (di = max; di >= 0; di -= DELTA_D) {
2897 if (search_dm) {
2898 d = di;
2899 scc_mgr_apply_group_dm_out1_delay(d);
2900 } else {
2901 /* For DQS, we go from 0...max */
2902 d = max - di;
2903 /*
2904 * Note: This only shifts DQS, so are we limiting ourselve to
2905 * width of DQ unnecessarily.
2906 */
2907 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2908 d + new_dqs);
2909 }
2910
2911 writel(0, &sdr_scc_mgr->update);
2912
2913 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2914 PASS_ALL_BITS, &bit_chk,
2915 0)) {
2916 /* Set current end of the window. */
2917 *end_curr = search_dm ? -d : d;
2918
2919 /*
2920 * If a starting edge of our window has not been seen
2921 * this is our current start of the DM window.
2922 */
2923 if (*bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2924 *bgn_curr = search_dm ? -d : d;
2925
2926 /*
2927 * If current window is bigger than best seen.
2928 * Set best seen to be current window.
2929 */
2930 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2931 *win_best = *end_curr - *bgn_curr + 1;
2932 *bgn_best = *bgn_curr;
2933 *end_best = *end_curr;
2934 }
2935 } else {
2936 /* We just saw a failing test. Reset temp edge. */
2937 *bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2938 *end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2939
2940 /* Early exit is only applicable to DQS. */
2941 if (search_dm)
2942 continue;
2943
2944 /*
2945 * Early exit optimization: if the remaining delay
2946 * chain space is less than already seen largest
2947 * window we can exit.
2948 */
2949 if (*win_best - 1 > IO_IO_OUT1_DELAY_MAX - new_dqs - d)
2950 break;
2951 }
2952 }
2953}
2954
Dinh Nguyen3da42852015-06-02 22:52:49 -05002955/*
Marek Vasuta386a502015-07-21 05:33:49 +02002956 * rw_mgr_mem_calibrate_writes_center() - Center all windows
2957 * @rank_bgn: Rank number
2958 * @write_group: Write group
2959 * @test_bgn: Rank at which the test begins
2960 *
2961 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen3da42852015-06-02 22:52:49 -05002962 * certain windows.
2963 */
Marek Vasut3b44f552015-07-21 05:00:42 +02002964static int
2965rw_mgr_mem_calibrate_writes_center(const u32 rank_bgn, const u32 write_group,
2966 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002967{
Marek Vasutc8570af2015-07-21 05:26:58 +02002968 int i;
Marek Vasut3b44f552015-07-21 05:00:42 +02002969 u32 sticky_bit_chk;
2970 u32 min_index;
Marek Vasut3b44f552015-07-21 05:00:42 +02002971 int left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2972 int right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2973 int mid;
2974 int mid_min, orig_mid_min;
2975 int new_dqs, start_dqs;
2976 int dq_margin, dqs_margin, dm_margin;
2977 int bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2978 int end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2979 int bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2980 int end_best = IO_IO_OUT1_DELAY_MAX + 1;
2981 int win_best = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002982
Marek Vasutc4907892015-07-13 02:11:02 +02002983 int ret;
2984
Dinh Nguyen3da42852015-06-02 22:52:49 -05002985 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2986
2987 dm_margin = 0;
2988
Marek Vasutc6540872015-07-21 05:29:05 +02002989 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
2990 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Dinh Nguyen3da42852015-06-02 22:52:49 -05002991 (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2992
Marek Vasut3b44f552015-07-21 05:00:42 +02002993 /* Per-bit deskew. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002994
2995 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02002996 * Set the left and right edge of each bit to an illegal value.
2997 * Use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002998 */
2999 sticky_bit_chk = 0;
3000 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
3001 left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
3002 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
3003 }
3004
Marek Vasut3b44f552015-07-21 05:00:42 +02003005 /* Search for the left edge of the window for each bit. */
Marek Vasut71120772015-07-13 02:38:15 +02003006 search_left_edge(1, rank_bgn, write_group, 0, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02003007 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02003008 left_edge, right_edge, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003009
Marek Vasut3b44f552015-07-21 05:00:42 +02003010 /* Search for the right edge of the window for each bit. */
Marek Vasutc4907892015-07-13 02:11:02 +02003011 ret = search_right_edge(1, rank_bgn, write_group, 0,
3012 start_dqs, 0,
Marek Vasut0c4be192015-07-18 20:34:00 +02003013 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02003014 left_edge, right_edge, 0);
3015 if (ret) {
3016 set_failing_group_stage(test_bgn + ret - 1, CAL_STAGE_WRITES,
3017 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutd043ee52015-07-21 05:32:49 +02003018 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003019 }
3020
Marek Vasutafb3eb82015-07-18 19:18:06 +02003021 min_index = get_window_mid_index(1, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003022
Marek Vasut3b44f552015-07-21 05:00:42 +02003023 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003024 orig_mid_min = mid_min;
3025 new_dqs = start_dqs;
3026 mid_min = 0;
Marek Vasut3b44f552015-07-21 05:00:42 +02003027 debug_cond(DLEVEL == 1,
3028 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3029 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003030
Marek Vasutffb8b662015-07-18 19:46:26 +02003031 /* Add delay to bring centre of all DQ windows to the same "level". */
3032 center_dq_windows(1, left_edge, right_edge, mid_min, orig_mid_min,
3033 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003034
3035 /* Move DQS */
3036 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02003037 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003038
3039 /* Centre DM */
3040 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
3041
3042 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003043 * Set the left and right edge of each bit to an illegal value.
3044 * Use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003045 */
3046 left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
3047 right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003048
Marek Vasut3b44f552015-07-21 05:00:42 +02003049 /* Search for the/part of the window with DM shift. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003050 search_window(1, rank_bgn, write_group, &bgn_curr, &end_curr,
3051 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003052
Marek Vasut3b44f552015-07-21 05:00:42 +02003053 /* Reset DM delay chains to 0. */
Marek Vasut32675242015-07-17 06:07:13 +02003054 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003055
3056 /*
3057 * Check to see if the current window nudges up aganist 0 delay.
3058 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasut3b44f552015-07-21 05:00:42 +02003059 * search begins as a new search.
3060 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003061 if (end_curr != 0) {
3062 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3063 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3064 }
3065
Marek Vasut3b44f552015-07-21 05:00:42 +02003066 /* Search for the/part of the window with DQS shifts. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003067 search_window(0, rank_bgn, write_group, &bgn_curr, &end_curr,
3068 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003069
Marek Vasut3b44f552015-07-21 05:00:42 +02003070 /* Assign left and right edge for cal and reporting. */
3071 left_edge[0] = -1 * bgn_best;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003072 right_edge[0] = end_best;
3073
Marek Vasut3b44f552015-07-21 05:00:42 +02003074 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n",
3075 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003076
Marek Vasut3b44f552015-07-21 05:00:42 +02003077 /* Move DQS (back to orig). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003078 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3079
3080 /* Move DM */
3081
Marek Vasut3b44f552015-07-21 05:00:42 +02003082 /* Find middle of window for the DM bit. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003083 mid = (left_edge[0] - right_edge[0]) / 2;
3084
Marek Vasut3b44f552015-07-21 05:00:42 +02003085 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003086 if (mid < 0)
3087 mid = 0;
3088
Marek Vasut3b44f552015-07-21 05:00:42 +02003089 /* dm_marign should fail if we never find a window. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003090 if (win_best == 0)
3091 dm_margin = -1;
3092 else
3093 dm_margin = left_edge[0] - mid;
3094
Marek Vasut32675242015-07-17 06:07:13 +02003095 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003096 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003097
Marek Vasut3b44f552015-07-21 05:00:42 +02003098 debug_cond(DLEVEL == 2,
3099 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3100 __func__, __LINE__, left_edge[0], right_edge[0],
3101 mid, dm_margin);
3102 /* Export values. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003103 gbl->fom_out += dq_margin + dqs_margin;
3104
Marek Vasut3b44f552015-07-21 05:00:42 +02003105 debug_cond(DLEVEL == 2,
3106 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3107 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003108
3109 /*
3110 * Do not remove this line as it makes sure all of our
3111 * decisions have been applied.
3112 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003113 writel(0, &sdr_scc_mgr->update);
Marek Vasut3b44f552015-07-21 05:00:42 +02003114
Marek Vasutd043ee52015-07-21 05:32:49 +02003115 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3116 return -EINVAL;
3117
3118 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003119}
3120
Marek Vasutdb3a6062015-07-18 07:23:25 +02003121/**
3122 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3123 * @rank_bgn: Rank number
3124 * @group: Read/Write Group
3125 * @test_bgn: Rank at which the test begins
3126 *
3127 * Stage 2: Write Calibration Part One.
3128 *
3129 * This function implements UniPHY calibration Stage 2, as explained in
3130 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3131 */
3132static int rw_mgr_mem_calibrate_writes(const u32 rank_bgn, const u32 group,
3133 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003134{
Marek Vasutdb3a6062015-07-18 07:23:25 +02003135 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003136
Marek Vasutdb3a6062015-07-18 07:23:25 +02003137 /* Update info for sims */
3138 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3139
3140 reg_file_set_group(group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003141 reg_file_set_stage(CAL_STAGE_WRITES);
3142 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3143
Marek Vasutdb3a6062015-07-18 07:23:25 +02003144 ret = rw_mgr_mem_calibrate_writes_center(rank_bgn, group, test_bgn);
Marek Vasutd043ee52015-07-21 05:32:49 +02003145 if (ret)
Marek Vasutdb3a6062015-07-18 07:23:25 +02003146 set_failing_group_stage(group, CAL_STAGE_WRITES,
Dinh Nguyen3da42852015-06-02 22:52:49 -05003147 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003148
Marek Vasutd043ee52015-07-21 05:32:49 +02003149 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003150}
3151
Marek Vasut4b0ac262015-07-20 07:33:33 +02003152/**
3153 * mem_precharge_and_activate() - Precharge all banks and activate
3154 *
3155 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3156 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003157static void mem_precharge_and_activate(void)
3158{
Marek Vasut4b0ac262015-07-20 07:33:33 +02003159 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003160
3161 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
Marek Vasut4b0ac262015-07-20 07:33:33 +02003162 /* Test if the rank should be skipped. */
3163 if (param->skip_ranks[r])
Dinh Nguyen3da42852015-06-02 22:52:49 -05003164 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003165
Marek Vasut4b0ac262015-07-20 07:33:33 +02003166 /* Set rank. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003167 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3168
Marek Vasut4b0ac262015-07-20 07:33:33 +02003169 /* Precharge all banks. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003170 writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3171 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003172
Marek Vasut1273dd92015-07-12 21:05:08 +02003173 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3174 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3175 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003176
Marek Vasut1273dd92015-07-12 21:05:08 +02003177 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3178 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3179 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003180
Marek Vasut4b0ac262015-07-20 07:33:33 +02003181 /* Activate rows. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003182 writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3183 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003184 }
3185}
3186
Marek Vasut16502a02015-07-17 01:57:41 +02003187/**
3188 * mem_init_latency() - Configure memory RLAT and WLAT settings
3189 *
3190 * Configure memory RLAT and WLAT parameters.
3191 */
3192static void mem_init_latency(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003193{
Marek Vasut16502a02015-07-17 01:57:41 +02003194 /*
3195 * For AV/CV, LFIFO is hardened and always runs at full rate
3196 * so max latency in AFI clocks, used here, is correspondingly
3197 * smaller.
3198 */
3199 const u32 max_latency = (1 << MAX_LATENCY_COUNT_WIDTH) - 1;
3200 u32 rlat, wlat;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003201
3202 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut16502a02015-07-17 01:57:41 +02003203
3204 /*
3205 * Read in write latency.
3206 * WL for Hard PHY does not include additive latency.
3207 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003208 wlat = readl(&data_mgr->t_wl_add);
3209 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003210
Marek Vasut16502a02015-07-17 01:57:41 +02003211 gbl->rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003212
Marek Vasut16502a02015-07-17 01:57:41 +02003213 /* Read in readl latency. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003214 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003215
Marek Vasut16502a02015-07-17 01:57:41 +02003216 /* Set a pretty high read latency initially. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003217 gbl->curr_read_lat = rlat + 16;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003218 if (gbl->curr_read_lat > max_latency)
3219 gbl->curr_read_lat = max_latency;
3220
Marek Vasut1273dd92015-07-12 21:05:08 +02003221 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003222
Marek Vasut16502a02015-07-17 01:57:41 +02003223 /* Advertise write latency. */
3224 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003225}
3226
Marek Vasut51cea0b2015-07-26 10:54:15 +02003227/**
3228 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3229 *
3230 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3231 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003232static void mem_skip_calibrate(void)
3233{
3234 uint32_t vfifo_offset;
3235 uint32_t i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003236
3237 debug("%s:%d\n", __func__, __LINE__);
3238 /* Need to update every shadow register set used by the interface */
3239 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003240 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003241 /*
3242 * Set output phase alignment settings appropriate for
3243 * skip calibration.
3244 */
3245 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3246 scc_mgr_set_dqs_en_phase(i, 0);
3247#if IO_DLL_CHAIN_LENGTH == 6
3248 scc_mgr_set_dqdqs_output_phase(i, 6);
3249#else
3250 scc_mgr_set_dqdqs_output_phase(i, 7);
3251#endif
3252 /*
3253 * Case:33398
3254 *
3255 * Write data arrives to the I/O two cycles before write
3256 * latency is reached (720 deg).
3257 * -> due to bit-slip in a/c bus
3258 * -> to allow board skew where dqs is longer than ck
3259 * -> how often can this happen!?
3260 * -> can claim back some ptaps for high freq
3261 * support if we can relax this, but i digress...
3262 *
3263 * The write_clk leads mem_ck by 90 deg
3264 * The minimum ptap of the OPA is 180 deg
3265 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3266 * The write_clk is always delayed by 2 ptaps
3267 *
3268 * Hence, to make DQS aligned to CK, we need to delay
3269 * DQS by:
3270 * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3271 *
3272 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3273 * gives us the number of ptaps, which simplies to:
3274 *
3275 * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3276 */
Marek Vasut51cea0b2015-07-26 10:54:15 +02003277 scc_mgr_set_dqdqs_output_phase(i,
3278 1.25 * IO_DLL_CHAIN_LENGTH - 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003279 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003280 writel(0xff, &sdr_scc_mgr->dqs_ena);
3281 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003282
Dinh Nguyen3da42852015-06-02 22:52:49 -05003283 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003284 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3285 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003286 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003287 writel(0xff, &sdr_scc_mgr->dq_ena);
3288 writel(0xff, &sdr_scc_mgr->dm_ena);
3289 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003290 }
3291
3292 /* Compensate for simulation model behaviour */
3293 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3294 scc_mgr_set_dqs_bus_in_delay(i, 10);
3295 scc_mgr_load_dqs(i);
3296 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003297 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003298
3299 /*
3300 * ArriaV has hard FIFOs that can only be initialized by incrementing
3301 * in sequencer.
3302 */
3303 vfifo_offset = CALIB_VFIFO_OFFSET;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003304 for (j = 0; j < vfifo_offset; j++)
Marek Vasut1273dd92015-07-12 21:05:08 +02003305 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasut1273dd92015-07-12 21:05:08 +02003306 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003307
3308 /*
Marek Vasut51cea0b2015-07-26 10:54:15 +02003309 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3310 * setting from generation-time constant.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003311 */
3312 gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
Marek Vasut1273dd92015-07-12 21:05:08 +02003313 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003314}
3315
Marek Vasut3589fbf2015-07-20 04:34:51 +02003316/**
3317 * mem_calibrate() - Memory calibration entry point.
3318 *
3319 * Perform memory calibration.
3320 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003321static uint32_t mem_calibrate(void)
3322{
3323 uint32_t i;
3324 uint32_t rank_bgn, sr;
3325 uint32_t write_group, write_test_bgn;
3326 uint32_t read_group, read_test_bgn;
3327 uint32_t run_groups, current_run;
3328 uint32_t failing_groups = 0;
3329 uint32_t group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003330
Marek Vasut33c42bb2015-07-17 02:21:47 +02003331 const u32 rwdqs_ratio = RW_MGR_MEM_IF_READ_DQS_WIDTH /
3332 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
3333
Dinh Nguyen3da42852015-06-02 22:52:49 -05003334 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003335
Marek Vasut16502a02015-07-17 01:57:41 +02003336 /* Initialize the data settings */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003337 gbl->error_substage = CAL_SUBSTAGE_NIL;
3338 gbl->error_stage = CAL_STAGE_NIL;
3339 gbl->error_group = 0xff;
3340 gbl->fom_in = 0;
3341 gbl->fom_out = 0;
3342
Marek Vasut16502a02015-07-17 01:57:41 +02003343 /* Initialize WLAT and RLAT. */
3344 mem_init_latency();
3345
3346 /* Initialize bit slips. */
3347 mem_precharge_and_activate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003348
Dinh Nguyen3da42852015-06-02 22:52:49 -05003349 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003350 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3351 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003352 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3353 if (i == 0)
3354 scc_mgr_set_hhp_extras();
3355
Marek Vasutc5c5f532015-07-17 02:06:20 +02003356 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003357 }
3358
Marek Vasut722c9682015-07-17 02:07:12 +02003359 /* Calibration is skipped. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003360 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3361 /*
3362 * Set VFIFO and LFIFO to instant-on settings in skip
3363 * calibration mode.
3364 */
3365 mem_skip_calibrate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003366
Marek Vasut722c9682015-07-17 02:07:12 +02003367 /*
3368 * Do not remove this line as it makes sure all of our
3369 * decisions have been applied.
3370 */
3371 writel(0, &sdr_scc_mgr->update);
3372 return 1;
3373 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003374
Marek Vasut722c9682015-07-17 02:07:12 +02003375 /* Calibration is not skipped. */
3376 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3377 /*
3378 * Zero all delay chain/phase settings for all
3379 * groups and all shadow register sets.
3380 */
3381 scc_mgr_zero_all();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003382
Marek Vasut722c9682015-07-17 02:07:12 +02003383 run_groups = ~param->skip_groups;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003384
Marek Vasut722c9682015-07-17 02:07:12 +02003385 for (write_group = 0, write_test_bgn = 0; write_group
3386 < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3387 write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003388
3389 /* Initialize the group failure */
Marek Vasut722c9682015-07-17 02:07:12 +02003390 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003391
Marek Vasut722c9682015-07-17 02:07:12 +02003392 current_run = run_groups & ((1 <<
3393 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3394 run_groups = run_groups >>
3395 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003396
Marek Vasut722c9682015-07-17 02:07:12 +02003397 if (current_run == 0)
3398 continue;
3399
3400 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3401 SCC_MGR_GROUP_COUNTER_OFFSET);
3402 scc_mgr_zero_group(write_group, 0);
3403
Marek Vasut33c42bb2015-07-17 02:21:47 +02003404 for (read_group = write_group * rwdqs_ratio,
3405 read_test_bgn = 0;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003406 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003407 read_group++,
3408 read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
3409 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3410 continue;
Marek Vasut722c9682015-07-17 02:07:12 +02003411
Marek Vasut33c42bb2015-07-17 02:21:47 +02003412 /* Calibrate the VFIFO */
3413 if (rw_mgr_mem_calibrate_vfifo(read_group,
3414 read_test_bgn))
3415 continue;
3416
Marek Vasutc452dcd2015-07-17 02:50:56 +02003417 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3418 return 0;
3419
3420 /* The group failed, we're done. */
3421 goto grp_failed;
3422 }
3423
3424 /* Calibrate the output side */
3425 for (rank_bgn = 0, sr = 0;
3426 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
3427 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3428 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3429 continue;
3430
3431 /* Not needed in quick mode! */
3432 if (STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS)
3433 continue;
3434
3435 /*
3436 * Determine if this set of ranks
3437 * should be skipped entirely.
3438 */
3439 if (param->skip_shadow_regs[sr])
3440 continue;
3441
3442 /* Calibrate WRITEs */
Marek Vasutdb3a6062015-07-18 07:23:25 +02003443 if (!rw_mgr_mem_calibrate_writes(rank_bgn,
Marek Vasutc452dcd2015-07-17 02:50:56 +02003444 write_group, write_test_bgn))
3445 continue;
3446
Marek Vasut33c42bb2015-07-17 02:21:47 +02003447 group_failed = 1;
3448 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3449 return 0;
Marek Vasut722c9682015-07-17 02:07:12 +02003450 }
3451
Marek Vasutc452dcd2015-07-17 02:50:56 +02003452 /* Some group failed, we're done. */
3453 if (group_failed)
3454 goto grp_failed;
Marek Vasut4ac21612015-07-17 02:31:04 +02003455
Marek Vasutc452dcd2015-07-17 02:50:56 +02003456 for (read_group = write_group * rwdqs_ratio,
3457 read_test_bgn = 0;
3458 read_group < (write_group + 1) * rwdqs_ratio;
3459 read_group++,
3460 read_test_bgn += RW_MGR_MEM_DQ_PER_READ_DQS) {
3461 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3462 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003463
Marek Vasut78cdd7d2015-07-18 05:58:44 +02003464 if (!rw_mgr_mem_calibrate_vfifo_end(read_group,
Marek Vasutc452dcd2015-07-17 02:50:56 +02003465 read_test_bgn))
3466 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003467
Marek Vasutc452dcd2015-07-17 02:50:56 +02003468 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3469 return 0;
Marek Vasut4ac21612015-07-17 02:31:04 +02003470
Marek Vasutc452dcd2015-07-17 02:50:56 +02003471 /* The group failed, we're done. */
3472 goto grp_failed;
Marek Vasut722c9682015-07-17 02:07:12 +02003473 }
3474
Marek Vasutc452dcd2015-07-17 02:50:56 +02003475 /* No group failed, continue as usual. */
3476 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003477
Marek Vasutc452dcd2015-07-17 02:50:56 +02003478grp_failed: /* A group failed, increment the counter. */
3479 failing_groups++;
Marek Vasut722c9682015-07-17 02:07:12 +02003480 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003481
Marek Vasut722c9682015-07-17 02:07:12 +02003482 /*
3483 * USER If there are any failing groups then report
3484 * the failure.
3485 */
3486 if (failing_groups != 0)
3487 return 0;
3488
Marek Vasutc50ae302015-07-17 02:40:21 +02003489 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3490 continue;
3491
3492 /*
3493 * If we're skipping groups as part of debug,
3494 * don't calibrate LFIFO.
3495 */
3496 if (param->skip_groups != 0)
3497 continue;
3498
Marek Vasut722c9682015-07-17 02:07:12 +02003499 /* Calibrate the LFIFO */
Marek Vasutc50ae302015-07-17 02:40:21 +02003500 if (!rw_mgr_mem_calibrate_lfifo())
3501 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003502 }
3503
3504 /*
3505 * Do not remove this line as it makes sure all of our decisions
3506 * have been applied.
3507 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003508 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003509 return 1;
3510}
3511
Marek Vasut23a040c2015-07-17 01:20:21 +02003512/**
3513 * run_mem_calibrate() - Perform memory calibration
3514 *
3515 * This function triggers the entire memory calibration procedure.
3516 */
3517static int run_mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003518{
Marek Vasut23a040c2015-07-17 01:20:21 +02003519 int pass;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003520
3521 debug("%s:%d\n", __func__, __LINE__);
3522
3523 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003524 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003525
Marek Vasut23a040c2015-07-17 01:20:21 +02003526 /* Stop tracking manager. */
3527 clrbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003528
Marek Vasut9fa9c902015-07-17 01:12:07 +02003529 phy_mgr_initialize();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003530 rw_mgr_mem_initialize();
3531
Marek Vasut23a040c2015-07-17 01:20:21 +02003532 /* Perform the actual memory calibration. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003533 pass = mem_calibrate();
3534
3535 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003536 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003537
Marek Vasut23a040c2015-07-17 01:20:21 +02003538 /* Handoff. */
3539 rw_mgr_mem_handoff();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003540 /*
Marek Vasut23a040c2015-07-17 01:20:21 +02003541 * In Hard PHY this is a 2-bit control:
3542 * 0: AFI Mux Select
3543 * 1: DDIO Mux Select
Dinh Nguyen3da42852015-06-02 22:52:49 -05003544 */
Marek Vasut23a040c2015-07-17 01:20:21 +02003545 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003546
Marek Vasut23a040c2015-07-17 01:20:21 +02003547 /* Start tracking manager. */
3548 setbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
3549
3550 return pass;
3551}
3552
3553/**
3554 * debug_mem_calibrate() - Report result of memory calibration
3555 * @pass: Value indicating whether calibration passed or failed
3556 *
3557 * This function reports the results of the memory calibration
3558 * and writes debug information into the register file.
3559 */
3560static void debug_mem_calibrate(int pass)
3561{
3562 uint32_t debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003563
3564 if (pass) {
3565 printf("%s: CALIBRATION PASSED\n", __FILE__);
3566
3567 gbl->fom_in /= 2;
3568 gbl->fom_out /= 2;
3569
3570 if (gbl->fom_in > 0xff)
3571 gbl->fom_in = 0xff;
3572
3573 if (gbl->fom_out > 0xff)
3574 gbl->fom_out = 0xff;
3575
3576 /* Update the FOM in the register file */
3577 debug_info = gbl->fom_in;
3578 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003579 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003580
Marek Vasut1273dd92015-07-12 21:05:08 +02003581 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3582 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003583 } else {
3584 printf("%s: CALIBRATION FAILED\n", __FILE__);
3585
3586 debug_info = gbl->error_stage;
3587 debug_info |= gbl->error_substage << 8;
3588 debug_info |= gbl->error_group << 16;
3589
Marek Vasut1273dd92015-07-12 21:05:08 +02003590 writel(debug_info, &sdr_reg_file->failing_stage);
3591 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3592 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003593
3594 /* Update the failing group/stage in the register file */
3595 debug_info = gbl->error_stage;
3596 debug_info |= gbl->error_substage << 8;
3597 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003598 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003599 }
3600
Marek Vasut23a040c2015-07-17 01:20:21 +02003601 printf("%s: Calibration complete\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003602}
3603
Marek Vasutbb064342015-07-19 06:12:42 +02003604/**
3605 * hc_initialize_rom_data() - Initialize ROM data
3606 *
3607 * Initialize ROM data.
3608 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003609static void hc_initialize_rom_data(void)
3610{
Marek Vasutbb064342015-07-19 06:12:42 +02003611 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003612
Marek Vasutc4815f72015-07-12 19:03:33 +02003613 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003614 for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3615 writel(inst_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003616
Marek Vasutc4815f72015-07-12 19:03:33 +02003617 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasutbb064342015-07-19 06:12:42 +02003618 for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3619 writel(ac_rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003620}
3621
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003622/**
3623 * initialize_reg_file() - Initialize SDR register file
3624 *
3625 * Initialize SDR register file.
3626 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003627static void initialize_reg_file(void)
3628{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003629 /* Initialize the register file with the correct data */
Marek Vasut1273dd92015-07-12 21:05:08 +02003630 writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3631 writel(0, &sdr_reg_file->debug_data_addr);
3632 writel(0, &sdr_reg_file->cur_stage);
3633 writel(0, &sdr_reg_file->fom);
3634 writel(0, &sdr_reg_file->failing_stage);
3635 writel(0, &sdr_reg_file->debug1);
3636 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003637}
3638
Marek Vasut2ca151f2015-07-19 06:14:04 +02003639/**
3640 * initialize_hps_phy() - Initialize HPS PHY
3641 *
3642 * Initialize HPS PHY.
3643 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003644static void initialize_hps_phy(void)
3645{
3646 uint32_t reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003647 /*
3648 * Tracking also gets configured here because it's in the
3649 * same register.
3650 */
3651 uint32_t trk_sample_count = 7500;
3652 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3653 /*
3654 * Format is number of outer loops in the 16 MSB, sample
3655 * count in 16 LSB.
3656 */
3657
3658 reg = 0;
3659 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3660 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3661 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3662 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3663 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3664 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3665 /*
3666 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3667 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3668 */
3669 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3670 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3671 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003672 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003673
3674 reg = 0;
3675 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3676 trk_sample_count >>
3677 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3678 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3679 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003680 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003681
3682 reg = 0;
3683 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3684 trk_long_idle_sample_count >>
3685 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003686 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003687}
3688
Marek Vasut880e46f2015-07-17 00:45:11 +02003689/**
3690 * initialize_tracking() - Initialize tracking
3691 *
3692 * Initialize the register file with usable initial data.
3693 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003694static void initialize_tracking(void)
3695{
Marek Vasut880e46f2015-07-17 00:45:11 +02003696 /*
3697 * Initialize the register file with the correct data.
3698 * Compute usable version of value in case we skip full
3699 * computation later.
3700 */
3701 writel(DIV_ROUND_UP(IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP) - 1,
3702 &sdr_reg_file->dtaps_per_ptap);
3703
3704 /* trk_sample_count */
3705 writel(7500, &sdr_reg_file->trk_sample_count);
3706
3707 /* longidle outer loop [15:0] */
3708 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003709
3710 /*
Marek Vasut880e46f2015-07-17 00:45:11 +02003711 * longidle sample count [31:24]
3712 * trfc, worst case of 933Mhz 4Gb [23:16]
3713 * trcd, worst case [15:8]
3714 * vfifo wait [7:0]
Dinh Nguyen3da42852015-06-02 22:52:49 -05003715 */
Marek Vasut880e46f2015-07-17 00:45:11 +02003716 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3717 &sdr_reg_file->delays);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003718
Marek Vasut880e46f2015-07-17 00:45:11 +02003719 /* mux delay */
3720 writel((RW_MGR_IDLE << 24) | (RW_MGR_ACTIVATE_1 << 16) |
3721 (RW_MGR_SGLE_READ << 8) | (RW_MGR_PRECHARGE_ALL << 0),
3722 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003723
Marek Vasut880e46f2015-07-17 00:45:11 +02003724 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH,
3725 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003726
Marek Vasut880e46f2015-07-17 00:45:11 +02003727 /* trefi [7:0] */
3728 writel((RW_MGR_REFRESH_ALL << 24) | (1000 << 0),
3729 &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003730}
3731
3732int sdram_calibration_full(void)
3733{
3734 struct param_type my_param;
3735 struct gbl_type my_gbl;
3736 uint32_t pass;
Marek Vasut84e0b0c2015-07-17 01:05:36 +02003737
3738 memset(&my_param, 0, sizeof(my_param));
3739 memset(&my_gbl, 0, sizeof(my_gbl));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003740
3741 param = &my_param;
3742 gbl = &my_gbl;
3743
Dinh Nguyen3da42852015-06-02 22:52:49 -05003744 /* Set the calibration enabled by default */
3745 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3746 /*
3747 * Only sweep all groups (regardless of fail state) by default
3748 * Set enabled read test by default.
3749 */
3750#if DISABLE_GUARANTEED_READ
3751 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3752#endif
3753 /* Initialize the register file */
3754 initialize_reg_file();
3755
3756 /* Initialize any PHY CSR */
3757 initialize_hps_phy();
3758
3759 scc_mgr_initialize();
3760
3761 initialize_tracking();
3762
Dinh Nguyen3da42852015-06-02 22:52:49 -05003763 printf("%s: Preparing to start memory calibration\n", __FILE__);
3764
3765 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut23f62b32015-07-13 01:05:27 +02003766 debug_cond(DLEVEL == 1,
3767 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3768 RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3769 RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3770 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3771 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3772 debug_cond(DLEVEL == 1,
3773 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3774 RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3775 RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3776 IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3777 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3778 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3779 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3780 IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3781 IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3782 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3783 IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3784 IO_IO_OUT2_DELAY_MAX);
3785 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3786 IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003787
3788 hc_initialize_rom_data();
3789
3790 /* update info for sims */
3791 reg_file_set_stage(CAL_STAGE_NIL);
3792 reg_file_set_group(0);
3793
3794 /*
3795 * Load global needed for those actions that require
3796 * some dynamic calibration support.
3797 */
3798 dyn_calib_steps = STATIC_CALIB_STEPS;
3799 /*
3800 * Load global to allow dynamic selection of delay loop settings
3801 * based on calibration mode.
3802 */
3803 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3804 skip_delay_mask = 0xff;
3805 else
3806 skip_delay_mask = 0x0;
3807
3808 pass = run_mem_calibrate();
Marek Vasut23a040c2015-07-17 01:20:21 +02003809 debug_mem_calibrate(pass);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003810 return pass;
3811}