]> git.ipfire.org Git - people/ms/u-boot.git/blame - post/post.c
bootstage: Define an optional microsecond timer
[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 26#include <watchdog.h>
c90a4dd7 27#include <div64.h>
a042ac84
WD
28#include <post.h>
29
9146d138
MF
30#ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
31#include <asm/gpio.h>
32#endif
33
56f94be3
WD
34#ifdef CONFIG_LOGBUFFER
35#include <logbuff.h>
36#endif
37
d87080b7
WD
38DECLARE_GLOBAL_DATA_PTR;
39
a042ac84
WD
40#define POST_MAX_NUMBER 32
41
42#define BOOTMODE_MAGIC 0xDEAD0000
43
e92372c8 44int post_init_f(void)
4532cb69 45{
4532cb69
WD
46 int res = 0;
47 unsigned int i;
48
49 for (i = 0; i < post_list_size; i++) {
50 struct post_test *test = post_list + i;
51
50da8376 52 if (test->init_f && test->init_f())
4532cb69 53 res = -1;
4532cb69 54 }
8bde7f77 55
4532cb69
WD
56 gd->post_init_f_time = post_time_ms(0);
57 if (!gd->post_init_f_time)
e92372c8 58 printf("%s: post_time_ms not implemented\n", __FILE__);
4532cb69
WD
59
60 return res;
61}
62
39ff7d5f
SR
63/*
64 * Supply a default implementation for post_hotkeys_pressed() for boards
65 * without hotkey support. We always return 0 here, so that the
66 * long-running tests won't be started.
67 *
68 * Boards with hotkey support can override this weak default function
69 * by defining one in their board specific code.
70 */
71int __post_hotkeys_pressed(void)
72{
9146d138
MF
73#ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
74 int ret;
75 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
76
77 ret = gpio_request(gpio, "hotkeys");
78 if (ret) {
79 printf("POST: gpio hotkey request failed\n");
80 return 0;
81 }
82
83 gpio_direction_input(gpio);
84 ret = gpio_get_value(gpio);
85 gpio_free(gpio);
86
87 return ret;
88#endif
89
39ff7d5f
SR
90 return 0; /* No hotkeys supported */
91}
92int post_hotkeys_pressed(void)
93 __attribute__((weak, alias("__post_hotkeys_pressed")));
94
95
e92372c8 96void post_bootmode_init(void)
a042ac84 97{
e92372c8 98 int bootmode = post_bootmode_get(0);
27b207fd 99 int newword;
42d1f039 100
e92372c8 101 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
27b207fd 102 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
e92372c8 103 else if (bootmode == 0)
27b207fd 104 newword = BOOTMODE_MAGIC | POST_POWERON;
e92372c8 105 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
27b207fd 106 newword = BOOTMODE_MAGIC | POST_NORMAL;
e92372c8 107 else
27b207fd 108 /* Use old value */
50da8376 109 newword = post_word_load() & ~POST_COLDBOOT;
a042ac84 110
27b207fd 111 if (bootmode == 0)
27b207fd
WD
112 /* We are booting after power-on */
113 newword |= POST_COLDBOOT;
27b207fd 114
e92372c8 115 post_word_store(newword);
27b207fd 116
228f29ac
WD
117 /* Reset activity record */
118 gd->post_log_word = 0;
79843950 119 gd->post_log_res = 0;
a042ac84
WD
120}
121
e92372c8 122int post_bootmode_get(unsigned int *last_test)
a042ac84 123{
e92372c8 124 unsigned long word = post_word_load();
a042ac84
WD
125 int bootmode;
126
e92372c8 127 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
a042ac84 128 return 0;
a042ac84 129
27b207fd 130 bootmode = word & 0x7F;
a042ac84 131
e92372c8 132 if (last_test && (bootmode & POST_POWERTEST))
a042ac84 133 *last_test = (word >> 8) & 0xFF;
a042ac84
WD
134
135 return bootmode;
136}
137
228f29ac 138/* POST tests run before relocation only mark status bits .... */
e92372c8 139static void post_log_mark_start(unsigned long testid)
228f29ac 140{
79843950 141 gd->post_log_word |= testid;
228f29ac
WD
142}
143
e92372c8 144static void post_log_mark_succ(unsigned long testid)
228f29ac 145{
79843950 146 gd->post_log_res |= testid;
228f29ac
WD
147}
148
149/* ... and the messages are output once we are relocated */
e92372c8 150void post_output_backlog(void)
228f29ac 151{
228f29ac
WD
152 int j;
153
154 for (j = 0; j < post_list_size; j++) {
79843950 155 if (gd->post_log_word & (post_list[j].testid)) {
50da8376 156 post_log("POST %s ", post_list[j].cmd);
79843950 157 if (gd->post_log_res & post_list[j].testid)
50da8376 158 post_log("PASSED\n");
63e73c9a 159 else {
e92372c8 160 post_log("FAILED\n");
8ade9506 161 show_boot_error(BOOTSTAGE_ID_POST_FAIL_R);
63e73c9a 162 }
228f29ac
WD
163 }
164 }
165}
166
e92372c8 167static void post_bootmode_test_on(unsigned int last_test)
a042ac84 168{
e92372c8 169 unsigned long word = post_word_load();
a042ac84
WD
170
171 word |= POST_POWERTEST;
172
173 word |= (last_test & 0xFF) << 8;
174
e92372c8 175 post_word_store(word);
a042ac84
WD
176}
177
e92372c8 178static void post_bootmode_test_off(void)
a042ac84 179{
e92372c8 180 unsigned long word = post_word_load();
a042ac84
WD
181
182 word &= ~POST_POWERTEST;
183
e92372c8 184 post_word_store(word);
a042ac84
WD
185}
186
212a0caf
VL
187#ifndef CONFIG_POST_SKIP_ENV_FLAGS
188static void post_get_env_flags(int *test_flags)
a042ac84 189{
e262efe3
YT
190 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
191 POST_CRITICAL };
192 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
193 "post_critical" };
d2397817 194 int varnum = ARRAY_SIZE(var);
a042ac84
WD
195 char list[128]; /* long enough for POST list */
196 char *name;
197 char *s;
198 int last;
199 int i, j;
200
a042ac84 201 for (i = 0; i < varnum; i++) {
50da8376 202 if (getenv_f(var[i], list, sizeof(list)) <= 0)
a042ac84
WD
203 continue;
204
50da8376 205 for (j = 0; j < post_list_size; j++)
a042ac84 206 test_flags[j] &= ~flag[i];
a042ac84
WD
207
208 last = 0;
209 name = list;
210 while (!last) {
211 while (*name && *name == ' ')
212 name++;
213 if (*name == 0)
214 break;
215 s = name + 1;
216 while (*s && *s != ' ')
217 s++;
218 if (*s == 0)
219 last = 1;
220 else
221 *s = 0;
222
223 for (j = 0; j < post_list_size; j++) {
50da8376 224 if (strcmp(post_list[j].cmd, name) == 0) {
a042ac84
WD
225 test_flags[j] |= flag[i];
226 break;
227 }
228 }
229
e92372c8 230 if (j == post_list_size)
50da8376 231 printf("No such test: %s\n", name);
a042ac84
WD
232
233 name = s + 1;
234 }
235 }
212a0caf
VL
236}
237#endif
238
239static void post_get_flags(int *test_flags)
240{
241 int j;
242
243 for (j = 0; j < post_list_size; j++)
244 test_flags[j] = post_list[j].flags;
245
246#ifndef CONFIG_POST_SKIP_ENV_FLAGS
247 post_get_env_flags(test_flags);
248#endif
6dff5529 249
e92372c8
HS
250 for (j = 0; j < post_list_size; j++)
251 if (test_flags[j] & POST_POWERON)
6dff5529 252 test_flags[j] |= POST_SLOWTEST;
a042ac84
WD
253}
254
e92372c8 255void __show_post_progress(unsigned int test_num, int before, int result)
e070a56c
MZ
256{
257}
e92372c8 258void show_post_progress(unsigned int, int, int)
e070a56c
MZ
259 __attribute__((weak, alias("__show_post_progress")));
260
e92372c8 261static int post_run_single(struct post_test *test,
a042ac84
WD
262 int test_flags, int flags, unsigned int i)
263{
264 if ((flags & test_flags & POST_ALWAYS) &&
265 (flags & test_flags & POST_MEM)) {
50da8376 266 WATCHDOG_RESET();
a042ac84
WD
267
268 if (!(flags & POST_REBOOT)) {
e92372c8
HS
269 if ((test_flags & POST_REBOOT) &&
270 !(flags & POST_MANUAL)) {
271 post_bootmode_test_on(
e262efe3
YT
272 (gd->flags & GD_FLG_POSTFAIL) ?
273 POST_FAIL_SAVE | i : i);
a042ac84
WD
274 }
275
228f29ac 276 if (test_flags & POST_PREREL)
e92372c8 277 post_log_mark_start(test->testid);
228f29ac 278 else
e92372c8 279 post_log("POST %s ", test->cmd);
a042ac84
WD
280 }
281
e070a56c
MZ
282 show_post_progress(i, POST_BEFORE, POST_FAILED);
283
228f29ac 284 if (test_flags & POST_PREREL) {
50da8376 285 if ((*test->test)(flags) == 0) {
e92372c8 286 post_log_mark_succ(test->testid);
e070a56c 287 show_post_progress(i, POST_AFTER, POST_PASSED);
50da8376 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");
8ade9506 298 show_boot_error(BOOTSTAGE_ID_POST_FAIL_R);
975afc34
JK
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 309
50da8376 310 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
e92372c8 311 post_bootmode_test_off();
a042ac84
WD
312
313 return 0;
314 } else {
315 return -1;
316 }
317}
318
50da8376 319int post_run(char *name, int flags)
a042ac84
WD
320{
321 unsigned int i;
322 int test_flags[POST_MAX_NUMBER];
323
e92372c8 324 post_get_flags(test_flags);
a042ac84
WD
325
326 if (name == NULL) {
327 unsigned int last;
328
28a38506
YT
329 if (gd->flags & GD_FLG_POSTSTOP)
330 return 0;
331
e92372c8 332 if (post_bootmode_get(&last) & POST_POWERTEST) {
e262efe3
YT
333 if (last & POST_FAIL_SAVE) {
334 last &= ~POST_FAIL_SAVE;
335 gd->flags |= GD_FLG_POSTFAIL;
336 }
a042ac84
WD
337 if (last < post_list_size &&
338 (flags & test_flags[last] & POST_ALWAYS) &&
339 (flags & test_flags[last] & POST_MEM)) {
340
e92372c8 341 post_run_single(post_list + last,
ea909b76
WD
342 test_flags[last],
343 flags | POST_REBOOT, last);
a042ac84
WD
344
345 for (i = last + 1; i < post_list_size; i++) {
28a38506
YT
346 if (gd->flags & GD_FLG_POSTSTOP)
347 break;
e92372c8 348 post_run_single(post_list + i,
ea909b76
WD
349 test_flags[i],
350 flags, i);
a042ac84
WD
351 }
352 }
353 } else {
354 for (i = 0; i < post_list_size; i++) {
28a38506
YT
355 if (gd->flags & GD_FLG_POSTSTOP)
356 break;
e92372c8 357 post_run_single(post_list + i,
ea909b76
WD
358 test_flags[i],
359 flags, i);
a042ac84
WD
360 }
361 }
362
363 return 0;
364 } else {
365 for (i = 0; i < post_list_size; i++) {
50da8376 366 if (strcmp(post_list[i].cmd, name) == 0)
a042ac84
WD
367 break;
368 }
369
370 if (i < post_list_size) {
5744ddc6 371 WATCHDOG_RESET();
e92372c8 372 return post_run_single(post_list + i,
a042ac84
WD
373 test_flags[i],
374 flags, i);
375 } else {
376 return -1;
377 }
378 }
379}
380
e92372c8 381static int post_info_single(struct post_test *test, int full)
a042ac84
WD
382{
383 if (test->flags & POST_MANUAL) {
384 if (full)
e92372c8 385 printf("%s - %s\n"
a042ac84
WD
386 " %s\n", test->cmd, test->name, test->desc);
387 else
e92372c8 388 printf(" %-15s - %s\n", test->cmd, test->name);
a042ac84
WD
389
390 return 0;
391 } else {
392 return -1;
393 }
394}
395
50da8376 396int post_info(char *name)
a042ac84
WD
397{
398 unsigned int i;
399
400 if (name == NULL) {
e92372c8
HS
401 for (i = 0; i < post_list_size; i++)
402 post_info_single(post_list + i, 0);
a042ac84
WD
403
404 return 0;
405 } else {
406 for (i = 0; i < post_list_size; i++) {
50da8376 407 if (strcmp(post_list[i].cmd, name) == 0)
a042ac84
WD
408 break;
409 }
410
50da8376 411 if (i < post_list_size)
e92372c8 412 return post_info_single(post_list + i, 1);
50da8376 413 else
a042ac84 414 return -1;
a042ac84
WD
415 }
416}
417
e92372c8 418int post_log(char *format, ...)
a042ac84
WD
419{
420 va_list args;
6d0f6bcf 421 char printbuffer[CONFIG_SYS_PBSIZE];
a042ac84 422
50da8376 423 va_start(args, format);
a042ac84
WD
424
425 /* For this to work, printbuffer must be larger than
426 * anything we ever want to print.
427 */
4d6402b0 428 vsprintf(printbuffer, format, args);
50da8376 429 va_end(args);
a042ac84 430
56f94be3 431#ifdef CONFIG_LOGBUFFER
228f29ac 432 /* Send to the logbuffer */
e92372c8 433 logbuff_log(printbuffer);
56f94be3 434#else
a042ac84 435 /* Send to the stdout file */
e92372c8 436 puts(printbuffer);
56f94be3 437#endif
a042ac84
WD
438
439 return 0;
440}
441
2e5167cc 442#ifdef CONFIG_NEEDS_MANUAL_RELOC
e92372c8 443void post_reloc(void)
a042ac84 444{
a042ac84
WD
445 unsigned int i;
446
447 /*
448 * We have to relocate the test table manually
449 */
450 for (i = 0; i < post_list_size; i++) {
451 ulong addr;
452 struct post_test *test = post_list + i;
453
454 if (test->name) {
50da8376 455 addr = (ulong)(test->name) + gd->reloc_off;
e92372c8 456 test->name = (char *)addr;
a042ac84
WD
457 }
458
459 if (test->cmd) {
50da8376 460 addr = (ulong)(test->cmd) + gd->reloc_off;
e92372c8 461 test->cmd = (char *)addr;
a042ac84
WD
462 }
463
464 if (test->desc) {
50da8376 465 addr = (ulong)(test->desc) + gd->reloc_off;
e92372c8 466 test->desc = (char *)addr;
a042ac84
WD
467 }
468
469 if (test->test) {
50da8376 470 addr = (ulong)(test->test) + gd->reloc_off;
a042ac84
WD
471 test->test = (int (*)(int flags)) addr;
472 }
4532cb69
WD
473
474 if (test->init_f) {
50da8376 475 addr = (ulong)(test->init_f) + gd->reloc_off;
4532cb69
WD
476 test->init_f = (int (*)(void)) addr;
477 }
478
479 if (test->reloc) {
50da8376 480 addr = (ulong)(test->reloc) + gd->reloc_off;
4532cb69 481 test->reloc = (void (*)(void)) addr;
8bde7f77 482
4532cb69
WD
483 test->reloc();
484 }
a042ac84
WD
485 }
486}
521af04d 487#endif
a042ac84 488
4532cb69
WD
489
490/*
491 * Some tests (e.g. SYSMON) need the time when post_init_f started,
492 * but we cannot use get_timer() at this point.
493 *
494 * On PowerPC we implement it using the timebase register.
495 */
e92372c8 496unsigned long post_time_ms(unsigned long base)
4532cb69 497{
23f1dded
MF
498#if defined(CONFIG_PPC) || defined(CONFIG_BLACKFIN) || \
499 (defined(CONFIG_ARM) && !defined(CONFIG_KIRKWOOD))
c90a4dd7 500 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
e92372c8 501 - base;
4532cb69 502#else
ad5bb451 503#warning "Not implemented yet"
4532cb69
WD
504 return 0; /* Not implemented yet */
505#endif
506}