]>
Commit | Line | Data |
---|---|---|
c74b2108 SK |
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, <gj@denx.de> | |
15 | * | |
16 | * (C) Copyright 2004 | |
17 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> | |
18 | * | |
19 | * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> | |
20 | * | |
21 | * See file CREDITS for list of people who contributed to this | |
22 | * project. | |
23 | * | |
24 | * This program is free software; you can redistribute it and/or | |
25 | * modify it under the terms of the GNU General Public License as | |
26 | * published by the Free Software Foundation; either version 2 of | |
27 | * the License, or (at your option) any later version. | |
28 | * | |
29 | * This program is distributed in the hope that it will be useful, | |
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
32 | * GNU General Public License for more details. | |
33 | * | |
34 | * You should have received a copy of the GNU General Public License | |
35 | * along with this program; if not, write to the Free Software | |
36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
37 | * MA 02111-1307 USA | |
38 | */ | |
39 | ||
40 | #include <common.h> | |
41 | #include <arm926ejs.h> | |
42 | ||
43 | typedef volatile struct { | |
44 | u_int32_t pid12; | |
80c40b76 DB |
45 | u_int32_t emumgt; |
46 | u_int32_t na1; | |
47 | u_int32_t na2; | |
c74b2108 SK |
48 | u_int32_t tim12; |
49 | u_int32_t tim34; | |
50 | u_int32_t prd12; | |
51 | u_int32_t prd34; | |
52 | u_int32_t tcr; | |
53 | u_int32_t tgcr; | |
54 | u_int32_t wdtcr; | |
c74b2108 SK |
55 | } davinci_timer; |
56 | ||
6d0f6bcf | 57 | davinci_timer *timer = (davinci_timer *)CONFIG_SYS_TIMERBASE; |
c74b2108 | 58 | |
6d0f6bcf | 59 | #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ) |
80c40b76 | 60 | #define TIM_CLK_DIV 16 |
ea686f52 | 61 | |
c74b2108 SK |
62 | static ulong timestamp; |
63 | static ulong lastinc; | |
64 | ||
65 | int timer_init(void) | |
66 | { | |
67 | /* We are using timer34 in unchained 32-bit mode, full speed */ | |
68 | timer->tcr = 0x0; | |
69 | timer->tgcr = 0x0; | |
80c40b76 | 70 | timer->tgcr = 0x06 | ((TIM_CLK_DIV - 1) << 8); |
c74b2108 SK |
71 | timer->tim34 = 0x0; |
72 | timer->prd34 = TIMER_LOAD_VAL; | |
73 | lastinc = 0; | |
c74b2108 | 74 | timestamp = 0; |
80c40b76 | 75 | timer->tcr = 2 << 22; |
c74b2108 SK |
76 | |
77 | return(0); | |
78 | } | |
79 | ||
80 | void reset_timer(void) | |
81 | { | |
80c40b76 DB |
82 | timer->tcr = 0x0; |
83 | timer->tim34 = 0; | |
84 | lastinc = 0; | |
c74b2108 | 85 | timestamp = 0; |
80c40b76 | 86 | timer->tcr = 2 << 22; |
c74b2108 SK |
87 | } |
88 | ||
80c40b76 | 89 | static ulong get_timer_raw(void) |
c74b2108 | 90 | { |
80c40b76 | 91 | ulong now = timer->tim34; |
c74b2108 SK |
92 | |
93 | if (now >= lastinc) { | |
94 | /* normal mode */ | |
95 | timestamp += now - lastinc; | |
96 | } else { | |
97 | /* overflow ... */ | |
80c40b76 | 98 | timestamp += now + TIMER_LOAD_VAL - lastinc; |
c74b2108 SK |
99 | } |
100 | lastinc = now; | |
101 | return timestamp; | |
102 | } | |
103 | ||
80c40b76 DB |
104 | ulong get_timer(ulong base) |
105 | { | |
106 | return((get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base); | |
107 | } | |
108 | ||
109 | void set_timer(ulong t) | |
c74b2108 | 110 | { |
80c40b76 | 111 | timestamp = t; |
c74b2108 SK |
112 | } |
113 | ||
80c40b76 | 114 | void udelay(unsigned long usec) |
c74b2108 SK |
115 | { |
116 | ulong tmo; | |
117 | ulong endtime; | |
118 | signed long diff; | |
119 | ||
6d0f6bcf | 120 | tmo = CONFIG_SYS_HZ_CLOCK / 1000; |
c74b2108 | 121 | tmo *= usec; |
80c40b76 | 122 | tmo /= (1000 * TIM_CLK_DIV); |
c74b2108 SK |
123 | |
124 | endtime = get_timer_raw() + tmo; | |
125 | ||
126 | do { | |
127 | ulong now = get_timer_raw(); | |
128 | diff = endtime - now; | |
129 | } while (diff >= 0); | |
130 | } | |
131 | ||
132 | /* | |
133 | * This function is derived from PowerPC code (read timebase as long long). | |
134 | * On ARM it just returns the timer value. | |
135 | */ | |
136 | unsigned long long get_ticks(void) | |
137 | { | |
138 | return(get_timer(0)); | |
139 | } | |
140 | ||
141 | /* | |
142 | * This function is derived from PowerPC code (timebase clock frequency). | |
143 | * On ARM it returns the number of timer ticks per second. | |
144 | */ | |
145 | ulong get_tbclk(void) | |
146 | { | |
6d0f6bcf | 147 | return CONFIG_SYS_HZ; |
c74b2108 | 148 | } |