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
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/IoLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiRuntimeServicesTableLib.h>
20 #include <Protocol/HardwareInterrupt.h>
21 #include <Protocol/WatchdogTimer.h>
23 #include "SP805Watchdog.h"
25 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
26 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL
*mInterrupt
;
27 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify
;
28 STATIC UINT32 mTimerPeriod
;
31 Make sure the SP805 registers are unlocked for writing.
33 Note: The SP805 Watchdog Timer supports locking of its registers,
34 i.e. it inhibits all writes to avoid rogue software accidentally
35 corrupting their contents.
43 if (MmioRead32 (SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_LOCKED
) {
44 MmioWrite32 (SP805_WDOG_LOCK_REG
, SP805_WDOG_SPECIAL_UNLOCK_CODE
);
49 Make sure the SP805 registers are locked and can not be overwritten.
51 Note: The SP805 Watchdog Timer supports locking of its registers,
52 i.e. it inhibits all writes to avoid rogue software accidentally
53 corrupting their contents.
61 if (MmioRead32 (SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_UNLOCKED
) {
62 // To lock it, just write in any number (except the special unlock code).
63 MmioWrite32 (SP805_WDOG_LOCK_REG
, SP805_WDOG_LOCK_IS_LOCKED
);
70 SP805InterruptHandler (
71 IN HARDWARE_INTERRUPT_SOURCE Source
,
72 IN EFI_SYSTEM_CONTEXT SystemContext
76 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
79 mInterrupt
->EndOfInterrupt (mInterrupt
, Source
);
82 // The notify function should be called with the elapsed number of ticks
83 // since the watchdog was armed, which should exceed the timer period.
84 // We don't actually know the elapsed number of ticks, so let's return
85 // the timer period plus 1.
87 if (mWatchdogNotify
!= NULL
) {
88 mWatchdogNotify (mTimerPeriod
+ 1);
91 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, 0, NULL
);
95 Stop the SP805 watchdog timer from counting down by disabling interrupts.
103 // Disable interrupts
104 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) != 0) {
105 MmioAnd32 (SP805_WDOG_CONTROL_REG
, ~SP805_WDOG_CTRL_INTEN
);
110 Starts the SP805 counting down by enabling interrupts.
111 The count down will start from the value stored in the Load register,
112 not from the value where it was previously stopped.
121 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0) {
122 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_INTEN
);
127 On exiting boot services we must make sure the SP805 Watchdog Timer
133 ExitBootServicesEvent (
144 This function registers the handler NotifyFunction so it is called every time
145 the watchdog timer expires. It also passes the amount of time since the last
146 handler call to the NotifyFunction.
147 If NotifyFunction is not NULL and a handler is not already registered,
148 then the new handler is registered and EFI_SUCCESS is returned.
149 If NotifyFunction is NULL, and a handler is already registered,
150 then that handler is unregistered.
151 If an attempt is made to register a handler when a handler is already registered,
152 then EFI_ALREADY_STARTED is returned.
153 If an attempt is made to unregister a handler when a handler is not registered,
154 then EFI_INVALID_PARAMETER is returned.
156 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
157 @param NotifyFunction The function to call when a timer interrupt fires. This
158 function executes at TPL_HIGH_LEVEL. The DXE Core will
159 register a handler for the timer interrupt, so it can know
160 how much time has passed. This information is used to
161 signal timer based events. NULL will unregister the handler.
163 @retval EFI_SUCCESS The watchdog timer handler was registered.
164 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
166 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
167 previously registered.
173 SP805RegisterHandler (
174 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
175 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
178 if (mWatchdogNotify
== NULL
&& NotifyFunction
== NULL
) {
179 return EFI_INVALID_PARAMETER
;
182 if (mWatchdogNotify
!= NULL
&& NotifyFunction
!= NULL
) {
183 return EFI_ALREADY_STARTED
;
186 mWatchdogNotify
= NotifyFunction
;
192 This function adjusts the period of timer interrupts to the value specified
193 by TimerPeriod. If the timer period is updated, then the selected timer
194 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
195 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
196 If an error occurs while attempting to update the timer period, then the
197 timer hardware will be put back in its state prior to this call, and
198 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
199 is disabled. This is not the same as disabling the CPU's interrupts.
200 Instead, it must either turn off the timer hardware, or it must adjust the
201 interrupt controller so that a CPU interrupt is not generated when the timer
204 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
205 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
206 the timer hardware is not programmable, then EFI_UNSUPPORTED is
207 returned. If the timer is programmable, then the timer period
208 will be rounded up to the nearest timer period that is supported
209 by the timer hardware. If TimerPeriod is set to 0, then the
210 timer interrupts will be disabled.
213 @retval EFI_SUCCESS The timer period was changed.
214 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
215 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
221 SP805SetTimerPeriod (
222 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
223 IN UINT64 TimerPeriod
// In 100ns units
231 Status
= EFI_SUCCESS
;
233 if (TimerPeriod
== 0) {
234 // This is a watchdog stop request
237 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
238 // The SP805 will count down to zero and generate an interrupt.
240 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
244 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
246 Ticks64bit
= MultU64x32 (TimerPeriod
, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz
));
247 Ticks64bit
= DivU64x32 (Ticks64bit
, 10 * 1000 * 1000);
249 // The registers in the SP805 are only 32 bits
250 if (Ticks64bit
> MAX_UINT32
) {
251 // We could load the watchdog with the maximum supported value but
252 // if a smaller value was requested, this could have the watchdog
253 // triggering before it was intended.
254 // Better generate an error to let the caller know.
255 Status
= EFI_DEVICE_ERROR
;
259 // Update the watchdog with a 32-bit value.
260 MmioWrite32 (SP805_WDOG_LOAD_REG
, (UINT32
)Ticks64bit
);
262 // Start the watchdog
266 mTimerPeriod
= TimerPeriod
;
269 // Ensure the watchdog is locked before exiting.
271 ASSERT_EFI_ERROR (Status
);
276 This function retrieves the period of timer interrupts in 100 ns units,
277 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
278 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
279 returned, then the timer is currently disabled.
281 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
282 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
283 0 is returned, then the timer is currently disabled.
286 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
287 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
293 SP805GetTimerPeriod (
294 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
295 OUT UINT64
*TimerPeriod
298 if (TimerPeriod
== NULL
) {
299 return EFI_INVALID_PARAMETER
;
302 *TimerPeriod
= mTimerPeriod
;
307 Interface structure for the Watchdog Architectural Protocol.
309 @par Protocol Description:
310 This protocol provides a service to set the amount of time to wait
311 before firing the watchdog timer, and it also provides a service to
312 register a handler that is invoked when the watchdog timer fires.
314 @par When the watchdog timer fires, control will be passed to a handler
315 if one has been registered. If no handler has been registered,
316 or the registered handler returns, then the system will be
317 reset by calling the Runtime Service ResetSystem().
319 @param RegisterHandler
320 Registers a handler that will be called each time the
321 watchdogtimer interrupt fires. TimerPeriod defines the minimum
322 time between timer interrupts, so TimerPeriod will also
323 be the minimum time between calls to the registered
325 NOTE: If the watchdog resets the system in hardware, then
326 this function will not have any chance of executing.
328 @param SetTimerPeriod
329 Sets the period of the timer interrupt in 100 nS units.
330 This function is optional, and may return EFI_UNSUPPORTED.
331 If this function is supported, then the timer period will
332 be rounded up to the nearest supported timer period.
334 @param GetTimerPeriod
335 Retrieves the period of the timer interrupt in 100 nS units.
338 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
339 SP805RegisterHandler
,
345 Initialize the state information for the Watchdog Timer Architectural Protocol.
347 @param ImageHandle of the loaded driver
348 @param SystemTable Pointer to the System Table
350 @retval EFI_SUCCESS Protocol registered
351 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
352 @retval EFI_DEVICE_ERROR Hardware problems
358 IN EFI_HANDLE ImageHandle
,
359 IN EFI_SYSTEM_TABLE
*SystemTable
365 // Find the interrupt controller protocol. ASSERT if not found.
366 Status
= gBS
->LocateProtocol (&gHardwareInterruptProtocolGuid
, NULL
,
367 (VOID
**)&mInterrupt
);
368 ASSERT_EFI_ERROR (Status
);
370 // Unlock access to the SP805 registers
373 // Stop the watchdog from triggering unexpectedly
376 // Set the watchdog to reset the board when triggered
377 // This is a last resort in case the interrupt handler fails
378 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_RESEN
) == 0) {
379 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_RESEN
);
382 // Clear any pending interrupts
383 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
385 // Prohibit any rogue access to SP805 registers
388 if (PcdGet32 (PcdSP805WatchdogInterrupt
) > 0) {
389 Status
= mInterrupt
->RegisterInterruptSource (mInterrupt
,
390 PcdGet32 (PcdSP805WatchdogInterrupt
),
391 SP805InterruptHandler
);
392 if (EFI_ERROR (Status
)) {
393 DEBUG ((DEBUG_ERROR
, "%a: failed to register watchdog interrupt - %r\n",
394 __FUNCTION__
, Status
));
398 DEBUG ((DEBUG_WARN
, "%a: no interrupt specified, running in RESET mode only\n",
403 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
404 // This will avoid conflicts with the universal watchdog
406 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
408 // Register for an ExitBootServicesEvent
409 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
,
410 ExitBootServicesEvent
, NULL
, &mEfiExitBootServicesEvent
);
411 if (EFI_ERROR (Status
)) {
412 Status
= EFI_OUT_OF_RESOURCES
;
416 // Install the Timer Architectural Protocol onto a new handle
418 Status
= gBS
->InstallMultipleProtocolInterfaces (
420 &gEfiWatchdogTimerArchProtocolGuid
, &mWatchdogTimer
,
423 if (EFI_ERROR (Status
)) {
424 Status
= EFI_OUT_OF_RESOURCES
;
429 ASSERT_EFI_ERROR (Status
);