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