3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/ppc4xx.h>
13 #include <asm/processor.h>
14 #include <spd_sdram.h>
15 #include <status_led.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 extern flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
25 unsigned char sha1_checksum[SHA1_SUM_LEN];
27 /* swap 4 Bits (Bit0 = Bit3, Bit1 = Bit2, Bit2 = Bit1 and Bit3 = Bit0) */
28 unsigned char swapbits[16] = {0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
29 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf};
31 static void set_leds (int val)
33 out32(GPIO0_OR, (in32 (GPIO0_OR) & ~0x78000000) | (val << 27));
36 #define GET_LEDS ((in32 (GPIO0_OR) & 0x78000000) >> 27)
38 void __led_init (led_id_t mask, int state)
42 if (state == STATUS_LED_ON)
49 void __led_set (led_id_t mask, int state)
53 if (state == STATUS_LED_ON)
55 else if (state == STATUS_LED_OFF)
60 void __led_toggle (led_id_t mask)
68 static void status_led_blink (void)
73 /* set all LED which are on, to state BLINKING */
74 for (i = 0; i < 4; i++) {
75 if (val & 0x01) status_led_set (3 - i, STATUS_LED_BLINKING);
76 else status_led_set (3 - i, STATUS_LED_OFF);
81 #if defined(CONFIG_SHOW_BOOT_PROGRESS)
82 void show_boot_progress (int val)
84 /* find all valid Codes for val in README */
85 if (val == -BOOTSTAGE_ID_NEED_RESET)
88 /* smthing goes wrong */
93 case BOOTSTAGE_ID_CHECK_MAGIC:
94 /* validating Image */
95 status_led_set(0, STATUS_LED_OFF);
96 status_led_set(1, STATUS_LED_ON);
97 status_led_set(2, STATUS_LED_ON);
99 case BOOTSTAGE_ID_RUN_OS:
100 status_led_set(0, STATUS_LED_ON);
101 status_led_set(1, STATUS_LED_ON);
102 status_led_set(2, STATUS_LED_ON);
105 case BOOTSTAGE_ID_NET_ETH_START:
106 /* starting Ethernet configuration */
107 status_led_set(0, STATUS_LED_OFF);
108 status_led_set(1, STATUS_LED_OFF);
109 status_led_set(2, STATUS_LED_ON);
112 case BOOTSTAGE_ID_NET_START:
114 status_led_set(0, STATUS_LED_ON);
115 status_led_set(1, STATUS_LED_OFF);
116 status_led_set(2, STATUS_LED_ON);
122 int board_early_init_f(void)
126 set_leds(0); /* display boot info counter */
128 /*--------------------------------------------------------------------
129 * Setup the external bus controller/chip selects
130 *-------------------------------------------------------------------*/
131 mtdcr(EBC0_CFGADDR, EBC0_CFG);
132 reg = mfdcr(EBC0_CFGDATA);
133 mtdcr(EBC0_CFGDATA, reg | 0x04000000); /* Set ATC */
135 /*--------------------------------------------------------------------
136 * GPIO's are alreay setup in arch/powerpc/cpu/ppc4xx/cpu_init.c
137 * via define from board config file.
138 *-------------------------------------------------------------------*/
140 /*--------------------------------------------------------------------
141 * Setup the interrupt controller polarities, triggers, etc.
142 *-------------------------------------------------------------------*/
143 mtdcr(UIC0SR, 0xffffffff); /* clear all */
144 mtdcr(UIC0ER, 0x00000000); /* disable all */
145 mtdcr(UIC0CR, 0x00000001); /* UIC1 crit is critical */
146 mtdcr(UIC0PR, 0xfffffe1f); /* per ref-board manual */
147 mtdcr(UIC0TR, 0x01c00000); /* per ref-board manual */
148 mtdcr(UIC0VR, 0x00000001); /* int31 highest, base=0x000 */
149 mtdcr(UIC0SR, 0xffffffff); /* clear all */
151 mtdcr(UIC1SR, 0xffffffff); /* clear all */
152 mtdcr(UIC1ER, 0x00000000); /* disable all */
153 mtdcr(UIC1CR, 0x00000000); /* all non-critical */
154 mtdcr(UIC1PR, 0xffffe0ff); /* per ref-board manual */
155 mtdcr(UIC1TR, 0x00ffc000); /* per ref-board manual */
156 mtdcr(UIC1VR, 0x00000001); /* int31 highest, base=0x000 */
157 mtdcr(UIC1SR, 0xffffffff); /* clear all */
159 /*--------------------------------------------------------------------
160 * Setup other serial configuration
161 *-------------------------------------------------------------------*/
162 mfsdr(SDR0_PCI0, reg);
163 mtsdr(SDR0_PCI0, 0x80000000 | reg); /* PCI arbiter enabled */
164 mtsdr(SDR0_PFC0, 0x00000000); /* Pin function: enable GPIO49-63 */
165 mtsdr(SDR0_PFC1, 0x00048000); /* Pin function: UART0 has 4 pins, select IRQ5 */
170 #define EEPROM_LEN 256
171 static void load_ethaddr(void)
173 int ok_ethaddr, ok_eth1addr;
175 uchar buf[EEPROM_LEN];
177 u16 checksumcrc16 = 0;
179 /* If the env is sane, then nothing for us to do */
180 ok_ethaddr = eth_getenv_enetaddr("ethaddr", buf);
181 ok_eth1addr = eth_getenv_enetaddr("eth1addr", buf);
182 if (ok_ethaddr && ok_eth1addr)
185 /* read the MACs from EEprom */
186 status_led_set (0, STATUS_LED_ON);
187 status_led_set (1, STATUS_LED_ON);
188 ret = eeprom_read (CONFIG_SYS_I2C_EEPROM_ADDR, 0, buf, EEPROM_LEN);
190 checksumcrc16 = cyg_crc16 (buf, EEPROM_LEN - 2);
191 /* check, if the EEprom is programmed:
192 * - The Prefix(Byte 0,1,2) is equal to "ATR"
193 * - The checksum, stored in the last 2 Bytes, is correct
195 if ((strncmp ((char *)buf,"ATR",3) != 0) ||
196 ((checksumcrc16 >> 8) != buf[EEPROM_LEN - 2]) ||
197 ((checksumcrc16 & 0xff) != buf[EEPROM_LEN - 1])) {
198 /* EEprom is not programmed */
199 printf("%s: EEPROM Checksum not OK\n", __FUNCTION__);
203 eth_setenv_enetaddr("ethaddr", &buf[3]);
205 eth_setenv_enetaddr("eth1addr", &buf[9]);
210 /* some error reading the EEprom */
211 if ((use_eeprom = getenv ("use_eeprom_ethaddr")) == NULL) {
212 /* dont use bootcmd */
213 setenv("bootdelay", "-1");
216 /* == default ? use standard */
217 if (strncmp (use_eeprom, "default", 7) == 0) {
220 /* Env doesnt exist -> hang */
222 /* here we do this "handy" because we have no interrupts
224 puts ("### EEPROM ERROR ### Please RESET the board ###\n");
232 #ifdef CONFIG_PREBOOT
234 static uchar kbd_magic_prefix[] = "key_magic";
235 static uchar kbd_command_prefix[] = "key_cmd";
242 struct kbd_data_t* get_keys (struct kbd_data_t *kbd_data)
247 /* use the DIPs for some bootoptions */
248 val = getenv (ENV_NAME_DIP);
249 tmp = simple_strtoul (val, NULL, 16);
251 kbd_data->s2 = (tmp & 0x0f);
252 kbd_data->s1 = (tmp & 0xf0) >> 4;
256 static int compare_magic (const struct kbd_data_t *kbd_data, char *str)
260 if (s1 >= '0' && s1 <= '9')
262 else if (s1 >= 'a' && s1 <= 'f')
264 else if (s1 >= 'A' && s1 <= 'F')
269 if (s1 != kbd_data->s1) return -1;
272 if (s1 >= '0' && s1 <= '9')
274 else if (s1 >= 'a' && s1 <= 'f')
276 else if (s1 >= 'A' && s1 <= 'F')
281 if (s1 != kbd_data->s2) return -1;
285 static char *key_match (const struct kbd_data_t *kbd_data)
287 char magic[sizeof (kbd_magic_prefix) + 1];
289 char *kbd_magic_keys;
292 * The following string defines the characters that can be appended
293 * to "key_magic" to form the names of environment variables that
294 * hold "magic" key codes, i. e. such key codes that can cause
295 * pre-boot actions. If the string is empty (""), then only
296 * "key_magic" is checked (old behaviour); the string "125" causes
297 * checks for "key_magic1", "key_magic2" and "key_magic5", etc.
299 if ((kbd_magic_keys = getenv ("magic_keys")) == NULL)
302 /* loop over all magic keys;
303 * use '\0' suffix in case of empty string
305 for (suffix = kbd_magic_keys; *suffix ||
306 suffix == kbd_magic_keys; ++suffix) {
307 sprintf (magic, "%s%c", kbd_magic_prefix, *suffix);
308 if (compare_magic (kbd_data, getenv (magic)) == 0) {
309 char cmd_name[sizeof (kbd_command_prefix) + 1];
312 sprintf (cmd_name, "%s%c", kbd_command_prefix, *suffix);
313 cmd = getenv (cmd_name);
321 #endif /* CONFIG_PREBOOT */
323 static int pcs440ep_readinputs (void)
328 /* read the inputs and set the Envvars */
329 /* Revision Level Bit 26 - 29 */
330 i = ((in32 (GPIO0_IR) & 0x0000003c) >> 2);
332 sprintf (value, "%02x", i);
333 setenv (ENV_NAME_REVLEV, value);
334 /* Solder Switch Bit 30 - 33 */
335 i = (in32 (GPIO0_IR) & 0x00000003) << 2;
336 i += (in32 (GPIO1_IR) & 0xc0000000) >> 30;
338 sprintf (value, "%02x", i);
339 setenv (ENV_NAME_SOLDER, value);
340 /* DIP Switch Bit 49 - 56 */
341 i = ((in32 (GPIO1_IR) & 0x00007f80) >> 7);
342 i = (swapbits[i & 0x0f] << 4) + swapbits[(i & 0xf0) >> 4];
343 sprintf (value, "%02x", i);
344 setenv (ENV_NAME_DIP, value);
349 #if defined(CONFIG_SHA1_CHECK_UB_IMG)
350 /*************************************************************************
351 * calculate a SHA1 sum for the U-Boot image in Flash.
353 ************************************************************************/
354 static int pcs440ep_sha1 (int docheck)
357 unsigned char *ptroff;
358 unsigned char output[20];
359 unsigned char org[20];
360 int i, len = CONFIG_SHA1_LEN;
362 memcpy ((char *)CONFIG_SYS_LOAD_ADDR, (char *)CONFIG_SHA1_START, len);
363 data = (unsigned char *)CONFIG_SYS_LOAD_ADDR;
364 ptroff = &data[len + SHA1_SUM_POS];
366 for (i = 0; i < SHA1_SUM_LEN; i++) {
371 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
374 for (i = 0; i < 20 ; i++) {
375 printf("%02X ", output[i]);
380 for (i = 0; i < 20 ; i++) {
381 if (org[i] != output[i]) return 1;
387 /*************************************************************************
388 * do some checks after the SHA1 checksum from the U-Boot Image was
391 ************************************************************************/
392 static void pcs440ep_checksha1 (void)
397 status_led_set (0, STATUS_LED_OFF);
398 status_led_set (1, STATUS_LED_OFF);
399 status_led_set (2, STATUS_LED_ON);
400 ret = pcs440ep_sha1 (1);
401 if (ret == 0) return;
403 if ((cs_test = getenv ("cs_test")) == NULL) {
404 /* Env doesnt exist -> hang */
406 /* here we do this "handy" because we have no interrupts
408 puts ("### SHA1 ERROR ### Please RESET the board ###\n");
415 if (strncmp (cs_test, "off", 3) == 0) {
416 printf ("SHA1 U-Boot sum NOT ok!\n");
417 setenv ("bootdelay", "-1");
421 static __inline__ void pcs440ep_checksha1 (void) { do {} while (0);}
424 int misc_init_r (void)
431 /* Re-do sizing to get full correct info */
432 mtdcr(EBC0_CFGADDR, PB0CR);
433 pbcr = mfdcr(EBC0_CFGDATA);
434 switch (gd->bd->bi_flashsize) {
460 pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
461 mtdcr(EBC0_CFGADDR, PB0CR);
462 mtdcr(EBC0_CFGDATA, pbcr);
464 /* adjust flash start and offset */
465 gd->bd->bi_flashstart = 0 - gd->bd->bi_flashsize;
466 gd->bd->bi_flashoffset = 0;
468 /* Monitor protection ON by default */
469 (void)flash_protect(FLAG_PROTECT_SET,
470 -CONFIG_SYS_MONITOR_LEN,
474 /* Env protection ON by default */
475 (void)flash_protect(FLAG_PROTECT_SET,
476 CONFIG_ENV_ADDR_REDUND,
477 CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
480 pcs440ep_readinputs ();
481 pcs440ep_checksha1 ();
482 #ifdef CONFIG_PREBOOT
484 struct kbd_data_t kbd_data;
486 char *str = strdup (key_match (get_keys (&kbd_data)));
487 /* Set or delete definition */
488 setenv ("preboot", str);
491 #endif /* CONFIG_PREBOOT */
498 int i = getenv_f("serial#", buf, sizeof(buf));
500 printf("Board: PCS440EP");
510 void spd_ddr_init_hang (void)
512 status_led_set (0, STATUS_LED_OFF);
513 status_led_set (1, STATUS_LED_ON);
514 /* we cannot use hang() because we are still running from
515 Flash, and so the status_led driver is not initialized */
516 puts ("### SDRAM ERROR ### Please RESET the board ###\n");
523 phys_size_t initdram (int board_type)
527 status_led_set (0, STATUS_LED_ON);
528 status_led_set (1, STATUS_LED_OFF);
529 dram_size = spd_sdram();
530 status_led_set (0, STATUS_LED_OFF);
531 status_led_set (1, STATUS_LED_ON);
532 if (dram_size == 0) {
539 /*************************************************************************
542 * This routine is called to reset (keep alive) the watchdog timer
544 ************************************************************************/
545 #if defined(CONFIG_HW_WATCHDOG)
546 void hw_watchdog_reset(void)
552 /*************************************************************************
553 * "led" Commando for the U-Boot shell
555 ************************************************************************/
556 int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
561 pattern = simple_strtoul (argv[1], NULL, 16);
562 if (pattern > 0x400) {
564 printf ("led: %x\n", val);
567 if (pattern > 0x200) {
572 if (pattern > 0x100) {
577 for (i = 0; i < 4; i++) {
578 if (pattern & 0x01) status_led_set (i, STATUS_LED_ON);
579 else status_led_set (i, STATUS_LED_OFF);
580 pattern = pattern >> 1;
588 "[bitmask] 0x01 = DIAG 1 on\n"
589 " 0x02 = DIAG 2 on\n"
590 " 0x04 = DIAG 3 on\n"
591 " 0x08 = DIAG 4 on\n"
592 " > 0x100 set the LED, who are on, to state blinking"
595 #if defined(CONFIG_SHA1_CHECK_UB_IMG)
596 /*************************************************************************
597 * "sha1" Commando for the U-Boot shell
599 ************************************************************************/
600 int do_sha1 (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
606 return cmd_usage(cmdtp);
611 unsigned char output[20];
615 data = (unsigned char *)simple_strtoul (argv[1], NULL, 16);
616 len = simple_strtoul (argv[2], NULL, 16);
617 sha1_csum (data, len, (unsigned char *)output);
618 printf ("U-Boot sum:\n");
619 for (i = 0; i < 20 ; i++) {
620 printf ("%02X ", output[i]);
624 data = (unsigned char *)simple_strtoul (argv[3], NULL, 16);
625 memcpy (data, output, 20);
631 if (*ptr != '-') goto usage;
633 if ((*ptr == 'c') || (*ptr == 'C')) {
634 rcode = pcs440ep_sha1 (1);
635 printf ("SHA1 U-Boot sum %sok!\n", (rcode != 0) ? "not " : "");
636 } else if ((*ptr == 'p') || (*ptr == 'P')) {
637 rcode = pcs440ep_sha1 (2);
639 rcode = pcs440ep_sha1 (0);
648 "calculate the SHA1 Sum",
649 "address len [addr] calculate the SHA1 sum [save at addr]\n"
650 " -p calculate the SHA1 sum from the U-Boot image in flash and print\n"
651 " -c check the U-Boot image in flash"
655 #if defined (CONFIG_CMD_IDE)
656 /* These addresses need to be shifted one place to the left
657 * ( bus per_addr 20 -30 is connectsd on CF bus A10-A0)
658 * These values are shifted
660 void inline ide_outb(int dev, int port, unsigned char val)
662 debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
663 dev, port, val, (ATA_CURR_BASE(dev)+port));
665 out_be16((u16 *)(ATA_CURR_BASE(dev)+(port << 1)), val);
667 unsigned char inline ide_inb(int dev, int port)
670 val = in_be16((u16 *)(ATA_CURR_BASE(dev)+(port << 1)));
671 debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
672 dev, port, (ATA_CURR_BASE(dev)+port), val);
677 #ifdef CONFIG_IDE_PREINIT
678 int ide_preinit (void)
680 /* Set True IDE Mode */
681 out32 (GPIO0_OR, (in32 (GPIO0_OR) | 0x00100000));
682 out32 (GPIO0_OR, (in32 (GPIO0_OR) | 0x00200000));
683 out32 (GPIO1_OR, (in32 (GPIO1_OR) & ~0x00008040));
689 #if defined (CONFIG_CMD_IDE) && defined (CONFIG_IDE_RESET)
690 void ide_set_reset (int idereset)
692 debug ("ide_reset(%d)\n", idereset);
694 out32 (GPIO0_OR, (in32 (GPIO0_OR) | 0x00200000));
696 out32 (GPIO0_OR, (in32 (GPIO0_OR) & ~0x00200000));
700 #endif /* defined (CONFIG_CMD_IDE) && defined (CONFIG_IDE_RESET) */
703 /* this is motly the same as it should, causing a little code duplication */
704 #if defined(CONFIG_CMD_IDE)
705 #define EIEIO __asm__ volatile ("eieio")
707 void ide_input_swap_data(int dev, ulong *sect_buf, int words)
709 volatile ushort *pbuf =
710 (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
711 ushort *dbuf = (ushort *) sect_buf;
713 debug("in input swap data base for read is %lx\n",
714 (unsigned long) pbuf);
722 void ide_output_data(int dev, const ulong *sect_buf, int words)
725 volatile ushort *pbuf;
727 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
728 dbuf = (ushort *) sect_buf;
731 *pbuf = ld_le16(dbuf++);
733 *pbuf = ld_le16(dbuf++);
737 void ide_input_data(int dev, ulong *sect_buf, int words)
740 volatile ushort *pbuf;
742 pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
743 dbuf = (ushort *) sect_buf;
745 debug("in input data base for read is %lx\n", (unsigned long) pbuf);
749 *dbuf++ = ld_le16(pbuf);
751 *dbuf++ = ld_le16(pbuf);