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 #elif defined(CONFIG_VCMA9)
280 /* search end sector */
281 for (i = 0; i < info->sector_count; i++)
282 if (size < info->start[i])
285 flash_protect(FLAG_PROTECT_CLEAR,
290 /* now erase flash */
291 printf("Erasing at %lx (sector %d) (start %lx)\n",
292 start,0,info->start[0]);
293 if ((rc = flash_erase (info, 0, i)) != 0) {
300 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
302 if ((rc = flash_write ((char *)src, start, size)) != 0) {
307 puts("OK programming done\n");
313 mpl_prg_image(uchar *ld_addr)
317 image_header_t *hdr = (image_header_t *)ld_addr;
320 #if defined(CONFIG_FIT)
321 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
322 puts ("Non legacy image format not supported\n");
327 if (!image_check_magic (hdr)) {
328 puts("Bad Magic Number\n");
331 image_print_contents (hdr);
332 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
333 puts("No U-Boot Image\n");
336 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
337 puts("No Firmware Image\n");
340 if (!image_check_hcrc (hdr)) {
341 puts("Bad Header Checksum\n");
344 puts("Verifying Checksum ... ");
345 if (!image_check_dcrc (hdr)) {
346 puts("Bad Data CRC\n");
351 data = (uchar *)image_get_data (hdr);
352 len = image_get_data_size (hdr);
354 if (image_get_comp (hdr) != IH_COMP_NONE) {
356 /* reserve space for uncompressed image */
357 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
358 puts("Insufficient space for decompression\n");
362 switch (image_get_comp (hdr)) {
364 puts("Uncompressing (GZIP) ... ");
365 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
367 puts("GUNZIP ERROR\n");
375 puts("Uncompressing (BZIP2) ... ");
377 uint retlen = IMAGE_SIZE;
378 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
379 (char *)data, len, 0, 0);
383 printf ("BUNZIP2 ERROR: %d\n", rc);
391 printf ("Unimplemented compression type %d\n",
392 image_get_comp (hdr));
397 rc = mpl_prg(buf, len);
400 rc = mpl_prg(data, len);
406 #if !defined(CONFIG_PATI)
407 void get_backup_values(backup_t *buf)
409 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
412 void set_backup_values(int overwrite)
417 get_backup_values(&back);
419 if(strncmp(back.signature,"MPL\0",4)==0) {
420 puts("Not possible to write Backup\n");
424 memcpy(back.signature,"MPL\0",4);
425 i = getenv_f("serial#",back.serial_name,16);
427 puts("Not possible to write Backup\n");
430 back.serial_name[16]=0;
431 i = getenv_f("ethaddr",back.eth_addr,20);
433 puts("Not possible to write Backup\n");
437 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
440 void clear_env_values(void)
443 unsigned char env_crc[4];
445 memset(&back,0xff,sizeof(backup_t));
446 memset(env_crc,0x00,4);
447 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
448 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
452 * check crc of "older" environment
454 int check_env_old_size(ulong oldsize)
461 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
463 (uchar *)&crc, sizeof(ulong));
470 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
472 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
473 new = crc32 (new, buf, n);
481 static ulong oldsizes[] = {
487 void copy_old_env(ulong size)
490 uchar value_buf[0x800];
497 value = &value_buf[0];
501 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
510 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
517 value = &value_buf[0];
518 if(strncmp((char *)name,"baudrate",8)!=0) {
519 setenv((char *)name,(char *)value);
537 if(check_env_old_size(oldsizes[i]))
542 /* no old environment has been found */
543 get_backup_values (&back);
544 if (strncmp (back.signature, "MPL\0", 4) == 0) {
545 sprintf (buf, "%s", back.serial_name);
546 setenv ("serial#", buf);
547 sprintf (buf, "%s", back.eth_addr);
548 setenv ("ethaddr", buf);
549 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
554 copy_old_env(oldsizes[i]);
555 puts("INFO: old environment ajusted, use saveenv\n");
559 /* check if back up is set */
560 get_backup_values(&back);
561 if(strncmp(back.signature,"MPL\0",4)!=0) {
562 set_backup_values(0);
567 #endif /* #if !defined(CONFIG_PATI) */
569 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
573 #if !defined(CONFIG_PATI)
574 ulong size = IMAGE_SIZE;
575 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
579 if (strcmp(argv[1], "flash") == 0)
581 #if defined(CONFIG_CMD_FDC)
582 if (strcmp(argv[2], "floppy") == 0) {
584 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
585 puts("\nupdating bootloader image from floppy\n");
586 local_args[0] = argv[0];
588 local_args[1] = argv[3];
589 local_args[2] = NULL;
590 ld_addr=simple_strtoul(argv[3], NULL, 16);
591 result=do_fdcboot(cmdtp, 0, 2, local_args);
594 local_args[1] = NULL;
595 ld_addr=CONFIG_SYS_LOAD_ADDR;
596 result=do_fdcboot(cmdtp, 0, 1, local_args);
598 result=mpl_prg_image((uchar *)ld_addr);
602 if (strcmp(argv[2], "mem") == 0) {
604 ld_addr=simple_strtoul(argv[3], NULL, 16);
609 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
610 result=mpl_prg_image((uchar *)ld_addr);
613 #if !defined(CONFIG_PATI)
614 if (strcmp(argv[2], "mps") == 0) {
615 puts("\nupdating bootloader image from MPS\n");
616 result=mpl_prg((uchar *)src,size);
619 #endif /* #if !defined(CONFIG_PATI) */
621 #if !defined(CONFIG_PATI)
622 if (strcmp(argv[1], "clearenvvalues") == 0)
624 if (strcmp(argv[2], "yes") == 0)
630 if (strcmp(argv[1], "getback") == 0) {
631 get_backup_values(&back);
633 back.serial_name[16]=0;
635 printf("GetBackUp: signature: %s\n",back.signature);
636 printf(" serial#: %s\n",back.serial_name);
637 printf(" ethaddr: %s\n",back.eth_addr);
640 if (strcmp(argv[1], "setback") == 0) {
641 set_backup_values(1);
645 return cmd_usage(cmdtp);
649 #if defined(CONFIG_CMD_DOC)
652 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
658 /******************************************************
659 * Routines to display the Board information
660 * to the screen (since the VGA will be initialized as last,
661 * we must resend the infos)
664 #ifdef CONFIG_CONSOLE_EXTRA_INFO
665 extern GraphicDevice ctfb;
666 extern int get_boot_mode(void);
668 void video_get_info_str (int line_number, char *info)
670 /* init video info strings for graphic console */
671 PPC4xx_SYS_INFO sys_info;
676 char buf1[32], buf2[32], buf3[32], buf4[32];
682 /* CPU and board infos */
684 get_sys_info (&sys_info);
686 case PVR_405GP_RB: rev='B'; break;
687 case PVR_405GP_RC: rev='C'; break;
688 case PVR_405GP_RD: rev='D'; break;
689 case PVR_405GP_RE: rev='E'; break;
690 case PVR_405GPR_RB: rev='B'; break;
691 default: rev='?'; break;
693 if(pvr==PVR_405GPR_RB)
694 sprintf(cpustr,"PPC405GPr %c",rev);
696 sprintf(cpustr,"PPC405GP %c",rev);
699 s=getenv ("serial#");
701 if (!s || strncmp (s, "PIP405", 6)) {
702 strcpy(buf,"### No HW ID - assuming PIP405");
705 #if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
706 if (!s || strncmp (s, "MIP405", 6)) {
707 strcpy(buf,"### No HW ID - assuming MIP405");
711 for (e = s; *e; ++e) {
722 strcpy(&buf[i]," SN ");
729 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
731 strmhz (buf1, gd->cpu_clk),
732 strmhz (buf2, sys_info.freqPLB),
733 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
734 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
738 boot = get_boot_mode();
739 bc = in8 (CONFIG_PORT_ADDR);
740 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
741 gd->bd->bi_memsize / 0x100000,
742 gd->bd->bi_flashsize / 0x100000,
744 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
748 strcpy(buf, CONFIG_IDENT_STRING);
749 sprintf (info, " %s", &buf[1]);
752 /* no more info lines */
756 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
758 #endif /* CONFIG_VIDEO */