ofwgencfg_clock.c revision 1.5 1 1.5 thorpej /* $NetBSD: ofwgencfg_clock.c,v 1.5 2004/08/29 17:37:03 thorpej 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.5 thorpej __KERNEL_RCSID(0, "$NetBSD: ofwgencfg_clock.c,v 1.5 2004/08/29 17:37:03 thorpej 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.5 thorpej
63 1.5 thorpej static int
64 1.5 thorpej clockhandler(struct clockframe *frame)
65 1.1 thorpej {
66 1.1 thorpej
67 1.1 thorpej hardclock(frame);
68 1.1 thorpej return(0); /* Pass the interrupt on down the chain */
69 1.1 thorpej }
70 1.1 thorpej
71 1.5 thorpej #if 0
72 1.1 thorpej /*
73 1.1 thorpej * int statclockhandler(struct clockframe *frame)
74 1.1 thorpej *
75 1.1 thorpej * Function called by timer 1 interrupts. This just calls
76 1.1 thorpej * statclock(). Eventually the irqhandler can call statclock() directly
77 1.1 thorpej * but for now we use this function so that we can debug IRQ's
78 1.1 thorpej */
79 1.5 thorpej
80 1.1 thorpej int
81 1.1 thorpej statclockhandler(frame)
82 1.1 thorpej struct clockframe *frame;
83 1.1 thorpej {
84 1.1 thorpej
85 1.1 thorpej statclock(frame);
86 1.1 thorpej return(0); /* Pass the interrupt on down the chain */
87 1.1 thorpej }
88 1.5 thorpej #endif
89 1.1 thorpej
90 1.1 thorpej /*
91 1.1 thorpej * void setstatclockrate(int hz)
92 1.1 thorpej *
93 1.1 thorpej * Set the stat clock rate. The stat clock uses timer1
94 1.1 thorpej */
95 1.1 thorpej
96 1.1 thorpej void
97 1.1 thorpej setstatclockrate(hz)
98 1.1 thorpej int hz;
99 1.1 thorpej {
100 1.1 thorpej
101 1.1 thorpej #ifdef OFWGENCFG
102 1.1 thorpej printf("Not setting statclock: OFW generic has only one clock.\n");
103 1.1 thorpej #endif
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej
107 1.1 thorpej /*
108 1.1 thorpej * void cpu_initclocks(void)
109 1.1 thorpej *
110 1.1 thorpej * Initialise the clocks.
111 1.1 thorpej * This sets up the two timers in the IOMD and installs the IRQ handlers
112 1.1 thorpej *
113 1.1 thorpej * NOTE: Currently only timer 0 is setup and the IRQ handler is not installed
114 1.1 thorpej */
115 1.1 thorpej
116 1.1 thorpej void
117 1.1 thorpej cpu_initclocks()
118 1.1 thorpej {
119 1.1 thorpej /*
120 1.1 thorpej * Load timer 0 with count down value
121 1.1 thorpej * This timer generates 100Hz interrupts for the system clock
122 1.1 thorpej */
123 1.1 thorpej
124 1.1 thorpej printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
125 1.1 thorpej
126 1.1 thorpej clockirq = intr_claim(IRQ_TIMER0, IPL_CLOCK, "tmr0 hard clk",
127 1.5 thorpej (int (*)(void *))clockhandler, 0);
128 1.1 thorpej if (clockirq == NULL)
129 1.3 provos panic("Cannot installer timer 0 IRQ handler");
130 1.1 thorpej
131 1.1 thorpej /* Notify callback handler that it can start processing ticks. */
132 1.1 thorpej ofw_handleticks = 1;
133 1.1 thorpej
134 1.1 thorpej if (stathz) {
135 1.1 thorpej printf("Not installing statclock: OFW generic has only one clock.\n");
136 1.1 thorpej }
137 1.1 thorpej }
138 1.1 thorpej
139 1.1 thorpej
140 1.1 thorpej /*
141 1.1 thorpej * void microtime(struct timeval *tvp)
142 1.1 thorpej *
143 1.1 thorpej * Fill in the specified timeval struct with the current time
144 1.1 thorpej * accurate to the microsecond.
145 1.1 thorpej */
146 1.1 thorpej
147 1.1 thorpej void
148 1.1 thorpej microtime(tvp)
149 1.1 thorpej struct timeval *tvp;
150 1.1 thorpej {
151 1.1 thorpej int s;
152 1.1 thorpej static struct timeval oldtv;
153 1.1 thorpej
154 1.1 thorpej s = splhigh();
155 1.1 thorpej
156 1.1 thorpej /* Fill in the timeval struct */
157 1.1 thorpej
158 1.1 thorpej *tvp = time;
159 1.1 thorpej
160 1.1 thorpej /* Make sure the micro seconds don't overflow. */
161 1.1 thorpej
162 1.1 thorpej while (tvp->tv_usec >= 1000000) {
163 1.1 thorpej tvp->tv_usec -= 1000000;
164 1.1 thorpej ++tvp->tv_sec;
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej /* Make sure the time has advanced. */
168 1.1 thorpej
169 1.1 thorpej if (tvp->tv_sec == oldtv.tv_sec &&
170 1.1 thorpej tvp->tv_usec <= oldtv.tv_usec) {
171 1.1 thorpej tvp->tv_usec = oldtv.tv_usec + 1;
172 1.1 thorpej if (tvp->tv_usec >= 1000000) {
173 1.1 thorpej tvp->tv_usec -= 1000000;
174 1.1 thorpej ++tvp->tv_sec;
175 1.1 thorpej }
176 1.1 thorpej }
177 1.1 thorpej
178 1.1 thorpej
179 1.1 thorpej oldtv = *tvp;
180 1.1 thorpej (void)splx(s);
181 1.1 thorpej }
182 1.1 thorpej
183 1.1 thorpej /*
184 1.1 thorpej * Estimated loop for n microseconds
185 1.1 thorpej */
186 1.1 thorpej
187 1.1 thorpej /* Need to re-write this to use the timers */
188 1.1 thorpej
189 1.1 thorpej /* One day soon I will actually do this */
190 1.1 thorpej
191 1.1 thorpej int delaycount = 50;
192 1.1 thorpej
193 1.1 thorpej void
194 1.1 thorpej delay(n)
195 1.1 thorpej u_int n;
196 1.1 thorpej {
197 1.1 thorpej u_int i;
198 1.1 thorpej
199 1.1 thorpej if (n == 0) return;
200 1.2 mycroft while (n-- > 0) {
201 1.1 thorpej if (cputype == CPU_ID_SA110) /* XXX - Seriously gross hack */
202 1.1 thorpej for (i = delaycount; --i;);
203 1.1 thorpej else
204 1.1 thorpej for (i = 8; --i;);
205 1.1 thorpej }
206 1.1 thorpej }
207