1 | /* $Id: bs3-cmn-pit.c 82968 2020-02-04 10:35:17Z 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 | * Calculate an interval.
|
---|
68 | */
|
---|
69 | if (cHzDesired <= 18)
|
---|
70 | {
|
---|
71 | cCount = 0; /* 1193182 / 65536 = 18.206512451171875 Hz */
|
---|
72 | cHzDesired = 18;
|
---|
73 | cNsInterval = UINT32_C(54925401); /* 65536 / 1193182 = 0.054925401154224586022920225078823 seconds */
|
---|
74 | cMsInterval = 55;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | cCount = BS3_PIT_HZ / cHzDesired;
|
---|
79 | cHzDesired = BS3_PIT_HZ / cCount;
|
---|
80 | /* 1s/1193182 = 0.000 000 838 095 110 38550698887512550474278 */
|
---|
81 | #if ARCH_BITS == 64
|
---|
82 | cNsInterval = cCount * UINT64_C(838095110) / 1000000;
|
---|
83 | #elif ARCH_BITS == 32
|
---|
84 | cNsInterval = cCount * UINT32_C(8381) / 10;
|
---|
85 | #else
|
---|
86 | cNsInterval = cCount * 838;
|
---|
87 | #endif
|
---|
88 | if (cCount <= 1194)
|
---|
89 | cMsInterval = 1; /* Must not be zero! */
|
---|
90 | else
|
---|
91 | cMsInterval = cCount / 1194;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Do the reprogramming.
|
---|
97 | */
|
---|
98 | fSaved = ASMIntDisableFlags();
|
---|
99 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
100 | (0 << 6) /* select: channel 0 */
|
---|
101 | | (3 << 4) /* access mode: lobyte/hibyte */
|
---|
102 | | (2 << 1) /* operation: Mode 2 */
|
---|
103 | | 0 /* binary mode */
|
---|
104 | );
|
---|
105 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)cCount);
|
---|
106 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)(cCount >> 8));
|
---|
107 |
|
---|
108 | g_cBs3PitIntervalNs = cNsInterval;
|
---|
109 | g_cBs3PitIntervalHz = cHzDesired;
|
---|
110 | g_cBs3PitIntervalMs = cMsInterval;
|
---|
111 |
|
---|
112 | Bs3PicUpdateMask(UINT16_C(0xfffe), 0);
|
---|
113 |
|
---|
114 | ASMSetFlags(fSaved);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | #undef Bs3PitDisable
|
---|
119 | BS3_CMN_DEF(void, Bs3PitDisable,(void))
|
---|
120 | {
|
---|
121 | if (g_cBs3PitIntervalMs != 0)
|
---|
122 | {
|
---|
123 | RTCCUINTREG fSaved = ASMIntDisableFlags();
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Not entirely sure what's the best way to do this, but let's try reprogram
|
---|
127 | * it to a no-reload mode like 0 and set the count to 1.
|
---|
128 | */
|
---|
129 | g_cBs3PitIntervalMs = 0;
|
---|
130 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
131 | (0 << 6) /* select: channel 0 */
|
---|
132 | | (1 << 4) /* access mode: lobyte */
|
---|
133 | | (0 << 1) /* operation: Mode 0 */
|
---|
134 | | 0 /* binary mode */
|
---|
135 | );
|
---|
136 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)1);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Then mask the PIT IRQ on the PIC.
|
---|
140 | */
|
---|
141 | Bs3PicUpdateMask(UINT16_C(0xffff), 1);
|
---|
142 |
|
---|
143 | ASMSetFlags(fSaved);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Reset all the values.
|
---|
148 | */
|
---|
149 | g_cBs3PitNs = 0;
|
---|
150 | g_cBs3PitMs = 0;
|
---|
151 | g_cBs3PitTicks = 0;
|
---|
152 | g_cBs3PitIntervalNs = 0;
|
---|
153 | g_cBs3PitIntervalMs = 0;
|
---|
154 | g_cBs3PitIntervalHz = 0;
|
---|
155 | }
|
---|
156 |
|
---|