Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #ifndef PC80_MC146818RTC_H |
| 2 | #define PC80_MC146818RTC_H |
| 3 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 4 | #include <types.h> |
| 5 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | #ifndef RTC_BASE_PORT |
| 7 | #define RTC_BASE_PORT 0x70 |
| 8 | #endif |
| 9 | |
| 10 | #define RTC_PORT(x) (RTC_BASE_PORT + (x)) |
| 11 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | /* control registers - Moto names |
| 13 | */ |
| 14 | #define RTC_REG_A 10 |
| 15 | #define RTC_REG_B 11 |
| 16 | #define RTC_REG_C 12 |
| 17 | #define RTC_REG_D 13 |
| 18 | |
| 19 | |
| 20 | /********************************************************************** |
| 21 | * register details |
| 22 | **********************************************************************/ |
| 23 | #define RTC_FREQ_SELECT RTC_REG_A |
| 24 | |
| 25 | /* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus, |
| 26 | * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete, |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 27 | * totaling to a max high interval of 2.228 ms. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | */ |
| 29 | # define RTC_UIP 0x80 |
| 30 | # define RTC_DIV_CTL 0x70 |
| 31 | /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */ |
| 32 | # define RTC_REF_CLCK_4MHZ 0x00 |
| 33 | # define RTC_REF_CLCK_1MHZ 0x10 |
| 34 | # define RTC_REF_CLCK_32KHZ 0x20 |
| 35 | /* 2 values for divider stage reset, others for "testing purposes only" */ |
| 36 | # define RTC_DIV_RESET1 0x60 |
| 37 | # define RTC_DIV_RESET2 0x70 |
| 38 | /* Periodic intr. / Square wave rate select. 0=none, 1=32.8kHz,... 15=2Hz */ |
| 39 | # define RTC_RATE_SELECT 0x0F |
| 40 | # define RTC_RATE_NONE 0x00 |
| 41 | # define RTC_RATE_32786HZ 0x01 |
| 42 | # define RTC_RATE_16384HZ 0x02 |
| 43 | # define RTC_RATE_8192HZ 0x03 |
| 44 | # define RTC_RATE_4096HZ 0x04 |
| 45 | # define RTC_RATE_2048HZ 0x05 |
| 46 | # define RTC_RATE_1024HZ 0x06 |
| 47 | # define RTC_RATE_512HZ 0x07 |
| 48 | # define RTC_RATE_256HZ 0x08 |
| 49 | # define RTC_RATE_128HZ 0x09 |
| 50 | # define RTC_RATE_64HZ 0x0a |
| 51 | # define RTC_RATE_32HZ 0x0b |
| 52 | # define RTC_RATE_16HZ 0x0c |
| 53 | # define RTC_RATE_8HZ 0x0d |
| 54 | # define RTC_RATE_4HZ 0x0e |
| 55 | # define RTC_RATE_2HZ 0x0f |
| 56 | |
| 57 | /**********************************************************************/ |
| 58 | #define RTC_CONTROL RTC_REG_B |
| 59 | # define RTC_SET 0x80 /* disable updates for clock setting */ |
| 60 | # define RTC_PIE 0x40 /* periodic interrupt enable */ |
| 61 | # define RTC_AIE 0x20 /* alarm interrupt enable */ |
| 62 | # define RTC_UIE 0x10 /* update-finished interrupt enable */ |
| 63 | # define RTC_SQWE 0x08 /* enable square-wave output */ |
| 64 | # define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */ |
| 65 | # define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */ |
| 66 | # define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */ |
| 67 | |
| 68 | /**********************************************************************/ |
| 69 | #define RTC_INTR_FLAGS RTC_REG_C |
| 70 | /* caution - cleared by read */ |
| 71 | # define RTC_IRQF 0x80 /* any of the following 3 is active */ |
| 72 | # define RTC_PF 0x40 |
| 73 | # define RTC_AF 0x20 |
| 74 | # define RTC_UF 0x10 |
| 75 | |
| 76 | /**********************************************************************/ |
| 77 | #define RTC_VALID RTC_REG_D |
| 78 | # define RTC_VRT 0x80 /* valid RAM and time */ |
| 79 | /**********************************************************************/ |
| 80 | |
Duncan Laurie | c8c836f | 2012-06-23 13:22:25 -0700 | [diff] [blame] | 81 | /* Date and Time in RTC CMOS */ |
| 82 | #define RTC_CLK_SECOND 0 |
| 83 | #define RTC_CLK_SECOND_ALARM 1 |
| 84 | #define RTC_CLK_MINUTE 2 |
| 85 | #define RTC_CLK_MINUTE_ALARM 3 |
| 86 | #define RTC_CLK_HOUR 4 |
| 87 | #define RTC_CLK_HOUR_ALARM 5 |
| 88 | #define RTC_CLK_DAYOFWEEK 6 |
| 89 | #define RTC_CLK_DAYOFMONTH 7 |
| 90 | #define RTC_CLK_MONTH 8 |
| 91 | #define RTC_CLK_YEAR 9 |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 92 | #define RTC_CLK_ALTCENTURY 0x32 |
| 93 | |
| 94 | #define RTC_HAS_ALTCENTURY 1 |
| 95 | #define RTC_HAS_NO_ALTCENTURY 0 |
Duncan Laurie | c8c836f | 2012-06-23 13:22:25 -0700 | [diff] [blame] | 96 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 97 | /* On PCs, the checksum is built only over bytes 16..45 */ |
| 98 | #define PC_CKS_RANGE_START 16 |
| 99 | #define PC_CKS_RANGE_END 45 |
| 100 | #define PC_CKS_LOC 46 |
| 101 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 102 | #ifndef UTIL_BUILD_OPTION_TABLE |
| 103 | #include <arch/io.h> |
| 104 | static inline unsigned char cmos_read(unsigned char addr) |
| 105 | { |
| 106 | int offs = 0; |
| 107 | if (addr >= 128) { |
| 108 | offs = 2; |
| 109 | addr -= 128; |
| 110 | } |
| 111 | outb(addr, RTC_BASE_PORT + offs + 0); |
| 112 | return inb(RTC_BASE_PORT + offs + 1); |
| 113 | } |
| 114 | |
Patrick Georgi | f943901 | 2012-11-15 14:54:05 +0100 | [diff] [blame] | 115 | static inline void cmos_write_inner(unsigned char val, unsigned char addr) |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 116 | { |
| 117 | int offs = 0; |
| 118 | if (addr >= 128) { |
| 119 | offs = 2; |
| 120 | addr -= 128; |
| 121 | } |
| 122 | outb(addr, RTC_BASE_PORT + offs + 0); |
| 123 | outb(val, RTC_BASE_PORT + offs + 1); |
| 124 | } |
Duncan Laurie | 654f293 | 2011-09-26 13:24:40 -0700 | [diff] [blame] | 125 | |
Patrick Georgi | f943901 | 2012-11-15 14:54:05 +0100 | [diff] [blame] | 126 | static inline void cmos_write(unsigned char val, unsigned char addr) |
| 127 | { |
| 128 | u8 control_state = cmos_read(RTC_CONTROL); |
| 129 | /* There are various places where RTC bits might be hiding, |
| 130 | * eg. the Century / AltCentury byte. So to be safe, disable |
| 131 | * RTC before changing any value. |
| 132 | */ |
| 133 | if ((addr != RTC_CONTROL) && !(control_state & RTC_SET)) { |
| 134 | cmos_write_inner(control_state | RTC_SET, RTC_CONTROL); |
| 135 | } |
| 136 | cmos_write_inner(val, addr); |
| 137 | /* reset to prior configuration */ |
| 138 | if ((addr != RTC_CONTROL) && !(control_state & RTC_SET)) { |
| 139 | cmos_write_inner(control_state, RTC_CONTROL); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static inline void cmos_disable_rtc(void) |
| 144 | { |
| 145 | u8 control_state = cmos_read(RTC_CONTROL); |
| 146 | cmos_write(control_state | RTC_SET, RTC_CONTROL); |
| 147 | } |
| 148 | |
| 149 | static inline void cmos_enable_rtc(void) |
| 150 | { |
| 151 | u8 control_state = cmos_read(RTC_CONTROL); |
| 152 | cmos_write(control_state & ~RTC_SET, RTC_CONTROL); |
| 153 | } |
| 154 | |
Duncan Laurie | 654f293 | 2011-09-26 13:24:40 -0700 | [diff] [blame] | 155 | static inline u32 cmos_read32(u8 offset) |
| 156 | { |
| 157 | u32 value = 0; |
| 158 | u8 i; |
| 159 | for (i = 0; i < sizeof(value); ++i) |
| 160 | value |= cmos_read(offset + i) << (i << 3); |
| 161 | return value; |
| 162 | } |
| 163 | |
| 164 | static inline void cmos_write32(u8 offset, u32 value) |
| 165 | { |
| 166 | u8 i; |
| 167 | for (i = 0; i < sizeof(value); ++i) |
| 168 | cmos_write((value >> (i << 3)) & 0xff, offset + i); |
| 169 | } |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
| 172 | #if !defined(__ROMCC__) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 173 | void rtc_init(int invalid); |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 174 | void rtc_check_update_cmos_date(u8 has_century); |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 175 | #if CONFIG_USE_OPTION_TABLE |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 176 | enum cb_err set_option(const char *name, void *val); |
| 177 | enum cb_err get_option(void *dest, const char *name); |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 178 | unsigned read_option_lowlevel(unsigned start, unsigned size, unsigned def); |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 179 | #else |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 180 | static inline enum cb_err set_option(const char *name __attribute__((unused)), |
| 181 | void *val __attribute__((unused))) |
| 182 | { return CB_CMOS_OTABLE_DISABLED; }; |
| 183 | static inline enum cb_err get_option(void *dest __attribute__((unused)), |
| 184 | const char *name __attribute__((unused))) |
| 185 | { return CB_CMOS_OTABLE_DISABLED; } |
Patrick Georgi | a27561c | 2011-11-22 10:27:24 +0100 | [diff] [blame] | 186 | #define read_option_lowlevel(start, size, def) def |
Luc Verhaegen | a9c5ea0 | 2009-06-03 14:19:33 +0000 | [diff] [blame] | 187 | #endif |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 188 | #else |
Stefan Reinauer | ae5e11d | 2012-04-27 02:31:28 +0200 | [diff] [blame] | 189 | #include <drivers/pc80/mc146818rtc_early.c> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 190 | #endif |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 191 | #define read_option(name, default) read_option_lowlevel(CMOS_VSTART_ ##name, CMOS_VLEN_ ##name, (default)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 192 | |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 193 | #if CONFIG_CMOS_POST |
| 194 | #if CONFIG_USE_OPTION_TABLE |
| 195 | # include "option_table.h" |
| 196 | # define CMOS_POST_OFFSET (CMOS_VSTART_cmos_post_offset >> 3) |
| 197 | #else |
| 198 | # if defined(CONFIG_CMOS_POST_OFFSET) |
| 199 | # define CMOS_POST_OFFSET CONFIG_CMOS_POST_OFFSET |
| 200 | # else |
| 201 | # error "Must define CONFIG_CMOS_POST_OFFSET" |
| 202 | # endif |
| 203 | #endif |
| 204 | |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 205 | /* |
| 206 | * 0 = Bank Select Magic |
| 207 | * 1 = Bank 0 POST |
| 208 | * 2 = Bank 1 POST |
| 209 | * 3-6 = BANK 0 Extra log |
| 210 | * 7-10 = BANK 1 Extra log |
| 211 | */ |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 212 | #define CMOS_POST_BANK_OFFSET (CMOS_POST_OFFSET) |
| 213 | #define CMOS_POST_BANK_0_MAGIC 0x80 |
| 214 | #define CMOS_POST_BANK_0_OFFSET (CMOS_POST_OFFSET + 1) |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 215 | #define CMOS_POST_BANK_0_EXTRA (CMOS_POST_OFFSET + 3) |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 216 | #define CMOS_POST_BANK_1_MAGIC 0x81 |
| 217 | #define CMOS_POST_BANK_1_OFFSET (CMOS_POST_OFFSET + 2) |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 218 | #define CMOS_POST_BANK_1_EXTRA (CMOS_POST_OFFSET + 7) |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 219 | |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame^] | 220 | #define CMOS_POST_EXTRA_DEV_PATH 0x01 |
| 221 | |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 222 | void cmos_post_log(void); |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 223 | #endif /* CONFIG_CMOS_POST */ |
| 224 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 225 | #endif /* PC80_MC146818RTC_H */ |