Home | History | Annotate | Line # | Download | only in ifpga
ifpga_clock.c revision 1.11
      1 /*	$NetBSD: ifpga_clock.c,v 1.11 2007/12/22 01:21:41 ad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001 ARM Ltd
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the company may not be used to endorse or promote
     16  *    products derived from this software without specific prior written
     17  *    permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * The IFPGA has three timers.  Timer 0 is clocked by the system bus clock,
     34  * while timers 1 and 2 are clocked at 24MHz.  To keep things simple here,
     35  * we use timers 1 and 2 only.  All three timers are 16-bit counters that
     36  * are programmable in either periodic mode or in one-shot mode.
     37  */
     38 
     39 /* Include header files */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: ifpga_clock.c,v 1.11 2007/12/22 01:21:41 ad Exp $");
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/time.h>
     49 #include <sys/device.h>
     50 
     51 #include <arm/cpufunc.h>
     52 #include <machine/intr.h>
     53 
     54 #include <evbarm/ifpga/ifpgavar.h>
     55 #include <evbarm/ifpga/ifpgamem.h>
     56 #include <evbarm/ifpga/ifpgareg.h>
     57 
     58 /*
     59  * Statistics clock interval and variance, in usec.  Variance must be a
     60  * power of two.  Since this gives us an even number, not an odd number,
     61  * we discard one case and compensate.  That is, a variance of 1024 would
     62  * give us offsets in [0..1023].  Instead, we take offsets in [1..1023].
     63  * This is symmetric about the point 512, or statvar/2, and thus averages
     64  * to that value (assuming uniform random numbers).
     65  */
     66 static int statvar = 1024 / 4;	/* {stat,prof}clock variance */
     67 static int statmin;		/* statclock interval - variance/2 */
     68 static int profmin;		/* profclock interval - variance/2 */
     69 static int timer2min;		/* current, from above choices */
     70 static int statprev;		/* previous value in stat timer */
     71 
     72 #define TIMER_1_CLEAR (IFPGA_TIMER1_BASE + TIMERx_CLR)
     73 #define TIMER_1_LOAD  (IFPGA_TIMER1_BASE + TIMERx_LOAD)
     74 #define TIMER_1_VALUE (IFPGA_TIMER1_BASE + TIMERx_VALUE)
     75 #define TIMER_1_CTRL  (IFPGA_TIMER1_BASE + TIMERx_CTRL)
     76 
     77 #define TIMER_2_CLEAR (IFPGA_TIMER2_BASE + TIMERx_CLR)
     78 #define TIMER_2_LOAD  (IFPGA_TIMER2_BASE + TIMERx_LOAD)
     79 #define TIMER_2_VALUE (IFPGA_TIMER2_BASE + TIMERx_VALUE)
     80 #define TIMER_2_CTRL  (IFPGA_TIMER2_BASE + TIMERx_CTRL)
     81 
     82 #define COUNTS_PER_SEC (IFPGA_TIMER1_FREQ / 16)
     83 
     84 extern struct ifpga_softc *ifpga_sc;
     85 
     86 static int clock_started = 0;
     87 
     88 static int load_timer(int, int);
     89 
     90 static inline u_int
     91 getclock(void)
     92 {
     93 	return bus_space_read_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
     94 	    TIMER_1_VALUE);
     95 }
     96 
     97 static inline u_int
     98 getstatclock(void)
     99 {
    100 	return bus_space_read_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
    101 	    TIMER_2_VALUE);
    102 }
    103 
    104 /*
    105  * int clockhandler(struct clockframe *frame)
    106  *
    107  * Function called by timer 1 interrupts.
    108  * This just clears the interrupt condition and calls hardclock().
    109  */
    110 
    111 static int
    112 clockhandler(void *fr)
    113 {
    114 	struct clockframe *frame = (struct clockframe *)fr;
    115 
    116 	bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
    117 	    TIMER_1_CLEAR, 0);
    118 	hardclock(frame);
    119 	return 0;	/* Pass the interrupt on down the chain */
    120 }
    121 
    122 
    123 /*
    124  * int statclockhandler(struct clockframe *frame)
    125  *
    126  * Function called by timer 2 interrupts.
    127  * Add some random jitter to the clock, and then call statclock().
    128  */
    129 
    130 static int
    131 statclockhandler(void *fr)
    132 {
    133 	struct clockframe *frame = (struct clockframe *) fr;
    134 	int newint, r, var;
    135 
    136 	var = statvar;
    137 	do {
    138 		r = random() & (var - 1);
    139 	} while (r == 0);
    140 	newint = timer2min + r;
    141 
    142 	if (newint & ~0x0000ffff)
    143 		panic("statclockhandler: statclock variance too large");
    144 
    145 	/*
    146 	 * The timer was automatically reloaded with the previous latch
    147 	 * value at the time of the interrupts.  Compensate now for the
    148 	 * amount of time that has run off since then, plus one tick
    149 	 * roundoff.  This should keep us closer to the mean.
    150 	 */
    151 
    152 	r = (statprev - getstatclock() + 1);
    153 	if (r < newint) {
    154 		newint -= r;
    155 		r = 0;
    156 	}
    157 	else
    158 		printf("statclockhandler: Statclock overrun\n");
    159 
    160 	statprev = load_timer(IFPGA_TIMER2_BASE, newint);
    161 	statclock(frame);
    162 	if (r)
    163 		/*
    164 		 * We've completely overrun the previous interval,
    165 		 * make sure we report the correct number of ticks.
    166 		 */
    167 		statclock(frame);
    168 
    169 	return 0;	/* Pass the interrupt on down the chain */
    170 }
    171 
    172 static int
    173 load_timer(int base, int intvl)
    174 {
    175 	int control;
    176 
    177 	if (intvl & ~0x0000ffff)
    178 		panic("clock: Invalid interval");
    179 
    180 	control = (TIMERx_CTRL_ENABLE | TIMERx_CTRL_MODE_PERIODIC |
    181 	    TIMERx_CTRL_PRESCALE_DIV16);
    182 
    183 	bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
    184 	    base + TIMERx_LOAD, intvl);
    185 	bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
    186 	    base + TIMERx_CTRL, control);
    187 	bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
    188 	    base + TIMERx_CLR, 0);
    189 	return intvl;
    190 }
    191 
    192 /*
    193  * void setstatclockrate(int hz)
    194  *
    195  * We assume that hz is either stathz or profhz, and that neither will
    196  * change after being set by cpu_initclocks().  We could recalculate the
    197  * intervals here, but that would be a pain.
    198  */
    199 
    200 void
    201 setstatclockrate(int new_hz)
    202 {
    203 	if (new_hz == stathz)
    204 		timer2min = statmin;
    205 	else
    206 		timer2min = profmin;
    207 }
    208 
    209 /*
    210  * void cpu_initclocks(void)
    211  *
    212  * Initialise the clocks.
    213  */
    214 
    215 void
    216 cpu_initclocks()
    217 {
    218 	int intvl;
    219 	int statint;
    220 	int profint;
    221 	int minint;
    222 
    223 	if (hz < 50 || COUNTS_PER_SEC % hz) {
    224 		printf("cannot get %d Hz clock; using 100 Hz\n", hz);
    225 		hz = 100;
    226 		tick = 1000000 / hz;
    227 	}
    228 
    229 	if (stathz == 0)
    230 		stathz = hz;
    231 	else if (stathz < 50 || COUNTS_PER_SEC % stathz) {
    232 		printf("cannot get %d Hz statclock; using 100 Hz\n", stathz);
    233 		stathz = 100;
    234 	}
    235 
    236 	if (profhz == 0)
    237 		profhz = stathz * 5;
    238 	else if (profhz < stathz || COUNTS_PER_SEC % profhz) {
    239 		printf("cannot get %d Hz profclock; using %d Hz\n", profhz,
    240 		    stathz);
    241 		profhz = stathz;
    242 	}
    243 
    244 	intvl = COUNTS_PER_SEC / hz;
    245 	statint = COUNTS_PER_SEC / stathz;
    246 	profint = COUNTS_PER_SEC / profhz;
    247 	minint = statint / 2 + 100;
    248 	while (statvar > minint)
    249 		statvar >>= 1;
    250 
    251 	/* Adjust interval counts, per note above.  */
    252 	intvl--;
    253 	statint--;
    254 	profint--;
    255 
    256 	/* Calculate the base reload values.  */
    257 	statmin = statint - (statvar >> 1);
    258 	profmin = profint - (statvar >> 1);
    259 	timer2min = statmin;
    260 	statprev = statint;
    261 
    262 	/* Report the clock frequencies */
    263 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    264 
    265 	/* Setup timer 1 and claim interrupt */
    266 	ifpga_sc->sc_clockintr = ifpga_intr_establish(IFPGA_TIMER1_IRQ,
    267 	    IPL_CLOCK, clockhandler, 0);
    268 	if (ifpga_sc->sc_clockintr == NULL)
    269 		panic("%s: Cannot install timer 1 interrupt handler",
    270 		    ifpga_sc->sc_dev.dv_xname);
    271 
    272 	ifpga_sc->sc_clock_count
    273 	    = load_timer(IFPGA_TIMER1_BASE, intvl);
    274 
    275 	/*
    276 	 * Use ticks per 256us for accuracy since ticks per us is often
    277 	 * fractional e.g. @ 66MHz
    278 	 */
    279 	ifpga_sc->sc_clock_ticks_per_256us =
    280 	    ((((ifpga_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
    281 
    282 	clock_started = 1;
    283 
    284 	/* Set up timer 2 as statclk/profclk. */
    285 	ifpga_sc->sc_statclockintr = ifpga_intr_establish(IFPGA_TIMER2_IRQ,
    286 	    IPL_HIGH, statclockhandler, 0);
    287 	if (ifpga_sc->sc_statclockintr == NULL)
    288 		panic("%s: Cannot install timer 2 interrupt handler",
    289 		    ifpga_sc->sc_dev.dv_xname);
    290 	load_timer(IFPGA_TIMER2_BASE, statint);
    291 }
    292 
    293 
    294 /*
    295  * void microtime(struct timeval *tvp)
    296  *
    297  * Fill in the specified timeval struct with the current time
    298  * accurate to the microsecond.
    299  */
    300 
    301 void
    302 microtime(struct timeval *tvp)
    303 {
    304 	int s;
    305 	int tm;
    306 	int deltatm;
    307 	static struct timeval oldtv;
    308 
    309 	if (ifpga_sc == NULL || ifpga_sc->sc_clock_count == 0)
    310 		return;
    311 
    312 	s = splhigh();
    313 
    314 	tm = getclock();
    315 
    316 	deltatm = ifpga_sc->sc_clock_count - tm;
    317 
    318 #ifdef DIAGNOSTIC
    319 	if (deltatm < 0)
    320 		panic("opps deltatm < 0 tm=%d deltatm=%d", tm, deltatm);
    321 #endif
    322 
    323 	/* Fill in the timeval struct */
    324 	*tvp = time;
    325 	tvp->tv_usec += ((deltatm << 8) / ifpga_sc->sc_clock_ticks_per_256us);
    326 
    327 	/* Make sure the micro seconds don't overflow. */
    328 	while (tvp->tv_usec >= 1000000) {
    329 		tvp->tv_usec -= 1000000;
    330 		++tvp->tv_sec;
    331 	}
    332 
    333 	/* Make sure the time has advanced. */
    334 	if (tvp->tv_sec == oldtv.tv_sec &&
    335 	    tvp->tv_usec <= oldtv.tv_usec) {
    336 		tvp->tv_usec = oldtv.tv_usec + 1;
    337 		if (tvp->tv_usec >= 1000000) {
    338 			tvp->tv_usec -= 1000000;
    339 			++tvp->tv_sec;
    340 		}
    341 	}
    342 
    343 	oldtv = *tvp;
    344 	(void)splx(s);
    345 }
    346 
    347 /*
    348  * Estimated loop for n microseconds
    349  */
    350 
    351 /* Need to re-write this to use the timers */
    352 
    353 /* One day soon I will actually do this */
    354 
    355 int delaycount = 50;
    356 
    357 void
    358 delay(u_int n)
    359 {
    360 	if (clock_started) {
    361 		u_int starttime;
    362 		u_int curtime;
    363 		u_int delta = 0;
    364 		u_int count_max = ifpga_sc->sc_clock_count;
    365 
    366 		starttime = getclock();
    367 
    368 		n *= IFPGA_TIMER1_FREQ / 1000000;
    369 
    370 		do {
    371 			n -= delta;
    372 			curtime = getclock();
    373 			delta = curtime - starttime;
    374 			if (curtime < starttime)
    375 				delta += count_max;
    376 			starttime = curtime;
    377 		} while (n > delta);
    378 	} else {
    379 		volatile u_int i;
    380 
    381 		if (n == 0) return;
    382 		while (n-- > 0) {
    383 			/* XXX - Seriously gross hack */
    384 			if (cputype == CPU_ID_SA110)
    385 				for (i = delaycount; --i;)
    386 					;
    387 			else
    388 				for (i = 8; --i;)
    389 					;
    390 		}
    391 	}
    392 }
    393