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