blob: b9cf5cbfccaf0b8ebdfa4a32616c2ebd3674cbae [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Stefan Roese36d830c2007-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 *
wdenkdb2f721f2003-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 Denk0c8721a2005-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 Roesed2d43272007-06-01 15:09:50 +020023 * (C) Copyright 2005-2007
Stefan Roesec157d8e2005-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 Roesed2d43272007-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 Roesee2ebe692007-03-07 16:39:36 +010054#include <asm/mmu.h>
wdenkfe8c2802002-11-03 00:38:21 +000055
Stefan Roese36d830c2007-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 Denkf013dac2005-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 Denkf013dac2005-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 Denkf013dac2005-12-04 00:40:34 +010071#define ONE_BILLION 1000000000
Stefan Roesec157d8e2005-08-01 16:41:48 +020072
Heiko Schochera5d71e22007-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 Schocher566a4942007-06-22 19:11:54 +020081
wdenkfe8c2802002-11-03 00:38:21 +000082/*-----------------------------------------------------------------------------
Stefan Roesec157d8e2005-08-01 16:41:48 +020083 | Memory Controller Options 0
84 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +020099 | Memory Controller Options 1
100 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200105 | SDRAM DEVPOT Options
106 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200111 | SDRAM MCSTS Options
112 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200118 | SDRAM Refresh Timer Register
119 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Denkf013dac2005-12-04 00:40:34 +0100122#define sdram_HZ_to_ns(hertz) (1000000000/(hertz))
wdenkfe8c2802002-11-03 00:38:21 +0000123
124/*-----------------------------------------------------------------------------+
Stefan Roesec157d8e2005-08-01 16:41:48 +0200125 | SDRAM UABus Base Address Reg
126 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100127#define SDRAM_UABBA_UBBA_MASK 0x0000000F
wdenkfe8c2802002-11-03 00:38:21 +0000128
129/*-----------------------------------------------------------------------------+
Stefan Roesec157d8e2005-08-01 16:41:48 +0200130 | Memory Bank 0-7 configuration
131 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200149 | SDRAM TR0 Options
150 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200188 | SDRAM TR1 Options
189 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200209 | SDRAM WDDCTR Options
210 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200218 | SDRAM CLKTR Options
219 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200227 | SDRAM DLYCAL Options
228 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesec157d8e2005-08-01 16:41:48 +0200234 | General Definition
235 +-----------------------------------------------------------------------------*/
Wolfgang Denkf013dac2005-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 Roesee2ebe692007-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
Stefan Roeseea2e1422007-10-31 20:57:11 +0100254 * CONFIG_4xx_DCACHE in the board config file and this code should setup
Stefan Roesee2ebe692007-03-07 16:39:36 +0100255 * everything correctly.
256 */
Stefan Roeseea2e1422007-10-31 20:57:11 +0100257#ifdef CONFIG_4xx_DCACHE
Stefan Roesee2ebe692007-03-07 16:39:36 +0100258#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 Roesefd49bf02005-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 OBriend2f68002007-07-31 10:24:56 +0200272extern const unsigned char cfg_simulate_spd_eeprom[128];
Stefan Roesefd49bf02005-11-15 16:04:58 +0100273#endif
wdenkfe8c2802002-11-03 00:38:21 +0000274
Stefan Roesed2d43272007-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 Roesed2d43272007-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 Roesed2d43272007-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 Denk0c8721a2005-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 Roesec157d8e2005-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 Denkf013dac2005-12-04 00:40:34 +0100322 unsigned long num_dimm_banks; /* on board dimm banks */
wdenkfe8c2802002-11-03 00:38:21 +0000323
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roeseea2e1422007-10-31 20:57:11 +0100348#if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200349 /*
350 * Soft-reset SDRAM controller.
351 */
352 mtsdr(sdr_srst, SDR0_SRST_DMC);
353 mtsdr(sdr_srst, 0x00000000);
wdenk63153492005-04-03 20:55:38 +0000354#endif
355
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesee2ebe692007-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 Roesedbca2082007-06-14 11:14:32 +0200384 program_tlb(0, 0, total_size, MY_TLB_WORD2_I_ENABLE);
Stefan Roesee2ebe692007-03-07 16:39:36 +0100385#endif
386
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +0200408 if ((mcsts & SDRAM_MCSTS_MRSC) != 0)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200409 break;
wdenk8bde7f72003-06-27 21:31:46 +0000410 }
wdenkfe8c2802002-11-03 00:38:21 +0000411
Stefan Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +0200417#ifdef CONFIG_DDR_ECC
Stefan Roesec157d8e2005-08-01 16:41:48 +0200418 /*
Stefan Roesed2d43272007-06-01 15:09:50 +0200419 * If ecc is enabled, initialize the parity bits.
Stefan Roesec157d8e2005-08-01 16:41:48 +0200420 */
Stefan Roesed2d43272007-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 Roesed2d43272007-06-01 15:09:50 +0200427static unsigned char spd_read(uchar chip, uint addr)
Stefan Roesefd49bf02005-11-15 16:04:58 +0100428{
wdenkfe8c2802002-11-03 00:38:21 +0000429 unsigned char data[2];
430
Stefan Roesefd49bf02005-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 Roesec157d8e2005-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 Roesed2d43272007-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +0200469 debug("DIMM slot %lu: populated\n", dimm_num);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200470 } else {
471 dimm_populated[dimm_num] = FALSE;
Stefan Roesed2d43272007-06-01 15:09:50 +0200472 debug("DIMM slot %lu: Not populated\n", dimm_num);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200473 }
wdenk8bde7f72003-06-27 21:31:46 +0000474 }
wdenkfe8c2802002-11-03 00:38:21 +0000475
Stefan Roesec157d8e2005-08-01 16:41:48 +0200476 if (dimm_found == FALSE) {
477 printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
Heiko Schochera5d71e22007-06-25 19:11:37 +0200478 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200479 }
wdenkfe8c2802002-11-03 00:38:21 +0000480}
481
Stefan Roesed2d43272007-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 Roesec157d8e2005-08-01 16:41:48 +0200486 unsigned long dimm_num;
487 unsigned char dimm_type;
wdenkfe8c2802002-11-03 00:38:21 +0000488
Stefan Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +0200494 debug("DIMM slot %lu: DDR SDRAM detected\n", dimm_num);
Stefan Roesec157d8e2005-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 Schochera5d71e22007-06-25 19:11:37 +0200501 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200502 break;
503 }
504 }
wdenk8bde7f72003-06-27 21:31:46 +0000505 }
wdenkfe8c2802002-11-03 00:38:21 +0000506}
507
Stefan Roesed2d43272007-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 Roesec157d8e2005-08-01 16:41:48 +0200512 unsigned long dimm_num;
513 unsigned long voltage_type;
wdenkfe8c2802002-11-03 00:38:21 +0000514
Stefan Roesec157d8e2005-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 Schochera5d71e22007-06-25 19:11:37 +0200521 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200522 } else {
Stefan Roesed2d43272007-06-01 15:09:50 +0200523 debug("DIMM %lu voltage level supported.\n", dimm_num);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200524 }
525 break;
526 }
wdenk8bde7f72003-06-27 21:31:46 +0000527 }
wdenkfe8c2802002-11-03 00:38:21 +0000528}
529
Stefan Roesed2d43272007-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Schochera5d71e22007-06-25 19:11:37 +0200592 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200593 }
594 break;
595 }
wdenk8bde7f72003-06-27 21:31:46 +0000596 }
wdenkfe8c2802002-11-03 00:38:21 +0000597
Stefan Roesec157d8e2005-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 Roesec157d8e2005-08-01 16:41:48 +0200607 /*
Stefan Roesea2c95a72006-07-28 18:34:58 +0200608 * program Page Management Unit (0 == enabled)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200609 */
Stefan Roesea2c95a72006-07-28 18:34:58 +0200610 cfg0 &= ~SDRAM_CFG0_PMUD;
wdenkfe8c2802002-11-03 00:38:21 +0000611
Stefan Roesec157d8e2005-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 Roesed2d43272007-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 Roesec157d8e2005-08-01 16:41:48 +0200624 unsigned long cfg1;
625 mfsdram(mem_cfg1, cfg1);
wdenkfe8c2802002-11-03 00:38:21 +0000626
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesed2d43272007-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 Roesec157d8e2005-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 Roese087dfdb2007-10-21 08:12:41 +0200648 PPC4xx_SYS_INFO sys_info;
wdenkfe8c2802002-11-03 00:38:21 +0000649
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-08-01 16:41:48 +0200685 break;
686 }
wdenk8bde7f72003-06-27 21:31:46 +0000687 }
wdenkfe8c2802002-11-03 00:38:21 +0000688
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesed2d43272007-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 Roesec157d8e2005-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 Roese087dfdb2007-10-21 08:12:41 +0200724 PPC4xx_SYS_INFO sys_info;
Stefan Roesec157d8e2005-08-01 16:41:48 +0200725 unsigned long residue;
wdenkfe8c2802002-11-03 00:38:21 +0000726
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Denkf013dac2005-12-04 00:40:34 +0100759 t_rp_ns = spd_read(iic0_dimm_addr[dimm_num], 27) >> 2;
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 }
776
777 if ((tcyc_reg & 0x0F) >= 10) {
778 printf("ERROR: Tcyc incorrect for DIMM in slot %lu\n",
779 dimm_num);
Heiko Schochera5d71e22007-06-25 19:11:37 +0200780 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200781 }
782
783 cycle_time_ns_x_10[cas_index] =
784 (((tcyc_reg & 0xF0) >> 4) * 10) + (tcyc_reg & 0x0F);
785 }
786
787 cas_index = 0;
788
789 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 }
796
797 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 }
806
807 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 }
816
817 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 }
826
827 break;
wdenk8bde7f72003-06-27 21:31:46 +0000828 }
wdenk8bde7f72003-06-27 21:31:46 +0000829 }
wdenkfe8c2802002-11-03 00:38:21 +0000830
Stefan Roesec157d8e2005-08-01 16:41:48 +0200831 /*
832 * Program SD_WR and SD_WCSBC fields
833 */
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100834 tr0 |= SDRAM_TR0_SDWR_2_CLK; /* Write Recovery: 2 CLK */
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Schochera5d71e22007-06-25 19:11:37 +0200860 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +0200861 }
wdenkfe8c2802002-11-03 00:38:21 +0000862
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +0200980 debug("tr0: %x\n", tr0);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200981 mtsdram(mem_tr0, tr0);
wdenkfe8c2802002-11-03 00:38:21 +0000982}
983
Stefan Roesed2d43272007-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 Denk239f05e2007-07-12 01:45:34 +02001019 /* printf("bank enabled base:%x\n", &membase[j]); */
Stefan Roesed2d43272007-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 Roesec157d8e2005-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 Roesed2d43272007-06-01 15:09:50 +02001055 unsigned long k;
Stefan Roesec157d8e2005-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 Roese087dfdb2007-10-21 08:12:41 +02001068 PPC4xx_SYS_INFO sys_info;
wdenkfe8c2802002-11-03 00:38:21 +00001069
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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;
wdenk8bde7f72003-06-27 21:31:46 +00001092 }
wdenkfe8c2802002-11-03 00:38:21 +00001093
Stefan Roesec157d8e2005-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);
1100
1101 /*
1102 * get the delay line calibration register value
1103 */
1104 mfsdram(mem_dlycal, dlycal);
1105 dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
1106
1107 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 Roesed2d43272007-06-01 15:09:50 +02001117 debug("Starting memory test ");
1118
Stefan Roesec157d8e2005-08-01 16:41:48 +02001119 for (k = 0; k < NUMHALFCYCLES; k++) {
Stefan Roesed2d43272007-06-01 15:09:50 +02001120 for (rdclt = 0; rdclt < dly_val; rdclt++) {
Stefan Roesec157d8e2005-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)));
1125
Stefan Roesed2d43272007-06-01 15:09:50 +02001126 if (short_mem_test()) {
Stefan Roesec157d8e2005-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 }
1132
1133 current_fail_length = 0;
1134 current_pass_length++;
1135
1136 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++;
1145
1146 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 }
1155 }
Stefan Roesed2d43272007-06-01 15:09:50 +02001156 debug(".");
1157
Stefan Roesec157d8e2005-08-01 16:41:48 +02001158 if (window_found == TRUE) {
1159 break;
1160 }
1161
1162 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
1163 rdclt_offset += dly_val;
1164 }
Stefan Roesed2d43272007-06-01 15:09:50 +02001165 debug("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001166
Stefan Roesec157d8e2005-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 Schochera5d71e22007-06-25 19:11:37 +02001172 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +02001173 }
wdenkfe8c2802002-11-03 00:38:21 +00001174
Stefan Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-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 Roesec157d8e2005-08-01 16:41:48 +02001199 rdclt_average = ((max_start + max_end) >> 1);
wdenkfe8c2802002-11-03 00:38:21 +00001200
Stefan Roesec157d8e2005-08-01 16:41:48 +02001201 if (rdclt_average < 0) {
1202 rdclt_average = 0;
1203 }
wdenkfe8c2802002-11-03 00:38:21 +00001204
Stefan Roesec157d8e2005-08-01 16:41:48 +02001205 if (rdclt_average >= dly_val) {
1206 rdclt_average -= dly_val;
1207 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
1208 }
1209 tr1 |= SDRAM_TR1_RDCT_ENCODE(rdclt_average);
wdenkfe8c2802002-11-03 00:38:21 +00001210
Stefan Roesed2d43272007-06-01 15:09:50 +02001211 debug("tr1: %x\n", tr1);
1212
Stefan Roesec157d8e2005-08-01 16:41:48 +02001213 /*
1214 * program SDRAM Timing Register 1 TR1
1215 */
1216 mtsdram(mem_tr1, tr1);
wdenkfe8c2802002-11-03 00:38:21 +00001217}
1218
Stefan Roesed2d43272007-06-01 15:09:50 +02001219static unsigned long program_bxcr(unsigned long *dimm_populated,
1220 unsigned char *iic0_dimm_addr,
1221 unsigned long num_dimm_banks)
wdenkfe8c2802002-11-03 00:38:21 +00001222{
Stefan Roesec157d8e2005-08-01 16:41:48 +02001223 unsigned long dimm_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001224 unsigned long bank_base_addr;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001225 unsigned long cr;
1226 unsigned long i;
Stefan Roesefd49bf02005-11-15 16:04:58 +01001227 unsigned long j;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001228 unsigned long temp;
1229 unsigned char num_row_addr;
1230 unsigned char num_col_addr;
1231 unsigned char num_banks;
1232 unsigned char bank_size_id;
Stefan Roesefd49bf02005-11-15 16:04:58 +01001233 unsigned long ctrl_bank_num[MAXBANKS];
1234 unsigned long bx_cr_num;
1235 unsigned long largest_size_index;
Wolfgang Denkf013dac2005-12-04 00:40:34 +01001236 unsigned long largest_size;
1237 unsigned long current_size_index;
Stefan Roesefd49bf02005-11-15 16:04:58 +01001238 BANKPARMS bank_parms[MAXBXCR];
1239 unsigned long sorted_bank_num[MAXBXCR]; /* DDR Controller bank number table (sorted by size) */
1240 unsigned long sorted_bank_size[MAXBXCR]; /* DDR Controller bank size table (sorted by size)*/
wdenkfe8c2802002-11-03 00:38:21 +00001241
Stefan Roesec157d8e2005-08-01 16:41:48 +02001242 /*
1243 * Set the BxCR regs. First, wipe out the bank config registers.
1244 */
Stefan Roesefd49bf02005-11-15 16:04:58 +01001245 for (bx_cr_num = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1246 mtdcr(memcfga, mem_b0cr + (bx_cr_num << 2));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001247 mtdcr(memcfgd, 0x00000000);
Stefan Roesefd49bf02005-11-15 16:04:58 +01001248 bank_parms[bx_cr_num].bank_size_bytes = 0;
wdenk8bde7f72003-06-27 21:31:46 +00001249 }
Stefan Roesefd49bf02005-11-15 16:04:58 +01001250
1251#ifdef CONFIG_BAMBOO
1252 /*
1253 * This next section is hardware dependent and must be programmed
Stefan Roesed2d43272007-06-01 15:09:50 +02001254 * to match the hardware. For bamboo, the following holds...
1255 * 1. SDRAM0_B0CR: Bank 0 of dimm 0 ctrl_bank_num : 0 (soldered onboard)
Stefan Roesefd49bf02005-11-15 16:04:58 +01001256 * 2. SDRAM0_B1CR: Bank 0 of dimm 1 ctrl_bank_num : 1
1257 * 3. SDRAM0_B2CR: Bank 1 of dimm 1 ctrl_bank_num : 1
1258 * 4. SDRAM0_B3CR: Bank 0 of dimm 2 ctrl_bank_num : 3
1259 * ctrl_bank_num corresponds to the first usable DDR controller bank number by DIMM
1260 */
1261 ctrl_bank_num[0] = 0;
1262 ctrl_bank_num[1] = 1;
1263 ctrl_bank_num[2] = 3;
1264#else
Stefan Roesed2d43272007-06-01 15:09:50 +02001265 /*
1266 * Ocotea, Ebony and the other IBM/AMCC eval boards have
1267 * 2 DIMM slots with each max 2 banks
1268 */
Stefan Roesefd49bf02005-11-15 16:04:58 +01001269 ctrl_bank_num[0] = 0;
Stefan Roesed2d43272007-06-01 15:09:50 +02001270 ctrl_bank_num[1] = 2;
Stefan Roese17f50f222005-08-04 17:09:16 +02001271#endif
wdenkfe8c2802002-11-03 00:38:21 +00001272
Stefan Roesec157d8e2005-08-01 16:41:48 +02001273 /*
1274 * reset the bank_base address
1275 */
1276 bank_base_addr = CFG_SDRAM_BASE;
1277
1278 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1279 if (dimm_populated[dimm_num] == TRUE) {
1280 num_row_addr = spd_read(iic0_dimm_addr[dimm_num], 3);
1281 num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
1282 num_banks = spd_read(iic0_dimm_addr[dimm_num], 5);
1283 bank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
Stefan Roesed2d43272007-06-01 15:09:50 +02001284 debug("DIMM%d: row=%d col=%d banks=%d\n", dimm_num,
1285 num_row_addr, num_col_addr, num_banks);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001286
1287 /*
1288 * Set the SDRAM0_BxCR regs
1289 */
1290 cr = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001291 switch (bank_size_id) {
1292 case 0x02:
1293 cr |= SDRAM_BXCR_SDSZ_8;
1294 break;
1295 case 0x04:
1296 cr |= SDRAM_BXCR_SDSZ_16;
1297 break;
1298 case 0x08:
1299 cr |= SDRAM_BXCR_SDSZ_32;
1300 break;
1301 case 0x10:
1302 cr |= SDRAM_BXCR_SDSZ_64;
1303 break;
1304 case 0x20:
1305 cr |= SDRAM_BXCR_SDSZ_128;
1306 break;
1307 case 0x40:
1308 cr |= SDRAM_BXCR_SDSZ_256;
1309 break;
1310 case 0x80:
1311 cr |= SDRAM_BXCR_SDSZ_512;
1312 break;
1313 default:
1314 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1315 dimm_num);
1316 printf("ERROR: Unsupported value for the banksize: %d.\n",
1317 bank_size_id);
1318 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schochera5d71e22007-06-25 19:11:37 +02001319 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +02001320 }
1321
1322 switch (num_col_addr) {
1323 case 0x08:
1324 cr |= SDRAM_BXCR_SDAM_1;
1325 break;
1326 case 0x09:
1327 cr |= SDRAM_BXCR_SDAM_2;
1328 break;
1329 case 0x0A:
1330 cr |= SDRAM_BXCR_SDAM_3;
1331 break;
1332 case 0x0B:
1333 cr |= SDRAM_BXCR_SDAM_4;
1334 break;
1335 default:
1336 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1337 dimm_num);
1338 printf("ERROR: Unsupported value for number of "
1339 "column addresses: %d.\n", num_col_addr);
1340 printf("Replace the DIMM module with a supported DIMM.\n\n");
Heiko Schochera5d71e22007-06-25 19:11:37 +02001341 spd_ddr_init_hang ();
Stefan Roesec157d8e2005-08-01 16:41:48 +02001342 }
1343
1344 /*
1345 * enable the bank
1346 */
1347 cr |= SDRAM_BXCR_SDBE;
1348
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001349 for (i = 0; i < num_banks; i++) {
Stefan Roesed2d43272007-06-01 15:09:50 +02001350 bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes =
1351 (4 << 20) * bank_size_id;
1352 bank_parms[ctrl_bank_num[dimm_num]+i].cr = cr;
1353 debug("DIMM%d-bank %d (SDRAM0_B%dCR): bank_size_bytes=%d\n",
1354 dimm_num, i, ctrl_bank_num[dimm_num]+i,
1355 bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes);
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001356 }
Stefan Roesec157d8e2005-08-01 16:41:48 +02001357 }
1358 }
1359
Stefan Roesefd49bf02005-11-15 16:04:58 +01001360 /* Initialize sort tables */
1361 for (i = 0; i < MAXBXCR; i++) {
1362 sorted_bank_num[i] = i;
1363 sorted_bank_size[i] = bank_parms[i].bank_size_bytes;
1364 }
1365
1366 for (i = 0; i < MAXBXCR-1; i++) {
1367 largest_size = sorted_bank_size[i];
1368 largest_size_index = 255;
1369
1370 /* Find the largest remaining value */
1371 for (j = i + 1; j < MAXBXCR; j++) {
1372 if (sorted_bank_size[j] > largest_size) {
1373 /* Save largest remaining value and its index */
1374 largest_size = sorted_bank_size[j];
1375 largest_size_index = j;
1376 }
1377 }
1378
1379 if (largest_size_index != 255) {
1380 /* Swap the current and largest values */
1381 current_size_index = sorted_bank_num[largest_size_index];
1382 sorted_bank_size[largest_size_index] = sorted_bank_size[i];
1383 sorted_bank_size[i] = largest_size;
1384 sorted_bank_num[largest_size_index] = sorted_bank_num[i];
1385 sorted_bank_num[i] = current_size_index;
1386 }
1387 }
1388
1389 /* Set the SDRAM0_BxCR regs thanks to sort tables */
1390 for (bx_cr_num = 0, bank_base_addr = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1391 if (bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes) {
1392 mtdcr(memcfga, mem_b0cr + (sorted_bank_num[bx_cr_num] << 2));
1393 temp = mfdcr(memcfgd) & ~(SDRAM_BXCR_SDBA_MASK | SDRAM_BXCR_SDSZ_MASK |
1394 SDRAM_BXCR_SDAM_MASK | SDRAM_BXCR_SDBE);
1395 temp = temp | (bank_base_addr & SDRAM_BXCR_SDBA_MASK) |
1396 bank_parms[sorted_bank_num[bx_cr_num]].cr;
1397 mtdcr(memcfgd, temp);
1398 bank_base_addr += bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes;
Stefan Roesed2d43272007-06-01 15:09:50 +02001399 debug("SDRAM0_B%dCR=0x%08lx\n", sorted_bank_num[bx_cr_num], temp);
Stefan Roesefd49bf02005-11-15 16:04:58 +01001400 }
1401 }
1402
Stefan Roesec157d8e2005-08-01 16:41:48 +02001403 return(bank_base_addr);
wdenkfe8c2802002-11-03 00:38:21 +00001404}
1405
Stefan Roesed2d43272007-06-01 15:09:50 +02001406#ifdef CONFIG_DDR_ECC
1407static void program_ecc(unsigned long num_bytes)
wdenkfe8c2802002-11-03 00:38:21 +00001408{
Stefan Roesec157d8e2005-08-01 16:41:48 +02001409 unsigned long bank_base_addr;
1410 unsigned long current_address;
1411 unsigned long end_address;
1412 unsigned long address_increment;
1413 unsigned long cfg0;
wdenkfe8c2802002-11-03 00:38:21 +00001414
Stefan Roesec157d8e2005-08-01 16:41:48 +02001415 /*
1416 * get Memory Controller Options 0 data
1417 */
1418 mfsdram(mem_cfg0, cfg0);
wdenkfe8c2802002-11-03 00:38:21 +00001419
Stefan Roesec157d8e2005-08-01 16:41:48 +02001420 /*
1421 * reset the bank_base address
1422 */
1423 bank_base_addr = CFG_SDRAM_BASE;
wdenkfe8c2802002-11-03 00:38:21 +00001424
Stefan Roesec157d8e2005-08-01 16:41:48 +02001425 if ((cfg0 & SDRAM_CFG0_MCHK_MASK) != SDRAM_CFG0_MCHK_NON) {
Stefan Roesed2d43272007-06-01 15:09:50 +02001426 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | SDRAM_CFG0_MCHK_GEN);
wdenkfe8c2802002-11-03 00:38:21 +00001427
Stefan Roesed2d43272007-06-01 15:09:50 +02001428 if ((cfg0 & SDRAM_CFG0_DMWD_MASK) == SDRAM_CFG0_DMWD_32)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001429 address_increment = 4;
Stefan Roesed2d43272007-06-01 15:09:50 +02001430 else
Stefan Roesec157d8e2005-08-01 16:41:48 +02001431 address_increment = 8;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001432
1433 current_address = (unsigned long)(bank_base_addr);
1434 end_address = (unsigned long)(bank_base_addr) + num_bytes;
1435
1436 while (current_address < end_address) {
1437 *((unsigned long*)current_address) = 0x00000000;
1438 current_address += address_increment;
1439 }
1440
1441 mtsdram(mem_cfg0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) |
1442 SDRAM_CFG0_MCHK_CHK);
wdenk8bde7f72003-06-27 21:31:46 +00001443 }
wdenkfe8c2802002-11-03 00:38:21 +00001444}
Stefan Roesed2d43272007-06-01 15:09:50 +02001445#endif /* CONFIG_DDR_ECC */
wdenkfe8c2802002-11-03 00:38:21 +00001446#endif /* CONFIG_SPD_EEPROM */