3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <environment.h>
15 #include <asm/processor.h>
17 #include <asm/ppc4xx-gpio.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define REBOOT_MAGIC 0x07081967
22 #define REBOOT_NOP 0x00000000
23 #define REBOOT_DO_POST 0x00000001
25 extern flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
27 ulong flash_get_size(ulong base, int banknum);
28 void env_crc_update(void);
30 static u32 start_time;
32 int board_early_init_f(void)
34 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
35 mtdcr(UIC0ER, 0x00000000); /* disable all ints */
36 mtdcr(UIC0CR, 0x00000000);
37 mtdcr(UIC0PR, 0xFFFF7F00); /* set int polarities */
38 mtdcr(UIC0TR, 0x00000000); /* set int trigger levels */
39 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
40 mtdcr(UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest priority */
43 * Configure CPC0_PCI to enable PerWE as output
45 mtdcr(CPC0_PCI, CPC0_PCI_SPE);
57 post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
58 post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
59 if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST)) {
61 * Set special bootline bootparameter to pass this POST boot
62 * mode to Linux to reset the username/password
64 setenv("addmisc", "setenv bootargs \\${bootargs} factory_reset=yes");
67 * Normally don't run POST tests, only when enabled
68 * via the sw-reset button. So disable further tests
69 * upon next bootup here.
71 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_NOP);
74 * Only run POST when initiated via the sw-reset button mechanism
82 start_time = get_timer(0);
88 /* Re-do sizing to get full correct info */
90 /* adjust flash start and offset */
92 switch (gd->bd->bi_flashsize) {
118 pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
122 * Re-check to get correct base address
124 flash_get_size(gd->bd->bi_flashstart, 0);
126 /* Monitor protection ON by default */
127 (void)flash_protect(FLAG_PROTECT_SET,
128 -CONFIG_SYS_MONITOR_LEN,
132 /* Env protection ON by default */
133 (void)flash_protect(FLAG_PROTECT_SET,
134 CONFIG_ENV_ADDR_REDUND,
135 CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
142 * Check Board Identity:
147 int i = getenv_f("serial#", buf, sizeof(buf));
149 puts("Board: Zeus-");
151 if (in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_ZEUS_PE))
156 puts(" of BulletEndPoint");
165 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
166 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
169 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 1);
170 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 1);
175 static int default_env_var(char *buf, char *var)
183 ptr = strstr(buf + 4, var);
185 printf("ERROR: %s not found!\n", var);
188 ptr += strlen(var) + 1;
191 * Now the ethaddr needs to be updated in the "normal"
192 * environment storage -> redundant flash.
196 printf("Updated %s from eeprom to %s!\n", var, val);
201 static int restore_default(void)
212 * Read board specific values from I2C EEPROM
213 * and set env variables accordingly
214 * -> ethaddr, eth1addr, serial#
216 buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
218 printf("ERROR: malloc() failed\n");
221 if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
222 (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
223 puts("\nError reading EEPROM!\n");
225 crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
226 if (crc != *(u32 *)buf) {
227 printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
231 default_env_var(buf, "ethaddr");
233 default_env_var(buf, "eth1addr");
235 default_env_var(buf, "serial#");
239 * Finally save updated env variables back to flash
248 int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
261 buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
262 memset(buf, 0, FACTORY_RESET_ENV_SIZE);
264 strcpy(var, "ethaddr");
265 printf("Setting %s to %s\n", var, argv[1]);
266 sprintf(str, "%s=%s", var, argv[1]);
267 strcpy(buf + 4, str);
268 buf += strlen(str) + 1;
270 strcpy(var, "eth1addr");
271 printf("Setting %s to %s\n", var, argv[2]);
272 sprintf(str, "%s=%s", var, argv[2]);
273 strcpy(buf + 4, str);
274 buf += strlen(str) + 1;
276 strcpy(var, "serial#");
277 printf("Setting %s to %s\n", var, argv[3]);
278 sprintf(str, "%s=%s", var, argv[3]);
279 strcpy(buf + 4, str);
281 crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
282 *(u32 *)buf_save = crc;
284 if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
285 (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
286 puts("\nError writing EEPROM!\n");
296 setdef, 4, 1, do_set_default,
297 "write board-specific values to EEPROM (ethaddr...)",
298 "ethaddr eth1addr serial#\n - write board-specific values to EEPROM"
301 static inline int sw_reset_pressed(void)
303 return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
306 int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char * const argv[])
311 int factory_reset = 0;
313 if (!sw_reset_pressed()) {
314 printf("SW-Reset already high (Button released)\n");
315 printf("-> No action taken!\n");
319 printf("Waiting for SW-Reset button to be released.");
322 delta = get_timer(start_time);
323 if (!sw_reset_pressed())
326 if ((delta > CONFIG_SYS_TIME_POST) && !post) {
327 printf("\nWhen released now, POST tests will be started.");
328 gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
332 if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
333 printf("\nWhen released now, factory default values"
334 " will be restored.");
335 gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
340 if (!(count++ % 1000))
345 printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
347 if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
348 printf("Starting factory reset value restoration...\n");
351 * Restore default setting
356 * Reset the board for default to become valid
358 do_reset(NULL, 0, 0, NULL);
363 if (delta > CONFIG_SYS_TIME_POST) {
364 printf("Starting POST configuration...\n");
367 * Enable POST upon next bootup
369 out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
370 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
371 post_bootmode_init();
374 * Reset the logbuffer for a clean start
378 do_reset(NULL, 0, 0, NULL);
387 chkreset, 1, 1, do_chkreset,
388 "Check for status of SW-reset button and act accordingly",
392 #if defined(CONFIG_POST)
394 * Returns 1 if keys pressed to start the power-on long-running tests
395 * Called from board_init_f().
397 int post_hotkeys_pressed(void)
402 post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
403 post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
405 if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
410 #endif /* CONFIG_POST */