blob: 9175b71d16aa17e5c75e442defb5ea4ae1625a6b [file] [log] [blame]
Matthias Weisser6052ac82010-08-09 13:31:49 +02001/*
2 * (C) Copyright 2007-2008
3 * Stelian Pop <stelian.pop@leadtechdesign.com>
4 * Lead Tech Design <www.leadtechdesign.com>
5 *
6 * (C) Copyright 2010
7 * Matthias Weisser, Graf-Syteco <weisserm@arcor.de>
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <div64.h>
29#include <common.h>
30#include <asm/io.h>
31#include <asm/arch/hardware.h>
32
33#define TIMER_LOAD_VAL 0xffffffff
34#define TIMER_FREQ (CONFIG_MB86R0x_IOCLK / 256)
35
36static unsigned long long timestamp;
37static ulong lastdec;
38
39static inline unsigned long long tick_to_time(unsigned long long tick)
40{
41 tick *= CONFIG_SYS_HZ;
42 do_div(tick, TIMER_FREQ);
43
44 return tick;
45}
46
47static inline unsigned long long usec_to_tick(unsigned long long usec)
48{
49 usec *= TIMER_FREQ;
50 do_div(usec, 1000000);
51
52 return usec;
53}
54
55/* nothing really to do with interrupts, just starts up a counter. */
56int timer_init(void)
57{
58 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
59 MB86R0x_TIMER_BASE;
60 ulong ctrl = readl(&timer->control);
61
62 writel(TIMER_LOAD_VAL, &timer->load);
63
64 ctrl |= MB86R0x_TIMER_ENABLE | MB86R0x_TIMER_PRS_8S |
65 MB86R0x_TIMER_SIZE_32;
66
67 writel(ctrl, &timer->control);
68
69 reset_timer_masked();
70
71 return 0;
72}
73
74/*
75 * timer without interrupts
76 */
77unsigned long long get_ticks(void)
78{
79 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
80 MB86R0x_TIMER_BASE;
81 ulong now = readl(&timer->value);
82
83 if (now <= lastdec) {
84 /* normal mode (non roll) */
85 /* move stamp forward with absolut diff ticks */
86 timestamp += lastdec - now;
87 } else {
88 /* we have rollover of incrementer */
89 timestamp += lastdec + TIMER_LOAD_VAL - now;
90 }
91 lastdec = now;
92 return timestamp;
93}
94
95void reset_timer_masked(void)
96{
97 struct mb86r0x_timer * timer = (struct mb86r0x_timer *)
98 MB86R0x_TIMER_BASE;
99
100 /* capture current value time */
101 lastdec = readl(&timer->value);
102 timestamp = 0; /* start "advancing" time stamp from 0 */
103}
104
105ulong get_timer_masked(void)
106{
107 return tick_to_time(get_ticks());
108}
109
110void __udelay(unsigned long usec)
111{
112 unsigned long long tmp;
113 ulong tmo;
114
115 tmo = usec_to_tick(usec);
116 tmp = get_ticks(); /* get current timestamp */
117
118 while ((get_ticks() - tmp) < tmo) /* loop till event */
119 /*NOP*/;
120}
121
122void reset_timer(void)
123{
124 reset_timer_masked();
125}
126
127ulong get_timer(ulong base)
128{
129 return get_timer_masked() - base;
130}
131
132/*
133 * This function is derived from PowerPC code (timebase clock frequency).
134 * On ARM it returns the number of timer ticks per second.
135 */
136ulong get_tbclk(void)
137{
138 ulong tbclk;
139
140 tbclk = TIMER_FREQ;
141 return tbclk;
142}