Home | History | Annotate | Line # | Download | only in ibm4xx
clock.c revision 1.14.10.2
      1 /*	$NetBSD: clock.c,v 1.14.10.2 2006/05/11 23:26:59 elad Exp $	*/
      2 /*      $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $  */
      3 
      4 /*
      5  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      6  * Copyright (C) 1995, 1996 TooLs GmbH.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.14.10.2 2006/05/11 23:26:59 elad Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 
     42 #include <prop/proplib.h>
     43 
     44 #include <machine/cpu.h>
     45 
     46 #include <powerpc/spr.h>
     47 
     48 /*
     49  * Initially we assume a processor with a bus frequency of 12.5 MHz.
     50  */
     51 static u_long ticks_per_sec;
     52 static u_long ns_per_tick;
     53 static long ticks_per_intr;
     54 static volatile u_long lasttb, lasttb2;
     55 static u_long ticksmissed;
     56 static volatile int tickspending;
     57 
     58 void decr_intr(struct clockframe *);	/* called from trap_subr.S */
     59 void stat_intr(struct clockframe *);	/* called from trap_subr.S */
     60 
     61 #ifdef FAST_STAT_CLOCK
     62 /* Stat clock runs at ~ 1.5 kHz */
     63 #define PERIOD_POWER	17
     64 #define TCR_PERIOD	TCR_FP_2_17
     65 #else
     66 /* Stat clock runs at ~ 95Hz */
     67 #define PERIOD_POWER	21
     68 #define TCR_PERIOD	TCR_FP_2_21
     69 #endif
     70 
     71 
     72 void
     73 stat_intr(struct clockframe *frame)
     74 {
     75 	extern u_long intrcnt[];
     76 
     77 	mtspr(SPR_TSR, TSR_FIS);	/* Clear TSR[FIS] */
     78 	intrcnt[CNT_STATCLOCK]++;
     79   	statclock(frame);
     80 }
     81 
     82 void
     83 decr_intr(struct clockframe *frame)
     84 {
     85 	int pri;
     86 	long tbtick, xticks;
     87 	int nticks;
     88 	extern u_long intrcnt[];
     89 	/*
     90 	 * Check whether we are initialized.
     91 	 */
     92 	if (!ticks_per_intr)
     93 		return;
     94 
     95 	tbtick = mftbl();
     96 	mtspr(SPR_TSR, TSR_PIS);	/* Clear TSR[PIS] */
     97 
     98 	xticks = tbtick - lasttb2;	/* Number of TLB cycles since last exception */
     99 	for (nticks = 0; xticks > ticks_per_intr; nticks++)
    100 		xticks -= ticks_per_intr;
    101 	lasttb2 = tbtick - xticks;
    102 
    103 	intrcnt[CNT_CLOCK]++;
    104 	pri = splclock();
    105 	if (pri & SPL_CLOCK) {
    106 		tickspending += nticks;
    107 		ticksmissed+= nticks;
    108 	} else {
    109 		nticks += tickspending;
    110 		tickspending = 0;
    111 
    112 		/*
    113 		 * lasttb is used during microtime. Set it to the virtual
    114 		 * start of this tick interval.
    115 		 */
    116 		lasttb = lasttb2;
    117 
    118 		/*
    119 		 * Reenable interrupts
    120 		 */
    121 		__asm volatile ("wrteei 1");
    122 
    123 		/*
    124 		 * Do standard timer interrupt stuff.
    125 		 * Do softclock stuff only on the last iteration.
    126 		 */
    127 		frame->pri = pri | SINT_CLOCK;
    128 		while (--nticks > 0)
    129 			hardclock(frame);
    130 		frame->pri = pri;
    131 		hardclock(frame);
    132 	}
    133 	splx(pri);
    134 }
    135 
    136 void
    137 cpu_initclocks(void)
    138 {
    139 
    140 	ticks_per_intr = ticks_per_sec / hz;
    141 	stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
    142 	printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
    143 	lasttb2 = lasttb = mftbl();
    144 	mtspr(SPR_PIT, ticks_per_intr);
    145 	/* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
    146 	mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
    147 }
    148 
    149 void
    150 calc_delayconst(void)
    151 {
    152 	prop_number_t freq;
    153 
    154 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    155 	KASSERT(freq != NULL);
    156 
    157 	ticks_per_sec = (u_long) prop_number_integer_value(freq);
    158 	ns_per_tick = 1000000000 / ticks_per_sec;
    159 }
    160 
    161 /*
    162  * Fill in *tvp with current time with microsecond resolution.
    163  */
    164 void
    165 microtime(struct timeval *tvp)
    166 {
    167 	u_long tb;
    168 	u_long ticks;
    169 	int msr;
    170 
    171 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
    172 
    173 	tb = mftbl();
    174 	ticks = ((tb - lasttb) * 1000000ULL) / ticks_per_sec;
    175 
    176 	*tvp = time;
    177 	tvp->tv_usec += ticks;
    178 	while (tvp->tv_usec >= 1000000) {
    179 		tvp->tv_usec -= 1000000;
    180 		tvp->tv_sec++;
    181 	}
    182 
    183 	__asm volatile ("mtmsr %0" :: "r"(msr));
    184 }
    185 
    186 /*
    187  * Wait for about n microseconds (at least!).
    188  */
    189 void
    190 delay(unsigned int n)
    191 {
    192 	u_quad_t tb;
    193 	u_long tbh, tbl, scratch;
    194 
    195 	tb = mftb();
    196 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    197 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    198 	tbh = tb >> 32;
    199 	tbl = tb;
    200 	__asm volatile (
    201 #ifdef PPC_IBM403
    202 	    "1:	mftbhi %0	\n"
    203 #else
    204 	    "1:	mftbu %0	\n"
    205 #endif
    206 	    "	cmplw %0,%1	\n"
    207 	    "	blt 1b		\n"
    208 	    "	bgt 2f		\n"
    209 #ifdef PPC_IBM403
    210 	    "	mftblo %0	\n"
    211 #else
    212 	    "	mftb %0		\n"
    213 #endif
    214 	    "	cmplw %0,%2	\n"
    215 	    "	blt 1b		\n"
    216 	    "2: 		\n"
    217 	    : "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
    218 }
    219 
    220 /*
    221  * Nothing to do.
    222  */
    223 void
    224 setstatclockrate(int arg)
    225 {
    226 
    227 	/* Do nothing */
    228 }
    229