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