]> git.sur5r.net Git - u-boot/blob - board/mpl/common/common_util.c
17871d2a1bf57474d06427fd53e4bb32e7ab3bca
[u-boot] / 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
46 extern int gunzip(void *, int, uchar *, int *);
47 extern int mem_test(ulong start, ulong ramsize, int quiet);
48
49 #define I2C_BACKUP_ADDR 0x7C00          /* 0x200 bytes for backup */
50 #define IMAGE_SIZE CFG_MONITOR_LEN      /* ugly, but it works for now */
51
52 extern flash_info_t flash_info[];       /* info for FLASH chips */
53
54 static image_header_t header;
55
56
57 static int 
58 mpl_prg(uchar *src, ulong size)
59 {
60         ulong start;
61         flash_info_t *info;
62         int i, rc;
63 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
64         char *copystr = (char *)src;
65         ulong *magic = (ulong *)src;
66 #endif
67
68         info = &flash_info[0];
69
70 #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
71         if (ntohl(magic[0]) != IH_MAGIC) {
72                 puts("Bad Magic number\n");
73                 return -1;
74         }
75         /* some more checks before we delete the Flash... */
76         /* Checking the ISO_STRING prevents to program a
77          * wrong Firmware Image into the flash.
78          */
79         i = 4; /* skip Magic number */
80         while (1) {
81                 if (strncmp(&copystr[i], "MEV-", 4) == 0)
82                         break;
83                 if (i++ >= 0x100) {
84                         puts("Firmware Image for unknown Target\n");
85                         return -1;
86                 }
87         }
88         /* we have the ISO STRING, check */
89         if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
90                 printf("Wrong Firmware Image: %s\n", &copystr[i]);
91                 return -1;
92         }
93         start = 0 - size;
94         for (i = info->sector_count-1; i > 0; i--) {
95                 info->protect[i] = 0; /* unprotect this sector */
96                 if (start >= info->start[i])
97                         break;
98         }
99         /* set-up flash location */
100         /* now erase flash */
101         printf("Erasing at %lx (sector %d) (start %lx)\n",
102                                 start,i,info->start[i]);
103         if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
104                 puts("ERROR ");
105                 flash_perror(rc);
106                 return (1);
107         }
108         
109
110 #elif defined(CONFIG_VCMA9)
111         start = 0;
112         for (i = 0; i <info->sector_count; i++) {
113                 info->protect[i] = 0; /* unprotect this sector */
114                 if (size < info->start[i])
115                     break;
116         }
117         /* set-up flash location */
118         /* now erase flash */
119         printf("Erasing at %lx (sector %d) (start %lx)\n",
120                                 start,0,info->start[0]);
121         if ((rc = flash_erase (info, 0, i)) != 0) {
122                 puts("ERROR ");
123                 flash_perror(rc);
124                 return (1);
125         }
126
127 #endif
128         printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",src,size);
129         if ((rc = flash_write (src, start, size)) != 0) {
130                 puts("ERROR ");
131                 flash_perror(rc);
132                 return (1);
133         }
134         puts("OK programming done\n");
135         return 0;
136 }
137
138
139 static int 
140 mpl_prg_image(uchar *ld_addr)
141 {
142         unsigned long len, checksum;
143         uchar *data;
144         image_header_t *hdr = &header;
145         int rc;
146         
147         /* Copy header so we can blank CRC field for re-calculation */
148         memcpy (&header, (char *)ld_addr, sizeof(image_header_t));
149         if (ntohl(hdr->ih_magic)  != IH_MAGIC) {
150                 puts("Bad Magic Number\n");
151                 return 1;
152         }
153         print_image_hdr(hdr);
154         if (hdr->ih_os  != IH_OS_U_BOOT) {
155                 puts("No U-Boot Image\n");
156                 return 1;
157         }
158         if (hdr->ih_type  != IH_TYPE_FIRMWARE) {
159                 puts("No Firmware Image\n");
160                 return 1;
161         }
162         data = (uchar *)&header;
163         len  = sizeof(image_header_t);
164         checksum = ntohl(hdr->ih_hcrc);
165         hdr->ih_hcrc = 0;
166         if (crc32 (0, (char *)data, len) != checksum) {
167                 puts("Bad Header Checksum\n");
168                 return 1;
169         }
170         data = ld_addr + sizeof(image_header_t);
171         len  = ntohl(hdr->ih_size);
172         puts("Verifying Checksum ... ");
173         if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
174                 puts("Bad Data CRC\n");
175                 return 1;
176         }
177         puts("OK\n");
178
179         if (hdr->ih_comp != IH_COMP_NONE) {
180                 uchar *buf;
181                 /* reserve space for uncompressed image */
182                 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
183                         puts("Insufficient space for decompression\n");
184                         return 1;
185                 }
186                                  
187                 switch (hdr->ih_comp) {
188                 case IH_COMP_GZIP:
189                         puts("Uncompressing (GZIP) ... ");
190                         rc = gunzip ((void *)(buf), IMAGE_SIZE, data, (int *)&len);
191                         if (rc != 0) {
192                                 puts("GUNZIP ERROR\n");
193                                 free(buf);
194                                 return 1;
195                         }
196                         puts("OK\n");
197                         break;
198 #if CONFIG_BZIP2
199                 case IH_COMP_BZIP2:
200                         puts("Uncompressing (BZIP2) ... ");
201                         {
202                         uint retlen = IMAGE_SIZE;
203                         rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
204                                 (char *)data, len, 0, 0);
205                         len = retlen;
206                         }
207                         if (rc != BZ_OK) {
208                                 printf ("BUNZIP2 ERROR: %d\n", rc);
209                                 free(buf);
210                                 return 1;
211                         }
212                         puts("OK\n");
213                         break;
214 #endif
215                 default:
216                         printf ("Unimplemented compression type %d\n", hdr->ih_comp);
217                         free(buf);
218                         return 1;
219                 }
220                 
221                 rc = mpl_prg(buf, len);
222                 free(buf);
223         } else {
224                 rc = mpl_prg(data, len);
225         }
226         
227         return(rc);
228 }
229
230
231 void get_backup_values(backup_t *buf)
232 {
233         i2c_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
234 }
235
236 void set_backup_values(int overwrite)
237 {
238         backup_t back;
239         int i;
240
241         get_backup_values(&back);
242         if(!overwrite) {
243                 if(strncmp(back.signature,"MPL\0",4)==0) {
244                         puts("Not possible to write Backup\n");
245                         return;
246                 }
247         }
248         memcpy(back.signature,"MPL\0",4);
249         i = getenv_r("serial#",back.serial_name,16);
250         if(i < 0) {
251                 puts("Not possible to write Backup\n");
252                 return;
253         }
254         back.serial_name[16]=0;
255         i = getenv_r("ethaddr",back.eth_addr,20);
256         if(i < 0) {
257                 puts("Not possible to write Backup\n");
258                 return;
259         }
260         back.eth_addr[20]=0;
261         i2c_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
262 }
263
264 void clear_env_values(void)
265 {
266         backup_t back;
267         unsigned char env_crc[4];
268
269         memset(&back,0xff,sizeof(backup_t));
270         memset(env_crc,0x00,4);
271         i2c_write(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
272         i2c_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,2,(void *)env_crc,4);
273 }
274
275 /*
276  * check crc of "older" environment
277  */
278 int check_env_old_size(ulong oldsize)
279 {
280         ulong crc, len, new;
281         unsigned off;
282         uchar buf[64];
283
284         /* read old CRC */
285         eeprom_read (CFG_DEF_EEPROM_ADDR,
286                      CFG_ENV_OFFSET,
287                      (uchar *)&crc, sizeof(ulong));
288
289         new = 0;
290         len = oldsize;
291         off = sizeof(long);
292         len = oldsize-off;
293         while (len > 0) {
294                 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
295
296                 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, buf, n);
297                 new = crc32 (new, buf, n);
298                 len -= n;
299                 off += n;
300         }
301
302         return (crc == new);
303 }
304
305 static ulong oldsizes[] = {
306         0x200,
307         0x800,
308         0
309 };
310
311 void copy_old_env(ulong size)
312 {
313         uchar name_buf[64];
314         uchar value_buf[0x800];
315         uchar c;
316         ulong len;
317         unsigned off;
318         uchar *name, *value;
319
320         name=&name_buf[0];
321         value=&value_buf[0];
322         len=size;
323         off = sizeof(long);
324         while (len > off) {
325                 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
326                 if(c != '=') {
327                         *name++=c;
328                         off++;
329                 }
330                 else {
331                         *name++='\0';
332                         off++;
333                         do {
334                                 eeprom_read (CFG_DEF_EEPROM_ADDR, CFG_ENV_OFFSET+off, &c, 1);
335                                 *value++=c;
336                                 off++;
337                                 if(c == '\0')
338                                         break;
339                         } while(len > off);
340                         name=&name_buf[0];
341                         value=&value_buf[0];
342                         if(strncmp(name,"baudrate",8)!=0) {
343                                 setenv(name,value);
344                         }
345
346                 }
347         }
348 }
349
350
351 void check_env(void)
352 {
353         unsigned char *s;
354         int i=0;
355         char buf[32];
356         backup_t back;
357
358         s=getenv("serial#");
359         if(!s) {
360                 while(oldsizes[i]) {
361                         if(check_env_old_size(oldsizes[i]))
362                                 break;
363                         i++;
364                 }
365                 if(!oldsizes[i]) {
366                         /* no old environment has been found */
367                         get_backup_values (&back);
368                         if (strncmp (back.signature, "MPL\0", 4) == 0) {
369                                 sprintf (buf, "%s", back.serial_name);
370                                 setenv ("serial#", buf);
371                                 sprintf (buf, "%s", back.eth_addr);
372                                 setenv ("ethaddr", buf);
373                                 printf ("INFO:  serial# and ethaddr recovered, use saveenv\n");
374                                 return;
375                         }
376                 }
377                 else {
378                         copy_old_env(oldsizes[i]);
379                         puts("INFO:  old environment ajusted, use saveenv\n");
380                 }
381         }
382         else {
383                 /* check if back up is set */
384                 get_backup_values(&back);
385                 if(strncmp(back.signature,"MPL\0",4)!=0) {
386                         set_backup_values(0);
387                 }
388         }
389 }
390
391
392 extern device_t *stdio_devices[];
393 extern char *stdio_names[];
394
395 void show_stdio_dev(void)
396 {
397         /* Print information */
398         puts("In:    ");
399         if (stdio_devices[stdin] == NULL) {
400                 puts("No input devices available!\n");
401         } else {
402                 printf ("%s\n", stdio_devices[stdin]->name);
403         }
404
405         puts("Out:   ");
406         if (stdio_devices[stdout] == NULL) {
407                 puts("No output devices available!\n");
408         } else {
409                 printf ("%s\n", stdio_devices[stdout]->name);
410         }
411
412         puts("Err:   ");
413         if (stdio_devices[stderr] == NULL) {
414                 puts("No error devices available!\n");
415         } else {
416                 printf ("%s\n", stdio_devices[stderr]->name);
417         }
418 }
419
420
421 int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
422 {
423         ulong size,src,ld_addr;
424         int result;
425         backup_t back;
426         src = MULTI_PURPOSE_SOCKET_ADDR;
427         size = IMAGE_SIZE;
428
429         if (strcmp(argv[1], "flash") == 0)
430         {
431 #if (CONFIG_COMMANDS & CFG_CMD_FDC)
432                 if (strcmp(argv[2], "floppy") == 0) {
433                         char *local_args[3];
434                         extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
435                         puts("\nupdating bootloader image from floppy\n");
436                         local_args[0] = argv[0];
437                         if(argc==4) {
438                                 local_args[1] = argv[3];
439                                 local_args[2] = NULL;
440                                 ld_addr=simple_strtoul(argv[3], NULL, 16);
441                                 result=do_fdcboot(cmdtp, 0, 2, local_args);
442                         }
443                         else {
444                                 local_args[1] = NULL;
445                                 ld_addr=CFG_LOAD_ADDR;
446                                 result=do_fdcboot(cmdtp, 0, 1, local_args);
447                         }
448                         result=mpl_prg_image(ld_addr);
449                         return result;
450                 }
451 #endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
452                 if (strcmp(argv[2], "mem") == 0) {
453                         if(argc==4) {
454                                 ld_addr=simple_strtoul(argv[3], NULL, 16);
455                         }
456                         else {
457                                 ld_addr=load_addr;
458                         }
459                         printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
460                         result=mpl_prg_image((uchar *)ld_addr);
461                         return result;
462                 }
463                 if (strcmp(argv[2], "mps") == 0) {
464                         puts("\nupdating bootloader image from MPS\n");
465                         result=mpl_prg((uchar *)src,size);
466                         return result;
467                 }
468         }
469         if (strcmp(argv[1], "mem") == 0)
470         {
471                 result=0;
472                 if(argc==3)
473                 {
474                         result = (int)simple_strtol(argv[2], NULL, 16);
475             }
476             src=(unsigned long)&result;
477             src-=CFG_MEMTEST_START;
478             src-=(100*1024); /* - 100k */
479             src&=0xfff00000;
480             size=0;
481             do {
482                 size++;
483                         printf("\n\nPass %ld\n",size);
484                         mem_test(CFG_MEMTEST_START,src,1);
485                         if(ctrlc())
486                                 break;
487                         if(result>0)
488                                 result--;
489
490                 }while(result);
491                 return 0;
492         }
493         if (strcmp(argv[1], "clearenvvalues") == 0)
494         {
495                 if (strcmp(argv[2], "yes") == 0)
496                 {
497                         clear_env_values();
498                         return 0;
499                 }
500         }
501         if (strcmp(argv[1], "getback") == 0) {
502                 get_backup_values(&back);
503                 back.signature[3]=0;
504                 back.serial_name[16]=0;
505                 back.eth_addr[20]=0;
506                 printf("GetBackUp: signature: %s\n",back.signature);
507                 printf("           serial#:   %s\n",back.serial_name);
508                 printf("           ethaddr:   %s\n",back.eth_addr);
509                 return 0;
510         }
511         if (strcmp(argv[1], "setback") == 0) {
512                 set_backup_values(1);
513                 return 0;
514         }
515         printf("Usage:\n%s\n", cmdtp->usage);
516         return 1;
517 }
518
519
520 #if (CONFIG_COMMANDS & CFG_CMD_DOC)
521 extern void doc_probe(ulong physadr);
522 void doc_init (void)
523 {
524   doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
525 }
526 #endif
527
528
529 #ifdef CONFIG_VIDEO
530 /******************************************************
531  * Routines to display the Board information
532  * to the screen (since the VGA will be initialized as last,
533  * we must resend the infos)
534  */
535
536 #ifdef CONFIG_CONSOLE_EXTRA_INFO
537 extern GraphicDevice ctfb;
538 extern int get_boot_mode(void);
539
540 void video_get_info_str (int line_number, char *info)
541 {
542         /* init video info strings for graphic console */
543         DECLARE_GLOBAL_DATA_PTR;
544         PPC405_SYS_INFO sys_info;
545         char rev;
546         int i,boot;
547         unsigned long pvr;
548         char buf[64];
549         char tmp[16];
550         char cpustr[16];
551         unsigned char *s, *e, bc;
552         switch (line_number)
553         {
554         case 2:
555                 /* CPU and board infos */
556                 pvr=get_pvr();
557                 get_sys_info (&sys_info);
558                 switch (pvr) {
559                         case PVR_405GP_RB: rev='B'; break;
560                         case PVR_405GP_RC: rev='C'; break;
561                         case PVR_405GP_RD: rev='D'; break;
562                         case PVR_405GP_RE: rev='E'; break;
563                         case PVR_405GPR_RB: rev='B'; break;
564                         default:           rev='?'; break;
565                 }
566                 if(pvr==PVR_405GPR_RB)
567                         sprintf(cpustr,"PPC405GPr %c",rev);
568                 else
569                         sprintf(cpustr,"PPC405GP %c",rev);
570                 /* Board info */
571                 i=0;
572                 s=getenv ("serial#");
573 #ifdef CONFIG_PIP405
574                 if (!s || strncmp (s, "PIP405", 6)) {
575                         sprintf(buf,"### No HW ID - assuming PIP405");
576                 }
577 #endif
578 #ifdef CONFIG_MIP405
579                 if (!s || strncmp (s, "MIP405", 6)) {
580                         sprintf(buf,"### No HW ID - assuming MIP405");
581                 }
582 #endif
583                 else {
584                         for (e = s; *e; ++e) {
585                                 if (*e == ' ')
586                                         break;
587                         }
588                         for (; s < e; ++s) {
589                                 if (*s == '_') {
590                                         ++s;
591                                         break;
592                                 }
593                                 buf[i++]=*s;
594                         }
595                         sprintf(&buf[i]," SN ");
596                         i+=4;
597                         for (; s < e; ++s) {
598                                 buf[i++]=*s;
599                         }
600                         buf[i++]=0;
601                 }
602                 sprintf (info," %s %s %s MHz (%lu/%lu/%lu MHz)",
603                         buf, cpustr,
604                         strmhz (tmp, gd->cpu_clk), sys_info.freqPLB / 1000000,
605                         sys_info.freqPLB / sys_info.pllOpbDiv / 1000000,
606                         sys_info.freqPLB / sys_info.pllExtBusDiv / 1000000);
607                 return;
608         case 3:
609                 /* Memory Info */
610                 boot = get_boot_mode();
611                 bc = in8 (CONFIG_PORT_ADDR);
612                 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
613                         gd->bd->bi_memsize / 0x100000,
614                         gd->bd->bi_flashsize / 0x100000,
615                         bc,
616                         (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
617                         ctfb.modeIdent);
618                 return;
619         case 1:
620                 sprintf (buf, "%s",CONFIG_IDENT_STRING);
621                 sprintf (info, " %s", &buf[1]);
622                 return;
623     }
624     /* no more info lines */
625     *info = 0;
626     return;
627 }
628 #endif /* CONFIG_CONSOLE_EXTRA_INFO */
629
630 #endif /* CONFIG_VIDEO */