1 // SPDX-License-Identifier: GPL-2.0+
15 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
19 DECLARE_GLOBAL_DATA_PTR;
21 #define POST_MAX_NUMBER 32
23 #define BOOTMODE_MAGIC 0xDEAD0000
30 for (i = 0; i < post_list_size; i++) {
31 struct post_test *test = post_list + i;
33 if (test->init_f && test->init_f())
37 gd->post_init_f_time = post_time_ms(0);
38 if (!gd->post_init_f_time)
39 printf("%s: post_time_ms not implemented\n", __FILE__);
45 * Supply a default implementation for post_hotkeys_pressed() for boards
46 * without hotkey support. We always return 0 here, so that the
47 * long-running tests won't be started.
49 * Boards with hotkey support can override this weak default function
50 * by defining one in their board specific code.
52 __weak int post_hotkeys_pressed(void)
54 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
56 unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
58 ret = gpio_request(gpio, "hotkeys");
60 printf("POST: gpio hotkey request failed\n");
64 gpio_direction_input(gpio);
65 ret = gpio_get_value(gpio);
71 return 0; /* No hotkeys supported */
74 void post_bootmode_init(void)
76 int bootmode = post_bootmode_get(0);
79 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST))
80 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
81 else if (bootmode == 0)
82 newword = BOOTMODE_MAGIC | POST_POWERON;
83 else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST)
84 newword = BOOTMODE_MAGIC | POST_NORMAL;
87 newword = post_word_load() & ~POST_COLDBOOT;
90 /* We are booting after power-on */
91 newword |= POST_COLDBOOT;
93 post_word_store(newword);
95 /* Reset activity record */
96 gd->post_log_word = 0;
100 int post_bootmode_get(unsigned int *last_test)
102 unsigned long word = post_word_load();
105 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC)
108 bootmode = word & 0x7F;
110 if (last_test && (bootmode & POST_POWERTEST))
111 *last_test = (word >> 8) & 0xFF;
116 /* POST tests run before relocation only mark status bits .... */
117 static void post_log_mark_start(unsigned long testid)
119 gd->post_log_word |= testid;
122 static void post_log_mark_succ(unsigned long testid)
124 gd->post_log_res |= testid;
127 /* ... and the messages are output once we are relocated */
128 void post_output_backlog(void)
132 for (j = 0; j < post_list_size; j++) {
133 if (gd->post_log_word & (post_list[j].testid)) {
134 post_log("POST %s ", post_list[j].cmd);
135 if (gd->post_log_res & post_list[j].testid)
136 post_log("PASSED\n");
138 post_log("FAILED\n");
139 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
145 static void post_bootmode_test_on(unsigned int last_test)
147 unsigned long word = post_word_load();
149 word |= POST_POWERTEST;
151 word |= (last_test & 0xFF) << 8;
153 post_word_store(word);
156 static void post_bootmode_test_off(void)
158 unsigned long word = post_word_load();
160 word &= ~POST_POWERTEST;
162 post_word_store(word);
165 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
166 static void post_get_env_flags(int *test_flags)
168 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
170 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
172 int varnum = ARRAY_SIZE(var);
173 char list[128]; /* long enough for POST list */
179 for (i = 0; i < varnum; i++) {
180 if (env_get_f(var[i], list, sizeof(list)) <= 0)
183 for (j = 0; j < post_list_size; j++)
184 test_flags[j] &= ~flag[i];
189 while (*name && *name == ' ')
194 while (*s && *s != ' ')
201 for (j = 0; j < post_list_size; j++) {
202 if (strcmp(post_list[j].cmd, name) == 0) {
203 test_flags[j] |= flag[i];
208 if (j == post_list_size)
209 printf("No such test: %s\n", name);
217 static void post_get_flags(int *test_flags)
221 for (j = 0; j < post_list_size; j++)
222 test_flags[j] = post_list[j].flags;
224 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
225 post_get_env_flags(test_flags);
228 for (j = 0; j < post_list_size; j++)
229 if (test_flags[j] & POST_POWERON)
230 test_flags[j] |= POST_SLOWTEST;
233 __weak void show_post_progress(unsigned int test_num, int before, int result)
237 static int post_run_single(struct post_test *test,
238 int test_flags, int flags, unsigned int i)
240 if ((flags & test_flags & POST_ALWAYS) &&
241 (flags & test_flags & POST_MEM)) {
244 if (!(flags & POST_REBOOT)) {
245 if ((test_flags & POST_REBOOT) &&
246 !(flags & POST_MANUAL)) {
247 post_bootmode_test_on(
248 (gd->flags & GD_FLG_POSTFAIL) ?
249 POST_FAIL_SAVE | i : i);
252 if (test_flags & POST_PREREL)
253 post_log_mark_start(test->testid);
255 post_log("POST %s ", test->cmd);
258 show_post_progress(i, POST_BEFORE, POST_FAILED);
260 if (test_flags & POST_PREREL) {
261 if ((*test->test)(flags) == 0) {
262 post_log_mark_succ(test->testid);
263 show_post_progress(i, POST_AFTER, POST_PASSED);
265 show_post_progress(i, POST_AFTER, POST_FAILED);
266 if (test_flags & POST_CRITICAL)
267 gd->flags |= GD_FLG_POSTFAIL;
268 if (test_flags & POST_STOP)
269 gd->flags |= GD_FLG_POSTSTOP;
272 if ((*test->test)(flags) != 0) {
273 post_log("FAILED\n");
274 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
275 show_post_progress(i, POST_AFTER, POST_FAILED);
276 if (test_flags & POST_CRITICAL)
277 gd->flags |= GD_FLG_POSTFAIL;
278 if (test_flags & POST_STOP)
279 gd->flags |= GD_FLG_POSTSTOP;
281 post_log("PASSED\n");
282 show_post_progress(i, POST_AFTER, POST_PASSED);
286 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
287 post_bootmode_test_off();
295 int post_run(char *name, int flags)
298 int test_flags[POST_MAX_NUMBER];
300 post_get_flags(test_flags);
305 if (gd->flags & GD_FLG_POSTSTOP)
308 if (post_bootmode_get(&last) & POST_POWERTEST) {
309 if (last & POST_FAIL_SAVE) {
310 last &= ~POST_FAIL_SAVE;
311 gd->flags |= GD_FLG_POSTFAIL;
313 if (last < post_list_size &&
314 (flags & test_flags[last] & POST_ALWAYS) &&
315 (flags & test_flags[last] & POST_MEM)) {
317 post_run_single(post_list + last,
319 flags | POST_REBOOT, last);
321 for (i = last + 1; i < post_list_size; i++) {
322 if (gd->flags & GD_FLG_POSTSTOP)
324 post_run_single(post_list + i,
330 for (i = 0; i < post_list_size; i++) {
331 if (gd->flags & GD_FLG_POSTSTOP)
333 post_run_single(post_list + i,
341 for (i = 0; i < post_list_size; i++) {
342 if (strcmp(post_list[i].cmd, name) == 0)
346 if (i < post_list_size) {
348 return post_run_single(post_list + i,
357 static int post_info_single(struct post_test *test, int full)
359 if (test->flags & POST_MANUAL) {
362 " %s\n", test->cmd, test->name, test->desc);
364 printf(" %-15s - %s\n", test->cmd, test->name);
372 int post_info(char *name)
377 for (i = 0; i < post_list_size; i++)
378 post_info_single(post_list + i, 0);
382 for (i = 0; i < post_list_size; i++) {
383 if (strcmp(post_list[i].cmd, name) == 0)
387 if (i < post_list_size)
388 return post_info_single(post_list + i, 1);
394 int post_log(char *format, ...)
397 char printbuffer[CONFIG_SYS_PBSIZE];
399 va_start(args, format);
401 /* For this to work, printbuffer must be larger than
402 * anything we ever want to print.
404 vsprintf(printbuffer, format, args);
407 /* Send to the stdout file */
413 #ifdef CONFIG_NEEDS_MANUAL_RELOC
414 void post_reloc(void)
419 * We have to relocate the test table manually
421 for (i = 0; i < post_list_size; i++) {
423 struct post_test *test = post_list + i;
426 addr = (ulong)(test->name) + gd->reloc_off;
427 test->name = (char *)addr;
431 addr = (ulong)(test->cmd) + gd->reloc_off;
432 test->cmd = (char *)addr;
436 addr = (ulong)(test->desc) + gd->reloc_off;
437 test->desc = (char *)addr;
441 addr = (ulong)(test->test) + gd->reloc_off;
442 test->test = (int (*)(int flags)) addr;
446 addr = (ulong)(test->init_f) + gd->reloc_off;
447 test->init_f = (int (*)(void)) addr;
451 addr = (ulong)(test->reloc) + gd->reloc_off;
452 test->reloc = (void (*)(void)) addr;
462 * Some tests (e.g. SYSMON) need the time when post_init_f started,
463 * but we cannot use get_timer() at this point.
465 * On PowerPC we implement it using the timebase register.
467 unsigned long post_time_ms(unsigned long base)
469 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
470 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
473 #warning "Not implemented yet"
474 return 0; /* Not implemented yet */