]> git.ipfire.org Git - people/ms/u-boot.git/blame - post/post.c
POST: allow redefinition of post_word_load/store
[people/ms/u-boot.git] / post / post.c
CommitLineData
a042ac84
WD
1/*
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>
52cb4d4f 25#include <stdio_dev.h>
a042ac84
WD
26#include <watchdog.h>
27#include <post.h>
28
9146d138
MF
29#ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
30#include <asm/gpio.h>
31#endif
32
56f94be3
WD
33#ifdef CONFIG_LOGBUFFER
34#include <logbuff.h>
35#endif
36
d87080b7
WD
37DECLARE_GLOBAL_DATA_PTR;
38
a042ac84
WD
39#define POST_MAX_NUMBER 32
40
41#define BOOTMODE_MAGIC 0xDEAD0000
42
4532cb69
WD
43int post_init_f (void)
44{
4532cb69
WD
45 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 }
8bde7f77 55
4532cb69
WD
56 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
39ff7d5f
SR
65/*
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{
9146d138
MF
75#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
39ff7d5f
SR
92 return 0; /* No hotkeys supported */
93}
94int post_hotkeys_pressed(void)
95 __attribute__((weak, alias("__post_hotkeys_pressed")));
96
97
a042ac84
WD
98void post_bootmode_init (void)
99{
100 int bootmode = post_bootmode_get (0);
27b207fd 101 int newword;
42d1f039 102
27b207fd
WD
103 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
104 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
6dff5529 105 } else if (bootmode == 0) {
27b207fd 106 newword = BOOTMODE_MAGIC | POST_POWERON;
6dff5529 107 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
27b207fd 108 newword = BOOTMODE_MAGIC | POST_NORMAL;
a042ac84 109 } else {
27b207fd
WD
110 /* Use old value */
111 newword = post_word_load () & ~POST_COLDBOOT;
a042ac84
WD
112 }
113
27b207fd
WD
114 if (bootmode == 0)
115 {
116 /* We are booting after power-on */
117 newword |= POST_COLDBOOT;
118 }
119
120 post_word_store (newword);
121
228f29ac
WD
122 /* Reset activity record */
123 gd->post_log_word = 0;
a042ac84
WD
124}
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
27b207fd 135 bootmode = word & 0x7F;
a042ac84
WD
136
137 if (last_test && (bootmode & POST_POWERTEST)) {
138 *last_test = (word >> 8) & 0xFF;
139 }
140
141 return bootmode;
142}
143
228f29ac
WD
144/* POST tests run before relocation only mark status bits .... */
145static void post_log_mark_start ( unsigned long testid )
146{
228f29ac
WD
147 gd->post_log_word |= (testid)<<16;
148}
149
150static void post_log_mark_succ ( unsigned long testid )
151{
228f29ac
WD
152 gd->post_log_word |= testid;
153}
154
155/* ... and the messages are output once we are relocated */
156void post_output_backlog ( void )
157{
228f29ac
WD
158 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");
63e73c9a 165 else {
228f29ac 166 post_log ("FAILED\n");
fad63407 167 show_boot_progress (-31);
63e73c9a 168 }
228f29ac
WD
169 }
170 }
171}
172
a042ac84
WD
173static 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{
e262efe3
YT
195 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
196 POST_CRITICAL };
197 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
198 "post_critical" };
d2397817 199 int varnum = ARRAY_SIZE(var);
a042ac84
WD
200 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++) {
cdb74977 211 if (getenv_f(var[i], list, sizeof (list)) <= 0)
a042ac84
WD
212 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 }
6dff5529
WD
247
248 for (j = 0; j < post_list_size; j++) {
249 if (test_flags[j] & POST_POWERON) {
250 test_flags[j] |= POST_SLOWTEST;
251 }
252 }
a042ac84
WD
253}
254
e070a56c
MZ
255void __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
a042ac84
WD
261static 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)) {
e262efe3
YT
270 post_bootmode_test_on (
271 (gd->flags & GD_FLG_POSTFAIL) ?
272 POST_FAIL_SAVE | i : i);
a042ac84
WD
273 }
274
228f29ac
WD
275 if (test_flags & POST_PREREL)
276 post_log_mark_start ( test->testid );
277 else
e070a56c 278 post_log ("POST %s ", test->cmd);
a042ac84
WD
279 }
280
e070a56c
MZ
281 show_post_progress(i, POST_BEFORE, POST_FAILED);
282
228f29ac 283 if (test_flags & POST_PREREL) {
e070a56c 284 if ((*test->test) (flags) == 0) {
228f29ac 285 post_log_mark_succ ( test->testid );
e070a56c
MZ
286 show_post_progress(i, POST_AFTER, POST_PASSED);
287 }
28a38506 288 else {
e070a56c 289 show_post_progress(i, POST_AFTER, POST_FAILED);
28a38506
YT
290 if (test_flags & POST_CRITICAL)
291 gd->flags |= GD_FLG_POSTFAIL;
292 if (test_flags & POST_STOP)
293 gd->flags |= GD_FLG_POSTSTOP;
294 }
228f29ac 295 } else {
975afc34
JK
296 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 }
228f29ac 308 }
a042ac84
WD
309
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
28a38506
YT
330 if (gd->flags & GD_FLG_POSTSTOP)
331 return 0;
332
a042ac84 333 if (post_bootmode_get (&last) & POST_POWERTEST) {
e262efe3
YT
334 if (last & POST_FAIL_SAVE) {
335 last &= ~POST_FAIL_SAVE;
336 gd->flags |= GD_FLG_POSTFAIL;
337 }
a042ac84
WD
338 if (last < post_list_size &&
339 (flags & test_flags[last] & POST_ALWAYS) &&
340 (flags & test_flags[last] & POST_MEM)) {
341
ea909b76
WD
342 post_run_single (post_list + last,
343 test_flags[last],
344 flags | POST_REBOOT, last);
a042ac84
WD
345
346 for (i = last + 1; i < post_list_size; i++) {
28a38506
YT
347 if (gd->flags & GD_FLG_POSTSTOP)
348 break;
ea909b76
WD
349 post_run_single (post_list + i,
350 test_flags[i],
351 flags, i);
a042ac84
WD
352 }
353 }
354 } else {
355 for (i = 0; i < post_list_size; i++) {
28a38506
YT
356 if (gd->flags & GD_FLG_POSTSTOP)
357 break;
ea909b76
WD
358 post_run_single (post_list + i,
359 test_flags[i],
360 flags, i);
a042ac84
WD
361 }
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) {
5744ddc6 372 WATCHDOG_RESET();
a042ac84
WD
373 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;
6d0f6bcf 425 char printbuffer[CONFIG_SYS_PBSIZE];
a042ac84
WD
426
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
56f94be3 435#ifdef CONFIG_LOGBUFFER
228f29ac 436 /* Send to the logbuffer */
56f94be3
WD
437 logbuff_log (printbuffer);
438#else
a042ac84
WD
439 /* Send to the stdout file */
440 puts (printbuffer);
56f94be3 441#endif
a042ac84
WD
442
443 return 0;
444}
445
2e5167cc 446#ifdef CONFIG_NEEDS_MANUAL_RELOC
a042ac84
WD
447void post_reloc (void)
448{
a042ac84
WD
449 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 }
4532cb69
WD
477
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;
8bde7f77 486
4532cb69
WD
487 test->reloc();
488 }
a042ac84
WD
489 }
490}
521af04d 491#endif
a042ac84 492
4532cb69
WD
493
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
6d0f6bcf 503 return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
4532cb69 504#else
ad5bb451 505#warning "Not implemented yet"
4532cb69
WD
506 return 0; /* Not implemented yet */
507#endif
508}