blob: b3df91aa5c0c67bf723151adb9013ef980d7757b [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");
wdenk63e73c92004-02-23 22:22:28 +0000134 else {
wdenk228f29a2002-12-08 09:53:23 +0000135 post_log ("FAILED\n");
wdenk63e73c92004-02-23 22:22:28 +0000136#ifdef CONFIG_SHOW_BOOT_PROGRESS
137 show_boot_progress(-31);
138#endif
139 }
wdenk228f29a2002-12-08 09:53:23 +0000140 }
141 }
142}
143
wdenka042ac82002-09-12 22:36:57 +0000144static void post_bootmode_test_on (unsigned int last_test)
145{
146 unsigned long word = post_word_load ();
147
148 word |= POST_POWERTEST;
149
150 word |= (last_test & 0xFF) << 8;
151
152 post_word_store (word);
153}
154
155static void post_bootmode_test_off (void)
156{
157 unsigned long word = post_word_load ();
158
159 word &= ~POST_POWERTEST;
160
161 post_word_store (word);
162}
163
164static void post_get_flags (int *test_flags)
165{
wdenk8564acf2003-07-14 22:13:32 +0000166 int flag[] = { POST_POWERON, POST_NORMAL, POST_SLOWTEST };
167 char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
wdenka042ac82002-09-12 22:36:57 +0000168 int varnum = sizeof (var) / sizeof (var[0]);
169 char list[128]; /* long enough for POST list */
170 char *name;
171 char *s;
172 int last;
173 int i, j;
174
175 for (j = 0; j < post_list_size; j++) {
176 test_flags[j] = post_list[j].flags;
177 }
178
179 for (i = 0; i < varnum; i++) {
180 if (getenv_r (var[i], list, sizeof (list)) <= 0)
181 continue;
182
183 for (j = 0; j < post_list_size; j++) {
184 test_flags[j] &= ~flag[i];
185 }
186
187 last = 0;
188 name = list;
189 while (!last) {
190 while (*name && *name == ' ')
191 name++;
192 if (*name == 0)
193 break;
194 s = name + 1;
195 while (*s && *s != ' ')
196 s++;
197 if (*s == 0)
198 last = 1;
199 else
200 *s = 0;
201
202 for (j = 0; j < post_list_size; j++) {
203 if (strcmp (post_list[j].cmd, name) == 0) {
204 test_flags[j] |= flag[i];
205 break;
206 }
207 }
208
209 if (j == post_list_size) {
210 printf ("No such test: %s\n", name);
211 }
212
213 name = s + 1;
214 }
215 }
wdenk6dff5522003-07-15 07:45:49 +0000216
217 for (j = 0; j < post_list_size; j++) {
218 if (test_flags[j] & POST_POWERON) {
219 test_flags[j] |= POST_SLOWTEST;
220 }
221 }
wdenka042ac82002-09-12 22:36:57 +0000222}
223
224static int post_run_single (struct post_test *test,
225 int test_flags, int flags, unsigned int i)
226{
227 if ((flags & test_flags & POST_ALWAYS) &&
228 (flags & test_flags & POST_MEM)) {
229 WATCHDOG_RESET ();
230
231 if (!(flags & POST_REBOOT)) {
232 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
233 post_bootmode_test_on (i);
234 }
235
wdenk228f29a2002-12-08 09:53:23 +0000236 if (test_flags & POST_PREREL)
237 post_log_mark_start ( test->testid );
238 else
wdenk56f94be2002-11-05 16:35:14 +0000239 post_log ("POST %s ", test->cmd);
wdenka042ac82002-09-12 22:36:57 +0000240 }
241
wdenk228f29a2002-12-08 09:53:23 +0000242 if (test_flags & POST_PREREL) {
243 if ((*test->test) (flags) == 0)
244 post_log_mark_succ ( test->testid );
245 } else {
wdenk63e73c92004-02-23 22:22:28 +0000246 if ((*test->test) (flags) != 0) {
wdenka042ac82002-09-12 22:36:57 +0000247 post_log ("FAILED\n");
wdenk63e73c92004-02-23 22:22:28 +0000248#ifdef CONFIG_SHOW_BOOT_PROGRESS
249 show_boot_progress(-32);
250#endif
251 }
wdenka042ac82002-09-12 22:36:57 +0000252 else
253 post_log ("PASSED\n");
wdenk228f29a2002-12-08 09:53:23 +0000254 }
wdenka042ac82002-09-12 22:36:57 +0000255
256 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
257 post_bootmode_test_off ();
258 }
259
260 return 0;
261 } else {
262 return -1;
263 }
264}
265
266int post_run (char *name, int flags)
267{
268 unsigned int i;
269 int test_flags[POST_MAX_NUMBER];
270
271 post_get_flags (test_flags);
272
273 if (name == NULL) {
274 unsigned int last;
275
276 if (post_bootmode_get (&last) & POST_POWERTEST) {
277 if (last < post_list_size &&
278 (flags & test_flags[last] & POST_ALWAYS) &&
279 (flags & test_flags[last] & POST_MEM)) {
280
wdenkea909b72002-11-21 23:11:29 +0000281 post_run_single (post_list + last,
282 test_flags[last],
283 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000284
285 for (i = last + 1; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000286 post_run_single (post_list + i,
287 test_flags[i],
288 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000289 }
290 }
291 } else {
292 for (i = 0; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000293 post_run_single (post_list + i,
294 test_flags[i],
295 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000296 }
297 }
298
299 return 0;
300 } else {
301 for (i = 0; i < post_list_size; i++) {
302 if (strcmp (post_list[i].cmd, name) == 0)
303 break;
304 }
305
306 if (i < post_list_size) {
307 return post_run_single (post_list + i,
308 test_flags[i],
309 flags, i);
310 } else {
311 return -1;
312 }
313 }
314}
315
316static int post_info_single (struct post_test *test, int full)
317{
318 if (test->flags & POST_MANUAL) {
319 if (full)
320 printf ("%s - %s\n"
321 " %s\n", test->cmd, test->name, test->desc);
322 else
323 printf (" %-15s - %s\n", test->cmd, test->name);
324
325 return 0;
326 } else {
327 return -1;
328 }
329}
330
331int post_info (char *name)
332{
333 unsigned int i;
334
335 if (name == NULL) {
336 for (i = 0; i < post_list_size; i++) {
337 post_info_single (post_list + i, 0);
338 }
339
340 return 0;
341 } else {
342 for (i = 0; i < post_list_size; i++) {
343 if (strcmp (post_list[i].cmd, name) == 0)
344 break;
345 }
346
347 if (i < post_list_size) {
348 return post_info_single (post_list + i, 1);
349 } else {
350 return -1;
351 }
352 }
353}
354
355int post_log (char *format, ...)
356{
357 va_list args;
358 uint i;
359 char printbuffer[CFG_PBSIZE];
360
361 va_start (args, format);
362
363 /* For this to work, printbuffer must be larger than
364 * anything we ever want to print.
365 */
366 i = vsprintf (printbuffer, format, args);
367 va_end (args);
368
wdenk56f94be2002-11-05 16:35:14 +0000369#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000370 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000371 logbuff_log (printbuffer);
372#else
wdenka042ac82002-09-12 22:36:57 +0000373 /* Send to the stdout file */
374 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000375#endif
wdenka042ac82002-09-12 22:36:57 +0000376
377 return 0;
378}
379
380void post_reloc (void)
381{
382 DECLARE_GLOBAL_DATA_PTR;
383
384 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
437 return (unsigned long)get_ticks () / (get_tbclk () / CFG_HZ) - base;
438#else
439 return 0; /* Not implemented yet */
440#endif
441}
442
wdenka042ac82002-09-12 22:36:57 +0000443#endif /* CONFIG_POST */