3 * Copyright (c) 2011-2012, 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
;
37 // Check if this is a PrimeCell Peripheral
38 if ( (MmioRead8 (SP805_WDOG_PCELL_ID0
) != 0x0D)
39 || (MmioRead8 (SP805_WDOG_PCELL_ID1
) != 0xF0)
40 || (MmioRead8 (SP805_WDOG_PCELL_ID2
) != 0x05)
41 || (MmioRead8 (SP805_WDOG_PCELL_ID3
) != 0xB1)) {
45 // Check if this PrimeCell Peripheral is the SP805 Watchdog Timer
46 if ( (MmioRead8 (SP805_WDOG_PERIPH_ID0
) != 0x05)
47 || (MmioRead8 (SP805_WDOG_PERIPH_ID1
) != 0x18)
48 || ((MmioRead8 (SP805_WDOG_PERIPH_ID2
) & 0x0000000F) != 0x04)
49 || (MmioRead8 (SP805_WDOG_PERIPH_ID3
) != 0x00)) {
57 Make sure the SP805 registers are unlocked for writing.
59 Note: The SP805 Watchdog Timer supports locking of its registers,
60 i.e. it inhibits all writes to avoid rogue software accidentally
61 corrupting their contents.
69 if( MmioRead32(SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_LOCKED
) {
70 MmioWrite32(SP805_WDOG_LOCK_REG
, SP805_WDOG_SPECIAL_UNLOCK_CODE
);
75 Make sure the SP805 registers are locked and can not be overwritten.
77 Note: The SP805 Watchdog Timer supports locking of its registers,
78 i.e. it inhibits all writes to avoid rogue software accidentally
79 corrupting their contents.
87 if( MmioRead32(SP805_WDOG_LOCK_REG
) == SP805_WDOG_LOCK_IS_UNLOCKED
) {
88 // To lock it, just write in any number (except the special unlock code).
89 MmioWrite32(SP805_WDOG_LOCK_REG
, SP805_WDOG_LOCK_IS_LOCKED
);
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
131 ExitBootServicesEvent (
142 This function registers the handler NotifyFunction so it is called every time
143 the watchdog timer expires. It also passes the amount of time since the last
144 handler call to the NotifyFunction.
145 If NotifyFunction is not NULL and a handler is not already registered,
146 then the new handler is registered and EFI_SUCCESS is returned.
147 If NotifyFunction is NULL, and a handler is already registered,
148 then that handler is unregistered.
149 If an attempt is made to register a handler when a handler is already registered,
150 then EFI_ALREADY_STARTED is returned.
151 If an attempt is made to unregister a handler when a handler is not registered,
152 then EFI_INVALID_PARAMETER is returned.
154 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
155 @param NotifyFunction The function to call when a timer interrupt fires. This
156 function executes at TPL_HIGH_LEVEL. The DXE Core will
157 register a handler for the timer interrupt, so it can know
158 how much time has passed. This information is used to
159 signal timer based events. NULL will unregister the handler.
161 @retval EFI_SUCCESS The watchdog timer handler was registered.
162 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
164 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
165 previously registered.
170 SP805RegisterHandler (
171 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
172 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
175 // ERROR: This function is not supported.
176 // The hardware watchdog will reset the board
177 return EFI_INVALID_PARAMETER
;
182 This function adjusts the period of timer interrupts to the value specified
183 by TimerPeriod. If the timer period is updated, then the selected timer
184 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
185 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
186 If an error occurs while attempting to update the timer period, then the
187 timer hardware will be put back in its state prior to this call, and
188 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
189 is disabled. This is not the same as disabling the CPU's interrupts.
190 Instead, it must either turn off the timer hardware, or it must adjust the
191 interrupt controller so that a CPU interrupt is not generated when the timer
194 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
195 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
196 the timer hardware is not programmable, then EFI_UNSUPPORTED is
197 returned. If the timer is programmable, then the timer period
198 will be rounded up to the nearest timer period that is supported
199 by the timer hardware. If TimerPeriod is set to 0, then the
200 timer interrupts will be disabled.
203 @retval EFI_SUCCESS The timer period was changed.
204 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
205 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
210 SP805SetTimerPeriod (
211 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
212 IN UINT64 TimerPeriod
// In 100ns units
215 EFI_STATUS Status
= EFI_SUCCESS
;
220 if( TimerPeriod
== 0 ) {
221 // This is a watchdog stop request
225 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
226 // The SP805 will count down to ZERO once, generate an interrupt and
227 // then it will again reload the initial value and start again.
228 // On the second time when it reaches ZERO, it will actually reset the board.
229 // Therefore, we need to load half the required delay.
231 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz) / 2 ;
235 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 20 MHz ;
237 Ticks64bit
= DivU64x32(MultU64x32(TimerPeriod
, (UINTN
)PcdGet32(PcdSP805WatchdogClockFrequencyInHz
)), 20000000);
239 // The registers in the SP805 are only 32 bits
240 if(Ticks64bit
> (UINT64
)0xFFFFFFFF) {
241 // We could load the watchdog with the maximum supported value but
242 // if a smaller value was requested, this could have the watchdog
243 // triggering before it was intended.
244 // Better generate an error to let the caller know.
245 Status
= EFI_DEVICE_ERROR
;
249 // Update the watchdog with a 32-bit value.
250 MmioWrite32(SP805_WDOG_LOAD_REG
, (UINT32
)Ticks64bit
);
252 // Start the watchdog
257 // Ensure the watchdog is locked before exiting.
263 This function retrieves the period of timer interrupts in 100 ns units,
264 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
265 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
266 returned, then the timer is currently disabled.
268 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
269 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
270 0 is returned, then the timer is currently disabled.
273 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
274 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
279 SP805GetTimerPeriod (
280 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
281 OUT UINT64
*TimerPeriod
284 EFI_STATUS Status
= EFI_SUCCESS
;
287 if (TimerPeriod
== NULL
) {
288 return EFI_INVALID_PARAMETER
;
291 // Check if the watchdog is stopped
292 if ( (MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_INTEN
) == 0 ) {
293 // It is stopped, so return zero.
296 // Convert the Watchdog ticks into TimerPeriod
297 // Ensure 64bit arithmetic throughout because the Watchdog ticks may already
298 // be at the maximum 32 bit value and we still need to multiply that by 600.
299 ReturnValue
= MultU64x32( MmioRead32(SP805_WDOG_LOAD_REG
), 600 );
302 *TimerPeriod
= ReturnValue
;
308 Interface structure for the Watchdog Architectural Protocol.
310 @par Protocol Description:
311 This protocol provides a service to set the amount of time to wait
312 before firing the watchdog timer, and it also provides a service to
313 register a handler that is invoked when the watchdog timer fires.
315 @par When the watchdog timer fires, control will be passed to a handler
316 if one has been registered. If no handler has been registered,
317 or the registered handler returns, then the system will be
318 reset by calling the Runtime Service ResetSystem().
320 @param RegisterHandler
321 Registers a handler that will be called each time the
322 watchdogtimer interrupt fires. TimerPeriod defines the minimum
323 time between timer interrupts, so TimerPeriod will also
324 be the minimum time between calls to the registered
326 NOTE: If the watchdog resets the system in hardware, then
327 this function will not have any chance of executing.
329 @param SetTimerPeriod
330 Sets the period of the timer interrupt in 100 nS units.
331 This function is optional, and may return EFI_UNSUPPORTED.
332 If this function is supported, then the timer period will
333 be rounded up to the nearest supported timer period.
335 @param GetTimerPeriod
336 Retrieves the period of the timer interrupt in 100 nS units.
339 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL gWatchdogTimer
= {
340 (EFI_WATCHDOG_TIMER_REGISTER_HANDLER
) SP805RegisterHandler
,
341 (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD
) SP805SetTimerPeriod
,
342 (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD
) SP805GetTimerPeriod
346 Initialize the state information for the Watchdog Timer Architectural Protocol.
348 @param ImageHandle of the loaded driver
349 @param SystemTable Pointer to the System Table
351 @retval EFI_SUCCESS Protocol registered
352 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
353 @retval EFI_DEVICE_ERROR Hardware problems
359 IN EFI_HANDLE ImageHandle
,
360 IN EFI_SYSTEM_TABLE
*SystemTable
366 // Check if the SP805 hardware watchdog module exists on board
367 Status
= SP805Identify();
368 if (EFI_ERROR(Status
)) {
369 Status
= EFI_DEVICE_ERROR
;
373 // Unlock access to the SP805 registers
376 // Stop the watchdog from triggering unexpectedly
379 // Set the watchdog to reset the board when triggered
380 if ((MmioRead32(SP805_WDOG_CONTROL_REG
) & SP805_WDOG_CTRL_RESEN
) == 0) {
381 MmioOr32 (SP805_WDOG_CONTROL_REG
, SP805_WDOG_CTRL_RESEN
);
384 // Prohibit any rogue access to SP805 registers
388 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
389 // This will avoid conflicts with the universal watchdog
391 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
393 // Register for an ExitBootServicesEvent
394 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
, ExitBootServicesEvent
, NULL
, &EfiExitBootServicesEvent
);
395 if (EFI_ERROR(Status
)) {
396 Status
= EFI_OUT_OF_RESOURCES
;
400 // Install the Timer Architectural Protocol onto a new handle
402 Status
= gBS
->InstallMultipleProtocolInterfaces(
404 &gEfiWatchdogTimerArchProtocolGuid
, &gWatchdogTimer
,
407 if (EFI_ERROR(Status
)) {
408 Status
= EFI_OUT_OF_RESOURCES
;
413 if(EFI_ERROR(Status
)) {
414 // The watchdog failed to initialize