blob: 1e495716b55bd913a8f27bf8fe2c33002afcca53 [file] [log] [blame]
Stefan Roese43f32472007-02-20 10:43:34 +01001/*
2 * cpu/ppc4xx/44x_spd_ddr2.c
3 * 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 Roesee3060b02008-01-05 09:12:41 +010012 * (C) Copyright 2007-2008
Stefan Roese43f32472007-02-20 10:43:34 +010013 * Stefan Roese, DENX Software Engineering, sr@denx.de.
14 *
15 * COPYRIGHT AMCC CORPORATION 2004
16 *
17 * See file CREDITS for list of people who contributed to this
18 * project.
19 *
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
33 * MA 02111-1307 USA
34 *
35 */
36
37/* define DEBUG for debugging output (obviously ;-)) */
38#if 0
39#define DEBUG
40#endif
41
42#include <common.h>
Stefan Roesebad41112007-03-01 21:11:36 +010043#include <command.h>
Stefan Roese43f32472007-02-20 10:43:34 +010044#include <ppc4xx.h>
45#include <i2c.h>
46#include <asm/io.h>
47#include <asm/processor.h>
48#include <asm/mmu.h>
Stefan Roese286b81b2008-04-29 13:57:07 +020049#include <asm/cache.h>
Stefan Roese43f32472007-02-20 10:43:34 +010050
Grant Ericksonb6933412008-05-22 14:44:14 -070051#include "ecc.h"
52
Stefan Roese2001a332008-07-10 15:32:32 +020053#if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
54
55#define PPC4xx_IBM_DDR2_DUMP_REGISTER(mnemonic) \
56 do { \
57 u32 data; \
58 mfsdram(SDRAM_##mnemonic, data); \
59 printf("%20s[%02x] = 0x%08X\n", \
60 "SDRAM_" #mnemonic, SDRAM_##mnemonic, data); \
61 } while (0)
62
Adam Graham446eb8d2008-10-08 10:13:14 -070063#if defined(CONFIG_440)
64/*
65 * This DDR2 setup code can dynamically setup the TLB entries for the DDR2
66 * memory region. Right now the cache should still be disabled in U-Boot
67 * because of the EMAC driver, that need its buffer descriptor to be located
68 * in non cached memory.
69 *
70 * If at some time this restriction doesn't apply anymore, just define
71 * CONFIG_4xx_DCACHE in the board config file and this code should setup
72 * everything correctly.
73 */
74#ifdef CONFIG_4xx_DCACHE
75/* enable caching on SDRAM */
76#define MY_TLB_WORD2_I_ENABLE 0
77#else
78/* disable caching on SDRAM */
79#define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
80#endif /* CONFIG_4xx_DCACHE */
81#endif /* CONFIG_440 */
82
Stefan Roese2001a332008-07-10 15:32:32 +020083#if defined(CONFIG_SPD_EEPROM)
Stefan Roese43f32472007-02-20 10:43:34 +010084
Stefan Roesebad41112007-03-01 21:11:36 +010085/*-----------------------------------------------------------------------------+
86 * Defines
87 *-----------------------------------------------------------------------------*/
Stefan Roese43f32472007-02-20 10:43:34 +010088#ifndef TRUE
Wolfgang Denk52232fd2007-02-27 14:26:04 +010089#define TRUE 1
Stefan Roese43f32472007-02-20 10:43:34 +010090#endif
91#ifndef FALSE
Wolfgang Denk52232fd2007-02-27 14:26:04 +010092#define FALSE 0
Stefan Roese43f32472007-02-20 10:43:34 +010093#endif
94
95#define SDRAM_DDR1 1
96#define SDRAM_DDR2 2
97#define SDRAM_NONE 0
98
Wolfgang Denk70df7bc2007-06-22 23:59:00 +020099#define MAXDIMMS 2
100#define MAXRANKS 4
Stefan Roese43f32472007-02-20 10:43:34 +0100101#define MAXBXCF 4
102#define MAX_SPD_BYTES 256 /* Max number of bytes on the DIMM's SPD EEPROM */
103
104#define ONE_BILLION 1000000000
105
106#define MULDIV64(m1, m2, d) (u32)(((u64)(m1) * (u64)(m2)) / (u64)(d))
107
Stefan Roesebad41112007-03-01 21:11:36 +0100108#define CMD_NOP (7 << 19)
109#define CMD_PRECHARGE (2 << 19)
110#define CMD_REFRESH (1 << 19)
111#define CMD_EMR (0 << 19)
112#define CMD_READ (5 << 19)
113#define CMD_WRITE (4 << 19)
Stefan Roese43f32472007-02-20 10:43:34 +0100114
Stefan Roesebad41112007-03-01 21:11:36 +0100115#define SELECT_MR (0 << 16)
116#define SELECT_EMR (1 << 16)
117#define SELECT_EMR2 (2 << 16)
118#define SELECT_EMR3 (3 << 16)
119
120/* MR */
121#define DLL_RESET 0x00000100
122
123#define WRITE_RECOV_2 (1 << 9)
124#define WRITE_RECOV_3 (2 << 9)
125#define WRITE_RECOV_4 (3 << 9)
126#define WRITE_RECOV_5 (4 << 9)
127#define WRITE_RECOV_6 (5 << 9)
128
129#define BURST_LEN_4 0x00000002
130
131/* EMR */
132#define ODT_0_OHM 0x00000000
133#define ODT_50_OHM 0x00000044
134#define ODT_75_OHM 0x00000004
135#define ODT_150_OHM 0x00000040
136
137#define ODS_FULL 0x00000000
138#define ODS_REDUCED 0x00000002
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700139#define OCD_CALIB_DEF 0x00000380
Stefan Roesebad41112007-03-01 21:11:36 +0100140
141/* defines for ODT (On Die Termination) of the 440SP(e) DDR2 controller */
142#define ODT_EB0R (0x80000000 >> 8)
143#define ODT_EB0W (0x80000000 >> 7)
144#define CALC_ODT_R(n) (ODT_EB0R << (n << 1))
145#define CALC_ODT_W(n) (ODT_EB0W << (n << 1))
146#define CALC_ODT_RW(n) (CALC_ODT_R(n) | CALC_ODT_W(n))
147
Stefan Roese43f32472007-02-20 10:43:34 +0100148/* Defines for the Read Cycle Delay test */
Stefan Roesef88e3602007-03-31 08:46:08 +0200149#define NUMMEMTESTS 8
150#define NUMMEMWORDS 8
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200151#define NUMLOOPS 64 /* memory test loops */
Stefan Roese43f32472007-02-20 10:43:34 +0100152
Stefan Roesebad41112007-03-01 21:11:36 +0100153/*
Stefan Roese0203a972008-07-09 17:33:57 +0200154 * Newer PPC's like 440SPe, 460EX/GT can be equipped with more than 2GB of SDRAM.
155 * To support such configurations, we "only" map the first 2GB via the TLB's. We
156 * need some free virtual address space for the remaining peripherals like, SoC
157 * devices, FLASH etc.
158 *
159 * Note that ECC is currently not supported on configurations with more than 2GB
160 * SDRAM. This is because we only map the first 2GB on such systems, and therefore
161 * the ECC parity byte of the remaining area can't be written.
162 */
Stefan Roese0203a972008-07-09 17:33:57 +0200163
164/*
Heiko Schocher68310b02007-06-25 19:11:37 +0200165 * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
166 */
167void __spd_ddr_init_hang (void)
168{
169 hang ();
170}
171void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang")));
172
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200173/*
174 * To provide an interface for board specific config values in this common
175 * DDR setup code, we implement he "weak" default functions here. They return
176 * the default value back to the caller.
177 *
178 * Please see include/configs/yucca.h for an example fora board specific
179 * implementation.
180 */
181u32 __ddr_wrdtr(u32 default_val)
182{
183 return default_val;
184}
185u32 ddr_wrdtr(u32) __attribute__((weak, alias("__ddr_wrdtr")));
186
187u32 __ddr_clktr(u32 default_val)
188{
189 return default_val;
190}
191u32 ddr_clktr(u32) __attribute__((weak, alias("__ddr_clktr")));
192
Heiko Schocher633e03a2007-06-22 19:11:54 +0200193
Stefan Roese43f32472007-02-20 10:43:34 +0100194/* Private Structure Definitions */
195
196/* enum only to ease code for cas latency setting */
197typedef enum ddr_cas_id {
198 DDR_CAS_2 = 20,
199 DDR_CAS_2_5 = 25,
200 DDR_CAS_3 = 30,
201 DDR_CAS_4 = 40,
202 DDR_CAS_5 = 50
203} ddr_cas_id_t;
204
205/*-----------------------------------------------------------------------------+
206 * Prototypes
207 *-----------------------------------------------------------------------------*/
Stefan Roese0203a972008-07-09 17:33:57 +0200208static phys_size_t sdram_memsize(void);
Stefan Roese43f32472007-02-20 10:43:34 +0100209static void get_spd_info(unsigned long *dimm_populated,
210 unsigned char *iic0_dimm_addr,
211 unsigned long num_dimm_banks);
212static void check_mem_type(unsigned long *dimm_populated,
213 unsigned char *iic0_dimm_addr,
214 unsigned long num_dimm_banks);
215static void check_frequency(unsigned long *dimm_populated,
216 unsigned char *iic0_dimm_addr,
217 unsigned long num_dimm_banks);
218static void check_rank_number(unsigned long *dimm_populated,
219 unsigned char *iic0_dimm_addr,
220 unsigned long num_dimm_banks);
221static void check_voltage_type(unsigned long *dimm_populated,
222 unsigned char *iic0_dimm_addr,
223 unsigned long num_dimm_banks);
224static void program_memory_queue(unsigned long *dimm_populated,
225 unsigned char *iic0_dimm_addr,
226 unsigned long num_dimm_banks);
227static void program_codt(unsigned long *dimm_populated,
228 unsigned char *iic0_dimm_addr,
229 unsigned long num_dimm_banks);
230static void program_mode(unsigned long *dimm_populated,
231 unsigned char *iic0_dimm_addr,
232 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +0100233 ddr_cas_id_t *selected_cas,
234 int *write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100235static void program_tr(unsigned long *dimm_populated,
236 unsigned char *iic0_dimm_addr,
237 unsigned long num_dimm_banks);
238static void program_rtr(unsigned long *dimm_populated,
239 unsigned char *iic0_dimm_addr,
240 unsigned long num_dimm_banks);
241static void program_bxcf(unsigned long *dimm_populated,
242 unsigned char *iic0_dimm_addr,
243 unsigned long num_dimm_banks);
244static void program_copt1(unsigned long *dimm_populated,
245 unsigned char *iic0_dimm_addr,
246 unsigned long num_dimm_banks);
247static void program_initplr(unsigned long *dimm_populated,
248 unsigned char *iic0_dimm_addr,
249 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +0100250 ddr_cas_id_t selected_cas,
Stefan Roesebad41112007-03-01 21:11:36 +0100251 int write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100252static unsigned long is_ecc_enabled(void);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100253#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +0100254static void program_ecc(unsigned long *dimm_populated,
255 unsigned char *iic0_dimm_addr,
Stefan Roesebad41112007-03-01 21:11:36 +0100256 unsigned long num_dimm_banks,
257 unsigned long tlb_word2_i_value);
Stefan Roese43f32472007-02-20 10:43:34 +0100258static void program_ecc_addr(unsigned long start_address,
Stefan Roesebad41112007-03-01 21:11:36 +0100259 unsigned long num_bytes,
260 unsigned long tlb_word2_i_value);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100261#endif
Adam Graham97a55812008-09-03 12:26:59 -0700262#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Stefan Roesebad41112007-03-01 21:11:36 +0100263static void program_DQS_calibration(unsigned long *dimm_populated,
Adam Graham97a55812008-09-03 12:26:59 -0700264 unsigned char *iic0_dimm_addr,
265 unsigned long num_dimm_banks);
Stefan Roese43f32472007-02-20 10:43:34 +0100266#ifdef HARD_CODED_DQS /* calibration test with hardvalues */
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100267static void test(void);
Stefan Roese43f32472007-02-20 10:43:34 +0100268#else
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100269static void DQS_calibration_process(void);
Stefan Roese43f32472007-02-20 10:43:34 +0100270#endif
Adam Graham97a55812008-09-03 12:26:59 -0700271#endif
Stefan Roesebad41112007-03-01 21:11:36 +0100272int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
273void dcbz_area(u32 start_address, u32 num_bytes);
Stefan Roese43f32472007-02-20 10:43:34 +0100274
Stefan Roese43f32472007-02-20 10:43:34 +0100275static unsigned char spd_read(uchar chip, uint addr)
276{
277 unsigned char data[2];
278
279 if (i2c_probe(chip) == 0)
280 if (i2c_read(chip, addr, 1, data, 1) == 0)
281 return data[0];
282
283 return 0;
284}
285
286/*-----------------------------------------------------------------------------+
287 * sdram_memsize
288 *-----------------------------------------------------------------------------*/
Stefan Roese0203a972008-07-09 17:33:57 +0200289static phys_size_t sdram_memsize(void)
Stefan Roese43f32472007-02-20 10:43:34 +0100290{
Stefan Roese0203a972008-07-09 17:33:57 +0200291 phys_size_t mem_size;
Stefan Roese43f32472007-02-20 10:43:34 +0100292 unsigned long mcopt2;
293 unsigned long mcstat;
294 unsigned long mb0cf;
295 unsigned long sdsz;
296 unsigned long i;
297
298 mem_size = 0;
299
300 mfsdram(SDRAM_MCOPT2, mcopt2);
301 mfsdram(SDRAM_MCSTAT, mcstat);
302
303 /* DDR controller must be enabled and not in self-refresh. */
304 /* Otherwise memsize is zero. */
305 if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
306 && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
307 && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
308 == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
Stefan Roesebad41112007-03-01 21:11:36 +0100309 for (i = 0; i < MAXBXCF; i++) {
Stefan Roese43f32472007-02-20 10:43:34 +0100310 mfsdram(SDRAM_MB0CF + (i << 2), mb0cf);
311 /* Banks enabled */
312 if ((mb0cf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
313 sdsz = mfdcr_any(SDRAM_R0BAS + i) & SDRAM_RXBAS_SDSZ_MASK;
314
315 switch(sdsz) {
316 case SDRAM_RXBAS_SDSZ_8:
317 mem_size+=8;
318 break;
319 case SDRAM_RXBAS_SDSZ_16:
320 mem_size+=16;
321 break;
322 case SDRAM_RXBAS_SDSZ_32:
323 mem_size+=32;
324 break;
325 case SDRAM_RXBAS_SDSZ_64:
326 mem_size+=64;
327 break;
328 case SDRAM_RXBAS_SDSZ_128:
329 mem_size+=128;
330 break;
331 case SDRAM_RXBAS_SDSZ_256:
332 mem_size+=256;
333 break;
334 case SDRAM_RXBAS_SDSZ_512:
335 mem_size+=512;
336 break;
337 case SDRAM_RXBAS_SDSZ_1024:
338 mem_size+=1024;
339 break;
340 case SDRAM_RXBAS_SDSZ_2048:
341 mem_size+=2048;
342 break;
343 case SDRAM_RXBAS_SDSZ_4096:
344 mem_size+=4096;
345 break;
346 default:
Stefan Roese251161b2008-07-10 09:58:06 +0200347 printf("WARNING: Unsupported bank size (SDSZ=0x%lx)!\n"
Stefan Roese0203a972008-07-09 17:33:57 +0200348 , sdsz);
Stefan Roese43f32472007-02-20 10:43:34 +0100349 mem_size=0;
350 break;
351 }
352 }
353 }
354 }
355
Stefan Roese0203a972008-07-09 17:33:57 +0200356 return mem_size << 20;
Stefan Roese43f32472007-02-20 10:43:34 +0100357}
358
359/*-----------------------------------------------------------------------------+
360 * initdram. Initializes the 440SP Memory Queue and DDR SDRAM controller.
361 * Note: This routine runs from flash with a stack set up in the chip's
362 * sram space. It is important that the routine does not require .sbss, .bss or
363 * .data sections. It also cannot call routines that require these sections.
364 *-----------------------------------------------------------------------------*/
365/*-----------------------------------------------------------------------------
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100366 * Function: initdram
Stefan Roese43f32472007-02-20 10:43:34 +0100367 * Description: Configures SDRAM memory banks for DDR operation.
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100368 * Auto Memory Configuration option reads the DDR SDRAM EEPROMs
369 * via the IIC bus and then configures the DDR SDRAM memory
370 * banks appropriately. If Auto Memory Configuration is
371 * not used, it is assumed that no DIMM is plugged
Stefan Roese43f32472007-02-20 10:43:34 +0100372 *-----------------------------------------------------------------------------*/
Becky Brucebd99ae72008-06-09 16:03:40 -0500373phys_size_t initdram(int board_type)
Stefan Roese43f32472007-02-20 10:43:34 +0100374{
Stefan Roesebad41112007-03-01 21:11:36 +0100375 unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
Stefan Roese43f32472007-02-20 10:43:34 +0100376 unsigned char spd0[MAX_SPD_BYTES];
377 unsigned char spd1[MAX_SPD_BYTES];
378 unsigned char *dimm_spd[MAXDIMMS];
379 unsigned long dimm_populated[MAXDIMMS];
Stefan Roese4a0f5902008-01-15 10:11:02 +0100380 unsigned long num_dimm_banks; /* on board dimm banks */
Stefan Roese43f32472007-02-20 10:43:34 +0100381 unsigned long val;
Stefan Roese4a0f5902008-01-15 10:11:02 +0100382 ddr_cas_id_t selected_cas = DDR_CAS_5; /* preset to silence compiler */
Stefan Roesebad41112007-03-01 21:11:36 +0100383 int write_recovery;
Stefan Roese0203a972008-07-09 17:33:57 +0200384 phys_size_t dram_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +0100385
386 num_dimm_banks = sizeof(iic0_dimm_addr);
387
388 /*------------------------------------------------------------------
389 * Set up an array of SPD matrixes.
390 *-----------------------------------------------------------------*/
391 dimm_spd[0] = spd0;
392 dimm_spd[1] = spd1;
393
394 /*------------------------------------------------------------------
Stefan Roese43f32472007-02-20 10:43:34 +0100395 * Reset the DDR-SDRAM controller.
396 *-----------------------------------------------------------------*/
Stefan Roesebad41112007-03-01 21:11:36 +0100397 mtsdr(SDR0_SRST, (0x80000000 >> 10));
Stefan Roese43f32472007-02-20 10:43:34 +0100398 mtsdr(SDR0_SRST, 0x00000000);
399
400 /*
401 * Make sure I2C controller is initialized
402 * before continuing.
403 */
404
405 /* switch to correct I2C bus */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200406 I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM);
407 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
Stefan Roese43f32472007-02-20 10:43:34 +0100408
409 /*------------------------------------------------------------------
410 * Clear out the serial presence detect buffers.
411 * Perform IIC reads from the dimm. Fill in the spds.
412 * Check to see if the dimm slots are populated
413 *-----------------------------------------------------------------*/
414 get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks);
415
416 /*------------------------------------------------------------------
417 * Check the memory type for the dimms plugged.
418 *-----------------------------------------------------------------*/
419 check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
420
421 /*------------------------------------------------------------------
422 * Check the frequency supported for the dimms plugged.
423 *-----------------------------------------------------------------*/
424 check_frequency(dimm_populated, iic0_dimm_addr, num_dimm_banks);
425
426 /*------------------------------------------------------------------
427 * Check the total rank number.
428 *-----------------------------------------------------------------*/
429 check_rank_number(dimm_populated, iic0_dimm_addr, num_dimm_banks);
430
431 /*------------------------------------------------------------------
432 * Check the voltage type for the dimms plugged.
433 *-----------------------------------------------------------------*/
434 check_voltage_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
435
436 /*------------------------------------------------------------------
437 * Program SDRAM controller options 2 register
438 * Except Enabling of the memory controller.
439 *-----------------------------------------------------------------*/
440 mfsdram(SDRAM_MCOPT2, val);
441 mtsdram(SDRAM_MCOPT2,
442 (val &
443 ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_PMEN_MASK |
444 SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_XSRP_MASK |
445 SDRAM_MCOPT2_ISIE_MASK))
446 | (SDRAM_MCOPT2_SREN_ENTER | SDRAM_MCOPT2_PMEN_DISABLE |
447 SDRAM_MCOPT2_IPTR_IDLE | SDRAM_MCOPT2_XSRP_ALLOW |
448 SDRAM_MCOPT2_ISIE_ENABLE));
449
450 /*------------------------------------------------------------------
451 * Program SDRAM controller options 1 register
452 * Note: Does not enable the memory controller.
453 *-----------------------------------------------------------------*/
454 program_copt1(dimm_populated, iic0_dimm_addr, num_dimm_banks);
455
456 /*------------------------------------------------------------------
457 * Set the SDRAM Controller On Die Termination Register
458 *-----------------------------------------------------------------*/
459 program_codt(dimm_populated, iic0_dimm_addr, num_dimm_banks);
460
461 /*------------------------------------------------------------------
462 * Program SDRAM refresh register.
463 *-----------------------------------------------------------------*/
464 program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
465
466 /*------------------------------------------------------------------
467 * Program SDRAM mode register.
468 *-----------------------------------------------------------------*/
Stefan Roesebad41112007-03-01 21:11:36 +0100469 program_mode(dimm_populated, iic0_dimm_addr, num_dimm_banks,
470 &selected_cas, &write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100471
472 /*------------------------------------------------------------------
473 * Set the SDRAM Write Data/DM/DQS Clock Timing Reg
474 *-----------------------------------------------------------------*/
475 mfsdram(SDRAM_WRDTR, val);
476 mtsdram(SDRAM_WRDTR, (val & ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK)) |
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200477 ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC | SDRAM_WRDTR_WTR_90_DEG_ADV));
Stefan Roese43f32472007-02-20 10:43:34 +0100478
479 /*------------------------------------------------------------------
480 * Set the SDRAM Clock Timing Register
481 *-----------------------------------------------------------------*/
482 mfsdram(SDRAM_CLKTR, val);
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200483 mtsdram(SDRAM_CLKTR, (val & ~SDRAM_CLKTR_CLKP_MASK) |
484 ddr_clktr(SDRAM_CLKTR_CLKP_0_DEG));
Stefan Roese43f32472007-02-20 10:43:34 +0100485
486 /*------------------------------------------------------------------
487 * Program the BxCF registers.
488 *-----------------------------------------------------------------*/
489 program_bxcf(dimm_populated, iic0_dimm_addr, num_dimm_banks);
490
491 /*------------------------------------------------------------------
492 * Program SDRAM timing registers.
493 *-----------------------------------------------------------------*/
494 program_tr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
495
496 /*------------------------------------------------------------------
497 * Set the Extended Mode register
498 *-----------------------------------------------------------------*/
499 mfsdram(SDRAM_MEMODE, val);
500 mtsdram(SDRAM_MEMODE,
501 (val & ~(SDRAM_MEMODE_DIC_MASK | SDRAM_MEMODE_DLL_MASK |
502 SDRAM_MEMODE_RTT_MASK | SDRAM_MEMODE_DQS_MASK)) |
503 (SDRAM_MEMODE_DIC_NORMAL | SDRAM_MEMODE_DLL_ENABLE
Stefan Roeseb39ef632007-03-08 10:06:09 +0100504 | SDRAM_MEMODE_RTT_150OHM | SDRAM_MEMODE_DQS_ENABLE));
Stefan Roese43f32472007-02-20 10:43:34 +0100505
506 /*------------------------------------------------------------------
507 * Program Initialization preload registers.
508 *-----------------------------------------------------------------*/
509 program_initplr(dimm_populated, iic0_dimm_addr, num_dimm_banks,
Stefan Roesebad41112007-03-01 21:11:36 +0100510 selected_cas, write_recovery);
Stefan Roese43f32472007-02-20 10:43:34 +0100511
512 /*------------------------------------------------------------------
513 * Delay to ensure 200usec have elapsed since reset.
514 *-----------------------------------------------------------------*/
515 udelay(400);
516
517 /*------------------------------------------------------------------
518 * Set the memory queue core base addr.
519 *-----------------------------------------------------------------*/
520 program_memory_queue(dimm_populated, iic0_dimm_addr, num_dimm_banks);
521
522 /*------------------------------------------------------------------
523 * Program SDRAM controller options 2 register
524 * Enable the memory controller.
525 *-----------------------------------------------------------------*/
526 mfsdram(SDRAM_MCOPT2, val);
527 mtsdram(SDRAM_MCOPT2,
528 (val & ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_DCEN_MASK |
529 SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_ISIE_MASK)) |
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700530 SDRAM_MCOPT2_IPTR_EXECUTE);
Stefan Roese43f32472007-02-20 10:43:34 +0100531
532 /*------------------------------------------------------------------
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700533 * Wait for IPTR_EXECUTE init sequence to complete.
Stefan Roese43f32472007-02-20 10:43:34 +0100534 *-----------------------------------------------------------------*/
535 do {
536 mfsdram(SDRAM_MCSTAT, val);
537 } while ((val & SDRAM_MCSTAT_MIC_MASK) == SDRAM_MCSTAT_MIC_NOTCOMP);
538
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -0700539 /* enable the controller only after init sequence completes */
540 mfsdram(SDRAM_MCOPT2, val);
541 mtsdram(SDRAM_MCOPT2, (val | SDRAM_MCOPT2_DCEN_ENABLE));
542
543 /* Make sure delay-line calibration is done before proceeding */
544 do {
545 mfsdram(SDRAM_DLCR, val);
546 } while (!(val & SDRAM_DLCR_DLCS_COMPLETE));
547
Stefan Roese43f32472007-02-20 10:43:34 +0100548 /* get installed memory size */
549 dram_size = sdram_memsize();
550
Stefan Roese0203a972008-07-09 17:33:57 +0200551 /*
552 * Limit size to 2GB
553 */
554 if (dram_size > CONFIG_MAX_MEM_MAPPED)
555 dram_size = CONFIG_MAX_MEM_MAPPED;
556
Stefan Roese43f32472007-02-20 10:43:34 +0100557 /* and program tlb entries for this size (dynamic) */
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200558
559 /*
560 * Program TLB entries with caches enabled, for best performace
561 * while auto-calibrating and ECC generation
562 */
563 program_tlb(0, 0, dram_size, 0);
Stefan Roese43f32472007-02-20 10:43:34 +0100564
Stefan Roese43f32472007-02-20 10:43:34 +0100565 /*------------------------------------------------------------------
Stefan Roesebad41112007-03-01 21:11:36 +0100566 * DQS calibration.
Stefan Roese43f32472007-02-20 10:43:34 +0100567 *-----------------------------------------------------------------*/
Adam Graham97a55812008-09-03 12:26:59 -0700568#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
569 DQS_autocalibration();
570#else
Stefan Roesebad41112007-03-01 21:11:36 +0100571 program_DQS_calibration(dimm_populated, iic0_dimm_addr, num_dimm_banks);
Adam Graham97a55812008-09-03 12:26:59 -0700572#endif
Stefan Roese43f32472007-02-20 10:43:34 +0100573
Stefan Roeseb39ef632007-03-08 10:06:09 +0100574#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +0100575 /*------------------------------------------------------------------
Stefan Roesebad41112007-03-01 21:11:36 +0100576 * If ecc is enabled, initialize the parity bits.
Stefan Roese43f32472007-02-20 10:43:34 +0100577 *-----------------------------------------------------------------*/
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200578 program_ecc(dimm_populated, iic0_dimm_addr, num_dimm_banks, 0);
Stefan Roeseb39ef632007-03-08 10:06:09 +0100579#endif
Stefan Roese43f32472007-02-20 10:43:34 +0100580
Stefan Roesebd2adeb2007-07-16 09:57:00 +0200581 /*
582 * Now after initialization (auto-calibration and ECC generation)
583 * remove the TLB entries with caches enabled and program again with
584 * desired cache functionality
585 */
586 remove_tlb(0, dram_size);
587 program_tlb(0, 0, dram_size, MY_TLB_WORD2_I_ENABLE);
588
Grant Erickson9416cd92008-07-09 16:46:35 -0700589 ppc4xx_ibm_ddr2_register_dump();
Stefan Roese43f32472007-02-20 10:43:34 +0100590
Stefan Roesebdd13d12008-03-11 15:05:26 +0100591 /*
592 * Clear potential errors resulting from auto-calibration.
593 * If not done, then we could get an interrupt later on when
594 * exceptions are enabled.
595 */
596 set_mcsr(get_mcsr());
597
Stefan Roese0203a972008-07-09 17:33:57 +0200598 return sdram_memsize();
Stefan Roese43f32472007-02-20 10:43:34 +0100599}
600
601static void get_spd_info(unsigned long *dimm_populated,
602 unsigned char *iic0_dimm_addr,
603 unsigned long num_dimm_banks)
604{
605 unsigned long dimm_num;
606 unsigned long dimm_found;
607 unsigned char num_of_bytes;
608 unsigned char total_size;
609
610 dimm_found = FALSE;
611 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
612 num_of_bytes = 0;
613 total_size = 0;
614
615 num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
616 debug("\nspd_read(0x%x) returned %d\n",
617 iic0_dimm_addr[dimm_num], num_of_bytes);
618 total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
619 debug("spd_read(0x%x) returned %d\n",
620 iic0_dimm_addr[dimm_num], total_size);
621
622 if ((num_of_bytes != 0) && (total_size != 0)) {
623 dimm_populated[dimm_num] = TRUE;
624 dimm_found = TRUE;
625 debug("DIMM slot %lu: populated\n", dimm_num);
626 } else {
627 dimm_populated[dimm_num] = FALSE;
628 debug("DIMM slot %lu: Not populated\n", dimm_num);
629 }
630 }
631
632 if (dimm_found == FALSE) {
633 printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200634 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100635 }
636}
637
Stefan Roese43f32472007-02-20 10:43:34 +0100638void board_add_ram_info(int use_default)
639{
Stefan Roeseedd73f22007-10-21 08:12:41 +0200640 PPC4xx_SYS_INFO board_cfg;
Stefan Roesef88e3602007-03-31 08:46:08 +0200641 u32 val;
642
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100643 if (is_ecc_enabled())
Stefan Roese5d48a842007-03-31 13:15:06 +0200644 puts(" (ECC");
Wolfgang Denk52232fd2007-02-27 14:26:04 +0100645 else
Stefan Roese5d48a842007-03-31 13:15:06 +0200646 puts(" (ECC not");
647
648 get_sys_info(&board_cfg);
649
650 mfsdr(SDR0_DDR0, val);
651 val = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(val), 1);
652 printf(" enabled, %d MHz", (val * 2) / 1000000);
Stefan Roesef88e3602007-03-31 08:46:08 +0200653
654 mfsdram(SDRAM_MMODE, val);
655 val = (val & SDRAM_MMODE_DCL_MASK) >> 4;
Stefan Roese5d48a842007-03-31 13:15:06 +0200656 printf(", CL%d)", val);
Stefan Roese43f32472007-02-20 10:43:34 +0100657}
Stefan Roese43f32472007-02-20 10:43:34 +0100658
659/*------------------------------------------------------------------
660 * For the memory DIMMs installed, this routine verifies that they
661 * really are DDR specific DIMMs.
662 *-----------------------------------------------------------------*/
663static void check_mem_type(unsigned long *dimm_populated,
664 unsigned char *iic0_dimm_addr,
665 unsigned long num_dimm_banks)
666{
667 unsigned long dimm_num;
668 unsigned long dimm_type;
669
670 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
671 if (dimm_populated[dimm_num] == TRUE) {
672 dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2);
673 switch (dimm_type) {
674 case 1:
675 printf("ERROR: Standard Fast Page Mode DRAM DIMM detected in "
676 "slot %d.\n", (unsigned int)dimm_num);
677 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
678 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200679 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100680 break;
681 case 2:
682 printf("ERROR: EDO DIMM detected in slot %d.\n",
683 (unsigned int)dimm_num);
684 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
685 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200686 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100687 break;
688 case 3:
689 printf("ERROR: Pipelined Nibble DIMM detected in slot %d.\n",
690 (unsigned int)dimm_num);
691 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
692 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200693 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100694 break;
695 case 4:
696 printf("ERROR: SDRAM DIMM detected in slot %d.\n",
697 (unsigned int)dimm_num);
698 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
699 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200700 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100701 break;
702 case 5:
703 printf("ERROR: Multiplexed ROM DIMM detected in slot %d.\n",
704 (unsigned int)dimm_num);
705 printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
706 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200707 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100708 break;
709 case 6:
710 printf("ERROR: SGRAM DIMM detected in slot %d.\n",
711 (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 7:
717 debug("DIMM slot %d: DDR1 SDRAM detected\n", dimm_num);
718 dimm_populated[dimm_num] = SDRAM_DDR1;
719 break;
720 case 8:
721 debug("DIMM slot %d: DDR2 SDRAM detected\n", dimm_num);
722 dimm_populated[dimm_num] = SDRAM_DDR2;
723 break;
724 default:
725 printf("ERROR: Unknown DIMM detected in slot %d.\n",
726 (unsigned int)dimm_num);
727 printf("Only DDR1 and DDR2 SDRAM DIMMs are supported.\n");
728 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200729 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100730 break;
731 }
732 }
733 }
734 for (dimm_num = 1; dimm_num < num_dimm_banks; dimm_num++) {
735 if ((dimm_populated[dimm_num-1] != SDRAM_NONE)
736 && (dimm_populated[dimm_num] != SDRAM_NONE)
737 && (dimm_populated[dimm_num-1] != dimm_populated[dimm_num])) {
738 printf("ERROR: DIMM's DDR1 and DDR2 type can not be mixed.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200739 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100740 }
741 }
742}
743
744/*------------------------------------------------------------------
745 * For the memory DIMMs installed, this routine verifies that
746 * frequency previously calculated is supported.
747 *-----------------------------------------------------------------*/
748static void check_frequency(unsigned long *dimm_populated,
749 unsigned char *iic0_dimm_addr,
750 unsigned long num_dimm_banks)
751{
752 unsigned long dimm_num;
753 unsigned long tcyc_reg;
754 unsigned long cycle_time;
755 unsigned long calc_cycle_time;
756 unsigned long sdram_freq;
757 unsigned long sdr_ddrpll;
Stefan Roeseedd73f22007-10-21 08:12:41 +0200758 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +0100759
760 /*------------------------------------------------------------------
761 * Get the board configuration info.
762 *-----------------------------------------------------------------*/
763 get_sys_info(&board_cfg);
764
Stefan Roeseb39ef632007-03-08 10:06:09 +0100765 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +0100766 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
767
768 /*
769 * calc_cycle_time is calculated from DDR frequency set by board/chip
770 * and is expressed in multiple of 10 picoseconds
771 * to match the way DIMM cycle time is calculated below.
772 */
773 calc_cycle_time = MULDIV64(ONE_BILLION, 100, sdram_freq);
774
775 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
776 if (dimm_populated[dimm_num] != SDRAM_NONE) {
777 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
778 /*
779 * Byte 9, Cycle time for CAS Latency=X, is split into two nibbles:
780 * the higher order nibble (bits 4-7) designates the cycle time
781 * to a granularity of 1ns;
782 * the value presented by the lower order nibble (bits 0-3)
783 * has a granularity of .1ns and is added to the value designated
784 * by the higher nibble. In addition, four lines of the lower order
785 * nibble are assigned to support +.25,+.33, +.66 and +.75.
786 */
787 /* Convert from hex to decimal */
788 if ((tcyc_reg & 0x0F) == 0x0D)
789 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
790 else if ((tcyc_reg & 0x0F) == 0x0C)
791 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 66;
792 else if ((tcyc_reg & 0x0F) == 0x0B)
793 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 33;
794 else if ((tcyc_reg & 0x0F) == 0x0A)
795 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 25;
796 else
797 cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) +
798 ((tcyc_reg & 0x0F)*10);
Stefan Roesef88e3602007-03-31 08:46:08 +0200799 debug("cycle_time=%d [10 picoseconds]\n", cycle_time);
Stefan Roese43f32472007-02-20 10:43:34 +0100800
801 if (cycle_time > (calc_cycle_time + 10)) {
802 /*
803 * the provided sdram cycle_time is too small
804 * for the available DIMM cycle_time.
805 * The additionnal 100ps is here to accept a small incertainty.
806 */
807 printf("ERROR: DRAM DIMM detected with cycle_time %d ps in "
808 "slot %d \n while calculated cycle time is %d ps.\n",
809 (unsigned int)(cycle_time*10),
810 (unsigned int)dimm_num,
811 (unsigned int)(calc_cycle_time*10));
812 printf("Replace the DIMM, or change DDR frequency via "
813 "strapping bits.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200814 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100815 }
816 }
817 }
818}
819
820/*------------------------------------------------------------------
821 * For the memory DIMMs installed, this routine verifies two
822 * ranks/banks maximum are availables.
823 *-----------------------------------------------------------------*/
824static void check_rank_number(unsigned long *dimm_populated,
825 unsigned char *iic0_dimm_addr,
826 unsigned long num_dimm_banks)
827{
828 unsigned long dimm_num;
829 unsigned long dimm_rank;
830 unsigned long total_rank = 0;
831
832 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
833 if (dimm_populated[dimm_num] != SDRAM_NONE) {
834 dimm_rank = spd_read(iic0_dimm_addr[dimm_num], 5);
835 if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
836 dimm_rank = (dimm_rank & 0x0F) +1;
837 else
838 dimm_rank = dimm_rank & 0x0F;
839
840
841 if (dimm_rank > MAXRANKS) {
Stefan Roese251161b2008-07-10 09:58:06 +0200842 printf("ERROR: DRAM DIMM detected with %lu ranks in "
843 "slot %lu is not supported.\n", dimm_rank, dimm_num);
Stefan Roese43f32472007-02-20 10:43:34 +0100844 printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
845 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200846 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100847 } else
848 total_rank += dimm_rank;
849 }
850 if (total_rank > MAXRANKS) {
851 printf("ERROR: DRAM DIMM detected with a total of %d ranks "
852 "for all slots.\n", (unsigned int)total_rank);
853 printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
854 printf("Remove one of the DIMM modules.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200855 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100856 }
857 }
858}
859
860/*------------------------------------------------------------------
861 * only support 2.5V modules.
862 * This routine verifies this.
863 *-----------------------------------------------------------------*/
864static void check_voltage_type(unsigned long *dimm_populated,
865 unsigned char *iic0_dimm_addr,
866 unsigned long num_dimm_banks)
867{
868 unsigned long dimm_num;
869 unsigned long voltage_type;
870
871 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
872 if (dimm_populated[dimm_num] != SDRAM_NONE) {
873 voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
874 switch (voltage_type) {
875 case 0x00:
876 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
877 printf("This DIMM is 5.0 Volt/TTL.\n");
878 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
879 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200880 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100881 break;
882 case 0x01:
883 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
884 printf("This DIMM is LVTTL.\n");
885 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
886 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200887 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100888 break;
889 case 0x02:
890 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
891 printf("This DIMM is 1.5 Volt.\n");
892 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
893 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200894 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100895 break;
896 case 0x03:
897 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
898 printf("This DIMM is 3.3 Volt/TTL.\n");
899 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
900 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200901 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100902 break;
903 case 0x04:
904 /* 2.5 Voltage only for DDR1 */
905 break;
906 case 0x05:
907 /* 1.8 Voltage only for DDR2 */
908 break;
909 default:
910 printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
911 printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
912 (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200913 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +0100914 break;
915 }
916 }
917 }
918}
919
920/*-----------------------------------------------------------------------------+
921 * program_copt1.
922 *-----------------------------------------------------------------------------*/
923static void program_copt1(unsigned long *dimm_populated,
924 unsigned char *iic0_dimm_addr,
925 unsigned long num_dimm_banks)
926{
927 unsigned long dimm_num;
928 unsigned long mcopt1;
929 unsigned long ecc_enabled;
930 unsigned long ecc = 0;
931 unsigned long data_width = 0;
932 unsigned long dimm_32bit;
933 unsigned long dimm_64bit;
934 unsigned long registered = 0;
935 unsigned long attribute = 0;
936 unsigned long buf0, buf1; /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
937 unsigned long bankcount;
938 unsigned long ddrtype;
939 unsigned long val;
940
Stefan Roeseb39ef632007-03-08 10:06:09 +0100941#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +0100942 ecc_enabled = TRUE;
Stefan Roeseb39ef632007-03-08 10:06:09 +0100943#else
944 ecc_enabled = FALSE;
945#endif
Stefan Roese43f32472007-02-20 10:43:34 +0100946 dimm_32bit = FALSE;
947 dimm_64bit = FALSE;
948 buf0 = FALSE;
949 buf1 = FALSE;
950
951 /*------------------------------------------------------------------
952 * Set memory controller options reg 1, SDRAM_MCOPT1.
953 *-----------------------------------------------------------------*/
954 mfsdram(SDRAM_MCOPT1, val);
955 mcopt1 = val & ~(SDRAM_MCOPT1_MCHK_MASK | SDRAM_MCOPT1_RDEN_MASK |
956 SDRAM_MCOPT1_PMU_MASK | SDRAM_MCOPT1_DMWD_MASK |
957 SDRAM_MCOPT1_UIOS_MASK | SDRAM_MCOPT1_BCNT_MASK |
958 SDRAM_MCOPT1_DDR_TYPE_MASK | SDRAM_MCOPT1_RWOO_MASK |
959 SDRAM_MCOPT1_WOOO_MASK | SDRAM_MCOPT1_DCOO_MASK |
960 SDRAM_MCOPT1_DREF_MASK);
961
962 mcopt1 |= SDRAM_MCOPT1_QDEP;
963 mcopt1 |= SDRAM_MCOPT1_PMU_OPEN;
964 mcopt1 |= SDRAM_MCOPT1_RWOO_DISABLED;
965 mcopt1 |= SDRAM_MCOPT1_WOOO_DISABLED;
966 mcopt1 |= SDRAM_MCOPT1_DCOO_DISABLED;
967 mcopt1 |= SDRAM_MCOPT1_DREF_NORMAL;
968
969 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
970 if (dimm_populated[dimm_num] != SDRAM_NONE) {
971 /* test ecc support */
972 ecc = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 11);
973 if (ecc != 0x02) /* ecc not supported */
974 ecc_enabled = FALSE;
975
976 /* test bank count */
977 bankcount = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 17);
978 if (bankcount == 0x04) /* bank count = 4 */
979 mcopt1 |= SDRAM_MCOPT1_4_BANKS;
980 else /* bank count = 8 */
981 mcopt1 |= SDRAM_MCOPT1_8_BANKS;
982
983 /* test DDR type */
984 ddrtype = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2);
985 /* test for buffered/unbuffered, registered, differential clocks */
986 registered = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 20);
987 attribute = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 21);
988
989 /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
990 if (dimm_num == 0) {
991 if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
992 mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
993 if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
994 mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
995 if (registered == 1) { /* DDR2 always buffered */
996 /* TODO: what about above comments ? */
997 mcopt1 |= SDRAM_MCOPT1_RDEN;
998 buf0 = TRUE;
999 } else {
1000 /* TODO: the mask 0x02 doesn't match Samsung def for byte 21. */
1001 if ((attribute & 0x02) == 0x00) {
1002 /* buffered not supported */
1003 buf0 = FALSE;
1004 } else {
1005 mcopt1 |= SDRAM_MCOPT1_RDEN;
1006 buf0 = TRUE;
1007 }
1008 }
1009 }
1010 else if (dimm_num == 1) {
1011 if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
1012 mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
1013 if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
1014 mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
1015 if (registered == 1) {
1016 /* DDR2 always buffered */
1017 mcopt1 |= SDRAM_MCOPT1_RDEN;
1018 buf1 = TRUE;
1019 } else {
1020 if ((attribute & 0x02) == 0x00) {
1021 /* buffered not supported */
1022 buf1 = FALSE;
1023 } else {
1024 mcopt1 |= SDRAM_MCOPT1_RDEN;
1025 buf1 = TRUE;
1026 }
1027 }
1028 }
1029
1030 /* Note that for DDR2 the byte 7 is reserved, but OK to keep code as is. */
1031 data_width = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 6) +
1032 (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 7)) << 8);
1033
1034 switch (data_width) {
1035 case 72:
1036 case 64:
1037 dimm_64bit = TRUE;
1038 break;
1039 case 40:
1040 case 32:
1041 dimm_32bit = TRUE;
1042 break;
1043 default:
Stefan Roese251161b2008-07-10 09:58:06 +02001044 printf("WARNING: Detected a DIMM with a data width of %lu bits.\n",
Stefan Roese43f32472007-02-20 10:43:34 +01001045 data_width);
1046 printf("Only DIMMs with 32 or 64 bit DDR-SDRAM widths are supported.\n");
1047 break;
1048 }
1049 }
1050 }
1051
1052 /* verify matching properties */
1053 if ((dimm_populated[0] != SDRAM_NONE) && (dimm_populated[1] != SDRAM_NONE)) {
1054 if (buf0 != buf1) {
1055 printf("ERROR: DIMM's buffered/unbuffered, registered, clocking don't match.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001056 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001057 }
1058 }
1059
1060 if ((dimm_64bit == TRUE) && (dimm_32bit == TRUE)) {
1061 printf("ERROR: Cannot mix 32 bit and 64 bit DDR-SDRAM DIMMs together.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001062 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001063 }
1064 else if ((dimm_64bit == TRUE) && (dimm_32bit == FALSE)) {
1065 mcopt1 |= SDRAM_MCOPT1_DMWD_64;
1066 } else if ((dimm_64bit == FALSE) && (dimm_32bit == TRUE)) {
1067 mcopt1 |= SDRAM_MCOPT1_DMWD_32;
1068 } else {
1069 printf("ERROR: Please install only 32 or 64 bit DDR-SDRAM DIMMs.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001070 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001071 }
1072
1073 if (ecc_enabled == TRUE)
1074 mcopt1 |= SDRAM_MCOPT1_MCHK_GEN;
1075 else
1076 mcopt1 |= SDRAM_MCOPT1_MCHK_NON;
1077
1078 mtsdram(SDRAM_MCOPT1, mcopt1);
1079}
1080
1081/*-----------------------------------------------------------------------------+
1082 * program_codt.
1083 *-----------------------------------------------------------------------------*/
1084static void program_codt(unsigned long *dimm_populated,
1085 unsigned char *iic0_dimm_addr,
1086 unsigned long num_dimm_banks)
1087{
1088 unsigned long codt;
1089 unsigned long modt0 = 0;
1090 unsigned long modt1 = 0;
1091 unsigned long modt2 = 0;
1092 unsigned long modt3 = 0;
1093 unsigned char dimm_num;
1094 unsigned char dimm_rank;
1095 unsigned char total_rank = 0;
1096 unsigned char total_dimm = 0;
1097 unsigned char dimm_type = 0;
1098 unsigned char firstSlot = 0;
1099
1100 /*------------------------------------------------------------------
1101 * Set the SDRAM Controller On Die Termination Register
1102 *-----------------------------------------------------------------*/
1103 mfsdram(SDRAM_CODT, codt);
1104 codt |= (SDRAM_CODT_IO_NMODE
1105 & (~SDRAM_CODT_DQS_SINGLE_END
1106 & ~SDRAM_CODT_CKSE_SINGLE_END
1107 & ~SDRAM_CODT_FEEBBACK_RCV_SINGLE_END
1108 & ~SDRAM_CODT_FEEBBACK_DRV_SINGLE_END));
1109
1110 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1111 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1112 dimm_rank = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 5);
1113 if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08) {
1114 dimm_rank = (dimm_rank & 0x0F) + 1;
1115 dimm_type = SDRAM_DDR2;
1116 } else {
1117 dimm_rank = dimm_rank & 0x0F;
1118 dimm_type = SDRAM_DDR1;
1119 }
1120
Stefan Roesebad41112007-03-01 21:11:36 +01001121 total_rank += dimm_rank;
1122 total_dimm++;
Stefan Roese43f32472007-02-20 10:43:34 +01001123 if ((dimm_num == 0) && (total_dimm == 1))
1124 firstSlot = TRUE;
1125 else
1126 firstSlot = FALSE;
1127 }
1128 }
1129 if (dimm_type == SDRAM_DDR2) {
1130 codt |= SDRAM_CODT_DQS_1_8_V_DDR2;
1131 if ((total_dimm == 1) && (firstSlot == TRUE)) {
Stefan Roese37628252008-08-06 14:05:38 +02001132 if (total_rank == 1) { /* PUUU */
Stefan Roesebad41112007-03-01 21:11:36 +01001133 codt |= CALC_ODT_R(0);
1134 modt0 = CALC_ODT_W(0);
Stefan Roese43f32472007-02-20 10:43:34 +01001135 modt1 = 0x00000000;
1136 modt2 = 0x00000000;
1137 modt3 = 0x00000000;
1138 }
Stefan Roese37628252008-08-06 14:05:38 +02001139 if (total_rank == 2) { /* PPUU */
Stefan Roesebad41112007-03-01 21:11:36 +01001140 codt |= CALC_ODT_R(0) | CALC_ODT_R(1);
Stefan Roese37628252008-08-06 14:05:38 +02001141 modt0 = CALC_ODT_W(0) | CALC_ODT_W(1);
1142 modt1 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001143 modt2 = 0x00000000;
1144 modt3 = 0x00000000;
1145 }
Stefan Roesebad41112007-03-01 21:11:36 +01001146 } else if ((total_dimm == 1) && (firstSlot != TRUE)) {
Stefan Roese37628252008-08-06 14:05:38 +02001147 if (total_rank == 1) { /* UUPU */
Stefan Roesebad41112007-03-01 21:11:36 +01001148 codt |= CALC_ODT_R(2);
1149 modt0 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001150 modt1 = 0x00000000;
Stefan Roesebad41112007-03-01 21:11:36 +01001151 modt2 = CALC_ODT_W(2);
Stefan Roese43f32472007-02-20 10:43:34 +01001152 modt3 = 0x00000000;
1153 }
Stefan Roese37628252008-08-06 14:05:38 +02001154 if (total_rank == 2) { /* UUPP */
Stefan Roesebad41112007-03-01 21:11:36 +01001155 codt |= CALC_ODT_R(2) | CALC_ODT_R(3);
1156 modt0 = 0x00000000;
1157 modt1 = 0x00000000;
Stefan Roese37628252008-08-06 14:05:38 +02001158 modt2 = CALC_ODT_W(2) | CALC_ODT_W(3);
1159 modt3 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001160 }
1161 }
1162 if (total_dimm == 2) {
Stefan Roese37628252008-08-06 14:05:38 +02001163 if (total_rank == 2) { /* PUPU */
Stefan Roesebad41112007-03-01 21:11:36 +01001164 codt |= CALC_ODT_R(0) | CALC_ODT_R(2);
1165 modt0 = CALC_ODT_RW(2);
Stefan Roese43f32472007-02-20 10:43:34 +01001166 modt1 = 0x00000000;
Stefan Roesebad41112007-03-01 21:11:36 +01001167 modt2 = CALC_ODT_RW(0);
Stefan Roese43f32472007-02-20 10:43:34 +01001168 modt3 = 0x00000000;
1169 }
Stefan Roese37628252008-08-06 14:05:38 +02001170 if (total_rank == 4) { /* PPPP */
Stefan Roese32a1cad2007-06-01 13:45:00 +02001171 codt |= CALC_ODT_R(0) | CALC_ODT_R(1) |
1172 CALC_ODT_R(2) | CALC_ODT_R(3);
Stefan Roese37628252008-08-06 14:05:38 +02001173 modt0 = CALC_ODT_RW(2) | CALC_ODT_RW(3);
Stefan Roesebad41112007-03-01 21:11:36 +01001174 modt1 = 0x00000000;
Stefan Roese37628252008-08-06 14:05:38 +02001175 modt2 = CALC_ODT_RW(0) | CALC_ODT_RW(1);
Stefan Roesebad41112007-03-01 21:11:36 +01001176 modt3 = 0x00000000;
Stefan Roese43f32472007-02-20 10:43:34 +01001177 }
1178 }
Wolfgang Denkf972e772007-03-04 01:36:05 +01001179 } else {
Stefan Roese43f32472007-02-20 10:43:34 +01001180 codt |= SDRAM_CODT_DQS_2_5_V_DDR1;
1181 modt0 = 0x00000000;
1182 modt1 = 0x00000000;
1183 modt2 = 0x00000000;
1184 modt3 = 0x00000000;
1185
1186 if (total_dimm == 1) {
1187 if (total_rank == 1)
1188 codt |= 0x00800000;
1189 if (total_rank == 2)
1190 codt |= 0x02800000;
1191 }
1192 if (total_dimm == 2) {
1193 if (total_rank == 2)
1194 codt |= 0x08800000;
1195 if (total_rank == 4)
1196 codt |= 0x2a800000;
1197 }
1198 }
1199
1200 debug("nb of dimm %d\n", total_dimm);
1201 debug("nb of rank %d\n", total_rank);
1202 if (total_dimm == 1)
1203 debug("dimm in slot %d\n", firstSlot);
1204
1205 mtsdram(SDRAM_CODT, codt);
1206 mtsdram(SDRAM_MODT0, modt0);
1207 mtsdram(SDRAM_MODT1, modt1);
1208 mtsdram(SDRAM_MODT2, modt2);
1209 mtsdram(SDRAM_MODT3, modt3);
1210}
1211
1212/*-----------------------------------------------------------------------------+
1213 * program_initplr.
1214 *-----------------------------------------------------------------------------*/
1215static void program_initplr(unsigned long *dimm_populated,
1216 unsigned char *iic0_dimm_addr,
1217 unsigned long num_dimm_banks,
Wolfgang Denkb38e0df2007-03-06 18:08:43 +01001218 ddr_cas_id_t selected_cas,
Stefan Roesebad41112007-03-01 21:11:36 +01001219 int write_recovery)
Stefan Roese43f32472007-02-20 10:43:34 +01001220{
Stefan Roesebad41112007-03-01 21:11:36 +01001221 u32 cas = 0;
1222 u32 odt = 0;
1223 u32 ods = 0;
1224 u32 mr;
1225 u32 wr;
1226 u32 emr;
1227 u32 emr2;
1228 u32 emr3;
1229 int dimm_num;
1230 int total_dimm = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01001231
1232 /******************************************************
1233 ** Assumption: if more than one DIMM, all DIMMs are the same
Wolfgang Denk52232fd2007-02-27 14:26:04 +01001234 ** as already checked in check_memory_type
Stefan Roese43f32472007-02-20 10:43:34 +01001235 ******************************************************/
1236
1237 if ((dimm_populated[0] == SDRAM_DDR1) || (dimm_populated[1] == SDRAM_DDR1)) {
1238 mtsdram(SDRAM_INITPLR0, 0x81B80000);
1239 mtsdram(SDRAM_INITPLR1, 0x81900400);
1240 mtsdram(SDRAM_INITPLR2, 0x81810000);
1241 mtsdram(SDRAM_INITPLR3, 0xff800162);
1242 mtsdram(SDRAM_INITPLR4, 0x81900400);
1243 mtsdram(SDRAM_INITPLR5, 0x86080000);
1244 mtsdram(SDRAM_INITPLR6, 0x86080000);
1245 mtsdram(SDRAM_INITPLR7, 0x81000062);
1246 } else if ((dimm_populated[0] == SDRAM_DDR2) || (dimm_populated[1] == SDRAM_DDR2)) {
1247 switch (selected_cas) {
Stefan Roese43f32472007-02-20 10:43:34 +01001248 case DDR_CAS_3:
Stefan Roesebad41112007-03-01 21:11:36 +01001249 cas = 3 << 4;
Stefan Roese43f32472007-02-20 10:43:34 +01001250 break;
1251 case DDR_CAS_4:
Stefan Roesebad41112007-03-01 21:11:36 +01001252 cas = 4 << 4;
Stefan Roese43f32472007-02-20 10:43:34 +01001253 break;
1254 case DDR_CAS_5:
Stefan Roesebad41112007-03-01 21:11:36 +01001255 cas = 5 << 4;
1256 break;
1257 default:
1258 printf("ERROR: ucode error on selected_cas value %d", selected_cas);
Heiko Schocher68310b02007-06-25 19:11:37 +02001259 spd_ddr_init_hang ();
Stefan Roesebad41112007-03-01 21:11:36 +01001260 break;
1261 }
1262
1263#if 0
1264 /*
1265 * ToDo - Still a problem with the write recovery:
1266 * On the Corsair CM2X512-5400C4 module, setting write recovery
1267 * in the INITPLR reg to the value calculated in program_mode()
1268 * results in not correctly working DDR2 memory (crash after
1269 * relocation).
1270 *
1271 * So for now, set the write recovery to 3. This seems to work
1272 * on the Corair module too.
1273 *
1274 * 2007-03-01, sr
1275 */
1276 switch (write_recovery) {
1277 case 3:
1278 wr = WRITE_RECOV_3;
1279 break;
1280 case 4:
1281 wr = WRITE_RECOV_4;
1282 break;
1283 case 5:
1284 wr = WRITE_RECOV_5;
1285 break;
1286 case 6:
1287 wr = WRITE_RECOV_6;
Stefan Roese43f32472007-02-20 10:43:34 +01001288 break;
1289 default:
Stefan Roesebad41112007-03-01 21:11:36 +01001290 printf("ERROR: write recovery not support (%d)", write_recovery);
Heiko Schocher68310b02007-06-25 19:11:37 +02001291 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001292 break;
1293 }
Stefan Roesebad41112007-03-01 21:11:36 +01001294#else
1295 wr = WRITE_RECOV_3; /* test-only, see description above */
1296#endif
1297
1298 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++)
1299 if (dimm_populated[dimm_num] != SDRAM_NONE)
1300 total_dimm++;
1301 if (total_dimm == 1) {
1302 odt = ODT_150_OHM;
1303 ods = ODS_FULL;
1304 } else if (total_dimm == 2) {
1305 odt = ODT_75_OHM;
1306 ods = ODS_REDUCED;
1307 } else {
1308 printf("ERROR: Unsupported number of DIMM's (%d)", total_dimm);
Heiko Schocher68310b02007-06-25 19:11:37 +02001309 spd_ddr_init_hang ();
Stefan Roesebad41112007-03-01 21:11:36 +01001310 }
Stefan Roese43f32472007-02-20 10:43:34 +01001311
Stefan Roesebad41112007-03-01 21:11:36 +01001312 mr = CMD_EMR | SELECT_MR | BURST_LEN_4 | wr | cas;
1313 emr = CMD_EMR | SELECT_EMR | odt | ods;
1314 emr2 = CMD_EMR | SELECT_EMR2;
1315 emr3 = CMD_EMR | SELECT_EMR3;
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001316 /* NOP - Wait 106 MemClk cycles */
1317 mtsdram(SDRAM_INITPLR0, SDRAM_INITPLR_ENABLE | CMD_NOP |
1318 SDRAM_INITPLR_IMWT_ENCODE(106));
Stefan Roesebad41112007-03-01 21:11:36 +01001319 udelay(1000);
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001320 /* precharge 4 MemClk cycles */
1321 mtsdram(SDRAM_INITPLR1, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
1322 SDRAM_INITPLR_IMWT_ENCODE(4));
1323 /* EMR2 - Wait tMRD (2 MemClk cycles) */
1324 mtsdram(SDRAM_INITPLR2, SDRAM_INITPLR_ENABLE | emr2 |
1325 SDRAM_INITPLR_IMWT_ENCODE(2));
1326 /* EMR3 - Wait tMRD (2 MemClk cycles) */
1327 mtsdram(SDRAM_INITPLR3, SDRAM_INITPLR_ENABLE | emr3 |
1328 SDRAM_INITPLR_IMWT_ENCODE(2));
1329 /* EMR DLL ENABLE - Wait tMRD (2 MemClk cycles) */
1330 mtsdram(SDRAM_INITPLR4, SDRAM_INITPLR_ENABLE | emr |
1331 SDRAM_INITPLR_IMWT_ENCODE(2));
1332 /* MR w/ DLL reset - 200 cycle wait for DLL reset */
1333 mtsdram(SDRAM_INITPLR5, SDRAM_INITPLR_ENABLE | mr | DLL_RESET |
1334 SDRAM_INITPLR_IMWT_ENCODE(200));
Stefan Roesebad41112007-03-01 21:11:36 +01001335 udelay(1000);
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07001336 /* precharge 4 MemClk cycles */
1337 mtsdram(SDRAM_INITPLR6, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
1338 SDRAM_INITPLR_IMWT_ENCODE(4));
1339 /* Refresh 25 MemClk cycles */
1340 mtsdram(SDRAM_INITPLR7, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1341 SDRAM_INITPLR_IMWT_ENCODE(25));
1342 /* Refresh 25 MemClk cycles */
1343 mtsdram(SDRAM_INITPLR8, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1344 SDRAM_INITPLR_IMWT_ENCODE(25));
1345 /* Refresh 25 MemClk cycles */
1346 mtsdram(SDRAM_INITPLR9, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1347 SDRAM_INITPLR_IMWT_ENCODE(25));
1348 /* Refresh 25 MemClk cycles */
1349 mtsdram(SDRAM_INITPLR10, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
1350 SDRAM_INITPLR_IMWT_ENCODE(25));
1351 /* MR w/o DLL reset - Wait tMRD (2 MemClk cycles) */
1352 mtsdram(SDRAM_INITPLR11, SDRAM_INITPLR_ENABLE | mr |
1353 SDRAM_INITPLR_IMWT_ENCODE(2));
1354 /* EMR OCD Default - Wait tMRD (2 MemClk cycles) */
1355 mtsdram(SDRAM_INITPLR12, SDRAM_INITPLR_ENABLE | OCD_CALIB_DEF |
1356 SDRAM_INITPLR_IMWT_ENCODE(2) | emr);
1357 /* EMR OCD Exit */
1358 mtsdram(SDRAM_INITPLR13, SDRAM_INITPLR_ENABLE | emr |
1359 SDRAM_INITPLR_IMWT_ENCODE(2));
Stefan Roese43f32472007-02-20 10:43:34 +01001360 } else {
1361 printf("ERROR: ucode error as unknown DDR type in program_initplr");
Heiko Schocher68310b02007-06-25 19:11:37 +02001362 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001363 }
1364}
1365
1366/*------------------------------------------------------------------
1367 * This routine programs the SDRAM_MMODE register.
1368 * the selected_cas is an output parameter, that will be passed
1369 * by caller to call the above program_initplr( )
1370 *-----------------------------------------------------------------*/
1371static void program_mode(unsigned long *dimm_populated,
1372 unsigned char *iic0_dimm_addr,
1373 unsigned long num_dimm_banks,
Stefan Roesebad41112007-03-01 21:11:36 +01001374 ddr_cas_id_t *selected_cas,
1375 int *write_recovery)
Stefan Roese43f32472007-02-20 10:43:34 +01001376{
1377 unsigned long dimm_num;
1378 unsigned long sdram_ddr1;
1379 unsigned long t_wr_ns;
1380 unsigned long t_wr_clk;
1381 unsigned long cas_bit;
1382 unsigned long cas_index;
1383 unsigned long sdram_freq;
1384 unsigned long ddr_check;
1385 unsigned long mmode;
1386 unsigned long tcyc_reg;
1387 unsigned long cycle_2_0_clk;
1388 unsigned long cycle_2_5_clk;
1389 unsigned long cycle_3_0_clk;
1390 unsigned long cycle_4_0_clk;
1391 unsigned long cycle_5_0_clk;
1392 unsigned long max_2_0_tcyc_ns_x_100;
1393 unsigned long max_2_5_tcyc_ns_x_100;
1394 unsigned long max_3_0_tcyc_ns_x_100;
1395 unsigned long max_4_0_tcyc_ns_x_100;
1396 unsigned long max_5_0_tcyc_ns_x_100;
1397 unsigned long cycle_time_ns_x_100[3];
Stefan Roeseedd73f22007-10-21 08:12:41 +02001398 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001399 unsigned char cas_2_0_available;
1400 unsigned char cas_2_5_available;
1401 unsigned char cas_3_0_available;
1402 unsigned char cas_4_0_available;
1403 unsigned char cas_5_0_available;
1404 unsigned long sdr_ddrpll;
1405
1406 /*------------------------------------------------------------------
1407 * Get the board configuration info.
1408 *-----------------------------------------------------------------*/
1409 get_sys_info(&board_cfg);
1410
Stefan Roeseb39ef632007-03-08 10:06:09 +01001411 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001412 sdram_freq = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(sdr_ddrpll), 1);
Stefan Roese5d48a842007-03-31 13:15:06 +02001413 debug("sdram_freq=%d\n", sdram_freq);
Stefan Roese43f32472007-02-20 10:43:34 +01001414
1415 /*------------------------------------------------------------------
1416 * Handle the timing. We need to find the worst case timing of all
1417 * the dimm modules installed.
1418 *-----------------------------------------------------------------*/
1419 t_wr_ns = 0;
1420 cas_2_0_available = TRUE;
1421 cas_2_5_available = TRUE;
1422 cas_3_0_available = TRUE;
1423 cas_4_0_available = TRUE;
1424 cas_5_0_available = TRUE;
1425 max_2_0_tcyc_ns_x_100 = 10;
1426 max_2_5_tcyc_ns_x_100 = 10;
1427 max_3_0_tcyc_ns_x_100 = 10;
1428 max_4_0_tcyc_ns_x_100 = 10;
1429 max_5_0_tcyc_ns_x_100 = 10;
1430 sdram_ddr1 = TRUE;
1431
1432 /* loop through all the DIMM slots on the board */
1433 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1434 /* If a dimm is installed in a particular slot ... */
1435 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1436 if (dimm_populated[dimm_num] == SDRAM_DDR1)
1437 sdram_ddr1 = TRUE;
1438 else
1439 sdram_ddr1 = FALSE;
1440
1441 /* t_wr_ns = max(t_wr_ns, (unsigned long)dimm_spd[dimm_num][36] >> 2); */ /* not used in this loop. */
1442 cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18);
Stefan Roese5d48a842007-03-31 13:15:06 +02001443 debug("cas_bit[SPD byte 18]=%02x\n", cas_bit);
Stefan Roese43f32472007-02-20 10:43:34 +01001444
1445 /* For a particular DIMM, grab the three CAS values it supports */
1446 for (cas_index = 0; cas_index < 3; cas_index++) {
1447 switch (cas_index) {
1448 case 0:
1449 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
1450 break;
1451 case 1:
1452 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23);
1453 break;
1454 default:
1455 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25);
1456 break;
1457 }
1458
1459 if ((tcyc_reg & 0x0F) >= 10) {
1460 if ((tcyc_reg & 0x0F) == 0x0D) {
1461 /* Convert from hex to decimal */
Stefan Roese5d48a842007-03-31 13:15:06 +02001462 cycle_time_ns_x_100[cas_index] =
1463 (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
Stefan Roese43f32472007-02-20 10:43:34 +01001464 } else {
1465 printf("ERROR: SPD reported Tcyc is incorrect for DIMM "
1466 "in slot %d\n", (unsigned int)dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +02001467 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001468 }
1469 } else {
1470 /* Convert from hex to decimal */
Stefan Roese5d48a842007-03-31 13:15:06 +02001471 cycle_time_ns_x_100[cas_index] =
1472 (((tcyc_reg & 0xF0) >> 4) * 100) +
Stefan Roese43f32472007-02-20 10:43:34 +01001473 ((tcyc_reg & 0x0F)*10);
1474 }
Stefan Roese5d48a842007-03-31 13:15:06 +02001475 debug("cas_index=%d: cycle_time_ns_x_100=%d\n", cas_index,
1476 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001477 }
1478
1479 /* The rest of this routine determines if CAS 2.0, 2.5, 3.0, 4.0 and 5.0 are */
1480 /* supported for a particular DIMM. */
1481 cas_index = 0;
1482
1483 if (sdram_ddr1) {
1484 /*
1485 * DDR devices use the following bitmask for CAS latency:
1486 * Bit 7 6 5 4 3 2 1 0
1487 * TBD 4.0 3.5 3.0 2.5 2.0 1.5 1.0
1488 */
Stefan Roese5d48a842007-03-31 13:15:06 +02001489 if (((cas_bit & 0x40) == 0x40) && (cas_index < 3) &&
1490 (cycle_time_ns_x_100[cas_index] != 0)) {
1491 max_4_0_tcyc_ns_x_100 = max(max_4_0_tcyc_ns_x_100,
1492 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001493 cas_index++;
1494 } else {
1495 if (cas_index != 0)
1496 cas_index++;
1497 cas_4_0_available = FALSE;
1498 }
1499
Stefan Roese5d48a842007-03-31 13:15:06 +02001500 if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
1501 (cycle_time_ns_x_100[cas_index] != 0)) {
1502 max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
1503 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001504 cas_index++;
1505 } else {
1506 if (cas_index != 0)
1507 cas_index++;
1508 cas_3_0_available = FALSE;
1509 }
1510
Stefan Roese5d48a842007-03-31 13:15:06 +02001511 if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
1512 (cycle_time_ns_x_100[cas_index] != 0)) {
1513 max_2_5_tcyc_ns_x_100 = max(max_2_5_tcyc_ns_x_100,
1514 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001515 cas_index++;
1516 } else {
1517 if (cas_index != 0)
1518 cas_index++;
1519 cas_2_5_available = FALSE;
1520 }
1521
Stefan Roese5d48a842007-03-31 13:15:06 +02001522 if (((cas_bit & 0x04) == 0x04) && (cas_index < 3) &&
1523 (cycle_time_ns_x_100[cas_index] != 0)) {
1524 max_2_0_tcyc_ns_x_100 = max(max_2_0_tcyc_ns_x_100,
1525 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001526 cas_index++;
1527 } else {
1528 if (cas_index != 0)
1529 cas_index++;
1530 cas_2_0_available = FALSE;
1531 }
1532 } else {
1533 /*
1534 * DDR2 devices use the following bitmask for CAS latency:
1535 * Bit 7 6 5 4 3 2 1 0
1536 * TBD 6.0 5.0 4.0 3.0 2.0 TBD TBD
1537 */
Stefan Roese5d48a842007-03-31 13:15:06 +02001538 if (((cas_bit & 0x20) == 0x20) && (cas_index < 3) &&
1539 (cycle_time_ns_x_100[cas_index] != 0)) {
1540 max_5_0_tcyc_ns_x_100 = max(max_5_0_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++;
1546 cas_5_0_available = FALSE;
1547 }
1548
Stefan Roese5d48a842007-03-31 13:15:06 +02001549 if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
1550 (cycle_time_ns_x_100[cas_index] != 0)) {
1551 max_4_0_tcyc_ns_x_100 = max(max_4_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++;
1557 cas_4_0_available = FALSE;
1558 }
1559
Stefan Roese5d48a842007-03-31 13:15:06 +02001560 if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
1561 (cycle_time_ns_x_100[cas_index] != 0)) {
1562 max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
1563 cycle_time_ns_x_100[cas_index]);
Stefan Roese43f32472007-02-20 10:43:34 +01001564 cas_index++;
1565 } else {
1566 if (cas_index != 0)
1567 cas_index++;
1568 cas_3_0_available = FALSE;
1569 }
1570 }
1571 }
1572 }
1573
1574 /*------------------------------------------------------------------
1575 * Set the SDRAM mode, SDRAM_MMODE
1576 *-----------------------------------------------------------------*/
1577 mfsdram(SDRAM_MMODE, mmode);
1578 mmode = mmode & ~(SDRAM_MMODE_WR_MASK | SDRAM_MMODE_DCL_MASK);
1579
Stefan Roeseb39ef632007-03-08 10:06:09 +01001580 /* add 10 here because of rounding problems */
1581 cycle_2_0_clk = MULDIV64(ONE_BILLION, 100, max_2_0_tcyc_ns_x_100) + 10;
1582 cycle_2_5_clk = MULDIV64(ONE_BILLION, 100, max_2_5_tcyc_ns_x_100) + 10;
1583 cycle_3_0_clk = MULDIV64(ONE_BILLION, 100, max_3_0_tcyc_ns_x_100) + 10;
1584 cycle_4_0_clk = MULDIV64(ONE_BILLION, 100, max_4_0_tcyc_ns_x_100) + 10;
1585 cycle_5_0_clk = MULDIV64(ONE_BILLION, 100, max_5_0_tcyc_ns_x_100) + 10;
Stefan Roese5d48a842007-03-31 13:15:06 +02001586 debug("cycle_3_0_clk=%d\n", cycle_3_0_clk);
1587 debug("cycle_4_0_clk=%d\n", cycle_4_0_clk);
1588 debug("cycle_5_0_clk=%d\n", cycle_5_0_clk);
Stefan Roese43f32472007-02-20 10:43:34 +01001589
1590 if (sdram_ddr1 == TRUE) { /* DDR1 */
1591 if ((cas_2_0_available == TRUE) && (sdram_freq <= cycle_2_0_clk)) {
1592 mmode |= SDRAM_MMODE_DCL_DDR1_2_0_CLK;
1593 *selected_cas = DDR_CAS_2;
1594 } else if ((cas_2_5_available == TRUE) && (sdram_freq <= cycle_2_5_clk)) {
1595 mmode |= SDRAM_MMODE_DCL_DDR1_2_5_CLK;
1596 *selected_cas = DDR_CAS_2_5;
1597 } else if ((cas_3_0_available == TRUE) && (sdram_freq <= cycle_3_0_clk)) {
1598 mmode |= SDRAM_MMODE_DCL_DDR1_3_0_CLK;
1599 *selected_cas = DDR_CAS_3;
1600 } else {
1601 printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
1602 printf("Only DIMMs DDR1 with CAS latencies of 2.0, 2.5, and 3.0 are supported.\n");
1603 printf("Make sure the PLB speed is within the supported range of the DIMMs.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001604 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001605 }
1606 } else { /* DDR2 */
Stefan Roesef88e3602007-03-31 08:46:08 +02001607 debug("cas_3_0_available=%d\n", cas_3_0_available);
1608 debug("cas_4_0_available=%d\n", cas_4_0_available);
1609 debug("cas_5_0_available=%d\n", cas_5_0_available);
Stefan Roese43f32472007-02-20 10:43:34 +01001610 if ((cas_3_0_available == TRUE) && (sdram_freq <= cycle_3_0_clk)) {
1611 mmode |= SDRAM_MMODE_DCL_DDR2_3_0_CLK;
1612 *selected_cas = DDR_CAS_3;
1613 } else if ((cas_4_0_available == TRUE) && (sdram_freq <= cycle_4_0_clk)) {
1614 mmode |= SDRAM_MMODE_DCL_DDR2_4_0_CLK;
1615 *selected_cas = DDR_CAS_4;
1616 } else if ((cas_5_0_available == TRUE) && (sdram_freq <= cycle_5_0_clk)) {
1617 mmode |= SDRAM_MMODE_DCL_DDR2_5_0_CLK;
1618 *selected_cas = DDR_CAS_5;
1619 } else {
1620 printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
1621 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 +01001622 printf("Make sure the PLB speed is within the supported range of the DIMMs.\n");
1623 printf("cas3=%d cas4=%d cas5=%d\n",
1624 cas_3_0_available, cas_4_0_available, cas_5_0_available);
Stefan Roese251161b2008-07-10 09:58:06 +02001625 printf("sdram_freq=%lu cycle3=%lu cycle4=%lu cycle5=%lu\n\n",
Stefan Roeseb39ef632007-03-08 10:06:09 +01001626 sdram_freq, cycle_3_0_clk, cycle_4_0_clk, cycle_5_0_clk);
Heiko Schocher68310b02007-06-25 19:11:37 +02001627 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001628 }
1629 }
1630
1631 if (sdram_ddr1 == TRUE)
1632 mmode |= SDRAM_MMODE_WR_DDR1;
1633 else {
1634
1635 /* loop through all the DIMM slots on the board */
1636 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1637 /* If a dimm is installed in a particular slot ... */
1638 if (dimm_populated[dimm_num] != SDRAM_NONE)
1639 t_wr_ns = max(t_wr_ns,
1640 spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
1641 }
1642
1643 /*
1644 * convert from nanoseconds to ddr clocks
1645 * round up if necessary
1646 */
1647 t_wr_clk = MULDIV64(sdram_freq, t_wr_ns, ONE_BILLION);
1648 ddr_check = MULDIV64(ONE_BILLION, t_wr_clk, t_wr_ns);
1649 if (sdram_freq != ddr_check)
1650 t_wr_clk++;
1651
1652 switch (t_wr_clk) {
1653 case 0:
1654 case 1:
1655 case 2:
1656 case 3:
1657 mmode |= SDRAM_MMODE_WR_DDR2_3_CYC;
1658 break;
1659 case 4:
1660 mmode |= SDRAM_MMODE_WR_DDR2_4_CYC;
1661 break;
1662 case 5:
1663 mmode |= SDRAM_MMODE_WR_DDR2_5_CYC;
1664 break;
1665 default:
1666 mmode |= SDRAM_MMODE_WR_DDR2_6_CYC;
1667 break;
1668 }
Stefan Roesebad41112007-03-01 21:11:36 +01001669 *write_recovery = t_wr_clk;
Stefan Roese43f32472007-02-20 10:43:34 +01001670 }
1671
Stefan Roesebad41112007-03-01 21:11:36 +01001672 debug("CAS latency = %d\n", *selected_cas);
1673 debug("Write recovery = %d\n", *write_recovery);
1674
Stefan Roese43f32472007-02-20 10:43:34 +01001675 mtsdram(SDRAM_MMODE, mmode);
1676}
1677
1678/*-----------------------------------------------------------------------------+
1679 * program_rtr.
1680 *-----------------------------------------------------------------------------*/
1681static void program_rtr(unsigned long *dimm_populated,
1682 unsigned char *iic0_dimm_addr,
1683 unsigned long num_dimm_banks)
1684{
Stefan Roeseedd73f22007-10-21 08:12:41 +02001685 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001686 unsigned long max_refresh_rate;
1687 unsigned long dimm_num;
1688 unsigned long refresh_rate_type;
1689 unsigned long refresh_rate;
1690 unsigned long rint;
1691 unsigned long sdram_freq;
1692 unsigned long sdr_ddrpll;
1693 unsigned long val;
1694
1695 /*------------------------------------------------------------------
1696 * Get the board configuration info.
1697 *-----------------------------------------------------------------*/
1698 get_sys_info(&board_cfg);
1699
1700 /*------------------------------------------------------------------
1701 * Set the SDRAM Refresh Timing Register, SDRAM_RTR
1702 *-----------------------------------------------------------------*/
Stefan Roeseb39ef632007-03-08 10:06:09 +01001703 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001704 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
1705
1706 max_refresh_rate = 0;
1707 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1708 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1709
1710 refresh_rate_type = spd_read(iic0_dimm_addr[dimm_num], 12);
1711 refresh_rate_type &= 0x7F;
1712 switch (refresh_rate_type) {
1713 case 0:
1714 refresh_rate = 15625;
1715 break;
1716 case 1:
1717 refresh_rate = 3906;
1718 break;
1719 case 2:
1720 refresh_rate = 7812;
1721 break;
1722 case 3:
1723 refresh_rate = 31250;
1724 break;
1725 case 4:
1726 refresh_rate = 62500;
1727 break;
1728 case 5:
1729 refresh_rate = 125000;
1730 break;
1731 default:
1732 refresh_rate = 0;
1733 printf("ERROR: DIMM %d unsupported refresh rate/type.\n",
1734 (unsigned int)dimm_num);
1735 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001736 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01001737 break;
1738 }
1739
1740 max_refresh_rate = max(max_refresh_rate, refresh_rate);
1741 }
1742 }
1743
1744 rint = MULDIV64(sdram_freq, max_refresh_rate, ONE_BILLION);
1745 mfsdram(SDRAM_RTR, val);
1746 mtsdram(SDRAM_RTR, (val & ~SDRAM_RTR_RINT_MASK) |
1747 (SDRAM_RTR_RINT_ENCODE(rint)));
1748}
1749
1750/*------------------------------------------------------------------
1751 * This routine programs the SDRAM_TRx registers.
1752 *-----------------------------------------------------------------*/
1753static void program_tr(unsigned long *dimm_populated,
1754 unsigned char *iic0_dimm_addr,
1755 unsigned long num_dimm_banks)
1756{
1757 unsigned long dimm_num;
1758 unsigned long sdram_ddr1;
1759 unsigned long t_rp_ns;
1760 unsigned long t_rcd_ns;
1761 unsigned long t_rrd_ns;
1762 unsigned long t_ras_ns;
1763 unsigned long t_rc_ns;
1764 unsigned long t_rfc_ns;
1765 unsigned long t_wpc_ns;
1766 unsigned long t_wtr_ns;
1767 unsigned long t_rpc_ns;
1768 unsigned long t_rp_clk;
1769 unsigned long t_rcd_clk;
1770 unsigned long t_rrd_clk;
1771 unsigned long t_ras_clk;
1772 unsigned long t_rc_clk;
1773 unsigned long t_rfc_clk;
1774 unsigned long t_wpc_clk;
1775 unsigned long t_wtr_clk;
1776 unsigned long t_rpc_clk;
1777 unsigned long sdtr1, sdtr2, sdtr3;
1778 unsigned long ddr_check;
1779 unsigned long sdram_freq;
1780 unsigned long sdr_ddrpll;
1781
Stefan Roeseedd73f22007-10-21 08:12:41 +02001782 PPC4xx_SYS_INFO board_cfg;
Stefan Roese43f32472007-02-20 10:43:34 +01001783
1784 /*------------------------------------------------------------------
1785 * Get the board configuration info.
1786 *-----------------------------------------------------------------*/
1787 get_sys_info(&board_cfg);
1788
Stefan Roeseb39ef632007-03-08 10:06:09 +01001789 mfsdr(SDR0_DDR0, sdr_ddrpll);
Stefan Roese43f32472007-02-20 10:43:34 +01001790 sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
1791
1792 /*------------------------------------------------------------------
1793 * Handle the timing. We need to find the worst case timing of all
1794 * the dimm modules installed.
1795 *-----------------------------------------------------------------*/
1796 t_rp_ns = 0;
1797 t_rrd_ns = 0;
1798 t_rcd_ns = 0;
1799 t_ras_ns = 0;
1800 t_rc_ns = 0;
1801 t_rfc_ns = 0;
1802 t_wpc_ns = 0;
1803 t_wtr_ns = 0;
1804 t_rpc_ns = 0;
1805 sdram_ddr1 = TRUE;
1806
1807 /* loop through all the DIMM slots on the board */
1808 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1809 /* If a dimm is installed in a particular slot ... */
1810 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1811 if (dimm_populated[dimm_num] == SDRAM_DDR2)
1812 sdram_ddr1 = TRUE;
1813 else
1814 sdram_ddr1 = FALSE;
1815
1816 t_rcd_ns = max(t_rcd_ns, spd_read(iic0_dimm_addr[dimm_num], 29) >> 2);
1817 t_rrd_ns = max(t_rrd_ns, spd_read(iic0_dimm_addr[dimm_num], 28) >> 2);
1818 t_rp_ns = max(t_rp_ns, spd_read(iic0_dimm_addr[dimm_num], 27) >> 2);
1819 t_ras_ns = max(t_ras_ns, spd_read(iic0_dimm_addr[dimm_num], 30));
1820 t_rc_ns = max(t_rc_ns, spd_read(iic0_dimm_addr[dimm_num], 41));
1821 t_rfc_ns = max(t_rfc_ns, spd_read(iic0_dimm_addr[dimm_num], 42));
1822 }
1823 }
1824
1825 /*------------------------------------------------------------------
1826 * Set the SDRAM Timing Reg 1, SDRAM_TR1
1827 *-----------------------------------------------------------------*/
1828 mfsdram(SDRAM_SDTR1, sdtr1);
1829 sdtr1 &= ~(SDRAM_SDTR1_LDOF_MASK | SDRAM_SDTR1_RTW_MASK |
1830 SDRAM_SDTR1_WTWO_MASK | SDRAM_SDTR1_RTRO_MASK);
1831
1832 /* default values */
1833 sdtr1 |= SDRAM_SDTR1_LDOF_2_CLK;
1834 sdtr1 |= SDRAM_SDTR1_RTW_2_CLK;
1835
1836 /* normal operations */
1837 sdtr1 |= SDRAM_SDTR1_WTWO_0_CLK;
1838 sdtr1 |= SDRAM_SDTR1_RTRO_1_CLK;
1839
1840 mtsdram(SDRAM_SDTR1, sdtr1);
1841
1842 /*------------------------------------------------------------------
1843 * Set the SDRAM Timing Reg 2, SDRAM_TR2
1844 *-----------------------------------------------------------------*/
1845 mfsdram(SDRAM_SDTR2, sdtr2);
1846 sdtr2 &= ~(SDRAM_SDTR2_RCD_MASK | SDRAM_SDTR2_WTR_MASK |
1847 SDRAM_SDTR2_XSNR_MASK | SDRAM_SDTR2_WPC_MASK |
1848 SDRAM_SDTR2_RPC_MASK | SDRAM_SDTR2_RP_MASK |
1849 SDRAM_SDTR2_RRD_MASK);
1850
1851 /*
1852 * convert t_rcd from nanoseconds to ddr clocks
1853 * round up if necessary
1854 */
1855 t_rcd_clk = MULDIV64(sdram_freq, t_rcd_ns, ONE_BILLION);
1856 ddr_check = MULDIV64(ONE_BILLION, t_rcd_clk, t_rcd_ns);
1857 if (sdram_freq != ddr_check)
1858 t_rcd_clk++;
1859
1860 switch (t_rcd_clk) {
1861 case 0:
1862 case 1:
1863 sdtr2 |= SDRAM_SDTR2_RCD_1_CLK;
1864 break;
1865 case 2:
1866 sdtr2 |= SDRAM_SDTR2_RCD_2_CLK;
1867 break;
1868 case 3:
1869 sdtr2 |= SDRAM_SDTR2_RCD_3_CLK;
1870 break;
1871 case 4:
1872 sdtr2 |= SDRAM_SDTR2_RCD_4_CLK;
1873 break;
1874 default:
1875 sdtr2 |= SDRAM_SDTR2_RCD_5_CLK;
1876 break;
1877 }
1878
1879 if (sdram_ddr1 == TRUE) { /* DDR1 */
1880 if (sdram_freq < 200000000) {
1881 sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
1882 sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
1883 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
1884 } else {
1885 sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
1886 sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
1887 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
1888 }
1889 } else { /* DDR2 */
1890 /* loop through all the DIMM slots on the board */
1891 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1892 /* If a dimm is installed in a particular slot ... */
1893 if (dimm_populated[dimm_num] != SDRAM_NONE) {
1894 t_wpc_ns = max(t_wtr_ns, spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
1895 t_wtr_ns = max(t_wtr_ns, spd_read(iic0_dimm_addr[dimm_num], 37) >> 2);
1896 t_rpc_ns = max(t_rpc_ns, spd_read(iic0_dimm_addr[dimm_num], 38) >> 2);
1897 }
1898 }
1899
1900 /*
1901 * convert from nanoseconds to ddr clocks
1902 * round up if necessary
1903 */
1904 t_wpc_clk = MULDIV64(sdram_freq, t_wpc_ns, ONE_BILLION);
1905 ddr_check = MULDIV64(ONE_BILLION, t_wpc_clk, t_wpc_ns);
1906 if (sdram_freq != ddr_check)
1907 t_wpc_clk++;
1908
1909 switch (t_wpc_clk) {
1910 case 0:
1911 case 1:
1912 case 2:
1913 sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
1914 break;
1915 case 3:
1916 sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
1917 break;
1918 case 4:
1919 sdtr2 |= SDRAM_SDTR2_WPC_4_CLK;
1920 break;
1921 case 5:
1922 sdtr2 |= SDRAM_SDTR2_WPC_5_CLK;
1923 break;
1924 default:
1925 sdtr2 |= SDRAM_SDTR2_WPC_6_CLK;
1926 break;
1927 }
1928
1929 /*
1930 * convert from nanoseconds to ddr clocks
1931 * round up if necessary
1932 */
1933 t_wtr_clk = MULDIV64(sdram_freq, t_wtr_ns, ONE_BILLION);
1934 ddr_check = MULDIV64(ONE_BILLION, t_wtr_clk, t_wtr_ns);
1935 if (sdram_freq != ddr_check)
1936 t_wtr_clk++;
1937
1938 switch (t_wtr_clk) {
1939 case 0:
1940 case 1:
1941 sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
1942 break;
1943 case 2:
1944 sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
1945 break;
1946 case 3:
1947 sdtr2 |= SDRAM_SDTR2_WTR_3_CLK;
1948 break;
1949 default:
1950 sdtr2 |= SDRAM_SDTR2_WTR_4_CLK;
1951 break;
1952 }
1953
1954 /*
1955 * convert from nanoseconds to ddr clocks
1956 * round up if necessary
1957 */
1958 t_rpc_clk = MULDIV64(sdram_freq, t_rpc_ns, ONE_BILLION);
1959 ddr_check = MULDIV64(ONE_BILLION, t_rpc_clk, t_rpc_ns);
1960 if (sdram_freq != ddr_check)
1961 t_rpc_clk++;
1962
1963 switch (t_rpc_clk) {
1964 case 0:
1965 case 1:
1966 case 2:
1967 sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
1968 break;
1969 case 3:
1970 sdtr2 |= SDRAM_SDTR2_RPC_3_CLK;
1971 break;
1972 default:
1973 sdtr2 |= SDRAM_SDTR2_RPC_4_CLK;
1974 break;
1975 }
1976 }
1977
1978 /* default value */
1979 sdtr2 |= SDRAM_SDTR2_XSNR_16_CLK;
1980
1981 /*
1982 * convert t_rrd from nanoseconds to ddr clocks
1983 * round up if necessary
1984 */
1985 t_rrd_clk = MULDIV64(sdram_freq, t_rrd_ns, ONE_BILLION);
1986 ddr_check = MULDIV64(ONE_BILLION, t_rrd_clk, t_rrd_ns);
1987 if (sdram_freq != ddr_check)
1988 t_rrd_clk++;
1989
1990 if (t_rrd_clk == 3)
1991 sdtr2 |= SDRAM_SDTR2_RRD_3_CLK;
1992 else
1993 sdtr2 |= SDRAM_SDTR2_RRD_2_CLK;
1994
1995 /*
1996 * convert t_rp from nanoseconds to ddr clocks
1997 * round up if necessary
1998 */
1999 t_rp_clk = MULDIV64(sdram_freq, t_rp_ns, ONE_BILLION);
2000 ddr_check = MULDIV64(ONE_BILLION, t_rp_clk, t_rp_ns);
2001 if (sdram_freq != ddr_check)
2002 t_rp_clk++;
2003
2004 switch (t_rp_clk) {
2005 case 0:
2006 case 1:
2007 case 2:
2008 case 3:
2009 sdtr2 |= SDRAM_SDTR2_RP_3_CLK;
2010 break;
2011 case 4:
2012 sdtr2 |= SDRAM_SDTR2_RP_4_CLK;
2013 break;
2014 case 5:
2015 sdtr2 |= SDRAM_SDTR2_RP_5_CLK;
2016 break;
2017 case 6:
2018 sdtr2 |= SDRAM_SDTR2_RP_6_CLK;
2019 break;
2020 default:
2021 sdtr2 |= SDRAM_SDTR2_RP_7_CLK;
2022 break;
2023 }
2024
2025 mtsdram(SDRAM_SDTR2, sdtr2);
2026
2027 /*------------------------------------------------------------------
2028 * Set the SDRAM Timing Reg 3, SDRAM_TR3
2029 *-----------------------------------------------------------------*/
2030 mfsdram(SDRAM_SDTR3, sdtr3);
2031 sdtr3 &= ~(SDRAM_SDTR3_RAS_MASK | SDRAM_SDTR3_RC_MASK |
2032 SDRAM_SDTR3_XCS_MASK | SDRAM_SDTR3_RFC_MASK);
2033
2034 /*
2035 * convert t_ras from nanoseconds to ddr clocks
2036 * round up if necessary
2037 */
2038 t_ras_clk = MULDIV64(sdram_freq, t_ras_ns, ONE_BILLION);
2039 ddr_check = MULDIV64(ONE_BILLION, t_ras_clk, t_ras_ns);
2040 if (sdram_freq != ddr_check)
2041 t_ras_clk++;
2042
2043 sdtr3 |= SDRAM_SDTR3_RAS_ENCODE(t_ras_clk);
2044
2045 /*
2046 * convert t_rc from nanoseconds to ddr clocks
2047 * round up if necessary
2048 */
2049 t_rc_clk = MULDIV64(sdram_freq, t_rc_ns, ONE_BILLION);
2050 ddr_check = MULDIV64(ONE_BILLION, t_rc_clk, t_rc_ns);
2051 if (sdram_freq != ddr_check)
2052 t_rc_clk++;
2053
2054 sdtr3 |= SDRAM_SDTR3_RC_ENCODE(t_rc_clk);
2055
2056 /* default xcs value */
2057 sdtr3 |= SDRAM_SDTR3_XCS;
2058
2059 /*
2060 * convert t_rfc from nanoseconds to ddr clocks
2061 * round up if necessary
2062 */
2063 t_rfc_clk = MULDIV64(sdram_freq, t_rfc_ns, ONE_BILLION);
2064 ddr_check = MULDIV64(ONE_BILLION, t_rfc_clk, t_rfc_ns);
2065 if (sdram_freq != ddr_check)
2066 t_rfc_clk++;
2067
2068 sdtr3 |= SDRAM_SDTR3_RFC_ENCODE(t_rfc_clk);
2069
2070 mtsdram(SDRAM_SDTR3, sdtr3);
2071}
2072
2073/*-----------------------------------------------------------------------------+
2074 * program_bxcf.
2075 *-----------------------------------------------------------------------------*/
2076static void program_bxcf(unsigned long *dimm_populated,
2077 unsigned char *iic0_dimm_addr,
2078 unsigned long num_dimm_banks)
2079{
2080 unsigned long dimm_num;
2081 unsigned long num_col_addr;
2082 unsigned long num_ranks;
2083 unsigned long num_banks;
2084 unsigned long mode;
2085 unsigned long ind_rank;
2086 unsigned long ind;
2087 unsigned long ind_bank;
2088 unsigned long bank_0_populated;
2089
2090 /*------------------------------------------------------------------
2091 * Set the BxCF regs. First, wipe out the bank config registers.
2092 *-----------------------------------------------------------------*/
Stefan Roeseedd73f22007-10-21 08:12:41 +02002093 mtsdram(SDRAM_MB0CF, 0x00000000);
2094 mtsdram(SDRAM_MB1CF, 0x00000000);
2095 mtsdram(SDRAM_MB2CF, 0x00000000);
2096 mtsdram(SDRAM_MB3CF, 0x00000000);
Stefan Roese43f32472007-02-20 10:43:34 +01002097
2098 mode = SDRAM_BXCF_M_BE_ENABLE;
2099
2100 bank_0_populated = 0;
2101
2102 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
2103 if (dimm_populated[dimm_num] != SDRAM_NONE) {
2104 num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
2105 num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
2106 if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
2107 num_ranks = (num_ranks & 0x0F) +1;
2108 else
2109 num_ranks = num_ranks & 0x0F;
2110
2111 num_banks = spd_read(iic0_dimm_addr[dimm_num], 17);
2112
2113 for (ind_bank = 0; ind_bank < 2; ind_bank++) {
2114 if (num_banks == 4)
2115 ind = 0;
2116 else
Stefan Roese964754e2008-04-30 10:49:43 +02002117 ind = 5 << 8;
Stefan Roese43f32472007-02-20 10:43:34 +01002118 switch (num_col_addr) {
2119 case 0x08:
2120 mode |= (SDRAM_BXCF_M_AM_0 + ind);
2121 break;
2122 case 0x09:
2123 mode |= (SDRAM_BXCF_M_AM_1 + ind);
2124 break;
2125 case 0x0A:
2126 mode |= (SDRAM_BXCF_M_AM_2 + ind);
2127 break;
2128 case 0x0B:
2129 mode |= (SDRAM_BXCF_M_AM_3 + ind);
2130 break;
2131 case 0x0C:
2132 mode |= (SDRAM_BXCF_M_AM_4 + ind);
2133 break;
2134 default:
2135 printf("DDR-SDRAM: DIMM %d BxCF configuration.\n",
2136 (unsigned int)dimm_num);
2137 printf("ERROR: Unsupported value for number of "
2138 "column addresses: %d.\n", (unsigned int)num_col_addr);
2139 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002140 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002141 }
2142 }
2143
2144 if ((dimm_populated[dimm_num] != SDRAM_NONE)&& (dimm_num ==1))
2145 bank_0_populated = 1;
2146
2147 for (ind_rank = 0; ind_rank < num_ranks; ind_rank++) {
Stefan Roeseedd73f22007-10-21 08:12:41 +02002148 mtsdram(SDRAM_MB0CF +
2149 ((dimm_num + bank_0_populated + ind_rank) << 2),
2150 mode);
Stefan Roese43f32472007-02-20 10:43:34 +01002151 }
2152 }
2153 }
2154}
2155
2156/*------------------------------------------------------------------
2157 * program memory queue.
2158 *-----------------------------------------------------------------*/
2159static void program_memory_queue(unsigned long *dimm_populated,
2160 unsigned char *iic0_dimm_addr,
2161 unsigned long num_dimm_banks)
2162{
2163 unsigned long dimm_num;
Stefan Roese0203a972008-07-09 17:33:57 +02002164 phys_size_t rank_base_addr;
Stefan Roese43f32472007-02-20 10:43:34 +01002165 unsigned long rank_reg;
Stefan Roese0203a972008-07-09 17:33:57 +02002166 phys_size_t rank_size_bytes;
Stefan Roese43f32472007-02-20 10:43:34 +01002167 unsigned long rank_size_id;
2168 unsigned long num_ranks;
2169 unsigned long baseadd_size;
2170 unsigned long i;
2171 unsigned long bank_0_populated = 0;
Stefan Roese0203a972008-07-09 17:33:57 +02002172 phys_size_t total_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002173
2174 /*------------------------------------------------------------------
2175 * Reset the rank_base_address.
2176 *-----------------------------------------------------------------*/
2177 rank_reg = SDRAM_R0BAS;
2178
2179 rank_base_addr = 0x00000000;
2180
2181 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
2182 if (dimm_populated[dimm_num] != SDRAM_NONE) {
2183 num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
2184 if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
2185 num_ranks = (num_ranks & 0x0F) + 1;
2186 else
2187 num_ranks = num_ranks & 0x0F;
2188
2189 rank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
2190
2191 /*------------------------------------------------------------------
2192 * Set the sizes
2193 *-----------------------------------------------------------------*/
2194 baseadd_size = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002195 switch (rank_size_id) {
Stefan Roesebdd13d12008-03-11 15:05:26 +01002196 case 0x01:
2197 baseadd_size |= SDRAM_RXBAS_SDSZ_1024;
2198 total_size = 1024;
2199 break;
Stefan Roese43f32472007-02-20 10:43:34 +01002200 case 0x02:
Stefan Roesebdd13d12008-03-11 15:05:26 +01002201 baseadd_size |= SDRAM_RXBAS_SDSZ_2048;
2202 total_size = 2048;
Stefan Roese43f32472007-02-20 10:43:34 +01002203 break;
2204 case 0x04:
Stefan Roesebdd13d12008-03-11 15:05:26 +01002205 baseadd_size |= SDRAM_RXBAS_SDSZ_4096;
2206 total_size = 4096;
Stefan Roese43f32472007-02-20 10:43:34 +01002207 break;
2208 case 0x08:
2209 baseadd_size |= SDRAM_RXBAS_SDSZ_32;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002210 total_size = 32;
Stefan Roese43f32472007-02-20 10:43:34 +01002211 break;
2212 case 0x10:
2213 baseadd_size |= SDRAM_RXBAS_SDSZ_64;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002214 total_size = 64;
Stefan Roese43f32472007-02-20 10:43:34 +01002215 break;
2216 case 0x20:
2217 baseadd_size |= SDRAM_RXBAS_SDSZ_128;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002218 total_size = 128;
Stefan Roese43f32472007-02-20 10:43:34 +01002219 break;
2220 case 0x40:
2221 baseadd_size |= SDRAM_RXBAS_SDSZ_256;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002222 total_size = 256;
Stefan Roese43f32472007-02-20 10:43:34 +01002223 break;
2224 case 0x80:
2225 baseadd_size |= SDRAM_RXBAS_SDSZ_512;
Stefan Roesebdd13d12008-03-11 15:05:26 +01002226 total_size = 512;
Stefan Roese43f32472007-02-20 10:43:34 +01002227 break;
2228 default:
2229 printf("DDR-SDRAM: DIMM %d memory queue configuration.\n",
2230 (unsigned int)dimm_num);
2231 printf("ERROR: Unsupported value for the banksize: %d.\n",
2232 (unsigned int)rank_size_id);
2233 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002234 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002235 }
Stefan Roesebdd13d12008-03-11 15:05:26 +01002236 rank_size_bytes = total_size << 20;
Stefan Roese43f32472007-02-20 10:43:34 +01002237
2238 if ((dimm_populated[dimm_num] != SDRAM_NONE) && (dimm_num == 1))
2239 bank_0_populated = 1;
2240
2241 for (i = 0; i < num_ranks; i++) {
2242 mtdcr_any(rank_reg+i+dimm_num+bank_0_populated,
Stefan Roeseb39ef632007-03-08 10:06:09 +01002243 (SDRAM_RXBAS_SDBA_ENCODE(rank_base_addr) |
2244 baseadd_size));
Stefan Roese43f32472007-02-20 10:43:34 +01002245 rank_base_addr += rank_size_bytes;
2246 }
2247 }
2248 }
Stefan Roesebdd13d12008-03-11 15:05:26 +01002249
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002250#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
2251 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
2252 defined(CONFIG_460SX)
Stefan Roesebdd13d12008-03-11 15:05:26 +01002253 /*
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002254 * Enable high bandwidth access
Stefan Roesebdd13d12008-03-11 15:05:26 +01002255 * This is currently not used, but with this setup
2256 * it is possible to use it later on in e.g. the Linux
2257 * EMAC driver for performance gain.
2258 */
2259 mtdcr(SDRAM_PLBADDULL, 0x00000000); /* MQ0_BAUL */
2260 mtdcr(SDRAM_PLBADDUHB, 0x00000008); /* MQ0_BAUH */
Prodyut Hazarika038f0d82008-08-20 09:38:51 -07002261
2262 /*
2263 * Set optimal value for Memory Queue HB/LL Configuration registers
2264 */
Yuri Tikhonovbbfab702008-10-17 12:54:18 +02002265 mtdcr(SDRAM_CONF1HB, (mfdcr(SDRAM_CONF1HB) & ~SDRAM_CONF1HB_MASK) |
2266 SDRAM_CONF1HB_AAFR | SDRAM_CONF1HB_RPEN | SDRAM_CONF1HB_RFTE |
2267 SDRAM_CONF1HB_RPLM | SDRAM_CONF1HB_WRCL);
2268 mtdcr(SDRAM_CONF1LL, (mfdcr(SDRAM_CONF1LL) & ~SDRAM_CONF1LL_MASK) |
2269 SDRAM_CONF1LL_AAFR | SDRAM_CONF1LL_RPEN | SDRAM_CONF1LL_RFTE |
2270 SDRAM_CONF1LL_RPLM);
Stefan Roese1abbbd02008-08-21 11:05:03 +02002271 mtdcr(SDRAM_CONFPATHB, mfdcr(SDRAM_CONFPATHB) | SDRAM_CONFPATHB_TPEN);
Stefan Roesebdd13d12008-03-11 15:05:26 +01002272#endif
Stefan Roese43f32472007-02-20 10:43:34 +01002273}
2274
2275/*-----------------------------------------------------------------------------+
2276 * is_ecc_enabled.
2277 *-----------------------------------------------------------------------------*/
2278static unsigned long is_ecc_enabled(void)
2279{
2280 unsigned long dimm_num;
2281 unsigned long ecc;
2282 unsigned long val;
2283
2284 ecc = 0;
2285 /* loop through all the DIMM slots on the board */
2286 for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
2287 mfsdram(SDRAM_MCOPT1, val);
2288 ecc = max(ecc, SDRAM_MCOPT1_MCHK_CHK_DECODE(val));
2289 }
2290
Stefan Roeseb39ef632007-03-08 10:06:09 +01002291 return ecc;
Stefan Roese43f32472007-02-20 10:43:34 +01002292}
2293
Stefan Roeseb39ef632007-03-08 10:06:09 +01002294#ifdef CONFIG_DDR_ECC
Stefan Roese43f32472007-02-20 10:43:34 +01002295/*-----------------------------------------------------------------------------+
2296 * program_ecc.
2297 *-----------------------------------------------------------------------------*/
2298static void program_ecc(unsigned long *dimm_populated,
2299 unsigned char *iic0_dimm_addr,
Stefan Roesebad41112007-03-01 21:11:36 +01002300 unsigned long num_dimm_banks,
2301 unsigned long tlb_word2_i_value)
Stefan Roese43f32472007-02-20 10:43:34 +01002302{
2303 unsigned long mcopt1;
2304 unsigned long mcopt2;
2305 unsigned long mcstat;
2306 unsigned long dimm_num;
2307 unsigned long ecc;
2308
2309 ecc = 0;
2310 /* loop through all the DIMM slots on the board */
2311 for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
2312 /* If a dimm is installed in a particular slot ... */
2313 if (dimm_populated[dimm_num] != SDRAM_NONE)
2314 ecc = max(ecc, spd_read(iic0_dimm_addr[dimm_num], 11));
2315 }
2316 if (ecc == 0)
2317 return;
Stefan Roese0203a972008-07-09 17:33:57 +02002318
2319 if (sdram_memsize() > CONFIG_MAX_MEM_MAPPED) {
2320 printf("\nWarning: Can't enable ECC on systems with more than 2GB of SDRAM!\n");
2321 return;
2322 }
Stefan Roese43f32472007-02-20 10:43:34 +01002323
2324 mfsdram(SDRAM_MCOPT1, mcopt1);
2325 mfsdram(SDRAM_MCOPT2, mcopt2);
2326
2327 if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
2328 /* DDR controller must be enabled and not in self-refresh. */
2329 mfsdram(SDRAM_MCSTAT, mcstat);
2330 if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
2331 && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
2332 && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
2333 == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
2334
Stefan Roesebad41112007-03-01 21:11:36 +01002335 program_ecc_addr(0, sdram_memsize(), tlb_word2_i_value);
Stefan Roese43f32472007-02-20 10:43:34 +01002336 }
2337 }
2338
2339 return;
2340}
2341
Stefan Roeseb39ef632007-03-08 10:06:09 +01002342static void wait_ddr_idle(void)
2343{
2344 u32 val;
2345
2346 do {
2347 mfsdram(SDRAM_MCSTAT, val);
2348 } while ((val & SDRAM_MCSTAT_IDLE_MASK) == SDRAM_MCSTAT_IDLE_NOT);
2349}
2350
Stefan Roese43f32472007-02-20 10:43:34 +01002351/*-----------------------------------------------------------------------------+
2352 * program_ecc_addr.
2353 *-----------------------------------------------------------------------------*/
2354static void program_ecc_addr(unsigned long start_address,
Stefan Roesebad41112007-03-01 21:11:36 +01002355 unsigned long num_bytes,
2356 unsigned long tlb_word2_i_value)
Stefan Roese43f32472007-02-20 10:43:34 +01002357{
2358 unsigned long current_address;
2359 unsigned long end_address;
2360 unsigned long address_increment;
2361 unsigned long mcopt1;
Stefan Roesef88e3602007-03-31 08:46:08 +02002362 char str[] = "ECC generation -";
2363 char slash[] = "\\|/-\\|/-";
2364 int loop = 0;
2365 int loopi = 0;
Stefan Roese43f32472007-02-20 10:43:34 +01002366
2367 current_address = start_address;
2368 mfsdram(SDRAM_MCOPT1, mcopt1);
2369 if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
2370 mtsdram(SDRAM_MCOPT1,
2371 (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_GEN);
2372 sync();
2373 eieio();
2374 wait_ddr_idle();
2375
Stefan Roesebad41112007-03-01 21:11:36 +01002376 puts(str);
2377 if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
2378 /* ECC bit set method for non-cached memory */
2379 if ((mcopt1 & SDRAM_MCOPT1_DMWD_MASK) == SDRAM_MCOPT1_DMWD_32)
2380 address_increment = 4;
2381 else
2382 address_increment = 8;
2383 end_address = current_address + num_bytes;
Stefan Roese43f32472007-02-20 10:43:34 +01002384
Stefan Roesebad41112007-03-01 21:11:36 +01002385 while (current_address < end_address) {
2386 *((unsigned long *)current_address) = 0x00000000;
2387 current_address += address_increment;
Stefan Roesef88e3602007-03-31 08:46:08 +02002388
2389 if ((loop++ % (2 << 20)) == 0) {
2390 putc('\b');
2391 putc(slash[loopi++ % 8]);
2392 }
Stefan Roesebad41112007-03-01 21:11:36 +01002393 }
Stefan Roesef88e3602007-03-31 08:46:08 +02002394
Stefan Roesebad41112007-03-01 21:11:36 +01002395 } else {
2396 /* ECC bit set method for cached memory */
2397 dcbz_area(start_address, num_bytes);
Stefan Roese286b81b2008-04-29 13:57:07 +02002398 /* Write modified dcache lines back to memory */
2399 clean_dcache_range(start_address, start_address + num_bytes);
Stefan Roese43f32472007-02-20 10:43:34 +01002400 }
Stefan Roesef88e3602007-03-31 08:46:08 +02002401
2402 blank_string(strlen(str));
Stefan Roesebad41112007-03-01 21:11:36 +01002403
Stefan Roese43f32472007-02-20 10:43:34 +01002404 sync();
2405 eieio();
2406 wait_ddr_idle();
2407
Stefan Roesebad41112007-03-01 21:11:36 +01002408 /* clear ECC error repoting registers */
2409 mtsdram(SDRAM_ECCCR, 0xffffffff);
2410 mtdcr(0x4c, 0xffffffff);
2411
Stefan Roese43f32472007-02-20 10:43:34 +01002412 mtsdram(SDRAM_MCOPT1,
Stefan Roesebad41112007-03-01 21:11:36 +01002413 (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_CHK_REP);
Stefan Roese43f32472007-02-20 10:43:34 +01002414 sync();
2415 eieio();
2416 wait_ddr_idle();
Stefan Roese43f32472007-02-20 10:43:34 +01002417 }
2418}
Stefan Roeseb39ef632007-03-08 10:06:09 +01002419#endif
Stefan Roese43f32472007-02-20 10:43:34 +01002420
Adam Graham97a55812008-09-03 12:26:59 -07002421#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Stefan Roese43f32472007-02-20 10:43:34 +01002422/*-----------------------------------------------------------------------------+
2423 * program_DQS_calibration.
2424 *-----------------------------------------------------------------------------*/
2425static void program_DQS_calibration(unsigned long *dimm_populated,
2426 unsigned char *iic0_dimm_addr,
2427 unsigned long num_dimm_banks)
2428{
2429 unsigned long val;
2430
2431#ifdef HARD_CODED_DQS /* calibration test with hardvalues */
2432 mtsdram(SDRAM_RQDC, 0x80000037);
2433 mtsdram(SDRAM_RDCC, 0x40000000);
2434 mtsdram(SDRAM_RFDC, 0x000001DF);
2435
2436 test();
2437#else
2438 /*------------------------------------------------------------------
2439 * Program RDCC register
2440 * Read sample cycle auto-update enable
2441 *-----------------------------------------------------------------*/
2442
Stefan Roese43f32472007-02-20 10:43:34 +01002443 mfsdram(SDRAM_RDCC, val);
2444 mtsdram(SDRAM_RDCC,
2445 (val & ~(SDRAM_RDCC_RDSS_MASK | SDRAM_RDCC_RSAE_MASK))
Stefan Roesee3060b02008-01-05 09:12:41 +01002446 | SDRAM_RDCC_RSAE_ENABLE);
Stefan Roese43f32472007-02-20 10:43:34 +01002447
2448 /*------------------------------------------------------------------
2449 * Program RQDC register
2450 * Internal DQS delay mechanism enable
2451 *-----------------------------------------------------------------*/
2452 mtsdram(SDRAM_RQDC, (SDRAM_RQDC_RQDE_ENABLE|SDRAM_RQDC_RQFD_ENCODE(0x38)));
2453
2454 /*------------------------------------------------------------------
2455 * Program RFDC register
2456 * Set Feedback Fractional Oversample
2457 * Auto-detect read sample cycle enable
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07002458 * Set RFOS to 1/4 of memclk cycle (0x3f)
Stefan Roese43f32472007-02-20 10:43:34 +01002459 *-----------------------------------------------------------------*/
2460 mfsdram(SDRAM_RFDC, val);
2461 mtsdram(SDRAM_RFDC,
2462 (val & ~(SDRAM_RFDC_ARSE_MASK | SDRAM_RFDC_RFOS_MASK |
2463 SDRAM_RFDC_RFFD_MASK))
Prodyut Hazarika52b6bec2008-08-27 16:39:00 -07002464 | (SDRAM_RFDC_ARSE_ENABLE | SDRAM_RFDC_RFOS_ENCODE(0x3f) |
Stefan Roese43f32472007-02-20 10:43:34 +01002465 SDRAM_RFDC_RFFD_ENCODE(0)));
2466
2467 DQS_calibration_process();
2468#endif
2469}
2470
Stefan Roesef88e3602007-03-31 08:46:08 +02002471static int short_mem_test(void)
Stefan Roese43f32472007-02-20 10:43:34 +01002472{
2473 u32 *membase;
2474 u32 bxcr_num;
2475 u32 bxcf;
2476 int i;
2477 int j;
Stefan Roese0203a972008-07-09 17:33:57 +02002478 phys_size_t base_addr;
Stefan Roese43f32472007-02-20 10:43:34 +01002479 u32 test[NUMMEMTESTS][NUMMEMWORDS] = {
2480 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
2481 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
2482 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
2483 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
2484 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
2485 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
2486 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
2487 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
2488 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
2489 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
2490 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
2491 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
2492 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
2493 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
2494 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
2495 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
Stefan Roesef88e3602007-03-31 08:46:08 +02002496 int l;
Stefan Roese43f32472007-02-20 10:43:34 +01002497
2498 for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
2499 mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf);
2500
2501 /* Banks enabled */
2502 if ((bxcf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
Stefan Roese43f32472007-02-20 10:43:34 +01002503 /* Bank is enabled */
Stefan Roese43f32472007-02-20 10:43:34 +01002504
Stefan Roese0203a972008-07-09 17:33:57 +02002505 /*
2506 * Only run test on accessable memory (below 2GB)
2507 */
2508 base_addr = SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num));
2509 if (base_addr >= CONFIG_MAX_MEM_MAPPED)
2510 continue;
2511
Stefan Roese43f32472007-02-20 10:43:34 +01002512 /*------------------------------------------------------------------
2513 * Run the short memory test.
2514 *-----------------------------------------------------------------*/
Stefan Roese0203a972008-07-09 17:33:57 +02002515 membase = (u32 *)(u32)base_addr;
Stefan Roesef88e3602007-03-31 08:46:08 +02002516
Stefan Roese43f32472007-02-20 10:43:34 +01002517 for (i = 0; i < NUMMEMTESTS; i++) {
2518 for (j = 0; j < NUMMEMWORDS; j++) {
2519 membase[j] = test[i][j];
2520 ppcDcbf((u32)&(membase[j]));
2521 }
2522 sync();
Stefan Roesef88e3602007-03-31 08:46:08 +02002523 for (l=0; l<NUMLOOPS; l++) {
2524 for (j = 0; j < NUMMEMWORDS; j++) {
2525 if (membase[j] != test[i][j]) {
2526 ppcDcbf((u32)&(membase[j]));
2527 return 0;
2528 }
Stefan Roese43f32472007-02-20 10:43:34 +01002529 ppcDcbf((u32)&(membase[j]));
Stefan Roese43f32472007-02-20 10:43:34 +01002530 }
Stefan Roesef88e3602007-03-31 08:46:08 +02002531 sync();
Stefan Roese43f32472007-02-20 10:43:34 +01002532 }
Stefan Roese43f32472007-02-20 10:43:34 +01002533 }
Stefan Roese43f32472007-02-20 10:43:34 +01002534 } /* if bank enabled */
2535 } /* for bxcf_num */
2536
Stefan Roesef88e3602007-03-31 08:46:08 +02002537 return 1;
Stefan Roese43f32472007-02-20 10:43:34 +01002538}
2539
2540#ifndef HARD_CODED_DQS
2541/*-----------------------------------------------------------------------------+
2542 * DQS_calibration_process.
2543 *-----------------------------------------------------------------------------*/
2544static void DQS_calibration_process(void)
2545{
Stefan Roese43f32472007-02-20 10:43:34 +01002546 unsigned long rfdc_reg;
2547 unsigned long rffd;
Stefan Roese43f32472007-02-20 10:43:34 +01002548 unsigned long val;
Stefan Roese43f32472007-02-20 10:43:34 +01002549 long rffd_average;
2550 long max_start;
2551 long min_end;
2552 unsigned long begin_rqfd[MAXRANKS];
2553 unsigned long begin_rffd[MAXRANKS];
2554 unsigned long end_rqfd[MAXRANKS];
2555 unsigned long end_rffd[MAXRANKS];
2556 char window_found;
2557 unsigned long dlycal;
2558 unsigned long dly_val;
2559 unsigned long max_pass_length;
2560 unsigned long current_pass_length;
2561 unsigned long current_fail_length;
2562 unsigned long current_start;
2563 long max_end;
2564 unsigned char fail_found;
2565 unsigned char pass_found;
Stefan Roesee3060b02008-01-05 09:12:41 +01002566#if !defined(CONFIG_DDR_RQDC_FIXED)
2567 u32 rqdc_reg;
2568 u32 rqfd;
Stefan Roesef88e3602007-03-31 08:46:08 +02002569 u32 rqfd_start;
Stefan Roesee3060b02008-01-05 09:12:41 +01002570 u32 rqfd_average;
2571 int loopi = 0;
Stefan Roesef88e3602007-03-31 08:46:08 +02002572 char str[] = "Auto calibration -";
2573 char slash[] = "\\|/-\\|/-";
Stefan Roese43f32472007-02-20 10:43:34 +01002574
2575 /*------------------------------------------------------------------
2576 * Test to determine the best read clock delay tuning bits.
2577 *
2578 * Before the DDR controller can be used, the read clock delay needs to be
2579 * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
2580 * This value cannot be hardcoded into the program because it changes
2581 * depending on the board's setup and environment.
2582 * To do this, all delay values are tested to see if they
2583 * work or not. By doing this, you get groups of fails with groups of
2584 * passing values. The idea is to find the start and end of a passing
2585 * window and take the center of it to use as the read clock delay.
2586 *
2587 * A failure has to be seen first so that when we hit a pass, we know
2588 * that it is truely the start of the window. If we get passing values
2589 * to start off with, we don't know if we are at the start of the window.
2590 *
2591 * The code assumes that a failure will always be found.
2592 * If a failure is not found, there is no easy way to get the middle
2593 * of the passing window. I guess we can pretty much pick any value
2594 * but some values will be better than others. Since the lowest speed
2595 * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
2596 * from experimentation it is safe to say you will always have a failure.
2597 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002598
2599 /* first fix RQDC[RQFD] to an average of 80 degre phase shift to find RFDC[RFFD] */
2600 rqfd_start = 64; /* test-only: don't know if this is the _best_ start value */
2601
2602 puts(str);
2603
2604calibration_loop:
2605 mfsdram(SDRAM_RQDC, rqdc_reg);
2606 mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
2607 SDRAM_RQDC_RQFD_ENCODE(rqfd_start));
Stefan Roesee3060b02008-01-05 09:12:41 +01002608#else /* CONFIG_DDR_RQDC_FIXED */
2609 /*
2610 * On Katmai the complete auto-calibration somehow doesn't seem to
2611 * produce the best results, meaning optimal values for RQFD/RFFD.
2612 * This was discovered by GDA using a high bandwidth scope,
2613 * analyzing the DDR2 signals. GDA provided a fixed value for RQFD,
2614 * so now on Katmai "only" RFFD is auto-calibrated.
2615 */
2616 mtsdram(SDRAM_RQDC, CONFIG_DDR_RQDC_FIXED);
2617#endif /* CONFIG_DDR_RQDC_FIXED */
Stefan Roese43f32472007-02-20 10:43:34 +01002618
2619 max_start = 0;
2620 min_end = 0;
2621 begin_rqfd[0] = 0;
2622 begin_rffd[0] = 0;
2623 begin_rqfd[1] = 0;
2624 begin_rffd[1] = 0;
2625 end_rqfd[0] = 0;
2626 end_rffd[0] = 0;
2627 end_rqfd[1] = 0;
2628 end_rffd[1] = 0;
2629 window_found = FALSE;
2630
2631 max_pass_length = 0;
2632 max_start = 0;
2633 max_end = 0;
2634 current_pass_length = 0;
2635 current_fail_length = 0;
2636 current_start = 0;
2637 window_found = FALSE;
2638 fail_found = FALSE;
2639 pass_found = FALSE;
2640
Stefan Roese43f32472007-02-20 10:43:34 +01002641 /*
2642 * get the delay line calibration register value
2643 */
2644 mfsdram(SDRAM_DLCR, dlycal);
2645 dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
2646
2647 for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
2648 mfsdram(SDRAM_RFDC, rfdc_reg);
2649 rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
2650
2651 /*------------------------------------------------------------------
2652 * Set the timing reg for the test.
2653 *-----------------------------------------------------------------*/
2654 mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
2655
Stefan Roese43f32472007-02-20 10:43:34 +01002656 /*------------------------------------------------------------------
2657 * See if the rffd value passed.
2658 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002659 if (short_mem_test()) {
Stefan Roese43f32472007-02-20 10:43:34 +01002660 if (fail_found == TRUE) {
2661 pass_found = TRUE;
2662 if (current_pass_length == 0)
2663 current_start = rffd;
2664
2665 current_fail_length = 0;
2666 current_pass_length++;
2667
2668 if (current_pass_length > max_pass_length) {
2669 max_pass_length = current_pass_length;
2670 max_start = current_start;
2671 max_end = rffd;
2672 }
2673 }
2674 } else {
2675 current_pass_length = 0;
2676 current_fail_length++;
2677
2678 if (current_fail_length >= (dly_val >> 2)) {
2679 if (fail_found == FALSE) {
2680 fail_found = TRUE;
2681 } else if (pass_found == TRUE) {
2682 window_found = TRUE;
2683 break;
2684 }
2685 }
2686 }
2687 } /* for rffd */
2688
Stefan Roese43f32472007-02-20 10:43:34 +01002689 /*------------------------------------------------------------------
2690 * Set the average RFFD value
2691 *-----------------------------------------------------------------*/
2692 rffd_average = ((max_start + max_end) >> 1);
2693
2694 if (rffd_average < 0)
2695 rffd_average = 0;
2696
2697 if (rffd_average > SDRAM_RFDC_RFFD_MAX)
2698 rffd_average = SDRAM_RFDC_RFFD_MAX;
2699 /* now fix RFDC[RFFD] found and find RQDC[RQFD] */
2700 mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
2701
Stefan Roesee3060b02008-01-05 09:12:41 +01002702#if !defined(CONFIG_DDR_RQDC_FIXED)
Stefan Roese43f32472007-02-20 10:43:34 +01002703 max_pass_length = 0;
2704 max_start = 0;
2705 max_end = 0;
2706 current_pass_length = 0;
2707 current_fail_length = 0;
2708 current_start = 0;
2709 window_found = FALSE;
2710 fail_found = FALSE;
2711 pass_found = FALSE;
2712
2713 for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
2714 mfsdram(SDRAM_RQDC, rqdc_reg);
2715 rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
2716
2717 /*------------------------------------------------------------------
2718 * Set the timing reg for the test.
2719 *-----------------------------------------------------------------*/
2720 mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
2721
Stefan Roese43f32472007-02-20 10:43:34 +01002722 /*------------------------------------------------------------------
2723 * See if the rffd value passed.
2724 *-----------------------------------------------------------------*/
Stefan Roesef88e3602007-03-31 08:46:08 +02002725 if (short_mem_test()) {
Stefan Roese43f32472007-02-20 10:43:34 +01002726 if (fail_found == TRUE) {
2727 pass_found = TRUE;
2728 if (current_pass_length == 0)
2729 current_start = rqfd;
2730
2731 current_fail_length = 0;
2732 current_pass_length++;
2733
2734 if (current_pass_length > max_pass_length) {
2735 max_pass_length = current_pass_length;
2736 max_start = current_start;
2737 max_end = rqfd;
2738 }
2739 }
2740 } else {
2741 current_pass_length = 0;
2742 current_fail_length++;
2743
2744 if (fail_found == FALSE) {
2745 fail_found = TRUE;
2746 } else if (pass_found == TRUE) {
2747 window_found = TRUE;
2748 break;
2749 }
2750 }
2751 }
2752
Stefan Roesef88e3602007-03-31 08:46:08 +02002753 rqfd_average = ((max_start + max_end) >> 1);
2754
Stefan Roese43f32472007-02-20 10:43:34 +01002755 /*------------------------------------------------------------------
2756 * Make sure we found the valid read passing window. Halt if not
2757 *-----------------------------------------------------------------*/
2758 if (window_found == FALSE) {
Stefan Roesef88e3602007-03-31 08:46:08 +02002759 if (rqfd_start < SDRAM_RQDC_RQFD_MAX) {
2760 putc('\b');
2761 putc(slash[loopi++ % 8]);
2762
2763 /* try again from with a different RQFD start value */
2764 rqfd_start++;
2765 goto calibration_loop;
2766 }
2767
2768 printf("\nERROR: Cannot determine a common read delay for the "
Stefan Roese43f32472007-02-20 10:43:34 +01002769 "DIMM(s) installed.\n");
2770 debug("%s[%d] ERROR : \n", __FUNCTION__,__LINE__);
Grant Erickson9416cd92008-07-09 16:46:35 -07002771 ppc4xx_ibm_ddr2_register_dump();
Heiko Schocher68310b02007-06-25 19:11:37 +02002772 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002773 }
2774
Stefan Roese43f32472007-02-20 10:43:34 +01002775 if (rqfd_average < 0)
2776 rqfd_average = 0;
2777
2778 if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
2779 rqfd_average = SDRAM_RQDC_RQFD_MAX;
2780
Stefan Roese43f32472007-02-20 10:43:34 +01002781 mtsdram(SDRAM_RQDC,
2782 (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
2783 SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
2784
Stefan Roesee3060b02008-01-05 09:12:41 +01002785 blank_string(strlen(str));
2786#endif /* CONFIG_DDR_RQDC_FIXED */
2787
2788 /*
2789 * Now complete RDSS configuration as mentioned on page 7 of the AMCC
2790 * PowerPC440SP/SPe DDR2 application note:
2791 * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
2792 */
2793 mfsdram(SDRAM_RTSR, val);
2794 if ((val & SDRAM_RTSR_TRK1SM_MASK) == SDRAM_RTSR_TRK1SM_ATPLS1) {
2795 mfsdram(SDRAM_RDCC, val);
2796 if ((val & SDRAM_RDCC_RDSS_MASK) != SDRAM_RDCC_RDSS_T4) {
2797 val += 0x40000000;
2798 mtsdram(SDRAM_RDCC, val);
2799 }
2800 }
2801
Stefan Roese43f32472007-02-20 10:43:34 +01002802 mfsdram(SDRAM_DLCR, val);
2803 debug("%s[%d] DLCR: 0x%08X\n", __FUNCTION__, __LINE__, val);
2804 mfsdram(SDRAM_RQDC, val);
2805 debug("%s[%d] RQDC: 0x%08X\n", __FUNCTION__, __LINE__, val);
2806 mfsdram(SDRAM_RFDC, val);
2807 debug("%s[%d] RFDC: 0x%08X\n", __FUNCTION__, __LINE__, val);
Stefan Roesee3060b02008-01-05 09:12:41 +01002808 mfsdram(SDRAM_RDCC, val);
2809 debug("%s[%d] RDCC: 0x%08X\n", __FUNCTION__, __LINE__, val);
Stefan Roese43f32472007-02-20 10:43:34 +01002810}
2811#else /* calibration test with hardvalues */
2812/*-----------------------------------------------------------------------------+
2813 * DQS_calibration_process.
2814 *-----------------------------------------------------------------------------*/
2815static void test(void)
2816{
2817 unsigned long dimm_num;
2818 unsigned long ecc_temp;
2819 unsigned long i, j;
2820 unsigned long *membase;
2821 unsigned long bxcf[MAXRANKS];
2822 unsigned long val;
2823 char window_found;
2824 char begin_found[MAXDIMMS];
2825 char end_found[MAXDIMMS];
2826 char search_end[MAXDIMMS];
2827 unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
2828 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
2829 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
2830 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
2831 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
2832 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
2833 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
2834 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
2835 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
2836 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
2837 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
2838 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
2839 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
2840 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
2841 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
2842 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
2843 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
2844
2845 /*------------------------------------------------------------------
2846 * Test to determine the best read clock delay tuning bits.
2847 *
2848 * Before the DDR controller can be used, the read clock delay needs to be
2849 * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
2850 * This value cannot be hardcoded into the program because it changes
2851 * depending on the board's setup and environment.
2852 * To do this, all delay values are tested to see if they
2853 * work or not. By doing this, you get groups of fails with groups of
2854 * passing values. The idea is to find the start and end of a passing
2855 * window and take the center of it to use as the read clock delay.
2856 *
2857 * A failure has to be seen first so that when we hit a pass, we know
2858 * that it is truely the start of the window. If we get passing values
2859 * to start off with, we don't know if we are at the start of the window.
2860 *
2861 * The code assumes that a failure will always be found.
2862 * If a failure is not found, there is no easy way to get the middle
2863 * of the passing window. I guess we can pretty much pick any value
2864 * but some values will be better than others. Since the lowest speed
2865 * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
2866 * from experimentation it is safe to say you will always have a failure.
2867 *-----------------------------------------------------------------*/
2868 mfsdram(SDRAM_MCOPT1, ecc_temp);
2869 ecc_temp &= SDRAM_MCOPT1_MCHK_MASK;
2870 mfsdram(SDRAM_MCOPT1, val);
2871 mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) |
2872 SDRAM_MCOPT1_MCHK_NON);
2873
2874 window_found = FALSE;
2875 begin_found[0] = FALSE;
2876 end_found[0] = FALSE;
2877 search_end[0] = FALSE;
2878 begin_found[1] = FALSE;
2879 end_found[1] = FALSE;
2880 search_end[1] = FALSE;
2881
2882 for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
2883 mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf[bxcr_num]);
2884
2885 /* Banks enabled */
2886 if ((bxcf[dimm_num] & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
2887
2888 /* Bank is enabled */
2889 membase =
2890 (unsigned long*)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+dimm_num)));
2891
2892 /*------------------------------------------------------------------
2893 * Run the short memory test.
2894 *-----------------------------------------------------------------*/
2895 for (i = 0; i < NUMMEMTESTS; i++) {
2896 for (j = 0; j < NUMMEMWORDS; j++) {
2897 membase[j] = test[i][j];
2898 ppcDcbf((u32)&(membase[j]));
2899 }
2900 sync();
2901 for (j = 0; j < NUMMEMWORDS; j++) {
2902 if (membase[j] != test[i][j]) {
2903 ppcDcbf((u32)&(membase[j]));
2904 break;
2905 }
2906 ppcDcbf((u32)&(membase[j]));
2907 }
2908 sync();
2909 if (j < NUMMEMWORDS)
2910 break;
2911 }
2912
2913 /*------------------------------------------------------------------
2914 * See if the rffd value passed.
2915 *-----------------------------------------------------------------*/
2916 if (i < NUMMEMTESTS) {
2917 if ((end_found[dimm_num] == FALSE) &&
2918 (search_end[dimm_num] == TRUE)) {
2919 end_found[dimm_num] = TRUE;
2920 }
2921 if ((end_found[0] == TRUE) &&
2922 (end_found[1] == TRUE))
2923 break;
2924 } else {
2925 if (begin_found[dimm_num] == FALSE) {
2926 begin_found[dimm_num] = TRUE;
2927 search_end[dimm_num] = TRUE;
2928 }
2929 }
2930 } else {
2931 begin_found[dimm_num] = TRUE;
2932 end_found[dimm_num] = TRUE;
2933 }
2934 }
2935
2936 if ((begin_found[0] == TRUE) && (begin_found[1] == TRUE))
2937 window_found = TRUE;
2938
2939 /*------------------------------------------------------------------
2940 * Make sure we found the valid read passing window. Halt if not
2941 *-----------------------------------------------------------------*/
2942 if (window_found == FALSE) {
2943 printf("ERROR: Cannot determine a common read delay for the "
2944 "DIMM(s) installed.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02002945 spd_ddr_init_hang ();
Stefan Roese43f32472007-02-20 10:43:34 +01002946 }
2947
2948 /*------------------------------------------------------------------
2949 * Restore the ECC variable to what it originally was
2950 *-----------------------------------------------------------------*/
2951 mtsdram(SDRAM_MCOPT1,
2952 (ppcMfdcr_sdram(SDRAM_MCOPT1) & ~SDRAM_MCOPT1_MCHK_MASK)
2953 | ecc_temp);
2954}
Adam Graham97a55812008-09-03 12:26:59 -07002955#endif /* !HARD_CODED_DQS */
2956#endif /* !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */
Stefan Roese43f32472007-02-20 10:43:34 +01002957
Stefan Roese2001a332008-07-10 15:32:32 +02002958#else /* CONFIG_SPD_EEPROM */
2959
Grant Ericksonb6933412008-05-22 14:44:14 -07002960/*-----------------------------------------------------------------------------
2961 * Function: initdram
Adam Graham446eb8d2008-10-08 10:13:14 -07002962 * Description: Configures the PPC4xx IBM DDR1/DDR2 SDRAM memory controller.
2963 * The configuration is performed using static, compile-
Grant Ericksonb6933412008-05-22 14:44:14 -07002964 * time parameters.
Adam Graham446eb8d2008-10-08 10:13:14 -07002965 * Configures the PPC405EX(r) and PPC460EX/GT
Grant Ericksonb6933412008-05-22 14:44:14 -07002966 *---------------------------------------------------------------------------*/
Becky Brucebd99ae72008-06-09 16:03:40 -05002967phys_size_t initdram(int board_type)
Grant Ericksonb6933412008-05-22 14:44:14 -07002968{
Stefan Roesea226c852008-06-02 17:13:55 +02002969 /*
2970 * Only run this SDRAM init code once. For NAND booting
2971 * targets like Kilauea, we call initdram() early from the
2972 * 4k NAND booting image (CONFIG_NAND_SPL) from nand_boot().
2973 * Later on the NAND U-Boot image runs (CONFIG_NAND_U_BOOT)
2974 * which calls initdram() again. This time the controller
2975 * mustn't be reconfigured again since we're already running
2976 * from SDRAM.
2977 */
2978#if !defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL)
Grant Ericksonb6933412008-05-22 14:44:14 -07002979 unsigned long val;
2980
Adam Graham446eb8d2008-10-08 10:13:14 -07002981#if defined(CONFIG_440)
2982 mtdcr(SDRAM_R0BAS, CONFIG_SYS_SDRAM_R0BAS);
2983 mtdcr(SDRAM_R1BAS, CONFIG_SYS_SDRAM_R1BAS);
2984 mtdcr(SDRAM_R2BAS, CONFIG_SYS_SDRAM_R2BAS);
2985 mtdcr(SDRAM_R3BAS, CONFIG_SYS_SDRAM_R3BAS);
2986 mtdcr(SDRAM_PLBADDULL, CONFIG_SYS_SDRAM_PLBADDULL); /* MQ0_BAUL */
2987 mtdcr(SDRAM_PLBADDUHB, CONFIG_SYS_SDRAM_PLBADDUHB); /* MQ0_BAUH */
2988 mtdcr(SDRAM_CONF1LL, CONFIG_SYS_SDRAM_CONF1LL);
2989 mtdcr(SDRAM_CONF1HB, CONFIG_SYS_SDRAM_CONF1HB);
2990 mtdcr(SDRAM_CONFPATHB, CONFIG_SYS_SDRAM_CONFPATHB);
2991#endif
2992
Grant Ericksonb6933412008-05-22 14:44:14 -07002993 /* Set Memory Bank Configuration Registers */
2994
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02002995 mtsdram(SDRAM_MB0CF, CONFIG_SYS_SDRAM0_MB0CF);
2996 mtsdram(SDRAM_MB1CF, CONFIG_SYS_SDRAM0_MB1CF);
2997 mtsdram(SDRAM_MB2CF, CONFIG_SYS_SDRAM0_MB2CF);
2998 mtsdram(SDRAM_MB3CF, CONFIG_SYS_SDRAM0_MB3CF);
Grant Ericksonb6933412008-05-22 14:44:14 -07002999
3000 /* Set Memory Clock Timing Register */
3001
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003002 mtsdram(SDRAM_CLKTR, CONFIG_SYS_SDRAM0_CLKTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07003003
3004 /* Set Refresh Time Register */
3005
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003006 mtsdram(SDRAM_RTR, CONFIG_SYS_SDRAM0_RTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07003007
3008 /* Set SDRAM Timing Registers */
3009
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003010 mtsdram(SDRAM_SDTR1, CONFIG_SYS_SDRAM0_SDTR1);
3011 mtsdram(SDRAM_SDTR2, CONFIG_SYS_SDRAM0_SDTR2);
3012 mtsdram(SDRAM_SDTR3, CONFIG_SYS_SDRAM0_SDTR3);
Grant Ericksonb6933412008-05-22 14:44:14 -07003013
3014 /* Set Mode and Extended Mode Registers */
3015
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003016 mtsdram(SDRAM_MMODE, CONFIG_SYS_SDRAM0_MMODE);
3017 mtsdram(SDRAM_MEMODE, CONFIG_SYS_SDRAM0_MEMODE);
Grant Ericksonb6933412008-05-22 14:44:14 -07003018
3019 /* Set Memory Controller Options 1 Register */
3020
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003021 mtsdram(SDRAM_MCOPT1, CONFIG_SYS_SDRAM0_MCOPT1);
Grant Ericksonb6933412008-05-22 14:44:14 -07003022
3023 /* Set Manual Initialization Control Registers */
3024
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003025 mtsdram(SDRAM_INITPLR0, CONFIG_SYS_SDRAM0_INITPLR0);
3026 mtsdram(SDRAM_INITPLR1, CONFIG_SYS_SDRAM0_INITPLR1);
3027 mtsdram(SDRAM_INITPLR2, CONFIG_SYS_SDRAM0_INITPLR2);
3028 mtsdram(SDRAM_INITPLR3, CONFIG_SYS_SDRAM0_INITPLR3);
3029 mtsdram(SDRAM_INITPLR4, CONFIG_SYS_SDRAM0_INITPLR4);
3030 mtsdram(SDRAM_INITPLR5, CONFIG_SYS_SDRAM0_INITPLR5);
3031 mtsdram(SDRAM_INITPLR6, CONFIG_SYS_SDRAM0_INITPLR6);
3032 mtsdram(SDRAM_INITPLR7, CONFIG_SYS_SDRAM0_INITPLR7);
3033 mtsdram(SDRAM_INITPLR8, CONFIG_SYS_SDRAM0_INITPLR8);
3034 mtsdram(SDRAM_INITPLR9, CONFIG_SYS_SDRAM0_INITPLR9);
3035 mtsdram(SDRAM_INITPLR10, CONFIG_SYS_SDRAM0_INITPLR10);
3036 mtsdram(SDRAM_INITPLR11, CONFIG_SYS_SDRAM0_INITPLR11);
3037 mtsdram(SDRAM_INITPLR12, CONFIG_SYS_SDRAM0_INITPLR12);
3038 mtsdram(SDRAM_INITPLR13, CONFIG_SYS_SDRAM0_INITPLR13);
3039 mtsdram(SDRAM_INITPLR14, CONFIG_SYS_SDRAM0_INITPLR14);
3040 mtsdram(SDRAM_INITPLR15, CONFIG_SYS_SDRAM0_INITPLR15);
Grant Ericksonb6933412008-05-22 14:44:14 -07003041
3042 /* Set On-Die Termination Registers */
3043
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003044 mtsdram(SDRAM_CODT, CONFIG_SYS_SDRAM0_CODT);
3045 mtsdram(SDRAM_MODT0, CONFIG_SYS_SDRAM0_MODT0);
3046 mtsdram(SDRAM_MODT1, CONFIG_SYS_SDRAM0_MODT1);
Grant Ericksonb6933412008-05-22 14:44:14 -07003047
3048 /* Set Write Timing Register */
3049
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003050 mtsdram(SDRAM_WRDTR, CONFIG_SYS_SDRAM0_WRDTR);
Grant Ericksonb6933412008-05-22 14:44:14 -07003051
3052 /*
3053 * Start Initialization by SDRAM0_MCOPT2[SREN] = 0 and
3054 * SDRAM0_MCOPT2[IPTR] = 1
3055 */
3056
3057 mtsdram(SDRAM_MCOPT2, (SDRAM_MCOPT2_SREN_EXIT |
3058 SDRAM_MCOPT2_IPTR_EXECUTE));
3059
3060 /*
3061 * Poll SDRAM0_MCSTAT[MIC] for assertion to indicate the
3062 * completion of initialization.
3063 */
3064
3065 do {
3066 mfsdram(SDRAM_MCSTAT, val);
3067 } while ((val & SDRAM_MCSTAT_MIC_MASK) != SDRAM_MCSTAT_MIC_COMP);
3068
3069 /* Set Delay Control Registers */
3070
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003071 mtsdram(SDRAM_DLCR, CONFIG_SYS_SDRAM0_DLCR);
Adam Graham97a55812008-09-03 12:26:59 -07003072
3073#if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003074 mtsdram(SDRAM_RDCC, CONFIG_SYS_SDRAM0_RDCC);
3075 mtsdram(SDRAM_RQDC, CONFIG_SYS_SDRAM0_RQDC);
3076 mtsdram(SDRAM_RFDC, CONFIG_SYS_SDRAM0_RFDC);
Adam Graham97a55812008-09-03 12:26:59 -07003077#endif /* !CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
Grant Ericksonb6933412008-05-22 14:44:14 -07003078
3079 /*
3080 * Enable Controller by SDRAM0_MCOPT2[DCEN] = 1:
3081 */
3082
3083 mfsdram(SDRAM_MCOPT2, val);
3084 mtsdram(SDRAM_MCOPT2, val | SDRAM_MCOPT2_DCEN_ENABLE);
3085
Adam Graham446eb8d2008-10-08 10:13:14 -07003086#if defined(CONFIG_440)
3087 /*
3088 * Program TLB entries with caches enabled, for best performace
3089 * while auto-calibrating and ECC generation
3090 */
3091 program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), 0);
3092#endif
3093
Adam Graham97a55812008-09-03 12:26:59 -07003094#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
3095#if !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)
3096 /*------------------------------------------------------------------
3097 | DQS calibration.
3098 +-----------------------------------------------------------------*/
3099 DQS_autocalibration();
3100#endif /* !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) */
3101#endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
3102
Grant Ericksonb6933412008-05-22 14:44:14 -07003103#if defined(CONFIG_DDR_ECC)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003104 ecc_init(CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_MBYTES_SDRAM << 20);
Grant Ericksonb6933412008-05-22 14:44:14 -07003105#endif /* defined(CONFIG_DDR_ECC) */
Grant Erickson9416cd92008-07-09 16:46:35 -07003106
Adam Graham446eb8d2008-10-08 10:13:14 -07003107#if defined(CONFIG_440)
3108 /*
3109 * Now after initialization (auto-calibration and ECC generation)
3110 * remove the TLB entries with caches enabled and program again with
3111 * desired cache functionality
3112 */
3113 remove_tlb(0, (CONFIG_SYS_MBYTES_SDRAM << 20));
3114 program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), MY_TLB_WORD2_I_ENABLE);
3115#endif
3116
Grant Erickson9416cd92008-07-09 16:46:35 -07003117 ppc4xx_ibm_ddr2_register_dump();
Adam Graham97a55812008-09-03 12:26:59 -07003118
3119#if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
3120 /*
3121 * Clear potential errors resulting from auto-calibration.
3122 * If not done, then we could get an interrupt later on when
3123 * exceptions are enabled.
3124 */
3125 set_mcsr(get_mcsr());
3126#endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
3127
Stefan Roesea226c852008-06-02 17:13:55 +02003128#endif /* !defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL) */
Grant Ericksonb6933412008-05-22 14:44:14 -07003129
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02003130 return (CONFIG_SYS_MBYTES_SDRAM << 20);
Grant Ericksonb6933412008-05-22 14:44:14 -07003131}
Stefan Roese2001a332008-07-10 15:32:32 +02003132#endif /* CONFIG_SPD_EEPROM */
Grant Erickson9416cd92008-07-09 16:46:35 -07003133
Adam Graham97a55812008-09-03 12:26:59 -07003134#if !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)
3135#if defined(CONFIG_440)
3136u32 mfdcr_any(u32 dcr)
3137{
3138 u32 val;
3139
3140 switch (dcr) {
3141 case SDRAM_R0BAS + 0:
3142 val = mfdcr(SDRAM_R0BAS + 0);
3143 break;
3144 case SDRAM_R0BAS + 1:
3145 val = mfdcr(SDRAM_R0BAS + 1);
3146 break;
3147 case SDRAM_R0BAS + 2:
3148 val = mfdcr(SDRAM_R0BAS + 2);
3149 break;
3150 case SDRAM_R0BAS + 3:
3151 val = mfdcr(SDRAM_R0BAS + 3);
3152 break;
3153 default:
3154 printf("DCR %d not defined in case statement!!!\n", dcr);
3155 val = 0; /* just to satisfy the compiler */
3156 }
3157
3158 return val;
3159}
3160
3161void mtdcr_any(u32 dcr, u32 val)
3162{
3163 switch (dcr) {
3164 case SDRAM_R0BAS + 0:
3165 mtdcr(SDRAM_R0BAS + 0, val);
3166 break;
3167 case SDRAM_R0BAS + 1:
3168 mtdcr(SDRAM_R0BAS + 1, val);
3169 break;
3170 case SDRAM_R0BAS + 2:
3171 mtdcr(SDRAM_R0BAS + 2, val);
3172 break;
3173 case SDRAM_R0BAS + 3:
3174 mtdcr(SDRAM_R0BAS + 3, val);
3175 break;
3176 default:
3177 printf("DCR %d not defined in case statement!!!\n", dcr);
3178 }
3179}
3180#endif /* defined(CONFIG_440) */
3181
3182void blank_string(int size)
3183{
3184 int i;
3185
3186 for (i = 0; i < size; i++)
3187 putc('\b');
3188 for (i = 0; i < size; i++)
3189 putc(' ');
3190 for (i = 0; i < size; i++)
3191 putc('\b');
3192}
3193#endif /* !defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) */
3194
3195inline void ppc4xx_ibm_ddr2_register_dump(void)
Grant Erickson9416cd92008-07-09 16:46:35 -07003196{
Stefan Roese2001a332008-07-10 15:32:32 +02003197#if defined(DEBUG)
Grant Erickson9416cd92008-07-09 16:46:35 -07003198 printf("\nPPC4xx IBM DDR2 Register Dump:\n");
3199
3200#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3201 defined(CONFIG_460EX) || defined(CONFIG_460GT))
3202 PPC4xx_IBM_DDR2_DUMP_REGISTER(R0BAS);
3203 PPC4xx_IBM_DDR2_DUMP_REGISTER(R1BAS);
3204 PPC4xx_IBM_DDR2_DUMP_REGISTER(R2BAS);
3205 PPC4xx_IBM_DDR2_DUMP_REGISTER(R3BAS);
3206#endif /* (defined(CONFIG_440SP) || ... */
3207#if defined(CONFIG_405EX)
3208 PPC4xx_IBM_DDR2_DUMP_REGISTER(BESR);
3209 PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARL);
3210 PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARH);
3211 PPC4xx_IBM_DDR2_DUMP_REGISTER(WMIRQ);
3212 PPC4xx_IBM_DDR2_DUMP_REGISTER(PLBOPT);
3213 PPC4xx_IBM_DDR2_DUMP_REGISTER(PUABA);
3214#endif /* defined(CONFIG_405EX) */
3215 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB0CF);
3216 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB1CF);
3217 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB2CF);
3218 PPC4xx_IBM_DDR2_DUMP_REGISTER(MB3CF);
3219 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCSTAT);
3220 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT1);
3221 PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT2);
3222 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT0);
3223 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT1);
3224 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT2);
3225 PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT3);
3226 PPC4xx_IBM_DDR2_DUMP_REGISTER(CODT);
3227#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3228 defined(CONFIG_460EX) || defined(CONFIG_460GT))
3229 PPC4xx_IBM_DDR2_DUMP_REGISTER(VVPR);
3230 PPC4xx_IBM_DDR2_DUMP_REGISTER(OPARS);
3231 /*
3232 * OPART is only used as a trigger register.
3233 *
3234 * No data is contained in this register, and reading or writing
3235 * to is can cause bad things to happen (hangs). Just skip it and
3236 * report "N/A".
3237 */
3238 printf("%20s = N/A\n", "SDRAM_OPART");
3239#endif /* defined(CONFIG_440SP) || ... */
3240 PPC4xx_IBM_DDR2_DUMP_REGISTER(RTR);
3241 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR0);
3242 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR1);
3243 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR2);
3244 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR3);
3245 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR4);
3246 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR5);
3247 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR6);
3248 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR7);
3249 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR8);
3250 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR9);
3251 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR10);
3252 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR11);
3253 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR12);
3254 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR13);
3255 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR14);
3256 PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR15);
3257 PPC4xx_IBM_DDR2_DUMP_REGISTER(RQDC);
3258 PPC4xx_IBM_DDR2_DUMP_REGISTER(RFDC);
3259 PPC4xx_IBM_DDR2_DUMP_REGISTER(RDCC);
3260 PPC4xx_IBM_DDR2_DUMP_REGISTER(DLCR);
3261 PPC4xx_IBM_DDR2_DUMP_REGISTER(CLKTR);
3262 PPC4xx_IBM_DDR2_DUMP_REGISTER(WRDTR);
3263 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR1);
3264 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR2);
3265 PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR3);
3266 PPC4xx_IBM_DDR2_DUMP_REGISTER(MMODE);
3267 PPC4xx_IBM_DDR2_DUMP_REGISTER(MEMODE);
3268 PPC4xx_IBM_DDR2_DUMP_REGISTER(ECCCR);
3269#if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
3270 defined(CONFIG_460EX) || defined(CONFIG_460GT))
3271 PPC4xx_IBM_DDR2_DUMP_REGISTER(CID);
3272#endif /* defined(CONFIG_440SP) || ... */
3273 PPC4xx_IBM_DDR2_DUMP_REGISTER(RID);
3274 PPC4xx_IBM_DDR2_DUMP_REGISTER(FCSR);
3275 PPC4xx_IBM_DDR2_DUMP_REGISTER(RTSR);
Stefan Roese2001a332008-07-10 15:32:32 +02003276#endif /* defined(DEBUG) */
3277}
3278
3279#endif /* CONFIG_SDRAM_PPC4xx_IBM_DDR2 */