1 | /* $Id: bs3-cmn-pit.c 88839 2021-05-03 14:28:49Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - PIT Setup and Disable code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "bs3kit-template-header.h"
|
---|
32 | #include <iprt/asm-amd64-x86.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /*********************************************************************************************************************************
|
---|
36 | * Defined Constants And Macros *
|
---|
37 | *********************************************************************************************************************************/
|
---|
38 | #define BS3_PIT_PORT_CMD 0x43
|
---|
39 | #define BS3_PIT_PORT_CH0_DATA 0x40
|
---|
40 | #define BS3_PIT_HZ UINT32_C(1193182)
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * External Symbols *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | extern FNBS3TRAPHANDLER16 bs3PitIrqHandler_c16;
|
---|
47 | extern FNBS3TRAPHANDLER32 bs3PitIrqHandler_c32;
|
---|
48 | extern FNBS3TRAPHANDLER64 bs3PitIrqHandler_c64;
|
---|
49 |
|
---|
50 |
|
---|
51 | #undef Bs3PitSetupAndEnablePeriodTimer
|
---|
52 | BS3_CMN_DEF(void, Bs3PitSetupAndEnablePeriodTimer,(uint16_t cHzDesired))
|
---|
53 | {
|
---|
54 | RTCCUINTREG fSaved;
|
---|
55 | uint16_t cCount;
|
---|
56 | uint16_t cMsInterval;
|
---|
57 | uint32_t cNsInterval;
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Disable the PIT and make sure we've configured the IRQ handlers.
|
---|
61 | */
|
---|
62 | Bs3PitDisable();
|
---|
63 | Bs3PicSetup();
|
---|
64 | Bs3TrapSetHandlerEx(0x70, bs3PitIrqHandler_c16, bs3PitIrqHandler_c32, bs3PitIrqHandler_c64);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Reset the counters.
|
---|
68 | */
|
---|
69 | g_cBs3PitNs = 0;
|
---|
70 | g_cBs3PitMs = 0;
|
---|
71 | g_cBs3PitTicks = 0;
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Calculate an interval.
|
---|
75 | */
|
---|
76 | if (cHzDesired <= 18)
|
---|
77 | {
|
---|
78 | cCount = 0; /* 1193182 / 65536 = 18.206512451171875 Hz */
|
---|
79 | cHzDesired = 18;
|
---|
80 | cNsInterval = UINT32_C(54925401); /* 65536 / 1193182 = 0.054925401154224586022920225078823 seconds */
|
---|
81 | cMsInterval = 55;
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | cCount = BS3_PIT_HZ / cHzDesired;
|
---|
86 | cHzDesired = BS3_PIT_HZ / cCount;
|
---|
87 | /* 1s/1193182 = 0.000 000 838 095 110 38550698887512550474278 */
|
---|
88 | #if ARCH_BITS == 64
|
---|
89 | cNsInterval = cCount * UINT64_C(838095110) / 1000000;
|
---|
90 | #elif ARCH_BITS == 32
|
---|
91 | cNsInterval = cCount * UINT32_C(8381) / 10;
|
---|
92 | #else
|
---|
93 | cNsInterval = cCount * 838;
|
---|
94 | #endif
|
---|
95 | if (cCount <= 1194)
|
---|
96 | cMsInterval = 1; /* Must not be zero! */
|
---|
97 | else
|
---|
98 | cMsInterval = cCount / 1194;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Do the reprogramming.
|
---|
104 | */
|
---|
105 | fSaved = ASMIntDisableFlags();
|
---|
106 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
107 | (0 << 6) /* select: channel 0 */
|
---|
108 | | (3 << 4) /* access mode: lobyte/hibyte */
|
---|
109 | | (2 << 1) /* operation: Mode 2 */
|
---|
110 | | 0 /* binary mode */
|
---|
111 | );
|
---|
112 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)cCount);
|
---|
113 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)(cCount >> 8));
|
---|
114 |
|
---|
115 | g_cBs3PitIntervalNs = cNsInterval;
|
---|
116 | g_cBs3PitIntervalHz = cHzDesired;
|
---|
117 | g_cBs3PitIntervalMs = cMsInterval;
|
---|
118 |
|
---|
119 | Bs3PicUpdateMask(UINT16_C(0xfffe), 0);
|
---|
120 |
|
---|
121 | ASMSetFlags(fSaved);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | #undef Bs3PitDisable
|
---|
126 | BS3_CMN_DEF(void, Bs3PitDisable,(void))
|
---|
127 | {
|
---|
128 | if (g_cBs3PitIntervalHz != 0)
|
---|
129 | {
|
---|
130 | RTCCUINTREG fSaved = ASMIntDisableFlags();
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Not entirely sure what's the best way to do this, but let's try reprogram
|
---|
134 | * it to a no-reload mode like 0 and set the count to 1.
|
---|
135 | */
|
---|
136 | g_cBs3PitIntervalMs = 0;
|
---|
137 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
138 | (0 << 6) /* select: channel 0 */
|
---|
139 | | (1 << 4) /* access mode: lobyte */
|
---|
140 | | (0 << 1) /* operation: Mode 0 */
|
---|
141 | | 0 /* binary mode */
|
---|
142 | );
|
---|
143 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)1);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Then mask the PIT IRQ on the PIC.
|
---|
147 | */
|
---|
148 | Bs3PicUpdateMask(UINT16_C(0xffff), 1);
|
---|
149 |
|
---|
150 | ASMSetFlags(fSaved);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Reset the interval values (leave the ticks and elapsed ns/ms values as-is).
|
---|
155 | */
|
---|
156 | g_cBs3PitIntervalNs = 0;
|
---|
157 | g_cBs3PitIntervalMs = 0;
|
---|
158 | g_cBs3PitIntervalHz = 0;
|
---|
159 | }
|
---|
160 |
|
---|