1 /* $NetBSD: s3c24x0_clk.c,v 1.9 2008/01/20 16:28:23 joerg Exp $ */
4 * Copyright (c) 2003 Genetec corporation. All rights reserved.
5 * Written by Hiroyuki Bessho for Genetec corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of Genetec corporation may not be used to endorse
16 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: s3c24x0_clk.c,v 1.9 2008/01/20 16:28:23 joerg Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/atomic.h>
40 #include <sys/timetc.h>
42 #include <machine/bus.h>
43 #include <machine/intr.h>
44 #include <arm/cpufunc.h>
46 #include <arm/s3c2xx0/s3c24x0reg.h>
47 #include <arm/s3c2xx0/s3c24x0var.h>
54 #define TIMER_FREQUENCY(pclk) ((pclk)/16) /* divider=1/16 */
56 static unsigned int timer4_reload_value
;
57 static unsigned int timer4_prescaler
;
58 static unsigned int timer4_mseccount
;
60 #define usec_to_counter(t) \
61 ((timer4_mseccount*(t))/1000)
63 #define counter_to_usec(c,pclk) \
64 (((c)*timer4_prescaler*1000)/(TIMER_FREQUENCY(pclk)/1000))
66 static u_int
s3c24x0_get_timecount(struct timecounter
*);
68 static struct timecounter s3c24x0_timecounter
= {
69 s3c24x0_get_timecount
, /* get_timecount */
71 0xffffffff, /* counter_mask */
73 "s3c234x0", /* name */
79 static volatile uint32_t s3c24x0_base
;
82 s3c24x0_get_timecount(struct timecounter
*tc
)
84 struct s3c24x0_softc
*sc
= (struct s3c24x0_softc
*) s3c2xx0_softc
;
85 int save
, int_pend0
, int_pend1
, count
;
87 save
= disable_interrupts(I32_bit
);
90 int_pend0
= S3C24X0_INT_TIMER4
&
91 bus_space_read_4(sc
->sc_sx
.sc_iot
, sc
->sc_sx
.sc_intctl_ioh
,
93 count
= bus_space_read_2(sc
->sc_sx
.sc_iot
, sc
->sc_timer_ioh
,
98 int_pend1
= S3C24X0_INT_TIMER4
&
99 bus_space_read_4(sc
->sc_sx
.sc_iot
, sc
->sc_sx
.sc_intctl_ioh
,
101 if( int_pend0
== int_pend1
)
105 * Down counter reached to zero while we were reading
106 * timer values. do it again to get consistent values.
108 int_pend0
= int_pend1
;
109 count
= bus_space_read_2(sc
->sc_sx
.sc_iot
, sc
->sc_timer_ioh
,
113 if (__predict_false(count
> timer4_reload_value
)) {
115 * Buggy Hardware Warning --- sometimes timer counter
116 * reads bogus value like 0xffff. I guess it happens when
117 * the timer is reloaded.
119 printf("Bogus value from timer counter: %d\n", count
);
123 restore_interrupts(save
);
126 count
-= timer4_reload_value
;
128 return s3c24x0_base
- count
;
132 read_timer(struct s3c24x0_softc
*sc
)
137 count
= bus_space_read_2(sc
->sc_sx
.sc_iot
, sc
->sc_timer_ioh
,
139 } while ( __predict_false(count
> timer4_reload_value
) );
147 * Delay for at least N microseconds.
152 struct s3c24x0_softc
*sc
= (struct s3c24x0_softc
*) s3c2xx0_softc
;
156 if ( timer4_reload_value
== 0 ){
157 /* not initialized yet */
161 for (m
=0; m
<100; ++m
)
167 /* read down counter */
170 ucnt
= usec_to_counter(n
);
176 delta
+= timer4_reload_value
;
178 if (delta
< 0 || delta
> timer4_reload_value
)
179 panic("wrong value from timer counter");
182 if((u_int
)delta
< ucnt
){
183 ucnt
-= (u_int
)delta
;
194 setstatclockrate(int newhz
)
201 atomic_add_32(&s3c24x0_base
, timer4_reload_value
);
203 hardclock((struct clockframe
*)arg
);
211 struct s3c24x0_softc
*sc
= (struct s3c24x0_softc
*)s3c2xx0_softc
;
214 int pclk
= s3c2xx0_softc
->sc_pclk
;
215 bus_space_tag_t iot
= sc
->sc_sx
.sc_iot
;
216 bus_space_handle_t ioh
= sc
->sc_timer_ioh
;
222 #define time_constant(hz) (TIMER_FREQUENCY(pclk) /(hz)/ prescaler)
223 #define calc_time_constant(hz) \
228 tc = time_constant(hz); \
229 } while( tc > 65536 ); \
233 /* Use the channels 4 and 3 for hardclock and statclock, respectively */
235 /* stop all timers */
236 bus_space_write_4(iot
, ioh
, TIMER_TCON
, 0);
238 /* calc suitable prescaler value */
240 calc_time_constant(h
);
242 timer4_prescaler
= prescaler
;
243 timer4_reload_value
= TIMER_FREQUENCY(pclk
) / hz
/ prescaler
;
244 timer4_mseccount
= TIMER_FREQUENCY(pclk
)/timer4_prescaler
/1000 ;
246 bus_space_write_4(iot
, ioh
, TIMER_TCNTB(4),
247 ((prescaler
- 1) << 16) | (timer4_reload_value
- 1));
249 printf("clock: hz=%d stathz = %d PCLK=%d prescaler=%d tc=%ld\n",
250 hz
, stathz
, pclk
, prescaler
, tc
);
252 bus_space_write_4(iot
, ioh
, TIMER_TCNTB(3),
253 ((prescaler
- 1) << 16) | (time_constant(stathz
) - 1));
255 s3c24x0_intr_establish(S3C24X0_INT_TIMER4
, IPL_CLOCK
,
256 IST_NONE
, hardintr
, 0);
258 s3c24x0_intr_establish(S3C24X0_INT_TIMER3
, IPL_STATCLOCK
,
259 IST_NONE
, statintr
, 0);
263 reg
= bus_space_read_4(iot
, ioh
, TIMER_TCFG0
);
264 bus_space_write_4(iot
, ioh
, TIMER_TCFG0
,
265 (reg
& ~0xff00) | ((prescaler
-1) << 8));
267 /* divider 1/16 for ch #3 and #4 */
268 reg
= bus_space_read_4(iot
, ioh
, TIMER_TCFG1
);
269 bus_space_write_4(iot
, ioh
, TIMER_TCFG1
,
270 (reg
& ~(TCFG1_MUX_MASK(3)|TCFG1_MUX_MASK(4))) |
271 (TCFG1_MUX_DIV16
<< TCFG1_MUX_SHIFT(3)) |
272 (TCFG1_MUX_DIV16
<< TCFG1_MUX_SHIFT(4)) );
276 reg
= bus_space_read_4(iot
, ioh
, TIMER_TCON
);
277 reg
&= ~(TCON_MASK(3)|TCON_MASK(4));
279 /* load the time constant */
280 bus_space_write_4(iot
, ioh
, TIMER_TCON
, reg
|
281 TCON_MANUALUPDATE(3) | TCON_MANUALUPDATE(4));
282 /* set auto reload and start */
283 bus_space_write_4(iot
, ioh
, TIMER_TCON
, reg
|
284 TCON_AUTORELOAD(3) | TCON_START(3) |
285 TCON_AUTORELOAD(4) | TCON_START(4) );
287 s3c24x0_timecounter
.tc_frequency
= TIMER_FREQUENCY(pclk
) / timer4_prescaler
;
288 tc_init(&s3c24x0_timecounter
);
293 /* test routine for delay() */
295 void delay_test(void);
299 struct s3c2xx0_softc
*sc
= s3c2xx0_softc
;
300 volatile int *pdatc
= (volatile int *)
301 ((char *)bus_space_vaddr(sc
->sc_iot
, sc
->sc_gpio_ioh
) + GPIO_PDATC
);
302 static const int d
[] = {0, 1, 5, 10, 50, 100, 500, 1000, -1};
304 int v
= *pdatc
& ~0x07;
309 for (i
=0; d
[i
] >= 0; ++i
) {