s3c24x0_clk.c revision 1.2 1 1.2 bsh /* $NetBSD: s3c24x0_clk.c,v 1.2 2003/08/04 12:41:44 bsh Exp $ */
2 1.1 bsh
3 1.1 bsh /*
4 1.1 bsh * Copyright (c) 2003 Genetec corporation. All rights reserved.
5 1.1 bsh * Written by Hiroyuki Bessho for Genetec corporation.
6 1.1 bsh *
7 1.1 bsh * Redistribution and use in source and binary forms, with or without
8 1.1 bsh * modification, are permitted provided that the following conditions
9 1.1 bsh * are met:
10 1.1 bsh * 1. Redistributions of source code must retain the above copyright
11 1.1 bsh * notice, this list of conditions and the following disclaimer.
12 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bsh * notice, this list of conditions and the following disclaimer in the
14 1.1 bsh * documentation and/or other materials provided with the distribution.
15 1.1 bsh * 3. The name of Genetec corporation may not be used to endorse
16 1.1 bsh * or promote products derived from this software without specific prior
17 1.1 bsh * written permission.
18 1.1 bsh *
19 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
23 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 bsh */
31 1.1 bsh
32 1.1 bsh #include <sys/cdefs.h>
33 1.2 bsh __KERNEL_RCSID(0, "$NetBSD: s3c24x0_clk.c,v 1.2 2003/08/04 12:41:44 bsh Exp $");
34 1.1 bsh
35 1.1 bsh #include <sys/param.h>
36 1.1 bsh #include <sys/systm.h>
37 1.1 bsh #include <sys/kernel.h>
38 1.1 bsh #include <sys/time.h>
39 1.1 bsh
40 1.1 bsh #include <machine/bus.h>
41 1.1 bsh #include <machine/intr.h>
42 1.1 bsh #include <arm/cpufunc.h>
43 1.1 bsh
44 1.1 bsh #include <arm/s3c2xx0/s3c24x0reg.h>
45 1.1 bsh #include <arm/s3c2xx0/s3c24x0var.h>
46 1.1 bsh
47 1.1 bsh
48 1.1 bsh #ifndef STATHZ
49 1.1 bsh #define STATHZ 64
50 1.1 bsh #endif
51 1.1 bsh
52 1.1 bsh #define TIMER_FREQUENCY(pclk) ((pclk)/16) /* divider=1/16 */
53 1.1 bsh
54 1.1 bsh static unsigned int timer4_reload_value;
55 1.1 bsh static unsigned int timer4_prescaler;
56 1.1 bsh static unsigned int timer4_mseccount;
57 1.1 bsh
58 1.1 bsh #define usec_to_counter(t) \
59 1.1 bsh ((timer4_mseccount*(t))/1000)
60 1.1 bsh
61 1.1 bsh #define counter_to_usec(c,pclk) \
62 1.1 bsh (((c)*timer4_prescaler*1000)/(TIMER_FREQUENCY(pclk)/1000))
63 1.1 bsh
64 1.1 bsh
65 1.1 bsh /*
66 1.1 bsh * microtime:
67 1.1 bsh *
68 1.1 bsh * Fill in the specified timeval struct with the current time
69 1.1 bsh * accurate to the microsecond.
70 1.1 bsh */
71 1.1 bsh void
72 1.1 bsh microtime(struct timeval *tvp)
73 1.1 bsh {
74 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *) s3c2xx0_softc;
75 1.1 bsh int save, int_pend0, int_pend1, count, delta;
76 1.1 bsh static struct timeval last;
77 1.1 bsh int pclk = s3c2xx0_softc->sc_pclk;
78 1.1 bsh
79 1.1 bsh if( timer4_reload_value == 0 ){
80 1.1 bsh /* not initialized yet */
81 1.1 bsh tvp->tv_sec = 0;
82 1.1 bsh tvp->tv_usec = 0;
83 1.1 bsh return;
84 1.1 bsh }
85 1.1 bsh
86 1.1 bsh save = disable_interrupts(I32_bit);
87 1.1 bsh
88 1.1 bsh again:
89 1.1 bsh int_pend0 = S3C24X0_INT_TIMER4 &
90 1.1 bsh bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
91 1.1 bsh INTCTL_SRCPND);
92 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
93 1.1 bsh TIMER_TO(4));
94 1.1 bsh
95 1.1 bsh for (;;){
96 1.1 bsh
97 1.1 bsh int_pend1 = S3C24X0_INT_TIMER4 &
98 1.1 bsh bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
99 1.1 bsh INTCTL_SRCPND);
100 1.1 bsh if( int_pend0 == int_pend1 )
101 1.1 bsh break;
102 1.1 bsh
103 1.1 bsh /*
104 1.1 bsh * Down counter reached to zero while we were reading
105 1.1 bsh * timer values. do it again to get consistent values.
106 1.1 bsh */
107 1.1 bsh int_pend0 = int_pend1;
108 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
109 1.1 bsh TIMER_TO(4));
110 1.1 bsh }
111 1.1 bsh
112 1.1 bsh if( __predict_false(count > timer4_reload_value) ){
113 1.1 bsh /*
114 1.1 bsh * Buggy Hardware Warning --- sometimes timer counter
115 1.1 bsh * reads bogus value like 0xffff. I guess it happens when
116 1.1 bsh * the timer is reloaded.
117 1.1 bsh */
118 1.1 bsh printf( "Bogus value from timer counter: %d\n", count );
119 1.1 bsh goto again;
120 1.1 bsh }
121 1.1 bsh
122 1.1 bsh /* copy system time */
123 1.1 bsh *tvp = time;
124 1.1 bsh
125 1.1 bsh restore_interrupts(save);
126 1.1 bsh
127 1.1 bsh delta = timer4_reload_value - count;
128 1.1 bsh
129 1.1 bsh if( int_pend1 ){
130 1.1 bsh /*
131 1.1 bsh * down counter underflow, but
132 1.1 bsh * clock interrupt have not serviced yet
133 1.1 bsh */
134 1.1 bsh tvp->tv_usec += tick;
135 1.1 bsh }
136 1.1 bsh
137 1.1 bsh tvp->tv_usec += counter_to_usec(delta, pclk);
138 1.1 bsh
139 1.1 bsh /* Make sure microseconds doesn't overflow. */
140 1.1 bsh tvp->tv_sec += tvp->tv_usec / 1000000;
141 1.1 bsh tvp->tv_usec = tvp->tv_usec % 1000000;
142 1.1 bsh
143 1.1 bsh if (last.tv_sec &&
144 1.1 bsh (tvp->tv_sec < last.tv_sec ||
145 1.1 bsh (tvp->tv_sec == last.tv_sec &&
146 1.1 bsh tvp->tv_usec < last.tv_usec) ) ){
147 1.1 bsh
148 1.1 bsh /* XXX: This happens very often when the kernel runs
149 1.1 bsh under Multi-ICE */
150 1.1 bsh #if 0
151 1.1 bsh printf("time reversal: %ld.%06ld(%d,%d) -> %ld.%06ld(%d,%d)\n",
152 1.1 bsh last.tv_sec, last.tv_usec,
153 1.1 bsh last_count, last_pend,
154 1.1 bsh tvp->tv_sec, tvp->tv_usec,
155 1.1 bsh count, int_pend1 );
156 1.1 bsh #endif
157 1.1 bsh
158 1.1 bsh /* make sure the time has advanced. */
159 1.1 bsh *tvp = last;
160 1.1 bsh tvp->tv_usec++;
161 1.1 bsh if( tvp->tv_usec >= 1000000 ){
162 1.1 bsh tvp->tv_usec -= 1000000;
163 1.1 bsh tvp->tv_sec++;
164 1.1 bsh }
165 1.1 bsh }
166 1.1 bsh
167 1.1 bsh last = *tvp;
168 1.1 bsh
169 1.1 bsh }
170 1.1 bsh
171 1.1 bsh static __inline int
172 1.1 bsh read_timer(struct s3c24x0_softc *sc)
173 1.1 bsh {
174 1.1 bsh int count;
175 1.1 bsh
176 1.1 bsh do {
177 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
178 1.1 bsh TIMER_TO(4));
179 1.1 bsh } while ( __predict_false(count > timer4_reload_value) );
180 1.1 bsh
181 1.1 bsh return count;
182 1.1 bsh }
183 1.1 bsh
184 1.1 bsh /*
185 1.1 bsh * delay:
186 1.1 bsh *
187 1.1 bsh * Delay for at least N microseconds.
188 1.1 bsh */
189 1.1 bsh void
190 1.1 bsh delay(u_int n)
191 1.1 bsh {
192 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *) s3c2xx0_softc;
193 1.1 bsh int v0, v1, delta;
194 1.1 bsh u_int ucnt;
195 1.1 bsh
196 1.1 bsh if ( timer4_reload_value == 0 ){
197 1.1 bsh /* not initialized yet */
198 1.1 bsh while ( n-- > 0 ){
199 1.1 bsh int m;
200 1.1 bsh
201 1.1 bsh for (m=0; m<100; ++m )
202 1.1 bsh ;
203 1.1 bsh }
204 1.1 bsh return;
205 1.1 bsh }
206 1.1 bsh
207 1.1 bsh /* read down counter */
208 1.1 bsh v0 = read_timer(sc);
209 1.1 bsh
210 1.1 bsh ucnt = usec_to_counter(n);
211 1.1 bsh
212 1.1 bsh while( ucnt > 0 ) {
213 1.1 bsh v1 = read_timer(sc);
214 1.1 bsh delta = v0 - v1;
215 1.1 bsh if ( delta < 0 )
216 1.1 bsh delta += timer4_reload_value;
217 1.1 bsh #ifdef DEBUG
218 1.1 bsh if (delta < 0 || delta > timer4_reload_value)
219 1.1 bsh panic("wrong value from timer counter");
220 1.1 bsh #endif
221 1.1 bsh
222 1.1 bsh if((u_int)delta < ucnt){
223 1.1 bsh ucnt -= (u_int)delta;
224 1.1 bsh v0 = v1;
225 1.1 bsh }
226 1.1 bsh else {
227 1.1 bsh ucnt = 0;
228 1.1 bsh }
229 1.1 bsh }
230 1.1 bsh /*NOTREACHED*/
231 1.1 bsh }
232 1.1 bsh
233 1.1 bsh /*
234 1.1 bsh * inittodr:
235 1.1 bsh *
236 1.1 bsh * Initialize time from the time-of-day register.
237 1.1 bsh */
238 1.1 bsh void
239 1.1 bsh inittodr(time_t base)
240 1.1 bsh {
241 1.1 bsh
242 1.1 bsh time.tv_sec = base;
243 1.1 bsh time.tv_usec = 0;
244 1.1 bsh }
245 1.1 bsh
246 1.1 bsh /*
247 1.1 bsh * resettodr:
248 1.1 bsh *
249 1.1 bsh * Reset the time-of-day register with the current time.
250 1.1 bsh */
251 1.1 bsh void
252 1.1 bsh resettodr(void)
253 1.1 bsh {
254 1.1 bsh }
255 1.1 bsh
256 1.1 bsh void
257 1.1 bsh setstatclockrate(hz)
258 1.1 bsh int hz;
259 1.1 bsh {
260 1.1 bsh }
261 1.1 bsh
262 1.1 bsh #define hardintr (int (*)(void *))hardclock
263 1.1 bsh #define statintr (int (*)(void *))statclock
264 1.1 bsh
265 1.1 bsh void
266 1.1 bsh cpu_initclocks(void)
267 1.1 bsh {
268 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *)s3c2xx0_softc;
269 1.1 bsh long tc;
270 1.1 bsh int prescaler, h;
271 1.1 bsh int pclk = s3c2xx0_softc->sc_pclk;
272 1.1 bsh bus_space_tag_t iot = sc->sc_sx.sc_iot;
273 1.2 bsh bus_space_handle_t ioh = sc->sc_timer_ioh;
274 1.1 bsh uint32_t reg;
275 1.1 bsh
276 1.1 bsh stathz = STATHZ;
277 1.1 bsh profhz = stathz;
278 1.1 bsh
279 1.1 bsh #define time_constant(hz) (TIMER_FREQUENCY(pclk) /(hz)/ prescaler)
280 1.1 bsh #define calc_time_constant(hz) \
281 1.1 bsh do { \
282 1.1 bsh prescaler = 1; \
283 1.1 bsh do { \
284 1.1 bsh ++prescaler; \
285 1.1 bsh tc = time_constant(hz); \
286 1.1 bsh } while( tc > 65536 ); \
287 1.1 bsh } while(0)
288 1.1 bsh
289 1.1 bsh
290 1.1 bsh /* Use the channels 4 and 3 for hardclock and statclock, respectively */
291 1.1 bsh
292 1.1 bsh /* stop all timers */
293 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, 0);
294 1.1 bsh
295 1.1 bsh /* calc suitable prescaler value */
296 1.1 bsh h = MIN(hz,stathz);
297 1.1 bsh calc_time_constant(h);
298 1.1 bsh
299 1.1 bsh timer4_prescaler = prescaler;
300 1.1 bsh timer4_reload_value = TIMER_FREQUENCY(pclk) / hz / prescaler;
301 1.1 bsh timer4_mseccount = TIMER_FREQUENCY(pclk)/timer4_prescaler/1000 ;
302 1.1 bsh
303 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TB(4),
304 1.1 bsh ((prescaler - 1) << 16) | (timer4_reload_value - 1));
305 1.1 bsh
306 1.1 bsh printf("clock: hz=%d stathz = %d PCLK=%d prescaler=%d tc=%ld\n",
307 1.1 bsh hz, stathz, pclk, prescaler, tc);
308 1.1 bsh
309 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TB(3),
310 1.1 bsh ((prescaler - 1) << 16) | (time_constant(stathz) - 1));
311 1.1 bsh
312 1.2 bsh s3c24x0_intr_establish(S3C24X0_INT_TIMER4, IPL_CLOCK,
313 1.1 bsh IST_NONE, hardintr, 0);
314 1.2 bsh s3c24x0_intr_establish(S3C24X0_INT_TIMER3, IPL_STATCLOCK,
315 1.1 bsh IST_NONE, statintr, 0);
316 1.1 bsh
317 1.1 bsh /* set prescaler1 */
318 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCFG0);
319 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCFG0,
320 1.1 bsh (reg & ~0xff00) | ((prescaler-1) << 8));
321 1.1 bsh
322 1.1 bsh /* divider 1/16 for ch #3 and #4 */
323 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCFG1);
324 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCFG1,
325 1.1 bsh (reg & ~(TCFG1_MUX_MASK(3)|TCFG1_MUX_MASK(4))) |
326 1.1 bsh (TCFG1_MUX_DIV16 << TCFG1_MUX_SHIFT(3)) |
327 1.1 bsh (TCFG1_MUX_DIV16 << TCFG1_MUX_SHIFT(4)) );
328 1.1 bsh
329 1.1 bsh
330 1.1 bsh /* start timers */
331 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCON);
332 1.1 bsh reg &= ~(TCON_MASK(3)|TCON_MASK(4));
333 1.2 bsh
334 1.1 bsh /* load the time constant */
335 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, reg |
336 1.1 bsh TCON_MANUALUPDATE(3) | TCON_MANUALUPDATE(4));
337 1.1 bsh /* set auto reload and start */
338 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, reg |
339 1.1 bsh TCON_AUTORELOAD(3) | TCON_START(3) |
340 1.1 bsh TCON_AUTORELOAD(4) | TCON_START(4) );
341 1.1 bsh }
342 1.1 bsh
343 1.1 bsh
344 1.1 bsh #if 0
345 1.1 bsh /* test routine for delay() */
346 1.1 bsh
347 1.1 bsh void delay_test(void);
348 1.1 bsh void
349 1.1 bsh delay_test(void)
350 1.1 bsh {
351 1.1 bsh struct s3c2xx0_softc *sc = s3c2xx0_softc;
352 1.1 bsh volatile int *pdatc = (volatile int *)
353 1.1 bsh ((char *)bus_space_vaddr(sc->sc_iot, sc->sc_gpio_ioh) + GPIO_PDATC);
354 1.1 bsh static const int d[] = {0, 1, 5, 10, 50, 100, 500, 1000, -1};
355 1.1 bsh int i;
356 1.1 bsh int v = *pdatc & ~0x07;
357 1.1 bsh
358 1.1 bsh for (;;) {
359 1.1 bsh *pdatc = v | 2;
360 1.1 bsh
361 1.1 bsh for (i=0; d[i] >= 0; ++i) {
362 1.1 bsh *pdatc = v | 3;
363 1.1 bsh delay(d[i]);
364 1.1 bsh *pdatc = v | 2;
365 1.1 bsh }
366 1.1 bsh *pdatc = v;
367 1.1 bsh }
368 1.1 bsh }
369 1.1 bsh #endif
370 1.1 bsh
371