3 * Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
4 * Copyright (C) 2017 Patrick Rudolph <siro@das-labor.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * - Name: MC146818: Real-time Clock Plus RAM (RTC)
33 * - PDF: http://www.freescale.com/files/microcontrollers/doc/data_sheet/MC146818.pdf
34 * - Order number: MC146818/D
39 * http://bochs.sourceforge.net/techspec/CMOS-reference.txt
40 * http://www.bioscentral.com/misc/cmosmap.htm
42 #include <libpayload-config.h>
43 #include <libpayload.h>
46 * PCs can have either 64 (very old ones), 128, or 256 bytes of CMOS RAM.
48 * Usually you access the lower 128 CMOS bytes via I/O port 0x70/0x71.
49 * For more recent chipsets with 256 bytes, you have to access the upper
50 * 128 bytes (128-255) using two different registers, usually 0x72/0x73.
52 * On some chipsets this can be different, though. The VIA VT8237R for example
53 * only recognizes the ports 0x74/0x75 for accessing the high 128 CMOS bytes
54 * (as seems to be the case for multiple VIA chipsets).
56 * It's very chipset-specific if and how the upper 128 bytes are enabled at
57 * all, but this work should be done in coreboot anyway. Libpayload assumes
58 * that coreboot has properly enabled access to the upper 128 bytes and
59 * doesn't try to do this on its own.
61 #define RTC_PORT_STANDARD 0x70
62 #if CONFIG(LP_RTC_PORT_EXTENDED_VIA)
63 #define RTC_PORT_EXTENDED 0x74
65 #define RTC_PORT_EXTENDED 0x72
69 * Read a byte from the specified NVRAM address.
71 * @param addr The NVRAM address to read a byte from.
72 * @return The byte at the given NVRAM address.
74 u8
nvram_read(u8 addr
)
76 u16 rtc_port
= addr
< 128 ? RTC_PORT_STANDARD
: RTC_PORT_EXTENDED
;
79 return inb(rtc_port
+ 1);
83 * Write a byte to the specified NVRAM address.
85 * @param val The byte to write to NVRAM.
86 * @param addr The NVRAM address to write to.
88 void nvram_write(u8 val
, u8 addr
)
90 u16 rtc_port
= addr
< 128 ? RTC_PORT_STANDARD
: RTC_PORT_EXTENDED
;
93 outb(val
, rtc_port
+ 1);
97 * Return 1 if the NVRAM is currently updating and a 0 otherwise
98 * @return A 1 if the NVRAM is updating and 0 otherwise
101 int nvram_updating(void)
103 return (nvram_read(NVRAM_RTC_FREQ_SELECT
) & NVRAM_RTC_UIP
) ? 1 : 0;
107 * Get the current time and date from the RTC
109 * @param time A pointer to a broken-down time structure
111 void rtc_read_clock(struct tm
*time
)
117 memset(time
, 0, sizeof(*time
));
119 while (nvram_updating())
123 statusB
= nvram_read(NVRAM_RTC_STATUSB
);
125 if (!(statusB
& NVRAM_RTC_FORMAT_BINARY
)) {
126 time
->tm_mon
= bcd2dec(nvram_read(NVRAM_RTC_MONTH
)) - 1;
127 time
->tm_sec
= bcd2dec(nvram_read(NVRAM_RTC_SECONDS
));
128 time
->tm_min
= bcd2dec(nvram_read(NVRAM_RTC_MINUTES
));
129 time
->tm_mday
= bcd2dec(nvram_read(NVRAM_RTC_DAY
));
131 if (!(statusB
& NVRAM_RTC_FORMAT_24HOUR
)) {
132 reg8
= nvram_read(NVRAM_RTC_HOURS
);
133 time
->tm_hour
= bcd2dec(reg8
& 0x7f);
134 time
->tm_hour
+= (reg8
& 0x80) ? 12 : 0;
137 time
->tm_hour
= bcd2dec(nvram_read(NVRAM_RTC_HOURS
));
139 time
->tm_year
= bcd2dec(nvram_read(NVRAM_RTC_YEAR
));
141 time
->tm_mon
= nvram_read(NVRAM_RTC_MONTH
) - 1;
142 time
->tm_sec
= nvram_read(NVRAM_RTC_SECONDS
);
143 time
->tm_min
= nvram_read(NVRAM_RTC_MINUTES
);
144 time
->tm_mday
= nvram_read(NVRAM_RTC_DAY
);
145 if (!(statusB
& NVRAM_RTC_FORMAT_24HOUR
)) {
146 reg8
= nvram_read(NVRAM_RTC_HOURS
);
147 time
->tm_hour
= reg8
& 0x7f;
148 time
->tm_hour
+= (reg8
& 0x80) ? 12 : 0;
151 time
->tm_hour
= nvram_read(NVRAM_RTC_HOURS
);
153 time
->tm_year
= nvram_read(NVRAM_RTC_YEAR
);
156 /* Instead of finding the century register,
157 we just make an assumption that if the year value is
158 less then 80, then it is 2000+
160 if (time
->tm_year
< 80)
161 time
->tm_year
+= 100;
165 * Write the current time and date to the RTC
167 * @param time A pointer to a broken-down time structure
169 void rtc_write_clock(const struct tm
*time
)
175 while (nvram_updating())
179 statusB
= nvram_read(NVRAM_RTC_STATUSB
);
181 year
= time
->tm_year
;
185 if (!(statusB
& NVRAM_RTC_FORMAT_BINARY
)) {
186 nvram_write(dec2bcd(time
->tm_mon
+ 1), NVRAM_RTC_MONTH
);
187 nvram_write(dec2bcd(time
->tm_sec
), NVRAM_RTC_SECONDS
);
188 nvram_write(dec2bcd(time
->tm_min
), NVRAM_RTC_MINUTES
);
189 nvram_write(dec2bcd(time
->tm_mday
), NVRAM_RTC_DAY
);
190 if (!(statusB
& NVRAM_RTC_FORMAT_24HOUR
)) {
191 if (time
->tm_hour
> 12)
192 reg8
= dec2bcd(time
->tm_hour
- 12) | 0x80;
194 reg8
= dec2bcd(time
->tm_hour
);
196 reg8
= dec2bcd(time
->tm_hour
);
198 nvram_write(reg8
, NVRAM_RTC_HOURS
);
199 nvram_write(dec2bcd(year
), NVRAM_RTC_YEAR
);
201 nvram_write(time
->tm_mon
+ 1, NVRAM_RTC_MONTH
);
202 nvram_write(time
->tm_sec
, NVRAM_RTC_SECONDS
);
203 nvram_write(time
->tm_min
, NVRAM_RTC_MINUTES
);
204 nvram_write(time
->tm_mday
, NVRAM_RTC_DAY
);
205 if (!(statusB
& NVRAM_RTC_FORMAT_24HOUR
)) {
206 if (time
->tm_hour
> 12)
207 reg8
= (time
->tm_hour
- 12) | 0x80;
209 reg8
= time
->tm_hour
;
211 reg8
= time
->tm_hour
;
213 nvram_write(reg8
, NVRAM_RTC_HOURS
);
214 nvram_write(year
, NVRAM_RTC_YEAR
);