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