blob: c82533173f8398c75e2bfdca7e52cc6915f54181 [file] [log] [blame]
Uwe Hermann8cc38d22008-03-27 23:26:40 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Datasheet:
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
35 */
36
37/*
38 * See also:
39 * http://bochs.sourceforge.net/techspec/CMOS-reference.txt
40 * http://www.bioscentral.com/misc/cmosmap.htm
41 */
Jordan Crousedb8c0ab2008-11-24 17:54:46 +000042#include <libpayload-config.h>
Uwe Hermann8cc38d22008-03-27 23:26:40 +000043#include <libpayload.h>
44
Uwe Hermann30d789b2008-03-31 20:21:49 +000045
46/**
47 * PCs can have either 64 (very old ones), 128, or 256 bytes of CMOS RAM.
48 *
49 * Usually you access the lower 128 CMOS bytes via I/O port 0x70/0x71.
50 * For more recent chipsets with 256 bytes, you have to access the upper
51 * 128 bytes (128-255) using two different registers, usually 0x72/0x73.
52 *
53 * On some chipsets this can be different, though. The VIA VT8237R for example
54 * only recognizes the ports 0x74/0x75 for accessing the high 128 CMOS bytes
55 * (as seems to be the case for multiple VIA chipsets).
56 *
57 * It's very chipset-specific if and how the upper 128 bytes are enabled at
58 * all, but this work should be done in coreboot anyway. Libpayload assumes
59 * that coreboot has properly enabled access to the upper 128 bytes and
60 * doesn't try to do this on its own.
61 */
62#define RTC_PORT_STANDARD 0x70
Stefan Reinauer1b4d3942015-06-29 15:47:34 -070063#if IS_ENABLED(CONFIG_LP_RTC_PORT_EXTENDED_VIA)
Uwe Hermann30d789b2008-03-31 20:21:49 +000064#define RTC_PORT_EXTENDED 0x74
65#else
66#define RTC_PORT_EXTENDED 0x72
67#endif
Uwe Hermann8cc38d22008-03-27 23:26:40 +000068
69/**
Uwe Hermannc16d24e2008-03-31 15:17:39 +000070 * Read a byte from the specified NVRAM address.
Stefan Reinauer14e22772010-04-27 06:56:47 +000071 *
Uwe Hermannc16d24e2008-03-31 15:17:39 +000072 * @param addr The NVRAM address to read a byte from.
73 * @return The byte at the given NVRAM address.
Uwe Hermann8cc38d22008-03-27 23:26:40 +000074 */
Uwe Hermannc16d24e2008-03-31 15:17:39 +000075u8 nvram_read(u8 addr)
Uwe Hermann8cc38d22008-03-27 23:26:40 +000076{
Uwe Hermann30d789b2008-03-31 20:21:49 +000077 u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED;
78
79 outb(addr, rtc_port);
80 return inb(rtc_port + 1);
Uwe Hermann8cc38d22008-03-27 23:26:40 +000081}
82
83/**
Uwe Hermannc16d24e2008-03-31 15:17:39 +000084 * Write a byte to the specified NVRAM address.
Stefan Reinauer14e22772010-04-27 06:56:47 +000085 *
Uwe Hermannc16d24e2008-03-31 15:17:39 +000086 * @param val The byte to write to NVRAM.
87 * @param addr The NVRAM address to write to.
Uwe Hermann8cc38d22008-03-27 23:26:40 +000088 */
Uwe Hermannc16d24e2008-03-31 15:17:39 +000089void nvram_write(u8 val, u8 addr)
Uwe Hermann8cc38d22008-03-27 23:26:40 +000090{
Uwe Hermann30d789b2008-03-31 20:21:49 +000091 u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED;
92
93 outb(addr, rtc_port);
94 outb(val, rtc_port + 1);
Uwe Hermann8cc38d22008-03-27 23:26:40 +000095}
Jordan Crousee2271432008-04-25 23:11:02 +000096
97/**
98 * Return 1 if the NVRAM is currently updating and a 0 otherwise
99 * @return A 1 if the NVRAM is updating and 0 otherwise
100 */
101
102int nvram_updating(void)
103{
104 return (nvram_read(NVRAM_RTC_FREQ_SELECT) & NVRAM_RTC_UIP) ? 1 : 0;
105}
106
107/**
108 * Get the current time and date from the RTC
109 *
110 * @param time A pointer to a broken-down time structure
111 */
112void rtc_read_clock(struct tm *time)
113{
114 memset(time, 0, sizeof(*time));
115
116 while(nvram_updating());
117
118 time->tm_mon = bcd2dec(nvram_read(NVRAM_RTC_MONTH)) - 1;
119 time->tm_sec = bcd2dec(nvram_read(NVRAM_RTC_SECONDS));
120 time->tm_min = bcd2dec(nvram_read(NVRAM_RTC_MINUTES));
121 time->tm_mday = bcd2dec(nvram_read(NVRAM_RTC_DAY));
122 time->tm_hour = bcd2dec(nvram_read(NVRAM_RTC_HOURS));
123
124 /* Instead of finding the century register,
125 we just make an assumption that if the year value is
126 less then 80, then it is 2000+
127 */
128
129 time->tm_year = bcd2dec(nvram_read(NVRAM_RTC_YEAR));
130
131 if (time->tm_year < 80)
132 time->tm_year += 100;
133}