This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / arm / mach-omap / time.c
index f673eee..cb3c5d6 100644 (file)
@@ -1,14 +1,8 @@
 /*
- * linux/arch/arm/mach-omap/time.c
+ * arch/arm/mach-omap/time.c
  *
- * OMAP Timers
+ * OMAP Timer Tick 
  *
- * Copyright (C) 2004 Nokia Corporation
- * Partial timer rewrite and additional VST timer support by
- * Tony Lindgen <tony@atomide.com> and
- * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
- *
- * MPU timer code based on the older MPU timer code for OMAP
  * Copyright (C) 2000 RidgeRun, Inc.
  * Author: Greg Lonnon <glonnon@ridgerun.com>
  *
@@ -39,7 +33,6 @@
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/sched.h>
-#include <linux/spinlock.h>
 
 #include <asm/system.h>
 #include <asm/hardware.h>
 #include <asm/irq.h>
 #include <asm/mach/irq.h>
 #include <asm/mach/time.h>
+#include <asm/arch/clocks.h>
+
+#ifndef __instrument
+#define __instrument
+#define __noinstrument __attribute__ ((no_instrument_function))
+#endif
 
-struct sys_timer omap_timer;
+typedef struct {
+       u32 cntl;     /* CNTL_TIMER, R/W */
+       u32 load_tim; /* LOAD_TIM,   W */
+       u32 read_tim; /* READ_TIM,   R */
+} mputimer_regs_t;
+
+#define mputimer_base(n) \
+    ((volatile mputimer_regs_t*)IO_ADDRESS(OMAP_MPUTIMER_BASE + \
+                                (n)*OMAP_MPUTIMER_OFFSET))
+
+static inline unsigned long timer32k_read(int reg) {
+       unsigned long val;
+       val = omap_readw(reg + OMAP_32kHz_TIMER_BASE);
+       return val;
+}
+static inline void timer32k_write(int reg,int val) {
+       omap_writew(val, reg + OMAP_32kHz_TIMER_BASE);
+}
 
 /*
- * ---------------------------------------------------------------------------
- * MPU timer
- * ---------------------------------------------------------------------------
+ * How long is the timer interval? 100 HZ, right...
+ * IRQ rate = (TVR + 1) / 32768 seconds
+ * TVR = 32768 * IRQ_RATE -1
+ * IRQ_RATE =  1/100
+ * TVR = 326
  */
-#define OMAP_MPU_TIMER1_BASE           (0xfffec500)
-#define OMAP_MPU_TIMER2_BASE           (0xfffec600)
-#define OMAP_MPU_TIMER3_BASE           (0xfffec700)
-#define OMAP_MPU_TIMER_BASE            OMAP_MPU_TIMER1_BASE
-#define OMAP_MPU_TIMER_OFFSET          0x100
+#define TIMER32k_PERIOD 326
+//#define TIMER32k_PERIOD 0x7ff
 
-#define MPU_TIMER_FREE                 (1 << 6)
-#define MPU_TIMER_CLOCK_ENABLE         (1 << 5)
-#define MPU_TIMER_AR                   (1 << 1)
-#define MPU_TIMER_ST                   (1 << 0)
+static inline void start_timer32k(void) {
+       timer32k_write(TIMER32k_CR,
+                      TIMER32k_TSS | TIMER32k_TRB |
+                      TIMER32k_INT | TIMER32k_ARL);
+}
 
-/*
- * MPU_TICKS_PER_SEC must be an even number, otherwise machinecycles_to_usecs
- * will break. On P2, the timer count rate is 6.5 MHz after programming PTV
- * with 0. This divides the 13MHz input by 2, and is undocumented.
- */
 #ifdef CONFIG_MACH_OMAP_PERSEUS2
