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