X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ArmPlatformPkg%2FDrivers%2FSP805WatchdogDxe%2FSP805Watchdog.c;h=5821dc1958222637b214628d044c9bbbf14a3ca5;hp=12c2f0a1fe499688fc318e7b64c27a8dcf985ec6;hb=HEAD;hpb=e3fa3d83e7c7e8830b31c9d916c0206a71bf2b48 diff --git a/ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c b/ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c index 12c2f0a1fe..5821dc1958 100644 --- a/ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c +++ b/ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c @@ -1,18 +1,11 @@ /** @file -* -* Copyright (c) 2011-2013, ARM Limited. All rights reserved. -* Copyright (c) 2018, Linaro Limited. All rights reserved. -* -* This program and the accompanying materials -* are licensed and made available under the terms and conditions of the BSD License -* which accompanies this distribution. The full text of the license may be found at -* http://opensource.org/licenses/bsd-license.php -* -* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -* -**/ + Copyright (c) 2011-2013, ARM Limited. All rights reserved. + Copyright (c) 2018, Linaro Limited. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ #include @@ -21,12 +14,17 @@ #include #include #include +#include +#include #include #include "SP805Watchdog.h" -STATIC EFI_EVENT mEfiExitBootServicesEvent; +STATIC EFI_EVENT mEfiExitBootServicesEvent; +STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL *mInterrupt; +STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify; +STATIC UINT32 mTimerPeriod; /** Make sure the SP805 registers are unlocked for writing. @@ -65,6 +63,33 @@ SP805Lock ( } } +STATIC +VOID +EFIAPI +SP805InterruptHandler ( + IN HARDWARE_INTERRUPT_SOURCE Source, + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + SP805Unlock (); + MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq + SP805Lock (); + + mInterrupt->EndOfInterrupt (mInterrupt, Source); + + // + // The notify function should be called with the elapsed number of ticks + // since the watchdog was armed, which should exceed the timer period. + // We don't actually know the elapsed number of ticks, so let's return + // the timer period plus 1. + // + if (mWatchdogNotify != NULL) { + mWatchdogNotify (mTimerPeriod + 1); + } + + gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL); +} + /** Stop the SP805 watchdog timer from counting down by disabling interrupts. **/ @@ -145,13 +170,20 @@ STATIC EFI_STATUS EFIAPI SP805RegisterHandler ( - IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, - IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction + IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction ) { - // ERROR: This function is not supported. - // The hardware watchdog will reset the board - return EFI_INVALID_PARAMETER; + if ((mWatchdogNotify == NULL) && (NotifyFunction == NULL)) { + return EFI_INVALID_PARAMETER; + } + + if ((mWatchdogNotify != NULL) && (NotifyFunction != NULL)) { + return EFI_ALREADY_STARTED; + } + + mWatchdogNotify = NotifyFunction; + return EFI_SUCCESS; } /** @@ -186,8 +218,8 @@ STATIC EFI_STATUS EFIAPI SP805SetTimerPeriod ( - IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, - IN UINT64 TimerPeriod // In 100ns units + IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + IN UINT64 TimerPeriod // In 100ns units ) { EFI_STATUS Status; @@ -202,19 +234,16 @@ SP805SetTimerPeriod ( SP805Stop (); } else { // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds - // The SP805 will count down to ZERO once, generate an interrupt and - // then it will again reload the initial value and start again. - // On the second time when it reaches ZERO, it will actually reset the board. - // Therefore, we need to load half the required delay. + // The SP805 will count down to zero and generate an interrupt. // - // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz) / 2 ; + // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz); // // i.e.: // - // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 20 MHz ; + // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ; Ticks64bit = MultU64x32 (TimerPeriod, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz)); - Ticks64bit = DivU64x32 (Ticks64bit, 20000000); + Ticks64bit = DivU64x32 (Ticks64bit, 10 * 1000 * 1000); // The registers in the SP805 are only 32 bits if (Ticks64bit > MAX_UINT32) { @@ -233,9 +262,12 @@ SP805SetTimerPeriod ( SP805Start (); } + mTimerPeriod = TimerPeriod; + EXIT: // Ensure the watchdog is locked before exiting. SP805Lock (); + ASSERT_EFI_ERROR (Status); return Status; } @@ -258,29 +290,15 @@ STATIC EFI_STATUS EFIAPI SP805GetTimerPeriod ( - IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, - OUT UINT64 *TimerPeriod + IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This, + OUT UINT64 *TimerPeriod ) { - UINT64 ReturnValue; - if (TimerPeriod == NULL) { return EFI_INVALID_PARAMETER; } - // Check if the watchdog is stopped - if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) { - // It is stopped, so return zero. - ReturnValue = 0; - } else { - // Convert the Watchdog ticks into TimerPeriod - // Ensure 64bit arithmetic throughout because the Watchdog ticks may already - // be at the maximum 32 bit value and we still need to multiply that by 600. - ReturnValue = MultU64x32 (MmioRead32 (SP805_WDOG_LOAD_REG), 600); - } - - *TimerPeriod = ReturnValue; - + *TimerPeriod = mTimerPeriod; return EFI_SUCCESS; } @@ -316,7 +334,7 @@ SP805GetTimerPeriod ( Retrieves the period of the timer interrupt in 100 nS units. **/ -STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = { +STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = { SP805RegisterHandler, SP805SetTimerPeriod, SP805GetTimerPeriod @@ -336,13 +354,21 @@ STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = { EFI_STATUS EFIAPI SP805Initialize ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable ) { EFI_STATUS Status; EFI_HANDLE Handle; + // Find the interrupt controller protocol. ASSERT if not found. + Status = gBS->LocateProtocol ( + &gHardwareInterruptProtocolGuid, + NULL, + (VOID **)&mInterrupt + ); + ASSERT_EFI_ERROR (Status); + // Unlock access to the SP805 registers SP805Unlock (); @@ -350,13 +376,40 @@ SP805Initialize ( SP805Stop (); // Set the watchdog to reset the board when triggered + // This is a last resort in case the interrupt handler fails if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_RESEN) == 0) { MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_RESEN); } + // Clear any pending interrupts + MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq + // Prohibit any rogue access to SP805 registers SP805Lock (); + if (PcdGet32 (PcdSP805WatchdogInterrupt) > 0) { + Status = mInterrupt->RegisterInterruptSource ( + mInterrupt, + PcdGet32 (PcdSP805WatchdogInterrupt), + SP805InterruptHandler + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: failed to register watchdog interrupt - %r\n", + __FUNCTION__, + Status + )); + return Status; + } + } else { + DEBUG (( + DEBUG_WARN, + "%a: no interrupt specified, running in RESET mode only\n", + __FUNCTION__ + )); + } + // // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet. // This will avoid conflicts with the universal watchdog @@ -364,8 +417,13 @@ SP805Initialize ( ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid); // Register for an ExitBootServicesEvent - Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, - ExitBootServicesEvent, NULL, &mEfiExitBootServicesEvent); + Status = gBS->CreateEvent ( + EVT_SIGNAL_EXIT_BOOT_SERVICES, + TPL_NOTIFY, + ExitBootServicesEvent, + NULL, + &mEfiExitBootServicesEvent + ); if (EFI_ERROR (Status)) { Status = EFI_OUT_OF_RESOURCES; goto EXIT; @@ -375,7 +433,8 @@ SP805Initialize ( Handle = NULL; Status = gBS->InstallMultipleProtocolInterfaces ( &Handle, - &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer, + &gEfiWatchdogTimerArchProtocolGuid, + &mWatchdogTimer, NULL ); if (EFI_ERROR (Status)) {