blob: 89ad6f0d58e3d94f8fa248e234523107fc81ac84 [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
wdenka042ac82002-09-12 22:36:57 +000033#ifdef CONFIG_POST
34
35#define POST_MAX_NUMBER 32
36
37#define BOOTMODE_MAGIC 0xDEAD0000
38
wdenk4532cb62003-04-27 22:52:51 +000039int post_init_f (void)
40{
41 DECLARE_GLOBAL_DATA_PTR;
42
43 int res = 0;
44 unsigned int i;
45
46 for (i = 0; i < post_list_size; i++) {
47 struct post_test *test = post_list + i;
48
49 if (test->init_f && test->init_f()) {
50 res = -1;
51 }
52 }
wdenk8bde7f72003-06-27 21:31:46 +000053
wdenk4532cb62003-04-27 22:52:51 +000054 gd->post_init_f_time = post_time_ms(0);
55 if (!gd->post_init_f_time)
56 {
57 printf("post/post.c: post_time_ms seems not to be implemented\n");
58 }
59
60 return res;
61}
62
wdenka042ac82002-09-12 22:36:57 +000063void post_bootmode_init (void)
64{
wdenk228f29a2002-12-08 09:53:23 +000065 DECLARE_GLOBAL_DATA_PTR;
wdenka042ac82002-09-12 22:36:57 +000066 int bootmode = post_bootmode_get (0);
wdenk27b207f2003-07-24 23:38:38 +000067 int newword;
wdenk42d1f032003-10-15 23:53:47 +000068
wdenk27b207f2003-07-24 23:38:38 +000069 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
70 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
wdenk6dff5522003-07-15 07:45:49 +000071 } else if (bootmode == 0) {
wdenk27b207f2003-07-24 23:38:38 +000072 newword = BOOTMODE_MAGIC | POST_POWERON;
wdenk6dff5522003-07-15 07:45:49 +000073 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
wdenk27b207f2003-07-24 23:38:38 +000074 newword = BOOTMODE_MAGIC | POST_NORMAL;
wdenka042ac82002-09-12 22:36:57 +000075 } else {
wdenk27b207f2003-07-24 23:38:38 +000076 /* Use old value */
77 newword = post_word_load () & ~POST_COLDBOOT;
wdenka042ac82002-09-12 22:36:57 +000078 }
79
wdenk27b207f2003-07-24 23:38:38 +000080 if (bootmode == 0)
81 {
82 /* We are booting after power-on */
83 newword |= POST_COLDBOOT;
84 }
85
86 post_word_store (newword);
87
wdenk228f29a2002-12-08 09:53:23 +000088 /* Reset activity record */
89 gd->post_log_word = 0;
wdenka042ac82002-09-12 22:36:57 +000090}
91
92int post_bootmode_get (unsigned int *last_test)
93{
94 unsigned long word = post_word_load ();
95 int bootmode;
96
97 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
98 return 0;
99 }
100
wdenk27b207f2003-07-24 23:38:38 +0000101 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +0000102
103 if (last_test && (bootmode & POST_POWERTEST)) {
104 *last_test = (word >> 8) & 0xFF;
105 }
106
107 return bootmode;
108}
109
wdenk228f29a2002-12-08 09:53:23 +0000110/* POST tests run before relocation only mark status bits .... */
111static void post_log_mark_start ( unsigned long testid )
112{
113 DECLARE_GLOBAL_DATA_PTR;
114 gd->post_log_word |= (testid)<<16;
115}
116
117static void post_log_mark_succ ( unsigned long testid )
118{
119 DECLARE_GLOBAL_DATA_PTR;
120 gd->post_log_word |= testid;
121}
122
123/* ... and the messages are output once we are relocated */
124void post_output_backlog ( void )
125{
126 DECLARE_GLOBAL_DATA_PTR;
127 int j;
128
129 for (j = 0; j < post_list_size; j++) {
130 if (gd->post_log_word & (post_list[j].testid<<16)) {
131 post_log ("POST %s ", post_list[j].cmd);
132 if (gd->post_log_word & post_list[j].testid)
133 post_log ("PASSED\n");
134 else
135 post_log ("FAILED\n");
136 }
137 }
138}
139
wdenka042ac82002-09-12 22:36:57 +0000140static void post_bootmode_test_on (unsigned int last_test)
141{
142 unsigned long word = post_word_load ();
143
144 word |= POST_POWERTEST;
145
146 word |= (last_test & 0xFF) << 8;
147
148 post_word_store (word);
149}
150
151static void post_bootmode_test_off (void)
152{
153 unsigned long word = post_word_load ();
154
155 word &= ~POST_POWERTEST;
156
157 post_word_store (word);
158}
159
160static void post_get_flags (int *test_flags)
161{
wdenk8564acf2003-07-14 22:13:32 +0000162 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST };
163 char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
wdenka042ac82002-09-12 22:36:57 +0000164 int varnum = sizeof (var) / sizeof (var[0]);
165 char list[128]; /* long enough for POST list */
166 char *name;
167 char *s;
168 int last;
169 int i, j;
170
171 for (j = 0; j < post_list_size; j++) {
172 test_flags[j] = post_list[j].flags;
173 }
174
175 for (i = 0; i < varnum; i++) {
176 if (getenv_r (var[i], list, sizeof (list)) <= 0)
177 continue;
178
179 for (j = 0; j < post_list_size; j++) {
180 test_flags[j] &= ~flag[i];
181 }
182
183 last = 0;
184 name = list;
185 while (!last) {
186 while (*name && *name == ' ')
187 name++;
188 if (*name == 0)
189 break;
190 s = name + 1;
191 while (*s && *s != ' ')
192 s++;
193 if (*s == 0)
194 last = 1;
195 else
196 *s = 0;
197
198 for (j = 0; j < post_list_size; j++) {
199 if (strcmp (post_list[j].cmd, name) == 0) {
200 test_flags[j] |= flag[i];
201 break;
202 }
203 }
204
205 if (j == post_list_size) {
206 printf ("No such test: %s\n", name);
207 }
208
209 name = s + 1;
210 }
211 }
wdenk6dff5522003-07-15 07:45:49 +0000212
213 for (j = 0; j < post_list_size; j++) {
214 if (test_flags[j] & POST_POWERON) {
215 test_flags[j] |= POST_SLOWTEST;
216 }
217 }
wdenka042ac82002-09-12 22:36:57 +0000218}
219
220static int post_run_single (struct post_test *test,
221 int test_flags, int flags, unsigned int i)
222{
223 if ((flags & test_flags & POST_ALWAYS) &&
224 (flags & test_flags & POST_MEM)) {
225 WATCHDOG_RESET ();
226
227 if (!(flags & POST_REBOOT)) {
228 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
229 post_bootmode_test_on (i);
230 }
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 );
241 } else {
wdenka042ac82002-09-12 22:36:57 +0000242 if ((*test->test) (flags) != 0)
243 post_log ("FAILED\n");
244 else
245 post_log ("PASSED\n");
wdenk228f29a2002-12-08 09:53:23 +0000246 }
wdenka042ac82002-09-12 22:36:57 +0000247
248 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
249 post_bootmode_test_off ();
250 }
251
252 return 0;
253 } else {
254 return -1;
255 }
256}
257
258int post_run (char *name, int flags)
259{
260 unsigned int i;
261 int test_flags[POST_MAX_NUMBER];
262
263 post_get_flags (test_flags);
264
265 if (name == NULL) {
266 unsigned int last;
267
268 if (post_bootmode_get (&last) & POST_POWERTEST) {
269 if (last < post_list_size &&
270 (flags & test_flags[last] & POST_ALWAYS) &&
271 (flags & test_flags[last] & POST_MEM)) {
272
wdenkea909b72002-11-21 23:11:29 +0000273 post_run_single (post_list + last,
274 test_flags[last],
275 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000276
277 for (i = last + 1; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000278 post_run_single (post_list + i,
279 test_flags[i],
280 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000281 }
282 }
283 } else {
284 for (i = 0; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000285 post_run_single (post_list + i,
286 test_flags[i],
287 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000288 }
289 }
290
291 return 0;
292 } else {
293 for (i = 0; i < post_list_size; i++) {
294 if (strcmp (post_list[i].cmd, name) == 0)
295 break;
296 }
297
298 if (i < post_list_size) {
299 return post_run_single (post_list + i,
300 test_flags[i],
301 flags, i);
302 } else {
303 return -1;
304 }
305 }
306}
307
308static int post_info_single (struct post_test *test, int full)
309{
310 if (test->flags & POST_MANUAL) {
311 if (full)
312 printf ("%s - %s\n"
313 " %s\n", test->cmd, test->name, test->desc);
314 else
315 printf (" %-15s - %s\n", test->cmd, test->name);
316
317 return 0;
318 } else {
319 return -1;
320 }
321}
322
323int post_info (char *name)
324{
325 unsigned int i;
326
327 if (name == NULL) {
328 for (i = 0; i < post_list_size; i++) {
329 post_info_single (post_list + i, 0);
330 }
331
332 return 0;
333 } else {
334 for (i = 0; i < post_list_size; i++) {
335 if (strcmp (post_list[i].cmd, name) == 0)
336 break;
337 }
338
339 if (i < post_list_size) {
340 return post_info_single (post_list + i, 1);
341 } else {
342 return -1;
343 }
344 }
345}
346
347int post_log (char *format, ...)
348{
349 va_list args;
350 uint i;
351 char printbuffer[CFG_PBSIZE];
352
353 va_start (args, format);
354
355 /* For this to work, printbuffer must be larger than
356 * anything we ever want to print.
357 */
358 i = vsprintf (printbuffer, format, args);
359 va_end (args);
360
wdenk56f94be2002-11-05 16:35:14 +0000361#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000362 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000363 logbuff_log (printbuffer);
364#else
wdenka042ac82002-09-12 22:36:57 +0000365 /* Send to the stdout file */
366 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000367#endif
wdenka042ac82002-09-12 22:36:57 +0000368
369 return 0;
370}
371
372void post_reloc (void)
373{
374 DECLARE_GLOBAL_DATA_PTR;
375
376 unsigned int i;
377
378 /*
379 * We have to relocate the test table manually
380 */
381 for (i = 0; i < post_list_size; i++) {
382 ulong addr;
383 struct post_test *test = post_list + i;
384
385 if (test->name) {
386 addr = (ulong) (test->name) + gd->reloc_off;
387 test->name = (char *) addr;
388 }
389
390 if (test->cmd) {
391 addr = (ulong) (test->cmd) + gd->reloc_off;
392 test->cmd = (char *) addr;
393 }
394
395 if (test->desc) {
396 addr = (ulong) (test->desc) + gd->reloc_off;
397 test->desc = (char *) addr;
398 }
399
400 if (test->test) {
401 addr = (ulong) (test->test) + gd->reloc_off;
402 test->test = (int (*)(int flags)) addr;
403 }
wdenk4532cb62003-04-27 22:52:51 +0000404
405 if (test->init_f) {
406 addr = (ulong) (test->init_f) + gd->reloc_off;
407 test->init_f = (int (*)(void)) addr;
408 }
409
410 if (test->reloc) {
411 addr = (ulong) (test->reloc) + gd->reloc_off;
412 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000413
wdenk4532cb62003-04-27 22:52:51 +0000414 test->reloc();
415 }
wdenka042ac82002-09-12 22:36:57 +0000416 }
417}
418
wdenk4532cb62003-04-27 22:52:51 +0000419
420/*
421 * Some tests (e.g. SYSMON) need the time when post_init_f started,
422 * but we cannot use get_timer() at this point.
423 *
424 * On PowerPC we implement it using the timebase register.
425 */
426unsigned long post_time_ms (unsigned long base)
427{
428#ifdef CONFIG_PPC
429 return (unsigned long)get_ticks () / (get_tbclk () / CFG_HZ) - base;
430#else
431 return 0; /* Not implemented yet */
432#endif
433}
434
wdenka042ac82002-09-12 22:36:57 +0000435#endif /* CONFIG_POST */