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