Home | History | Annotate | Line # | Download | only in footbridge
footbridge_clock.c revision 1.10
      1  1.10  thorpej /*	$NetBSD: footbridge_clock.c,v 1.10 2002/10/02 05:02:30 thorpej Exp $	*/
      2   1.1    chris 
      3   1.1    chris /*
      4   1.1    chris  * Copyright (c) 1997 Mark Brinicombe.
      5   1.1    chris  * Copyright (c) 1997 Causality Limited.
      6   1.1    chris  * All rights reserved.
      7   1.1    chris  *
      8   1.1    chris  * Redistribution and use in source and binary forms, with or without
      9   1.1    chris  * modification, are permitted provided that the following conditions
     10   1.1    chris  * are met:
     11   1.1    chris  * 1. Redistributions of source code must retain the above copyright
     12   1.1    chris  *    notice, this list of conditions and the following disclaimer.
     13   1.1    chris  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    chris  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    chris  *    documentation and/or other materials provided with the distribution.
     16   1.1    chris  * 3. All advertising materials mentioning features or use of this software
     17   1.1    chris  *    must display the following acknowledgement:
     18   1.1    chris  *	This product includes software developed by Mark Brinicombe
     19   1.1    chris  *	for the NetBSD Project.
     20   1.1    chris  * 4. The name of the company nor the name of the author may be used to
     21   1.1    chris  *    endorse or promote products derived from this software without specific
     22   1.1    chris  *    prior written permission.
     23   1.1    chris  *
     24   1.1    chris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25   1.1    chris  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26   1.1    chris  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27   1.1    chris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     28   1.1    chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     29   1.1    chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     30   1.1    chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1    chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1    chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1    chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1    chris  * SUCH DAMAGE.
     35   1.1    chris  */
     36   1.1    chris 
     37   1.1    chris /* Include header files */
     38   1.1    chris 
     39   1.1    chris #include <sys/types.h>
     40   1.1    chris #include <sys/param.h>
     41   1.1    chris #include <sys/systm.h>
     42   1.1    chris #include <sys/kernel.h>
     43   1.1    chris #include <sys/time.h>
     44   1.1    chris #include <sys/device.h>
     45   1.1    chris 
     46   1.2     matt #include <machine/intr.h>
     47   1.3  thorpej 
     48   1.3  thorpej #include <arm/cpufunc.h>
     49   1.3  thorpej 
     50   1.1    chris #include <arm/footbridge/dc21285reg.h>
     51   1.1    chris #include <arm/footbridge/footbridgevar.h>
     52   1.6    chris #include <arm/footbridge/footbridge.h>
     53   1.1    chris 
     54   1.1    chris extern struct footbridge_softc *clock_sc;
     55   1.1    chris extern u_int dc21285_fclk;
     56   1.1    chris 
     57   1.4    chris int clockhandler __P((void *));
     58   1.4    chris int statclockhandler __P((void *));
     59   1.4    chris static int load_timer __P((int, int));
     60   1.4    chris 
     61   1.4    chris 
     62   1.1    chris #if 0
     63   1.1    chris static int clockmatch	__P((struct device *parent, struct cfdata *cf, void *aux));
     64   1.1    chris static void clockattach	__P((struct device *parent, struct device *self, void *aux));
     65   1.1    chris 
     66  1.10  thorpej CFATTACH_DECL(footbridge_clock, sizeof(struct clock_softc),
     67  1.10  thorpej     clockmatch, clockattach, NULL, NULL);
     68   1.1    chris 
     69   1.1    chris /*
     70   1.1    chris  * int clockmatch(struct device *parent, void *match, void *aux)
     71   1.1    chris  *
     72   1.1    chris  * Just return ok for this if it is device 0
     73   1.1    chris  */
     74   1.1    chris 
     75   1.1    chris static int
     76   1.1    chris clockmatch(parent, cf, aux)
     77   1.1    chris 	struct device *parent;
     78   1.1    chris 	struct cfdata *cf;
     79   1.1    chris 	void *aux;
     80   1.1    chris {
     81   1.1    chris 	union footbridge_attach_args *fba = aux;
     82   1.1    chris 
     83   1.1    chris 	if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
     84   1.1    chris 		return(1);
     85   1.1    chris 	return(0);
     86   1.1    chris }
     87   1.1    chris 
     88   1.1    chris 
     89   1.1    chris /*
     90   1.1    chris  * void clockattach(struct device *parent, struct device *dev, void *aux)
     91   1.1    chris  *
     92   1.1    chris  */
     93   1.1    chris 
     94   1.1    chris static void
     95   1.1    chris clockattach(parent, self, aux)
     96   1.1    chris 	struct device *parent;
     97   1.1    chris 	struct device *self;
     98   1.1    chris 	void *aux;
     99   1.1    chris {
    100   1.1    chris 	struct clock_softc *sc = (struct clock_softc *)self;
    101   1.1    chris 	union footbridge_attach_args *fba = aux;
    102   1.1    chris 
    103   1.1    chris 	sc->sc_iot = fba->fba_ca.ca_iot;
    104   1.1    chris 	sc->sc_ioh = fba->fba_ca.ca_ioh;
    105   1.1    chris 
    106   1.1    chris 	clock_sc = sc;
    107   1.1    chris 
    108   1.1    chris 	/* Cannot do anything until cpu_initclocks() has been called */
    109   1.1    chris 
    110   1.1    chris 	printf("\n");
    111   1.1    chris }
    112   1.1    chris #endif
    113   1.1    chris 
    114   1.1    chris /*
    115   1.1    chris  * int clockhandler(struct clockframe *frame)
    116   1.1    chris  *
    117   1.1    chris  * Function called by timer 1 interrupts.
    118   1.1    chris  * This just clears the interrupt condition and calls hardclock().
    119   1.1    chris  */
    120   1.1    chris 
    121   1.1    chris int
    122   1.4    chris clockhandler(aframe)
    123   1.4    chris 	void *aframe;
    124   1.1    chris {
    125   1.4    chris 	struct clockframe *frame = aframe;
    126   1.1    chris 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    127   1.1    chris 	    TIMER_1_CLEAR, 0);
    128   1.1    chris 	hardclock(frame);
    129   1.1    chris 	return(0);	/* Pass the interrupt on down the chain */
    130   1.1    chris }
    131   1.1    chris 
    132   1.1    chris 
    133   1.1    chris /*
    134   1.1    chris  * int statclockhandler(struct clockframe *frame)
    135   1.1    chris  *
    136   1.1    chris  * Function called by timer 2 interrupts.
    137   1.1    chris  * This just clears the interrupt condition and calls statclock().
    138   1.1    chris  */
    139   1.1    chris 
    140   1.1    chris int
    141   1.4    chris statclockhandler(aframe)
    142   1.4    chris 	void *aframe;
    143   1.1    chris {
    144   1.4    chris 	struct clockframe *frame = aframe;
    145   1.1    chris 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    146   1.1    chris 	    TIMER_2_CLEAR, 0);
    147   1.1    chris 	statclock(frame);
    148   1.1    chris 	return(0);	/* Pass the interrupt on down the chain */
    149   1.1    chris }
    150   1.1    chris 
    151   1.1    chris static int
    152   1.1    chris load_timer(base, hz)
    153   1.1    chris 	int base;
    154   1.1    chris 	int hz;
    155   1.1    chris {
    156   1.1    chris 	unsigned int timer_count;
    157   1.1    chris 	int control;
    158   1.1    chris 
    159   1.1    chris 	timer_count = dc21285_fclk / hz;
    160   1.1    chris 	if (timer_count > TIMER_MAX * 16) {
    161   1.1    chris 		control = TIMER_FCLK_256;
    162   1.1    chris 		timer_count >>= 8;
    163   1.1    chris 	} else if (timer_count > TIMER_MAX) {
    164   1.1    chris 		control = TIMER_FCLK_16;
    165   1.1    chris 		timer_count >>= 4;
    166   1.1    chris 	} else
    167   1.1    chris 		control = TIMER_FCLK;
    168   1.1    chris 
    169   1.1    chris 	control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
    170   1.1    chris 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    171   1.1    chris 	    base + TIMER_LOAD, timer_count);
    172   1.1    chris 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    173   1.1    chris 	    base + TIMER_CONTROL, control);
    174   1.1    chris 	bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    175   1.1    chris 	    base + TIMER_CLEAR, 0);
    176   1.1    chris 	return(timer_count);
    177   1.1    chris }
    178   1.1    chris 
    179   1.1    chris /*
    180   1.1    chris  * void setstatclockrate(int hz)
    181   1.1    chris  *
    182   1.1    chris  * Set the stat clock rate. The stat clock uses timer2
    183   1.1    chris  */
    184   1.1    chris 
    185   1.1    chris void
    186   1.1    chris setstatclockrate(hz)
    187   1.1    chris 	int hz;
    188   1.1    chris {
    189   1.1    chris 
    190   1.1    chris 	clock_sc->sc_statclock_count = load_timer(TIMER_2_BASE, hz);
    191   1.1    chris }
    192   1.1    chris 
    193   1.1    chris /*
    194   1.1    chris  * void cpu_initclocks(void)
    195   1.1    chris  *
    196   1.1    chris  * Initialise the clocks.
    197   1.1    chris  *
    198   1.1    chris  * Timer 1 is used for the main system clock (hardclock)
    199   1.1    chris  * Timer 2 is used for the statistics clock (statclock)
    200   1.1    chris  */
    201   1.1    chris 
    202   1.1    chris void
    203   1.1    chris cpu_initclocks()
    204   1.1    chris {
    205   1.9    chris 	/* stathz and profhz should be set to something, we have the timer */
    206   1.9    chris 	if (stathz == 0)
    207   1.9    chris 		stathz = 64;
    208   1.9    chris 
    209   1.9    chris 	if (profhz == 0)
    210   1.9    chris 		profhz = stathz * 5;
    211   1.1    chris 
    212   1.1    chris 	/* Report the clock frequencies */
    213   1.1    chris 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    214   1.1    chris 
    215   1.1    chris 	/* Setup timer 1 and claim interrupt */
    216   1.1    chris 	clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
    217   1.1    chris 
    218   1.1    chris 	/*
    219   1.1    chris 	 * Use ticks per 256us for accuracy since ticks per us is often
    220   1.1    chris 	 * fractional e.g. @ 66MHz
    221   1.1    chris 	 */
    222   1.1    chris 	clock_sc->sc_clock_ticks_per_256us =
    223   1.1    chris 	    ((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
    224   1.1    chris 	clock_sc->sc_clockintr = intr_claim(IRQ_TIMER_1, IPL_CLOCK,
    225   1.1    chris 	    "tmr1 hard clk", clockhandler, 0);
    226   1.1    chris 
    227   1.1    chris 	if (clock_sc->sc_clockintr == NULL)
    228   1.7   provos 		panic("%s: Cannot install timer 1 interrupt handler",
    229   1.1    chris 		    clock_sc->sc_dev.dv_xname);
    230   1.1    chris 
    231   1.1    chris 	/* If stathz is non-zero then setup the stat clock */
    232   1.1    chris 	if (stathz) {
    233   1.1    chris 		/* Setup timer 2 and claim interrupt */
    234   1.1    chris 		setstatclockrate(stathz);
    235   1.9    chris        		clock_sc->sc_statclockintr = intr_claim(IRQ_TIMER_2, IPL_STATCLOCK,
    236   1.1    chris        		    "tmr2 stat clk", statclockhandler, 0);
    237   1.1    chris 		if (clock_sc->sc_statclockintr == NULL)
    238   1.7   provos 			panic("%s: Cannot install timer 2 interrupt handler",
    239   1.1    chris 			    clock_sc->sc_dev.dv_xname);
    240   1.1    chris 	}
    241   1.1    chris }
    242   1.1    chris 
    243   1.1    chris 
    244   1.1    chris /*
    245   1.1    chris  * void microtime(struct timeval *tvp)
    246   1.1    chris  *
    247   1.1    chris  * Fill in the specified timeval struct with the current time
    248   1.1    chris  * accurate to the microsecond.
    249   1.1    chris  */
    250   1.1    chris 
    251   1.1    chris void
    252   1.1    chris microtime(tvp)
    253   1.1    chris 	struct timeval *tvp;
    254   1.1    chris {
    255   1.1    chris 	int s;
    256   1.1    chris 	int tm;
    257   1.1    chris 	int deltatm;
    258   1.1    chris 	static struct timeval oldtv;
    259   1.1    chris 
    260   1.1    chris 	if (clock_sc == NULL || clock_sc->sc_clock_count == 0)
    261   1.1    chris 		return;
    262   1.1    chris 
    263   1.1    chris 	s = splhigh();
    264   1.1    chris 
    265   1.1    chris 	tm = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    266   1.1    chris 	    TIMER_1_VALUE);
    267   1.1    chris 
    268   1.1    chris 	deltatm = clock_sc->sc_clock_count - tm;
    269   1.1    chris 
    270   1.1    chris #ifdef DIAGNOSTIC
    271   1.1    chris 	if (deltatm < 0)
    272   1.7   provos 		panic("opps deltatm < 0 tm=%d deltatm=%d", tm, deltatm);
    273   1.1    chris #endif
    274   1.1    chris 
    275   1.1    chris 	/* Fill in the timeval struct */
    276   1.1    chris 	*tvp = time;
    277   1.1    chris 	tvp->tv_usec += ((deltatm << 8) / clock_sc->sc_clock_ticks_per_256us);
    278   1.1    chris 
    279   1.1    chris 	/* Make sure the micro seconds don't overflow. */
    280   1.1    chris 	while (tvp->tv_usec >= 1000000) {
    281   1.1    chris 		tvp->tv_usec -= 1000000;
    282   1.1    chris 		++tvp->tv_sec;
    283   1.1    chris 	}
    284   1.1    chris 
    285   1.1    chris 	/* Make sure the time has advanced. */
    286   1.1    chris 	if (tvp->tv_sec == oldtv.tv_sec &&
    287   1.1    chris 	    tvp->tv_usec <= oldtv.tv_usec) {
    288   1.1    chris 		tvp->tv_usec = oldtv.tv_usec + 1;
    289   1.1    chris 		if (tvp->tv_usec >= 1000000) {
    290   1.1    chris 			tvp->tv_usec -= 1000000;
    291   1.1    chris 			++tvp->tv_sec;
    292   1.1    chris 		}
    293   1.1    chris 	}
    294   1.1    chris 
    295   1.1    chris 	oldtv = *tvp;
    296   1.1    chris 	(void)splx(s);
    297   1.1    chris }
    298   1.1    chris 
    299   1.1    chris /*
    300   1.6    chris  * Use a timer to track microseconds, if the footbridge hasn't been setup we
    301   1.6    chris  * rely on an estimated loop, however footbridge is attached very early on.
    302   1.1    chris  */
    303   1.1    chris 
    304   1.6    chris static int delay_clock_count = 0;
    305   1.6    chris static int delay_count_per_usec = 0;
    306   1.1    chris 
    307   1.6    chris void
    308   1.6    chris calibrate_delay(void)
    309   1.6    chris {
    310   1.6    chris      delay_clock_count = load_timer(TIMER_3_BASE, 100);
    311   1.6    chris      delay_count_per_usec = delay_clock_count/10000;
    312   1.6    chris }
    313   1.1    chris 
    314   1.6    chris int delaycount = 500;
    315   1.1    chris 
    316   1.1    chris void
    317   1.1    chris delay(n)
    318   1.1    chris 	u_int n;
    319   1.1    chris {
    320   1.6    chris 	volatile u_int i;
    321   1.6    chris 	uint32_t cur, last, delta, usecs;
    322   1.1    chris 
    323   1.1    chris 	if (n == 0) return;
    324   1.6    chris 
    325   1.6    chris 
    326   1.6    chris 	// not calibrated the timer yet, so try to live with this horrible
    327   1.6    chris 	// loop!
    328   1.6    chris 	if (delay_clock_count == 0)
    329   1.6    chris 	{
    330   1.6    chris 	    while (n-- > 0) {
    331   1.6    chris 		for (i = delaycount; --i;);
    332   1.6    chris 	    }
    333   1.6    chris 	    return;
    334   1.6    chris 	}
    335   1.6    chris 	last = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    336   1.6    chris 		TIMER_3_VALUE);
    337   1.6    chris 
    338   1.6    chris 	delta = usecs = 0;
    339   1.6    chris 
    340   1.6    chris 	while (n > usecs)
    341   1.6    chris 	{
    342   1.6    chris 	    cur = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    343   1.6    chris 		    TIMER_3_VALUE);
    344   1.6    chris 	    if (last < cur)
    345   1.6    chris 		/* timer has wrapped */
    346   1.6    chris 		delta += ((delay_clock_count - cur) + last);
    347   1.6    chris 	    else
    348   1.6    chris 		delta += (last - cur);
    349   1.6    chris 
    350   1.6    chris 	    if (last == 0 && cur == 0)
    351   1.6    chris 	    {
    352   1.6    chris 		/* reset the timer, not sure this is really needed */
    353   1.6    chris 		bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
    354   1.6    chris 			TIMER_3_CLEAR, 0);
    355   1.6    chris 	    }
    356   1.6    chris 	    last = cur;
    357   1.6    chris 
    358   1.6    chris 	    if (delta >= delay_count_per_usec)
    359   1.6    chris 	    {
    360   1.6    chris 		usecs += delta / delay_count_per_usec;
    361   1.6    chris 		delta %= delay_count_per_usec;
    362   1.6    chris 	    }
    363   1.1    chris 	}
    364   1.1    chris }
    365   1.1    chris 
    366   1.1    chris /* End of footbridge_clock.c */
    367