2011-01-18 04:02:52 +03:00
|
|
|
/* $NetBSD: clock.c,v 1.24 2011/01/18 01:02:53 matt Exp $ */
|
2002-03-14 02:12:11 +03:00
|
|
|
/* $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1995, 1996 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-07-15 06:54:31 +04:00
|
|
|
#include <sys/cdefs.h>
|
2011-01-18 04:02:52 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.24 2011/01/18 01:02:53 matt Exp $");
|
2003-07-15 06:54:31 +04:00
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2008-01-08 16:47:49 +03:00
|
|
|
#include <sys/timetc.h>
|
2006-05-05 22:04:41 +04:00
|
|
|
|
2008-01-09 09:50:36 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2006-05-05 22:04:41 +04:00
|
|
|
#include <prop/proplib.h>
|
2002-03-14 02:12:11 +03:00
|
|
|
|
2003-03-11 13:40:15 +03:00
|
|
|
#include <machine/cpu.h>
|
2002-03-14 02:12:11 +03:00
|
|
|
|
|
|
|
#include <powerpc/spr.h>
|
2010-02-26 02:31:47 +03:00
|
|
|
#include <powerpc/ibm4xx/spr.h>
|
2002-03-14 02:12:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initially we assume a processor with a bus frequency of 12.5 MHz.
|
|
|
|
*/
|
|
|
|
static u_long ticks_per_sec;
|
|
|
|
static u_long ns_per_tick;
|
|
|
|
static long ticks_per_intr;
|
2005-11-23 16:00:51 +03:00
|
|
|
static volatile u_long lasttb, lasttb2;
|
2002-03-14 02:12:11 +03:00
|
|
|
static u_long ticksmissed;
|
|
|
|
static volatile int tickspending;
|
|
|
|
|
2008-01-08 16:47:49 +03:00
|
|
|
static void init_ppc4xx_tc(void);
|
|
|
|
static u_int get_ppc4xx_timecount(struct timecounter *);
|
|
|
|
|
|
|
|
static struct timecounter ppc4xx_timecounter = {
|
|
|
|
get_ppc4xx_timecount, /* get_timecount */
|
|
|
|
0, /* no poll_pps */
|
2008-01-08 16:52:00 +03:00
|
|
|
~0u, /* counter_mask */
|
2008-01-08 16:47:49 +03:00
|
|
|
0, /* frequency */
|
|
|
|
"ppc_timebase", /* name */
|
|
|
|
100, /* quality */
|
|
|
|
NULL, /* tc_priv */
|
|
|
|
NULL /* tc_next */
|
|
|
|
};
|
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
void decr_intr(struct clockframe *); /* called from trap_subr.S */
|
|
|
|
void stat_intr(struct clockframe *); /* called from trap_subr.S */
|
|
|
|
|
|
|
|
#ifdef FAST_STAT_CLOCK
|
2006-03-09 02:46:22 +03:00
|
|
|
/* Stat clock runs at ~ 1.5 kHz */
|
2002-03-14 02:12:11 +03:00
|
|
|
#define PERIOD_POWER 17
|
|
|
|
#define TCR_PERIOD TCR_FP_2_17
|
|
|
|
#else
|
|
|
|
/* Stat clock runs at ~ 95Hz */
|
|
|
|
#define PERIOD_POWER 21
|
|
|
|
#define TCR_PERIOD TCR_FP_2_21
|
2002-07-11 05:38:48 +04:00
|
|
|
#endif
|
2002-03-14 02:12:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
stat_intr(struct clockframe *frame)
|
|
|
|
{
|
2010-12-20 03:25:23 +03:00
|
|
|
struct cpu_info * const ci = curcpu();
|
2008-01-09 09:50:36 +03:00
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
|
2010-12-20 03:25:23 +03:00
|
|
|
ci->ci_data.cpu_nintr++;
|
|
|
|
ci->ci_ev_statclock.ev_count++;
|
2006-09-27 13:11:47 +04:00
|
|
|
|
|
|
|
/* Nobody can interrupt us, but see if we're allowed to run. */
|
2010-12-20 03:25:23 +03:00
|
|
|
if (! (ci->ci_cpl & mask_statclock))
|
2006-09-27 13:11:47 +04:00
|
|
|
statclock(frame);
|
2002-03-14 02:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
decr_intr(struct clockframe *frame)
|
|
|
|
{
|
2010-12-20 03:25:23 +03:00
|
|
|
struct cpu_info * const ci = curcpu();
|
2002-03-14 02:12:11 +03:00
|
|
|
int pri;
|
2005-06-03 15:54:48 +04:00
|
|
|
long tbtick, xticks;
|
2002-03-14 02:12:11 +03:00
|
|
|
int nticks;
|
2006-06-30 21:54:50 +04:00
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
/*
|
|
|
|
* Check whether we are initialized.
|
|
|
|
*/
|
|
|
|
if (!ticks_per_intr)
|
|
|
|
return;
|
|
|
|
|
2005-06-03 15:54:48 +04:00
|
|
|
tbtick = mftbl();
|
2002-03-14 02:12:11 +03:00
|
|
|
mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
|
2005-11-23 16:00:51 +03:00
|
|
|
|
|
|
|
xticks = tbtick - lasttb2; /* Number of TLB cycles since last exception */
|
2002-03-14 02:12:11 +03:00
|
|
|
for (nticks = 0; xticks > ticks_per_intr; nticks++)
|
|
|
|
xticks -= ticks_per_intr;
|
2005-11-23 16:00:51 +03:00
|
|
|
lasttb2 = tbtick - xticks;
|
2002-03-14 02:12:11 +03:00
|
|
|
|
2010-12-20 03:25:23 +03:00
|
|
|
ci->ci_data.cpu_nintr++;
|
|
|
|
ci->ci_ev_clock.ev_count++;
|
2002-03-14 02:12:11 +03:00
|
|
|
pri = splclock();
|
2006-06-30 21:54:50 +04:00
|
|
|
if (pri & mask_clock) {
|
2002-03-14 02:12:11 +03:00
|
|
|
tickspending += nticks;
|
2006-06-30 21:54:50 +04:00
|
|
|
ticksmissed += nticks;
|
2002-03-14 02:12:11 +03:00
|
|
|
} else {
|
|
|
|
nticks += tickspending;
|
|
|
|
tickspending = 0;
|
|
|
|
|
2005-11-23 16:00:51 +03:00
|
|
|
/*
|
|
|
|
* lasttb is used during microtime. Set it to the virtual
|
|
|
|
* start of this tick interval.
|
|
|
|
*/
|
|
|
|
lasttb = lasttb2;
|
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
/*
|
|
|
|
* Reenable interrupts
|
|
|
|
*/
|
2005-12-25 01:45:33 +03:00
|
|
|
__asm volatile ("wrteei 1");
|
2002-03-14 02:12:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do standard timer interrupt stuff.
|
|
|
|
* Do softclock stuff only on the last iteration.
|
|
|
|
*/
|
|
|
|
while (--nticks > 0)
|
|
|
|
hardclock(frame);
|
|
|
|
hardclock(frame);
|
|
|
|
}
|
|
|
|
splx(pri);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cpu_initclocks(void)
|
|
|
|
{
|
2010-12-20 03:25:23 +03:00
|
|
|
struct cpu_info * const ci = curcpu();
|
|
|
|
|
2006-06-30 21:54:50 +04:00
|
|
|
/* Initialized in powerpc/ibm4xx/cpu.c */
|
2010-12-20 03:25:23 +03:00
|
|
|
evcnt_attach_static(&ci->ci_ev_clock);
|
|
|
|
evcnt_attach_static(&ci->ci_ev_statclock);
|
2002-08-03 17:12:44 +04:00
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
ticks_per_intr = ticks_per_sec / hz;
|
2002-08-03 17:12:44 +04:00
|
|
|
stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
|
2006-06-30 21:54:50 +04:00
|
|
|
|
|
|
|
printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz,
|
|
|
|
ticks_per_intr);
|
|
|
|
|
2005-11-23 16:00:51 +03:00
|
|
|
lasttb2 = lasttb = mftbl();
|
2002-03-14 02:12:11 +03:00
|
|
|
mtspr(SPR_PIT, ticks_per_intr);
|
2006-06-30 21:54:50 +04:00
|
|
|
|
2002-03-14 02:12:11 +03:00
|
|
|
/* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
|
2002-07-11 05:38:48 +04:00
|
|
|
mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
|
2008-01-08 16:47:49 +03:00
|
|
|
|
|
|
|
init_ppc4xx_tc();
|
2002-03-14 02:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
calc_delayconst(void)
|
|
|
|
{
|
2006-05-05 22:04:41 +04:00
|
|
|
prop_number_t freq;
|
2002-03-14 02:12:11 +03:00
|
|
|
|
2006-05-05 22:04:41 +04:00
|
|
|
freq = prop_dictionary_get(board_properties, "processor-frequency");
|
|
|
|
KASSERT(freq != NULL);
|
2002-03-16 00:12:07 +03:00
|
|
|
|
2006-05-05 22:04:41 +04:00
|
|
|
ticks_per_sec = (u_long) prop_number_integer_value(freq);
|
2002-03-14 02:12:11 +03:00
|
|
|
ns_per_tick = 1000000000 / ticks_per_sec;
|
|
|
|
}
|
|
|
|
|
2008-01-08 16:47:49 +03:00
|
|
|
static u_int
|
|
|
|
get_ppc4xx_timecount(struct timecounter *tc)
|
2002-03-14 02:12:11 +03:00
|
|
|
{
|
|
|
|
u_long tb;
|
|
|
|
int msr;
|
|
|
|
|
2005-12-25 01:45:33 +03:00
|
|
|
__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
|
2003-03-11 13:40:15 +03:00
|
|
|
tb = mftbl();
|
2005-12-25 01:45:33 +03:00
|
|
|
__asm volatile ("mtmsr %0" :: "r"(msr));
|
2008-01-08 16:47:49 +03:00
|
|
|
|
|
|
|
return tb;
|
2002-03-14 02:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for about n microseconds (at least!).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
delay(unsigned int n)
|
|
|
|
{
|
|
|
|
u_quad_t tb;
|
|
|
|
u_long tbh, tbl, scratch;
|
|
|
|
|
|
|
|
tb = mftb();
|
|
|
|
/* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
|
|
|
|
tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
|
|
|
|
tbh = tb >> 32;
|
|
|
|
tbl = tb;
|
2005-12-25 01:45:33 +03:00
|
|
|
__asm volatile (
|
2003-03-11 13:40:15 +03:00
|
|
|
#ifdef PPC_IBM403
|
|
|
|
"1: mftbhi %0 \n"
|
|
|
|
#else
|
|
|
|
"1: mftbu %0 \n"
|
|
|
|
#endif
|
|
|
|
" cmplw %0,%1 \n"
|
|
|
|
" blt 1b \n"
|
|
|
|
" bgt 2f \n"
|
|
|
|
#ifdef PPC_IBM403
|
|
|
|
" mftblo %0 \n"
|
|
|
|
#else
|
|
|
|
" mftb %0 \n"
|
|
|
|
#endif
|
|
|
|
" cmplw %0,%2 \n"
|
|
|
|
" blt 1b \n"
|
|
|
|
"2: \n"
|
2004-09-02 12:22:58 +04:00
|
|
|
: "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
|
2002-03-14 02:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nothing to do.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
setstatclockrate(int arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
2008-01-08 16:47:49 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_ppc4xx_tc(void)
|
|
|
|
{
|
|
|
|
/* from machdep initialization */
|
|
|
|
ppc4xx_timecounter.tc_frequency = ticks_per_sec;
|
|
|
|
tc_init(&ppc4xx_timecounter);
|
|
|
|
}
|