Home | History | Annotate | Line # | Download | only in footbridge
footbridge_clock.c revision 1.2
      1 /*	$NetBSD: footbridge_clock.c,v 1.2 2001/09/05 16:17:35 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Mark Brinicombe.
      5  * Copyright (c) 1997 Causality Limited.
      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. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Mark Brinicombe
     19  *	for the NetBSD Project.
     20  * 4. The name of the company nor the name of the author may be used to
     21  *    endorse or promote products derived from this software without specific
     22  *    prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 /* Include header files */
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/time.h>
     44 #include <sys/device.h>
     45 
     46 #include <machine/cpufunc.h>
     47 #include <machine/intr.h>
     48 #include <arm/footbridge/dc21285reg.h>
     49 #include <arm/footbridge/footbridgevar.h>
     50 
     51 extern struct footbridge_softc *clock_sc;
     52 extern u_int dc21285_fclk;
     53 
     54 #if 0
     55 static int clockmatch	__P((struct device *parent, struct cfdata *cf, void *aux));
     56 static void clockattach	__P((struct device *parent, struct device *self, void *aux));
     57 
     58 struct cfattach footbridge_clock_ca = {
     59 	sizeof(struct clock_softc), clockmatch, clockattach
     60 };
     61 
     62 /*
     63  * int clockmatch(struct device *parent, void *match, void *aux)
     64  *
     65  * Just return ok for this if it is device 0
     66  */
     67 
     68 static int
     69 clockmatch(parent, cf, aux)
     70 	struct device *parent;
     71 	struct cfdata *cf;
     72 	void *aux;
     73 {
     74 	union footbridge_attach_args *fba = aux;
     75 
     76 	if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
     77 		return(1);
     78 	return(0);
     79 }
     80 
     81 
     82 /*
     83  * void clockattach(struct device *parent, struct device *dev, void *aux)
     84  *
     85  */
     86 
     87 static void
     88 clockattach(parent, self, aux)
     89 	struct device *parent;
     90 	struct device *self;
     91 	void *aux;
     92 {
     93 	struct clock_softc *sc = (struct clock_softc *)self;
     94 	union footbridge_attach_args *fba = aux;
     95 
     96 	sc->sc_iot = fba->fba_ca.ca_iot;
     97 	sc->sc_ioh = fba->fba_ca.ca_ioh;
     98 
     99 	clock_sc = sc;
    100 
    101 	/* Cannot do anything until cpu_initclocks() has been called */
    102 
    103 	printf("\n");
    104 }
    105 #endif
    106 
    107 /*
    108  * int clockhandler(struct clockframe *frame)
    109  *
    110  * Function called by timer 1 interrupts.
    111  * This just clears the interrupt condition and calls hardclock().
    112  */
    113 
    114 int
    115 clockhandler(frame)
    116 	struct clockframe *frame;
    117 {
    118 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    119 	    TIMER_1_CLEAR, 0);
    120 	hardclock(frame);
    121 	return(0);	/* Pass the interrupt on down the chain */
    122 }
    123 
    124 
    125 /*
    126  * int statclockhandler(struct clockframe *frame)
    127  *
    128  * Function called by timer 2 interrupts.
    129  * This just clears the interrupt condition and calls statclock().
    130  */
    131 
    132 int
    133 statclockhandler(frame)
    134 	struct clockframe *frame;
    135 {
    136 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    137 	    TIMER_2_CLEAR, 0);
    138 	statclock(frame);
    139 	return(0);	/* Pass the interrupt on down the chain */
    140 }
    141 
    142 static int
    143 load_timer(base, hz)
    144 	int base;
    145 	int hz;
    146 {
    147 	unsigned int timer_count;
    148 	int control;
    149 
    150 	timer_count = dc21285_fclk / hz;
    151 	if (timer_count > TIMER_MAX * 16) {
    152 		control = TIMER_FCLK_256;
    153 		timer_count >>= 8;
    154 	} else if (timer_count > TIMER_MAX) {
    155 		control = TIMER_FCLK_16;
    156 		timer_count >>= 4;
    157 	} else
    158 		control = TIMER_FCLK;
    159 
    160 	control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
    161 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    162 	    base + TIMER_LOAD, timer_count);
    163 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    164 	    base + TIMER_CONTROL, control);
    165 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    166 	    base + TIMER_CLEAR, 0);
    167 	return(timer_count);
    168 }
    169 
    170 /*
    171  * void setstatclockrate(int hz)
    172  *
    173  * Set the stat clock rate. The stat clock uses timer2
    174  */
    175 
    176 void
    177 setstatclockrate(hz)
    178 	int hz;
    179 {
    180 
    181 	clock_sc->sc_statclock_count = load_timer(TIMER_2_BASE, hz);
    182 }
    183 
    184 /*
    185  * void cpu_initclocks(void)
    186  *
    187  * Initialise the clocks.
    188  *
    189  * Timer 1 is used for the main system clock (hardclock)
    190  * Timer 2 is used for the statistics clock (statclock)
    191  */
    192 
    193 void
    194 cpu_initclocks()
    195 {
    196 
    197 	/* Report the clock frequencies */
    198 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    199 
    200 	/* Setup timer 1 and claim interrupt */
    201 	clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
    202 
    203 	/*
    204 	 * Use ticks per 256us for accuracy since ticks per us is often
    205 	 * fractional e.g. @ 66MHz
    206 	 */
    207 	clock_sc->sc_clock_ticks_per_256us =
    208 	    ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
    209 	clock_sc->sc_clockintr = intr_claim(IRQ_TIMER_1, IPL_CLOCK,
    210 	    "tmr1 hard clk", clockhandler, 0);
    211 
    212 	if (clock_sc->sc_clockintr == NULL)
    213 		panic("%s: Cannot install timer 1 interrupt handler\n",
    214 		    clock_sc->sc_dev.dv_xname);
    215 
    216 	/* If stathz is non-zero then setup the stat clock */
    217 	if (stathz) {
    218 		/* Setup timer 2 and claim interrupt */
    219 		setstatclockrate(stathz);
    220        		clock_sc->sc_statclockintr = intr_claim(IRQ_TIMER_2, IPL_CLOCK,
    221        		    "tmr2 stat clk", statclockhandler, 0);
    222 		if (clock_sc->sc_statclockintr == NULL)
    223 			panic("%s: Cannot install timer 2 interrupt handler\n",
    224 			    clock_sc->sc_dev.dv_xname);
    225 	}
    226 }
    227 
    228 
    229 /*
    230  * void microtime(struct timeval *tvp)
    231  *
    232  * Fill in the specified timeval struct with the current time
    233  * accurate to the microsecond.
    234  */
    235 
    236 void
    237 microtime(tvp)
    238 	struct timeval *tvp;
    239 {
    240 	int s;
    241 	int tm;
    242 	int deltatm;
    243 	static struct timeval oldtv;
    244 
    245 	if (clock_sc == NULL || clock_sc->sc_clock_count == 0)
    246 		return;
    247 
    248 	s = splhigh();
    249 
    250 	tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    251 	    TIMER_1_VALUE);
    252 
    253 	deltatm = clock_sc->sc_clock_count - tm;
    254 
    255 #ifdef DIAGNOSTIC
    256 	if (deltatm < 0)
    257 		panic("opps deltatm < 0 tm=%d deltatm=%d\n", tm, deltatm);
    258 #endif
    259 
    260 	/* Fill in the timeval struct */
    261 	*tvp = time;
    262 	tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us);
    263 
    264 	/* Make sure the micro seconds don't overflow. */
    265 	while (tvp->tv_usec >= 1000000) {
    266 		tvp->tv_usec -= 1000000;
    267 		++tvp->tv_sec;
    268 	}
    269 
    270 	/* Make sure the time has advanced. */
    271 	if (tvp->tv_sec == oldtv.tv_sec &&
    272 	    tvp->tv_usec <= oldtv.tv_usec) {
    273 		tvp->tv_usec = oldtv.tv_usec + 1;
    274 		if (tvp->tv_usec >= 1000000) {
    275 			tvp->tv_usec -= 1000000;
    276 			++tvp->tv_sec;
    277 		}
    278 	}
    279 
    280 	oldtv = *tvp;
    281 	(void)splx(s);
    282 }
    283 
    284 /*
    285  * Estimated loop for n microseconds
    286  */
    287 
    288 /* Need to re-write this to use the timers */
    289 
    290 /* One day soon I will actually do this */
    291 
    292 int delaycount = 50;
    293 
    294 void
    295 delay(n)
    296 	u_int n;
    297 {
    298 	u_int i;
    299 
    300 	if (n == 0) return;
    301 	while (--n > 0) {
    302 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    303 			for (i = delaycount; --i;);
    304 		else
    305 			for (i = 8; --i;);
    306 	}
    307 }
    308 
    309 /* End of footbridge_clock.c */
    310