Home | History | Annotate | Line # | Download | only in ibm4xx
clock.c revision 1.23
      1 /*	$NetBSD: clock.c,v 1.23 2010/12/20 00:25:41 matt 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.23 2010/12/20 00:25:41 matt Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/timetc.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <prop/proplib.h>
     46 
     47 #include <machine/cpu.h>
     48 
     49 #include <powerpc/spr.h>
     50 #include <powerpc/ibm4xx/spr.h>
     51 
     52 /*
     53  * Initially we assume a processor with a bus frequency of 12.5 MHz.
     54  */
     55 static u_long ticks_per_sec;
     56 static u_long ns_per_tick;
     57 static long ticks_per_intr;
     58 static volatile u_long lasttb, lasttb2;
     59 static u_long ticksmissed;
     60 static volatile int tickspending;
     61 
     62 static void init_ppc4xx_tc(void);
     63 static u_int get_ppc4xx_timecount(struct timecounter *);
     64 
     65 static struct timecounter ppc4xx_timecounter = {
     66 	get_ppc4xx_timecount,	/* get_timecount */
     67 	0,			/* no poll_pps */
     68 	~0u,			/* counter_mask */
     69 	0,			/* frequency */
     70 	"ppc_timebase",		/* name */
     71 	100,			/* quality */
     72 	NULL,			/* tc_priv */
     73 	NULL			/* tc_next */
     74 };
     75 
     76 void decr_intr(struct clockframe *);	/* called from trap_subr.S */
     77 void stat_intr(struct clockframe *);	/* called from trap_subr.S */
     78 
     79 #ifdef FAST_STAT_CLOCK
     80 /* Stat clock runs at ~ 1.5 kHz */
     81 #define PERIOD_POWER	17
     82 #define TCR_PERIOD	TCR_FP_2_17
     83 #else
     84 /* Stat clock runs at ~ 95Hz */
     85 #define PERIOD_POWER	21
     86 #define TCR_PERIOD	TCR_FP_2_21
     87 #endif
     88 
     89 
     90 void
     91 stat_intr(struct clockframe *frame)
     92 {
     93 	struct cpu_info * const ci = curcpu();
     94 
     95 	mtspr(SPR_TSR, TSR_FIS);	/* Clear TSR[FIS] */
     96 	ci->ci_data.cpu_nintr++;
     97 	ci->ci_ev_statclock.ev_count++;
     98 
     99 	/* Nobody can interrupt us, but see if we're allowed to run. */
    100 	if (! (ci->ci_cpl & mask_statclock))
    101   		statclock(frame);
    102 }
    103 
    104 void
    105 decr_intr(struct clockframe *frame)
    106 {
    107 	struct cpu_info * const ci = curcpu();
    108 	int pri;
    109 	long tbtick, xticks;
    110 	int nticks;
    111 
    112 	/*
    113 	 * Check whether we are initialized.
    114 	 */
    115 	if (!ticks_per_intr)
    116 		return;
    117 
    118 	tbtick = mftbl();
    119 	mtspr(SPR_TSR, TSR_PIS);	/* Clear TSR[PIS] */
    120 
    121 	xticks = tbtick - lasttb2;	/* Number of TLB cycles since last exception */
    122 	for (nticks = 0; xticks > ticks_per_intr; nticks++)
    123 		xticks -= ticks_per_intr;
    124 	lasttb2 = tbtick - xticks;
    125 
    126 	ci->ci_data.cpu_nintr++;
    127 	ci->ci_ev_clock.ev_count++;
    128 	pri = splclock();
    129 	if (pri & mask_clock) {
    130 		tickspending += nticks;
    131 		ticksmissed += nticks;
    132 	} else {
    133 		nticks += tickspending;
    134 		tickspending = 0;
    135 
    136 		/*
    137 		 * lasttb is used during microtime. Set it to the virtual
    138 		 * start of this tick interval.
    139 		 */
    140 		lasttb = lasttb2;
    141 
    142 		/*
    143 		 * Reenable interrupts
    144 		 */
    145 		__asm volatile ("wrteei 1");
    146 
    147 		/*
    148 		 * Do standard timer interrupt stuff.
    149 		 * Do softclock stuff only on the last iteration.
    150 		 */
    151 		frame->pri = pri | mask_clock;
    152 		while (--nticks > 0)
    153 			hardclock(frame);
    154 		frame->pri = pri;
    155 		hardclock(frame);
    156 	}
    157 	splx(pri);
    158 }
    159 
    160 void
    161 cpu_initclocks(void)
    162 {
    163 	struct cpu_info * const ci = curcpu();
    164 
    165 	/* Initialized in powerpc/ibm4xx/cpu.c */
    166 	evcnt_attach_static(&ci->ci_ev_clock);
    167 	evcnt_attach_static(&ci->ci_ev_statclock);
    168 
    169 	ticks_per_intr = ticks_per_sec / hz;
    170 	stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
    171 
    172 	printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz,
    173 	    ticks_per_intr);
    174 
    175 	lasttb2 = lasttb = mftbl();
    176 	mtspr(SPR_PIT, ticks_per_intr);
    177 
    178 	/* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
    179 	mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
    180 
    181 	init_ppc4xx_tc();
    182 }
    183 
    184 void
    185 calc_delayconst(void)
    186 {
    187 	prop_number_t freq;
    188 
    189 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    190 	KASSERT(freq != NULL);
    191 
    192 	ticks_per_sec = (u_long) prop_number_integer_value(freq);
    193 	ns_per_tick = 1000000000 / ticks_per_sec;
    194 }
    195 
    196 static u_int
    197 get_ppc4xx_timecount(struct timecounter *tc)
    198 {
    199 	u_long tb;
    200 	int msr;
    201 
    202 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
    203 	tb = mftbl();
    204 	__asm volatile ("mtmsr %0" :: "r"(msr));
    205 
    206 	return tb;
    207 }
    208 
    209 /*
    210  * Wait for about n microseconds (at least!).
    211  */
    212 void
    213 delay(unsigned int n)
    214 {
    215 	u_quad_t tb;
    216 	u_long tbh, tbl, scratch;
    217 
    218 	tb = mftb();
    219 	/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
    220 	tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
    221 	tbh = tb >> 32;
    222 	tbl = tb;
    223 	__asm volatile (
    224 #ifdef PPC_IBM403
    225 	    "1:	mftbhi %0	\n"
    226 #else
    227 	    "1:	mftbu %0	\n"
    228 #endif
    229 	    "	cmplw %0,%1	\n"
    230 	    "	blt 1b		\n"
    231 	    "	bgt 2f		\n"
    232 #ifdef PPC_IBM403
    233 	    "	mftblo %0	\n"
    234 #else
    235 	    "	mftb %0		\n"
    236 #endif
    237 	    "	cmplw %0,%2	\n"
    238 	    "	blt 1b		\n"
    239 	    "2: 		\n"
    240 	    : "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
    241 }
    242 
    243 /*
    244  * Nothing to do.
    245  */
    246 void
    247 setstatclockrate(int arg)
    248 {
    249 
    250 	/* Do nothing */
    251 }
    252 
    253 static void
    254 init_ppc4xx_tc(void)
    255 {
    256 	/* from machdep initialization */
    257 	ppc4xx_timecounter.tc_frequency = ticks_per_sec;
    258 	tc_init(&ppc4xx_timecounter);
    259 }
    260