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>
45 #if defined(CONFIG_PATI)
46 #define FIRM_START 0xFFF00000
49 extern int gunzip(void *, int, uchar
*, unsigned long *);
50 extern int mem_test(ulong start
, ulong ramsize
, int quiet
);
52 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
53 #define IMAGE_SIZE CFG_MONITOR_LEN /* ugly, but it works for now */
55 extern flash_info_t flash_info
[]; /* info for FLASH chips */
57 static image_header_t header
;
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 (ntohl(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
)
179 unsigned long len
, checksum
;
181 image_header_t
*hdr
= &header
;
184 /* Copy header so we can blank CRC field for re-calculation */
185 memcpy (&header
, (char *)ld_addr
, sizeof(image_header_t
));
186 if (ntohl(hdr
->ih_magic
) != IH_MAGIC
) {
187 puts("Bad Magic Number\n");
190 print_image_hdr(hdr
);
191 if (hdr
->ih_os
!= IH_OS_U_BOOT
) {
192 puts("No U-Boot Image\n");
195 if (hdr
->ih_type
!= IH_TYPE_FIRMWARE
) {
196 puts("No Firmware Image\n");
199 data
= (uchar
*)&header
;
200 len
= sizeof(image_header_t
);
201 checksum
= ntohl(hdr
->ih_hcrc
);
203 if (crc32 (0, (uchar
*)data
, len
) != checksum
) {
204 puts("Bad Header Checksum\n");
207 data
= ld_addr
+ sizeof(image_header_t
);
208 len
= ntohl(hdr
->ih_size
);
209 puts("Verifying Checksum ... ");
210 if (crc32 (0, (uchar
*)data
, len
) != ntohl(hdr
->ih_dcrc
)) {
211 puts("Bad Data CRC\n");
216 if (hdr
->ih_comp
!= IH_COMP_NONE
) {
218 /* reserve space for uncompressed image */
219 if ((buf
= malloc(IMAGE_SIZE
)) == NULL
) {
220 puts("Insufficient space for decompression\n");
224 switch (hdr
->ih_comp
) {
226 puts("Uncompressing (GZIP) ... ");
227 rc
= gunzip ((void *)(buf
), IMAGE_SIZE
, data
, &len
);
229 puts("GUNZIP ERROR\n");
237 puts("Uncompressing (BZIP2) ... ");
239 uint retlen
= IMAGE_SIZE
;
240 rc
= BZ2_bzBuffToBuffDecompress ((char *)(buf
), &retlen
,
241 (char *)data
, len
, 0, 0);
245 printf ("BUNZIP2 ERROR: %d\n", rc
);
253 printf ("Unimplemented compression type %d\n", hdr
->ih_comp
);
258 rc
= mpl_prg(buf
, len
);
261 rc
= mpl_prg(data
, len
);
267 #if !defined(CONFIG_PATI)
268 void get_backup_values(backup_t
*buf
)
270 i2c_read(CFG_DEF_EEPROM_ADDR
, I2C_BACKUP_ADDR
,2,(void *)buf
,sizeof(backup_t
));
273 void set_backup_values(int overwrite
)
278 get_backup_values(&back
);
280 if(strncmp(back
.signature
,"MPL\0",4)==0) {
281 puts("Not possible to write Backup\n");
285 memcpy(back
.signature
,"MPL\0",4);
286 i
= getenv_r("serial#",back
.serial_name
,16);
288 puts("Not possible to write Backup\n");
291 back
.serial_name
[16]=0;
292 i
= getenv_r("ethaddr",back
.eth_addr
,20);
294 puts("Not possible to write Backup\n");
298 i2c_write(CFG_DEF_EEPROM_ADDR
, I2C_BACKUP_ADDR
,2,(void *)&back
,sizeof(backup_t
));
301 void clear_env_values(void)
304 unsigned char env_crc
[4];
306 memset(&back
,0xff,sizeof(backup_t
));
307 memset(env_crc
,0x00,4);
308 i2c_write(CFG_DEF_EEPROM_ADDR
,I2C_BACKUP_ADDR
,2,(void *)&back
,sizeof(backup_t
));
309 i2c_write(CFG_DEF_EEPROM_ADDR
,CFG_ENV_OFFSET
,2,(void *)env_crc
,4);
313 * check crc of "older" environment
315 int check_env_old_size(ulong oldsize
)
322 eeprom_read (CFG_DEF_EEPROM_ADDR
,
324 (uchar
*)&crc
, sizeof(ulong
));
331 int n
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
333 eeprom_read (CFG_DEF_EEPROM_ADDR
, CFG_ENV_OFFSET
+off
, buf
, n
);
334 new = crc32 (new, buf
, n
);
342 static ulong oldsizes
[] = {
348 void copy_old_env(ulong size
)
351 uchar value_buf
[0x800];
362 eeprom_read (CFG_DEF_EEPROM_ADDR
, CFG_ENV_OFFSET
+off
, &c
, 1);
371 eeprom_read (CFG_DEF_EEPROM_ADDR
, CFG_ENV_OFFSET
+off
, &c
, 1);
379 if(strncmp((char *)name
,"baudrate",8)!=0) {
380 setenv((char *)name
,(char *)value
);
398 if(check_env_old_size(oldsizes
[i
]))
403 /* no old environment has been found */
404 get_backup_values (&back
);
405 if (strncmp (back
.signature
, "MPL\0", 4) == 0) {
406 sprintf (buf
, "%s", back
.serial_name
);
407 setenv ("serial#", buf
);
408 sprintf (buf
, "%s", back
.eth_addr
);
409 setenv ("ethaddr", buf
);
410 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
415 copy_old_env(oldsizes
[i
]);
416 puts("INFO: old environment ajusted, use saveenv\n");
420 /* check if back up is set */
421 get_backup_values(&back
);
422 if(strncmp(back
.signature
,"MPL\0",4)!=0) {
423 set_backup_values(0);
429 extern device_t
*stdio_devices
[];
430 extern char *stdio_names
[];
432 void show_stdio_dev(void)
434 /* Print information */
436 if (stdio_devices
[stdin
] == NULL
) {
437 puts("No input devices available!\n");
439 printf ("%s\n", stdio_devices
[stdin
]->name
);
443 if (stdio_devices
[stdout
] == NULL
) {
444 puts("No output devices available!\n");
446 printf ("%s\n", stdio_devices
[stdout
]->name
);
450 if (stdio_devices
[stderr
] == NULL
) {
451 puts("No error devices available!\n");
453 printf ("%s\n", stdio_devices
[stderr
]->name
);
457 #endif /* #if !defined(CONFIG_PATI) */
459 int do_mplcommon(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
461 ulong size
,src
,ld_addr
;
463 #if !defined(CONFIG_PATI)
465 src
= MULTI_PURPOSE_SOCKET_ADDR
;
469 if (strcmp(argv
[1], "flash") == 0)
471 #if (CONFIG_COMMANDS & CFG_CMD_FDC)
472 if (strcmp(argv
[2], "floppy") == 0) {
474 extern int do_fdcboot (cmd_tbl_t
*, int, int, char *[]);
475 puts("\nupdating bootloader image from floppy\n");
476 local_args
[0] = argv
[0];
478 local_args
[1] = argv
[3];
479 local_args
[2] = NULL
;
480 ld_addr
=simple_strtoul(argv
[3], NULL
, 16);
481 result
=do_fdcboot(cmdtp
, 0, 2, local_args
);
484 local_args
[1] = NULL
;
485 ld_addr
=CFG_LOAD_ADDR
;
486 result
=do_fdcboot(cmdtp
, 0, 1, local_args
);
488 result
=mpl_prg_image((uchar
*)ld_addr
);
491 #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
492 if (strcmp(argv
[2], "mem") == 0) {
494 ld_addr
=simple_strtoul(argv
[3], NULL
, 16);
499 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr
);
500 result
=mpl_prg_image((uchar
*)ld_addr
);
503 #if !defined(CONFIG_PATI)
504 if (strcmp(argv
[2], "mps") == 0) {
505 puts("\nupdating bootloader image from MPS\n");
506 result
=mpl_prg((uchar
*)src
,size
);
509 #endif /* #if !defined(CONFIG_PATI) */
511 if (strcmp(argv
[1], "mem") == 0)
516 result
= (int)simple_strtol(argv
[2], NULL
, 16);
518 src
=(unsigned long)&result
;
519 src
-=CFG_MEMTEST_START
;
520 src
-=(100*1024); /* - 100k */
525 printf("\n\nPass %ld\n",size
);
526 mem_test(CFG_MEMTEST_START
,src
,1);
535 #if !defined(CONFIG_PATI)
536 if (strcmp(argv
[1], "clearenvvalues") == 0)
538 if (strcmp(argv
[2], "yes") == 0)
544 if (strcmp(argv
[1], "getback") == 0) {
545 get_backup_values(&back
);
547 back
.serial_name
[16]=0;
549 printf("GetBackUp: signature: %s\n",back
.signature
);
550 printf(" serial#: %s\n",back
.serial_name
);
551 printf(" ethaddr: %s\n",back
.eth_addr
);
554 if (strcmp(argv
[1], "setback") == 0) {
555 set_backup_values(1);
559 printf("Usage:\n%s\n", cmdtp
->usage
);
564 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
565 extern void doc_probe(ulong physadr
);
568 doc_probe(MULTI_PURPOSE_SOCKET_ADDR
);
574 /******************************************************
575 * Routines to display the Board information
576 * to the screen (since the VGA will be initialized as last,
577 * we must resend the infos)
580 #ifdef CONFIG_CONSOLE_EXTRA_INFO
581 extern GraphicDevice ctfb
;
582 extern int get_boot_mode(void);
584 void video_get_info_str (int line_number
, char *info
)
586 /* init video info strings for graphic console */
587 DECLARE_GLOBAL_DATA_PTR
;
588 PPC405_SYS_INFO sys_info
;
599 /* CPU and board infos */
601 get_sys_info (&sys_info
);
603 case PVR_405GP_RB
: rev
='B'; break;
604 case PVR_405GP_RC
: rev
='C'; break;
605 case PVR_405GP_RD
: rev
='D'; break;
606 case PVR_405GP_RE
: rev
='E'; break;
607 case PVR_405GPR_RB
: rev
='B'; break;
608 default: rev
='?'; break;
610 if(pvr
==PVR_405GPR_RB
)
611 sprintf(cpustr
,"PPC405GPr %c",rev
);
613 sprintf(cpustr
,"PPC405GP %c",rev
);
616 s
=getenv ("serial#");
618 if (!s
|| strncmp (s
, "PIP405", 6)) {
619 sprintf(buf
,"### No HW ID - assuming PIP405");
623 if (!s
|| strncmp (s
, "MIP405", 6)) {
624 sprintf(buf
,"### No HW ID - assuming MIP405");
628 for (e
= s
; *e
; ++e
) {
639 sprintf(&buf
[i
]," SN ");
646 sprintf (info
," %s %s %s MHz (%lu/%lu/%lu MHz)",
648 strmhz (tmp
, gd
->cpu_clk
), sys_info
.freqPLB
/ 1000000,
649 sys_info
.freqPLB
/ sys_info
.pllOpbDiv
/ 1000000,
650 sys_info
.freqPLB
/ sys_info
.pllExtBusDiv
/ 1000000);
654 boot
= get_boot_mode();
655 bc
= in8 (CONFIG_PORT_ADDR
);
656 sprintf(info
, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
657 gd
->bd
->bi_memsize
/ 0x100000,
658 gd
->bd
->bi_flashsize
/ 0x100000,
660 (boot
& BOOT_MPS
) ? "MPS boot" : "Flash boot",
664 sprintf (buf
, "%s",CONFIG_IDENT_STRING
);
665 sprintf (info
, " %s", &buf
[1]);
668 /* no more info lines */
672 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
674 #endif /* CONFIG_VIDEO */