LEON: GPTIMER driver, Timer Library and System Clock for LEON3

With this patch the LEON family can access the GRLIB GPTIMER using
the Timer library (TLIB).

A System Clock driver instead of BSP/clock/ck_init.c is provided
using the TLIB. The classic clock driver is split in two parts,
clock driver and timer driver. The BSPs need only to fullfill the
timer interface instead of the clock interface. Currently only
LEON3 uses it. The LEON2 Timer is not ported to TLIB.

The GPTIMER driver is implemented using the Driver Manager, so the
System Clock Driver is at this point only suitable for LEON3 when
the driver manager is initialized during BSP startup. When the DrvMgr
is not initialized during startup the standard BSP/clock dirver is
used.

LEON2 sometimes also needs to access GPTIMER when a off-chip GRLIB AMBA
systems is connected, for example AMBA-over-PCI.
This commit is contained in:
Daniel Hellstrom
2015-04-17 01:10:16 +02:00
parent 3db9e5b03d
commit cd64fbfb68
12 changed files with 1049 additions and 3 deletions
+6
View File
@@ -28,6 +28,12 @@ EXTRA_DIST += shared/amba/ambapp_names.c
EXTRA_DIST += shared/amba/ambapp_old.c
EXTRA_DIST += shared/amba/ambapp_show.c
# Clock Driver and Timer Library
EXTRA_DIST += shared/include/tlib.h
EXTRA_DIST += shared/timer/gptimer.c
EXTRA_DIST += shared/timer/tlib.c
EXTRA_DIST += shared/timer/tlib_ckinit.c
# PCI bus
EXTRA_DIST += shared/include/pci.h
EXTRA_DIST += shared/pci/pcifinddevice.c
+6
View File
@@ -102,6 +102,12 @@ libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_parent.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_old.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_names.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_show.c
# Clock Driver and Timer Library
include_HEADERS += ../../sparc/shared/include/tlib.h
libbsp_a_SOURCES += ../../sparc/shared/timer/gptimer.c
libbsp_a_SOURCES += ../../sparc/shared/timer/tlib.c
# PCI
libbsp_a_SOURCES += pci/pci.c ../../sparc/shared/pci/pcifinddevice.c
# RASTA Kit
@@ -226,6 +226,7 @@ int cchip1_register(void);
* image bigger.
*/
#define AMBAPPBUS_INFO_AVAIL /* AMBAPP Bus driver */
#define GPTIMER_INFO_AVAIL /* GPTIMER Timer driver */
#ifdef __cplusplus
}
@@ -169,6 +169,10 @@ $(PROJECT_INCLUDE)/grlib.h: ../../sparc/shared/include/grlib.h $(PROJECT_INCLUDE
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/grlib.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/grlib.h
$(PROJECT_INCLUDE)/tlib.h: ../../sparc/shared/include/tlib.h $(PROJECT_INCLUDE)/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/tlib.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/tlib.h
$(PROJECT_INCLUDE)/i2cmst.h: ../../sparc/shared/include/i2cmst.h $(PROJECT_INCLUDE)/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/i2cmst.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/i2cmst.h
+11 -3
View File
@@ -66,14 +66,22 @@ libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_parent.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_old.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_names.c
libbsp_a_SOURCES += ../../sparc/shared/amba/ambapp_show.c
# Clock Driver and Timer Library
include_HEADERS += ../../sparc/shared/include/tlib.h
libbsp_a_SOURCES += ../../sparc/shared/timer/gptimer.c
libbsp_a_SOURCES += ../../sparc/shared/timer/tlib.c
libbsp_a_SOURCES += ../../sparc/shared/timer/tlib_ckinit.c
# non-Driver Manager Clock Implementation
libbsp_a_SOURCES += clock/ckinit.c
libbsp_a_SOURCES += ../../shared/clockdrv_shell.h
# console
libbsp_a_SOURCES += ../../shared/console-termios.c
libbsp_a_SOURCES += console/console.c
# debugio
libbsp_a_SOURCES += console/printk_support.c
# clock
libbsp_a_SOURCES += clock/ckinit.c
libbsp_a_SOURCES += ../../shared/clockdrv_shell.h
# IRQ
include_bsp_HEADERS += \
../../shared/include/irq-generic.h \
@@ -255,6 +255,7 @@ extern const unsigned char LEON3_irq_to_cpu[32];
* image bigger.
*/
#define AMBAPPBUS_INFO_AVAIL /* AMBAPP Bus driver */
#define GPTIMER_INFO_AVAIL /* GPTIMER Timer driver */
#ifdef __cplusplus
}
@@ -105,6 +105,10 @@ $(PROJECT_INCLUDE)/grlib.h: ../../sparc/shared/include/grlib.h $(PROJECT_INCLUDE
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/grlib.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/grlib.h
$(PROJECT_INCLUDE)/tlib.h: ../../sparc/shared/include/tlib.h $(PROJECT_INCLUDE)/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/tlib.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/tlib.h
$(PROJECT_INCLUDE)/bsp/irq-generic.h: ../../shared/include/irq-generic.h $(PROJECT_INCLUDE)/bsp/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/bsp/irq-generic.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/bsp/irq-generic.h
@@ -27,6 +27,9 @@ extern "C" {
#define DRIVER_AMBAPP_ID(vendor, device) \
DRIVER_ID(DRVMGR_BUS_TYPE_AMBAPP, ((((vendor) & 0xff) << 16) | ((device) & 0xfff)))
/*** Gaisler Hardware Device Driver IDs ***/
#define DRIVER_AMBAPP_GAISLER_GPTIMER_ID DRIVER_AMBAPP_ID(VENDOR_GAISLER, GAISLER_GPTIMER)
struct amba_dev_id {
unsigned short vendor;
unsigned short device;
@@ -0,0 +1,169 @@
/*
* Timer Library (TLIB)
*
* The Library rely on timer drivers, the timer presented by the
* timer driver must look like a down-counter timer, which generates
* interrupt (if configured) when underflown.
*
* If Timer hardware is an up-counter the Timer driver must recalculate
* into values that would match as if it was a down-counter.
*
* COPYRIGHT (c) 2011.
* Cobham Gaisler AB.
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
struct tlib_dev;
typedef void (*tlib_isr_t)(void *data);
struct tlib_drv {
/*** Functions ***/
void (*reset)(struct tlib_dev *hand);
void (*get_freq)(
struct tlib_dev *hand,
unsigned int *basefreq,
unsigned int *tickrate);
int (*set_freq)(struct tlib_dev *hand, unsigned int tickrate);
void (*irq_reg)(struct tlib_dev *hand, tlib_isr_t func, void *data);
void (*irq_unreg)(struct tlib_dev *hand, tlib_isr_t func,void *data);
void (*start)(struct tlib_dev *hand, int once);
void (*stop)(struct tlib_dev *hand);
void (*restart)(struct tlib_dev *hand);
void (*get_counter)(struct tlib_dev *hand, unsigned int *counter);
int (*custom)(struct tlib_dev *hand, int cmd, void *arg);
};
struct tlib_dev {
struct tlib_dev *next;
char status; /* 0=closed, 1=open, 2=timer started */
char index; /* Timer Index */
tlib_isr_t isr_func;
void *isr_data;
struct tlib_drv *drv;
};
#ifdef RTEMS_DRVMGR_STARTUP
/* Clock Driver Timer register function. Only used when the TLIB-Clock
* driver is used. A specific Timer is registered as the System Clock
* timer.
*/
extern void Clock_timer_register(int timer_number);
#endif
/* Register Timer. Called by Timer Drivers in order to register
* a Timer to the Timer Library. The registration order determines
* the Timer Number used in tlib_open() to identify a specific
* Timer.
*/
extern int tlib_dev_reg(struct tlib_dev *newdev);
/* Allocate a Timer.
*
* A Timer handle is returned identifying the timer in later calls.
*/
extern void *tlib_open(int timer_no);
/* Close Timer */
extern void tlib_close(void *hand);
/* Returns Number of Timers currently registered to Timer Library */
extern int tlib_ntimer(void);
static inline void tlib_reset(void *hand)
{
struct tlib_dev *dev = hand;
dev->drv->reset(dev);
}
/* Get Frequencies:
* - Base Frequency (unchangable base freq rate of timer, prescaler, clkinput)
* - Current Tick Rate [in multiples of Base Frequency]
*/
static inline void tlib_get_freq(
void *hand,
unsigned int *basefreq,
unsigned int *tickrate)
{
struct tlib_dev *dev = hand;
dev->drv->get_freq(dev, basefreq, tickrate);
}
/* Set current Tick Rate in number of "Base-Frequency ticks" */
static inline int tlib_set_freq(void *hand, unsigned int tickrate)
{
struct tlib_dev *dev = hand;
return dev->drv->set_freq(dev, tickrate);
}
/* Register ISR at Timer ISR */
static inline void tlib_irq_unregister(void *hand)
{
struct tlib_dev *dev = hand;
if ( dev->isr_func ) {
dev->drv->irq_unreg(dev, dev->isr_func, dev->isr_data);
dev->isr_func = NULL;
}
}
/* Register ISR at Timer ISR */
static inline void tlib_irq_register(void *hand, tlib_isr_t func, void *data)
{
struct tlib_dev *dev = hand;
/* Unregister previous ISR if installed */
tlib_irq_unregister(hand);
dev->isr_func = func;
dev->isr_data = data;
dev->drv->irq_reg(dev, func, data);
}
/* Start Timer, ISRs will be generated if enabled.
*
* once determines if timer should restart (=0) on underflow automatically,
* or stop when underflow is reached (=1).
*/
static inline void tlib_start(void *hand, int once)
{
struct tlib_dev *dev = hand;
dev->drv->start(dev, once);
}
/* Stop Timer, no more ISRs will be generated */
static inline void tlib_stop(void *hand)
{
struct tlib_dev *dev = hand;
dev->drv->stop(dev);
}
/* Restart/Reload Timer, may be usefull if a Watchdog Timer */
static inline void tlib_restart(void *hand)
{
struct tlib_dev *dev = hand;
dev->drv->restart(dev);
}
/* Get current counter value (since last tick) */
static inline void tlib_get_counter(void *hand, unsigned int *counter)
{
struct tlib_dev *dev = hand;
dev->drv->get_counter(dev, counter);
}
/* Do a custom operation */
static inline void tlib_custom(void *hand, int cmd, void *arg)
{
struct tlib_dev *dev = hand;
dev->drv->custom(dev, cmd, arg);
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,77 @@
/*
* Timer Library (TLIB)
*
* COPYRIGHT (c) 2011.
* Cobham Gaisler AB.
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#include <rtems.h>
#include <tlib.h>
struct tlib_dev *tlib_dev_head = NULL;
struct tlib_dev *tlib_dev_tail = NULL;
static int tlib_dev_cnt = 0;
/* Register Timer device to Timer Library */
int tlib_dev_reg(struct tlib_dev *newdev)
{
/* Reset device */
newdev->status = 0;
newdev->isr_func = NULL;
newdev->index = tlib_dev_cnt;
/* Insert last in queue */
newdev->next = NULL;
if ( tlib_dev_tail == NULL ) {
tlib_dev_head = newdev;
} else {
tlib_dev_tail->next = newdev;
}
tlib_dev_tail = newdev;
/* Return Index of Registered Timer */
return tlib_dev_cnt++;
}
void *tlib_open(int timer_no)
{
struct tlib_dev *dev;
if ( timer_no < 0 )
return NULL;
dev = tlib_dev_head;
while ( (timer_no > 0) && dev ) {
timer_no--;
dev = dev->next;
}
if ( dev ) {
if ( dev->status )
return NULL;
dev->status = 1;
/* Reset Timer to initial state */
tlib_reset(dev);
}
return dev;
}
void tlib_close(void *hand)
{
struct tlib_dev *dev = hand;
/* Stop any ongoing timer operation and unregister IRQ if registered */
tlib_stop(dev);
tlib_irq_unregister(dev);
/* Mark not open */
dev->status = 0;
}
int tlib_ntimer(void)
{
return tlib_dev_cnt;
}
@@ -0,0 +1,264 @@
/*
* Clock Tick Device Driver using Timer Library implemented
* by the GRLIB GPTIMER / LEON2 Timer drivers.
*
* COPYRIGHT (c) 2010.
* Cobham Gaisler AB.
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*
*/
#include <stdlib.h>
#include <bsp.h>
#include <tlib.h>
/* Undefine (default) this to save space in standard LEON configurations,
* it will assume that Prescaler is running at 1MHz.
*/
/*#undef CLOCK_DRIVER_DONT_ASSUME_PRESCALER_1MHZ*/
/* Set the below defines from bsp.h if function needed.
#undef CLOCK_DRIVER_ISRS_PER_TICK
#undef CLOCK_DRIVER_USE_FAST_IDLE
*/
#define Clock_driver_support_at_tick()
/*
* Number of Clock ticks since initialization
*/
volatile uint32_t Clock_driver_ticks;
/*
* Timer Number in Timer Library. Defaults to the first Timer in
* the System.
*/
int Clock_timer = 0;
/*
* Timer Handle in Timer Library
*/
void *Clock_handle = NULL;
#ifdef CLOCK_DRIVER_DONT_ASSUME_PRESCALER_1MHZ
unsigned int Clock_basefreq;
#endif
void Clock_exit(void);
/*
* Major and minor number.
*/
rtems_device_major_number rtems_clock_major = UINT32_MAX;
rtems_device_minor_number rtems_clock_minor;
/*
* Clock_isr
*
* This is the clock tick interrupt handler.
*
* Input parameters:
* vector - vector number
*
* Output parameters: NONE
*
* Return values: NONE
*
*/
void Clock_isr(void *arg_unused)
{
/*
* Accurate count of ISRs
*/
Clock_driver_ticks += 1;
#ifdef CLOCK_DRIVER_USE_FAST_IDLE
do {
rtems_clock_tick();
} while ( _Thread_Executing == _Thread_Idle &&
_Thread_Heir == _Thread_Executing);
Clock_driver_support_at_tick();
return;
#else
/*
* Add custom handling at every tick from bsp.h
*/
Clock_driver_support_at_tick();
#ifdef CLOCK_DRIVER_ISRS_PER_TICK
/*
* The driver is multiple ISRs per clock tick.
*/
if ( !Clock_driver_isrs ) {
rtems_clock_tick();
Clock_driver_isrs = CLOCK_DRIVER_ISRS_PER_TICK;
}
Clock_driver_isrs--;
#else
/*
* The driver is one ISR per clock tick.
*/
rtems_clock_tick();
#endif
#endif
}
/*
* Clock_exit
*
* This routine allows the clock driver to exit by masking the interrupt and
* disabling the clock's counter.
*
* Input parameters: NONE
*
* Output parameters: NONE
*
* Return values: NONE
*
*/
void Clock_exit( void )
{
/* Stop all activity of the Timer, no more ISRs.
* We could be using tlib_close(), however tlib_stop() is quicker
* and independent of IRQ unregister code.
*/
if ( Clock_handle ) {
tlib_stop(Clock_handle);
Clock_handle = NULL;
}
}
uint32_t Clock_nanoseconds_since_last_tick(void)
{
uint32_t clicks;
if ( !Clock_handle )
return 0;
tlib_get_counter(Clock_handle, (unsigned int *)&clicks);
#ifdef CLOCK_DRIVER_DONT_ASSUME_PRESCALER_1MHZ
{
/* Down counter. Calc from BaseFreq. */
uint64_t tmp;
tmp = ((uint64_t)clicks * 1000000000) / ((uint64_t)Clock_basefreq);
return (uint32_t)tmp;
}
#else
/* Down counter. Timer base frequency is initialized to 1 MHz */
return (uint32_t)
(rtems_configuration_get_microseconds_per_tick() - clicks) * 1000;
#endif
}
/*
* Clock_initialize
*
* This routine initializes the clock driver and starts the Clock.
*
* Input parameters:
* major - clock device major number
* minor - clock device minor number
* parg - pointer to optional device driver arguments
*
* Output parameters: NONE
*
* Return values:
* rtems_device_driver status code
*/
rtems_device_driver Clock_initialize(
rtems_device_major_number major,
rtems_device_minor_number minor,
void *pargp
)
{
unsigned int tick_hz;
/*
* Take Timer that should be used as system timer.
*
*/
Clock_handle = tlib_open(Clock_timer);
if ( Clock_handle == NULL ) {
/* System Clock Timer not found */
return RTEMS_NOT_DEFINED;
}
/*
* Install Clock ISR before starting timer
*/
tlib_irq_register(Clock_handle, Clock_isr, NULL);
/* Set Timer Frequency to tick at Configured value. The Timer
* Frequency is set in multiples of the timer base frequency.
*
* In standard LEON3 designs the base frequency is is 1MHz, to
* save instructions define CLOCK_DRIVER_ASSUME_PRESCALER_1MHZ
* to avoid 64-bit calculation.
*/
#ifdef CLOCK_DRIVER_DONT_ASSUME_PRESCALER_1MHZ
{
uint64_t tmp;
tlib_get_freq(Clock_handle, &Clock_basefreq, NULL);
tmp = (uint64_t)Clock_basefreq;
tmp = tmp * (unint64_t)rtems_configuration_get_microseconds_per_tick();
tick_hz = tmp / 1000000;
}
#else
tick_hz = rtems_configuration_get_microseconds_per_tick();
#endif
tlib_set_freq(Clock_handle, tick_hz);
rtems_clock_set_nanoseconds_extension(Clock_nanoseconds_since_last_tick);
/*
* IRQ and Frequency is setup, now we start the Timer. IRQ is still
* disabled globally during startup, so IRQ will hold for a while.
*/
tlib_start(Clock_handle, 0);
/*
* Register function called at system shutdown
*/
atexit( Clock_exit );
/*
* make major/minor avail to others such as shared memory driver
*/
rtems_clock_major = major;
rtems_clock_minor = minor;
/*
* If we are counting ISRs per tick, then initialize the counter.
*/
#ifdef CLOCK_DRIVER_ISRS_PER_TICK
Clock_driver_isrs = CLOCK_DRIVER_ISRS_PER_TICK;
#endif
return RTEMS_SUCCESSFUL;
}
/*** Timer Driver Interface ***/
void Clock_timer_register(int timer_number)
{
Clock_timer = timer_number;
}