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