ArmPlatformPkg/SP805WatchdogDxe: switch to interrupt mode
[mirror_edk2.git] / ArmPlatformPkg / Drivers / SP805WatchdogDxe / SP805Watchdog.c
1 /** @file
2 *
3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 * Copyright (c) 2018, Linaro Limited. All rights reserved.
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/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeServicesTableLib.h>
25
26 #include <Protocol/HardwareInterrupt.h>
27 #include <Protocol/WatchdogTimer.h>
28
29 #include "SP805Watchdog.h"
30
31 STATIC EFI_EVENT mEfiExitBootServicesEvent;
32 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL *mInterrupt;
33 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;
34 STATIC UINT32 mTimerPeriod;
35
36 /**
37 Make sure the SP805 registers are unlocked for writing.
38
39 Note: The SP805 Watchdog Timer supports locking of its registers,
40 i.e. it inhibits all writes to avoid rogue software accidentally
41 corrupting their contents.
42 **/
43 STATIC
44 VOID
45 SP805Unlock (
46 VOID
47 )
48 {
49 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_LOCKED) {
50 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_SPECIAL_UNLOCK_CODE);
51 }
52 }
53
54 /**
55 Make sure the SP805 registers are locked and can not be overwritten.
56
57 Note: The SP805 Watchdog Timer supports locking of its registers,
58 i.e. it inhibits all writes to avoid rogue software accidentally
59 corrupting their contents.
60 **/
61 STATIC
62 VOID
63 SP805Lock (
64 VOID
65 )
66 {
67 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_UNLOCKED) {
68 // To lock it, just write in any number (except the special unlock code).
69 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_LOCK_IS_LOCKED);
70 }
71 }
72
73 STATIC
74 VOID
75 EFIAPI
76 SP805InterruptHandler (
77 IN HARDWARE_INTERRUPT_SOURCE Source,
78 IN EFI_SYSTEM_CONTEXT SystemContext
79 )
80 {
81 SP805Unlock ();
82 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
83 SP805Lock ();
84
85 mInterrupt->EndOfInterrupt (mInterrupt, Source);
86
87 //
88 // The notify function should be called with the elapsed number of ticks
89 // since the watchdog was armed, which should exceed the timer period.
90 // We don't actually know the elapsed number of ticks, so let's return
91 // the timer period plus 1.
92 //
93 if (mWatchdogNotify != NULL) {
94 mWatchdogNotify (mTimerPeriod + 1);
95 }
96
97 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
98 }
99
100 /**
101 Stop the SP805 watchdog timer from counting down by disabling interrupts.
102 **/
103 STATIC
104 VOID
105 SP805Stop (
106 VOID
107 )
108 {
109 // Disable interrupts
110 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) != 0) {
111 MmioAnd32 (SP805_WDOG_CONTROL_REG, ~SP805_WDOG_CTRL_INTEN);
112 }
113 }
114
115 /**
116 Starts the SP805 counting down by enabling interrupts.
117 The count down will start from the value stored in the Load register,
118 not from the value where it was previously stopped.
119 **/
120 STATIC
121 VOID
122 SP805Start (
123 VOID
124 )
125 {
126 // Enable interrupts
127 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) {
128 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_INTEN);
129 }
130 }
131
132 /**
133 On exiting boot services we must make sure the SP805 Watchdog Timer
134 is stopped.
135 **/
136 STATIC
137 VOID
138 EFIAPI
139 ExitBootServicesEvent (
140 IN EFI_EVENT Event,
141 IN VOID *Context
142 )
143 {
144 SP805Unlock ();
145 SP805Stop ();
146 SP805Lock ();
147 }
148
149 /**
150 This function registers the handler NotifyFunction so it is called every time
151 the watchdog timer expires. It also passes the amount of time since the last
152 handler call to the NotifyFunction.
153 If NotifyFunction is not NULL and a handler is not already registered,
154 then the new handler is registered and EFI_SUCCESS is returned.
155 If NotifyFunction is NULL, and a handler is already registered,
156 then that handler is unregistered.
157 If an attempt is made to register a handler when a handler is already registered,
158 then EFI_ALREADY_STARTED is returned.
159 If an attempt is made to unregister a handler when a handler is not registered,
160 then EFI_INVALID_PARAMETER is returned.
161
162 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
163 @param NotifyFunction The function to call when a timer interrupt fires. This
164 function executes at TPL_HIGH_LEVEL. The DXE Core will
165 register a handler for the timer interrupt, so it can know
166 how much time has passed. This information is used to
167 signal timer based events. NULL will unregister the handler.
168
169 @retval EFI_SUCCESS The watchdog timer handler was registered.
170 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
171 registered.
172 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
173 previously registered.
174
175 **/
176 STATIC
177 EFI_STATUS
178 EFIAPI
179 SP805RegisterHandler (
180 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
181 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
182 )
183 {
184 if (mWatchdogNotify == NULL && NotifyFunction == NULL) {
185 return EFI_INVALID_PARAMETER;
186 }
187
188 if (mWatchdogNotify != NULL && NotifyFunction != NULL) {
189 return EFI_ALREADY_STARTED;
190 }
191
192 mWatchdogNotify = NotifyFunction;
193 return EFI_SUCCESS;
194 }
195
196 /**
197
198 This function adjusts the period of timer interrupts to the value specified
199 by TimerPeriod. If the timer period is updated, then the selected timer
200 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
201 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
202 If an error occurs while attempting to update the timer period, then the
203 timer hardware will be put back in its state prior to this call, and
204 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
205 is disabled. This is not the same as disabling the CPU's interrupts.
206 Instead, it must either turn off the timer hardware, or it must adjust the
207 interrupt controller so that a CPU interrupt is not generated when the timer
208 interrupt fires.
209
210 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
211 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
212 the timer hardware is not programmable, then EFI_UNSUPPORTED is
213 returned. If the timer is programmable, then the timer period
214 will be rounded up to the nearest timer period that is supported
215 by the timer hardware. If TimerPeriod is set to 0, then the
216 timer interrupts will be disabled.
217
218
219 @retval EFI_SUCCESS The timer period was changed.
220 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
221 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
222
223 **/
224 STATIC
225 EFI_STATUS
226 EFIAPI
227 SP805SetTimerPeriod (
228 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
229 IN UINT64 TimerPeriod // In 100ns units
230 )
231 {
232 EFI_STATUS Status;
233 UINT64 Ticks64bit;
234
235 SP805Unlock ();
236
237 Status = EFI_SUCCESS;
238
239 if (TimerPeriod == 0) {
240 // This is a watchdog stop request
241 SP805Stop ();
242 } else {
243 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
244 // The SP805 will count down to zero and generate an interrupt.
245 //
246 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
247 //
248 // i.e.:
249 //
250 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
251
252 Ticks64bit = MultU64x32 (TimerPeriod, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz));
253 Ticks64bit = DivU64x32 (Ticks64bit, 10 * 1000 * 1000);
254
255 // The registers in the SP805 are only 32 bits
256 if (Ticks64bit > MAX_UINT32) {
257 // We could load the watchdog with the maximum supported value but
258 // if a smaller value was requested, this could have the watchdog
259 // triggering before it was intended.
260 // Better generate an error to let the caller know.
261 Status = EFI_DEVICE_ERROR;
262 goto EXIT;
263 }
264
265 // Update the watchdog with a 32-bit value.
266 MmioWrite32 (SP805_WDOG_LOAD_REG, (UINT32)Ticks64bit);
267
268 // Start the watchdog
269 SP805Start ();
270 }
271
272 mTimerPeriod = TimerPeriod;
273
274 EXIT:
275 // Ensure the watchdog is locked before exiting.
276 SP805Lock ();
277 ASSERT_EFI_ERROR (Status);
278 return Status;
279 }
280
281 /**
282 This function retrieves the period of timer interrupts in 100 ns units,
283 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
284 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
285 returned, then the timer is currently disabled.
286
287 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
288 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
289 0 is returned, then the timer is currently disabled.
290
291
292 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
293 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
294
295 **/
296 STATIC
297 EFI_STATUS
298 EFIAPI
299 SP805GetTimerPeriod (
300 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
301 OUT UINT64 *TimerPeriod
302 )
303 {
304 if (TimerPeriod == NULL) {
305 return EFI_INVALID_PARAMETER;
306 }
307
308 *TimerPeriod = mTimerPeriod;
309 return EFI_SUCCESS;
310 }
311
312 /**
313 Interface structure for the Watchdog Architectural Protocol.
314
315 @par Protocol Description:
316 This protocol provides a service to set the amount of time to wait
317 before firing the watchdog timer, and it also provides a service to
318 register a handler that is invoked when the watchdog timer fires.
319
320 @par When the watchdog timer fires, control will be passed to a handler
321 if one has been registered. If no handler has been registered,
322 or the registered handler returns, then the system will be
323 reset by calling the Runtime Service ResetSystem().
324
325 @param RegisterHandler
326 Registers a handler that will be called each time the
327 watchdogtimer interrupt fires. TimerPeriod defines the minimum
328 time between timer interrupts, so TimerPeriod will also
329 be the minimum time between calls to the registered
330 handler.
331 NOTE: If the watchdog resets the system in hardware, then
332 this function will not have any chance of executing.
333
334 @param SetTimerPeriod
335 Sets the period of the timer interrupt in 100 nS units.
336 This function is optional, and may return EFI_UNSUPPORTED.
337 If this function is supported, then the timer period will
338 be rounded up to the nearest supported timer period.
339
340 @param GetTimerPeriod
341 Retrieves the period of the timer interrupt in 100 nS units.
342
343 **/
344 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
345 SP805RegisterHandler,
346 SP805SetTimerPeriod,
347 SP805GetTimerPeriod
348 };
349
350 /**
351 Initialize the state information for the Watchdog Timer Architectural Protocol.
352
353 @param ImageHandle of the loaded driver
354 @param SystemTable Pointer to the System Table
355
356 @retval EFI_SUCCESS Protocol registered
357 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
358 @retval EFI_DEVICE_ERROR Hardware problems
359
360 **/
361 EFI_STATUS
362 EFIAPI
363 SP805Initialize (
364 IN EFI_HANDLE ImageHandle,
365 IN EFI_SYSTEM_TABLE *SystemTable
366 )
367 {
368 EFI_STATUS Status;
369 EFI_HANDLE Handle;
370
371 // Find the interrupt controller protocol. ASSERT if not found.
372 Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL,
373 (VOID **)&mInterrupt);
374 ASSERT_EFI_ERROR (Status);
375
376 // Unlock access to the SP805 registers
377 SP805Unlock ();
378
379 // Stop the watchdog from triggering unexpectedly
380 SP805Stop ();
381
382 // Set the watchdog to reset the board when triggered
383 // This is a last resort in case the interrupt handler fails
384 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_RESEN) == 0) {
385 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_RESEN);
386 }
387
388 // Clear any pending interrupts
389 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
390
391 // Prohibit any rogue access to SP805 registers
392 SP805Lock ();
393
394 if (PcdGet32 (PcdSP805WatchdogInterrupt) > 0) {
395 Status = mInterrupt->RegisterInterruptSource (mInterrupt,
396 PcdGet32 (PcdSP805WatchdogInterrupt),
397 SP805InterruptHandler);
398 if (EFI_ERROR (Status)) {
399 DEBUG ((DEBUG_ERROR, "%a: failed to register watchdog interrupt - %r\n",
400 __FUNCTION__, Status));
401 return Status;
402 }
403 } else {
404 DEBUG ((DEBUG_WARN, "%a: no interrupt specified, running in RESET mode only\n",
405 __FUNCTION__));
406 }
407
408 //
409 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
410 // This will avoid conflicts with the universal watchdog
411 //
412 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
413
414 // Register for an ExitBootServicesEvent
415 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
416 ExitBootServicesEvent, NULL, &mEfiExitBootServicesEvent);
417 if (EFI_ERROR (Status)) {
418 Status = EFI_OUT_OF_RESOURCES;
419 goto EXIT;
420 }
421
422 // Install the Timer Architectural Protocol onto a new handle
423 Handle = NULL;
424 Status = gBS->InstallMultipleProtocolInterfaces (
425 &Handle,
426 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
427 NULL
428 );
429 if (EFI_ERROR (Status)) {
430 Status = EFI_OUT_OF_RESOURCES;
431 goto EXIT;
432 }
433
434 EXIT:
435 ASSERT_EFI_ERROR (Status);
436 return Status;
437 }