xref: /openbmc/u-boot/post/post.c (revision 445a886d7ac7bdd5746a8bb18ba033242c3e0fed)
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 	gd->post_log_res = 0;
125 }
126 
127 int post_bootmode_get (unsigned int *last_test)
128 {
129 	unsigned long word = post_word_load ();
130 	int bootmode;
131 
132 	if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
133 		return 0;
134 	}
135 
136 	bootmode = word & 0x7F;
137 
138 	if (last_test && (bootmode & POST_POWERTEST)) {
139 		*last_test = (word >> 8) & 0xFF;
140 	}
141 
142 	return bootmode;
143 }
144 
145 /* POST tests run before relocation only mark status bits .... */
146 static void post_log_mark_start ( unsigned long testid )
147 {
148 	gd->post_log_word |= testid;
149 }
150 
151 static void post_log_mark_succ ( unsigned long testid )
152 {
153 	gd->post_log_res |= testid;
154 }
155 
156 /* ... and the messages are output once we are relocated */
157 void post_output_backlog ( void )
158 {
159 	int j;
160 
161 	for (j = 0; j < post_list_size; j++) {
162 		if (gd->post_log_word & (post_list[j].testid)) {
163 			post_log ("POST %s ", post_list[j].cmd);
164 			if (gd->post_log_res & post_list[j].testid)
165 				post_log ("PASSED\n");
166 			else {
167 				post_log ("FAILED\n");
168 				show_boot_progress (-31);
169 			}
170 		}
171 	}
172 }
173 
174 static void post_bootmode_test_on (unsigned int last_test)
175 {
176 	unsigned long word = post_word_load ();
177 
178 	word |= POST_POWERTEST;
179 
180 	word |= (last_test & 0xFF) << 8;
181 
182 	post_word_store (word);
183 }
184 
185 static void post_bootmode_test_off (void)
186 {
187 	unsigned long word = post_word_load ();
188 
189 	word &= ~POST_POWERTEST;
190 
191 	post_word_store (word);
192 }
193 
194 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
195 static void post_get_env_flags(int *test_flags)
196 {
197 	int  flag[] = {  POST_POWERON,   POST_NORMAL,   POST_SLOWTEST,
198 			 POST_CRITICAL };
199 	char *var[] = { "post_poweron", "post_normal", "post_slowtest",
200 			"post_critical" };
201 	int varnum = ARRAY_SIZE(var);
202 	char list[128];			/* long enough for POST list */
203 	char *name;
204 	char *s;
205 	int last;
206 	int i, j;
207 
208 	for (i = 0; i < varnum; i++) {
209 		if (getenv_f(var[i], list, sizeof (list)) <= 0)
210 			continue;
211 
212 		for (j = 0; j < post_list_size; j++) {
213 			test_flags[j] &= ~flag[i];
214 		}
215 
216 		last = 0;
217 		name = list;
218 		while (!last) {
219 			while (*name && *name == ' ')
220 				name++;
221 			if (*name == 0)
222 				break;
223 			s = name + 1;
224 			while (*s && *s != ' ')
225 				s++;
226 			if (*s == 0)
227 				last = 1;
228 			else
229 				*s = 0;
230 
231 			for (j = 0; j < post_list_size; j++) {
232 				if (strcmp (post_list[j].cmd, name) == 0) {
233 					test_flags[j] |= flag[i];
234 					break;
235 				}
236 			}
237 
238 			if (j == post_list_size) {
239 				printf ("No such test: %s\n", name);
240 			}
241 
242 			name = s + 1;
243 		}
244 	}
245 }
246 #endif
247 
248 static void post_get_flags(int *test_flags)
249 {
250 	int j;
251 
252 	for (j = 0; j < post_list_size; j++)
253 		test_flags[j] = post_list[j].flags;
254 
255 #ifndef CONFIG_POST_SKIP_ENV_FLAGS
256 	post_get_env_flags(test_flags);
257 #endif
258 
259 	for (j = 0; j < post_list_size; j++) {
260 		if (test_flags[j] & POST_POWERON) {
261 			test_flags[j] |= POST_SLOWTEST;
262 		}
263 	}
264 }
265 
266 void __show_post_progress (unsigned int test_num, int before, int result)
267 {
268 }
269 void show_post_progress (unsigned int, int, int)
270 			__attribute__((weak, alias("__show_post_progress")));
271 
272 static int post_run_single (struct post_test *test,
273 				int test_flags, int flags, unsigned int i)
274 {
275 	if ((flags & test_flags & POST_ALWAYS) &&
276 		(flags & test_flags & POST_MEM)) {
277 		WATCHDOG_RESET ();
278 
279 		if (!(flags & POST_REBOOT)) {
280 			if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
281 				post_bootmode_test_on (
282 					(gd->flags & GD_FLG_POSTFAIL) ?
283 						POST_FAIL_SAVE | i : i);
284 			}
285 
286 			if (test_flags & POST_PREREL)
287 				post_log_mark_start ( test->testid );
288 			else
289 				post_log ("POST %s ", test->cmd);
290 		}
291 
292 		show_post_progress(i, POST_BEFORE, POST_FAILED);
293 
294 		if (test_flags & POST_PREREL) {
295 			if ((*test->test) (flags) == 0) {
296 				post_log_mark_succ ( test->testid );
297 				show_post_progress(i, POST_AFTER, POST_PASSED);
298 			}
299 			else {
300 				show_post_progress(i, POST_AFTER, POST_FAILED);
301 				if (test_flags & POST_CRITICAL)
302 					gd->flags |= GD_FLG_POSTFAIL;
303 				if (test_flags & POST_STOP)
304 					gd->flags |= GD_FLG_POSTSTOP;
305 			}
306 		} else {
307 			if ((*test->test)(flags) != 0) {
308 				post_log("FAILED\n");
309 				show_boot_progress(-32);
310 				show_post_progress(i, POST_AFTER, POST_FAILED);
311 				if (test_flags & POST_CRITICAL)
312 					gd->flags |= GD_FLG_POSTFAIL;
313 				if (test_flags & POST_STOP)
314 					gd->flags |= GD_FLG_POSTSTOP;
315 			} else {
316 				post_log("PASSED\n");
317 				show_post_progress(i, POST_AFTER, POST_PASSED);
318 			}
319 		}
320 
321 		if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
322 			post_bootmode_test_off ();
323 		}
324 
325 		return 0;
326 	} else {
327 		return -1;
328 	}
329 }
330 
331 int post_run (char *name, int flags)
332 {
333 	unsigned int i;
334 	int test_flags[POST_MAX_NUMBER];
335 
336 	post_get_flags (test_flags);
337 
338 	if (name == NULL) {
339 		unsigned int last;
340 
341 		if (gd->flags & GD_FLG_POSTSTOP)
342 			return 0;
343 
344 		if (post_bootmode_get (&last) & POST_POWERTEST) {
345 			if (last & POST_FAIL_SAVE) {
346 				last &= ~POST_FAIL_SAVE;
347 				gd->flags |= GD_FLG_POSTFAIL;
348 			}
349 			if (last < post_list_size &&
350 				(flags & test_flags[last] & POST_ALWAYS) &&
351 				(flags & test_flags[last] & POST_MEM)) {
352 
353 				post_run_single (post_list + last,
354 						 test_flags[last],
355 						 flags | POST_REBOOT, last);
356 
357 				for (i = last + 1; i < post_list_size; i++) {
358 					if (gd->flags & GD_FLG_POSTSTOP)
359 						break;
360 					post_run_single (post_list + i,
361 							 test_flags[i],
362 							 flags, i);
363 				}
364 			}
365 		} else {
366 			for (i = 0; i < post_list_size; i++) {
367 				if (gd->flags & GD_FLG_POSTSTOP)
368 					break;
369 				post_run_single (post_list + i,
370 						 test_flags[i],
371 						 flags, i);
372 			}
373 		}
374 
375 		return 0;
376 	} else {
377 		for (i = 0; i < post_list_size; i++) {
378 			if (strcmp (post_list[i].cmd, name) == 0)
379 				break;
380 		}
381 
382 		if (i < post_list_size) {
383 			WATCHDOG_RESET();
384 			return post_run_single (post_list + i,
385 						test_flags[i],
386 						flags, i);
387 		} else {
388 			return -1;
389 		}
390 	}
391 }
392 
393 static int post_info_single (struct post_test *test, int full)
394 {
395 	if (test->flags & POST_MANUAL) {
396 		if (full)
397 			printf ("%s - %s\n"
398 				"  %s\n", test->cmd, test->name, test->desc);
399 		else
400 			printf ("  %-15s - %s\n", test->cmd, test->name);
401 
402 		return 0;
403 	} else {
404 		return -1;
405 	}
406 }
407 
408 int post_info (char *name)
409 {
410 	unsigned int i;
411 
412 	if (name == NULL) {
413 		for (i = 0; i < post_list_size; i++) {
414 			post_info_single (post_list + i, 0);
415 		}
416 
417 		return 0;
418 	} else {
419 		for (i = 0; i < post_list_size; i++) {
420 			if (strcmp (post_list[i].cmd, name) == 0)
421 				break;
422 		}
423 
424 		if (i < post_list_size) {
425 			return post_info_single (post_list + i, 1);
426 		} else {
427 			return -1;
428 		}
429 	}
430 }
431 
432 int post_log (char *format, ...)
433 {
434 	va_list args;
435 	uint i;
436 	char printbuffer[CONFIG_SYS_PBSIZE];
437 
438 	va_start (args, format);
439 
440 	/* For this to work, printbuffer must be larger than
441 	 * anything we ever want to print.
442 	 */
443 	i = vsprintf (printbuffer, format, args);
444 	va_end (args);
445 
446 #ifdef CONFIG_LOGBUFFER
447 	/* Send to the logbuffer */
448 	logbuff_log (printbuffer);
449 #else
450 	/* Send to the stdout file */
451 	puts (printbuffer);
452 #endif
453 
454 	return 0;
455 }
456 
457 #ifdef CONFIG_NEEDS_MANUAL_RELOC
458 void post_reloc (void)
459 {
460 	unsigned int i;
461 
462 	/*
463 	 * We have to relocate the test table manually
464 	 */
465 	for (i = 0; i < post_list_size; i++) {
466 		ulong addr;
467 		struct post_test *test = post_list + i;
468 
469 		if (test->name) {
470 			addr = (ulong) (test->name) + gd->reloc_off;
471 			test->name = (char *) addr;
472 		}
473 
474 		if (test->cmd) {
475 			addr = (ulong) (test->cmd) + gd->reloc_off;
476 			test->cmd = (char *) addr;
477 		}
478 
479 		if (test->desc) {
480 			addr = (ulong) (test->desc) + gd->reloc_off;
481 			test->desc = (char *) addr;
482 		}
483 
484 		if (test->test) {
485 			addr = (ulong) (test->test) + gd->reloc_off;
486 			test->test = (int (*)(int flags)) addr;
487 		}
488 
489 		if (test->init_f) {
490 			addr = (ulong) (test->init_f) + gd->reloc_off;
491 			test->init_f = (int (*)(void)) addr;
492 		}
493 
494 		if (test->reloc) {
495 			addr = (ulong) (test->reloc) + gd->reloc_off;
496 			test->reloc = (void (*)(void)) addr;
497 
498 			test->reloc();
499 		}
500 	}
501 }
502 #endif
503 
504 
505 /*
506  * Some tests (e.g. SYSMON) need the time when post_init_f started,
507  * but we cannot use get_timer() at this point.
508  *
509  * On PowerPC we implement it using the timebase register.
510  */
511 unsigned long post_time_ms (unsigned long base)
512 {
513 #ifdef CONFIG_PPC
514 	return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
515 #else
516 #warning "Not implemented yet"
517 	return 0; /* Not implemented yet */
518 #endif
519 }
520