blob: a6edb6dbca08089c23ef3b157044c083c59d0ad5 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <video_fb.h>
11#include "common_util.h"
12#include <asm/processor.h>
wdenk1fe2c702003-03-06 21:55:29 +000013#include <asm/byteorder.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <i2c.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <pci.h>
wdenk4ea537d2003-12-07 18:32:37 +000016#include <malloc.h>
17#include <bzlib.h>
Simon Glass88ecb9b2016-10-17 20:12:54 -060018#include <video.h>
wdenkc6097192002-11-03 00:24:07 +000019
wdenk7d076412003-05-23 11:38:58 +000020#ifdef CONFIG_PIP405
21#include "../pip405/pip405.h"
Stefan Roeseca8725f2007-10-03 15:01:02 +020022#include <asm/4xx_pci.h>
wdenk7d076412003-05-23 11:38:58 +000023#endif
Tom Rini45ae6732016-09-19 21:55:34 -040024#if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
wdenk7d076412003-05-23 11:38:58 +000025#include "../mip405/mip405.h"
Stefan Roeseca8725f2007-10-03 15:01:02 +020026#include <asm/4xx_pci.h>
wdenk7d076412003-05-23 11:38:58 +000027#endif
Wolfgang Denk6405a152006-03-31 18:32:53 +020028
29DECLARE_GLOBAL_DATA_PTR;
30
wdenkbc01dd52004-01-02 16:05:07 +000031#if defined(CONFIG_PATI)
32#define FIRM_START 0xFFF00000
33#endif
wdenk7d076412003-05-23 11:38:58 +000034
wdenk4ea537d2003-12-07 18:32:37 +000035extern int mem_test(ulong start, ulong ramsize, int quiet);
wdenkc6097192002-11-03 00:24:07 +000036
wdenk4ea537d2003-12-07 18:32:37 +000037#define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038#define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
wdenkc6097192002-11-03 00:24:07 +000039
Tom Rini45ae6732016-09-19 21:55:34 -040040#if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) \
41 || defined(CONFIG_TARGET_MIP405T)
David Müllerb4a931e2011-12-22 13:38:19 +010042/*-----------------------------------------------------------------------
43 * On PIP/MIP405 we have 3 (4) possible boot mode
44 *
45 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
46 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
47 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
48 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
49 * The flash init is the first board specific routine which is called
50 * after code relocation (running from SDRAM)
51 * The first thing we do is to map the Flash CS to the Flash area and
52 * the MPS CS to the MPS area. Since the flash size is unknown at this
53 * point, we use the max flash size and the lowest flash address as base.
54 *
55 * After flash detection we adjust the size of the CS area accordingly.
56 * update_flash_size() will fix in wrong values in the flash_info structure,
57 * misc_init_r() will fix the values in the board info structure
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 /* boot via MPS or MPS mapping */
66 res = BOOT_MPS;
67 if (pbcr & PSR_ROM_LOC)
68 /* boot via PCI.. */
69 res |= BOOT_PCI;
70 return res;
71}
72
73/* Map the flash high (in boot area)
74 This code can only be executed from SDRAM (after relocation).
75*/
76void setup_cs_reloc(void)
77{
78 int mode;
79 /*
80 * since we are relocated, we can set-up the CS finaly
81 * but first of all, switch off PCI mapping (in case it
82 * was a PCI boot)
83 */
84 out32r(PMM0MA, 0L);
85 /* get boot mode */
86 mode = get_boot_mode();
87 /*
88 * we map the flash high in every case
89 * first find out to which CS the flash is attached to
90 */
91 if (mode & BOOT_MPS) {
92 /* map flash high on CS1 and MPS on CS0 */
93 mtdcr(EBC0_CFGADDR, PB0AP);
94 mtdcr(EBC0_CFGDATA, MPS_AP);
95 mtdcr(EBC0_CFGADDR, PB0CR);
96 mtdcr(EBC0_CFGDATA, MPS_CR);
97 /*
98 * we use the default values (max values) for the flash
99 * because its real size is not yet known
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 /* map flash high on CS0 and MPS on CS1 */
107 mtdcr(EBC0_CFGADDR, PB1AP);
108 mtdcr(EBC0_CFGDATA, MPS_AP);
109 mtdcr(EBC0_CFGADDR, PB1CR);
110 mtdcr(EBC0_CFGDATA, MPS_CR);
111 /*
112 * we use the default values (max values) for the flash
113 * because its real size is not yet known
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}
Tom Rini45ae6732016-09-19 21:55:34 -0400121#endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) */
David Müllerb4a931e2011-12-22 13:38:19 +0100122
123#ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
124/* adjust flash start and protection info */
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 /* set up flash CS according to the size */
142 mode = get_boot_mode();
143 if (mode & BOOT_MPS) {
144 /* flash is on CS1 */
145 mtdcr(EBC0_CFGADDR, PB1CR);
146 flashcr = mfdcr(EBC0_CFGDATA);
147 /* we map the flash high in every case */
148 flashcr &= 0x0001FFFF; /* mask out address bits */
149 flashcr |= flash_base; /* start addr */
150 flashcr |= (i << 17); /* size addr */
151 mtdcr(EBC0_CFGADDR, PB1CR);
152 mtdcr(EBC0_CFGDATA, flashcr);
153 } else {
154 /* flash is on CS0 */
155 mtdcr(EBC0_CFGADDR, PB0CR);
156 flashcr = mfdcr(EBC0_CFGDATA);
157 /* we map the flash high in every case */
158 flashcr &= 0x0001FFFF; /* mask out address bits */
159 flashcr |= flash_base; /* start addr */
160 flashcr |= (i << 17); /* size addr */
161 mtdcr(EBC0_CFGADDR, PB0CR);
162 mtdcr(EBC0_CFGDATA, flashcr);
163 }
164
165 for (i = 0; i < info->sector_count; i++)
166 /* adjust sector start address */
167 info->start[i] = flash_base +
168 (info->start[i] - CONFIG_SYS_FLASH_BASE);
169
170 /* unprotect all sectors */
171 flash_protect(FLAG_PROTECT_CLEAR,
172 info->start[0],
173 0xFFFFFFFF,
174 info);
175 flash_protect_default();
176 /* protect reset vector too*/
177 flash_protect(FLAG_PROTECT_SET,
178 info->start[info->sector_count-1],
179 0xFFFFFFFF,
180 info);
181
182 return 0;
183}
184#endif
wdenkc6097192002-11-03 00:24:07 +0000185
wdenk1ebf41e2004-01-02 14:00:00 +0000186static int
wdenk4ea537d2003-12-07 18:32:37 +0000187mpl_prg(uchar *src, ulong size)
wdenkc6097192002-11-03 00:24:07 +0000188{
wdenk4ea537d2003-12-07 18:32:37 +0000189 ulong start;
David Müllerb4a931e2011-12-22 13:38:19 +0100190 flash_info_t *info = &flash_info[0];
wdenk4ea537d2003-12-07 18:32:37 +0000191 int i, rc;
wdenkbc01dd52004-01-02 16:05:07 +0000192#if defined(CONFIG_PATI)
193 int start_sect;
194#endif
Tom Rini45ae6732016-09-19 21:55:34 -0400195#if defined(CONFIG_PIP405) || defined(CONFIG_TARGET_MIP405) \
196 || defined(CONFIG_TARGET_MIP405T) || defined(CONFIG_PATI)
wdenke39c2842003-06-04 15:05:30 +0000197 char *copystr = (char *)src;
wdenk4ea537d2003-12-07 18:32:37 +0000198 ulong *magic = (ulong *)src;
wdenkc6097192002-11-03 00:24:07 +0000199
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100200 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
wdenk4ea537d2003-12-07 18:32:37 +0000201 puts("Bad Magic number\n");
wdenk1fe2c702003-03-06 21:55:29 +0000202 return -1;
203 }
wdenke39c2842003-06-04 15:05:30 +0000204 /* some more checks before we delete the Flash... */
205 /* Checking the ISO_STRING prevents to program a
206 * wrong Firmware Image into the flash.
207 */
wdenk4ea537d2003-12-07 18:32:37 +0000208 i = 4; /* skip Magic number */
209 while (1) {
210 if (strncmp(&copystr[i], "MEV-", 4) == 0)
wdenke39c2842003-06-04 15:05:30 +0000211 break;
wdenk4ea537d2003-12-07 18:32:37 +0000212 if (i++ >= 0x100) {
213 puts("Firmware Image for unknown Target\n");
wdenke39c2842003-06-04 15:05:30 +0000214 return -1;
215 }
216 }
217 /* we have the ISO STRING, check */
wdenk4ea537d2003-12-07 18:32:37 +0000218 if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
219 printf("Wrong Firmware Image: %s\n", &copystr[i]);
wdenke39c2842003-06-04 15:05:30 +0000220 return -1;
221 }
wdenkbc01dd52004-01-02 16:05:07 +0000222#if !defined(CONFIG_PATI)
wdenke39c2842003-06-04 15:05:30 +0000223 start = 0 - size;
David Müllerb4a931e2011-12-22 13:38:19 +0100224
225 /* unprotect sectors used by u-boot */
226 flash_protect(FLAG_PROTECT_CLEAR,
227 start,
228 0xFFFFFFFF,
229 info);
230
231 /* search start sector */
232 for (i = info->sector_count-1; i > 0; i--)
wdenk4ea537d2003-12-07 18:32:37 +0000233 if (start >= info->start[i])
234 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100235
wdenkc6097192002-11-03 00:24:07 +0000236 /* now erase flash */
wdenkc6097192002-11-03 00:24:07 +0000237 printf("Erasing at %lx (sector %d) (start %lx)\n",
238 start,i,info->start[i]);
wdenk4ea537d2003-12-07 18:32:37 +0000239 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
240 puts("ERROR ");
241 flash_perror(rc);
242 return (1);
243 }
wdenk1fe2c702003-03-06 21:55:29 +0000244
Tom Rini45ae6732016-09-19 21:55:34 -0400245#else /* #if !defined(CONFIG_PATI) */
wdenkbc01dd52004-01-02 16:05:07 +0000246 start = FIRM_START;
247 start_sect = -1;
David Müllerb4a931e2011-12-22 13:38:19 +0100248
249 /* search start sector */
250 for (i = info->sector_count-1; i > 0; i--)
251 if (start >= info->start[i])
wdenkbc01dd52004-01-02 16:05:07 +0000252 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100253
254 start_sect = i;
wdenkbc01dd52004-01-02 16:05:07 +0000255
David Müllerb4a931e2011-12-22 13:38:19 +0100256 for (i = info->sector_count-1; i > 0; i--)
257 if ((start + size) >= info->start[i])
wdenkbc01dd52004-01-02 16:05:07 +0000258 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100259
260 /* unprotect sectors used by u-boot */
261 flash_protect(FLAG_PROTECT_CLEAR,
262 start,
263 start + size,
264 info);
wdenkbc01dd52004-01-02 16:05:07 +0000265
wdenkbc01dd52004-01-02 16:05:07 +0000266 /* now erase flash */
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 /* defined(CONFIG_PATI) */
276
wdenk1fe2c702003-03-06 21:55:29 +0000277#elif defined(CONFIG_VCMA9)
278 start = 0;
David Müllerb4a931e2011-12-22 13:38:19 +0100279
280 /* search end sector */
281 for (i = 0; i < info->sector_count; i++)
wdenk1fe2c702003-03-06 21:55:29 +0000282 if (size < info->start[i])
283 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100284
285 flash_protect(FLAG_PROTECT_CLEAR,
286 start,
287 size,
288 info);
289
wdenk1fe2c702003-03-06 21:55:29 +0000290 /* now erase flash */
291 printf("Erasing at %lx (sector %d) (start %lx)\n",
292 start,0,info->start[0]);
wdenk4ea537d2003-12-07 18:32:37 +0000293 if ((rc = flash_erase (info, 0, i)) != 0) {
294 puts("ERROR ");
295 flash_perror(rc);
296 return (1);
297 }
wdenk1fe2c702003-03-06 21:55:29 +0000298
299#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000300 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
301 (ulong)src, size);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200302 if ((rc = flash_write ((char *)src, start, size)) != 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000303 puts("ERROR ");
304 flash_perror(rc);
wdenkc6097192002-11-03 00:24:07 +0000305 return (1);
306 }
wdenk4ea537d2003-12-07 18:32:37 +0000307 puts("OK programming done\n");
wdenkc6097192002-11-03 00:24:07 +0000308 return 0;
309}
310
311
wdenk1ebf41e2004-01-02 14:00:00 +0000312static int
wdenk4ea537d2003-12-07 18:32:37 +0000313mpl_prg_image(uchar *ld_addr)
wdenkc6097192002-11-03 00:24:07 +0000314{
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100315 unsigned long len;
wdenk4ea537d2003-12-07 18:32:37 +0000316 uchar *data;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100317 image_header_t *hdr = (image_header_t *)ld_addr;
wdenk4ea537d2003-12-07 18:32:37 +0000318 int rc;
wdenk1ebf41e2004-01-02 14:00:00 +0000319
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100320#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100321 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100322 puts ("Non legacy image format not supported\n");
323 return -1;
324 }
325#endif
326
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100327 if (!image_check_magic (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000328 puts("Bad Magic Number\n");
wdenkc6097192002-11-03 00:24:07 +0000329 return 1;
330 }
Marian Balakowicza1cc1472008-02-21 17:27:41 +0100331 image_print_contents (hdr);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100332 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
wdenk4ea537d2003-12-07 18:32:37 +0000333 puts("No U-Boot Image\n");
wdenkc6097192002-11-03 00:24:07 +0000334 return 1;
335 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100336 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
wdenk4ea537d2003-12-07 18:32:37 +0000337 puts("No Firmware Image\n");
wdenkc6097192002-11-03 00:24:07 +0000338 return 1;
339 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100340 if (!image_check_hcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000341 puts("Bad Header Checksum\n");
wdenkc6097192002-11-03 00:24:07 +0000342 return 1;
343 }
wdenk4ea537d2003-12-07 18:32:37 +0000344 puts("Verifying Checksum ... ");
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100345 if (!image_check_dcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000346 puts("Bad Data CRC\n");
wdenkc6097192002-11-03 00:24:07 +0000347 return 1;
348 }
wdenk4ea537d2003-12-07 18:32:37 +0000349 puts("OK\n");
350
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100351 data = (uchar *)image_get_data (hdr);
352 len = image_get_data_size (hdr);
353
354 if (image_get_comp (hdr) != IH_COMP_NONE) {
wdenk4ea537d2003-12-07 18:32:37 +0000355 uchar *buf;
356 /* reserve space for uncompressed image */
357 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200358 puts("Insufficient space for decompression\n");
wdenkc6097192002-11-03 00:24:07 +0000359 return 1;
360 }
wdenk1ebf41e2004-01-02 14:00:00 +0000361
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100362 switch (image_get_comp (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000363 case IH_COMP_GZIP:
364 puts("Uncompressing (GZIP) ... ");
wdenka0ebde52004-09-08 22:03:11 +0000365 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
wdenk4ea537d2003-12-07 18:32:37 +0000366 if (rc != 0) {
367 puts("GUNZIP ERROR\n");
368 free(buf);
369 return 1;
370 }
371 puts("OK\n");
372 break;
wdenkc35ba4e2004-03-14 22:25:36 +0000373#ifdef CONFIG_BZIP2
wdenk4ea537d2003-12-07 18:32:37 +0000374 case IH_COMP_BZIP2:
375 puts("Uncompressing (BZIP2) ... ");
376 {
377 uint retlen = IMAGE_SIZE;
378 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
379 (char *)data, len, 0, 0);
380 len = retlen;
381 }
382 if (rc != BZ_OK) {
383 printf ("BUNZIP2 ERROR: %d\n", rc);
384 free(buf);
385 return 1;
386 }
387 puts("OK\n");
388 break;
389#endif
390 default:
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100391 printf ("Unimplemented compression type %d\n",
392 image_get_comp (hdr));
wdenk4ea537d2003-12-07 18:32:37 +0000393 free(buf);
394 return 1;
395 }
wdenk1ebf41e2004-01-02 14:00:00 +0000396
wdenk4ea537d2003-12-07 18:32:37 +0000397 rc = mpl_prg(buf, len);
398 free(buf);
399 } else {
400 rc = mpl_prg(data, len);
wdenkc6097192002-11-03 00:24:07 +0000401 }
wdenk1ebf41e2004-01-02 14:00:00 +0000402
wdenk4ea537d2003-12-07 18:32:37 +0000403 return(rc);
wdenkc6097192002-11-03 00:24:07 +0000404}
405
wdenkbc01dd52004-01-02 16:05:07 +0000406#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000407void get_backup_values(backup_t *buf)
408{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200409 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000410}
411
412void set_backup_values(int overwrite)
413{
414 backup_t back;
415 int i;
416
417 get_backup_values(&back);
418 if(!overwrite) {
419 if(strncmp(back.signature,"MPL\0",4)==0) {
wdenk4ea537d2003-12-07 18:32:37 +0000420 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000421 return;
422 }
423 }
424 memcpy(back.signature,"MPL\0",4);
Wolfgang Denk76af2782010-07-24 21:55:43 +0200425 i = getenv_f("serial#",back.serial_name,16);
wdenk1fe2c702003-03-06 21:55:29 +0000426 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000427 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000428 return;
429 }
430 back.serial_name[16]=0;
Wolfgang Denk76af2782010-07-24 21:55:43 +0200431 i = getenv_f("ethaddr",back.eth_addr,20);
wdenk1fe2c702003-03-06 21:55:29 +0000432 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000433 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000434 return;
435 }
436 back.eth_addr[20]=0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200437 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000438}
439
440void clear_env_values(void)
441{
442 backup_t back;
443 unsigned char env_crc[4];
444
445 memset(&back,0xff,sizeof(backup_t));
446 memset(env_crc,0x00,4);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200447 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
448 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
wdenkc6097192002-11-03 00:24:07 +0000449}
450
451/*
452 * check crc of "older" environment
453 */
454int check_env_old_size(ulong oldsize)
455{
456 ulong crc, len, new;
457 unsigned off;
458 uchar buf[64];
459
460 /* read old CRC */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200461 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200462 CONFIG_ENV_OFFSET,
wdenkc6097192002-11-03 00:24:07 +0000463 (uchar *)&crc, sizeof(ulong));
464
465 new = 0;
466 len = oldsize;
467 off = sizeof(long);
468 len = oldsize-off;
469 while (len > 0) {
470 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
471
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200472 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
wdenkc6097192002-11-03 00:24:07 +0000473 new = crc32 (new, buf, n);
474 len -= n;
475 off += n;
476 }
477
478 return (crc == new);
479}
480
481static ulong oldsizes[] = {
482 0x200,
483 0x800,
484 0
485};
486
487void copy_old_env(ulong size)
488{
489 uchar name_buf[64];
490 uchar value_buf[0x800];
491 uchar c;
492 ulong len;
493 unsigned off;
494 uchar *name, *value;
495
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200496 name = &name_buf[0];
497 value = &value_buf[0];
wdenkc6097192002-11-03 00:24:07 +0000498 len=size;
499 off = sizeof(long);
500 while (len > off) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200501 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000502 if(c != '=') {
503 *name++=c;
504 off++;
505 }
506 else {
507 *name++='\0';
508 off++;
509 do {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200510 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000511 *value++=c;
512 off++;
513 if(c == '\0')
514 break;
515 } while(len > off);
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200516 name = &name_buf[0];
517 value = &value_buf[0];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200518 if(strncmp((char *)name,"baudrate",8)!=0) {
519 setenv((char *)name,(char *)value);
wdenkc6097192002-11-03 00:24:07 +0000520 }
521
522 }
523 }
524}
525
526
527void check_env(void)
528{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200529 char *s;
wdenkc6097192002-11-03 00:24:07 +0000530 int i=0;
531 char buf[32];
532 backup_t back;
533
534 s=getenv("serial#");
535 if(!s) {
536 while(oldsizes[i]) {
537 if(check_env_old_size(oldsizes[i]))
538 break;
539 i++;
540 }
541 if(!oldsizes[i]) {
542 /* no old environment has been found */
543 get_backup_values (&back);
544 if (strncmp (back.signature, "MPL\0", 4) == 0) {
545 sprintf (buf, "%s", back.serial_name);
546 setenv ("serial#", buf);
547 sprintf (buf, "%s", back.eth_addr);
548 setenv ("ethaddr", buf);
549 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
550 return;
551 }
552 }
553 else {
554 copy_old_env(oldsizes[i]);
wdenk4ea537d2003-12-07 18:32:37 +0000555 puts("INFO: old environment ajusted, use saveenv\n");
wdenkc6097192002-11-03 00:24:07 +0000556 }
557 }
558 else {
559 /* check if back up is set */
560 get_backup_values(&back);
561 if(strncmp(back.signature,"MPL\0",4)!=0) {
562 set_backup_values(0);
563 }
564 }
565}
566
wdenkbc01dd52004-01-02 16:05:07 +0000567#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000568
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200569int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000570{
Peter Tysere9b1b492010-10-05 23:48:47 -0500571 ulong ld_addr;
wdenkc6097192002-11-03 00:24:07 +0000572 int result;
wdenkbc01dd52004-01-02 16:05:07 +0000573#if !defined(CONFIG_PATI)
Peter Tysere9b1b492010-10-05 23:48:47 -0500574 ulong size = IMAGE_SIZE;
575 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000576 backup_t back;
wdenkbc01dd52004-01-02 16:05:07 +0000577#endif
wdenkc6097192002-11-03 00:24:07 +0000578
579 if (strcmp(argv[1], "flash") == 0)
580 {
Jon Loeliger145318c2007-07-09 18:38:39 -0500581#if defined(CONFIG_CMD_FDC)
wdenkc6097192002-11-03 00:24:07 +0000582 if (strcmp(argv[2], "floppy") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200583 char *local_args[3];
wdenkc6097192002-11-03 00:24:07 +0000584 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
wdenk4ea537d2003-12-07 18:32:37 +0000585 puts("\nupdating bootloader image from floppy\n");
wdenkc6097192002-11-03 00:24:07 +0000586 local_args[0] = argv[0];
Wolfgang Denka1be4762008-05-20 16:00:29 +0200587 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000588 local_args[1] = argv[3];
589 local_args[2] = NULL;
590 ld_addr=simple_strtoul(argv[3], NULL, 16);
591 result=do_fdcboot(cmdtp, 0, 2, local_args);
592 }
593 else {
594 local_args[1] = NULL;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200595 ld_addr=CONFIG_SYS_LOAD_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000596 result=do_fdcboot(cmdtp, 0, 1, local_args);
597 }
wdenk1ebf41e2004-01-02 14:00:00 +0000598 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000599 return result;
600 }
Jon Loeliger145318c2007-07-09 18:38:39 -0500601#endif
wdenkc6097192002-11-03 00:24:07 +0000602 if (strcmp(argv[2], "mem") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200603 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000604 ld_addr=simple_strtoul(argv[3], NULL, 16);
605 }
606 else {
607 ld_addr=load_addr;
608 }
609 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
wdenk4ea537d2003-12-07 18:32:37 +0000610 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000611 return result;
612 }
wdenkbc01dd52004-01-02 16:05:07 +0000613#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000614 if (strcmp(argv[2], "mps") == 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000615 puts("\nupdating bootloader image from MPS\n");
616 result=mpl_prg((uchar *)src,size);
wdenkc6097192002-11-03 00:24:07 +0000617 return result;
618 }
wdenkbc01dd52004-01-02 16:05:07 +0000619#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000620 }
wdenkbc01dd52004-01-02 16:05:07 +0000621#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000622 if (strcmp(argv[1], "clearenvvalues") == 0)
623 {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200624 if (strcmp(argv[2], "yes") == 0)
wdenkc6097192002-11-03 00:24:07 +0000625 {
626 clear_env_values();
627 return 0;
628 }
629 }
630 if (strcmp(argv[1], "getback") == 0) {
631 get_backup_values(&back);
632 back.signature[3]=0;
633 back.serial_name[16]=0;
634 back.eth_addr[20]=0;
635 printf("GetBackUp: signature: %s\n",back.signature);
636 printf(" serial#: %s\n",back.serial_name);
637 printf(" ethaddr: %s\n",back.eth_addr);
638 return 0;
639 }
640 if (strcmp(argv[1], "setback") == 0) {
641 set_backup_values(1);
642 return 0;
643 }
wdenkbc01dd52004-01-02 16:05:07 +0000644#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200645 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000646}
647
648
Jon Loeliger145318c2007-07-09 18:38:39 -0500649#if defined(CONFIG_CMD_DOC)
wdenkc6097192002-11-03 00:24:07 +0000650void doc_init (void)
651{
652 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
653}
654#endif
655
656
657#ifdef CONFIG_VIDEO
658/******************************************************
659 * Routines to display the Board information
660 * to the screen (since the VGA will be initialized as last,
661 * we must resend the infos)
662 */
663
664#ifdef CONFIG_CONSOLE_EXTRA_INFO
665extern GraphicDevice ctfb;
wdenk2c9b05d2003-09-10 22:30:53 +0000666extern int get_boot_mode(void);
wdenkc6097192002-11-03 00:24:07 +0000667
668void video_get_info_str (int line_number, char *info)
669{
670 /* init video info strings for graphic console */
Stefan Roeseedd73f22007-10-21 08:12:41 +0200671 PPC4xx_SYS_INFO sys_info;
wdenkc6097192002-11-03 00:24:07 +0000672 char rev;
wdenk7d076412003-05-23 11:38:58 +0000673 int i,boot;
wdenkc6097192002-11-03 00:24:07 +0000674 unsigned long pvr;
675 char buf[64];
Wolfgang Denk20591042008-10-19 02:35:49 +0200676 char buf1[32], buf2[32], buf3[32], buf4[32];
wdenk7d076412003-05-23 11:38:58 +0000677 char cpustr[16];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200678 char *s, *e, bc;
wdenkc6097192002-11-03 00:24:07 +0000679 switch (line_number)
680 {
681 case 2:
682 /* CPU and board infos */
683 pvr=get_pvr();
684 get_sys_info (&sys_info);
685 switch (pvr) {
686 case PVR_405GP_RB: rev='B'; break;
687 case PVR_405GP_RC: rev='C'; break;
688 case PVR_405GP_RD: rev='D'; break;
689 case PVR_405GP_RE: rev='E'; break;
wdenk7d076412003-05-23 11:38:58 +0000690 case PVR_405GPR_RB: rev='B'; break;
wdenkc6097192002-11-03 00:24:07 +0000691 default: rev='?'; break;
692 }
wdenk7d076412003-05-23 11:38:58 +0000693 if(pvr==PVR_405GPR_RB)
694 sprintf(cpustr,"PPC405GPr %c",rev);
695 else
696 sprintf(cpustr,"PPC405GP %c",rev);
wdenkc6097192002-11-03 00:24:07 +0000697 /* Board info */
698 i=0;
699 s=getenv ("serial#");
700#ifdef CONFIG_PIP405
701 if (!s || strncmp (s, "PIP405", 6)) {
Ben Whitten34fd6c92015-12-30 13:05:58 +0000702 strcpy(buf,"### No HW ID - assuming PIP405");
wdenkc6097192002-11-03 00:24:07 +0000703 }
704#endif
Tom Rini45ae6732016-09-19 21:55:34 -0400705#if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
wdenkc6097192002-11-03 00:24:07 +0000706 if (!s || strncmp (s, "MIP405", 6)) {
Ben Whitten34fd6c92015-12-30 13:05:58 +0000707 strcpy(buf,"### No HW ID - assuming MIP405");
wdenkc6097192002-11-03 00:24:07 +0000708 }
709#endif
710 else {
711 for (e = s; *e; ++e) {
712 if (*e == ' ')
713 break;
714 }
715 for (; s < e; ++s) {
716 if (*s == '_') {
717 ++s;
718 break;
719 }
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200720 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000721 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000722 strcpy(&buf[i]," SN ");
wdenkc6097192002-11-03 00:24:07 +0000723 i+=4;
724 for (; s < e; ++s) {
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200725 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000726 }
727 buf[i++]=0;
728 }
Wolfgang Denk20591042008-10-19 02:35:49 +0200729 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
wdenk7d076412003-05-23 11:38:58 +0000730 buf, cpustr,
Wolfgang Denk20591042008-10-19 02:35:49 +0200731 strmhz (buf1, gd->cpu_clk),
732 strmhz (buf2, sys_info.freqPLB),
733 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
734 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
wdenkc6097192002-11-03 00:24:07 +0000735 return;
736 case 3:
737 /* Memory Info */
wdenk7d076412003-05-23 11:38:58 +0000738 boot = get_boot_mode();
wdenkc6097192002-11-03 00:24:07 +0000739 bc = in8 (CONFIG_PORT_ADDR);
740 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
741 gd->bd->bi_memsize / 0x100000,
742 gd->bd->bi_flashsize / 0x100000,
743 bc,
wdenk7d076412003-05-23 11:38:58 +0000744 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
wdenkc6097192002-11-03 00:24:07 +0000745 ctfb.modeIdent);
746 return;
747 case 1:
Ben Whitten34fd6c92015-12-30 13:05:58 +0000748 strcpy(buf, CONFIG_IDENT_STRING);
wdenkc6097192002-11-03 00:24:07 +0000749 sprintf (info, " %s", &buf[1]);
750 return;
751 }
752 /* no more info lines */
753 *info = 0;
754 return;
755}
756#endif /* CONFIG_CONSOLE_EXTRA_INFO */
757
758#endif /* CONFIG_VIDEO */