blob: c41555f8f0fa92c9f9e2d0e3cb82bb08dada246c [file] [log] [blame]
Dinh Nguyen3da42852015-06-02 22:52:49 -05001/*
2 * Copyright Altera Corporation (C) 2012-2015
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/sdram.h>
Marek Vasut04372fb2015-07-18 02:46:56 +020010#include <errno.h>
Dinh Nguyen3da42852015-06-02 22:52:49 -050011#include "sequencer.h"
Marek Vasut9c76df52015-08-02 16:55:45 +020012
Dinh Nguyen3da42852015-06-02 22:52:49 -050013static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasut139823e2015-08-02 19:47:01 +020014 (struct socfpga_sdr_rw_load_manager *)
15 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Dinh Nguyen3da42852015-06-02 22:52:49 -050016static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
Marek Vasut139823e2015-08-02 19:47:01 +020017 (struct socfpga_sdr_rw_load_jump_manager *)
18 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050019static struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasuta1c654a2015-07-12 18:31:05 +020020 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050021static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasut139823e2015-08-02 19:47:01 +020022 (struct socfpga_sdr_scc_mgr *)
23 (SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Dinh Nguyen3da42852015-06-02 22:52:49 -050024static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasut1bc6f142015-07-12 18:54:37 +020025 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Dinh Nguyen3da42852015-06-02 22:52:49 -050026static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasut139823e2015-08-02 19:47:01 +020027 (struct socfpga_phy_mgr_cfg *)
28 (SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Dinh Nguyen3da42852015-06-02 22:52:49 -050029static struct socfpga_data_mgr *data_mgr =
Marek Vasutc4815f72015-07-12 19:03:33 +020030 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Marek Vasut6cb9f162015-07-12 20:49:39 +020031static struct socfpga_sdr_ctrl *sdr_ctrl =
32 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
33
Marek Vasutd718a262015-08-02 18:12:08 +020034const struct socfpga_sdram_rw_mgr_config *rwcfg;
Marek Vasut10c14262015-08-02 19:00:23 +020035const struct socfpga_sdram_io_config *iocfg;
Marek Vasut042ff2d2015-08-02 19:18:47 +020036const struct socfpga_sdram_misc_config *misccfg;
Marek Vasutd718a262015-08-02 18:12:08 +020037
Dinh Nguyen3da42852015-06-02 22:52:49 -050038#define DELTA_D 1
Dinh Nguyen3da42852015-06-02 22:52:49 -050039
40/*
41 * In order to reduce ROM size, most of the selectable calibration steps are
42 * decided at compile time based on the user's calibration mode selection,
43 * as captured by the STATIC_CALIB_STEPS selection below.
44 *
45 * However, to support simulation-time selection of fast simulation mode, where
46 * we skip everything except the bare minimum, we need a few of the steps to
47 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
48 * check, which is based on the rtl-supplied value, or we dynamically compute
49 * the value to use based on the dynamically-chosen calibration mode
50 */
51
52#define DLEVEL 0
53#define STATIC_IN_RTL_SIM 0
54#define STATIC_SKIP_DELAY_LOOPS 0
55
56#define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
57 STATIC_SKIP_DELAY_LOOPS)
58
59/* calibration steps requested by the rtl */
Marek Vasut5ded7322015-08-02 19:42:26 +020060u16 dyn_calib_steps;
Dinh Nguyen3da42852015-06-02 22:52:49 -050061
62/*
63 * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
64 * instead of static, we use boolean logic to select between
65 * non-skip and skip values
66 *
67 * The mask is set to include all bits when not-skipping, but is
68 * zero when skipping
69 */
70
Marek Vasut5ded7322015-08-02 19:42:26 +020071u16 skip_delay_mask; /* mask off bits when skipping/not-skipping */
Dinh Nguyen3da42852015-06-02 22:52:49 -050072
73#define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
74 ((non_skip_value) & skip_delay_mask)
75
76struct gbl_type *gbl;
77struct param_type *param;
Dinh Nguyen3da42852015-06-02 22:52:49 -050078
Marek Vasut5ded7322015-08-02 19:42:26 +020079static void set_failing_group_stage(u32 group, u32 stage,
80 u32 substage)
Dinh Nguyen3da42852015-06-02 22:52:49 -050081{
82 /*
83 * Only set the global stage if there was not been any other
84 * failing group
85 */
86 if (gbl->error_stage == CAL_STAGE_NIL) {
87 gbl->error_substage = substage;
88 gbl->error_stage = stage;
89 gbl->error_group = group;
90 }
91}
92
Marek Vasut2c0d2d92015-07-12 21:10:24 +020093static void reg_file_set_group(u16 set_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -050094{
Marek Vasut2c0d2d92015-07-12 21:10:24 +020095 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen3da42852015-06-02 22:52:49 -050096}
97
Marek Vasut2c0d2d92015-07-12 21:10:24 +020098static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -050099{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200100 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500101}
102
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200103static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500104{
Marek Vasut2c0d2d92015-07-12 21:10:24 +0200105 set_sub_stage &= 0xff;
106 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500107}
108
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200109/**
110 * phy_mgr_initialize() - Initialize PHY Manager
111 *
112 * Initialize PHY Manager.
113 */
Marek Vasut9fa9c902015-07-17 01:12:07 +0200114static void phy_mgr_initialize(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500115{
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200116 u32 ratio;
117
Dinh Nguyen3da42852015-06-02 22:52:49 -0500118 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200119 /* Calibration has control over path to memory */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500120 /*
121 * In Hard PHY this is a 2-bit control:
122 * 0: AFI Mux Select
123 * 1: DDIO Mux Select
124 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200125 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500126
127 /* USER memory clock is not stable we begin initialization */
Marek Vasut1273dd92015-07-12 21:05:08 +0200128 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500129
130 /* USER calibration status all set to zero */
Marek Vasut1273dd92015-07-12 21:05:08 +0200131 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500132
Marek Vasut1273dd92015-07-12 21:05:08 +0200133 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500134
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200135 /* Init params only if we do NOT skip calibration. */
136 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
137 return;
138
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200139 ratio = rwcfg->mem_dq_per_read_dqs /
140 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasut7c89c2d2015-07-17 01:36:32 +0200141 param->read_correct_mask_vg = (1 << ratio) - 1;
142 param->write_correct_mask_vg = (1 << ratio) - 1;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200143 param->read_correct_mask = (1 << rwcfg->mem_dq_per_read_dqs) - 1;
144 param->write_correct_mask = (1 << rwcfg->mem_dq_per_write_dqs) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500145}
146
Marek Vasut080bf642015-07-20 08:15:57 +0200147/**
148 * set_rank_and_odt_mask() - Set Rank and ODT mask
149 * @rank: Rank mask
150 * @odt_mode: ODT mode, OFF or READ_WRITE
151 *
152 * Set Rank and ODT mask (On-Die Termination).
153 */
Marek Vasutb2dfd102015-07-20 08:03:11 +0200154static void set_rank_and_odt_mask(const u32 rank, const u32 odt_mode)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500155{
Marek Vasutb2dfd102015-07-20 08:03:11 +0200156 u32 odt_mask_0 = 0;
157 u32 odt_mask_1 = 0;
158 u32 cs_and_odt_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500159
Marek Vasutb2dfd102015-07-20 08:03:11 +0200160 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
161 odt_mask_0 = 0x0;
162 odt_mask_1 = 0x0;
163 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200164 switch (rwcfg->mem_number_of_ranks) {
Marek Vasut287cdf62015-07-20 08:09:05 +0200165 case 1: /* 1 Rank */
166 /* Read: ODT = 0 ; Write: ODT = 1 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500167 odt_mask_0 = 0x0;
168 odt_mask_1 = 0x1;
Marek Vasut287cdf62015-07-20 08:09:05 +0200169 break;
170 case 2: /* 2 Ranks */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200171 if (rwcfg->mem_number_of_cs_per_dimm == 1) {
Marek Vasut080bf642015-07-20 08:15:57 +0200172 /*
173 * - Dual-Slot , Single-Rank (1 CS per DIMM)
174 * OR
175 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
176 *
177 * Since MEM_NUMBER_OF_RANKS is 2, they
178 * are both single rank with 2 CS each
179 * (special for RDIMM).
180 *
Dinh Nguyen3da42852015-06-02 22:52:49 -0500181 * Read: Turn on ODT on the opposite rank
182 * Write: Turn on ODT on all ranks
183 */
184 odt_mask_0 = 0x3 & ~(1 << rank);
185 odt_mask_1 = 0x3;
186 } else {
187 /*
Marek Vasut080bf642015-07-20 08:15:57 +0200188 * - Single-Slot , Dual-Rank (2 CS per DIMM)
189 *
190 * Read: Turn on ODT off on all ranks
191 * Write: Turn on ODT on active rank
Dinh Nguyen3da42852015-06-02 22:52:49 -0500192 */
193 odt_mask_0 = 0x0;
194 odt_mask_1 = 0x3 & (1 << rank);
195 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200196 break;
197 case 4: /* 4 Ranks */
198 /* Read:
Dinh Nguyen3da42852015-06-02 22:52:49 -0500199 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500200 * | ODT |
201 * Read From +-----------------------+
202 * Rank | 3 | 2 | 1 | 0 |
203 * ----------+-----+-----+-----+-----+
204 * 0 | 0 | 1 | 0 | 0 |
205 * 1 | 1 | 0 | 0 | 0 |
206 * 2 | 0 | 0 | 0 | 1 |
207 * 3 | 0 | 0 | 1 | 0 |
208 * ----------+-----+-----+-----+-----+
209 *
210 * Write:
211 * ----------+-----------------------+
Dinh Nguyen3da42852015-06-02 22:52:49 -0500212 * | ODT |
213 * Write To +-----------------------+
214 * Rank | 3 | 2 | 1 | 0 |
215 * ----------+-----+-----+-----+-----+
216 * 0 | 0 | 1 | 0 | 1 |
217 * 1 | 1 | 0 | 1 | 0 |
218 * 2 | 0 | 1 | 0 | 1 |
219 * 3 | 1 | 0 | 1 | 0 |
220 * ----------+-----+-----+-----+-----+
221 */
222 switch (rank) {
223 case 0:
224 odt_mask_0 = 0x4;
225 odt_mask_1 = 0x5;
226 break;
227 case 1:
228 odt_mask_0 = 0x8;
229 odt_mask_1 = 0xA;
230 break;
231 case 2:
232 odt_mask_0 = 0x1;
233 odt_mask_1 = 0x5;
234 break;
235 case 3:
236 odt_mask_0 = 0x2;
237 odt_mask_1 = 0xA;
238 break;
239 }
Marek Vasut287cdf62015-07-20 08:09:05 +0200240 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500241 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500242 }
243
Marek Vasutb2dfd102015-07-20 08:03:11 +0200244 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
245 ((0xFF & odt_mask_0) << 8) |
246 ((0xFF & odt_mask_1) << 16);
Marek Vasut1273dd92015-07-12 21:05:08 +0200247 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
248 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500249}
250
Marek Vasutc76976d2015-07-12 22:28:33 +0200251/**
252 * scc_mgr_set() - Set SCC Manager register
253 * @off: Base offset in SCC Manager space
254 * @grp: Read/Write group
255 * @val: Value to be set
256 *
257 * This function sets the SCC Manager (Scan Chain Control Manager) register.
258 */
259static void scc_mgr_set(u32 off, u32 grp, u32 val)
260{
261 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
262}
263
Marek Vasute893f4d2015-07-20 07:16:42 +0200264/**
265 * scc_mgr_initialize() - Initialize SCC Manager registers
266 *
267 * Initialize SCC Manager registers.
268 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500269static void scc_mgr_initialize(void)
270{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500271 /*
Marek Vasute893f4d2015-07-20 07:16:42 +0200272 * Clear register file for HPS. 16 (2^4) is the size of the
273 * full register file in the scc mgr:
274 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
275 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500276 */
Marek Vasutc76976d2015-07-12 22:28:33 +0200277 int i;
Marek Vasute893f4d2015-07-20 07:16:42 +0200278
Dinh Nguyen3da42852015-06-02 22:52:49 -0500279 for (i = 0; i < 16; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200280 debug_cond(DLEVEL >= 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -0500281 __func__, __LINE__, i);
Marek Vasut8e9e62c2016-04-04 17:28:16 +0200282 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500283 }
284}
285
Marek Vasut5ded7322015-08-02 19:42:26 +0200286static void scc_mgr_set_dqdqs_output_phase(u32 write_group, u32 phase)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200287{
Marek Vasutc76976d2015-07-12 22:28:33 +0200288 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200289}
290
Marek Vasut5ded7322015-08-02 19:42:26 +0200291static void scc_mgr_set_dqs_bus_in_delay(u32 read_group, u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500292{
Marek Vasutc76976d2015-07-12 22:28:33 +0200293 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500294}
295
Marek Vasut5ded7322015-08-02 19:42:26 +0200296static void scc_mgr_set_dqs_en_phase(u32 read_group, u32 phase)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500297{
Marek Vasutc76976d2015-07-12 22:28:33 +0200298 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500299}
300
Marek Vasut5ded7322015-08-02 19:42:26 +0200301static void scc_mgr_set_dqs_en_delay(u32 read_group, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200302{
Marek Vasutc76976d2015-07-12 22:28:33 +0200303 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200304}
305
Marek Vasut70ed80a2016-04-04 21:16:18 +0200306static void scc_mgr_set_dq_in_delay(u32 dq_in_group, u32 delay)
307{
308 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
309}
310
Marek Vasut5ded7322015-08-02 19:42:26 +0200311static void scc_mgr_set_dqs_io_in_delay(u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200312{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200313 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200314 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200315}
316
Marek Vasut70ed80a2016-04-04 21:16:18 +0200317static void scc_mgr_set_dm_in_delay(u32 dm, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200318{
Marek Vasut70ed80a2016-04-04 21:16:18 +0200319 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
320 rwcfg->mem_dq_per_write_dqs + 1 + dm,
321 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200322}
323
Marek Vasut5ded7322015-08-02 19:42:26 +0200324static void scc_mgr_set_dq_out1_delay(u32 dq_in_group, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200325{
Marek Vasutc76976d2015-07-12 22:28:33 +0200326 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200327}
328
Marek Vasut5ded7322015-08-02 19:42:26 +0200329static void scc_mgr_set_dqs_out1_delay(u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200330{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200331 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, rwcfg->mem_dq_per_write_dqs,
Marek Vasutc76976d2015-07-12 22:28:33 +0200332 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200333}
334
Marek Vasut5ded7322015-08-02 19:42:26 +0200335static void scc_mgr_set_dm_out1_delay(u32 dm, u32 delay)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200336{
Marek Vasutc76976d2015-07-12 22:28:33 +0200337 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200338 rwcfg->mem_dq_per_write_dqs + 1 + dm,
Marek Vasutc76976d2015-07-12 22:28:33 +0200339 delay);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200340}
341
342/* load up dqs config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200343static void scc_mgr_load_dqs(u32 dqs)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200344{
345 writel(dqs, &sdr_scc_mgr->dqs_ena);
346}
347
348/* load up dqs io config settings */
349static void scc_mgr_load_dqs_io(void)
350{
351 writel(0, &sdr_scc_mgr->dqs_io_ena);
352}
353
354/* load up dq config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200355static void scc_mgr_load_dq(u32 dq_in_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200356{
357 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
358}
359
360/* load up dm config settings */
Marek Vasut5ded7322015-08-02 19:42:26 +0200361static void scc_mgr_load_dm(u32 dm)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200362{
363 writel(dm, &sdr_scc_mgr->dm_ena);
364}
365
Marek Vasut0b69b802015-07-12 23:25:21 +0200366/**
367 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
368 * @off: Base offset in SCC Manager space
369 * @grp: Read/Write group
370 * @val: Value to be set
371 * @update: If non-zero, trigger SCC Manager update for all ranks
372 *
373 * This function sets the SCC Manager (Scan Chain Control Manager) register
374 * and optionally triggers the SCC update for all ranks.
375 */
376static void scc_mgr_set_all_ranks(const u32 off, const u32 grp, const u32 val,
377 const int update)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500378{
Marek Vasut0b69b802015-07-12 23:25:21 +0200379 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500380
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200381 for (r = 0; r < rwcfg->mem_number_of_ranks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500382 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut0b69b802015-07-12 23:25:21 +0200383 scc_mgr_set(off, grp, val);
Marek Vasut162d60e2015-07-12 23:14:33 +0200384
Marek Vasut0b69b802015-07-12 23:25:21 +0200385 if (update || (r == 0)) {
386 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasut1273dd92015-07-12 21:05:08 +0200387 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500388 }
389 }
390}
391
Marek Vasut0b69b802015-07-12 23:25:21 +0200392static void scc_mgr_set_dqs_en_phase_all_ranks(u32 read_group, u32 phase)
393{
394 /*
395 * USER although the h/w doesn't support different phases per
396 * shadow register, for simplicity our scc manager modeling
397 * keeps different phase settings per shadow reg, and it's
398 * important for us to keep them in sync to match h/w.
399 * for efficiency, the scan chain update should occur only
400 * once to sr0.
401 */
402 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_PHASE_OFFSET,
403 read_group, phase, 0);
404}
405
Marek Vasut5ded7322015-08-02 19:42:26 +0200406static void scc_mgr_set_dqdqs_output_phase_all_ranks(u32 write_group,
407 u32 phase)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500408{
Marek Vasut0b69b802015-07-12 23:25:21 +0200409 /*
410 * USER although the h/w doesn't support different phases per
411 * shadow register, for simplicity our scc manager modeling
412 * keeps different phase settings per shadow reg, and it's
413 * important for us to keep them in sync to match h/w.
414 * for efficiency, the scan chain update should occur only
415 * once to sr0.
416 */
417 scc_mgr_set_all_ranks(SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
418 write_group, phase, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500419}
420
Marek Vasut5ded7322015-08-02 19:42:26 +0200421static void scc_mgr_set_dqs_en_delay_all_ranks(u32 read_group,
422 u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500423{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500424 /*
425 * In shadow register mode, the T11 settings are stored in
426 * registers in the core, which are updated by the DQS_ENA
427 * signals. Not issuing the SCC_MGR_UPD command allows us to
428 * save lots of rank switching overhead, by calling
429 * select_shadow_regs_for_update with update_scan_chains
430 * set to 0.
431 */
Marek Vasut0b69b802015-07-12 23:25:21 +0200432 scc_mgr_set_all_ranks(SCC_MGR_DQS_EN_DELAY_OFFSET,
433 read_group, delay, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500434}
435
Marek Vasut5be355c2015-07-12 23:39:06 +0200436/**
437 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
438 * @write_group: Write group
439 * @delay: Delay value
440 *
441 * This function sets the OCT output delay in SCC manager.
442 */
443static void scc_mgr_set_oct_out1_delay(const u32 write_group, const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500444{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200445 const int ratio = rwcfg->mem_if_read_dqs_width /
446 rwcfg->mem_if_write_dqs_width;
Marek Vasut5be355c2015-07-12 23:39:06 +0200447 const int base = write_group * ratio;
448 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500449 /*
450 * Load the setting in the SCC manager
451 * Although OCT affects only write data, the OCT delay is controlled
452 * by the DQS logic block which is instantiated once per read group.
453 * For protocols where a write group consists of multiple read groups,
454 * the setting must be set multiple times.
455 */
Marek Vasut5be355c2015-07-12 23:39:06 +0200456 for (i = 0; i < ratio; i++)
457 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500458}
459
Marek Vasut37a37ca2015-07-19 01:32:55 +0200460/**
461 * scc_mgr_set_hhp_extras() - Set HHP extras.
462 *
463 * Load the fixed setting in the SCC manager HHP extras.
464 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500465static void scc_mgr_set_hhp_extras(void)
466{
467 /*
468 * Load the fixed setting in the SCC manager
Marek Vasut37a37ca2015-07-19 01:32:55 +0200469 * bits: 0:0 = 1'b1 - DQS bypass
470 * bits: 1:1 = 1'b1 - DQ bypass
471 * bits: 4:2 = 3'b001 - rfifo_mode
472 * bits: 6:5 = 2'b01 - rfifo clock_select
473 * bits: 7:7 = 1'b0 - separate gating from ungating setting
474 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen3da42852015-06-02 22:52:49 -0500475 */
Marek Vasut37a37ca2015-07-19 01:32:55 +0200476 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
477 (1 << 2) | (1 << 1) | (1 << 0);
478 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
479 SCC_MGR_HHP_GLOBALS_OFFSET |
480 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500481
Marek Vasutea9aa242016-04-04 21:21:05 +0200482 debug_cond(DLEVEL >= 1, "%s:%d Setting HHP Extras\n",
Marek Vasut37a37ca2015-07-19 01:32:55 +0200483 __func__, __LINE__);
484 writel(value, addr);
Marek Vasutea9aa242016-04-04 21:21:05 +0200485 debug_cond(DLEVEL >= 1, "%s:%d Done Setting HHP Extras\n",
Marek Vasut37a37ca2015-07-19 01:32:55 +0200486 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500487}
488
Marek Vasutf42af352015-07-20 04:41:53 +0200489/**
490 * scc_mgr_zero_all() - Zero all DQS config
491 *
492 * Zero all DQS config.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500493 */
494static void scc_mgr_zero_all(void)
495{
Marek Vasutf42af352015-07-20 04:41:53 +0200496 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500497
498 /*
499 * USER Zero all DQS config settings, across all groups and all
500 * shadow registers
501 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200502 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf42af352015-07-20 04:41:53 +0200503 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200504 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500505 /*
506 * The phases actually don't exist on a per-rank basis,
507 * but there's no harm updating them several times, so
508 * let's keep the code simple.
509 */
Marek Vasut160695d2015-08-02 19:10:58 +0200510 scc_mgr_set_dqs_bus_in_delay(i, iocfg->dqs_in_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500511 scc_mgr_set_dqs_en_phase(i, 0);
512 scc_mgr_set_dqs_en_delay(i, 0);
513 }
514
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200515 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -0500516 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasutf42af352015-07-20 04:41:53 +0200517 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200518 scc_mgr_set_oct_out1_delay(i, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500519 }
520 }
521
Marek Vasutf42af352015-07-20 04:41:53 +0200522 /* Multicast to all DQS group enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200523 writel(0xff, &sdr_scc_mgr->dqs_ena);
524 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500525}
526
Marek Vasutc5c5f532015-07-17 02:06:20 +0200527/**
528 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
529 * @write_group: Write group
530 *
531 * Set bypass mode and trigger SCC update.
532 */
533static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500534{
Marek Vasutc5c5f532015-07-17 02:06:20 +0200535 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200536 writel(0xff, &sdr_scc_mgr->dq_ena);
537 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500538
Marek Vasutc5c5f532015-07-17 02:06:20 +0200539 /* Update current DQS IO enable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200540 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500541
Marek Vasutc5c5f532015-07-17 02:06:20 +0200542 /* Update the DQS logic. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200543 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500544
Marek Vasutc5c5f532015-07-17 02:06:20 +0200545 /* Hit update. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200546 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500547}
548
Marek Vasut5e837892015-07-13 00:30:09 +0200549/**
550 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
551 * @write_group: Write group
552 *
553 * Load DQS settings for Write Group, do not trigger SCC update.
554 */
555static void scc_mgr_load_dqs_for_write_group(const u32 write_group)
Marek Vasut5ff825b2015-07-12 22:11:55 +0200556{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200557 const int ratio = rwcfg->mem_if_read_dqs_width /
558 rwcfg->mem_if_write_dqs_width;
Marek Vasut5e837892015-07-13 00:30:09 +0200559 const int base = write_group * ratio;
560 int i;
Marek Vasut5ff825b2015-07-12 22:11:55 +0200561 /*
Marek Vasut5e837892015-07-13 00:30:09 +0200562 * Load the setting in the SCC manager
Marek Vasut5ff825b2015-07-12 22:11:55 +0200563 * Although OCT affects only write data, the OCT delay is controlled
564 * by the DQS logic block which is instantiated once per read group.
565 * For protocols where a write group consists of multiple read groups,
Marek Vasut5e837892015-07-13 00:30:09 +0200566 * the setting must be set multiple times.
Marek Vasut5ff825b2015-07-12 22:11:55 +0200567 */
Marek Vasut5e837892015-07-13 00:30:09 +0200568 for (i = 0; i < ratio; i++)
569 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut5ff825b2015-07-12 22:11:55 +0200570}
571
Marek Vasutd41ea932015-07-20 08:41:04 +0200572/**
573 * scc_mgr_zero_group() - Zero all configs for a group
574 *
575 * Zero DQ, DM, DQS and OCT configs for a group.
576 */
577static void scc_mgr_zero_group(const u32 write_group, const int out_only)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500578{
Marek Vasutd41ea932015-07-20 08:41:04 +0200579 int i, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500580
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200581 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutd41ea932015-07-20 08:41:04 +0200582 r += NUM_RANKS_PER_SHADOW_REG) {
583 /* Zero all DQ config settings. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200584 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200585 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500586 if (!out_only)
Marek Vasut07aee5b2015-07-12 22:07:33 +0200587 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500588 }
589
Marek Vasutd41ea932015-07-20 08:41:04 +0200590 /* Multicast to all DQ enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200591 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500592
Marek Vasutd41ea932015-07-20 08:41:04 +0200593 /* Zero all DM config settings. */
Marek Vasut70ed80a2016-04-04 21:16:18 +0200594 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
595 if (!out_only)
596 scc_mgr_set_dm_in_delay(i, 0);
Marek Vasut07aee5b2015-07-12 22:07:33 +0200597 scc_mgr_set_dm_out1_delay(i, 0);
Marek Vasut70ed80a2016-04-04 21:16:18 +0200598 }
Dinh Nguyen3da42852015-06-02 22:52:49 -0500599
Marek Vasutd41ea932015-07-20 08:41:04 +0200600 /* Multicast to all DM enables. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200601 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500602
Marek Vasutd41ea932015-07-20 08:41:04 +0200603 /* Zero all DQS IO settings. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500604 if (!out_only)
Marek Vasut32675242015-07-17 06:07:13 +0200605 scc_mgr_set_dqs_io_in_delay(0);
Marek Vasutd41ea932015-07-20 08:41:04 +0200606
607 /* Arria V/Cyclone V don't have out2. */
Marek Vasut160695d2015-08-02 19:10:58 +0200608 scc_mgr_set_dqs_out1_delay(iocfg->dqs_out_reserve);
609 scc_mgr_set_oct_out1_delay(write_group, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500610 scc_mgr_load_dqs_for_write_group(write_group);
611
Marek Vasutd41ea932015-07-20 08:41:04 +0200612 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasut1273dd92015-07-12 21:05:08 +0200613 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500614
Marek Vasutd41ea932015-07-20 08:41:04 +0200615 /* Hit update to zero everything. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200616 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500617 }
618}
619
Dinh Nguyen3da42852015-06-02 22:52:49 -0500620/*
621 * apply and load a particular input delay for the DQ pins in a group
622 * group_bgn is the index of the first dq pin (in the write group)
623 */
Marek Vasut5ded7322015-08-02 19:42:26 +0200624static void scc_mgr_apply_group_dq_in_delay(u32 group_bgn, u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500625{
Marek Vasut5ded7322015-08-02 19:42:26 +0200626 u32 i, p;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500627
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200628 for (i = 0, p = group_bgn; i < rwcfg->mem_dq_per_read_dqs; i++, p++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200629 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500630 scc_mgr_load_dq(p);
631 }
632}
633
Marek Vasut300c2e62015-07-17 05:42:49 +0200634/**
635 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the DQ pins in a group
636 * @delay: Delay value
637 *
638 * Apply and load a particular output delay for the DQ pins in a group.
639 */
640static void scc_mgr_apply_group_dq_out1_delay(const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500641{
Marek Vasut300c2e62015-07-17 05:42:49 +0200642 int i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500643
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200644 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut300c2e62015-07-17 05:42:49 +0200645 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500646 scc_mgr_load_dq(i);
647 }
648}
649
650/* apply and load a particular output delay for the DM pins in a group */
Marek Vasut5ded7322015-08-02 19:42:26 +0200651static void scc_mgr_apply_group_dm_out1_delay(u32 delay1)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500652{
Marek Vasut5ded7322015-08-02 19:42:26 +0200653 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500654
655 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Marek Vasut07aee5b2015-07-12 22:07:33 +0200656 scc_mgr_set_dm_out1_delay(i, delay1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500657 scc_mgr_load_dm(i);
658 }
659}
660
661
662/* apply and load delay on both DQS and OCT out1 */
Marek Vasut5ded7322015-08-02 19:42:26 +0200663static void scc_mgr_apply_group_dqs_io_and_oct_out1(u32 write_group,
664 u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500665{
Marek Vasut32675242015-07-17 06:07:13 +0200666 scc_mgr_set_dqs_out1_delay(delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500667 scc_mgr_load_dqs_io();
668
669 scc_mgr_set_oct_out1_delay(write_group, delay);
670 scc_mgr_load_dqs_for_write_group(write_group);
671}
672
Marek Vasut5cb1b502015-07-17 05:33:28 +0200673/**
674 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side: DQ, DM, DQS, OCT
675 * @write_group: Write group
676 * @delay: Delay value
677 *
678 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
679 */
Marek Vasut8eccde32015-07-17 05:30:14 +0200680static void scc_mgr_apply_group_all_out_delay_add(const u32 write_group,
Marek Vasut8eccde32015-07-17 05:30:14 +0200681 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500682{
Marek Vasut8eccde32015-07-17 05:30:14 +0200683 u32 i, new_delay;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500684
Marek Vasut8eccde32015-07-17 05:30:14 +0200685 /* DQ shift */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200686 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500687 scc_mgr_load_dq(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500688
Marek Vasut8eccde32015-07-17 05:30:14 +0200689 /* DM shift */
690 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500691 scc_mgr_load_dm(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500692
Marek Vasut5cb1b502015-07-17 05:33:28 +0200693 /* DQS shift */
694 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200695 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200696 debug_cond(DLEVEL >= 1,
Marek Vasut5cb1b502015-07-17 05:33:28 +0200697 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
698 __func__, __LINE__, write_group, delay, new_delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200699 iocfg->io_out2_delay_max,
700 new_delay - iocfg->io_out2_delay_max);
701 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200702 scc_mgr_set_dqs_out1_delay(new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500703 }
704
705 scc_mgr_load_dqs_io();
706
Marek Vasut5cb1b502015-07-17 05:33:28 +0200707 /* OCT shift */
708 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Marek Vasut160695d2015-08-02 19:10:58 +0200709 if (new_delay > iocfg->io_out2_delay_max) {
Marek Vasutea9aa242016-04-04 21:21:05 +0200710 debug_cond(DLEVEL >= 1,
Marek Vasut5cb1b502015-07-17 05:33:28 +0200711 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
712 __func__, __LINE__, write_group, delay,
Marek Vasut160695d2015-08-02 19:10:58 +0200713 new_delay, iocfg->io_out2_delay_max,
714 new_delay - iocfg->io_out2_delay_max);
715 new_delay -= iocfg->io_out2_delay_max;
Marek Vasut5cb1b502015-07-17 05:33:28 +0200716 scc_mgr_set_oct_out1_delay(write_group, new_delay);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500717 }
718
719 scc_mgr_load_dqs_for_write_group(write_group);
720}
721
Marek Vasutf51a7d32015-07-19 02:18:21 +0200722/**
723 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output side to all ranks
724 * @write_group: Write group
725 * @delay: Delay value
726 *
727 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500728 */
Marek Vasutf51a7d32015-07-19 02:18:21 +0200729static void
730scc_mgr_apply_group_all_out_delay_add_all_ranks(const u32 write_group,
731 const u32 delay)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500732{
Marek Vasutf51a7d32015-07-19 02:18:21 +0200733 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500734
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200735 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasutf51a7d32015-07-19 02:18:21 +0200736 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut5cb1b502015-07-17 05:33:28 +0200737 scc_mgr_apply_group_all_out_delay_add(write_group, delay);
Marek Vasut1273dd92015-07-12 21:05:08 +0200738 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500739 }
740}
741
Marek Vasutf936f942015-07-26 11:07:19 +0200742/**
743 * set_jump_as_return() - Return instruction optimization
744 *
745 * Optimization used to recover some slots in ddr3 inst_rom could be
746 * applied to other protocols if we wanted to
747 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500748static void set_jump_as_return(void)
749{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500750 /*
Marek Vasutf936f942015-07-26 11:07:19 +0200751 * To save space, we replace return with jump to special shared
Dinh Nguyen3da42852015-06-02 22:52:49 -0500752 * RETURN instruction so we set the counter to large value so that
Marek Vasutf936f942015-07-26 11:07:19 +0200753 * we always jump.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500754 */
Marek Vasut1273dd92015-07-12 21:05:08 +0200755 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200756 writel(rwcfg->rreturn, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500757}
758
Marek Vasut3de96222015-07-26 11:46:04 +0200759/**
760 * delay_for_n_mem_clocks() - Delay for N memory clocks
761 * @clocks: Length of the delay
762 *
763 * Delay for N memory clocks.
Dinh Nguyen3da42852015-06-02 22:52:49 -0500764 */
Marek Vasut90a584b2015-07-26 11:11:28 +0200765static void delay_for_n_mem_clocks(const u32 clocks)
Dinh Nguyen3da42852015-06-02 22:52:49 -0500766{
Marek Vasut90a584b2015-07-26 11:11:28 +0200767 u32 afi_clocks;
Marek Vasut6a39be62015-07-26 11:42:53 +0200768 u16 c_loop;
769 u8 inner;
770 u8 outer;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500771
772 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
773
Marek Vasutcbcaf462015-07-26 11:34:09 +0200774 /* Scale (rounding up) to get afi clocks. */
Marek Vasut96fd4362015-08-02 19:26:55 +0200775 afi_clocks = DIV_ROUND_UP(clocks, misccfg->afi_rate_ratio);
Marek Vasutcbcaf462015-07-26 11:34:09 +0200776 if (afi_clocks) /* Temporary underflow protection */
777 afi_clocks--;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500778
779 /*
Marek Vasut90a584b2015-07-26 11:11:28 +0200780 * Note, we don't bother accounting for being off a little
781 * bit because of a few extra instructions in outer loops.
782 * Note, the loops have a test at the end, and do the test
783 * before the decrement, and so always perform the loop
Dinh Nguyen3da42852015-06-02 22:52:49 -0500784 * 1 time more than the counter value
785 */
Marek Vasut6a39be62015-07-26 11:42:53 +0200786 c_loop = afi_clocks >> 16;
787 outer = c_loop ? 0xff : (afi_clocks >> 8);
788 inner = outer ? 0xff : afi_clocks;
Dinh Nguyen3da42852015-06-02 22:52:49 -0500789
790 /*
791 * rom instructions are structured as follows:
792 *
793 * IDLE_LOOP2: jnz cntr0, TARGET_A
794 * IDLE_LOOP1: jnz cntr1, TARGET_B
795 * return
796 *
797 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
798 * TARGET_B is set to IDLE_LOOP2 as well
799 *
800 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
801 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
802 *
803 * a little confusing, but it helps save precious space in the inst_rom
804 * and sequencer rom and keeps the delays more accurate and reduces
805 * overhead
806 */
Marek Vasutcbcaf462015-07-26 11:34:09 +0200807 if (afi_clocks < 0x100) {
Marek Vasut1273dd92015-07-12 21:05:08 +0200808 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasut139823e2015-08-02 19:47:01 +0200809 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500810
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200811 writel(rwcfg->idle_loop1,
Marek Vasut139823e2015-08-02 19:47:01 +0200812 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500813
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200814 writel(rwcfg->idle_loop1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +0200815 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500816 } else {
Marek Vasut1273dd92015-07-12 21:05:08 +0200817 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasut139823e2015-08-02 19:47:01 +0200818 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500819
Marek Vasut1273dd92015-07-12 21:05:08 +0200820 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
Marek Vasut139823e2015-08-02 19:47:01 +0200821 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500822
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200823 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200824 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500825
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200826 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200827 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500828
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200829 do {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200830 writel(rwcfg->idle_loop2,
Marek Vasut139823e2015-08-02 19:47:01 +0200831 SDR_PHYGRP_RWMGRGRP_ADDRESS |
832 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Marek Vasut0c1b81b2015-07-26 11:44:54 +0200833 } while (c_loop-- != 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500834 }
835 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
836}
837
Marek Vasut944fe712015-07-13 00:44:30 +0200838/**
839 * rw_mgr_mem_init_load_regs() - Load instruction registers
840 * @cntr0: Counter 0 value
841 * @cntr1: Counter 1 value
842 * @cntr2: Counter 2 value
843 * @jump: Jump instruction value
844 *
845 * Load instruction registers.
846 */
847static void rw_mgr_mem_init_load_regs(u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
848{
Marek Vasut5ded7322015-08-02 19:42:26 +0200849 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut944fe712015-07-13 00:44:30 +0200850 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
851
852 /* Load counters */
853 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
854 &sdr_rw_load_mgr_regs->load_cntr0);
855 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
856 &sdr_rw_load_mgr_regs->load_cntr1);
857 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
858 &sdr_rw_load_mgr_regs->load_cntr2);
859
860 /* Load jump address */
861 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
862 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
863 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
864
865 /* Execute count instruction */
866 writel(jump, grpaddr);
867}
868
Marek Vasutecd23342015-07-13 00:51:05 +0200869/**
870 * rw_mgr_mem_load_user() - Load user calibration values
871 * @fin1: Final instruction 1
872 * @fin2: Final instruction 2
873 * @precharge: If 1, precharge the banks at the end
874 *
875 * Load user calibration values and optionally precharge the banks.
876 */
877static void rw_mgr_mem_load_user(const u32 fin1, const u32 fin2,
878 const int precharge)
879{
880 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
881 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
882 u32 r;
883
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200884 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasutecd23342015-07-13 00:51:05 +0200885 /* set rank */
886 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
887
888 /* precharge all banks ... */
889 if (precharge)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200890 writel(rwcfg->precharge_all, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200891
892 /*
893 * USER Use Mirror-ed commands for odd ranks if address
894 * mirrorring is on
895 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200896 if ((rwcfg->mem_address_mirroring >> r) & 0x1) {
Marek Vasutecd23342015-07-13 00:51:05 +0200897 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200898 writel(rwcfg->mrs2_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200899 delay_for_n_mem_clocks(4);
900 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200901 writel(rwcfg->mrs3_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200902 delay_for_n_mem_clocks(4);
903 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200904 writel(rwcfg->mrs1_mirr, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200905 delay_for_n_mem_clocks(4);
906 set_jump_as_return();
907 writel(fin1, grpaddr);
908 } else {
909 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200910 writel(rwcfg->mrs2, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200911 delay_for_n_mem_clocks(4);
912 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200913 writel(rwcfg->mrs3, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200914 delay_for_n_mem_clocks(4);
915 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200916 writel(rwcfg->mrs1, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200917 set_jump_as_return();
918 writel(fin2, grpaddr);
919 }
920
921 if (precharge)
922 continue;
923
924 set_jump_as_return();
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200925 writel(rwcfg->zqcl, grpaddr);
Marek Vasutecd23342015-07-13 00:51:05 +0200926
927 /* tZQinit = tDLLK = 512 ck cycles */
928 delay_for_n_mem_clocks(512);
929 }
930}
931
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200932/**
933 * rw_mgr_mem_initialize() - Initialize RW Manager
934 *
935 * Initialize RW Manager.
936 */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500937static void rw_mgr_mem_initialize(void)
938{
Dinh Nguyen3da42852015-06-02 22:52:49 -0500939 debug("%s:%d\n", __func__, __LINE__);
940
941 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasut1273dd92015-07-12 21:05:08 +0200942 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
943 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500944
945 /*
946 * Here's how you load register for a loop
947 * Counters are located @ 0x800
948 * Jump address are located @ 0xC00
949 * For both, registers 0 to 3 are selected using bits 3 and 2, like
950 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
951 * I know this ain't pretty, but Avalon bus throws away the 2 least
952 * significant bits
953 */
954
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200955 /* Start with memory RESET activated */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500956
957 /* tINIT = 200us */
958
959 /*
960 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
961 * If a and b are the number of iteration in 2 nested loops
962 * it takes the following number of cycles to complete the operation:
963 * number_of_cycles = ((2 + n) * a + 2) * b
964 * where n is the number of instruction in the inner loop
965 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
966 * b = 6A
967 */
Marek Vasut139823e2015-08-02 19:47:01 +0200968 rw_mgr_mem_init_load_regs(misccfg->tinit_cntr0_val,
969 misccfg->tinit_cntr1_val,
Marek Vasut96fd4362015-08-02 19:26:55 +0200970 misccfg->tinit_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200971 rwcfg->init_reset_0_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500972
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200973 /* Indicate that memory is stable. */
Marek Vasut1273dd92015-07-12 21:05:08 +0200974 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500975
976 /*
977 * transition the RESET to high
978 * Wait for 500us
979 */
980
981 /*
982 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
983 * If a and b are the number of iteration in 2 nested loops
984 * it takes the following number of cycles to complete the operation
985 * number_of_cycles = ((2 + n) * a + 2) * b
986 * where n is the number of instruction in the inner loop
987 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
988 * b = FF
989 */
Marek Vasut139823e2015-08-02 19:47:01 +0200990 rw_mgr_mem_init_load_regs(misccfg->treset_cntr0_val,
991 misccfg->treset_cntr1_val,
Marek Vasut96fd4362015-08-02 19:26:55 +0200992 misccfg->treset_cntr2_val,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +0200993 rwcfg->init_reset_1_cke_0);
Dinh Nguyen3da42852015-06-02 22:52:49 -0500994
Marek Vasut8e9d7d02015-07-26 10:57:06 +0200995 /* Bring up clock enable. */
Dinh Nguyen3da42852015-06-02 22:52:49 -0500996
997 /* tXRP < 250 ck cycles */
998 delay_for_n_mem_clocks(250);
999
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001000 rw_mgr_mem_load_user(rwcfg->mrs0_dll_reset_mirr, rwcfg->mrs0_dll_reset,
Marek Vasutecd23342015-07-13 00:51:05 +02001001 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001002}
1003
Marek Vasutf1f22f72015-07-26 10:59:19 +02001004/**
1005 * rw_mgr_mem_handoff() - Hand off the memory to user
1006 *
1007 * At the end of calibration we have to program the user settings in
1008 * and hand off the memory to the user.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001009 */
1010static void rw_mgr_mem_handoff(void)
1011{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001012 rw_mgr_mem_load_user(rwcfg->mrs0_user_mirr, rwcfg->mrs0_user, 1);
Marek Vasutecd23342015-07-13 00:51:05 +02001013 /*
Marek Vasutf1f22f72015-07-26 10:59:19 +02001014 * Need to wait tMOD (12CK or 15ns) time before issuing other
1015 * commands, but we will have plenty of NIOS cycles before actual
1016 * handoff so its okay.
Marek Vasutecd23342015-07-13 00:51:05 +02001017 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001018}
1019
Marek Vasut8371c2e2015-07-21 06:00:36 +02001020/**
1021 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1022 * @group: Write Group
1023 * @use_dm: Use DM
1024 *
1025 * Issue write test command. Two variants are provided, one that just tests
1026 * a write pattern and another that tests datamask functionality.
Marek Vasutad64769c2015-07-21 05:43:37 +02001027 */
Marek Vasut8371c2e2015-07-21 06:00:36 +02001028static void rw_mgr_mem_calibrate_write_test_issue(u32 group,
1029 u32 test_dm)
Marek Vasutad64769c2015-07-21 05:43:37 +02001030{
Marek Vasut8371c2e2015-07-21 06:00:36 +02001031 const u32 quick_write_mode =
1032 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001033 misccfg->enable_super_quick_calibration;
Marek Vasut8371c2e2015-07-21 06:00:36 +02001034 u32 mcc_instruction;
1035 u32 rw_wl_nop_cycles;
Marek Vasutad64769c2015-07-21 05:43:37 +02001036
1037 /*
1038 * Set counter and jump addresses for the right
1039 * number of NOP cycles.
1040 * The number of supported NOP cycles can range from -1 to infinity
1041 * Three different cases are handled:
1042 *
1043 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1044 * mechanism will be used to insert the right number of NOPs
1045 *
1046 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1047 * issuing the write command will jump straight to the
1048 * micro-instruction that turns on DQS (for DDRx), or outputs write
1049 * data (for RLD), skipping
1050 * the NOP micro-instruction all together
1051 *
1052 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1053 * turned on in the same micro-instruction that issues the write
1054 * command. Then we need
1055 * to directly jump to the micro-instruction that sends out the data
1056 *
1057 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1058 * (2 and 3). One jump-counter (0) is used to perform multiple
1059 * write-read operations.
1060 * one counter left to issue this command in "multiple-group" mode
1061 */
1062
1063 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
1064
1065 if (rw_wl_nop_cycles == -1) {
1066 /*
1067 * CNTR 2 - We want to execute the special write operation that
1068 * turns on DQS right away and then skip directly to the
1069 * instruction that sends out the data. We set the counter to a
1070 * large number so that the jump is always taken.
1071 */
1072 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1073
1074 /* CNTR 3 - Not used */
1075 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001076 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0_wl_1;
1077 writel(rwcfg->lfsr_wr_rd_dm_bank_0_data,
Marek Vasutad64769c2015-07-21 05:43:37 +02001078 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001079 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasutad64769c2015-07-21 05:43:37 +02001080 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1081 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001082 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0_wl_1;
1083 writel(rwcfg->lfsr_wr_rd_bank_0_data,
Marek Vasut139823e2015-08-02 19:47:01 +02001084 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001085 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001086 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001087 }
1088 } else if (rw_wl_nop_cycles == 0) {
1089 /*
1090 * CNTR 2 - We want to skip the NOP operation and go straight
1091 * to the DQS enable instruction. We set the counter to a large
1092 * number so that the jump is always taken.
1093 */
1094 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1095
1096 /* CNTR 3 - Not used */
1097 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001098 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1099 writel(rwcfg->lfsr_wr_rd_dm_bank_0_dqs,
Marek Vasutad64769c2015-07-21 05:43:37 +02001100 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1101 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001102 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1103 writel(rwcfg->lfsr_wr_rd_bank_0_dqs,
Marek Vasut139823e2015-08-02 19:47:01 +02001104 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasutad64769c2015-07-21 05:43:37 +02001105 }
1106 } else {
1107 /*
1108 * CNTR 2 - In this case we want to execute the next instruction
1109 * and NOT take the jump. So we set the counter to 0. The jump
1110 * address doesn't count.
1111 */
1112 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1113 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1114
1115 /*
1116 * CNTR 3 - Set the nop counter to the number of cycles we
1117 * need to loop for, minus 1.
1118 */
1119 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1120 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001121 mcc_instruction = rwcfg->lfsr_wr_rd_dm_bank_0;
1122 writel(rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001123 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001124 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001125 mcc_instruction = rwcfg->lfsr_wr_rd_bank_0;
1126 writel(rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasut139823e2015-08-02 19:47:01 +02001127 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasutad64769c2015-07-21 05:43:37 +02001128 }
1129 }
1130
1131 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1132 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1133
1134 if (quick_write_mode)
1135 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1136 else
1137 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1138
1139 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1140
1141 /*
1142 * CNTR 1 - This is used to ensure enough time elapses
1143 * for read data to come back.
1144 */
1145 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1146
1147 if (test_dm) {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001148 writel(rwcfg->lfsr_wr_rd_dm_bank_0_wait,
Marek Vasut139823e2015-08-02 19:47:01 +02001149 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasutad64769c2015-07-21 05:43:37 +02001150 } else {
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001151 writel(rwcfg->lfsr_wr_rd_bank_0_wait,
Marek Vasut139823e2015-08-02 19:47:01 +02001152 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasutad64769c2015-07-21 05:43:37 +02001153 }
1154
Marek Vasut8371c2e2015-07-21 06:00:36 +02001155 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1156 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1157 (group << 2));
Marek Vasutad64769c2015-07-21 05:43:37 +02001158}
1159
Marek Vasut4a82854b2015-07-21 05:57:11 +02001160/**
1161 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple pass
1162 * @rank_bgn: Rank number
1163 * @write_group: Write Group
1164 * @use_dm: Use DM
1165 * @all_correct: All bits must be correct in the mask
1166 * @bit_chk: Resulting bit mask after the test
1167 * @all_ranks: Test all ranks
1168 *
1169 * Test writes, can check for a single bit pass or multiple bit pass.
1170 */
Marek Vasutb9452ea2015-07-21 05:54:39 +02001171static int
1172rw_mgr_mem_calibrate_write_test(const u32 rank_bgn, const u32 write_group,
1173 const u32 use_dm, const u32 all_correct,
1174 u32 *bit_chk, const u32 all_ranks)
Marek Vasutad64769c2015-07-21 05:43:37 +02001175{
Marek Vasutb9452ea2015-07-21 05:54:39 +02001176 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001177 rwcfg->mem_number_of_ranks :
Marek Vasutb9452ea2015-07-21 05:54:39 +02001178 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001179 const u32 shift_ratio = rwcfg->mem_dq_per_write_dqs /
1180 rwcfg->mem_virtual_groups_per_write_dqs;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001181 const u32 correct_mask_vg = param->write_correct_mask_vg;
1182
1183 u32 tmp_bit_chk, base_rw_mgr;
1184 int vg, r;
Marek Vasutad64769c2015-07-21 05:43:37 +02001185
1186 *bit_chk = param->write_correct_mask;
Marek Vasutad64769c2015-07-21 05:43:37 +02001187
1188 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutb9452ea2015-07-21 05:54:39 +02001189 /* Set rank */
Marek Vasutad64769c2015-07-21 05:43:37 +02001190 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1191
1192 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001193 for (vg = rwcfg->mem_virtual_groups_per_write_dqs - 1;
Marek Vasutb9452ea2015-07-21 05:54:39 +02001194 vg >= 0; vg--) {
1195 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutad64769c2015-07-21 05:43:37 +02001196 writel(0, &phy_mgr_cmd->fifo_reset);
1197
Marek Vasutb9452ea2015-07-21 05:54:39 +02001198 rw_mgr_mem_calibrate_write_test_issue(
1199 write_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001200 rwcfg->mem_virtual_groups_per_write_dqs + vg,
Marek Vasutad64769c2015-07-21 05:43:37 +02001201 use_dm);
1202
Marek Vasutb9452ea2015-07-21 05:54:39 +02001203 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1204 tmp_bit_chk <<= shift_ratio;
1205 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasutad64769c2015-07-21 05:43:37 +02001206 }
Marek Vasutb9452ea2015-07-21 05:54:39 +02001207
Marek Vasutad64769c2015-07-21 05:43:37 +02001208 *bit_chk &= tmp_bit_chk;
1209 }
1210
Marek Vasutb9452ea2015-07-21 05:54:39 +02001211 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutad64769c2015-07-21 05:43:37 +02001212 if (all_correct) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001213 debug_cond(DLEVEL >= 2,
Marek Vasutb9452ea2015-07-21 05:54:39 +02001214 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1215 write_group, use_dm, *bit_chk,
1216 param->write_correct_mask,
1217 *bit_chk == param->write_correct_mask);
Marek Vasutad64769c2015-07-21 05:43:37 +02001218 return *bit_chk == param->write_correct_mask;
1219 } else {
Marek Vasutea9aa242016-04-04 21:21:05 +02001220 debug_cond(DLEVEL >= 2,
Marek Vasutb9452ea2015-07-21 05:54:39 +02001221 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1222 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasutad64769c2015-07-21 05:43:37 +02001223 return *bit_chk != 0x00;
1224 }
1225}
1226
Marek Vasutd844c7d2015-07-18 03:55:07 +02001227/**
1228 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1229 * @rank_bgn: Rank number
1230 * @group: Read/Write Group
1231 * @all_ranks: Test all ranks
1232 *
1233 * Performs a guaranteed read on the patterns we are going to use during a
1234 * read test to ensure memory works.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001235 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02001236static int
1237rw_mgr_mem_calibrate_read_test_patterns(const u32 rank_bgn, const u32 group,
1238 const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001239{
Marek Vasutd844c7d2015-07-18 03:55:07 +02001240 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1241 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1242 const u32 addr_offset =
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001243 (group * rwcfg->mem_virtual_groups_per_read_dqs) << 2;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001244 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001245 rwcfg->mem_number_of_ranks :
Marek Vasutd844c7d2015-07-18 03:55:07 +02001246 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001247 const u32 shift_ratio = rwcfg->mem_dq_per_read_dqs /
1248 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001249 const u32 correct_mask_vg = param->read_correct_mask_vg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001250
Marek Vasutd844c7d2015-07-18 03:55:07 +02001251 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1252 int vg, r;
1253 int ret = 0;
1254
1255 bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001256
1257 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutd844c7d2015-07-18 03:55:07 +02001258 /* Set rank */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001259 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1260
1261 /* Load up a constant bursts of read commands */
Marek Vasut1273dd92015-07-12 21:05:08 +02001262 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001263 writel(rwcfg->guaranteed_read,
Marek Vasut139823e2015-08-02 19:47:01 +02001264 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001265
Marek Vasut1273dd92015-07-12 21:05:08 +02001266 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001267 writel(rwcfg->guaranteed_read_cont,
Marek Vasut139823e2015-08-02 19:47:01 +02001268 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001269
1270 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001271 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1;
Marek Vasutd844c7d2015-07-18 03:55:07 +02001272 vg >= 0; vg--) {
1273 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001274 writel(0, &phy_mgr_cmd->fifo_reset);
1275 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1276 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001277 writel(rwcfg->guaranteed_read,
Marek Vasutd844c7d2015-07-18 03:55:07 +02001278 addr + addr_offset + (vg << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001279
Marek Vasut1273dd92015-07-12 21:05:08 +02001280 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001281 tmp_bit_chk <<= shift_ratio;
1282 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001283 }
Marek Vasutd844c7d2015-07-18 03:55:07 +02001284
1285 bit_chk &= tmp_bit_chk;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001286 }
1287
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001288 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001289
1290 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
Marek Vasutd844c7d2015-07-18 03:55:07 +02001291
1292 if (bit_chk != param->read_correct_mask)
1293 ret = -EIO;
1294
Marek Vasutea9aa242016-04-04 21:21:05 +02001295 debug_cond(DLEVEL >= 1,
Marek Vasutd844c7d2015-07-18 03:55:07 +02001296 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1297 __func__, __LINE__, group, bit_chk,
1298 param->read_correct_mask, ret);
1299
1300 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001301}
1302
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001303/**
1304 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read test
1305 * @rank_bgn: Rank number
1306 * @all_ranks: Test all ranks
1307 *
1308 * Load up the patterns we are going to use during a read test.
1309 */
1310static void rw_mgr_mem_calibrate_read_load_patterns(const u32 rank_bgn,
1311 const int all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001312{
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001313 const u32 rank_end = all_ranks ?
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001314 rwcfg->mem_number_of_ranks :
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001315 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1316 u32 r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001317
1318 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutb6cb7f92015-07-18 03:34:22 +02001319
Dinh Nguyen3da42852015-06-02 22:52:49 -05001320 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001321 /* set rank */
1322 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1323
1324 /* Load up a constant bursts */
Marek Vasut1273dd92015-07-12 21:05:08 +02001325 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001326
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001327 writel(rwcfg->guaranteed_write_wait0,
Marek Vasut139823e2015-08-02 19:47:01 +02001328 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001329
Marek Vasut1273dd92015-07-12 21:05:08 +02001330 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001331
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001332 writel(rwcfg->guaranteed_write_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02001333 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001334
Marek Vasut1273dd92015-07-12 21:05:08 +02001335 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001336
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001337 writel(rwcfg->guaranteed_write_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02001338 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001339
Marek Vasut1273dd92015-07-12 21:05:08 +02001340 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001341
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001342 writel(rwcfg->guaranteed_write_wait3,
Marek Vasut139823e2015-08-02 19:47:01 +02001343 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001344
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001345 writel(rwcfg->guaranteed_write, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02001346 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001347 }
1348
1349 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1350}
1351
Marek Vasut783fcf52015-07-20 03:26:05 +02001352/**
1353 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1354 * @rank_bgn: Rank number
1355 * @group: Read/Write group
1356 * @num_tries: Number of retries of the test
1357 * @all_correct: All bits must be correct in the mask
1358 * @bit_chk: Resulting bit mask after the test
1359 * @all_groups: Test all R/W groups
1360 * @all_ranks: Test all ranks
1361 *
1362 * Try a read and see if it returns correct data back. Test has dummy reads
1363 * inserted into the mix used to align DQS enable. Test has more thorough
1364 * checks than the regular read test.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001365 */
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001366static int
1367rw_mgr_mem_calibrate_read_test(const u32 rank_bgn, const u32 group,
1368 const u32 num_tries, const u32 all_correct,
1369 u32 *bit_chk,
1370 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001371{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001372 const u32 rank_end = all_ranks ? rwcfg->mem_number_of_ranks :
Dinh Nguyen3da42852015-06-02 22:52:49 -05001373 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001374 const u32 quick_read_mode =
1375 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
Marek Vasut96fd4362015-08-02 19:26:55 +02001376 misccfg->enable_super_quick_calibration);
Marek Vasut3cb8bf32015-07-19 07:48:58 +02001377 u32 correct_mask_vg = param->read_correct_mask_vg;
1378 u32 tmp_bit_chk;
1379 u32 base_rw_mgr;
1380 u32 addr;
1381
1382 int r, vg, ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001383
1384 *bit_chk = param->read_correct_mask;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001385
1386 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001387 /* set rank */
1388 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1389
Marek Vasut1273dd92015-07-12 21:05:08 +02001390 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001391
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001392 writel(rwcfg->read_b2b_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02001393 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001394
Marek Vasut1273dd92015-07-12 21:05:08 +02001395 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001396 writel(rwcfg->read_b2b_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02001397 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001398
Dinh Nguyen3da42852015-06-02 22:52:49 -05001399 if (quick_read_mode)
Marek Vasut1273dd92015-07-12 21:05:08 +02001400 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001401 /* need at least two (1+1) reads to capture failures */
1402 else if (all_groups)
Marek Vasut1273dd92015-07-12 21:05:08 +02001403 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001404 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001405 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001406
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001407 writel(rwcfg->read_b2b,
Marek Vasut139823e2015-08-02 19:47:01 +02001408 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001409 if (all_groups)
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001410 writel(rwcfg->mem_if_read_dqs_width *
1411 rwcfg->mem_virtual_groups_per_read_dqs - 1,
Marek Vasut1273dd92015-07-12 21:05:08 +02001412 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001413 else
Marek Vasut1273dd92015-07-12 21:05:08 +02001414 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001415
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001416 writel(rwcfg->read_b2b,
Marek Vasut139823e2015-08-02 19:47:01 +02001417 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001418
1419 tmp_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001420 for (vg = rwcfg->mem_virtual_groups_per_read_dqs - 1; vg >= 0;
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001421 vg--) {
Marek Vasutba522c72015-07-19 07:57:28 +02001422 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02001423 writel(0, &phy_mgr_cmd->fifo_reset);
1424 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1425 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001426
Marek Vasutba522c72015-07-19 07:57:28 +02001427 if (all_groups) {
1428 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1429 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1430 } else {
1431 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1432 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1433 }
Marek Vasutc4815f72015-07-12 19:03:33 +02001434
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001435 writel(rwcfg->read_b2b, addr +
Marek Vasut139823e2015-08-02 19:47:01 +02001436 ((group *
1437 rwcfg->mem_virtual_groups_per_read_dqs +
1438 vg) << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001439
Marek Vasut1273dd92015-07-12 21:05:08 +02001440 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001441 tmp_bit_chk <<= rwcfg->mem_dq_per_read_dqs /
1442 rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasutba522c72015-07-19 07:57:28 +02001443 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001444 }
Marek Vasut7ce23bb2015-07-19 07:51:17 +02001445
Dinh Nguyen3da42852015-06-02 22:52:49 -05001446 *bit_chk &= tmp_bit_chk;
1447 }
1448
Marek Vasutc4815f72015-07-12 19:03:33 +02001449 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001450 writel(rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05001451
Marek Vasut3853d652015-07-19 07:44:21 +02001452 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1453
Dinh Nguyen3da42852015-06-02 22:52:49 -05001454 if (all_correct) {
Marek Vasut3853d652015-07-19 07:44:21 +02001455 ret = (*bit_chk == param->read_correct_mask);
Marek Vasutea9aa242016-04-04 21:21:05 +02001456 debug_cond(DLEVEL >= 2,
Marek Vasut3853d652015-07-19 07:44:21 +02001457 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1458 __func__, __LINE__, group, all_groups, *bit_chk,
1459 param->read_correct_mask, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001460 } else {
Marek Vasut3853d652015-07-19 07:44:21 +02001461 ret = (*bit_chk != 0x00);
Marek Vasutea9aa242016-04-04 21:21:05 +02001462 debug_cond(DLEVEL >= 2,
Marek Vasut3853d652015-07-19 07:44:21 +02001463 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1464 __func__, __LINE__, group, all_groups, *bit_chk,
1465 0, ret);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001466 }
Marek Vasut3853d652015-07-19 07:44:21 +02001467
1468 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001469}
1470
Marek Vasut96df6032015-07-19 07:35:36 +02001471/**
1472 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1473 * @grp: Read/Write group
1474 * @num_tries: Number of retries of the test
1475 * @all_correct: All bits must be correct in the mask
1476 * @all_groups: Test all R/W groups
1477 *
1478 * Perform a READ test across all memory ranks.
1479 */
1480static int
1481rw_mgr_mem_calibrate_read_test_all_ranks(const u32 grp, const u32 num_tries,
1482 const u32 all_correct,
1483 const u32 all_groups)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001484{
Marek Vasut96df6032015-07-19 07:35:36 +02001485 u32 bit_chk;
1486 return rw_mgr_mem_calibrate_read_test(0, grp, num_tries, all_correct,
1487 &bit_chk, all_groups, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001488}
1489
Marek Vasut60bb8a82015-07-19 06:25:27 +02001490/**
1491 * rw_mgr_incr_vfifo() - Increase VFIFO value
1492 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001493 *
1494 * Increase VFIFO value.
1495 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001496static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001497{
Marek Vasut1273dd92015-07-12 21:05:08 +02001498 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001499}
1500
Marek Vasut60bb8a82015-07-19 06:25:27 +02001501/**
1502 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1503 * @grp: Read/Write group
Marek Vasut60bb8a82015-07-19 06:25:27 +02001504 *
1505 * Decrease VFIFO value.
1506 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001507static void rw_mgr_decr_vfifo(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001508{
Marek Vasut60bb8a82015-07-19 06:25:27 +02001509 u32 i;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001510
Marek Vasut96fd4362015-08-02 19:26:55 +02001511 for (i = 0; i < misccfg->read_valid_fifo_size - 1; i++)
Marek Vasut8c887b62015-07-19 06:37:51 +02001512 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001513}
1514
Marek Vasutd145ca92015-07-19 06:45:43 +02001515/**
1516 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1517 * @grp: Read/Write group
1518 *
1519 * Push VFIFO until a failing read happens.
1520 */
1521static int find_vfifo_failing_read(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001522{
Marek Vasut96df6032015-07-19 07:35:36 +02001523 u32 v, ret, fail_cnt = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001524
Marek Vasut96fd4362015-08-02 19:26:55 +02001525 for (v = 0; v < misccfg->read_valid_fifo_size; v++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001526 debug_cond(DLEVEL >= 2, "%s:%d: vfifo %u\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001527 __func__, __LINE__, v);
Marek Vasutd145ca92015-07-19 06:45:43 +02001528 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001529 PASS_ONE_BIT, 0);
Marek Vasutd145ca92015-07-19 06:45:43 +02001530 if (!ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001531 fail_cnt++;
1532
1533 if (fail_cnt == 2)
Marek Vasutd145ca92015-07-19 06:45:43 +02001534 return v;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001535 }
1536
Marek Vasutd145ca92015-07-19 06:45:43 +02001537 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001538 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001539 }
1540
Marek Vasutd145ca92015-07-19 06:45:43 +02001541 /* No failing read found! Something must have gone wrong. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001542 debug_cond(DLEVEL >= 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
Marek Vasutd145ca92015-07-19 06:45:43 +02001543 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001544}
1545
Marek Vasut192d6f92015-07-19 05:26:49 +02001546/**
Marek Vasut52e8f212015-07-19 07:27:06 +02001547 * sdr_find_phase_delay() - Find DQS enable phase or delay
1548 * @working: If 1, look for working phase/delay, if 0, look for non-working
1549 * @delay: If 1, look for delay, if 0, look for phase
1550 * @grp: Read/Write group
1551 * @work: Working window position
1552 * @work_inc: Working window increment
1553 * @pd: DQS Phase/Delay Iterator
1554 *
1555 * Find working or non-working DQS enable phase setting.
1556 */
1557static int sdr_find_phase_delay(int working, int delay, const u32 grp,
1558 u32 *work, const u32 work_inc, u32 *pd)
1559{
Marek Vasut139823e2015-08-02 19:47:01 +02001560 const u32 max = delay ? iocfg->dqs_en_delay_max :
1561 iocfg->dqs_en_phase_max;
Marek Vasut96df6032015-07-19 07:35:36 +02001562 u32 ret;
Marek Vasut52e8f212015-07-19 07:27:06 +02001563
1564 for (; *pd <= max; (*pd)++) {
1565 if (delay)
1566 scc_mgr_set_dqs_en_delay_all_ranks(grp, *pd);
1567 else
1568 scc_mgr_set_dqs_en_phase_all_ranks(grp, *pd);
1569
1570 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001571 PASS_ONE_BIT, 0);
Marek Vasut52e8f212015-07-19 07:27:06 +02001572 if (!working)
1573 ret = !ret;
1574
1575 if (ret)
1576 return 0;
1577
1578 if (work)
1579 *work += work_inc;
1580 }
1581
1582 return -EINVAL;
1583}
1584/**
Marek Vasut192d6f92015-07-19 05:26:49 +02001585 * sdr_find_phase() - Find DQS enable phase
1586 * @working: If 1, look for working phase, if 0, look for non-working phase
1587 * @grp: Read/Write group
Marek Vasut192d6f92015-07-19 05:26:49 +02001588 * @work: Working window position
1589 * @i: Iterator
1590 * @p: DQS Phase Iterator
Marek Vasut192d6f92015-07-19 05:26:49 +02001591 *
1592 * Find working or non-working DQS enable phase setting.
1593 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001594static int sdr_find_phase(int working, const u32 grp, u32 *work,
Marek Vasut86a39dc2015-07-19 05:35:40 +02001595 u32 *i, u32 *p)
Marek Vasut192d6f92015-07-19 05:26:49 +02001596{
Marek Vasut96fd4362015-08-02 19:26:55 +02001597 const u32 end = misccfg->read_valid_fifo_size + (working ? 0 : 1);
Marek Vasut52e8f212015-07-19 07:27:06 +02001598 int ret;
Marek Vasut192d6f92015-07-19 05:26:49 +02001599
1600 for (; *i < end; (*i)++) {
1601 if (working)
1602 *p = 0;
1603
Marek Vasut52e8f212015-07-19 07:27:06 +02001604 ret = sdr_find_phase_delay(working, 0, grp, work,
Marek Vasut160695d2015-08-02 19:10:58 +02001605 iocfg->delay_per_opa_tap, p);
Marek Vasut52e8f212015-07-19 07:27:06 +02001606 if (!ret)
1607 return 0;
Marek Vasut192d6f92015-07-19 05:26:49 +02001608
Marek Vasut160695d2015-08-02 19:10:58 +02001609 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001610 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001611 rw_mgr_incr_vfifo(grp);
Marek Vasut192d6f92015-07-19 05:26:49 +02001612 if (!working)
1613 *p = 0;
1614 }
1615 }
1616
1617 return -EINVAL;
1618}
1619
Marek Vasut4c5e5842015-07-19 06:04:00 +02001620/**
1621 * sdr_working_phase() - Find working DQS enable phase
1622 * @grp: Read/Write group
1623 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001624 * @d: dtaps output value
1625 * @p: DQS Phase Iterator
1626 * @i: Iterator
1627 *
1628 * Find working DQS enable phase setting.
1629 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001630static int sdr_working_phase(const u32 grp, u32 *work_bgn, u32 *d,
Marek Vasut4c5e5842015-07-19 06:04:00 +02001631 u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001632{
Marek Vasut160695d2015-08-02 19:10:58 +02001633 const u32 dtaps_per_ptap = iocfg->delay_per_opa_tap /
1634 iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasut192d6f92015-07-19 05:26:49 +02001635 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001636
Marek Vasut192d6f92015-07-19 05:26:49 +02001637 *work_bgn = 0;
1638
1639 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1640 *i = 0;
Marek Vasut521fe392015-07-19 04:34:12 +02001641 scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
Marek Vasut8c887b62015-07-19 06:37:51 +02001642 ret = sdr_find_phase(1, grp, work_bgn, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001643 if (!ret)
1644 return 0;
Marek Vasut160695d2015-08-02 19:10:58 +02001645 *work_bgn += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001646 }
1647
Marek Vasut38ed6922015-07-19 05:01:12 +02001648 /* Cannot find working solution */
Marek Vasutea9aa242016-04-04 21:21:05 +02001649 debug_cond(DLEVEL >= 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
Marek Vasut192d6f92015-07-19 05:26:49 +02001650 __func__, __LINE__);
1651 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001652}
1653
Marek Vasut4c5e5842015-07-19 06:04:00 +02001654/**
1655 * sdr_backup_phase() - Find DQS enable backup phase
1656 * @grp: Read/Write group
1657 * @work_bgn: Working window start position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001658 * @p: DQS Phase Iterator
1659 *
1660 * Find DQS enable backup phase setting.
1661 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001662static void sdr_backup_phase(const u32 grp, u32 *work_bgn, u32 *p)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001663{
Marek Vasut96df6032015-07-19 07:35:36 +02001664 u32 tmp_delay, d;
Marek Vasut4c5e5842015-07-19 06:04:00 +02001665 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001666
1667 /* Special case code for backing up a phase */
1668 if (*p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001669 *p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001670 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001671 } else {
1672 (*p)--;
1673 }
Marek Vasut160695d2015-08-02 19:10:58 +02001674 tmp_delay = *work_bgn - iocfg->delay_per_opa_tap;
Marek Vasut521fe392015-07-19 04:34:12 +02001675 scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001676
Marek Vasut139823e2015-08-02 19:47:01 +02001677 for (d = 0; d <= iocfg->dqs_en_delay_max && tmp_delay < *work_bgn;
1678 d++) {
Marek Vasut49891df62015-07-19 05:48:30 +02001679 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001680
Marek Vasut4c5e5842015-07-19 06:04:00 +02001681 ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Marek Vasut96df6032015-07-19 07:35:36 +02001682 PASS_ONE_BIT, 0);
Marek Vasut4c5e5842015-07-19 06:04:00 +02001683 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001684 *work_bgn = tmp_delay;
1685 break;
1686 }
Marek Vasut49891df62015-07-19 05:48:30 +02001687
Marek Vasut160695d2015-08-02 19:10:58 +02001688 tmp_delay += iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001689 }
1690
Marek Vasut4c5e5842015-07-19 06:04:00 +02001691 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001692 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001693 if (*p > iocfg->dqs_en_phase_max) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05001694 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001695 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001696 }
1697
Marek Vasut521fe392015-07-19 04:34:12 +02001698 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001699}
1700
Marek Vasut4c5e5842015-07-19 06:04:00 +02001701/**
1702 * sdr_nonworking_phase() - Find non-working DQS enable phase
1703 * @grp: Read/Write group
1704 * @work_end: Working window end position
Marek Vasut4c5e5842015-07-19 06:04:00 +02001705 * @p: DQS Phase Iterator
1706 * @i: Iterator
1707 *
1708 * Find non-working DQS enable phase setting.
1709 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001710static int sdr_nonworking_phase(const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001711{
Marek Vasut192d6f92015-07-19 05:26:49 +02001712 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001713
1714 (*p)++;
Marek Vasut160695d2015-08-02 19:10:58 +02001715 *work_end += iocfg->delay_per_opa_tap;
1716 if (*p > iocfg->dqs_en_phase_max) {
Marek Vasut192d6f92015-07-19 05:26:49 +02001717 /* Fiddle with FIFO. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001718 *p = 0;
Marek Vasut8c887b62015-07-19 06:37:51 +02001719 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001720 }
1721
Marek Vasut8c887b62015-07-19 06:37:51 +02001722 ret = sdr_find_phase(0, grp, work_end, i, p);
Marek Vasut192d6f92015-07-19 05:26:49 +02001723 if (ret) {
1724 /* Cannot see edge of failing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001725 debug_cond(DLEVEL >= 2, "%s:%d: end: failed\n",
Marek Vasut192d6f92015-07-19 05:26:49 +02001726 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001727 }
1728
Marek Vasut192d6f92015-07-19 05:26:49 +02001729 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001730}
1731
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001732/**
1733 * sdr_find_window_center() - Find center of the working DQS window.
1734 * @grp: Read/Write group
1735 * @work_bgn: First working settings
1736 * @work_end: Last working settings
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001737 *
1738 * Find center of the working DQS enable window.
1739 */
1740static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
Marek Vasut8c887b62015-07-19 06:37:51 +02001741 const u32 work_end)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001742{
Marek Vasut96df6032015-07-19 07:35:36 +02001743 u32 work_mid;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001744 int tmp_delay = 0;
Marek Vasut28fd2422015-07-19 02:56:59 +02001745 int i, p, d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001746
Marek Vasut28fd2422015-07-19 02:56:59 +02001747 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001748
Marek Vasutea9aa242016-04-04 21:21:05 +02001749 debug_cond(DLEVEL >= 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasut28fd2422015-07-19 02:56:59 +02001750 work_bgn, work_end, work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001751 /* Get the middle delay to be less than a VFIFO delay */
Marek Vasut160695d2015-08-02 19:10:58 +02001752 tmp_delay = (iocfg->dqs_en_phase_max + 1) * iocfg->delay_per_opa_tap;
Marek Vasut28fd2422015-07-19 02:56:59 +02001753
Marek Vasutea9aa242016-04-04 21:21:05 +02001754 debug_cond(DLEVEL >= 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001755 work_mid %= tmp_delay;
Marek Vasutea9aa242016-04-04 21:21:05 +02001756 debug_cond(DLEVEL >= 2, "new work_mid %d\n", work_mid);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001757
Marek Vasut160695d2015-08-02 19:10:58 +02001758 tmp_delay = rounddown(work_mid, iocfg->delay_per_opa_tap);
1759 if (tmp_delay > iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap)
1760 tmp_delay = iocfg->dqs_en_phase_max * iocfg->delay_per_opa_tap;
1761 p = tmp_delay / iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001762
Marek Vasutea9aa242016-04-04 21:21:05 +02001763 debug_cond(DLEVEL >= 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001764
Marek Vasut139823e2015-08-02 19:47:01 +02001765 d = DIV_ROUND_UP(work_mid - tmp_delay,
1766 iocfg->delay_per_dqs_en_dchain_tap);
Marek Vasut160695d2015-08-02 19:10:58 +02001767 if (d > iocfg->dqs_en_delay_max)
1768 d = iocfg->dqs_en_delay_max;
1769 tmp_delay += d * iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001770
Marek Vasutea9aa242016-04-04 21:21:05 +02001771 debug_cond(DLEVEL >= 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
Marek Vasut28fd2422015-07-19 02:56:59 +02001772
Marek Vasutcbb0b7e2015-07-19 04:04:33 +02001773 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
Marek Vasut28fd2422015-07-19 02:56:59 +02001774 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001775
1776 /*
1777 * push vfifo until we can successfully calibrate. We can do this
1778 * because the largest possible margin in 1 VFIFO cycle.
1779 */
Marek Vasut96fd4362015-08-02 19:26:55 +02001780 for (i = 0; i < misccfg->read_valid_fifo_size; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001781 debug_cond(DLEVEL >= 2, "find_dqs_en_phase: center\n");
Marek Vasut28fd2422015-07-19 02:56:59 +02001782 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05001783 PASS_ONE_BIT,
Marek Vasut96df6032015-07-19 07:35:36 +02001784 0)) {
Marek Vasutea9aa242016-04-04 21:21:05 +02001785 debug_cond(DLEVEL >= 2,
Marek Vasut8c887b62015-07-19 06:37:51 +02001786 "%s:%d center: found: ptap=%u dtap=%u\n",
1787 __func__, __LINE__, p, d);
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001788 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001789 }
1790
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001791 /* Fiddle with FIFO. */
Marek Vasut8c887b62015-07-19 06:37:51 +02001792 rw_mgr_incr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001793 }
1794
Marek Vasutea9aa242016-04-04 21:21:05 +02001795 debug_cond(DLEVEL >= 2, "%s:%d center: failed.\n",
Marek Vasut0a13a0f2015-07-19 04:14:32 +02001796 __func__, __LINE__);
1797 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001798}
1799
Marek Vasut33756892015-07-20 09:11:09 +02001800/**
1801 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to use
1802 * @grp: Read/Write Group
1803 *
1804 * Find a good DQS enable to use.
1805 */
Marek Vasut914546e2015-07-20 09:20:42 +02001806static int rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(const u32 grp)
Dinh Nguyen3da42852015-06-02 22:52:49 -05001807{
Marek Vasut57355402015-07-20 09:20:20 +02001808 u32 d, p, i;
1809 u32 dtaps_per_ptap;
1810 u32 work_bgn, work_end;
Marek Vasut35e47b72015-08-10 23:01:43 +02001811 u32 found_passing_read, found_failing_read = 0, initial_failing_dtap;
Marek Vasut57355402015-07-20 09:20:20 +02001812 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001813
1814 debug("%s:%d %u\n", __func__, __LINE__, grp);
1815
1816 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1817
1818 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1819 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1820
Marek Vasut2f3589c2015-07-19 02:42:21 +02001821 /* Step 0: Determine number of delay taps for each phase tap. */
Marek Vasut139823e2015-08-02 19:47:01 +02001822 dtaps_per_ptap = iocfg->delay_per_opa_tap /
1823 iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001824
Marek Vasut2f3589c2015-07-19 02:42:21 +02001825 /* Step 1: First push vfifo until we get a failing read. */
Marek Vasutd145ca92015-07-19 06:45:43 +02001826 find_vfifo_failing_read(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001827
Marek Vasut2f3589c2015-07-19 02:42:21 +02001828 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001829 work_bgn = 0;
Marek Vasut914546e2015-07-20 09:20:42 +02001830 ret = sdr_working_phase(grp, &work_bgn, &d, &p, &i);
1831 if (ret)
1832 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001833
1834 work_end = work_bgn;
1835
1836 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001837 * If d is 0 then the working window covers a phase tap and we can
1838 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen3da42852015-06-02 22:52:49 -05001839 * and we need to increment the dtaps until we find the end.
1840 */
1841 if (d == 0) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001842 /*
1843 * Step 3a: If we have room, back off by one and
1844 * increment in dtaps.
1845 */
Marek Vasut8c887b62015-07-19 06:37:51 +02001846 sdr_backup_phase(grp, &work_bgn, &p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001847
Marek Vasut2f3589c2015-07-19 02:42:21 +02001848 /*
1849 * Step 4a: go forward from working phase to non working
1850 * phase, increment in ptaps.
1851 */
Marek Vasut914546e2015-07-20 09:20:42 +02001852 ret = sdr_nonworking_phase(grp, &work_end, &p, &i);
1853 if (ret)
1854 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001855
Marek Vasut2f3589c2015-07-19 02:42:21 +02001856 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05001857
1858 /* Special case code for backing up a phase */
1859 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001860 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001861 rw_mgr_decr_vfifo(grp);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001862 } else {
1863 p = p - 1;
1864 }
1865
Marek Vasut160695d2015-08-02 19:10:58 +02001866 work_end -= iocfg->delay_per_opa_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001867 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1868
Dinh Nguyen3da42852015-06-02 22:52:49 -05001869 d = 0;
1870
Marek Vasutea9aa242016-04-04 21:21:05 +02001871 debug_cond(DLEVEL >= 2, "%s:%d p: ptap=%u\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001872 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001873 }
1874
Marek Vasut2f3589c2015-07-19 02:42:21 +02001875 /* The dtap increment to find the failing edge is done here. */
Marek Vasut52e8f212015-07-19 07:27:06 +02001876 sdr_find_phase_delay(0, 1, grp, &work_end,
Marek Vasut160695d2015-08-02 19:10:58 +02001877 iocfg->delay_per_dqs_en_dchain_tap, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001878
1879 /* Go back to working dtap */
1880 if (d != 0)
Marek Vasut160695d2015-08-02 19:10:58 +02001881 work_end -= iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001882
Marek Vasutea9aa242016-04-04 21:21:05 +02001883 debug_cond(DLEVEL >= 2,
Marek Vasut2f3589c2015-07-19 02:42:21 +02001884 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1885 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001886
1887 if (work_end < work_bgn) {
1888 /* nil range */
Marek Vasutea9aa242016-04-04 21:21:05 +02001889 debug_cond(DLEVEL >= 2, "%s:%d end-2: failed\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001890 __func__, __LINE__);
Marek Vasut914546e2015-07-20 09:20:42 +02001891 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001892 }
1893
Marek Vasutea9aa242016-04-04 21:21:05 +02001894 debug_cond(DLEVEL >= 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001895 __func__, __LINE__, work_bgn, work_end);
1896
Dinh Nguyen3da42852015-06-02 22:52:49 -05001897 /*
Marek Vasut2f3589c2015-07-19 02:42:21 +02001898 * We need to calculate the number of dtaps that equal a ptap.
1899 * To do that we'll back up a ptap and re-find the edge of the
1900 * window using dtaps
Dinh Nguyen3da42852015-06-02 22:52:49 -05001901 */
Marek Vasutea9aa242016-04-04 21:21:05 +02001902 debug_cond(DLEVEL >= 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001903 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001904
1905 /* Special case code for backing up a phase */
1906 if (p == 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02001907 p = iocfg->dqs_en_phase_max;
Marek Vasut8c887b62015-07-19 06:37:51 +02001908 rw_mgr_decr_vfifo(grp);
Marek Vasutea9aa242016-04-04 21:21:05 +02001909 debug_cond(DLEVEL >= 2, "%s:%d backedup cycle/phase: p=%u\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001910 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001911 } else {
1912 p = p - 1;
Marek Vasutea9aa242016-04-04 21:21:05 +02001913 debug_cond(DLEVEL >= 2, "%s:%d backedup phase only: p=%u",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001914 __func__, __LINE__, p);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001915 }
1916
1917 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1918
1919 /*
1920 * Increase dtap until we first see a passing read (in case the
Marek Vasut2f3589c2015-07-19 02:42:21 +02001921 * window is smaller than a ptap), and then a failing read to
1922 * mark the edge of the window again.
Dinh Nguyen3da42852015-06-02 22:52:49 -05001923 */
1924
Marek Vasut2f3589c2015-07-19 02:42:21 +02001925 /* Find a passing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001926 debug_cond(DLEVEL >= 2, "%s:%d find passing read\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05001927 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001928
Dinh Nguyen3da42852015-06-02 22:52:49 -05001929 initial_failing_dtap = d;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001930
Marek Vasut52e8f212015-07-19 07:27:06 +02001931 found_passing_read = !sdr_find_phase_delay(1, 1, grp, NULL, 0, &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001932 if (found_passing_read) {
Marek Vasut2f3589c2015-07-19 02:42:21 +02001933 /* Find a failing read. */
Marek Vasutea9aa242016-04-04 21:21:05 +02001934 debug_cond(DLEVEL >= 2, "%s:%d find failing read\n",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001935 __func__, __LINE__);
Marek Vasut52e8f212015-07-19 07:27:06 +02001936 d++;
1937 found_failing_read = !sdr_find_phase_delay(0, 1, grp, NULL, 0,
1938 &d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001939 } else {
Marek Vasutea9aa242016-04-04 21:21:05 +02001940 debug_cond(DLEVEL >= 1,
Marek Vasut2f3589c2015-07-19 02:42:21 +02001941 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1942 __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001943 }
1944
1945 /*
1946 * The dynamically calculated dtaps_per_ptap is only valid if we
1947 * found a passing/failing read. If we didn't, it means d hit the max
Marek Vasut160695d2015-08-02 19:10:58 +02001948 * (iocfg->dqs_en_delay_max). Otherwise, dtaps_per_ptap retains its
Dinh Nguyen3da42852015-06-02 22:52:49 -05001949 * statically calculated value.
1950 */
1951 if (found_passing_read && found_failing_read)
1952 dtaps_per_ptap = d - initial_failing_dtap;
1953
Marek Vasut1273dd92015-07-12 21:05:08 +02001954 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasutea9aa242016-04-04 21:21:05 +02001955 debug_cond(DLEVEL >= 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
Marek Vasut2f3589c2015-07-19 02:42:21 +02001956 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001957
Marek Vasut2f3589c2015-07-19 02:42:21 +02001958 /* Step 6: Find the centre of the window. */
Marek Vasut914546e2015-07-20 09:20:42 +02001959 ret = sdr_find_window_center(grp, work_bgn, work_end);
Dinh Nguyen3da42852015-06-02 22:52:49 -05001960
Marek Vasut914546e2015-07-20 09:20:42 +02001961 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05001962}
1963
Marek Vasutc4907892015-07-13 02:11:02 +02001964/**
Marek Vasut901dc362015-07-13 02:48:34 +02001965 * search_stop_check() - Check if the detected edge is valid
1966 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1967 * @d: DQS delay
1968 * @rank_bgn: Rank number
1969 * @write_group: Write Group
1970 * @read_group: Read Group
1971 * @bit_chk: Resulting bit mask after the test
1972 * @sticky_bit_chk: Resulting sticky bit mask after the test
1973 * @use_read_test: Perform read test
1974 *
1975 * Test if the found edge is valid.
1976 */
1977static u32 search_stop_check(const int write, const int d, const int rank_bgn,
1978 const u32 write_group, const u32 read_group,
1979 u32 *bit_chk, u32 *sticky_bit_chk,
1980 const u32 use_read_test)
1981{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001982 const u32 ratio = rwcfg->mem_if_read_dqs_width /
1983 rwcfg->mem_if_write_dqs_width;
Marek Vasut901dc362015-07-13 02:48:34 +02001984 const u32 correct_mask = write ? param->write_correct_mask :
1985 param->read_correct_mask;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02001986 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
1987 rwcfg->mem_dq_per_read_dqs;
Marek Vasut901dc362015-07-13 02:48:34 +02001988 u32 ret;
1989 /*
1990 * Stop searching when the read test doesn't pass AND when
1991 * we've seen a passing read on every bit.
1992 */
1993 if (write) { /* WRITE-ONLY */
1994 ret = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1995 0, PASS_ONE_BIT,
1996 bit_chk, 0);
1997 } else if (use_read_test) { /* READ-ONLY */
1998 ret = !rw_mgr_mem_calibrate_read_test(rank_bgn, read_group,
1999 NUM_READ_PB_TESTS,
2000 PASS_ONE_BIT, bit_chk,
2001 0, 0);
2002 } else { /* READ-ONLY */
2003 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 0,
2004 PASS_ONE_BIT, bit_chk, 0);
2005 *bit_chk = *bit_chk >> (per_dqs *
2006 (read_group - (write_group * ratio)));
2007 ret = (*bit_chk == 0);
2008 }
2009 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2010 ret = ret && (*sticky_bit_chk == correct_mask);
Marek Vasutea9aa242016-04-04 21:21:05 +02002011 debug_cond(DLEVEL >= 2,
Marek Vasut901dc362015-07-13 02:48:34 +02002012 "%s:%d center(left): dtap=%u => %u == %u && %u",
2013 __func__, __LINE__, d,
2014 *sticky_bit_chk, correct_mask, ret);
2015 return ret;
2016}
2017
2018/**
Marek Vasut71120772015-07-13 02:38:15 +02002019 * search_left_edge() - Find left edge of DQ/DQS working phase
2020 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2021 * @rank_bgn: Rank number
2022 * @write_group: Write Group
2023 * @read_group: Read Group
2024 * @test_bgn: Rank number to begin the test
Marek Vasut71120772015-07-13 02:38:15 +02002025 * @sticky_bit_chk: Resulting sticky bit mask after the test
2026 * @left_edge: Left edge of the DQ/DQS phase
2027 * @right_edge: Right edge of the DQ/DQS phase
2028 * @use_read_test: Perform read test
2029 *
2030 * Find left edge of DQ/DQS working phase.
2031 */
2032static void search_left_edge(const int write, const int rank_bgn,
2033 const u32 write_group, const u32 read_group, const u32 test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002034 u32 *sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002035 int *left_edge, int *right_edge, const u32 use_read_test)
2036{
Marek Vasut139823e2015-08-02 19:47:01 +02002037 const u32 delay_max = write ? iocfg->io_out1_delay_max :
2038 iocfg->io_in_delay_max;
2039 const u32 dqs_max = write ? iocfg->io_out1_delay_max :
2040 iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002041 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2042 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002043 u32 stop, bit_chk;
Marek Vasut71120772015-07-13 02:38:15 +02002044 int i, d;
2045
2046 for (d = 0; d <= dqs_max; d++) {
2047 if (write)
2048 scc_mgr_apply_group_dq_out1_delay(d);
2049 else
2050 scc_mgr_apply_group_dq_in_delay(test_bgn, d);
2051
2052 writel(0, &sdr_scc_mgr->update);
2053
Marek Vasut901dc362015-07-13 02:48:34 +02002054 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002055 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002056 use_read_test);
Marek Vasut71120772015-07-13 02:38:15 +02002057 if (stop == 1)
2058 break;
2059
2060 /* stop != 1 */
2061 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002062 if (bit_chk & 1) {
Marek Vasut71120772015-07-13 02:38:15 +02002063 /*
2064 * Remember a passing test as
2065 * the left_edge.
2066 */
2067 left_edge[i] = d;
2068 } else {
2069 /*
2070 * If a left edge has not been seen
2071 * yet, then a future passing test
2072 * will mark this edge as the right
2073 * edge.
2074 */
2075 if (left_edge[i] == delay_max + 1)
2076 right_edge[i] = -(d + 1);
2077 }
Marek Vasut0c4be192015-07-18 20:34:00 +02002078 bit_chk >>= 1;
Marek Vasut71120772015-07-13 02:38:15 +02002079 }
2080 }
2081
2082 /* Reset DQ delay chains to 0 */
2083 if (write)
2084 scc_mgr_apply_group_dq_out1_delay(0);
2085 else
2086 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2087
2088 *sticky_bit_chk = 0;
2089 for (i = per_dqs - 1; i >= 0; i--) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002090 debug_cond(DLEVEL >= 2,
Marek Vasut71120772015-07-13 02:38:15 +02002091 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2092 __func__, __LINE__, i, left_edge[i],
2093 i, right_edge[i]);
2094
2095 /*
2096 * Check for cases where we haven't found the left edge,
2097 * which makes our assignment of the the right edge invalid.
2098 * Reset it to the illegal value.
2099 */
2100 if ((left_edge[i] == delay_max + 1) &&
2101 (right_edge[i] != delay_max + 1)) {
2102 right_edge[i] = delay_max + 1;
Marek Vasutea9aa242016-04-04 21:21:05 +02002103 debug_cond(DLEVEL >= 2,
Marek Vasut71120772015-07-13 02:38:15 +02002104 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2105 __func__, __LINE__, i, right_edge[i]);
2106 }
2107
2108 /*
2109 * Reset sticky bit
2110 * READ: except for bits where we have seen both
2111 * the left and right edge.
2112 * WRITE: except for bits where we have seen the
2113 * left edge.
2114 */
2115 *sticky_bit_chk <<= 1;
2116 if (write) {
2117 if (left_edge[i] != delay_max + 1)
2118 *sticky_bit_chk |= 1;
2119 } else {
2120 if ((left_edge[i] != delay_max + 1) &&
2121 (right_edge[i] != delay_max + 1))
2122 *sticky_bit_chk |= 1;
2123 }
2124 }
Marek Vasut71120772015-07-13 02:38:15 +02002125}
2126
2127/**
Marek Vasutc4907892015-07-13 02:11:02 +02002128 * search_right_edge() - Find right edge of DQ/DQS working phase
2129 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2130 * @rank_bgn: Rank number
2131 * @write_group: Write Group
2132 * @read_group: Read Group
2133 * @start_dqs: DQS start phase
2134 * @start_dqs_en: DQS enable start phase
Marek Vasutc4907892015-07-13 02:11:02 +02002135 * @sticky_bit_chk: Resulting sticky bit mask after the test
2136 * @left_edge: Left edge of the DQ/DQS phase
2137 * @right_edge: Right edge of the DQ/DQS phase
2138 * @use_read_test: Perform read test
2139 *
2140 * Find right edge of DQ/DQS working phase.
2141 */
2142static int search_right_edge(const int write, const int rank_bgn,
2143 const u32 write_group, const u32 read_group,
2144 const int start_dqs, const int start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002145 u32 *sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002146 int *left_edge, int *right_edge, const u32 use_read_test)
2147{
Marek Vasut139823e2015-08-02 19:47:01 +02002148 const u32 delay_max = write ? iocfg->io_out1_delay_max :
2149 iocfg->io_in_delay_max;
2150 const u32 dqs_max = write ? iocfg->io_out1_delay_max :
2151 iocfg->dqs_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002152 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2153 rwcfg->mem_dq_per_read_dqs;
Marek Vasut0c4be192015-07-18 20:34:00 +02002154 u32 stop, bit_chk;
Marek Vasutc4907892015-07-13 02:11:02 +02002155 int i, d;
2156
2157 for (d = 0; d <= dqs_max - start_dqs; d++) {
2158 if (write) { /* WRITE-ONLY */
2159 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2160 d + start_dqs);
2161 } else { /* READ-ONLY */
2162 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002163 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut5ded7322015-08-02 19:42:26 +02002164 u32 delay = d + start_dqs_en;
Marek Vasut160695d2015-08-02 19:10:58 +02002165 if (delay > iocfg->dqs_en_delay_max)
2166 delay = iocfg->dqs_en_delay_max;
Marek Vasutc4907892015-07-13 02:11:02 +02002167 scc_mgr_set_dqs_en_delay(read_group, delay);
2168 }
2169 scc_mgr_load_dqs(read_group);
2170 }
2171
2172 writel(0, &sdr_scc_mgr->update);
2173
Marek Vasut901dc362015-07-13 02:48:34 +02002174 stop = search_stop_check(write, d, rank_bgn, write_group,
Marek Vasut0c4be192015-07-18 20:34:00 +02002175 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut901dc362015-07-13 02:48:34 +02002176 use_read_test);
Marek Vasutc4907892015-07-13 02:11:02 +02002177 if (stop == 1) {
2178 if (write && (d == 0)) { /* WRITE-ONLY */
Marek Vasut139823e2015-08-02 19:47:01 +02002179 for (i = 0; i < rwcfg->mem_dq_per_write_dqs;
2180 i++) {
Marek Vasutc4907892015-07-13 02:11:02 +02002181 /*
2182 * d = 0 failed, but it passed when
2183 * testing the left edge, so it must be
2184 * marginal, set it to -1
2185 */
2186 if (right_edge[i] == delay_max + 1 &&
2187 left_edge[i] != delay_max + 1)
2188 right_edge[i] = -1;
2189 }
2190 }
2191 break;
2192 }
2193
2194 /* stop != 1 */
2195 for (i = 0; i < per_dqs; i++) {
Marek Vasut0c4be192015-07-18 20:34:00 +02002196 if (bit_chk & 1) {
Marek Vasutc4907892015-07-13 02:11:02 +02002197 /*
2198 * Remember a passing test as
2199 * the right_edge.
2200 */
2201 right_edge[i] = d;
2202 } else {
2203 if (d != 0) {
2204 /*
2205 * If a right edge has not
2206 * been seen yet, then a future
2207 * passing test will mark this
2208 * edge as the left edge.
2209 */
2210 if (right_edge[i] == delay_max + 1)
2211 left_edge[i] = -(d + 1);
2212 } else {
2213 /*
2214 * d = 0 failed, but it passed
2215 * when testing the left edge,
2216 * so it must be marginal, set
2217 * it to -1
2218 */
2219 if (right_edge[i] == delay_max + 1 &&
2220 left_edge[i] != delay_max + 1)
2221 right_edge[i] = -1;
2222 /*
2223 * If a right edge has not been
2224 * seen yet, then a future
2225 * passing test will mark this
2226 * edge as the left edge.
2227 */
2228 else if (right_edge[i] == delay_max + 1)
2229 left_edge[i] = -(d + 1);
2230 }
2231 }
2232
Marek Vasutea9aa242016-04-04 21:21:05 +02002233 debug_cond(DLEVEL >= 2, "%s:%d center[r,d=%u]: ",
Marek Vasutc4907892015-07-13 02:11:02 +02002234 __func__, __LINE__, d);
Marek Vasutea9aa242016-04-04 21:21:05 +02002235 debug_cond(DLEVEL >= 2,
Marek Vasutc4907892015-07-13 02:11:02 +02002236 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasut0c4be192015-07-18 20:34:00 +02002237 bit_chk & 1, i, left_edge[i]);
Marek Vasutea9aa242016-04-04 21:21:05 +02002238 debug_cond(DLEVEL >= 2, "right_edge[%u]: %d\n", i,
Marek Vasutc4907892015-07-13 02:11:02 +02002239 right_edge[i]);
Marek Vasut0c4be192015-07-18 20:34:00 +02002240 bit_chk >>= 1;
Marek Vasutc4907892015-07-13 02:11:02 +02002241 }
2242 }
2243
2244 /* Check that all bits have a window */
2245 for (i = 0; i < per_dqs; i++) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002246 debug_cond(DLEVEL >= 2,
Marek Vasutc4907892015-07-13 02:11:02 +02002247 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2248 __func__, __LINE__, i, left_edge[i],
2249 i, right_edge[i]);
2250 if ((left_edge[i] == dqs_max + 1) ||
2251 (right_edge[i] == dqs_max + 1))
2252 return i + 1; /* FIXME: If we fail, retval > 0 */
2253 }
2254
2255 return 0;
2256}
2257
Marek Vasutafb3eb82015-07-18 19:18:06 +02002258/**
2259 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2260 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2261 * @left_edge: Left edge of the DQ/DQS phase
2262 * @right_edge: Right edge of the DQ/DQS phase
2263 * @mid_min: Best DQ/DQS phase middle setting
2264 *
2265 * Find index and value of the middle of the DQ/DQS working phase.
2266 */
2267static int get_window_mid_index(const int write, int *left_edge,
2268 int *right_edge, int *mid_min)
2269{
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002270 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2271 rwcfg->mem_dq_per_read_dqs;
Marek Vasutafb3eb82015-07-18 19:18:06 +02002272 int i, mid, min_index;
2273
2274 /* Find middle of window for each DQ bit */
2275 *mid_min = left_edge[0] - right_edge[0];
2276 min_index = 0;
2277 for (i = 1; i < per_dqs; i++) {
2278 mid = left_edge[i] - right_edge[i];
2279 if (mid < *mid_min) {
2280 *mid_min = mid;
2281 min_index = i;
2282 }
2283 }
2284
2285 /*
2286 * -mid_min/2 represents the amount that we need to move DQS.
2287 * If mid_min is odd and positive we'll need to add one to make
2288 * sure the rounding in further calculations is correct (always
2289 * bias to the right), so just add 1 for all positive values.
2290 */
2291 if (*mid_min > 0)
2292 (*mid_min)++;
2293 *mid_min = *mid_min / 2;
2294
Marek Vasutea9aa242016-04-04 21:21:05 +02002295 debug_cond(DLEVEL >= 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
Marek Vasutafb3eb82015-07-18 19:18:06 +02002296 __func__, __LINE__, *mid_min, min_index);
2297 return min_index;
2298}
2299
Marek Vasutffb8b662015-07-18 19:46:26 +02002300/**
2301 * center_dq_windows() - Center the DQ/DQS windows
2302 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2303 * @left_edge: Left edge of the DQ/DQS phase
2304 * @right_edge: Right edge of the DQ/DQS phase
2305 * @mid_min: Adjusted DQ/DQS phase middle setting
2306 * @orig_mid_min: Original DQ/DQS phase middle setting
2307 * @min_index: DQ/DQS phase middle setting index
2308 * @test_bgn: Rank number to begin the test
2309 * @dq_margin: Amount of shift for the DQ
2310 * @dqs_margin: Amount of shift for the DQS
2311 *
2312 * Align the DQ/DQS windows in each group.
2313 */
2314static void center_dq_windows(const int write, int *left_edge, int *right_edge,
2315 const int mid_min, const int orig_mid_min,
2316 const int min_index, const int test_bgn,
2317 int *dq_margin, int *dqs_margin)
2318{
Marek Vasut139823e2015-08-02 19:47:01 +02002319 const u32 delay_max = write ? iocfg->io_out1_delay_max :
2320 iocfg->io_in_delay_max;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002321 const u32 per_dqs = write ? rwcfg->mem_dq_per_write_dqs :
2322 rwcfg->mem_dq_per_read_dqs;
Marek Vasutffb8b662015-07-18 19:46:26 +02002323 const u32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
2324 SCC_MGR_IO_IN_DELAY_OFFSET;
2325 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
2326
2327 u32 temp_dq_io_delay1, temp_dq_io_delay2;
2328 int shift_dq, i, p;
2329
2330 /* Initialize data for export structures */
2331 *dqs_margin = delay_max + 1;
2332 *dq_margin = delay_max + 1;
2333
2334 /* add delay to bring centre of all DQ windows to the same "level" */
2335 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2336 /* Use values before divide by 2 to reduce round off error */
2337 shift_dq = (left_edge[i] - right_edge[i] -
2338 (left_edge[min_index] - right_edge[min_index]))/2 +
2339 (orig_mid_min - mid_min);
2340
Marek Vasutea9aa242016-04-04 21:21:05 +02002341 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002342 "vfifo_center: before: shift_dq[%u]=%d\n",
2343 i, shift_dq);
2344
2345 temp_dq_io_delay1 = readl(addr + (p << 2));
2346 temp_dq_io_delay2 = readl(addr + (i << 2));
2347
2348 if (shift_dq + temp_dq_io_delay1 > delay_max)
2349 shift_dq = delay_max - temp_dq_io_delay2;
2350 else if (shift_dq + temp_dq_io_delay1 < 0)
2351 shift_dq = -temp_dq_io_delay1;
2352
Marek Vasutea9aa242016-04-04 21:21:05 +02002353 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002354 "vfifo_center: after: shift_dq[%u]=%d\n",
2355 i, shift_dq);
2356
2357 if (write)
Marek Vasut139823e2015-08-02 19:47:01 +02002358 scc_mgr_set_dq_out1_delay(i,
2359 temp_dq_io_delay1 + shift_dq);
Marek Vasutffb8b662015-07-18 19:46:26 +02002360 else
Marek Vasut139823e2015-08-02 19:47:01 +02002361 scc_mgr_set_dq_in_delay(p,
2362 temp_dq_io_delay1 + shift_dq);
Marek Vasutffb8b662015-07-18 19:46:26 +02002363
2364 scc_mgr_load_dq(p);
2365
Marek Vasutea9aa242016-04-04 21:21:05 +02002366 debug_cond(DLEVEL >= 2,
Marek Vasutffb8b662015-07-18 19:46:26 +02002367 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2368 left_edge[i] - shift_dq + (-mid_min),
2369 right_edge[i] + shift_dq - (-mid_min));
2370
2371 /* To determine values for export structures */
2372 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2373 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2374
2375 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2376 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2377 }
Marek Vasutffb8b662015-07-18 19:46:26 +02002378}
2379
Marek Vasutac63b9a2015-07-21 04:27:32 +02002380/**
2381 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2382 * @rank_bgn: Rank number
2383 * @rw_group: Read/Write Group
2384 * @test_bgn: Rank at which the test begins
2385 * @use_read_test: Perform a read test
2386 * @update_fom: Update FOM
2387 *
2388 * Per-bit deskew DQ and centering.
2389 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002390static int rw_mgr_mem_calibrate_vfifo_center(const u32 rank_bgn,
2391 const u32 rw_group, const u32 test_bgn,
2392 const int use_read_test, const int update_fom)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002393{
Marek Vasut5d6db442015-07-18 19:57:12 +02002394 const u32 addr =
2395 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasut0113c3e2015-07-18 20:42:27 +02002396 (rw_group << 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002397 /*
2398 * Store these as signed since there are comparisons with
2399 * signed numbers.
2400 */
Marek Vasut5ded7322015-08-02 19:42:26 +02002401 u32 sticky_bit_chk;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002402 int32_t left_edge[rwcfg->mem_dq_per_read_dqs];
2403 int32_t right_edge[rwcfg->mem_dq_per_read_dqs];
Dinh Nguyen3da42852015-06-02 22:52:49 -05002404 int32_t orig_mid_min, mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002405 int32_t new_dqs, start_dqs, start_dqs_en = 0, final_dqs_en;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002406 int32_t dq_margin, dqs_margin;
Marek Vasut5d6db442015-07-18 19:57:12 +02002407 int i, min_index;
Marek Vasutc4907892015-07-13 02:11:02 +02002408 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002409
Marek Vasut0113c3e2015-07-18 20:42:27 +02002410 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002411
Marek Vasut5d6db442015-07-18 19:57:12 +02002412 start_dqs = readl(addr);
Marek Vasut160695d2015-08-02 19:10:58 +02002413 if (iocfg->shift_dqs_en_when_shift_dqs)
2414 start_dqs_en = readl(addr - iocfg->dqs_en_delay_offset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002415
2416 /* set the left and right edge of each bit to an illegal value */
Marek Vasut160695d2015-08-02 19:10:58 +02002417 /* use (iocfg->io_in_delay_max + 1) as an illegal value */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002418 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002419 for (i = 0; i < rwcfg->mem_dq_per_read_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002420 left_edge[i] = iocfg->io_in_delay_max + 1;
2421 right_edge[i] = iocfg->io_in_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002422 }
2423
Dinh Nguyen3da42852015-06-02 22:52:49 -05002424 /* Search for the left edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002425 search_left_edge(0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002426 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002427 left_edge, right_edge, use_read_test);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002428
Marek Vasutf0712c32015-07-18 08:01:45 +02002429
Dinh Nguyen3da42852015-06-02 22:52:49 -05002430 /* Search for the right edge of the window for each bit */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002431 ret = search_right_edge(0, rank_bgn, rw_group, rw_group,
Marek Vasutc4907892015-07-13 02:11:02 +02002432 start_dqs, start_dqs_en,
Marek Vasut0c4be192015-07-18 20:34:00 +02002433 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002434 left_edge, right_edge, use_read_test);
2435 if (ret) {
2436 /*
2437 * Restore delay chain settings before letting the loop
2438 * in rw_mgr_mem_calibrate_vfifo to retry different
2439 * dqs/ck relationships.
2440 */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002441 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Marek Vasut160695d2015-08-02 19:10:58 +02002442 if (iocfg->shift_dqs_en_when_shift_dqs)
Marek Vasut0113c3e2015-07-18 20:42:27 +02002443 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002444
Marek Vasut0113c3e2015-07-18 20:42:27 +02002445 scc_mgr_load_dqs(rw_group);
Marek Vasut1273dd92015-07-12 21:05:08 +02002446 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002447
Marek Vasutea9aa242016-04-04 21:21:05 +02002448 debug_cond(DLEVEL >= 1,
Marek Vasutc4907892015-07-13 02:11:02 +02002449 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2450 __func__, __LINE__, i, left_edge[i], right_edge[i]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002451 if (use_read_test) {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002452 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002453 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002454 CAL_STAGE_VFIFO,
2455 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002456 } else {
Marek Vasut0113c3e2015-07-18 20:42:27 +02002457 set_failing_group_stage(rw_group *
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002458 rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutc4907892015-07-13 02:11:02 +02002459 CAL_STAGE_VFIFO_AFTER_WRITES,
2460 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002461 }
Marek Vasut98668242015-07-18 20:44:28 +02002462 return -EIO;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002463 }
2464
Marek Vasutafb3eb82015-07-18 19:18:06 +02002465 min_index = get_window_mid_index(0, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002466
2467 /* Determine the amount we can change DQS (which is -mid_min) */
2468 orig_mid_min = mid_min;
2469 new_dqs = start_dqs - mid_min;
Marek Vasut160695d2015-08-02 19:10:58 +02002470 if (new_dqs > iocfg->dqs_in_delay_max)
2471 new_dqs = iocfg->dqs_in_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002472 else if (new_dqs < 0)
2473 new_dqs = 0;
2474
2475 mid_min = start_dqs - new_dqs;
Marek Vasutea9aa242016-04-04 21:21:05 +02002476 debug_cond(DLEVEL >= 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
Dinh Nguyen3da42852015-06-02 22:52:49 -05002477 mid_min, new_dqs);
2478
Marek Vasut160695d2015-08-02 19:10:58 +02002479 if (iocfg->shift_dqs_en_when_shift_dqs) {
2480 if (start_dqs_en - mid_min > iocfg->dqs_en_delay_max)
Marek Vasut139823e2015-08-02 19:47:01 +02002481 mid_min += start_dqs_en - mid_min -
2482 iocfg->dqs_en_delay_max;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002483 else if (start_dqs_en - mid_min < 0)
2484 mid_min += start_dqs_en - mid_min;
2485 }
2486 new_dqs = start_dqs - mid_min;
2487
Marek Vasutea9aa242016-04-04 21:21:05 +02002488 debug_cond(DLEVEL >= 1,
Marek Vasutf0712c32015-07-18 08:01:45 +02002489 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2490 start_dqs,
Marek Vasut160695d2015-08-02 19:10:58 +02002491 iocfg->shift_dqs_en_when_shift_dqs ? start_dqs_en : -1,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002492 new_dqs, mid_min);
2493
Marek Vasutffb8b662015-07-18 19:46:26 +02002494 /* Add delay to bring centre of all DQ windows to the same "level". */
2495 center_dq_windows(0, left_edge, right_edge, mid_min, orig_mid_min,
2496 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002497
Dinh Nguyen3da42852015-06-02 22:52:49 -05002498 /* Move DQS-en */
Marek Vasut160695d2015-08-02 19:10:58 +02002499 if (iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut5d6db442015-07-18 19:57:12 +02002500 final_dqs_en = start_dqs_en - mid_min;
Marek Vasut0113c3e2015-07-18 20:42:27 +02002501 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2502 scc_mgr_load_dqs(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002503 }
2504
2505 /* Move DQS */
Marek Vasut0113c3e2015-07-18 20:42:27 +02002506 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2507 scc_mgr_load_dqs(rw_group);
Marek Vasutea9aa242016-04-04 21:21:05 +02002508 debug_cond(DLEVEL >= 2,
Marek Vasutf0712c32015-07-18 08:01:45 +02002509 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2510 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002511
2512 /*
2513 * Do not remove this line as it makes sure all of our decisions
2514 * have been applied. Apply the update bit.
2515 */
Marek Vasut1273dd92015-07-12 21:05:08 +02002516 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002517
Marek Vasut98668242015-07-18 20:44:28 +02002518 if ((dq_margin < 0) || (dqs_margin < 0))
2519 return -EINVAL;
2520
2521 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002522}
2523
Marek Vasutbce24ef2015-07-17 03:16:45 +02002524/**
Marek Vasut04372fb2015-07-18 02:46:56 +02002525 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the device
2526 * @rw_group: Read/Write Group
2527 * @phase: DQ/DQS phase
2528 *
2529 * Because initially no communication ca be reliably performed with the memory
2530 * device, the sequencer uses a guaranteed write mechanism to write data into
2531 * the memory device.
2532 */
2533static int rw_mgr_mem_calibrate_guaranteed_write(const u32 rw_group,
2534 const u32 phase)
2535{
Marek Vasut04372fb2015-07-18 02:46:56 +02002536 int ret;
2537
2538 /* Set a particular DQ/DQS phase. */
2539 scc_mgr_set_dqdqs_output_phase_all_ranks(rw_group, phase);
2540
Marek Vasutea9aa242016-04-04 21:21:05 +02002541 debug_cond(DLEVEL >= 1, "%s:%d guaranteed write: g=%u p=%u\n",
Marek Vasut04372fb2015-07-18 02:46:56 +02002542 __func__, __LINE__, rw_group, phase);
2543
2544 /*
2545 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2546 * Load up the patterns used by read calibration using the
2547 * current DQDQS phase.
2548 */
2549 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2550
2551 if (gbl->phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2552 return 0;
2553
2554 /*
2555 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2556 * Back-to-Back reads of the patterns used for calibration.
2557 */
Marek Vasutd844c7d2015-07-18 03:55:07 +02002558 ret = rw_mgr_mem_calibrate_read_test_patterns(0, rw_group, 1);
2559 if (ret)
Marek Vasutea9aa242016-04-04 21:21:05 +02002560 debug_cond(DLEVEL >= 1,
Marek Vasut04372fb2015-07-18 02:46:56 +02002561 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2562 __func__, __LINE__, rw_group, phase);
Marek Vasutd844c7d2015-07-18 03:55:07 +02002563 return ret;
Marek Vasut04372fb2015-07-18 02:46:56 +02002564}
2565
2566/**
Marek Vasutf09da112015-07-18 02:57:32 +02002567 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2568 * @rw_group: Read/Write Group
2569 * @test_bgn: Rank at which the test begins
2570 *
2571 * DQS enable calibration ensures reliable capture of the DQ signal without
2572 * glitches on the DQS line.
2573 */
2574static int rw_mgr_mem_calibrate_dqs_enable_calibration(const u32 rw_group,
2575 const u32 test_bgn)
2576{
Marek Vasutf09da112015-07-18 02:57:32 +02002577 /*
2578 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2579 * DQS and DQS Eanble Signal Relationships.
2580 */
Marek Vasut28ea8272015-07-18 04:28:42 +02002581
2582 /* We start at zero, so have one less dq to devide among */
Marek Vasut160695d2015-08-02 19:10:58 +02002583 const u32 delay_step = iocfg->io_in_delay_max /
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002584 (rwcfg->mem_dq_per_read_dqs - 1);
Marek Vasut914546e2015-07-20 09:20:42 +02002585 int ret;
Marek Vasut28ea8272015-07-18 04:28:42 +02002586 u32 i, p, d, r;
2587
2588 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2589
2590 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002591 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002592 r += NUM_RANKS_PER_SHADOW_REG) {
2593 for (i = 0, p = test_bgn, d = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002594 i < rwcfg->mem_dq_per_read_dqs;
Marek Vasut28ea8272015-07-18 04:28:42 +02002595 i++, p++, d += delay_step) {
Marek Vasutea9aa242016-04-04 21:21:05 +02002596 debug_cond(DLEVEL >= 1,
Marek Vasut28ea8272015-07-18 04:28:42 +02002597 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2598 __func__, __LINE__, rw_group, r, i, p, d);
2599
2600 scc_mgr_set_dq_in_delay(p, d);
2601 scc_mgr_load_dq(p);
2602 }
2603
2604 writel(0, &sdr_scc_mgr->update);
2605 }
2606
2607 /*
2608 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2609 * dq_in_delay values
2610 */
Marek Vasut914546e2015-07-20 09:20:42 +02002611 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(rw_group);
Marek Vasut28ea8272015-07-18 04:28:42 +02002612
Marek Vasutea9aa242016-04-04 21:21:05 +02002613 debug_cond(DLEVEL >= 1,
Marek Vasut28ea8272015-07-18 04:28:42 +02002614 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut914546e2015-07-20 09:20:42 +02002615 __func__, __LINE__, rw_group, !ret);
Marek Vasut28ea8272015-07-18 04:28:42 +02002616
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002617 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut28ea8272015-07-18 04:28:42 +02002618 r += NUM_RANKS_PER_SHADOW_REG) {
2619 scc_mgr_apply_group_dq_in_delay(test_bgn, 0);
2620 writel(0, &sdr_scc_mgr->update);
2621 }
2622
Marek Vasut914546e2015-07-20 09:20:42 +02002623 return ret;
Marek Vasutf09da112015-07-18 02:57:32 +02002624}
2625
2626/**
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002627 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2628 * @rw_group: Read/Write Group
2629 * @test_bgn: Rank at which the test begins
2630 * @use_read_test: Perform a read test
2631 * @update_fom: Update FOM
2632 *
2633 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2634 * within a group.
2635 */
2636static int
2637rw_mgr_mem_calibrate_dq_dqs_centering(const u32 rw_group, const u32 test_bgn,
2638 const int use_read_test,
2639 const int update_fom)
2640
2641{
2642 int ret, grp_calibrated;
2643 u32 rank_bgn, sr;
2644
2645 /*
2646 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2647 * Read per-bit deskew can be done on a per shadow register basis.
2648 */
2649 grp_calibrated = 1;
2650 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002651 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002652 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002653 ret = rw_mgr_mem_calibrate_vfifo_center(rank_bgn, rw_group,
Marek Vasut0113c3e2015-07-18 20:42:27 +02002654 test_bgn,
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002655 use_read_test,
2656 update_fom);
Marek Vasut98668242015-07-18 20:44:28 +02002657 if (!ret)
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002658 continue;
2659
2660 grp_calibrated = 0;
2661 }
2662
2663 if (!grp_calibrated)
2664 return -EIO;
2665
2666 return 0;
2667}
2668
2669/**
Marek Vasutbce24ef2015-07-17 03:16:45 +02002670 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2671 * @rw_group: Read/Write Group
2672 * @test_bgn: Rank at which the test begins
Dinh Nguyen3da42852015-06-02 22:52:49 -05002673 *
Marek Vasutbce24ef2015-07-17 03:16:45 +02002674 * Stage 1: Calibrate the read valid prediction FIFO.
2675 *
2676 * This function implements UniPHY calibration Stage 1, as explained in
2677 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2678 *
2679 * - read valid prediction will consist of finding:
2680 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2681 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002682 * - we also do a per-bit deskew on the DQ lines.
2683 */
Marek Vasutc336ca32015-07-17 04:24:18 +02002684static int rw_mgr_mem_calibrate_vfifo(const u32 rw_group, const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002685{
Marek Vasut5ded7322015-08-02 19:42:26 +02002686 u32 p, d;
2687 u32 dtaps_per_ptap;
2688 u32 failed_substage;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002689
Marek Vasut04372fb2015-07-18 02:46:56 +02002690 int ret;
2691
Marek Vasutc336ca32015-07-17 04:24:18 +02002692 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002693
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002694 /* Update info for sims */
2695 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002696 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002697 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002698
Marek Vasut7c0a9df2015-07-18 03:15:34 +02002699 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2700
2701 /* USER Determine number of delay taps for each phase tap. */
Marek Vasut160695d2015-08-02 19:10:58 +02002702 dtaps_per_ptap = DIV_ROUND_UP(iocfg->delay_per_opa_tap,
2703 iocfg->delay_per_dqs_en_dchain_tap) - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002704
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002705 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002706 /*
2707 * In RLDRAMX we may be messing the delay of pins in
Marek Vasutc336ca32015-07-17 04:24:18 +02002708 * the same write rw_group but outside of the current read
2709 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutac70d2f2015-07-17 03:44:26 +02002710 * output side yet.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002711 */
2712 if (d > 0) {
Marek Vasutf51a7d32015-07-19 02:18:21 +02002713 scc_mgr_apply_group_all_out_delay_add_all_ranks(
Marek Vasutc336ca32015-07-17 04:24:18 +02002714 rw_group, d);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002715 }
2716
Marek Vasut160695d2015-08-02 19:10:58 +02002717 for (p = 0; p <= iocfg->dqdqs_out_phase_max; p++) {
Marek Vasut04372fb2015-07-18 02:46:56 +02002718 /* 1) Guaranteed Write */
2719 ret = rw_mgr_mem_calibrate_guaranteed_write(rw_group, p);
2720 if (ret)
2721 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002722
Marek Vasutf09da112015-07-18 02:57:32 +02002723 /* 2) DQS Enable Calibration */
2724 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(rw_group,
2725 test_bgn);
2726 if (ret) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05002727 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002728 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002729 }
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002730
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002731 /* 3) Centering DQ/DQS */
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002732 /*
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002733 * If doing read after write calibration, do not update
2734 * FOM now. Do it then.
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002735 */
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002736 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group,
2737 test_bgn, 1, 0);
2738 if (ret) {
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002739 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002740 continue;
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002741 }
2742
Marek Vasut16cfc4b2015-07-18 03:10:31 +02002743 /* All done. */
2744 goto cal_done_ok;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002745 }
2746 }
2747
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002748 /* Calibration Stage 1 failed. */
Marek Vasutc336ca32015-07-17 04:24:18 +02002749 set_failing_group_stage(rw_group, CAL_STAGE_VFIFO, failed_substage);
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002750 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002751
Marek Vasutfe2d0a22015-07-17 03:50:17 +02002752 /* Calibration Stage 1 completed OK. */
2753cal_done_ok:
Dinh Nguyen3da42852015-06-02 22:52:49 -05002754 /*
2755 * Reset the delay chains back to zero if they have moved > 1
2756 * (check for > 1 because loop will increase d even when pass in
2757 * first case).
2758 */
2759 if (d > 2)
Marek Vasutc336ca32015-07-17 04:24:18 +02002760 scc_mgr_zero_group(rw_group, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002761
2762 return 1;
2763}
2764
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002765/**
2766 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2767 * @rw_group: Read/Write Group
2768 * @test_bgn: Rank at which the test begins
2769 *
2770 * Stage 3: DQ/DQS Centering.
2771 *
2772 * This function implements UniPHY calibration Stage 3, as explained in
2773 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2774 */
2775static int rw_mgr_mem_calibrate_vfifo_end(const u32 rw_group,
2776 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002777{
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002778 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002779
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002780 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002781
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002782 /* Update info for sims. */
2783 reg_file_set_group(rw_group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002784 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2785 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2786
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002787 ret = rw_mgr_mem_calibrate_dq_dqs_centering(rw_group, test_bgn, 0, 1);
2788 if (ret)
2789 set_failing_group_stage(rw_group,
Dinh Nguyen3da42852015-06-02 22:52:49 -05002790 CAL_STAGE_VFIFO_AFTER_WRITES,
2791 CAL_SUBSTAGE_VFIFO_CENTER);
Marek Vasut78cdd7d2015-07-18 05:58:44 +02002792 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002793}
2794
Marek Vasutc9842782015-07-21 06:18:57 +02002795/**
2796 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2797 *
2798 * Stage 4: Minimize latency.
2799 *
2800 * This function implements UniPHY calibration Stage 4, as explained in
2801 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2802 * Calibrate LFIFO to find smallest read latency.
2803 */
Marek Vasut5ded7322015-08-02 19:42:26 +02002804static u32 rw_mgr_mem_calibrate_lfifo(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002805{
Marek Vasutc9842782015-07-21 06:18:57 +02002806 int found_one = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002807
2808 debug("%s:%d\n", __func__, __LINE__);
2809
Marek Vasutc9842782015-07-21 06:18:57 +02002810 /* Update info for sims. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002811 reg_file_set_stage(CAL_STAGE_LFIFO);
2812 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2813
2814 /* Load up the patterns used by read calibration for all ranks */
2815 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002816
Dinh Nguyen3da42852015-06-02 22:52:49 -05002817 do {
Marek Vasut1273dd92015-07-12 21:05:08 +02002818 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutea9aa242016-04-04 21:21:05 +02002819 debug_cond(DLEVEL >= 2, "%s:%d lfifo: read_lat=%u",
Dinh Nguyen3da42852015-06-02 22:52:49 -05002820 __func__, __LINE__, gbl->curr_read_lat);
2821
Marek Vasutc9842782015-07-21 06:18:57 +02002822 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0, NUM_READ_TESTS,
2823 PASS_ALL_BITS, 1))
Dinh Nguyen3da42852015-06-02 22:52:49 -05002824 break;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002825
2826 found_one = 1;
Marek Vasutc9842782015-07-21 06:18:57 +02002827 /*
2828 * Reduce read latency and see if things are
2829 * working correctly.
2830 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002831 gbl->curr_read_lat--;
2832 } while (gbl->curr_read_lat > 0);
2833
Marek Vasutc9842782015-07-21 06:18:57 +02002834 /* Reset the fifos to get pointers to known state. */
Marek Vasut1273dd92015-07-12 21:05:08 +02002835 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002836
2837 if (found_one) {
Marek Vasutc9842782015-07-21 06:18:57 +02002838 /* Add a fudge factor to the read latency that was determined */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002839 gbl->curr_read_lat += 2;
Marek Vasut1273dd92015-07-12 21:05:08 +02002840 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasutea9aa242016-04-04 21:21:05 +02002841 debug_cond(DLEVEL >= 2,
Marek Vasutc9842782015-07-21 06:18:57 +02002842 "%s:%d lfifo: success: using read_lat=%u\n",
2843 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002844 } else {
2845 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2846 CAL_SUBSTAGE_READ_LATENCY);
2847
Marek Vasutea9aa242016-04-04 21:21:05 +02002848 debug_cond(DLEVEL >= 2,
Marek Vasutc9842782015-07-21 06:18:57 +02002849 "%s:%d lfifo: failed at initial read_lat=%u\n",
2850 __func__, __LINE__, gbl->curr_read_lat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002851 }
Marek Vasutc9842782015-07-21 06:18:57 +02002852
2853 return found_one;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002854}
2855
Marek Vasutc8570af2015-07-21 05:26:58 +02002856/**
2857 * search_window() - Search for the/part of the window with DM/DQS shift
2858 * @search_dm: If 1, search for the DM shift, if 0, search for DQS shift
2859 * @rank_bgn: Rank number
2860 * @write_group: Write Group
2861 * @bgn_curr: Current window begin
2862 * @end_curr: Current window end
2863 * @bgn_best: Current best window begin
2864 * @end_best: Current best window end
2865 * @win_best: Size of the best window
2866 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2867 *
2868 * Search for the/part of the window with DM/DQS shift.
2869 */
2870static void search_window(const int search_dm,
2871 const u32 rank_bgn, const u32 write_group,
2872 int *bgn_curr, int *end_curr, int *bgn_best,
2873 int *end_best, int *win_best, int new_dqs)
2874{
2875 u32 bit_chk;
Marek Vasut160695d2015-08-02 19:10:58 +02002876 const int max = iocfg->io_out1_delay_max - new_dqs;
Marek Vasutc8570af2015-07-21 05:26:58 +02002877 int d, di;
2878
2879 /* Search for the/part of the window with DM/DQS shift. */
2880 for (di = max; di >= 0; di -= DELTA_D) {
2881 if (search_dm) {
2882 d = di;
2883 scc_mgr_apply_group_dm_out1_delay(d);
2884 } else {
2885 /* For DQS, we go from 0...max */
2886 d = max - di;
2887 /*
Marek Vasut139823e2015-08-02 19:47:01 +02002888 * Note: This only shifts DQS, so are we limiting
2889 * ourselves to width of DQ unnecessarily.
Marek Vasutc8570af2015-07-21 05:26:58 +02002890 */
2891 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2892 d + new_dqs);
2893 }
2894
2895 writel(0, &sdr_scc_mgr->update);
2896
2897 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2898 PASS_ALL_BITS, &bit_chk,
2899 0)) {
2900 /* Set current end of the window. */
2901 *end_curr = search_dm ? -d : d;
2902
2903 /*
2904 * If a starting edge of our window has not been seen
2905 * this is our current start of the DM window.
2906 */
Marek Vasut160695d2015-08-02 19:10:58 +02002907 if (*bgn_curr == iocfg->io_out1_delay_max + 1)
Marek Vasutc8570af2015-07-21 05:26:58 +02002908 *bgn_curr = search_dm ? -d : d;
2909
2910 /*
2911 * If current window is bigger than best seen.
2912 * Set best seen to be current window.
2913 */
2914 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2915 *win_best = *end_curr - *bgn_curr + 1;
2916 *bgn_best = *bgn_curr;
2917 *end_best = *end_curr;
2918 }
2919 } else {
2920 /* We just saw a failing test. Reset temp edge. */
Marek Vasut160695d2015-08-02 19:10:58 +02002921 *bgn_curr = iocfg->io_out1_delay_max + 1;
2922 *end_curr = iocfg->io_out1_delay_max + 1;
Marek Vasutc8570af2015-07-21 05:26:58 +02002923
2924 /* Early exit is only applicable to DQS. */
2925 if (search_dm)
2926 continue;
2927
2928 /*
2929 * Early exit optimization: if the remaining delay
2930 * chain space is less than already seen largest
2931 * window we can exit.
2932 */
Marek Vasut160695d2015-08-02 19:10:58 +02002933 if (*win_best - 1 > iocfg->io_out1_delay_max - new_dqs - d)
Marek Vasutc8570af2015-07-21 05:26:58 +02002934 break;
2935 }
2936 }
2937}
2938
Dinh Nguyen3da42852015-06-02 22:52:49 -05002939/*
Marek Vasuta386a502015-07-21 05:33:49 +02002940 * rw_mgr_mem_calibrate_writes_center() - Center all windows
2941 * @rank_bgn: Rank number
2942 * @write_group: Write group
2943 * @test_bgn: Rank at which the test begins
2944 *
2945 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen3da42852015-06-02 22:52:49 -05002946 * certain windows.
2947 */
Marek Vasut3b44f552015-07-21 05:00:42 +02002948static int
2949rw_mgr_mem_calibrate_writes_center(const u32 rank_bgn, const u32 write_group,
2950 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05002951{
Marek Vasutc8570af2015-07-21 05:26:58 +02002952 int i;
Marek Vasut3b44f552015-07-21 05:00:42 +02002953 u32 sticky_bit_chk;
2954 u32 min_index;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002955 int left_edge[rwcfg->mem_dq_per_write_dqs];
2956 int right_edge[rwcfg->mem_dq_per_write_dqs];
Marek Vasut3b44f552015-07-21 05:00:42 +02002957 int mid;
2958 int mid_min, orig_mid_min;
2959 int new_dqs, start_dqs;
2960 int dq_margin, dqs_margin, dm_margin;
Marek Vasut160695d2015-08-02 19:10:58 +02002961 int bgn_curr = iocfg->io_out1_delay_max + 1;
2962 int end_curr = iocfg->io_out1_delay_max + 1;
2963 int bgn_best = iocfg->io_out1_delay_max + 1;
2964 int end_best = iocfg->io_out1_delay_max + 1;
Marek Vasut3b44f552015-07-21 05:00:42 +02002965 int win_best = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002966
Marek Vasutc4907892015-07-13 02:11:02 +02002967 int ret;
2968
Dinh Nguyen3da42852015-06-02 22:52:49 -05002969 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2970
2971 dm_margin = 0;
2972
Marek Vasutc6540872015-07-21 05:29:05 +02002973 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
2974 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002975 (rwcfg->mem_dq_per_write_dqs << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05002976
Marek Vasut3b44f552015-07-21 05:00:42 +02002977 /* Per-bit deskew. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05002978
2979 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02002980 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02002981 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05002982 */
2983 sticky_bit_chk = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02002984 for (i = 0; i < rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasut160695d2015-08-02 19:10:58 +02002985 left_edge[i] = iocfg->io_out1_delay_max + 1;
2986 right_edge[i] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05002987 }
2988
Marek Vasut3b44f552015-07-21 05:00:42 +02002989 /* Search for the left edge of the window for each bit. */
Marek Vasut71120772015-07-13 02:38:15 +02002990 search_left_edge(1, rank_bgn, write_group, 0, test_bgn,
Marek Vasut0c4be192015-07-18 20:34:00 +02002991 &sticky_bit_chk,
Marek Vasut71120772015-07-13 02:38:15 +02002992 left_edge, right_edge, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05002993
Marek Vasut3b44f552015-07-21 05:00:42 +02002994 /* Search for the right edge of the window for each bit. */
Marek Vasutc4907892015-07-13 02:11:02 +02002995 ret = search_right_edge(1, rank_bgn, write_group, 0,
2996 start_dqs, 0,
Marek Vasut0c4be192015-07-18 20:34:00 +02002997 &sticky_bit_chk,
Marek Vasutc4907892015-07-13 02:11:02 +02002998 left_edge, right_edge, 0);
2999 if (ret) {
3000 set_failing_group_stage(test_bgn + ret - 1, CAL_STAGE_WRITES,
3001 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutd043ee52015-07-21 05:32:49 +02003002 return -EINVAL;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003003 }
3004
Marek Vasutafb3eb82015-07-18 19:18:06 +02003005 min_index = get_window_mid_index(1, left_edge, right_edge, &mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003006
Marek Vasut3b44f552015-07-21 05:00:42 +02003007 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003008 orig_mid_min = mid_min;
3009 new_dqs = start_dqs;
3010 mid_min = 0;
Marek Vasutea9aa242016-04-04 21:21:05 +02003011 debug_cond(DLEVEL >= 1,
Marek Vasut3b44f552015-07-21 05:00:42 +02003012 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3013 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003014
Marek Vasutffb8b662015-07-18 19:46:26 +02003015 /* Add delay to bring centre of all DQ windows to the same "level". */
3016 center_dq_windows(1, left_edge, right_edge, mid_min, orig_mid_min,
3017 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003018
3019 /* Move DQS */
3020 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
Marek Vasut1273dd92015-07-12 21:05:08 +02003021 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003022
3023 /* Centre DM */
Marek Vasutea9aa242016-04-04 21:21:05 +02003024 debug_cond(DLEVEL >= 2, "%s:%d write_center: DM\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003025
3026 /*
Marek Vasut3b44f552015-07-21 05:00:42 +02003027 * Set the left and right edge of each bit to an illegal value.
Marek Vasut160695d2015-08-02 19:10:58 +02003028 * Use (iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003029 */
Marek Vasut160695d2015-08-02 19:10:58 +02003030 left_edge[0] = iocfg->io_out1_delay_max + 1;
3031 right_edge[0] = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003032
Marek Vasut3b44f552015-07-21 05:00:42 +02003033 /* Search for the/part of the window with DM shift. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003034 search_window(1, rank_bgn, write_group, &bgn_curr, &end_curr,
3035 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003036
Marek Vasut3b44f552015-07-21 05:00:42 +02003037 /* Reset DM delay chains to 0. */
Marek Vasut32675242015-07-17 06:07:13 +02003038 scc_mgr_apply_group_dm_out1_delay(0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003039
3040 /*
3041 * Check to see if the current window nudges up aganist 0 delay.
3042 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasut3b44f552015-07-21 05:00:42 +02003043 * search begins as a new search.
3044 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003045 if (end_curr != 0) {
Marek Vasut160695d2015-08-02 19:10:58 +02003046 bgn_curr = iocfg->io_out1_delay_max + 1;
3047 end_curr = iocfg->io_out1_delay_max + 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003048 }
3049
Marek Vasut3b44f552015-07-21 05:00:42 +02003050 /* Search for the/part of the window with DQS shifts. */
Marek Vasutc8570af2015-07-21 05:26:58 +02003051 search_window(0, rank_bgn, write_group, &bgn_curr, &end_curr,
3052 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003053
Marek Vasut3b44f552015-07-21 05:00:42 +02003054 /* Assign left and right edge for cal and reporting. */
3055 left_edge[0] = -1 * bgn_best;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003056 right_edge[0] = end_best;
3057
Marek Vasutea9aa242016-04-04 21:21:05 +02003058 debug_cond(DLEVEL >= 2, "%s:%d dm_calib: left=%d right=%d\n",
Marek Vasut3b44f552015-07-21 05:00:42 +02003059 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003060
Marek Vasut3b44f552015-07-21 05:00:42 +02003061 /* Move DQS (back to orig). */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003062 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3063
3064 /* Move DM */
3065
Marek Vasut3b44f552015-07-21 05:00:42 +02003066 /* Find middle of window for the DM bit. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003067 mid = (left_edge[0] - right_edge[0]) / 2;
3068
Marek Vasut3b44f552015-07-21 05:00:42 +02003069 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003070 if (mid < 0)
3071 mid = 0;
3072
Marek Vasut3b44f552015-07-21 05:00:42 +02003073 /* dm_marign should fail if we never find a window. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003074 if (win_best == 0)
3075 dm_margin = -1;
3076 else
3077 dm_margin = left_edge[0] - mid;
3078
Marek Vasut32675242015-07-17 06:07:13 +02003079 scc_mgr_apply_group_dm_out1_delay(mid);
Marek Vasut1273dd92015-07-12 21:05:08 +02003080 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003081
Marek Vasutea9aa242016-04-04 21:21:05 +02003082 debug_cond(DLEVEL >= 2,
Marek Vasut3b44f552015-07-21 05:00:42 +02003083 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3084 __func__, __LINE__, left_edge[0], right_edge[0],
3085 mid, dm_margin);
3086 /* Export values. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003087 gbl->fom_out += dq_margin + dqs_margin;
3088
Marek Vasutea9aa242016-04-04 21:21:05 +02003089 debug_cond(DLEVEL >= 2,
Marek Vasut3b44f552015-07-21 05:00:42 +02003090 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3091 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003092
3093 /*
3094 * Do not remove this line as it makes sure all of our
3095 * decisions have been applied.
3096 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003097 writel(0, &sdr_scc_mgr->update);
Marek Vasut3b44f552015-07-21 05:00:42 +02003098
Marek Vasutd043ee52015-07-21 05:32:49 +02003099 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3100 return -EINVAL;
3101
3102 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003103}
3104
Marek Vasutdb3a6062015-07-18 07:23:25 +02003105/**
3106 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3107 * @rank_bgn: Rank number
3108 * @group: Read/Write Group
3109 * @test_bgn: Rank at which the test begins
3110 *
3111 * Stage 2: Write Calibration Part One.
3112 *
3113 * This function implements UniPHY calibration Stage 2, as explained in
3114 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3115 */
3116static int rw_mgr_mem_calibrate_writes(const u32 rank_bgn, const u32 group,
3117 const u32 test_bgn)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003118{
Marek Vasutdb3a6062015-07-18 07:23:25 +02003119 int ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003120
Marek Vasutdb3a6062015-07-18 07:23:25 +02003121 /* Update info for sims */
3122 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3123
3124 reg_file_set_group(group);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003125 reg_file_set_stage(CAL_STAGE_WRITES);
3126 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3127
Marek Vasutdb3a6062015-07-18 07:23:25 +02003128 ret = rw_mgr_mem_calibrate_writes_center(rank_bgn, group, test_bgn);
Marek Vasutd043ee52015-07-21 05:32:49 +02003129 if (ret)
Marek Vasutdb3a6062015-07-18 07:23:25 +02003130 set_failing_group_stage(group, CAL_STAGE_WRITES,
Dinh Nguyen3da42852015-06-02 22:52:49 -05003131 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003132
Marek Vasutd043ee52015-07-21 05:32:49 +02003133 return ret;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003134}
3135
Marek Vasut4b0ac262015-07-20 07:33:33 +02003136/**
3137 * mem_precharge_and_activate() - Precharge all banks and activate
3138 *
3139 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3140 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003141static void mem_precharge_and_activate(void)
3142{
Marek Vasut4b0ac262015-07-20 07:33:33 +02003143 int r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003144
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003145 for (r = 0; r < rwcfg->mem_number_of_ranks; r++) {
Marek Vasut4b0ac262015-07-20 07:33:33 +02003146 /* Set rank. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003147 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3148
Marek Vasut4b0ac262015-07-20 07:33:33 +02003149 /* Precharge all banks. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003150 writel(rwcfg->precharge_all, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003151 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003152
Marek Vasut1273dd92015-07-12 21:05:08 +02003153 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003154 writel(rwcfg->activate_0_and_1_wait1,
Marek Vasut139823e2015-08-02 19:47:01 +02003155 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003156
Marek Vasut1273dd92015-07-12 21:05:08 +02003157 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003158 writel(rwcfg->activate_0_and_1_wait2,
Marek Vasut139823e2015-08-02 19:47:01 +02003159 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003160
Marek Vasut4b0ac262015-07-20 07:33:33 +02003161 /* Activate rows. */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003162 writel(rwcfg->activate_0_and_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut1273dd92015-07-12 21:05:08 +02003163 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003164 }
3165}
3166
Marek Vasut16502a02015-07-17 01:57:41 +02003167/**
3168 * mem_init_latency() - Configure memory RLAT and WLAT settings
3169 *
3170 * Configure memory RLAT and WLAT parameters.
3171 */
3172static void mem_init_latency(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003173{
Marek Vasut16502a02015-07-17 01:57:41 +02003174 /*
3175 * For AV/CV, LFIFO is hardened and always runs at full rate
3176 * so max latency in AFI clocks, used here, is correspondingly
3177 * smaller.
3178 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003179 const u32 max_latency = (1 << misccfg->max_latency_count_width) - 1;
Marek Vasut16502a02015-07-17 01:57:41 +02003180 u32 rlat, wlat;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003181
3182 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut16502a02015-07-17 01:57:41 +02003183
3184 /*
3185 * Read in write latency.
3186 * WL for Hard PHY does not include additive latency.
3187 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003188 wlat = readl(&data_mgr->t_wl_add);
3189 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003190
Marek Vasut16502a02015-07-17 01:57:41 +02003191 gbl->rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003192
Marek Vasut16502a02015-07-17 01:57:41 +02003193 /* Read in readl latency. */
Marek Vasut1273dd92015-07-12 21:05:08 +02003194 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003195
Marek Vasut16502a02015-07-17 01:57:41 +02003196 /* Set a pretty high read latency initially. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003197 gbl->curr_read_lat = rlat + 16;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003198 if (gbl->curr_read_lat > max_latency)
3199 gbl->curr_read_lat = max_latency;
3200
Marek Vasut1273dd92015-07-12 21:05:08 +02003201 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003202
Marek Vasut16502a02015-07-17 01:57:41 +02003203 /* Advertise write latency. */
3204 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003205}
3206
Marek Vasut51cea0b2015-07-26 10:54:15 +02003207/**
3208 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3209 *
3210 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3211 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003212static void mem_skip_calibrate(void)
3213{
Marek Vasut5ded7322015-08-02 19:42:26 +02003214 u32 vfifo_offset;
3215 u32 i, j, r;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003216
3217 debug("%s:%d\n", __func__, __LINE__);
3218 /* Need to update every shadow register set used by the interface */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003219 for (r = 0; r < rwcfg->mem_number_of_ranks;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003220 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003221 /*
3222 * Set output phase alignment settings appropriate for
3223 * skip calibration.
3224 */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003225 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003226 scc_mgr_set_dqs_en_phase(i, 0);
Marek Vasut160695d2015-08-02 19:10:58 +02003227 if (iocfg->dll_chain_length == 6)
3228 scc_mgr_set_dqdqs_output_phase(i, 6);
3229 else
3230 scc_mgr_set_dqdqs_output_phase(i, 7);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003231 /*
3232 * Case:33398
3233 *
3234 * Write data arrives to the I/O two cycles before write
3235 * latency is reached (720 deg).
3236 * -> due to bit-slip in a/c bus
3237 * -> to allow board skew where dqs is longer than ck
3238 * -> how often can this happen!?
3239 * -> can claim back some ptaps for high freq
3240 * support if we can relax this, but i digress...
3241 *
3242 * The write_clk leads mem_ck by 90 deg
3243 * The minimum ptap of the OPA is 180 deg
3244 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3245 * The write_clk is always delayed by 2 ptaps
3246 *
3247 * Hence, to make DQS aligned to CK, we need to delay
3248 * DQS by:
Marek Vasut139823e2015-08-02 19:47:01 +02003249 * (720 - 90 - 180 - 2) *
3250 * (360 / iocfg->dll_chain_length)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003251 *
Marek Vasut160695d2015-08-02 19:10:58 +02003252 * Dividing the above by (360 / iocfg->dll_chain_length)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003253 * gives us the number of ptaps, which simplies to:
3254 *
Marek Vasut160695d2015-08-02 19:10:58 +02003255 * (1.25 * iocfg->dll_chain_length - 2)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003256 */
Marek Vasut51cea0b2015-07-26 10:54:15 +02003257 scc_mgr_set_dqdqs_output_phase(i,
Marek Vasut6d7a3332015-08-10 22:50:11 +02003258 ((125 * iocfg->dll_chain_length) / 100) - 2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003259 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003260 writel(0xff, &sdr_scc_mgr->dqs_ena);
3261 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003262
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003263 for (i = 0; i < rwcfg->mem_if_write_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003264 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3265 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003266 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003267 writel(0xff, &sdr_scc_mgr->dq_ena);
3268 writel(0xff, &sdr_scc_mgr->dm_ena);
3269 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003270 }
3271
3272 /* Compensate for simulation model behaviour */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003273 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen3da42852015-06-02 22:52:49 -05003274 scc_mgr_set_dqs_bus_in_delay(i, 10);
3275 scc_mgr_load_dqs(i);
3276 }
Marek Vasut1273dd92015-07-12 21:05:08 +02003277 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003278
3279 /*
3280 * ArriaV has hard FIFOs that can only be initialized by incrementing
3281 * in sequencer.
3282 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003283 vfifo_offset = misccfg->calib_vfifo_offset;
Marek Vasut51cea0b2015-07-26 10:54:15 +02003284 for (j = 0; j < vfifo_offset; j++)
Marek Vasut1273dd92015-07-12 21:05:08 +02003285 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasut1273dd92015-07-12 21:05:08 +02003286 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003287
3288 /*
Marek Vasut51cea0b2015-07-26 10:54:15 +02003289 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3290 * setting from generation-time constant.
Dinh Nguyen3da42852015-06-02 22:52:49 -05003291 */
Marek Vasut96fd4362015-08-02 19:26:55 +02003292 gbl->curr_read_lat = misccfg->calib_lfifo_offset;
Marek Vasut1273dd92015-07-12 21:05:08 +02003293 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003294}
3295
Marek Vasut3589fbf2015-07-20 04:34:51 +02003296/**
3297 * mem_calibrate() - Memory calibration entry point.
3298 *
3299 * Perform memory calibration.
3300 */
Marek Vasut5ded7322015-08-02 19:42:26 +02003301static u32 mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003302{
Marek Vasut5ded7322015-08-02 19:42:26 +02003303 u32 i;
3304 u32 rank_bgn, sr;
3305 u32 write_group, write_test_bgn;
3306 u32 read_group, read_test_bgn;
3307 u32 run_groups, current_run;
3308 u32 failing_groups = 0;
3309 u32 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003310
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003311 const u32 rwdqs_ratio = rwcfg->mem_if_read_dqs_width /
3312 rwcfg->mem_if_write_dqs_width;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003313
Dinh Nguyen3da42852015-06-02 22:52:49 -05003314 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003315
Marek Vasut16502a02015-07-17 01:57:41 +02003316 /* Initialize the data settings */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003317 gbl->error_substage = CAL_SUBSTAGE_NIL;
3318 gbl->error_stage = CAL_STAGE_NIL;
3319 gbl->error_group = 0xff;
3320 gbl->fom_in = 0;
3321 gbl->fom_out = 0;
3322
Marek Vasut16502a02015-07-17 01:57:41 +02003323 /* Initialize WLAT and RLAT. */
3324 mem_init_latency();
3325
3326 /* Initialize bit slips. */
3327 mem_precharge_and_activate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003328
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003329 for (i = 0; i < rwcfg->mem_if_read_dqs_width; i++) {
Marek Vasut1273dd92015-07-12 21:05:08 +02003330 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3331 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutfa5d8212015-07-19 01:34:43 +02003332 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3333 if (i == 0)
3334 scc_mgr_set_hhp_extras();
3335
Marek Vasutc5c5f532015-07-17 02:06:20 +02003336 scc_set_bypass_mode(i);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003337 }
3338
Marek Vasut722c9682015-07-17 02:07:12 +02003339 /* Calibration is skipped. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003340 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3341 /*
3342 * Set VFIFO and LFIFO to instant-on settings in skip
3343 * calibration mode.
3344 */
3345 mem_skip_calibrate();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003346
Marek Vasut722c9682015-07-17 02:07:12 +02003347 /*
3348 * Do not remove this line as it makes sure all of our
3349 * decisions have been applied.
3350 */
3351 writel(0, &sdr_scc_mgr->update);
3352 return 1;
3353 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003354
Marek Vasut722c9682015-07-17 02:07:12 +02003355 /* Calibration is not skipped. */
3356 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3357 /*
3358 * Zero all delay chain/phase settings for all
3359 * groups and all shadow register sets.
3360 */
3361 scc_mgr_zero_all();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003362
Marek Vasutf085ac32015-08-02 18:27:21 +02003363 run_groups = ~0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003364
Marek Vasut722c9682015-07-17 02:07:12 +02003365 for (write_group = 0, write_test_bgn = 0; write_group
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003366 < rwcfg->mem_if_write_dqs_width; write_group++,
3367 write_test_bgn += rwcfg->mem_dq_per_write_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003368 /* Initialize the group failure */
Marek Vasut722c9682015-07-17 02:07:12 +02003369 group_failed = 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003370
Marek Vasut722c9682015-07-17 02:07:12 +02003371 current_run = run_groups & ((1 <<
3372 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3373 run_groups = run_groups >>
3374 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003375
Marek Vasut722c9682015-07-17 02:07:12 +02003376 if (current_run == 0)
3377 continue;
3378
3379 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3380 SCC_MGR_GROUP_COUNTER_OFFSET);
3381 scc_mgr_zero_group(write_group, 0);
3382
Marek Vasut33c42bb2015-07-17 02:21:47 +02003383 for (read_group = write_group * rwdqs_ratio,
3384 read_test_bgn = 0;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003385 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasut33c42bb2015-07-17 02:21:47 +02003386 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003387 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasut33c42bb2015-07-17 02:21:47 +02003388 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3389 continue;
Marek Vasut722c9682015-07-17 02:07:12 +02003390
Marek Vasut33c42bb2015-07-17 02:21:47 +02003391 /* Calibrate the VFIFO */
3392 if (rw_mgr_mem_calibrate_vfifo(read_group,
3393 read_test_bgn))
3394 continue;
3395
Marek Vasut139823e2015-08-02 19:47:01 +02003396 if (!(gbl->phy_debug_mode_flags &
3397 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003398 return 0;
3399
3400 /* The group failed, we're done. */
3401 goto grp_failed;
3402 }
3403
3404 /* Calibrate the output side */
3405 for (rank_bgn = 0, sr = 0;
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003406 rank_bgn < rwcfg->mem_number_of_ranks;
Marek Vasutc452dcd2015-07-17 02:50:56 +02003407 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3408 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3409 continue;
3410
3411 /* Not needed in quick mode! */
Marek Vasut139823e2015-08-02 19:47:01 +02003412 if (STATIC_CALIB_STEPS &
3413 CALIB_SKIP_DELAY_SWEEPS)
Marek Vasutc452dcd2015-07-17 02:50:56 +02003414 continue;
3415
Marek Vasutc452dcd2015-07-17 02:50:56 +02003416 /* Calibrate WRITEs */
Marek Vasutdb3a6062015-07-18 07:23:25 +02003417 if (!rw_mgr_mem_calibrate_writes(rank_bgn,
Marek Vasut139823e2015-08-02 19:47:01 +02003418 write_group,
3419 write_test_bgn))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003420 continue;
3421
Marek Vasut33c42bb2015-07-17 02:21:47 +02003422 group_failed = 1;
Marek Vasut139823e2015-08-02 19:47:01 +02003423 if (!(gbl->phy_debug_mode_flags &
3424 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasut33c42bb2015-07-17 02:21:47 +02003425 return 0;
Marek Vasut722c9682015-07-17 02:07:12 +02003426 }
3427
Marek Vasutc452dcd2015-07-17 02:50:56 +02003428 /* Some group failed, we're done. */
3429 if (group_failed)
3430 goto grp_failed;
Marek Vasut4ac21612015-07-17 02:31:04 +02003431
Marek Vasutc452dcd2015-07-17 02:50:56 +02003432 for (read_group = write_group * rwdqs_ratio,
3433 read_test_bgn = 0;
3434 read_group < (write_group + 1) * rwdqs_ratio;
3435 read_group++,
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003436 read_test_bgn += rwcfg->mem_dq_per_read_dqs) {
Marek Vasutc452dcd2015-07-17 02:50:56 +02003437 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3438 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003439
Marek Vasut78cdd7d2015-07-18 05:58:44 +02003440 if (!rw_mgr_mem_calibrate_vfifo_end(read_group,
Marek Vasut139823e2015-08-02 19:47:01 +02003441 read_test_bgn))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003442 continue;
Marek Vasut4ac21612015-07-17 02:31:04 +02003443
Marek Vasut139823e2015-08-02 19:47:01 +02003444 if (!(gbl->phy_debug_mode_flags &
3445 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasutc452dcd2015-07-17 02:50:56 +02003446 return 0;
Marek Vasut4ac21612015-07-17 02:31:04 +02003447
Marek Vasutc452dcd2015-07-17 02:50:56 +02003448 /* The group failed, we're done. */
3449 goto grp_failed;
Marek Vasut722c9682015-07-17 02:07:12 +02003450 }
3451
Marek Vasutc452dcd2015-07-17 02:50:56 +02003452 /* No group failed, continue as usual. */
3453 continue;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003454
Marek Vasutc452dcd2015-07-17 02:50:56 +02003455grp_failed: /* A group failed, increment the counter. */
3456 failing_groups++;
Marek Vasut722c9682015-07-17 02:07:12 +02003457 }
Dinh Nguyen3da42852015-06-02 22:52:49 -05003458
Marek Vasut722c9682015-07-17 02:07:12 +02003459 /*
3460 * USER If there are any failing groups then report
3461 * the failure.
3462 */
3463 if (failing_groups != 0)
3464 return 0;
3465
Marek Vasutc50ae302015-07-17 02:40:21 +02003466 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3467 continue;
3468
Marek Vasut722c9682015-07-17 02:07:12 +02003469 /* Calibrate the LFIFO */
Marek Vasutc50ae302015-07-17 02:40:21 +02003470 if (!rw_mgr_mem_calibrate_lfifo())
3471 return 0;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003472 }
3473
3474 /*
3475 * Do not remove this line as it makes sure all of our decisions
3476 * have been applied.
3477 */
Marek Vasut1273dd92015-07-12 21:05:08 +02003478 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003479 return 1;
3480}
3481
Marek Vasut23a040c2015-07-17 01:20:21 +02003482/**
3483 * run_mem_calibrate() - Perform memory calibration
3484 *
3485 * This function triggers the entire memory calibration procedure.
3486 */
3487static int run_mem_calibrate(void)
Dinh Nguyen3da42852015-06-02 22:52:49 -05003488{
Marek Vasut23a040c2015-07-17 01:20:21 +02003489 int pass;
Marek Vasutbba77112016-04-05 23:41:56 +02003490 u32 ctrl_cfg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003491
3492 debug("%s:%d\n", __func__, __LINE__);
3493
3494 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasut1273dd92015-07-12 21:05:08 +02003495 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003496
Marek Vasut23a040c2015-07-17 01:20:21 +02003497 /* Stop tracking manager. */
Marek Vasutbba77112016-04-05 23:41:56 +02003498 ctrl_cfg = readl(&sdr_ctrl->ctrl_cfg);
3499 writel(ctrl_cfg & ~SDR_CTRLGRP_CTRLCFG_DQSTRKEN_MASK,
3500 &sdr_ctrl->ctrl_cfg);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003501
Marek Vasut9fa9c902015-07-17 01:12:07 +02003502 phy_mgr_initialize();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003503 rw_mgr_mem_initialize();
3504
Marek Vasut23a040c2015-07-17 01:20:21 +02003505 /* Perform the actual memory calibration. */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003506 pass = mem_calibrate();
3507
3508 mem_precharge_and_activate();
Marek Vasut1273dd92015-07-12 21:05:08 +02003509 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003510
Marek Vasut23a040c2015-07-17 01:20:21 +02003511 /* Handoff. */
3512 rw_mgr_mem_handoff();
Dinh Nguyen3da42852015-06-02 22:52:49 -05003513 /*
Marek Vasut23a040c2015-07-17 01:20:21 +02003514 * In Hard PHY this is a 2-bit control:
3515 * 0: AFI Mux Select
3516 * 1: DDIO Mux Select
Dinh Nguyen3da42852015-06-02 22:52:49 -05003517 */
Marek Vasut23a040c2015-07-17 01:20:21 +02003518 writel(0x2, &phy_mgr_cfg->mux_sel);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003519
Marek Vasut23a040c2015-07-17 01:20:21 +02003520 /* Start tracking manager. */
Marek Vasutbba77112016-04-05 23:41:56 +02003521 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Marek Vasut23a040c2015-07-17 01:20:21 +02003522
3523 return pass;
3524}
3525
3526/**
3527 * debug_mem_calibrate() - Report result of memory calibration
3528 * @pass: Value indicating whether calibration passed or failed
3529 *
3530 * This function reports the results of the memory calibration
3531 * and writes debug information into the register file.
3532 */
3533static void debug_mem_calibrate(int pass)
3534{
Marek Vasut5ded7322015-08-02 19:42:26 +02003535 u32 debug_info;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003536
3537 if (pass) {
3538 printf("%s: CALIBRATION PASSED\n", __FILE__);
3539
3540 gbl->fom_in /= 2;
3541 gbl->fom_out /= 2;
3542
3543 if (gbl->fom_in > 0xff)
3544 gbl->fom_in = 0xff;
3545
3546 if (gbl->fom_out > 0xff)
3547 gbl->fom_out = 0xff;
3548
3549 /* Update the FOM in the register file */
3550 debug_info = gbl->fom_in;
3551 debug_info |= gbl->fom_out << 8;
Marek Vasut1273dd92015-07-12 21:05:08 +02003552 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003553
Marek Vasut1273dd92015-07-12 21:05:08 +02003554 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3555 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003556 } else {
3557 printf("%s: CALIBRATION FAILED\n", __FILE__);
3558
3559 debug_info = gbl->error_stage;
3560 debug_info |= gbl->error_substage << 8;
3561 debug_info |= gbl->error_group << 16;
3562
Marek Vasut1273dd92015-07-12 21:05:08 +02003563 writel(debug_info, &sdr_reg_file->failing_stage);
3564 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3565 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003566
3567 /* Update the failing group/stage in the register file */
3568 debug_info = gbl->error_stage;
3569 debug_info |= gbl->error_substage << 8;
3570 debug_info |= gbl->error_group << 16;
Marek Vasut1273dd92015-07-12 21:05:08 +02003571 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003572 }
3573
Marek Vasut23a040c2015-07-17 01:20:21 +02003574 printf("%s: Calibration complete\n", __FILE__);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003575}
3576
Marek Vasutbb064342015-07-19 06:12:42 +02003577/**
3578 * hc_initialize_rom_data() - Initialize ROM data
3579 *
3580 * Initialize ROM data.
3581 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003582static void hc_initialize_rom_data(void)
3583{
Marek Vasut04955cf2015-08-02 17:15:19 +02003584 unsigned int nelem = 0;
3585 const u32 *rom_init;
Marek Vasutbb064342015-07-19 06:12:42 +02003586 u32 i, addr;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003587
Marek Vasut04955cf2015-08-02 17:15:19 +02003588 socfpga_get_seq_inst_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003589 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003590 for (i = 0; i < nelem; i++)
3591 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003592
Marek Vasut04955cf2015-08-02 17:15:19 +02003593 socfpga_get_seq_ac_init(&rom_init, &nelem);
Marek Vasutc4815f72015-07-12 19:03:33 +02003594 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasut04955cf2015-08-02 17:15:19 +02003595 for (i = 0; i < nelem; i++)
3596 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003597}
3598
Marek Vasut9c1ab2c2015-07-19 06:13:37 +02003599/**
3600 * initialize_reg_file() - Initialize SDR register file
3601 *
3602 * Initialize SDR register file.
3603 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003604static void initialize_reg_file(void)
3605{
Dinh Nguyen3da42852015-06-02 22:52:49 -05003606 /* Initialize the register file with the correct data */
Marek Vasut96fd4362015-08-02 19:26:55 +02003607 writel(misccfg->reg_file_init_seq_signature, &sdr_reg_file->signature);
Marek Vasut1273dd92015-07-12 21:05:08 +02003608 writel(0, &sdr_reg_file->debug_data_addr);
3609 writel(0, &sdr_reg_file->cur_stage);
3610 writel(0, &sdr_reg_file->fom);
3611 writel(0, &sdr_reg_file->failing_stage);
3612 writel(0, &sdr_reg_file->debug1);
3613 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003614}
3615
Marek Vasut2ca151f2015-07-19 06:14:04 +02003616/**
3617 * initialize_hps_phy() - Initialize HPS PHY
3618 *
3619 * Initialize HPS PHY.
3620 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003621static void initialize_hps_phy(void)
3622{
Marek Vasut5ded7322015-08-02 19:42:26 +02003623 u32 reg;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003624 /*
3625 * Tracking also gets configured here because it's in the
3626 * same register.
3627 */
Marek Vasut5ded7322015-08-02 19:42:26 +02003628 u32 trk_sample_count = 7500;
3629 u32 trk_long_idle_sample_count = (10 << 16) | 100;
Dinh Nguyen3da42852015-06-02 22:52:49 -05003630 /*
3631 * Format is number of outer loops in the 16 MSB, sample
3632 * count in 16 LSB.
3633 */
3634
3635 reg = 0;
3636 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3637 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3638 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3639 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3640 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3641 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3642 /*
3643 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3644 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3645 */
3646 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3647 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3648 trk_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003649 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003650
3651 reg = 0;
3652 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3653 trk_sample_count >>
3654 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3655 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3656 trk_long_idle_sample_count);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003657 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003658
3659 reg = 0;
3660 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3661 trk_long_idle_sample_count >>
3662 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasut6cb9f162015-07-12 20:49:39 +02003663 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003664}
3665
Marek Vasut880e46f2015-07-17 00:45:11 +02003666/**
3667 * initialize_tracking() - Initialize tracking
3668 *
3669 * Initialize the register file with usable initial data.
3670 */
Dinh Nguyen3da42852015-06-02 22:52:49 -05003671static void initialize_tracking(void)
3672{
Marek Vasut880e46f2015-07-17 00:45:11 +02003673 /*
3674 * Initialize the register file with the correct data.
3675 * Compute usable version of value in case we skip full
3676 * computation later.
3677 */
Marek Vasut139823e2015-08-02 19:47:01 +02003678 writel(DIV_ROUND_UP(iocfg->delay_per_opa_tap,
3679 iocfg->delay_per_dchain_tap) - 1,
Marek Vasut880e46f2015-07-17 00:45:11 +02003680 &sdr_reg_file->dtaps_per_ptap);
3681
3682 /* trk_sample_count */
3683 writel(7500, &sdr_reg_file->trk_sample_count);
3684
3685 /* longidle outer loop [15:0] */
3686 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003687
3688 /*
Marek Vasut880e46f2015-07-17 00:45:11 +02003689 * longidle sample count [31:24]
3690 * trfc, worst case of 933Mhz 4Gb [23:16]
3691 * trcd, worst case [15:8]
3692 * vfifo wait [7:0]
Dinh Nguyen3da42852015-06-02 22:52:49 -05003693 */
Marek Vasut880e46f2015-07-17 00:45:11 +02003694 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3695 &sdr_reg_file->delays);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003696
Marek Vasut880e46f2015-07-17 00:45:11 +02003697 /* mux delay */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003698 writel((rwcfg->idle << 24) | (rwcfg->activate_1 << 16) |
3699 (rwcfg->sgle_read << 8) | (rwcfg->precharge_all << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003700 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003701
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003702 writel(rwcfg->mem_if_read_dqs_width,
Marek Vasut880e46f2015-07-17 00:45:11 +02003703 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003704
Marek Vasut880e46f2015-07-17 00:45:11 +02003705 /* trefi [7:0] */
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003706 writel((rwcfg->refresh_all << 24) | (1000 << 0),
Marek Vasut880e46f2015-07-17 00:45:11 +02003707 &sdr_reg_file->trk_rfsh);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003708}
3709
3710int sdram_calibration_full(void)
3711{
3712 struct param_type my_param;
3713 struct gbl_type my_gbl;
Marek Vasut5ded7322015-08-02 19:42:26 +02003714 u32 pass;
Marek Vasut84e0b0c2015-07-17 01:05:36 +02003715
3716 memset(&my_param, 0, sizeof(my_param));
3717 memset(&my_gbl, 0, sizeof(my_gbl));
Dinh Nguyen3da42852015-06-02 22:52:49 -05003718
3719 param = &my_param;
3720 gbl = &my_gbl;
3721
Marek Vasutd718a262015-08-02 18:12:08 +02003722 rwcfg = socfpga_get_sdram_rwmgr_config();
Marek Vasut10c14262015-08-02 19:00:23 +02003723 iocfg = socfpga_get_sdram_io_config();
Marek Vasut042ff2d2015-08-02 19:18:47 +02003724 misccfg = socfpga_get_sdram_misc_config();
Marek Vasutd718a262015-08-02 18:12:08 +02003725
Dinh Nguyen3da42852015-06-02 22:52:49 -05003726 /* Set the calibration enabled by default */
3727 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3728 /*
3729 * Only sweep all groups (regardless of fail state) by default
3730 * Set enabled read test by default.
3731 */
3732#if DISABLE_GUARANTEED_READ
3733 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3734#endif
3735 /* Initialize the register file */
3736 initialize_reg_file();
3737
3738 /* Initialize any PHY CSR */
3739 initialize_hps_phy();
3740
3741 scc_mgr_initialize();
3742
3743 initialize_tracking();
3744
Dinh Nguyen3da42852015-06-02 22:52:49 -05003745 printf("%s: Preparing to start memory calibration\n", __FILE__);
3746
3747 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutea9aa242016-04-04 21:21:05 +02003748 debug_cond(DLEVEL >= 1,
Marek Vasut23f62b32015-07-13 01:05:27 +02003749 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003750 rwcfg->mem_number_of_ranks, rwcfg->mem_number_of_cs_per_dimm,
3751 rwcfg->mem_dq_per_read_dqs, rwcfg->mem_dq_per_write_dqs,
3752 rwcfg->mem_virtual_groups_per_read_dqs,
3753 rwcfg->mem_virtual_groups_per_write_dqs);
Marek Vasutea9aa242016-04-04 21:21:05 +02003754 debug_cond(DLEVEL >= 1,
Marek Vasut23f62b32015-07-13 01:05:27 +02003755 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
Marek Vasut1fa0c8c2015-08-02 18:44:06 +02003756 rwcfg->mem_if_read_dqs_width, rwcfg->mem_if_write_dqs_width,
3757 rwcfg->mem_data_width, rwcfg->mem_data_mask_width,
Marek Vasut160695d2015-08-02 19:10:58 +02003758 iocfg->delay_per_opa_tap, iocfg->delay_per_dchain_tap);
Marek Vasutea9aa242016-04-04 21:21:05 +02003759 debug_cond(DLEVEL >= 1, "dtap_dqsen_delay=%u, dll=%u",
Marek Vasut160695d2015-08-02 19:10:58 +02003760 iocfg->delay_per_dqs_en_dchain_tap, iocfg->dll_chain_length);
Marek Vasutea9aa242016-04-04 21:21:05 +02003761 debug_cond(DLEVEL >= 1,
Marek Vasut139823e2015-08-02 19:47:01 +02003762 "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003763 iocfg->dqs_en_phase_max, iocfg->dqdqs_out_phase_max,
3764 iocfg->dqs_en_delay_max, iocfg->dqs_in_delay_max);
Marek Vasutea9aa242016-04-04 21:21:05 +02003765 debug_cond(DLEVEL >= 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
Marek Vasut160695d2015-08-02 19:10:58 +02003766 iocfg->io_in_delay_max, iocfg->io_out1_delay_max,
3767 iocfg->io_out2_delay_max);
Marek Vasutea9aa242016-04-04 21:21:05 +02003768 debug_cond(DLEVEL >= 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
Marek Vasut160695d2015-08-02 19:10:58 +02003769 iocfg->dqs_in_reserve, iocfg->dqs_out_reserve);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003770
3771 hc_initialize_rom_data();
3772
3773 /* update info for sims */
3774 reg_file_set_stage(CAL_STAGE_NIL);
3775 reg_file_set_group(0);
3776
3777 /*
3778 * Load global needed for those actions that require
3779 * some dynamic calibration support.
3780 */
3781 dyn_calib_steps = STATIC_CALIB_STEPS;
3782 /*
3783 * Load global to allow dynamic selection of delay loop settings
3784 * based on calibration mode.
3785 */
3786 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3787 skip_delay_mask = 0xff;
3788 else
3789 skip_delay_mask = 0x0;
3790
3791 pass = run_mem_calibrate();
Marek Vasut23a040c2015-07-17 01:20:21 +02003792 debug_mem_calibrate(pass);
Dinh Nguyen3da42852015-06-02 22:52:49 -05003793 return pass;
3794}