Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 1 | /* |
| 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 Crouse | db8c0ab | 2008-11-24 17:54:46 +0000 | [diff] [blame] | 42 | #include <libpayload-config.h> |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 43 | #include <libpayload.h> |
| 44 | |
Uwe Hermann | 30d789b | 2008-03-31 20:21:49 +0000 | [diff] [blame] | 45 | |
| 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 |
| 63 | #ifdef CONFIG_RTC_PORT_EXTENDED_VIA |
| 64 | #define RTC_PORT_EXTENDED 0x74 |
| 65 | #else |
| 66 | #define RTC_PORT_EXTENDED 0x72 |
| 67 | #endif |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 70 | * Read a byte from the specified NVRAM address. |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 71 | * |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 72 | * @param addr The NVRAM address to read a byte from. |
| 73 | * @return The byte at the given NVRAM address. |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 74 | */ |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 75 | u8 nvram_read(u8 addr) |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 76 | { |
Uwe Hermann | 30d789b | 2008-03-31 20:21:49 +0000 | [diff] [blame] | 77 | u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED; |
| 78 | |
| 79 | outb(addr, rtc_port); |
| 80 | return inb(rtc_port + 1); |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 84 | * Write a byte to the specified NVRAM address. |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 85 | * |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 86 | * @param val The byte to write to NVRAM. |
| 87 | * @param addr The NVRAM address to write to. |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 88 | */ |
Uwe Hermann | c16d24e | 2008-03-31 15:17:39 +0000 | [diff] [blame] | 89 | void nvram_write(u8 val, u8 addr) |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 90 | { |
Uwe Hermann | 30d789b | 2008-03-31 20:21:49 +0000 | [diff] [blame] | 91 | u16 rtc_port = addr < 128 ? RTC_PORT_STANDARD : RTC_PORT_EXTENDED; |
| 92 | |
| 93 | outb(addr, rtc_port); |
| 94 | outb(val, rtc_port + 1); |
Uwe Hermann | 8cc38d2 | 2008-03-27 23:26:40 +0000 | [diff] [blame] | 95 | } |
Jordan Crouse | e227143 | 2008-04-25 23:11:02 +0000 | [diff] [blame] | 96 | |
| 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 | |
| 102 | int 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 | */ |
| 112 | void 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 | } |
| 134 | |
| 135 | |