xref: /netbsd-src/sys/arch/arc/arc/timer.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /* $NetBSD: timer.c,v 1.6 2005/12/11 12:16:37 christos Exp $ */
2 /* NetBSD: clock.c,v 1.31 2001/05/27 13:53:24 sommerfeld Exp  */
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department and Ralph Campbell.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * from: Utah Hdr: clock.c 1.18 91/01/21
37  *
38  *	@(#)clock.c	8.1 (Berkeley) 6/10/93
39  */
40 /*
41  * Copyright (c) 1988 University of Utah.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * the Systems Programming Group of the University of Utah Computer
45  * Science Department and Ralph Campbell.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *	This product includes software developed by the University of
58  *	California, Berkeley and its contributors.
59  * 4. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  * from: Utah Hdr: clock.c 1.18 91/01/21
76  *
77  *	@(#)clock.c	8.1 (Berkeley) 6/10/93
78  */
79 
80 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
81 
82 __KERNEL_RCSID(0, "$NetBSD: timer.c,v 1.6 2005/12/11 12:16:37 christos Exp $");
83 
84 #include <sys/param.h>
85 #include <sys/kernel.h>
86 #include <sys/systm.h>
87 
88 #include <mips/locore.h>
89 
90 #include <arc/arc/timervar.h>
91 
92 struct device *timerdev;
93 const struct timerfns *timerfns;
94 int timerinitted;
95 uint32_t last_cp0_count;
96 
97 void
98 timerattach(struct device *dev, const struct timerfns *fns)
99 {
100 
101 	/*
102 	 * Just bookkeeping.
103 	 */
104 
105 	if (timerfns != NULL)
106 		panic("timerattach: multiple timers");
107 	timerdev = dev;
108 	timerfns = fns;
109 }
110 
111 /*
112  * Machine-dependent clock routines.
113  */
114 
115 /*
116  * Start the real-time and statistics clocks. Leave stathz 0 since there
117  * are no other timers available.
118  */
119 void
120 cpu_initclocks(void)
121 {
122 	if (timerfns == NULL)
123 		panic("cpu_initclocks: no timer attached");
124 
125 	tick = 1000000 / hz;	/* number of microseconds between interrupts */
126 	tickfix = 1000000 - (hz * tick);
127 	if (tickfix) {
128 		int ftp;
129 
130 		ftp = min(ffs(tickfix), ffs(hz));
131 		tickfix >>= (ftp - 1);
132 		tickfixinterval = hz >> (ftp - 1);
133         }
134 
135 	/*
136 	 * Get the clock started.
137 	 */
138 	(*timerfns->tf_init)(timerdev);
139 }
140 
141 /*
142  * We assume newhz is either stathz or profhz, and that neither will
143  * change after being set up above.  Could recalculate intervals here
144  * but that would be a drag.
145  */
146 void
147 setstatclockrate(int newhz)
148 {
149 
150 	/* nothing we can do */
151 }
152 
153 /*
154  * Wait "n" microseconds.
155  */
156 void
157 delay(unsigned int n)
158 {
159 	uint32_t cur, last, delta, usecs;
160 
161 	last = mips3_cp0_count_read();
162 	delta = usecs = 0;
163 
164 	while (n > usecs) {
165 		cur = mips3_cp0_count_read();
166 
167 		/* Check to see if the timer has wrapped around. */
168 		if (cur < last)
169  			delta += ((curcpu()->ci_cycles_per_hz - last) + cur);
170 		else
171 			delta += (cur - last);
172 
173 		last = cur;
174 
175 		if (delta >= curcpu()->ci_divisor_delay) {
176 			usecs += delta / curcpu()->ci_divisor_delay;
177 			delta %= curcpu()->ci_divisor_delay;
178 		}
179 	}
180 }
181