clock.c revision 1.2.4.4 1 /* $NetBSD: clock.c,v 1.2.4.4 2002/08/06 22:47:08 nathanw 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 #include <sys/properties.h>
39
40 #include <machine/walnut.h>
41 #include <machine/dcr.h>
42
43 #include <powerpc/spr.h>
44
45 /*
46 * Initially we assume a processor with a bus frequency of 12.5 MHz.
47 */
48 static u_long ticks_per_sec;
49 static u_long ns_per_tick;
50 static long ticks_per_intr;
51 static volatile u_long lasttb;
52 static u_long ticksmissed;
53 static volatile int tickspending;
54
55 void decr_intr(struct clockframe *); /* called from trap_subr.S */
56 void stat_intr(struct clockframe *); /* called from trap_subr.S */
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
134 ticks_per_intr = ticks_per_sec / hz;
135 stathz = profhz = ticks_per_sec / (1 << PERIOD_POWER);
136 printf("Setting PIT to %ld/%d = %ld\n", ticks_per_sec, hz, ticks_per_intr);
137 asm volatile ("mftb %0" : "=r"(lasttb));
138 mtspr(SPR_PIT, ticks_per_intr);
139 /* Enable PIT & FIT(2^17c = 0.655ms) interrupts and auto-reload */
140 mtspr(SPR_TCR, TCR_PIE | TCR_ARE | TCR_FIE | TCR_PERIOD);
141 }
142
143 void
144 calc_delayconst(void)
145 {
146 unsigned int processor_freq;
147
148 if (board_info_get("processor-frequency",
149 &processor_freq, sizeof(processor_freq)) == -1)
150 panic("no processor-frequency");
151
152 ticks_per_sec = processor_freq;
153 ns_per_tick = 1000000000 / ticks_per_sec;
154
155 /* Make sure that timers run at CPU frequency */
156 mtdcr(DCR_CPC0_CR1, mfdcr(DCR_CPC0_CR1) & ~CPC0_CR1_CETE);
157 }
158
159 /*
160 * Fill in *tvp with current time with microsecond resolution.
161 */
162 void
163 microtime(struct timeval *tvp)
164 {
165 u_long tb;
166 u_long ticks;
167 int msr;
168
169 asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
170 asm ("mftb %0" : "=r"(tb));
171 ticks = (tb - lasttb) * ns_per_tick;
172 *tvp = time;
173 asm volatile ("mtmsr %0" :: "r"(msr));
174 ticks /= 1000;
175 tvp->tv_usec += ticks;
176 while (tvp->tv_usec >= 1000000) {
177 tvp->tv_usec -= 1000000;
178 tvp->tv_sec++;
179 }
180 }
181
182 /*
183 * Wait for about n microseconds (at least!).
184 */
185 void
186 delay(unsigned int n)
187 {
188 u_quad_t tb;
189 u_long tbh, tbl, scratch;
190
191 tb = mftb();
192 /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
193 tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
194 tbh = tb >> 32;
195 tbl = tb;
196 asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
197 "mftb %0; cmplw %0,%2; blt 1b; 2:"
198 : "=r"(scratch) : "r"(tbh), "r"(tbl));
199 }
200
201 /*
202 * Nothing to do.
203 */
204 void
205 setstatclockrate(int arg)
206 {
207
208 /* Do nothing */
209 }
210