-/* REVISIT: This ifdef construct should be replaced by a query to clock
- * framework to see if timer base frequency is 12.0, 13.0 or 19.2 MHz.
+/*
+ * After programming PTV with 0 and setting the MPUTIM_CLOCK_ENABLE
+ * (external clock enable)  bit, the timer count rate is 6.5 MHz (13
+ * MHZ input/2). !! The divider by 2 is undocumented !!
  */
-#define MPU_TICKS_PER_SEC              (13000000 / 2)
+#define MPUTICKS_PER_SEC (13000000/2)
 #else
-#define MPU_TICKS_PER_SEC              (12000000 / 2)
+/*
+ * After programming PTV with 0, the timer count rate is 6 MHz.
+ * WARNING! this must be an even number, or machinecycles_to_usecs
+ * below will break.
+ */
+#define MPUTICKS_PER_SEC  (12000000/2)
 #endif
 
-#define MPU_TIMER_TICK_PERIOD          ((MPU_TICKS_PER_SEC / HZ) - 1)
+static int mputimer_started[3] = {0,0,0};
 
-typedef struct {
-       u32 cntl;                       /* CNTL_TIMER, R/W */
-       u32 load_tim;                   /* LOAD_TIM,   W */
-       u32 read_tim;                   /* READ_TIM,   R */
-} omap_mpu_timer_regs_t;
+static inline void __noinstrument start_mputimer(int n,
+                                                unsigned long load_val)
+{
+       volatile mputimer_regs_t* timer = mputimer_base(n);
+
+       mputimer_started[n] = 0;
+       timer->cntl = MPUTIM_CLOCK_ENABLE;
+       udelay(1);
+
+       timer->load_tim = load_val;
+        udelay(1);
+       timer->cntl = (MPUTIM_CLOCK_ENABLE | MPUTIM_AR | MPUTIM_ST);
+       mputimer_started[n] = 1;
+}
 
-#define omap_mpu_timer_base(n)                                         \
-((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE +     \
-                                (n)*OMAP_MPU_TIMER_OFFSET))
+static inline unsigned long __noinstrument
+read_mputimer(int n)
+{
+       volatile mputimer_regs_t* timer = mputimer_base(n);
+       return (mputimer_started[n] ? timer->read_tim : 0);
+}
 
-static inline unsigned long omap_mpu_timer_read(int nr)
+void __noinstrument start_mputimer1(unsigned long load_val)
+{
+       start_mputimer(0, load_val);
+}
+void __noinstrument start_mputimer2(unsigned long load_val)
 {
-       volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
-       return timer->read_tim;
+       start_mputimer(1, load_val);
+}
+void __noinstrument start_mputimer3(unsigned long load_val)
+{
+       start_mputimer(2, load_val);
 }
 
-static inline void omap_mpu_timer_start(int nr, unsigned long load_val)
+unsigned long __noinstrument read_mputimer1(void)
+{
+       return read_mputimer(0);
+}
+unsigned long __noinstrument read_mputimer2(void)
+{
+       return read_mputimer(1);
+}
+unsigned long __noinstrument read_mputimer3(void)
 {
-       volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
+       return read_mputimer(2);
+}
 
-       timer->cntl = MPU_TIMER_CLOCK_ENABLE;
-       udelay(1);
-       timer->load_tim = load_val;
-        udelay(1);
-       timer->cntl = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_AR | MPU_TIMER_ST);
+unsigned long __noinstrument do_getmachinecycles(void)
+{
+       return 0 - read_mputimer(0);
 }
 
-unsigned long omap_mpu_timer_ticks_to_usecs(unsigned long nr_ticks)
+unsigned long __noinstrument machinecycles_to_usecs(unsigned long mputicks)
 {
        /* Round up to nearest usec */
-       return ((nr_ticks * 1000) / (MPU_TICKS_PER_SEC / 2 / 1000) + 1) >> 1;
+       return ((mputicks * 1000) / (MPUTICKS_PER_SEC / 2 / 1000) + 1) >> 1;
 }
 
 /*
- * Last processed system timer interrupt
+ * This marks the time of the last system timer interrupt
+ * that was *processed by the ISR* (timer 2).
  */
