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