]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/booke/clock.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / powerpc / booke / clock.c
1 /*-
2  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3  * Copyright (C) 1995, 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by TooLs GmbH.
17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *      $NetBSD: clock.c,v 1.9 2000/01/19 02:52:19 msaitoh Exp $
32  */
33 /*
34  * Copyright (C) 2001 Benno Rice.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/sysctl.h>
65 #include <sys/bus.h>
66 #include <sys/ktr.h>
67 #include <sys/pcpu.h>
68 #include <sys/timetc.h>
69 #include <sys/interrupt.h>
70
71 #include <machine/clock.h>
72 #include <machine/platform.h>
73 #include <machine/psl.h>
74 #include <machine/spr.h>
75 #include <machine/cpu.h>
76 #include <machine/intr.h>
77 #include <machine/md_var.h>
78
79 /*
80  * Initially we assume a processor with a bus frequency of 12.5 MHz.
81  */
82 u_int tickspending;
83 u_long ns_per_tick = 80;
84 static u_long ticks_per_sec = 12500000;
85 static long ticks_per_intr;
86
87 #define DIFF19041970    2082844800
88
89 static timecounter_get_t decr_get_timecount;
90
91 static struct timecounter       decr_timecounter = {
92         decr_get_timecount,     /* get_timecount */
93         0,                      /* no poll_pps */
94         ~0u,                    /* counter_mask */
95         0,                      /* frequency */
96         "decrementer"           /* name */
97 };
98
99 void
100 decr_intr(struct trapframe *frame)
101 {
102
103         /*
104          * Check whether we are initialized.
105          */
106         if (!ticks_per_intr)
107                 return;
108
109         /*
110          * Interrupt handler must reset DIS to avoid getting another
111          * interrupt once EE is enabled.
112          */
113         mtspr(SPR_TSR, TSR_DIS);
114
115         CTR1(KTR_INTR, "%s: DEC interrupt", __func__);
116
117         if (PCPU_GET(cpuid) == 0)
118                 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
119         else
120                 hardclock_cpu(TRAPF_USERMODE(frame));
121
122         statclock(TRAPF_USERMODE(frame));
123         if (profprocs != 0)
124                 profclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
125 }
126
127 void
128 cpu_initclocks(void)
129 {
130
131         decr_tc_init();
132         stathz = hz;
133         profhz = hz;
134 }
135
136 void
137 decr_init(void)
138 {
139         struct cpuref cpu;
140         unsigned int msr;
141
142         if (platform_smp_get_bsp(&cpu) != 0)
143                 platform_smp_first_cpu(&cpu);
144         ticks_per_sec = platform_timebase_freq(&cpu);
145
146         msr = mfmsr();
147         mtmsr(msr & ~(PSL_EE));
148
149         ns_per_tick = 1000000000 / ticks_per_sec;
150         ticks_per_intr = ticks_per_sec / hz;
151
152         mtdec(ticks_per_intr);
153
154         mtspr(SPR_DECAR, ticks_per_intr);
155         mtspr(SPR_TCR, mfspr(SPR_TCR) | TCR_DIE | TCR_ARE);
156
157         set_cputicker(mftb, ticks_per_sec, 0);
158
159         mtmsr(msr);
160 }
161
162 #ifdef SMP
163 void
164 decr_ap_init(void)
165 {
166
167         /* Set auto-reload value and enable DEC interrupts in TCR */
168         mtspr(SPR_DECAR, ticks_per_intr);
169         mtspr(SPR_TCR, mfspr(SPR_TCR) | TCR_DIE | TCR_ARE);
170
171         CTR2(KTR_INTR, "%s: set TCR=%p", __func__, mfspr(SPR_TCR));
172 }
173 #endif
174
175 void
176 decr_tc_init(void)
177 {
178
179         decr_timecounter.tc_frequency = ticks_per_sec;
180         tc_init(&decr_timecounter);
181 }
182
183 static unsigned
184 decr_get_timecount(struct timecounter *tc)
185 {
186         quad_t tb;
187
188         tb = mftb();
189         return (tb);
190 }
191
192 /*
193  * Wait for about n microseconds (at least!).
194  */
195 void
196 DELAY(int n)
197 {
198         u_quad_t start, end, now;
199
200         start = mftb();
201         end = start + (u_quad_t)ticks_per_sec / (1000000ULL / n);
202         do {
203                 now = mftb();
204         } while (now < end || (now > start && end < start));
205 }
206
207 /*
208  * Nothing to do.
209  */
210 void
211 cpu_startprofclock(void)
212 {
213
214         /* Do nothing */
215 }
216
217 void
218 cpu_stopprofclock(void)
219 {
220
221 }