blob: a61a98cf77fa5a4831fa2d090c6a2e7d92962cda [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 *
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>
wdenk1fe2c702003-03-06 21:55:29 +000030#include <asm/byteorder.h>
wdenkc6097192002-11-03 00:24:07 +000031#include <i2c.h>
wdenkc6097192002-11-03 00:24:07 +000032#include <pci.h>
wdenk4ea537d2003-12-07 18:32:37 +000033#include <malloc.h>
34#include <bzlib.h>
wdenkc6097192002-11-03 00:24:07 +000035
wdenk7d076412003-05-23 11:38:58 +000036#ifdef CONFIG_PIP405
37#include "../pip405/pip405.h"
Stefan Roeseca8725f2007-10-03 15:01:02 +020038#include <asm/4xx_pci.h>
wdenk7d076412003-05-23 11:38:58 +000039#endif
40#ifdef CONFIG_MIP405
41#include "../mip405/mip405.h"
Stefan Roeseca8725f2007-10-03 15:01:02 +020042#include <asm/4xx_pci.h>
wdenk7d076412003-05-23 11:38:58 +000043#endif
Wolfgang Denk6405a152006-03-31 18:32:53 +020044
45DECLARE_GLOBAL_DATA_PTR;
46
wdenkbc01dd52004-01-02 16:05:07 +000047#if defined(CONFIG_PATI)
48#define FIRM_START 0xFFF00000
49#endif
wdenk7d076412003-05-23 11:38:58 +000050
wdenk4ea537d2003-12-07 18:32:37 +000051extern int mem_test(ulong start, ulong ramsize, int quiet);
wdenkc6097192002-11-03 00:24:07 +000052
wdenk4ea537d2003-12-07 18:32:37 +000053#define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054#define IMAGE_SIZE CONFIG_SYS_MONITOR_LEN /* ugly, but it works for now */
wdenkc6097192002-11-03 00:24:07 +000055
David Müllerb4a931e2011-12-22 13:38:19 +010056#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405)
57/*-----------------------------------------------------------------------
58 * On PIP/MIP405 we have 3 (4) possible boot mode
59 *
60 * - Boot from Flash (Flash CS = CS0, MPS CS = CS1)
61 * - Boot from MPS (Flash CS = CS1, MPS CS = CS0)
62 * - Boot from PCI with Flash map (Flash CS = CS0, MPS CS = CS1)
63 * - Boot from PCI with MPS map (Flash CS = CS1, MPS CS = CS0)
64 * The flash init is the first board specific routine which is called
65 * after code relocation (running from SDRAM)
66 * The first thing we do is to map the Flash CS to the Flash area and
67 * the MPS CS to the MPS area. Since the flash size is unknown at this
68 * point, we use the max flash size and the lowest flash address as base.
69 *
70 * After flash detection we adjust the size of the CS area accordingly.
71 * update_flash_size() will fix in wrong values in the flash_info structure,
72 * misc_init_r() will fix the values in the board info structure
73 */
74int get_boot_mode(void)
75{
76 unsigned long pbcr;
77 int res = 0;
78 pbcr = mfdcr(CPC0_PSR);
79 if ((pbcr & PSR_ROM_WIDTH_MASK) == 0)
80 /* boot via MPS or MPS mapping */
81 res = BOOT_MPS;
82 if (pbcr & PSR_ROM_LOC)
83 /* boot via PCI.. */
84 res |= BOOT_PCI;
85 return res;
86}
87
88/* Map the flash high (in boot area)
89 This code can only be executed from SDRAM (after relocation).
90*/
91void setup_cs_reloc(void)
92{
93 int mode;
94 /*
95 * since we are relocated, we can set-up the CS finaly
96 * but first of all, switch off PCI mapping (in case it
97 * was a PCI boot)
98 */
99 out32r(PMM0MA, 0L);
100 /* get boot mode */
101 mode = get_boot_mode();
102 /*
103 * we map the flash high in every case
104 * first find out to which CS the flash is attached to
105 */
106 if (mode & BOOT_MPS) {
107 /* map flash high on CS1 and MPS on CS0 */
108 mtdcr(EBC0_CFGADDR, PB0AP);
109 mtdcr(EBC0_CFGDATA, MPS_AP);
110 mtdcr(EBC0_CFGADDR, PB0CR);
111 mtdcr(EBC0_CFGDATA, MPS_CR);
112 /*
113 * we use the default values (max values) for the flash
114 * because its real size is not yet known
115 */
116 mtdcr(EBC0_CFGADDR, PB1AP);
117 mtdcr(EBC0_CFGDATA, FLASH_AP);
118 mtdcr(EBC0_CFGADDR, PB1CR);
119 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
120 } else {
121 /* map flash high on CS0 and MPS on CS1 */
122 mtdcr(EBC0_CFGADDR, PB1AP);
123 mtdcr(EBC0_CFGDATA, MPS_AP);
124 mtdcr(EBC0_CFGADDR, PB1CR);
125 mtdcr(EBC0_CFGDATA, MPS_CR);
126 /*
127 * we use the default values (max values) for the flash
128 * because its real size is not yet known
129 */
130 mtdcr(EBC0_CFGADDR, PB0AP);
131 mtdcr(EBC0_CFGDATA, FLASH_AP);
132 mtdcr(EBC0_CFGADDR, PB0CR);
133 mtdcr(EBC0_CFGDATA, FLASH_CR_B);
134 }
135}
136#endif /* #if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) */
137
138#ifdef CONFIG_SYS_UPDATE_FLASH_SIZE
139/* adjust flash start and protection info */
140int update_flash_size(int flash_size)
141{
142 int i = 0, mode;
143 flash_info_t *info = &flash_info[0];
144 unsigned long flashcr;
145 unsigned long flash_base = (0 - flash_size) & 0xFFF00000;
146
147 if (flash_size > 128*1024*1024) {
148 printf("\n ### ERROR, wrong flash size: %X, reset board ###\n",
149 flash_size);
150 hang();
151 }
152
153 if ((flash_size >> 20) != 0)
154 i = __ilog2(flash_size >> 20);
155
156 /* set up flash CS according to the size */
157 mode = get_boot_mode();
158 if (mode & BOOT_MPS) {
159 /* flash is on CS1 */
160 mtdcr(EBC0_CFGADDR, PB1CR);
161 flashcr = mfdcr(EBC0_CFGDATA);
162 /* we map the flash high in every case */
163 flashcr &= 0x0001FFFF; /* mask out address bits */
164 flashcr |= flash_base; /* start addr */
165 flashcr |= (i << 17); /* size addr */
166 mtdcr(EBC0_CFGADDR, PB1CR);
167 mtdcr(EBC0_CFGDATA, flashcr);
168 } else {
169 /* flash is on CS0 */
170 mtdcr(EBC0_CFGADDR, PB0CR);
171 flashcr = mfdcr(EBC0_CFGDATA);
172 /* we map the flash high in every case */
173 flashcr &= 0x0001FFFF; /* mask out address bits */
174 flashcr |= flash_base; /* start addr */
175 flashcr |= (i << 17); /* size addr */
176 mtdcr(EBC0_CFGADDR, PB0CR);
177 mtdcr(EBC0_CFGDATA, flashcr);
178 }
179
180 for (i = 0; i < info->sector_count; i++)
181 /* adjust sector start address */
182 info->start[i] = flash_base +
183 (info->start[i] - CONFIG_SYS_FLASH_BASE);
184
185 /* unprotect all sectors */
186 flash_protect(FLAG_PROTECT_CLEAR,
187 info->start[0],
188 0xFFFFFFFF,
189 info);
190 flash_protect_default();
191 /* protect reset vector too*/
192 flash_protect(FLAG_PROTECT_SET,
193 info->start[info->sector_count-1],
194 0xFFFFFFFF,
195 info);
196
197 return 0;
198}
199#endif
wdenkc6097192002-11-03 00:24:07 +0000200
wdenk1ebf41e2004-01-02 14:00:00 +0000201static int
wdenk4ea537d2003-12-07 18:32:37 +0000202mpl_prg(uchar *src, ulong size)
wdenkc6097192002-11-03 00:24:07 +0000203{
wdenk4ea537d2003-12-07 18:32:37 +0000204 ulong start;
David Müllerb4a931e2011-12-22 13:38:19 +0100205 flash_info_t *info = &flash_info[0];
wdenk4ea537d2003-12-07 18:32:37 +0000206 int i, rc;
wdenkbc01dd52004-01-02 16:05:07 +0000207#if defined(CONFIG_PATI)
208 int start_sect;
209#endif
210#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
wdenke39c2842003-06-04 15:05:30 +0000211 char *copystr = (char *)src;
wdenk4ea537d2003-12-07 18:32:37 +0000212 ulong *magic = (ulong *)src;
wdenk6b58f332003-03-14 20:47:52 +0000213#endif
wdenkc6097192002-11-03 00:24:07 +0000214
wdenkbc01dd52004-01-02 16:05:07 +0000215#if defined(CONFIG_PIP405) || defined(CONFIG_MIP405) || defined(CONFIG_PATI)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100216 if (uimage_to_cpu (magic[0]) != IH_MAGIC) {
wdenk4ea537d2003-12-07 18:32:37 +0000217 puts("Bad Magic number\n");
wdenk1fe2c702003-03-06 21:55:29 +0000218 return -1;
219 }
wdenke39c2842003-06-04 15:05:30 +0000220 /* some more checks before we delete the Flash... */
221 /* Checking the ISO_STRING prevents to program a
222 * wrong Firmware Image into the flash.
223 */
wdenk4ea537d2003-12-07 18:32:37 +0000224 i = 4; /* skip Magic number */
225 while (1) {
226 if (strncmp(&copystr[i], "MEV-", 4) == 0)
wdenke39c2842003-06-04 15:05:30 +0000227 break;
wdenk4ea537d2003-12-07 18:32:37 +0000228 if (i++ >= 0x100) {
229 puts("Firmware Image for unknown Target\n");
wdenke39c2842003-06-04 15:05:30 +0000230 return -1;
231 }
232 }
233 /* we have the ISO STRING, check */
wdenk4ea537d2003-12-07 18:32:37 +0000234 if (strncmp(&copystr[i], CONFIG_ISO_STRING, sizeof(CONFIG_ISO_STRING)-1) != 0) {
235 printf("Wrong Firmware Image: %s\n", &copystr[i]);
wdenke39c2842003-06-04 15:05:30 +0000236 return -1;
237 }
wdenkbc01dd52004-01-02 16:05:07 +0000238#if !defined(CONFIG_PATI)
wdenke39c2842003-06-04 15:05:30 +0000239 start = 0 - size;
David Müllerb4a931e2011-12-22 13:38:19 +0100240
241 /* unprotect sectors used by u-boot */
242 flash_protect(FLAG_PROTECT_CLEAR,
243 start,
244 0xFFFFFFFF,
245 info);
246
247 /* search start sector */
248 for (i = info->sector_count-1; i > 0; i--)
wdenk4ea537d2003-12-07 18:32:37 +0000249 if (start >= info->start[i])
250 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100251
wdenkc6097192002-11-03 00:24:07 +0000252 /* now erase flash */
wdenkc6097192002-11-03 00:24:07 +0000253 printf("Erasing at %lx (sector %d) (start %lx)\n",
254 start,i,info->start[i]);
wdenk4ea537d2003-12-07 18:32:37 +0000255 if ((rc = flash_erase (info, i, info->sector_count-1)) != 0) {
256 puts("ERROR ");
257 flash_perror(rc);
258 return (1);
259 }
wdenk1fe2c702003-03-06 21:55:29 +0000260
wdenkbc01dd52004-01-02 16:05:07 +0000261#else /* #if !defined(CONFIG_PATI */
262 start = FIRM_START;
263 start_sect = -1;
David Müllerb4a931e2011-12-22 13:38:19 +0100264
265 /* search start sector */
266 for (i = info->sector_count-1; i > 0; i--)
267 if (start >= info->start[i])
wdenkbc01dd52004-01-02 16:05:07 +0000268 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100269
270 start_sect = i;
wdenkbc01dd52004-01-02 16:05:07 +0000271
David Müllerb4a931e2011-12-22 13:38:19 +0100272 for (i = info->sector_count-1; i > 0; i--)
273 if ((start + size) >= info->start[i])
wdenkbc01dd52004-01-02 16:05:07 +0000274 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100275
276 /* unprotect sectors used by u-boot */
277 flash_protect(FLAG_PROTECT_CLEAR,
278 start,
279 start + size,
280 info);
wdenkbc01dd52004-01-02 16:05:07 +0000281
wdenkbc01dd52004-01-02 16:05:07 +0000282 /* now erase flash */
283 printf ("Erasing at %lx to %lx (sector %d to %d) (%lx to %lx)\n",
284 start, start + size, start_sect, i,
285 info->start[start_sect], info->start[i]);
286 if ((rc = flash_erase (info, start_sect, i)) != 0) {
287 puts ("ERROR ");
288 flash_perror (rc);
289 return (1);
290 }
291#endif /* defined(CONFIG_PATI) */
292
wdenk1fe2c702003-03-06 21:55:29 +0000293#elif defined(CONFIG_VCMA9)
294 start = 0;
David Müllerb4a931e2011-12-22 13:38:19 +0100295
296 /* search end sector */
297 for (i = 0; i < info->sector_count; i++)
wdenk1fe2c702003-03-06 21:55:29 +0000298 if (size < info->start[i])
299 break;
David Müllerb4a931e2011-12-22 13:38:19 +0100300
301 flash_protect(FLAG_PROTECT_CLEAR,
302 start,
303 size,
304 info);
305
wdenk1fe2c702003-03-06 21:55:29 +0000306 /* now erase flash */
307 printf("Erasing at %lx (sector %d) (start %lx)\n",
308 start,0,info->start[0]);
wdenk4ea537d2003-12-07 18:32:37 +0000309 if ((rc = flash_erase (info, 0, i)) != 0) {
310 puts("ERROR ");
311 flash_perror(rc);
312 return (1);
313 }
wdenk1fe2c702003-03-06 21:55:29 +0000314
315#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000316 printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",
317 (ulong)src, size);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200318 if ((rc = flash_write ((char *)src, start, size)) != 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000319 puts("ERROR ");
320 flash_perror(rc);
wdenkc6097192002-11-03 00:24:07 +0000321 return (1);
322 }
wdenk4ea537d2003-12-07 18:32:37 +0000323 puts("OK programming done\n");
wdenkc6097192002-11-03 00:24:07 +0000324 return 0;
325}
326
327
wdenk1ebf41e2004-01-02 14:00:00 +0000328static int
wdenk4ea537d2003-12-07 18:32:37 +0000329mpl_prg_image(uchar *ld_addr)
wdenkc6097192002-11-03 00:24:07 +0000330{
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100331 unsigned long len;
wdenk4ea537d2003-12-07 18:32:37 +0000332 uchar *data;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100333 image_header_t *hdr = (image_header_t *)ld_addr;
wdenk4ea537d2003-12-07 18:32:37 +0000334 int rc;
wdenk1ebf41e2004-01-02 14:00:00 +0000335
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100336#if defined(CONFIG_FIT)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100337 if (genimg_get_format ((void *)hdr) != IMAGE_FORMAT_LEGACY) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100338 puts ("Non legacy image format not supported\n");
339 return -1;
340 }
341#endif
342
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100343 if (!image_check_magic (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000344 puts("Bad Magic Number\n");
wdenkc6097192002-11-03 00:24:07 +0000345 return 1;
346 }
Marian Balakowicza1cc1472008-02-21 17:27:41 +0100347 image_print_contents (hdr);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100348 if (!image_check_os (hdr, IH_OS_U_BOOT)) {
wdenk4ea537d2003-12-07 18:32:37 +0000349 puts("No U-Boot Image\n");
wdenkc6097192002-11-03 00:24:07 +0000350 return 1;
351 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100352 if (!image_check_type (hdr, IH_TYPE_FIRMWARE)) {
wdenk4ea537d2003-12-07 18:32:37 +0000353 puts("No Firmware Image\n");
wdenkc6097192002-11-03 00:24:07 +0000354 return 1;
355 }
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100356 if (!image_check_hcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000357 puts("Bad Header Checksum\n");
wdenkc6097192002-11-03 00:24:07 +0000358 return 1;
359 }
wdenk4ea537d2003-12-07 18:32:37 +0000360 puts("Verifying Checksum ... ");
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100361 if (!image_check_dcrc (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000362 puts("Bad Data CRC\n");
wdenkc6097192002-11-03 00:24:07 +0000363 return 1;
364 }
wdenk4ea537d2003-12-07 18:32:37 +0000365 puts("OK\n");
366
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100367 data = (uchar *)image_get_data (hdr);
368 len = image_get_data_size (hdr);
369
370 if (image_get_comp (hdr) != IH_COMP_NONE) {
wdenk4ea537d2003-12-07 18:32:37 +0000371 uchar *buf;
372 /* reserve space for uncompressed image */
373 if ((buf = malloc(IMAGE_SIZE)) == NULL) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200374 puts("Insufficient space for decompression\n");
wdenkc6097192002-11-03 00:24:07 +0000375 return 1;
376 }
wdenk1ebf41e2004-01-02 14:00:00 +0000377
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100378 switch (image_get_comp (hdr)) {
wdenk4ea537d2003-12-07 18:32:37 +0000379 case IH_COMP_GZIP:
380 puts("Uncompressing (GZIP) ... ");
wdenka0ebde52004-09-08 22:03:11 +0000381 rc = gunzip ((void *)(buf), IMAGE_SIZE, data, &len);
wdenk4ea537d2003-12-07 18:32:37 +0000382 if (rc != 0) {
383 puts("GUNZIP ERROR\n");
384 free(buf);
385 return 1;
386 }
387 puts("OK\n");
388 break;
wdenkc35ba4e2004-03-14 22:25:36 +0000389#ifdef CONFIG_BZIP2
wdenk4ea537d2003-12-07 18:32:37 +0000390 case IH_COMP_BZIP2:
391 puts("Uncompressing (BZIP2) ... ");
392 {
393 uint retlen = IMAGE_SIZE;
394 rc = BZ2_bzBuffToBuffDecompress ((char *)(buf), &retlen,
395 (char *)data, len, 0, 0);
396 len = retlen;
397 }
398 if (rc != BZ_OK) {
399 printf ("BUNZIP2 ERROR: %d\n", rc);
400 free(buf);
401 return 1;
402 }
403 puts("OK\n");
404 break;
405#endif
406 default:
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100407 printf ("Unimplemented compression type %d\n",
408 image_get_comp (hdr));
wdenk4ea537d2003-12-07 18:32:37 +0000409 free(buf);
410 return 1;
411 }
wdenk1ebf41e2004-01-02 14:00:00 +0000412
wdenk4ea537d2003-12-07 18:32:37 +0000413 rc = mpl_prg(buf, len);
414 free(buf);
415 } else {
416 rc = mpl_prg(data, len);
wdenkc6097192002-11-03 00:24:07 +0000417 }
wdenk1ebf41e2004-01-02 14:00:00 +0000418
wdenk4ea537d2003-12-07 18:32:37 +0000419 return(rc);
wdenkc6097192002-11-03 00:24:07 +0000420}
421
wdenkbc01dd52004-01-02 16:05:07 +0000422#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000423void get_backup_values(backup_t *buf)
424{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200425 i2c_read(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)buf,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000426}
427
428void set_backup_values(int overwrite)
429{
430 backup_t back;
431 int i;
432
433 get_backup_values(&back);
434 if(!overwrite) {
435 if(strncmp(back.signature,"MPL\0",4)==0) {
wdenk4ea537d2003-12-07 18:32:37 +0000436 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000437 return;
438 }
439 }
440 memcpy(back.signature,"MPL\0",4);
Wolfgang Denk76af2782010-07-24 21:55:43 +0200441 i = getenv_f("serial#",back.serial_name,16);
wdenk1fe2c702003-03-06 21:55:29 +0000442 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000443 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000444 return;
445 }
446 back.serial_name[16]=0;
Wolfgang Denk76af2782010-07-24 21:55:43 +0200447 i = getenv_f("ethaddr",back.eth_addr,20);
wdenk1fe2c702003-03-06 21:55:29 +0000448 if(i < 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000449 puts("Not possible to write Backup\n");
wdenkc6097192002-11-03 00:24:07 +0000450 return;
451 }
452 back.eth_addr[20]=0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200453 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
wdenkc6097192002-11-03 00:24:07 +0000454}
455
456void clear_env_values(void)
457{
458 backup_t back;
459 unsigned char env_crc[4];
460
461 memset(&back,0xff,sizeof(backup_t));
462 memset(env_crc,0x00,4);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200463 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,2,(void *)&back,sizeof(backup_t));
464 i2c_write(CONFIG_SYS_DEF_EEPROM_ADDR,CONFIG_ENV_OFFSET,2,(void *)env_crc,4);
wdenkc6097192002-11-03 00:24:07 +0000465}
466
467/*
468 * check crc of "older" environment
469 */
470int check_env_old_size(ulong oldsize)
471{
472 ulong crc, len, new;
473 unsigned off;
474 uchar buf[64];
475
476 /* read old CRC */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200477 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200478 CONFIG_ENV_OFFSET,
wdenkc6097192002-11-03 00:24:07 +0000479 (uchar *)&crc, sizeof(ulong));
480
481 new = 0;
482 len = oldsize;
483 off = sizeof(long);
484 len = oldsize-off;
485 while (len > 0) {
486 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
487
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200488 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, buf, n);
wdenkc6097192002-11-03 00:24:07 +0000489 new = crc32 (new, buf, n);
490 len -= n;
491 off += n;
492 }
493
494 return (crc == new);
495}
496
497static ulong oldsizes[] = {
498 0x200,
499 0x800,
500 0
501};
502
503void copy_old_env(ulong size)
504{
505 uchar name_buf[64];
506 uchar value_buf[0x800];
507 uchar c;
508 ulong len;
509 unsigned off;
510 uchar *name, *value;
511
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200512 name = &name_buf[0];
513 value = &value_buf[0];
wdenkc6097192002-11-03 00:24:07 +0000514 len=size;
515 off = sizeof(long);
516 while (len > off) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200517 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000518 if(c != '=') {
519 *name++=c;
520 off++;
521 }
522 else {
523 *name++='\0';
524 off++;
525 do {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200526 eeprom_read (CONFIG_SYS_DEF_EEPROM_ADDR, CONFIG_ENV_OFFSET+off, &c, 1);
wdenkc6097192002-11-03 00:24:07 +0000527 *value++=c;
528 off++;
529 if(c == '\0')
530 break;
531 } while(len > off);
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200532 name = &name_buf[0];
533 value = &value_buf[0];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200534 if(strncmp((char *)name,"baudrate",8)!=0) {
535 setenv((char *)name,(char *)value);
wdenkc6097192002-11-03 00:24:07 +0000536 }
537
538 }
539 }
540}
541
542
543void check_env(void)
544{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200545 char *s;
wdenkc6097192002-11-03 00:24:07 +0000546 int i=0;
547 char buf[32];
548 backup_t back;
549
550 s=getenv("serial#");
551 if(!s) {
552 while(oldsizes[i]) {
553 if(check_env_old_size(oldsizes[i]))
554 break;
555 i++;
556 }
557 if(!oldsizes[i]) {
558 /* no old environment has been found */
559 get_backup_values (&back);
560 if (strncmp (back.signature, "MPL\0", 4) == 0) {
561 sprintf (buf, "%s", back.serial_name);
562 setenv ("serial#", buf);
563 sprintf (buf, "%s", back.eth_addr);
564 setenv ("ethaddr", buf);
565 printf ("INFO: serial# and ethaddr recovered, use saveenv\n");
566 return;
567 }
568 }
569 else {
570 copy_old_env(oldsizes[i]);
wdenk4ea537d2003-12-07 18:32:37 +0000571 puts("INFO: old environment ajusted, use saveenv\n");
wdenkc6097192002-11-03 00:24:07 +0000572 }
573 }
574 else {
575 /* check if back up is set */
576 get_backup_values(&back);
577 if(strncmp(back.signature,"MPL\0",4)!=0) {
578 set_backup_values(0);
579 }
580 }
581}
582
wdenkbc01dd52004-01-02 16:05:07 +0000583#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000584
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200585int do_mplcommon(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000586{
Peter Tysere9b1b492010-10-05 23:48:47 -0500587 ulong ld_addr;
wdenkc6097192002-11-03 00:24:07 +0000588 int result;
wdenkbc01dd52004-01-02 16:05:07 +0000589#if !defined(CONFIG_PATI)
Peter Tysere9b1b492010-10-05 23:48:47 -0500590 ulong size = IMAGE_SIZE;
591 ulong src = MULTI_PURPOSE_SOCKET_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000592 backup_t back;
wdenkbc01dd52004-01-02 16:05:07 +0000593#endif
wdenkc6097192002-11-03 00:24:07 +0000594
595 if (strcmp(argv[1], "flash") == 0)
596 {
Jon Loeliger145318c2007-07-09 18:38:39 -0500597#if defined(CONFIG_CMD_FDC)
wdenkc6097192002-11-03 00:24:07 +0000598 if (strcmp(argv[2], "floppy") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200599 char *local_args[3];
wdenkc6097192002-11-03 00:24:07 +0000600 extern int do_fdcboot (cmd_tbl_t *, int, int, char *[]);
wdenk4ea537d2003-12-07 18:32:37 +0000601 puts("\nupdating bootloader image from floppy\n");
wdenkc6097192002-11-03 00:24:07 +0000602 local_args[0] = argv[0];
Wolfgang Denka1be4762008-05-20 16:00:29 +0200603 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000604 local_args[1] = argv[3];
605 local_args[2] = NULL;
606 ld_addr=simple_strtoul(argv[3], NULL, 16);
607 result=do_fdcboot(cmdtp, 0, 2, local_args);
608 }
609 else {
610 local_args[1] = NULL;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200611 ld_addr=CONFIG_SYS_LOAD_ADDR;
wdenkc6097192002-11-03 00:24:07 +0000612 result=do_fdcboot(cmdtp, 0, 1, local_args);
613 }
wdenk1ebf41e2004-01-02 14:00:00 +0000614 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000615 return result;
616 }
Jon Loeliger145318c2007-07-09 18:38:39 -0500617#endif
wdenkc6097192002-11-03 00:24:07 +0000618 if (strcmp(argv[2], "mem") == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200619 if(argc==4) {
wdenkc6097192002-11-03 00:24:07 +0000620 ld_addr=simple_strtoul(argv[3], NULL, 16);
621 }
622 else {
623 ld_addr=load_addr;
624 }
625 printf ("\nupdating bootloader image from memory at %lX\n",ld_addr);
wdenk4ea537d2003-12-07 18:32:37 +0000626 result=mpl_prg_image((uchar *)ld_addr);
wdenkc6097192002-11-03 00:24:07 +0000627 return result;
628 }
wdenkbc01dd52004-01-02 16:05:07 +0000629#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000630 if (strcmp(argv[2], "mps") == 0) {
wdenk4ea537d2003-12-07 18:32:37 +0000631 puts("\nupdating bootloader image from MPS\n");
632 result=mpl_prg((uchar *)src,size);
wdenkc6097192002-11-03 00:24:07 +0000633 return result;
634 }
wdenkbc01dd52004-01-02 16:05:07 +0000635#endif /* #if !defined(CONFIG_PATI) */
wdenkc6097192002-11-03 00:24:07 +0000636 }
wdenkbc01dd52004-01-02 16:05:07 +0000637#if !defined(CONFIG_PATI)
wdenkc6097192002-11-03 00:24:07 +0000638 if (strcmp(argv[1], "clearenvvalues") == 0)
639 {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200640 if (strcmp(argv[2], "yes") == 0)
wdenkc6097192002-11-03 00:24:07 +0000641 {
642 clear_env_values();
643 return 0;
644 }
645 }
646 if (strcmp(argv[1], "getback") == 0) {
647 get_backup_values(&back);
648 back.signature[3]=0;
649 back.serial_name[16]=0;
650 back.eth_addr[20]=0;
651 printf("GetBackUp: signature: %s\n",back.signature);
652 printf(" serial#: %s\n",back.serial_name);
653 printf(" ethaddr: %s\n",back.eth_addr);
654 return 0;
655 }
656 if (strcmp(argv[1], "setback") == 0) {
657 set_backup_values(1);
658 return 0;
659 }
wdenkbc01dd52004-01-02 16:05:07 +0000660#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200661 return cmd_usage(cmdtp);
wdenkc6097192002-11-03 00:24:07 +0000662}
663
664
Jon Loeliger145318c2007-07-09 18:38:39 -0500665#if defined(CONFIG_CMD_DOC)
wdenkc6097192002-11-03 00:24:07 +0000666void doc_init (void)
667{
668 doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
669}
670#endif
671
672
673#ifdef CONFIG_VIDEO
674/******************************************************
675 * Routines to display the Board information
676 * to the screen (since the VGA will be initialized as last,
677 * we must resend the infos)
678 */
679
680#ifdef CONFIG_CONSOLE_EXTRA_INFO
681extern GraphicDevice ctfb;
wdenk2c9b05d2003-09-10 22:30:53 +0000682extern int get_boot_mode(void);
wdenkc6097192002-11-03 00:24:07 +0000683
684void video_get_info_str (int line_number, char *info)
685{
686 /* init video info strings for graphic console */
Stefan Roeseedd73f22007-10-21 08:12:41 +0200687 PPC4xx_SYS_INFO sys_info;
wdenkc6097192002-11-03 00:24:07 +0000688 char rev;
wdenk7d076412003-05-23 11:38:58 +0000689 int i,boot;
wdenkc6097192002-11-03 00:24:07 +0000690 unsigned long pvr;
691 char buf[64];
Wolfgang Denk20591042008-10-19 02:35:49 +0200692 char buf1[32], buf2[32], buf3[32], buf4[32];
wdenk7d076412003-05-23 11:38:58 +0000693 char cpustr[16];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200694 char *s, *e, bc;
wdenkc6097192002-11-03 00:24:07 +0000695 switch (line_number)
696 {
697 case 2:
698 /* CPU and board infos */
699 pvr=get_pvr();
700 get_sys_info (&sys_info);
701 switch (pvr) {
702 case PVR_405GP_RB: rev='B'; break;
703 case PVR_405GP_RC: rev='C'; break;
704 case PVR_405GP_RD: rev='D'; break;
705 case PVR_405GP_RE: rev='E'; break;
wdenk7d076412003-05-23 11:38:58 +0000706 case PVR_405GPR_RB: rev='B'; break;
wdenkc6097192002-11-03 00:24:07 +0000707 default: rev='?'; break;
708 }
wdenk7d076412003-05-23 11:38:58 +0000709 if(pvr==PVR_405GPR_RB)
710 sprintf(cpustr,"PPC405GPr %c",rev);
711 else
712 sprintf(cpustr,"PPC405GP %c",rev);
wdenkc6097192002-11-03 00:24:07 +0000713 /* Board info */
714 i=0;
715 s=getenv ("serial#");
716#ifdef CONFIG_PIP405
717 if (!s || strncmp (s, "PIP405", 6)) {
718 sprintf(buf,"### No HW ID - assuming PIP405");
719 }
720#endif
721#ifdef CONFIG_MIP405
722 if (!s || strncmp (s, "MIP405", 6)) {
723 sprintf(buf,"### No HW ID - assuming MIP405");
724 }
725#endif
726 else {
727 for (e = s; *e; ++e) {
728 if (*e == ' ')
729 break;
730 }
731 for (; s < e; ++s) {
732 if (*s == '_') {
733 ++s;
734 break;
735 }
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200736 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000737 }
738 sprintf(&buf[i]," SN ");
739 i+=4;
740 for (; s < e; ++s) {
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200741 buf[i++] = *s;
wdenkc6097192002-11-03 00:24:07 +0000742 }
743 buf[i++]=0;
744 }
Wolfgang Denk20591042008-10-19 02:35:49 +0200745 sprintf (info," %s %s %s MHz (%s/%s/%s MHz)",
wdenk7d076412003-05-23 11:38:58 +0000746 buf, cpustr,
Wolfgang Denk20591042008-10-19 02:35:49 +0200747 strmhz (buf1, gd->cpu_clk),
748 strmhz (buf2, sys_info.freqPLB),
749 strmhz (buf3, sys_info.freqPLB / sys_info.pllOpbDiv),
750 strmhz (buf4, sys_info.freqPLB / sys_info.pllExtBusDiv));
wdenkc6097192002-11-03 00:24:07 +0000751 return;
752 case 3:
753 /* Memory Info */
wdenk7d076412003-05-23 11:38:58 +0000754 boot = get_boot_mode();
wdenkc6097192002-11-03 00:24:07 +0000755 bc = in8 (CONFIG_PORT_ADDR);
756 sprintf(info, " %luMB RAM, %luMB Flash Cfg 0x%02X %s %s",
757 gd->bd->bi_memsize / 0x100000,
758 gd->bd->bi_flashsize / 0x100000,
759 bc,
wdenk7d076412003-05-23 11:38:58 +0000760 (boot & BOOT_MPS) ? "MPS boot" : "Flash boot",
wdenkc6097192002-11-03 00:24:07 +0000761 ctfb.modeIdent);
762 return;
763 case 1:
764 sprintf (buf, "%s",CONFIG_IDENT_STRING);
765 sprintf (info, " %s", &buf[1]);
766 return;
767 }
768 /* no more info lines */
769 *info = 0;
770 return;
771}
772#endif /* CONFIG_CONSOLE_EXTRA_INFO */
773
774#endif /* CONFIG_VIDEO */