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>
37 #include "../pip405/pip405.h"
38 #include <asm/4xx_pci.h>
41 #include "../mip405/mip405.h"
42 #include <asm/4xx_pci.h>
45 DECLARE_GLOBAL_DATA_PTR;
47 #if defined(CONFIG_PATI)
48 #define FIRM_START 0xFFF00000
51 extern int mem_test(ulong start, ulong ramsize, int quiet);
53 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
54 #define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
56 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
57 /*-----------------------------------------------------------------------
58 * On PIP/MIP405 we have 3 (4) possible boot mode
60 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
61 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
62 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
63 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
64 * The flash init is the first board specific routine which is called
65 * after code relocation (running from SDRAM)
66 * The first thing we do is to map the Flash CS to the Flash area and
67 * the MPS CS to the MPS area. Since the flash size is unknown at this
68 * point, we use the max flash size and the lowest flash address as base.
70 * After flash detection we adjust the size of the CS area accordingly.
71 * update_flash_size() will fix in wrong values in the flash_info structure,
72 * misc_init_r() will fix the values in the board info structure
74 int get_boot_mode(void)
78 pbcr = mfdcr(CPC0_PSR);
79 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
80 /* boot via MPS or MPS mapping */
82 if (pbcr & PSR_ROM_LOC)
88 /* Map the flash high (in boot area)
89 This code can only be executed from SDRAM (after relocation).
91 void setup_cs_reloc(void)
95 * since we are relocated, we can set-up the CS finaly
96 * but first of all, switch off PCI mapping (in case it
101 mode = get_boot_mode();
103 * we map the flash high in every case
104 * first find out to which CS the flash is attached to
106 if (mode & BOOT_MPS) {
107 /* map flash high on CS1 and MPS on CS0 */
108 mtdcr(EBC0_CFGADDR, PB0AP);
109 mtdcr(EBC0_CFGDATA, MPS_AP);
110 mtdcr(EBC0_CFGADDR, PB0CR);
111 mtdcr(EBC0_CFGDATA, MPS_CR);
113 * we use the default values (max values) for the flash
114 * because its real size is not yet known
116 mtdcr(EBC0_CFGADDR, PB1AP);
117 mtdcr(EBC0_CFGDATA, FLASH_AP);
118 mtdcr(EBC0_CFGADDR, PB1CR);
119 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
121 /* map flash high on CS0 and MPS on CS1 */
122 mtdcr(EBC0_CFGADDR, PB1AP);
123 mtdcr(EBC0_CFGDATA, MPS_AP);
124 mtdcr(EBC0_CFGADDR, PB1CR);
125 mtdcr(EBC0_CFGDATA, MPS_CR);
127 * we use the default values (max values) for the flash
128 * because its real size is not yet known
130 mtdcr(EBC0_CFGADDR, PB0AP);
131 mtdcr(EBC0_CFGDATA, FLASH_AP);
132 mtdcr(EBC0_CFGADDR, PB0CR);
133 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
136 #endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) */
138 #ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
139 /* adjust flash start and protection info */
140 int update_flash_size(int flash_size)
143 flash_info_t *info = &flash_info[0];
144 unsigned long flashcr;
145 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
147 if (flash_size > 128*1024*1024) {
148 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
153 if ((flash_size >> 20) != 0)
154 i = __ilog2(flash_size >> 20);
156 /* set up flash CS according to the size */
157 mode = get_boot_mode();
158 if (mode & BOOT_MPS) {
159 /* flash is on CS1 */
160 mtdcr(EBC0_CFGADDR, PB1CR);
161 flashcr = mfdcr(EBC0_CFGDATA);
162 /* we map the flash high in every case */
163 flashcr &= 0x0001FFFF; /* mask out address bits */
164 flashcr |= flash_base; /* start addr */
165 flashcr |= (i << 17); /* size addr */
166 mtdcr(EBC0_CFGADDR, PB1CR);
167 mtdcr(EBC0_CFGDATA, flashcr);
169 /* flash is on CS0 */
170 mtdcr(EBC0_CFGADDR, PB0CR);
171 flashcr = mfdcr(EBC0_CFGDATA);
172 /* we map the flash high in every case */
173 flashcr &= 0x0001FFFF; /* mask out address bits */
174 flashcr |= flash_base; /* start addr */
175 flashcr |= (i << 17); /* size addr */
176 mtdcr(EBC0_CFGADDR, PB0CR);
177 mtdcr(EBC0_CFGDATA, flashcr);
180 for (i = 0; i < info->sector_count; i++)
181 /* adjust sector start address */
182 info->start[i] = flash_base +
183 (info->start[i] - CONFIG_SYS_FLASH_BASE);
185 /* unprotect all sectors */
186 flash_protect(FLAG_PROTECT_CLEAR,
190 flash_protect_default();
191 /* protect reset vector too*/
192 flash_protect(FLAG_PROTECT_SET,
193 info->start[info->sector_count-1],
202 mpl_prg(uchar *src, ulong size)
205 flash_info_t *info = &flash_info[0];
207 #if defined(CONFIG_PATI)
210 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
211 char *copystr = (char *)src;
212 ulong *magic = (ulong *)src;
215 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
216 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
217 puts("Bad Magic number\n");
220 /* some more checks before we delete the Flash... */
221 /* Checking the ISO_STRING prevents to program a
222 * wrong Firmware Image into the flash.
224 i = 4; /* skip Magic number */
226 if (strncmp(©str[i], "MEV-", 4) == 0)
229 puts("Firmware Image for unknown Target\n");
233 /* we have the ISO STRING, check */
234 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
235 printf("Wrong Firmware Image: %s\n", ©str[i]);
238 #if !defined(CONFIG_PATI)
241 /* unprotect sectors used by u-boot */
242 flash_protect(FLAG_PROTECT_CLEAR,
247 /* search start sector */
248 for (i = info->sector_count-1; i > 0; i--)
249 if (start >= info->start[i])
252 /* now erase flash */
253 printf("Erasing at %lx (sector %d) (start %lx)\n",
254 start,i,info->start[i]);
255 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
261 #else /* #if !defined(CONFIG_PATI */
265 /* search start sector */
266 for (i = info->sector_count-1; i > 0; i--)
267 if (start >= info->start[i])
272 for (i = info->sector_count-1; i > 0; i--)
273 if ((start + size) >= info->start[i])
276 /* unprotect sectors used by u-boot */
277 flash_protect(FLAG_PROTECT_CLEAR,
282 /* now erase flash */
283 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
284 start, start + size, start_sect, i,
285 info->start[start_sect], info->start[i]);
286 if ((rc = flash_erase (info, start_sect, i)) != 0) {
291 #endif /* defined(CONFIG_PATI) */
293 #elif defined(CONFIG_VCMA9)
296 /* search end sector */
297 for (i = 0; i < info->sector_count; i++)
298 if (size < info->start[i])
301 flash_protect(FLAG_PROTECT_CLEAR,
306 /* now erase flash */
307 printf("Erasing at %lx (sector %d) (start %lx)\n",
308 start,0,info->start[0]);
309 if ((rc = flash_erase (info, 0, i)) != 0) {
316 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
318 if ((rc = flash_write ((char *)src, start, size)) != 0) {
323 puts("OK programming done\n");
329 mpl_prg_image(uchar *ld_addr)
333 image_header_t *hdr = (image_header_t *)ld_addr;
336 #if defined(CONFIG_FIT)
337 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
338 puts ("Non legacy image format not supported\n");
343 if (!image_check_magic (hdr)) {
344 puts("Bad Magic Number\n");
347 image_print_contents (hdr);
348 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
349 puts("No U-Boot Image\n");
352 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
353 puts("No Firmware Image\n");
356 if (!image_check_hcrc (hdr)) {
357 puts("Bad Header Checksum\n");
360 puts("Verifying Checksum ... ");
361 if (!image_check_dcrc (hdr)) {
362 puts("Bad Data CRC\n");
367 data = (uchar *)image_get_data (hdr);
368 len = image_get_data_size (hdr);
370 if (image_get_comp (hdr) != IH_COMP_NONE) {
372 /* reserve space for uncompressed image */
373 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
374 puts("Insufficient space for decompression\n");
378 switch (image_get_comp (hdr)) {
380 puts("Uncompressing (GZIP) ... ");
381 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
383 puts("GUNZIP ERROR\n");
391 puts("Uncompressing (BZIP2) ... ");
393 uint retlen = IMAGE_SIZE;
394 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
395 (char *)data, len, 0, 0);
399 printf ("BUNZIP2 ERROR: %d\n", rc);
407 printf ("Unimplemented compression type %d\n",
408 image_get_comp (hdr));
413 rc = mpl_prg(buf, len);
416 rc = mpl_prg(data, len);
422 #if !defined(CONFIG_PATI)
423 void get_backup_values(backup_t *buf)
425 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
428 void set_backup_values(int overwrite)
433 get_backup_values(&back);
435 if(strncmp(back.signature,"MPL\0",4)==0) {
436 puts("Not possible to write Backup\n");
440 memcpy(back.signature,"MPL\0",4);
441 i = getenv_f("serial#",back.serial_name,16);
443 puts("Not possible to write Backup\n");
446 back.serial_name[16]=0;
447 i = getenv_f("ethaddr",back.eth_addr,20);
449 puts("Not possible to write Backup\n");
453 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
456 void clear_env_values(void)
459 unsigned char env_crc[4];
461 memset(&back,0xff,sizeof(backup_t));
462 memset(env_crc,0x00,4);
463 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
464 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
468 * check crc of "older" environment
470 int check_env_old_size(ulong oldsize)
477 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
479 (uchar *)&crc, sizeof(ulong));
486 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
488 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
489 new = crc32 (new, buf, n);
497 static ulong oldsizes[] = {
503 void copy_old_env(ulong size)
506 uchar value_buf[0x800];
513 value = &value_buf[0];
517 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
526 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
533 value = &value_buf[0];
534 if(strncmp((char *)name,"baudrate",8)!=0) {
535 setenv((char *)name,(char *)value);
553 if(check_env_old_size(oldsizes[i]))
558 /* no old environment has been found */
559 get_backup_values (&back);
560 if (strncmp (back.signature, "MPL\0", 4) == 0) {
561 sprintf (buf, "%s", back.serial_name);
562 setenv ("serial#", buf);
563 sprintf (buf, "%s", back.eth_addr);
564 setenv ("ethaddr", buf);
565 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
570 copy_old_env(oldsizes[i]);
571 puts("INFO: old environment ajusted, use saveenv\n");
575 /* check if back up is set */
576 get_backup_values(&back);
577 if(strncmp(back.signature,"MPL\0",4)!=0) {
578 set_backup_values(0);
583 #endif /* #if !defined(CONFIG_PATI) */
585 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
589 #if !defined(CONFIG_PATI)
590 ulong size = IMAGE_SIZE;
591 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
595 if (strcmp(argv[1], "flash") == 0)
597 #if defined(CONFIG_CMD_FDC)
598 if (strcmp(argv[2], "floppy") == 0) {
600 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
601 puts("\nupdating bootloader image from floppy\n");
602 local_args[0] = argv[0];
604 local_args[1] = argv[3];
605 local_args[2] = NULL;
606 ld_addr=simple_strtoul(argv[3], NULL, 16);
607 result=do_fdcboot(cmdtp, 0, 2, local_args);
610 local_args[1] = NULL;
611 ld_addr=CONFIG_SYS_LOAD_ADDR;
612 result=do_fdcboot(cmdtp, 0, 1, local_args);
614 result=mpl_prg_image((uchar *)ld_addr);
618 if (strcmp(argv[2], "mem") == 0) {
620 ld_addr=simple_strtoul(argv[3], NULL, 16);
625 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
626 result=mpl_prg_image((uchar *)ld_addr);
629 #if !defined(CONFIG_PATI)
630 if (strcmp(argv[2], "mps") == 0) {
631 puts("\nupdating bootloader image from MPS\n");
632 result=mpl_prg((uchar *)src,size);
635 #endif /* #if !defined(CONFIG_PATI) */
637 #if !defined(CONFIG_PATI)
638 if (strcmp(argv[1], "clearenvvalues") == 0)
640 if (strcmp(argv[2], "yes") == 0)
646 if (strcmp(argv[1], "getback") == 0) {
647 get_backup_values(&back);
649 back.serial_name[16]=0;
651 printf("GetBackUp: signature: %s\n",back.signature);
652 printf(" serial#: %s\n",back.serial_name);
653 printf(" ethaddr: %s\n",back.eth_addr);
656 if (strcmp(argv[1], "setback") == 0) {
657 set_backup_values(1);
661 return cmd_usage(cmdtp);
665 #if defined(CONFIG_CMD_DOC)
668 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
674 /******************************************************
675 * Routines to display the Board information
676 * to the screen (since the VGA will be initialized as last,
677 * we must resend the infos)
680 #ifdef CONFIG_CONSOLE_EXTRA_INFO
681 extern GraphicDevice ctfb;
682 extern int get_boot_mode(void);
684 void video_get_info_str (int line_number, char *info)
686 /* init video info strings for graphic console */
687 PPC4xx_SYS_INFO sys_info;
692 char buf1[32], buf2[32], buf3[32], buf4[32];
698 /* CPU and board infos */
700 get_sys_info (&sys_info);
702 case PVR_405GP_RB: rev='B'; break;
703 case PVR_405GP_RC: rev='C'; break;
704 case PVR_405GP_RD: rev='D'; break;
705 case PVR_405GP_RE: rev='E'; break;
706 case PVR_405GPR_RB: rev='B'; break;
707 default: rev='?'; break;
709 if(pvr==PVR_405GPR_RB)
710 sprintf(cpustr,"PPC405GPr %c",rev);
712 sprintf(cpustr,"PPC405GP %c",rev);
715 s=getenv ("serial#");
717 if (!s || strncmp (s, "PIP405", 6)) {
718 sprintf(buf,"### No HW ID - assuming PIP405");
722 if (!s || strncmp (s, "MIP405", 6)) {
723 sprintf(buf,"### No HW ID - assuming MIP405");
727 for (e = s; *e; ++e) {
738 sprintf(&buf[i]," SN ");
745 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
747 strmhz (buf1, gd->cpu_clk),
748 strmhz (buf2, sys_info.freqPLB),
749 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
750 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
754 boot = get_boot_mode();
755 bc = in8 (CONFIG_PORT_ADDR);
756 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
757 gd->bd->bi_memsize / 0x100000,
758 gd->bd->bi_flashsize / 0x100000,
760 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
764 sprintf (buf, "%s",CONFIG_IDENT_STRING);
765 sprintf (info, " %s", &buf[1]);
768 /* no more info lines */
772 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
774 #endif /* CONFIG_VIDEO */