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