POST: add post_log_res field for post results in global data
[oweals/u-boot.git] / post / post.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <stdio_dev.h>
26 #include <watchdog.h>
27 #include <post.h>
28
29 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
30 #include <asm/gpio.h>
31 #endif
32
33 #ifdef CONFIG_LOGBUFFER
34 #include <logbuff.h>
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 #define POST_MAX_NUMBER         32
40
41 #define BOOTMODE_MAGIC  0xDEAD0000
42
43 int post_init_f (void)
44 {
45         int res = 0;
46         unsigned int i;
47
48         for (i = 0; i < post_list_size; i++) {
49                 struct post_test *test = post_list + i;
50
51                 if (test->init_f && test->init_f()) {
52                         res = -1;
53                 }
54         }
55
56         gd->post_init_f_time = post_time_ms(0);
57         if (!gd->post_init_f_time)
58         {
59                 printf("post/post.c: post_time_ms seems not to be implemented\n");
60         }
61
62         return res;
63 }
64
65 /*
66  * Supply a default implementation for post_hotkeys_pressed() for boards
67  * without hotkey support. We always return 0 here, so that the
68  * long-running tests won't be started.
69  *
70  * Boards with hotkey support can override this weak default function
71  * by defining one in their board specific code.
72  */
73 int __post_hotkeys_pressed(void)
74 {
75 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
76         int ret;
77         unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
78
79         ret = gpio_request(gpio, "hotkeys");
80         if (ret) {
81                 printf("POST: gpio hotkey request failed\n");
82                 return 0;
83         }
84
85         gpio_direction_input(gpio);
86         ret = gpio_get_value(gpio);
87         gpio_free(gpio);
88
89         return ret;
90 #endif
91
92         return 0;       /* No hotkeys supported */
93 }
94 int post_hotkeys_pressed(void)
95         __attribute__((weak, alias("__post_hotkeys_pressed")));
96
97
98 void post_bootmode_init (void)
99 {
100         int bootmode = post_bootmode_get (0);
101         int newword;
102
103         if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
104                 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
105         } else if (bootmode == 0) {
106                 newword = BOOTMODE_MAGIC | POST_POWERON;
107         } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
108                 newword = BOOTMODE_MAGIC | POST_NORMAL;
109         } else {
110                 /* Use old value */
111                 newword = post_word_load () & ~POST_COLDBOOT;
112         }
113
114         if (bootmode == 0)
115         {
116                 /* We are booting after power-on */
117                 newword |= POST_COLDBOOT;
118         }
119
120         post_word_store (newword);
121
122         /* Reset activity record */
123         gd->post_log_word = 0;
124         gd->post_log_res = 0;
125 }
126
127 int post_bootmode_get (unsigned int *last_test)
128 {
129         unsigned long word = post_word_load ();
130         int bootmode;
131
132         if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
133                 return 0;
134         }
135
136         bootmode = word & 0x7F;
137
138         if (last_test && (bootmode & POST_POWERTEST)) {
139                 *last_test = (word >> 8) & 0xFF;
140         }
141
142         return bootmode;
143 }
144
145 /* POST tests run before relocation only mark status bits .... */
146 static void post_log_mark_start ( unsigned long testid )
147 {
148         gd->post_log_word |= testid;
149 }
150
151 static void post_log_mark_succ ( unsigned long testid )
152 {
153         gd->post_log_res |= testid;
154 }
155
156 /* ... and the messages are output once we are relocated */
157 void post_output_backlog ( void )
158 {
159         int j;
160
161         for (j = 0; j < post_list_size; j++) {
162                 if (gd->post_log_word & (post_list[j].testid)) {
163                         post_log ("POST %s ", post_list[j].cmd);
164                         if (gd->post_log_res & post_list[j].testid)
165                                 post_log ("PASSED\n");
166                         else {
167                                 post_log ("FAILED\n");
168                                 show_boot_progress (-31);
169                         }
170                 }
171         }
172 }
173
174 static void post_bootmode_test_on (unsigned int last_test)
175 {
176         unsigned long word = post_word_load ();
177
178         word |= POST_POWERTEST;
179
180         word |= (last_test & 0xFF) << 8;
181
182         post_word_store (word);
183 }
184
185 static void post_bootmode_test_off (void)
186 {
187         unsigned long word = post_word_load ();
188
189         word &= ~POST_POWERTEST;
190
191         post_word_store (word);
192 }
193
194 static void post_get_flags (int *test_flags)
195 {
196         int  flag[] = {  POST_POWERON,   POST_NORMAL,   POST_SLOWTEST,
197                          POST_CRITICAL };
198         char *var[] = { "post_poweron", "post_normal", "post_slowtest",
199                         "post_critical" };
200         int varnum = ARRAY_SIZE(var);
201         char list[128];                 /* long enough for POST list */
202         char *name;
203         char *s;
204         int last;
205         int i, j;
206
207         for (j = 0; j < post_list_size; j++) {
208                 test_flags[j] = post_list[j].flags;
209         }
210
211         for (i = 0; i < varnum; i++) {
212                 if (getenv_f(var[i], list, sizeof (list)) <= 0)
213                         continue;
214
215                 for (j = 0; j < post_list_size; j++) {
216                         test_flags[j] &= ~flag[i];
217                 }
218
219                 last = 0;
220                 name = list;
221                 while (!last) {
222                         while (*name && *name == ' ')
223                                 name++;
224                         if (*name == 0)
225                                 break;
226                         s = name + 1;
227                         while (*s && *s != ' ')
228                                 s++;
229                         if (*s == 0)
230                                 last = 1;
231                         else
232                                 *s = 0;
233
234                         for (j = 0; j < post_list_size; j++) {
235                                 if (strcmp (post_list[j].cmd, name) == 0) {
236                                         test_flags[j] |= flag[i];
237                                         break;
238                                 }
239                         }
240
241                         if (j == post_list_size) {
242                                 printf ("No such test: %s\n", name);
243                         }
244
245                         name = s + 1;
246                 }
247         }
248
249         for (j = 0; j < post_list_size; j++) {
250                 if (test_flags[j] & POST_POWERON) {
251                         test_flags[j] |= POST_SLOWTEST;
252                 }
253         }
254 }
255
256 void __show_post_progress (unsigned int test_num, int before, int result)
257 {
258 }
259 void show_post_progress (unsigned int, int, int)
260                         __attribute__((weak, alias("__show_post_progress")));
261
262 static int post_run_single (struct post_test *test,
263                                 int test_flags, int flags, unsigned int i)
264 {
265         if ((flags & test_flags & POST_ALWAYS) &&
266                 (flags & test_flags & POST_MEM)) {
267                 WATCHDOG_RESET ();
268
269                 if (!(flags & POST_REBOOT)) {
270                         if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
271                                 post_bootmode_test_on (
272                                         (gd->flags & GD_FLG_POSTFAIL) ?
273                                                 POST_FAIL_SAVE | i : i);
274                         }
275
276                         if (test_flags & POST_PREREL)
277                                 post_log_mark_start ( test->testid );
278                         else
279                                 post_log ("POST %s ", test->cmd);
280                 }
281
282                 show_post_progress(i, POST_BEFORE, POST_FAILED);
283
284                 if (test_flags & POST_PREREL) {
285                         if ((*test->test) (flags) == 0) {
286                                 post_log_mark_succ ( test->testid );
287                                 show_post_progress(i, POST_AFTER, POST_PASSED);
288                         }
289                         else {
290                                 show_post_progress(i, POST_AFTER, POST_FAILED);
291                                 if (test_flags & POST_CRITICAL)
292                                         gd->flags |= GD_FLG_POSTFAIL;
293                                 if (test_flags & POST_STOP)
294                                         gd->flags |= GD_FLG_POSTSTOP;
295                         }
296                 } else {
297                         if ((*test->test)(flags) != 0) {
298                                 post_log("FAILED\n");
299                                 show_boot_progress(-32);
300                                 show_post_progress(i, POST_AFTER, POST_FAILED);
301                                 if (test_flags & POST_CRITICAL)
302                                         gd->flags |= GD_FLG_POSTFAIL;
303                                 if (test_flags & POST_STOP)
304                                         gd->flags |= GD_FLG_POSTSTOP;
305                         } else {
306                                 post_log("PASSED\n");
307                                 show_post_progress(i, POST_AFTER, POST_PASSED);
308                         }
309                 }
310
311                 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
312                         post_bootmode_test_off ();
313                 }
314
315                 return 0;
316         } else {
317                 return -1;
318         }
319 }
320
321 int post_run (char *name, int flags)
322 {
323         unsigned int i;
324         int test_flags[POST_MAX_NUMBER];
325
326         post_get_flags (test_flags);
327
328         if (name == NULL) {
329                 unsigned int last;
330
331                 if (gd->flags & GD_FLG_POSTSTOP)
332                         return 0;
333
334                 if (post_bootmode_get (&last) & POST_POWERTEST) {
335                         if (last & POST_FAIL_SAVE) {
336                                 last &= ~POST_FAIL_SAVE;
337                                 gd->flags |= GD_FLG_POSTFAIL;
338                         }
339                         if (last < post_list_size &&
340                                 (flags & test_flags[last] & POST_ALWAYS) &&
341                                 (flags & test_flags[last] & POST_MEM)) {
342
343                                 post_run_single (post_list + last,
344                                                  test_flags[last],
345                                                  flags | POST_REBOOT, last);
346
347                                 for (i = last + 1; i < post_list_size; i++) {
348                                         if (gd->flags & GD_FLG_POSTSTOP)
349                                                 break;
350                                         post_run_single (post_list + i,
351                                                          test_flags[i],
352                                                          flags, i);
353                                 }
354                         }
355                 } else {
356                         for (i = 0; i < post_list_size; i++) {
357                                 if (gd->flags & GD_FLG_POSTSTOP)
358                                         break;
359                                 post_run_single (post_list + i,
360                                                  test_flags[i],
361                                                  flags, i);
362                         }
363                 }
364
365                 return 0;
366         } else {
367                 for (i = 0; i < post_list_size; i++) {
368                         if (strcmp (post_list[i].cmd, name) == 0)
369                                 break;
370                 }
371
372                 if (i < post_list_size) {
373                         WATCHDOG_RESET();
374                         return post_run_single (post_list + i,
375                                                 test_flags[i],
376                                                 flags, i);
377                 } else {
378                         return -1;
379                 }
380         }
381 }
382
383 static int post_info_single (struct post_test *test, int full)
384 {
385         if (test->flags & POST_MANUAL) {
386                 if (full)
387                         printf ("%s - %s\n"
388                                 "  %s\n", test->cmd, test->name, test->desc);
389                 else
390                         printf ("  %-15s - %s\n", test->cmd, test->name);
391
392                 return 0;
393         } else {
394                 return -1;
395         }
396 }
397
398 int post_info (char *name)
399 {
400         unsigned int i;
401
402         if (name == NULL) {
403                 for (i = 0; i < post_list_size; i++) {
404                         post_info_single (post_list + i, 0);
405                 }
406
407                 return 0;
408         } else {
409                 for (i = 0; i < post_list_size; i++) {
410                         if (strcmp (post_list[i].cmd, name) == 0)
411                                 break;
412                 }
413
414                 if (i < post_list_size) {
415                         return post_info_single (post_list + i, 1);
416                 } else {
417                         return -1;
418                 }
419         }
420 }
421
422 int post_log (char *format, ...)
423 {
424         va_list args;
425         uint i;
426         char printbuffer[CONFIG_SYS_PBSIZE];
427
428         va_start (args, format);
429
430         /* For this to work, printbuffer must be larger than
431          * anything we ever want to print.
432          */
433         i = vsprintf (printbuffer, format, args);
434         va_end (args);
435
436 #ifdef CONFIG_LOGBUFFER
437         /* Send to the logbuffer */
438         logbuff_log (printbuffer);
439 #else
440         /* Send to the stdout file */
441         puts (printbuffer);
442 #endif
443
444         return 0;
445 }
446
447 #ifdef CONFIG_NEEDS_MANUAL_RELOC
448 void post_reloc (void)
449 {
450         unsigned int i;
451
452         /*
453          * We have to relocate the test table manually
454          */
455         for (i = 0; i < post_list_size; i++) {
456                 ulong addr;
457                 struct post_test *test = post_list + i;
458
459                 if (test->name) {
460                         addr = (ulong) (test->name) + gd->reloc_off;
461                         test->name = (char *) addr;
462                 }
463
464                 if (test->cmd) {
465                         addr = (ulong) (test->cmd) + gd->reloc_off;
466                         test->cmd = (char *) addr;
467                 }
468
469                 if (test->desc) {
470                         addr = (ulong) (test->desc) + gd->reloc_off;
471                         test->desc = (char *) addr;
472                 }
473
474                 if (test->test) {
475                         addr = (ulong) (test->test) + gd->reloc_off;
476                         test->test = (int (*)(int flags)) addr;
477                 }
478
479                 if (test->init_f) {
480                         addr = (ulong) (test->init_f) + gd->reloc_off;
481                         test->init_f = (int (*)(void)) addr;
482                 }
483
484                 if (test->reloc) {
485                         addr = (ulong) (test->reloc) + gd->reloc_off;
486                         test->reloc = (void (*)(void)) addr;
487
488                         test->reloc();
489                 }
490         }
491 }
492 #endif
493
494
495 /*
496  * Some tests (e.g. SYSMON) need the time when post_init_f started,
497  * but we cannot use get_timer() at this point.
498  *
499  * On PowerPC we implement it using the timebase register.
500  */
501 unsigned long post_time_ms (unsigned long base)
502 {
503 #ifdef CONFIG_PPC
504         return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
505 #else
506 #warning "Not implemented yet"
507         return 0; /* Not implemented yet */
508 #endif
509 }