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