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