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 extern int gunzip(void *, int, uchar *, int *);
47 extern int mem_test(ulong start, ulong ramsize, int quiet);
49 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
50 #define IMAGE_SIZE CFG_MONITOR_LEN /* ugly, but it works for now */
52 extern flash_info_t flash_info[]; /* info for FLASH chips */
54 static image_header_t header;
58 mpl_prg(uchar *src, ulong size)
63 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
64 char *copystr = (char *)src;
65 ulong *magic = (ulong *)src;
68 info = &flash_info[0];
70 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
71 if (ntohl(magic[0]) != IH_MAGIC) {
72 puts("Bad Magic number\n");
75 /* some more checks before we delete the Flash... */
76 /* Checking the ISO_STRING prevents to program a
77 * wrong Firmware Image into the flash.
79 i = 4; /* skip Magic number */
81 if (strncmp(©str[i], "MEV-", 4) == 0)
84 puts("Firmware Image for unknown Target\n");
88 /* we have the ISO STRING, check */
89 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
90 printf("Wrong Firmware Image: %s\n", ©str[i]);
94 for (i = info->sector_count-1; i > 0; i--) {
95 info->protect[i] = 0; /* unprotect this sector */
96 if (start >= info->start[i])
99 /* set-up flash location */
100 /* now erase flash */
101 printf("Erasing at %lx (sector %d) (start %lx)\n",
102 start,i,info->start[i]);
103 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
109 #elif defined(CONFIG_VCMA9)
111 for (i = 0; i <info->sector_count; i++) {
112 info->protect[i] = 0; /* unprotect this sector */
113 if (size < info->start[i])
116 /* set-up flash location */
117 /* now erase flash */
118 printf("Erasing at %lx (sector %d) (start %lx)\n",
119 start,0,info->start[0]);
120 if ((rc = flash_erase (info, 0, i)) != 0) {
127 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
129 if ((rc = flash_write (src, start, size)) != 0) {
134 puts("OK programming done\n");
140 mpl_prg_image(uchar *ld_addr)
142 unsigned long len, checksum;
144 image_header_t *hdr = &header;
147 /* Copy header so we can blank CRC field for re-calculation */
148 memcpy (&header, (char *)ld_addr, sizeof(image_header_t));
149 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
150 puts("Bad Magic Number\n");
153 print_image_hdr(hdr);
154 if (hdr->ih_os != IH_OS_U_BOOT) {
155 puts("No U-Boot Image\n");
158 if (hdr->ih_type != IH_TYPE_FIRMWARE) {
159 puts("No Firmware Image\n");
162 data = (uchar *)&header;
163 len = sizeof(image_header_t);
164 checksum = ntohl(hdr->ih_hcrc);
166 if (crc32 (0, (char *)data, len) != checksum) {
167 puts("Bad Header Checksum\n");
170 data = ld_addr + sizeof(image_header_t);
171 len = ntohl(hdr->ih_size);
172 puts("Verifying Checksum ... ");
173 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
174 puts("Bad Data CRC\n");
179 if (hdr->ih_comp != IH_COMP_NONE) {
181 /* reserve space for uncompressed image */
182 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
183 puts("Insufficient space for decompression\n");
187 switch (hdr->ih_comp) {
189 puts("Uncompressing (GZIP) ... ");
190 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, (int *)&len);
192 puts("GUNZIP ERROR\n");
200 puts("Uncompressing (BZIP2) ... ");
202 uint retlen = IMAGE_SIZE;
203 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
204 (char *)data, len, 0, 0);
208 printf ("BUNZIP2 ERROR: %d\n", rc);
216 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
221 rc = mpl_prg(buf, len);
224 rc = mpl_prg(data, len);
231 void get_backup_values(backup_t *buf)
233 i2c_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
236 void set_backup_values(int overwrite)
241 get_backup_values(&back);
243 if(strncmp(back.signature,"MPL\0",4)==0) {
244 puts("Not possible to write Backup\n");
248 memcpy(back.signature,"MPL\0",4);
249 i = getenv_r("serial#",back.serial_name,16);
251 puts("Not possible to write Backup\n");
254 back.serial_name[16]=0;
255 i = getenv_r("ethaddr",back.eth_addr,20);
257 puts("Not possible to write Backup\n");
261 i2c_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
264 void clear_env_values(void)
267 unsigned char env_crc[4];
269 memset(&back,0xff,sizeof(backup_t));
270 memset(env_crc,0x00,4);
271 i2c_write(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
272 i2c_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,2,(void *)env_crc,4);
276 * check crc of "older" environment
278 int check_env_old_size(ulong oldsize)
285 eeprom_read (CFG_DEF_EEPROM_ADDR,
287 (uchar *)&crc, sizeof(ulong));
294 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
296 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
297 new = crc32 (new, buf, n);
305 static ulong oldsizes[] = {
311 void copy_old_env(ulong size)
314 uchar value_buf[0x800];
325 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
334 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
342 if(strncmp(name,"baudrate",8)!=0) {
361 if(check_env_old_size(oldsizes[i]))
366 /* no old environment has been found */
367 get_backup_values (&back);
368 if (strncmp (back.signature, "MPL\0", 4) == 0) {
369 sprintf (buf, "%s", back.serial_name);
370 setenv ("serial#", buf);
371 sprintf (buf, "%s", back.eth_addr);
372 setenv ("ethaddr", buf);
373 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
378 copy_old_env(oldsizes[i]);
379 puts("INFO: old environment ajusted, use saveenv\n");
383 /* check if back up is set */
384 get_backup_values(&back);
385 if(strncmp(back.signature,"MPL\0",4)!=0) {
386 set_backup_values(0);
392 extern device_t *stdio_devices[];
393 extern char *stdio_names[];
395 void show_stdio_dev(void)
397 /* Print information */
399 if (stdio_devices[stdin] == NULL) {
400 puts("No input devices available!\n");
402 printf ("%s\n", stdio_devices[stdin]->name);
406 if (stdio_devices[stdout] == NULL) {
407 puts("No output devices available!\n");
409 printf ("%s\n", stdio_devices[stdout]->name);
413 if (stdio_devices[stderr] == NULL) {
414 puts("No error devices available!\n");
416 printf ("%s\n", stdio_devices[stderr]->name);
421 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
423 ulong size,src,ld_addr;
426 src = MULTI_PURPOSE_SOCKET_ADDR;
429 if (strcmp(argv[1], "flash") == 0)
431 #if (CONFIG_COMMANDS & CFG_CMD_FDC)
432 if (strcmp(argv[2], "floppy") == 0) {
434 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
435 puts("\nupdating bootloader image from floppy\n");
436 local_args[0] = argv[0];
438 local_args[1] = argv[3];
439 local_args[2] = NULL;
440 ld_addr=simple_strtoul(argv[3], NULL, 16);
441 result=do_fdcboot(cmdtp, 0, 2, local_args);
444 local_args[1] = NULL;
445 ld_addr=CFG_LOAD_ADDR;
446 result=do_fdcboot(cmdtp, 0, 1, local_args);
448 result=mpl_prg_image((uchar *)ld_addr);
451 #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
452 if (strcmp(argv[2], "mem") == 0) {
454 ld_addr=simple_strtoul(argv[3], NULL, 16);
459 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
460 result=mpl_prg_image((uchar *)ld_addr);
463 if (strcmp(argv[2], "mps") == 0) {
464 puts("\nupdating bootloader image from MPS\n");
465 result=mpl_prg((uchar *)src,size);
469 if (strcmp(argv[1], "mem") == 0)
474 result = (int)simple_strtol(argv[2], NULL, 16);
476 src=(unsigned long)&result;
477 src-=CFG_MEMTEST_START;
478 src-=(100*1024); /* - 100k */
483 printf("\n\nPass %ld\n",size);
484 mem_test(CFG_MEMTEST_START,src,1);
493 if (strcmp(argv[1], "clearenvvalues") == 0)
495 if (strcmp(argv[2], "yes") == 0)
501 if (strcmp(argv[1], "getback") == 0) {
502 get_backup_values(&back);
504 back.serial_name[16]=0;
506 printf("GetBackUp: signature: %s\n",back.signature);
507 printf(" serial#: %s\n",back.serial_name);
508 printf(" ethaddr: %s\n",back.eth_addr);
511 if (strcmp(argv[1], "setback") == 0) {
512 set_backup_values(1);
515 printf("Usage:\n%s\n", cmdtp->usage);
520 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
521 extern void doc_probe(ulong physadr);
524 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
530 /******************************************************
531 * Routines to display the Board information
532 * to the screen (since the VGA will be initialized as last,
533 * we must resend the infos)
536 #ifdef CONFIG_CONSOLE_EXTRA_INFO
537 extern GraphicDevice ctfb;
538 extern int get_boot_mode(void);
540 void video_get_info_str (int line_number, char *info)
542 /* init video info strings for graphic console */
543 DECLARE_GLOBAL_DATA_PTR;
544 PPC405_SYS_INFO sys_info;
551 unsigned char *s, *e, bc;
555 /* CPU and board infos */
557 get_sys_info (&sys_info);
559 case PVR_405GP_RB: rev='B'; break;
560 case PVR_405GP_RC: rev='C'; break;
561 case PVR_405GP_RD: rev='D'; break;
562 case PVR_405GP_RE: rev='E'; break;
563 case PVR_405GPR_RB: rev='B'; break;
564 default: rev='?'; break;
566 if(pvr==PVR_405GPR_RB)
567 sprintf(cpustr,"PPC405GPr %c",rev);
569 sprintf(cpustr,"PPC405GP %c",rev);
572 s=getenv ("serial#");
574 if (!s || strncmp (s, "PIP405", 6)) {
575 sprintf(buf,"### No HW ID - assuming PIP405");
579 if (!s || strncmp (s, "MIP405", 6)) {
580 sprintf(buf,"### No HW ID - assuming MIP405");
584 for (e = s; *e; ++e) {
595 sprintf(&buf[i]," SN ");
602 sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
604 strmhz (tmp, gd->cpu_clk), sys_info.freqPLB / 1000000,
605 sys_info.freqPLB / sys_info.pllOpbDiv / 1000000,
606 sys_info.freqPLB / sys_info.pllExtBusDiv / 1000000);
610 boot = get_boot_mode();
611 bc = in8 (CONFIG_PORT_ADDR);
612 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
613 gd->bd->bi_memsize / 0x100000,
614 gd->bd->bi_flashsize / 0x100000,
616 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
620 sprintf (buf, "%s",CONFIG_IDENT_STRING);
621 sprintf (info, " %s", &buf[1]);
624 /* no more info lines */
628 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
630 #endif /* CONFIG_VIDEO */