clock_ebus.c revision 1.6 1 1.6 tsutsui /* $NetBSD: clock_ebus.c,v 1.6 2011/06/12 04:44:27 tsutsui Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code was written by Alessandro Forin and Neil Pittman
8 1.1 pooka * at Microsoft Research and contributed to The NetBSD Foundation
9 1.1 pooka * by Microsoft Corporation.
10 1.1 pooka *
11 1.1 pooka * Redistribution and use in source and binary forms, with or without
12 1.1 pooka * modification, are permitted provided that the following conditions
13 1.1 pooka * are met:
14 1.1 pooka * 1. Redistributions of source code must retain the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer.
16 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pooka * notice, this list of conditions and the following disclaimer in the
18 1.1 pooka * documentation and/or other materials provided with the distribution.
19 1.1 pooka *
20 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
31 1.1 pooka */
32 1.1 pooka
33 1.1 pooka #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 1.6 tsutsui __KERNEL_RCSID(0, "$NetBSD: clock_ebus.c,v 1.6 2011/06/12 04:44:27 tsutsui Exp $");
35 1.1 pooka
36 1.1 pooka #include <sys/param.h>
37 1.1 pooka #include <sys/kernel.h>
38 1.1 pooka #include <sys/device.h>
39 1.1 pooka #include <sys/systm.h>
40 1.1 pooka #include <sys/timetc.h>
41 1.1 pooka
42 1.1 pooka #include <dev/clock_subr.h>
43 1.1 pooka
44 1.1 pooka #include <emips/ebus/ebusvar.h>
45 1.1 pooka #include <emips/emips/machdep.h>
46 1.1 pooka #include <machine/emipsreg.h>
47 1.1 pooka
48 1.1 pooka /*
49 1.1 pooka * Device softc
50 1.1 pooka */
51 1.1 pooka struct eclock_softc {
52 1.6 tsutsui device_t sc_dev;
53 1.1 pooka struct _Tc *sc_dp;
54 1.4 tsutsui uint32_t sc_reload;
55 1.2 tsutsui struct timecounter sc_tc;
56 1.2 tsutsui struct todr_chip_handle sc_todr;
57 1.1 pooka };
58 1.1 pooka
59 1.2 tsutsui static int eclock_ebus_match(device_t, cfdata_t, void *);
60 1.2 tsutsui static void eclock_ebus_attach(device_t, device_t, void *);
61 1.1 pooka
62 1.6 tsutsui CFATTACH_DECL_NEW(eclock_ebus, sizeof (struct eclock_softc),
63 1.1 pooka eclock_ebus_match, eclock_ebus_attach, NULL, NULL);
64 1.1 pooka
65 1.2 tsutsui void eclock_init(device_t);
66 1.1 pooka
67 1.2 tsutsui static void __eclock_init(device_t);
68 1.2 tsutsui static int eclock_gettime(struct todr_chip_handle *, struct timeval *);
69 1.2 tsutsui static int eclock_settime(struct todr_chip_handle *, struct timeval *);
70 1.2 tsutsui static int eclock_ebus_intr(void *, void *);
71 1.2 tsutsui static u_int eclock_counter(struct timecounter *);
72 1.2 tsutsui
73 1.2 tsutsui /* BUGBUG resolve the gap between cpu_initclocks() and eclock_init(x) */
74 1.2 tsutsui device_t clockdev = NULL;
75 1.1 pooka
76 1.1 pooka void
77 1.2 tsutsui eclock_init(device_t dev)
78 1.1 pooka {
79 1.2 tsutsui
80 1.2 tsutsui if (dev == NULL)
81 1.2 tsutsui dev = clockdev;
82 1.2 tsutsui if (dev == NULL)
83 1.2 tsutsui panic("eclock_init");
84 1.2 tsutsui __eclock_init(dev);
85 1.1 pooka }
86 1.1 pooka
87 1.1 pooka static void
88 1.2 tsutsui __eclock_init(device_t dev)
89 1.1 pooka {
90 1.6 tsutsui struct eclock_softc *sc = device_private(dev);
91 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
92 1.4 tsutsui uint32_t reload = 10 * 1000000; /* 1sec in 100ns units (10MHz clock) */
93 1.1 pooka
94 1.2 tsutsui /*
95 1.2 tsutsui * Compute reload according to whatever value passed in,
96 1.2 tsutsui * Warn if fractional
97 1.2 tsutsui */
98 1.2 tsutsui if (hz > 1) {
99 1.2 tsutsui uint32_t r = reload / hz;
100 1.2 tsutsui if ((r * hz) != reload)
101 1.2 tsutsui printf("%s: %d Hz clock will cause roundoffs"
102 1.2 tsutsui " with 10MHz xtal (%d)\n",
103 1.6 tsutsui device_xname(sc->sc_dev), hz, reload - (r * hz));
104 1.1 pooka reload = r;
105 1.1 pooka }
106 1.1 pooka
107 1.4 tsutsui sc->sc_reload = reload;
108 1.1 pooka
109 1.2 tsutsui /* Start the counter */
110 1.2 tsutsui tc->DownCounterHigh = 0;
111 1.4 tsutsui tc->DownCounter = sc->sc_reload;
112 1.2 tsutsui tc->Control = TCCT_ENABLE | TCCT_INT_ENABLE;
113 1.1 pooka }
114 1.1 pooka
115 1.1 pooka /*
116 1.1 pooka * Get the time of day, based on the clock's value and/or the base value.
117 1.1 pooka * NB: At 10MHz, our 64bits FreeRunning is worth 58,426 years.
118 1.1 pooka */
119 1.1 pooka
120 1.1 pooka extern u_quad_t __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq);
121 1.1 pooka
122 1.1 pooka
123 1.1 pooka static int
124 1.1 pooka eclock_gettime(struct todr_chip_handle *todr, struct timeval *tv)
125 1.1 pooka {
126 1.2 tsutsui struct eclock_softc *sc = todr->cookie;
127 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
128 1.2 tsutsui uint64_t free;
129 1.2 tsutsui int s;
130 1.2 tsutsui
131 1.2 tsutsui /*
132 1.2 tsutsui * 32bit processor, guard against interrupts in the middle of
133 1.2 tsutsui * reading this 64bit entity
134 1.2 tsutsui */
135 1.2 tsutsui /* BUGBUG Should read it "twice" to guard against rollover too. */
136 1.1 pooka s = splhigh();
137 1.2 tsutsui free = tc->FreeRunning;
138 1.1 pooka splx(s);
139 1.1 pooka
140 1.2 tsutsui /*
141 1.2 tsutsui * Big fight with the compiler here, it gets very confused by 64bits.
142 1.2 tsutsui */
143 1.1 pooka #if 0
144 1.2 tsutsui /*
145 1.2 tsutsui * This is in C:
146 1.2 tsutsui */
147 1.2 tsutsui {
148 1.2 tsutsui uint64_t freeS, freeU;
149 1.2 tsutsui freeS = free / (10 * 1000 * 1000);
150 1.2 tsutsui freeU = free % (10 * 1000 * 1000);
151 1.2 tsutsui tv->tv_sec = freeS;
152 1.2 tsutsui tv->tv_usec = freeU / 10;
153 1.2 tsutsui #if 0
154 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
155 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n",
156 1.2 tsutsui tv->tv_sec, tv->tv_usec, freeS, freeU, free);
157 1.2 tsutsui #endif
158 1.2 tsutsui }
159 1.1 pooka #else
160 1.2 tsutsui /*
161 1.2 tsutsui * And this is in assembly :-)
162 1.2 tsutsui */
163 1.2 tsutsui {
164 1.2 tsutsui u_quad_t r;
165 1.2 tsutsui u_quad_t d = __qdivrem(free,(u_quad_t)10000000,&r);
166 1.2 tsutsui uint32_t su, uu;
167 1.2 tsutsui su = (uint32_t)d;
168 1.2 tsutsui uu = (uint32_t)r;
169 1.2 tsutsui uu = uu / 10; /* in usecs */
170 1.2 tsutsui tv->tv_sec = su;
171 1.2 tsutsui tv->tv_usec = uu;
172 1.2 tsutsui #if 0
173 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
174 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n",
175 1.2 tsutsui tv->tv_sec, tv->tv_usec, d, r, free);
176 1.2 tsutsui #endif
177 1.2 tsutsui }
178 1.1 pooka #endif
179 1.1 pooka
180 1.2 tsutsui return 0;
181 1.1 pooka }
182 1.1 pooka
183 1.1 pooka /*
184 1.1 pooka * Reset the TODR based on the time value.
185 1.1 pooka */
186 1.1 pooka static int
187 1.1 pooka eclock_settime(struct todr_chip_handle *todr, struct timeval *tv)
188 1.1 pooka {
189 1.2 tsutsui struct eclock_softc *sc = todr->cookie;
190 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
191 1.4 tsutsui uint64_t free, su;
192 1.4 tsutsui uint32_t uu;
193 1.2 tsutsui int s;
194 1.1 pooka
195 1.2 tsutsui /* Careful with what we do here, else the compilerbugs hit hard */
196 1.1 pooka s = splhigh();
197 1.1 pooka
198 1.4 tsutsui su = (uint64_t)tv->tv_sec; /* 0(tv) */
199 1.4 tsutsui uu = (uint32_t)tv->tv_usec; /* 8(tv) */
200 1.1 pooka
201 1.1 pooka
202 1.4 tsutsui free = su * 10 * 1000 * 1000;
203 1.2 tsutsui free += uu * 10;
204 1.1 pooka
205 1.2 tsutsui tc->FreeRunning = free;
206 1.1 pooka splx(s);
207 1.1 pooka
208 1.1 pooka #if 0
209 1.2 tsutsui /*
210 1.1 pooka Should compile to something like this:
211 1.1 pooka 80260c84 <eclock_settime>:
212 1.1 pooka 80260c84: 27bdffc0 addiu sp,sp,-64
213 1.1 pooka 80260c88: afbf0038 sw ra,56(sp)
214 1.1 pooka 80260c8c: afb40030 sw s4,48(sp)
215 1.1 pooka 80260c90: afb3002c sw s3,44(sp)
216 1.1 pooka 80260c94: afb20028 sw s2,40(sp)
217 1.1 pooka 80260c98: afb10024 sw s1,36(sp)
218 1.1 pooka 80260c9c: afb00020 sw s0,32(sp)
219 1.1 pooka 80260ca0: afb50034 sw s5,52(sp)
220 1.1 pooka 80260ca4: 8c820000 lw v0,0(a0)
221 1.1 pooka 80260ca8: 00a09021 move s2,a1
222 1.1 pooka 80260cac: 8c55003c lw s5,60(v0) //s5=tc
223 1.1 pooka 80260cb0: 0c004122 jal 80010488 <_splraise>
224 1.1 pooka 80260cb4: 3404ff00 li a0,0xff00
225 1.1 pooka 80260cb8: 8e540000 lw s4,0(s2) //s4=tv->tv_sec=us
226 1.1 pooka 80260cbc: 3c060098 lui a2,0x98
227 1.1 pooka 80260cc0: 34c69680 ori a2,a2,0x9680 //a2=10000000
228 1.1 pooka 80260cc4: 02860019 multu s4,a2 //free=us*10000000
229 1.1 pooka 80260cc8: 8e530004 lw s3,4(s2) //s3=uu
230 1.1 pooka 80260ccc: 00402021 move a0,v0 //s=splhigh()
231 1.1 pooka 80260cd0: 001328c0 sll a1,s3,0x3
232 1.1 pooka 80260cd4: 00131040 sll v0,s3,0x1
233 1.1 pooka 80260cd8: 00451021 addu v0,v0,a1
234 1.1 pooka 80260cdc: 00401821 move v1,v0 //v1 = uu*10
235 1.1 pooka 80260ce0: 00001021 move v0,zero
236 1.1 pooka 80260ce4: 00003812 mflo a3 //a3=low(free)
237 1.1 pooka 80260ce8: 00e38821 addu s1,a3,v1 //s1=low(free)+(uu*10)
238 1.1 pooka 80260cec: 0227282b sltu a1,s1,a3 //a1=overflow bit
239 1.1 pooka 80260cf0: 00003010 mfhi a2 //a2=high(free)
240 1.1 pooka 80260cf4: 00c28021 addu s0,a2,v0 //s0=a2=high(free) [useless, v0=0]
241 1.1 pooka 80260cf8: 00b08021 addu s0,a1,s0 //s0+=overflow bit
242 1.1 pooka 80260cfc: aeb1000c sw s1,12(s5)
243 1.1 pooka 80260d00: aeb00008 sw s0,8(s5)
244 1.1 pooka 80260d04: 0c00413f jal 800104fc <_splset>
245 1.1 pooka 80260d08: 00000000 nop
246 1.2 tsutsui */
247 1.2 tsutsui #endif
248 1.1 pooka
249 1.2 tsutsui #if 0
250 1.2 tsutsui printf("est: s x%" PRIx64 " u x%lx (%d %d), free %" PRId64 "\n",
251 1.2 tsutsui tv->tv_sec, tv->tv_usec, su, uu, free);
252 1.1 pooka #endif
253 1.1 pooka
254 1.2 tsutsui return 0;
255 1.1 pooka }
256 1.1 pooka
257 1.1 pooka static int
258 1.1 pooka eclock_ebus_intr(void *cookie, void *f)
259 1.1 pooka {
260 1.1 pooka struct eclock_softc *sc = cookie;
261 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
262 1.2 tsutsui struct clockframe *cf = f;
263 1.2 tsutsui volatile uint32_t x;
264 1.2 tsutsui
265 1.2 tsutsui x = tc->Control;
266 1.2 tsutsui tc->DownCounterHigh = 0;
267 1.4 tsutsui tc->DownCounter = sc->sc_reload;
268 1.1 pooka
269 1.2 tsutsui hardclock(cf);
270 1.2 tsutsui emips_clock_evcnt.ev_count++;
271 1.1 pooka
272 1.2 tsutsui return 0;
273 1.1 pooka }
274 1.1 pooka
275 1.1 pooka static u_int
276 1.1 pooka eclock_counter(struct timecounter *tc)
277 1.1 pooka {
278 1.1 pooka struct eclock_softc *sc = tc->tc_priv;
279 1.2 tsutsui struct _Tc *Tc = sc->sc_dp;
280 1.2 tsutsui
281 1.2 tsutsui return (u_int)Tc->FreeRunning; /* NB: chops to 32bits */
282 1.1 pooka }
283 1.1 pooka
284 1.1 pooka
285 1.1 pooka static int
286 1.2 tsutsui eclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
287 1.1 pooka {
288 1.1 pooka struct ebus_attach_args *ia = aux;
289 1.2 tsutsui struct _Tc *mc = (struct _Tc *)ia->ia_vaddr;
290 1.1 pooka
291 1.1 pooka if (strcmp("eclock", ia->ia_name) != 0)
292 1.2 tsutsui return 0;
293 1.2 tsutsui if ((mc == NULL) ||
294 1.2 tsutsui (mc->Tag != PMTTAG_TIMER))
295 1.2 tsutsui return 0;
296 1.1 pooka
297 1.2 tsutsui return 1;
298 1.1 pooka }
299 1.1 pooka
300 1.1 pooka static void
301 1.2 tsutsui eclock_ebus_attach(device_t parent, device_t self, void *aux)
302 1.1 pooka {
303 1.6 tsutsui struct eclock_softc *sc = device_private(self);
304 1.2 tsutsui struct ebus_attach_args *ia = aux;
305 1.1 pooka
306 1.6 tsutsui sc->sc_dev = self;
307 1.2 tsutsui sc->sc_dp = (struct _Tc *)ia->ia_vaddr;
308 1.1 pooka
309 1.2 tsutsui /* NB: We are chopping our 64bit free-running down to 32bits */
310 1.2 tsutsui sc->sc_tc.tc_get_timecount = eclock_counter;
311 1.2 tsutsui sc->sc_tc.tc_poll_pps = 0;
312 1.2 tsutsui sc->sc_tc.tc_counter_mask = 0xffffffff;
313 1.5 tsutsui sc->sc_tc.tc_frequency = 10 * 1000 * 1000; /* 10 MHz */
314 1.2 tsutsui sc->sc_tc.tc_name = "eclock"; /* BUGBUG is it unique per instance?? */
315 1.2 tsutsui sc->sc_tc.tc_quality = 2000; /* uhu? */
316 1.2 tsutsui sc->sc_tc.tc_priv = sc;
317 1.2 tsutsui sc->sc_tc.tc_next = NULL;
318 1.1 pooka
319 1.1 pooka #if DEBUG
320 1.2 tsutsui printf(" virt=%p ", (void *)sc->sc_dp);
321 1.1 pooka #endif
322 1.1 pooka printf(": eMIPS clock\n");
323 1.1 pooka
324 1.1 pooka /* Turn interrupts off, just in case. */
325 1.2 tsutsui sc->sc_dp->Control &= ~(TCCT_INT_ENABLE|TCCT_INTERRUPT);
326 1.1 pooka
327 1.1 pooka ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_CLOCK,
328 1.1 pooka eclock_ebus_intr, sc);
329 1.1 pooka
330 1.1 pooka #ifdef EVCNT_COUNTERS
331 1.1 pooka evcnt_attach_dynamic(&clock_intr_evcnt, EVCNT_TYPE_INTR, NULL,
332 1.6 tsutsui device_xname(self), "intr");
333 1.1 pooka #endif
334 1.1 pooka
335 1.2 tsutsui clockdev = self;
336 1.5 tsutsui memset(&sc->sc_todr, 0, sizeof sc->sc_todr);
337 1.2 tsutsui sc->sc_todr.cookie = sc;
338 1.2 tsutsui sc->sc_todr.todr_gettime = eclock_gettime;
339 1.2 tsutsui sc->sc_todr.todr_settime = eclock_settime;
340 1.2 tsutsui todr_attach(&sc->sc_todr);
341 1.1 pooka
342 1.2 tsutsui tc_init(&sc->sc_tc);
343 1.1 pooka }
344