summaryrefslogtreecommitdiff
path: root/src/drivers/pc80/rtc/mc146818rtc_boot.c
blob: 26bcac5acf45772c6786f3158908d6671365ab76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * This file is part of the coreboot 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; version 2 of the License.
 *
 * 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.
 */

#include <stdint.h>
#ifdef __ROMCC__
#include <arch/cbfs.h>
#else
#include <cbfs.h>
#endif
#include <pc80/mc146818rtc.h>
#if CONFIG(USE_OPTION_TABLE)
#include <option_table.h>
#endif

int cmos_error(void);
int cmos_error(void)
{
	unsigned char reg_d;
	/* See if the cmos error condition has been flagged */
	reg_d = cmos_read(RTC_REG_D);
	return (reg_d & RTC_VRT) == 0;
}

int cmos_chksum_valid(void);
int cmos_chksum_valid(void)
{
#if CONFIG(USE_OPTION_TABLE)
	unsigned char addr;
	u16 sum, old_sum;

	sum = 0;
	/* Compute the cmos checksum */
	for (addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++)
		sum += cmos_read(addr);

	/* Read the stored checksum */
	old_sum = cmos_read(LB_CKS_LOC) << 8;
	old_sum |= cmos_read(LB_CKS_LOC + 1);

	return sum == old_sum;
#else
	return 0;
#endif
}

#if CONFIG(USE_OPTION_TABLE)
void sanitize_cmos(void)
{
	if (cmos_error() || !cmos_chksum_valid() ||
	    CONFIG(STATIC_OPTION_TABLE)) {
		size_t length = 128;
		const unsigned char *cmos_default =
#ifdef __ROMCC__
			walkcbfs("cmos.default");
#else
			cbfs_boot_map_with_leak("cmos.default",
					CBFS_COMPONENT_CMOS_DEFAULT, &length);
#endif
		if (cmos_default) {
			int i;
			cmos_disable_rtc();
			for (i = 14; i < MIN(128, length); i++)
				cmos_write_inner(cmos_default[i], i);
			cmos_enable_rtc();
		}
	}
}
#endif