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