1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #include <stdio_dev.h>
17 #include <asm/global_data.h>
19 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
23 DECLARE_GLOBAL_DATA_PTR;
25 #define POST_MAX_NUMBER 32
27 #define BOOTMODE_MAGIC 0xDEAD0000
34 for (i = 0; i < post_list_size; i++) {
35 struct post_test *test = post_list + i;
37 if (test->init_f && test->init_f())
41 gd->post_init_f_time = post_time_ms(0);
42 if (!gd->post_init_f_time)
43 printf("%s: post_time_ms not implemented\n", __FILE__);
49 * Supply a default implementation for post_hotkeys_pressed() for boards
50 * without hotkey support. We always return 0 here, so that the
51 * long-running tests won't be started.
53 * Boards with hotkey support can override this weak default function
54 * by defining one in their board specific code.
56 __weak int post_hotkeys_pressed(void)
58 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
60 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
62 ret = gpio_request(gpio, "hotkeys");
64 printf("POST: gpio hotkey request failed\n");
68 gpio_direction_input(gpio);
69 ret = gpio_get_value(gpio);
75 return 0; /* No hotkeys supported */
78 void post_bootmode_init(void)
80 int bootmode = post_bootmode_get(0);
83 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
84 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
85 else if (bootmode == 0)
86 newword = BOOTMODE_MAGIC | POST_POWERON;
87 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
88 newword = BOOTMODE_MAGIC | POST_NORMAL;
91 newword = post_word_load() & ~POST_COLDBOOT;
94 /* We are booting after power-on */
95 newword |= POST_COLDBOOT;
97 post_word_store(newword);
99 /* Reset activity record */
100 gd->post_log_word = 0;
101 gd->post_log_res = 0;
104 int post_bootmode_get(unsigned int *last_test)
106 unsigned long word = post_word_load();
109 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
112 bootmode = word & 0x7F;
114 if (last_test && (bootmode & POST_POWERTEST))
115 *last_test = (word >> 8) & 0xFF;
120 /* POST tests run before relocation only mark status bits .... */
121 static void post_log_mark_start(unsigned long testid)
123 gd->post_log_word |= testid;
126 static void post_log_mark_succ(unsigned long testid)
128 gd->post_log_res |= testid;
131 /* ... and the messages are output once we are relocated */
132 int post_output_backlog(void)
136 for (j = 0; j < post_list_size; j++) {
137 if (gd->post_log_word & (post_list[j].testid)) {
138 post_log("POST %s ", post_list[j].cmd);
139 if (gd->post_log_res & post_list[j].testid)
140 post_log("PASSED\n");
142 post_log("FAILED\n");
143 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
151 static void post_bootmode_test_on(unsigned int last_test)
153 unsigned long word = post_word_load();
155 word |= POST_POWERTEST;
157 word |= (last_test & 0xFF) << 8;
159 post_word_store(word);
162 static void post_bootmode_test_off(void)
164 unsigned long word = post_word_load();
166 word &= ~POST_POWERTEST;
168 post_word_store(word);
171 #ifndef CFG_POST_SKIP_ENV_FLAGS
172 static void post_get_env_flags(int *test_flags)
174 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
176 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
178 int varnum = ARRAY_SIZE(var);
179 char list[128]; /* long enough for POST list */
185 for (i = 0; i < varnum; i++) {
186 if (env_get_f(var[i], list, sizeof(list)) <= 0)
189 for (j = 0; j < post_list_size; j++)
190 test_flags[j] &= ~flag[i];
200 while (*s && *s != ' ')
207 for (j = 0; j < post_list_size; j++) {
208 if (strcmp(post_list[j].cmd, name) == 0) {
209 test_flags[j] |= flag[i];
214 if (j == post_list_size)
215 printf("No such test: %s\n", name);
223 static void post_get_flags(int *test_flags)
227 for (j = 0; j < post_list_size; j++)
228 test_flags[j] = post_list[j].flags;
230 #ifndef CFG_POST_SKIP_ENV_FLAGS
231 post_get_env_flags(test_flags);
234 for (j = 0; j < post_list_size; j++)
235 if (test_flags[j] & POST_POWERON)
236 test_flags[j] |= POST_SLOWTEST;
239 __weak void show_post_progress(unsigned int test_num, int before, int result)
243 static int post_run_single(struct post_test *test,
244 int test_flags, int flags, unsigned int i)
246 if ((flags & test_flags & POST_ALWAYS) &&
247 (flags & test_flags & POST_MEM)) {
250 if (!(flags & POST_REBOOT)) {
251 if ((test_flags & POST_REBOOT) &&
252 !(flags & POST_MANUAL)) {
253 post_bootmode_test_on(
254 (gd->flags & GD_FLG_POSTFAIL) ?
255 POST_FAIL_SAVE | i : i);
258 if (test_flags & POST_PREREL)
259 post_log_mark_start(test->testid);
261 post_log("POST %s ", test->cmd);
264 show_post_progress(i, POST_BEFORE, POST_FAILED);
266 if (test_flags & POST_PREREL) {
267 if ((*test->test)(flags) == 0) {
268 post_log_mark_succ(test->testid);
269 show_post_progress(i, POST_AFTER, POST_PASSED);
271 show_post_progress(i, POST_AFTER, POST_FAILED);
272 if (test_flags & POST_CRITICAL)
273 gd->flags |= GD_FLG_POSTFAIL;
274 if (test_flags & POST_STOP)
275 gd->flags |= GD_FLG_POSTSTOP;
278 if ((*test->test)(flags) != 0) {
279 post_log("FAILED\n");
280 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
281 show_post_progress(i, POST_AFTER, POST_FAILED);
282 if (test_flags & POST_CRITICAL)
283 gd->flags |= GD_FLG_POSTFAIL;
284 if (test_flags & POST_STOP)
285 gd->flags |= GD_FLG_POSTSTOP;
287 post_log("PASSED\n");
288 show_post_progress(i, POST_AFTER, POST_PASSED);
292 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
293 post_bootmode_test_off();
301 int post_run(char *name, int flags)
304 int test_flags[POST_MAX_NUMBER];
306 post_get_flags(test_flags);
311 if (gd->flags & GD_FLG_POSTSTOP)
314 if (post_bootmode_get(&last) & POST_POWERTEST) {
315 if (last & POST_FAIL_SAVE) {
316 last &= ~POST_FAIL_SAVE;
317 gd->flags |= GD_FLG_POSTFAIL;
319 if (last < post_list_size &&
320 (flags & test_flags[last] & POST_ALWAYS) &&
321 (flags & test_flags[last] & POST_MEM)) {
323 post_run_single(post_list + last,
325 flags | POST_REBOOT, last);
327 for (i = last + 1; i < post_list_size; i++) {
328 if (gd->flags & GD_FLG_POSTSTOP)
330 post_run_single(post_list + i,
336 for (i = 0; i < post_list_size; i++) {
337 if (gd->flags & GD_FLG_POSTSTOP)
339 post_run_single(post_list + i,
347 for (i = 0; i < post_list_size; i++) {
348 if (strcmp(post_list[i].cmd, name) == 0)
352 if (i < post_list_size) {
354 return post_run_single(post_list + i,
363 static int post_info_single(struct post_test *test, int full)
365 if (test->flags & POST_MANUAL) {
368 " %s\n", test->cmd, test->name, test->desc);
370 printf(" %-15s - %s\n", test->cmd, test->name);
378 int post_info(char *name)
383 for (i = 0; i < post_list_size; i++)
384 post_info_single(post_list + i, 0);
388 for (i = 0; i < post_list_size; i++) {
389 if (strcmp(post_list[i].cmd, name) == 0)
393 if (i < post_list_size)
394 return post_info_single(post_list + i, 1);
400 int post_log(char *format, ...)
403 char printbuffer[CONFIG_SYS_PBSIZE];
405 va_start(args, format);
407 /* For this to work, printbuffer must be larger than
408 * anything we ever want to print.
410 vsprintf(printbuffer, format, args);
413 /* Send to the stdout file */
419 #ifdef CONFIG_NEEDS_MANUAL_RELOC
420 void post_reloc(void)
425 * We have to relocate the test table manually
427 for (i = 0; i < post_list_size; i++) {
429 struct post_test *test = post_list + i;
432 addr = (ulong)(test->name) + gd->reloc_off;
433 test->name = (char *)addr;
437 addr = (ulong)(test->cmd) + gd->reloc_off;
438 test->cmd = (char *)addr;
442 addr = (ulong)(test->desc) + gd->reloc_off;
443 test->desc = (char *)addr;
447 addr = (ulong)(test->test) + gd->reloc_off;
448 test->test = (int (*)(int flags)) addr;
452 addr = (ulong)(test->init_f) + gd->reloc_off;
453 test->init_f = (int (*)(void)) addr;
457 addr = (ulong)(test->reloc) + gd->reloc_off;
458 test->reloc = (void (*)(void)) addr;
468 * Some tests (e.g. SYSMON) need the time when post_init_f started,
469 * but we cannot use get_timer() at this point.
471 * On PowerPC we implement it using the timebase register.
473 unsigned long post_time_ms(unsigned long base)
475 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
476 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
479 #warning "Not implemented yet"
480 return 0; /* Not implemented yet */