Home | History | Annotate | Line # | Download | only in footbridge
footbridge_clock.c revision 1.3
      1 /*	$NetBSD: footbridge_clock.c,v 1.3 2001/11/23 19:21:47 thorpej 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/intr.h>
     47 
     48 #include <arm/cpufunc.h>
     49 
     50 #include <arm/footbridge/dc21285reg.h>
     51 #include <arm/footbridge/footbridgevar.h>
     52 
     53 extern struct footbridge_softc *clock_sc;
     54 extern u_int dc21285_fclk;
     55 
     56 #if 0
     57 static int clockmatch	__P((struct device *parent, struct cfdata *cf, void *aux));
     58 static void clockattach	__P((struct device *parent, struct device *self, void *aux));
     59 
     60 struct cfattach footbridge_clock_ca = {
     61 	sizeof(struct clock_softc), clockmatch, clockattach
     62 };
     63 
     64 /*
     65  * int clockmatch(struct device *parent, void *match, void *aux)
     66  *
     67  * Just return ok for this if it is device 0
     68  */
     69 
     70 static int
     71 clockmatch(parent, cf, aux)
     72 	struct device *parent;
     73 	struct cfdata *cf;
     74 	void *aux;
     75 {
     76 	union footbridge_attach_args *fba = aux;
     77 
     78 	if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
     79 		return(1);
     80 	return(0);
     81 }
     82 
     83 
     84 /*
     85  * void clockattach(struct device *parent, struct device *dev, void *aux)
     86  *
     87  */
     88 
     89 static void
     90 clockattach(parent, self, aux)
     91 	struct device *parent;
     92 	struct device *self;
     93 	void *aux;
     94 {
     95 	struct clock_softc *sc = (struct clock_softc *)self;
     96 	union footbridge_attach_args *fba = aux;
     97 
     98 	sc->sc_iot = fba->fba_ca.ca_iot;
     99 	sc->sc_ioh = fba->fba_ca.ca_ioh;
    100 
    101 	clock_sc = sc;
    102 
    103 	/* Cannot do anything until cpu_initclocks() has been called */
    104 
    105 	printf("\n");
    106 }
    107 #endif
    108 
    109 /*
    110  * int clockhandler(struct clockframe *frame)
    111  *
    112  * Function called by timer 1 interrupts.
    113  * This just clears the interrupt condition and calls hardclock().
    114  */
    115 
    116 int
    117 clockhandler(frame)
    118 	struct clockframe *frame;
    119 {
    120 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    121 	    TIMER_1_CLEAR, 0);
    122 	hardclock(frame);
    123 	return(0);	/* Pass the interrupt on down the chain */
    124 }
    125 
    126 
    127 /*
    128  * int statclockhandler(struct clockframe *frame)
    129  *
    130  * Function called by timer 2 interrupts.
    131  * This just clears the interrupt condition and calls statclock().
    132  */
    133 
    134 int
    135 statclockhandler(frame)
    136 	struct clockframe *frame;
    137 {
    138 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    139 	    TIMER_2_CLEAR, 0);
    140 	statclock(frame);
    141 	return(0);	/* Pass the interrupt on down the chain */
    142 }
    143 
    144 static int
    145 load_timer(base, hz)
    146 	int base;
    147 	int hz;
    148 {
    149 	unsigned int timer_count;
    150 	int control;
    151 
    152 	timer_count = dc21285_fclk / hz;
    153 	if (timer_count > TIMER_MAX * 16) {
    154 		control = TIMER_FCLK_256;
    155 		timer_count >>= 8;
    156 	} else if (timer_count > TIMER_MAX) {
    157 		control = TIMER_FCLK_16;
    158 		timer_count >>= 4;
    159 	} else
    160 		control = TIMER_FCLK;
    161 
    162 	control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
    163 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    164 	    base + TIMER_LOAD, timer_count);
    165 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    166 	    base + TIMER_CONTROL, control);
    167 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    168 	    base + TIMER_CLEAR, 0);
    169 	return(timer_count);
    170 }
    171 
    172 /*
    173  * void setstatclockrate(int hz)
    174  *
    175  * Set the stat clock rate. The stat clock uses timer2
    176  */
    177 
    178 void
    179 setstatclockrate(hz)
    180 	int hz;
    181 {
    182 
    183 	clock_sc->sc_statclock_count = load_timer(TIMER_2_BASE, hz);
    184 }
    185 
    186 /*
    187  * void cpu_initclocks(void)
    188  *
    189  * Initialise the clocks.
    190  *
    191  * Timer 1 is used for the main system clock (hardclock)
    192  * Timer 2 is used for the statistics clock (statclock)
    193  */
    194 
    195 void
    196 cpu_initclocks()
    197 {
    198 
    199 	/* Report the clock frequencies */
    200 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    201 
    202 	/* Setup timer 1 and claim interrupt */
    203 	clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
    204 
    205 	/*
    206 	 * Use ticks per 256us for accuracy since ticks per us is often
    207 	 * fractional e.g. @ 66MHz
    208 	 */
    209 	clock_sc->sc_clock_ticks_per_256us =
    210 	    ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
    211 	clock_sc->sc_clockintr = intr_claim(IRQ_TIMER_1, IPL_CLOCK,
    212 	    "tmr1 hard clk", clockhandler, 0);
    213 
    214 	if (clock_sc->sc_clockintr == NULL)
    215 		panic("%s: Cannot install timer 1 interrupt handler\n",
    216 		    clock_sc->sc_dev.dv_xname);
    217 
    218 	/* If stathz is non-zero then setup the stat clock */
    219 	if (stathz) {
    220 		/* Setup timer 2 and claim interrupt */
    221 		setstatclockrate(stathz);
    222        		clock_sc->sc_statclockintr = intr_claim(IRQ_TIMER_2, IPL_CLOCK,
    223        		    "tmr2 stat clk", statclockhandler, 0);
    224 		if (clock_sc->sc_statclockintr == NULL)
    225 			panic("%s: Cannot install timer 2 interrupt handler\n",
    226 			    clock_sc->sc_dev.dv_xname);
    227 	}
    228 }
    229 
    230 
    231 /*
    232  * void microtime(struct timeval *tvp)
    233  *
    234  * Fill in the specified timeval struct with the current time
    235  * accurate to the microsecond.
    236  */
    237 
    238 void
    239 microtime(tvp)
    240 	struct timeval *tvp;
    241 {
    242 	int s;
    243 	int tm;
    244 	int deltatm;
    245 	static struct timeval oldtv;
    246 
    247 	if (clock_sc == NULL || clock_sc->sc_clock_count == 0)
    248 		return;
    249 
    250 	s = splhigh();
    251 
    252 	tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    253 	    TIMER_1_VALUE);
    254 
    255 	deltatm = clock_sc->sc_clock_count - tm;
    256 
    257 #ifdef DIAGNOSTIC
    258 	if (deltatm < 0)
    259 		panic("opps deltatm < 0 tm=%d deltatm=%d\n", tm, deltatm);
    260 #endif
    261 
    262 	/* Fill in the timeval struct */
    263 	*tvp = time;
    264 	tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us);
    265 
    266 	/* Make sure the micro seconds don't overflow. */
    267 	while (tvp->tv_usec >= 1000000) {
    268 		tvp->tv_usec -= 1000000;
    269 		++tvp->tv_sec;
    270 	}
    271 
    272 	/* Make sure the time has advanced. */
    273 	if (tvp->tv_sec == oldtv.tv_sec &&
    274 	    tvp->tv_usec <= oldtv.tv_usec) {
    275 		tvp->tv_usec = oldtv.tv_usec + 1;
    276 		if (tvp->tv_usec >= 1000000) {
    277 			tvp->tv_usec -= 1000000;
    278 			++tvp->tv_sec;
    279 		}
    280 	}
    281 
    282 	oldtv = *tvp;
    283 	(void)splx(s);
    284 }
    285 
    286 /*
    287  * Estimated loop for n microseconds
    288  */
    289 
    290 /* Need to re-write this to use the timers */
    291 
    292 /* One day soon I will actually do this */
    293 
    294 int delaycount = 50;
    295 
    296 void
    297 delay(n)
    298 	u_int n;
    299 {
    300 	u_int i;
    301 
    302 	if (n == 0) return;
    303 	while (--n > 0) {
    304 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    305 			for (i = delaycount; --i;);
    306 		else
    307 			for (i = 8; --i;);
    308 	}
    309 }
    310 
    311 /* End of footbridge_clock.c */
    312