3 * Copyright (c) 2011, 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 License
7 * which accompanies this distribution. The full text of the license may be found at
8 * 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.
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/IoLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeServicesTableLib.h>
25 #include <Library/UefiLib.h>
27 #include <Protocol/WatchdogTimer.h>
28 #include <Drivers/SP805Watchdog.h>
30 EFI_EVENT EfiExitBootServicesEvent
= (EFI_EVENT
)NULL
;
32 BOOLEAN mSP805Initialized
= FALSE
;
39 // Check if this is a PrimeCell Peripheral
40 if ( (MmioRead8 (SP805_WDOG_PCELL_ID0
) != 0x0D)
41 || (MmioRead8 (SP805_WDOG_PCELL_ID1
) != 0xF0)
42 || (MmioRead8 (SP805_WDOG_PCELL_ID2
) != 0x05)
43 || (MmioRead8 (SP805_WDOG_PCELL_ID3
) != 0xB1)) {
47 // Check if this PrimeCell Peripheral is the SP805 Watchdog Timer
48 if ( (MmioRead8 (SP805_WDOG_PERIPH_ID0
) != 0x05)
49 || (MmioRead8 (SP805_WDOG_PERIPH_ID1
) != 0x18)
50 || ((MmioRead8 (SP805_WDOG_PERIPH_ID2
) & 0x0000000F) != 0x04)
51 || (MmioRead8 (SP805_WDOG_PERIPH_ID3
) != 0x00)) {
59 Make sure the SP805 registers are unlocked for writing.
61 Note: The SP805 Watchdog Timer supports locking of its registers,
62 i.e. it inhibits all writes to avoid rogue software accidentally
63 corrupting their contents.
71 if( MmioRead32(SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_LOCKED
) {
72 MmioWrite32(SP805_WDOG_LOCK_REG
, SP805_WDOG_SPECIAL_UNLOCK_CODE
);
77 Make sure the SP805 registers are locked and can not be overwritten.
79 Note: The SP805 Watchdog Timer supports locking of its registers,
80 i.e. it inhibits all writes to avoid rogue software accidentally
81 corrupting their contents.
89 if( MmioRead32(SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_UNLOCKED
) {
90 // To lock it, just write in any number (except the special unlock code).
91 MmioWrite32(SP805_WDOG_LOCK_REG
, SP805_WDOG_LOCK_IS_LOCKED
);
96 Stop the SP805 watchdog timer from counting down by disabling interrupts.
104 // Disable interrupts
105 if ( (MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) != 0 ) {
106 MmioAnd32(SP805_WDOG_CONTROL_REG
, ~SP805_WDOG_CTRL_INTEN
);
111 Starts the SP805 counting down by enabling interrupts.
112 The count down will start from the value stored in the Load register,
113 not from the value where it was previously stopped.
122 if ( (MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0 ) {
123 MmioOr32(SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_INTEN
);
128 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.
172 SP805RegisterHandler (
173 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
174 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
177 // ERROR: This function is not supported.
178 // The hardware watchdog will reset the board
179 return EFI_INVALID_PARAMETER
;
184 This function adjusts the period of timer interrupts to the value specified
185 by TimerPeriod. If the timer period is updated, then the selected timer
186 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
187 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
188 If an error occurs while attempting to update the timer period, then the
189 timer hardware will be put back in its state prior to this call, and
190 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
191 is disabled. This is not the same as disabling the CPU's interrupts.
192 Instead, it must either turn off the timer hardware, or it must adjust the
193 interrupt controller so that a CPU interrupt is not generated when the timer
196 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
197 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
198 the timer hardware is not programmable, then EFI_UNSUPPORTED is
199 returned. If the timer is programmable, then the timer period
200 will be rounded up to the nearest timer period that is supported
201 by the timer hardware. If TimerPeriod is set to 0, then the
202 timer interrupts will be disabled.
205 @retval EFI_SUCCESS The timer period was changed.
206 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
207 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
212 SP805SetTimerPeriod (
213 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
214 IN UINT64 TimerPeriod
// In 100ns units
217 EFI_STATUS Status
= EFI_SUCCESS
;
220 // Initialize the hardware if not already done
221 if( !mSP805Initialized
) {
222 Status
= SP805Initialize();
223 if (EFI_ERROR(Status
)) {
230 if( TimerPeriod
== 0 ) {
231 // This is a watchdog stop request
235 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
236 // The SP805 will count down to ZERO once, generate an interrupt and
237 // then it will again reload the initial value and start again.
238 // On the second time when it reaches ZERO, it will actually reset the board.
239 // Therefore, we need to load half the required delay.
241 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz) / 2 ;
245 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 20 MHz ;
247 Ticks64bit
= DivU64x32(MultU64x32(TimerPeriod
, (UINTN
)PcdGet32(PcdSP805WatchdogClockFrequencyInHz
)), 20000000);
249 // The registers in the SP805 are only 32 bits
250 if(Ticks64bit
> (UINT64
)0xFFFFFFFF) {
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
267 // Ensure the watchdog is locked before exiting.
273 This function retrieves the period of timer interrupts in 100 ns units,
274 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
275 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
276 returned, then the timer is currently disabled.
278 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
279 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
280 0 is returned, then the timer is currently disabled.
283 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
284 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
289 SP805GetTimerPeriod (
290 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
291 OUT UINT64
*TimerPeriod
294 EFI_STATUS Status
= EFI_SUCCESS
;
297 if (TimerPeriod
== NULL
) {
298 return EFI_INVALID_PARAMETER
;
301 // Initialize the hardware if not already done
302 if( !mSP805Initialized
) {
303 Status
= SP805Initialize();
304 if (EFI_ERROR(Status
)) {
309 // Check if the watchdog is stopped
310 if ( (MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0 ) {
311 // It is stopped, so return zero.
314 // Convert the Watchdog ticks into TimerPeriod
315 // Ensure 64bit arithmetic throughout because the Watchdog ticks may already
316 // be at the maximum 32 bit value and we still need to multiply that by 600.
317 ReturnValue
= MultU64x32( MmioRead32(SP805_WDOG_LOAD_REG
), 600 );
320 *TimerPeriod
= ReturnValue
;
326 Interface structure for the Watchdog Architectural Protocol.
328 @par Protocol Description:
329 This protocol provides a service to set the amount of time to wait
330 before firing the watchdog timer, and it also provides a service to
331 register a handler that is invoked when the watchdog timer fires.
333 @par When the watchdog timer fires, control will be passed to a handler
334 if one has been registered. If no handler has been registered,
335 or the registered handler returns, then the system will be
336 reset by calling the Runtime Service ResetSystem().
338 @param RegisterHandler
339 Registers a handler that will be called each time the
340 watchdogtimer interrupt fires. TimerPeriod defines the minimum
341 time between timer interrupts, so TimerPeriod will also
342 be the minimum time between calls to the registered
344 NOTE: If the watchdog resets the system in hardware, then
345 this function will not have any chance of executing.
347 @param SetTimerPeriod
348 Sets the period of the timer interrupt in 100 nS units.
349 This function is optional, and may return EFI_UNSUPPORTED.
350 If this function is supported, then the timer period will
351 be rounded up to the nearest supported timer period.
353 @param GetTimerPeriod
354 Retrieves the period of the timer interrupt in 100 nS units.
357 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL gWatchdogTimer
= {
358 (EFI_WATCHDOG_TIMER_REGISTER_HANDLER
) SP805RegisterHandler
,
359 (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD
) SP805SetTimerPeriod
,
360 (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD
) SP805GetTimerPeriod
364 Initialize the state information for the Watchdog Timer Architectural Protocol.
366 @param ImageHandle of the loaded driver
367 @param SystemTable Pointer to the System Table
369 @retval EFI_SUCCESS Protocol registered
370 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
371 @retval EFI_DEVICE_ERROR Hardware problems
381 // Check if the SP805 hardware watchdog module exists on board
382 Status
= SP805Identify();
383 if (EFI_ERROR(Status
)) {
384 Status
= EFI_DEVICE_ERROR
;
388 // Unlock access to the SP805 registers
391 // Stop the watchdog from triggering unexpectedly
394 // Set the watchdog to reset the board when triggered
395 if ((MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_RESEN
) == 0) {
396 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_RESEN
);
399 // Prohibit any rogue access to SP805 registers
402 mSP805Initialized
= TRUE
;
410 SP805InstallProtocol (
411 IN EFI_HANDLE ImageHandle
,
412 IN EFI_SYSTEM_TABLE
*SystemTable
419 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
420 // This will avoid conflicts with the universal watchdog
422 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
424 // Register for an ExitBootServicesEvent
425 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
, ExitBootServicesEvent
, NULL
, &EfiExitBootServicesEvent
);
426 if (EFI_ERROR(Status
)) {
427 Status
= EFI_OUT_OF_RESOURCES
;
431 // Install the Timer Architectural Protocol onto a new handle
433 Status
= gBS
->InstallMultipleProtocolInterfaces(
435 &gEfiWatchdogTimerArchProtocolGuid
, &gWatchdogTimer
,
438 if (EFI_ERROR(Status
)) {
439 Status
= EFI_OUT_OF_RESOURCES
;
444 if(EFI_ERROR(Status
)) {
445 // The watchdog failed to initialize