Home | History | Annotate | Line # | Download | only in ibm4xx
clock.c revision 1.4
      1 /*	$NetBSD: clock.c,v 1.4 2002/08/03 13:12:44 simonb 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/param.h>
     36 #include <sys/kernel.h>
     37 #include <sys/systm.h>
     38 #include <sys/properties.h>
     39 
     40 #include <machine/walnut.h>
     41 #include <machine/dcr.h>
     42 
     43 #include <powerpc/spr.h>
     44 
     45 /*
     46  * Initially we assume a processor with a bus frequency of 12.5 MHz.
     47  */
     48 static u_long ticks_per_sec;
     49 static u_long ns_per_tick;
     50 static long ticks_per_intr;
     51 static volatile u_long lasttb;
     52 static u_long ticksmissed;
     53 static volatile int tickspending;
     54 
     55 void decr_intr(struct clockframe *);	/* called from trap_subr.S */
     56 void stat_intr(struct clockframe *);	/* called from trap_subr.S */
     57 static inline u_quad_t mftb(void);
     58 
     59 #ifdef FAST_STAT_CLOCK
     60 /* Stat clock runs at ~ 1.5KHz */
     61 #define PERIOD_POWER	17
     62 #define TCR_PERIOD	TCR_FP_2_17
     63 #else
     64 /* Stat clock runs at ~ 95Hz */
     65 #define PERIOD_POWER	21
     66 #define TCR_PERIOD	TCR_FP_2_21
     67 #endif
     68 
     69 
     70 void
     71 stat_intr(struct clockframe *frame)
     72 {
     73 	extern u_long intrcnt[];
     74 
     75 	mtspr(SPR_TSR, TSR_FIS);	/* Clear TSR[FIS] */
     76 	intrcnt[CNT_STATCLOCK]++;
     77   	statclock(frame);
     78 }
     79 
     80 void
     81 decr_intr(struct clockframe *frame)
     82 {
     83 	int pri;
     84 	long tick, xticks;
     85 	int nticks;
     86 	extern u_long intrcnt[];
     87 	/*
     88 	 * Check whether we are initialized.
     89 	 */
     90 	if (!ticks_per_intr)
     91 		return;
     92 
     93 	asm volatile("mftb %0":"=r"(tick):);
     94 	mtspr(SPR_TSR, TSR_PIS);	/* Clear TSR[PIS] */
     95 	/*
     96 	 * lasttb is used during microtime. Set it to the virtual
     97 	 * start of this tick interval.
     98 	 */
     99 	xticks = tick - lasttb;	/* Number of TLB cycles since last exception */
    100 	for (nticks = 0; xticks > ticks_per_intr; nticks++)
    101 		xticks -= ticks_per_intr;
    102 	lasttb = tick - xticks;
    103 
    104 	intrcnt[CNT_CLOCK]++;
    105 	pri = splclock();
    106 	if (pri & SPL_CLOCK) {
    107 		tickspending += nticks;
    108 		ticksmissed+= nticks;
    109 	} else {
    110 		nticks += tickspending;
    111 		tickspending = 0;
    112 
    113 		/*
    114 		 * Reenable interrupts
    115 		 */
    116 		asm volatile ("wrteei 1");
    117 
    118 		/*
    119 		 * Do standard timer interrupt stuff.
    120 		 * Do softclock stuff only on the last iteration.
    121 		 */
    122 		frame->pri = pri | SINT_CLOCK;
    123 		while (--nticks > 0)
    124 			hardclock(frame);
    125 		frame->pri = pri;
    126 		hardclock(frame);
    127 	}
    128 	splx(pri);
    129 }
    130 
    131 void
    132 cpu_initclocks(void)
    133 {
    134 
    135 	ticks_per_intr = ticks_per_sec / hz;
    136 	stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
    137 	printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
    138 	asm volatile ("mftb %0" : "=r"(lasttb));
    139 	mtspr(SPR_PIT, ticks_per_intr);
    140 	/* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
    141 	mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
    142 }
    143 
    144 void
    145 calc_delayconst(void)
    146 {
    147 	unsigned int processor_freq;
    148 
    149 	if (board_info_get("processor-frequency",
    150 		&processor_freq, sizeof(processor_freq)) == -1)
    151 		panic("no processor-frequency");
    152 
    153 	ticks_per_sec = processor_freq;
    154 	ns_per_tick = 1000000000 / ticks_per_sec;
    155 
    156 	/* Make sure that timers run at CPU frequency */
    157 	mtdcr(DCR_CPC0_CR1, mfdcr(DCR_CPC0_CR1) & ~CPC0_CR1_CETE);
    158 }
    159 
    160 static inline u_quad_t
    161 mftb(void)
    162 {
    163 	u_long scratch;
    164 	u_quad_t tb;
    165 
    166 	asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
    167 	    : "=r"(tb), "=r"(scratch));
    168 	return tb;
    169 }
    170 
    171 /*
    172  * Fill in *tvp with current time with microsecond resolution.
    173  */
    174 void
    175 microtime(struct timeval *tvp)
    176 {
    177 	u_long tb;
    178 	u_long ticks;
    179 	int msr;
    180 
    181 	asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
    182 	asm ("mftb %0" : "=r"(tb));
    183 	ticks = (tb - lasttb) * ns_per_tick;
    184 	*tvp = time;
    185 	asm volatile ("mtmsr %0" :: "r"(msr));
    186 	ticks /= 1000;
    187 	tvp->tv_usec += ticks;
    188 	while (tvp->tv_usec >= 1000000) {
    189 		tvp->tv_usec -= 1000000;
    190 		tvp->tv_sec++;
    191 	}
    192 }
    193 
    194 /*
    195  * Wait for about n microseconds (at least!).
    196  */
    197 void
    198 delay(unsigned int n)
    199 {
    200 	u_quad_t tb;
    201 	u_long tbh, tbl, scratch;
    202 
    203 	tb = mftb();
    204 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    205 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    206 	tbh = tb >> 32;
    207 	tbl = tb;
    208 	asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
    209 		      "mftb %0; cmplw %0,%2; blt 1b; 2:"
    210 		      : "=r"(scratch) : "r"(tbh), "r"(tbl));
    211 }
    212 
    213 /*
    214  * Nothing to do.
    215  */
    216 void
    217 setstatclockrate(int arg)
    218 {
    219 
    220 	/* Do nothing */
    221 }
    222