post.c 9.3 KB
Newer Older
W
wdenk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * (C) Copyright 2002
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
25
#include <stdio_dev.h>
W
wdenk 已提交
26 27 28
#include <watchdog.h>
#include <post.h>

29 30 31 32
#ifdef CONFIG_LOGBUFFER
#include <logbuff.h>
#endif

33 34
DECLARE_GLOBAL_DATA_PTR;

W
wdenk 已提交
35 36 37 38
#define POST_MAX_NUMBER		32

#define BOOTMODE_MAGIC	0xDEAD0000

W
wdenk 已提交
39 40 41 42 43 44 45 46 47 48 49 50
int post_init_f (void)
{
	int res = 0;
	unsigned int i;

	for (i = 0; i < post_list_size; i++) {
		struct post_test *test = post_list + i;

		if (test->init_f && test->init_f()) {
			res = -1;
		}
	}
W
wdenk 已提交
51

W
wdenk 已提交
52 53 54 55 56 57 58 59 60
	gd->post_init_f_time = post_time_ms(0);
	if (!gd->post_init_f_time)
	{
		printf("post/post.c: post_time_ms seems not to be implemented\n");
	}

	return res;
}

W
wdenk 已提交
61 62 63
void post_bootmode_init (void)
{
	int bootmode = post_bootmode_get (0);
64
	int newword;
W
wdenk 已提交
65

66 67
	if (post_hotkeys_pressed() && !(bootmode & POST_POWERTEST)) {
		newword = BOOTMODE_MAGIC | POST_SLOWTEST;
W
wdenk 已提交
68
	} else if (bootmode == 0) {
69
		newword = BOOTMODE_MAGIC | POST_POWERON;
W
wdenk 已提交
70
	} else if (bootmode == POST_POWERON || bootmode == POST_SLOWTEST) {
71
		newword = BOOTMODE_MAGIC | POST_NORMAL;
W
wdenk 已提交
72
	} else {
73 74
		/* Use old value */
		newword = post_word_load () & ~POST_COLDBOOT;
W
wdenk 已提交
75 76
	}

77 78 79 80 81 82 83 84
	if (bootmode == 0)
	{
		/* We are booting after power-on */
		newword |= POST_COLDBOOT;
	}

	post_word_store (newword);

85 86
	/* Reset activity record */
	gd->post_log_word = 0;
W
wdenk 已提交
87 88 89 90 91 92 93 94 95 96 97
}

int post_bootmode_get (unsigned int *last_test)
{
	unsigned long word = post_word_load ();
	int bootmode;

	if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
		return 0;
	}

98
	bootmode = word & 0x7F;
W
wdenk 已提交
99 100 101 102 103 104 105 106

	if (last_test && (bootmode & POST_POWERTEST)) {
		*last_test = (word >> 8) & 0xFF;
	}

	return bootmode;
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/* POST tests run before relocation only mark status bits .... */
static void post_log_mark_start ( unsigned long testid )
{
	gd->post_log_word |= (testid)<<16;
}

static void post_log_mark_succ ( unsigned long testid )
{
	gd->post_log_word |= testid;
}

/* ... and the messages are output once we are relocated */
void post_output_backlog ( void )
{
	int j;

	for (j = 0; j < post_list_size; j++) {
		if (gd->post_log_word & (post_list[j].testid<<16)) {
			post_log ("POST %s ", post_list[j].cmd);
			if (gd->post_log_word & post_list[j].testid)
				post_log ("PASSED\n");
128
			else {
129
				post_log ("FAILED\n");
130
				show_boot_progress (-31);
131
			}
132 133 134 135
		}
	}
}

W
wdenk 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static void post_bootmode_test_on (unsigned int last_test)
{
	unsigned long word = post_word_load ();

	word |= POST_POWERTEST;

	word |= (last_test & 0xFF) << 8;

	post_word_store (word);
}

static void post_bootmode_test_off (void)
{
	unsigned long word = post_word_load ();

	word &= ~POST_POWERTEST;

	post_word_store (word);
}

