blob: c016c3ae4062b8575ee2194697e627168ea244f4 [file] [log] [blame]
wdenka042ac82002-09-12 22:36:57 +00001/*
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 <console.h>
26#include <watchdog.h>
27#include <post.h>
28
wdenk56f94be2002-11-05 16:35:14 +000029#ifdef CONFIG_LOGBUFFER
30#include <logbuff.h>
31#endif
32
Wolfgang Denkd87080b2006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenka042ac82002-09-12 22:36:57 +000035#define POST_MAX_NUMBER 32
36
37#define BOOTMODE_MAGIC 0xDEAD0000
38
wdenk4532cb62003-04-27 22:52:51 +000039int post_init_f (void)
40{
wdenk4532cb62003-04-27 22:52:51 +000041 int res = 0;
42 unsigned int i;
43
44 for (i = 0; i < post_list_size; i++) {
45 struct post_test *test = post_list + i;
46
47 if (test->init_f && test->init_f()) {
48 res = -1;
49 }
50 }
wdenk8bde7f72003-06-27 21:31:46 +000051
wdenk4532cb62003-04-27 22:52:51 +000052 gd->post_init_f_time = post_time_ms(0);
53 if (!gd->post_init_f_time)
54 {
55 printf("post/post.c: post_time_ms seems not to be implemented\n");
56 }
57
58 return res;
59}
60
wdenka042ac82002-09-12 22:36:57 +000061void post_bootmode_init (void)
62{
63 int bootmode = post_bootmode_get (0);
wdenk27b207f2003-07-24 23:38:38 +000064 int newword;
wdenk42d1f032003-10-15 23:53:47 +000065
wdenk27b207f2003-07-24 23:38:38 +000066 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
67 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
wdenk6dff5522003-07-15 07:45:49 +000068 } else if (bootmode == 0) {
wdenk27b207f2003-07-24 23:38:38 +000069 newword = BOOTMODE_MAGIC | POST_POWERON;
wdenk6dff5522003-07-15 07:45:49 +000070 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
wdenk27b207f2003-07-24 23:38:38 +000071 newword = BOOTMODE_MAGIC | POST_NORMAL;
wdenka042ac82002-09-12 22:36:57 +000072 } else {
wdenk27b207f2003-07-24 23:38:38 +000073 /* Use old value */
74 newword = post_word_load () & ~POST_COLDBOOT;
wdenka042ac82002-09-12 22:36:57 +000075 }
76
wdenk27b207f2003-07-24 23:38:38 +000077 if (bootmode == 0)
78 {
79 /* We are booting after power-on */
80 newword |= POST_COLDBOOT;
81 }
82
83 post_word_store (newword);
84
wdenk228f29a2002-12-08 09:53:23 +000085 /* Reset activity record */
86 gd->post_log_word = 0;
wdenka042ac82002-09-12 22:36:57 +000087}
88
89int post_bootmode_get (unsigned int *last_test)
90{
91 unsigned long word = post_word_load ();
92 int bootmode;
93
94 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
95 return 0;
96 }
97
wdenk27b207f2003-07-24 23:38:38 +000098 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +000099
100 if (last_test && (bootmode & POST_POWERTEST)) {
101 *last_test = (word >> 8) & 0xFF;
102 }
103
104 return bootmode;
105}
106
wdenk228f29a2002-12-08 09:53:23 +0000107/* POST tests run before relocation only mark status bits .... */
108static void post_log_mark_start ( unsigned long testid )
109{
wdenk228f29a2002-12-08 09:53:23 +0000110 gd->post_log_word |= (testid)<<16;
111}
112
113static void post_log_mark_succ ( unsigned long testid )
114{
wdenk228f29a2002-12-08 09:53:23 +0000115 gd->post_log_word |= testid;
116}
117
118/* ... and the messages are output once we are relocated */
119void post_output_backlog ( void )
120{
wdenk228f29a2002-12-08 09:53:23 +0000121 int j;
122
123 for (j = 0; j < post_list_size; j++) {
124 if (gd->post_log_word & (post_list[j].testid<<16)) {
125 post_log ("POST %s ", post_list[j].cmd);
126 if (gd->post_log_word & post_list[j].testid)
127 post_log ("PASSED\n");
wdenk63e73c92004-02-23 22:22:28 +0000128 else {
wdenk228f29a2002-12-08 09:53:23 +0000129 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200130 show_boot_progress (-31);
wdenk63e73c92004-02-23 22:22:28 +0000131 }
wdenk228f29a2002-12-08 09:53:23 +0000132 }
133 }
134}
135
wdenka042ac82002-09-12 22:36:57 +0000136static void post_bootmode_test_on (unsigned int last_test)
137{
138 unsigned long word = post_word_load ();
139
140 word |= POST_POWERTEST;
141
142 word |= (last_test & 0xFF) << 8;
143
144 post_word_store (word);
145}
146
147static void post_bootmode_test_off (void)
148{
149 unsigned long word = post_word_load ();
150
151 word &= ~POST_POWERTEST;
152
153 post_word_store (word);
154}
155
156static void post_get_flags (int *test_flags)
157{
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100158 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST,
159 POST_CRITICAL };
160 char *var[] = { "post_poweron", "post_normal", "post_slowtest",
161 "post_critical" };
wdenka042ac82002-09-12 22:36:57 +0000162 int varnum = sizeof (var) / sizeof (var[0]);
163 char list[128]; /* long enough for POST list */
164 char *name;
165 char *s;
166 int last;
167 int i, j;
168
169 for (j = 0; j < post_list_size; j++) {
170 test_flags[j] = post_list[j].flags;
171 }
172
173 for (i = 0; i < varnum; i++) {
174 if (getenv_r (var[i], list, sizeof (list)) <= 0)
175 continue;
176
177 for (j = 0; j < post_list_size; j++) {
178 test_flags[j] &= ~flag[i];
179 }
180
181 last = 0;
182 name = list;
183 while (!last) {
184 while (*name && *name == ' ')
185 name++;
186 if (*name == 0)
187 break;
188 s = name + 1;
189 while (*s && *s != ' ')
190 s++;
191 if (*s == 0)
192 last = 1;
193 else
194 *s = 0;
195
196 for (j = 0; j < post_list_size; j++) {
197 if (strcmp (post_list[j].cmd, name) == 0) {
198 test_flags[j] |= flag[i];
199 break;
200 }
201 }
202
203 if (j == post_list_size) {
204 printf ("No such test: %s\n", name);
205 }
206
207 name = s + 1;
208 }
209 }
wdenk6dff5522003-07-15 07:45:49 +0000210
211 for (j = 0; j < post_list_size; j++) {
212 if (test_flags[j] & POST_POWERON) {
213 test_flags[j] |= POST_SLOWTEST;
214 }
215 }
wdenka042ac82002-09-12 22:36:57 +0000216}
217
218static int post_run_single (struct post_test *test,
219 int test_flags, int flags, unsigned int i)
220{
221 if ((flags & test_flags & POST_ALWAYS) &&
222 (flags & test_flags & POST_MEM)) {
223 WATCHDOG_RESET ();
224
225 if (!(flags & POST_REBOOT)) {
226 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100227 post_bootmode_test_on (
228 (gd->flags & GD_FLG_POSTFAIL) ?
229 POST_FAIL_SAVE | i : i);
wdenka042ac82002-09-12 22:36:57 +0000230 }
231
wdenk228f29a2002-12-08 09:53:23 +0000232 if (test_flags & POST_PREREL)
233 post_log_mark_start ( test->testid );
234 else
wdenk56f94be2002-11-05 16:35:14 +0000235 post_log ("POST %s ", test->cmd);
wdenka042ac82002-09-12 22:36:57 +0000236 }
237
wdenk228f29a2002-12-08 09:53:23 +0000238 if (test_flags & POST_PREREL) {
239 if ((*test->test) (flags) == 0)
240 post_log_mark_succ ( test->testid );
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100241 else if (test_flags & POST_CRITICAL)
242 gd->flags |= GD_FLG_POSTFAIL;
wdenk228f29a2002-12-08 09:53:23 +0000243 } else {
wdenk63e73c92004-02-23 22:22:28 +0000244 if ((*test->test) (flags) != 0) {
wdenka042ac82002-09-12 22:36:57 +0000245 post_log ("FAILED\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200246 show_boot_progress (-32);
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100247 if (test_flags & POST_CRITICAL)
248 gd->flags |= GD_FLG_POSTFAIL;
wdenk63e73c92004-02-23 22:22:28 +0000249 }
wdenka042ac82002-09-12 22:36:57 +0000250 else
251 post_log ("PASSED\n");
wdenk228f29a2002-12-08 09:53:23 +0000252 }
wdenka042ac82002-09-12 22:36:57 +0000253
254 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
255 post_bootmode_test_off ();
256 }
257
258 return 0;
259 } else {
260 return -1;
261 }
262}
263
264int post_run (char *name, int flags)
265{
266 unsigned int i;
267 int test_flags[POST_MAX_NUMBER];
268
269 post_get_flags (test_flags);
270
271 if (name == NULL) {
272 unsigned int last;
273
274 if (post_bootmode_get (&last) & POST_POWERTEST) {
Yuri Tikhonove262efe2008-02-04 14:11:03 +0100275 if (last & POST_FAIL_SAVE) {
276 last &= ~POST_FAIL_SAVE;
277 gd->flags |= GD_FLG_POSTFAIL;
278 }
wdenka042ac82002-09-12 22:36:57 +0000279 if (last < post_list_size &&
280 (flags & test_flags[last] & POST_ALWAYS) &&
281 (flags & test_flags[last] & POST_MEM)) {
282
wdenkea909b72002-11-21 23:11:29 +0000283 post_run_single (post_list + last,
284 test_flags[last],
285 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000286
287 for (i = last + 1; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000288 post_run_single (post_list + i,
289 test_flags[i],
290 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000291 }
292 }
293 } else {
294 for (i = 0; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000295 post_run_single (post_list + i,
296 test_flags[i],
297 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000298 }
299 }
300
301 return 0;
302 } else {
303 for (i = 0; i < post_list_size; i++) {
304 if (strcmp (post_list[i].cmd, name) == 0)
305 break;
306 }
307
308 if (i < post_list_size) {
309 return post_run_single (post_list + i,
310 test_flags[i],
311 flags, i);
312 } else {
313 return -1;
314 }
315 }
316}
317
318static int post_info_single (struct post_test *test, int full)
319{
320 if (test->flags & POST_MANUAL) {
321 if (full)
322 printf ("%s - %s\n"
323 " %s\n", test->cmd, test->name, test->desc);
324 else
325 printf (" %-15s - %s\n", test->cmd, test->name);
326
327 return 0;
328 } else {
329 return -1;
330 }
331}
332
333int post_info (char *name)
334{
335 unsigned int i;
336
337 if (name == NULL) {
338 for (i = 0; i < post_list_size; i++) {
339 post_info_single (post_list + i, 0);
340 }
341
342 return 0;
343 } else {
344 for (i = 0; i < post_list_size; i++) {
345 if (strcmp (post_list[i].cmd, name) == 0)
346 break;
347 }
348
349 if (i < post_list_size) {
350 return post_info_single (post_list + i, 1);
351 } else {
352 return -1;
353 }
354 }
355}
356
357int post_log (char *format, ...)
358{
359 va_list args;
360 uint i;
361 char printbuffer[CFG_PBSIZE];
362
363 va_start (args, format);
364
365 /* For this to work, printbuffer must be larger than
366 * anything we ever want to print.
367 */
368 i = vsprintf (printbuffer, format, args);
369 va_end (args);
370
wdenk56f94be2002-11-05 16:35:14 +0000371#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000372 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000373 logbuff_log (printbuffer);
374#else
wdenka042ac82002-09-12 22:36:57 +0000375 /* Send to the stdout file */
376 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000377#endif
wdenka042ac82002-09-12 22:36:57 +0000378
379 return 0;
380}
381
382void post_reloc (void)
383{
wdenka042ac82002-09-12 22:36:57 +0000384 unsigned int i;
385
386 /*
387 * We have to relocate the test table manually
388 */
389 for (i = 0; i < post_list_size; i++) {
390 ulong addr;
391 struct post_test *test = post_list + i;
392
393 if (test->name) {
394 addr = (ulong) (test->name) + gd->reloc_off;
395 test->name = (char *) addr;
396 }
397
398 if (test->cmd) {
399 addr = (ulong) (test->cmd) + gd->reloc_off;
400 test->cmd = (char *) addr;
401 }
402
403 if (test->desc) {
404 addr = (ulong) (test->desc) + gd->reloc_off;
405 test->desc = (char *) addr;
406 }
407
408 if (test->test) {
409 addr = (ulong) (test->test) + gd->reloc_off;
410 test->test = (int (*)(int flags)) addr;
411 }
wdenk4532cb62003-04-27 22:52:51 +0000412
413 if (test->init_f) {
414 addr = (ulong) (test->init_f) + gd->reloc_off;
415 test->init_f = (int (*)(void)) addr;
416 }
417
418 if (test->reloc) {
419 addr = (ulong) (test->reloc) + gd->reloc_off;
420 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000421
wdenk4532cb62003-04-27 22:52:51 +0000422 test->reloc();
423 }
wdenka042ac82002-09-12 22:36:57 +0000424 }
425}
426
wdenk4532cb62003-04-27 22:52:51 +0000427
428/*
429 * Some tests (e.g. SYSMON) need the time when post_init_f started,
430 * but we cannot use get_timer() at this point.
431 *
432 * On PowerPC we implement it using the timebase register.
433 */
434unsigned long post_time_ms (unsigned long base)
435{
436#ifdef CONFIG_PPC
Igor Lisitsina11e0692007-03-28 19:06:19 +0400437 return (unsigned long)(get_ticks () / (get_tbclk () / CFG_HZ)) - base;
wdenk4532cb62003-04-27 22:52:51 +0000438#else
Wolfgang Denkad5bb452007-03-06 18:08:43 +0100439#warning "Not implemented yet"
wdenk4532cb62003-04-27 22:52:51 +0000440 return 0; /* Not implemented yet */
441#endif
442}