blob: b85b56efe5d8fc4130b0967c84141004466cc6b5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002/*
3 * Copyright Altera Corporation (C) 2012-2015
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05004 */
5
6#include <common.h>
7#include <asm/io.h>
8#include <asm/arch/sdram.h>
Marek Vasut6ca5b962015-07-18 02:46:56 +02009#include <errno.h>
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050010#include "sequencer.h"
Marek Vasut662a8a62015-08-02 16:55:45 +020011
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020012static const struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
Marek Vasutc85b9b32015-08-02 19:47:01 +020013 (struct socfpga_sdr_rw_load_manager *)
14 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020015static const struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs
16 = (struct socfpga_sdr_rw_load_jump_manager *)
Marek Vasutc85b9b32015-08-02 19:47:01 +020017 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020018static const struct socfpga_sdr_reg_file *sdr_reg_file =
Marek Vasut341ceec2015-07-12 18:31:05 +020019 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020020static const struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
Marek Vasutc85b9b32015-08-02 19:47:01 +020021 (struct socfpga_sdr_scc_mgr *)
22 (SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020023static const struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
Marek Vasutc3b9b0f2015-07-12 18:54:37 +020024 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020025static const struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
Marek Vasutc85b9b32015-08-02 19:47:01 +020026 (struct socfpga_phy_mgr_cfg *)
27 (SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020028static const struct socfpga_data_mgr *data_mgr =
Marek Vasuta3340102015-07-12 19:03:33 +020029 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020030static const struct socfpga_sdr_ctrl *sdr_ctrl =
Marek Vasutcd5d38e2015-07-12 20:49:39 +020031 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
32
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050033#define DELTA_D 1
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050034
35/*
36 * In order to reduce ROM size, most of the selectable calibration steps are
37 * decided at compile time based on the user's calibration mode selection,
38 * as captured by the STATIC_CALIB_STEPS selection below.
39 *
40 * However, to support simulation-time selection of fast simulation mode, where
41 * we skip everything except the bare minimum, we need a few of the steps to
42 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
43 * check, which is based on the rtl-supplied value, or we dynamically compute
44 * the value to use based on the dynamically-chosen calibration mode
45 */
46
47#define DLEVEL 0
48#define STATIC_IN_RTL_SIM 0
49#define STATIC_SKIP_DELAY_LOOPS 0
50
51#define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
52 STATIC_SKIP_DELAY_LOOPS)
53
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050054#define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020055 ((non_skip_value) & seq->skip_delay_mask)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050056
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020057static void set_failing_group_stage(struct socfpga_sdrseq *seq,
58 u32 group, u32 stage, u32 substage)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050059{
60 /*
61 * Only set the global stage if there was not been any other
62 * failing group
63 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020064 if (seq->gbl.error_stage == CAL_STAGE_NIL) {
65 seq->gbl.error_substage = substage;
66 seq->gbl.error_stage = stage;
67 seq->gbl.error_group = group;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050068 }
69}
70
Marek Vasut6eeb7472015-07-12 21:10:24 +020071static void reg_file_set_group(u16 set_group)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050072{
Marek Vasut6eeb7472015-07-12 21:10:24 +020073 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050074}
75
Marek Vasut6eeb7472015-07-12 21:10:24 +020076static void reg_file_set_stage(u8 set_stage)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050077{
Marek Vasut6eeb7472015-07-12 21:10:24 +020078 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050079}
80
Marek Vasut6eeb7472015-07-12 21:10:24 +020081static void reg_file_set_sub_stage(u8 set_sub_stage)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050082{
Marek Vasut6eeb7472015-07-12 21:10:24 +020083 set_sub_stage &= 0xff;
84 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050085}
86
Marek Vasutfe5aa452015-07-17 01:36:32 +020087/**
88 * phy_mgr_initialize() - Initialize PHY Manager
89 *
90 * Initialize PHY Manager.
91 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +020092static void phy_mgr_initialize(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050093{
Marek Vasutfe5aa452015-07-17 01:36:32 +020094 u32 ratio;
95
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050096 debug("%s:%d\n", __func__, __LINE__);
Marek Vasutfe5aa452015-07-17 01:36:32 +020097 /* Calibration has control over path to memory */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -050098 /*
99 * In Hard PHY this is a 2-bit control:
100 * 0: AFI Mux Select
101 * 1: DDIO Mux Select
102 */
Marek Vasutb5450962015-07-12 21:05:08 +0200103 writel(0x3, &phy_mgr_cfg->mux_sel);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500104
105 /* USER memory clock is not stable we begin initialization */
Marek Vasutb5450962015-07-12 21:05:08 +0200106 writel(0, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500107
108 /* USER calibration status all set to zero */
Marek Vasutb5450962015-07-12 21:05:08 +0200109 writel(0, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500110
Marek Vasutb5450962015-07-12 21:05:08 +0200111 writel(0, &phy_mgr_cfg->cal_debug_info);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500112
Marek Vasutfe5aa452015-07-17 01:36:32 +0200113 /* Init params only if we do NOT skip calibration. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200114 if ((seq->dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
Marek Vasutfe5aa452015-07-17 01:36:32 +0200115 return;
116
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200117 ratio = seq->rwcfg->mem_dq_per_read_dqs /
118 seq->rwcfg->mem_virtual_groups_per_read_dqs;
119 seq->param.read_correct_mask_vg = (1 << ratio) - 1;
120 seq->param.write_correct_mask_vg = (1 << ratio) - 1;
121 seq->param.read_correct_mask = (1 << seq->rwcfg->mem_dq_per_read_dqs)
122 - 1;
123 seq->param.write_correct_mask = (1 << seq->rwcfg->mem_dq_per_write_dqs)
124 - 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500125}
126
Marek Vasut575029d2015-07-20 08:15:57 +0200127/**
128 * set_rank_and_odt_mask() - Set Rank and ODT mask
129 * @rank: Rank mask
130 * @odt_mode: ODT mode, OFF or READ_WRITE
131 *
132 * Set Rank and ODT mask (On-Die Termination).
133 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200134static void set_rank_and_odt_mask(struct socfpga_sdrseq *seq,
135 const u32 rank, const u32 odt_mode)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500136{
Marek Vasut0b5e2572015-07-20 08:03:11 +0200137 u32 odt_mask_0 = 0;
138 u32 odt_mask_1 = 0;
139 u32 cs_and_odt_mask;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500140
Marek Vasut0b5e2572015-07-20 08:03:11 +0200141 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
142 odt_mask_0 = 0x0;
143 odt_mask_1 = 0x0;
144 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200145 switch (seq->rwcfg->mem_number_of_ranks) {
Marek Vasut92523082015-07-20 08:09:05 +0200146 case 1: /* 1 Rank */
147 /* Read: ODT = 0 ; Write: ODT = 1 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500148 odt_mask_0 = 0x0;
149 odt_mask_1 = 0x1;
Marek Vasut92523082015-07-20 08:09:05 +0200150 break;
151 case 2: /* 2 Ranks */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200152 if (seq->rwcfg->mem_number_of_cs_per_dimm == 1) {
Marek Vasut575029d2015-07-20 08:15:57 +0200153 /*
154 * - Dual-Slot , Single-Rank (1 CS per DIMM)
155 * OR
156 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
157 *
158 * Since MEM_NUMBER_OF_RANKS is 2, they
159 * are both single rank with 2 CS each
160 * (special for RDIMM).
161 *
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500162 * Read: Turn on ODT on the opposite rank
163 * Write: Turn on ODT on all ranks
164 */
165 odt_mask_0 = 0x3 & ~(1 << rank);
166 odt_mask_1 = 0x3;
167 } else {
168 /*
Marek Vasut575029d2015-07-20 08:15:57 +0200169 * - Single-Slot , Dual-Rank (2 CS per DIMM)
170 *
171 * Read: Turn on ODT off on all ranks
172 * Write: Turn on ODT on active rank
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500173 */
174 odt_mask_0 = 0x0;
175 odt_mask_1 = 0x3 & (1 << rank);
176 }
Marek Vasut92523082015-07-20 08:09:05 +0200177 break;
178 case 4: /* 4 Ranks */
179 /* Read:
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500180 * ----------+-----------------------+
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500181 * | ODT |
182 * Read From +-----------------------+
183 * Rank | 3 | 2 | 1 | 0 |
184 * ----------+-----+-----+-----+-----+
185 * 0 | 0 | 1 | 0 | 0 |
186 * 1 | 1 | 0 | 0 | 0 |
187 * 2 | 0 | 0 | 0 | 1 |
188 * 3 | 0 | 0 | 1 | 0 |
189 * ----------+-----+-----+-----+-----+
190 *
191 * Write:
192 * ----------+-----------------------+
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500193 * | ODT |
194 * Write To +-----------------------+
195 * Rank | 3 | 2 | 1 | 0 |
196 * ----------+-----+-----+-----+-----+
197 * 0 | 0 | 1 | 0 | 1 |
198 * 1 | 1 | 0 | 1 | 0 |
199 * 2 | 0 | 1 | 0 | 1 |
200 * 3 | 1 | 0 | 1 | 0 |
201 * ----------+-----+-----+-----+-----+
202 */
203 switch (rank) {
204 case 0:
205 odt_mask_0 = 0x4;
206 odt_mask_1 = 0x5;
207 break;
208 case 1:
209 odt_mask_0 = 0x8;
210 odt_mask_1 = 0xA;
211 break;
212 case 2:
213 odt_mask_0 = 0x1;
214 odt_mask_1 = 0x5;
215 break;
216 case 3:
217 odt_mask_0 = 0x2;
218 odt_mask_1 = 0xA;
219 break;
220 }
Marek Vasut92523082015-07-20 08:09:05 +0200221 break;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500222 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500223 }
224
Marek Vasut0b5e2572015-07-20 08:03:11 +0200225 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
226 ((0xFF & odt_mask_0) << 8) |
227 ((0xFF & odt_mask_1) << 16);
Marek Vasutb5450962015-07-12 21:05:08 +0200228 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
229 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500230}
231
Marek Vasut303a3dc2015-07-12 22:28:33 +0200232/**
233 * scc_mgr_set() - Set SCC Manager register
234 * @off: Base offset in SCC Manager space
235 * @grp: Read/Write group
236 * @val: Value to be set
237 *
238 * This function sets the SCC Manager (Scan Chain Control Manager) register.
239 */
240static void scc_mgr_set(u32 off, u32 grp, u32 val)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500241{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200242 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
243}
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500244
Marek Vasut8957b492015-07-20 07:16:42 +0200245/**
246 * scc_mgr_initialize() - Initialize SCC Manager registers
247 *
248 * Initialize SCC Manager registers.
249 */
Marek Vasut303a3dc2015-07-12 22:28:33 +0200250static void scc_mgr_initialize(void)
251{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500252 /*
Marek Vasut8957b492015-07-20 07:16:42 +0200253 * Clear register file for HPS. 16 (2^4) is the size of the
254 * full register file in the scc mgr:
255 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
256 * MEM_IF_READ_DQS_WIDTH - 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500257 */
Marek Vasut303a3dc2015-07-12 22:28:33 +0200258 int i;
Marek Vasut8957b492015-07-20 07:16:42 +0200259
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500260 for (i = 0; i < 16; i++) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +0200261 debug_cond(DLEVEL >= 1, "%s:%d: Clearing SCC RFILE index %u\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500262 __func__, __LINE__, i);
Marek Vasut45ce2962016-04-04 17:28:16 +0200263 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500264 }
265}
266
Marek Vasut8af9ca02015-08-02 19:42:26 +0200267static void scc_mgr_set_dqdqs_output_phase(u32 write_group, u32 phase)
Marek Vasut7481b692015-07-12 22:11:55 +0200268{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200269 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
Marek Vasut7481b692015-07-12 22:11:55 +0200270}
271
Marek Vasut8af9ca02015-08-02 19:42:26 +0200272static void scc_mgr_set_dqs_bus_in_delay(u32 read_group, u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500273{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200274 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500275}
276
Marek Vasut8af9ca02015-08-02 19:42:26 +0200277static void scc_mgr_set_dqs_en_phase(u32 read_group, u32 phase)
Marek Vasut7481b692015-07-12 22:11:55 +0200278{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200279 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
Marek Vasut7481b692015-07-12 22:11:55 +0200280}
281
Marek Vasut8af9ca02015-08-02 19:42:26 +0200282static void scc_mgr_set_dqs_en_delay(u32 read_group, u32 delay)
Marek Vasut7481b692015-07-12 22:11:55 +0200283{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200284 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200285}
286
Marek Vasutf4d38622016-04-04 21:16:18 +0200287static void scc_mgr_set_dq_in_delay(u32 dq_in_group, u32 delay)
288{
289 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
290}
291
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200292static void scc_mgr_set_dqs_io_in_delay(struct socfpga_sdrseq *seq,
293 u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500294{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200295 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
296 seq->rwcfg->mem_dq_per_write_dqs, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500297}
298
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200299static void scc_mgr_set_dm_in_delay(struct socfpga_sdrseq *seq, u32 dm,
300 u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500301{
Marek Vasutf4d38622016-04-04 21:16:18 +0200302 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200303 seq->rwcfg->mem_dq_per_write_dqs + 1 + dm,
Marek Vasutf4d38622016-04-04 21:16:18 +0200304 delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200305}
306
Marek Vasut8af9ca02015-08-02 19:42:26 +0200307static void scc_mgr_set_dq_out1_delay(u32 dq_in_group, u32 delay)
Marek Vasut7481b692015-07-12 22:11:55 +0200308{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200309 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200310}
311
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200312static void scc_mgr_set_dqs_out1_delay(struct socfpga_sdrseq *seq,
313 u32 delay)
Marek Vasut7481b692015-07-12 22:11:55 +0200314{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200315 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
316 seq->rwcfg->mem_dq_per_write_dqs, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500317}
318
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200319static void scc_mgr_set_dm_out1_delay(struct socfpga_sdrseq *seq, u32 dm,
320 u32 delay)
Marek Vasut7481b692015-07-12 22:11:55 +0200321{
Marek Vasut303a3dc2015-07-12 22:28:33 +0200322 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200323 seq->rwcfg->mem_dq_per_write_dqs + 1 + dm,
Marek Vasut303a3dc2015-07-12 22:28:33 +0200324 delay);
Marek Vasut7481b692015-07-12 22:11:55 +0200325}
326
327/* load up dqs config settings */
Marek Vasut8af9ca02015-08-02 19:42:26 +0200328static void scc_mgr_load_dqs(u32 dqs)
Marek Vasut7481b692015-07-12 22:11:55 +0200329{
330 writel(dqs, &sdr_scc_mgr->dqs_ena);
331}
332
333/* load up dqs io config settings */
334static void scc_mgr_load_dqs_io(void)
335{
336 writel(0, &sdr_scc_mgr->dqs_io_ena);
337}
338
339/* load up dq config settings */
Marek Vasut8af9ca02015-08-02 19:42:26 +0200340static void scc_mgr_load_dq(u32 dq_in_group)
Marek Vasut7481b692015-07-12 22:11:55 +0200341{
342 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
343}
344
345/* load up dm config settings */
Marek Vasut8af9ca02015-08-02 19:42:26 +0200346static void scc_mgr_load_dm(u32 dm)
Marek Vasut7481b692015-07-12 22:11:55 +0200347{
348 writel(dm, &sdr_scc_mgr->dm_ena);
349}
350
Marek Vasut1d3cde32015-07-12 23:25:21 +0200351/**
352 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
353 * @off: Base offset in SCC Manager space
354 * @grp: Read/Write group
355 * @val: Value to be set
356 * @update: If non-zero, trigger SCC Manager update for all ranks
357 *
358 * This function sets the SCC Manager (Scan Chain Control Manager) register
359 * and optionally triggers the SCC update for all ranks.
360 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200361static void scc_mgr_set_all_ranks(struct socfpga_sdrseq *seq,
362 const u32 off, const u32 grp, const u32 val,
Marek Vasut1d3cde32015-07-12 23:25:21 +0200363 const int update)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500364{
Marek Vasut1d3cde32015-07-12 23:25:21 +0200365 u32 r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500366
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200367 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500368 r += NUM_RANKS_PER_SHADOW_REG) {
Marek Vasut1d3cde32015-07-12 23:25:21 +0200369 scc_mgr_set(off, grp, val);
Marek Vasut49722822015-07-12 23:14:33 +0200370
Marek Vasut1d3cde32015-07-12 23:25:21 +0200371 if (update || (r == 0)) {
372 writel(grp, &sdr_scc_mgr->dqs_ena);
Marek Vasutb5450962015-07-12 21:05:08 +0200373 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500374 }
375 }
376}
377
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200378static void scc_mgr_set_dqs_en_phase_all_ranks(struct socfpga_sdrseq *seq,
379 u32 read_group, u32 phase)
Marek Vasut1d3cde32015-07-12 23:25:21 +0200380{
381 /*
382 * USER although the h/w doesn't support different phases per
383 * shadow register, for simplicity our scc manager modeling
384 * keeps different phase settings per shadow reg, and it's
385 * important for us to keep them in sync to match h/w.
386 * for efficiency, the scan chain update should occur only
387 * once to sr0.
388 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200389 scc_mgr_set_all_ranks(seq, SCC_MGR_DQS_EN_PHASE_OFFSET,
Marek Vasut1d3cde32015-07-12 23:25:21 +0200390 read_group, phase, 0);
391}
392
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200393static void scc_mgr_set_dqdqs_output_phase_all_ranks(struct socfpga_sdrseq *seq,
394 u32 write_group, u32 phase)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500395{
Marek Vasut1d3cde32015-07-12 23:25:21 +0200396 /*
397 * USER although the h/w doesn't support different phases per
398 * shadow register, for simplicity our scc manager modeling
399 * keeps different phase settings per shadow reg, and it's
400 * important for us to keep them in sync to match h/w.
401 * for efficiency, the scan chain update should occur only
402 * once to sr0.
403 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200404 scc_mgr_set_all_ranks(seq, SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
Marek Vasut1d3cde32015-07-12 23:25:21 +0200405 write_group, phase, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500406}
407
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200408static void scc_mgr_set_dqs_en_delay_all_ranks(struct socfpga_sdrseq *seq,
409 u32 read_group, u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500410{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500411 /*
412 * In shadow register mode, the T11 settings are stored in
413 * registers in the core, which are updated by the DQS_ENA
414 * signals. Not issuing the SCC_MGR_UPD command allows us to
415 * save lots of rank switching overhead, by calling
416 * select_shadow_regs_for_update with update_scan_chains
417 * set to 0.
418 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200419 scc_mgr_set_all_ranks(seq, SCC_MGR_DQS_EN_DELAY_OFFSET,
Marek Vasut1d3cde32015-07-12 23:25:21 +0200420 read_group, delay, 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500421}
422
Marek Vasute62f6912015-07-12 23:39:06 +0200423/**
424 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
425 * @write_group: Write group
426 * @delay: Delay value
427 *
428 * This function sets the OCT output delay in SCC manager.
429 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200430static void scc_mgr_set_oct_out1_delay(struct socfpga_sdrseq *seq,
431 const u32 write_group, const u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500432{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200433 const int ratio = seq->rwcfg->mem_if_read_dqs_width /
434 seq->rwcfg->mem_if_write_dqs_width;
Marek Vasute62f6912015-07-12 23:39:06 +0200435 const int base = write_group * ratio;
436 int i;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500437 /*
438 * Load the setting in the SCC manager
439 * Although OCT affects only write data, the OCT delay is controlled
440 * by the DQS logic block which is instantiated once per read group.
441 * For protocols where a write group consists of multiple read groups,
442 * the setting must be set multiple times.
443 */
Marek Vasute62f6912015-07-12 23:39:06 +0200444 for (i = 0; i < ratio; i++)
445 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500446}
447
Marek Vasut3b8e5b02015-07-19 01:32:55 +0200448/**
449 * scc_mgr_set_hhp_extras() - Set HHP extras.
450 *
451 * Load the fixed setting in the SCC manager HHP extras.
452 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500453static void scc_mgr_set_hhp_extras(void)
454{
455 /*
456 * Load the fixed setting in the SCC manager
Marek Vasut3b8e5b02015-07-19 01:32:55 +0200457 * bits: 0:0 = 1'b1 - DQS bypass
458 * bits: 1:1 = 1'b1 - DQ bypass
459 * bits: 4:2 = 3'b001 - rfifo_mode
460 * bits: 6:5 = 2'b01 - rfifo clock_select
461 * bits: 7:7 = 1'b0 - separate gating from ungating setting
462 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500463 */
Marek Vasut3b8e5b02015-07-19 01:32:55 +0200464 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
465 (1 << 2) | (1 << 1) | (1 << 0);
466 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
467 SCC_MGR_HHP_GLOBALS_OFFSET |
468 SCC_MGR_HHP_EXTRAS_OFFSET;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500469
Marek Vasut4df2d7b2016-04-04 21:21:05 +0200470 debug_cond(DLEVEL >= 1, "%s:%d Setting HHP Extras\n",
Marek Vasut3b8e5b02015-07-19 01:32:55 +0200471 __func__, __LINE__);
472 writel(value, addr);
Marek Vasut4df2d7b2016-04-04 21:21:05 +0200473 debug_cond(DLEVEL >= 1, "%s:%d Done Setting HHP Extras\n",
Marek Vasut3b8e5b02015-07-19 01:32:55 +0200474 __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500475}
476
Marek Vasut08bcb982015-07-20 04:41:53 +0200477/**
478 * scc_mgr_zero_all() - Zero all DQS config
479 *
480 * Zero all DQS config.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500481 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200482static void scc_mgr_zero_all(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500483{
Marek Vasut08bcb982015-07-20 04:41:53 +0200484 int i, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500485
486 /*
487 * USER Zero all DQS config settings, across all groups and all
488 * shadow registers
489 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200490 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut08bcb982015-07-20 04:41:53 +0200491 r += NUM_RANKS_PER_SHADOW_REG) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200492 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500493 /*
494 * The phases actually don't exist on a per-rank basis,
495 * but there's no harm updating them several times, so
496 * let's keep the code simple.
497 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200498 scc_mgr_set_dqs_bus_in_delay(i,
499 seq->iocfg->dqs_in_reserve
500 );
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500501 scc_mgr_set_dqs_en_phase(i, 0);
502 scc_mgr_set_dqs_en_delay(i, 0);
503 }
504
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200505 for (i = 0; i < seq->rwcfg->mem_if_write_dqs_width; i++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500506 scc_mgr_set_dqdqs_output_phase(i, 0);
Marek Vasut08bcb982015-07-20 04:41:53 +0200507 /* Arria V/Cyclone V don't have out2. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200508 scc_mgr_set_oct_out1_delay(seq, i,
509 seq->iocfg->dqs_out_reserve);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500510 }
511 }
512
Marek Vasut08bcb982015-07-20 04:41:53 +0200513 /* Multicast to all DQS group enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200514 writel(0xff, &sdr_scc_mgr->dqs_ena);
515 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500516}
517
Marek Vasut0341de42015-07-17 02:06:20 +0200518/**
519 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
520 * @write_group: Write group
521 *
522 * Set bypass mode and trigger SCC update.
523 */
524static void scc_set_bypass_mode(const u32 write_group)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500525{
Marek Vasut0341de42015-07-17 02:06:20 +0200526 /* Multicast to all DQ enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200527 writel(0xff, &sdr_scc_mgr->dq_ena);
528 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500529
Marek Vasut0341de42015-07-17 02:06:20 +0200530 /* Update current DQS IO enable. */
Marek Vasutb5450962015-07-12 21:05:08 +0200531 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500532
Marek Vasut0341de42015-07-17 02:06:20 +0200533 /* Update the DQS logic. */
Marek Vasutb5450962015-07-12 21:05:08 +0200534 writel(write_group, &sdr_scc_mgr->dqs_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500535
Marek Vasut0341de42015-07-17 02:06:20 +0200536 /* Hit update. */
Marek Vasutb5450962015-07-12 21:05:08 +0200537 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500538}
539
Marek Vasut5a4379e2015-07-13 00:30:09 +0200540/**
541 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
542 * @write_group: Write group
543 *
544 * Load DQS settings for Write Group, do not trigger SCC update.
545 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200546static void scc_mgr_load_dqs_for_write_group(struct socfpga_sdrseq *seq,
547 const u32 write_group)
Marek Vasut7481b692015-07-12 22:11:55 +0200548{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200549 const int ratio = seq->rwcfg->mem_if_read_dqs_width /
550 seq->rwcfg->mem_if_write_dqs_width;
Marek Vasut5a4379e2015-07-13 00:30:09 +0200551 const int base = write_group * ratio;
552 int i;
Marek Vasut7481b692015-07-12 22:11:55 +0200553 /*
Marek Vasut5a4379e2015-07-13 00:30:09 +0200554 * Load the setting in the SCC manager
Marek Vasut7481b692015-07-12 22:11:55 +0200555 * Although OCT affects only write data, the OCT delay is controlled
556 * by the DQS logic block which is instantiated once per read group.
557 * For protocols where a write group consists of multiple read groups,
Marek Vasut5a4379e2015-07-13 00:30:09 +0200558 * the setting must be set multiple times.
Marek Vasut7481b692015-07-12 22:11:55 +0200559 */
Marek Vasut5a4379e2015-07-13 00:30:09 +0200560 for (i = 0; i < ratio; i++)
561 writel(base + i, &sdr_scc_mgr->dqs_ena);
Marek Vasut7481b692015-07-12 22:11:55 +0200562}
563
Marek Vasut62d3c692015-07-20 08:41:04 +0200564/**
565 * scc_mgr_zero_group() - Zero all configs for a group
566 *
567 * Zero DQ, DM, DQS and OCT configs for a group.
568 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200569static void scc_mgr_zero_group(struct socfpga_sdrseq *seq,
570 const u32 write_group, const int out_only)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500571{
Marek Vasut62d3c692015-07-20 08:41:04 +0200572 int i, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500573
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200574 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut62d3c692015-07-20 08:41:04 +0200575 r += NUM_RANKS_PER_SHADOW_REG) {
576 /* Zero all DQ config settings. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200577 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasutcab80792015-07-12 22:07:33 +0200578 scc_mgr_set_dq_out1_delay(i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500579 if (!out_only)
Marek Vasutcab80792015-07-12 22:07:33 +0200580 scc_mgr_set_dq_in_delay(i, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500581 }
582
Marek Vasut62d3c692015-07-20 08:41:04 +0200583 /* Multicast to all DQ enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200584 writel(0xff, &sdr_scc_mgr->dq_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500585
Marek Vasut62d3c692015-07-20 08:41:04 +0200586 /* Zero all DM config settings. */
Marek Vasutf4d38622016-04-04 21:16:18 +0200587 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
588 if (!out_only)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200589 scc_mgr_set_dm_in_delay(seq, i, 0);
590 scc_mgr_set_dm_out1_delay(seq, i, 0);
Marek Vasutf4d38622016-04-04 21:16:18 +0200591 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500592
Marek Vasut62d3c692015-07-20 08:41:04 +0200593 /* Multicast to all DM enables. */
Marek Vasutb5450962015-07-12 21:05:08 +0200594 writel(0xff, &sdr_scc_mgr->dm_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500595
Marek Vasut62d3c692015-07-20 08:41:04 +0200596 /* Zero all DQS IO settings. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500597 if (!out_only)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200598 scc_mgr_set_dqs_io_in_delay(seq, 0);
Marek Vasut62d3c692015-07-20 08:41:04 +0200599
600 /* Arria V/Cyclone V don't have out2. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200601 scc_mgr_set_dqs_out1_delay(seq, seq->iocfg->dqs_out_reserve);
602 scc_mgr_set_oct_out1_delay(seq, write_group,
603 seq->iocfg->dqs_out_reserve);
604 scc_mgr_load_dqs_for_write_group(seq, write_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500605
Marek Vasut62d3c692015-07-20 08:41:04 +0200606 /* Multicast to all DQS IO enables (only 1 in total). */
Marek Vasutb5450962015-07-12 21:05:08 +0200607 writel(0, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500608
Marek Vasut62d3c692015-07-20 08:41:04 +0200609 /* Hit update to zero everything. */
Marek Vasutb5450962015-07-12 21:05:08 +0200610 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500611 }
612}
613
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500614/*
615 * apply and load a particular input delay for the DQ pins in a group
616 * group_bgn is the index of the first dq pin (in the write group)
617 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200618static void scc_mgr_apply_group_dq_in_delay(struct socfpga_sdrseq *seq,
619 u32 group_bgn, u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500620{
Marek Vasut8af9ca02015-08-02 19:42:26 +0200621 u32 i, p;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500622
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200623 for (i = 0, p = group_bgn; i < seq->rwcfg->mem_dq_per_read_dqs;
624 i++, p++) {
Marek Vasutcab80792015-07-12 22:07:33 +0200625 scc_mgr_set_dq_in_delay(p, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500626 scc_mgr_load_dq(p);
627 }
628}
629
Marek Vasutcd649502015-07-17 05:42:49 +0200630/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200631 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the
632 * DQ pins in a group
Marek Vasutcd649502015-07-17 05:42:49 +0200633 * @delay: Delay value
634 *
635 * Apply and load a particular output delay for the DQ pins in a group.
636 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200637static void scc_mgr_apply_group_dq_out1_delay(struct socfpga_sdrseq *seq,
638 const u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500639{
Marek Vasutcd649502015-07-17 05:42:49 +0200640 int i;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500641
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200642 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
Marek Vasutcd649502015-07-17 05:42:49 +0200643 scc_mgr_set_dq_out1_delay(i, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500644 scc_mgr_load_dq(i);
645 }
646}
647
648/* apply and load a particular output delay for the DM pins in a group */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200649static void scc_mgr_apply_group_dm_out1_delay(struct socfpga_sdrseq *seq,
650 u32 delay1)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500651{
Marek Vasut8af9ca02015-08-02 19:42:26 +0200652 u32 i;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500653
654 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200655 scc_mgr_set_dm_out1_delay(seq, i, delay1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500656 scc_mgr_load_dm(i);
657 }
658}
659
660
661/* apply and load delay on both DQS and OCT out1 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200662static void scc_mgr_apply_group_dqs_io_and_oct_out1(struct socfpga_sdrseq *seq,
663 u32 write_group, u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500664{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200665 scc_mgr_set_dqs_out1_delay(seq, delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500666 scc_mgr_load_dqs_io();
667
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200668 scc_mgr_set_oct_out1_delay(seq, write_group, delay);
669 scc_mgr_load_dqs_for_write_group(seq, write_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500670}
671
Marek Vasut484fb3b2015-07-17 05:33:28 +0200672/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200673 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output
674 * side: DQ, DM, DQS, OCT
Marek Vasut484fb3b2015-07-17 05:33:28 +0200675 * @write_group: Write group
676 * @delay: Delay value
677 *
678 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
679 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200680static void scc_mgr_apply_group_all_out_delay_add(struct socfpga_sdrseq *seq,
681 const u32 write_group,
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200682 const u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500683{
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200684 u32 i, new_delay;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500685
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200686 /* DQ shift */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200687 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500688 scc_mgr_load_dq(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500689
Marek Vasut20bfb9d2015-07-17 05:30:14 +0200690 /* DM shift */
691 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500692 scc_mgr_load_dm(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500693
Marek Vasut484fb3b2015-07-17 05:33:28 +0200694 /* DQS shift */
695 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200696 if (new_delay > seq->iocfg->io_out2_delay_max) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +0200697 debug_cond(DLEVEL >= 1,
Marek Vasut484fb3b2015-07-17 05:33:28 +0200698 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
699 __func__, __LINE__, write_group, delay, new_delay,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200700 seq->iocfg->io_out2_delay_max,
701 new_delay - seq->iocfg->io_out2_delay_max);
702 new_delay -= seq->iocfg->io_out2_delay_max;
703 scc_mgr_set_dqs_out1_delay(seq, new_delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500704 }
705
706 scc_mgr_load_dqs_io();
707
Marek Vasut484fb3b2015-07-17 05:33:28 +0200708 /* OCT shift */
709 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200710 if (new_delay > seq->iocfg->io_out2_delay_max) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +0200711 debug_cond(DLEVEL >= 1,
Marek Vasut484fb3b2015-07-17 05:33:28 +0200712 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
713 __func__, __LINE__, write_group, delay,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200714 new_delay, seq->iocfg->io_out2_delay_max,
715 new_delay - seq->iocfg->io_out2_delay_max);
716 new_delay -= seq->iocfg->io_out2_delay_max;
717 scc_mgr_set_oct_out1_delay(seq, write_group, new_delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500718 }
719
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200720 scc_mgr_load_dqs_for_write_group(seq, write_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500721}
722
Marek Vasut788870f2015-07-19 02:18:21 +0200723/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200724 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output
725 * side to all ranks
Marek Vasut788870f2015-07-19 02:18:21 +0200726 * @write_group: Write group
727 * @delay: Delay value
728 *
729 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500730 */
Marek Vasut788870f2015-07-19 02:18:21 +0200731static void
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200732scc_mgr_apply_group_all_out_delay_add_all_ranks(struct socfpga_sdrseq *seq,
733 const u32 write_group,
Marek Vasut788870f2015-07-19 02:18:21 +0200734 const u32 delay)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500735{
Marek Vasut788870f2015-07-19 02:18:21 +0200736 int r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500737
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200738 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut788870f2015-07-19 02:18:21 +0200739 r += NUM_RANKS_PER_SHADOW_REG) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200740 scc_mgr_apply_group_all_out_delay_add(seq, write_group, delay);
Marek Vasutb5450962015-07-12 21:05:08 +0200741 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500742 }
743}
744
Marek Vasut42e78602015-07-26 11:07:19 +0200745/**
746 * set_jump_as_return() - Return instruction optimization
747 *
748 * Optimization used to recover some slots in ddr3 inst_rom could be
749 * applied to other protocols if we wanted to
750 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200751static void set_jump_as_return(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500752{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500753 /*
Marek Vasut42e78602015-07-26 11:07:19 +0200754 * To save space, we replace return with jump to special shared
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500755 * RETURN instruction so we set the counter to large value so that
Marek Vasut42e78602015-07-26 11:07:19 +0200756 * we always jump.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500757 */
Marek Vasutb5450962015-07-12 21:05:08 +0200758 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200759 writel(seq->rwcfg->rreturn, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500760}
761
Marek Vasut98d279a2015-07-26 11:46:04 +0200762/**
763 * delay_for_n_mem_clocks() - Delay for N memory clocks
764 * @clocks: Length of the delay
765 *
766 * Delay for N memory clocks.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500767 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200768static void delay_for_n_mem_clocks(struct socfpga_sdrseq *seq,
769 const u32 clocks)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500770{
Marek Vasut50d71992015-07-26 11:11:28 +0200771 u32 afi_clocks;
Marek Vasut13ee4382015-07-26 11:42:53 +0200772 u16 c_loop;
773 u8 inner;
774 u8 outer;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500775
776 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
777
Marek Vasut4b203df2015-07-26 11:34:09 +0200778 /* Scale (rounding up) to get afi clocks. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200779 afi_clocks = DIV_ROUND_UP(clocks, seq->misccfg->afi_rate_ratio);
Marek Vasut4b203df2015-07-26 11:34:09 +0200780 if (afi_clocks) /* Temporary underflow protection */
781 afi_clocks--;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500782
783 /*
Marek Vasut50d71992015-07-26 11:11:28 +0200784 * Note, we don't bother accounting for being off a little
785 * bit because of a few extra instructions in outer loops.
786 * Note, the loops have a test at the end, and do the test
787 * before the decrement, and so always perform the loop
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500788 * 1 time more than the counter value
789 */
Marek Vasut13ee4382015-07-26 11:42:53 +0200790 c_loop = afi_clocks >> 16;
791 outer = c_loop ? 0xff : (afi_clocks >> 8);
792 inner = outer ? 0xff : afi_clocks;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500793
794 /*
795 * rom instructions are structured as follows:
796 *
797 * IDLE_LOOP2: jnz cntr0, TARGET_A
798 * IDLE_LOOP1: jnz cntr1, TARGET_B
799 * return
800 *
801 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
802 * TARGET_B is set to IDLE_LOOP2 as well
803 *
804 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
805 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
806 *
807 * a little confusing, but it helps save precious space in the inst_rom
808 * and sequencer rom and keeps the delays more accurate and reduces
809 * overhead
810 */
Marek Vasut4b203df2015-07-26 11:34:09 +0200811 if (afi_clocks < 0x100) {
Marek Vasutb5450962015-07-12 21:05:08 +0200812 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasutc85b9b32015-08-02 19:47:01 +0200813 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500814
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200815 writel(seq->rwcfg->idle_loop1,
Marek Vasutc85b9b32015-08-02 19:47:01 +0200816 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500817
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200818 writel(seq->rwcfg->idle_loop1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasutb5450962015-07-12 21:05:08 +0200819 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500820 } else {
Marek Vasutb5450962015-07-12 21:05:08 +0200821 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
Marek Vasutc85b9b32015-08-02 19:47:01 +0200822 &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500823
Marek Vasutb5450962015-07-12 21:05:08 +0200824 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
Marek Vasutc85b9b32015-08-02 19:47:01 +0200825 &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500826
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200827 writel(seq->rwcfg->idle_loop2,
Marek Vasutc85b9b32015-08-02 19:47:01 +0200828 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500829
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200830 writel(seq->rwcfg->idle_loop2,
Marek Vasutc85b9b32015-08-02 19:47:01 +0200831 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500832
Marek Vasut7574c872015-07-26 11:44:54 +0200833 do {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200834 writel(seq->rwcfg->idle_loop2,
Marek Vasutc85b9b32015-08-02 19:47:01 +0200835 SDR_PHYGRP_RWMGRGRP_ADDRESS |
836 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Marek Vasut7574c872015-07-26 11:44:54 +0200837 } while (c_loop-- != 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500838 }
839 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
840}
841
Marek Vasut8bf92272015-07-13 00:44:30 +0200842/**
843 * rw_mgr_mem_init_load_regs() - Load instruction registers
844 * @cntr0: Counter 0 value
845 * @cntr1: Counter 1 value
846 * @cntr2: Counter 2 value
847 * @jump: Jump instruction value
848 *
849 * Load instruction registers.
850 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200851static void rw_mgr_mem_init_load_regs(struct socfpga_sdrseq *seq,
852 u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
Marek Vasut8bf92272015-07-13 00:44:30 +0200853{
Marek Vasut8af9ca02015-08-02 19:42:26 +0200854 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasut8bf92272015-07-13 00:44:30 +0200855 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
856
857 /* Load counters */
858 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
859 &sdr_rw_load_mgr_regs->load_cntr0);
860 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
861 &sdr_rw_load_mgr_regs->load_cntr1);
862 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
863 &sdr_rw_load_mgr_regs->load_cntr2);
864
865 /* Load jump address */
866 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
867 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
868 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
869
870 /* Execute count instruction */
871 writel(jump, grpaddr);
872}
873
Marek Vasutc577ab52015-07-13 00:51:05 +0200874/**
875 * rw_mgr_mem_load_user() - Load user calibration values
876 * @fin1: Final instruction 1
877 * @fin2: Final instruction 2
878 * @precharge: If 1, precharge the banks at the end
879 *
880 * Load user calibration values and optionally precharge the banks.
881 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200882static void rw_mgr_mem_load_user(struct socfpga_sdrseq *seq,
883 const u32 fin1, const u32 fin2,
Marek Vasutc577ab52015-07-13 00:51:05 +0200884 const int precharge)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500885{
Marek Vasutc577ab52015-07-13 00:51:05 +0200886 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
887 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
888 u32 r;
889
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200890 for (r = 0; r < seq->rwcfg->mem_number_of_ranks; r++) {
Marek Vasutc577ab52015-07-13 00:51:05 +0200891 /* set rank */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200892 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_OFF);
Marek Vasutc577ab52015-07-13 00:51:05 +0200893
894 /* precharge all banks ... */
895 if (precharge)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200896 writel(seq->rwcfg->precharge_all, grpaddr);
Marek Vasutc577ab52015-07-13 00:51:05 +0200897
898 /*
899 * USER Use Mirror-ed commands for odd ranks if address
900 * mirrorring is on
901 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200902 if ((seq->rwcfg->mem_address_mirroring >> r) & 0x1) {
903 set_jump_as_return(seq);
904 writel(seq->rwcfg->mrs2_mirr, grpaddr);
905 delay_for_n_mem_clocks(seq, 4);
906 set_jump_as_return(seq);
907 writel(seq->rwcfg->mrs3_mirr, grpaddr);
908 delay_for_n_mem_clocks(seq, 4);
909 set_jump_as_return(seq);
910 writel(seq->rwcfg->mrs1_mirr, grpaddr);
911 delay_for_n_mem_clocks(seq, 4);
912 set_jump_as_return(seq);
Marek Vasutc577ab52015-07-13 00:51:05 +0200913 writel(fin1, grpaddr);
914 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200915 set_jump_as_return(seq);
916 writel(seq->rwcfg->mrs2, grpaddr);
917 delay_for_n_mem_clocks(seq, 4);
918 set_jump_as_return(seq);
919 writel(seq->rwcfg->mrs3, grpaddr);
920 delay_for_n_mem_clocks(seq, 4);
921 set_jump_as_return(seq);
922 writel(seq->rwcfg->mrs1, grpaddr);
923 set_jump_as_return(seq);
Marek Vasutc577ab52015-07-13 00:51:05 +0200924 writel(fin2, grpaddr);
925 }
926
927 if (precharge)
928 continue;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500929
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200930 set_jump_as_return(seq);
931 writel(seq->rwcfg->zqcl, grpaddr);
Marek Vasutc577ab52015-07-13 00:51:05 +0200932
933 /* tZQinit = tDLLK = 512 ck cycles */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200934 delay_for_n_mem_clocks(seq, 512);
Marek Vasutc577ab52015-07-13 00:51:05 +0200935 }
936}
937
Marek Vasut1185e222015-07-26 10:57:06 +0200938/**
939 * rw_mgr_mem_initialize() - Initialize RW Manager
940 *
941 * Initialize RW Manager.
942 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200943static void rw_mgr_mem_initialize(struct socfpga_sdrseq *seq)
Marek Vasutc577ab52015-07-13 00:51:05 +0200944{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500945 debug("%s:%d\n", __func__, __LINE__);
946
947 /* The reset / cke part of initialization is broadcasted to all ranks */
Marek Vasutb5450962015-07-12 21:05:08 +0200948 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
949 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500950
951 /*
952 * Here's how you load register for a loop
953 * Counters are located @ 0x800
954 * Jump address are located @ 0xC00
955 * For both, registers 0 to 3 are selected using bits 3 and 2, like
956 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
957 * I know this ain't pretty, but Avalon bus throws away the 2 least
958 * significant bits
959 */
960
Marek Vasut1185e222015-07-26 10:57:06 +0200961 /* Start with memory RESET activated */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500962
963 /* tINIT = 200us */
964
965 /*
966 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
967 * If a and b are the number of iteration in 2 nested loops
968 * it takes the following number of cycles to complete the operation:
969 * number_of_cycles = ((2 + n) * a + 2) * b
970 * where n is the number of instruction in the inner loop
971 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
972 * b = 6A
973 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200974 rw_mgr_mem_init_load_regs(seq, seq->misccfg->tinit_cntr0_val,
975 seq->misccfg->tinit_cntr1_val,
976 seq->misccfg->tinit_cntr2_val,
977 seq->rwcfg->init_reset_0_cke_0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500978
Marek Vasut1185e222015-07-26 10:57:06 +0200979 /* Indicate that memory is stable. */
Marek Vasutb5450962015-07-12 21:05:08 +0200980 writel(1, &phy_mgr_cfg->reset_mem_stbl);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -0500981
982 /*
983 * transition the RESET to high
984 * Wait for 500us
985 */
986
987 /*
988 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
989 * If a and b are the number of iteration in 2 nested loops
990 * it takes the following number of cycles to complete the operation
991 * number_of_cycles = ((2 + n) * a + 2) * b
992 * where n is the number of instruction in the inner loop
993 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
994 * b = FF
995 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +0200996 rw_mgr_mem_init_load_regs(seq, seq->misccfg->treset_cntr0_val,
997 seq->misccfg->treset_cntr1_val,
998 seq->misccfg->treset_cntr2_val,
999 seq->rwcfg->init_reset_1_cke_0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001000
Marek Vasut1185e222015-07-26 10:57:06 +02001001 /* Bring up clock enable. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001002
1003 /* tXRP < 250 ck cycles */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001004 delay_for_n_mem_clocks(seq, 250);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001005
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001006 rw_mgr_mem_load_user(seq, seq->rwcfg->mrs0_dll_reset_mirr,
1007 seq->rwcfg->mrs0_dll_reset, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001008}
1009
Marek Vasutc1402752015-07-26 10:59:19 +02001010/**
1011 * rw_mgr_mem_handoff() - Hand off the memory to user
1012 *
1013 * At the end of calibration we have to program the user settings in
1014 * and hand off the memory to the user.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001015 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001016static void rw_mgr_mem_handoff(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001017{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001018 rw_mgr_mem_load_user(seq, seq->rwcfg->mrs0_user_mirr,
1019 seq->rwcfg->mrs0_user, 1);
Marek Vasutc577ab52015-07-13 00:51:05 +02001020 /*
Marek Vasutc1402752015-07-26 10:59:19 +02001021 * Need to wait tMOD (12CK or 15ns) time before issuing other
1022 * commands, but we will have plenty of NIOS cycles before actual
1023 * handoff so its okay.
Marek Vasutc577ab52015-07-13 00:51:05 +02001024 */
Marek Vasut0b97c422015-07-21 05:43:37 +02001025}
1026
Marek Vasutadbaa2d2015-07-21 06:00:36 +02001027/**
1028 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1029 * @group: Write Group
1030 * @use_dm: Use DM
1031 *
1032 * Issue write test command. Two variants are provided, one that just tests
1033 * a write pattern and another that tests datamask functionality.
Marek Vasut0b97c422015-07-21 05:43:37 +02001034 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001035static void rw_mgr_mem_calibrate_write_test_issue(struct socfpga_sdrseq *seq,
1036 u32 group, u32 test_dm)
Marek Vasut0b97c422015-07-21 05:43:37 +02001037{
Marek Vasutadbaa2d2015-07-21 06:00:36 +02001038 const u32 quick_write_mode =
1039 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001040 seq->misccfg->enable_super_quick_calibration;
Marek Vasutadbaa2d2015-07-21 06:00:36 +02001041 u32 mcc_instruction;
1042 u32 rw_wl_nop_cycles;
Marek Vasut0b97c422015-07-21 05:43:37 +02001043
1044 /*
1045 * Set counter and jump addresses for the right
1046 * number of NOP cycles.
1047 * The number of supported NOP cycles can range from -1 to infinity
1048 * Three different cases are handled:
1049 *
1050 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1051 * mechanism will be used to insert the right number of NOPs
1052 *
1053 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1054 * issuing the write command will jump straight to the
1055 * micro-instruction that turns on DQS (for DDRx), or outputs write
1056 * data (for RLD), skipping
1057 * the NOP micro-instruction all together
1058 *
1059 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1060 * turned on in the same micro-instruction that issues the write
1061 * command. Then we need
1062 * to directly jump to the micro-instruction that sends out the data
1063 *
1064 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1065 * (2 and 3). One jump-counter (0) is used to perform multiple
1066 * write-read operations.
1067 * one counter left to issue this command in "multiple-group" mode
1068 */
1069
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001070 rw_wl_nop_cycles = seq->gbl.rw_wl_nop_cycles;
Marek Vasut0b97c422015-07-21 05:43:37 +02001071
1072 if (rw_wl_nop_cycles == -1) {
1073 /*
1074 * CNTR 2 - We want to execute the special write operation that
1075 * turns on DQS right away and then skip directly to the
1076 * instruction that sends out the data. We set the counter to a
1077 * large number so that the jump is always taken.
1078 */
1079 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1080
1081 /* CNTR 3 - Not used */
1082 if (test_dm) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001083 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0_wl_1;
1084 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_data,
Marek Vasut0b97c422015-07-21 05:43:37 +02001085 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001086 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasut0b97c422015-07-21 05:43:37 +02001087 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1088 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001089 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0_wl_1;
1090 writel(seq->rwcfg->lfsr_wr_rd_bank_0_data,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001091 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001092 writel(seq->rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001093 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasut0b97c422015-07-21 05:43:37 +02001094 }
1095 } else if (rw_wl_nop_cycles == 0) {
1096 /*
1097 * CNTR 2 - We want to skip the NOP operation and go straight
1098 * to the DQS enable instruction. We set the counter to a large
1099 * number so that the jump is always taken.
1100 */
1101 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1102
1103 /* CNTR 3 - Not used */
1104 if (test_dm) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001105 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0;
1106 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_dqs,
Marek Vasut0b97c422015-07-21 05:43:37 +02001107 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1108 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001109 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0;
1110 writel(seq->rwcfg->lfsr_wr_rd_bank_0_dqs,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001111 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Marek Vasut0b97c422015-07-21 05:43:37 +02001112 }
1113 } else {
1114 /*
1115 * CNTR 2 - In this case we want to execute the next instruction
1116 * and NOT take the jump. So we set the counter to 0. The jump
1117 * address doesn't count.
1118 */
1119 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1120 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1121
1122 /*
1123 * CNTR 3 - Set the nop counter to the number of cycles we
1124 * need to loop for, minus 1.
1125 */
1126 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1127 if (test_dm) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001128 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0;
1129 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_nop,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001130 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasut0b97c422015-07-21 05:43:37 +02001131 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001132 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0;
1133 writel(seq->rwcfg->lfsr_wr_rd_bank_0_nop,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001134 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Marek Vasut0b97c422015-07-21 05:43:37 +02001135 }
1136 }
1137
1138 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1139 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1140
1141 if (quick_write_mode)
1142 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1143 else
1144 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1145
1146 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1147
1148 /*
1149 * CNTR 1 - This is used to ensure enough time elapses
1150 * for read data to come back.
1151 */
1152 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1153
1154 if (test_dm) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001155 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_wait,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001156 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasut0b97c422015-07-21 05:43:37 +02001157 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001158 writel(seq->rwcfg->lfsr_wr_rd_bank_0_wait,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001159 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Marek Vasut0b97c422015-07-21 05:43:37 +02001160 }
1161
Marek Vasutadbaa2d2015-07-21 06:00:36 +02001162 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1163 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1164 (group << 2));
Marek Vasut0b97c422015-07-21 05:43:37 +02001165}
1166
Marek Vasutc67d9622015-07-21 05:57:11 +02001167/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001168 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple
1169 * pass
Marek Vasutc67d9622015-07-21 05:57:11 +02001170 * @rank_bgn: Rank number
1171 * @write_group: Write Group
1172 * @use_dm: Use DM
1173 * @all_correct: All bits must be correct in the mask
1174 * @bit_chk: Resulting bit mask after the test
1175 * @all_ranks: Test all ranks
1176 *
1177 * Test writes, can check for a single bit pass or multiple bit pass.
1178 */
Marek Vasutbc773a12015-07-21 05:54:39 +02001179static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001180rw_mgr_mem_calibrate_write_test(struct socfpga_sdrseq *seq,
1181 const u32 rank_bgn, const u32 write_group,
Marek Vasutbc773a12015-07-21 05:54:39 +02001182 const u32 use_dm, const u32 all_correct,
1183 u32 *bit_chk, const u32 all_ranks)
Marek Vasut0b97c422015-07-21 05:43:37 +02001184{
Marek Vasutbc773a12015-07-21 05:54:39 +02001185 const u32 rank_end = all_ranks ?
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001186 seq->rwcfg->mem_number_of_ranks :
Marek Vasutbc773a12015-07-21 05:54:39 +02001187 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001188 const u32 shift_ratio = seq->rwcfg->mem_dq_per_write_dqs /
1189 seq->rwcfg->mem_virtual_groups_per_write_dqs;
1190 const u32 correct_mask_vg = seq->param.write_correct_mask_vg;
Marek Vasutbc773a12015-07-21 05:54:39 +02001191
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001192 u32 tmp_bit_chk, base_rw_mgr, group;
Marek Vasutbc773a12015-07-21 05:54:39 +02001193 int vg, r;
Marek Vasut0b97c422015-07-21 05:43:37 +02001194
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001195 *bit_chk = seq->param.write_correct_mask;
Marek Vasut0b97c422015-07-21 05:43:37 +02001196
1197 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasutbc773a12015-07-21 05:54:39 +02001198 /* Set rank */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001199 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
Marek Vasut0b97c422015-07-21 05:43:37 +02001200
1201 tmp_bit_chk = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001202 for (vg = seq->rwcfg->mem_virtual_groups_per_write_dqs - 1;
Marek Vasutbc773a12015-07-21 05:54:39 +02001203 vg >= 0; vg--) {
1204 /* Reset the FIFOs to get pointers to known state. */
Marek Vasut0b97c422015-07-21 05:43:37 +02001205 writel(0, &phy_mgr_cmd->fifo_reset);
1206
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001207 group = write_group *
1208 seq->rwcfg->mem_virtual_groups_per_write_dqs
1209 + vg;
1210 rw_mgr_mem_calibrate_write_test_issue(seq, group,
1211 use_dm);
Marek Vasut0b97c422015-07-21 05:43:37 +02001212
Marek Vasutbc773a12015-07-21 05:54:39 +02001213 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1214 tmp_bit_chk <<= shift_ratio;
1215 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
Marek Vasut0b97c422015-07-21 05:43:37 +02001216 }
Marek Vasutbc773a12015-07-21 05:54:39 +02001217
Marek Vasut0b97c422015-07-21 05:43:37 +02001218 *bit_chk &= tmp_bit_chk;
1219 }
1220
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001221 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
Marek Vasut0b97c422015-07-21 05:43:37 +02001222 if (all_correct) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001223 debug_cond(DLEVEL >= 2,
Marek Vasutbc773a12015-07-21 05:54:39 +02001224 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1225 write_group, use_dm, *bit_chk,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001226 seq->param.write_correct_mask,
1227 *bit_chk == seq->param.write_correct_mask);
1228 return *bit_chk == seq->param.write_correct_mask;
Marek Vasut0b97c422015-07-21 05:43:37 +02001229 } else {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001230 debug_cond(DLEVEL >= 2,
Marek Vasutbc773a12015-07-21 05:54:39 +02001231 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1232 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
Marek Vasut0b97c422015-07-21 05:43:37 +02001233 return *bit_chk != 0x00;
1234 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001235}
1236
Marek Vasut55c4d692015-07-18 03:55:07 +02001237/**
1238 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1239 * @rank_bgn: Rank number
1240 * @group: Read/Write Group
1241 * @all_ranks: Test all ranks
1242 *
1243 * Performs a guaranteed read on the patterns we are going to use during a
1244 * read test to ensure memory works.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001245 */
Marek Vasut55c4d692015-07-18 03:55:07 +02001246static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001247rw_mgr_mem_calibrate_read_test_patterns(struct socfpga_sdrseq *seq,
1248 const u32 rank_bgn, const u32 group,
Marek Vasut55c4d692015-07-18 03:55:07 +02001249 const u32 all_ranks)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001250{
Marek Vasut55c4d692015-07-18 03:55:07 +02001251 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1252 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1253 const u32 addr_offset =
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001254 (group * seq->rwcfg->mem_virtual_groups_per_read_dqs)
1255 << 2;
Marek Vasut55c4d692015-07-18 03:55:07 +02001256 const u32 rank_end = all_ranks ?
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001257 seq->rwcfg->mem_number_of_ranks :
Marek Vasut55c4d692015-07-18 03:55:07 +02001258 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001259 const u32 shift_ratio = seq->rwcfg->mem_dq_per_read_dqs /
1260 seq->rwcfg->mem_virtual_groups_per_read_dqs;
1261 const u32 correct_mask_vg = seq->param.read_correct_mask_vg;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001262
Marek Vasut55c4d692015-07-18 03:55:07 +02001263 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1264 int vg, r;
1265 int ret = 0;
1266
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001267 bit_chk = seq->param.read_correct_mask;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001268
1269 for (r = rank_bgn; r < rank_end; r++) {
Marek Vasut55c4d692015-07-18 03:55:07 +02001270 /* Set rank */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001271 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001272
1273 /* Load up a constant bursts of read commands */
Marek Vasutb5450962015-07-12 21:05:08 +02001274 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001275 writel(seq->rwcfg->guaranteed_read,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001276 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001277
Marek Vasutb5450962015-07-12 21:05:08 +02001278 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001279 writel(seq->rwcfg->guaranteed_read_cont,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001280 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001281
1282 tmp_bit_chk = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001283 for (vg = seq->rwcfg->mem_virtual_groups_per_read_dqs - 1;
Marek Vasut55c4d692015-07-18 03:55:07 +02001284 vg >= 0; vg--) {
1285 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutb5450962015-07-12 21:05:08 +02001286 writel(0, &phy_mgr_cmd->fifo_reset);
1287 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1288 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001289 writel(seq->rwcfg->guaranteed_read,
Marek Vasut55c4d692015-07-18 03:55:07 +02001290 addr + addr_offset + (vg << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001291
Marek Vasutb5450962015-07-12 21:05:08 +02001292 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Marek Vasut55c4d692015-07-18 03:55:07 +02001293 tmp_bit_chk <<= shift_ratio;
1294 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001295 }
Marek Vasut55c4d692015-07-18 03:55:07 +02001296
1297 bit_chk &= tmp_bit_chk;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001298 }
1299
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001300 writel(seq->rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001301
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001302 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
Marek Vasut55c4d692015-07-18 03:55:07 +02001303
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001304 if (bit_chk != seq->param.read_correct_mask)
Marek Vasut55c4d692015-07-18 03:55:07 +02001305 ret = -EIO;
1306
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001307 debug_cond(DLEVEL >= 1,
Marek Vasut55c4d692015-07-18 03:55:07 +02001308 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1309 __func__, __LINE__, group, bit_chk,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001310 seq->param.read_correct_mask, ret);
Marek Vasut55c4d692015-07-18 03:55:07 +02001311
1312 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001313}
1314
Marek Vasut6a752782015-07-18 03:34:22 +02001315/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001316 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read
1317 * test
Marek Vasut6a752782015-07-18 03:34:22 +02001318 * @rank_bgn: Rank number
1319 * @all_ranks: Test all ranks
1320 *
1321 * Load up the patterns we are going to use during a read test.
1322 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001323static void rw_mgr_mem_calibrate_read_load_patterns(struct socfpga_sdrseq *seq,
1324 const u32 rank_bgn,
Marek Vasut6a752782015-07-18 03:34:22 +02001325 const int all_ranks)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001326{
Marek Vasut6a752782015-07-18 03:34:22 +02001327 const u32 rank_end = all_ranks ?
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001328 seq->rwcfg->mem_number_of_ranks :
Marek Vasut6a752782015-07-18 03:34:22 +02001329 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1330 u32 r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001331
1332 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut6a752782015-07-18 03:34:22 +02001333
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001334 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001335 /* set rank */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001336 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001337
1338 /* Load up a constant bursts */
Marek Vasutb5450962015-07-12 21:05:08 +02001339 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001340
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001341 writel(seq->rwcfg->guaranteed_write_wait0,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001342 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001343
Marek Vasutb5450962015-07-12 21:05:08 +02001344 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001345
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001346 writel(seq->rwcfg->guaranteed_write_wait1,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001347 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001348
Marek Vasutb5450962015-07-12 21:05:08 +02001349 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001350
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001351 writel(seq->rwcfg->guaranteed_write_wait2,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001352 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001353
Marek Vasutb5450962015-07-12 21:05:08 +02001354 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001355
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001356 writel(seq->rwcfg->guaranteed_write_wait3,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001357 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001358
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001359 writel(seq->rwcfg->guaranteed_write,
1360 SDR_PHYGRP_RWMGRGRP_ADDRESS |
1361 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001362 }
1363
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001364 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001365}
1366
Marek Vasut656002e2015-07-20 03:26:05 +02001367/**
1368 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1369 * @rank_bgn: Rank number
1370 * @group: Read/Write group
1371 * @num_tries: Number of retries of the test
1372 * @all_correct: All bits must be correct in the mask
1373 * @bit_chk: Resulting bit mask after the test
1374 * @all_groups: Test all R/W groups
1375 * @all_ranks: Test all ranks
1376 *
1377 * Try a read and see if it returns correct data back. Test has dummy reads
1378 * inserted into the mix used to align DQS enable. Test has more thorough
1379 * checks than the regular read test.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001380 */
Marek Vasutc6c1fe72015-07-19 07:48:58 +02001381static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001382rw_mgr_mem_calibrate_read_test(struct socfpga_sdrseq *seq,
1383 const u32 rank_bgn, const u32 group,
Marek Vasutc6c1fe72015-07-19 07:48:58 +02001384 const u32 num_tries, const u32 all_correct,
1385 u32 *bit_chk,
1386 const u32 all_groups, const u32 all_ranks)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001387{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001388 const u32 rank_end = all_ranks ? seq->rwcfg->mem_number_of_ranks :
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001389 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
Marek Vasutc6c1fe72015-07-19 07:48:58 +02001390 const u32 quick_read_mode =
1391 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001392 seq->misccfg->enable_super_quick_calibration);
1393 u32 correct_mask_vg = seq->param.read_correct_mask_vg;
Marek Vasutc6c1fe72015-07-19 07:48:58 +02001394 u32 tmp_bit_chk;
1395 u32 base_rw_mgr;
1396 u32 addr;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001397
Marek Vasutc6c1fe72015-07-19 07:48:58 +02001398 int r, vg, ret;
Marek Vasuta005c772015-07-19 07:44:21 +02001399
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001400 *bit_chk = seq->param.read_correct_mask;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001401
1402 for (r = rank_bgn; r < rank_end; r++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001403 /* set rank */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001404 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001405
Marek Vasutb5450962015-07-12 21:05:08 +02001406 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001407
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001408 writel(seq->rwcfg->read_b2b_wait1,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001409 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001410
Marek Vasutb5450962015-07-12 21:05:08 +02001411 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001412 writel(seq->rwcfg->read_b2b_wait2,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001413 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001414
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001415 if (quick_read_mode)
Marek Vasutb5450962015-07-12 21:05:08 +02001416 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001417 /* need at least two (1+1) reads to capture failures */
1418 else if (all_groups)
Marek Vasutb5450962015-07-12 21:05:08 +02001419 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001420 else
Marek Vasutb5450962015-07-12 21:05:08 +02001421 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001422
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001423 writel(seq->rwcfg->read_b2b,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001424 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001425 if (all_groups)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001426 writel(seq->rwcfg->mem_if_read_dqs_width *
1427 seq->rwcfg->mem_virtual_groups_per_read_dqs - 1,
Marek Vasutb5450962015-07-12 21:05:08 +02001428 &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001429 else
Marek Vasutb5450962015-07-12 21:05:08 +02001430 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001431
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001432 writel(seq->rwcfg->read_b2b,
Marek Vasutc85b9b32015-08-02 19:47:01 +02001433 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001434
1435 tmp_bit_chk = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001436 for (vg = seq->rwcfg->mem_virtual_groups_per_read_dqs - 1;
1437 vg >= 0; vg--) {
Marek Vasut50a780f2015-07-19 07:57:28 +02001438 /* Reset the FIFOs to get pointers to known state. */
Marek Vasutb5450962015-07-12 21:05:08 +02001439 writel(0, &phy_mgr_cmd->fifo_reset);
1440 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1441 RW_MGR_RESET_READ_DATAPATH_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001442
Marek Vasut50a780f2015-07-19 07:57:28 +02001443 if (all_groups) {
1444 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1445 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1446 } else {
1447 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1448 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1449 }
Marek Vasuta3340102015-07-12 19:03:33 +02001450
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001451 writel(seq->rwcfg->read_b2b, addr +
Marek Vasutc85b9b32015-08-02 19:47:01 +02001452 ((group *
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001453 seq->rwcfg->mem_virtual_groups_per_read_dqs +
Marek Vasutc85b9b32015-08-02 19:47:01 +02001454 vg) << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001455
Marek Vasutb5450962015-07-12 21:05:08 +02001456 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001457 tmp_bit_chk <<=
1458 seq->rwcfg->mem_dq_per_read_dqs /
1459 seq->rwcfg->mem_virtual_groups_per_read_dqs;
Marek Vasut50a780f2015-07-19 07:57:28 +02001460 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001461 }
Marek Vasut28957f32015-07-19 07:51:17 +02001462
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001463 *bit_chk &= tmp_bit_chk;
1464 }
1465
Marek Vasuta3340102015-07-12 19:03:33 +02001466 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001467 writel(seq->rwcfg->clear_dqs_enable, addr + (group << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001468
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001469 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
Marek Vasuta005c772015-07-19 07:44:21 +02001470
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001471 if (all_correct) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001472 ret = (*bit_chk == seq->param.read_correct_mask);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001473 debug_cond(DLEVEL >= 2,
Marek Vasuta005c772015-07-19 07:44:21 +02001474 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1475 __func__, __LINE__, group, all_groups, *bit_chk,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001476 seq->param.read_correct_mask, ret);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001477 } else {
Marek Vasuta005c772015-07-19 07:44:21 +02001478 ret = (*bit_chk != 0x00);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001479 debug_cond(DLEVEL >= 2,
Marek Vasuta005c772015-07-19 07:44:21 +02001480 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1481 __func__, __LINE__, group, all_groups, *bit_chk,
1482 0, ret);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001483 }
Marek Vasuta005c772015-07-19 07:44:21 +02001484
1485 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001486}
1487
Marek Vasuta50d5d72015-07-19 07:35:36 +02001488/**
1489 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1490 * @grp: Read/Write group
1491 * @num_tries: Number of retries of the test
1492 * @all_correct: All bits must be correct in the mask
1493 * @all_groups: Test all R/W groups
1494 *
1495 * Perform a READ test across all memory ranks.
1496 */
1497static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001498rw_mgr_mem_calibrate_read_test_all_ranks(struct socfpga_sdrseq *seq,
1499 const u32 grp, const u32 num_tries,
Marek Vasuta50d5d72015-07-19 07:35:36 +02001500 const u32 all_correct,
1501 const u32 all_groups)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001502{
Marek Vasuta50d5d72015-07-19 07:35:36 +02001503 u32 bit_chk;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001504 return rw_mgr_mem_calibrate_read_test(seq, 0, grp, num_tries,
1505 all_correct, &bit_chk, all_groups,
1506 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001507}
1508
Marek Vasut1c9f25b2015-07-19 06:25:27 +02001509/**
1510 * rw_mgr_incr_vfifo() - Increase VFIFO value
1511 * @grp: Read/Write group
Marek Vasut1c9f25b2015-07-19 06:25:27 +02001512 *
1513 * Increase VFIFO value.
1514 */
Marek Vasut42e43ab2015-07-19 06:37:51 +02001515static void rw_mgr_incr_vfifo(const u32 grp)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001516{
Marek Vasutb5450962015-07-12 21:05:08 +02001517 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001518}
1519
Marek Vasut1c9f25b2015-07-19 06:25:27 +02001520/**
1521 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1522 * @grp: Read/Write group
Marek Vasut1c9f25b2015-07-19 06:25:27 +02001523 *
1524 * Decrease VFIFO value.
1525 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001526static void rw_mgr_decr_vfifo(struct socfpga_sdrseq *seq, const u32 grp)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001527{
Marek Vasut1c9f25b2015-07-19 06:25:27 +02001528 u32 i;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001529
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001530 for (i = 0; i < seq->misccfg->read_valid_fifo_size - 1; i++)
Marek Vasut42e43ab2015-07-19 06:37:51 +02001531 rw_mgr_incr_vfifo(grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001532}
1533
Marek Vasut088eb212015-07-19 06:45:43 +02001534/**
1535 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1536 * @grp: Read/Write group
1537 *
1538 * Push VFIFO until a failing read happens.
1539 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001540static int find_vfifo_failing_read(struct socfpga_sdrseq *seq,
1541 const u32 grp)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001542{
Marek Vasuta50d5d72015-07-19 07:35:36 +02001543 u32 v, ret, fail_cnt = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001544
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001545 for (v = 0; v < seq->misccfg->read_valid_fifo_size; v++) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001546 debug_cond(DLEVEL >= 2, "%s:%d: vfifo %u\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001547 __func__, __LINE__, v);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001548 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1549 PASS_ONE_BIT, 0);
Marek Vasut088eb212015-07-19 06:45:43 +02001550 if (!ret) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001551 fail_cnt++;
1552
1553 if (fail_cnt == 2)
Marek Vasut088eb212015-07-19 06:45:43 +02001554 return v;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001555 }
1556
Marek Vasut088eb212015-07-19 06:45:43 +02001557 /* Fiddle with FIFO. */
Marek Vasut42e43ab2015-07-19 06:37:51 +02001558 rw_mgr_incr_vfifo(grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001559 }
1560
Marek Vasut088eb212015-07-19 06:45:43 +02001561 /* No failing read found! Something must have gone wrong. */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001562 debug_cond(DLEVEL >= 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
Marek Vasut088eb212015-07-19 06:45:43 +02001563 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001564}
1565
Marek Vasutf2b02d42015-07-19 05:26:49 +02001566/**
Marek Vasut6ff36b72015-07-19 07:27:06 +02001567 * sdr_find_phase_delay() - Find DQS enable phase or delay
1568 * @working: If 1, look for working phase/delay, if 0, look for non-working
1569 * @delay: If 1, look for delay, if 0, look for phase
1570 * @grp: Read/Write group
1571 * @work: Working window position
1572 * @work_inc: Working window increment
1573 * @pd: DQS Phase/Delay Iterator
1574 *
1575 * Find working or non-working DQS enable phase setting.
1576 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001577static int sdr_find_phase_delay(struct socfpga_sdrseq *seq, int working,
1578 int delay, const u32 grp, u32 *work,
1579 const u32 work_inc, u32 *pd)
Marek Vasut6ff36b72015-07-19 07:27:06 +02001580{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001581 const u32 max = delay ? seq->iocfg->dqs_en_delay_max :
1582 seq->iocfg->dqs_en_phase_max;
Marek Vasuta50d5d72015-07-19 07:35:36 +02001583 u32 ret;
Marek Vasut6ff36b72015-07-19 07:27:06 +02001584
1585 for (; *pd <= max; (*pd)++) {
1586 if (delay)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001587 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, *pd);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001588 else
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001589 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, *pd);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001590
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001591 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1592 PASS_ONE_BIT, 0);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001593 if (!working)
1594 ret = !ret;
1595
1596 if (ret)
1597 return 0;
1598
1599 if (work)
1600 *work += work_inc;
1601 }
1602
1603 return -EINVAL;
1604}
1605/**
Marek Vasutf2b02d42015-07-19 05:26:49 +02001606 * sdr_find_phase() - Find DQS enable phase
1607 * @working: If 1, look for working phase, if 0, look for non-working phase
1608 * @grp: Read/Write group
Marek Vasutf2b02d42015-07-19 05:26:49 +02001609 * @work: Working window position
1610 * @i: Iterator
1611 * @p: DQS Phase Iterator
Marek Vasutf2b02d42015-07-19 05:26:49 +02001612 *
1613 * Find working or non-working DQS enable phase setting.
1614 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001615static int sdr_find_phase(struct socfpga_sdrseq *seq, int working,
1616 const u32 grp, u32 *work, u32 *i, u32 *p)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001617{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001618 const u32 end = seq->misccfg->read_valid_fifo_size + (working ? 0 : 1);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001619 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001620
Marek Vasutf2b02d42015-07-19 05:26:49 +02001621 for (; *i < end; (*i)++) {
1622 if (working)
1623 *p = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001624
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001625 ret = sdr_find_phase_delay(seq, working, 0, grp, work,
1626 seq->iocfg->delay_per_opa_tap, p);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001627 if (!ret)
1628 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001629
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001630 if (*p > seq->iocfg->dqs_en_phase_max) {
Marek Vasutf2b02d42015-07-19 05:26:49 +02001631 /* Fiddle with FIFO. */
Marek Vasut42e43ab2015-07-19 06:37:51 +02001632 rw_mgr_incr_vfifo(grp);
Marek Vasutf2b02d42015-07-19 05:26:49 +02001633 if (!working)
1634 *p = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001635 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001636 }
1637
Marek Vasutf2b02d42015-07-19 05:26:49 +02001638 return -EINVAL;
1639}
1640
Marek Vasut6394ef52015-07-19 06:04:00 +02001641/**
1642 * sdr_working_phase() - Find working DQS enable phase
1643 * @grp: Read/Write group
1644 * @work_bgn: Working window start position
Marek Vasut6394ef52015-07-19 06:04:00 +02001645 * @d: dtaps output value
1646 * @p: DQS Phase Iterator
1647 * @i: Iterator
1648 *
1649 * Find working DQS enable phase setting.
1650 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001651static int sdr_working_phase(struct socfpga_sdrseq *seq, const u32 grp,
1652 u32 *work_bgn, u32 *d, u32 *p, u32 *i)
Marek Vasutf2b02d42015-07-19 05:26:49 +02001653{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001654 const u32 dtaps_per_ptap = seq->iocfg->delay_per_opa_tap /
1655 seq->iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutf2b02d42015-07-19 05:26:49 +02001656 int ret;
1657
1658 *work_bgn = 0;
1659
1660 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1661 *i = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001662 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, *d);
1663 ret = sdr_find_phase(seq, 1, grp, work_bgn, i, p);
Marek Vasutf2b02d42015-07-19 05:26:49 +02001664 if (!ret)
1665 return 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001666 *work_bgn += seq->iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutf2b02d42015-07-19 05:26:49 +02001667 }
1668
Marek Vasutb148ebe2015-07-19 05:01:12 +02001669 /* Cannot find working solution */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001670 debug_cond(DLEVEL >= 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
Marek Vasutf2b02d42015-07-19 05:26:49 +02001671 __func__, __LINE__);
1672 return -EINVAL;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001673}
1674
Marek Vasut6394ef52015-07-19 06:04:00 +02001675/**
1676 * sdr_backup_phase() - Find DQS enable backup phase
1677 * @grp: Read/Write group
1678 * @work_bgn: Working window start position
Marek Vasut6394ef52015-07-19 06:04:00 +02001679 * @p: DQS Phase Iterator
1680 *
1681 * Find DQS enable backup phase setting.
1682 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001683static void sdr_backup_phase(struct socfpga_sdrseq *seq, const u32 grp,
1684 u32 *work_bgn, u32 *p)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001685{
Marek Vasuta50d5d72015-07-19 07:35:36 +02001686 u32 tmp_delay, d;
Marek Vasut6394ef52015-07-19 06:04:00 +02001687 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001688
1689 /* Special case code for backing up a phase */
1690 if (*p == 0) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001691 *p = seq->iocfg->dqs_en_phase_max;
1692 rw_mgr_decr_vfifo(seq, grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001693 } else {
1694 (*p)--;
1695 }
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001696 tmp_delay = *work_bgn - seq->iocfg->delay_per_opa_tap;
1697 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, *p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001698
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001699 for (d = 0; d <= seq->iocfg->dqs_en_delay_max && tmp_delay < *work_bgn;
Marek Vasutc85b9b32015-08-02 19:47:01 +02001700 d++) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001701 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001702
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001703 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1704 PASS_ONE_BIT, 0);
Marek Vasut6394ef52015-07-19 06:04:00 +02001705 if (ret) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001706 *work_bgn = tmp_delay;
1707 break;
1708 }
Marek Vasut6eff8032015-07-19 05:48:30 +02001709
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001710 tmp_delay += seq->iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001711 }
1712
Marek Vasut6394ef52015-07-19 06:04:00 +02001713 /* Restore VFIFO to old state before we decremented it (if needed). */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001714 (*p)++;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001715 if (*p > seq->iocfg->dqs_en_phase_max) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001716 *p = 0;
Marek Vasut42e43ab2015-07-19 06:37:51 +02001717 rw_mgr_incr_vfifo(grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001718 }
1719
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001720 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001721}
1722
Marek Vasut6394ef52015-07-19 06:04:00 +02001723/**
1724 * sdr_nonworking_phase() - Find non-working DQS enable phase
1725 * @grp: Read/Write group
1726 * @work_end: Working window end position
Marek Vasut6394ef52015-07-19 06:04:00 +02001727 * @p: DQS Phase Iterator
1728 * @i: Iterator
1729 *
1730 * Find non-working DQS enable phase setting.
1731 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001732static int sdr_nonworking_phase(struct socfpga_sdrseq *seq,
1733 const u32 grp, u32 *work_end, u32 *p, u32 *i)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001734{
Marek Vasutf2b02d42015-07-19 05:26:49 +02001735 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001736
1737 (*p)++;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001738 *work_end += seq->iocfg->delay_per_opa_tap;
1739 if (*p > seq->iocfg->dqs_en_phase_max) {
Marek Vasutf2b02d42015-07-19 05:26:49 +02001740 /* Fiddle with FIFO. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001741 *p = 0;
Marek Vasut42e43ab2015-07-19 06:37:51 +02001742 rw_mgr_incr_vfifo(grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001743 }
1744
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001745 ret = sdr_find_phase(seq, 0, grp, work_end, i, p);
Marek Vasutf2b02d42015-07-19 05:26:49 +02001746 if (ret) {
1747 /* Cannot see edge of failing read. */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001748 debug_cond(DLEVEL >= 2, "%s:%d: end: failed\n",
Marek Vasutf2b02d42015-07-19 05:26:49 +02001749 __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001750 }
1751
Marek Vasutf2b02d42015-07-19 05:26:49 +02001752 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001753}
1754
Marek Vasutfea03c32015-07-19 04:14:32 +02001755/**
1756 * sdr_find_window_center() - Find center of the working DQS window.
1757 * @grp: Read/Write group
1758 * @work_bgn: First working settings
1759 * @work_end: Last working settings
Marek Vasutfea03c32015-07-19 04:14:32 +02001760 *
1761 * Find center of the working DQS enable window.
1762 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001763static int sdr_find_window_center(struct socfpga_sdrseq *seq,
1764 const u32 grp, const u32 work_bgn,
Marek Vasut42e43ab2015-07-19 06:37:51 +02001765 const u32 work_end)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001766{
Marek Vasuta50d5d72015-07-19 07:35:36 +02001767 u32 work_mid;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001768 int tmp_delay = 0;
Marek Vasutd996e802015-07-19 02:56:59 +02001769 int i, p, d;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001770
Marek Vasutd996e802015-07-19 02:56:59 +02001771 work_mid = (work_bgn + work_end) / 2;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001772
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001773 debug_cond(DLEVEL >= 2, "work_bgn=%d work_end=%d work_mid=%d\n",
Marek Vasutd996e802015-07-19 02:56:59 +02001774 work_bgn, work_end, work_mid);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001775 /* Get the middle delay to be less than a VFIFO delay */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001776 tmp_delay = (seq->iocfg->dqs_en_phase_max + 1)
1777 * seq->iocfg->delay_per_opa_tap;
Marek Vasutd996e802015-07-19 02:56:59 +02001778
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001779 debug_cond(DLEVEL >= 2, "vfifo ptap delay %d\n", tmp_delay);
Marek Vasutea4c4bb2015-07-19 04:04:33 +02001780 work_mid %= tmp_delay;
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001781 debug_cond(DLEVEL >= 2, "new work_mid %d\n", work_mid);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001782
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001783 tmp_delay = rounddown(work_mid, seq->iocfg->delay_per_opa_tap);
1784 if (tmp_delay > seq->iocfg->dqs_en_phase_max
1785 * seq->iocfg->delay_per_opa_tap) {
1786 tmp_delay = seq->iocfg->dqs_en_phase_max
1787 * seq->iocfg->delay_per_opa_tap;
1788 }
1789 p = tmp_delay / seq->iocfg->delay_per_opa_tap;
Marek Vasutd996e802015-07-19 02:56:59 +02001790
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001791 debug_cond(DLEVEL >= 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
Marek Vasutea4c4bb2015-07-19 04:04:33 +02001792
Marek Vasutc85b9b32015-08-02 19:47:01 +02001793 d = DIV_ROUND_UP(work_mid - tmp_delay,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001794 seq->iocfg->delay_per_dqs_en_dchain_tap);
1795 if (d > seq->iocfg->dqs_en_delay_max)
1796 d = seq->iocfg->dqs_en_delay_max;
1797 tmp_delay += d * seq->iocfg->delay_per_dqs_en_dchain_tap;
Marek Vasutea4c4bb2015-07-19 04:04:33 +02001798
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001799 debug_cond(DLEVEL >= 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001800
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001801 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
1802 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001803
1804 /*
1805 * push vfifo until we can successfully calibrate. We can do this
1806 * because the largest possible margin in 1 VFIFO cycle.
1807 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001808 for (i = 0; i < seq->misccfg->read_valid_fifo_size; i++) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001809 debug_cond(DLEVEL >= 2, "find_dqs_en_phase: center\n");
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001810 if (rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001811 PASS_ONE_BIT,
Marek Vasuta50d5d72015-07-19 07:35:36 +02001812 0)) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001813 debug_cond(DLEVEL >= 2,
Marek Vasut42e43ab2015-07-19 06:37:51 +02001814 "%s:%d center: found: ptap=%u dtap=%u\n",
1815 __func__, __LINE__, p, d);
Marek Vasutfea03c32015-07-19 04:14:32 +02001816 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001817 }
1818
Marek Vasutfea03c32015-07-19 04:14:32 +02001819 /* Fiddle with FIFO. */
Marek Vasut42e43ab2015-07-19 06:37:51 +02001820 rw_mgr_incr_vfifo(grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001821 }
1822
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001823 debug_cond(DLEVEL >= 2, "%s:%d center: failed.\n",
Marek Vasutfea03c32015-07-19 04:14:32 +02001824 __func__, __LINE__);
1825 return -EINVAL;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001826}
1827
Marek Vasutec4bbd32015-07-20 09:11:09 +02001828/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001829 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to
1830 * use
Marek Vasutec4bbd32015-07-20 09:11:09 +02001831 * @grp: Read/Write Group
1832 *
1833 * Find a good DQS enable to use.
1834 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001835static int
1836rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(struct socfpga_sdrseq *seq,
1837 const u32 grp)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001838{
Marek Vasut59729a62015-07-20 09:20:20 +02001839 u32 d, p, i;
1840 u32 dtaps_per_ptap;
1841 u32 work_bgn, work_end;
Marek Vasuteb447cb2015-08-10 23:01:43 +02001842 u32 found_passing_read, found_failing_read = 0, initial_failing_dtap;
Marek Vasut59729a62015-07-20 09:20:20 +02001843 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001844
1845 debug("%s:%d %u\n", __func__, __LINE__, grp);
1846
1847 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1848
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001849 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, 0);
1850 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001851
Marek Vasut4896bcc2015-07-19 02:42:21 +02001852 /* Step 0: Determine number of delay taps for each phase tap. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001853 dtaps_per_ptap = seq->iocfg->delay_per_opa_tap /
1854 seq->iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001855
Marek Vasut4896bcc2015-07-19 02:42:21 +02001856 /* Step 1: First push vfifo until we get a failing read. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001857 find_vfifo_failing_read(seq, grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001858
Marek Vasut4896bcc2015-07-19 02:42:21 +02001859 /* Step 2: Find first working phase, increment in ptaps. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001860 work_bgn = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001861 ret = sdr_working_phase(seq, grp, &work_bgn, &d, &p, &i);
Marek Vasut28dbf122015-07-20 09:20:42 +02001862 if (ret)
1863 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001864
1865 work_end = work_bgn;
1866
1867 /*
Marek Vasut4896bcc2015-07-19 02:42:21 +02001868 * If d is 0 then the working window covers a phase tap and we can
1869 * follow the old procedure. Otherwise, we've found the beginning
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001870 * and we need to increment the dtaps until we find the end.
1871 */
1872 if (d == 0) {
Marek Vasut4896bcc2015-07-19 02:42:21 +02001873 /*
1874 * Step 3a: If we have room, back off by one and
1875 * increment in dtaps.
1876 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001877 sdr_backup_phase(seq, grp, &work_bgn, &p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001878
Marek Vasut4896bcc2015-07-19 02:42:21 +02001879 /*
1880 * Step 4a: go forward from working phase to non working
1881 * phase, increment in ptaps.
1882 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001883 ret = sdr_nonworking_phase(seq, grp, &work_end, &p, &i);
Marek Vasut28dbf122015-07-20 09:20:42 +02001884 if (ret)
1885 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001886
Marek Vasut4896bcc2015-07-19 02:42:21 +02001887 /* Step 5a: Back off one from last, increment in dtaps. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001888
1889 /* Special case code for backing up a phase */
1890 if (p == 0) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001891 p = seq->iocfg->dqs_en_phase_max;
1892 rw_mgr_decr_vfifo(seq, grp);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001893 } else {
1894 p = p - 1;
1895 }
1896
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001897 work_end -= seq->iocfg->delay_per_opa_tap;
1898 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001899
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001900 d = 0;
1901
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001902 debug_cond(DLEVEL >= 2, "%s:%d p: ptap=%u\n",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001903 __func__, __LINE__, p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001904 }
1905
Marek Vasut4896bcc2015-07-19 02:42:21 +02001906 /* The dtap increment to find the failing edge is done here. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001907 sdr_find_phase_delay(seq, 0, 1, grp, &work_end,
1908 seq->iocfg->delay_per_dqs_en_dchain_tap, &d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001909
1910 /* Go back to working dtap */
1911 if (d != 0)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001912 work_end -= seq->iocfg->delay_per_dqs_en_dchain_tap;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001913
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001914 debug_cond(DLEVEL >= 2,
Marek Vasut4896bcc2015-07-19 02:42:21 +02001915 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
1916 __func__, __LINE__, p, d - 1, work_end);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001917
1918 if (work_end < work_bgn) {
1919 /* nil range */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001920 debug_cond(DLEVEL >= 2, "%s:%d end-2: failed\n",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001921 __func__, __LINE__);
Marek Vasut28dbf122015-07-20 09:20:42 +02001922 return -EINVAL;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001923 }
1924
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001925 debug_cond(DLEVEL >= 2, "%s:%d found range [%u,%u]\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001926 __func__, __LINE__, work_bgn, work_end);
1927
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001928 /*
Marek Vasut4896bcc2015-07-19 02:42:21 +02001929 * We need to calculate the number of dtaps that equal a ptap.
1930 * To do that we'll back up a ptap and re-find the edge of the
1931 * window using dtaps
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001932 */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001933 debug_cond(DLEVEL >= 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001934 __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001935
1936 /* Special case code for backing up a phase */
1937 if (p == 0) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001938 p = seq->iocfg->dqs_en_phase_max;
1939 rw_mgr_decr_vfifo(seq, grp);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001940 debug_cond(DLEVEL >= 2, "%s:%d backedup cycle/phase: p=%u\n",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001941 __func__, __LINE__, p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001942 } else {
1943 p = p - 1;
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001944 debug_cond(DLEVEL >= 2, "%s:%d backedup phase only: p=%u",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001945 __func__, __LINE__, p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001946 }
1947
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001948 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001949
1950 /*
1951 * Increase dtap until we first see a passing read (in case the
Marek Vasut4896bcc2015-07-19 02:42:21 +02001952 * window is smaller than a ptap), and then a failing read to
1953 * mark the edge of the window again.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001954 */
1955
Marek Vasut4896bcc2015-07-19 02:42:21 +02001956 /* Find a passing read. */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001957 debug_cond(DLEVEL >= 2, "%s:%d find passing read\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001958 __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001959
Marek Vasut6ff36b72015-07-19 07:27:06 +02001960 initial_failing_dtap = d;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001961
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001962 found_passing_read = !sdr_find_phase_delay(seq, 1, 1, grp, NULL, 0, &d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001963 if (found_passing_read) {
Marek Vasut4896bcc2015-07-19 02:42:21 +02001964 /* Find a failing read. */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001965 debug_cond(DLEVEL >= 2, "%s:%d find failing read\n",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001966 __func__, __LINE__);
Marek Vasut6ff36b72015-07-19 07:27:06 +02001967 d++;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001968 found_failing_read = !sdr_find_phase_delay(seq, 0, 1, grp, NULL,
1969 0, &d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001970 } else {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001971 debug_cond(DLEVEL >= 1,
Marek Vasut4896bcc2015-07-19 02:42:21 +02001972 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
1973 __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001974 }
1975
1976 /*
1977 * The dynamically calculated dtaps_per_ptap is only valid if we
1978 * found a passing/failing read. If we didn't, it means d hit the max
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001979 * (seq->iocfg->dqs_en_delay_max). Otherwise, dtaps_per_ptap retains its
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001980 * statically calculated value.
1981 */
1982 if (found_passing_read && found_failing_read)
1983 dtaps_per_ptap = d - initial_failing_dtap;
1984
Marek Vasutb5450962015-07-12 21:05:08 +02001985 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02001986 debug_cond(DLEVEL >= 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
Marek Vasut4896bcc2015-07-19 02:42:21 +02001987 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001988
Marek Vasut4896bcc2015-07-19 02:42:21 +02001989 /* Step 6: Find the centre of the window. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02001990 ret = sdr_find_window_center(seq, grp, work_bgn, work_end);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001991
Marek Vasut28dbf122015-07-20 09:20:42 +02001992 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05001993}
1994
Marek Vasutb20a5062015-07-13 02:11:02 +02001995/**
Marek Vasut85cd4d72015-07-13 02:48:34 +02001996 * search_stop_check() - Check if the detected edge is valid
1997 * @write: Perform read (Stage 2) or write (Stage 3) calibration
1998 * @d: DQS delay
1999 * @rank_bgn: Rank number
2000 * @write_group: Write Group
2001 * @read_group: Read Group
2002 * @bit_chk: Resulting bit mask after the test
2003 * @sticky_bit_chk: Resulting sticky bit mask after the test
2004 * @use_read_test: Perform read test
2005 *
2006 * Test if the found edge is valid.
2007 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002008static u32 search_stop_check(struct socfpga_sdrseq *seq, const int write,
2009 const int d, const int rank_bgn,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002010 const u32 write_group, const u32 read_group,
2011 u32 *bit_chk, u32 *sticky_bit_chk,
2012 const u32 use_read_test)
2013{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002014 const u32 ratio = seq->rwcfg->mem_if_read_dqs_width /
2015 seq->rwcfg->mem_if_write_dqs_width;
2016 const u32 correct_mask = write ? seq->param.write_correct_mask :
2017 seq->param.read_correct_mask;
2018 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2019 seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasut85cd4d72015-07-13 02:48:34 +02002020 u32 ret;
2021 /*
2022 * Stop searching when the read test doesn't pass AND when
2023 * we've seen a passing read on every bit.
2024 */
2025 if (write) { /* WRITE-ONLY */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002026 ret = !rw_mgr_mem_calibrate_write_test(seq, rank_bgn,
2027 write_group, 0,
2028 PASS_ONE_BIT, bit_chk,
2029 0);
Marek Vasut85cd4d72015-07-13 02:48:34 +02002030 } else if (use_read_test) { /* READ-ONLY */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002031 ret = !rw_mgr_mem_calibrate_read_test(seq, rank_bgn, read_group,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002032 NUM_READ_PB_TESTS,
2033 PASS_ONE_BIT, bit_chk,
2034 0, 0);
2035 } else { /* READ-ONLY */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002036 rw_mgr_mem_calibrate_write_test(seq, rank_bgn, write_group, 0,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002037 PASS_ONE_BIT, bit_chk, 0);
2038 *bit_chk = *bit_chk >> (per_dqs *
2039 (read_group - (write_group * ratio)));
2040 ret = (*bit_chk == 0);
2041 }
2042 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2043 ret = ret && (*sticky_bit_chk == correct_mask);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002044 debug_cond(DLEVEL >= 2,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002045 "%s:%d center(left): dtap=%u => %u == %u && %u",
2046 __func__, __LINE__, d,
2047 *sticky_bit_chk, correct_mask, ret);
2048 return ret;
2049}
2050
2051/**
Marek Vasute624caf2015-07-13 02:38:15 +02002052 * search_left_edge() - Find left edge of DQ/DQS working phase
2053 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2054 * @rank_bgn: Rank number
2055 * @write_group: Write Group
2056 * @read_group: Read Group
2057 * @test_bgn: Rank number to begin the test
Marek Vasute624caf2015-07-13 02:38:15 +02002058 * @sticky_bit_chk: Resulting sticky bit mask after the test
2059 * @left_edge: Left edge of the DQ/DQS phase
2060 * @right_edge: Right edge of the DQ/DQS phase
2061 * @use_read_test: Perform read test
2062 *
2063 * Find left edge of DQ/DQS working phase.
2064 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002065static void search_left_edge(struct socfpga_sdrseq *seq, const int write,
2066 const int rank_bgn, const u32 write_group,
2067 const u32 read_group, const u32 test_bgn,
2068 u32 *sticky_bit_chk, int *left_edge,
2069 int *right_edge, const u32 use_read_test)
Marek Vasute624caf2015-07-13 02:38:15 +02002070{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002071 const u32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2072 seq->iocfg->io_in_delay_max;
2073 const u32 dqs_max = write ? seq->iocfg->io_out1_delay_max :
2074 seq->iocfg->dqs_in_delay_max;
2075 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2076 seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasutb69c2472015-07-18 20:34:00 +02002077 u32 stop, bit_chk;
Marek Vasute624caf2015-07-13 02:38:15 +02002078 int i, d;
2079
2080 for (d = 0; d <= dqs_max; d++) {
2081 if (write)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002082 scc_mgr_apply_group_dq_out1_delay(seq, d);
Marek Vasute624caf2015-07-13 02:38:15 +02002083 else
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002084 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, d);
Marek Vasute624caf2015-07-13 02:38:15 +02002085
2086 writel(0, &sdr_scc_mgr->update);
2087
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002088 stop = search_stop_check(seq, write, d, rank_bgn, write_group,
Marek Vasutb69c2472015-07-18 20:34:00 +02002089 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002090 use_read_test);
Marek Vasute624caf2015-07-13 02:38:15 +02002091 if (stop == 1)
2092 break;
2093
2094 /* stop != 1 */
2095 for (i = 0; i < per_dqs; i++) {
Marek Vasutb69c2472015-07-18 20:34:00 +02002096 if (bit_chk & 1) {
Marek Vasute624caf2015-07-13 02:38:15 +02002097 /*
2098 * Remember a passing test as
2099 * the left_edge.
2100 */
2101 left_edge[i] = d;
2102 } else {
2103 /*
2104 * If a left edge has not been seen
2105 * yet, then a future passing test
2106 * will mark this edge as the right
2107 * edge.
2108 */
2109 if (left_edge[i] == delay_max + 1)
2110 right_edge[i] = -(d + 1);
2111 }
Marek Vasutb69c2472015-07-18 20:34:00 +02002112 bit_chk >>= 1;
Marek Vasute624caf2015-07-13 02:38:15 +02002113 }
2114 }
2115
2116 /* Reset DQ delay chains to 0 */
2117 if (write)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002118 scc_mgr_apply_group_dq_out1_delay(seq, 0);
Marek Vasute624caf2015-07-13 02:38:15 +02002119 else
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002120 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, 0);
Marek Vasute624caf2015-07-13 02:38:15 +02002121
2122 *sticky_bit_chk = 0;
2123 for (i = per_dqs - 1; i >= 0; i--) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002124 debug_cond(DLEVEL >= 2,
Marek Vasute624caf2015-07-13 02:38:15 +02002125 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2126 __func__, __LINE__, i, left_edge[i],
2127 i, right_edge[i]);
2128
2129 /*
2130 * Check for cases where we haven't found the left edge,
2131 * which makes our assignment of the the right edge invalid.
2132 * Reset it to the illegal value.
2133 */
2134 if ((left_edge[i] == delay_max + 1) &&
2135 (right_edge[i] != delay_max + 1)) {
2136 right_edge[i] = delay_max + 1;
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002137 debug_cond(DLEVEL >= 2,
Marek Vasute624caf2015-07-13 02:38:15 +02002138 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2139 __func__, __LINE__, i, right_edge[i]);
2140 }
2141
2142 /*
2143 * Reset sticky bit
2144 * READ: except for bits where we have seen both
2145 * the left and right edge.
2146 * WRITE: except for bits where we have seen the
2147 * left edge.
2148 */
2149 *sticky_bit_chk <<= 1;
2150 if (write) {
2151 if (left_edge[i] != delay_max + 1)
2152 *sticky_bit_chk |= 1;
2153 } else {
2154 if ((left_edge[i] != delay_max + 1) &&
2155 (right_edge[i] != delay_max + 1))
2156 *sticky_bit_chk |= 1;
2157 }
2158 }
Marek Vasute624caf2015-07-13 02:38:15 +02002159}
2160
2161/**
Marek Vasutb20a5062015-07-13 02:11:02 +02002162 * search_right_edge() - Find right edge of DQ/DQS working phase
2163 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2164 * @rank_bgn: Rank number
2165 * @write_group: Write Group
2166 * @read_group: Read Group
2167 * @start_dqs: DQS start phase
2168 * @start_dqs_en: DQS enable start phase
Marek Vasutb20a5062015-07-13 02:11:02 +02002169 * @sticky_bit_chk: Resulting sticky bit mask after the test
2170 * @left_edge: Left edge of the DQ/DQS phase
2171 * @right_edge: Right edge of the DQ/DQS phase
2172 * @use_read_test: Perform read test
2173 *
2174 * Find right edge of DQ/DQS working phase.
2175 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002176static int search_right_edge(struct socfpga_sdrseq *seq, const int write,
2177 const int rank_bgn, const u32 write_group,
2178 const u32 read_group, const int start_dqs,
2179 const int start_dqs_en, u32 *sticky_bit_chk,
2180 int *left_edge, int *right_edge,
2181 const u32 use_read_test)
Marek Vasutb20a5062015-07-13 02:11:02 +02002182{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002183 const u32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2184 seq->iocfg->io_in_delay_max;
2185 const u32 dqs_max = write ? seq->iocfg->io_out1_delay_max :
2186 seq->iocfg->dqs_in_delay_max;
2187 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2188 seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasutb69c2472015-07-18 20:34:00 +02002189 u32 stop, bit_chk;
Marek Vasutb20a5062015-07-13 02:11:02 +02002190 int i, d;
2191
2192 for (d = 0; d <= dqs_max - start_dqs; d++) {
2193 if (write) { /* WRITE-ONLY */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002194 scc_mgr_apply_group_dqs_io_and_oct_out1(seq,
2195 write_group,
Marek Vasutb20a5062015-07-13 02:11:02 +02002196 d + start_dqs);
2197 } else { /* READ-ONLY */
2198 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002199 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasut8af9ca02015-08-02 19:42:26 +02002200 u32 delay = d + start_dqs_en;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002201 if (delay > seq->iocfg->dqs_en_delay_max)
2202 delay = seq->iocfg->dqs_en_delay_max;
Marek Vasutb20a5062015-07-13 02:11:02 +02002203 scc_mgr_set_dqs_en_delay(read_group, delay);
2204 }
2205 scc_mgr_load_dqs(read_group);
2206 }
2207
2208 writel(0, &sdr_scc_mgr->update);
2209
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002210 stop = search_stop_check(seq, write, d, rank_bgn, write_group,
Marek Vasutb69c2472015-07-18 20:34:00 +02002211 read_group, &bit_chk, sticky_bit_chk,
Marek Vasut85cd4d72015-07-13 02:48:34 +02002212 use_read_test);
Marek Vasutb20a5062015-07-13 02:11:02 +02002213 if (stop == 1) {
2214 if (write && (d == 0)) { /* WRITE-ONLY */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002215 for (i = 0;
2216 i < seq->rwcfg->mem_dq_per_write_dqs;
Marek Vasutc85b9b32015-08-02 19:47:01 +02002217 i++) {
Marek Vasutb20a5062015-07-13 02:11:02 +02002218 /*
2219 * d = 0 failed, but it passed when
2220 * testing the left edge, so it must be
2221 * marginal, set it to -1
2222 */
2223 if (right_edge[i] == delay_max + 1 &&
2224 left_edge[i] != delay_max + 1)
2225 right_edge[i] = -1;
2226 }
2227 }
2228 break;
2229 }
2230
2231 /* stop != 1 */
2232 for (i = 0; i < per_dqs; i++) {
Marek Vasutb69c2472015-07-18 20:34:00 +02002233 if (bit_chk & 1) {
Marek Vasutb20a5062015-07-13 02:11:02 +02002234 /*
2235 * Remember a passing test as
2236 * the right_edge.
2237 */
2238 right_edge[i] = d;
2239 } else {
2240 if (d != 0) {
2241 /*
2242 * If a right edge has not
2243 * been seen yet, then a future
2244 * passing test will mark this
2245 * edge as the left edge.
2246 */
2247 if (right_edge[i] == delay_max + 1)
2248 left_edge[i] = -(d + 1);
2249 } else {
2250 /*
2251 * d = 0 failed, but it passed
2252 * when testing the left edge,
2253 * so it must be marginal, set
2254 * it to -1
2255 */
2256 if (right_edge[i] == delay_max + 1 &&
2257 left_edge[i] != delay_max + 1)
2258 right_edge[i] = -1;
2259 /*
2260 * If a right edge has not been
2261 * seen yet, then a future
2262 * passing test will mark this
2263 * edge as the left edge.
2264 */
2265 else if (right_edge[i] == delay_max + 1)
2266 left_edge[i] = -(d + 1);
2267 }
2268 }
2269
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002270 debug_cond(DLEVEL >= 2, "%s:%d center[r,d=%u]: ",
Marek Vasutb20a5062015-07-13 02:11:02 +02002271 __func__, __LINE__, d);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002272 debug_cond(DLEVEL >= 2,
Marek Vasutb20a5062015-07-13 02:11:02 +02002273 "bit_chk_test=%i left_edge[%u]: %d ",
Marek Vasutb69c2472015-07-18 20:34:00 +02002274 bit_chk & 1, i, left_edge[i]);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002275 debug_cond(DLEVEL >= 2, "right_edge[%u]: %d\n", i,
Marek Vasutb20a5062015-07-13 02:11:02 +02002276 right_edge[i]);
Marek Vasutb69c2472015-07-18 20:34:00 +02002277 bit_chk >>= 1;
Marek Vasutb20a5062015-07-13 02:11:02 +02002278 }
2279 }
2280
2281 /* Check that all bits have a window */
2282 for (i = 0; i < per_dqs; i++) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002283 debug_cond(DLEVEL >= 2,
Marek Vasutb20a5062015-07-13 02:11:02 +02002284 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2285 __func__, __LINE__, i, left_edge[i],
2286 i, right_edge[i]);
2287 if ((left_edge[i] == dqs_max + 1) ||
2288 (right_edge[i] == dqs_max + 1))
2289 return i + 1; /* FIXME: If we fail, retval > 0 */
2290 }
2291
2292 return 0;
2293}
2294
Marek Vasutaa0e6e12015-07-18 19:18:06 +02002295/**
2296 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2297 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2298 * @left_edge: Left edge of the DQ/DQS phase
2299 * @right_edge: Right edge of the DQ/DQS phase
2300 * @mid_min: Best DQ/DQS phase middle setting
2301 *
2302 * Find index and value of the middle of the DQ/DQS working phase.
2303 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002304static int get_window_mid_index(struct socfpga_sdrseq *seq,
2305 const int write, int *left_edge,
Marek Vasutaa0e6e12015-07-18 19:18:06 +02002306 int *right_edge, int *mid_min)
2307{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002308 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2309 seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasutaa0e6e12015-07-18 19:18:06 +02002310 int i, mid, min_index;
2311
2312 /* Find middle of window for each DQ bit */
2313 *mid_min = left_edge[0] - right_edge[0];
2314 min_index = 0;
2315 for (i = 1; i < per_dqs; i++) {
2316 mid = left_edge[i] - right_edge[i];
2317 if (mid < *mid_min) {
2318 *mid_min = mid;
2319 min_index = i;
2320 }
2321 }
2322
2323 /*
2324 * -mid_min/2 represents the amount that we need to move DQS.
2325 * If mid_min is odd and positive we'll need to add one to make
2326 * sure the rounding in further calculations is correct (always
2327 * bias to the right), so just add 1 for all positive values.
2328 */
2329 if (*mid_min > 0)
2330 (*mid_min)++;
2331 *mid_min = *mid_min / 2;
2332
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002333 debug_cond(DLEVEL >= 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
Marek Vasutaa0e6e12015-07-18 19:18:06 +02002334 __func__, __LINE__, *mid_min, min_index);
2335 return min_index;
2336}
2337
Marek Vasut89feb502015-07-18 19:46:26 +02002338/**
2339 * center_dq_windows() - Center the DQ/DQS windows
2340 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2341 * @left_edge: Left edge of the DQ/DQS phase
2342 * @right_edge: Right edge of the DQ/DQS phase
2343 * @mid_min: Adjusted DQ/DQS phase middle setting
2344 * @orig_mid_min: Original DQ/DQS phase middle setting
2345 * @min_index: DQ/DQS phase middle setting index
2346 * @test_bgn: Rank number to begin the test
2347 * @dq_margin: Amount of shift for the DQ
2348 * @dqs_margin: Amount of shift for the DQS
2349 *
2350 * Align the DQ/DQS windows in each group.
2351 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002352static void center_dq_windows(struct socfpga_sdrseq *seq,
2353 const int write, int *left_edge, int *right_edge,
Marek Vasut89feb502015-07-18 19:46:26 +02002354 const int mid_min, const int orig_mid_min,
2355 const int min_index, const int test_bgn,
2356 int *dq_margin, int *dqs_margin)
2357{
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002358 const s32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2359 seq->iocfg->io_in_delay_max;
2360 const s32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2361 seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasut66acabc2016-04-05 23:17:35 +02002362 const s32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
Marek Vasut89feb502015-07-18 19:46:26 +02002363 SCC_MGR_IO_IN_DELAY_OFFSET;
Marek Vasut66acabc2016-04-05 23:17:35 +02002364 const s32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
Marek Vasut89feb502015-07-18 19:46:26 +02002365
Marek Vasut66acabc2016-04-05 23:17:35 +02002366 s32 temp_dq_io_delay1;
Marek Vasut89feb502015-07-18 19:46:26 +02002367 int shift_dq, i, p;
2368
2369 /* Initialize data for export structures */
2370 *dqs_margin = delay_max + 1;
2371 *dq_margin = delay_max + 1;
2372
2373 /* add delay to bring centre of all DQ windows to the same "level" */
2374 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2375 /* Use values before divide by 2 to reduce round off error */
2376 shift_dq = (left_edge[i] - right_edge[i] -
2377 (left_edge[min_index] - right_edge[min_index]))/2 +
2378 (orig_mid_min - mid_min);
2379
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002380 debug_cond(DLEVEL >= 2,
Marek Vasut89feb502015-07-18 19:46:26 +02002381 "vfifo_center: before: shift_dq[%u]=%d\n",
2382 i, shift_dq);
2383
Marek Vasut66acabc2016-04-05 23:17:35 +02002384 temp_dq_io_delay1 = readl(addr + (i << 2));
Marek Vasut89feb502015-07-18 19:46:26 +02002385
2386 if (shift_dq + temp_dq_io_delay1 > delay_max)
Marek Vasut66acabc2016-04-05 23:17:35 +02002387 shift_dq = delay_max - temp_dq_io_delay1;
Marek Vasut89feb502015-07-18 19:46:26 +02002388 else if (shift_dq + temp_dq_io_delay1 < 0)
2389 shift_dq = -temp_dq_io_delay1;
2390
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002391 debug_cond(DLEVEL >= 2,
Marek Vasut89feb502015-07-18 19:46:26 +02002392 "vfifo_center: after: shift_dq[%u]=%d\n",
2393 i, shift_dq);
2394
2395 if (write)
Marek Vasutc85b9b32015-08-02 19:47:01 +02002396 scc_mgr_set_dq_out1_delay(i,
2397 temp_dq_io_delay1 + shift_dq);
Marek Vasut89feb502015-07-18 19:46:26 +02002398 else
Marek Vasutc85b9b32015-08-02 19:47:01 +02002399 scc_mgr_set_dq_in_delay(p,
2400 temp_dq_io_delay1 + shift_dq);
Marek Vasut89feb502015-07-18 19:46:26 +02002401
2402 scc_mgr_load_dq(p);
2403
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002404 debug_cond(DLEVEL >= 2,
Marek Vasut89feb502015-07-18 19:46:26 +02002405 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2406 left_edge[i] - shift_dq + (-mid_min),
2407 right_edge[i] + shift_dq - (-mid_min));
2408
2409 /* To determine values for export structures */
2410 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2411 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2412
2413 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2414 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2415 }
Marek Vasut89feb502015-07-18 19:46:26 +02002416}
2417
Marek Vasut9cdbb962015-07-21 04:27:32 +02002418/**
2419 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2420 * @rank_bgn: Rank number
2421 * @rw_group: Read/Write Group
2422 * @test_bgn: Rank at which the test begins
2423 * @use_read_test: Perform a read test
2424 * @update_fom: Update FOM
2425 *
2426 * Per-bit deskew DQ and centering.
2427 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002428static int rw_mgr_mem_calibrate_vfifo_center(struct socfpga_sdrseq *seq,
2429 const u32 rank_bgn,
2430 const u32 rw_group,
2431 const u32 test_bgn,
2432 const int use_read_test,
2433 const int update_fom)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002434{
Marek Vasutf1b8f712015-07-18 19:57:12 +02002435 const u32 addr =
2436 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
Marek Vasutdfed1e62015-07-18 20:42:27 +02002437 (rw_group << 2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002438 /*
2439 * Store these as signed since there are comparisons with
2440 * signed numbers.
2441 */
Marek Vasut8af9ca02015-08-02 19:42:26 +02002442 u32 sticky_bit_chk;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002443 s32 left_edge[seq->rwcfg->mem_dq_per_read_dqs];
2444 s32 right_edge[seq->rwcfg->mem_dq_per_read_dqs];
2445 s32 orig_mid_min, mid_min;
2446 s32 new_dqs, start_dqs, start_dqs_en = 0, final_dqs_en;
2447 s32 dq_margin, dqs_margin;
Marek Vasutf1b8f712015-07-18 19:57:12 +02002448 int i, min_index;
Marek Vasutb20a5062015-07-13 02:11:02 +02002449 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002450
Marek Vasutdfed1e62015-07-18 20:42:27 +02002451 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002452
Marek Vasutf1b8f712015-07-18 19:57:12 +02002453 start_dqs = readl(addr);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002454 if (seq->iocfg->shift_dqs_en_when_shift_dqs)
2455 start_dqs_en = readl(addr - seq->iocfg->dqs_en_delay_offset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002456
2457 /* set the left and right edge of each bit to an illegal value */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002458 /* use (seq->iocfg->io_in_delay_max + 1) as an illegal value */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002459 sticky_bit_chk = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002460 for (i = 0; i < seq->rwcfg->mem_dq_per_read_dqs; i++) {
2461 left_edge[i] = seq->iocfg->io_in_delay_max + 1;
2462 right_edge[i] = seq->iocfg->io_in_delay_max + 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002463 }
2464
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002465 /* Search for the left edge of the window for each bit */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002466 search_left_edge(seq, 0, rank_bgn, rw_group, rw_group, test_bgn,
Marek Vasutb69c2472015-07-18 20:34:00 +02002467 &sticky_bit_chk,
Marek Vasute624caf2015-07-13 02:38:15 +02002468 left_edge, right_edge, use_read_test);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002469
Marek Vasutca8ea372015-07-18 08:01:45 +02002470
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002471 /* Search for the right edge of the window for each bit */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002472 ret = search_right_edge(seq, 0, rank_bgn, rw_group, rw_group,
Marek Vasutb20a5062015-07-13 02:11:02 +02002473 start_dqs, start_dqs_en,
Marek Vasutb69c2472015-07-18 20:34:00 +02002474 &sticky_bit_chk,
Marek Vasutb20a5062015-07-13 02:11:02 +02002475 left_edge, right_edge, use_read_test);
2476 if (ret) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002477 /*
Marek Vasutb20a5062015-07-13 02:11:02 +02002478 * Restore delay chain settings before letting the loop
2479 * in rw_mgr_mem_calibrate_vfifo to retry different
2480 * dqs/ck relationships.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002481 */
Marek Vasutdfed1e62015-07-18 20:42:27 +02002482 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002483 if (seq->iocfg->shift_dqs_en_when_shift_dqs)
Marek Vasutdfed1e62015-07-18 20:42:27 +02002484 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002485
Marek Vasutdfed1e62015-07-18 20:42:27 +02002486 scc_mgr_load_dqs(rw_group);
Marek Vasutb20a5062015-07-13 02:11:02 +02002487 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002488
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002489 debug_cond(DLEVEL >= 1,
Marek Vasutb20a5062015-07-13 02:11:02 +02002490 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2491 __func__, __LINE__, i, left_edge[i], right_edge[i]);
2492 if (use_read_test) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002493 set_failing_group_stage(seq, rw_group *
2494 seq->rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutb20a5062015-07-13 02:11:02 +02002495 CAL_STAGE_VFIFO,
2496 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002497 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002498 set_failing_group_stage(seq, rw_group *
2499 seq->rwcfg->mem_dq_per_read_dqs + i,
Marek Vasutb20a5062015-07-13 02:11:02 +02002500 CAL_STAGE_VFIFO_AFTER_WRITES,
2501 CAL_SUBSTAGE_VFIFO_CENTER);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002502 }
Marek Vasutd29f8042015-07-18 20:44:28 +02002503 return -EIO;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002504 }
2505
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002506 min_index = get_window_mid_index(seq, 0, left_edge, right_edge,
2507 &mid_min);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002508
2509 /* Determine the amount we can change DQS (which is -mid_min) */
2510 orig_mid_min = mid_min;
2511 new_dqs = start_dqs - mid_min;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002512 if (new_dqs > seq->iocfg->dqs_in_delay_max)
2513 new_dqs = seq->iocfg->dqs_in_delay_max;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002514 else if (new_dqs < 0)
2515 new_dqs = 0;
2516
2517 mid_min = start_dqs - new_dqs;
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002518 debug_cond(DLEVEL >= 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002519 mid_min, new_dqs);
2520
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002521 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
2522 if (start_dqs_en - mid_min > seq->iocfg->dqs_en_delay_max)
Marek Vasutc85b9b32015-08-02 19:47:01 +02002523 mid_min += start_dqs_en - mid_min -
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002524 seq->iocfg->dqs_en_delay_max;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002525 else if (start_dqs_en - mid_min < 0)
2526 mid_min += start_dqs_en - mid_min;
2527 }
2528 new_dqs = start_dqs - mid_min;
2529
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002530 debug_cond(DLEVEL >= 1,
Marek Vasutca8ea372015-07-18 08:01:45 +02002531 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2532 start_dqs,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002533 seq->iocfg->shift_dqs_en_when_shift_dqs ? start_dqs_en : -1,
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002534 new_dqs, mid_min);
2535
Marek Vasut89feb502015-07-18 19:46:26 +02002536 /* Add delay to bring centre of all DQ windows to the same "level". */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002537 center_dq_windows(seq, 0, left_edge, right_edge, mid_min, orig_mid_min,
Marek Vasut89feb502015-07-18 19:46:26 +02002538 min_index, test_bgn, &dq_margin, &dqs_margin);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002539
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002540 /* Move DQS-en */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002541 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
Marek Vasutf1b8f712015-07-18 19:57:12 +02002542 final_dqs_en = start_dqs_en - mid_min;
Marek Vasutdfed1e62015-07-18 20:42:27 +02002543 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2544 scc_mgr_load_dqs(rw_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002545 }
2546
2547 /* Move DQS */
Marek Vasutdfed1e62015-07-18 20:42:27 +02002548 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2549 scc_mgr_load_dqs(rw_group);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002550 debug_cond(DLEVEL >= 2,
Marek Vasutca8ea372015-07-18 08:01:45 +02002551 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2552 __func__, __LINE__, dq_margin, dqs_margin);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002553
2554 /*
2555 * Do not remove this line as it makes sure all of our decisions
2556 * have been applied. Apply the update bit.
2557 */
Marek Vasutb5450962015-07-12 21:05:08 +02002558 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002559
Marek Vasutd29f8042015-07-18 20:44:28 +02002560 if ((dq_margin < 0) || (dqs_margin < 0))
2561 return -EINVAL;
2562
2563 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002564}
2565
Marek Vasutc27ea622015-07-17 03:16:45 +02002566/**
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002567 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the
2568 * device
Marek Vasut6ca5b962015-07-18 02:46:56 +02002569 * @rw_group: Read/Write Group
2570 * @phase: DQ/DQS phase
2571 *
2572 * Because initially no communication ca be reliably performed with the memory
2573 * device, the sequencer uses a guaranteed write mechanism to write data into
2574 * the memory device.
2575 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002576static int rw_mgr_mem_calibrate_guaranteed_write(struct socfpga_sdrseq *seq,
2577 const u32 rw_group,
Marek Vasut6ca5b962015-07-18 02:46:56 +02002578 const u32 phase)
2579{
Marek Vasut6ca5b962015-07-18 02:46:56 +02002580 int ret;
2581
2582 /* Set a particular DQ/DQS phase. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002583 scc_mgr_set_dqdqs_output_phase_all_ranks(seq, rw_group, phase);
Marek Vasut6ca5b962015-07-18 02:46:56 +02002584
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002585 debug_cond(DLEVEL >= 1, "%s:%d guaranteed write: g=%u p=%u\n",
Marek Vasut6ca5b962015-07-18 02:46:56 +02002586 __func__, __LINE__, rw_group, phase);
2587
2588 /*
2589 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2590 * Load up the patterns used by read calibration using the
2591 * current DQDQS phase.
2592 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002593 rw_mgr_mem_calibrate_read_load_patterns(seq, 0, 1);
Marek Vasut6ca5b962015-07-18 02:46:56 +02002594
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002595 if (seq->gbl.phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
Marek Vasut6ca5b962015-07-18 02:46:56 +02002596 return 0;
2597
2598 /*
2599 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2600 * Back-to-Back reads of the patterns used for calibration.
2601 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002602 ret = rw_mgr_mem_calibrate_read_test_patterns(seq, 0, rw_group, 1);
Marek Vasut55c4d692015-07-18 03:55:07 +02002603 if (ret)
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002604 debug_cond(DLEVEL >= 1,
Marek Vasut6ca5b962015-07-18 02:46:56 +02002605 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2606 __func__, __LINE__, rw_group, phase);
Marek Vasut55c4d692015-07-18 03:55:07 +02002607 return ret;
Marek Vasut6ca5b962015-07-18 02:46:56 +02002608}
2609
2610/**
Marek Vasutfeb5e652015-07-18 02:57:32 +02002611 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2612 * @rw_group: Read/Write Group
2613 * @test_bgn: Rank at which the test begins
2614 *
2615 * DQS enable calibration ensures reliable capture of the DQ signal without
2616 * glitches on the DQS line.
2617 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002618static int
2619rw_mgr_mem_calibrate_dqs_enable_calibration(struct socfpga_sdrseq *seq,
2620 const u32 rw_group,
2621 const u32 test_bgn)
Marek Vasutfeb5e652015-07-18 02:57:32 +02002622{
Marek Vasutfeb5e652015-07-18 02:57:32 +02002623 /*
2624 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2625 * DQS and DQS Eanble Signal Relationships.
2626 */
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002627
2628 /* We start at zero, so have one less dq to devide among */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002629 const u32 delay_step = seq->iocfg->io_in_delay_max /
2630 (seq->rwcfg->mem_dq_per_read_dqs - 1);
Marek Vasut28dbf122015-07-20 09:20:42 +02002631 int ret;
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002632 u32 i, p, d, r;
2633
2634 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2635
2636 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002637 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002638 r += NUM_RANKS_PER_SHADOW_REG) {
2639 for (i = 0, p = test_bgn, d = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002640 i < seq->rwcfg->mem_dq_per_read_dqs;
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002641 i++, p++, d += delay_step) {
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002642 debug_cond(DLEVEL >= 1,
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002643 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2644 __func__, __LINE__, rw_group, r, i, p, d);
2645
2646 scc_mgr_set_dq_in_delay(p, d);
2647 scc_mgr_load_dq(p);
2648 }
2649
2650 writel(0, &sdr_scc_mgr->update);
2651 }
2652
2653 /*
2654 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2655 * dq_in_delay values
2656 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002657 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(seq, rw_group);
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002658
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002659 debug_cond(DLEVEL >= 1,
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002660 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
Marek Vasut28dbf122015-07-20 09:20:42 +02002661 __func__, __LINE__, rw_group, !ret);
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002662
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002663 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002664 r += NUM_RANKS_PER_SHADOW_REG) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002665 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, 0);
Marek Vasut3aa19dc2015-07-18 04:28:42 +02002666 writel(0, &sdr_scc_mgr->update);
2667 }
2668
Marek Vasut28dbf122015-07-20 09:20:42 +02002669 return ret;
Marek Vasutfeb5e652015-07-18 02:57:32 +02002670}
2671
2672/**
Marek Vasut349ea3e2015-07-18 03:10:31 +02002673 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2674 * @rw_group: Read/Write Group
2675 * @test_bgn: Rank at which the test begins
2676 * @use_read_test: Perform a read test
2677 * @update_fom: Update FOM
2678 *
2679 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2680 * within a group.
2681 */
2682static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002683rw_mgr_mem_calibrate_dq_dqs_centering(struct socfpga_sdrseq *seq,
2684 const u32 rw_group, const u32 test_bgn,
Marek Vasut349ea3e2015-07-18 03:10:31 +02002685 const int use_read_test,
2686 const int update_fom)
2687
2688{
2689 int ret, grp_calibrated;
2690 u32 rank_bgn, sr;
2691
2692 /*
2693 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2694 * Read per-bit deskew can be done on a per shadow register basis.
2695 */
2696 grp_calibrated = 1;
2697 for (rank_bgn = 0, sr = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002698 rank_bgn < seq->rwcfg->mem_number_of_ranks;
Marek Vasut349ea3e2015-07-18 03:10:31 +02002699 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002700 ret = rw_mgr_mem_calibrate_vfifo_center(seq, rank_bgn, rw_group,
Marek Vasutdfed1e62015-07-18 20:42:27 +02002701 test_bgn,
Marek Vasut349ea3e2015-07-18 03:10:31 +02002702 use_read_test,
2703 update_fom);
Marek Vasutd29f8042015-07-18 20:44:28 +02002704 if (!ret)
Marek Vasut349ea3e2015-07-18 03:10:31 +02002705 continue;
2706
2707 grp_calibrated = 0;
2708 }
2709
2710 if (!grp_calibrated)
2711 return -EIO;
2712
2713 return 0;
2714}
2715
2716/**
Marek Vasutc27ea622015-07-17 03:16:45 +02002717 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2718 * @rw_group: Read/Write Group
2719 * @test_bgn: Rank at which the test begins
2720 *
2721 * Stage 1: Calibrate the read valid prediction FIFO.
2722 *
2723 * This function implements UniPHY calibration Stage 1, as explained in
2724 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002725 *
Marek Vasutc27ea622015-07-17 03:16:45 +02002726 * - read valid prediction will consist of finding:
2727 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2728 * - DQS input phase and DQS input delay (DQ/DQS Centering)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002729 * - we also do a per-bit deskew on the DQ lines.
2730 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002731static int rw_mgr_mem_calibrate_vfifo(struct socfpga_sdrseq *seq,
2732 const u32 rw_group, const u32 test_bgn)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002733{
Marek Vasut8af9ca02015-08-02 19:42:26 +02002734 u32 p, d;
2735 u32 dtaps_per_ptap;
2736 u32 failed_substage;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002737
Marek Vasut6ca5b962015-07-18 02:46:56 +02002738 int ret;
2739
Marek Vasute42fcea2015-07-17 04:24:18 +02002740 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002741
Marek Vasut912d43e2015-07-18 03:15:34 +02002742 /* Update info for sims */
2743 reg_file_set_group(rw_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002744 reg_file_set_stage(CAL_STAGE_VFIFO);
Marek Vasut912d43e2015-07-18 03:15:34 +02002745 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002746
Marek Vasut912d43e2015-07-18 03:15:34 +02002747 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2748
2749 /* USER Determine number of delay taps for each phase tap. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002750 dtaps_per_ptap = DIV_ROUND_UP(seq->iocfg->delay_per_opa_tap,
2751 seq->iocfg->delay_per_dqs_en_dchain_tap)
2752 - 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002753
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002754 for (d = 0; d <= dtaps_per_ptap; d += 2) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002755 /*
2756 * In RLDRAMX we may be messing the delay of pins in
Marek Vasute42fcea2015-07-17 04:24:18 +02002757 * the same write rw_group but outside of the current read
2758 * the rw_group, but that's ok because we haven't calibrated
Marek Vasutd7f49152015-07-17 03:44:26 +02002759 * output side yet.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002760 */
2761 if (d > 0) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002762 scc_mgr_apply_group_all_out_delay_add_all_ranks(seq,
2763 rw_group,
2764 d);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002765 }
2766
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002767 for (p = 0; p <= seq->iocfg->dqdqs_out_phase_max; p++) {
Marek Vasut6ca5b962015-07-18 02:46:56 +02002768 /* 1) Guaranteed Write */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002769 ret = rw_mgr_mem_calibrate_guaranteed_write(seq,
2770 rw_group,
2771 p);
Marek Vasut6ca5b962015-07-18 02:46:56 +02002772 if (ret)
2773 break;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002774
Marek Vasutfeb5e652015-07-18 02:57:32 +02002775 /* 2) DQS Enable Calibration */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002776 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(seq,
2777 rw_group,
Marek Vasutfeb5e652015-07-18 02:57:32 +02002778 test_bgn);
2779 if (ret) {
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002780 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2781 continue;
2782 }
2783
Marek Vasut349ea3e2015-07-18 03:10:31 +02002784 /* 3) Centering DQ/DQS */
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002785 /*
Marek Vasut349ea3e2015-07-18 03:10:31 +02002786 * If doing read after write calibration, do not update
2787 * FOM now. Do it then.
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002788 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002789 ret = rw_mgr_mem_calibrate_dq_dqs_centering(seq,
2790 rw_group,
2791 test_bgn,
2792 1, 0);
Marek Vasut349ea3e2015-07-18 03:10:31 +02002793 if (ret) {
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002794 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
Marek Vasut349ea3e2015-07-18 03:10:31 +02002795 continue;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002796 }
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002797
Marek Vasut349ea3e2015-07-18 03:10:31 +02002798 /* All done. */
2799 goto cal_done_ok;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002800 }
2801 }
2802
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002803 /* Calibration Stage 1 failed. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002804 set_failing_group_stage(seq, rw_group, CAL_STAGE_VFIFO,
2805 failed_substage);
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002806 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002807
Marek Vasutf2a4bda2015-07-17 03:50:17 +02002808 /* Calibration Stage 1 completed OK. */
2809cal_done_ok:
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002810 /*
2811 * Reset the delay chains back to zero if they have moved > 1
2812 * (check for > 1 because loop will increase d even when pass in
2813 * first case).
2814 */
2815 if (d > 2)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002816 scc_mgr_zero_group(seq, rw_group, 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002817
2818 return 1;
2819}
2820
Marek Vasut2da02572015-07-18 05:58:44 +02002821/**
2822 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2823 * @rw_group: Read/Write Group
2824 * @test_bgn: Rank at which the test begins
2825 *
2826 * Stage 3: DQ/DQS Centering.
2827 *
2828 * This function implements UniPHY calibration Stage 3, as explained in
2829 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2830 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002831static int rw_mgr_mem_calibrate_vfifo_end(struct socfpga_sdrseq *seq,
2832 const u32 rw_group,
Marek Vasut2da02572015-07-18 05:58:44 +02002833 const u32 test_bgn)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002834{
Marek Vasut2da02572015-07-18 05:58:44 +02002835 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002836
Marek Vasut2da02572015-07-18 05:58:44 +02002837 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002838
Marek Vasut2da02572015-07-18 05:58:44 +02002839 /* Update info for sims. */
2840 reg_file_set_group(rw_group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002841 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2842 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2843
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002844 ret = rw_mgr_mem_calibrate_dq_dqs_centering(seq, rw_group, test_bgn, 0,
2845 1);
Marek Vasut2da02572015-07-18 05:58:44 +02002846 if (ret)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002847 set_failing_group_stage(seq, rw_group,
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002848 CAL_STAGE_VFIFO_AFTER_WRITES,
2849 CAL_SUBSTAGE_VFIFO_CENTER);
Marek Vasut2da02572015-07-18 05:58:44 +02002850 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002851}
2852
Marek Vasuta3581272015-07-21 06:18:57 +02002853/**
2854 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2855 *
2856 * Stage 4: Minimize latency.
2857 *
2858 * This function implements UniPHY calibration Stage 4, as explained in
2859 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2860 * Calibrate LFIFO to find smallest read latency.
2861 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002862static u32 rw_mgr_mem_calibrate_lfifo(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002863{
Marek Vasuta3581272015-07-21 06:18:57 +02002864 int found_one = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002865
2866 debug("%s:%d\n", __func__, __LINE__);
2867
Marek Vasuta3581272015-07-21 06:18:57 +02002868 /* Update info for sims. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002869 reg_file_set_stage(CAL_STAGE_LFIFO);
2870 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2871
2872 /* Load up the patterns used by read calibration for all ranks */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002873 rw_mgr_mem_calibrate_read_load_patterns(seq, 0, 1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002874
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002875 do {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002876 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002877 debug_cond(DLEVEL >= 2, "%s:%d lfifo: read_lat=%u",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002878 __func__, __LINE__, seq->gbl.curr_read_lat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002879
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002880 if (!rw_mgr_mem_calibrate_read_test_all_ranks(seq, 0,
2881 NUM_READ_TESTS,
Marek Vasuta3581272015-07-21 06:18:57 +02002882 PASS_ALL_BITS, 1))
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002883 break;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002884
2885 found_one = 1;
Marek Vasuta3581272015-07-21 06:18:57 +02002886 /*
2887 * Reduce read latency and see if things are
2888 * working correctly.
2889 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002890 seq->gbl.curr_read_lat--;
2891 } while (seq->gbl.curr_read_lat > 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002892
Marek Vasuta3581272015-07-21 06:18:57 +02002893 /* Reset the fifos to get pointers to known state. */
Marek Vasutb5450962015-07-12 21:05:08 +02002894 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002895
2896 if (found_one) {
Marek Vasuta3581272015-07-21 06:18:57 +02002897 /* Add a fudge factor to the read latency that was determined */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002898 seq->gbl.curr_read_lat += 2;
2899 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002900 debug_cond(DLEVEL >= 2,
Marek Vasuta3581272015-07-21 06:18:57 +02002901 "%s:%d lfifo: success: using read_lat=%u\n",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002902 __func__, __LINE__, seq->gbl.curr_read_lat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002903 } else {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002904 set_failing_group_stage(seq, 0xff, CAL_STAGE_LFIFO,
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002905 CAL_SUBSTAGE_READ_LATENCY);
2906
Marek Vasut4df2d7b2016-04-04 21:21:05 +02002907 debug_cond(DLEVEL >= 2,
Marek Vasuta3581272015-07-21 06:18:57 +02002908 "%s:%d lfifo: failed at initial read_lat=%u\n",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002909 __func__, __LINE__, seq->gbl.curr_read_lat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002910 }
Marek Vasuta3581272015-07-21 06:18:57 +02002911
2912 return found_one;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05002913}
2914
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002915/**
2916 * search_window() - Search for the/part of the window with DM/DQS shift
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002917 * @search_dm: If 1, search for the DM shift, if 0, search for DQS
2918 * shift
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002919 * @rank_bgn: Rank number
2920 * @write_group: Write Group
2921 * @bgn_curr: Current window begin
2922 * @end_curr: Current window end
2923 * @bgn_best: Current best window begin
2924 * @end_best: Current best window end
2925 * @win_best: Size of the best window
2926 * @new_dqs: New DQS value (only applicable if search_dm = 0).
2927 *
2928 * Search for the/part of the window with DM/DQS shift.
2929 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002930static void search_window(struct socfpga_sdrseq *seq,
2931 const int search_dm, const u32 rank_bgn,
2932 const u32 write_group, int *bgn_curr, int *end_curr,
2933 int *bgn_best, int *end_best, int *win_best,
2934 int new_dqs)
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002935{
2936 u32 bit_chk;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002937 const int max = seq->iocfg->io_out1_delay_max - new_dqs;
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002938 int d, di;
2939
2940 /* Search for the/part of the window with DM/DQS shift. */
2941 for (di = max; di >= 0; di -= DELTA_D) {
2942 if (search_dm) {
2943 d = di;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002944 scc_mgr_apply_group_dm_out1_delay(seq, d);
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002945 } else {
2946 /* For DQS, we go from 0...max */
2947 d = max - di;
2948 /*
Marek Vasutc85b9b32015-08-02 19:47:01 +02002949 * Note: This only shifts DQS, so are we limiting
2950 * ourselves to width of DQ unnecessarily.
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002951 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002952 scc_mgr_apply_group_dqs_io_and_oct_out1(seq,
2953 write_group,
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002954 d + new_dqs);
2955 }
2956
2957 writel(0, &sdr_scc_mgr->update);
2958
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002959 if (rw_mgr_mem_calibrate_write_test(seq, rank_bgn, write_group,
2960 1, PASS_ALL_BITS, &bit_chk,
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002961 0)) {
2962 /* Set current end of the window. */
2963 *end_curr = search_dm ? -d : d;
2964
2965 /*
2966 * If a starting edge of our window has not been seen
2967 * this is our current start of the DM window.
2968 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002969 if (*bgn_curr == seq->iocfg->io_out1_delay_max + 1)
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002970 *bgn_curr = search_dm ? -d : d;
2971
2972 /*
2973 * If current window is bigger than best seen.
2974 * Set best seen to be current window.
2975 */
2976 if ((*end_curr - *bgn_curr + 1) > *win_best) {
2977 *win_best = *end_curr - *bgn_curr + 1;
2978 *bgn_best = *bgn_curr;
2979 *end_best = *end_curr;
2980 }
2981 } else {
2982 /* We just saw a failing test. Reset temp edge. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002983 *bgn_curr = seq->iocfg->io_out1_delay_max + 1;
2984 *end_curr = seq->iocfg->io_out1_delay_max + 1;
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002985
2986 /* Early exit is only applicable to DQS. */
2987 if (search_dm)
2988 continue;
2989
2990 /*
2991 * Early exit optimization: if the remaining delay
2992 * chain space is less than already seen largest
2993 * window we can exit.
2994 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02002995 if (*win_best - 1 > seq->iocfg->io_out1_delay_max
2996 - new_dqs - d)
Marek Vasut4e79b0a2015-07-21 05:26:58 +02002997 break;
2998 }
2999 }
3000}
3001
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003002/*
Marek Vasut2595b242015-07-21 05:33:49 +02003003 * rw_mgr_mem_calibrate_writes_center() - Center all windows
3004 * @rank_bgn: Rank number
3005 * @write_group: Write group
3006 * @test_bgn: Rank at which the test begins
3007 *
3008 * Center all windows. Do per-bit-deskew to possibly increase size of
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003009 * certain windows.
3010 */
Marek Vasutaffbc892015-07-21 05:00:42 +02003011static int
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003012rw_mgr_mem_calibrate_writes_center(struct socfpga_sdrseq *seq,
3013 const u32 rank_bgn, const u32 write_group,
Marek Vasutaffbc892015-07-21 05:00:42 +02003014 const u32 test_bgn)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003015{
Marek Vasut4e79b0a2015-07-21 05:26:58 +02003016 int i;
Marek Vasutaffbc892015-07-21 05:00:42 +02003017 u32 sticky_bit_chk;
3018 u32 min_index;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003019 int left_edge[seq->rwcfg->mem_dq_per_write_dqs];
3020 int right_edge[seq->rwcfg->mem_dq_per_write_dqs];
Marek Vasutaffbc892015-07-21 05:00:42 +02003021 int mid;
3022 int mid_min, orig_mid_min;
3023 int new_dqs, start_dqs;
3024 int dq_margin, dqs_margin, dm_margin;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003025 int bgn_curr = seq->iocfg->io_out1_delay_max + 1;
3026 int end_curr = seq->iocfg->io_out1_delay_max + 1;
3027 int bgn_best = seq->iocfg->io_out1_delay_max + 1;
3028 int end_best = seq->iocfg->io_out1_delay_max + 1;
Marek Vasutaffbc892015-07-21 05:00:42 +02003029 int win_best = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003030
Marek Vasutb20a5062015-07-13 02:11:02 +02003031 int ret;
3032
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003033 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
3034
3035 dm_margin = 0;
3036
Marek Vasut1bb221e2015-07-21 05:29:05 +02003037 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
3038 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003039 (seq->rwcfg->mem_dq_per_write_dqs << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003040
Marek Vasutaffbc892015-07-21 05:00:42 +02003041 /* Per-bit deskew. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003042
3043 /*
Marek Vasutaffbc892015-07-21 05:00:42 +02003044 * Set the left and right edge of each bit to an illegal value.
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003045 * Use (seq->iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003046 */
3047 sticky_bit_chk = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003048 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
3049 left_edge[i] = seq->iocfg->io_out1_delay_max + 1;
3050 right_edge[i] = seq->iocfg->io_out1_delay_max + 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003051 }
3052
Marek Vasutaffbc892015-07-21 05:00:42 +02003053 /* Search for the left edge of the window for each bit. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003054 search_left_edge(seq, 1, rank_bgn, write_group, 0, test_bgn,
Marek Vasutb69c2472015-07-18 20:34:00 +02003055 &sticky_bit_chk,
Marek Vasute624caf2015-07-13 02:38:15 +02003056 left_edge, right_edge, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003057
Marek Vasutaffbc892015-07-21 05:00:42 +02003058 /* Search for the right edge of the window for each bit. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003059 ret = search_right_edge(seq, 1, rank_bgn, write_group, 0,
Marek Vasutb20a5062015-07-13 02:11:02 +02003060 start_dqs, 0,
Marek Vasutb69c2472015-07-18 20:34:00 +02003061 &sticky_bit_chk,
Marek Vasutb20a5062015-07-13 02:11:02 +02003062 left_edge, right_edge, 0);
3063 if (ret) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003064 set_failing_group_stage(seq, test_bgn + ret - 1,
3065 CAL_STAGE_WRITES,
Marek Vasutb20a5062015-07-13 02:11:02 +02003066 CAL_SUBSTAGE_WRITES_CENTER);
Marek Vasutfc2ec8f2015-07-21 05:32:49 +02003067 return -EINVAL;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003068 }
3069
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003070 min_index = get_window_mid_index(seq, 1, left_edge, right_edge,
3071 &mid_min);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003072
Marek Vasutaffbc892015-07-21 05:00:42 +02003073 /* Determine the amount we can change DQS (which is -mid_min). */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003074 orig_mid_min = mid_min;
3075 new_dqs = start_dqs;
3076 mid_min = 0;
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003077 debug_cond(DLEVEL >= 1,
Marek Vasutaffbc892015-07-21 05:00:42 +02003078 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3079 __func__, __LINE__, start_dqs, new_dqs, mid_min);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003080
Marek Vasut89feb502015-07-18 19:46:26 +02003081 /* Add delay to bring centre of all DQ windows to the same "level". */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003082 center_dq_windows(seq, 1, left_edge, right_edge, mid_min, orig_mid_min,
Marek Vasut89feb502015-07-18 19:46:26 +02003083 min_index, 0, &dq_margin, &dqs_margin);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003084
3085 /* Move DQS */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003086 scc_mgr_apply_group_dqs_io_and_oct_out1(seq, write_group, new_dqs);
Marek Vasutb5450962015-07-12 21:05:08 +02003087 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003088
3089 /* Centre DM */
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003090 debug_cond(DLEVEL >= 2, "%s:%d write_center: DM\n", __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003091
3092 /*
Marek Vasutaffbc892015-07-21 05:00:42 +02003093 * Set the left and right edge of each bit to an illegal value.
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003094 * Use (seq->iocfg->io_out1_delay_max + 1) as an illegal value.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003095 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003096 left_edge[0] = seq->iocfg->io_out1_delay_max + 1;
3097 right_edge[0] = seq->iocfg->io_out1_delay_max + 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003098
Marek Vasutaffbc892015-07-21 05:00:42 +02003099 /* Search for the/part of the window with DM shift. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003100 search_window(seq, 1, rank_bgn, write_group, &bgn_curr, &end_curr,
Marek Vasut4e79b0a2015-07-21 05:26:58 +02003101 &bgn_best, &end_best, &win_best, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003102
Marek Vasutaffbc892015-07-21 05:00:42 +02003103 /* Reset DM delay chains to 0. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003104 scc_mgr_apply_group_dm_out1_delay(seq, 0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003105
3106 /*
3107 * Check to see if the current window nudges up aganist 0 delay.
3108 * If so we need to continue the search by shifting DQS otherwise DQS
Marek Vasutaffbc892015-07-21 05:00:42 +02003109 * search begins as a new search.
3110 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003111 if (end_curr != 0) {
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003112 bgn_curr = seq->iocfg->io_out1_delay_max + 1;
3113 end_curr = seq->iocfg->io_out1_delay_max + 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003114 }
3115
Marek Vasutaffbc892015-07-21 05:00:42 +02003116 /* Search for the/part of the window with DQS shifts. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003117 search_window(seq, 0, rank_bgn, write_group, &bgn_curr, &end_curr,
Marek Vasut4e79b0a2015-07-21 05:26:58 +02003118 &bgn_best, &end_best, &win_best, new_dqs);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003119
Marek Vasutaffbc892015-07-21 05:00:42 +02003120 /* Assign left and right edge for cal and reporting. */
3121 left_edge[0] = -1 * bgn_best;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003122 right_edge[0] = end_best;
3123
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003124 debug_cond(DLEVEL >= 2, "%s:%d dm_calib: left=%d right=%d\n",
Marek Vasutaffbc892015-07-21 05:00:42 +02003125 __func__, __LINE__, left_edge[0], right_edge[0]);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003126
Marek Vasutaffbc892015-07-21 05:00:42 +02003127 /* Move DQS (back to orig). */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003128 scc_mgr_apply_group_dqs_io_and_oct_out1(seq, write_group, new_dqs);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003129
3130 /* Move DM */
3131
Marek Vasutaffbc892015-07-21 05:00:42 +02003132 /* Find middle of window for the DM bit. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003133 mid = (left_edge[0] - right_edge[0]) / 2;
3134
Marek Vasutaffbc892015-07-21 05:00:42 +02003135 /* Only move right, since we are not moving DQS/DQ. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003136 if (mid < 0)
3137 mid = 0;
3138
Marek Vasutaffbc892015-07-21 05:00:42 +02003139 /* dm_marign should fail if we never find a window. */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003140 if (win_best == 0)
3141 dm_margin = -1;
3142 else
3143 dm_margin = left_edge[0] - mid;
3144
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003145 scc_mgr_apply_group_dm_out1_delay(seq, mid);
Marek Vasutb5450962015-07-12 21:05:08 +02003146 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003147
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003148 debug_cond(DLEVEL >= 2,
Marek Vasutaffbc892015-07-21 05:00:42 +02003149 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3150 __func__, __LINE__, left_edge[0], right_edge[0],
3151 mid, dm_margin);
3152 /* Export values. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003153 seq->gbl.fom_out += dq_margin + dqs_margin;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003154
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003155 debug_cond(DLEVEL >= 2,
Marek Vasutaffbc892015-07-21 05:00:42 +02003156 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3157 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003158
3159 /*
3160 * Do not remove this line as it makes sure all of our
3161 * decisions have been applied.
3162 */
Marek Vasutb5450962015-07-12 21:05:08 +02003163 writel(0, &sdr_scc_mgr->update);
Marek Vasutaffbc892015-07-21 05:00:42 +02003164
Marek Vasutfc2ec8f2015-07-21 05:32:49 +02003165 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3166 return -EINVAL;
3167
3168 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003169}
3170
Marek Vasut4a78cc72015-07-18 07:23:25 +02003171/**
3172 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3173 * @rank_bgn: Rank number
3174 * @group: Read/Write Group
3175 * @test_bgn: Rank at which the test begins
3176 *
3177 * Stage 2: Write Calibration Part One.
3178 *
3179 * This function implements UniPHY calibration Stage 2, as explained in
3180 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3181 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003182static int rw_mgr_mem_calibrate_writes(struct socfpga_sdrseq *seq,
3183 const u32 rank_bgn, const u32 group,
Marek Vasut4a78cc72015-07-18 07:23:25 +02003184 const u32 test_bgn)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003185{
Marek Vasut4a78cc72015-07-18 07:23:25 +02003186 int ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003187
Marek Vasut4a78cc72015-07-18 07:23:25 +02003188 /* Update info for sims */
3189 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3190
3191 reg_file_set_group(group);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003192 reg_file_set_stage(CAL_STAGE_WRITES);
3193 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3194
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003195 ret = rw_mgr_mem_calibrate_writes_center(seq, rank_bgn, group,
3196 test_bgn);
Marek Vasutfc2ec8f2015-07-21 05:32:49 +02003197 if (ret)
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003198 set_failing_group_stage(seq, group, CAL_STAGE_WRITES,
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003199 CAL_SUBSTAGE_WRITES_CENTER);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003200
Marek Vasutfc2ec8f2015-07-21 05:32:49 +02003201 return ret;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003202}
3203
Marek Vasutbe333bc2015-07-20 07:33:33 +02003204/**
3205 * mem_precharge_and_activate() - Precharge all banks and activate
3206 *
3207 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3208 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003209static void mem_precharge_and_activate(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003210{
Marek Vasutbe333bc2015-07-20 07:33:33 +02003211 int r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003212
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003213 for (r = 0; r < seq->rwcfg->mem_number_of_ranks; r++) {
Marek Vasutbe333bc2015-07-20 07:33:33 +02003214 /* Set rank. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003215 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_OFF);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003216
Marek Vasutbe333bc2015-07-20 07:33:33 +02003217 /* Precharge all banks. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003218 writel(seq->rwcfg->precharge_all, SDR_PHYGRP_RWMGRGRP_ADDRESS |
Marek Vasutb5450962015-07-12 21:05:08 +02003219 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003220
Marek Vasutb5450962015-07-12 21:05:08 +02003221 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003222 writel(seq->rwcfg->activate_0_and_1_wait1,
Marek Vasutc85b9b32015-08-02 19:47:01 +02003223 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003224
Marek Vasutb5450962015-07-12 21:05:08 +02003225 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003226 writel(seq->rwcfg->activate_0_and_1_wait2,
Marek Vasutc85b9b32015-08-02 19:47:01 +02003227 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003228
Marek Vasutbe333bc2015-07-20 07:33:33 +02003229 /* Activate rows. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003230 writel(seq->rwcfg->activate_0_and_1,
3231 SDR_PHYGRP_RWMGRGRP_ADDRESS |
3232 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003233 }
3234}
3235
Marek Vasut0f0840d2015-07-17 01:57:41 +02003236/**
3237 * mem_init_latency() - Configure memory RLAT and WLAT settings
3238 *
3239 * Configure memory RLAT and WLAT parameters.
3240 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003241static void mem_init_latency(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003242{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003243 /*
Marek Vasut0f0840d2015-07-17 01:57:41 +02003244 * For AV/CV, LFIFO is hardened and always runs at full rate
3245 * so max latency in AFI clocks, used here, is correspondingly
3246 * smaller.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003247 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003248 const u32 max_latency = (1 << seq->misccfg->max_latency_count_width)
3249 - 1;
Marek Vasut0f0840d2015-07-17 01:57:41 +02003250 u32 rlat, wlat;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003251
Marek Vasut0f0840d2015-07-17 01:57:41 +02003252 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003253
3254 /*
Marek Vasut0f0840d2015-07-17 01:57:41 +02003255 * Read in write latency.
3256 * WL for Hard PHY does not include additive latency.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003257 */
Marek Vasut0f0840d2015-07-17 01:57:41 +02003258 wlat = readl(&data_mgr->t_wl_add);
3259 wlat += readl(&data_mgr->mem_t_add);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003260
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003261 seq->gbl.rw_wl_nop_cycles = wlat - 1;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003262
Marek Vasut0f0840d2015-07-17 01:57:41 +02003263 /* Read in readl latency. */
3264 rlat = readl(&data_mgr->t_rl_add);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003265
Marek Vasut0f0840d2015-07-17 01:57:41 +02003266 /* Set a pretty high read latency initially. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003267 seq->gbl.curr_read_lat = rlat + 16;
3268 if (seq->gbl.curr_read_lat > max_latency)
3269 seq->gbl.curr_read_lat = max_latency;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003270
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003271 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003272
Marek Vasut0f0840d2015-07-17 01:57:41 +02003273 /* Advertise write latency. */
3274 writel(wlat, &phy_mgr_cfg->afi_wlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003275}
3276
Marek Vasut60daef82015-07-26 10:54:15 +02003277/**
3278 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3279 *
3280 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3281 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003282static void mem_skip_calibrate(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003283{
Marek Vasut8af9ca02015-08-02 19:42:26 +02003284 u32 vfifo_offset;
3285 u32 i, j, r;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003286
3287 debug("%s:%d\n", __func__, __LINE__);
3288 /* Need to update every shadow register set used by the interface */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003289 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
Marek Vasut60daef82015-07-26 10:54:15 +02003290 r += NUM_RANKS_PER_SHADOW_REG) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003291 /*
3292 * Set output phase alignment settings appropriate for
3293 * skip calibration.
3294 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003295 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003296 scc_mgr_set_dqs_en_phase(i, 0);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003297 if (seq->iocfg->dll_chain_length == 6)
Marek Vasut7e8f8a72015-08-02 19:10:58 +02003298 scc_mgr_set_dqdqs_output_phase(i, 6);
3299 else
3300 scc_mgr_set_dqdqs_output_phase(i, 7);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003301 /*
3302 * Case:33398
3303 *
3304 * Write data arrives to the I/O two cycles before write
3305 * latency is reached (720 deg).
3306 * -> due to bit-slip in a/c bus
3307 * -> to allow board skew where dqs is longer than ck
3308 * -> how often can this happen!?
3309 * -> can claim back some ptaps for high freq
3310 * support if we can relax this, but i digress...
3311 *
3312 * The write_clk leads mem_ck by 90 deg
3313 * The minimum ptap of the OPA is 180 deg
3314 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3315 * The write_clk is always delayed by 2 ptaps
3316 *
3317 * Hence, to make DQS aligned to CK, we need to delay
3318 * DQS by:
Marek Vasutc85b9b32015-08-02 19:47:01 +02003319 * (720 - 90 - 180 - 2) *
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003320 * (360 / seq->iocfg->dll_chain_length)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003321 *
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003322 * Dividing the above by
3323 (360 / seq->iocfg->dll_chain_length)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003324 * gives us the number of ptaps, which simplies to:
3325 *
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003326 * (1.25 * seq->iocfg->dll_chain_length - 2)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003327 */
Marek Vasut60daef82015-07-26 10:54:15 +02003328 scc_mgr_set_dqdqs_output_phase(i,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003329 ((125 * seq->iocfg->dll_chain_length)
3330 / 100) - 2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003331 }
Marek Vasutb5450962015-07-12 21:05:08 +02003332 writel(0xff, &sdr_scc_mgr->dqs_ena);
3333 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003334
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003335 for (i = 0; i < seq->rwcfg->mem_if_write_dqs_width; i++) {
Marek Vasutb5450962015-07-12 21:05:08 +02003336 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3337 SCC_MGR_GROUP_COUNTER_OFFSET);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003338 }
Marek Vasutb5450962015-07-12 21:05:08 +02003339 writel(0xff, &sdr_scc_mgr->dq_ena);
3340 writel(0xff, &sdr_scc_mgr->dm_ena);
3341 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003342 }
3343
3344 /* Compensate for simulation model behaviour */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003345 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003346 scc_mgr_set_dqs_bus_in_delay(i, 10);
3347 scc_mgr_load_dqs(i);
3348 }
Marek Vasutb5450962015-07-12 21:05:08 +02003349 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003350
3351 /*
3352 * ArriaV has hard FIFOs that can only be initialized by incrementing
3353 * in sequencer.
3354 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003355 vfifo_offset = seq->misccfg->calib_vfifo_offset;
Marek Vasut60daef82015-07-26 10:54:15 +02003356 for (j = 0; j < vfifo_offset; j++)
Marek Vasutb5450962015-07-12 21:05:08 +02003357 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
Marek Vasutb5450962015-07-12 21:05:08 +02003358 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003359
3360 /*
Marek Vasut60daef82015-07-26 10:54:15 +02003361 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3362 * setting from generation-time constant.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003363 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003364 seq->gbl.curr_read_lat = seq->misccfg->calib_lfifo_offset;
3365 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003366}
3367
Marek Vasutd9fcf9a2015-07-20 04:34:51 +02003368/**
3369 * mem_calibrate() - Memory calibration entry point.
3370 *
3371 * Perform memory calibration.
3372 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003373static u32 mem_calibrate(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003374{
Marek Vasut8af9ca02015-08-02 19:42:26 +02003375 u32 i;
3376 u32 rank_bgn, sr;
3377 u32 write_group, write_test_bgn;
3378 u32 read_group, read_test_bgn;
3379 u32 run_groups, current_run;
3380 u32 failing_groups = 0;
3381 u32 group_failed = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003382
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003383 const u32 rwdqs_ratio = seq->rwcfg->mem_if_read_dqs_width /
3384 seq->rwcfg->mem_if_write_dqs_width;
Marek Vasutd6f28792015-07-17 02:21:47 +02003385
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003386 debug("%s:%d\n", __func__, __LINE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003387
Marek Vasut0f0840d2015-07-17 01:57:41 +02003388 /* Initialize the data settings */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003389 seq->gbl.error_substage = CAL_SUBSTAGE_NIL;
3390 seq->gbl.error_stage = CAL_STAGE_NIL;
3391 seq->gbl.error_group = 0xff;
3392 seq->gbl.fom_in = 0;
3393 seq->gbl.fom_out = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003394
Marek Vasut0f0840d2015-07-17 01:57:41 +02003395 /* Initialize WLAT and RLAT. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003396 mem_init_latency(seq);
Marek Vasut0f0840d2015-07-17 01:57:41 +02003397
3398 /* Initialize bit slips. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003399 mem_precharge_and_activate(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003400
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003401 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
Marek Vasutb5450962015-07-12 21:05:08 +02003402 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3403 SCC_MGR_GROUP_COUNTER_OFFSET);
Marek Vasutd4d3de22015-07-19 01:34:43 +02003404 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3405 if (i == 0)
3406 scc_mgr_set_hhp_extras();
3407
Marek Vasut0341de42015-07-17 02:06:20 +02003408 scc_set_bypass_mode(i);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003409 }
3410
Marek Vasutb984ee82015-07-17 02:07:12 +02003411 /* Calibration is skipped. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003412 if ((seq->dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003413 /*
3414 * Set VFIFO and LFIFO to instant-on settings in skip
3415 * calibration mode.
3416 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003417 mem_skip_calibrate(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003418
Marek Vasutb984ee82015-07-17 02:07:12 +02003419 /*
3420 * Do not remove this line as it makes sure all of our
3421 * decisions have been applied.
3422 */
3423 writel(0, &sdr_scc_mgr->update);
3424 return 1;
3425 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003426
Marek Vasutb984ee82015-07-17 02:07:12 +02003427 /* Calibration is not skipped. */
3428 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3429 /*
3430 * Zero all delay chain/phase settings for all
3431 * groups and all shadow register sets.
3432 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003433 scc_mgr_zero_all(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003434
Marek Vasuteb98b382015-08-02 18:27:21 +02003435 run_groups = ~0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003436
Marek Vasutb984ee82015-07-17 02:07:12 +02003437 for (write_group = 0, write_test_bgn = 0; write_group
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003438 < seq->rwcfg->mem_if_write_dqs_width; write_group++,
3439 write_test_bgn += seq->rwcfg->mem_dq_per_write_dqs) {
Marek Vasut0568f222015-07-17 02:50:56 +02003440 /* Initialize the group failure */
Marek Vasutb984ee82015-07-17 02:07:12 +02003441 group_failed = 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003442
Marek Vasutb984ee82015-07-17 02:07:12 +02003443 current_run = run_groups & ((1 <<
3444 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3445 run_groups = run_groups >>
3446 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003447
Marek Vasutb984ee82015-07-17 02:07:12 +02003448 if (current_run == 0)
3449 continue;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003450
Marek Vasutb984ee82015-07-17 02:07:12 +02003451 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3452 SCC_MGR_GROUP_COUNTER_OFFSET);
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003453 scc_mgr_zero_group(seq, write_group, 0);
Marek Vasutb984ee82015-07-17 02:07:12 +02003454
Marek Vasutd6f28792015-07-17 02:21:47 +02003455 for (read_group = write_group * rwdqs_ratio,
3456 read_test_bgn = 0;
Marek Vasut0568f222015-07-17 02:50:56 +02003457 read_group < (write_group + 1) * rwdqs_ratio;
Marek Vasutd6f28792015-07-17 02:21:47 +02003458 read_group++,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003459 read_test_bgn += seq->rwcfg->mem_dq_per_read_dqs) {
Marek Vasutd6f28792015-07-17 02:21:47 +02003460 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3461 continue;
3462
Marek Vasutb984ee82015-07-17 02:07:12 +02003463 /* Calibrate the VFIFO */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003464 if (rw_mgr_mem_calibrate_vfifo(seq, read_group,
Marek Vasutd6f28792015-07-17 02:21:47 +02003465 read_test_bgn))
3466 continue;
Marek Vasutb984ee82015-07-17 02:07:12 +02003467
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003468 if (!(seq->gbl.phy_debug_mode_flags &
Marek Vasutc85b9b32015-08-02 19:47:01 +02003469 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasutd6f28792015-07-17 02:21:47 +02003470 return 0;
Marek Vasut0568f222015-07-17 02:50:56 +02003471
3472 /* The group failed, we're done. */
3473 goto grp_failed;
Marek Vasutb984ee82015-07-17 02:07:12 +02003474 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003475
Marek Vasutb984ee82015-07-17 02:07:12 +02003476 /* Calibrate the output side */
Marek Vasut0568f222015-07-17 02:50:56 +02003477 for (rank_bgn = 0, sr = 0;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003478 rank_bgn < seq->rwcfg->mem_number_of_ranks;
Marek Vasut0568f222015-07-17 02:50:56 +02003479 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3480 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3481 continue;
Marek Vasutf04045f2015-07-17 02:31:04 +02003482
Marek Vasut0568f222015-07-17 02:50:56 +02003483 /* Not needed in quick mode! */
Marek Vasutc85b9b32015-08-02 19:47:01 +02003484 if (STATIC_CALIB_STEPS &
3485 CALIB_SKIP_DELAY_SWEEPS)
Marek Vasut0568f222015-07-17 02:50:56 +02003486 continue;
Marek Vasutf04045f2015-07-17 02:31:04 +02003487
Marek Vasut0568f222015-07-17 02:50:56 +02003488 /* Calibrate WRITEs */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003489 if (!rw_mgr_mem_calibrate_writes(seq, rank_bgn,
Marek Vasutc85b9b32015-08-02 19:47:01 +02003490 write_group,
3491 write_test_bgn))
Marek Vasut0568f222015-07-17 02:50:56 +02003492 continue;
Marek Vasutf04045f2015-07-17 02:31:04 +02003493
Marek Vasut0568f222015-07-17 02:50:56 +02003494 group_failed = 1;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003495 if (!(seq->gbl.phy_debug_mode_flags &
Marek Vasutc85b9b32015-08-02 19:47:01 +02003496 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasut0568f222015-07-17 02:50:56 +02003497 return 0;
Marek Vasutb984ee82015-07-17 02:07:12 +02003498 }
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003499
Marek Vasut0568f222015-07-17 02:50:56 +02003500 /* Some group failed, we're done. */
3501 if (group_failed)
3502 goto grp_failed;
Marek Vasut6db55732015-07-17 02:38:51 +02003503
Marek Vasut0568f222015-07-17 02:50:56 +02003504 for (read_group = write_group * rwdqs_ratio,
3505 read_test_bgn = 0;
3506 read_group < (write_group + 1) * rwdqs_ratio;
3507 read_group++,
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003508 read_test_bgn += seq->rwcfg->mem_dq_per_read_dqs) {
Marek Vasut0568f222015-07-17 02:50:56 +02003509 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3510 continue;
3511
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003512 if (!rw_mgr_mem_calibrate_vfifo_end(seq,
3513 read_group,
Marek Vasutc85b9b32015-08-02 19:47:01 +02003514 read_test_bgn))
Marek Vasut0568f222015-07-17 02:50:56 +02003515 continue;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003516
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003517 if (!(seq->gbl.phy_debug_mode_flags &
Marek Vasutc85b9b32015-08-02 19:47:01 +02003518 PHY_DEBUG_SWEEP_ALL_GROUPS))
Marek Vasut0568f222015-07-17 02:50:56 +02003519 return 0;
3520
3521 /* The group failed, we're done. */
3522 goto grp_failed;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003523 }
3524
Marek Vasut0568f222015-07-17 02:50:56 +02003525 /* No group failed, continue as usual. */
3526 continue;
3527
3528grp_failed: /* A group failed, increment the counter. */
3529 failing_groups++;
Marek Vasutb984ee82015-07-17 02:07:12 +02003530 }
3531
3532 /*
3533 * USER If there are any failing groups then report
3534 * the failure.
3535 */
3536 if (failing_groups != 0)
3537 return 0;
3538
Marek Vasutfc38d5c2015-07-17 02:40:21 +02003539 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3540 continue;
3541
Marek Vasutb984ee82015-07-17 02:07:12 +02003542 /* Calibrate the LFIFO */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003543 if (!rw_mgr_mem_calibrate_lfifo(seq))
Marek Vasutfc38d5c2015-07-17 02:40:21 +02003544 return 0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003545 }
3546
3547 /*
3548 * Do not remove this line as it makes sure all of our decisions
3549 * have been applied.
3550 */
Marek Vasutb5450962015-07-12 21:05:08 +02003551 writel(0, &sdr_scc_mgr->update);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003552 return 1;
3553}
3554
Marek Vasut092a1ef2015-07-17 01:20:21 +02003555/**
3556 * run_mem_calibrate() - Perform memory calibration
3557 *
3558 * This function triggers the entire memory calibration procedure.
3559 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003560static int run_mem_calibrate(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003561{
Marek Vasut092a1ef2015-07-17 01:20:21 +02003562 int pass;
Marek Vasut69469892016-04-05 23:41:56 +02003563 u32 ctrl_cfg;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003564
3565 debug("%s:%d\n", __func__, __LINE__);
3566
3567 /* Reset pass/fail status shown on afi_cal_success/fail */
Marek Vasutb5450962015-07-12 21:05:08 +02003568 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003569
Marek Vasut092a1ef2015-07-17 01:20:21 +02003570 /* Stop tracking manager. */
Marek Vasut69469892016-04-05 23:41:56 +02003571 ctrl_cfg = readl(&sdr_ctrl->ctrl_cfg);
3572 writel(ctrl_cfg & ~SDR_CTRLGRP_CTRLCFG_DQSTRKEN_MASK,
3573 &sdr_ctrl->ctrl_cfg);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003574
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003575 phy_mgr_initialize(seq);
3576 rw_mgr_mem_initialize(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003577
Marek Vasut092a1ef2015-07-17 01:20:21 +02003578 /* Perform the actual memory calibration. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003579 pass = mem_calibrate(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003580
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003581 mem_precharge_and_activate(seq);
Marek Vasutb5450962015-07-12 21:05:08 +02003582 writel(0, &phy_mgr_cmd->fifo_reset);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003583
Marek Vasut092a1ef2015-07-17 01:20:21 +02003584 /* Handoff. */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003585 rw_mgr_mem_handoff(seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003586 /*
Marek Vasut092a1ef2015-07-17 01:20:21 +02003587 * In Hard PHY this is a 2-bit control:
3588 * 0: AFI Mux Select
3589 * 1: DDIO Mux Select
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003590 */
Marek Vasut092a1ef2015-07-17 01:20:21 +02003591 writel(0x2, &phy_mgr_cfg->mux_sel);
3592
3593 /* Start tracking manager. */
Marek Vasut69469892016-04-05 23:41:56 +02003594 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Marek Vasut092a1ef2015-07-17 01:20:21 +02003595
3596 return pass;
3597}
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003598
Marek Vasut092a1ef2015-07-17 01:20:21 +02003599/**
3600 * debug_mem_calibrate() - Report result of memory calibration
3601 * @pass: Value indicating whether calibration passed or failed
3602 *
3603 * This function reports the results of the memory calibration
3604 * and writes debug information into the register file.
3605 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003606static void debug_mem_calibrate(struct socfpga_sdrseq *seq, int pass)
Marek Vasut092a1ef2015-07-17 01:20:21 +02003607{
Marek Vasut8af9ca02015-08-02 19:42:26 +02003608 u32 debug_info;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003609
3610 if (pass) {
Goldschmidt Simona4af9142018-01-25 06:04:44 +00003611 debug("%s: CALIBRATION PASSED\n", __FILE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003612
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003613 seq->gbl.fom_in /= 2;
3614 seq->gbl.fom_out /= 2;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003615
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003616 if (seq->gbl.fom_in > 0xff)
3617 seq->gbl.fom_in = 0xff;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003618
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003619 if (seq->gbl.fom_out > 0xff)
3620 seq->gbl.fom_out = 0xff;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003621
3622 /* Update the FOM in the register file */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003623 debug_info = seq->gbl.fom_in;
3624 debug_info |= seq->gbl.fom_out << 8;
Marek Vasutb5450962015-07-12 21:05:08 +02003625 writel(debug_info, &sdr_reg_file->fom);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003626
Marek Vasutb5450962015-07-12 21:05:08 +02003627 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3628 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003629 } else {
Goldschmidt Simona4af9142018-01-25 06:04:44 +00003630 debug("%s: CALIBRATION FAILED\n", __FILE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003631
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003632 debug_info = seq->gbl.error_stage;
3633 debug_info |= seq->gbl.error_substage << 8;
3634 debug_info |= seq->gbl.error_group << 16;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003635
Marek Vasutb5450962015-07-12 21:05:08 +02003636 writel(debug_info, &sdr_reg_file->failing_stage);
3637 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3638 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003639
3640 /* Update the failing group/stage in the register file */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003641 debug_info = seq->gbl.error_stage;
3642 debug_info |= seq->gbl.error_substage << 8;
3643 debug_info |= seq->gbl.error_group << 16;
Marek Vasutb5450962015-07-12 21:05:08 +02003644 writel(debug_info, &sdr_reg_file->failing_stage);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003645 }
3646
Goldschmidt Simona4af9142018-01-25 06:04:44 +00003647 debug("%s: Calibration complete\n", __FILE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003648}
3649
Marek Vasutea9771b2015-07-19 06:12:42 +02003650/**
3651 * hc_initialize_rom_data() - Initialize ROM data
3652 *
3653 * Initialize ROM data.
3654 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003655static void hc_initialize_rom_data(void)
3656{
Marek Vasut3384e742015-08-02 17:15:19 +02003657 unsigned int nelem = 0;
3658 const u32 *rom_init;
Marek Vasutea9771b2015-07-19 06:12:42 +02003659 u32 i, addr;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003660
Marek Vasut3384e742015-08-02 17:15:19 +02003661 socfpga_get_seq_inst_init(&rom_init, &nelem);
Marek Vasuta3340102015-07-12 19:03:33 +02003662 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
Marek Vasut3384e742015-08-02 17:15:19 +02003663 for (i = 0; i < nelem; i++)
3664 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003665
Marek Vasut3384e742015-08-02 17:15:19 +02003666 socfpga_get_seq_ac_init(&rom_init, &nelem);
Marek Vasuta3340102015-07-12 19:03:33 +02003667 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
Marek Vasut3384e742015-08-02 17:15:19 +02003668 for (i = 0; i < nelem; i++)
3669 writel(rom_init[i], addr + (i << 2));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003670}
3671
Marek Vasuta17ae0f2015-07-19 06:13:37 +02003672/**
3673 * initialize_reg_file() - Initialize SDR register file
3674 *
3675 * Initialize SDR register file.
3676 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003677static void initialize_reg_file(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003678{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003679 /* Initialize the register file with the correct data */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003680 writel(seq->misccfg->reg_file_init_seq_signature,
3681 &sdr_reg_file->signature);
Marek Vasutb5450962015-07-12 21:05:08 +02003682 writel(0, &sdr_reg_file->debug_data_addr);
3683 writel(0, &sdr_reg_file->cur_stage);
3684 writel(0, &sdr_reg_file->fom);
3685 writel(0, &sdr_reg_file->failing_stage);
3686 writel(0, &sdr_reg_file->debug1);
3687 writel(0, &sdr_reg_file->debug2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003688}
3689
Marek Vasut0c9f3cb2015-07-19 06:14:04 +02003690/**
3691 * initialize_hps_phy() - Initialize HPS PHY
3692 *
3693 * Initialize HPS PHY.
3694 */
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003695static void initialize_hps_phy(void)
3696{
Marek Vasut8af9ca02015-08-02 19:42:26 +02003697 u32 reg;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003698 /*
3699 * Tracking also gets configured here because it's in the
3700 * same register.
3701 */
Marek Vasut8af9ca02015-08-02 19:42:26 +02003702 u32 trk_sample_count = 7500;
3703 u32 trk_long_idle_sample_count = (10 << 16) | 100;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003704 /*
3705 * Format is number of outer loops in the 16 MSB, sample
3706 * count in 16 LSB.
3707 */
3708
3709 reg = 0;
3710 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3711 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3712 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3713 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3714 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3715 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3716 /*
3717 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3718 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3719 */
3720 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3721 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3722 trk_sample_count);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003723 writel(reg, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003724
3725 reg = 0;
3726 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3727 trk_sample_count >>
3728 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3729 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3730 trk_long_idle_sample_count);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003731 writel(reg, &sdr_ctrl->phy_ctrl1);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003732
3733 reg = 0;
3734 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3735 trk_long_idle_sample_count >>
3736 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
Marek Vasutcd5d38e2015-07-12 20:49:39 +02003737 writel(reg, &sdr_ctrl->phy_ctrl2);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003738}
3739
Marek Vasutb0563cf2015-07-17 00:45:11 +02003740/**
3741 * initialize_tracking() - Initialize tracking
3742 *
3743 * Initialize the register file with usable initial data.
3744 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003745static void initialize_tracking(struct socfpga_sdrseq *seq)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003746{
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003747 /*
Marek Vasutb0563cf2015-07-17 00:45:11 +02003748 * Initialize the register file with the correct data.
3749 * Compute usable version of value in case we skip full
3750 * computation later.
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003751 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003752 writel(DIV_ROUND_UP(seq->iocfg->delay_per_opa_tap,
3753 seq->iocfg->delay_per_dchain_tap) - 1,
Marek Vasutb0563cf2015-07-17 00:45:11 +02003754 &sdr_reg_file->dtaps_per_ptap);
3755
3756 /* trk_sample_count */
3757 writel(7500, &sdr_reg_file->trk_sample_count);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003758
Marek Vasutb0563cf2015-07-17 00:45:11 +02003759 /* longidle outer loop [15:0] */
3760 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
3761
3762 /*
3763 * longidle sample count [31:24]
3764 * trfc, worst case of 933Mhz 4Gb [23:16]
3765 * trcd, worst case [15:8]
3766 * vfifo wait [7:0]
3767 */
3768 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3769 &sdr_reg_file->delays);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003770
Marek Vasutb0563cf2015-07-17 00:45:11 +02003771 /* mux delay */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003772 writel((seq->rwcfg->idle << 24) | (seq->rwcfg->activate_1 << 16) |
3773 (seq->rwcfg->sgle_read << 8) | (seq->rwcfg->precharge_all << 0),
Marek Vasutb0563cf2015-07-17 00:45:11 +02003774 &sdr_reg_file->trk_rw_mgr_addr);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003775
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003776 writel(seq->rwcfg->mem_if_read_dqs_width,
Marek Vasutb0563cf2015-07-17 00:45:11 +02003777 &sdr_reg_file->trk_read_dqs_width);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003778
Marek Vasutb0563cf2015-07-17 00:45:11 +02003779 /* trefi [7:0] */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003780 writel((seq->rwcfg->refresh_all << 24) | (1000 << 0),
Marek Vasutb0563cf2015-07-17 00:45:11 +02003781 &sdr_reg_file->trk_rfsh);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003782}
3783
Simon Goldschmidt24910c32019-04-16 22:04:39 +02003784int sdram_calibration_full(struct socfpga_sdr *sdr)
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003785{
Marek Vasut8af9ca02015-08-02 19:42:26 +02003786 u32 pass;
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003787 struct socfpga_sdrseq seq;
Marek Vasut5da0f5b2015-07-17 01:05:36 +02003788
Simon Goldschmidt24910c32019-04-16 22:04:39 +02003789 /*
3790 * For size reasons, this file uses hard coded addresses.
3791 * Check if we are called with the correct address.
3792 */
3793 if (sdr != (struct socfpga_sdr *)SOCFPGA_SDR_ADDRESS)
3794 return -ENODEV;
3795
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003796 memset(&seq, 0, sizeof(seq));
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003797
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003798 seq.rwcfg = socfpga_get_sdram_rwmgr_config();
3799 seq.iocfg = socfpga_get_sdram_io_config();
3800 seq.misccfg = socfpga_get_sdram_misc_config();
Marek Vasut39b620e2015-08-02 18:12:08 +02003801
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003802 /* Set the calibration enabled by default */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003803 seq.gbl.phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003804 /*
3805 * Only sweep all groups (regardless of fail state) by default
3806 * Set enabled read test by default.
3807 */
3808#if DISABLE_GUARANTEED_READ
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003809 seq.gbl.phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003810#endif
3811 /* Initialize the register file */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003812 initialize_reg_file(&seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003813
3814 /* Initialize any PHY CSR */
3815 initialize_hps_phy();
3816
3817 scc_mgr_initialize();
3818
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003819 initialize_tracking(&seq);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003820
Goldschmidt Simona4af9142018-01-25 06:04:44 +00003821 debug("%s: Preparing to start memory calibration\n", __FILE__);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003822
3823 debug("%s:%d\n", __func__, __LINE__);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003824 debug_cond(DLEVEL >= 1,
Marek Vasut6283b4c2015-07-13 01:05:27 +02003825 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003826 seq.rwcfg->mem_number_of_ranks,
3827 seq.rwcfg->mem_number_of_cs_per_dimm,
3828 seq.rwcfg->mem_dq_per_read_dqs,
3829 seq.rwcfg->mem_dq_per_write_dqs,
3830 seq.rwcfg->mem_virtual_groups_per_read_dqs,
3831 seq.rwcfg->mem_virtual_groups_per_write_dqs);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003832 debug_cond(DLEVEL >= 1,
Marek Vasut6283b4c2015-07-13 01:05:27 +02003833 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003834 seq.rwcfg->mem_if_read_dqs_width,
3835 seq.rwcfg->mem_if_write_dqs_width,
3836 seq.rwcfg->mem_data_width, seq.rwcfg->mem_data_mask_width,
3837 seq.iocfg->delay_per_opa_tap,
3838 seq.iocfg->delay_per_dchain_tap);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003839 debug_cond(DLEVEL >= 1, "dtap_dqsen_delay=%u, dll=%u",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003840 seq.iocfg->delay_per_dqs_en_dchain_tap,
3841 seq.iocfg->dll_chain_length);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003842 debug_cond(DLEVEL >= 1,
Marek Vasutc85b9b32015-08-02 19:47:01 +02003843 "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003844 seq.iocfg->dqs_en_phase_max, seq.iocfg->dqdqs_out_phase_max,
3845 seq.iocfg->dqs_en_delay_max, seq.iocfg->dqs_in_delay_max);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003846 debug_cond(DLEVEL >= 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003847 seq.iocfg->io_in_delay_max, seq.iocfg->io_out1_delay_max,
3848 seq.iocfg->io_out2_delay_max);
Marek Vasut4df2d7b2016-04-04 21:21:05 +02003849 debug_cond(DLEVEL >= 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003850 seq.iocfg->dqs_in_reserve, seq.iocfg->dqs_out_reserve);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003851
3852 hc_initialize_rom_data();
3853
3854 /* update info for sims */
3855 reg_file_set_stage(CAL_STAGE_NIL);
3856 reg_file_set_group(0);
3857
3858 /*
3859 * Load global needed for those actions that require
3860 * some dynamic calibration support.
3861 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003862 seq.dyn_calib_steps = STATIC_CALIB_STEPS;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003863 /*
3864 * Load global to allow dynamic selection of delay loop settings
3865 * based on calibration mode.
3866 */
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003867 if (!(seq.dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3868 seq.skip_delay_mask = 0xff;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003869 else
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003870 seq.skip_delay_mask = 0x0;
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003871
Simon Goldschmidt2be4a3e2019-07-11 21:18:12 +02003872 pass = run_mem_calibrate(&seq);
3873 debug_mem_calibrate(&seq, pass);
Dinh Nguyen135cc7f2015-06-02 22:52:49 -05003874 return pass;
3875}