]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
ArmPkg/GenericWatchdogDxe: Split 64bit register write to 2x32bit
[mirror_edk2.git] / ArmPkg / Drivers / GenericWatchdogDxe / GenericWatchdogDxe.c
CommitLineData
9f38945f
OM
1/** @file\r
2*\r
8fe18cba 3* Copyright (c) 2013-2018, ARM Limited. All rights reserved.\r
9f38945f
OM
4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD\r
7* License which accompanies this distribution. The full text of the license\r
8* may be found at http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/IoLib.h>\r
21#include <Library/PcdLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/UefiRuntimeServicesTableLib.h>\r
24#include <Library/UefiLib.h>\r
25#include <Library/ArmGenericTimerCounterLib.h>\r
26\r
baee8efb 27#include <Protocol/HardwareInterrupt2.h>\r
9f38945f 28#include <Protocol/WatchdogTimer.h>\r
9f38945f 29\r
0b4d97a0 30#include "GenericWatchdog.h"\r
9f38945f 31\r
b0393756
EL
32/* The number of 100ns periods (the unit of time passed to these functions)\r
33 in a second */\r
9f38945f
OM
34#define TIME_UNITS_PER_SECOND 10000000\r
35\r
b0393756 36// Tick frequency of the generic timer basis of the generic watchdog.\r
9f38945f
OM
37UINTN mTimerFrequencyHz = 0;\r
38\r
b0393756
EL
39/* In cases where the compare register was set manually, information about\r
40 how long the watchdog was asked to wait cannot be retrieved from hardware.\r
41 It is therefore stored here. 0 means the timer is not running. */\r
9f38945f
OM
42UINT64 mNumTimerTicks = 0;\r
43\r
baee8efb 44EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;\r
9f38945f 45\r
8fe18cba 46VOID\r
9f38945f
OM
47WatchdogWriteOffsetRegister (\r
48 UINT32 Value\r
49 )\r
50{\r
8fe18cba 51 MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r
9f38945f
OM
52}\r
53\r
8fe18cba 54VOID\r
9f38945f
OM
55WatchdogWriteCompareRegister (\r
56 UINT64 Value\r
57 )\r
58{\r
dd4cae4d
MW
59 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW, Value & MAX_UINT32);\r
60 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH, (Value >> 32) & MAX_UINT32);\r
9f38945f
OM
61}\r
62\r
8fe18cba 63VOID\r
9f38945f
OM
64WatchdogEnable (\r
65 VOID\r
66 )\r
67{\r
8fe18cba 68 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r
9f38945f
OM
69}\r
70\r
8fe18cba 71VOID\r
9f38945f
OM
72WatchdogDisable (\r
73 VOID\r
74 )\r
75{\r
8fe18cba 76 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);\r
9f38945f
OM
77}\r
78\r
b0393756 79/** On exiting boot services we must make sure the Watchdog Timer\r
9f38945f
OM
80 is stopped.\r
81**/\r
82VOID\r
83EFIAPI\r
84WatchdogExitBootServicesEvent (\r
85 IN EFI_EVENT Event,\r
86 IN VOID *Context\r
87 )\r
88{\r
89 WatchdogDisable ();\r
90 mNumTimerTicks = 0;\r
91}\r
92\r
b0393756
EL
93/* This function is called when the watchdog's first signal (WS0) goes high.\r
94 It uses the ResetSystem Runtime Service to reset the board.\r
9f38945f
OM
95*/\r
96VOID\r
97EFIAPI\r
98WatchdogInterruptHandler (\r
99 IN HARDWARE_INTERRUPT_SOURCE Source,\r
100 IN EFI_SYSTEM_CONTEXT SystemContext\r
101 )\r
102{\r
b0393756 103 STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";\r
9f38945f
OM
104\r
105 WatchdogDisable ();\r
106\r
107 mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);\r
108\r
109 gRT->ResetSystem (\r
110 EfiResetCold,\r
111 EFI_TIMEOUT,\r
112 StrSize (ResetString),\r
baeddf2b 113 (VOID *) &ResetString\r
9f38945f
OM
114 );\r
115\r
116 // If we got here then the reset didn't work\r
117 ASSERT (FALSE);\r
118}\r
119\r
120/**\r
121 This function registers the handler NotifyFunction so it is called every time\r
122 the watchdog timer expires. It also passes the amount of time since the last\r
123 handler call to the NotifyFunction.\r
124 If NotifyFunction is not NULL and a handler is not already registered,\r
125 then the new handler is registered and EFI_SUCCESS is returned.\r
126 If NotifyFunction is NULL, and a handler is already registered,\r
127 then that handler is unregistered.\r
b0393756
EL
128 If an attempt is made to register a handler when a handler is already\r
129 registered, then EFI_ALREADY_STARTED is returned.\r
130 If an attempt is made to unregister a handler when a handler is not\r
131 registered, then EFI_INVALID_PARAMETER is returned.\r
9f38945f
OM
132\r
133 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
134 @param NotifyFunction The function to call when a timer interrupt fires.\r
135 This function executes at TPL_HIGH_LEVEL. The DXE\r
136 Core will register a handler for the timer interrupt,\r
137 so it can know how much time has passed. This\r
138 information is used to signal timer based events.\r
139 NULL will unregister the handler.\r
140\r
b0393756 141 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.\r
9f38945f
OM
142\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
146WatchdogRegisterHandler (\r
147 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
148 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction\r
149 )\r
150{\r
151 // ERROR: This function is not supported.\r
152 // The watchdog will reset the board\r
153 return EFI_UNSUPPORTED;\r
154}\r
155\r
156/**\r
157 This function sets the amount of time to wait before firing the watchdog\r
b0393756 158 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog\r
9f38945f
OM
159 timer is disabled.\r
160\r
161 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
b0393756
EL
162 @param TimerPeriod The amount of time in 100ns units to wait before\r
163 the watchdog timer is fired. If TimerPeriod is zero,\r
164 then the watchdog timer is disabled.\r
9f38945f 165\r
b0393756 166 @retval EFI_SUCCESS The watchdog timer has been programmed to fire\r
8fe18cba 167 in TimerPeriod 100ns units.\r
9f38945f
OM
168\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172WatchdogSetTimerPeriod (\r
173 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
174 IN UINT64 TimerPeriod // In 100ns units\r
175 )\r
176{\r
23666400 177 UINTN SystemCount;\r
9f38945f 178\r
b0393756 179 // if TimerPeriod is 0, this is a request to stop the watchdog.\r
9f38945f
OM
180 if (TimerPeriod == 0) {\r
181 mNumTimerTicks = 0;\r
8fe18cba
A
182 WatchdogDisable ();\r
183 return EFI_SUCCESS;\r
9f38945f
OM
184 }\r
185\r
186 // Work out how many timer ticks will equate to TimerPeriod\r
187 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;\r
188\r
b0393756
EL
189 /* If the number of required ticks is greater than the max the watchdog's\r
190 offset register (WOR) can hold, we need to manually compute and set\r
191 the compare register (WCV) */\r
9f38945f 192 if (mNumTimerTicks > MAX_UINT32) {\r
b0393756
EL
193 /* We need to enable the watchdog *before* writing to the compare register,\r
194 because enabling the watchdog causes an "explicit refresh", which\r
195 clobbers the compare register (WCV). In order to make sure this doesn't\r
196 trigger an interrupt, set the offset to max. */\r
8fe18cba 197 WatchdogWriteOffsetRegister (MAX_UINT32);\r
9f38945f 198 WatchdogEnable ();\r
23666400 199 SystemCount = ArmGenericTimerGetSystemCount ();\r
8fe18cba 200 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r
9f38945f 201 } else {\r
8fe18cba 202 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r
9f38945f
OM
203 WatchdogEnable ();\r
204 }\r
205\r
8fe18cba 206 return EFI_SUCCESS;\r
9f38945f
OM
207}\r
208\r
209/**\r
b0393756 210 This function retrieves the period of timer interrupts in 100ns units,\r
9f38945f
OM
211 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
212 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
213 returned, then the timer is currently disabled.\r
214\r
215 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
b0393756
EL
216 @param TimerPeriod A pointer to the timer period to retrieve in\r
217 100ns units. If 0 is returned, then the timer is\r
9f38945f
OM
218 currently disabled.\r
219\r
220\r
221 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
222 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
223\r
224**/\r
225EFI_STATUS\r
226EFIAPI\r
227WatchdogGetTimerPeriod (\r
228 IN CONST EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
229 OUT UINT64 *TimerPeriod\r
230 )\r
231{\r
232 if (TimerPeriod == NULL) {\r
233 return EFI_INVALID_PARAMETER;\r
234 }\r
235\r
236 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);\r
237\r
238 return EFI_SUCCESS;\r
239}\r
240\r
241/**\r
242 Interface structure for the Watchdog Architectural Protocol.\r
243\r
244 @par Protocol Description:\r
245 This protocol provides a service to set the amount of time to wait\r
246 before firing the watchdog timer, and it also provides a service to\r
247 register a handler that is invoked when the watchdog timer fires.\r
248\r
249 @par When the watchdog timer fires, control will be passed to a handler\r
250 if one has been registered. If no handler has been registered,\r
251 or the registered handler returns, then the system will be\r
252 reset by calling the Runtime Service ResetSystem().\r
253\r
254 @param RegisterHandler\r
255 Registers a handler that will be called each time the\r
256 watchdogtimer interrupt fires. TimerPeriod defines the minimum\r
257 time between timer interrupts, so TimerPeriod will also\r
258 be the minimum time between calls to the registered\r
259 handler.\r
260 NOTE: If the watchdog resets the system in hardware, then\r
261 this function will not have any chance of executing.\r
262\r
263 @param SetTimerPeriod\r
b0393756 264 Sets the period of the timer interrupt in 100ns units.\r
9f38945f
OM
265 This function is optional, and may return EFI_UNSUPPORTED.\r
266 If this function is supported, then the timer period will\r
267 be rounded up to the nearest supported timer period.\r
268\r
269 @param GetTimerPeriod\r
b0393756 270 Retrieves the period of the timer interrupt in 100ns units.\r
9f38945f
OM
271\r
272**/\r
273EFI_WATCHDOG_TIMER_ARCH_PROTOCOL gWatchdogTimer = {\r
b0393756
EL
274 (EFI_WATCHDOG_TIMER_REGISTER_HANDLER)WatchdogRegisterHandler,\r
275 (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD)WatchdogSetTimerPeriod,\r
276 (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD)WatchdogGetTimerPeriod\r
9f38945f
OM
277};\r
278\r
279EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;\r
280\r
281EFI_STATUS\r
282EFIAPI\r
283GenericWatchdogEntry (\r
284 IN EFI_HANDLE ImageHandle,\r
285 IN EFI_SYSTEM_TABLE *SystemTable\r
286 )\r
287{\r
288 EFI_STATUS Status;\r
289 EFI_HANDLE Handle;\r
290\r
b0393756
EL
291 /* Make sure the Watchdog Timer Architectural Protocol has not been installed\r
292 in the system yet.\r
293 This will avoid conflicts with the universal watchdog */\r
9f38945f
OM
294 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r
295\r
296 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r
297 ASSERT (mTimerFrequencyHz != 0);\r
298\r
299 // Register for an ExitBootServicesEvent\r
300 Status = gBS->CreateEvent (\r
b0393756
EL
301 EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
302 TPL_NOTIFY,\r
303 WatchdogExitBootServicesEvent,\r
304 NULL,\r
305 &EfiExitBootServicesEvent\r
9f38945f
OM
306 );\r
307 if (!EFI_ERROR (Status)) {\r
308 // Install interrupt handler\r
309 Status = gBS->LocateProtocol (\r
baee8efb 310 &gHardwareInterrupt2ProtocolGuid,\r
9f38945f
OM
311 NULL,\r
312 (VOID **)&mInterruptProtocol\r
313 );\r
314 if (!EFI_ERROR (Status)) {\r
315 Status = mInterruptProtocol->RegisterInterruptSource (\r
b0393756
EL
316 mInterruptProtocol,\r
317 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
318 WatchdogInterruptHandler\r
319 );\r
9f38945f 320 if (!EFI_ERROR (Status)) {\r
baee8efb
AB
321 Status = mInterruptProtocol->SetTriggerType (\r
322 mInterruptProtocol,\r
323 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
324 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING\r
325 );\r
326 if (!EFI_ERROR (Status)) {\r
327 // Install the Timer Architectural Protocol onto a new handle\r
328 Handle = NULL;\r
329 Status = gBS->InstallMultipleProtocolInterfaces (\r
330 &Handle,\r
331 &gEfiWatchdogTimerArchProtocolGuid,\r
332 &gWatchdogTimer,\r
333 NULL\r
334 );\r
335 }\r
9f38945f
OM
336 }\r
337 }\r
338 }\r
339\r
b0393756 340 ASSERT_EFI_ERROR (Status);\r
9f38945f
OM
341\r
342 mNumTimerTicks = 0;\r
343 WatchdogDisable ();\r
344\r
345 return Status;\r
346}\r