]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a042ac84 WD |
2 | /* |
3 | * (C) Copyright 2002 | |
4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
a042ac84 WD |
5 | */ |
6 | ||
7 | #include <common.h> | |
52f24238 | 8 | #include <bootstage.h> |
3a7d5571 | 9 | #include <env.h> |
f7ae49fc | 10 | #include <log.h> |
336d4615 | 11 | #include <malloc.h> |
52cb4d4f | 12 | #include <stdio_dev.h> |
1045315d | 13 | #include <time.h> |
a042ac84 | 14 | #include <watchdog.h> |
c90a4dd7 | 15 | #include <div64.h> |
a042ac84 | 16 | #include <post.h> |
401d1c4f | 17 | #include <asm/global_data.h> |
a042ac84 | 18 | |
9146d138 MF |
19 | #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO |
20 | #include <asm/gpio.h> | |
21 | #endif | |
22 | ||
d87080b7 WD |
23 | DECLARE_GLOBAL_DATA_PTR; |
24 | ||
a042ac84 WD |
25 | #define POST_MAX_NUMBER 32 |
26 | ||
27 | #define BOOTMODE_MAGIC 0xDEAD0000 | |
28 | ||
e92372c8 | 29 | int post_init_f(void) |
4532cb69 | 30 | { |
4532cb69 WD |
31 | int res = 0; |
32 | unsigned int i; | |
33 | ||
34 | for (i = 0; i < post_list_size; i++) { | |
35 | struct post_test *test = post_list + i; | |
36 | ||
50da8376 | 37 | if (test->init_f && test->init_f()) |
4532cb69 | 38 | res = -1; |
4532cb69 | 39 | } |
8bde7f77 | 40 | |
4532cb69 WD |
41 | gd->post_init_f_time = post_time_ms(0); |
42 | if (!gd->post_init_f_time) | |
e92372c8 | 43 | printf("%s: post_time_ms not implemented\n", __FILE__); |
4532cb69 WD |
44 | |
45 | return res; | |
46 | } | |
47 | ||
39ff7d5f SR |
48 | /* |
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. | |
52 | * | |
53 | * Boards with hotkey support can override this weak default function | |
54 | * by defining one in their board specific code. | |
55 | */ | |
002ad7b8 | 56 | __weak int post_hotkeys_pressed(void) |
39ff7d5f | 57 | { |
9146d138 MF |
58 | #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO |
59 | int ret; | |
60 | unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO; | |
61 | ||
62 | ret = gpio_request(gpio, "hotkeys"); | |
63 | if (ret) { | |
64 | printf("POST: gpio hotkey request failed\n"); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | gpio_direction_input(gpio); | |
69 | ret = gpio_get_value(gpio); | |
70 | gpio_free(gpio); | |
71 | ||
72 | return ret; | |
73 | #endif | |
74 | ||
39ff7d5f SR |
75 | return 0; /* No hotkeys supported */ |
76 | } | |
39ff7d5f | 77 | |
e92372c8 | 78 | void post_bootmode_init(void) |
a042ac84 | 79 | { |
e92372c8 | 80 | int bootmode = post_bootmode_get(0); |
27b207fd | 81 | int newword; |
42d1f039 | 82 | |
e92372c8 | 83 | if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) |
27b207fd | 84 | newword = BOOTMODE_MAGIC | POST_SLOWTEST; |
e92372c8 | 85 | else if (bootmode == 0) |
27b207fd | 86 | newword = BOOTMODE_MAGIC | POST_POWERON; |
e92372c8 | 87 | else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) |
27b207fd | 88 | newword = BOOTMODE_MAGIC | POST_NORMAL; |
e92372c8 | 89 | else |
27b207fd | 90 | /* Use old value */ |
50da8376 | 91 | newword = post_word_load() & ~POST_COLDBOOT; |
a042ac84 | 92 | |
27b207fd | 93 | if (bootmode == 0) |
27b207fd WD |
94 | /* We are booting after power-on */ |
95 | newword |= POST_COLDBOOT; | |
27b207fd | 96 | |
e92372c8 | 97 | post_word_store(newword); |
27b207fd | 98 | |
228f29ac WD |
99 | /* Reset activity record */ |
100 | gd->post_log_word = 0; | |
79843950 | 101 | gd->post_log_res = 0; |
a042ac84 WD |
102 | } |
103 | ||
e92372c8 | 104 | int post_bootmode_get(unsigned int *last_test) |
a042ac84 | 105 | { |
e92372c8 | 106 | unsigned long word = post_word_load(); |
a042ac84 WD |
107 | int bootmode; |
108 | ||
e92372c8 | 109 | if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) |
a042ac84 | 110 | return 0; |
a042ac84 | 111 | |
27b207fd | 112 | bootmode = word & 0x7F; |
a042ac84 | 113 | |
e92372c8 | 114 | if (last_test && (bootmode & POST_POWERTEST)) |
a042ac84 | 115 | *last_test = (word >> 8) & 0xFF; |
a042ac84 WD |
116 | |
117 | return bootmode; | |
118 | } | |
119 | ||
228f29ac | 120 | /* POST tests run before relocation only mark status bits .... */ |
e92372c8 | 121 | static void post_log_mark_start(unsigned long testid) |
228f29ac | 122 | { |
79843950 | 123 | gd->post_log_word |= testid; |
228f29ac WD |
124 | } |
125 | ||
e92372c8 | 126 | static void post_log_mark_succ(unsigned long testid) |
228f29ac | 127 | { |
79843950 | 128 | gd->post_log_res |= testid; |
228f29ac WD |
129 | } |
130 | ||
131 | /* ... and the messages are output once we are relocated */ | |
7addd3c6 | 132 | int post_output_backlog(void) |
228f29ac | 133 | { |
228f29ac WD |
134 | int j; |
135 | ||
136 | for (j = 0; j < post_list_size; j++) { | |
79843950 | 137 | if (gd->post_log_word & (post_list[j].testid)) { |
50da8376 | 138 | post_log("POST %s ", post_list[j].cmd); |
79843950 | 139 | if (gd->post_log_res & post_list[j].testid) |
50da8376 | 140 | post_log("PASSED\n"); |
63e73c9a | 141 | else { |
e92372c8 | 142 | post_log("FAILED\n"); |
770605e4 | 143 | bootstage_error(BOOTSTAGE_ID_POST_FAIL_R); |
63e73c9a | 144 | } |
228f29ac WD |
145 | } |
146 | } | |
7addd3c6 OP |
147 | |
148 | return 0; | |
228f29ac WD |
149 | } |
150 | ||
e92372c8 | 151 | static void post_bootmode_test_on(unsigned int last_test) |
a042ac84 | 152 | { |
e92372c8 | 153 | unsigned long word = post_word_load(); |
a042ac84 WD |
154 | |
155 | word |= POST_POWERTEST; | |
156 | ||
157 | word |= (last_test & 0xFF) << 8; | |
158 | ||
e92372c8 | 159 | post_word_store(word); |
a042ac84 WD |
160 | } |
161 | ||
e92372c8 | 162 | static void post_bootmode_test_off(void) |
a042ac84 | 163 | { |
e92372c8 | 164 | unsigned long word = post_word_load(); |
a042ac84 WD |
165 | |
166 | word &= ~POST_POWERTEST; | |
167 | ||
e92372c8 | 168 | post_word_store(word); |
a042ac84 WD |
169 | } |
170 | ||
212a0caf VL |
171 | #ifndef CONFIG_POST_SKIP_ENV_FLAGS |
172 | static void post_get_env_flags(int *test_flags) | |
a042ac84 | 173 | { |
e262efe3 YT |
174 | int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST, |
175 | POST_CRITICAL }; | |
176 | char *var[] = { "post_poweron", "post_normal", "post_slowtest", | |
177 | "post_critical" }; | |
d2397817 | 178 | int varnum = ARRAY_SIZE(var); |
a042ac84 WD |
179 | char list[128]; /* long enough for POST list */ |
180 | char *name; | |
181 | char *s; | |
182 | int last; | |
183 | int i, j; | |
184 | ||
a042ac84 | 185 | for (i = 0; i < varnum; i++) { |
00caae6d | 186 | if (env_get_f(var[i], list, sizeof(list)) <= 0) |
a042ac84 WD |
187 | continue; |
188 | ||
50da8376 | 189 | for (j = 0; j < post_list_size; j++) |
a042ac84 | 190 | test_flags[j] &= ~flag[i]; |
a042ac84 WD |
191 | |
192 | last = 0; | |
193 | name = list; | |
194 | while (!last) { | |
86eeac7b | 195 | while (*name == ' ') |
a042ac84 WD |
196 | name++; |
197 | if (*name == 0) | |
198 | break; | |
199 | s = name + 1; | |
200 | while (*s && *s != ' ') | |
201 | s++; | |
202 | if (*s == 0) | |
203 | last = 1; | |
204 | else | |
205 | *s = 0; | |
206 | ||
207 | for (j = 0; j < post_list_size; j++) { | |
50da8376 | 208 | if (strcmp(post_list[j].cmd, name) == 0) { |
a042ac84 WD |
209 | test_flags[j] |= flag[i]; |
210 | break; | |
211 | } | |
212 | } | |
213 | ||
e92372c8 | 214 | if (j == post_list_size) |
50da8376 | 215 | printf("No such test: %s\n", name); |
a042ac84 WD |
216 | |
217 | name = s + 1; | |
218 | } | |
219 | } | |
212a0caf VL |
220 | } |
221 | #endif | |
222 | ||
223 | static void post_get_flags(int *test_flags) | |
224 | { | |
225 | int j; | |
226 | ||
227 | for (j = 0; j < post_list_size; j++) | |
228 | test_flags[j] = post_list[j].flags; | |
229 | ||
230 | #ifndef CONFIG_POST_SKIP_ENV_FLAGS | |
231 | post_get_env_flags(test_flags); | |
232 | #endif | |
6dff5529 | 233 | |
e92372c8 HS |
234 | for (j = 0; j < post_list_size; j++) |
235 | if (test_flags[j] & POST_POWERON) | |
6dff5529 | 236 | test_flags[j] |= POST_SLOWTEST; |
a042ac84 WD |
237 | } |
238 | ||
002ad7b8 | 239 | __weak void show_post_progress(unsigned int test_num, int before, int result) |
e070a56c MZ |
240 | { |
241 | } | |
e070a56c | 242 | |
e92372c8 | 243 | static int post_run_single(struct post_test *test, |
a042ac84 WD |
244 | int test_flags, int flags, unsigned int i) |
245 | { | |
246 | if ((flags & test_flags & POST_ALWAYS) && | |
247 | (flags & test_flags & POST_MEM)) { | |
50da8376 | 248 | WATCHDOG_RESET(); |
a042ac84 WD |
249 | |
250 | if (!(flags & POST_REBOOT)) { | |
e92372c8 HS |
251 | if ((test_flags & POST_REBOOT) && |
252 | !(flags & POST_MANUAL)) { | |
253 | post_bootmode_test_on( | |
e262efe3 YT |
254 | (gd->flags & GD_FLG_POSTFAIL) ? |
255 | POST_FAIL_SAVE | i : i); | |
a042ac84 WD |
256 | } |
257 | ||
228f29ac | 258 | if (test_flags & POST_PREREL) |
e92372c8 | 259 | post_log_mark_start(test->testid); |
228f29ac | 260 | else |
e92372c8 | 261 | post_log("POST %s ", test->cmd); |
a042ac84 WD |
262 | } |
263 | ||
e070a56c MZ |
264 | show_post_progress(i, POST_BEFORE, POST_FAILED); |
265 | ||
228f29ac | 266 | if (test_flags & POST_PREREL) { |
50da8376 | 267 | if ((*test->test)(flags) == 0) { |
e92372c8 | 268 | post_log_mark_succ(test->testid); |
e070a56c | 269 | show_post_progress(i, POST_AFTER, POST_PASSED); |
50da8376 | 270 | } else { |
e070a56c | 271 | show_post_progress(i, POST_AFTER, POST_FAILED); |
28a38506 YT |
272 | if (test_flags & POST_CRITICAL) |
273 | gd->flags |= GD_FLG_POSTFAIL; | |
274 | if (test_flags & POST_STOP) | |
275 | gd->flags |= GD_FLG_POSTSTOP; | |
276 | } | |
228f29ac | 277 | } else { |
975afc34 JK |
278 | if ((*test->test)(flags) != 0) { |
279 | post_log("FAILED\n"); | |
770605e4 | 280 | bootstage_error(BOOTSTAGE_ID_POST_FAIL_R); |
975afc34 JK |
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; | |
286 | } else { | |
287 | post_log("PASSED\n"); | |
288 | show_post_progress(i, POST_AFTER, POST_PASSED); | |
289 | } | |
228f29ac | 290 | } |
a042ac84 | 291 | |
50da8376 | 292 | if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) |
e92372c8 | 293 | post_bootmode_test_off(); |
a042ac84 WD |
294 | |
295 | return 0; | |
296 | } else { | |
297 | return -1; | |
298 | } | |
299 | } | |
300 | ||
50da8376 | 301 | int post_run(char *name, int flags) |
a042ac84 WD |
302 | { |
303 | unsigned int i; | |
304 | int test_flags[POST_MAX_NUMBER]; | |
305 | ||
e92372c8 | 306 | post_get_flags(test_flags); |
a042ac84 WD |
307 | |
308 | if (name == NULL) { | |
309 | unsigned int last; | |
310 | ||
28a38506 YT |
311 | if (gd->flags & GD_FLG_POSTSTOP) |
312 | return 0; | |
313 | ||
e92372c8 | 314 | if (post_bootmode_get(&last) & POST_POWERTEST) { |
e262efe3 YT |
315 | if (last & POST_FAIL_SAVE) { |
316 | last &= ~POST_FAIL_SAVE; | |
317 | gd->flags |= GD_FLG_POSTFAIL; | |
318 | } | |
a042ac84 WD |
319 | if (last < post_list_size && |
320 | (flags & test_flags[last] & POST_ALWAYS) && | |
321 | (flags & test_flags[last] & POST_MEM)) { | |
322 | ||
e92372c8 | 323 | post_run_single(post_list + last, |
ea909b76 WD |
324 | test_flags[last], |
325 | flags | POST_REBOOT, last); | |
a042ac84 WD |
326 | |
327 | for (i = last + 1; i < post_list_size; i++) { | |
28a38506 YT |
328 | if (gd->flags & GD_FLG_POSTSTOP) |
329 | break; | |
e92372c8 | 330 | post_run_single(post_list + i, |
ea909b76 WD |
331 | test_flags[i], |
332 | flags, i); | |
a042ac84 WD |
333 | } |
334 | } | |
335 | } else { | |
336 | for (i = 0; i < post_list_size; i++) { | |
28a38506 YT |
337 | if (gd->flags & GD_FLG_POSTSTOP) |
338 | break; | |
e92372c8 | 339 | post_run_single(post_list + i, |
ea909b76 WD |
340 | test_flags[i], |
341 | flags, i); | |
a042ac84 WD |
342 | } |
343 | } | |
344 | ||
345 | return 0; | |
346 | } else { | |
347 | for (i = 0; i < post_list_size; i++) { | |
50da8376 | 348 | if (strcmp(post_list[i].cmd, name) == 0) |
a042ac84 WD |
349 | break; |
350 | } | |
351 | ||
352 | if (i < post_list_size) { | |
5744ddc6 | 353 | WATCHDOG_RESET(); |
e92372c8 | 354 | return post_run_single(post_list + i, |
a042ac84 WD |
355 | test_flags[i], |
356 | flags, i); | |
357 | } else { | |
358 | return -1; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
e92372c8 | 363 | static int post_info_single(struct post_test *test, int full) |
a042ac84 WD |
364 | { |
365 | if (test->flags & POST_MANUAL) { | |
366 | if (full) | |
e92372c8 | 367 | printf("%s - %s\n" |
a042ac84 WD |
368 | " %s\n", test->cmd, test->name, test->desc); |
369 | else | |
e92372c8 | 370 | printf(" %-15s - %s\n", test->cmd, test->name); |
a042ac84 WD |
371 | |
372 | return 0; | |
373 | } else { | |
374 | return -1; | |
375 | } | |
376 | } | |
377 | ||
50da8376 | 378 | int post_info(char *name) |
a042ac84 WD |
379 | { |
380 | unsigned int i; | |
381 | ||
382 | if (name == NULL) { | |
e92372c8 HS |
383 | for (i = 0; i < post_list_size; i++) |
384 | post_info_single(post_list + i, 0); | |
a042ac84 WD |
385 | |
386 | return 0; | |
387 | } else { | |
388 | for (i = 0; i < post_list_size; i++) { | |
50da8376 | 389 | if (strcmp(post_list[i].cmd, name) == 0) |
a042ac84 WD |
390 | break; |
391 | } | |
392 | ||
50da8376 | 393 | if (i < post_list_size) |
e92372c8 | 394 | return post_info_single(post_list + i, 1); |
50da8376 | 395 | else |
a042ac84 | 396 | return -1; |
a042ac84 WD |
397 | } |
398 | } | |
399 | ||
e92372c8 | 400 | int post_log(char *format, ...) |
a042ac84 WD |
401 | { |
402 | va_list args; | |
6d0f6bcf | 403 | char printbuffer[CONFIG_SYS_PBSIZE]; |
a042ac84 | 404 | |
50da8376 | 405 | va_start(args, format); |
a042ac84 WD |
406 | |
407 | /* For this to work, printbuffer must be larger than | |
408 | * anything we ever want to print. | |
409 | */ | |
4d6402b0 | 410 | vsprintf(printbuffer, format, args); |
50da8376 | 411 | va_end(args); |
a042ac84 WD |
412 | |
413 | /* Send to the stdout file */ | |
e92372c8 | 414 | puts(printbuffer); |
a042ac84 WD |
415 | |
416 | return 0; | |
417 | } | |
418 | ||
2e5167cc | 419 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
e92372c8 | 420 | void post_reloc(void) |
a042ac84 | 421 | { |
a042ac84 WD |
422 | unsigned int i; |
423 | ||
424 | /* | |
425 | * We have to relocate the test table manually | |
426 | */ | |
427 | for (i = 0; i < post_list_size; i++) { | |
428 | ulong addr; | |
429 | struct post_test *test = post_list + i; | |
430 | ||
431 | if (test->name) { | |
50da8376 | 432 | addr = (ulong)(test->name) + gd->reloc_off; |
e92372c8 | 433 | test->name = (char *)addr; |
a042ac84 WD |
434 | } |
435 | ||
436 | if (test->cmd) { | |
50da8376 | 437 | addr = (ulong)(test->cmd) + gd->reloc_off; |
e92372c8 | 438 | test->cmd = (char *)addr; |
a042ac84 WD |
439 | } |
440 | ||
441 | if (test->desc) { | |
50da8376 | 442 | addr = (ulong)(test->desc) + gd->reloc_off; |
e92372c8 | 443 | test->desc = (char *)addr; |
a042ac84 WD |
444 | } |
445 | ||
446 | if (test->test) { | |
50da8376 | 447 | addr = (ulong)(test->test) + gd->reloc_off; |
a042ac84 WD |
448 | test->test = (int (*)(int flags)) addr; |
449 | } | |
4532cb69 WD |
450 | |
451 | if (test->init_f) { | |
50da8376 | 452 | addr = (ulong)(test->init_f) + gd->reloc_off; |
4532cb69 WD |
453 | test->init_f = (int (*)(void)) addr; |
454 | } | |
455 | ||
456 | if (test->reloc) { | |
50da8376 | 457 | addr = (ulong)(test->reloc) + gd->reloc_off; |
4532cb69 | 458 | test->reloc = (void (*)(void)) addr; |
8bde7f77 | 459 | |
4532cb69 WD |
460 | test->reloc(); |
461 | } | |
a042ac84 WD |
462 | } |
463 | } | |
521af04d | 464 | #endif |
a042ac84 | 465 | |
4532cb69 WD |
466 | |
467 | /* | |
468 | * Some tests (e.g. SYSMON) need the time when post_init_f started, | |
469 | * but we cannot use get_timer() at this point. | |
470 | * | |
471 | * On PowerPC we implement it using the timebase register. | |
472 | */ | |
e92372c8 | 473 | unsigned long post_time_ms(unsigned long base) |
4532cb69 | 474 | { |
ea3310e8 | 475 | #if defined(CONFIG_PPC) || defined(CONFIG_ARM) |
c90a4dd7 | 476 | return (unsigned long)lldiv(get_ticks(), get_tbclk() / CONFIG_SYS_HZ) |
e92372c8 | 477 | - base; |
4532cb69 | 478 | #else |
ad5bb451 | 479 | #warning "Not implemented yet" |
4532cb69 WD |
480 | return 0; /* Not implemented yet */ |
481 | #endif | |
482 | } |