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