3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <asm/processor.h>
30 #include <spd_sdram.h>
31 #include <status_led.h>
36 DECLARE_GLOBAL_DATA_PTR
;
38 extern flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
40 unsigned char sha1_checksum
[SHA1_SUM_LEN
];
42 /* swap 4 Bits (Bit0 = Bit3, Bit1 = Bit2, Bit2 = Bit1 and Bit3 = Bit0) */
43 unsigned char swapbits
[16] = {0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
44 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf};
46 static void set_leds (int val
)
48 out32(GPIO0_OR
, (in32 (GPIO0_OR
) & ~0x78000000) | (val
<< 27));
51 #define GET_LEDS ((in32 (GPIO0_OR) & 0x78000000) >> 27)
53 void __led_init (led_id_t mask
, int state
)
57 if (state
== STATUS_LED_ON
)
64 void __led_set (led_id_t mask
, int state
)
68 if (state
== STATUS_LED_ON
)
70 else if (state
== STATUS_LED_OFF
)
75 void __led_toggle (led_id_t mask
)
83 static void status_led_blink (void)
88 /* set all LED which are on, to state BLINKING */
89 for (i
= 0; i
< 4; i
++) {
90 if (val
& 0x01) status_led_set (3 - i
, STATUS_LED_BLINKING
);
91 else status_led_set (3 - i
, STATUS_LED_OFF
);
96 #if defined(CONFIG_SHOW_BOOT_PROGRESS)
97 void show_boot_progress (int val
)
99 /* find all valid Codes for val in README */
100 if (val
== -30) return;
102 /* smthing goes wrong */
108 /* validating Image */
109 status_led_set (0, STATUS_LED_OFF
);
110 status_led_set (1, STATUS_LED_ON
);
111 status_led_set (2, STATUS_LED_ON
);
115 status_led_set (0, STATUS_LED_ON
);
116 status_led_set (1, STATUS_LED_ON
);
117 status_led_set (2, STATUS_LED_ON
);
121 /* starting Ethernet configuration */
122 status_led_set (0, STATUS_LED_OFF
);
123 status_led_set (1, STATUS_LED_OFF
);
124 status_led_set (2, STATUS_LED_ON
);
129 status_led_set (0, STATUS_LED_ON
);
130 status_led_set (1, STATUS_LED_OFF
);
131 status_led_set (2, STATUS_LED_ON
);
137 int board_early_init_f(void)
141 set_leds(0); /* display boot info counter */
143 /*--------------------------------------------------------------------
144 * Setup the external bus controller/chip selects
145 *-------------------------------------------------------------------*/
146 mtdcr(EBC0_CFGADDR
, EBC0_CFG
);
147 reg
= mfdcr(EBC0_CFGDATA
);
148 mtdcr(EBC0_CFGDATA
, reg
| 0x04000000); /* Set ATC */
150 /*--------------------------------------------------------------------
151 * GPIO's are alreay setup in arch/powerpc/cpu/ppc4xx/cpu_init.c
152 * via define from board config file.
153 *-------------------------------------------------------------------*/
155 /*--------------------------------------------------------------------
156 * Setup the interrupt controller polarities, triggers, etc.
157 *-------------------------------------------------------------------*/
158 mtdcr(UIC0SR
, 0xffffffff); /* clear all */
159 mtdcr(UIC0ER
, 0x00000000); /* disable all */
160 mtdcr(UIC0CR
, 0x00000001); /* UIC1 crit is critical */
161 mtdcr(UIC0PR
, 0xfffffe1f); /* per ref-board manual */
162 mtdcr(UIC0TR
, 0x01c00000); /* per ref-board manual */
163 mtdcr(UIC0VR
, 0x00000001); /* int31 highest, base=0x000 */
164 mtdcr(UIC0SR
, 0xffffffff); /* clear all */
166 mtdcr(UIC1SR
, 0xffffffff); /* clear all */
167 mtdcr(UIC1ER
, 0x00000000); /* disable all */
168 mtdcr(UIC1CR
, 0x00000000); /* all non-critical */
169 mtdcr(UIC1PR
, 0xffffe0ff); /* per ref-board manual */
170 mtdcr(UIC1TR
, 0x00ffc000); /* per ref-board manual */
171 mtdcr(UIC1VR
, 0x00000001); /* int31 highest, base=0x000 */
172 mtdcr(UIC1SR
, 0xffffffff); /* clear all */
174 /*--------------------------------------------------------------------
175 * Setup other serial configuration
176 *-------------------------------------------------------------------*/
177 mfsdr(SDR0_PCI0
, reg
);
178 mtsdr(SDR0_PCI0
, 0x80000000 | reg
); /* PCI arbiter enabled */
179 mtsdr(SDR0_PFC0
, 0x00000000); /* Pin function: enable GPIO49-63 */
180 mtsdr(SDR0_PFC1
, 0x00048000); /* Pin function: UART0 has 4 pins, select IRQ5 */
185 #define EEPROM_LEN 256
186 static void load_ethaddr(void)
188 int ok_ethaddr
, ok_eth1addr
;
190 uchar buf
[EEPROM_LEN
];
192 u16 checksumcrc16
= 0;
194 /* If the env is sane, then nothing for us to do */
195 ok_ethaddr
= eth_getenv_enetaddr("ethaddr", buf
);
196 ok_eth1addr
= eth_getenv_enetaddr("eth1addr", buf
);
197 if (ok_ethaddr
&& ok_eth1addr
)
200 /* read the MACs from EEprom */
201 status_led_set (0, STATUS_LED_ON
);
202 status_led_set (1, STATUS_LED_ON
);
203 ret
= eeprom_read (CONFIG_SYS_I2C_EEPROM_ADDR
, 0, buf
, EEPROM_LEN
);
205 checksumcrc16
= cyg_crc16 (buf
, EEPROM_LEN
- 2);
206 /* check, if the EEprom is programmed:
207 * - The Prefix(Byte 0,1,2) is equal to "ATR"
208 * - The checksum, stored in the last 2 Bytes, is correct
210 if ((strncmp ((char *)buf
,"ATR",3) != 0) ||
211 ((checksumcrc16
>> 8) != buf
[EEPROM_LEN
- 2]) ||
212 ((checksumcrc16
& 0xff) != buf
[EEPROM_LEN
- 1])) {
213 /* EEprom is not programmed */
214 printf("%s: EEPROM Checksum not OK\n", __FUNCTION__
);
218 eth_setenv_enetaddr("ethaddr", &buf
[3]);
220 eth_setenv_enetaddr("eth1addr", &buf
[9]);
225 /* some error reading the EEprom */
226 if ((use_eeprom
= getenv ("use_eeprom_ethaddr")) == NULL
) {
227 /* dont use bootcmd */
228 setenv("bootdelay", "-1");
231 /* == default ? use standard */
232 if (strncmp (use_eeprom
, "default", 7) == 0) {
235 /* Env doesnt exist -> hang */
237 /* here we do this "handy" because we have no interrupts
239 puts ("### EEPROM ERROR ### Please RESET the board ###\n");
247 #ifdef CONFIG_PREBOOT
249 static uchar kbd_magic_prefix
[] = "key_magic";
250 static uchar kbd_command_prefix
[] = "key_cmd";
257 struct kbd_data_t
* get_keys (struct kbd_data_t
*kbd_data
)
262 /* use the DIPs for some bootoptions */
263 val
= getenv (ENV_NAME_DIP
);
264 tmp
= simple_strtoul (val
, NULL
, 16);
266 kbd_data
->s2
= (tmp
& 0x0f);
267 kbd_data
->s1
= (tmp
& 0xf0) >> 4;
271 static int compare_magic (const struct kbd_data_t
*kbd_data
, char *str
)
275 if (s1
>= '0' && s1
<= '9')
277 else if (s1
>= 'a' && s1
<= 'f')
279 else if (s1
>= 'A' && s1
<= 'F')
284 if (s1
!= kbd_data
->s1
) return -1;
287 if (s1
>= '0' && s1
<= '9')
289 else if (s1
>= 'a' && s1
<= 'f')
291 else if (s1
>= 'A' && s1
<= 'F')
296 if (s1
!= kbd_data
->s2
) return -1;
300 static char *key_match (const struct kbd_data_t
*kbd_data
)
302 char magic
[sizeof (kbd_magic_prefix
) + 1];
304 char *kbd_magic_keys
;
307 * The following string defines the characters that can be appended
308 * to "key_magic" to form the names of environment variables that
309 * hold "magic" key codes, i. e. such key codes that can cause
310 * pre-boot actions. If the string is empty (""), then only
311 * "key_magic" is checked (old behaviour); the string "125" causes
312 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
314 if ((kbd_magic_keys
= getenv ("magic_keys")) == NULL
)
317 /* loop over all magic keys;
318 * use '\0' suffix in case of empty string
320 for (suffix
= kbd_magic_keys
; *suffix
||
321 suffix
== kbd_magic_keys
; ++suffix
) {
322 sprintf (magic
, "%s%c", kbd_magic_prefix
, *suffix
);
323 if (compare_magic (kbd_data
, getenv (magic
)) == 0) {
324 char cmd_name
[sizeof (kbd_command_prefix
) + 1];
327 sprintf (cmd_name
, "%s%c", kbd_command_prefix
, *suffix
);
328 cmd
= getenv (cmd_name
);
336 #endif /* CONFIG_PREBOOT */
338 static int pcs440ep_readinputs (void)
343 /* read the inputs and set the Envvars */
344 /* Revision Level Bit 26 - 29 */
345 i
= ((in32 (GPIO0_IR
) & 0x0000003c) >> 2);
347 sprintf (value
, "%02x", i
);
348 setenv (ENV_NAME_REVLEV
, value
);
349 /* Solder Switch Bit 30 - 33 */
350 i
= (in32 (GPIO0_IR
) & 0x00000003) << 2;
351 i
+= (in32 (GPIO1_IR
) & 0xc0000000) >> 30;
353 sprintf (value
, "%02x", i
);
354 setenv (ENV_NAME_SOLDER
, value
);
355 /* DIP Switch Bit 49 - 56 */
356 i
= ((in32 (GPIO1_IR
) & 0x00007f80) >> 7);
357 i
= (swapbits
[i
& 0x0f] << 4) + swapbits
[(i
& 0xf0) >> 4];
358 sprintf (value
, "%02x", i
);
359 setenv (ENV_NAME_DIP
, value
);
364 #if defined(CONFIG_SHA1_CHECK_UB_IMG)
365 /*************************************************************************
366 * calculate a SHA1 sum for the U-Boot image in Flash.
368 ************************************************************************/
369 static int pcs440ep_sha1 (int docheck
)
372 unsigned char *ptroff
;
373 unsigned char output
[20];
374 unsigned char org
[20];
375 int i
, len
= CONFIG_SHA1_LEN
;
377 memcpy ((char *)CONFIG_SYS_LOAD_ADDR
, (char *)CONFIG_SHA1_START
, len
);
378 data
= (unsigned char *)CONFIG_SYS_LOAD_ADDR
;
379 ptroff
= &data
[len
+ SHA1_SUM_POS
];
381 for (i
= 0; i
< SHA1_SUM_LEN
; i
++) {
386 sha1_csum ((unsigned char *) data
, len
, (unsigned char *)output
);
389 for (i
= 0; i
< 20 ; i
++) {
390 printf("%02X ", output
[i
]);
395 for (i
= 0; i
< 20 ; i
++) {
396 if (org
[i
] != output
[i
]) return 1;
402 /*************************************************************************
403 * do some checks after the SHA1 checksum from the U-Boot Image was
406 ************************************************************************/
407 static void pcs440ep_checksha1 (void)
412 status_led_set (0, STATUS_LED_OFF
);
413 status_led_set (1, STATUS_LED_OFF
);
414 status_led_set (2, STATUS_LED_ON
);
415 ret
= pcs440ep_sha1 (1);
416 if (ret
== 0) return;
418 if ((cs_test
= getenv ("cs_test")) == NULL
) {
419 /* Env doesnt exist -> hang */
421 /* here we do this "handy" because we have no interrupts
423 puts ("### SHA1 ERROR ### Please RESET the board ###\n");
430 if (strncmp (cs_test
, "off", 3) == 0) {
431 printf ("SHA1 U-Boot sum NOT ok!\n");
432 setenv ("bootdelay", "-1");
436 static __inline__
void pcs440ep_checksha1 (void) { do {} while (0);}
439 int misc_init_r (void)
446 /* Re-do sizing to get full correct info */
447 mtdcr(EBC0_CFGADDR
, PB0CR
);
448 pbcr
= mfdcr(EBC0_CFGDATA
);
449 switch (gd
->bd
->bi_flashsize
) {
475 pbcr
= (pbcr
& 0x0001ffff) | gd
->bd
->bi_flashstart
| (size_val
<< 17);
476 mtdcr(EBC0_CFGADDR
, PB0CR
);
477 mtdcr(EBC0_CFGDATA
, pbcr
);
479 /* adjust flash start and offset */
480 gd
->bd
->bi_flashstart
= 0 - gd
->bd
->bi_flashsize
;
481 gd
->bd
->bi_flashoffset
= 0;
483 /* Monitor protection ON by default */
484 (void)flash_protect(FLAG_PROTECT_SET
,
485 -CONFIG_SYS_MONITOR_LEN
,
489 /* Env protection ON by default */
490 (void)flash_protect(FLAG_PROTECT_SET
,
491 CONFIG_ENV_ADDR_REDUND
,
492 CONFIG_ENV_ADDR_REDUND
+ 2*CONFIG_ENV_SECT_SIZE
- 1,
495 pcs440ep_readinputs ();
496 pcs440ep_checksha1 ();
497 #ifdef CONFIG_PREBOOT
499 struct kbd_data_t kbd_data
;
501 char *str
= strdup (key_match (get_keys (&kbd_data
)));
502 /* Set or delete definition */
503 setenv ("preboot", str
);
506 #endif /* CONFIG_PREBOOT */
512 char *s
= getenv("serial#");
514 printf("Board: PCS440EP");
524 void spd_ddr_init_hang (void)
526 status_led_set (0, STATUS_LED_OFF
);
527 status_led_set (1, STATUS_LED_ON
);
528 /* we cannot use hang() because we are still running from
529 Flash, and so the status_led driver is not initialized */
530 puts ("### SDRAM ERROR ### Please RESET the board ###\n");
537 phys_size_t
initdram (int board_type
)
541 status_led_set (0, STATUS_LED_ON
);
542 status_led_set (1, STATUS_LED_OFF
);
543 dram_size
= spd_sdram();
544 status_led_set (0, STATUS_LED_OFF
);
545 status_led_set (1, STATUS_LED_ON
);
546 if (dram_size
== 0) {
553 /*************************************************************************
556 * This routine is called to reset (keep alive) the watchdog timer
558 ************************************************************************/
559 #if defined(CONFIG_HW_WATCHDOG)
560 void hw_watchdog_reset(void)
566 /*************************************************************************
567 * "led" Commando for the U-Boot shell
569 ************************************************************************/
570 int do_led (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
575 pattern
= simple_strtoul (argv
[1], NULL
, 16);
576 if (pattern
> 0x400) {
578 printf ("led: %x\n", val
);
581 if (pattern
> 0x200) {
586 if (pattern
> 0x100) {
591 for (i
= 0; i
< 4; i
++) {
592 if (pattern
& 0x01) status_led_set (i
, STATUS_LED_ON
);
593 else status_led_set (i
, STATUS_LED_OFF
);
594 pattern
= pattern
>> 1;
602 "[bitmask] 0x01 = DIAG 1 on\n"
603 " 0x02 = DIAG 2 on\n"
604 " 0x04 = DIAG 3 on\n"
605 " 0x08 = DIAG 4 on\n"
606 " > 0x100 set the LED, who are on, to state blinking"
609 #if defined(CONFIG_SHA1_CHECK_UB_IMG)
610 /*************************************************************************
611 * "sha1" Commando for the U-Boot shell
613 ************************************************************************/
614 int do_sha1 (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
620 return cmd_usage(cmdtp
);
625 unsigned char output
[20];
629 data
= (unsigned char *)simple_strtoul (argv
[1], NULL
, 16);
630 len
= simple_strtoul (argv
[2], NULL
, 16);
631 sha1_csum (data
, len
, (unsigned char *)output
);
632 printf ("U-Boot sum:\n");
633 for (i
= 0; i
< 20 ; i
++) {
634 printf ("%02X ", output
[i
]);
638 data
= (unsigned char *)simple_strtoul (argv
[3], NULL
, 16);
639 memcpy (data
, output
, 20);
645 if (*ptr
!= '-') goto usage
;
647 if ((*ptr
== 'c') || (*ptr
== 'C')) {
648 rcode
= pcs440ep_sha1 (1);
649 printf ("SHA1 U-Boot sum %sok!\n", (rcode
!= 0) ? "not " : "");
650 } else if ((*ptr
== 'p') || (*ptr
== 'P')) {
651 rcode
= pcs440ep_sha1 (2);
653 rcode
= pcs440ep_sha1 (0);
662 "calculate the SHA1 Sum",
663 "address len [addr] calculate the SHA1 sum [save at addr]\n"
664 " -p calculate the SHA1 sum from the U-Boot image in flash and print\n"
665 " -c check the U-Boot image in flash"
669 #if defined (CONFIG_CMD_IDE)
670 /* These addresses need to be shifted one place to the left
671 * ( bus per_addr 20 -30 is connectsd on CF bus A10-A0)
672 * These values are shifted
674 extern ulong
*ide_bus_offset
;
675 void inline ide_outb(int dev
, int port
, unsigned char val
)
677 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
678 dev
, port
, val
, (ATA_CURR_BASE(dev
)+port
));
680 out_be16((u16
*)(ATA_CURR_BASE(dev
)+(port
<< 1)), val
);
682 unsigned char inline ide_inb(int dev
, int port
)
685 val
= in_be16((u16
*)(ATA_CURR_BASE(dev
)+(port
<< 1)));
686 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
687 dev
, port
, (ATA_CURR_BASE(dev
)+port
), val
);
692 #ifdef CONFIG_IDE_PREINIT
693 int ide_preinit (void)
695 /* Set True IDE Mode */
696 out32 (GPIO0_OR
, (in32 (GPIO0_OR
) | 0x00100000));
697 out32 (GPIO0_OR
, (in32 (GPIO0_OR
) | 0x00200000));
698 out32 (GPIO1_OR
, (in32 (GPIO1_OR
) & ~0x00008040));
704 #if defined (CONFIG_CMD_IDE) && defined (CONFIG_IDE_RESET)
705 void ide_set_reset (int idereset
)
707 debug ("ide_reset(%d)\n", idereset
);
709 out32 (GPIO0_OR
, (in32 (GPIO0_OR
) | 0x00200000));
711 out32 (GPIO0_OR
, (in32 (GPIO0_OR
) & ~0x00200000));
715 #endif /* defined (CONFIG_CMD_IDE) && defined (CONFIG_IDE_RESET) */