3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 * Copyright (c) 2018, Linaro Limited. All rights reserved.
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
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.
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>
26 #include <Protocol/HardwareInterrupt.h>
27 #include <Protocol/WatchdogTimer.h>
29 #include "SP805Watchdog.h"
31 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
32 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL
*mInterrupt
;
33 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify
;
34 STATIC UINT32 mTimerPeriod
;
37 Make sure the SP805 registers are unlocked for writing.
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.
49 if (MmioRead32 (SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_LOCKED
) {
50 MmioWrite32 (SP805_WDOG_LOCK_REG
, SP805_WDOG_SPECIAL_UNLOCK_CODE
);
55 Make sure the SP805 registers are locked and can not be overwritten.
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.
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
);
76 SP805InterruptHandler (
77 IN HARDWARE_INTERRUPT_SOURCE Source
,
78 IN EFI_SYSTEM_CONTEXT SystemContext
82 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
85 mInterrupt
->EndOfInterrupt (mInterrupt
, Source
);
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.
93 if (mWatchdogNotify
!= NULL
) {
94 mWatchdogNotify (mTimerPeriod
+ 1);
97 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, 0, NULL
);
101 Stop the SP805 watchdog timer from counting down by disabling interrupts.
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
);
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.
127 if ((MmioRead32 (SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0) {
128 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_INTEN
);
133 On exiting boot services we must make sure the SP805 Watchdog Timer
139 ExitBootServicesEvent (
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.
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.
169 @retval EFI_SUCCESS The watchdog timer handler was registered.
170 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
172 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
173 previously registered.
179 SP805RegisterHandler (
180 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
181 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
184 if (mWatchdogNotify
== NULL
&& NotifyFunction
== NULL
) {
185 return EFI_INVALID_PARAMETER
;
188 if (mWatchdogNotify
!= NULL
&& NotifyFunction
!= NULL
) {
189 return EFI_ALREADY_STARTED
;
192 mWatchdogNotify
= NotifyFunction
;
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
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.
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.
227 SP805SetTimerPeriod (
228 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
229 IN UINT64 TimerPeriod
// In 100ns units
237 Status
= EFI_SUCCESS
;
239 if (TimerPeriod
== 0) {
240 // This is a watchdog stop request
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.
246 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
250 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
252 Ticks64bit
= MultU64x32 (TimerPeriod
, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz
));
253 Ticks64bit
= DivU64x32 (Ticks64bit
, 10 * 1000 * 1000);
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
;
265 // Update the watchdog with a 32-bit value.
266 MmioWrite32 (SP805_WDOG_LOAD_REG
, (UINT32
)Ticks64bit
);
268 // Start the watchdog
272 mTimerPeriod
= TimerPeriod
;
275 // Ensure the watchdog is locked before exiting.
277 ASSERT_EFI_ERROR (Status
);
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.
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.
292 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
293 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
299 SP805GetTimerPeriod (
300 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
301 OUT UINT64
*TimerPeriod
304 if (TimerPeriod
== NULL
) {
305 return EFI_INVALID_PARAMETER
;
308 *TimerPeriod
= mTimerPeriod
;
313 Interface structure for the Watchdog Architectural Protocol.
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.
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().
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
331 NOTE: If the watchdog resets the system in hardware, then
332 this function will not have any chance of executing.
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.
340 @param GetTimerPeriod
341 Retrieves the period of the timer interrupt in 100 nS units.
344 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
345 SP805RegisterHandler
,
351 Initialize the state information for the Watchdog Timer Architectural Protocol.
353 @param ImageHandle of the loaded driver
354 @param SystemTable Pointer to the System Table
356 @retval EFI_SUCCESS Protocol registered
357 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
358 @retval EFI_DEVICE_ERROR Hardware problems
364 IN EFI_HANDLE ImageHandle
,
365 IN EFI_SYSTEM_TABLE
*SystemTable
371 // Find the interrupt controller protocol. ASSERT if not found.
372 Status
= gBS
->LocateProtocol (&gHardwareInterruptProtocolGuid
, NULL
,
373 (VOID
**)&mInterrupt
);
374 ASSERT_EFI_ERROR (Status
);
376 // Unlock access to the SP805 registers
379 // Stop the watchdog from triggering unexpectedly
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
);
388 // Clear any pending interrupts
389 MmioWrite32 (SP805_WDOG_INT_CLR_REG
, 0); // write of any value clears the irq
391 // Prohibit any rogue access to SP805 registers
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
));
404 DEBUG ((DEBUG_WARN
, "%a: no interrupt specified, running in RESET mode only\n",
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
412 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
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
;
422 // Install the Timer Architectural Protocol onto a new handle
424 Status
= gBS
->InstallMultipleProtocolInterfaces (
426 &gEfiWatchdogTimerArchProtocolGuid
, &mWatchdogTimer
,
429 if (EFI_ERROR (Status
)) {
430 Status
= EFI_OUT_OF_RESOURCES
;
435 ASSERT_EFI_ERROR (Status
);