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