blob: b29eb87fc2113701ca8c92618b62bdf568ee8e24 [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
wdenk56f94be2002-11-05 16:35:14 +000029#ifdef CONFIG_LOGBUFFER
30#include <logbuff.h>
31#endif
32
Wolfgang Denkd87080b2006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenka042ac82002-09-12 22:36:57 +000035#define POST_MAX_NUMBER 32
36
37#define BOOTMODE_MAGIC 0xDEAD0000
38
wdenk4532cb62003-04-27 22:52:51 +000039int post_init_f (void)
40{
wdenk4532cb62003-04-27 22:52:51 +000041 int res = 0;
42 unsigned int i;
43
44 for (i = 0; i < post_list_size; i++) {
45 struct post_test *test = post_list + i;
46
47 if (test->init_f && test->init_f()) {
48 res = -1;
49 }
50 }
wdenk8bde7f72003-06-27 21:31:46 +000051
wdenk4532cb62003-04-27 22:52:51 +000052 gd->post_init_f_time = post_time_ms(0);
53 if (!gd->post_init_f_time)
54 {
55 printf("post/post.c: post_time_ms seems not to be implemented\n");
56 }
57
58 return res;
59}
60
Stefan Roese39ff7d52009-12-03 06:24:30 +010061/*
62 * Supply a default implementation for post_hotkeys_pressed() for boards
63 * without hotkey support. We always return 0 here, so that the
64 * long-running tests won't be started.
65 *
66 * Boards with hotkey support can override this weak default function
67 * by defining one in their board specific code.
68 */
69int __post_hotkeys_pressed(void)
70{
71 return 0; /* No hotkeys supported */
72}
73int post_hotkeys_pressed(void)
74 __attribute__((weak, alias("__post_hotkeys_pressed")));
75
76
wdenka042ac82002-09-12 22:36:57 +000077void post_bootmode_init (void)
78{
79 int bootmode = post_bootmode_get (0);
wdenk27b207f2003-07-24 23:38:38 +000080 int newword;
wdenk42d1f032003-10-15 23:53:47 +000081
wdenk27b207f2003-07-24 23:38:38 +000082 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
83 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
wdenk6dff5522003-07-15 07:45:49 +000084 } else if (bootmode == 0) {
wdenk27b207f2003-07-24 23:38:38 +000085 newword = BOOTMODE_MAGIC | POST_POWERON;
wdenk6dff5522003-07-15 07:45:49 +000086 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
wdenk27b207f2003-07-24 23:38:38 +000087 newword = BOOTMODE_MAGIC | POST_NORMAL;
wdenka042ac82002-09-12 22:36:57 +000088 } else {
wdenk27b207f2003-07-24 23:38:38 +000089 /* Use old value */
90 newword = post_word_load () & ~POST_COLDBOOT;
wdenka042ac82002-09-12 22:36:57 +000091 }
92
wdenk27b207f2003-07-24 23:38:38 +000093 if (bootmode == 0)
94 {
95 /* We are booting after power-on */
96 newword |= POST_COLDBOOT;
97 }
98
99 post_word_store (newword);
100
wdenk228f29a2002-12-08 09:53:23 +0000101 /* Reset activity record */
102 gd->post_log_word = 0;
wdenka042ac82002-09-12 22:36:57 +0000103}
104
105int post_bootmode_get (unsigned int *last_test)
106{
107 unsigned long word = post_word_load ();
108 int bootmode;
109
110 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
111 return 0;
112 }
113
wdenk27b207f2003-07-24 23:38:38 +0000114 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +0000115
116 if (last_test && (bootmode & POST_POWERTEST)) {
117 *last_test = (word >> 8) & 0xFF;
118 }
119
120 return bootmode;
121}
122
wdenk228f29a2002-12-08 09:53:23 +0000123/* POST tests run before relocation only mark status bits .... */
124static void post_log_mark_start ( unsigned long testid )
125{
wdenk228f29a2002-12-08 09:53:23 +0000126 gd->post_log_word |= (testid)<<16;
127}
128
129static void post_log_mark_succ ( unsigned long testid )
130{
wdenk228f29a2002-12-08 09:53:23 +0000131 gd->post_log_word |= testid;
132}
133
134/* ... and the messages are output once we are relocated */
135void post_output_backlog ( void )
136{
wdenk228f29a2002-12-08 09:53:23 +0000137 int j;
138
139 for (j = 0; j < post_list_size; j++) {
140 if (gd->post_log_word & (post_list[j].testid<<16)) {
141 post_log ("POST %s ", post_list[j].cmd);
142 if (gd->post_log_word & post_list[j].testid)
143 post_log ("PASSED\n");
wdenk63e73c92004-02-23 22:22:28 +0000144 else {
wdenk228f29a2002-12-08 09:53:23 +0000145 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200146 show_boot_progress (-31);
wdenk63e73c92004-02-23 22:22:28 +0000147 }
wdenk228f29a2002-12-08 09:53:23 +0000148 }
149 }
150}
151
wdenka042ac82002-09-12 22:36:57 +0000152static void post_bootmode_test_on (unsigned int last_test)
153{
154 unsigned long word = post_word_load ();
155
156 word |= POST_POWERTEST;
157
158 word |= (last_test & 0xFF) << 8;
159
160 post_word_store (word);
161}
162
163static void post_bootmode_test_off (void)
164{
165 unsigned long word = post_word_load ();
166
167 word &= ~POST_POWERTEST;
168
169 post_word_store (word);
170}
171
172static void post_get_flags (int *test_flags)
173{
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100174 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
175 POST_CRITICAL };
176 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
177 "post_critical" };
wdenka042ac82002-09-12 22:36:57 +0000178 int varnum = sizeof (var) / sizeof (var[0]);
179 char list[128]; /* long enough for POST list */
180 char *name;
181 char *s;
182 int last;
183 int i, j;
184
185 for (j = 0; j < post_list_size; j++) {
186 test_flags[j] = post_list[j].flags;
187 }
188
189 for (i = 0; i < varnum; i++) {
190 if (getenv_r (var[i], list, sizeof (list)) <= 0)
191 continue;
192
193 for (j = 0; j < post_list_size; j++) {
194 test_flags[j] &= ~flag[i];
195 }
196
197 last = 0;
198 name = list;
199 while (!last) {
200 while (*name && *name == ' ')
201 name++;
202 if (*name == 0)
203 break;
204 s = name + 1;
205 while (*s && *s != ' ')
206 s++;
207 if (*s == 0)
208 last = 1;
209 else
210 *s = 0;
211
212 for (j = 0; j < post_list_size; j++) {
213 if (strcmp (post_list[j].cmd, name) == 0) {
214 test_flags[j] |= flag[i];
215 break;
216 }
217 }
218
219 if (j == post_list_size) {
220 printf ("No such test: %s\n", name);
221 }
222
223 name = s + 1;
224 }
225 }
wdenk6dff5522003-07-15 07:45:49 +0000226
227 for (j = 0; j < post_list_size; j++) {
228 if (test_flags[j] & POST_POWERON) {
229 test_flags[j] |= POST_SLOWTEST;
230 }
231 }
wdenka042ac82002-09-12 22:36:57 +0000232}
233
234static int post_run_single (struct post_test *test,
235 int test_flags, int flags, unsigned int i)
236{
237 if ((flags & test_flags & POST_ALWAYS) &&
238 (flags & test_flags & POST_MEM)) {
239 WATCHDOG_RESET ();
240
241 if (!(flags & POST_REBOOT)) {
242 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100243 post_bootmode_test_on (
244 (gd->flags & GD_FLG_POSTFAIL) ?
245 POST_FAIL_SAVE | i : i);
wdenka042ac82002-09-12 22:36:57 +0000246 }
247
wdenk228f29a2002-12-08 09:53:23 +0000248 if (test_flags & POST_PREREL)
249 post_log_mark_start ( test->testid );
250 else
wdenk56f94be2002-11-05 16:35:14 +0000251 post_log ("POST %s ", test->cmd);
wdenka042ac82002-09-12 22:36:57 +0000252 }
253
wdenk228f29a2002-12-08 09:53:23 +0000254 if (test_flags & POST_PREREL) {
255 if ((*test->test) (flags) == 0)
256 post_log_mark_succ ( test->testid );
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200257 else {
258 if (test_flags & POST_CRITICAL)
259 gd->flags |= GD_FLG_POSTFAIL;
260 if (test_flags & POST_STOP)
261 gd->flags |= GD_FLG_POSTSTOP;
262 }
wdenk228f29a2002-12-08 09:53:23 +0000263 } else {
wdenk63e73c92004-02-23 22:22:28 +0000264 if ((*test->test) (flags) != 0) {
wdenka042ac82002-09-12 22:36:57 +0000265 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200266 show_boot_progress (-32);
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100267 if (test_flags & POST_CRITICAL)
268 gd->flags |= GD_FLG_POSTFAIL;
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200269 if (test_flags & POST_STOP)
270 gd->flags |= GD_FLG_POSTSTOP;
wdenk63e73c92004-02-23 22:22:28 +0000271 }
wdenka042ac82002-09-12 22:36:57 +0000272 else
273 post_log ("PASSED\n");
wdenk228f29a2002-12-08 09:53:23 +0000274 }
wdenka042ac82002-09-12 22:36:57 +0000275
276 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
277 post_bootmode_test_off ();
278 }
279
280 return 0;
281 } else {
282 return -1;
283 }
284}
285
286int post_run (char *name, int flags)
287{
288 unsigned int i;
289 int test_flags[POST_MAX_NUMBER];
290
291 post_get_flags (test_flags);
292
293 if (name == NULL) {
294 unsigned int last;
295
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200296 if (gd->flags & GD_FLG_POSTSTOP)
297 return 0;
298
wdenka042ac82002-09-12 22:36:57 +0000299 if (post_bootmode_get (&last) & POST_POWERTEST) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100300 if (last & POST_FAIL_SAVE) {
301 last &= ~POST_FAIL_SAVE;
302 gd->flags |= GD_FLG_POSTFAIL;
303 }
wdenka042ac82002-09-12 22:36:57 +0000304 if (last < post_list_size &&
305 (flags & test_flags[last] & POST_ALWAYS) &&
306 (flags & test_flags[last] & POST_MEM)) {
307
wdenkea909b72002-11-21 23:11:29 +0000308 post_run_single (post_list + last,
309 test_flags[last],
310 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000311
312 for (i = last + 1; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200313 if (gd->flags & GD_FLG_POSTSTOP)
314 break;
wdenkea909b72002-11-21 23:11:29 +0000315 post_run_single (post_list + i,
316 test_flags[i],
317 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000318 }
319 }
320 } else {
321 for (i = 0; i < post_list_size; i++) {
Yuri Tikhonov28a38502008-05-08 15:45:26 +0200322 if (gd->flags & GD_FLG_POSTSTOP)
323 break;
wdenkea909b72002-11-21 23:11:29 +0000324 post_run_single (post_list + i,
325 test_flags[i],
326 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000327 }
328 }
329
330 return 0;
331 } else {
332 for (i = 0; i < post_list_size; i++) {
333 if (strcmp (post_list[i].cmd, name) == 0)
334 break;
335 }
336
337 if (i < post_list_size) {
Sascha Laue5744ddc2008-05-30 09:48:14 +0200338 WATCHDOG_RESET();
wdenka042ac82002-09-12 22:36:57 +0000339 return post_run_single (post_list + i,
340 test_flags[i],
341 flags, i);
342 } else {
343 return -1;
344 }
345 }
346}
347
348static int post_info_single (struct post_test *test, int full)
349{
350 if (test->flags & POST_MANUAL) {
351 if (full)
352 printf ("%s - %s\n"
353 " %s\n", test->cmd, test->name, test->desc);
354 else
355 printf (" %-15s - %s\n", test->cmd, test->name);
356
357 return 0;
358 } else {
359 return -1;
360 }
361}
362
363int post_info (char *name)
364{
365 unsigned int i;
366
367 if (name == NULL) {
368 for (i = 0; i < post_list_size; i++) {
369 post_info_single (post_list + i, 0);
370 }
371
372 return 0;
373 } else {
374 for (i = 0; i < post_list_size; i++) {
375 if (strcmp (post_list[i].cmd, name) == 0)
376 break;
377 }
378
379 if (i < post_list_size) {
380 return post_info_single (post_list + i, 1);
381 } else {
382 return -1;
383 }
384 }
385}
386
387int post_log (char *format, ...)
388{
389 va_list args;
390 uint i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 char printbuffer[CONFIG_SYS_PBSIZE];
wdenka042ac82002-09-12 22:36:57 +0000392
393 va_start (args, format);
394
395 /* For this to work, printbuffer must be larger than
396 * anything we ever want to print.
397 */
398 i = vsprintf (printbuffer, format, args);
399 va_end (args);
400
wdenk56f94be2002-11-05 16:35:14 +0000401#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000402 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000403 logbuff_log (printbuffer);
404#else
wdenka042ac82002-09-12 22:36:57 +0000405 /* Send to the stdout file */
406 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000407#endif
wdenka042ac82002-09-12 22:36:57 +0000408
409 return 0;
410}
411
Peter Tyser521af042009-09-21 11:20:36 -0500412#ifndef CONFIG_RELOC_FIXUP_WORKS
wdenka042ac82002-09-12 22:36:57 +0000413void post_reloc (void)
414{
wdenka042ac82002-09-12 22:36:57 +0000415 unsigned int i;
416
417 /*
418 * We have to relocate the test table manually
419 */
420 for (i = 0; i < post_list_size; i++) {
421 ulong addr;
422 struct post_test *test = post_list + i;
423
424 if (test->name) {
425 addr = (ulong) (test->name) + gd->reloc_off;
426 test->name = (char *) addr;
427 }
428
429 if (test->cmd) {
430 addr = (ulong) (test->cmd) + gd->reloc_off;
431 test->cmd = (char *) addr;
432 }
433
434 if (test->desc) {
435 addr = (ulong) (test->desc) + gd->reloc_off;
436 test->desc = (char *) addr;
437 }
438
439 if (test->test) {
440 addr = (ulong) (test->test) + gd->reloc_off;
441 test->test = (int (*)(int flags)) addr;
442 }
wdenk4532cb62003-04-27 22:52:51 +0000443
444 if (test->init_f) {
445 addr = (ulong) (test->init_f) + gd->reloc_off;
446 test->init_f = (int (*)(void)) addr;
447 }
448
449 if (test->reloc) {
450 addr = (ulong) (test->reloc) + gd->reloc_off;
451 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000452
wdenk4532cb62003-04-27 22:52:51 +0000453 test->reloc();
454 }
wdenka042ac82002-09-12 22:36:57 +0000455 }
456}
Peter Tyser521af042009-09-21 11:20:36 -0500457#endif
wdenka042ac82002-09-12 22:36:57 +0000458
wdenk4532cb62003-04-27 22:52:51 +0000459
460/*
461 * Some tests (e.g. SYSMON) need the time when post_init_f started,
462 * but we cannot use get_timer() at this point.
463 *
464 * On PowerPC we implement it using the timebase register.
465 */
466unsigned long post_time_ms (unsigned long base)
467{
468#ifdef CONFIG_PPC
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200469 return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
wdenk4532cb62003-04-27 22:52:51 +0000470#else
Wolfgang Denkad5bb452007-03-06 18:08:43 +0100471#warning "Not implemented yet"
wdenk4532cb62003-04-27 22:52:51 +0000472 return 0; /* Not implemented yet */
473#endif
474}