1 /* $NetBSD: clock.c,v 1.20 2008/01/08 13:52:00 joerg Exp $ */
2 /* $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $ */
5 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1996 TooLs GmbH.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.20 2008/01/08 13:52:00 joerg Exp $");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/timetc.h>
43 #include <uvm/uvm_extern.h>
45 #include <prop/proplib.h>
47 #include <machine/cpu.h>
49 #include <powerpc/spr.h>
52 * Initially we assume a processor with a bus frequency of 12.5 MHz.
54 static u_long ticks_per_sec
;
55 static u_long ns_per_tick
;
56 static long ticks_per_intr
;
57 static volatile u_long lasttb
, lasttb2
;
58 static u_long ticksmissed
;
59 static volatile int tickspending
;
61 static void init_ppc4xx_tc(void);
62 static u_int
get_ppc4xx_timecount(struct timecounter
*);
64 static struct timecounter ppc4xx_timecounter
= {
65 get_ppc4xx_timecount
, /* get_timecount */
67 ~0u, /* counter_mask */
69 "ppc_timebase", /* name */
75 void decr_intr(struct clockframe
*); /* called from trap_subr.S */
76 void stat_intr(struct clockframe
*); /* called from trap_subr.S */
78 #ifdef FAST_STAT_CLOCK
79 /* Stat clock runs at ~ 1.5 kHz */
80 #define PERIOD_POWER 17
81 #define TCR_PERIOD TCR_FP_2_17
83 /* Stat clock runs at ~ 95Hz */
84 #define PERIOD_POWER 21
85 #define TCR_PERIOD TCR_FP_2_21
90 stat_intr(struct clockframe
*frame
)
93 mtspr(SPR_TSR
, TSR_FIS
); /* Clear TSR[FIS] */
95 curcpu()->ci_ev_statclock
.ev_count
++;
97 /* Nobody can interrupt us, but see if we're allowed to run. */
98 if (! (curcpu()->ci_cpl
& mask_statclock
))
103 decr_intr(struct clockframe
*frame
)
110 * Check whether we are initialized.
116 mtspr(SPR_TSR
, TSR_PIS
); /* Clear TSR[PIS] */
118 xticks
= tbtick
- lasttb2
; /* Number of TLB cycles since last exception */
119 for (nticks
= 0; xticks
> ticks_per_intr
; nticks
++)
120 xticks
-= ticks_per_intr
;
121 lasttb2
= tbtick
- xticks
;
124 curcpu()->ci_ev_clock
.ev_count
++;
126 if (pri
& mask_clock
) {
127 tickspending
+= nticks
;
128 ticksmissed
+= nticks
;
130 nticks
+= tickspending
;
134 * lasttb is used during microtime. Set it to the virtual
135 * start of this tick interval.
140 * Reenable interrupts
142 __asm
volatile ("wrteei 1");
145 * Do standard timer interrupt stuff.
146 * Do softclock stuff only on the last iteration.
148 frame
->pri
= pri
| mask_clock
;
160 /* Initialized in powerpc/ibm4xx/cpu.c */
161 evcnt_attach_static(&curcpu()->ci_ev_clock
);
162 evcnt_attach_static(&curcpu()->ci_ev_statclock
);
164 ticks_per_intr
= ticks_per_sec
/ hz
;
165 stathz
= profhz
= ticks_per_sec
/ (1 << PERIOD_POWER
);
167 printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec
, hz
,
170 lasttb2
= lasttb
= mftbl();
171 mtspr(SPR_PIT
, ticks_per_intr
);
173 /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
174 mtspr(SPR_TCR
, TCR_PIE
| TCR_ARE
| TCR_FIE
| TCR_PERIOD
);
180 calc_delayconst(void)
184 freq
= prop_dictionary_get(board_properties
, "processor-frequency");
185 KASSERT(freq
!= NULL
);
187 ticks_per_sec
= (u_long
) prop_number_integer_value(freq
);
188 ns_per_tick
= 1000000000 / ticks_per_sec
;
192 get_ppc4xx_timecount(struct timecounter
*tc
)
197 __asm
volatile ("mfmsr %0; wrteei 0" : "=r"(msr
) :);
199 __asm
volatile ("mtmsr %0" :: "r"(msr
));
205 * Wait for about n microseconds (at least!).
208 delay(unsigned int n
)
211 u_long tbh
, tbl
, scratch
;
214 /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
215 tb
+= (n
* 1000ULL + ns_per_tick
- 1) / ns_per_tick
;
235 : "=&r"(scratch
) : "r"(tbh
), "r"(tbl
) : "cr0");
242 setstatclockrate(int arg
)
251 /* from machdep initialization */
252 ppc4xx_timecounter
.tc_frequency
= ticks_per_sec
;
253 tc_init(&ppc4xx_timecounter
);