clock.c revision 1.2.4.4 1 1.2.4.4 nathanw /* $NetBSD: clock.c,v 1.2.4.4 2002/08/06 22:47:08 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
58 1.2.4.2 nathanw #ifdef FAST_STAT_CLOCK
59 1.2.4.2 nathanw /* Stat clock runs at ~ 1.5KHz */
60 1.2.4.2 nathanw #define PERIOD_POWER 17
61 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_17
62 1.2.4.2 nathanw #else
63 1.2.4.2 nathanw /* Stat clock runs at ~ 95Hz */
64 1.2.4.2 nathanw #define PERIOD_POWER 21
65 1.2.4.2 nathanw #define TCR_PERIOD TCR_FP_2_21
66 1.2.4.3 nathanw #endif
67 1.2.4.2 nathanw
68 1.2.4.2 nathanw
69 1.2.4.2 nathanw void
70 1.2.4.2 nathanw stat_intr(struct clockframe *frame)
71 1.2.4.2 nathanw {
72 1.2.4.2 nathanw extern u_long intrcnt[];
73 1.2.4.3 nathanw
74 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_FIS); /* Clear TSR[FIS] */
75 1.2.4.2 nathanw intrcnt[CNT_STATCLOCK]++;
76 1.2.4.2 nathanw statclock(frame);
77 1.2.4.2 nathanw }
78 1.2.4.2 nathanw
79 1.2.4.2 nathanw void
80 1.2.4.2 nathanw decr_intr(struct clockframe *frame)
81 1.2.4.2 nathanw {
82 1.2.4.2 nathanw int pri;
83 1.2.4.2 nathanw long tick, xticks;
84 1.2.4.2 nathanw int nticks;
85 1.2.4.2 nathanw extern u_long intrcnt[];
86 1.2.4.2 nathanw /*
87 1.2.4.2 nathanw * Check whether we are initialized.
88 1.2.4.2 nathanw */
89 1.2.4.2 nathanw if (!ticks_per_intr)
90 1.2.4.2 nathanw return;
91 1.2.4.2 nathanw
92 1.2.4.2 nathanw asm volatile("mftb %0":"=r"(tick):);
93 1.2.4.2 nathanw mtspr(SPR_TSR, TSR_PIS); /* Clear TSR[PIS] */
94 1.2.4.2 nathanw /*
95 1.2.4.2 nathanw * lasttb is used during microtime. Set it to the virtual
96 1.2.4.2 nathanw * start of this tick interval.
97 1.2.4.2 nathanw */
98 1.2.4.2 nathanw xticks = tick - lasttb; /* Number of TLB cycles since last exception */
99 1.2.4.2 nathanw for (nticks = 0; xticks > ticks_per_intr; nticks++)
100 1.2.4.2 nathanw xticks -= ticks_per_intr;
101 1.2.4.2 nathanw lasttb = tick - xticks;
102 1.2.4.2 nathanw
103 1.2.4.2 nathanw intrcnt[CNT_CLOCK]++;
104 1.2.4.2 nathanw pri = splclock();
105 1.2.4.2 nathanw if (pri & SPL_CLOCK) {
106 1.2.4.2 nathanw tickspending += nticks;
107 1.2.4.2 nathanw ticksmissed+= nticks;
108 1.2.4.2 nathanw } else {
109 1.2.4.2 nathanw nticks += tickspending;
110 1.2.4.2 nathanw tickspending = 0;
111 1.2.4.2 nathanw
112 1.2.4.2 nathanw /*
113 1.2.4.2 nathanw * Reenable interrupts
114 1.2.4.2 nathanw */
115 1.2.4.2 nathanw asm volatile ("wrteei 1");
116 1.2.4.2 nathanw
117 1.2.4.2 nathanw /*
118 1.2.4.2 nathanw * Do standard timer interrupt stuff.
119 1.2.4.2 nathanw * Do softclock stuff only on the last iteration.
120 1.2.4.2 nathanw */
121 1.2.4.2 nathanw frame->pri = pri | SINT_CLOCK;
122 1.2.4.2 nathanw while (--nticks > 0)
123 1.2.4.2 nathanw hardclock(frame);
124 1.2.4.2 nathanw frame->pri = pri;
125 1.2.4.2 nathanw hardclock(frame);
126 1.2.4.2 nathanw }
127 1.2.4.2 nathanw splx(pri);
128 1.2.4.2 nathanw }
129 1.2.4.2 nathanw
130 1.2.4.2 nathanw void
131 1.2.4.2 nathanw cpu_initclocks(void)
132 1.2.4.2 nathanw {
133 1.2.4.4 nathanw
134 1.2.4.2 nathanw ticks_per_intr = ticks_per_sec / hz;
135 1.2.4.4 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 /*
160 1.2.4.2 nathanw * Fill in *tvp with current time with microsecond resolution.
161 1.2.4.2 nathanw */
162 1.2.4.2 nathanw void
163 1.2.4.2 nathanw microtime(struct timeval *tvp)
164 1.2.4.2 nathanw {
165 1.2.4.2 nathanw u_long tb;
166 1.2.4.2 nathanw u_long ticks;
167 1.2.4.2 nathanw int msr;
168 1.2.4.2 nathanw
169 1.2.4.2 nathanw asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr) :);
170 1.2.4.2 nathanw asm ("mftb %0" : "=r"(tb));
171 1.2.4.2 nathanw ticks = (tb - lasttb) * ns_per_tick;
172 1.2.4.2 nathanw *tvp = time;
173 1.2.4.2 nathanw asm volatile ("mtmsr %0" :: "r"(msr));
174 1.2.4.2 nathanw ticks /= 1000;
175 1.2.4.2 nathanw tvp->tv_usec += ticks;
176 1.2.4.2 nathanw while (tvp->tv_usec >= 1000000) {
177 1.2.4.2 nathanw tvp->tv_usec -= 1000000;
178 1.2.4.2 nathanw tvp->tv_sec++;
179 1.2.4.2 nathanw }
180 1.2.4.2 nathanw }
181 1.2.4.2 nathanw
182 1.2.4.2 nathanw /*
183 1.2.4.2 nathanw * Wait for about n microseconds (at least!).
184 1.2.4.2 nathanw */
185 1.2.4.2 nathanw void
186 1.2.4.2 nathanw delay(unsigned int n)
187 1.2.4.2 nathanw {
188 1.2.4.2 nathanw u_quad_t tb;
189 1.2.4.2 nathanw u_long tbh, tbl, scratch;
190 1.2.4.2 nathanw
191 1.2.4.2 nathanw tb = mftb();
192 1.2.4.2 nathanw /* use 1000ULL to force 64 bit math to avoid 32 bit overflows */
193 1.2.4.2 nathanw tb += (n * 1000ULL + ns_per_tick - 1) / ns_per_tick;
194 1.2.4.2 nathanw tbh = tb >> 32;
195 1.2.4.2 nathanw tbl = tb;
196 1.2.4.2 nathanw asm volatile ("1: mftbu %0; cmplw %0,%1; blt 1b; bgt 2f;"
197 1.2.4.2 nathanw "mftb %0; cmplw %0,%2; blt 1b; 2:"
198 1.2.4.2 nathanw : "=r"(scratch) : "r"(tbh), "r"(tbl));
199 1.2.4.2 nathanw }
200 1.2.4.2 nathanw
201 1.2.4.2 nathanw /*
202 1.2.4.2 nathanw * Nothing to do.
203 1.2.4.2 nathanw */
204 1.2.4.2 nathanw void
205 1.2.4.2 nathanw setstatclockrate(int arg)
206 1.2.4.2 nathanw {
207 1.2.4.2 nathanw
208 1.2.4.2 nathanw /* Do nothing */
209 1.2.4.2 nathanw }
210