blob: 56a84e9afab6901a97c69e0f1057788161c02fb8 [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 Roeseafabb492010-09-12 06:21:37 +020068#include <asm/ppc4xx.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020069#include <asm/ppc4xx-i2c.h>
wdenk858b1a62002-09-30 16:12:23 +000070#include <miiphy.h>
71#include "../common/common_util.h"
Jean-Christophe PLAGNIOL-VILLARD28c34502009-05-16 12:14:56 +020072#include <stdio_dev.h>
wdenk858b1a62002-09-30 16:12:23 +000073#include <i2c.h>
wdenk27b207f2003-07-24 23:38:38 +000074#include <rtc.h>
Wolfgang Denkd87080b2006-03-31 18:32:53 +020075
76DECLARE_GLOBAL_DATA_PTR;
77
wdenk858b1a62002-09-30 16:12:23 +000078#undef SDRAM_DEBUG
wdenkf3e0de62003-06-04 15:05:30 +000079#define ENABLE_ECC /* for ecc boards */
wdenk858b1a62002-09-30 16:12:23 +000080#define FALSE 0
81#define TRUE 1
82
83/* stdlib.h causes some compatibility problems; should fixe these! -- wd */
84#ifndef __ldiv_t_defined
85typedef struct {
86 long int quot; /* Quotient */
87 long int rem; /* Remainder */
88} ldiv_t;
89extern ldiv_t ldiv (long int __numer, long int __denom);
90# define __ldiv_t_defined 1
91#endif
92
93
Wolfgang Denk53677ef2008-05-20 16:00:29 +020094#define PLD_PART_REG PER_PLD_ADDR + 0
95#define PLD_VERS_REG PER_PLD_ADDR + 1
96#define PLD_BOARD_CFG_REG PER_PLD_ADDR + 2
97#define PLD_IRQ_REG PER_PLD_ADDR + 3
98#define PLD_COM_MODE_REG PER_PLD_ADDR + 4
99#define PLD_EXT_CONF_REG PER_PLD_ADDR + 5
wdenk858b1a62002-09-30 16:12:23 +0000100
101#define MEGA_BYTE (1024*1024)
102
103typedef struct {
104 unsigned char boardtype; /* Board revision and Population Options */
105 unsigned char cal; /* cas Latency (will be programmend as cal-1) */
106 unsigned char trp; /* datain27 in clocks */
107 unsigned char trcd; /* datain29 in clocks */
108 unsigned char tras; /* datain30 in clocks */
109 unsigned char tctp; /* tras - trcd in clocks */
110 unsigned char am; /* Address Mod (will be programmed as am-1) */
111 unsigned char sz; /* log binary => Size = (4MByte<<sz) 5 = 128, 4 = 64, 3 = 32, 2 = 16, 1=8 */
112 unsigned char ecc; /* if true, ecc is enabled */
113} sdram_t;
wdenkf3e0de62003-06-04 15:05:30 +0000114#if defined(CONFIG_MIP405T)
115const sdram_t sdram_table[] = {
wdenk27b207f2003-07-24 23:38:38 +0000116 { 0x0F, /* MIP405T Rev A, 64MByte -1 Board */
wdenkf3e0de62003-06-04 15:05:30 +0000117 3, /* Case Latenty = 3 */
118 3, /* trp 20ns / 7.5 ns datain[27] */
119 3, /* trcd 20ns /7.5 ns (datain[29]) */
120 6, /* tras 44ns /7.5 ns (datain[30]) */
121 4, /* tcpt 44 - 20ns = 24ns */
wdenk27b207f2003-07-24 23:38:38 +0000122 2, /* Address Mode = 2 (12x9x4) */
123 3, /* size value (32MByte) */
wdenkf3e0de62003-06-04 15:05:30 +0000124 0}, /* ECC disabled */
125 { 0xff, /* terminator */
126 0xff,
127 0xff,
128 0xff,
129 0xff,
130 0xff,
131 0xff,
132 0xff }
133};
134#else
wdenk858b1a62002-09-30 16:12:23 +0000135const sdram_t sdram_table[] = {
136 { 0x0f, /* Rev A, 128MByte -1 Board */
137 3, /* Case Latenty = 3 */
138 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk33149b82003-05-23 11:38:58 +0000139 3, /* trcd 20ns /7.5 ns (datain[29]) */
140 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000141 4, /* tcpt 44 - 20ns = 24ns */
wdenk33149b82003-05-23 11:38:58 +0000142 3, /* Address Mode = 3 */
wdenk858b1a62002-09-30 16:12:23 +0000143 5, /* size value */
144 1}, /* ECC enabled */
145 { 0x07, /* Rev A, 64MByte -2 Board */
146 3, /* Case Latenty = 3 */
147 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk33149b82003-05-23 11:38:58 +0000148 3, /* trcd 20ns /7.5 ns (datain[29]) */
149 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000150 4, /* tcpt 44 - 20ns = 24ns */
wdenk33149b82003-05-23 11:38:58 +0000151 2, /* Address Mode = 2 */
wdenk858b1a62002-09-30 16:12:23 +0000152 4, /* size value */
153 1}, /* ECC enabled */
wdenk3e386912003-04-05 00:53:31 +0000154 { 0x03, /* Rev A, 128MByte -4 Board */
155 3, /* Case Latenty = 3 */
156 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk33149b82003-05-23 11:38:58 +0000157 3, /* trcd 20ns /7.5 ns (datain[29]) */
158 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk3e386912003-04-05 00:53:31 +0000159 4, /* tcpt 44 - 20ns = 24ns */
wdenk33149b82003-05-23 11:38:58 +0000160 3, /* Address Mode = 3 */
161 5, /* size value */
162 1}, /* ECC enabled */
163 { 0x1f, /* Rev B, 128MByte -3 Board */
164 3, /* Case Latenty = 3 */
165 3, /* trp 20ns / 7.5 ns datain[27] */
166 3, /* trcd 20ns /7.5 ns (datain[29]) */
167 6, /* tras 44ns /7.5 ns (datain[30]) */
168 4, /* tcpt 44 - 20ns = 24ns */
169 3, /* Address Mode = 3 */
wdenk3e386912003-04-05 00:53:31 +0000170 5, /* size value */
171 1}, /* ECC enabled */
wdenk4a551702003-10-08 23:26:14 +0000172 { 0x2f, /* Rev C, 128MByte -3 Board */
173 3, /* Case Latenty = 3 */
174 3, /* trp 20ns / 7.5 ns datain[27] */
175 3, /* trcd 20ns /7.5 ns (datain[29]) */
176 6, /* tras 44ns /7.5 ns (datain[30]) */
177 4, /* tcpt 44 - 20ns = 24ns */
178 3, /* Address Mode = 3 */
179 5, /* size value */
180 1}, /* ECC enabled */
wdenk858b1a62002-09-30 16:12:23 +0000181 { 0xff, /* terminator */
182 0xff,
183 0xff,
184 0xff,
185 0xff,
186 0xff,
187 0xff,
188 0xff }
189};
wdenkf3e0de62003-06-04 15:05:30 +0000190#endif /*CONFIG_MIP405T */
wdenk858b1a62002-09-30 16:12:23 +0000191void SDRAM_err (const char *s)
192{
193#ifndef SDRAM_DEBUG
wdenk858b1a62002-09-30 16:12:23 +0000194 (void) get_clocks ();
195 gd->baudrate = 9600;
196 serial_init ();
197#endif
198 serial_puts ("\n");
199 serial_puts (s);
200 serial_puts ("\n enable SDRAM_DEBUG for more info\n");
201 for (;;);
202}
203
204
205unsigned char get_board_revcfg (void)
206{
207 out8 (PER_BOARD_ADDR, 0);
208 return (in8 (PER_BOARD_ADDR));
209}
210
211
212#ifdef SDRAM_DEBUG
213
214void write_hex (unsigned char i)
215{
216 char cc;
217
218 cc = i >> 4;
219 cc &= 0xf;
220 if (cc > 9)
221 serial_putc (cc + 55);
222 else
223 serial_putc (cc + 48);
224 cc = i & 0xf;
225 if (cc > 9)
226 serial_putc (cc + 55);
227 else
228 serial_putc (cc + 48);
229}
230
231void write_4hex (unsigned long val)
232{
233 write_hex ((unsigned char) (val >> 24));
234 write_hex ((unsigned char) (val >> 16));
235 write_hex ((unsigned char) (val >> 8));
236 write_hex ((unsigned char) val);
237}
238
239#endif
240
241
242int init_sdram (void)
243{
wdenk858b1a62002-09-30 16:12:23 +0000244 unsigned long tmp, baseaddr;
245 unsigned short i;
246 unsigned char trp_clocks,
247 trcd_clocks,
248 tras_clocks,
Stefan Roese4233faf2011-11-15 08:03:39 +0000249 trc_clocks;
wdenk858b1a62002-09-30 16:12:23 +0000250 unsigned char cal_val;
251 unsigned char bc;
wdenkf3e0de62003-06-04 15:05:30 +0000252 unsigned long sdram_tim, sdram_bank;
wdenk858b1a62002-09-30 16:12:23 +0000253
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-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 ();
wdenkf3e0de62003-06-04 15:05:30 +0000258 /* set up the pld */
Stefan Roesed1c3b272009-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);
wdenkf3e0de62003-06-04 15:05:30 +0000263 /* THIS IS OBSOLETE */
264 /* set up the board rev reg*/
Stefan Roesed1c3b272009-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);
wdenkf3e0de62003-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
wdenkf3e0de62003-06-04 15:05:30 +0000291 /* set-up the chipselect machine */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200292 mtdcr (EBC0_CFGADDR, PB0CR); /* get cs0 config reg */
293 tmp = mfdcr (EBC0_CFGDATA);
wdenkf3e0de62003-06-04 15:05:30 +0000294 if ((tmp & 0x00002000) == 0) {
wdenk858b1a62002-09-30 16:12:23 +0000295 /* MPS Boot, set up the flash */
Stefan Roesed1c3b272009-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 Roesed1c3b272009-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 Roesed1c3b272009-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);
wdenkf3e0de62003-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-VILLARD6d0f6bc2008-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
wdenk27b207f2003-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 */
wdenk858b1a62002-09-30 16:12:23 +0000347 /* trc_clocks is sum of trp_clocks + tras_clocks */
348 trc_clocks = trp_clocks + tras_clocks;
349 /* get SDRAM timing register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200350 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200351 sdram_tim = mfdcr (SDRAM0_CFGDATA) & ~0x018FC01F;
wdenk858b1a62002-09-30 16:12:23 +0000352 /* insert CASL value */
353 sdram_tim |= ((unsigned long) (cal_val)) << 23;
354 /* insert PTA value */
355 sdram_tim |= ((unsigned long) (trp_clocks - 1)) << 18;
356 /* insert CTP value */
357 sdram_tim |=
358 ((unsigned long) (trc_clocks - trp_clocks -
359 trcd_clocks)) << 16;
360 /* insert LDF (always 01) */
361 sdram_tim |= ((unsigned long) 0x01) << 14;
362 /* insert RFTA value */
363 sdram_tim |= ((unsigned long) (trc_clocks - 4)) << 2;
364 /* insert RCD value */
365 sdram_tim |= ((unsigned long) (trcd_clocks - 1)) << 0;
366
367 tmp = ((unsigned long) (sdram_table[i].am - 1) << 13); /* AM = 3 */
368 /* insert SZ value; */
369 tmp |= ((unsigned long) sdram_table[i].sz << 17);
370 /* get SDRAM bank 0 register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200371 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200372 sdram_bank = mfdcr (SDRAM0_CFGDATA) & ~0xFFCEE001;
wdenk858b1a62002-09-30 16:12:23 +0000373 sdram_bank |= (baseaddr | tmp | 0x01);
374
375#ifdef SDRAM_DEBUG
376 serial_puts ("sdtr: ");
377 write_4hex (sdram_tim);
378 serial_puts ("\n");
379#endif
380
381 /* write SDRAM timing register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200382 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200383 mtdcr (SDRAM0_CFGDATA, sdram_tim);
wdenk858b1a62002-09-30 16:12:23 +0000384
385#ifdef SDRAM_DEBUG
386 serial_puts ("mb0cf: ");
387 write_4hex (sdram_bank);
388 serial_puts ("\n");
389#endif
390
391 /* write SDRAM bank 0 register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200392 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200393 mtdcr (SDRAM0_CFGDATA, sdram_bank);
wdenk858b1a62002-09-30 16:12:23 +0000394
395 if (get_bus_freq (tmp) > 110000000) { /* > 110MHz */
396 /* get SDRAM refresh interval register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200397 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200398 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000399 tmp |= 0x07F00000;
400 } else {
401 /* get SDRAM refresh interval register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200402 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200403 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000404 tmp |= 0x05F00000;
405 }
406 /* write SDRAM refresh interval register */
Stefan Roese95b602b2009-09-24 13:59:57 +0200407 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200408 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000409 /* enable ECC if used */
wdenkf3e0de62003-06-04 15:05:30 +0000410#if defined(ENABLE_ECC) && !defined(CONFIG_BOOT_PCI)
wdenk858b1a62002-09-30 16:12:23 +0000411 if (sdram_table[i].ecc) {
412 /* disable checking for all banks */
wdenkf3e0de62003-06-04 15:05:30 +0000413 unsigned long *p;
wdenk858b1a62002-09-30 16:12:23 +0000414#ifdef SDRAM_DEBUG
415 serial_puts ("disable ECC.. ");
416#endif
Stefan Roese95b602b2009-09-24 13:59:57 +0200417 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200418 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000419 tmp &= 0xff0fffff; /* disable all banks */
Stefan Roese95b602b2009-09-24 13:59:57 +0200420 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
wdenk858b1a62002-09-30 16:12:23 +0000421 /* set up SDRAM Controller with ECC enabled */
422#ifdef SDRAM_DEBUG
423 serial_puts ("setup SDRAM Controller.. ");
424#endif
Stefan Roesed1c3b272009-09-09 16:25:29 +0200425 mtdcr (SDRAM0_CFGDATA, tmp);
Stefan Roese95b602b2009-09-24 13:59:57 +0200426 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200427 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x90800000;
Stefan Roese95b602b2009-09-24 13:59:57 +0200428 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200429 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000430 udelay (600);
431#ifdef SDRAM_DEBUG
432 serial_puts ("fill the memory..\n");
433#endif
434 serial_puts (".");
435 /* now, fill all the memory */
436 tmp = ((4 * MEGA_BYTE) << sdram_table[i].sz);
437 p = (unsigned long) 0;
438 while ((unsigned long) p < tmp) {
439 *p++ = 0L;
440 if (!((unsigned long) p % 0x00800000)) /* every 8MByte */
441 serial_puts (".");
wdenk858b1a62002-09-30 16:12:23 +0000442 }
443 /* enable bank 0 */
444 serial_puts (".");
445#ifdef SDRAM_DEBUG
446 serial_puts ("enable ECC\n");
447#endif
448 udelay (400);
Stefan Roese95b602b2009-09-24 13:59:57 +0200449 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200450 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000451 tmp |= 0x00800000; /* enable bank 0 */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200452 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000453 udelay (400);
454 } else
455#endif
456 {
457 /* enable SDRAM controller with no ECC, 32-bit SDRAM width, 16 byte burst */
Stefan Roese95b602b2009-09-24 13:59:57 +0200458 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200459 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x80C00000;
Stefan Roese95b602b2009-09-24 13:59:57 +0200460 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200461 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000462 udelay (400);
463 }
464 serial_puts ("\n");
465 return (0);
466}
467
wdenkc837dcb2004-01-20 23:12:12 +0000468int board_early_init_f (void)
wdenk858b1a62002-09-30 16:12:23 +0000469{
470 init_sdram ();
471
472 /*-------------------------------------------------------------------------+
473 | Interrupt controller setup for the PIP405 board.
474 | Note: IRQ 0-15 405GP internally generated; active high; level sensitive
475 | IRQ 16 405GP internally generated; active low; level sensitive
476 | IRQ 17-24 RESERVED
477 | IRQ 25 (EXT IRQ 0) SouthBridge; active low; level sensitive
478 | IRQ 26 (EXT IRQ 1) NMI: active low; level sensitive
479 | IRQ 27 (EXT IRQ 2) SMI: active Low; level sensitive
480 | IRQ 28 (EXT IRQ 3) PCI SLOT 3; active low; level sensitive
481 | IRQ 29 (EXT IRQ 4) PCI SLOT 2; active low; level sensitive
482 | IRQ 30 (EXT IRQ 5) PCI SLOT 1; active low; level sensitive
483 | IRQ 31 (EXT IRQ 6) PCI SLOT 0; active low; level sensitive
484 | Note for MIP405 board:
485 | An interrupt taken for the SouthBridge (IRQ 25) indicates that
486 | the Interrupt Controller in the South Bridge has caused the
487 | interrupt. The IC must be read to determine which device
488 | caused the interrupt.
489 |
490 +-------------------------------------------------------------------------*/
Stefan Roese952e7762009-09-24 09:55:50 +0200491 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
492 mtdcr (UIC0ER, 0x00000000); /* disable all ints */
493 mtdcr (UIC0CR, 0x00000000); /* set all to be non-critical (for now) */
494 mtdcr (UIC0PR, 0xFFFFFF80); /* set int polarities */
495 mtdcr (UIC0TR, 0x10000000); /* set int trigger levels */
496 mtdcr (UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
497 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
wdenk858b1a62002-09-30 16:12:23 +0000498 return 0;
499}
500
David Müller39441b32011-12-22 13:38:21 +0100501int board_early_init_r(void)
502{
503 int mode;
504
505 /*
506 * since we are relocated, we can finally enable i-cache
507 * and set up the flash CS correctly
508 */
509 icache_enable();
510 setup_cs_reloc();
511 /* get and display boot mode */
512 mode = get_boot_mode();
513 if (mode & BOOT_PCI)
514 printf("PCI Boot %s Map\n", (mode & BOOT_MPS) ?
515 "MPS" : "Flash");
516 else
517 printf("%s Boot\n", (mode & BOOT_MPS) ?
518 "MPS" : "Flash");
519
520 return 0;
521}
wdenk858b1a62002-09-30 16:12:23 +0000522
523/*
524 * Get some PLD Registers
525 */
526
527unsigned short get_pld_parvers (void)
528{
529 unsigned short result;
530 unsigned char rc;
531
532 rc = in8 (PLD_PART_REG);
533 result = (unsigned short) rc << 8;
534 rc = in8 (PLD_VERS_REG);
535 result |= rc;
536 return result;
537}
538
539
wdenk858b1a62002-09-30 16:12:23 +0000540void user_led0 (unsigned char on)
541{
542 if (on)
543 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x4));
544 else
545 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfb));
546}
547
548
549void ide_set_reset (int idereset)
550{
551 /* if reset = 1 IDE reset will be asserted */
552 if (idereset)
553 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x1));
554 else {
555 udelay (10000);
556 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfe));
557 }
558}
559
560
561/* ------------------------------------------------------------------------- */
562
wdenkf3e0de62003-06-04 15:05:30 +0000563void get_pcbrev_var(unsigned char *pcbrev, unsigned char *var)
wdenk858b1a62002-09-30 16:12:23 +0000564{
wdenkf3e0de62003-06-04 15:05:30 +0000565#if !defined(CONFIG_MIP405T)
566 unsigned char bc,rc,tmp;
wdenk858b1a62002-09-30 16:12:23 +0000567 int i;
wdenk858b1a62002-09-30 16:12:23 +0000568
wdenkf3e0de62003-06-04 15:05:30 +0000569 bc = in8 (PLD_BOARD_CFG_REG);
570 tmp = ~bc;
571 tmp &= 0xf;
wdenk858b1a62002-09-30 16:12:23 +0000572 rc = 0;
573 for (i = 0; i < 4; i++) {
574 rc <<= 1;
wdenkf3e0de62003-06-04 15:05:30 +0000575 rc += (tmp & 0x1);
576 tmp >>= 1;
wdenk858b1a62002-09-30 16:12:23 +0000577 }
578 rc++;
wdenk4a551702003-10-08 23:26:14 +0000579 if(( (((bc>>4) & 0xf)==0x2) /* Rev C PCB or */
580 || (((bc>>4) & 0xf)==0x1)) /* Rev B PCB with */
wdenk33149b82003-05-23 11:38:58 +0000581 && (rc==0x1)) /* Population Option 1 is a -3 */
582 rc=3;
wdenkf3e0de62003-06-04 15:05:30 +0000583 *pcbrev=(bc >> 4) & 0xf;
584 *var=rc;
585#else
586 unsigned char bc;
587 bc = in8 (PLD_BOARD_CFG_REG);
588 *pcbrev=(bc >> 4) & 0xf;
wdenk27b207f2003-07-24 23:38:38 +0000589 *var=16-(bc & 0xf);
wdenkf3e0de62003-06-04 15:05:30 +0000590#endif
591}
592
593/*
594 * Check Board Identity:
595 */
596/* serial String: "MIP405_1000" OR "MIP405T_1000" */
597#if !defined(CONFIG_MIP405T)
598#define BOARD_NAME "MIP405"
599#else
600#define BOARD_NAME "MIP405T"
601#endif
602
603int checkboard (void)
604{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200605 char s[50];
wdenkf3e0de62003-06-04 15:05:30 +0000606 unsigned char bc, var;
607 int i;
608 backup_t *b = (backup_t *) s;
609
610 puts ("Board: ");
611 get_pcbrev_var(&bc,&var);
Wolfgang Denkcdb74972010-07-24 21:55:43 +0200612 i = getenv_f("serial#", (char *)s, 32);
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200613 if ((i == 0) || strncmp ((char *)s, BOARD_NAME,sizeof(BOARD_NAME))) {
wdenk858b1a62002-09-30 16:12:23 +0000614 get_backup_values (b);
615 if (strncmp (b->signature, "MPL\0", 4) != 0) {
wdenkf3e0de62003-06-04 15:05:30 +0000616 puts ("### No HW ID - assuming " BOARD_NAME);
617 printf ("-%d Rev %c", var, 'A' + bc);
wdenk858b1a62002-09-30 16:12:23 +0000618 } else {
wdenkf3e0de62003-06-04 15:05:30 +0000619 b->serial_name[sizeof(BOARD_NAME)-1] = 0;
620 printf ("%s-%d Rev %c SN: %s", b->serial_name, var,
621 'A' + bc, &b->serial_name[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000622 }
623 } else {
wdenkf3e0de62003-06-04 15:05:30 +0000624 s[sizeof(BOARD_NAME)-1] = 0;
625 printf ("%s-%d Rev %c SN: %s", s, var,'A' + bc,
626 &s[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000627 }
628 bc = in8 (PLD_EXT_CONF_REG);
629 printf (" Boot Config: 0x%x\n", bc);
630 return (0);
631}
632
633
634/* ------------------------------------------------------------------------- */
635/* ------------------------------------------------------------------------- */
636/*
637 initdram(int board_type) reads EEPROM via I2c. EEPROM contains all of
638 the necessary info for SDRAM controller configuration
639*/
640/* ------------------------------------------------------------------------- */
641/* ------------------------------------------------------------------------- */
642static int test_dram (unsigned long ramsize);
643
Becky Bruce9973e3c2008-06-09 16:03:40 -0500644phys_size_t initdram (int board_type)
wdenk858b1a62002-09-30 16:12:23 +0000645{
646
647 unsigned long bank_reg[4], tmp, bank_size;
Stefan Roese4233faf2011-11-15 08:03:39 +0000648 int i;
wdenk858b1a62002-09-30 16:12:23 +0000649 unsigned long TotalSize;
650
wdenk858b1a62002-09-30 16:12:23 +0000651 /* since the DRAM controller is allready set up, calculate the size with the
652 bank registers */
Stefan Roese95b602b2009-09-24 13:59:57 +0200653 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200654 bank_reg[0] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese95b602b2009-09-24 13:59:57 +0200655 mtdcr (SDRAM0_CFGADDR, SDRAM0_B1CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200656 bank_reg[1] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese95b602b2009-09-24 13:59:57 +0200657 mtdcr (SDRAM0_CFGADDR, SDRAM0_B2CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200658 bank_reg[2] = mfdcr (SDRAM0_CFGDATA);
Stefan Roese95b602b2009-09-24 13:59:57 +0200659 mtdcr (SDRAM0_CFGADDR, SDRAM0_B3CR);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200660 bank_reg[3] = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000661 TotalSize = 0;
662 for (i = 0; i < 4; i++) {
663 if ((bank_reg[i] & 0x1) == 0x1) {
664 tmp = (bank_reg[i] >> 17) & 0x7;
665 bank_size = 4 << tmp;
666 TotalSize += bank_size;
Stefan Roese4233faf2011-11-15 08:03:39 +0000667 }
wdenk858b1a62002-09-30 16:12:23 +0000668 }
Stefan Roese95b602b2009-09-24 13:59:57 +0200669 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200670 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000671
672 if (!tmp)
673 printf ("No ");
674 printf ("ECC ");
675
676 test_dram (TotalSize * MEGA_BYTE);
677 return (TotalSize * MEGA_BYTE);
678}
679
680/* ------------------------------------------------------------------------- */
681
wdenk858b1a62002-09-30 16:12:23 +0000682
683static int test_dram (unsigned long ramsize)
684{
685#ifdef SDRAM_DEBUG
686 mem_test (0L, ramsize, 1);
687#endif
688 /* not yet implemented */
689 return (1);
690}
691
wdenk27b207f2003-07-24 23:38:38 +0000692/* used to check if the time in RTC is valid */
693static unsigned long start;
694static struct rtc_time tm;
695
wdenk858b1a62002-09-30 16:12:23 +0000696int misc_init_r (void)
697{
wdenk7205e402003-09-10 22:30:53 +0000698 /* adjust flash start and size as well as the offset */
699 gd->bd->bi_flashstart=0-flash_info[0].size;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200700 gd->bd->bi_flashsize=flash_info[0].size-CONFIG_SYS_MONITOR_LEN;
wdenk7205e402003-09-10 22:30:53 +0000701 gd->bd->bi_flashoffset=0;
702
wdenk27b207f2003-07-24 23:38:38 +0000703 /* check, if RTC is running */
704 rtc_get (&tm);
705 start=get_timer(0);
wdenkf3e0de62003-06-04 15:05:30 +0000706 /* if MIP405 has booted from PCI, reset CCR0[24] as described in errata PCI_18 */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200707 if (mfdcr(CPC0_PSR) & PSR_ROM_LOC)
Matthias Fuchs58ea1422009-07-22 17:27:56 +0200708 mtspr(SPRN_CCR0, (mfspr(SPRN_CCR0) & ~0x80));
wdenkf3e0de62003-06-04 15:05:30 +0000709
wdenk858b1a62002-09-30 16:12:23 +0000710 return (0);
711}
712
713
714void print_mip405_rev (void)
715{
wdenkf3e0de62003-06-04 15:05:30 +0000716 unsigned char part, vers, pcbrev, var;
wdenk858b1a62002-09-30 16:12:23 +0000717
wdenkf3e0de62003-06-04 15:05:30 +0000718 get_pcbrev_var(&pcbrev,&var);
wdenk858b1a62002-09-30 16:12:23 +0000719 part = in8 (PLD_PART_REG);
720 vers = in8 (PLD_VERS_REG);
wdenkf3e0de62003-06-04 15:05:30 +0000721 printf ("Rev: " BOARD_NAME "-%d Rev %c PLD %d Vers %d\n",
722 var, pcbrev + 'A', part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000723}
724
wdenk63e73c92004-02-23 22:22:28 +0000725
wdenk27b207f2003-07-24 23:38:38 +0000726extern int mk_date (char *, struct rtc_time *);
wdenk858b1a62002-09-30 16:12:23 +0000727
728int last_stage_init (void)
729{
wdenk27b207f2003-07-24 23:38:38 +0000730 unsigned long stop;
731 struct rtc_time newtm;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200732 char *s;
Peter Tyser331ab602009-09-21 11:20:33 -0500733
wdenk3e386912003-04-05 00:53:31 +0000734 /* write correct LED configuration */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200735 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x14, 0x2402) != 0) {
wdenk858b1a62002-09-30 16:12:23 +0000736 printf ("Error writing to the PHY\n");
737 }
wdenk3e386912003-04-05 00:53:31 +0000738 /* since LED/CFG2 is not connected on the -2,
739 * write to correct capability information */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200740 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x4, 0x01E1) != 0) {
wdenk3e386912003-04-05 00:53:31 +0000741 printf ("Error writing to the PHY\n");
742 }
wdenk858b1a62002-09-30 16:12:23 +0000743 print_mip405_rev ();
Jean-Christophe PLAGNIOL-VILLARD28c34502009-05-16 12:14:56 +0200744 stdio_print_current_devices ();
wdenk858b1a62002-09-30 16:12:23 +0000745 check_env ();
wdenk27b207f2003-07-24 23:38:38 +0000746 /* check if RTC time is valid */
747 stop=get_timer(start);
748 while(stop<1200) { /* we wait 1.2 sec to check if the RTC is running */
749 udelay(1000);
750 stop=get_timer(start);
751 }
752 rtc_get (&newtm);
753 if(tm.tm_sec==newtm.tm_sec) {
754 s=getenv("defaultdate");
755 if(!s)
756 mk_date ("010112001970", &newtm);
757 else
758 if(mk_date (s, &newtm)!=0) {
759 printf("RTC: Bad date format in defaultdate\n");
760 return 0;
761 }
762 rtc_reset ();
763 rtc_set(&newtm);
764 }
wdenk858b1a62002-09-30 16:12:23 +0000765 return 0;
766}
767
768/***************************************************************************
769 * some helping routines
770 */
771
772int overwrite_console (void)
773{
774 return ((in8 (PLD_EXT_CONF_REG) & 0x1)==0); /* return TRUE if console should be overwritten */
775}
776
777
778/************************************************************************
779* Print MIP405 Info
780************************************************************************/
781void print_mip405_info (void)
782{
783 unsigned char part, vers, cfg, irq_reg, com_mode, ext;
784
785 part = in8 (PLD_PART_REG);
786 vers = in8 (PLD_VERS_REG);
787 cfg = in8 (PLD_BOARD_CFG_REG);
788 irq_reg = in8 (PLD_IRQ_REG);
789 com_mode = in8 (PLD_COM_MODE_REG);
790 ext = in8 (PLD_EXT_CONF_REG);
791
wdenkf3e0de62003-06-04 15:05:30 +0000792 printf ("PLD Part %d version %d\n", part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000793 printf ("Board Revision %c\n", ((cfg >> 4) & 0xf) + 'A');
794 printf ("Population Options %d %d %d %d\n", (cfg) & 0x1,
795 (cfg >> 1) & 0x1, (cfg >> 2) & 0x1, (cfg >> 3) & 0x1);
796 printf ("User LED %s\n", (com_mode & 0x4) ? "on" : "off");
797 printf ("UART Clocks %d\n", (com_mode >> 4) & 0x3);
wdenkf3e0de62003-06-04 15:05:30 +0000798#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000799 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
800 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
801 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
802 (ext >> 6) & 0x1, (ext >> 7) & 0x1);
803 printf ("SER1 uses handshakes %s\n",
804 (ext & 0x80) ? "DTR/DSR" : "RTS/CTS");
wdenkf3e0de62003-06-04 15:05:30 +0000805#else
wdenk27b207f2003-07-24 23:38:38 +0000806 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
wdenkf3e0de62003-06-04 15:05:30 +0000807 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
808 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
wdenk27b207f2003-07-24 23:38:38 +0000809 (ext >> 6) & 0x1,(ext >> 7) & 0x1);
wdenkf3e0de62003-06-04 15:05:30 +0000810#endif
wdenk858b1a62002-09-30 16:12:23 +0000811 printf ("IDE Reset %s\n", (ext & 0x01) ? "asserted" : "not asserted");
812 printf ("IRQs:\n");
813 printf (" PIIX INTR: %s\n", (irq_reg & 0x80) ? "inactive" : "active");
wdenkf3e0de62003-06-04 15:05:30 +0000814#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000815 printf (" UART0 IRQ: %s\n", (irq_reg & 0x40) ? "inactive" : "active");
816 printf (" UART1 IRQ: %s\n", (irq_reg & 0x20) ? "inactive" : "active");
wdenkf3e0de62003-06-04 15:05:30 +0000817#endif
wdenk858b1a62002-09-30 16:12:23 +0000818 printf (" PIIX SMI: %s\n", (irq_reg & 0x10) ? "inactive" : "active");
819 printf (" PIIX INIT: %s\n", (irq_reg & 0x8) ? "inactive" : "active");
820 printf (" PIIX NMI: %s\n", (irq_reg & 0x4) ? "inactive" : "active");
821}