2 * Copyright Altera Corporation (C) 2012-2015
4 * SPDX-License-Identifier: BSD-3-Clause
9 #include <asm/arch/sdram.h>
10 #include "sequencer.h"
11 #include "sequencer_auto.h"
12 #include "sequencer_auto_ac_init.h"
13 #include "sequencer_auto_inst_init.h"
14 #include "sequencer_defines.h"
16 static void scc_mgr_load_dqs_for_write_group(uint32_t write_group);
18 static struct socfpga_sdr_rw_load_manager *sdr_rw_load_mgr_regs =
19 (struct socfpga_sdr_rw_load_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0x800);
21 static struct socfpga_sdr_rw_load_jump_manager *sdr_rw_load_jump_mgr_regs =
22 (struct socfpga_sdr_rw_load_jump_manager *)(SDR_PHYGRP_RWMGRGRP_ADDRESS | 0xC00);
24 static struct socfpga_sdr_reg_file *sdr_reg_file =
25 (struct socfpga_sdr_reg_file *)SDR_PHYGRP_REGFILEGRP_ADDRESS;
27 static struct socfpga_sdr_scc_mgr *sdr_scc_mgr =
28 (struct socfpga_sdr_scc_mgr *)(SDR_PHYGRP_SCCGRP_ADDRESS | 0xe00);
30 static struct socfpga_phy_mgr_cmd *phy_mgr_cmd =
31 (struct socfpga_phy_mgr_cmd *)SDR_PHYGRP_PHYMGRGRP_ADDRESS;
33 static struct socfpga_phy_mgr_cfg *phy_mgr_cfg =
34 (struct socfpga_phy_mgr_cfg *)(SDR_PHYGRP_PHYMGRGRP_ADDRESS | 0x40);
36 static struct socfpga_data_mgr *data_mgr =
37 (struct socfpga_data_mgr *)SDR_PHYGRP_DATAMGRGRP_ADDRESS;
39 static struct socfpga_sdr_ctrl *sdr_ctrl =
40 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
45 * In order to reduce ROM size, most of the selectable calibration steps are
46 * decided at compile time based on the user's calibration mode selection,
47 * as captured by the STATIC_CALIB_STEPS selection below.
49 * However, to support simulation-time selection of fast simulation mode, where
50 * we skip everything except the bare minimum, we need a few of the steps to
51 * be dynamic. In those cases, we either use the DYNAMIC_CALIB_STEPS for the
52 * check, which is based on the rtl-supplied value, or we dynamically compute
53 * the value to use based on the dynamically-chosen calibration mode
57 #define STATIC_IN_RTL_SIM 0
58 #define STATIC_SKIP_DELAY_LOOPS 0
60 #define STATIC_CALIB_STEPS (STATIC_IN_RTL_SIM | CALIB_SKIP_FULL_TEST | \
61 STATIC_SKIP_DELAY_LOOPS)
63 /* calibration steps requested by the rtl */
64 uint16_t dyn_calib_steps;
67 * To make CALIB_SKIP_DELAY_LOOPS a dynamic conditional option
68 * instead of static, we use boolean logic to select between
69 * non-skip and skip values
71 * The mask is set to include all bits when not-skipping, but is
75 uint16_t skip_delay_mask; /* mask off bits when skipping/not-skipping */
77 #define SKIP_DELAY_LOOP_VALUE_OR_ZERO(non_skip_value) \
78 ((non_skip_value) & skip_delay_mask)
81 struct param_type *param;
82 uint32_t curr_shadow_reg;
84 static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
85 uint32_t write_group, uint32_t use_dm,
86 uint32_t all_correct, uint32_t *bit_chk, uint32_t all_ranks);
88 static void set_failing_group_stage(uint32_t group, uint32_t stage,
92 * Only set the global stage if there was not been any other
95 if (gbl->error_stage == CAL_STAGE_NIL) {
96 gbl->error_substage = substage;
97 gbl->error_stage = stage;
98 gbl->error_group = group;
102 static void reg_file_set_group(u16 set_group)
104 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff0000, set_group << 16);
107 static void reg_file_set_stage(u8 set_stage)
109 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xffff, set_stage & 0xff);
112 static void reg_file_set_sub_stage(u8 set_sub_stage)
114 set_sub_stage &= 0xff;
115 clrsetbits_le32(&sdr_reg_file->cur_stage, 0xff00, set_sub_stage << 8);
118 static void initialize(void)
120 debug("%s:%d\n", __func__, __LINE__);
121 /* USER calibration has control over path to memory */
123 * In Hard PHY this is a 2-bit control:
127 writel(0x3, &phy_mgr_cfg->mux_sel);
129 /* USER memory clock is not stable we begin initialization */
130 writel(0, &phy_mgr_cfg->reset_mem_stbl);
132 /* USER calibration status all set to zero */
133 writel(0, &phy_mgr_cfg->cal_status);
135 writel(0, &phy_mgr_cfg->cal_debug_info);
137 if ((dyn_calib_steps & CALIB_SKIP_ALL) != CALIB_SKIP_ALL) {
138 param->read_correct_mask_vg = ((uint32_t)1 <<
139 (RW_MGR_MEM_DQ_PER_READ_DQS /
140 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
141 param->write_correct_mask_vg = ((uint32_t)1 <<
142 (RW_MGR_MEM_DQ_PER_READ_DQS /
143 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS)) - 1;
144 param->read_correct_mask = ((uint32_t)1 <<
145 RW_MGR_MEM_DQ_PER_READ_DQS) - 1;
146 param->write_correct_mask = ((uint32_t)1 <<
147 RW_MGR_MEM_DQ_PER_WRITE_DQS) - 1;
148 param->dm_correct_mask = ((uint32_t)1 <<
149 (RW_MGR_MEM_DATA_WIDTH / RW_MGR_MEM_DATA_MASK_WIDTH))
154 static void set_rank_and_odt_mask(uint32_t rank, uint32_t odt_mode)
156 uint32_t odt_mask_0 = 0;
157 uint32_t odt_mask_1 = 0;
158 uint32_t cs_and_odt_mask;
160 if (odt_mode == RW_MGR_ODT_MODE_READ_WRITE) {
161 if (RW_MGR_MEM_NUMBER_OF_RANKS == 1) {
169 } else if (RW_MGR_MEM_NUMBER_OF_RANKS == 2) {
171 if (RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM == 1) {
172 /* - Dual-Slot , Single-Rank
173 * (1 chip-select per DIMM)
175 * - RDIMM, 4 total CS (2 CS per DIMM)
177 * Since MEM_NUMBER_OF_RANKS is 2 they are
179 * with 2 CS each (special for RDIMM)
180 * Read: Turn on ODT on the opposite rank
181 * Write: Turn on ODT on all ranks
183 odt_mask_0 = 0x3 & ~(1 << rank);
187 * USER - Single-Slot , Dual-rank DIMMs
188 * (2 chip-selects per DIMM)
189 * USER Read: Turn on ODT off on all ranks
190 * USER Write: Turn on ODT on active rank
193 odt_mask_1 = 0x3 & (1 << rank);
198 * ----------+-----------------------+
201 * Read From +-----------------------+
202 * Rank | 3 | 2 | 1 | 0 |
203 * ----------+-----+-----+-----+-----+
204 * 0 | 0 | 1 | 0 | 0 |
205 * 1 | 1 | 0 | 0 | 0 |
206 * 2 | 0 | 0 | 0 | 1 |
207 * 3 | 0 | 0 | 1 | 0 |
208 * ----------+-----+-----+-----+-----+
211 * ----------+-----------------------+
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 * ----------+-----+-----+-----+-----+
248 (0xFF & ~(1 << rank)) |
249 ((0xFF & odt_mask_0) << 8) |
250 ((0xFF & odt_mask_1) << 16);
251 writel(cs_and_odt_mask, SDR_PHYGRP_RWMGRGRP_ADDRESS |
252 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
255 static void scc_mgr_initialize(void)
257 u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_HHP_RFILE_OFFSET;
260 * Clear register file for HPS
261 * 16 (2^4) is the size of the full register file in the scc mgr:
262 * RFILE_DEPTH = log2(MEM_DQ_PER_DQS + 1 + MEM_DM_PER_DQS +
263 * MEM_IF_READ_DQS_WIDTH - 1) + 1;
266 for (i = 0; i < 16; i++) {
267 debug_cond(DLEVEL == 1, "%s:%d: Clearing SCC RFILE index %u\n",
268 __func__, __LINE__, i);
269 writel(0, addr + (i << 2));
273 static void scc_mgr_set_dqdqs_output_phase(uint32_t write_group, uint32_t phase)
275 u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQDQS_OUT_PHASE_OFFSET;
277 /* Load the setting in the SCC manager */
278 writel(phase, addr + (write_group << 2));
281 static void scc_mgr_set_dqs_bus_in_delay(uint32_t read_group, uint32_t delay)
283 u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
285 /* Load the setting in the SCC manager */
286 writel(delay, addr + (read_group << 2));
289 static void scc_mgr_set_dqs_en_phase(uint32_t read_group, uint32_t phase)
291 u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_EN_PHASE_OFFSET;
293 /* Load the setting in the SCC manager */
294 writel(phase, addr + (read_group << 2));
297 static void scc_mgr_set_dqs_en_delay(uint32_t read_group, uint32_t delay)
299 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_EN_DELAY_OFFSET;
301 /* Load the setting in the SCC manager */
302 writel(delay + IO_DQS_EN_DELAY_OFFSET, addr + (read_group << 2));
305 static void scc_mgr_set_dqs_io_in_delay(uint32_t write_group, uint32_t delay)
307 u32 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
309 writel(delay, addr + (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
312 static void scc_mgr_set_dq_in_delay(uint32_t dq_in_group, uint32_t delay)
314 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
316 /* Load the setting in the SCC manager */
317 writel(delay, addr + (dq_in_group << 2));
320 static void scc_mgr_set_dq_out1_delay(uint32_t dq_in_group, uint32_t delay)
322 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
324 /* Load the setting in the SCC manager */
325 writel(delay, addr + (dq_in_group << 2));
328 static void scc_mgr_set_dqs_out1_delay(uint32_t write_group,
331 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
333 /* Load the setting in the SCC manager */
334 writel(delay, addr + (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
337 static void scc_mgr_set_dm_out1_delay(uint32_t dm, uint32_t delay)
339 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
341 /* Load the setting in the SCC manager */
342 writel(delay, addr + ((RW_MGR_MEM_DQ_PER_WRITE_DQS + 1 + dm) << 2));
345 /* load up dqs config settings */
346 static void scc_mgr_load_dqs(uint32_t dqs)
348 writel(dqs, &sdr_scc_mgr->dqs_ena);
351 /* load up dqs io config settings */
352 static void scc_mgr_load_dqs_io(void)
354 writel(0, &sdr_scc_mgr->dqs_io_ena);
357 /* load up dq config settings */
358 static void scc_mgr_load_dq(uint32_t dq_in_group)
360 writel(dq_in_group, &sdr_scc_mgr->dq_ena);
363 /* load up dm config settings */
364 static void scc_mgr_load_dm(uint32_t dm)
366 writel(dm, &sdr_scc_mgr->dm_ena);
369 static void scc_mgr_set_dqs_en_phase_all_ranks(uint32_t read_group,
373 uint32_t update_scan_chains;
375 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
376 r += NUM_RANKS_PER_SHADOW_REG) {
378 * USER although the h/w doesn't support different phases per
379 * shadow register, for simplicity our scc manager modeling
380 * keeps different phase settings per shadow reg, and it's
381 * important for us to keep them in sync to match h/w.
382 * for efficiency, the scan chain update should occur only
385 update_scan_chains = (r == 0) ? 1 : 0;
387 scc_mgr_set_dqs_en_phase(read_group, phase);
389 if (update_scan_chains) {
390 writel(read_group, &sdr_scc_mgr->dqs_ena);
391 writel(0, &sdr_scc_mgr->update);
396 static void scc_mgr_set_dqdqs_output_phase_all_ranks(uint32_t write_group,
400 uint32_t update_scan_chains;
402 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
403 r += NUM_RANKS_PER_SHADOW_REG) {
405 * USER although the h/w doesn't support different phases per
406 * shadow register, for simplicity our scc manager modeling
407 * keeps different phase settings per shadow reg, and it's
408 * important for us to keep them in sync to match h/w.
409 * for efficiency, the scan chain update should occur only
412 update_scan_chains = (r == 0) ? 1 : 0;
414 scc_mgr_set_dqdqs_output_phase(write_group, phase);
416 if (update_scan_chains) {
417 writel(write_group, &sdr_scc_mgr->dqs_ena);
418 writel(0, &sdr_scc_mgr->update);
423 static void scc_mgr_set_dqs_en_delay_all_ranks(uint32_t read_group,
428 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
429 r += NUM_RANKS_PER_SHADOW_REG) {
430 scc_mgr_set_dqs_en_delay(read_group, delay);
432 writel(read_group, &sdr_scc_mgr->dqs_ena);
434 * In shadow register mode, the T11 settings are stored in
435 * registers in the core, which are updated by the DQS_ENA
436 * signals. Not issuing the SCC_MGR_UPD command allows us to
437 * save lots of rank switching overhead, by calling
438 * select_shadow_regs_for_update with update_scan_chains
441 writel(0, &sdr_scc_mgr->update);
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 writel(0, &sdr_scc_mgr->update);
454 static void scc_mgr_set_oct_out1_delay(uint32_t write_group, uint32_t delay)
457 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_OCT_OUT1_DELAY_OFFSET;
460 * Load the setting in the SCC manager
461 * Although OCT affects only write data, the OCT delay is controlled
462 * by the DQS logic block which is instantiated once per read group.
463 * For protocols where a write group consists of multiple read groups,
464 * the setting must be set multiple times.
466 for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
467 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
468 read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
469 RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
470 writel(delay, addr + (read_group << 2));
473 static void scc_mgr_set_hhp_extras(void)
476 * Load the fixed setting in the SCC manager
477 * bits: 0:0 = 1'b1 - dqs bypass
478 * bits: 1:1 = 1'b1 - dq bypass
479 * bits: 4:2 = 3'b001 - rfifo_mode
480 * bits: 6:5 = 2'b01 - rfifo clock_select
481 * bits: 7:7 = 1'b0 - separate gating from ungating setting
482 * bits: 8:8 = 1'b0 - separate OE from Output delay setting
484 uint32_t value = (0<<8) | (0<<7) | (1<<5) | (1<<2) | (1<<1) | (1<<0);
485 uint32_t addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_HHP_GLOBALS_OFFSET;
487 writel(value, addr + SCC_MGR_HHP_EXTRAS_OFFSET);
491 * USER Zero all DQS config
492 * TODO: maybe rename to scc_mgr_zero_dqs_config (or something)
494 static void scc_mgr_zero_all(void)
499 * USER Zero all DQS config settings, across all groups and all
502 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
503 NUM_RANKS_PER_SHADOW_REG) {
504 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
506 * The phases actually don't exist on a per-rank basis,
507 * but there's no harm updating them several times, so
508 * let's keep the code simple.
510 scc_mgr_set_dqs_bus_in_delay(i, IO_DQS_IN_RESERVE);
511 scc_mgr_set_dqs_en_phase(i, 0);
512 scc_mgr_set_dqs_en_delay(i, 0);
515 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
516 scc_mgr_set_dqdqs_output_phase(i, 0);
517 /* av/cv don't have out2 */
518 scc_mgr_set_oct_out1_delay(i, IO_DQS_OUT_RESERVE);
522 /* multicast to all DQS group enables */
523 writel(0xff, &sdr_scc_mgr->dqs_ena);
524 writel(0, &sdr_scc_mgr->update);
527 static void scc_set_bypass_mode(uint32_t write_group, uint32_t mode)
529 /* mode = 0 : Do NOT bypass - Half Rate Mode */
530 /* mode = 1 : Bypass - Full Rate Mode */
532 /* only need to set once for all groups, pins, dq, dqs, dm */
533 if (write_group == 0) {
534 debug_cond(DLEVEL == 1, "%s:%d Setting HHP Extras\n", __func__,
536 scc_mgr_set_hhp_extras();
537 debug_cond(DLEVEL == 1, "%s:%d Done Setting HHP Extras\n",
540 /* multicast to all DQ enables */
541 writel(0xff, &sdr_scc_mgr->dq_ena);
542 writel(0xff, &sdr_scc_mgr->dm_ena);
544 /* update current DQS IO enable */
545 writel(0, &sdr_scc_mgr->dqs_io_ena);
547 /* update the DQS logic */
548 writel(write_group, &sdr_scc_mgr->dqs_ena);
551 writel(0, &sdr_scc_mgr->update);
554 static void scc_mgr_load_dqs_for_write_group(uint32_t write_group)
557 uint32_t addr = (u32)&sdr_scc_mgr->dqs_ena;
559 * Although OCT affects only write data, the OCT delay is controlled
560 * by the DQS logic block which is instantiated once per read group.
561 * For protocols where a write group consists of multiple read groups,
562 * the setting must be scanned multiple times.
564 for (read_group = write_group * RW_MGR_MEM_IF_READ_DQS_WIDTH /
565 RW_MGR_MEM_IF_WRITE_DQS_WIDTH;
566 read_group < (write_group + 1) * RW_MGR_MEM_IF_READ_DQS_WIDTH /
567 RW_MGR_MEM_IF_WRITE_DQS_WIDTH; ++read_group)
568 writel(read_group, addr);
571 static void scc_mgr_zero_group(uint32_t write_group, uint32_t test_begin,
576 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r +=
577 NUM_RANKS_PER_SHADOW_REG) {
578 /* Zero all DQ config settings */
579 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
580 scc_mgr_set_dq_out1_delay(i, 0);
582 scc_mgr_set_dq_in_delay(i, 0);
585 /* multicast to all DQ enables */
586 writel(0xff, &sdr_scc_mgr->dq_ena);
588 /* Zero all DM config settings */
589 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
590 scc_mgr_set_dm_out1_delay(i, 0);
593 /* multicast to all DM enables */
594 writel(0xff, &sdr_scc_mgr->dm_ena);
596 /* zero all DQS io settings */
598 scc_mgr_set_dqs_io_in_delay(write_group, 0);
599 /* av/cv don't have out2 */
600 scc_mgr_set_dqs_out1_delay(write_group, IO_DQS_OUT_RESERVE);
601 scc_mgr_set_oct_out1_delay(write_group, IO_DQS_OUT_RESERVE);
602 scc_mgr_load_dqs_for_write_group(write_group);
604 /* multicast to all DQS IO enables (only 1) */
605 writel(0, &sdr_scc_mgr->dqs_io_ena);
607 /* hit update to zero everything */
608 writel(0, &sdr_scc_mgr->update);
613 * apply and load a particular input delay for the DQ pins in a group
614 * group_bgn is the index of the first dq pin (in the write group)
616 static void scc_mgr_apply_group_dq_in_delay(uint32_t write_group,
617 uint32_t group_bgn, uint32_t delay)
621 for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
622 scc_mgr_set_dq_in_delay(p, delay);
627 /* apply and load a particular output delay for the DQ pins in a group */
628 static void scc_mgr_apply_group_dq_out1_delay(uint32_t write_group,
634 for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
635 scc_mgr_set_dq_out1_delay(i, delay1);
640 /* apply and load a particular output delay for the DM pins in a group */
641 static void scc_mgr_apply_group_dm_out1_delay(uint32_t write_group,
646 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
647 scc_mgr_set_dm_out1_delay(i, delay1);
653 /* apply and load delay on both DQS and OCT out1 */
654 static void scc_mgr_apply_group_dqs_io_and_oct_out1(uint32_t write_group,
657 scc_mgr_set_dqs_out1_delay(write_group, delay);
658 scc_mgr_load_dqs_io();
660 scc_mgr_set_oct_out1_delay(write_group, delay);
661 scc_mgr_load_dqs_for_write_group(write_group);
664 /* apply a delay to the entire output side: DQ, DM, DQS, OCT */
665 static void scc_mgr_apply_group_all_out_delay_add(uint32_t write_group,
669 uint32_t i, p, new_delay;
672 for (i = 0, p = group_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
673 new_delay = READ_SCC_DQ_OUT2_DELAY;
676 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
677 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQ[%u,%u]:\
678 %u > %lu => %lu", __func__, __LINE__,
679 write_group, group_bgn, delay, i, p, new_delay,
680 (long unsigned int)IO_IO_OUT2_DELAY_MAX,
681 (long unsigned int)IO_IO_OUT2_DELAY_MAX);
682 new_delay = IO_IO_OUT2_DELAY_MAX;
689 for (i = 0; i < RW_MGR_NUM_DM_PER_WRITE_GROUP; i++) {
690 new_delay = READ_SCC_DM_IO_OUT2_DELAY;
693 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
694 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DM[%u]:\
695 %u > %lu => %lu\n", __func__, __LINE__,
696 write_group, group_bgn, delay, i, new_delay,
697 (long unsigned int)IO_IO_OUT2_DELAY_MAX,
698 (long unsigned int)IO_IO_OUT2_DELAY_MAX);
699 new_delay = IO_IO_OUT2_DELAY_MAX;
706 new_delay = READ_SCC_DQS_IO_OUT2_DELAY;
709 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
710 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
711 " adding %u to OUT1\n", __func__, __LINE__,
712 write_group, group_bgn, delay, new_delay,
713 IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
714 new_delay - IO_IO_OUT2_DELAY_MAX);
715 scc_mgr_set_dqs_out1_delay(write_group, new_delay -
716 IO_IO_OUT2_DELAY_MAX);
717 new_delay = IO_IO_OUT2_DELAY_MAX;
720 scc_mgr_load_dqs_io();
723 new_delay = READ_SCC_OCT_OUT2_DELAY;
726 if (new_delay > IO_IO_OUT2_DELAY_MAX) {
727 debug_cond(DLEVEL == 1, "%s:%d (%u, %u, %u) DQS: %u > %d => %d;"
728 " adding %u to OUT1\n", __func__, __LINE__,
729 write_group, group_bgn, delay, new_delay,
730 IO_IO_OUT2_DELAY_MAX, IO_IO_OUT2_DELAY_MAX,
731 new_delay - IO_IO_OUT2_DELAY_MAX);
732 scc_mgr_set_oct_out1_delay(write_group, new_delay -
733 IO_IO_OUT2_DELAY_MAX);
734 new_delay = IO_IO_OUT2_DELAY_MAX;
737 scc_mgr_load_dqs_for_write_group(write_group);
741 * USER apply a delay to the entire output side (DQ, DM, DQS, OCT)
744 static void scc_mgr_apply_group_all_out_delay_add_all_ranks(
745 uint32_t write_group, uint32_t group_bgn, uint32_t delay)
749 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
750 r += NUM_RANKS_PER_SHADOW_REG) {
751 scc_mgr_apply_group_all_out_delay_add(write_group,
753 writel(0, &sdr_scc_mgr->update);
757 /* optimization used to recover some slots in ddr3 inst_rom */
758 /* could be applied to other protocols if we wanted to */
759 static void set_jump_as_return(void)
762 * to save space, we replace return with jump to special shared
763 * RETURN instruction so we set the counter to large value so that
766 writel(0xff, &sdr_rw_load_mgr_regs->load_cntr0);
767 writel(RW_MGR_RETURN, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
771 * should always use constants as argument to ensure all computations are
772 * performed at compile time
774 static void delay_for_n_mem_clocks(const uint32_t clocks)
781 debug("%s:%d: clocks=%u ... start\n", __func__, __LINE__, clocks);
784 afi_clocks = (clocks + AFI_RATE_RATIO-1) / AFI_RATE_RATIO;
785 /* scale (rounding up) to get afi clocks */
788 * Note, we don't bother accounting for being off a little bit
789 * because of a few extra instructions in outer loops
790 * Note, the loops have a test at the end, and do the test before
791 * the decrement, and so always perform the loop
792 * 1 time more than the counter value
794 if (afi_clocks == 0) {
796 } else if (afi_clocks <= 0x100) {
797 inner = afi_clocks-1;
800 } else if (afi_clocks <= 0x10000) {
802 outer = (afi_clocks-1) >> 8;
807 c_loop = (afi_clocks-1) >> 16;
811 * rom instructions are structured as follows:
813 * IDLE_LOOP2: jnz cntr0, TARGET_A
814 * IDLE_LOOP1: jnz cntr1, TARGET_B
817 * so, when doing nested loops, TARGET_A is set to IDLE_LOOP2, and
818 * TARGET_B is set to IDLE_LOOP2 as well
820 * if we have no outer loop, though, then we can use IDLE_LOOP1 only,
821 * and set TARGET_B to IDLE_LOOP1 and we skip IDLE_LOOP2 entirely
823 * a little confusing, but it helps save precious space in the inst_rom
824 * and sequencer rom and keeps the delays more accurate and reduces
827 if (afi_clocks <= 0x100) {
828 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
829 &sdr_rw_load_mgr_regs->load_cntr1);
831 writel(RW_MGR_IDLE_LOOP1,
832 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
834 writel(RW_MGR_IDLE_LOOP1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
835 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
837 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(inner),
838 &sdr_rw_load_mgr_regs->load_cntr0);
840 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(outer),
841 &sdr_rw_load_mgr_regs->load_cntr1);
843 writel(RW_MGR_IDLE_LOOP2,
844 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
846 writel(RW_MGR_IDLE_LOOP2,
847 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
849 /* hack to get around compiler not being smart enough */
850 if (afi_clocks <= 0x10000) {
851 /* only need to run once */
852 writel(RW_MGR_IDLE_LOOP2, SDR_PHYGRP_RWMGRGRP_ADDRESS |
853 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
856 writel(RW_MGR_IDLE_LOOP2,
857 SDR_PHYGRP_RWMGRGRP_ADDRESS |
858 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
859 } while (c_loop-- != 0);
862 debug("%s:%d clocks=%u ... end\n", __func__, __LINE__, clocks);
865 static void rw_mgr_mem_initialize(void)
868 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
869 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
871 debug("%s:%d\n", __func__, __LINE__);
873 /* The reset / cke part of initialization is broadcasted to all ranks */
874 writel(RW_MGR_RANK_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
875 RW_MGR_SET_CS_AND_ODT_MASK_OFFSET);
878 * Here's how you load register for a loop
879 * Counters are located @ 0x800
880 * Jump address are located @ 0xC00
881 * For both, registers 0 to 3 are selected using bits 3 and 2, like
882 * in 0x800, 0x804, 0x808, 0x80C and 0xC00, 0xC04, 0xC08, 0xC0C
883 * I know this ain't pretty, but Avalon bus throws away the 2 least
887 /* start with memory RESET activated */
892 * 200us @ 266MHz (3.75 ns) ~ 54000 clock cycles
893 * If a and b are the number of iteration in 2 nested loops
894 * it takes the following number of cycles to complete the operation:
895 * number_of_cycles = ((2 + n) * a + 2) * b
896 * where n is the number of instruction in the inner loop
897 * One possible solution is n = 0 , a = 256 , b = 106 => a = FF,
902 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR0_VAL),
903 &sdr_rw_load_mgr_regs->load_cntr0);
904 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR1_VAL),
905 &sdr_rw_load_mgr_regs->load_cntr1);
906 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TINIT_CNTR2_VAL),
907 &sdr_rw_load_mgr_regs->load_cntr2);
909 /* Load jump address */
910 writel(RW_MGR_INIT_RESET_0_CKE_0,
911 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
912 writel(RW_MGR_INIT_RESET_0_CKE_0,
913 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
914 writel(RW_MGR_INIT_RESET_0_CKE_0,
915 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
917 /* Execute count instruction */
918 writel(RW_MGR_INIT_RESET_0_CKE_0, grpaddr);
920 /* indicate that memory is stable */
921 writel(1, &phy_mgr_cfg->reset_mem_stbl);
924 * transition the RESET to high
929 * 500us @ 266MHz (3.75 ns) ~ 134000 clock cycles
930 * If a and b are the number of iteration in 2 nested loops
931 * it takes the following number of cycles to complete the operation
932 * number_of_cycles = ((2 + n) * a + 2) * b
933 * where n is the number of instruction in the inner loop
934 * One possible solution is n = 2 , a = 131 , b = 256 => a = 83,
939 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR0_VAL),
940 &sdr_rw_load_mgr_regs->load_cntr0);
941 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR1_VAL),
942 &sdr_rw_load_mgr_regs->load_cntr1);
943 writel(SKIP_DELAY_LOOP_VALUE_OR_ZERO(SEQ_TRESET_CNTR2_VAL),
944 &sdr_rw_load_mgr_regs->load_cntr2);
946 /* Load jump address */
947 writel(RW_MGR_INIT_RESET_1_CKE_0,
948 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
949 writel(RW_MGR_INIT_RESET_1_CKE_0,
950 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
951 writel(RW_MGR_INIT_RESET_1_CKE_0,
952 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
954 writel(RW_MGR_INIT_RESET_1_CKE_0, grpaddr);
956 /* bring up clock enable */
958 /* tXRP < 250 ck cycles */
959 delay_for_n_mem_clocks(250);
961 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
962 if (param->skip_ranks[r]) {
963 /* request to skip the rank */
968 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
971 * USER Use Mirror-ed commands for odd ranks if address
974 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
975 set_jump_as_return();
976 writel(RW_MGR_MRS2_MIRR, grpaddr);
977 delay_for_n_mem_clocks(4);
978 set_jump_as_return();
979 writel(RW_MGR_MRS3_MIRR, grpaddr);
980 delay_for_n_mem_clocks(4);
981 set_jump_as_return();
982 writel(RW_MGR_MRS1_MIRR, grpaddr);
983 delay_for_n_mem_clocks(4);
984 set_jump_as_return();
985 writel(RW_MGR_MRS0_DLL_RESET_MIRR, grpaddr);
987 set_jump_as_return();
988 writel(RW_MGR_MRS2, grpaddr);
989 delay_for_n_mem_clocks(4);
990 set_jump_as_return();
991 writel(RW_MGR_MRS3, grpaddr);
992 delay_for_n_mem_clocks(4);
993 set_jump_as_return();
994 writel(RW_MGR_MRS1, grpaddr);
995 set_jump_as_return();
996 writel(RW_MGR_MRS0_DLL_RESET, grpaddr);
998 set_jump_as_return();
999 writel(RW_MGR_ZQCL, grpaddr);
1001 /* tZQinit = tDLLK = 512 ck cycles */
1002 delay_for_n_mem_clocks(512);
1007 * At the end of calibration we have to program the user settings in, and
1008 * USER hand off the memory to the user.
1010 static void rw_mgr_mem_handoff(void)
1013 uint32_t grpaddr = SDR_PHYGRP_RWMGRGRP_ADDRESS |
1014 RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1016 debug("%s:%d\n", __func__, __LINE__);
1017 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
1018 if (param->skip_ranks[r])
1019 /* request to skip the rank */
1022 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
1024 /* precharge all banks ... */
1025 writel(RW_MGR_PRECHARGE_ALL, grpaddr);
1027 /* load up MR settings specified by user */
1030 * Use Mirror-ed commands for odd ranks if address
1033 if ((RW_MGR_MEM_ADDRESS_MIRRORING >> r) & 0x1) {
1034 set_jump_as_return();
1035 writel(RW_MGR_MRS2_MIRR, grpaddr);
1036 delay_for_n_mem_clocks(4);
1037 set_jump_as_return();
1038 writel(RW_MGR_MRS3_MIRR, grpaddr);
1039 delay_for_n_mem_clocks(4);
1040 set_jump_as_return();
1041 writel(RW_MGR_MRS1_MIRR, grpaddr);
1042 delay_for_n_mem_clocks(4);
1043 set_jump_as_return();
1044 writel(RW_MGR_MRS0_USER_MIRR, grpaddr);
1046 set_jump_as_return();
1047 writel(RW_MGR_MRS2, grpaddr);
1048 delay_for_n_mem_clocks(4);
1049 set_jump_as_return();
1050 writel(RW_MGR_MRS3, grpaddr);
1051 delay_for_n_mem_clocks(4);
1052 set_jump_as_return();
1053 writel(RW_MGR_MRS1, grpaddr);
1054 delay_for_n_mem_clocks(4);
1055 set_jump_as_return();
1056 writel(RW_MGR_MRS0_USER, grpaddr);
1059 * USER need to wait tMOD (12CK or 15ns) time before issuing
1060 * other commands, but we will have plenty of NIOS cycles before
1061 * actual handoff so its okay.
1067 * performs a guaranteed read on the patterns we are going to use during a
1068 * read test to ensure memory works
1070 static uint32_t rw_mgr_mem_calibrate_read_test_patterns(uint32_t rank_bgn,
1071 uint32_t group, uint32_t num_tries, uint32_t *bit_chk,
1075 uint32_t correct_mask_vg;
1076 uint32_t tmp_bit_chk;
1077 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1078 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1080 uint32_t base_rw_mgr;
1082 *bit_chk = param->read_correct_mask;
1083 correct_mask_vg = param->read_correct_mask_vg;
1085 for (r = rank_bgn; r < rank_end; r++) {
1086 if (param->skip_ranks[r])
1087 /* request to skip the rank */
1091 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1093 /* Load up a constant bursts of read commands */
1094 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1095 writel(RW_MGR_GUARANTEED_READ,
1096 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1098 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1099 writel(RW_MGR_GUARANTEED_READ_CONT,
1100 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1103 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1104 /* reset the fifos to get pointers to known state */
1106 writel(0, &phy_mgr_cmd->fifo_reset);
1107 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1108 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1110 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1111 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1113 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1114 writel(RW_MGR_GUARANTEED_READ, addr +
1115 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1118 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1119 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & (~base_rw_mgr));
1124 *bit_chk &= tmp_bit_chk;
1127 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1128 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
1130 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1131 debug_cond(DLEVEL == 1, "%s:%d test_load_patterns(%u,ALL) => (%u == %u) =>\
1132 %lu\n", __func__, __LINE__, group, *bit_chk, param->read_correct_mask,
1133 (long unsigned int)(*bit_chk == param->read_correct_mask));
1134 return *bit_chk == param->read_correct_mask;
1137 static uint32_t rw_mgr_mem_calibrate_read_test_patterns_all_ranks
1138 (uint32_t group, uint32_t num_tries, uint32_t *bit_chk)
1140 return rw_mgr_mem_calibrate_read_test_patterns(0, group,
1141 num_tries, bit_chk, 1);
1144 /* load up the patterns we are going to use during a read test */
1145 static void rw_mgr_mem_calibrate_read_load_patterns(uint32_t rank_bgn,
1149 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1150 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1152 debug("%s:%d\n", __func__, __LINE__);
1153 for (r = rank_bgn; r < rank_end; r++) {
1154 if (param->skip_ranks[r])
1155 /* request to skip the rank */
1159 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1161 /* Load up a constant bursts */
1162 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr0);
1164 writel(RW_MGR_GUARANTEED_WRITE_WAIT0,
1165 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1167 writel(0x20, &sdr_rw_load_mgr_regs->load_cntr1);
1169 writel(RW_MGR_GUARANTEED_WRITE_WAIT1,
1170 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1172 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr2);
1174 writel(RW_MGR_GUARANTEED_WRITE_WAIT2,
1175 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1177 writel(0x04, &sdr_rw_load_mgr_regs->load_cntr3);
1179 writel(RW_MGR_GUARANTEED_WRITE_WAIT3,
1180 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1182 writel(RW_MGR_GUARANTEED_WRITE, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1183 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
1186 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1190 * try a read and see if it returns correct data back. has dummy reads
1191 * inserted into the mix used to align dqs enable. has more thorough checks
1192 * than the regular read test.
1194 static uint32_t rw_mgr_mem_calibrate_read_test(uint32_t rank_bgn, uint32_t group,
1195 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1196 uint32_t all_groups, uint32_t all_ranks)
1199 uint32_t correct_mask_vg;
1200 uint32_t tmp_bit_chk;
1201 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
1202 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
1204 uint32_t base_rw_mgr;
1206 *bit_chk = param->read_correct_mask;
1207 correct_mask_vg = param->read_correct_mask_vg;
1209 uint32_t quick_read_mode = (((STATIC_CALIB_STEPS) &
1210 CALIB_SKIP_DELAY_SWEEPS) && ENABLE_SUPER_QUICK_CALIBRATION);
1212 for (r = rank_bgn; r < rank_end; r++) {
1213 if (param->skip_ranks[r])
1214 /* request to skip the rank */
1218 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
1220 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr1);
1222 writel(RW_MGR_READ_B2B_WAIT1,
1223 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
1225 writel(0x10, &sdr_rw_load_mgr_regs->load_cntr2);
1226 writel(RW_MGR_READ_B2B_WAIT2,
1227 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
1229 if (quick_read_mode)
1230 writel(0x1, &sdr_rw_load_mgr_regs->load_cntr0);
1231 /* need at least two (1+1) reads to capture failures */
1232 else if (all_groups)
1233 writel(0x06, &sdr_rw_load_mgr_regs->load_cntr0);
1235 writel(0x32, &sdr_rw_load_mgr_regs->load_cntr0);
1237 writel(RW_MGR_READ_B2B,
1238 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
1240 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH *
1241 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS - 1,
1242 &sdr_rw_load_mgr_regs->load_cntr3);
1244 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr3);
1246 writel(RW_MGR_READ_B2B,
1247 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
1250 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS-1; ; vg--) {
1251 /* reset the fifos to get pointers to known state */
1252 writel(0, &phy_mgr_cmd->fifo_reset);
1253 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
1254 RW_MGR_RESET_READ_DATAPATH_OFFSET);
1256 tmp_bit_chk = tmp_bit_chk << (RW_MGR_MEM_DQ_PER_READ_DQS
1257 / RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS);
1260 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_ALL_GROUPS_OFFSET;
1262 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1264 writel(RW_MGR_READ_B2B, addr +
1265 ((group * RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS +
1268 base_rw_mgr = readl(SDR_PHYGRP_RWMGRGRP_ADDRESS);
1269 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
1274 *bit_chk &= tmp_bit_chk;
1277 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
1278 writel(RW_MGR_CLEAR_DQS_ENABLE, addr + (group << 2));
1281 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1282 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ALL,%u) =>\
1283 (%u == %u) => %lu", __func__, __LINE__, group,
1284 all_groups, *bit_chk, param->read_correct_mask,
1285 (long unsigned int)(*bit_chk ==
1286 param->read_correct_mask));
1287 return *bit_chk == param->read_correct_mask;
1289 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
1290 debug_cond(DLEVEL == 2, "%s:%d read_test(%u,ONE,%u) =>\
1291 (%u != %lu) => %lu\n", __func__, __LINE__,
1292 group, all_groups, *bit_chk, (long unsigned int)0,
1293 (long unsigned int)(*bit_chk != 0x00));
1294 return *bit_chk != 0x00;
1298 static uint32_t rw_mgr_mem_calibrate_read_test_all_ranks(uint32_t group,
1299 uint32_t num_tries, uint32_t all_correct, uint32_t *bit_chk,
1300 uint32_t all_groups)
1302 return rw_mgr_mem_calibrate_read_test(0, group, num_tries, all_correct,
1303 bit_chk, all_groups, 1);
1306 static void rw_mgr_incr_vfifo(uint32_t grp, uint32_t *v)
1308 writel(grp, &phy_mgr_cmd->inc_vfifo_hard_phy);
1312 static void rw_mgr_decr_vfifo(uint32_t grp, uint32_t *v)
1316 for (i = 0; i < VFIFO_SIZE-1; i++)
1317 rw_mgr_incr_vfifo(grp, v);
1320 static int find_vfifo_read(uint32_t grp, uint32_t *bit_chk)
1323 uint32_t fail_cnt = 0;
1324 uint32_t test_status;
1326 for (v = 0; v < VFIFO_SIZE; ) {
1327 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo %u\n",
1328 __func__, __LINE__, v);
1329 test_status = rw_mgr_mem_calibrate_read_test_all_ranks
1330 (grp, 1, PASS_ONE_BIT, bit_chk, 0);
1338 /* fiddle with FIFO */
1339 rw_mgr_incr_vfifo(grp, &v);
1342 if (v >= VFIFO_SIZE) {
1343 /* no failing read found!! Something must have gone wrong */
1344 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: vfifo failed\n",
1345 __func__, __LINE__);
1352 static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
1353 uint32_t dtaps_per_ptap, uint32_t *work_bgn,
1354 uint32_t *v, uint32_t *d, uint32_t *p,
1355 uint32_t *i, uint32_t *max_working_cnt)
1357 uint32_t found_begin = 0;
1358 uint32_t tmp_delay = 0;
1359 uint32_t test_status;
1361 for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
1362 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1363 *work_bgn = tmp_delay;
1364 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1366 for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
1367 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
1368 IO_DELAY_PER_OPA_TAP) {
1369 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1372 rw_mgr_mem_calibrate_read_test_all_ranks
1373 (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
1376 *max_working_cnt = 1;
1385 if (*p > IO_DQS_EN_PHASE_MAX)
1386 /* fiddle with FIFO */
1387 rw_mgr_incr_vfifo(*grp, v);
1394 if (*i >= VFIFO_SIZE) {
1395 /* cannot find working solution */
1396 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
1397 ptap/dtap\n", __func__, __LINE__);
1404 static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
1405 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1406 uint32_t *p, uint32_t *max_working_cnt)
1408 uint32_t found_begin = 0;
1411 /* Special case code for backing up a phase */
1413 *p = IO_DQS_EN_PHASE_MAX;
1414 rw_mgr_decr_vfifo(*grp, v);
1418 tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
1419 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1421 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
1422 (*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1423 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1425 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1429 *work_bgn = tmp_delay;
1434 /* We have found a working dtap before the ptap found above */
1435 if (found_begin == 1)
1436 (*max_working_cnt)++;
1439 * Restore VFIFO to old state before we decremented it
1443 if (*p > IO_DQS_EN_PHASE_MAX) {
1445 rw_mgr_incr_vfifo(*grp, v);
1448 scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
1451 static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
1452 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1453 uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
1456 uint32_t found_end = 0;
1459 *work_end += IO_DELAY_PER_OPA_TAP;
1460 if (*p > IO_DQS_EN_PHASE_MAX) {
1461 /* fiddle with FIFO */
1463 rw_mgr_incr_vfifo(*grp, v);
1466 for (; *i < VFIFO_SIZE + 1; (*i)++) {
1467 for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
1468 += IO_DELAY_PER_OPA_TAP) {
1469 scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
1471 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1472 (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
1476 (*max_working_cnt)++;
1483 if (*p > IO_DQS_EN_PHASE_MAX) {
1484 /* fiddle with FIFO */
1485 rw_mgr_incr_vfifo(*grp, v);
1490 if (*i >= VFIFO_SIZE + 1) {
1491 /* cannot see edge of failing read */
1492 debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
1493 failed\n", __func__, __LINE__);
1500 static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
1501 uint32_t *work_bgn, uint32_t *v, uint32_t *d,
1502 uint32_t *p, uint32_t *work_mid,
1508 *work_mid = (*work_bgn + *work_end) / 2;
1510 debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
1511 *work_bgn, *work_end, *work_mid);
1512 /* Get the middle delay to be less than a VFIFO delay */
1513 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX;
1514 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1516 debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
1517 while (*work_mid > tmp_delay)
1518 *work_mid -= tmp_delay;
1519 debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
1522 for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX && tmp_delay < *work_mid;
1523 (*p)++, tmp_delay += IO_DELAY_PER_OPA_TAP)
1525 tmp_delay -= IO_DELAY_PER_OPA_TAP;
1526 debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", (*p) - 1, tmp_delay);
1527 for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_mid; (*d)++,
1528 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
1530 debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", *d, tmp_delay);
1532 scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
1533 scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
1536 * push vfifo until we can successfully calibrate. We can do this
1537 * because the largest possible margin in 1 VFIFO cycle.
1539 for (i = 0; i < VFIFO_SIZE; i++) {
1540 debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
1542 if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
1548 /* fiddle with FIFO */
1549 rw_mgr_incr_vfifo(*grp, v);
1552 if (i >= VFIFO_SIZE) {
1553 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
1554 failed\n", __func__, __LINE__);
1561 /* find a good dqs enable to use */
1562 static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
1564 uint32_t v, d, p, i;
1565 uint32_t max_working_cnt;
1567 uint32_t dtaps_per_ptap;
1568 uint32_t work_bgn, work_mid, work_end;
1569 uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
1571 debug("%s:%d %u\n", __func__, __LINE__, grp);
1573 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
1575 scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
1576 scc_mgr_set_dqs_en_phase_all_ranks(grp, 0);
1578 /* ************************************************************** */
1579 /* * Step 0 : Determine number of delay taps for each phase tap * */
1580 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP/IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1582 /* ********************************************************* */
1583 /* * Step 1 : First push vfifo until we get a failing read * */
1584 v = find_vfifo_read(grp, &bit_chk);
1586 max_working_cnt = 0;
1588 /* ******************************************************** */
1589 /* * step 2: find first working phase, increment in ptaps * */
1591 if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
1592 &p, &i, &max_working_cnt) == 0)
1595 work_end = work_bgn;
1598 * If d is 0 then the working window covers a phase tap and
1599 * we can follow the old procedure otherwise, we've found the beginning,
1600 * and we need to increment the dtaps until we find the end.
1603 /* ********************************************************* */
1604 /* * step 3a: if we have room, back off by one and
1605 increment in dtaps * */
1607 sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1610 /* ********************************************************* */
1611 /* * step 4a: go forward from working phase to non working
1612 phase, increment in ptaps * */
1613 if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1614 &i, &max_working_cnt, &work_end) == 0)
1617 /* ********************************************************* */
1618 /* * step 5a: back off one from last, increment in dtaps * */
1620 /* Special case code for backing up a phase */
1622 p = IO_DQS_EN_PHASE_MAX;
1623 rw_mgr_decr_vfifo(grp, &v);
1628 work_end -= IO_DELAY_PER_OPA_TAP;
1629 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1631 /* * The actual increment of dtaps is done outside of
1632 the if/else loop to share code */
1635 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p: \
1636 vfifo=%u ptap=%u\n", __func__, __LINE__,
1639 /* ******************************************************* */
1640 /* * step 3-5b: Find the right edge of the window using
1642 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase:vfifo=%u \
1643 ptap=%u dtap=%u bgn=%u\n", __func__, __LINE__,
1646 work_end = work_bgn;
1648 /* * The actual increment of dtaps is done outside of the
1649 if/else loop to share code */
1651 /* Only here to counterbalance a subtract later on which is
1652 not needed if this branch of the algorithm is taken */
1656 /* The dtap increment to find the failing edge is done here */
1657 for (; d <= IO_DQS_EN_DELAY_MAX; d++, work_end +=
1658 IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
1659 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1660 end-2: dtap=%u\n", __func__, __LINE__, d);
1661 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1663 if (!rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1670 /* Go back to working dtap */
1672 work_end -= IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
1674 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: v/p/d: vfifo=%u \
1675 ptap=%u dtap=%u end=%u\n", __func__, __LINE__,
1676 v, p, d-1, work_end);
1678 if (work_end < work_bgn) {
1680 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: end-2: \
1681 failed\n", __func__, __LINE__);
1685 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: found range [%u,%u]\n",
1686 __func__, __LINE__, work_bgn, work_end);
1688 /* *************************************************************** */
1690 * * We need to calculate the number of dtaps that equal a ptap
1691 * * To do that we'll back up a ptap and re-find the edge of the
1692 * * window using dtaps
1695 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: calculate dtaps_per_ptap \
1696 for tracking\n", __func__, __LINE__);
1698 /* Special case code for backing up a phase */
1700 p = IO_DQS_EN_PHASE_MAX;
1701 rw_mgr_decr_vfifo(grp, &v);
1702 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1703 cycle/phase: v=%u p=%u\n", __func__, __LINE__,
1707 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: backedup \
1708 phase only: v=%u p=%u", __func__, __LINE__,
1712 scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
1715 * Increase dtap until we first see a passing read (in case the
1716 * window is smaller than a ptap),
1717 * and then a failing read to mark the edge of the window again
1720 /* Find a passing read */
1721 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find passing read\n",
1722 __func__, __LINE__);
1723 found_passing_read = 0;
1724 found_failing_read = 0;
1725 initial_failing_dtap = d;
1726 for (; d <= IO_DQS_EN_DELAY_MAX; d++) {
1727 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: testing \
1728 read d=%u\n", __func__, __LINE__, d);
1729 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1731 if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
1734 found_passing_read = 1;
1739 if (found_passing_read) {
1740 /* Find a failing read */
1741 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: find failing \
1742 read\n", __func__, __LINE__);
1743 for (d = d + 1; d <= IO_DQS_EN_DELAY_MAX; d++) {
1744 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: \
1745 testing read d=%u\n", __func__, __LINE__, d);
1746 scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
1748 if (!rw_mgr_mem_calibrate_read_test_all_ranks
1749 (grp, 1, PASS_ONE_BIT, &bit_chk, 0)) {
1750 found_failing_read = 1;
1755 debug_cond(DLEVEL == 1, "%s:%d find_dqs_en_phase: failed to \
1756 calculate dtaps", __func__, __LINE__);
1757 debug_cond(DLEVEL == 1, "per ptap. Fall back on static value\n");
1761 * The dynamically calculated dtaps_per_ptap is only valid if we
1762 * found a passing/failing read. If we didn't, it means d hit the max
1763 * (IO_DQS_EN_DELAY_MAX). Otherwise, dtaps_per_ptap retains its
1764 * statically calculated value.
1766 if (found_passing_read && found_failing_read)
1767 dtaps_per_ptap = d - initial_failing_dtap;
1769 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
1770 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: dtaps_per_ptap=%u \
1771 - %u = %u", __func__, __LINE__, d,
1772 initial_failing_dtap, dtaps_per_ptap);
1774 /* ******************************************** */
1775 /* * step 6: Find the centre of the window * */
1776 if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
1777 &work_mid, &work_end) == 0)
1780 debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
1781 vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
1787 * Try rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase across different
1788 * dq_in_delay values
1791 rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
1792 (uint32_t write_group, uint32_t read_group, uint32_t test_bgn)
1800 const uint32_t delay_step = IO_IO_IN_DELAY_MAX /
1801 (RW_MGR_MEM_DQ_PER_READ_DQS-1);
1802 /* we start at zero, so have one less dq to devide among */
1804 debug("%s:%d (%u,%u,%u)", __func__, __LINE__, write_group, read_group,
1807 /* try different dq_in_delays since the dq path is shorter than dqs */
1809 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1810 r += NUM_RANKS_PER_SHADOW_REG) {
1811 for (i = 0, p = test_bgn, d = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1812 i++, p++, d += delay_step) {
1813 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_\
1814 vfifo_find_dqs_", __func__, __LINE__);
1815 debug_cond(DLEVEL == 1, "en_phase_sweep_dq_in_delay: g=%u/%u ",
1816 write_group, read_group);
1817 debug_cond(DLEVEL == 1, "r=%u, i=%u p=%u d=%u\n", r, i , p, d);
1818 scc_mgr_set_dq_in_delay(p, d);
1821 writel(0, &sdr_scc_mgr->update);
1824 found = rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(read_group);
1826 debug_cond(DLEVEL == 1, "%s:%d rw_mgr_mem_calibrate_vfifo_find_dqs_\
1827 en_phase_sweep_dq", __func__, __LINE__);
1828 debug_cond(DLEVEL == 1, "_in_delay: g=%u/%u found=%u; Reseting delay \
1829 chain to zero\n", write_group, read_group, found);
1831 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
1832 r += NUM_RANKS_PER_SHADOW_REG) {
1833 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS;
1835 scc_mgr_set_dq_in_delay(p, 0);
1838 writel(0, &sdr_scc_mgr->update);
1844 /* per-bit deskew DQ and center */
1845 static uint32_t rw_mgr_mem_calibrate_vfifo_center(uint32_t rank_bgn,
1846 uint32_t write_group, uint32_t read_group, uint32_t test_bgn,
1847 uint32_t use_read_test, uint32_t update_fom)
1849 uint32_t i, p, d, min_index;
1851 * Store these as signed since there are comparisons with
1855 uint32_t sticky_bit_chk;
1856 int32_t left_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1857 int32_t right_edge[RW_MGR_MEM_DQ_PER_READ_DQS];
1858 int32_t final_dq[RW_MGR_MEM_DQ_PER_READ_DQS];
1860 int32_t orig_mid_min, mid_min;
1861 int32_t new_dqs, start_dqs, start_dqs_en, shift_dq, final_dqs,
1863 int32_t dq_margin, dqs_margin;
1865 uint32_t temp_dq_in_delay1, temp_dq_in_delay2;
1868 debug("%s:%d: %u %u", __func__, __LINE__, read_group, test_bgn);
1870 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_DQS_IN_DELAY_OFFSET;
1871 start_dqs = readl(addr + (read_group << 2));
1872 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
1873 start_dqs_en = readl(addr + ((read_group << 2)
1874 - IO_DQS_EN_DELAY_OFFSET));
1876 /* set the left and right edge of each bit to an illegal value */
1877 /* use (IO_IO_IN_DELAY_MAX + 1) as an illegal value */
1879 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1880 left_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1881 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1884 /* Search for the left edge of the window for each bit */
1885 for (d = 0; d <= IO_IO_IN_DELAY_MAX; d++) {
1886 scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, d);
1888 writel(0, &sdr_scc_mgr->update);
1891 * Stop searching when the read test doesn't pass AND when
1892 * we've seen a passing read on every bit.
1894 if (use_read_test) {
1895 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1896 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1899 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1902 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1903 (read_group - (write_group *
1904 RW_MGR_MEM_IF_READ_DQS_WIDTH /
1905 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
1906 stop = (bit_chk == 0);
1908 sticky_bit_chk = sticky_bit_chk | bit_chk;
1909 stop = stop && (sticky_bit_chk == param->read_correct_mask);
1910 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(left): dtap=%u => %u == %u \
1911 && %u", __func__, __LINE__, d,
1913 param->read_correct_mask, stop);
1918 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
1920 /* Remember a passing test as the
1924 /* If a left edge has not been seen yet,
1925 then a future passing test will mark
1926 this edge as the right edge */
1928 IO_IO_IN_DELAY_MAX + 1) {
1929 right_edge[i] = -(d + 1);
1932 bit_chk = bit_chk >> 1;
1937 /* Reset DQ delay chains to 0 */
1938 scc_mgr_apply_group_dq_in_delay(write_group, test_bgn, 0);
1940 for (i = RW_MGR_MEM_DQ_PER_READ_DQS - 1;; i--) {
1941 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
1942 %d right_edge[%u]: %d\n", __func__, __LINE__,
1943 i, left_edge[i], i, right_edge[i]);
1946 * Check for cases where we haven't found the left edge,
1947 * which makes our assignment of the the right edge invalid.
1948 * Reset it to the illegal value.
1950 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) && (
1951 right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1952 right_edge[i] = IO_IO_IN_DELAY_MAX + 1;
1953 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: reset \
1954 right_edge[%u]: %d\n", __func__, __LINE__,
1959 * Reset sticky bit (except for bits where we have seen
1960 * both the left and right edge).
1962 sticky_bit_chk = sticky_bit_chk << 1;
1963 if ((left_edge[i] != IO_IO_IN_DELAY_MAX + 1) &&
1964 (right_edge[i] != IO_IO_IN_DELAY_MAX + 1)) {
1965 sticky_bit_chk = sticky_bit_chk | 1;
1972 /* Search for the right edge of the window for each bit */
1973 for (d = 0; d <= IO_DQS_IN_DELAY_MAX - start_dqs; d++) {
1974 scc_mgr_set_dqs_bus_in_delay(read_group, d + start_dqs);
1975 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
1976 uint32_t delay = d + start_dqs_en;
1977 if (delay > IO_DQS_EN_DELAY_MAX)
1978 delay = IO_DQS_EN_DELAY_MAX;
1979 scc_mgr_set_dqs_en_delay(read_group, delay);
1981 scc_mgr_load_dqs(read_group);
1983 writel(0, &sdr_scc_mgr->update);
1986 * Stop searching when the read test doesn't pass AND when
1987 * we've seen a passing read on every bit.
1989 if (use_read_test) {
1990 stop = !rw_mgr_mem_calibrate_read_test(rank_bgn,
1991 read_group, NUM_READ_PB_TESTS, PASS_ONE_BIT,
1994 rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
1997 bit_chk = bit_chk >> (RW_MGR_MEM_DQ_PER_READ_DQS *
1998 (read_group - (write_group *
1999 RW_MGR_MEM_IF_READ_DQS_WIDTH /
2000 RW_MGR_MEM_IF_WRITE_DQS_WIDTH)));
2001 stop = (bit_chk == 0);
2003 sticky_bit_chk = sticky_bit_chk | bit_chk;
2004 stop = stop && (sticky_bit_chk == param->read_correct_mask);
2006 debug_cond(DLEVEL == 2, "%s:%d vfifo_center(right): dtap=%u => %u == \
2007 %u && %u", __func__, __LINE__, d,
2008 sticky_bit_chk, param->read_correct_mask, stop);
2013 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2015 /* Remember a passing test as
2020 /* If a right edge has not been
2021 seen yet, then a future passing
2022 test will mark this edge as the
2024 if (right_edge[i] ==
2025 IO_IO_IN_DELAY_MAX + 1) {
2026 left_edge[i] = -(d + 1);
2029 /* d = 0 failed, but it passed
2030 when testing the left edge,
2031 so it must be marginal,
2033 if (right_edge[i] ==
2034 IO_IO_IN_DELAY_MAX + 1 &&
2040 /* If a right edge has not been
2041 seen yet, then a future passing
2042 test will mark this edge as the
2044 else if (right_edge[i] ==
2045 IO_IO_IN_DELAY_MAX +
2047 left_edge[i] = -(d + 1);
2052 debug_cond(DLEVEL == 2, "%s:%d vfifo_center[r,\
2053 d=%u]: ", __func__, __LINE__, d);
2054 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d ",
2055 (int)(bit_chk & 1), i, left_edge[i]);
2056 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2058 bit_chk = bit_chk >> 1;
2063 /* Check that all bits have a window */
2064 for (i = 0; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2065 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: left_edge[%u]: \
2066 %d right_edge[%u]: %d", __func__, __LINE__,
2067 i, left_edge[i], i, right_edge[i]);
2068 if ((left_edge[i] == IO_IO_IN_DELAY_MAX + 1) || (right_edge[i]
2069 == IO_IO_IN_DELAY_MAX + 1)) {
2071 * Restore delay chain settings before letting the loop
2072 * in rw_mgr_mem_calibrate_vfifo to retry different
2073 * dqs/ck relationships.
2075 scc_mgr_set_dqs_bus_in_delay(read_group, start_dqs);
2076 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2077 scc_mgr_set_dqs_en_delay(read_group,
2080 scc_mgr_load_dqs(read_group);
2081 writel(0, &sdr_scc_mgr->update);
2083 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: failed to \
2084 find edge [%u]: %d %d", __func__, __LINE__,
2085 i, left_edge[i], right_edge[i]);
2086 if (use_read_test) {
2087 set_failing_group_stage(read_group *
2088 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2090 CAL_SUBSTAGE_VFIFO_CENTER);
2092 set_failing_group_stage(read_group *
2093 RW_MGR_MEM_DQ_PER_READ_DQS + i,
2094 CAL_STAGE_VFIFO_AFTER_WRITES,
2095 CAL_SUBSTAGE_VFIFO_CENTER);
2101 /* Find middle of window for each DQ bit */
2102 mid_min = left_edge[0] - right_edge[0];
2104 for (i = 1; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++) {
2105 mid = left_edge[i] - right_edge[i];
2106 if (mid < mid_min) {
2113 * -mid_min/2 represents the amount that we need to move DQS.
2114 * If mid_min is odd and positive we'll need to add one to
2115 * make sure the rounding in further calculations is correct
2116 * (always bias to the right), so just add 1 for all positive values.
2121 mid_min = mid_min / 2;
2123 debug_cond(DLEVEL == 1, "%s:%d vfifo_center: mid_min=%d (index=%u)\n",
2124 __func__, __LINE__, mid_min, min_index);
2126 /* Determine the amount we can change DQS (which is -mid_min) */
2127 orig_mid_min = mid_min;
2128 new_dqs = start_dqs - mid_min;
2129 if (new_dqs > IO_DQS_IN_DELAY_MAX)
2130 new_dqs = IO_DQS_IN_DELAY_MAX;
2131 else if (new_dqs < 0)
2134 mid_min = start_dqs - new_dqs;
2135 debug_cond(DLEVEL == 1, "vfifo_center: new mid_min=%d new_dqs=%d\n",
2138 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2139 if (start_dqs_en - mid_min > IO_DQS_EN_DELAY_MAX)
2140 mid_min += start_dqs_en - mid_min - IO_DQS_EN_DELAY_MAX;
2141 else if (start_dqs_en - mid_min < 0)
2142 mid_min += start_dqs_en - mid_min;
2144 new_dqs = start_dqs - mid_min;
2146 debug_cond(DLEVEL == 1, "vfifo_center: start_dqs=%d start_dqs_en=%d \
2147 new_dqs=%d mid_min=%d\n", start_dqs,
2148 IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS ? start_dqs_en : -1,
2151 /* Initialize data for export structures */
2152 dqs_margin = IO_IO_IN_DELAY_MAX + 1;
2153 dq_margin = IO_IO_IN_DELAY_MAX + 1;
2155 /* add delay to bring centre of all DQ windows to the same "level" */
2156 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_READ_DQS; i++, p++) {
2157 /* Use values before divide by 2 to reduce round off error */
2158 shift_dq = (left_edge[i] - right_edge[i] -
2159 (left_edge[min_index] - right_edge[min_index]))/2 +
2160 (orig_mid_min - mid_min);
2162 debug_cond(DLEVEL == 2, "vfifo_center: before: \
2163 shift_dq[%u]=%d\n", i, shift_dq);
2165 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_IN_DELAY_OFFSET;
2166 temp_dq_in_delay1 = readl(addr + (p << 2));
2167 temp_dq_in_delay2 = readl(addr + (i << 2));
2169 if (shift_dq + (int32_t)temp_dq_in_delay1 >
2170 (int32_t)IO_IO_IN_DELAY_MAX) {
2171 shift_dq = (int32_t)IO_IO_IN_DELAY_MAX - temp_dq_in_delay2;
2172 } else if (shift_dq + (int32_t)temp_dq_in_delay1 < 0) {
2173 shift_dq = -(int32_t)temp_dq_in_delay1;
2175 debug_cond(DLEVEL == 2, "vfifo_center: after: \
2176 shift_dq[%u]=%d\n", i, shift_dq);
2177 final_dq[i] = temp_dq_in_delay1 + shift_dq;
2178 scc_mgr_set_dq_in_delay(p, final_dq[i]);
2181 debug_cond(DLEVEL == 2, "vfifo_center: margin[%u]=[%d,%d]\n", i,
2182 left_edge[i] - shift_dq + (-mid_min),
2183 right_edge[i] + shift_dq - (-mid_min));
2184 /* To determine values for export structures */
2185 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2186 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2188 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2189 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2192 final_dqs = new_dqs;
2193 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS)
2194 final_dqs_en = start_dqs_en - mid_min;
2197 if (IO_SHIFT_DQS_EN_WHEN_SHIFT_DQS) {
2198 scc_mgr_set_dqs_en_delay(read_group, final_dqs_en);
2199 scc_mgr_load_dqs(read_group);
2203 scc_mgr_set_dqs_bus_in_delay(read_group, final_dqs);
2204 scc_mgr_load_dqs(read_group);
2205 debug_cond(DLEVEL == 2, "%s:%d vfifo_center: dq_margin=%d \
2206 dqs_margin=%d", __func__, __LINE__,
2207 dq_margin, dqs_margin);
2210 * Do not remove this line as it makes sure all of our decisions
2211 * have been applied. Apply the update bit.
2213 writel(0, &sdr_scc_mgr->update);
2215 return (dq_margin >= 0) && (dqs_margin >= 0);
2219 * calibrate the read valid prediction FIFO.
2221 * - read valid prediction will consist of finding a good DQS enable phase,
2222 * DQS enable delay, DQS input phase, and DQS input delay.
2223 * - we also do a per-bit deskew on the DQ lines.
2225 static uint32_t rw_mgr_mem_calibrate_vfifo(uint32_t read_group,
2228 uint32_t p, d, rank_bgn, sr;
2229 uint32_t dtaps_per_ptap;
2232 uint32_t grp_calibrated;
2233 uint32_t write_group, write_test_bgn;
2234 uint32_t failed_substage;
2236 debug("%s:%d: %u %u\n", __func__, __LINE__, read_group, test_bgn);
2238 /* update info for sims */
2239 reg_file_set_stage(CAL_STAGE_VFIFO);
2241 write_group = read_group;
2242 write_test_bgn = test_bgn;
2244 /* USER Determine number of delay taps for each phase tap */
2247 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
2249 tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
2254 /* update info for sims */
2255 reg_file_set_group(read_group);
2259 reg_file_set_sub_stage(CAL_SUBSTAGE_GUARANTEED_READ);
2260 failed_substage = CAL_SUBSTAGE_GUARANTEED_READ;
2262 for (d = 0; d <= dtaps_per_ptap && grp_calibrated == 0; d += 2) {
2264 * In RLDRAMX we may be messing the delay of pins in
2265 * the same write group but outside of the current read
2266 * the group, but that's ok because we haven't
2267 * calibrated output side yet.
2270 scc_mgr_apply_group_all_out_delay_add_all_ranks
2271 (write_group, write_test_bgn, d);
2274 for (p = 0; p <= IO_DQDQS_OUT_PHASE_MAX && grp_calibrated == 0;
2276 /* set a particular dqdqs phase */
2277 scc_mgr_set_dqdqs_output_phase_all_ranks(read_group, p);
2279 debug_cond(DLEVEL == 1, "%s:%d calibrate_vfifo: g=%u \
2280 p=%u d=%u\n", __func__, __LINE__,
2284 * Load up the patterns used by read calibration
2285 * using current DQDQS phase.
2287 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2288 if (!(gbl->phy_debug_mode_flags &
2289 PHY_DEBUG_DISABLE_GUARANTEED_READ)) {
2290 if (!rw_mgr_mem_calibrate_read_test_patterns_all_ranks
2291 (read_group, 1, &bit_chk)) {
2292 debug_cond(DLEVEL == 1, "%s:%d Guaranteed read test failed:",
2293 __func__, __LINE__);
2294 debug_cond(DLEVEL == 1, " g=%u p=%u d=%u\n",
2302 if (rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase_sweep_dq_in_delay
2303 (write_group, read_group, test_bgn)) {
2305 * USER Read per-bit deskew can be done on a
2306 * per shadow register basis.
2308 for (rank_bgn = 0, sr = 0;
2309 rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2310 rank_bgn += NUM_RANKS_PER_SHADOW_REG,
2313 * Determine if this set of ranks
2314 * should be skipped entirely.
2316 if (!param->skip_shadow_regs[sr]) {
2318 * If doing read after write
2319 * calibration, do not update
2320 * FOM, now - do it then.
2322 if (!rw_mgr_mem_calibrate_vfifo_center
2323 (rank_bgn, write_group,
2324 read_group, test_bgn, 1, 0)) {
2327 CAL_SUBSTAGE_VFIFO_CENTER;
2333 failed_substage = CAL_SUBSTAGE_DQS_EN_PHASE;
2338 if (grp_calibrated == 0) {
2339 set_failing_group_stage(write_group, CAL_STAGE_VFIFO,
2345 * Reset the delay chains back to zero if they have moved > 1
2346 * (check for > 1 because loop will increase d even when pass in
2350 scc_mgr_zero_group(write_group, write_test_bgn, 1);
2355 /* VFIFO Calibration -- Read Deskew Calibration after write deskew */
2356 static uint32_t rw_mgr_mem_calibrate_vfifo_end(uint32_t read_group,
2359 uint32_t rank_bgn, sr;
2360 uint32_t grp_calibrated;
2361 uint32_t write_group;
2363 debug("%s:%d %u %u", __func__, __LINE__, read_group, test_bgn);
2365 /* update info for sims */
2367 reg_file_set_stage(CAL_STAGE_VFIFO_AFTER_WRITES);
2368 reg_file_set_sub_stage(CAL_SUBSTAGE_VFIFO_CENTER);
2370 write_group = read_group;
2372 /* update info for sims */
2373 reg_file_set_group(read_group);
2376 /* Read per-bit deskew can be done on a per shadow register basis */
2377 for (rank_bgn = 0, sr = 0; rank_bgn < RW_MGR_MEM_NUMBER_OF_RANKS;
2378 rank_bgn += NUM_RANKS_PER_SHADOW_REG, ++sr) {
2379 /* Determine if this set of ranks should be skipped entirely */
2380 if (!param->skip_shadow_regs[sr]) {
2381 /* This is the last calibration round, update FOM here */
2382 if (!rw_mgr_mem_calibrate_vfifo_center(rank_bgn,
2393 if (grp_calibrated == 0) {
2394 set_failing_group_stage(write_group,
2395 CAL_STAGE_VFIFO_AFTER_WRITES,
2396 CAL_SUBSTAGE_VFIFO_CENTER);
2403 /* Calibrate LFIFO to find smallest read latency */
2404 static uint32_t rw_mgr_mem_calibrate_lfifo(void)
2409 debug("%s:%d\n", __func__, __LINE__);
2411 /* update info for sims */
2412 reg_file_set_stage(CAL_STAGE_LFIFO);
2413 reg_file_set_sub_stage(CAL_SUBSTAGE_READ_LATENCY);
2415 /* Load up the patterns used by read calibration for all ranks */
2416 rw_mgr_mem_calibrate_read_load_patterns(0, 1);
2420 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
2421 debug_cond(DLEVEL == 2, "%s:%d lfifo: read_lat=%u",
2422 __func__, __LINE__, gbl->curr_read_lat);
2424 if (!rw_mgr_mem_calibrate_read_test_all_ranks(0,
2432 /* reduce read latency and see if things are working */
2434 gbl->curr_read_lat--;
2435 } while (gbl->curr_read_lat > 0);
2437 /* reset the fifos to get pointers to known state */
2439 writel(0, &phy_mgr_cmd->fifo_reset);
2442 /* add a fudge factor to the read latency that was determined */
2443 gbl->curr_read_lat += 2;
2444 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
2445 debug_cond(DLEVEL == 2, "%s:%d lfifo: success: using \
2446 read_lat=%u\n", __func__, __LINE__,
2447 gbl->curr_read_lat);
2450 set_failing_group_stage(0xff, CAL_STAGE_LFIFO,
2451 CAL_SUBSTAGE_READ_LATENCY);
2453 debug_cond(DLEVEL == 2, "%s:%d lfifo: failed at initial \
2454 read_lat=%u\n", __func__, __LINE__,
2455 gbl->curr_read_lat);
2461 * issue write test command.
2462 * two variants are provided. one that just tests a write pattern and
2463 * another that tests datamask functionality.
2465 static void rw_mgr_mem_calibrate_write_test_issue(uint32_t group,
2468 uint32_t mcc_instruction;
2469 uint32_t quick_write_mode = (((STATIC_CALIB_STEPS) & CALIB_SKIP_WRITES) &&
2470 ENABLE_SUPER_QUICK_CALIBRATION);
2471 uint32_t rw_wl_nop_cycles;
2475 * Set counter and jump addresses for the right
2476 * number of NOP cycles.
2477 * The number of supported NOP cycles can range from -1 to infinity
2478 * Three different cases are handled:
2480 * 1. For a number of NOP cycles greater than 0, the RW Mgr looping
2481 * mechanism will be used to insert the right number of NOPs
2483 * 2. For a number of NOP cycles equals to 0, the micro-instruction
2484 * issuing the write command will jump straight to the
2485 * micro-instruction that turns on DQS (for DDRx), or outputs write
2486 * data (for RLD), skipping
2487 * the NOP micro-instruction all together
2489 * 3. A number of NOP cycles equal to -1 indicates that DQS must be
2490 * turned on in the same micro-instruction that issues the write
2491 * command. Then we need
2492 * to directly jump to the micro-instruction that sends out the data
2494 * NOTE: Implementing this mechanism uses 2 RW Mgr jump-counters
2495 * (2 and 3). One jump-counter (0) is used to perform multiple
2496 * write-read operations.
2497 * one counter left to issue this command in "multiple-group" mode
2500 rw_wl_nop_cycles = gbl->rw_wl_nop_cycles;
2502 if (rw_wl_nop_cycles == -1) {
2504 * CNTR 2 - We want to execute the special write operation that
2505 * turns on DQS right away and then skip directly to the
2506 * instruction that sends out the data. We set the counter to a
2507 * large number so that the jump is always taken.
2509 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
2511 /* CNTR 3 - Not used */
2513 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0_WL_1;
2514 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DATA,
2515 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2516 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2517 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
2519 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0_WL_1;
2520 writel(RW_MGR_LFSR_WR_RD_BANK_0_DATA,
2521 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2522 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2523 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
2525 } else if (rw_wl_nop_cycles == 0) {
2527 * CNTR 2 - We want to skip the NOP operation and go straight
2528 * to the DQS enable instruction. We set the counter to a large
2529 * number so that the jump is always taken.
2531 writel(0xFF, &sdr_rw_load_mgr_regs->load_cntr2);
2533 /* CNTR 3 - Not used */
2535 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
2536 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_DQS,
2537 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2539 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
2540 writel(RW_MGR_LFSR_WR_RD_BANK_0_DQS,
2541 &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2545 * CNTR 2 - In this case we want to execute the next instruction
2546 * and NOT take the jump. So we set the counter to 0. The jump
2547 * address doesn't count.
2549 writel(0x0, &sdr_rw_load_mgr_regs->load_cntr2);
2550 writel(0x0, &sdr_rw_load_jump_mgr_regs->load_jump_add2);
2553 * CNTR 3 - Set the nop counter to the number of cycles we
2554 * need to loop for, minus 1.
2556 writel(rw_wl_nop_cycles - 1, &sdr_rw_load_mgr_regs->load_cntr3);
2558 mcc_instruction = RW_MGR_LFSR_WR_RD_DM_BANK_0;
2559 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_NOP,
2560 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
2562 mcc_instruction = RW_MGR_LFSR_WR_RD_BANK_0;
2563 writel(RW_MGR_LFSR_WR_RD_BANK_0_NOP,
2564 &sdr_rw_load_jump_mgr_regs->load_jump_add3);
2568 writel(0, SDR_PHYGRP_RWMGRGRP_ADDRESS |
2569 RW_MGR_RESET_READ_DATAPATH_OFFSET);
2571 if (quick_write_mode)
2572 writel(0x08, &sdr_rw_load_mgr_regs->load_cntr0);
2574 writel(0x40, &sdr_rw_load_mgr_regs->load_cntr0);
2576 writel(mcc_instruction, &sdr_rw_load_jump_mgr_regs->load_jump_add0);
2579 * CNTR 1 - This is used to ensure enough time elapses
2580 * for read data to come back.
2582 writel(0x30, &sdr_rw_load_mgr_regs->load_cntr1);
2585 writel(RW_MGR_LFSR_WR_RD_DM_BANK_0_WAIT,
2586 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
2588 writel(RW_MGR_LFSR_WR_RD_BANK_0_WAIT,
2589 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
2592 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_RUN_SINGLE_GROUP_OFFSET;
2593 writel(mcc_instruction, addr + (group << 2));
2596 /* Test writes, can check for a single bit pass or multiple bit pass */
2597 static uint32_t rw_mgr_mem_calibrate_write_test(uint32_t rank_bgn,
2598 uint32_t write_group, uint32_t use_dm, uint32_t all_correct,
2599 uint32_t *bit_chk, uint32_t all_ranks)
2602 uint32_t correct_mask_vg;
2603 uint32_t tmp_bit_chk;
2605 uint32_t rank_end = all_ranks ? RW_MGR_MEM_NUMBER_OF_RANKS :
2606 (rank_bgn + NUM_RANKS_PER_SHADOW_REG);
2607 uint32_t addr_rw_mgr;
2608 uint32_t base_rw_mgr;
2610 *bit_chk = param->write_correct_mask;
2611 correct_mask_vg = param->write_correct_mask_vg;
2613 for (r = rank_bgn; r < rank_end; r++) {
2614 if (param->skip_ranks[r]) {
2615 /* request to skip the rank */
2620 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_READ_WRITE);
2623 addr_rw_mgr = SDR_PHYGRP_RWMGRGRP_ADDRESS;
2624 for (vg = RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS-1; ; vg--) {
2625 /* reset the fifos to get pointers to known state */
2626 writel(0, &phy_mgr_cmd->fifo_reset);
2628 tmp_bit_chk = tmp_bit_chk <<
2629 (RW_MGR_MEM_DQ_PER_WRITE_DQS /
2630 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
2631 rw_mgr_mem_calibrate_write_test_issue(write_group *
2632 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS+vg,
2635 base_rw_mgr = readl(addr_rw_mgr);
2636 tmp_bit_chk = tmp_bit_chk | (correct_mask_vg & ~(base_rw_mgr));
2640 *bit_chk &= tmp_bit_chk;
2644 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2645 debug_cond(DLEVEL == 2, "write_test(%u,%u,ALL) : %u == \
2646 %u => %lu", write_group, use_dm,
2647 *bit_chk, param->write_correct_mask,
2648 (long unsigned int)(*bit_chk ==
2649 param->write_correct_mask));
2650 return *bit_chk == param->write_correct_mask;
2652 set_rank_and_odt_mask(0, RW_MGR_ODT_MODE_OFF);
2653 debug_cond(DLEVEL == 2, "write_test(%u,%u,ONE) : %u != ",
2654 write_group, use_dm, *bit_chk);
2655 debug_cond(DLEVEL == 2, "%lu" " => %lu", (long unsigned int)0,
2656 (long unsigned int)(*bit_chk != 0));
2657 return *bit_chk != 0x00;
2662 * center all windows. do per-bit-deskew to possibly increase size of
2665 static uint32_t rw_mgr_mem_calibrate_writes_center(uint32_t rank_bgn,
2666 uint32_t write_group, uint32_t test_bgn)
2668 uint32_t i, p, min_index;
2671 * Store these as signed since there are comparisons with
2675 uint32_t sticky_bit_chk;
2676 int32_t left_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2677 int32_t right_edge[RW_MGR_MEM_DQ_PER_WRITE_DQS];
2679 int32_t mid_min, orig_mid_min;
2680 int32_t new_dqs, start_dqs, shift_dq;
2681 int32_t dq_margin, dqs_margin, dm_margin;
2683 uint32_t temp_dq_out1_delay;
2686 debug("%s:%d %u %u", __func__, __LINE__, write_group, test_bgn);
2690 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
2691 start_dqs = readl(addr +
2692 (RW_MGR_MEM_DQ_PER_WRITE_DQS << 2));
2694 /* per-bit deskew */
2697 * set the left and right edge of each bit to an illegal value
2698 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value.
2701 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2702 left_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2703 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2706 /* Search for the left edge of the window for each bit */
2707 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX; d++) {
2708 scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, d);
2710 writel(0, &sdr_scc_mgr->update);
2713 * Stop searching when the read test doesn't pass AND when
2714 * we've seen a passing read on every bit.
2716 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2717 0, PASS_ONE_BIT, &bit_chk, 0);
2718 sticky_bit_chk = sticky_bit_chk | bit_chk;
2719 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2720 debug_cond(DLEVEL == 2, "write_center(left): dtap=%d => %u \
2721 == %u && %u [bit_chk= %u ]\n",
2722 d, sticky_bit_chk, param->write_correct_mask,
2728 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2731 * Remember a passing test as the
2737 * If a left edge has not been seen
2738 * yet, then a future passing test will
2739 * mark this edge as the right edge.
2742 IO_IO_OUT1_DELAY_MAX + 1) {
2743 right_edge[i] = -(d + 1);
2746 debug_cond(DLEVEL == 2, "write_center[l,d=%d):", d);
2747 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2748 (int)(bit_chk & 1), i, left_edge[i]);
2749 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2751 bit_chk = bit_chk >> 1;
2756 /* Reset DQ delay chains to 0 */
2757 scc_mgr_apply_group_dq_out1_delay(write_group, test_bgn, 0);
2759 for (i = RW_MGR_MEM_DQ_PER_WRITE_DQS - 1;; i--) {
2760 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2761 %d right_edge[%u]: %d\n", __func__, __LINE__,
2762 i, left_edge[i], i, right_edge[i]);
2765 * Check for cases where we haven't found the left edge,
2766 * which makes our assignment of the the right edge invalid.
2767 * Reset it to the illegal value.
2769 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) &&
2770 (right_edge[i] != IO_IO_OUT1_DELAY_MAX + 1)) {
2771 right_edge[i] = IO_IO_OUT1_DELAY_MAX + 1;
2772 debug_cond(DLEVEL == 2, "%s:%d write_center: reset \
2773 right_edge[%u]: %d\n", __func__, __LINE__,
2778 * Reset sticky bit (except for bits where we have
2779 * seen the left edge).
2781 sticky_bit_chk = sticky_bit_chk << 1;
2782 if ((left_edge[i] != IO_IO_OUT1_DELAY_MAX + 1))
2783 sticky_bit_chk = sticky_bit_chk | 1;
2789 /* Search for the right edge of the window for each bit */
2790 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - start_dqs; d++) {
2791 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
2794 writel(0, &sdr_scc_mgr->update);
2797 * Stop searching when the read test doesn't pass AND when
2798 * we've seen a passing read on every bit.
2800 stop = !rw_mgr_mem_calibrate_write_test(rank_bgn, write_group,
2801 0, PASS_ONE_BIT, &bit_chk, 0);
2803 sticky_bit_chk = sticky_bit_chk | bit_chk;
2804 stop = stop && (sticky_bit_chk == param->write_correct_mask);
2806 debug_cond(DLEVEL == 2, "write_center (right): dtap=%u => %u == \
2807 %u && %u\n", d, sticky_bit_chk,
2808 param->write_correct_mask, stop);
2812 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS;
2814 /* d = 0 failed, but it passed when
2815 testing the left edge, so it must be
2816 marginal, set it to -1 */
2817 if (right_edge[i] ==
2818 IO_IO_OUT1_DELAY_MAX + 1 &&
2820 IO_IO_OUT1_DELAY_MAX + 1) {
2827 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2830 * Remember a passing test as
2837 * If a right edge has not
2838 * been seen yet, then a future
2839 * passing test will mark this
2840 * edge as the left edge.
2842 if (right_edge[i] ==
2843 IO_IO_OUT1_DELAY_MAX + 1)
2844 left_edge[i] = -(d + 1);
2847 * d = 0 failed, but it passed
2848 * when testing the left edge,
2849 * so it must be marginal, set
2852 if (right_edge[i] ==
2853 IO_IO_OUT1_DELAY_MAX + 1 &&
2855 IO_IO_OUT1_DELAY_MAX + 1)
2858 * If a right edge has not been
2859 * seen yet, then a future
2860 * passing test will mark this
2861 * edge as the left edge.
2863 else if (right_edge[i] ==
2864 IO_IO_OUT1_DELAY_MAX +
2866 left_edge[i] = -(d + 1);
2869 debug_cond(DLEVEL == 2, "write_center[r,d=%d):", d);
2870 debug_cond(DLEVEL == 2, "bit_chk_test=%d left_edge[%u]: %d",
2871 (int)(bit_chk & 1), i, left_edge[i]);
2872 debug_cond(DLEVEL == 2, "right_edge[%u]: %d\n", i,
2874 bit_chk = bit_chk >> 1;
2879 /* Check that all bits have a window */
2880 for (i = 0; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2881 debug_cond(DLEVEL == 2, "%s:%d write_center: left_edge[%u]: \
2882 %d right_edge[%u]: %d", __func__, __LINE__,
2883 i, left_edge[i], i, right_edge[i]);
2884 if ((left_edge[i] == IO_IO_OUT1_DELAY_MAX + 1) ||
2885 (right_edge[i] == IO_IO_OUT1_DELAY_MAX + 1)) {
2886 set_failing_group_stage(test_bgn + i,
2888 CAL_SUBSTAGE_WRITES_CENTER);
2893 /* Find middle of window for each DQ bit */
2894 mid_min = left_edge[0] - right_edge[0];
2896 for (i = 1; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++) {
2897 mid = left_edge[i] - right_edge[i];
2898 if (mid < mid_min) {
2905 * -mid_min/2 represents the amount that we need to move DQS.
2906 * If mid_min is odd and positive we'll need to add one to
2907 * make sure the rounding in further calculations is correct
2908 * (always bias to the right), so just add 1 for all positive values.
2912 mid_min = mid_min / 2;
2913 debug_cond(DLEVEL == 1, "%s:%d write_center: mid_min=%d\n", __func__,
2916 /* Determine the amount we can change DQS (which is -mid_min) */
2917 orig_mid_min = mid_min;
2918 new_dqs = start_dqs;
2920 debug_cond(DLEVEL == 1, "%s:%d write_center: start_dqs=%d new_dqs=%d \
2921 mid_min=%d\n", __func__, __LINE__, start_dqs, new_dqs, mid_min);
2922 /* Initialize data for export structures */
2923 dqs_margin = IO_IO_OUT1_DELAY_MAX + 1;
2924 dq_margin = IO_IO_OUT1_DELAY_MAX + 1;
2926 /* add delay to bring centre of all DQ windows to the same "level" */
2927 for (i = 0, p = test_bgn; i < RW_MGR_MEM_DQ_PER_WRITE_DQS; i++, p++) {
2928 /* Use values before divide by 2 to reduce round off error */
2929 shift_dq = (left_edge[i] - right_edge[i] -
2930 (left_edge[min_index] - right_edge[min_index]))/2 +
2931 (orig_mid_min - mid_min);
2933 debug_cond(DLEVEL == 2, "%s:%d write_center: before: shift_dq \
2934 [%u]=%d\n", __func__, __LINE__, i, shift_dq);
2936 addr = SDR_PHYGRP_SCCGRP_ADDRESS | SCC_MGR_IO_OUT1_DELAY_OFFSET;
2937 temp_dq_out1_delay = readl(addr + (i << 2));
2938 if (shift_dq + (int32_t)temp_dq_out1_delay >
2939 (int32_t)IO_IO_OUT1_DELAY_MAX) {
2940 shift_dq = (int32_t)IO_IO_OUT1_DELAY_MAX - temp_dq_out1_delay;
2941 } else if (shift_dq + (int32_t)temp_dq_out1_delay < 0) {
2942 shift_dq = -(int32_t)temp_dq_out1_delay;
2944 debug_cond(DLEVEL == 2, "write_center: after: shift_dq[%u]=%d\n",
2946 scc_mgr_set_dq_out1_delay(i, temp_dq_out1_delay + shift_dq);
2949 debug_cond(DLEVEL == 2, "write_center: margin[%u]=[%d,%d]\n", i,
2950 left_edge[i] - shift_dq + (-mid_min),
2951 right_edge[i] + shift_dq - (-mid_min));
2952 /* To determine values for export structures */
2953 if (left_edge[i] - shift_dq + (-mid_min) < dq_margin)
2954 dq_margin = left_edge[i] - shift_dq + (-mid_min);
2956 if (right_edge[i] + shift_dq - (-mid_min) < dqs_margin)
2957 dqs_margin = right_edge[i] + shift_dq - (-mid_min);
2961 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
2962 writel(0, &sdr_scc_mgr->update);
2965 debug_cond(DLEVEL == 2, "%s:%d write_center: DM\n", __func__, __LINE__);
2968 * set the left and right edge of each bit to an illegal value,
2969 * use (IO_IO_OUT1_DELAY_MAX + 1) as an illegal value,
2971 left_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2972 right_edge[0] = IO_IO_OUT1_DELAY_MAX + 1;
2973 int32_t bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
2974 int32_t end_curr = IO_IO_OUT1_DELAY_MAX + 1;
2975 int32_t bgn_best = IO_IO_OUT1_DELAY_MAX + 1;
2976 int32_t end_best = IO_IO_OUT1_DELAY_MAX + 1;
2977 int32_t win_best = 0;
2979 /* Search for the/part of the window with DM shift */
2980 for (d = IO_IO_OUT1_DELAY_MAX; d >= 0; d -= DELTA_D) {
2981 scc_mgr_apply_group_dm_out1_delay(write_group, d);
2982 writel(0, &sdr_scc_mgr->update);
2984 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
2985 PASS_ALL_BITS, &bit_chk,
2987 /* USE Set current end of the window */
2990 * If a starting edge of our window has not been seen
2991 * this is our current start of the DM window.
2993 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
2997 * If current window is bigger than best seen.
2998 * Set best seen to be current window.
3000 if ((end_curr-bgn_curr+1) > win_best) {
3001 win_best = end_curr-bgn_curr+1;
3002 bgn_best = bgn_curr;
3003 end_best = end_curr;
3006 /* We just saw a failing test. Reset temp edge */
3007 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3008 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3013 /* Reset DM delay chains to 0 */
3014 scc_mgr_apply_group_dm_out1_delay(write_group, 0);
3017 * Check to see if the current window nudges up aganist 0 delay.
3018 * If so we need to continue the search by shifting DQS otherwise DQS
3019 * search begins as a new search. */
3020 if (end_curr != 0) {
3021 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3022 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3025 /* Search for the/part of the window with DQS shifts */
3026 for (d = 0; d <= IO_IO_OUT1_DELAY_MAX - new_dqs; d += DELTA_D) {
3028 * Note: This only shifts DQS, so are we limiting ourselve to
3029 * width of DQ unnecessarily.
3031 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group,
3034 writel(0, &sdr_scc_mgr->update);
3035 if (rw_mgr_mem_calibrate_write_test(rank_bgn, write_group, 1,
3036 PASS_ALL_BITS, &bit_chk,
3038 /* USE Set current end of the window */
3041 * If a beginning edge of our window has not been seen
3042 * this is our current begin of the DM window.
3044 if (bgn_curr == IO_IO_OUT1_DELAY_MAX + 1)
3048 * If current window is bigger than best seen. Set best
3049 * seen to be current window.
3051 if ((end_curr-bgn_curr+1) > win_best) {
3052 win_best = end_curr-bgn_curr+1;
3053 bgn_best = bgn_curr;
3054 end_best = end_curr;
3057 /* We just saw a failing test. Reset temp edge */
3058 bgn_curr = IO_IO_OUT1_DELAY_MAX + 1;
3059 end_curr = IO_IO_OUT1_DELAY_MAX + 1;
3061 /* Early exit optimization: if ther remaining delay
3062 chain space is less than already seen largest window
3065 (IO_IO_OUT1_DELAY_MAX - new_dqs - d)) {
3071 /* assign left and right edge for cal and reporting; */
3072 left_edge[0] = -1*bgn_best;
3073 right_edge[0] = end_best;
3075 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d\n", __func__,
3076 __LINE__, left_edge[0], right_edge[0]);
3078 /* Move DQS (back to orig) */
3079 scc_mgr_apply_group_dqs_io_and_oct_out1(write_group, new_dqs);
3083 /* Find middle of window for the DM bit */
3084 mid = (left_edge[0] - right_edge[0]) / 2;
3086 /* only move right, since we are not moving DQS/DQ */
3090 /* dm_marign should fail if we never find a window */
3094 dm_margin = left_edge[0] - mid;
3096 scc_mgr_apply_group_dm_out1_delay(write_group, mid);
3097 writel(0, &sdr_scc_mgr->update);
3099 debug_cond(DLEVEL == 2, "%s:%d dm_calib: left=%d right=%d mid=%d \
3100 dm_margin=%d\n", __func__, __LINE__, left_edge[0],
3101 right_edge[0], mid, dm_margin);
3103 gbl->fom_out += dq_margin + dqs_margin;
3105 debug_cond(DLEVEL == 2, "%s:%d write_center: dq_margin=%d \
3106 dqs_margin=%d dm_margin=%d\n", __func__, __LINE__,
3107 dq_margin, dqs_margin, dm_margin);
3110 * Do not remove this line as it makes sure all of our
3111 * decisions have been applied.
3113 writel(0, &sdr_scc_mgr->update);
3114 return (dq_margin >= 0) && (dqs_margin >= 0) && (dm_margin >= 0);
3117 /* calibrate the write operations */
3118 static uint32_t rw_mgr_mem_calibrate_writes(uint32_t rank_bgn, uint32_t g,
3121 /* update info for sims */
3122 debug("%s:%d %u %u\n", __func__, __LINE__, g, test_bgn);
3124 reg_file_set_stage(CAL_STAGE_WRITES);
3125 reg_file_set_sub_stage(CAL_SUBSTAGE_WRITES_CENTER);
3127 reg_file_set_group(g);
3129 if (!rw_mgr_mem_calibrate_writes_center(rank_bgn, g, test_bgn)) {
3130 set_failing_group_stage(g, CAL_STAGE_WRITES,
3131 CAL_SUBSTAGE_WRITES_CENTER);
3138 /* precharge all banks and activate row 0 in bank "000..." and bank "111..." */
3139 static void mem_precharge_and_activate(void)
3143 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS; r++) {
3144 if (param->skip_ranks[r]) {
3145 /* request to skip the rank */
3150 set_rank_and_odt_mask(r, RW_MGR_ODT_MODE_OFF);
3152 /* precharge all banks ... */
3153 writel(RW_MGR_PRECHARGE_ALL, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3154 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3156 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr0);
3157 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT1,
3158 &sdr_rw_load_jump_mgr_regs->load_jump_add0);
3160 writel(0x0F, &sdr_rw_load_mgr_regs->load_cntr1);
3161 writel(RW_MGR_ACTIVATE_0_AND_1_WAIT2,
3162 &sdr_rw_load_jump_mgr_regs->load_jump_add1);
3165 writel(RW_MGR_ACTIVATE_0_AND_1, SDR_PHYGRP_RWMGRGRP_ADDRESS |
3166 RW_MGR_RUN_SINGLE_GROUP_OFFSET);
3170 /* Configure various memory related parameters. */
3171 static void mem_config(void)
3173 uint32_t rlat, wlat;
3174 uint32_t rw_wl_nop_cycles;
3175 uint32_t max_latency;
3177 debug("%s:%d\n", __func__, __LINE__);
3178 /* read in write and read latency */
3179 wlat = readl(&data_mgr->t_wl_add);
3180 wlat += readl(&data_mgr->mem_t_add);
3182 /* WL for hard phy does not include additive latency */
3185 * add addtional write latency to offset the address/command extra
3186 * clock cycle. We change the AC mux setting causing AC to be delayed
3187 * by one mem clock cycle. Only do this for DDR3
3191 rlat = readl(&data_mgr->t_rl_add);
3193 rw_wl_nop_cycles = wlat - 2;
3194 gbl->rw_wl_nop_cycles = rw_wl_nop_cycles;
3197 * For AV/CV, lfifo is hardened and always runs at full rate so
3198 * max latency in AFI clocks, used here, is correspondingly smaller.
3200 max_latency = (1<<MAX_LATENCY_COUNT_WIDTH)/1 - 1;
3201 /* configure for a burst length of 8 */
3204 /* Adjust Write Latency for Hard PHY */
3207 /* set a pretty high read latency initially */
3208 gbl->curr_read_lat = rlat + 16;
3210 if (gbl->curr_read_lat > max_latency)
3211 gbl->curr_read_lat = max_latency;
3213 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
3215 /* advertise write latency */
3216 gbl->curr_write_lat = wlat;
3217 writel(wlat - 2, &phy_mgr_cfg->afi_wlat);
3219 /* initialize bit slips */
3220 mem_precharge_and_activate();
3223 /* Set VFIFO and LFIFO to instant-on settings in skip calibration mode */
3224 static void mem_skip_calibrate(void)
3226 uint32_t vfifo_offset;
3229 debug("%s:%d\n", __func__, __LINE__);
3230 /* Need to update every shadow register set used by the interface */
3231 for (r = 0; r < RW_MGR_MEM_NUMBER_OF_RANKS;
3232 r += NUM_RANKS_PER_SHADOW_REG) {
3234 * Set output phase alignment settings appropriate for
3237 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3238 scc_mgr_set_dqs_en_phase(i, 0);
3239 #if IO_DLL_CHAIN_LENGTH == 6
3240 scc_mgr_set_dqdqs_output_phase(i, 6);
3242 scc_mgr_set_dqdqs_output_phase(i, 7);
3247 * Write data arrives to the I/O two cycles before write
3248 * latency is reached (720 deg).
3249 * -> due to bit-slip in a/c bus
3250 * -> to allow board skew where dqs is longer than ck
3251 * -> how often can this happen!?
3252 * -> can claim back some ptaps for high freq
3253 * support if we can relax this, but i digress...
3255 * The write_clk leads mem_ck by 90 deg
3256 * The minimum ptap of the OPA is 180 deg
3257 * Each ptap has (360 / IO_DLL_CHAIN_LENGH) deg of delay
3258 * The write_clk is always delayed by 2 ptaps
3260 * Hence, to make DQS aligned to CK, we need to delay
3262 * (720 - 90 - 180 - 2 * (360 / IO_DLL_CHAIN_LENGTH))
3264 * Dividing the above by (360 / IO_DLL_CHAIN_LENGTH)
3265 * gives us the number of ptaps, which simplies to:
3267 * (1.25 * IO_DLL_CHAIN_LENGTH - 2)
3269 scc_mgr_set_dqdqs_output_phase(i, (1.25 *
3270 IO_DLL_CHAIN_LENGTH - 2));
3272 writel(0xff, &sdr_scc_mgr->dqs_ena);
3273 writel(0xff, &sdr_scc_mgr->dqs_io_ena);
3275 for (i = 0; i < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; i++) {
3276 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3277 SCC_MGR_GROUP_COUNTER_OFFSET);
3279 writel(0xff, &sdr_scc_mgr->dq_ena);
3280 writel(0xff, &sdr_scc_mgr->dm_ena);
3281 writel(0, &sdr_scc_mgr->update);
3284 /* Compensate for simulation model behaviour */
3285 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3286 scc_mgr_set_dqs_bus_in_delay(i, 10);
3287 scc_mgr_load_dqs(i);
3289 writel(0, &sdr_scc_mgr->update);
3292 * ArriaV has hard FIFOs that can only be initialized by incrementing
3295 vfifo_offset = CALIB_VFIFO_OFFSET;
3296 for (j = 0; j < vfifo_offset; j++) {
3297 writel(0xff, &phy_mgr_cmd->inc_vfifo_hard_phy);
3299 writel(0, &phy_mgr_cmd->fifo_reset);
3302 * For ACV with hard lfifo, we get the skip-cal setting from
3303 * generation-time constant.
3305 gbl->curr_read_lat = CALIB_LFIFO_OFFSET;
3306 writel(gbl->curr_read_lat, &phy_mgr_cfg->phy_rlat);
3309 /* Memory calibration entry point */
3310 static uint32_t mem_calibrate(void)
3313 uint32_t rank_bgn, sr;
3314 uint32_t write_group, write_test_bgn;
3315 uint32_t read_group, read_test_bgn;
3316 uint32_t run_groups, current_run;
3317 uint32_t failing_groups = 0;
3318 uint32_t group_failed = 0;
3319 uint32_t sr_failed = 0;
3321 debug("%s:%d\n", __func__, __LINE__);
3322 /* Initialize the data settings */
3324 gbl->error_substage = CAL_SUBSTAGE_NIL;
3325 gbl->error_stage = CAL_STAGE_NIL;
3326 gbl->error_group = 0xff;
3332 uint32_t bypass_mode = 0x1;
3333 for (i = 0; i < RW_MGR_MEM_IF_READ_DQS_WIDTH; i++) {
3334 writel(i, SDR_PHYGRP_SCCGRP_ADDRESS |
3335 SCC_MGR_GROUP_COUNTER_OFFSET);
3336 scc_set_bypass_mode(i, bypass_mode);
3339 if ((dyn_calib_steps & CALIB_SKIP_ALL) == CALIB_SKIP_ALL) {
3341 * Set VFIFO and LFIFO to instant-on settings in skip
3344 mem_skip_calibrate();
3346 for (i = 0; i < NUM_CALIB_REPEAT; i++) {
3348 * Zero all delay chain/phase settings for all
3349 * groups and all shadow register sets.
3353 run_groups = ~param->skip_groups;
3355 for (write_group = 0, write_test_bgn = 0; write_group
3356 < RW_MGR_MEM_IF_WRITE_DQS_WIDTH; write_group++,
3357 write_test_bgn += RW_MGR_MEM_DQ_PER_WRITE_DQS) {
3358 /* Initialized the group failure */
3361 current_run = run_groups & ((1 <<
3362 RW_MGR_NUM_DQS_PER_WRITE_GROUP) - 1);
3363 run_groups = run_groups >>
3364 RW_MGR_NUM_DQS_PER_WRITE_GROUP;
3366 if (current_run == 0)
3369 writel(write_group, SDR_PHYGRP_SCCGRP_ADDRESS |
3370 SCC_MGR_GROUP_COUNTER_OFFSET);
3371 scc_mgr_zero_group(write_group, write_test_bgn,
3374 for (read_group = write_group *
3375 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3376 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3378 read_group < (write_group + 1) *
3379 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3380 RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3382 read_group++, read_test_bgn +=
3383 RW_MGR_MEM_DQ_PER_READ_DQS) {
3384 /* Calibrate the VFIFO */
3385 if (!((STATIC_CALIB_STEPS) &
3386 CALIB_SKIP_VFIFO)) {
3387 if (!rw_mgr_mem_calibrate_vfifo
3393 phy_debug_mode_flags &
3394 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3401 /* Calibrate the output side */
3402 if (group_failed == 0) {
3403 for (rank_bgn = 0, sr = 0; rank_bgn
3404 < RW_MGR_MEM_NUMBER_OF_RANKS;
3406 NUM_RANKS_PER_SHADOW_REG,
3409 if (!((STATIC_CALIB_STEPS) &
3410 CALIB_SKIP_WRITES)) {
3411 if ((STATIC_CALIB_STEPS)
3412 & CALIB_SKIP_DELAY_SWEEPS) {
3413 /* not needed in quick mode! */
3416 * Determine if this set of
3417 * ranks should be skipped
3420 if (!param->skip_shadow_regs[sr]) {
3421 if (!rw_mgr_mem_calibrate_writes
3422 (rank_bgn, write_group,
3426 phy_debug_mode_flags &
3427 PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3439 if (group_failed == 0) {
3440 for (read_group = write_group *
3441 RW_MGR_MEM_IF_READ_DQS_WIDTH /
3442 RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3444 read_group < (write_group + 1)
3445 * RW_MGR_MEM_IF_READ_DQS_WIDTH
3446 / RW_MGR_MEM_IF_WRITE_DQS_WIDTH &&
3448 read_group++, read_test_bgn +=
3449 RW_MGR_MEM_DQ_PER_READ_DQS) {
3450 if (!((STATIC_CALIB_STEPS) &
3451 CALIB_SKIP_WRITES)) {
3452 if (!rw_mgr_mem_calibrate_vfifo_end
3453 (read_group, read_test_bgn)) {
3456 if (!(gbl->phy_debug_mode_flags
3457 & PHY_DEBUG_SWEEP_ALL_GROUPS)) {
3465 if (group_failed != 0)
3470 * USER If there are any failing groups then report
3473 if (failing_groups != 0)
3476 /* Calibrate the LFIFO */
3477 if (!((STATIC_CALIB_STEPS) & CALIB_SKIP_LFIFO)) {
3479 * If we're skipping groups as part of debug,
3480 * don't calibrate LFIFO.
3482 if (param->skip_groups == 0) {
3483 if (!rw_mgr_mem_calibrate_lfifo())
3491 * Do not remove this line as it makes sure all of our decisions
3492 * have been applied.
3494 writel(0, &sdr_scc_mgr->update);
3498 static uint32_t run_mem_calibrate(void)
3501 uint32_t debug_info;
3503 debug("%s:%d\n", __func__, __LINE__);
3505 /* Reset pass/fail status shown on afi_cal_success/fail */
3506 writel(PHY_MGR_CAL_RESET, &phy_mgr_cfg->cal_status);
3508 /* stop tracking manger */
3509 uint32_t ctrlcfg = readl(&sdr_ctrl->ctrl_cfg);
3511 writel(ctrlcfg & 0xFFBFFFFF, &sdr_ctrl->ctrl_cfg);
3514 rw_mgr_mem_initialize();
3516 pass = mem_calibrate();
3518 mem_precharge_and_activate();
3519 writel(0, &phy_mgr_cmd->fifo_reset);
3523 * Don't return control of the PHY back to AFI when in debug mode.
3525 if ((gbl->phy_debug_mode_flags & PHY_DEBUG_IN_DEBUG_MODE) == 0) {
3526 rw_mgr_mem_handoff();
3528 * In Hard PHY this is a 2-bit control:
3530 * 1: DDIO Mux Select
3532 writel(0x2, &phy_mgr_cfg->mux_sel);
3535 writel(ctrlcfg, &sdr_ctrl->ctrl_cfg);
3538 printf("%s: CALIBRATION PASSED\n", __FILE__);
3543 if (gbl->fom_in > 0xff)
3546 if (gbl->fom_out > 0xff)
3547 gbl->fom_out = 0xff;
3549 /* Update the FOM in the register file */
3550 debug_info = gbl->fom_in;
3551 debug_info |= gbl->fom_out << 8;
3552 writel(debug_info, &sdr_reg_file->fom);
3554 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3555 writel(PHY_MGR_CAL_SUCCESS, &phy_mgr_cfg->cal_status);
3557 printf("%s: CALIBRATION FAILED\n", __FILE__);
3559 debug_info = gbl->error_stage;
3560 debug_info |= gbl->error_substage << 8;
3561 debug_info |= gbl->error_group << 16;
3563 writel(debug_info, &sdr_reg_file->failing_stage);
3564 writel(debug_info, &phy_mgr_cfg->cal_debug_info);
3565 writel(PHY_MGR_CAL_FAIL, &phy_mgr_cfg->cal_status);
3567 /* Update the failing group/stage in the register file */
3568 debug_info = gbl->error_stage;
3569 debug_info |= gbl->error_substage << 8;
3570 debug_info |= gbl->error_group << 16;
3571 writel(debug_info, &sdr_reg_file->failing_stage);
3578 * hc_initialize_rom_data() - Initialize ROM data
3580 * Initialize ROM data.
3582 static void hc_initialize_rom_data(void)
3586 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_INST_ROM_WRITE_OFFSET;
3587 for (i = 0; i < ARRAY_SIZE(inst_rom_init); i++)
3588 writel(inst_rom_init[i], addr + (i << 2));
3590 addr = SDR_PHYGRP_RWMGRGRP_ADDRESS | RW_MGR_AC_ROM_WRITE_OFFSET;
3591 for (i = 0; i < ARRAY_SIZE(ac_rom_init); i++)
3592 writel(ac_rom_init[i], addr + (i << 2));
3596 * initialize_reg_file() - Initialize SDR register file
3598 * Initialize SDR register file.
3600 static void initialize_reg_file(void)
3602 /* Initialize the register file with the correct data */
3603 writel(REG_FILE_INIT_SEQ_SIGNATURE, &sdr_reg_file->signature);
3604 writel(0, &sdr_reg_file->debug_data_addr);
3605 writel(0, &sdr_reg_file->cur_stage);
3606 writel(0, &sdr_reg_file->fom);
3607 writel(0, &sdr_reg_file->failing_stage);
3608 writel(0, &sdr_reg_file->debug1);
3609 writel(0, &sdr_reg_file->debug2);
3613 * initialize_hps_phy() - Initialize HPS PHY
3615 * Initialize HPS PHY.
3617 static void initialize_hps_phy(void)
3621 * Tracking also gets configured here because it's in the
3624 uint32_t trk_sample_count = 7500;
3625 uint32_t trk_long_idle_sample_count = (10 << 16) | 100;
3627 * Format is number of outer loops in the 16 MSB, sample
3632 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ACDELAYEN_SET(2);
3633 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQDELAYEN_SET(1);
3634 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSDELAYEN_SET(1);
3635 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_DQSLOGICDELAYEN_SET(1);
3636 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_RESETDELAYEN_SET(0);
3637 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_LPDDRDIS_SET(1);
3639 * This field selects the intrinsic latency to RDATA_EN/FULL path.
3640 * 00-bypass, 01- add 5 cycles, 10- add 10 cycles, 11- add 15 cycles.
3642 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_ADDLATSEL_SET(0);
3643 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_SET(
3645 writel(reg, &sdr_ctrl->phy_ctrl0);
3648 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_SAMPLECOUNT_31_20_SET(
3650 SDR_CTRLGRP_PHYCTRL_PHYCTRL_0_SAMPLECOUNT_19_0_WIDTH);
3651 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_SET(
3652 trk_long_idle_sample_count);
3653 writel(reg, &sdr_ctrl->phy_ctrl1);
3656 reg |= SDR_CTRLGRP_PHYCTRL_PHYCTRL_2_LONGIDLESAMPLECOUNT_31_20_SET(
3657 trk_long_idle_sample_count >>
3658 SDR_CTRLGRP_PHYCTRL_PHYCTRL_1_LONGIDLESAMPLECOUNT_19_0_WIDTH);
3659 writel(reg, &sdr_ctrl->phy_ctrl2);
3662 static void initialize_tracking(void)
3664 uint32_t concatenated_longidle = 0x0;
3665 uint32_t concatenated_delays = 0x0;
3666 uint32_t concatenated_rw_addr = 0x0;
3667 uint32_t concatenated_refresh = 0x0;
3668 uint32_t trk_sample_count = 7500;
3669 uint32_t dtaps_per_ptap;
3673 * compute usable version of value in case we skip full
3678 while (tmp_delay < IO_DELAY_PER_OPA_TAP) {
3680 tmp_delay += IO_DELAY_PER_DCHAIN_TAP;
3684 concatenated_longidle = concatenated_longidle ^ 10;
3685 /*longidle outer loop */
3686 concatenated_longidle = concatenated_longidle << 16;
3687 concatenated_longidle = concatenated_longidle ^ 100;
3688 /*longidle sample count */
3689 concatenated_delays = concatenated_delays ^ 243;
3690 /* trfc, worst case of 933Mhz 4Gb */
3691 concatenated_delays = concatenated_delays << 8;
3692 concatenated_delays = concatenated_delays ^ 14;
3693 /* trcd, worst case */
3694 concatenated_delays = concatenated_delays << 8;
3695 concatenated_delays = concatenated_delays ^ 10;
3697 concatenated_delays = concatenated_delays << 8;
3698 concatenated_delays = concatenated_delays ^ 4;
3701 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_IDLE;
3702 concatenated_rw_addr = concatenated_rw_addr << 8;
3703 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_ACTIVATE_1;
3704 concatenated_rw_addr = concatenated_rw_addr << 8;
3705 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_SGLE_READ;
3706 concatenated_rw_addr = concatenated_rw_addr << 8;
3707 concatenated_rw_addr = concatenated_rw_addr ^ RW_MGR_PRECHARGE_ALL;
3709 concatenated_refresh = concatenated_refresh ^ RW_MGR_REFRESH_ALL;
3710 concatenated_refresh = concatenated_refresh << 24;
3711 concatenated_refresh = concatenated_refresh ^ 1000; /* trefi */
3713 /* Initialize the register file with the correct data */
3714 writel(dtaps_per_ptap, &sdr_reg_file->dtaps_per_ptap);
3715 writel(trk_sample_count, &sdr_reg_file->trk_sample_count);
3716 writel(concatenated_longidle, &sdr_reg_file->trk_longidle);
3717 writel(concatenated_delays, &sdr_reg_file->delays);
3718 writel(concatenated_rw_addr, &sdr_reg_file->trk_rw_mgr_addr);
3719 writel(RW_MGR_MEM_IF_READ_DQS_WIDTH, &sdr_reg_file->trk_read_dqs_width);
3720 writel(concatenated_refresh, &sdr_reg_file->trk_rfsh);
3723 int sdram_calibration_full(void)
3725 struct param_type my_param;
3726 struct gbl_type my_gbl;
3733 /* Initialize the debug mode flags */
3734 gbl->phy_debug_mode_flags = 0;
3735 /* Set the calibration enabled by default */
3736 gbl->phy_debug_mode_flags |= PHY_DEBUG_ENABLE_CAL_RPT;
3738 * Only sweep all groups (regardless of fail state) by default
3739 * Set enabled read test by default.
3741 #if DISABLE_GUARANTEED_READ
3742 gbl->phy_debug_mode_flags |= PHY_DEBUG_DISABLE_GUARANTEED_READ;
3744 /* Initialize the register file */
3745 initialize_reg_file();
3747 /* Initialize any PHY CSR */
3748 initialize_hps_phy();
3750 scc_mgr_initialize();
3752 initialize_tracking();
3754 /* USER Enable all ranks, groups */
3755 for (i = 0; i < RW_MGR_MEM_NUMBER_OF_RANKS; i++)
3756 param->skip_ranks[i] = 0;
3757 for (i = 0; i < NUM_SHADOW_REGS; ++i)
3758 param->skip_shadow_regs[i] = 0;
3759 param->skip_groups = 0;
3761 printf("%s: Preparing to start memory calibration\n", __FILE__);
3763 debug("%s:%d\n", __func__, __LINE__);
3764 debug_cond(DLEVEL == 1,
3765 "DDR3 FULL_RATE ranks=%u cs/dimm=%u dq/dqs=%u,%u vg/dqs=%u,%u ",
3766 RW_MGR_MEM_NUMBER_OF_RANKS, RW_MGR_MEM_NUMBER_OF_CS_PER_DIMM,
3767 RW_MGR_MEM_DQ_PER_READ_DQS, RW_MGR_MEM_DQ_PER_WRITE_DQS,
3768 RW_MGR_MEM_VIRTUAL_GROUPS_PER_READ_DQS,
3769 RW_MGR_MEM_VIRTUAL_GROUPS_PER_WRITE_DQS);
3770 debug_cond(DLEVEL == 1,
3771 "dqs=%u,%u dq=%u dm=%u ptap_delay=%u dtap_delay=%u ",
3772 RW_MGR_MEM_IF_READ_DQS_WIDTH, RW_MGR_MEM_IF_WRITE_DQS_WIDTH,
3773 RW_MGR_MEM_DATA_WIDTH, RW_MGR_MEM_DATA_MASK_WIDTH,
3774 IO_DELAY_PER_OPA_TAP, IO_DELAY_PER_DCHAIN_TAP);
3775 debug_cond(DLEVEL == 1, "dtap_dqsen_delay=%u, dll=%u",
3776 IO_DELAY_PER_DQS_EN_DCHAIN_TAP, IO_DLL_CHAIN_LENGTH);
3777 debug_cond(DLEVEL == 1, "max values: en_p=%u dqdqs_p=%u en_d=%u dqs_in_d=%u ",
3778 IO_DQS_EN_PHASE_MAX, IO_DQDQS_OUT_PHASE_MAX,
3779 IO_DQS_EN_DELAY_MAX, IO_DQS_IN_DELAY_MAX);
3780 debug_cond(DLEVEL == 1, "io_in_d=%u io_out1_d=%u io_out2_d=%u ",
3781 IO_IO_IN_DELAY_MAX, IO_IO_OUT1_DELAY_MAX,
3782 IO_IO_OUT2_DELAY_MAX);
3783 debug_cond(DLEVEL == 1, "dqs_in_reserve=%u dqs_out_reserve=%u\n",
3784 IO_DQS_IN_RESERVE, IO_DQS_OUT_RESERVE);
3786 hc_initialize_rom_data();
3788 /* update info for sims */
3789 reg_file_set_stage(CAL_STAGE_NIL);
3790 reg_file_set_group(0);
3793 * Load global needed for those actions that require
3794 * some dynamic calibration support.
3796 dyn_calib_steps = STATIC_CALIB_STEPS;
3798 * Load global to allow dynamic selection of delay loop settings
3799 * based on calibration mode.
3801 if (!(dyn_calib_steps & CALIB_SKIP_DELAY_LOOPS))
3802 skip_delay_mask = 0xff;
3804 skip_delay_mask = 0x0;
3806 pass = run_mem_calibrate();
3808 printf("%s: Calibration complete\n", __FILE__);