3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include "common_util.h"
29 #include <asm/processor.h>
30 #include <asm/byteorder.h>
38 #include "../pip405/pip405.h"
39 #include <asm/4xx_pci.h>
42 #include "../mip405/mip405.h"
43 #include <asm/4xx_pci.h>
46 DECLARE_GLOBAL_DATA_PTR;
48 #if defined(CONFIG_PATI)
49 #define FIRM_START 0xFFF00000
52 extern int gunzip(void *, int, uchar *, unsigned long *);
53 extern int mem_test(ulong start, ulong ramsize, int quiet);
55 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
56 #define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
58 extern flash_info_t flash_info[]; /* info for FLASH chips */
61 mpl_prg(uchar *src, ulong size)
66 #if defined(CONFIG_PATI)
69 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
70 char *copystr = (char *)src;
71 ulong *magic = (ulong *)src;
74 info = &flash_info[0];
76 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
77 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
78 puts("Bad Magic number\n");
81 /* some more checks before we delete the Flash... */
82 /* Checking the ISO_STRING prevents to program a
83 * wrong Firmware Image into the flash.
85 i = 4; /* skip Magic number */
87 if (strncmp(©str[i], "MEV-", 4) == 0)
90 puts("Firmware Image for unknown Target\n");
94 /* we have the ISO STRING, check */
95 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
96 printf("Wrong Firmware Image: %s\n", ©str[i]);
99 #if !defined(CONFIG_PATI)
101 for (i = info->sector_count-1; i > 0; i--) {
102 info->protect[i] = 0; /* unprotect this sector */
103 if (start >= info->start[i])
106 /* set-up flash location */
107 /* now erase flash */
108 printf("Erasing at %lx (sector %d) (start %lx)\n",
109 start,i,info->start[i]);
110 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
116 #else /* #if !defined(CONFIG_PATI */
119 for (i = 0; i < info->sector_count; i++) {
120 if (start < info->start[i]) {
126 info->protect[i - 1] = 0; /* unprotect this sector */
127 for (; i < info->sector_count; i++) {
128 if ((start + size) < info->start[i])
130 info->protect[i] = 0; /* unprotect this sector */
134 /* set-up flash location */
135 /* now erase flash */
136 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
137 start, start + size, start_sect, i,
138 info->start[start_sect], info->start[i]);
139 if ((rc = flash_erase (info, start_sect, i)) != 0) {
144 #endif /* defined(CONFIG_PATI) */
146 #elif defined(CONFIG_VCMA9)
148 for (i = 0; i <info->sector_count; i++) {
149 info->protect[i] = 0; /* unprotect this sector */
150 if (size < info->start[i])
153 /* set-up flash location */
154 /* now erase flash */
155 printf("Erasing at %lx (sector %d) (start %lx)\n",
156 start,0,info->start[0]);
157 if ((rc = flash_erase (info, 0, i)) != 0) {
164 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
166 if ((rc = flash_write ((char *)src, start, size)) != 0) {
171 puts("OK programming done\n");
177 mpl_prg_image(uchar *ld_addr)
181 image_header_t *hdr = (image_header_t *)ld_addr;
184 #if defined(CONFIG_FIT)
185 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
186 puts ("Non legacy image format not supported\n");
191 if (!image_check_magic (hdr)) {
192 puts("Bad Magic Number\n");
195 image_print_contents (hdr);
196 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
197 puts("No U-Boot Image\n");
200 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
201 puts("No Firmware Image\n");
204 if (!image_check_hcrc (hdr)) {
205 puts("Bad Header Checksum\n");
208 puts("Verifying Checksum ... ");
209 if (!image_check_dcrc (hdr)) {
210 puts("Bad Data CRC\n");
215 data = (uchar *)image_get_data (hdr);
216 len = image_get_data_size (hdr);
218 if (image_get_comp (hdr) != IH_COMP_NONE) {
220 /* reserve space for uncompressed image */
221 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
222 puts("Insufficient space for decompression\n");
226 switch (image_get_comp (hdr)) {
228 puts("Uncompressing (GZIP) ... ");
229 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
231 puts("GUNZIP ERROR\n");
239 puts("Uncompressing (BZIP2) ... ");
241 uint retlen = IMAGE_SIZE;
242 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
243 (char *)data, len, 0, 0);
247 printf ("BUNZIP2 ERROR: %d\n", rc);
255 printf ("Unimplemented compression type %d\n",
256 image_get_comp (hdr));
261 rc = mpl_prg(buf, len);
264 rc = mpl_prg(data, len);
270 #if !defined(CONFIG_PATI)
271 void get_backup_values(backup_t *buf)
273 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
276 void set_backup_values(int overwrite)
281 get_backup_values(&back);
283 if(strncmp(back.signature,"MPL\0",4)==0) {
284 puts("Not possible to write Backup\n");
288 memcpy(back.signature,"MPL\0",4);
289 i = getenv_r("serial#",back.serial_name,16);
291 puts("Not possible to write Backup\n");
294 back.serial_name[16]=0;
295 i = getenv_r("ethaddr",back.eth_addr,20);
297 puts("Not possible to write Backup\n");
301 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
304 void clear_env_values(void)
307 unsigned char env_crc[4];
309 memset(&back,0xff,sizeof(backup_t));
310 memset(env_crc,0x00,4);
311 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
312 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
316 * check crc of "older" environment
318 int check_env_old_size(ulong oldsize)
325 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
327 (uchar *)&crc, sizeof(ulong));
334 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
336 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
337 new = crc32 (new, buf, n);
345 static ulong oldsizes[] = {
351 void copy_old_env(ulong size)
354 uchar value_buf[0x800];
361 value = &value_buf[0];
365 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
374 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
381 value = &value_buf[0];
382 if(strncmp((char *)name,"baudrate",8)!=0) {
383 setenv((char *)name,(char *)value);
401 if(check_env_old_size(oldsizes[i]))
406 /* no old environment has been found */
407 get_backup_values (&back);
408 if (strncmp (back.signature, "MPL\0", 4) == 0) {
409 sprintf (buf, "%s", back.serial_name);
410 setenv ("serial#", buf);
411 sprintf (buf, "%s", back.eth_addr);
412 setenv ("ethaddr", buf);
413 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
418 copy_old_env(oldsizes[i]);
419 puts("INFO: old environment ajusted, use saveenv\n");
423 /* check if back up is set */
424 get_backup_values(&back);
425 if(strncmp(back.signature,"MPL\0",4)!=0) {
426 set_backup_values(0);
432 extern device_t *stdio_devices[];
433 extern char *stdio_names[];
435 void show_stdio_dev(void)
437 /* Print information */
439 if (stdio_devices[stdin] == NULL) {
440 puts("No input devices available!\n");
442 printf ("%s\n", stdio_devices[stdin]->name);
446 if (stdio_devices[stdout] == NULL) {
447 puts("No output devices available!\n");
449 printf ("%s\n", stdio_devices[stdout]->name);
453 if (stdio_devices[stderr] == NULL) {
454 puts("No error devices available!\n");
456 printf ("%s\n", stdio_devices[stderr]->name);
460 #endif /* #if !defined(CONFIG_PATI) */
462 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
464 ulong size,src,ld_addr;
466 #if !defined(CONFIG_PATI)
468 src = MULTI_PURPOSE_SOCKET_ADDR;
472 if (strcmp(argv[1], "flash") == 0)
474 #if defined(CONFIG_CMD_FDC)
475 if (strcmp(argv[2], "floppy") == 0) {
477 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
478 puts("\nupdating bootloader image from floppy\n");
479 local_args[0] = argv[0];
481 local_args[1] = argv[3];
482 local_args[2] = NULL;
483 ld_addr=simple_strtoul(argv[3], NULL, 16);
484 result=do_fdcboot(cmdtp, 0, 2, local_args);
487 local_args[1] = NULL;
488 ld_addr=CONFIG_SYS_LOAD_ADDR;
489 result=do_fdcboot(cmdtp, 0, 1, local_args);
491 result=mpl_prg_image((uchar *)ld_addr);
495 if (strcmp(argv[2], "mem") == 0) {
497 ld_addr=simple_strtoul(argv[3], NULL, 16);
502 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
503 result=mpl_prg_image((uchar *)ld_addr);
506 #if !defined(CONFIG_PATI)
507 if (strcmp(argv[2], "mps") == 0) {
508 puts("\nupdating bootloader image from MPS\n");
509 result=mpl_prg((uchar *)src,size);
512 #endif /* #if !defined(CONFIG_PATI) */
514 if (strcmp(argv[1], "mem") == 0)
519 result = (int)simple_strtol(argv[2], NULL, 16);
521 src=(unsigned long)&result;
522 src-=CONFIG_SYS_MEMTEST_START;
523 src-=(100*1024); /* - 100k */
528 printf("\n\nPass %ld\n",size);
529 mem_test(CONFIG_SYS_MEMTEST_START,src,1);
538 #if !defined(CONFIG_PATI)
539 if (strcmp(argv[1], "clearenvvalues") == 0)
541 if (strcmp(argv[2], "yes") == 0)
547 if (strcmp(argv[1], "getback") == 0) {
548 get_backup_values(&back);
550 back.serial_name[16]=0;
552 printf("GetBackUp: signature: %s\n",back.signature);
553 printf(" serial#: %s\n",back.serial_name);
554 printf(" ethaddr: %s\n",back.eth_addr);
557 if (strcmp(argv[1], "setback") == 0) {
558 set_backup_values(1);
567 #if defined(CONFIG_CMD_DOC)
570 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
576 /******************************************************
577 * Routines to display the Board information
578 * to the screen (since the VGA will be initialized as last,
579 * we must resend the infos)
582 #ifdef CONFIG_CONSOLE_EXTRA_INFO
583 extern GraphicDevice ctfb;
584 extern int get_boot_mode(void);
586 void video_get_info_str (int line_number, char *info)
588 /* init video info strings for graphic console */
589 PPC4xx_SYS_INFO sys_info;
594 char buf1[32], buf2[32], buf3[32], buf4[32];
600 /* CPU and board infos */
602 get_sys_info (&sys_info);
604 case PVR_405GP_RB: rev='B'; break;
605 case PVR_405GP_RC: rev='C'; break;
606 case PVR_405GP_RD: rev='D'; break;
607 case PVR_405GP_RE: rev='E'; break;
608 case PVR_405GPR_RB: rev='B'; break;
609 default: rev='?'; break;
611 if(pvr==PVR_405GPR_RB)
612 sprintf(cpustr,"PPC405GPr %c",rev);
614 sprintf(cpustr,"PPC405GP %c",rev);
617 s=getenv ("serial#");
619 if (!s || strncmp (s, "PIP405", 6)) {
620 sprintf(buf,"### No HW ID - assuming PIP405");
624 if (!s || strncmp (s, "MIP405", 6)) {
625 sprintf(buf,"### No HW ID - assuming MIP405");
629 for (e = s; *e; ++e) {
640 sprintf(&buf[i]," SN ");
647 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
649 strmhz (buf1, gd->cpu_clk),
650 strmhz (buf2, sys_info.freqPLB),
651 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
652 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
656 boot = get_boot_mode();
657 bc = in8 (CONFIG_PORT_ADDR);
658 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
659 gd->bd->bi_memsize / 0x100000,
660 gd->bd->bi_flashsize / 0x100000,
662 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
666 sprintf (buf, "%s",CONFIG_IDENT_STRING);
667 sprintf (info, " %s", &buf[1]);
670 /* no more info lines */
674 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
676 #endif /* CONFIG_VIDEO */