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