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 | */ |
| 42 | |
| 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 | } |