-static unsigned long omap_mpu_timer_last = 0;
+static unsigned long systimer_mark;
 
-/*
- * Returns elapsed usecs since last system timer interrupt
- */
-static unsigned long omap_mpu_timer_gettimeoffset(void)
+static unsigned long omap_gettimeoffset(void)
 {
-       unsigned long now = 0 - omap_mpu_timer_read(0);
-       unsigned long elapsed = now - omap_mpu_timer_last;
-
-       return omap_mpu_timer_ticks_to_usecs(elapsed);
+       /* Return elapsed usecs since last system timer ISR */
+       return machinecycles_to_usecs(do_getmachinecycles() - systimer_mark);
 }
 
-/*
- * Elapsed time between interrupts is calculated using timer0.
- * Latency during the interrupt is calculated using timer1.
- * Both timer0 and timer1 are counting at 6MHz (P2 6.5MHz).
- */
-static irqreturn_t omap_mpu_timer_interrupt(int irq, void *dev_id,
-                                       struct pt_regs *regs)
+static irqreturn_t
+omap_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 {
-       unsigned long now, latency;
+       unsigned long now, ilatency;
+
+       /*
+        * Mark the time at which the timer interrupt ocurred using
+        * timer1. We need to remove interrupt latency, which we can
+        * retrieve from the current system timer2 counter. Both the
+        * offset timer1 and the system timer2 are counting at 6MHz,
+        * so we're ok.
+        */
+       now = 0 - read_mputimer1();
+       ilatency = MPUTICKS_PER_SEC / 100 - read_mputimer2();
+       systimer_mark = now - ilatency;
 
-       write_seqlock(&xtime_lock);
-       now = 0 - omap_mpu_timer_read(0);
-       latency = MPU_TICKS_PER_SEC / HZ - omap_mpu_timer_read(1);
-       omap_mpu_timer_last = now - latency;
        timer_tick(regs);
-       write_sequnlock(&xtime_lock);
 
        return IRQ_HANDLED;
 }
 
-static struct irqaction omap_mpu_timer_irq = {
-       .name           = "mpu timer",
+static struct irqaction omap_timer_irq = {
+       .name           = "OMAP Timer Tick",
        .flags          = SA_INTERRUPT,
-       .handler        = omap_mpu_timer_interrupt
+       .handler        = omap_timer_interrupt
 };
 
-static __init void omap_init_mpu_timer(void)
-{
-       omap_timer.offset = omap_mpu_timer_gettimeoffset;
-       setup_irq(INT_TIMER2, &omap_mpu_timer_irq);
-       omap_mpu_timer_start(0, 0xffffffff);
-       omap_mpu_timer_start(1, MPU_TIMER_TICK_PERIOD);
-}
-
-/*
- * ---------------------------------------------------------------------------
- * Timer initialization
- * ---------------------------------------------------------------------------
- */
-void __init omap_timer_init(void)
+void __init omap_init_time(void)
 {
-       omap_init_mpu_timer();
+       /* Since we don't call request_irq, we must init the structure */
+       gettimeoffset = omap_gettimeoffset;
+
+#ifdef OMAP1510_USE_32KHZ_TIMER
+       timer32k_write(TIMER32k_CR, 0x0);
+       timer32k_write(TIMER32k_TVR,TIMER32k_PERIOD);
+       setup_irq(INT_OS_32kHz_TIMER, &omap_timer_irq);
+       start_timer32k();
+#else
+       setup_irq(INT_TIMER2, &omap_timer_irq);
+       start_mputimer2(MPUTICKS_PER_SEC / 100 - 1);
+#endif
 }
 
-struct sys_timer omap_timer = {
-       .init           = omap_timer_init,
-       .offset         = NULL,         /* Initialized later */
-};