blob: ac419908605578ce66322fe5ea26ba08303a9073 [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
Wolfgang Denkd87080b2006-03-31 18:32:53 +020035DECLARE_GLOBAL_DATA_PTR;
36
wdenka042ac82002-09-12 22:36:57 +000037#define POST_MAX_NUMBER 32
38
39#define BOOTMODE_MAGIC 0xDEAD0000
40
wdenk4532cb62003-04-27 22:52:51 +000041int post_init_f (void)
42{
wdenk4532cb62003-04-27 22:52:51 +000043 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{
65 int bootmode = post_bootmode_get (0);
wdenk27b207f2003-07-24 23:38:38 +000066 int newword;
wdenk42d1f032003-10-15 23:53:47 +000067
wdenk27b207f2003-07-24 23:38:38 +000068 if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
69 newword = BOOTMODE_MAGIC | POST_SLOWTEST;
wdenk6dff5522003-07-15 07:45:49 +000070 } else if (bootmode == 0) {
wdenk27b207f2003-07-24 23:38:38 +000071 newword = BOOTMODE_MAGIC | POST_POWERON;
wdenk6dff5522003-07-15 07:45:49 +000072 } else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
wdenk27b207f2003-07-24 23:38:38 +000073 newword = BOOTMODE_MAGIC | POST_NORMAL;
wdenka042ac82002-09-12 22:36:57 +000074 } else {
wdenk27b207f2003-07-24 23:38:38 +000075 /* Use old value */
76 newword = post_word_load () & ~POST_COLDBOOT;
wdenka042ac82002-09-12 22:36:57 +000077 }
78
wdenk27b207f2003-07-24 23:38:38 +000079 if (bootmode == 0)
80 {
81 /* We are booting after power-on */
82 newword |= POST_COLDBOOT;
83 }
84
85 post_word_store (newword);
86
wdenk228f29a2002-12-08 09:53:23 +000087 /* Reset activity record */
88 gd->post_log_word = 0;
wdenka042ac82002-09-12 22:36:57 +000089}
90
91int post_bootmode_get (unsigned int *last_test)
92{
93 unsigned long word = post_word_load ();
94 int bootmode;
95
96 if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
97 return 0;
98 }
99
wdenk27b207f2003-07-24 23:38:38 +0000100 bootmode = word & 0x7F;
wdenka042ac82002-09-12 22:36:57 +0000101
102 if (last_test && (bootmode & POST_POWERTEST)) {
103 *last_test = (word >> 8) & 0xFF;
104 }
105
106 return bootmode;
107}
108
wdenk228f29a2002-12-08 09:53:23 +0000109/* POST tests run before relocation only mark status bits .... */
110static void post_log_mark_start ( unsigned long testid )
111{
wdenk228f29a2002-12-08 09:53:23 +0000112 gd->post_log_word |= (testid)<<16;
113}
114
115static void post_log_mark_succ ( unsigned long testid )
116{
wdenk228f29a2002-12-08 09:53:23 +0000117 gd->post_log_word |= testid;
118}
119
120/* ... and the messages are output once we are relocated */
121void post_output_backlog ( void )
122{
wdenk228f29a2002-12-08 09:53:23 +0000123 int j;
124
125 for (j = 0; j < post_list_size; j++) {
126 if (gd->post_log_word & (post_list[j].testid<<16)) {
127 post_log ("POST %s ", post_list[j].cmd);
128 if (gd->post_log_word & post_list[j].testid)
129 post_log ("PASSED\n");
wdenk63e73c92004-02-23 22:22:28 +0000130 else {
wdenk228f29a2002-12-08 09:53:23 +0000131 post_log ("FAILED\n");
wdenk63e73c92004-02-23 22:22:28 +0000132#ifdef CONFIG_SHOW_BOOT_PROGRESS
133 show_boot_progress(-31);
134#endif
135 }
wdenk228f29a2002-12-08 09:53:23 +0000136 }
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 {
wdenk63e73c92004-02-23 22:22:28 +0000242 if ((*test->test) (flags) != 0) {
wdenka042ac82002-09-12 22:36:57 +0000243 post_log ("FAILED\n");
wdenk63e73c92004-02-23 22:22:28 +0000244#ifdef CONFIG_SHOW_BOOT_PROGRESS
245 show_boot_progress(-32);
246#endif
247 }
wdenka042ac82002-09-12 22:36:57 +0000248 else
249 post_log ("PASSED\n");
wdenk228f29a2002-12-08 09:53:23 +0000250 }
wdenka042ac82002-09-12 22:36:57 +0000251
252 if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
253 post_bootmode_test_off ();
254 }
255
256 return 0;
257 } else {
258 return -1;
259 }
260}
261
262int post_run (char *name, int flags)
263{
264 unsigned int i;
265 int test_flags[POST_MAX_NUMBER];
266
267 post_get_flags (test_flags);
268
269 if (name == NULL) {
270 unsigned int last;
271
272 if (post_bootmode_get (&last) & POST_POWERTEST) {
273 if (last < post_list_size &&
274 (flags & test_flags[last] & POST_ALWAYS) &&
275 (flags & test_flags[last] & POST_MEM)) {
276
wdenkea909b72002-11-21 23:11:29 +0000277 post_run_single (post_list + last,
278 test_flags[last],
279 flags | POST_REBOOT, last);
wdenka042ac82002-09-12 22:36:57 +0000280
281 for (i = last + 1; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000282 post_run_single (post_list + i,
283 test_flags[i],
284 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000285 }
286 }
287 } else {
288 for (i = 0; i < post_list_size; i++) {
wdenkea909b72002-11-21 23:11:29 +0000289 post_run_single (post_list + i,
290 test_flags[i],
291 flags, i);
wdenka042ac82002-09-12 22:36:57 +0000292 }
293 }
294
295 return 0;
296 } else {
297 for (i = 0; i < post_list_size; i++) {
298 if (strcmp (post_list[i].cmd, name) == 0)
299 break;
300 }
301
302 if (i < post_list_size) {
303 return post_run_single (post_list + i,
304 test_flags[i],
305 flags, i);
306 } else {
307 return -1;
308 }
309 }
310}
311
312static int post_info_single (struct post_test *test, int full)
313{
314 if (test->flags & POST_MANUAL) {
315 if (full)
316 printf ("%s - %s\n"
317 " %s\n", test->cmd, test->name, test->desc);
318 else
319 printf (" %-15s - %s\n", test->cmd, test->name);
320
321 return 0;
322 } else {
323 return -1;
324 }
325}
326
327int post_info (char *name)
328{
329 unsigned int i;
330
331 if (name == NULL) {
332 for (i = 0; i < post_list_size; i++) {
333 post_info_single (post_list + i, 0);
334 }
335
336 return 0;
337 } else {
338 for (i = 0; i < post_list_size; i++) {
339 if (strcmp (post_list[i].cmd, name) == 0)
340 break;
341 }
342
343 if (i < post_list_size) {
344 return post_info_single (post_list + i, 1);
345 } else {
346 return -1;
347 }
348 }
349}
350
351int post_log (char *format, ...)
352{
353 va_list args;
354 uint i;
355 char printbuffer[CFG_PBSIZE];
356
357 va_start (args, format);
358
359 /* For this to work, printbuffer must be larger than
360 * anything we ever want to print.
361 */
362 i = vsprintf (printbuffer, format, args);
363 va_end (args);
364
wdenk56f94be2002-11-05 16:35:14 +0000365#ifdef CONFIG_LOGBUFFER
wdenk228f29a2002-12-08 09:53:23 +0000366 /* Send to the logbuffer */
wdenk56f94be2002-11-05 16:35:14 +0000367 logbuff_log (printbuffer);
368#else
wdenka042ac82002-09-12 22:36:57 +0000369 /* Send to the stdout file */
370 puts (printbuffer);
wdenk56f94be2002-11-05 16:35:14 +0000371#endif
wdenka042ac82002-09-12 22:36:57 +0000372
373 return 0;
374}
375
376void post_reloc (void)
377{
wdenka042ac82002-09-12 22:36:57 +0000378 unsigned int i;
379
380 /*
381 * We have to relocate the test table manually
382 */
383 for (i = 0; i < post_list_size; i++) {
384 ulong addr;
385 struct post_test *test = post_list + i;
386
387 if (test->name) {
388 addr = (ulong) (test->name) + gd->reloc_off;
389 test->name = (char *) addr;
390 }
391
392 if (test->cmd) {
393 addr = (ulong) (test->cmd) + gd->reloc_off;
394 test->cmd = (char *) addr;
395 }
396
397 if (test->desc) {
398 addr = (ulong) (test->desc) + gd->reloc_off;
399 test->desc = (char *) addr;
400 }
401
402 if (test->test) {
403 addr = (ulong) (test->test) + gd->reloc_off;
404 test->test = (int (*)(int flags)) addr;
405 }
wdenk4532cb62003-04-27 22:52:51 +0000406
407 if (test->init_f) {
408 addr = (ulong) (test->init_f) + gd->reloc_off;
409 test->init_f = (int (*)(void)) addr;
410 }
411
412 if (test->reloc) {
413 addr = (ulong) (test->reloc) + gd->reloc_off;
414 test->reloc = (void (*)(void)) addr;
wdenk8bde7f72003-06-27 21:31:46 +0000415
wdenk4532cb62003-04-27 22:52:51 +0000416 test->reloc();
417 }
wdenka042ac82002-09-12 22:36:57 +0000418 }
419}
420
wdenk4532cb62003-04-27 22:52:51 +0000421
422/*
423 * Some tests (e.g. SYSMON) need the time when post_init_f started,
424 * but we cannot use get_timer() at this point.
425 *
426 * On PowerPC we implement it using the timebase register.
427 */
428unsigned long post_time_ms (unsigned long base)
429{
430#ifdef CONFIG_PPC
431 return (unsigned long)get_ticks () / (get_tbclk () / CFG_HZ) - base;
432#else
Wolfgang Denkad5bb452007-03-06 18:08:43 +0100433#warning "Not implemented yet"
wdenk4532cb62003-04-27 22:52:51 +0000434 return 0; /* Not implemented yet */
435#endif
436}
437
wdenka042ac82002-09-12 22:36:57 +0000438#endif /* CONFIG_POST */