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 extern int mem_test(ulong start, ulong ramsize, int quiet);
33 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
34 #define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
36 #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) \
37 || defined(CONFIG_TARGET_MIP405T)
38 /*-----------------------------------------------------------------------
39 * On PIP/MIP405 we have 3 (4) possible boot mode
41 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
42 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
43 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
44 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
45 * The flash init is the first board specific routine which is called
46 * after code relocation (running from SDRAM)
47 * The first thing we do is to map the Flash CS to the Flash area and
48 * the MPS CS to the MPS area. Since the flash size is unknown at this
49 * point, we use the max flash size and the lowest flash address as base.
51 * After flash detection we adjust the size of the CS area accordingly.
52 * update_flash_size() will fix in wrong values in the flash_info structure,
53 * misc_init_r() will fix the values in the board info structure
55 int get_boot_mode(void)
59 pbcr = mfdcr(CPC0_PSR);
60 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
61 /* boot via MPS or MPS mapping */
63 if (pbcr & PSR_ROM_LOC)
69 /* Map the flash high (in boot area)
70 This code can only be executed from SDRAM (after relocation).
72 void setup_cs_reloc(void)
76 * since we are relocated, we can set-up the CS finaly
77 * but first of all, switch off PCI mapping (in case it
82 mode = get_boot_mode();
84 * we map the flash high in every case
85 * first find out to which CS the flash is attached to
87 if (mode & BOOT_MPS) {
88 /* map flash high on CS1 and MPS on CS0 */
89 mtdcr(EBC0_CFGADDR, PB0AP);
90 mtdcr(EBC0_CFGDATA, MPS_AP);
91 mtdcr(EBC0_CFGADDR, PB0CR);
92 mtdcr(EBC0_CFGDATA, MPS_CR);
94 * we use the default values (max values) for the flash
95 * because its real size is not yet known
97 mtdcr(EBC0_CFGADDR, PB1AP);
98 mtdcr(EBC0_CFGDATA, FLASH_AP);
99 mtdcr(EBC0_CFGADDR, PB1CR);
100 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
102 /* map flash high on CS0 and MPS on CS1 */
103 mtdcr(EBC0_CFGADDR, PB1AP);
104 mtdcr(EBC0_CFGDATA, MPS_AP);
105 mtdcr(EBC0_CFGADDR, PB1CR);
106 mtdcr(EBC0_CFGDATA, MPS_CR);
108 * we use the default values (max values) for the flash
109 * because its real size is not yet known
111 mtdcr(EBC0_CFGADDR, PB0AP);
112 mtdcr(EBC0_CFGDATA, FLASH_AP);
113 mtdcr(EBC0_CFGADDR, PB0CR);
114 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
117 #endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) */
119 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
120 /* adjust flash start and protection info */
121 int update_flash_size(int flash_size)
124 flash_info_t *info = &flash_info[0];
125 unsigned long flashcr;
126 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
128 if (flash_size > 128*1024*1024) {
129 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
134 if ((flash_size >> 20) != 0)
135 i = __ilog2(flash_size >> 20);
137 /* set up flash CS according to the size */
138 mode = get_boot_mode();
139 if (mode & BOOT_MPS) {
140 /* flash is on CS1 */
141 mtdcr(EBC0_CFGADDR, PB1CR);
142 flashcr = mfdcr(EBC0_CFGDATA);
143 /* we map the flash high in every case */
144 flashcr &= 0x0001FFFF; /* mask out address bits */
145 flashcr |= flash_base; /* start addr */
146 flashcr |= (i << 17); /* size addr */
147 mtdcr(EBC0_CFGADDR, PB1CR);
148 mtdcr(EBC0_CFGDATA, flashcr);
150 /* flash is on CS0 */
151 mtdcr(EBC0_CFGADDR, PB0CR);
152 flashcr = mfdcr(EBC0_CFGDATA);
153 /* we map the flash high in every case */
154 flashcr &= 0x0001FFFF; /* mask out address bits */
155 flashcr |= flash_base; /* start addr */
156 flashcr |= (i << 17); /* size addr */
157 mtdcr(EBC0_CFGADDR, PB0CR);
158 mtdcr(EBC0_CFGDATA, flashcr);
161 for (i = 0; i < info->sector_count; i++)
162 /* adjust sector start address */
163 info->start[i] = flash_base +
164 (info->start[i] - CONFIG_SYS_FLASH_BASE);
166 /* unprotect all sectors */
167 flash_protect(FLAG_PROTECT_CLEAR,
171 flash_protect_default();
172 /* protect reset vector too*/
173 flash_protect(FLAG_PROTECT_SET,
174 info->start[info->sector_count-1],
183 mpl_prg(uchar *src, ulong size)
186 flash_info_t *info = &flash_info[0];
188 #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) || \
189 defined(CONFIG_TARGET_MIP405T)
190 char *copystr = (char *)src;
191 ulong *magic = (ulong *)src;
193 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
194 puts("Bad Magic number\n");
197 /* some more checks before we delete the Flash... */
198 /* Checking the ISO_STRING prevents to program a
199 * wrong Firmware Image into the flash.
201 i = 4; /* skip Magic number */
203 if (strncmp(©str[i], "MEV-", 4) == 0)
206 puts("Firmware Image for unknown Target\n");
210 /* we have the ISO STRING, check */
211 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
212 printf("Wrong Firmware Image: %s\n", ©str[i]);
217 /* unprotect sectors used by u-boot */
218 flash_protect(FLAG_PROTECT_CLEAR,
223 /* search start sector */
224 for (i = info->sector_count-1; i > 0; i--)
225 if (start >= info->start[i])
228 /* now erase flash */
229 printf("Erasing at %lx (sector %d) (start %lx)\n",
230 start,i,info->start[i]);
231 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
237 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
239 if ((rc = flash_write ((char *)src, start, size)) != 0) {
244 puts("OK programming done\n");
250 mpl_prg_image(uchar *ld_addr)
254 image_header_t *hdr = (image_header_t *)ld_addr;
257 #if defined(CONFIG_FIT)
258 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
259 puts ("Non legacy image format not supported\n");
264 if (!image_check_magic (hdr)) {
265 puts("Bad Magic Number\n");
268 image_print_contents (hdr);
269 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
270 puts("No U-Boot Image\n");
273 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
274 puts("No Firmware Image\n");
277 if (!image_check_hcrc (hdr)) {
278 puts("Bad Header Checksum\n");
281 puts("Verifying Checksum ... ");
282 if (!image_check_dcrc (hdr)) {
283 puts("Bad Data CRC\n");
288 data = (uchar *)image_get_data (hdr);
289 len = image_get_data_size (hdr);
291 if (image_get_comp (hdr) != IH_COMP_NONE) {
293 /* reserve space for uncompressed image */
294 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
295 puts("Insufficient space for decompression\n");
299 switch (image_get_comp (hdr)) {
301 puts("Uncompressing (GZIP) ... ");
302 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
304 puts("GUNZIP ERROR\n");
312 puts("Uncompressing (BZIP2) ... ");
314 uint retlen = IMAGE_SIZE;
315 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
316 (char *)data, len, 0, 0);
320 printf ("BUNZIP2 ERROR: %d\n", rc);
328 printf ("Unimplemented compression type %d\n",
329 image_get_comp (hdr));
334 rc = mpl_prg(buf, len);
337 rc = mpl_prg(data, len);
343 void get_backup_values(backup_t *buf)
345 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
348 void set_backup_values(int overwrite)
353 get_backup_values(&back);
355 if(strncmp(back.signature,"MPL\0",4)==0) {
356 puts("Not possible to write Backup\n");
360 memcpy(back.signature,"MPL\0",4);
361 i = getenv_f("serial#",back.serial_name,16);
363 puts("Not possible to write Backup\n");
366 back.serial_name[16]=0;
367 i = getenv_f("ethaddr",back.eth_addr,20);
369 puts("Not possible to write Backup\n");
373 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
376 void clear_env_values(void)
379 unsigned char env_crc[4];
381 memset(&back,0xff,sizeof(backup_t));
382 memset(env_crc,0x00,4);
383 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
384 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
388 * check crc of "older" environment
390 int check_env_old_size(ulong oldsize)
397 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
399 (uchar *)&crc, sizeof(ulong));
406 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
408 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
409 new = crc32 (new, buf, n);
417 static ulong oldsizes[] = {
423 void copy_old_env(ulong size)
426 uchar value_buf[0x800];
433 value = &value_buf[0];
437 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
446 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
453 value = &value_buf[0];
454 if(strncmp((char *)name,"baudrate",8)!=0) {
455 setenv((char *)name,(char *)value);
473 if(check_env_old_size(oldsizes[i]))
478 /* no old environment has been found */
479 get_backup_values (&back);
480 if (strncmp (back.signature, "MPL\0", 4) == 0) {
481 sprintf (buf, "%s", back.serial_name);
482 setenv ("serial#", buf);
483 sprintf (buf, "%s", back.eth_addr);
484 setenv ("ethaddr", buf);
485 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
490 copy_old_env(oldsizes[i]);
491 puts("INFO: old environment ajusted, use saveenv\n");
495 /* check if back up is set */
496 get_backup_values(&back);
497 if(strncmp(back.signature,"MPL\0",4)!=0) {
498 set_backup_values(0);
503 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
507 ulong size = IMAGE_SIZE;
508 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
511 if (strcmp(argv[1], "flash") == 0)
513 #if defined(CONFIG_CMD_FDC)
514 if (strcmp(argv[2], "floppy") == 0) {
516 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
517 puts("\nupdating bootloader image from floppy\n");
518 local_args[0] = argv[0];
520 local_args[1] = argv[3];
521 local_args[2] = NULL;
522 ld_addr=simple_strtoul(argv[3], NULL, 16);
523 result=do_fdcboot(cmdtp, 0, 2, local_args);
526 local_args[1] = NULL;
527 ld_addr=CONFIG_SYS_LOAD_ADDR;
528 result=do_fdcboot(cmdtp, 0, 1, local_args);
530 result=mpl_prg_image((uchar *)ld_addr);
534 if (strcmp(argv[2], "mem") == 0) {
536 ld_addr=simple_strtoul(argv[3], NULL, 16);
541 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
542 result=mpl_prg_image((uchar *)ld_addr);
545 if (strcmp(argv[2], "mps") == 0) {
546 puts("\nupdating bootloader image from MPS\n");
547 result=mpl_prg((uchar *)src,size);
551 if (strcmp(argv[1], "clearenvvalues") == 0)
553 if (strcmp(argv[2], "yes") == 0)
559 if (strcmp(argv[1], "getback") == 0) {
560 get_backup_values(&back);
562 back.serial_name[16]=0;
564 printf("GetBackUp: signature: %s\n",back.signature);
565 printf(" serial#: %s\n",back.serial_name);
566 printf(" ethaddr: %s\n",back.eth_addr);
569 if (strcmp(argv[1], "setback") == 0) {
570 set_backup_values(1);
573 return cmd_usage(cmdtp);
577 /******************************************************
578 * Routines to display the Board information
579 * to the screen (since the VGA will be initialized as last,
580 * we must resend the infos)
583 #ifdef CONFIG_CONSOLE_EXTRA_INFO
584 extern GraphicDevice ctfb;
585 extern int get_boot_mode(void);
587 void video_get_info_str (int line_number, char *info)
589 /* init video info strings for graphic console */
590 PPC4xx_SYS_INFO sys_info;
595 char buf1[32], buf2[32], buf3[32], buf4[32];
601 /* CPU and board infos */
603 get_sys_info (&sys_info);
605 case PVR_405GP_RB: rev='B'; break;
606 case PVR_405GP_RC: rev='C'; break;
607 case PVR_405GP_RD: rev='D'; break;
608 case PVR_405GP_RE: rev='E'; break;
609 case PVR_405GPR_RB: rev='B'; break;
610 default: rev='?'; break;
612 if(pvr==PVR_405GPR_RB)
613 sprintf(cpustr,"PPC405GPr %c",rev);
615 sprintf(cpustr,"PPC405GP %c",rev);
618 s=getenv ("serial#");
620 if (!s || strncmp (s, "PIP405", 6)) {
621 strcpy(buf,"### No HW ID - assuming PIP405");
624 #if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
625 if (!s || strncmp (s, "MIP405", 6)) {
626 strcpy(buf,"### No HW ID - assuming MIP405");
630 for (e = s; *e; ++e) {
641 strcpy(&buf[i]," SN ");
648 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
650 strmhz (buf1, gd->cpu_clk),
651 strmhz (buf2, sys_info.freqPLB),
652 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
653 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
657 boot = get_boot_mode();
658 bc = in8 (CONFIG_PORT_ADDR);
659 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
660 gd->bd->bi_memsize / 0x100000,
661 gd->bd->bi_flashsize / 0x100000,
663 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
667 strcpy(buf, CONFIG_IDENT_STRING);
668 sprintf (info, " %s", &buf[1]);
671 /* no more info lines */
675 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
677 #endif /* CONFIG_VIDEO */