Add more linker protections around variables accessed from 16bit mode.
[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" // 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
16 // RTC register flags
17 #define RTC_A_UIP 0x80
18 #define RTC_B_SET 0x80
19 #define RTC_B_PIE 0x40
20 #define RTC_B_AIE 0x20
21 #define RTC_B_UIE 0x10
22
23 // Bits for PORT_PS2_CTRLB
24 #define PPCB_T2GATE (1<<0)
25 #define PPCB_SPKR   (1<<1)
26 #define PPCB_T2OUT  (1<<5)
27
28 // Bits for PORT_PIT_MODE
29 #define PM_SEL_TIMER0   (0<<6)
30 #define PM_SEL_TIMER1   (1<<6)
31 #define PM_SEL_TIMER2   (2<<6)
32 #define PM_SEL_READBACK (3<<6)
33 #define PM_ACCESS_LATCH  (0<<4)
34 #define PM_ACCESS_LOBYTE (1<<4)
35 #define PM_ACCESS_HIBYTE (2<<4)
36 #define PM_ACCESS_WORD   (3<<4)
37 #define PM_MODE0 (0<<1)
38 #define PM_MODE1 (1<<1)
39 #define PM_MODE2 (2<<1)
40 #define PM_MODE3 (3<<1)
41 #define PM_MODE4 (4<<1)
42 #define PM_MODE5 (5<<1)
43 #define PM_CNT_BINARY (0<<0)
44 #define PM_CNT_BCD    (1<<0)
45
46
47 /****************************************************************
48  * TSC timer
49  ****************************************************************/
50
51 #define PIT_TICK_RATE 1193182 // Underlying HZ of PIT
52 #define CALIBRATE_COUNT 0x800 // Approx 1.7ms
53
54 u32 cpu_khz VAR16_32;
55
56 static void
57 calibrate_tsc()
58 {
59     // Setup "timer2"
60     u8 orig = inb(PORT_PS2_CTRLB);
61     outb((orig & ~PPCB_SPKR) | PPCB_T2GATE, PORT_PS2_CTRLB);
62     /* binary, mode 0, LSB/MSB, Ch 2 */
63     outb(PM_SEL_TIMER2|PM_ACCESS_WORD|PM_MODE0|PM_CNT_BINARY, PORT_PIT_MODE);
64     /* LSB of ticks */
65     outb(CALIBRATE_COUNT & 0xFF, PORT_PIT_COUNTER2);
66     /* MSB of ticks */
67     outb(CALIBRATE_COUNT >> 8, PORT_PIT_COUNTER2);
68
69     u64 start = rdtscll();
70     while ((inb(PORT_PS2_CTRLB) & PPCB_T2OUT) == 0)
71         ;
72     u64 end = rdtscll();
73
74     // Restore PORT_PS2_CTRLB
75     outb(orig, PORT_PS2_CTRLB);
76
77     // Store calibrated cpu khz.
78     u64 diff = end - start;
79     dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n"
80             , (u32)start, (u32)end, (u32)diff);
81     u32 hz = diff * PIT_TICK_RATE / CALIBRATE_COUNT;
82     SET_GLOBAL(cpu_khz, hz / 1000);
83
84     dprintf(1, "CPU Mhz=%u\n", hz / 1000000);
85 }
86
87 static void
88 tscsleep(u64 diff)
89 {
90     u64 start = rdtscll();
91     u64 end = start + diff;
92     while (rdtscll() < end)
93         cpu_relax();
94 }
95
96 void
97 ndelay(u32 count)
98 {
99     u32 khz = GET_GLOBAL(cpu_khz);
100     tscsleep(count * khz / 1000000);
101 }
102 void
103 udelay(u32 count)
104 {
105     u32 khz = GET_GLOBAL(cpu_khz);
106     tscsleep(count * khz / 1000);
107 }
108 void
109 mdelay(u32 count)
110 {
111     u32 khz = GET_GLOBAL(cpu_khz);
112     tscsleep(count * khz);
113 }
114
115 // Return the TSC value that is 'msecs' time in the future.
116 u64
117 calc_future_tsc(u32 msecs)
118 {
119     u32 khz = GET_GLOBAL(cpu_khz);
120     return rdtscll() + ((u64)khz * msecs);
121 }
122
123
124 /****************************************************************
125  * Init
126  ****************************************************************/
127
128 static int
129 rtc_updating()
130 {
131     // This function checks to see if the update-in-progress bit
132     // is set in CMOS Status Register A.  If not, it returns 0.
133     // If it is set, it tries to wait until there is a transition
134     // to 0, and will return 0 if such a transition occurs.  A -1
135     // is returned only after timing out.  The maximum period
136     // that this bit should be set is constrained to 244useconds, so
137     // we wait for 1 msec max.
138
139     if ((inb_cmos(CMOS_STATUS_A) & 0x80) == 0)
140         return 0;
141     u64 end = calc_future_tsc(1);
142     do {
143         if ((inb_cmos(CMOS_STATUS_A) & 0x80) == 0)
144             return 0;
145     } while (rdtscll() <= end);
146
147     // update-in-progress never transitioned to 0
148     return -1;
149 }
150
151 static void
152 pit_setup()
153 {
154     // timer0: binary count, 16bit count, mode 2
155     outb(PM_SEL_TIMER0|PM_ACCESS_WORD|PM_MODE2|PM_CNT_BINARY, PORT_PIT_MODE);
156     // maximum count of 0000H = 18.2Hz
157     outb(0x0, PORT_PIT_COUNTER0);
158     outb(0x0, PORT_PIT_COUNTER0);
159 }
160
161 static u32
162 bcd2bin(u8 val)
163 {
164     return (val & 0xf) + ((val >> 4) * 10);
165 }
166
167 void
168 timer_setup()
169 {
170     dprintf(3, "init timer\n");
171     calibrate_tsc();
172     pit_setup();
173
174     rtc_updating();
175     u32 seconds = bcd2bin(inb_cmos(CMOS_RTC_SECONDS));
176     u32 ticks = (seconds * 18206507) / 1000000;
177     u32 minutes = bcd2bin(inb_cmos(CMOS_RTC_MINUTES));
178     ticks += (minutes * 10923904) / 10000;
179     u32 hours = bcd2bin(inb_cmos(CMOS_RTC_HOURS));
180     ticks += (hours * 65543427) / 1000;
181     SET_BDA(timer_counter, ticks);
182     SET_BDA(timer_rollover, 0);
183
184     enable_hwirq(0, entry_08);
185     enable_hwirq(8, entry_70);
186 }
187
188 static void
189 init_rtc()
190 {
191     outb_cmos(0x26, CMOS_STATUS_A);
192     outb_cmos(0x02, CMOS_STATUS_B);
193     inb_cmos(CMOS_STATUS_C);
194     inb_cmos(CMOS_STATUS_D);
195 }
196
197
198 /****************************************************************
199  * Standard clock functions
200  ****************************************************************/
201
202 // get current clock count
203 static void
204 handle_1a00(struct bregs *regs)
205 {
206     u32 ticks = GET_BDA(timer_counter);
207     regs->cx = ticks >> 16;
208     regs->dx = ticks;
209     regs->al = GET_BDA(timer_rollover);
210     SET_BDA(timer_rollover, 0); // reset flag
211     set_success(regs);
212 }
213
214 // Set Current Clock Count
215 static void
216 handle_1a01(struct bregs *regs)
217 {
218     u32 ticks = (regs->cx << 16) | regs->dx;
219     SET_BDA(timer_counter, ticks);
220     SET_BDA(timer_rollover, 0); // reset flag
221     // XXX - should use set_code_success()?
222     regs->ah = 0;
223     set_success(regs);
224 }
225
226 // Read CMOS Time
227 static void
228 handle_1a02(struct bregs *regs)
229 {
230     if (rtc_updating()) {
231         set_fail(regs);
232         return;
233     }
234
235     regs->dh = inb_cmos(CMOS_RTC_SECONDS);
236     regs->cl = inb_cmos(CMOS_RTC_MINUTES);
237     regs->ch = inb_cmos(CMOS_RTC_HOURS);
238     regs->dl = inb_cmos(CMOS_STATUS_B) & 0x01;
239     regs->ah = 0;
240     regs->al = regs->ch;
241     set_success(regs);
242 }
243
244 // Set CMOS Time
245 static void
246 handle_1a03(struct bregs *regs)
247 {
248     // Using a debugger, I notice the following masking/setting
249     // of bits in Status Register B, by setting Reg B to
250     // a few values and getting its value after INT 1A was called.
251     //
252     //        try#1       try#2       try#3
253     // before 1111 1101   0111 1101   0000 0000
254     // after  0110 0010   0110 0010   0000 0010
255     //
256     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
257     // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
258     if (rtc_updating()) {
259         init_rtc();
260         // fall through as if an update were not in progress
261     }
262     outb_cmos(regs->dh, CMOS_RTC_SECONDS);
263     outb_cmos(regs->cl, CMOS_RTC_MINUTES);
264     outb_cmos(regs->ch, CMOS_RTC_HOURS);
265     // Set Daylight Savings time enabled bit to requested value
266     u8 val8 = (inb_cmos(CMOS_STATUS_B) & 0x60) | 0x02 | (regs->dl & 0x01);
267     outb_cmos(val8, CMOS_STATUS_B);
268     regs->ah = 0;
269     regs->al = val8; // val last written to Reg B
270     set_success(regs);
271 }
272
273 // Read CMOS Date
274 static void
275 handle_1a04(struct bregs *regs)
276 {
277     regs->ah = 0;
278     if (rtc_updating()) {
279         set_fail(regs);
280         return;
281     }
282     regs->cl = inb_cmos(CMOS_RTC_YEAR);
283     regs->dh = inb_cmos(CMOS_RTC_MONTH);
284     regs->dl = inb_cmos(CMOS_RTC_DAY_MONTH);
285     regs->ch = inb_cmos(CMOS_CENTURY);
286     regs->al = regs->ch;
287     set_success(regs);
288 }
289
290 // Set CMOS Date
291 static void
292 handle_1a05(struct bregs *regs)
293 {
294     // Using a debugger, I notice the following masking/setting
295     // of bits in Status Register B, by setting Reg B to
296     // a few values and getting its value after INT 1A was called.
297     //
298     //        try#1       try#2       try#3       try#4
299     // before 1111 1101   0111 1101   0000 0010   0000 0000
300     // after  0110 1101   0111 1101   0000 0010   0000 0000
301     //
302     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
303     // My assumption: RegB = (RegB & 01111111b)
304     if (rtc_updating()) {
305         init_rtc();
306         set_fail(regs);
307         return;
308     }
309     outb_cmos(regs->cl, CMOS_RTC_YEAR);
310     outb_cmos(regs->dh, CMOS_RTC_MONTH);
311     outb_cmos(regs->dl, CMOS_RTC_DAY_MONTH);
312     outb_cmos(regs->ch, CMOS_CENTURY);
313     // clear halt-clock bit
314     u8 val8 = inb_cmos(CMOS_STATUS_B) & ~RTC_B_SET;
315     outb_cmos(val8, CMOS_STATUS_B);
316     regs->ah = 0;
317     regs->al = val8; // AL = val last written to Reg B
318     set_success(regs);
319 }
320
321 // Set Alarm Time in CMOS
322 static void
323 handle_1a06(struct bregs *regs)
324 {
325     // Using a debugger, I notice the following masking/setting
326     // of bits in Status Register B, by setting Reg B to
327     // a few values and getting its value after INT 1A was called.
328     //
329     //        try#1       try#2       try#3
330     // before 1101 1111   0101 1111   0000 0000
331     // after  0110 1111   0111 1111   0010 0000
332     //
333     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
334     // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
335     u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
336     regs->ax = 0;
337     if (val8 & 0x20) {
338         // Alarm interrupt enabled already
339         set_fail(regs);
340         return;
341     }
342     if (rtc_updating()) {
343         init_rtc();
344         // fall through as if an update were not in progress
345     }
346     outb_cmos(regs->dh, CMOS_RTC_SECONDS_ALARM);
347     outb_cmos(regs->cl, CMOS_RTC_MINUTES_ALARM);
348     outb_cmos(regs->ch, CMOS_RTC_HOURS_ALARM);
349     // enable Status Reg B alarm bit, clear halt clock bit
350     outb_cmos((val8 & ~RTC_B_SET) | RTC_B_AIE, CMOS_STATUS_B);
351     set_success(regs);
352 }
353
354 // Turn off Alarm
355 static void
356 handle_1a07(struct bregs *regs)
357 {
358     // Using a debugger, I notice the following masking/setting
359     // of bits in Status Register B, by setting Reg B to
360     // a few values and getting its value after INT 1A was called.
361     //
362     //        try#1       try#2       try#3       try#4
363     // before 1111 1101   0111 1101   0010 0000   0010 0010
364     // after  0100 0101   0101 0101   0000 0000   0000 0010
365     //
366     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
367     // My assumption: RegB = (RegB & 01010111b)
368     u8 val8 = inb_cmos(CMOS_STATUS_B); // Get Status Reg B
369     // clear clock-halt bit, disable alarm bit
370     outb_cmos(val8 & ~(RTC_B_SET|RTC_B_AIE), CMOS_STATUS_B);
371     regs->ah = 0;
372     regs->al = val8; // val last written to Reg B
373     set_success(regs);
374 }
375
376 // Unsupported
377 static void
378 handle_1aXX(struct bregs *regs)
379 {
380     set_fail(regs);
381 }
382
383 // INT 1Ah Time-of-day Service Entry Point
384 void VISIBLE16
385 handle_1a(struct bregs *regs)
386 {
387     debug_enter(regs, DEBUG_HDL_1a);
388     switch (regs->ah) {
389     case 0x00: handle_1a00(regs); break;
390     case 0x01: handle_1a01(regs); break;
391     case 0x02: handle_1a02(regs); break;
392     case 0x03: handle_1a03(regs); break;
393     case 0x04: handle_1a04(regs); break;
394     case 0x05: handle_1a05(regs); break;
395     case 0x06: handle_1a06(regs); break;
396     case 0x07: handle_1a07(regs); break;
397     case 0xb1: handle_1ab1(regs); break;
398     default:   handle_1aXX(regs); break;
399     }
400 }
401
402 // User Timer Tick
403 void VISIBLE16
404 handle_1c()
405 {
406     debug_isr(DEBUG_ISR_1c);
407 }
408
409 // INT 08h System Timer ISR Entry Point
410 void VISIBLE16
411 handle_08()
412 {
413     debug_isr(DEBUG_ISR_08);
414     irq_enable();
415
416     floppy_tick();
417
418     u32 counter = GET_BDA(timer_counter);
419     counter++;
420     // compare to one days worth of timer ticks at 18.2 hz
421     if (counter >= 0x001800B0) {
422         // there has been a midnight rollover at this point
423         counter = 0;
424         SET_BDA(timer_rollover, GET_BDA(timer_rollover) + 1);
425     }
426
427     SET_BDA(timer_counter, counter);
428
429     // chain to user timer tick INT #0x1c
430     struct bregs br;
431     memset(&br, 0, sizeof(br));
432     call16_int(0x1c, &br);
433
434     irq_disable();
435
436     eoi_pic1();
437 }
438
439
440 /****************************************************************
441  * Periodic timer
442  ****************************************************************/
443
444 static int
445 set_usertimer(u32 usecs, u16 seg, u16 offset)
446 {
447     if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING)
448         return -1;
449
450     // Interval not already set.
451     SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING);  // Set status byte.
452     SET_BDA(ptr_user_wait_complete_flag, (seg << 16) | offset);
453     SET_BDA(user_wait_timeout, usecs);
454
455     // Turn on the Periodic Interrupt timer
456     u8 bRegister = inb_cmos(CMOS_STATUS_B);
457     outb_cmos(bRegister | RTC_B_PIE, CMOS_STATUS_B);
458
459     return 0;
460 }
461
462 static void
463 clear_usertimer()
464 {
465     // Turn off status byte.
466     SET_BDA(rtc_wait_flag, 0);
467     // Clear the Periodic Interrupt.
468     u8 bRegister = inb_cmos(CMOS_STATUS_B);
469     outb_cmos(bRegister & ~RTC_B_PIE, CMOS_STATUS_B);
470 }
471
472 #define RET_ECLOCKINUSE  0x83
473
474 // Wait for CX:DX microseconds
475 void
476 handle_1586(struct bregs *regs)
477 {
478     // Use the rtc to wait for the specified time.
479     u8 statusflag = 0;
480     u32 count = (regs->cx << 16) | regs->dx;
481     int ret = set_usertimer(count, GET_SEG(SS), (u32)&statusflag);
482     if (ret) {
483         set_code_fail(regs, RET_ECLOCKINUSE);
484         return;
485     }
486     irq_enable();
487     while (!statusflag)
488         cpu_relax();
489     irq_disable();
490     set_success(regs);
491 }
492
493 // Set Interval requested.
494 static void
495 handle_158300(struct bregs *regs)
496 {
497     int ret = set_usertimer((regs->cx << 16) | regs->dx, regs->es, regs->bx);
498     if (ret)
499         // Interval already set.
500         set_code_fail(regs, RET_EUNSUPPORTED);
501     else
502         set_success(regs);
503 }
504
505 // Clear interval requested
506 static void
507 handle_158301(struct bregs *regs)
508 {
509     clear_usertimer();
510     set_success(regs);
511 }
512
513 static void
514 handle_1583XX(struct bregs *regs)
515 {
516     set_code_fail(regs, RET_EUNSUPPORTED);
517     regs->al--;
518 }
519
520 void
521 handle_1583(struct bregs *regs)
522 {
523     switch (regs->al) {
524     case 0x00: handle_158300(regs); break;
525     case 0x01: handle_158301(regs); break;
526     default:   handle_1583XX(regs); break;
527     }
528 }
529
530 // int70h: IRQ8 - CMOS RTC
531 void VISIBLE16
532 handle_70()
533 {
534     debug_isr(DEBUG_ISR_70);
535
536     // Check which modes are enabled and have occurred.
537     u8 registerB = inb_cmos(CMOS_STATUS_B);
538     u8 registerC = inb_cmos(CMOS_STATUS_C);
539
540     if (!(registerB & (RTC_B_PIE|RTC_B_AIE)))
541         goto done;
542     if (registerC & 0x20) {
543         // Handle Alarm Interrupt.
544         struct bregs br;
545         memset(&br, 0, sizeof(br));
546         call16_int(0x4a, &br);
547         irq_disable();
548     }
549     if (!(registerC & 0x40))
550         goto done;
551
552     // Handle Periodic Interrupt.
553
554     if (!GET_BDA(rtc_wait_flag))
555         goto done;
556
557     // Wait Interval (Int 15, AH=83) active.
558     u32 time = GET_BDA(user_wait_timeout);  // Time left in microseconds.
559     if (time < 0x3D1) {
560         // Done waiting - write to specified flag byte.
561         u32 segoff = GET_BDA(ptr_user_wait_complete_flag);
562         u16 segment = segoff >> 16;
563         u16 offset = segoff & 0xffff;
564         u8 oldval = GET_FARVAR(segment, *(u8*)(offset+0));
565         SET_FARVAR(segment, *(u8*)(offset+0), oldval | 0x80);
566
567         clear_usertimer();
568     } else {
569         // Continue waiting.
570         time -= 0x3D1;
571         SET_BDA(user_wait_timeout, time);
572     }
573
574 done:
575     eoi_pic2();
576 }