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