1
2
3
4
5
6
7
8#include <common.h>
9#include <command.h>
10#include <video_fb.h>
11#include "common_util.h"
12#include <asm/processor.h>
13#include <asm/byteorder.h>
14#include <i2c.h>
15#include <pci.h>
16#include <malloc.h>
17#include <bzlib.h>
18
19#ifdef CONFIG_PIP405
20#include "../pip405/pip405.h"
21#include <asm/4xx_pci.h>
22#endif
23#ifdef CONFIG_MIP405
24#include "../mip405/mip405.h"
25#include <asm/4xx_pci.h>
26#endif
27
28DECLARE_GLOBAL_DATA_PTR;
29
30#if defined(CONFIG_PATI)
31#define FIRM_START 0xFFF00000
32#endif
33
34extern int mem_test(ulong start, ulong ramsize, int quiet);
35
36#define I2C_BACKUP_ADDR 0x7C00
37#define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN
38
39#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57int get_boot_mode(void)
58{
59 unsigned long pbcr;
60 int res = 0;
61 pbcr = mfdcr(CPC0_PSR);
62 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
63
64 res = BOOT_MPS;
65 if (pbcr & PSR_ROM_LOC)
66
67 res |= BOOT_PCI;
68 return res;
69}
70
71
72
73
74void setup_cs_reloc(void)
75{
76 int mode;
77
78
79
80
81
82 out32r(PMM0MA, 0L);
83
84 mode = get_boot_mode();
85
86
87
88
89 if (mode & BOOT_MPS) {
90
91 mtdcr(EBC0_CFGADDR, PB0AP);
92 mtdcr(EBC0_CFGDATA, MPS_AP);
93 mtdcr(EBC0_CFGADDR, PB0CR);
94 mtdcr(EBC0_CFGDATA, MPS_CR);
95
96
97
98
99 mtdcr(EBC0_CFGADDR, PB1AP);
100 mtdcr(EBC0_CFGDATA, FLASH_AP);
101 mtdcr(EBC0_CFGADDR, PB1CR);
102 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
103 } else {
104
105 mtdcr(EBC0_CFGADDR, PB1AP);
106 mtdcr(EBC0_CFGDATA, MPS_AP);
107 mtdcr(EBC0_CFGADDR, PB1CR);
108 mtdcr(EBC0_CFGDATA, MPS_CR);
109
110
111
112
113 mtdcr(EBC0_CFGADDR, PB0AP);
114 mtdcr(EBC0_CFGDATA, FLASH_AP);
115 mtdcr(EBC0_CFGADDR, PB0CR);
116 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
117 }
118}
119#endif
120
121#ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
122
123int update_flash_size(int flash_size)
124{
125 int i = 0, mode;
126 flash_info_t *info = &flash_info[0];
127 unsigned long flashcr;
128 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
129
130 if (flash_size > 128*1024*1024) {
131 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
132 flash_size);
133 hang();
134 }
135
136 if ((flash_size >> 20) != 0)
137 i = __ilog2(flash_size >> 20);
138
139
140 mode = get_boot_mode();
141 if (mode & BOOT_MPS) {
142
143 mtdcr(EBC0_CFGADDR, PB1CR);
144 flashcr = mfdcr(EBC0_CFGDATA);
145
146 flashcr &= 0x0001FFFF;
147 flashcr |= flash_base;
148 flashcr |= (i << 17);
149 mtdcr(EBC0_CFGADDR, PB1CR);
150 mtdcr(EBC0_CFGDATA, flashcr);
151 } else {
152
153 mtdcr(EBC0_CFGADDR, PB0CR);
154 flashcr = mfdcr(EBC0_CFGDATA);
155
156 flashcr &= 0x0001FFFF;
157 flashcr |= flash_base;
158 flashcr |= (i << 17);
159 mtdcr(EBC0_CFGADDR, PB0CR);
160 mtdcr(EBC0_CFGDATA, flashcr);
161 }
162
163 for (i = 0; i < info->sector_count; i++)
164
165 info->start[i] = flash_base +
166 (info->start[i] - CONFIG_SYS_FLASH_BASE);
167
168
169 flash_protect(FLAG_PROTECT_CLEAR,
170 info->start[0],
171 0xFFFFFFFF,
172 info);
173 flash_protect_default();
174
175 flash_protect(FLAG_PROTECT_SET,
176 info->start[info->sector_count-1],
177 0xFFFFFFFF,
178 info);
179
180 return 0;
181}
182#endif
183
184static int
185mpl_prg(uchar *src, ulong size)
186{
187 ulong start;
188 flash_info_t *info = &flash_info[0];
189 int i, rc;
190#if defined(CONFIG_PATI)
191 int start_sect;
192#endif
193#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
194 char *copystr = (char *)src;
195 ulong *magic = (ulong *)src;
196#endif
197
198#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
199 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
200 puts("Bad Magic number\n");
201 return -1;
202 }
203
204
205
206
207 i = 4;
208 while (1) {
209 if (strncmp(©str[i], "MEV-", 4) == 0)
210 break;
211 if (i++ >= 0x100) {
212 puts("Firmware Image for unknown Target\n");
213 return -1;
214 }
215 }
216
217 if (strncmp(©str[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
218 printf("Wrong Firmware Image: %s\n", ©str[i]);
219 return -1;
220 }
221#if !defined(CONFIG_PATI)
222 start = 0 - size;
223
224
225 flash_protect(FLAG_PROTECT_CLEAR,
226 start,
227 0xFFFFFFFF,
228 info);
229
230
231 for (i = info->sector_count-1; i > 0; i--)
232 if (start >= info->start[i])
233 break;
234
235
236 printf("Erasing at %lx (sector %d) (start %lx)\n",
237 start,i,info->start[i]);
238 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
239 puts("ERROR ");
240 flash_perror(rc);
241 return (1);
242 }
243
244#else
245 start = FIRM_START;
246 start_sect = -1;
247
248
249 for (i = info->sector_count-1; i > 0; i--)
250 if (start >= info->start[i])
251 break;
252
253 start_sect = i;
254
255 for (i = info->sector_count-1; i > 0; i--)
256 if ((start + size) >= info->start[i])
257 break;
258
259
260 flash_protect(FLAG_PROTECT_CLEAR,
261 start,
262 start + size,
263 info);
264
265
266 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
267 start, start + size, start_sect, i,
268 info->start[start_sect], info->start[i]);
269 if ((rc = flash_erase (info, start_sect, i)) != 0) {
270 puts ("ERROR ");
271 flash_perror (rc);
272 return (1);
273 }
274#endif
275
276#elif defined(CONFIG_VCMA9)
277 start = 0;
278
279
280 for (i = 0; i < info->sector_count; i++)
281 if (size < info->start[i])
282 break;
283
284 flash_protect(FLAG_PROTECT_CLEAR,
285 start,
286 size,
287 info);
288
289
290 printf("Erasing at %lx (sector %d) (start %lx)\n",
291 start,0,info->start[0]);
292 if ((rc = flash_erase (info, 0, i)) != 0) {
293 puts("ERROR ");
294 flash_perror(rc);
295 return (1);
296 }
297
298#endif
299 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
300 (ulong)src, size);
301 if ((rc = flash_write ((char *)src, start, size)) != 0) {
302 puts("ERROR ");
303 flash_perror(rc);
304 return (1);
305 }
306 puts("OK programming done\n");
307 return 0;
308}
309
310
311static int
312mpl_prg_image(uchar *ld_addr)
313{
314 unsigned long len;
315 uchar *data;
316 image_header_t *hdr = (image_header_t *)ld_addr;
317 int rc;
318
319#if defined(CONFIG_FIT)
320 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
321 puts ("Non legacy image format not supported\n");
322 return -1;
323 }
324#endif
325
326 if (!image_check_magic (hdr)) {
327 puts("Bad Magic Number\n");
328 return 1;
329 }
330 image_print_contents (hdr);
331 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
332 puts("No U-Boot Image\n");
333 return 1;
334 }
335 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
336 puts("No Firmware Image\n");
337 return 1;
338 }
339 if (!image_check_hcrc (hdr)) {
340 puts("Bad Header Checksum\n");
341 return 1;
342 }
343 puts("Verifying Checksum ... ");
344 if (!image_check_dcrc (hdr)) {
345 puts("Bad Data CRC\n");
346 return 1;
347 }
348 puts("OK\n");
349
350 data = (uchar *)image_get_data (hdr);
351 len = image_get_data_size (hdr);
352
353 if (image_get_comp (hdr) != IH_COMP_NONE) {
354 uchar *buf;
355
356 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
357 puts("Insufficient space for decompression\n");
358 return 1;
359 }
360
361 switch (image_get_comp (hdr)) {
362 case IH_COMP_GZIP:
363 puts("Uncompressing (GZIP) ... ");
364 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
365 if (rc != 0) {
366 puts("GUNZIP ERROR\n");
367 free(buf);
368 return 1;
369 }
370 puts("OK\n");
371 break;
372#ifdef CONFIG_BZIP2
373 case IH_COMP_BZIP2:
374 puts("Uncompressing (BZIP2) ... ");
375 {
376 uint retlen = IMAGE_SIZE;
377 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
378 (char *)data, len, 0, 0);
379 len = retlen;
380 }
381 if (rc != BZ_OK) {
382 printf ("BUNZIP2 ERROR: %d\n", rc);
383 free(buf);
384 return 1;
385 }
386 puts("OK\n");
387 break;
388#endif
389 default:
390 printf ("Unimplemented compression type %d\n",
391 image_get_comp (hdr));
392 free(buf);
393 return 1;
394 }
395
396 rc = mpl_prg(buf, len);
397 free(buf);
398 } else {
399 rc = mpl_prg(data, len);
400 }
401
402 return(rc);
403}
404
405#if !defined(CONFIG_PATI)
406void get_backup_values(backup_t *buf)
407{
408 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
409}
410
411void set_backup_values(int overwrite)
412{
413 backup_t back;
414 int i;
415
416 get_backup_values(&back);
417 if(!overwrite) {
418 if(strncmp(back.signature,"MPL\0",4)==0) {
419 puts("Not possible to write Backup\n");
420 return;
421 }
422 }
423 memcpy(back.signature,"MPL\0",4);
424 i = getenv_f("serial#",back.serial_name,16);
425 if(i < 0) {
426 puts("Not possible to write Backup\n");
427 return;
428 }
429 back.serial_name[16]=0;
430 i = getenv_f("ethaddr",back.eth_addr,20);
431 if(i < 0) {
432 puts("Not possible to write Backup\n");
433 return;
434 }
435 back.eth_addr[20]=0;
436 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
437}
438
439void clear_env_values(void)
440{
441 backup_t back;
442 unsigned char env_crc[4];
443
444 memset(&back,0xff,sizeof(backup_t));
445 memset(env_crc,0x00,4);
446 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
447 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
448}
449
450
451
452
453int check_env_old_size(ulong oldsize)
454{
455 ulong crc, len, new;
456 unsigned off;
457 uchar buf[64];
458
459
460 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
461 CONFIG_ENV_OFFSET,
462 (uchar *)&crc, sizeof(ulong));
463
464 new = 0;
465 len = oldsize;
466 off = sizeof(long);
467 len = oldsize-off;
468 while (len > 0) {
469 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
470
471 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
472 new = crc32 (new, buf, n);
473 len -= n;
474 off += n;
475 }
476
477 return (crc == new);
478}
479
480static ulong oldsizes[] = {
481 0x200,
482 0x800,
483 0
484};
485
486void copy_old_env(ulong size)
487{
488 uchar name_buf[64];
489 uchar value_buf[0x800];
490 uchar c;
491 ulong len;
492 unsigned off;
493 uchar *name, *value;
494
495 name = &name_buf[0];
496 value = &value_buf[0];
497 len=size;
498 off = sizeof(long);
499 while (len > off) {
500 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
501 if(c != '=') {
502 *name++=c;
503 off++;
504 }
505 else {
506 *name++='\0';
507 off++;
508 do {
509 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
510 *value++=c;
511 off++;
512 if(c == '\0')
513 break;
514 } while(len > off);
515 name = &name_buf[0];
516 value = &value_buf[0];
517 if(strncmp((char *)name,"baudrate",8)!=0) {
518 setenv((char *)name,(char *)value);
519 }
520
521 }
522 }
523}
524
525
526void check_env(void)
527{
528 char *s;
529 int i=0;
530 char buf[32];
531 backup_t back;
532
533 s=getenv("serial#");
534 if(!s) {
535 while(oldsizes[i]) {
536 if(check_env_old_size(oldsizes[i]))
537 break;
538 i++;
539 }
540 if(!oldsizes[i]) {
541
542 get_backup_values (&back);
543 if (strncmp (back.signature, "MPL\0", 4) == 0) {
544 sprintf (buf, "%s", back.serial_name);
545 setenv ("serial#", buf);
546 sprintf (buf, "%s", back.eth_addr);
547 setenv ("ethaddr", buf);
548 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
549 return;
550 }
551 }
552 else {
553 copy_old_env(oldsizes[i]);
554 puts("INFO: old environment ajusted, use saveenv\n");
555 }
556 }
557 else {
558
559 get_backup_values(&back);
560 if(strncmp(back.signature,"MPL\0",4)!=0) {
561 set_backup_values(0);
562 }
563 }
564}
565
566#endif
567
568int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
569{
570 ulong ld_addr;
571 int result;
572#if !defined(CONFIG_PATI)
573 ulong size = IMAGE_SIZE;
574 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
575 backup_t back;
576#endif
577
578 if (strcmp(argv[1], "flash") == 0)
579 {
580#if defined(CONFIG_CMD_FDC)
581 if (strcmp(argv[2], "floppy") == 0) {
582 char *local_args[3];
583 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
584 puts("\nupdating bootloader image from floppy\n");
585 local_args[0] = argv[0];
586 if(argc==4) {
587 local_args[1] = argv[3];
588 local_args[2] = NULL;
589 ld_addr=simple_strtoul(argv[3], NULL, 16);
590 result=do_fdcboot(cmdtp, 0, 2, local_args);
591 }
592 else {
593 local_args[1] = NULL;
594 ld_addr=CONFIG_SYS_LOAD_ADDR;
595 result=do_fdcboot(cmdtp, 0, 1, local_args);
596 }
597 result=mpl_prg_image((uchar *)ld_addr);
598 return result;
599 }
600#endif
601 if (strcmp(argv[2], "mem") == 0) {
602 if(argc==4) {
603 ld_addr=simple_strtoul(argv[3], NULL, 16);
604 }
605 else {
606 ld_addr=load_addr;
607 }
608 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
609 result=mpl_prg_image((uchar *)ld_addr);
610 return result;
611 }
612#if !defined(CONFIG_PATI)
613 if (strcmp(argv[2], "mps") == 0) {
614 puts("\nupdating bootloader image from MPS\n");
615 result=mpl_prg((uchar *)src,size);
616 return result;
617 }
618#endif
619 }
620#if !defined(CONFIG_PATI)
621 if (strcmp(argv[1], "clearenvvalues") == 0)
622 {
623 if (strcmp(argv[2], "yes") == 0)
624 {
625 clear_env_values();
626 return 0;
627 }
628 }
629 if (strcmp(argv[1], "getback") == 0) {
630 get_backup_values(&back);
631 back.signature[3]=0;
632 back.serial_name[16]=0;
633 back.eth_addr[20]=0;
634 printf("GetBackUp: signature: %s\n",back.signature);
635 printf(" serial#: %s\n",back.serial_name);
636 printf(" ethaddr: %s\n",back.eth_addr);
637 return 0;
638 }
639 if (strcmp(argv[1], "setback") == 0) {
640 set_backup_values(1);
641 return 0;
642 }
643#endif
644 return cmd_usage(cmdtp);
645}
646
647
648#if defined(CONFIG_CMD_DOC)
649void doc_init (void)
650{
651 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
652}
653#endif
654
655
656#ifdef CONFIG_VIDEO
657
658
659
660
661
662
663#ifdef CONFIG_CONSOLE_EXTRA_INFO
664extern GraphicDevice ctfb;
665extern int get_boot_mode(void);
666
667void video_get_info_str (int line_number, char *info)
668{
669
670 PPC4xx_SYS_INFO sys_info;
671 char rev;
672 int i,boot;
673 unsigned long pvr;
674 char buf[64];
675 char buf1[32], buf2[32], buf3[32], buf4[32];
676 char cpustr[16];
677 char *s, *e, bc;
678 switch (line_number)
679 {
680 case 2:
681
682 pvr=get_pvr();
683 get_sys_info (&sys_info);
684 switch (pvr) {
685 case PVR_405GP_RB: rev='B'; break;
686 case PVR_405GP_RC: rev='C'; break;
687 case PVR_405GP_RD: rev='D'; break;
688 case PVR_405GP_RE: rev='E'; break;
689 case PVR_405GPR_RB: rev='B'; break;
690 default: rev='?'; break;
691 }
692 if(pvr==PVR_405GPR_RB)
693 sprintf(cpustr,"PPC405GPr %c",rev);
694 else
695 sprintf(cpustr,"PPC405GP %c",rev);
696
697 i=0;
698 s=getenv ("serial#");
699#ifdef CONFIG_PIP405
700 if (!s || strncmp (s, "PIP405", 6)) {
701 sprintf(buf,"### No HW ID - assuming PIP405");
702 }
703#endif
704#ifdef CONFIG_MIP405
705 if (!s || strncmp (s, "MIP405", 6)) {
706 sprintf(buf,"### No HW ID - assuming MIP405");
707 }
708#endif
709 else {
710 for (e = s; *e; ++e) {
711 if (*e == ' ')
712 break;
713 }
714 for (; s < e; ++s) {
715 if (*s == '_') {
716 ++s;
717 break;
718 }
719 buf[i++] = *s;
720 }
721 sprintf(&buf[i]," SN ");
722 i+=4;
723 for (; s < e; ++s) {
724 buf[i++] = *s;
725 }
726 buf[i++]=0;
727 }
728 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
729 buf, cpustr,
730 strmhz (buf1, gd->cpu_clk),
731 strmhz (buf2, sys_info.freqPLB),
732 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
733 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
734 return;
735 case 3:
736
737 boot = get_boot_mode();
738 bc = in8 (CONFIG_PORT_ADDR);
739 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
740 gd->bd->bi_memsize / 0x100000,
741 gd->bd->bi_flashsize / 0x100000,
742 bc,
743 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
744 ctfb.modeIdent);
745 return;
746 case 1:
747 sprintf (buf, "%s",CONFIG_IDENT_STRING);
748 sprintf (info, " %s", &buf[1]);
749 return;
750 }
751
752 *info = 0;
753 return;
754}
755#endif
756
757#endif
758