]> git.ipfire.org Git - people/ms/u-boot.git/blob - arch/arm/cpu/arm920t/a320/timer.c
Timer: Remove set_timer completely
[people/ms/u-boot.git] / arch / arm / cpu / arm920t / a320 / timer.c
1 /*
2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <common.h>
21 #include <asm/io.h>
22 #include <faraday/ftpmu010.h>
23 #include <faraday/fttmr010.h>
24
25 static ulong timestamp;
26 static ulong lastdec;
27
28 static struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
29
30 #define TIMER_CLOCK 32768
31 #define TIMER_LOAD_VAL 0xffffffff
32
33 int timer_init(void)
34 {
35 unsigned int cr;
36
37 debug("%s()\n", __func__);
38
39 /* disable timers */
40 writel(0, &tmr->cr);
41
42 /* use 32768Hz oscillator for RTC, WDT, TIMER */
43 ftpmu010_32768osc_enable();
44
45 /* setup timer */
46 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
47 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
48 writel(0, &tmr->timer3_match1);
49 writel(0, &tmr->timer3_match2);
50
51 /* we don't want timer to issue interrupts */
52 writel(FTTMR010_TM3_MATCH1 |
53 FTTMR010_TM3_MATCH2 |
54 FTTMR010_TM3_OVERFLOW,
55 &tmr->interrupt_mask);
56
57 cr = readl(&tmr->cr);
58 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
59 cr |= FTTMR010_TM3_ENABLE;
60 writel(cr, &tmr->cr);
61
62 /* init the timestamp and lastdec value */
63 reset_timer_masked();
64
65 return 0;
66 }
67
68 /*
69 * timer without interrupts
70 */
71
72 /*
73 * reset time
74 */
75 void reset_timer_masked(void)
76 {
77 /* capure current decrementer value time */
78 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
79 timestamp = 0; /* start "advancing" time stamp from 0 */
80
81 debug("%s(): lastdec = %lx\n", __func__, lastdec);
82 }
83
84 void reset_timer(void)
85 {
86 debug("%s()\n", __func__);
87 reset_timer_masked();
88 }
89
90 /*
91 * return timer ticks
92 */
93 ulong get_timer_masked(void)
94 {
95 /* current tick value */
96 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
97
98 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
99
100 if (lastdec >= now) {
101 /*
102 * normal mode (non roll)
103 * move stamp fordward with absoulte diff ticks
104 */
105 timestamp += lastdec - now;
106 } else {
107 /*
108 * we have overflow of the count down timer
109 *
110 * nts = ts + ld + (TLV - now)
111 * ts=old stamp, ld=time that passed before passing through -1
112 * (TLV-now) amount of time after passing though -1
113 * nts = new "advancing time stamp"...it could also roll and
114 * cause problems.
115 */
116 timestamp += lastdec + TIMER_LOAD_VAL - now;
117 }
118
119 lastdec = now;
120
121 debug("%s() returns %lx\n", __func__, timestamp);
122
123 return timestamp;
124 }
125
126 /*
127 * return difference between timer ticks and base
128 */
129 ulong get_timer(ulong base)
130 {
131 debug("%s(%lx)\n", __func__, base);
132 return get_timer_masked() - base;
133 }
134
135 /* delay x useconds AND preserve advance timestamp value */
136 void __udelay(unsigned long usec)
137 {
138 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
139 unsigned long now, last = readl(&tmr->timer3_counter);
140
141 debug("%s(%lu)\n", __func__, usec);
142 while (tmo > 0) {
143 now = readl(&tmr->timer3_counter);
144 if (now > last) /* count down timer overflow */
145 tmo -= TIMER_LOAD_VAL + last - now;
146 else
147 tmo -= last - now;
148 last = now;
149 }
150 }
151
152 /*
153 * This function is derived from PowerPC code (read timebase as long long).
154 * On ARM it just returns the timer value.
155 */
156 unsigned long long get_ticks(void)
157 {
158 debug("%s()\n", __func__);
159 return get_timer(0);
160 }
161
162 /*
163 * This function is derived from PowerPC code (timebase clock frequency).
164 * On ARM it returns the number of timer ticks per second.
165 */
166 ulong get_tbclk(void)
167 {
168 debug("%s()\n", __func__);
169 return CONFIG_SYS_HZ;
170 }