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