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