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