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