123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- /*++
- Copyright (c) 2014 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- timer.c
- Abstract:
- This module implements platform timer services for the TI AM335x. These
- timers function identically to the OMAP4 timers, and could be merged.
- Author:
- Evan Green 19-Dec-2014
- Environment:
- Firmware
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <uefifw.h>
- #include "bbonefw.h"
- #include <minoca/soc/am335x.h>
- //
- // --------------------------------------------------------------------- Macros
- //
- //
- // This macro reads from an AM335 timer. _Base should be a pointer, and
- // _Register should be a AM335_TIMER_REGISTER value.
- //
- #define READ_TIMER_REGISTER(_Base, _Register) \
- EfiReadRegister32((VOID *)(_Base) + (_Register))
- //
- // This macro writes to an AM335 timer. _Base should be a pointer,
- // _Register should be GP_TIMER_REGISTER value, and _Value should be a ULONG.
- //
- #define WRITE_TIMER_REGISTER(_Base, _Register, _Value) \
- EfiWriteRegister32((VOID *)(_Base) + (_Register), (_Value))
- //
- // These macros read from and write to the watchdog timer.
- //
- #define AM335_READ_WATCHDOG(_Register) \
- EfiReadRegister32((VOID *)AM335_WATCHDOG_BASE + (_Register))
- #define AM335_WRITE_WATCHDOG(_Register, _Value) \
- EfiWriteRegister32((VOID *)AM335_WATCHDOG_BASE + (_Register), (_Value))
- //
- // These macros read from and write to the RTC.
- //
- #define AM3_READ_RTC(_Register) \
- *(volatile UINT32 *)(AM335_RTC_BASE + (_Register))
- #define AM3_WRITE_RTC(_Register, _Value) \
- *((volatile UINT32 *)(AM335_RTC_BASE + (_Register))) = (_Value)
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure stores the internal state associated with an AM335 DM
- timer.
- Members:
- Base - Stores the virtual address of the timer.
- Index - Stores the zero-based index of this timer within the timer block.
- --*/
- typedef struct _AM335_TIMER_DATA {
- VOID *Base;
- UINT32 Index;
- UINT32 Offset;
- } AM335_TIMER_DATA, *PAM335_TIMER_DATA;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- EfipPlatformServiceTimerInterrupt (
- UINT32 InterruptNumber
- );
- UINT64
- EfipPlatformReadTimer (
- VOID
- );
- VOID
- EfipAm335TimerInitialize (
- PAM335_TIMER_DATA Context
- );
- UINT64
- EfipAm335TimerRead (
- PAM335_TIMER_DATA Context
- );
- VOID
- EfipAm335TimerArm (
- PAM335_TIMER_DATA Context,
- BOOLEAN Periodic,
- UINT64 TickCount
- );
- VOID
- EfipAm335TimerDisarm (
- PAM335_TIMER_DATA Context
- );
- VOID
- EfipAm335TimerAcknowledgeInterrupt (
- PAM335_TIMER_DATA Context
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- AM335_TIMER_DATA EfiBeagleBoneClockTimer;
- AM335_TIMER_DATA EfiBeagleBoneTimeCounter;
- //
- // ------------------------------------------------------------------ Functions
- //
- EFIAPI
- EFI_STATUS
- EfiPlatformSetWatchdogTimer (
- UINTN Timeout,
- UINT64 WatchdogCode,
- UINTN DataSize,
- CHAR16 *WatchdogData
- )
- /*++
- Routine Description:
- This routine sets the system's watchdog timer.
- Arguments:
- Timeout - Supplies the number of seconds to set the timer for.
- WatchdogCode - Supplies a numeric code to log on a watchdog timeout event.
- DataSize - Supplies the size of the watchdog data.
- WatchdogData - Supplies an optional buffer that includes a null-terminated
- string, optionally followed by additional binary data.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INVALID_PARAMETER if the supplied watchdog code is invalid.
- EFI_UNSUPPORTED if there is no watchdog timer.
- EFI_DEVICE_ERROR if an error occurred accessing the device hardware.
- --*/
- {
- UINT32 Count;
- Count = 0 - (Timeout * AM335_WATCHDOG_FREQUENCY);
- //
- // First, disable the watchdog timer.
- //
- AM335_WRITE_WATCHDOG(Am335WatchdogStartStop, AM335_WATCHDOG_DISABLE1);
- EfiStall(1000);
- AM335_WRITE_WATCHDOG(Am335WatchdogStartStop, AM335_WATCHDOG_DISABLE2);
- EfiStall(1000);
- //
- // If the watchdog timer is being enabled, set the count value and fire it
- // back up.
- //
- if ((Count != 0) && (EfiDisableWatchdog == FALSE)) {
- AM335_WRITE_WATCHDOG(Am335WatchdogLoadCount, Count);
- EfiStall(1000);
- AM335_WRITE_WATCHDOG(Am335WatchdogCurrentCount, Count);
- EfiStall(1000);
- AM335_WRITE_WATCHDOG(Am335WatchdogStartStop, AM335_WATCHDOG_ENABLE1);
- EfiStall(1000);
- AM335_WRITE_WATCHDOG(Am335WatchdogStartStop, AM335_WATCHDOG_ENABLE2);
- }
- return EFI_SUCCESS;
- }
- EFI_STATUS
- EfiPlatformInitializeTimers (
- UINT32 *ClockTimerInterruptNumber,
- EFI_PLATFORM_SERVICE_TIMER_INTERRUPT *ClockTimerServiceRoutine,
- EFI_PLATFORM_READ_TIMER *ReadTimerRoutine,
- UINT64 *ReadTimerFrequency,
- UINT32 *ReadTimerWidth
- )
- /*++
- Routine Description:
- This routine initializes platform timer services. There are actually two
- different timer services returned in this routine. The periodic timer tick
- provides a periodic interrupt. The read timer provides a free running
- counter value. These are likely serviced by different timers. For the
- periodic timer tick, this routine should start the periodic interrupts
- coming in. The periodic rate of the timer can be anything reasonable, as
- the time counter will be used to count actual duration. The rate should be
- greater than twice the rollover rate of the time counter to ensure proper
- time accounting. Interrupts are disabled at the processor core for the
- duration of this routine.
- Arguments:
- ClockTimerInterruptNumber - Supplies a pointer where the interrupt line
- number of the periodic timer tick will be returned.
- ClockTimerServiceRoutine - Supplies a pointer where a pointer to a routine
- called when the periodic timer tick interrupt occurs will be returned.
- ReadTimerRoutine - Supplies a pointer where a pointer to a routine
- called to read the current timer value will be returned.
- ReadTimerFrequency - Supplies the frequency of the counter.
- ReadTimerWidth - Supplies a pointer where the read timer bit width will be
- returned.
- Return Value:
- EFI Status code.
- --*/
- {
- EFI_STATUS Status;
- *ClockTimerInterruptNumber = AM335_IRQ_DMTIMER0;
- *ClockTimerServiceRoutine = EfipPlatformServiceTimerInterrupt;
- *ReadTimerRoutine = EfipPlatformReadTimer;
- *ReadTimerFrequency = AM335_32KHZ_FREQUENCY;
- *ReadTimerWidth = 32;
- //
- // Use GP timer 0 for the clock timer and GP timer 2 for the time counter.
- // Both run at 32kHz.
- //
- EfiBeagleBoneClockTimer.Base = (VOID *)AM335_DMTIMER0_BASE;
- EfiBeagleBoneClockTimer.Index = 0;
- EfiBeagleBoneTimeCounter.Base = (VOID *)AM335_DMTIMER2_BASE;
- EfiBeagleBoneTimeCounter.Index = 2;
- EfipAm335TimerInitialize(&EfiBeagleBoneClockTimer);
- EfipAm335TimerArm(&EfiBeagleBoneClockTimer,
- TRUE,
- BEAGLEBONE_TIMER_TICK_COUNT);
- EfipAm335TimerInitialize(&EfiBeagleBoneTimeCounter);
- Status = EfipPlatformSetInterruptLineState(*ClockTimerInterruptNumber,
- TRUE,
- FALSE);
- return Status;
- }
- VOID
- EfiPlatformTerminateTimers (
- VOID
- )
- /*++
- Routine Description:
- This routine terminates timer services in preparation for the termination
- of boot services.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- EfipAm335TimerDisarm(&EfiBeagleBoneClockTimer);
- return;
- }
- VOID
- EfipBeagleBoneBlackInitializeRtc (
- VOID
- )
- /*++
- Routine Description:
- This routine fires up the RTC in the AM335x for the BeagleBone Black, if it
- is not already running.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- UINT32 Control;
- UINT32 Status;
- UINT32 Value;
- //
- // Set the RTC to smart idle wakeup-capable.
- //
- Value = AM335_RTC_SYS_CONFIG_IDLE_MODE_SMART_WAKEUP;
- AM3_WRITE_RTC(Am335RtcSysConfig, Value);
- //
- // If the RTC is already running, then it's been set up from a previous
- // boot.
- //
- Status = AM3_READ_RTC(Am335RtcStatus);
- if ((Status & AM335_RTC_STATUS_RUN) != 0) {
- goto BeagleBoneBlackInitializeRtcEnd;
- }
- //
- // If the RTC has been disabled by a previous boot, leave it alone, as the
- // spec seems to indicate there's no turning it back on once it's off.
- //
- Control = AM3_READ_RTC(Am335RtcControl);
- if ((Control & AM335_RTC_CONTROL_RTC_DISABLE) != 0) {
- return;
- }
- //
- // Unlock the RTC to program it.
- //
- AM3_WRITE_RTC(Am335RtcKick0, AM335_RTC_KICK0_KEY);
- AM3_WRITE_RTC(Am335RtcKick1, AM335_RTC_KICK1_KEY);
- //
- // Select the internal clock source, and enable inputs.
- //
- Value = AM3_READ_RTC(Am335RtcOscillator);
- Value &= ~AM335_RTC_OSCILLATOR_SOURCE_EXTERNAL;
- AM3_WRITE_RTC(Am335RtcOscillator, Value);
- Value |= AM335_RTC_OSCILLATOR_ENABLE;
- AM3_WRITE_RTC(Am335RtcOscillator, Value);
- //
- // Start the RTC running in 24 hour mode.
- //
- Value = AM335_RTC_CONTROL_RUN;
- AM3_WRITE_RTC(Am335RtcControl, Value);
- do {
- Value = AM3_READ_RTC(Am335RtcStatus);
- } while ((Value & AM335_RTC_STATUS_RUN) == 0);
- //
- // Lock the RTC to prevent accidental writes.
- //
- AM3_WRITE_RTC(Am335RtcKick0, AM335_RTC_KICK0_KEY);
- AM3_WRITE_RTC(Am335RtcKick1, 0xFFFFFFFF);
- BeagleBoneBlackInitializeRtcEnd:
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- EfipPlatformServiceTimerInterrupt (
- UINT32 InterruptNumber
- )
- /*++
- Routine Description:
- This routine is called to acknowledge a platform timer interrupt. This
- routine is responsible for quiescing the interrupt.
- Arguments:
- InterruptNumber - Supplies the interrupt number that occurred.
- Return Value:
- None.
- --*/
- {
- EfipAm335TimerAcknowledgeInterrupt(&EfiBeagleBoneClockTimer);
- return;
- }
- UINT64
- EfipPlatformReadTimer (
- VOID
- )
- /*++
- Routine Description:
- This routine is called to read the current platform time value. The timer
- is assumed to be free running at a constant frequency, and should have a
- bit width as reported in the initialize function. The UEFI core will
- manage software bit extension out to 64 bits, this routine should just
- reporte the hardware timer value.
- Arguments:
- None.
- Return Value:
- Returns the hardware timer value.
- --*/
- {
- return EfipAm335TimerRead(&EfiBeagleBoneTimeCounter);
- }
- VOID
- EfipAm335TimerInitialize (
- PAM335_TIMER_DATA Context
- )
- /*++
- Routine Description:
- This routine initializes an AM335 timer.
- Arguments:
- Context - Supplies the pointer to the timer's context.
- Return Value:
- None.
- --*/
- {
- UINT32 Value;
- if (Context->Base == NULL) {
- return;
- }
- //
- // Program the timer in free running mode with no interrupt.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerOcpConfig,
- AM335_TIMER_IDLEMODE_SMART);
- //
- // Disable wakeup functionality.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptWakeEnable,
- 0);
- //
- // Set the synchronous interface configuration register to non-posted mode,
- // which means that writes don't return until they complete. Posted mode
- // is faster for writes but requires polling a bit for reads.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerSynchronousInterfaceControl,
- 0);
- //
- // Disable all interrupts for now. The alternate register interface uses a
- // set/clear style for the interrupt mask bits.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptEnableClear,
- AM335_TIMER_INTERRUPT_MASK);
- //
- // Set the load value to zero to create a free-running timer, and reset the
- // current counter now too.
- //
- WRITE_TIMER_REGISTER(Context->Base, Am335TimerLoad, 0);
- WRITE_TIMER_REGISTER(Context->Base, Am335TimerCount, 0);
- //
- // Set the mode register to auto-reload, and start the timer.
- //
- Value = AM335_TIMER_OVERFLOW_TRIGGER | AM335_TIMER_STARTED |
- AM335_TIMER_AUTORELOAD;
- WRITE_TIMER_REGISTER(Context->Base, Am335TimerControl, Value);
- //
- // Reset all interrupt-pending bits.
- //
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptStatus,
- AM335_TIMER_INTERRUPT_MASK);
- return;
- }
- UINT64
- EfipAm335TimerRead (
- PAM335_TIMER_DATA Context
- )
- /*++
- Routine Description:
- This routine returns the hardware counter's raw value.
- Arguments:
- Context - Supplies the pointer to the timer's context.
- Return Value:
- Returns the timer's current count.
- --*/
- {
- UINT32 Value;
- Value = READ_TIMER_REGISTER(Context->Base, Am335TimerCount);
- return Value;
- }
- VOID
- EfipAm335TimerArm (
- PAM335_TIMER_DATA Context,
- BOOLEAN Periodic,
- UINT64 TickCount
- )
- /*++
- Routine Description:
- This routine arms the timer to fire an interrupt after the specified number
- of ticks.
- Arguments:
- Context - Supplies the pointer to the timer's context.
- Periodic - Supplies a boolean indicating if the timer should be armed
- periodically or one-shot.
- TickCount - Supplies the interval, in ticks, from now for the timer to fire
- in.
- Return Value:
- None.
- --*/
- {
- UINT32 Value;
- if (TickCount >= 0xFFFFFFFF) {
- TickCount = 0xFFFFFFFF;
- }
- //
- // Start the timer ticking.
- //
- WRITE_TIMER_REGISTER(Context->Base, Am335TimerControl, 0);
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerLoad,
- 0xFFFFFFFF - (UINT32)TickCount);
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerCount,
- 0xFFFFFFFF - (UINT32)TickCount);
- Value = AM335_TIMER_STARTED;
- if (Periodic != FALSE) {
- Value |= AM335_TIMER_AUTORELOAD;
- }
- WRITE_TIMER_REGISTER(Context->Base, Am335TimerControl, Value);
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptEnableSet,
- AM335_TIMER_OVERFLOW_INTERRUPT);
- return;
- }
- VOID
- EfipAm335TimerDisarm (
- PAM335_TIMER_DATA Context
- )
- /*++
- Routine Description:
- This routine disarms the timer, stopping interrupts from firing.
- Arguments:
- Context - Supplies the pointer to the timer's context.
- Return Value:
- None.
- --*/
- {
- //
- // Disable all interrupts.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptEnableClear,
- AM335_TIMER_INTERRUPT_MASK);
- //
- // Reset all interrupt-pending bits.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptStatus,
- AM335_TIMER_INTERRUPT_MASK);
- return;
- }
- VOID
- EfipAm335TimerAcknowledgeInterrupt (
- PAM335_TIMER_DATA Context
- )
- /*++
- Routine Description:
- This routine performs any actions necessary upon reciept of a timer's
- interrupt. This may involve writing to an acknowledge register to re-enable
- the timer to fire again, or other hardware specific actions.
- Arguments:
- Context - Supplies the pointer to the timer's context.
- Return Value:
- None.
- --*/
- {
- //
- // Clear the overflow interrupt by writing a 1 to the status bit.
- //
- WRITE_TIMER_REGISTER(Context->Base,
- Am335TimerInterruptStatus,
- AM335_TIMER_OVERFLOW_INTERRUPT);
- return;
- }
|