]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c
ArmPlatformPkg/SP805WatchdogDxe: Removed late initialization capability from the...
[mirror_edk2.git] / ArmPlatformPkg / Drivers / SP805WatchdogDxe / SP805Watchdog.c
1 /** @file
2 *
3 * Copyright (c) 2011-2012, 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 #include <Drivers/SP805Watchdog.h>
29
30 EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;
31
32 EFI_STATUS
33 SP805Identify (
34 VOID
35 )
36 {
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)) {
42 return EFI_NOT_FOUND;
43 }
44
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)) {
50 return EFI_NOT_FOUND;
51 }
52
53 return EFI_SUCCESS;
54 }
55
56 /**
57 Make sure the SP805 registers are unlocked for writing.
58
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.
62 **/
63 inline
64 VOID
65 SP805Unlock (
66 VOID
67 )
68 {
69 if( MmioRead32(SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_LOCKED ) {
70 MmioWrite32(SP805_WDOG_LOCK_REG, SP805_WDOG_SPECIAL_UNLOCK_CODE);
71 }
72 }
73
74 /**
75 Make sure the SP805 registers are locked and can not be overwritten.
76
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.
80 **/
81 inline
82 VOID
83 SP805Lock (
84 VOID
85 )
86 {
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);
90 }
91 }
92
93 /**
94 Stop the SP805 watchdog timer from counting down by disabling interrupts.
95 **/
96 inline
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 inline
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 VOID
130 EFIAPI
131 ExitBootServicesEvent (
132 IN EFI_EVENT Event,
133 IN VOID *Context
134 )
135 {
136 SP805Unlock();
137 SP805Stop();
138 SP805Lock();
139 }
140
141 /**
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.
153
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.
160
161 @retval EFI_SUCCESS The watchdog timer handler was registered.
162 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
163 registered.
164 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
165 previously registered.
166
167 **/
168 EFI_STATUS
169 EFIAPI
170 SP805RegisterHandler (
171 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
172 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
173 )
174 {
175 // ERROR: This function is not supported.
176 // The hardware watchdog will reset the board
177 return EFI_INVALID_PARAMETER;
178 }
179
180 /**
181
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
192 interrupt fires.
193
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.
201
202
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.
206
207 **/
208 EFI_STATUS
209 EFIAPI
210 SP805SetTimerPeriod (
211 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
212 IN UINT64 TimerPeriod // In 100ns units
213 )
214 {
215 EFI_STATUS Status = EFI_SUCCESS;
216 UINT64 Ticks64bit;
217
218 SP805Unlock();
219
220 if( TimerPeriod == 0 ) {
221 // This is a watchdog stop request
222 SP805Stop();
223 goto EXIT;
224 } else {
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.
230 //
231 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz) / 2 ;
232 //
233 // i.e.:
234 //
235 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 20 MHz ;
236
237 Ticks64bit = DivU64x32(MultU64x32(TimerPeriod, (UINTN)PcdGet32(PcdSP805WatchdogClockFrequencyInHz)), 20000000);
238
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;
246 goto EXIT;
247 }
248
249 // Update the watchdog with a 32-bit value.
250 MmioWrite32(SP805_WDOG_LOAD_REG, (UINT32)Ticks64bit);
251
252 // Start the watchdog
253 SP805Start();
254 }
255
256 EXIT:
257 // Ensure the watchdog is locked before exiting.
258 SP805Lock();
259 return Status;
260 }
261
262 /**
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.
267
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.
271
272
273 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
274 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
275
276 **/
277 EFI_STATUS
278 EFIAPI
279 SP805GetTimerPeriod (
280 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
281 OUT UINT64 *TimerPeriod
282 )
283 {
284 EFI_STATUS Status = EFI_SUCCESS;
285 UINT64 ReturnValue;
286
287 if (TimerPeriod == NULL) {
288 return EFI_INVALID_PARAMETER;
289 }
290
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.
294 ReturnValue = 0;
295 } else {
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 );
300 }
301
302 *TimerPeriod = ReturnValue;
303
304 return Status;
305 }
306
307 /**
308 Interface structure for the Watchdog Architectural Protocol.
309
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.
314
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().
319
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
325 handler.
326 NOTE: If the watchdog resets the system in hardware, then
327 this function will not have any chance of executing.
328
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.
334
335 @param GetTimerPeriod
336 Retrieves the period of the timer interrupt in 100 nS units.
337
338 **/
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
343 };
344
345 /**
346 Initialize the state information for the Watchdog Timer Architectural Protocol.
347
348 @param ImageHandle of the loaded driver
349 @param SystemTable Pointer to the System Table
350
351 @retval EFI_SUCCESS Protocol registered
352 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
353 @retval EFI_DEVICE_ERROR Hardware problems
354
355 **/
356 EFI_STATUS
357 EFIAPI
358 SP805Initialize (
359 IN EFI_HANDLE ImageHandle,
360 IN EFI_SYSTEM_TABLE *SystemTable
361 )
362 {
363 EFI_STATUS Status;
364 EFI_HANDLE Handle;
365
366 // Check if the SP805 hardware watchdog module exists on board
367 Status = SP805Identify();
368 if (EFI_ERROR(Status)) {
369 Status = EFI_DEVICE_ERROR;
370 goto EXIT;
371 }
372
373 // Unlock access to the SP805 registers
374 SP805Unlock ();
375
376 // Stop the watchdog from triggering unexpectedly
377 SP805Stop ();
378
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);
382 }
383
384 // Prohibit any rogue access to SP805 registers
385 SP805Lock();
386
387 //
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
390 //
391 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
392
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;
397 goto EXIT;
398 }
399
400 // Install the Timer Architectural Protocol onto a new handle
401 Handle = NULL;
402 Status = gBS->InstallMultipleProtocolInterfaces(
403 &Handle,
404 &gEfiWatchdogTimerArchProtocolGuid, &gWatchdogTimer,
405 NULL
406 );
407 if (EFI_ERROR(Status)) {
408 Status = EFI_OUT_OF_RESOURCES;
409 goto EXIT;
410 }
411
412 EXIT:
413 if(EFI_ERROR(Status)) {
414 // The watchdog failed to initialize
415 ASSERT(FALSE);
416 }
417 return Status;
418 }