Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #ifndef PC80_MC146818RTC_H |
| 2 | #define PC80_MC146818RTC_H |
| 3 | |
Edward O'Callaghan | c5fff75 | 2014-06-24 16:29:12 +1000 | [diff] [blame] | 4 | #if CONFIG_ARCH_X86 |
| 5 | |
Edward O'Callaghan | 502c3db | 2014-06-21 23:02:46 +1000 | [diff] [blame] | 6 | #include <arch/io.h> |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 7 | #include <types.h> |
| 8 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 9 | #ifndef RTC_BASE_PORT |
| 10 | #define RTC_BASE_PORT 0x70 |
| 11 | #endif |
| 12 | |
| 13 | #define RTC_PORT(x) (RTC_BASE_PORT + (x)) |
| 14 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 15 | /* control registers - Moto names |
| 16 | */ |
| 17 | #define RTC_REG_A 10 |
| 18 | #define RTC_REG_B 11 |
| 19 | #define RTC_REG_C 12 |
| 20 | #define RTC_REG_D 13 |
| 21 | |
| 22 | |
| 23 | /********************************************************************** |
| 24 | * register details |
| 25 | **********************************************************************/ |
| 26 | #define RTC_FREQ_SELECT RTC_REG_A |
| 27 | |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 28 | #define RTC_BOOT_NORMAL 0x1 |
| 29 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | /* update-in-progress - set to "1" 244 microsecs before RTC goes off the bus, |
| 31 | * reset after update (may take 1.984ms @ 32768Hz RefClock) is complete, |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 32 | * totaling to a max high interval of 2.228 ms. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | */ |
| 34 | # define RTC_UIP 0x80 |
| 35 | # define RTC_DIV_CTL 0x70 |
| 36 | /* divider control: refclock values 4.194 / 1.049 MHz / 32.768 kHz */ |
| 37 | # define RTC_REF_CLCK_4MHZ 0x00 |
| 38 | # define RTC_REF_CLCK_1MHZ 0x10 |
| 39 | # define RTC_REF_CLCK_32KHZ 0x20 |
| 40 | /* 2 values for divider stage reset, others for "testing purposes only" */ |
| 41 | # define RTC_DIV_RESET1 0x60 |
| 42 | # define RTC_DIV_RESET2 0x70 |
Elyes HAOUAS | a0fed37 | 2016-09-16 20:17:40 +0200 | [diff] [blame] | 43 | /* Periodic intr. / Square wave rate select. 0 = none, 1 = 32.8kHz,... 15 = 2Hz */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 44 | # define RTC_RATE_SELECT 0x0F |
| 45 | # define RTC_RATE_NONE 0x00 |
| 46 | # define RTC_RATE_32786HZ 0x01 |
| 47 | # define RTC_RATE_16384HZ 0x02 |
| 48 | # define RTC_RATE_8192HZ 0x03 |
| 49 | # define RTC_RATE_4096HZ 0x04 |
| 50 | # define RTC_RATE_2048HZ 0x05 |
| 51 | # define RTC_RATE_1024HZ 0x06 |
| 52 | # define RTC_RATE_512HZ 0x07 |
| 53 | # define RTC_RATE_256HZ 0x08 |
| 54 | # define RTC_RATE_128HZ 0x09 |
| 55 | # define RTC_RATE_64HZ 0x0a |
| 56 | # define RTC_RATE_32HZ 0x0b |
| 57 | # define RTC_RATE_16HZ 0x0c |
| 58 | # define RTC_RATE_8HZ 0x0d |
| 59 | # define RTC_RATE_4HZ 0x0e |
| 60 | # define RTC_RATE_2HZ 0x0f |
| 61 | |
| 62 | /**********************************************************************/ |
| 63 | #define RTC_CONTROL RTC_REG_B |
| 64 | # define RTC_SET 0x80 /* disable updates for clock setting */ |
| 65 | # define RTC_PIE 0x40 /* periodic interrupt enable */ |
| 66 | # define RTC_AIE 0x20 /* alarm interrupt enable */ |
| 67 | # define RTC_UIE 0x10 /* update-finished interrupt enable */ |
| 68 | # define RTC_SQWE 0x08 /* enable square-wave output */ |
| 69 | # define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */ |
| 70 | # define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */ |
| 71 | # define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */ |
| 72 | |
| 73 | /**********************************************************************/ |
| 74 | #define RTC_INTR_FLAGS RTC_REG_C |
| 75 | /* caution - cleared by read */ |
| 76 | # define RTC_IRQF 0x80 /* any of the following 3 is active */ |
| 77 | # define RTC_PF 0x40 |
| 78 | # define RTC_AF 0x20 |
| 79 | # define RTC_UF 0x10 |
| 80 | |
| 81 | /**********************************************************************/ |
| 82 | #define RTC_VALID RTC_REG_D |
| 83 | # define RTC_VRT 0x80 /* valid RAM and time */ |
| 84 | /**********************************************************************/ |
| 85 | |
Duncan Laurie | c8c836f | 2012-06-23 13:22:25 -0700 | [diff] [blame] | 86 | /* Date and Time in RTC CMOS */ |
| 87 | #define RTC_CLK_SECOND 0 |
| 88 | #define RTC_CLK_SECOND_ALARM 1 |
| 89 | #define RTC_CLK_MINUTE 2 |
| 90 | #define RTC_CLK_MINUTE_ALARM 3 |
| 91 | #define RTC_CLK_HOUR 4 |
| 92 | #define RTC_CLK_HOUR_ALARM 5 |
| 93 | #define RTC_CLK_DAYOFWEEK 6 |
| 94 | #define RTC_CLK_DAYOFMONTH 7 |
| 95 | #define RTC_CLK_MONTH 8 |
| 96 | #define RTC_CLK_YEAR 9 |
zbao | a1e6a9c | 2012-08-02 19:02:26 +0800 | [diff] [blame] | 97 | #define RTC_CLK_ALTCENTURY 0x32 |
| 98 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 99 | /* On PCs, the checksum is built only over bytes 16..45 */ |
| 100 | #define PC_CKS_RANGE_START 16 |
| 101 | #define PC_CKS_RANGE_END 45 |
| 102 | #define PC_CKS_LOC 46 |
| 103 | |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 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 | |
Marshall Dawson | 5a043fe | 2016-11-05 18:31:33 -0600 | [diff] [blame] | 115 | /* Upon return the caller is guaranteed 244 microseconds to complete any |
| 116 | * RTC operations. wait_uip may be called a single time prior to multiple |
| 117 | * accesses, but sequences requiring more time should call wait_uip again. |
| 118 | */ |
| 119 | static inline void wait_uip(void) |
| 120 | { |
| 121 | while (cmos_read(RTC_REG_A) & RTC_UIP) |
| 122 | ; |
| 123 | } |
| 124 | |
Patrick Georgi | f943901 | 2012-11-15 14:54:05 +0100 | [diff] [blame] | 125 | static inline void cmos_write_inner(unsigned char val, unsigned char addr) |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 126 | { |
| 127 | int offs = 0; |
| 128 | if (addr >= 128) { |
| 129 | offs = 2; |
| 130 | addr -= 128; |
| 131 | } |
| 132 | outb(addr, RTC_BASE_PORT + offs + 0); |
| 133 | outb(val, RTC_BASE_PORT + offs + 1); |
| 134 | } |
Duncan Laurie | 654f293 | 2011-09-26 13:24:40 -0700 | [diff] [blame] | 135 | |
Patrick Georgi | f943901 | 2012-11-15 14:54:05 +0100 | [diff] [blame] | 136 | static inline void cmos_write(unsigned char val, unsigned char addr) |
| 137 | { |
| 138 | u8 control_state = cmos_read(RTC_CONTROL); |
| 139 | /* There are various places where RTC bits might be hiding, |
| 140 | * eg. the Century / AltCentury byte. So to be safe, disable |
| 141 | * RTC before changing any value. |
| 142 | */ |
| 143 | if ((addr != RTC_CONTROL) && !(control_state & RTC_SET)) { |
| 144 | cmos_write_inner(control_state | RTC_SET, RTC_CONTROL); |
| 145 | } |
| 146 | cmos_write_inner(val, addr); |
| 147 | /* reset to prior configuration */ |
| 148 | if ((addr != RTC_CONTROL) && !(control_state & RTC_SET)) { |
| 149 | cmos_write_inner(control_state, RTC_CONTROL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static inline void cmos_disable_rtc(void) |
| 154 | { |
| 155 | u8 control_state = cmos_read(RTC_CONTROL); |
| 156 | cmos_write(control_state | RTC_SET, RTC_CONTROL); |
| 157 | } |
| 158 | |
| 159 | static inline void cmos_enable_rtc(void) |
| 160 | { |
| 161 | u8 control_state = cmos_read(RTC_CONTROL); |
| 162 | cmos_write(control_state & ~RTC_SET, RTC_CONTROL); |
| 163 | } |
| 164 | |
Duncan Laurie | 654f293 | 2011-09-26 13:24:40 -0700 | [diff] [blame] | 165 | static inline u32 cmos_read32(u8 offset) |
| 166 | { |
| 167 | u32 value = 0; |
| 168 | u8 i; |
| 169 | for (i = 0; i < sizeof(value); ++i) |
| 170 | value |= cmos_read(offset + i) << (i << 3); |
| 171 | return value; |
| 172 | } |
| 173 | |
| 174 | static inline void cmos_write32(u8 offset, u32 value) |
| 175 | { |
| 176 | u8 i; |
| 177 | for (i = 0; i < sizeof(value); ++i) |
| 178 | cmos_write((value >> (i << 3)) & 0xff, offset + i); |
| 179 | } |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 180 | |
| 181 | #if !defined(__ROMCC__) |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 182 | void cmos_init(bool invalid); |
Gabe Black | 03abaee21 | 2014-04-30 21:31:44 -0700 | [diff] [blame] | 183 | void cmos_check_update_date(void); |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 184 | |
Alexandru Gagniuc | d7134e0 | 2013-11-23 18:54:44 -0600 | [diff] [blame] | 185 | enum cb_err set_option(const char *name, void *val); |
| 186 | enum cb_err get_option(void *dest, const char *name); |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame^] | 187 | unsigned int read_option_lowlevel(unsigned int start, unsigned int size, |
| 188 | unsigned int def); |
Alexandru Gagniuc | b5669ba | 2015-01-30 00:07:12 -0600 | [diff] [blame] | 189 | |
Edward O'Callaghan | d638c2b | 2014-06-26 18:11:07 +1000 | [diff] [blame] | 190 | #else /* defined(__ROMCC__) */ |
Stefan Reinauer | 86ddd73 | 2016-03-11 20:22:28 -0800 | [diff] [blame] | 191 | #include <drivers/pc80/rtc/mc146818rtc_early.c> |
Edward O'Callaghan | d638c2b | 2014-06-26 18:11:07 +1000 | [diff] [blame] | 192 | #endif /* !defined(__ROMCC__) */ |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 193 | #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] | 194 | |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 195 | #if CONFIG_CMOS_POST |
| 196 | #if CONFIG_USE_OPTION_TABLE |
| 197 | # include "option_table.h" |
| 198 | # define CMOS_POST_OFFSET (CMOS_VSTART_cmos_post_offset >> 3) |
| 199 | #else |
Martin Roth | 46cf9f7 | 2015-07-11 13:56:58 -0600 | [diff] [blame] | 200 | # if defined(CONFIG_CMOS_POST_OFFSET) && CONFIG_CMOS_POST_OFFSET |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 201 | # define CMOS_POST_OFFSET CONFIG_CMOS_POST_OFFSET |
| 202 | # else |
Martin Roth | 46cf9f7 | 2015-07-11 13:56:58 -0600 | [diff] [blame] | 203 | # error "Must configure CONFIG_CMOS_POST_OFFSET" |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 204 | # endif |
| 205 | #endif |
| 206 | |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 207 | /* |
| 208 | * 0 = Bank Select Magic |
| 209 | * 1 = Bank 0 POST |
| 210 | * 2 = Bank 1 POST |
| 211 | * 3-6 = BANK 0 Extra log |
| 212 | * 7-10 = BANK 1 Extra log |
| 213 | */ |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 214 | #define CMOS_POST_BANK_OFFSET (CMOS_POST_OFFSET) |
| 215 | #define CMOS_POST_BANK_0_MAGIC 0x80 |
| 216 | #define CMOS_POST_BANK_0_OFFSET (CMOS_POST_OFFSET + 1) |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 217 | #define CMOS_POST_BANK_0_EXTRA (CMOS_POST_OFFSET + 3) |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 218 | #define CMOS_POST_BANK_1_MAGIC 0x81 |
| 219 | #define CMOS_POST_BANK_1_OFFSET (CMOS_POST_OFFSET + 2) |
Duncan Laurie | d5686fe | 2013-06-10 10:21:41 -0700 | [diff] [blame] | 220 | #define CMOS_POST_BANK_1_EXTRA (CMOS_POST_OFFSET + 7) |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 221 | |
Duncan Laurie | 8adf7a2 | 2013-06-10 10:34:20 -0700 | [diff] [blame] | 222 | #define CMOS_POST_EXTRA_DEV_PATH 0x01 |
| 223 | |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 224 | void cmos_post_log(void); |
Aaron Durbin | 49342cd | 2017-01-05 10:07:19 -0600 | [diff] [blame] | 225 | |
| 226 | /* cmos_post_init() is exposed in this manner because it also needs to be called |
| 227 | * by bootblock code compiled by romcc. */ |
| 228 | static inline void cmos_post_init(void) |
| 229 | { |
| 230 | u8 magic = CMOS_POST_BANK_0_MAGIC; |
| 231 | |
| 232 | /* Switch to the other bank */ |
| 233 | switch (cmos_read(CMOS_POST_BANK_OFFSET)) { |
| 234 | case CMOS_POST_BANK_1_MAGIC: |
| 235 | break; |
| 236 | case CMOS_POST_BANK_0_MAGIC: |
| 237 | magic = CMOS_POST_BANK_1_MAGIC; |
| 238 | break; |
| 239 | default: |
| 240 | /* Initialize to zero */ |
| 241 | cmos_write(0, CMOS_POST_BANK_0_OFFSET); |
| 242 | cmos_write(0, CMOS_POST_BANK_1_OFFSET); |
| 243 | #if CONFIG_CMOS_POST_EXTRA |
| 244 | cmos_write32(CMOS_POST_BANK_0_EXTRA, 0); |
| 245 | cmos_write32(CMOS_POST_BANK_1_EXTRA, 0); |
| 246 | #endif |
| 247 | } |
| 248 | |
| 249 | cmos_write(magic, CMOS_POST_BANK_OFFSET); |
| 250 | } |
Edward O'Callaghan | 5c97142 | 2014-04-15 14:32:53 +1000 | [diff] [blame] | 251 | #else |
| 252 | static inline void cmos_post_log(void) {} |
Aaron Durbin | 49342cd | 2017-01-05 10:07:19 -0600 | [diff] [blame] | 253 | static inline void cmos_post_init(void) {} |
Duncan Laurie | b6e97b1 | 2012-09-09 19:09:56 -0700 | [diff] [blame] | 254 | #endif /* CONFIG_CMOS_POST */ |
| 255 | |
Aaron Durbin | 49342cd | 2017-01-05 10:07:19 -0600 | [diff] [blame] | 256 | #else /* !CONFIG_ARCH_X86 */ |
| 257 | static inline void cmos_post_init(void) {} |
Edward O'Callaghan | c5fff75 | 2014-06-24 16:29:12 +1000 | [diff] [blame] | 258 | #endif /* CONFIG_ARCH_X86 */ |
| 259 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 260 | #endif /* PC80_MC146818RTC_H */ |