VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS-new/timepci.c@ 38699

Last change on this file since 38699 was 38699, checked in by vboxsync, 13 years ago

Converted system BIOS to Watcom C.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/*
2 * Copyright (C) 2006-2011 Oracle Corporation
3 *
4 * This file is part of VirtualBox Open Source Edition (OSE), as
5 * available from http://www.virtualbox.org. This file is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU
7 * General Public License (GPL) as published by the Free Software
8 * Foundation, in version 2 as it comes in the "COPYING" file of the
9 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
11 * --------------------------------------------------------------------
12 *
13 * This code is based on:
14 *
15 * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
16 *
17 * Copyright (C) 2002 MandrakeSoft S.A.
18 *
19 * MandrakeSoft S.A.
20 * 43, rue d'Aboukir
21 * 75002 Paris - France
22 * http://www.linux-mandrake.com/
23 * http://www.mandrakesoft.com/
24 *
25 * This library is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU Lesser General Public
27 * License as published by the Free Software Foundation; either
28 * version 2 of the License, or (at your option) any later version.
29 *
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public
36 * License along with this library; if not, write to the Free Software
37 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 *
39 */
40
41
42#include <stdint.h>
43#include "biosint.h"
44#include "inlines.h"
45
46#if DEBUG_INT1A
47# define BX_DEBUG_INT1A(...) BX_DEBUG(__VA_ARGS__)
48#else
49# define BX_DEBUG_INT1A(...)
50#endif
51
52// for access to RAM area which is used by interrupt vectors
53// and BIOS Data Area
54
55typedef struct {
56 uint8_t filler1[0x400];
57 uint8_t filler2[0x6c];
58 uint16_t ticks_low;
59 uint16_t ticks_high;
60 uint8_t midnight_flag;
61} bios_data_t;
62
63#define BiosData ((bios_data_t __far *) 0)
64
65void init_rtc(void)
66{
67 outb_cmos(0x0a, 0x26);
68 outb_cmos(0x0b, 0x02);
69 inb_cmos(0x0c);
70 inb_cmos(0x0d);
71}
72
73bx_bool rtc_updating(void)
74{
75 // This function checks to see if the update-in-progress bit
76 // is set in CMOS Status Register A. If not, it returns 0.
77 // If it is set, it tries to wait until there is a transition
78 // to 0, and will return 0 if such a transition occurs. A 1
79 // is returned only after timing out. The maximum period
80 // that this bit should be set is constrained to 244useconds.
81 // The count I use below guarantees coverage or more than
82 // this time, with any reasonable IPS setting.
83
84 uint16_t iter;
85
86 iter = 25000;
87 while (--iter != 0) {
88 if ( (inb_cmos(0x0a) & 0x80) == 0 )
89 return 0;
90 }
91 return 1; // update-in-progress never transitioned to 0
92}
93
94
95extern void eoi_both_pics(void); /* in assembly code */
96#pragma aux eoi_both_pics "*";
97
98void call_int_4a(void);
99#pragma aux call_int_4a = "int 4Ah";
100
101void BIOSCALL int70_function(pusha_regs_t regs, uint16_t ds, uint16_t es, iret_addr_t iret_addr)
102{
103 // INT 70h: IRQ 8 - CMOS RTC interrupt from periodic or alarm modes
104 uint8_t registerB = 0, registerC = 0;
105
106 // Check which modes are enabled and have occurred.
107 registerB = inb_cmos( 0xB );
108 registerC = inb_cmos( 0xC );
109
110 if( ( registerB & 0x60 ) != 0 ) {
111 if( ( registerC & 0x20 ) != 0 ) {
112 // Handle Alarm Interrupt.
113 int_enable();
114 call_int_4a();
115 int_disable();
116 }
117 if( ( registerC & 0x40 ) != 0 ) {
118 // Handle Periodic Interrupt.
119
120 if( read_byte( 0x40, 0xA0 ) != 0 ) {
121 // Wait Interval (Int 15, AH=83) active.
122 uint32_t time;
123
124 time = read_dword( 0x40, 0x9C ); // Time left in microseconds.
125 if( time < 0x3D1 ) {
126 // Done waiting.
127 uint16_t segment, offset;
128
129 segment = read_word( 0x40, 0x98 );
130 offset = read_word( 0x40, 0x9A );
131 write_byte( 0x40, 0xA0, 0 ); // Turn of status byte.
132 outb_cmos( 0xB, registerB & 0x37 ); // Clear the Periodic Interrupt.
133 write_byte( segment, offset, read_byte(segment, offset) | 0x80 ); // Write to specified flag byte.
134 } else {
135 // Continue waiting.
136 time -= 0x3D1;
137 write_dword( 0x40, 0x9C, time );
138 }
139 }
140 }
141 }
142 eoi_both_pics();
143}
144
145// @todo: the coding style WRT register access is totally inconsistent
146// in the following routines
147
148/* The PCI BIOS routine must be callable from protected mode, and
149 * therefore must not modify any segment registers(!).
150 */
151#if BX_PCIBIOS
152void BIOSCALL int1a_function_pci(i1apci_regs_t r)
153{
154#if 0
155 // real mode PCI BIOS functions now handled in assembler code
156 // this C code handles the error code for information only
157 if (r.gr.u.r8.ah == 0xff) {
158 BX_INFO("PCI BIOS: PCI not present\n");
159 } else if (r.gr.u.r8.ah == 0x81) {
160 BX_INFO("unsupported PCI BIOS function 0x%02x\n", r.gr.u.r8.al);
161 } else if (r.gr.u.r8.ah == 0x83) {
162 BX_INFO("bad PCI vendor ID %04x\n", r.gr.u.r16.dx);
163 } else if (r.gr.u.r8.ah == 0x86) {
164 if (r.gr.u.r8.ah == 0x02) {
165 BX_INFO("PCI device %04x:%04x not found at index %d\n", r.gr.u.r16.dx, r.gr.u.r16.cx, r.gr.u.r16.si);
166 } else {
167 BX_INFO("no PCI device with class code 0x%02x%04x found at index %d\n", r.gr.u.r8.cl, r.gr.u.r16.dx, r.gr.u.r16.si);
168 }
169 }
170#endif
171 //@todo: OS/2 MCP2 apparently calls us in protected mode with a DS which does not
172 // point to the BIOS segment!?!
173 send(BIOS_PRINTF_INFO, '!');
174 send(BIOS_PRINTF_INFO, '\n');
175 if (!GetCF(r.ra.flags)) {
176 SetCF(r.ra.flags);
177 }
178}
179#endif
180
181
182void BIOSCALL int1a_function(pusha_regs_t regs, uint16_t ds, uint16_t es, iret_addr_t iret_addr)
183{
184 uint8_t val8;
185
186 BX_DEBUG_INT1A("int1a: AX=%04x BX=%04x CX=%04x DX=%04x DS=%04x\n",
187 regs.u.r16.ax, regs.u.r16.bx, regs.u.r16.cx, regs.u.r16.dx, ds);
188 int_enable();
189
190 switch (regs.u.r8.ah) {
191 case 0: // get current clock count
192 int_disable();
193 regs.u.r16.cx = BiosData->ticks_high;
194 regs.u.r16.dx = BiosData->ticks_low;
195 regs.u.r8.al = BiosData->midnight_flag;
196 BiosData->midnight_flag = 0; // reset flag
197 int_enable();
198 // AH already 0
199 ClearCF(iret_addr.flags); // OK
200 break;
201
202 case 1: // Set Current Clock Count
203 int_disable();
204 BiosData->ticks_high = regs.u.r16.cx;
205 BiosData->ticks_low = regs.u.r16.dx;
206 BiosData->midnight_flag = 0; // reset flag
207 int_enable();
208 regs.u.r8.ah = 0;
209 ClearCF(iret_addr.flags); // OK
210 break;
211
212 case 2: // Read CMOS Time
213 if (rtc_updating()) {
214 SetCF(iret_addr.flags);
215 break;
216 }
217
218 regs.u.r8.dh = inb_cmos(0x00); // Seconds
219 regs.u.r8.cl = inb_cmos(0x02); // Minutes
220 regs.u.r8.ch = inb_cmos(0x04); // Hours
221 regs.u.r8.dl = inb_cmos(0x0b) & 0x01; // Stat Reg B
222 regs.u.r8.ah = 0;
223 regs.u.r8.al = regs.u.r8.ch;
224 ClearCF(iret_addr.flags); // OK
225 break;
226
227 case 3: // Set CMOS Time
228 // Using a debugger, I notice the following masking/setting
229 // of bits in Status Register B, by setting Reg B to
230 // a few values and getting its value after INT 1A was called.
231 //
232 // try#1 try#2 try#3
233 // before 1111 1101 0111 1101 0000 0000
234 // after 0110 0010 0110 0010 0000 0010
235 //
236 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
237 // My assumption: RegB = ((RegB & 01100000b) | 00000010b)
238 if (rtc_updating()) {
239 init_rtc();
240 // fall through as if an update were not in progress
241 }
242 outb_cmos(0x00, regs.u.r8.dh); // Seconds
243 outb_cmos(0x02, regs.u.r8.cl); // Minutes
244 outb_cmos(0x04, regs.u.r8.ch); // Hours
245 // Set Daylight Savings time enabled bit to requested value
246 val8 = (inb_cmos(0x0b) & 0x60) | 0x02 | (regs.u.r8.dl & 0x01);
247 // (reg B already selected)
248 outb_cmos(0x0b, val8);
249 regs.u.r8.ah = 0;
250 regs.u.r8.al = val8; // val last written to Reg B
251 ClearCF(iret_addr.flags); // OK
252 break;
253
254 case 4: // Read CMOS Date
255 regs.u.r8.ah = 0;
256 if (rtc_updating()) {
257 SetCF(iret_addr.flags);
258 break;
259 }
260 regs.u.r8.cl = inb_cmos(0x09); // Year
261 regs.u.r8.dh = inb_cmos(0x08); // Month
262 regs.u.r8.dl = inb_cmos(0x07); // Day of Month
263 regs.u.r8.ch = inb_cmos(0x32); // Century
264 regs.u.r8.al = regs.u.r8.ch;
265 ClearCF(iret_addr.flags); // OK
266 break;
267
268 case 5: // Set CMOS Date
269 // Using a debugger, I notice the following masking/setting
270 // of bits in Status Register B, by setting Reg B to
271 // a few values and getting its value after INT 1A was called.
272 //
273 // try#1 try#2 try#3 try#4
274 // before 1111 1101 0111 1101 0000 0010 0000 0000
275 // after 0110 1101 0111 1101 0000 0010 0000 0000
276 //
277 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
278 // My assumption: RegB = (RegB & 01111111b)
279 if (rtc_updating()) {
280 init_rtc();
281 SetCF(iret_addr.flags);
282 break;
283 }
284 outb_cmos(0x09, regs.u.r8.cl); // Year
285 outb_cmos(0x08, regs.u.r8.dh); // Month
286 outb_cmos(0x07, regs.u.r8.dl); // Day of Month
287 outb_cmos(0x32, regs.u.r8.ch); // Century
288 val8 = inb_cmos(0x0b) & 0x7f; // clear halt-clock bit
289 outb_cmos(0x0b, val8);
290 regs.u.r8.ah = 0;
291 regs.u.r8.al = val8; // AL = val last written to Reg B
292 ClearCF(iret_addr.flags); // OK
293 break;
294
295 case 6: // Set Alarm Time in CMOS
296 // Using a debugger, I notice the following masking/setting
297 // of bits in Status Register B, by setting Reg B to
298 // a few values and getting its value after INT 1A was called.
299 //
300 // try#1 try#2 try#3
301 // before 1101 1111 0101 1111 0000 0000
302 // after 0110 1111 0111 1111 0010 0000
303 //
304 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
305 // My assumption: RegB = ((RegB & 01111111b) | 00100000b)
306 val8 = inb_cmos(0x0b); // Get Status Reg B
307 regs.u.r16.ax = 0;
308 if (val8 & 0x20) {
309 // Alarm interrupt enabled already
310 SetCF(iret_addr.flags); // Error: alarm in use
311 break;
312 }
313 if (rtc_updating()) {
314 init_rtc();
315 // fall through as if an update were not in progress
316 }
317 outb_cmos(0x01, regs.u.r8.dh); // Seconds alarm
318 outb_cmos(0x03, regs.u.r8.cl); // Minutes alarm
319 outb_cmos(0x05, regs.u.r8.ch); // Hours alarm
320 outb(0xa1, inb(0xa1) & 0xfe); // enable IRQ 8
321 // enable Status Reg B alarm bit, clear halt clock bit
322 outb_cmos(0x0b, (val8 & 0x7f) | 0x20);
323 ClearCF(iret_addr.flags); // OK
324 break;
325
326 case 7: // Turn off Alarm
327 // Using a debugger, I notice the following masking/setting
328 // of bits in Status Register B, by setting Reg B to
329 // a few values and getting its value after INT 1A was called.
330 //
331 // try#1 try#2 try#3 try#4
332 // before 1111 1101 0111 1101 0010 0000 0010 0010
333 // after 0100 0101 0101 0101 0000 0000 0000 0010
334 //
335 // Bit4 in try#1 flipped in hardware (forced low) due to bit7=1
336 // My assumption: RegB = (RegB & 01010111b)
337 val8 = inb_cmos(0x0b); // Get Status Reg B
338 // clear clock-halt bit, disable alarm bit
339 outb_cmos(0x0b, val8 & 0x57); // disable alarm bit
340 regs.u.r8.ah = 0;
341 regs.u.r8.al = val8; // val last written to Reg B
342 ClearCF(iret_addr.flags); // OK
343 break;
344
345 default:
346 BX_DEBUG_INT1A("int1a: AX=%04x unsupported\n", regs.u.r16.ax);
347 SetCF(iret_addr.flags); // Unsupported
348 }
349}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use