Home | History | Annotate | Line # | Download | only in iomd
iomd_clock.c revision 1.7
      1  1.7   provos /*	$NetBSD: iomd_clock.c,v 1.7 2002/09/27 15:35:45 provos Exp $	*/
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.1  reinoud  * Copyright (c) 1994-1997 Mark Brinicombe.
      5  1.1  reinoud  * Copyright (c) 1994 Brini.
      6  1.1  reinoud  * All rights reserved.
      7  1.1  reinoud  *
      8  1.1  reinoud  * This code is derived from software written for Brini by Mark Brinicombe
      9  1.1  reinoud  *
     10  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
     11  1.1  reinoud  * modification, are permitted provided that the following conditions
     12  1.1  reinoud  * are met:
     13  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     14  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     15  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     18  1.1  reinoud  * 3. All advertising materials mentioning features or use of this software
     19  1.1  reinoud  *    must display the following acknowledgement:
     20  1.1  reinoud  *	This product includes software developed by Mark Brinicombe.
     21  1.1  reinoud  * 4. The name of the company nor the name of the author may be used to
     22  1.1  reinoud  *    endorse or promote products derived from this software without specific
     23  1.1  reinoud  *    prior written permission.
     24  1.1  reinoud  *
     25  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26  1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     29  1.1  reinoud  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     30  1.1  reinoud  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     31  1.1  reinoud  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  reinoud  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  reinoud  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  reinoud  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  reinoud  * SUCH DAMAGE.
     36  1.1  reinoud  *
     37  1.1  reinoud  * RiscBSD kernel project
     38  1.1  reinoud  *
     39  1.1  reinoud  * clock.c
     40  1.1  reinoud  *
     41  1.1  reinoud  * Timer related machine specific code
     42  1.1  reinoud  *
     43  1.1  reinoud  * Created      : 29/09/94
     44  1.1  reinoud  */
     45  1.1  reinoud 
     46  1.1  reinoud /* Include header files */
     47  1.1  reinoud 
     48  1.1  reinoud #include <sys/param.h>
     49  1.5    bjh21 
     50  1.5    bjh21 __RCSID("$NetBSD");
     51  1.5    bjh21 
     52  1.1  reinoud #include <sys/systm.h>
     53  1.1  reinoud #include <sys/kernel.h>
     54  1.1  reinoud #include <sys/time.h>
     55  1.1  reinoud #include <sys/device.h>
     56  1.1  reinoud 
     57  1.4  thorpej #include <machine/intr.h>
     58  1.3  thorpej 
     59  1.3  thorpej #include <arm/cpufunc.h>
     60  1.3  thorpej 
     61  1.1  reinoud #include <arm/iomd/iomdvar.h>
     62  1.1  reinoud #include <arm/iomd/iomdreg.h>
     63  1.1  reinoud 
     64  1.1  reinoud struct clock_softc {
     65  1.1  reinoud 	struct device 		sc_dev;
     66  1.1  reinoud 	bus_space_tag_t		sc_iot;
     67  1.1  reinoud 	bus_space_handle_t	sc_ioh;
     68  1.1  reinoud };
     69  1.1  reinoud 
     70  1.1  reinoud #define TIMER_FREQUENCY 2000000		/* 2MHz clock */
     71  1.1  reinoud #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY / 1000000)
     72  1.1  reinoud 
     73  1.1  reinoud static void *clockirq;
     74  1.1  reinoud static void *statclockirq;
     75  1.1  reinoud static struct clock_softc *clock_sc;
     76  1.1  reinoud static int timer0_count;
     77  1.1  reinoud 
     78  1.1  reinoud static int clockmatch	__P((struct device *parent, struct cfdata *cf, void *aux));
     79  1.1  reinoud static void clockattach	__P((struct device *parent, struct device *self, void *aux));
     80  1.1  reinoud #ifdef DIAGNOSTIC
     81  1.1  reinoud static void checkdelay	__P((void));
     82  1.1  reinoud #endif
     83  1.1  reinoud 
     84  1.5    bjh21 int clockhandler	__P((void *));
     85  1.5    bjh21 int statclockhandler	__P((void *));
     86  1.5    bjh21 
     87  1.1  reinoud struct cfattach clock_ca = {
     88  1.1  reinoud 	sizeof(struct clock_softc), clockmatch, clockattach
     89  1.1  reinoud };
     90  1.1  reinoud 
     91  1.1  reinoud /*
     92  1.1  reinoud  * int clockmatch(struct device *parent, void *match, void *aux)
     93  1.1  reinoud  *
     94  1.1  reinoud  * Just return ok for this if it is device 0
     95  1.1  reinoud  */
     96  1.1  reinoud 
     97  1.1  reinoud static int
     98  1.1  reinoud clockmatch(parent, cf, aux)
     99  1.1  reinoud 	struct device *parent;
    100  1.1  reinoud 	struct cfdata *cf;
    101  1.1  reinoud 	void *aux;
    102  1.1  reinoud {
    103  1.1  reinoud 	struct clk_attach_args *ca = aux;
    104  1.1  reinoud 
    105  1.1  reinoud 	if (strcmp(ca->ca_name, "clk") == 0)
    106  1.1  reinoud 		return(1);
    107  1.1  reinoud 	return(0);
    108  1.1  reinoud }
    109  1.1  reinoud 
    110  1.1  reinoud 
    111  1.1  reinoud /*
    112  1.1  reinoud  * void clockattach(struct device *parent, struct device *dev, void *aux)
    113  1.1  reinoud  *
    114  1.1  reinoud  * Map the IOMD and identify it.
    115  1.1  reinoud  * Then configure the child devices based on the IOMD ID.
    116  1.1  reinoud  */
    117  1.1  reinoud 
    118  1.1  reinoud static void
    119  1.1  reinoud clockattach(parent, self, aux)
    120  1.1  reinoud 	struct device *parent;
    121  1.1  reinoud 	struct device *self;
    122  1.1  reinoud 	void *aux;
    123  1.1  reinoud {
    124  1.1  reinoud 	struct clock_softc *sc = (struct clock_softc *)self;
    125  1.1  reinoud 	struct clk_attach_args *ca = aux;
    126  1.1  reinoud 
    127  1.1  reinoud 	sc->sc_iot = ca->ca_iot;
    128  1.1  reinoud 	sc->sc_ioh = ca->ca_ioh; /* This is a handle for the whole IOMD */
    129  1.1  reinoud 
    130  1.1  reinoud 	clock_sc = sc;
    131  1.1  reinoud 
    132  1.1  reinoud 	/* Cannot do anything until cpu_initclocks() has been called */
    133  1.1  reinoud 
    134  1.1  reinoud 	printf("\n");
    135  1.1  reinoud }
    136  1.1  reinoud 
    137  1.1  reinoud 
    138  1.1  reinoud /*
    139  1.1  reinoud  * int clockhandler(struct clockframe *frame)
    140  1.1  reinoud  *
    141  1.1  reinoud  * Function called by timer 0 interrupts. This just calls
    142  1.1  reinoud  * hardclock(). Eventually the irqhandler can call hardclock() directly
    143  1.1  reinoud  * but for now we use this function so that we can debug IRQ's
    144  1.1  reinoud  */
    145  1.1  reinoud 
    146  1.1  reinoud int
    147  1.5    bjh21 clockhandler(cookie)
    148  1.5    bjh21 	void *cookie;
    149  1.1  reinoud {
    150  1.5    bjh21 	struct clockframe *frame = cookie;
    151  1.5    bjh21 
    152  1.1  reinoud 	hardclock(frame);
    153  1.1  reinoud 	return(0);	/* Pass the interrupt on down the chain */
    154  1.1  reinoud }
    155  1.1  reinoud 
    156  1.1  reinoud 
    157  1.1  reinoud /*
    158  1.1  reinoud  * int statclockhandler(struct clockframe *frame)
    159  1.1  reinoud  *
    160  1.1  reinoud  * Function called by timer 1 interrupts. This just calls
    161  1.1  reinoud  * statclock(). Eventually the irqhandler can call statclock() directly
    162  1.1  reinoud  * but for now we use this function so that we can debug IRQ's
    163  1.1  reinoud  */
    164  1.1  reinoud 
    165  1.1  reinoud int
    166  1.5    bjh21 statclockhandler(cookie)
    167  1.5    bjh21 	void *cookie;
    168  1.1  reinoud {
    169  1.5    bjh21 	struct clockframe *frame = cookie;
    170  1.5    bjh21 
    171  1.1  reinoud 	statclock(frame);
    172  1.1  reinoud 	return(0);	/* Pass the interrupt on down the chain */
    173  1.1  reinoud }
    174  1.1  reinoud 
    175  1.1  reinoud 
    176  1.1  reinoud /*
    177  1.1  reinoud  * void setstatclockrate(int hz)
    178  1.1  reinoud  *
    179  1.1  reinoud  * Set the stat clock rate. The stat clock uses timer1
    180  1.1  reinoud  */
    181  1.1  reinoud 
    182  1.1  reinoud void
    183  1.1  reinoud setstatclockrate(hz)
    184  1.1  reinoud 	int hz;
    185  1.1  reinoud {
    186  1.1  reinoud 	int count;
    187  1.1  reinoud 
    188  1.1  reinoud 	count = TIMER_FREQUENCY / hz;
    189  1.1  reinoud 
    190  1.1  reinoud 	printf("Setting statclock to %dHz (%d ticks)\n", hz, count);
    191  1.1  reinoud 
    192  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    193  1.1  reinoud 	    IOMD_T1LOW, (count >> 0) & 0xff);
    194  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    195  1.1  reinoud 	    IOMD_T1HIGH, (count >> 8) & 0xff);
    196  1.1  reinoud 
    197  1.1  reinoud 	/* reload the counter */
    198  1.1  reinoud 
    199  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    200  1.1  reinoud 	    IOMD_T1GO, 0);
    201  1.1  reinoud }
    202  1.1  reinoud 
    203  1.1  reinoud 
    204  1.1  reinoud #ifdef DIAGNOSTIC
    205  1.1  reinoud static void
    206  1.1  reinoud checkdelay()
    207  1.1  reinoud {
    208  1.1  reinoud 	struct timeval start, end, diff;
    209  1.1  reinoud 
    210  1.1  reinoud 	microtime(&start);
    211  1.1  reinoud 	delay(10000);
    212  1.1  reinoud 	microtime(&end);
    213  1.1  reinoud 	timersub(&end, &start, &diff);
    214  1.1  reinoud 	if (diff.tv_sec > 0)
    215  1.1  reinoud 		return;
    216  1.1  reinoud 	if (diff.tv_usec > 10000)
    217  1.1  reinoud 		return;
    218  1.1  reinoud 	printf("WARNING: delay(10000) took %ld us\n", diff.tv_usec);
    219  1.1  reinoud }
    220  1.1  reinoud #endif
    221  1.1  reinoud 
    222  1.1  reinoud /*
    223  1.1  reinoud  * void cpu_initclocks(void)
    224  1.1  reinoud  *
    225  1.1  reinoud  * Initialise the clocks.
    226  1.1  reinoud  * This sets up the two timers in the IOMD and installs the IRQ handlers
    227  1.1  reinoud  *
    228  1.1  reinoud  * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
    229  1.1  reinoud  */
    230  1.1  reinoud 
    231  1.1  reinoud void
    232  1.1  reinoud cpu_initclocks()
    233  1.1  reinoud {
    234  1.1  reinoud 	/*
    235  1.1  reinoud 	 * Load timer 0 with count down value
    236  1.1  reinoud 	 * This timer generates 100Hz interrupts for the system clock
    237  1.1  reinoud 	 */
    238  1.1  reinoud 
    239  1.1  reinoud 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    240  1.1  reinoud 
    241  1.1  reinoud 	timer0_count = TIMER_FREQUENCY / hz;
    242  1.1  reinoud 
    243  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    244  1.1  reinoud 	    IOMD_T0LOW, (timer0_count >> 0) & 0xff);
    245  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    246  1.1  reinoud 	    IOMD_T0HIGH, (timer0_count >> 8) & 0xff);
    247  1.1  reinoud 
    248  1.1  reinoud 	/* reload the counter */
    249  1.1  reinoud 
    250  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    251  1.1  reinoud 	    IOMD_T0GO, 0);
    252  1.1  reinoud 
    253  1.1  reinoud 	clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
    254  1.1  reinoud 	    clockhandler, 0);
    255  1.1  reinoud 
    256  1.1  reinoud 	if (clockirq == NULL)
    257  1.7   provos 		panic("%s: Cannot installer timer 0 IRQ handler",
    258  1.1  reinoud 		    clock_sc->sc_dev.dv_xname);
    259  1.1  reinoud 
    260  1.1  reinoud 	if (stathz) {
    261  1.1  reinoud 		setstatclockrate(stathz);
    262  1.1  reinoud        		statclockirq = intr_claim(IRQ_TIMER1, IPL_CLOCK,
    263  1.1  reinoud        		    "tmr1 stat clk", statclockhandler, 0);
    264  1.1  reinoud 		if (statclockirq == NULL)
    265  1.7   provos 			panic("%s: Cannot installer timer 1 IRQ handler",
    266  1.1  reinoud 			    clock_sc->sc_dev.dv_xname);
    267  1.1  reinoud 	}
    268  1.1  reinoud #ifdef DIAGNOSTIC
    269  1.1  reinoud 	checkdelay();
    270  1.1  reinoud #endif
    271  1.1  reinoud }
    272  1.1  reinoud 
    273  1.1  reinoud 
    274  1.1  reinoud /*
    275  1.1  reinoud  * void microtime(struct timeval *tvp)
    276  1.1  reinoud  *
    277  1.1  reinoud  * Fill in the specified timeval struct with the current time
    278  1.1  reinoud  * accurate to the microsecond.
    279  1.1  reinoud  */
    280  1.1  reinoud 
    281  1.1  reinoud void
    282  1.1  reinoud microtime(tvp)
    283  1.1  reinoud 	struct timeval *tvp;
    284  1.1  reinoud {
    285  1.1  reinoud 	int s;
    286  1.1  reinoud 	int tm;
    287  1.1  reinoud 	int deltatm;
    288  1.1  reinoud 	static struct timeval oldtv;
    289  1.1  reinoud 
    290  1.1  reinoud 	if (timer0_count == 0)
    291  1.1  reinoud 		return;
    292  1.1  reinoud 
    293  1.1  reinoud 	s = splhigh();
    294  1.1  reinoud 
    295  1.1  reinoud 	/*
    296  1.1  reinoud 	 * Latch the current value of the timer and then read it.
    297  1.1  reinoud 	 * This garentees an atmoic reading of the time.
    298  1.1  reinoud 	 */
    299  1.1  reinoud 
    300  1.1  reinoud 	bus_space_write_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    301  1.1  reinoud 	    IOMD_T0LATCH, 0);
    302  1.1  reinoud 
    303  1.1  reinoud 	tm = bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    304  1.1  reinoud 	    IOMD_T0LOW);
    305  1.1  reinoud 	tm += (bus_space_read_1(clock_sc->sc_iot, clock_sc->sc_ioh,
    306  1.1  reinoud 	    IOMD_T0HIGH) << 8);
    307  1.1  reinoud 
    308  1.1  reinoud 	deltatm = timer0_count - tm;
    309  1.1  reinoud 	if (deltatm < 0)
    310  1.1  reinoud 		printf("opps deltatm < 0 tm=%d deltatm=%d\n",
    311  1.1  reinoud 		    tm, deltatm);
    312  1.1  reinoud 
    313  1.1  reinoud 	/* Fill in the timeval struct */
    314  1.1  reinoud 	*tvp = time;
    315  1.1  reinoud 
    316  1.1  reinoud 	tvp->tv_usec += (deltatm / TICKS_PER_MICROSECOND);
    317  1.1  reinoud 
    318  1.1  reinoud 	/* Make sure the micro seconds don't overflow. */
    319  1.1  reinoud 	while (tvp->tv_usec >= 1000000) {
    320  1.1  reinoud 		tvp->tv_usec -= 1000000;
    321  1.1  reinoud 		++tvp->tv_sec;
    322  1.1  reinoud 	}
    323  1.1  reinoud 
    324  1.1  reinoud 	/* Make sure the time has advanced. */
    325  1.1  reinoud 	if (tvp->tv_sec == oldtv.tv_sec &&
    326  1.1  reinoud 	    tvp->tv_usec <= oldtv.tv_usec) {
    327  1.1  reinoud 		tvp->tv_usec = oldtv.tv_usec + 1;
    328  1.1  reinoud 		if (tvp->tv_usec >= 1000000) {
    329  1.1  reinoud 			tvp->tv_usec -= 1000000;
    330  1.1  reinoud 			++tvp->tv_sec;
    331  1.1  reinoud 		}
    332  1.1  reinoud 	}
    333  1.1  reinoud 
    334  1.1  reinoud 	oldtv = *tvp;
    335  1.1  reinoud 	(void)splx(s);
    336  1.1  reinoud }
    337  1.1  reinoud 
    338  1.1  reinoud /*
    339  1.1  reinoud  * Estimated loop for n microseconds
    340  1.1  reinoud  */
    341  1.1  reinoud 
    342  1.1  reinoud /* Need to re-write this to use the timers */
    343  1.1  reinoud 
    344  1.1  reinoud /* One day soon I will actually do this */
    345  1.1  reinoud 
    346  1.1  reinoud int delaycount = 100;
    347  1.1  reinoud 
    348  1.1  reinoud void
    349  1.1  reinoud delay(n)
    350  1.1  reinoud 	u_int n;
    351  1.1  reinoud {
    352  1.1  reinoud 	u_int i;
    353  1.1  reinoud 
    354  1.1  reinoud 	if (n == 0) return;
    355  1.6  mycroft 	while (n-- > 0) {
    356  1.1  reinoud 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    357  1.1  reinoud 			for (i = delaycount; --i;);
    358  1.1  reinoud 		else
    359  1.1  reinoud 			for (i = 8; --i;);
    360  1.1  reinoud 	}
    361  1.1  reinoud }
    362  1.1  reinoud 
    363  1.1  reinoud /* End of iomd_clock.c */
    364