]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
ArmPkg: Fix bug in Generic Watchdog driver
[mirror_edk2.git] / ArmPkg / Drivers / GenericWatchdogDxe / GenericWatchdogDxe.c
index ba09227806ad50016faec647ea8bcd0059b9fe8a..3180f011253639c408b7151c79c2106a352c7340 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 *\r
-*  Copyright (c) 2013-2014, ARM Limited. All rights reserved.\r
+*  Copyright (c) 2013-2018, ARM Limited. All rights reserved.\r
 *\r
 *  This program and the accompanying materials\r
 *  are licensed and made available under the terms and conditions of the BSD\r
 #include <Library/UefiLib.h>\r
 #include <Library/ArmGenericTimerCounterLib.h>\r
 \r
+#include <Protocol/HardwareInterrupt2.h>\r
 #include <Protocol/WatchdogTimer.h>\r
-#include <Protocol/HardwareInterrupt.h>\r
 \r
 #include "GenericWatchdog.h"\r
 \r
-// The number of 100ns periods (the unit of time passed to these functions)\r
-// in a second\r
+/* The number of 100ns periods (the unit of time passed to these functions)\r
+   in a second */\r
 #define TIME_UNITS_PER_SECOND 10000000\r
 \r
-// Tick frequency of the generic timer that is the basis of the generic watchdog\r
+// Tick frequency of the generic timer basis of the generic watchdog.\r
 UINTN mTimerFrequencyHz = 0;\r
 \r
-// In cases where the compare register was set manually, information about\r
-// how long the watchdog was asked to wait cannot be retrieved from hardware.\r
-// It is therefore stored here. 0 means the timer is not running.\r
+/* In cases where the compare register was set manually, information about\r
+   how long the watchdog was asked to wait cannot be retrieved from hardware.\r
+   It is therefore stored here. 0 means the timer is not running. */\r
 UINT64 mNumTimerTicks = 0;\r
 \r
-EFI_HARDWARE_INTERRUPT_PROTOCOL *mInterruptProtocol;\r
+EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;\r
 \r
-EFI_STATUS\r
+VOID\r
 WatchdogWriteOffsetRegister (\r
   UINT32  Value\r
   )\r
 {\r
-  return MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r
+  MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);\r
 }\r
 \r
-EFI_STATUS\r
+VOID\r
 WatchdogWriteCompareRegister (\r
   UINT64  Value\r
   )\r
 {\r
-  return MmioWrite64 (GENERIC_WDOG_COMPARE_VALUE_REG, Value);\r
+  MmioWrite64 (GENERIC_WDOG_COMPARE_VALUE_REG, Value);\r
 }\r
 \r
-EFI_STATUS\r
+VOID\r
 WatchdogEnable (\r
   VOID\r
   )\r
 {\r
-  return MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r
+  MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);\r
 }\r
 \r
-EFI_STATUS\r
+VOID\r
 WatchdogDisable (\r
   VOID\r
   )\r
 {\r
-  return MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);\r
+  MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);\r
 }\r
 \r
-/**\r
-    On exiting boot services we must make sure the Watchdog Timer\r
+/** On exiting boot services we must make sure the Watchdog Timer\r
     is stopped.\r
 **/\r
 VOID\r
@@ -90,9 +89,8 @@ WatchdogExitBootServicesEvent (
   mNumTimerTicks = 0;\r
 }\r
 \r
-/*\r
-  This function is called when the watchdog's first signal (WS0) goes high.\r
-  It uses the ResetSystem Runtime Service to reset the board.\r
+/* This function is called when the watchdog's first signal (WS0) goes high.\r
+   It uses the ResetSystem Runtime Service to reset the board.\r
 */\r
 VOID\r
 EFIAPI\r
