12c2f0a1fe499688fc318e7b64c27a8dcf985ec6
[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 * 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
10 *
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.
13 *
14 **/
15
16
17 #include <PiDxe.h>
18
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
25 #include <Protocol/WatchdogTimer.h>
26
27 #include "SP805Watchdog.h"
28
29 STATIC EFI_EVENT mEfiExitBootServicesEvent;
30
31 /**
32 Make sure the SP805 registers are unlocked for writing.
33
34 Note: The SP805 Watchdog Timer supports locking of its registers,
35 i.e. it inhibits all writes to avoid rogue software accidentally
36 corrupting their contents.
37 **/
38 STATIC
39 VOID
40 SP805Unlock (
41 VOID
42 )
43 {
44 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_LOCKED) {
45 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_SPECIAL_UNLOCK_CODE);
46 }
47 }
48
49 /**
50 Make sure the SP805 registers are locked and can not be overwritten.
51
52 Note: The SP805 Watchdog Timer supports locking of its registers,
53 i.e. it inhibits all writes to avoid rogue software accidentally
54 corrupting their contents.
55 **/
56 STATIC
57 VOID
58 SP805Lock (
59 VOID
60 )
61 {
62 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_UNLOCKED) {
63 // To lock it, just write in any number (except the special unlock code).
64 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_LOCK_IS_LOCKED);
65 }
66 }
67
68 /**
69 Stop the SP805 watchdog timer from counting down by disabling interrupts.
70 **/
71 STATIC
72 VOID
73 SP805Stop (
74 VOID
75 )
76 {
77 // Disable interrupts
78 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) != 0) {
79 MmioAnd32 (SP805_WDOG_CONTROL_REG, ~SP805_WDOG_CTRL_INTEN);
80 }
81 }
82
83 /**
84 Starts the SP805 counting down by enabling interrupts.
85 The count down will start from the value stored in the Load register,
86 not from the value where it was previously stopped.
87 **/
88 STATIC
89 VOID
90 SP805Start (
91 VOID
92 )
93 {
94 // Enable interrupts
95 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) {
96 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_INTEN);
97 }
98 }
99
100 /**
101 On exiting boot services we must make sure the SP805 Watchdog Timer
102 is stopped.
103 **/
104 STATIC
105 VOID
106 EFIAPI
107 ExitBootServicesEvent (
108 IN EFI_EVENT Event,
109 IN VOID *Context
110 )
111 {
112 SP805Unlock ();
113 SP805Stop ();
114 SP805Lock ();
115 }
116
117 /**
118 This function registers the handler NotifyFunction so it is called every time
119 the watchdog timer expires. It also passes the amount of time since the last
120 handler call to the NotifyFunction.
121 If NotifyFunction is not NULL and a handler is not already registered,
122 then the new handler is registered and EFI_SUCCESS is returned.
123 If NotifyFunction is NULL, and a handler is already registered,
124 then that handler is unregistered.
125 If an attempt is made to register a handler when a handler is already registered,
126 then EFI_ALREADY_STARTED is returned.
127 If an attempt is made to unregister a handler when a handler is not registered,
128 then EFI_INVALID_PARAMETER is returned.
129
130 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
131 @param NotifyFunction The function to call when a timer interrupt fires. This
132 function executes at TPL_HIGH_LEVEL. The DXE Core will
133 register a handler for the timer interrupt, so it can know
134 how much time has passed. This information is used to
135 signal timer based events. NULL will unregister the handler.
136
137 @retval EFI_SUCCESS The watchdog timer handler was registered.
138 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
139 registered.
140 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
141 previously registered.
142
143 **/
144 STATIC
145 EFI_STATUS
146 EFIAPI
147 SP805RegisterHandler (
148 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
149 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
150 )
151 {
152 // ERROR: This function is not supported.
153 // The hardware watchdog will reset the board
154 return EFI_INVALID_PARAMETER;
155 }
156
157 /**
158
159 This function adjusts the period of timer interrupts to the value specified
160 by TimerPeriod. If the timer period is updated, then the selected timer
161 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
162 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
163 If an error occurs while attempting to update the timer period, then the
164 timer hardware will be put back in its state prior to this call, and
165 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
166 is disabled. This is not the same as disabling the CPU's interrupts.
167 Instead, it must either turn off the timer hardware, or it must adjust the
168 interrupt controller so that a CPU interrupt is not generated when the timer
169 interrupt fires.
170
171 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
172 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
173 the timer hardware is not programmable, then EFI_UNSUPPORTED is
174 returned. If the timer is programmable, then the timer period
175 will be rounded up to the nearest timer period that is supported
176 by the timer hardware. If TimerPeriod is set to 0, then the
177 timer interrupts will be disabled.
178
179
180 @retval EFI_SUCCESS The timer period was changed.
181 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
182 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
183
184 **/
185 STATIC
186 EFI_STATUS
187 EFIAPI
188 SP805SetTimerPeriod (
189 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
190 IN UINT64 TimerPeriod // In 100ns units
191 )
192 {
193 EFI_STATUS Status;
194 UINT64 Ticks64bit;
195
196 SP805Unlock ();
197
198 Status = EFI_SUCCESS;
199
200 if (TimerPeriod == 0) {
201 // This is a watchdog stop request
202 SP805Stop ();
203 } else {
204 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
205 // The SP805 will count down to ZERO once, generate an interrupt and
206 // then it will again reload the initial value and start again.
207 // On the second time when it reaches ZERO, it will actually reset the board.
208 // Therefore, we need to load half the required delay.
209 //
210 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz) / 2 ;
211 //
212 // i.e.:
213 //
214 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 20 MHz ;
215
216 Ticks64bit = MultU64x32 (TimerPeriod, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz));
217 Ticks64bit = DivU64x32 (Ticks64bit, 20000000);
218
219 // The registers in the SP805 are only 32 bits
220 if (Ticks64bit > MAX_UINT32) {
221 // We could load the watchdog with the maximum supported value but
222 // if a smaller value was requested, this could have the watchdog
223 // triggering before it was intended.
224 // Better generate an error to let the caller know.
225 Status = EFI_DEVICE_ERROR;
226 goto EXIT;
227 }
228
229 // Update the watchdog with a 32-bit value.
230 MmioWrite32 (SP805_WDOG_LOAD_REG, (UINT32)Ticks64bit);
231
232 // Start the watchdog
233 SP805Start ();
234 }
235
236 EXIT:
237 // Ensure the watchdog is locked before exiting.
238 SP805Lock ();
239 return Status;
240 }
241
242 /**
243 This function retrieves the period of timer interrupts in 100 ns units,
244 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
245 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
246 returned, then the timer is currently disabled.
247
248 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
249 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
250 0 is returned, then the timer is currently disabled.
251
252
253 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
254 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
255
256 **/
257 STATIC
258 EFI_STATUS
259 EFIAPI
260 SP805GetTimerPeriod (
261 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
262 OUT UINT64 *TimerPeriod
263 )
264 {
265 UINT64 ReturnValue;
266
267 if (TimerPeriod == NULL) {
268 return EFI_INVALID_PARAMETER;
269 }
270
271 // Check if the watchdog is stopped
272 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) {
273 // It is stopped, so return zero.
274 ReturnValue = 0;
275 } else {
276 // Convert the Watchdog ticks into TimerPeriod
277 // Ensure 64bit arithmetic throughout because the Watchdog ticks may already
278 // be at the maximum 32 bit value and we still need to multiply that by 600.
279 ReturnValue = MultU64x32 (MmioRead32 (SP805_WDOG_LOAD_REG), 600);
280 }
281
282 *TimerPeriod = ReturnValue;
283
284 return EFI_SUCCESS;
285 }
286
287 /**
288 Interface structure for the Watchdog Architectural Protocol.
289
290 @par Protocol Description:
291 This protocol provides a service to set the amount of time to wait
292 before firing the watchdog timer, and it also provides a service to
293 register a handler that is invoked when the watchdog timer fires.
294
295 @par When the watchdog timer fires, control will be passed to a handler
296 if one has been registered. If no handler has been registered,
297 or the registered handler returns, then the system will be
298 reset by calling the Runtime Service ResetSystem().
299
300 @param RegisterHandler
301 Registers a handler that will be called each time the
302 watchdogtimer interrupt fires. TimerPeriod defines the minimum
303 time between timer interrupts, so TimerPeriod will also
304 be the minimum time between calls to the registered
305 handler.
306 NOTE: If the watchdog resets the system in hardware, then
307 this function will not have any chance of executing.
308
309 @param SetTimerPeriod
310 Sets the period of the timer interrupt in 100 nS units.
311 This function is optional, and may return EFI_UNSUPPORTED.
312 If this function is supported, then the timer period will
313 be rounded up to the nearest supported timer period.
314
315 @param GetTimerPeriod
316 Retrieves the period of the timer interrupt in 100 nS units.
317
318 **/
319 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
320 SP805RegisterHandler,
321 SP805SetTimerPeriod,
322 SP805GetTimerPeriod
323 };
324
325 /**
326 Initialize the state information for the Watchdog Timer Architectural Protocol.
327
328 @param ImageHandle of the loaded driver
329 @param SystemTable Pointer to the System Table
330
331 @retval EFI_SUCCESS Protocol registered
332 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
333 @retval EFI_DEVICE_ERROR Hardware problems
334
335 **/
336 EFI_STATUS
337 EFIAPI
338 SP805Initialize (
339 IN EFI_HANDLE ImageHandle,
340 IN EFI_SYSTEM_TABLE *SystemTable
341 )
342 {
343 EFI_STATUS Status;
344 EFI_HANDLE Handle;
345
346 // Unlock access to the SP805 registers
347 SP805Unlock ();
348
349 // Stop the watchdog from triggering unexpectedly
350 SP805Stop ();
351
352 // Set the watchdog to reset the board when triggered
353 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_RESEN) == 0) {
354 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_RESEN);
355 }
356
357 // Prohibit any rogue access to SP805 registers
358 SP805Lock ();
359
360 //
361 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
362 // This will avoid conflicts with the universal watchdog
363 //
364 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
365
366 // Register for an ExitBootServicesEvent
367 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
368 ExitBootServicesEvent, NULL, &mEfiExitBootServicesEvent);
369 if (EFI_ERROR (Status)) {
370 Status = EFI_OUT_OF_RESOURCES;
371 goto EXIT;
372 }
373
374 // Install the Timer Architectural Protocol onto a new handle
375 Handle = NULL;
376 Status = gBS->InstallMultipleProtocolInterfaces (
377 &Handle,
378 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
379 NULL
380 );
381 if (EFI_ERROR (Status)) {
382 Status = EFI_OUT_OF_RESOURCES;
383 goto EXIT;
384 }
385
386 EXIT:
387 ASSERT_EFI_ERROR (Status);
388 return Status;
389 }