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