3 * Copyright (c) 2013-2018, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD
7 * License which accompanies this distribution. The full text of the license
8 * may be found at http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/ArmGenericTimerCounterLib.h>
27 #include <Protocol/HardwareInterrupt2.h>
28 #include <Protocol/WatchdogTimer.h>
30 #include "GenericWatchdog.h"
32 /* The number of 100ns periods (the unit of time passed to these functions)
34 #define TIME_UNITS_PER_SECOND 10000000
36 // Tick frequency of the generic timer basis of the generic watchdog.
37 STATIC UINTN mTimerFrequencyHz
= 0;
39 /* In cases where the compare register was set manually, information about
40 how long the watchdog was asked to wait cannot be retrieved from hardware.
41 It is therefore stored here. 0 means the timer is not running. */
42 STATIC UINT64 mNumTimerTicks
= 0;
44 STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL
*mInterruptProtocol
;
48 WatchdogWriteOffsetRegister (
52 MmioWrite32 (GENERIC_WDOG_OFFSET_REG
, Value
);
57 WatchdogWriteCompareRegister (
61 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW
, Value
& MAX_UINT32
);
62 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH
, (Value
>> 32) & MAX_UINT32
);
71 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG
, GENERIC_WDOG_ENABLED
);
80 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG
, GENERIC_WDOG_DISABLED
);
83 /** On exiting boot services we must make sure the Watchdog Timer
89 WatchdogExitBootServicesEvent (
98 /* This function is called when the watchdog's first signal (WS0) goes high.
99 It uses the ResetSystem Runtime Service to reset the board.
104 WatchdogInterruptHandler (
105 IN HARDWARE_INTERRUPT_SOURCE Source
,
106 IN EFI_SYSTEM_CONTEXT SystemContext
109 STATIC CONST CHAR16 ResetString
[]= L
"The generic watchdog timer ran out.";
113 mInterruptProtocol
->EndOfInterrupt (mInterruptProtocol
, Source
);
118 StrSize (ResetString
),
119 (VOID
*) &ResetString
122 // If we got here then the reset didn't work
127 This function registers the handler NotifyFunction so it is called every time
128 the watchdog timer expires. It also passes the amount of time since the last
129 handler call to the NotifyFunction.
130 If NotifyFunction is not NULL and a handler is not already registered,
131 then the new handler is registered and EFI_SUCCESS is returned.
132 If NotifyFunction is NULL, and a handler is already registered,
133 then that handler is unregistered.
134 If an attempt is made to register a handler when a handler is already
135 registered, then EFI_ALREADY_STARTED is returned.
136 If an attempt is made to unregister a handler when a handler is not
137 registered, then EFI_INVALID_PARAMETER is returned.
139 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
140 @param NotifyFunction The function to call when a timer interrupt fires.
141 This function executes at TPL_HIGH_LEVEL. The DXE
142 Core will register a handler for the timer interrupt,
143 so it can know how much time has passed. This
144 information is used to signal timer based events.
145 NULL will unregister the handler.
147 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
153 WatchdogRegisterHandler (
154 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
155 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
158 // ERROR: This function is not supported.
159 // The watchdog will reset the board
160 return EFI_UNSUPPORTED
;
164 This function sets the amount of time to wait before firing the watchdog
165 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog
168 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
169 @param TimerPeriod The amount of time in 100ns units to wait before
170 the watchdog timer is fired. If TimerPeriod is zero,
171 then the watchdog timer is disabled.
173 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
174 in TimerPeriod 100ns units.
180 WatchdogSetTimerPeriod (
181 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
182 IN UINT64 TimerPeriod
// In 100ns units
187 // if TimerPeriod is 0, this is a request to stop the watchdog.
188 if (TimerPeriod
== 0) {
194 // Work out how many timer ticks will equate to TimerPeriod
195 mNumTimerTicks
= (mTimerFrequencyHz
* TimerPeriod
) / TIME_UNITS_PER_SECOND
;
197 /* If the number of required ticks is greater than the max the watchdog's
198 offset register (WOR) can hold, we need to manually compute and set
199 the compare register (WCV) */
200 if (mNumTimerTicks
> MAX_UINT32
) {
201 /* We need to enable the watchdog *before* writing to the compare register,
202 because enabling the watchdog causes an "explicit refresh", which
203 clobbers the compare register (WCV). In order to make sure this doesn't
204 trigger an interrupt, set the offset to max. */
205 WatchdogWriteOffsetRegister (MAX_UINT32
);
207 SystemCount
= ArmGenericTimerGetSystemCount ();
208 WatchdogWriteCompareRegister (SystemCount
+ mNumTimerTicks
);
210 WatchdogWriteOffsetRegister ((UINT32
)mNumTimerTicks
);
218 This function retrieves the period of timer interrupts in 100ns units,
219 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
220 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
221 returned, then the timer is currently disabled.
223 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
224 @param TimerPeriod A pointer to the timer period to retrieve in
225 100ns units. If 0 is returned, then the timer is
229 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
230 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
236 WatchdogGetTimerPeriod (
237 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
238 OUT UINT64
*TimerPeriod
241 if (TimerPeriod
== NULL
) {
242 return EFI_INVALID_PARAMETER
;
245 *TimerPeriod
= ((TIME_UNITS_PER_SECOND
/ mTimerFrequencyHz
) * mNumTimerTicks
);
251 Interface structure for the Watchdog Architectural Protocol.
253 @par Protocol Description:
254 This protocol provides a service to set the amount of time to wait
255 before firing the watchdog timer, and it also provides a service to
256 register a handler that is invoked when the watchdog timer fires.
258 @par When the watchdog timer fires, control will be passed to a handler
259 if one has been registered. If no handler has been registered,
260 or the registered handler returns, then the system will be
261 reset by calling the Runtime Service ResetSystem().
263 @param RegisterHandler
264 Registers a handler that will be called each time the
265 watchdogtimer interrupt fires. TimerPeriod defines the minimum
266 time between timer interrupts, so TimerPeriod will also
267 be the minimum time between calls to the registered
269 NOTE: If the watchdog resets the system in hardware, then
270 this function will not have any chance of executing.
272 @param SetTimerPeriod
273 Sets the period of the timer interrupt in 100ns units.
274 This function is optional, and may return EFI_UNSUPPORTED.
275 If this function is supported, then the timer period will
276 be rounded up to the nearest supported timer period.
278 @param GetTimerPeriod
279 Retrieves the period of the timer interrupt in 100ns units.
282 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
283 WatchdogRegisterHandler
,
284 WatchdogSetTimerPeriod
,
285 WatchdogGetTimerPeriod
288 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
292 GenericWatchdogEntry (
293 IN EFI_HANDLE ImageHandle
,
294 IN EFI_SYSTEM_TABLE
*SystemTable
300 Status
= gBS
->LocateProtocol (&gHardwareInterrupt2ProtocolGuid
, NULL
,
301 (VOID
**)&mInterruptProtocol
);
302 ASSERT_EFI_ERROR (Status
);
304 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
306 This will avoid conflicts with the universal watchdog */
307 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
309 mTimerFrequencyHz
= ArmGenericTimerGetTimerFreq ();
310 ASSERT (mTimerFrequencyHz
!= 0);
312 // Install interrupt handler
313 Status
= mInterruptProtocol
->RegisterInterruptSource (mInterruptProtocol
,
314 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
315 WatchdogInterruptHandler
);
316 if (EFI_ERROR (Status
)) {
320 Status
= mInterruptProtocol
->SetTriggerType (mInterruptProtocol
,
321 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
322 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING
);
323 if (EFI_ERROR (Status
)) {
324 goto UnregisterHandler
;
327 // Install the Timer Architectural Protocol onto a new handle
329 Status
= gBS
->InstallMultipleProtocolInterfaces (&Handle
,
330 &gEfiWatchdogTimerArchProtocolGuid
, &mWatchdogTimer
,
332 if (EFI_ERROR (Status
)) {
333 goto UnregisterHandler
;
336 // Register for an ExitBootServicesEvent
337 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
,
338 WatchdogExitBootServicesEvent
, NULL
,
339 &mEfiExitBootServicesEvent
);
340 ASSERT_EFI_ERROR (Status
);
348 // Unregister the handler
349 mInterruptProtocol
->RegisterInterruptSource (mInterruptProtocol
,
350 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),