3 Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 Copyright (c) 2018, Linaro Limited. All rights reserved.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/IoLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiRuntimeServicesTableLib.h>
19 #include <Protocol/HardwareInterrupt.h>
20 #include <Protocol/WatchdogTimer.h>
22 #include "SP805Watchdog.h"
24 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
25 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL
*mInterrupt
;
26 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify
;
27 STATIC UINT32 mTimerPeriod
;
30 Make sure the SP805 registers are unlocked for writing.
32 Note: The SP805 Watchdog Timer supports locking of its registers,
33 i.e. it inhibits all writes to avoid rogue software accidentally
34 corrupting their contents.
42 if (MmioRead32 (SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_LOCKED
) {
43 MmioWrite32 (SP805_WDOG_LOCK_REG
, SP805_WDOG_SPECIAL_UNLOCK_CODE
);
48 Make sure the SP805 registers are locked and can not be overwritten.
50 Note: The SP805 Watchdog Timer supports locking of its registers,
51 i.e. it inhibits all writes to avoid rogue software accidentally
52 corrupting their contents.
60 if (MmioRead32 (SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_UNLOCKED
) {
61 // To lock it, just write in any number (except the special unlock code).
62 MmioWrite32 (SP805_WDOG_LOCK_REG
, SP805_WDOG_LOCK_IS_LOCKED
);
69 SP805InterruptHandler (
70 IN HARDWARE_INTERRUPT_SOURCE Source
,
71 IN EFI_SYSTEM_CONTEXT SystemContext
75 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
78 mInterrupt
->EndOfInterrupt (mInterrupt
, Source
);
81 // The notify function should be called with the elapsed number of ticks
82 // since the watchdog was armed, which should exceed the timer period.
83 // We don't actually know the elapsed number of ticks, so let's return
84 // the timer period plus 1.
86 if (mWatchdogNotify
!= NULL
) {
87 mWatchdogNotify (mTimerPeriod
+ 1);
90 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, 0, NULL
);
94 Stop the SP805 watchdog timer from counting down by disabling interrupts.
102 // Disable interrupts
103 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) != 0) {
104 MmioAnd32 (SP805_WDOG_CONTROL_REG
, ~SP805_WDOG_CTRL_INTEN
);
109 Starts the SP805 counting down by enabling interrupts.
110 The count down will start from the value stored in the Load register,
111 not from the value where it was previously stopped.
120 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0) {
121 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_INTEN
);
126 On exiting boot services we must make sure the SP805 Watchdog Timer
132 ExitBootServicesEvent (
143 This function registers the handler NotifyFunction so it is called every time
144 the watchdog timer expires. It also passes the amount of time since the last
145 handler call to the NotifyFunction.
146 If NotifyFunction is not NULL and a handler is not already registered,
147 then the new handler is registered and EFI_SUCCESS is returned.
148 If NotifyFunction is NULL, and a handler is already registered,
149 then that handler is unregistered.
150 If an attempt is made to register a handler when a handler is already registered,
151 then EFI_ALREADY_STARTED is returned.
152 If an attempt is made to unregister a handler when a handler is not registered,
153 then EFI_INVALID_PARAMETER is returned.
155 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
156 @param NotifyFunction The function to call when a timer interrupt fires. This
157 function executes at TPL_HIGH_LEVEL. The DXE Core will
158 register a handler for the timer interrupt, so it can know
159 how much time has passed. This information is used to
160 signal timer based events. NULL will unregister the handler.
162 @retval EFI_SUCCESS The watchdog timer handler was registered.
163 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
165 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
166 previously registered.
172 SP805RegisterHandler (
173 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
174 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
177 if ((mWatchdogNotify
== NULL
) && (NotifyFunction
== NULL
)) {
178 return EFI_INVALID_PARAMETER
;
181 if ((mWatchdogNotify
!= NULL
) && (NotifyFunction
!= NULL
)) {
182 return EFI_ALREADY_STARTED
;
185 mWatchdogNotify
= NotifyFunction
;
191 This function adjusts the period of timer interrupts to the value specified
192 by TimerPeriod. If the timer period is updated, then the selected timer
193 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
194 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
195 If an error occurs while attempting to update the timer period, then the
196 timer hardware will be put back in its state prior to this call, and
197 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
198 is disabled. This is not the same as disabling the CPU's interrupts.
199 Instead, it must either turn off the timer hardware, or it must adjust the
200 interrupt controller so that a CPU interrupt is not generated when the timer
203 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
204 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
205 the timer hardware is not programmable, then EFI_UNSUPPORTED is
206 returned. If the timer is programmable, then the timer period
207 will be rounded up to the nearest timer period that is supported
208 by the timer hardware. If TimerPeriod is set to 0, then the
209 timer interrupts will be disabled.
212 @retval EFI_SUCCESS The timer period was changed.
213 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
214 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
220 SP805SetTimerPeriod (
221 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
222 IN UINT64 TimerPeriod
// In 100ns units
230 Status
= EFI_SUCCESS
;
232 if (TimerPeriod
== 0) {
233 // This is a watchdog stop request
236 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
237 // The SP805 will count down to zero and generate an interrupt.
239 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
243 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
245 Ticks64bit
= MultU64x32 (TimerPeriod
, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz
));
246 Ticks64bit
= DivU64x32 (Ticks64bit
, 10 * 1000 * 1000);
248 // The registers in the SP805 are only 32 bits
249 if (Ticks64bit
> MAX_UINT32
) {
250 // We could load the watchdog with the maximum supported value but
251 // if a smaller value was requested, this could have the watchdog
252 // triggering before it was intended.
253 // Better generate an error to let the caller know.
254 Status
= EFI_DEVICE_ERROR
;
258 // Update the watchdog with a 32-bit value.
259 MmioWrite32 (SP805_WDOG_LOAD_REG
, (UINT32
)Ticks64bit
);
261 // Start the watchdog
265 mTimerPeriod
= TimerPeriod
;
268 // Ensure the watchdog is locked before exiting.
270 ASSERT_EFI_ERROR (Status
);
275 This function retrieves the period of timer interrupts in 100 ns units,
276 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
277 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
278 returned, then the timer is currently disabled.
280 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
281 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
282 0 is returned, then the timer is currently disabled.
285 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
286 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
292 SP805GetTimerPeriod (
293 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
294 OUT UINT64
*TimerPeriod
297 if (TimerPeriod
== NULL
) {
298 return EFI_INVALID_PARAMETER
;
301 *TimerPeriod
= mTimerPeriod
;
306 Interface structure for the Watchdog Architectural Protocol.
308 @par Protocol Description:
309 This protocol provides a service to set the amount of time to wait
310 before firing the watchdog timer, and it also provides a service to
311 register a handler that is invoked when the watchdog timer fires.
313 @par When the watchdog timer fires, control will be passed to a handler
314 if one has been registered. If no handler has been registered,
315 or the registered handler returns, then the system will be
316 reset by calling the Runtime Service ResetSystem().
318 @param RegisterHandler
319 Registers a handler that will be called each time the
320 watchdogtimer interrupt fires. TimerPeriod defines the minimum
321 time between timer interrupts, so TimerPeriod will also
322 be the minimum time between calls to the registered
324 NOTE: If the watchdog resets the system in hardware, then
325 this function will not have any chance of executing.
327 @param SetTimerPeriod
328 Sets the period of the timer interrupt in 100 nS units.
329 This function is optional, and may return EFI_UNSUPPORTED.
330 If this function is supported, then the timer period will
331 be rounded up to the nearest supported timer period.
333 @param GetTimerPeriod
334 Retrieves the period of the timer interrupt in 100 nS units.
337 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
338 SP805RegisterHandler
,
344 Initialize the state information for the Watchdog Timer Architectural Protocol.
346 @param ImageHandle of the loaded driver
347 @param SystemTable Pointer to the System Table
349 @retval EFI_SUCCESS Protocol registered
350 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
351 @retval EFI_DEVICE_ERROR Hardware problems
357 IN EFI_HANDLE ImageHandle
,
358 IN EFI_SYSTEM_TABLE
*SystemTable
364 // Find the interrupt controller protocol. ASSERT if not found.
365 Status
= gBS
->LocateProtocol (
366 &gHardwareInterruptProtocolGuid
,
370 ASSERT_EFI_ERROR (Status
);
372 // Unlock access to the SP805 registers
375 // Stop the watchdog from triggering unexpectedly
378 // Set the watchdog to reset the board when triggered
379 // This is a last resort in case the interrupt handler fails
380 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_RESEN
) == 0) {
381 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_RESEN
);
384 // Clear any pending interrupts
385 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
387 // Prohibit any rogue access to SP805 registers
390 if (PcdGet32 (PcdSP805WatchdogInterrupt
) > 0) {
391 Status
= mInterrupt
->RegisterInterruptSource (
393 PcdGet32 (PcdSP805WatchdogInterrupt
),
394 SP805InterruptHandler
396 if (EFI_ERROR (Status
)) {
399 "%a: failed to register watchdog interrupt - %r\n",
408 "%a: no interrupt specified, running in RESET mode only\n",
414 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
415 // This will avoid conflicts with the universal watchdog
417 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
419 // Register for an ExitBootServicesEvent
420 Status
= gBS
->CreateEvent (
421 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
423 ExitBootServicesEvent
,
425 &mEfiExitBootServicesEvent
427 if (EFI_ERROR (Status
)) {
428 Status
= EFI_OUT_OF_RESOURCES
;
432 // Install the Timer Architectural Protocol onto a new handle
434 Status
= gBS
->InstallMultipleProtocolInterfaces (
436 &gEfiWatchdogTimerArchProtocolGuid
,
440 if (EFI_ERROR (Status
)) {
441 Status
= EFI_OUT_OF_RESOURCES
;
446 ASSERT_EFI_ERROR (Status
);