Home | History | Annotate | Line # | Download | only in footbridge
footbridge_clock.c revision 1.12
      1 /*	$NetBSD: footbridge_clock.c,v 1.12 2002/10/10 10:12:27 chris 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 #include <arm/footbridge/footbridge.h>
     53 
     54 extern struct footbridge_softc *clock_sc;
     55 extern u_int dc21285_fclk;
     56 
     57 int clockhandler __P((void *));
     58 int statclockhandler __P((void *));
     59 static int load_timer __P((int, int));
     60 
     61 /*
     62  * Statistics clock variance, in usec.  Variance must be a
     63  * power of two.  Since this gives us an even number, not an odd number,
     64  * we discard one case and compensate.  That is, a variance of 1024 would
     65  * give us offsets in [0..1023].  Instead, we take offsets in [1..1023].
     66  * This is symmetric about the point 512, or statvar/2, and thus averages
     67  * to that value (assuming uniform random numbers).
     68  */
     69 const int statvar = 1024;
     70 int statmin;			/* minimum stat clock count in ticks */
     71 int statcountperusec;		/* number of ticks per usec at current stathz */
     72 int statprev;			/* last value of we set statclock to */
     73 
     74 #if 0
     75 static int clockmatch	__P((struct device *parent, struct cfdata *cf, void *aux));
     76 static void clockattach	__P((struct device *parent, struct device *self, void *aux));
     77 
     78 CFATTACH_DECL(footbridge_clock, sizeof(struct clock_softc),
     79     clockmatch, clockattach, NULL, NULL);
     80 
     81 /*
     82  * int clockmatch(struct device *parent, void *match, void *aux)
     83  *
     84  * Just return ok for this if it is device 0
     85  */
     86 
     87 static int
     88 clockmatch(parent, cf, aux)
     89 	struct device *parent;
     90 	struct cfdata *cf;
     91 	void *aux;
     92 {
     93 	union footbridge_attach_args *fba = aux;
     94 
     95 	if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
     96 		return(1);
     97 	return(0);
     98 }
     99 
    100 
    101 /*
    102  * void clockattach(struct device *parent, struct device *dev, void *aux)
    103  *
    104  */
    105 
    106 static void
    107 clockattach(parent, self, aux)
    108 	struct device *parent;
    109 	struct device *self;
    110 	void *aux;
    111 {
    112 	struct clock_softc *sc = (struct clock_softc *)self;
    113 	union footbridge_attach_args *fba = aux;
    114 
    115 	sc->sc_iot = fba->fba_ca.ca_iot;
    116 	sc->sc_ioh = fba->fba_ca.ca_ioh;
    117 
    118 	clock_sc = sc;
    119 
    120 	/* Cannot do anything until cpu_initclocks() has been called */
    121 
    122 	printf("\n");
    123 }
    124 #endif
    125 
    126 /*
    127  * int clockhandler(struct clockframe *frame)
    128  *
    129  * Function called by timer 1 interrupts.
    130  * This just clears the interrupt condition and calls hardclock().
    131  */
    132 
    133 int
    134 clockhandler(aframe)
    135 	void *aframe;
    136 {
    137 	struct clockframe *frame = aframe;
    138 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    139 	    TIMER_1_CLEAR, 0);
    140 	hardclock(frame);
    141 	return(0);	/* Pass the interrupt on down the chain */
    142 }
    143 
    144 /*
    145  * int statclockhandler(struct clockframe *frame)
    146  *
    147  * Function called by timer 2 interrupts.
    148  * This just clears the interrupt condition and calls statclock().
    149  */
    150 
    151 int
    152 statclockhandler(aframe)
    153 	void *aframe;
    154 {
    155 	struct clockframe *frame = aframe;
    156 	int newint, r;
    157 	int currentclock ;
    158 
    159 	/* start the clock off again */
    160 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    161 			TIMER_2_CLEAR, 0);
    162 
    163 	do {
    164 		r = random() & (statvar-1);
    165 	} while (r == 0);
    166 	newint = statmin + (r * statcountperusec);
    167 
    168 	/* fetch the current count */
    169 	currentclock = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    170 		    TIMER_2_VALUE);
    171 
    172 	/*
    173 	 * work out how much time has run, add another usec for time spent
    174 	 * here
    175 	 */
    176 	r = ((statprev - currentclock) + statcountperusec);
    177 
    178 	if (r < newint) {
    179 		newint -= r;
    180 		r = 0;
    181 	}
    182 	else
    183 		printf("statclockhandler: Statclock overrun\n");
    184 
    185 
    186 	/*
    187 	 * update the clock to the new counter, this reloads the existing
    188 	 * timer
    189 	 */
    190 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    191 	    		TIMER_2_LOAD, newint);
    192 	statprev = newint;
    193 	statclock(frame);
    194 	if (r)
    195 		/*
    196 		 * We've completely overrun the previous interval,
    197 		 * make sure we report the correct number of ticks.
    198 		 */
    199 		statclock(frame);
    200 
    201 	return(0);	/* Pass the interrupt on down the chain */
    202 }
    203 
    204 static int
    205 load_timer(base, hz)
    206 	int base;
    207 	int hz;
    208 {
    209 	unsigned int timer_count;
    210 	int control;
    211 
    212 	timer_count = dc21285_fclk / hz;
    213 	if (timer_count > TIMER_MAX * 16) {
    214 		control = TIMER_FCLK_256;
    215 		timer_count >>= 8;
    216 	} else if (timer_count > TIMER_MAX) {
    217 		control = TIMER_FCLK_16;
    218 		timer_count >>= 4;
    219 	} else
    220 		control = TIMER_FCLK;
    221 
    222 	control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
    223 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    224 	    base + TIMER_LOAD, timer_count);
    225 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    226 	    base + TIMER_CONTROL, control);
    227 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    228 	    base + TIMER_CLEAR, 0);
    229 	return(timer_count);
    230 }
    231 
    232 /*
    233  * void setstatclockrate(int hz)
    234  *
    235  * Set the stat clock rate. The stat clock uses timer2
    236  */
    237 
    238 void
    239 setstatclockrate(hz)
    240 	int hz;
    241 {
    242 	int statint;
    243 	int countpersecond;
    244 	int statvarticks;
    245 
    246 	/* statint == num in counter to drop by desired hz */
    247 	statint = clock_sc->sc_statclock_count = load_timer(TIMER_2_BASE, hz);
    248 
    249 	/* Get the total ticks a second */
    250 	countpersecond = statint * hz;
    251 
    252 	/* now work out how many ticks per usec */
    253 	statcountperusec = countpersecond / 1000000;
    254 
    255 	/* calculate a variance range of statvar */
    256 	statvarticks = statcountperusec * statvar;
    257 
    258 	/* minimum is statint - 50% of variant */
    259 	statmin = statint - (statvarticks / 2);
    260 }
    261 
    262 /*
    263  * void cpu_initclocks(void)
    264  *
    265  * Initialise the clocks.
    266  *
    267  * Timer 1 is used for the main system clock (hardclock)
    268  * Timer 2 is used for the statistics clock (statclock)
    269  */
    270 
    271 void
    272 cpu_initclocks()
    273 {
    274 	/* stathz and profhz should be set to something, we have the timer */
    275 	if (stathz == 0)
    276 		stathz = hz;
    277 
    278 	if (profhz == 0)
    279 		profhz = stathz * 5;
    280 
    281 	/* Report the clock frequencies */
    282 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    283 
    284 	/* Setup timer 1 and claim interrupt */
    285 	clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
    286 
    287 	/*
    288 	 * Use ticks per 256us for accuracy since ticks per us is often
    289 	 * fractional e.g. @ 66MHz
    290 	 */
    291 	clock_sc->sc_clock_ticks_per_256us =
    292 	    ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
    293 	clock_sc->sc_clockintr = intr_claim(IRQ_TIMER_1, IPL_CLOCK,
    294 	    "tmr1 hard clk", clockhandler, 0);
    295 
    296 	if (clock_sc->sc_clockintr == NULL)
    297 		panic("%s: Cannot install timer 1 interrupt handler",
    298 		    clock_sc->sc_dev.dv_xname);
    299 
    300 	/* If stathz is non-zero then setup the stat clock */
    301 	if (stathz) {
    302 		/* Setup timer 2 and claim interrupt */
    303 		setstatclockrate(stathz);
    304        		clock_sc->sc_statclockintr = intr_claim(IRQ_TIMER_2, IPL_STATCLOCK,
    305        		    "tmr2 stat clk", statclockhandler, 0);
    306 		if (clock_sc->sc_statclockintr == NULL)
    307 			panic("%s: Cannot install timer 2 interrupt handler",
    308 			    clock_sc->sc_dev.dv_xname);
    309 	}
    310 }
    311 
    312 
    313 /*
    314  * void microtime(struct timeval *tvp)
    315  *
    316  * Fill in the specified timeval struct with the current time
    317  * accurate to the microsecond.
    318  */
    319 
    320 void
    321 microtime(tvp)
    322 	struct timeval *tvp;
    323 {
    324 	int s;
    325 	int tm;
    326 	int deltatm;
    327 	static struct timeval oldtv;
    328 
    329 	if (clock_sc == NULL || clock_sc->sc_clock_count == 0)
    330 		return;
    331 
    332 	s = splhigh();
    333 
    334 	tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    335 	    TIMER_1_VALUE);
    336 
    337 	deltatm = clock_sc->sc_clock_count - tm;
    338 
    339 #ifdef DIAGNOSTIC
    340 	if (deltatm < 0)
    341 		panic("opps deltatm < 0 tm=%d deltatm=%d", tm, deltatm);
    342 #endif
    343 
    344 	/* Fill in the timeval struct */
    345 	*tvp = time;
    346 	tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us);
    347 
    348 	/* Make sure the micro seconds don't overflow. */
    349 	while (tvp->tv_usec >= 1000000) {
    350 		tvp->tv_usec -= 1000000;
    351 		++tvp->tv_sec;
    352 	}
    353 
    354 	/* Make sure the time has advanced. */
    355 	if (tvp->tv_sec == oldtv.tv_sec &&
    356 	    tvp->tv_usec <= oldtv.tv_usec) {
    357 		tvp->tv_usec = oldtv.tv_usec + 1;
    358 		if (tvp->tv_usec >= 1000000) {
    359 			tvp->tv_usec -= 1000000;
    360 			++tvp->tv_sec;
    361 		}
    362 	}
    363 
    364 	oldtv = *tvp;
    365 	(void)splx(s);
    366 }
    367 
    368 /*
    369  * Use a timer to track microseconds, if the footbridge hasn't been setup we
    370  * rely on an estimated loop, however footbridge is attached very early on.
    371  */
    372 
    373 static int delay_clock_count = 0;
    374 static int delay_count_per_usec = 0;
    375 
    376 void
    377 calibrate_delay(void)
    378 {
    379      delay_clock_count = load_timer(TIMER_3_BASE, 100);
    380      delay_count_per_usec = delay_clock_count/10000;
    381 #ifdef VERBOSE_DELAY_CALIBRATION
    382      printf("delay calibration: delay_cc = %d, delay_c/us=%d\n",
    383 		     delay_clock_count, delay_count_per_usec);
    384 
    385      printf("0..");
    386      delay(1000000);
    387      printf("1..");
    388      delay(1000000);
    389      printf("2..");
    390      delay(1000000);
    391      printf("3..");
    392      delay(1000000);
    393      printf("4..");
    394       delay(1000000);
    395      printf("5..");
    396       delay(1000000);
    397      printf("6..");
    398       delay(1000000);
    399      printf("7..");
    400       delay(1000000);
    401      printf("8..");
    402       delay(1000000);
    403      printf("9..");
    404       delay(1000000);
    405      printf("10\n");
    406 #endif
    407 }
    408 
    409 int delaycount = 500;
    410 
    411 void
    412 delay(n)
    413 	u_int n;
    414 {
    415 	volatile u_int i;
    416 	uint32_t cur, last, delta, usecs;
    417 
    418 	if (n == 0) return;
    419 
    420 
    421 	// not calibrated the timer yet, so try to live with this horrible
    422 	// loop!
    423 	if (delay_clock_count == 0)
    424 	{
    425 	    while (n-- > 0) {
    426 		for (i = delaycount; --i;);
    427 	    }
    428 	    return;
    429 	}
    430 	last = delay_clock_count;
    431 
    432 	/* reset timer */
    433 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    434 			TIMER_3_CLEAR, 0);
    435 
    436 	delta = usecs = 0;
    437 
    438 	while (n > usecs)
    439 	{
    440 	    cur = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    441 		    TIMER_3_VALUE);
    442 	    if (last < cur)
    443 		/* timer has wrapped */
    444 		delta += ((delay_clock_count - cur) + last);
    445 	    else
    446 		delta += (last - cur);
    447 
    448 	    if (cur == 0)
    449 	    {
    450 		/* reset the timer */
    451 		bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    452 			TIMER_3_CLEAR, 0);
    453 	    }
    454 	    last = cur;
    455 
    456 	    if (delta >= delay_count_per_usec)
    457 	    {
    458 		usecs += delta / delay_count_per_usec;
    459 		delta %= delay_count_per_usec;
    460 	    }
    461 	}
    462 }
    463 
    464 /* End of footbridge_clock.c */
    465