ofwgencfg_clock.c revision 1.4 1 1.4 lukem /* $NetBSD: ofwgencfg_clock.c,v 1.4 2003/07/15 00:24:48 lukem Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright 1997
5 1.1 thorpej * Digital Equipment Corporation. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software is furnished under license and may be used and
8 1.1 thorpej * copied only in accordance with the following terms and conditions.
9 1.1 thorpej * Subject to these conditions, you may download, copy, install,
10 1.1 thorpej * use, modify and distribute this software in source and/or binary
11 1.1 thorpej * form. No title or ownership is transferred hereby.
12 1.1 thorpej *
13 1.1 thorpej * 1) Any source code used, modified or distributed must reproduce
14 1.1 thorpej * and retain this copyright notice and list of conditions as
15 1.1 thorpej * they appear in the source file.
16 1.1 thorpej *
17 1.1 thorpej * 2) No right is granted to use any trade name, trademark, or logo of
18 1.1 thorpej * Digital Equipment Corporation. Neither the "Digital Equipment
19 1.1 thorpej * Corporation" name nor any trademark or logo of Digital Equipment
20 1.1 thorpej * Corporation may be used to endorse or promote products derived
21 1.1 thorpej * from this software without the prior written permission of
22 1.1 thorpej * Digital Equipment Corporation.
23 1.1 thorpej *
24 1.1 thorpej * 3) This software is provided "AS-IS" and any express or implied
25 1.1 thorpej * warranties, including but not limited to, any implied warranties
26 1.1 thorpej * of merchantability, fitness for a particular purpose, or
27 1.1 thorpej * non-infringement are disclaimed. In no event shall DIGITAL be
28 1.1 thorpej * liable for any damages whatsoever, and in particular, DIGITAL
29 1.1 thorpej * shall not be liable for special, indirect, consequential, or
30 1.1 thorpej * incidental damages or damages for lost profits, loss of
31 1.1 thorpej * revenue or loss of use, whether such damages arise in contract,
32 1.1 thorpej * negligence, tort, under statute, in equity, at law or otherwise,
33 1.1 thorpej * even if advised of the possibility of such damage.
34 1.1 thorpej */
35 1.1 thorpej
36 1.1 thorpej /* Include header files */
37 1.4 lukem
38 1.4 lukem #include <sys/cdefs.h>
39 1.4 lukem __KERNEL_RCSID(0, "$NetBSD: ofwgencfg_clock.c,v 1.4 2003/07/15 00:24:48 lukem Exp $");
40 1.1 thorpej
41 1.1 thorpej #include <sys/types.h>
42 1.1 thorpej #include <sys/param.h>
43 1.1 thorpej #include <sys/systm.h>
44 1.1 thorpej #include <sys/kernel.h>
45 1.1 thorpej #include <sys/time.h>
46 1.1 thorpej
47 1.1 thorpej #include <machine/intr.h>
48 1.1 thorpej #include <arm/cpufunc.h>
49 1.1 thorpej #include <machine/cpu.h>
50 1.1 thorpej #include <machine/ofw.h>
51 1.1 thorpej
52 1.1 thorpej static void *clockirq;
53 1.1 thorpej
54 1.1 thorpej
55 1.1 thorpej /*
56 1.1 thorpej * int clockhandler(struct clockframe *frame)
57 1.1 thorpej *
58 1.1 thorpej * Function called by timer 0 interrupts. This just calls
59 1.1 thorpej * hardclock(). Eventually the irqhandler can call hardclock() directly
60 1.1 thorpej * but for now we use this function so that we can debug IRQ's
61 1.1 thorpej */
62 1.1 thorpej
63 1.1 thorpej int
64 1.1 thorpej clockhandler(frame)
65 1.1 thorpej struct clockframe *frame;
66 1.1 thorpej {
67 1.1 thorpej
68 1.1 thorpej hardclock(frame);
69 1.1 thorpej return(0); /* Pass the interrupt on down the chain */
70 1.1 thorpej }
71 1.1 thorpej
72 1.1 thorpej
73 1.1 thorpej /*
74 1.1 thorpej * int statclockhandler(struct clockframe *frame)
75 1.1 thorpej *
76 1.1 thorpej * Function called by timer 1 interrupts. This just calls
77 1.1 thorpej * statclock(). Eventually the irqhandler can call statclock() directly
78 1.1 thorpej * but for now we use this function so that we can debug IRQ's
79 1.1 thorpej */
80 1.1 thorpej
81 1.1 thorpej int
82 1.1 thorpej statclockhandler(frame)
83 1.1 thorpej struct clockframe *frame;
84 1.1 thorpej {
85 1.1 thorpej
86 1.1 thorpej statclock(frame);
87 1.1 thorpej return(0); /* Pass the interrupt on down the chain */
88 1.1 thorpej }
89 1.1 thorpej
90 1.1 thorpej
91 1.1 thorpej /*
92 1.1 thorpej * void setstatclockrate(int hz)
93 1.1 thorpej *
94 1.1 thorpej * Set the stat clock rate. The stat clock uses timer1
95 1.1 thorpej */
96 1.1 thorpej
97 1.1 thorpej void
98 1.1 thorpej setstatclockrate(hz)
99 1.1 thorpej int hz;
100 1.1 thorpej {
101 1.1 thorpej
102 1.1 thorpej #ifdef OFWGENCFG
103 1.1 thorpej printf("Not setting statclock: OFW generic has only one clock.\n");
104 1.1 thorpej #endif
105 1.1 thorpej }
106 1.1 thorpej
107 1.1 thorpej
108 1.1 thorpej /*
109 1.1 thorpej * void cpu_initclocks(void)
110 1.1 thorpej *
111 1.1 thorpej * Initialise the clocks.
112 1.1 thorpej * This sets up the two timers in the IOMD and installs the IRQ handlers
113 1.1 thorpej *
114 1.1 thorpej * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
115 1.1 thorpej */
116 1.1 thorpej
117 1.1 thorpej void
118 1.1 thorpej cpu_initclocks()
119 1.1 thorpej {
120 1.1 thorpej /*
121 1.1 thorpej * Load timer 0 with count down value
122 1.1 thorpej * This timer generates 100Hz interrupts for the system clock
123 1.1 thorpej */
124 1.1 thorpej
125 1.1 thorpej printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
126 1.1 thorpej
127 1.1 thorpej clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
128 1.1 thorpej clockhandler, 0);
129 1.1 thorpej if (clockirq == NULL)
130 1.3 provos panic("Cannot installer timer 0 IRQ handler");
131 1.1 thorpej
132 1.1 thorpej /* Notify callback handler that it can start processing ticks. */
133 1.1 thorpej ofw_handleticks = 1;
134 1.1 thorpej
135 1.1 thorpej if (stathz) {
136 1.1 thorpej printf("Not installing statclock: OFW generic has only one clock.\n");
137 1.1 thorpej }
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej
141 1.1 thorpej /*
142 1.1 thorpej * void microtime(struct timeval *tvp)
143 1.1 thorpej *
144 1.1 thorpej * Fill in the specified timeval struct with the current time
145 1.1 thorpej * accurate to the microsecond.
146 1.1 thorpej */
147 1.1 thorpej
148 1.1 thorpej void
149 1.1 thorpej microtime(tvp)
150 1.1 thorpej struct timeval *tvp;
151 1.1 thorpej {
152 1.1 thorpej int s;
153 1.1 thorpej static struct timeval oldtv;
154 1.1 thorpej
155 1.1 thorpej s = splhigh();
156 1.1 thorpej
157 1.1 thorpej /* Fill in the timeval struct */
158 1.1 thorpej
159 1.1 thorpej *tvp = time;
160 1.1 thorpej
161 1.1 thorpej /* Make sure the micro seconds don't overflow. */
162 1.1 thorpej
163 1.1 thorpej while (tvp->tv_usec >= 1000000) {
164 1.1 thorpej tvp->tv_usec -= 1000000;
165 1.1 thorpej ++tvp->tv_sec;
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej /* Make sure the time has advanced. */
169 1.1 thorpej
170 1.1 thorpej if (tvp->tv_sec == oldtv.tv_sec &&
171 1.1 thorpej tvp->tv_usec <= oldtv.tv_usec) {
172 1.1 thorpej tvp->tv_usec = oldtv.tv_usec + 1;
173 1.1 thorpej if (tvp->tv_usec >= 1000000) {
174 1.1 thorpej tvp->tv_usec -= 1000000;
175 1.1 thorpej ++tvp->tv_sec;
176 1.1 thorpej }
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej
180 1.1 thorpej oldtv = *tvp;
181 1.1 thorpej (void)splx(s);
182 1.1 thorpej }
183 1.1 thorpej
184 1.1 thorpej /*
185 1.1 thorpej * Estimated loop for n microseconds
186 1.1 thorpej */
187 1.1 thorpej
188 1.1 thorpej /* Need to re-write this to use the timers */
189 1.1 thorpej
190 1.1 thorpej /* One day soon I will actually do this */
191 1.1 thorpej
192 1.1 thorpej int delaycount = 50;
193 1.1 thorpej
194 1.1 thorpej void
195 1.1 thorpej delay(n)
196 1.1 thorpej u_int n;
197 1.1 thorpej {
198 1.1 thorpej u_int i;
199 1.1 thorpej
200 1.1 thorpej if (n == 0) return;
201 1.2 mycroft while (n-- > 0) {
202 1.1 thorpej if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */
203 1.1 thorpej for (i = delaycount; --i;);
204 1.1 thorpej else
205 1.1 thorpej for (i = 8; --i;);
206 1.1 thorpej }
207 1.1 thorpej }
208