1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #include <stdio_dev.h>
18 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
22 DECLARE_GLOBAL_DATA_PTR;
24 #define POST_MAX_NUMBER 32
26 #define BOOTMODE_MAGIC 0xDEAD0000
33 for (i = 0; i < post_list_size; i++) {
34 struct post_test *test = post_list + i;
36 if (test->init_f && test->init_f())
40 gd->post_init_f_time = post_time_ms(0);
41 if (!gd->post_init_f_time)
42 printf("%s: post_time_ms not implemented\n", __FILE__);
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.
52 * Boards with hotkey support can override this weak default function
53 * by defining one in their board specific code.
55 __weak int post_hotkeys_pressed(void)
57 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
59 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
61 ret = gpio_request(gpio, "hotkeys");
63 printf("POST: gpio hotkey request failed\n");
67 gpio_direction_input(gpio);
68 ret = gpio_get_value(gpio);
74 return 0; /* No hotkeys supported */
77 void post_bootmode_init(void)
79 int bootmode = post_bootmode_get(0);
82 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
83 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
84 else if (bootmode == 0)
85 newword = BOOTMODE_MAGIC | POST_POWERON;
86 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
87 newword = BOOTMODE_MAGIC | POST_NORMAL;
90 newword = post_word_load() & ~POST_COLDBOOT;
93 /* We are booting after power-on */
94 newword |= POST_COLDBOOT;
96 post_word_store(newword);
98 /* Reset activity record */
99 gd->post_log_word = 0;
100 gd->post_log_res = 0;
103 int post_bootmode_get(unsigned int *last_test)
105 unsigned long word = post_word_load();
108 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
111 bootmode = word & 0x7F;
113 if (last_test && (bootmode & POST_POWERTEST))
114 *last_test = (word >> 8) & 0xFF;
119 /* POST tests run before relocation only mark status bits .... */
120 static void post_log_mark_start(unsigned long testid)
122 gd->post_log_word |= testid;
125 static void post_log_mark_succ(unsigned long testid)
127 gd->post_log_res |= testid;
130 /* ... and the messages are output once we are relocated */
131 int post_output_backlog(void)
135 for (j = 0; j < post_list_size; j++) {
136 if (gd->post_log_word & (post_list[j].testid)) {
137 post_log("POST %s ", post_list[j].cmd);
138 if (gd->post_log_res & post_list[j].testid)
139 post_log("PASSED\n");
141 post_log("FAILED\n");
142 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
150 static void post_bootmode_test_on(unsigned int last_test)
152 unsigned long word = post_word_load();
154 word |= POST_POWERTEST;
156 word |= (last_test & 0xFF) << 8;
158 post_word_store(word);
161 static void post_bootmode_test_off(void)
163 unsigned long word = post_word_load();
165 word &= ~POST_POWERTEST;
167 post_word_store(word);
170 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
171 static void post_get_env_flags(int *test_flags)
173 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
175 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
177 int varnum = ARRAY_SIZE(var);
178 char list[128]; /* long enough for POST list */
184 for (i = 0; i < varnum; i++) {
185 if (env_get_f(var[i], list, sizeof(list)) <= 0)
188 for (j = 0; j < post_list_size; j++)
189 test_flags[j] &= ~flag[i];
199 while (*s && *s != ' ')
206 for (j = 0; j < post_list_size; j++) {
207 if (strcmp(post_list[j].cmd, name) == 0) {
208 test_flags[j] |= flag[i];
213 if (j == post_list_size)
214 printf("No such test: %s\n", name);
222 static void post_get_flags(int *test_flags)
226 for (j = 0; j < post_list_size; j++)
227 test_flags[j] = post_list[j].flags;
229 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
230 post_get_env_flags(test_flags);
233 for (j = 0; j < post_list_size; j++)
234 if (test_flags[j] & POST_POWERON)
235 test_flags[j] |= POST_SLOWTEST;
238 __weak void show_post_progress(unsigned int test_num, int before, int result)
242 static int post_run_single(struct post_test *test,
243 int test_flags, int flags, unsigned int i)
245 if ((flags & test_flags & POST_ALWAYS) &&
246 (flags & test_flags & POST_MEM)) {
249 if (!(flags & POST_REBOOT)) {
250 if ((test_flags & POST_REBOOT) &&
251 !(flags & POST_MANUAL)) {
252 post_bootmode_test_on(
253 (gd->flags & GD_FLG_POSTFAIL) ?
254 POST_FAIL_SAVE | i : i);
257 if (test_flags & POST_PREREL)
258 post_log_mark_start(test->testid);
260 post_log("POST %s ", test->cmd);
263 show_post_progress(i, POST_BEFORE, POST_FAILED);
265 if (test_flags & POST_PREREL) {
266 if ((*test->test)(flags) == 0) {
267 post_log_mark_succ(test->testid);
268 show_post_progress(i, POST_AFTER, POST_PASSED);
270 show_post_progress(i, POST_AFTER, POST_FAILED);
271 if (test_flags & POST_CRITICAL)
272 gd->flags |= GD_FLG_POSTFAIL;
273 if (test_flags & POST_STOP)
274 gd->flags |= GD_FLG_POSTSTOP;
277 if ((*test->test)(flags) != 0) {
278 post_log("FAILED\n");
279 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
280 show_post_progress(i, POST_AFTER, POST_FAILED);
281 if (test_flags & POST_CRITICAL)
282 gd->flags |= GD_FLG_POSTFAIL;
283 if (test_flags & POST_STOP)
284 gd->flags |= GD_FLG_POSTSTOP;
286 post_log("PASSED\n");
287 show_post_progress(i, POST_AFTER, POST_PASSED);
291 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
292 post_bootmode_test_off();
300 int post_run(char *name, int flags)
303 int test_flags[POST_MAX_NUMBER];
305 post_get_flags(test_flags);
310 if (gd->flags & GD_FLG_POSTSTOP)
313 if (post_bootmode_get(&last) & POST_POWERTEST) {
314 if (last & POST_FAIL_SAVE) {
315 last &= ~POST_FAIL_SAVE;
316 gd->flags |= GD_FLG_POSTFAIL;
318 if (last < post_list_size &&
319 (flags & test_flags[last] & POST_ALWAYS) &&
320 (flags & test_flags[last] & POST_MEM)) {
322 post_run_single(post_list + last,
324 flags | POST_REBOOT, last);
326 for (i = last + 1; i < post_list_size; i++) {
327 if (gd->flags & GD_FLG_POSTSTOP)
329 post_run_single(post_list + i,
335 for (i = 0; i < post_list_size; i++) {
336 if (gd->flags & GD_FLG_POSTSTOP)
338 post_run_single(post_list + i,
346 for (i = 0; i < post_list_size; i++) {
347 if (strcmp(post_list[i].cmd, name) == 0)
351 if (i < post_list_size) {
353 return post_run_single(post_list + i,
362 static int post_info_single(struct post_test *test, int full)
364 if (test->flags & POST_MANUAL) {
367 " %s\n", test->cmd, test->name, test->desc);
369 printf(" %-15s - %s\n", test->cmd, test->name);
377 int post_info(char *name)
382 for (i = 0; i < post_list_size; i++)
383 post_info_single(post_list + i, 0);
387 for (i = 0; i < post_list_size; i++) {
388 if (strcmp(post_list[i].cmd, name) == 0)
392 if (i < post_list_size)
393 return post_info_single(post_list + i, 1);
399 int post_log(char *format, ...)
402 char printbuffer[CONFIG_SYS_PBSIZE];
404 va_start(args, format);
406 /* For this to work, printbuffer must be larger than
407 * anything we ever want to print.
409 vsprintf(printbuffer, format, args);
412 /* Send to the stdout file */
418 #ifdef CONFIG_NEEDS_MANUAL_RELOC
419 void post_reloc(void)
424 * We have to relocate the test table manually
426 for (i = 0; i < post_list_size; i++) {
428 struct post_test *test = post_list + i;
431 addr = (ulong)(test->name) + gd->reloc_off;
432 test->name = (char *)addr;
436 addr = (ulong)(test->cmd) + gd->reloc_off;
437 test->cmd = (char *)addr;
441 addr = (ulong)(test->desc) + gd->reloc_off;
442 test->desc = (char *)addr;
446 addr = (ulong)(test->test) + gd->reloc_off;
447 test->test = (int (*)(int flags)) addr;
451 addr = (ulong)(test->init_f) + gd->reloc_off;
452 test->init_f = (int (*)(void)) addr;
456 addr = (ulong)(test->reloc) + gd->reloc_off;
457 test->reloc = (void (*)(void)) addr;
467 * Some tests (e.g. SYSMON) need the time when post_init_f started,
468 * but we cannot use get_timer() at this point.
470 * On PowerPC we implement it using the timebase register.
472 unsigned long post_time_ms(unsigned long base)
474 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
475 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
478 #warning "Not implemented yet"
479 return 0; /* Not implemented yet */