2 * Copyright (C) Marvell International Ltd. and its affiliates
4 * SPDX-License-Identifier: GPL-2.0
11 #include <asm/arch/cpu.h>
12 #include <asm/arch/soc.h>
14 #include "ddr3_hw_training.h"
19 #define DEBUG_RL_C(s, d, l) \
20 DEBUG_RL_S(s); DEBUG_RL_D(d, l); DEBUG_RL_S("\n")
21 #define DEBUG_RL_FULL_C(s, d, l) \
22 DEBUG_RL_FULL_S(s); DEBUG_RL_FULL_D(d, l); DEBUG_RL_FULL_S("\n")
25 #define DEBUG_RL_S(s) \
26 debug_cond(ddr3_get_log_level() >= MV_LOG_LEVEL_2, "%s", s)
27 #define DEBUG_RL_D(d, l) \
28 debug_cond(ddr3_get_log_level() >= MV_LOG_LEVEL_2, "%x", d)
31 #define DEBUG_RL_D(d, l)
34 #ifdef MV_DEBUG_RL_FULL
35 #define DEBUG_RL_FULL_S(s) puts(s)
36 #define DEBUG_RL_FULL_D(d, l) printf("%x", d)
38 #define DEBUG_RL_FULL_S(s)
39 #define DEBUG_RL_FULL_D(d, l)
42 extern u32 rl_pattern[LEN_STD_PATTERN];
45 static int ddr3_read_leveling_single_cs_rl_mode(u32 cs, u32 freq,
46 int ratio_2to1, u32 ecc,
47 MV_DRAM_INFO *dram_info);
49 static int ddr3_read_leveling_single_cs_window_mode(u32 cs, u32 freq,
50 int ratio_2to1, u32 ecc,
51 MV_DRAM_INFO *dram_info);
55 * Name: ddr3_read_leveling_hw
56 * Desc: Execute the Read leveling phase by HW
57 * Args: dram_info - main struct
58 * freq - current sequence frequency
60 * Returns: MV_OK if success, MV_FAIL if fail.
62 int ddr3_read_leveling_hw(u32 freq, MV_DRAM_INFO *dram_info)
66 /* Debug message - Start Read leveling procedure */
67 DEBUG_RL_S("DDR3 - Read Leveling - Starting HW RL procedure\n");
69 /* Start Auto Read Leveling procedure */
70 reg = 1 << REG_DRAM_TRAINING_RL_OFFS;
71 /* Config the retest number */
72 reg |= (COUNT_HW_RL << REG_DRAM_TRAINING_RETEST_OFFS);
74 /* Enable CS in the automatic process */
75 reg |= (dram_info->cs_ena << REG_DRAM_TRAINING_CS_OFFS);
77 reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
79 reg = reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) |
80 (1 << REG_DRAM_TRAINING_AUTO_OFFS);
81 reg_write(REG_DRAM_TRAINING_SHADOW_ADDR, reg);
85 reg = reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) &
86 (1 << REG_DRAM_TRAINING_AUTO_OFFS);
87 } while (reg); /* Wait for '0' */
89 /* Check if Successful */
90 if (reg_read(REG_DRAM_TRAINING_SHADOW_ADDR) &
91 (1 << REG_DRAM_TRAINING_ERROR_OFFS)) {
92 u32 delay, phase, pup, cs;
94 dram_info->rl_max_phase = 0;
95 dram_info->rl_min_phase = 10;
97 /* Read results to arrays */
98 for (cs = 0; cs < MAX_CS; cs++) {
99 if (dram_info->cs_ena & (1 << cs)) {
101 pup < dram_info->num_of_total_pups;
103 if (pup == dram_info->num_of_std_pups
104 && dram_info->ecc_ena)
107 ddr3_read_pup_reg(PUP_RL_MODE, cs,
109 phase = (reg >> REG_PHY_PHASE_OFFS) &
111 delay = reg & PUP_DELAY_MASK;
112 dram_info->rl_val[cs][pup][P] = phase;
113 if (phase > dram_info->rl_max_phase)
114 dram_info->rl_max_phase = phase;
115 if (phase < dram_info->rl_min_phase)
116 dram_info->rl_min_phase = phase;
117 dram_info->rl_val[cs][pup][D] = delay;
118 dram_info->rl_val[cs][pup][S] =
121 ddr3_read_pup_reg(PUP_RL_MODE + 0x1,
123 dram_info->rl_val[cs][pup][DQS] =
128 DEBUG_RL_C("DDR3 - Read Leveling - Results for CS - ",
132 pup < (dram_info->num_of_total_pups);
134 if (pup == dram_info->num_of_std_pups
135 && dram_info->ecc_ena)
137 DEBUG_RL_S("DDR3 - Read Leveling - PUP: ");
138 DEBUG_RL_D((u32) pup, 1);
139 DEBUG_RL_S(", Phase: ");
140 DEBUG_RL_D((u32) dram_info->
141 rl_val[cs][pup][P], 1);
142 DEBUG_RL_S(", Delay: ");
143 DEBUG_RL_D((u32) dram_info->
144 rl_val[cs][pup][D], 2);
151 dram_info->rd_rdy_dly =
152 reg_read(REG_READ_DATA_READY_DELAYS_ADDR) &
153 REG_READ_DATA_SAMPLE_DELAYS_MASK;
154 dram_info->rd_smpl_dly =
155 reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR) &
156 REG_READ_DATA_READY_DELAYS_MASK;
158 DEBUG_RL_C("DDR3 - Read Leveling - Read Sample Delay: ",
159 dram_info->rd_smpl_dly, 2);
160 DEBUG_RL_C("DDR3 - Read Leveling - Read Ready Delay: ",
161 dram_info->rd_rdy_dly, 2);
162 DEBUG_RL_S("DDR3 - Read Leveling - HW RL Ended Successfully\n");
167 DEBUG_RL_S("DDR3 - Read Leveling - HW RL Error\n");
173 * Name: ddr3_read_leveling_sw
174 * Desc: Execute the Read leveling phase by SW
175 * Args: dram_info - main struct
176 * freq - current sequence frequency
178 * Returns: MV_OK if success, MV_FAIL if fail.
180 int ddr3_read_leveling_sw(u32 freq, int ratio_2to1, MV_DRAM_INFO *dram_info)
182 u32 reg, cs, ecc, pup_num, phase, delay, pup;
185 /* Debug message - Start Read leveling procedure */
186 DEBUG_RL_S("DDR3 - Read Leveling - Starting SW RL procedure\n");
188 /* Enable SW Read Leveling */
189 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
190 (1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
191 reg &= ~(1 << REG_DRAM_TRAINING_2_RL_MODE_OFFS);
192 /* [0]=1 - Enable SW override */
193 /* 0x15B8 - Training SW 2 Register */
194 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
197 reg = (dram_info->cs_ena << REG_DRAM_TRAINING_CS_OFFS) |
198 (1 << REG_DRAM_TRAINING_AUTO_OFFS);
199 reg_write(REG_DRAM_TRAINING_ADDR, reg); /* 0x15B0 - Training Register */
202 /* Loop for each CS */
203 for (cs = 0; cs < dram_info->num_cs; cs++) {
204 DEBUG_RL_C("DDR3 - Read Leveling - CS - ", (u32) cs, 1);
206 for (ecc = 0; ecc <= (dram_info->ecc_ena); ecc++) {
207 /* ECC Support - Switch ECC Mux on ecc=1 */
208 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
209 ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
210 reg |= (dram_info->ecc_ena *
211 ecc << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
212 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
215 DEBUG_RL_S("DDR3 - Read Leveling - ECC Mux Enabled\n");
217 DEBUG_RL_S("DDR3 - Read Leveling - ECC Mux Disabled\n");
219 /* Set current sample delays */
220 reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
221 reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK <<
222 (REG_READ_DATA_SAMPLE_DELAYS_OFFS * cs));
223 reg |= (dram_info->cl <<
224 (REG_READ_DATA_SAMPLE_DELAYS_OFFS * cs));
225 reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR, reg);
227 /* Set current Ready delay */
228 reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
229 reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
230 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
233 reg |= ((dram_info->cl + 1) <<
234 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
237 reg |= ((dram_info->cl + 2) <<
238 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
240 reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
242 /* Read leveling Single CS[cs] */
245 ddr3_read_leveling_single_cs_rl_mode(cs, freq,
253 ddr3_read_leveling_single_cs_window_mode(cs, freq,
263 DEBUG_RL_C("DDR3 - Read Leveling - Results for CS - ", (u32) cs,
267 pup < (dram_info->num_of_std_pups + dram_info->ecc_ena);
269 DEBUG_RL_S("DDR3 - Read Leveling - PUP: ");
270 DEBUG_RL_D((u32) pup, 1);
271 DEBUG_RL_S(", Phase: ");
272 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][P], 1);
273 DEBUG_RL_S(", Delay: ");
274 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][D], 2);
278 DEBUG_RL_C("DDR3 - Read Leveling - Read Sample Delay: ",
279 dram_info->rd_smpl_dly, 2);
280 DEBUG_RL_C("DDR3 - Read Leveling - Read Ready Delay: ",
281 dram_info->rd_rdy_dly, 2);
283 /* Configure PHY with average of 3 locked leveling settings */
285 pup < (dram_info->num_of_std_pups + dram_info->ecc_ena);
287 /* ECC support - bit 8 */
288 pup_num = (pup == dram_info->num_of_std_pups) ? ECC_BIT : pup;
290 /* For now, set last cnt result */
291 phase = dram_info->rl_val[cs][pup][P];
292 delay = dram_info->rl_val[cs][pup][D];
293 ddr3_write_pup_reg(PUP_RL_MODE, cs, pup_num, phase,
298 /* Reset PHY read FIFO */
299 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
300 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
301 /* 0x15B8 - Training SW 2 Register */
302 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
305 reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
306 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
307 } while (reg); /* Wait for '0' */
309 /* ECC Support - Switch ECC Mux off ecc=0 */
310 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
311 ~(1 << REG_DRAM_TRAINING_2_ECC_MUX_OFFS);
312 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
315 reg_write(REG_DRAM_TRAINING_ADDR, 0); /* 0x15B0 - Training Register */
318 /* Disable SW Read Leveling */
319 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) &
320 ~(1 << REG_DRAM_TRAINING_2_SW_OVRD_OFFS);
321 /* [0] = 0 - Disable SW override */
322 reg = (reg | (0x1 << REG_DRAM_TRAINING_2_RL_MODE_OFFS));
323 /* [3] = 1 - Disable RL MODE */
324 /* 0x15B8 - Training SW 2 Register */
325 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
327 DEBUG_RL_S("DDR3 - Read Leveling - Finished RL procedure for all CS\n");
333 * overrun() extracted from ddr3_read_leveling_single_cs_rl_mode().
334 * This just got too much indented making it hard to read / edit.
336 static void overrun(u32 cs, MV_DRAM_INFO *info, u32 pup, u32 locked_pups,
337 u32 *locked_sum, u32 ecc, int *first_octet_locked,
338 int *counter_in_progress, int final_delay, u32 delay,
342 if (((~locked_pups >> pup) & 0x1) && (final_delay == 0)) {
345 idx = pup + ecc * ECC_BIT;
347 /* PUP passed, start examining */
348 if (info->rl_val[cs][idx][S] == RL_UNLOCK_STATE) {
349 /* Must be RL_UNLOCK_STATE */
350 /* Match expected value ? - Update State Machine */
351 if (info->rl_val[cs][idx][C] < RL_RETRY_COUNT) {
352 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have no overrun and a match on pup: ",
354 info->rl_val[cs][idx][C]++;
356 /* If pup got to last state - lock the delays */
357 if (info->rl_val[cs][idx][C] == RL_RETRY_COUNT) {
358 info->rl_val[cs][idx][C] = 0;
359 info->rl_val[cs][idx][DS] = delay;
360 info->rl_val[cs][idx][PS] = phase;
362 /* Go to Final State */
363 info->rl_val[cs][idx][S] = RL_FINAL_STATE;
364 *locked_sum = *locked_sum + 1;
365 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have locked pup: ",
369 * If first lock - need to lock delays
371 if (*first_octet_locked == 0) {
372 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got first lock on pup: ",
374 *first_octet_locked = 1;
378 * If pup is in not in final state but
379 * there was match - dont increment
383 *counter_in_progress = 1;
391 * Name: ddr3_read_leveling_single_cs_rl_mode
392 * Desc: Execute Read leveling for single Chip select
393 * Args: cs - current chip select
394 * freq - current sequence frequency
395 * ecc - ecc iteration indication
396 * dram_info - main struct
398 * Returns: MV_OK if success, MV_FAIL if fail.
400 static int ddr3_read_leveling_single_cs_rl_mode(u32 cs, u32 freq,
401 int ratio_2to1, u32 ecc,
402 MV_DRAM_INFO *dram_info)
404 u32 reg, delay, phase, pup, rd_sample_delay, add, locked_pups,
405 repeat_max_cnt, sdram_offset, locked_sum;
406 u32 phase_min, ui_max_delay;
407 int all_locked, first_octet_locked, counter_in_progress;
410 DEBUG_RL_FULL_C("DDR3 - Read Leveling - Single CS - ", (u32) cs, 1);
415 rd_sample_delay = dram_info->cl;
417 first_octet_locked = 0;
421 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
423 dram_info->rl_val[cs][pup + ecc * ECC_BIT][S] = 0;
426 while (!all_locked) {
427 counter_in_progress = 0;
429 DEBUG_RL_FULL_S("DDR3 - Read Leveling - RdSmplDly = ");
430 DEBUG_RL_FULL_D(rd_sample_delay, 2);
431 DEBUG_RL_FULL_S(", RdRdyDly = ");
432 DEBUG_RL_FULL_D(dram_info->rd_rdy_dly, 2);
433 DEBUG_RL_FULL_S(", Phase = ");
434 DEBUG_RL_FULL_D(phase, 1);
435 DEBUG_RL_FULL_S(", Delay = ");
436 DEBUG_RL_FULL_D(delay, 2);
437 DEBUG_RL_FULL_S("\n");
440 * Broadcast to all PUPs current RL delays: DQS phase,
443 ddr3_write_pup_reg(PUP_RL_MODE, cs, PUP_BC, phase, delay);
445 /* Reset PHY read FIFO */
446 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
447 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
448 /* 0x15B8 - Training SW 2 Register */
449 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
452 reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
453 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
454 } while (reg); /* Wait for '0' */
456 /* Read pattern from SDRAM */
457 sdram_offset = cs * (SDRAM_CS_SIZE + 1) + SDRAM_RL_OFFS;
460 ddr3_sdram_compare(dram_info, 0xFF, &locked_pups,
461 rl_pattern, LEN_STD_PATTERN,
462 sdram_offset, 0, 0, NULL, 0))
463 return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PATTERN;
465 /* Octet evaluation */
466 /* pup_num = Q or 1 for ECC */
467 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
469 if (!((reg_read(REG_DRAM_TRAINING_2_ADDR) >>
470 (REG_DRAM_TRAINING_2_OVERRUN_OFFS + pup)) & 0x1)) {
471 overrun(cs, dram_info, pup, locked_pups,
472 &locked_sum, ecc, &first_octet_locked,
473 &counter_in_progress, final_delay,
476 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got overrun on pup: ",
481 if (locked_sum == (dram_info->num_of_std_pups *
484 DEBUG_RL_FULL_S("DDR3 - Read Leveling - Single Cs - All pups locked\n");
488 * This is a fix for unstable condition where pups are
489 * toggling between match and no match
492 * If some of the pups is >1 <3, check if we did it too
495 if (counter_in_progress == 1) {
496 /* Notify at least one Counter is >=1 and < 3 */
497 if (repeat_max_cnt < RL_RETRY_COUNT) {
499 counter_in_progress = 1;
500 DEBUG_RL_FULL_S("DDR3 - Read Leveling - Counter is >=1 and <3\n");
501 DEBUG_RL_FULL_S("DDR3 - Read Leveling - So we will not increment the delay to see if locked again\n");
503 DEBUG_RL_FULL_S("DDR3 - Read Leveling - repeat_max_cnt reached max so now we will increment the delay\n");
504 counter_in_progress = 0;
509 * Check some of the pups are in the middle of state machine
510 * and don't increment the delays
512 if (!counter_in_progress && !all_locked) {
515 idx = pup + ecc * ECC_BIT;
519 if ((!ratio_2to1) && ((phase == 0) || (phase == 4)))
520 ui_max_delay = MAX_DELAY_INV;
522 ui_max_delay = MAX_DELAY;
524 /* Increment Delay */
525 if (delay < ui_max_delay) {
528 * Mark the last delay/pahse place for
531 if (delay == ui_max_delay) {
532 if ((!ratio_2to1 && phase ==
534 || (ratio_2to1 && phase ==
535 MAX_PHASE_RL_L_2TO1))
539 /* Phase+CL Incrementation */
544 if (first_octet_locked) {
545 /* some Pup was Locked */
546 if (phase < MAX_PHASE_RL_L_1TO1) {
551 delay = MIN_DELAY_PHASE_1_LIMIT;
554 DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
555 DEBUG_RL_S("1)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked n");
556 return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PUP_UNLOCK;
559 /* NO Pup was Locked */
560 if (phase < MAX_PHASE_RL_UL_1TO1) {
563 MIN_DELAY_PHASE_1_LIMIT;
570 if (first_octet_locked) {
571 /* some Pup was Locked */
572 if (phase < MAX_PHASE_RL_L_2TO1) {
575 DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
576 DEBUG_RL_S("2)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
577 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
578 /* pup_num = Q or 1 for ECC */
579 if (dram_info->rl_val[cs][idx][S]
581 DEBUG_RL_C("Failed byte is = ",
585 return MV_DDR3_TRAINING_ERR_RD_LVL_RL_PUP_UNLOCK;
588 /* No Pup was Locked */
589 if (phase < MAX_PHASE_RL_UL_2TO1)
597 * If we finished a full Phases cycle (so now
598 * phase = 0, need to increment rd_sample_dly
600 if (phase == 0 && first_octet_locked == 0) {
602 if (rd_sample_delay == 0x10) {
603 DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
604 DEBUG_RL_S("3)DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
605 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc); pup++) {
606 /* pup_num = Q or 1 for ECC */
608 rl_val[cs][idx][S] == 0) {
609 DEBUG_RL_C("Failed byte is = ",
613 return MV_DDR3_TRAINING_ERR_RD_LVL_PUP_UNLOCK;
616 /* Set current rd_sample_delay */
617 reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
618 reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK
619 << (REG_READ_DATA_SAMPLE_DELAYS_OFFS
621 reg |= (rd_sample_delay <<
622 (REG_READ_DATA_SAMPLE_DELAYS_OFFS *
624 reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR,
629 * Set current rdReadyDelay according to the
630 * hash table (Need to do this in every phase
635 add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
639 REG_TRAINING_DEBUG_2_OFFS);
643 (REG_TRAINING_DEBUG_2_OFFS
648 (REG_TRAINING_DEBUG_2_OFFS
653 (REG_TRAINING_DEBUG_2_OFFS
657 add &= REG_TRAINING_DEBUG_2_MASK;
660 add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
663 REG_TRAINING_DEBUG_3_OFFS));
664 add &= REG_TRAINING_DEBUG_3_MASK;
667 reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
668 reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
669 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
670 reg |= ((rd_sample_delay + add) <<
671 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
672 reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
673 dram_info->rd_smpl_dly = rd_sample_delay;
674 dram_info->rd_rdy_dly = rd_sample_delay + add;
677 /* Reset counters for pups with states<RD_STATE_COUNT */
679 (dram_info->num_of_std_pups * (1 - ecc) + ecc);
681 if (dram_info->rl_val[cs][idx][C] < RL_RETRY_COUNT)
682 dram_info->rl_val[cs][idx][C] = 0;
689 for (pup = 0; pup < (dram_info->num_of_std_pups); pup++) {
690 if (dram_info->rl_val[cs][pup][PS] < phase_min)
691 phase_min = dram_info->rl_val[cs][pup][PS];
695 * Set current rdReadyDelay according to the hash table (Need to
696 * do this in every phase change)
700 add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
703 add = (add >> REG_TRAINING_DEBUG_2_OFFS);
706 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 3));
709 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 6));
712 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 9));
715 add &= REG_TRAINING_DEBUG_2_MASK;
718 add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
719 add = (add >> (phase_min * REG_TRAINING_DEBUG_3_OFFS));
720 add &= REG_TRAINING_DEBUG_3_MASK;
723 reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
724 reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
725 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
726 reg |= ((rd_sample_delay + add) << (REG_READ_DATA_READY_DELAYS_OFFS * cs));
727 reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
728 dram_info->rd_rdy_dly = rd_sample_delay + add;
730 for (cs = 0; cs < dram_info->num_cs; cs++) {
731 for (pup = 0; pup < dram_info->num_of_total_pups; pup++) {
732 reg = ddr3_read_pup_reg(PUP_RL_MODE + 0x1, cs, pup);
733 dram_info->rl_val[cs][pup][DQS] = (reg & 0x3F);
743 * Name: ddr3_read_leveling_single_cs_window_mode
744 * Desc: Execute Read leveling for single Chip select
745 * Args: cs - current chip select
746 * freq - current sequence frequency
747 * ecc - ecc iteration indication
748 * dram_info - main struct
750 * Returns: MV_OK if success, MV_FAIL if fail.
752 static int ddr3_read_leveling_single_cs_window_mode(u32 cs, u32 freq,
753 int ratio_2to1, u32 ecc,
754 MV_DRAM_INFO *dram_info)
756 u32 reg, delay, phase, sum, pup, rd_sample_delay, add, locked_pups,
757 repeat_max_cnt, sdram_offset, final_sum, locked_sum;
758 u32 delay_s, delay_e, tmp, phase_min, ui_max_delay;
759 int all_locked, first_octet_locked, counter_in_progress;
762 DEBUG_RL_FULL_C("DDR3 - Read Leveling - Single CS - ", (u32) cs, 1);
767 rd_sample_delay = dram_info->cl;
769 first_octet_locked = 0;
775 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
777 dram_info->rl_val[cs][pup + ecc * ECC_BIT][S] = 0;
780 while (!all_locked) {
781 counter_in_progress = 0;
783 DEBUG_RL_FULL_S("DDR3 - Read Leveling - RdSmplDly = ");
784 DEBUG_RL_FULL_D(rd_sample_delay, 2);
785 DEBUG_RL_FULL_S(", RdRdyDly = ");
786 DEBUG_RL_FULL_D(dram_info->rd_rdy_dly, 2);
787 DEBUG_RL_FULL_S(", Phase = ");
788 DEBUG_RL_FULL_D(phase, 1);
789 DEBUG_RL_FULL_S(", Delay = ");
790 DEBUG_RL_FULL_D(delay, 2);
791 DEBUG_RL_FULL_S("\n");
794 * Broadcast to all PUPs current RL delays: DQS phase,leveling
797 ddr3_write_pup_reg(PUP_RL_MODE, cs, PUP_BC, phase, delay);
799 /* Reset PHY read FIFO */
800 reg = reg_read(REG_DRAM_TRAINING_2_ADDR) |
801 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
802 /* 0x15B8 - Training SW 2 Register */
803 reg_write(REG_DRAM_TRAINING_2_ADDR, reg);
806 reg = (reg_read(REG_DRAM_TRAINING_2_ADDR)) &
807 (1 << REG_DRAM_TRAINING_2_FIFO_RST_OFFS);
808 } while (reg); /* Wait for '0' */
810 /* Read pattern from SDRAM */
811 sdram_offset = cs * (SDRAM_CS_SIZE + 1) + SDRAM_RL_OFFS;
814 ddr3_sdram_compare(dram_info, 0xFF, &locked_pups,
815 rl_pattern, LEN_STD_PATTERN,
816 sdram_offset, 0, 0, NULL, 0))
817 return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PATTERN;
819 /* Octet evaluation */
820 for (pup = 0; pup < (dram_info->num_of_std_pups *
821 (1 - ecc) + ecc); pup++) {
822 /* pup_num = Q or 1 for ECC */
825 idx = pup + ecc * ECC_BIT;
828 if (!((reg_read(REG_DRAM_TRAINING_2_ADDR) >>
829 (REG_DRAM_TRAINING_2_OVERRUN_OFFS +
833 /* Inside the window */
834 if (dram_info->rl_val[cs][idx][S] == RL_WINDOW_STATE) {
836 * Match expected value ? - Update
839 if (((~locked_pups >> pup) & 0x1)
840 && (final_delay == 0)) {
841 /* Match - Still inside the Window */
842 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got another match inside the window for pup: ",
846 /* We got fail -> this is the end of the window */
847 dram_info->rl_val[cs][idx][DE] = delay;
848 dram_info->rl_val[cs][idx][PE] = phase;
849 /* Go to Final State */
850 dram_info->rl_val[cs][idx][S]++;
852 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We finished the window for pup: ",
856 /* Before the start of the window */
857 } else if (dram_info->rl_val[cs][idx][S] ==
859 /* Must be RL_UNLOCK_STATE */
861 * Match expected value ? - Update
864 if (dram_info->rl_val[cs][idx][C] <
866 if (((~locked_pups >> pup) & 0x1)) {
868 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We have no overrun and a match on pup: ",
870 dram_info->rl_val[cs][idx][C]++;
872 /* If pup got to last state - lock the delays */
873 if (dram_info->rl_val[cs][idx][C] ==
875 dram_info->rl_val[cs][idx][C] = 0;
876 dram_info->rl_val[cs][idx][DS] =
878 dram_info->rl_val[cs][idx][PS] =
880 dram_info->rl_val[cs][idx][S]++; /* Go to Window State */
882 /* Will count the pups that got locked */
884 /* IF First lock - need to lock delays */
885 if (first_octet_locked == 0) {
886 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got first lock on pup: ",
894 /* if pup is in not in final state but there was match - dont increment counter */
903 DEBUG_RL_FULL_C("DDR3 - Read Leveling - We got overrun on pup: ",
905 counter_in_progress = 1;
909 if (final_sum == (dram_info->num_of_std_pups * (1 - ecc) + ecc)) {
911 DEBUG_RL_FULL_S("DDR3 - Read Leveling - Single Cs - All pups locked\n");
915 * This is a fix for unstable condition where pups are
916 * toggling between match and no match
919 * If some of the pups is >1 <3, check if we did it too many
922 if (counter_in_progress == 1) {
923 if (repeat_max_cnt < RL_RETRY_COUNT) {
924 /* Notify at least one Counter is >=1 and < 3 */
926 counter_in_progress = 1;
927 DEBUG_RL_FULL_S("DDR3 - Read Leveling - Counter is >=1 and <3\n");
928 DEBUG_RL_FULL_S("DDR3 - Read Leveling - So we will not increment the delay to see if locked again\n");
930 DEBUG_RL_FULL_S("DDR3 - Read Leveling - repeat_max_cnt reached max so now we will increment the delay\n");
931 counter_in_progress = 0;
936 * Check some of the pups are in the middle of state machine
937 * and don't increment the delays
939 if (!counter_in_progress && !all_locked) {
942 ui_max_delay = MAX_DELAY_INV;
944 ui_max_delay = MAX_DELAY;
946 /* Increment Delay */
947 if (delay < ui_max_delay) {
948 /* Delay Incrementation */
950 if (delay == ui_max_delay) {
952 * Mark the last delay/pahse place
953 * for window final place
956 && phase == MAX_PHASE_RL_L_1TO1)
959 MAX_PHASE_RL_L_2TO1))
963 /* Phase+CL Incrementation */
967 if (first_octet_locked) {
968 /* some pupet was Locked */
969 if (phase < MAX_PHASE_RL_L_1TO1) {
979 DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
980 return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PUP_UNLOCK;
983 /* No Pup was Locked */
984 if (phase < MAX_PHASE_RL_UL_1TO1) {
996 if (first_octet_locked) {
997 /* Some Pup was Locked */
998 if (phase < MAX_PHASE_RL_L_2TO1) {
1001 DEBUG_RL_FULL_S("DDR3 - Read Leveling - ERROR - NOT all PUPs Locked\n");
1002 return MV_DDR3_TRAINING_ERR_RD_LVL_WIN_PUP_UNLOCK;
1005 /* No Pup was Locked */
1006 if (phase < MAX_PHASE_RL_UL_2TO1)
1014 * If we finished a full Phases cycle (so
1015 * now phase = 0, need to increment
1018 if (phase == 0 && first_octet_locked == 0) {
1021 /* Set current rd_sample_delay */
1022 reg = reg_read(REG_READ_DATA_SAMPLE_DELAYS_ADDR);
1023 reg &= ~(REG_READ_DATA_SAMPLE_DELAYS_MASK <<
1024 (REG_READ_DATA_SAMPLE_DELAYS_OFFS
1026 reg |= (rd_sample_delay <<
1027 (REG_READ_DATA_SAMPLE_DELAYS_OFFS *
1029 reg_write(REG_READ_DATA_SAMPLE_DELAYS_ADDR,
1034 * Set current rdReadyDelay according to the
1035 * hash table (Need to do this in every phase
1040 add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
1044 REG_TRAINING_DEBUG_2_OFFS;
1048 (REG_TRAINING_DEBUG_2_OFFS
1053 (REG_TRAINING_DEBUG_2_OFFS
1058 (REG_TRAINING_DEBUG_2_OFFS
1064 add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
1065 add = (add >> phase *
1066 REG_TRAINING_DEBUG_3_OFFS);
1068 add &= REG_TRAINING_DEBUG_2_MASK;
1069 reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
1070 reg &= ~(REG_READ_DATA_READY_DELAYS_MASK <<
1071 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
1072 reg |= ((rd_sample_delay + add) <<
1073 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
1074 reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
1075 dram_info->rd_smpl_dly = rd_sample_delay;
1076 dram_info->rd_rdy_dly = rd_sample_delay + add;
1079 /* Reset counters for pups with states<RD_STATE_COUNT */
1082 (dram_info->num_of_std_pups * (1 - ecc) + ecc);
1084 if (dram_info->rl_val[cs][idx][C] < RL_RETRY_COUNT)
1085 dram_info->rl_val[cs][idx][C] = 0;
1092 for (pup = 0; pup < (dram_info->num_of_std_pups); pup++) {
1093 DEBUG_RL_S("DDR3 - Read Leveling - Window info - PUP: ");
1094 DEBUG_RL_D((u32) pup, 1);
1095 DEBUG_RL_S(", PS: ");
1096 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][PS], 1);
1097 DEBUG_RL_S(", DS: ");
1098 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][DS], 2);
1099 DEBUG_RL_S(", PE: ");
1100 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][PE], 1);
1101 DEBUG_RL_S(", DE: ");
1102 DEBUG_RL_D((u32) dram_info->rl_val[cs][pup][DE], 2);
1106 /* Find center of the window procedure */
1107 for (pup = 0; pup < (dram_info->num_of_std_pups * (1 - ecc) + ecc);
1110 if (!ratio_2to1) { /* 1:1 mode */
1111 if (dram_info->rl_val[cs][idx][PS] == 4)
1112 dram_info->rl_val[cs][idx][PS] = 1;
1113 if (dram_info->rl_val[cs][idx][PE] == 4)
1114 dram_info->rl_val[cs][idx][PE] = 1;
1116 delay_s = dram_info->rl_val[cs][idx][PS] *
1117 MAX_DELAY_INV + dram_info->rl_val[cs][idx][DS];
1118 delay_e = dram_info->rl_val[cs][idx][PE] *
1119 MAX_DELAY_INV + dram_info->rl_val[cs][idx][DE];
1121 tmp = (delay_e - delay_s) / 2 + delay_s;
1122 phase = tmp / MAX_DELAY_INV;
1123 if (phase == 1) /* 1:1 mode */
1126 if (phase < phase_min) /* for the read ready delay */
1129 dram_info->rl_val[cs][idx][P] = phase;
1130 dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY_INV;
1133 delay_s = dram_info->rl_val[cs][idx][PS] *
1134 MAX_DELAY + dram_info->rl_val[cs][idx][DS];
1135 delay_e = dram_info->rl_val[cs][idx][PE] *
1136 MAX_DELAY + dram_info->rl_val[cs][idx][DE];
1138 tmp = (delay_e - delay_s) / 2 + delay_s;
1139 phase = tmp / MAX_DELAY;
1141 if (phase < phase_min) /* for the read ready delay */
1144 dram_info->rl_val[cs][idx][P] = phase;
1145 dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY;
1148 if (!ratio_2to1) { /* 1:1 mode */
1149 if (dram_info->rl_val[cs][idx][PS] > 1)
1150 dram_info->rl_val[cs][idx][PS] -= 2;
1151 if (dram_info->rl_val[cs][idx][PE] > 1)
1152 dram_info->rl_val[cs][idx][PE] -= 2;
1155 delay_s = dram_info->rl_val[cs][idx][PS] * MAX_DELAY +
1156 dram_info->rl_val[cs][idx][DS];
1157 delay_e = dram_info->rl_val[cs][idx][PE] * MAX_DELAY +
1158 dram_info->rl_val[cs][idx][DE];
1160 tmp = (delay_e - delay_s) / 2 + delay_s;
1161 phase = tmp / MAX_DELAY;
1162 if (!ratio_2to1 && phase > 1) /* 1:1 mode */
1165 if (phase < phase_min) /* for the read ready delay */
1168 dram_info->rl_val[cs][idx][P] = phase;
1169 dram_info->rl_val[cs][idx][D] = tmp % MAX_DELAY;
1173 /* Set current rdReadyDelay according to the hash table (Need to do this in every phase change) */
1174 if (!ratio_2to1) { /* 1:1 mode */
1175 add = reg_read(REG_TRAINING_DEBUG_2_ADDR);
1176 switch (phase_min) {
1178 add = (add >> REG_TRAINING_DEBUG_2_OFFS);
1181 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 3));
1184 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 6));
1187 add = (add >> (REG_TRAINING_DEBUG_2_OFFS + 9));
1190 } else { /* 2:1 mode */
1191 add = reg_read(REG_TRAINING_DEBUG_3_ADDR);
1192 add = (add >> phase_min * REG_TRAINING_DEBUG_3_OFFS);
1195 add &= REG_TRAINING_DEBUG_2_MASK;
1196 reg = reg_read(REG_READ_DATA_READY_DELAYS_ADDR);
1198 ~(REG_READ_DATA_READY_DELAYS_MASK <<
1199 (REG_READ_DATA_READY_DELAYS_OFFS * cs));
1201 ((rd_sample_delay + add) << (REG_READ_DATA_READY_DELAYS_OFFS * cs));
1202 reg_write(REG_READ_DATA_READY_DELAYS_ADDR, reg);
1203 dram_info->rd_rdy_dly = rd_sample_delay + add;
1205 for (cs = 0; cs < dram_info->num_cs; cs++) {
1206 for (pup = 0; pup < dram_info->num_of_total_pups; pup++) {
1207 reg = ddr3_read_pup_reg(PUP_RL_MODE + 0x1, cs, pup);
1208 dram_info->rl_val[cs][pup][DQS] = (reg & 0x3F);