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