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>
20 #include "../pip405/pip405.h"
21 #include <asm/4xx_pci.h>
24 #include "../mip405/mip405.h"
25 #include <asm/4xx_pci.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #if defined(CONFIG_PATI)
31 #define FIRM_START 0xFFF00000
34 extern int mem_test(ulong start, ulong ramsize, int quiet);
36 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
37 #define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
39 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
40 /*-----------------------------------------------------------------------
41 * On PIP/MIP405 we have 3 (4) possible boot mode
43 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
44 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
45 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
46 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
47 * The flash init is the first board specific routine which is called
48 * after code relocation (running from SDRAM)
49 * The first thing we do is to map the Flash CS to the Flash area and
50 * the MPS CS to the MPS area. Since the flash size is unknown at this
51 * point, we use the max flash size and the lowest flash address as base.
53 * After flash detection we adjust the size of the CS area accordingly.
54 * update_flash_size() will fix in wrong values in the flash_info structure,
55 * misc_init_r() will fix the values in the board info structure
57 int get_boot_mode(void)
61 pbcr = mfdcr(CPC0_PSR);
62 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
63 /* boot via MPS or MPS mapping */
65 if (pbcr & PSR_ROM_LOC)
71 /* Map the flash high (in boot area)
72 This code can only be executed from SDRAM (after relocation).
74 void setup_cs_reloc(void)
78 * since we are relocated, we can set-up the CS finaly
79 * but first of all, switch off PCI mapping (in case it
84 mode = get_boot_mode();
86 * we map the flash high in every case
87 * first find out to which CS the flash is attached to
89 if (mode & BOOT_MPS) {
90 /* map flash high on CS1 and MPS on CS0 */
91 mtdcr(EBC0_CFGADDR, PB0AP);
92 mtdcr(EBC0_CFGDATA, MPS_AP);
93 mtdcr(EBC0_CFGADDR, PB0CR);
94 mtdcr(EBC0_CFGDATA, MPS_CR);
96 * we use the default values (max values) for the flash
97 * because its real size is not yet known
99 mtdcr(EBC0_CFGADDR, PB1AP);
100 mtdcr(EBC0_CFGDATA, FLASH_AP);
101 mtdcr(EBC0_CFGADDR, PB1CR);
102 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
104 /* map flash high on CS0 and MPS on CS1 */
105 mtdcr(EBC0_CFGADDR, PB1AP);
106 mtdcr(EBC0_CFGDATA, MPS_AP);
107 mtdcr(EBC0_CFGADDR, PB1CR);
108 mtdcr(EBC0_CFGDATA, MPS_CR);
110 * we use the default values (max values) for the flash
111 * because its real size is not yet known
113 mtdcr(EBC0_CFGADDR, PB0AP);
114 mtdcr(EBC0_CFGDATA, FLASH_AP);
115 mtdcr(EBC0_CFGADDR, PB0CR);
116 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
119 #endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) */
121 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
122 /* adjust flash start and protection info */
123 int update_flash_size(int flash_size)
126 flash_info_t *info = &flash_info[0];
127 unsigned long flashcr;
128 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
130 if (flash_size > 128*1024*1024) {
131 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
136 if ((flash_size >> 20) != 0)
137 i = __ilog2(flash_size >> 20);
139 /* set up flash CS according to the size */
140 mode = get_boot_mode();
141 if (mode & BOOT_MPS) {
142 /* flash is on CS1 */
143 mtdcr(EBC0_CFGADDR, PB1CR);
144 flashcr = mfdcr(EBC0_CFGDATA);
145 /* we map the flash high in every case */
146 flashcr &= 0x0001FFFF; /* mask out address bits */
147 flashcr |= flash_base; /* start addr */
148 flashcr |= (i << 17); /* size addr */
149 mtdcr(EBC0_CFGADDR, PB1CR);
150 mtdcr(EBC0_CFGDATA, flashcr);
152 /* flash is on CS0 */
153 mtdcr(EBC0_CFGADDR, PB0CR);
154 flashcr = mfdcr(EBC0_CFGDATA);
155 /* we map the flash high in every case */
156 flashcr &= 0x0001FFFF; /* mask out address bits */
157 flashcr |= flash_base; /* start addr */
158 flashcr |= (i << 17); /* size addr */
159 mtdcr(EBC0_CFGADDR, PB0CR);
160 mtdcr(EBC0_CFGDATA, flashcr);
163 for (i = 0; i < info->sector_count; i++)
164 /* adjust sector start address */
165 info->start[i] = flash_base +
166 (info->start[i] - CONFIG_SYS_FLASH_BASE);
168 /* unprotect all sectors */
169 flash_protect(FLAG_PROTECT_CLEAR,
173 flash_protect_default();
174 /* protect reset vector too*/
175 flash_protect(FLAG_PROTECT_SET,
176 info->start[info->sector_count-1],
185 mpl_prg(uchar *src, ulong size)
188 flash_info_t *info = &flash_info[0];
190 #if defined(CONFIG_PATI)
193 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
194 char *copystr = (char *)src;
195 ulong *magic = (ulong *)src;
198 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
199 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
200 puts("Bad Magic number\n");
203 /* some more checks before we delete the Flash... */
204 /* Checking the ISO_STRING prevents to program a
205 * wrong Firmware Image into the flash.
207 i = 4; /* skip Magic number */
209 if (strncmp(©str[i], "MEV-", 4) == 0)
212 puts("Firmware Image for unknown Target\n");
216 /* we have the ISO STRING, check */
217 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
218 printf("Wrong Firmware Image: %s\n", ©str[i]);
221 #if !defined(CONFIG_PATI)
224 /* unprotect sectors used by u-boot */
225 flash_protect(FLAG_PROTECT_CLEAR,
230 /* search start sector */
231 for (i = info->sector_count-1; i > 0; i--)
232 if (start >= info->start[i])
235 /* now erase flash */
236 printf("Erasing at %lx (sector %d) (start %lx)\n",
237 start,i,info->start[i]);
238 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
244 #else /* #if !defined(CONFIG_PATI */
248 /* search start sector */
249 for (i = info->sector_count-1; i > 0; i--)
250 if (start >= info->start[i])
255 for (i = info->sector_count-1; i > 0; i--)
256 if ((start + size) >= info->start[i])
259 /* unprotect sectors used by u-boot */
260 flash_protect(FLAG_PROTECT_CLEAR,
265 /* now erase flash */
266 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
267 start, start + size, start_sect, i,
268 info->start[start_sect], info->start[i]);
269 if ((rc = flash_erase (info, start_sect, i)) != 0) {
274 #endif /* defined(CONFIG_PATI) */
276 #elif defined(CONFIG_VCMA9)
279 /* search end sector */
280 for (i = 0; i < info->sector_count; i++)
281 if (size < info->start[i])
284 flash_protect(FLAG_PROTECT_CLEAR,
289 /* now erase flash */
290 printf("Erasing at %lx (sector %d) (start %lx)\n",
291 start,0,info->start[0]);
292 if ((rc = flash_erase (info, 0, i)) != 0) {
299 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
301 if ((rc = flash_write ((char *)src, start, size)) != 0) {
306 puts("OK programming done\n");
312 mpl_prg_image(uchar *ld_addr)
316 image_header_t *hdr = (image_header_t *)ld_addr;
319 #if defined(CONFIG_FIT)
320 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
321 puts ("Non legacy image format not supported\n");
326 if (!image_check_magic (hdr)) {
327 puts("Bad Magic Number\n");
330 image_print_contents (hdr);
331 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
332 puts("No U-Boot Image\n");
335 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
336 puts("No Firmware Image\n");
339 if (!image_check_hcrc (hdr)) {
340 puts("Bad Header Checksum\n");
343 puts("Verifying Checksum ... ");
344 if (!image_check_dcrc (hdr)) {
345 puts("Bad Data CRC\n");
350 data = (uchar *)image_get_data (hdr);
351 len = image_get_data_size (hdr);
353 if (image_get_comp (hdr) != IH_COMP_NONE) {
355 /* reserve space for uncompressed image */
356 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
357 puts("Insufficient space for decompression\n");
361 switch (image_get_comp (hdr)) {
363 puts("Uncompressing (GZIP) ... ");
364 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
366 puts("GUNZIP ERROR\n");
374 puts("Uncompressing (BZIP2) ... ");
376 uint retlen = IMAGE_SIZE;
377 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
378 (char *)data, len, 0, 0);
382 printf ("BUNZIP2 ERROR: %d\n", rc);
390 printf ("Unimplemented compression type %d\n",
391 image_get_comp (hdr));
396 rc = mpl_prg(buf, len);
399 rc = mpl_prg(data, len);
405 #if !defined(CONFIG_PATI)
406 void get_backup_values(backup_t *buf)
408 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
411 void set_backup_values(int overwrite)
416 get_backup_values(&back);
418 if(strncmp(back.signature,"MPL\0",4)==0) {
419 puts("Not possible to write Backup\n");
423 memcpy(back.signature,"MPL\0",4);
424 i = getenv_f("serial#",back.serial_name,16);
426 puts("Not possible to write Backup\n");
429 back.serial_name[16]=0;
430 i = getenv_f("ethaddr",back.eth_addr,20);
432 puts("Not possible to write Backup\n");
436 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
439 void clear_env_values(void)
442 unsigned char env_crc[4];
444 memset(&back,0xff,sizeof(backup_t));
445 memset(env_crc,0x00,4);
446 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
447 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
451 * check crc of "older" environment
453 int check_env_old_size(ulong oldsize)
460 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
462 (uchar *)&crc, sizeof(ulong));
469 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
471 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
472 new = crc32 (new, buf, n);
480 static ulong oldsizes[] = {
486 void copy_old_env(ulong size)
489 uchar value_buf[0x800];
496 value = &value_buf[0];
500 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
509 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
516 value = &value_buf[0];
517 if(strncmp((char *)name,"baudrate",8)!=0) {
518 setenv((char *)name,(char *)value);
536 if(check_env_old_size(oldsizes[i]))
541 /* no old environment has been found */
542 get_backup_values (&back);
543 if (strncmp (back.signature, "MPL\0", 4) == 0) {
544 sprintf (buf, "%s", back.serial_name);
545 setenv ("serial#", buf);
546 sprintf (buf, "%s", back.eth_addr);
547 setenv ("ethaddr", buf);
548 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
553 copy_old_env(oldsizes[i]);
554 puts("INFO: old environment ajusted, use saveenv\n");
558 /* check if back up is set */
559 get_backup_values(&back);
560 if(strncmp(back.signature,"MPL\0",4)!=0) {
561 set_backup_values(0);
566 #endif /* #if !defined(CONFIG_PATI) */
568 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
572 #if !defined(CONFIG_PATI)
573 ulong size = IMAGE_SIZE;
574 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
578 if (strcmp(argv[1], "flash") == 0)
580 #if defined(CONFIG_CMD_FDC)
581 if (strcmp(argv[2], "floppy") == 0) {
583 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
584 puts("\nupdating bootloader image from floppy\n");
585 local_args[0] = argv[0];
587 local_args[1] = argv[3];
588 local_args[2] = NULL;
589 ld_addr=simple_strtoul(argv[3], NULL, 16);
590 result=do_fdcboot(cmdtp, 0, 2, local_args);
593 local_args[1] = NULL;
594 ld_addr=CONFIG_SYS_LOAD_ADDR;
595 result=do_fdcboot(cmdtp, 0, 1, local_args);
597 result=mpl_prg_image((uchar *)ld_addr);
601 if (strcmp(argv[2], "mem") == 0) {
603 ld_addr=simple_strtoul(argv[3], NULL, 16);
608 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
609 result=mpl_prg_image((uchar *)ld_addr);
612 #if !defined(CONFIG_PATI)
613 if (strcmp(argv[2], "mps") == 0) {
614 puts("\nupdating bootloader image from MPS\n");
615 result=mpl_prg((uchar *)src,size);
618 #endif /* #if !defined(CONFIG_PATI) */
620 #if !defined(CONFIG_PATI)
621 if (strcmp(argv[1], "clearenvvalues") == 0)
623 if (strcmp(argv[2], "yes") == 0)
629 if (strcmp(argv[1], "getback") == 0) {
630 get_backup_values(&back);
632 back.serial_name[16]=0;
634 printf("GetBackUp: signature: %s\n",back.signature);
635 printf(" serial#: %s\n",back.serial_name);
636 printf(" ethaddr: %s\n",back.eth_addr);
639 if (strcmp(argv[1], "setback") == 0) {
640 set_backup_values(1);
644 return cmd_usage(cmdtp);
648 #if defined(CONFIG_CMD_DOC)
651 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
657 /******************************************************
658 * Routines to display the Board information
659 * to the screen (since the VGA will be initialized as last,
660 * we must resend the infos)
663 #ifdef CONFIG_CONSOLE_EXTRA_INFO
664 extern GraphicDevice ctfb;
665 extern int get_boot_mode(void);
667 void video_get_info_str (int line_number, char *info)
669 /* init video info strings for graphic console */
670 PPC4xx_SYS_INFO sys_info;
675 char buf1[32], buf2[32], buf3[32], buf4[32];
681 /* CPU and board infos */
683 get_sys_info (&sys_info);
685 case PVR_405GP_RB: rev='B'; break;
686 case PVR_405GP_RC: rev='C'; break;
687 case PVR_405GP_RD: rev='D'; break;
688 case PVR_405GP_RE: rev='E'; break;
689 case PVR_405GPR_RB: rev='B'; break;
690 default: rev='?'; break;
692 if(pvr==PVR_405GPR_RB)
693 sprintf(cpustr,"PPC405GPr %c",rev);
695 sprintf(cpustr,"PPC405GP %c",rev);
698 s=getenv ("serial#");
700 if (!s || strncmp (s, "PIP405", 6)) {
701 sprintf(buf,"### No HW ID - assuming PIP405");
705 if (!s || strncmp (s, "MIP405", 6)) {
706 sprintf(buf,"### No HW ID - assuming MIP405");
710 for (e = s; *e; ++e) {
721 sprintf(&buf[i]," SN ");
728 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
730 strmhz (buf1, gd->cpu_clk),
731 strmhz (buf2, sys_info.freqPLB),
732 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
733 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
737 boot = get_boot_mode();
738 bc = in8 (CONFIG_PORT_ADDR);
739 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
740 gd->bd->bi_memsize / 0x100000,
741 gd->bd->bi_flashsize / 0x100000,
743 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
747 sprintf (buf, "%s",CONFIG_IDENT_STRING);
748 sprintf (info, " %s", &buf[1]);
751 /* no more info lines */
755 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
757 #endif /* CONFIG_VIDEO */