Merge branch 'marex@denx.de' of git://git.denx.de/u-boot-staging
[oweals/u-boot.git] / arch / arm / cpu / armv7 / omap-common / timer.c
index 6b8cf7ba2dc634d54296c82cc062522d8c5f6688..9f8bc934470bf2129459e1d6934997f98ca93218 100644 (file)
 #include <common.h>
 #include <asm/io.h>
 
-static ulong timestamp;
-static ulong lastinc;
+DECLARE_GLOBAL_DATA_PTR;
+
 static struct gptimer *timer_base = (struct gptimer *)CONFIG_SYS_TIMERBASE;
 
 /*
  * Nothing really to do with interrupts, just starts up a counter.
  */
 
-#define TIMER_CLOCK    (V_SCLK / (2 << CONFIG_SYS_PTV))
-#define TIMER_LOAD_VAL 0xffffffff
+#define TIMER_CLOCK            (V_SCLK / (2 << CONFIG_SYS_PTV))
+#define TIMER_OVERFLOW_VAL     0xffffffff
+#define TIMER_LOAD_VAL         0
 
 int timer_init(void)
 {
@@ -54,7 +55,9 @@ int timer_init(void)
        writel((CONFIG_SYS_PTV << 2) | TCLR_PRE | TCLR_AR | TCLR_ST,
                &timer_base->tclr);
 
-       reset_timer_masked();   /* init the timestamp and lastinc value */
+       /* reset time, capture current incrementer value time */
+       gd->lastinc = readl(&timer_base->tcrr) / (TIMER_CLOCK / CONFIG_SYS_HZ);
+       gd->tbl = 0;            /* start "advancing" time stamp from 0 */
 
        return 0;
 }
@@ -62,21 +65,11 @@ int timer_init(void)
 /*
  * timer without interrupts
  */
-void reset_timer(void)
-{
-       reset_timer_masked();
-}
-
 ulong get_timer(ulong base)
 {
        return get_timer_masked() - base;
 }
 
-void set_timer(ulong t)
-{
-       timestamp = t;
-}
-
 /* delay x useconds */
 void __udelay(unsigned long usec)
 {
@@ -86,33 +79,26 @@ void __udelay(unsigned long usec)
        while (tmo > 0) {
                now = readl(&timer_base->tcrr);
                if (last > now) /* count up timer overflow */
-                       tmo -= TIMER_LOAD_VAL - last + now;
+                       tmo -= TIMER_OVERFLOW_VAL - last + now + 1;
                else
                        tmo -= now - last;
                last = now;
        }
 }
 
-void reset_timer_masked(void)
-{
-       /* reset time, capture current incrementer value time */
-       lastinc = readl(&timer_base->tcrr) / (TIMER_CLOCK / CONFIG_SYS_HZ);
-       timestamp = 0;          /* start "advancing" time stamp from 0 */
-}
-
 ulong get_timer_masked(void)
 {
        /* current tick value */
        ulong now = readl(&timer_base->tcrr) / (TIMER_CLOCK / CONFIG_SYS_HZ);
 
-       if (now >= lastinc)     /* normal mode (non roll) */
+       if (now >= gd->lastinc) /* normal mode (non roll) */
                /* move stamp fordward with absoulte diff ticks */
-               timestamp += (now - lastinc);
+               gd->tbl += (now - gd->lastinc);
        else    /* we have rollover of incrementer */
-               timestamp += ((TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ))
-                               - lastinc) + now;
-       lastinc = now;
-       return timestamp;
+               gd->tbl += ((TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ))
+                            - gd->lastinc) + now;
+       gd->lastinc = now;
+       return gd->tbl;
 }
 
 /*