Home | History | Annotate | Line # | Download | only in ofw
ofwgencfg_clock.c revision 1.7
      1  1.7     matt /*	$NetBSD: ofwgencfg_clock.c,v 1.7 2007/03/08 20:48:39 matt 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.4    lukem 
     38  1.4    lukem #include <sys/cdefs.h>
     39  1.7     matt __KERNEL_RCSID(0, "$NetBSD: ofwgencfg_clock.c,v 1.7 2007/03/08 20:48:39 matt Exp $");
     40  1.1  thorpej 
     41  1.1  thorpej #include <sys/types.h>
     42  1.1  thorpej #include <sys/param.h>
     43  1.1  thorpej #include <sys/systm.h>
     44  1.1  thorpej #include <sys/kernel.h>
     45  1.1  thorpej #include <sys/time.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <machine/intr.h>
     48  1.7     matt #include <machine/irqhandler.h>
     49  1.1  thorpej #include <arm/cpufunc.h>
     50  1.1  thorpej #include <machine/cpu.h>
     51  1.1  thorpej #include <machine/ofw.h>
     52  1.1  thorpej 
     53  1.1  thorpej static void *clockirq;
     54  1.1  thorpej 
     55  1.1  thorpej 
     56  1.1  thorpej /*
     57  1.1  thorpej  * int clockhandler(struct clockframe *frame)
     58  1.1  thorpej  *
     59  1.1  thorpej  * Function called by timer 0 interrupts. This just calls
     60  1.1  thorpej  * hardclock(). Eventually the irqhandler can call hardclock() directly
     61  1.1  thorpej  * but for now we use this function so that we can debug IRQ's
     62  1.1  thorpej  */
     63  1.5  thorpej 
     64  1.5  thorpej static int
     65  1.5  thorpej clockhandler(struct clockframe *frame)
     66  1.1  thorpej {
     67  1.1  thorpej 
     68  1.1  thorpej 	hardclock(frame);
     69  1.1  thorpej 	return(0);	/* Pass the interrupt on down the chain */
     70  1.1  thorpej }
     71  1.1  thorpej 
     72  1.5  thorpej #if 0
     73  1.1  thorpej /*
     74  1.1  thorpej  * int statclockhandler(struct clockframe *frame)
     75  1.1  thorpej  *
     76  1.1  thorpej  * Function called by timer 1 interrupts. This just calls
     77  1.1  thorpej  * statclock(). Eventually the irqhandler can call statclock() directly
     78  1.1  thorpej  * but for now we use this function so that we can debug IRQ's
     79  1.1  thorpej  */
     80  1.5  thorpej 
     81  1.1  thorpej int
     82  1.1  thorpej statclockhandler(frame)
     83  1.1  thorpej 	struct clockframe *frame;
     84  1.1  thorpej {
     85  1.1  thorpej 
     86  1.1  thorpej 	statclock(frame);
     87  1.1  thorpej 	return(0);	/* Pass the interrupt on down the chain */
     88  1.1  thorpej }
     89  1.5  thorpej #endif
     90  1.1  thorpej 
     91  1.1  thorpej /*
     92  1.1  thorpej  * void setstatclockrate(int hz)
     93  1.1  thorpej  *
     94  1.1  thorpej  * Set the stat clock rate. The stat clock uses timer1
     95  1.1  thorpej  */
     96  1.1  thorpej 
     97  1.1  thorpej void
     98  1.7     matt setstatclockrate(int arg)
     99  1.1  thorpej {
    100  1.1  thorpej #ifdef	OFWGENCFG
    101  1.1  thorpej 	printf("Not setting statclock: OFW generic has only one clock.\n");
    102  1.1  thorpej #endif
    103  1.1  thorpej }
    104  1.1  thorpej 
    105  1.1  thorpej 
    106  1.1  thorpej /*
    107  1.1  thorpej  * void cpu_initclocks(void)
    108  1.1  thorpej  *
    109  1.1  thorpej  * Initialise the clocks.
    110  1.1  thorpej  * This sets up the two timers in the IOMD and installs the IRQ handlers
    111  1.1  thorpej  *
    112  1.1  thorpej  * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
    113  1.1  thorpej  */
    114  1.1  thorpej 
    115  1.1  thorpej void
    116  1.1  thorpej cpu_initclocks()
    117  1.1  thorpej {
    118  1.1  thorpej 	/*
    119  1.1  thorpej 	 * Load timer 0 with count down value
    120  1.1  thorpej 	 * This timer generates 100Hz interrupts for the system clock
    121  1.1  thorpej 	 */
    122  1.1  thorpej 
    123  1.1  thorpej 	printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
    124  1.1  thorpej 
    125  1.7     matt         clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK,
    126  1.7     matt             (int (*)(void *))clockhandler, 0, "clock", "hard intr");
    127  1.1  thorpej         if (clockirq == NULL)
    128  1.3   provos                 panic("Cannot installer timer 0 IRQ handler");
    129  1.1  thorpej 
    130  1.1  thorpej 	/* Notify callback handler that it can start processing ticks. */
    131  1.1  thorpej 	ofw_handleticks = 1;
    132  1.1  thorpej 
    133  1.1  thorpej 	if (stathz) {
    134  1.1  thorpej 	    printf("Not installing statclock: OFW generic has only one clock.\n");
    135  1.1  thorpej 	}
    136  1.1  thorpej }
    137  1.1  thorpej 
    138  1.1  thorpej 
    139  1.1  thorpej /*
    140  1.1  thorpej  * void microtime(struct timeval *tvp)
    141  1.1  thorpej  *
    142  1.1  thorpej  * Fill in the specified timeval struct with the current time
    143  1.1  thorpej  * accurate to the microsecond.
    144  1.1  thorpej  */
    145  1.1  thorpej 
    146  1.1  thorpej void
    147  1.1  thorpej microtime(tvp)
    148  1.1  thorpej 	struct timeval *tvp;
    149  1.1  thorpej {
    150  1.1  thorpej 	int s;
    151  1.1  thorpej 	static struct timeval oldtv;
    152  1.1  thorpej 
    153  1.1  thorpej 	s = splhigh();
    154  1.1  thorpej 
    155  1.1  thorpej 	/* Fill in the timeval struct */
    156  1.1  thorpej 
    157  1.1  thorpej 	*tvp = time;
    158  1.1  thorpej 
    159  1.1  thorpej 	/* Make sure the micro seconds don't overflow. */
    160  1.1  thorpej 
    161  1.1  thorpej 	while (tvp->tv_usec >= 1000000) {
    162  1.1  thorpej 		tvp->tv_usec -= 1000000;
    163  1.1  thorpej 		++tvp->tv_sec;
    164  1.1  thorpej 	}
    165  1.1  thorpej 
    166  1.1  thorpej 	/* Make sure the time has advanced. */
    167  1.1  thorpej 
    168  1.1  thorpej 	if (tvp->tv_sec == oldtv.tv_sec &&
    169  1.1  thorpej 	    tvp->tv_usec <= oldtv.tv_usec) {
    170  1.1  thorpej 		tvp->tv_usec = oldtv.tv_usec + 1;
    171  1.1  thorpej 		if (tvp->tv_usec >= 1000000) {
    172  1.1  thorpej 			tvp->tv_usec -= 1000000;
    173  1.1  thorpej 			++tvp->tv_sec;
    174  1.1  thorpej 		}
    175  1.1  thorpej 	}
    176  1.1  thorpej 
    177  1.1  thorpej 
    178  1.1  thorpej 	oldtv = *tvp;
    179  1.1  thorpej 	(void)splx(s);
    180  1.1  thorpej }
    181  1.1  thorpej 
    182  1.1  thorpej /*
    183  1.1  thorpej  * Estimated loop for n microseconds
    184  1.1  thorpej  */
    185  1.1  thorpej 
    186  1.1  thorpej /* Need to re-write this to use the timers */
    187  1.1  thorpej 
    188  1.1  thorpej /* One day soon I will actually do this */
    189  1.1  thorpej 
    190  1.1  thorpej int delaycount = 50;
    191  1.1  thorpej 
    192  1.1  thorpej void
    193  1.1  thorpej delay(n)
    194  1.1  thorpej 	u_int n;
    195  1.1  thorpej {
    196  1.1  thorpej 	u_int i;
    197  1.1  thorpej 
    198  1.1  thorpej 	if (n == 0) return;
    199  1.2  mycroft 	while (n-- > 0) {
    200  1.1  thorpej 		if (cputype == CPU_ID_SA110)	/* XXX - Seriously gross hack */
    201  1.1  thorpej 			for (i = delaycount; --i;);
    202  1.1  thorpej 		else
    203  1.1  thorpej 			for (i = 8; --i;);
    204  1.1  thorpej 	}
    205  1.1  thorpej }
    206