blob: 852d6a5dab51ca84623487658c7d0b36abcf13be [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;
wdenka042ac82002-09-12 22:36:57 +0000124}
125
126int post_bootmode_get (unsigned int *last_test)
127{
128 unsigned long word = post_word_load ();
129 int bootmode;
130
131 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
132 return 0;
133 }
134
wdenk27b207f2003-07-24 23:38:38 +0000135 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +0000136
137 if (last_test && (bootmode & POST_POWERTEST)) {
138 *last_test = (word >> 8) & 0xFF;
139 }
140
141 return bootmode;
142}
143
wdenk228f29a2002-12-08 09:53:23 +0000144/* POST tests run before relocation only mark status bits .... */
145static void post_log_mark_start ( unsigned long testid )
146{
wdenk228f29a2002-12-08 09:53:23 +0000147 gd->post_log_word |= (testid)<<16;
148}
149
150static void post_log_mark_succ ( unsigned long testid )
151{
wdenk228f29a2002-12-08 09:53:23 +0000152 gd->post_log_word |= testid;
153}
154
155/* ... and the messages are output once we are relocated */
156void post_output_backlog ( void )
157{
wdenk228f29a2002-12-08 09:53:23 +0000158 int j;
159
160 for (j = 0; j < post_list_size; j++) {
161 if (gd->post_log_word & (post_list[j].testid<<16)) {
162 post_log ("POST %s ", post_list[j].cmd);
163 if (gd->post_log_word & post_list[j].testid)
164 post_log ("PASSED\n");
wdenk63e73c92004-02-23 22:22:28 +0000165 else {
wdenk228f29a2002-12-08 09:53:23 +0000166 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200167 show_boot_progress (-31);
wdenk63e73c92004-02-23 22:22:28 +0000168 }
wdenk228f29a2002-12-08 09:53:23 +0000169 }
170 }
171}
172
wdenka042ac82002-09-12 22:36:57 +0000173static void post_bootmode_test_on (unsigned int last_test)
174{
175 unsigned long word = post_word_load ();
176
177 word |= POST_POWERTEST;
178
179 word |= (last_test & 0xFF) << 8;
180
181 post_word_store (word);
182}
183
184static void post_bootmode_test_off (void)
185{
186 unsigned long word = post_word_load ();
187
188 word &= ~POST_POWERTEST;
189
190 post_word_store (word);
191}
192
193static void post_get_flags (int *test_flags)
194{
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100195 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
196 POST_CRITICAL };
197 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
198 "post_critical" };
Mike Frysingerd2397812011-05-10 07:28:35 +0000199 int varnum = ARRAY_SIZE(var);
wdenka042ac82002-09-12 22:36:57 +0000200 char list[128]; /* long enough for POST list */
201 char *name;
202 char *s;
203 int last;
204 int i, j;
205
206 for (j = 0; j < post_list_size; j++) {
207 test_flags[j] = post_list[j].flags;
208 }
209
210 for (i = 0; i < varnum; i++) {
Wolfgang Denkcdb74972010-07-24 21:55:43 +0200211 if (getenv_f(var[i], list, sizeof (list)) <= 0)
wdenka042ac82002-09-12 22:36:57 +0000212 continue;
213
214 for (j = 0; j < post_list_size; j++) {
215 test_flags[j] &= ~flag[i];
216 }
217
218 last = 0;
219 name = list;
220 while (!last) {
221 while (*name && *name == ' ')
222 name++;
223 if (*name == 0)
224 break;
225 s = name + 1;
226 while (*s && *s != ' ')
227 s++;
228 if (*s == 0)
229 last = 1;
230 else
231 *s = 0;
232
233 for (j = 0; j < post_list_size; j++) {
234 if (strcmp (post_list[j].cmd, name) == 0) {
235 test_flags[j] |= flag[i];
236 break;
237 }
238 }
239
240 if (j == post_list_size) {
241 printf ("No such test: %s\n", name);
242 }
243
244 name = s + 1;
245 }
246 }
wdenk6dff5522003-07-15 07:45:49 +0000247
248 for (j = 0; j < post_list_size; j++) {
249 if (test_flags[j] & POST_POWERON) {
250 test_flags[j] |= POST_SLOWTEST;
251 }
252 }
wdenka042ac82002-09-12 22:36:57 +0000253}
254
Michael Zaidmane070a562010-03-01 11:47:36 +0200255void __show_post_progress (unsigned int test_num, int before, int result)
256{
257}
258void show_post_progress (unsigned int, int, int)
259 __attribute__((weak, alias("__show_post_progress")));
260
wdenka042ac82002-09-12 22:36:57 +0000261static int post_run_single (struct post_test *test,
262 int test_flags, int flags, unsigned int i)
263{
264 if ((flags & test_flags & POST_ALWAYS) &&
265 (flags & test_flags & POST_MEM)) {
266 WATCHDOG_RESET ();
267
268 if (!(flags & POST_REBOOT)) {
269 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100270 post_bootmode_test_on (
271 (gd->flags & GD_FLG_POSTFAIL) ?
272 POST_FAIL_SAVE | i : i);
wdenka042ac82002-09-12 22:36:57 +0000273 }
274
wdenk228f29a2002-12-08 09:53:23 +0000275 if (test_flags & POST_PREREL)
276 post_log_mark_start ( test->testid );
277 else
Michael Zaidmane070a562010-03-01 11:47:36 +0200278 post_log ("POST %s ", test->cmd);
wdenka042ac82002-09-12 22:36:57 +0000279 }
280
Michael Zaidmane070a562010-03-01 11:47:36 +0200281 show_post_progress(i, POST_BEFORE, POST_FAILED);
282
wdenk228f29a2002-12-08 09:53:23 +0000283 if (test_flags & POST_PREREL) {
Michael Zaidmane070a562010-03-01 11:47:36 +0200284 if ((*test->test) (flags) == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000285 post_log_mark_succ ( test->testid );
Michael Zaidmane070a562010-03-01 11:47:36 +0200286 show_post_progress(i, POST_AFTER, POST_PASSED);
287 }
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200288 else {
Michael Zaidmane070a562010-03-01 11:47:36 +0200289 show_post_progress(i, POST_AFTER, POST_FAILED);
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200290 if (test_flags & POST_CRITICAL)
291 gd->flags |= GD_FLG_POSTFAIL;
292 if (test_flags & POST_STOP)
293 gd->flags |= GD_FLG_POSTSTOP;
294 }
wdenk228f29a2002-12-08 09:53:23 +0000295 } else {
James Kosin975afc32011-07-14 08:15:06 +0000296 if ((*test->test)(flags) != 0) {
297 post_log("FAILED\n");
298 show_boot_progress(-32);
299 show_post_progress(i, POST_AFTER, POST_FAILED);
300 if (test_flags & POST_CRITICAL)
301 gd->flags |= GD_FLG_POSTFAIL;
302 if (test_flags & POST_STOP)
303 gd->flags |= GD_FLG_POSTSTOP;
304 } else {
305 post_log("PASSED\n");
306 show_post_progress(i, POST_AFTER, POST_PASSED);
307 }
wdenk228f29a2002-12-08 09:53:23 +0000308 }
wdenka042ac82002-09-12 22:36:57 +0000309
310 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
311 post_bootmode_test_off ();
312 }
313
314 return 0;
315 } else {
316 return -1;
317 }
318}
319
320int post_run (char *name, int flags)
321{
322 unsigned int i;
323 int test_flags[POST_MAX_NUMBER];
324
325 post_get_flags (test_flags);
326
327 if (name == NULL) {
328 unsigned int last;
329
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200330 if (gd->flags & GD_FLG_POSTSTOP)
331 return 0;
332
wdenka042ac82002-09-12 22:36:57 +0000333 if (post_bootmode_get (&last) & POST_POWERTEST) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100334 if (last & POST_FAIL_SAVE) {
335 last &= ~POST_FAIL_SAVE;
336 gd->flags |= GD_FLG_POSTFAIL;
337 }
wdenka042ac82002-09-12 22:36:57 +0000338 if (last < post_list_size &&
339 (flags & test_flags[last] & POST_ALWAYS) &&
340 (flags & test_flags[last] & POST_MEM)) {
341
wdenkea909b72002-11-21 23:11:29 +0000342 post_run_single (post_list + last,
343 test_flags[last],
344 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000345
346 for (i = last + 1; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200347 if (gd->flags & GD_FLG_POSTSTOP)
348 break;
wdenkea909b72002-11-21 23:11:29 +0000349 post_run_single (post_list + i,
350 test_flags[i],
351 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000352 }
353 }
354 } else {
355 for (i = 0; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200356 if (gd->flags & GD_FLG_POSTSTOP)
357 break;
wdenkea909b72002-11-21 23:11:29 +0000358 post_run_single (post_list + i,
359 test_flags[i],
360 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000361 }
362 }
363
364 return 0;
365 } else {
366 for (i = 0; i < post_list_size; i++) {
367 if (strcmp (post_list[i].cmd, name) == 0)
368 break;
369 }
370
371 if (i < post_list_size) {
Sascha Laue5744ddc2008-05-30 09:48:14 +0200372 WATCHDOG_RESET();
wdenka042ac82002-09-12 22:36:57 +0000373 return post_run_single (post_list + i,
374 test_flags[i],
375 flags, i);
376 } else {
377 return -1;
378 }
379 }
380}
381
382static int post_info_single (struct post_test *test, int full)
383{
384 if (test->flags & POST_MANUAL) {
385 if (full)
386 printf ("%s - %s\n"
387 " %s\n", test->cmd, test->name, test->desc);
388 else
389 printf (" %-15s - %s\n", test->cmd, test->name);
390
391 return 0;
392 } else {
393 return -1;
394 }
395}
396
397int post_info (char *name)
398{
399 unsigned int i;
400
401 if (name == NULL) {
402 for (i = 0; i < post_list_size; i++) {
403 post_info_single (post_list + i, 0);
404 }
405
406 return 0;
407 } else {
408 for (i = 0; i < post_list_size; i++) {
409 if (strcmp (post_list[i].cmd, name) == 0)
410 break;
411 }
412
413 if (i < post_list_size) {
414 return post_info_single (post_list + i, 1);
415 } else {
416 return -1;
417 }
418 }
419}
420
421int post_log (char *format, ...)
422{
423 va_list args;
424 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425 char printbuffer[CONFIG_SYS_PBSIZE];
wdenka042ac82002-09-12 22:36:57 +0000426
427 va_start (args, format);
428
429 /* For this to work, printbuffer must be larger than
430 * anything we ever want to print.
431 */
432 i = vsprintf (printbuffer, format, args);
433 va_end (args);
434
wdenk56f94be2002-11-05 16:35:14 +0000435#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000436 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000437 logbuff_log (printbuffer);
438#else
wdenka042ac82002-09-12 22:36:57 +0000439 /* Send to the stdout file */
440 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000441#endif
wdenka042ac82002-09-12 22:36:57 +0000442
443 return 0;
444}
445
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200446#ifdef CONFIG_NEEDS_MANUAL_RELOC
wdenka042ac82002-09-12 22:36:57 +0000447void post_reloc (void)
448{
wdenka042ac82002-09-12 22:36:57 +0000449 unsigned int i;
450
451 /*
452 * We have to relocate the test table manually
453 */
454 for (i = 0; i < post_list_size; i++) {
455 ulong addr;
456 struct post_test *test = post_list + i;
457
458 if (test->name) {
459 addr = (ulong) (test->name) + gd->reloc_off;
460 test->name = (char *) addr;
461 }
462
463 if (test->cmd) {
464 addr = (ulong) (test->cmd) + gd->reloc_off;
465 test->cmd = (char *) addr;
466 }
467
468 if (test->desc) {
469 addr = (ulong) (test->desc) + gd->reloc_off;
470 test->desc = (char *) addr;
471 }
472
473 if (test->test) {
474 addr = (ulong) (test->test) + gd->reloc_off;
475 test->test = (int (*)(int flags)) addr;
476 }
wdenk4532cb62003-04-27 22:52:51 +0000477
478 if (test->init_f) {
479 addr = (ulong) (test->init_f) + gd->reloc_off;
480 test->init_f = (int (*)(void)) addr;
481 }
482
483 if (test->reloc) {
484 addr = (ulong) (test->reloc) + gd->reloc_off;
485 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000486
wdenk4532cb62003-04-27 22:52:51 +0000487 test->reloc();
488 }
wdenka042ac82002-09-12 22:36:57 +0000489 }
490}
Peter Tyser521af042009-09-21 11:20:36 -0500491#endif
wdenka042ac82002-09-12 22:36:57 +0000492
wdenk4532cb62003-04-27 22:52:51 +0000493
494/*
495 * Some tests (e.g. SYSMON) need the time when post_init_f started,
496 * but we cannot use get_timer() at this point.
497 *
498 * On PowerPC we implement it using the timebase register.
499 */
500unsigned long post_time_ms (unsigned long base)
501{
502#ifdef CONFIG_PPC
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200503 return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
wdenk4532cb62003-04-27 22:52:51 +0000504#else
Wolfgang Denkad5bb452007-03-06 18:08:43 +0100505#warning "Not implemented yet"
wdenk4532cb62003-04-27 22:52:51 +0000506 return 0; /* Not implemented yet */
507#endif
508}