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