@@ -101,7 +99,7 @@ WatchdogInterruptHandler (
   IN  EFI_SYSTEM_CONTEXT          SystemContext\r
   )\r
 {\r
-  STATIC CONST CHAR16      ResetString[] = L"The generic watchdog timer ran out.";\r
+  STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";\r
 \r
   WatchdogDisable ();\r
 \r
@@ -111,7 +109,7 @@ WatchdogInterruptHandler (
          EfiResetCold,\r
          EFI_TIMEOUT,\r
          StrSize (ResetString),\r
-         &ResetString\r
+         (VOID *) &ResetString\r
          );\r
 \r
   // If we got here then the reset didn't work\r
@@ -126,10 +124,10 @@ WatchdogInterruptHandler (
   then the new handler is registered and EFI_SUCCESS is returned.\r
   If NotifyFunction is NULL, and a handler is already registered,\r
   then that handler is unregistered.\r
-  If an attempt is made to register a handler when a handler is already registered,\r
-  then EFI_ALREADY_STARTED is returned.\r
-  If an attempt is made to unregister a handler when a handler is not registered,\r
-  then EFI_INVALID_PARAMETER is returned.\r
+  If an attempt is made to register a handler when a handler is already\r
+  registered, then EFI_ALREADY_STARTED is returned.\r
+  If an attempt is made to unregister a handler when a handler is not\r
+  registered, then EFI_INVALID_PARAMETER is returned.\r
 \r
   @param  This             The EFI_TIMER_ARCH_PROTOCOL instance.\r
   @param  NotifyFunction   The function to call when a timer interrupt fires.\r
@@ -139,11 +137,7 @@ WatchdogInterruptHandler (
                            information is used to signal timer based events.\r
                            NULL will unregister the handler.\r
 \r
-  @retval EFI_SUCCESS           The watchdog timer handler was registered.\r
-  @retval EFI_ALREADY_STARTED   NotifyFunction is not NULL, and a handler is already\r
-                                registered.\r
-  @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r
-                                previously registered.\r
+  @retval EFI_UNSUPPORTED       The code does not support NotifyFunction.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -160,18 +154,16 @@ WatchdogRegisterHandler (
 \r
 /**\r
   This function sets the amount of time to wait before firing the watchdog\r
-  timer to TimerPeriod 100 nS units.  If TimerPeriod is 0, then the watchdog\r
+  timer to TimerPeriod 100ns units.  If TimerPeriod is 0, then the watchdog\r
   timer is disabled.\r
 \r
   @param  This             The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
-  @param  TimerPeriod      The amount of time in 100 nS units to wait before the watchdog\r
-                           timer is fired. If TimerPeriod is zero, then the watchdog\r
-                           timer is disabled.\r
+  @param  TimerPeriod      The amount of time in 100ns units to wait before\r
+                           the watchdog timer is fired. If TimerPeriod is zero,\r
+                           then the watchdog timer is disabled.\r
 \r
-  @retval EFI_SUCCESS           The watchdog timer has been programmed to fire in Time\r
-                                100 nS units.\r
-  @retval EFI_DEVICE_ERROR      A watchdog timer could not be programmed due to a device\r
-                                error.\r
+  @retval EFI_SUCCESS           The watchdog timer has been programmed to fire\r
+                                in TimerPeriod 100ns units.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -181,54 +173,47 @@ WatchdogSetTimerPeriod (
   IN UINT64                                   TimerPeriod   // In 100ns units\r
   )\r
 {\r
-  UINTN       TimerVal;\r
-  EFI_STATUS  Status;\r
+  UINTN       SystemCount;\r
 \r
-  // if TimerPerdiod is 0, this is a request to stop the watchdog.\r
+  // if TimerPeriod is 0, this is a request to stop the watchdog.\r
   if (TimerPeriod == 0) {\r
     mNumTimerTicks = 0;\r
-    return WatchdogDisable ();\r
+    WatchdogDisable ();\r
+    return EFI_SUCCESS;\r
   }\r
 \r
   // Work out how many timer ticks will equate to TimerPeriod\r
   mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;\r
 \r
-  //\r
-  // If the number of required ticks is greater than the max number the\r
-  // watchdog's offset register (WOR) can hold, we need to manually compute and\r
-  // set the compare register (WCV)\r
-  //\r
+  /* If the number of required ticks is greater than the max the watchdog's\r
+     offset register (WOR) can hold, we need to manually compute and set\r
+     the compare register (WCV) */\r
   if (mNumTimerTicks > MAX_UINT32) {\r
-    //\r
-    // We need to enable the watchdog *before* writing to the compare register,\r
-    // because enabling the watchdog causes an "explicit refresh", which\r
-    // clobbers the compare register (WCV). In order to make sure this doesn't\r
-    // trigger an interrupt, set the offset to max.\r
-    //\r
-    Status = WatchdogWriteOffsetRegister (MAX_UINT32);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
+    /* We need to enable the watchdog *before* writing to the compare register,\r
+       because enabling the watchdog causes an "explicit refresh", which\r
+       clobbers the compare register (WCV). In order to make sure this doesn't\r
+       trigger an interrupt, set the offset to max. */\r
+    WatchdogWriteOffsetRegister (MAX_UINT32);\r
     WatchdogEnable ();\r
-    TimerVal = ArmGenericTimerGetTimerVal ();\r
-    Status = WatchdogWriteCompareRegister (TimerVal + mNumTimerTicks);\r
+    SystemCount = ArmGenericTimerGetSystemCount ();\r
+    WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);\r
   } else {\r
-    Status = WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r
+    WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);\r
     WatchdogEnable ();\r
   }\r
 \r
-  return Status;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  This function retrieves the period of timer interrupts in 100 ns units,\r
+  This function retrieves the period of timer interrupts in 100ns units,\r
   returns that value in TimerPeriod, and returns EFI_SUCCESS.  If TimerPeriod\r
   is NULL, then EFI_INVALID_PARAMETER is returned.  If a TimerPeriod of 0 is\r
   returned, then the timer is currently disabled.\r
 \r
   @param  This             The EFI_TIMER_ARCH_PROTOCOL instance.\r
-  @param  TimerPeriod      A pointer to the timer period to retrieve in 100\r
-                           ns units. If 0 is returned, then the timer is\r
+  @param  TimerPeriod      A pointer to the timer period to retrieve in\r
+                           100ns units. If 0 is returned, then the timer is\r
                            currently disabled.\r
 \r
 \r
@@ -275,19 +260,19 @@ WatchdogGetTimerPeriod (
         this function will not have any chance of executing.\r
 \r
   @param SetTimerPeriod\r
-  Sets the period of the timer interrupt in 100 nS units.\r
+  Sets the period of the timer interrupt in 100ns units.\r
   This function is optional, and may return EFI_UNSUPPORTED.\r
   If this function is supported, then the timer period will\r
   be rounded up to the nearest supported timer period.\r
 \r
   @param GetTimerPeriod\r
-  Retrieves the period of the timer interrupt in 100 nS units.\r
+  Retrieves the period of the timer interrupt in 100ns units.\r
 \r
 **/\r
 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL    gWatchdogTimer = {\r
-  (EFI_WATCHDOG_TIMER_REGISTER_HANDLER) WatchdogRegisterHandler,\r
-  (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD) WatchdogSetTimerPeriod,\r
-  (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD) WatchdogGetTimerPeriod\r
+  (EFI_WATCHDOG_TIMER_REGISTER_HANDLER)WatchdogRegisterHandler,\r
+  (EFI_WATCHDOG_TIMER_SET_TIMER_PERIOD)WatchdogSetTimerPeriod,\r
+  (EFI_WATCHDOG_TIMER_GET_TIMER_PERIOD)WatchdogGetTimerPeriod\r
 };\r
 \r
 EFI_EVENT                           EfiExitBootServicesEvent = (EFI_EVENT)NULL;\r
@@ -302,11 +287,9 @@ GenericWatchdogEntry (
   EFI_STATUS                      Status;\r
   EFI_HANDLE                      Handle;\r
 \r
-  //\r
-  // Make sure the Watchdog Timer Architectural Protocol has not been installed\r
-  // in the system yet.\r
-  // This will avoid conflicts with the universal watchdog\r
-  //\r
+  /* Make sure the Watchdog Timer Architectural Protocol has not been installed\r
+     in the system yet.\r
+     This will avoid conflicts with the universal watchdog */\r
   ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r
 \r
   mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();\r
@@ -314,38 +297,46 @@ GenericWatchdogEntry (
 \r
   // Register for an ExitBootServicesEvent\r
   Status = gBS->CreateEvent (\r
-                  EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,\r
-                  WatchdogExitBootServicesEvent, NULL, &EfiExitBootServicesEvent\r
+                  EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
+                  TPL_NOTIFY,\r
+                  WatchdogExitBootServicesEvent,\r
+                  NULL,\r
+                  &EfiExitBootServicesEvent\r
                   );\r
   if (!EFI_ERROR (Status)) {\r
     // Install interrupt handler\r
     Status = gBS->LocateProtocol (\r
-                    &gHardwareInterruptProtocolGuid,\r
+                    &gHardwareInterrupt2ProtocolGuid,\r
                     NULL,\r
                     (VOID **)&mInterruptProtocol\r
                     );\r
     if (!EFI_ERROR (Status)) {\r
       Status = mInterruptProtocol->RegisterInterruptSource (\r
-                                    mInterruptProtocol,\r
-                                    FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
-                                    WatchdogInterruptHandler\r
-                                    );\r
+                 mInterruptProtocol,\r
+                 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
+                 WatchdogInterruptHandler\r
+                 );\r
       if (!EFI_ERROR (Status)) {\r
-        // Install the Timer Architectural Protocol onto a new handle\r
-        Handle = NULL;\r
-        Status = gBS->InstallMultipleProtocolInterfaces (\r
-                        &Handle,\r
-                        &gEfiWatchdogTimerArchProtocolGuid, &gWatchdogTimer,\r
-                        NULL\r
-                        );\r
+        Status = mInterruptProtocol->SetTriggerType (\r
+                   mInterruptProtocol,\r
+                   FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),\r
+                   EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING\r
+                   );\r
+        if (!EFI_ERROR (Status)) {\r
+          // Install the Timer Architectural Protocol onto a new handle\r
+          Handle = NULL;\r
+          Status = gBS->InstallMultipleProtocolInterfaces (\r
+                          &Handle,\r
+                          &gEfiWatchdogTimerArchProtocolGuid,\r
+                          &gWatchdogTimer,\r
+                          NULL\r
+                          );\r
+        }\r
       }\r
     }\r
   }\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    // The watchdog failed to initialize\r
-    ASSERT (FALSE);\r
-  }\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   mNumTimerTicks = 0;\r
   WatchdogDisable ();\r