clock.c revision 1.2.4.3 1 1.2.4.3 nathanw /* $NetBSD: clock.c,v 1.2.4.3 2002/08/01 02:43:00 nathanw Exp $ */
2 1.2.4.2 nathanw /* $OpenBSD: clock.c,v 1.3 1997/10/13 13:42:53 pefo Exp $ */
3 1.2.4.2 nathanw
4 1.2.4.2 nathanw /*
5 1.2.4.2 nathanw * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6 1.2.4.2 nathanw * Copyright (C) 1995, 1996 TooLs GmbH.
7 1.2.4.2 nathanw * All rights reserved.
8 1.2.4.2 nathanw *
9 1.2.4.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.2.4.2 nathanw * modification, are permitted provided that the following conditions
11 1.2.4.2 nathanw * are met:
12 1.2.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.2.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.2.4.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.2.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.2.4.2 nathanw * must display the following acknowledgement:
19 1.2.4.2 nathanw * This product includes software developed by TooLs GmbH.
20 1.2.4.2 nathanw * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 1.2.4.2 nathanw * derived from this software without specific prior written permission.
22 1.2.4.2 nathanw *
23 1.2.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 1.2.4.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.2.4.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.2.4.2 nathanw * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 1.2.4.2 nathanw * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.2.4.2 nathanw * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 1.2.4.2 nathanw * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 1.2.4.2 nathanw * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 1.2.4.2 nathanw * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 1.2.4.2 nathanw * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.2.4.2 nathanw */
34 1.2.4.2 nathanw
35 1.2.4.2 nathanw #include <sys/param.h>
36 1.2.4.2 nathanw #include <sys/kernel.h>
37 1.2.4.2 nathanw #include <sys/systm.h>
38 1.2.4.2 nathanw #include <sys/properties.h>
39 1.2.4.2 nathanw
40 1.2.4.2 nathanw #include <machine/walnut.h>
41 1.2.4.2 nathanw #include <machine/dcr.h>
42 1.2.4.2 nathanw
43 1.2.4.2 nathanw #include <powerpc/spr.h>
44 1.2.4.2 nathanw
45 1.2.4.2 nathanw /*
46 1.2.4.2 nathanw * Initially we assume a processor with a bus frequency of 12.5 MHz.
47 1.2.4.2 nathanw */
48 1.2.4.2 nathanw static u_long ticks_per_sec;
49 1.2.4.2 nathanw static u_long ns_per_tick;
50 1.2.4.2 nathanw static long ticks_per_intr;
51 1.2.4.2 nathanw static volatile u_long lasttb;
52 1.2.4.2 nathanw static u_long ticksmissed;
53 1.2.4.2 nathanw static volatile int tickspending;
54 1.2.4.2 nathanw
55 1.2.4.2 nathanw void decr_intr(struct clockframe *); /* called from trap_subr.S */
56 1.2.4.2 nathanw void stat_intr(struct clockframe *); /* called from trap_subr.S */
57 1.2.4.2 nathanw static inline u_quad_t mftb(void);
58 1.2.4.2 nathanw
59 1.2.4.2 nathanw #ifdef FAST_STAT_CLOCK
60 1.2.4.2 nathanw /* Stat clock runs at ~ 1.5KHz */
61 1.2.4.2 nathanw #define PERIOD_POWER 17
62 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_17
63 1.2.4.2 nathanw #else
64 1.2.4.2 nathanw /* Stat clock runs at ~ 95Hz */
65 1.2.4.2 nathanw #define PERIOD_POWER 21
66 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_21
67 1.2.4.3 nathanw #endif
68 1.2.4.2 nathanw
69 1.2.4.2 nathanw
70 1.2.4.2 nathanw void
71 1.2.4.2 nathanw stat_intr(struct clockframe *frame)
72 1.2.4.2 nathanw {
73 1.2.4.2 nathanw extern u_long intrcnt[];
74 1.2.4.3 nathanw
75 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
76 1.2.4.2 nathanw intrcnt[CNT_STATCLOCK]++;
77 1.2.4.2 nathanw statclock(frame);
78 1.2.4.2 nathanw }
79 1.2.4.2 nathanw
80 1.2.4.2 nathanw void
81 1.2.4.2 nathanw decr_intr(struct clockframe *frame)
82 1.2.4.2 nathanw {
83 1.2.4.2 nathanw int pri;
84 1.2.4.2 nathanw long tick, xticks;
85 1.2.4.2 nathanw int nticks;
86 1.2.4.2 nathanw extern u_long intrcnt[];
87 1.2.4.2 nathanw /*
88 1.2.4.2 nathanw * Check whether we are initialized.
89 1.2.4.2 nathanw */
90 1.2.4.2 nathanw if (!ticks_per_intr)
91 1.2.4.2 nathanw return;
92 1.2.4.2 nathanw
93 1.2.4.2 nathanw asm volatile("mftb %0":"=r"(tick):);
94 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
95 1.2.4.2 nathanw /*
96 1.2.4.2 nathanw * lasttb is used during microtime. Set it to the virtual
97 1.2.4.2 nathanw * start of this tick interval.
98 1.2.4.2 nathanw */
99 1.2.4.2 nathanw xticks = tick - lasttb; /* Number of TLB cycles since last exception */
100 1.2.4.2 nathanw for (nticks = 0; xticks > ticks_per_intr; nticks++)
101 1.2.4.2 nathanw xticks -= ticks_per_intr;
102 1.2.4.2 nathanw lasttb = tick - xticks;
103 1.2.4.2 nathanw
104 1.2.4.2 nathanw intrcnt[CNT_CLOCK]++;
105 1.2.4.2 nathanw pri = splclock();
106 1.2.4.2 nathanw if (pri & SPL_CLOCK) {
107 1.2.4.2 nathanw tickspending += nticks;
108 1.2.4.2 nathanw ticksmissed+= nticks;
109 1.2.4.2 nathanw } else {
110 1.2.4.2 nathanw nticks += tickspending;
111 1.2.4.2 nathanw tickspending = 0;
112 1.2.4.2 nathanw
113 1.2.4.2 nathanw /*
114 1.2.4.2 nathanw * Reenable interrupts
115 1.2.4.2 nathanw */
116 1.2.4.2 nathanw asm volatile ("wrteei 1");
117 1.2.4.2 nathanw
118 1.2.4.2 nathanw /*
119 1.2.4.2 nathanw * Do standard timer interrupt stuff.
120 1.2.4.2 nathanw * Do softclock stuff only on the last iteration.
121 1.2.4.2 nathanw */
122 1.2.4.2 nathanw frame->pri = pri | SINT_CLOCK;
123 1.2.4.2 nathanw while (--nticks > 0)
124 1.2.4.2 nathanw hardclock(frame);
125 1.2.4.2 nathanw frame->pri = pri;
126 1.2.4.2 nathanw hardclock(frame);
127 1.2.4.2 nathanw }
128 1.2.4.2 nathanw splx(pri);
129 1.2.4.2 nathanw }
130 1.2.4.2 nathanw
131 1.2.4.2 nathanw void
132 1.2.4.2 nathanw cpu_initclocks(void)
133 1.2.4.2 nathanw {
134 1.2.4.2 nathanw ticks_per_intr = ticks_per_sec / hz;
135 1.2.4.3 nathanw stathz = profhz = ticks_per_sec / (1<<PERIOD_POWER);
136 1.2.4.2 nathanw printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
137 1.2.4.2 nathanw asm volatile ("mftb %0" : "=r"(lasttb));
138 1.2.4.2 nathanw mtspr(SPR_PIT, ticks_per_intr);
139 1.2.4.2 nathanw /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
140 1.2.4.3 nathanw mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
141 1.2.4.2 nathanw }
142 1.2.4.2 nathanw
143 1.2.4.2 nathanw void
144 1.2.4.2 nathanw calc_delayconst(void)
145 1.2.4.2 nathanw {
146 1.2.4.2 nathanw unsigned int processor_freq;
147 1.2.4.2 nathanw
148 1.2.4.3 nathanw if (board_info_get("processor-frequency",
149 1.2.4.2 nathanw &processor_freq, sizeof(processor_freq)) == -1)
150 1.2.4.2 nathanw panic("no processor-frequency");
151 1.2.4.2 nathanw
152 1.2.4.2 nathanw ticks_per_sec = processor_freq;
153 1.2.4.2 nathanw ns_per_tick = 1000000000 / ticks_per_sec;
154 1.2.4.2 nathanw
155 1.2.4.2 nathanw /* Make sure that timers run at CPU frequency */
156 1.2.4.2 nathanw mtdcr(DCR_CPC0_CR1, mfdcr(DCR_CPC0_CR1) & ~CPC0_CR1_CETE);
157 1.2.4.2 nathanw }
158 1.2.4.2 nathanw
159 1.2.4.2 nathanw static inline u_quad_t
160 1.2.4.2 nathanw mftb(void)
161 1.2.4.2 nathanw {
162 1.2.4.2 nathanw u_long scratch;
163 1.2.4.2 nathanw u_quad_t tb;
164 1.2.4.2 nathanw
165 1.2.4.2 nathanw asm ("1: mftbu %0; mftb %0+1; mftbu %1; cmpw %0,%1; bne 1b"
166 1.2.4.2 nathanw : "=r"(tb), "=r"(scratch));
167 1.2.4.2 nathanw return tb;
168 1.2.4.2 nathanw }
169 1.2.4.2 nathanw
170 1.2.4.2 nathanw /*
171 1.2.4.2 nathanw * Fill in *tvp with current time with microsecond resolution.
172 1.2.4.2 nathanw */
173 1.2.4.2 nathanw void
174 1.2.4.2 nathanw microtime(struct timeval *tvp)
175 1.2.4.2 nathanw {
176 1.2.4.2 nathanw u_long tb;
177 1.2.4.2 nathanw u_long ticks;
178 1.2.4.2 nathanw int msr;
179 1.2.4.2 nathanw
180 1.2.4.2 nathanw asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
181 1.2.4.2 nathanw asm ("mftb %0" : "=r"(tb));
182 1.2.4.2 nathanw ticks = (tb - lasttb) * ns_per_tick;
183 1.2.4.2 nathanw *tvp = time;
184 1.2.4.2 nathanw asm volatile ("mtmsr %0" :: "r"(msr));
185 1.2.4.2 nathanw ticks /= 1000;
186 1.2.4.2 nathanw tvp->tv_usec += ticks;
187 1.2.4.2 nathanw while (tvp->tv_usec >= 1000000) {
188 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
189 1.2.4.2 nathanw tvp->tv_sec++;
190 1.2.4.2 nathanw }
191 1.2.4.2 nathanw }
192 1.2.4.2 nathanw
193 1.2.4.2 nathanw /*
194 1.2.4.2 nathanw * Wait for about n microseconds (at least!).
195 1.2.4.2 nathanw */
196 1.2.4.2 nathanw void
197 1.2.4.2 nathanw delay(unsigned int n)
198 1.2.4.2 nathanw {
199 1.2.4.2 nathanw u_quad_t tb;
200 1.2.4.2 nathanw u_long tbh, tbl, scratch;
201 1.2.4.2 nathanw
202 1.2.4.2 nathanw tb = mftb();
203 1.2.4.2 nathanw /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
204 1.2.4.2 nathanw tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
205 1.2.4.2 nathanw tbh = tb >> 32;
206 1.2.4.2 nathanw tbl = tb;
207 1.2.4.2 nathanw asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
208 1.2.4.2 nathanw "mftb %0; cmplw %0,%2; blt 1b; 2:"
209 1.2.4.2 nathanw : "=r"(scratch) : "r"(tbh), "r"(tbl));
210 1.2.4.2 nathanw }
211 1.2.4.2 nathanw
212 1.2.4.2 nathanw /*
213 1.2.4.2 nathanw * Nothing to do.
214 1.2.4.2 nathanw */
215 1.2.4.2 nathanw void
216 1.2.4.2 nathanw setstatclockrate(int arg)
217 1.2.4.2 nathanw {
218 1.2.4.2 nathanw
219 1.2.4.2 nathanw /* Do nothing */
220 1.2.4.2 nathanw }
221