No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / powerpc / ibm4xx / clock.c
blobd5baed377d55cabf8bf0cb029e251d203748a17f
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 $ */
4 /*
5 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 * Copyright (C) 1995, 1996 TooLs GmbH.
7 * All rights reserved.
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.
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 */
66 0, /* no poll_pps */
67 ~0u, /* counter_mask */
68 0, /* frequency */
69 "ppc_timebase", /* name */
70 100, /* quality */
71 NULL, /* tc_priv */
72 NULL /* tc_next */
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
82 #else
83 /* Stat clock runs at ~ 95Hz */
84 #define PERIOD_POWER 21
85 #define TCR_PERIOD TCR_FP_2_21
86 #endif
89 void
90 stat_intr(struct clockframe *frame)
93 mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
94 uvmexp.intrs++;
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))
99 statclock(frame);
102 void
103 decr_intr(struct clockframe *frame)
105 int pri;
106 long tbtick, xticks;
107 int nticks;
110 * Check whether we are initialized.
112 if (!ticks_per_intr)
113 return;
115 tbtick = mftbl();
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;
123 uvmexp.intrs++;
124 curcpu()->ci_ev_clock.ev_count++;
125 pri = splclock();
126 if (pri & mask_clock) {
127 tickspending += nticks;
128 ticksmissed += nticks;
129 } else {
130 nticks += tickspending;
131 tickspending = 0;
134 * lasttb is used during microtime. Set it to the virtual
135 * start of this tick interval.
137 lasttb = lasttb2;
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;
149 while (--nticks > 0)
150 hardclock(frame);
151 frame->pri = pri;
152 hardclock(frame);
154 splx(pri);
157 void
158 cpu_initclocks(void)
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,
168 ticks_per_intr);
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);
176 init_ppc4xx_tc();
179 void
180 calc_delayconst(void)
182 prop_number_t freq;
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;
191 static u_int
192 get_ppc4xx_timecount(struct timecounter *tc)
194 u_long tb;
195 int msr;
197 __asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
198 tb = mftbl();
199 __asm volatile ("mtmsr %0" :: "r"(msr));
201 return tb;
205 * Wait for about n microseconds (at least!).
207 void
208 delay(unsigned int n)
210 u_quad_t tb;
211 u_long tbh, tbl, scratch;
213 tb = mftb();
214 /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
215 tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
216 tbh = tb >> 32;
217 tbl = tb;
218 __asm volatile (
219 #ifdef PPC_IBM403
220 "1: mftbhi %0 \n"
221 #else
222 "1: mftbu %0 \n"
223 #endif
224 " cmplw %0,%1 \n"
225 " blt 1b \n"
226 " bgt 2f \n"
227 #ifdef PPC_IBM403
228 " mftblo %0 \n"
229 #else
230 " mftb %0 \n"
231 #endif
232 " cmplw %0,%2 \n"
233 " blt 1b \n"
234 "2: \n"
235 : "=&r"(scratch) : "r"(tbh), "r"(tbl) : "cr0");
239 * Nothing to do.
241 void
242 setstatclockrate(int arg)
245 /* Do nothing */
248 static void
249 init_ppc4xx_tc(void)
251 /* from machdep initialization */
252 ppc4xx_timecounter.tc_frequency = ticks_per_sec;
253 tc_init(&ppc4xx_timecounter);