]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/mpl/common/common_util.c
b331d6ec474234b1cedf7674e5d43e4cfa429d17
[people/ms/u-boot.git] / board / mpl / common / common_util.c
1 /*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 *
23 */
24
25 #include <common.h>
26 #include <command.h>
27 #include <video_fb.h>
28 #include "common_util.h"
29 #include <asm/processor.h>
30 #include <asm/byteorder.h>
31 #include <i2c.h>
32 #include <devices.h>
33 #include <pci.h>
34 #include <malloc.h>
35 #include <bzlib.h>
36
37 #ifdef CONFIG_PIP405
38 #include "../pip405/pip405.h"
39 #include <405gp_pci.h>
40 #endif
41 #ifdef CONFIG_MIP405
42 #include "../mip405/mip405.h"
43 #include <405gp_pci.h>
44 #endif
45 #if defined(CONFIG_PATI)
46 #define FIRM_START 0xFFF00000
47 #endif
48
49 extern int gunzip(void *, int, uchar *, unsigned long *);
50 extern int mem_test(ulong start, ulong ramsize, int quiet);
51
52 #define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
53 #define IMAGE_SIZE CFG_MONITOR_LEN /* ugly, but it works for now */
54
55 extern flash_info_t flash_info[]; /* info for FLASH chips */
56
57 static image_header_t header;
58
59
60 static int
61 mpl_prg(uchar *src, ulong size)
62 {
63 ulong start;
64 flash_info_t *info;
65 int i, rc;
66 #if defined(CONFIG_PATI)
67 int start_sect;
68 #endif
69 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
70 char *copystr = (char *)src;
71 ulong *magic = (ulong *)src;
72 #endif
73
74 info = &flash_info[0];
75
76 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
77 if (ntohl(magic[0]) != IH_MAGIC) {
78 puts("Bad Magic number\n");
79 return -1;
80 }
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.
84 */
85 i = 4; /* skip Magic number */
86 while (1) {
87 if (strncmp(&copystr[i], "MEV-", 4) == 0)
88 break;
89 if (i++ >= 0x100) {
90 puts("Firmware Image for unknown Target\n");
91 return -1;
92 }
93 }
94 /* we have the ISO STRING, check */
95 if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
96 printf("Wrong Firmware Image: %s\n", &copystr[i]);
97 return -1;
98 }
99 #if !defined(CONFIG_PATI)
100 start = 0 - size;
101 for (i = info->sector_count-1; i > 0; i--) {
102 info->protect[i] = 0; /* unprotect this sector */
103 if (start >= info->start[i])
104 break;
105 }
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) {
111 puts("ERROR ");
112 flash_perror(rc);
113 return (1);
114 }
115
116 #else /* #if !defined(CONFIG_PATI */
117 start = FIRM_START;
118 start_sect = -1;
119 for (i = 0; i < info->sector_count; i++) {
120 if (start < info->start[i]) {
121 start_sect = i - 1;
122 break;
123 }
124 }
125
126 info->protect[i - 1] = 0; /* unprotect this sector */
127 for (; i < info->sector_count; i++) {
128 if ((start + size) < info->start[i])
129 break;
130 info->protect[i] = 0; /* unprotect this sector */
131 }
132
133 i--;
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) {
140 puts ("ERROR ");
141 flash_perror (rc);
142 return (1);
143 }
144 #endif /* defined(CONFIG_PATI) */
145
146 #elif defined(CONFIG_VCMA9)
147 start = 0;
148 for (i = 0; i <info->sector_count; i++) {
149 info->protect[i] = 0; /* unprotect this sector */
150 if (size < info->start[i])
151 break;
152 }
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) {
158 puts("ERROR ");
159 flash_perror(rc);
160 return (1);
161 }
162
163 #endif
164 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
165 (ulong)src, size);
166 if ((rc = flash_write ((char *)src, start, size)) != 0) {
167 puts("ERROR ");
168 flash_perror(rc);
169 return (1);
170 }
171 puts("OK programming done\n");
172 return 0;
173 }
174
175
176 static int
177 mpl_prg_image(uchar *ld_addr)
178 {
179 unsigned long len, checksum;
180 uchar *data;
181 image_header_t *hdr = &header;
182 int rc;
183
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");
188 return 1;
189 }
190 print_image_hdr(hdr);
191 if (hdr->ih_os != IH_OS_U_BOOT) {
192 puts("No U-Boot Image\n");
193 return 1;
194 }
195 if (hdr->ih_type != IH_TYPE_FIRMWARE) {
196 puts("No Firmware Image\n");
197 return 1;
198 }
199 data = (uchar *)&header;
200 len = sizeof(image_header_t);
201 checksum = ntohl(hdr->ih_hcrc);
202 hdr->ih_hcrc = 0;
203 if (crc32 (0, (uchar *)data, len) != checksum) {
204 puts("Bad Header Checksum\n");
205 return 1;
206 }
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");
212 return 1;
213 }
214 puts("OK\n");
215
216 if (hdr->ih_comp != IH_COMP_NONE) {
217 uchar *buf;
218 /* reserve space for uncompressed image */
219 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
220 puts("Insufficient space for decompression\n");
221 return 1;
222 }
223
224 switch (hdr->ih_comp) {
225 case IH_COMP_GZIP:
226 puts("Uncompressing (GZIP) ... ");
227 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
228 if (rc != 0) {
229 puts("GUNZIP ERROR\n");
230 free(buf);
231 return 1;
232 }
233 puts("OK\n");
234 break;
235 #ifdef CONFIG_BZIP2
236 case IH_COMP_BZIP2:
237 puts("Uncompressing (BZIP2) ... ");
238 {
239 uint retlen = IMAGE_SIZE;
240 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
241 (char *)data, len, 0, 0);
242 len = retlen;
243 }
244 if (rc != BZ_OK) {
245 printf ("BUNZIP2 ERROR: %d\n", rc);
246 free(buf);
247 return 1;
248 }
249 puts("OK\n");
250 break;
251 #endif
252 default:
253 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
254 free(buf);
255 return 1;
256 }
257
258 rc = mpl_prg(buf, len);
259 free(buf);
260 } else {
261 rc = mpl_prg(data, len);
262 }
263
264 return(rc);
265 }
266
267 #if !defined(CONFIG_PATI)
268 void get_backup_values(backup_t *buf)
269 {
270 i2c_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
271 }
272
273 void set_backup_values(int overwrite)
274 {
275 backup_t back;
276 int i;
277
278 get_backup_values(&back);
279 if(!overwrite) {
280 if(strncmp(back.signature,"MPL\0",4)==0) {
281 puts("Not possible to write Backup\n");
282 return;
283 }
284 }
285 memcpy(back.signature,"MPL\0",4);
286 i = getenv_r("serial#",back.serial_name,16);
287 if(i < 0) {
288 puts("Not possible to write Backup\n");
289 return;
290 }
291 back.serial_name[16]=0;
292 i = getenv_r("ethaddr",back.eth_addr,20);
293 if(i < 0) {
294 puts("Not possible to write Backup\n");
295 return;
296 }
297 back.eth_addr[20]=0;
298 i2c_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
299 }
300
301 void clear_env_values(void)
302 {
303 backup_t back;
304 unsigned char env_crc[4];
305
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);
310 }
311
312 /*
313 * check crc of "older" environment
314 */
315 int check_env_old_size(ulong oldsize)
316 {
317 ulong crc, len, new;
318 unsigned off;
319 uchar buf[64];
320
321 /* read old CRC */
322 eeprom_read (CFG_DEF_EEPROM_ADDR,
323 CFG_ENV_OFFSET,
324 (uchar *)&crc, sizeof(ulong));
325
326 new = 0;
327 len = oldsize;
328 off = sizeof(long);
329 len = oldsize-off;
330 while (len > 0) {
331 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
332
333 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
334 new = crc32 (new, buf, n);
335 len -= n;
336 off += n;
337 }
338
339 return (crc == new);
340 }
341
342 static ulong oldsizes[] = {
343 0x200,
344 0x800,
345 0
346 };
347
348 void copy_old_env(ulong size)
349 {
350 uchar name_buf[64];
351 uchar value_buf[0x800];
352 uchar c;
353 ulong len;
354 unsigned off;
355 uchar *name, *value;
356
357 name=&name_buf[0];
358 value=&value_buf[0];
359 len=size;
360 off = sizeof(long);
361 while (len > off) {
362 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
363 if(c != '=') {
364 *name++=c;
365 off++;
366 }
367 else {
368 *name++='\0';
369 off++;
370 do {
371 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
372 *value++=c;
373 off++;
374 if(c == '\0')
375 break;
376 } while(len > off);
377 name=&name_buf[0];
378 value=&value_buf[0];
379 if(strncmp((char *)name,"baudrate",8)!=0) {
380 setenv((char *)name,(char *)value);
381 }
382
383 }
384 }
385 }
386
387
388 void check_env(void)
389 {
390 char *s;
391 int i=0;
392 char buf[32];
393 backup_t back;
394
395 s=getenv("serial#");
396 if(!s) {
397 while(oldsizes[i]) {
398 if(check_env_old_size(oldsizes[i]))
399 break;
400 i++;
401 }
402 if(!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");
411 return;
412 }
413 }
414 else {
415 copy_old_env(oldsizes[i]);
416 puts("INFO: old environment ajusted, use saveenv\n");
417 }
418 }
419 else {
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);
424 }
425 }
426 }
427
428
429 extern device_t *stdio_devices[];
430 extern char *stdio_names[];
431
432 void show_stdio_dev(void)
433 {
434 /* Print information */
435 puts("In: ");
436 if (stdio_devices[stdin] == NULL) {
437 puts("No input devices available!\n");
438 } else {
439 printf ("%s\n", stdio_devices[stdin]->name);
440 }
441
442 puts("Out: ");
443 if (stdio_devices[stdout] == NULL) {
444 puts("No output devices available!\n");
445 } else {
446 printf ("%s\n", stdio_devices[stdout]->name);
447 }
448
449 puts("Err: ");
450 if (stdio_devices[stderr] == NULL) {
451 puts("No error devices available!\n");
452 } else {
453 printf ("%s\n", stdio_devices[stderr]->name);
454 }
455 }
456
457 #endif /* #if !defined(CONFIG_PATI) */
458
459 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
460 {
461 ulong size,src,ld_addr;
462 int result;
463 #if !defined(CONFIG_PATI)
464 backup_t back;
465 src = MULTI_PURPOSE_SOCKET_ADDR;
466 size = IMAGE_SIZE;
467 #endif
468
469 if (strcmp(argv[1], "flash") == 0)
470 {
471 #if (CONFIG_COMMANDS & CFG_CMD_FDC)
472 if (strcmp(argv[2], "floppy") == 0) {
473 char *local_args[3];
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];
477 if(argc==4) {
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);
482 }
483 else {
484 local_args[1] = NULL;
485 ld_addr=CFG_LOAD_ADDR;
486 result=do_fdcboot(cmdtp, 0, 1, local_args);
487 }
488 result=mpl_prg_image((uchar *)ld_addr);
489 return result;
490 }
491 #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
492 if (strcmp(argv[2], "mem") == 0) {
493 if(argc==4) {
494 ld_addr=simple_strtoul(argv[3], NULL, 16);
495 }
496 else {
497 ld_addr=load_addr;
498 }
499 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
500 result=mpl_prg_image((uchar *)ld_addr);
501 return result;
502 }
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);
507 return result;
508 }
509 #endif /* #if !defined(CONFIG_PATI) */
510 }
511 if (strcmp(argv[1], "mem") == 0)
512 {
513 result=0;
514 if(argc==3)
515 {
516 result = (int)simple_strtol(argv[2], NULL, 16);
517 }
518 src=(unsigned long)&result;
519 src-=CFG_MEMTEST_START;
520 src-=(100*1024); /* - 100k */
521 src&=0xfff00000;
522 size=0;
523 do {
524 size++;
525 printf("\n\nPass %ld\n",size);
526 mem_test(CFG_MEMTEST_START,src,1);
527 if(ctrlc())
528 break;
529 if(result>0)
530 result--;
531
532 }while(result);
533 return 0;
534 }
535 #if !defined(CONFIG_PATI)
536 if (strcmp(argv[1], "clearenvvalues") == 0)
537 {
538 if (strcmp(argv[2], "yes") == 0)
539 {
540 clear_env_values();
541 return 0;
542 }
543 }
544 if (strcmp(argv[1], "getback") == 0) {
545 get_backup_values(&back);
546 back.signature[3]=0;
547 back.serial_name[16]=0;
548 back.eth_addr[20]=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);
552 return 0;
553 }
554 if (strcmp(argv[1], "setback") == 0) {
555 set_backup_values(1);
556 return 0;
557 }
558 #endif
559 printf("Usage:\n%s\n", cmdtp->usage);
560 return 1;
561 }
562
563
564 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
565 extern void doc_probe(ulong physadr);
566 void doc_init (void)
567 {
568 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
569 }
570 #endif
571
572
573 #ifdef CONFIG_VIDEO
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)
578 */
579
580 #ifdef CONFIG_CONSOLE_EXTRA_INFO
581 extern GraphicDevice ctfb;
582 extern int get_boot_mode(void);
583
584 void video_get_info_str (int line_number, char *info)
585 {
586 /* init video info strings for graphic console */
587 DECLARE_GLOBAL_DATA_PTR;
588 PPC405_SYS_INFO sys_info;
589 char rev;
590 int i,boot;
591 unsigned long pvr;
592 char buf[64];
593 char tmp[16];
594 char cpustr[16];
595 char *s, *e, bc;
596 switch (line_number)
597 {
598 case 2:
599 /* CPU and board infos */
600 pvr=get_pvr();
601 get_sys_info (&sys_info);
602 switch (pvr) {
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;
609 }
610 if(pvr==PVR_405GPR_RB)
611 sprintf(cpustr,"PPC405GPr %c",rev);
612 else
613 sprintf(cpustr,"PPC405GP %c",rev);
614 /* Board info */
615 i=0;
616 s=getenv ("serial#");
617 #ifdef CONFIG_PIP405
618 if (!s || strncmp (s, "PIP405", 6)) {
619 sprintf(buf,"### No HW ID - assuming PIP405");
620 }
621 #endif
622 #ifdef CONFIG_MIP405
623 if (!s || strncmp (s, "MIP405", 6)) {
624 sprintf(buf,"### No HW ID - assuming MIP405");
625 }
626 #endif
627 else {
628 for (e = s; *e; ++e) {
629 if (*e == ' ')
630 break;
631 }
632 for (; s < e; ++s) {
633 if (*s == '_') {
634 ++s;
635 break;
636 }
637 buf[i++]=*s;
638 }
639 sprintf(&buf[i]," SN ");
640 i+=4;
641 for (; s < e; ++s) {
642 buf[i++]=*s;
643 }
644 buf[i++]=0;
645 }
646 sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
647 buf, cpustr,
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);
651 return;
652 case 3:
653 /* Memory Info */
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,
659 bc,
660 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
661 ctfb.modeIdent);
662 return;
663 case 1:
664 sprintf (buf, "%s",CONFIG_IDENT_STRING);
665 sprintf (info, " %s", &buf[1]);
666 return;
667 }
668 /* no more info lines */
669 *info = 0;
670 return;
671 }
672 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
673
674 #endif /* CONFIG_VIDEO */