1 | /* $Id: bs3-cmn-pit.c 61389 2016-06-02 00:04:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * BS3Kit - PIT Setup and Disable code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2016 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 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include "bs3kit-template-header.h"
|
---|
31 | #include <iprt/asm-amd64-x86.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | /*********************************************************************************************************************************
|
---|
35 | * Defined Constants And Macros *
|
---|
36 | *********************************************************************************************************************************/
|
---|
37 | #define BS3_PIT_PORT_CMD 0x43
|
---|
38 | #define BS3_PIT_PORT_CH0_DATA 0x40
|
---|
39 | #define BS3_PIT_HZ UINT32_C(1193182)
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * External Symbols *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | extern FNBS3TRAPHANDLER16 bs3PitIrqHandler_c16;
|
---|
46 | extern FNBS3TRAPHANDLER32 bs3PitIrqHandler_c32;
|
---|
47 | extern FNBS3TRAPHANDLER64 bs3PitIrqHandler_c64;
|
---|
48 |
|
---|
49 |
|
---|
50 | #undef Bs3PitSetupAndEnablePeriodTimer
|
---|
51 | BS3_CMN_DEF(void, Bs3PitSetupAndEnablePeriodTimer,(uint16_t cHzDesired))
|
---|
52 | {
|
---|
53 | RTCCUINTREG fSaved;
|
---|
54 | uint16_t cCount;
|
---|
55 | uint16_t cMsInterval;
|
---|
56 | uint32_t cNsInterval;
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Disable the PIT and make sure we've configured the IRQ handlers.
|
---|
60 | */
|
---|
61 | Bs3PitDisable();
|
---|
62 | Bs3PicSetup();
|
---|
63 | Bs3TrapSetHandlerEx(0x70, bs3PitIrqHandler_c16, bs3PitIrqHandler_c32, bs3PitIrqHandler_c64);
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Calculate an interval.
|
---|
67 | */
|
---|
68 | if (cHzDesired <= 18)
|
---|
69 | {
|
---|
70 | cCount = 0; /* 1193182 / 65536 = 18.206512451171875 Hz */
|
---|
71 | cHzDesired = 18;
|
---|
72 | cNsInterval = UINT32_C(54925401); /* 65536 / 1193182 = 0.054925401154224586022920225078823 seconds */
|
---|
73 | cMsInterval = 55;
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | cCount = BS3_PIT_HZ / cHzDesired;
|
---|
78 | cHzDesired = BS3_PIT_HZ / cCount;
|
---|
79 | /* 1s/1193182 = 0.000 000 838 095 110 38550698887512550474278 */
|
---|
80 | #if ARCH_BITS == 64
|
---|
81 | cNsInterval = cCount * UINT64_C(838095110) / 1000000;
|
---|
82 | #elif ARCH_BITS == 32
|
---|
83 | cNsInterval = cCount * UINT32_C(8381) / 10;
|
---|
84 | #else
|
---|
85 | cNsInterval = cCount * 838;
|
---|
86 | #endif
|
---|
87 | if (cCount <= 1194)
|
---|
88 | cMsInterval = 1; /* Must not be zero! */
|
---|
89 | else
|
---|
90 | cMsInterval = cCount / 1194;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Do the reprogramming.
|
---|
96 | */
|
---|
97 | fSaved = ASMIntDisableFlags();
|
---|
98 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
99 | (0 << 6) /* select: channel 0 */
|
---|
100 | | (3 << 4) /* access mode: lobyte/hibyte */
|
---|
101 | | (2 << 1) /* operation: Mode 2 */
|
---|
102 | | 0 /* binary mode */
|
---|
103 | );
|
---|
104 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)cCount);
|
---|
105 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)(cCount >> 8));
|
---|
106 |
|
---|
107 | g_cBs3PitIntervalNs = cNsInterval;
|
---|
108 | g_cBs3PitIntervalHz = cHzDesired;
|
---|
109 | g_cBs3PitIntervalMs = cMsInterval;
|
---|
110 |
|
---|
111 | Bs3PicUpdateMask(UINT16_C(0xfffe), 0);
|
---|
112 |
|
---|
113 | ASMSetFlags(fSaved);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | #undef Bs3PitDisable
|
---|
118 | BS3_CMN_DEF(void, Bs3PitDisable,(void))
|
---|
119 | {
|
---|
120 | if (g_cBs3PitIntervalMs != 0)
|
---|
121 | {
|
---|
122 | RTCCUINTREG fSaved = ASMIntDisableFlags();
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Not entirely sure what's the best way to do this, but let's try reprogram
|
---|
126 | * it to a no-reload mode like 0 and set the count to 1.
|
---|
127 | */
|
---|
128 | g_cBs3PitIntervalMs = 0;
|
---|
129 | ASMOutU8(BS3_PIT_PORT_CMD,
|
---|
130 | (0 << 6) /* select: channel 0 */
|
---|
131 | | (1 << 4) /* access mode: lobyte */
|
---|
132 | | (0 << 1) /* operation: Mode 0 */
|
---|
133 | | 0 /* binary mode */
|
---|
134 | );
|
---|
135 | ASMOutU8(BS3_PIT_PORT_CH0_DATA, (uint8_t)1);
|
---|
136 |
|
---|
137 | /*
|
---|
138 | * Then mask the PIT IRQ on the PIC.
|
---|
139 | */
|
---|
140 | Bs3PicUpdateMask(UINT16_C(0xffff), 1);
|
---|
141 |
|
---|
142 | ASMSetFlags(fSaved);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Reset all the values.
|
---|
147 | */
|
---|
148 | g_cBs3PitNs = 0;
|
---|
149 | g_cBs3PitMs = 0;
|
---|
150 | g_cBs3PitTicks = 0;
|
---|
151 | g_cBs3PitIntervalNs = 0;
|
---|
152 | g_cBs3PitIntervalMs = 0;
|
---|
153 | g_cBs3PitIntervalHz = 0;
|
---|
154 | }
|
---|
155 |
|
---|