Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / 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 "bregs.h" // struct bregs
10 #include "hw/pic.h" // pic_eoi1
11 #include "hw/rtc.h" // rtc_read
12 #include "hw/usb-hid.h" // usb_check_event
13 #include "output.h" // debug_enter
14 #include "stacks.h" // yield
15 #include "string.h" // memset
16 #include "util.h" // clock_setup
17
18
19 /****************************************************************
20  * Init
21  ****************************************************************/
22
23 static u32
24 bcd2bin(u8 val)
25 {
26     return (val & 0xf) + ((val >> 4) * 10);
27 }
28
29 u8 Century VARLOW;
30
31 void
32 clock_setup(void)
33 {
34     dprintf(3, "init timer\n");
35     pit_setup();
36
37     rtc_setup();
38     rtc_updating();
39     u32 seconds = bcd2bin(rtc_read(CMOS_RTC_SECONDS));
40     u32 minutes = bcd2bin(rtc_read(CMOS_RTC_MINUTES));
41     u32 hours = bcd2bin(rtc_read(CMOS_RTC_HOURS));
42     u32 ticks = ticks_from_ms(((hours * 60 + minutes) * 60 + seconds) * 1000);
43     SET_BDA(timer_counter, ticks % TICKS_PER_DAY);
44
45     // Setup Century storage
46     if (CONFIG_QEMU) {
47         Century = rtc_read(CMOS_CENTURY);
48     } else {
49         // Infer current century from the year.
50         u8 year = rtc_read(CMOS_RTC_YEAR);
51         if (year > 0x80)
52             Century = 0x19;
53         else
54             Century = 0x20;
55     }
56
57     enable_hwirq(0, FUNC16(entry_08));
58     enable_hwirq(8, FUNC16(entry_70));
59 }
60
61
62 /****************************************************************
63  * Standard clock functions
64  ****************************************************************/
65
66 // get current clock count
67 static void
68 handle_1a00(struct bregs *regs)
69 {
70     yield();
71     u32 ticks = GET_BDA(timer_counter);
72     regs->cx = ticks >> 16;
73     regs->dx = ticks;
74     regs->al = GET_BDA(timer_rollover);
75     SET_BDA(timer_rollover, 0); // reset flag
76     set_success(regs);
77 }
78
79 // Set Current Clock Count
80 static void
81 handle_1a01(struct bregs *regs)
82 {
83     u32 ticks = (regs->cx << 16) | regs->dx;
84     SET_BDA(timer_counter, ticks);
85     SET_BDA(timer_rollover, 0); // reset flag
86     // XXX - should use set_code_success()?
87     regs->ah = 0;
88     set_success(regs);
89 }
90
91 // Read CMOS Time
92 static void
93 handle_1a02(struct bregs *regs)
94 {
95     if (rtc_updating()) {
96         set_invalid(regs);
97         return;
98     }
99
100     regs->dh = rtc_read(CMOS_RTC_SECONDS);
101     regs->cl = rtc_read(CMOS_RTC_MINUTES);
102     regs->ch = rtc_read(CMOS_RTC_HOURS);
103     regs->dl = rtc_read(CMOS_STATUS_B) & RTC_B_DSE;
104     regs->ah = 0;
105     regs->al = regs->ch;
106     set_success(regs);
107 }
108
109 // Set CMOS Time
110 static void
111 handle_1a03(struct bregs *regs)
112 {
113     // Using a debugger, I notice the following masking/setting
114     // of bits in Status Register B, by setting Reg B to
115     // a few values and getting its value after INT 1A was called.
116     //
117     //        try#1       try#2       try#3
118     // before 1111 1101   0111 1101   0000 0000
119     // after  0110 0010   0110 0010   0000 0010
120     //
121     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
122     // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
123     if (rtc_updating()) {
124         rtc_setup();
125         // fall through as if an update were not in progress
126     }
127     rtc_write(CMOS_RTC_SECONDS, regs->dh);
128     rtc_write(CMOS_RTC_MINUTES, regs->cl);
129     rtc_write(CMOS_RTC_HOURS, regs->ch);
130     // Set Daylight Savings time enabled bit to requested value
131     u8 val8 = ((rtc_read(CMOS_STATUS_B) & (RTC_B_PIE|RTC_B_AIE))
132                | RTC_B_24HR | (regs->dl & RTC_B_DSE));
133     rtc_write(CMOS_STATUS_B, val8);
134     regs->ah = 0;
135     regs->al = val8; // val last written to Reg B
136     set_success(regs);
137 }
138
139 // Read CMOS Date
140 static void
141 handle_1a04(struct bregs *regs)
142 {
143     regs->ah = 0;
144     if (rtc_updating()) {
145         set_invalid(regs);
146         return;
147     }
148     regs->cl = rtc_read(CMOS_RTC_YEAR);
149     regs->dh = rtc_read(CMOS_RTC_MONTH);
150     regs->dl = rtc_read(CMOS_RTC_DAY_MONTH);
151     regs->ch = GET_LOW(Century);
152     regs->al = regs->ch;
153     set_success(regs);
154 }
155
156 // Set CMOS Date
157 static void
158 handle_1a05(struct bregs *regs)
159 {
160     // Using a debugger, I notice the following masking/setting
161     // of bits in Status Register B, by setting Reg B to
162     // a few values and getting its value after INT 1A was called.
163     //
164     //        try#1       try#2       try#3       try#4
165     // before 1111 1101   0111 1101   0000 0010   0000 0000
166     // after  0110 1101   0111 1101   0000 0010   0000 0000
167     //
168     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
169     // My assumption: RegB = (RegB & 01111111b)
170     if (rtc_updating()) {
171         rtc_setup();
172         set_invalid(regs);
173         return;
174     }
175     rtc_write(CMOS_RTC_YEAR, regs->cl);
176     rtc_write(CMOS_RTC_MONTH, regs->dh);
177     rtc_write(CMOS_RTC_DAY_MONTH, regs->dl);
178     SET_LOW(Century, regs->ch);
179     // clear halt-clock bit
180     u8 val8 = rtc_read(CMOS_STATUS_B) & ~RTC_B_SET;
181     rtc_write(CMOS_STATUS_B, val8);
182     regs->ah = 0;
183     regs->al = val8; // AL = val last written to Reg B
184     set_success(regs);
185 }
186
187 // Set Alarm Time in CMOS
188 static void
189 handle_1a06(struct bregs *regs)
190 {
191     // Using a debugger, I notice the following masking/setting
192     // of bits in Status Register B, by setting Reg B to
193     // a few values and getting its value after INT 1A was called.
194     //
195     //        try#1       try#2       try#3
196     // before 1101 1111   0101 1111   0000 0000
197     // after  0110 1111   0111 1111   0010 0000
198     //
199     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
200     // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
201     u8 val8 = rtc_read(CMOS_STATUS_B); // Get Status Reg B
202     regs->ax = 0;
203     if (val8 & RTC_B_AIE) {
204         // Alarm interrupt enabled already
205         set_invalid(regs);
206         return;
207     }
208     if (rtc_updating()) {
209         rtc_setup();
210         // fall through as if an update were not in progress
211     }
212     rtc_write(CMOS_RTC_SECONDS_ALARM, regs->dh);
213     rtc_write(CMOS_RTC_MINUTES_ALARM, regs->cl);
214     rtc_write(CMOS_RTC_HOURS_ALARM, regs->ch);
215     // enable Status Reg B alarm bit, clear halt clock bit
216     rtc_write(CMOS_STATUS_B, (val8 & ~RTC_B_SET) | RTC_B_AIE);
217     set_success(regs);
218 }
219
220 // Turn off Alarm
221 static void
222 handle_1a07(struct bregs *regs)
223 {
224     // Using a debugger, I notice the following masking/setting
225     // of bits in Status Register B, by setting Reg B to
226     // a few values and getting its value after INT 1A was called.
227     //
228     //        try#1       try#2       try#3       try#4
229     // before 1111 1101   0111 1101   0010 0000   0010 0010
230     // after  0100 0101   0101 0101   0000 0000   0000 0010
231     //
232     // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
233     // My assumption: RegB = (RegB & 01010111b)
234     u8 val8 = rtc_read(CMOS_STATUS_B); // Get Status Reg B
235     // clear clock-halt bit, disable alarm bit
236     rtc_write(CMOS_STATUS_B, val8 & ~(RTC_B_SET|RTC_B_AIE));
237     regs->ah = 0;
238     regs->al = val8; // val last written to Reg B
239     set_success(regs);
240 }
241
242 // Unsupported
243 static void
244 handle_1aXX(struct bregs *regs)
245 {
246     set_unimplemented(regs);
247 }
248
249 // INT 1Ah Time-of-day Service Entry Point
250 void VISIBLE16
251 handle_1a(struct bregs *regs)
252 {
253     debug_enter(regs, DEBUG_HDL_1a);
254     switch (regs->ah) {
255     case 0x00: handle_1a00(regs); break;
256     case 0x01: handle_1a01(regs); break;
257     case 0x02: handle_1a02(regs); break;
258     case 0x03: handle_1a03(regs); break;
259     case 0x04: handle_1a04(regs); break;
260     case 0x05: handle_1a05(regs); break;
261     case 0x06: handle_1a06(regs); break;
262     case 0x07: handle_1a07(regs); break;
263     default:   handle_1aXX(regs); break;
264     }
265 }
266
267 // INT 08h System Timer ISR Entry Point
268 void VISIBLE16
269 handle_08(void)
270 {
271     debug_isr(DEBUG_ISR_08);
272
273     // Update counter
274     u32 counter = GET_BDA(timer_counter);
275     counter++;
276     // compare to one days worth of timer ticks at 18.2 hz
277     if (counter >= TICKS_PER_DAY) {
278         // there has been a midnight rollover at this point
279         counter = 0;
280         SET_BDA(timer_rollover, GET_BDA(timer_rollover) + 1);
281     }
282     SET_BDA(timer_counter, counter);
283
284     // Check for internal events.
285     floppy_tick();
286     usb_check_event();
287
288     // chain to user timer tick INT #0x1c
289     struct bregs br;
290     memset(&br, 0, sizeof(br));
291     br.flags = F_IF;
292     call16_int(0x1c, &br);
293
294     pic_eoi1();
295 }
296
297
298 /****************************************************************
299  * IRQ based timer
300  ****************************************************************/
301
302 // Calculate the timer value at 'count' number of full timer ticks in
303 // the future.
304 u32
305 irqtimer_calc_ticks(u32 count)
306 {
307     return (GET_BDA(timer_counter) + count + 1) % TICKS_PER_DAY;
308 }
309
310 // Return the timer value that is 'msecs' time in the future.
311 u32
312 irqtimer_calc(u32 msecs)
313 {
314     if (!msecs)
315         return GET_BDA(timer_counter);
316     return irqtimer_calc_ticks(ticks_from_ms(msecs));
317 }
318
319 // Check if the given timer value has passed.
320 int
321 irqtimer_check(u32 end)
322 {
323     return (((GET_BDA(timer_counter) + TICKS_PER_DAY - end) % TICKS_PER_DAY)
324             < (TICKS_PER_DAY/2));
325 }
326
327
328 /****************************************************************
329  * Periodic timer
330  ****************************************************************/
331
332 static int
333 set_usertimer(u32 usecs, u16 seg, u16 offset)
334 {
335     if (GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING)
336         return -1;
337
338     // Interval not already set.
339     SET_BDA(rtc_wait_flag, RWS_WAIT_PENDING);  // Set status byte.
340     SET_BDA(user_wait_complete_flag, SEGOFF(seg, offset));
341     SET_BDA(user_wait_timeout, usecs);
342     rtc_use();
343     return 0;
344 }
345
346 static void
347 clear_usertimer(void)
348 {
349     if (!(GET_BDA(rtc_wait_flag) & RWS_WAIT_PENDING))
350         return;
351     // Turn off status byte.
352     SET_BDA(rtc_wait_flag, 0);
353     rtc_release();
354 }
355
356 #define RET_ECLOCKINUSE  0x83
357
358 // Wait for CX:DX microseconds
359 void
360 handle_1586(struct bregs *regs)
361 {
362     // Use the rtc to wait for the specified time.
363     u8 statusflag = 0;
364     u32 count = (regs->cx << 16) | regs->dx;
365     int ret = set_usertimer(count, GET_SEG(SS), (u32)&statusflag);
366     if (ret) {
367         set_code_invalid(regs, RET_ECLOCKINUSE);
368         return;
369     }
370     while (!statusflag)
371         yield_toirq();
372     set_success(regs);
373 }
374
375 // Set Interval requested.
376 static void
377 handle_158300(struct bregs *regs)
378 {
379     int ret = set_usertimer((regs->cx << 16) | regs->dx, regs->es, regs->bx);
380     if (ret)
381         // Interval already set.
382         set_code_invalid(regs, RET_EUNSUPPORTED);
383     else
384         set_success(regs);
385 }
386
387 // Clear interval requested
388 static void
389 handle_158301(struct bregs *regs)
390 {
391     clear_usertimer();
392     set_success(regs);
393 }
394
395 static void
396 handle_1583XX(struct bregs *regs)
397 {
398     set_code_unimplemented(regs, RET_EUNSUPPORTED);
399     regs->al--;
400 }
401
402 void
403 handle_1583(struct bregs *regs)
404 {
405     switch (regs->al) {
406     case 0x00: handle_158300(regs); break;
407     case 0x01: handle_158301(regs); break;
408     default:   handle_1583XX(regs); break;
409     }
410 }
411
412 #define USEC_PER_RTC DIV_ROUND_CLOSEST(1000000, 1024)
413
414 // int70h: IRQ8 - CMOS RTC
415 void VISIBLE16
416 handle_70(void)
417 {
418     debug_isr(DEBUG_ISR_70);
419
420     // Check which modes are enabled and have occurred.
421     u8 registerB = rtc_read(CMOS_STATUS_B);
422     u8 registerC = rtc_read(CMOS_STATUS_C);
423
424     if (!(registerB & (RTC_B_PIE|RTC_B_AIE)))
425         goto done;
426     if (registerC & RTC_B_AIE) {
427         // Handle Alarm Interrupt.
428         struct bregs br;
429         memset(&br, 0, sizeof(br));
430         br.flags = F_IF;
431         call16_int(0x4a, &br);
432     }
433     if (!(registerC & RTC_B_PIE))
434         goto done;
435
436     // Handle Periodic Interrupt.
437
438     check_preempt();
439
440     if (!GET_BDA(rtc_wait_flag))
441         goto done;
442
443     // Wait Interval (Int 15, AH=83) active.
444     u32 time = GET_BDA(user_wait_timeout);  // Time left in microseconds.
445     if (time < USEC_PER_RTC) {
446         // Done waiting - write to specified flag byte.
447         struct segoff_s segoff = GET_BDA(user_wait_complete_flag);
448         u16 ptr_seg = segoff.seg;
449         u8 *ptr_far = (u8*)(segoff.offset+0);
450         u8 oldval = GET_FARVAR(ptr_seg, *ptr_far);
451         SET_FARVAR(ptr_seg, *ptr_far, oldval | 0x80);
452
453         clear_usertimer();
454     } else {
455         // Continue waiting.
456         time -= USEC_PER_RTC;
457         SET_BDA(user_wait_timeout, time);
458     }
459
460 done:
461     pic_eoi2();
462 }