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