1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
18 DECLARE_GLOBAL_DATA_PTR;
20 #define POST_MAX_NUMBER 32
22 #define BOOTMODE_MAGIC 0xDEAD0000
29 for (i = 0; i < post_list_size; i++) {
30 struct post_test *test = post_list + i;
32 if (test->init_f && test->init_f())
36 gd->post_init_f_time = post_time_ms(0);
37 if (!gd->post_init_f_time)
38 printf("%s: post_time_ms not implemented\n", __FILE__);
44 * Supply a default implementation for post_hotkeys_pressed() for boards
45 * without hotkey support. We always return 0 here, so that the
46 * long-running tests won't be started.
48 * Boards with hotkey support can override this weak default function
49 * by defining one in their board specific code.
51 __weak int post_hotkeys_pressed(void)
53 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
55 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
57 ret = gpio_request(gpio, "hotkeys");
59 printf("POST: gpio hotkey request failed\n");
63 gpio_direction_input(gpio);
64 ret = gpio_get_value(gpio);
70 return 0; /* No hotkeys supported */
73 void post_bootmode_init(void)
75 int bootmode = post_bootmode_get(0);
78 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
79 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
80 else if (bootmode == 0)
81 newword = BOOTMODE_MAGIC | POST_POWERON;
82 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
83 newword = BOOTMODE_MAGIC | POST_NORMAL;
86 newword = post_word_load() & ~POST_COLDBOOT;
89 /* We are booting after power-on */
90 newword |= POST_COLDBOOT;
92 post_word_store(newword);
94 /* Reset activity record */
95 gd->post_log_word = 0;
99 int post_bootmode_get(unsigned int *last_test)
101 unsigned long word = post_word_load();
104 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
107 bootmode = word & 0x7F;
109 if (last_test && (bootmode & POST_POWERTEST))
110 *last_test = (word >> 8) & 0xFF;
115 /* POST tests run before relocation only mark status bits .... */
116 static void post_log_mark_start(unsigned long testid)
118 gd->post_log_word |= testid;
121 static void post_log_mark_succ(unsigned long testid)
123 gd->post_log_res |= testid;
126 /* ... and the messages are output once we are relocated */
127 void post_output_backlog(void)
131 for (j = 0; j < post_list_size; j++) {
132 if (gd->post_log_word & (post_list[j].testid)) {
133 post_log("POST %s ", post_list[j].cmd);
134 if (gd->post_log_res & post_list[j].testid)
135 post_log("PASSED\n");
137 post_log("FAILED\n");
138 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
144 static void post_bootmode_test_on(unsigned int last_test)
146 unsigned long word = post_word_load();
148 word |= POST_POWERTEST;
150 word |= (last_test & 0xFF) << 8;
152 post_word_store(word);
155 static void post_bootmode_test_off(void)
157 unsigned long word = post_word_load();
159 word &= ~POST_POWERTEST;
161 post_word_store(word);
164 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
165 static void post_get_env_flags(int *test_flags)
167 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
169 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
171 int varnum = ARRAY_SIZE(var);
172 char list[128]; /* long enough for POST list */
178 for (i = 0; i < varnum; i++) {
179 if (env_get_f(var[i], list, sizeof(list)) <= 0)
182 for (j = 0; j < post_list_size; j++)
183 test_flags[j] &= ~flag[i];
188 while (*name && *name == ' ')
193 while (*s && *s != ' ')
200 for (j = 0; j < post_list_size; j++) {
201 if (strcmp(post_list[j].cmd, name) == 0) {
202 test_flags[j] |= flag[i];
207 if (j == post_list_size)
208 printf("No such test: %s\n", name);
216 static void post_get_flags(int *test_flags)
220 for (j = 0; j < post_list_size; j++)
221 test_flags[j] = post_list[j].flags;
223 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
224 post_get_env_flags(test_flags);
227 for (j = 0; j < post_list_size; j++)
228 if (test_flags[j] & POST_POWERON)
229 test_flags[j] |= POST_SLOWTEST;
232 __weak void show_post_progress(unsigned int test_num, int before, int result)
236 static int post_run_single(struct post_test *test,
237 int test_flags, int flags, unsigned int i)
239 if ((flags & test_flags & POST_ALWAYS) &&
240 (flags & test_flags & POST_MEM)) {
243 if (!(flags & POST_REBOOT)) {
244 if ((test_flags & POST_REBOOT) &&
245 !(flags & POST_MANUAL)) {
246 post_bootmode_test_on(
247 (gd->flags & GD_FLG_POSTFAIL) ?
248 POST_FAIL_SAVE | i : i);
251 if (test_flags & POST_PREREL)
252 post_log_mark_start(test->testid);
254 post_log("POST %s ", test->cmd);
257 show_post_progress(i, POST_BEFORE, POST_FAILED);
259 if (test_flags & POST_PREREL) {
260 if ((*test->test)(flags) == 0) {
261 post_log_mark_succ(test->testid);
262 show_post_progress(i, POST_AFTER, POST_PASSED);
264 show_post_progress(i, POST_AFTER, POST_FAILED);
265 if (test_flags & POST_CRITICAL)
266 gd->flags |= GD_FLG_POSTFAIL;
267 if (test_flags & POST_STOP)
268 gd->flags |= GD_FLG_POSTSTOP;
271 if ((*test->test)(flags) != 0) {
272 post_log("FAILED\n");
273 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
274 show_post_progress(i, POST_AFTER, POST_FAILED);
275 if (test_flags & POST_CRITICAL)
276 gd->flags |= GD_FLG_POSTFAIL;
277 if (test_flags & POST_STOP)
278 gd->flags |= GD_FLG_POSTSTOP;
280 post_log("PASSED\n");
281 show_post_progress(i, POST_AFTER, POST_PASSED);
285 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
286 post_bootmode_test_off();
294 int post_run(char *name, int flags)
297 int test_flags[POST_MAX_NUMBER];
299 post_get_flags(test_flags);
304 if (gd->flags & GD_FLG_POSTSTOP)
307 if (post_bootmode_get(&last) & POST_POWERTEST) {
308 if (last & POST_FAIL_SAVE) {
309 last &= ~POST_FAIL_SAVE;
310 gd->flags |= GD_FLG_POSTFAIL;
312 if (last < post_list_size &&
313 (flags & test_flags[last] & POST_ALWAYS) &&
314 (flags & test_flags[last] & POST_MEM)) {
316 post_run_single(post_list + last,
318 flags | POST_REBOOT, last);
320 for (i = last + 1; i < post_list_size; i++) {
321 if (gd->flags & GD_FLG_POSTSTOP)
323 post_run_single(post_list + i,
329 for (i = 0; i < post_list_size; i++) {
330 if (gd->flags & GD_FLG_POSTSTOP)
332 post_run_single(post_list + i,
340 for (i = 0; i < post_list_size; i++) {
341 if (strcmp(post_list[i].cmd, name) == 0)
345 if (i < post_list_size) {
347 return post_run_single(post_list + i,
356 static int post_info_single(struct post_test *test, int full)
358 if (test->flags & POST_MANUAL) {
361 " %s\n", test->cmd, test->name, test->desc);
363 printf(" %-15s - %s\n", test->cmd, test->name);
371 int post_info(char *name)
376 for (i = 0; i < post_list_size; i++)
377 post_info_single(post_list + i, 0);
381 for (i = 0; i < post_list_size; i++) {
382 if (strcmp(post_list[i].cmd, name) == 0)
386 if (i < post_list_size)
387 return post_info_single(post_list + i, 1);
393 int post_log(char *format, ...)
396 char printbuffer[CONFIG_SYS_PBSIZE];
398 va_start(args, format);
400 /* For this to work, printbuffer must be larger than
401 * anything we ever want to print.
403 vsprintf(printbuffer, format, args);
406 /* Send to the stdout file */
412 #ifdef CONFIG_NEEDS_MANUAL_RELOC
413 void post_reloc(void)
418 * We have to relocate the test table manually
420 for (i = 0; i < post_list_size; i++) {
422 struct post_test *test = post_list + i;
425 addr = (ulong)(test->name) + gd->reloc_off;
426 test->name = (char *)addr;
430 addr = (ulong)(test->cmd) + gd->reloc_off;
431 test->cmd = (char *)addr;
435 addr = (ulong)(test->desc) + gd->reloc_off;
436 test->desc = (char *)addr;
440 addr = (ulong)(test->test) + gd->reloc_off;
441 test->test = (int (*)(int flags)) addr;
445 addr = (ulong)(test->init_f) + gd->reloc_off;
446 test->init_f = (int (*)(void)) addr;
450 addr = (ulong)(test->reloc) + gd->reloc_off;
451 test->reloc = (void (*)(void)) addr;
461 * Some tests (e.g. SYSMON) need the time when post_init_f started,
462 * but we cannot use get_timer() at this point.
464 * On PowerPC we implement it using the timebase register.
466 unsigned long post_time_ms(unsigned long base)
468 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
469 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
472 #warning "Not implemented yet"
473 return 0; /* Not implemented yet */