xref: /openbmc/u-boot/post/post.c (revision a562e1bd)
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 <console.h>
26 #include <watchdog.h>
27 #include <post.h>
28 
29 #ifdef CONFIG_LOGBUFFER
30 #include <logbuff.h>
31 #endif
32 
33 #ifdef CONFIG_POST
34 
35 #define POST_MAX_NUMBER		32
36 
37 #define BOOTMODE_MAGIC	0xDEAD0000
38 
39 int 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 	}
53 
54 	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 
63 void post_bootmode_init (void)
64 {
65 	DECLARE_GLOBAL_DATA_PTR;
66 	int bootmode = post_bootmode_get (0);
67 	int newword;
68 
69 	if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
70 		newword = BOOTMODE_MAGIC | POST_SLOWTEST;
71 	} else if (bootmode == 0) {
72 		newword = BOOTMODE_MAGIC | POST_POWERON;
73 	} else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
74 		newword = BOOTMODE_MAGIC | POST_NORMAL;
75 	} else {
76 		/* Use old value */
77 		newword = post_word_load () & ~POST_COLDBOOT;
78 	}
79 
80 	if (bootmode == 0)
81 	{
82 		/* We are booting after power-on */
83 		newword |= POST_COLDBOOT;
84 	}
85 
86 	post_word_store (newword);
87 
88 	/* Reset activity record */
89 	gd->post_log_word = 0;
90 }
91 
92 int 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 
101 	bootmode = word & 0x7F;
102 
103 	if (last_test && (bootmode & POST_POWERTEST)) {
104 		*last_test = (word >> 8) & 0xFF;
105 	}
106 
107 	return bootmode;
108 }
109 
110 /* POST tests run before relocation only mark status bits .... */
111 static void post_log_mark_start ( unsigned long testid )
112 {
113 	DECLARE_GLOBAL_DATA_PTR;
114 	gd->post_log_word |= (testid)<<16;
115 }
116 
117 static 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 */
124 void 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 #ifdef CONFIG_SHOW_BOOT_PROGRESS
137 				show_boot_progress(-31);
138 #endif
139 			}
140 		}
141 	}
142 }
143 
144 static 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 
155 static 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 
164 static void post_get_flags (int *test_flags)
165 {
166 	int  flag[] = {  POST_POWERON,   POST_NORMAL,   POST_SLOWTEST };
167 	char *var[] = { "post_poweron", "post_normal", "post_slowtest" };
168 	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 	}
216 
217 	for (j = 0; j < post_list_size; j++) {
218 		if (test_flags[j] & POST_POWERON) {
219 			test_flags[j] |= POST_SLOWTEST;
220 		}
221 	}
222 }
223 
224 static 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 
236 			if (test_flags & POST_PREREL)
237 				post_log_mark_start ( test->testid );
238 			else
239 			post_log ("POST %s ", test->cmd);
240 		}
241 
242 		if (test_flags & POST_PREREL) {
243 			if ((*test->test) (flags) == 0)
244 				post_log_mark_succ ( test->testid );
245 		} else {
246 		if ((*test->test) (flags) != 0) {
247 			post_log ("FAILED\n");
248 #ifdef CONFIG_SHOW_BOOT_PROGRESS
249 			show_boot_progress(-32);
250 #endif
251 		}
252 		else
253 			post_log ("PASSED\n");
254 		}
255 
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 
266 int 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 
281 				post_run_single (post_list + last,
282 						 test_flags[last],
283 						 flags | POST_REBOOT, last);
284 
285 				for (i = last + 1; i < post_list_size; i++) {
286 					post_run_single (post_list + i,
287 							 test_flags[i],
288 							 flags, i);
289 				}
290 			}
291 		} else {
292 			for (i = 0; i < post_list_size; i++) {
293 				post_run_single (post_list + i,
294 						 test_flags[i],
295 						 flags, i);
296 			}
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 
316 static 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 
331 int 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 
355 int 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 
369 #ifdef CONFIG_LOGBUFFER
370 	/* Send to the logbuffer */
371 	logbuff_log (printbuffer);
372 #else
373 	/* Send to the stdout file */
374 	puts (printbuffer);
375 #endif
376 
377 	return 0;
378 }
379 
380 void 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 		}
412 
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;
421 
422 			test->reloc();
423 		}
424 	}
425 }
426 
427 
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  */
434 unsigned 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 
443 #endif /* CONFIG_POST */
444