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 <405gp_pci.h>
42 #include "../mip405/mip405.h"
43 #include <405gp_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 CFG_MONITOR_LEN /* ugly, but it works for now */
58 extern flash_info_t flash_info[]; /* info for FLASH chips */
60 static image_header_t header;
64 mpl_prg(uchar *src, ulong size)
69 #if defined(CONFIG_PATI)
72 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
73 char *copystr = (char *)src;
74 ulong *magic = (ulong *)src;
77 info = &flash_info[0];
79 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
80 if (ntohl(magic[0]) != IH_MAGIC) {
81 puts("Bad Magic number\n");
84 /* some more checks before we delete the Flash... */
85 /* Checking the ISO_STRING prevents to program a
86 * wrong Firmware Image into the flash.
88 i = 4; /* skip Magic number */
90 if (strncmp(©str[i], "MEV-", 4) == 0)
93 puts("Firmware Image for unknown Target\n");
97 /* we have the ISO STRING, check */
98 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
99 printf("Wrong Firmware Image: %s\n", ©str[i]);
102 #if !defined(CONFIG_PATI)
104 for (i = info->sector_count-1; i > 0; i--) {
105 info->protect[i] = 0; /* unprotect this sector */
106 if (start >= info->start[i])
109 /* set-up flash location */
110 /* now erase flash */
111 printf("Erasing at %lx (sector %d) (start %lx)\n",
112 start,i,info->start[i]);
113 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
119 #else /* #if !defined(CONFIG_PATI */
122 for (i = 0; i < info->sector_count; i++) {
123 if (start < info->start[i]) {
129 info->protect[i - 1] = 0; /* unprotect this sector */
130 for (; i < info->sector_count; i++) {
131 if ((start + size) < info->start[i])
133 info->protect[i] = 0; /* unprotect this sector */
137 /* set-up flash location */
138 /* now erase flash */
139 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
140 start, start + size, start_sect, i,
141 info->start[start_sect], info->start[i]);
142 if ((rc = flash_erase (info, start_sect, i)) != 0) {
147 #endif /* defined(CONFIG_PATI) */
149 #elif defined(CONFIG_VCMA9)
151 for (i = 0; i <info->sector_count; i++) {
152 info->protect[i] = 0; /* unprotect this sector */
153 if (size < info->start[i])
156 /* set-up flash location */
157 /* now erase flash */
158 printf("Erasing at %lx (sector %d) (start %lx)\n",
159 start,0,info->start[0]);
160 if ((rc = flash_erase (info, 0, i)) != 0) {
167 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
169 if ((rc = flash_write ((char *)src, start, size)) != 0) {
174 puts("OK programming done\n");
180 mpl_prg_image(uchar *ld_addr)
182 unsigned long len, checksum;
184 image_header_t *hdr = &header;
187 /* Copy header so we can blank CRC field for re-calculation */
188 memcpy (&header, (char *)ld_addr, sizeof(image_header_t));
189 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
190 puts("Bad Magic Number\n");
193 print_image_hdr(hdr);
194 if (hdr->ih_os != IH_OS_U_BOOT) {
195 puts("No U-Boot Image\n");
198 if (hdr->ih_type != IH_TYPE_FIRMWARE) {
199 puts("No Firmware Image\n");
202 data = (uchar *)&header;
203 len = sizeof(image_header_t);
204 checksum = ntohl(hdr->ih_hcrc);
206 if (crc32 (0, (uchar *)data, len) != checksum) {
207 puts("Bad Header Checksum\n");
210 data = ld_addr + sizeof(image_header_t);
211 len = ntohl(hdr->ih_size);
212 puts("Verifying Checksum ... ");
213 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
214 puts("Bad Data CRC\n");
219 if (hdr->ih_comp != IH_COMP_NONE) {
221 /* reserve space for uncompressed image */
222 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
223 puts("Insufficient space for decompression\n");
227 switch (hdr->ih_comp) {
229 puts("Uncompressing (GZIP) ... ");
230 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
232 puts("GUNZIP ERROR\n");
240 puts("Uncompressing (BZIP2) ... ");
242 uint retlen = IMAGE_SIZE;
243 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
244 (char *)data, len, 0, 0);
248 printf ("BUNZIP2 ERROR: %d\n", rc);
256 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
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(CFG_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(CFG_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(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
312 i2c_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,2,(void *)env_crc,4);
316 * check crc of "older" environment
318 int check_env_old_size(ulong oldsize)
325 eeprom_read (CFG_DEF_EEPROM_ADDR,
327 (uchar *)&crc, sizeof(ulong));
334 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
336 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_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];
365 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
374 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
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 (CONFIG_COMMANDS & CFG_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=CFG_LOAD_ADDR;
489 result=do_fdcboot(cmdtp, 0, 1, local_args);
491 result=mpl_prg_image((uchar *)ld_addr);
494 #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
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-=CFG_MEMTEST_START;
523 src-=(100*1024); /* - 100k */
528 printf("\n\nPass %ld\n",size);
529 mem_test(CFG_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);
562 printf("Usage:\n%s\n", cmdtp->usage);
567 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
568 extern void doc_probe(ulong physadr);
571 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
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 PPC405_SYS_INFO sys_info;
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 sprintf(buf,"### No HW ID - assuming PIP405");
625 if (!s || strncmp (s, "MIP405", 6)) {
626 sprintf(buf,"### No HW ID - assuming MIP405");
630 for (e = s; *e; ++e) {
641 sprintf(&buf[i]," SN ");
648 sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
650 strmhz (tmp, gd->cpu_clk), sys_info.freqPLB / 1000000,
651 sys_info.freqPLB / sys_info.pllOpbDiv / 1000000,
652 sys_info.freqPLB / sys_info.pllExtBusDiv / 1000000);
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 */