blob: 5ea5a5187ba9bfe7810baedd1b9aef2c77b50fe1 [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) */
wdenk1fe2c702003-03-06 21:55:29 +0000276#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000277 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
278 (ulong)src, size);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200279 if ((rc = flash_write ((char *)src, start, size)) != 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000280 puts("ERROR ");
281 flash_perror(rc);
wdenkc6097192002-11-03 00:24:07 +0000282 return (1);
283 }
wdenk4ea537d2003-12-07 18:32:37 +0000284 puts("OK programming done\n");
wdenkc6097192002-11-03 00:24:07 +0000285 return 0;
286}
287
288
wdenk1ebf41e2004-01-02 14:00:00 +0000289static int
wdenk4ea537d2003-12-07 18:32:37 +0000290mpl_prg_image(uchar *ld_addr)
wdenkc6097192002-11-03 00:24:07 +0000291{
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100292 unsigned long len;
wdenk4ea537d2003-12-07 18:32:37 +0000293 uchar *data;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100294 image_header_t *hdr = (image_header_t *)ld_addr;
wdenk4ea537d2003-12-07 18:32:37 +0000295 int rc;
wdenk1ebf41e2004-01-02 14:00:00 +0000296
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100297#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100298 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100299 puts ("Non legacy image format not supported\n");
300 return -1;
301 }
302#endif
303
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100304 if (!image_check_magic (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000305 puts("Bad Magic Number\n");
wdenkc6097192002-11-03 00:24:07 +0000306 return 1;
307 }
Marian Balakowicza1cc1472008-02-21 17:27:41 +0100308 image_print_contents (hdr);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100309 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
wdenk4ea537d2003-12-07 18:32:37 +0000310 puts("No U-Boot Image\n");
wdenkc6097192002-11-03 00:24:07 +0000311 return 1;
312 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100313 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
wdenk4ea537d2003-12-07 18:32:37 +0000314 puts("No Firmware Image\n");
wdenkc6097192002-11-03 00:24:07 +0000315 return 1;
316 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100317 if (!image_check_hcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000318 puts("Bad Header Checksum\n");
wdenkc6097192002-11-03 00:24:07 +0000319 return 1;
320 }
wdenk4ea537d2003-12-07 18:32:37 +0000321 puts("Verifying Checksum ... ");
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100322 if (!image_check_dcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000323 puts("Bad Data CRC\n");
wdenkc6097192002-11-03 00:24:07 +0000324 return 1;
325 }
wdenk4ea537d2003-12-07 18:32:37 +0000326 puts("OK\n");
327
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100328 data = (uchar *)image_get_data (hdr);
329 len = image_get_data_size (hdr);
330
331 if (image_get_comp (hdr) != IH_COMP_NONE) {
wdenk4ea537d2003-12-07 18:32:37 +0000332 uchar *buf;
333 /* reserve space for uncompressed image */
334 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200335 puts("Insufficient space for decompression\n");
wdenkc6097192002-11-03 00:24:07 +0000336 return 1;
337 }
wdenk1ebf41e2004-01-02 14:00:00 +0000338
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100339 switch (image_get_comp (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000340 case IH_COMP_GZIP:
341 puts("Uncompressing (GZIP) ... ");
wdenka0ebde52004-09-08 22:03:11 +0000342 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
wdenk4ea537d2003-12-07 18:32:37 +0000343 if (rc != 0) {
344 puts("GUNZIP ERROR\n");
345 free(buf);
346 return 1;
347 }
348 puts("OK\n");
349 break;
wdenkc35ba4e2004-03-14 22:25:36 +0000350#ifdef CONFIG_BZIP2
wdenk4ea537d2003-12-07 18:32:37 +0000351 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:
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100368 printf ("Unimplemented compression type %d\n",
369 image_get_comp (hdr));
wdenk4ea537d2003-12-07 18:32:37 +0000370 free(buf);
371 return 1;
372 }
wdenk1ebf41e2004-01-02 14:00:00 +0000373
wdenk4ea537d2003-12-07 18:32:37 +0000374 rc = mpl_prg(buf, len);
375 free(buf);
376 } else {
377 rc = mpl_prg(data, len);
wdenkc6097192002-11-03 00:24:07 +0000378 }
wdenk1ebf41e2004-01-02 14:00:00 +0000379
wdenk4ea537d2003-12-07 18:32:37 +0000380 return(rc);
wdenkc6097192002-11-03 00:24:07 +0000381}
382
wdenkbc01dd52004-01-02 16:05:07 +0000383#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000384void get_backup_values(backup_t *buf)
385{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200386 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000387}
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) {
wdenk4ea537d2003-12-07 18:32:37 +0000397 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000398 return;
399 }
400 }
401 memcpy(back.signature,"MPL\0",4);
Wolfgang Denk76af2782010-07-24 21:55:43 +0200402 i = getenv_f("serial#",back.serial_name,16);
wdenk1fe2c702003-03-06 21:55:29 +0000403 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000404 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000405 return;
406 }
407 back.serial_name[16]=0;
Wolfgang Denk76af2782010-07-24 21:55:43 +0200408 i = getenv_f("ethaddr",back.eth_addr,20);
wdenk1fe2c702003-03-06 21:55:29 +0000409 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000410 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000411 return;
412 }
413 back.eth_addr[20]=0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200414 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000415}
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);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200424 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);
wdenkc6097192002-11-03 00:24:07 +0000426}
427
428/*
429 * check crc of "older" environment
430 */
431int check_env_old_size(ulong oldsize)
432{
433 ulong crc, len, new;
434 unsigned off;
435 uchar buf[64];
436
437 /* read old CRC */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200438 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200439 CONFIG_ENV_OFFSET,
wdenkc6097192002-11-03 00:24:07 +0000440 (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
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200449 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
wdenkc6097192002-11-03 00:24:07 +0000450 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
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200473 name = &name_buf[0];
474 value = &value_buf[0];
wdenkc6097192002-11-03 00:24:07 +0000475 len=size;
476 off = sizeof(long);
477 while (len > off) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200478 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000479 if(c != '=') {
480 *name++=c;
481 off++;
482 }
483 else {
484 *name++='\0';
485 off++;
486 do {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200487 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000488 *value++=c;
489 off++;
490 if(c == '\0')
491 break;
492 } while(len > off);
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200493 name = &name_buf[0];
494 value = &value_buf[0];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200495 if(strncmp((char *)name,"baudrate",8)!=0) {
496 setenv((char *)name,(char *)value);
wdenkc6097192002-11-03 00:24:07 +0000497 }
498
499 }
500 }
501}
502
503
504void check_env(void)
505{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200506 char *s;
wdenkc6097192002-11-03 00:24:07 +0000507 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 /* no old environment has been found */
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]);
wdenk4ea537d2003-12-07 18:32:37 +0000532 puts("INFO: old environment ajusted, use saveenv\n");
wdenkc6097192002-11-03 00:24:07 +0000533 }
534 }
535 else {
536 /* check if back up is set */
537 get_backup_values(&back);
538 if(strncmp(back.signature,"MPL\0",4)!=0) {
539 set_backup_values(0);
540 }
541 }
542}
543
wdenkbc01dd52004-01-02 16:05:07 +0000544#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000545
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200546int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000547{
Peter Tysere9b1b492010-10-05 23:48:47 -0500548 ulong ld_addr;
wdenkc6097192002-11-03 00:24:07 +0000549 int result;
wdenkbc01dd52004-01-02 16:05:07 +0000550#if !defined(CONFIG_PATI)
Peter Tysere9b1b492010-10-05 23:48:47 -0500551 ulong size = IMAGE_SIZE;
552 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000553 backup_t back;
wdenkbc01dd52004-01-02 16:05:07 +0000554#endif
wdenkc6097192002-11-03 00:24:07 +0000555
556 if (strcmp(argv[1], "flash") == 0)
557 {
Jon Loeliger145318c2007-07-09 18:38:39 -0500558#if defined(CONFIG_CMD_FDC)
wdenkc6097192002-11-03 00:24:07 +0000559 if (strcmp(argv[2], "floppy") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200560 char *local_args[3];
wdenkc6097192002-11-03 00:24:07 +0000561 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
wdenk4ea537d2003-12-07 18:32:37 +0000562 puts("\nupdating bootloader image from floppy\n");
wdenkc6097192002-11-03 00:24:07 +0000563 local_args[0] = argv[0];
Wolfgang Denka1be4762008-05-20 16:00:29 +0200564 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000565 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;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200572 ld_addr=CONFIG_SYS_LOAD_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000573 result=do_fdcboot(cmdtp, 0, 1, local_args);
574 }
wdenk1ebf41e2004-01-02 14:00:00 +0000575 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000576 return result;
577 }
Jon Loeliger145318c2007-07-09 18:38:39 -0500578#endif
wdenkc6097192002-11-03 00:24:07 +0000579 if (strcmp(argv[2], "mem") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200580 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000581 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);
wdenk4ea537d2003-12-07 18:32:37 +0000587 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000588 return result;
589 }
wdenkbc01dd52004-01-02 16:05:07 +0000590#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000591 if (strcmp(argv[2], "mps") == 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000592 puts("\nupdating bootloader image from MPS\n");
593 result=mpl_prg((uchar *)src,size);
wdenkc6097192002-11-03 00:24:07 +0000594 return result;
595 }
wdenkbc01dd52004-01-02 16:05:07 +0000596#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000597 }
wdenkbc01dd52004-01-02 16:05:07 +0000598#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000599 if (strcmp(argv[1], "clearenvvalues") == 0)
600 {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200601 if (strcmp(argv[2], "yes") == 0)
wdenkc6097192002-11-03 00:24:07 +0000602 {
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 }
wdenkbc01dd52004-01-02 16:05:07 +0000621#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200622 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000623}
624
wdenkc6097192002-11-03 00:24:07 +0000625#ifdef CONFIG_VIDEO
626/******************************************************
627 * Routines to display the Board information
628 * to the screen (since the VGA will be initialized as last,
629 * we must resend the infos)
630 */
631
632#ifdef CONFIG_CONSOLE_EXTRA_INFO
633extern GraphicDevice ctfb;
wdenk2c9b05d2003-09-10 22:30:53 +0000634extern int get_boot_mode(void);
wdenkc6097192002-11-03 00:24:07 +0000635
636void video_get_info_str (int line_number, char *info)
637{
638 /* init video info strings for graphic console */
Stefan Roeseedd73f22007-10-21 08:12:41 +0200639 PPC4xx_SYS_INFO sys_info;
wdenkc6097192002-11-03 00:24:07 +0000640 char rev;
wdenk7d076412003-05-23 11:38:58 +0000641 int i,boot;
wdenkc6097192002-11-03 00:24:07 +0000642 unsigned long pvr;
643 char buf[64];
Wolfgang Denk20591042008-10-19 02:35:49 +0200644 char buf1[32], buf2[32], buf3[32], buf4[32];
wdenk7d076412003-05-23 11:38:58 +0000645 char cpustr[16];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200646 char *s, *e, bc;
wdenkc6097192002-11-03 00:24:07 +0000647 switch (line_number)
648 {
649 case 2:
650 /* CPU and board infos */
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;
wdenk7d076412003-05-23 11:38:58 +0000658 case PVR_405GPR_RB: rev='B'; break;
wdenkc6097192002-11-03 00:24:07 +0000659 default: rev='?'; break;
660 }
wdenk7d076412003-05-23 11:38:58 +0000661 if(pvr==PVR_405GPR_RB)
662 sprintf(cpustr,"PPC405GPr %c",rev);
663 else
664 sprintf(cpustr,"PPC405GP %c",rev);
wdenkc6097192002-11-03 00:24:07 +0000665 /* Board info */
666 i=0;
667 s=getenv ("serial#");
668#ifdef CONFIG_PIP405
669 if (!s || strncmp (s, "PIP405", 6)) {
Ben Whitten34fd6c92015-12-30 13:05:58 +0000670 strcpy(buf,"### No HW ID - assuming PIP405");
wdenkc6097192002-11-03 00:24:07 +0000671 }
672#endif
Tom Rini45ae6732016-09-19 21:55:34 -0400673#if defined(CONFIG_TARGET_MIP405) || defined(CONFIG_TARGET_MIP405T)
wdenkc6097192002-11-03 00:24:07 +0000674 if (!s || strncmp (s, "MIP405", 6)) {
Ben Whitten34fd6c92015-12-30 13:05:58 +0000675 strcpy(buf,"### No HW ID - assuming MIP405");
wdenkc6097192002-11-03 00:24:07 +0000676 }
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 }
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200688 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000689 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000690 strcpy(&buf[i]," SN ");
wdenkc6097192002-11-03 00:24:07 +0000691 i+=4;
692 for (; s < e; ++s) {
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200693 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000694 }
695 buf[i++]=0;
696 }
Wolfgang Denk20591042008-10-19 02:35:49 +0200697 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
wdenk7d076412003-05-23 11:38:58 +0000698 buf, cpustr,
Wolfgang Denk20591042008-10-19 02:35:49 +0200699 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));
wdenkc6097192002-11-03 00:24:07 +0000703 return;
704 case 3:
705 /* Memory Info */
wdenk7d076412003-05-23 11:38:58 +0000706 boot = get_boot_mode();
wdenkc6097192002-11-03 00:24:07 +0000707 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,
wdenk7d076412003-05-23 11:38:58 +0000712 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
wdenkc6097192002-11-03 00:24:07 +0000713 ctfb.modeIdent);
714 return;
715 case 1:
Ben Whitten34fd6c92015-12-30 13:05:58 +0000716 strcpy(buf, CONFIG_IDENT_STRING);
wdenkc6097192002-11-03 00:24:07 +0000717 sprintf (info, " %s", &buf[1]);
718 return;
719 }
720 /* no more info lines */
721 *info = 0;
722 return;
723}
724#endif /* CONFIG_CONSOLE_EXTRA_INFO */
725
726#endif /* CONFIG_VIDEO */