Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 16bit code to handle system clocks. |
| 2 | // |
Kevin O'Connor | abf31d3 | 2010-07-26 22:33:54 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 7 | |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 8 | #include "biosvar.h" // SET_BDA |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 9 | #include "util.h" // debug_enter |
| 10 | #include "disk.h" // floppy_tick |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 11 | #include "cmos.h" // inb_cmos |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 12 | #include "pic.h" // eoi_pic1 |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 13 | #include "bregs.h" // struct bregs |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 14 | #include "biosvar.h" // GET_GLOBAL |
Kevin O'Connor | 0e88576 | 2010-05-01 22:14:40 -0400 | [diff] [blame] | 15 | #include "usb-hid.h" // usb_check_event |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 16 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 17 | // RTC register flags |
| 18 | #define RTC_A_UIP 0x80 |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 19 | |
| 20 | #define RTC_B_SET 0x80 |
| 21 | #define RTC_B_PIE 0x40 |
| 22 | #define RTC_B_AIE 0x20 |
| 23 | #define RTC_B_UIE 0x10 |
| 24 | #define RTC_B_BIN 0x04 |
| 25 | #define RTC_B_24HR 0x02 |
| 26 | #define RTC_B_DSE 0x01 |
| 27 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 28 | |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 29 | // Bits for PORT_PS2_CTRLB |
| 30 | #define PPCB_T2GATE (1<<0) |
| 31 | #define PPCB_SPKR (1<<1) |
| 32 | #define PPCB_T2OUT (1<<5) |
| 33 | |
| 34 | // Bits for PORT_PIT_MODE |
| 35 | #define PM_SEL_TIMER0 (0<<6) |
| 36 | #define PM_SEL_TIMER1 (1<<6) |
| 37 | #define PM_SEL_TIMER2 (2<<6) |
| 38 | #define PM_SEL_READBACK (3<<6) |
| 39 | #define PM_ACCESS_LATCH (0<<4) |
| 40 | #define PM_ACCESS_LOBYTE (1<<4) |
| 41 | #define PM_ACCESS_HIBYTE (2<<4) |
| 42 | #define PM_ACCESS_WORD (3<<4) |
| 43 | #define PM_MODE0 (0<<1) |
| 44 | #define PM_MODE1 (1<<1) |
| 45 | #define PM_MODE2 (2<<1) |
| 46 | #define PM_MODE3 (3<<1) |
| 47 | #define PM_MODE4 (4<<1) |
| 48 | #define PM_MODE5 (5<<1) |
| 49 | #define PM_CNT_BINARY (0<<0) |
| 50 | #define PM_CNT_BCD (1<<0) |
| 51 | |
| 52 | |
| 53 | /**************************************************************** |
| 54 | * TSC timer |
| 55 | ****************************************************************/ |
| 56 | |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 57 | #define CALIBRATE_COUNT 0x800 // Approx 1.7ms |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 58 | |
Kevin O'Connor | 372e071 | 2009-09-09 09:51:31 -0400 | [diff] [blame] | 59 | u32 cpu_khz VAR16VISIBLE; |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 60 | |
| 61 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 62 | calibrate_tsc(void) |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 63 | { |
| 64 | // Setup "timer2" |
| 65 | u8 orig = inb(PORT_PS2_CTRLB); |
| 66 | outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB); |
| 67 | /* binary, mode 0, LSB/MSB, Ch 2 */ |
| 68 | outb(PM_SEL_TIMER2|PM_ACCESS_WORD|PM_MODE0|PM_CNT_BINARY, PORT_PIT_MODE); |
| 69 | /* LSB of ticks */ |
| 70 | outb(CALIBRATE_COUNT & 0xFF, PORT_PIT_COUNTER2); |
| 71 | /* MSB of ticks */ |
| 72 | outb(CALIBRATE_COUNT >> 8, PORT_PIT_COUNTER2); |
| 73 | |
| 74 | u64 start = rdtscll(); |
| 75 | while ((inb(PORT_PS2_CTRLB) & PPCB_T2OUT) == 0) |
| 76 | ; |
| 77 | u64 end = rdtscll(); |
| 78 | |
| 79 | // Restore PORT_PS2_CTRLB |
| 80 | outb(orig, PORT_PS2_CTRLB); |
| 81 | |
| 82 | // Store calibrated cpu khz. |
| 83 | u64 diff = end - start; |
| 84 | dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n" |
| 85 | , (u32)start, (u32)end, (u32)diff); |
| 86 | u32 hz = diff * PIT_TICK_RATE / CALIBRATE_COUNT; |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 87 | SET_GLOBAL(cpu_khz, hz / 1000); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 88 | |
| 89 | dprintf(1, "CPU Mhz=%u\n", hz / 1000000); |
| 90 | } |
| 91 | |
| 92 | static void |
Kevin O'Connor | 89eb624 | 2009-10-22 22:30:37 -0400 | [diff] [blame] | 93 | tscdelay(u64 diff) |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 94 | { |
| 95 | u64 start = rdtscll(); |
| 96 | u64 end = start + diff; |
Kevin O'Connor | 144817b | 2010-05-23 10:46:49 -0400 | [diff] [blame] | 97 | while (!check_tsc(end)) |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 98 | cpu_relax(); |
| 99 | } |
| 100 | |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 101 | static void |
| 102 | tscsleep(u64 diff) |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 103 | { |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 104 | u64 start = rdtscll(); |
| 105 | u64 end = start + diff; |
Kevin O'Connor | 144817b | 2010-05-23 10:46:49 -0400 | [diff] [blame] | 106 | while (!check_tsc(end)) |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 107 | yield(); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 108 | } |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 109 | |
| 110 | void ndelay(u32 count) { |
| 111 | tscdelay(count * GET_GLOBAL(cpu_khz) / 1000000); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 112 | } |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 113 | void udelay(u32 count) { |
| 114 | tscdelay(count * GET_GLOBAL(cpu_khz) / 1000); |
| 115 | } |
| 116 | void mdelay(u32 count) { |
| 117 | tscdelay(count * GET_GLOBAL(cpu_khz)); |
| 118 | } |
| 119 | |
| 120 | void nsleep(u32 count) { |
| 121 | tscsleep(count * GET_GLOBAL(cpu_khz) / 1000000); |
| 122 | } |
| 123 | void usleep(u32 count) { |
| 124 | tscsleep(count * GET_GLOBAL(cpu_khz) / 1000); |
| 125 | } |
| 126 | void msleep(u32 count) { |
| 127 | tscsleep(count * GET_GLOBAL(cpu_khz)); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 130 | // Return the TSC value that is 'msecs' time in the future. |
| 131 | u64 |
| 132 | calc_future_tsc(u32 msecs) |
| 133 | { |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 134 | u32 khz = GET_GLOBAL(cpu_khz); |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 135 | return rdtscll() + ((u64)khz * msecs); |
| 136 | } |
Kevin O'Connor | 1c46a54 | 2009-10-17 23:53:32 -0400 | [diff] [blame] | 137 | u64 |
| 138 | calc_future_tsc_usec(u32 usecs) |
| 139 | { |
| 140 | u32 khz = GET_GLOBAL(cpu_khz); |
| 141 | return rdtscll() + ((u64)(khz/1000) * usecs); |
| 142 | } |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 143 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 144 | |
| 145 | /**************************************************************** |
| 146 | * Init |
| 147 | ****************************************************************/ |
| 148 | |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 149 | static int |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 150 | rtc_updating(void) |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 151 | { |
| 152 | // This function checks to see if the update-in-progress bit |
| 153 | // is set in CMOS Status Register A. If not, it returns 0. |
| 154 | // If it is set, it tries to wait until there is a transition |
| 155 | // to 0, and will return 0 if such a transition occurs. A -1 |
| 156 | // is returned only after timing out. The maximum period |
Kevin O'Connor | 4f5586c | 2009-02-16 10:14:10 -0500 | [diff] [blame] | 157 | // that this bit should be set is constrained to (1984+244) |
Kevin O'Connor | 11cc662 | 2010-03-13 23:04:41 -0500 | [diff] [blame] | 158 | // useconds, but we wait for longer just to be sure. |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 159 | |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 160 | if ((inb_cmos(CMOS_STATUS_A) & RTC_A_UIP) == 0) |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 161 | return 0; |
Kevin O'Connor | 11cc662 | 2010-03-13 23:04:41 -0500 | [diff] [blame] | 162 | u64 end = calc_future_tsc(15); |
| 163 | for (;;) { |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 164 | if ((inb_cmos(CMOS_STATUS_A) & RTC_A_UIP) == 0) |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 165 | return 0; |
Kevin O'Connor | 144817b | 2010-05-23 10:46:49 -0400 | [diff] [blame] | 166 | if (check_tsc(end)) |
Kevin O'Connor | 11cc662 | 2010-03-13 23:04:41 -0500 | [diff] [blame] | 167 | // update-in-progress never transitioned to 0 |
| 168 | return -1; |
| 169 | yield(); |
| 170 | } |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 173 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 174 | pit_setup(void) |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 175 | { |
| 176 | // timer0: binary count, 16bit count, mode 2 |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 177 | outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 178 | // maximum count of 0000H = 18.2Hz |
| 179 | outb(0x0, PORT_PIT_COUNTER0); |
| 180 | outb(0x0, PORT_PIT_COUNTER0); |
| 181 | } |
| 182 | |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 183 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 184 | init_rtc(void) |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 185 | { |
Kevin O'Connor | 4f5586c | 2009-02-16 10:14:10 -0500 | [diff] [blame] | 186 | outb_cmos(0x26, CMOS_STATUS_A); // 32,768Khz src, 976.5625us updates |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 187 | u8 regB = inb_cmos(CMOS_STATUS_B); |
| 188 | outb_cmos((regB & RTC_B_DSE) | RTC_B_24HR, CMOS_STATUS_B); |
| 189 | inb_cmos(CMOS_STATUS_C); |
| 190 | inb_cmos(CMOS_STATUS_D); |
| 191 | } |
| 192 | |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 193 | static u32 |
| 194 | bcd2bin(u8 val) |
| 195 | { |
| 196 | return (val & 0xf) + ((val >> 4) * 10); |
| 197 | } |
| 198 | |
| 199 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 200 | timer_setup(void) |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 201 | { |
Kevin O'Connor | 35192dd | 2008-06-08 19:18:33 -0400 | [diff] [blame] | 202 | dprintf(3, "init timer\n"); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 203 | calibrate_tsc(); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 204 | pit_setup(); |
| 205 | |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 206 | init_rtc(); |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 207 | rtc_updating(); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 208 | u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS)); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 209 | u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES)); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 210 | u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS)); |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 211 | u32 ticks = (hours * 60 + minutes) * 60 + seconds; |
| 212 | ticks = ((u64)ticks * PIT_TICK_RATE) / PIT_TICK_INTERVAL; |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 213 | SET_BDA(timer_counter, ticks); |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 214 | |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 215 | enable_hwirq(0, FUNC16(entry_08)); |
| 216 | enable_hwirq(8, FUNC16(entry_70)); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 219 | |
| 220 | /**************************************************************** |
| 221 | * Standard clock functions |
| 222 | ****************************************************************/ |
| 223 | |
Kevin O'Connor | b5cc2ca | 2010-05-23 11:38:53 -0400 | [diff] [blame] | 224 | #define TICKS_PER_DAY (u32)((u64)60*60*24*PIT_TICK_RATE / PIT_TICK_INTERVAL) |
| 225 | |
| 226 | // Calculate the timer value at 'count' number of full timer ticks in |
| 227 | // the future. |
| 228 | u32 |
| 229 | calc_future_timer_ticks(u32 count) |
| 230 | { |
| 231 | return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY; |
| 232 | } |
Kevin O'Connor | bb68591 | 2010-05-23 12:40:40 -0400 | [diff] [blame] | 233 | |
Kevin O'Connor | b5cc2ca | 2010-05-23 11:38:53 -0400 | [diff] [blame] | 234 | // Return the timer value that is 'msecs' time in the future. |
| 235 | u32 |
| 236 | calc_future_timer(u32 msecs) |
| 237 | { |
Kevin O'Connor | bb68591 | 2010-05-23 12:40:40 -0400 | [diff] [blame] | 238 | if (!msecs) |
| 239 | return GET_BDA(timer_counter); |
Kevin O'Connor | abf31d3 | 2010-07-26 22:33:54 -0400 | [diff] [blame] | 240 | u32 kticks = DIV_ROUND_UP((u64)msecs * PIT_TICK_RATE, PIT_TICK_INTERVAL); |
Kevin O'Connor | b5cc2ca | 2010-05-23 11:38:53 -0400 | [diff] [blame] | 241 | u32 ticks = DIV_ROUND_UP(kticks, 1000); |
| 242 | return calc_future_timer_ticks(ticks); |
| 243 | } |
Kevin O'Connor | bb68591 | 2010-05-23 12:40:40 -0400 | [diff] [blame] | 244 | |
Kevin O'Connor | b5cc2ca | 2010-05-23 11:38:53 -0400 | [diff] [blame] | 245 | // Check if the given timer value has passed. |
| 246 | int |
| 247 | check_timer(u32 end) |
| 248 | { |
| 249 | return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY) |
| 250 | < (TICKS_PER_DAY/2)); |
| 251 | } |
| 252 | |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 253 | // get current clock count |
| 254 | static void |
| 255 | handle_1a00(struct bregs *regs) |
| 256 | { |
Kevin O'Connor | 68c5139 | 2010-03-13 22:23:44 -0500 | [diff] [blame] | 257 | yield(); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 258 | u32 ticks = GET_BDA(timer_counter); |
| 259 | regs->cx = ticks >> 16; |
| 260 | regs->dx = ticks; |
| 261 | regs->al = GET_BDA(timer_rollover); |
| 262 | SET_BDA(timer_rollover, 0); // reset flag |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 263 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Set Current Clock Count |
| 267 | static void |
| 268 | handle_1a01(struct bregs *regs) |
| 269 | { |
| 270 | u32 ticks = (regs->cx << 16) | regs->dx; |
| 271 | SET_BDA(timer_counter, ticks); |
| 272 | SET_BDA(timer_rollover, 0); // reset flag |
Kevin O'Connor | 15157a3 | 2008-12-13 11:10:37 -0500 | [diff] [blame] | 273 | // XXX - should use set_code_success()? |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 274 | regs->ah = 0; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 275 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // Read CMOS Time |
| 279 | static void |
| 280 | handle_1a02(struct bregs *regs) |
| 281 | { |
| 282 | if (rtc_updating()) { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 283 | set_invalid(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
| 287 | regs->dh = inb_cmos(CMOS_RTC_SECONDS); |
| 288 | regs->cl = inb_cmos(CMOS_RTC_MINUTES); |
| 289 | regs->ch = inb_cmos(CMOS_RTC_HOURS); |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 290 | regs->dl = inb_cmos(CMOS_STATUS_B) & RTC_B_DSE; |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 291 | regs->ah = 0; |
| 292 | regs->al = regs->ch; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 293 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Set CMOS Time |
| 297 | static void |
| 298 | handle_1a03(struct bregs *regs) |
| 299 | { |
| 300 | // Using a debugger, I notice the following masking/setting |
| 301 | // of bits in Status Register B, by setting Reg B to |
| 302 | // a few values and getting its value after INT 1A was called. |
| 303 | // |
| 304 | // try#1 try#2 try#3 |
| 305 | // before 1111 1101 0111 1101 0000 0000 |
| 306 | // after 0110 0010 0110 0010 0000 0010 |
| 307 | // |
| 308 | // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1 |
| 309 | // My assumption: RegB = ((RegB & 01100000b) | 00000010b) |
| 310 | if (rtc_updating()) { |
| 311 | init_rtc(); |
| 312 | // fall through as if an update were not in progress |
| 313 | } |
| 314 | outb_cmos(regs->dh, CMOS_RTC_SECONDS); |
| 315 | outb_cmos(regs->cl, CMOS_RTC_MINUTES); |
| 316 | outb_cmos(regs->ch, CMOS_RTC_HOURS); |
| 317 | // Set Daylight Savings time enabled bit to requested value |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 318 | u8 val8 = ((inb_cmos(CMOS_STATUS_B) & (RTC_B_PIE|RTC_B_AIE)) |
| 319 | | RTC_B_24HR | (regs->dl & RTC_B_DSE)); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 320 | outb_cmos(val8, CMOS_STATUS_B); |
| 321 | regs->ah = 0; |
| 322 | regs->al = val8; // val last written to Reg B |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 323 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Read CMOS Date |
| 327 | static void |
| 328 | handle_1a04(struct bregs *regs) |
| 329 | { |
| 330 | regs->ah = 0; |
| 331 | if (rtc_updating()) { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 332 | set_invalid(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | regs->cl = inb_cmos(CMOS_RTC_YEAR); |
| 336 | regs->dh = inb_cmos(CMOS_RTC_MONTH); |
| 337 | regs->dl = inb_cmos(CMOS_RTC_DAY_MONTH); |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 338 | if (CONFIG_COREBOOT) { |
| 339 | if (regs->cl > 0x80) |
| 340 | regs->ch = 0x19; |
| 341 | else |
| 342 | regs->ch = 0x20; |
| 343 | } else { |
| 344 | regs->ch = inb_cmos(CMOS_CENTURY); |
| 345 | } |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 346 | regs->al = regs->ch; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 347 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Set CMOS Date |
| 351 | static void |
| 352 | handle_1a05(struct bregs *regs) |
| 353 | { |
| 354 | // Using a debugger, I notice the following masking/setting |
| 355 | // of bits in Status Register B, by setting Reg B to |
| 356 | // a few values and getting its value after INT 1A was called. |
| 357 | // |
| 358 | // try#1 try#2 try#3 try#4 |
| 359 | // before 1111 1101 0111 1101 0000 0010 0000 0000 |
| 360 | // after 0110 1101 0111 1101 0000 0010 0000 0000 |
| 361 | // |
| 362 | // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1 |
| 363 | // My assumption: RegB = (RegB & 01111111b) |
| 364 | if (rtc_updating()) { |
| 365 | init_rtc(); |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 366 | set_invalid(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | outb_cmos(regs->cl, CMOS_RTC_YEAR); |
| 370 | outb_cmos(regs->dh, CMOS_RTC_MONTH); |
| 371 | outb_cmos(regs->dl, CMOS_RTC_DAY_MONTH); |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 372 | if (!CONFIG_COREBOOT) |
| 373 | outb_cmos(regs->ch, CMOS_CENTURY); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 374 | // clear halt-clock bit |
| 375 | u8 val8 = inb_cmos(CMOS_STATUS_B) & ~RTC_B_SET; |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 376 | outb_cmos(val8, CMOS_STATUS_B); |
| 377 | regs->ah = 0; |
| 378 | regs->al = val8; // AL = val last written to Reg B |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 379 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Set Alarm Time in CMOS |
| 383 | static void |
| 384 | handle_1a06(struct bregs *regs) |
| 385 | { |
| 386 | // Using a debugger, I notice the following masking/setting |
| 387 | // of bits in Status Register B, by setting Reg B to |
| 388 | // a few values and getting its value after INT 1A was called. |
| 389 | // |
| 390 | // try#1 try#2 try#3 |
| 391 | // before 1101 1111 0101 1111 0000 0000 |
| 392 | // after 0110 1111 0111 1111 0010 0000 |
| 393 | // |
| 394 | // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1 |
| 395 | // My assumption: RegB = ((RegB & 01111111b) | 00100000b) |
| 396 | u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B |
| 397 | regs->ax = 0; |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 398 | if (val8 & RTC_B_AIE) { |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 399 | // Alarm interrupt enabled already |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 400 | set_invalid(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | if (rtc_updating()) { |
| 404 | init_rtc(); |
| 405 | // fall through as if an update were not in progress |
| 406 | } |
| 407 | outb_cmos(regs->dh, CMOS_RTC_SECONDS_ALARM); |
| 408 | outb_cmos(regs->cl, CMOS_RTC_MINUTES_ALARM); |
| 409 | outb_cmos(regs->ch, CMOS_RTC_HOURS_ALARM); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 410 | // enable Status Reg B alarm bit, clear halt clock bit |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 411 | outb_cmos((val8 & ~RTC_B_SET) | RTC_B_AIE, CMOS_STATUS_B); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 412 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Turn off Alarm |
| 416 | static void |
| 417 | handle_1a07(struct bregs *regs) |
| 418 | { |
| 419 | // Using a debugger, I notice the following masking/setting |
| 420 | // of bits in Status Register B, by setting Reg B to |
| 421 | // a few values and getting its value after INT 1A was called. |
| 422 | // |
| 423 | // try#1 try#2 try#3 try#4 |
| 424 | // before 1111 1101 0111 1101 0010 0000 0010 0010 |
| 425 | // after 0100 0101 0101 0101 0000 0000 0000 0010 |
| 426 | // |
| 427 | // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1 |
| 428 | // My assumption: RegB = (RegB & 01010111b) |
| 429 | u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B |
| 430 | // clear clock-halt bit, disable alarm bit |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 431 | outb_cmos(val8 & ~(RTC_B_SET|RTC_B_AIE), CMOS_STATUS_B); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 432 | regs->ah = 0; |
| 433 | regs->al = val8; // val last written to Reg B |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 434 | set_success(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 437 | // Unsupported |
| 438 | static void |
| 439 | handle_1aXX(struct bregs *regs) |
| 440 | { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 441 | set_unimplemented(regs); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 442 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 443 | |
| 444 | // INT 1Ah Time-of-day Service Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 445 | void VISIBLE16 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 446 | handle_1a(struct bregs *regs) |
| 447 | { |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 448 | debug_enter(regs, DEBUG_HDL_1a); |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 449 | switch (regs->ah) { |
| 450 | case 0x00: handle_1a00(regs); break; |
| 451 | case 0x01: handle_1a01(regs); break; |
| 452 | case 0x02: handle_1a02(regs); break; |
| 453 | case 0x03: handle_1a03(regs); break; |
| 454 | case 0x04: handle_1a04(regs); break; |
| 455 | case 0x05: handle_1a05(regs); break; |
| 456 | case 0x06: handle_1a06(regs); break; |
| 457 | case 0x07: handle_1a07(regs); break; |
| 458 | case 0xb1: handle_1ab1(regs); break; |
| 459 | default: handle_1aXX(regs); break; |
| 460 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 461 | } |
| 462 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 463 | // INT 08h System Timer ISR Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 464 | void VISIBLE16 |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 465 | handle_08(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 466 | { |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 467 | debug_isr(DEBUG_ISR_08); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 468 | |
| 469 | floppy_tick(); |
| 470 | |
| 471 | u32 counter = GET_BDA(timer_counter); |
| 472 | counter++; |
| 473 | // compare to one days worth of timer ticks at 18.2 hz |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 474 | if (counter >= TICKS_PER_DAY) { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 475 | // there has been a midnight rollover at this point |
| 476 | counter = 0; |
| 477 | SET_BDA(timer_rollover, GET_BDA(timer_rollover) + 1); |
| 478 | } |
| 479 | |
| 480 | SET_BDA(timer_counter, counter); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 481 | |
Kevin O'Connor | 0e88576 | 2010-05-01 22:14:40 -0400 | [diff] [blame] | 482 | usb_check_event(); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 483 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 484 | // chain to user timer tick INT #0x1c |
Kevin O'Connor | a83ff55 | 2009-01-01 21:00:59 -0500 | [diff] [blame] | 485 | u32 eax=0, flags; |
| 486 | call16_simpint(0x1c, &eax, &flags); |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 487 | |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 488 | eoi_pic1(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 489 | } |
| 490 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 491 | |
| 492 | /**************************************************************** |
| 493 | * Periodic timer |
| 494 | ****************************************************************/ |
| 495 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 496 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 497 | useRTC(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 498 | { |
| 499 | u16 ebda_seg = get_ebda_seg(); |
| 500 | int count = GET_EBDA2(ebda_seg, RTCusers); |
| 501 | SET_EBDA2(ebda_seg, RTCusers, count+1); |
| 502 | if (count) |
| 503 | return; |
| 504 | // Turn on the Periodic Interrupt timer |
| 505 | u8 bRegister = inb_cmos(CMOS_STATUS_B); |
| 506 | outb_cmos(bRegister | RTC_B_PIE, CMOS_STATUS_B); |
| 507 | } |
| 508 | |
| 509 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 510 | releaseRTC(void) |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 511 | { |
| 512 | u16 ebda_seg = get_ebda_seg(); |
| 513 | int count = GET_EBDA2(ebda_seg, RTCusers); |
| 514 | SET_EBDA2(ebda_seg, RTCusers, count-1); |
| 515 | if (count != 1) |
| 516 | return; |
| 517 | // Clear the Periodic Interrupt. |
| 518 | u8 bRegister = inb_cmos(CMOS_STATUS_B); |
| 519 | outb_cmos(bRegister & ~RTC_B_PIE, CMOS_STATUS_B); |
| 520 | } |
| 521 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 522 | static int |
Kevin O'Connor | 72743f1 | 2008-05-24 23:04:09 -0400 | [diff] [blame] | 523 | set_usertimer(u32 usecs, u16 seg, u16 offset) |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 524 | { |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 525 | if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING) |
| 526 | return -1; |
| 527 | |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 528 | // Interval not already set. |
| 529 | SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING); // Set status byte. |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 530 | SET_BDA(user_wait_complete_flag, SEGOFF(seg, offset)); |
Kevin O'Connor | 72743f1 | 2008-05-24 23:04:09 -0400 | [diff] [blame] | 531 | SET_BDA(user_wait_timeout, usecs); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 532 | useRTC(); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 537 | clear_usertimer(void) |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 538 | { |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 539 | if (!(GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING)) |
| 540 | return; |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 541 | // Turn off status byte. |
| 542 | SET_BDA(rtc_wait_flag, 0); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 543 | releaseRTC(); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 546 | #define RET_ECLOCKINUSE 0x83 |
| 547 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 548 | // Wait for CX:DX microseconds |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 549 | void |
| 550 | handle_1586(struct bregs *regs) |
| 551 | { |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 552 | // Use the rtc to wait for the specified time. |
| 553 | u8 statusflag = 0; |
| 554 | u32 count = (regs->cx << 16) | regs->dx; |
| 555 | int ret = set_usertimer(count, GET_SEG(SS), (u32)&statusflag); |
| 556 | if (ret) { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 557 | set_code_invalid(regs, RET_ECLOCKINUSE); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 558 | return; |
| 559 | } |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 560 | while (!statusflag) |
Kevin O'Connor | ee2efa7 | 2009-09-20 15:33:08 -0400 | [diff] [blame] | 561 | wait_irq(); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 562 | set_success(regs); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Set Interval requested. |
| 566 | static void |
| 567 | handle_158300(struct bregs *regs) |
| 568 | { |
| 569 | int ret = set_usertimer((regs->cx << 16) | regs->dx, regs->es, regs->bx); |
| 570 | if (ret) |
| 571 | // Interval already set. |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 572 | set_code_invalid(regs, RET_EUNSUPPORTED); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 573 | else |
| 574 | set_success(regs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Clear interval requested |
| 578 | static void |
| 579 | handle_158301(struct bregs *regs) |
| 580 | { |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 581 | clear_usertimer(); |
| 582 | set_success(regs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static void |
| 586 | handle_1583XX(struct bregs *regs) |
| 587 | { |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 588 | set_code_unimplemented(regs, RET_EUNSUPPORTED); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 589 | regs->al--; |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void |
| 593 | handle_1583(struct bregs *regs) |
| 594 | { |
| 595 | switch (regs->al) { |
| 596 | case 0x00: handle_158300(regs); break; |
| 597 | case 0x01: handle_158301(regs); break; |
| 598 | default: handle_1583XX(regs); break; |
| 599 | } |
| 600 | } |
| 601 | |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 602 | #define USEC_PER_RTC DIV_ROUND_CLOSEST(1000000, 1024) |
| 603 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 604 | // int70h: IRQ8 - CMOS RTC |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 605 | void VISIBLE16 |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 606 | handle_70(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 607 | { |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 608 | debug_isr(DEBUG_ISR_70); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 609 | |
| 610 | // Check which modes are enabled and have occurred. |
| 611 | u8 registerB = inb_cmos(CMOS_STATUS_B); |
| 612 | u8 registerC = inb_cmos(CMOS_STATUS_C); |
| 613 | |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 614 | if (!(registerB & (RTC_B_PIE|RTC_B_AIE))) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 615 | goto done; |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 616 | if (registerC & RTC_B_AIE) { |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 617 | // Handle Alarm Interrupt. |
Kevin O'Connor | a83ff55 | 2009-01-01 21:00:59 -0500 | [diff] [blame] | 618 | u32 eax=0, flags; |
| 619 | call16_simpint(0x4a, &eax, &flags); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 620 | } |
Kevin O'Connor | f358759 | 2009-02-15 13:02:56 -0500 | [diff] [blame] | 621 | if (!(registerC & RTC_B_PIE)) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 622 | goto done; |
| 623 | |
| 624 | // Handle Periodic Interrupt. |
| 625 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 626 | check_preempt(); |
| 627 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 628 | if (!GET_BDA(rtc_wait_flag)) |
| 629 | goto done; |
| 630 | |
| 631 | // Wait Interval (Int 15, AH=83) active. |
| 632 | u32 time = GET_BDA(user_wait_timeout); // Time left in microseconds. |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 633 | if (time < USEC_PER_RTC) { |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 634 | // Done waiting - write to specified flag byte. |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 635 | struct segoff_s segoff = GET_BDA(user_wait_complete_flag); |
| 636 | u16 ptr_seg = segoff.seg; |
| 637 | u8 *ptr_far = (u8*)(segoff.offset+0); |
| 638 | u8 oldval = GET_FARVAR(ptr_seg, *ptr_far); |
| 639 | SET_FARVAR(ptr_seg, *ptr_far, oldval | 0x80); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 640 | |
| 641 | clear_usertimer(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 642 | } else { |
| 643 | // Continue waiting. |
Kevin O'Connor | 6aee52d | 2009-09-27 20:07:40 -0400 | [diff] [blame] | 644 | time -= USEC_PER_RTC; |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 645 | SET_BDA(user_wait_timeout, time); |
| 646 | } |
| 647 | |
| 648 | done: |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 649 | eoi_pic2(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 650 | } |