]>
Commit | Line | Data |
---|---|---|
2ef2b01e A |
1 | /** @file\r |
2 | Template for Timer Architecture Protocol driver of the ARM flavor\r | |
3 | \r | |
4 | Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r | |
5 | \r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | \r | |
17 | #include <PiDxe.h>\r | |
18 | \r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/BaseMemoryLib.h>\r | |
22 | #include <Library/UefiBootServicesTableLib.h>\r | |
23 | #include <Library/UefiLib.h>\r | |
24 | #include <Library/PcdLib.h>\r | |
25 | #include <Library/IoLib.h>\r | |
26 | \r | |
27 | #include <Protocol/Timer.h>\r | |
28 | #include <Protocol/HardwareInterrupt.h>\r | |
29 | \r | |
30 | //\r | |
31 | // Get Base Address of timer block from platform .DSC file\r | |
32 | //\r | |
33 | #define TIMER_BASE ((UINTN)FixedPcdGet32 (PcdTimerBaseAddress) + 0x00c0)\r | |
34 | \r | |
35 | \r | |
36 | #define TIMER_CMD ((UINTN)FixedPcdGet32 (PcdTimerBaseAddress) + 0x00000004)\r | |
37 | #define TIMER_DATA ((UINTN)FixedPcdGet32 (PcdTimerBaseAddress) + 0x00000008)\r | |
38 | \r | |
39 | //\r | |
40 | // The notification function to call on every timer interrupt.\r | |
41 | // A bug in the compiler prevents us from initializing this here.\r | |
42 | //\r | |
43 | volatile EFI_TIMER_NOTIFY mTimerNotifyFunction;\r | |
44 | \r | |
45 | //\r | |
46 | // The current period of the timer interrupt\r | |
47 | //\r | |
48 | volatile UINT64 mTimerPeriod = 0;\r | |
49 | \r | |
50 | //\r | |
51 | // Cached copy of the Hardware Interrupt protocol instance\r | |
52 | //\r | |
53 | EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;\r | |
54 | \r | |
55 | \r | |
56 | /**\r | |
57 | C Interrupt Handler calledin the interrupt context when Source interrupt is active.\r | |
58 | \r | |
59 | @param Source Source of the interrupt. Hardware routing off a specific platform defines\r | |
60 | what source means.\r | |
61 | @param SystemContext Pointer to system register context. Mostly used by debuggers and will\r | |
62 | update the system context after the return from the interrupt if \r | |
63 | modified. Don't change these values unless you know what you are doing\r | |
64 | \r | |
65 | **/\r | |
66 | VOID\r | |
67 | EFIAPI\r | |
68 | TimerInterruptHandler (\r | |
69 | IN HARDWARE_INTERRUPT_SOURCE Source,\r | |
70 | IN EFI_SYSTEM_CONTEXT SystemContext \r | |
71 | )\r | |
72 | {\r | |
73 | EFI_TPL OriginalTPL;\r | |
74 | \r | |
75 | // Mask all interrupts\r | |
76 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
77 | \r | |
78 | MmioWrite32 (TIMER_CMD, 0);\r | |
79 | \r | |
80 | if (mTimerNotifyFunction) {\r | |
81 | mTimerNotifyFunction (mTimerPeriod);\r | |
82 | }\r | |
83 | \r | |
84 | // restore state\r | |
85 | gBS->RestoreTPL (OriginalTPL);\r | |
86 | }\r | |
87 | \r | |
88 | \r | |
89 | \r | |
90 | /**\r | |
91 | This function registers the handler NotifyFunction so it is called every time \r | |
92 | the timer interrupt fires. It also passes the amount of time since the last \r | |
93 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the \r | |
94 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is \r | |
95 | returned. If the CPU does not support registering a timer interrupt handler, \r | |
96 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler \r | |
97 | when a handler is already registered, then EFI_ALREADY_STARTED is returned. \r | |
98 | If an attempt is made to unregister a handler when a handler is not registered, \r | |
99 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to \r | |
100 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR \r | |
101 | is returned.\r | |
102 | \r | |
103 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
104 | @param NotifyFunction The function to call when a timer interrupt fires. This\r | |
105 | function executes at TPL_HIGH_LEVEL. The DXE Core will\r | |
106 | register a handler for the timer interrupt, so it can know\r | |
107 | how much time has passed. This information is used to\r | |
108 | signal timer based events. NULL will unregister the handler.\r | |
109 | \r | |
110 | @retval EFI_SUCCESS The timer handler was registered.\r | |
111 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r | |
112 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r | |
113 | registered.\r | |
114 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r | |
115 | previously registered.\r | |
116 | @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r | |
117 | \r | |
118 | **/\r | |
119 | EFI_STATUS\r | |
120 | EFIAPI\r | |
121 | TimerDriverRegisterHandler (\r | |
122 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
123 | IN EFI_TIMER_NOTIFY NotifyFunction\r | |
124 | )\r | |
125 | {\r | |
126 | //\r | |
127 | // Check for invalid parameters\r | |
128 | //\r | |
129 | if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {\r | |
130 | return EFI_INVALID_PARAMETER;\r | |
131 | }\r | |
132 | \r | |
133 | if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {\r | |
134 | return EFI_ALREADY_STARTED;\r | |
135 | }\r | |
136 | \r | |
137 | mTimerNotifyFunction = NotifyFunction;\r | |
138 | \r | |
139 | return EFI_SUCCESS;\r | |
140 | }\r | |
141 | \r | |
142 | \r | |
143 | \r | |
144 | /**\r | |
145 | This function adjusts the period of timer interrupts to the value specified \r | |
146 | by TimerPeriod. If the timer period is updated, then the selected timer \r | |
147 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If \r | |
148 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. \r | |
149 | If an error occurs while attempting to update the timer period, then the \r | |
150 | timer hardware will be put back in its state prior to this call, and \r | |
151 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt \r | |
152 | is disabled. This is not the same as disabling the CPU's interrupts. \r | |
153 | Instead, it must either turn off the timer hardware, or it must adjust the \r | |
154 | interrupt controller so that a CPU interrupt is not generated when the timer \r | |
155 | interrupt fires. \r | |
156 | \r | |
157 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
158 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r | |
159 | the timer hardware is not programmable, then EFI_UNSUPPORTED is\r | |
160 | returned. If the timer is programmable, then the timer period\r | |
161 | will be rounded up to the nearest timer period that is supported\r | |
162 | by the timer hardware. If TimerPeriod is set to 0, then the\r | |
163 | timer interrupts will be disabled.\r | |
164 | \r | |
165 | @retval EFI_SUCCESS The timer period was changed.\r | |
166 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r | |
167 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r | |
168 | \r | |
169 | **/\r | |
170 | EFI_STATUS\r | |
171 | EFIAPI\r | |
172 | TimerDriverSetTimerPeriod (\r | |
173 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
174 | IN UINT64 TimerPeriod\r | |
175 | )\r | |
176 | {\r | |
177 | EFI_STATUS Status;\r | |
178 | UINT64 TimerCount;\r | |
179 | \r | |
180 | if (TimerPeriod == 0) {\r | |
181 | //\r | |
182 | // Disable interrupt 0 and timer\r | |
183 | //\r | |
184 | MmioAnd32 (TIMER_DATA, 0);\r | |
185 | \r | |
186 | Status = gInterrupt->DisableInterruptSource (gInterrupt, FixedPcdGet32 (PcdTimerVector)); \r | |
187 | } else {\r | |
188 | //\r | |
189 | // Convert TimerPeriod into Timer F counts\r | |
190 | //\r | |
191 | TimerCount = DivU64x32 (TimerPeriod + 5, 10);\r | |
192 | \r | |
193 | //\r | |
194 | // Program Timer F with the new count value\r | |
195 | //\r | |
196 | MmioWrite32 (TIMER_DATA, (UINT32)TimerCount);\r | |
197 | \r | |
198 | //\r | |
199 | // Enable interrupt and initialize and enable timer.\r | |
200 | //\r | |
201 | MmioOr32 (TIMER_CMD, 0x11);\r | |
202 | \r | |
203 | Status = gInterrupt->EnableInterruptSource (gInterrupt, FixedPcdGet32 (PcdTimerVector)); \r | |
204 | }\r | |
205 | \r | |
206 | //\r | |
207 | // Save the new timer period\r | |
208 | //\r | |
209 | mTimerPeriod = TimerPeriod;\r | |
210 | return Status;\r | |
211 | }\r | |
212 | \r | |
213 | \r | |
214 | /**\r | |
215 | This function retrieves the period of timer interrupts in 100 ns units, \r | |
216 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod \r | |
217 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is \r | |
218 | returned, then the timer is currently disabled.\r | |
219 | \r | |
220 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
221 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r | |
222 | 0 is returned, then the timer is currently disabled.\r | |
223 | \r | |
224 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r | |
225 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r | |
226 | \r | |
227 | **/\r | |
228 | EFI_STATUS\r | |
229 | EFIAPI\r | |
230 | TimerDriverGetTimerPeriod (\r | |
231 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
232 | OUT UINT64 *TimerPeriod\r | |
233 | )\r | |
234 | {\r | |
235 | if (TimerPeriod == NULL) {\r | |
236 | return EFI_INVALID_PARAMETER;\r | |
237 | }\r | |
238 | \r | |
239 | *TimerPeriod = mTimerPeriod;\r | |
240 | return EFI_SUCCESS;\r | |
241 | }\r | |
242 | \r | |
243 | \r | |
244 | \r | |
245 | /**\r | |
246 | This function generates a soft timer interrupt. If the platform does not support soft \r | |
247 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned. \r | |
248 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler() \r | |
249 | service, then a soft timer interrupt will be generated. If the timer interrupt is \r | |
250 | enabled when this service is called, then the registered handler will be invoked. The \r | |
251 | registered handler should not be able to distinguish a hardware-generated timer \r | |
252 | interrupt from a software-generated timer interrupt.\r | |
253 | \r | |
254 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
255 | \r | |
256 | @retval EFI_SUCCESS The soft timer interrupt was generated.\r | |
257 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r | |
258 | \r | |
259 | **/\r | |
260 | EFI_STATUS\r | |
261 | EFIAPI\r | |
262 | TimerDriverGenerateSoftInterrupt (\r | |
263 | IN EFI_TIMER_ARCH_PROTOCOL *This\r | |
264 | )\r | |
265 | {\r | |
266 | return EFI_UNSUPPORTED;\r | |
267 | }\r | |
268 | \r | |
269 | \r | |
270 | /**\r | |
271 | Interface stucture for the Timer Architectural Protocol.\r | |
272 | \r | |
273 | @par Protocol Description:\r | |
274 | This protocol provides the services to initialize a periodic timer \r | |
275 | interrupt, and to register a handler that is called each time the timer\r | |
276 | interrupt fires. It may also provide a service to adjust the rate of the\r | |
277 | periodic timer interrupt. When a timer interrupt occurs, the handler is \r | |
278 | passed the amount of time that has passed since the previous timer \r | |
279 | interrupt.\r | |
280 | \r | |
281 | @param RegisterHandler\r | |
282 | Registers a handler that will be called each time the \r | |
283 | timer interrupt fires. TimerPeriod defines the minimum \r | |
284 | time between timer interrupts, so TimerPeriod will also \r | |
285 | be the minimum time between calls to the registered \r | |
286 | handler.\r | |
287 | \r | |
288 | @param SetTimerPeriod\r | |
289 | Sets the period of the timer interrupt in 100 nS units. \r | |
290 | This function is optional, and may return EFI_UNSUPPORTED. \r | |
291 | If this function is supported, then the timer period will \r | |
292 | be rounded up to the nearest supported timer period.\r | |
293 | \r | |
294 | @param GetTimerPeriod\r | |
295 | Retrieves the period of the timer interrupt in 100 nS units.\r | |
296 | \r | |
297 | @param GenerateSoftInterrupt\r | |
298 | Generates a soft timer interrupt that simulates the firing of \r | |
299 | the timer interrupt. This service can be used to invoke the \r | |
300 | registered handler if the timer interrupt has been masked for \r | |
301 | a period of time.\r | |
302 | \r | |
303 | **/\r | |
304 | EFI_TIMER_ARCH_PROTOCOL gTimer = {\r | |
305 | TimerDriverRegisterHandler,\r | |
306 | TimerDriverSetTimerPeriod,\r | |
307 | TimerDriverGetTimerPeriod,\r | |
308 | TimerDriverGenerateSoftInterrupt\r | |
309 | };\r | |
310 | \r | |
311 | EFI_HANDLE gTimerHandle = NULL;\r | |
312 | \r | |
313 | \r | |
314 | /**\r | |
315 | Initialize the state information for the Timer Architectural Protocol\r | |
316 | \r | |
317 | @param ImageHandle of the loaded driver\r | |
318 | @param SystemTable Pointer to the System Table\r | |
319 | \r | |
320 | @retval EFI_SUCCESS Protocol registered\r | |
321 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r | |
322 | @retval EFI_DEVICE_ERROR Hardware problems\r | |
323 | \r | |
324 | **/\r | |
325 | EFI_STATUS\r | |
326 | EFIAPI\r | |
327 | TimerInitialize (\r | |
328 | IN EFI_HANDLE ImageHandle,\r | |
329 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
330 | )\r | |
331 | {\r | |
332 | EFI_STATUS Status;\r | |
333 | \r | |
334 | //\r | |
335 | // Find the interrupt controller protocol. ASSERT if not found.\r | |
336 | //\r | |
337 | Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, ( VOID ** )&gInterrupt);\r | |
338 | ASSERT_EFI_ERROR (Status);\r | |
339 | \r | |
340 | MmioWrite32 (TIMER_CMD, 0x01);\r | |
341 | \r | |
342 | //\r | |
343 | // Force the timer to be disabled\r | |
344 | //\r | |
345 | Status = TimerDriverSetTimerPeriod (&gTimer, 0);\r | |
346 | ASSERT_EFI_ERROR (Status);\r | |
347 | \r | |
348 | //\r | |
349 | // Install interrupt handler\r | |
350 | //\r | |
351 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, FixedPcdGet32 (PcdTimerVector), TimerInterruptHandler);\r | |
352 | ASSERT_EFI_ERROR (Status);\r | |
353 | \r | |
354 | //\r | |
355 | // Force the timer to be enabled at its default period\r | |
356 | //\r | |
357 | Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32 (PcdTimerPeriod));\r | |
358 | ASSERT_EFI_ERROR (Status);\r | |
359 | \r | |
360 | \r | |
361 | //\r | |
362 | // Install the Timer Architectural Protocol onto a new handle\r | |
363 | //\r | |
364 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
365 | &gTimerHandle,\r | |
366 | &gEfiTimerArchProtocolGuid, &gTimer,\r | |
367 | NULL\r | |
368 | );\r | |
369 | ASSERT_EFI_ERROR (Status);\r | |
370 | \r | |
371 | return Status;\r | |
372 | }\r | |
373 | \r |