blob: 1ee0a2927efedfd70b569fa84fd0f0c4b5e1dd04 [file] [log] [blame]
wdenka042ac82002-09-12 22:36:57 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@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>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020025#include <stdio_dev.h>
wdenka042ac82002-09-12 22:36:57 +000026#include <watchdog.h>
27#include <post.h>
28
Mike Frysinger9146d132011-05-10 07:01:21 +000029#ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
30#include <asm/gpio.h>
31#endif
32
wdenk56f94be2002-11-05 16:35:14 +000033#ifdef CONFIG_LOGBUFFER
34#include <logbuff.h>
35#endif
36
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
wdenka042ac82002-09-12 22:36:57 +000039#define POST_MAX_NUMBER 32
40
41#define BOOTMODE_MAGIC 0xDEAD0000
42
wdenk4532cb62003-04-27 22:52:51 +000043int post_init_f (void)
44{
wdenk4532cb62003-04-27 22:52:51 +000045 int res = 0;
46 unsigned int i;
47
48 for (i = 0; i < post_list_size; i++) {
49 struct post_test *test = post_list + i;
50
51 if (test->init_f && test->init_f()) {
52 res = -1;
53 }
54 }
wdenk8bde7f72003-06-27 21:31:46 +000055
wdenk4532cb62003-04-27 22:52:51 +000056 gd->post_init_f_time = post_time_ms(0);
57 if (!gd->post_init_f_time)
58 {
59 printf("post/post.c: post_time_ms seems not to be implemented\n");
60 }
61
62 return res;
63}
64
Stefan Roese39ff7d52009-12-03 06:24:30 +010065/*
66 * Supply a default implementation for post_hotkeys_pressed() for boards
67 * without hotkey support. We always return 0 here, so that the
68 * long-running tests won't be started.
69 *
70 * Boards with hotkey support can override this weak default function
71 * by defining one in their board specific code.
72 */
73int __post_hotkeys_pressed(void)
74{
Mike Frysinger9146d132011-05-10 07:01:21 +000075#ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
76 int ret;
77 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
78
79 ret = gpio_request(gpio, "hotkeys");
80 if (ret) {
81 printf("POST: gpio hotkey request failed\n");
82 return 0;
83 }
84
85 gpio_direction_input(gpio);
86 ret = gpio_get_value(gpio);
87 gpio_free(gpio);
88
89 return ret;
90#endif
91
Stefan Roese39ff7d52009-12-03 06:24:30 +010092 return 0; /* No hotkeys supported */
93}
94int post_hotkeys_pressed(void)
95 __attribute__((weak, alias("__post_hotkeys_pressed")));
96
97
wdenka042ac82002-09-12 22:36:57 +000098void post_bootmode_init (void)
99{
100 int bootmode = post_bootmode_get (0);
wdenk27b207f2003-07-24 23:38:38 +0000101 int newword;
wdenk42d1f032003-10-15 23:53:47 +0000102
wdenk27b207f2003-07-24 23:38:38 +0000103 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
104 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
wdenk6dff5522003-07-15 07:45:49 +0000105 } else if (bootmode == 0) {
wdenk27b207f2003-07-24 23:38:38 +0000106 newword = BOOTMODE_MAGIC | POST_POWERON;
wdenk6dff5522003-07-15 07:45:49 +0000107 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
wdenk27b207f2003-07-24 23:38:38 +0000108 newword = BOOTMODE_MAGIC | POST_NORMAL;
wdenka042ac82002-09-12 22:36:57 +0000109 } else {
wdenk27b207f2003-07-24 23:38:38 +0000110 /* Use old value */
111 newword = post_word_load () & ~POST_COLDBOOT;
wdenka042ac82002-09-12 22:36:57 +0000112 }
113
wdenk27b207f2003-07-24 23:38:38 +0000114 if (bootmode == 0)
115 {
116 /* We are booting after power-on */
117 newword |= POST_COLDBOOT;
118 }
119
120 post_word_store (newword);
121
wdenk228f29a2002-12-08 09:53:23 +0000122 /* Reset activity record */
123 gd->post_log_word = 0;
Valentin Longchamp79843952011-08-03 02:37:01 +0000124 gd->post_log_res = 0;
wdenka042ac82002-09-12 22:36:57 +0000125}
126
127int post_bootmode_get (unsigned int *last_test)
128{
129 unsigned long word = post_word_load ();
130 int bootmode;
131
132 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
133 return 0;
134 }
135
wdenk27b207f2003-07-24 23:38:38 +0000136 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +0000137
138 if (last_test && (bootmode & POST_POWERTEST)) {
139 *last_test = (word >> 8) & 0xFF;
140 }
141
142 return bootmode;
143}
144
wdenk228f29a2002-12-08 09:53:23 +0000145/* POST tests run before relocation only mark status bits .... */
146static void post_log_mark_start ( unsigned long testid )
147{
Valentin Longchamp79843952011-08-03 02:37:01 +0000148 gd->post_log_word |= testid;
wdenk228f29a2002-12-08 09:53:23 +0000149}
150
151static void post_log_mark_succ ( unsigned long testid )
152{
Valentin Longchamp79843952011-08-03 02:37:01 +0000153 gd->post_log_res |= testid;
wdenk228f29a2002-12-08 09:53:23 +0000154}
155
156/* ... and the messages are output once we are relocated */
157void post_output_backlog ( void )
158{
wdenk228f29a2002-12-08 09:53:23 +0000159 int j;
160
161 for (j = 0; j < post_list_size; j++) {
Valentin Longchamp79843952011-08-03 02:37:01 +0000162 if (gd->post_log_word & (post_list[j].testid)) {
wdenk228f29a2002-12-08 09:53:23 +0000163 post_log ("POST %s ", post_list[j].cmd);
Valentin Longchamp79843952011-08-03 02:37:01 +0000164 if (gd->post_log_res & post_list[j].testid)
wdenk228f29a2002-12-08 09:53:23 +0000165 post_log ("PASSED\n");
wdenk63e73c92004-02-23 22:22:28 +0000166 else {
wdenk228f29a2002-12-08 09:53:23 +0000167 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200168 show_boot_progress (-31);
wdenk63e73c92004-02-23 22:22:28 +0000169 }
wdenk228f29a2002-12-08 09:53:23 +0000170 }
171 }
172}
173
wdenka042ac82002-09-12 22:36:57 +0000174static void post_bootmode_test_on (unsigned int last_test)
175{
176 unsigned long word = post_word_load ();
177
178 word |= POST_POWERTEST;
179
180 word |= (last_test & 0xFF) << 8;
181
182 post_word_store (word);
183}
184
185static void post_bootmode_test_off (void)
186{
187 unsigned long word = post_word_load ();
188
189 word &= ~POST_POWERTEST;
190
191 post_word_store (word);
192}
193
Valentin Longchamp212a0ca2011-08-03 02:37:02 +0000194#ifndef CONFIG_POST_SKIP_ENV_FLAGS
195static void post_get_env_flags(int *test_flags)
wdenka042ac82002-09-12 22:36:57 +0000196{
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100197 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
198 POST_CRITICAL };
199 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
200 "post_critical" };
Mike Frysingerd2397812011-05-10 07:28:35 +0000201 int varnum = ARRAY_SIZE(var);
wdenka042ac82002-09-12 22:36:57 +0000202 char list[128]; /* long enough for POST list */
203 char *name;
204 char *s;
205 int last;
206 int i, j;
207
wdenka042ac82002-09-12 22:36:57 +0000208 for (i = 0; i < varnum; i++) {
Wolfgang Denkcdb74972010-07-24 21:55:43 +0200209 if (getenv_f(var[i], list, sizeof (list)) <= 0)
wdenka042ac82002-09-12 22:36:57 +0000210 continue;
211
212 for (j = 0; j < post_list_size; j++) {
213 test_flags[j] &= ~flag[i];
214 }
215
216 last = 0;
217 name = list;
218 while (!last) {
219 while (*name && *name == ' ')
220 name++;
221 if (*name == 0)
222 break;
223 s = name + 1;
224 while (*s && *s != ' ')
225 s++;
226 if (*s == 0)
227 last = 1;
228 else
229 *s = 0;
230
231 for (j = 0; j < post_list_size; j++) {
232 if (strcmp (post_list[j].cmd, name) == 0) {
233 test_flags[j] |= flag[i];
234 break;
235 }
236 }
237
238 if (j == post_list_size) {
239 printf ("No such test: %s\n", name);
240 }
241
242 name = s + 1;
243 }
244 }
Valentin Longchamp212a0ca2011-08-03 02:37:02 +0000245}
246#endif
247
248static void post_get_flags(int *test_flags)
249{
250 int j;
251
252 for (j = 0; j < post_list_size; j++)
253 test_flags[j] = post_list[j].flags;
254
255#ifndef CONFIG_POST_SKIP_ENV_FLAGS
256 post_get_env_flags(test_flags);
257#endif
wdenk6dff5522003-07-15 07:45:49 +0000258
259 for (j = 0; j < post_list_size; j++) {
260 if (test_flags[j] & POST_POWERON) {
261 test_flags[j] |= POST_SLOWTEST;
262 }
263 }
wdenka042ac82002-09-12 22:36:57 +0000264}
265
Michael Zaidmane070a562010-03-01 11:47:36 +0200266void __show_post_progress (unsigned int test_num, int before, int result)
267{
268}
269void show_post_progress (unsigned int, int, int)
270 __attribute__((weak, alias("__show_post_progress")));
271
wdenka042ac82002-09-12 22:36:57 +0000272static int post_run_single (struct post_test *test,
273 int test_flags, int flags, unsigned int i)
274{
275 if ((flags & test_flags & POST_ALWAYS) &&
276 (flags & test_flags & POST_MEM)) {
277 WATCHDOG_RESET ();
278
279 if (!(flags & POST_REBOOT)) {
280 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100281 post_bootmode_test_on (
282 (gd->flags & GD_FLG_POSTFAIL) ?
283 POST_FAIL_SAVE | i : i);
wdenka042ac82002-09-12 22:36:57 +0000284 }
285
wdenk228f29a2002-12-08 09:53:23 +0000286 if (test_flags & POST_PREREL)
287 post_log_mark_start ( test->testid );
288 else
Michael Zaidmane070a562010-03-01 11:47:36 +0200289 post_log ("POST %s ", test->cmd);
wdenka042ac82002-09-12 22:36:57 +0000290 }
291
Michael Zaidmane070a562010-03-01 11:47:36 +0200292 show_post_progress(i, POST_BEFORE, POST_FAILED);
293
wdenk228f29a2002-12-08 09:53:23 +0000294 if (test_flags & POST_PREREL) {
Michael Zaidmane070a562010-03-01 11:47:36 +0200295 if ((*test->test) (flags) == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000296 post_log_mark_succ ( test->testid );
Michael Zaidmane070a562010-03-01 11:47:36 +0200297 show_post_progress(i, POST_AFTER, POST_PASSED);
298 }
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200299 else {
Michael Zaidmane070a562010-03-01 11:47:36 +0200300 show_post_progress(i, POST_AFTER, POST_FAILED);
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200301 if (test_flags & POST_CRITICAL)
302 gd->flags |= GD_FLG_POSTFAIL;
303 if (test_flags & POST_STOP)
304 gd->flags |= GD_FLG_POSTSTOP;
305 }
wdenk228f29a2002-12-08 09:53:23 +0000306 } else {
James Kosin975afc32011-07-14 08:15:06 +0000307 if ((*test->test)(flags) != 0) {
308 post_log("FAILED\n");
309 show_boot_progress(-32);
310 show_post_progress(i, POST_AFTER, POST_FAILED);
311 if (test_flags & POST_CRITICAL)
312 gd->flags |= GD_FLG_POSTFAIL;
313 if (test_flags & POST_STOP)
314 gd->flags |= GD_FLG_POSTSTOP;
315 } else {
316 post_log("PASSED\n");
317 show_post_progress(i, POST_AFTER, POST_PASSED);
318 }
wdenk228f29a2002-12-08 09:53:23 +0000319 }
wdenka042ac82002-09-12 22:36:57 +0000320
321 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
322 post_bootmode_test_off ();
323 }
324
325 return 0;
326 } else {
327 return -1;
328 }
329}
330
331int post_run (char *name, int flags)
332{
333 unsigned int i;
334 int test_flags[POST_MAX_NUMBER];
335
336 post_get_flags (test_flags);
337
338 if (name == NULL) {
339 unsigned int last;
340
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200341 if (gd->flags & GD_FLG_POSTSTOP)
342 return 0;
343
wdenka042ac82002-09-12 22:36:57 +0000344 if (post_bootmode_get (&last) & POST_POWERTEST) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100345 if (last & POST_FAIL_SAVE) {
346 last &= ~POST_FAIL_SAVE;
347 gd->flags |= GD_FLG_POSTFAIL;
348 }
wdenka042ac82002-09-12 22:36:57 +0000349 if (last < post_list_size &&
350 (flags & test_flags[last] & POST_ALWAYS) &&
351 (flags & test_flags[last] & POST_MEM)) {
352
wdenkea909b72002-11-21 23:11:29 +0000353 post_run_single (post_list + last,
354 test_flags[last],
355 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000356
357 for (i = last + 1; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200358 if (gd->flags & GD_FLG_POSTSTOP)
359 break;
wdenkea909b72002-11-21 23:11:29 +0000360 post_run_single (post_list + i,
361 test_flags[i],
362 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000363 }
364 }
365 } else {
366 for (i = 0; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200367 if (gd->flags & GD_FLG_POSTSTOP)
368 break;
wdenkea909b72002-11-21 23:11:29 +0000369 post_run_single (post_list + i,
370 test_flags[i],
371 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000372 }
373 }
374
375 return 0;
376 } else {
377 for (i = 0; i < post_list_size; i++) {
378 if (strcmp (post_list[i].cmd, name) == 0)
379 break;
380 }
381
382 if (i < post_list_size) {
Sascha Laue5744ddc2008-05-30 09:48:14 +0200383 WATCHDOG_RESET();
wdenka042ac82002-09-12 22:36:57 +0000384 return post_run_single (post_list + i,
385 test_flags[i],
386 flags, i);
387 } else {
388 return -1;
389 }
390 }
391}
392
393static int post_info_single (struct post_test *test, int full)
394{
395 if (test->flags & POST_MANUAL) {
396 if (full)
397 printf ("%s - %s\n"
398 " %s\n", test->cmd, test->name, test->desc);
399 else
400 printf (" %-15s - %s\n", test->cmd, test->name);
401
402 return 0;
403 } else {
404 return -1;
405 }
406}
407
408int post_info (char *name)
409{
410 unsigned int i;
411
412 if (name == NULL) {
413 for (i = 0; i < post_list_size; i++) {
414 post_info_single (post_list + i, 0);
415 }
416
417 return 0;
418 } else {
419 for (i = 0; i < post_list_size; i++) {
420 if (strcmp (post_list[i].cmd, name) == 0)
421 break;
422 }
423
424 if (i < post_list_size) {
425 return post_info_single (post_list + i, 1);
426 } else {
427 return -1;
428 }
429 }
430}
431
432int post_log (char *format, ...)
433{
434 va_list args;
435 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200436 char printbuffer[CONFIG_SYS_PBSIZE];
wdenka042ac82002-09-12 22:36:57 +0000437
438 va_start (args, format);
439
440 /* For this to work, printbuffer must be larger than
441 * anything we ever want to print.
442 */
443 i = vsprintf (printbuffer, format, args);
444 va_end (args);
445
wdenk56f94be2002-11-05 16:35:14 +0000446#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000447 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000448 logbuff_log (printbuffer);
449#else
wdenka042ac82002-09-12 22:36:57 +0000450 /* Send to the stdout file */
451 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000452#endif
wdenka042ac82002-09-12 22:36:57 +0000453
454 return 0;
455}
456
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200457#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenka042ac82002-09-12 22:36:57 +0000458void post_reloc (void)
459{
wdenka042ac82002-09-12 22:36:57 +0000460 unsigned int i;
461
462 /*
463 * We have to relocate the test table manually
464 */
465 for (i = 0; i < post_list_size; i++) {
466 ulong addr;
467 struct post_test *test = post_list + i;
468
469 if (test->name) {
470 addr = (ulong) (test->name) + gd->reloc_off;
471 test->name = (char *) addr;
472 }
473
474 if (test->cmd) {
475 addr = (ulong) (test->cmd) + gd->reloc_off;
476 test->cmd = (char *) addr;
477 }
478
479 if (test->desc) {
480 addr = (ulong) (test->desc) + gd->reloc_off;
481 test->desc = (char *) addr;
482 }
483
484 if (test->test) {
485 addr = (ulong) (test->test) + gd->reloc_off;
486 test->test = (int (*)(int flags)) addr;
487 }
wdenk4532cb62003-04-27 22:52:51 +0000488
489 if (test->init_f) {
490 addr = (ulong) (test->init_f) + gd->reloc_off;
491 test->init_f = (int (*)(void)) addr;
492 }
493
494 if (test->reloc) {
495 addr = (ulong) (test->reloc) + gd->reloc_off;
496 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000497
wdenk4532cb62003-04-27 22:52:51 +0000498 test->reloc();
499 }
wdenka042ac82002-09-12 22:36:57 +0000500 }
501}
Peter Tyser521af042009-09-21 11:20:36 -0500502#endif
wdenka042ac82002-09-12 22:36:57 +0000503
wdenk4532cb62003-04-27 22:52:51 +0000504
505/*
506 * Some tests (e.g. SYSMON) need the time when post_init_f started,
507 * but we cannot use get_timer() at this point.
508 *
509 * On PowerPC we implement it using the timebase register.
510 */
511unsigned long post_time_ms (unsigned long base)
512{
513#ifdef CONFIG_PPC
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200514 return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
wdenk4532cb62003-04-27 22:52:51 +0000515#else
Wolfgang Denkad5bb452007-03-06 18:08:43 +0100516#warning "Not implemented yet"
wdenk4532cb62003-04-27 22:52:51 +0000517 return 0; /* Not implemented yet */
518#endif
519}