clock.c revision 1.1 1 /* $NetBSD: clock.c,v 1.1 2002/03/13 23:12:11 eeh 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/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38
39 #include <machine/walnut.h>
40 #include <machine/dcr.h>
41
42 #include <powerpc/spr.h>
43
44 /*
45 * Initially we assume a processor with a bus frequency of 12.5 MHz.
46 */
47 static u_long ticks_per_sec;
48 static u_long ns_per_tick;
49 static long ticks_per_intr;
50 static volatile u_long lasttb;
51 static u_long ticksmissed;
52 static volatile int tickspending;
53
54 void decr_intr(struct clockframe *); /* called from trap_subr.S */
55 void stat_intr(struct clockframe *); /* called from trap_subr.S */
56 static inline u_quad_t mftb(void);
57
58 #ifdef FAST_STAT_CLOCK
59 /* Stat clock runs at ~ 1.5KHz */
60 #define PERIOD_POWER 17
61 #define TCR_PERIOD TCR_FP_2_17
62 #else
63 /* Stat clock runs at ~ 95Hz */
64 #define PERIOD_POWER 21
65 #define TCR_PERIOD TCR_FP_2_21
66 #endif
67
68
69 void
70 stat_intr(struct clockframe *frame)
71 {
72 extern u_long intrcnt[];
73
74 mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
75 intrcnt[CNT_STATCLOCK]++;
76 statclock(frame);
77 }
78
79 void
80 decr_intr(struct clockframe *frame)
81 {
82 int pri;
83 long tick, xticks;
84 int nticks;
85 extern u_long intrcnt[];
86 /*
87 * Check whether we are initialized.
88 */
89 if (!ticks_per_intr)
90 return;
91
92 asm volatile("mftb %0":"=r"(tick):);
93 mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
94 /*
95 * lasttb is used during microtime. Set it to the virtual
96 * start of this tick interval.
97 */
98 xticks = tick - lasttb; /* Number of TLB cycles since last exception */
99 for (nticks = 0; xticks > ticks_per_intr; nticks++)
100 xticks -= ticks_per_intr;
101 lasttb = tick - xticks;
102
103 intrcnt[CNT_CLOCK]++;
104 pri = splclock();
105 if (pri & SPL_CLOCK) {
106 tickspending += nticks;
107 ticksmissed+= nticks;
108 } else {
109 nticks += tickspending;
110 tickspending = 0;
111
112 /*
113 * Reenable interrupts
114 */
115 asm volatile ("wrteei 1");
116
117 /*
118 * Do standard timer interrupt stuff.
119 * Do softclock stuff only on the last iteration.
120 */
121 frame->pri = pri | SINT_CLOCK;
122 while (--nticks > 0)
123 hardclock(frame);
124 frame->pri = pri;
125 hardclock(frame);
126 }
127 splx(pri);
128 }
129
130 void
131 cpu_initclocks(void)
132 {
133 ticks_per_intr = ticks_per_sec / hz;
134 stathz = profhz = ticks_per_sec / (1<<PERIOD_POWER);
135 printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
136 asm volatile ("mftb %0" : "=r"(lasttb));
137 mtspr(SPR_PIT, ticks_per_intr);
138 /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
139 mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
140 }
141
142 void
143 calc_delayconst(void)
144 {
145
146 ticks_per_sec = board_data.processor_speed;
147 ns_per_tick = 1000000000 / ticks_per_sec;
148
149 /* Make sure that timers run at CPU frequency */
150 mtdcr(DCR_CPC0_CR1, mfdcr(DCR_CPC0_CR1) & ~CPC0_CR1_CETE);
151 }
152
153 static inline u_quad_t
154 mftb(void)
155 {
156 u_long scratch;
157 u_quad_t tb;
158
159 asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
160 : "=r"(tb), "=r"(scratch));
161 return tb;
162 }
163
164 /*
165 * Fill in *tvp with current time with microsecond resolution.
166 */
167 void
168 microtime(struct timeval *tvp)
169 {
170 u_long tb;
171 u_long ticks;
172 int msr;
173
174 asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
175 asm ("mftb %0" : "=r"(tb));
176 ticks = (tb - lasttb) * ns_per_tick;
177 *tvp = time;
178 asm volatile ("mtmsr %0" :: "r"(msr));
179 ticks /= 1000;
180 tvp->tv_usec += ticks;
181 while (tvp->tv_usec >= 1000000) {
182 tvp->tv_usec -= 1000000;
183 tvp->tv_sec++;
184 }
185 }
186
187 /*
188 * Wait for about n microseconds (at least!).
189 */
190 void
191 delay(unsigned int n)
192 {
193 u_quad_t tb;
194 u_long tbh, tbl, scratch;
195
196 tb = mftb();
197 /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
198 tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
199 tbh = tb >> 32;
200 tbl = tb;
201 asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
202 "mftb %0; cmplw %0,%2; blt 1b; 2:"
203 : "=r"(scratch) : "r"(tbh), "r"(tbl));
204 }
205
206 /*
207 * Nothing to do.
208 */
209 void
210 setstatclockrate(int arg)
211 {
212
213 /* Do nothing */
214 }
215