s3c24x0_clk.c revision 1.10 1 1.10 bsh /* $NetBSD: s3c24x0_clk.c,v 1.10 2008/07/04 11:59:45 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.10 bsh __KERNEL_RCSID(0, "$NetBSD: s3c24x0_clk.c,v 1.10 2008/07/04 11:59:45 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.9 joerg #include <sys/atomic.h>
39 1.1 bsh #include <sys/time.h>
40 1.9 joerg #include <sys/timetc.h>
41 1.1 bsh
42 1.1 bsh #include <machine/bus.h>
43 1.1 bsh #include <machine/intr.h>
44 1.1 bsh #include <arm/cpufunc.h>
45 1.1 bsh
46 1.1 bsh #include <arm/s3c2xx0/s3c24x0reg.h>
47 1.1 bsh #include <arm/s3c2xx0/s3c24x0var.h>
48 1.1 bsh
49 1.1 bsh
50 1.1 bsh #ifndef STATHZ
51 1.1 bsh #define STATHZ 64
52 1.1 bsh #endif
53 1.1 bsh
54 1.1 bsh #define TIMER_FREQUENCY(pclk) ((pclk)/16) /* divider=1/16 */
55 1.1 bsh
56 1.1 bsh static unsigned int timer4_reload_value;
57 1.1 bsh static unsigned int timer4_prescaler;
58 1.1 bsh static unsigned int timer4_mseccount;
59 1.1 bsh
60 1.1 bsh #define usec_to_counter(t) \
61 1.1 bsh ((timer4_mseccount*(t))/1000)
62 1.1 bsh
63 1.1 bsh #define counter_to_usec(c,pclk) \
64 1.1 bsh (((c)*timer4_prescaler*1000)/(TIMER_FREQUENCY(pclk)/1000))
65 1.1 bsh
66 1.9 joerg static u_int s3c24x0_get_timecount(struct timecounter *);
67 1.1 bsh
68 1.9 joerg static struct timecounter s3c24x0_timecounter = {
69 1.9 joerg s3c24x0_get_timecount, /* get_timecount */
70 1.9 joerg 0, /* no poll_pps */
71 1.9 joerg 0xffffffff, /* counter_mask */
72 1.9 joerg 0, /* frequency */
73 1.9 joerg "s3c234x0", /* name */
74 1.9 joerg 100, /* quality */
75 1.9 joerg NULL, /* prev */
76 1.9 joerg NULL, /* next */
77 1.9 joerg };
78 1.9 joerg
79 1.9 joerg static volatile uint32_t s3c24x0_base;
80 1.9 joerg
81 1.9 joerg static u_int
82 1.9 joerg s3c24x0_get_timecount(struct timecounter *tc)
83 1.1 bsh {
84 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *) s3c2xx0_softc;
85 1.9 joerg int save, int_pend0, int_pend1, count;
86 1.1 bsh
87 1.1 bsh save = disable_interrupts(I32_bit);
88 1.1 bsh
89 1.1 bsh again:
90 1.1 bsh int_pend0 = S3C24X0_INT_TIMER4 &
91 1.1 bsh bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
92 1.1 bsh INTCTL_SRCPND);
93 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
94 1.3 bsh TIMER_TCNTO(4));
95 1.1 bsh
96 1.9 joerg for (;;) {
97 1.1 bsh
98 1.1 bsh int_pend1 = S3C24X0_INT_TIMER4 &
99 1.1 bsh bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
100 1.1 bsh INTCTL_SRCPND);
101 1.1 bsh if( int_pend0 == int_pend1 )
102 1.1 bsh break;
103 1.1 bsh
104 1.1 bsh /*
105 1.1 bsh * Down counter reached to zero while we were reading
106 1.1 bsh * timer values. do it again to get consistent values.
107 1.1 bsh */
108 1.1 bsh int_pend0 = int_pend1;
109 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
110 1.3 bsh TIMER_TCNTO(4));
111 1.1 bsh }
112 1.1 bsh
113 1.9 joerg if (__predict_false(count > timer4_reload_value)) {
114 1.1 bsh /*
115 1.1 bsh * Buggy Hardware Warning --- sometimes timer counter
116 1.1 bsh * reads bogus value like 0xffff. I guess it happens when
117 1.1 bsh * the timer is reloaded.
118 1.1 bsh */
119 1.9 joerg printf("Bogus value from timer counter: %d\n", count);
120 1.1 bsh goto again;
121 1.1 bsh }
122 1.1 bsh
123 1.1 bsh restore_interrupts(save);
124 1.1 bsh
125 1.9 joerg if (int_pend1)
126 1.9 joerg count -= timer4_reload_value;
127 1.1 bsh
128 1.9 joerg return s3c24x0_base - count;
129 1.1 bsh }
130 1.1 bsh
131 1.6 perry static inline int
132 1.1 bsh read_timer(struct s3c24x0_softc *sc)
133 1.1 bsh {
134 1.1 bsh int count;
135 1.1 bsh
136 1.1 bsh do {
137 1.1 bsh count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_timer_ioh,
138 1.3 bsh TIMER_TCNTO(4));
139 1.1 bsh } while ( __predict_false(count > timer4_reload_value) );
140 1.1 bsh
141 1.1 bsh return count;
142 1.1 bsh }
143 1.1 bsh
144 1.1 bsh /*
145 1.1 bsh * delay:
146 1.1 bsh *
147 1.1 bsh * Delay for at least N microseconds.
148 1.1 bsh */
149 1.1 bsh void
150 1.1 bsh delay(u_int n)
151 1.1 bsh {
152 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *) s3c2xx0_softc;
153 1.1 bsh int v0, v1, delta;
154 1.1 bsh u_int ucnt;
155 1.1 bsh
156 1.1 bsh if ( timer4_reload_value == 0 ){
157 1.1 bsh /* not initialized yet */
158 1.1 bsh while ( n-- > 0 ){
159 1.1 bsh int m;
160 1.1 bsh
161 1.1 bsh for (m=0; m<100; ++m )
162 1.1 bsh ;
163 1.1 bsh }
164 1.1 bsh return;
165 1.1 bsh }
166 1.1 bsh
167 1.1 bsh /* read down counter */
168 1.1 bsh v0 = read_timer(sc);
169 1.1 bsh
170 1.1 bsh ucnt = usec_to_counter(n);
171 1.1 bsh
172 1.1 bsh while( ucnt > 0 ) {
173 1.1 bsh v1 = read_timer(sc);
174 1.1 bsh delta = v0 - v1;
175 1.1 bsh if ( delta < 0 )
176 1.1 bsh delta += timer4_reload_value;
177 1.1 bsh #ifdef DEBUG
178 1.1 bsh if (delta < 0 || delta > timer4_reload_value)
179 1.1 bsh panic("wrong value from timer counter");
180 1.1 bsh #endif
181 1.1 bsh
182 1.1 bsh if((u_int)delta < ucnt){
183 1.1 bsh ucnt -= (u_int)delta;
184 1.1 bsh v0 = v1;
185 1.1 bsh }
186 1.1 bsh else {
187 1.1 bsh ucnt = 0;
188 1.1 bsh }
189 1.1 bsh }
190 1.1 bsh /*NOTREACHED*/
191 1.1 bsh }
192 1.1 bsh
193 1.1 bsh void
194 1.4 he setstatclockrate(int newhz)
195 1.1 bsh {
196 1.1 bsh }
197 1.1 bsh
198 1.9 joerg static int
199 1.9 joerg hardintr(void *arg)
200 1.9 joerg {
201 1.9 joerg atomic_add_32(&s3c24x0_base, timer4_reload_value);
202 1.9 joerg
203 1.9 joerg hardclock((struct clockframe *)arg);
204 1.9 joerg
205 1.9 joerg return 1;
206 1.9 joerg }
207 1.1 bsh
208 1.1 bsh void
209 1.1 bsh cpu_initclocks(void)
210 1.1 bsh {
211 1.1 bsh struct s3c24x0_softc *sc = (struct s3c24x0_softc *)s3c2xx0_softc;
212 1.1 bsh long tc;
213 1.1 bsh int prescaler, h;
214 1.1 bsh int pclk = s3c2xx0_softc->sc_pclk;
215 1.1 bsh bus_space_tag_t iot = sc->sc_sx.sc_iot;
216 1.2 bsh bus_space_handle_t ioh = sc->sc_timer_ioh;
217 1.1 bsh uint32_t reg;
218 1.1 bsh
219 1.1 bsh stathz = STATHZ;
220 1.1 bsh profhz = stathz;
221 1.1 bsh
222 1.1 bsh #define time_constant(hz) (TIMER_FREQUENCY(pclk) /(hz)/ prescaler)
223 1.1 bsh #define calc_time_constant(hz) \
224 1.1 bsh do { \
225 1.1 bsh prescaler = 1; \
226 1.1 bsh do { \
227 1.1 bsh ++prescaler; \
228 1.1 bsh tc = time_constant(hz); \
229 1.1 bsh } while( tc > 65536 ); \
230 1.1 bsh } while(0)
231 1.1 bsh
232 1.1 bsh
233 1.1 bsh /* Use the channels 4 and 3 for hardclock and statclock, respectively */
234 1.1 bsh
235 1.1 bsh /* stop all timers */
236 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, 0);
237 1.1 bsh
238 1.1 bsh /* calc suitable prescaler value */
239 1.1 bsh h = MIN(hz,stathz);
240 1.1 bsh calc_time_constant(h);
241 1.1 bsh
242 1.1 bsh timer4_prescaler = prescaler;
243 1.1 bsh timer4_reload_value = TIMER_FREQUENCY(pclk) / hz / prescaler;
244 1.1 bsh timer4_mseccount = TIMER_FREQUENCY(pclk)/timer4_prescaler/1000 ;
245 1.1 bsh
246 1.3 bsh bus_space_write_4(iot, ioh, TIMER_TCNTB(4),
247 1.1 bsh ((prescaler - 1) << 16) | (timer4_reload_value - 1));
248 1.1 bsh
249 1.1 bsh printf("clock: hz=%d stathz = %d PCLK=%d prescaler=%d tc=%ld\n",
250 1.1 bsh hz, stathz, pclk, prescaler, tc);
251 1.1 bsh
252 1.3 bsh bus_space_write_4(iot, ioh, TIMER_TCNTB(3),
253 1.1 bsh ((prescaler - 1) << 16) | (time_constant(stathz) - 1));
254 1.1 bsh
255 1.2 bsh s3c24x0_intr_establish(S3C24X0_INT_TIMER4, IPL_CLOCK,
256 1.1 bsh IST_NONE, hardintr, 0);
257 1.8 matt #ifdef IPL_STATCLOCK
258 1.2 bsh s3c24x0_intr_establish(S3C24X0_INT_TIMER3, IPL_STATCLOCK,
259 1.1 bsh IST_NONE, statintr, 0);
260 1.8 matt #endif
261 1.1 bsh
262 1.1 bsh /* set prescaler1 */
263 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCFG0);
264 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCFG0,
265 1.1 bsh (reg & ~0xff00) | ((prescaler-1) << 8));
266 1.1 bsh
267 1.1 bsh /* divider 1/16 for ch #3 and #4 */
268 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCFG1);
269 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCFG1,
270 1.1 bsh (reg & ~(TCFG1_MUX_MASK(3)|TCFG1_MUX_MASK(4))) |
271 1.1 bsh (TCFG1_MUX_DIV16 << TCFG1_MUX_SHIFT(3)) |
272 1.1 bsh (TCFG1_MUX_DIV16 << TCFG1_MUX_SHIFT(4)) );
273 1.1 bsh
274 1.1 bsh
275 1.1 bsh /* start timers */
276 1.1 bsh reg = bus_space_read_4(iot, ioh, TIMER_TCON);
277 1.1 bsh reg &= ~(TCON_MASK(3)|TCON_MASK(4));
278 1.2 bsh
279 1.1 bsh /* load the time constant */
280 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, reg |
281 1.1 bsh TCON_MANUALUPDATE(3) | TCON_MANUALUPDATE(4));
282 1.1 bsh /* set auto reload and start */
283 1.1 bsh bus_space_write_4(iot, ioh, TIMER_TCON, reg |
284 1.1 bsh TCON_AUTORELOAD(3) | TCON_START(3) |
285 1.1 bsh TCON_AUTORELOAD(4) | TCON_START(4) );
286 1.9 joerg
287 1.10 bsh s3c24x0_timecounter.tc_frequency = TIMER_FREQUENCY(pclk) / timer4_prescaler;
288 1.9 joerg tc_init(&s3c24x0_timecounter);
289 1.1 bsh }
290 1.1 bsh
291 1.1 bsh
292 1.1 bsh #if 0
293 1.1 bsh /* test routine for delay() */
294 1.1 bsh
295 1.1 bsh void delay_test(void);
296 1.1 bsh void
297 1.1 bsh delay_test(void)
298 1.1 bsh {
299 1.1 bsh struct s3c2xx0_softc *sc = s3c2xx0_softc;
300 1.1 bsh volatile int *pdatc = (volatile int *)
301 1.1 bsh ((char *)bus_space_vaddr(sc->sc_iot, sc->sc_gpio_ioh) + GPIO_PDATC);
302 1.1 bsh static const int d[] = {0, 1, 5, 10, 50, 100, 500, 1000, -1};
303 1.1 bsh int i;
304 1.1 bsh int v = *pdatc & ~0x07;
305 1.1 bsh
306 1.1 bsh for (;;) {
307 1.1 bsh *pdatc = v | 2;
308 1.1 bsh
309 1.1 bsh for (i=0; d[i] >= 0; ++i) {
310 1.1 bsh *pdatc = v | 3;
311 1.1 bsh delay(d[i]);
312 1.1 bsh *pdatc = v | 2;
313 1.1 bsh }
314 1.1 bsh *pdatc = v;
315 1.1 bsh }
316 1.1 bsh }
317 1.1 bsh #endif
318 1.1 bsh
319