uboot/arch/arm/cpu/arm926ejs/omap/timer.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2003
   3 * Texas Instruments <www.ti.com>
   4 *
   5 * (C) Copyright 2002
   6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   7 * Marius Groeger <mgroeger@sysgo.de>
   8 *
   9 * (C) Copyright 2002
  10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11 * Alex Zuepke <azu@sysgo.de>
  12 *
  13 * (C) Copyright 2002-2004
  14 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  15 *
  16 * (C) Copyright 2004
  17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  18 *
  19 * SPDX-License-Identifier:     GPL-2.0+
  20 */
  21
  22#include <common.h>
  23#include <asm/io.h>
  24
  25#define TIMER_CLOCK     (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
  26#define TIMER_LOAD_VAL  0xffffffff
  27
  28/* macro to read the 32 bit timer */
  29#define READ_TIMER      readl(CONFIG_SYS_TIMERBASE+8) \
  30                        / (TIMER_CLOCK / CONFIG_SYS_HZ)
  31
  32DECLARE_GLOBAL_DATA_PTR;
  33
  34#define timestamp gd->arch.tbl
  35#define lastdec gd->arch.lastinc
  36
  37int timer_init (void)
  38{
  39        int32_t val;
  40
  41        /* Start the decrementer ticking down from 0xffffffff */
  42        *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL;
  43        val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT);
  44        *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val;
  45
  46        /* init the timestamp and lastdec value */
  47        reset_timer_masked();
  48
  49        return 0;
  50}
  51
  52/*
  53 * timer without interrupts
  54 */
  55ulong get_timer (ulong base)
  56{
  57        return get_timer_masked () - base;
  58}
  59
  60/* delay x useconds AND preserve advance timestamp value */
  61void __udelay (unsigned long usec)
  62{
  63        ulong tmo, tmp;
  64
  65        if(usec >= 1000){               /* if "big" number, spread normalization to seconds */
  66                tmo = usec / 1000;      /* start to normalize for usec to ticks per sec */
  67                tmo *= CONFIG_SYS_HZ;   /* find number of "ticks" to wait to achieve target */
  68                tmo /= 1000;            /* finish normalize. */
  69        }else{                          /* else small number, don't kill it prior to HZ multiply */
  70                tmo = usec * CONFIG_SYS_HZ;
  71                tmo /= (1000*1000);
  72        }
  73
  74        tmp = get_timer (0);            /* get current timestamp */
  75        if( (tmo + tmp + 1) < tmp )     /* if setting this fordward will roll time stamp */
  76                reset_timer_masked ();  /* reset "advancing" timestamp to 0, set lastdec value */
  77        else
  78                tmo += tmp;             /* else, set advancing stamp wake up time */
  79
  80        while (get_timer_masked () < tmo)/* loop till event */
  81                /*NOP*/;
  82}
  83
  84void reset_timer_masked (void)
  85{
  86        /* reset time */
  87        lastdec = READ_TIMER;  /* capure current decrementer value time */
  88        timestamp = 0;         /* start "advancing" time stamp from 0 */
  89}
  90
  91ulong get_timer_masked (void)
  92{
  93        ulong now = READ_TIMER;         /* current tick value */
  94
  95        if (lastdec >= now) {           /* normal mode (non roll) */
  96                /* normal mode */
  97                timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
  98        } else {                        /* we have overflow of the count down timer */
  99                /* nts = ts + ld + (TLV - now)
 100                 * ts=old stamp, ld=time that passed before passing through -1
 101                 * (TLV-now) amount of time after passing though -1
 102                 * nts = new "advancing time stamp"...it could also roll and cause problems.
 103                 */
 104                timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK /
 105                                        CONFIG_SYS_HZ)) - now;
 106        }
 107        lastdec = now;
 108
 109        return timestamp;
 110}
 111
 112/* waits specified delay value and resets timestamp */
 113void udelay_masked (unsigned long usec)
 114{
 115        ulong tmo;
 116        ulong endtime;
 117        signed long diff;
 118
 119        if (usec >= 1000) {             /* if "big" number, spread normalization to seconds */
 120                tmo = usec / 1000;      /* start to normalize for usec to ticks per sec */
 121                tmo *= CONFIG_SYS_HZ;           /* find number of "ticks" to wait to achieve target */
 122                tmo /= 1000;            /* finish normalize. */
 123        } else {                        /* else small number, don't kill it prior to HZ multiply */
 124                tmo = usec * CONFIG_SYS_HZ;
 125                tmo /= (1000*1000);
 126        }
 127
 128        endtime = get_timer_masked () + tmo;
 129
 130        do {
 131                ulong now = get_timer_masked ();
 132                diff = endtime - now;
 133        } while (diff >= 0);
 134}
 135
 136/*
 137 * This function is derived from PowerPC code (read timebase as long long).
 138 * On ARM it just returns the timer value.
 139 */
 140unsigned long long get_ticks(void)
 141{
 142        return get_timer(0);
 143}
 144
 145/*
 146 * This function is derived from PowerPC code (timebase clock frequency).
 147 * On ARM it returns the number of timer ticks per second.
 148 */
 149ulong get_tbclk (void)
 150{
 151        return CONFIG_SYS_HZ;
 152}
 153