3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Adapted from FADS and other board config files to GTH by thomas@corelatus.com
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include "ee_access.h"
35 #define printf(a,...) /* nothing */
41 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
48 /* Turn on leds and setup for reading rev and id */
50 #define PB_OUTS (PB_BLUE_LED|PB_ID_GND)
51 #define PB_INS (PB_ID_0|PB_ID_1|PB_ID_2|PB_ID_3|PB_REV_1|PB_REV_0)
53 immap
->im_cpm
.cp_pbpar
&= ~(PB_OUTS
| PB_INS
);
55 immap
->im_cpm
.cp_pbdir
&= ~PB_INS
;
57 immap
->im_cpm
.cp_pbdir
|= PB_OUTS
;
58 immap
->im_cpm
.cp_pbodr
|= PB_OUTS
;
59 immap
->im_cpm
.cp_pbdat
&= ~PB_OUTS
;
61 /* Hold 100 Mbit in reset until fpga is loaded */
62 immap
->im_ioport
.iop_pcpar
&= ~PC_ENET100_RESET
;
63 immap
->im_ioport
.iop_pcdir
|= PC_ENET100_RESET
;
64 immap
->im_ioport
.iop_pcso
&= ~PC_ENET100_RESET
;
65 immap
->im_ioport
.iop_pcdat
&= ~PC_ENET100_RESET
;
67 /* Turn on front led to show that we are alive */
68 immap
->im_ioport
.iop_papar
&= ~PA_FRONT_LED
;
69 immap
->im_ioport
.iop_padir
|= PA_FRONT_LED
;
70 immap
->im_ioport
.iop_paodr
|= PA_FRONT_LED
;
71 immap
->im_ioport
.iop_padat
&= ~PA_FRONT_LED
;
73 Pbdat
= immap
->im_cpm
.cp_pbdat
;
75 if (!(Pbdat
& PB_ID_0
))
77 if (!(Pbdat
& PB_ID_1
))
79 if (!(Pbdat
& PB_ID_2
))
81 if (!(Pbdat
& PB_ID_3
))
89 /* Turn ID off since we dont need it anymore */
90 immap
->im_cpm
.cp_pbdat
|= PB_ID_GND
;
92 printf ("GTH board, rev %d, id=0x%01x\n", Rev
, Id
);
96 #define _NOT_USED_ 0xffffffff
97 const uint sdram_table
[] = {
98 /* Single read, offset 0 */
99 0x0f3dfc04, 0x0eefbc04, 0x01bf7c04, 0x0feafc00,
100 0x1fb5fc45, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
102 /* Burst read, Offset 0x8, 4 reads */
103 0x0f3dfc04, 0x0eefbc04, 0x00bf7c04, 0x00ffec00,
104 0x00fffc00, 0x01eafc00, 0x1fb5fc00, 0xfffffc45,
105 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
107 /* Not used part of burst read is used for MRS, Offset 0x14 */
108 0xefeabc34, 0x1fb57c34, 0xfffffc05, _NOT_USED_
,
109 /* _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_, */
111 /* Single write, Offset 0x18 */
112 0x0f3dfc04, 0x0eebbc00, 0x01a27c04, 0x1fb5fc45,
113 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
115 /* Burst write, Offset 0x20. 4 writes */
116 0x0f3dfc04, 0x0eebbc00, 0x00b77c00, 0x00fffc00,
117 0x00fffc00, 0x01eafc04, 0x1fb5fc45, _NOT_USED_
,
118 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
120 /* Not used part of burst write is used for precharge, Offset 0x2C */
121 0x0ff5fc04, 0xfffffc05, _NOT_USED_
, _NOT_USED_
,
122 /* _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_, */
124 /* Period timer service. Offset 0x30. Refresh. Wait at least 70 ns after refresh command */
125 0x1ffd7c04, 0xfffffc04, 0xfffffc04, 0xfffffc05,
126 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
127 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
129 /* Exception, Offset 0x3C */
130 0xfffffc04, 0xfffffc05, _NOT_USED_
, _NOT_USED_
133 const uint fpga_table
[] = {
134 /* Single read, offset 0 */
135 0x0cffec04, 0x00ffec04, 0x00ffec04, 0x00ffec04,
136 0x00fffc04, 0x00fffc00, 0x00ffec04, 0xffffec05,
138 /* Burst read, Offset 0x8 */
139 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
140 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
141 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
142 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
144 /* Single write, Offset 0x18 */
145 0x0cffec04, 0x00ffec04, 0x00ffec04, 0x00ffec04,
146 0x00fffc04, 0x00fffc00, 0x00ffec04, 0xffffec05,
148 /* Burst write, Offset 0x20. */
149 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
150 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
151 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
152 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
154 /* Period timer service. Offset 0x30. */
155 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
156 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
157 _NOT_USED_
, _NOT_USED_
, _NOT_USED_
, _NOT_USED_
,
159 /* Exception, Offset 0x3C */
160 0xfffffc04, 0xfffffc05, _NOT_USED_
, _NOT_USED_
163 int _initsdram (uint base
, uint
* noMbytes
)
165 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
166 volatile memctl8xx_t
*mc
= &immap
->im_memctl
;
167 volatile u32
*memptr
;
169 mc
->memc_mptpr
= MPTPR_PTP_DIV16
; /* (16-17) */
173 GPL_0 is connected instead of A19 to SDRAM.
174 According to table 16-17, AMx should be 001, i.e. type 1
175 and GPL_0 should hold address A10 when multiplexing */
177 mc
->memc_mamr
= (0x2E << MAMR_PTA_SHIFT
) | MAMR_PTAE
| MAMR_AMA_TYPE_1
| MAMR_G0CLA_A10
| MAMR_RLFA_1X
| MAMR_WLFA_1X
| MAMR_TLFA_1X
; /* (16-13) */
179 upmconfig (UPMA
, (uint
*) sdram_table
,
180 sizeof (sdram_table
) / sizeof (uint
));
182 /* Perform init of sdram ( Datasheet Page 9 )
184 mc
->memc_mcr
= 0x8000212C; /* run upm a at 0x2C (16-15) */
186 /* Run 2 refresh cycles */
187 mc
->memc_mcr
= 0x80002130; /* run upm a at 0x30 (16-15) */
188 mc
->memc_mcr
= 0x80002130; /* run upm a at 0x30 (16-15) */
190 /* Set Mode register */
191 mc
->memc_mar
= 0x00000088; /* set mode register (address) to 0x022 (16-17) */
192 /* Lower 2 bits are not connected to chip */
193 mc
->memc_mcr
= 0x80002114; /* run upm a at 0x14 (16-15) */
195 /* CS1, base 0x0000000 - 64 Mbyte, use UPM A */
196 mc
->memc_or1
= 0xfc000000 | OR_CSNT_SAM
;
197 mc
->memc_br1
= BR_MS_UPMA
| BR_V
; /* SDRAM base always 0 */
199 /* Test if we really have 64 MB SDRAM */
203 memptr
= (u32
*) 0x2000000; /* First u32 in upper 32 MB */
204 *memptr
= 0x12345678;
207 if (*memptr
== 0x12345678) {
208 /* Wrapped, only have 32 MB */
209 mc
->memc_or1
= 0xfe000000 | OR_CSNT_SAM
;
216 /* Setup FPGA in UPMB */
217 upmconfig (UPMB
, (uint
*) fpga_table
,
218 sizeof (fpga_table
) / sizeof (uint
));
221 mc
->memc_mbmr
= MBMR_GPL_B4DIS
; /* (16-13) */
223 /* CS2, base FPGA_2_BASE - 4 MByte, use UPM B 32 Bit */
224 mc
->memc_or2
= 0xffc00000 | OR_BI
;
225 mc
->memc_br2
= FPGA_2_BASE
| BR_MS_UPMB
| BR_V
;
227 /* CS3, base FPGA_3_BASE - 4 MByte, use UPM B 16 bit */
228 mc
->memc_or3
= 0xffc00000 | OR_BI
;
229 mc
->memc_br3
= FPGA_3_BASE
| BR_MS_UPMB
| BR_V
| BR_PS_16
;
234 /* ------------------------------------------------------------------------- */
236 void _sdramdisable (void)
238 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
239 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
241 memctl
->memc_br1
= 0x00000000;
243 /* maybe we should turn off upmb here or something */
246 /* ------------------------------------------------------------------------- */
248 int initsdram (uint base
, uint
* noMbytes
)
252 #ifdef CONFIG_START_IN_RAM
253 /* SDRAM is already setup. Dont touch it */
257 if (!_initsdram (base
, noMbytes
)) {
268 long int initdram (int board_type
)
274 /* GTH only have SDRAM */
277 if (!initsdram (0x00000000, &sdramsz
)) {
278 printf ("(%u MB SDRAM) ", sdramsz
);
280 /********************************
281 *SDRAM ERROR, HALT PROCESSOR
282 *********************************/
283 printf ("SDRAM ERROR\n");
287 #ifndef CONFIG_START_IN_RAM
289 #define U32_S ((sdramsz<<18)-1)
292 /* Do a simple memory test */
293 for (i
= (u32
*) 0, j
= 0; (u32
) i
< U32_S
; i
+= 2, j
+= 2) {
295 *(i
+ 1) = ~(j
+ (j
<< 18));
302 for (i
= (u32
*) 0, j
= 0; (u32
) i
< U32_S
; i
+= 2, j
+= 2) {
304 if (k
!= (j
+ (j
<< 17))) {
305 printf ("Mem test error, i=0x%x, 0x%x\n, 0x%x", (u32
) i
, j
, k
);
309 if (k
!= ~(j
+ (j
<< 18))) {
310 printf ("Mem test error(+1), i=0x%x, 0x%x\n, 0x%x",
320 for (i
= (u32
*) 0; (u32
) i
< U32_S
; i
++) {
323 #endif /* !start in ram */
327 return (sdramsz
<< 20);
330 #define POWER_OFFSET 0xF0000
331 #define SW_WATCHDOG_REASON 13
333 #define BOOTDATA_OFFSET 0xF8000
334 #define MAX_ATTEMPTS 5
336 #define FAILSAFE_BOOT 1
337 #define SYSTEM_BOOT 2
339 #define WRITE_FLASH16(a, d) \
342 *((volatile u16 *) (a)) = (d);\
345 static void write_bootdata (volatile u16
* addr
, u8 System
, u8 Count
)
348 volatile u16
*flash
= (u16
*) (CFG_FLASH_BASE
);
350 if ((System
!= FAILSAFE_BOOT
) & (System
!= SYSTEM_BOOT
)) {
351 printf ("Invalid system data %u, setting failsafe\n", System
);
352 System
= FAILSAFE_BOOT
;
355 if ((Count
< 1) | (Count
> MAX_ATTEMPTS
)) {
356 printf ("Invalid boot count %u, setting 1\n", Count
);
360 if (System
== FAILSAFE_BOOT
) {
361 printf ("Setting failsafe boot in flash\n");
363 printf ("Setting system boot in flash\n");
365 printf ("Boot attempt %d\n", Count
);
367 data
= (System
<< 8) | Count
;
369 WRITE_FLASH16 (&flash
[0x555], 0xAAAA);
370 WRITE_FLASH16 (&flash
[0x2AA], 0x5555);
371 WRITE_FLASH16 (&flash
[0x555], 0xA0A0);
373 WRITE_FLASH16 (addr
, data
);
376 static void maybe_update_restart_reason (volatile u32
* addr32
)
378 /* Update addr if sw wd restart */
379 volatile u16
*flash
= (u16
*) (CFG_FLASH_BASE
);
380 volatile u16
*addr_16
= (u16
*) addr32
;
383 /* Dont reset register now */
384 rsr
= ((volatile immap_t
*) CFG_IMMR
)->im_clkrst
.car_rsr
;
389 /* Was really a sw wd restart, update reason */
391 printf ("Last restart by software watchdog\n");
394 WRITE_FLASH16 (&flash
[0x555], 0xAAAA);
395 WRITE_FLASH16 (&flash
[0x2AA], 0x5555);
396 WRITE_FLASH16 (&flash
[0x555], 0xA0A0);
398 WRITE_FLASH16 (addr_16
, 0);
405 WRITE_FLASH16 (&flash
[0x555], 0xAAAA);
406 WRITE_FLASH16 (&flash
[0x2AA], 0x5555);
407 WRITE_FLASH16 (&flash
[0x555], 0xA0A0);
409 WRITE_FLASH16 (addr_16
+ 1, SW_WATCHDOG_REASON
);
414 static void check_restart_reason (void)
416 /* Update restart reason if sw watchdog was
422 raddr
= (u32
*) (CFG_FLASH_BASE
+ POWER_OFFSET
);
424 if (*raddr
== 0xFFFFFFFF) {
425 /* Nothing written */
426 maybe_update_restart_reason (raddr
);
428 /* Search for latest written reason */
430 while ((*(raddr
+ 2) != 0xFFFFFFFF) & (i
< 2000)) {
435 /* Whoa, dont write any more */
436 printf ("*** No free restart reason found ***\n");
438 /* Check if written */
440 /* Erased by kernel, no new reason written */
441 maybe_update_restart_reason (raddr
+ 2);
447 static void check_boot_tries (void)
449 /* Count the number of boot attemps
450 switch system if too many */
459 addr
= (u16
*) (CFG_FLASH_BASE
+ BOOTDATA_OFFSET
);
461 if (*addr
== 0xFFFF) {
462 printf ("*** No bootdata exists. ***\n");
463 write_bootdata (addr
, FAILSAFE_BOOT
, 1);
465 /* Search for latest written bootdata */
467 while ((*(addr
+ 1) != 0xFFFF) & (i
< 8000)) {
472 /* Whoa, dont write any more */
473 printf ("*** No bootdata found. Not updating flash***\n");
475 /* See how many times we have tried to boot real system */
479 if ((system
!= SYSTEM_BOOT
) & (system
!= FAILSAFE_BOOT
)) {
480 printf ("*** Wrong system %d\n", system
);
481 system
= FAILSAFE_BOOT
;
490 /* Try same system again if needed */
495 /* Switch system and reset tries */
498 printf ("***Too many boot attempts, switching system***\n");
501 /* Switch system, start over and hope it works */
502 printf ("***Unexpected data on addr 0x%x, %u***\n",
508 write_bootdata (addr
+ 1, system
, count
);
509 if (system
== SYSTEM_BOOT
) {
515 printf ("Booting failsafe system\n");
516 setenv ("bootargs", "panic=1 root=/dev/hda7");
517 setenv ("bootcmd", "disk 100000 0:5;bootm 100000");
519 printf ("Using normal system\n");
520 setenv ("bootargs", "panic=1 root=/dev/hda4");
521 setenv ("bootcmd", "disk 100000 0:2;bootm 100000");
525 int misc_init_r (void)
531 volatile immap_t
*immap
= (immap_t
*) CFG_IMMR
;
534 immap
->im_ioport
.iop_papar
&= ~(PA_FL_CONFIG
| PA_FL_CE
);
535 immap
->im_ioport
.iop_padir
|= (PA_FL_CONFIG
| PA_FL_CE
);
536 immap
->im_ioport
.iop_paodr
&= ~(PA_FL_CONFIG
| PA_FL_CE
);
538 /* Enable fpga, active low */
539 immap
->im_ioport
.iop_padat
&= ~PA_FL_CE
;
541 /* Start configuration */
542 immap
->im_ioport
.iop_padat
&= ~PA_FL_CONFIG
;
545 immap
->im_ioport
.iop_padat
|= (PA_FL_CONFIG
| PA_FL_CE
);
547 /* Check if we need to boot failsafe system */
550 /* Check if we need to update restart reason */
551 check_restart_reason ();
553 if (ee_init_data ()) {
554 printf ("EEPROM init failed\n");
558 /* Read the pages where ethernet address is stored */
560 for (page
= EE_USER_PAGE_0
; page
<= EE_USER_PAGE_0
+ 2; page
++) {
561 /* Copy from nvram to scratchpad */
562 Tx
[0] = RECALL_MEMORY
;
564 if (ee_do_command (Tx
, 2, NULL
, 0, TRUE
)) {
565 printf ("EE user page %d recall failed\n", page
);
569 Tx
[0] = READ_SCRATCHPAD
;
570 if (ee_do_command (Tx
, 2, Rx
+ read
, 9, TRUE
)) {
571 printf ("EE user page %d read failed\n", page
);
574 /* Crc in 9:th byte */
575 if (!ee_crc_ok (Rx
+ read
, 8, *(Rx
+ read
+ 8))) {
576 printf ("EE read failed, page %d. CRC error\n", page
);
582 /* Add eos after eth addr */
585 printf ("Ethernet addr read from eeprom: %s\n\n", Rx
);
589 (Rx
[8] != ':') | (Rx
[11] != ':') | (Rx
[14] != ':')) {
590 printf ("*** ethernet addr invalid, using default ***\n");
592 setenv ("ethaddr", (char *)Rx
);