SP805Watchdog.c 14 KB

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