2012-10-08 03:38:03 +02:00
|
|
|
#include "kernel/kernel.h"
|
|
|
|
#include "kernel/clock.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <machine/cpu.h>
|
2013-12-13 14:12:50 +01:00
|
|
|
#include <minix/board.h>
|
2013-05-23 18:02:23 +02:00
|
|
|
#include <minix/mmio.h>
|
2013-06-11 16:07:43 +02:00
|
|
|
#include <assert.h>
|
2012-10-08 03:38:03 +02:00
|
|
|
#include <io.h>
|
2013-05-23 18:02:23 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2012-10-08 03:38:03 +02:00
|
|
|
#include "arch_proto.h"
|
|
|
|
#include "omap_timer.h"
|
|
|
|
#include "omap_intr.h"
|
|
|
|
|
|
|
|
static irq_hook_t omap3_timer_hook; /* interrupt handler hook */
|
2013-02-03 19:28:24 +01:00
|
|
|
static u64_t high_frc;
|
|
|
|
|
2013-05-23 18:02:23 +02:00
|
|
|
struct omap_timer_registers;
|
|
|
|
|
|
|
|
struct omap_timer {
|
|
|
|
vir_bytes base;
|
|
|
|
int irq_nr;
|
|
|
|
struct omap_timer_registers *regs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct omap_timer_registers {
|
|
|
|
vir_bytes TIDR;
|
|
|
|
vir_bytes TIOCP_CFG;
|
|
|
|
vir_bytes TISTAT;
|
|
|
|
vir_bytes TISR;
|
|
|
|
vir_bytes TIER;
|
|
|
|
vir_bytes TWER;
|
|
|
|
vir_bytes TCLR;
|
|
|
|
vir_bytes TCRR;
|
|
|
|
vir_bytes TLDR;
|
|
|
|
vir_bytes TTGR;
|
|
|
|
vir_bytes TWPS;
|
|
|
|
vir_bytes TMAR;
|
|
|
|
vir_bytes TCAR1;
|
|
|
|
vir_bytes TSICR;
|
|
|
|
vir_bytes TCAR2;
|
|
|
|
vir_bytes TPIR;
|
|
|
|
vir_bytes TNIR;
|
|
|
|
vir_bytes TCVR;
|
|
|
|
vir_bytes TOCR;
|
|
|
|
vir_bytes TOWR;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct omap_timer_registers regs_v1 = {
|
|
|
|
.TIDR = OMAP3_TIMER_TIDR,
|
|
|
|
.TIOCP_CFG = OMAP3_TIMER_TIOCP_CFG,
|
|
|
|
.TISTAT = OMAP3_TIMER_TISTAT,
|
|
|
|
.TISR = OMAP3_TIMER_TISR,
|
|
|
|
.TIER = OMAP3_TIMER_TIER,
|
|
|
|
.TWER = OMAP3_TIMER_TWER,
|
|
|
|
.TCLR = OMAP3_TIMER_TCLR,
|
|
|
|
.TCRR = OMAP3_TIMER_TCRR,
|
|
|
|
.TLDR = OMAP3_TIMER_TLDR,
|
|
|
|
.TTGR = OMAP3_TIMER_TTGR,
|
|
|
|
.TWPS = OMAP3_TIMER_TWPS,
|
|
|
|
.TMAR = OMAP3_TIMER_TMAR,
|
|
|
|
.TCAR1 = OMAP3_TIMER_TCAR1,
|
|
|
|
.TSICR = OMAP3_TIMER_TSICR,
|
|
|
|
.TCAR2 = OMAP3_TIMER_TCAR2,
|
|
|
|
.TPIR = OMAP3_TIMER_TPIR,
|
|
|
|
.TNIR = OMAP3_TIMER_TNIR,
|
|
|
|
.TCVR = OMAP3_TIMER_TCVR,
|
|
|
|
.TOCR = OMAP3_TIMER_TOCR,
|
|
|
|
.TOWR = OMAP3_TIMER_TOWR,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* AM335X has a different ip block for the non
|
|
|
|
1ms timers */
|
|
|
|
static struct omap_timer_registers regs_v2 = {
|
|
|
|
.TIDR = AM335X_TIMER_TIDR,
|
|
|
|
.TIOCP_CFG = AM335X_TIMER_TIOCP_CFG,
|
|
|
|
.TISTAT = AM335X_TIMER_IRQSTATUS_RAW,
|
|
|
|
.TISR = AM335X_TIMER_IRQSTATUS,
|
|
|
|
.TIER = AM335X_TIMER_IRQENABLE_SET,
|
|
|
|
.TWER = AM335X_TIMER_IRQWAKEEN,
|
|
|
|
.TCLR = AM335X_TIMER_TCLR,
|
|
|
|
.TCRR = AM335X_TIMER_TCRR,
|
|
|
|
.TLDR = AM335X_TIMER_TLDR,
|
|
|
|
.TTGR = AM335X_TIMER_TTGR,
|
|
|
|
.TWPS = AM335X_TIMER_TWPS,
|
|
|
|
.TMAR = AM335X_TIMER_TMAR,
|
|
|
|
.TCAR1 = AM335X_TIMER_TCAR1,
|
|
|
|
.TSICR = AM335X_TIMER_TSICR,
|
|
|
|
.TCAR2 = AM335X_TIMER_TCAR2,
|
|
|
|
.TPIR = -1 , /* UNDEF */
|
|
|
|
.TNIR = -1 , /* UNDEF */
|
|
|
|
.TCVR = -1 , /* UNDEF */
|
|
|
|
.TOCR = -1 , /* UNDEF */
|
|
|
|
.TOWR = -1 /* UNDEF */
|
|
|
|
};
|
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
static struct omap_timer dm37xx_timer = {
|
|
|
|
.base = OMAP3_GPTIMER1_BASE,
|
|
|
|
.irq_nr = OMAP3_GPT1_IRQ,
|
|
|
|
.regs = ®s_v1
|
|
|
|
};
|
|
|
|
|
|
|
|
/* free running timer */
|
|
|
|
static struct omap_timer dm37xx_fr_timer = {
|
|
|
|
.base = OMAP3_GPTIMER10_BASE,
|
|
|
|
.irq_nr = OMAP3_GPT10_IRQ,
|
|
|
|
.regs = ®s_v1
|
|
|
|
};
|
2013-05-23 18:02:23 +02:00
|
|
|
/* normal timer */
|
2013-12-13 14:12:50 +01:00
|
|
|
static struct omap_timer am335x_timer = {
|
2013-05-23 18:02:23 +02:00
|
|
|
.base = AM335X_DMTIMER1_1MS_BASE,
|
|
|
|
.irq_nr = AM335X_INT_TINT1_1MS,
|
|
|
|
.regs = ®s_v1
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/* free running timer */
|
2013-12-13 14:12:50 +01:00
|
|
|
static struct omap_timer am335x_fr_timer = {
|
2013-05-23 18:02:23 +02:00
|
|
|
.base = AM335X_DMTIMER7_BASE,
|
|
|
|
.irq_nr = AM335X_INT_TINT7,
|
|
|
|
.regs = ®s_v2
|
|
|
|
};
|
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
static struct omap_timer *timer;
|
|
|
|
static struct omap_timer *fr_timer;
|
|
|
|
|
2013-05-23 18:02:23 +02:00
|
|
|
|
|
|
|
static int done = 0;
|
2012-10-08 03:38:03 +02:00
|
|
|
|
|
|
|
int omap3_register_timer_handler(const irq_handler_t handler)
|
|
|
|
{
|
|
|
|
/* Initialize the CLOCK's interrupt hook. */
|
|
|
|
omap3_timer_hook.proc_nr_e = NONE;
|
2013-12-13 14:12:50 +01:00
|
|
|
omap3_timer_hook.irq = timer->irq_nr;
|
2012-10-08 03:38:03 +02:00
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
put_irq_handler(&omap3_timer_hook, timer->irq_nr, handler);
|
2013-08-27 14:23:34 +02:00
|
|
|
/* only unmask interrupts after registering */
|
2013-12-13 14:12:50 +01:00
|
|
|
omap3_irq_unmask(timer->irq_nr);
|
2012-10-08 03:38:03 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-11 16:07:43 +02:00
|
|
|
|
|
|
|
/* meta data for remapping */
|
|
|
|
static kern_phys_map timer_phys_map;
|
|
|
|
static kern_phys_map fr_timer_phys_map;
|
|
|
|
|
2013-01-29 20:58:00 +01:00
|
|
|
void omap3_frclock_init(void)
|
|
|
|
{
|
|
|
|
u32_t tisr;
|
|
|
|
|
2013-05-23 18:02:23 +02:00
|
|
|
/* enable the clock */
|
2013-12-13 14:12:50 +01:00
|
|
|
if(BOARD_IS_BBXM(machine.board_id)){
|
|
|
|
fr_timer = &dm37xx_fr_timer;
|
|
|
|
kern_phys_map_ptr(fr_timer->base,ARM_PAGE_SIZE, &fr_timer_phys_map, (vir_bytes) &fr_timer->base);
|
|
|
|
|
|
|
|
/* Stop timer */
|
|
|
|
mmio_clear(fr_timer->base + fr_timer->regs->TCLR, OMAP3_TCLR_ST);
|
|
|
|
|
|
|
|
/* Use functional clock source for GPTIMER10 */
|
|
|
|
mmio_set(OMAP3_CM_CLKSEL_CORE, OMAP3_CLKSEL_GPT10);
|
|
|
|
|
|
|
|
/* Scale timer down to 13/8 = 1.625 Mhz to roughly get microsecond ticks */
|
|
|
|
/* The scale is computed as 2^(PTV+1). So if PTV == 2, we get 2^3 = 8.
|
|
|
|
*/
|
|
|
|
mmio_set(fr_timer->base + fr_timer->regs->TCLR, (2 << OMAP3_TCLR_PTV));
|
|
|
|
} else if(BOARD_IS_BB(machine.board_id)){
|
|
|
|
fr_timer = &am335x_fr_timer;
|
|
|
|
kern_phys_map_ptr(fr_timer->base,ARM_PAGE_SIZE, &fr_timer_phys_map, (vir_bytes) &fr_timer->base);
|
|
|
|
/* Disable the module and wait for the module to be disabled */
|
|
|
|
set32(CM_PER_TIMER7_CLKCTRL, CM_MODULEMODE_MASK,CM_MODULEMODE_DISABLED);
|
|
|
|
while( (mmio_read(CM_PER_TIMER7_CLKCTRL) & CM_CLKCTRL_IDLEST) != CM_CLKCTRL_IDLEST_DISABLE);
|
|
|
|
|
|
|
|
set32(CLKSEL_TIMER7_CLK,CLKSEL_TIMER7_CLK_SEL_MASK, CLKSEL_TIMER7_CLK_SEL_SEL2);
|
|
|
|
while( (read32(CLKSEL_TIMER7_CLK) & CLKSEL_TIMER7_CLK_SEL_MASK) != CLKSEL_TIMER7_CLK_SEL_SEL2);
|
|
|
|
|
|
|
|
/* enable the module and wait for the module to be ready */
|
|
|
|
set32(CM_PER_TIMER7_CLKCTRL,CM_MODULEMODE_MASK,CM_MODULEMODE_ENABLE);
|
|
|
|
while( (mmio_read(CM_PER_TIMER7_CLKCTRL) & CM_CLKCTRL_IDLEST) != CM_CLKCTRL_IDLEST_FUNC);
|
|
|
|
|
|
|
|
/* Stop timer */
|
|
|
|
mmio_clear(fr_timer->base + fr_timer->regs->TCLR, OMAP3_TCLR_ST);
|
|
|
|
|
|
|
|
/* 24Mhz / 16 = 1.5 Mhz */
|
|
|
|
mmio_set(fr_timer->base + fr_timer->regs->TCLR, (3 << OMAP3_TCLR_PTV));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 20:58:00 +01:00
|
|
|
|
|
|
|
/* Start and auto-reload at 0 */
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_write(fr_timer->base + fr_timer->regs->TLDR, 0x0);
|
|
|
|
mmio_write(fr_timer->base + fr_timer->regs->TCRR, 0x0);
|
2013-01-29 20:58:00 +01:00
|
|
|
|
|
|
|
/* Set up overflow interrupt */
|
|
|
|
tisr = OMAP3_TISR_MAT_IT_FLAG | OMAP3_TISR_OVF_IT_FLAG |
|
|
|
|
OMAP3_TISR_TCAR_IT_FLAG;
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_write(fr_timer->base + fr_timer->regs->TISR, tisr); /* Clear interrupt status */
|
|
|
|
mmio_write(fr_timer->base + fr_timer->regs->TIER, OMAP3_TIER_OVF_IT_ENA);
|
2013-01-29 20:58:00 +01:00
|
|
|
|
|
|
|
/* Start timer */
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_set(fr_timer->base + fr_timer->regs->TCLR,
|
2013-01-29 20:58:00 +01:00
|
|
|
OMAP3_TCLR_OVF_TRG|OMAP3_TCLR_AR|OMAP3_TCLR_ST|OMAP3_TCLR_PRE);
|
2013-06-19 11:39:05 +02:00
|
|
|
done = 1;
|
2013-01-29 20:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void omap3_frclock_stop()
|
|
|
|
{
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_clear(fr_timer->base + fr_timer->regs->TCLR, OMAP3_TCLR_ST);
|
2013-01-29 20:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-08 03:38:03 +02:00
|
|
|
void omap3_timer_init(unsigned freq)
|
|
|
|
{
|
2013-08-27 14:23:34 +02:00
|
|
|
/* we only support 1ms resolution */
|
2013-01-08 13:02:38 +01:00
|
|
|
u32_t tisr;
|
2013-12-13 14:12:50 +01:00
|
|
|
if(BOARD_IS_BBXM(machine.board_id)){
|
|
|
|
timer = &dm37xx_timer;
|
|
|
|
kern_phys_map_ptr(timer->base,ARM_PAGE_SIZE, &timer_phys_map, (vir_bytes) &timer->base);
|
|
|
|
/* Stop timer */
|
|
|
|
mmio_clear(timer->base + timer->regs->TCLR, OMAP3_TCLR_ST);
|
|
|
|
|
|
|
|
/* Use 32 KHz clock source for GPTIMER1 */
|
|
|
|
mmio_clear(OMAP3_CM_CLKSEL_WKUP, OMAP3_CLKSEL_GPT1);
|
|
|
|
} else if(BOARD_IS_BB(machine.board_id)){
|
|
|
|
timer = &am335x_timer;
|
|
|
|
kern_phys_map_ptr(timer->base,ARM_PAGE_SIZE, &timer_phys_map, (vir_bytes) &timer->base);
|
|
|
|
/* disable the module and wait for the module to be disabled */
|
|
|
|
set32(CM_WKUP_TIMER1_CLKCTRL, CM_MODULEMODE_MASK,CM_MODULEMODE_DISABLED);
|
|
|
|
while( (mmio_read(CM_WKUP_TIMER1_CLKCTRL) & CM_CLKCTRL_IDLEST) != CM_CLKCTRL_IDLEST_DISABLE);
|
|
|
|
|
2013-01-08 13:02:38 +01:00
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
set32(CLKSEL_TIMER1MS_CLK,CLKSEL_TIMER1MS_CLK_SEL_MASK, CLKSEL_TIMER1MS_CLK_SEL_SEL2);
|
|
|
|
while( (read32(CLKSEL_TIMER1MS_CLK) & CLKSEL_TIMER1MS_CLK_SEL_MASK) != CLKSEL_TIMER1MS_CLK_SEL_SEL2);
|
2013-05-23 18:02:23 +02:00
|
|
|
|
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
/* enable the module and wait for the module to be ready */
|
|
|
|
set32(CM_WKUP_TIMER1_CLKCTRL,CM_MODULEMODE_MASK,CM_MODULEMODE_ENABLE);
|
|
|
|
while( (mmio_read(CM_WKUP_TIMER1_CLKCTRL) & CM_CLKCTRL_IDLEST) != CM_CLKCTRL_IDLEST_FUNC);
|
2013-05-23 18:02:23 +02:00
|
|
|
|
2013-12-13 14:12:50 +01:00
|
|
|
/* Stop timer */
|
|
|
|
mmio_clear(timer->base + timer->regs->TCLR, OMAP3_TCLR_ST);
|
|
|
|
}
|
2012-10-08 03:38:03 +02:00
|
|
|
|
|
|
|
|
2013-01-08 13:02:38 +01:00
|
|
|
/* Use 1-ms tick mode for GPTIMER1 TRM 16.2.4.2.1 */
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_write(timer->base + timer->regs->TPIR, 232000);
|
|
|
|
mmio_write(timer->base + timer->regs->TNIR, -768000);
|
|
|
|
mmio_write(timer->base + timer->regs->TLDR, 0xffffffff - (32768 / freq) +1);
|
|
|
|
mmio_write(timer->base + timer->regs->TCRR, 0xffffffff - (32768 / freq) +1);
|
2013-08-27 14:23:34 +02:00
|
|
|
|
2012-10-08 03:38:03 +02:00
|
|
|
|
|
|
|
/* Set up overflow interrupt */
|
2013-01-08 13:02:38 +01:00
|
|
|
tisr = OMAP3_TISR_MAT_IT_FLAG | OMAP3_TISR_OVF_IT_FLAG |
|
|
|
|
OMAP3_TISR_TCAR_IT_FLAG;
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_write(timer->base + timer->regs->TISR, tisr); /* Clear interrupt status */
|
|
|
|
mmio_write(timer->base + timer->regs->TIER, OMAP3_TIER_OVF_IT_ENA);
|
2012-10-08 03:38:03 +02:00
|
|
|
|
|
|
|
/* Start timer */
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_set(timer->base + timer->regs->TCLR,
|
2012-10-08 03:38:03 +02:00
|
|
|
OMAP3_TCLR_OVF_TRG|OMAP3_TCLR_AR|OMAP3_TCLR_ST);
|
|
|
|
}
|
|
|
|
|
|
|
|
void omap3_timer_stop()
|
|
|
|
{
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_clear(timer->base + timer->regs->TCLR, OMAP3_TCLR_ST);
|
2012-10-08 03:38:03 +02:00
|
|
|
}
|
|
|
|
|
2013-02-03 19:28:24 +01:00
|
|
|
static u32_t read_frc(void)
|
|
|
|
{
|
2013-05-23 18:02:23 +02:00
|
|
|
if (done == 0)
|
|
|
|
return 0;
|
2013-12-13 14:12:50 +01:00
|
|
|
return mmio_read(fr_timer->base + fr_timer->regs->TCRR);
|
2013-02-03 19:28:24 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 11:29:04 +02:00
|
|
|
/*
|
|
|
|
* Check if the free running clock has overflown and
|
|
|
|
* increase the high free running clock counter if
|
|
|
|
* so. This method takes the current timer value as
|
|
|
|
* parameter to ensure the overflow check is done
|
|
|
|
* on the current timer value.
|
|
|
|
*
|
|
|
|
* To compose the current timer value (64 bits) you
|
|
|
|
* need to follow the following sequence:
|
|
|
|
* read the current timer value.
|
|
|
|
* call the overflow check
|
|
|
|
* compose the 64 bits time based on the current timer value
|
|
|
|
* and high_frc.
|
|
|
|
*/
|
|
|
|
static void frc_overflow_check(u32_t cur_frc)
|
2013-02-03 19:28:24 +01:00
|
|
|
{
|
|
|
|
static int prev_frc_valid;
|
|
|
|
static u32_t prev_frc;
|
|
|
|
if(prev_frc_valid && prev_frc > cur_frc)
|
|
|
|
high_frc++;
|
|
|
|
prev_frc = cur_frc;
|
|
|
|
prev_frc_valid = 1;
|
|
|
|
}
|
|
|
|
|
2012-10-08 03:38:03 +02:00
|
|
|
void omap3_timer_int_handler()
|
|
|
|
{
|
2013-01-08 13:02:38 +01:00
|
|
|
/* Clear all interrupts */
|
2013-09-27 11:29:04 +02:00
|
|
|
u32_t tisr,now;
|
2013-01-08 13:02:38 +01:00
|
|
|
|
2013-05-23 18:02:23 +02:00
|
|
|
|
|
|
|
/* when the kernel itself is running interrupts are disabled.
|
|
|
|
* We should therefore also read the overflow counter to detect
|
|
|
|
* this as to not miss events.
|
|
|
|
*/
|
2013-01-08 13:02:38 +01:00
|
|
|
tisr = OMAP3_TISR_MAT_IT_FLAG | OMAP3_TISR_OVF_IT_FLAG |
|
|
|
|
OMAP3_TISR_TCAR_IT_FLAG;
|
2013-12-13 14:12:50 +01:00
|
|
|
mmio_write(timer->base + timer->regs->TISR, tisr);
|
2013-01-29 20:58:00 +01:00
|
|
|
|
2013-09-27 11:29:04 +02:00
|
|
|
now = read_frc();
|
|
|
|
frc_overflow_check(now);
|
2012-10-08 03:38:03 +02:00
|
|
|
}
|
|
|
|
|
2013-02-03 19:28:24 +01:00
|
|
|
/* Use the free running clock as TSC */
|
2012-10-08 03:38:03 +02:00
|
|
|
void read_tsc_64(u64_t *t)
|
|
|
|
{
|
2013-02-03 19:28:24 +01:00
|
|
|
u32_t now;
|
|
|
|
now = read_frc();
|
2013-09-27 11:29:04 +02:00
|
|
|
frc_overflow_check(now);
|
2013-02-03 19:28:24 +01:00
|
|
|
*t = (u64_t) now + (high_frc << 32);
|
2012-10-08 03:38:03 +02:00
|
|
|
}
|