xref: /openbmc/u-boot/post/post.c (revision 6c0bf27d)
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 <stdio_dev.h>
26 #include <watchdog.h>
27 #include <post.h>
28 
29 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
30 #include <asm/gpio.h>
31 #endif
32 
33 #ifdef CONFIG_LOGBUFFER
34 #include <logbuff.h>
35 #endif
36 
37 DECLARE_GLOBAL_DATA_PTR;
38 
39 #define POST_MAX_NUMBER		32
40 
41 #define BOOTMODE_MAGIC	0xDEAD0000
42 
43 int post_init_f (void)
44 {
45 	int res = 0;
46 	unsigned int i;
47 
48 	for (i = 0; i < post_list_size; i++) {
49 		struct post_test *test = post_list + i;
50 
51 		if (test->init_f && test->init_f()) {
52 			res = -1;
53 		}
54 	}
55 
56 	gd->post_init_f_time = post_time_ms(0);
57 	if (!gd->post_init_f_time)
58 	{
59 		printf("post/post.c: post_time_ms seems not to be implemented\n");
60 	}
61 
62 	return res;
63 }
64 
65 /*
66  * Supply a default implementation for post_hotkeys_pressed() for boards
67  * without hotkey support. We always return 0 here, so that the
68  * long-running tests won't be started.
69  *
70  * Boards with hotkey support can override this weak default function
71  * by defining one in their board specific code.
72  */
73 int __post_hotkeys_pressed(void)
74 {
75 #ifdef CONFIG_SYS_POST_HOTKEYS_GPIO
76 	int ret;
77 	unsigned gpio = CONFIG_SYS_POST_HOTKEYS_GPIO;
78 
79 	ret = gpio_request(gpio, "hotkeys");
80 	if (ret) {
81 		printf("POST: gpio hotkey request failed\n");
82 		return 0;
83 	}
84 
85 	gpio_direction_input(gpio);
86 	ret = gpio_get_value(gpio);
87 	gpio_free(gpio);
88 
89 	return ret;
90 #endif
91 
92 	return 0;	/* No hotkeys supported */
93 }
94 int post_hotkeys_pressed(void)
95 	__attribute__((weak, alias("__post_hotkeys_pressed")));
96 
97 
98 void post_bootmode_init (void)
99 {
100 	int bootmode = post_bootmode_get (0);
101 	int newword;
102 
103 	if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
104 		newword = BOOTMODE_MAGIC | POST_SLOWTEST;
105 	} else if (bootmode == 0) {
106 		newword = BOOTMODE_MAGIC | POST_POWERON;
107 	} else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
108 		newword = BOOTMODE_MAGIC | POST_NORMAL;
109 	} else {
110 		/* Use old value */
111 		newword = post_word_load () & ~POST_COLDBOOT;
112 	}
113 
114 	if (bootmode == 0)
115 	{
116 		/* We are booting after power-on */
117 		newword |= POST_COLDBOOT;
118 	}
119 
120 	post_word_store (newword);
121 
122 	/* Reset activity record */
123 	gd->post_log_word = 0;
124 }
125 
126 int post_bootmode_get (unsigned int *last_test)
127 {
128 	unsigned long word = post_word_load ();
129 	int bootmode;
130 
131 	if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
132 		return 0;
133 	}
134 
135 	bootmode = word & 0x7F;
136 
137 	if (last_test && (bootmode & POST_POWERTEST)) {
138 		*last_test = (word >> 8) & 0xFF;
139 	}
140 
141 	return bootmode;
142 }
143 
144 /* POST tests run before relocation only mark status bits .... */
145 static void post_log_mark_start ( unsigned long testid )
146 {
147 	gd->post_log_word |= (testid)<<16;
148 }
149 
150 static void post_log_mark_succ ( unsigned long testid )
151 {
152 	gd->post_log_word |= testid;
153 }
154 
155 /* ... and the messages are output once we are relocated */
156 void post_output_backlog ( void )
157 {
158 	int j;
159 
160 	for (j = 0; j < post_list_size; j++) {
161 		if (gd->post_log_word & (post_list[j].testid<<16)) {
162 			post_log ("POST %s ", post_list[j].cmd);
163 			if (gd->post_log_word & post_list[j].testid)
164 				post_log ("PASSED\n");
165 			else {
166 				post_log ("FAILED\n");
167 				show_boot_progress (-31);
168 			}
169 		}
170 	}
171 }
172 
173 static void post_bootmode_test_on (unsigned int last_test)
174 {
175 	unsigned long word = post_word_load ();
176 
177 	word |= POST_POWERTEST;
178 
179 	word |= (last_test & 0xFF) << 8;
180 
181 	post_word_store (word);
182 }
183 
184 static void post_bootmode_test_off (void)
185 {
186 	unsigned long word = post_word_load ();
187 
188 	word &= ~POST_POWERTEST;
189 
190 	post_word_store (word);
191 }
192 
193 static void post_get_flags (int *test_flags)
194 {
195 	int  flag[] = {  POST_POWERON,   POST_NORMAL,   POST_SLOWTEST,
196 			 POST_CRITICAL };
197 	char *var[] = { "post_poweron", "post_normal", "post_slowtest",
198 			"post_critical" };
199 	int varnum = ARRAY_SIZE(var);
200 	char list[128];			/* long enough for POST list */
201 	char *name;
202 	char *s;
203 	int last;
204 	int i, j;
205 
206 	for (j = 0; j < post_list_size; j++) {
207 		test_flags[j] = post_list[j].flags;
208 	}
209 
210 	for (i = 0; i < varnum; i++) {
211 		if (getenv_f(var[i], list, sizeof (list)) <= 0)
212 			continue;
213 
214 		for (j = 0; j < post_list_size; j++) {
215 			test_flags[j] &= ~flag[i];
216 		}
217 
218 		last = 0;
219 		name = list;
220 		while (!last) {
221 			while (*name && *name == ' ')
222 				name++;
223 			if (*name == 0)
224 				break;
225 			s = name + 1;
226 			while (*s && *s != ' ')
227 				s++;
228 			if (*s == 0)
229 				last = 1;
230 			else
231 				*s = 0;
232 
233 			for (j = 0; j < post_list_size; j++) {
234 				if (strcmp (post_list[j].cmd, name) == 0) {
235 					test_flags[j] |= flag[i];
236 					break;
237 				}
238 			}
239 
240 			if (j == post_list_size) {
241 				printf ("No such test: %s\n", name);
242 			}
243 
244 			name = s + 1;
245 		}
246 	}
247 
248 	for (j = 0; j < post_list_size; j++) {
249 		if (test_flags[j] & POST_POWERON) {
250 			test_flags[j] |= POST_SLOWTEST;
251 		}
252 	}
253 }
254 
255 void __show_post_progress (unsigned int test_num, int before, int result)
256 {
257 }
258 void show_post_progress (unsigned int, int, int)
259 			__attribute__((weak, alias("__show_post_progress")));
260 
261 static int post_run_single (struct post_test *test,
262 				int test_flags, int flags, unsigned int i)
263 {
264 	if ((flags & test_flags & POST_ALWAYS) &&
265 		(flags & test_flags & POST_MEM)) {
266 		WATCHDOG_RESET ();
267 
268 		if (!(flags & POST_REBOOT)) {
269 			if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
270 				post_bootmode_test_on (
271 					(gd->flags & GD_FLG_POSTFAIL) ?
272 						POST_FAIL_SAVE | i : i);
273 			}
274 
275 			if (test_flags & POST_PREREL)
276 				post_log_mark_start ( test->testid );
277 			else
278 				post_log ("POST %s ", test->cmd);
279 		}
280 
281 		show_post_progress(i, POST_BEFORE, POST_FAILED);
282 
283 		if (test_flags & POST_PREREL) {
284 			if ((*test->test) (flags) == 0) {
285 				post_log_mark_succ ( test->testid );
286 				show_post_progress(i, POST_AFTER, POST_PASSED);
287 			}
288 			else {
289 				show_post_progress(i, POST_AFTER, POST_FAILED);
290 				if (test_flags & POST_CRITICAL)
291 					gd->flags |= GD_FLG_POSTFAIL;
292 				if (test_flags & POST_STOP)
293 					gd->flags |= GD_FLG_POSTSTOP;
294 			}
295 		} else {
296 			if ((*test->test)(flags) != 0) {
297 				post_log("FAILED\n");
298 				show_boot_progress(-32);
299 				show_post_progress(i, POST_AFTER, POST_FAILED);
300 				if (test_flags & POST_CRITICAL)
301 					gd->flags |= GD_FLG_POSTFAIL;
302 				if (test_flags & POST_STOP)
303 					gd->flags |= GD_FLG_POSTSTOP;
304 			} else {
305 				post_log("PASSED\n");
306 				show_post_progress(i, POST_AFTER, POST_PASSED);
307 			}
308 		}
309 
310 		if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
311 			post_bootmode_test_off ();
312 		}
313 
314 		return 0;
315 	} else {
316 		return -1;
317 	}
318 }
319 
320 int post_run (char *name, int flags)
321 {
322 	unsigned int i;
323 	int test_flags[POST_MAX_NUMBER];
324 
325 	post_get_flags (test_flags);
326 
327 	if (name == NULL) {
328 		unsigned int last;
329 
330 		if (gd->flags & GD_FLG_POSTSTOP)
331 			return 0;
332 
333 		if (post_bootmode_get (&last) & POST_POWERTEST) {
334 			if (last & POST_FAIL_SAVE) {
335 				last &= ~POST_FAIL_SAVE;
336 				gd->flags |= GD_FLG_POSTFAIL;
337 			}
338 			if (last < post_list_size &&
339 				(flags & test_flags[last] & POST_ALWAYS) &&
340 				(flags & test_flags[last] & POST_MEM)) {
341 
342 				post_run_single (post_list + last,
343 						 test_flags[last],
344 						 flags | POST_REBOOT, last);
345 
346 				for (i = last + 1; i < post_list_size; i++) {
347 					if (gd->flags & GD_FLG_POSTSTOP)
348 						break;
349 					post_run_single (post_list + i,
350 							 test_flags[i],
351 							 flags, i);
352 				}
353 			}
354 		} else {
355 			for (i = 0; i < post_list_size; i++) {
356 				if (gd->flags & GD_FLG_POSTSTOP)
357 					break;
358 				post_run_single (post_list + i,
359 						 test_flags[i],
360 						 flags, i);
361 			}
362 		}
363 
364 		return 0;
365 	} else {
366 		for (i = 0; i < post_list_size; i++) {
367 			if (strcmp (post_list[i].cmd, name) == 0)
368 				break;
369 		}
370 
371 		if (i < post_list_size) {
372 			WATCHDOG_RESET();
373 			return post_run_single (post_list + i,
374 						test_flags[i],
375 						flags, i);
376 		} else {
377 			return -1;
378 		}
379 	}
380 }
381 
382 static int post_info_single (struct post_test *test, int full)
383 {
384 	if (test->flags & POST_MANUAL) {
385 		if (full)
386 			printf ("%s - %s\n"
387 				"  %s\n", test->cmd, test->name, test->desc);
388 		else
389 			printf ("  %-15s - %s\n", test->cmd, test->name);
390 
391 		return 0;
392 	} else {
393 		return -1;
394 	}
395 }
396 
397 int post_info (char *name)
398 {
399 	unsigned int i;
400 
401 	if (name == NULL) {
402 		for (i = 0; i < post_list_size; i++) {
403 			post_info_single (post_list + i, 0);
404 		}
405 
406 		return 0;
407 	} else {
408 		for (i = 0; i < post_list_size; i++) {
409 			if (strcmp (post_list[i].cmd, name) == 0)
410 				break;
411 		}
412 
413 		if (i < post_list_size) {
414 			return post_info_single (post_list + i, 1);
415 		} else {
416 			return -1;
417 		}
418 	}
419 }
420 
421 int post_log (char *format, ...)
422 {
423 	va_list args;
424 	uint i;
425 	char printbuffer[CONFIG_SYS_PBSIZE];
426 
427 	va_start (args, format);
428 
429 	/* For this to work, printbuffer must be larger than
430 	 * anything we ever want to print.
431 	 */
432 	i = vsprintf (printbuffer, format, args);
433 	va_end (args);
434 
435 #ifdef CONFIG_LOGBUFFER
436 	/* Send to the logbuffer */
437 	logbuff_log (printbuffer);
438 #else
439 	/* Send to the stdout file */
440 	puts (printbuffer);
441 #endif
442 
443 	return 0;
444 }
445 
446 #ifdef CONFIG_NEEDS_MANUAL_RELOC
447 void post_reloc (void)
448 {
449 	unsigned int i;
450 
451 	/*
452 	 * We have to relocate the test table manually
453 	 */
454 	for (i = 0; i < post_list_size; i++) {
455 		ulong addr;
456 		struct post_test *test = post_list + i;
457 
458 		if (test->name) {
459 			addr = (ulong) (test->name) + gd->reloc_off;
460 			test->name = (char *) addr;
461 		}
462 
463 		if (test->cmd) {
464 			addr = (ulong) (test->cmd) + gd->reloc_off;
465 			test->cmd = (char *) addr;
466 		}
467 
468 		if (test->desc) {
469 			addr = (ulong) (test->desc) + gd->reloc_off;
470 			test->desc = (char *) addr;
471 		}
472 
473 		if (test->test) {
474 			addr = (ulong) (test->test) + gd->reloc_off;
475 			test->test = (int (*)(int flags)) addr;
476 		}
477 
478 		if (test->init_f) {
479 			addr = (ulong) (test->init_f) + gd->reloc_off;
480 			test->init_f = (int (*)(void)) addr;
481 		}
482 
483 		if (test->reloc) {
484 			addr = (ulong) (test->reloc) + gd->reloc_off;
485 			test->reloc = (void (*)(void)) addr;
486 
487 			test->reloc();
488 		}
489 	}
490 }
491 #endif
492 
493 
494 /*
495  * Some tests (e.g. SYSMON) need the time when post_init_f started,
496  * but we cannot use get_timer() at this point.
497  *
498  * On PowerPC we implement it using the timebase register.
499  */
500 unsigned long post_time_ms (unsigned long base)
501 {
502 #ifdef CONFIG_PPC
503 	return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
504 #else
505 #warning "Not implemented yet"
506 	return 0; /* Not implemented yet */
507 #endif
508 }
509