blob: 5e7a943b680c9b0a8776e064655c98c61d862768 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Dinh Nguyen3da42852015-06-02 22:52:49 -05002/*
3 * Copyright Altera Corporation (C) 2012-2015
Dinh Nguyen3da42852015-06-02 22:52:49 -05004 */
5
6#include <common.h>
7#include <asm/io.h>
8#include <asm/arch/sdram.h>
Marek Vasut04372fb2015-07-18 02:46:56 +02009#include <errno.h>
Dinh Nguyen3da42852015-06-02 22:52:49 -050010#include "sequencer.h"
Marek Vasut9c76df52015-08-02 16:55:45 +020011
Dinh Nguyen3da42852015-06-02 22:52:49 -050012static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut139823e2015-08-02 19:47:01 +020013 (struct socfpga_sdr_rw_load_manager *)
14 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen3da42852015-06-02 22:52:49 -050015static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut139823e2015-08-02 19:47:01 +020016 (struct socfpga_sdr_rw_load_jump_manager *)
17 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050018static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasuta1c654a2015-07-12 18:31:05 +020019 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050020static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasut139823e2015-08-02 19:47:01 +020021 (struct socfpga_sdr_scc_mgr *)
22 (SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050023static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasut1bc6f142015-07-12 18:54:37 +020024 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050025static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasut139823e2015-08-02 19:47:01 +020026 (struct socfpga_phy_mgr_cfg *)
27 (SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen3da42852015-06-02 22:52:49 -050028static struct socfpga_data_mgr *data_mgr =
Marek Vasutc4815f72015-07-12 19:03:33 +020029 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Marek Vasut6cb9f162015-07-12 20:49:39 +020030static struct socfpga_sdr_ctrl *sdr_ctrl =
31 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
32
Marek Vasutd718a262015-08-02 18:12:08 +020033const struct socfpga_sdram_rw_mgr_config *rwcfg;
Marek Vasut10c14262015-08-02 19:00:23 +020034const struct socfpga_sdram_io_config *iocfg;
Marek Vasut042ff2d2015-08-02 19:18:47 +020035const struct socfpga_sdram_misc_config *misccfg;
Marek Vasutd718a262015-08-02 18:12:08 +020036
Dinh Nguyen3da42852015-06-02 22:52:49 -050037#define DELTA_D 1
Dinh Nguyen3da42852015-06-02 22:52:49 -050038
39/*
40 * In order to reduce ROM size, most of the selectable calibration steps are
41 * decided at compile time based on the user's calibration mode selection,
42 * as captured by the STATIC_CALIB_STEPS selection below.
43 *
44 * However, to support simulation-time selection of fast simulation mode, where
45 * we skip everything except the bare minimum, we need a few of the steps to
46 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
47 * check, which is based on the rtl-supplied value, or we dynamically compute
48 * the value to use based on the dynamically-chosen calibration mode
49 */
50
51#define DLEVEL 0
52#define STATIC_IN_RTL_SIM 0
53#define STATIC_SKIP_DELAY_LOOPS 0
54
55#define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
56 STATIC_SKIP_DELAY_LOOPS)
57
58/* calibration steps requested by the rtl */
Marek Vasut85f76622016-04-05 11:18:38 +020059static u16 dyn_calib_steps;
Dinh Nguyen3da42852015-06-02 22:52:49 -050060
61/*
62 * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
63 * instead of static, we use boolean logic to select between
64 * non-skip and skip values
65 *
66 * The mask is set to include all bits when not-skipping, but is
67 * zero when skipping
68 */
69
Marek Vasut85f76622016-04-05 11:18:38 +020070static u16 skip_delay_mask; /* mask off bits when skipping/not-skipping */
Dinh Nguyen3da42852015-06-02 22:52:49 -050071
72#define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
73 ((non_skip_value) & skip_delay_mask)
74
Marek Vasut85f76622016-04-05 11:18:38 +020075static struct gbl_type *gbl;
76static struct param_type *param;
Dinh Nguyen3da42852015-06-02 22:52:49 -050077
Marek Vasut5ded7322015-08-02 19:42:26 +020078static void set_failing_group_stage(u32 group, u32 stage,
79 u32 substage)
Dinh Nguyen3da42852015-06-02 22:52:49 -050080{
81 /*
82 * Only set the global stage if there was not been any other
83 * failing group
84 */
85 if (gbl->error_stage == CAL_STAGE_NIL) {
86 gbl->error_substage = substage;
87 gbl->error_stage = stage;
88 gbl->error_group = group;
89 }
90}
91
Marek Vasut2c0d2d92015-07-12 21:10:24 +020092static void reg_file_set_group(u16 set_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -050093{
Marek Vasut2c0d2d92015-07-12 21:10:24 +020094 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen3da42852015-06-02 22:52:49 -050095}
96
Marek Vasut2c0d2d92015-07-12 21:10:24 +020097static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -050098{
Marek Vasut2c0d2d92015-07-12 21:10:24 +020099 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500100}
101
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200102static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500103{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200104 set_sub_stage &= 0xff;
105 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500106}
107
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200108/**
109 * phy_mgr_initialize() - Initialize PHY Manager
110 *
111 * Initialize PHY Manager.
112 */
Marek Vasut9fa9c902015-07-17 01:12:07 +0200113static void phy_mgr_initialize(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500114{
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200115 u32 ratio;
116
Dinh Nguyen3da42852015-06-02 22:52:49 -0500117 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200118 /* Calibration has control over path to memory */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500119 /*
120 * In Hard PHY this is a 2-bit control:
121 * 0: AFI Mux Select
122 * 1: DDIO Mux Select
123 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200124 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500125
126 /* USER memory clock is not stable we begin initialization */
Marek Vasut1273dd92015-07-12 21:05:08 +0200127 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500128
129 /* USER calibration status all set to zero */
Marek Vasut1273dd92015-07-12 21:05:08 +0200130 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500131
Marek Vasut1273dd92015-07-12 21:05:08 +0200132 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500133
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200134 /* Init params only if we do NOT skip calibration. */
135 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
136 return;
137
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200138 ratio = rwcfg->mem_dq_per_read_dqs /
139 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200140 param->read_correct_mask_vg = (1 << ratio) - 1;
141 param->write_correct_mask_vg = (1 << ratio) - 1;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200142 param->read_correct_mask = (1 << rwcfg->mem_dq_per_read_dqs) - 1;
143 param->write_correct_mask = (1 << rwcfg->mem_dq_per_write_dqs) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500144}
145
Marek Vasut080bf642015-07-20 08:15:57 +0200146/**
147 * set_rank_and_odt_mask() - Set Rank and ODT mask
148 * @rank: Rank mask
149 * @odt_mode: ODT mode, OFF or READ_WRITE
150 *
151 * Set Rank and ODT mask (On-Die Termination).
152 */
Marek Vasutb2dfd102015-07-20 08:03:11 +0200153static void set_rank_and_odt_mask(const u32 rank, const u32 odt_mode)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500154{
Marek Vasutb2dfd102015-07-20 08:03:11 +0200155 u32 odt_mask_0 = 0;
156 u32 odt_mask_1 = 0;
157 u32 cs_and_odt_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500158
Marek Vasutb2dfd102015-07-20 08:03:11 +0200159 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
160 odt_mask_0 = 0x0;
161 odt_mask_1 = 0x0;
162 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200163 switch (rwcfg->mem_number_of_ranks) {
Marek Vasut287cdf62015-07-20 08:09:05 +0200164 case 1: /* 1 Rank */
165 /* Read: ODT = 0 ; Write: ODT = 1 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500166 odt_mask_0 = 0x0;
167 odt_mask_1 = 0x1;
Marek Vasut287cdf62015-07-20 08:09:05 +0200168 break;
169 case 2: /* 2 Ranks */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200170 if (rwcfg->mem_number_of_cs_per_dimm == 1) {
Marek Vasut080bf642015-07-20 08:15:57 +0200171 /*
172 * - Dual-Slot , Single-Rank (1 CS per DIMM)
173 * OR
174 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
175 *
176 * Since MEM_NUMBER_OF_RANKS is 2, they
177 * are both single rank with 2 CS each
178 * (special for RDIMM).
179 *
Dinh Nguyen3da42852015-06-02 22:52:49 -0500180 * Read: Turn on ODT on the opposite rank
181 * Write: Turn on ODT on all ranks
182 */
183 odt_mask_0 = 0x3 & ~(1 << rank);
184 odt_mask_1 = 0x3;
185 } else {
186 /*
Marek Vasut080bf642015-07-20 08:15:57 +0200187 * - Single-Slot , Dual-Rank (2 CS per DIMM)
188 *
189 * Read: Turn on ODT off on all ranks
190 * Write: Turn on ODT on active rank
Dinh Nguyen3da42852015-06-02 22:52:49 -0500191 */
192 odt_mask_0 = 0x0;
193 odt_mask_1 = 0x3 & (1 << rank);
194 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200195 break;
196 case 4: /* 4 Ranks */
197 /* Read:
Dinh Nguyen3da42852015-06-02 22:52:49 -0500198 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500199 * | ODT |
200 * Read From +-----------------------+
201 * Rank | 3 | 2 | 1 | 0 |
202 * ----------+-----+-----+-----+-----+
203 * 0 | 0 | 1 | 0 | 0 |
204 * 1 | 1 | 0 | 0 | 0 |
205 * 2 | 0 | 0 | 0 | 1 |
206 * 3 | 0 | 0 | 1 | 0 |
207 * ----------+-----+-----+-----+-----+
208 *
209 * Write:
210 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500211 * | ODT |
212 * Write To +-----------------------+
213 * Rank | 3 | 2 | 1 | 0 |
214 * ----------+-----+-----+-----+-----+
215 * 0 | 0 | 1 | 0 | 1 |
216 * 1 | 1 | 0 | 1 | 0 |
217 * 2 | 0 | 1 | 0 | 1 |
218 * 3 | 1 | 0 | 1 | 0 |
219 * ----------+-----+-----+-----+-----+
220 */
221 switch (rank) {
222 case 0:
223 odt_mask_0 = 0x4;
224 odt_mask_1 = 0x5;
225 break;
226 case 1:
227 odt_mask_0 = 0x8;
228 odt_mask_1 = 0xA;
229 break;
230 case 2:
231 odt_mask_0 = 0x1;
232 odt_mask_1 = 0x5;
233 break;
234 case 3:
235 odt_mask_0 = 0x2;
236 odt_mask_1 = 0xA;
237 break;
238 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200239 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500240 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500241 }
242
Marek Vasutb2dfd102015-07-20 08:03:11 +0200243 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
244 ((0xFF & odt_mask_0) << 8) |
245 ((0xFF & odt_mask_1) << 16);
Marek Vasut1273dd92015-07-12 21:05:08 +0200246 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
247 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500248}
249
Marek Vasutc76976d2015-07-12 22:28:33 +0200250/**
251 * scc_mgr_set() - Set SCC Manager register
252 * @off: Base offset in SCC Manager space
253 * @grp: Read/Write group
254 * @val: Value to be set
255 *
256 * This function sets the SCC Manager (Scan Chain Control Manager) register.
257 */
258static void scc_mgr_set(u32 off, u32 grp, u32 val)
259{
260 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
261}
262
Marek Vasute893f4d2015-07-20 07:16:42 +0200263/**
264 * scc_mgr_initialize() - Initialize SCC Manager registers
265 *
266 * Initialize SCC Manager registers.
267 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500268static void scc_mgr_initialize(void)
269{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500270 /*
Marek Vasute893f4d2015-07-20 07:16:42 +0200271 * Clear register file for HPS. 16 (2^4) is the size of the
272 * full register file in the scc mgr:
273 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
274 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500275 */
Marek Vasutc76976d2015-07-12 22:28:33 +0200276 int i;
Marek Vasute893f4d2015-07-20 07:16:42 +0200277
Dinh Nguyen3da42852015-06-02 22:52:49 -0500278 for (i = 0; i < 16; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200279 debug_cond(DLEVEL >= 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -0500280 __func__, __LINE__, i);
Marek Vasut8e9e62c2016-04-04 17:28:16 +0200281 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500282 }
283}
284
Marek Vasut5ded7322015-08-02 19:42:26 +0200285static void scc_mgr_set_dqdqs_output_phase(u32 write_group, u32 phase)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200286{
Marek Vasutc76976d2015-07-12 22:28:33 +0200287 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200288}
289
Marek Vasut5ded7322015-08-02 19:42:26 +0200290static void scc_mgr_set_dqs_bus_in_delay(u32 read_group, u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500291{
Marek Vasutc76976d2015-07-12 22:28:33 +0200292 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500293}
294
Marek Vasut5ded7322015-08-02 19:42:26 +0200295static void scc_mgr_set_dqs_en_phase(u32 read_group, u32 phase)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500296{
Marek Vasutc76976d2015-07-12 22:28:33 +0200297 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500298}
299
Marek Vasut5ded7322015-08-02 19:42:26 +0200300static void scc_mgr_set_dqs_en_delay(u32 read_group, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200301{
Marek Vasutc76976d2015-07-12 22:28:33 +0200302 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200303}
304
Marek Vasut70ed80a2016-04-04 21:16:18 +0200305static void scc_mgr_set_dq_in_delay(u32 dq_in_group, u32 delay)
306{
307 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
308}
309
Marek Vasut5ded7322015-08-02 19:42:26 +0200310static void scc_mgr_set_dqs_io_in_delay(u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200311{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200312 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200313 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200314}
315
Marek Vasut70ed80a2016-04-04 21:16:18 +0200316static void scc_mgr_set_dm_in_delay(u32 dm, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200317{
Marek Vasut70ed80a2016-04-04 21:16:18 +0200318 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
319 rwcfg->mem_dq_per_write_dqs + 1 + dm,
320 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200321}
322
Marek Vasut5ded7322015-08-02 19:42:26 +0200323static void scc_mgr_set_dq_out1_delay(u32 dq_in_group, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200324{
Marek Vasutc76976d2015-07-12 22:28:33 +0200325 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200326}
327
Marek Vasut5ded7322015-08-02 19:42:26 +0200328static void scc_mgr_set_dqs_out1_delay(u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200329{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200330 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200331 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200332}
333
Marek Vasut5ded7322015-08-02 19:42:26 +0200334static void scc_mgr_set_dm_out1_delay(u32 dm, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200335{
Marek Vasutc76976d2015-07-12 22:28:33 +0200336 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200337 rwcfg->mem_dq_per_write_dqs + 1 + dm,
Marek Vasutc76976d2015-07-12 22:28:33 +0200338 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200339}
340
341/* load up dqs config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200342static void scc_mgr_load_dqs(u32 dqs)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200343{
344 writel(dqs, &sdr_scc_mgr->dqs_ena);
345}
346
347/* load up dqs io config settings */
348static void scc_mgr_load_dqs_io(void)
349{
350 writel(0, &sdr_scc_mgr->dqs_io_ena);
351}
352
353/* load up dq config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200354static void scc_mgr_load_dq(u32 dq_in_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200355{
356 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
357}
358
359/* load up dm config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200360static void scc_mgr_load_dm(u32 dm)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200361{
362 writel(dm, &sdr_scc_mgr->dm_ena);
363}
364
Marek Vasut0b69b802015-07-12 23:25:21 +0200365/**
366 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
367 * @off: Base offset in SCC Manager space
368 * @grp: Read/Write group
369 * @val: Value to be set
370 * @update: If non-zero, trigger SCC Manager update for all ranks
371 *
372 * This function sets the SCC Manager (Scan Chain Control Manager) register
373 * and optionally triggers the SCC update for all ranks.
374 */
375static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
376 const int update)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500377{
Marek Vasut0b69b802015-07-12 23:25:21 +0200378 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500379
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200380 for (r = 0; r < rwcfg->mem_number_of_ranks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500381 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut0b69b802015-07-12 23:25:21 +0200382 scc_mgr_set(off, grp, val);
Marek Vasut162d60e2015-07-12 23:14:33 +0200383
Marek Vasut0b69b802015-07-12 23:25:21 +0200384 if (update || (r == 0)) {
385 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasut1273dd92015-07-12 21:05:08 +0200386 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500387 }
388 }
389}
390
Marek Vasut0b69b802015-07-12 23:25:21 +0200391static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
392{
393 /*
394 * USER although the h/w doesn't support different phases per
395 * shadow register, for simplicity our scc manager modeling
396 * keeps different phase settings per shadow reg, and it's
397 * important for us to keep them in sync to match h/w.
398 * for efficiency, the scan chain update should occur only
399 * once to sr0.
400 */
401 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
402 read_group, phase, 0);
403}
404
Marek Vasut5ded7322015-08-02 19:42:26 +0200405static void scc_mgr_set_dqdqs_output_phase_all_ranks(u32 write_group,
406 u32 phase)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500407{
Marek Vasut0b69b802015-07-12 23:25:21 +0200408 /*
409 * USER although the h/w doesn't support different phases per
410 * shadow register, for simplicity our scc manager modeling
411 * keeps different phase settings per shadow reg, and it's
412 * important for us to keep them in sync to match h/w.
413 * for efficiency, the scan chain update should occur only
414 * once to sr0.
415 */
416 scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
417 write_group, phase, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500418}
419
Marek Vasut5ded7322015-08-02 19:42:26 +0200420static void scc_mgr_set_dqs_en_delay_all_ranks(u32 read_group,
421 u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500422{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500423 /*
424 * In shadow register mode, the T11 settings are stored in
425 * registers in the core, which are updated by the DQS_ENA
426 * signals. Not issuing the SCC_MGR_UPD command allows us to
427 * save lots of rank switching overhead, by calling
428 * select_shadow_regs_for_update with update_scan_chains
429 * set to 0.
430 */
Marek Vasut0b69b802015-07-12 23:25:21 +0200431 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
432 read_group, delay, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500433}
434
Marek Vasut5be355c2015-07-12 23:39:06 +0200435/**
436 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
437 * @write_group: Write group
438 * @delay: Delay value
439 *
440 * This function sets the OCT output delay in SCC manager.
441 */
442static void scc_mgr_set_oct_out1_delay(const u32 write_group, const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500443{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200444 const int ratio = rwcfg->mem_if_read_dqs_width /
445 rwcfg->mem_if_write_dqs_width;
Marek Vasut5be355c2015-07-12 23:39:06 +0200446 const int base = write_group * ratio;
447 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500448 /*
449 * Load the setting in the SCC manager
450 * Although OCT affects only write data, the OCT delay is controlled
451 * by the DQS logic block which is instantiated once per read group.
452 * For protocols where a write group consists of multiple read groups,
453 * the setting must be set multiple times.
454 */
Marek Vasut5be355c2015-07-12 23:39:06 +0200455 for (i = 0; i < ratio; i++)
456 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500457}
458
Marek Vasut37a37ca2015-07-19 01:32:55 +0200459/**
460 * scc_mgr_set_hhp_extras() - Set HHP extras.
461 *
462 * Load the fixed setting in the SCC manager HHP extras.
463 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500464static void scc_mgr_set_hhp_extras(void)
465{
466 /*
467 * Load the fixed setting in the SCC manager
Marek Vasut37a37ca2015-07-19 01:32:55 +0200468 * bits: 0:0 = 1'b1 - DQS bypass
469 * bits: 1:1 = 1'b1 - DQ bypass
470 * bits: 4:2 = 3'b001 - rfifo_mode
471 * bits: 6:5 = 2'b01 - rfifo clock_select
472 * bits: 7:7 = 1'b0 - separate gating from ungating setting
473 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen3da42852015-06-02 22:52:49 -0500474 */
Marek Vasut37a37ca2015-07-19 01:32:55 +0200475 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
476 (1 << 2) | (1 << 1) | (1 << 0);
477 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
478 SCC_MGR_HHP_GLOBALS_OFFSET |
479 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500480
Marek Vasutea9aa242016-04-04 21:21:05 +0200481 debug_cond(DLEVEL >= 1, "%s:%d Setting HHP Extras\n",
Marek Vasut37a37ca2015-07-19 01:32:55 +0200482 __func__, __LINE__);
483 writel(value, addr);
Marek Vasutea9aa242016-04-04 21:21:05 +0200484 debug_cond(DLEVEL >= 1, "%s:%d Done Setting HHP Extras\n",
Marek Vasut37a37ca2015-07-19 01:32:55 +0200485 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500486}
487
Marek Vasutf42af352015-07-20 04:41:53 +0200488/**
489 * scc_mgr_zero_all() - Zero all DQS config
490 *
491 * Zero all DQS config.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500492 */
493static void scc_mgr_zero_all(void)
494{
Marek Vasutf42af352015-07-20 04:41:53 +0200495 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500496
497 /*
498 * USER Zero all DQS config settings, across all groups and all
499 * shadow registers
500 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200501 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf42af352015-07-20 04:41:53 +0200502 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200503 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500504 /*
505 * The phases actually don't exist on a per-rank basis,
506 * but there's no harm updating them several times, so
507 * let's keep the code simple.
508 */
Marek Vasut160695d2015-08-02 19:10:58 +0200509 scc_mgr_set_dqs_bus_in_delay(i, iocfg->dqs_in_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500510 scc_mgr_set_dqs_en_phase(i, 0);
511 scc_mgr_set_dqs_en_delay(i, 0);
512 }
513
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200514 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500515 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasutf42af352015-07-20 04:41:53 +0200516 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200517 scc_mgr_set_oct_out1_delay(i, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500518 }
519 }
520
Marek Vasutf42af352015-07-20 04:41:53 +0200521 /* Multicast to all DQS group enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200522 writel(0xff, &sdr_scc_mgr->dqs_ena);
523 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500524}
525
Marek Vasutc5c5f532015-07-17 02:06:20 +0200526/**
527 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
528 * @write_group: Write group
529 *
530 * Set bypass mode and trigger SCC update.
531 */
532static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500533{
Marek Vasutc5c5f532015-07-17 02:06:20 +0200534 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200535 writel(0xff, &sdr_scc_mgr->dq_ena);
536 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500537
Marek Vasutc5c5f532015-07-17 02:06:20 +0200538 /* Update current DQS IO enable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200539 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500540
Marek Vasutc5c5f532015-07-17 02:06:20 +0200541 /* Update the DQS logic. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200542 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500543
Marek Vasutc5c5f532015-07-17 02:06:20 +0200544 /* Hit update. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200545 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500546}
547
Marek Vasut5e837892015-07-13 00:30:09 +0200548/**
549 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
550 * @write_group: Write group
551 *
552 * Load DQS settings for Write Group, do not trigger SCC update.
553 */
554static void scc_mgr_load_dqs_for_write_group(const u32 write_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200555{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200556 const int ratio = rwcfg->mem_if_read_dqs_width /
557 rwcfg->mem_if_write_dqs_width;
Marek Vasut5e837892015-07-13 00:30:09 +0200558 const int base = write_group * ratio;
559 int i;
Marek Vasut5ff825b2015-07-12 22:11:55 +0200560 /*
Marek Vasut5e837892015-07-13 00:30:09 +0200561 * Load the setting in the SCC manager
Marek Vasut5ff825b2015-07-12 22:11:55 +0200562 * Although OCT affects only write data, the OCT delay is controlled
563 * by the DQS logic block which is instantiated once per read group.
564 * For protocols where a write group consists of multiple read groups,
Marek Vasut5e837892015-07-13 00:30:09 +0200565 * the setting must be set multiple times.
Marek Vasut5ff825b2015-07-12 22:11:55 +0200566 */
Marek Vasut5e837892015-07-13 00:30:09 +0200567 for (i = 0; i < ratio; i++)
568 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200569}
570
Marek Vasutd41ea932015-07-20 08:41:04 +0200571/**
572 * scc_mgr_zero_group() - Zero all configs for a group
573 *
574 * Zero DQ, DM, DQS and OCT configs for a group.
575 */
576static void scc_mgr_zero_group(const u32 write_group, const int out_only)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500577{
Marek Vasutd41ea932015-07-20 08:41:04 +0200578 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500579
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200580 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutd41ea932015-07-20 08:41:04 +0200581 r += NUM_RANKS_PER_SHADOW_REG) {
582 /* Zero all DQ config settings. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200583 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200584 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500585 if (!out_only)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200586 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500587 }
588
Marek Vasutd41ea932015-07-20 08:41:04 +0200589 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200590 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500591
Marek Vasutd41ea932015-07-20 08:41:04 +0200592 /* Zero all DM config settings. */
Marek Vasut70ed80a2016-04-04 21:16:18 +0200593 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
594 if (!out_only)
595 scc_mgr_set_dm_in_delay(i, 0);
Marek Vasut07aee5b2015-07-12 22:07:33 +0200596 scc_mgr_set_dm_out1_delay(i, 0);
Marek Vasut70ed80a2016-04-04 21:16:18 +0200597 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500598
Marek Vasutd41ea932015-07-20 08:41:04 +0200599 /* Multicast to all DM enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200600 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500601
Marek Vasutd41ea932015-07-20 08:41:04 +0200602 /* Zero all DQS IO settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500603 if (!out_only)
Marek Vasut32675242015-07-17 06:07:13 +0200604 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasutd41ea932015-07-20 08:41:04 +0200605
606 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200607 scc_mgr_set_dqs_out1_delay(iocfg->dqs_out_reserve);
608 scc_mgr_set_oct_out1_delay(write_group, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500609 scc_mgr_load_dqs_for_write_group(write_group);
610
Marek Vasutd41ea932015-07-20 08:41:04 +0200611 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasut1273dd92015-07-12 21:05:08 +0200612 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500613
Marek Vasutd41ea932015-07-20 08:41:04 +0200614 /* Hit update to zero everything. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200615 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500616 }
617}
618
Dinh Nguyen3da42852015-06-02 22:52:49 -0500619/*
620 * apply and load a particular input delay for the DQ pins in a group
621 * group_bgn is the index of the first dq pin (in the write group)
622 */
Marek Vasut5ded7322015-08-02 19:42:26 +0200623static void scc_mgr_apply_group_dq_in_delay(u32 group_bgn, u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500624{
Marek Vasut5ded7322015-08-02 19:42:26 +0200625 u32 i, p;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500626
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200627 for (i = 0, p = group_bgn; i < rwcfg->mem_dq_per_read_dqs; i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200628 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500629 scc_mgr_load_dq(p);
630 }
631}
632
Marek Vasut300c2e62015-07-17 05:42:49 +0200633/**
634 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the DQ pins in a group
635 * @delay: Delay value
636 *
637 * Apply and load a particular output delay for the DQ pins in a group.
638 */
639static void scc_mgr_apply_group_dq_out1_delay(const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500640{
Marek Vasut300c2e62015-07-17 05:42:49 +0200641 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500642
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200643 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut300c2e62015-07-17 05:42:49 +0200644 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500645 scc_mgr_load_dq(i);
646 }
647}
648
649/* apply and load a particular output delay for the DM pins in a group */
Marek Vasut5ded7322015-08-02 19:42:26 +0200650static void scc_mgr_apply_group_dm_out1_delay(u32 delay1)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500651{
Marek Vasut5ded7322015-08-02 19:42:26 +0200652 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500653
654 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200655 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500656 scc_mgr_load_dm(i);
657 }
658}
659
660
661/* apply and load delay on both DQS and OCT out1 */
Marek Vasut5ded7322015-08-02 19:42:26 +0200662static void scc_mgr_apply_group_dqs_io_and_oct_out1(u32 write_group,
663 u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500664{
Marek Vasut32675242015-07-17 06:07:13 +0200665 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500666 scc_mgr_load_dqs_io();
667
668 scc_mgr_set_oct_out1_delay(write_group, delay);
669 scc_mgr_load_dqs_for_write_group(write_group);
670}
671
Marek Vasut5cb1b502015-07-17 05:33:28 +0200672/**
673 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side: DQ, DM, DQS, OCT
674 * @write_group: Write group
675 * @delay: Delay value
676 *
677 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
678 */
Marek Vasut8eccde32015-07-17 05:30:14 +0200679static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
Marek Vasut8eccde32015-07-17 05:30:14 +0200680 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500681{
Marek Vasut8eccde32015-07-17 05:30:14 +0200682 u32 i, new_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500683
Marek Vasut8eccde32015-07-17 05:30:14 +0200684 /* DQ shift */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200685 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500686 scc_mgr_load_dq(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500687
Marek Vasut8eccde32015-07-17 05:30:14 +0200688 /* DM shift */
689 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500690 scc_mgr_load_dm(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500691
Marek Vasut5cb1b502015-07-17 05:33:28 +0200692 /* DQS shift */
693 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200694 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200695 debug_cond(DLEVEL >= 1,
Marek Vasut5cb1b502015-07-17 05:33:28 +0200696 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
697 __func__, __LINE__, write_group, delay, new_delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200698 iocfg->io_out2_delay_max,
699 new_delay - iocfg->io_out2_delay_max);
700 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200701 scc_mgr_set_dqs_out1_delay(new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500702 }
703
704 scc_mgr_load_dqs_io();
705
Marek Vasut5cb1b502015-07-17 05:33:28 +0200706 /* OCT shift */
707 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200708 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200709 debug_cond(DLEVEL >= 1,
Marek Vasut5cb1b502015-07-17 05:33:28 +0200710 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
711 __func__, __LINE__, write_group, delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200712 new_delay, iocfg->io_out2_delay_max,
713 new_delay - iocfg->io_out2_delay_max);
714 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200715 scc_mgr_set_oct_out1_delay(write_group, new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500716 }
717
718 scc_mgr_load_dqs_for_write_group(write_group);
719}
720
Marek Vasutf51a7d32015-07-19 02:18:21 +0200721/**
722 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side to all ranks
723 * @write_group: Write group
724 * @delay: Delay value
725 *
726 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500727 */
Marek Vasutf51a7d32015-07-19 02:18:21 +0200728static void
729scc_mgr_apply_group_all_out_delay_add_all_ranks(const u32 write_group,
730 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500731{
Marek Vasutf51a7d32015-07-19 02:18:21 +0200732 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500733
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200734 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf51a7d32015-07-19 02:18:21 +0200735 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200736 scc_mgr_apply_group_all_out_delay_add(write_group, delay);
Marek Vasut1273dd92015-07-12 21:05:08 +0200737 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500738 }
739}
740
Marek Vasutf936f942015-07-26 11:07:19 +0200741/**
742 * set_jump_as_return() - Return instruction optimization
743 *
744 * Optimization used to recover some slots in ddr3 inst_rom could be
745 * applied to other protocols if we wanted to
746 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500747static void set_jump_as_return(void)
748{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500749 /*
Marek Vasutf936f942015-07-26 11:07:19 +0200750 * To save space, we replace return with jump to special shared
Dinh Nguyen3da42852015-06-02 22:52:49 -0500751 * RETURN instruction so we set the counter to large value so that
Marek Vasutf936f942015-07-26 11:07:19 +0200752 * we always jump.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500753 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200754 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200755 writel(rwcfg->rreturn, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500756}
757
Marek Vasut3de96222015-07-26 11:46:04 +0200758/**
759 * delay_for_n_mem_clocks() - Delay for N memory clocks
760 * @clocks: Length of the delay
761 *
762 * Delay for N memory clocks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500763 */
Marek Vasut90a584b2015-07-26 11:11:28 +0200764static void delay_for_n_mem_clocks(const u32 clocks)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500765{
Marek Vasut90a584b2015-07-26 11:11:28 +0200766 u32 afi_clocks;
Marek Vasut6a39be62015-07-26 11:42:53 +0200767 u16 c_loop;
768 u8 inner;
769 u8 outer;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500770
771 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
772
Marek Vasutcbcaf462015-07-26 11:34:09 +0200773 /* Scale (rounding up) to get afi clocks. */
Marek Vasut96fd4362015-08-02 19:26:55 +0200774 afi_clocks = DIV_ROUND_UP(clocks, misccfg->afi_rate_ratio);
Marek Vasutcbcaf462015-07-26 11:34:09 +0200775 if (afi_clocks) /* Temporary underflow protection */
776 afi_clocks--;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500777
778 /*
Marek Vasut90a584b2015-07-26 11:11:28 +0200779 * Note, we don't bother accounting for being off a little
780 * bit because of a few extra instructions in outer loops.
781 * Note, the loops have a test at the end, and do the test
782 * before the decrement, and so always perform the loop
Dinh Nguyen3da42852015-06-02 22:52:49 -0500783 * 1 time more than the counter value
784 */
Marek Vasut6a39be62015-07-26 11:42:53 +0200785 c_loop = afi_clocks >> 16;
786 outer = c_loop ? 0xff : (afi_clocks >> 8);
787 inner = outer ? 0xff : afi_clocks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500788
789 /*
790 * rom instructions are structured as follows:
791 *
792 * IDLE_LOOP2: jnz cntr0, TARGET_A
793 * IDLE_LOOP1: jnz cntr1, TARGET_B
794 * return
795 *
796 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
797 * TARGET_B is set to IDLE_LOOP2 as well
798 *
799 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
800 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
801 *
802 * a little confusing, but it helps save precious space in the inst_rom
803 * and sequencer rom and keeps the delays more accurate and reduces
804 * overhead
805 */
Marek Vasutcbcaf462015-07-26 11:34:09 +0200806 if (afi_clocks < 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200807 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasut139823e2015-08-02 19:47:01 +0200808 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500809
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200810 writel(rwcfg->idle_loop1,
Marek Vasut139823e2015-08-02 19:47:01 +0200811 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500812
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200813 writel(rwcfg->idle_loop1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +0200814 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500815 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200816 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasut139823e2015-08-02 19:47:01 +0200817 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500818
Marek Vasut1273dd92015-07-12 21:05:08 +0200819 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
Marek Vasut139823e2015-08-02 19:47:01 +0200820 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500821
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200822 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200823 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500824
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200825 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200826 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500827
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200828 do {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200829 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200830 SDR_PHYGRP_RWMGRGRP_ADDRESS |
831 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200832 } while (c_loop-- != 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500833 }
834 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
835}
836
Marek Vasut944fe712015-07-13 00:44:30 +0200837/**
838 * rw_mgr_mem_init_load_regs() - Load instruction registers
839 * @cntr0: Counter 0 value
840 * @cntr1: Counter 1 value
841 * @cntr2: Counter 2 value
842 * @jump: Jump instruction value
843 *
844 * Load instruction registers.
845 */
846static void rw_mgr_mem_init_load_regs(u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
847{
Marek Vasut5ded7322015-08-02 19:42:26 +0200848 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut944fe712015-07-13 00:44:30 +0200849 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
850
851 /* Load counters */
852 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
853 &sdr_rw_load_mgr_regs->load_cntr0);
854 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
855 &sdr_rw_load_mgr_regs->load_cntr1);
856 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
857 &sdr_rw_load_mgr_regs->load_cntr2);
858
859 /* Load jump address */
860 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
861 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
862 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
863
864 /* Execute count instruction */
865 writel(jump, grpaddr);
866}
867
Marek Vasutecd23342015-07-13 00:51:05 +0200868/**
869 * rw_mgr_mem_load_user() - Load user calibration values
870 * @fin1: Final instruction 1
871 * @fin2: Final instruction 2
872 * @precharge: If 1, precharge the banks at the end
873 *
874 * Load user calibration values and optionally precharge the banks.
875 */
876static void rw_mgr_mem_load_user(const u32 fin1, const u32 fin2,
877 const int precharge)
878{
879 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
880 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
881 u32 r;
882
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200883 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasutecd23342015-07-13 00:51:05 +0200884 /* set rank */
885 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
886
887 /* precharge all banks ... */
888 if (precharge)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200889 writel(rwcfg->precharge_all, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200890
891 /*
892 * USER Use Mirror-ed commands for odd ranks if address
893 * mirrorring is on
894 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200895 if ((rwcfg->mem_address_mirroring >> r) & 0x1) {
Marek Vasutecd23342015-07-13 00:51:05 +0200896 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200897 writel(rwcfg->mrs2_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200898 delay_for_n_mem_clocks(4);
899 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200900 writel(rwcfg->mrs3_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200901 delay_for_n_mem_clocks(4);
902 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200903 writel(rwcfg->mrs1_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200904 delay_for_n_mem_clocks(4);
905 set_jump_as_return();
906 writel(fin1, grpaddr);
907 } else {
908 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200909 writel(rwcfg->mrs2, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200910 delay_for_n_mem_clocks(4);
911 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200912 writel(rwcfg->mrs3, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200913 delay_for_n_mem_clocks(4);
914 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200915 writel(rwcfg->mrs1, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200916 set_jump_as_return();
917 writel(fin2, grpaddr);
918 }
919
920 if (precharge)
921 continue;
922
923 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200924 writel(rwcfg->zqcl, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200925
926 /* tZQinit = tDLLK = 512 ck cycles */
927 delay_for_n_mem_clocks(512);
928 }
929}
930
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200931/**
932 * rw_mgr_mem_initialize() - Initialize RW Manager
933 *
934 * Initialize RW Manager.
935 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500936static void rw_mgr_mem_initialize(void)
937{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500938 debug("%s:%d\n", __func__, __LINE__);
939
940 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200941 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
942 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500943
944 /*
945 * Here's how you load register for a loop
946 * Counters are located @ 0x800
947 * Jump address are located @ 0xC00
948 * For both, registers 0 to 3 are selected using bits 3 and 2, like
949 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
950 * I know this ain't pretty, but Avalon bus throws away the 2 least
951 * significant bits
952 */
953
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200954 /* Start with memory RESET activated */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500955
956 /* tINIT = 200us */
957
958 /*
959 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
960 * If a and b are the number of iteration in 2 nested loops
961 * it takes the following number of cycles to complete the operation:
962 * number_of_cycles = ((2 + n) * a + 2) * b
963 * where n is the number of instruction in the inner loop
964 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
965 * b = 6A
966 */
Marek Vasut139823e2015-08-02 19:47:01 +0200967 rw_mgr_mem_init_load_regs(misccfg->tinit_cntr0_val,
968 misccfg->tinit_cntr1_val,
Marek Vasut96fd4362015-08-02 19:26:55 +0200969 misccfg->tinit_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200970 rwcfg->init_reset_0_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500971
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200972 /* Indicate that memory is stable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200973 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500974
975 /*
976 * transition the RESET to high
977 * Wait for 500us
978 */
979
980 /*
981 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
982 * If a and b are the number of iteration in 2 nested loops
983 * it takes the following number of cycles to complete the operation
984 * number_of_cycles = ((2 + n) * a + 2) * b
985 * where n is the number of instruction in the inner loop
986 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
987 * b = FF
988 */
Marek Vasut139823e2015-08-02 19:47:01 +0200989 rw_mgr_mem_init_load_regs(misccfg->treset_cntr0_val,
990 misccfg->treset_cntr1_val,
Marek Vasut96fd4362015-08-02 19:26:55 +0200991 misccfg->treset_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200992 rwcfg->init_reset_1_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500993
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200994 /* Bring up clock enable. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500995
996 /* tXRP < 250 ck cycles */
997 delay_for_n_mem_clocks(250);
998
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200999 rw_mgr_mem_load_user(rwcfg->mrs0_dll_reset_mirr, rwcfg->mrs0_dll_reset,
Marek Vasutecd23342015-07-13 00:51:05 +02001000 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001001}
1002
Marek Vasutf1f22f72015-07-26 10:59:19 +02001003/**
1004 * rw_mgr_mem_handoff() - Hand off the memory to user
1005 *
1006 * At the end of calibration we have to program the user settings in
1007 * and hand off the memory to the user.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001008 */
1009static void rw_mgr_mem_handoff(void)
1010{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001011 rw_mgr_mem_load_user(rwcfg->mrs0_user_mirr, rwcfg->mrs0_user, 1);
Marek Vasutecd23342015-07-13 00:51:05 +02001012 /*
Marek Vasutf1f22f72015-07-26 10:59:19 +02001013 * Need to wait tMOD (12CK or 15ns) time before issuing other
1014 * commands, but we will have plenty of NIOS cycles before actual
1015 * handoff so its okay.
Marek Vasutecd23342015-07-13 00:51:05 +02001016 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001017}
1018
Marek Vasut8371c2e2015-07-21 06:00:36 +02001019/**
1020 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1021 * @group: Write Group
1022 * @use_dm: Use DM
1023 *
1024 * Issue write test command. Two variants are provided, one that just tests
1025 * a write pattern and another that tests datamask functionality.
Marek Vasutad64769c2015-07-21 05:43:37 +02001026 */
Marek Vasut8371c2e2015-07-21 06:00:36 +02001027static void rw_mgr_mem_calibrate_write_test_issue(u32 group,
1028 u32 test_dm)
Marek Vasutad64769c2015-07-21 05:43:37 +02001029{
Marek Vasut8371c2e2015-07-21 06:00:36 +02001030 const u32 quick_write_mode =
1031 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001032 misccfg->enable_super_quick_calibration;
Marek Vasut8371c2e2015-07-21 06:00:36 +02001033 u32 mcc_instruction;
1034 u32 rw_wl_nop_cycles;
Marek Vasutad64769c2015-07-21 05:43:37 +02001035
1036 /*
1037 * Set counter and jump addresses for the right
1038 * number of NOP cycles.
1039 * The number of supported NOP cycles can range from -1 to infinity
1040 * Three different cases are handled:
1041 *
1042 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1043 * mechanism will be used to insert the right number of NOPs
1044 *
1045 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1046 * issuing the write command will jump straight to the
1047 * micro-instruction that turns on DQS (for DDRx), or outputs write
1048 * data (for RLD), skipping
1049 * the NOP micro-instruction all together
1050 *
1051 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1052 * turned on in the same micro-instruction that issues the write
1053 * command. Then we need
1054 * to directly jump to the micro-instruction that sends out the data
1055 *
1056 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1057 * (2 and 3). One jump-counter (0) is used to perform multiple
1058 * write-read operations.
1059 * one counter left to issue this command in "multiple-group" mode
1060 */
1061
1062 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
1063
1064 if (rw_wl_nop_cycles == -1) {
1065 /*
1066 * CNTR 2 - We want to execute the special write operation that
1067 * turns on DQS right away and then skip directly to the
1068 * instruction that sends out the data. We set the counter to a
1069 * large number so that the jump is always taken.
1070 */
1071 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1072
1073 /* CNTR 3 - Not used */
1074 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001075 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0_wl_1;
1076 writel(rwcfg->lfsr_wr_rd_dm_bank_0_data,
Marek Vasutad64769c2015-07-21 05:43:37 +02001077 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001078 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001079 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1080 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001081 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0_wl_1;
1082 writel(rwcfg->lfsr_wr_rd_bank_0_data,
Marek Vasut139823e2015-08-02 19:47:01 +02001083 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001084 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001085 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001086 }
1087 } else if (rw_wl_nop_cycles == 0) {
1088 /*
1089 * CNTR 2 - We want to skip the NOP operation and go straight
1090 * to the DQS enable instruction. We set the counter to a large
1091 * number so that the jump is always taken.
1092 */
1093 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1094
1095 /* CNTR 3 - Not used */
1096 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001097 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1098 writel(rwcfg->lfsr_wr_rd_dm_bank_0_dqs,
Marek Vasutad64769c2015-07-21 05:43:37 +02001099 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1100 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001101 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1102 writel(rwcfg->lfsr_wr_rd_bank_0_dqs,
Marek Vasut139823e2015-08-02 19:47:01 +02001103 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasutad64769c2015-07-21 05:43:37 +02001104 }
1105 } else {
1106 /*
1107 * CNTR 2 - In this case we want to execute the next instruction
1108 * and NOT take the jump. So we set the counter to 0. The jump
1109 * address doesn't count.
1110 */
1111 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1112 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1113
1114 /*
1115 * CNTR 3 - Set the nop counter to the number of cycles we
1116 * need to loop for, minus 1.
1117 */
1118 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1119 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001120 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1121 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001122 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001123 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001124 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1125 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001126 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001127 }
1128 }
1129
1130 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1131 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1132
1133 if (quick_write_mode)
1134 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1135 else
1136 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1137
1138 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1139
1140 /*
1141 * CNTR 1 - This is used to ensure enough time elapses
1142 * for read data to come back.
1143 */
1144 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1145
1146 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001147 writel(rwcfg->lfsr_wr_rd_dm_bank_0_wait,
Marek Vasut139823e2015-08-02 19:47:01 +02001148 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasutad64769c2015-07-21 05:43:37 +02001149 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001150 writel(rwcfg->lfsr_wr_rd_bank_0_wait,
Marek Vasut139823e2015-08-02 19:47:01 +02001151 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasutad64769c2015-07-21 05:43:37 +02001152 }
1153
Marek Vasut8371c2e2015-07-21 06:00:36 +02001154 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1155 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1156 (group << 2));
Marek Vasutad64769c2015-07-21 05:43:37 +02001157}
1158
Marek Vasut4a82854b2015-07-21 05:57:11 +02001159/**
1160 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple pass
1161 * @rank_bgn: Rank number
1162 * @write_group: Write Group
1163 * @use_dm: Use DM
1164 * @all_correct: All bits must be correct in the mask
1165 * @bit_chk: Resulting bit mask after the test
1166 * @all_ranks: Test all ranks
1167 *
1168 * Test writes, can check for a single bit pass or multiple bit pass.
1169 */
Marek Vasutb9452ea2015-07-21 05:54:39 +02001170static int
1171rw_mgr_mem_calibrate_write_test(const u32 rank_bgn, const u32 write_group,
1172 const u32 use_dm, const u32 all_correct,
1173 u32 *bit_chk, const u32 all_ranks)
Marek Vasutad64769c2015-07-21 05:43:37 +02001174{
Marek Vasutb9452ea2015-07-21 05:54:39 +02001175 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001176 rwcfg->mem_number_of_ranks :
Marek Vasutb9452ea2015-07-21 05:54:39 +02001177 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001178 const u32 shift_ratio = rwcfg->mem_dq_per_write_dqs /
1179 rwcfg->mem_virtual_groups_per_write_dqs;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001180 const u32 correct_mask_vg = param->write_correct_mask_vg;
1181
1182 u32 tmp_bit_chk, base_rw_mgr;
1183 int vg, r;
Marek Vasutad64769c2015-07-21 05:43:37 +02001184
1185 *bit_chk = param->write_correct_mask;
Marek Vasutad64769c2015-07-21 05:43:37 +02001186
1187 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001188 /* Set rank */
Marek Vasutad64769c2015-07-21 05:43:37 +02001189 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1190
1191 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001192 for (vg = rwcfg->mem_virtual_groups_per_write_dqs - 1;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001193 vg >= 0; vg--) {
1194 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutad64769c2015-07-21 05:43:37 +02001195 writel(0, &phy_mgr_cmd->fifo_reset);
1196
Marek Vasutb9452ea2015-07-21 05:54:39 +02001197 rw_mgr_mem_calibrate_write_test_issue(
1198 write_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001199 rwcfg->mem_virtual_groups_per_write_dqs + vg,
Marek Vasutad64769c2015-07-21 05:43:37 +02001200 use_dm);
1201
Marek Vasutb9452ea2015-07-21 05:54:39 +02001202 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1203 tmp_bit_chk <<= shift_ratio;
1204 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasutad64769c2015-07-21 05:43:37 +02001205 }
Marek Vasutb9452ea2015-07-21 05:54:39 +02001206
Marek Vasutad64769c2015-07-21 05:43:37 +02001207 *bit_chk &= tmp_bit_chk;
1208 }
1209
Marek Vasutb9452ea2015-07-21 05:54:39 +02001210 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutad64769c2015-07-21 05:43:37 +02001211 if (all_correct) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001212 debug_cond(DLEVEL >= 2,
Marek Vasutb9452ea2015-07-21 05:54:39 +02001213 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1214 write_group, use_dm, *bit_chk,
1215 param->write_correct_mask,
1216 *bit_chk == param->write_correct_mask);
Marek Vasutad64769c2015-07-21 05:43:37 +02001217 return *bit_chk == param->write_correct_mask;
1218 } else {
Marek Vasutea9aa242016-04-04 21:21:05 +02001219 debug_cond(DLEVEL >= 2,
Marek Vasutb9452ea2015-07-21 05:54:39 +02001220 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1221 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasutad64769c2015-07-21 05:43:37 +02001222 return *bit_chk != 0x00;
1223 }
1224}
1225
Marek Vasutd844c7d2015-07-18 03:55:07 +02001226/**
1227 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1228 * @rank_bgn: Rank number
1229 * @group: Read/Write Group
1230 * @all_ranks: Test all ranks
1231 *
1232 * Performs a guaranteed read on the patterns we are going to use during a
1233 * read test to ensure memory works.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001234 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02001235static int
1236rw_mgr_mem_calibrate_read_test_patterns(const u32 rank_bgn, const u32 group,
1237 const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001238{
Marek Vasutd844c7d2015-07-18 03:55:07 +02001239 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1240 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1241 const u32 addr_offset =
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001242 (group * rwcfg->mem_virtual_groups_per_read_dqs) << 2;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001243 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001244 rwcfg->mem_number_of_ranks :
Marek Vasutd844c7d2015-07-18 03:55:07 +02001245 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001246 const u32 shift_ratio = rwcfg->mem_dq_per_read_dqs /
1247 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001248 const u32 correct_mask_vg = param->read_correct_mask_vg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001249
Marek Vasutd844c7d2015-07-18 03:55:07 +02001250 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1251 int vg, r;
1252 int ret = 0;
1253
1254 bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001255
1256 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutd844c7d2015-07-18 03:55:07 +02001257 /* Set rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001258 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1259
1260 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001261 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001262 writel(rwcfg->guaranteed_read,
Marek Vasut139823e2015-08-02 19:47:01 +02001263 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001264
Marek Vasut1273dd92015-07-12 21:05:08 +02001265 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001266 writel(rwcfg->guaranteed_read_cont,
Marek Vasut139823e2015-08-02 19:47:01 +02001267 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001268
1269 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001270 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001271 vg >= 0; vg--) {
1272 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001273 writel(0, &phy_mgr_cmd->fifo_reset);
1274 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1275 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001276 writel(rwcfg->guaranteed_read,
Marek Vasutd844c7d2015-07-18 03:55:07 +02001277 addr + addr_offset + (vg << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001278
Marek Vasut1273dd92015-07-12 21:05:08 +02001279 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001280 tmp_bit_chk <<= shift_ratio;
1281 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001282 }
Marek Vasutd844c7d2015-07-18 03:55:07 +02001283
1284 bit_chk &= tmp_bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001285 }
1286
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001287 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001288
1289 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001290
1291 if (bit_chk != param->read_correct_mask)
1292 ret = -EIO;
1293
Marek Vasutea9aa242016-04-04 21:21:05 +02001294 debug_cond(DLEVEL >= 1,
Marek Vasutd844c7d2015-07-18 03:55:07 +02001295 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1296 __func__, __LINE__, group, bit_chk,
1297 param->read_correct_mask, ret);
1298
1299 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001300}
1301
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001302/**
1303 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read test
1304 * @rank_bgn: Rank number
1305 * @all_ranks: Test all ranks
1306 *
1307 * Load up the patterns we are going to use during a read test.
1308 */
1309static void rw_mgr_mem_calibrate_read_load_patterns(const u32 rank_bgn,
1310 const int all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001311{
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001312 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001313 rwcfg->mem_number_of_ranks :
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001314 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1315 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001316
1317 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001318
Dinh Nguyen3da42852015-06-02 22:52:49 -05001319 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001320 /* set rank */
1321 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1322
1323 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001324 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001325
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001326 writel(rwcfg->guaranteed_write_wait0,
Marek Vasut139823e2015-08-02 19:47:01 +02001327 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001328
Marek Vasut1273dd92015-07-12 21:05:08 +02001329 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001330
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001331 writel(rwcfg->guaranteed_write_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02001332 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001333
Marek Vasut1273dd92015-07-12 21:05:08 +02001334 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001335
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001336 writel(rwcfg->guaranteed_write_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02001337 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001338
Marek Vasut1273dd92015-07-12 21:05:08 +02001339 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001340
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001341 writel(rwcfg->guaranteed_write_wait3,
Marek Vasut139823e2015-08-02 19:47:01 +02001342 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001343
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001344 writel(rwcfg->guaranteed_write, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02001345 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001346 }
1347
1348 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1349}
1350
Marek Vasut783fcf52015-07-20 03:26:05 +02001351/**
1352 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1353 * @rank_bgn: Rank number
1354 * @group: Read/Write group
1355 * @num_tries: Number of retries of the test
1356 * @all_correct: All bits must be correct in the mask
1357 * @bit_chk: Resulting bit mask after the test
1358 * @all_groups: Test all R/W groups
1359 * @all_ranks: Test all ranks
1360 *
1361 * Try a read and see if it returns correct data back. Test has dummy reads
1362 * inserted into the mix used to align DQS enable. Test has more thorough
1363 * checks than the regular read test.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001364 */
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001365static int
1366rw_mgr_mem_calibrate_read_test(const u32 rank_bgn, const u32 group,
1367 const u32 num_tries, const u32 all_correct,
1368 u32 *bit_chk,
1369 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001370{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001371 const u32 rank_end = all_ranks ? rwcfg->mem_number_of_ranks :
Dinh Nguyen3da42852015-06-02 22:52:49 -05001372 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001373 const u32 quick_read_mode =
1374 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001375 misccfg->enable_super_quick_calibration);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001376 u32 correct_mask_vg = param->read_correct_mask_vg;
1377 u32 tmp_bit_chk;
1378 u32 base_rw_mgr;
1379 u32 addr;
1380
1381 int r, vg, ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001382
1383 *bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001384
1385 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001386 /* set rank */
1387 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1388
Marek Vasut1273dd92015-07-12 21:05:08 +02001389 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001390
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001391 writel(rwcfg->read_b2b_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02001392 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001393
Marek Vasut1273dd92015-07-12 21:05:08 +02001394 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001395 writel(rwcfg->read_b2b_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02001396 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001397
Dinh Nguyen3da42852015-06-02 22:52:49 -05001398 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001399 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001400 /* need at least two (1+1) reads to capture failures */
1401 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001402 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001403 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001404 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001405
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001406 writel(rwcfg->read_b2b,
Marek Vasut139823e2015-08-02 19:47:01 +02001407 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001408 if (all_groups)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001409 writel(rwcfg->mem_if_read_dqs_width *
1410 rwcfg->mem_virtual_groups_per_read_dqs - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001411 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001412 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001413 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001414
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001415 writel(rwcfg->read_b2b,
Marek Vasut139823e2015-08-02 19:47:01 +02001416 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001417
1418 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001419 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1; vg >= 0;
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001420 vg--) {
Marek Vasutba522c72015-07-19 07:57:28 +02001421 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001422 writel(0, &phy_mgr_cmd->fifo_reset);
1423 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1424 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001425
Marek Vasutba522c72015-07-19 07:57:28 +02001426 if (all_groups) {
1427 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1428 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1429 } else {
1430 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1431 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1432 }
Marek Vasutc4815f72015-07-12 19:03:33 +02001433
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001434 writel(rwcfg->read_b2b, addr +
Marek Vasut139823e2015-08-02 19:47:01 +02001435 ((group *
1436 rwcfg->mem_virtual_groups_per_read_dqs +
1437 vg) << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001438
Marek Vasut1273dd92015-07-12 21:05:08 +02001439 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001440 tmp_bit_chk <<= rwcfg->mem_dq_per_read_dqs /
1441 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutba522c72015-07-19 07:57:28 +02001442 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001443 }
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001444
Dinh Nguyen3da42852015-06-02 22:52:49 -05001445 *bit_chk &= tmp_bit_chk;
1446 }
1447
Marek Vasutc4815f72015-07-12 19:03:33 +02001448 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001449 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001450
Marek Vasut3853d652015-07-19 07:44:21 +02001451 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1452
Dinh Nguyen3da42852015-06-02 22:52:49 -05001453 if (all_correct) {
Marek Vasut3853d652015-07-19 07:44:21 +02001454 ret = (*bit_chk == param->read_correct_mask);
Marek Vasutea9aa242016-04-04 21:21:05 +02001455 debug_cond(DLEVEL >= 2,
Marek Vasut3853d652015-07-19 07:44:21 +02001456 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1457 __func__, __LINE__, group, all_groups, *bit_chk,
1458 param->read_correct_mask, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001459 } else {
Marek Vasut3853d652015-07-19 07:44:21 +02001460 ret = (*bit_chk != 0x00);
Marek Vasutea9aa242016-04-04 21:21:05 +02001461 debug_cond(DLEVEL >= 2,
Marek Vasut3853d652015-07-19 07:44:21 +02001462 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1463 __func__, __LINE__, group, all_groups, *bit_chk,
1464 0, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001465 }
Marek Vasut3853d652015-07-19 07:44:21 +02001466
1467 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001468}
1469
Marek Vasut96df6032015-07-19 07:35:36 +02001470/**
1471 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1472 * @grp: Read/Write group
1473 * @num_tries: Number of retries of the test
1474 * @all_correct: All bits must be correct in the mask
1475 * @all_groups: Test all R/W groups
1476 *
1477 * Perform a READ test across all memory ranks.
1478 */
1479static int
1480rw_mgr_mem_calibrate_read_test_all_ranks(const u32 grp, const u32 num_tries,
1481 const u32 all_correct,
1482 const u32 all_groups)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001483{
Marek Vasut96df6032015-07-19 07:35:36 +02001484 u32 bit_chk;
1485 return rw_mgr_mem_calibrate_read_test(0, grp, num_tries, all_correct,
1486 &bit_chk, all_groups, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001487}
1488
Marek Vasut60bb8a82015-07-19 06:25:27 +02001489/**
1490 * rw_mgr_incr_vfifo() - Increase VFIFO value
1491 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001492 *
1493 * Increase VFIFO value.
1494 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001495static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001496{
Marek Vasut1273dd92015-07-12 21:05:08 +02001497 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001498}
1499
Marek Vasut60bb8a82015-07-19 06:25:27 +02001500/**
1501 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1502 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001503 *
1504 * Decrease VFIFO value.
1505 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001506static void rw_mgr_decr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001507{
Marek Vasut60bb8a82015-07-19 06:25:27 +02001508 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001509
Marek Vasut96fd4362015-08-02 19:26:55 +02001510 for (i = 0; i < misccfg->read_valid_fifo_size - 1; i++)
Marek Vasut8c887b62015-07-19 06:37:51 +02001511 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001512}
1513
Marek Vasutd145ca92015-07-19 06:45:43 +02001514/**
1515 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1516 * @grp: Read/Write group
1517 *
1518 * Push VFIFO until a failing read happens.
1519 */
1520static int find_vfifo_failing_read(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001521{
Marek Vasut96df6032015-07-19 07:35:36 +02001522 u32 v, ret, fail_cnt = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001523
Marek Vasut96fd4362015-08-02 19:26:55 +02001524 for (v = 0; v < misccfg->read_valid_fifo_size; v++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001525 debug_cond(DLEVEL >= 2, "%s:%d: vfifo %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001526 __func__, __LINE__, v);
Marek Vasutd145ca92015-07-19 06:45:43 +02001527 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001528 PASS_ONE_BIT, 0);
Marek Vasutd145ca92015-07-19 06:45:43 +02001529 if (!ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001530 fail_cnt++;
1531
1532 if (fail_cnt == 2)
Marek Vasutd145ca92015-07-19 06:45:43 +02001533 return v;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001534 }
1535
Marek Vasutd145ca92015-07-19 06:45:43 +02001536 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001537 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001538 }
1539
Marek Vasutd145ca92015-07-19 06:45:43 +02001540 /* No failing read found! Something must have gone wrong. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001541 debug_cond(DLEVEL >= 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
Marek Vasutd145ca92015-07-19 06:45:43 +02001542 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001543}
1544
Marek Vasut192d6f92015-07-19 05:26:49 +02001545/**
Marek Vasut52e8f212015-07-19 07:27:06 +02001546 * sdr_find_phase_delay() - Find DQS enable phase or delay
1547 * @working: If 1, look for working phase/delay, if 0, look for non-working
1548 * @delay: If 1, look for delay, if 0, look for phase
1549 * @grp: Read/Write group
1550 * @work: Working window position
1551 * @work_inc: Working window increment
1552 * @pd: DQS Phase/Delay Iterator
1553 *
1554 * Find working or non-working DQS enable phase setting.
1555 */
1556static int sdr_find_phase_delay(int working, int delay, const u32 grp,
1557 u32 *work, const u32 work_inc, u32 *pd)
1558{
Marek Vasut139823e2015-08-02 19:47:01 +02001559 const u32 max = delay ? iocfg->dqs_en_delay_max :
1560 iocfg->dqs_en_phase_max;
Marek Vasut96df6032015-07-19 07:35:36 +02001561 u32 ret;
Marek Vasut52e8f212015-07-19 07:27:06 +02001562
1563 for (; *pd <= max; (*pd)++) {
1564 if (delay)
1565 scc_mgr_set_dqs_en_delay_all_ranks(grp, *pd);
1566 else
1567 scc_mgr_set_dqs_en_phase_all_ranks(grp, *pd);
1568
1569 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001570 PASS_ONE_BIT, 0);
Marek Vasut52e8f212015-07-19 07:27:06 +02001571 if (!working)
1572 ret = !ret;
1573
1574 if (ret)
1575 return 0;
1576
1577 if (work)
1578 *work += work_inc;
1579 }
1580
1581 return -EINVAL;
1582}
1583/**
Marek Vasut192d6f92015-07-19 05:26:49 +02001584 * sdr_find_phase() - Find DQS enable phase
1585 * @working: If 1, look for working phase, if 0, look for non-working phase
1586 * @grp: Read/Write group
Marek Vasut192d6f92015-07-19 05:26:49 +02001587 * @work: Working window position
1588 * @i: Iterator
1589 * @p: DQS Phase Iterator
Marek Vasut192d6f92015-07-19 05:26:49 +02001590 *
1591 * Find working or non-working DQS enable phase setting.
1592 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001593static int sdr_find_phase(int working, const u32 grp, u32 *work,
Marek Vasut86a39dc2015-07-19 05:35:40 +02001594 u32 *i, u32 *p)
Marek Vasut192d6f92015-07-19 05:26:49 +02001595{
Marek Vasut96fd4362015-08-02 19:26:55 +02001596 const u32 end = misccfg->read_valid_fifo_size + (working ? 0 : 1);
Marek Vasut52e8f212015-07-19 07:27:06 +02001597 int ret;
Marek Vasut192d6f92015-07-19 05:26:49 +02001598
1599 for (; *i < end; (*i)++) {
1600 if (working)
1601 *p = 0;
1602
Marek Vasut52e8f212015-07-19 07:27:06 +02001603 ret = sdr_find_phase_delay(working, 0, grp, work,
Marek Vasut160695d2015-08-02 19:10:58 +02001604 iocfg->delay_per_opa_tap, p);
Marek Vasut52e8f212015-07-19 07:27:06 +02001605 if (!ret)
1606 return 0;
Marek Vasut192d6f92015-07-19 05:26:49 +02001607
Marek Vasut160695d2015-08-02 19:10:58 +02001608 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001609 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001610 rw_mgr_incr_vfifo(grp);
Marek Vasut192d6f92015-07-19 05:26:49 +02001611 if (!working)
1612 *p = 0;
1613 }
1614 }
1615
1616 return -EINVAL;
1617}
1618
Marek Vasut4c5e5842015-07-19 06:04:00 +02001619/**
1620 * sdr_working_phase() - Find working DQS enable phase
1621 * @grp: Read/Write group
1622 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001623 * @d: dtaps output value
1624 * @p: DQS Phase Iterator
1625 * @i: Iterator
1626 *
1627 * Find working DQS enable phase setting.
1628 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001629static int sdr_working_phase(const u32 grp, u32 *work_bgn, u32 *d,
Marek Vasut4c5e5842015-07-19 06:04:00 +02001630 u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001631{
Marek Vasut160695d2015-08-02 19:10:58 +02001632 const u32 dtaps_per_ptap = iocfg->delay_per_opa_tap /
1633 iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasut192d6f92015-07-19 05:26:49 +02001634 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001635
Marek Vasut192d6f92015-07-19 05:26:49 +02001636 *work_bgn = 0;
1637
1638 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1639 *i = 0;
Marek Vasut521fe392015-07-19 04:34:12 +02001640 scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
Marek Vasut8c887b62015-07-19 06:37:51 +02001641 ret = sdr_find_phase(1, grp, work_bgn, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001642 if (!ret)
1643 return 0;
Marek Vasut160695d2015-08-02 19:10:58 +02001644 *work_bgn += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001645 }
1646
Marek Vasut38ed6922015-07-19 05:01:12 +02001647 /* Cannot find working solution */
Marek Vasutea9aa242016-04-04 21:21:05 +02001648 debug_cond(DLEVEL >= 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
Marek Vasut192d6f92015-07-19 05:26:49 +02001649 __func__, __LINE__);
1650 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001651}
1652
Marek Vasut4c5e5842015-07-19 06:04:00 +02001653/**
1654 * sdr_backup_phase() - Find DQS enable backup phase
1655 * @grp: Read/Write group
1656 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001657 * @p: DQS Phase Iterator
1658 *
1659 * Find DQS enable backup phase setting.
1660 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001661static void sdr_backup_phase(const u32 grp, u32 *work_bgn, u32 *p)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001662{
Marek Vasut96df6032015-07-19 07:35:36 +02001663 u32 tmp_delay, d;
Marek Vasut4c5e5842015-07-19 06:04:00 +02001664 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001665
1666 /* Special case code for backing up a phase */
1667 if (*p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001668 *p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001669 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001670 } else {
1671 (*p)--;
1672 }
Marek Vasut160695d2015-08-02 19:10:58 +02001673 tmp_delay = *work_bgn - iocfg->delay_per_opa_tap;
Marek Vasut521fe392015-07-19 04:34:12 +02001674 scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001675
Marek Vasut139823e2015-08-02 19:47:01 +02001676 for (d = 0; d <= iocfg->dqs_en_delay_max && tmp_delay < *work_bgn;
1677 d++) {
Marek Vasut49891df62015-07-19 05:48:30 +02001678 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001679
Marek Vasut4c5e5842015-07-19 06:04:00 +02001680 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001681 PASS_ONE_BIT, 0);
Marek Vasut4c5e5842015-07-19 06:04:00 +02001682 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001683 *work_bgn = tmp_delay;
1684 break;
1685 }
Marek Vasut49891df62015-07-19 05:48:30 +02001686
Marek Vasut160695d2015-08-02 19:10:58 +02001687 tmp_delay += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001688 }
1689
Marek Vasut4c5e5842015-07-19 06:04:00 +02001690 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001691 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001692 if (*p > iocfg->dqs_en_phase_max) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001693 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001694 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001695 }
1696
Marek Vasut521fe392015-07-19 04:34:12 +02001697 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001698}
1699
Marek Vasut4c5e5842015-07-19 06:04:00 +02001700/**
1701 * sdr_nonworking_phase() - Find non-working DQS enable phase
1702 * @grp: Read/Write group
1703 * @work_end: Working window end position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001704 * @p: DQS Phase Iterator
1705 * @i: Iterator
1706 *
1707 * Find non-working DQS enable phase setting.
1708 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001709static int sdr_nonworking_phase(const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001710{
Marek Vasut192d6f92015-07-19 05:26:49 +02001711 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001712
1713 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001714 *work_end += iocfg->delay_per_opa_tap;
1715 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001716 /* Fiddle with FIFO. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001717 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001718 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001719 }
1720
Marek Vasut8c887b62015-07-19 06:37:51 +02001721 ret = sdr_find_phase(0, grp, work_end, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001722 if (ret) {
1723 /* Cannot see edge of failing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001724 debug_cond(DLEVEL >= 2, "%s:%d: end: failed\n",
Marek Vasut192d6f92015-07-19 05:26:49 +02001725 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001726 }
1727
Marek Vasut192d6f92015-07-19 05:26:49 +02001728 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001729}
1730
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001731/**
1732 * sdr_find_window_center() - Find center of the working DQS window.
1733 * @grp: Read/Write group
1734 * @work_bgn: First working settings
1735 * @work_end: Last working settings
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001736 *
1737 * Find center of the working DQS enable window.
1738 */
1739static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
Marek Vasut8c887b62015-07-19 06:37:51 +02001740 const u32 work_end)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001741{
Marek Vasut96df6032015-07-19 07:35:36 +02001742 u32 work_mid;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001743 int tmp_delay = 0;
Marek Vasut28fd2422015-07-19 02:56:59 +02001744 int i, p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001745
Marek Vasut28fd2422015-07-19 02:56:59 +02001746 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001747
Marek Vasutea9aa242016-04-04 21:21:05 +02001748 debug_cond(DLEVEL >= 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasut28fd2422015-07-19 02:56:59 +02001749 work_bgn, work_end, work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001750 /* Get the middle delay to be less than a VFIFO delay */
Marek Vasut160695d2015-08-02 19:10:58 +02001751 tmp_delay = (iocfg->dqs_en_phase_max + 1) * iocfg->delay_per_opa_tap;
Marek Vasut28fd2422015-07-19 02:56:59 +02001752
Marek Vasutea9aa242016-04-04 21:21:05 +02001753 debug_cond(DLEVEL >= 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001754 work_mid %= tmp_delay;
Marek Vasutea9aa242016-04-04 21:21:05 +02001755 debug_cond(DLEVEL >= 2, "new work_mid %d\n", work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001756
Marek Vasut160695d2015-08-02 19:10:58 +02001757 tmp_delay = rounddown(work_mid, iocfg->delay_per_opa_tap);
1758 if (tmp_delay > iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap)
1759 tmp_delay = iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap;
1760 p = tmp_delay / iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001761
Marek Vasutea9aa242016-04-04 21:21:05 +02001762 debug_cond(DLEVEL >= 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001763
Marek Vasut139823e2015-08-02 19:47:01 +02001764 d = DIV_ROUND_UP(work_mid - tmp_delay,
1765 iocfg->delay_per_dqs_en_dchain_tap);
Marek Vasut160695d2015-08-02 19:10:58 +02001766 if (d > iocfg->dqs_en_delay_max)
1767 d = iocfg->dqs_en_delay_max;
1768 tmp_delay += d * iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001769
Marek Vasutea9aa242016-04-04 21:21:05 +02001770 debug_cond(DLEVEL >= 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
Marek Vasut28fd2422015-07-19 02:56:59 +02001771
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001772 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
Marek Vasut28fd2422015-07-19 02:56:59 +02001773 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001774
1775 /*
1776 * push vfifo until we can successfully calibrate. We can do this
1777 * because the largest possible margin in 1 VFIFO cycle.
1778 */
Marek Vasut96fd4362015-08-02 19:26:55 +02001779 for (i = 0; i < misccfg->read_valid_fifo_size; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001780 debug_cond(DLEVEL >= 2, "find_dqs_en_phase: center\n");
Marek Vasut28fd2422015-07-19 02:56:59 +02001781 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05001782 PASS_ONE_BIT,
Marek Vasut96df6032015-07-19 07:35:36 +02001783 0)) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001784 debug_cond(DLEVEL >= 2,
Marek Vasut8c887b62015-07-19 06:37:51 +02001785 "%s:%d center: found: ptap=%u dtap=%u\n",
1786 __func__, __LINE__, p, d);
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001787 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001788 }
1789
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001790 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001791 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001792 }
1793
Marek Vasutea9aa242016-04-04 21:21:05 +02001794 debug_cond(DLEVEL >= 2, "%s:%d center: failed.\n",
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001795 __func__, __LINE__);
1796 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001797}
1798
Marek Vasut33756892015-07-20 09:11:09 +02001799/**
1800 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to use
1801 * @grp: Read/Write Group
1802 *
1803 * Find a good DQS enable to use.
1804 */
Marek Vasut914546e2015-07-20 09:20:42 +02001805static int rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001806{
Marek Vasut57355402015-07-20 09:20:20 +02001807 u32 d, p, i;
1808 u32 dtaps_per_ptap;
1809 u32 work_bgn, work_end;
Marek Vasut35e47b72015-08-10 23:01:43 +02001810 u32 found_passing_read, found_failing_read = 0, initial_failing_dtap;
Marek Vasut57355402015-07-20 09:20:20 +02001811 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001812
1813 debug("%s:%d %u\n", __func__, __LINE__, grp);
1814
1815 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1816
1817 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1818 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1819
Marek Vasut2f3589c2015-07-19 02:42:21 +02001820 /* Step 0: Determine number of delay taps for each phase tap. */
Marek Vasut139823e2015-08-02 19:47:01 +02001821 dtaps_per_ptap = iocfg->delay_per_opa_tap /
1822 iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001823
Marek Vasut2f3589c2015-07-19 02:42:21 +02001824 /* Step 1: First push vfifo until we get a failing read. */
Marek Vasutd145ca92015-07-19 06:45:43 +02001825 find_vfifo_failing_read(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001826
Marek Vasut2f3589c2015-07-19 02:42:21 +02001827 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001828 work_bgn = 0;
Marek Vasut914546e2015-07-20 09:20:42 +02001829 ret = sdr_working_phase(grp, &work_bgn, &d, &p, &i);
1830 if (ret)
1831 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001832
1833 work_end = work_bgn;
1834
1835 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001836 * If d is 0 then the working window covers a phase tap and we can
1837 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen3da42852015-06-02 22:52:49 -05001838 * and we need to increment the dtaps until we find the end.
1839 */
1840 if (d == 0) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001841 /*
1842 * Step 3a: If we have room, back off by one and
1843 * increment in dtaps.
1844 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001845 sdr_backup_phase(grp, &work_bgn, &p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001846
Marek Vasut2f3589c2015-07-19 02:42:21 +02001847 /*
1848 * Step 4a: go forward from working phase to non working
1849 * phase, increment in ptaps.
1850 */
Marek Vasut914546e2015-07-20 09:20:42 +02001851 ret = sdr_nonworking_phase(grp, &work_end, &p, &i);
1852 if (ret)
1853 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001854
Marek Vasut2f3589c2015-07-19 02:42:21 +02001855 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001856
1857 /* Special case code for backing up a phase */
1858 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001859 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001860 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001861 } else {
1862 p = p - 1;
1863 }
1864
Marek Vasut160695d2015-08-02 19:10:58 +02001865 work_end -= iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001866 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1867
Dinh Nguyen3da42852015-06-02 22:52:49 -05001868 d = 0;
1869
Marek Vasutea9aa242016-04-04 21:21:05 +02001870 debug_cond(DLEVEL >= 2, "%s:%d p: ptap=%u\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001871 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001872 }
1873
Marek Vasut2f3589c2015-07-19 02:42:21 +02001874 /* The dtap increment to find the failing edge is done here. */
Marek Vasut52e8f212015-07-19 07:27:06 +02001875 sdr_find_phase_delay(0, 1, grp, &work_end,
Marek Vasut160695d2015-08-02 19:10:58 +02001876 iocfg->delay_per_dqs_en_dchain_tap, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001877
1878 /* Go back to working dtap */
1879 if (d != 0)
Marek Vasut160695d2015-08-02 19:10:58 +02001880 work_end -= iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001881
Marek Vasutea9aa242016-04-04 21:21:05 +02001882 debug_cond(DLEVEL >= 2,
Marek Vasut2f3589c2015-07-19 02:42:21 +02001883 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1884 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001885
1886 if (work_end < work_bgn) {
1887 /* nil range */
Marek Vasutea9aa242016-04-04 21:21:05 +02001888 debug_cond(DLEVEL >= 2, "%s:%d end-2: failed\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001889 __func__, __LINE__);
Marek Vasut914546e2015-07-20 09:20:42 +02001890 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001891 }
1892
Marek Vasutea9aa242016-04-04 21:21:05 +02001893 debug_cond(DLEVEL >= 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001894 __func__, __LINE__, work_bgn, work_end);
1895
Dinh Nguyen3da42852015-06-02 22:52:49 -05001896 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001897 * We need to calculate the number of dtaps that equal a ptap.
1898 * To do that we'll back up a ptap and re-find the edge of the
1899 * window using dtaps
Dinh Nguyen3da42852015-06-02 22:52:49 -05001900 */
Marek Vasutea9aa242016-04-04 21:21:05 +02001901 debug_cond(DLEVEL >= 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001902 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001903
1904 /* Special case code for backing up a phase */
1905 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001906 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001907 rw_mgr_decr_vfifo(grp);
Marek Vasutea9aa242016-04-04 21:21:05 +02001908 debug_cond(DLEVEL >= 2, "%s:%d backedup cycle/phase: p=%u\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001909 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001910 } else {
1911 p = p - 1;
Marek Vasutea9aa242016-04-04 21:21:05 +02001912 debug_cond(DLEVEL >= 2, "%s:%d backedup phase only: p=%u",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001913 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001914 }
1915
1916 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1917
1918 /*
1919 * Increase dtap until we first see a passing read (in case the
Marek Vasut2f3589c2015-07-19 02:42:21 +02001920 * window is smaller than a ptap), and then a failing read to
1921 * mark the edge of the window again.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001922 */
1923
Marek Vasut2f3589c2015-07-19 02:42:21 +02001924 /* Find a passing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001925 debug_cond(DLEVEL >= 2, "%s:%d find passing read\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001926 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001927
Dinh Nguyen3da42852015-06-02 22:52:49 -05001928 initial_failing_dtap = d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001929
Marek Vasut52e8f212015-07-19 07:27:06 +02001930 found_passing_read = !sdr_find_phase_delay(1, 1, grp, NULL, 0, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001931 if (found_passing_read) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001932 /* Find a failing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001933 debug_cond(DLEVEL >= 2, "%s:%d find failing read\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001934 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001935 d++;
1936 found_failing_read = !sdr_find_phase_delay(0, 1, grp, NULL, 0,
1937 &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001938 } else {
Marek Vasutea9aa242016-04-04 21:21:05 +02001939 debug_cond(DLEVEL >= 1,
Marek Vasut2f3589c2015-07-19 02:42:21 +02001940 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1941 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001942 }
1943
1944 /*
1945 * The dynamically calculated dtaps_per_ptap is only valid if we
1946 * found a passing/failing read. If we didn't, it means d hit the max
Marek Vasut160695d2015-08-02 19:10:58 +02001947 * (iocfg->dqs_en_delay_max). Otherwise, dtaps_per_ptap retains its
Dinh Nguyen3da42852015-06-02 22:52:49 -05001948 * statically calculated value.
1949 */
1950 if (found_passing_read && found_failing_read)
1951 dtaps_per_ptap = d - initial_failing_dtap;
1952
Marek Vasut1273dd92015-07-12 21:05:08 +02001953 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasutea9aa242016-04-04 21:21:05 +02001954 debug_cond(DLEVEL >= 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001955 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001956
Marek Vasut2f3589c2015-07-19 02:42:21 +02001957 /* Step 6: Find the centre of the window. */
Marek Vasut914546e2015-07-20 09:20:42 +02001958 ret = sdr_find_window_center(grp, work_bgn, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001959
Marek Vasut914546e2015-07-20 09:20:42 +02001960 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001961}
1962
Marek Vasutc4907892015-07-13 02:11:02 +02001963/**
Marek Vasut901dc362015-07-13 02:48:34 +02001964 * search_stop_check() - Check if the detected edge is valid
1965 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1966 * @d: DQS delay
1967 * @rank_bgn: Rank number
1968 * @write_group: Write Group
1969 * @read_group: Read Group
1970 * @bit_chk: Resulting bit mask after the test
1971 * @sticky_bit_chk: Resulting sticky bit mask after the test
1972 * @use_read_test: Perform read test
1973 *
1974 * Test if the found edge is valid.
1975 */
1976static u32 search_stop_check(const int write, const int d, const int rank_bgn,
1977 const u32 write_group, const u32 read_group,
1978 u32 *bit_chk, u32 *sticky_bit_chk,
1979 const u32 use_read_test)
1980{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001981 const u32 ratio = rwcfg->mem_if_read_dqs_width /
1982 rwcfg->mem_if_write_dqs_width;
Marek Vasut901dc362015-07-13 02:48:34 +02001983 const u32 correct_mask = write ? param->write_correct_mask :
1984 param->read_correct_mask;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001985 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
1986 rwcfg->mem_dq_per_read_dqs;
Marek Vasut901dc362015-07-13 02:48:34 +02001987 u32 ret;
1988 /*
1989 * Stop searching when the read test doesn't pass AND when
1990 * we've seen a passing read on every bit.
1991 */
1992 if (write) { /* WRITE-ONLY */
1993 ret = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1994 0, PASS_ONE_BIT,
1995 bit_chk, 0);
1996 } else if (use_read_test) { /* READ-ONLY */
1997 ret = !rw_mgr_mem_calibrate_read_test(rank_bgn, read_group,
1998 NUM_READ_PB_TESTS,
1999 PASS_ONE_BIT, bit_chk,
2000 0, 0);
2001 } else { /* READ-ONLY */
2002 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 0,
2003 PASS_ONE_BIT, bit_chk, 0);
2004 *bit_chk = *bit_chk >> (per_dqs *
2005 (read_group - (write_group * ratio)));
2006 ret = (*bit_chk == 0);
2007 }
2008 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2009 ret = ret && (*sticky_bit_chk == correct_mask);
Marek Vasutea9aa242016-04-04 21:21:05 +02002010 debug_cond(DLEVEL >= 2,
Marek Vasut901dc362015-07-13 02:48:34 +02002011 "%s:%d center(left): dtap=%u => %u == %u && %u",
2012 __func__, __LINE__, d,
2013 *sticky_bit_chk, correct_mask, ret);
2014 return ret;
2015}
2016
2017/**
Marek Vasut71120772015-07-13 02:38:15 +02002018 * search_left_edge() - Find left edge of DQ/DQS working phase
2019 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2020 * @rank_bgn: Rank number
2021 * @write_group: Write Group
2022 * @read_group: Read Group
2023 * @test_bgn: Rank number to begin the test
Marek Vasut71120772015-07-13 02:38:15 +02002024 * @sticky_bit_chk: Resulting sticky bit mask after the test
2025 * @left_edge: Left edge of the DQ/DQS phase
2026 * @right_edge: Right edge of the DQ/DQS phase
2027 * @use_read_test: Perform read test
2028 *
2029 * Find left edge of DQ/DQS working phase.
2030 */
2031static void search_left_edge(const int write, const int rank_bgn,
2032 const u32 write_group, const u32 read_group, const u32 test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002033 u32 *sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002034 int *left_edge, int *right_edge, const u32 use_read_test)
2035{
Marek Vasut139823e2015-08-02 19:47:01 +02002036 const u32 delay_max = write ? iocfg->io_out1_delay_max :
2037 iocfg->io_in_delay_max;
2038 const u32 dqs_max = write ? iocfg->io_out1_delay_max :
2039 iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002040 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2041 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002042 u32 stop, bit_chk;
Marek Vasut71120772015-07-13 02:38:15 +02002043 int i, d;
2044
2045 for (d = 0; d <= dqs_max; d++) {
2046 if (write)
2047 scc_mgr_apply_group_dq_out1_delay(d);
2048 else
2049 scc_mgr_apply_group_dq_in_delay(test_bgn, d);
2050
2051 writel(0, &sdr_scc_mgr->update);
2052
Marek Vasut901dc362015-07-13 02:48:34 +02002053 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002054 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002055 use_read_test);
Marek Vasut71120772015-07-13 02:38:15 +02002056 if (stop == 1)
2057 break;
2058
2059 /* stop != 1 */
2060 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002061 if (bit_chk & 1) {
Marek Vasut71120772015-07-13 02:38:15 +02002062 /*
2063 * Remember a passing test as
2064 * the left_edge.
2065 */
2066 left_edge[i] = d;
2067 } else {
2068 /*
2069 * If a left edge has not been seen
2070 * yet, then a future passing test
2071 * will mark this edge as the right
2072 * edge.
2073 */
2074 if (left_edge[i] == delay_max + 1)
2075 right_edge[i] = -(d + 1);
2076 }
Marek Vasut0c4be192015-07-18 20:34:00 +02002077 bit_chk >>= 1;
Marek Vasut71120772015-07-13 02:38:15 +02002078 }
2079 }
2080
2081 /* Reset DQ delay chains to 0 */
2082 if (write)
2083 scc_mgr_apply_group_dq_out1_delay(0);
2084 else
2085 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2086
2087 *sticky_bit_chk = 0;
2088 for (i = per_dqs - 1; i >= 0; i--) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002089 debug_cond(DLEVEL >= 2,
Marek Vasut71120772015-07-13 02:38:15 +02002090 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2091 __func__, __LINE__, i, left_edge[i],
2092 i, right_edge[i]);
2093
2094 /*
2095 * Check for cases where we haven't found the left edge,
2096 * which makes our assignment of the the right edge invalid.
2097 * Reset it to the illegal value.
2098 */
2099 if ((left_edge[i] == delay_max + 1) &&
2100 (right_edge[i] != delay_max + 1)) {
2101 right_edge[i] = delay_max + 1;
Marek Vasutea9aa242016-04-04 21:21:05 +02002102 debug_cond(DLEVEL >= 2,
Marek Vasut71120772015-07-13 02:38:15 +02002103 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2104 __func__, __LINE__, i, right_edge[i]);
2105 }
2106
2107 /*
2108 * Reset sticky bit
2109 * READ: except for bits where we have seen both
2110 * the left and right edge.
2111 * WRITE: except for bits where we have seen the
2112 * left edge.
2113 */
2114 *sticky_bit_chk <<= 1;
2115 if (write) {
2116 if (left_edge[i] != delay_max + 1)
2117 *sticky_bit_chk |= 1;
2118 } else {
2119 if ((left_edge[i] != delay_max + 1) &&
2120 (right_edge[i] != delay_max + 1))
2121 *sticky_bit_chk |= 1;
2122 }
2123 }
Marek Vasut71120772015-07-13 02:38:15 +02002124}
2125
2126/**
Marek Vasutc4907892015-07-13 02:11:02 +02002127 * search_right_edge() - Find right edge of DQ/DQS working phase
2128 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2129 * @rank_bgn: Rank number
2130 * @write_group: Write Group
2131 * @read_group: Read Group
2132 * @start_dqs: DQS start phase
2133 * @start_dqs_en: DQS enable start phase
Marek Vasutc4907892015-07-13 02:11:02 +02002134 * @sticky_bit_chk: Resulting sticky bit mask after the test
2135 * @left_edge: Left edge of the DQ/DQS phase
2136 * @right_edge: Right edge of the DQ/DQS phase
2137 * @use_read_test: Perform read test
2138 *
2139 * Find right edge of DQ/DQS working phase.
2140 */
2141static int search_right_edge(const int write, const int rank_bgn,
2142 const u32 write_group, const u32 read_group,
2143 const int start_dqs, const int start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002144 u32 *sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002145 int *left_edge, int *right_edge, const u32 use_read_test)
2146{
Marek Vasut139823e2015-08-02 19:47:01 +02002147 const u32 delay_max = write ? iocfg->io_out1_delay_max :
2148 iocfg->io_in_delay_max;
2149 const u32 dqs_max = write ? iocfg->io_out1_delay_max :
2150 iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002151 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2152 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002153 u32 stop, bit_chk;
Marek Vasutc4907892015-07-13 02:11:02 +02002154 int i, d;
2155
2156 for (d = 0; d <= dqs_max - start_dqs; d++) {
2157 if (write) { /* WRITE-ONLY */
2158 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2159 d + start_dqs);
2160 } else { /* READ-ONLY */
2161 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002162 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut5ded7322015-08-02 19:42:26 +02002163 u32 delay = d + start_dqs_en;
Marek Vasut160695d2015-08-02 19:10:58 +02002164 if (delay > iocfg->dqs_en_delay_max)
2165 delay = iocfg->dqs_en_delay_max;
Marek Vasutc4907892015-07-13 02:11:02 +02002166 scc_mgr_set_dqs_en_delay(read_group, delay);
2167 }
2168 scc_mgr_load_dqs(read_group);
2169 }
2170
2171 writel(0, &sdr_scc_mgr->update);
2172
Marek Vasut901dc362015-07-13 02:48:34 +02002173 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002174 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002175 use_read_test);
Marek Vasutc4907892015-07-13 02:11:02 +02002176 if (stop == 1) {
2177 if (write && (d == 0)) { /* WRITE-ONLY */
Marek Vasut139823e2015-08-02 19:47:01 +02002178 for (i = 0; i < rwcfg->mem_dq_per_write_dqs;
2179 i++) {
Marek Vasutc4907892015-07-13 02:11:02 +02002180 /*
2181 * d = 0 failed, but it passed when
2182 * testing the left edge, so it must be
2183 * marginal, set it to -1
2184 */
2185 if (right_edge[i] == delay_max + 1 &&
2186 left_edge[i] != delay_max + 1)
2187 right_edge[i] = -1;
2188 }
2189 }
2190 break;
2191 }
2192
2193 /* stop != 1 */
2194 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002195 if (bit_chk & 1) {
Marek Vasutc4907892015-07-13 02:11:02 +02002196 /*
2197 * Remember a passing test as
2198 * the right_edge.
2199 */
2200 right_edge[i] = d;
2201 } else {
2202 if (d != 0) {
2203 /*
2204 * If a right edge has not
2205 * been seen yet, then a future
2206 * passing test will mark this
2207 * edge as the left edge.
2208 */
2209 if (right_edge[i] == delay_max + 1)
2210 left_edge[i] = -(d + 1);
2211 } else {
2212 /*
2213 * d = 0 failed, but it passed
2214 * when testing the left edge,
2215 * so it must be marginal, set
2216 * it to -1
2217 */
2218 if (right_edge[i] == delay_max + 1 &&
2219 left_edge[i] != delay_max + 1)
2220 right_edge[i] = -1;
2221 /*
2222 * If a right edge has not been
2223 * seen yet, then a future
2224 * passing test will mark this
2225 * edge as the left edge.
2226 */
2227 else if (right_edge[i] == delay_max + 1)
2228 left_edge[i] = -(d + 1);
2229 }
2230 }
2231
Marek Vasutea9aa242016-04-04 21:21:05 +02002232 debug_cond(DLEVEL >= 2, "%s:%d center[r,d=%u]: ",
Marek Vasutc4907892015-07-13 02:11:02 +02002233 __func__, __LINE__, d);
Marek Vasutea9aa242016-04-04 21:21:05 +02002234 debug_cond(DLEVEL >= 2,
Marek Vasutc4907892015-07-13 02:11:02 +02002235 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasut0c4be192015-07-18 20:34:00 +02002236 bit_chk & 1, i, left_edge[i]);
Marek Vasutea9aa242016-04-04 21:21:05 +02002237 debug_cond(DLEVEL >= 2, "right_edge[%u]: %d\n", i,
Marek Vasutc4907892015-07-13 02:11:02 +02002238 right_edge[i]);
Marek Vasut0c4be192015-07-18 20:34:00 +02002239 bit_chk >>= 1;
Marek Vasutc4907892015-07-13 02:11:02 +02002240 }
2241 }
2242
2243 /* Check that all bits have a window */
2244 for (i = 0; i < per_dqs; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002245 debug_cond(DLEVEL >= 2,
Marek Vasutc4907892015-07-13 02:11:02 +02002246 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2247 __func__, __LINE__, i, left_edge[i],
2248 i, right_edge[i]);
2249 if ((left_edge[i] == dqs_max + 1) ||
2250 (right_edge[i] == dqs_max + 1))
2251 return i + 1; /* FIXME: If we fail, retval > 0 */
2252 }
2253
2254 return 0;
2255}
2256
Marek Vasutafb3eb82015-07-18 19:18:06 +02002257/**
2258 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2259 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2260 * @left_edge: Left edge of the DQ/DQS phase
2261 * @right_edge: Right edge of the DQ/DQS phase
2262 * @mid_min: Best DQ/DQS phase middle setting
2263 *
2264 * Find index and value of the middle of the DQ/DQS working phase.
2265 */
2266static int get_window_mid_index(const int write, int *left_edge,
2267 int *right_edge, int *mid_min)
2268{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002269 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2270 rwcfg->mem_dq_per_read_dqs;
Marek Vasutafb3eb82015-07-18 19:18:06 +02002271 int i, mid, min_index;
2272
2273 /* Find middle of window for each DQ bit */
2274 *mid_min = left_edge[0] - right_edge[0];
2275 min_index = 0;
2276 for (i = 1; i < per_dqs; i++) {
2277 mid = left_edge[i] - right_edge[i];
2278 if (mid < *mid_min) {
2279 *mid_min = mid;
2280 min_index = i;
2281 }
2282 }
2283
2284 /*
2285 * -mid_min/2 represents the amount that we need to move DQS.
2286 * If mid_min is odd and positive we'll need to add one to make
2287 * sure the rounding in further calculations is correct (always
2288 * bias to the right), so just add 1 for all positive values.
2289 */
2290 if (*mid_min > 0)
2291 (*mid_min)++;
2292 *mid_min = *mid_min / 2;
2293
Marek Vasutea9aa242016-04-04 21:21:05 +02002294 debug_cond(DLEVEL >= 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
Marek Vasutafb3eb82015-07-18 19:18:06 +02002295 __func__, __LINE__, *mid_min, min_index);
2296 return min_index;
2297}
2298
Marek Vasutffb8b662015-07-18 19:46:26 +02002299/**
2300 * center_dq_windows() - Center the DQ/DQS windows
2301 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2302 * @left_edge: Left edge of the DQ/DQS phase
2303 * @right_edge: Right edge of the DQ/DQS phase
2304 * @mid_min: Adjusted DQ/DQS phase middle setting
2305 * @orig_mid_min: Original DQ/DQS phase middle setting
2306 * @min_index: DQ/DQS phase middle setting index
2307 * @test_bgn: Rank number to begin the test
2308 * @dq_margin: Amount of shift for the DQ
2309 * @dqs_margin: Amount of shift for the DQS
2310 *
2311 * Align the DQ/DQS windows in each group.
2312 */
2313static void center_dq_windows(const int write, int *left_edge, int *right_edge,
2314 const int mid_min, const int orig_mid_min,
2315 const int min_index, const int test_bgn,
2316 int *dq_margin, int *dqs_margin)
2317{
Marek Vasute026b982016-04-05 23:17:35 +02002318 const s32 delay_max = write ? iocfg->io_out1_delay_max :
Marek Vasut139823e2015-08-02 19:47:01 +02002319 iocfg->io_in_delay_max;
Marek Vasute026b982016-04-05 23:17:35 +02002320 const s32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002321 rwcfg->mem_dq_per_read_dqs;
Marek Vasute026b982016-04-05 23:17:35 +02002322 const s32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
Marek Vasutffb8b662015-07-18 19:46:26 +02002323 SCC_MGR_IO_IN_DELAY_OFFSET;
Marek Vasute026b982016-04-05 23:17:35 +02002324 const s32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
Marek Vasutffb8b662015-07-18 19:46:26 +02002325
Marek Vasute026b982016-04-05 23:17:35 +02002326 s32 temp_dq_io_delay1;
Marek Vasutffb8b662015-07-18 19:46:26 +02002327 int shift_dq, i, p;
2328
2329 /* Initialize data for export structures */
2330 *dqs_margin = delay_max + 1;
2331 *dq_margin = delay_max + 1;
2332
2333 /* add delay to bring centre of all DQ windows to the same "level" */
2334 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2335 /* Use values before divide by 2 to reduce round off error */
2336 shift_dq = (left_edge[i] - right_edge[i] -
2337 (left_edge[min_index] - right_edge[min_index]))/2 +
2338 (orig_mid_min - mid_min);
2339
Marek Vasutea9aa242016-04-04 21:21:05 +02002340 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002341 "vfifo_center: before: shift_dq[%u]=%d\n",
2342 i, shift_dq);
2343
Marek Vasute026b982016-04-05 23:17:35 +02002344 temp_dq_io_delay1 = readl(addr + (i << 2));
Marek Vasutffb8b662015-07-18 19:46:26 +02002345
2346 if (shift_dq + temp_dq_io_delay1 > delay_max)
Marek Vasute026b982016-04-05 23:17:35 +02002347 shift_dq = delay_max - temp_dq_io_delay1;
Marek Vasutffb8b662015-07-18 19:46:26 +02002348 else if (shift_dq + temp_dq_io_delay1 < 0)
2349 shift_dq = -temp_dq_io_delay1;
2350
Marek Vasutea9aa242016-04-04 21:21:05 +02002351 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002352 "vfifo_center: after: shift_dq[%u]=%d\n",
2353 i, shift_dq);
2354
2355 if (write)
Marek Vasut139823e2015-08-02 19:47:01 +02002356 scc_mgr_set_dq_out1_delay(i,
2357 temp_dq_io_delay1 + shift_dq);
Marek Vasutffb8b662015-07-18 19:46:26 +02002358 else
Marek Vasut139823e2015-08-02 19:47:01 +02002359 scc_mgr_set_dq_in_delay(p,
2360 temp_dq_io_delay1 + shift_dq);
Marek Vasutffb8b662015-07-18 19:46:26 +02002361
2362 scc_mgr_load_dq(p);
2363
Marek Vasutea9aa242016-04-04 21:21:05 +02002364 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002365 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2366 left_edge[i] - shift_dq + (-mid_min),
2367 right_edge[i] + shift_dq - (-mid_min));
2368
2369 /* To determine values for export structures */
2370 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2371 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2372
2373 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2374 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2375 }
Marek Vasutffb8b662015-07-18 19:46:26 +02002376}
2377
Marek Vasutac63b9a2015-07-21 04:27:32 +02002378/**
2379 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2380 * @rank_bgn: Rank number
2381 * @rw_group: Read/Write Group
2382 * @test_bgn: Rank at which the test begins
2383 * @use_read_test: Perform a read test
2384 * @update_fom: Update FOM
2385 *
2386 * Per-bit deskew DQ and centering.
2387 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002388static int rw_mgr_mem_calibrate_vfifo_center(const u32 rank_bgn,
2389 const u32 rw_group, const u32 test_bgn,
2390 const int use_read_test, const int update_fom)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002391{
Marek Vasut5d6db442015-07-18 19:57:12 +02002392 const u32 addr =
2393 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasut0113c3e2015-07-18 20:42:27 +02002394 (rw_group << 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002395 /*
2396 * Store these as signed since there are comparisons with
2397 * signed numbers.
2398 */
Marek Vasut5ded7322015-08-02 19:42:26 +02002399 u32 sticky_bit_chk;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002400 int32_t left_edge[rwcfg->mem_dq_per_read_dqs];
2401 int32_t right_edge[rwcfg->mem_dq_per_read_dqs];
Dinh Nguyen3da42852015-06-02 22:52:49 -05002402 int32_t orig_mid_min, mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002403 int32_t new_dqs, start_dqs, start_dqs_en = 0, final_dqs_en;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002404 int32_t dq_margin, dqs_margin;
Marek Vasut5d6db442015-07-18 19:57:12 +02002405 int i, min_index;
Marek Vasutc4907892015-07-13 02:11:02 +02002406 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002407
Marek Vasut0113c3e2015-07-18 20:42:27 +02002408 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002409
Marek Vasut5d6db442015-07-18 19:57:12 +02002410 start_dqs = readl(addr);
Marek Vasut160695d2015-08-02 19:10:58 +02002411 if (iocfg->shift_dqs_en_when_shift_dqs)
2412 start_dqs_en = readl(addr - iocfg->dqs_en_delay_offset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002413
2414 /* set the left and right edge of each bit to an illegal value */
Marek Vasut160695d2015-08-02 19:10:58 +02002415 /* use (iocfg->io_in_delay_max + 1) as an illegal value */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002416 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002417 for (i = 0; i < rwcfg->mem_dq_per_read_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002418 left_edge[i] = iocfg->io_in_delay_max + 1;
2419 right_edge[i] = iocfg->io_in_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002420 }
2421
Dinh Nguyen3da42852015-06-02 22:52:49 -05002422 /* Search for the left edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002423 search_left_edge(0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002424 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002425 left_edge, right_edge, use_read_test);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002426
Marek Vasutf0712c32015-07-18 08:01:45 +02002427
Dinh Nguyen3da42852015-06-02 22:52:49 -05002428 /* Search for the right edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002429 ret = search_right_edge(0, rank_bgn, rw_group, rw_group,
Marek Vasutc4907892015-07-13 02:11:02 +02002430 start_dqs, start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002431 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002432 left_edge, right_edge, use_read_test);
2433 if (ret) {
2434 /*
2435 * Restore delay chain settings before letting the loop
2436 * in rw_mgr_mem_calibrate_vfifo to retry different
2437 * dqs/ck relationships.
2438 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002439 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002440 if (iocfg->shift_dqs_en_when_shift_dqs)
Marek Vasut0113c3e2015-07-18 20:42:27 +02002441 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002442
Marek Vasut0113c3e2015-07-18 20:42:27 +02002443 scc_mgr_load_dqs(rw_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002444 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002445
Marek Vasutea9aa242016-04-04 21:21:05 +02002446 debug_cond(DLEVEL >= 1,
Marek Vasutc4907892015-07-13 02:11:02 +02002447 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2448 __func__, __LINE__, i, left_edge[i], right_edge[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002449 if (use_read_test) {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002450 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002451 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002452 CAL_STAGE_VFIFO,
2453 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002454 } else {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002455 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002456 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002457 CAL_STAGE_VFIFO_AFTER_WRITES,
2458 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002459 }
Marek Vasut98668242015-07-18 20:44:28 +02002460 return -EIO;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002461 }
2462
Marek Vasutafb3eb82015-07-18 19:18:06 +02002463 min_index = get_window_mid_index(0, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002464
2465 /* Determine the amount we can change DQS (which is -mid_min) */
2466 orig_mid_min = mid_min;
2467 new_dqs = start_dqs - mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002468 if (new_dqs > iocfg->dqs_in_delay_max)
2469 new_dqs = iocfg->dqs_in_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002470 else if (new_dqs < 0)
2471 new_dqs = 0;
2472
2473 mid_min = start_dqs - new_dqs;
Marek Vasutea9aa242016-04-04 21:21:05 +02002474 debug_cond(DLEVEL >= 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05002475 mid_min, new_dqs);
2476
Marek Vasut160695d2015-08-02 19:10:58 +02002477 if (iocfg->shift_dqs_en_when_shift_dqs) {
2478 if (start_dqs_en - mid_min > iocfg->dqs_en_delay_max)
Marek Vasut139823e2015-08-02 19:47:01 +02002479 mid_min += start_dqs_en - mid_min -
2480 iocfg->dqs_en_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002481 else if (start_dqs_en - mid_min < 0)
2482 mid_min += start_dqs_en - mid_min;
2483 }
2484 new_dqs = start_dqs - mid_min;
2485
Marek Vasutea9aa242016-04-04 21:21:05 +02002486 debug_cond(DLEVEL >= 1,
Marek Vasutf0712c32015-07-18 08:01:45 +02002487 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2488 start_dqs,
Marek Vasut160695d2015-08-02 19:10:58 +02002489 iocfg->shift_dqs_en_when_shift_dqs ? start_dqs_en : -1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002490 new_dqs, mid_min);
2491
Marek Vasutffb8b662015-07-18 19:46:26 +02002492 /* Add delay to bring centre of all DQ windows to the same "level". */
2493 center_dq_windows(0, left_edge, right_edge, mid_min, orig_mid_min,
2494 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002495
Dinh Nguyen3da42852015-06-02 22:52:49 -05002496 /* Move DQS-en */
Marek Vasut160695d2015-08-02 19:10:58 +02002497 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut5d6db442015-07-18 19:57:12 +02002498 final_dqs_en = start_dqs_en - mid_min;
Marek Vasut0113c3e2015-07-18 20:42:27 +02002499 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2500 scc_mgr_load_dqs(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002501 }
2502
2503 /* Move DQS */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002504 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2505 scc_mgr_load_dqs(rw_group);
Marek Vasutea9aa242016-04-04 21:21:05 +02002506 debug_cond(DLEVEL >= 2,
Marek Vasutf0712c32015-07-18 08:01:45 +02002507 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2508 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002509
2510 /*
2511 * Do not remove this line as it makes sure all of our decisions
2512 * have been applied. Apply the update bit.
2513 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002514 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002515
Marek Vasut98668242015-07-18 20:44:28 +02002516 if ((dq_margin < 0) || (dqs_margin < 0))
2517 return -EINVAL;
2518
2519 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002520}
2521
Marek Vasutbce24ef2015-07-17 03:16:45 +02002522/**
Marek Vasut04372fb2015-07-18 02:46:56 +02002523 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
2524 * @rw_group: Read/Write Group
2525 * @phase: DQ/DQS phase
2526 *
2527 * Because initially no communication ca be reliably performed with the memory
2528 * device, the sequencer uses a guaranteed write mechanism to write data into
2529 * the memory device.
2530 */
2531static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
2532 const u32 phase)
2533{
Marek Vasut04372fb2015-07-18 02:46:56 +02002534 int ret;
2535
2536 /* Set a particular DQ/DQS phase. */
2537 scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
2538
Marek Vasutea9aa242016-04-04 21:21:05 +02002539 debug_cond(DLEVEL >= 1, "%s:%d guaranteed write: g=%u p=%u\n",
Marek Vasut04372fb2015-07-18 02:46:56 +02002540 __func__, __LINE__, rw_group, phase);
2541
2542 /*
2543 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2544 * Load up the patterns used by read calibration using the
2545 * current DQDQS phase.
2546 */
2547 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2548
2549 if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2550 return 0;
2551
2552 /*
2553 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2554 * Back-to-Back reads of the patterns used for calibration.
2555 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02002556 ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
2557 if (ret)
Marek Vasutea9aa242016-04-04 21:21:05 +02002558 debug_cond(DLEVEL >= 1,
Marek Vasut04372fb2015-07-18 02:46:56 +02002559 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2560 __func__, __LINE__, rw_group, phase);
Marek Vasutd844c7d2015-07-18 03:55:07 +02002561 return ret;
Marek Vasut04372fb2015-07-18 02:46:56 +02002562}
2563
2564/**
Marek Vasutf09da112015-07-18 02:57:32 +02002565 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2566 * @rw_group: Read/Write Group
2567 * @test_bgn: Rank at which the test begins
2568 *
2569 * DQS enable calibration ensures reliable capture of the DQ signal without
2570 * glitches on the DQS line.
2571 */
2572static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
2573 const u32 test_bgn)
2574{
Marek Vasutf09da112015-07-18 02:57:32 +02002575 /*
2576 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2577 * DQS and DQS Eanble Signal Relationships.
2578 */
Marek Vasut28ea8272015-07-18 04:28:42 +02002579
2580 /* We start at zero, so have one less dq to devide among */
Marek Vasut160695d2015-08-02 19:10:58 +02002581 const u32 delay_step = iocfg->io_in_delay_max /
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002582 (rwcfg->mem_dq_per_read_dqs - 1);
Marek Vasut914546e2015-07-20 09:20:42 +02002583 int ret;
Marek Vasut28ea8272015-07-18 04:28:42 +02002584 u32 i, p, d, r;
2585
2586 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2587
2588 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002589 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002590 r += NUM_RANKS_PER_SHADOW_REG) {
2591 for (i = 0, p = test_bgn, d = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002592 i < rwcfg->mem_dq_per_read_dqs;
Marek Vasut28ea8272015-07-18 04:28:42 +02002593 i++, p++, d += delay_step) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002594 debug_cond(DLEVEL >= 1,
Marek Vasut28ea8272015-07-18 04:28:42 +02002595 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2596 __func__, __LINE__, rw_group, r, i, p, d);
2597
2598 scc_mgr_set_dq_in_delay(p, d);
2599 scc_mgr_load_dq(p);
2600 }
2601
2602 writel(0, &sdr_scc_mgr->update);
2603 }
2604
2605 /*
2606 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2607 * dq_in_delay values
2608 */
Marek Vasut914546e2015-07-20 09:20:42 +02002609 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(rw_group);
Marek Vasut28ea8272015-07-18 04:28:42 +02002610
Marek Vasutea9aa242016-04-04 21:21:05 +02002611 debug_cond(DLEVEL >= 1,
Marek Vasut28ea8272015-07-18 04:28:42 +02002612 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut914546e2015-07-20 09:20:42 +02002613 __func__, __LINE__, rw_group, !ret);
Marek Vasut28ea8272015-07-18 04:28:42 +02002614
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002615 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002616 r += NUM_RANKS_PER_SHADOW_REG) {
2617 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2618 writel(0, &sdr_scc_mgr->update);
2619 }
2620
Marek Vasut914546e2015-07-20 09:20:42 +02002621 return ret;
Marek Vasutf09da112015-07-18 02:57:32 +02002622}
2623
2624/**
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002625 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2626 * @rw_group: Read/Write Group
2627 * @test_bgn: Rank at which the test begins
2628 * @use_read_test: Perform a read test
2629 * @update_fom: Update FOM
2630 *
2631 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2632 * within a group.
2633 */
2634static int
2635rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
2636 const int use_read_test,
2637 const int update_fom)
2638
2639{
2640 int ret, grp_calibrated;
2641 u32 rank_bgn, sr;
2642
2643 /*
2644 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2645 * Read per-bit deskew can be done on a per shadow register basis.
2646 */
2647 grp_calibrated = 1;
2648 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002649 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002650 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002651 ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
Marek Vasut0113c3e2015-07-18 20:42:27 +02002652 test_bgn,
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002653 use_read_test,
2654 update_fom);
Marek Vasut98668242015-07-18 20:44:28 +02002655 if (!ret)
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002656 continue;
2657
2658 grp_calibrated = 0;
2659 }
2660
2661 if (!grp_calibrated)
2662 return -EIO;
2663
2664 return 0;
2665}
2666
2667/**
Marek Vasutbce24ef2015-07-17 03:16:45 +02002668 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2669 * @rw_group: Read/Write Group
2670 * @test_bgn: Rank at which the test begins
Dinh Nguyen3da42852015-06-02 22:52:49 -05002671 *
Marek Vasutbce24ef2015-07-17 03:16:45 +02002672 * Stage 1: Calibrate the read valid prediction FIFO.
2673 *
2674 * This function implements UniPHY calibration Stage 1, as explained in
2675 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2676 *
2677 * - read valid prediction will consist of finding:
2678 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2679 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002680 * - we also do a per-bit deskew on the DQ lines.
2681 */
Marek Vasutc336ca32015-07-17 04:24:18 +02002682static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002683{
Marek Vasut5ded7322015-08-02 19:42:26 +02002684 u32 p, d;
2685 u32 dtaps_per_ptap;
2686 u32 failed_substage;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002687
Marek Vasut04372fb2015-07-18 02:46:56 +02002688 int ret;
2689
Marek Vasutc336ca32015-07-17 04:24:18 +02002690 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002691
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002692 /* Update info for sims */
2693 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002694 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002695 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002696
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002697 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2698
2699 /* USER Determine number of delay taps for each phase tap. */
Marek Vasut160695d2015-08-02 19:10:58 +02002700 dtaps_per_ptap = DIV_ROUND_UP(iocfg->delay_per_opa_tap,
2701 iocfg->delay_per_dqs_en_dchain_tap) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002702
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002703 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002704 /*
2705 * In RLDRAMX we may be messing the delay of pins in
Marek Vasutc336ca32015-07-17 04:24:18 +02002706 * the same write rw_group but outside of the current read
2707 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutac70d2f2015-07-17 03:44:26 +02002708 * output side yet.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002709 */
2710 if (d > 0) {
Marek Vasutf51a7d32015-07-19 02:18:21 +02002711 scc_mgr_apply_group_all_out_delay_add_all_ranks(
Marek Vasutc336ca32015-07-17 04:24:18 +02002712 rw_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002713 }
2714
Marek Vasut160695d2015-08-02 19:10:58 +02002715 for (p = 0; p <= iocfg->dqdqs_out_phase_max; p++) {
Marek Vasut04372fb2015-07-18 02:46:56 +02002716 /* 1) Guaranteed Write */
2717 ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
2718 if (ret)
2719 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002720
Marek Vasutf09da112015-07-18 02:57:32 +02002721 /* 2) DQS Enable Calibration */
2722 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
2723 test_bgn);
2724 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002725 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002726 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002727 }
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002728
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002729 /* 3) Centering DQ/DQS */
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002730 /*
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002731 * If doing read after write calibration, do not update
2732 * FOM now. Do it then.
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002733 */
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002734 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
2735 test_bgn, 1, 0);
2736 if (ret) {
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002737 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002738 continue;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002739 }
2740
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002741 /* All done. */
2742 goto cal_done_ok;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002743 }
2744 }
2745
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002746 /* Calibration Stage 1 failed. */
Marek Vasutc336ca32015-07-17 04:24:18 +02002747 set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002748 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002749
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002750 /* Calibration Stage 1 completed OK. */
2751cal_done_ok:
Dinh Nguyen3da42852015-06-02 22:52:49 -05002752 /*
2753 * Reset the delay chains back to zero if they have moved > 1
2754 * (check for > 1 because loop will increase d even when pass in
2755 * first case).
2756 */
2757 if (d > 2)
Marek Vasutc336ca32015-07-17 04:24:18 +02002758 scc_mgr_zero_group(rw_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002759
2760 return 1;
2761}
2762
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002763/**
2764 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2765 * @rw_group: Read/Write Group
2766 * @test_bgn: Rank at which the test begins
2767 *
2768 * Stage 3: DQ/DQS Centering.
2769 *
2770 * This function implements UniPHY calibration Stage 3, as explained in
2771 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2772 */
2773static int rw_mgr_mem_calibrate_vfifo_end(const u32 rw_group,
2774 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002775{
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002776 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002777
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002778 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002779
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002780 /* Update info for sims. */
2781 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002782 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2783 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2784
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002785 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group, test_bgn, 0, 1);
2786 if (ret)
2787 set_failing_group_stage(rw_group,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002788 CAL_STAGE_VFIFO_AFTER_WRITES,
2789 CAL_SUBSTAGE_VFIFO_CENTER);
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002790 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002791}
2792
Marek Vasutc9842782015-07-21 06:18:57 +02002793/**
2794 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2795 *
2796 * Stage 4: Minimize latency.
2797 *
2798 * This function implements UniPHY calibration Stage 4, as explained in
2799 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2800 * Calibrate LFIFO to find smallest read latency.
2801 */
Marek Vasut5ded7322015-08-02 19:42:26 +02002802static u32 rw_mgr_mem_calibrate_lfifo(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002803{
Marek Vasutc9842782015-07-21 06:18:57 +02002804 int found_one = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002805
2806 debug("%s:%d\n", __func__, __LINE__);
2807
Marek Vasutc9842782015-07-21 06:18:57 +02002808 /* Update info for sims. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002809 reg_file_set_stage(CAL_STAGE_LFIFO);
2810 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2811
2812 /* Load up the patterns used by read calibration for all ranks */
2813 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002814
Dinh Nguyen3da42852015-06-02 22:52:49 -05002815 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002816 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutea9aa242016-04-04 21:21:05 +02002817 debug_cond(DLEVEL >= 2, "%s:%d lfifo: read_lat=%u",
Dinh Nguyen3da42852015-06-02 22:52:49 -05002818 __func__, __LINE__, gbl->curr_read_lat);
2819
Marek Vasutc9842782015-07-21 06:18:57 +02002820 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0, NUM_READ_TESTS,
2821 PASS_ALL_BITS, 1))
Dinh Nguyen3da42852015-06-02 22:52:49 -05002822 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002823
2824 found_one = 1;
Marek Vasutc9842782015-07-21 06:18:57 +02002825 /*
2826 * Reduce read latency and see if things are
2827 * working correctly.
2828 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002829 gbl->curr_read_lat--;
2830 } while (gbl->curr_read_lat > 0);
2831
Marek Vasutc9842782015-07-21 06:18:57 +02002832 /* Reset the fifos to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02002833 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002834
2835 if (found_one) {
Marek Vasutc9842782015-07-21 06:18:57 +02002836 /* Add a fudge factor to the read latency that was determined */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002837 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002838 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutea9aa242016-04-04 21:21:05 +02002839 debug_cond(DLEVEL >= 2,
Marek Vasutc9842782015-07-21 06:18:57 +02002840 "%s:%d lfifo: success: using read_lat=%u\n",
2841 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002842 } else {
2843 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2844 CAL_SUBSTAGE_READ_LATENCY);
2845
Marek Vasutea9aa242016-04-04 21:21:05 +02002846 debug_cond(DLEVEL >= 2,
Marek Vasutc9842782015-07-21 06:18:57 +02002847 "%s:%d lfifo: failed at initial read_lat=%u\n",
2848 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002849 }
Marek Vasutc9842782015-07-21 06:18:57 +02002850
2851 return found_one;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002852}
2853
Marek Vasutc8570af2015-07-21 05:26:58 +02002854/**
2855 * search_window() - Search for the/part of the window with DM/DQS shift
2856 * @search_dm: If 1, search for the DM shift, if 0, search for DQS shift
2857 * @rank_bgn: Rank number
2858 * @write_group: Write Group
2859 * @bgn_curr: Current window begin
2860 * @end_curr: Current window end
2861 * @bgn_best: Current best window begin
2862 * @end_best: Current best window end
2863 * @win_best: Size of the best window
2864 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2865 *
2866 * Search for the/part of the window with DM/DQS shift.
2867 */
2868static void search_window(const int search_dm,
2869 const u32 rank_bgn, const u32 write_group,
2870 int *bgn_curr, int *end_curr, int *bgn_best,
2871 int *end_best, int *win_best, int new_dqs)
2872{
2873 u32 bit_chk;
Marek Vasut160695d2015-08-02 19:10:58 +02002874 const int max = iocfg->io_out1_delay_max - new_dqs;
Marek Vasutc8570af2015-07-21 05:26:58 +02002875 int d, di;
2876
2877 /* Search for the/part of the window with DM/DQS shift. */
2878 for (di = max; di >= 0; di -= DELTA_D) {
2879 if (search_dm) {
2880 d = di;
2881 scc_mgr_apply_group_dm_out1_delay(d);
2882 } else {
2883 /* For DQS, we go from 0...max */
2884 d = max - di;
2885 /*
Marek Vasut139823e2015-08-02 19:47:01 +02002886 * Note: This only shifts DQS, so are we limiting
2887 * ourselves to width of DQ unnecessarily.
Marek Vasutc8570af2015-07-21 05:26:58 +02002888 */
2889 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2890 d + new_dqs);
2891 }
2892
2893 writel(0, &sdr_scc_mgr->update);
2894
2895 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2896 PASS_ALL_BITS, &bit_chk,
2897 0)) {
2898 /* Set current end of the window. */
2899 *end_curr = search_dm ? -d : d;
2900
2901 /*
2902 * If a starting edge of our window has not been seen
2903 * this is our current start of the DM window.
2904 */
Marek Vasut160695d2015-08-02 19:10:58 +02002905 if (*bgn_curr == iocfg->io_out1_delay_max + 1)
Marek Vasutc8570af2015-07-21 05:26:58 +02002906 *bgn_curr = search_dm ? -d : d;
2907
2908 /*
2909 * If current window is bigger than best seen.
2910 * Set best seen to be current window.
2911 */
2912 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2913 *win_best = *end_curr - *bgn_curr + 1;
2914 *bgn_best = *bgn_curr;
2915 *end_best = *end_curr;
2916 }
2917 } else {
2918 /* We just saw a failing test. Reset temp edge. */
Marek Vasut160695d2015-08-02 19:10:58 +02002919 *bgn_curr = iocfg->io_out1_delay_max + 1;
2920 *end_curr = iocfg->io_out1_delay_max + 1;
Marek Vasutc8570af2015-07-21 05:26:58 +02002921
2922 /* Early exit is only applicable to DQS. */
2923 if (search_dm)
2924 continue;
2925
2926 /*
2927 * Early exit optimization: if the remaining delay
2928 * chain space is less than already seen largest
2929 * window we can exit.
2930 */
Marek Vasut160695d2015-08-02 19:10:58 +02002931 if (*win_best - 1 > iocfg->io_out1_delay_max - new_dqs - d)
Marek Vasutc8570af2015-07-21 05:26:58 +02002932 break;
2933 }
2934 }
2935}
2936
Dinh Nguyen3da42852015-06-02 22:52:49 -05002937/*
Marek Vasuta386a502015-07-21 05:33:49 +02002938 * rw_mgr_mem_calibrate_writes_center() - Center all windows
2939 * @rank_bgn: Rank number
2940 * @write_group: Write group
2941 * @test_bgn: Rank at which the test begins
2942 *
2943 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen3da42852015-06-02 22:52:49 -05002944 * certain windows.
2945 */
Marek Vasut3b44f552015-07-21 05:00:42 +02002946static int
2947rw_mgr_mem_calibrate_writes_center(const u32 rank_bgn, const u32 write_group,
2948 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002949{
Marek Vasutc8570af2015-07-21 05:26:58 +02002950 int i;
Marek Vasut3b44f552015-07-21 05:00:42 +02002951 u32 sticky_bit_chk;
2952 u32 min_index;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002953 int left_edge[rwcfg->mem_dq_per_write_dqs];
2954 int right_edge[rwcfg->mem_dq_per_write_dqs];
Marek Vasut3b44f552015-07-21 05:00:42 +02002955 int mid;
2956 int mid_min, orig_mid_min;
2957 int new_dqs, start_dqs;
2958 int dq_margin, dqs_margin, dm_margin;
Marek Vasut160695d2015-08-02 19:10:58 +02002959 int bgn_curr = iocfg->io_out1_delay_max + 1;
2960 int end_curr = iocfg->io_out1_delay_max + 1;
2961 int bgn_best = iocfg->io_out1_delay_max + 1;
2962 int end_best = iocfg->io_out1_delay_max + 1;
Marek Vasut3b44f552015-07-21 05:00:42 +02002963 int win_best = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002964
Marek Vasutc4907892015-07-13 02:11:02 +02002965 int ret;
2966
Dinh Nguyen3da42852015-06-02 22:52:49 -05002967 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2968
2969 dm_margin = 0;
2970
Marek Vasutc6540872015-07-21 05:29:05 +02002971 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
2972 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002973 (rwcfg->mem_dq_per_write_dqs << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002974
Marek Vasut3b44f552015-07-21 05:00:42 +02002975 /* Per-bit deskew. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002976
2977 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02002978 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02002979 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002980 */
2981 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002982 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002983 left_edge[i] = iocfg->io_out1_delay_max + 1;
2984 right_edge[i] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002985 }
2986
Marek Vasut3b44f552015-07-21 05:00:42 +02002987 /* Search for the left edge of the window for each bit. */
Marek Vasut71120772015-07-13 02:38:15 +02002988 search_left_edge(1, rank_bgn, write_group, 0, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002989 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002990 left_edge, right_edge, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002991
Marek Vasut3b44f552015-07-21 05:00:42 +02002992 /* Search for the right edge of the window for each bit. */
Marek Vasutc4907892015-07-13 02:11:02 +02002993 ret = search_right_edge(1, rank_bgn, write_group, 0,
2994 start_dqs, 0,
Marek Vasut0c4be192015-07-18 20:34:00 +02002995 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002996 left_edge, right_edge, 0);
2997 if (ret) {
2998 set_failing_group_stage(test_bgn + ret - 1, CAL_STAGE_WRITES,
2999 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutd043ee52015-07-21 05:32:49 +02003000 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003001 }
3002
Marek Vasutafb3eb82015-07-18 19:18:06 +02003003 min_index = get_window_mid_index(1, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003004
Marek Vasut3b44f552015-07-21 05:00:42 +02003005 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003006 orig_mid_min = mid_min;
3007 new_dqs = start_dqs;
3008 mid_min = 0;
Marek Vasutea9aa242016-04-04 21:21:05 +02003009 debug_cond(DLEVEL >= 1,
Marek Vasut3b44f552015-07-21 05:00:42 +02003010 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3011 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003012
Marek Vasutffb8b662015-07-18 19:46:26 +02003013 /* Add delay to bring centre of all DQ windows to the same "level". */
3014 center_dq_windows(1, left_edge, right_edge, mid_min, orig_mid_min,
3015 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003016
3017 /* Move DQS */
3018 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02003019 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003020
3021 /* Centre DM */
Marek Vasutea9aa242016-04-04 21:21:05 +02003022 debug_cond(DLEVEL >= 2, "%s:%d write_center: DM\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003023
3024 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003025 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02003026 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003027 */
Marek Vasut160695d2015-08-02 19:10:58 +02003028 left_edge[0] = iocfg->io_out1_delay_max + 1;
3029 right_edge[0] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003030
Marek Vasut3b44f552015-07-21 05:00:42 +02003031 /* Search for the/part of the window with DM shift. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003032 search_window(1, rank_bgn, write_group, &bgn_curr, &end_curr,
3033 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003034
Marek Vasut3b44f552015-07-21 05:00:42 +02003035 /* Reset DM delay chains to 0. */
Marek Vasut32675242015-07-17 06:07:13 +02003036 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003037
3038 /*
3039 * Check to see if the current window nudges up aganist 0 delay.
3040 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasut3b44f552015-07-21 05:00:42 +02003041 * search begins as a new search.
3042 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003043 if (end_curr != 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02003044 bgn_curr = iocfg->io_out1_delay_max + 1;
3045 end_curr = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003046 }
3047
Marek Vasut3b44f552015-07-21 05:00:42 +02003048 /* Search for the/part of the window with DQS shifts. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003049 search_window(0, rank_bgn, write_group, &bgn_curr, &end_curr,
3050 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003051
Marek Vasut3b44f552015-07-21 05:00:42 +02003052 /* Assign left and right edge for cal and reporting. */
3053 left_edge[0] = -1 * bgn_best;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003054 right_edge[0] = end_best;
3055
Marek Vasutea9aa242016-04-04 21:21:05 +02003056 debug_cond(DLEVEL >= 2, "%s:%d dm_calib: left=%d right=%d\n",
Marek Vasut3b44f552015-07-21 05:00:42 +02003057 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003058
Marek Vasut3b44f552015-07-21 05:00:42 +02003059 /* Move DQS (back to orig). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003060 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3061
3062 /* Move DM */
3063
Marek Vasut3b44f552015-07-21 05:00:42 +02003064 /* Find middle of window for the DM bit. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003065 mid = (left_edge[0] - right_edge[0]) / 2;
3066
Marek Vasut3b44f552015-07-21 05:00:42 +02003067 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003068 if (mid < 0)
3069 mid = 0;
3070
Marek Vasut3b44f552015-07-21 05:00:42 +02003071 /* dm_marign should fail if we never find a window. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003072 if (win_best == 0)
3073 dm_margin = -1;
3074 else
3075 dm_margin = left_edge[0] - mid;
3076
Marek Vasut32675242015-07-17 06:07:13 +02003077 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003078 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003079
Marek Vasutea9aa242016-04-04 21:21:05 +02003080 debug_cond(DLEVEL >= 2,
Marek Vasut3b44f552015-07-21 05:00:42 +02003081 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3082 __func__, __LINE__, left_edge[0], right_edge[0],
3083 mid, dm_margin);
3084 /* Export values. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003085 gbl->fom_out += dq_margin + dqs_margin;
3086
Marek Vasutea9aa242016-04-04 21:21:05 +02003087 debug_cond(DLEVEL >= 2,
Marek Vasut3b44f552015-07-21 05:00:42 +02003088 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3089 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003090
3091 /*
3092 * Do not remove this line as it makes sure all of our
3093 * decisions have been applied.
3094 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003095 writel(0, &sdr_scc_mgr->update);
Marek Vasut3b44f552015-07-21 05:00:42 +02003096
Marek Vasutd043ee52015-07-21 05:32:49 +02003097 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3098 return -EINVAL;
3099
3100 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003101}
3102
Marek Vasutdb3a6062015-07-18 07:23:25 +02003103/**
3104 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3105 * @rank_bgn: Rank number
3106 * @group: Read/Write Group
3107 * @test_bgn: Rank at which the test begins
3108 *
3109 * Stage 2: Write Calibration Part One.
3110 *
3111 * This function implements UniPHY calibration Stage 2, as explained in
3112 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3113 */
3114static int rw_mgr_mem_calibrate_writes(const u32 rank_bgn, const u32 group,
3115 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003116{
Marek Vasutdb3a6062015-07-18 07:23:25 +02003117 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003118
Marek Vasutdb3a6062015-07-18 07:23:25 +02003119 /* Update info for sims */
3120 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3121
3122 reg_file_set_group(group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003123 reg_file_set_stage(CAL_STAGE_WRITES);
3124 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3125
Marek Vasutdb3a6062015-07-18 07:23:25 +02003126 ret = rw_mgr_mem_calibrate_writes_center(rank_bgn, group, test_bgn);
Marek Vasutd043ee52015-07-21 05:32:49 +02003127 if (ret)
Marek Vasutdb3a6062015-07-18 07:23:25 +02003128 set_failing_group_stage(group, CAL_STAGE_WRITES,
Dinh Nguyen3da42852015-06-02 22:52:49 -05003129 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003130
Marek Vasutd043ee52015-07-21 05:32:49 +02003131 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003132}
3133
Marek Vasut4b0ac262015-07-20 07:33:33 +02003134/**
3135 * mem_precharge_and_activate() - Precharge all banks and activate
3136 *
3137 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3138 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003139static void mem_precharge_and_activate(void)
3140{
Marek Vasut4b0ac262015-07-20 07:33:33 +02003141 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003142
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003143 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasut4b0ac262015-07-20 07:33:33 +02003144 /* Set rank. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003145 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3146
Marek Vasut4b0ac262015-07-20 07:33:33 +02003147 /* Precharge all banks. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003148 writel(rwcfg->precharge_all, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003149 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003150
Marek Vasut1273dd92015-07-12 21:05:08 +02003151 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003152 writel(rwcfg->activate_0_and_1_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02003153 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003154
Marek Vasut1273dd92015-07-12 21:05:08 +02003155 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003156 writel(rwcfg->activate_0_and_1_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02003157 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003158
Marek Vasut4b0ac262015-07-20 07:33:33 +02003159 /* Activate rows. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003160 writel(rwcfg->activate_0_and_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003161 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003162 }
3163}
3164
Marek Vasut16502a02015-07-17 01:57:41 +02003165/**
3166 * mem_init_latency() - Configure memory RLAT and WLAT settings
3167 *
3168 * Configure memory RLAT and WLAT parameters.
3169 */
3170static void mem_init_latency(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003171{
Marek Vasut16502a02015-07-17 01:57:41 +02003172 /*
3173 * For AV/CV, LFIFO is hardened and always runs at full rate
3174 * so max latency in AFI clocks, used here, is correspondingly
3175 * smaller.
3176 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003177 const u32 max_latency = (1 << misccfg->max_latency_count_width) - 1;
Marek Vasut16502a02015-07-17 01:57:41 +02003178 u32 rlat, wlat;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003179
3180 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut16502a02015-07-17 01:57:41 +02003181
3182 /*
3183 * Read in write latency.
3184 * WL for Hard PHY does not include additive latency.
3185 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003186 wlat = readl(&data_mgr->t_wl_add);
3187 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003188
Marek Vasut16502a02015-07-17 01:57:41 +02003189 gbl->rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003190
Marek Vasut16502a02015-07-17 01:57:41 +02003191 /* Read in readl latency. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003192 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003193
Marek Vasut16502a02015-07-17 01:57:41 +02003194 /* Set a pretty high read latency initially. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003195 gbl->curr_read_lat = rlat + 16;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003196 if (gbl->curr_read_lat > max_latency)
3197 gbl->curr_read_lat = max_latency;
3198
Marek Vasut1273dd92015-07-12 21:05:08 +02003199 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003200
Marek Vasut16502a02015-07-17 01:57:41 +02003201 /* Advertise write latency. */
3202 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003203}
3204
Marek Vasut51cea0b2015-07-26 10:54:15 +02003205/**
3206 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3207 *
3208 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3209 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003210static void mem_skip_calibrate(void)
3211{
Marek Vasut5ded7322015-08-02 19:42:26 +02003212 u32 vfifo_offset;
3213 u32 i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003214
3215 debug("%s:%d\n", __func__, __LINE__);
3216 /* Need to update every shadow register set used by the interface */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003217 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003218 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003219 /*
3220 * Set output phase alignment settings appropriate for
3221 * skip calibration.
3222 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003223 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003224 scc_mgr_set_dqs_en_phase(i, 0);
Marek Vasut160695d2015-08-02 19:10:58 +02003225 if (iocfg->dll_chain_length == 6)
3226 scc_mgr_set_dqdqs_output_phase(i, 6);
3227 else
3228 scc_mgr_set_dqdqs_output_phase(i, 7);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003229 /*
3230 * Case:33398
3231 *
3232 * Write data arrives to the I/O two cycles before write
3233 * latency is reached (720 deg).
3234 * -> due to bit-slip in a/c bus
3235 * -> to allow board skew where dqs is longer than ck
3236 * -> how often can this happen!?
3237 * -> can claim back some ptaps for high freq
3238 * support if we can relax this, but i digress...
3239 *
3240 * The write_clk leads mem_ck by 90 deg
3241 * The minimum ptap of the OPA is 180 deg
3242 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3243 * The write_clk is always delayed by 2 ptaps
3244 *
3245 * Hence, to make DQS aligned to CK, we need to delay
3246 * DQS by:
Marek Vasut139823e2015-08-02 19:47:01 +02003247 * (720 - 90 - 180 - 2) *
3248 * (360 / iocfg->dll_chain_length)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003249 *
Marek Vasut160695d2015-08-02 19:10:58 +02003250 * Dividing the above by (360 / iocfg->dll_chain_length)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003251 * gives us the number of ptaps, which simplies to:
3252 *
Marek Vasut160695d2015-08-02 19:10:58 +02003253 * (1.25 * iocfg->dll_chain_length - 2)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003254 */
Marek Vasut51cea0b2015-07-26 10:54:15 +02003255 scc_mgr_set_dqdqs_output_phase(i,
Marek Vasut6d7a3332015-08-10 22:50:11 +02003256 ((125 * iocfg->dll_chain_length) / 100) - 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003257 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003258 writel(0xff, &sdr_scc_mgr->dqs_ena);
3259 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003260
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003261 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003262 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3263 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003264 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003265 writel(0xff, &sdr_scc_mgr->dq_ena);
3266 writel(0xff, &sdr_scc_mgr->dm_ena);
3267 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003268 }
3269
3270 /* Compensate for simulation model behaviour */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003271 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003272 scc_mgr_set_dqs_bus_in_delay(i, 10);
3273 scc_mgr_load_dqs(i);
3274 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003275 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003276
3277 /*
3278 * ArriaV has hard FIFOs that can only be initialized by incrementing
3279 * in sequencer.
3280 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003281 vfifo_offset = misccfg->calib_vfifo_offset;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003282 for (j = 0; j < vfifo_offset; j++)
Marek Vasut1273dd92015-07-12 21:05:08 +02003283 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasut1273dd92015-07-12 21:05:08 +02003284 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003285
3286 /*
Marek Vasut51cea0b2015-07-26 10:54:15 +02003287 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3288 * setting from generation-time constant.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003289 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003290 gbl->curr_read_lat = misccfg->calib_lfifo_offset;
Marek Vasut1273dd92015-07-12 21:05:08 +02003291 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003292}
3293
Marek Vasut3589fbf2015-07-20 04:34:51 +02003294/**
3295 * mem_calibrate() - Memory calibration entry point.
3296 *
3297 * Perform memory calibration.
3298 */
Marek Vasut5ded7322015-08-02 19:42:26 +02003299static u32 mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003300{
Marek Vasut5ded7322015-08-02 19:42:26 +02003301 u32 i;
3302 u32 rank_bgn, sr;
3303 u32 write_group, write_test_bgn;
3304 u32 read_group, read_test_bgn;
3305 u32 run_groups, current_run;
3306 u32 failing_groups = 0;
3307 u32 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003308
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003309 const u32 rwdqs_ratio = rwcfg->mem_if_read_dqs_width /
3310 rwcfg->mem_if_write_dqs_width;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003311
Dinh Nguyen3da42852015-06-02 22:52:49 -05003312 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003313
Marek Vasut16502a02015-07-17 01:57:41 +02003314 /* Initialize the data settings */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003315 gbl->error_substage = CAL_SUBSTAGE_NIL;
3316 gbl->error_stage = CAL_STAGE_NIL;
3317 gbl->error_group = 0xff;
3318 gbl->fom_in = 0;
3319 gbl->fom_out = 0;
3320
Marek Vasut16502a02015-07-17 01:57:41 +02003321 /* Initialize WLAT and RLAT. */
3322 mem_init_latency();
3323
3324 /* Initialize bit slips. */
3325 mem_precharge_and_activate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003326
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003327 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003328 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3329 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003330 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3331 if (i == 0)
3332 scc_mgr_set_hhp_extras();
3333
Marek Vasutc5c5f532015-07-17 02:06:20 +02003334 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003335 }
3336
Marek Vasut722c9682015-07-17 02:07:12 +02003337 /* Calibration is skipped. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003338 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3339 /*
3340 * Set VFIFO and LFIFO to instant-on settings in skip
3341 * calibration mode.
3342 */
3343 mem_skip_calibrate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003344
Marek Vasut722c9682015-07-17 02:07:12 +02003345 /*
3346 * Do not remove this line as it makes sure all of our
3347 * decisions have been applied.
3348 */
3349 writel(0, &sdr_scc_mgr->update);
3350 return 1;
3351 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003352
Marek Vasut722c9682015-07-17 02:07:12 +02003353 /* Calibration is not skipped. */
3354 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3355 /*
3356 * Zero all delay chain/phase settings for all
3357 * groups and all shadow register sets.
3358 */
3359 scc_mgr_zero_all();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003360
Marek Vasutf085ac32015-08-02 18:27:21 +02003361 run_groups = ~0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003362
Marek Vasut722c9682015-07-17 02:07:12 +02003363 for (write_group = 0, write_test_bgn = 0; write_group
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003364 < rwcfg->mem_if_write_dqs_width; write_group++,
3365 write_test_bgn += rwcfg->mem_dq_per_write_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003366 /* Initialize the group failure */
Marek Vasut722c9682015-07-17 02:07:12 +02003367 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003368
Marek Vasut722c9682015-07-17 02:07:12 +02003369 current_run = run_groups & ((1 <<
3370 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3371 run_groups = run_groups >>
3372 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003373
Marek Vasut722c9682015-07-17 02:07:12 +02003374 if (current_run == 0)
3375 continue;
3376
3377 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3378 SCC_MGR_GROUP_COUNTER_OFFSET);
3379 scc_mgr_zero_group(write_group, 0);
3380
Marek Vasut33c42bb2015-07-17 02:21:47 +02003381 for (read_group = write_group * rwdqs_ratio,
3382 read_test_bgn = 0;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003383 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003384 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003385 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasut33c42bb2015-07-17 02:21:47 +02003386 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3387 continue;
Marek Vasut722c9682015-07-17 02:07:12 +02003388
Marek Vasut33c42bb2015-07-17 02:21:47 +02003389 /* Calibrate the VFIFO */
3390 if (rw_mgr_mem_calibrate_vfifo(read_group,
3391 read_test_bgn))
3392 continue;
3393
Marek Vasut139823e2015-08-02 19:47:01 +02003394 if (!(gbl->phy_debug_mode_flags &
3395 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003396 return 0;
3397
3398 /* The group failed, we're done. */
3399 goto grp_failed;
3400 }
3401
3402 /* Calibrate the output side */
3403 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003404 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003405 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3406 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3407 continue;
3408
3409 /* Not needed in quick mode! */
Marek Vasut139823e2015-08-02 19:47:01 +02003410 if (STATIC_CALIB_STEPS &
3411 CALIB_SKIP_DELAY_SWEEPS)
Marek Vasutc452dcd2015-07-17 02:50:56 +02003412 continue;
3413
Marek Vasutc452dcd2015-07-17 02:50:56 +02003414 /* Calibrate WRITEs */
Marek Vasutdb3a6062015-07-18 07:23:25 +02003415 if (!rw_mgr_mem_calibrate_writes(rank_bgn,
Marek Vasut139823e2015-08-02 19:47:01 +02003416 write_group,
3417 write_test_bgn))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003418 continue;
3419
Marek Vasut33c42bb2015-07-17 02:21:47 +02003420 group_failed = 1;
Marek Vasut139823e2015-08-02 19:47:01 +02003421 if (!(gbl->phy_debug_mode_flags &
3422 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasut33c42bb2015-07-17 02:21:47 +02003423 return 0;
Marek Vasut722c9682015-07-17 02:07:12 +02003424 }
3425
Marek Vasutc452dcd2015-07-17 02:50:56 +02003426 /* Some group failed, we're done. */
3427 if (group_failed)
3428 goto grp_failed;
Marek Vasut4ac21612015-07-17 02:31:04 +02003429
Marek Vasutc452dcd2015-07-17 02:50:56 +02003430 for (read_group = write_group * rwdqs_ratio,
3431 read_test_bgn = 0;
3432 read_group < (write_group + 1) * rwdqs_ratio;
3433 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003434 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003435 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3436 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003437
Marek Vasut78cdd7d2015-07-18 05:58:44 +02003438 if (!rw_mgr_mem_calibrate_vfifo_end(read_group,
Marek Vasut139823e2015-08-02 19:47:01 +02003439 read_test_bgn))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003440 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003441
Marek Vasut139823e2015-08-02 19:47:01 +02003442 if (!(gbl->phy_debug_mode_flags &
3443 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003444 return 0;
Marek Vasut4ac21612015-07-17 02:31:04 +02003445
Marek Vasutc452dcd2015-07-17 02:50:56 +02003446 /* The group failed, we're done. */
3447 goto grp_failed;
Marek Vasut722c9682015-07-17 02:07:12 +02003448 }
3449
Marek Vasutc452dcd2015-07-17 02:50:56 +02003450 /* No group failed, continue as usual. */
3451 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003452
Marek Vasutc452dcd2015-07-17 02:50:56 +02003453grp_failed: /* A group failed, increment the counter. */
3454 failing_groups++;
Marek Vasut722c9682015-07-17 02:07:12 +02003455 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003456
Marek Vasut722c9682015-07-17 02:07:12 +02003457 /*
3458 * USER If there are any failing groups then report
3459 * the failure.
3460 */
3461 if (failing_groups != 0)
3462 return 0;
3463
Marek Vasutc50ae302015-07-17 02:40:21 +02003464 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3465 continue;
3466
Marek Vasut722c9682015-07-17 02:07:12 +02003467 /* Calibrate the LFIFO */
Marek Vasutc50ae302015-07-17 02:40:21 +02003468 if (!rw_mgr_mem_calibrate_lfifo())
3469 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003470 }
3471
3472 /*
3473 * Do not remove this line as it makes sure all of our decisions
3474 * have been applied.
3475 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003476 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003477 return 1;
3478}
3479
Marek Vasut23a040c2015-07-17 01:20:21 +02003480/**
3481 * run_mem_calibrate() - Perform memory calibration
3482 *
3483 * This function triggers the entire memory calibration procedure.
3484 */
3485static int run_mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003486{
Marek Vasut23a040c2015-07-17 01:20:21 +02003487 int pass;
Marek Vasutbba77112016-04-05 23:41:56 +02003488 u32 ctrl_cfg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003489
3490 debug("%s:%d\n", __func__, __LINE__);
3491
3492 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003493 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003494
Marek Vasut23a040c2015-07-17 01:20:21 +02003495 /* Stop tracking manager. */
Marek Vasutbba77112016-04-05 23:41:56 +02003496 ctrl_cfg = readl(&sdr_ctrl->ctrl_cfg);
3497 writel(ctrl_cfg & ~SDR_CTRLGRP_CTRLCFG_DQSTRKEN_MASK,
3498 &sdr_ctrl->ctrl_cfg);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003499
Marek Vasut9fa9c902015-07-17 01:12:07 +02003500 phy_mgr_initialize();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003501 rw_mgr_mem_initialize();
3502
Marek Vasut23a040c2015-07-17 01:20:21 +02003503 /* Perform the actual memory calibration. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003504 pass = mem_calibrate();
3505
3506 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003507 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003508
Marek Vasut23a040c2015-07-17 01:20:21 +02003509 /* Handoff. */
3510 rw_mgr_mem_handoff();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003511 /*
Marek Vasut23a040c2015-07-17 01:20:21 +02003512 * In Hard PHY this is a 2-bit control:
3513 * 0: AFI Mux Select
3514 * 1: DDIO Mux Select
Dinh Nguyen3da42852015-06-02 22:52:49 -05003515 */
Marek Vasut23a040c2015-07-17 01:20:21 +02003516 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003517
Marek Vasut23a040c2015-07-17 01:20:21 +02003518 /* Start tracking manager. */
Marek Vasutbba77112016-04-05 23:41:56 +02003519 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Marek Vasut23a040c2015-07-17 01:20:21 +02003520
3521 return pass;
3522}
3523
3524/**
3525 * debug_mem_calibrate() - Report result of memory calibration
3526 * @pass: Value indicating whether calibration passed or failed
3527 *
3528 * This function reports the results of the memory calibration
3529 * and writes debug information into the register file.
3530 */
3531static void debug_mem_calibrate(int pass)
3532{
Marek Vasut5ded7322015-08-02 19:42:26 +02003533 u32 debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003534
3535 if (pass) {
Goldschmidt Simon92962b32018-01-25 06:04:44 +00003536 debug("%s: CALIBRATION PASSED\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003537
3538 gbl->fom_in /= 2;
3539 gbl->fom_out /= 2;
3540
3541 if (gbl->fom_in > 0xff)
3542 gbl->fom_in = 0xff;
3543
3544 if (gbl->fom_out > 0xff)
3545 gbl->fom_out = 0xff;
3546
3547 /* Update the FOM in the register file */
3548 debug_info = gbl->fom_in;
3549 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003550 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003551
Marek Vasut1273dd92015-07-12 21:05:08 +02003552 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3553 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003554 } else {
Goldschmidt Simon92962b32018-01-25 06:04:44 +00003555 debug("%s: CALIBRATION FAILED\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003556
3557 debug_info = gbl->error_stage;
3558 debug_info |= gbl->error_substage << 8;
3559 debug_info |= gbl->error_group << 16;
3560
Marek Vasut1273dd92015-07-12 21:05:08 +02003561 writel(debug_info, &sdr_reg_file->failing_stage);
3562 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3563 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003564
3565 /* Update the failing group/stage in the register file */
3566 debug_info = gbl->error_stage;
3567 debug_info |= gbl->error_substage << 8;
3568 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003569 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003570 }
3571
Goldschmidt Simon92962b32018-01-25 06:04:44 +00003572 debug("%s: Calibration complete\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003573}
3574
Marek Vasutbb064342015-07-19 06:12:42 +02003575/**
3576 * hc_initialize_rom_data() - Initialize ROM data
3577 *
3578 * Initialize ROM data.
3579 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003580static void hc_initialize_rom_data(void)
3581{
Marek Vasut04955cf2015-08-02 17:15:19 +02003582 unsigned int nelem = 0;
3583 const u32 *rom_init;
Marek Vasutbb064342015-07-19 06:12:42 +02003584 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003585
Marek Vasut04955cf2015-08-02 17:15:19 +02003586 socfpga_get_seq_inst_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003587 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003588 for (i = 0; i < nelem; i++)
3589 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003590
Marek Vasut04955cf2015-08-02 17:15:19 +02003591 socfpga_get_seq_ac_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003592 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003593 for (i = 0; i < nelem; i++)
3594 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003595}
3596
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003597/**
3598 * initialize_reg_file() - Initialize SDR register file
3599 *
3600 * Initialize SDR register file.
3601 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003602static void initialize_reg_file(void)
3603{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003604 /* Initialize the register file with the correct data */
Marek Vasut96fd4362015-08-02 19:26:55 +02003605 writel(misccfg->reg_file_init_seq_signature, &sdr_reg_file->signature);
Marek Vasut1273dd92015-07-12 21:05:08 +02003606 writel(0, &sdr_reg_file->debug_data_addr);
3607 writel(0, &sdr_reg_file->cur_stage);
3608 writel(0, &sdr_reg_file->fom);
3609 writel(0, &sdr_reg_file->failing_stage);
3610 writel(0, &sdr_reg_file->debug1);
3611 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003612}
3613
Marek Vasut2ca151f2015-07-19 06:14:04 +02003614/**
3615 * initialize_hps_phy() - Initialize HPS PHY
3616 *
3617 * Initialize HPS PHY.
3618 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003619static void initialize_hps_phy(void)
3620{
Marek Vasut5ded7322015-08-02 19:42:26 +02003621 u32 reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003622 /*
3623 * Tracking also gets configured here because it's in the
3624 * same register.
3625 */
Marek Vasut5ded7322015-08-02 19:42:26 +02003626 u32 trk_sample_count = 7500;
3627 u32 trk_long_idle_sample_count = (10 << 16) | 100;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003628 /*
3629 * Format is number of outer loops in the 16 MSB, sample
3630 * count in 16 LSB.
3631 */
3632
3633 reg = 0;
3634 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3635 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3636 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3637 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3638 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3639 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3640 /*
3641 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3642 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3643 */
3644 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3645 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3646 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003647 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003648
3649 reg = 0;
3650 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3651 trk_sample_count >>
3652 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3653 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3654 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003655 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003656
3657 reg = 0;
3658 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3659 trk_long_idle_sample_count >>
3660 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003661 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003662}
3663
Marek Vasut880e46f2015-07-17 00:45:11 +02003664/**
3665 * initialize_tracking() - Initialize tracking
3666 *
3667 * Initialize the register file with usable initial data.
3668 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003669static void initialize_tracking(void)
3670{
Marek Vasut880e46f2015-07-17 00:45:11 +02003671 /*
3672 * Initialize the register file with the correct data.
3673 * Compute usable version of value in case we skip full
3674 * computation later.
3675 */
Marek Vasut139823e2015-08-02 19:47:01 +02003676 writel(DIV_ROUND_UP(iocfg->delay_per_opa_tap,
3677 iocfg->delay_per_dchain_tap) - 1,
Marek Vasut880e46f2015-07-17 00:45:11 +02003678 &sdr_reg_file->dtaps_per_ptap);
3679
3680 /* trk_sample_count */
3681 writel(7500, &sdr_reg_file->trk_sample_count);
3682
3683 /* longidle outer loop [15:0] */
3684 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003685
3686 /*
Marek Vasut880e46f2015-07-17 00:45:11 +02003687 * longidle sample count [31:24]
3688 * trfc, worst case of 933Mhz 4Gb [23:16]
3689 * trcd, worst case [15:8]
3690 * vfifo wait [7:0]
Dinh Nguyen3da42852015-06-02 22:52:49 -05003691 */
Marek Vasut880e46f2015-07-17 00:45:11 +02003692 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3693 &sdr_reg_file->delays);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003694
Marek Vasut880e46f2015-07-17 00:45:11 +02003695 /* mux delay */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003696 writel((rwcfg->idle << 24) | (rwcfg->activate_1 << 16) |
3697 (rwcfg->sgle_read << 8) | (rwcfg->precharge_all << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003698 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003699
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003700 writel(rwcfg->mem_if_read_dqs_width,
Marek Vasut880e46f2015-07-17 00:45:11 +02003701 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003702
Marek Vasut880e46f2015-07-17 00:45:11 +02003703 /* trefi [7:0] */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003704 writel((rwcfg->refresh_all << 24) | (1000 << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003705 &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003706}
3707
3708int sdram_calibration_full(void)
3709{
3710 struct param_type my_param;
3711 struct gbl_type my_gbl;
Marek Vasut5ded7322015-08-02 19:42:26 +02003712 u32 pass;
Marek Vasut84e0b0c2015-07-17 01:05:36 +02003713
3714 memset(&my_param, 0, sizeof(my_param));
3715 memset(&my_gbl, 0, sizeof(my_gbl));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003716
3717 param = &my_param;
3718 gbl = &my_gbl;
3719
Marek Vasutd718a262015-08-02 18:12:08 +02003720 rwcfg = socfpga_get_sdram_rwmgr_config();
Marek Vasut10c14262015-08-02 19:00:23 +02003721 iocfg = socfpga_get_sdram_io_config();
Marek Vasut042ff2d2015-08-02 19:18:47 +02003722 misccfg = socfpga_get_sdram_misc_config();
Marek Vasutd718a262015-08-02 18:12:08 +02003723
Dinh Nguyen3da42852015-06-02 22:52:49 -05003724 /* Set the calibration enabled by default */
3725 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3726 /*
3727 * Only sweep all groups (regardless of fail state) by default
3728 * Set enabled read test by default.
3729 */
3730#if DISABLE_GUARANTEED_READ
3731 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3732#endif
3733 /* Initialize the register file */
3734 initialize_reg_file();
3735
3736 /* Initialize any PHY CSR */
3737 initialize_hps_phy();
3738
3739 scc_mgr_initialize();
3740
3741 initialize_tracking();
3742
Goldschmidt Simon92962b32018-01-25 06:04:44 +00003743 debug("%s: Preparing to start memory calibration\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003744
3745 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutea9aa242016-04-04 21:21:05 +02003746 debug_cond(DLEVEL >= 1,
Marek Vasut23f62b32015-07-13 01:05:27 +02003747 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003748 rwcfg->mem_number_of_ranks, rwcfg->mem_number_of_cs_per_dimm,
3749 rwcfg->mem_dq_per_read_dqs, rwcfg->mem_dq_per_write_dqs,
3750 rwcfg->mem_virtual_groups_per_read_dqs,
3751 rwcfg->mem_virtual_groups_per_write_dqs);
Marek Vasutea9aa242016-04-04 21:21:05 +02003752 debug_cond(DLEVEL >= 1,
Marek Vasut23f62b32015-07-13 01:05:27 +02003753 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003754 rwcfg->mem_if_read_dqs_width, rwcfg->mem_if_write_dqs_width,
3755 rwcfg->mem_data_width, rwcfg->mem_data_mask_width,
Marek Vasut160695d2015-08-02 19:10:58 +02003756 iocfg->delay_per_opa_tap, iocfg->delay_per_dchain_tap);
Marek Vasutea9aa242016-04-04 21:21:05 +02003757 debug_cond(DLEVEL >= 1, "dtap_dqsen_delay=%u, dll=%u",
Marek Vasut160695d2015-08-02 19:10:58 +02003758 iocfg->delay_per_dqs_en_dchain_tap, iocfg->dll_chain_length);
Marek Vasutea9aa242016-04-04 21:21:05 +02003759 debug_cond(DLEVEL >= 1,
Marek Vasut139823e2015-08-02 19:47:01 +02003760 "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003761 iocfg->dqs_en_phase_max, iocfg->dqdqs_out_phase_max,
3762 iocfg->dqs_en_delay_max, iocfg->dqs_in_delay_max);
Marek Vasutea9aa242016-04-04 21:21:05 +02003763 debug_cond(DLEVEL >= 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003764 iocfg->io_in_delay_max, iocfg->io_out1_delay_max,
3765 iocfg->io_out2_delay_max);
Marek Vasutea9aa242016-04-04 21:21:05 +02003766 debug_cond(DLEVEL >= 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
Marek Vasut160695d2015-08-02 19:10:58 +02003767 iocfg->dqs_in_reserve, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003768
3769 hc_initialize_rom_data();
3770
3771 /* update info for sims */
3772 reg_file_set_stage(CAL_STAGE_NIL);
3773 reg_file_set_group(0);
3774
3775 /*
3776 * Load global needed for those actions that require
3777 * some dynamic calibration support.
3778 */
3779 dyn_calib_steps = STATIC_CALIB_STEPS;
3780 /*
3781 * Load global to allow dynamic selection of delay loop settings
3782 * based on calibration mode.
3783 */
3784 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3785 skip_delay_mask = 0xff;
3786 else
3787 skip_delay_mask = 0x0;
3788
3789 pass = run_mem_calibrate();
Marek Vasut23a040c2015-07-17 01:20:21 +02003790 debug_mem_calibrate(pass);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003791 return pass;
3792}