blob: e8a48a13c1733bdd20b3f176dc829eeaabc1a6df [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to handle system clocks.
2//
Kevin O'Connorabf31d32010-07-26 22:33:54 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#include "util.h" // debug_enter
10#include "disk.h" // floppy_tick
Kevin O'Connor4b60c002008-02-25 22:29:55 -050011#include "cmos.h" // inb_cmos
Kevin O'Connord21c0892008-11-26 17:02:43 -050012#include "pic.h" // eoi_pic1
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "bregs.h" // struct bregs
Kevin O'Connor15157a32008-12-13 11:10:37 -050014#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor0e885762010-05-01 22:14:40 -040015#include "usb-hid.h" // usb_check_event
Kevin O'Connor4b60c002008-02-25 22:29:55 -050016
Kevin O'Connor5be04902008-05-18 17:12:06 -040017// RTC register flags
18#define RTC_A_UIP 0x80
Kevin O'Connorf3587592009-02-15 13:02:56 -050019
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'Connor5be04902008-05-18 17:12:06 -040028
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050029// 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)
Kevin O'Connor745de852012-01-29 14:15:14 -050051#define PM_READ_COUNTER0 (1<<1)
52#define PM_READ_COUNTER1 (1<<2)
53#define PM_READ_COUNTER2 (1<<3)
54#define PM_READ_STATUSVALUE (0<<4)
55#define PM_READ_VALUE (1<<4)
56#define PM_READ_STATUS (2<<4)
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050057
58
59/****************************************************************
60 * TSC timer
61 ****************************************************************/
62
Kevin O'Connor6aee52d2009-09-27 20:07:40 -040063#define CALIBRATE_COUNT 0x800 // Approx 1.7ms
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050064
Kevin O'Connor372e0712009-09-09 09:51:31 -040065u32 cpu_khz VAR16VISIBLE;
Kevin O'Connor745de852012-01-29 14:15:14 -050066u8 no_tsc VAR16VISIBLE;
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050067
68static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050069calibrate_tsc(void)
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050070{
Kevin O'Connor745de852012-01-29 14:15:14 -050071 u32 eax, ebx, ecx, edx, cpuid_features = 0;
72 cpuid(0, &eax, &ebx, &ecx, &edx);
73 if (eax > 0)
74 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
75
76 if (!(cpuid_features & CPUID_TSC)) {
77 SET_GLOBAL(no_tsc, 1);
78 SET_GLOBAL(cpu_khz, PIT_TICK_RATE / 1000);
79 dprintf(3, "386/486 class CPU. Using TSC emulation\n");
80 return;
81 }
82
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050083 // Setup "timer2"
84 u8 orig = inb(PORT_PS2_CTRLB);
85 outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
86 /* binary, mode 0, LSB/MSB, Ch 2 */
87 outb(PM_SEL_TIMER2|PM_ACCESS_WORD|PM_MODE0|PM_CNT_BINARY, PORT_PIT_MODE);
88 /* LSB of ticks */
89 outb(CALIBRATE_COUNT & 0xFF, PORT_PIT_COUNTER2);
90 /* MSB of ticks */
91 outb(CALIBRATE_COUNT >> 8, PORT_PIT_COUNTER2);
92
93 u64 start = rdtscll();
94 while ((inb(PORT_PS2_CTRLB) & PPCB_T2OUT) == 0)
95 ;
96 u64 end = rdtscll();
97
98 // Restore PORT_PS2_CTRLB
99 outb(orig, PORT_PS2_CTRLB);
100
101 // Store calibrated cpu khz.
102 u64 diff = end - start;
103 dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n"
104 , (u32)start, (u32)end, (u32)diff);
105 u32 hz = diff * PIT_TICK_RATE / CALIBRATE_COUNT;
Kevin O'Connor15157a32008-12-13 11:10:37 -0500106 SET_GLOBAL(cpu_khz, hz / 1000);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500107
108 dprintf(1, "CPU Mhz=%u\n", hz / 1000000);
109}
110
Kevin O'Connor745de852012-01-29 14:15:14 -0500111static u64
112emulate_tsc(void)
113{
114 int cnt, d;
115 u16 ebda_seg = get_ebda_seg();
116 u64 ret;
117 /* read timer 0 current count */
118 ret = GET_EBDA2(ebda_seg, tsc_8254);
119 /* readback mode has slightly shifted registers, works on all 8254, readback PIT0 latch */
120 outb(PM_SEL_READBACK | PM_READ_VALUE | PM_READ_COUNTER0, PORT_PIT_MODE);
121 cnt = (inb(PORT_PIT_COUNTER0) | (inb(PORT_PIT_COUNTER0) << 8));
122 d = GET_EBDA2(ebda_seg, last_tsc_8254) - cnt;
123 /* Determine the ticks count from last invocation of this function */
124 ret += (d > 0) ? d : (PIT_TICK_INTERVAL + d);
125 SET_EBDA2(ebda_seg, last_tsc_8254, cnt);
126 SET_EBDA2(ebda_seg, tsc_8254, ret);
127 return ret;
128}
129
130static u64
131get_tsc(void)
132{
133 if (unlikely(GET_GLOBAL(no_tsc)))
134 return emulate_tsc();
135 return rdtscll();
136}
137
138int
139check_tsc(u64 end)
140{
141 return (s64)(get_tsc() - end) > 0;
142}
143
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500144static void
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400145tscdelay(u64 diff)
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500146{
Kevin O'Connor745de852012-01-29 14:15:14 -0500147 u64 start = get_tsc();
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500148 u64 end = start + diff;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400149 while (!check_tsc(end))
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500150 cpu_relax();
151}
152
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400153static void
154tscsleep(u64 diff)
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500155{
Kevin O'Connor745de852012-01-29 14:15:14 -0500156 u64 start = get_tsc();
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400157 u64 end = start + diff;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400158 while (!check_tsc(end))
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400159 yield();
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500160}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400161
162void ndelay(u32 count) {
163 tscdelay(count * GET_GLOBAL(cpu_khz) / 1000000);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500164}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400165void udelay(u32 count) {
166 tscdelay(count * GET_GLOBAL(cpu_khz) / 1000);
167}
168void mdelay(u32 count) {
169 tscdelay(count * GET_GLOBAL(cpu_khz));
170}
171
172void nsleep(u32 count) {
173 tscsleep(count * GET_GLOBAL(cpu_khz) / 1000000);
174}
175void usleep(u32 count) {
176 tscsleep(count * GET_GLOBAL(cpu_khz) / 1000);
177}
178void msleep(u32 count) {
179 tscsleep(count * GET_GLOBAL(cpu_khz));
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500180}
181
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500182// Return the TSC value that is 'msecs' time in the future.
183u64
184calc_future_tsc(u32 msecs)
185{
Kevin O'Connor15157a32008-12-13 11:10:37 -0500186 u32 khz = GET_GLOBAL(cpu_khz);
Kevin O'Connor745de852012-01-29 14:15:14 -0500187 return get_tsc() + ((u64)khz * msecs);
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500188}
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400189u64
190calc_future_tsc_usec(u32 usecs)
191{
192 u32 khz = GET_GLOBAL(cpu_khz);
Kevin O'Connor745de852012-01-29 14:15:14 -0500193 return get_tsc() + ((u64)(khz/1000) * usecs);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400194}
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500195
Kevin O'Connor5be04902008-05-18 17:12:06 -0400196
197/****************************************************************
198 * Init
199 ****************************************************************/
200
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500201static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500202rtc_updating(void)
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500203{
204 // This function checks to see if the update-in-progress bit
205 // is set in CMOS Status Register A. If not, it returns 0.
206 // If it is set, it tries to wait until there is a transition
207 // to 0, and will return 0 if such a transition occurs. A -1
208 // is returned only after timing out. The maximum period
Kevin O'Connor4f5586c2009-02-16 10:14:10 -0500209 // that this bit should be set is constrained to (1984+244)
Kevin O'Connor11cc6622010-03-13 23:04:41 -0500210 // useconds, but we wait for longer just to be sure.
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500211
Kevin O'Connorf3587592009-02-15 13:02:56 -0500212 if ((inb_cmos(CMOS_STATUS_A) & RTC_A_UIP) == 0)
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500213 return 0;
Kevin O'Connor11cc6622010-03-13 23:04:41 -0500214 u64 end = calc_future_tsc(15);
215 for (;;) {
Kevin O'Connorf3587592009-02-15 13:02:56 -0500216 if ((inb_cmos(CMOS_STATUS_A) & RTC_A_UIP) == 0)
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500217 return 0;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400218 if (check_tsc(end))
Kevin O'Connor11cc6622010-03-13 23:04:41 -0500219 // update-in-progress never transitioned to 0
220 return -1;
221 yield();
222 }
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500223}
224
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500225static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500226pit_setup(void)
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400227{
228 // timer0: binary count, 16bit count, mode 2
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500229 outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE);
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400230 // maximum count of 0000H = 18.2Hz
231 outb(0x0, PORT_PIT_COUNTER0);
232 outb(0x0, PORT_PIT_COUNTER0);
233}
234
Kevin O'Connorf3587592009-02-15 13:02:56 -0500235static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500236init_rtc(void)
Kevin O'Connorf3587592009-02-15 13:02:56 -0500237{
Kevin O'Connor4f5586c2009-02-16 10:14:10 -0500238 outb_cmos(0x26, CMOS_STATUS_A); // 32,768Khz src, 976.5625us updates
Kevin O'Connorf3587592009-02-15 13:02:56 -0500239 u8 regB = inb_cmos(CMOS_STATUS_B);
240 outb_cmos((regB & RTC_B_DSE) | RTC_B_24HR, CMOS_STATUS_B);
241 inb_cmos(CMOS_STATUS_C);
242 inb_cmos(CMOS_STATUS_D);
243}
244
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400245static u32
246bcd2bin(u8 val)
247{
248 return (val & 0xf) + ((val >> 4) * 10);
249}
250
251void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500252timer_setup(void)
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400253{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400254 dprintf(3, "init timer\n");
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500255 calibrate_tsc();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400256 pit_setup();
257
Kevin O'Connorf3587592009-02-15 13:02:56 -0500258 init_rtc();
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500259 rtc_updating();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400260 u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400261 u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400262 u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400263 u32 ticks = (hours * 60 + minutes) * 60 + seconds;
264 ticks = ((u64)ticks * PIT_TICK_RATE) / PIT_TICK_INTERVAL;
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400265 SET_BDA(timer_counter, ticks);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400266
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -0400267 enable_hwirq(0, FUNC16(entry_08));
268 enable_hwirq(8, FUNC16(entry_70));
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400269}
270
Kevin O'Connor5be04902008-05-18 17:12:06 -0400271
272/****************************************************************
273 * Standard clock functions
274 ****************************************************************/
275
Kevin O'Connorb5cc2ca2010-05-23 11:38:53 -0400276#define TICKS_PER_DAY (u32)((u64)60*60*24*PIT_TICK_RATE / PIT_TICK_INTERVAL)
277
278// Calculate the timer value at 'count' number of full timer ticks in
279// the future.
280u32
281calc_future_timer_ticks(u32 count)
282{
283 return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
284}
Kevin O'Connorbb685912010-05-23 12:40:40 -0400285
Kevin O'Connorb5cc2ca2010-05-23 11:38:53 -0400286// Return the timer value that is 'msecs' time in the future.
287u32
288calc_future_timer(u32 msecs)
289{
Kevin O'Connorbb685912010-05-23 12:40:40 -0400290 if (!msecs)
291 return GET_BDA(timer_counter);
Kevin O'Connorabf31d32010-07-26 22:33:54 -0400292 u32 kticks = DIV_ROUND_UP((u64)msecs * PIT_TICK_RATE, PIT_TICK_INTERVAL);
Kevin O'Connorb5cc2ca2010-05-23 11:38:53 -0400293 u32 ticks = DIV_ROUND_UP(kticks, 1000);
294 return calc_future_timer_ticks(ticks);
295}
Kevin O'Connorbb685912010-05-23 12:40:40 -0400296
Kevin O'Connorb5cc2ca2010-05-23 11:38:53 -0400297// Check if the given timer value has passed.
298int
299check_timer(u32 end)
300{
301 return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
302 < (TICKS_PER_DAY/2));
303}
304
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500305// get current clock count
306static void
307handle_1a00(struct bregs *regs)
308{
Kevin O'Connor68c51392010-03-13 22:23:44 -0500309 yield();
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500310 u32 ticks = GET_BDA(timer_counter);
311 regs->cx = ticks >> 16;
312 regs->dx = ticks;
313 regs->al = GET_BDA(timer_rollover);
314 SET_BDA(timer_rollover, 0); // reset flag
Kevin O'Connor6c781222008-03-09 12:19:23 -0400315 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500316}
317
318// Set Current Clock Count
319static void
320handle_1a01(struct bregs *regs)
321{
322 u32 ticks = (regs->cx << 16) | regs->dx;
323 SET_BDA(timer_counter, ticks);
324 SET_BDA(timer_rollover, 0); // reset flag
Kevin O'Connor15157a32008-12-13 11:10:37 -0500325 // XXX - should use set_code_success()?
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500326 regs->ah = 0;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400327 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500328}
329
330// Read CMOS Time
331static void
332handle_1a02(struct bregs *regs)
333{
334 if (rtc_updating()) {
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500335 set_invalid(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500336 return;
337 }
338
339 regs->dh = inb_cmos(CMOS_RTC_SECONDS);
340 regs->cl = inb_cmos(CMOS_RTC_MINUTES);
341 regs->ch = inb_cmos(CMOS_RTC_HOURS);
Kevin O'Connorf3587592009-02-15 13:02:56 -0500342 regs->dl = inb_cmos(CMOS_STATUS_B) & RTC_B_DSE;
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500343 regs->ah = 0;
344 regs->al = regs->ch;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400345 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500346}
347
348// Set CMOS Time
349static void
350handle_1a03(struct bregs *regs)
351{
352 // Using a debugger, I notice the following masking/setting
353 // of bits in Status Register B, by setting Reg B to
354 // a few values and getting its value after INT 1A was called.
355 //
356 // try#1 try#2 try#3
357 // before 1111 1101 0111 1101 0000 0000
358 // after 0110 0010 0110 0010 0000 0010
359 //
360 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
361 // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
362 if (rtc_updating()) {
363 init_rtc();
364 // fall through as if an update were not in progress
365 }
366 outb_cmos(regs->dh, CMOS_RTC_SECONDS);
367 outb_cmos(regs->cl, CMOS_RTC_MINUTES);
368 outb_cmos(regs->ch, CMOS_RTC_HOURS);
369 // Set Daylight Savings time enabled bit to requested value
Kevin O'Connorf3587592009-02-15 13:02:56 -0500370 u8 val8 = ((inb_cmos(CMOS_STATUS_B) & (RTC_B_PIE|RTC_B_AIE))
371 | RTC_B_24HR | (regs->dl & RTC_B_DSE));
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500372 outb_cmos(val8, CMOS_STATUS_B);
373 regs->ah = 0;
374 regs->al = val8; // val last written to Reg B
Kevin O'Connor6c781222008-03-09 12:19:23 -0400375 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500376}
377
378// Read CMOS Date
379static void
380handle_1a04(struct bregs *regs)
381{
382 regs->ah = 0;
383 if (rtc_updating()) {
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500384 set_invalid(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500385 return;
386 }
387 regs->cl = inb_cmos(CMOS_RTC_YEAR);
388 regs->dh = inb_cmos(CMOS_RTC_MONTH);
389 regs->dl = inb_cmos(CMOS_RTC_DAY_MONTH);
Kevin O'Connorf3587592009-02-15 13:02:56 -0500390 if (CONFIG_COREBOOT) {
391 if (regs->cl > 0x80)
392 regs->ch = 0x19;
393 else
394 regs->ch = 0x20;
395 } else {
396 regs->ch = inb_cmos(CMOS_CENTURY);
397 }
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500398 regs->al = regs->ch;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400399 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500400}
401
402// Set CMOS Date
403static void
404handle_1a05(struct bregs *regs)
405{
406 // Using a debugger, I notice the following masking/setting
407 // of bits in Status Register B, by setting Reg B to
408 // a few values and getting its value after INT 1A was called.
409 //
410 // try#1 try#2 try#3 try#4
411 // before 1111 1101 0111 1101 0000 0010 0000 0000
412 // after 0110 1101 0111 1101 0000 0010 0000 0000
413 //
414 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
415 // My assumption: RegB = (RegB & 01111111b)
416 if (rtc_updating()) {
417 init_rtc();
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500418 set_invalid(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500419 return;
420 }
421 outb_cmos(regs->cl, CMOS_RTC_YEAR);
422 outb_cmos(regs->dh, CMOS_RTC_MONTH);
423 outb_cmos(regs->dl, CMOS_RTC_DAY_MONTH);
Kevin O'Connorf3587592009-02-15 13:02:56 -0500424 if (!CONFIG_COREBOOT)
425 outb_cmos(regs->ch, CMOS_CENTURY);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400426 // clear halt-clock bit
427 u8 val8 = inb_cmos(CMOS_STATUS_B) & ~RTC_B_SET;
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500428 outb_cmos(val8, CMOS_STATUS_B);
429 regs->ah = 0;
430 regs->al = val8; // AL = val last written to Reg B
Kevin O'Connor6c781222008-03-09 12:19:23 -0400431 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500432}
433
434// Set Alarm Time in CMOS
435static void
436handle_1a06(struct bregs *regs)
437{
438 // Using a debugger, I notice the following masking/setting
439 // of bits in Status Register B, by setting Reg B to
440 // a few values and getting its value after INT 1A was called.
441 //
442 // try#1 try#2 try#3
443 // before 1101 1111 0101 1111 0000 0000
444 // after 0110 1111 0111 1111 0010 0000
445 //
446 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
447 // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
448 u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
449 regs->ax = 0;
Kevin O'Connorf3587592009-02-15 13:02:56 -0500450 if (val8 & RTC_B_AIE) {
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500451 // Alarm interrupt enabled already
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500452 set_invalid(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500453 return;
454 }
455 if (rtc_updating()) {
456 init_rtc();
457 // fall through as if an update were not in progress
458 }
459 outb_cmos(regs->dh, CMOS_RTC_SECONDS_ALARM);
460 outb_cmos(regs->cl, CMOS_RTC_MINUTES_ALARM);
461 outb_cmos(regs->ch, CMOS_RTC_HOURS_ALARM);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500462 // enable Status Reg B alarm bit, clear halt clock bit
Kevin O'Connor5be04902008-05-18 17:12:06 -0400463 outb_cmos((val8 & ~RTC_B_SET) | RTC_B_AIE, CMOS_STATUS_B);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400464 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500465}
466
467// Turn off Alarm
468static void
469handle_1a07(struct bregs *regs)
470{
471 // Using a debugger, I notice the following masking/setting
472 // of bits in Status Register B, by setting Reg B to
473 // a few values and getting its value after INT 1A was called.
474 //
475 // try#1 try#2 try#3 try#4
476 // before 1111 1101 0111 1101 0010 0000 0010 0010
477 // after 0100 0101 0101 0101 0000 0000 0000 0010
478 //
479 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
480 // My assumption: RegB = (RegB & 01010111b)
481 u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
482 // clear clock-halt bit, disable alarm bit
Kevin O'Connor5be04902008-05-18 17:12:06 -0400483 outb_cmos(val8 & ~(RTC_B_SET|RTC_B_AIE), CMOS_STATUS_B);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500484 regs->ah = 0;
485 regs->al = val8; // val last written to Reg B
Kevin O'Connor6c781222008-03-09 12:19:23 -0400486 set_success(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500487}
488
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500489// Unsupported
490static void
491handle_1aXX(struct bregs *regs)
492{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500493 set_unimplemented(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500494}
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500495
496// INT 1Ah Time-of-day Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500497void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500498handle_1a(struct bregs *regs)
499{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400500 debug_enter(regs, DEBUG_HDL_1a);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500501 switch (regs->ah) {
502 case 0x00: handle_1a00(regs); break;
503 case 0x01: handle_1a01(regs); break;
504 case 0x02: handle_1a02(regs); break;
505 case 0x03: handle_1a03(regs); break;
506 case 0x04: handle_1a04(regs); break;
507 case 0x05: handle_1a05(regs); break;
508 case 0x06: handle_1a06(regs); break;
509 case 0x07: handle_1a07(regs); break;
510 case 0xb1: handle_1ab1(regs); break;
511 default: handle_1aXX(regs); break;
512 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500513}
514
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500515// INT 08h System Timer ISR Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500516void VISIBLE16
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500517handle_08(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500518{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400519 debug_isr(DEBUG_ISR_08);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500520
521 floppy_tick();
522
523 u32 counter = GET_BDA(timer_counter);
524 counter++;
525 // compare to one days worth of timer ticks at 18.2 hz
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400526 if (counter >= TICKS_PER_DAY) {
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500527 // there has been a midnight rollover at this point
528 counter = 0;
529 SET_BDA(timer_rollover, GET_BDA(timer_rollover) + 1);
530 }
531
532 SET_BDA(timer_counter, counter);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500533
Kevin O'Connor0e885762010-05-01 22:14:40 -0400534 usb_check_event();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400535
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500536 // chain to user timer tick INT #0x1c
Kevin O'Connora83ff552009-01-01 21:00:59 -0500537 u32 eax=0, flags;
538 call16_simpint(0x1c, &eax, &flags);
Kevin O'Connored128492008-03-11 11:14:59 -0400539
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400540 eoi_pic1();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500541}
542
Kevin O'Connor5be04902008-05-18 17:12:06 -0400543
544/****************************************************************
545 * Periodic timer
546 ****************************************************************/
547
Kevin O'Connorad901592009-12-13 11:25:25 -0500548void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500549useRTC(void)
Kevin O'Connorad901592009-12-13 11:25:25 -0500550{
551 u16 ebda_seg = get_ebda_seg();
552 int count = GET_EBDA2(ebda_seg, RTCusers);
553 SET_EBDA2(ebda_seg, RTCusers, count+1);
554 if (count)
555 return;
556 // Turn on the Periodic Interrupt timer
557 u8 bRegister = inb_cmos(CMOS_STATUS_B);
558 outb_cmos(bRegister | RTC_B_PIE, CMOS_STATUS_B);
559}
560
561void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500562releaseRTC(void)
Kevin O'Connorad901592009-12-13 11:25:25 -0500563{
564 u16 ebda_seg = get_ebda_seg();
565 int count = GET_EBDA2(ebda_seg, RTCusers);
566 SET_EBDA2(ebda_seg, RTCusers, count-1);
567 if (count != 1)
568 return;
569 // Clear the Periodic Interrupt.
570 u8 bRegister = inb_cmos(CMOS_STATUS_B);
571 outb_cmos(bRegister & ~RTC_B_PIE, CMOS_STATUS_B);
572}
573
Kevin O'Connor5be04902008-05-18 17:12:06 -0400574static int
Kevin O'Connor72743f12008-05-24 23:04:09 -0400575set_usertimer(u32 usecs, u16 seg, u16 offset)
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500576{
Kevin O'Connor5be04902008-05-18 17:12:06 -0400577 if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING)
578 return -1;
579
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500580 // Interval not already set.
581 SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING); // Set status byte.
Kevin O'Connor9f985422009-09-09 11:34:39 -0400582 SET_BDA(user_wait_complete_flag, SEGOFF(seg, offset));
Kevin O'Connor72743f12008-05-24 23:04:09 -0400583 SET_BDA(user_wait_timeout, usecs);
Kevin O'Connorad901592009-12-13 11:25:25 -0500584 useRTC();
Kevin O'Connor5be04902008-05-18 17:12:06 -0400585 return 0;
586}
587
588static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500589clear_usertimer(void)
Kevin O'Connor5be04902008-05-18 17:12:06 -0400590{
Kevin O'Connorad901592009-12-13 11:25:25 -0500591 if (!(GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING))
592 return;
Kevin O'Connor5be04902008-05-18 17:12:06 -0400593 // Turn off status byte.
594 SET_BDA(rtc_wait_flag, 0);
Kevin O'Connorad901592009-12-13 11:25:25 -0500595 releaseRTC();
Kevin O'Connor5be04902008-05-18 17:12:06 -0400596}
597
Kevin O'Connor5be04902008-05-18 17:12:06 -0400598#define RET_ECLOCKINUSE 0x83
599
Kevin O'Connord21c0892008-11-26 17:02:43 -0500600// Wait for CX:DX microseconds
Kevin O'Connor5be04902008-05-18 17:12:06 -0400601void
602handle_1586(struct bregs *regs)
603{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500604 // Use the rtc to wait for the specified time.
605 u8 statusflag = 0;
606 u32 count = (regs->cx << 16) | regs->dx;
607 int ret = set_usertimer(count, GET_SEG(SS), (u32)&statusflag);
608 if (ret) {
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500609 set_code_invalid(regs, RET_ECLOCKINUSE);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500610 return;
611 }
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500612 while (!statusflag)
Kevin O'Connoree2efa72009-09-20 15:33:08 -0400613 wait_irq();
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500614 set_success(regs);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400615}
616
617// Set Interval requested.
618static void
619handle_158300(struct bregs *regs)
620{
621 int ret = set_usertimer((regs->cx << 16) | regs->dx, regs->es, regs->bx);
622 if (ret)
623 // Interval already set.
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500624 set_code_invalid(regs, RET_EUNSUPPORTED);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400625 else
626 set_success(regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500627}
628
629// Clear interval requested
630static void
631handle_158301(struct bregs *regs)
632{
Kevin O'Connor5be04902008-05-18 17:12:06 -0400633 clear_usertimer();
634 set_success(regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500635}
636
637static void
638handle_1583XX(struct bregs *regs)
639{
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500640 set_code_unimplemented(regs, RET_EUNSUPPORTED);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500641 regs->al--;
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500642}
643
644void
645handle_1583(struct bregs *regs)
646{
647 switch (regs->al) {
648 case 0x00: handle_158300(regs); break;
649 case 0x01: handle_158301(regs); break;
650 default: handle_1583XX(regs); break;
651 }
652}
653
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400654#define USEC_PER_RTC DIV_ROUND_CLOSEST(1000000, 1024)
655
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500656// int70h: IRQ8 - CMOS RTC
Kevin O'Connor19786762008-03-05 21:09:59 -0500657void VISIBLE16
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500658handle_70(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500659{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400660 debug_isr(DEBUG_ISR_70);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500661
662 // Check which modes are enabled and have occurred.
663 u8 registerB = inb_cmos(CMOS_STATUS_B);
664 u8 registerC = inb_cmos(CMOS_STATUS_C);
665
Kevin O'Connor5be04902008-05-18 17:12:06 -0400666 if (!(registerB & (RTC_B_PIE|RTC_B_AIE)))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500667 goto done;
Kevin O'Connorf3587592009-02-15 13:02:56 -0500668 if (registerC & RTC_B_AIE) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500669 // Handle Alarm Interrupt.
Kevin O'Connora83ff552009-01-01 21:00:59 -0500670 u32 eax=0, flags;
671 call16_simpint(0x4a, &eax, &flags);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500672 }
Kevin O'Connorf3587592009-02-15 13:02:56 -0500673 if (!(registerC & RTC_B_PIE))
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500674 goto done;
675
676 // Handle Periodic Interrupt.
677
Kevin O'Connorad901592009-12-13 11:25:25 -0500678 check_preempt();
679
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500680 if (!GET_BDA(rtc_wait_flag))
681 goto done;
682
683 // Wait Interval (Int 15, AH=83) active.
684 u32 time = GET_BDA(user_wait_timeout); // Time left in microseconds.
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400685 if (time < USEC_PER_RTC) {
Kevin O'Connor5be04902008-05-18 17:12:06 -0400686 // Done waiting - write to specified flag byte.
Kevin O'Connor9f985422009-09-09 11:34:39 -0400687 struct segoff_s segoff = GET_BDA(user_wait_complete_flag);
688 u16 ptr_seg = segoff.seg;
689 u8 *ptr_far = (u8*)(segoff.offset+0);
690 u8 oldval = GET_FARVAR(ptr_seg, *ptr_far);
691 SET_FARVAR(ptr_seg, *ptr_far, oldval | 0x80);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400692
693 clear_usertimer();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500694 } else {
695 // Continue waiting.
Kevin O'Connor6aee52d2009-09-27 20:07:40 -0400696 time -= USEC_PER_RTC;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500697 SET_BDA(user_wait_timeout, time);
698 }
699
700done:
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400701 eoi_pic2();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500702}