Improve some debugging output.
[seabios.git] / src / clock.c
1 // 16bit code to handle system clocks.
2 //
3 // Copyright (C) 2008  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 GPLv3 license.
7
8 #include "biosvar.h" // struct bregs
9 #include "util.h" // debug_enter
10 #include "disk.h" // floppy_tick
11 #include "cmos.h" // inb_cmos
12 #include "pic.h" // unmask_pic1
13
14 // RTC register flags
15 #define RTC_A_UIP 0x80
16 #define RTC_B_SET 0x80
17 #define RTC_B_PIE 0x40
18 #define RTC_B_AIE 0x20
19 #define RTC_B_UIE 0x10
20
21
22 /****************************************************************
23  * Init
24  ****************************************************************/
25
26 static void
27 pit_setup()
28 {
29     // timer0: binary count, 16bit count, mode 2
30     outb(0x34, PORT_PIT_MODE);
31     // maximum count of 0000H = 18.2Hz
32     outb(0x0, PORT_PIT_COUNTER0);
33     outb(0x0, PORT_PIT_COUNTER0);
34 }
35
36 static u32
37 bcd2bin(u8 val)
38 {
39     return (val & 0xf) + ((val >> 4) * 10);
40 }
41
42 void
43 timer_setup()
44 {
45     dprintf(3, "init timer\n");
46     pit_setup();
47
48     u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
49     u32 ticks = (seconds * 18206507) / 1000000;
50     u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
51     ticks += (minutes * 10923904) / 10000;
52     u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
53     ticks += (hours * 65543427) / 1000;
54     SET_BDA(timer_counter, ticks);
55     SET_BDA(timer_rollover, 0);
56
57     // Enable IRQ0 (handle_08)
58     unmask_pic1(PIC1_IRQ0);
59 }
60
61 static void
62 init_rtc()
63 {
64     outb_cmos(0x26, CMOS_STATUS_A);
65     outb_cmos(0x02, CMOS_STATUS_B);
66     inb_cmos(CMOS_STATUS_C);
67     inb_cmos(CMOS_STATUS_D);
68 }
69
70
71 /****************************************************************
72  * Standard clock functions
73  ****************************************************************/
74
75 static u8
76 rtc_updating()
77 {
78     // This function checks to see if the update-in-progress bit
79     // is set in CMOS Status Register A.  If not, it returns 0.
80     // If it is set, it tries to wait until there is a transition
81     // to 0, and will return 0 if such a transition occurs.  A 1
82     // is returned only after timing out.  The maximum period
83     // that this bit should be set is constrained to 244useconds.
84     // The count I use below guarantees coverage or more than
85     // this time, with any reasonable IPS setting.
86
87     u16 count = 25000;
88     while (--count != 0) {
89         if ( (inb_cmos(CMOS_STATUS_A) & 0x80) == 0 )
90             return 0;
91     }
92     return 1; // update-in-progress never transitioned to 0
93 }
94
95 // get current clock count
96 static void
97 handle_1a00(struct bregs *regs)
98 {
99     u32 ticks = GET_BDA(timer_counter);
100     regs->cx = ticks >> 16;
101     regs->dx = ticks;
102     regs->al = GET_BDA(timer_rollover);
103     SET_BDA(timer_rollover, 0); // reset flag
104     set_success(regs);
105 }
106
107 // Set Current Clock Count
108 static void
109 handle_1a01(struct bregs *regs)
110 {
111     u32 ticks = (regs->cx << 16) | regs->dx;
112     SET_BDA(timer_counter, ticks);
113     SET_BDA(timer_rollover, 0); // reset flag
114     regs->ah = 0;
115     set_success(regs);
116 }
117
118 // Read CMOS Time
119 static void
120 handle_1a02(struct bregs *regs)
121 {
122     if (rtc_updating()) {
123         set_fail(regs);
124         return;
125     }
126
127     regs->dh = inb_cmos(CMOS_RTC_SECONDS);
128     regs->cl = inb_cmos(CMOS_RTC_MINUTES);
129     regs->ch = inb_cmos(CMOS_RTC_HOURS);
130     regs->dl = inb_cmos(CMOS_STATUS_B) & 0x01;
131     regs->ah = 0;
132     regs->al = regs->ch;
133     set_success(regs);
134 }
135
136 // Set CMOS Time
137 static void
138 handle_1a03(struct bregs *regs)
139 {
140     // Using a debugger, I notice the following masking/setting
141     // of bits in Status Register B, by setting Reg B to
142     // a few values and getting its value after INT 1A was called.
143     //
144     //        try#1       try#2       try#3
145     // before 1111 1101   0111 1101   0000 0000
146     // after  0110 0010   0110 0010   0000 0010
147     //
148     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
149     // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
150     if (rtc_updating()) {
151         init_rtc();
152         // fall through as if an update were not in progress
153     }
154     outb_cmos(regs->dh, CMOS_RTC_SECONDS);
155     outb_cmos(regs->cl, CMOS_RTC_MINUTES);
156     outb_cmos(regs->ch, CMOS_RTC_HOURS);
157     // Set Daylight Savings time enabled bit to requested value
158     u8 val8 = (inb_cmos(CMOS_STATUS_B) & 0x60) | 0x02 | (regs->dl & 0x01);
159     outb_cmos(val8, CMOS_STATUS_B);
160     regs->ah = 0;
161     regs->al = val8; // val last written to Reg B
162     set_success(regs);
163 }
164
165 // Read CMOS Date
166 static void
167 handle_1a04(struct bregs *regs)
168 {
169     regs->ah = 0;
170     if (rtc_updating()) {
171         set_fail(regs);
172         return;
173     }
174     regs->cl = inb_cmos(CMOS_RTC_YEAR);
175     regs->dh = inb_cmos(CMOS_RTC_MONTH);
176     regs->dl = inb_cmos(CMOS_RTC_DAY_MONTH);
177     regs->ch = inb_cmos(CMOS_CENTURY);
178     regs->al = regs->ch;
179     set_success(regs);
180 }
181
182 // Set CMOS Date
183 static void
184 handle_1a05(struct bregs *regs)
185 {
186     // Using a debugger, I notice the following masking/setting
187     // of bits in Status Register B, by setting Reg B to
188     // a few values and getting its value after INT 1A was called.
189     //
190     //        try#1       try#2       try#3       try#4
191     // before 1111 1101   0111 1101   0000 0010   0000 0000
192     // after  0110 1101   0111 1101   0000 0010   0000 0000
193     //
194     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
195     // My assumption: RegB = (RegB & 01111111b)
196     if (rtc_updating()) {
197         init_rtc();
198         set_fail(regs);
199         return;
200     }
201     outb_cmos(regs->cl, CMOS_RTC_YEAR);
202     outb_cmos(regs->dh, CMOS_RTC_MONTH);
203     outb_cmos(regs->dl, CMOS_RTC_DAY_MONTH);
204     outb_cmos(regs->ch, CMOS_CENTURY);
205     // clear halt-clock bit
206     u8 val8 = inb_cmos(CMOS_STATUS_B) & ~RTC_B_SET;
207     outb_cmos(val8, CMOS_STATUS_B);
208     regs->ah = 0;
209     regs->al = val8; // AL = val last written to Reg B
210     set_success(regs);
211 }
212
213 // Set Alarm Time in CMOS
214 static void
215 handle_1a06(struct bregs *regs)
216 {
217     // Using a debugger, I notice the following masking/setting
218     // of bits in Status Register B, by setting Reg B to
219     // a few values and getting its value after INT 1A was called.
220     //
221     //        try#1       try#2       try#3
222     // before 1101 1111   0101 1111   0000 0000
223     // after  0110 1111   0111 1111   0010 0000
224     //
225     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
226     // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
227     u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
228     regs->ax = 0;
229     if (val8 & 0x20) {
230         // Alarm interrupt enabled already
231         set_fail(regs);
232         return;
233     }
234     if (rtc_updating()) {
235         init_rtc();
236         // fall through as if an update were not in progress
237     }
238     outb_cmos(regs->dh, CMOS_RTC_SECONDS_ALARM);
239     outb_cmos(regs->cl, CMOS_RTC_MINUTES_ALARM);
240     outb_cmos(regs->ch, CMOS_RTC_HOURS_ALARM);
241     unmask_pic2(PIC2_IRQ8); // enable IRQ 8
242     // enable Status Reg B alarm bit, clear halt clock bit
243     outb_cmos((val8 & ~RTC_B_SET) | RTC_B_AIE, CMOS_STATUS_B);
244     set_success(regs);
245 }
246
247 // Turn off Alarm
248 static void
249 handle_1a07(struct bregs *regs)
250 {
251     // Using a debugger, I notice the following masking/setting
252     // of bits in Status Register B, by setting Reg B to
253     // a few values and getting its value after INT 1A was called.
254     //
255     //        try#1       try#2       try#3       try#4
256     // before 1111 1101   0111 1101   0010 0000   0010 0010
257     // after  0100 0101   0101 0101   0000 0000   0000 0010
258     //
259     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
260     // My assumption: RegB = (RegB & 01010111b)
261     u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
262     // clear clock-halt bit, disable alarm bit
263     outb_cmos(val8 & ~(RTC_B_SET|RTC_B_AIE), CMOS_STATUS_B);
264     regs->ah = 0;
265     regs->al = val8; // val last written to Reg B
266     set_success(regs);
267 }
268
269 // Unsupported
270 static void
271 handle_1aXX(struct bregs *regs)
272 {
273     set_fail(regs);
274 }
275
276 // INT 1Ah Time-of-day Service Entry Point
277 void VISIBLE16
278 handle_1a(struct bregs *regs)
279 {
280     debug_enter(regs, DEBUG_HDL_1a);
281     switch (regs->ah) {
282     case 0x00: handle_1a00(regs); break;
283     case 0x01: handle_1a01(regs); break;
284     case 0x02: handle_1a02(regs); break;
285     case 0x03: handle_1a03(regs); break;
286     case 0x04: handle_1a04(regs); break;
287     case 0x05: handle_1a05(regs); break;
288     case 0x06: handle_1a06(regs); break;
289     case 0x07: handle_1a07(regs); break;
290     case 0xb1: handle_1ab1(regs); break;
291     default:   handle_1aXX(regs); break;
292     }
293 }
294
295 // User Timer Tick
296 void VISIBLE16
297 handle_1c()
298 {
299     debug_isr(DEBUG_ISR_1c);
300 }
301
302 // INT 08h System Timer ISR Entry Point
303 void VISIBLE16
304 handle_08()
305 {
306     debug_isr(DEBUG_ISR_08);
307     irq_enable();
308
309     floppy_tick();
310
311     u32 counter = GET_BDA(timer_counter);
312     counter++;
313     // compare to one days worth of timer ticks at 18.2 hz
314     if (counter >= 0x001800B0) {
315         // there has been a midnight rollover at this point
316         counter = 0;
317         SET_BDA(timer_rollover, GET_BDA(timer_rollover) + 1);
318     }
319
320     SET_BDA(timer_counter, counter);
321
322     // chain to user timer tick INT #0x1c
323     struct bregs br;
324     memset(&br, 0, sizeof(br));
325     call16_int(0x1c, &br);
326
327     irq_disable();
328
329     eoi_pic1();
330 }
331
332
333 /****************************************************************
334  * Periodic timer
335  ****************************************************************/
336
337 static int
338 set_usertimer(u32 usecs, u16 seg, u16 offset)
339 {
340     if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING)
341         return -1;
342
343     // Interval not already set.
344     SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING);  // Set status byte.
345     SET_BDA(ptr_user_wait_complete_flag, (seg << 16) | offset);
346     SET_BDA(user_wait_timeout, usecs);
347
348     // Unmask IRQ8 so INT70 will get through.
349     unmask_pic2(PIC2_IRQ8);
350     // Turn on the Periodic Interrupt timer
351     u8 bRegister = inb_cmos(CMOS_STATUS_B);
352     outb_cmos(bRegister | RTC_B_PIE, CMOS_STATUS_B);
353
354     return 0;
355 }
356
357 static void
358 clear_usertimer()
359 {
360     // Turn off status byte.
361     SET_BDA(rtc_wait_flag, 0);
362     // Clear the Periodic Interrupt.
363     u8 bRegister = inb_cmos(CMOS_STATUS_B);
364     outb_cmos(bRegister & ~RTC_B_PIE, CMOS_STATUS_B);
365 }
366
367 // Sleep for n microseconds.
368 int
369 usleep(u32 count)
370 {
371 #ifdef MODE16
372     // In 16bit mode, use the rtc to wait for the specified time.
373     u8 statusflag = 0;
374     int ret = set_usertimer(count, GET_SEG(SS), (u32)&statusflag);
375     if (ret)
376         return -1;
377     irq_enable();
378     while (!statusflag)
379         cpu_relax();
380     irq_disable();
381     return 0;
382 #else
383     // In 32bit mode, we need to call into 16bit mode to sleep.
384     struct bregs br;
385     memset(&br, 0, sizeof(br));
386     br.ah = 0x86;
387     br.cx = count >> 16;
388     br.dx = count;
389     call16_int(0x15, &br);
390     if (br.flags & F_CF)
391         return -1;
392     return 0;
393 #endif
394 }
395
396 #define RET_ECLOCKINUSE  0x83
397
398 // Wait for CX:DX microseconds. currently using the
399 // refresh request port 0x61 bit4, toggling every 15usec
400 void
401 handle_1586(struct bregs *regs)
402 {
403     int ret = usleep((regs->cx << 16) | regs->dx);
404     if (ret)
405         set_code_fail(regs, RET_ECLOCKINUSE);
406     else
407         set_success(regs);
408 }
409
410 // Set Interval requested.
411 static void
412 handle_158300(struct bregs *regs)
413 {
414     int ret = set_usertimer((regs->cx << 16) | regs->dx, regs->es, regs->bx);
415     if (ret)
416         // Interval already set.
417         set_code_fail(regs, RET_EUNSUPPORTED);
418     else
419         set_success(regs);
420 }
421
422 // Clear interval requested
423 static void
424 handle_158301(struct bregs *regs)
425 {
426     clear_usertimer();
427     set_success(regs);
428 }
429
430 static void
431 handle_1583XX(struct bregs *regs)
432 {
433     set_code_fail(regs, RET_EUNSUPPORTED);
434     regs->al--;
435 }
436
437 void
438 handle_1583(struct bregs *regs)
439 {
440     switch (regs->al) {
441     case 0x00: handle_158300(regs); break;
442     case 0x01: handle_158301(regs); break;
443     default:   handle_1583XX(regs); break;
444     }
445 }
446
447 // int70h: IRQ8 - CMOS RTC
448 void VISIBLE16
449 handle_70()
450 {
451     debug_isr(DEBUG_ISR_70);
452
453     // Check which modes are enabled and have occurred.
454     u8 registerB = inb_cmos(CMOS_STATUS_B);
455     u8 registerC = inb_cmos(CMOS_STATUS_C);
456
457     if (!(registerB & (RTC_B_PIE|RTC_B_AIE)))
458         goto done;
459     if (registerC & 0x20) {
460         // Handle Alarm Interrupt.
461         struct bregs br;
462         memset(&br, 0, sizeof(br));
463         call16_int(0x4a, &br);
464         irq_disable();
465     }
466     if (!(registerC & 0x40))
467         goto done;
468
469     // Handle Periodic Interrupt.
470
471     if (!GET_BDA(rtc_wait_flag))
472         goto done;
473
474     // Wait Interval (Int 15, AH=83) active.
475     u32 time = GET_BDA(user_wait_timeout);  // Time left in microseconds.
476     if (time < 0x3D1) {
477         // Done waiting - write to specified flag byte.
478         u32 segoff = GET_BDA(ptr_user_wait_complete_flag);
479         u16 segment = segoff >> 16;
480         u16 offset = segoff & 0xffff;
481         u8 oldval = GET_FARVAR(segment, *(u8*)(offset+0));
482         SET_FARVAR(segment, *(u8*)(offset+0), oldval | 0x80);
483
484         clear_usertimer();
485     } else {
486         // Continue waiting.
487         time -= 0x3D1;
488         SET_BDA(user_wait_timeout, time);
489     }
490
491 done:
492     eoi_pic2();
493 }