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