3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
14 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
18 #ifdef CONFIG_LOGBUFFER
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 void 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);
148 static void post_bootmode_test_on(unsigned int last_test)
150 unsigned long word = post_word_load();
152 word |= POST_POWERTEST;
154 word |= (last_test & 0xFF) << 8;
156 post_word_store(word);
159 static void post_bootmode_test_off(void)
161 unsigned long word = post_word_load();
163 word &= ~POST_POWERTEST;
165 post_word_store(word);
168 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
169 static void post_get_env_flags(int *test_flags)
171 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
173 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
175 int varnum = ARRAY_SIZE(var);
176 char list[128]; /* long enough for POST list */
182 for (i = 0; i < varnum; i++) {
183 if (env_get_f(var[i], list, sizeof(list)) <= 0)
186 for (j = 0; j < post_list_size; j++)
187 test_flags[j] &= ~flag[i];
192 while (*name && *name == ' ')
197 while (*s && *s != ' ')
204 for (j = 0; j < post_list_size; j++) {
205 if (strcmp(post_list[j].cmd, name) == 0) {
206 test_flags[j] |= flag[i];
211 if (j == post_list_size)
212 printf("No such test: %s\n", name);
220 static void post_get_flags(int *test_flags)
224 for (j = 0; j < post_list_size; j++)
225 test_flags[j] = post_list[j].flags;
227 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
228 post_get_env_flags(test_flags);
231 for (j = 0; j < post_list_size; j++)
232 if (test_flags[j] & POST_POWERON)
233 test_flags[j] |= POST_SLOWTEST;
236 __weak void show_post_progress(unsigned int test_num, int before, int result)
240 static int post_run_single(struct post_test *test,
241 int test_flags, int flags, unsigned int i)
243 if ((flags & test_flags & POST_ALWAYS) &&
244 (flags & test_flags & POST_MEM)) {
247 if (!(flags & POST_REBOOT)) {
248 if ((test_flags & POST_REBOOT) &&
249 !(flags & POST_MANUAL)) {
250 post_bootmode_test_on(
251 (gd->flags & GD_FLG_POSTFAIL) ?
252 POST_FAIL_SAVE | i : i);
255 if (test_flags & POST_PREREL)
256 post_log_mark_start(test->testid);
258 post_log("POST %s ", test->cmd);
261 show_post_progress(i, POST_BEFORE, POST_FAILED);
263 if (test_flags & POST_PREREL) {
264 if ((*test->test)(flags) == 0) {
265 post_log_mark_succ(test->testid);
266 show_post_progress(i, POST_AFTER, POST_PASSED);
268 show_post_progress(i, POST_AFTER, POST_FAILED);
269 if (test_flags & POST_CRITICAL)
270 gd->flags |= GD_FLG_POSTFAIL;
271 if (test_flags & POST_STOP)
272 gd->flags |= GD_FLG_POSTSTOP;
275 if ((*test->test)(flags) != 0) {
276 post_log("FAILED\n");
277 bootstage_error(BOOTSTAGE_ID_POST_FAIL_R);
278 show_post_progress(i, POST_AFTER, POST_FAILED);
279 if (test_flags & POST_CRITICAL)
280 gd->flags |= GD_FLG_POSTFAIL;
281 if (test_flags & POST_STOP)
282 gd->flags |= GD_FLG_POSTSTOP;
284 post_log("PASSED\n");
285 show_post_progress(i, POST_AFTER, POST_PASSED);
289 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL))
290 post_bootmode_test_off();
298 int post_run(char *name, int flags)
301 int test_flags[POST_MAX_NUMBER];
303 post_get_flags(test_flags);
308 if (gd->flags & GD_FLG_POSTSTOP)
311 if (post_bootmode_get(&last) & POST_POWERTEST) {
312 if (last & POST_FAIL_SAVE) {
313 last &= ~POST_FAIL_SAVE;
314 gd->flags |= GD_FLG_POSTFAIL;
316 if (last < post_list_size &&
317 (flags & test_flags[last] & POST_ALWAYS) &&
318 (flags & test_flags[last] & POST_MEM)) {
320 post_run_single(post_list + last,
322 flags | POST_REBOOT, last);
324 for (i = last + 1; i < post_list_size; i++) {
325 if (gd->flags & GD_FLG_POSTSTOP)
327 post_run_single(post_list + i,
333 for (i = 0; i < post_list_size; i++) {
334 if (gd->flags & GD_FLG_POSTSTOP)
336 post_run_single(post_list + i,
344 for (i = 0; i < post_list_size; i++) {
345 if (strcmp(post_list[i].cmd, name) == 0)
349 if (i < post_list_size) {
351 return post_run_single(post_list + i,
360 static int post_info_single(struct post_test *test, int full)
362 if (test->flags & POST_MANUAL) {
365 " %s\n", test->cmd, test->name, test->desc);
367 printf(" %-15s - %s\n", test->cmd, test->name);
375 int post_info(char *name)
380 for (i = 0; i < post_list_size; i++)
381 post_info_single(post_list + i, 0);
385 for (i = 0; i < post_list_size; i++) {
386 if (strcmp(post_list[i].cmd, name) == 0)
390 if (i < post_list_size)
391 return post_info_single(post_list + i, 1);
397 int post_log(char *format, ...)
400 char printbuffer[CONFIG_SYS_PBSIZE];
402 va_start(args, format);
404 /* For this to work, printbuffer must be larger than
405 * anything we ever want to print.
407 vsprintf(printbuffer, format, args);
410 #ifdef CONFIG_LOGBUFFER
411 /* Send to the logbuffer */
412 logbuff_log(printbuffer);
414 /* Send to the stdout file */
421 #ifdef CONFIG_NEEDS_MANUAL_RELOC
422 void post_reloc(void)
427 * We have to relocate the test table manually
429 for (i = 0; i < post_list_size; i++) {
431 struct post_test *test = post_list + i;
434 addr = (ulong)(test->name) + gd->reloc_off;
435 test->name = (char *)addr;
439 addr = (ulong)(test->cmd) + gd->reloc_off;
440 test->cmd = (char *)addr;
444 addr = (ulong)(test->desc) + gd->reloc_off;
445 test->desc = (char *)addr;
449 addr = (ulong)(test->test) + gd->reloc_off;
450 test->test = (int (*)(int flags)) addr;
454 addr = (ulong)(test->init_f) + gd->reloc_off;
455 test->init_f = (int (*)(void)) addr;
459 addr = (ulong)(test->reloc) + gd->reloc_off;
460 test->reloc = (void (*)(void)) addr;
470 * Some tests (e.g. SYSMON) need the time when post_init_f started,
471 * but we cannot use get_timer() at this point.
473 * On PowerPC we implement it using the timebase register.
475 unsigned long post_time_ms(unsigned long base)
477 #if defined(CONFIG_PPC) || defined(CONFIG_ARM)
478 return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ)
481 #warning "Not implemented yet"
482 return 0; /* Not implemented yet */