1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 #include <stdio_dev.h>
16 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
20 DECLARE_GLOBAL_DATA_PTR;
22 #define POST_MAX_NUMBER 32
24 #define BOOTMODE_MAGIC 0xDEAD0000
31 for (i = 0; i < post_list_size; i++) {
32 struct post_test *test = post_list + i;
34 if (test->init_f && test->init_f())
38 gd->post_init_f_time = post_time_ms(0);
39 if (!gd->post_init_f_time)
40 printf("%s: post_time_ms not implemented\n", __FILE__);
46 * Supply a default implementation for post_hotkeys_pressed() for boards
47 * without hotkey support. We always return 0 here, so that the
48 * long-running tests won't be started.
50 * Boards with hotkey support can override this weak default function
51 * by defining one in their board specific code.
53 __weak int post_hotkeys_pressed(void)
55 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
57 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
59 ret = gpio_request(gpio, "hotkeys");
61 printf("POST: gpio hotkey request failed\n");
65 gpio_direction_input(gpio);
66 ret = gpio_get_value(gpio);
72 return 0; /* No hotkeys supported */
75 void post_bootmode_init(void)
77 int bootmode = post_bootmode_get(0);
80 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
81 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
82 else if (bootmode == 0)
83 newword = BOOTMODE_MAGIC | POST_POWERON;
84 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
85 newword = BOOTMODE_MAGIC | POST_NORMAL;
88 newword = post_word_load() & ~POST_COLDBOOT;
91 /* We are booting after power-on */
92 newword |= POST_COLDBOOT;
94 post_word_store(newword);
96 /* Reset activity record */
97 gd->post_log_word = 0;
101 int post_bootmode_get(unsigned int *last_test)
103 unsigned long word = post_word_load();
106 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
109 bootmode = word & 0x7F;
111 if (last_test && (bootmode & POST_POWERTEST))
112 *last_test = (word >> 8) & 0xFF;
117 /* POST tests run before relocation only mark status bits .... */
118 static void post_log_mark_start(unsigned long testid)
120 gd->post_log_word |= testid;
123 static void post_log_mark_succ(unsigned long testid)
125 gd->post_log_res |= testid;
128 /* ... and the messages are output once we are relocated */
129 void post_output_backlog(void)
133 for (j = 0; j < post_list_size; j++) {
134 if (gd->post_log_word & (post_list[j].testid)) {
135 post_log("POST %s ", post_list[j].cmd);
136 if (gd->post_log_res & post_list[j].testid)
137 post_log("PASSED\n");
139 post_log("FAILED\n");
140 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
146 static void post_bootmode_test_on(unsigned int last_test)
148 unsigned long word = post_word_load();
150 word |= POST_POWERTEST;
152 word |= (last_test & 0xFF) << 8;
154 post_word_store(word);
157 static void post_bootmode_test_off(void)
159 unsigned long word = post_word_load();
161 word &= ~POST_POWERTEST;
163 post_word_store(word);
166 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
167 static void post_get_env_flags(int *test_flags)
169 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
171 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
173 int varnum = ARRAY_SIZE(var);
174 char list[128]; /* long enough for POST list */
180 for (i = 0; i < varnum; i++) {
181 if (env_get_f(var[i], list, sizeof(list)) <= 0)
184 for (j = 0; j < post_list_size; j++)
185 test_flags[j] &= ~flag[i];
190 while (*name && *name == ' ')
195 while (*s && *s != ' ')
202 for (j = 0; j < post_list_size; j++) {
203 if (strcmp(post_list[j].cmd, name) == 0) {
204 test_flags[j] |= flag[i];
209 if (j == post_list_size)
210 printf("No such test: %s\n", name);
218 static void post_get_flags(int *test_flags)
222 for (j = 0; j < post_list_size; j++)
223 test_flags[j] = post_list[j].flags;
225 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
226 post_get_env_flags(test_flags);
229 for (j = 0; j < post_list_size; j++)
230 if (test_flags[j] & POST_POWERON)
231 test_flags[j] |= POST_SLOWTEST;
234 __weak void show_post_progress(unsigned int test_num, int before, int result)
238 static int post_run_single(struct post_test *test,
239 int test_flags, int flags, unsigned int i)
241 if ((flags & test_flags & POST_ALWAYS) &&
242 (flags & test_flags & POST_MEM)) {
245 if (!(flags & POST_REBOOT)) {
246 if ((test_flags & POST_REBOOT) &&
247 !(flags & POST_MANUAL)) {
248 post_bootmode_test_on(
249 (gd->flags & GD_FLG_POSTFAIL) ?
250 POST_FAIL_SAVE | i : i);
253 if (test_flags & POST_PREREL)
254 post_log_mark_start(test->testid);
256 post_log("POST %s ", test->cmd);
259 show_post_progress(i, POST_BEFORE, POST_FAILED);
261 if (test_flags & POST_PREREL) {
262 if ((*test->test)(flags) == 0) {
263 post_log_mark_succ(test->testid);
264 show_post_progress(i, POST_AFTER, POST_PASSED);
266 show_post_progress(i, POST_AFTER, POST_FAILED);
267 if (test_flags & POST_CRITICAL)
268 gd->flags |= GD_FLG_POSTFAIL;
269 if (test_flags & POST_STOP)
270 gd->flags |= GD_FLG_POSTSTOP;
273 if ((*test->test)(flags) != 0) {
274 post_log("FAILED\n");
275 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
276 show_post_progress(i, POST_AFTER, POST_FAILED);
277 if (test_flags & POST_CRITICAL)
278 gd->flags |= GD_FLG_POSTFAIL;
279 if (test_flags & POST_STOP)
280 gd->flags |= GD_FLG_POSTSTOP;
282 post_log("PASSED\n");
283 show_post_progress(i, POST_AFTER, POST_PASSED);
287 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
288 post_bootmode_test_off();
296 int post_run(char *name, int flags)
299 int test_flags[POST_MAX_NUMBER];
301 post_get_flags(test_flags);
306 if (gd->flags & GD_FLG_POSTSTOP)
309 if (post_bootmode_get(&last) & POST_POWERTEST) {
310 if (last & POST_FAIL_SAVE) {
311 last &= ~POST_FAIL_SAVE;
312 gd->flags |= GD_FLG_POSTFAIL;
314 if (last < post_list_size &&
315 (flags & test_flags[last] & POST_ALWAYS) &&
316 (flags & test_flags[last] & POST_MEM)) {
318 post_run_single(post_list + last,
320 flags | POST_REBOOT, last);
322 for (i = last + 1; i < post_list_size; i++) {
323 if (gd->flags & GD_FLG_POSTSTOP)
325 post_run_single(post_list + i,
331 for (i = 0; i < post_list_size; i++) {
332 if (gd->flags & GD_FLG_POSTSTOP)
334 post_run_single(post_list + i,
342 for (i = 0; i < post_list_size; i++) {
343 if (strcmp(post_list[i].cmd, name) == 0)
347 if (i < post_list_size) {
349 return post_run_single(post_list + i,
358 static int post_info_single(struct post_test *test, int full)
360 if (test->flags & POST_MANUAL) {
363 " %s\n", test->cmd, test->name, test->desc);
365 printf(" %-15s - %s\n", test->cmd, test->name);
373 int post_info(char *name)
378 for (i = 0; i < post_list_size; i++)
379 post_info_single(post_list + i, 0);
383 for (i = 0; i < post_list_size; i++) {
384 if (strcmp(post_list[i].cmd, name) == 0)
388 if (i < post_list_size)
389 return post_info_single(post_list + i, 1);
395 int post_log(char *format, ...)
398 char printbuffer[CONFIG_SYS_PBSIZE];
400 va_start(args, format);
402 /* For this to work, printbuffer must be larger than
403 * anything we ever want to print.
405 vsprintf(printbuffer, format, args);
408 /* Send to the stdout file */
414 #ifdef CONFIG_NEEDS_MANUAL_RELOC
415 void post_reloc(void)
420 * We have to relocate the test table manually
422 for (i = 0; i < post_list_size; i++) {
424 struct post_test *test = post_list + i;
427 addr = (ulong)(test->name) + gd->reloc_off;
428 test->name = (char *)addr;
432 addr = (ulong)(test->cmd) + gd->reloc_off;
433 test->cmd = (char *)addr;
437 addr = (ulong)(test->desc) + gd->reloc_off;
438 test->desc = (char *)addr;
442 addr = (ulong)(test->test) + gd->reloc_off;
443 test->test = (int (*)(int flags)) addr;
447 addr = (ulong)(test->init_f) + gd->reloc_off;
448 test->init_f = (int (*)(void)) addr;
452 addr = (ulong)(test->reloc) + gd->reloc_off;
453 test->reloc = (void (*)(void)) addr;
463 * Some tests (e.g. SYSMON) need the time when post_init_f started,
464 * but we cannot use get_timer() at this point.
466 * On PowerPC we implement it using the timebase register.
468 unsigned long post_time_ms(unsigned long base)
470 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
471 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
474 #warning "Not implemented yet"
475 return 0; /* Not implemented yet */