blob: 65ce46daf346c72981e00be6479d7a81290dd868 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Stefan Roesec2295332007-02-20 10:35:42 +01002 * cpu/ppc4xx/44x_spd_ddr.c
3 * This SPD DDR detection code supports IBM/AMCC PPC44x cpu with a
4 * DDR controller. Those are 440GP/GX/EP/GR.
5 *
wdenkfe8c2802002-11-03 00:38:21 +00006 * (C) Copyright 2001
7 * Bill Hunter, Wave 7 Optics, williamhunter@attbi.com
8 *
9 * Based on code by:
10 *
wdenkb666c8f2003-03-06 00:58:30 +000011 * Kenneth Johansson ,Ericsson AB.
12 * kenneth.johansson@etx.ericsson.se
wdenkfe8c2802002-11-03 00:38:21 +000013 *
14 * hacked up by bill hunter. fixed so we could run before
15 * serial_init and console_init. previous version avoided this by
16 * running out of cache memory during serial/console init, then running
17 * this code later.
18 *
19 * (C) Copyright 2002
20 * Jun Gu, Artesyn Technology, jung@artesyncp.com
Wolfgang Denk0ee70772005-09-23 11:05:55 +020021 * Support for AMCC 440 based on OpenBIOS draminit.c from IBM.
wdenkfe8c2802002-11-03 00:38:21 +000022 *
Stefan Roese63dc63c2007-06-01 15:09:50 +020023 * (C) Copyright 2005-2007
Stefan Roese326c9712005-08-01 16:41:48 +020024 * Stefan Roese, DENX Software Engineering, sr@denx.de.
25 *
wdenkfe8c2802002-11-03 00:38:21 +000026 * See file CREDITS for list of people who contributed to this
27 * project.
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License as
31 * published by the Free Software Foundation; either version 2 of
32 * the License, or (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 * MA 02111-1307 USA
43 */
44
Stefan Roese63dc63c2007-06-01 15:09:50 +020045/* define DEBUG for debugging output (obviously ;-)) */
46#if 0
47#define DEBUG
48#endif
49
wdenkfe8c2802002-11-03 00:38:21 +000050#include <common.h>
51#include <asm/processor.h>
52#include <i2c.h>
53#include <ppc4xx.h>
Stefan Roese5b89fc12007-03-07 16:39:36 +010054#include <asm/mmu.h>
wdenkfe8c2802002-11-03 00:38:21 +000055
Stefan Roesec2295332007-02-20 10:35:42 +010056#if defined(CONFIG_SPD_EEPROM) && \
57 (defined(CONFIG_440GP) || defined(CONFIG_440GX) || \
58 defined(CONFIG_440EP) || defined(CONFIG_440GR))
wdenkfe8c2802002-11-03 00:38:21 +000059
60/*
61 * Set default values
62 */
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010063#ifndef CFG_I2C_SPEED
64#define CFG_I2C_SPEED 50000
wdenkfe8c2802002-11-03 00:38:21 +000065#endif
66
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010067#ifndef CFG_I2C_SLAVE
68#define CFG_I2C_SLAVE 0xFE
wdenkfe8c2802002-11-03 00:38:21 +000069#endif
70
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010071#define ONE_BILLION 1000000000
Stefan Roese326c9712005-08-01 16:41:48 +020072
Heiko Schocher68310b02007-06-25 19:11:37 +020073/*
74 * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
75 */
76void __spd_ddr_init_hang (void)
77{
78 hang ();
79}
80void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang")));
Heiko Schocher633e03a2007-06-22 19:11:54 +020081
wdenkfe8c2802002-11-03 00:38:21 +000082/*-----------------------------------------------------------------------------
Stefan Roese326c9712005-08-01 16:41:48 +020083 | Memory Controller Options 0
84 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010085#define SDRAM_CFG0_DCEN 0x80000000 /* SDRAM Controller Enable */
86#define SDRAM_CFG0_MCHK_MASK 0x30000000 /* Memory data errchecking mask */
87#define SDRAM_CFG0_MCHK_NON 0x00000000 /* No ECC generation */
88#define SDRAM_CFG0_MCHK_GEN 0x20000000 /* ECC generation */
89#define SDRAM_CFG0_MCHK_CHK 0x30000000 /* ECC generation and checking */
90#define SDRAM_CFG0_RDEN 0x08000000 /* Registered DIMM enable */
91#define SDRAM_CFG0_PMUD 0x04000000 /* Page management unit */
92#define SDRAM_CFG0_DMWD_MASK 0x02000000 /* DRAM width mask */
93#define SDRAM_CFG0_DMWD_32 0x00000000 /* 32 bits */
94#define SDRAM_CFG0_DMWD_64 0x02000000 /* 64 bits */
95#define SDRAM_CFG0_UIOS_MASK 0x00C00000 /* Unused IO State */
96#define SDRAM_CFG0_PDP 0x00200000 /* Page deallocation policy */
wdenkfe8c2802002-11-03 00:38:21 +000097
98/*-----------------------------------------------------------------------------
Stefan Roese326c9712005-08-01 16:41:48 +020099 | Memory Controller Options 1
100 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100101#define SDRAM_CFG1_SRE 0x80000000 /* Self-Refresh Entry */
102#define SDRAM_CFG1_PMEN 0x40000000 /* Power Management Enable */
wdenkfe8c2802002-11-03 00:38:21 +0000103
104/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200105 | SDRAM DEVPOT Options
106 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100107#define SDRAM_DEVOPT_DLL 0x80000000
108#define SDRAM_DEVOPT_DS 0x40000000
wdenkfe8c2802002-11-03 00:38:21 +0000109
110/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200111 | SDRAM MCSTS Options
112 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100113#define SDRAM_MCSTS_MRSC 0x80000000
114#define SDRAM_MCSTS_SRMS 0x40000000
115#define SDRAM_MCSTS_CIS 0x20000000
wdenkfe8c2802002-11-03 00:38:21 +0000116
117/*-----------------------------------------------------------------------------
Stefan Roese326c9712005-08-01 16:41:48 +0200118 | SDRAM Refresh Timer Register
119 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100120#define SDRAM_RTR_RINT_MASK 0xFFFF0000
wdenkfe8c2802002-11-03 00:38:21 +0000121#define SDRAM_RTR_RINT_ENCODE(n) (((n) << 16) & SDRAM_RTR_RINT_MASK)
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100122#define sdram_HZ_to_ns(hertz) (1000000000/(hertz))
wdenkfe8c2802002-11-03 00:38:21 +0000123
124/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200125 | SDRAM UABus Base Address Reg
126 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100127#define SDRAM_UABBA_UBBA_MASK 0x0000000F
wdenkfe8c2802002-11-03 00:38:21 +0000128
129/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200130 | Memory Bank 0-7 configuration
131 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100132#define SDRAM_BXCR_SDBA_MASK 0xff800000 /* Base address */
133#define SDRAM_BXCR_SDSZ_MASK 0x000e0000 /* Size */
134#define SDRAM_BXCR_SDSZ_8 0x00020000 /* 8M */
135#define SDRAM_BXCR_SDSZ_16 0x00040000 /* 16M */
136#define SDRAM_BXCR_SDSZ_32 0x00060000 /* 32M */
137#define SDRAM_BXCR_SDSZ_64 0x00080000 /* 64M */
138#define SDRAM_BXCR_SDSZ_128 0x000a0000 /* 128M */
139#define SDRAM_BXCR_SDSZ_256 0x000c0000 /* 256M */
140#define SDRAM_BXCR_SDSZ_512 0x000e0000 /* 512M */
141#define SDRAM_BXCR_SDAM_MASK 0x0000e000 /* Addressing mode */
142#define SDRAM_BXCR_SDAM_1 0x00000000 /* Mode 1 */
143#define SDRAM_BXCR_SDAM_2 0x00002000 /* Mode 2 */
144#define SDRAM_BXCR_SDAM_3 0x00004000 /* Mode 3 */
145#define SDRAM_BXCR_SDAM_4 0x00006000 /* Mode 4 */
146#define SDRAM_BXCR_SDBE 0x00000001 /* Memory Bank Enable */
wdenkfe8c2802002-11-03 00:38:21 +0000147
148/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200149 | SDRAM TR0 Options
150 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100151#define SDRAM_TR0_SDWR_MASK 0x80000000
152#define SDRAM_TR0_SDWR_2_CLK 0x00000000
153#define SDRAM_TR0_SDWR_3_CLK 0x80000000
154#define SDRAM_TR0_SDWD_MASK 0x40000000
155#define SDRAM_TR0_SDWD_0_CLK 0x00000000
156#define SDRAM_TR0_SDWD_1_CLK 0x40000000
157#define SDRAM_TR0_SDCL_MASK 0x01800000
158#define SDRAM_TR0_SDCL_2_0_CLK 0x00800000
159#define SDRAM_TR0_SDCL_2_5_CLK 0x01000000
160#define SDRAM_TR0_SDCL_3_0_CLK 0x01800000
161#define SDRAM_TR0_SDPA_MASK 0x000C0000
162#define SDRAM_TR0_SDPA_2_CLK 0x00040000
163#define SDRAM_TR0_SDPA_3_CLK 0x00080000
164#define SDRAM_TR0_SDPA_4_CLK 0x000C0000
165#define SDRAM_TR0_SDCP_MASK 0x00030000
166#define SDRAM_TR0_SDCP_2_CLK 0x00000000
167#define SDRAM_TR0_SDCP_3_CLK 0x00010000
168#define SDRAM_TR0_SDCP_4_CLK 0x00020000
169#define SDRAM_TR0_SDCP_5_CLK 0x00030000
170#define SDRAM_TR0_SDLD_MASK 0x0000C000
171#define SDRAM_TR0_SDLD_1_CLK 0x00000000
172#define SDRAM_TR0_SDLD_2_CLK 0x00004000
173#define SDRAM_TR0_SDRA_MASK 0x0000001C
174#define SDRAM_TR0_SDRA_6_CLK 0x00000000
175#define SDRAM_TR0_SDRA_7_CLK 0x00000004
176#define SDRAM_TR0_SDRA_8_CLK 0x00000008
177#define SDRAM_TR0_SDRA_9_CLK 0x0000000C
178#define SDRAM_TR0_SDRA_10_CLK 0x00000010
179#define SDRAM_TR0_SDRA_11_CLK 0x00000014
180#define SDRAM_TR0_SDRA_12_CLK 0x00000018
181#define SDRAM_TR0_SDRA_13_CLK 0x0000001C
182#define SDRAM_TR0_SDRD_MASK 0x00000003
183#define SDRAM_TR0_SDRD_2_CLK 0x00000001
184#define SDRAM_TR0_SDRD_3_CLK 0x00000002
185#define SDRAM_TR0_SDRD_4_CLK 0x00000003
wdenkfe8c2802002-11-03 00:38:21 +0000186
187/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200188 | SDRAM TR1 Options
189 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100190#define SDRAM_TR1_RDSS_MASK 0xC0000000
191#define SDRAM_TR1_RDSS_TR0 0x00000000
192#define SDRAM_TR1_RDSS_TR1 0x40000000
193#define SDRAM_TR1_RDSS_TR2 0x80000000
194#define SDRAM_TR1_RDSS_TR3 0xC0000000
195#define SDRAM_TR1_RDSL_MASK 0x00C00000
196#define SDRAM_TR1_RDSL_STAGE1 0x00000000
197#define SDRAM_TR1_RDSL_STAGE2 0x00400000
198#define SDRAM_TR1_RDSL_STAGE3 0x00800000
199#define SDRAM_TR1_RDCD_MASK 0x00000800
200#define SDRAM_TR1_RDCD_RCD_0_0 0x00000000
201#define SDRAM_TR1_RDCD_RCD_1_2 0x00000800
202#define SDRAM_TR1_RDCT_MASK 0x000001FF
203#define SDRAM_TR1_RDCT_ENCODE(x) (((x) << 0) & SDRAM_TR1_RDCT_MASK)
204#define SDRAM_TR1_RDCT_DECODE(x) (((x) & SDRAM_TR1_RDCT_MASK) >> 0)
205#define SDRAM_TR1_RDCT_MIN 0x00000000
206#define SDRAM_TR1_RDCT_MAX 0x000001FF
wdenkfe8c2802002-11-03 00:38:21 +0000207
208/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200209 | SDRAM WDDCTR Options
210 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100211#define SDRAM_WDDCTR_WRCP_MASK 0xC0000000
212#define SDRAM_WDDCTR_WRCP_0DEG 0x00000000
213#define SDRAM_WDDCTR_WRCP_90DEG 0x40000000
214#define SDRAM_WDDCTR_WRCP_180DEG 0x80000000
215#define SDRAM_WDDCTR_DCD_MASK 0x000001FF
wdenkfe8c2802002-11-03 00:38:21 +0000216
217/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200218 | SDRAM CLKTR Options
219 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100220#define SDRAM_CLKTR_CLKP_MASK 0xC0000000
221#define SDRAM_CLKTR_CLKP_0DEG 0x00000000
222#define SDRAM_CLKTR_CLKP_90DEG 0x40000000
223#define SDRAM_CLKTR_CLKP_180DEG 0x80000000
224#define SDRAM_CLKTR_DCDT_MASK 0x000001FF
wdenkfe8c2802002-11-03 00:38:21 +0000225
226/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200227 | SDRAM DLYCAL Options
228 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100229#define SDRAM_DLYCAL_DLCV_MASK 0x000003FC
230#define SDRAM_DLYCAL_DLCV_ENCODE(x) (((x)<<2) & SDRAM_DLYCAL_DLCV_MASK)
231#define SDRAM_DLYCAL_DLCV_DECODE(x) (((x) & SDRAM_DLYCAL_DLCV_MASK)>>2)
wdenkfe8c2802002-11-03 00:38:21 +0000232
233/*-----------------------------------------------------------------------------+
Stefan Roese326c9712005-08-01 16:41:48 +0200234 | General Definition
235 +-----------------------------------------------------------------------------*/
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100236#define DEFAULT_SPD_ADDR1 0x53
237#define DEFAULT_SPD_ADDR2 0x52
238#define MAXBANKS 4 /* at most 4 dimm banks */
239#define MAX_SPD_BYTES 256
240#define NUMHALFCYCLES 4
241#define NUMMEMTESTS 8
242#define NUMMEMWORDS 8
243#define MAXBXCR 4
244#define TRUE 1
245#define FALSE 0
wdenkfe8c2802002-11-03 00:38:21 +0000246
Stefan Roese5b89fc12007-03-07 16:39:36 +0100247/*
248 * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory
249 * region. Right now the cache should still be disabled in U-Boot because of the
250 * EMAC driver, that need it's buffer descriptor to be located in non cached
251 * memory.
252 *
253 * If at some time this restriction doesn't apply anymore, just define
254 * CFG_ENABLE_SDRAM_CACHE in the board config file and this code should setup
255 * everything correctly.
256 */
257#ifdef CFG_ENABLE_SDRAM_CACHE
258#define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */
259#else
260#define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */
261#endif
262
Stefan Roesec45d1e32005-11-15 16:04:58 +0100263/* bank_parms is used to sort the bank sizes by descending order */
264struct bank_param {
265 unsigned long cr;
266 unsigned long bank_size_bytes;
267};
268
269typedef struct bank_param BANKPARMS;
270
271#ifdef CFG_SIMULATE_SPD_EEPROM
Eugene OBrienc59d1a02007-07-31 10:24:56 +0200272extern const unsigned char cfg_simulate_spd_eeprom[128];
Stefan Roesec45d1e32005-11-15 16:04:58 +0100273#endif
wdenkfe8c2802002-11-03 00:38:21 +0000274
Stefan Roese63dc63c2007-06-01 15:09:50 +0200275static unsigned char spd_read(uchar chip, uint addr);
276static void get_spd_info(unsigned long *dimm_populated,
277 unsigned char *iic0_dimm_addr,
278 unsigned long num_dimm_banks);
279static void check_mem_type(unsigned long *dimm_populated,
280 unsigned char *iic0_dimm_addr,
281 unsigned long num_dimm_banks);
282static void check_volt_type(unsigned long *dimm_populated,
283 unsigned char *iic0_dimm_addr,
284 unsigned long num_dimm_banks);
285static void program_cfg0(unsigned long *dimm_populated,
286 unsigned char *iic0_dimm_addr,
287 unsigned long num_dimm_banks);
288static void program_cfg1(unsigned long *dimm_populated,
289 unsigned char *iic0_dimm_addr,
290 unsigned long num_dimm_banks);
291static void program_rtr(unsigned long *dimm_populated,
292 unsigned char *iic0_dimm_addr,
293 unsigned long num_dimm_banks);
294static void program_tr0(unsigned long *dimm_populated,
295 unsigned char *iic0_dimm_addr,
296 unsigned long num_dimm_banks);
297static void program_tr1(void);
wdenkfe8c2802002-11-03 00:38:21 +0000298
Stefan Roese63dc63c2007-06-01 15:09:50 +0200299#ifdef CONFIG_DDR_ECC
300static void program_ecc(unsigned long num_bytes);
301#endif
wdenkfe8c2802002-11-03 00:38:21 +0000302
Stefan Roese63dc63c2007-06-01 15:09:50 +0200303static unsigned long program_bxcr(unsigned long *dimm_populated,
304 unsigned char *iic0_dimm_addr,
305 unsigned long num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000306
307/*
308 * This function is reading data from the DIMM module EEPROM over the SPD bus
309 * and uses that to program the sdram controller.
310 *
Wolfgang Denk0ee70772005-09-23 11:05:55 +0200311 * This works on boards that has the same schematics that the AMCC walnut has.
wdenkfe8c2802002-11-03 00:38:21 +0000312 *
313 * BUG: Don't handle ECC memory
314 * BUG: A few values in the TR register is currently hardcoded
315 */
wdenkfe8c2802002-11-03 00:38:21 +0000316long int spd_sdram(void) {
Stefan Roese326c9712005-08-01 16:41:48 +0200317 unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
318 unsigned long dimm_populated[sizeof(iic0_dimm_addr)];
319 unsigned long total_size;
320 unsigned long cfg0;
321 unsigned long mcsts;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100322 unsigned long num_dimm_banks; /* on board dimm banks */
wdenkfe8c2802002-11-03 00:38:21 +0000323
Stefan Roese326c9712005-08-01 16:41:48 +0200324 num_dimm_banks = sizeof(iic0_dimm_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000325
326 /*
327 * Make sure I2C controller is initialized
328 * before continuing.
329 */
330 i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
331
Stefan Roese326c9712005-08-01 16:41:48 +0200332 /*
333 * Read the SPD information using I2C interface. Check to see if the
334 * DIMM slots are populated.
335 */
336 get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000337
Stefan Roese326c9712005-08-01 16:41:48 +0200338 /*
339 * Check the memory type for the dimms plugged.
340 */
341 check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000342
Stefan Roese326c9712005-08-01 16:41:48 +0200343 /*
344 * Check the voltage type for the dimms plugged.
345 */
346 check_volt_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000347
Stefan Roese82041f52006-06-13 18:55:07 +0200348#if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR) || defined(CONFIG_440SP)
Stefan Roese326c9712005-08-01 16:41:48 +0200349 /*
350 * Soft-reset SDRAM controller.
351 */
352 mtsdr(sdr_srst, SDR0_SRST_DMC);
353 mtsdr(sdr_srst, 0x00000000);
wdenk6148e742005-04-03 20:55:38 +0000354#endif
355
Stefan Roese326c9712005-08-01 16:41:48 +0200356 /*
357 * program 440GP SDRAM controller options (SDRAM0_CFG0)
358 */
359 program_cfg0(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000360
Stefan Roese326c9712005-08-01 16:41:48 +0200361 /*
362 * program 440GP SDRAM controller options (SDRAM0_CFG1)
363 */
364 program_cfg1(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000365
Stefan Roese326c9712005-08-01 16:41:48 +0200366 /*
367 * program SDRAM refresh register (SDRAM0_RTR)
368 */
369 program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000370
Stefan Roese326c9712005-08-01 16:41:48 +0200371 /*
372 * program SDRAM Timing Register 0 (SDRAM0_TR0)
373 */
374 program_tr0(dimm_populated, iic0_dimm_addr, num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000375
Stefan Roese326c9712005-08-01 16:41:48 +0200376 /*
377 * program the BxCR registers to find out total sdram installed
378 */
379 total_size = program_bxcr(dimm_populated, iic0_dimm_addr,
380 num_dimm_banks);
wdenkfe8c2802002-11-03 00:38:21 +0000381
Stefan Roese5b89fc12007-03-07 16:39:36 +0100382#ifdef CONFIG_PROG_SDRAM_TLB /* this define should eventually be removed */
383 /* and program tlb entries for this size (dynamic) */
Stefan Roesefb33c762007-06-14 11:14:32 +0200384 program_tlb(0, 0, total_size, MY_TLB_WORD2_I_ENABLE);
Stefan Roese5b89fc12007-03-07 16:39:36 +0100385#endif
386
Stefan Roese326c9712005-08-01 16:41:48 +0200387 /*
388 * program SDRAM Clock Timing Register (SDRAM0_CLKTR)
389 */
390 mtsdram(mem_clktr, 0x40000000);
wdenkfe8c2802002-11-03 00:38:21 +0000391
Stefan Roese326c9712005-08-01 16:41:48 +0200392 /*
393 * delay to ensure 200 usec has elapsed
394 */
395 udelay(400);
wdenkfe8c2802002-11-03 00:38:21 +0000396
Stefan Roese326c9712005-08-01 16:41:48 +0200397 /*
398 * enable the memory controller
399 */
400 mfsdram(mem_cfg0, cfg0);
401 mtsdram(mem_cfg0, cfg0 | SDRAM_CFG0_DCEN);
wdenkfe8c2802002-11-03 00:38:21 +0000402
Stefan Roese326c9712005-08-01 16:41:48 +0200403 /*
404 * wait for SDRAM_CFG0_DC_EN to complete
405 */
406 while (1) {
407 mfsdram(mem_mcsts, mcsts);
Stefan Roese63dc63c2007-06-01 15:09:50 +0200408 if ((mcsts & SDRAM_MCSTS_MRSC) != 0)
Stefan Roese326c9712005-08-01 16:41:48 +0200409 break;
wdenk57b2d802003-06-27 21:31:46 +0000410 }
wdenkfe8c2802002-11-03 00:38:21 +0000411
Stefan Roese326c9712005-08-01 16:41:48 +0200412 /*
413 * program SDRAM Timing Register 1, adding some delays
414 */
415 program_tr1();
wdenkfe8c2802002-11-03 00:38:21 +0000416
Stefan Roese63dc63c2007-06-01 15:09:50 +0200417#ifdef CONFIG_DDR_ECC
Stefan Roese326c9712005-08-01 16:41:48 +0200418 /*
Stefan Roese63dc63c2007-06-01 15:09:50 +0200419 * If ecc is enabled, initialize the parity bits.
Stefan Roese326c9712005-08-01 16:41:48 +0200420 */
Stefan Roese63dc63c2007-06-01 15:09:50 +0200421 program_ecc(total_size);
422#endif
wdenkfe8c2802002-11-03 00:38:21 +0000423
424 return total_size;
425}
426
Stefan Roese63dc63c2007-06-01 15:09:50 +0200427static unsigned char spd_read(uchar chip, uint addr)
Stefan Roesec45d1e32005-11-15 16:04:58 +0100428{
wdenkfe8c2802002-11-03 00:38:21 +0000429 unsigned char data[2];
430
Stefan Roesec45d1e32005-11-15 16:04:58 +0100431#ifdef CFG_SIMULATE_SPD_EEPROM
432 if (chip == CFG_SIMULATE_SPD_EEPROM) {
433 /*
434 * Onboard spd eeprom requested -> simulate values
435 */
436 return cfg_simulate_spd_eeprom[addr];
437 }
438#endif /* CFG_SIMULATE_SPD_EEPROM */
439
Stefan Roese326c9712005-08-01 16:41:48 +0200440 if (i2c_probe(chip) == 0) {
441 if (i2c_read(chip, addr, 1, data, 1) == 0) {
442 return data[0];
443 }
444 }
445
446 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000447}
448
Stefan Roese63dc63c2007-06-01 15:09:50 +0200449static void get_spd_info(unsigned long *dimm_populated,
450 unsigned char *iic0_dimm_addr,
451 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000452{
Stefan Roese326c9712005-08-01 16:41:48 +0200453 unsigned long dimm_num;
454 unsigned long dimm_found;
455 unsigned char num_of_bytes;
456 unsigned char total_size;
wdenkfe8c2802002-11-03 00:38:21 +0000457
Stefan Roese326c9712005-08-01 16:41:48 +0200458 dimm_found = FALSE;
459 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
460 num_of_bytes = 0;
461 total_size = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000462
Stefan Roese326c9712005-08-01 16:41:48 +0200463 num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
464 total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
wdenkfe8c2802002-11-03 00:38:21 +0000465
Stefan Roese326c9712005-08-01 16:41:48 +0200466 if ((num_of_bytes != 0) && (total_size != 0)) {
467 dimm_populated[dimm_num] = TRUE;
468 dimm_found = TRUE;
Stefan Roese63dc63c2007-06-01 15:09:50 +0200469 debug("DIMM slot %lu: populated\n", dimm_num);
Stefan Roese326c9712005-08-01 16:41:48 +0200470 } else {
471 dimm_populated[dimm_num] = FALSE;
Stefan Roese63dc63c2007-06-01 15:09:50 +0200472 debug("DIMM slot %lu: Not populated\n", dimm_num);
Stefan Roese326c9712005-08-01 16:41:48 +0200473 }
wdenk57b2d802003-06-27 21:31:46 +0000474 }
wdenkfe8c2802002-11-03 00:38:21 +0000475
Stefan Roese326c9712005-08-01 16:41:48 +0200476 if (dimm_found == FALSE) {
477 printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200478 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200479 }
wdenkfe8c2802002-11-03 00:38:21 +0000480}
481
Stefan Roese63dc63c2007-06-01 15:09:50 +0200482static void check_mem_type(unsigned long *dimm_populated,
483 unsigned char *iic0_dimm_addr,
484 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000485{
Stefan Roese326c9712005-08-01 16:41:48 +0200486 unsigned long dimm_num;
487 unsigned char dimm_type;
wdenkfe8c2802002-11-03 00:38:21 +0000488
Stefan Roese326c9712005-08-01 16:41:48 +0200489 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
490 if (dimm_populated[dimm_num] == TRUE) {
491 dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2);
492 switch (dimm_type) {
493 case 7:
Stefan Roese63dc63c2007-06-01 15:09:50 +0200494 debug("DIMM slot %lu: DDR SDRAM detected\n", dimm_num);
Stefan Roese326c9712005-08-01 16:41:48 +0200495 break;
496 default:
497 printf("ERROR: Unsupported DIMM detected in slot %lu.\n",
498 dimm_num);
499 printf("Only DDR SDRAM DIMMs are supported.\n");
500 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200501 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200502 break;
503 }
504 }
wdenk57b2d802003-06-27 21:31:46 +0000505 }
wdenkfe8c2802002-11-03 00:38:21 +0000506}
507
Stefan Roese63dc63c2007-06-01 15:09:50 +0200508static void check_volt_type(unsigned long *dimm_populated,
509 unsigned char *iic0_dimm_addr,
510 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000511{
Stefan Roese326c9712005-08-01 16:41:48 +0200512 unsigned long dimm_num;
513 unsigned long voltage_type;
wdenkfe8c2802002-11-03 00:38:21 +0000514
Stefan Roese326c9712005-08-01 16:41:48 +0200515 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
516 if (dimm_populated[dimm_num] == TRUE) {
517 voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
518 if (voltage_type != 0x04) {
519 printf("ERROR: DIMM %lu with unsupported voltage level.\n",
520 dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200521 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200522 } else {
Stefan Roese63dc63c2007-06-01 15:09:50 +0200523 debug("DIMM %lu voltage level supported.\n", dimm_num);
Stefan Roese326c9712005-08-01 16:41:48 +0200524 }
525 break;
526 }
wdenk57b2d802003-06-27 21:31:46 +0000527 }
wdenkfe8c2802002-11-03 00:38:21 +0000528}
529
Stefan Roese63dc63c2007-06-01 15:09:50 +0200530static void program_cfg0(unsigned long *dimm_populated,
531 unsigned char *iic0_dimm_addr,
532 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000533{
Stefan Roese326c9712005-08-01 16:41:48 +0200534 unsigned long dimm_num;
535 unsigned long cfg0;
536 unsigned long ecc_enabled;
537 unsigned char ecc;
538 unsigned char attributes;
539 unsigned long data_width;
540 unsigned long dimm_32bit;
541 unsigned long dimm_64bit;
wdenkfe8c2802002-11-03 00:38:21 +0000542
Stefan Roese326c9712005-08-01 16:41:48 +0200543 /*
544 * get Memory Controller Options 0 data
545 */
546 mfsdram(mem_cfg0, cfg0);
wdenkfe8c2802002-11-03 00:38:21 +0000547
Stefan Roese326c9712005-08-01 16:41:48 +0200548 /*
549 * clear bits
550 */
551 cfg0 &= ~(SDRAM_CFG0_DCEN | SDRAM_CFG0_MCHK_MASK |
552 SDRAM_CFG0_RDEN | SDRAM_CFG0_PMUD |
553 SDRAM_CFG0_DMWD_MASK |
554 SDRAM_CFG0_UIOS_MASK | SDRAM_CFG0_PDP);
wdenkfe8c2802002-11-03 00:38:21 +0000555
556
Stefan Roese326c9712005-08-01 16:41:48 +0200557 /*
558 * FIXME: assume the DDR SDRAMs in both banks are the same
559 */
560 ecc_enabled = TRUE;
561 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
562 if (dimm_populated[dimm_num] == TRUE) {
563 ecc = spd_read(iic0_dimm_addr[dimm_num], 11);
564 if (ecc != 0x02) {
565 ecc_enabled = FALSE;
566 }
wdenkfe8c2802002-11-03 00:38:21 +0000567
Stefan Roese326c9712005-08-01 16:41:48 +0200568 /*
569 * program Registered DIMM Enable
570 */
571 attributes = spd_read(iic0_dimm_addr[dimm_num], 21);
572 if ((attributes & 0x02) != 0x00) {
573 cfg0 |= SDRAM_CFG0_RDEN;
574 }
wdenkfe8c2802002-11-03 00:38:21 +0000575
Stefan Roese326c9712005-08-01 16:41:48 +0200576 /*
577 * program DDR SDRAM Data Width
578 */
579 data_width =
580 (unsigned long)spd_read(iic0_dimm_addr[dimm_num],6) +
581 (((unsigned long)spd_read(iic0_dimm_addr[dimm_num],7)) << 8);
582 if (data_width == 64 || data_width == 72) {
583 dimm_64bit = TRUE;
584 cfg0 |= SDRAM_CFG0_DMWD_64;
585 } else if (data_width == 32 || data_width == 40) {
586 dimm_32bit = TRUE;
587 cfg0 |= SDRAM_CFG0_DMWD_32;
588 } else {
589 printf("WARNING: DIMM with datawidth of %lu bits.\n",
590 data_width);
591 printf("Only DIMMs with 32 or 64 bit datawidths supported.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200592 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200593 }
594 break;
595 }
wdenk57b2d802003-06-27 21:31:46 +0000596 }
wdenkfe8c2802002-11-03 00:38:21 +0000597
Stefan Roese326c9712005-08-01 16:41:48 +0200598 /*
599 * program Memory Data Error Checking
600 */
601 if (ecc_enabled == TRUE) {
602 cfg0 |= SDRAM_CFG0_MCHK_GEN;
603 } else {
604 cfg0 |= SDRAM_CFG0_MCHK_NON;
605 }
wdenkfe8c2802002-11-03 00:38:21 +0000606
Stefan Roese326c9712005-08-01 16:41:48 +0200607 /*
Stefan Roesee335ee02006-07-28 18:34:58 +0200608 * program Page Management Unit (0 == enabled)
Stefan Roese326c9712005-08-01 16:41:48 +0200609 */
Stefan Roesee335ee02006-07-28 18:34:58 +0200610 cfg0 &= ~SDRAM_CFG0_PMUD;
wdenkfe8c2802002-11-03 00:38:21 +0000611
Stefan Roese326c9712005-08-01 16:41:48 +0200612 /*
613 * program Memory Controller Options 0
614 * Note: DCEN must be enabled after all DDR SDRAM controller
615 * configuration registers get initialized.
616 */
617 mtsdram(mem_cfg0, cfg0);
wdenkfe8c2802002-11-03 00:38:21 +0000618}
619
Stefan Roese63dc63c2007-06-01 15:09:50 +0200620static void program_cfg1(unsigned long *dimm_populated,
621 unsigned char *iic0_dimm_addr,
622 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000623{
Stefan Roese326c9712005-08-01 16:41:48 +0200624 unsigned long cfg1;
625 mfsdram(mem_cfg1, cfg1);
wdenkfe8c2802002-11-03 00:38:21 +0000626
Stefan Roese326c9712005-08-01 16:41:48 +0200627 /*
628 * Self-refresh exit, disable PM
629 */
630 cfg1 &= ~(SDRAM_CFG1_SRE | SDRAM_CFG1_PMEN);
wdenkfe8c2802002-11-03 00:38:21 +0000631
Stefan Roese326c9712005-08-01 16:41:48 +0200632 /*
633 * program Memory Controller Options 1
634 */
635 mtsdram(mem_cfg1, cfg1);
wdenkfe8c2802002-11-03 00:38:21 +0000636}
637
Stefan Roese63dc63c2007-06-01 15:09:50 +0200638static void program_rtr(unsigned long *dimm_populated,
639 unsigned char *iic0_dimm_addr,
640 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000641{
Stefan Roese326c9712005-08-01 16:41:48 +0200642 unsigned long dimm_num;
643 unsigned long bus_period_x_10;
644 unsigned long refresh_rate = 0;
645 unsigned char refresh_rate_type;
646 unsigned long refresh_interval;
647 unsigned long sdram_rtr;
Stefan Roeseedd73f22007-10-21 08:12:41 +0200648 PPC4xx_SYS_INFO sys_info;
wdenkfe8c2802002-11-03 00:38:21 +0000649
Stefan Roese326c9712005-08-01 16:41:48 +0200650 /*
651 * get the board info
652 */
653 get_sys_info(&sys_info);
654 bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
wdenkfe8c2802002-11-03 00:38:21 +0000655
Stefan Roese326c9712005-08-01 16:41:48 +0200656 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
657 if (dimm_populated[dimm_num] == TRUE) {
658 refresh_rate_type = 0x7F & spd_read(iic0_dimm_addr[dimm_num], 12);
659 switch (refresh_rate_type) {
660 case 0x00:
661 refresh_rate = 15625;
662 break;
663 case 0x01:
664 refresh_rate = 15625/4;
665 break;
666 case 0x02:
667 refresh_rate = 15625/2;
668 break;
669 case 0x03:
670 refresh_rate = 15626*2;
671 break;
672 case 0x04:
673 refresh_rate = 15625*4;
674 break;
675 case 0x05:
676 refresh_rate = 15625*8;
677 break;
678 default:
679 printf("ERROR: DIMM %lu, unsupported refresh rate/type.\n",
680 dimm_num);
681 printf("Replace the DIMM module with a supported DIMM.\n");
682 break;
683 }
wdenkfe8c2802002-11-03 00:38:21 +0000684
Stefan Roese326c9712005-08-01 16:41:48 +0200685 break;
686 }
wdenk57b2d802003-06-27 21:31:46 +0000687 }
wdenkfe8c2802002-11-03 00:38:21 +0000688
Stefan Roese326c9712005-08-01 16:41:48 +0200689 refresh_interval = refresh_rate * 10 / bus_period_x_10;
690 sdram_rtr = (refresh_interval & 0x3ff8) << 16;
wdenkfe8c2802002-11-03 00:38:21 +0000691
Stefan Roese326c9712005-08-01 16:41:48 +0200692 /*
693 * program Refresh Timer Register (SDRAM0_RTR)
694 */
695 mtsdram(mem_rtr, sdram_rtr);
wdenkfe8c2802002-11-03 00:38:21 +0000696}
697
Stefan Roese63dc63c2007-06-01 15:09:50 +0200698static void program_tr0(unsigned long *dimm_populated,
699 unsigned char *iic0_dimm_addr,
700 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +0000701{
Stefan Roese326c9712005-08-01 16:41:48 +0200702 unsigned long dimm_num;
703 unsigned long tr0;
704 unsigned char wcsbc;
705 unsigned char t_rp_ns;
706 unsigned char t_rcd_ns;
707 unsigned char t_ras_ns;
708 unsigned long t_rp_clk;
709 unsigned long t_ras_rcd_clk;
710 unsigned long t_rcd_clk;
711 unsigned long t_rfc_clk;
712 unsigned long plb_check;
713 unsigned char cas_bit;
714 unsigned long cas_index;
715 unsigned char cas_2_0_available;
716 unsigned char cas_2_5_available;
717 unsigned char cas_3_0_available;
718 unsigned long cycle_time_ns_x_10[3];
719 unsigned long tcyc_3_0_ns_x_10;
720 unsigned long tcyc_2_5_ns_x_10;
721 unsigned long tcyc_2_0_ns_x_10;
722 unsigned long tcyc_reg;
723 unsigned long bus_period_x_10;
Stefan Roeseedd73f22007-10-21 08:12:41 +0200724 PPC4xx_SYS_INFO sys_info;
Stefan Roese326c9712005-08-01 16:41:48 +0200725 unsigned long residue;
wdenkfe8c2802002-11-03 00:38:21 +0000726
Stefan Roese326c9712005-08-01 16:41:48 +0200727 /*
728 * get the board info
729 */
730 get_sys_info(&sys_info);
731 bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
wdenkfe8c2802002-11-03 00:38:21 +0000732
Stefan Roese326c9712005-08-01 16:41:48 +0200733 /*
734 * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits
735 */
736 mfsdram(mem_tr0, tr0);
737 tr0 &= ~(SDRAM_TR0_SDWR_MASK | SDRAM_TR0_SDWD_MASK |
738 SDRAM_TR0_SDCL_MASK | SDRAM_TR0_SDPA_MASK |
739 SDRAM_TR0_SDCP_MASK | SDRAM_TR0_SDLD_MASK |
740 SDRAM_TR0_SDRA_MASK | SDRAM_TR0_SDRD_MASK);
wdenkfe8c2802002-11-03 00:38:21 +0000741
Stefan Roese326c9712005-08-01 16:41:48 +0200742 /*
743 * initialization
744 */
745 wcsbc = 0;
746 t_rp_ns = 0;
747 t_rcd_ns = 0;
748 t_ras_ns = 0;
749 cas_2_0_available = TRUE;
750 cas_2_5_available = TRUE;
751 cas_3_0_available = TRUE;
752 tcyc_2_0_ns_x_10 = 0;
753 tcyc_2_5_ns_x_10 = 0;
754 tcyc_3_0_ns_x_10 = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000755
Stefan Roese326c9712005-08-01 16:41:48 +0200756 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
757 if (dimm_populated[dimm_num] == TRUE) {
758 wcsbc = spd_read(iic0_dimm_addr[dimm_num], 15);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100759 t_rp_ns = spd_read(iic0_dimm_addr[dimm_num], 27) >> 2;
Stefan Roese326c9712005-08-01 16:41:48 +0200760 t_rcd_ns = spd_read(iic0_dimm_addr[dimm_num], 29) >> 2;
761 t_ras_ns = spd_read(iic0_dimm_addr[dimm_num], 30);
762 cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18);
wdenkfe8c2802002-11-03 00:38:21 +0000763
Stefan Roese326c9712005-08-01 16:41:48 +0200764 for (cas_index = 0; cas_index < 3; cas_index++) {
765 switch (cas_index) {
766 case 0:
767 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
768 break;
769 case 1:
770 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23);
771 break;
772 default:
773 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25);
774 break;
775 }
wdenkfe8c2802002-11-03 00:38:21 +0000776
Stefan Roese326c9712005-08-01 16:41:48 +0200777 if ((tcyc_reg & 0x0F) >= 10) {
778 printf("ERROR: Tcyc incorrect for DIMM in slot %lu\n",
779 dimm_num);
Heiko Schocher68310b02007-06-25 19:11:37 +0200780 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200781 }
wdenkfe8c2802002-11-03 00:38:21 +0000782
Stefan Roese326c9712005-08-01 16:41:48 +0200783 cycle_time_ns_x_10[cas_index] =
784 (((tcyc_reg & 0xF0) >> 4) * 10) + (tcyc_reg & 0x0F);
785 }
wdenkfe8c2802002-11-03 00:38:21 +0000786
Stefan Roese326c9712005-08-01 16:41:48 +0200787 cas_index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000788
Stefan Roese326c9712005-08-01 16:41:48 +0200789 if ((cas_bit & 0x80) != 0) {
790 cas_index += 3;
791 } else if ((cas_bit & 0x40) != 0) {
792 cas_index += 2;
793 } else if ((cas_bit & 0x20) != 0) {
794 cas_index += 1;
795 }
wdenkfe8c2802002-11-03 00:38:21 +0000796
Stefan Roese326c9712005-08-01 16:41:48 +0200797 if (((cas_bit & 0x10) != 0) && (cas_index < 3)) {
798 tcyc_3_0_ns_x_10 = cycle_time_ns_x_10[cas_index];
799 cas_index++;
800 } else {
801 if (cas_index != 0) {
802 cas_index++;
803 }
804 cas_3_0_available = FALSE;
805 }
wdenkfe8c2802002-11-03 00:38:21 +0000806
Stefan Roese326c9712005-08-01 16:41:48 +0200807 if (((cas_bit & 0x08) != 0) || (cas_index < 3)) {
808 tcyc_2_5_ns_x_10 = cycle_time_ns_x_10[cas_index];
809 cas_index++;
810 } else {
811 if (cas_index != 0) {
812 cas_index++;
813 }
814 cas_2_5_available = FALSE;
815 }
wdenkfe8c2802002-11-03 00:38:21 +0000816
Stefan Roese326c9712005-08-01 16:41:48 +0200817 if (((cas_bit & 0x04) != 0) || (cas_index < 3)) {
818 tcyc_2_0_ns_x_10 = cycle_time_ns_x_10[cas_index];
819 cas_index++;
820 } else {
821 if (cas_index != 0) {
822 cas_index++;
823 }
824 cas_2_0_available = FALSE;
825 }
wdenkfe8c2802002-11-03 00:38:21 +0000826
Stefan Roese326c9712005-08-01 16:41:48 +0200827 break;
828 }
wdenk57b2d802003-06-27 21:31:46 +0000829 }
wdenkfe8c2802002-11-03 00:38:21 +0000830
Stefan Roese326c9712005-08-01 16:41:48 +0200831 /*
832 * Program SD_WR and SD_WCSBC fields
833 */
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100834 tr0 |= SDRAM_TR0_SDWR_2_CLK; /* Write Recovery: 2 CLK */
Stefan Roese326c9712005-08-01 16:41:48 +0200835 switch (wcsbc) {
836 case 0:
837 tr0 |= SDRAM_TR0_SDWD_0_CLK;
838 break;
839 default:
840 tr0 |= SDRAM_TR0_SDWD_1_CLK;
841 break;
842 }
wdenkfe8c2802002-11-03 00:38:21 +0000843
Stefan Roese326c9712005-08-01 16:41:48 +0200844 /*
845 * Program SD_CASL field
846 */
847 if ((cas_2_0_available == TRUE) &&
848 (bus_period_x_10 >= tcyc_2_0_ns_x_10)) {
849 tr0 |= SDRAM_TR0_SDCL_2_0_CLK;
850 } else if ((cas_2_5_available == TRUE) &&
851 (bus_period_x_10 >= tcyc_2_5_ns_x_10)) {
852 tr0 |= SDRAM_TR0_SDCL_2_5_CLK;
853 } else if ((cas_3_0_available == TRUE) &&
854 (bus_period_x_10 >= tcyc_3_0_ns_x_10)) {
855 tr0 |= SDRAM_TR0_SDCL_3_0_CLK;
856 } else {
857 printf("ERROR: No supported CAS latency with the installed DIMMs.\n");
858 printf("Only CAS latencies of 2.0, 2.5, and 3.0 are supported.\n");
859 printf("Make sure the PLB speed is within the supported range.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +0200860 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +0200861 }
wdenkfe8c2802002-11-03 00:38:21 +0000862
Stefan Roese326c9712005-08-01 16:41:48 +0200863 /*
864 * Calculate Trp in clock cycles and round up if necessary
865 * Program SD_PTA field
866 */
867 t_rp_clk = sys_info.freqPLB * t_rp_ns / ONE_BILLION;
868 plb_check = ONE_BILLION * t_rp_clk / t_rp_ns;
869 if (sys_info.freqPLB != plb_check) {
870 t_rp_clk++;
871 }
872 switch ((unsigned long)t_rp_clk) {
873 case 0:
874 case 1:
875 case 2:
876 tr0 |= SDRAM_TR0_SDPA_2_CLK;
877 break;
878 case 3:
879 tr0 |= SDRAM_TR0_SDPA_3_CLK;
880 break;
881 default:
882 tr0 |= SDRAM_TR0_SDPA_4_CLK;
883 break;
884 }
wdenkfe8c2802002-11-03 00:38:21 +0000885
Stefan Roese326c9712005-08-01 16:41:48 +0200886 /*
887 * Program SD_CTP field
888 */
889 t_ras_rcd_clk = sys_info.freqPLB * (t_ras_ns - t_rcd_ns) / ONE_BILLION;
890 plb_check = ONE_BILLION * t_ras_rcd_clk / (t_ras_ns - t_rcd_ns);
891 if (sys_info.freqPLB != plb_check) {
892 t_ras_rcd_clk++;
893 }
894 switch (t_ras_rcd_clk) {
895 case 0:
896 case 1:
897 case 2:
898 tr0 |= SDRAM_TR0_SDCP_2_CLK;
899 break;
900 case 3:
901 tr0 |= SDRAM_TR0_SDCP_3_CLK;
902 break;
903 case 4:
904 tr0 |= SDRAM_TR0_SDCP_4_CLK;
905 break;
906 default:
907 tr0 |= SDRAM_TR0_SDCP_5_CLK;
908 break;
909 }
wdenkfe8c2802002-11-03 00:38:21 +0000910
Stefan Roese326c9712005-08-01 16:41:48 +0200911 /*
912 * Program SD_LDF field
913 */
914 tr0 |= SDRAM_TR0_SDLD_2_CLK;
wdenkfe8c2802002-11-03 00:38:21 +0000915
Stefan Roese326c9712005-08-01 16:41:48 +0200916 /*
917 * Program SD_RFTA field
918 * FIXME tRFC hardcoded as 75 nanoseconds
919 */
920 t_rfc_clk = sys_info.freqPLB / (ONE_BILLION / 75);
921 residue = sys_info.freqPLB % (ONE_BILLION / 75);
922 if (residue >= (ONE_BILLION / 150)) {
923 t_rfc_clk++;
924 }
925 switch (t_rfc_clk) {
926 case 0:
927 case 1:
928 case 2:
929 case 3:
930 case 4:
931 case 5:
932 case 6:
933 tr0 |= SDRAM_TR0_SDRA_6_CLK;
934 break;
935 case 7:
936 tr0 |= SDRAM_TR0_SDRA_7_CLK;
937 break;
938 case 8:
939 tr0 |= SDRAM_TR0_SDRA_8_CLK;
940 break;
941 case 9:
942 tr0 |= SDRAM_TR0_SDRA_9_CLK;
943 break;
944 case 10:
945 tr0 |= SDRAM_TR0_SDRA_10_CLK;
946 break;
947 case 11:
948 tr0 |= SDRAM_TR0_SDRA_11_CLK;
949 break;
950 case 12:
951 tr0 |= SDRAM_TR0_SDRA_12_CLK;
952 break;
953 default:
954 tr0 |= SDRAM_TR0_SDRA_13_CLK;
955 break;
956 }
wdenkfe8c2802002-11-03 00:38:21 +0000957
Stefan Roese326c9712005-08-01 16:41:48 +0200958 /*
959 * Program SD_RCD field
960 */
961 t_rcd_clk = sys_info.freqPLB * t_rcd_ns / ONE_BILLION;
962 plb_check = ONE_BILLION * t_rcd_clk / t_rcd_ns;
963 if (sys_info.freqPLB != plb_check) {
964 t_rcd_clk++;
965 }
966 switch (t_rcd_clk) {
967 case 0:
968 case 1:
969 case 2:
970 tr0 |= SDRAM_TR0_SDRD_2_CLK;
971 break;
972 case 3:
973 tr0 |= SDRAM_TR0_SDRD_3_CLK;
974 break;
975 default:
976 tr0 |= SDRAM_TR0_SDRD_4_CLK;
977 break;
978 }
wdenkfe8c2802002-11-03 00:38:21 +0000979
Stefan Roese63dc63c2007-06-01 15:09:50 +0200980 debug("tr0: %x\n", tr0);
Stefan Roese326c9712005-08-01 16:41:48 +0200981 mtsdram(mem_tr0, tr0);
wdenkfe8c2802002-11-03 00:38:21 +0000982}
983
Stefan Roese63dc63c2007-06-01 15:09:50 +0200984static int short_mem_test(void)
985{
986 unsigned long i, j;
987 unsigned long bxcr_num;
988 unsigned long *membase;
989 const unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
990 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
991 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
992 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
993 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
994 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
995 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
996 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
997 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
998 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
999 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
1000 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
1001 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
1002 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
1003 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
1004 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
1005 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55}};
1006
1007 for (bxcr_num = 0; bxcr_num < MAXBXCR; bxcr_num++) {
1008 mtdcr(memcfga, mem_b0cr + (bxcr_num << 2));
1009 if ((mfdcr(memcfgd) & SDRAM_BXCR_SDBE) == SDRAM_BXCR_SDBE) {
1010 /* Bank is enabled */
1011 membase = (unsigned long*)
1012 (mfdcr(memcfgd) & SDRAM_BXCR_SDBA_MASK);
1013
1014 /*
1015 * Run the short memory test
1016 */
1017 for (i = 0; i < NUMMEMTESTS; i++) {
1018 for (j = 0; j < NUMMEMWORDS; j++) {
Wolfgang Denke8779842007-07-12 01:45:34 +02001019 /* printf("bank enabled base:%x\n", &membase[j]); */
Stefan Roese63dc63c2007-06-01 15:09:50 +02001020 membase[j] = test[i][j];
1021 ppcDcbf((unsigned long)&(membase[j]));
1022 }
1023
1024 for (j = 0; j < NUMMEMWORDS; j++) {
1025 if (membase[j] != test[i][j]) {
1026 ppcDcbf((unsigned long)&(membase[j]));
1027 return 0;
1028 }
1029 ppcDcbf((unsigned long)&(membase[j]));
1030 }
1031
1032 if (j < NUMMEMWORDS)
1033 return 0;
1034 }
1035
1036 /*
1037 * see if the rdclt value passed
1038 */
1039 if (i < NUMMEMTESTS)
1040 return 0;
1041 }
1042 }
1043
1044 return 1;
1045}
1046
1047static void program_tr1(void)
wdenkfe8c2802002-11-03 00:38:21 +00001048{
Stefan Roese326c9712005-08-01 16:41:48 +02001049 unsigned long tr0;
1050 unsigned long tr1;
1051 unsigned long cfg0;
1052 unsigned long ecc_temp;
1053 unsigned long dlycal;
1054 unsigned long dly_val;
Stefan Roese63dc63c2007-06-01 15:09:50 +02001055 unsigned long k;
Stefan Roese326c9712005-08-01 16:41:48 +02001056 unsigned long max_pass_length;
1057 unsigned long current_pass_length;
1058 unsigned long current_fail_length;
1059 unsigned long current_start;
1060 unsigned long rdclt;
1061 unsigned long rdclt_offset;
1062 long max_start;
1063 long max_end;
1064 long rdclt_average;
1065 unsigned char window_found;
1066 unsigned char fail_found;
1067 unsigned char pass_found;
Stefan Roeseedd73f22007-10-21 08:12:41 +02001068 PPC4xx_SYS_INFO sys_info;
wdenkfe8c2802002-11-03 00:38:21 +00001069
Stefan Roese326c9712005-08-01 16:41:48 +02001070 /*
1071 * get the board info
1072 */
1073 get_sys_info(&sys_info);
wdenkfe8c2802002-11-03 00:38:21 +00001074
Stefan Roese326c9712005-08-01 16:41:48 +02001075 /*
1076 * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits
1077 */
1078 mfsdram(mem_tr1, tr1);
1079 tr1 &= ~(SDRAM_TR1_RDSS_MASK | SDRAM_TR1_RDSL_MASK |
1080 SDRAM_TR1_RDCD_MASK | SDRAM_TR1_RDCT_MASK);
wdenkfe8c2802002-11-03 00:38:21 +00001081
Stefan Roese326c9712005-08-01 16:41:48 +02001082 mfsdram(mem_tr0, tr0);
1083 if (((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) &&
1084 (sys_info.freqPLB > 100000000)) {
1085 tr1 |= SDRAM_TR1_RDSS_TR2;
1086 tr1 |= SDRAM_TR1_RDSL_STAGE3;
1087 tr1 |= SDRAM_TR1_RDCD_RCD_1_2;
1088 } else {
1089 tr1 |= SDRAM_TR1_RDSS_TR1;
1090 tr1 |= SDRAM_TR1_RDSL_STAGE2;
1091 tr1 |= SDRAM_TR1_RDCD_RCD_0_0;
1092 }
wdenkfe8c2802002-11-03 00:38:21 +00001093
Stefan Roese326c9712005-08-01 16:41:48 +02001094 /*
1095 * save CFG0 ECC setting to a temporary variable and turn ECC off
1096 */
1097 mfsdram(mem_cfg0, cfg0);
1098 ecc_temp = cfg0 & SDRAM_CFG0_MCHK_MASK;
1099 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | SDRAM_CFG0_MCHK_NON);
wdenkfe8c2802002-11-03 00:38:21 +00001100
Stefan Roese326c9712005-08-01 16:41:48 +02001101 /*
1102 * get the delay line calibration register value
1103 */
1104 mfsdram(mem_dlycal, dlycal);
1105 dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
wdenkfe8c2802002-11-03 00:38:21 +00001106
Stefan Roese326c9712005-08-01 16:41:48 +02001107 max_pass_length = 0;
1108 max_start = 0;
1109 max_end = 0;
1110 current_pass_length = 0;
1111 current_fail_length = 0;
1112 current_start = 0;
1113 rdclt_offset = 0;
1114 window_found = FALSE;
1115 fail_found = FALSE;
1116 pass_found = FALSE;
Stefan Roese63dc63c2007-06-01 15:09:50 +02001117 debug("Starting memory test ");
1118
Stefan Roese326c9712005-08-01 16:41:48 +02001119 for (k = 0; k < NUMHALFCYCLES; k++) {
Stefan Roese63dc63c2007-06-01 15:09:50 +02001120 for (rdclt = 0; rdclt < dly_val; rdclt++) {
Stefan Roese326c9712005-08-01 16:41:48 +02001121 /*
1122 * Set the timing reg for the test.
1123 */
1124 mtsdram(mem_tr1, (tr1 | SDRAM_TR1_RDCT_ENCODE(rdclt)));
wdenkfe8c2802002-11-03 00:38:21 +00001125
Stefan Roese63dc63c2007-06-01 15:09:50 +02001126 if (short_mem_test()) {
Stefan Roese326c9712005-08-01 16:41:48 +02001127 if (fail_found == TRUE) {
1128 pass_found = TRUE;
1129 if (current_pass_length == 0) {
1130 current_start = rdclt_offset + rdclt;
1131 }
wdenkfe8c2802002-11-03 00:38:21 +00001132
Stefan Roese326c9712005-08-01 16:41:48 +02001133 current_fail_length = 0;
1134 current_pass_length++;
wdenkfe8c2802002-11-03 00:38:21 +00001135
Stefan Roese326c9712005-08-01 16:41:48 +02001136 if (current_pass_length > max_pass_length) {
1137 max_pass_length = current_pass_length;
1138 max_start = current_start;
1139 max_end = rdclt_offset + rdclt;
1140 }
1141 }
1142 } else {
1143 current_pass_length = 0;
1144 current_fail_length++;
wdenkfe8c2802002-11-03 00:38:21 +00001145
Stefan Roese326c9712005-08-01 16:41:48 +02001146 if (current_fail_length >= (dly_val>>2)) {
1147 if (fail_found == FALSE) {
1148 fail_found = TRUE;
1149 } else if (pass_found == TRUE) {
1150 window_found = TRUE;
1151 break;
1152 }
1153 }
1154 }
wdenk57b2d802003-06-27 21:31:46 +00001155 }
Stefan Roese63dc63c2007-06-01 15:09:50 +02001156 debug(".");
1157
Stefan Roese326c9712005-08-01 16:41:48 +02001158 if (window_found == TRUE) {
1159 break;
1160 }
wdenkfe8c2802002-11-03 00:38:21 +00001161
Stefan Roese326c9712005-08-01 16:41:48 +02001162 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
1163 rdclt_offset += dly_val;
1164 }
Stefan Roese63dc63c2007-06-01 15:09:50 +02001165 debug("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001166
Stefan Roese326c9712005-08-01 16:41:48 +02001167 /*
1168 * make sure we find the window
1169 */
1170 if (window_found == FALSE) {
1171 printf("ERROR: Cannot determine a common read delay.\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001172 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +02001173 }
wdenkfe8c2802002-11-03 00:38:21 +00001174
Stefan Roese326c9712005-08-01 16:41:48 +02001175 /*
1176 * restore the orignal ECC setting
1177 */
1178 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | ecc_temp);
wdenkfe8c2802002-11-03 00:38:21 +00001179
Stefan Roese326c9712005-08-01 16:41:48 +02001180 /*
1181 * set the SDRAM TR1 RDCD value
1182 */
1183 tr1 &= ~SDRAM_TR1_RDCD_MASK;
1184 if ((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) {
1185 tr1 |= SDRAM_TR1_RDCD_RCD_1_2;
1186 } else {
1187 tr1 |= SDRAM_TR1_RDCD_RCD_0_0;
1188 }
wdenkfe8c2802002-11-03 00:38:21 +00001189
Stefan Roese326c9712005-08-01 16:41:48 +02001190 /*
1191 * set the SDRAM TR1 RDCLT value
1192 */
1193 tr1 &= ~SDRAM_TR1_RDCT_MASK;
1194 while (max_end >= (dly_val << 1)) {
1195 max_end -= (dly_val << 1);
1196 max_start -= (dly_val << 1);
1197 }
wdenkfe8c2802002-11-03 00:38:21 +00001198
Stefan Roese326c9712005-08-01 16:41:48 +02001199 rdclt_average = ((max_start + max_end) >> 1);
1200 if (rdclt_average >= 0x60)
1201 while (1)
1202 ;
wdenkfe8c2802002-11-03 00:38:21 +00001203
Stefan Roese326c9712005-08-01 16:41:48 +02001204 if (rdclt_average < 0) {
1205 rdclt_average = 0;
1206 }
wdenkfe8c2802002-11-03 00:38:21 +00001207
Stefan Roese326c9712005-08-01 16:41:48 +02001208 if (rdclt_average >= dly_val) {
1209 rdclt_average -= dly_val;
1210 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
1211 }
1212 tr1 |= SDRAM_TR1_RDCT_ENCODE(rdclt_average);
wdenkfe8c2802002-11-03 00:38:21 +00001213
Stefan Roese63dc63c2007-06-01 15:09:50 +02001214 debug("tr1: %x\n", tr1);
1215
Stefan Roese326c9712005-08-01 16:41:48 +02001216 /*
1217 * program SDRAM Timing Register 1 TR1
1218 */
1219 mtsdram(mem_tr1, tr1);
wdenkfe8c2802002-11-03 00:38:21 +00001220}
1221
Stefan Roese63dc63c2007-06-01 15:09:50 +02001222static unsigned long program_bxcr(unsigned long *dimm_populated,
1223 unsigned char *iic0_dimm_addr,
1224 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +00001225{
Stefan Roese326c9712005-08-01 16:41:48 +02001226 unsigned long dimm_num;
Stefan Roese326c9712005-08-01 16:41:48 +02001227 unsigned long bank_base_addr;
Stefan Roese326c9712005-08-01 16:41:48 +02001228 unsigned long cr;
1229 unsigned long i;
Stefan Roesec45d1e32005-11-15 16:04:58 +01001230 unsigned long j;
Stefan Roese326c9712005-08-01 16:41:48 +02001231 unsigned long temp;
1232 unsigned char num_row_addr;
1233 unsigned char num_col_addr;
1234 unsigned char num_banks;
1235 unsigned char bank_size_id;
Stefan Roesec45d1e32005-11-15 16:04:58 +01001236 unsigned long ctrl_bank_num[MAXBANKS];
1237 unsigned long bx_cr_num;
1238 unsigned long largest_size_index;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +01001239 unsigned long largest_size;
1240 unsigned long current_size_index;
Stefan Roesec45d1e32005-11-15 16:04:58 +01001241 BANKPARMS bank_parms[MAXBXCR];
1242 unsigned long sorted_bank_num[MAXBXCR]; /* DDR Controller bank number table (sorted by size) */
1243 unsigned long sorted_bank_size[MAXBXCR]; /* DDR Controller bank size table (sorted by size)*/
wdenkfe8c2802002-11-03 00:38:21 +00001244
Stefan Roese326c9712005-08-01 16:41:48 +02001245 /*
1246 * Set the BxCR regs. First, wipe out the bank config registers.
1247 */
Stefan Roesec45d1e32005-11-15 16:04:58 +01001248 for (bx_cr_num = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1249 mtdcr(memcfga, mem_b0cr + (bx_cr_num << 2));
Stefan Roese326c9712005-08-01 16:41:48 +02001250 mtdcr(memcfgd, 0x00000000);
Stefan Roesec45d1e32005-11-15 16:04:58 +01001251 bank_parms[bx_cr_num].bank_size_bytes = 0;
Stefan Roese326c9712005-08-01 16:41:48 +02001252 }
Stefan Roesec45d1e32005-11-15 16:04:58 +01001253
1254#ifdef CONFIG_BAMBOO
1255 /*
1256 * This next section is hardware dependent and must be programmed
Stefan Roese63dc63c2007-06-01 15:09:50 +02001257 * to match the hardware. For bamboo, the following holds...
1258 * 1. SDRAM0_B0CR: Bank 0 of dimm 0 ctrl_bank_num : 0 (soldered onboard)
Stefan Roesec45d1e32005-11-15 16:04:58 +01001259 * 2. SDRAM0_B1CR: Bank 0 of dimm 1 ctrl_bank_num : 1
1260 * 3. SDRAM0_B2CR: Bank 1 of dimm 1 ctrl_bank_num : 1
1261 * 4. SDRAM0_B3CR: Bank 0 of dimm 2 ctrl_bank_num : 3
1262 * ctrl_bank_num corresponds to the first usable DDR controller bank number by DIMM
1263 */
1264 ctrl_bank_num[0] = 0;
1265 ctrl_bank_num[1] = 1;
1266 ctrl_bank_num[2] = 3;
1267#else
Stefan Roese63dc63c2007-06-01 15:09:50 +02001268 /*
1269 * Ocotea, Ebony and the other IBM/AMCC eval boards have
1270 * 2 DIMM slots with each max 2 banks
1271 */
Stefan Roesec45d1e32005-11-15 16:04:58 +01001272 ctrl_bank_num[0] = 0;
Stefan Roese63dc63c2007-06-01 15:09:50 +02001273 ctrl_bank_num[1] = 2;
Stefan Roese363330b2005-08-04 17:09:16 +02001274#endif
wdenkfe8c2802002-11-03 00:38:21 +00001275
Stefan Roese326c9712005-08-01 16:41:48 +02001276 /*
1277 * reset the bank_base address
1278 */
1279 bank_base_addr = CFG_SDRAM_BASE;
wdenkfe8c2802002-11-03 00:38:21 +00001280
Stefan Roese326c9712005-08-01 16:41:48 +02001281 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1282 if (dimm_populated[dimm_num] == TRUE) {
1283 num_row_addr = spd_read(iic0_dimm_addr[dimm_num], 3);
1284 num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
1285 num_banks = spd_read(iic0_dimm_addr[dimm_num], 5);
1286 bank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
Stefan Roese63dc63c2007-06-01 15:09:50 +02001287 debug("DIMM%d: row=%d col=%d banks=%d\n", dimm_num,
1288 num_row_addr, num_col_addr, num_banks);
wdenkfe8c2802002-11-03 00:38:21 +00001289
Stefan Roese326c9712005-08-01 16:41:48 +02001290 /*
1291 * Set the SDRAM0_BxCR regs
1292 */
1293 cr = 0;
Stefan Roese326c9712005-08-01 16:41:48 +02001294 switch (bank_size_id) {
1295 case 0x02:
1296 cr |= SDRAM_BXCR_SDSZ_8;
1297 break;
1298 case 0x04:
1299 cr |= SDRAM_BXCR_SDSZ_16;
1300 break;
1301 case 0x08:
1302 cr |= SDRAM_BXCR_SDSZ_32;
1303 break;
1304 case 0x10:
1305 cr |= SDRAM_BXCR_SDSZ_64;
1306 break;
1307 case 0x20:
1308 cr |= SDRAM_BXCR_SDSZ_128;
1309 break;
1310 case 0x40:
1311 cr |= SDRAM_BXCR_SDSZ_256;
1312 break;
1313 case 0x80:
1314 cr |= SDRAM_BXCR_SDSZ_512;
1315 break;
1316 default:
1317 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1318 dimm_num);
1319 printf("ERROR: Unsupported value for the banksize: %d.\n",
1320 bank_size_id);
1321 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001322 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +02001323 }
wdenkfe8c2802002-11-03 00:38:21 +00001324
Stefan Roese326c9712005-08-01 16:41:48 +02001325 switch (num_col_addr) {
1326 case 0x08:
1327 cr |= SDRAM_BXCR_SDAM_1;
1328 break;
1329 case 0x09:
1330 cr |= SDRAM_BXCR_SDAM_2;
1331 break;
1332 case 0x0A:
1333 cr |= SDRAM_BXCR_SDAM_3;
1334 break;
1335 case 0x0B:
1336 cr |= SDRAM_BXCR_SDAM_4;
1337 break;
1338 default:
1339 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1340 dimm_num);
1341 printf("ERROR: Unsupported value for number of "
1342 "column addresses: %d.\n", num_col_addr);
1343 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schocher68310b02007-06-25 19:11:37 +02001344 spd_ddr_init_hang ();
Stefan Roese326c9712005-08-01 16:41:48 +02001345 }
wdenkfe8c2802002-11-03 00:38:21 +00001346
Stefan Roese326c9712005-08-01 16:41:48 +02001347 /*
1348 * enable the bank
1349 */
1350 cr |= SDRAM_BXCR_SDBE;
wdenkfe8c2802002-11-03 00:38:21 +00001351
Wolfgang Denk70df7bc2007-06-22 23:59:00 +02001352 for (i = 0; i < num_banks; i++) {
Stefan Roese63dc63c2007-06-01 15:09:50 +02001353 bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes =
1354 (4 << 20) * bank_size_id;
1355 bank_parms[ctrl_bank_num[dimm_num]+i].cr = cr;
1356 debug("DIMM%d-bank %d (SDRAM0_B%dCR): bank_size_bytes=%d\n",
1357 dimm_num, i, ctrl_bank_num[dimm_num]+i,
1358 bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes);
Wolfgang Denk70df7bc2007-06-22 23:59:00 +02001359 }
Stefan Roesec45d1e32005-11-15 16:04:58 +01001360 }
1361 }
1362
1363 /* Initialize sort tables */
1364 for (i = 0; i < MAXBXCR; i++) {
1365 sorted_bank_num[i] = i;
1366 sorted_bank_size[i] = bank_parms[i].bank_size_bytes;
1367 }
1368
1369 for (i = 0; i < MAXBXCR-1; i++) {
1370 largest_size = sorted_bank_size[i];
1371 largest_size_index = 255;
1372
1373 /* Find the largest remaining value */
1374 for (j = i + 1; j < MAXBXCR; j++) {
1375 if (sorted_bank_size[j] > largest_size) {
1376 /* Save largest remaining value and its index */
1377 largest_size = sorted_bank_size[j];
1378 largest_size_index = j;
Stefan Roese326c9712005-08-01 16:41:48 +02001379 }
wdenk57b2d802003-06-27 21:31:46 +00001380 }
Stefan Roesec45d1e32005-11-15 16:04:58 +01001381
1382 if (largest_size_index != 255) {
1383 /* Swap the current and largest values */
1384 current_size_index = sorted_bank_num[largest_size_index];
1385 sorted_bank_size[largest_size_index] = sorted_bank_size[i];
1386 sorted_bank_size[i] = largest_size;
1387 sorted_bank_num[largest_size_index] = sorted_bank_num[i];
1388 sorted_bank_num[i] = current_size_index;
1389 }
1390 }
1391
1392 /* Set the SDRAM0_BxCR regs thanks to sort tables */
1393 for (bx_cr_num = 0, bank_base_addr = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1394 if (bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes) {
1395 mtdcr(memcfga, mem_b0cr + (sorted_bank_num[bx_cr_num] << 2));
1396 temp = mfdcr(memcfgd) & ~(SDRAM_BXCR_SDBA_MASK | SDRAM_BXCR_SDSZ_MASK |
1397 SDRAM_BXCR_SDAM_MASK | SDRAM_BXCR_SDBE);
1398 temp = temp | (bank_base_addr & SDRAM_BXCR_SDBA_MASK) |
1399 bank_parms[sorted_bank_num[bx_cr_num]].cr;
1400 mtdcr(memcfgd, temp);
1401 bank_base_addr += bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes;
Stefan Roese63dc63c2007-06-01 15:09:50 +02001402 debug("SDRAM0_B%dCR=0x%08lx\n", sorted_bank_num[bx_cr_num], temp);
Stefan Roesec45d1e32005-11-15 16:04:58 +01001403 }
wdenk57b2d802003-06-27 21:31:46 +00001404 }
wdenkfe8c2802002-11-03 00:38:21 +00001405
Stefan Roese326c9712005-08-01 16:41:48 +02001406 return(bank_base_addr);
wdenkfe8c2802002-11-03 00:38:21 +00001407}
1408
Stefan Roese63dc63c2007-06-01 15:09:50 +02001409#ifdef CONFIG_DDR_ECC
1410static void program_ecc(unsigned long num_bytes)
wdenkfe8c2802002-11-03 00:38:21 +00001411{
Stefan Roese326c9712005-08-01 16:41:48 +02001412 unsigned long bank_base_addr;
1413 unsigned long current_address;
1414 unsigned long end_address;
1415 unsigned long address_increment;
1416 unsigned long cfg0;
wdenkfe8c2802002-11-03 00:38:21 +00001417
Stefan Roese326c9712005-08-01 16:41:48 +02001418 /*
1419 * get Memory Controller Options 0 data
1420 */
1421 mfsdram(mem_cfg0, cfg0);
wdenkfe8c2802002-11-03 00:38:21 +00001422
Stefan Roese326c9712005-08-01 16:41:48 +02001423 /*
1424 * reset the bank_base address
1425 */
1426 bank_base_addr = CFG_SDRAM_BASE;
wdenkfe8c2802002-11-03 00:38:21 +00001427
Stefan Roese326c9712005-08-01 16:41:48 +02001428 if ((cfg0 & SDRAM_CFG0_MCHK_MASK) != SDRAM_CFG0_MCHK_NON) {
Stefan Roese63dc63c2007-06-01 15:09:50 +02001429 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | SDRAM_CFG0_MCHK_GEN);
wdenkfe8c2802002-11-03 00:38:21 +00001430
Stefan Roese63dc63c2007-06-01 15:09:50 +02001431 if ((cfg0 & SDRAM_CFG0_DMWD_MASK) == SDRAM_CFG0_DMWD_32)
Stefan Roese326c9712005-08-01 16:41:48 +02001432 address_increment = 4;
Stefan Roese63dc63c2007-06-01 15:09:50 +02001433 else
Stefan Roese326c9712005-08-01 16:41:48 +02001434 address_increment = 8;
wdenkfe8c2802002-11-03 00:38:21 +00001435
Stefan Roese326c9712005-08-01 16:41:48 +02001436 current_address = (unsigned long)(bank_base_addr);
1437 end_address = (unsigned long)(bank_base_addr) + num_bytes;
wdenkfe8c2802002-11-03 00:38:21 +00001438
Stefan Roese326c9712005-08-01 16:41:48 +02001439 while (current_address < end_address) {
1440 *((unsigned long*)current_address) = 0x00000000;
1441 current_address += address_increment;
1442 }
wdenkfe8c2802002-11-03 00:38:21 +00001443
Stefan Roese326c9712005-08-01 16:41:48 +02001444 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) |
1445 SDRAM_CFG0_MCHK_CHK);
1446 }
wdenkfe8c2802002-11-03 00:38:21 +00001447}
Stefan Roese63dc63c2007-06-01 15:09:50 +02001448#endif /* CONFIG_DDR_ECC */
wdenkfe8c2802002-11-03 00:38:21 +00001449#endif /* CONFIG_SPD_EEPROM */