static void post_get_flags (int *test_flags)
{
158 159 160 161
	int  flag[] = {  POST_POWERON,   POST_NORMAL,   POST_SLOWTEST,
			 POST_CRITICAL };
	char *var[] = { "post_poweron", "post_normal", "post_slowtest",
			"post_critical" };
W
wdenk 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	int varnum = sizeof (var) / sizeof (var[0]);
	char list[128];			/* long enough for POST list */
	char *name;
	char *s;
	int last;
	int i, j;

	for (j = 0; j < post_list_size; j++) {
		test_flags[j] = post_list[j].flags;
	}

	for (i = 0; i < varnum; i++) {
		if (getenv_r (var[i], list, sizeof (list)) <= 0)
			continue;

		for (j = 0; j < post_list_size; j++) {
			test_flags[j] &= ~flag[i];
		}

		last = 0;
		name = list;
		while (!last) {
			while (*name && *name == ' ')
				name++;
			if (*name == 0)
				break;
			s = name + 1;
			while (*s && *s != ' ')
				s++;
			if (*s == 0)
				last = 1;
			else
				*s = 0;

			for (j = 0; j < post_list_size; j++) {
				if (strcmp (post_list[j].cmd, name) == 0) {
					test_flags[j] |= flag[i];
					break;
				}
			}

			if (j == post_list_size) {
				printf ("No such test: %s\n", name);
			}

			name = s + 1;
		}
	}
W
wdenk 已提交
210 211 212 213 214 215

	for (j = 0; j < post_list_size; j++) {
		if (test_flags[j] & POST_POWERON) {
			test_flags[j] |= POST_SLOWTEST;
		}
	}
W
wdenk 已提交
216 217 218 219 220 221 222 223 224 225 226
}

static int post_run_single (struct post_test *test,
				int test_flags, int flags, unsigned int i)
{
	if ((flags & test_flags & POST_ALWAYS) &&
		(flags & test_flags & POST_MEM)) {
		WATCHDOG_RESET ();

		if (!(flags & POST_REBOOT)) {
			if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
227 228 229
				post_bootmode_test_on (
					(gd->flags & GD_FLG_POSTFAIL) ?
						POST_FAIL_SAVE | i : i);
W
wdenk 已提交
230 231
			}

232 233 234
			if (test_flags & POST_PREREL)
				post_log_mark_start ( test->testid );
			else
235
			post_log ("POST %s ", test->cmd);
W
wdenk 已提交
236 237
		}

238 239 240
		if (test_flags & POST_PREREL) {
			if ((*test->test) (flags) == 0)
				post_log_mark_succ ( test->testid );
Y
Yuri Tikhonov 已提交
241 242 243 244 245 246
			else {
				if (test_flags & POST_CRITICAL)
					gd->flags |= GD_FLG_POSTFAIL;
				if (test_flags & POST_STOP)
					gd->flags |= GD_FLG_POSTSTOP;
			}
247
		} else {
248
		if ((*test->test) (flags) != 0) {
W
wdenk 已提交
249
			post_log ("FAILED\n");
250
			show_boot_progress (-32);
251 252
			if (test_flags & POST_CRITICAL)
				gd->flags |= GD_FLG_POSTFAIL;
Y
Yuri Tikhonov 已提交
253 254
			if (test_flags & POST_STOP)
				gd->flags |= GD_FLG_POSTSTOP;
255
		}
W
wdenk 已提交
256 257
		else
			post_log ("PASSED\n");
258
		}
W
wdenk 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

		if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
			post_bootmode_test_off ();
		}

		return 0;
	} else {
		return -1;
	}
}

int post_run (char *name, int flags)
{
	unsigned int i;
	int test_flags[POST_MAX_NUMBER];

	post_get_flags (test_flags);

	if (name == NULL) {
		unsigned int last;

Y
Yuri Tikhonov 已提交
280 281 282
		if (gd->flags & GD_FLG_POSTSTOP)
			return 0;

W
wdenk 已提交
283
		if (post_bootmode_get (&last) & POST_POWERTEST) {
284 285 286 287
			if (last & POST_FAIL_SAVE) {
				last &= ~POST_FAIL_SAVE;
				gd->flags |= GD_FLG_POSTFAIL;
			}
W
wdenk 已提交
288 289 290 291
			if (last < post_list_size &&
				(flags & test_flags[last] & POST_ALWAYS) &&
				(flags & test_flags[last] & POST_MEM)) {

292 293 294
				post_run_single (post_list + last,
						 test_flags[last],
						 flags | POST_REBOOT, last);
W
wdenk 已提交
295 296

				for (i = last + 1; i < post_list_size; i++) {
Y
Yuri Tikhonov 已提交
297 298
					if (gd->flags & GD_FLG_POSTSTOP)
						break;
299 300 301
					post_run_single (post_list + i,
							 test_flags[i],
							 flags, i);
W
wdenk 已提交
302 303 304 305
				}
			}
		} else {
			for (i = 0; i < post_list_size; i++) {
Y
Yuri Tikhonov 已提交
306 307
				if (gd->flags & GD_FLG_POSTSTOP)
					break;
308 309 310
				post_run_single (post_list + i,
						 test_flags[i],
						 flags, i);
W
wdenk 已提交
311 312 313 314 315 316 317 318 319 320 321
			}
		}

		return 0;
	} else {
		for (i = 0; i < post_list_size; i++) {
			if (strcmp (post_list[i].cmd, name) == 0)
				break;
		}

		if (i < post_list_size) {
322
			WATCHDOG_RESET();
W
wdenk 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
			return post_run_single (post_list + i,
						test_flags[i],
						flags, i);
		} else {
			return -1;
		}
	}
}

