blob: 18cb85faf3371797bb18a10ee1fab9427ade3deb [file] [log] [blame]
Stefan Roese779e9752007-08-14 14:44:41 +02001/*
2 * (C) Copyright 2007
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
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#include <common.h>
25#include <command.h>
26#include <malloc.h>
27#include <environment.h>
28#include <logbuff.h>
29#include <post.h>
30
31#include <asm/processor.h>
32#include <asm/io.h>
Stefan Roese09887762010-09-16 14:30:37 +020033#include <asm/ppc4xx-gpio.h>
Stefan Roese779e9752007-08-14 14:44:41 +020034
35DECLARE_GLOBAL_DATA_PTR;
36
37#define REBOOT_MAGIC 0x07081967
38#define REBOOT_NOP 0x00000000
39#define REBOOT_DO_POST 0x00000001
40
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020041extern flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
Stefan Roese779e9752007-08-14 14:44:41 +020042extern env_t *env_ptr;
43extern uchar default_environment[];
44
45ulong flash_get_size(ulong base, int banknum);
46void env_crc_update(void);
Stefan Roese779e9752007-08-14 14:44:41 +020047
48static u32 start_time;
49
50int board_early_init_f(void)
51{
Stefan Roese952e7762009-09-24 09:55:50 +020052 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
53 mtdcr(UIC0ER, 0x00000000); /* disable all ints */
54 mtdcr(UIC0CR, 0x00000000);
55 mtdcr(UIC0PR, 0xFFFF7F00); /* set int polarities */
56 mtdcr(UIC0TR, 0x00000000); /* set int trigger levels */
57 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
58 mtdcr(UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
Stefan Roese779e9752007-08-14 14:44:41 +020059
60 /*
61 * Configure CPC0_PCI to enable PerWE as output
62 */
Stefan Roesed1c3b272009-09-09 16:25:29 +020063 mtdcr(CPC0_PCI, CPC0_PCI_SPE);
Stefan Roese779e9752007-08-14 14:44:41 +020064
65 return 0;
66}
67
68int misc_init_r(void)
69{
70 u32 pbcr;
71 int size_val = 0;
72 u32 post_magic;
73 u32 post_val;
74
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020075 post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
76 post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
Stefan Roese779e9752007-08-14 14:44:41 +020077 if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST)) {
78 /*
79 * Set special bootline bootparameter to pass this POST boot
80 * mode to Linux to reset the username/password
81 */
82 setenv("addmisc", "setenv bootargs \\${bootargs} factory_reset=yes");
83
84 /*
85 * Normally don't run POST tests, only when enabled
86 * via the sw-reset button. So disable further tests
87 * upon next bootup here.
88 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_NOP);
Stefan Roese779e9752007-08-14 14:44:41 +020090 } else {
91 /*
92 * Only run POST when initiated via the sw-reset button mechanism
93 */
94 post_word_store(0);
95 }
96
97 /*
98 * Get current time
99 */
100 start_time = get_timer(0);
101
102 /*
103 * FLASH stuff...
104 */
105
106 /* Re-do sizing to get full correct info */
107
108 /* adjust flash start and offset */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200109 mfebc(PB0CR, pbcr);
Stefan Roese779e9752007-08-14 14:44:41 +0200110 switch (gd->bd->bi_flashsize) {
111 case 1 << 20:
112 size_val = 0;
113 break;
114 case 2 << 20:
115 size_val = 1;
116 break;
117 case 4 << 20:
118 size_val = 2;
119 break;
120 case 8 << 20:
121 size_val = 3;
122 break;
123 case 16 << 20:
124 size_val = 4;
125 break;
126 case 32 << 20:
127 size_val = 5;
128 break;
129 case 64 << 20:
130 size_val = 6;
131 break;
132 case 128 << 20:
133 size_val = 7;
134 break;
135 }
136 pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200137 mtebc(PB0CR, pbcr);
Stefan Roese779e9752007-08-14 14:44:41 +0200138
139 /*
140 * Re-check to get correct base address
141 */
142 flash_get_size(gd->bd->bi_flashstart, 0);
143
144 /* Monitor protection ON by default */
145 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200146 -CONFIG_SYS_MONITOR_LEN,
Stefan Roese779e9752007-08-14 14:44:41 +0200147 0xffffffff,
148 &flash_info[0]);
149
150 /* Env protection ON by default */
151 (void)flash_protect(FLAG_PROTECT_SET,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200152 CONFIG_ENV_ADDR_REDUND,
153 CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
Stefan Roese779e9752007-08-14 14:44:41 +0200154 &flash_info[0]);
155
156 return 0;
157}
158
159/*
160 * Check Board Identity:
161 */
162int checkboard(void)
163{
Wolfgang Denkf0c0b3a2011-05-04 10:32:28 +0000164 char buf[64];
165 int i = getenv_f("serial#", buf, sizeof(buf));
Stefan Roese779e9752007-08-14 14:44:41 +0200166
167 puts("Board: Zeus-");
168
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200169 if (in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_ZEUS_PE))
Stefan Roese779e9752007-08-14 14:44:41 +0200170 puts("PE");
171 else
172 puts("CE");
173
174 puts(" of BulletEndPoint");
175
Wolfgang Denkf0c0b3a2011-05-04 10:32:28 +0000176 if (i > 0) {
Stefan Roese779e9752007-08-14 14:44:41 +0200177 puts(", serial# ");
Wolfgang Denkf0c0b3a2011-05-04 10:32:28 +0000178 puts(buf);
Stefan Roese779e9752007-08-14 14:44:41 +0200179 }
180 putc('\n');
181
182 /* both LED's off */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200183 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
184 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
Stefan Roese779e9752007-08-14 14:44:41 +0200185 udelay(10000);
186 /* and on again */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200187 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 1);
188 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 1);
Stefan Roese779e9752007-08-14 14:44:41 +0200189
190 return (0);
191}
192
Stefan Roese779e9752007-08-14 14:44:41 +0200193static int default_env_var(char *buf, char *var)
194{
195 char *ptr;
196 char *val;
197
198 /*
199 * Find env variable
200 */
201 ptr = strstr(buf + 4, var);
202 if (ptr == NULL) {
203 printf("ERROR: %s not found!\n", var);
204 return -1;
205 }
206 ptr += strlen(var) + 1;
207
208 /*
209 * Now the ethaddr needs to be updated in the "normal"
210 * environment storage -> redundant flash.
211 */
212 val = ptr;
213 setenv(var, val);
214 printf("Updated %s from eeprom to %s!\n", var, val);
215
216 return 0;
217}
218
219static int restore_default(void)
220{
221 char *buf;
222 char *buf_save;
223 u32 crc;
224
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200225 set_default_env("");
Stefan Roese779e9752007-08-14 14:44:41 +0200226
Stefan Roese779e9752007-08-14 14:44:41 +0200227 gd->env_valid = 1;
228
229 /*
230 * Read board specific values from I2C EEPROM
231 * and set env variables accordingly
232 * -> ethaddr, eth1addr, serial#
233 */
234 buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 if (buf == NULL) {
236 printf("ERROR: malloc() failed\n");
237 return -1;
238 }
Stefan Roese779e9752007-08-14 14:44:41 +0200239 if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
240 (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
241 puts("\nError reading EEPROM!\n");
242 } else {
243 crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
244 if (crc != *(u32 *)buf) {
Stefan Roeseb0021442008-07-10 09:58:06 +0200245 printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
Stefan Roese779e9752007-08-14 14:44:41 +0200246 return -1;
247 }
248
249 default_env_var(buf, "ethaddr");
250 buf += 8 + 18;
251 default_env_var(buf, "eth1addr");
252 buf += 9 + 18;
253 default_env_var(buf, "serial#");
254 }
255
256 /*
257 * Finally save updated env variables back to flash
258 */
259 saveenv();
260
261 free(buf_save);
262
263 return 0;
264}
265
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200266int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roese779e9752007-08-14 14:44:41 +0200267{
268 char *buf;
269 char *buf_save;
270 char str[32];
271 u32 crc;
272 char var[32];
273
274 if (argc < 4) {
275 puts("ERROR!\n");
276 return -1;
277 }
278
279 buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
280 memset(buf, 0, FACTORY_RESET_ENV_SIZE);
281
282 strcpy(var, "ethaddr");
283 printf("Setting %s to %s\n", var, argv[1]);
284 sprintf(str, "%s=%s", var, argv[1]);
285 strcpy(buf + 4, str);
286 buf += strlen(str) + 1;
287
288 strcpy(var, "eth1addr");
289 printf("Setting %s to %s\n", var, argv[2]);
290 sprintf(str, "%s=%s", var, argv[2]);
291 strcpy(buf + 4, str);
292 buf += strlen(str) + 1;
293
294 strcpy(var, "serial#");
295 printf("Setting %s to %s\n", var, argv[3]);
296 sprintf(str, "%s=%s", var, argv[3]);
297 strcpy(buf + 4, str);
298
299 crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
300 *(u32 *)buf_save = crc;
301
302 if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
303 (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
304 puts("\nError writing EEPROM!\n");
305 return -1;
306 }
307
308 free(buf_save);
309
310 return 0;
311}
312
313U_BOOT_CMD(
314 setdef, 4, 1, do_set_default,
Peter Tyser2fb26042009-01-27 18:03:12 -0600315 "write board-specific values to EEPROM (ethaddr...)",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200316 "ethaddr eth1addr serial#\n - write board-specific values to EEPROM"
Stefan Roese779e9752007-08-14 14:44:41 +0200317 );
318
319static inline int sw_reset_pressed(void)
320{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200321 return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
Stefan Roese779e9752007-08-14 14:44:41 +0200322}
323
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200324int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char * const argv[])
Stefan Roese779e9752007-08-14 14:44:41 +0200325{
326 int delta;
327 int count = 0;
328 int post = 0;
329 int factory_reset = 0;
330
331 if (!sw_reset_pressed()) {
332 printf("SW-Reset already high (Button released)\n");
333 printf("-> No action taken!\n");
334 return 0;
335 }
336
337 printf("Waiting for SW-Reset button to be released.");
338
339 while (1) {
340 delta = get_timer(start_time);
341 if (!sw_reset_pressed())
342 break;
343
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200344 if ((delta > CONFIG_SYS_TIME_POST) && !post) {
Stefan Roese779e9752007-08-14 14:44:41 +0200345 printf("\nWhen released now, POST tests will be started.");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200346 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
Stefan Roese779e9752007-08-14 14:44:41 +0200347 post = 1;
348 }
349
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200350 if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
Stefan Roese779e9752007-08-14 14:44:41 +0200351 printf("\nWhen released now, factory default values"
352 " will be restored.");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200353 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
Stefan Roese779e9752007-08-14 14:44:41 +0200354 factory_reset = 1;
355 }
356
357 udelay(1000);
358 if (!(count++ % 1000))
359 printf(".");
360 }
361
362
363 printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
364
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200365 if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
Stefan Roese779e9752007-08-14 14:44:41 +0200366 printf("Starting factory reset value restoration...\n");
367
368 /*
369 * Restore default setting
370 */
371 restore_default();
372
373 /*
374 * Reset the board for default to become valid
375 */
376 do_reset(NULL, 0, 0, NULL);
377
378 return 0;
379 }
380
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200381 if (delta > CONFIG_SYS_TIME_POST) {
Stefan Roese779e9752007-08-14 14:44:41 +0200382 printf("Starting POST configuration...\n");
383
384 /*
385 * Enable POST upon next bootup
386 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200387 out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
388 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
Stefan Roese779e9752007-08-14 14:44:41 +0200389 post_bootmode_init();
390
391 /*
392 * Reset the logbuffer for a clean start
393 */
394 logbuff_reset();
395
396 do_reset(NULL, 0, 0, NULL);
397
398 return 0;
399 }
400
401 return 0;
402}
403
404U_BOOT_CMD (
405 chkreset, 1, 1, do_chkreset,
Peter Tyser2fb26042009-01-27 18:03:12 -0600406 "Check for status of SW-reset button and act accordingly",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200407 ""
Stefan Roese779e9752007-08-14 14:44:41 +0200408);
409
410#if defined(CONFIG_POST)
411/*
412 * Returns 1 if keys pressed to start the power-on long-running tests
413 * Called from board_init_f().
414 */
415int post_hotkeys_pressed(void)
416{
417 u32 post_magic;
418 u32 post_val;
419
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200420 post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
421 post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
Stefan Roese779e9752007-08-14 14:44:41 +0200422
423 if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
424 return 1;
425 else
426 return 0;
427}
428#endif /* CONFIG_POST */