add chaos_calmer branch
[15.05/openwrt.git] / package / boot / uboot-oxnas / files / arch / arm / cpu / arm1136 / nas782x / timer.c
1 /*
2  * (C) Copyright 2004
3  * Texas Instruments
4  * Richard Woodruff <r-woodruff2@ti.com>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Marius Groeger <mgroeger@sysgo.de>
9  * Alex Zuepke <azu@sysgo.de>
10  *
11  * (C) Copyright 2002
12  * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  */
32
33 #include <common.h>
34 #include <asm/io.h>
35
36 #define TIMER_CLOCK     (CONFIG_SYS_CLK_FREQ / (1 << (CONFIG_TIMER_PRESCALE * 4)))
37 #define TIMER_LOAD_VAL 0xFFFFFF
38
39 /* macro to read the 32 bit timer */
40 #define READ_TIMER      (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR)) \
41                         / (TIMER_CLOCK / CONFIG_SYS_HZ)
42
43 #define READ_TIMER_HW   (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR))
44
45 DECLARE_GLOBAL_DATA_PTR;
46
47 int timer_init (void)
48 {
49         int32_t val;
50
51         /* Start the counter ticking up */
52         writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + TIMER_LOAD);      /* reload value on overflow*/
53
54         val = (CONFIG_TIMER_PRESCALE << TIMER_PRESCALE_SHIFT) |
55                         (TIMER_MODE_PERIODIC << TIMER_MODE_SHIFT) |
56                         (TIMER_ENABLE << TIMER_ENABLE_SHIFT);           /* mask to enable timer*/
57         writel(val, CONFIG_SYS_TIMERBASE + TIMER_CTRL); /* start timer */
58
59         /* reset time */
60         gd->arch.lastinc = READ_TIMER;  /* capture current incrementer value */
61         gd->arch.tbl = 0;               /* start "advancing" time stamp */
62
63         return(0);
64 }
65 /*
66  * timer without interrupts
67  */
68 ulong get_timer (ulong base)
69 {
70         return get_timer_masked () - base;
71 }
72
73 /* delay x useconds AND preserve advance timestamp value */
74 void __udelay (unsigned long usec)
75 {
76         ulong tmo, tmp;
77
78         if (usec > 100000) {            /* if "big" number, spread normalization to seconds */
79                 tmo = usec / 1000;      /* start to normalize for usec to ticks per sec */
80                 tmo *= CONFIG_SYS_HZ;   /* find number of "ticks" to wait to achieve target */
81                 tmo /= 1000;            /* finish normalize. */
82
83                 tmp = get_timer (0);            /* get current timestamp */
84                 while (get_timer (tmp) < tmo)/* loop till event */
85                         /*NOP*/;
86         } else {                        /* else small number, convert to hw ticks */
87                 tmo = usec * (TIMER_CLOCK / 1000) / 1000;
88                 /* timeout is no more than 0.1s, and the hw timer will roll over at most once */
89                 tmp = READ_TIMER_HW;
90                 while (((READ_TIMER_HW -tmp) & TIMER_LOAD_VAL) < tmo)/* loop till event */
91                         /*NOP*/;
92         }
93 }
94
95 ulong get_timer_masked (void)
96 {
97         ulong now = READ_TIMER;         /* current tick value */
98
99         if (now >= gd->arch.lastinc) {          /* normal mode (non roll) */
100                 /* move stamp fordward with absoulte diff ticks */
101                 gd->arch.tbl += (now - gd->arch.lastinc);
102         } else {
103                 /* we have rollover of incrementer */
104                 gd->arch.tbl += ((TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ))
105                                  - gd->arch.lastinc) + now;
106         }
107         gd->arch.lastinc = now;
108         return gd->arch.tbl;
109 }
110
111
112 /*
113  * This function is derived from PowerPC code (read timebase as long long).
114  * On ARM it just returns the timer value.
115  */
116 unsigned long long get_ticks(void)
117 {
118         return get_timer(0);
119 }
120 /*
121  * This function is derived from PowerPC code (timebase clock frequency).
122  * On ARM it returns the number of timer ticks per second.
123  */
124 ulong get_tbclk (void)
125 {
126         ulong tbclk;
127         tbclk = CONFIG_SYS_HZ;
128         return tbclk;
129 }