]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c
ArmPlatformPkg: Apply uncrustify changes
[mirror_edk2.git] / ArmPlatformPkg / Drivers / SP805WatchdogDxe / SP805Watchdog.c
1 /** @file
2
3 Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 Copyright (c) 2018, Linaro Limited. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiDxe.h>
11
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/IoLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiRuntimeServicesTableLib.h>
18
19 #include <Protocol/HardwareInterrupt.h>
20 #include <Protocol/WatchdogTimer.h>
21
22 #include "SP805Watchdog.h"
23
24 STATIC EFI_EVENT mEfiExitBootServicesEvent;
25 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL *mInterrupt;
26 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;
27 STATIC UINT32 mTimerPeriod;
28
29 /**
30 Make sure the SP805 registers are unlocked for writing.
31
32 Note: The SP805 Watchdog Timer supports locking of its registers,
33 i.e. it inhibits all writes to avoid rogue software accidentally
34 corrupting their contents.
35 **/
36 STATIC
37 VOID
38 SP805Unlock (
39 VOID
40 )
41 {
42 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_LOCKED) {
43 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_SPECIAL_UNLOCK_CODE);
44 }
45 }
46
47 /**
48 Make sure the SP805 registers are locked and can not be overwritten.
49
50 Note: The SP805 Watchdog Timer supports locking of its registers,
51 i.e. it inhibits all writes to avoid rogue software accidentally
52 corrupting their contents.
53 **/
54 STATIC
55 VOID
56 SP805Lock (
57 VOID
58 )
59 {
60 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_UNLOCKED) {
61 // To lock it, just write in any number (except the special unlock code).
62 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_LOCK_IS_LOCKED);
63 }
64 }
65
66 STATIC
67 VOID
68 EFIAPI
69 SP805InterruptHandler (
70 IN HARDWARE_INTERRUPT_SOURCE Source,
71 IN EFI_SYSTEM_CONTEXT SystemContext
72 )
73 {
74 SP805Unlock ();
75 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
76 SP805Lock ();
77
78 mInterrupt->EndOfInterrupt (mInterrupt, Source);
79
80 //
81 // The notify function should be called with the elapsed number of ticks
82 // since the watchdog was armed, which should exceed the timer period.
83 // We don't actually know the elapsed number of ticks, so let's return
84 // the timer period plus 1.
85 //
86 if (mWatchdogNotify != NULL) {
87 mWatchdogNotify (mTimerPeriod + 1);
88 }
89
90 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
91 }
92
93 /**
94 Stop the SP805 watchdog timer from counting down by disabling interrupts.
95 **/
96 STATIC
97 VOID
98 SP805Stop (
99 VOID
100 )
101 {
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);
105 }
106 }
107
108 /**
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.
112 **/
113 STATIC
114 VOID
115 SP805Start (
116 VOID
117 )
118 {
119 // Enable interrupts
120 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) {
121 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_INTEN);
122 }
123 }
124
125 /**
126 On exiting boot services we must make sure the SP805 Watchdog Timer
127 is stopped.
128 **/
129 STATIC
130 VOID
131 EFIAPI
132 ExitBootServicesEvent (
133 IN EFI_EVENT Event,
134 IN VOID *Context
135 )
136 {
137 SP805Unlock ();
138 SP805Stop ();
139 SP805Lock ();
140 }
141
142 /**
143 This function registers the handler NotifyFunction so it is called every time
144 the watchdog timer expires. It also passes the amount of time since the last
145 handler call to the NotifyFunction.
146 If NotifyFunction is not NULL and a handler is not already registered,
147 then the new handler is registered and EFI_SUCCESS is returned.
148 If NotifyFunction is NULL, and a handler is already registered,
149 then that handler is unregistered.
150 If an attempt is made to register a handler when a handler is already registered,
151 then EFI_ALREADY_STARTED is returned.
152 If an attempt is made to unregister a handler when a handler is not registered,
153 then EFI_INVALID_PARAMETER is returned.
154
155 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
156 @param NotifyFunction The function to call when a timer interrupt fires. This
157 function executes at TPL_HIGH_LEVEL. The DXE Core will
158 register a handler for the timer interrupt, so it can know
159 how much time has passed. This information is used to
160 signal timer based events. NULL will unregister the handler.
161
162 @retval EFI_SUCCESS The watchdog timer handler was registered.
163 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
164 registered.
165 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
166 previously registered.
167
168 **/
169 STATIC
170 EFI_STATUS
171 EFIAPI
172 SP805RegisterHandler (
173 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
174 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
175 )
176 {
177 if ((mWatchdogNotify == NULL) && (NotifyFunction == NULL)) {
178 return EFI_INVALID_PARAMETER;
179 }
180
181 if ((mWatchdogNotify != NULL) && (NotifyFunction != NULL)) {
182 return EFI_ALREADY_STARTED;
183 }
184
185 mWatchdogNotify = NotifyFunction;
186 return EFI_SUCCESS;
187 }
188
189 /**
190
191 This function adjusts the period of timer interrupts to the value specified
192 by TimerPeriod. If the timer period is updated, then the selected timer
193 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
194 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
195 If an error occurs while attempting to update the timer period, then the
196 timer hardware will be put back in its state prior to this call, and
197 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
198 is disabled. This is not the same as disabling the CPU's interrupts.
199 Instead, it must either turn off the timer hardware, or it must adjust the
200 interrupt controller so that a CPU interrupt is not generated when the timer
201 interrupt fires.
202
203 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
204 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
205 the timer hardware is not programmable, then EFI_UNSUPPORTED is
206 returned. If the timer is programmable, then the timer period
207 will be rounded up to the nearest timer period that is supported
208 by the timer hardware. If TimerPeriod is set to 0, then the
209 timer interrupts will be disabled.
210
211
212 @retval EFI_SUCCESS The timer period was changed.
213 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
214 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
215
216 **/
217 STATIC
218 EFI_STATUS
219 EFIAPI
220 SP805SetTimerPeriod (
221 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
222 IN UINT64 TimerPeriod // In 100ns units
223 )
224 {
225 EFI_STATUS Status;
226 UINT64 Ticks64bit;
227
228 SP805Unlock ();
229
230 Status = EFI_SUCCESS;
231
232 if (TimerPeriod == 0) {
233 // This is a watchdog stop request
234 SP805Stop ();
235 } else {
236 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
237 // The SP805 will count down to zero and generate an interrupt.
238 //
239 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
240 //
241 // i.e.:
242 //
243 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
244
245 Ticks64bit = MultU64x32 (TimerPeriod, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz));
246 Ticks64bit = DivU64x32 (Ticks64bit, 10 * 1000 * 1000);
247
248 // The registers in the SP805 are only 32 bits
249 if (Ticks64bit > MAX_UINT32) {
250 // We could load the watchdog with the maximum supported value but
251 // if a smaller value was requested, this could have the watchdog
252 // triggering before it was intended.
253 // Better generate an error to let the caller know.
254 Status = EFI_DEVICE_ERROR;
255 goto EXIT;
256 }
257
258 // Update the watchdog with a 32-bit value.
259 MmioWrite32 (SP805_WDOG_LOAD_REG, (UINT32)Ticks64bit);
260
261 // Start the watchdog
262 SP805Start ();
263 }
264
265 mTimerPeriod = TimerPeriod;
266
267 EXIT:
268 // Ensure the watchdog is locked before exiting.
269 SP805Lock ();
270 ASSERT_EFI_ERROR (Status);
271 return Status;
272 }
273
274 /**
275 This function retrieves the period of timer interrupts in 100 ns units,
276 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
277 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
278 returned, then the timer is currently disabled.
279
280 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
281 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
282 0 is returned, then the timer is currently disabled.
283
284
285 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
286 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
287
288 **/
289 STATIC
290 EFI_STATUS
291 EFIAPI
292 SP805GetTimerPeriod (
293 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
294 OUT UINT64 *TimerPeriod
295 )
296 {
297 if (TimerPeriod == NULL) {
298 return EFI_INVALID_PARAMETER;
299 }
300
301 *TimerPeriod = mTimerPeriod;
302 return EFI_SUCCESS;
303 }
304
305 /**
306 Interface structure for the Watchdog Architectural Protocol.
307
308 @par Protocol Description:
309 This protocol provides a service to set the amount of time to wait
310 before firing the watchdog timer, and it also provides a service to
311 register a handler that is invoked when the watchdog timer fires.
312
313 @par When the watchdog timer fires, control will be passed to a handler
314 if one has been registered. If no handler has been registered,
315 or the registered handler returns, then the system will be
316 reset by calling the Runtime Service ResetSystem().
317
318 @param RegisterHandler
319 Registers a handler that will be called each time the
320 watchdogtimer interrupt fires. TimerPeriod defines the minimum
321 time between timer interrupts, so TimerPeriod will also
322 be the minimum time between calls to the registered
323 handler.
324 NOTE: If the watchdog resets the system in hardware, then
325 this function will not have any chance of executing.
326
327 @param SetTimerPeriod
328 Sets the period of the timer interrupt in 100 nS units.
329 This function is optional, and may return EFI_UNSUPPORTED.
330 If this function is supported, then the timer period will
331 be rounded up to the nearest supported timer period.
332
333 @param GetTimerPeriod
334 Retrieves the period of the timer interrupt in 100 nS units.
335
336 **/
337 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
338 SP805RegisterHandler,
339 SP805SetTimerPeriod,
340 SP805GetTimerPeriod
341 };
342
343 /**
344 Initialize the state information for the Watchdog Timer Architectural Protocol.
345
346 @param ImageHandle of the loaded driver
347 @param SystemTable Pointer to the System Table
348
349 @retval EFI_SUCCESS Protocol registered
350 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
351 @retval EFI_DEVICE_ERROR Hardware problems
352
353 **/
354 EFI_STATUS
355 EFIAPI
356 SP805Initialize (
357 IN EFI_HANDLE ImageHandle,
358 IN EFI_SYSTEM_TABLE *SystemTable
359 )
360 {
361 EFI_STATUS Status;
362 EFI_HANDLE Handle;
363
364 // Find the interrupt controller protocol. ASSERT if not found.
365 Status = gBS->LocateProtocol (
366 &gHardwareInterruptProtocolGuid,
367 NULL,
368 (VOID **)&mInterrupt
369 );
370 ASSERT_EFI_ERROR (Status);
371
372 // Unlock access to the SP805 registers
373 SP805Unlock ();
374
375 // Stop the watchdog from triggering unexpectedly
376 SP805Stop ();
377
378 // Set the watchdog to reset the board when triggered
379 // This is a last resort in case the interrupt handler fails
380 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_RESEN) == 0) {
381 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_RESEN);
382 }
383
384 // Clear any pending interrupts
385 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
386
387 // Prohibit any rogue access to SP805 registers
388 SP805Lock ();
389
390 if (PcdGet32 (PcdSP805WatchdogInterrupt) > 0) {
391 Status = mInterrupt->RegisterInterruptSource (
392 mInterrupt,
393 PcdGet32 (PcdSP805WatchdogInterrupt),
394 SP805InterruptHandler
395 );
396 if (EFI_ERROR (Status)) {
397 DEBUG ((
398 DEBUG_ERROR,
399 "%a: failed to register watchdog interrupt - %r\n",
400 __FUNCTION__,
401 Status
402 ));
403 return Status;
404 }
405 } else {
406 DEBUG ((
407 DEBUG_WARN,
408 "%a: no interrupt specified, running in RESET mode only\n",
409 __FUNCTION__
410 ));
411 }
412
413 //
414 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
415 // This will avoid conflicts with the universal watchdog
416 //
417 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
418
419 // Register for an ExitBootServicesEvent
420 Status = gBS->CreateEvent (
421 EVT_SIGNAL_EXIT_BOOT_SERVICES,
422 TPL_NOTIFY,
423 ExitBootServicesEvent,
424 NULL,
425 &mEfiExitBootServicesEvent
426 );
427 if (EFI_ERROR (Status)) {
428 Status = EFI_OUT_OF_RESOURCES;
429 goto EXIT;
430 }
431
432 // Install the Timer Architectural Protocol onto a new handle
433 Handle = NULL;
434 Status = gBS->InstallMultipleProtocolInterfaces (
435 &Handle,
436 &gEfiWatchdogTimerArchProtocolGuid,
437 &mWatchdogTimer,
438 NULL
439 );
440 if (EFI_ERROR (Status)) {
441 Status = EFI_OUT_OF_RESOURCES;
442 goto EXIT;
443 }
444
445 EXIT:
446 ASSERT_EFI_ERROR (Status);
447 return Status;
448 }