2 * cpu/ppc4xx/4xx_ibm_ddr2_autocalib.c
3 * This SPD SDRAM detection code supports AMCC PPC44x cpu's with a
4 * DDR2 controller (non Denali Core). Those currently are:
7 * 440/460: 440SP/440SPe/460EX/460GT/460SX
9 * (C) Copyright 2008 Applied Micro Circuits Corporation
10 * Adam Graham <agraham@amcc.com>
12 * (C) Copyright 2007-2008
13 * Stefan Roese, DENX Software Engineering, sr@denx.de.
15 * COPYRIGHT AMCC CORPORATION 2004
17 * See file CREDITS for list of people who contributed to this
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 /* define DEBUG for debugging output (obviously ;-)) */
43 #include <asm/processor.h>
45 #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
48 * Only compile the DDR auto-calibration code for NOR boot and
49 * not for NAND boot (NAND SPL and NAND U-Boot - NUB)
51 #if !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)
54 #define SDRAM_RXBAS_SHIFT_1M 20
56 #if defined(CONFIG_SYS_DECREMENT_PATTERNS)
57 #define NUMMEMTESTS 24
60 #endif /* CONFIG_SYS_DECREMENT_PATTERNS */
61 #define NUMLOOPS 1 /* configure as you deem approporiate */
62 #define NUMMEMWORDS 16
64 /* Private Structure Definitions */
87 struct sdram_timing_clks
{
95 struct sdram_timing_clks clocks
;
96 struct ddrautocal autocal
;
99 /*--------------------------------------------------------------------------+
101 *--------------------------------------------------------------------------*/
102 #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
103 static u32
DQS_calibration_methodA(struct ddrautocal
*);
104 static u32
program_DQS_calibration_methodA(struct ddrautocal
*);
106 static u32
DQS_calibration_methodB(struct ddrautocal
*);
107 static u32
program_DQS_calibration_methodB(struct ddrautocal
*);
109 static int short_mem_test(u32
*);
112 * To provide an interface for board specific config values in this common
113 * DDR setup code, we implement he "weak" default functions here. They return
114 * the default value back to the caller.
116 * Please see include/configs/yucca.h for an example fora board specific
120 #if !defined(CONFIG_SPD_EEPROM)
121 u32
__ddr_wrdtr(u32 default_val
)
125 u32
ddr_wrdtr(u32
) __attribute__((weak
, alias("__ddr_wrdtr")));
127 u32
__ddr_clktr(u32 default_val
)
131 u32
ddr_clktr(u32
) __attribute__((weak
, alias("__ddr_clktr")));
134 * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
136 void __spd_ddr_init_hang(void)
141 spd_ddr_init_hang(void) __attribute__((weak
, alias("__spd_ddr_init_hang")));
142 #endif /* defined(CONFIG_SPD_EEPROM) */
144 ulong
__ddr_scan_option(ulong default_val
)
148 ulong
ddr_scan_option(ulong
) __attribute__((weak
, alias("__ddr_scan_option")));
150 static u32
*get_membase(int bxcr_num
)
155 #if defined(SDRAM_R0BAS)
156 /* BAS from Memory Queue rank reg. */
158 (u32
*)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS
+bxcr_num
)));
159 bxcf
= 0; /* just to satisfy the compiler */
161 /* BAS from SDRAM_MBxCF mem rank reg. */
162 mfsdram(SDRAM_MB0CF
+ (bxcr_num
<<2), bxcf
);
163 membase
= (u32
*)((bxcf
& 0xfff80000) << 3);
169 static inline void ecc_clear_status_reg(void)
171 mtsdram(SDRAM_ECCCR
, 0xffffffff);
172 #if defined(SDRAM_R0BAS)
173 mtdcr(SDRAM_ERRSTATLL
, 0xffffffff);
178 * Reset and relock memory DLL after SDRAM_CLKTR change
180 static inline void relock_memory_DLL(void)
184 mtsdram(SDRAM_MCOPT2
, SDRAM_MCOPT2_IPTR_EXECUTE
);
187 mfsdram(SDRAM_MCSTAT
, reg
);
188 } while (!(reg
& SDRAM_MCSTAT_MIC_COMP
));
190 mfsdram(SDRAM_MCOPT2
, reg
);
191 mtsdram(SDRAM_MCOPT2
, reg
| SDRAM_MCOPT2_DCEN_ENABLE
);
194 static int ecc_check_status_reg(void)
199 * Compare suceeded, now check
200 * if got ecc error. If got an
201 * ecc error, then don't count
202 * this as a passing value
204 mfsdram(SDRAM_ECCCR
, ecc_status
);
205 if (ecc_status
!= 0x00000000) {
207 ecc_clear_status_reg();
208 /* ecc check failure */
211 ecc_clear_status_reg();
217 /* return 1 if passes, 0 if fail */
218 static int short_mem_test(u32
*base_address
)
223 ulong test
[NUMMEMTESTS
][NUMMEMWORDS
] = {
224 /* 0 */ {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
225 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
226 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
227 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
228 /* 1 */ {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
229 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
230 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
231 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
232 /* 2 */ {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
233 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
234 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
235 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
236 /* 3 */ {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
237 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
238 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
239 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
240 /* 4 */ {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
241 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
242 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
243 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
244 /* 5 */ {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
245 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
246 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
247 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
248 /* 6 */ {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
249 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
250 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
251 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
252 /* 7 */ {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
253 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
254 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
255 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55},
257 #if defined(CONFIG_SYS_DECREMENT_PATTERNS)
258 /* 8 */ {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
259 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
260 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
261 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff},
262 /* 9 */ {0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
263 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
264 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe,
265 0xfffefffe, 0xfffefffe, 0xfffefffe, 0xfffefffe},
266 /* 10 */{0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
267 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
268 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd,
269 0xfffdfffd, 0xfffdfffd, 0xfffdffff, 0xfffdfffd},
270 /* 11 */{0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
271 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
272 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc,
273 0xfffcfffc, 0xfffcfffc, 0xfffcfffc, 0xfffcfffc},
274 /* 12 */{0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
275 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
276 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb,
277 0xfffbfffb, 0xfffffffb, 0xfffffffb, 0xfffffffb},
278 /* 13 */{0xfffafffa, 0xfffafffa, 0xfffffffa, 0xfffafffa,
279 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa,
280 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa,
281 0xfffafffa, 0xfffafffa, 0xfffafffa, 0xfffafffa},
282 /* 14 */{0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
283 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
284 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9,
285 0xfff9fff9, 0xfff9fff9, 0xfff9fff9, 0xfff9fff9},
286 /* 15 */{0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
287 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
288 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8,
289 0xfff8fff8, 0xfff8fff8, 0xfff8fff8, 0xfff8fff8},
290 /* 16 */{0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
291 0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
292 0xfff7fff7, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7,
293 0xfff7ffff, 0xfff7ffff, 0xfff7fff7, 0xfff7fff7},
294 /* 17 */{0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
295 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
296 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7,
297 0xfff6fff5, 0xfff6ffff, 0xfff6fff6, 0xfff6fff7},
298 /* 18 */{0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
299 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
300 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5,
301 0xfff5fff4, 0xfff5ffff, 0xfff5fff5, 0xfff5fff5},
302 /* 19 */{0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
303 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
304 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4,
305 0xfff4fff3, 0xfff4ffff, 0xfff4fff4, 0xfff4fff4},
306 /* 20 */{0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
307 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
308 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3,
309 0xfff3fff2, 0xfff3ffff, 0xfff3fff3, 0xfff3fff3},
310 /* 21 */{0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
311 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
312 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2,
313 0xfff2ffff, 0xfff2ffff, 0xfff2fff2, 0xfff2fff2},
314 /* 22 */{0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
315 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
316 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1,
317 0xfff1ffff, 0xfff1ffff, 0xfff1fff1, 0xfff1fff1},
318 /* 23 */{0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
319 0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
320 0xfff0fff0, 0xfff0fff0, 0xfff0fff0, 0xfff0fff0,
321 0xfff0fff0, 0xfff0fffe, 0xfff0fff0, 0xfff0fff0},
322 #endif /* CONFIG_SYS_DECREMENT_PATTERNS */
325 mfsdram(SDRAM_MCOPT1
, ecc_mode
);
326 if ((ecc_mode
& SDRAM_MCOPT1_MCHK_CHK_REP
) ==
327 SDRAM_MCOPT1_MCHK_CHK_REP
) {
328 ecc_clear_status_reg();
336 * Run the short memory test.
338 for (i
= 0; i
< NUMMEMTESTS
; i
++) {
339 for (j
= 0; j
< NUMMEMWORDS
; j
++) {
340 base_address
[j
] = test
[i
][j
];
341 ppcDcbf((ulong
)&(base_address
[j
]));
344 for (l
= 0; l
< NUMLOOPS
; l
++) {
345 for (j
= 0; j
< NUMMEMWORDS
; j
++) {
346 if (base_address
[j
] != test
[i
][j
]) {
347 ppcDcbf((u32
)&(base_address
[j
]));
351 if (!ecc_check_status_reg())
355 ppcDcbf((u32
)&(base_address
[j
]));
356 } /* for (j = 0; j < NUMMEMWORDS; j++) */
358 } /* for (l=0; l<NUMLOOPS; l++) */
364 #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
365 /*-----------------------------------------------------------------------------+
366 | program_DQS_calibration_methodA.
367 +-----------------------------------------------------------------------------*/
368 static u32
program_DQS_calibration_methodA(struct ddrautocal
*ddrcal
)
375 mfsdram(SDRAM_RDCC
, temp
);
376 debug("<%s>SDRAM_RDCC=0x%08x\n", __func__
, temp
);
379 pass_result
= DQS_calibration_methodA(ddrcal
);
385 * DQS_calibration_methodA()
387 * Autocalibration Method A
389 * ARRAY [Entire DQS Range] DQS_Valid_Window ; initialized to all zeros
390 * ARRAY [Entire FDBK Range] FDBK_Valid_Window; initialized to all zeros
391 * MEMWRITE(addr, expected_data);
392 * for (i = 0; i < Entire DQS Range; i++) { RQDC.RQFD
393 * for (j = 0; j < Entire FDBK Range; j++) { RFDC.RFFD
394 * MEMREAD(addr, actual_data);
395 * if (actual_data == expected_data) {
396 * DQS_Valid_Window[i] = 1; RQDC.RQFD
397 * FDBK_Valid_Window[i][j] = 1; RFDC.RFFD
402 static u32
DQS_calibration_methodA(struct ddrautocal
*cal
)
419 struct autocal_regs curr_win_min
;
420 struct autocal_regs curr_win_max
;
421 struct autocal_regs best_win_min
;
422 struct autocal_regs best_win_max
;
423 struct autocal_regs loop_win_min
;
424 struct autocal_regs loop_win_max
;
431 char slash
[] = "\\|/-\\|/-";
437 memset(&curr_win_min
, 0, sizeof(curr_win_min
));
438 memset(&curr_win_max
, 0, sizeof(curr_win_max
));
439 memset(&best_win_min
, 0, sizeof(best_win_min
));
440 memset(&best_win_max
, 0, sizeof(best_win_max
));
441 memset(&loop_win_min
, 0, sizeof(loop_win_min
));
442 memset(&loop_win_max
, 0, sizeof(loop_win_max
));
447 * Program RDCC register
448 * Read sample cycle auto-update enable
450 mtsdram(SDRAM_RDCC
, SDRAM_RDCC_RDSS_T1
| SDRAM_RDCC_RSAE_ENABLE
);
453 mfsdram(SDRAM_RDCC
, temp
);
454 debug("<%s>SDRAM_RDCC=0x%x\n", __func__
, temp
);
455 mfsdram(SDRAM_RTSR
, temp
);
456 debug("<%s>SDRAM_RTSR=0x%x\n", __func__
, temp
);
457 mfsdram(SDRAM_FCSR
, temp
);
458 debug("<%s>SDRAM_FCSR=0x%x\n", __func__
, temp
);
462 * Program RQDC register
463 * Internal DQS delay mechanism enable
466 SDRAM_RQDC_RQDE_ENABLE
| SDRAM_RQDC_RQFD_ENCODE(0x00));
469 mfsdram(SDRAM_RQDC
, temp
);
470 debug("<%s>SDRAM_RQDC=0x%x\n", __func__
, temp
);
474 * Program RFDC register
475 * Set Feedback Fractional Oversample
476 * Auto-detect read sample cycle enable
478 mtsdram(SDRAM_RFDC
, SDRAM_RFDC_ARSE_ENABLE
|
479 SDRAM_RFDC_RFOS_ENCODE(0) | SDRAM_RFDC_RFFD_ENCODE(0));
482 mfsdram(SDRAM_RFDC
, temp
);
483 debug("<%s>SDRAM_RFDC=0x%x\n", __func__
, temp
);
487 for (rqfd
= 0; rqfd
<= SDRAM_RQDC_RQFD_MAX
; rqfd
++) {
489 mfsdram(SDRAM_RQDC
, rqdc_reg
);
490 rqdc_reg
&= ~(SDRAM_RQDC_RQFD_MASK
);
491 mtsdram(SDRAM_RQDC
, rqdc_reg
| SDRAM_RQDC_RQFD_ENCODE(rqfd
));
494 putc(slash
[loopi
++ % 8]);
496 curr_win_min
.rffd
= 0;
497 curr_win_max
.rffd
= 0;
500 for (rffd
= 0, pass
= 0; rffd
<= SDRAM_RFDC_RFFD_MAX
; rffd
++) {
501 mfsdram(SDRAM_RFDC
, rfdc_reg
);
502 rfdc_reg
&= ~(SDRAM_RFDC_RFFD_MASK
);
504 rfdc_reg
| SDRAM_RFDC_RFFD_ENCODE(rffd
));
506 for (bxcr_num
= 0; bxcr_num
< MAXBXCF
; bxcr_num
++) {
507 mfsdram(SDRAM_MB0CF
+ (bxcr_num
<<2), bxcf
);
510 if (bxcf
& SDRAM_BXCF_M_BE_MASK
) {
511 /* Bank is enabled */
512 membase
= get_membase(bxcr_num
);
513 pass
= short_mem_test(membase
);
514 } /* if bank enabled */
517 /* If this value passed update RFFD windows */
518 if (pass
&& !in_window
) { /* at the start of window */
520 curr_win_min
.rffd
= curr_win_max
.rffd
= rffd
;
521 curr_win_min
.rqfd
= curr_win_max
.rqfd
= rqfd
;
522 mfsdram(SDRAM_RDCC
, rdcc
); /*record this value*/
523 } else if (!pass
&& in_window
) { /* at end of window */
525 } else if (pass
&& in_window
) { /* within the window */
526 curr_win_max
.rffd
= rffd
;
527 curr_win_max
.rqfd
= rqfd
;
529 /* else if (!pass && !in_window)
530 skip - no pass, not currently in a window */
533 if ((curr_win_max
.rffd
- curr_win_min
.rffd
) >
534 (best_win_max
.rffd
- best_win_min
.rffd
)) {
535 best_win_min
.rffd
= curr_win_min
.rffd
;
536 best_win_max
.rffd
= curr_win_max
.rffd
;
538 best_win_min
.rqfd
= curr_win_min
.rqfd
;
539 best_win_max
.rqfd
= curr_win_max
.rqfd
;
547 * save-off the best window results of the RFDC.RFFD
548 * for this RQDC.RQFD setting
551 * if (just ended RFDC.RFDC loop pass window) >
552 * (prior RFDC.RFFD loop pass window)
554 if ((best_win_max
.rffd
- best_win_min
.rffd
) >
555 (loop_win_max
.rffd
- loop_win_min
.rffd
)) {
556 loop_win_min
.rffd
= best_win_min
.rffd
;
557 loop_win_max
.rffd
= best_win_max
.rffd
;
558 loop_win_min
.rqfd
= rqfd
;
559 loop_win_max
.rqfd
= rqfd
;
560 debug("RQFD.min 0x%08x, RQFD.max 0x%08x, "
561 "RFFD.min 0x%08x, RFFD.max 0x%08x\n",
562 loop_win_min
.rqfd
, loop_win_max
.rqfd
,
563 loop_win_min
.rffd
, loop_win_max
.rffd
);
571 if ((loop_win_min
.rffd
== 0) && (loop_win_max
.rffd
== 0) &&
572 (best_win_min
.rffd
== 0) && (best_win_max
.rffd
== 0) &&
573 (best_win_min
.rqfd
== 0) && (best_win_max
.rqfd
== 0)) {
578 * Need to program RQDC before RFDC.
580 debug("<%s> RQFD Min: 0x%x\n", __func__
, loop_win_min
.rqfd
);
581 debug("<%s> RQFD Max: 0x%x\n", __func__
, loop_win_max
.rqfd
);
582 rqfd_average
= loop_win_max
.rqfd
;
584 if (rqfd_average
< 0)
587 if (rqfd_average
> SDRAM_RQDC_RQFD_MAX
)
588 rqfd_average
= SDRAM_RQDC_RQFD_MAX
;
590 debug("<%s> RFFD average: 0x%08x\n", __func__
, rqfd_average
);
591 mtsdram(SDRAM_RQDC
, (rqdc_reg
& ~SDRAM_RQDC_RQFD_MASK
) |
592 SDRAM_RQDC_RQFD_ENCODE(rqfd_average
));
594 debug("<%s> RFFD Min: 0x%08x\n", __func__
, loop_win_min
.rffd
);
595 debug("<%s> RFFD Max: 0x%08x\n", __func__
, loop_win_max
.rffd
);
596 rffd_average
= ((loop_win_min
.rffd
+ loop_win_max
.rffd
) / 2);
598 if (rffd_average
< 0)
601 if (rffd_average
> SDRAM_RFDC_RFFD_MAX
)
602 rffd_average
= SDRAM_RFDC_RFFD_MAX
;
604 debug("<%s> RFFD average: 0x%08x\n", __func__
, rffd_average
);
605 mtsdram(SDRAM_RFDC
, rfdc_reg
| SDRAM_RFDC_RFFD_ENCODE(rffd_average
));
607 /* if something passed, then return the size of the largest window */
609 passed
= loop_win_max
.rffd
- loop_win_min
.rffd
;
610 cal
->rqfd
= rqfd_average
;
611 cal
->rffd
= rffd_average
;
612 cal
->rffd_min
= loop_win_min
.rffd
;
613 cal
->rffd_max
= loop_win_max
.rffd
;
619 #else /* !defined(CONFIG_PPC4xx_DDR_METHOD_A) */
621 /*-----------------------------------------------------------------------------+
622 | program_DQS_calibration_methodB.
623 +-----------------------------------------------------------------------------*/
624 static u32
program_DQS_calibration_methodB(struct ddrautocal
*ddrcal
)
633 * Program RDCC register
634 * Read sample cycle auto-update enable
636 mtsdram(SDRAM_RDCC
, SDRAM_RDCC_RDSS_T2
| SDRAM_RDCC_RSAE_ENABLE
);
639 mfsdram(SDRAM_RDCC
, temp
);
640 debug("<%s>SDRAM_RDCC=0x%08x\n", __func__
, temp
);
644 * Program RQDC register
645 * Internal DQS delay mechanism enable
648 #if defined(CONFIG_DDR_RQDC_START_VAL)
649 SDRAM_RQDC_RQDE_ENABLE
|
650 SDRAM_RQDC_RQFD_ENCODE(CONFIG_DDR_RQDC_START_VAL
));
652 SDRAM_RQDC_RQDE_ENABLE
| SDRAM_RQDC_RQFD_ENCODE(0x38));
656 mfsdram(SDRAM_RQDC
, temp
);
657 debug("<%s>SDRAM_RQDC=0x%08x\n", __func__
, temp
);
661 * Program RFDC register
662 * Set Feedback Fractional Oversample
663 * Auto-detect read sample cycle enable
665 mtsdram(SDRAM_RFDC
, SDRAM_RFDC_ARSE_ENABLE
|
666 SDRAM_RFDC_RFOS_ENCODE(0) |
667 SDRAM_RFDC_RFFD_ENCODE(0));
670 mfsdram(SDRAM_RFDC
, temp
);
671 debug("<%s>SDRAM_RFDC=0x%08x\n", __func__
, temp
);
674 pass_result
= DQS_calibration_methodB(ddrcal
);
680 * DQS_calibration_methodB()
682 * Autocalibration Method B
684 * ARRAY [Entire DQS Range] DQS_Valid_Window ; initialized to all zeros
685 * ARRAY [Entire Feedback Range] FDBK_Valid_Window; initialized to all zeros
686 * MEMWRITE(addr, expected_data);
687 * Initialialize the DQS delay to 80 degrees (MCIF0_RRQDC[RQFD]=0x38).
689 * for (j = 0; j < Entire Feedback Range; j++) {
690 * MEMREAD(addr, actual_data);
691 * if (actual_data == expected_data) {
692 * FDBK_Valid_Window[j] = 1;
696 * Set MCIF0_RFDC[RFFD] to the middle of the FDBK_Valid_Window.
698 * for (i = 0; i < Entire DQS Range; i++) {
699 * MEMREAD(addr, actual_data);
700 * if (actual_data == expected_data) {
701 * DQS_Valid_Window[i] = 1;
705 * Set MCIF0_RRQDC[RQFD] to the middle of the DQS_Valid_Window.
707 /*-----------------------------------------------------------------------------+
708 | DQS_calibration_methodB.
709 +-----------------------------------------------------------------------------*/
710 static u32
DQS_calibration_methodB(struct ddrautocal
*cal
)
729 u32 curr_win_min
, curr_win_max
;
730 u32 best_win_min
, best_win_max
;
733 /*------------------------------------------------------------------
734 | Test to determine the best read clock delay tuning bits.
736 | Before the DDR controller can be used, the read clock delay needs to
737 | be set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
738 | This value cannot be hardcoded into the program because it changes
739 | depending on the board's setup and environment.
740 | To do this, all delay values are tested to see if they
741 | work or not. By doing this, you get groups of fails with groups of
742 | passing values. The idea is to find the start and end of a passing
743 | window and take the center of it to use as the read clock delay.
745 | A failure has to be seen first so that when we hit a pass, we know
746 | that it is truely the start of the window. If we get passing values
747 | to start off with, we don't know if we are at the start of the window
749 | The code assumes that a failure will always be found.
750 | If a failure is not found, there is no easy way to get the middle
751 | of the passing window. I guess we can pretty much pick any value
752 | but some values will be better than others. Since the lowest speed
753 | we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
754 | from experimentation it is safe to say you will always have a failure
755 +-----------------------------------------------------------------*/
762 curr_win_min
= curr_win_max
= 0;
763 best_win_min
= best_win_max
= 0;
764 for (rffd
= 0; rffd
<= SDRAM_RFDC_RFFD_MAX
; rffd
++) {
765 mfsdram(SDRAM_RFDC
, rfdc_reg
);
766 rfdc_reg
&= ~(SDRAM_RFDC_RFFD_MASK
);
767 mtsdram(SDRAM_RFDC
, rfdc_reg
| SDRAM_RFDC_RFFD_ENCODE(rffd
));
770 for (bxcr_num
= 0; bxcr_num
< MAXBXCF
; bxcr_num
++) {
771 mfsdram(SDRAM_MB0CF
+ (bxcr_num
<<2), bxcf
);
774 if (bxcf
& SDRAM_BXCF_M_BE_MASK
) {
775 /* Bank is enabled */
776 membase
= get_membase(bxcr_num
);
777 pass
&= short_mem_test(membase
);
778 } /* if bank enabled */
781 /* If this value passed */
782 if (pass
&& !in_window
) { /* start of passing window */
784 curr_win_min
= curr_win_max
= rffd
;
785 mfsdram(SDRAM_RDCC
, rdcc
); /* record this value */
786 } else if (!pass
&& in_window
) { /* end passing window */
788 } else if (pass
&& in_window
) { /* within the passing window */
793 if ((curr_win_max
- curr_win_min
) >
794 (best_win_max
- best_win_min
)) {
795 best_win_min
= curr_win_min
;
796 best_win_max
= curr_win_max
;
803 if ((best_win_min
== 0) && (best_win_max
== 0))
806 size
= best_win_max
- best_win_min
;
808 debug("RFFD Min: 0x%x\n", best_win_min
);
809 debug("RFFD Max: 0x%x\n", best_win_max
);
810 rffd_average
= ((best_win_min
+ best_win_max
) / 2);
812 cal
->rffd_min
= best_win_min
;
813 cal
->rffd_max
= best_win_max
;
815 if (rffd_average
< 0)
818 if (rffd_average
> SDRAM_RFDC_RFFD_MAX
)
819 rffd_average
= SDRAM_RFDC_RFFD_MAX
;
821 mtsdram(SDRAM_RFDC
, rfdc_reg
| SDRAM_RFDC_RFFD_ENCODE(rffd_average
));
826 curr_win_min
= curr_win_max
= 0;
827 best_win_min
= best_win_max
= 0;
828 for (rqfd
= 0; rqfd
<= SDRAM_RQDC_RQFD_MAX
; rqfd
++) {
829 mfsdram(SDRAM_RQDC
, rqdc_reg
);
830 rqdc_reg
&= ~(SDRAM_RQDC_RQFD_MASK
);
831 mtsdram(SDRAM_RQDC
, rqdc_reg
| SDRAM_RQDC_RQFD_ENCODE(rqfd
));
834 for (bxcr_num
= 0; bxcr_num
< MAXBXCF
; bxcr_num
++) {
836 mfsdram(SDRAM_MB0CF
+ (bxcr_num
<<2), bxcf
);
839 if (bxcf
& SDRAM_BXCF_M_BE_MASK
) {
840 /* Bank is enabled */
841 membase
= get_membase(bxcr_num
);
842 pass
&= short_mem_test(membase
);
843 } /* if bank enabled */
846 /* If this value passed */
847 if (pass
&& !in_window
) {
849 curr_win_min
= curr_win_max
= rqfd
;
850 } else if (!pass
&& in_window
) {
852 } else if (pass
&& in_window
) {
857 if ((curr_win_max
- curr_win_min
) >
858 (best_win_max
- best_win_min
)) {
859 best_win_min
= curr_win_min
;
860 best_win_max
= curr_win_max
;
866 if ((best_win_min
== 0) && (best_win_max
== 0))
869 debug("RQFD Min: 0x%x\n", best_win_min
);
870 debug("RQFD Max: 0x%x\n", best_win_max
);
871 rqfd_average
= ((best_win_min
+ best_win_max
) / 2);
873 if (rqfd_average
< 0)
876 if (rqfd_average
> SDRAM_RQDC_RQFD_MAX
)
877 rqfd_average
= SDRAM_RQDC_RQFD_MAX
;
879 mtsdram(SDRAM_RQDC
, (rqdc_reg
& ~SDRAM_RQDC_RQFD_MASK
) |
880 SDRAM_RQDC_RQFD_ENCODE(rqfd_average
));
882 mfsdram(SDRAM_RQDC
, rqdc_reg
);
883 mfsdram(SDRAM_RFDC
, rfdc_reg
);
886 * Need to program RQDC before RFDC. The value is read above.
887 * That is the reason why auto cal not work.
888 * See, comments below.
890 mtsdram(SDRAM_RQDC
, rqdc_reg
);
891 mtsdram(SDRAM_RFDC
, rfdc_reg
);
893 debug("RQDC: 0x%08X\n", rqdc_reg
);
894 debug("RFDC: 0x%08X\n", rfdc_reg
);
896 /* if something passed, then return the size of the largest window */
899 cal
->rqfd
= rqfd_average
;
900 cal
->rffd
= rffd_average
;
905 #endif /* defined(CONFIG_PPC4xx_DDR_METHOD_A) */
908 * Default table for DDR auto-calibration of all
909 * possible WRDTR and CLKTR values.
911 * {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]}
913 * Table is terminated with {-1, -1} value pair.
915 * Board vendors can specify their own board specific subset of
916 * known working {SDRAM_WRDTR.[WDTR], SDRAM_CLKTR.[CKTR]} value
917 * pairs via a board defined ddr_scan_option() function.
919 struct sdram_timing full_scan_options
[] = {
920 {0, 0}, {0, 1}, {0, 2}, {0, 3},
921 {1, 0}, {1, 1}, {1, 2}, {1, 3},
922 {2, 0}, {2, 1}, {2, 2}, {2, 3},
923 {3, 0}, {3, 1}, {3, 2}, {3, 3},
924 {4, 0}, {4, 1}, {4, 2}, {4, 3},
925 {5, 0}, {5, 1}, {5, 2}, {5, 3},
926 {6, 0}, {6, 1}, {6, 2}, {6, 3},
930 /*---------------------------------------------------------------------------+
932 +----------------------------------------------------------------------------*/
933 u32
DQS_autocalibration(void)
940 struct ddrautocal ddrcal
;
941 struct autocal_clks tcal
;
947 char slash
[] = "\\|/-\\|/-";
949 struct sdram_timing
*scan_list
;
951 #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
953 char tmp
[64]; /* long enough for environment variables */
956 memset(&tcal
, 0, sizeof(tcal
));
958 ddr_scan_option((ulong
)full_scan_options
);
961 (struct sdram_timing
*)ddr_scan_option((ulong
)full_scan_options
);
963 mfsdram(SDRAM_MCOPT1
, val
);
964 if ((val
& SDRAM_MCOPT1_MCHK_CHK_REP
) == SDRAM_MCOPT1_MCHK_CHK_REP
)
965 str
= "ECC Auto calibration -";
967 str
= "Auto calibration -";
971 #if defined(DEBUG_PPC4xx_DDR_AUTOCALIBRATION)
972 i
= getenv_r("autocalib", tmp
, sizeof(tmp
));
974 strcpy(tmp
, CONFIG_AUTOCALIB
);
976 if (strcmp(tmp
, "final") == 0) {
977 /* display the final autocalibration results only */
979 } else if (strcmp(tmp
, "loop") == 0) {
980 /* display summary autocalibration info per iteration */
982 } else if (strcmp(tmp
, "display") == 0) {
983 /* display full debug autocalibration window info. */
986 #endif /* (DEBUG_PPC4xx_DDR_AUTOCALIBRATION) */
988 best_rdcc
= (SDRAM_RDCC_RDSS_T4
>> 30);
990 while ((scan_list
->wrdtr
!= -1) && (scan_list
->clktr
!= -1)) {
991 wdtr
= scan_list
->wrdtr
;
992 clkp
= scan_list
->clktr
;
994 mfsdram(SDRAM_WRDTR
, val
);
995 val
&= ~(SDRAM_WRDTR_LLWP_MASK
| SDRAM_WRDTR_WTR_MASK
);
996 mtsdram(SDRAM_WRDTR
, (val
|
997 ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC
| (wdtr
<< 25))));
999 mtsdram(SDRAM_CLKTR
, clkp
<< 30);
1001 relock_memory_DLL();
1004 putc(slash
[loopi
++ % 8]);
1008 debug("*** --------------\n");
1009 mfsdram(SDRAM_WRDTR
, val
);
1010 debug("*** SDRAM_WRDTR set to 0x%08x\n", val
);
1011 mfsdram(SDRAM_CLKTR
, val
);
1012 debug("*** SDRAM_CLKTR set to 0x%08x\n", val
);
1016 if (verbose_lvl
> 2) {
1017 printf("*** SDRAM_WRDTR (wdtr) set to %d\n", wdtr
);
1018 printf("*** SDRAM_CLKTR (clkp) set to %d\n", clkp
);
1021 memset(&ddrcal
, 0, sizeof(ddrcal
));
1027 * program_DQS_calibration_method[A|B]() returns 0 if no
1028 * passing RFDC.[RFFD] window is found or returns the size
1029 * of the best passing window; in the case of a found passing
1030 * window, the ddrcal will contain the values of the best
1031 * window RQDC.[RQFD] and RFDC.[RFFD].
1035 * Call PPC4xx SDRAM DDR autocalibration methodA or methodB.
1036 * Default is methodB.
1037 * Defined the autocalibration method in the board specific
1039 * Please see include/configs/kilauea.h for an example for
1040 * a board specific implementation.
1042 #if defined(CONFIG_PPC4xx_DDR_METHOD_A)
1043 result
= program_DQS_calibration_methodA(&ddrcal
);
1045 result
= program_DQS_calibration_methodB(&ddrcal
);
1051 * Clear potential errors resulting from auto-calibration.
1052 * If not done, then we could get an interrupt later on when
1053 * exceptions are enabled.
1055 set_mcsr(get_mcsr());
1057 val
= ddrcal
.rdcc
; /* RDCC from the best passing window */
1061 if (verbose_lvl
> 1) {
1063 switch ((val
>> 30)) {
1083 printf("** WRDTR(%d) CLKTR(%d), Wind (%d), best (%d), "
1084 "max-min(0x%04x)(0x%04x), RDCC: %s\n",
1085 wdtr
, clkp
, result
, best_result
,
1086 ddrcal
.rffd_min
, ddrcal
.rffd_max
, tstr
);
1090 * The DQS calibration "result" is either "0"
1091 * if no passing window was found, or is the
1092 * size of the RFFD passing window.
1095 tcal
.autocal
.flags
= 1;
1096 debug("*** (%d)(%d) result passed window size: 0x%08x, "
1097 "rqfd = 0x%08x, rffd = 0x%08x, rdcc = 0x%08x\n",
1098 wdtr
, clkp
, result
, ddrcal
.rqfd
,
1099 ddrcal
.rffd
, ddrcal
.rdcc
);
1101 * Save the SDRAM_WRDTR and SDRAM_CLKTR
1102 * settings for the largest returned
1103 * RFFD passing window size.
1105 if (result
> best_result
) {
1107 * want the lowest Read Sample Cycle Select
1109 val
= (val
& SDRAM_RDCC_RDSS_MASK
) >> 30;
1110 debug("*** (%d) (%d) current_rdcc, best_rdcc\n",
1112 if (val
<= best_rdcc
) {
1114 tcal
.clocks
.wrdtr
= wdtr
;
1115 tcal
.clocks
.clktr
= clkp
;
1116 tcal
.clocks
.rdcc
= (val
<< 30);
1117 tcal
.autocal
.rqfd
= ddrcal
.rqfd
;
1118 tcal
.autocal
.rffd
= ddrcal
.rffd
;
1119 best_result
= result
;
1121 if (verbose_lvl
> 2) {
1122 printf("** (%d)(%d) "
1123 "best result: 0x%04x\n",
1126 printf("** (%d)(%d) "
1127 "best WRDTR: 0x%04x\n",
1130 printf("** (%d)(%d) "
1131 "best CLKTR: 0x%04x\n",
1134 printf("** (%d)(%d) "
1135 "best RQDC: 0x%04x\n",
1138 printf("** (%d)(%d) "
1139 "best RFDC: 0x%04x\n",
1142 printf("** (%d)(%d) "
1143 "best RDCC: 0x%08x\n",
1145 (u32
)tcal
.clocks
.rdcc
);
1146 mfsdram(SDRAM_RTSR
, val
);
1147 printf("** (%d)(%d) best "
1148 "loop RTSR: 0x%08x\n",
1150 mfsdram(SDRAM_FCSR
, val
);
1151 printf("** (%d)(%d) best "
1152 "loop FCSR: 0x%08x\n",
1155 } /* if (val <= best_rdcc) */
1156 } /* if (result >= best_result) */
1157 } /* if (result != 0) */
1159 } /* while ((scan_list->wrdtr != -1) && (scan_list->clktr != -1)) */
1161 if (tcal
.autocal
.flags
== 1) {
1162 if (verbose_lvl
> 0) {
1163 printf("*** --------------\n");
1164 printf("*** best_result window size: %d\n",
1166 printf("*** best_result WRDTR: 0x%04x\n",
1168 printf("*** best_result CLKTR: 0x%04x\n",
1170 printf("*** best_result RQFD: 0x%04x\n",
1172 printf("*** best_result RFFD: 0x%04x\n",
1174 printf("*** best_result RDCC: 0x%04x\n",
1176 printf("*** --------------\n");
1181 * if got best passing result window, then lock in the
1182 * best CLKTR, WRDTR, RQFD, and RFFD values
1184 mfsdram(SDRAM_WRDTR
, val
);
1185 mtsdram(SDRAM_WRDTR
, (val
&
1186 ~(SDRAM_WRDTR_LLWP_MASK
| SDRAM_WRDTR_WTR_MASK
)) |
1187 ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC
|
1188 (tcal
.clocks
.wrdtr
<< 25)));
1190 mtsdram(SDRAM_CLKTR
, tcal
.clocks
.clktr
<< 30);
1192 relock_memory_DLL();
1194 mfsdram(SDRAM_RQDC
, rqdc_reg
);
1195 rqdc_reg
&= ~(SDRAM_RQDC_RQFD_MASK
);
1196 mtsdram(SDRAM_RQDC
, rqdc_reg
|
1197 SDRAM_RQDC_RQFD_ENCODE(tcal
.autocal
.rqfd
));
1199 mfsdram(SDRAM_RQDC
, rqdc_reg
);
1200 debug("*** best_result: read value SDRAM_RQDC 0x%08x\n",
1203 mfsdram(SDRAM_RFDC
, rfdc_reg
);
1204 rfdc_reg
&= ~(SDRAM_RFDC_RFFD_MASK
);
1205 mtsdram(SDRAM_RFDC
, rfdc_reg
|
1206 SDRAM_RFDC_RFFD_ENCODE(tcal
.autocal
.rffd
));
1208 mfsdram(SDRAM_RFDC
, rfdc_reg
);
1209 debug("*** best_result: read value SDRAM_RFDC 0x%08x\n",
1211 mfsdram(SDRAM_RDCC
, val
);
1212 debug("*** SDRAM_RDCC 0x%08x\n", val
);
1215 * no valid windows were found
1217 printf("DQS memory calibration window can not be determined, "
1218 "terminating u-boot.\n");
1219 ppc4xx_ibm_ddr2_register_dump();
1220 spd_ddr_init_hang();
1223 blank_string(strlen(str
));
1227 #else /* defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL) */
1228 u32
DQS_autocalibration(void)
1232 #endif /* !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) */
1233 #endif /* defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */