Home | History | Annotate | Line # | Download | only in ofw
ofwgencfg_clock.c revision 1.3
      1  1.3   provos /*	$NetBSD: ofwgencfg_clock.c,v 1.3 2002/09/27 15:35:49 provos Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright 1997
      5  1.1  thorpej  * Digital Equipment Corporation. All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This software is furnished under license and may be used and
      8  1.1  thorpej  * copied only in accordance with the following terms and conditions.
      9  1.1  thorpej  * Subject to these conditions, you may download, copy, install,
     10  1.1  thorpej  * use, modify and distribute this software in source and/or binary
     11  1.1  thorpej  * form. No title or ownership is transferred hereby.
     12  1.1  thorpej  *
     13  1.1  thorpej  * 1) Any source code used, modified or distributed must reproduce
     14  1.1  thorpej  *    and retain this copyright notice and list of conditions as
     15  1.1  thorpej  *    they appear in the source file.
     16  1.1  thorpej  *
     17  1.1  thorpej  * 2) No right is granted to use any trade name, trademark, or logo of
     18  1.1  thorpej  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  1.1  thorpej  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  1.1  thorpej  *    Corporation may be used to endorse or promote products derived
     21  1.1  thorpej  *    from this software without the prior written permission of
     22  1.1  thorpej  *    Digital Equipment Corporation.
     23  1.1  thorpej  *
     24  1.1  thorpej  * 3) This software is provided "AS-IS" and any express or implied
     25  1.1  thorpej  *    warranties, including but not limited to, any implied warranties
     26  1.1  thorpej  *    of merchantability, fitness for a particular purpose, or
     27  1.1  thorpej  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  1.1  thorpej  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  1.1  thorpej  *    shall not be liable for special, indirect, consequential, or
     30  1.1  thorpej  *    incidental damages or damages for lost profits, loss of
     31  1.1  thorpej  *    revenue or loss of use, whether such damages arise in contract,
     32  1.1  thorpej  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  1.1  thorpej  *    even if advised of the possibility of such damage.
     34  1.1  thorpej  */
     35  1.1  thorpej 
     36  1.1  thorpej /* Include header files */
     37  1.1  thorpej 
     38  1.1  thorpej #include <sys/types.h>
     39  1.1  thorpej #include <sys/param.h>
     40  1.1  thorpej #include <sys/systm.h>
     41  1.1  thorpej #include <sys/kernel.h>
     42  1.1  thorpej #include <sys/time.h>
     43  1.1  thorpej 
     44  1.1  thorpej #include <machine/intr.h>
     45  1.1  thorpej #include <arm/cpufunc.h>
     46  1.1  thorpej #include <machine/cpu.h>
     47  1.1  thorpej #include <machine/ofw.h>
     48  1.1  thorpej 
     49  1.1  thorpej static void *clockirq;
     50  1.1  thorpej 
     51  1.1  thorpej 
     52  1.1  thorpej /*
     53  1.1  thorpej  * int clockhandler(struct clockframe *frame)
     54  1.1  thorpej  *
     55  1.1  thorpej  * Function called by timer 0 interrupts. This just calls
     56  1.1  thorpej  * hardclock(). Eventually the irqhandler can call hardclock() directly
     57  1.1  thorpej  * but for now we use this function so that we can debug IRQ's
     58  1.1  thorpej  */
     59  1.1  thorpej 
     60  1.1  thorpej int
     61  1.1  thorpej clockhandler(frame)
     62  1.1  thorpej 	struct clockframe *frame;
     63  1.1  thorpej {
     64  1.1  thorpej 
     65  1.1  thorpej 	hardclock(frame);
     66  1.1  thorpej 	return(0);	/* Pass the interrupt on down the chain */
     67  1.1  thorpej }
     68  1.1  thorpej 
     69  1.1  thorpej 
     70  1.1  thorpej /*
     71  1.1  thorpej  * int statclockhandler(struct clockframe *frame)
     72  1.1  thorpej  *
     73  1.1  thorpej  * Function called by timer 1 interrupts. This just calls
     74  1.1  thorpej  * statclock(). Eventually the irqhandler can call statclock() directly
     75  1.1  thorpej  * but for now we use this function so that we can debug IRQ's
     76  1.1  thorpej  */
     77  1.1  thorpej 
     78  1.1  thorpej int
     79  1.1  thorpej statclockhandler(frame)
     80  1.1  thorpej 	struct clockframe *frame;
     81  1.1  thorpej {
     82  1.1  thorpej 
     83  1.1  thorpej 	statclock(frame);
     84  1.1  thorpej 	return(0);	/* Pass the interrupt on down the chain */
     85  1.1  thorpej }
     86  1.1  thorpej 
     87  1.1  thorpej 
     88  1.1  thorpej /*
     89  1.1  thorpej  * void setstatclockrate(int hz)
     90  1.1  thorpej  *
     91  1.1  thorpej  * Set the stat clock rate. The stat clock uses timer1
     92  1.1  thorpej  */
     93  1.1  thorpej 
     94  1.1  thorpej void
     95  1.1  thorpej setstatclockrate(hz)
     96  1.1  thorpej 	int hz;
     97  1.1  thorpej {
     98  1.1  thorpej 
     99  1.1  thorpej #ifdef	OFWGENCFG
    100  1.1  thorpej 	printf("Not setting statclock: OFW generic has only one clock.\n");
    101  1.1  thorpej #endif
    102  1.1  thorpej }
    103  1.1  thorpej 
    104  1.1  thorpej 
    105  1.1  thorpej /*
    106  1.1  thorpej  * void cpu_initclocks(void)
    107  1.1  thorpej  *
    108  1.1  thorpej  * Initialise the clocks.
    109  1.1  thorpej  * This sets up the two timers in the IOMD and installs the IRQ handlers
    110  1.1  thorpej  *
    111  1.1  thorpej  * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
    112  1.1  thorpej  */
    113  1.1  thorpej 
    114  1.1  thorpej void
    115  1.1  thorpej cpu_initclocks()
    116  1.1  thorpej {
    117  1.1  thorpej 	/*
    118  1.1  thorpej 	 * Load timer 0 with count down value
    119  1.1  thorpej 	 * This timer generates 100Hz interrupts for the system clock
    120  1.1  thorpej 	 */
    121  1.1  thorpej 
    122  1.1  thorpej 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    123  1.1  thorpej 
    124  1.1  thorpej         clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
    125  1.1  thorpej             clockhandler, 0);
    126  1.1  thorpej         if (clockirq == NULL)
    127  1.3   provos                 panic("Cannot installer timer 0 IRQ handler");
    128  1.1  thorpej 
    129  1.1  thorpej 	/* Notify callback handler that it can start processing ticks. */
    130  1.1  thorpej 	ofw_handleticks = 1;
    131  1.1  thorpej 
    132  1.1  thorpej 	if (stathz) {
    133  1.1  thorpej 	    printf("Not installing statclock: OFW generic has only one clock.\n");
    134  1.1  thorpej 	}
    135  1.1  thorpej }
    136  1.1  thorpej 
    137  1.1  thorpej 
    138  1.1  thorpej /*
    139  1.1  thorpej  * void microtime(struct timeval *tvp)
    140  1.1  thorpej  *
    141  1.1  thorpej  * Fill in the specified timeval struct with the current time
    142  1.1  thorpej  * accurate to the microsecond.
    143  1.1  thorpej  */
    144  1.1  thorpej 
    145  1.1  thorpej void
    146  1.1  thorpej microtime(tvp)
    147  1.1  thorpej 	struct timeval *tvp;
    148  1.1  thorpej {
    149  1.1  thorpej 	int s;
    150  1.1  thorpej 	static struct timeval oldtv;
    151  1.1  thorpej 
    152  1.1  thorpej 	s = splhigh();
    153  1.1  thorpej 
    154  1.1  thorpej 	/* Fill in the timeval struct */
    155  1.1  thorpej 
    156  1.1  thorpej 	*tvp = time;
    157  1.1  thorpej 
    158  1.1  thorpej 	/* Make sure the micro seconds don't overflow. */
    159  1.1  thorpej 
    160  1.1  thorpej 	while (tvp->tv_usec >= 1000000) {
    161  1.1  thorpej 		tvp->tv_usec -= 1000000;
    162  1.1  thorpej 		++tvp->tv_sec;
    163  1.1  thorpej 	}
    164  1.1  thorpej 
    165  1.1  thorpej 	/* Make sure the time has advanced. */
    166  1.1  thorpej 
    167  1.1  thorpej 	if (tvp->tv_sec == oldtv.tv_sec &&
    168  1.1  thorpej 	    tvp->tv_usec <= oldtv.tv_usec) {
    169  1.1  thorpej 		tvp->tv_usec = oldtv.tv_usec + 1;
    170  1.1  thorpej 		if (tvp->tv_usec >= 1000000) {
    171  1.1  thorpej 			tvp->tv_usec -= 1000000;
    172  1.1  thorpej 			++tvp->tv_sec;
    173  1.1  thorpej 		}
    174  1.1  thorpej 	}
    175  1.1  thorpej 
    176  1.1  thorpej 
    177  1.1  thorpej 	oldtv = *tvp;
    178  1.1  thorpej 	(void)splx(s);
    179  1.1  thorpej }
    180  1.1  thorpej 
    181  1.1  thorpej /*
    182  1.1  thorpej  * Estimated loop for n microseconds
    183  1.1  thorpej  */
    184  1.1  thorpej 
    185  1.1  thorpej /* Need to re-write this to use the timers */
    186  1.1  thorpej 
    187  1.1  thorpej /* One day soon I will actually do this */
    188  1.1  thorpej 
    189  1.1  thorpej int delaycount = 50;
    190  1.1  thorpej 
    191  1.1  thorpej void
    192  1.1  thorpej delay(n)
    193  1.1  thorpej 	u_int n;
    194  1.1  thorpej {
    195  1.1  thorpej 	u_int i;
    196  1.1  thorpej 
    197  1.1  thorpej 	if (n == 0) return;
    198  1.2  mycroft 	while (n-- > 0) {
    199  1.1  thorpej 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    200  1.1  thorpej 			for (i = delaycount; --i;);
    201  1.1  thorpej 		else
    202  1.1  thorpej 			for (i = 8; --i;);
    203  1.1  thorpej 	}
    204  1.1  thorpej }
    205