]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/armv7/highbank/timer.c
ARM: highbank: set timer prescaler to 256
[u-boot] / arch / arm / cpu / armv7 / highbank / timer.c
1 /*
2  * Copyright 2010-2011 Calxeda, Inc.
3  *
4  * Based on arm926ejs/mx27/timer.c
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <common.h>
21 #include <div64.h>
22 #include <linux/types.h>        /* for size_t */
23 #include <linux/stddef.h>       /* for NULL */
24 #include <asm/io.h>
25 #include <asm/arch-armv7/systimer.h>
26
27 #undef SYSTIMER_BASE
28 #define SYSTIMER_BASE           0xFFF34000      /* Timer 0 and 1 base   */
29 #define SYSTIMER_RATE           (150000000 / 256)
30
31 static ulong timestamp;
32 static ulong lastinc;
33 static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
34
35 /*
36  * Start the timer
37  */
38 int timer_init(void)
39 {
40         /*
41          * Setup timer0
42          */
43         writel(0, &systimer_base->timer0control);
44         writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
45         writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
46         writel(SYSTIMER_EN | SYSTIMER_32BIT | SYSTIMER_PRESC_256,
47                 &systimer_base->timer0control);
48
49         reset_timer_masked();
50
51         return 0;
52
53 }
54
55 #define TICK_PER_TIME   ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
56 #define NS_PER_TICK     (1000000000 / SYSTIMER_RATE)
57
58 static inline unsigned long long tick_to_time(unsigned long long tick)
59 {
60         do_div(tick, TICK_PER_TIME);
61         return tick;
62 }
63
64 static inline unsigned long long time_to_tick(unsigned long long time)
65 {
66         return time * TICK_PER_TIME;
67 }
68
69 static inline unsigned long long us_to_tick(unsigned long long us)
70 {
71         unsigned long long tick = us * 1000;
72         tick += NS_PER_TICK - 1;
73         do_div(tick, NS_PER_TICK);
74         return tick;
75 }
76
77 unsigned long long get_ticks(void)
78 {
79         ulong now = ~readl(&systimer_base->timer0value);
80
81         if (now >= lastinc)     /* normal mode (non roll) */
82                 /* move stamp forward with absolut diff ticks */
83                 timestamp += (now - lastinc);
84         else                    /* we have rollover of incrementer */
85                 timestamp += (0xFFFFFFFF - lastinc) + now;
86         lastinc = now;
87         return timestamp;
88 }
89
90 /*
91  * Delay x useconds AND preserve advance timstamp value
92  *     assumes timer is ticking at 1 msec
93  */
94 void __udelay(ulong usec)
95 {
96         unsigned long long tmp;
97         ulong tmo;
98
99         tmo = us_to_tick(usec);
100         tmp = get_ticks() + tmo;        /* get current timestamp */
101
102         while (get_ticks() < tmp)       /* loop till event */
103                  /*NOP*/;
104 }
105
106 ulong get_timer(ulong base)
107 {
108         return get_timer_masked() - base;
109 }
110
111 void reset_timer_masked(void)
112 {
113         lastinc = ~readl(&systimer_base->timer0value);
114         timestamp = 0;
115 }
116
117 void reset_timer(void)
118 {
119         reset_timer_masked();
120 }
121
122 ulong get_timer_masked(void)
123 {
124         return tick_to_time(get_ticks());
125 }
126
127 ulong get_tbclk(void)
128 {
129         return SYSTIMER_RATE;
130 }