]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/zeus/zeus.c
01273a1f7db1ea06af686ae379203c05f89ac026
[karo-tx-uboot.git] / board / zeus / zeus.c
1 /*
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>
33 #include <asm/gpio.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 #define REBOOT_MAGIC    0x07081967
38 #define REBOOT_NOP      0x00000000
39 #define REBOOT_DO_POST  0x00000001
40
41 extern flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips     */
42 extern env_t *env_ptr;
43 extern uchar default_environment[];
44
45 ulong flash_get_size(ulong base, int banknum);
46 void env_crc_update(void);
47 int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
48
49 static u32 start_time;
50
51 int board_early_init_f(void)
52 {
53         mtdcr(UIC0SR, 0xFFFFFFFF);      /* clear all ints */
54         mtdcr(UIC0ER, 0x00000000);      /* disable all ints */
55         mtdcr(UIC0CR, 0x00000000);
56         mtdcr(UIC0PR, 0xFFFF7F00);      /* set int polarities */
57         mtdcr(UIC0TR, 0x00000000);      /* set int trigger levels */
58         mtdcr(UIC0SR, 0xFFFFFFFF);      /* clear all ints */
59         mtdcr(UIC0VCR, 0x00000001);     /* set vect base=0,INT0 highest priority */
60
61         /*
62          * Configure CPC0_PCI to enable PerWE as output
63          */
64         mtdcr(CPC0_PCI, CPC0_PCI_SPE);
65
66         return 0;
67 }
68
69 int misc_init_r(void)
70 {
71         u32 pbcr;
72         int size_val = 0;
73         u32 post_magic;
74         u32 post_val;
75
76         post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
77         post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
78         if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST)) {
79                 /*
80                  * Set special bootline bootparameter to pass this POST boot
81                  * mode to Linux to reset the username/password
82                  */
83                 setenv("addmisc", "setenv bootargs \\${bootargs} factory_reset=yes");
84
85                 /*
86                  * Normally don't run POST tests, only when enabled
87                  * via the sw-reset button. So disable further tests
88                  * upon next bootup here.
89                  */
90                 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_NOP);
91         } else {
92                 /*
93                  * Only run POST when initiated via the sw-reset button mechanism
94                  */
95                 post_word_store(0);
96         }
97
98         /*
99          * Get current time
100          */
101         start_time = get_timer(0);
102
103         /*
104          * FLASH stuff...
105          */
106
107         /* Re-do sizing to get full correct info */
108
109         /* adjust flash start and offset */
110         mfebc(PB0CR, pbcr);
111         switch (gd->bd->bi_flashsize) {
112         case 1 << 20:
113                 size_val = 0;
114                 break;
115         case 2 << 20:
116                 size_val = 1;
117                 break;
118         case 4 << 20:
119                 size_val = 2;
120                 break;
121         case 8 << 20:
122                 size_val = 3;
123                 break;
124         case 16 << 20:
125                 size_val = 4;
126                 break;
127         case 32 << 20:
128                 size_val = 5;
129                 break;
130         case 64 << 20:
131                 size_val = 6;
132                 break;
133         case 128 << 20:
134                 size_val = 7;
135                 break;
136         }
137         pbcr = (pbcr & 0x0001ffff) | gd->bd->bi_flashstart | (size_val << 17);
138         mtebc(PB0CR, pbcr);
139
140         /*
141          * Re-check to get correct base address
142          */
143         flash_get_size(gd->bd->bi_flashstart, 0);
144
145         /* Monitor protection ON by default */
146         (void)flash_protect(FLAG_PROTECT_SET,
147                             -CONFIG_SYS_MONITOR_LEN,
148                             0xffffffff,
149                             &flash_info[0]);
150
151         /* Env protection ON by default */
152         (void)flash_protect(FLAG_PROTECT_SET,
153                             CONFIG_ENV_ADDR_REDUND,
154                             CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
155                             &flash_info[0]);
156
157         return 0;
158 }
159
160 /*
161  * Check Board Identity:
162  */
163 int checkboard(void)
164 {
165         char *s = getenv("serial#");
166
167         puts("Board: Zeus-");
168
169         if (in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_ZEUS_PE))
170                 puts("PE");
171         else
172                 puts("CE");
173
174         puts(" of BulletEndPoint");
175
176         if (s != NULL) {
177                 puts(", serial# ");
178                 puts(s);
179         }
180         putc('\n');
181
182         /* both LED's off */
183         gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
184         gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
185         udelay(10000);
186         /* and on again */
187         gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 1);
188         gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 1);
189
190         return (0);
191 }
192
193 static 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
219 static int restore_default(void)
220 {
221         char *buf;
222         char *buf_save;
223         u32 crc;
224
225         /*
226          * Unprotect and erase environment area
227          */
228         flash_protect(FLAG_PROTECT_CLEAR,
229                       CONFIG_ENV_ADDR_REDUND,
230                       CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1,
231                       &flash_info[0]);
232
233         flash_sect_erase(CONFIG_ENV_ADDR_REDUND,
234                          CONFIG_ENV_ADDR_REDUND + 2*CONFIG_ENV_SECT_SIZE - 1);
235
236         /*
237          * Now restore default environment from U-Boot image
238          * -> ipaddr, serverip...
239          */
240         memset(env_ptr, 0, sizeof(env_t));
241         memcpy(env_ptr->data, default_environment, ENV_SIZE);
242 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
243         env_ptr->flags = 0xFF;
244 #endif
245         env_crc_update();
246         gd->env_valid = 1;
247
248         /*
249          * Read board specific values from I2C EEPROM
250          * and set env variables accordingly
251          * -> ethaddr, eth1addr, serial#
252          */
253         buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
254         if (eeprom_read(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
255                         (u8 *)buf, FACTORY_RESET_ENV_SIZE)) {
256                 puts("\nError reading EEPROM!\n");
257         } else {
258                 crc = crc32(0, (u8 *)(buf + 4), FACTORY_RESET_ENV_SIZE - 4);
259                 if (crc != *(u32 *)buf) {
260                         printf("ERROR: crc mismatch %08x %08x\n", crc, *(u32 *)buf);
261                         return -1;
262                 }
263
264                 default_env_var(buf, "ethaddr");
265                 buf += 8 + 18;
266                 default_env_var(buf, "eth1addr");
267                 buf += 9 + 18;
268                 default_env_var(buf, "serial#");
269         }
270
271         /*
272          * Finally save updated env variables back to flash
273          */
274         saveenv();
275
276         free(buf_save);
277
278         return 0;
279 }
280
281 int do_set_default(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
282 {
283         char *buf;
284         char *buf_save;
285         char str[32];
286         u32 crc;
287         char var[32];
288
289         if (argc < 4) {
290                 puts("ERROR!\n");
291                 return -1;
292         }
293
294         buf = buf_save = malloc(FACTORY_RESET_ENV_SIZE);
295         memset(buf, 0, FACTORY_RESET_ENV_SIZE);
296
297         strcpy(var, "ethaddr");
298         printf("Setting %s to %s\n", var, argv[1]);
299         sprintf(str, "%s=%s", var, argv[1]);
300         strcpy(buf + 4, str);
301         buf += strlen(str) + 1;
302
303         strcpy(var, "eth1addr");
304         printf("Setting %s to %s\n", var, argv[2]);
305         sprintf(str, "%s=%s", var, argv[2]);
306         strcpy(buf + 4, str);
307         buf += strlen(str) + 1;
308
309         strcpy(var, "serial#");
310         printf("Setting %s to %s\n", var, argv[3]);
311         sprintf(str, "%s=%s", var, argv[3]);
312         strcpy(buf + 4, str);
313
314         crc = crc32(0, (u8 *)(buf_save + 4), FACTORY_RESET_ENV_SIZE - 4);
315         *(u32 *)buf_save = crc;
316
317         if (eeprom_write(FACTORY_RESET_I2C_EEPROM, FACTORY_RESET_ENV_OFFS,
318                          (u8 *)buf_save, FACTORY_RESET_ENV_SIZE)) {
319                 puts("\nError writing EEPROM!\n");
320                 return -1;
321         }
322
323         free(buf_save);
324
325         return 0;
326 }
327
328 U_BOOT_CMD(
329         setdef, 4,      1,      do_set_default,
330         "write board-specific values to EEPROM (ethaddr...)",
331         "ethaddr eth1addr serial#\n    - write board-specific values to EEPROM"
332         );
333
334 static inline int sw_reset_pressed(void)
335 {
336         return !(in_be32((void *)GPIO0_IR) & GPIO_VAL(CONFIG_SYS_GPIO_SW_RESET));
337 }
338
339 int do_chkreset(cmd_tbl_t* cmdtp, int flag, int argc, char* argv[])
340 {
341         int delta;
342         int count = 0;
343         int post = 0;
344         int factory_reset = 0;
345
346         if (!sw_reset_pressed()) {
347                 printf("SW-Reset already high (Button released)\n");
348                 printf("-> No action taken!\n");
349                 return 0;
350         }
351
352         printf("Waiting for SW-Reset button to be released.");
353
354         while (1) {
355                 delta = get_timer(start_time);
356                 if (!sw_reset_pressed())
357                         break;
358
359                 if ((delta > CONFIG_SYS_TIME_POST) && !post) {
360                         printf("\nWhen released now, POST tests will be started.");
361                         gpio_write_bit(CONFIG_SYS_GPIO_LED_GREEN, 0);
362                         post = 1;
363                 }
364
365                 if ((delta > CONFIG_SYS_TIME_FACTORY_RESET) && !factory_reset) {
366                         printf("\nWhen released now, factory default values"
367                                " will be restored.");
368                         gpio_write_bit(CONFIG_SYS_GPIO_LED_RED, 0);
369                         factory_reset = 1;
370                 }
371
372                 udelay(1000);
373                 if (!(count++ % 1000))
374                         printf(".");
375         }
376
377
378         printf("\nSW-Reset Button released after %d milli-seconds!\n", delta);
379
380         if (delta > CONFIG_SYS_TIME_FACTORY_RESET) {
381                 printf("Starting factory reset value restoration...\n");
382
383                 /*
384                  * Restore default setting
385                  */
386                 restore_default();
387
388                 /*
389                  * Reset the board for default to become valid
390                  */
391                 do_reset(NULL, 0, 0, NULL);
392
393                 return 0;
394         }
395
396         if (delta > CONFIG_SYS_TIME_POST) {
397                 printf("Starting POST configuration...\n");
398
399                 /*
400                  * Enable POST upon next bootup
401                  */
402                 out_be32((void *)CONFIG_SYS_POST_MAGIC, REBOOT_MAGIC);
403                 out_be32((void *)CONFIG_SYS_POST_VAL, REBOOT_DO_POST);
404                 post_bootmode_init();
405
406                 /*
407                  * Reset the logbuffer for a clean start
408                  */
409                 logbuff_reset();
410
411                 do_reset(NULL, 0, 0, NULL);
412
413                 return 0;
414         }
415
416         return 0;
417 }
418
419 U_BOOT_CMD (
420         chkreset, 1, 1, do_chkreset,
421         "Check for status of SW-reset button and act accordingly",
422         ""
423 );
424
425 #if defined(CONFIG_POST)
426 /*
427  * Returns 1 if keys pressed to start the power-on long-running tests
428  * Called from board_init_f().
429  */
430 int post_hotkeys_pressed(void)
431 {
432         u32 post_magic;
433         u32 post_val;
434
435         post_magic = in_be32((void *)CONFIG_SYS_POST_MAGIC);
436         post_val = in_be32((void *)CONFIG_SYS_POST_VAL);
437
438         if ((post_magic == REBOOT_MAGIC) && (post_val == REBOOT_DO_POST))
439                 return 1;
440         else
441                 return 0;
442 }
443 #endif /* CONFIG_POST */