]> git.proxmox.com Git - mirror_edk2.git/blame - PcAtChipsetPkg/8254TimerDxe/Timer.c
Correct Include style to pass GCC build.
[mirror_edk2.git] / PcAtChipsetPkg / 8254TimerDxe / Timer.c
CommitLineData
18c97f53 1/** @file\r
2 Timer Architectural Protocol as defined in the DXE CIS\r
eb16e240 3\r
95d48e82 4Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>\r
18c97f53 5This program and the accompanying materials \r
eb16e240 6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
18c97f53 13**/\r
eb16e240 14\r
15#include "Timer.h"\r
16\r
17//\r
18// The handle onto which the Timer Architectural Protocol will be installed\r
19//\r
20EFI_HANDLE mTimerHandle = NULL;\r
21\r
22//\r
23// The Timer Architectural Protocol that this driver produces\r
24//\r
25EFI_TIMER_ARCH_PROTOCOL mTimer = {\r
26 TimerDriverRegisterHandler,\r
27 TimerDriverSetTimerPeriod,\r
28 TimerDriverGetTimerPeriod,\r
29 TimerDriverGenerateSoftInterrupt\r
30};\r
31\r
32//\r
33// Pointer to the CPU Architectural Protocol instance\r
34//\r
35EFI_CPU_ARCH_PROTOCOL *mCpu;\r
36\r
eb16e240 37//\r
38// Pointer to the Legacy 8259 Protocol instance\r
39//\r
40EFI_LEGACY_8259_PROTOCOL *mLegacy8259;\r
41\r
42//\r
43// The notification function to call on every timer interrupt.\r
44// A bug in the compiler prevents us from initializing this here.\r
45//\r
2b7d16cf 46EFI_TIMER_NOTIFY mTimerNotifyFunction;\r
eb16e240 47\r
48//\r
49// The current period of the timer interrupt\r
50//\r
51volatile UINT64 mTimerPeriod = 0;\r
52\r
53//\r
54// Worker Functions\r
55//\r
18c97f53 56/**\r
57 Sets the counter value for Timer #0 in a legacy 8254 timer.\r
58\r
59 @param Count The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.\r
60**/\r
eb16e240 61VOID\r
62SetPitCount (\r
63 IN UINT16 Count\r
64 )\r
eb16e240 65{\r
2952f72d 66 IoWrite8 (TIMER_CONTROL_PORT, 0x36);\r
67 IoWrite8 (TIMER0_COUNT_PORT, (UINT8)(Count & 0xff));\r
68 IoWrite8 (TIMER0_COUNT_PORT, (UINT8)((Count >> 8) & 0xff));\r
eb16e240 69}\r
70\r
18c97f53 71/**\r
24115e44 72 8254 Timer #0 Interrupt Handler.\r
18c97f53 73\r
74 @param InterruptType The type of interrupt that occured\r
75 @param SystemContext A pointer to the system context when the interrupt occured\r
76**/\r
eb16e240 77VOID\r
78EFIAPI\r
79TimerInterruptHandler (\r
80 IN EFI_EXCEPTION_TYPE InterruptType,\r
81 IN EFI_SYSTEM_CONTEXT SystemContext\r
82 )\r
eb16e240 83{\r
84 EFI_TPL OriginalTPL;\r
85\r
86 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
87\r
88 mLegacy8259->EndOfInterrupt (mLegacy8259, Efi8259Irq0);\r
89\r
24115e44 90 if (mTimerNotifyFunction != NULL) {\r
eb16e240 91 //\r
24115e44 92 // @bug : This does not handle missed timer interrupts\r
eb16e240 93 //\r
94 mTimerNotifyFunction (mTimerPeriod);\r
95 }\r
96\r
97 gBS->RestoreTPL (OriginalTPL);\r
98}\r
99\r
18c97f53 100/**\r
101\r
102 This function registers the handler NotifyFunction so it is called every time\r
103 the timer interrupt fires. It also passes the amount of time since the last\r
104 handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r
105 handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r
106 returned. If the CPU does not support registering a timer interrupt handler,\r
107 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r
108 when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r
109 If an attempt is made to unregister a handler when a handler is not registered,\r
110 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r
111 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r
112 is returned.\r
113\r
114\r
115 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
116 @param NotifyFunction The function to call when a timer interrupt fires. This\r
117 function executes at TPL_HIGH_LEVEL. The DXE Core will\r
118 register a handler for the timer interrupt, so it can know\r
119 how much time has passed. This information is used to\r
120 signal timer based events. NULL will unregister the handler.\r
121\r
122 @retval EFI_SUCCESS The timer handler was registered.\r
123 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r
124 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r
125 registered.\r
126 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r
127 previously registered.\r
128 @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r
129\r
130**/\r
eb16e240 131EFI_STATUS\r
132EFIAPI\r
133TimerDriverRegisterHandler (\r
134 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
135 IN EFI_TIMER_NOTIFY NotifyFunction\r
136 )\r
eb16e240 137{\r
138 //\r
139 // Check for invalid parameters\r
140 //\r
141 if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r
142 return EFI_INVALID_PARAMETER;\r
143 }\r
144\r
145 if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r
146 return EFI_ALREADY_STARTED;\r
147 }\r
148\r
149 mTimerNotifyFunction = NotifyFunction;\r
150\r
151 return EFI_SUCCESS;\r
152}\r
153\r
18c97f53 154/**\r
155\r
156 This function adjusts the period of timer interrupts to the value specified\r
157 by TimerPeriod. If the timer period is updated, then the selected timer\r
158 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r
159 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r
160 If an error occurs while attempting to update the timer period, then the\r
161 timer hardware will be put back in its state prior to this call, and\r
162 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r
163 is disabled. This is not the same as disabling the CPU's interrupts.\r
164 Instead, it must either turn off the timer hardware, or it must adjust the\r
165 interrupt controller so that a CPU interrupt is not generated when the timer\r
166 interrupt fires.\r
167\r
168\r
169 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
170 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r
171 the timer hardware is not programmable, then EFI_UNSUPPORTED is\r
172 returned. If the timer is programmable, then the timer period\r
173 will be rounded up to the nearest timer period that is supported\r
174 by the timer hardware. If TimerPeriod is set to 0, then the\r
175 timer interrupts will be disabled.\r
176\r
177 @retval EFI_SUCCESS The timer period was changed.\r
178 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r
179 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r
180\r
181**/\r
eb16e240 182EFI_STATUS\r
183EFIAPI\r
184TimerDriverSetTimerPeriod (\r
185 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
186 IN UINT64 TimerPeriod\r
187 )\r
eb16e240 188{\r
189 UINT64 TimerCount;\r
190\r
191 //\r
192 // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns.\r
193 // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic\r
194 // TimerCount = (TimerPeriod * 119318)/1000000.\r
195 //\r
196 // Round up to next highest integer. This guarantees that the timer is\r
197 // equal to or slightly longer than the requested time.\r
198 // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000\r
199 //\r
200 // Note that a TimerCount of 0 is equivalent to a count of 65,536\r
201 //\r
202 // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited\r
203 // to 20 bits.\r
204 //\r
205 if (TimerPeriod == 0) {\r
206 //\r
207 // Disable timer interrupt for a TimerPeriod of 0\r
208 //\r
209 mLegacy8259->DisableIrq (mLegacy8259, Efi8259Irq0);\r
210 } else {\r
2952f72d 211\r
eb16e240 212 //\r
213 // Convert TimerPeriod into 8254 counts\r
214 //\r
394bbc59 215 TimerCount = DivU64x32 (MultU64x32 (119318, (UINT32) TimerPeriod) + 500000, 1000000);\r
eb16e240 216\r
217 //\r
218 // Check for overflow\r
219 //\r
220 if (TimerCount >= 65536) {\r
221 TimerCount = 0;\r
222 if (TimerPeriod >= DEFAULT_TIMER_TICK_DURATION) {\r
223 TimerPeriod = DEFAULT_TIMER_TICK_DURATION;\r
224 }\r
225 }\r
226 //\r
227 // Program the 8254 timer with the new count value\r
228 //\r
229 SetPitCount ((UINT16) TimerCount);\r
230\r
231 //\r
232 // Enable timer interrupt\r
233 //\r
234 mLegacy8259->EnableIrq (mLegacy8259, Efi8259Irq0, FALSE);\r
235 }\r
236 //\r
237 // Save the new timer period\r
238 //\r
239 mTimerPeriod = TimerPeriod;\r
240\r
241 return EFI_SUCCESS;\r
242}\r
243\r
18c97f53 244/**\r
eb16e240 245\r
18c97f53 246 This function retrieves the period of timer interrupts in 100 ns units,\r
247 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
248 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
eb16e240 249 returned, then the timer is currently disabled.\r
250\r
eb16e240 251\r
18c97f53 252 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
253 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r
254 0 is returned, then the timer is currently disabled.\r
eb16e240 255\r
18c97f53 256 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
257 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
eb16e240 258\r
18c97f53 259**/\r
260EFI_STATUS\r
261EFIAPI\r
262TimerDriverGetTimerPeriod (\r
263 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
264 OUT UINT64 *TimerPeriod\r
265 )\r
eb16e240 266{\r
267 if (TimerPeriod == NULL) {\r
268 return EFI_INVALID_PARAMETER;\r
269 }\r
270\r
271 *TimerPeriod = mTimerPeriod;\r
272\r
273 return EFI_SUCCESS;\r
274}\r
275\r
18c97f53 276/**\r
eb16e240 277\r
18c97f53 278 This function generates a soft timer interrupt. If the platform does not support soft\r
279 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r
280 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r
281 service, then a soft timer interrupt will be generated. If the timer interrupt is\r
282 enabled when this service is called, then the registered handler will be invoked. The\r
283 registered handler should not be able to distinguish a hardware-generated timer\r
eb16e240 284 interrupt from a software-generated timer interrupt.\r
285\r
eb16e240 286\r
18c97f53 287 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
eb16e240 288\r
18c97f53 289 @retval EFI_SUCCESS The soft timer interrupt was generated.\r
290 @retval EFI_UNSUPPORTEDT The platform does not support the generation of soft timer interrupts.\r
eb16e240 291\r
18c97f53 292**/\r
293EFI_STATUS\r
294EFIAPI\r
295TimerDriverGenerateSoftInterrupt (\r
296 IN EFI_TIMER_ARCH_PROTOCOL *This\r
297 )\r
eb16e240 298{\r
299 EFI_STATUS Status;\r
300 UINT16 IRQMask;\r
301 EFI_TPL OriginalTPL;\r
302 \r
303 //\r
304 // If the timer interrupt is enabled, then the registered handler will be invoked.\r
305 //\r
306 Status = mLegacy8259->GetMask (mLegacy8259, NULL, NULL, &IRQMask, NULL);\r
307 ASSERT_EFI_ERROR (Status);\r
308 if ((IRQMask & 0x1) == 0) {\r
309 //\r
310 // Invoke the registered handler\r
311 //\r
312 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
313\r
24115e44 314 if (mTimerNotifyFunction != NULL) {\r
eb16e240 315 //\r
24115e44 316 // @bug : This does not handle missed timer interrupts\r
eb16e240 317 //\r
318 mTimerNotifyFunction (mTimerPeriod);\r
319 }\r
320 \r
321 gBS->RestoreTPL (OriginalTPL);\r
322 } else {\r
323 return EFI_UNSUPPORTED;\r
324 }\r
325\r
326 return EFI_SUCCESS;\r
327}\r
328\r
18c97f53 329/**\r
330 Initialize the Timer Architectural Protocol driver\r
331\r
332 @param ImageHandle ImageHandle of the loaded driver\r
333 @param SystemTable Pointer to the System Table\r
334\r
335 @retval EFI_SUCCESS Timer Architectural Protocol created\r
336 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r
337 @retval EFI_DEVICE_ERROR A device error occured attempting to initialize the driver.\r
338\r
339**/\r
eb16e240 340EFI_STATUS\r
341EFIAPI\r
342TimerDriverInitialize (\r
343 IN EFI_HANDLE ImageHandle,\r
344 IN EFI_SYSTEM_TABLE *SystemTable\r
345 )\r
eb16e240 346{\r
347 EFI_STATUS Status;\r
348 UINT32 TimerVector;\r
349\r
350 //\r
351 // Initialize the pointer to our notify function.\r
352 //\r
353 mTimerNotifyFunction = NULL;\r
354\r
355 //\r
356 // Make sure the Timer Architectural Protocol is not already installed in the system\r
357 //\r
358 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r
359\r
eb16e240 360 //\r
361 // Find the CPU architectural protocol.\r
362 //\r
363 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu);\r
364 ASSERT_EFI_ERROR (Status);\r
365\r
366 //\r
367 // Find the Legacy8259 protocol.\r
368 //\r
369 Status = gBS->LocateProtocol (&gEfiLegacy8259ProtocolGuid, NULL, (VOID **) &mLegacy8259);\r
370 ASSERT_EFI_ERROR (Status);\r
371\r
372 //\r
373 // Force the timer to be disabled\r
374 //\r
375 Status = TimerDriverSetTimerPeriod (&mTimer, 0);\r
376 ASSERT_EFI_ERROR (Status);\r
377\r
378 //\r
379 // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver\r
380 //\r
381 TimerVector = 0;\r
382 Status = mLegacy8259->GetVector (mLegacy8259, Efi8259Irq0, (UINT8 *) &TimerVector);\r
383 ASSERT_EFI_ERROR (Status);\r
384\r
385 //\r
386 // Install interrupt handler for 8254 Timer #0 (ISA IRQ0)\r
387 //\r
388 Status = mCpu->RegisterInterruptHandler (mCpu, TimerVector, TimerInterruptHandler);\r
389 ASSERT_EFI_ERROR (Status);\r
390\r
391 //\r
392 // Force the timer to be enabled at its default period\r
393 //\r
394 Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);\r
395 ASSERT_EFI_ERROR (Status);\r
396\r
397 //\r
398 // Install the Timer Architectural Protocol onto a new handle\r
399 //\r
400 Status = gBS->InstallMultipleProtocolInterfaces (\r
401 &mTimerHandle,\r
2952f72d 402 &gEfiTimerArchProtocolGuid, &mTimer,\r
eb16e240 403 NULL\r
404 );\r
405 ASSERT_EFI_ERROR (Status);\r
406\r
407 return Status;\r
408}\r
2952f72d 409\r