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