blob: 7202c3fc46f96d94d091f0ba586b6a2b8e84ee0a [file] [log] [blame]
Stefan Roese43f32472007-02-20 10:43:34 +01001/*
Stefan Roese88fbf932010-04-15 16:07:28 +02002 * arch/powerpc/cpu/ppc4xx/44x_spd_ddr2.c
Stefan Roese43f32472007-02-20 10:43:34 +01003 * This SPD SDRAM detection code supports AMCC PPC44x cpu's with a
Stefan Roese964754e2008-04-30 10:49:43 +02004 * DDR2 controller (non Denali Core). Those currently are:
5 *
Grant Ericksonb6933412008-05-22 14:44:14 -07006 * 405: 405EX(r)
Stefan Roese964754e2008-04-30 10:49:43 +02007 * 440/460: 440SP/440SPe/460EX/460GT
Stefan Roese43f32472007-02-20 10:43:34 +01008 *
Grant Ericksonb6933412008-05-22 14:44:14 -07009 * Copyright (c) 2008 Nuovation System Designs, LLC
10 * Grant Erickson <gerickson@nuovations.com>
11
Stefan Roesecb9ebd02009-09-28 17:33:45 +020012 * (C) Copyright 2007-2009
Stefan Roese43f32472007-02-20 10:43:34 +010013 * Stefan Roese, DENX Software Engineering, sr@denx.de.
14 *
15 * COPYRIGHT AMCC CORPORATION 2004
16 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese43f32472007-02-20 10:43:34 +010018 */
19
20/* define DEBUG for debugging output (obviously ;-)) */
21#if 0
22#define DEBUG
23#endif
24
25#include <common.h>
Stefan Roesebad41112007-03-01 21:11:36 +010026#include <command.h>
Stefan Roese247e9d72010-09-09 19:18:00 +020027#include <asm/ppc4xx.h>
Stefan Roese43f32472007-02-20 10:43:34 +010028#include <i2c.h>
29#include <asm/io.h>
30#include <asm/processor.h>
31#include <asm/mmu.h>
Stefan Roese286b81b2008-04-29 13:57:07 +020032#include <asm/cache.h>
Stefan Roese43f32472007-02-20 10:43:34 +010033
Stefan Roesecb9ebd02009-09-28 17:33:45 +020034#include "ecc.h"
35
Stefan Roese2001a332008-07-10 15:32:32 +020036#define PPC4xx_IBM_DDR2_DUMP_REGISTER(mnemonic) \
37 do { \
38 u32 data; \
39 mfsdram(SDRAM_##mnemonic, data); \
40 printf("%20s[%02x] = 0x%08X\n", \
41 "SDRAM_" #mnemonic, SDRAM_##mnemonic, data); \
42 } while (0)
43
Felix Radensky8d4d4a62009-07-01 11:37:46 +030044#define PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(mnemonic) \
45 do { \
46 u32 data; \
47 data = mfdcr(SDRAM_##mnemonic); \
48 printf("%20s[%02x] = 0x%08X\n", \
49 "SDRAM_" #mnemonic, SDRAM_##mnemonic, data); \
50 } while (0)
51
Stefan Roesec3bcfce2010-05-25 15:33:14 +020052static void update_rdcc(void)
53{
54 u32 val;
55
56 /*
57 * Complete RDSS configuration as mentioned on page 7 of the AMCC
58 * PowerPC440SP/SPe DDR2 application note:
59 * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
60 *
61 * Or item #10 "10. Complete RDSS configuration" in chapter
62 * "22.2.9 SDRAM Initialization" of AMCC PPC460EX/EXr/GT users
63 * manual.
64 */
65 mfsdram(SDRAM_RTSR, val);
66 if ((val & SDRAM_RTSR_TRK1SM_MASK) == SDRAM_RTSR_TRK1SM_ATPLS1) {
67 mfsdram(SDRAM_RDCC, val);
68 if ((val & SDRAM_RDCC_RDSS_MASK) != SDRAM_RDCC_RDSS_T4) {
69 val += 0x40000000;
70 mtsdram(SDRAM_RDCC, val);
71 }
72 }
73}
Stefan Roesec3bcfce2010-05-25 15:33:14 +020074
Adam Graham446eb8d2008-10-08 10:13:14 -070075#if defined(CONFIG_440)
76/*
77 * This DDR2 setup code can dynamically setup the TLB entries for the DDR2
78 * memory region. Right now the cache should still be disabled in U-Boot
79 * because of the EMAC driver, that need its buffer descriptor to be located
80 * in non cached memory.
81 *
82 * If at some time this restriction doesn't apply anymore, just define
83 * CONFIG_4xx_DCACHE in the board config file and this code should setup
84 * everything correctly.
85 */
86#ifdef CONFIG_4xx_DCACHE
87/* enable caching on SDRAM */
88#define MY_TLB_WORD2_I_ENABLE 0
89#else
90/* disable caching on SDRAM */
91#define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
92#endif /* CONFIG_4xx_DCACHE */
Felix Radensky0e925362009-09-27 23:56:12 +020093
94void dcbz_area(u32 start_address, u32 num_bytes);
Adam Graham446eb8d2008-10-08 10:13:14 -070095#endif /* CONFIG_440 */
96
Felix Radensky0e925362009-09-27 23:56:12 +020097#define MAXRANKS 4
98#define MAXBXCF 4
99
100#define MULDIV64(m1, m2, d) (u32)(((u64)(m1) * (u64)(m2)) / (u64)(d))
101
Felix Radensky0e925362009-09-27 23:56:12 +0200102/*-----------------------------------------------------------------------------+
103 * sdram_memsize
104 *-----------------------------------------------------------------------------*/
Stefan Roesecb9ebd02009-09-28 17:33:45 +0200105phys_size_t sdram_memsize(void)
Felix Radensky0e925362009-09-27 23:56:12 +0200106{
107 phys_size_t mem_size;
108 unsigned long mcopt2;
109 unsigned long mcstat;
110 unsigned long mb0cf;
111 unsigned long sdsz;
112 unsigned long i;
113
114 mem_size = 0;
115
116 mfsdram(SDRAM_MCOPT2, mcopt2);
117 mfsdram(SDRAM_MCSTAT, mcstat);
118
119 /* DDR controller must be enabled and not in self-refresh. */
120 /* Otherwise memsize is zero. */
121 if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
122 && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
123 && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
124 == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
125 for (i = 0; i < MAXBXCF; i++) {
126 mfsdram(SDRAM_MB0CF + (i << 2), mb0cf);
127 /* Banks enabled */
128 if ((mb0cf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
129#if defined(CONFIG_440)
130 sdsz = mfdcr_any(SDRAM_R0BAS + i) & SDRAM_RXBAS_SDSZ_MASK;
131#else
132 sdsz = mb0cf & SDRAM_RXBAS_SDSZ_MASK;
133#endif
134 switch(sdsz) {
135 case SDRAM_RXBAS_SDSZ_8:
136 mem_size+=8;
137 break;
138 case SDRAM_RXBAS_SDSZ_16:
139 mem_size+=16;
140 break;
141 case SDRAM_RXBAS_SDSZ_32:
142 mem_size+=32;
143 break;
144 case SDRAM_RXBAS_SDSZ_64:
145 mem_size+=64;
146 break;
147 case SDRAM_RXBAS_SDSZ_128:
148 mem_size+=128;
149 break;
150 case SDRAM_RXBAS_SDSZ_256:
151 mem_size+=256;
152 break;
153 case SDRAM_RXBAS_SDSZ_512:
154 mem_size+=512;
155 break;
156 case SDRAM_RXBAS_SDSZ_1024:
157 mem_size+=1024;
158 break;
159 case SDRAM_RXBAS_SDSZ_2048:
160 mem_size+=2048;
161 break;
162 case SDRAM_RXBAS_SDSZ_4096:
163 mem_size+=4096;
164 break;
165 default:
166 printf("WARNING: Unsupported bank size (SDSZ=0x%lx)!\n"
167 , sdsz);
168 mem_size=0;
169 break;
170 }
171 }
172 }
173 }
174
175 return mem_size << 20;
176}
177
178/*-----------------------------------------------------------------------------+
Stefan Roesecb9ebd02009-09-28 17:33:45 +0200179 * is_ecc_enabled
180 *-----------------------------------------------------------------------------*/
181static unsigned long is_ecc_enabled(void)
182{
183 unsigned long val;
184
185 mfsdram(SDRAM_MCOPT1, val);
186
187 return SDRAM_MCOPT1_MCHK_CHK_DECODE(val);
188}
189
190/*-----------------------------------------------------------------------------+
Felix Radensky0e925362009-09-27 23:56:12 +0200191 * board_add_ram_info
192 *-----------------------------------------------------------------------------*/
193void board_add_ram_info(int use_default)
194{
195 PPC4xx_SYS_INFO board_cfg;
196 u32 val;
197
198 if (is_ecc_enabled())
199 puts(" (ECC");
200 else
201 puts(" (ECC not");
202
203 get_sys_info(&board_cfg);
204
Stefan Roesecb9ebd02009-09-28 17:33:45 +0200205#if defined(CONFIG_405EX)
206 val = board_cfg.freqPLB;
207#else
Felix Radensky0e925362009-09-27 23:56:12 +0200208 mfsdr(SDR0_DDR0, val);
209 val = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(val), 1);
Felix Radensky0e925362009-09-27 23:56:12 +0200210#endif
211 printf(" enabled, %d MHz", (val * 2) / 1000000);
212
213 mfsdram(SDRAM_MMODE, val);
214 val = (val & SDRAM_MMODE_DCL_MASK) >> 4;
215 printf(", CL%d)", val);
216}
Felix Radensky0e925362009-09-27 23:56:12 +0200217
Stefan Roese2001a332008-07-10 15:32:32 +0200218#if defined(CONFIG_SPD_EEPROM)
Stefan Roese43f32472007-02-20 10:43:34 +0100219
Stefan Roesebad41112007-03-01 21:11:36 +0100220/*-----------------------------------------------------------------------------+
221 * Defines
222 *-----------------------------------------------------------------------------*/
Stefan Roese43f32472007-02-20 10:43:34 +0100223#define SDRAM_DDR1 1
224#define SDRAM_DDR2 2
225#define SDRAM_NONE 0
226
Wolfgang Denk70df7bc2007-06-22 23:59:00 +0200227#define MAXDIMMS 2
Stefan Roese43f32472007-02-20 10:43:34 +0100228#define MAX_SPD_BYTES 256 /* Max number of bytes on the DIMM's SPD EEPROM */
229
230#define ONE_BILLION 1000000000
231
Stefan Roesebad41112007-03-01 21:11:36 +0100232#define CMD_NOP (7 << 19)
233#define CMD_PRECHARGE (2 << 19)
234#define CMD_REFRESH (1 << 19)
235#define CMD_EMR (0 << 19)
236#define CMD_READ (5 << 19)
237#define CMD_WRITE (4 << 19)
Stefan Roese43f32472007-02-20 10:43:34 +0100238
Stefan Roesebad41112007-03-01 21:11:36 +0100239#define SELECT_MR (0 << 16)
240#define SELECT_EMR (1 << 16)
241#define SELECT_EMR2 (2 << 16)
242#define SELECT_EMR3 (3 << 16)
243
244/* MR */
245#define DLL_RESET 0x00000100
246
247#define WRITE_RECOV_2 (1 << 9)
248#define WRITE_RECOV_3 (2 << 9)
249#define WRITE_RECOV_4 (3 << 9)
250#define WRITE_RECOV_5 (4 << 9)
251#define WRITE_RECOV_6 (5 << 9)
252
253#define BURST_LEN_4 0x00000002
254
255/* EMR */
256#define ODT_0_OHM 0x00000000
257#define ODT_50_OHM 0x00000044
258#define ODT_75_OHM 0x00000004
259#define ODT_150_OHM 0x00000040
260
261#define ODS_FULL 0x00000000
262#define ODS_REDUCED 0x00000002
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700263#define OCD_CALIB_DEF 0x00000380
Stefan Roesebad41112007-03-01 21:11:36 +0100264
265/* defines for ODT (On Die Termination) of the 440SP(e) DDR2 controller */
266#define ODT_EB0R (0x80000000 >> 8)
267#define ODT_EB0W (0x80000000 >> 7)
268#define CALC_ODT_R(n) (ODT_EB0R << (n << 1))
269#define CALC_ODT_W(n) (ODT_EB0W << (n << 1))
270#define CALC_ODT_RW(n) (CALC_ODT_R(n) | CALC_ODT_W(n))
271
Stefan Roese43f32472007-02-20 10:43:34 +0100272/* Defines for the Read Cycle Delay test */
Stefan Roesef88e3602007-03-31 08:46:08 +0200273#define NUMMEMTESTS 8
274#define NUMMEMWORDS 8
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200275#define NUMLOOPS 64 /* memory test loops */
Stefan Roese43f32472007-02-20 10:43:34 +0100276
Stefan Roesebad41112007-03-01 21:11:36 +0100277/*
Stefan Roese0203a972008-07-09 17:33:57 +0200278 * Newer PPC's like 440SPe, 460EX/GT can be equipped with more than 2GB of SDRAM.
279 * To support such configurations, we "only" map the first 2GB via the TLB's. We
280 * need some free virtual address space for the remaining peripherals like, SoC
281 * devices, FLASH etc.
282 *
283 * Note that ECC is currently not supported on configurations with more than 2GB
284 * SDRAM. This is because we only map the first 2GB on such systems, and therefore
285 * the ECC parity byte of the remaining area can't be written.
286 */
Stefan Roese0203a972008-07-09 17:33:57 +0200287
288/*
Heiko Schocher68310b02007-06-25 19:11:37 +0200289 * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
290 */
291void __spd_ddr_init_hang (void)
292{
293 hang ();
294}
295void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang")));
296
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200297/*
298 * To provide an interface for board specific config values in this common
299 * DDR setup code, we implement he "weak" default functions here. They return
300 * the default value back to the caller.
301 *
302 * Please see include/configs/yucca.h for an example fora board specific
303 * implementation.
304 */
305u32 __ddr_wrdtr(u32 default_val)
306{
307 return default_val;
308}
309u32 ddr_wrdtr(u32) __attribute__((weak, alias("__ddr_wrdtr")));
310
311u32 __ddr_clktr(u32 default_val)
312{
313 return default_val;
314}
315u32 ddr_clktr(u32) __attribute__((weak, alias("__ddr_clktr")));
316
Heiko Schocher633e03a2007-06-22 19:11:54 +0200317
Stefan Roese43f32472007-02-20 10:43:34 +0100318/* Private Structure Definitions */
319
320/* enum only to ease code for cas latency setting */
321typedef enum ddr_cas_id {
322 DDR_CAS_2 = 20,
323 DDR_CAS_2_5 = 25,
324 DDR_CAS_3 = 30,
325 DDR_CAS_4 = 40,
326 DDR_CAS_5 = 50
327} ddr_cas_id_t;
328
329/*-----------------------------------------------------------------------------+
330 * Prototypes
331 *-----------------------------------------------------------------------------*/
Stefan Roese43f32472007-02-20 10:43:34 +0100332static void get_spd_info(unsigned long *dimm_populated,
333 unsigned char *iic0_dimm_addr,
334 unsigned long num_dimm_banks);
335static void check_mem_type(unsigned long *dimm_populated,
336 unsigned char *iic0_dimm_addr,
337 unsigned long num_dimm_banks);
338static void check_frequency(unsigned long *dimm_populated,
339 unsigned char *iic0_dimm_addr,
340 unsigned long num_dimm_banks);
341static void check_rank_number(unsigned long *dimm_populated,
342 unsigned char *iic0_dimm_addr,
343 unsigned long num_dimm_banks);
344static void check_voltage_type(unsigned long *dimm_populated,
345 unsigned char *iic0_dimm_addr,
346 unsigned long num_dimm_banks);
347static void program_memory_queue(unsigned long *dimm_populated,
348 unsigned char *iic0_dimm_addr,
349 unsigned long num_dimm_banks);
350static void program_codt(unsigned long *dimm_populated,
351 unsigned char *iic0_dimm_addr,
352 unsigned long num_dimm_banks);
353static void program_mode(unsigned long *dimm_populated,
354 unsigned char *iic0_dimm_addr,
355 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +0100356 ddr_cas_id_t *selected_cas,
357 int *write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100358static void program_tr(unsigned long *dimm_populated,
359 unsigned char *iic0_dimm_addr,
360 unsigned long num_dimm_banks);
361static void program_rtr(unsigned long *dimm_populated,
362 unsigned char *iic0_dimm_addr,
363 unsigned long num_dimm_banks);
364static void program_bxcf(unsigned long *dimm_populated,
365 unsigned char *iic0_dimm_addr,
366 unsigned long num_dimm_banks);
367static void program_copt1(unsigned long *dimm_populated,
368 unsigned char *iic0_dimm_addr,
369 unsigned long num_dimm_banks);
370static void program_initplr(unsigned long *dimm_populated,
371 unsigned char *iic0_dimm_addr,
372 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +0100373 ddr_cas_id_t selected_cas,
Stefan Roesebad41112007-03-01 21:11:36 +0100374 int write_recovery);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100375#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +0100376static void program_ecc(unsigned long *dimm_populated,
377 unsigned char *iic0_dimm_addr,
Stefan Roesebad41112007-03-01 21:11:36 +0100378 unsigned long num_dimm_banks,
379 unsigned long tlb_word2_i_value);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100380#endif
Adam Graham97a55812008-09-03 12:26:59 -0700381#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Stefan Roesebad41112007-03-01 21:11:36 +0100382static void program_DQS_calibration(unsigned long *dimm_populated,
Adam Graham97a55812008-09-03 12:26:59 -0700383 unsigned char *iic0_dimm_addr,
384 unsigned long num_dimm_banks);
Stefan Roese43f32472007-02-20 10:43:34 +0100385#ifdef HARD_CODED_DQS /* calibration test with hardvalues */
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100386static void test(void);
Stefan Roese43f32472007-02-20 10:43:34 +0100387#else
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100388static void DQS_calibration_process(void);
Stefan Roese43f32472007-02-20 10:43:34 +0100389#endif
Adam Graham97a55812008-09-03 12:26:59 -0700390#endif
Stefan Roese43f32472007-02-20 10:43:34 +0100391
Stefan Roese43f32472007-02-20 10:43:34 +0100392static unsigned char spd_read(uchar chip, uint addr)
393{
394 unsigned char data[2];
395
396 if (i2c_probe(chip) == 0)
397 if (i2c_read(chip, addr, 1, data, 1) == 0)
398 return data[0];
399
400 return 0;
401}
402
403/*-----------------------------------------------------------------------------+
Stefan Roese43f32472007-02-20 10:43:34 +0100404 * initdram. Initializes the 440SP Memory Queue and DDR SDRAM controller.
405 * Note: This routine runs from flash with a stack set up in the chip's
406 * sram space. It is important that the routine does not require .sbss, .bss or
407 * .data sections. It also cannot call routines that require these sections.
408 *-----------------------------------------------------------------------------*/
409/*-----------------------------------------------------------------------------
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100410 * Function: initdram
Stefan Roese43f32472007-02-20 10:43:34 +0100411 * Description: Configures SDRAM memory banks for DDR operation.
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100412 * Auto Memory Configuration option reads the DDR SDRAM EEPROMs
413 * via the IIC bus and then configures the DDR SDRAM memory
414 * banks appropriately. If Auto Memory Configuration is
415 * not used, it is assumed that no DIMM is plugged
Stefan Roese43f32472007-02-20 10:43:34 +0100416 *-----------------------------------------------------------------------------*/
Becky Brucebd99ae72008-06-09 16:03:40 -0500417phys_size_t initdram(int board_type)
Stefan Roese43f32472007-02-20 10:43:34 +0100418{
Stefan Roesebad41112007-03-01 21:11:36 +0100419 unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
Felix Radensky389672f2010-01-19 21:19:06 +0200420 unsigned long dimm_populated[MAXDIMMS] = {SDRAM_NONE, SDRAM_NONE};
Stefan Roese4a0f5902008-01-15 10:11:02 +0100421 unsigned long num_dimm_banks; /* on board dimm banks */
Stefan Roese43f32472007-02-20 10:43:34 +0100422 unsigned long val;
Stefan Roese4a0f5902008-01-15 10:11:02 +0100423 ddr_cas_id_t selected_cas = DDR_CAS_5; /* preset to silence compiler */
Stefan Roesebad41112007-03-01 21:11:36 +0100424 int write_recovery;
Stefan Roese0203a972008-07-09 17:33:57 +0200425 phys_size_t dram_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +0100426
Simon Glassa8734272015-02-07 11:51:38 -0700427 if (IS_ENABLED(CONFIG_SYS_RAMBOOT)) {
428 /*
429 * Reduce RAM size to avoid overwriting memory used by
430 * current stack? Not sure what is happening.
431 */
432 return sdram_memsize() / 2;
433 }
434
Stefan Roese43f32472007-02-20 10:43:34 +0100435 num_dimm_banks = sizeof(iic0_dimm_addr);
436
437 /*------------------------------------------------------------------
Stefan Roese43f32472007-02-20 10:43:34 +0100438 * Reset the DDR-SDRAM controller.
439 *-----------------------------------------------------------------*/
Stefan Roese95ca5fa2010-09-11 09:31:43 +0200440 mtsdr(SDR0_SRST, SDR0_SRST0_DMC);
Stefan Roese43f32472007-02-20 10:43:34 +0100441 mtsdr(SDR0_SRST, 0x00000000);
442
443 /*
444 * Make sure I2C controller is initialized
445 * before continuing.
446 */
447
448 /* switch to correct I2C bus */
Dirk Eibach42b204f2013-04-25 02:40:01 +0000449 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
Stefan Roese43f32472007-02-20 10:43:34 +0100450
451 /*------------------------------------------------------------------
452 * Clear out the serial presence detect buffers.
453 * Perform IIC reads from the dimm. Fill in the spds.
454 * Check to see if the dimm slots are populated
455 *-----------------------------------------------------------------*/
456 get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks);
457
458 /*------------------------------------------------------------------
459 * Check the memory type for the dimms plugged.
460 *-----------------------------------------------------------------*/
461 check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
462
463 /*------------------------------------------------------------------
464 * Check the frequency supported for the dimms plugged.
465 *-----------------------------------------------------------------*/
466 check_frequency(dimm_populated, iic0_dimm_addr, num_dimm_banks);
467
468 /*------------------------------------------------------------------
469 * Check the total rank number.
470 *-----------------------------------------------------------------*/
471 check_rank_number(dimm_populated, iic0_dimm_addr, num_dimm_banks);
472
473 /*------------------------------------------------------------------
474 * Check the voltage type for the dimms plugged.
475 *-----------------------------------------------------------------*/
476 check_voltage_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
477
478 /*------------------------------------------------------------------
479 * Program SDRAM controller options 2 register
480 * Except Enabling of the memory controller.
481 *-----------------------------------------------------------------*/
482 mfsdram(SDRAM_MCOPT2, val);
483 mtsdram(SDRAM_MCOPT2,
484 (val &
485 ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_PMEN_MASK |
486 SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_XSRP_MASK |
487 SDRAM_MCOPT2_ISIE_MASK))
488 | (SDRAM_MCOPT2_SREN_ENTER | SDRAM_MCOPT2_PMEN_DISABLE |
489 SDRAM_MCOPT2_IPTR_IDLE | SDRAM_MCOPT2_XSRP_ALLOW |
490 SDRAM_MCOPT2_ISIE_ENABLE));
491
492 /*------------------------------------------------------------------
493 * Program SDRAM controller options 1 register
494 * Note: Does not enable the memory controller.
495 *-----------------------------------------------------------------*/
496 program_copt1(dimm_populated, iic0_dimm_addr, num_dimm_banks);
497
498 /*------------------------------------------------------------------
499 * Set the SDRAM Controller On Die Termination Register
500 *-----------------------------------------------------------------*/
501 program_codt(dimm_populated, iic0_dimm_addr, num_dimm_banks);
502
503 /*------------------------------------------------------------------
504 * Program SDRAM refresh register.
505 *-----------------------------------------------------------------*/
506 program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
507
508 /*------------------------------------------------------------------
509 * Program SDRAM mode register.
510 *-----------------------------------------------------------------*/
Stefan Roesebad41112007-03-01 21:11:36 +0100511 program_mode(dimm_populated, iic0_dimm_addr, num_dimm_banks,
512 &selected_cas, &write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100513
514 /*------------------------------------------------------------------
515 * Set the SDRAM Write Data/DM/DQS Clock Timing Reg
516 *-----------------------------------------------------------------*/
517 mfsdram(SDRAM_WRDTR, val);
518 mtsdram(SDRAM_WRDTR, (val & ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK)) |
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200519 ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC | SDRAM_WRDTR_WTR_90_DEG_ADV));
Stefan Roese43f32472007-02-20 10:43:34 +0100520
521 /*------------------------------------------------------------------
522 * Set the SDRAM Clock Timing Register
523 *-----------------------------------------------------------------*/
524 mfsdram(SDRAM_CLKTR, val);
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200525 mtsdram(SDRAM_CLKTR, (val & ~SDRAM_CLKTR_CLKP_MASK) |
526 ddr_clktr(SDRAM_CLKTR_CLKP_0_DEG));
Stefan Roese43f32472007-02-20 10:43:34 +0100527
528 /*------------------------------------------------------------------
529 * Program the BxCF registers.
530 *-----------------------------------------------------------------*/
531 program_bxcf(dimm_populated, iic0_dimm_addr, num_dimm_banks);
532
533 /*------------------------------------------------------------------
534 * Program SDRAM timing registers.
535 *-----------------------------------------------------------------*/
536 program_tr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
537
538 /*------------------------------------------------------------------
539 * Set the Extended Mode register
540 *-----------------------------------------------------------------*/
541 mfsdram(SDRAM_MEMODE, val);
542 mtsdram(SDRAM_MEMODE,
543 (val & ~(SDRAM_MEMODE_DIC_MASK | SDRAM_MEMODE_DLL_MASK |
544 SDRAM_MEMODE_RTT_MASK | SDRAM_MEMODE_DQS_MASK)) |
545 (SDRAM_MEMODE_DIC_NORMAL | SDRAM_MEMODE_DLL_ENABLE
Stefan Roeseb39ef632007-03-08 10:06:09 +0100546 | SDRAM_MEMODE_RTT_150OHM | SDRAM_MEMODE_DQS_ENABLE));
Stefan Roese43f32472007-02-20 10:43:34 +0100547
548 /*------------------------------------------------------------------
549 * Program Initialization preload registers.
550 *-----------------------------------------------------------------*/
551 program_initplr(dimm_populated, iic0_dimm_addr, num_dimm_banks,
Stefan Roesebad41112007-03-01 21:11:36 +0100552 selected_cas, write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100553
554 /*------------------------------------------------------------------
555 * Delay to ensure 200usec have elapsed since reset.
556 *-----------------------------------------------------------------*/
557 udelay(400);
558
559 /*------------------------------------------------------------------
560 * Set the memory queue core base addr.
561 *-----------------------------------------------------------------*/
562 program_memory_queue(dimm_populated, iic0_dimm_addr, num_dimm_banks);
563
564 /*------------------------------------------------------------------
565 * Program SDRAM controller options 2 register
566 * Enable the memory controller.
567 *-----------------------------------------------------------------*/
568 mfsdram(SDRAM_MCOPT2, val);
569 mtsdram(SDRAM_MCOPT2,
570 (val & ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_DCEN_MASK |
571 SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_ISIE_MASK)) |
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700572 SDRAM_MCOPT2_IPTR_EXECUTE);
Stefan Roese43f32472007-02-20 10:43:34 +0100573
574 /*------------------------------------------------------------------
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700575 * Wait for IPTR_EXECUTE init sequence to complete.
Stefan Roese43f32472007-02-20 10:43:34 +0100576 *-----------------------------------------------------------------*/
577 do {
578 mfsdram(SDRAM_MCSTAT, val);
579 } while ((val & SDRAM_MCSTAT_MIC_MASK) == SDRAM_MCSTAT_MIC_NOTCOMP);
580
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700581 /* enable the controller only after init sequence completes */
582 mfsdram(SDRAM_MCOPT2, val);
583 mtsdram(SDRAM_MCOPT2, (val | SDRAM_MCOPT2_DCEN_ENABLE));
584
585 /* Make sure delay-line calibration is done before proceeding */
586 do {
587 mfsdram(SDRAM_DLCR, val);
588 } while (!(val & SDRAM_DLCR_DLCS_COMPLETE));
589
Stefan Roese43f32472007-02-20 10:43:34 +0100590 /* get installed memory size */
591 dram_size = sdram_memsize();
592
Stefan Roese0203a972008-07-09 17:33:57 +0200593 /*
594 * Limit size to 2GB
595 */
596 if (dram_size > CONFIG_MAX_MEM_MAPPED)
597 dram_size = CONFIG_MAX_MEM_MAPPED;
598
Stefan Roese43f32472007-02-20 10:43:34 +0100599 /* and program tlb entries for this size (dynamic) */
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200600
601 /*
602 * Program TLB entries with caches enabled, for best performace
603 * while auto-calibrating and ECC generation
604 */
605 program_tlb(0, 0, dram_size, 0);
Stefan Roese43f32472007-02-20 10:43:34 +0100606
Stefan Roese43f32472007-02-20 10:43:34 +0100607 /*------------------------------------------------------------------
Stefan Roesebad41112007-03-01 21:11:36 +0100608 * DQS calibration.
Stefan Roese43f32472007-02-20 10:43:34 +0100609 *-----------------------------------------------------------------*/
Adam Graham97a55812008-09-03 12:26:59 -0700610#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
611 DQS_autocalibration();
612#else
Stefan Roesebad41112007-03-01 21:11:36 +0100613 program_DQS_calibration(dimm_populated, iic0_dimm_addr, num_dimm_banks);
Adam Graham97a55812008-09-03 12:26:59 -0700614#endif
Stefan Roesec3bcfce2010-05-25 15:33:14 +0200615 /*
616 * Now complete RDSS configuration as mentioned on page 7 of the AMCC
617 * PowerPC440SP/SPe DDR2 application note:
618 * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
619 */
620 update_rdcc();
Stefan Roese43f32472007-02-20 10:43:34 +0100621
Stefan Roeseb39ef632007-03-08 10:06:09 +0100622#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +0100623 /*------------------------------------------------------------------
Stefan Roesebad41112007-03-01 21:11:36 +0100624 * If ecc is enabled, initialize the parity bits.
Stefan Roese43f32472007-02-20 10:43:34 +0100625 *-----------------------------------------------------------------*/
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200626 program_ecc(dimm_populated, iic0_dimm_addr, num_dimm_banks, 0);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100627#endif
Stefan Roese43f32472007-02-20 10:43:34 +0100628
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200629 /*
Stefan Roesefae16c92011-09-16 12:54:58 +0200630 * Flush the dcache before removing the TLB with caches
631 * enabled. Otherwise this might lead to problems later on,
632 * e.g. while booting Linux (as seen on ICON-440SPe).
633 */
634 flush_dcache();
635
636 /*
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200637 * Now after initialization (auto-calibration and ECC generation)
638 * remove the TLB entries with caches enabled and program again with
639 * desired cache functionality
640 */
641 remove_tlb(0, dram_size);
642 program_tlb(0, 0, dram_size, MY_TLB_WORD2_I_ENABLE);
643
Grant Erickson9416cd92008-07-09 16:46:35 -0700644 ppc4xx_ibm_ddr2_register_dump();
Stefan Roese43f32472007-02-20 10:43:34 +0100645
Stefan Roesebdd13d12008-03-11 15:05:26 +0100646 /*
647 * Clear potential errors resulting from auto-calibration.
648 * If not done, then we could get an interrupt later on when
649 * exceptions are enabled.
650 */
651 set_mcsr(get_mcsr());
652
Stefan Roese0203a972008-07-09 17:33:57 +0200653 return sdram_memsize();
Stefan Roese43f32472007-02-20 10:43:34 +0100654}
655
656static void get_spd_info(unsigned long *dimm_populated,
657 unsigned char *iic0_dimm_addr,
658 unsigned long num_dimm_banks)
659{
660 unsigned long dimm_num;
661 unsigned long dimm_found;
662 unsigned char num_of_bytes;
663 unsigned char total_size;
664
York Sun4a598092013-04-01 11:29:11 -0700665 dimm_found = false;
Stefan Roese43f32472007-02-20 10:43:34 +0100666 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
667 num_of_bytes = 0;
668 total_size = 0;
669
670 num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
671 debug("\nspd_read(0x%x) returned %d\n",
672 iic0_dimm_addr[dimm_num], num_of_bytes);
673 total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
674 debug("spd_read(0x%x) returned %d\n",
675 iic0_dimm_addr[dimm_num], total_size);
676
677 if ((num_of_bytes != 0) && (total_size != 0)) {
York Sun4a598092013-04-01 11:29:11 -0700678 dimm_populated[dimm_num] = true;
679 dimm_found = true;
Stefan Roese43f32472007-02-20 10:43:34 +0100680 debug("DIMM slot %lu: populated\n", dimm_num);
681 } else {
York Sun4a598092013-04-01 11:29:11 -0700682 dimm_populated[dimm_num] = false;
Stefan Roese43f32472007-02-20 10:43:34 +0100683 debug("DIMM slot %lu: Not populated\n", dimm_num);
684 }
685 }
686
York Sun4a598092013-04-01 11:29:11 -0700687 if (dimm_found == false) {
Stefan Roese43f32472007-02-20 10:43:34 +0100688 printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200689 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100690 }
691}
692
Stefan Roese43f32472007-02-20 10:43:34 +0100693
694/*------------------------------------------------------------------
695 * For the memory DIMMs installed, this routine verifies that they
696 * really are DDR specific DIMMs.
697 *-----------------------------------------------------------------*/
698static void check_mem_type(unsigned long *dimm_populated,
699 unsigned char *iic0_dimm_addr,
700 unsigned long num_dimm_banks)
701{
702 unsigned long dimm_num;
703 unsigned long dimm_type;
704
705 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
York Sun4a598092013-04-01 11:29:11 -0700706 if (dimm_populated[dimm_num] == true) {
Stefan Roese43f32472007-02-20 10:43:34 +0100707 dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2);
708 switch (dimm_type) {
709 case 1:
710 printf("ERROR: Standard Fast Page Mode DRAM DIMM detected in "
711 "slot %d.\n", (unsigned int)dimm_num);
712 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
713 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200714 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100715 break;
716 case 2:
717 printf("ERROR: EDO DIMM detected in slot %d.\n",
718 (unsigned int)dimm_num);
719 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
720 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200721 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100722 break;
723 case 3:
724 printf("ERROR: Pipelined Nibble DIMM detected in slot %d.\n",
725 (unsigned int)dimm_num);
726 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
727 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200728 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100729 break;
730 case 4:
731 printf("ERROR: SDRAM DIMM detected in slot %d.\n",
732 (unsigned int)dimm_num);
733 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
734 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200735 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100736 break;
737 case 5:
738 printf("ERROR: Multiplexed ROM DIMM detected in slot %d.\n",
739 (unsigned int)dimm_num);
740 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
741 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200742 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100743 break;
744 case 6:
745 printf("ERROR: SGRAM DIMM detected in slot %d.\n",
746 (unsigned int)dimm_num);
747 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
748 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200749 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100750 break;
751 case 7:
Felix Radensky8d4d4a62009-07-01 11:37:46 +0300752 debug("DIMM slot %lu: DDR1 SDRAM detected\n", dimm_num);
Stefan Roese43f32472007-02-20 10:43:34 +0100753 dimm_populated[dimm_num] = SDRAM_DDR1;
754 break;
755 case 8:
Felix Radensky8d4d4a62009-07-01 11:37:46 +0300756 debug("DIMM slot %lu: DDR2 SDRAM detected\n", dimm_num);
Stefan Roese43f32472007-02-20 10:43:34 +0100757 dimm_populated[dimm_num] = SDRAM_DDR2;
758 break;
759 default:
760 printf("ERROR: Unknown DIMM detected in slot %d.\n",
761 (unsigned int)dimm_num);
762 printf("Only DDR1 and DDR2 SDRAM DIMMs are supported.\n");
763 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200764 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100765 break;
766 }
767 }
768 }
769 for (dimm_num = 1; dimm_num < num_dimm_banks; dimm_num++) {
770 if ((dimm_populated[dimm_num-1] != SDRAM_NONE)
771 && (dimm_populated[dimm_num] != SDRAM_NONE)
772 && (dimm_populated[dimm_num-1] != dimm_populated[dimm_num])) {
773 printf("ERROR: DIMM's DDR1 and DDR2 type can not be mixed.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200774 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100775 }
776 }
777}
778
779/*------------------------------------------------------------------
780 * For the memory DIMMs installed, this routine verifies that
781 * frequency previously calculated is supported.
782 *-----------------------------------------------------------------*/
783static void check_frequency(unsigned long *dimm_populated,
784 unsigned char *iic0_dimm_addr,
785 unsigned long num_dimm_banks)
786{
787 unsigned long dimm_num;
788 unsigned long tcyc_reg;
789 unsigned long cycle_time;
790 unsigned long calc_cycle_time;
791 unsigned long sdram_freq;
792 unsigned long sdr_ddrpll;
Stefan Roeseedd73f22007-10-21 08:12:41 +0200793 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +0100794
795 /*------------------------------------------------------------------
796 * Get the board configuration info.
797 *-----------------------------------------------------------------*/
798 get_sys_info(&board_cfg);
799
Stefan Roeseb39ef632007-03-08 10:06:09 +0100800 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +0100801 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
802
803 /*
804 * calc_cycle_time is calculated from DDR frequency set by board/chip
805 * and is expressed in multiple of 10 picoseconds
806 * to match the way DIMM cycle time is calculated below.
807 */
808 calc_cycle_time = MULDIV64(ONE_BILLION, 100, sdram_freq);
809
810 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
811 if (dimm_populated[dimm_num] != SDRAM_NONE) {
812 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
813 /*
814 * Byte 9, Cycle time for CAS Latency=X, is split into two nibbles:
815 * the higher order nibble (bits 4-7) designates the cycle time
816 * to a granularity of 1ns;
817 * the value presented by the lower order nibble (bits 0-3)
818 * has a granularity of .1ns and is added to the value designated
819 * by the higher nibble. In addition, four lines of the lower order
820 * nibble are assigned to support +.25,+.33, +.66 and +.75.
821 */
822 /* Convert from hex to decimal */
823 if ((tcyc_reg & 0x0F) == 0x0D)
824 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
825 else if ((tcyc_reg & 0x0F) == 0x0C)
826 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 66;
827 else if ((tcyc_reg & 0x0F) == 0x0B)
828 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 33;
829 else if ((tcyc_reg & 0x0F) == 0x0A)
830 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 25;
831 else
832 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) +
833 ((tcyc_reg & 0x0F)*10);
Felix Radensky8d4d4a62009-07-01 11:37:46 +0300834 debug("cycle_time=%lu [10 picoseconds]\n", cycle_time);
Stefan Roese43f32472007-02-20 10:43:34 +0100835
836 if (cycle_time > (calc_cycle_time + 10)) {
837 /*
838 * the provided sdram cycle_time is too small
839 * for the available DIMM cycle_time.
840 * The additionnal 100ps is here to accept a small incertainty.
841 */
842 printf("ERROR: DRAM DIMM detected with cycle_time %d ps in "
843 "slot %d \n while calculated cycle time is %d ps.\n",
844 (unsigned int)(cycle_time*10),
845 (unsigned int)dimm_num,
846 (unsigned int)(calc_cycle_time*10));
847 printf("Replace the DIMM, or change DDR frequency via "
848 "strapping bits.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200849 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100850 }
851 }
852 }
853}
854
855/*------------------------------------------------------------------
856 * For the memory DIMMs installed, this routine verifies two
857 * ranks/banks maximum are availables.
858 *-----------------------------------------------------------------*/
859static void check_rank_number(unsigned long *dimm_populated,
860 unsigned char *iic0_dimm_addr,
861 unsigned long num_dimm_banks)
862{
863 unsigned long dimm_num;
864 unsigned long dimm_rank;
865 unsigned long total_rank = 0;
866
867 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
868 if (dimm_populated[dimm_num] != SDRAM_NONE) {
869 dimm_rank = spd_read(iic0_dimm_addr[dimm_num], 5);
870 if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
871 dimm_rank = (dimm_rank & 0x0F) +1;
872 else
873 dimm_rank = dimm_rank & 0x0F;
874
875
876 if (dimm_rank > MAXRANKS) {
Stefan Roese251161b2008-07-10 09:58:06 +0200877 printf("ERROR: DRAM DIMM detected with %lu ranks in "
878 "slot %lu is not supported.\n", dimm_rank, dimm_num);
Stefan Roese43f32472007-02-20 10:43:34 +0100879 printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
880 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200881 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100882 } else
883 total_rank += dimm_rank;
884 }
885 if (total_rank > MAXRANKS) {
886 printf("ERROR: DRAM DIMM detected with a total of %d ranks "
887 "for all slots.\n", (unsigned int)total_rank);
888 printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
889 printf("Remove one of the DIMM modules.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200890 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100891 }
892 }
893}
894
895/*------------------------------------------------------------------
896 * only support 2.5V modules.
897 * This routine verifies this.
898 *-----------------------------------------------------------------*/
899static void check_voltage_type(unsigned long *dimm_populated,
900 unsigned char *iic0_dimm_addr,
901 unsigned long num_dimm_banks)
902{
903 unsigned long dimm_num;
904 unsigned long voltage_type;
905
906 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
907 if (dimm_populated[dimm_num] != SDRAM_NONE) {
908 voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
909 switch (voltage_type) {
910 case 0x00:
911 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
912 printf("This DIMM is 5.0 Volt/TTL.\n");
913 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
914 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200915 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100916 break;
917 case 0x01:
918 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
919 printf("This DIMM is LVTTL.\n");
920 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
921 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200922 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100923 break;
924 case 0x02:
925 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
926 printf("This DIMM is 1.5 Volt.\n");
927 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
928 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200929 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100930 break;
931 case 0x03:
932 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
933 printf("This DIMM is 3.3 Volt/TTL.\n");
934 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
935 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200936 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100937 break;
938 case 0x04:
939 /* 2.5 Voltage only for DDR1 */
940 break;
941 case 0x05:
942 /* 1.8 Voltage only for DDR2 */
943 break;
944 default:
945 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
946 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
947 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200948 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100949 break;
950 }
951 }
952 }
953}
954
955/*-----------------------------------------------------------------------------+
956 * program_copt1.
957 *-----------------------------------------------------------------------------*/
958static void program_copt1(unsigned long *dimm_populated,
959 unsigned char *iic0_dimm_addr,
960 unsigned long num_dimm_banks)
961{
962 unsigned long dimm_num;
963 unsigned long mcopt1;
964 unsigned long ecc_enabled;
965 unsigned long ecc = 0;
966 unsigned long data_width = 0;
967 unsigned long dimm_32bit;
968 unsigned long dimm_64bit;
969 unsigned long registered = 0;
970 unsigned long attribute = 0;
971 unsigned long buf0, buf1; /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
972 unsigned long bankcount;
Stefan Roese43f32472007-02-20 10:43:34 +0100973 unsigned long val;
974
Stefan Roeseb39ef632007-03-08 10:06:09 +0100975#ifdef CONFIG_DDR_ECC
York Sun4a598092013-04-01 11:29:11 -0700976 ecc_enabled = true;
Stefan Roeseb39ef632007-03-08 10:06:09 +0100977#else
York Sun4a598092013-04-01 11:29:11 -0700978 ecc_enabled = false;
Stefan Roeseb39ef632007-03-08 10:06:09 +0100979#endif
York Sun4a598092013-04-01 11:29:11 -0700980 dimm_32bit = false;
981 dimm_64bit = false;
982 buf0 = false;
983 buf1 = false;
Stefan Roese43f32472007-02-20 10:43:34 +0100984
985 /*------------------------------------------------------------------
986 * Set memory controller options reg 1, SDRAM_MCOPT1.
987 *-----------------------------------------------------------------*/
988 mfsdram(SDRAM_MCOPT1, val);
989 mcopt1 = val & ~(SDRAM_MCOPT1_MCHK_MASK | SDRAM_MCOPT1_RDEN_MASK |
990 SDRAM_MCOPT1_PMU_MASK | SDRAM_MCOPT1_DMWD_MASK |
991 SDRAM_MCOPT1_UIOS_MASK | SDRAM_MCOPT1_BCNT_MASK |
992 SDRAM_MCOPT1_DDR_TYPE_MASK | SDRAM_MCOPT1_RWOO_MASK |
993 SDRAM_MCOPT1_WOOO_MASK | SDRAM_MCOPT1_DCOO_MASK |
994 SDRAM_MCOPT1_DREF_MASK);
995
996 mcopt1 |= SDRAM_MCOPT1_QDEP;
997 mcopt1 |= SDRAM_MCOPT1_PMU_OPEN;
998 mcopt1 |= SDRAM_MCOPT1_RWOO_DISABLED;
999 mcopt1 |= SDRAM_MCOPT1_WOOO_DISABLED;
1000 mcopt1 |= SDRAM_MCOPT1_DCOO_DISABLED;
1001 mcopt1 |= SDRAM_MCOPT1_DREF_NORMAL;
1002
1003 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1004 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1005 /* test ecc support */
1006 ecc = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 11);
1007 if (ecc != 0x02) /* ecc not supported */
York Sun4a598092013-04-01 11:29:11 -07001008 ecc_enabled = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001009
1010 /* test bank count */
1011 bankcount = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 17);
1012 if (bankcount == 0x04) /* bank count = 4 */
1013 mcopt1 |= SDRAM_MCOPT1_4_BANKS;
1014 else /* bank count = 8 */
1015 mcopt1 |= SDRAM_MCOPT1_8_BANKS;
1016
Stefan Roese43f32472007-02-20 10:43:34 +01001017 /* test for buffered/unbuffered, registered, differential clocks */
1018 registered = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 20);
1019 attribute = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 21);
1020
1021 /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
1022 if (dimm_num == 0) {
1023 if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
1024 mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
1025 if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
1026 mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
1027 if (registered == 1) { /* DDR2 always buffered */
1028 /* TODO: what about above comments ? */
1029 mcopt1 |= SDRAM_MCOPT1_RDEN;
York Sun4a598092013-04-01 11:29:11 -07001030 buf0 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001031 } else {
1032 /* TODO: the mask 0x02 doesn't match Samsung def for byte 21. */
1033 if ((attribute & 0x02) == 0x00) {
1034 /* buffered not supported */
York Sun4a598092013-04-01 11:29:11 -07001035 buf0 = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001036 } else {
1037 mcopt1 |= SDRAM_MCOPT1_RDEN;
York Sun4a598092013-04-01 11:29:11 -07001038 buf0 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001039 }
1040 }
1041 }
1042 else if (dimm_num == 1) {
1043 if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
1044 mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
1045 if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
1046 mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
1047 if (registered == 1) {
1048 /* DDR2 always buffered */
1049 mcopt1 |= SDRAM_MCOPT1_RDEN;
York Sun4a598092013-04-01 11:29:11 -07001050 buf1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001051 } else {
1052 if ((attribute & 0x02) == 0x00) {
1053 /* buffered not supported */
York Sun4a598092013-04-01 11:29:11 -07001054 buf1 = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001055 } else {
1056 mcopt1 |= SDRAM_MCOPT1_RDEN;
York Sun4a598092013-04-01 11:29:11 -07001057 buf1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001058 }
1059 }
1060 }
1061
1062 /* Note that for DDR2 the byte 7 is reserved, but OK to keep code as is. */
1063 data_width = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 6) +
1064 (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 7)) << 8);
1065
1066 switch (data_width) {
1067 case 72:
1068 case 64:
York Sun4a598092013-04-01 11:29:11 -07001069 dimm_64bit = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001070 break;
1071 case 40:
1072 case 32:
York Sun4a598092013-04-01 11:29:11 -07001073 dimm_32bit = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001074 break;
1075 default:
Stefan Roese251161b2008-07-10 09:58:06 +02001076 printf("WARNING: Detected a DIMM with a data width of %lu bits.\n",
Stefan Roese43f32472007-02-20 10:43:34 +01001077 data_width);
1078 printf("Only DIMMs with 32 or 64 bit DDR-SDRAM widths are supported.\n");
1079 break;
1080 }
1081 }
1082 }
1083
1084 /* verify matching properties */
1085 if ((dimm_populated[0] != SDRAM_NONE) && (dimm_populated[1] != SDRAM_NONE)) {
1086 if (buf0 != buf1) {
1087 printf("ERROR: DIMM's buffered/unbuffered, registered, clocking don't match.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001088 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001089 }
1090 }
1091
York Sun4a598092013-04-01 11:29:11 -07001092 if ((dimm_64bit == true) && (dimm_32bit == true)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001093 printf("ERROR: Cannot mix 32 bit and 64 bit DDR-SDRAM DIMMs together.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001094 spd_ddr_init_hang ();
York Sun4a598092013-04-01 11:29:11 -07001095 } else if ((dimm_64bit == true) && (dimm_32bit == false)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001096 mcopt1 |= SDRAM_MCOPT1_DMWD_64;
York Sun4a598092013-04-01 11:29:11 -07001097 } else if ((dimm_64bit == false) && (dimm_32bit == true)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001098 mcopt1 |= SDRAM_MCOPT1_DMWD_32;
1099 } else {
1100 printf("ERROR: Please install only 32 or 64 bit DDR-SDRAM DIMMs.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001101 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001102 }
1103
York Sun4a598092013-04-01 11:29:11 -07001104 if (ecc_enabled == true)
Stefan Roese43f32472007-02-20 10:43:34 +01001105 mcopt1 |= SDRAM_MCOPT1_MCHK_GEN;
1106 else
1107 mcopt1 |= SDRAM_MCOPT1_MCHK_NON;
1108
1109 mtsdram(SDRAM_MCOPT1, mcopt1);
1110}
1111
1112/*-----------------------------------------------------------------------------+
1113 * program_codt.
1114 *-----------------------------------------------------------------------------*/
1115static void program_codt(unsigned long *dimm_populated,
1116 unsigned char *iic0_dimm_addr,
1117 unsigned long num_dimm_banks)
1118{
1119 unsigned long codt;
1120 unsigned long modt0 = 0;
1121 unsigned long modt1 = 0;
1122 unsigned long modt2 = 0;
1123 unsigned long modt3 = 0;
1124 unsigned char dimm_num;
1125 unsigned char dimm_rank;
1126 unsigned char total_rank = 0;
1127 unsigned char total_dimm = 0;
1128 unsigned char dimm_type = 0;
1129 unsigned char firstSlot = 0;
1130
1131 /*------------------------------------------------------------------
1132 * Set the SDRAM Controller On Die Termination Register
1133 *-----------------------------------------------------------------*/
1134 mfsdram(SDRAM_CODT, codt);
Carolyn Smith5b648422009-02-12 06:13:44 +01001135 codt &= ~(SDRAM_CODT_DQS_SINGLE_END | SDRAM_CODT_CKSE_SINGLE_END);
1136 codt |= SDRAM_CODT_IO_NMODE;
Stefan Roese43f32472007-02-20 10:43:34 +01001137
1138 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1139 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1140 dimm_rank = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 5);
1141 if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08) {
1142 dimm_rank = (dimm_rank & 0x0F) + 1;
1143 dimm_type = SDRAM_DDR2;
1144 } else {
1145 dimm_rank = dimm_rank & 0x0F;
1146 dimm_type = SDRAM_DDR1;
1147 }
1148
Stefan Roesebad41112007-03-01 21:11:36 +01001149 total_rank += dimm_rank;
1150 total_dimm++;
Stefan Roese43f32472007-02-20 10:43:34 +01001151 if ((dimm_num == 0) && (total_dimm == 1))
York Sun4a598092013-04-01 11:29:11 -07001152 firstSlot = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001153 else
York Sun4a598092013-04-01 11:29:11 -07001154 firstSlot = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001155 }
1156 }
1157 if (dimm_type == SDRAM_DDR2) {
1158 codt |= SDRAM_CODT_DQS_1_8_V_DDR2;
York Sun4a598092013-04-01 11:29:11 -07001159 if ((total_dimm == 1) && (firstSlot == true)) {
Stefan Roese37628252008-08-06 14:05:38 +02001160 if (total_rank == 1) { /* PUUU */
Stefan Roesebad41112007-03-01 21:11:36 +01001161 codt |= CALC_ODT_R(0);
1162 modt0 = CALC_ODT_W(0);
Stefan Roese43f32472007-02-20 10:43:34 +01001163 modt1 = 0x00000000;
1164 modt2 = 0x00000000;
1165 modt3 = 0x00000000;
1166 }
Stefan Roese37628252008-08-06 14:05:38 +02001167 if (total_rank == 2) { /* PPUU */
Stefan Roesebad41112007-03-01 21:11:36 +01001168 codt |= CALC_ODT_R(0) | CALC_ODT_R(1);
Stefan Roese37628252008-08-06 14:05:38 +02001169 modt0 = CALC_ODT_W(0) | CALC_ODT_W(1);
1170 modt1 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001171 modt2 = 0x00000000;
1172 modt3 = 0x00000000;
1173 }
York Sun4a598092013-04-01 11:29:11 -07001174 } else if ((total_dimm == 1) && (firstSlot != true)) {
Stefan Roese37628252008-08-06 14:05:38 +02001175 if (total_rank == 1) { /* UUPU */
Stefan Roesebad41112007-03-01 21:11:36 +01001176 codt |= CALC_ODT_R(2);
1177 modt0 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001178 modt1 = 0x00000000;
Stefan Roesebad41112007-03-01 21:11:36 +01001179 modt2 = CALC_ODT_W(2);
Stefan Roese43f32472007-02-20 10:43:34 +01001180 modt3 = 0x00000000;
1181 }
Stefan Roese37628252008-08-06 14:05:38 +02001182 if (total_rank == 2) { /* UUPP */
Stefan Roesebad41112007-03-01 21:11:36 +01001183 codt |= CALC_ODT_R(2) | CALC_ODT_R(3);
1184 modt0 = 0x00000000;
1185 modt1 = 0x00000000;
Stefan Roese37628252008-08-06 14:05:38 +02001186 modt2 = CALC_ODT_W(2) | CALC_ODT_W(3);
1187 modt3 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001188 }
1189 }
1190 if (total_dimm == 2) {
Stefan Roese37628252008-08-06 14:05:38 +02001191 if (total_rank == 2) { /* PUPU */
Stefan Roesebad41112007-03-01 21:11:36 +01001192 codt |= CALC_ODT_R(0) | CALC_ODT_R(2);
1193 modt0 = CALC_ODT_RW(2);
Stefan Roese43f32472007-02-20 10:43:34 +01001194 modt1 = 0x00000000;
Stefan Roesebad41112007-03-01 21:11:36 +01001195 modt2 = CALC_ODT_RW(0);
Stefan Roese43f32472007-02-20 10:43:34 +01001196 modt3 = 0x00000000;
1197 }
Stefan Roese37628252008-08-06 14:05:38 +02001198 if (total_rank == 4) { /* PPPP */
Stefan Roese32a1cad2007-06-01 13:45:00 +02001199 codt |= CALC_ODT_R(0) | CALC_ODT_R(1) |
1200 CALC_ODT_R(2) | CALC_ODT_R(3);
Stefan Roese37628252008-08-06 14:05:38 +02001201 modt0 = CALC_ODT_RW(2) | CALC_ODT_RW(3);
Stefan Roesebad41112007-03-01 21:11:36 +01001202 modt1 = 0x00000000;
Stefan Roese37628252008-08-06 14:05:38 +02001203 modt2 = CALC_ODT_RW(0) | CALC_ODT_RW(1);
Stefan Roesebad41112007-03-01 21:11:36 +01001204 modt3 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001205 }
1206 }
Wolfgang Denkf972e772007-03-04 01:36:05 +01001207 } else {
Stefan Roese43f32472007-02-20 10:43:34 +01001208 codt |= SDRAM_CODT_DQS_2_5_V_DDR1;
1209 modt0 = 0x00000000;
1210 modt1 = 0x00000000;
1211 modt2 = 0x00000000;
1212 modt3 = 0x00000000;
1213
1214 if (total_dimm == 1) {
1215 if (total_rank == 1)
1216 codt |= 0x00800000;
1217 if (total_rank == 2)
1218 codt |= 0x02800000;
1219 }
1220 if (total_dimm == 2) {
1221 if (total_rank == 2)
1222 codt |= 0x08800000;
1223 if (total_rank == 4)
1224 codt |= 0x2a800000;
1225 }
1226 }
1227
1228 debug("nb of dimm %d\n", total_dimm);
1229 debug("nb of rank %d\n", total_rank);
1230 if (total_dimm == 1)
1231 debug("dimm in slot %d\n", firstSlot);
1232
1233 mtsdram(SDRAM_CODT, codt);
1234 mtsdram(SDRAM_MODT0, modt0);
1235 mtsdram(SDRAM_MODT1, modt1);
1236 mtsdram(SDRAM_MODT2, modt2);
1237 mtsdram(SDRAM_MODT3, modt3);
1238}
1239
1240/*-----------------------------------------------------------------------------+
1241 * program_initplr.
1242 *-----------------------------------------------------------------------------*/
1243static void program_initplr(unsigned long *dimm_populated,
1244 unsigned char *iic0_dimm_addr,
1245 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +01001246 ddr_cas_id_t selected_cas,
Stefan Roesebad41112007-03-01 21:11:36 +01001247 int write_recovery)
Stefan Roese43f32472007-02-20 10:43:34 +01001248{
Stefan Roesebad41112007-03-01 21:11:36 +01001249 u32 cas = 0;
1250 u32 odt = 0;
1251 u32 ods = 0;
1252 u32 mr;
1253 u32 wr;
1254 u32 emr;
1255 u32 emr2;
1256 u32 emr3;
1257 int dimm_num;
1258 int total_dimm = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01001259
1260 /******************************************************
1261 ** Assumption: if more than one DIMM, all DIMMs are the same
Wolfgang Denk52232fd2007-02-27 14:26:04 +01001262 ** as already checked in check_memory_type
Stefan Roese43f32472007-02-20 10:43:34 +01001263 ******************************************************/
1264
1265 if ((dimm_populated[0] == SDRAM_DDR1) || (dimm_populated[1] == SDRAM_DDR1)) {
1266 mtsdram(SDRAM_INITPLR0, 0x81B80000);
1267 mtsdram(SDRAM_INITPLR1, 0x81900400);
1268 mtsdram(SDRAM_INITPLR2, 0x81810000);
1269 mtsdram(SDRAM_INITPLR3, 0xff800162);
1270 mtsdram(SDRAM_INITPLR4, 0x81900400);
1271 mtsdram(SDRAM_INITPLR5, 0x86080000);
1272 mtsdram(SDRAM_INITPLR6, 0x86080000);
1273 mtsdram(SDRAM_INITPLR7, 0x81000062);
1274 } else if ((dimm_populated[0] == SDRAM_DDR2) || (dimm_populated[1] == SDRAM_DDR2)) {
1275 switch (selected_cas) {
Stefan Roese43f32472007-02-20 10:43:34 +01001276 case DDR_CAS_3:
Stefan Roesebad41112007-03-01 21:11:36 +01001277 cas = 3 << 4;
Stefan Roese43f32472007-02-20 10:43:34 +01001278 break;
1279 case DDR_CAS_4:
Stefan Roesebad41112007-03-01 21:11:36 +01001280 cas = 4 << 4;
Stefan Roese43f32472007-02-20 10:43:34 +01001281 break;
1282 case DDR_CAS_5:
Stefan Roesebad41112007-03-01 21:11:36 +01001283 cas = 5 << 4;
1284 break;
1285 default:
1286 printf("ERROR: ucode error on selected_cas value %d", selected_cas);
Heiko Schocher68310b02007-06-25 19:11:37 +02001287 spd_ddr_init_hang ();
Stefan Roesebad41112007-03-01 21:11:36 +01001288 break;
1289 }
1290
1291#if 0
1292 /*
1293 * ToDo - Still a problem with the write recovery:
1294 * On the Corsair CM2X512-5400C4 module, setting write recovery
1295 * in the INITPLR reg to the value calculated in program_mode()
1296 * results in not correctly working DDR2 memory (crash after
1297 * relocation).
1298 *
1299 * So for now, set the write recovery to 3. This seems to work
1300 * on the Corair module too.
1301 *
1302 * 2007-03-01, sr
1303 */
1304 switch (write_recovery) {
1305 case 3:
1306 wr = WRITE_RECOV_3;
1307 break;
1308 case 4:
1309 wr = WRITE_RECOV_4;
1310 break;
1311 case 5:
1312 wr = WRITE_RECOV_5;
1313 break;
1314 case 6:
1315 wr = WRITE_RECOV_6;
Stefan Roese43f32472007-02-20 10:43:34 +01001316 break;
1317 default:
Stefan Roesebad41112007-03-01 21:11:36 +01001318 printf("ERROR: write recovery not support (%d)", write_recovery);
Heiko Schocher68310b02007-06-25 19:11:37 +02001319 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001320 break;
1321 }
Stefan Roesebad41112007-03-01 21:11:36 +01001322#else
1323 wr = WRITE_RECOV_3; /* test-only, see description above */
1324#endif
1325
1326 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++)
1327 if (dimm_populated[dimm_num] != SDRAM_NONE)
1328 total_dimm++;
1329 if (total_dimm == 1) {
1330 odt = ODT_150_OHM;
1331 ods = ODS_FULL;
1332 } else if (total_dimm == 2) {
1333 odt = ODT_75_OHM;
1334 ods = ODS_REDUCED;
1335 } else {
1336 printf("ERROR: Unsupported number of DIMM's (%d)", total_dimm);
Heiko Schocher68310b02007-06-25 19:11:37 +02001337 spd_ddr_init_hang ();
Stefan Roesebad41112007-03-01 21:11:36 +01001338 }
Stefan Roese43f32472007-02-20 10:43:34 +01001339
Stefan Roesebad41112007-03-01 21:11:36 +01001340 mr = CMD_EMR | SELECT_MR | BURST_LEN_4 | wr | cas;
1341 emr = CMD_EMR | SELECT_EMR | odt | ods;
1342 emr2 = CMD_EMR | SELECT_EMR2;
1343 emr3 = CMD_EMR | SELECT_EMR3;
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001344 /* NOP - Wait 106 MemClk cycles */
1345 mtsdram(SDRAM_INITPLR0, SDRAM_INITPLR_ENABLE | CMD_NOP |
1346 SDRAM_INITPLR_IMWT_ENCODE(106));
Stefan Roesebad41112007-03-01 21:11:36 +01001347 udelay(1000);
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001348 /* precharge 4 MemClk cycles */
1349 mtsdram(SDRAM_INITPLR1, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
1350 SDRAM_INITPLR_IMWT_ENCODE(4));
1351 /* EMR2 - Wait tMRD (2 MemClk cycles) */
1352 mtsdram(SDRAM_INITPLR2, SDRAM_INITPLR_ENABLE | emr2 |
1353 SDRAM_INITPLR_IMWT_ENCODE(2));
1354 /* EMR3 - Wait tMRD (2 MemClk cycles) */
1355 mtsdram(SDRAM_INITPLR3, SDRAM_INITPLR_ENABLE | emr3 |
1356 SDRAM_INITPLR_IMWT_ENCODE(2));
1357 /* EMR DLL ENABLE - Wait tMRD (2 MemClk cycles) */
1358 mtsdram(SDRAM_INITPLR4, SDRAM_INITPLR_ENABLE | emr |
1359 SDRAM_INITPLR_IMWT_ENCODE(2));
1360 /* MR w/ DLL reset - 200 cycle wait for DLL reset */
1361 mtsdram(SDRAM_INITPLR5, SDRAM_INITPLR_ENABLE | mr | DLL_RESET |
1362 SDRAM_INITPLR_IMWT_ENCODE(200));
Stefan Roesebad41112007-03-01 21:11:36 +01001363 udelay(1000);
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001364 /* precharge 4 MemClk cycles */
1365 mtsdram(SDRAM_INITPLR6, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
1366 SDRAM_INITPLR_IMWT_ENCODE(4));
1367 /* Refresh 25 MemClk cycles */
1368 mtsdram(SDRAM_INITPLR7, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1369 SDRAM_INITPLR_IMWT_ENCODE(25));
1370 /* Refresh 25 MemClk cycles */
1371 mtsdram(SDRAM_INITPLR8, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1372 SDRAM_INITPLR_IMWT_ENCODE(25));
1373 /* Refresh 25 MemClk cycles */
1374 mtsdram(SDRAM_INITPLR9, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1375 SDRAM_INITPLR_IMWT_ENCODE(25));
1376 /* Refresh 25 MemClk cycles */
1377 mtsdram(SDRAM_INITPLR10, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1378 SDRAM_INITPLR_IMWT_ENCODE(25));
1379 /* MR w/o DLL reset - Wait tMRD (2 MemClk cycles) */
1380 mtsdram(SDRAM_INITPLR11, SDRAM_INITPLR_ENABLE | mr |
1381 SDRAM_INITPLR_IMWT_ENCODE(2));
1382 /* EMR OCD Default - Wait tMRD (2 MemClk cycles) */
1383 mtsdram(SDRAM_INITPLR12, SDRAM_INITPLR_ENABLE | OCD_CALIB_DEF |
1384 SDRAM_INITPLR_IMWT_ENCODE(2) | emr);
1385 /* EMR OCD Exit */
1386 mtsdram(SDRAM_INITPLR13, SDRAM_INITPLR_ENABLE | emr |
1387 SDRAM_INITPLR_IMWT_ENCODE(2));
Stefan Roese43f32472007-02-20 10:43:34 +01001388 } else {
1389 printf("ERROR: ucode error as unknown DDR type in program_initplr");
Heiko Schocher68310b02007-06-25 19:11:37 +02001390 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001391 }
1392}
1393
1394/*------------------------------------------------------------------
1395 * This routine programs the SDRAM_MMODE register.
1396 * the selected_cas is an output parameter, that will be passed
1397 * by caller to call the above program_initplr( )
1398 *-----------------------------------------------------------------*/
1399static void program_mode(unsigned long *dimm_populated,
1400 unsigned char *iic0_dimm_addr,
1401 unsigned long num_dimm_banks,
Stefan Roesebad41112007-03-01 21:11:36 +01001402 ddr_cas_id_t *selected_cas,
1403 int *write_recovery)
Stefan Roese43f32472007-02-20 10:43:34 +01001404{
1405 unsigned long dimm_num;
1406 unsigned long sdram_ddr1;
1407 unsigned long t_wr_ns;
1408 unsigned long t_wr_clk;
1409 unsigned long cas_bit;
1410 unsigned long cas_index;
1411 unsigned long sdram_freq;
1412 unsigned long ddr_check;
1413 unsigned long mmode;
1414 unsigned long tcyc_reg;
1415 unsigned long cycle_2_0_clk;
1416 unsigned long cycle_2_5_clk;
1417 unsigned long cycle_3_0_clk;
1418 unsigned long cycle_4_0_clk;
1419 unsigned long cycle_5_0_clk;
1420 unsigned long max_2_0_tcyc_ns_x_100;
1421 unsigned long max_2_5_tcyc_ns_x_100;
1422 unsigned long max_3_0_tcyc_ns_x_100;
1423 unsigned long max_4_0_tcyc_ns_x_100;
1424 unsigned long max_5_0_tcyc_ns_x_100;
1425 unsigned long cycle_time_ns_x_100[3];
Stefan Roeseedd73f22007-10-21 08:12:41 +02001426 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001427 unsigned char cas_2_0_available;
1428 unsigned char cas_2_5_available;
1429 unsigned char cas_3_0_available;
1430 unsigned char cas_4_0_available;
1431 unsigned char cas_5_0_available;
1432 unsigned long sdr_ddrpll;
1433
1434 /*------------------------------------------------------------------
1435 * Get the board configuration info.
1436 *-----------------------------------------------------------------*/
1437 get_sys_info(&board_cfg);
1438
Stefan Roeseb39ef632007-03-08 10:06:09 +01001439 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001440 sdram_freq = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(sdr_ddrpll), 1);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03001441 debug("sdram_freq=%lu\n", sdram_freq);
Stefan Roese43f32472007-02-20 10:43:34 +01001442
1443 /*------------------------------------------------------------------
1444 * Handle the timing. We need to find the worst case timing of all
1445 * the dimm modules installed.
1446 *-----------------------------------------------------------------*/
1447 t_wr_ns = 0;
York Sun4a598092013-04-01 11:29:11 -07001448 cas_2_0_available = true;
1449 cas_2_5_available = true;
1450 cas_3_0_available = true;
1451 cas_4_0_available = true;
1452 cas_5_0_available = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001453 max_2_0_tcyc_ns_x_100 = 10;
1454 max_2_5_tcyc_ns_x_100 = 10;
1455 max_3_0_tcyc_ns_x_100 = 10;
1456 max_4_0_tcyc_ns_x_100 = 10;
1457 max_5_0_tcyc_ns_x_100 = 10;
York Sun4a598092013-04-01 11:29:11 -07001458 sdram_ddr1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001459
1460 /* loop through all the DIMM slots on the board */
1461 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1462 /* If a dimm is installed in a particular slot ... */
1463 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1464 if (dimm_populated[dimm_num] == SDRAM_DDR1)
York Sun4a598092013-04-01 11:29:11 -07001465 sdram_ddr1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001466 else
York Sun4a598092013-04-01 11:29:11 -07001467 sdram_ddr1 = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001468
Stefan Roese43f32472007-02-20 10:43:34 +01001469 cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03001470 debug("cas_bit[SPD byte 18]=%02lx\n", cas_bit);
Stefan Roese43f32472007-02-20 10:43:34 +01001471
1472 /* For a particular DIMM, grab the three CAS values it supports */
1473 for (cas_index = 0; cas_index < 3; cas_index++) {
1474 switch (cas_index) {
1475 case 0:
1476 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
1477 break;
1478 case 1:
1479 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23);
1480 break;
1481 default:
1482 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25);
1483 break;
1484 }
1485
1486 if ((tcyc_reg & 0x0F) >= 10) {
1487 if ((tcyc_reg & 0x0F) == 0x0D) {
1488 /* Convert from hex to decimal */
Stefan Roese5d48a842007-03-31 13:15:06 +02001489 cycle_time_ns_x_100[cas_index] =
1490 (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
Stefan Roese43f32472007-02-20 10:43:34 +01001491 } else {
1492 printf("ERROR: SPD reported Tcyc is incorrect for DIMM "
1493 "in slot %d\n", (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +02001494 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001495 }
1496 } else {
1497 /* Convert from hex to decimal */
Stefan Roese5d48a842007-03-31 13:15:06 +02001498 cycle_time_ns_x_100[cas_index] =
1499 (((tcyc_reg & 0xF0) >> 4) * 100) +
Stefan Roese43f32472007-02-20 10:43:34 +01001500 ((tcyc_reg & 0x0F)*10);
1501 }
Felix Radensky8d4d4a62009-07-01 11:37:46 +03001502 debug("cas_index=%lu: cycle_time_ns_x_100=%lu\n", cas_index,
Stefan Roese5d48a842007-03-31 13:15:06 +02001503 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001504 }
1505
1506 /* The rest of this routine determines if CAS 2.0, 2.5, 3.0, 4.0 and 5.0 are */
1507 /* supported for a particular DIMM. */
1508 cas_index = 0;
1509
1510 if (sdram_ddr1) {
1511 /*
1512 * DDR devices use the following bitmask for CAS latency:
1513 * Bit 7 6 5 4 3 2 1 0
1514 * TBD 4.0 3.5 3.0 2.5 2.0 1.5 1.0
1515 */
Stefan Roese5d48a842007-03-31 13:15:06 +02001516 if (((cas_bit & 0x40) == 0x40) && (cas_index < 3) &&
1517 (cycle_time_ns_x_100[cas_index] != 0)) {
1518 max_4_0_tcyc_ns_x_100 = max(max_4_0_tcyc_ns_x_100,
1519 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001520 cas_index++;
1521 } else {
1522 if (cas_index != 0)
1523 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001524 cas_4_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001525 }
1526
Stefan Roese5d48a842007-03-31 13:15:06 +02001527 if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
1528 (cycle_time_ns_x_100[cas_index] != 0)) {
1529 max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
1530 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001531 cas_index++;
1532 } else {
1533 if (cas_index != 0)
1534 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001535 cas_3_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001536 }
1537
Stefan Roese5d48a842007-03-31 13:15:06 +02001538 if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
1539 (cycle_time_ns_x_100[cas_index] != 0)) {
1540 max_2_5_tcyc_ns_x_100 = max(max_2_5_tcyc_ns_x_100,
1541 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001542 cas_index++;
1543 } else {
1544 if (cas_index != 0)
1545 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001546 cas_2_5_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001547 }
1548
Stefan Roese5d48a842007-03-31 13:15:06 +02001549 if (((cas_bit & 0x04) == 0x04) && (cas_index < 3) &&
1550 (cycle_time_ns_x_100[cas_index] != 0)) {
1551 max_2_0_tcyc_ns_x_100 = max(max_2_0_tcyc_ns_x_100,
1552 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001553 cas_index++;
1554 } else {
1555 if (cas_index != 0)
1556 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001557 cas_2_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001558 }
1559 } else {
1560 /*
1561 * DDR2 devices use the following bitmask for CAS latency:
1562 * Bit 7 6 5 4 3 2 1 0
1563 * TBD 6.0 5.0 4.0 3.0 2.0 TBD TBD
1564 */
Stefan Roese5d48a842007-03-31 13:15:06 +02001565 if (((cas_bit & 0x20) == 0x20) && (cas_index < 3) &&
1566 (cycle_time_ns_x_100[cas_index] != 0)) {
1567 max_5_0_tcyc_ns_x_100 = max(max_5_0_tcyc_ns_x_100,
1568 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001569 cas_index++;
1570 } else {
1571 if (cas_index != 0)
1572 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001573 cas_5_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001574 }
1575
Stefan Roese5d48a842007-03-31 13:15:06 +02001576 if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
1577 (cycle_time_ns_x_100[cas_index] != 0)) {
1578 max_4_0_tcyc_ns_x_100 = max(max_4_0_tcyc_ns_x_100,
1579 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001580 cas_index++;
1581 } else {
1582 if (cas_index != 0)
1583 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001584 cas_4_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001585 }
1586
Stefan Roese5d48a842007-03-31 13:15:06 +02001587 if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
1588 (cycle_time_ns_x_100[cas_index] != 0)) {
1589 max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
1590 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001591 cas_index++;
1592 } else {
1593 if (cas_index != 0)
1594 cas_index++;
York Sun4a598092013-04-01 11:29:11 -07001595 cas_3_0_available = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001596 }
1597 }
1598 }
1599 }
1600
1601 /*------------------------------------------------------------------
1602 * Set the SDRAM mode, SDRAM_MMODE
1603 *-----------------------------------------------------------------*/
1604 mfsdram(SDRAM_MMODE, mmode);
1605 mmode = mmode & ~(SDRAM_MMODE_WR_MASK | SDRAM_MMODE_DCL_MASK);
1606
Stefan Roeseb39ef632007-03-08 10:06:09 +01001607 /* add 10 here because of rounding problems */
1608 cycle_2_0_clk = MULDIV64(ONE_BILLION, 100, max_2_0_tcyc_ns_x_100) + 10;
1609 cycle_2_5_clk = MULDIV64(ONE_BILLION, 100, max_2_5_tcyc_ns_x_100) + 10;
1610 cycle_3_0_clk = MULDIV64(ONE_BILLION, 100, max_3_0_tcyc_ns_x_100) + 10;
1611 cycle_4_0_clk = MULDIV64(ONE_BILLION, 100, max_4_0_tcyc_ns_x_100) + 10;
1612 cycle_5_0_clk = MULDIV64(ONE_BILLION, 100, max_5_0_tcyc_ns_x_100) + 10;
Felix Radensky8d4d4a62009-07-01 11:37:46 +03001613 debug("cycle_3_0_clk=%lu\n", cycle_3_0_clk);
1614 debug("cycle_4_0_clk=%lu\n", cycle_4_0_clk);
1615 debug("cycle_5_0_clk=%lu\n", cycle_5_0_clk);
Stefan Roese43f32472007-02-20 10:43:34 +01001616
York Sun4a598092013-04-01 11:29:11 -07001617 if (sdram_ddr1 == true) { /* DDR1 */
1618 if ((cas_2_0_available == true) &&
1619 (sdram_freq <= cycle_2_0_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001620 mmode |= SDRAM_MMODE_DCL_DDR1_2_0_CLK;
1621 *selected_cas = DDR_CAS_2;
York Sun4a598092013-04-01 11:29:11 -07001622 } else if ((cas_2_5_available == true) &&
1623 (sdram_freq <= cycle_2_5_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001624 mmode |= SDRAM_MMODE_DCL_DDR1_2_5_CLK;
1625 *selected_cas = DDR_CAS_2_5;
York Sun4a598092013-04-01 11:29:11 -07001626 } else if ((cas_3_0_available == true) &&
1627 (sdram_freq <= cycle_3_0_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001628 mmode |= SDRAM_MMODE_DCL_DDR1_3_0_CLK;
1629 *selected_cas = DDR_CAS_3;
1630 } else {
1631 printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
1632 printf("Only DIMMs DDR1 with CAS latencies of 2.0, 2.5, and 3.0 are supported.\n");
1633 printf("Make sure the PLB speed is within the supported range of the DIMMs.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001634 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001635 }
1636 } else { /* DDR2 */
Stefan Roesef88e3602007-03-31 08:46:08 +02001637 debug("cas_3_0_available=%d\n", cas_3_0_available);
1638 debug("cas_4_0_available=%d\n", cas_4_0_available);
1639 debug("cas_5_0_available=%d\n", cas_5_0_available);
York Sun4a598092013-04-01 11:29:11 -07001640 if ((cas_3_0_available == true) &&
1641 (sdram_freq <= cycle_3_0_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001642 mmode |= SDRAM_MMODE_DCL_DDR2_3_0_CLK;
1643 *selected_cas = DDR_CAS_3;
York Sun4a598092013-04-01 11:29:11 -07001644 } else if ((cas_4_0_available == true) &&
1645 (sdram_freq <= cycle_4_0_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001646 mmode |= SDRAM_MMODE_DCL_DDR2_4_0_CLK;
1647 *selected_cas = DDR_CAS_4;
York Sun4a598092013-04-01 11:29:11 -07001648 } else if ((cas_5_0_available == true) &&
1649 (sdram_freq <= cycle_5_0_clk)) {
Stefan Roese43f32472007-02-20 10:43:34 +01001650 mmode |= SDRAM_MMODE_DCL_DDR2_5_0_CLK;
1651 *selected_cas = DDR_CAS_5;
1652 } else {
1653 printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
1654 printf("Only DIMMs DDR2 with CAS latencies of 3.0, 4.0, and 5.0 are supported.\n");
Stefan Roeseb39ef632007-03-08 10:06:09 +01001655 printf("Make sure the PLB speed is within the supported range of the DIMMs.\n");
1656 printf("cas3=%d cas4=%d cas5=%d\n",
1657 cas_3_0_available, cas_4_0_available, cas_5_0_available);
Stefan Roese251161b2008-07-10 09:58:06 +02001658 printf("sdram_freq=%lu cycle3=%lu cycle4=%lu cycle5=%lu\n\n",
Stefan Roeseb39ef632007-03-08 10:06:09 +01001659 sdram_freq, cycle_3_0_clk, cycle_4_0_clk, cycle_5_0_clk);
Heiko Schocher68310b02007-06-25 19:11:37 +02001660 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001661 }
1662 }
1663
York Sun4a598092013-04-01 11:29:11 -07001664 if (sdram_ddr1 == true)
Stefan Roese43f32472007-02-20 10:43:34 +01001665 mmode |= SDRAM_MMODE_WR_DDR1;
1666 else {
1667
1668 /* loop through all the DIMM slots on the board */
1669 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1670 /* If a dimm is installed in a particular slot ... */
1671 if (dimm_populated[dimm_num] != SDRAM_NONE)
Masahiro Yamadadb204642014-11-07 03:03:31 +09001672 t_wr_ns = max(t_wr_ns, (unsigned long)
Stefan Roese43f32472007-02-20 10:43:34 +01001673 spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
1674 }
1675
1676 /*
1677 * convert from nanoseconds to ddr clocks
1678 * round up if necessary
1679 */
1680 t_wr_clk = MULDIV64(sdram_freq, t_wr_ns, ONE_BILLION);
1681 ddr_check = MULDIV64(ONE_BILLION, t_wr_clk, t_wr_ns);
1682 if (sdram_freq != ddr_check)
1683 t_wr_clk++;
1684
1685 switch (t_wr_clk) {
1686 case 0:
1687 case 1:
1688 case 2:
1689 case 3:
1690 mmode |= SDRAM_MMODE_WR_DDR2_3_CYC;
1691 break;
1692 case 4:
1693 mmode |= SDRAM_MMODE_WR_DDR2_4_CYC;
1694 break;
1695 case 5:
1696 mmode |= SDRAM_MMODE_WR_DDR2_5_CYC;
1697 break;
1698 default:
1699 mmode |= SDRAM_MMODE_WR_DDR2_6_CYC;
1700 break;
1701 }
Stefan Roesebad41112007-03-01 21:11:36 +01001702 *write_recovery = t_wr_clk;
Stefan Roese43f32472007-02-20 10:43:34 +01001703 }
1704
Stefan Roesebad41112007-03-01 21:11:36 +01001705 debug("CAS latency = %d\n", *selected_cas);
1706 debug("Write recovery = %d\n", *write_recovery);
1707
Stefan Roese43f32472007-02-20 10:43:34 +01001708 mtsdram(SDRAM_MMODE, mmode);
1709}
1710
1711/*-----------------------------------------------------------------------------+
1712 * program_rtr.
1713 *-----------------------------------------------------------------------------*/
1714static void program_rtr(unsigned long *dimm_populated,
1715 unsigned char *iic0_dimm_addr,
1716 unsigned long num_dimm_banks)
1717{
Stefan Roeseedd73f22007-10-21 08:12:41 +02001718 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001719 unsigned long max_refresh_rate;
1720 unsigned long dimm_num;
1721 unsigned long refresh_rate_type;
1722 unsigned long refresh_rate;
1723 unsigned long rint;
1724 unsigned long sdram_freq;
1725 unsigned long sdr_ddrpll;
1726 unsigned long val;
1727
1728 /*------------------------------------------------------------------
1729 * Get the board configuration info.
1730 *-----------------------------------------------------------------*/
1731 get_sys_info(&board_cfg);
1732
1733 /*------------------------------------------------------------------
1734 * Set the SDRAM Refresh Timing Register, SDRAM_RTR
1735 *-----------------------------------------------------------------*/
Stefan Roeseb39ef632007-03-08 10:06:09 +01001736 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001737 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
1738
1739 max_refresh_rate = 0;
1740 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1741 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1742
1743 refresh_rate_type = spd_read(iic0_dimm_addr[dimm_num], 12);
1744 refresh_rate_type &= 0x7F;
1745 switch (refresh_rate_type) {
1746 case 0:
1747 refresh_rate = 15625;
1748 break;
1749 case 1:
1750 refresh_rate = 3906;
1751 break;
1752 case 2:
1753 refresh_rate = 7812;
1754 break;
1755 case 3:
1756 refresh_rate = 31250;
1757 break;
1758 case 4:
1759 refresh_rate = 62500;
1760 break;
1761 case 5:
1762 refresh_rate = 125000;
1763 break;
1764 default:
1765 refresh_rate = 0;
1766 printf("ERROR: DIMM %d unsupported refresh rate/type.\n",
1767 (unsigned int)dimm_num);
1768 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001769 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001770 break;
1771 }
1772
1773 max_refresh_rate = max(max_refresh_rate, refresh_rate);
1774 }
1775 }
1776
1777 rint = MULDIV64(sdram_freq, max_refresh_rate, ONE_BILLION);
1778 mfsdram(SDRAM_RTR, val);
1779 mtsdram(SDRAM_RTR, (val & ~SDRAM_RTR_RINT_MASK) |
1780 (SDRAM_RTR_RINT_ENCODE(rint)));
1781}
1782
1783/*------------------------------------------------------------------
1784 * This routine programs the SDRAM_TRx registers.
1785 *-----------------------------------------------------------------*/
1786static void program_tr(unsigned long *dimm_populated,
1787 unsigned char *iic0_dimm_addr,
1788 unsigned long num_dimm_banks)
1789{
1790 unsigned long dimm_num;
1791 unsigned long sdram_ddr1;
1792 unsigned long t_rp_ns;
1793 unsigned long t_rcd_ns;
1794 unsigned long t_rrd_ns;
1795 unsigned long t_ras_ns;
1796 unsigned long t_rc_ns;
1797 unsigned long t_rfc_ns;
1798 unsigned long t_wpc_ns;
1799 unsigned long t_wtr_ns;
1800 unsigned long t_rpc_ns;
1801 unsigned long t_rp_clk;
1802 unsigned long t_rcd_clk;
1803 unsigned long t_rrd_clk;
1804 unsigned long t_ras_clk;
1805 unsigned long t_rc_clk;
1806 unsigned long t_rfc_clk;
1807 unsigned long t_wpc_clk;
1808 unsigned long t_wtr_clk;
1809 unsigned long t_rpc_clk;
1810 unsigned long sdtr1, sdtr2, sdtr3;
1811 unsigned long ddr_check;
1812 unsigned long sdram_freq;
1813 unsigned long sdr_ddrpll;
1814
Stefan Roeseedd73f22007-10-21 08:12:41 +02001815 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001816
1817 /*------------------------------------------------------------------
1818 * Get the board configuration info.
1819 *-----------------------------------------------------------------*/
1820 get_sys_info(&board_cfg);
1821
Stefan Roeseb39ef632007-03-08 10:06:09 +01001822 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001823 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
1824
1825 /*------------------------------------------------------------------
1826 * Handle the timing. We need to find the worst case timing of all
1827 * the dimm modules installed.
1828 *-----------------------------------------------------------------*/
1829 t_rp_ns = 0;
1830 t_rrd_ns = 0;
1831 t_rcd_ns = 0;
1832 t_ras_ns = 0;
1833 t_rc_ns = 0;
1834 t_rfc_ns = 0;
1835 t_wpc_ns = 0;
1836 t_wtr_ns = 0;
1837 t_rpc_ns = 0;
York Sun4a598092013-04-01 11:29:11 -07001838 sdram_ddr1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001839
1840 /* loop through all the DIMM slots on the board */
1841 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1842 /* If a dimm is installed in a particular slot ... */
1843 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1844 if (dimm_populated[dimm_num] == SDRAM_DDR2)
York Sun4a598092013-04-01 11:29:11 -07001845 sdram_ddr1 = true;
Stefan Roese43f32472007-02-20 10:43:34 +01001846 else
York Sun4a598092013-04-01 11:29:11 -07001847 sdram_ddr1 = false;
Stefan Roese43f32472007-02-20 10:43:34 +01001848
Masahiro Yamadadb204642014-11-07 03:03:31 +09001849 t_rcd_ns = max(t_rcd_ns,
1850 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 29) >> 2);
1851 t_rrd_ns = max(t_rrd_ns,
1852 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 28) >> 2);
1853 t_rp_ns = max(t_rp_ns,
1854 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 27) >> 2);
1855 t_ras_ns = max(t_ras_ns,
1856 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 30));
1857 t_rc_ns = max(t_rc_ns,
1858 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 41));
1859 t_rfc_ns = max(t_rfc_ns,
1860 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 42));
Stefan Roese43f32472007-02-20 10:43:34 +01001861 }
1862 }
1863
1864 /*------------------------------------------------------------------
1865 * Set the SDRAM Timing Reg 1, SDRAM_TR1
1866 *-----------------------------------------------------------------*/
1867 mfsdram(SDRAM_SDTR1, sdtr1);
1868 sdtr1 &= ~(SDRAM_SDTR1_LDOF_MASK | SDRAM_SDTR1_RTW_MASK |
1869 SDRAM_SDTR1_WTWO_MASK | SDRAM_SDTR1_RTRO_MASK);
1870
1871 /* default values */
1872 sdtr1 |= SDRAM_SDTR1_LDOF_2_CLK;
1873 sdtr1 |= SDRAM_SDTR1_RTW_2_CLK;
1874
1875 /* normal operations */
1876 sdtr1 |= SDRAM_SDTR1_WTWO_0_CLK;
1877 sdtr1 |= SDRAM_SDTR1_RTRO_1_CLK;
1878
1879 mtsdram(SDRAM_SDTR1, sdtr1);
1880
1881 /*------------------------------------------------------------------
1882 * Set the SDRAM Timing Reg 2, SDRAM_TR2
1883 *-----------------------------------------------------------------*/
1884 mfsdram(SDRAM_SDTR2, sdtr2);
1885 sdtr2 &= ~(SDRAM_SDTR2_RCD_MASK | SDRAM_SDTR2_WTR_MASK |
1886 SDRAM_SDTR2_XSNR_MASK | SDRAM_SDTR2_WPC_MASK |
1887 SDRAM_SDTR2_RPC_MASK | SDRAM_SDTR2_RP_MASK |
1888 SDRAM_SDTR2_RRD_MASK);
1889
1890 /*
1891 * convert t_rcd from nanoseconds to ddr clocks
1892 * round up if necessary
1893 */
1894 t_rcd_clk = MULDIV64(sdram_freq, t_rcd_ns, ONE_BILLION);
1895 ddr_check = MULDIV64(ONE_BILLION, t_rcd_clk, t_rcd_ns);
1896 if (sdram_freq != ddr_check)
1897 t_rcd_clk++;
1898
1899 switch (t_rcd_clk) {
1900 case 0:
1901 case 1:
1902 sdtr2 |= SDRAM_SDTR2_RCD_1_CLK;
1903 break;
1904 case 2:
1905 sdtr2 |= SDRAM_SDTR2_RCD_2_CLK;
1906 break;
1907 case 3:
1908 sdtr2 |= SDRAM_SDTR2_RCD_3_CLK;
1909 break;
1910 case 4:
1911 sdtr2 |= SDRAM_SDTR2_RCD_4_CLK;
1912 break;
1913 default:
1914 sdtr2 |= SDRAM_SDTR2_RCD_5_CLK;
1915 break;
1916 }
1917
York Sun4a598092013-04-01 11:29:11 -07001918 if (sdram_ddr1 == true) { /* DDR1 */
Stefan Roese43f32472007-02-20 10:43:34 +01001919 if (sdram_freq < 200000000) {
1920 sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
1921 sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
1922 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
1923 } else {
1924 sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
1925 sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
1926 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
1927 }
1928 } else { /* DDR2 */
1929 /* loop through all the DIMM slots on the board */
1930 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1931 /* If a dimm is installed in a particular slot ... */
1932 if (dimm_populated[dimm_num] != SDRAM_NONE) {
Masahiro Yamadadb204642014-11-07 03:03:31 +09001933 t_wpc_ns = max(t_wtr_ns,
1934 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
1935 t_wtr_ns = max(t_wtr_ns,
1936 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 37) >> 2);
1937 t_rpc_ns = max(t_rpc_ns,
1938 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 38) >> 2);
Stefan Roese43f32472007-02-20 10:43:34 +01001939 }
1940 }
1941
1942 /*
1943 * convert from nanoseconds to ddr clocks
1944 * round up if necessary
1945 */
1946 t_wpc_clk = MULDIV64(sdram_freq, t_wpc_ns, ONE_BILLION);
1947 ddr_check = MULDIV64(ONE_BILLION, t_wpc_clk, t_wpc_ns);
1948 if (sdram_freq != ddr_check)
1949 t_wpc_clk++;
1950
1951 switch (t_wpc_clk) {
1952 case 0:
1953 case 1:
1954 case 2:
1955 sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
1956 break;
1957 case 3:
1958 sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
1959 break;
1960 case 4:
1961 sdtr2 |= SDRAM_SDTR2_WPC_4_CLK;
1962 break;
1963 case 5:
1964 sdtr2 |= SDRAM_SDTR2_WPC_5_CLK;
1965 break;
1966 default:
1967 sdtr2 |= SDRAM_SDTR2_WPC_6_CLK;
1968 break;
1969 }
1970
1971 /*
1972 * convert from nanoseconds to ddr clocks
1973 * round up if necessary
1974 */
1975 t_wtr_clk = MULDIV64(sdram_freq, t_wtr_ns, ONE_BILLION);
1976 ddr_check = MULDIV64(ONE_BILLION, t_wtr_clk, t_wtr_ns);
1977 if (sdram_freq != ddr_check)
1978 t_wtr_clk++;
1979
1980 switch (t_wtr_clk) {
1981 case 0:
1982 case 1:
1983 sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
1984 break;
1985 case 2:
1986 sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
1987 break;
1988 case 3:
1989 sdtr2 |= SDRAM_SDTR2_WTR_3_CLK;
1990 break;
1991 default:
1992 sdtr2 |= SDRAM_SDTR2_WTR_4_CLK;
1993 break;
1994 }
1995
1996 /*
1997 * convert from nanoseconds to ddr clocks
1998 * round up if necessary
1999 */
2000 t_rpc_clk = MULDIV64(sdram_freq, t_rpc_ns, ONE_BILLION);
2001 ddr_check = MULDIV64(ONE_BILLION, t_rpc_clk, t_rpc_ns);
2002 if (sdram_freq != ddr_check)
2003 t_rpc_clk++;
2004
2005 switch (t_rpc_clk) {
2006 case 0:
2007 case 1:
2008 case 2:
2009 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
2010 break;
2011 case 3:
2012 sdtr2 |= SDRAM_SDTR2_RPC_3_CLK;
2013 break;
2014 default:
2015 sdtr2 |= SDRAM_SDTR2_RPC_4_CLK;
2016 break;
2017 }
2018 }
2019
2020 /* default value */
2021 sdtr2 |= SDRAM_SDTR2_XSNR_16_CLK;
2022
2023 /*
2024 * convert t_rrd from nanoseconds to ddr clocks
2025 * round up if necessary
2026 */
2027 t_rrd_clk = MULDIV64(sdram_freq, t_rrd_ns, ONE_BILLION);
2028 ddr_check = MULDIV64(ONE_BILLION, t_rrd_clk, t_rrd_ns);
2029 if (sdram_freq != ddr_check)
2030 t_rrd_clk++;
2031
2032 if (t_rrd_clk == 3)
2033 sdtr2 |= SDRAM_SDTR2_RRD_3_CLK;
2034 else
2035 sdtr2 |= SDRAM_SDTR2_RRD_2_CLK;
2036
2037 /*
2038 * convert t_rp from nanoseconds to ddr clocks
2039 * round up if necessary
2040 */
2041 t_rp_clk = MULDIV64(sdram_freq, t_rp_ns, ONE_BILLION);
2042 ddr_check = MULDIV64(ONE_BILLION, t_rp_clk, t_rp_ns);
2043 if (sdram_freq != ddr_check)
2044 t_rp_clk++;
2045
2046 switch (t_rp_clk) {
2047 case 0:
2048 case 1:
2049 case 2:
2050 case 3:
2051 sdtr2 |= SDRAM_SDTR2_RP_3_CLK;
2052 break;
2053 case 4:
2054 sdtr2 |= SDRAM_SDTR2_RP_4_CLK;
2055 break;
2056 case 5:
2057 sdtr2 |= SDRAM_SDTR2_RP_5_CLK;
2058 break;
2059 case 6:
2060 sdtr2 |= SDRAM_SDTR2_RP_6_CLK;
2061 break;
2062 default:
2063 sdtr2 |= SDRAM_SDTR2_RP_7_CLK;
2064 break;
2065 }
2066
2067 mtsdram(SDRAM_SDTR2, sdtr2);
2068
2069 /*------------------------------------------------------------------
2070 * Set the SDRAM Timing Reg 3, SDRAM_TR3
2071 *-----------------------------------------------------------------*/
2072 mfsdram(SDRAM_SDTR3, sdtr3);
2073 sdtr3 &= ~(SDRAM_SDTR3_RAS_MASK | SDRAM_SDTR3_RC_MASK |
2074 SDRAM_SDTR3_XCS_MASK | SDRAM_SDTR3_RFC_MASK);
2075
2076 /*
2077 * convert t_ras from nanoseconds to ddr clocks
2078 * round up if necessary
2079 */
2080 t_ras_clk = MULDIV64(sdram_freq, t_ras_ns, ONE_BILLION);
2081 ddr_check = MULDIV64(ONE_BILLION, t_ras_clk, t_ras_ns);
2082 if (sdram_freq != ddr_check)
2083 t_ras_clk++;
2084
2085 sdtr3 |= SDRAM_SDTR3_RAS_ENCODE(t_ras_clk);
2086
2087 /*
2088 * convert t_rc from nanoseconds to ddr clocks
2089 * round up if necessary
2090 */
2091 t_rc_clk = MULDIV64(sdram_freq, t_rc_ns, ONE_BILLION);
2092 ddr_check = MULDIV64(ONE_BILLION, t_rc_clk, t_rc_ns);
2093 if (sdram_freq != ddr_check)
2094 t_rc_clk++;
2095
2096 sdtr3 |= SDRAM_SDTR3_RC_ENCODE(t_rc_clk);
2097
2098 /* default xcs value */
2099 sdtr3 |= SDRAM_SDTR3_XCS;
2100
2101 /*
2102 * convert t_rfc from nanoseconds to ddr clocks
2103 * round up if necessary
2104 */
2105 t_rfc_clk = MULDIV64(sdram_freq, t_rfc_ns, ONE_BILLION);
2106 ddr_check = MULDIV64(ONE_BILLION, t_rfc_clk, t_rfc_ns);
2107 if (sdram_freq != ddr_check)
2108 t_rfc_clk++;
2109
2110 sdtr3 |= SDRAM_SDTR3_RFC_ENCODE(t_rfc_clk);
2111
2112 mtsdram(SDRAM_SDTR3, sdtr3);
2113}
2114
2115/*-----------------------------------------------------------------------------+
2116 * program_bxcf.
2117 *-----------------------------------------------------------------------------*/
2118static void program_bxcf(unsigned long *dimm_populated,
2119 unsigned char *iic0_dimm_addr,
2120 unsigned long num_dimm_banks)
2121{
2122 unsigned long dimm_num;
2123 unsigned long num_col_addr;
2124 unsigned long num_ranks;
2125 unsigned long num_banks;
2126 unsigned long mode;
2127 unsigned long ind_rank;
2128 unsigned long ind;
2129 unsigned long ind_bank;
2130 unsigned long bank_0_populated;
2131
2132 /*------------------------------------------------------------------
2133 * Set the BxCF regs. First, wipe out the bank config registers.
2134 *-----------------------------------------------------------------*/
Stefan Roeseedd73f22007-10-21 08:12:41 +02002135 mtsdram(SDRAM_MB0CF, 0x00000000);
2136 mtsdram(SDRAM_MB1CF, 0x00000000);
2137 mtsdram(SDRAM_MB2CF, 0x00000000);
2138 mtsdram(SDRAM_MB3CF, 0x00000000);
Stefan Roese43f32472007-02-20 10:43:34 +01002139
2140 mode = SDRAM_BXCF_M_BE_ENABLE;
2141
2142 bank_0_populated = 0;
2143
2144 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
2145 if (dimm_populated[dimm_num] != SDRAM_NONE) {
2146 num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
2147 num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
2148 if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
2149 num_ranks = (num_ranks & 0x0F) +1;
2150 else
2151 num_ranks = num_ranks & 0x0F;
2152
2153 num_banks = spd_read(iic0_dimm_addr[dimm_num], 17);
2154
2155 for (ind_bank = 0; ind_bank < 2; ind_bank++) {
2156 if (num_banks == 4)
2157 ind = 0;
2158 else
Stefan Roese964754e2008-04-30 10:49:43 +02002159 ind = 5 << 8;
Stefan Roese43f32472007-02-20 10:43:34 +01002160 switch (num_col_addr) {
2161 case 0x08:
2162 mode |= (SDRAM_BXCF_M_AM_0 + ind);
2163 break;
2164 case 0x09:
2165 mode |= (SDRAM_BXCF_M_AM_1 + ind);
2166 break;
2167 case 0x0A:
2168 mode |= (SDRAM_BXCF_M_AM_2 + ind);
2169 break;
2170 case 0x0B:
2171 mode |= (SDRAM_BXCF_M_AM_3 + ind);
2172 break;
2173 case 0x0C:
2174 mode |= (SDRAM_BXCF_M_AM_4 + ind);
2175 break;
2176 default:
2177 printf("DDR-SDRAM: DIMM %d BxCF configuration.\n",
2178 (unsigned int)dimm_num);
2179 printf("ERROR: Unsupported value for number of "
2180 "column addresses: %d.\n", (unsigned int)num_col_addr);
2181 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002182 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002183 }
2184 }
2185
2186 if ((dimm_populated[dimm_num] != SDRAM_NONE)&& (dimm_num ==1))
2187 bank_0_populated = 1;
2188
2189 for (ind_rank = 0; ind_rank < num_ranks; ind_rank++) {
Stefan Roeseedd73f22007-10-21 08:12:41 +02002190 mtsdram(SDRAM_MB0CF +
2191 ((dimm_num + bank_0_populated + ind_rank) << 2),
2192 mode);
Stefan Roese43f32472007-02-20 10:43:34 +01002193 }
2194 }
2195 }
2196}
2197
2198/*------------------------------------------------------------------
2199 * program memory queue.
2200 *-----------------------------------------------------------------*/
2201static void program_memory_queue(unsigned long *dimm_populated,
2202 unsigned char *iic0_dimm_addr,
2203 unsigned long num_dimm_banks)
2204{
2205 unsigned long dimm_num;
Stefan Roese0203a972008-07-09 17:33:57 +02002206 phys_size_t rank_base_addr;
Stefan Roese43f32472007-02-20 10:43:34 +01002207 unsigned long rank_reg;
Stefan Roese0203a972008-07-09 17:33:57 +02002208 phys_size_t rank_size_bytes;
Stefan Roese43f32472007-02-20 10:43:34 +01002209 unsigned long rank_size_id;
2210 unsigned long num_ranks;
2211 unsigned long baseadd_size;
2212 unsigned long i;
2213 unsigned long bank_0_populated = 0;
Stefan Roese0203a972008-07-09 17:33:57 +02002214 phys_size_t total_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002215
2216 /*------------------------------------------------------------------
2217 * Reset the rank_base_address.
2218 *-----------------------------------------------------------------*/
2219 rank_reg = SDRAM_R0BAS;
2220
2221 rank_base_addr = 0x00000000;
2222
2223 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
2224 if (dimm_populated[dimm_num] != SDRAM_NONE) {
2225 num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
2226 if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
2227 num_ranks = (num_ranks & 0x0F) + 1;
2228 else
2229 num_ranks = num_ranks & 0x0F;
2230
2231 rank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
2232
2233 /*------------------------------------------------------------------
2234 * Set the sizes
2235 *-----------------------------------------------------------------*/
2236 baseadd_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002237 switch (rank_size_id) {
Stefan Roesebdd13d12008-03-11 15:05:26 +01002238 case 0x01:
2239 baseadd_size |= SDRAM_RXBAS_SDSZ_1024;
2240 total_size = 1024;
2241 break;
Stefan Roese43f32472007-02-20 10:43:34 +01002242 case 0x02:
Stefan Roesebdd13d12008-03-11 15:05:26 +01002243 baseadd_size |= SDRAM_RXBAS_SDSZ_2048;
2244 total_size = 2048;
Stefan Roese43f32472007-02-20 10:43:34 +01002245 break;
2246 case 0x04:
Stefan Roesebdd13d12008-03-11 15:05:26 +01002247 baseadd_size |= SDRAM_RXBAS_SDSZ_4096;
2248 total_size = 4096;
Stefan Roese43f32472007-02-20 10:43:34 +01002249 break;
2250 case 0x08:
2251 baseadd_size |= SDRAM_RXBAS_SDSZ_32;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002252 total_size = 32;
Stefan Roese43f32472007-02-20 10:43:34 +01002253 break;
2254 case 0x10:
2255 baseadd_size |= SDRAM_RXBAS_SDSZ_64;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002256 total_size = 64;
Stefan Roese43f32472007-02-20 10:43:34 +01002257 break;
2258 case 0x20:
2259 baseadd_size |= SDRAM_RXBAS_SDSZ_128;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002260 total_size = 128;
Stefan Roese43f32472007-02-20 10:43:34 +01002261 break;
2262 case 0x40:
2263 baseadd_size |= SDRAM_RXBAS_SDSZ_256;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002264 total_size = 256;
Stefan Roese43f32472007-02-20 10:43:34 +01002265 break;
2266 case 0x80:
2267 baseadd_size |= SDRAM_RXBAS_SDSZ_512;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002268 total_size = 512;
Stefan Roese43f32472007-02-20 10:43:34 +01002269 break;
2270 default:
2271 printf("DDR-SDRAM: DIMM %d memory queue configuration.\n",
2272 (unsigned int)dimm_num);
2273 printf("ERROR: Unsupported value for the banksize: %d.\n",
2274 (unsigned int)rank_size_id);
2275 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002276 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002277 }
Stefan Roesebdd13d12008-03-11 15:05:26 +01002278 rank_size_bytes = total_size << 20;
Stefan Roese43f32472007-02-20 10:43:34 +01002279
2280 if ((dimm_populated[dimm_num] != SDRAM_NONE) && (dimm_num == 1))
2281 bank_0_populated = 1;
2282
2283 for (i = 0; i < num_ranks; i++) {
2284 mtdcr_any(rank_reg+i+dimm_num+bank_0_populated,
Stefan Roeseb39ef632007-03-08 10:06:09 +01002285 (SDRAM_RXBAS_SDBA_ENCODE(rank_base_addr) |
2286 baseadd_size));
Stefan Roese43f32472007-02-20 10:43:34 +01002287 rank_base_addr += rank_size_bytes;
2288 }
2289 }
2290 }
Stefan Roesebdd13d12008-03-11 15:05:26 +01002291
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002292#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
2293 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
2294 defined(CONFIG_460SX)
Stefan Roesebdd13d12008-03-11 15:05:26 +01002295 /*
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002296 * Enable high bandwidth access
Stefan Roesebdd13d12008-03-11 15:05:26 +01002297 * This is currently not used, but with this setup
2298 * it is possible to use it later on in e.g. the Linux
2299 * EMAC driver for performance gain.
2300 */
2301 mtdcr(SDRAM_PLBADDULL, 0x00000000); /* MQ0_BAUL */
2302 mtdcr(SDRAM_PLBADDUHB, 0x00000008); /* MQ0_BAUH */
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002303
2304 /*
2305 * Set optimal value for Memory Queue HB/LL Configuration registers
2306 */
Yuri Tikhonovbbfab702008-10-17 12:54:18 +02002307 mtdcr(SDRAM_CONF1HB, (mfdcr(SDRAM_CONF1HB) & ~SDRAM_CONF1HB_MASK) |
2308 SDRAM_CONF1HB_AAFR | SDRAM_CONF1HB_RPEN | SDRAM_CONF1HB_RFTE |
2309 SDRAM_CONF1HB_RPLM | SDRAM_CONF1HB_WRCL);
2310 mtdcr(SDRAM_CONF1LL, (mfdcr(SDRAM_CONF1LL) & ~SDRAM_CONF1LL_MASK) |
2311 SDRAM_CONF1LL_AAFR | SDRAM_CONF1LL_RPEN | SDRAM_CONF1LL_RFTE |
2312 SDRAM_CONF1LL_RPLM);
Stefan Roese1abbbd02008-08-21 11:05:03 +02002313 mtdcr(SDRAM_CONFPATHB, mfdcr(SDRAM_CONFPATHB) | SDRAM_CONFPATHB_TPEN);
Stefan Roesebdd13d12008-03-11 15:05:26 +01002314#endif
Stefan Roese43f32472007-02-20 10:43:34 +01002315}
2316
Stefan Roeseb39ef632007-03-08 10:06:09 +01002317#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +01002318/*-----------------------------------------------------------------------------+
2319 * program_ecc.
2320 *-----------------------------------------------------------------------------*/
2321static void program_ecc(unsigned long *dimm_populated,
2322 unsigned char *iic0_dimm_addr,
Stefan Roesebad41112007-03-01 21:11:36 +01002323 unsigned long num_dimm_banks,
2324 unsigned long tlb_word2_i_value)
Stefan Roese43f32472007-02-20 10:43:34 +01002325{
Stefan Roese43f32472007-02-20 10:43:34 +01002326 unsigned long dimm_num;
2327 unsigned long ecc;
2328
2329 ecc = 0;
2330 /* loop through all the DIMM slots on the board */
2331 for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
2332 /* If a dimm is installed in a particular slot ... */
2333 if (dimm_populated[dimm_num] != SDRAM_NONE)
Masahiro Yamadadb204642014-11-07 03:03:31 +09002334 ecc = max(ecc,
2335 (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 11));
Stefan Roese43f32472007-02-20 10:43:34 +01002336 }
2337 if (ecc == 0)
2338 return;
Stefan Roesebad41112007-03-01 21:11:36 +01002339
Felix Radensky0e925362009-09-27 23:56:12 +02002340 do_program_ecc(tlb_word2_i_value);
Stefan Roese43f32472007-02-20 10:43:34 +01002341}
Stefan Roeseb39ef632007-03-08 10:06:09 +01002342#endif
Stefan Roese43f32472007-02-20 10:43:34 +01002343
Adam Graham97a55812008-09-03 12:26:59 -07002344#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Stefan Roese43f32472007-02-20 10:43:34 +01002345/*-----------------------------------------------------------------------------+
2346 * program_DQS_calibration.
2347 *-----------------------------------------------------------------------------*/
2348static void program_DQS_calibration(unsigned long *dimm_populated,
2349 unsigned char *iic0_dimm_addr,
2350 unsigned long num_dimm_banks)
2351{
2352 unsigned long val;
2353
2354#ifdef HARD_CODED_DQS /* calibration test with hardvalues */
2355 mtsdram(SDRAM_RQDC, 0x80000037);
2356 mtsdram(SDRAM_RDCC, 0x40000000);
2357 mtsdram(SDRAM_RFDC, 0x000001DF);
2358
2359 test();
2360#else
2361 /*------------------------------------------------------------------
2362 * Program RDCC register
2363 * Read sample cycle auto-update enable
2364 *-----------------------------------------------------------------*/
2365
Stefan Roese43f32472007-02-20 10:43:34 +01002366 mfsdram(SDRAM_RDCC, val);
2367 mtsdram(SDRAM_RDCC,
2368 (val & ~(SDRAM_RDCC_RDSS_MASK | SDRAM_RDCC_RSAE_MASK))
Stefan Roesee3060b02008-01-05 09:12:41 +01002369 | SDRAM_RDCC_RSAE_ENABLE);
Stefan Roese43f32472007-02-20 10:43:34 +01002370
2371 /*------------------------------------------------------------------
2372 * Program RQDC register
2373 * Internal DQS delay mechanism enable
2374 *-----------------------------------------------------------------*/
2375 mtsdram(SDRAM_RQDC, (SDRAM_RQDC_RQDE_ENABLE|SDRAM_RQDC_RQFD_ENCODE(0x38)));
2376
2377 /*------------------------------------------------------------------
2378 * Program RFDC register
2379 * Set Feedback Fractional Oversample
2380 * Auto-detect read sample cycle enable
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07002381 * Set RFOS to 1/4 of memclk cycle (0x3f)
Stefan Roese43f32472007-02-20 10:43:34 +01002382 *-----------------------------------------------------------------*/
2383 mfsdram(SDRAM_RFDC, val);
2384 mtsdram(SDRAM_RFDC,
2385 (val & ~(SDRAM_RFDC_ARSE_MASK | SDRAM_RFDC_RFOS_MASK |
2386 SDRAM_RFDC_RFFD_MASK))
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07002387 | (SDRAM_RFDC_ARSE_ENABLE | SDRAM_RFDC_RFOS_ENCODE(0x3f) |
Stefan Roese43f32472007-02-20 10:43:34 +01002388 SDRAM_RFDC_RFFD_ENCODE(0)));
2389
2390 DQS_calibration_process();
2391#endif
2392}
2393
Stefan Roesef88e3602007-03-31 08:46:08 +02002394static int short_mem_test(void)
Stefan Roese43f32472007-02-20 10:43:34 +01002395{
2396 u32 *membase;
2397 u32 bxcr_num;
2398 u32 bxcf;
2399 int i;
2400 int j;
Stefan Roese0203a972008-07-09 17:33:57 +02002401 phys_size_t base_addr;
Stefan Roese43f32472007-02-20 10:43:34 +01002402 u32 test[NUMMEMTESTS][NUMMEMWORDS] = {
2403 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
2404 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
2405 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
2406 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
2407 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
2408 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
2409 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
2410 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
2411 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
2412 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
2413 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
2414 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
2415 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
2416 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
2417 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
2418 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
Stefan Roesef88e3602007-03-31 08:46:08 +02002419 int l;
Stefan Roese43f32472007-02-20 10:43:34 +01002420
2421 for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
2422 mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf);
2423
2424 /* Banks enabled */
2425 if ((bxcf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
Stefan Roese43f32472007-02-20 10:43:34 +01002426 /* Bank is enabled */
Stefan Roese43f32472007-02-20 10:43:34 +01002427
Stefan Roese0203a972008-07-09 17:33:57 +02002428 /*
2429 * Only run test on accessable memory (below 2GB)
2430 */
2431 base_addr = SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num));
2432 if (base_addr >= CONFIG_MAX_MEM_MAPPED)
2433 continue;
2434
Stefan Roese43f32472007-02-20 10:43:34 +01002435 /*------------------------------------------------------------------
2436 * Run the short memory test.
2437 *-----------------------------------------------------------------*/
Stefan Roese0203a972008-07-09 17:33:57 +02002438 membase = (u32 *)(u32)base_addr;
Stefan Roesef88e3602007-03-31 08:46:08 +02002439
Stefan Roese43f32472007-02-20 10:43:34 +01002440 for (i = 0; i < NUMMEMTESTS; i++) {
2441 for (j = 0; j < NUMMEMWORDS; j++) {
2442 membase[j] = test[i][j];
2443 ppcDcbf((u32)&(membase[j]));
2444 }
2445 sync();
Stefan Roesef88e3602007-03-31 08:46:08 +02002446 for (l=0; l<NUMLOOPS; l++) {
2447 for (j = 0; j < NUMMEMWORDS; j++) {
2448 if (membase[j] != test[i][j]) {
2449 ppcDcbf((u32)&(membase[j]));
2450 return 0;
2451 }
Stefan Roese43f32472007-02-20 10:43:34 +01002452 ppcDcbf((u32)&(membase[j]));
Stefan Roese43f32472007-02-20 10:43:34 +01002453 }
Stefan Roesef88e3602007-03-31 08:46:08 +02002454 sync();
Stefan Roese43f32472007-02-20 10:43:34 +01002455 }
Stefan Roese43f32472007-02-20 10:43:34 +01002456 }
Stefan Roese43f32472007-02-20 10:43:34 +01002457 } /* if bank enabled */
2458 } /* for bxcf_num */
2459
Stefan Roesef88e3602007-03-31 08:46:08 +02002460 return 1;
Stefan Roese43f32472007-02-20 10:43:34 +01002461}
2462
2463#ifndef HARD_CODED_DQS
2464/*-----------------------------------------------------------------------------+
2465 * DQS_calibration_process.
2466 *-----------------------------------------------------------------------------*/
2467static void DQS_calibration_process(void)
2468{
Stefan Roese43f32472007-02-20 10:43:34 +01002469 unsigned long rfdc_reg;
2470 unsigned long rffd;
Stefan Roese43f32472007-02-20 10:43:34 +01002471 unsigned long val;
Stefan Roese43f32472007-02-20 10:43:34 +01002472 long rffd_average;
2473 long max_start;
Stefan Roese43f32472007-02-20 10:43:34 +01002474 unsigned long dlycal;
2475 unsigned long dly_val;
2476 unsigned long max_pass_length;
2477 unsigned long current_pass_length;
2478 unsigned long current_fail_length;
2479 unsigned long current_start;
2480 long max_end;
2481 unsigned char fail_found;
2482 unsigned char pass_found;
Stefan Roesee3060b02008-01-05 09:12:41 +01002483#if !defined(CONFIG_DDR_RQDC_FIXED)
Stefan Roese65a57122011-11-15 08:02:30 +00002484 int window_found;
Stefan Roesee3060b02008-01-05 09:12:41 +01002485 u32 rqdc_reg;
2486 u32 rqfd;
Stefan Roesef88e3602007-03-31 08:46:08 +02002487 u32 rqfd_start;
Stefan Roesee3060b02008-01-05 09:12:41 +01002488 u32 rqfd_average;
2489 int loopi = 0;
Stefan Roesef88e3602007-03-31 08:46:08 +02002490 char str[] = "Auto calibration -";
2491 char slash[] = "\\|/-\\|/-";
Stefan Roese43f32472007-02-20 10:43:34 +01002492
2493 /*------------------------------------------------------------------
2494 * Test to determine the best read clock delay tuning bits.
2495 *
2496 * Before the DDR controller can be used, the read clock delay needs to be
2497 * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
2498 * This value cannot be hardcoded into the program because it changes
2499 * depending on the board's setup and environment.
2500 * To do this, all delay values are tested to see if they
2501 * work or not. By doing this, you get groups of fails with groups of
2502 * passing values. The idea is to find the start and end of a passing
2503 * window and take the center of it to use as the read clock delay.
2504 *
2505 * A failure has to be seen first so that when we hit a pass, we know
2506 * that it is truely the start of the window. If we get passing values
2507 * to start off with, we don't know if we are at the start of the window.
2508 *
2509 * The code assumes that a failure will always be found.
2510 * If a failure is not found, there is no easy way to get the middle
2511 * of the passing window. I guess we can pretty much pick any value
2512 * but some values will be better than others. Since the lowest speed
2513 * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
2514 * from experimentation it is safe to say you will always have a failure.
2515 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002516
2517 /* first fix RQDC[RQFD] to an average of 80 degre phase shift to find RFDC[RFFD] */
2518 rqfd_start = 64; /* test-only: don't know if this is the _best_ start value */
2519
2520 puts(str);
2521
2522calibration_loop:
2523 mfsdram(SDRAM_RQDC, rqdc_reg);
2524 mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
2525 SDRAM_RQDC_RQFD_ENCODE(rqfd_start));
Stefan Roesee3060b02008-01-05 09:12:41 +01002526#else /* CONFIG_DDR_RQDC_FIXED */
2527 /*
2528 * On Katmai the complete auto-calibration somehow doesn't seem to
2529 * produce the best results, meaning optimal values for RQFD/RFFD.
2530 * This was discovered by GDA using a high bandwidth scope,
2531 * analyzing the DDR2 signals. GDA provided a fixed value for RQFD,
2532 * so now on Katmai "only" RFFD is auto-calibrated.
2533 */
2534 mtsdram(SDRAM_RQDC, CONFIG_DDR_RQDC_FIXED);
2535#endif /* CONFIG_DDR_RQDC_FIXED */
Stefan Roese43f32472007-02-20 10:43:34 +01002536
2537 max_start = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002538
2539 max_pass_length = 0;
2540 max_start = 0;
2541 max_end = 0;
2542 current_pass_length = 0;
2543 current_fail_length = 0;
2544 current_start = 0;
York Sun4a598092013-04-01 11:29:11 -07002545 fail_found = false;
2546 pass_found = false;
Stefan Roese43f32472007-02-20 10:43:34 +01002547
Stefan Roese43f32472007-02-20 10:43:34 +01002548 /*
2549 * get the delay line calibration register value
2550 */
2551 mfsdram(SDRAM_DLCR, dlycal);
2552 dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
2553
2554 for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
2555 mfsdram(SDRAM_RFDC, rfdc_reg);
2556 rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
2557
2558 /*------------------------------------------------------------------
2559 * Set the timing reg for the test.
2560 *-----------------------------------------------------------------*/
2561 mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
2562
Stefan Roese43f32472007-02-20 10:43:34 +01002563 /*------------------------------------------------------------------
2564 * See if the rffd value passed.
2565 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002566 if (short_mem_test()) {
York Sun4a598092013-04-01 11:29:11 -07002567 if (fail_found == true) {
2568 pass_found = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002569 if (current_pass_length == 0)
2570 current_start = rffd;
2571
2572 current_fail_length = 0;
2573 current_pass_length++;
2574
2575 if (current_pass_length > max_pass_length) {
2576 max_pass_length = current_pass_length;
2577 max_start = current_start;
2578 max_end = rffd;
2579 }
2580 }
2581 } else {
2582 current_pass_length = 0;
2583 current_fail_length++;
2584
2585 if (current_fail_length >= (dly_val >> 2)) {
York Sun4a598092013-04-01 11:29:11 -07002586 if (fail_found == false)
2587 fail_found = true;
2588 else if (pass_found == true)
Stefan Roese43f32472007-02-20 10:43:34 +01002589 break;
Stefan Roese43f32472007-02-20 10:43:34 +01002590 }
2591 }
2592 } /* for rffd */
2593
Stefan Roese43f32472007-02-20 10:43:34 +01002594 /*------------------------------------------------------------------
2595 * Set the average RFFD value
2596 *-----------------------------------------------------------------*/
2597 rffd_average = ((max_start + max_end) >> 1);
2598
2599 if (rffd_average < 0)
2600 rffd_average = 0;
2601
2602 if (rffd_average > SDRAM_RFDC_RFFD_MAX)
2603 rffd_average = SDRAM_RFDC_RFFD_MAX;
2604 /* now fix RFDC[RFFD] found and find RQDC[RQFD] */
2605 mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
2606
Stefan Roesee3060b02008-01-05 09:12:41 +01002607#if !defined(CONFIG_DDR_RQDC_FIXED)
Stefan Roese43f32472007-02-20 10:43:34 +01002608 max_pass_length = 0;
2609 max_start = 0;
2610 max_end = 0;
2611 current_pass_length = 0;
2612 current_fail_length = 0;
2613 current_start = 0;
York Sun4a598092013-04-01 11:29:11 -07002614 window_found = false;
2615 fail_found = false;
2616 pass_found = false;
Stefan Roese43f32472007-02-20 10:43:34 +01002617
2618 for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
2619 mfsdram(SDRAM_RQDC, rqdc_reg);
2620 rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
2621
2622 /*------------------------------------------------------------------
2623 * Set the timing reg for the test.
2624 *-----------------------------------------------------------------*/
2625 mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
2626
Stefan Roese43f32472007-02-20 10:43:34 +01002627 /*------------------------------------------------------------------
2628 * See if the rffd value passed.
2629 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002630 if (short_mem_test()) {
York Sun4a598092013-04-01 11:29:11 -07002631 if (fail_found == true) {
2632 pass_found = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002633 if (current_pass_length == 0)
2634 current_start = rqfd;
2635
2636 current_fail_length = 0;
2637 current_pass_length++;
2638
2639 if (current_pass_length > max_pass_length) {
2640 max_pass_length = current_pass_length;
2641 max_start = current_start;
2642 max_end = rqfd;
2643 }
2644 }
2645 } else {
2646 current_pass_length = 0;
2647 current_fail_length++;
2648
York Sun4a598092013-04-01 11:29:11 -07002649 if (fail_found == false) {
2650 fail_found = true;
2651 } else if (pass_found == true) {
2652 window_found = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002653 break;
2654 }
2655 }
2656 }
2657
Stefan Roesef88e3602007-03-31 08:46:08 +02002658 rqfd_average = ((max_start + max_end) >> 1);
2659
Stefan Roese43f32472007-02-20 10:43:34 +01002660 /*------------------------------------------------------------------
2661 * Make sure we found the valid read passing window. Halt if not
2662 *-----------------------------------------------------------------*/
York Sun4a598092013-04-01 11:29:11 -07002663 if (window_found == false) {
Stefan Roesef88e3602007-03-31 08:46:08 +02002664 if (rqfd_start < SDRAM_RQDC_RQFD_MAX) {
2665 putc('\b');
2666 putc(slash[loopi++ % 8]);
2667
2668 /* try again from with a different RQFD start value */
2669 rqfd_start++;
2670 goto calibration_loop;
2671 }
2672
2673 printf("\nERROR: Cannot determine a common read delay for the "
Stefan Roese43f32472007-02-20 10:43:34 +01002674 "DIMM(s) installed.\n");
2675 debug("%s[%d] ERROR : \n", __FUNCTION__,__LINE__);
Grant Erickson9416cd92008-07-09 16:46:35 -07002676 ppc4xx_ibm_ddr2_register_dump();
Heiko Schocher68310b02007-06-25 19:11:37 +02002677 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002678 }
2679
Stefan Roese43f32472007-02-20 10:43:34 +01002680 if (rqfd_average < 0)
2681 rqfd_average = 0;
2682
2683 if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
2684 rqfd_average = SDRAM_RQDC_RQFD_MAX;
2685
Stefan Roese43f32472007-02-20 10:43:34 +01002686 mtsdram(SDRAM_RQDC,
2687 (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
2688 SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
2689
Stefan Roesee3060b02008-01-05 09:12:41 +01002690 blank_string(strlen(str));
2691#endif /* CONFIG_DDR_RQDC_FIXED */
2692
Stefan Roese43f32472007-02-20 10:43:34 +01002693 mfsdram(SDRAM_DLCR, val);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03002694 debug("%s[%d] DLCR: 0x%08lX\n", __FUNCTION__, __LINE__, val);
Stefan Roese43f32472007-02-20 10:43:34 +01002695 mfsdram(SDRAM_RQDC, val);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03002696 debug("%s[%d] RQDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
Stefan Roese43f32472007-02-20 10:43:34 +01002697 mfsdram(SDRAM_RFDC, val);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03002698 debug("%s[%d] RFDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
Stefan Roesee3060b02008-01-05 09:12:41 +01002699 mfsdram(SDRAM_RDCC, val);
Felix Radensky8d4d4a62009-07-01 11:37:46 +03002700 debug("%s[%d] RDCC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
Stefan Roese43f32472007-02-20 10:43:34 +01002701}
2702#else /* calibration test with hardvalues */
2703/*-----------------------------------------------------------------------------+
2704 * DQS_calibration_process.
2705 *-----------------------------------------------------------------------------*/
2706static void test(void)
2707{
2708 unsigned long dimm_num;
2709 unsigned long ecc_temp;
2710 unsigned long i, j;
2711 unsigned long *membase;
2712 unsigned long bxcf[MAXRANKS];
2713 unsigned long val;
2714 char window_found;
2715 char begin_found[MAXDIMMS];
2716 char end_found[MAXDIMMS];
2717 char search_end[MAXDIMMS];
2718 unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
2719 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
2720 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
2721 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
2722 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
2723 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
2724 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
2725 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
2726 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
2727 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
2728 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
2729 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
2730 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
2731 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
2732 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
2733 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
2734 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
2735
2736 /*------------------------------------------------------------------
2737 * Test to determine the best read clock delay tuning bits.
2738 *
2739 * Before the DDR controller can be used, the read clock delay needs to be
2740 * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
2741 * This value cannot be hardcoded into the program because it changes
2742 * depending on the board's setup and environment.
2743 * To do this, all delay values are tested to see if they
2744 * work or not. By doing this, you get groups of fails with groups of
2745 * passing values. The idea is to find the start and end of a passing
2746 * window and take the center of it to use as the read clock delay.
2747 *
2748 * A failure has to be seen first so that when we hit a pass, we know
2749 * that it is truely the start of the window. If we get passing values
2750 * to start off with, we don't know if we are at the start of the window.
2751 *
2752 * The code assumes that a failure will always be found.
2753 * If a failure is not found, there is no easy way to get the middle
2754 * of the passing window. I guess we can pretty much pick any value
2755 * but some values will be better than others. Since the lowest speed
2756 * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
2757 * from experimentation it is safe to say you will always have a failure.
2758 *-----------------------------------------------------------------*/
2759 mfsdram(SDRAM_MCOPT1, ecc_temp);
2760 ecc_temp &= SDRAM_MCOPT1_MCHK_MASK;
2761 mfsdram(SDRAM_MCOPT1, val);
2762 mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) |
2763 SDRAM_MCOPT1_MCHK_NON);
2764
York Sun4a598092013-04-01 11:29:11 -07002765 window_found = false;
2766 begin_found[0] = false;
2767 end_found[0] = false;
2768 search_end[0] = false;
2769 begin_found[1] = false;
2770 end_found[1] = false;
2771 search_end[1] = false;
Stefan Roese43f32472007-02-20 10:43:34 +01002772
2773 for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
2774 mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf[bxcr_num]);
2775
2776 /* Banks enabled */
2777 if ((bxcf[dimm_num] & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
2778
2779 /* Bank is enabled */
2780 membase =
2781 (unsigned long*)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+dimm_num)));
2782
2783 /*------------------------------------------------------------------
2784 * Run the short memory test.
2785 *-----------------------------------------------------------------*/
2786 for (i = 0; i < NUMMEMTESTS; i++) {
2787 for (j = 0; j < NUMMEMWORDS; j++) {
2788 membase[j] = test[i][j];
2789 ppcDcbf((u32)&(membase[j]));
2790 }
2791 sync();
2792 for (j = 0; j < NUMMEMWORDS; j++) {
2793 if (membase[j] != test[i][j]) {
2794 ppcDcbf((u32)&(membase[j]));
2795 break;
2796 }
2797 ppcDcbf((u32)&(membase[j]));
2798 }
2799 sync();
2800 if (j < NUMMEMWORDS)
2801 break;
2802 }
2803
2804 /*------------------------------------------------------------------
2805 * See if the rffd value passed.
2806 *-----------------------------------------------------------------*/
2807 if (i < NUMMEMTESTS) {
York Sun4a598092013-04-01 11:29:11 -07002808 if ((end_found[dimm_num] == false) &&
2809 (search_end[dimm_num] == true)) {
2810 end_found[dimm_num] = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002811 }
York Sun4a598092013-04-01 11:29:11 -07002812 if ((end_found[0] == true) &&
2813 (end_found[1] == true))
Stefan Roese43f32472007-02-20 10:43:34 +01002814 break;
2815 } else {
York Sun4a598092013-04-01 11:29:11 -07002816 if (begin_found[dimm_num] == false) {
2817 begin_found[dimm_num] = true;
2818 search_end[dimm_num] = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002819 }
2820 }
2821 } else {
York Sun4a598092013-04-01 11:29:11 -07002822 begin_found[dimm_num] = true;
2823 end_found[dimm_num] = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002824 }
2825 }
2826
York Sun4a598092013-04-01 11:29:11 -07002827 if ((begin_found[0] == true) && (begin_found[1] == true))
2828 window_found = true;
Stefan Roese43f32472007-02-20 10:43:34 +01002829
2830 /*------------------------------------------------------------------
2831 * Make sure we found the valid read passing window. Halt if not
2832 *-----------------------------------------------------------------*/
York Sun4a598092013-04-01 11:29:11 -07002833 if (window_found == false) {
Stefan Roese43f32472007-02-20 10:43:34 +01002834 printf("ERROR: Cannot determine a common read delay for the "
2835 "DIMM(s) installed.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002836 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002837 }
2838
2839 /*------------------------------------------------------------------
2840 * Restore the ECC variable to what it originally was
2841 *-----------------------------------------------------------------*/
2842 mtsdram(SDRAM_MCOPT1,
2843 (ppcMfdcr_sdram(SDRAM_MCOPT1) & ~SDRAM_MCOPT1_MCHK_MASK)
2844 | ecc_temp);
2845}
Adam Graham97a55812008-09-03 12:26:59 -07002846#endif /* !HARD_CODED_DQS */
2847#endif /* !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */
Stefan Roese43f32472007-02-20 10:43:34 +01002848
Stefan Roese2001a332008-07-10 15:32:32 +02002849#else /* CONFIG_SPD_EEPROM */
2850
Grant Ericksonb6933412008-05-22 14:44:14 -07002851/*-----------------------------------------------------------------------------
2852 * Function: initdram
Adam Graham446eb8d2008-10-08 10:13:14 -07002853 * Description: Configures the PPC4xx IBM DDR1/DDR2 SDRAM memory controller.
2854 * The configuration is performed using static, compile-
Grant Ericksonb6933412008-05-22 14:44:14 -07002855 * time parameters.
Adam Graham446eb8d2008-10-08 10:13:14 -07002856 * Configures the PPC405EX(r) and PPC460EX/GT
Grant Ericksonb6933412008-05-22 14:44:14 -07002857 *---------------------------------------------------------------------------*/
Becky Brucebd99ae72008-06-09 16:03:40 -05002858phys_size_t initdram(int board_type)
Grant Ericksonb6933412008-05-22 14:44:14 -07002859{
2860 unsigned long val;
2861
Adam Graham446eb8d2008-10-08 10:13:14 -07002862#if defined(CONFIG_440)
2863 mtdcr(SDRAM_R0BAS, CONFIG_SYS_SDRAM_R0BAS);
2864 mtdcr(SDRAM_R1BAS, CONFIG_SYS_SDRAM_R1BAS);
2865 mtdcr(SDRAM_R2BAS, CONFIG_SYS_SDRAM_R2BAS);
2866 mtdcr(SDRAM_R3BAS, CONFIG_SYS_SDRAM_R3BAS);
2867 mtdcr(SDRAM_PLBADDULL, CONFIG_SYS_SDRAM_PLBADDULL); /* MQ0_BAUL */
2868 mtdcr(SDRAM_PLBADDUHB, CONFIG_SYS_SDRAM_PLBADDUHB); /* MQ0_BAUH */
2869 mtdcr(SDRAM_CONF1LL, CONFIG_SYS_SDRAM_CONF1LL);
2870 mtdcr(SDRAM_CONF1HB, CONFIG_SYS_SDRAM_CONF1HB);
2871 mtdcr(SDRAM_CONFPATHB, CONFIG_SYS_SDRAM_CONFPATHB);
2872#endif
2873
Grant Ericksonb6933412008-05-22 14:44:14 -07002874 /* Set Memory Bank Configuration Registers */
2875
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002876 mtsdram(SDRAM_MB0CF, CONFIG_SYS_SDRAM0_MB0CF);
2877 mtsdram(SDRAM_MB1CF, CONFIG_SYS_SDRAM0_MB1CF);
2878 mtsdram(SDRAM_MB2CF, CONFIG_SYS_SDRAM0_MB2CF);
2879 mtsdram(SDRAM_MB3CF, CONFIG_SYS_SDRAM0_MB3CF);
Grant Ericksonb6933412008-05-22 14:44:14 -07002880
2881 /* Set Memory Clock Timing Register */
2882
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002883 mtsdram(SDRAM_CLKTR, CONFIG_SYS_SDRAM0_CLKTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07002884
2885 /* Set Refresh Time Register */
2886
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002887 mtsdram(SDRAM_RTR, CONFIG_SYS_SDRAM0_RTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07002888
2889 /* Set SDRAM Timing Registers */
2890
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002891 mtsdram(SDRAM_SDTR1, CONFIG_SYS_SDRAM0_SDTR1);
2892 mtsdram(SDRAM_SDTR2, CONFIG_SYS_SDRAM0_SDTR2);
2893 mtsdram(SDRAM_SDTR3, CONFIG_SYS_SDRAM0_SDTR3);
Grant Ericksonb6933412008-05-22 14:44:14 -07002894
2895 /* Set Mode and Extended Mode Registers */
2896
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002897 mtsdram(SDRAM_MMODE, CONFIG_SYS_SDRAM0_MMODE);
2898 mtsdram(SDRAM_MEMODE, CONFIG_SYS_SDRAM0_MEMODE);
Grant Ericksonb6933412008-05-22 14:44:14 -07002899
2900 /* Set Memory Controller Options 1 Register */
2901
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002902 mtsdram(SDRAM_MCOPT1, CONFIG_SYS_SDRAM0_MCOPT1);
Grant Ericksonb6933412008-05-22 14:44:14 -07002903
2904 /* Set Manual Initialization Control Registers */
2905
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002906 mtsdram(SDRAM_INITPLR0, CONFIG_SYS_SDRAM0_INITPLR0);
2907 mtsdram(SDRAM_INITPLR1, CONFIG_SYS_SDRAM0_INITPLR1);
2908 mtsdram(SDRAM_INITPLR2, CONFIG_SYS_SDRAM0_INITPLR2);
2909 mtsdram(SDRAM_INITPLR3, CONFIG_SYS_SDRAM0_INITPLR3);
2910 mtsdram(SDRAM_INITPLR4, CONFIG_SYS_SDRAM0_INITPLR4);
2911 mtsdram(SDRAM_INITPLR5, CONFIG_SYS_SDRAM0_INITPLR5);
2912 mtsdram(SDRAM_INITPLR6, CONFIG_SYS_SDRAM0_INITPLR6);
2913 mtsdram(SDRAM_INITPLR7, CONFIG_SYS_SDRAM0_INITPLR7);
2914 mtsdram(SDRAM_INITPLR8, CONFIG_SYS_SDRAM0_INITPLR8);
2915 mtsdram(SDRAM_INITPLR9, CONFIG_SYS_SDRAM0_INITPLR9);
2916 mtsdram(SDRAM_INITPLR10, CONFIG_SYS_SDRAM0_INITPLR10);
2917 mtsdram(SDRAM_INITPLR11, CONFIG_SYS_SDRAM0_INITPLR11);
2918 mtsdram(SDRAM_INITPLR12, CONFIG_SYS_SDRAM0_INITPLR12);
2919 mtsdram(SDRAM_INITPLR13, CONFIG_SYS_SDRAM0_INITPLR13);
2920 mtsdram(SDRAM_INITPLR14, CONFIG_SYS_SDRAM0_INITPLR14);
2921 mtsdram(SDRAM_INITPLR15, CONFIG_SYS_SDRAM0_INITPLR15);
Grant Ericksonb6933412008-05-22 14:44:14 -07002922
2923 /* Set On-Die Termination Registers */
2924
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002925 mtsdram(SDRAM_CODT, CONFIG_SYS_SDRAM0_CODT);
2926 mtsdram(SDRAM_MODT0, CONFIG_SYS_SDRAM0_MODT0);
2927 mtsdram(SDRAM_MODT1, CONFIG_SYS_SDRAM0_MODT1);
Grant Ericksonb6933412008-05-22 14:44:14 -07002928
2929 /* Set Write Timing Register */
2930
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002931 mtsdram(SDRAM_WRDTR, CONFIG_SYS_SDRAM0_WRDTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07002932
2933 /*
2934 * Start Initialization by SDRAM0_MCOPT2[SREN] = 0 and
2935 * SDRAM0_MCOPT2[IPTR] = 1
2936 */
2937
2938 mtsdram(SDRAM_MCOPT2, (SDRAM_MCOPT2_SREN_EXIT |
2939 SDRAM_MCOPT2_IPTR_EXECUTE));
2940
2941 /*
2942 * Poll SDRAM0_MCSTAT[MIC] for assertion to indicate the
2943 * completion of initialization.
2944 */
2945
2946 do {
2947 mfsdram(SDRAM_MCSTAT, val);
2948 } while ((val & SDRAM_MCSTAT_MIC_MASK) != SDRAM_MCSTAT_MIC_COMP);
2949
2950 /* Set Delay Control Registers */
2951
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002952 mtsdram(SDRAM_DLCR, CONFIG_SYS_SDRAM0_DLCR);
Adam Graham97a55812008-09-03 12:26:59 -07002953
2954#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002955 mtsdram(SDRAM_RDCC, CONFIG_SYS_SDRAM0_RDCC);
2956 mtsdram(SDRAM_RQDC, CONFIG_SYS_SDRAM0_RQDC);
2957 mtsdram(SDRAM_RFDC, CONFIG_SYS_SDRAM0_RFDC);
Adam Graham97a55812008-09-03 12:26:59 -07002958#endif /* !CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
Grant Ericksonb6933412008-05-22 14:44:14 -07002959
2960 /*
2961 * Enable Controller by SDRAM0_MCOPT2[DCEN] = 1:
2962 */
2963
2964 mfsdram(SDRAM_MCOPT2, val);
2965 mtsdram(SDRAM_MCOPT2, val | SDRAM_MCOPT2_DCEN_ENABLE);
2966
Adam Graham446eb8d2008-10-08 10:13:14 -07002967#if defined(CONFIG_440)
2968 /*
2969 * Program TLB entries with caches enabled, for best performace
2970 * while auto-calibrating and ECC generation
2971 */
2972 program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), 0);
2973#endif
2974
Adam Graham97a55812008-09-03 12:26:59 -07002975#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Adam Graham97a55812008-09-03 12:26:59 -07002976 /*------------------------------------------------------------------
2977 | DQS calibration.
2978 +-----------------------------------------------------------------*/
2979 DQS_autocalibration();
Adam Graham97a55812008-09-03 12:26:59 -07002980#endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
2981
Stefan Roesec3bcfce2010-05-25 15:33:14 +02002982 /*
2983 * Now complete RDSS configuration as mentioned on page 7 of the AMCC
2984 * PowerPC440SP/SPe DDR2 application note:
2985 * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
2986 */
2987 update_rdcc();
2988
Grant Ericksonb6933412008-05-22 14:44:14 -07002989#if defined(CONFIG_DDR_ECC)
Felix Radensky0e925362009-09-27 23:56:12 +02002990 do_program_ecc(0);
Grant Ericksonb6933412008-05-22 14:44:14 -07002991#endif /* defined(CONFIG_DDR_ECC) */
Grant Erickson9416cd92008-07-09 16:46:35 -07002992
Adam Graham446eb8d2008-10-08 10:13:14 -07002993#if defined(CONFIG_440)
2994 /*
2995 * Now after initialization (auto-calibration and ECC generation)
2996 * remove the TLB entries with caches enabled and program again with
2997 * desired cache functionality
2998 */
2999 remove_tlb(0, (CONFIG_SYS_MBYTES_SDRAM << 20));
3000 program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), MY_TLB_WORD2_I_ENABLE);
3001#endif
3002
Grant Erickson9416cd92008-07-09 16:46:35 -07003003 ppc4xx_ibm_ddr2_register_dump();
Adam Graham97a55812008-09-03 12:26:59 -07003004
3005#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
3006 /*
3007 * Clear potential errors resulting from auto-calibration.
3008 * If not done, then we could get an interrupt later on when
3009 * exceptions are enabled.
3010 */
3011 set_mcsr(get_mcsr());
3012#endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
3013
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003014 return (CONFIG_SYS_MBYTES_SDRAM << 20);
Grant Ericksonb6933412008-05-22 14:44:14 -07003015}
Stefan Roese2001a332008-07-10 15:32:32 +02003016#endif /* CONFIG_SPD_EEPROM */
Grant Erickson9416cd92008-07-09 16:46:35 -07003017
Adam Graham97a55812008-09-03 12:26:59 -07003018#if defined(CONFIG_440)
3019u32 mfdcr_any(u32 dcr)
3020{
3021 u32 val;
3022
3023 switch (dcr) {
3024 case SDRAM_R0BAS + 0:
3025 val = mfdcr(SDRAM_R0BAS + 0);
3026 break;
3027 case SDRAM_R0BAS + 1:
3028 val = mfdcr(SDRAM_R0BAS + 1);
3029 break;
3030 case SDRAM_R0BAS + 2:
3031 val = mfdcr(SDRAM_R0BAS + 2);
3032 break;
3033 case SDRAM_R0BAS + 3:
3034 val = mfdcr(SDRAM_R0BAS + 3);
3035 break;
3036 default:
3037 printf("DCR %d not defined in case statement!!!\n", dcr);
3038 val = 0; /* just to satisfy the compiler */
3039 }
3040
3041 return val;
3042}
3043
3044void mtdcr_any(u32 dcr, u32 val)
3045{
3046 switch (dcr) {
3047 case SDRAM_R0BAS + 0:
3048 mtdcr(SDRAM_R0BAS + 0, val);
3049 break;
3050 case SDRAM_R0BAS + 1:
3051 mtdcr(SDRAM_R0BAS + 1, val);
3052 break;
3053 case SDRAM_R0BAS + 2:
3054 mtdcr(SDRAM_R0BAS + 2, val);
3055 break;
3056 case SDRAM_R0BAS + 3:
3057 mtdcr(SDRAM_R0BAS + 3, val);
3058 break;
3059 default:
3060 printf("DCR %d not defined in case statement!!!\n", dcr);
3061 }
3062}
3063#endif /* defined(CONFIG_440) */
Adam Graham97a55812008-09-03 12:26:59 -07003064
3065inline void ppc4xx_ibm_ddr2_register_dump(void)
Grant Erickson9416cd92008-07-09 16:46:35 -07003066{
Stefan Roese2001a332008-07-10 15:32:32 +02003067#if defined(DEBUG)
Grant Erickson9416cd92008-07-09 16:46:35 -07003068 printf("\nPPC4xx IBM DDR2 Register Dump:\n");
3069
3070#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3071 defined(CONFIG_460EX) || defined(CONFIG_460GT))
Felix Radensky8d4d4a62009-07-01 11:37:46 +03003072 PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R0BAS);
3073 PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R1BAS);
3074 PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R2BAS);
3075 PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R3BAS);
Grant Erickson9416cd92008-07-09 16:46:35 -07003076#endif /* (defined(CONFIG_440SP) || ... */
3077#if defined(CONFIG_405EX)
3078 PPC4xx_IBM_DDR2_DUMP_REGISTER(BESR);
3079 PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARL);
3080 PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARH);
3081 PPC4xx_IBM_DDR2_DUMP_REGISTER(WMIRQ);
3082 PPC4xx_IBM_DDR2_DUMP_REGISTER(PLBOPT);
3083 PPC4xx_IBM_DDR2_DUMP_REGISTER(PUABA);
3084#endif /* defined(CONFIG_405EX) */
3085 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB0CF);
3086 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB1CF);
3087 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB2CF);
3088 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB3CF);
3089 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCSTAT);
3090 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT1);
3091 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT2);
3092 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT0);
3093 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT1);
3094 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT2);
3095 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT3);
3096 PPC4xx_IBM_DDR2_DUMP_REGISTER(CODT);
3097#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3098 defined(CONFIG_460EX) || defined(CONFIG_460GT))
3099 PPC4xx_IBM_DDR2_DUMP_REGISTER(VVPR);
3100 PPC4xx_IBM_DDR2_DUMP_REGISTER(OPARS);
3101 /*
3102 * OPART is only used as a trigger register.
3103 *
3104 * No data is contained in this register, and reading or writing
3105 * to is can cause bad things to happen (hangs). Just skip it and
3106 * report "N/A".
3107 */
3108 printf("%20s = N/A\n", "SDRAM_OPART");
3109#endif /* defined(CONFIG_440SP) || ... */
3110 PPC4xx_IBM_DDR2_DUMP_REGISTER(RTR);
3111 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR0);
3112 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR1);
3113 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR2);
3114 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR3);
3115 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR4);
3116 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR5);
3117 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR6);
3118 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR7);
3119 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR8);
3120 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR9);
3121 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR10);
3122 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR11);
3123 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR12);
3124 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR13);
3125 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR14);
3126 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR15);
3127 PPC4xx_IBM_DDR2_DUMP_REGISTER(RQDC);
3128 PPC4xx_IBM_DDR2_DUMP_REGISTER(RFDC);
3129 PPC4xx_IBM_DDR2_DUMP_REGISTER(RDCC);
3130 PPC4xx_IBM_DDR2_DUMP_REGISTER(DLCR);
3131 PPC4xx_IBM_DDR2_DUMP_REGISTER(CLKTR);
3132 PPC4xx_IBM_DDR2_DUMP_REGISTER(WRDTR);
3133 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR1);
3134 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR2);
3135 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR3);
3136 PPC4xx_IBM_DDR2_DUMP_REGISTER(MMODE);
3137 PPC4xx_IBM_DDR2_DUMP_REGISTER(MEMODE);
Stefan Roese37748882009-11-03 14:34:45 +01003138 PPC4xx_IBM_DDR2_DUMP_REGISTER(ECCES);
Grant Erickson9416cd92008-07-09 16:46:35 -07003139#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3140 defined(CONFIG_460EX) || defined(CONFIG_460GT))
3141 PPC4xx_IBM_DDR2_DUMP_REGISTER(CID);
3142#endif /* defined(CONFIG_440SP) || ... */
3143 PPC4xx_IBM_DDR2_DUMP_REGISTER(RID);
3144 PPC4xx_IBM_DDR2_DUMP_REGISTER(FCSR);
3145 PPC4xx_IBM_DDR2_DUMP_REGISTER(RTSR);
Stefan Roese2001a332008-07-10 15:32:32 +02003146#endif /* defined(DEBUG) */
3147}