blob: af3a98a42fb4255fd518b6fbd950d19ec2b0c2c0 [file] [log] [blame]
wdenk858b1a62002-09-30 16:12:23 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 *
24 * TODO: clean-up
25 */
26
27/*
28 * How do I program the SDRAM Timing Register (SDRAM0_TR) for a specific SDRAM or DIMM?
29 *
30 * As an example, consider a case where PC133 memory with CAS Latency equal to 2 is being
31 * used with a 200MHz 405GP. For a typical 128Mb, PC133 SDRAM, the relevant minimum
32 * parameters from the datasheet are:
33 * Tclk = 7.5ns (CL = 2)
34 * Trp = 15ns
35 * Trc = 60ns
36 * Trcd = 15ns
37 * Trfc = 66ns
38 *
39 * If we are operating the 405GP with the MemClk output frequency set to 100 MHZ, the clock
40 * period is 10ns and the parameters needed for the Timing Register are:
41 * CASL = CL = 2 clock cycles
42 * PTA = Trp = 15ns / 10ns = 2 clock cycles
43 * CTP = Trc - Trcd - Trp = (60ns - 15ns - 15ns) / 10ns= 3 clock cycles
44 * LDF = 2 clock cycles (but can be extended to meet board-level timing)
45 * RFTA = Trfc = 66ns / 10ns= 7 clock cycles
46 * RCD = Trcd = 15ns / 10ns= 2 clock cycles
47 *
48 * The actual bit settings in the register would be:
49 *
50 * CASL = 0b01
51 * PTA = 0b01
52 * CTP = 0b10
53 * LDF = 0b01
54 * RFTA = 0b011
55 * RCD = 0b01
56 *
57 * If Trfc is not specified in the datasheet for PC100 or PC133 memory, set RFTA = Trc
58 * instead. Figure 24 in the PC SDRAM Specification Rev. 1.7 shows refresh to active delay
59 * defined as Trc rather than Trfc.
60 * When using DIMM modules, most but not all of the required timing parameters can be read
61 * from the Serial Presence Detect (SPD) EEPROM on the module. Specifically, Trc and Trfc
62 * are not available from the EEPROM
63 */
64
65#include <common.h>
66#include "mip405.h"
67#include <asm/processor.h>
Stefan Roesed07117e2007-02-20 10:27:08 +010068#include <4xx_i2c.h>
wdenk858b1a62002-09-30 16:12:23 +000069#include <miiphy.h>
70#include "../common/common_util.h"
Jean-Christophe PLAGNIOL-VILLARDd4303af2009-05-16 12:14:56 +020071#include <stdio_dev.h>
wdenk858b1a62002-09-30 16:12:23 +000072#include <i2c.h>
wdenk874ac262003-07-24 23:38:38 +000073#include <rtc.h>
Wolfgang Denk6405a152006-03-31 18:32:53 +020074
75DECLARE_GLOBAL_DATA_PTR;
76
wdenk858b1a62002-09-30 16:12:23 +000077#undef SDRAM_DEBUG
wdenke39c2842003-06-04 15:05:30 +000078#define ENABLE_ECC /* for ecc boards */
wdenk858b1a62002-09-30 16:12:23 +000079#define FALSE 0
80#define TRUE 1
81
82/* stdlib.h causes some compatibility problems; should fixe these! -- wd */
83#ifndef __ldiv_t_defined
84typedef struct {
85 long int quot; /* Quotient */
86 long int rem; /* Remainder */
87} ldiv_t;
88extern ldiv_t ldiv (long int __numer, long int __denom);
89# define __ldiv_t_defined 1
90#endif
91
92
Wolfgang Denka1be4762008-05-20 16:00:29 +020093#define PLD_PART_REG PER_PLD_ADDR + 0
94#define PLD_VERS_REG PER_PLD_ADDR + 1
95#define PLD_BOARD_CFG_REG PER_PLD_ADDR + 2
96#define PLD_IRQ_REG PER_PLD_ADDR + 3
97#define PLD_COM_MODE_REG PER_PLD_ADDR + 4
98#define PLD_EXT_CONF_REG PER_PLD_ADDR + 5
wdenk858b1a62002-09-30 16:12:23 +000099
100#define MEGA_BYTE (1024*1024)
101
102typedef struct {
103 unsigned char boardtype; /* Board revision and Population Options */
104 unsigned char cal; /* cas Latency (will be programmend as cal-1) */
105 unsigned char trp; /* datain27 in clocks */
106 unsigned char trcd; /* datain29 in clocks */
107 unsigned char tras; /* datain30 in clocks */
108 unsigned char tctp; /* tras - trcd in clocks */
109 unsigned char am; /* Address Mod (will be programmed as am-1) */
110 unsigned char sz; /* log binary => Size = (4MByte<<sz) 5 = 128, 4 = 64, 3 = 32, 2 = 16, 1=8 */
111 unsigned char ecc; /* if true, ecc is enabled */
112} sdram_t;
wdenke39c2842003-06-04 15:05:30 +0000113#if defined(CONFIG_MIP405T)
114const sdram_t sdram_table[] = {
wdenk874ac262003-07-24 23:38:38 +0000115 { 0x0F, /* MIP405T Rev A, 64MByte -1 Board */
wdenke39c2842003-06-04 15:05:30 +0000116 3, /* Case Latenty = 3 */
117 3, /* trp 20ns / 7.5 ns datain[27] */
118 3, /* trcd 20ns /7.5 ns (datain[29]) */
119 6, /* tras 44ns /7.5 ns (datain[30]) */
120 4, /* tcpt 44 - 20ns = 24ns */
wdenk874ac262003-07-24 23:38:38 +0000121 2, /* Address Mode = 2 (12x9x4) */
122 3, /* size value (32MByte) */
wdenke39c2842003-06-04 15:05:30 +0000123 0}, /* ECC disabled */
124 { 0xff, /* terminator */
125 0xff,
126 0xff,
127 0xff,
128 0xff,
129 0xff,
130 0xff,
131 0xff }
132};
133#else
wdenk858b1a62002-09-30 16:12:23 +0000134const sdram_t sdram_table[] = {
135 { 0x0f, /* Rev A, 128MByte -1 Board */
136 3, /* Case Latenty = 3 */
137 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk7d076412003-05-23 11:38:58 +0000138 3, /* trcd 20ns /7.5 ns (datain[29]) */
139 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000140 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000141 3, /* Address Mode = 3 */
wdenk858b1a62002-09-30 16:12:23 +0000142 5, /* size value */
143 1}, /* ECC enabled */
144 { 0x07, /* Rev A, 64MByte -2 Board */
145 3, /* Case Latenty = 3 */
146 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk7d076412003-05-23 11:38:58 +0000147 3, /* trcd 20ns /7.5 ns (datain[29]) */
148 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000149 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000150 2, /* Address Mode = 2 */
wdenk858b1a62002-09-30 16:12:23 +0000151 4, /* size value */
152 1}, /* ECC enabled */
wdenkb02744a2003-04-05 00:53:31 +0000153 { 0x03, /* Rev A, 128MByte -4 Board */
154 3, /* Case Latenty = 3 */
155 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk7d076412003-05-23 11:38:58 +0000156 3, /* trcd 20ns /7.5 ns (datain[29]) */
157 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenkb02744a2003-04-05 00:53:31 +0000158 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000159 3, /* Address Mode = 3 */
wdenkb02744a2003-04-05 00:53:31 +0000160 5, /* size value */
161 1}, /* ECC enabled */
wdenk7d076412003-05-23 11:38:58 +0000162 { 0x1f, /* Rev B, 128MByte -3 Board */
163 3, /* Case Latenty = 3 */
164 3, /* trp 20ns / 7.5 ns datain[27] */
165 3, /* trcd 20ns /7.5 ns (datain[29]) */
166 6, /* tras 44ns /7.5 ns (datain[30]) */
167 4, /* tcpt 44 - 20ns = 24ns */
168 3, /* Address Mode = 3 */
169 5, /* size value */
170 1}, /* ECC enabled */
wdenk60164a82003-10-08 23:26:14 +0000171 { 0x2f, /* Rev C, 128MByte -3 Board */
172 3, /* Case Latenty = 3 */
173 3, /* trp 20ns / 7.5 ns datain[27] */
174 3, /* trcd 20ns /7.5 ns (datain[29]) */
175 6, /* tras 44ns /7.5 ns (datain[30]) */
176 4, /* tcpt 44 - 20ns = 24ns */
177 3, /* Address Mode = 3 */
178 5, /* size value */
179 1}, /* ECC enabled */
wdenk858b1a62002-09-30 16:12:23 +0000180 { 0xff, /* terminator */
181 0xff,
182 0xff,
183 0xff,
184 0xff,
185 0xff,
186 0xff,
187 0xff }
188};
wdenke39c2842003-06-04 15:05:30 +0000189#endif /*CONFIG_MIP405T */
wdenk858b1a62002-09-30 16:12:23 +0000190void SDRAM_err (const char *s)
191{
192#ifndef SDRAM_DEBUG
wdenk858b1a62002-09-30 16:12:23 +0000193 (void) get_clocks ();
194 gd->baudrate = 9600;
195 serial_init ();
196#endif
197 serial_puts ("\n");
198 serial_puts (s);
199 serial_puts ("\n enable SDRAM_DEBUG for more info\n");
200 for (;;);
201}
202
203
204unsigned char get_board_revcfg (void)
205{
206 out8 (PER_BOARD_ADDR, 0);
207 return (in8 (PER_BOARD_ADDR));
208}
209
210
211#ifdef SDRAM_DEBUG
212
213void write_hex (unsigned char i)
214{
215 char cc;
216
217 cc = i >> 4;
218 cc &= 0xf;
219 if (cc > 9)
220 serial_putc (cc + 55);
221 else
222 serial_putc (cc + 48);
223 cc = i & 0xf;
224 if (cc > 9)
225 serial_putc (cc + 55);
226 else
227 serial_putc (cc + 48);
228}
229
230void write_4hex (unsigned long val)
231{
232 write_hex ((unsigned char) (val >> 24));
233 write_hex ((unsigned char) (val >> 16));
234 write_hex ((unsigned char) (val >> 8));
235 write_hex ((unsigned char) val);
236}
237
238#endif
239
240
241int init_sdram (void)
242{
wdenk858b1a62002-09-30 16:12:23 +0000243 unsigned long tmp, baseaddr;
244 unsigned short i;
245 unsigned char trp_clocks,
246 trcd_clocks,
247 tras_clocks,
248 trc_clocks,
249 tctp_clocks;
250 unsigned char cal_val;
251 unsigned char bc;
wdenke39c2842003-06-04 15:05:30 +0000252 unsigned long sdram_tim, sdram_bank;
wdenk858b1a62002-09-30 16:12:23 +0000253
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200254 /*i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);*/
wdenk858b1a62002-09-30 16:12:23 +0000255 (void) get_clocks ();
256 gd->baudrate = 9600;
257 serial_init ();
wdenke39c2842003-06-04 15:05:30 +0000258 /* set up the pld */
Stefan Roese918010a2009-09-09 16:25:29 +0200259 mtdcr (EBC0_CFGADDR, PB7AP);
260 mtdcr (EBC0_CFGDATA, PLD_AP);
261 mtdcr (EBC0_CFGADDR, PB7CR);
262 mtdcr (EBC0_CFGDATA, PLD_CR);
wdenke39c2842003-06-04 15:05:30 +0000263 /* THIS IS OBSOLETE */
264 /* set up the board rev reg*/
Stefan Roese918010a2009-09-09 16:25:29 +0200265 mtdcr (EBC0_CFGADDR, PB5AP);
266 mtdcr (EBC0_CFGDATA, BOARD_AP);
267 mtdcr (EBC0_CFGADDR, PB5CR);
268 mtdcr (EBC0_CFGDATA, BOARD_CR);
wdenke39c2842003-06-04 15:05:30 +0000269#ifdef SDRAM_DEBUG
270 /* get all informations from PLD */
271 serial_puts ("\nPLD Part 0x");
272 bc = in8 (PLD_PART_REG);
273 write_hex (bc);
274 serial_puts ("\nPLD Vers 0x");
275 bc = in8 (PLD_VERS_REG);
276 write_hex (bc);
277 serial_puts ("\nBoard Rev 0x");
278 bc = in8 (PLD_BOARD_CFG_REG);
279 write_hex (bc);
280 serial_puts ("\n");
281#endif
282 /* check board */
283 bc = in8 (PLD_PART_REG);
284#if defined(CONFIG_MIP405T)
285 if((bc & 0x80)==0)
286 SDRAM_err ("U-Boot configured for a MIP405T not for a MIP405!!!\n");
287#else
288 if((bc & 0x80)==0x80)
289 SDRAM_err ("U-Boot configured for a MIP405 not for a MIP405T!!!\n");
290#endif
wdenke39c2842003-06-04 15:05:30 +0000291 /* set-up the chipselect machine */
Stefan Roese918010a2009-09-09 16:25:29 +0200292 mtdcr (EBC0_CFGADDR, PB0CR); /* get cs0 config reg */
293 tmp = mfdcr (EBC0_CFGDATA);
wdenke39c2842003-06-04 15:05:30 +0000294 if ((tmp & 0x00002000) == 0) {
wdenk858b1a62002-09-30 16:12:23 +0000295 /* MPS Boot, set up the flash */
Stefan Roese918010a2009-09-09 16:25:29 +0200296 mtdcr (EBC0_CFGADDR, PB1AP);
297 mtdcr (EBC0_CFGDATA, FLASH_AP);
298 mtdcr (EBC0_CFGADDR, PB1CR);
299 mtdcr (EBC0_CFGDATA, FLASH_CR);
wdenk858b1a62002-09-30 16:12:23 +0000300 } else {
301 /* Flash boot, set up the MPS */
Stefan Roese918010a2009-09-09 16:25:29 +0200302 mtdcr (EBC0_CFGADDR, PB1AP);
303 mtdcr (EBC0_CFGDATA, MPS_AP);
304 mtdcr (EBC0_CFGADDR, PB1CR);
305 mtdcr (EBC0_CFGDATA, MPS_CR);
wdenk858b1a62002-09-30 16:12:23 +0000306 }
307 /* set up UART0 (CS2) and UART1 (CS3) */
Stefan Roese918010a2009-09-09 16:25:29 +0200308 mtdcr (EBC0_CFGADDR, PB2AP);
309 mtdcr (EBC0_CFGDATA, UART0_AP);
310 mtdcr (EBC0_CFGADDR, PB2CR);
311 mtdcr (EBC0_CFGDATA, UART0_CR);
312 mtdcr (EBC0_CFGADDR, PB3AP);
313 mtdcr (EBC0_CFGDATA, UART1_AP);
314 mtdcr (EBC0_CFGADDR, PB3CR);
315 mtdcr (EBC0_CFGDATA, UART1_CR);
wdenke39c2842003-06-04 15:05:30 +0000316 bc = in8 (PLD_BOARD_CFG_REG);
wdenk858b1a62002-09-30 16:12:23 +0000317#ifdef SDRAM_DEBUG
318 serial_puts ("\nstart SDRAM Setup\n");
319 serial_puts ("\nBoard Rev: ");
320 write_hex (bc);
321 serial_puts ("\n");
322#endif
323 i = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200324 baseaddr = CONFIG_SYS_SDRAM_BASE;
wdenk858b1a62002-09-30 16:12:23 +0000325 while (sdram_table[i].sz != 0xff) {
326 if (sdram_table[i].boardtype == bc)
327 break;
328 i++;
329 }
330 if (sdram_table[i].boardtype != bc)
331 SDRAM_err ("No SDRAM table found for this board!!!\n");
332#ifdef SDRAM_DEBUG
333 serial_puts (" found table ");
334 write_hex (i);
335 serial_puts (" \n");
336#endif
wdenk874ac262003-07-24 23:38:38 +0000337 /* since the ECC initialisation needs some time,
338 * we show that we're alive
339 */
340 if (sdram_table[i].ecc)
341 serial_puts ("\nInitializing SDRAM, Please stand by");
wdenk858b1a62002-09-30 16:12:23 +0000342 cal_val = sdram_table[i].cal - 1; /* Cas Latency */
343 trp_clocks = sdram_table[i].trp; /* 20ns / 7.5 ns datain[27] */
344 trcd_clocks = sdram_table[i].trcd; /* 20ns /7.5 ns (datain[29]) */
345 tras_clocks = sdram_table[i].tras; /* 44ns /7.5 ns (datain[30]) */
346 /* ctp = ((trp + tras) - trp - trcd) => tras - trcd */
347 tctp_clocks = sdram_table[i].tctp; /* 44 - 20ns = 24ns */
348 /* trc_clocks is sum of trp_clocks + tras_clocks */
349 trc_clocks = trp_clocks + tras_clocks;
350 /* get SDRAM timing register */
Stefan Roese6987e652009-09-24 13:59:57 +0200351 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roese918010a2009-09-09 16:25:29 +0200352 sdram_tim = mfdcr (SDRAM0_CFGDATA) & ~0x018FC01F;
wdenk858b1a62002-09-30 16:12:23 +0000353 /* insert CASL value */
354 sdram_tim |= ((unsigned long) (cal_val)) << 23;
355 /* insert PTA value */
356 sdram_tim |= ((unsigned long) (trp_clocks - 1)) << 18;
357 /* insert CTP value */
358 sdram_tim |=
359 ((unsigned long) (trc_clocks - trp_clocks -
360 trcd_clocks)) << 16;
361 /* insert LDF (always 01) */
362 sdram_tim |= ((unsigned long) 0x01) << 14;
363 /* insert RFTA value */
364 sdram_tim |= ((unsigned long) (trc_clocks - 4)) << 2;
365 /* insert RCD value */
366 sdram_tim |= ((unsigned long) (trcd_clocks - 1)) << 0;
367
368 tmp = ((unsigned long) (sdram_table[i].am - 1) << 13); /* AM = 3 */
369 /* insert SZ value; */
370 tmp |= ((unsigned long) sdram_table[i].sz << 17);
371 /* get SDRAM bank 0 register */
Stefan Roese6987e652009-09-24 13:59:57 +0200372 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200373 sdram_bank = mfdcr (SDRAM0_CFGDATA) & ~0xFFCEE001;
wdenk858b1a62002-09-30 16:12:23 +0000374 sdram_bank |= (baseaddr | tmp | 0x01);
375
376#ifdef SDRAM_DEBUG
377 serial_puts ("sdtr: ");
378 write_4hex (sdram_tim);
379 serial_puts ("\n");
380#endif
381
382 /* write SDRAM timing register */
Stefan Roese6987e652009-09-24 13:59:57 +0200383 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roese918010a2009-09-09 16:25:29 +0200384 mtdcr (SDRAM0_CFGDATA, sdram_tim);
wdenk858b1a62002-09-30 16:12:23 +0000385
386#ifdef SDRAM_DEBUG
387 serial_puts ("mb0cf: ");
388 write_4hex (sdram_bank);
389 serial_puts ("\n");
390#endif
391
392 /* write SDRAM bank 0 register */
Stefan Roese6987e652009-09-24 13:59:57 +0200393 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200394 mtdcr (SDRAM0_CFGDATA, sdram_bank);
wdenk858b1a62002-09-30 16:12:23 +0000395
396 if (get_bus_freq (tmp) > 110000000) { /* > 110MHz */
397 /* get SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200398 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200399 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000400 tmp |= 0x07F00000;
401 } else {
402 /* get SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200403 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200404 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000405 tmp |= 0x05F00000;
406 }
407 /* write SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200408 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200409 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000410 /* enable ECC if used */
wdenke39c2842003-06-04 15:05:30 +0000411#if defined(ENABLE_ECC) && !defined(CONFIG_BOOT_PCI)
wdenk858b1a62002-09-30 16:12:23 +0000412 if (sdram_table[i].ecc) {
413 /* disable checking for all banks */
wdenke39c2842003-06-04 15:05:30 +0000414 unsigned long *p;
wdenk858b1a62002-09-30 16:12:23 +0000415#ifdef SDRAM_DEBUG
416 serial_puts ("disable ECC.. ");
417#endif
Stefan Roese6987e652009-09-24 13:59:57 +0200418 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200419 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000420 tmp &= 0xff0fffff; /* disable all banks */
Stefan Roese6987e652009-09-24 13:59:57 +0200421 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
wdenk858b1a62002-09-30 16:12:23 +0000422 /* set up SDRAM Controller with ECC enabled */
423#ifdef SDRAM_DEBUG
424 serial_puts ("setup SDRAM Controller.. ");
425#endif
Stefan Roese918010a2009-09-09 16:25:29 +0200426 mtdcr (SDRAM0_CFGDATA, tmp);
Stefan Roese6987e652009-09-24 13:59:57 +0200427 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200428 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x90800000;
Stefan Roese6987e652009-09-24 13:59:57 +0200429 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200430 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000431 udelay (600);
432#ifdef SDRAM_DEBUG
433 serial_puts ("fill the memory..\n");
434#endif
435 serial_puts (".");
436 /* now, fill all the memory */
437 tmp = ((4 * MEGA_BYTE) << sdram_table[i].sz);
438 p = (unsigned long) 0;
439 while ((unsigned long) p < tmp) {
440 *p++ = 0L;
441 if (!((unsigned long) p % 0x00800000)) /* every 8MByte */
442 serial_puts (".");
wdenk858b1a62002-09-30 16:12:23 +0000443 }
444 /* enable bank 0 */
445 serial_puts (".");
446#ifdef SDRAM_DEBUG
447 serial_puts ("enable ECC\n");
448#endif
449 udelay (400);
Stefan Roese6987e652009-09-24 13:59:57 +0200450 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200451 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000452 tmp |= 0x00800000; /* enable bank 0 */
Stefan Roese918010a2009-09-09 16:25:29 +0200453 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000454 udelay (400);
455 } else
456#endif
457 {
458 /* enable SDRAM controller with no ECC, 32-bit SDRAM width, 16 byte burst */
Stefan Roese6987e652009-09-24 13:59:57 +0200459 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200460 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x80C00000;
Stefan Roese6987e652009-09-24 13:59:57 +0200461 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200462 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000463 udelay (400);
464 }
465 serial_puts ("\n");
466 return (0);
467}
468
wdenkda55c6e2004-01-20 23:12:12 +0000469int board_early_init_f (void)
wdenk858b1a62002-09-30 16:12:23 +0000470{
471 init_sdram ();
472
473 /*-------------------------------------------------------------------------+
474 | Interrupt controller setup for the PIP405 board.
475 | Note: IRQ 0-15 405GP internally generated; active high; level sensitive
476 | IRQ 16 405GP internally generated; active low; level sensitive
477 | IRQ 17-24 RESERVED
478 | IRQ 25 (EXT IRQ 0) SouthBridge; active low; level sensitive
479 | IRQ 26 (EXT IRQ 1) NMI: active low; level sensitive
480 | IRQ 27 (EXT IRQ 2) SMI: active Low; level sensitive
481 | IRQ 28 (EXT IRQ 3) PCI SLOT 3; active low; level sensitive
482 | IRQ 29 (EXT IRQ 4) PCI SLOT 2; active low; level sensitive
483 | IRQ 30 (EXT IRQ 5) PCI SLOT 1; active low; level sensitive
484 | IRQ 31 (EXT IRQ 6) PCI SLOT 0; active low; level sensitive
485 | Note for MIP405 board:
486 | An interrupt taken for the SouthBridge (IRQ 25) indicates that
487 | the Interrupt Controller in the South Bridge has caused the
488 | interrupt. The IC must be read to determine which device
489 | caused the interrupt.
490 |
491 +-------------------------------------------------------------------------*/
Stefan Roese707fd362009-09-24 09:55:50 +0200492 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
493 mtdcr (UIC0ER, 0x00000000); /* disable all ints */
494 mtdcr (UIC0CR, 0x00000000); /* set all to be non-critical (for now) */
495 mtdcr (UIC0PR, 0xFFFFFF80); /* set int polarities */
496 mtdcr (UIC0TR, 0x10000000); /* set int trigger levels */
497 mtdcr (UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
498 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
wdenk858b1a62002-09-30 16:12:23 +0000499 return 0;
500}
501
502
503/*
504 * Get some PLD Registers
505 */
506
507unsigned short get_pld_parvers (void)
508{
509 unsigned short result;
510 unsigned char rc;
511
512 rc = in8 (PLD_PART_REG);
513 result = (unsigned short) rc << 8;
514 rc = in8 (PLD_VERS_REG);
515 result |= rc;
516 return result;
517}
518
519
wdenk858b1a62002-09-30 16:12:23 +0000520void user_led0 (unsigned char on)
521{
522 if (on)
523 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x4));
524 else
525 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfb));
526}
527
528
529void ide_set_reset (int idereset)
530{
531 /* if reset = 1 IDE reset will be asserted */
532 if (idereset)
533 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x1));
534 else {
535 udelay (10000);
536 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfe));
537 }
538}
539
540
541/* ------------------------------------------------------------------------- */
542
wdenke39c2842003-06-04 15:05:30 +0000543void get_pcbrev_var(unsigned char *pcbrev, unsigned char *var)
wdenk858b1a62002-09-30 16:12:23 +0000544{
wdenke39c2842003-06-04 15:05:30 +0000545#if !defined(CONFIG_MIP405T)
546 unsigned char bc,rc,tmp;
wdenk858b1a62002-09-30 16:12:23 +0000547 int i;
wdenk858b1a62002-09-30 16:12:23 +0000548
wdenke39c2842003-06-04 15:05:30 +0000549 bc = in8 (PLD_BOARD_CFG_REG);
550 tmp = ~bc;
551 tmp &= 0xf;
wdenk858b1a62002-09-30 16:12:23 +0000552 rc = 0;
553 for (i = 0; i < 4; i++) {
554 rc <<= 1;
wdenke39c2842003-06-04 15:05:30 +0000555 rc += (tmp & 0x1);
556 tmp >>= 1;
wdenk858b1a62002-09-30 16:12:23 +0000557 }
558 rc++;
wdenk60164a82003-10-08 23:26:14 +0000559 if(( (((bc>>4) & 0xf)==0x2) /* Rev C PCB or */
560 || (((bc>>4) & 0xf)==0x1)) /* Rev B PCB with */
wdenk7d076412003-05-23 11:38:58 +0000561 && (rc==0x1)) /* Population Option 1 is a -3 */
562 rc=3;
wdenke39c2842003-06-04 15:05:30 +0000563 *pcbrev=(bc >> 4) & 0xf;
564 *var=rc;
565#else
566 unsigned char bc;
567 bc = in8 (PLD_BOARD_CFG_REG);
568 *pcbrev=(bc >> 4) & 0xf;
wdenk874ac262003-07-24 23:38:38 +0000569 *var=16-(bc & 0xf);
wdenke39c2842003-06-04 15:05:30 +0000570#endif
571}
572
573/*
574 * Check Board Identity:
575 */
576/* serial String: "MIP405_1000" OR "MIP405T_1000" */
577#if !defined(CONFIG_MIP405T)
578#define BOARD_NAME "MIP405"
579#else
580#define BOARD_NAME "MIP405T"
581#endif
582
583int checkboard (void)
584{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200585 char s[50];
wdenke39c2842003-06-04 15:05:30 +0000586 unsigned char bc, var;
587 int i;
588 backup_t *b = (backup_t *) s;
589
590 puts ("Board: ");
591 get_pcbrev_var(&bc,&var);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200592 i = getenv_r ("serial#", (char *)s, 32);
593 if ((i == 0) || strncmp ((char *)s, BOARD_NAME,sizeof(BOARD_NAME))) {
wdenk858b1a62002-09-30 16:12:23 +0000594 get_backup_values (b);
595 if (strncmp (b->signature, "MPL\0", 4) != 0) {
wdenke39c2842003-06-04 15:05:30 +0000596 puts ("### No HW ID - assuming " BOARD_NAME);
597 printf ("-%d Rev %c", var, 'A' + bc);
wdenk858b1a62002-09-30 16:12:23 +0000598 } else {
wdenke39c2842003-06-04 15:05:30 +0000599 b->serial_name[sizeof(BOARD_NAME)-1] = 0;
600 printf ("%s-%d Rev %c SN: %s", b->serial_name, var,
601 'A' + bc, &b->serial_name[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000602 }
603 } else {
wdenke39c2842003-06-04 15:05:30 +0000604 s[sizeof(BOARD_NAME)-1] = 0;
605 printf ("%s-%d Rev %c SN: %s", s, var,'A' + bc,
606 &s[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000607 }
608 bc = in8 (PLD_EXT_CONF_REG);
609 printf (" Boot Config: 0x%x\n", bc);
610 return (0);
611}
612
613
614/* ------------------------------------------------------------------------- */
615/* ------------------------------------------------------------------------- */
616/*
617 initdram(int board_type) reads EEPROM via I2c. EEPROM contains all of
618 the necessary info for SDRAM controller configuration
619*/
620/* ------------------------------------------------------------------------- */
621/* ------------------------------------------------------------------------- */
622static int test_dram (unsigned long ramsize);
623
Becky Brucebd99ae72008-06-09 16:03:40 -0500624phys_size_t initdram (int board_type)
wdenk858b1a62002-09-30 16:12:23 +0000625{
626
627 unsigned long bank_reg[4], tmp, bank_size;
628 int i, ds;
629 unsigned long TotalSize;
630
631 ds = 0;
632 /* since the DRAM controller is allready set up, calculate the size with the
633 bank registers */
Stefan Roese6987e652009-09-24 13:59:57 +0200634 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200635 bank_reg[0] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese6987e652009-09-24 13:59:57 +0200636 mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200637 bank_reg[1] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese6987e652009-09-24 13:59:57 +0200638 mtdcr (SDRAM0_CFGADDR, SDRAM0_B2CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200639 bank_reg[2] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese6987e652009-09-24 13:59:57 +0200640 mtdcr (SDRAM0_CFGADDR, SDRAM0_B3CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200641 bank_reg[3] = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000642 TotalSize = 0;
643 for (i = 0; i < 4; i++) {
644 if ((bank_reg[i] & 0x1) == 0x1) {
645 tmp = (bank_reg[i] >> 17) & 0x7;
646 bank_size = 4 << tmp;
647 TotalSize += bank_size;
648 } else
649 ds = 1;
650 }
Stefan Roese6987e652009-09-24 13:59:57 +0200651 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200652 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000653
654 if (!tmp)
655 printf ("No ");
656 printf ("ECC ");
657
658 test_dram (TotalSize * MEGA_BYTE);
659 return (TotalSize * MEGA_BYTE);
660}
661
662/* ------------------------------------------------------------------------- */
663
wdenk858b1a62002-09-30 16:12:23 +0000664
665static int test_dram (unsigned long ramsize)
666{
667#ifdef SDRAM_DEBUG
668 mem_test (0L, ramsize, 1);
669#endif
670 /* not yet implemented */
671 return (1);
672}
673
wdenk874ac262003-07-24 23:38:38 +0000674/* used to check if the time in RTC is valid */
675static unsigned long start;
676static struct rtc_time tm;
wdenk2c9b05d2003-09-10 22:30:53 +0000677extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk874ac262003-07-24 23:38:38 +0000678
wdenk858b1a62002-09-30 16:12:23 +0000679int misc_init_r (void)
680{
wdenk2c9b05d2003-09-10 22:30:53 +0000681 /* adjust flash start and size as well as the offset */
682 gd->bd->bi_flashstart=0-flash_info[0].size;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200683 gd->bd->bi_flashsize=flash_info[0].size-CONFIG_SYS_MONITOR_LEN;
wdenk2c9b05d2003-09-10 22:30:53 +0000684 gd->bd->bi_flashoffset=0;
685
wdenk874ac262003-07-24 23:38:38 +0000686 /* check, if RTC is running */
687 rtc_get (&tm);
688 start=get_timer(0);
wdenke39c2842003-06-04 15:05:30 +0000689 /* if MIP405 has booted from PCI, reset CCR0[24] as described in errata PCI_18 */
Stefan Roese918010a2009-09-09 16:25:29 +0200690 if (mfdcr(CPC0_PSR) & PSR_ROM_LOC)
Matthias Fuchs730b2d22009-07-22 17:27:56 +0200691 mtspr(SPRN_CCR0, (mfspr(SPRN_CCR0) & ~0x80));
wdenke39c2842003-06-04 15:05:30 +0000692
wdenk858b1a62002-09-30 16:12:23 +0000693 return (0);
694}
695
696
697void print_mip405_rev (void)
698{
wdenke39c2842003-06-04 15:05:30 +0000699 unsigned char part, vers, pcbrev, var;
wdenk858b1a62002-09-30 16:12:23 +0000700
wdenke39c2842003-06-04 15:05:30 +0000701 get_pcbrev_var(&pcbrev,&var);
wdenk858b1a62002-09-30 16:12:23 +0000702 part = in8 (PLD_PART_REG);
703 vers = in8 (PLD_VERS_REG);
wdenke39c2842003-06-04 15:05:30 +0000704 printf ("Rev: " BOARD_NAME "-%d Rev %c PLD %d Vers %d\n",
705 var, pcbrev + 'A', part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000706}
707
wdenke97d3d92004-02-23 22:22:28 +0000708
wdenk874ac262003-07-24 23:38:38 +0000709extern int mk_date (char *, struct rtc_time *);
wdenk858b1a62002-09-30 16:12:23 +0000710
711int last_stage_init (void)
712{
wdenk874ac262003-07-24 23:38:38 +0000713 unsigned long stop;
714 struct rtc_time newtm;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200715 char *s;
Peter Tyser43c08d72009-09-21 11:20:33 -0500716
wdenkb02744a2003-04-05 00:53:31 +0000717 /* write correct LED configuration */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200718 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x14, 0x2402) != 0) {
wdenk858b1a62002-09-30 16:12:23 +0000719 printf ("Error writing to the PHY\n");
720 }
wdenkb02744a2003-04-05 00:53:31 +0000721 /* since LED/CFG2 is not connected on the -2,
722 * write to correct capability information */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200723 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x4, 0x01E1) != 0) {
wdenkb02744a2003-04-05 00:53:31 +0000724 printf ("Error writing to the PHY\n");
725 }
wdenk858b1a62002-09-30 16:12:23 +0000726 print_mip405_rev ();
Jean-Christophe PLAGNIOL-VILLARDd4303af2009-05-16 12:14:56 +0200727 stdio_print_current_devices ();
wdenk858b1a62002-09-30 16:12:23 +0000728 check_env ();
wdenk874ac262003-07-24 23:38:38 +0000729 /* check if RTC time is valid */
730 stop=get_timer(start);
731 while(stop<1200) { /* we wait 1.2 sec to check if the RTC is running */
732 udelay(1000);
733 stop=get_timer(start);
734 }
735 rtc_get (&newtm);
736 if(tm.tm_sec==newtm.tm_sec) {
737 s=getenv("defaultdate");
738 if(!s)
739 mk_date ("010112001970", &newtm);
740 else
741 if(mk_date (s, &newtm)!=0) {
742 printf("RTC: Bad date format in defaultdate\n");
743 return 0;
744 }
745 rtc_reset ();
746 rtc_set(&newtm);
747 }
wdenk858b1a62002-09-30 16:12:23 +0000748 return 0;
749}
750
751/***************************************************************************
752 * some helping routines
753 */
754
755int overwrite_console (void)
756{
757 return ((in8 (PLD_EXT_CONF_REG) & 0x1)==0); /* return TRUE if console should be overwritten */
758}
759
760
761/************************************************************************
762* Print MIP405 Info
763************************************************************************/
764void print_mip405_info (void)
765{
766 unsigned char part, vers, cfg, irq_reg, com_mode, ext;
767
768 part = in8 (PLD_PART_REG);
769 vers = in8 (PLD_VERS_REG);
770 cfg = in8 (PLD_BOARD_CFG_REG);
771 irq_reg = in8 (PLD_IRQ_REG);
772 com_mode = in8 (PLD_COM_MODE_REG);
773 ext = in8 (PLD_EXT_CONF_REG);
774
wdenke39c2842003-06-04 15:05:30 +0000775 printf ("PLD Part %d version %d\n", part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000776 printf ("Board Revision %c\n", ((cfg >> 4) & 0xf) + 'A');
777 printf ("Population Options %d %d %d %d\n", (cfg) & 0x1,
778 (cfg >> 1) & 0x1, (cfg >> 2) & 0x1, (cfg >> 3) & 0x1);
779 printf ("User LED %s\n", (com_mode & 0x4) ? "on" : "off");
780 printf ("UART Clocks %d\n", (com_mode >> 4) & 0x3);
wdenke39c2842003-06-04 15:05:30 +0000781#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000782 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
783 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
784 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
785 (ext >> 6) & 0x1, (ext >> 7) & 0x1);
786 printf ("SER1 uses handshakes %s\n",
787 (ext & 0x80) ? "DTR/DSR" : "RTS/CTS");
wdenke39c2842003-06-04 15:05:30 +0000788#else
wdenk874ac262003-07-24 23:38:38 +0000789 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
wdenke39c2842003-06-04 15:05:30 +0000790 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
791 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
wdenk874ac262003-07-24 23:38:38 +0000792 (ext >> 6) & 0x1,(ext >> 7) & 0x1);
wdenke39c2842003-06-04 15:05:30 +0000793#endif
wdenk858b1a62002-09-30 16:12:23 +0000794 printf ("IDE Reset %s\n", (ext & 0x01) ? "asserted" : "not asserted");
795 printf ("IRQs:\n");
796 printf (" PIIX INTR: %s\n", (irq_reg & 0x80) ? "inactive" : "active");
wdenke39c2842003-06-04 15:05:30 +0000797#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000798 printf (" UART0 IRQ: %s\n", (irq_reg & 0x40) ? "inactive" : "active");
799 printf (" UART1 IRQ: %s\n", (irq_reg & 0x20) ? "inactive" : "active");
wdenke39c2842003-06-04 15:05:30 +0000800#endif
wdenk858b1a62002-09-30 16:12:23 +0000801 printf (" PIIX SMI: %s\n", (irq_reg & 0x10) ? "inactive" : "active");
802 printf (" PIIX INIT: %s\n", (irq_reg & 0x8) ? "inactive" : "active");
803 printf (" PIIX NMI: %s\n", (irq_reg & 0x4) ? "inactive" : "active");
804}