blob: 4a0d6966a6faffe8a4c251c85afe58c64f79ea61 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk858b1a62002-09-30 16:12:23 +00006 *
7 * TODO: clean-up
8 */
9
10/*
11 * How do I program the SDRAM Timing Register (SDRAM0_TR) for a specific SDRAM or DIMM?
12 *
13 * As an example, consider a case where PC133 memory with CAS Latency equal to 2 is being
14 * used with a 200MHz 405GP. For a typical 128Mb, PC133 SDRAM, the relevant minimum
15 * parameters from the datasheet are:
16 * Tclk = 7.5ns (CL = 2)
17 * Trp = 15ns
18 * Trc = 60ns
19 * Trcd = 15ns
20 * Trfc = 66ns
21 *
22 * If we are operating the 405GP with the MemClk output frequency set to 100 MHZ, the clock
23 * period is 10ns and the parameters needed for the Timing Register are:
24 * CASL = CL = 2 clock cycles
25 * PTA = Trp = 15ns / 10ns = 2 clock cycles
26 * CTP = Trc - Trcd - Trp = (60ns - 15ns - 15ns) / 10ns= 3 clock cycles
27 * LDF = 2 clock cycles (but can be extended to meet board-level timing)
28 * RFTA = Trfc = 66ns / 10ns= 7 clock cycles
29 * RCD = Trcd = 15ns / 10ns= 2 clock cycles
30 *
31 * The actual bit settings in the register would be:
32 *
33 * CASL = 0b01
34 * PTA = 0b01
35 * CTP = 0b10
36 * LDF = 0b01
37 * RFTA = 0b011
38 * RCD = 0b01
39 *
40 * If Trfc is not specified in the datasheet for PC100 or PC133 memory, set RFTA = Trc
41 * instead. Figure 24 in the PC SDRAM Specification Rev. 1.7 shows refresh to active delay
42 * defined as Trc rather than Trfc.
43 * When using DIMM modules, most but not all of the required timing parameters can be read
44 * from the Serial Presence Detect (SPD) EEPROM on the module. Specifically, Trc and Trfc
45 * are not available from the EEPROM
46 */
47
48#include <common.h>
49#include "mip405.h"
50#include <asm/processor.h>
Stefan Roese8cb251a2010-09-12 06:21:37 +020051#include <asm/ppc4xx.h>
Stefan Roese247e9d72010-09-09 19:18:00 +020052#include <asm/ppc4xx-i2c.h>
wdenk858b1a62002-09-30 16:12:23 +000053#include <miiphy.h>
54#include "../common/common_util.h"
Jean-Christophe PLAGNIOL-VILLARDd4303af2009-05-16 12:14:56 +020055#include <stdio_dev.h>
wdenk858b1a62002-09-30 16:12:23 +000056#include <i2c.h>
wdenk874ac262003-07-24 23:38:38 +000057#include <rtc.h>
Wolfgang Denk6405a152006-03-31 18:32:53 +020058
59DECLARE_GLOBAL_DATA_PTR;
60
wdenk858b1a62002-09-30 16:12:23 +000061#undef SDRAM_DEBUG
wdenke39c2842003-06-04 15:05:30 +000062#define ENABLE_ECC /* for ecc boards */
wdenk858b1a62002-09-30 16:12:23 +000063
64/* stdlib.h causes some compatibility problems; should fixe these! -- wd */
65#ifndef __ldiv_t_defined
66typedef struct {
67 long int quot; /* Quotient */
68 long int rem; /* Remainder */
69} ldiv_t;
70extern ldiv_t ldiv (long int __numer, long int __denom);
71# define __ldiv_t_defined 1
72#endif
73
74
Wolfgang Denka1be4762008-05-20 16:00:29 +020075#define PLD_PART_REG PER_PLD_ADDR + 0
76#define PLD_VERS_REG PER_PLD_ADDR + 1
77#define PLD_BOARD_CFG_REG PER_PLD_ADDR + 2
78#define PLD_IRQ_REG PER_PLD_ADDR + 3
79#define PLD_COM_MODE_REG PER_PLD_ADDR + 4
80#define PLD_EXT_CONF_REG PER_PLD_ADDR + 5
wdenk858b1a62002-09-30 16:12:23 +000081
82#define MEGA_BYTE (1024*1024)
83
84typedef struct {
85 unsigned char boardtype; /* Board revision and Population Options */
86 unsigned char cal; /* cas Latency (will be programmend as cal-1) */
87 unsigned char trp; /* datain27 in clocks */
88 unsigned char trcd; /* datain29 in clocks */
89 unsigned char tras; /* datain30 in clocks */
90 unsigned char tctp; /* tras - trcd in clocks */
91 unsigned char am; /* Address Mod (will be programmed as am-1) */
92 unsigned char sz; /* log binary => Size = (4MByte<<sz) 5 = 128, 4 = 64, 3 = 32, 2 = 16, 1=8 */
93 unsigned char ecc; /* if true, ecc is enabled */
94} sdram_t;
wdenke39c2842003-06-04 15:05:30 +000095#if defined(CONFIG_MIP405T)
96const sdram_t sdram_table[] = {
wdenk874ac262003-07-24 23:38:38 +000097 { 0x0F, /* MIP405T Rev A, 64MByte -1 Board */
wdenke39c2842003-06-04 15:05:30 +000098 3, /* Case Latenty = 3 */
99 3, /* trp 20ns / 7.5 ns datain[27] */
100 3, /* trcd 20ns /7.5 ns (datain[29]) */
101 6, /* tras 44ns /7.5 ns (datain[30]) */
102 4, /* tcpt 44 - 20ns = 24ns */
wdenk874ac262003-07-24 23:38:38 +0000103 2, /* Address Mode = 2 (12x9x4) */
104 3, /* size value (32MByte) */
wdenke39c2842003-06-04 15:05:30 +0000105 0}, /* ECC disabled */
106 { 0xff, /* terminator */
107 0xff,
108 0xff,
109 0xff,
110 0xff,
111 0xff,
112 0xff,
113 0xff }
114};
115#else
wdenk858b1a62002-09-30 16:12:23 +0000116const sdram_t sdram_table[] = {
117 { 0x0f, /* Rev A, 128MByte -1 Board */
118 3, /* Case Latenty = 3 */
119 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk7d076412003-05-23 11:38:58 +0000120 3, /* trcd 20ns /7.5 ns (datain[29]) */
121 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000122 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000123 3, /* Address Mode = 3 */
wdenk858b1a62002-09-30 16:12:23 +0000124 5, /* size value */
125 1}, /* ECC enabled */
126 { 0x07, /* Rev A, 64MByte -2 Board */
127 3, /* Case Latenty = 3 */
128 3, /* trp 20ns / 7.5 ns datain[27] */
wdenk7d076412003-05-23 11:38:58 +0000129 3, /* trcd 20ns /7.5 ns (datain[29]) */
130 6, /* tras 44ns /7.5 ns (datain[30]) */
wdenk858b1a62002-09-30 16:12:23 +0000131 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000132 2, /* Address Mode = 2 */
wdenk858b1a62002-09-30 16:12:23 +0000133 4, /* size value */
134 1}, /* ECC enabled */
wdenkb02744a2003-04-05 00:53:31 +0000135 { 0x03, /* Rev A, 128MByte -4 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]) */
wdenkb02744a2003-04-05 00:53:31 +0000140 4, /* tcpt 44 - 20ns = 24ns */
wdenk7d076412003-05-23 11:38:58 +0000141 3, /* Address Mode = 3 */
wdenkb02744a2003-04-05 00:53:31 +0000142 5, /* size value */
143 1}, /* ECC enabled */
wdenk7d076412003-05-23 11:38:58 +0000144 { 0x1f, /* Rev B, 128MByte -3 Board */
145 3, /* Case Latenty = 3 */
146 3, /* trp 20ns / 7.5 ns datain[27] */
147 3, /* trcd 20ns /7.5 ns (datain[29]) */
148 6, /* tras 44ns /7.5 ns (datain[30]) */
149 4, /* tcpt 44 - 20ns = 24ns */
150 3, /* Address Mode = 3 */
151 5, /* size value */
152 1}, /* ECC enabled */
wdenk60164a82003-10-08 23:26:14 +0000153 { 0x2f, /* Rev C, 128MByte -3 Board */
154 3, /* Case Latenty = 3 */
155 3, /* trp 20ns / 7.5 ns datain[27] */
156 3, /* trcd 20ns /7.5 ns (datain[29]) */
157 6, /* tras 44ns /7.5 ns (datain[30]) */
158 4, /* tcpt 44 - 20ns = 24ns */
159 3, /* Address Mode = 3 */
160 5, /* size value */
161 1}, /* ECC enabled */
wdenk858b1a62002-09-30 16:12:23 +0000162 { 0xff, /* terminator */
163 0xff,
164 0xff,
165 0xff,
166 0xff,
167 0xff,
168 0xff,
169 0xff }
170};
wdenke39c2842003-06-04 15:05:30 +0000171#endif /*CONFIG_MIP405T */
wdenk858b1a62002-09-30 16:12:23 +0000172void SDRAM_err (const char *s)
173{
174#ifndef SDRAM_DEBUG
wdenk858b1a62002-09-30 16:12:23 +0000175 (void) get_clocks ();
176 gd->baudrate = 9600;
177 serial_init ();
178#endif
179 serial_puts ("\n");
180 serial_puts (s);
181 serial_puts ("\n enable SDRAM_DEBUG for more info\n");
182 for (;;);
183}
184
185
186unsigned char get_board_revcfg (void)
187{
188 out8 (PER_BOARD_ADDR, 0);
189 return (in8 (PER_BOARD_ADDR));
190}
191
192
193#ifdef SDRAM_DEBUG
194
195void write_hex (unsigned char i)
196{
197 char cc;
198
199 cc = i >> 4;
200 cc &= 0xf;
201 if (cc > 9)
202 serial_putc (cc + 55);
203 else
204 serial_putc (cc + 48);
205 cc = i & 0xf;
206 if (cc > 9)
207 serial_putc (cc + 55);
208 else
209 serial_putc (cc + 48);
210}
211
212void write_4hex (unsigned long val)
213{
214 write_hex ((unsigned char) (val >> 24));
215 write_hex ((unsigned char) (val >> 16));
216 write_hex ((unsigned char) (val >> 8));
217 write_hex ((unsigned char) val);
218}
219
220#endif
221
222
223int init_sdram (void)
224{
wdenk858b1a62002-09-30 16:12:23 +0000225 unsigned long tmp, baseaddr;
226 unsigned short i;
227 unsigned char trp_clocks,
228 trcd_clocks,
229 tras_clocks,
Stefan Roese208cd452011-11-15 08:03:39 +0000230 trc_clocks;
wdenk858b1a62002-09-30 16:12:23 +0000231 unsigned char cal_val;
232 unsigned char bc;
wdenke39c2842003-06-04 15:05:30 +0000233 unsigned long sdram_tim, sdram_bank;
wdenk858b1a62002-09-30 16:12:23 +0000234
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200235 /*i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);*/
wdenk858b1a62002-09-30 16:12:23 +0000236 (void) get_clocks ();
237 gd->baudrate = 9600;
238 serial_init ();
wdenke39c2842003-06-04 15:05:30 +0000239 /* set up the pld */
Stefan Roese918010a2009-09-09 16:25:29 +0200240 mtdcr (EBC0_CFGADDR, PB7AP);
241 mtdcr (EBC0_CFGDATA, PLD_AP);
242 mtdcr (EBC0_CFGADDR, PB7CR);
243 mtdcr (EBC0_CFGDATA, PLD_CR);
wdenke39c2842003-06-04 15:05:30 +0000244 /* THIS IS OBSOLETE */
245 /* set up the board rev reg*/
Stefan Roese918010a2009-09-09 16:25:29 +0200246 mtdcr (EBC0_CFGADDR, PB5AP);
247 mtdcr (EBC0_CFGDATA, BOARD_AP);
248 mtdcr (EBC0_CFGADDR, PB5CR);
249 mtdcr (EBC0_CFGDATA, BOARD_CR);
wdenke39c2842003-06-04 15:05:30 +0000250#ifdef SDRAM_DEBUG
251 /* get all informations from PLD */
252 serial_puts ("\nPLD Part 0x");
253 bc = in8 (PLD_PART_REG);
254 write_hex (bc);
255 serial_puts ("\nPLD Vers 0x");
256 bc = in8 (PLD_VERS_REG);
257 write_hex (bc);
258 serial_puts ("\nBoard Rev 0x");
259 bc = in8 (PLD_BOARD_CFG_REG);
260 write_hex (bc);
261 serial_puts ("\n");
262#endif
263 /* check board */
264 bc = in8 (PLD_PART_REG);
265#if defined(CONFIG_MIP405T)
266 if((bc & 0x80)==0)
267 SDRAM_err ("U-Boot configured for a MIP405T not for a MIP405!!!\n");
268#else
269 if((bc & 0x80)==0x80)
270 SDRAM_err ("U-Boot configured for a MIP405 not for a MIP405T!!!\n");
271#endif
wdenke39c2842003-06-04 15:05:30 +0000272 /* set-up the chipselect machine */
Stefan Roese918010a2009-09-09 16:25:29 +0200273 mtdcr (EBC0_CFGADDR, PB0CR); /* get cs0 config reg */
274 tmp = mfdcr (EBC0_CFGDATA);
wdenke39c2842003-06-04 15:05:30 +0000275 if ((tmp & 0x00002000) == 0) {
wdenk858b1a62002-09-30 16:12:23 +0000276 /* MPS Boot, set up the flash */
Stefan Roese918010a2009-09-09 16:25:29 +0200277 mtdcr (EBC0_CFGADDR, PB1AP);
278 mtdcr (EBC0_CFGDATA, FLASH_AP);
279 mtdcr (EBC0_CFGADDR, PB1CR);
280 mtdcr (EBC0_CFGDATA, FLASH_CR);
wdenk858b1a62002-09-30 16:12:23 +0000281 } else {
282 /* Flash boot, set up the MPS */
Stefan Roese918010a2009-09-09 16:25:29 +0200283 mtdcr (EBC0_CFGADDR, PB1AP);
284 mtdcr (EBC0_CFGDATA, MPS_AP);
285 mtdcr (EBC0_CFGADDR, PB1CR);
286 mtdcr (EBC0_CFGDATA, MPS_CR);
wdenk858b1a62002-09-30 16:12:23 +0000287 }
288 /* set up UART0 (CS2) and UART1 (CS3) */
Stefan Roese918010a2009-09-09 16:25:29 +0200289 mtdcr (EBC0_CFGADDR, PB2AP);
290 mtdcr (EBC0_CFGDATA, UART0_AP);
291 mtdcr (EBC0_CFGADDR, PB2CR);
292 mtdcr (EBC0_CFGDATA, UART0_CR);
293 mtdcr (EBC0_CFGADDR, PB3AP);
294 mtdcr (EBC0_CFGDATA, UART1_AP);
295 mtdcr (EBC0_CFGADDR, PB3CR);
296 mtdcr (EBC0_CFGDATA, UART1_CR);
wdenke39c2842003-06-04 15:05:30 +0000297 bc = in8 (PLD_BOARD_CFG_REG);
wdenk858b1a62002-09-30 16:12:23 +0000298#ifdef SDRAM_DEBUG
299 serial_puts ("\nstart SDRAM Setup\n");
300 serial_puts ("\nBoard Rev: ");
301 write_hex (bc);
302 serial_puts ("\n");
303#endif
304 i = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200305 baseaddr = CONFIG_SYS_SDRAM_BASE;
wdenk858b1a62002-09-30 16:12:23 +0000306 while (sdram_table[i].sz != 0xff) {
307 if (sdram_table[i].boardtype == bc)
308 break;
309 i++;
310 }
311 if (sdram_table[i].boardtype != bc)
312 SDRAM_err ("No SDRAM table found for this board!!!\n");
313#ifdef SDRAM_DEBUG
314 serial_puts (" found table ");
315 write_hex (i);
316 serial_puts (" \n");
317#endif
wdenk874ac262003-07-24 23:38:38 +0000318 /* since the ECC initialisation needs some time,
319 * we show that we're alive
320 */
321 if (sdram_table[i].ecc)
322 serial_puts ("\nInitializing SDRAM, Please stand by");
wdenk858b1a62002-09-30 16:12:23 +0000323 cal_val = sdram_table[i].cal - 1; /* Cas Latency */
324 trp_clocks = sdram_table[i].trp; /* 20ns / 7.5 ns datain[27] */
325 trcd_clocks = sdram_table[i].trcd; /* 20ns /7.5 ns (datain[29]) */
326 tras_clocks = sdram_table[i].tras; /* 44ns /7.5 ns (datain[30]) */
327 /* ctp = ((trp + tras) - trp - trcd) => tras - trcd */
wdenk858b1a62002-09-30 16:12:23 +0000328 /* trc_clocks is sum of trp_clocks + tras_clocks */
329 trc_clocks = trp_clocks + tras_clocks;
330 /* get SDRAM timing register */
Stefan Roese6987e652009-09-24 13:59:57 +0200331 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roese918010a2009-09-09 16:25:29 +0200332 sdram_tim = mfdcr (SDRAM0_CFGDATA) & ~0x018FC01F;
wdenk858b1a62002-09-30 16:12:23 +0000333 /* insert CASL value */
334 sdram_tim |= ((unsigned long) (cal_val)) << 23;
335 /* insert PTA value */
336 sdram_tim |= ((unsigned long) (trp_clocks - 1)) << 18;
337 /* insert CTP value */
338 sdram_tim |=
339 ((unsigned long) (trc_clocks - trp_clocks -
340 trcd_clocks)) << 16;
341 /* insert LDF (always 01) */
342 sdram_tim |= ((unsigned long) 0x01) << 14;
343 /* insert RFTA value */
344 sdram_tim |= ((unsigned long) (trc_clocks - 4)) << 2;
345 /* insert RCD value */
346 sdram_tim |= ((unsigned long) (trcd_clocks - 1)) << 0;
347
348 tmp = ((unsigned long) (sdram_table[i].am - 1) << 13); /* AM = 3 */
349 /* insert SZ value; */
350 tmp |= ((unsigned long) sdram_table[i].sz << 17);
351 /* get SDRAM bank 0 register */
Stefan Roese6987e652009-09-24 13:59:57 +0200352 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200353 sdram_bank = mfdcr (SDRAM0_CFGDATA) & ~0xFFCEE001;
wdenk858b1a62002-09-30 16:12:23 +0000354 sdram_bank |= (baseaddr | tmp | 0x01);
355
356#ifdef SDRAM_DEBUG
357 serial_puts ("sdtr: ");
358 write_4hex (sdram_tim);
359 serial_puts ("\n");
360#endif
361
362 /* write SDRAM timing register */
Stefan Roese6987e652009-09-24 13:59:57 +0200363 mtdcr (SDRAM0_CFGADDR, SDRAM0_TR);
Stefan Roese918010a2009-09-09 16:25:29 +0200364 mtdcr (SDRAM0_CFGDATA, sdram_tim);
wdenk858b1a62002-09-30 16:12:23 +0000365
366#ifdef SDRAM_DEBUG
367 serial_puts ("mb0cf: ");
368 write_4hex (sdram_bank);
369 serial_puts ("\n");
370#endif
371
372 /* write SDRAM bank 0 register */
Stefan Roese6987e652009-09-24 13:59:57 +0200373 mtdcr (SDRAM0_CFGADDR, SDRAM0_B0CR);
Stefan Roese918010a2009-09-09 16:25:29 +0200374 mtdcr (SDRAM0_CFGDATA, sdram_bank);
wdenk858b1a62002-09-30 16:12:23 +0000375
376 if (get_bus_freq (tmp) > 110000000) { /* > 110MHz */
377 /* get SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200378 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200379 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000380 tmp |= 0x07F00000;
381 } else {
382 /* get SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200383 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200384 tmp = mfdcr (SDRAM0_CFGDATA) & ~0x3FF80000;
wdenk858b1a62002-09-30 16:12:23 +0000385 tmp |= 0x05F00000;
386 }
387 /* write SDRAM refresh interval register */
Stefan Roese6987e652009-09-24 13:59:57 +0200388 mtdcr (SDRAM0_CFGADDR, SDRAM0_RTR);
Stefan Roese918010a2009-09-09 16:25:29 +0200389 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000390 /* enable ECC if used */
wdenke39c2842003-06-04 15:05:30 +0000391#if defined(ENABLE_ECC) && !defined(CONFIG_BOOT_PCI)
wdenk858b1a62002-09-30 16:12:23 +0000392 if (sdram_table[i].ecc) {
393 /* disable checking for all banks */
wdenke39c2842003-06-04 15:05:30 +0000394 unsigned long *p;
wdenk858b1a62002-09-30 16:12:23 +0000395#ifdef SDRAM_DEBUG
396 serial_puts ("disable ECC.. ");
397#endif
Stefan Roese6987e652009-09-24 13:59:57 +0200398 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200399 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000400 tmp &= 0xff0fffff; /* disable all banks */
Stefan Roese6987e652009-09-24 13:59:57 +0200401 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
wdenk858b1a62002-09-30 16:12:23 +0000402 /* set up SDRAM Controller with ECC enabled */
403#ifdef SDRAM_DEBUG
404 serial_puts ("setup SDRAM Controller.. ");
405#endif
Stefan Roese918010a2009-09-09 16:25:29 +0200406 mtdcr (SDRAM0_CFGDATA, tmp);
Stefan Roese6987e652009-09-24 13:59:57 +0200407 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200408 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x90800000;
Stefan Roese6987e652009-09-24 13:59:57 +0200409 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200410 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000411 udelay (600);
412#ifdef SDRAM_DEBUG
413 serial_puts ("fill the memory..\n");
414#endif
415 serial_puts (".");
416 /* now, fill all the memory */
417 tmp = ((4 * MEGA_BYTE) << sdram_table[i].sz);
418 p = (unsigned long) 0;
419 while ((unsigned long) p < tmp) {
420 *p++ = 0L;
421 if (!((unsigned long) p % 0x00800000)) /* every 8MByte */
422 serial_puts (".");
wdenk858b1a62002-09-30 16:12:23 +0000423 }
424 /* enable bank 0 */
425 serial_puts (".");
426#ifdef SDRAM_DEBUG
427 serial_puts ("enable ECC\n");
428#endif
429 udelay (400);
Stefan Roese6987e652009-09-24 13:59:57 +0200430 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200431 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000432 tmp |= 0x00800000; /* enable bank 0 */
Stefan Roese918010a2009-09-09 16:25:29 +0200433 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000434 udelay (400);
435 } else
436#endif
437 {
438 /* enable SDRAM controller with no ECC, 32-bit SDRAM width, 16 byte burst */
Stefan Roese6987e652009-09-24 13:59:57 +0200439 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200440 tmp = (mfdcr (SDRAM0_CFGDATA) & ~0xFFE00000) | 0x80C00000;
Stefan Roese6987e652009-09-24 13:59:57 +0200441 mtdcr (SDRAM0_CFGADDR, SDRAM0_CFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200442 mtdcr (SDRAM0_CFGDATA, tmp);
wdenk858b1a62002-09-30 16:12:23 +0000443 udelay (400);
444 }
445 serial_puts ("\n");
446 return (0);
447}
448
wdenkda55c6e2004-01-20 23:12:12 +0000449int board_early_init_f (void)
wdenk858b1a62002-09-30 16:12:23 +0000450{
451 init_sdram ();
452
453 /*-------------------------------------------------------------------------+
454 | Interrupt controller setup for the PIP405 board.
455 | Note: IRQ 0-15 405GP internally generated; active high; level sensitive
456 | IRQ 16 405GP internally generated; active low; level sensitive
457 | IRQ 17-24 RESERVED
458 | IRQ 25 (EXT IRQ 0) SouthBridge; active low; level sensitive
459 | IRQ 26 (EXT IRQ 1) NMI: active low; level sensitive
460 | IRQ 27 (EXT IRQ 2) SMI: active Low; level sensitive
461 | IRQ 28 (EXT IRQ 3) PCI SLOT 3; active low; level sensitive
462 | IRQ 29 (EXT IRQ 4) PCI SLOT 2; active low; level sensitive
463 | IRQ 30 (EXT IRQ 5) PCI SLOT 1; active low; level sensitive
464 | IRQ 31 (EXT IRQ 6) PCI SLOT 0; active low; level sensitive
465 | Note for MIP405 board:
466 | An interrupt taken for the SouthBridge (IRQ 25) indicates that
467 | the Interrupt Controller in the South Bridge has caused the
468 | interrupt. The IC must be read to determine which device
469 | caused the interrupt.
470 |
471 +-------------------------------------------------------------------------*/
Stefan Roese707fd362009-09-24 09:55:50 +0200472 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
473 mtdcr (UIC0ER, 0x00000000); /* disable all ints */
474 mtdcr (UIC0CR, 0x00000000); /* set all to be non-critical (for now) */
475 mtdcr (UIC0PR, 0xFFFFFF80); /* set int polarities */
476 mtdcr (UIC0TR, 0x10000000); /* set int trigger levels */
477 mtdcr (UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
478 mtdcr (UIC0SR, 0xFFFFFFFF); /* clear all ints */
wdenk858b1a62002-09-30 16:12:23 +0000479 return 0;
480}
481
David Müllera24c8782011-12-22 13:38:21 +0100482int board_early_init_r(void)
483{
484 int mode;
485
486 /*
487 * since we are relocated, we can finally enable i-cache
488 * and set up the flash CS correctly
489 */
490 icache_enable();
491 setup_cs_reloc();
492 /* get and display boot mode */
493 mode = get_boot_mode();
494 if (mode & BOOT_PCI)
495 printf("PCI Boot %s Map\n", (mode & BOOT_MPS) ?
496 "MPS" : "Flash");
497 else
498 printf("%s Boot\n", (mode & BOOT_MPS) ?
499 "MPS" : "Flash");
500
501 return 0;
502}
wdenk858b1a62002-09-30 16:12:23 +0000503
504/*
505 * Get some PLD Registers
506 */
507
508unsigned short get_pld_parvers (void)
509{
510 unsigned short result;
511 unsigned char rc;
512
513 rc = in8 (PLD_PART_REG);
514 result = (unsigned short) rc << 8;
515 rc = in8 (PLD_VERS_REG);
516 result |= rc;
517 return result;
518}
519
520
wdenk858b1a62002-09-30 16:12:23 +0000521void user_led0 (unsigned char on)
522{
523 if (on)
524 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x4));
525 else
526 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfb));
527}
528
529
530void ide_set_reset (int idereset)
531{
532 /* if reset = 1 IDE reset will be asserted */
533 if (idereset)
534 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) | 0x1));
535 else {
536 udelay (10000);
537 out8 (PLD_COM_MODE_REG, (in8 (PLD_COM_MODE_REG) & 0xfe));
538 }
539}
540
541
542/* ------------------------------------------------------------------------- */
543
wdenke39c2842003-06-04 15:05:30 +0000544void get_pcbrev_var(unsigned char *pcbrev, unsigned char *var)
wdenk858b1a62002-09-30 16:12:23 +0000545{
wdenke39c2842003-06-04 15:05:30 +0000546#if !defined(CONFIG_MIP405T)
547 unsigned char bc,rc,tmp;
wdenk858b1a62002-09-30 16:12:23 +0000548 int i;
wdenk858b1a62002-09-30 16:12:23 +0000549
wdenke39c2842003-06-04 15:05:30 +0000550 bc = in8 (PLD_BOARD_CFG_REG);
551 tmp = ~bc;
552 tmp &= 0xf;
wdenk858b1a62002-09-30 16:12:23 +0000553 rc = 0;
554 for (i = 0; i < 4; i++) {
555 rc <<= 1;
wdenke39c2842003-06-04 15:05:30 +0000556 rc += (tmp & 0x1);
557 tmp >>= 1;
wdenk858b1a62002-09-30 16:12:23 +0000558 }
559 rc++;
wdenk60164a82003-10-08 23:26:14 +0000560 if(( (((bc>>4) & 0xf)==0x2) /* Rev C PCB or */
561 || (((bc>>4) & 0xf)==0x1)) /* Rev B PCB with */
wdenk7d076412003-05-23 11:38:58 +0000562 && (rc==0x1)) /* Population Option 1 is a -3 */
563 rc=3;
wdenke39c2842003-06-04 15:05:30 +0000564 *pcbrev=(bc >> 4) & 0xf;
565 *var=rc;
566#else
567 unsigned char bc;
568 bc = in8 (PLD_BOARD_CFG_REG);
569 *pcbrev=(bc >> 4) & 0xf;
wdenk874ac262003-07-24 23:38:38 +0000570 *var=16-(bc & 0xf);
wdenke39c2842003-06-04 15:05:30 +0000571#endif
572}
573
574/*
575 * Check Board Identity:
576 */
577/* serial String: "MIP405_1000" OR "MIP405T_1000" */
578#if !defined(CONFIG_MIP405T)
579#define BOARD_NAME "MIP405"
580#else
581#define BOARD_NAME "MIP405T"
582#endif
583
584int checkboard (void)
585{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200586 char s[50];
wdenke39c2842003-06-04 15:05:30 +0000587 unsigned char bc, var;
588 int i;
589 backup_t *b = (backup_t *) s;
590
591 puts ("Board: ");
592 get_pcbrev_var(&bc,&var);
Wolfgang Denk76af2782010-07-24 21:55:43 +0200593 i = getenv_f("serial#", (char *)s, 32);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200594 if ((i == 0) || strncmp ((char *)s, BOARD_NAME,sizeof(BOARD_NAME))) {
wdenk858b1a62002-09-30 16:12:23 +0000595 get_backup_values (b);
596 if (strncmp (b->signature, "MPL\0", 4) != 0) {
wdenke39c2842003-06-04 15:05:30 +0000597 puts ("### No HW ID - assuming " BOARD_NAME);
598 printf ("-%d Rev %c", var, 'A' + bc);
wdenk858b1a62002-09-30 16:12:23 +0000599 } else {
wdenke39c2842003-06-04 15:05:30 +0000600 b->serial_name[sizeof(BOARD_NAME)-1] = 0;
601 printf ("%s-%d Rev %c SN: %s", b->serial_name, var,
602 'A' + bc, &b->serial_name[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000603 }
604 } else {
wdenke39c2842003-06-04 15:05:30 +0000605 s[sizeof(BOARD_NAME)-1] = 0;
606 printf ("%s-%d Rev %c SN: %s", s, var,'A' + bc,
607 &s[sizeof(BOARD_NAME)]);
wdenk858b1a62002-09-30 16:12:23 +0000608 }
609 bc = in8 (PLD_EXT_CONF_REG);
610 printf (" Boot Config: 0x%x\n", bc);
611 return (0);
612}
613
614
615/* ------------------------------------------------------------------------- */
616/* ------------------------------------------------------------------------- */
617/*
618 initdram(int board_type) reads EEPROM via I2c. EEPROM contains all of
619 the necessary info for SDRAM controller configuration
620*/
621/* ------------------------------------------------------------------------- */
622/* ------------------------------------------------------------------------- */
623static int test_dram (unsigned long ramsize);
624
Becky Brucebd99ae72008-06-09 16:03:40 -0500625phys_size_t initdram (int board_type)
wdenk858b1a62002-09-30 16:12:23 +0000626{
627
628 unsigned long bank_reg[4], tmp, bank_size;
Stefan Roese208cd452011-11-15 08:03:39 +0000629 int i;
wdenk858b1a62002-09-30 16:12:23 +0000630 unsigned long TotalSize;
631
wdenk858b1a62002-09-30 16:12:23 +0000632 /* 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;
Stefan Roese208cd452011-11-15 08:03:39 +0000648 }
wdenk858b1a62002-09-30 16:12:23 +0000649 }
Stefan Roese6987e652009-09-24 13:59:57 +0200650 mtdcr (SDRAM0_CFGADDR, SDRAM0_ECCCFG);
Stefan Roese918010a2009-09-09 16:25:29 +0200651 tmp = mfdcr (SDRAM0_CFGDATA);
wdenk858b1a62002-09-30 16:12:23 +0000652
653 if (!tmp)
654 printf ("No ");
655 printf ("ECC ");
656
657 test_dram (TotalSize * MEGA_BYTE);
658 return (TotalSize * MEGA_BYTE);
659}
660
661/* ------------------------------------------------------------------------- */
662
wdenk858b1a62002-09-30 16:12:23 +0000663
664static int test_dram (unsigned long ramsize)
665{
666#ifdef SDRAM_DEBUG
667 mem_test (0L, ramsize, 1);
668#endif
669 /* not yet implemented */
670 return (1);
671}
672
wdenk874ac262003-07-24 23:38:38 +0000673/* used to check if the time in RTC is valid */
674static unsigned long start;
675static struct rtc_time tm;
676
wdenk858b1a62002-09-30 16:12:23 +0000677int misc_init_r (void)
678{
wdenk2c9b05d2003-09-10 22:30:53 +0000679 /* adjust flash start and size as well as the offset */
680 gd->bd->bi_flashstart=0-flash_info[0].size;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200681 gd->bd->bi_flashsize=flash_info[0].size-CONFIG_SYS_MONITOR_LEN;
wdenk2c9b05d2003-09-10 22:30:53 +0000682 gd->bd->bi_flashoffset=0;
683
wdenk874ac262003-07-24 23:38:38 +0000684 /* check, if RTC is running */
685 rtc_get (&tm);
686 start=get_timer(0);
wdenke39c2842003-06-04 15:05:30 +0000687 /* if MIP405 has booted from PCI, reset CCR0[24] as described in errata PCI_18 */
Stefan Roese918010a2009-09-09 16:25:29 +0200688 if (mfdcr(CPC0_PSR) & PSR_ROM_LOC)
Matthias Fuchs730b2d22009-07-22 17:27:56 +0200689 mtspr(SPRN_CCR0, (mfspr(SPRN_CCR0) & ~0x80));
wdenke39c2842003-06-04 15:05:30 +0000690
wdenk858b1a62002-09-30 16:12:23 +0000691 return (0);
692}
693
694
695void print_mip405_rev (void)
696{
wdenke39c2842003-06-04 15:05:30 +0000697 unsigned char part, vers, pcbrev, var;
wdenk858b1a62002-09-30 16:12:23 +0000698
wdenke39c2842003-06-04 15:05:30 +0000699 get_pcbrev_var(&pcbrev,&var);
wdenk858b1a62002-09-30 16:12:23 +0000700 part = in8 (PLD_PART_REG);
701 vers = in8 (PLD_VERS_REG);
wdenke39c2842003-06-04 15:05:30 +0000702 printf ("Rev: " BOARD_NAME "-%d Rev %c PLD %d Vers %d\n",
703 var, pcbrev + 'A', part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000704}
705
wdenke97d3d92004-02-23 22:22:28 +0000706
wdenk874ac262003-07-24 23:38:38 +0000707extern int mk_date (char *, struct rtc_time *);
wdenk858b1a62002-09-30 16:12:23 +0000708
709int last_stage_init (void)
710{
wdenk874ac262003-07-24 23:38:38 +0000711 unsigned long stop;
712 struct rtc_time newtm;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200713 char *s;
Peter Tyser43c08d72009-09-21 11:20:33 -0500714
wdenkb02744a2003-04-05 00:53:31 +0000715 /* write correct LED configuration */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200716 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x14, 0x2402) != 0) {
wdenk858b1a62002-09-30 16:12:23 +0000717 printf ("Error writing to the PHY\n");
718 }
wdenkb02744a2003-04-05 00:53:31 +0000719 /* since LED/CFG2 is not connected on the -2,
720 * write to correct capability information */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200721 if (miiphy_write("ppc_4xx_eth0", 0x1, 0x4, 0x01E1) != 0) {
wdenkb02744a2003-04-05 00:53:31 +0000722 printf ("Error writing to the PHY\n");
723 }
wdenk858b1a62002-09-30 16:12:23 +0000724 print_mip405_rev ();
Jean-Christophe PLAGNIOL-VILLARDd4303af2009-05-16 12:14:56 +0200725 stdio_print_current_devices ();
wdenk858b1a62002-09-30 16:12:23 +0000726 check_env ();
wdenk874ac262003-07-24 23:38:38 +0000727 /* check if RTC time is valid */
728 stop=get_timer(start);
729 while(stop<1200) { /* we wait 1.2 sec to check if the RTC is running */
730 udelay(1000);
731 stop=get_timer(start);
732 }
733 rtc_get (&newtm);
734 if(tm.tm_sec==newtm.tm_sec) {
735 s=getenv("defaultdate");
736 if(!s)
737 mk_date ("010112001970", &newtm);
738 else
739 if(mk_date (s, &newtm)!=0) {
740 printf("RTC: Bad date format in defaultdate\n");
741 return 0;
742 }
743 rtc_reset ();
744 rtc_set(&newtm);
745 }
wdenk858b1a62002-09-30 16:12:23 +0000746 return 0;
747}
748
749/***************************************************************************
750 * some helping routines
751 */
752
753int overwrite_console (void)
754{
York Sun4a598092013-04-01 11:29:11 -0700755 /* return true if console should be overwritten */
756 return ((in8(PLD_EXT_CONF_REG) & 0x1) == 0);
wdenk858b1a62002-09-30 16:12:23 +0000757}
758
759
760/************************************************************************
761* Print MIP405 Info
762************************************************************************/
763void print_mip405_info (void)
764{
765 unsigned char part, vers, cfg, irq_reg, com_mode, ext;
766
767 part = in8 (PLD_PART_REG);
768 vers = in8 (PLD_VERS_REG);
769 cfg = in8 (PLD_BOARD_CFG_REG);
770 irq_reg = in8 (PLD_IRQ_REG);
771 com_mode = in8 (PLD_COM_MODE_REG);
772 ext = in8 (PLD_EXT_CONF_REG);
773
wdenke39c2842003-06-04 15:05:30 +0000774 printf ("PLD Part %d version %d\n", part & 0x7F, vers);
wdenk858b1a62002-09-30 16:12:23 +0000775 printf ("Board Revision %c\n", ((cfg >> 4) & 0xf) + 'A');
776 printf ("Population Options %d %d %d %d\n", (cfg) & 0x1,
777 (cfg >> 1) & 0x1, (cfg >> 2) & 0x1, (cfg >> 3) & 0x1);
778 printf ("User LED %s\n", (com_mode & 0x4) ? "on" : "off");
779 printf ("UART Clocks %d\n", (com_mode >> 4) & 0x3);
wdenke39c2842003-06-04 15:05:30 +0000780#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000781 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
782 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
783 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
784 (ext >> 6) & 0x1, (ext >> 7) & 0x1);
785 printf ("SER1 uses handshakes %s\n",
786 (ext & 0x80) ? "DTR/DSR" : "RTS/CTS");
wdenke39c2842003-06-04 15:05:30 +0000787#else
wdenk874ac262003-07-24 23:38:38 +0000788 printf ("User Config Switch %d %d %d %d %d %d %d %d\n",
wdenke39c2842003-06-04 15:05:30 +0000789 (ext) & 0x1, (ext >> 1) & 0x1, (ext >> 2) & 0x1,
790 (ext >> 3) & 0x1, (ext >> 4) & 0x1, (ext >> 5) & 0x1,
wdenk874ac262003-07-24 23:38:38 +0000791 (ext >> 6) & 0x1,(ext >> 7) & 0x1);
wdenke39c2842003-06-04 15:05:30 +0000792#endif
wdenk858b1a62002-09-30 16:12:23 +0000793 printf ("IDE Reset %s\n", (ext & 0x01) ? "asserted" : "not asserted");
794 printf ("IRQs:\n");
795 printf (" PIIX INTR: %s\n", (irq_reg & 0x80) ? "inactive" : "active");
wdenke39c2842003-06-04 15:05:30 +0000796#if !defined(CONFIG_MIP405T)
wdenk858b1a62002-09-30 16:12:23 +0000797 printf (" UART0 IRQ: %s\n", (irq_reg & 0x40) ? "inactive" : "active");
798 printf (" UART1 IRQ: %s\n", (irq_reg & 0x20) ? "inactive" : "active");
wdenke39c2842003-06-04 15:05:30 +0000799#endif
wdenk858b1a62002-09-30 16:12:23 +0000800 printf (" PIIX SMI: %s\n", (irq_reg & 0x10) ? "inactive" : "active");
801 printf (" PIIX INIT: %s\n", (irq_reg & 0x8) ? "inactive" : "active");
802 printf (" PIIX NMI: %s\n", (irq_reg & 0x4) ? "inactive" : "active");
803}