3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
5 * SPDX-License-Identifier: GPL-2.0+
11 #include "common_util.h"
12 #include <asm/processor.h>
13 #include <asm/byteorder.h>
21 #include "../pip405/pip405.h"
22 #include <asm/4xx_pci.h>
24 #if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
25 #include "../mip405/mip405.h"
26 #include <asm/4xx_pci.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 #if defined(CONFIG_PATI)
32 #define FIRM_START 0xFFF00000
35 extern int mem_test(ulong start, ulong ramsize, int quiet);
37 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
38 #define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
40 #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) \
41 || defined(CONFIG_TARGET_MIP405T)
42 /*-----------------------------------------------------------------------
43 * On PIP/MIP405 we have 3 (4) possible boot mode
45 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
46 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
47 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
48 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
49 * The flash init is the first board specific routine which is called
50 * after code relocation (running from SDRAM)
51 * The first thing we do is to map the Flash CS to the Flash area and
52 * the MPS CS to the MPS area. Since the flash size is unknown at this
53 * point, we use the max flash size and the lowest flash address as base.
55 * After flash detection we adjust the size of the CS area accordingly.
56 * update_flash_size() will fix in wrong values in the flash_info structure,
57 * misc_init_r() will fix the values in the board info structure
59 int get_boot_mode(void)
63 pbcr = mfdcr(CPC0_PSR);
64 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
65 /* boot via MPS or MPS mapping */
67 if (pbcr & PSR_ROM_LOC)
73 /* Map the flash high (in boot area)
74 This code can only be executed from SDRAM (after relocation).
76 void setup_cs_reloc(void)
80 * since we are relocated, we can set-up the CS finaly
81 * but first of all, switch off PCI mapping (in case it
86 mode = get_boot_mode();
88 * we map the flash high in every case
89 * first find out to which CS the flash is attached to
91 if (mode & BOOT_MPS) {
92 /* map flash high on CS1 and MPS on CS0 */
93 mtdcr(EBC0_CFGADDR, PB0AP);
94 mtdcr(EBC0_CFGDATA, MPS_AP);
95 mtdcr(EBC0_CFGADDR, PB0CR);
96 mtdcr(EBC0_CFGDATA, MPS_CR);
98 * we use the default values (max values) for the flash
99 * because its real size is not yet known
101 mtdcr(EBC0_CFGADDR, PB1AP);
102 mtdcr(EBC0_CFGDATA, FLASH_AP);
103 mtdcr(EBC0_CFGADDR, PB1CR);
104 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
106 /* map flash high on CS0 and MPS on CS1 */
107 mtdcr(EBC0_CFGADDR, PB1AP);
108 mtdcr(EBC0_CFGDATA, MPS_AP);
109 mtdcr(EBC0_CFGADDR, PB1CR);
110 mtdcr(EBC0_CFGDATA, MPS_CR);
112 * we use the default values (max values) for the flash
113 * because its real size is not yet known
115 mtdcr(EBC0_CFGADDR, PB0AP);
116 mtdcr(EBC0_CFGDATA, FLASH_AP);
117 mtdcr(EBC0_CFGADDR, PB0CR);
118 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
121 #endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) */
123 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
124 /* adjust flash start and protection info */
125 int update_flash_size(int flash_size)
128 flash_info_t *info = &flash_info[0];
129 unsigned long flashcr;
130 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
132 if (flash_size > 128*1024*1024) {
133 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
138 if ((flash_size >> 20) != 0)
139 i = __ilog2(flash_size >> 20);
141 /* set up flash CS according to the size */
142 mode = get_boot_mode();
143 if (mode & BOOT_MPS) {
144 /* flash is on CS1 */
145 mtdcr(EBC0_CFGADDR, PB1CR);
146 flashcr = mfdcr(EBC0_CFGDATA);
147 /* we map the flash high in every case */
148 flashcr &= 0x0001FFFF; /* mask out address bits */
149 flashcr |= flash_base; /* start addr */
150 flashcr |= (i << 17); /* size addr */
151 mtdcr(EBC0_CFGADDR, PB1CR);
152 mtdcr(EBC0_CFGDATA, flashcr);
154 /* flash is on CS0 */
155 mtdcr(EBC0_CFGADDR, PB0CR);
156 flashcr = mfdcr(EBC0_CFGDATA);
157 /* we map the flash high in every case */
158 flashcr &= 0x0001FFFF; /* mask out address bits */
159 flashcr |= flash_base; /* start addr */
160 flashcr |= (i << 17); /* size addr */
161 mtdcr(EBC0_CFGADDR, PB0CR);
162 mtdcr(EBC0_CFGDATA, flashcr);
165 for (i = 0; i < info->sector_count; i++)
166 /* adjust sector start address */
167 info->start[i] = flash_base +
168 (info->start[i] - CONFIG_SYS_FLASH_BASE);
170 /* unprotect all sectors */
171 flash_protect(FLAG_PROTECT_CLEAR,
175 flash_protect_default();
176 /* protect reset vector too*/
177 flash_protect(FLAG_PROTECT_SET,
178 info->start[info->sector_count-1],
187 mpl_prg(uchar *src, ulong size)
190 flash_info_t *info = &flash_info[0];
192 #if defined(CONFIG_PATI)
195 #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) \
196 || defined(CONFIG_TARGET_MIP405T) || defined(CONFIG_PATI)
197 char *copystr = (char *)src;
198 ulong *magic = (ulong *)src;
200 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
201 puts("Bad Magic number\n");
204 /* some more checks before we delete the Flash... */
205 /* Checking the ISO_STRING prevents to program a
206 * wrong Firmware Image into the flash.
208 i = 4; /* skip Magic number */
210 if (strncmp(©str[i], "MEV-", 4) == 0)
213 puts("Firmware Image for unknown Target\n");
217 /* we have the ISO STRING, check */
218 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
219 printf("Wrong Firmware Image: %s\n", ©str[i]);
222 #if !defined(CONFIG_PATI)
225 /* unprotect sectors used by u-boot */
226 flash_protect(FLAG_PROTECT_CLEAR,
231 /* search start sector */
232 for (i = info->sector_count-1; i > 0; i--)
233 if (start >= info->start[i])
236 /* now erase flash */
237 printf("Erasing at %lx (sector %d) (start %lx)\n",
238 start,i,info->start[i]);
239 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
245 #else /* #if !defined(CONFIG_PATI) */
249 /* search start sector */
250 for (i = info->sector_count-1; i > 0; i--)
251 if (start >= info->start[i])
256 for (i = info->sector_count-1; i > 0; i--)
257 if ((start + size) >= info->start[i])
260 /* unprotect sectors used by u-boot */
261 flash_protect(FLAG_PROTECT_CLEAR,
266 /* now erase flash */
267 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
268 start, start + size, start_sect, i,
269 info->start[start_sect], info->start[i]);
270 if ((rc = flash_erase (info, start_sect, i)) != 0) {
275 #endif /* defined(CONFIG_PATI) */
277 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
279 if ((rc = flash_write ((char *)src, start, size)) != 0) {
284 puts("OK programming done\n");
290 mpl_prg_image(uchar *ld_addr)
294 image_header_t *hdr = (image_header_t *)ld_addr;
297 #if defined(CONFIG_FIT)
298 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
299 puts ("Non legacy image format not supported\n");
304 if (!image_check_magic (hdr)) {
305 puts("Bad Magic Number\n");
308 image_print_contents (hdr);
309 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
310 puts("No U-Boot Image\n");
313 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
314 puts("No Firmware Image\n");
317 if (!image_check_hcrc (hdr)) {
318 puts("Bad Header Checksum\n");
321 puts("Verifying Checksum ... ");
322 if (!image_check_dcrc (hdr)) {
323 puts("Bad Data CRC\n");
328 data = (uchar *)image_get_data (hdr);
329 len = image_get_data_size (hdr);
331 if (image_get_comp (hdr) != IH_COMP_NONE) {
333 /* reserve space for uncompressed image */
334 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
335 puts("Insufficient space for decompression\n");
339 switch (image_get_comp (hdr)) {
341 puts("Uncompressing (GZIP) ... ");
342 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
344 puts("GUNZIP ERROR\n");
352 puts("Uncompressing (BZIP2) ... ");
354 uint retlen = IMAGE_SIZE;
355 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
356 (char *)data, len, 0, 0);
360 printf ("BUNZIP2 ERROR: %d\n", rc);
368 printf ("Unimplemented compression type %d\n",
369 image_get_comp (hdr));
374 rc = mpl_prg(buf, len);
377 rc = mpl_prg(data, len);
383 #if !defined(CONFIG_PATI)
384 void get_backup_values(backup_t *buf)
386 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
389 void set_backup_values(int overwrite)
394 get_backup_values(&back);
396 if(strncmp(back.signature,"MPL\0",4)==0) {
397 puts("Not possible to write Backup\n");
401 memcpy(back.signature,"MPL\0",4);
402 i = getenv_f("serial#",back.serial_name,16);
404 puts("Not possible to write Backup\n");
407 back.serial_name[16]=0;
408 i = getenv_f("ethaddr",back.eth_addr,20);
410 puts("Not possible to write Backup\n");
414 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
417 void clear_env_values(void)
420 unsigned char env_crc[4];
422 memset(&back,0xff,sizeof(backup_t));
423 memset(env_crc,0x00,4);
424 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
425 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
429 * check crc of "older" environment
431 int check_env_old_size(ulong oldsize)
438 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
440 (uchar *)&crc, sizeof(ulong));
447 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
449 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
450 new = crc32 (new, buf, n);
458 static ulong oldsizes[] = {
464 void copy_old_env(ulong size)
467 uchar value_buf[0x800];
474 value = &value_buf[0];
478 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
487 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
494 value = &value_buf[0];
495 if(strncmp((char *)name,"baudrate",8)!=0) {
496 setenv((char *)name,(char *)value);
514 if(check_env_old_size(oldsizes[i]))
519 /* no old environment has been found */
520 get_backup_values (&back);
521 if (strncmp (back.signature, "MPL\0", 4) == 0) {
522 sprintf (buf, "%s", back.serial_name);
523 setenv ("serial#", buf);
524 sprintf (buf, "%s", back.eth_addr);
525 setenv ("ethaddr", buf);
526 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
531 copy_old_env(oldsizes[i]);
532 puts("INFO: old environment ajusted, use saveenv\n");
536 /* check if back up is set */
537 get_backup_values(&back);
538 if(strncmp(back.signature,"MPL\0",4)!=0) {
539 set_backup_values(0);
544 #endif /* #if !defined(CONFIG_PATI) */
546 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
550 #if !defined(CONFIG_PATI)
551 ulong size = IMAGE_SIZE;
552 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
556 if (strcmp(argv[1], "flash") == 0)
558 #if defined(CONFIG_CMD_FDC)
559 if (strcmp(argv[2], "floppy") == 0) {
561 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
562 puts("\nupdating bootloader image from floppy\n");
563 local_args[0] = argv[0];
565 local_args[1] = argv[3];
566 local_args[2] = NULL;
567 ld_addr=simple_strtoul(argv[3], NULL, 16);
568 result=do_fdcboot(cmdtp, 0, 2, local_args);
571 local_args[1] = NULL;
572 ld_addr=CONFIG_SYS_LOAD_ADDR;
573 result=do_fdcboot(cmdtp, 0, 1, local_args);
575 result=mpl_prg_image((uchar *)ld_addr);
579 if (strcmp(argv[2], "mem") == 0) {
581 ld_addr=simple_strtoul(argv[3], NULL, 16);
586 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
587 result=mpl_prg_image((uchar *)ld_addr);
590 #if !defined(CONFIG_PATI)
591 if (strcmp(argv[2], "mps") == 0) {
592 puts("\nupdating bootloader image from MPS\n");
593 result=mpl_prg((uchar *)src,size);
596 #endif /* #if !defined(CONFIG_PATI) */
598 #if !defined(CONFIG_PATI)
599 if (strcmp(argv[1], "clearenvvalues") == 0)
601 if (strcmp(argv[2], "yes") == 0)
607 if (strcmp(argv[1], "getback") == 0) {
608 get_backup_values(&back);
610 back.serial_name[16]=0;
612 printf("GetBackUp: signature: %s\n",back.signature);
613 printf(" serial#: %s\n",back.serial_name);
614 printf(" ethaddr: %s\n",back.eth_addr);
617 if (strcmp(argv[1], "setback") == 0) {
618 set_backup_values(1);
622 return cmd_usage(cmdtp);
626 #if defined(CONFIG_CMD_DOC)
629 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
635 /******************************************************
636 * Routines to display the Board information
637 * to the screen (since the VGA will be initialized as last,
638 * we must resend the infos)
641 #ifdef CONFIG_CONSOLE_EXTRA_INFO
642 extern GraphicDevice ctfb;
643 extern int get_boot_mode(void);
645 void video_get_info_str (int line_number, char *info)
647 /* init video info strings for graphic console */
648 PPC4xx_SYS_INFO sys_info;
653 char buf1[32], buf2[32], buf3[32], buf4[32];
659 /* CPU and board infos */
661 get_sys_info (&sys_info);
663 case PVR_405GP_RB: rev='B'; break;
664 case PVR_405GP_RC: rev='C'; break;
665 case PVR_405GP_RD: rev='D'; break;
666 case PVR_405GP_RE: rev='E'; break;
667 case PVR_405GPR_RB: rev='B'; break;
668 default: rev='?'; break;
670 if(pvr==PVR_405GPR_RB)
671 sprintf(cpustr,"PPC405GPr %c",rev);
673 sprintf(cpustr,"PPC405GP %c",rev);
676 s=getenv ("serial#");
678 if (!s || strncmp (s, "PIP405", 6)) {
679 strcpy(buf,"### No HW ID - assuming PIP405");
682 #if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
683 if (!s || strncmp (s, "MIP405", 6)) {
684 strcpy(buf,"### No HW ID - assuming MIP405");
688 for (e = s; *e; ++e) {
699 strcpy(&buf[i]," SN ");
706 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
708 strmhz (buf1, gd->cpu_clk),
709 strmhz (buf2, sys_info.freqPLB),
710 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
711 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
715 boot = get_boot_mode();
716 bc = in8 (CONFIG_PORT_ADDR);
717 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
718 gd->bd->bi_memsize / 0x100000,
719 gd->bd->bi_flashsize / 0x100000,
721 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
725 strcpy(buf, CONFIG_IDENT_STRING);
726 sprintf (info, " %s", &buf[1]);
729 /* no more info lines */
733 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
735 #endif /* CONFIG_VIDEO */