blob: e6cc12ea54e76abae97c6097567ad7c5da1f2639 [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"
Marek Vasut9c76df52015-08-02 16:55:45 +020012
Dinh Nguyen3da42852015-06-02 22:52:49 -050013static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020014 (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen3da42852015-06-02 22:52:49 -050015
16static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut6afb4fe2015-07-12 18:46:52 +020017 (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050018
19static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasuta1c654a2015-07-12 18:31:05 +020020 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050021
22static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasute79025a2015-07-12 18:42:34 +020023 (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050024
25static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasut1bc6f142015-07-12 18:54:37 +020026 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050027
28static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasut1bc6f142015-07-12 18:54:37 +020029 (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen3da42852015-06-02 22:52:49 -050030
31static struct socfpga_data_mgr *data_mgr =
Marek Vasutc4815f72015-07-12 19:03:33 +020032 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050033
Marek Vasut6cb9f162015-07-12 20:49:39 +020034static struct socfpga_sdr_ctrl *sdr_ctrl =
35 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
36
Marek Vasutd718a262015-08-02 18:12:08 +020037const struct socfpga_sdram_rw_mgr_config *rwcfg;
Marek Vasut10c14262015-08-02 19:00:23 +020038const struct socfpga_sdram_io_config *iocfg;
Marek Vasut042ff2d2015-08-02 19:18:47 +020039const struct socfpga_sdram_misc_config *misccfg;
Marek Vasutd718a262015-08-02 18:12:08 +020040
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;
Dinh Nguyen3da42852015-06-02 22:52:49 -050081
Dinh Nguyen3da42852015-06-02 22:52:49 -050082static void set_failing_group_stage(uint32_t group, uint32_t stage,
83 uint32_t substage)
84{
85 /*
86 * Only set the global stage if there was not been any other
87 * failing group
88 */
89 if (gbl->error_stage == CAL_STAGE_NIL) {
90 gbl->error_substage = substage;
91 gbl->error_stage = stage;
92 gbl->error_group = group;
93 }
94}
95
Marek Vasut2c0d2d92015-07-12 21:10:24 +020096static void reg_file_set_group(u16 set_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -050097{
Marek Vasut2c0d2d92015-07-12 21:10:24 +020098 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen3da42852015-06-02 22:52:49 -050099}
100
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200101static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500102{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200103 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500104}
105
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200106static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500107{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200108 set_sub_stage &= 0xff;
109 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500110}
111
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200112/**
113 * phy_mgr_initialize() - Initialize PHY Manager
114 *
115 * Initialize PHY Manager.
116 */
Marek Vasut9fa9c902015-07-17 01:12:07 +0200117static void phy_mgr_initialize(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500118{
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200119 u32 ratio;
120
Dinh Nguyen3da42852015-06-02 22:52:49 -0500121 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200122 /* Calibration has control over path to memory */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500123 /*
124 * In Hard PHY this is a 2-bit control:
125 * 0: AFI Mux Select
126 * 1: DDIO Mux Select
127 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200128 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500129
130 /* USER memory clock is not stable we begin initialization */
Marek Vasut1273dd92015-07-12 21:05:08 +0200131 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500132
133 /* USER calibration status all set to zero */
Marek Vasut1273dd92015-07-12 21:05:08 +0200134 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500135
Marek Vasut1273dd92015-07-12 21:05:08 +0200136 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500137
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200138 /* Init params only if we do NOT skip calibration. */
139 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
140 return;
141
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200142 ratio = rwcfg->mem_dq_per_read_dqs /
143 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200144 param->read_correct_mask_vg = (1 << ratio) - 1;
145 param->write_correct_mask_vg = (1 << ratio) - 1;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200146 param->read_correct_mask = (1 << rwcfg->mem_dq_per_read_dqs) - 1;
147 param->write_correct_mask = (1 << rwcfg->mem_dq_per_write_dqs) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500148}
149
Marek Vasut080bf642015-07-20 08:15:57 +0200150/**
151 * set_rank_and_odt_mask() - Set Rank and ODT mask
152 * @rank: Rank mask
153 * @odt_mode: ODT mode, OFF or READ_WRITE
154 *
155 * Set Rank and ODT mask (On-Die Termination).
156 */
Marek Vasutb2dfd102015-07-20 08:03:11 +0200157static void set_rank_and_odt_mask(const u32 rank, const u32 odt_mode)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500158{
Marek Vasutb2dfd102015-07-20 08:03:11 +0200159 u32 odt_mask_0 = 0;
160 u32 odt_mask_1 = 0;
161 u32 cs_and_odt_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500162
Marek Vasutb2dfd102015-07-20 08:03:11 +0200163 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
164 odt_mask_0 = 0x0;
165 odt_mask_1 = 0x0;
166 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200167 switch (rwcfg->mem_number_of_ranks) {
Marek Vasut287cdf62015-07-20 08:09:05 +0200168 case 1: /* 1 Rank */
169 /* Read: ODT = 0 ; Write: ODT = 1 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500170 odt_mask_0 = 0x0;
171 odt_mask_1 = 0x1;
Marek Vasut287cdf62015-07-20 08:09:05 +0200172 break;
173 case 2: /* 2 Ranks */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200174 if (rwcfg->mem_number_of_cs_per_dimm == 1) {
Marek Vasut080bf642015-07-20 08:15:57 +0200175 /*
176 * - Dual-Slot , Single-Rank (1 CS per DIMM)
177 * OR
178 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
179 *
180 * Since MEM_NUMBER_OF_RANKS is 2, they
181 * are both single rank with 2 CS each
182 * (special for RDIMM).
183 *
Dinh Nguyen3da42852015-06-02 22:52:49 -0500184 * Read: Turn on ODT on the opposite rank
185 * Write: Turn on ODT on all ranks
186 */
187 odt_mask_0 = 0x3 & ~(1 << rank);
188 odt_mask_1 = 0x3;
189 } else {
190 /*
Marek Vasut080bf642015-07-20 08:15:57 +0200191 * - Single-Slot , Dual-Rank (2 CS per DIMM)
192 *
193 * Read: Turn on ODT off on all ranks
194 * Write: Turn on ODT on active rank
Dinh Nguyen3da42852015-06-02 22:52:49 -0500195 */
196 odt_mask_0 = 0x0;
197 odt_mask_1 = 0x3 & (1 << rank);
198 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200199 break;
200 case 4: /* 4 Ranks */
201 /* Read:
Dinh Nguyen3da42852015-06-02 22:52:49 -0500202 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500203 * | ODT |
204 * Read From +-----------------------+
205 * Rank | 3 | 2 | 1 | 0 |
206 * ----------+-----+-----+-----+-----+
207 * 0 | 0 | 1 | 0 | 0 |
208 * 1 | 1 | 0 | 0 | 0 |
209 * 2 | 0 | 0 | 0 | 1 |
210 * 3 | 0 | 0 | 1 | 0 |
211 * ----------+-----+-----+-----+-----+
212 *
213 * Write:
214 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500215 * | ODT |
216 * Write To +-----------------------+
217 * Rank | 3 | 2 | 1 | 0 |
218 * ----------+-----+-----+-----+-----+
219 * 0 | 0 | 1 | 0 | 1 |
220 * 1 | 1 | 0 | 1 | 0 |
221 * 2 | 0 | 1 | 0 | 1 |
222 * 3 | 1 | 0 | 1 | 0 |
223 * ----------+-----+-----+-----+-----+
224 */
225 switch (rank) {
226 case 0:
227 odt_mask_0 = 0x4;
228 odt_mask_1 = 0x5;
229 break;
230 case 1:
231 odt_mask_0 = 0x8;
232 odt_mask_1 = 0xA;
233 break;
234 case 2:
235 odt_mask_0 = 0x1;
236 odt_mask_1 = 0x5;
237 break;
238 case 3:
239 odt_mask_0 = 0x2;
240 odt_mask_1 = 0xA;
241 break;
242 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200243 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500244 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500245 }
246
Marek Vasutb2dfd102015-07-20 08:03:11 +0200247 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
248 ((0xFF & odt_mask_0) << 8) |
249 ((0xFF & odt_mask_1) << 16);
Marek Vasut1273dd92015-07-12 21:05:08 +0200250 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
251 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500252}
253
Marek Vasutc76976d2015-07-12 22:28:33 +0200254/**
255 * scc_mgr_set() - Set SCC Manager register
256 * @off: Base offset in SCC Manager space
257 * @grp: Read/Write group
258 * @val: Value to be set
259 *
260 * This function sets the SCC Manager (Scan Chain Control Manager) register.
261 */
262static void scc_mgr_set(u32 off, u32 grp, u32 val)
263{
264 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
265}
266
Marek Vasute893f4d2015-07-20 07:16:42 +0200267/**
268 * scc_mgr_initialize() - Initialize SCC Manager registers
269 *
270 * Initialize SCC Manager registers.
271 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500272static void scc_mgr_initialize(void)
273{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500274 /*
Marek Vasute893f4d2015-07-20 07:16:42 +0200275 * Clear register file for HPS. 16 (2^4) is the size of the
276 * full register file in the scc mgr:
277 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
278 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500279 */
Marek Vasutc76976d2015-07-12 22:28:33 +0200280 int i;
Marek Vasute893f4d2015-07-20 07:16:42 +0200281
Dinh Nguyen3da42852015-06-02 22:52:49 -0500282 for (i = 0; i < 16; i++) {
Marek Vasut7ac40d22015-06-26 18:56:54 +0200283 debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -0500284 __func__, __LINE__, i);
Marek Vasutc76976d2015-07-12 22:28:33 +0200285 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, 0, i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500286 }
287}
288
Marek Vasut5ff825b2015-07-12 22:11:55 +0200289static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
290{
Marek Vasutc76976d2015-07-12 22:28:33 +0200291 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200292}
293
294static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500295{
Marek Vasutc76976d2015-07-12 22:28:33 +0200296 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500297}
298
Dinh Nguyen3da42852015-06-02 22:52:49 -0500299static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
300{
Marek Vasutc76976d2015-07-12 22:28:33 +0200301 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500302}
303
Marek Vasut5ff825b2015-07-12 22:11:55 +0200304static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
305{
Marek Vasutc76976d2015-07-12 22:28:33 +0200306 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200307}
308
Marek Vasut32675242015-07-17 06:07:13 +0200309static void scc_mgr_set_dqs_io_in_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200310{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200311 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200312 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200313}
314
315static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
316{
Marek Vasutc76976d2015-07-12 22:28:33 +0200317 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200318}
319
320static void scc_mgr_set_dq_out1_delay(uint32_t dq_in_group, uint32_t delay)
321{
Marek Vasutc76976d2015-07-12 22:28:33 +0200322 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200323}
324
Marek Vasut32675242015-07-17 06:07:13 +0200325static void scc_mgr_set_dqs_out1_delay(uint32_t delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200326{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200327 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200328 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200329}
330
331static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
332{
Marek Vasutc76976d2015-07-12 22:28:33 +0200333 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200334 rwcfg->mem_dq_per_write_dqs + 1 + dm,
Marek Vasutc76976d2015-07-12 22:28:33 +0200335 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200336}
337
338/* load up dqs config settings */
339static void scc_mgr_load_dqs(uint32_t dqs)
340{
341 writel(dqs, &sdr_scc_mgr->dqs_ena);
342}
343
344/* load up dqs io config settings */
345static void scc_mgr_load_dqs_io(void)
346{
347 writel(0, &sdr_scc_mgr->dqs_io_ena);
348}
349
350/* load up dq config settings */
351static void scc_mgr_load_dq(uint32_t dq_in_group)
352{
353 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
354}
355
356/* load up dm config settings */
357static void scc_mgr_load_dm(uint32_t dm)
358{
359 writel(dm, &sdr_scc_mgr->dm_ena);
360}
361
Marek Vasut0b69b802015-07-12 23:25:21 +0200362/**
363 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
364 * @off: Base offset in SCC Manager space
365 * @grp: Read/Write group
366 * @val: Value to be set
367 * @update: If non-zero, trigger SCC Manager update for all ranks
368 *
369 * This function sets the SCC Manager (Scan Chain Control Manager) register
370 * and optionally triggers the SCC update for all ranks.
371 */
372static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
373 const int update)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500374{
Marek Vasut0b69b802015-07-12 23:25:21 +0200375 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500376
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200377 for (r = 0; r < rwcfg->mem_number_of_ranks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500378 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut0b69b802015-07-12 23:25:21 +0200379 scc_mgr_set(off, grp, val);
Marek Vasut162d60e2015-07-12 23:14:33 +0200380
Marek Vasut0b69b802015-07-12 23:25:21 +0200381 if (update || (r == 0)) {
382 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasut1273dd92015-07-12 21:05:08 +0200383 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500384 }
385 }
386}
387
Marek Vasut0b69b802015-07-12 23:25:21 +0200388static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
389{
390 /*
391 * USER although the h/w doesn't support different phases per
392 * shadow register, for simplicity our scc manager modeling
393 * keeps different phase settings per shadow reg, and it's
394 * important for us to keep them in sync to match h/w.
395 * for efficiency, the scan chain update should occur only
396 * once to sr0.
397 */
398 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
399 read_group, phase, 0);
400}
401
Dinh Nguyen3da42852015-06-02 22:52:49 -0500402static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
403 uint32_t phase)
404{
Marek Vasut0b69b802015-07-12 23:25:21 +0200405 /*
406 * USER although the h/w doesn't support different phases per
407 * shadow register, for simplicity our scc manager modeling
408 * keeps different phase settings per shadow reg, and it's
409 * important for us to keep them in sync to match h/w.
410 * for efficiency, the scan chain update should occur only
411 * once to sr0.
412 */
413 scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
414 write_group, phase, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500415}
416
Dinh Nguyen3da42852015-06-02 22:52:49 -0500417static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
418 uint32_t delay)
419{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500420 /*
421 * In shadow register mode, the T11 settings are stored in
422 * registers in the core, which are updated by the DQS_ENA
423 * signals. Not issuing the SCC_MGR_UPD command allows us to
424 * save lots of rank switching overhead, by calling
425 * select_shadow_regs_for_update with update_scan_chains
426 * set to 0.
427 */
Marek Vasut0b69b802015-07-12 23:25:21 +0200428 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
429 read_group, delay, 1);
Marek Vasut1273dd92015-07-12 21:05:08 +0200430 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500431}
432
Marek Vasut5be355c2015-07-12 23:39:06 +0200433/**
434 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
435 * @write_group: Write group
436 * @delay: Delay value
437 *
438 * This function sets the OCT output delay in SCC manager.
439 */
440static void scc_mgr_set_oct_out1_delay(const u32 write_group, const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500441{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200442 const int ratio = rwcfg->mem_if_read_dqs_width /
443 rwcfg->mem_if_write_dqs_width;
Marek Vasut5be355c2015-07-12 23:39:06 +0200444 const int base = write_group * ratio;
445 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500446 /*
447 * Load the setting in the SCC manager
448 * Although OCT affects only write data, the OCT delay is controlled
449 * by the DQS logic block which is instantiated once per read group.
450 * For protocols where a write group consists of multiple read groups,
451 * the setting must be set multiple times.
452 */
Marek Vasut5be355c2015-07-12 23:39:06 +0200453 for (i = 0; i < ratio; i++)
454 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500455}
456
Marek Vasut37a37ca2015-07-19 01:32:55 +0200457/**
458 * scc_mgr_set_hhp_extras() - Set HHP extras.
459 *
460 * Load the fixed setting in the SCC manager HHP extras.
461 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500462static void scc_mgr_set_hhp_extras(void)
463{
464 /*
465 * Load the fixed setting in the SCC manager
Marek Vasut37a37ca2015-07-19 01:32:55 +0200466 * bits: 0:0 = 1'b1 - DQS bypass
467 * bits: 1:1 = 1'b1 - DQ bypass
468 * bits: 4:2 = 3'b001 - rfifo_mode
469 * bits: 6:5 = 2'b01 - rfifo clock_select
470 * bits: 7:7 = 1'b0 - separate gating from ungating setting
471 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen3da42852015-06-02 22:52:49 -0500472 */
Marek Vasut37a37ca2015-07-19 01:32:55 +0200473 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
474 (1 << 2) | (1 << 1) | (1 << 0);
475 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
476 SCC_MGR_HHP_GLOBALS_OFFSET |
477 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500478
Marek Vasut37a37ca2015-07-19 01:32:55 +0200479 debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n",
480 __func__, __LINE__);
481 writel(value, addr);
482 debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
483 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500484}
485
Marek Vasutf42af352015-07-20 04:41:53 +0200486/**
487 * scc_mgr_zero_all() - Zero all DQS config
488 *
489 * Zero all DQS config.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500490 */
491static void scc_mgr_zero_all(void)
492{
Marek Vasutf42af352015-07-20 04:41:53 +0200493 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500494
495 /*
496 * USER Zero all DQS config settings, across all groups and all
497 * shadow registers
498 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200499 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf42af352015-07-20 04:41:53 +0200500 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200501 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500502 /*
503 * The phases actually don't exist on a per-rank basis,
504 * but there's no harm updating them several times, so
505 * let's keep the code simple.
506 */
Marek Vasut160695d2015-08-02 19:10:58 +0200507 scc_mgr_set_dqs_bus_in_delay(i, iocfg->dqs_in_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500508 scc_mgr_set_dqs_en_phase(i, 0);
509 scc_mgr_set_dqs_en_delay(i, 0);
510 }
511
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200512 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500513 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasutf42af352015-07-20 04:41:53 +0200514 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200515 scc_mgr_set_oct_out1_delay(i, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500516 }
517 }
518
Marek Vasutf42af352015-07-20 04:41:53 +0200519 /* Multicast to all DQS group enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200520 writel(0xff, &sdr_scc_mgr->dqs_ena);
521 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500522}
523
Marek Vasutc5c5f532015-07-17 02:06:20 +0200524/**
525 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
526 * @write_group: Write group
527 *
528 * Set bypass mode and trigger SCC update.
529 */
530static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500531{
Marek Vasutc5c5f532015-07-17 02:06:20 +0200532 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200533 writel(0xff, &sdr_scc_mgr->dq_ena);
534 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500535
Marek Vasutc5c5f532015-07-17 02:06:20 +0200536 /* Update current DQS IO enable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200537 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500538
Marek Vasutc5c5f532015-07-17 02:06:20 +0200539 /* Update the DQS logic. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200540 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500541
Marek Vasutc5c5f532015-07-17 02:06:20 +0200542 /* Hit update. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200543 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500544}
545
Marek Vasut5e837892015-07-13 00:30:09 +0200546/**
547 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
548 * @write_group: Write group
549 *
550 * Load DQS settings for Write Group, do not trigger SCC update.
551 */
552static void scc_mgr_load_dqs_for_write_group(const u32 write_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200553{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200554 const int ratio = rwcfg->mem_if_read_dqs_width /
555 rwcfg->mem_if_write_dqs_width;
Marek Vasut5e837892015-07-13 00:30:09 +0200556 const int base = write_group * ratio;
557 int i;
Marek Vasut5ff825b2015-07-12 22:11:55 +0200558 /*
Marek Vasut5e837892015-07-13 00:30:09 +0200559 * Load the setting in the SCC manager
Marek Vasut5ff825b2015-07-12 22:11:55 +0200560 * Although OCT affects only write data, the OCT delay is controlled
561 * by the DQS logic block which is instantiated once per read group.
562 * For protocols where a write group consists of multiple read groups,
Marek Vasut5e837892015-07-13 00:30:09 +0200563 * the setting must be set multiple times.
Marek Vasut5ff825b2015-07-12 22:11:55 +0200564 */
Marek Vasut5e837892015-07-13 00:30:09 +0200565 for (i = 0; i < ratio; i++)
566 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200567}
568
Marek Vasutd41ea932015-07-20 08:41:04 +0200569/**
570 * scc_mgr_zero_group() - Zero all configs for a group
571 *
572 * Zero DQ, DM, DQS and OCT configs for a group.
573 */
574static void scc_mgr_zero_group(const u32 write_group, const int out_only)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500575{
Marek Vasutd41ea932015-07-20 08:41:04 +0200576 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500577
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200578 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutd41ea932015-07-20 08:41:04 +0200579 r += NUM_RANKS_PER_SHADOW_REG) {
580 /* Zero all DQ config settings. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200581 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200582 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500583 if (!out_only)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200584 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500585 }
586
Marek Vasutd41ea932015-07-20 08:41:04 +0200587 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200588 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500589
Marek Vasutd41ea932015-07-20 08:41:04 +0200590 /* Zero all DM config settings. */
591 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200592 scc_mgr_set_dm_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500593
Marek Vasutd41ea932015-07-20 08:41:04 +0200594 /* Multicast to all DM enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200595 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500596
Marek Vasutd41ea932015-07-20 08:41:04 +0200597 /* Zero all DQS IO settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500598 if (!out_only)
Marek Vasut32675242015-07-17 06:07:13 +0200599 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasutd41ea932015-07-20 08:41:04 +0200600
601 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200602 scc_mgr_set_dqs_out1_delay(iocfg->dqs_out_reserve);
603 scc_mgr_set_oct_out1_delay(write_group, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500604 scc_mgr_load_dqs_for_write_group(write_group);
605
Marek Vasutd41ea932015-07-20 08:41:04 +0200606 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasut1273dd92015-07-12 21:05:08 +0200607 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500608
Marek Vasutd41ea932015-07-20 08:41:04 +0200609 /* Hit update to zero everything. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200610 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500611 }
612}
613
Dinh Nguyen3da42852015-06-02 22:52:49 -0500614/*
615 * apply and load a particular input delay for the DQ pins in a group
616 * group_bgn is the index of the first dq pin (in the write group)
617 */
Marek Vasut32675242015-07-17 06:07:13 +0200618static void scc_mgr_apply_group_dq_in_delay(uint32_t group_bgn, uint32_t delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500619{
620 uint32_t i, p;
621
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200622 for (i = 0, p = group_bgn; i < rwcfg->mem_dq_per_read_dqs; i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200623 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500624 scc_mgr_load_dq(p);
625 }
626}
627
Marek Vasut300c2e62015-07-17 05:42:49 +0200628/**
629 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the DQ pins in a group
630 * @delay: Delay value
631 *
632 * Apply and load a particular output delay for the DQ pins in a group.
633 */
634static void scc_mgr_apply_group_dq_out1_delay(const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500635{
Marek Vasut300c2e62015-07-17 05:42:49 +0200636 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500637
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200638 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut300c2e62015-07-17 05:42:49 +0200639 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500640 scc_mgr_load_dq(i);
641 }
642}
643
644/* apply and load a particular output delay for the DM pins in a group */
Marek Vasut32675242015-07-17 06:07:13 +0200645static void scc_mgr_apply_group_dm_out1_delay(uint32_t delay1)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500646{
647 uint32_t i;
648
649 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200650 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500651 scc_mgr_load_dm(i);
652 }
653}
654
655
656/* apply and load delay on both DQS and OCT out1 */
657static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
658 uint32_t delay)
659{
Marek Vasut32675242015-07-17 06:07:13 +0200660 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500661 scc_mgr_load_dqs_io();
662
663 scc_mgr_set_oct_out1_delay(write_group, delay);
664 scc_mgr_load_dqs_for_write_group(write_group);
665}
666
Marek Vasut5cb1b502015-07-17 05:33:28 +0200667/**
668 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side: DQ, DM, DQS, OCT
669 * @write_group: Write group
670 * @delay: Delay value
671 *
672 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
673 */
Marek Vasut8eccde32015-07-17 05:30:14 +0200674static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
Marek Vasut8eccde32015-07-17 05:30:14 +0200675 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500676{
Marek Vasut8eccde32015-07-17 05:30:14 +0200677 u32 i, new_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500678
Marek Vasut8eccde32015-07-17 05:30:14 +0200679 /* DQ shift */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200680 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500681 scc_mgr_load_dq(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500682
Marek Vasut8eccde32015-07-17 05:30:14 +0200683 /* DM shift */
684 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500685 scc_mgr_load_dm(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500686
Marek Vasut5cb1b502015-07-17 05:33:28 +0200687 /* DQS shift */
688 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200689 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200690 debug_cond(DLEVEL == 1,
691 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
692 __func__, __LINE__, write_group, delay, new_delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200693 iocfg->io_out2_delay_max,
694 new_delay - iocfg->io_out2_delay_max);
695 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200696 scc_mgr_set_dqs_out1_delay(new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500697 }
698
699 scc_mgr_load_dqs_io();
700
Marek Vasut5cb1b502015-07-17 05:33:28 +0200701 /* OCT shift */
702 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200703 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200704 debug_cond(DLEVEL == 1,
705 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
706 __func__, __LINE__, write_group, delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200707 new_delay, iocfg->io_out2_delay_max,
708 new_delay - iocfg->io_out2_delay_max);
709 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200710 scc_mgr_set_oct_out1_delay(write_group, new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500711 }
712
713 scc_mgr_load_dqs_for_write_group(write_group);
714}
715
Marek Vasutf51a7d32015-07-19 02:18:21 +0200716/**
717 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side to all ranks
718 * @write_group: Write group
719 * @delay: Delay value
720 *
721 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500722 */
Marek Vasutf51a7d32015-07-19 02:18:21 +0200723static void
724scc_mgr_apply_group_all_out_delay_add_all_ranks(const u32 write_group,
725 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500726{
Marek Vasutf51a7d32015-07-19 02:18:21 +0200727 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500728
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200729 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf51a7d32015-07-19 02:18:21 +0200730 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200731 scc_mgr_apply_group_all_out_delay_add(write_group, delay);
Marek Vasut1273dd92015-07-12 21:05:08 +0200732 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500733 }
734}
735
Marek Vasutf936f942015-07-26 11:07:19 +0200736/**
737 * set_jump_as_return() - Return instruction optimization
738 *
739 * Optimization used to recover some slots in ddr3 inst_rom could be
740 * applied to other protocols if we wanted to
741 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500742static void set_jump_as_return(void)
743{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500744 /*
Marek Vasutf936f942015-07-26 11:07:19 +0200745 * To save space, we replace return with jump to special shared
Dinh Nguyen3da42852015-06-02 22:52:49 -0500746 * RETURN instruction so we set the counter to large value so that
Marek Vasutf936f942015-07-26 11:07:19 +0200747 * we always jump.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500748 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200749 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200750 writel(rwcfg->rreturn, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500751}
752
Marek Vasut3de96222015-07-26 11:46:04 +0200753/**
754 * delay_for_n_mem_clocks() - Delay for N memory clocks
755 * @clocks: Length of the delay
756 *
757 * Delay for N memory clocks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500758 */
Marek Vasut90a584b2015-07-26 11:11:28 +0200759static void delay_for_n_mem_clocks(const u32 clocks)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500760{
Marek Vasut90a584b2015-07-26 11:11:28 +0200761 u32 afi_clocks;
Marek Vasut6a39be62015-07-26 11:42:53 +0200762 u16 c_loop;
763 u8 inner;
764 u8 outer;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500765
766 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
767
Marek Vasutcbcaf462015-07-26 11:34:09 +0200768 /* Scale (rounding up) to get afi clocks. */
Marek Vasut96fd4362015-08-02 19:26:55 +0200769 afi_clocks = DIV_ROUND_UP(clocks, misccfg->afi_rate_ratio);
Marek Vasutcbcaf462015-07-26 11:34:09 +0200770 if (afi_clocks) /* Temporary underflow protection */
771 afi_clocks--;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500772
773 /*
Marek Vasut90a584b2015-07-26 11:11:28 +0200774 * Note, we don't bother accounting for being off a little
775 * bit because of a few extra instructions in outer loops.
776 * Note, the loops have a test at the end, and do the test
777 * before the decrement, and so always perform the loop
Dinh Nguyen3da42852015-06-02 22:52:49 -0500778 * 1 time more than the counter value
779 */
Marek Vasut6a39be62015-07-26 11:42:53 +0200780 c_loop = afi_clocks >> 16;
781 outer = c_loop ? 0xff : (afi_clocks >> 8);
782 inner = outer ? 0xff : afi_clocks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500783
784 /*
785 * rom instructions are structured as follows:
786 *
787 * IDLE_LOOP2: jnz cntr0, TARGET_A
788 * IDLE_LOOP1: jnz cntr1, TARGET_B
789 * return
790 *
791 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
792 * TARGET_B is set to IDLE_LOOP2 as well
793 *
794 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
795 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
796 *
797 * a little confusing, but it helps save precious space in the inst_rom
798 * and sequencer rom and keeps the delays more accurate and reduces
799 * overhead
800 */
Marek Vasutcbcaf462015-07-26 11:34:09 +0200801 if (afi_clocks < 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200802 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
803 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500804
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200805 writel(rwcfg->idle_loop1,
Marek Vasut1273dd92015-07-12 21:05:08 +0200806 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500807
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200808 writel(rwcfg->idle_loop1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +0200809 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500810 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200811 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
812 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500813
Marek Vasut1273dd92015-07-12 21:05:08 +0200814 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
815 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500816
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200817 writel(rwcfg->idle_loop2,
Marek Vasut1273dd92015-07-12 21:05:08 +0200818 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500819
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200820 writel(rwcfg->idle_loop2,
Marek Vasut1273dd92015-07-12 21:05:08 +0200821 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500822
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200823 do {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200824 writel(rwcfg->idle_loop2,
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200825 SDR_PHYGRP_RWMGRGRP_ADDRESS |
826 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
827 } while (c_loop-- != 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500828 }
829 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
830}
831
Marek Vasut944fe712015-07-13 00:44:30 +0200832/**
833 * rw_mgr_mem_init_load_regs() - Load instruction registers
834 * @cntr0: Counter 0 value
835 * @cntr1: Counter 1 value
836 * @cntr2: Counter 2 value
837 * @jump: Jump instruction value
838 *
839 * Load instruction registers.
840 */
841static void rw_mgr_mem_init_load_regs(u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
842{
843 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
844 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
845
846 /* Load counters */
847 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
848 &sdr_rw_load_mgr_regs->load_cntr0);
849 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
850 &sdr_rw_load_mgr_regs->load_cntr1);
851 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
852 &sdr_rw_load_mgr_regs->load_cntr2);
853
854 /* Load jump address */
855 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
856 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
857 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
858
859 /* Execute count instruction */
860 writel(jump, grpaddr);
861}
862
Marek Vasutecd23342015-07-13 00:51:05 +0200863/**
864 * rw_mgr_mem_load_user() - Load user calibration values
865 * @fin1: Final instruction 1
866 * @fin2: Final instruction 2
867 * @precharge: If 1, precharge the banks at the end
868 *
869 * Load user calibration values and optionally precharge the banks.
870 */
871static void rw_mgr_mem_load_user(const u32 fin1, const u32 fin2,
872 const int precharge)
873{
874 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
875 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
876 u32 r;
877
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200878 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasutecd23342015-07-13 00:51:05 +0200879 /* set rank */
880 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
881
882 /* precharge all banks ... */
883 if (precharge)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200884 writel(rwcfg->precharge_all, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200885
886 /*
887 * USER Use Mirror-ed commands for odd ranks if address
888 * mirrorring is on
889 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200890 if ((rwcfg->mem_address_mirroring >> r) & 0x1) {
Marek Vasutecd23342015-07-13 00:51:05 +0200891 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200892 writel(rwcfg->mrs2_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200893 delay_for_n_mem_clocks(4);
894 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200895 writel(rwcfg->mrs3_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200896 delay_for_n_mem_clocks(4);
897 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200898 writel(rwcfg->mrs1_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200899 delay_for_n_mem_clocks(4);
900 set_jump_as_return();
901 writel(fin1, grpaddr);
902 } else {
903 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200904 writel(rwcfg->mrs2, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200905 delay_for_n_mem_clocks(4);
906 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200907 writel(rwcfg->mrs3, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200908 delay_for_n_mem_clocks(4);
909 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200910 writel(rwcfg->mrs1, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200911 set_jump_as_return();
912 writel(fin2, grpaddr);
913 }
914
915 if (precharge)
916 continue;
917
918 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200919 writel(rwcfg->zqcl, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200920
921 /* tZQinit = tDLLK = 512 ck cycles */
922 delay_for_n_mem_clocks(512);
923 }
924}
925
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200926/**
927 * rw_mgr_mem_initialize() - Initialize RW Manager
928 *
929 * Initialize RW Manager.
930 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500931static void rw_mgr_mem_initialize(void)
932{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500933 debug("%s:%d\n", __func__, __LINE__);
934
935 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200936 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
937 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500938
939 /*
940 * Here's how you load register for a loop
941 * Counters are located @ 0x800
942 * Jump address are located @ 0xC00
943 * For both, registers 0 to 3 are selected using bits 3 and 2, like
944 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
945 * I know this ain't pretty, but Avalon bus throws away the 2 least
946 * significant bits
947 */
948
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200949 /* Start with memory RESET activated */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500950
951 /* tINIT = 200us */
952
953 /*
954 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
955 * If a and b are the number of iteration in 2 nested loops
956 * it takes the following number of cycles to complete the operation:
957 * number_of_cycles = ((2 + n) * a + 2) * b
958 * where n is the number of instruction in the inner loop
959 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
960 * b = 6A
961 */
Marek Vasut96fd4362015-08-02 19:26:55 +0200962 rw_mgr_mem_init_load_regs(misccfg->tinit_cntr0_val, misccfg->tinit_cntr1_val,
963 misccfg->tinit_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200964 rwcfg->init_reset_0_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500965
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200966 /* Indicate that memory is stable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200967 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500968
969 /*
970 * transition the RESET to high
971 * Wait for 500us
972 */
973
974 /*
975 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
976 * If a and b are the number of iteration in 2 nested loops
977 * it takes the following number of cycles to complete the operation
978 * number_of_cycles = ((2 + n) * a + 2) * b
979 * where n is the number of instruction in the inner loop
980 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
981 * b = FF
982 */
Marek Vasut96fd4362015-08-02 19:26:55 +0200983 rw_mgr_mem_init_load_regs(misccfg->treset_cntr0_val, misccfg->treset_cntr1_val,
984 misccfg->treset_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200985 rwcfg->init_reset_1_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500986
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200987 /* Bring up clock enable. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500988
989 /* tXRP < 250 ck cycles */
990 delay_for_n_mem_clocks(250);
991
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200992 rw_mgr_mem_load_user(rwcfg->mrs0_dll_reset_mirr, rwcfg->mrs0_dll_reset,
Marek Vasutecd23342015-07-13 00:51:05 +0200993 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500994}
995
Marek Vasutf1f22f72015-07-26 10:59:19 +0200996/**
997 * rw_mgr_mem_handoff() - Hand off the memory to user
998 *
999 * At the end of calibration we have to program the user settings in
1000 * and hand off the memory to the user.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001001 */
1002static void rw_mgr_mem_handoff(void)
1003{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001004 rw_mgr_mem_load_user(rwcfg->mrs0_user_mirr, rwcfg->mrs0_user, 1);
Marek Vasutecd23342015-07-13 00:51:05 +02001005 /*
Marek Vasutf1f22f72015-07-26 10:59:19 +02001006 * Need to wait tMOD (12CK or 15ns) time before issuing other
1007 * commands, but we will have plenty of NIOS cycles before actual
1008 * handoff so its okay.
Marek Vasutecd23342015-07-13 00:51:05 +02001009 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001010}
1011
Marek Vasut8371c2e2015-07-21 06:00:36 +02001012/**
1013 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1014 * @group: Write Group
1015 * @use_dm: Use DM
1016 *
1017 * Issue write test command. Two variants are provided, one that just tests
1018 * a write pattern and another that tests datamask functionality.
Marek Vasutad64769c2015-07-21 05:43:37 +02001019 */
Marek Vasut8371c2e2015-07-21 06:00:36 +02001020static void rw_mgr_mem_calibrate_write_test_issue(u32 group,
1021 u32 test_dm)
Marek Vasutad64769c2015-07-21 05:43:37 +02001022{
Marek Vasut8371c2e2015-07-21 06:00:36 +02001023 const u32 quick_write_mode =
1024 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001025 misccfg->enable_super_quick_calibration;
Marek Vasut8371c2e2015-07-21 06:00:36 +02001026 u32 mcc_instruction;
1027 u32 rw_wl_nop_cycles;
Marek Vasutad64769c2015-07-21 05:43:37 +02001028
1029 /*
1030 * Set counter and jump addresses for the right
1031 * number of NOP cycles.
1032 * The number of supported NOP cycles can range from -1 to infinity
1033 * Three different cases are handled:
1034 *
1035 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1036 * mechanism will be used to insert the right number of NOPs
1037 *
1038 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1039 * issuing the write command will jump straight to the
1040 * micro-instruction that turns on DQS (for DDRx), or outputs write
1041 * data (for RLD), skipping
1042 * the NOP micro-instruction all together
1043 *
1044 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1045 * turned on in the same micro-instruction that issues the write
1046 * command. Then we need
1047 * to directly jump to the micro-instruction that sends out the data
1048 *
1049 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1050 * (2 and 3). One jump-counter (0) is used to perform multiple
1051 * write-read operations.
1052 * one counter left to issue this command in "multiple-group" mode
1053 */
1054
1055 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
1056
1057 if (rw_wl_nop_cycles == -1) {
1058 /*
1059 * CNTR 2 - We want to execute the special write operation that
1060 * turns on DQS right away and then skip directly to the
1061 * instruction that sends out the data. We set the counter to a
1062 * large number so that the jump is always taken.
1063 */
1064 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1065
1066 /* CNTR 3 - Not used */
1067 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001068 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0_wl_1;
1069 writel(rwcfg->lfsr_wr_rd_dm_bank_0_data,
Marek Vasutad64769c2015-07-21 05:43:37 +02001070 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001071 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001072 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1073 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001074 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0_wl_1;
1075 writel(rwcfg->lfsr_wr_rd_bank_0_data,
Marek Vasutad64769c2015-07-21 05:43:37 +02001076 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001077 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001078 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1079 }
1080 } else if (rw_wl_nop_cycles == 0) {
1081 /*
1082 * CNTR 2 - We want to skip the NOP operation and go straight
1083 * to the DQS enable instruction. We set the counter to a large
1084 * number so that the jump is always taken.
1085 */
1086 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1087
1088 /* CNTR 3 - Not used */
1089 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001090 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1091 writel(rwcfg->lfsr_wr_rd_dm_bank_0_dqs,
Marek Vasutad64769c2015-07-21 05:43:37 +02001092 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1093 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001094 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1095 writel(rwcfg->lfsr_wr_rd_bank_0_dqs,
Marek Vasutad64769c2015-07-21 05:43:37 +02001096 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1097 }
1098 } else {
1099 /*
1100 * CNTR 2 - In this case we want to execute the next instruction
1101 * and NOT take the jump. So we set the counter to 0. The jump
1102 * address doesn't count.
1103 */
1104 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1105 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1106
1107 /*
1108 * CNTR 3 - Set the nop counter to the number of cycles we
1109 * need to loop for, minus 1.
1110 */
1111 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1112 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001113 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1114 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001115 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1116 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001117 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1118 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001119 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1120 }
1121 }
1122
1123 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1124 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1125
1126 if (quick_write_mode)
1127 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1128 else
1129 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1130
1131 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1132
1133 /*
1134 * CNTR 1 - This is used to ensure enough time elapses
1135 * for read data to come back.
1136 */
1137 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1138
1139 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001140 writel(rwcfg->lfsr_wr_rd_dm_bank_0_wait,
Marek Vasutad64769c2015-07-21 05:43:37 +02001141 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1142 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001143 writel(rwcfg->lfsr_wr_rd_bank_0_wait,
Marek Vasutad64769c2015-07-21 05:43:37 +02001144 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1145 }
1146
Marek Vasut8371c2e2015-07-21 06:00:36 +02001147 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1148 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1149 (group << 2));
Marek Vasutad64769c2015-07-21 05:43:37 +02001150}
1151
Marek Vasut4a82854b2015-07-21 05:57:11 +02001152/**
1153 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple pass
1154 * @rank_bgn: Rank number
1155 * @write_group: Write Group
1156 * @use_dm: Use DM
1157 * @all_correct: All bits must be correct in the mask
1158 * @bit_chk: Resulting bit mask after the test
1159 * @all_ranks: Test all ranks
1160 *
1161 * Test writes, can check for a single bit pass or multiple bit pass.
1162 */
Marek Vasutb9452ea2015-07-21 05:54:39 +02001163static int
1164rw_mgr_mem_calibrate_write_test(const u32 rank_bgn, const u32 write_group,
1165 const u32 use_dm, const u32 all_correct,
1166 u32 *bit_chk, const u32 all_ranks)
Marek Vasutad64769c2015-07-21 05:43:37 +02001167{
Marek Vasutb9452ea2015-07-21 05:54:39 +02001168 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001169 rwcfg->mem_number_of_ranks :
Marek Vasutb9452ea2015-07-21 05:54:39 +02001170 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001171 const u32 shift_ratio = rwcfg->mem_dq_per_write_dqs /
1172 rwcfg->mem_virtual_groups_per_write_dqs;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001173 const u32 correct_mask_vg = param->write_correct_mask_vg;
1174
1175 u32 tmp_bit_chk, base_rw_mgr;
1176 int vg, r;
Marek Vasutad64769c2015-07-21 05:43:37 +02001177
1178 *bit_chk = param->write_correct_mask;
Marek Vasutad64769c2015-07-21 05:43:37 +02001179
1180 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001181 /* Set rank */
Marek Vasutad64769c2015-07-21 05:43:37 +02001182 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1183
1184 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001185 for (vg = rwcfg->mem_virtual_groups_per_write_dqs - 1;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001186 vg >= 0; vg--) {
1187 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutad64769c2015-07-21 05:43:37 +02001188 writel(0, &phy_mgr_cmd->fifo_reset);
1189
Marek Vasutb9452ea2015-07-21 05:54:39 +02001190 rw_mgr_mem_calibrate_write_test_issue(
1191 write_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001192 rwcfg->mem_virtual_groups_per_write_dqs + vg,
Marek Vasutad64769c2015-07-21 05:43:37 +02001193 use_dm);
1194
Marek Vasutb9452ea2015-07-21 05:54:39 +02001195 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1196 tmp_bit_chk <<= shift_ratio;
1197 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasutad64769c2015-07-21 05:43:37 +02001198 }
Marek Vasutb9452ea2015-07-21 05:54:39 +02001199
Marek Vasutad64769c2015-07-21 05:43:37 +02001200 *bit_chk &= tmp_bit_chk;
1201 }
1202
Marek Vasutb9452ea2015-07-21 05:54:39 +02001203 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutad64769c2015-07-21 05:43:37 +02001204 if (all_correct) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001205 debug_cond(DLEVEL == 2,
1206 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1207 write_group, use_dm, *bit_chk,
1208 param->write_correct_mask,
1209 *bit_chk == param->write_correct_mask);
Marek Vasutad64769c2015-07-21 05:43:37 +02001210 return *bit_chk == param->write_correct_mask;
1211 } else {
1212 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutb9452ea2015-07-21 05:54:39 +02001213 debug_cond(DLEVEL == 2,
1214 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1215 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasutad64769c2015-07-21 05:43:37 +02001216 return *bit_chk != 0x00;
1217 }
1218}
1219
Marek Vasutd844c7d2015-07-18 03:55:07 +02001220/**
1221 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1222 * @rank_bgn: Rank number
1223 * @group: Read/Write Group
1224 * @all_ranks: Test all ranks
1225 *
1226 * Performs a guaranteed read on the patterns we are going to use during a
1227 * read test to ensure memory works.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001228 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02001229static int
1230rw_mgr_mem_calibrate_read_test_patterns(const u32 rank_bgn, const u32 group,
1231 const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001232{
Marek Vasutd844c7d2015-07-18 03:55:07 +02001233 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1234 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1235 const u32 addr_offset =
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001236 (group * rwcfg->mem_virtual_groups_per_read_dqs) << 2;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001237 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001238 rwcfg->mem_number_of_ranks :
Marek Vasutd844c7d2015-07-18 03:55:07 +02001239 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001240 const u32 shift_ratio = rwcfg->mem_dq_per_read_dqs /
1241 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001242 const u32 correct_mask_vg = param->read_correct_mask_vg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001243
Marek Vasutd844c7d2015-07-18 03:55:07 +02001244 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1245 int vg, r;
1246 int ret = 0;
1247
1248 bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001249
1250 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutd844c7d2015-07-18 03:55:07 +02001251 /* Set rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001252 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1253
1254 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001255 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001256 writel(rwcfg->guaranteed_read,
Marek Vasut1273dd92015-07-12 21:05:08 +02001257 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001258
Marek Vasut1273dd92015-07-12 21:05:08 +02001259 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001260 writel(rwcfg->guaranteed_read_cont,
Marek Vasut1273dd92015-07-12 21:05:08 +02001261 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001262
1263 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001264 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001265 vg >= 0; vg--) {
1266 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001267 writel(0, &phy_mgr_cmd->fifo_reset);
1268 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1269 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001270 writel(rwcfg->guaranteed_read,
Marek Vasutd844c7d2015-07-18 03:55:07 +02001271 addr + addr_offset + (vg << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001272
Marek Vasut1273dd92015-07-12 21:05:08 +02001273 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001274 tmp_bit_chk <<= shift_ratio;
1275 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001276 }
Marek Vasutd844c7d2015-07-18 03:55:07 +02001277
1278 bit_chk &= tmp_bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001279 }
1280
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001281 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001282
1283 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001284
1285 if (bit_chk != param->read_correct_mask)
1286 ret = -EIO;
1287
1288 debug_cond(DLEVEL == 1,
1289 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1290 __func__, __LINE__, group, bit_chk,
1291 param->read_correct_mask, ret);
1292
1293 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001294}
1295
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001296/**
1297 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read test
1298 * @rank_bgn: Rank number
1299 * @all_ranks: Test all ranks
1300 *
1301 * Load up the patterns we are going to use during a read test.
1302 */
1303static void rw_mgr_mem_calibrate_read_load_patterns(const u32 rank_bgn,
1304 const int all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001305{
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001306 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001307 rwcfg->mem_number_of_ranks :
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001308 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1309 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001310
1311 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001312
Dinh Nguyen3da42852015-06-02 22:52:49 -05001313 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001314 /* set rank */
1315 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1316
1317 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001318 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001319
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001320 writel(rwcfg->guaranteed_write_wait0,
Marek Vasut1273dd92015-07-12 21:05:08 +02001321 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001322
Marek Vasut1273dd92015-07-12 21:05:08 +02001323 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001324
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001325 writel(rwcfg->guaranteed_write_wait1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001326 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001327
Marek Vasut1273dd92015-07-12 21:05:08 +02001328 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001329
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001330 writel(rwcfg->guaranteed_write_wait2,
Marek Vasut1273dd92015-07-12 21:05:08 +02001331 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001332
Marek Vasut1273dd92015-07-12 21:05:08 +02001333 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001334
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001335 writel(rwcfg->guaranteed_write_wait3,
Marek Vasut1273dd92015-07-12 21:05:08 +02001336 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001337
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001338 writel(rwcfg->guaranteed_write, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02001339 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001340 }
1341
1342 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1343}
1344
Marek Vasut783fcf52015-07-20 03:26:05 +02001345/**
1346 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1347 * @rank_bgn: Rank number
1348 * @group: Read/Write group
1349 * @num_tries: Number of retries of the test
1350 * @all_correct: All bits must be correct in the mask
1351 * @bit_chk: Resulting bit mask after the test
1352 * @all_groups: Test all R/W groups
1353 * @all_ranks: Test all ranks
1354 *
1355 * Try a read and see if it returns correct data back. Test has dummy reads
1356 * inserted into the mix used to align DQS enable. Test has more thorough
1357 * checks than the regular read test.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001358 */
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001359static int
1360rw_mgr_mem_calibrate_read_test(const u32 rank_bgn, const u32 group,
1361 const u32 num_tries, const u32 all_correct,
1362 u32 *bit_chk,
1363 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001364{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001365 const u32 rank_end = all_ranks ? rwcfg->mem_number_of_ranks :
Dinh Nguyen3da42852015-06-02 22:52:49 -05001366 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001367 const u32 quick_read_mode =
1368 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001369 misccfg->enable_super_quick_calibration);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001370 u32 correct_mask_vg = param->read_correct_mask_vg;
1371 u32 tmp_bit_chk;
1372 u32 base_rw_mgr;
1373 u32 addr;
1374
1375 int r, vg, ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001376
1377 *bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001378
1379 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001380 /* set rank */
1381 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1382
Marek Vasut1273dd92015-07-12 21:05:08 +02001383 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001384
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001385 writel(rwcfg->read_b2b_wait1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001386 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001387
Marek Vasut1273dd92015-07-12 21:05:08 +02001388 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001389 writel(rwcfg->read_b2b_wait2,
Marek Vasut1273dd92015-07-12 21:05:08 +02001390 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001391
Dinh Nguyen3da42852015-06-02 22:52:49 -05001392 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001393 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001394 /* need at least two (1+1) reads to capture failures */
1395 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001396 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001397 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001398 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001399
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001400 writel(rwcfg->read_b2b,
Marek Vasut1273dd92015-07-12 21:05:08 +02001401 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001402 if (all_groups)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001403 writel(rwcfg->mem_if_read_dqs_width *
1404 rwcfg->mem_virtual_groups_per_read_dqs - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001405 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001406 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001407 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001408
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001409 writel(rwcfg->read_b2b,
Marek Vasut1273dd92015-07-12 21:05:08 +02001410 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001411
1412 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001413 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1; vg >= 0;
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001414 vg--) {
Marek Vasutba522c72015-07-19 07:57:28 +02001415 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001416 writel(0, &phy_mgr_cmd->fifo_reset);
1417 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1418 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001419
Marek Vasutba522c72015-07-19 07:57:28 +02001420 if (all_groups) {
1421 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1422 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1423 } else {
1424 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1425 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1426 }
Marek Vasutc4815f72015-07-12 19:03:33 +02001427
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001428 writel(rwcfg->read_b2b, addr +
1429 ((group * rwcfg->mem_virtual_groups_per_read_dqs +
Dinh Nguyen3da42852015-06-02 22:52:49 -05001430 vg) << 2));
1431
Marek Vasut1273dd92015-07-12 21:05:08 +02001432 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001433 tmp_bit_chk <<= rwcfg->mem_dq_per_read_dqs /
1434 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutba522c72015-07-19 07:57:28 +02001435 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001436 }
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001437
Dinh Nguyen3da42852015-06-02 22:52:49 -05001438 *bit_chk &= tmp_bit_chk;
1439 }
1440
Marek Vasutc4815f72015-07-12 19:03:33 +02001441 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001442 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001443
Marek Vasut3853d652015-07-19 07:44:21 +02001444 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1445
Dinh Nguyen3da42852015-06-02 22:52:49 -05001446 if (all_correct) {
Marek Vasut3853d652015-07-19 07:44:21 +02001447 ret = (*bit_chk == param->read_correct_mask);
1448 debug_cond(DLEVEL == 2,
1449 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1450 __func__, __LINE__, group, all_groups, *bit_chk,
1451 param->read_correct_mask, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001452 } else {
Marek Vasut3853d652015-07-19 07:44:21 +02001453 ret = (*bit_chk != 0x00);
1454 debug_cond(DLEVEL == 2,
1455 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1456 __func__, __LINE__, group, all_groups, *bit_chk,
1457 0, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001458 }
Marek Vasut3853d652015-07-19 07:44:21 +02001459
1460 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001461}
1462
Marek Vasut96df6032015-07-19 07:35:36 +02001463/**
1464 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1465 * @grp: Read/Write group
1466 * @num_tries: Number of retries of the test
1467 * @all_correct: All bits must be correct in the mask
1468 * @all_groups: Test all R/W groups
1469 *
1470 * Perform a READ test across all memory ranks.
1471 */
1472static int
1473rw_mgr_mem_calibrate_read_test_all_ranks(const u32 grp, const u32 num_tries,
1474 const u32 all_correct,
1475 const u32 all_groups)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001476{
Marek Vasut96df6032015-07-19 07:35:36 +02001477 u32 bit_chk;
1478 return rw_mgr_mem_calibrate_read_test(0, grp, num_tries, all_correct,
1479 &bit_chk, all_groups, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001480}
1481
Marek Vasut60bb8a82015-07-19 06:25:27 +02001482/**
1483 * rw_mgr_incr_vfifo() - Increase VFIFO value
1484 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001485 *
1486 * Increase VFIFO value.
1487 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001488static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001489{
Marek Vasut1273dd92015-07-12 21:05:08 +02001490 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001491}
1492
Marek Vasut60bb8a82015-07-19 06:25:27 +02001493/**
1494 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1495 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001496 *
1497 * Decrease VFIFO value.
1498 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001499static void rw_mgr_decr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001500{
Marek Vasut60bb8a82015-07-19 06:25:27 +02001501 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001502
Marek Vasut96fd4362015-08-02 19:26:55 +02001503 for (i = 0; i < misccfg->read_valid_fifo_size - 1; i++)
Marek Vasut8c887b62015-07-19 06:37:51 +02001504 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001505}
1506
Marek Vasutd145ca92015-07-19 06:45:43 +02001507/**
1508 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1509 * @grp: Read/Write group
1510 *
1511 * Push VFIFO until a failing read happens.
1512 */
1513static int find_vfifo_failing_read(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001514{
Marek Vasut96df6032015-07-19 07:35:36 +02001515 u32 v, ret, fail_cnt = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001516
Marek Vasut96fd4362015-08-02 19:26:55 +02001517 for (v = 0; v < misccfg->read_valid_fifo_size; v++) {
Marek Vasutd145ca92015-07-19 06:45:43 +02001518 debug_cond(DLEVEL == 2, "%s:%d: vfifo %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001519 __func__, __LINE__, v);
Marek Vasutd145ca92015-07-19 06:45:43 +02001520 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001521 PASS_ONE_BIT, 0);
Marek Vasutd145ca92015-07-19 06:45:43 +02001522 if (!ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001523 fail_cnt++;
1524
1525 if (fail_cnt == 2)
Marek Vasutd145ca92015-07-19 06:45:43 +02001526 return v;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001527 }
1528
Marek Vasutd145ca92015-07-19 06:45:43 +02001529 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001530 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001531 }
1532
Marek Vasutd145ca92015-07-19 06:45:43 +02001533 /* No failing read found! Something must have gone wrong. */
1534 debug_cond(DLEVEL == 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
1535 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001536}
1537
Marek Vasut192d6f92015-07-19 05:26:49 +02001538/**
Marek Vasut52e8f212015-07-19 07:27:06 +02001539 * sdr_find_phase_delay() - Find DQS enable phase or delay
1540 * @working: If 1, look for working phase/delay, if 0, look for non-working
1541 * @delay: If 1, look for delay, if 0, look for phase
1542 * @grp: Read/Write group
1543 * @work: Working window position
1544 * @work_inc: Working window increment
1545 * @pd: DQS Phase/Delay Iterator
1546 *
1547 * Find working or non-working DQS enable phase setting.
1548 */
1549static int sdr_find_phase_delay(int working, int delay, const u32 grp,
1550 u32 *work, const u32 work_inc, u32 *pd)
1551{
Marek Vasut160695d2015-08-02 19:10:58 +02001552 const u32 max = delay ? iocfg->dqs_en_delay_max : iocfg->dqs_en_phase_max;
Marek Vasut96df6032015-07-19 07:35:36 +02001553 u32 ret;
Marek Vasut52e8f212015-07-19 07:27:06 +02001554
1555 for (; *pd <= max; (*pd)++) {
1556 if (delay)
1557 scc_mgr_set_dqs_en_delay_all_ranks(grp, *pd);
1558 else
1559 scc_mgr_set_dqs_en_phase_all_ranks(grp, *pd);
1560
1561 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001562 PASS_ONE_BIT, 0);
Marek Vasut52e8f212015-07-19 07:27:06 +02001563 if (!working)
1564 ret = !ret;
1565
1566 if (ret)
1567 return 0;
1568
1569 if (work)
1570 *work += work_inc;
1571 }
1572
1573 return -EINVAL;
1574}
1575/**
Marek Vasut192d6f92015-07-19 05:26:49 +02001576 * sdr_find_phase() - Find DQS enable phase
1577 * @working: If 1, look for working phase, if 0, look for non-working phase
1578 * @grp: Read/Write group
Marek Vasut192d6f92015-07-19 05:26:49 +02001579 * @work: Working window position
1580 * @i: Iterator
1581 * @p: DQS Phase Iterator
Marek Vasut192d6f92015-07-19 05:26:49 +02001582 *
1583 * Find working or non-working DQS enable phase setting.
1584 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001585static int sdr_find_phase(int working, const u32 grp, u32 *work,
Marek Vasut86a39dc2015-07-19 05:35:40 +02001586 u32 *i, u32 *p)
Marek Vasut192d6f92015-07-19 05:26:49 +02001587{
Marek Vasut96fd4362015-08-02 19:26:55 +02001588 const u32 end = misccfg->read_valid_fifo_size + (working ? 0 : 1);
Marek Vasut52e8f212015-07-19 07:27:06 +02001589 int ret;
Marek Vasut192d6f92015-07-19 05:26:49 +02001590
1591 for (; *i < end; (*i)++) {
1592 if (working)
1593 *p = 0;
1594
Marek Vasut52e8f212015-07-19 07:27:06 +02001595 ret = sdr_find_phase_delay(working, 0, grp, work,
Marek Vasut160695d2015-08-02 19:10:58 +02001596 iocfg->delay_per_opa_tap, p);
Marek Vasut52e8f212015-07-19 07:27:06 +02001597 if (!ret)
1598 return 0;
Marek Vasut192d6f92015-07-19 05:26:49 +02001599
Marek Vasut160695d2015-08-02 19:10:58 +02001600 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001601 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001602 rw_mgr_incr_vfifo(grp);
Marek Vasut192d6f92015-07-19 05:26:49 +02001603 if (!working)
1604 *p = 0;
1605 }
1606 }
1607
1608 return -EINVAL;
1609}
1610
Marek Vasut4c5e5842015-07-19 06:04:00 +02001611/**
1612 * sdr_working_phase() - Find working DQS enable phase
1613 * @grp: Read/Write group
1614 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001615 * @d: dtaps output value
1616 * @p: DQS Phase Iterator
1617 * @i: Iterator
1618 *
1619 * Find working DQS enable phase setting.
1620 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001621static int sdr_working_phase(const u32 grp, u32 *work_bgn, u32 *d,
Marek Vasut4c5e5842015-07-19 06:04:00 +02001622 u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001623{
Marek Vasut160695d2015-08-02 19:10:58 +02001624 const u32 dtaps_per_ptap = iocfg->delay_per_opa_tap /
1625 iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasut192d6f92015-07-19 05:26:49 +02001626 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001627
Marek Vasut192d6f92015-07-19 05:26:49 +02001628 *work_bgn = 0;
1629
1630 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1631 *i = 0;
Marek Vasut521fe392015-07-19 04:34:12 +02001632 scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
Marek Vasut8c887b62015-07-19 06:37:51 +02001633 ret = sdr_find_phase(1, grp, work_bgn, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001634 if (!ret)
1635 return 0;
Marek Vasut160695d2015-08-02 19:10:58 +02001636 *work_bgn += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001637 }
1638
Marek Vasut38ed6922015-07-19 05:01:12 +02001639 /* Cannot find working solution */
Marek Vasut192d6f92015-07-19 05:26:49 +02001640 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
1641 __func__, __LINE__);
1642 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001643}
1644
Marek Vasut4c5e5842015-07-19 06:04:00 +02001645/**
1646 * sdr_backup_phase() - Find DQS enable backup phase
1647 * @grp: Read/Write group
1648 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001649 * @p: DQS Phase Iterator
1650 *
1651 * Find DQS enable backup phase setting.
1652 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001653static void sdr_backup_phase(const u32 grp, u32 *work_bgn, u32 *p)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001654{
Marek Vasut96df6032015-07-19 07:35:36 +02001655 u32 tmp_delay, d;
Marek Vasut4c5e5842015-07-19 06:04:00 +02001656 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001657
1658 /* Special case code for backing up a phase */
1659 if (*p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001660 *p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001661 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001662 } else {
1663 (*p)--;
1664 }
Marek Vasut160695d2015-08-02 19:10:58 +02001665 tmp_delay = *work_bgn - iocfg->delay_per_opa_tap;
Marek Vasut521fe392015-07-19 04:34:12 +02001666 scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001667
Marek Vasut160695d2015-08-02 19:10:58 +02001668 for (d = 0; d <= iocfg->dqs_en_delay_max && tmp_delay < *work_bgn; d++) {
Marek Vasut49891df62015-07-19 05:48:30 +02001669 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001670
Marek Vasut4c5e5842015-07-19 06:04:00 +02001671 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001672 PASS_ONE_BIT, 0);
Marek Vasut4c5e5842015-07-19 06:04:00 +02001673 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001674 *work_bgn = tmp_delay;
1675 break;
1676 }
Marek Vasut49891df62015-07-19 05:48:30 +02001677
Marek Vasut160695d2015-08-02 19:10:58 +02001678 tmp_delay += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001679 }
1680
Marek Vasut4c5e5842015-07-19 06:04:00 +02001681 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001682 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001683 if (*p > iocfg->dqs_en_phase_max) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001684 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001685 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001686 }
1687
Marek Vasut521fe392015-07-19 04:34:12 +02001688 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001689}
1690
Marek Vasut4c5e5842015-07-19 06:04:00 +02001691/**
1692 * sdr_nonworking_phase() - Find non-working DQS enable phase
1693 * @grp: Read/Write group
1694 * @work_end: Working window end position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001695 * @p: DQS Phase Iterator
1696 * @i: Iterator
1697 *
1698 * Find non-working DQS enable phase setting.
1699 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001700static int sdr_nonworking_phase(const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001701{
Marek Vasut192d6f92015-07-19 05:26:49 +02001702 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001703
1704 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001705 *work_end += iocfg->delay_per_opa_tap;
1706 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001707 /* Fiddle with FIFO. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001708 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001709 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001710 }
1711
Marek Vasut8c887b62015-07-19 06:37:51 +02001712 ret = sdr_find_phase(0, grp, work_end, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001713 if (ret) {
1714 /* Cannot see edge of failing read. */
1715 debug_cond(DLEVEL == 2, "%s:%d: end: failed\n",
1716 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001717 }
1718
Marek Vasut192d6f92015-07-19 05:26:49 +02001719 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001720}
1721
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001722/**
1723 * sdr_find_window_center() - Find center of the working DQS window.
1724 * @grp: Read/Write group
1725 * @work_bgn: First working settings
1726 * @work_end: Last working settings
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001727 *
1728 * Find center of the working DQS enable window.
1729 */
1730static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
Marek Vasut8c887b62015-07-19 06:37:51 +02001731 const u32 work_end)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001732{
Marek Vasut96df6032015-07-19 07:35:36 +02001733 u32 work_mid;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001734 int tmp_delay = 0;
Marek Vasut28fd2422015-07-19 02:56:59 +02001735 int i, p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001736
Marek Vasut28fd2422015-07-19 02:56:59 +02001737 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001738
1739 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasut28fd2422015-07-19 02:56:59 +02001740 work_bgn, work_end, work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001741 /* Get the middle delay to be less than a VFIFO delay */
Marek Vasut160695d2015-08-02 19:10:58 +02001742 tmp_delay = (iocfg->dqs_en_phase_max + 1) * iocfg->delay_per_opa_tap;
Marek Vasut28fd2422015-07-19 02:56:59 +02001743
Dinh Nguyen3da42852015-06-02 22:52:49 -05001744 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001745 work_mid %= tmp_delay;
Marek Vasut28fd2422015-07-19 02:56:59 +02001746 debug_cond(DLEVEL == 2, "new work_mid %d\n", work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001747
Marek Vasut160695d2015-08-02 19:10:58 +02001748 tmp_delay = rounddown(work_mid, iocfg->delay_per_opa_tap);
1749 if (tmp_delay > iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap)
1750 tmp_delay = iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap;
1751 p = tmp_delay / iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001752
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001753 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
1754
Marek Vasut160695d2015-08-02 19:10:58 +02001755 d = DIV_ROUND_UP(work_mid - tmp_delay, iocfg->delay_per_dqs_en_dchain_tap);
1756 if (d > iocfg->dqs_en_delay_max)
1757 d = iocfg->dqs_en_delay_max;
1758 tmp_delay += d * iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001759
Marek Vasut28fd2422015-07-19 02:56:59 +02001760 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
1761
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001762 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
Marek Vasut28fd2422015-07-19 02:56:59 +02001763 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001764
1765 /*
1766 * push vfifo until we can successfully calibrate. We can do this
1767 * because the largest possible margin in 1 VFIFO cycle.
1768 */
Marek Vasut96fd4362015-08-02 19:26:55 +02001769 for (i = 0; i < misccfg->read_valid_fifo_size; i++) {
Marek Vasut8c887b62015-07-19 06:37:51 +02001770 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center\n");
Marek Vasut28fd2422015-07-19 02:56:59 +02001771 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05001772 PASS_ONE_BIT,
Marek Vasut96df6032015-07-19 07:35:36 +02001773 0)) {
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001774 debug_cond(DLEVEL == 2,
Marek Vasut8c887b62015-07-19 06:37:51 +02001775 "%s:%d center: found: ptap=%u dtap=%u\n",
1776 __func__, __LINE__, p, d);
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001777 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001778 }
1779
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001780 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001781 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001782 }
1783
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001784 debug_cond(DLEVEL == 2, "%s:%d center: failed.\n",
1785 __func__, __LINE__);
1786 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001787}
1788
Marek Vasut33756892015-07-20 09:11:09 +02001789/**
1790 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to use
1791 * @grp: Read/Write Group
1792 *
1793 * Find a good DQS enable to use.
1794 */
Marek Vasut914546e2015-07-20 09:20:42 +02001795static int rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001796{
Marek Vasut57355402015-07-20 09:20:20 +02001797 u32 d, p, i;
1798 u32 dtaps_per_ptap;
1799 u32 work_bgn, work_end;
1800 u32 found_passing_read, found_failing_read, initial_failing_dtap;
1801 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001802
1803 debug("%s:%d %u\n", __func__, __LINE__, grp);
1804
1805 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1806
1807 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1808 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1809
Marek Vasut2f3589c2015-07-19 02:42:21 +02001810 /* Step 0: Determine number of delay taps for each phase tap. */
Marek Vasut160695d2015-08-02 19:10:58 +02001811 dtaps_per_ptap = iocfg->delay_per_opa_tap / iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001812
Marek Vasut2f3589c2015-07-19 02:42:21 +02001813 /* Step 1: First push vfifo until we get a failing read. */
Marek Vasutd145ca92015-07-19 06:45:43 +02001814 find_vfifo_failing_read(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001815
Marek Vasut2f3589c2015-07-19 02:42:21 +02001816 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001817 work_bgn = 0;
Marek Vasut914546e2015-07-20 09:20:42 +02001818 ret = sdr_working_phase(grp, &work_bgn, &d, &p, &i);
1819 if (ret)
1820 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001821
1822 work_end = work_bgn;
1823
1824 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001825 * If d is 0 then the working window covers a phase tap and we can
1826 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen3da42852015-06-02 22:52:49 -05001827 * and we need to increment the dtaps until we find the end.
1828 */
1829 if (d == 0) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001830 /*
1831 * Step 3a: If we have room, back off by one and
1832 * increment in dtaps.
1833 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001834 sdr_backup_phase(grp, &work_bgn, &p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001835
Marek Vasut2f3589c2015-07-19 02:42:21 +02001836 /*
1837 * Step 4a: go forward from working phase to non working
1838 * phase, increment in ptaps.
1839 */
Marek Vasut914546e2015-07-20 09:20:42 +02001840 ret = sdr_nonworking_phase(grp, &work_end, &p, &i);
1841 if (ret)
1842 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001843
Marek Vasut2f3589c2015-07-19 02:42:21 +02001844 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001845
1846 /* Special case code for backing up a phase */
1847 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001848 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001849 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001850 } else {
1851 p = p - 1;
1852 }
1853
Marek Vasut160695d2015-08-02 19:10:58 +02001854 work_end -= iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001855 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1856
Dinh Nguyen3da42852015-06-02 22:52:49 -05001857 d = 0;
1858
Marek Vasut2f3589c2015-07-19 02:42:21 +02001859 debug_cond(DLEVEL == 2, "%s:%d p: ptap=%u\n",
1860 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001861 }
1862
Marek Vasut2f3589c2015-07-19 02:42:21 +02001863 /* The dtap increment to find the failing edge is done here. */
Marek Vasut52e8f212015-07-19 07:27:06 +02001864 sdr_find_phase_delay(0, 1, grp, &work_end,
Marek Vasut160695d2015-08-02 19:10:58 +02001865 iocfg->delay_per_dqs_en_dchain_tap, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001866
1867 /* Go back to working dtap */
1868 if (d != 0)
Marek Vasut160695d2015-08-02 19:10:58 +02001869 work_end -= iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001870
Marek Vasut2f3589c2015-07-19 02:42:21 +02001871 debug_cond(DLEVEL == 2,
1872 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1873 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001874
1875 if (work_end < work_bgn) {
1876 /* nil range */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001877 debug_cond(DLEVEL == 2, "%s:%d end-2: failed\n",
1878 __func__, __LINE__);
Marek Vasut914546e2015-07-20 09:20:42 +02001879 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001880 }
1881
Marek Vasut2f3589c2015-07-19 02:42:21 +02001882 debug_cond(DLEVEL == 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001883 __func__, __LINE__, work_bgn, work_end);
1884
Dinh Nguyen3da42852015-06-02 22:52:49 -05001885 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001886 * We need to calculate the number of dtaps that equal a ptap.
1887 * To do that we'll back up a ptap and re-find the edge of the
1888 * window using dtaps
Dinh Nguyen3da42852015-06-02 22:52:49 -05001889 */
Marek Vasut2f3589c2015-07-19 02:42:21 +02001890 debug_cond(DLEVEL == 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
1891 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001892
1893 /* Special case code for backing up a phase */
1894 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001895 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001896 rw_mgr_decr_vfifo(grp);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001897 debug_cond(DLEVEL == 2, "%s:%d backedup cycle/phase: p=%u\n",
1898 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001899 } else {
1900 p = p - 1;
Marek Vasut2f3589c2015-07-19 02:42:21 +02001901 debug_cond(DLEVEL == 2, "%s:%d backedup phase only: p=%u",
1902 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001903 }
1904
1905 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1906
1907 /*
1908 * Increase dtap until we first see a passing read (in case the
Marek Vasut2f3589c2015-07-19 02:42:21 +02001909 * window is smaller than a ptap), and then a failing read to
1910 * mark the edge of the window again.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001911 */
1912
Marek Vasut2f3589c2015-07-19 02:42:21 +02001913 /* Find a passing read. */
1914 debug_cond(DLEVEL == 2, "%s:%d find passing read\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001915 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001916
Dinh Nguyen3da42852015-06-02 22:52:49 -05001917 initial_failing_dtap = d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001918
Marek Vasut52e8f212015-07-19 07:27:06 +02001919 found_passing_read = !sdr_find_phase_delay(1, 1, grp, NULL, 0, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001920 if (found_passing_read) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001921 /* Find a failing read. */
1922 debug_cond(DLEVEL == 2, "%s:%d find failing read\n",
1923 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001924 d++;
1925 found_failing_read = !sdr_find_phase_delay(0, 1, grp, NULL, 0,
1926 &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001927 } else {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001928 debug_cond(DLEVEL == 1,
1929 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1930 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001931 }
1932
1933 /*
1934 * The dynamically calculated dtaps_per_ptap is only valid if we
1935 * found a passing/failing read. If we didn't, it means d hit the max
Marek Vasut160695d2015-08-02 19:10:58 +02001936 * (iocfg->dqs_en_delay_max). Otherwise, dtaps_per_ptap retains its
Dinh Nguyen3da42852015-06-02 22:52:49 -05001937 * statically calculated value.
1938 */
1939 if (found_passing_read && found_failing_read)
1940 dtaps_per_ptap = d - initial_failing_dtap;
1941
Marek Vasut1273dd92015-07-12 21:05:08 +02001942 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasut2f3589c2015-07-19 02:42:21 +02001943 debug_cond(DLEVEL == 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
1944 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001945
Marek Vasut2f3589c2015-07-19 02:42:21 +02001946 /* Step 6: Find the centre of the window. */
Marek Vasut914546e2015-07-20 09:20:42 +02001947 ret = sdr_find_window_center(grp, work_bgn, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001948
Marek Vasut914546e2015-07-20 09:20:42 +02001949 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001950}
1951
Marek Vasutc4907892015-07-13 02:11:02 +02001952/**
Marek Vasut901dc362015-07-13 02:48:34 +02001953 * search_stop_check() - Check if the detected edge is valid
1954 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1955 * @d: DQS delay
1956 * @rank_bgn: Rank number
1957 * @write_group: Write Group
1958 * @read_group: Read Group
1959 * @bit_chk: Resulting bit mask after the test
1960 * @sticky_bit_chk: Resulting sticky bit mask after the test
1961 * @use_read_test: Perform read test
1962 *
1963 * Test if the found edge is valid.
1964 */
1965static u32 search_stop_check(const int write, const int d, const int rank_bgn,
1966 const u32 write_group, const u32 read_group,
1967 u32 *bit_chk, u32 *sticky_bit_chk,
1968 const u32 use_read_test)
1969{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001970 const u32 ratio = rwcfg->mem_if_read_dqs_width /
1971 rwcfg->mem_if_write_dqs_width;
Marek Vasut901dc362015-07-13 02:48:34 +02001972 const u32 correct_mask = write ? param->write_correct_mask :
1973 param->read_correct_mask;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001974 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
1975 rwcfg->mem_dq_per_read_dqs;
Marek Vasut901dc362015-07-13 02:48:34 +02001976 u32 ret;
1977 /*
1978 * Stop searching when the read test doesn't pass AND when
1979 * we've seen a passing read on every bit.
1980 */
1981 if (write) { /* WRITE-ONLY */
1982 ret = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1983 0, PASS_ONE_BIT,
1984 bit_chk, 0);
1985 } else if (use_read_test) { /* READ-ONLY */
1986 ret = !rw_mgr_mem_calibrate_read_test(rank_bgn, read_group,
1987 NUM_READ_PB_TESTS,
1988 PASS_ONE_BIT, bit_chk,
1989 0, 0);
1990 } else { /* READ-ONLY */
1991 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 0,
1992 PASS_ONE_BIT, bit_chk, 0);
1993 *bit_chk = *bit_chk >> (per_dqs *
1994 (read_group - (write_group * ratio)));
1995 ret = (*bit_chk == 0);
1996 }
1997 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
1998 ret = ret && (*sticky_bit_chk == correct_mask);
1999 debug_cond(DLEVEL == 2,
2000 "%s:%d center(left): dtap=%u => %u == %u && %u",
2001 __func__, __LINE__, d,
2002 *sticky_bit_chk, correct_mask, ret);
2003 return ret;
2004}
2005
2006/**
Marek Vasut71120772015-07-13 02:38:15 +02002007 * search_left_edge() - Find left edge of DQ/DQS working phase
2008 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2009 * @rank_bgn: Rank number
2010 * @write_group: Write Group
2011 * @read_group: Read Group
2012 * @test_bgn: Rank number to begin the test
Marek Vasut71120772015-07-13 02:38:15 +02002013 * @sticky_bit_chk: Resulting sticky bit mask after the test
2014 * @left_edge: Left edge of the DQ/DQS phase
2015 * @right_edge: Right edge of the DQ/DQS phase
2016 * @use_read_test: Perform read test
2017 *
2018 * Find left edge of DQ/DQS working phase.
2019 */
2020static void search_left_edge(const int write, const int rank_bgn,
2021 const u32 write_group, const u32 read_group, const u32 test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002022 u32 *sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002023 int *left_edge, int *right_edge, const u32 use_read_test)
2024{
Marek Vasut160695d2015-08-02 19:10:58 +02002025 const u32 delay_max = write ? iocfg->io_out1_delay_max : iocfg->io_in_delay_max;
2026 const u32 dqs_max = write ? iocfg->io_out1_delay_max : iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002027 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2028 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002029 u32 stop, bit_chk;
Marek Vasut71120772015-07-13 02:38:15 +02002030 int i, d;
2031
2032 for (d = 0; d <= dqs_max; d++) {
2033 if (write)
2034 scc_mgr_apply_group_dq_out1_delay(d);
2035 else
2036 scc_mgr_apply_group_dq_in_delay(test_bgn, d);
2037
2038 writel(0, &sdr_scc_mgr->update);
2039
Marek Vasut901dc362015-07-13 02:48:34 +02002040 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002041 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002042 use_read_test);
Marek Vasut71120772015-07-13 02:38:15 +02002043 if (stop == 1)
2044 break;
2045
2046 /* stop != 1 */
2047 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002048 if (bit_chk & 1) {
Marek Vasut71120772015-07-13 02:38:15 +02002049 /*
2050 * Remember a passing test as
2051 * the left_edge.
2052 */
2053 left_edge[i] = d;
2054 } else {
2055 /*
2056 * If a left edge has not been seen
2057 * yet, then a future passing test
2058 * will mark this edge as the right
2059 * edge.
2060 */
2061 if (left_edge[i] == delay_max + 1)
2062 right_edge[i] = -(d + 1);
2063 }
Marek Vasut0c4be192015-07-18 20:34:00 +02002064 bit_chk >>= 1;
Marek Vasut71120772015-07-13 02:38:15 +02002065 }
2066 }
2067
2068 /* Reset DQ delay chains to 0 */
2069 if (write)
2070 scc_mgr_apply_group_dq_out1_delay(0);
2071 else
2072 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2073
2074 *sticky_bit_chk = 0;
2075 for (i = per_dqs - 1; i >= 0; i--) {
2076 debug_cond(DLEVEL == 2,
2077 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2078 __func__, __LINE__, i, left_edge[i],
2079 i, right_edge[i]);
2080
2081 /*
2082 * Check for cases where we haven't found the left edge,
2083 * which makes our assignment of the the right edge invalid.
2084 * Reset it to the illegal value.
2085 */
2086 if ((left_edge[i] == delay_max + 1) &&
2087 (right_edge[i] != delay_max + 1)) {
2088 right_edge[i] = delay_max + 1;
2089 debug_cond(DLEVEL == 2,
2090 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2091 __func__, __LINE__, i, right_edge[i]);
2092 }
2093
2094 /*
2095 * Reset sticky bit
2096 * READ: except for bits where we have seen both
2097 * the left and right edge.
2098 * WRITE: except for bits where we have seen the
2099 * left edge.
2100 */
2101 *sticky_bit_chk <<= 1;
2102 if (write) {
2103 if (left_edge[i] != delay_max + 1)
2104 *sticky_bit_chk |= 1;
2105 } else {
2106 if ((left_edge[i] != delay_max + 1) &&
2107 (right_edge[i] != delay_max + 1))
2108 *sticky_bit_chk |= 1;
2109 }
2110 }
2111
2112
2113}
2114
2115/**
Marek Vasutc4907892015-07-13 02:11:02 +02002116 * search_right_edge() - Find right edge of DQ/DQS working phase
2117 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2118 * @rank_bgn: Rank number
2119 * @write_group: Write Group
2120 * @read_group: Read Group
2121 * @start_dqs: DQS start phase
2122 * @start_dqs_en: DQS enable start phase
Marek Vasutc4907892015-07-13 02:11:02 +02002123 * @sticky_bit_chk: Resulting sticky bit mask after the test
2124 * @left_edge: Left edge of the DQ/DQS phase
2125 * @right_edge: Right edge of the DQ/DQS phase
2126 * @use_read_test: Perform read test
2127 *
2128 * Find right edge of DQ/DQS working phase.
2129 */
2130static int search_right_edge(const int write, const int rank_bgn,
2131 const u32 write_group, const u32 read_group,
2132 const int start_dqs, const int start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002133 u32 *sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002134 int *left_edge, int *right_edge, const u32 use_read_test)
2135{
Marek Vasut160695d2015-08-02 19:10:58 +02002136 const u32 delay_max = write ? iocfg->io_out1_delay_max : iocfg->io_in_delay_max;
2137 const u32 dqs_max = write ? iocfg->io_out1_delay_max : iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002138 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2139 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002140 u32 stop, bit_chk;
Marek Vasutc4907892015-07-13 02:11:02 +02002141 int i, d;
2142
2143 for (d = 0; d <= dqs_max - start_dqs; d++) {
2144 if (write) { /* WRITE-ONLY */
2145 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2146 d + start_dqs);
2147 } else { /* READ-ONLY */
2148 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002149 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasutc4907892015-07-13 02:11:02 +02002150 uint32_t delay = d + start_dqs_en;
Marek Vasut160695d2015-08-02 19:10:58 +02002151 if (delay > iocfg->dqs_en_delay_max)
2152 delay = iocfg->dqs_en_delay_max;
Marek Vasutc4907892015-07-13 02:11:02 +02002153 scc_mgr_set_dqs_en_delay(read_group, delay);
2154 }
2155 scc_mgr_load_dqs(read_group);
2156 }
2157
2158 writel(0, &sdr_scc_mgr->update);
2159
Marek Vasut901dc362015-07-13 02:48:34 +02002160 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002161 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002162 use_read_test);
Marek Vasutc4907892015-07-13 02:11:02 +02002163 if (stop == 1) {
2164 if (write && (d == 0)) { /* WRITE-ONLY */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002165 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasutc4907892015-07-13 02:11:02 +02002166 /*
2167 * d = 0 failed, but it passed when
2168 * testing the left edge, so it must be
2169 * marginal, set it to -1
2170 */
2171 if (right_edge[i] == delay_max + 1 &&
2172 left_edge[i] != delay_max + 1)
2173 right_edge[i] = -1;
2174 }
2175 }
2176 break;
2177 }
2178
2179 /* stop != 1 */
2180 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002181 if (bit_chk & 1) {
Marek Vasutc4907892015-07-13 02:11:02 +02002182 /*
2183 * Remember a passing test as
2184 * the right_edge.
2185 */
2186 right_edge[i] = d;
2187 } else {
2188 if (d != 0) {
2189 /*
2190 * If a right edge has not
2191 * been seen yet, then a future
2192 * passing test will mark this
2193 * edge as the left edge.
2194 */
2195 if (right_edge[i] == delay_max + 1)
2196 left_edge[i] = -(d + 1);
2197 } else {
2198 /*
2199 * d = 0 failed, but it passed
2200 * when testing the left edge,
2201 * so it must be marginal, set
2202 * it to -1
2203 */
2204 if (right_edge[i] == delay_max + 1 &&
2205 left_edge[i] != delay_max + 1)
2206 right_edge[i] = -1;
2207 /*
2208 * If a right edge has not been
2209 * seen yet, then a future
2210 * passing test will mark this
2211 * edge as the left edge.
2212 */
2213 else if (right_edge[i] == delay_max + 1)
2214 left_edge[i] = -(d + 1);
2215 }
2216 }
2217
2218 debug_cond(DLEVEL == 2, "%s:%d center[r,d=%u]: ",
2219 __func__, __LINE__, d);
2220 debug_cond(DLEVEL == 2,
2221 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasut0c4be192015-07-18 20:34:00 +02002222 bit_chk & 1, i, left_edge[i]);
Marek Vasutc4907892015-07-13 02:11:02 +02002223 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2224 right_edge[i]);
Marek Vasut0c4be192015-07-18 20:34:00 +02002225 bit_chk >>= 1;
Marek Vasutc4907892015-07-13 02:11:02 +02002226 }
2227 }
2228
2229 /* Check that all bits have a window */
2230 for (i = 0; i < per_dqs; i++) {
2231 debug_cond(DLEVEL == 2,
2232 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2233 __func__, __LINE__, i, left_edge[i],
2234 i, right_edge[i]);
2235 if ((left_edge[i] == dqs_max + 1) ||
2236 (right_edge[i] == dqs_max + 1))
2237 return i + 1; /* FIXME: If we fail, retval > 0 */
2238 }
2239
2240 return 0;
2241}
2242
Marek Vasutafb3eb82015-07-18 19:18:06 +02002243/**
2244 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2245 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2246 * @left_edge: Left edge of the DQ/DQS phase
2247 * @right_edge: Right edge of the DQ/DQS phase
2248 * @mid_min: Best DQ/DQS phase middle setting
2249 *
2250 * Find index and value of the middle of the DQ/DQS working phase.
2251 */
2252static int get_window_mid_index(const int write, int *left_edge,
2253 int *right_edge, int *mid_min)
2254{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002255 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2256 rwcfg->mem_dq_per_read_dqs;
Marek Vasutafb3eb82015-07-18 19:18:06 +02002257 int i, mid, min_index;
2258
2259 /* Find middle of window for each DQ bit */
2260 *mid_min = left_edge[0] - right_edge[0];
2261 min_index = 0;
2262 for (i = 1; i < per_dqs; i++) {
2263 mid = left_edge[i] - right_edge[i];
2264 if (mid < *mid_min) {
2265 *mid_min = mid;
2266 min_index = i;
2267 }
2268 }
2269
2270 /*
2271 * -mid_min/2 represents the amount that we need to move DQS.
2272 * If mid_min is odd and positive we'll need to add one to make
2273 * sure the rounding in further calculations is correct (always
2274 * bias to the right), so just add 1 for all positive values.
2275 */
2276 if (*mid_min > 0)
2277 (*mid_min)++;
2278 *mid_min = *mid_min / 2;
2279
2280 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
2281 __func__, __LINE__, *mid_min, min_index);
2282 return min_index;
2283}
2284
Marek Vasutffb8b662015-07-18 19:46:26 +02002285/**
2286 * center_dq_windows() - Center the DQ/DQS windows
2287 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2288 * @left_edge: Left edge of the DQ/DQS phase
2289 * @right_edge: Right edge of the DQ/DQS phase
2290 * @mid_min: Adjusted DQ/DQS phase middle setting
2291 * @orig_mid_min: Original DQ/DQS phase middle setting
2292 * @min_index: DQ/DQS phase middle setting index
2293 * @test_bgn: Rank number to begin the test
2294 * @dq_margin: Amount of shift for the DQ
2295 * @dqs_margin: Amount of shift for the DQS
2296 *
2297 * Align the DQ/DQS windows in each group.
2298 */
2299static void center_dq_windows(const int write, int *left_edge, int *right_edge,
2300 const int mid_min, const int orig_mid_min,
2301 const int min_index, const int test_bgn,
2302 int *dq_margin, int *dqs_margin)
2303{
Marek Vasut160695d2015-08-02 19:10:58 +02002304 const u32 delay_max = write ? iocfg->io_out1_delay_max : iocfg->io_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002305 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2306 rwcfg->mem_dq_per_read_dqs;
Marek Vasutffb8b662015-07-18 19:46:26 +02002307 const u32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
2308 SCC_MGR_IO_IN_DELAY_OFFSET;
2309 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
2310
2311 u32 temp_dq_io_delay1, temp_dq_io_delay2;
2312 int shift_dq, i, p;
2313
2314 /* Initialize data for export structures */
2315 *dqs_margin = delay_max + 1;
2316 *dq_margin = delay_max + 1;
2317
2318 /* add delay to bring centre of all DQ windows to the same "level" */
2319 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2320 /* Use values before divide by 2 to reduce round off error */
2321 shift_dq = (left_edge[i] - right_edge[i] -
2322 (left_edge[min_index] - right_edge[min_index]))/2 +
2323 (orig_mid_min - mid_min);
2324
2325 debug_cond(DLEVEL == 2,
2326 "vfifo_center: before: shift_dq[%u]=%d\n",
2327 i, shift_dq);
2328
2329 temp_dq_io_delay1 = readl(addr + (p << 2));
2330 temp_dq_io_delay2 = readl(addr + (i << 2));
2331
2332 if (shift_dq + temp_dq_io_delay1 > delay_max)
2333 shift_dq = delay_max - temp_dq_io_delay2;
2334 else if (shift_dq + temp_dq_io_delay1 < 0)
2335 shift_dq = -temp_dq_io_delay1;
2336
2337 debug_cond(DLEVEL == 2,
2338 "vfifo_center: after: shift_dq[%u]=%d\n",
2339 i, shift_dq);
2340
2341 if (write)
2342 scc_mgr_set_dq_out1_delay(i, temp_dq_io_delay1 + shift_dq);
2343 else
2344 scc_mgr_set_dq_in_delay(p, temp_dq_io_delay1 + shift_dq);
2345
2346 scc_mgr_load_dq(p);
2347
2348 debug_cond(DLEVEL == 2,
2349 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2350 left_edge[i] - shift_dq + (-mid_min),
2351 right_edge[i] + shift_dq - (-mid_min));
2352
2353 /* To determine values for export structures */
2354 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2355 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2356
2357 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2358 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2359 }
2360
2361}
2362
Marek Vasutac63b9a2015-07-21 04:27:32 +02002363/**
2364 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2365 * @rank_bgn: Rank number
2366 * @rw_group: Read/Write Group
2367 * @test_bgn: Rank at which the test begins
2368 * @use_read_test: Perform a read test
2369 * @update_fom: Update FOM
2370 *
2371 * Per-bit deskew DQ and centering.
2372 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002373static int rw_mgr_mem_calibrate_vfifo_center(const u32 rank_bgn,
2374 const u32 rw_group, const u32 test_bgn,
2375 const int use_read_test, const int update_fom)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002376{
Marek Vasut5d6db442015-07-18 19:57:12 +02002377 const u32 addr =
2378 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasut0113c3e2015-07-18 20:42:27 +02002379 (rw_group << 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002380 /*
2381 * Store these as signed since there are comparisons with
2382 * signed numbers.
2383 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002384 uint32_t sticky_bit_chk;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002385 int32_t left_edge[rwcfg->mem_dq_per_read_dqs];
2386 int32_t right_edge[rwcfg->mem_dq_per_read_dqs];
Dinh Nguyen3da42852015-06-02 22:52:49 -05002387 int32_t orig_mid_min, mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002388 int32_t new_dqs, start_dqs, start_dqs_en = 0, final_dqs_en;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002389 int32_t dq_margin, dqs_margin;
Marek Vasut5d6db442015-07-18 19:57:12 +02002390 int i, min_index;
Marek Vasutc4907892015-07-13 02:11:02 +02002391 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002392
Marek Vasut0113c3e2015-07-18 20:42:27 +02002393 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002394
Marek Vasut5d6db442015-07-18 19:57:12 +02002395 start_dqs = readl(addr);
Marek Vasut160695d2015-08-02 19:10:58 +02002396 if (iocfg->shift_dqs_en_when_shift_dqs)
2397 start_dqs_en = readl(addr - iocfg->dqs_en_delay_offset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002398
2399 /* set the left and right edge of each bit to an illegal value */
Marek Vasut160695d2015-08-02 19:10:58 +02002400 /* use (iocfg->io_in_delay_max + 1) as an illegal value */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002401 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002402 for (i = 0; i < rwcfg->mem_dq_per_read_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002403 left_edge[i] = iocfg->io_in_delay_max + 1;
2404 right_edge[i] = iocfg->io_in_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002405 }
2406
Dinh Nguyen3da42852015-06-02 22:52:49 -05002407 /* Search for the left edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002408 search_left_edge(0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002409 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002410 left_edge, right_edge, use_read_test);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002411
Marek Vasutf0712c32015-07-18 08:01:45 +02002412
Dinh Nguyen3da42852015-06-02 22:52:49 -05002413 /* Search for the right edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002414 ret = search_right_edge(0, rank_bgn, rw_group, rw_group,
Marek Vasutc4907892015-07-13 02:11:02 +02002415 start_dqs, start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002416 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002417 left_edge, right_edge, use_read_test);
2418 if (ret) {
2419 /*
2420 * Restore delay chain settings before letting the loop
2421 * in rw_mgr_mem_calibrate_vfifo to retry different
2422 * dqs/ck relationships.
2423 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002424 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002425 if (iocfg->shift_dqs_en_when_shift_dqs)
Marek Vasut0113c3e2015-07-18 20:42:27 +02002426 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002427
Marek Vasut0113c3e2015-07-18 20:42:27 +02002428 scc_mgr_load_dqs(rw_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002429 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002430
Marek Vasutc4907892015-07-13 02:11:02 +02002431 debug_cond(DLEVEL == 1,
2432 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2433 __func__, __LINE__, i, left_edge[i], right_edge[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002434 if (use_read_test) {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002435 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002436 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002437 CAL_STAGE_VFIFO,
2438 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002439 } else {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002440 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002441 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002442 CAL_STAGE_VFIFO_AFTER_WRITES,
2443 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002444 }
Marek Vasut98668242015-07-18 20:44:28 +02002445 return -EIO;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002446 }
2447
Marek Vasutafb3eb82015-07-18 19:18:06 +02002448 min_index = get_window_mid_index(0, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002449
2450 /* Determine the amount we can change DQS (which is -mid_min) */
2451 orig_mid_min = mid_min;
2452 new_dqs = start_dqs - mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002453 if (new_dqs > iocfg->dqs_in_delay_max)
2454 new_dqs = iocfg->dqs_in_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002455 else if (new_dqs < 0)
2456 new_dqs = 0;
2457
2458 mid_min = start_dqs - new_dqs;
2459 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2460 mid_min, new_dqs);
2461
Marek Vasut160695d2015-08-02 19:10:58 +02002462 if (iocfg->shift_dqs_en_when_shift_dqs) {
2463 if (start_dqs_en - mid_min > iocfg->dqs_en_delay_max)
2464 mid_min += start_dqs_en - mid_min - iocfg->dqs_en_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002465 else if (start_dqs_en - mid_min < 0)
2466 mid_min += start_dqs_en - mid_min;
2467 }
2468 new_dqs = start_dqs - mid_min;
2469
Marek Vasutf0712c32015-07-18 08:01:45 +02002470 debug_cond(DLEVEL == 1,
2471 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2472 start_dqs,
Marek Vasut160695d2015-08-02 19:10:58 +02002473 iocfg->shift_dqs_en_when_shift_dqs ? start_dqs_en : -1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002474 new_dqs, mid_min);
2475
Marek Vasutffb8b662015-07-18 19:46:26 +02002476 /* Add delay to bring centre of all DQ windows to the same "level". */
2477 center_dq_windows(0, left_edge, right_edge, mid_min, orig_mid_min,
2478 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002479
Dinh Nguyen3da42852015-06-02 22:52:49 -05002480 /* Move DQS-en */
Marek Vasut160695d2015-08-02 19:10:58 +02002481 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut5d6db442015-07-18 19:57:12 +02002482 final_dqs_en = start_dqs_en - mid_min;
Marek Vasut0113c3e2015-07-18 20:42:27 +02002483 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2484 scc_mgr_load_dqs(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002485 }
2486
2487 /* Move DQS */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002488 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2489 scc_mgr_load_dqs(rw_group);
Marek Vasutf0712c32015-07-18 08:01:45 +02002490 debug_cond(DLEVEL == 2,
2491 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2492 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002493
2494 /*
2495 * Do not remove this line as it makes sure all of our decisions
2496 * have been applied. Apply the update bit.
2497 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002498 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002499
Marek Vasut98668242015-07-18 20:44:28 +02002500 if ((dq_margin < 0) || (dqs_margin < 0))
2501 return -EINVAL;
2502
2503 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002504}
2505
Marek Vasutbce24ef2015-07-17 03:16:45 +02002506/**
Marek Vasut04372fb2015-07-18 02:46:56 +02002507 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
2508 * @rw_group: Read/Write Group
2509 * @phase: DQ/DQS phase
2510 *
2511 * Because initially no communication ca be reliably performed with the memory
2512 * device, the sequencer uses a guaranteed write mechanism to write data into
2513 * the memory device.
2514 */
2515static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
2516 const u32 phase)
2517{
Marek Vasut04372fb2015-07-18 02:46:56 +02002518 int ret;
2519
2520 /* Set a particular DQ/DQS phase. */
2521 scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
2522
2523 debug_cond(DLEVEL == 1, "%s:%d guaranteed write: g=%u p=%u\n",
2524 __func__, __LINE__, rw_group, phase);
2525
2526 /*
2527 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2528 * Load up the patterns used by read calibration using the
2529 * current DQDQS phase.
2530 */
2531 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2532
2533 if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2534 return 0;
2535
2536 /*
2537 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2538 * Back-to-Back reads of the patterns used for calibration.
2539 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02002540 ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
2541 if (ret)
Marek Vasut04372fb2015-07-18 02:46:56 +02002542 debug_cond(DLEVEL == 1,
2543 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2544 __func__, __LINE__, rw_group, phase);
Marek Vasutd844c7d2015-07-18 03:55:07 +02002545 return ret;
Marek Vasut04372fb2015-07-18 02:46:56 +02002546}
2547
2548/**
Marek Vasutf09da112015-07-18 02:57:32 +02002549 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2550 * @rw_group: Read/Write Group
2551 * @test_bgn: Rank at which the test begins
2552 *
2553 * DQS enable calibration ensures reliable capture of the DQ signal without
2554 * glitches on the DQS line.
2555 */
2556static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
2557 const u32 test_bgn)
2558{
Marek Vasutf09da112015-07-18 02:57:32 +02002559 /*
2560 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2561 * DQS and DQS Eanble Signal Relationships.
2562 */
Marek Vasut28ea8272015-07-18 04:28:42 +02002563
2564 /* We start at zero, so have one less dq to devide among */
Marek Vasut160695d2015-08-02 19:10:58 +02002565 const u32 delay_step = iocfg->io_in_delay_max /
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002566 (rwcfg->mem_dq_per_read_dqs - 1);
Marek Vasut914546e2015-07-20 09:20:42 +02002567 int ret;
Marek Vasut28ea8272015-07-18 04:28:42 +02002568 u32 i, p, d, r;
2569
2570 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2571
2572 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002573 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002574 r += NUM_RANKS_PER_SHADOW_REG) {
2575 for (i = 0, p = test_bgn, d = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002576 i < rwcfg->mem_dq_per_read_dqs;
Marek Vasut28ea8272015-07-18 04:28:42 +02002577 i++, p++, d += delay_step) {
2578 debug_cond(DLEVEL == 1,
2579 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2580 __func__, __LINE__, rw_group, r, i, p, d);
2581
2582 scc_mgr_set_dq_in_delay(p, d);
2583 scc_mgr_load_dq(p);
2584 }
2585
2586 writel(0, &sdr_scc_mgr->update);
2587 }
2588
2589 /*
2590 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2591 * dq_in_delay values
2592 */
Marek Vasut914546e2015-07-20 09:20:42 +02002593 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(rw_group);
Marek Vasut28ea8272015-07-18 04:28:42 +02002594
2595 debug_cond(DLEVEL == 1,
2596 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut914546e2015-07-20 09:20:42 +02002597 __func__, __LINE__, rw_group, !ret);
Marek Vasut28ea8272015-07-18 04:28:42 +02002598
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002599 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002600 r += NUM_RANKS_PER_SHADOW_REG) {
2601 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2602 writel(0, &sdr_scc_mgr->update);
2603 }
2604
Marek Vasut914546e2015-07-20 09:20:42 +02002605 return ret;
Marek Vasutf09da112015-07-18 02:57:32 +02002606}
2607
2608/**
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002609 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2610 * @rw_group: Read/Write Group
2611 * @test_bgn: Rank at which the test begins
2612 * @use_read_test: Perform a read test
2613 * @update_fom: Update FOM
2614 *
2615 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2616 * within a group.
2617 */
2618static int
2619rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
2620 const int use_read_test,
2621 const int update_fom)
2622
2623{
2624 int ret, grp_calibrated;
2625 u32 rank_bgn, sr;
2626
2627 /*
2628 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2629 * Read per-bit deskew can be done on a per shadow register basis.
2630 */
2631 grp_calibrated = 1;
2632 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002633 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002634 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002635 ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
Marek Vasut0113c3e2015-07-18 20:42:27 +02002636 test_bgn,
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002637 use_read_test,
2638 update_fom);
Marek Vasut98668242015-07-18 20:44:28 +02002639 if (!ret)
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002640 continue;
2641
2642 grp_calibrated = 0;
2643 }
2644
2645 if (!grp_calibrated)
2646 return -EIO;
2647
2648 return 0;
2649}
2650
2651/**
Marek Vasutbce24ef2015-07-17 03:16:45 +02002652 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2653 * @rw_group: Read/Write Group
2654 * @test_bgn: Rank at which the test begins
Dinh Nguyen3da42852015-06-02 22:52:49 -05002655 *
Marek Vasutbce24ef2015-07-17 03:16:45 +02002656 * Stage 1: Calibrate the read valid prediction FIFO.
2657 *
2658 * This function implements UniPHY calibration Stage 1, as explained in
2659 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2660 *
2661 * - read valid prediction will consist of finding:
2662 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2663 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002664 * - we also do a per-bit deskew on the DQ lines.
2665 */
Marek Vasutc336ca32015-07-17 04:24:18 +02002666static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002667{
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002668 uint32_t p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002669 uint32_t dtaps_per_ptap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002670 uint32_t failed_substage;
2671
Marek Vasut04372fb2015-07-18 02:46:56 +02002672 int ret;
2673
Marek Vasutc336ca32015-07-17 04:24:18 +02002674 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002675
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002676 /* Update info for sims */
2677 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002678 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002679 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002680
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002681 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2682
2683 /* USER Determine number of delay taps for each phase tap. */
Marek Vasut160695d2015-08-02 19:10:58 +02002684 dtaps_per_ptap = DIV_ROUND_UP(iocfg->delay_per_opa_tap,
2685 iocfg->delay_per_dqs_en_dchain_tap) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002686
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002687 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002688 /*
2689 * In RLDRAMX we may be messing the delay of pins in
Marek Vasutc336ca32015-07-17 04:24:18 +02002690 * the same write rw_group but outside of the current read
2691 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutac70d2f2015-07-17 03:44:26 +02002692 * output side yet.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002693 */
2694 if (d > 0) {
Marek Vasutf51a7d32015-07-19 02:18:21 +02002695 scc_mgr_apply_group_all_out_delay_add_all_ranks(
Marek Vasutc336ca32015-07-17 04:24:18 +02002696 rw_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002697 }
2698
Marek Vasut160695d2015-08-02 19:10:58 +02002699 for (p = 0; p <= iocfg->dqdqs_out_phase_max; p++) {
Marek Vasut04372fb2015-07-18 02:46:56 +02002700 /* 1) Guaranteed Write */
2701 ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
2702 if (ret)
2703 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002704
Marek Vasutf09da112015-07-18 02:57:32 +02002705 /* 2) DQS Enable Calibration */
2706 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
2707 test_bgn);
2708 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002709 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002710 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002711 }
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002712
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002713 /* 3) Centering DQ/DQS */
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002714 /*
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002715 * If doing read after write calibration, do not update
2716 * FOM now. Do it then.
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002717 */
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002718 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
2719 test_bgn, 1, 0);
2720 if (ret) {
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002721 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002722 continue;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002723 }
2724
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002725 /* All done. */
2726 goto cal_done_ok;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002727 }
2728 }
2729
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002730 /* Calibration Stage 1 failed. */
Marek Vasutc336ca32015-07-17 04:24:18 +02002731 set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002732 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002733
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002734 /* Calibration Stage 1 completed OK. */
2735cal_done_ok:
Dinh Nguyen3da42852015-06-02 22:52:49 -05002736 /*
2737 * Reset the delay chains back to zero if they have moved > 1
2738 * (check for > 1 because loop will increase d even when pass in
2739 * first case).
2740 */
2741 if (d > 2)
Marek Vasutc336ca32015-07-17 04:24:18 +02002742 scc_mgr_zero_group(rw_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002743
2744 return 1;
2745}
2746
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002747/**
2748 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2749 * @rw_group: Read/Write Group
2750 * @test_bgn: Rank at which the test begins
2751 *
2752 * Stage 3: DQ/DQS Centering.
2753 *
2754 * This function implements UniPHY calibration Stage 3, as explained in
2755 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2756 */
2757static int rw_mgr_mem_calibrate_vfifo_end(const u32 rw_group,
2758 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002759{
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002760 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002761
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002762 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002763
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002764 /* Update info for sims. */
2765 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002766 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2767 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2768
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002769 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group, test_bgn, 0, 1);
2770 if (ret)
2771 set_failing_group_stage(rw_group,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002772 CAL_STAGE_VFIFO_AFTER_WRITES,
2773 CAL_SUBSTAGE_VFIFO_CENTER);
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002774 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002775}
2776
Marek Vasutc9842782015-07-21 06:18:57 +02002777/**
2778 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2779 *
2780 * Stage 4: Minimize latency.
2781 *
2782 * This function implements UniPHY calibration Stage 4, as explained in
2783 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2784 * Calibrate LFIFO to find smallest read latency.
2785 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002786static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2787{
Marek Vasutc9842782015-07-21 06:18:57 +02002788 int found_one = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002789
2790 debug("%s:%d\n", __func__, __LINE__);
2791
Marek Vasutc9842782015-07-21 06:18:57 +02002792 /* Update info for sims. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002793 reg_file_set_stage(CAL_STAGE_LFIFO);
2794 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2795
2796 /* Load up the patterns used by read calibration for all ranks */
2797 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002798
Dinh Nguyen3da42852015-06-02 22:52:49 -05002799 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002800 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002801 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2802 __func__, __LINE__, gbl->curr_read_lat);
2803
Marek Vasutc9842782015-07-21 06:18:57 +02002804 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0, NUM_READ_TESTS,
2805 PASS_ALL_BITS, 1))
Dinh Nguyen3da42852015-06-02 22:52:49 -05002806 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002807
2808 found_one = 1;
Marek Vasutc9842782015-07-21 06:18:57 +02002809 /*
2810 * Reduce read latency and see if things are
2811 * working correctly.
2812 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002813 gbl->curr_read_lat--;
2814 } while (gbl->curr_read_lat > 0);
2815
Marek Vasutc9842782015-07-21 06:18:57 +02002816 /* Reset the fifos to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02002817 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002818
2819 if (found_one) {
Marek Vasutc9842782015-07-21 06:18:57 +02002820 /* Add a fudge factor to the read latency that was determined */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002821 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002822 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutc9842782015-07-21 06:18:57 +02002823 debug_cond(DLEVEL == 2,
2824 "%s:%d lfifo: success: using read_lat=%u\n",
2825 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002826 } else {
2827 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2828 CAL_SUBSTAGE_READ_LATENCY);
2829
Marek Vasutc9842782015-07-21 06:18:57 +02002830 debug_cond(DLEVEL == 2,
2831 "%s:%d lfifo: failed at initial read_lat=%u\n",
2832 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002833 }
Marek Vasutc9842782015-07-21 06:18:57 +02002834
2835 return found_one;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002836}
2837
Marek Vasutc8570af2015-07-21 05:26:58 +02002838/**
2839 * search_window() - Search for the/part of the window with DM/DQS shift
2840 * @search_dm: If 1, search for the DM shift, if 0, search for DQS shift
2841 * @rank_bgn: Rank number
2842 * @write_group: Write Group
2843 * @bgn_curr: Current window begin
2844 * @end_curr: Current window end
2845 * @bgn_best: Current best window begin
2846 * @end_best: Current best window end
2847 * @win_best: Size of the best window
2848 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2849 *
2850 * Search for the/part of the window with DM/DQS shift.
2851 */
2852static void search_window(const int search_dm,
2853 const u32 rank_bgn, const u32 write_group,
2854 int *bgn_curr, int *end_curr, int *bgn_best,
2855 int *end_best, int *win_best, int new_dqs)
2856{
2857 u32 bit_chk;
Marek Vasut160695d2015-08-02 19:10:58 +02002858 const int max = iocfg->io_out1_delay_max - new_dqs;
Marek Vasutc8570af2015-07-21 05:26:58 +02002859 int d, di;
2860
2861 /* Search for the/part of the window with DM/DQS shift. */
2862 for (di = max; di >= 0; di -= DELTA_D) {
2863 if (search_dm) {
2864 d = di;
2865 scc_mgr_apply_group_dm_out1_delay(d);
2866 } else {
2867 /* For DQS, we go from 0...max */
2868 d = max - di;
2869 /*
2870 * Note: This only shifts DQS, so are we limiting ourselve to
2871 * width of DQ unnecessarily.
2872 */
2873 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2874 d + new_dqs);
2875 }
2876
2877 writel(0, &sdr_scc_mgr->update);
2878
2879 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2880 PASS_ALL_BITS, &bit_chk,
2881 0)) {
2882 /* Set current end of the window. */
2883 *end_curr = search_dm ? -d : d;
2884
2885 /*
2886 * If a starting edge of our window has not been seen
2887 * this is our current start of the DM window.
2888 */
Marek Vasut160695d2015-08-02 19:10:58 +02002889 if (*bgn_curr == iocfg->io_out1_delay_max + 1)
Marek Vasutc8570af2015-07-21 05:26:58 +02002890 *bgn_curr = search_dm ? -d : d;
2891
2892 /*
2893 * If current window is bigger than best seen.
2894 * Set best seen to be current window.
2895 */
2896 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2897 *win_best = *end_curr - *bgn_curr + 1;
2898 *bgn_best = *bgn_curr;
2899 *end_best = *end_curr;
2900 }
2901 } else {
2902 /* We just saw a failing test. Reset temp edge. */
Marek Vasut160695d2015-08-02 19:10:58 +02002903 *bgn_curr = iocfg->io_out1_delay_max + 1;
2904 *end_curr = iocfg->io_out1_delay_max + 1;
Marek Vasutc8570af2015-07-21 05:26:58 +02002905
2906 /* Early exit is only applicable to DQS. */
2907 if (search_dm)
2908 continue;
2909
2910 /*
2911 * Early exit optimization: if the remaining delay
2912 * chain space is less than already seen largest
2913 * window we can exit.
2914 */
Marek Vasut160695d2015-08-02 19:10:58 +02002915 if (*win_best - 1 > iocfg->io_out1_delay_max - new_dqs - d)
Marek Vasutc8570af2015-07-21 05:26:58 +02002916 break;
2917 }
2918 }
2919}
2920
Dinh Nguyen3da42852015-06-02 22:52:49 -05002921/*
Marek Vasuta386a502015-07-21 05:33:49 +02002922 * rw_mgr_mem_calibrate_writes_center() - Center all windows
2923 * @rank_bgn: Rank number
2924 * @write_group: Write group
2925 * @test_bgn: Rank at which the test begins
2926 *
2927 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen3da42852015-06-02 22:52:49 -05002928 * certain windows.
2929 */
Marek Vasut3b44f552015-07-21 05:00:42 +02002930static int
2931rw_mgr_mem_calibrate_writes_center(const u32 rank_bgn, const u32 write_group,
2932 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002933{
Marek Vasutc8570af2015-07-21 05:26:58 +02002934 int i;
Marek Vasut3b44f552015-07-21 05:00:42 +02002935 u32 sticky_bit_chk;
2936 u32 min_index;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002937 int left_edge[rwcfg->mem_dq_per_write_dqs];
2938 int right_edge[rwcfg->mem_dq_per_write_dqs];
Marek Vasut3b44f552015-07-21 05:00:42 +02002939 int mid;
2940 int mid_min, orig_mid_min;
2941 int new_dqs, start_dqs;
2942 int dq_margin, dqs_margin, dm_margin;
Marek Vasut160695d2015-08-02 19:10:58 +02002943 int bgn_curr = iocfg->io_out1_delay_max + 1;
2944 int end_curr = iocfg->io_out1_delay_max + 1;
2945 int bgn_best = iocfg->io_out1_delay_max + 1;
2946 int end_best = iocfg->io_out1_delay_max + 1;
Marek Vasut3b44f552015-07-21 05:00:42 +02002947 int win_best = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002948
Marek Vasutc4907892015-07-13 02:11:02 +02002949 int ret;
2950
Dinh Nguyen3da42852015-06-02 22:52:49 -05002951 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2952
2953 dm_margin = 0;
2954
Marek Vasutc6540872015-07-21 05:29:05 +02002955 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
2956 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002957 (rwcfg->mem_dq_per_write_dqs << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002958
Marek Vasut3b44f552015-07-21 05:00:42 +02002959 /* Per-bit deskew. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002960
2961 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02002962 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02002963 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002964 */
2965 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002966 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002967 left_edge[i] = iocfg->io_out1_delay_max + 1;
2968 right_edge[i] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002969 }
2970
Marek Vasut3b44f552015-07-21 05:00:42 +02002971 /* Search for the left edge of the window for each bit. */
Marek Vasut71120772015-07-13 02:38:15 +02002972 search_left_edge(1, rank_bgn, write_group, 0, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002973 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002974 left_edge, right_edge, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002975
Marek Vasut3b44f552015-07-21 05:00:42 +02002976 /* Search for the right edge of the window for each bit. */
Marek Vasutc4907892015-07-13 02:11:02 +02002977 ret = search_right_edge(1, rank_bgn, write_group, 0,
2978 start_dqs, 0,
Marek Vasut0c4be192015-07-18 20:34:00 +02002979 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002980 left_edge, right_edge, 0);
2981 if (ret) {
2982 set_failing_group_stage(test_bgn + ret - 1, CAL_STAGE_WRITES,
2983 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutd043ee52015-07-21 05:32:49 +02002984 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002985 }
2986
Marek Vasutafb3eb82015-07-18 19:18:06 +02002987 min_index = get_window_mid_index(1, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002988
Marek Vasut3b44f552015-07-21 05:00:42 +02002989 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002990 orig_mid_min = mid_min;
2991 new_dqs = start_dqs;
2992 mid_min = 0;
Marek Vasut3b44f552015-07-21 05:00:42 +02002993 debug_cond(DLEVEL == 1,
2994 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
2995 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002996
Marek Vasutffb8b662015-07-18 19:46:26 +02002997 /* Add delay to bring centre of all DQ windows to the same "level". */
2998 center_dq_windows(1, left_edge, right_edge, mid_min, orig_mid_min,
2999 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003000
3001 /* Move DQS */
3002 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02003003 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003004
3005 /* Centre DM */
3006 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
3007
3008 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003009 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02003010 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003011 */
Marek Vasut160695d2015-08-02 19:10:58 +02003012 left_edge[0] = iocfg->io_out1_delay_max + 1;
3013 right_edge[0] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003014
Marek Vasut3b44f552015-07-21 05:00:42 +02003015 /* Search for the/part of the window with DM shift. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003016 search_window(1, rank_bgn, write_group, &bgn_curr, &end_curr,
3017 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003018
Marek Vasut3b44f552015-07-21 05:00:42 +02003019 /* Reset DM delay chains to 0. */
Marek Vasut32675242015-07-17 06:07:13 +02003020 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003021
3022 /*
3023 * Check to see if the current window nudges up aganist 0 delay.
3024 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasut3b44f552015-07-21 05:00:42 +02003025 * search begins as a new search.
3026 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003027 if (end_curr != 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02003028 bgn_curr = iocfg->io_out1_delay_max + 1;
3029 end_curr = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003030 }
3031
Marek Vasut3b44f552015-07-21 05:00:42 +02003032 /* Search for the/part of the window with DQS shifts. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003033 search_window(0, rank_bgn, write_group, &bgn_curr, &end_curr,
3034 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003035
Marek Vasut3b44f552015-07-21 05:00:42 +02003036 /* Assign left and right edge for cal and reporting. */
3037 left_edge[0] = -1 * bgn_best;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003038 right_edge[0] = end_best;
3039
Marek Vasut3b44f552015-07-21 05:00:42 +02003040 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n",
3041 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003042
Marek Vasut3b44f552015-07-21 05:00:42 +02003043 /* Move DQS (back to orig). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003044 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3045
3046 /* Move DM */
3047
Marek Vasut3b44f552015-07-21 05:00:42 +02003048 /* Find middle of window for the DM bit. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003049 mid = (left_edge[0] - right_edge[0]) / 2;
3050
Marek Vasut3b44f552015-07-21 05:00:42 +02003051 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003052 if (mid < 0)
3053 mid = 0;
3054
Marek Vasut3b44f552015-07-21 05:00:42 +02003055 /* dm_marign should fail if we never find a window. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003056 if (win_best == 0)
3057 dm_margin = -1;
3058 else
3059 dm_margin = left_edge[0] - mid;
3060
Marek Vasut32675242015-07-17 06:07:13 +02003061 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003062 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003063
Marek Vasut3b44f552015-07-21 05:00:42 +02003064 debug_cond(DLEVEL == 2,
3065 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3066 __func__, __LINE__, left_edge[0], right_edge[0],
3067 mid, dm_margin);
3068 /* Export values. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003069 gbl->fom_out += dq_margin + dqs_margin;
3070
Marek Vasut3b44f552015-07-21 05:00:42 +02003071 debug_cond(DLEVEL == 2,
3072 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3073 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003074
3075 /*
3076 * Do not remove this line as it makes sure all of our
3077 * decisions have been applied.
3078 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003079 writel(0, &sdr_scc_mgr->update);
Marek Vasut3b44f552015-07-21 05:00:42 +02003080
Marek Vasutd043ee52015-07-21 05:32:49 +02003081 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3082 return -EINVAL;
3083
3084 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003085}
3086
Marek Vasutdb3a6062015-07-18 07:23:25 +02003087/**
3088 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3089 * @rank_bgn: Rank number
3090 * @group: Read/Write Group
3091 * @test_bgn: Rank at which the test begins
3092 *
3093 * Stage 2: Write Calibration Part One.
3094 *
3095 * This function implements UniPHY calibration Stage 2, as explained in
3096 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3097 */
3098static int rw_mgr_mem_calibrate_writes(const u32 rank_bgn, const u32 group,
3099 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003100{
Marek Vasutdb3a6062015-07-18 07:23:25 +02003101 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003102
Marek Vasutdb3a6062015-07-18 07:23:25 +02003103 /* Update info for sims */
3104 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3105
3106 reg_file_set_group(group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003107 reg_file_set_stage(CAL_STAGE_WRITES);
3108 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3109
Marek Vasutdb3a6062015-07-18 07:23:25 +02003110 ret = rw_mgr_mem_calibrate_writes_center(rank_bgn, group, test_bgn);
Marek Vasutd043ee52015-07-21 05:32:49 +02003111 if (ret)
Marek Vasutdb3a6062015-07-18 07:23:25 +02003112 set_failing_group_stage(group, CAL_STAGE_WRITES,
Dinh Nguyen3da42852015-06-02 22:52:49 -05003113 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003114
Marek Vasutd043ee52015-07-21 05:32:49 +02003115 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003116}
3117
Marek Vasut4b0ac262015-07-20 07:33:33 +02003118/**
3119 * mem_precharge_and_activate() - Precharge all banks and activate
3120 *
3121 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3122 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003123static void mem_precharge_and_activate(void)
3124{
Marek Vasut4b0ac262015-07-20 07:33:33 +02003125 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003126
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003127 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasut4b0ac262015-07-20 07:33:33 +02003128 /* Set rank. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003129 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3130
Marek Vasut4b0ac262015-07-20 07:33:33 +02003131 /* Precharge all banks. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003132 writel(rwcfg->precharge_all, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003133 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003134
Marek Vasut1273dd92015-07-12 21:05:08 +02003135 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003136 writel(rwcfg->activate_0_and_1_wait1,
Marek Vasut1273dd92015-07-12 21:05:08 +02003137 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003138
Marek Vasut1273dd92015-07-12 21:05:08 +02003139 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003140 writel(rwcfg->activate_0_and_1_wait2,
Marek Vasut1273dd92015-07-12 21:05:08 +02003141 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003142
Marek Vasut4b0ac262015-07-20 07:33:33 +02003143 /* Activate rows. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003144 writel(rwcfg->activate_0_and_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003145 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003146 }
3147}
3148
Marek Vasut16502a02015-07-17 01:57:41 +02003149/**
3150 * mem_init_latency() - Configure memory RLAT and WLAT settings
3151 *
3152 * Configure memory RLAT and WLAT parameters.
3153 */
3154static void mem_init_latency(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003155{
Marek Vasut16502a02015-07-17 01:57:41 +02003156 /*
3157 * For AV/CV, LFIFO is hardened and always runs at full rate
3158 * so max latency in AFI clocks, used here, is correspondingly
3159 * smaller.
3160 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003161 const u32 max_latency = (1 << misccfg->max_latency_count_width) - 1;
Marek Vasut16502a02015-07-17 01:57:41 +02003162 u32 rlat, wlat;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003163
3164 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut16502a02015-07-17 01:57:41 +02003165
3166 /*
3167 * Read in write latency.
3168 * WL for Hard PHY does not include additive latency.
3169 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003170 wlat = readl(&data_mgr->t_wl_add);
3171 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003172
Marek Vasut16502a02015-07-17 01:57:41 +02003173 gbl->rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003174
Marek Vasut16502a02015-07-17 01:57:41 +02003175 /* Read in readl latency. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003176 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003177
Marek Vasut16502a02015-07-17 01:57:41 +02003178 /* Set a pretty high read latency initially. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003179 gbl->curr_read_lat = rlat + 16;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003180 if (gbl->curr_read_lat > max_latency)
3181 gbl->curr_read_lat = max_latency;
3182
Marek Vasut1273dd92015-07-12 21:05:08 +02003183 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003184
Marek Vasut16502a02015-07-17 01:57:41 +02003185 /* Advertise write latency. */
3186 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003187}
3188
Marek Vasut51cea0b2015-07-26 10:54:15 +02003189/**
3190 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3191 *
3192 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3193 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003194static void mem_skip_calibrate(void)
3195{
3196 uint32_t vfifo_offset;
3197 uint32_t i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003198
3199 debug("%s:%d\n", __func__, __LINE__);
3200 /* Need to update every shadow register set used by the interface */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003201 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003202 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003203 /*
3204 * Set output phase alignment settings appropriate for
3205 * skip calibration.
3206 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003207 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003208 scc_mgr_set_dqs_en_phase(i, 0);
Marek Vasut160695d2015-08-02 19:10:58 +02003209 if (iocfg->dll_chain_length == 6)
3210 scc_mgr_set_dqdqs_output_phase(i, 6);
3211 else
3212 scc_mgr_set_dqdqs_output_phase(i, 7);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003213 /*
3214 * Case:33398
3215 *
3216 * Write data arrives to the I/O two cycles before write
3217 * latency is reached (720 deg).
3218 * -> due to bit-slip in a/c bus
3219 * -> to allow board skew where dqs is longer than ck
3220 * -> how often can this happen!?
3221 * -> can claim back some ptaps for high freq
3222 * support if we can relax this, but i digress...
3223 *
3224 * The write_clk leads mem_ck by 90 deg
3225 * The minimum ptap of the OPA is 180 deg
3226 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3227 * The write_clk is always delayed by 2 ptaps
3228 *
3229 * Hence, to make DQS aligned to CK, we need to delay
3230 * DQS by:
Marek Vasut160695d2015-08-02 19:10:58 +02003231 * (720 - 90 - 180 - 2 * (360 / iocfg->dll_chain_length))
Dinh Nguyen3da42852015-06-02 22:52:49 -05003232 *
Marek Vasut160695d2015-08-02 19:10:58 +02003233 * Dividing the above by (360 / iocfg->dll_chain_length)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003234 * gives us the number of ptaps, which simplies to:
3235 *
Marek Vasut160695d2015-08-02 19:10:58 +02003236 * (1.25 * iocfg->dll_chain_length - 2)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003237 */
Marek Vasut51cea0b2015-07-26 10:54:15 +02003238 scc_mgr_set_dqdqs_output_phase(i,
Marek Vasut160695d2015-08-02 19:10:58 +02003239 1.25 * iocfg->dll_chain_length - 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003240 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003241 writel(0xff, &sdr_scc_mgr->dqs_ena);
3242 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003243
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003244 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003245 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3246 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003247 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003248 writel(0xff, &sdr_scc_mgr->dq_ena);
3249 writel(0xff, &sdr_scc_mgr->dm_ena);
3250 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003251 }
3252
3253 /* Compensate for simulation model behaviour */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003254 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003255 scc_mgr_set_dqs_bus_in_delay(i, 10);
3256 scc_mgr_load_dqs(i);
3257 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003258 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003259
3260 /*
3261 * ArriaV has hard FIFOs that can only be initialized by incrementing
3262 * in sequencer.
3263 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003264 vfifo_offset = misccfg->calib_vfifo_offset;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003265 for (j = 0; j < vfifo_offset; j++)
Marek Vasut1273dd92015-07-12 21:05:08 +02003266 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasut1273dd92015-07-12 21:05:08 +02003267 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003268
3269 /*
Marek Vasut51cea0b2015-07-26 10:54:15 +02003270 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3271 * setting from generation-time constant.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003272 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003273 gbl->curr_read_lat = misccfg->calib_lfifo_offset;
Marek Vasut1273dd92015-07-12 21:05:08 +02003274 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003275}
3276
Marek Vasut3589fbf2015-07-20 04:34:51 +02003277/**
3278 * mem_calibrate() - Memory calibration entry point.
3279 *
3280 * Perform memory calibration.
3281 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003282static uint32_t mem_calibrate(void)
3283{
3284 uint32_t i;
3285 uint32_t rank_bgn, sr;
3286 uint32_t write_group, write_test_bgn;
3287 uint32_t read_group, read_test_bgn;
3288 uint32_t run_groups, current_run;
3289 uint32_t failing_groups = 0;
3290 uint32_t group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003291
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003292 const u32 rwdqs_ratio = rwcfg->mem_if_read_dqs_width /
3293 rwcfg->mem_if_write_dqs_width;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003294
Dinh Nguyen3da42852015-06-02 22:52:49 -05003295 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003296
Marek Vasut16502a02015-07-17 01:57:41 +02003297 /* Initialize the data settings */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003298 gbl->error_substage = CAL_SUBSTAGE_NIL;
3299 gbl->error_stage = CAL_STAGE_NIL;
3300 gbl->error_group = 0xff;
3301 gbl->fom_in = 0;
3302 gbl->fom_out = 0;
3303
Marek Vasut16502a02015-07-17 01:57:41 +02003304 /* Initialize WLAT and RLAT. */
3305 mem_init_latency();
3306
3307 /* Initialize bit slips. */
3308 mem_precharge_and_activate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003309
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003310 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003311 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3312 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003313 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3314 if (i == 0)
3315 scc_mgr_set_hhp_extras();
3316
Marek Vasutc5c5f532015-07-17 02:06:20 +02003317 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003318 }
3319
Marek Vasut722c9682015-07-17 02:07:12 +02003320 /* Calibration is skipped. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003321 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3322 /*
3323 * Set VFIFO and LFIFO to instant-on settings in skip
3324 * calibration mode.
3325 */
3326 mem_skip_calibrate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003327
Marek Vasut722c9682015-07-17 02:07:12 +02003328 /*
3329 * Do not remove this line as it makes sure all of our
3330 * decisions have been applied.
3331 */
3332 writel(0, &sdr_scc_mgr->update);
3333 return 1;
3334 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003335
Marek Vasut722c9682015-07-17 02:07:12 +02003336 /* Calibration is not skipped. */
3337 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3338 /*
3339 * Zero all delay chain/phase settings for all
3340 * groups and all shadow register sets.
3341 */
3342 scc_mgr_zero_all();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003343
Marek Vasutf085ac32015-08-02 18:27:21 +02003344 run_groups = ~0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003345
Marek Vasut722c9682015-07-17 02:07:12 +02003346 for (write_group = 0, write_test_bgn = 0; write_group
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003347 < rwcfg->mem_if_write_dqs_width; write_group++,
3348 write_test_bgn += rwcfg->mem_dq_per_write_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003349
3350 /* Initialize the group failure */
Marek Vasut722c9682015-07-17 02:07:12 +02003351 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003352
Marek Vasut722c9682015-07-17 02:07:12 +02003353 current_run = run_groups & ((1 <<
3354 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3355 run_groups = run_groups >>
3356 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003357
Marek Vasut722c9682015-07-17 02:07:12 +02003358 if (current_run == 0)
3359 continue;
3360
3361 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3362 SCC_MGR_GROUP_COUNTER_OFFSET);
3363 scc_mgr_zero_group(write_group, 0);
3364
Marek Vasut33c42bb2015-07-17 02:21:47 +02003365 for (read_group = write_group * rwdqs_ratio,
3366 read_test_bgn = 0;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003367 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003368 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003369 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasut33c42bb2015-07-17 02:21:47 +02003370 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3371 continue;
Marek Vasut722c9682015-07-17 02:07:12 +02003372
Marek Vasut33c42bb2015-07-17 02:21:47 +02003373 /* Calibrate the VFIFO */
3374 if (rw_mgr_mem_calibrate_vfifo(read_group,
3375 read_test_bgn))
3376 continue;
3377
Marek Vasutc452dcd2015-07-17 02:50:56 +02003378 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3379 return 0;
3380
3381 /* The group failed, we're done. */
3382 goto grp_failed;
3383 }
3384
3385 /* Calibrate the output side */
3386 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003387 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003388 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3389 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3390 continue;
3391
3392 /* Not needed in quick mode! */
3393 if (STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS)
3394 continue;
3395
Marek Vasutc452dcd2015-07-17 02:50:56 +02003396 /* Calibrate WRITEs */
Marek Vasutdb3a6062015-07-18 07:23:25 +02003397 if (!rw_mgr_mem_calibrate_writes(rank_bgn,
Marek Vasutc452dcd2015-07-17 02:50:56 +02003398 write_group, write_test_bgn))
3399 continue;
3400
Marek Vasut33c42bb2015-07-17 02:21:47 +02003401 group_failed = 1;
3402 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3403 return 0;
Marek Vasut722c9682015-07-17 02:07:12 +02003404 }
3405
Marek Vasutc452dcd2015-07-17 02:50:56 +02003406 /* Some group failed, we're done. */
3407 if (group_failed)
3408 goto grp_failed;
Marek Vasut4ac21612015-07-17 02:31:04 +02003409
Marek Vasutc452dcd2015-07-17 02:50:56 +02003410 for (read_group = write_group * rwdqs_ratio,
3411 read_test_bgn = 0;
3412 read_group < (write_group + 1) * rwdqs_ratio;
3413 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003414 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003415 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3416 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003417
Marek Vasut78cdd7d2015-07-18 05:58:44 +02003418 if (!rw_mgr_mem_calibrate_vfifo_end(read_group,
Marek Vasutc452dcd2015-07-17 02:50:56 +02003419 read_test_bgn))
3420 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003421
Marek Vasutc452dcd2015-07-17 02:50:56 +02003422 if (!(gbl->phy_debug_mode_flags & PHY_DEBUG_SWEEP_ALL_GROUPS))
3423 return 0;
Marek Vasut4ac21612015-07-17 02:31:04 +02003424
Marek Vasutc452dcd2015-07-17 02:50:56 +02003425 /* The group failed, we're done. */
3426 goto grp_failed;
Marek Vasut722c9682015-07-17 02:07:12 +02003427 }
3428
Marek Vasutc452dcd2015-07-17 02:50:56 +02003429 /* No group failed, continue as usual. */
3430 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003431
Marek Vasutc452dcd2015-07-17 02:50:56 +02003432grp_failed: /* A group failed, increment the counter. */
3433 failing_groups++;
Marek Vasut722c9682015-07-17 02:07:12 +02003434 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003435
Marek Vasut722c9682015-07-17 02:07:12 +02003436 /*
3437 * USER If there are any failing groups then report
3438 * the failure.
3439 */
3440 if (failing_groups != 0)
3441 return 0;
3442
Marek Vasutc50ae302015-07-17 02:40:21 +02003443 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3444 continue;
3445
Marek Vasut722c9682015-07-17 02:07:12 +02003446 /* Calibrate the LFIFO */
Marek Vasutc50ae302015-07-17 02:40:21 +02003447 if (!rw_mgr_mem_calibrate_lfifo())
3448 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003449 }
3450
3451 /*
3452 * Do not remove this line as it makes sure all of our decisions
3453 * have been applied.
3454 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003455 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003456 return 1;
3457}
3458
Marek Vasut23a040c2015-07-17 01:20:21 +02003459/**
3460 * run_mem_calibrate() - Perform memory calibration
3461 *
3462 * This function triggers the entire memory calibration procedure.
3463 */
3464static int run_mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003465{
Marek Vasut23a040c2015-07-17 01:20:21 +02003466 int pass;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003467
3468 debug("%s:%d\n", __func__, __LINE__);
3469
3470 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003471 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003472
Marek Vasut23a040c2015-07-17 01:20:21 +02003473 /* Stop tracking manager. */
3474 clrbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003475
Marek Vasut9fa9c902015-07-17 01:12:07 +02003476 phy_mgr_initialize();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003477 rw_mgr_mem_initialize();
3478
Marek Vasut23a040c2015-07-17 01:20:21 +02003479 /* Perform the actual memory calibration. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003480 pass = mem_calibrate();
3481
3482 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003483 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003484
Marek Vasut23a040c2015-07-17 01:20:21 +02003485 /* Handoff. */
3486 rw_mgr_mem_handoff();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003487 /*
Marek Vasut23a040c2015-07-17 01:20:21 +02003488 * In Hard PHY this is a 2-bit control:
3489 * 0: AFI Mux Select
3490 * 1: DDIO Mux Select
Dinh Nguyen3da42852015-06-02 22:52:49 -05003491 */
Marek Vasut23a040c2015-07-17 01:20:21 +02003492 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003493
Marek Vasut23a040c2015-07-17 01:20:21 +02003494 /* Start tracking manager. */
3495 setbits_le32(&sdr_ctrl->ctrl_cfg, 1 << 22);
3496
3497 return pass;
3498}
3499
3500/**
3501 * debug_mem_calibrate() - Report result of memory calibration
3502 * @pass: Value indicating whether calibration passed or failed
3503 *
3504 * This function reports the results of the memory calibration
3505 * and writes debug information into the register file.
3506 */
3507static void debug_mem_calibrate(int pass)
3508{
3509 uint32_t debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003510
3511 if (pass) {
3512 printf("%s: CALIBRATION PASSED\n", __FILE__);
3513
3514 gbl->fom_in /= 2;
3515 gbl->fom_out /= 2;
3516
3517 if (gbl->fom_in > 0xff)
3518 gbl->fom_in = 0xff;
3519
3520 if (gbl->fom_out > 0xff)
3521 gbl->fom_out = 0xff;
3522
3523 /* Update the FOM in the register file */
3524 debug_info = gbl->fom_in;
3525 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003526 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003527
Marek Vasut1273dd92015-07-12 21:05:08 +02003528 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3529 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003530 } else {
3531 printf("%s: CALIBRATION FAILED\n", __FILE__);
3532
3533 debug_info = gbl->error_stage;
3534 debug_info |= gbl->error_substage << 8;
3535 debug_info |= gbl->error_group << 16;
3536
Marek Vasut1273dd92015-07-12 21:05:08 +02003537 writel(debug_info, &sdr_reg_file->failing_stage);
3538 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3539 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003540
3541 /* Update the failing group/stage in the register file */
3542 debug_info = gbl->error_stage;
3543 debug_info |= gbl->error_substage << 8;
3544 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003545 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003546 }
3547
Marek Vasut23a040c2015-07-17 01:20:21 +02003548 printf("%s: Calibration complete\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003549}
3550
Marek Vasutbb064342015-07-19 06:12:42 +02003551/**
3552 * hc_initialize_rom_data() - Initialize ROM data
3553 *
3554 * Initialize ROM data.
3555 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003556static void hc_initialize_rom_data(void)
3557{
Marek Vasut04955cf2015-08-02 17:15:19 +02003558 unsigned int nelem = 0;
3559 const u32 *rom_init;
Marek Vasutbb064342015-07-19 06:12:42 +02003560 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003561
Marek Vasut04955cf2015-08-02 17:15:19 +02003562 socfpga_get_seq_inst_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003563 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003564 for (i = 0; i < nelem; i++)
3565 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003566
Marek Vasut04955cf2015-08-02 17:15:19 +02003567 socfpga_get_seq_ac_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003568 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003569 for (i = 0; i < nelem; i++)
3570 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003571}
3572
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003573/**
3574 * initialize_reg_file() - Initialize SDR register file
3575 *
3576 * Initialize SDR register file.
3577 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003578static void initialize_reg_file(void)
3579{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003580 /* Initialize the register file with the correct data */
Marek Vasut96fd4362015-08-02 19:26:55 +02003581 writel(misccfg->reg_file_init_seq_signature, &sdr_reg_file->signature);
Marek Vasut1273dd92015-07-12 21:05:08 +02003582 writel(0, &sdr_reg_file->debug_data_addr);
3583 writel(0, &sdr_reg_file->cur_stage);
3584 writel(0, &sdr_reg_file->fom);
3585 writel(0, &sdr_reg_file->failing_stage);
3586 writel(0, &sdr_reg_file->debug1);
3587 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003588}
3589
Marek Vasut2ca151f2015-07-19 06:14:04 +02003590/**
3591 * initialize_hps_phy() - Initialize HPS PHY
3592 *
3593 * Initialize HPS PHY.
3594 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003595static void initialize_hps_phy(void)
3596{
3597 uint32_t reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003598 /*
3599 * Tracking also gets configured here because it's in the
3600 * same register.
3601 */
3602 uint32_t trk_sample_count = 7500;
3603 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3604 /*
3605 * Format is number of outer loops in the 16 MSB, sample
3606 * count in 16 LSB.
3607 */
3608
3609 reg = 0;
3610 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3611 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3612 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3613 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3614 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3615 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3616 /*
3617 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3618 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3619 */
3620 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3621 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3622 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003623 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003624
3625 reg = 0;
3626 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3627 trk_sample_count >>
3628 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3629 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3630 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003631 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003632
3633 reg = 0;
3634 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3635 trk_long_idle_sample_count >>
3636 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003637 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003638}
3639
Marek Vasut880e46f2015-07-17 00:45:11 +02003640/**
3641 * initialize_tracking() - Initialize tracking
3642 *
3643 * Initialize the register file with usable initial data.
3644 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003645static void initialize_tracking(void)
3646{
Marek Vasut880e46f2015-07-17 00:45:11 +02003647 /*
3648 * Initialize the register file with the correct data.
3649 * Compute usable version of value in case we skip full
3650 * computation later.
3651 */
Marek Vasut160695d2015-08-02 19:10:58 +02003652 writel(DIV_ROUND_UP(iocfg->delay_per_opa_tap, iocfg->delay_per_dchain_tap) - 1,
Marek Vasut880e46f2015-07-17 00:45:11 +02003653 &sdr_reg_file->dtaps_per_ptap);
3654
3655 /* trk_sample_count */
3656 writel(7500, &sdr_reg_file->trk_sample_count);
3657
3658 /* longidle outer loop [15:0] */
3659 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003660
3661 /*
Marek Vasut880e46f2015-07-17 00:45:11 +02003662 * longidle sample count [31:24]
3663 * trfc, worst case of 933Mhz 4Gb [23:16]
3664 * trcd, worst case [15:8]
3665 * vfifo wait [7:0]
Dinh Nguyen3da42852015-06-02 22:52:49 -05003666 */
Marek Vasut880e46f2015-07-17 00:45:11 +02003667 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3668 &sdr_reg_file->delays);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003669
Marek Vasut880e46f2015-07-17 00:45:11 +02003670 /* mux delay */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003671 writel((rwcfg->idle << 24) | (rwcfg->activate_1 << 16) |
3672 (rwcfg->sgle_read << 8) | (rwcfg->precharge_all << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003673 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003674
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003675 writel(rwcfg->mem_if_read_dqs_width,
Marek Vasut880e46f2015-07-17 00:45:11 +02003676 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003677
Marek Vasut880e46f2015-07-17 00:45:11 +02003678 /* trefi [7:0] */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003679 writel((rwcfg->refresh_all << 24) | (1000 << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003680 &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003681}
3682
3683int sdram_calibration_full(void)
3684{
3685 struct param_type my_param;
3686 struct gbl_type my_gbl;
3687 uint32_t pass;
Marek Vasut84e0b0c2015-07-17 01:05:36 +02003688
3689 memset(&my_param, 0, sizeof(my_param));
3690 memset(&my_gbl, 0, sizeof(my_gbl));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003691
3692 param = &my_param;
3693 gbl = &my_gbl;
3694
Marek Vasutd718a262015-08-02 18:12:08 +02003695 rwcfg = socfpga_get_sdram_rwmgr_config();
Marek Vasut10c14262015-08-02 19:00:23 +02003696 iocfg = socfpga_get_sdram_io_config();
Marek Vasut042ff2d2015-08-02 19:18:47 +02003697 misccfg = socfpga_get_sdram_misc_config();
Marek Vasutd718a262015-08-02 18:12:08 +02003698
Dinh Nguyen3da42852015-06-02 22:52:49 -05003699 /* Set the calibration enabled by default */
3700 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3701 /*
3702 * Only sweep all groups (regardless of fail state) by default
3703 * Set enabled read test by default.
3704 */
3705#if DISABLE_GUARANTEED_READ
3706 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3707#endif
3708 /* Initialize the register file */
3709 initialize_reg_file();
3710
3711 /* Initialize any PHY CSR */
3712 initialize_hps_phy();
3713
3714 scc_mgr_initialize();
3715
3716 initialize_tracking();
3717
Dinh Nguyen3da42852015-06-02 22:52:49 -05003718 printf("%s: Preparing to start memory calibration\n", __FILE__);
3719
3720 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut23f62b32015-07-13 01:05:27 +02003721 debug_cond(DLEVEL == 1,
3722 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003723 rwcfg->mem_number_of_ranks, rwcfg->mem_number_of_cs_per_dimm,
3724 rwcfg->mem_dq_per_read_dqs, rwcfg->mem_dq_per_write_dqs,
3725 rwcfg->mem_virtual_groups_per_read_dqs,
3726 rwcfg->mem_virtual_groups_per_write_dqs);
Marek Vasut23f62b32015-07-13 01:05:27 +02003727 debug_cond(DLEVEL == 1,
3728 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003729 rwcfg->mem_if_read_dqs_width, rwcfg->mem_if_write_dqs_width,
3730 rwcfg->mem_data_width, rwcfg->mem_data_mask_width,
Marek Vasut160695d2015-08-02 19:10:58 +02003731 iocfg->delay_per_opa_tap, iocfg->delay_per_dchain_tap);
Marek Vasut23f62b32015-07-13 01:05:27 +02003732 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
Marek Vasut160695d2015-08-02 19:10:58 +02003733 iocfg->delay_per_dqs_en_dchain_tap, iocfg->dll_chain_length);
Marek Vasut23f62b32015-07-13 01:05:27 +02003734 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003735 iocfg->dqs_en_phase_max, iocfg->dqdqs_out_phase_max,
3736 iocfg->dqs_en_delay_max, iocfg->dqs_in_delay_max);
Marek Vasut23f62b32015-07-13 01:05:27 +02003737 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003738 iocfg->io_in_delay_max, iocfg->io_out1_delay_max,
3739 iocfg->io_out2_delay_max);
Marek Vasut23f62b32015-07-13 01:05:27 +02003740 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
Marek Vasut160695d2015-08-02 19:10:58 +02003741 iocfg->dqs_in_reserve, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003742
3743 hc_initialize_rom_data();
3744
3745 /* update info for sims */
3746 reg_file_set_stage(CAL_STAGE_NIL);
3747 reg_file_set_group(0);
3748
3749 /*
3750 * Load global needed for those actions that require
3751 * some dynamic calibration support.
3752 */
3753 dyn_calib_steps = STATIC_CALIB_STEPS;
3754 /*
3755 * Load global to allow dynamic selection of delay loop settings
3756 * based on calibration mode.
3757 */
3758 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3759 skip_delay_mask = 0xff;
3760 else
3761 skip_delay_mask = 0x0;
3762
3763 pass = run_mem_calibrate();
Marek Vasut23a040c2015-07-17 01:20:21 +02003764 debug_mem_calibrate(pass);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003765 return pass;
3766}