]>
Commit | Line | Data |
---|---|---|
1e57a462 | 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.<BR>\r | |
1e57a462 | 5 | \r |
3402aac7 RC |
6 | 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 | |
1e57a462 | 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 | #include <Library/OmapLib.h>\r | |
27 | \r | |
28 | #include <Protocol/Timer.h>\r | |
29 | #include <Protocol/HardwareInterrupt.h>\r | |
30 | \r | |
31 | #include <Omap3530/Omap3530.h>\r | |
32 | \r | |
33 | \r | |
34 | // The notification function to call on every timer interrupt.\r | |
35 | volatile EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;\r | |
36 | \r | |
37 | \r | |
38 | // The current period of the timer interrupt\r | |
39 | volatile UINT64 mTimerPeriod = 0;\r | |
40 | \r | |
41 | // Cached copy of the Hardware Interrupt protocol instance\r | |
42 | EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;\r | |
43 | \r | |
44 | // Cached registers\r | |
45 | volatile UINT32 TISR;\r | |
46 | volatile UINT32 TCLR;\r | |
47 | volatile UINT32 TLDR;\r | |
48 | volatile UINT32 TCRR;\r | |
49 | volatile UINT32 TIER;\r | |
50 | \r | |
51 | // Cached interrupt vector\r | |
52 | volatile UINTN gVector;\r | |
53 | \r | |
54 | \r | |
55 | /**\r | |
56 | \r | |
57 | C Interrupt Handler calledin the interrupt context when Source interrupt is active.\r | |
58 | \r | |
59 | \r | |
60 | @param Source Source of the interrupt. Hardware routing off a specific platform defines\r | |
61 | what source means.\r | |
62 | \r | |
63 | @param SystemContext Pointer to system register context. Mostly used by debuggers and will\r | |
3402aac7 | 64 | update the system context after the return from the interrupt if\r |
1e57a462 | 65 | modified. Don't change these values unless you know what you are doing\r |
66 | \r | |
67 | **/\r | |
68 | VOID\r | |
69 | EFIAPI\r | |
70 | TimerInterruptHandler (\r | |
71 | IN HARDWARE_INTERRUPT_SOURCE Source,\r | |
3402aac7 | 72 | IN EFI_SYSTEM_CONTEXT SystemContext\r |
1e57a462 | 73 | )\r |
74 | {\r | |
75 | EFI_TPL OriginalTPL;\r | |
76 | \r | |
77 | \r | |
78 | \r | |
79 | //\r | |
3402aac7 | 80 | // DXE core uses this callback for the EFI timer tick. The DXE core uses locks\r |
1e57a462 | 81 | // that raise to TPL_HIGH and then restore back to current level. Thus we need\r |
3402aac7 | 82 | // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.\r |
1e57a462 | 83 | //\r |
84 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
85 | \r | |
86 | if (mTimerNotifyFunction) {\r | |
87 | mTimerNotifyFunction(mTimerPeriod);\r | |
88 | }\r | |
89 | \r | |
90 | // Clear all timer interrupts\r | |
3402aac7 | 91 | MmioWrite32 (TISR, TISR_CLEAR_ALL);\r |
1e57a462 | 92 | \r |
93 | // Poll interrupt status bits to ensure clearing\r | |
94 | while ((MmioRead32 (TISR) & TISR_ALL_INTERRUPT_MASK) != TISR_NO_INTERRUPTS_PENDING);\r | |
95 | \r | |
96 | gBS->RestoreTPL (OriginalTPL);\r | |
97 | }\r | |
98 | \r | |
99 | /**\r | |
3402aac7 RC |
100 | This function registers the handler NotifyFunction so it is called every time\r |
101 | the timer interrupt fires. It also passes the amount of time since the last\r | |
102 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r | |
103 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r | |
104 | returned. If the CPU does not support registering a timer interrupt handler,\r | |
105 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r | |
106 | when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r | |
107 | If an attempt is made to unregister a handler when a handler is not registered,\r | |
108 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r | |
109 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r | |
1e57a462 | 110 | is returned.\r |
111 | \r | |
112 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
113 | @param NotifyFunction The function to call when a timer interrupt fires. This\r | |
114 | function executes at TPL_HIGH_LEVEL. The DXE Core will\r | |
115 | register a handler for the timer interrupt, so it can know\r | |
116 | how much time has passed. This information is used to\r | |
117 | signal timer based events. NULL will unregister the handler.\r | |
118 | @retval EFI_SUCCESS The timer handler was registered.\r | |
119 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r | |
120 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r | |
121 | registered.\r | |
122 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r | |
123 | previously registered.\r | |
124 | @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r | |
125 | \r | |
126 | **/\r | |
127 | EFI_STATUS\r | |
128 | EFIAPI\r | |
129 | TimerDriverRegisterHandler (\r | |
130 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
131 | IN EFI_TIMER_NOTIFY NotifyFunction\r | |
132 | )\r | |
133 | {\r | |
134 | if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {\r | |
135 | return EFI_INVALID_PARAMETER;\r | |
136 | }\r | |
137 | \r | |
138 | if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {\r | |
139 | return EFI_ALREADY_STARTED;\r | |
140 | }\r | |
141 | \r | |
142 | mTimerNotifyFunction = NotifyFunction;\r | |
143 | \r | |
144 | return EFI_SUCCESS;\r | |
145 | }\r | |
146 | \r | |
147 | /**\r | |
148 | \r | |
3402aac7 RC |
149 | This function adjusts the period of timer interrupts to the value specified\r |
150 | by TimerPeriod. If the timer period is updated, then the selected timer\r | |
151 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r | |
152 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r | |
153 | If an error occurs while attempting to update the timer period, then the\r | |
154 | timer hardware will be put back in its state prior to this call, and\r | |
155 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r | |
156 | is disabled. This is not the same as disabling the CPU's interrupts.\r | |
157 | Instead, it must either turn off the timer hardware, or it must adjust the\r | |
158 | interrupt controller so that a CPU interrupt is not generated when the timer\r | |
159 | interrupt fires.\r | |
1e57a462 | 160 | \r |
161 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
162 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r | |
163 | the timer hardware is not programmable, then EFI_UNSUPPORTED is\r | |
164 | returned. If the timer is programmable, then the timer period\r | |
165 | will be rounded up to the nearest timer period that is supported\r | |
166 | by the timer hardware. If TimerPeriod is set to 0, then the\r | |
167 | timer interrupts will be disabled.\r | |
168 | \r | |
169 | \r | |
170 | @retval EFI_SUCCESS The timer period was changed.\r | |
171 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r | |
172 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r | |
173 | \r | |
174 | **/\r | |
175 | EFI_STATUS\r | |
176 | EFIAPI\r | |
177 | TimerDriverSetTimerPeriod (\r | |
178 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
179 | IN UINT64 TimerPeriod\r | |
180 | )\r | |
181 | {\r | |
182 | EFI_STATUS Status;\r | |
183 | UINT64 TimerCount;\r | |
184 | INT32 LoadValue;\r | |
3402aac7 | 185 | \r |
1e57a462 | 186 | if (TimerPeriod == 0) {\r |
187 | // Turn off GPTIMER3\r | |
188 | MmioWrite32 (TCLR, TCLR_ST_OFF);\r | |
3402aac7 RC |
189 | \r |
190 | Status = gInterrupt->DisableInterruptSource(gInterrupt, gVector);\r | |
191 | } else {\r | |
1e57a462 | 192 | // Calculate required timer count\r |
193 | TimerCount = DivU64x32(TimerPeriod * 100, PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds));\r | |
194 | \r | |
195 | // Set GPTIMER3 Load register\r | |
196 | LoadValue = (INT32) -TimerCount;\r | |
197 | MmioWrite32 (TLDR, LoadValue);\r | |
198 | MmioWrite32 (TCRR, LoadValue);\r | |
199 | \r | |
200 | // Enable Overflow interrupt\r | |
201 | MmioWrite32 (TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_ENABLE | TIER_MAT_IT_DISABLE);\r | |
202 | \r | |
203 | // Turn on GPTIMER3, it will reload at overflow\r | |
204 | MmioWrite32 (TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);\r | |
205 | \r | |
3402aac7 | 206 | Status = gInterrupt->EnableInterruptSource(gInterrupt, gVector);\r |
1e57a462 | 207 | }\r |
208 | \r | |
209 | //\r | |
210 | // Save the new timer period\r | |
211 | //\r | |
212 | mTimerPeriod = TimerPeriod;\r | |
213 | return Status;\r | |
214 | }\r | |
215 | \r | |
216 | \r | |
217 | /**\r | |
3402aac7 RC |
218 | This function retrieves the period of timer interrupts in 100 ns units,\r |
219 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r | |
220 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r | |
1e57a462 | 221 | returned, then the timer is currently disabled.\r |
222 | \r | |
223 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
224 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r | |
225 | 0 is returned, then the timer is currently disabled.\r | |
226 | \r | |
227 | \r | |
228 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r | |
229 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r | |
230 | \r | |
231 | **/\r | |
232 | EFI_STATUS\r | |
233 | EFIAPI\r | |
234 | TimerDriverGetTimerPeriod (\r | |
235 | IN EFI_TIMER_ARCH_PROTOCOL *This,\r | |
236 | OUT UINT64 *TimerPeriod\r | |
237 | )\r | |
238 | {\r | |
239 | if (TimerPeriod == NULL) {\r | |
240 | return EFI_INVALID_PARAMETER;\r | |
241 | }\r | |
242 | \r | |
243 | *TimerPeriod = mTimerPeriod;\r | |
244 | return EFI_SUCCESS;\r | |
245 | }\r | |
246 | \r | |
247 | /**\r | |
3402aac7 RC |
248 | This function generates a soft timer interrupt. If the platform does not support soft\r |
249 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r | |
250 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r | |
251 | service, then a soft timer interrupt will be generated. If the timer interrupt is\r | |
252 | enabled when this service is called, then the registered handler will be invoked. The\r | |
253 | registered handler should not be able to distinguish a hardware-generated timer\r | |
1e57a462 | 254 | interrupt from a software-generated timer interrupt.\r |
255 | \r | |
256 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r | |
257 | \r | |
258 | @retval EFI_SUCCESS The soft timer interrupt was generated.\r | |
259 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r | |
260 | \r | |
261 | **/\r | |
262 | EFI_STATUS\r | |
263 | EFIAPI\r | |
264 | TimerDriverGenerateSoftInterrupt (\r | |
265 | IN EFI_TIMER_ARCH_PROTOCOL *This\r | |
266 | )\r | |
267 | {\r | |
268 | return EFI_UNSUPPORTED;\r | |
269 | }\r | |
270 | \r | |
271 | \r | |
272 | /**\r | |
273 | Interface stucture for the Timer Architectural Protocol.\r | |
274 | \r | |
275 | @par Protocol Description:\r | |
3402aac7 | 276 | This protocol provides the services to initialize a periodic timer\r |
1e57a462 | 277 | interrupt, and to register a handler that is called each time the timer\r |
278 | interrupt fires. It may also provide a service to adjust the rate of the\r | |
3402aac7 RC |
279 | periodic timer interrupt. When a timer interrupt occurs, the handler is\r |
280 | passed the amount of time that has passed since the previous timer\r | |
1e57a462 | 281 | interrupt.\r |
282 | \r | |
283 | @param RegisterHandler\r | |
3402aac7 RC |
284 | Registers a handler that will be called each time the\r |
285 | timer interrupt fires. TimerPeriod defines the minimum\r | |
286 | time between timer interrupts, so TimerPeriod will also\r | |
287 | be the minimum time between calls to the registered\r | |
1e57a462 | 288 | handler.\r |
289 | \r | |
290 | @param SetTimerPeriod\r | |
3402aac7 RC |
291 | Sets the period of the timer interrupt in 100 nS units.\r |
292 | This function is optional, and may return EFI_UNSUPPORTED.\r | |
293 | If this function is supported, then the timer period will\r | |
1e57a462 | 294 | be rounded up to the nearest supported timer period.\r |
295 | \r | |
296 | \r | |
297 | @param GetTimerPeriod\r | |
298 | Retrieves the period of the timer interrupt in 100 nS units.\r | |
299 | \r | |
300 | @param GenerateSoftInterrupt\r | |
3402aac7 RC |
301 | Generates a soft timer interrupt that simulates the firing of\r |
302 | the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for\r | |
1e57a462 | 303 | a period of time.\r |
304 | \r | |
305 | **/\r | |
306 | EFI_TIMER_ARCH_PROTOCOL gTimer = {\r | |
307 | TimerDriverRegisterHandler,\r | |
308 | TimerDriverSetTimerPeriod,\r | |
309 | TimerDriverGetTimerPeriod,\r | |
310 | TimerDriverGenerateSoftInterrupt\r | |
311 | };\r | |
312 | \r | |
313 | \r | |
314 | /**\r | |
315 | Initialize the state information for the Timer Architectural Protocol and\r | |
316 | the Timer Debug support protocol that allows the debugger to break into a\r | |
317 | running program.\r | |
318 | \r | |
319 | @param ImageHandle of the loaded driver\r | |
320 | @param SystemTable Pointer to the System Table\r | |
321 | \r | |
322 | @retval EFI_SUCCESS Protocol registered\r | |
323 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r | |
324 | @retval EFI_DEVICE_ERROR Hardware problems\r | |
325 | \r | |
326 | **/\r | |
327 | EFI_STATUS\r | |
328 | EFIAPI\r | |
329 | TimerInitialize (\r | |
330 | IN EFI_HANDLE ImageHandle,\r | |
331 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
332 | )\r | |
333 | {\r | |
334 | EFI_HANDLE Handle = NULL;\r | |
335 | EFI_STATUS Status;\r | |
336 | UINT32 TimerBaseAddress;\r | |
337 | \r | |
338 | // Find the interrupt controller protocol. ASSERT if not found.\r | |
339 | Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);\r | |
340 | ASSERT_EFI_ERROR (Status);\r | |
341 | \r | |
342 | // Set up the timer registers\r | |
343 | TimerBaseAddress = TimerBase (FixedPcdGet32(PcdOmap35xxArchTimer));\r | |
344 | TISR = TimerBaseAddress + GPTIMER_TISR;\r | |
345 | TCLR = TimerBaseAddress + GPTIMER_TCLR;\r | |
346 | TLDR = TimerBaseAddress + GPTIMER_TLDR;\r | |
347 | TCRR = TimerBaseAddress + GPTIMER_TCRR;\r | |
348 | TIER = TimerBaseAddress + GPTIMER_TIER;\r | |
349 | \r | |
350 | // Disable the timer\r | |
351 | Status = TimerDriverSetTimerPeriod (&gTimer, 0);\r | |
352 | ASSERT_EFI_ERROR (Status);\r | |
353 | \r | |
354 | // Install interrupt handler\r | |
355 | gVector = InterruptVectorForTimer (FixedPcdGet32(PcdOmap35xxArchTimer));\r | |
356 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, gVector, TimerInterruptHandler);\r | |
357 | ASSERT_EFI_ERROR (Status);\r | |
358 | \r | |
359 | // Turn on the functional clock for Timer\r | |
360 | MmioOr32 (CM_FCLKEN_PER, CM_FCLKEN_PER_EN_GPT3_ENABLE);\r | |
361 | \r | |
362 | // Set up default timer\r | |
363 | Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod));\r | |
364 | ASSERT_EFI_ERROR (Status);\r | |
365 | \r | |
366 | // Install the Timer Architectural Protocol onto a new handle\r | |
367 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
368 | &Handle,\r | |
369 | &gEfiTimerArchProtocolGuid, &gTimer,\r | |
370 | NULL\r | |
371 | );\r | |
372 | ASSERT_EFI_ERROR(Status);\r | |
373 | \r | |
374 | return Status;\r | |
375 | }\r | |
376 | \r |