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