1 // SPDX-License-Identifier: BSD-3-Clause
3 * Copyright Altera Corporation (C) 2012-2015
9 #include <asm/arch/sdram.h>
12 #include "sequencer.h"
14 static const struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
15 (struct socfpga_sdr_rw_load_manager *)
16 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
17 static const struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs
18 = (struct socfpga_sdr_rw_load_jump_manager *)
19 (SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
20 static const struct socfpga_sdr_reg_file *sdr_reg_file =
21 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
22 static const struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
23 (struct socfpga_sdr_scc_mgr *)
24 (SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
25 static const struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
26 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
27 static const struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
28 (struct socfpga_phy_mgr_cfg *)
29 (SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
30 static const struct socfpga_data_mgr *data_mgr =
31 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
32 static const struct socfpga_sdr_ctrl *sdr_ctrl =
33 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
38 * In order to reduce ROM size, most of the selectable calibration steps are
39 * decided at compile time based on the user's calibration mode selection,
40 * as captured by the STATIC_CALIB_STEPS selection below.
42 * However, to support simulation-time selection of fast simulation mode, where
43 * we skip everything except the bare minimum, we need a few of the steps to
44 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
45 * check, which is based on the rtl-supplied value, or we dynamically compute
46 * the value to use based on the dynamically-chosen calibration mode
50 #define STATIC_IN_RTL_SIM 0
51 #define STATIC_SKIP_DELAY_LOOPS 0
53 #define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
54 STATIC_SKIP_DELAY_LOOPS)
56 #define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
57 ((non_skip_value) & seq->skip_delay_mask)
59 bool dram_is_ddr(const u8 ddr)
61 const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
62 const u8 type = (cfg->ctrl_cfg >> SDR_CTRLGRP_CTRLCFG_MEMTYPE_LSB) &
63 SDR_CTRLGRP_CTRLCFG_MEMTYPE_MASK;
65 if (ddr == 2 && type == 1) /* DDR2 */
68 if (ddr == 3 && type == 2) /* DDR3 */
74 static void set_failing_group_stage(struct socfpga_sdrseq *seq,
75 u32 group, u32 stage, u32 substage)
78 * Only set the global stage if there was not been any other
81 if (seq->gbl.error_stage == CAL_STAGE_NIL) {
82 seq->gbl.error_substage = substage;
83 seq->gbl.error_stage = stage;
84 seq->gbl.error_group = group;
88 static void reg_file_set_group(u16 set_group)
90 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
93 static void reg_file_set_stage(u8 set_stage)
95 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
98 static void reg_file_set_sub_stage(u8 set_sub_stage)
100 set_sub_stage &= 0xff;
101 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
105 * phy_mgr_initialize() - Initialize PHY Manager
107 * Initialize PHY Manager.
109 static void phy_mgr_initialize(struct socfpga_sdrseq *seq)
113 debug("%s:%d\n", __func__, __LINE__);
114 /* Calibration has control over path to memory */
116 * In Hard PHY this is a 2-bit control:
120 writel(0x3, &phy_mgr_cfg->mux_sel);
122 /* USER memory clock is not stable we begin initialization */
123 writel(0, &phy_mgr_cfg->reset_mem_stbl);
125 /* USER calibration status all set to zero */
126 writel(0, &phy_mgr_cfg->cal_status);
128 writel(0, &phy_mgr_cfg->cal_debug_info);
130 /* Init params only if we do NOT skip calibration. */
131 if ((seq->dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL)
134 ratio = seq->rwcfg->mem_dq_per_read_dqs /
135 seq->rwcfg->mem_virtual_groups_per_read_dqs;
136 seq->param.read_correct_mask_vg = (1 << ratio) - 1;
137 seq->param.write_correct_mask_vg = (1 << ratio) - 1;
138 seq->param.read_correct_mask = (1 << seq->rwcfg->mem_dq_per_read_dqs)
140 seq->param.write_correct_mask = (1 << seq->rwcfg->mem_dq_per_write_dqs)
145 * set_rank_and_odt_mask() - Set Rank and ODT mask
147 * @odt_mode: ODT mode, OFF or READ_WRITE
149 * Set Rank and ODT mask (On-Die Termination).
151 static void set_rank_and_odt_mask(struct socfpga_sdrseq *seq,
152 const u32 rank, const u32 odt_mode)
158 if (odt_mode == RW_MGR_ODT_MODE_OFF) {
161 } else { /* RW_MGR_ODT_MODE_READ_WRITE */
162 switch (seq->rwcfg->mem_number_of_ranks) {
164 /* Read: ODT = 0 ; Write: ODT = 1 */
168 case 2: /* 2 Ranks */
169 if (seq->rwcfg->mem_number_of_cs_per_dimm == 1) {
171 * - Dual-Slot , Single-Rank (1 CS per DIMM)
173 * - RDIMM, 4 total CS (2 CS per DIMM, 2 DIMM)
175 * Since MEM_NUMBER_OF_RANKS is 2, they
176 * are both single rank with 2 CS each
177 * (special for RDIMM).
179 * Read: Turn on ODT on the opposite rank
180 * Write: Turn on ODT on all ranks
182 odt_mask_0 = 0x3 & ~(1 << rank);
185 odt_mask_1 &= ~(1 << rank);
188 * - Single-Slot , Dual-Rank (2 CS per DIMM)
190 * Read: Turn on ODT off on all ranks
191 * Write: Turn on ODT on active rank
194 odt_mask_1 = 0x3 & (1 << rank);
197 case 4: /* 4 Ranks */
199 * DDR3 Read, DDR2 Read/Write:
200 * ----------+-----------------------+
202 * +-----------------------+
203 * Rank | 3 | 2 | 1 | 0 |
204 * ----------+-----+-----+-----+-----+
205 * 0 | 0 | 1 | 0 | 0 |
206 * 1 | 1 | 0 | 0 | 0 |
207 * 2 | 0 | 0 | 0 | 1 |
208 * 3 | 0 | 0 | 1 | 0 |
209 * ----------+-----+-----+-----+-----+
212 * ----------+-----------------------+
214 * Write To +-----------------------+
215 * Rank | 3 | 2 | 1 | 0 |
216 * ----------+-----+-----+-----+-----+
217 * 0 | 0 | 1 | 0 | 1 |
218 * 1 | 1 | 0 | 1 | 0 |
219 * 2 | 0 | 1 | 0 | 1 |
220 * 3 | 1 | 0 | 1 | 0 |
221 * ----------+-----+-----+-----+-----+
228 else if (dram_is_ddr(3))
235 else if (dram_is_ddr(3))
242 else if (dram_is_ddr(3))
249 else if (dram_is_ddr(3))
257 cs_and_odt_mask = (0xFF & ~(1 << rank)) |
258 ((0xFF & odt_mask_0) << 8) |
259 ((0xFF & odt_mask_1) << 16);
260 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
261 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
265 * scc_mgr_set() - Set SCC Manager register
266 * @off: Base offset in SCC Manager space
267 * @grp: Read/Write group
268 * @val: Value to be set
270 * This function sets the SCC Manager (Scan Chain Control Manager) register.
272 static void scc_mgr_set(u32 off, u32 grp, u32 val)
274 writel(val, SDR_PHYGRP_SCCGRP_ADDRESS | off | (grp << 2));
278 * scc_mgr_initialize() - Initialize SCC Manager registers
280 * Initialize SCC Manager registers.
282 static void scc_mgr_initialize(void)
285 * Clear register file for HPS. 16 (2^4) is the size of the
286 * full register file in the scc mgr:
287 * RFILE_DEPTH = 1 + log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
288 * MEM_IF_READ_DQS_WIDTH - 1);
292 for (i = 0; i < 16; i++) {
293 debug_cond(DLEVEL >= 1, "%s:%d: Clearing SCC RFILE index %u\n",
294 __func__, __LINE__, i);
295 scc_mgr_set(SCC_MGR_HHP_RFILE_OFFSET, i, 0);
299 static void scc_mgr_set_dqdqs_output_phase(u32 write_group, u32 phase)
301 scc_mgr_set(SCC_MGR_DQDQS_OUT_PHASE_OFFSET, write_group, phase);
304 static void scc_mgr_set_dqs_bus_in_delay(u32 read_group, u32 delay)
306 scc_mgr_set(SCC_MGR_DQS_IN_DELAY_OFFSET, read_group, delay);
309 static void scc_mgr_set_dqs_en_phase(u32 read_group, u32 phase)
311 scc_mgr_set(SCC_MGR_DQS_EN_PHASE_OFFSET, read_group, phase);
314 static void scc_mgr_set_dqs_en_delay(u32 read_group, u32 delay)
316 scc_mgr_set(SCC_MGR_DQS_EN_DELAY_OFFSET, read_group, delay);
319 static void scc_mgr_set_dq_in_delay(u32 dq_in_group, u32 delay)
321 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET, dq_in_group, delay);
324 static void scc_mgr_set_dqs_io_in_delay(struct socfpga_sdrseq *seq,
327 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
328 seq->rwcfg->mem_dq_per_write_dqs, delay);
331 static void scc_mgr_set_dm_in_delay(struct socfpga_sdrseq *seq, u32 dm,
334 scc_mgr_set(SCC_MGR_IO_IN_DELAY_OFFSET,
335 seq->rwcfg->mem_dq_per_write_dqs + 1 + dm,
339 static void scc_mgr_set_dq_out1_delay(u32 dq_in_group, u32 delay)
341 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET, dq_in_group, delay);
344 static void scc_mgr_set_dqs_out1_delay(struct socfpga_sdrseq *seq,
347 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
348 seq->rwcfg->mem_dq_per_write_dqs, delay);
351 static void scc_mgr_set_dm_out1_delay(struct socfpga_sdrseq *seq, u32 dm,
354 scc_mgr_set(SCC_MGR_IO_OUT1_DELAY_OFFSET,
355 seq->rwcfg->mem_dq_per_write_dqs + 1 + dm,
359 /* load up dqs config settings */
360 static void scc_mgr_load_dqs(u32 dqs)
362 writel(dqs, &sdr_scc_mgr->dqs_ena);
365 /* load up dqs io config settings */
366 static void scc_mgr_load_dqs_io(void)
368 writel(0, &sdr_scc_mgr->dqs_io_ena);
371 /* load up dq config settings */
372 static void scc_mgr_load_dq(u32 dq_in_group)
374 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
377 /* load up dm config settings */
378 static void scc_mgr_load_dm(u32 dm)
380 writel(dm, &sdr_scc_mgr->dm_ena);
384 * scc_mgr_set_all_ranks() - Set SCC Manager register for all ranks
385 * @off: Base offset in SCC Manager space
386 * @grp: Read/Write group
387 * @val: Value to be set
388 * @update: If non-zero, trigger SCC Manager update for all ranks
390 * This function sets the SCC Manager (Scan Chain Control Manager) register
391 * and optionally triggers the SCC update for all ranks.
393 static void scc_mgr_set_all_ranks(struct socfpga_sdrseq *seq,
394 const u32 off, const u32 grp, const u32 val,
399 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
400 r += NUM_RANKS_PER_SHADOW_REG) {
401 scc_mgr_set(off, grp, val);
403 if (update || (r == 0)) {
404 writel(grp, &sdr_scc_mgr->dqs_ena);
405 writel(0, &sdr_scc_mgr->update);
410 static void scc_mgr_set_dqs_en_phase_all_ranks(struct socfpga_sdrseq *seq,
411 u32 read_group, u32 phase)
414 * USER although the h/w doesn't support different phases per
415 * shadow register, for simplicity our scc manager modeling
416 * keeps different phase settings per shadow reg, and it's
417 * important for us to keep them in sync to match h/w.
418 * for efficiency, the scan chain update should occur only
421 scc_mgr_set_all_ranks(seq, SCC_MGR_DQS_EN_PHASE_OFFSET,
422 read_group, phase, 0);
425 static void scc_mgr_set_dqdqs_output_phase_all_ranks(struct socfpga_sdrseq *seq,
426 u32 write_group, u32 phase)
429 * USER although the h/w doesn't support different phases per
430 * shadow register, for simplicity our scc manager modeling
431 * keeps different phase settings per shadow reg, and it's
432 * important for us to keep them in sync to match h/w.
433 * for efficiency, the scan chain update should occur only
436 scc_mgr_set_all_ranks(seq, SCC_MGR_DQDQS_OUT_PHASE_OFFSET,
437 write_group, phase, 0);
440 static void scc_mgr_set_dqs_en_delay_all_ranks(struct socfpga_sdrseq *seq,
441 u32 read_group, u32 delay)
444 * In shadow register mode, the T11 settings are stored in
445 * registers in the core, which are updated by the DQS_ENA
446 * signals. Not issuing the SCC_MGR_UPD command allows us to
447 * save lots of rank switching overhead, by calling
448 * select_shadow_regs_for_update with update_scan_chains
451 scc_mgr_set_all_ranks(seq, SCC_MGR_DQS_EN_DELAY_OFFSET,
452 read_group, delay, 1);
456 * scc_mgr_set_oct_out1_delay() - Set OCT output delay
457 * @write_group: Write group
458 * @delay: Delay value
460 * This function sets the OCT output delay in SCC manager.
462 static void scc_mgr_set_oct_out1_delay(struct socfpga_sdrseq *seq,
463 const u32 write_group, const u32 delay)
465 const int ratio = seq->rwcfg->mem_if_read_dqs_width /
466 seq->rwcfg->mem_if_write_dqs_width;
467 const int base = write_group * ratio;
470 * Load the setting in the SCC manager
471 * Although OCT affects only write data, the OCT delay is controlled
472 * by the DQS logic block which is instantiated once per read group.
473 * For protocols where a write group consists of multiple read groups,
474 * the setting must be set multiple times.
476 for (i = 0; i < ratio; i++)
477 scc_mgr_set(SCC_MGR_OCT_OUT1_DELAY_OFFSET, base + i, delay);
481 * scc_mgr_set_hhp_extras() - Set HHP extras.
483 * Load the fixed setting in the SCC manager HHP extras.
485 static void scc_mgr_set_hhp_extras(void)
488 * Load the fixed setting in the SCC manager
489 * bits: 0:0 = 1'b1 - DQS bypass
490 * bits: 1:1 = 1'b1 - DQ bypass
491 * bits: 4:2 = 3'b001 - rfifo_mode
492 * bits: 6:5 = 2'b01 - rfifo clock_select
493 * bits: 7:7 = 1'b0 - separate gating from ungating setting
494 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
496 const u32 value = (0 << 8) | (0 << 7) | (1 << 5) |
497 (1 << 2) | (1 << 1) | (1 << 0);
498 const u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS |
499 SCC_MGR_HHP_GLOBALS_OFFSET |
500 SCC_MGR_HHP_EXTRAS_OFFSET;
502 debug_cond(DLEVEL >= 1, "%s:%d Setting HHP Extras\n",
505 debug_cond(DLEVEL >= 1, "%s:%d Done Setting HHP Extras\n",
510 * scc_mgr_zero_all() - Zero all DQS config
512 * Zero all DQS config.
514 static void scc_mgr_zero_all(struct socfpga_sdrseq *seq)
519 * USER Zero all DQS config settings, across all groups and all
522 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
523 r += NUM_RANKS_PER_SHADOW_REG) {
524 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
526 * The phases actually don't exist on a per-rank basis,
527 * but there's no harm updating them several times, so
528 * let's keep the code simple.
530 scc_mgr_set_dqs_bus_in_delay(i,
531 seq->iocfg->dqs_in_reserve
533 scc_mgr_set_dqs_en_phase(i, 0);
534 scc_mgr_set_dqs_en_delay(i, 0);
537 for (i = 0; i < seq->rwcfg->mem_if_write_dqs_width; i++) {
538 scc_mgr_set_dqdqs_output_phase(i, 0);
539 /* Arria V/Cyclone V don't have out2. */
540 scc_mgr_set_oct_out1_delay(seq, i,
541 seq->iocfg->dqs_out_reserve);
545 /* Multicast to all DQS group enables. */
546 writel(0xff, &sdr_scc_mgr->dqs_ena);
547 writel(0, &sdr_scc_mgr->update);
551 * scc_set_bypass_mode() - Set bypass mode and trigger SCC update
552 * @write_group: Write group
554 * Set bypass mode and trigger SCC update.
556 static void scc_set_bypass_mode(const u32 write_group)
558 /* Multicast to all DQ enables. */
559 writel(0xff, &sdr_scc_mgr->dq_ena);
560 writel(0xff, &sdr_scc_mgr->dm_ena);
562 /* Update current DQS IO enable. */
563 writel(0, &sdr_scc_mgr->dqs_io_ena);
565 /* Update the DQS logic. */
566 writel(write_group, &sdr_scc_mgr->dqs_ena);
569 writel(0, &sdr_scc_mgr->update);
573 * scc_mgr_load_dqs_for_write_group() - Load DQS settings for Write Group
574 * @write_group: Write group
576 * Load DQS settings for Write Group, do not trigger SCC update.
578 static void scc_mgr_load_dqs_for_write_group(struct socfpga_sdrseq *seq,
579 const u32 write_group)
581 const int ratio = seq->rwcfg->mem_if_read_dqs_width /
582 seq->rwcfg->mem_if_write_dqs_width;
583 const int base = write_group * ratio;
586 * Load the setting in the SCC manager
587 * Although OCT affects only write data, the OCT delay is controlled
588 * by the DQS logic block which is instantiated once per read group.
589 * For protocols where a write group consists of multiple read groups,
590 * the setting must be set multiple times.
592 for (i = 0; i < ratio; i++)
593 writel(base + i, &sdr_scc_mgr->dqs_ena);
597 * scc_mgr_zero_group() - Zero all configs for a group
599 * Zero DQ, DM, DQS and OCT configs for a group.
601 static void scc_mgr_zero_group(struct socfpga_sdrseq *seq,
602 const u32 write_group, const int out_only)
606 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
607 r += NUM_RANKS_PER_SHADOW_REG) {
608 /* Zero all DQ config settings. */
609 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
610 scc_mgr_set_dq_out1_delay(i, 0);
612 scc_mgr_set_dq_in_delay(i, 0);
615 /* Multicast to all DQ enables. */
616 writel(0xff, &sdr_scc_mgr->dq_ena);
618 /* Zero all DM config settings. */
619 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
621 scc_mgr_set_dm_in_delay(seq, i, 0);
622 scc_mgr_set_dm_out1_delay(seq, i, 0);
625 /* Multicast to all DM enables. */
626 writel(0xff, &sdr_scc_mgr->dm_ena);
628 /* Zero all DQS IO settings. */
630 scc_mgr_set_dqs_io_in_delay(seq, 0);
632 /* Arria V/Cyclone V don't have out2. */
633 scc_mgr_set_dqs_out1_delay(seq, seq->iocfg->dqs_out_reserve);
634 scc_mgr_set_oct_out1_delay(seq, write_group,
635 seq->iocfg->dqs_out_reserve);
636 scc_mgr_load_dqs_for_write_group(seq, write_group);
638 /* Multicast to all DQS IO enables (only 1 in total). */
639 writel(0, &sdr_scc_mgr->dqs_io_ena);
641 /* Hit update to zero everything. */
642 writel(0, &sdr_scc_mgr->update);
647 * apply and load a particular input delay for the DQ pins in a group
648 * group_bgn is the index of the first dq pin (in the write group)
650 static void scc_mgr_apply_group_dq_in_delay(struct socfpga_sdrseq *seq,
651 u32 group_bgn, u32 delay)
655 for (i = 0, p = group_bgn; i < seq->rwcfg->mem_dq_per_read_dqs;
657 scc_mgr_set_dq_in_delay(p, delay);
663 * scc_mgr_apply_group_dq_out1_delay() - Apply and load an output delay for the
665 * @delay: Delay value
667 * Apply and load a particular output delay for the DQ pins in a group.
669 static void scc_mgr_apply_group_dq_out1_delay(struct socfpga_sdrseq *seq,
674 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
675 scc_mgr_set_dq_out1_delay(i, delay);
680 /* apply and load a particular output delay for the DM pins in a group */
681 static void scc_mgr_apply_group_dm_out1_delay(struct socfpga_sdrseq *seq,
686 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
687 scc_mgr_set_dm_out1_delay(seq, i, delay1);
693 /* apply and load delay on both DQS and OCT out1 */
694 static void scc_mgr_apply_group_dqs_io_and_oct_out1(struct socfpga_sdrseq *seq,
695 u32 write_group, u32 delay)
697 scc_mgr_set_dqs_out1_delay(seq, delay);
698 scc_mgr_load_dqs_io();
700 scc_mgr_set_oct_out1_delay(seq, write_group, delay);
701 scc_mgr_load_dqs_for_write_group(seq, write_group);
705 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output
706 * side: DQ, DM, DQS, OCT
707 * @write_group: Write group
708 * @delay: Delay value
710 * Apply a delay to the entire output side: DQ, DM, DQS, OCT.
712 static void scc_mgr_apply_group_all_out_delay_add(struct socfpga_sdrseq *seq,
713 const u32 write_group,
719 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++)
723 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++)
727 new_delay = READ_SCC_DQS_IO_OUT2_DELAY + delay;
728 if (new_delay > seq->iocfg->io_out2_delay_max) {
729 debug_cond(DLEVEL >= 1,
730 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
731 __func__, __LINE__, write_group, delay, new_delay,
732 seq->iocfg->io_out2_delay_max,
733 new_delay - seq->iocfg->io_out2_delay_max);
734 new_delay -= seq->iocfg->io_out2_delay_max;
735 scc_mgr_set_dqs_out1_delay(seq, new_delay);
738 scc_mgr_load_dqs_io();
741 new_delay = READ_SCC_OCT_OUT2_DELAY + delay;
742 if (new_delay > seq->iocfg->io_out2_delay_max) {
743 debug_cond(DLEVEL >= 1,
744 "%s:%d (%u, %u) DQS: %u > %d; adding %u to OUT1\n",
745 __func__, __LINE__, write_group, delay,
746 new_delay, seq->iocfg->io_out2_delay_max,
747 new_delay - seq->iocfg->io_out2_delay_max);
748 new_delay -= seq->iocfg->io_out2_delay_max;
749 scc_mgr_set_oct_out1_delay(seq, write_group, new_delay);
752 scc_mgr_load_dqs_for_write_group(seq, write_group);
756 * scc_mgr_apply_group_all_out_delay_add() - Apply a delay to the entire output
758 * @write_group: Write group
759 * @delay: Delay value
761 * Apply a delay to the entire output side (DQ, DM, DQS, OCT) to all ranks.
764 scc_mgr_apply_group_all_out_delay_add_all_ranks(struct socfpga_sdrseq *seq,
765 const u32 write_group,
770 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
771 r += NUM_RANKS_PER_SHADOW_REG) {
772 scc_mgr_apply_group_all_out_delay_add(seq, write_group, delay);
773 writel(0, &sdr_scc_mgr->update);
778 * set_jump_as_return() - Return instruction optimization
780 * Optimization used to recover some slots in ddr3 inst_rom could be
781 * applied to other protocols if we wanted to
783 static void set_jump_as_return(struct socfpga_sdrseq *seq)
786 * To save space, we replace return with jump to special shared
787 * RETURN instruction so we set the counter to large value so that
790 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
791 writel(seq->rwcfg->rreturn, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
795 * delay_for_n_mem_clocks() - Delay for N memory clocks
796 * @clocks: Length of the delay
798 * Delay for N memory clocks.
800 static void delay_for_n_mem_clocks(struct socfpga_sdrseq *seq,
808 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
810 /* Scale (rounding up) to get afi clocks. */
811 afi_clocks = DIV_ROUND_UP(clocks, seq->misccfg->afi_rate_ratio);
812 if (afi_clocks) /* Temporary underflow protection */
816 * Note, we don't bother accounting for being off a little
817 * bit because of a few extra instructions in outer loops.
818 * Note, the loops have a test at the end, and do the test
819 * before the decrement, and so always perform the loop
820 * 1 time more than the counter value
822 c_loop = afi_clocks >> 16;
823 outer = c_loop ? 0xff : (afi_clocks >> 8);
824 inner = outer ? 0xff : afi_clocks;
827 * rom instructions are structured as follows:
829 * IDLE_LOOP2: jnz cntr0, TARGET_A
830 * IDLE_LOOP1: jnz cntr1, TARGET_B
833 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
834 * TARGET_B is set to IDLE_LOOP2 as well
836 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
837 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
839 * a little confusing, but it helps save precious space in the inst_rom
840 * and sequencer rom and keeps the delays more accurate and reduces
843 if (afi_clocks < 0x100) {
844 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
845 &sdr_rw_load_mgr_regs->load_cntr1);
847 writel(seq->rwcfg->idle_loop1,
848 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
850 writel(seq->rwcfg->idle_loop1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
851 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
853 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
854 &sdr_rw_load_mgr_regs->load_cntr0);
856 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
857 &sdr_rw_load_mgr_regs->load_cntr1);
859 writel(seq->rwcfg->idle_loop2,
860 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
862 writel(seq->rwcfg->idle_loop2,
863 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
866 writel(seq->rwcfg->idle_loop2,
867 SDR_PHYGRP_RWMGRGRP_ADDRESS |
868 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
869 } while (c_loop-- != 0);
871 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
874 static void delay_for_n_ns(struct socfpga_sdrseq *seq, const u32 ns)
876 delay_for_n_mem_clocks(seq, (ns * seq->misccfg->afi_clk_freq *
877 seq->misccfg->afi_rate_ratio) / 1000);
881 * rw_mgr_mem_init_load_regs() - Load instruction registers
882 * @cntr0: Counter 0 value
883 * @cntr1: Counter 1 value
884 * @cntr2: Counter 2 value
885 * @jump: Jump instruction value
887 * Load instruction registers.
889 static void rw_mgr_mem_init_load_regs(struct socfpga_sdrseq *seq,
890 u32 cntr0, u32 cntr1, u32 cntr2, u32 jump)
892 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
893 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
896 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr0),
897 &sdr_rw_load_mgr_regs->load_cntr0);
898 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr1),
899 &sdr_rw_load_mgr_regs->load_cntr1);
900 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(cntr2),
901 &sdr_rw_load_mgr_regs->load_cntr2);
903 /* Load jump address */
904 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
905 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add1);
906 writel(jump, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
908 /* Execute count instruction */
909 writel(jump, grpaddr);
913 * rw_mgr_mem_load_user_ddr2() - Load user calibration values for DDR2
914 * @handoff: Indicate whether this is initialization or handoff phase
916 * Load user calibration values and optionally precharge the banks.
918 static void rw_mgr_mem_load_user_ddr2(struct socfpga_sdrseq *seq,
921 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
922 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
925 for (r = 0; r < seq->rwcfg->mem_number_of_ranks; r++) {
927 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_OFF);
929 /* precharge all banks ... */
930 writel(seq->rwcfg->precharge_all, grpaddr);
932 writel(seq->rwcfg->emr2, grpaddr);
933 writel(seq->rwcfg->emr3, grpaddr);
934 writel(seq->rwcfg->emr, grpaddr);
937 writel(seq->rwcfg->mr_user, grpaddr);
941 writel(seq->rwcfg->mr_dll_reset, grpaddr);
943 writel(seq->rwcfg->precharge_all, grpaddr);
945 writel(seq->rwcfg->refresh, grpaddr);
946 delay_for_n_ns(seq, 200);
947 writel(seq->rwcfg->refresh, grpaddr);
948 delay_for_n_ns(seq, 200);
950 writel(seq->rwcfg->mr_calib, grpaddr);
951 writel(/*seq->rwcfg->*/0x0b, grpaddr); // EMR_OCD_ENABLE
952 writel(seq->rwcfg->emr, grpaddr);
953 delay_for_n_mem_clocks(seq, 200);
958 * rw_mgr_mem_load_user_ddr3() - Load user calibration values
959 * @fin1: Final instruction 1
960 * @fin2: Final instruction 2
961 * @precharge: If 1, precharge the banks at the end
963 * Load user calibration values and optionally precharge the banks.
965 static void rw_mgr_mem_load_user_ddr3(struct socfpga_sdrseq *seq,
966 const u32 fin1, const u32 fin2,
969 u32 grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
970 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
973 for (r = 0; r < seq->rwcfg->mem_number_of_ranks; r++) {
975 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_OFF);
977 /* precharge all banks ... */
979 writel(seq->rwcfg->precharge_all, grpaddr);
982 * USER Use Mirror-ed commands for odd ranks if address
985 if ((seq->rwcfg->mem_address_mirroring >> r) & 0x1) {
986 set_jump_as_return(seq);
987 writel(seq->rwcfg->mrs2_mirr, grpaddr);
988 delay_for_n_mem_clocks(seq, 4);
989 set_jump_as_return(seq);
990 writel(seq->rwcfg->mrs3_mirr, grpaddr);
991 delay_for_n_mem_clocks(seq, 4);
992 set_jump_as_return(seq);
993 writel(seq->rwcfg->mrs1_mirr, grpaddr);
994 delay_for_n_mem_clocks(seq, 4);
995 set_jump_as_return(seq);
996 writel(fin1, grpaddr);
998 set_jump_as_return(seq);
999 writel(seq->rwcfg->mrs2, grpaddr);
1000 delay_for_n_mem_clocks(seq, 4);
1001 set_jump_as_return(seq);
1002 writel(seq->rwcfg->mrs3, grpaddr);
1003 delay_for_n_mem_clocks(seq, 4);
1004 set_jump_as_return(seq);
1005 writel(seq->rwcfg->mrs1, grpaddr);
1006 set_jump_as_return(seq);
1007 writel(fin2, grpaddr);
1013 set_jump_as_return(seq);
1014 writel(seq->rwcfg->zqcl, grpaddr);
1016 /* tZQinit = tDLLK = 512 ck cycles */
1017 delay_for_n_mem_clocks(seq, 512);
1022 * rw_mgr_mem_load_user() - Load user calibration values
1023 * @fin1: Final instruction 1
1024 * @fin2: Final instruction 2
1025 * @precharge: If 1, precharge the banks at the end
1027 * Load user calibration values and optionally precharge the banks.
1029 static void rw_mgr_mem_load_user(struct socfpga_sdrseq *seq,
1030 const u32 fin1, const u32 fin2,
1031 const int precharge)
1034 rw_mgr_mem_load_user_ddr2(seq, precharge);
1035 else if (dram_is_ddr(3))
1036 rw_mgr_mem_load_user_ddr3(seq, fin1, fin2, precharge);
1041 * rw_mgr_mem_initialize() - Initialize RW Manager
1043 * Initialize RW Manager.
1045 static void rw_mgr_mem_initialize(struct socfpga_sdrseq *seq)
1047 debug("%s:%d\n", __func__, __LINE__);
1049 /* The reset / cke part of initialization is broadcasted to all ranks */
1050 if (dram_is_ddr(3)) {
1051 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1052 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
1056 * Here's how you load register for a loop
1057 * Counters are located @ 0x800
1058 * Jump address are located @ 0xC00
1059 * For both, registers 0 to 3 are selected using bits 3 and 2, like
1060 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
1061 * I know this ain't pretty, but Avalon bus throws away the 2 least
1065 /* Start with memory RESET activated */
1070 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
1071 * If a and b are the number of iteration in 2 nested loops
1072 * it takes the following number of cycles to complete the operation:
1073 * number_of_cycles = ((2 + n) * a + 2) * b
1074 * where n is the number of instruction in the inner loop
1075 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
1078 rw_mgr_mem_init_load_regs(seq, seq->misccfg->tinit_cntr0_val,
1079 seq->misccfg->tinit_cntr1_val,
1080 seq->misccfg->tinit_cntr2_val,
1081 seq->rwcfg->init_reset_0_cke_0);
1083 /* Indicate that memory is stable. */
1084 writel(1, &phy_mgr_cfg->reset_mem_stbl);
1086 if (dram_is_ddr(2)) {
1087 writel(seq->rwcfg->nop, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1088 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
1090 /* Bring up clock enable. */
1092 /* tXRP < 400 ck cycles */
1093 delay_for_n_ns(seq, 400);
1094 } else if (dram_is_ddr(3)) {
1096 * transition the RESET to high
1101 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
1102 * If a and b are the number of iteration in 2 nested loops
1103 * it takes the following number of cycles to complete the
1104 * operation number_of_cycles = ((2 + n) * a + 2) * b
1105 * where n is the number of instruction in the inner loop
1106 * One possible solution is
1107 * n = 2 , a = 131 , b = 256 => a = 83, b = FF
1109 rw_mgr_mem_init_load_regs(seq, seq->misccfg->treset_cntr0_val,
1110 seq->misccfg->treset_cntr1_val,
1111 seq->misccfg->treset_cntr2_val,
1112 seq->rwcfg->init_reset_1_cke_0);
1113 /* Bring up clock enable. */
1115 /* tXRP < 250 ck cycles */
1116 delay_for_n_mem_clocks(seq, 250);
1119 rw_mgr_mem_load_user(seq, seq->rwcfg->mrs0_dll_reset_mirr,
1120 seq->rwcfg->mrs0_dll_reset, 0);
1124 * rw_mgr_mem_handoff() - Hand off the memory to user
1126 * At the end of calibration we have to program the user settings in
1127 * and hand off the memory to the user.
1129 static void rw_mgr_mem_handoff(struct socfpga_sdrseq *seq)
1131 rw_mgr_mem_load_user(seq, seq->rwcfg->mrs0_user_mirr,
1132 seq->rwcfg->mrs0_user, 1);
1134 * Need to wait tMOD (12CK or 15ns) time before issuing other
1135 * commands, but we will have plenty of NIOS cycles before actual
1136 * handoff so its okay.
1141 * rw_mgr_mem_calibrate_write_test_issue() - Issue write test command
1142 * @group: Write Group
1145 * Issue write test command. Two variants are provided, one that just tests
1146 * a write pattern and another that tests datamask functionality.
1148 static void rw_mgr_mem_calibrate_write_test_issue(struct socfpga_sdrseq *seq,
1149 u32 group, u32 test_dm)
1151 const u32 quick_write_mode =
1152 (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES) &&
1153 seq->misccfg->enable_super_quick_calibration;
1154 u32 mcc_instruction;
1155 u32 rw_wl_nop_cycles;
1158 * Set counter and jump addresses for the right
1159 * number of NOP cycles.
1160 * The number of supported NOP cycles can range from -1 to infinity
1161 * Three different cases are handled:
1163 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
1164 * mechanism will be used to insert the right number of NOPs
1166 * 2. For a number of NOP cycles equals to 0, the micro-instruction
1167 * issuing the write command will jump straight to the
1168 * micro-instruction that turns on DQS (for DDRx), or outputs write
1169 * data (for RLD), skipping
1170 * the NOP micro-instruction all together
1172 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
1173 * turned on in the same micro-instruction that issues the write
1174 * command. Then we need
1175 * to directly jump to the micro-instruction that sends out the data
1177 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
1178 * (2 and 3). One jump-counter (0) is used to perform multiple
1179 * write-read operations.
1180 * one counter left to issue this command in "multiple-group" mode
1183 rw_wl_nop_cycles = seq->gbl.rw_wl_nop_cycles;
1185 if (rw_wl_nop_cycles == -1) {
1187 * CNTR 2 - We want to execute the special write operation that
1188 * turns on DQS right away and then skip directly to the
1189 * instruction that sends out the data. We set the counter to a
1190 * large number so that the jump is always taken.
1192 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1194 /* CNTR 3 - Not used */
1196 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0_wl_1;
1197 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_data,
1198 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1199 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_nop,
1200 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1202 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0_wl_1;
1203 writel(seq->rwcfg->lfsr_wr_rd_bank_0_data,
1204 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1205 writel(seq->rwcfg->lfsr_wr_rd_bank_0_nop,
1206 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1208 } else if (rw_wl_nop_cycles == 0) {
1210 * CNTR 2 - We want to skip the NOP operation and go straight
1211 * to the DQS enable instruction. We set the counter to a large
1212 * number so that the jump is always taken.
1214 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
1216 /* CNTR 3 - Not used */
1218 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0;
1219 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_dqs,
1220 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1222 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0;
1223 writel(seq->rwcfg->lfsr_wr_rd_bank_0_dqs,
1224 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1228 * CNTR 2 - In this case we want to execute the next instruction
1229 * and NOT take the jump. So we set the counter to 0. The jump
1230 * address doesn't count.
1232 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
1233 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1236 * CNTR 3 - Set the nop counter to the number of cycles we
1237 * need to loop for, minus 1.
1239 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
1241 mcc_instruction = seq->rwcfg->lfsr_wr_rd_dm_bank_0;
1242 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_nop,
1243 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1245 mcc_instruction = seq->rwcfg->lfsr_wr_rd_bank_0;
1246 writel(seq->rwcfg->lfsr_wr_rd_bank_0_nop,
1247 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1251 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1252 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1254 if (quick_write_mode)
1255 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
1257 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
1259 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1262 * CNTR 1 - This is used to ensure enough time elapses
1263 * for read data to come back.
1265 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
1268 writel(seq->rwcfg->lfsr_wr_rd_dm_bank_0_wait,
1269 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1271 writel(seq->rwcfg->lfsr_wr_rd_bank_0_wait,
1272 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1275 writel(mcc_instruction, (SDR_PHYGRP_RWMGRGRP_ADDRESS |
1276 RW_MGR_RUN_SINGLE_GROUP_OFFSET) +
1281 * rw_mgr_mem_calibrate_write_test() - Test writes, check for single/multiple
1283 * @rank_bgn: Rank number
1284 * @write_group: Write Group
1286 * @all_correct: All bits must be correct in the mask
1287 * @bit_chk: Resulting bit mask after the test
1288 * @all_ranks: Test all ranks
1290 * Test writes, can check for a single bit pass or multiple bit pass.
1293 rw_mgr_mem_calibrate_write_test(struct socfpga_sdrseq *seq,
1294 const u32 rank_bgn, const u32 write_group,
1295 const u32 use_dm, const u32 all_correct,
1296 u32 *bit_chk, const u32 all_ranks)
1298 const u32 rank_end = all_ranks ?
1299 seq->rwcfg->mem_number_of_ranks :
1300 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1301 const u32 shift_ratio = seq->rwcfg->mem_dq_per_write_dqs /
1302 seq->rwcfg->mem_virtual_groups_per_write_dqs;
1303 const u32 correct_mask_vg = seq->param.write_correct_mask_vg;
1305 u32 tmp_bit_chk, base_rw_mgr, group;
1308 *bit_chk = seq->param.write_correct_mask;
1310 for (r = rank_bgn; r < rank_end; r++) {
1312 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
1315 for (vg = seq->rwcfg->mem_virtual_groups_per_write_dqs - 1;
1317 /* Reset the FIFOs to get pointers to known state. */
1318 writel(0, &phy_mgr_cmd->fifo_reset);
1320 group = write_group *
1321 seq->rwcfg->mem_virtual_groups_per_write_dqs
1323 rw_mgr_mem_calibrate_write_test_issue(seq, group,
1326 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1327 tmp_bit_chk <<= shift_ratio;
1328 tmp_bit_chk |= (correct_mask_vg & ~(base_rw_mgr));
1331 *bit_chk &= tmp_bit_chk;
1334 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
1336 debug_cond(DLEVEL >= 2,
1337 "write_test(%u,%u,ALL) : %u == %u => %i\n",
1338 write_group, use_dm, *bit_chk,
1339 seq->param.write_correct_mask,
1340 *bit_chk == seq->param.write_correct_mask);
1341 return *bit_chk == seq->param.write_correct_mask;
1343 debug_cond(DLEVEL >= 2,
1344 "write_test(%u,%u,ONE) : %u != %i => %i\n",
1345 write_group, use_dm, *bit_chk, 0, *bit_chk != 0);
1346 return *bit_chk != 0x00;
1351 * rw_mgr_mem_calibrate_read_test_patterns() - Read back test patterns
1352 * @rank_bgn: Rank number
1353 * @group: Read/Write Group
1354 * @all_ranks: Test all ranks
1356 * Performs a guaranteed read on the patterns we are going to use during a
1357 * read test to ensure memory works.
1360 rw_mgr_mem_calibrate_read_test_patterns(struct socfpga_sdrseq *seq,
1361 const u32 rank_bgn, const u32 group,
1362 const u32 all_ranks)
1364 const u32 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1365 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1366 const u32 addr_offset =
1367 (group * seq->rwcfg->mem_virtual_groups_per_read_dqs)
1369 const u32 rank_end = all_ranks ?
1370 seq->rwcfg->mem_number_of_ranks :
1371 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1372 const u32 shift_ratio = seq->rwcfg->mem_dq_per_read_dqs /
1373 seq->rwcfg->mem_virtual_groups_per_read_dqs;
1374 const u32 correct_mask_vg = seq->param.read_correct_mask_vg;
1376 u32 tmp_bit_chk, base_rw_mgr, bit_chk;
1380 bit_chk = seq->param.read_correct_mask;
1382 for (r = rank_bgn; r < rank_end; r++) {
1384 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
1386 /* Load up a constant bursts of read commands */
1387 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1388 writel(seq->rwcfg->guaranteed_read,
1389 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1391 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1392 writel(seq->rwcfg->guaranteed_read_cont,
1393 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1396 for (vg = seq->rwcfg->mem_virtual_groups_per_read_dqs - 1;
1398 /* Reset the FIFOs to get pointers to known state. */
1399 writel(0, &phy_mgr_cmd->fifo_reset);
1400 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1401 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1402 writel(seq->rwcfg->guaranteed_read,
1403 addr + addr_offset + (vg << 2));
1405 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1406 tmp_bit_chk <<= shift_ratio;
1407 tmp_bit_chk |= correct_mask_vg & ~base_rw_mgr;
1410 bit_chk &= tmp_bit_chk;
1413 writel(seq->rwcfg->clear_dqs_enable, addr + (group << 2));
1415 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
1417 if (bit_chk != seq->param.read_correct_mask)
1420 debug_cond(DLEVEL >= 1,
1421 "%s:%d test_load_patterns(%u,ALL) => (%u == %u) => %i\n",
1422 __func__, __LINE__, group, bit_chk,
1423 seq->param.read_correct_mask, ret);
1429 * rw_mgr_mem_calibrate_read_load_patterns() - Load up the patterns for read
1431 * @rank_bgn: Rank number
1432 * @all_ranks: Test all ranks
1434 * Load up the patterns we are going to use during a read test.
1436 static void rw_mgr_mem_calibrate_read_load_patterns(struct socfpga_sdrseq *seq,
1438 const int all_ranks)
1440 const u32 rank_end = all_ranks ?
1441 seq->rwcfg->mem_number_of_ranks :
1442 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1445 debug("%s:%d\n", __func__, __LINE__);
1447 for (r = rank_bgn; r < rank_end; r++) {
1449 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
1451 /* Load up a constant bursts */
1452 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1454 writel(seq->rwcfg->guaranteed_write_wait0,
1455 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1457 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1459 writel(seq->rwcfg->guaranteed_write_wait1,
1460 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1462 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
1464 writel(seq->rwcfg->guaranteed_write_wait2,
1465 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1467 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
1469 writel(seq->rwcfg->guaranteed_write_wait3,
1470 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1472 writel(seq->rwcfg->guaranteed_write,
1473 SDR_PHYGRP_RWMGRGRP_ADDRESS |
1474 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
1477 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
1481 * rw_mgr_mem_calibrate_read_test() - Perform READ test on single rank
1482 * @rank_bgn: Rank number
1483 * @group: Read/Write group
1484 * @num_tries: Number of retries of the test
1485 * @all_correct: All bits must be correct in the mask
1486 * @bit_chk: Resulting bit mask after the test
1487 * @all_groups: Test all R/W groups
1488 * @all_ranks: Test all ranks
1490 * Try a read and see if it returns correct data back. Test has dummy reads
1491 * inserted into the mix used to align DQS enable. Test has more thorough
1492 * checks than the regular read test.
1495 rw_mgr_mem_calibrate_read_test(struct socfpga_sdrseq *seq,
1496 const u32 rank_bgn, const u32 group,
1497 const u32 num_tries, const u32 all_correct,
1499 const u32 all_groups, const u32 all_ranks)
1501 const u32 rank_end = all_ranks ? seq->rwcfg->mem_number_of_ranks :
1502 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1503 const u32 quick_read_mode =
1504 ((STATIC_CALIB_STEPS & CALIB_SKIP_DELAY_SWEEPS) &&
1505 seq->misccfg->enable_super_quick_calibration);
1506 u32 correct_mask_vg = seq->param.read_correct_mask_vg;
1513 *bit_chk = seq->param.read_correct_mask;
1515 for (r = rank_bgn; r < rank_end; r++) {
1517 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_READ_WRITE);
1519 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
1521 writel(seq->rwcfg->read_b2b_wait1,
1522 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1524 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1525 writel(seq->rwcfg->read_b2b_wait2,
1526 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1528 if (quick_read_mode)
1529 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
1530 /* need at least two (1+1) reads to capture failures */
1531 else if (all_groups)
1532 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
1534 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
1536 writel(seq->rwcfg->read_b2b,
1537 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1539 writel(seq->rwcfg->mem_if_read_dqs_width *
1540 seq->rwcfg->mem_virtual_groups_per_read_dqs - 1,
1541 &sdr_rw_load_mgr_regs->load_cntr3);
1543 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
1545 writel(seq->rwcfg->read_b2b,
1546 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1549 for (vg = seq->rwcfg->mem_virtual_groups_per_read_dqs - 1;
1551 /* Reset the FIFOs to get pointers to known state. */
1552 writel(0, &phy_mgr_cmd->fifo_reset);
1553 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1554 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1557 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1558 RW_MGR_RUN_ALL_GROUPS_OFFSET;
1560 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1561 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1564 writel(seq->rwcfg->read_b2b, addr +
1566 seq->rwcfg->mem_virtual_groups_per_read_dqs +
1569 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1571 seq->rwcfg->mem_dq_per_read_dqs /
1572 seq->rwcfg->mem_virtual_groups_per_read_dqs;
1573 tmp_bit_chk |= correct_mask_vg & ~(base_rw_mgr);
1576 *bit_chk &= tmp_bit_chk;
1579 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1580 writel(seq->rwcfg->clear_dqs_enable, addr + (group << 2));
1582 set_rank_and_odt_mask(seq, 0, RW_MGR_ODT_MODE_OFF);
1585 ret = (*bit_chk == seq->param.read_correct_mask);
1586 debug_cond(DLEVEL >= 2,
1587 "%s:%d read_test(%u,ALL,%u) => (%u == %u) => %i\n",
1588 __func__, __LINE__, group, all_groups, *bit_chk,
1589 seq->param.read_correct_mask, ret);
1591 ret = (*bit_chk != 0x00);
1592 debug_cond(DLEVEL >= 2,
1593 "%s:%d read_test(%u,ONE,%u) => (%u != %u) => %i\n",
1594 __func__, __LINE__, group, all_groups, *bit_chk,
1602 * rw_mgr_mem_calibrate_read_test_all_ranks() - Perform READ test on all ranks
1603 * @grp: Read/Write group
1604 * @num_tries: Number of retries of the test
1605 * @all_correct: All bits must be correct in the mask
1606 * @all_groups: Test all R/W groups
1608 * Perform a READ test across all memory ranks.
1611 rw_mgr_mem_calibrate_read_test_all_ranks(struct socfpga_sdrseq *seq,
1612 const u32 grp, const u32 num_tries,
1613 const u32 all_correct,
1614 const u32 all_groups)
1617 return rw_mgr_mem_calibrate_read_test(seq, 0, grp, num_tries,
1618 all_correct, &bit_chk, all_groups,
1623 * rw_mgr_incr_vfifo() - Increase VFIFO value
1624 * @grp: Read/Write group
1626 * Increase VFIFO value.
1628 static void rw_mgr_incr_vfifo(const u32 grp)
1630 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
1634 * rw_mgr_decr_vfifo() - Decrease VFIFO value
1635 * @grp: Read/Write group
1637 * Decrease VFIFO value.
1639 static void rw_mgr_decr_vfifo(struct socfpga_sdrseq *seq, const u32 grp)
1643 for (i = 0; i < seq->misccfg->read_valid_fifo_size - 1; i++)
1644 rw_mgr_incr_vfifo(grp);
1648 * find_vfifo_failing_read() - Push VFIFO to get a failing read
1649 * @grp: Read/Write group
1651 * Push VFIFO until a failing read happens.
1653 static int find_vfifo_failing_read(struct socfpga_sdrseq *seq,
1656 u32 v, ret, fail_cnt = 0;
1658 for (v = 0; v < seq->misccfg->read_valid_fifo_size; v++) {
1659 debug_cond(DLEVEL >= 2, "%s:%d: vfifo %u\n",
1660 __func__, __LINE__, v);
1661 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1670 /* Fiddle with FIFO. */
1671 rw_mgr_incr_vfifo(grp);
1674 /* No failing read found! Something must have gone wrong. */
1675 debug_cond(DLEVEL >= 2, "%s:%d: vfifo failed\n", __func__, __LINE__);
1680 * sdr_find_phase_delay() - Find DQS enable phase or delay
1681 * @working: If 1, look for working phase/delay, if 0, look for non-working
1682 * @delay: If 1, look for delay, if 0, look for phase
1683 * @grp: Read/Write group
1684 * @work: Working window position
1685 * @work_inc: Working window increment
1686 * @pd: DQS Phase/Delay Iterator
1688 * Find working or non-working DQS enable phase setting.
1690 static int sdr_find_phase_delay(struct socfpga_sdrseq *seq, int working,
1691 int delay, const u32 grp, u32 *work,
1692 const u32 work_inc, u32 *pd)
1694 const u32 max = delay ? seq->iocfg->dqs_en_delay_max :
1695 seq->iocfg->dqs_en_phase_max;
1698 for (; *pd <= max; (*pd)++) {
1700 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, *pd);
1702 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, *pd);
1704 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1719 * sdr_find_phase() - Find DQS enable phase
1720 * @working: If 1, look for working phase, if 0, look for non-working phase
1721 * @grp: Read/Write group
1722 * @work: Working window position
1724 * @p: DQS Phase Iterator
1726 * Find working or non-working DQS enable phase setting.
1728 static int sdr_find_phase(struct socfpga_sdrseq *seq, int working,
1729 const u32 grp, u32 *work, u32 *i, u32 *p)
1731 const u32 end = seq->misccfg->read_valid_fifo_size + (working ? 0 : 1);
1734 for (; *i < end; (*i)++) {
1738 ret = sdr_find_phase_delay(seq, working, 0, grp, work,
1739 seq->iocfg->delay_per_opa_tap, p);
1743 if (*p > seq->iocfg->dqs_en_phase_max) {
1744 /* Fiddle with FIFO. */
1745 rw_mgr_incr_vfifo(grp);
1755 * sdr_working_phase() - Find working DQS enable phase
1756 * @grp: Read/Write group
1757 * @work_bgn: Working window start position
1758 * @d: dtaps output value
1759 * @p: DQS Phase Iterator
1762 * Find working DQS enable phase setting.
1764 static int sdr_working_phase(struct socfpga_sdrseq *seq, const u32 grp,
1765 u32 *work_bgn, u32 *d, u32 *p, u32 *i)
1767 const u32 dtaps_per_ptap = seq->iocfg->delay_per_opa_tap /
1768 seq->iocfg->delay_per_dqs_en_dchain_tap;
1773 for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
1775 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, *d);
1776 ret = sdr_find_phase(seq, 1, grp, work_bgn, i, p);
1779 *work_bgn += seq->iocfg->delay_per_dqs_en_dchain_tap;
1782 /* Cannot find working solution */
1783 debug_cond(DLEVEL >= 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
1784 __func__, __LINE__);
1789 * sdr_backup_phase() - Find DQS enable backup phase
1790 * @grp: Read/Write group
1791 * @work_bgn: Working window start position
1792 * @p: DQS Phase Iterator
1794 * Find DQS enable backup phase setting.
1796 static void sdr_backup_phase(struct socfpga_sdrseq *seq, const u32 grp,
1797 u32 *work_bgn, u32 *p)
1802 /* Special case code for backing up a phase */
1804 *p = seq->iocfg->dqs_en_phase_max;
1805 rw_mgr_decr_vfifo(seq, grp);
1809 tmp_delay = *work_bgn - seq->iocfg->delay_per_opa_tap;
1810 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, *p);
1812 for (d = 0; d <= seq->iocfg->dqs_en_delay_max && tmp_delay < *work_bgn;
1814 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, d);
1816 ret = rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1819 *work_bgn = tmp_delay;
1823 tmp_delay += seq->iocfg->delay_per_dqs_en_dchain_tap;
1826 /* Restore VFIFO to old state before we decremented it (if needed). */
1828 if (*p > seq->iocfg->dqs_en_phase_max) {
1830 rw_mgr_incr_vfifo(grp);
1833 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, 0);
1837 * sdr_nonworking_phase() - Find non-working DQS enable phase
1838 * @grp: Read/Write group
1839 * @work_end: Working window end position
1840 * @p: DQS Phase Iterator
1843 * Find non-working DQS enable phase setting.
1845 static int sdr_nonworking_phase(struct socfpga_sdrseq *seq,
1846 const u32 grp, u32 *work_end, u32 *p, u32 *i)
1851 *work_end += seq->iocfg->delay_per_opa_tap;
1852 if (*p > seq->iocfg->dqs_en_phase_max) {
1853 /* Fiddle with FIFO. */
1855 rw_mgr_incr_vfifo(grp);
1858 ret = sdr_find_phase(seq, 0, grp, work_end, i, p);
1860 /* Cannot see edge of failing read. */
1861 debug_cond(DLEVEL >= 2, "%s:%d: end: failed\n",
1862 __func__, __LINE__);
1869 * sdr_find_window_center() - Find center of the working DQS window.
1870 * @grp: Read/Write group
1871 * @work_bgn: First working settings
1872 * @work_end: Last working settings
1874 * Find center of the working DQS enable window.
1876 static int sdr_find_window_center(struct socfpga_sdrseq *seq,
1877 const u32 grp, const u32 work_bgn,
1884 work_mid = (work_bgn + work_end) / 2;
1886 debug_cond(DLEVEL >= 2, "work_bgn=%d work_end=%d work_mid=%d\n",
1887 work_bgn, work_end, work_mid);
1888 /* Get the middle delay to be less than a VFIFO delay */
1889 tmp_delay = (seq->iocfg->dqs_en_phase_max + 1)
1890 * seq->iocfg->delay_per_opa_tap;
1892 debug_cond(DLEVEL >= 2, "vfifo ptap delay %d\n", tmp_delay);
1893 work_mid %= tmp_delay;
1894 debug_cond(DLEVEL >= 2, "new work_mid %d\n", work_mid);
1896 tmp_delay = rounddown(work_mid, seq->iocfg->delay_per_opa_tap);
1897 if (tmp_delay > seq->iocfg->dqs_en_phase_max
1898 * seq->iocfg->delay_per_opa_tap) {
1899 tmp_delay = seq->iocfg->dqs_en_phase_max
1900 * seq->iocfg->delay_per_opa_tap;
1902 p = tmp_delay / seq->iocfg->delay_per_opa_tap;
1904 debug_cond(DLEVEL >= 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
1906 d = DIV_ROUND_UP(work_mid - tmp_delay,
1907 seq->iocfg->delay_per_dqs_en_dchain_tap);
1908 if (d > seq->iocfg->dqs_en_delay_max)
1909 d = seq->iocfg->dqs_en_delay_max;
1910 tmp_delay += d * seq->iocfg->delay_per_dqs_en_dchain_tap;
1912 debug_cond(DLEVEL >= 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
1914 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
1915 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, d);
1918 * push vfifo until we can successfully calibrate. We can do this
1919 * because the largest possible margin in 1 VFIFO cycle.
1921 for (i = 0; i < seq->misccfg->read_valid_fifo_size; i++) {
1922 debug_cond(DLEVEL >= 2, "find_dqs_en_phase: center\n");
1923 if (rw_mgr_mem_calibrate_read_test_all_ranks(seq, grp, 1,
1926 debug_cond(DLEVEL >= 2,
1927 "%s:%d center: found: ptap=%u dtap=%u\n",
1928 __func__, __LINE__, p, d);
1932 /* Fiddle with FIFO. */
1933 rw_mgr_incr_vfifo(grp);
1936 debug_cond(DLEVEL >= 2, "%s:%d center: failed.\n",
1937 __func__, __LINE__);
1942 * rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase() - Find a good DQS enable to
1944 * @grp: Read/Write Group
1946 * Find a good DQS enable to use.
1949 rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(struct socfpga_sdrseq *seq,
1954 u32 work_bgn, work_end;
1955 u32 found_passing_read, found_failing_read = 0, initial_failing_dtap;
1958 debug("%s:%d %u\n", __func__, __LINE__, grp);
1960 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1962 scc_mgr_set_dqs_en_delay_all_ranks(seq, grp, 0);
1963 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, 0);
1965 /* Step 0: Determine number of delay taps for each phase tap. */
1966 dtaps_per_ptap = seq->iocfg->delay_per_opa_tap /
1967 seq->iocfg->delay_per_dqs_en_dchain_tap;
1969 /* Step 1: First push vfifo until we get a failing read. */
1970 find_vfifo_failing_read(seq, grp);
1972 /* Step 2: Find first working phase, increment in ptaps. */
1974 ret = sdr_working_phase(seq, grp, &work_bgn, &d, &p, &i);
1978 work_end = work_bgn;
1981 * If d is 0 then the working window covers a phase tap and we can
1982 * follow the old procedure. Otherwise, we've found the beginning
1983 * and we need to increment the dtaps until we find the end.
1987 * Step 3a: If we have room, back off by one and
1988 * increment in dtaps.
1990 sdr_backup_phase(seq, grp, &work_bgn, &p);
1993 * Step 4a: go forward from working phase to non working
1994 * phase, increment in ptaps.
1996 ret = sdr_nonworking_phase(seq, grp, &work_end, &p, &i);
2000 /* Step 5a: Back off one from last, increment in dtaps. */
2002 /* Special case code for backing up a phase */
2004 p = seq->iocfg->dqs_en_phase_max;
2005 rw_mgr_decr_vfifo(seq, grp);
2010 work_end -= seq->iocfg->delay_per_opa_tap;
2011 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
2015 debug_cond(DLEVEL >= 2, "%s:%d p: ptap=%u\n",
2016 __func__, __LINE__, p);
2019 /* The dtap increment to find the failing edge is done here. */
2020 sdr_find_phase_delay(seq, 0, 1, grp, &work_end,
2021 seq->iocfg->delay_per_dqs_en_dchain_tap, &d);
2023 /* Go back to working dtap */
2025 work_end -= seq->iocfg->delay_per_dqs_en_dchain_tap;
2027 debug_cond(DLEVEL >= 2,
2028 "%s:%d p/d: ptap=%u dtap=%u end=%u\n",
2029 __func__, __LINE__, p, d - 1, work_end);
2031 if (work_end < work_bgn) {
2033 debug_cond(DLEVEL >= 2, "%s:%d end-2: failed\n",
2034 __func__, __LINE__);
2038 debug_cond(DLEVEL >= 2, "%s:%d found range [%u,%u]\n",
2039 __func__, __LINE__, work_bgn, work_end);
2042 * We need to calculate the number of dtaps that equal a ptap.
2043 * To do that we'll back up a ptap and re-find the edge of the
2044 * window using dtaps
2046 debug_cond(DLEVEL >= 2, "%s:%d calculate dtaps_per_ptap for tracking\n",
2047 __func__, __LINE__);
2049 /* Special case code for backing up a phase */
2051 p = seq->iocfg->dqs_en_phase_max;
2052 rw_mgr_decr_vfifo(seq, grp);
2053 debug_cond(DLEVEL >= 2, "%s:%d backedup cycle/phase: p=%u\n",
2054 __func__, __LINE__, p);
2057 debug_cond(DLEVEL >= 2, "%s:%d backedup phase only: p=%u",
2058 __func__, __LINE__, p);
2061 scc_mgr_set_dqs_en_phase_all_ranks(seq, grp, p);
2064 * Increase dtap until we first see a passing read (in case the
2065 * window is smaller than a ptap), and then a failing read to
2066 * mark the edge of the window again.
2069 /* Find a passing read. */
2070 debug_cond(DLEVEL >= 2, "%s:%d find passing read\n",
2071 __func__, __LINE__);
2073 initial_failing_dtap = d;
2075 found_passing_read = !sdr_find_phase_delay(seq, 1, 1, grp, NULL, 0, &d);
2076 if (found_passing_read) {
2077 /* Find a failing read. */
2078 debug_cond(DLEVEL >= 2, "%s:%d find failing read\n",
2079 __func__, __LINE__);
2081 found_failing_read = !sdr_find_phase_delay(seq, 0, 1, grp, NULL,
2084 debug_cond(DLEVEL >= 1,
2085 "%s:%d failed to calculate dtaps per ptap. Fall back on static value\n",
2086 __func__, __LINE__);
2090 * The dynamically calculated dtaps_per_ptap is only valid if we
2091 * found a passing/failing read. If we didn't, it means d hit the max
2092 * (seq->iocfg->dqs_en_delay_max). Otherwise, dtaps_per_ptap retains its
2093 * statically calculated value.
2095 if (found_passing_read && found_failing_read)
2096 dtaps_per_ptap = d - initial_failing_dtap;
2098 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
2099 debug_cond(DLEVEL >= 2, "%s:%d dtaps_per_ptap=%u - %u = %u",
2100 __func__, __LINE__, d, initial_failing_dtap, dtaps_per_ptap);
2102 /* Step 6: Find the centre of the window. */
2103 ret = sdr_find_window_center(seq, grp, work_bgn, work_end);
2109 * search_stop_check() - Check if the detected edge is valid
2110 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2112 * @rank_bgn: Rank number
2113 * @write_group: Write Group
2114 * @read_group: Read Group
2115 * @bit_chk: Resulting bit mask after the test
2116 * @sticky_bit_chk: Resulting sticky bit mask after the test
2117 * @use_read_test: Perform read test
2119 * Test if the found edge is valid.
2121 static u32 search_stop_check(struct socfpga_sdrseq *seq, const int write,
2122 const int d, const int rank_bgn,
2123 const u32 write_group, const u32 read_group,
2124 u32 *bit_chk, u32 *sticky_bit_chk,
2125 const u32 use_read_test)
2127 const u32 ratio = seq->rwcfg->mem_if_read_dqs_width /
2128 seq->rwcfg->mem_if_write_dqs_width;
2129 const u32 correct_mask = write ? seq->param.write_correct_mask :
2130 seq->param.read_correct_mask;
2131 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2132 seq->rwcfg->mem_dq_per_read_dqs;
2135 * Stop searching when the read test doesn't pass AND when
2136 * we've seen a passing read on every bit.
2138 if (write) { /* WRITE-ONLY */
2139 ret = !rw_mgr_mem_calibrate_write_test(seq, rank_bgn,
2141 PASS_ONE_BIT, bit_chk,
2143 } else if (use_read_test) { /* READ-ONLY */
2144 ret = !rw_mgr_mem_calibrate_read_test(seq, rank_bgn, read_group,
2146 PASS_ONE_BIT, bit_chk,
2148 } else { /* READ-ONLY */
2149 rw_mgr_mem_calibrate_write_test(seq, rank_bgn, write_group, 0,
2150 PASS_ONE_BIT, bit_chk, 0);
2151 *bit_chk = *bit_chk >> (per_dqs *
2152 (read_group - (write_group * ratio)));
2153 ret = (*bit_chk == 0);
2155 *sticky_bit_chk = *sticky_bit_chk | *bit_chk;
2156 ret = ret && (*sticky_bit_chk == correct_mask);
2157 debug_cond(DLEVEL >= 2,
2158 "%s:%d center(left): dtap=%u => %u == %u && %u",
2159 __func__, __LINE__, d,
2160 *sticky_bit_chk, correct_mask, ret);
2165 * search_left_edge() - Find left edge of DQ/DQS working phase
2166 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2167 * @rank_bgn: Rank number
2168 * @write_group: Write Group
2169 * @read_group: Read Group
2170 * @test_bgn: Rank number to begin the test
2171 * @sticky_bit_chk: Resulting sticky bit mask after the test
2172 * @left_edge: Left edge of the DQ/DQS phase
2173 * @right_edge: Right edge of the DQ/DQS phase
2174 * @use_read_test: Perform read test
2176 * Find left edge of DQ/DQS working phase.
2178 static void search_left_edge(struct socfpga_sdrseq *seq, const int write,
2179 const int rank_bgn, const u32 write_group,
2180 const u32 read_group, const u32 test_bgn,
2181 u32 *sticky_bit_chk, int *left_edge,
2182 int *right_edge, const u32 use_read_test)
2184 const u32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2185 seq->iocfg->io_in_delay_max;
2186 const u32 dqs_max = write ? seq->iocfg->io_out1_delay_max :
2187 seq->iocfg->dqs_in_delay_max;
2188 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2189 seq->rwcfg->mem_dq_per_read_dqs;
2193 for (d = 0; d <= dqs_max; d++) {
2195 scc_mgr_apply_group_dq_out1_delay(seq, d);
2197 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, d);
2199 writel(0, &sdr_scc_mgr->update);
2201 stop = search_stop_check(seq, write, d, rank_bgn, write_group,
2202 read_group, &bit_chk, sticky_bit_chk,
2208 for (i = 0; i < per_dqs; i++) {
2211 * Remember a passing test as
2217 * If a left edge has not been seen
2218 * yet, then a future passing test
2219 * will mark this edge as the right
2222 if (left_edge[i] == delay_max + 1)
2223 right_edge[i] = -(d + 1);
2229 /* Reset DQ delay chains to 0 */
2231 scc_mgr_apply_group_dq_out1_delay(seq, 0);
2233 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, 0);
2235 *sticky_bit_chk = 0;
2236 for (i = per_dqs - 1; i >= 0; i--) {
2237 debug_cond(DLEVEL >= 2,
2238 "%s:%d vfifo_center: left_edge[%u]: %d right_edge[%u]: %d\n",
2239 __func__, __LINE__, i, left_edge[i],
2243 * Check for cases where we haven't found the left edge,
2244 * which makes our assignment of the the right edge invalid.
2245 * Reset it to the illegal value.
2247 if ((left_edge[i] == delay_max + 1) &&
2248 (right_edge[i] != delay_max + 1)) {
2249 right_edge[i] = delay_max + 1;
2250 debug_cond(DLEVEL >= 2,
2251 "%s:%d vfifo_center: reset right_edge[%u]: %d\n",
2252 __func__, __LINE__, i, right_edge[i]);
2257 * READ: except for bits where we have seen both
2258 * the left and right edge.
2259 * WRITE: except for bits where we have seen the
2262 *sticky_bit_chk <<= 1;
2264 if (left_edge[i] != delay_max + 1)
2265 *sticky_bit_chk |= 1;
2267 if ((left_edge[i] != delay_max + 1) &&
2268 (right_edge[i] != delay_max + 1))
2269 *sticky_bit_chk |= 1;
2275 * search_right_edge() - Find right edge of DQ/DQS working phase
2276 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2277 * @rank_bgn: Rank number
2278 * @write_group: Write Group
2279 * @read_group: Read Group
2280 * @start_dqs: DQS start phase
2281 * @start_dqs_en: DQS enable start phase
2282 * @sticky_bit_chk: Resulting sticky bit mask after the test
2283 * @left_edge: Left edge of the DQ/DQS phase
2284 * @right_edge: Right edge of the DQ/DQS phase
2285 * @use_read_test: Perform read test
2287 * Find right edge of DQ/DQS working phase.
2289 static int search_right_edge(struct socfpga_sdrseq *seq, const int write,
2290 const int rank_bgn, const u32 write_group,
2291 const u32 read_group, const int start_dqs,
2292 const int start_dqs_en, u32 *sticky_bit_chk,
2293 int *left_edge, int *right_edge,
2294 const u32 use_read_test)
2296 const u32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2297 seq->iocfg->io_in_delay_max;
2298 const u32 dqs_max = write ? seq->iocfg->io_out1_delay_max :
2299 seq->iocfg->dqs_in_delay_max;
2300 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2301 seq->rwcfg->mem_dq_per_read_dqs;
2305 for (d = 0; d <= dqs_max - start_dqs; d++) {
2306 if (write) { /* WRITE-ONLY */
2307 scc_mgr_apply_group_dqs_io_and_oct_out1(seq,
2310 } else { /* READ-ONLY */
2311 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
2312 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
2313 u32 delay = d + start_dqs_en;
2314 if (delay > seq->iocfg->dqs_en_delay_max)
2315 delay = seq->iocfg->dqs_en_delay_max;
2316 scc_mgr_set_dqs_en_delay(read_group, delay);
2318 scc_mgr_load_dqs(read_group);
2321 writel(0, &sdr_scc_mgr->update);
2323 stop = search_stop_check(seq, write, d, rank_bgn, write_group,
2324 read_group, &bit_chk, sticky_bit_chk,
2327 if (write && (d == 0)) { /* WRITE-ONLY */
2329 i < seq->rwcfg->mem_dq_per_write_dqs;
2332 * d = 0 failed, but it passed when
2333 * testing the left edge, so it must be
2334 * marginal, set it to -1
2336 if (right_edge[i] == delay_max + 1 &&
2337 left_edge[i] != delay_max + 1)
2345 for (i = 0; i < per_dqs; i++) {
2348 * Remember a passing test as
2355 * If a right edge has not
2356 * been seen yet, then a future
2357 * passing test will mark this
2358 * edge as the left edge.
2360 if (right_edge[i] == delay_max + 1)
2361 left_edge[i] = -(d + 1);
2364 * d = 0 failed, but it passed
2365 * when testing the left edge,
2366 * so it must be marginal, set
2369 if (right_edge[i] == delay_max + 1 &&
2370 left_edge[i] != delay_max + 1)
2373 * If a right edge has not been
2374 * seen yet, then a future
2375 * passing test will mark this
2376 * edge as the left edge.
2378 else if (right_edge[i] == delay_max + 1)
2379 left_edge[i] = -(d + 1);
2383 debug_cond(DLEVEL >= 2, "%s:%d center[r,d=%u]: ",
2384 __func__, __LINE__, d);
2385 debug_cond(DLEVEL >= 2,
2386 "bit_chk_test=%i left_edge[%u]: %d ",
2387 bit_chk & 1, i, left_edge[i]);
2388 debug_cond(DLEVEL >= 2, "right_edge[%u]: %d\n", i,
2394 /* Check that all bits have a window */
2395 for (i = 0; i < per_dqs; i++) {
2396 debug_cond(DLEVEL >= 2,
2397 "%s:%d write_center: left_edge[%u]: %d right_edge[%u]: %d",
2398 __func__, __LINE__, i, left_edge[i],
2400 if ((left_edge[i] == dqs_max + 1) ||
2401 (right_edge[i] == dqs_max + 1))
2402 return i + 1; /* FIXME: If we fail, retval > 0 */
2409 * get_window_mid_index() - Find the best middle setting of DQ/DQS phase
2410 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2411 * @left_edge: Left edge of the DQ/DQS phase
2412 * @right_edge: Right edge of the DQ/DQS phase
2413 * @mid_min: Best DQ/DQS phase middle setting
2415 * Find index and value of the middle of the DQ/DQS working phase.
2417 static int get_window_mid_index(struct socfpga_sdrseq *seq,
2418 const int write, int *left_edge,
2419 int *right_edge, int *mid_min)
2421 const u32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2422 seq->rwcfg->mem_dq_per_read_dqs;
2423 int i, mid, min_index;
2425 /* Find middle of window for each DQ bit */
2426 *mid_min = left_edge[0] - right_edge[0];
2428 for (i = 1; i < per_dqs; i++) {
2429 mid = left_edge[i] - right_edge[i];
2430 if (mid < *mid_min) {
2437 * -mid_min/2 represents the amount that we need to move DQS.
2438 * If mid_min is odd and positive we'll need to add one to make
2439 * sure the rounding in further calculations is correct (always
2440 * bias to the right), so just add 1 for all positive values.
2444 *mid_min = *mid_min / 2;
2446 debug_cond(DLEVEL >= 1, "%s:%d vfifo_center: *mid_min=%d (index=%u)\n",
2447 __func__, __LINE__, *mid_min, min_index);
2452 * center_dq_windows() - Center the DQ/DQS windows
2453 * @write: Perform read (Stage 2) or write (Stage 3) calibration
2454 * @left_edge: Left edge of the DQ/DQS phase
2455 * @right_edge: Right edge of the DQ/DQS phase
2456 * @mid_min: Adjusted DQ/DQS phase middle setting
2457 * @orig_mid_min: Original DQ/DQS phase middle setting
2458 * @min_index: DQ/DQS phase middle setting index
2459 * @test_bgn: Rank number to begin the test
2460 * @dq_margin: Amount of shift for the DQ
2461 * @dqs_margin: Amount of shift for the DQS
2463 * Align the DQ/DQS windows in each group.
2465 static void center_dq_windows(struct socfpga_sdrseq *seq,
2466 const int write, int *left_edge, int *right_edge,
2467 const int mid_min, const int orig_mid_min,
2468 const int min_index, const int test_bgn,
2469 int *dq_margin, int *dqs_margin)
2471 const s32 delay_max = write ? seq->iocfg->io_out1_delay_max :
2472 seq->iocfg->io_in_delay_max;
2473 const s32 per_dqs = write ? seq->rwcfg->mem_dq_per_write_dqs :
2474 seq->rwcfg->mem_dq_per_read_dqs;
2475 const s32 delay_off = write ? SCC_MGR_IO_OUT1_DELAY_OFFSET :
2476 SCC_MGR_IO_IN_DELAY_OFFSET;
2477 const s32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | delay_off;
2479 s32 temp_dq_io_delay1;
2482 /* Initialize data for export structures */
2483 *dqs_margin = delay_max + 1;
2484 *dq_margin = delay_max + 1;
2486 /* add delay to bring centre of all DQ windows to the same "level" */
2487 for (i = 0, p = test_bgn; i < per_dqs; i++, p++) {
2488 /* Use values before divide by 2 to reduce round off error */
2489 shift_dq = (left_edge[i] - right_edge[i] -
2490 (left_edge[min_index] - right_edge[min_index]))/2 +
2491 (orig_mid_min - mid_min);
2493 debug_cond(DLEVEL >= 2,
2494 "vfifo_center: before: shift_dq[%u]=%d\n",
2497 temp_dq_io_delay1 = readl(addr + (i << 2));
2499 if (shift_dq + temp_dq_io_delay1 > delay_max)
2500 shift_dq = delay_max - temp_dq_io_delay1;
2501 else if (shift_dq + temp_dq_io_delay1 < 0)
2502 shift_dq = -temp_dq_io_delay1;
2504 debug_cond(DLEVEL >= 2,
2505 "vfifo_center: after: shift_dq[%u]=%d\n",
2509 scc_mgr_set_dq_out1_delay(i,
2510 temp_dq_io_delay1 + shift_dq);
2512 scc_mgr_set_dq_in_delay(p,
2513 temp_dq_io_delay1 + shift_dq);
2517 debug_cond(DLEVEL >= 2,
2518 "vfifo_center: margin[%u]=[%d,%d]\n", i,
2519 left_edge[i] - shift_dq + (-mid_min),
2520 right_edge[i] + shift_dq - (-mid_min));
2522 /* To determine values for export structures */
2523 if (left_edge[i] - shift_dq + (-mid_min) < *dq_margin)
2524 *dq_margin = left_edge[i] - shift_dq + (-mid_min);
2526 if (right_edge[i] + shift_dq - (-mid_min) < *dqs_margin)
2527 *dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2532 * rw_mgr_mem_calibrate_vfifo_center() - Per-bit deskew DQ and centering
2533 * @rank_bgn: Rank number
2534 * @rw_group: Read/Write Group
2535 * @test_bgn: Rank at which the test begins
2536 * @use_read_test: Perform a read test
2537 * @update_fom: Update FOM
2539 * Per-bit deskew DQ and centering.
2541 static int rw_mgr_mem_calibrate_vfifo_center(struct socfpga_sdrseq *seq,
2545 const int use_read_test,
2546 const int update_fom)
2549 SDR_PHYGRP_SCCGRP_ADDRESS + SCC_MGR_DQS_IN_DELAY_OFFSET +
2552 * Store these as signed since there are comparisons with
2556 s32 left_edge[seq->rwcfg->mem_dq_per_read_dqs];
2557 s32 right_edge[seq->rwcfg->mem_dq_per_read_dqs];
2558 s32 orig_mid_min, mid_min;
2559 s32 new_dqs, start_dqs, start_dqs_en = 0, final_dqs_en;
2560 s32 dq_margin, dqs_margin;
2564 debug("%s:%d: %u %u", __func__, __LINE__, rw_group, test_bgn);
2566 start_dqs = readl(addr);
2567 if (seq->iocfg->shift_dqs_en_when_shift_dqs)
2568 start_dqs_en = readl(addr - seq->iocfg->dqs_en_delay_offset);
2570 /* set the left and right edge of each bit to an illegal value */
2571 /* use (seq->iocfg->io_in_delay_max + 1) as an illegal value */
2573 for (i = 0; i < seq->rwcfg->mem_dq_per_read_dqs; i++) {
2574 left_edge[i] = seq->iocfg->io_in_delay_max + 1;
2575 right_edge[i] = seq->iocfg->io_in_delay_max + 1;
2578 /* Search for the left edge of the window for each bit */
2579 search_left_edge(seq, 0, rank_bgn, rw_group, rw_group, test_bgn,
2581 left_edge, right_edge, use_read_test);
2584 /* Search for the right edge of the window for each bit */
2585 ret = search_right_edge(seq, 0, rank_bgn, rw_group, rw_group,
2586 start_dqs, start_dqs_en,
2588 left_edge, right_edge, use_read_test);
2591 * Restore delay chain settings before letting the loop
2592 * in rw_mgr_mem_calibrate_vfifo to retry different
2593 * dqs/ck relationships.
2595 scc_mgr_set_dqs_bus_in_delay(rw_group, start_dqs);
2596 if (seq->iocfg->shift_dqs_en_when_shift_dqs)
2597 scc_mgr_set_dqs_en_delay(rw_group, start_dqs_en);
2599 scc_mgr_load_dqs(rw_group);
2600 writel(0, &sdr_scc_mgr->update);
2602 debug_cond(DLEVEL >= 1,
2603 "%s:%d vfifo_center: failed to find edge [%u]: %d %d",
2604 __func__, __LINE__, i, left_edge[i], right_edge[i]);
2605 if (use_read_test) {
2606 set_failing_group_stage(seq, rw_group *
2607 seq->rwcfg->mem_dq_per_read_dqs + i,
2609 CAL_SUBSTAGE_VFIFO_CENTER);
2611 set_failing_group_stage(seq, rw_group *
2612 seq->rwcfg->mem_dq_per_read_dqs + i,
2613 CAL_STAGE_VFIFO_AFTER_WRITES,
2614 CAL_SUBSTAGE_VFIFO_CENTER);
2619 min_index = get_window_mid_index(seq, 0, left_edge, right_edge,
2622 /* Determine the amount we can change DQS (which is -mid_min) */
2623 orig_mid_min = mid_min;
2624 new_dqs = start_dqs - mid_min;
2625 if (new_dqs > seq->iocfg->dqs_in_delay_max)
2626 new_dqs = seq->iocfg->dqs_in_delay_max;
2627 else if (new_dqs < 0)
2630 mid_min = start_dqs - new_dqs;
2631 debug_cond(DLEVEL >= 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2634 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
2635 if (start_dqs_en - mid_min > seq->iocfg->dqs_en_delay_max)
2636 mid_min += start_dqs_en - mid_min -
2637 seq->iocfg->dqs_en_delay_max;
2638 else if (start_dqs_en - mid_min < 0)
2639 mid_min += start_dqs_en - mid_min;
2641 new_dqs = start_dqs - mid_min;
2643 debug_cond(DLEVEL >= 1,
2644 "vfifo_center: start_dqs=%d start_dqs_en=%d new_dqs=%d mid_min=%d\n",
2646 seq->iocfg->shift_dqs_en_when_shift_dqs ? start_dqs_en : -1,
2649 /* Add delay to bring centre of all DQ windows to the same "level". */
2650 center_dq_windows(seq, 0, left_edge, right_edge, mid_min, orig_mid_min,
2651 min_index, test_bgn, &dq_margin, &dqs_margin);
2654 if (seq->iocfg->shift_dqs_en_when_shift_dqs) {
2655 final_dqs_en = start_dqs_en - mid_min;
2656 scc_mgr_set_dqs_en_delay(rw_group, final_dqs_en);
2657 scc_mgr_load_dqs(rw_group);
2661 scc_mgr_set_dqs_bus_in_delay(rw_group, new_dqs);
2662 scc_mgr_load_dqs(rw_group);
2663 debug_cond(DLEVEL >= 2,
2664 "%s:%d vfifo_center: dq_margin=%d dqs_margin=%d",
2665 __func__, __LINE__, dq_margin, dqs_margin);
2668 * Do not remove this line as it makes sure all of our decisions
2669 * have been applied. Apply the update bit.
2671 writel(0, &sdr_scc_mgr->update);
2673 if ((dq_margin < 0) || (dqs_margin < 0))
2680 * rw_mgr_mem_calibrate_guaranteed_write() - Perform guaranteed write into the
2682 * @rw_group: Read/Write Group
2683 * @phase: DQ/DQS phase
2685 * Because initially no communication ca be reliably performed with the memory
2686 * device, the sequencer uses a guaranteed write mechanism to write data into
2687 * the memory device.
2689 static int rw_mgr_mem_calibrate_guaranteed_write(struct socfpga_sdrseq *seq,
2695 /* Set a particular DQ/DQS phase. */
2696 scc_mgr_set_dqdqs_output_phase_all_ranks(seq, rw_group, phase);
2698 debug_cond(DLEVEL >= 1, "%s:%d guaranteed write: g=%u p=%u\n",
2699 __func__, __LINE__, rw_group, phase);
2702 * Altera EMI_RM 2015.05.04 :: Figure 1-25
2703 * Load up the patterns used by read calibration using the
2704 * current DQDQS phase.
2706 rw_mgr_mem_calibrate_read_load_patterns(seq, 0, 1);
2708 if (seq->gbl.phy_debug_mode_flags & PHY_DEBUG_DISABLE_GUARANTEED_READ)
2712 * Altera EMI_RM 2015.05.04 :: Figure 1-26
2713 * Back-to-Back reads of the patterns used for calibration.
2715 ret = rw_mgr_mem_calibrate_read_test_patterns(seq, 0, rw_group, 1);
2717 debug_cond(DLEVEL >= 1,
2718 "%s:%d Guaranteed read test failed: g=%u p=%u\n",
2719 __func__, __LINE__, rw_group, phase);
2724 * rw_mgr_mem_calibrate_dqs_enable_calibration() - DQS Enable Calibration
2725 * @rw_group: Read/Write Group
2726 * @test_bgn: Rank at which the test begins
2728 * DQS enable calibration ensures reliable capture of the DQ signal without
2729 * glitches on the DQS line.
2732 rw_mgr_mem_calibrate_dqs_enable_calibration(struct socfpga_sdrseq *seq,
2737 * Altera EMI_RM 2015.05.04 :: Figure 1-27
2738 * DQS and DQS Eanble Signal Relationships.
2741 /* We start at zero, so have one less dq to devide among */
2742 const u32 delay_step = seq->iocfg->io_in_delay_max /
2743 (seq->rwcfg->mem_dq_per_read_dqs - 1);
2747 debug("%s:%d (%u,%u)\n", __func__, __LINE__, rw_group, test_bgn);
2749 /* Try different dq_in_delays since the DQ path is shorter than DQS. */
2750 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
2751 r += NUM_RANKS_PER_SHADOW_REG) {
2752 for (i = 0, p = test_bgn, d = 0;
2753 i < seq->rwcfg->mem_dq_per_read_dqs;
2754 i++, p++, d += delay_step) {
2755 debug_cond(DLEVEL >= 1,
2756 "%s:%d: g=%u r=%u i=%u p=%u d=%u\n",
2757 __func__, __LINE__, rw_group, r, i, p, d);
2759 scc_mgr_set_dq_in_delay(p, d);
2763 writel(0, &sdr_scc_mgr->update);
2767 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
2768 * dq_in_delay values
2770 ret = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(seq, rw_group);
2772 debug_cond(DLEVEL >= 1,
2773 "%s:%d: g=%u found=%u; Reseting delay chain to zero\n",
2774 __func__, __LINE__, rw_group, !ret);
2776 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
2777 r += NUM_RANKS_PER_SHADOW_REG) {
2778 scc_mgr_apply_group_dq_in_delay(seq, test_bgn, 0);
2779 writel(0, &sdr_scc_mgr->update);
2786 * rw_mgr_mem_calibrate_dq_dqs_centering() - Centering DQ/DQS
2787 * @rw_group: Read/Write Group
2788 * @test_bgn: Rank at which the test begins
2789 * @use_read_test: Perform a read test
2790 * @update_fom: Update FOM
2792 * The centerin DQ/DQS stage attempts to align DQ and DQS signals on reads
2796 rw_mgr_mem_calibrate_dq_dqs_centering(struct socfpga_sdrseq *seq,
2797 const u32 rw_group, const u32 test_bgn,
2798 const int use_read_test,
2799 const int update_fom)
2802 int ret, grp_calibrated;
2806 * Altera EMI_RM 2015.05.04 :: Figure 1-28
2807 * Read per-bit deskew can be done on a per shadow register basis.
2810 for (rank_bgn = 0, sr = 0;
2811 rank_bgn < seq->rwcfg->mem_number_of_ranks;
2812 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
2813 ret = rw_mgr_mem_calibrate_vfifo_center(seq, rank_bgn, rw_group,
2823 if (!grp_calibrated)
2830 * rw_mgr_mem_calibrate_vfifo() - Calibrate the read valid prediction FIFO
2831 * @rw_group: Read/Write Group
2832 * @test_bgn: Rank at which the test begins
2834 * Stage 1: Calibrate the read valid prediction FIFO.
2836 * This function implements UniPHY calibration Stage 1, as explained in
2837 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2839 * - read valid prediction will consist of finding:
2840 * - DQS enable phase and DQS enable delay (DQS Enable Calibration)
2841 * - DQS input phase and DQS input delay (DQ/DQS Centering)
2842 * - we also do a per-bit deskew on the DQ lines.
2844 static int rw_mgr_mem_calibrate_vfifo(struct socfpga_sdrseq *seq,
2845 const u32 rw_group, const u32 test_bgn)
2849 u32 failed_substage;
2853 debug("%s:%d: %u %u\n", __func__, __LINE__, rw_group, test_bgn);
2855 /* Update info for sims */
2856 reg_file_set_group(rw_group);
2857 reg_file_set_stage(CAL_STAGE_VFIFO);
2858 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
2860 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2862 /* USER Determine number of delay taps for each phase tap. */
2863 dtaps_per_ptap = DIV_ROUND_UP(seq->iocfg->delay_per_opa_tap,
2864 seq->iocfg->delay_per_dqs_en_dchain_tap)
2867 for (d = 0; d <= dtaps_per_ptap; d += 2) {
2869 * In RLDRAMX we may be messing the delay of pins in
2870 * the same write rw_group but outside of the current read
2871 * the rw_group, but that's ok because we haven't calibrated
2875 scc_mgr_apply_group_all_out_delay_add_all_ranks(seq,
2880 for (p = 0; p <= seq->iocfg->dqdqs_out_phase_max; p++) {
2881 /* 1) Guaranteed Write */
2882 ret = rw_mgr_mem_calibrate_guaranteed_write(seq,
2888 /* 2) DQS Enable Calibration */
2889 ret = rw_mgr_mem_calibrate_dqs_enable_calibration(seq,
2893 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2897 /* 3) Centering DQ/DQS */
2899 * If doing read after write calibration, do not update
2900 * FOM now. Do it then.
2902 ret = rw_mgr_mem_calibrate_dq_dqs_centering(seq,
2907 failed_substage = CAL_SUBSTAGE_VFIFO_CENTER;
2916 /* Calibration Stage 1 failed. */
2917 set_failing_group_stage(seq, rw_group, CAL_STAGE_VFIFO,
2921 /* Calibration Stage 1 completed OK. */
2924 * Reset the delay chains back to zero if they have moved > 1
2925 * (check for > 1 because loop will increase d even when pass in
2929 scc_mgr_zero_group(seq, rw_group, 1);
2935 * rw_mgr_mem_calibrate_vfifo_end() - DQ/DQS Centering.
2936 * @rw_group: Read/Write Group
2937 * @test_bgn: Rank at which the test begins
2939 * Stage 3: DQ/DQS Centering.
2941 * This function implements UniPHY calibration Stage 3, as explained in
2942 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2944 static int rw_mgr_mem_calibrate_vfifo_end(struct socfpga_sdrseq *seq,
2950 debug("%s:%d %u %u", __func__, __LINE__, rw_group, test_bgn);
2952 /* Update info for sims. */
2953 reg_file_set_group(rw_group);
2954 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2955 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2957 ret = rw_mgr_mem_calibrate_dq_dqs_centering(seq, rw_group, test_bgn, 0,
2960 set_failing_group_stage(seq, rw_group,
2961 CAL_STAGE_VFIFO_AFTER_WRITES,
2962 CAL_SUBSTAGE_VFIFO_CENTER);
2967 * rw_mgr_mem_calibrate_lfifo() - Minimize latency
2969 * Stage 4: Minimize latency.
2971 * This function implements UniPHY calibration Stage 4, as explained in
2972 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
2973 * Calibrate LFIFO to find smallest read latency.
2975 static u32 rw_mgr_mem_calibrate_lfifo(struct socfpga_sdrseq *seq)
2979 debug("%s:%d\n", __func__, __LINE__);
2981 /* Update info for sims. */
2982 reg_file_set_stage(CAL_STAGE_LFIFO);
2983 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2985 /* Load up the patterns used by read calibration for all ranks */
2986 rw_mgr_mem_calibrate_read_load_patterns(seq, 0, 1);
2989 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
2990 debug_cond(DLEVEL >= 2, "%s:%d lfifo: read_lat=%u",
2991 __func__, __LINE__, seq->gbl.curr_read_lat);
2993 if (!rw_mgr_mem_calibrate_read_test_all_ranks(seq, 0,
3000 * Reduce read latency and see if things are
3001 * working correctly.
3003 seq->gbl.curr_read_lat--;
3004 } while (seq->gbl.curr_read_lat > 0);
3006 /* Reset the fifos to get pointers to known state. */
3007 writel(0, &phy_mgr_cmd->fifo_reset);
3010 /* Add a fudge factor to the read latency that was determined */
3011 seq->gbl.curr_read_lat += 2;
3012 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
3013 debug_cond(DLEVEL >= 2,
3014 "%s:%d lfifo: success: using read_lat=%u\n",
3015 __func__, __LINE__, seq->gbl.curr_read_lat);
3017 set_failing_group_stage(seq, 0xff, CAL_STAGE_LFIFO,
3018 CAL_SUBSTAGE_READ_LATENCY);
3020 debug_cond(DLEVEL >= 2,
3021 "%s:%d lfifo: failed at initial read_lat=%u\n",
3022 __func__, __LINE__, seq->gbl.curr_read_lat);
3029 * search_window() - Search for the/part of the window with DM/DQS shift
3030 * @search_dm: If 1, search for the DM shift, if 0, search for DQS
3032 * @rank_bgn: Rank number
3033 * @write_group: Write Group
3034 * @bgn_curr: Current window begin
3035 * @end_curr: Current window end
3036 * @bgn_best: Current best window begin
3037 * @end_best: Current best window end
3038 * @win_best: Size of the best window
3039 * @new_dqs: New DQS value (only applicable if search_dm = 0).
3041 * Search for the/part of the window with DM/DQS shift.
3043 static void search_window(struct socfpga_sdrseq *seq,
3044 const int search_dm, const u32 rank_bgn,
3045 const u32 write_group, int *bgn_curr, int *end_curr,
3046 int *bgn_best, int *end_best, int *win_best,
3050 const int max = seq->iocfg->io_out1_delay_max - new_dqs;
3053 /* Search for the/part of the window with DM/DQS shift. */
3054 for (di = max; di >= 0; di -= DELTA_D) {
3057 scc_mgr_apply_group_dm_out1_delay(seq, d);
3059 /* For DQS, we go from 0...max */
3062 * Note: This only shifts DQS, so are we limiting
3063 * ourselves to width of DQ unnecessarily.
3065 scc_mgr_apply_group_dqs_io_and_oct_out1(seq,
3070 writel(0, &sdr_scc_mgr->update);
3072 if (rw_mgr_mem_calibrate_write_test(seq, rank_bgn, write_group,
3073 1, PASS_ALL_BITS, &bit_chk,
3075 /* Set current end of the window. */
3076 *end_curr = search_dm ? -d : d;
3079 * If a starting edge of our window has not been seen
3080 * this is our current start of the DM window.
3082 if (*bgn_curr == seq->iocfg->io_out1_delay_max + 1)
3083 *bgn_curr = search_dm ? -d : d;
3086 * If current window is bigger than best seen.
3087 * Set best seen to be current window.
3089 if ((*end_curr - *bgn_curr + 1) > *win_best) {
3090 *win_best = *end_curr - *bgn_curr + 1;
3091 *bgn_best = *bgn_curr;
3092 *end_best = *end_curr;
3095 /* We just saw a failing test. Reset temp edge. */
3096 *bgn_curr = seq->iocfg->io_out1_delay_max + 1;
3097 *end_curr = seq->iocfg->io_out1_delay_max + 1;
3099 /* Early exit is only applicable to DQS. */
3104 * Early exit optimization: if the remaining delay
3105 * chain space is less than already seen largest
3106 * window we can exit.
3108 if (*win_best - 1 > seq->iocfg->io_out1_delay_max
3116 * rw_mgr_mem_calibrate_writes_center() - Center all windows
3117 * @rank_bgn: Rank number
3118 * @write_group: Write group
3119 * @test_bgn: Rank at which the test begins
3121 * Center all windows. Do per-bit-deskew to possibly increase size of
3125 rw_mgr_mem_calibrate_writes_center(struct socfpga_sdrseq *seq,
3126 const u32 rank_bgn, const u32 write_group,
3132 int left_edge[seq->rwcfg->mem_dq_per_write_dqs];
3133 int right_edge[seq->rwcfg->mem_dq_per_write_dqs];
3135 int mid_min, orig_mid_min;
3136 int new_dqs, start_dqs;
3137 int dq_margin, dqs_margin, dm_margin;
3138 int bgn_curr = seq->iocfg->io_out1_delay_max + 1;
3139 int end_curr = seq->iocfg->io_out1_delay_max + 1;
3140 int bgn_best = seq->iocfg->io_out1_delay_max + 1;
3141 int end_best = seq->iocfg->io_out1_delay_max + 1;
3146 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
3150 start_dqs = readl((SDR_PHYGRP_SCCGRP_ADDRESS |
3151 SCC_MGR_IO_OUT1_DELAY_OFFSET) +
3152 (seq->rwcfg->mem_dq_per_write_dqs << 2));
3154 /* Per-bit deskew. */
3157 * Set the left and right edge of each bit to an illegal value.
3158 * Use (seq->iocfg->io_out1_delay_max + 1) as an illegal value.
3161 for (i = 0; i < seq->rwcfg->mem_dq_per_write_dqs; i++) {
3162 left_edge[i] = seq->iocfg->io_out1_delay_max + 1;
3163 right_edge[i] = seq->iocfg->io_out1_delay_max + 1;
3166 /* Search for the left edge of the window for each bit. */
3167 search_left_edge(seq, 1, rank_bgn, write_group, 0, test_bgn,
3169 left_edge, right_edge, 0);
3171 /* Search for the right edge of the window for each bit. */
3172 ret = search_right_edge(seq, 1, rank_bgn, write_group, 0,
3175 left_edge, right_edge, 0);
3177 set_failing_group_stage(seq, test_bgn + ret - 1,
3179 CAL_SUBSTAGE_WRITES_CENTER);
3183 min_index = get_window_mid_index(seq, 1, left_edge, right_edge,
3186 /* Determine the amount we can change DQS (which is -mid_min). */
3187 orig_mid_min = mid_min;
3188 new_dqs = start_dqs;
3190 debug_cond(DLEVEL >= 1,
3191 "%s:%d write_center: start_dqs=%d new_dqs=%d mid_min=%d\n",
3192 __func__, __LINE__, start_dqs, new_dqs, mid_min);
3194 /* Add delay to bring centre of all DQ windows to the same "level". */
3195 center_dq_windows(seq, 1, left_edge, right_edge, mid_min, orig_mid_min,
3196 min_index, 0, &dq_margin, &dqs_margin);
3199 scc_mgr_apply_group_dqs_io_and_oct_out1(seq, write_group, new_dqs);
3200 writel(0, &sdr_scc_mgr->update);
3203 debug_cond(DLEVEL >= 2, "%s:%d write_center: DM\n", __func__, __LINE__);
3205 /* Search for the/part of the window with DM shift. */
3206 search_window(seq, 1, rank_bgn, write_group, &bgn_curr, &end_curr,
3207 &bgn_best, &end_best, &win_best, 0);
3209 /* Reset DM delay chains to 0. */
3210 scc_mgr_apply_group_dm_out1_delay(seq, 0);
3213 * Check to see if the current window nudges up aganist 0 delay.
3214 * If so we need to continue the search by shifting DQS otherwise DQS
3215 * search begins as a new search.
3217 if (end_curr != 0) {
3218 bgn_curr = seq->iocfg->io_out1_delay_max + 1;
3219 end_curr = seq->iocfg->io_out1_delay_max + 1;
3222 /* Search for the/part of the window with DQS shifts. */
3223 search_window(seq, 0, rank_bgn, write_group, &bgn_curr, &end_curr,
3224 &bgn_best, &end_best, &win_best, new_dqs);
3226 /* Assign left and right edge for cal and reporting. */
3227 left_edge[0] = -1 * bgn_best;
3228 right_edge[0] = end_best;
3230 debug_cond(DLEVEL >= 2, "%s:%d dm_calib: left=%d right=%d\n",
3231 __func__, __LINE__, left_edge[0], right_edge[0]);
3233 /* Move DQS (back to orig). */
3234 scc_mgr_apply_group_dqs_io_and_oct_out1(seq, write_group, new_dqs);
3238 /* Find middle of window for the DM bit. */
3239 mid = (left_edge[0] - right_edge[0]) / 2;
3241 /* Only move right, since we are not moving DQS/DQ. */
3245 /* dm_marign should fail if we never find a window. */
3249 dm_margin = left_edge[0] - mid;
3251 scc_mgr_apply_group_dm_out1_delay(seq, mid);
3252 writel(0, &sdr_scc_mgr->update);
3254 debug_cond(DLEVEL >= 2,
3255 "%s:%d dm_calib: left=%d right=%d mid=%d dm_margin=%d\n",
3256 __func__, __LINE__, left_edge[0], right_edge[0],
3258 /* Export values. */
3259 seq->gbl.fom_out += dq_margin + dqs_margin;
3261 debug_cond(DLEVEL >= 2,
3262 "%s:%d write_center: dq_margin=%d dqs_margin=%d dm_margin=%d\n",
3263 __func__, __LINE__, dq_margin, dqs_margin, dm_margin);
3266 * Do not remove this line as it makes sure all of our
3267 * decisions have been applied.
3269 writel(0, &sdr_scc_mgr->update);
3271 if ((dq_margin < 0) || (dqs_margin < 0) || (dm_margin < 0))
3278 * rw_mgr_mem_calibrate_writes() - Write Calibration Part One
3279 * @rank_bgn: Rank number
3280 * @group: Read/Write Group
3281 * @test_bgn: Rank at which the test begins
3283 * Stage 2: Write Calibration Part One.
3285 * This function implements UniPHY calibration Stage 2, as explained in
3286 * detail in Altera EMI_RM 2015.05.04 , "UniPHY Calibration Stages".
3288 static int rw_mgr_mem_calibrate_writes(struct socfpga_sdrseq *seq,
3289 const u32 rank_bgn, const u32 group,
3294 /* Update info for sims */
3295 debug("%s:%d %u %u\n", __func__, __LINE__, group, test_bgn);
3297 reg_file_set_group(group);
3298 reg_file_set_stage(CAL_STAGE_WRITES);
3299 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3301 ret = rw_mgr_mem_calibrate_writes_center(seq, rank_bgn, group,
3304 set_failing_group_stage(seq, group, CAL_STAGE_WRITES,
3305 CAL_SUBSTAGE_WRITES_CENTER);
3311 * mem_precharge_and_activate() - Precharge all banks and activate
3313 * Precharge all banks and activate row 0 in bank "000..." and bank "111...".
3315 static void mem_precharge_and_activate(struct socfpga_sdrseq *seq)
3319 for (r = 0; r < seq->rwcfg->mem_number_of_ranks; r++) {
3321 set_rank_and_odt_mask(seq, r, RW_MGR_ODT_MODE_OFF);
3323 /* Precharge all banks. */
3324 writel(seq->rwcfg->precharge_all, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3325 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3327 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3328 writel(seq->rwcfg->activate_0_and_1_wait1,
3329 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
3331 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3332 writel(seq->rwcfg->activate_0_and_1_wait2,
3333 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
3335 /* Activate rows. */
3336 writel(seq->rwcfg->activate_0_and_1,
3337 SDR_PHYGRP_RWMGRGRP_ADDRESS |
3338 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3343 * mem_init_latency() - Configure memory RLAT and WLAT settings
3345 * Configure memory RLAT and WLAT parameters.
3347 static void mem_init_latency(struct socfpga_sdrseq *seq)
3350 * For AV/CV, LFIFO is hardened and always runs at full rate
3351 * so max latency in AFI clocks, used here, is correspondingly
3354 const u32 max_latency = (1 << seq->misccfg->max_latency_count_width)
3358 debug("%s:%d\n", __func__, __LINE__);
3361 * Read in write latency.
3362 * WL for Hard PHY does not include additive latency.
3364 wlat = readl(&data_mgr->t_wl_add);
3365 wlat += readl(&data_mgr->mem_t_add);
3367 seq->gbl.rw_wl_nop_cycles = wlat - 1;
3369 /* Read in readl latency. */
3370 rlat = readl(&data_mgr->t_rl_add);
3372 /* Set a pretty high read latency initially. */
3373 seq->gbl.curr_read_lat = rlat + 16;
3374 if (seq->gbl.curr_read_lat > max_latency)
3375 seq->gbl.curr_read_lat = max_latency;
3377 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
3379 /* Advertise write latency. */
3380 writel(wlat, &phy_mgr_cfg->afi_wlat);
3384 * @mem_skip_calibrate() - Set VFIFO and LFIFO to instant-on settings
3386 * Set VFIFO and LFIFO to instant-on settings in skip calibration mode.
3388 static void mem_skip_calibrate(struct socfpga_sdrseq *seq)
3393 debug("%s:%d\n", __func__, __LINE__);
3394 /* Need to update every shadow register set used by the interface */
3395 for (r = 0; r < seq->rwcfg->mem_number_of_ranks;
3396 r += NUM_RANKS_PER_SHADOW_REG) {
3398 * Set output phase alignment settings appropriate for
3401 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
3402 scc_mgr_set_dqs_en_phase(i, 0);
3403 if (seq->iocfg->dll_chain_length == 6)
3404 scc_mgr_set_dqdqs_output_phase(i, 6);
3406 scc_mgr_set_dqdqs_output_phase(i, 7);
3410 * Write data arrives to the I/O two cycles before write
3411 * latency is reached (720 deg).
3412 * -> due to bit-slip in a/c bus
3413 * -> to allow board skew where dqs is longer than ck
3414 * -> how often can this happen!?
3415 * -> can claim back some ptaps for high freq
3416 * support if we can relax this, but i digress...
3418 * The write_clk leads mem_ck by 90 deg
3419 * The minimum ptap of the OPA is 180 deg
3420 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3421 * The write_clk is always delayed by 2 ptaps
3423 * Hence, to make DQS aligned to CK, we need to delay
3425 * (720 - 90 - 180 - 2) *
3426 * (360 / seq->iocfg->dll_chain_length)
3428 * Dividing the above by
3429 (360 / seq->iocfg->dll_chain_length)
3430 * gives us the number of ptaps, which simplies to:
3432 * (1.25 * seq->iocfg->dll_chain_length - 2)
3434 scc_mgr_set_dqdqs_output_phase(i,
3435 ((125 * seq->iocfg->dll_chain_length)
3438 writel(0xff, &sdr_scc_mgr->dqs_ena);
3439 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
3441 for (i = 0; i < seq->rwcfg->mem_if_write_dqs_width; i++) {
3442 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3443 SCC_MGR_GROUP_COUNTER_OFFSET);
3445 writel(0xff, &sdr_scc_mgr->dq_ena);
3446 writel(0xff, &sdr_scc_mgr->dm_ena);
3447 writel(0, &sdr_scc_mgr->update);
3450 /* Compensate for simulation model behaviour */
3451 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
3452 scc_mgr_set_dqs_bus_in_delay(i, 10);
3453 scc_mgr_load_dqs(i);
3455 writel(0, &sdr_scc_mgr->update);
3458 * ArriaV has hard FIFOs that can only be initialized by incrementing
3461 vfifo_offset = seq->misccfg->calib_vfifo_offset;
3462 for (j = 0; j < vfifo_offset; j++)
3463 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
3464 writel(0, &phy_mgr_cmd->fifo_reset);
3467 * For Arria V and Cyclone V with hard LFIFO, we get the skip-cal
3468 * setting from generation-time constant.
3470 seq->gbl.curr_read_lat = seq->misccfg->calib_lfifo_offset;
3471 writel(seq->gbl.curr_read_lat, &phy_mgr_cfg->phy_rlat);
3475 * mem_calibrate() - Memory calibration entry point.
3477 * Perform memory calibration.
3479 static u32 mem_calibrate(struct socfpga_sdrseq *seq)
3483 u32 write_group, write_test_bgn;
3484 u32 read_group, read_test_bgn;
3485 u32 run_groups, current_run;
3486 u32 failing_groups = 0;
3487 u32 group_failed = 0;
3489 const u32 rwdqs_ratio = seq->rwcfg->mem_if_read_dqs_width /
3490 seq->rwcfg->mem_if_write_dqs_width;
3492 debug("%s:%d\n", __func__, __LINE__);
3494 /* Initialize the data settings */
3495 seq->gbl.error_substage = CAL_SUBSTAGE_NIL;
3496 seq->gbl.error_stage = CAL_STAGE_NIL;
3497 seq->gbl.error_group = 0xff;
3498 seq->gbl.fom_in = 0;
3499 seq->gbl.fom_out = 0;
3501 /* Initialize WLAT and RLAT. */
3502 mem_init_latency(seq);
3504 /* Initialize bit slips. */
3505 mem_precharge_and_activate(seq);
3507 for (i = 0; i < seq->rwcfg->mem_if_read_dqs_width; i++) {
3508 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3509 SCC_MGR_GROUP_COUNTER_OFFSET);
3510 /* Only needed once to set all groups, pins, DQ, DQS, DM. */
3512 scc_mgr_set_hhp_extras();
3514 scc_set_bypass_mode(i);
3517 /* Calibration is skipped. */
3518 if ((seq->dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3520 * Set VFIFO and LFIFO to instant-on settings in skip
3523 mem_skip_calibrate(seq);
3526 * Do not remove this line as it makes sure all of our
3527 * decisions have been applied.
3529 writel(0, &sdr_scc_mgr->update);
3533 /* Calibration is not skipped. */
3534 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3536 * Zero all delay chain/phase settings for all
3537 * groups and all shadow register sets.
3539 scc_mgr_zero_all(seq);
3543 for (write_group = 0, write_test_bgn = 0; write_group
3544 < seq->rwcfg->mem_if_write_dqs_width; write_group++,
3545 write_test_bgn += seq->rwcfg->mem_dq_per_write_dqs) {
3546 /* Initialize the group failure */
3549 current_run = run_groups & ((1 <<
3550 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3551 run_groups = run_groups >>
3552 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
3554 if (current_run == 0)
3557 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3558 SCC_MGR_GROUP_COUNTER_OFFSET);
3559 scc_mgr_zero_group(seq, write_group, 0);
3561 for (read_group = write_group * rwdqs_ratio,
3563 read_group < (write_group + 1) * rwdqs_ratio;
3565 read_test_bgn += seq->rwcfg->mem_dq_per_read_dqs) {
3566 if (STATIC_CALIB_STEPS & CALIB_SKIP_VFIFO)
3569 /* Calibrate the VFIFO */
3570 if (rw_mgr_mem_calibrate_vfifo(seq, read_group,
3574 if (!(seq->gbl.phy_debug_mode_flags &
3575 PHY_DEBUG_SWEEP_ALL_GROUPS))
3578 /* The group failed, we're done. */
3582 /* Calibrate the output side */
3583 for (rank_bgn = 0, sr = 0;
3584 rank_bgn < seq->rwcfg->mem_number_of_ranks;
3585 rank_bgn += NUM_RANKS_PER_SHADOW_REG, sr++) {
3586 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3589 /* Not needed in quick mode! */
3590 if (STATIC_CALIB_STEPS &
3591 CALIB_SKIP_DELAY_SWEEPS)
3594 /* Calibrate WRITEs */
3595 if (!rw_mgr_mem_calibrate_writes(seq, rank_bgn,
3601 if (!(seq->gbl.phy_debug_mode_flags &
3602 PHY_DEBUG_SWEEP_ALL_GROUPS))
3606 /* Some group failed, we're done. */
3610 for (read_group = write_group * rwdqs_ratio,
3612 read_group < (write_group + 1) * rwdqs_ratio;
3614 read_test_bgn += seq->rwcfg->mem_dq_per_read_dqs) {
3615 if (STATIC_CALIB_STEPS & CALIB_SKIP_WRITES)
3618 if (!rw_mgr_mem_calibrate_vfifo_end(seq,
3623 if (!(seq->gbl.phy_debug_mode_flags &
3624 PHY_DEBUG_SWEEP_ALL_GROUPS))
3627 /* The group failed, we're done. */
3631 /* No group failed, continue as usual. */
3634 grp_failed: /* A group failed, increment the counter. */
3639 * USER If there are any failing groups then report
3642 if (failing_groups != 0)
3645 if (STATIC_CALIB_STEPS & CALIB_SKIP_LFIFO)
3648 /* Calibrate the LFIFO */
3649 if (!rw_mgr_mem_calibrate_lfifo(seq))
3654 * Do not remove this line as it makes sure all of our decisions
3655 * have been applied.
3657 writel(0, &sdr_scc_mgr->update);
3662 * run_mem_calibrate() - Perform memory calibration
3664 * This function triggers the entire memory calibration procedure.
3666 static int run_mem_calibrate(struct socfpga_sdrseq *seq)
3671 debug("%s:%d\n", __func__, __LINE__);
3673 /* Reset pass/fail status shown on afi_cal_success/fail */
3674 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
3676 /* Stop tracking manager. */
3677 ctrl_cfg = readl(&sdr_ctrl->ctrl_cfg);
3678 writel(ctrl_cfg & ~SDR_CTRLGRP_CTRLCFG_DQSTRKEN_MASK,
3679 &sdr_ctrl->ctrl_cfg);
3681 phy_mgr_initialize(seq);
3682 rw_mgr_mem_initialize(seq);
3684 /* Perform the actual memory calibration. */
3685 pass = mem_calibrate(seq);
3687 mem_precharge_and_activate(seq);
3688 writel(0, &phy_mgr_cmd->fifo_reset);
3691 rw_mgr_mem_handoff(seq);
3693 * In Hard PHY this is a 2-bit control:
3695 * 1: DDIO Mux Select
3697 writel(0x2, &phy_mgr_cfg->mux_sel);
3699 /* Start tracking manager. */
3700 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
3706 * debug_mem_calibrate() - Report result of memory calibration
3707 * @pass: Value indicating whether calibration passed or failed
3709 * This function reports the results of the memory calibration
3710 * and writes debug information into the register file.
3712 static void debug_mem_calibrate(struct socfpga_sdrseq *seq, int pass)
3717 debug("%s: CALIBRATION PASSED\n", __FILE__);
3719 seq->gbl.fom_in /= 2;
3720 seq->gbl.fom_out /= 2;
3722 if (seq->gbl.fom_in > 0xff)
3723 seq->gbl.fom_in = 0xff;
3725 if (seq->gbl.fom_out > 0xff)
3726 seq->gbl.fom_out = 0xff;
3728 /* Update the FOM in the register file */
3729 debug_info = seq->gbl.fom_in;
3730 debug_info |= seq->gbl.fom_out << 8;
3731 writel(debug_info, &sdr_reg_file->fom);
3733 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3734 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
3736 debug("%s: CALIBRATION FAILED\n", __FILE__);
3738 debug_info = seq->gbl.error_stage;
3739 debug_info |= seq->gbl.error_substage << 8;
3740 debug_info |= seq->gbl.error_group << 16;
3742 writel(debug_info, &sdr_reg_file->failing_stage);
3743 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3744 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
3746 /* Update the failing group/stage in the register file */
3747 debug_info = seq->gbl.error_stage;
3748 debug_info |= seq->gbl.error_substage << 8;
3749 debug_info |= seq->gbl.error_group << 16;
3750 writel(debug_info, &sdr_reg_file->failing_stage);
3753 debug("%s: Calibration complete\n", __FILE__);
3757 * hc_initialize_rom_data() - Initialize ROM data
3759 * Initialize ROM data.
3761 static void hc_initialize_rom_data(void)
3763 unsigned int nelem = 0;
3764 const u32 *rom_init;
3767 socfpga_get_seq_inst_init(&rom_init, &nelem);
3768 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
3769 for (i = 0; i < nelem; i++)
3770 writel(rom_init[i], addr + (i << 2));
3772 socfpga_get_seq_ac_init(&rom_init, &nelem);
3773 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
3774 for (i = 0; i < nelem; i++)
3775 writel(rom_init[i], addr + (i << 2));
3779 * initialize_reg_file() - Initialize SDR register file
3781 * Initialize SDR register file.
3783 static void initialize_reg_file(struct socfpga_sdrseq *seq)
3785 /* Initialize the register file with the correct data */
3786 writel(seq->misccfg->reg_file_init_seq_signature,
3787 &sdr_reg_file->signature);
3788 writel(0, &sdr_reg_file->debug_data_addr);
3789 writel(0, &sdr_reg_file->cur_stage);
3790 writel(0, &sdr_reg_file->fom);
3791 writel(0, &sdr_reg_file->failing_stage);
3792 writel(0, &sdr_reg_file->debug1);
3793 writel(0, &sdr_reg_file->debug2);
3797 * initialize_hps_phy() - Initialize HPS PHY
3799 * Initialize HPS PHY.
3801 static void initialize_hps_phy(void)
3805 * Tracking also gets configured here because it's in the
3808 u32 trk_sample_count = 7500;
3809 u32 trk_long_idle_sample_count = (10 << 16) | 100;
3811 * Format is number of outer loops in the 16 MSB, sample
3816 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3817 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3818 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3819 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3820 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3821 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3823 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3824 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3826 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3827 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3829 writel(reg, &sdr_ctrl->phy_ctrl0);
3832 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3834 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3835 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3836 trk_long_idle_sample_count);
3837 writel(reg, &sdr_ctrl->phy_ctrl1);
3840 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3841 trk_long_idle_sample_count >>
3842 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
3843 writel(reg, &sdr_ctrl->phy_ctrl2);
3847 * initialize_tracking() - Initialize tracking
3849 * Initialize the register file with usable initial data.
3851 static void initialize_tracking(struct socfpga_sdrseq *seq)
3854 * Initialize the register file with the correct data.
3855 * Compute usable version of value in case we skip full
3856 * computation later.
3858 writel(DIV_ROUND_UP(seq->iocfg->delay_per_opa_tap,
3859 seq->iocfg->delay_per_dchain_tap) - 1,
3860 &sdr_reg_file->dtaps_per_ptap);
3862 /* trk_sample_count */
3863 writel(7500, &sdr_reg_file->trk_sample_count);
3865 /* longidle outer loop [15:0] */
3866 writel((10 << 16) | (100 << 0), &sdr_reg_file->trk_longidle);
3869 * longidle sample count [31:24]
3870 * trfc, worst case of 933Mhz 4Gb [23:16]
3871 * trcd, worst case [15:8]
3874 writel((243 << 24) | (14 << 16) | (10 << 8) | (4 << 0),
3875 &sdr_reg_file->delays);
3878 if (dram_is_ddr(2)) {
3879 writel(0, &sdr_reg_file->trk_rw_mgr_addr);
3880 } else if (dram_is_ddr(3)) {
3881 writel((seq->rwcfg->idle << 24) |
3882 (seq->rwcfg->activate_1 << 16) |
3883 (seq->rwcfg->sgle_read << 8) |
3884 (seq->rwcfg->precharge_all << 0),
3885 &sdr_reg_file->trk_rw_mgr_addr);
3888 writel(seq->rwcfg->mem_if_read_dqs_width,
3889 &sdr_reg_file->trk_read_dqs_width);
3892 if (dram_is_ddr(2)) {
3893 writel(1000 << 0, &sdr_reg_file->trk_rfsh);
3894 } else if (dram_is_ddr(3)) {
3895 writel((seq->rwcfg->refresh_all << 24) | (1000 << 0),
3896 &sdr_reg_file->trk_rfsh);
3900 int sdram_calibration_full(struct socfpga_sdr *sdr)
3903 struct socfpga_sdrseq seq;
3906 * For size reasons, this file uses hard coded addresses.
3907 * Check if we are called with the correct address.
3909 if (sdr != (struct socfpga_sdr *)SOCFPGA_SDR_ADDRESS)
3912 memset(&seq, 0, sizeof(seq));
3914 seq.rwcfg = socfpga_get_sdram_rwmgr_config();
3915 seq.iocfg = socfpga_get_sdram_io_config();
3916 seq.misccfg = socfpga_get_sdram_misc_config();
3918 /* Set the calibration enabled by default */
3919 seq.gbl.phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3921 * Only sweep all groups (regardless of fail state) by default
3922 * Set enabled read test by default.
3924 #if DISABLE_GUARANTEED_READ
3925 seq.gbl.phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3927 /* Initialize the register file */
3928 initialize_reg_file(&seq);
3930 /* Initialize any PHY CSR */
3931 initialize_hps_phy();
3933 scc_mgr_initialize();
3935 initialize_tracking(&seq);
3937 debug("%s: Preparing to start memory calibration\n", __FILE__);
3939 debug("%s:%d\n", __func__, __LINE__);
3940 debug_cond(DLEVEL >= 1,
3941 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3942 seq.rwcfg->mem_number_of_ranks,
3943 seq.rwcfg->mem_number_of_cs_per_dimm,
3944 seq.rwcfg->mem_dq_per_read_dqs,
3945 seq.rwcfg->mem_dq_per_write_dqs,
3946 seq.rwcfg->mem_virtual_groups_per_read_dqs,
3947 seq.rwcfg->mem_virtual_groups_per_write_dqs);
3948 debug_cond(DLEVEL >= 1,
3949 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3950 seq.rwcfg->mem_if_read_dqs_width,
3951 seq.rwcfg->mem_if_write_dqs_width,
3952 seq.rwcfg->mem_data_width, seq.rwcfg->mem_data_mask_width,
3953 seq.iocfg->delay_per_opa_tap,
3954 seq.iocfg->delay_per_dchain_tap);
3955 debug_cond(DLEVEL >= 1, "dtap_dqsen_delay=%u, dll=%u",
3956 seq.iocfg->delay_per_dqs_en_dchain_tap,
3957 seq.iocfg->dll_chain_length);
3958 debug_cond(DLEVEL >= 1,
3959 "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3960 seq.iocfg->dqs_en_phase_max, seq.iocfg->dqdqs_out_phase_max,
3961 seq.iocfg->dqs_en_delay_max, seq.iocfg->dqs_in_delay_max);
3962 debug_cond(DLEVEL >= 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3963 seq.iocfg->io_in_delay_max, seq.iocfg->io_out1_delay_max,
3964 seq.iocfg->io_out2_delay_max);
3965 debug_cond(DLEVEL >= 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3966 seq.iocfg->dqs_in_reserve, seq.iocfg->dqs_out_reserve);
3968 hc_initialize_rom_data();
3970 /* update info for sims */
3971 reg_file_set_stage(CAL_STAGE_NIL);
3972 reg_file_set_group(0);
3975 * Load global needed for those actions that require
3976 * some dynamic calibration support.
3978 seq.dyn_calib_steps = STATIC_CALIB_STEPS;
3980 * Load global to allow dynamic selection of delay loop settings
3981 * based on calibration mode.
3983 if (!(seq.dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3984 seq.skip_delay_mask = 0xff;
3986 seq.skip_delay_mask = 0x0;
3988 pass = run_mem_calibrate(&seq);
3989 debug_mem_calibrate(&seq, pass);