static int post_info_single (struct post_test *test, int full)
{
	if (test->flags & POST_MANUAL) {
		if (full)
			printf ("%s - %s\n"
				"  %s\n", test->cmd, test->name, test->desc);
		else
			printf ("  %-15s - %s\n", test->cmd, test->name);

		return 0;
	} else {
		return -1;
	}
}

int post_info (char *name)
{
	unsigned int i;

	if (name == NULL) {
		for (i = 0; i < post_list_size; i++) {
			post_info_single (post_list + i, 0);
		}

		return 0;
	} else {
		for (i = 0; i < post_list_size; i++) {
			if (strcmp (post_list[i].cmd, name) == 0)
				break;
		}

		if (i < post_list_size) {
			return post_info_single (post_list + i, 1);
		} else {
			return -1;
		}
	}
}

int post_log (char *format, ...)
{
	va_list args;
	uint i;
375
	char printbuffer[CONFIG_SYS_PBSIZE];
W
wdenk 已提交
376 377 378 379 380 381 382 383 384

	va_start (args, format);

	/* For this to work, printbuffer must be larger than
	 * anything we ever want to print.
	 */
	i = vsprintf (printbuffer, format, args);
	va_end (args);

385
#ifdef CONFIG_LOGBUFFER
386
	/* Send to the logbuffer */
387 388
	logbuff_log (printbuffer);
#else
W
wdenk 已提交
389 390
	/* Send to the stdout file */
	puts (printbuffer);
391
#endif
W
wdenk 已提交
392 393 394 395

	return 0;
}

396
#ifndef CONFIG_RELOC_FIXUP_WORKS
W
wdenk 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
void post_reloc (void)
{
	unsigned int i;

	/*
	 * We have to relocate the test table manually
	 */
	for (i = 0; i < post_list_size; i++) {
		ulong addr;
		struct post_test *test = post_list + i;

		if (test->name) {
			addr = (ulong) (test->name) + gd->reloc_off;
			test->name = (char *) addr;
		}

		if (test->cmd) {
			addr = (ulong) (test->cmd) + gd->reloc_off;
			test->cmd = (char *) addr;
		}

		if (test->desc) {
			addr = (ulong) (test->desc) + gd->reloc_off;
			test->desc = (char *) addr;
		}

		if (test->test) {
			addr = (ulong) (test->test) + gd->reloc_off;
			test->test = (int (*)(int flags)) addr;
		}
W
wdenk 已提交
427 428 429 430 431 432 433 434 435

		if (test->init_f) {
			addr = (ulong) (test->init_f) + gd->reloc_off;
			test->init_f = (int (*)(void)) addr;
		}

		if (test->reloc) {
			addr = (ulong) (test->reloc) + gd->reloc_off;
			test->reloc = (void (*)(void)) addr;
W
wdenk 已提交
436

W
wdenk 已提交
437 438
			test->reloc();
		}
W
wdenk 已提交
439 440
	}
}
441
#endif
W
wdenk 已提交
442

W
wdenk 已提交
443 444 445 446 447 448 449 450 451 452

/*
 * Some tests (e.g. SYSMON) need the time when post_init_f started,
 * but we cannot use get_timer() at this point.
 *
 * On PowerPC we implement it using the timebase register.
 */
unsigned long post_time_ms (unsigned long base)
{
#ifdef CONFIG_PPC
453
	return (unsigned long)(get_ticks () / (get_tbclk () / CONFIG_SYS_HZ)) - base;
W
wdenk 已提交
454
#else
455
#warning "Not implemented yet"
W
wdenk 已提交
456 457 458
	return 0; /* Not implemented yet */
#endif
}