clock_ebus.c revision 1.2 1 1.2 tsutsui /* $NetBSD: clock_ebus.c,v 1.2 2011/06/12 04:17:30 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.2 tsutsui __KERNEL_RCSID(0, "$NetBSD: clock_ebus.c,v 1.2 2011/06/12 04:17:30 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.1 pooka struct device sc_dev;
53 1.1 pooka struct _Tc *sc_dp;
54 1.2 tsutsui uint32_t reload;
55 1.2 tsutsui struct timecounter sc_tc;
56 1.1 pooka #ifdef __HAVE_GENERIC_TODR
57 1.2 tsutsui struct todr_chip_handle sc_todr;
58 1.1 pooka #endif
59 1.1 pooka };
60 1.1 pooka
61 1.2 tsutsui static int eclock_ebus_match(device_t, cfdata_t, void *);
62 1.2 tsutsui static void eclock_ebus_attach(device_t, device_t, void *);
63 1.1 pooka
64 1.1 pooka CFATTACH_DECL(eclock_ebus, sizeof (struct eclock_softc),
65 1.1 pooka eclock_ebus_match, eclock_ebus_attach, NULL, NULL);
66 1.1 pooka
67 1.2 tsutsui void eclock_init(device_t);
68 1.1 pooka
69 1.2 tsutsui static void __eclock_init(device_t);
70 1.2 tsutsui static int eclock_gettime(struct todr_chip_handle *, struct timeval *);
71 1.2 tsutsui static int eclock_settime(struct todr_chip_handle *, struct timeval *);
72 1.2 tsutsui static int eclock_ebus_intr(void *, void *);
73 1.2 tsutsui static u_int eclock_counter(struct timecounter *);
74 1.2 tsutsui
75 1.2 tsutsui /* BUGBUG resolve the gap between cpu_initclocks() and eclock_init(x) */
76 1.2 tsutsui device_t clockdev = NULL;
77 1.1 pooka
78 1.1 pooka void
79 1.2 tsutsui eclock_init(device_t dev)
80 1.1 pooka {
81 1.2 tsutsui
82 1.2 tsutsui if (dev == NULL)
83 1.2 tsutsui dev = clockdev;
84 1.2 tsutsui if (dev == NULL)
85 1.2 tsutsui panic("eclock_init");
86 1.2 tsutsui __eclock_init(dev);
87 1.1 pooka }
88 1.1 pooka
89 1.1 pooka static void
90 1.2 tsutsui __eclock_init(device_t dev)
91 1.1 pooka {
92 1.1 pooka struct eclock_softc *sc = (struct eclock_softc *)dev;
93 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
94 1.2 tsutsui uint32_t reload = 10*1000000; /* 1sec in 100ns units (10MHz clock) */
95 1.1 pooka
96 1.2 tsutsui /*
97 1.2 tsutsui * Compute reload according to whatever value passed in,
98 1.2 tsutsui * Warn if fractional
99 1.2 tsutsui */
100 1.2 tsutsui if (hz > 1) {
101 1.2 tsutsui uint32_t r = reload / hz;
102 1.2 tsutsui if ((r * hz) != reload)
103 1.2 tsutsui printf("%s: %d Hz clock will cause roundoffs"
104 1.2 tsutsui " with 10MHz xtal (%d)\n",
105 1.2 tsutsui sc->sc_dev.dv_xname, hz, reload - (r * hz));
106 1.1 pooka reload = r;
107 1.1 pooka }
108 1.1 pooka
109 1.2 tsutsui sc->reload = reload;
110 1.1 pooka
111 1.2 tsutsui /* Start the counter */
112 1.2 tsutsui tc->DownCounterHigh = 0;
113 1.2 tsutsui tc->DownCounter = sc->reload;
114 1.2 tsutsui tc->Control = TCCT_ENABLE | TCCT_INT_ENABLE;
115 1.1 pooka }
116 1.1 pooka
117 1.1 pooka /*
118 1.1 pooka * Get the time of day, based on the clock's value and/or the base value.
119 1.1 pooka * NB: At 10MHz, our 64bits FreeRunning is worth 58,426 years.
120 1.1 pooka */
121 1.1 pooka
122 1.1 pooka extern u_quad_t __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq);
123 1.1 pooka
124 1.1 pooka
125 1.1 pooka static int
126 1.1 pooka eclock_gettime(struct todr_chip_handle *todr, struct timeval *tv)
127 1.1 pooka {
128 1.2 tsutsui struct eclock_softc *sc = todr->cookie;
129 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
130 1.2 tsutsui uint64_t free;
131 1.2 tsutsui int s;
132 1.2 tsutsui
133 1.2 tsutsui /*
134 1.2 tsutsui * 32bit processor, guard against interrupts in the middle of
135 1.2 tsutsui * reading this 64bit entity
136 1.2 tsutsui */
137 1.2 tsutsui /* BUGBUG Should read it "twice" to guard against rollover too. */
138 1.1 pooka s = splhigh();
139 1.2 tsutsui free = tc->FreeRunning;
140 1.1 pooka splx(s);
141 1.1 pooka
142 1.2 tsutsui /*
143 1.2 tsutsui * Big fight with the compiler here, it gets very confused by 64bits.
144 1.2 tsutsui */
145 1.1 pooka #if 0
146 1.2 tsutsui /*
147 1.2 tsutsui * This is in C:
148 1.2 tsutsui */
149 1.2 tsutsui {
150 1.2 tsutsui uint64_t freeS, freeU;
151 1.2 tsutsui freeS = free / (10 * 1000 * 1000);
152 1.2 tsutsui freeU = free % (10 * 1000 * 1000);
153 1.2 tsutsui tv->tv_sec = freeS;
154 1.2 tsutsui tv->tv_usec = freeU / 10;
155 1.2 tsutsui #if 0
156 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
157 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n",
158 1.2 tsutsui tv->tv_sec, tv->tv_usec, freeS, freeU, free);
159 1.2 tsutsui #endif
160 1.2 tsutsui }
161 1.1 pooka #else
162 1.2 tsutsui /*
163 1.2 tsutsui * And this is in assembly :-)
164 1.2 tsutsui */
165 1.2 tsutsui {
166 1.2 tsutsui u_quad_t r;
167 1.2 tsutsui u_quad_t d = __qdivrem(free,(u_quad_t)10000000,&r);
168 1.2 tsutsui uint32_t su, uu;
169 1.2 tsutsui su = (uint32_t)d;
170 1.2 tsutsui uu = (uint32_t)r;
171 1.2 tsutsui uu = uu / 10; /* in usecs */
172 1.2 tsutsui tv->tv_sec = su;
173 1.2 tsutsui tv->tv_usec = uu;
174 1.2 tsutsui #if 0
175 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
176 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n",
177 1.2 tsutsui tv->tv_sec, tv->tv_usec, d, r, free);
178 1.2 tsutsui #endif
179 1.2 tsutsui }
180 1.1 pooka #endif
181 1.1 pooka
182 1.2 tsutsui return 0;
183 1.1 pooka }
184 1.1 pooka
185 1.1 pooka /*
186 1.1 pooka * Reset the TODR based on the time value.
187 1.1 pooka */
188 1.1 pooka static int
189 1.1 pooka eclock_settime(struct todr_chip_handle *todr, struct timeval *tv)
190 1.1 pooka {
191 1.2 tsutsui struct eclock_softc *sc = todr->cookie;
192 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
193 1.2 tsutsui uint64_t free;
194 1.2 tsutsui uint32_t su, uu;
195 1.2 tsutsui int s;
196 1.1 pooka
197 1.2 tsutsui /* Careful with what we do here, else the compilerbugs hit hard */
198 1.1 pooka s = splhigh();
199 1.1 pooka
200 1.2 tsutsui su = (uint32_t)tv->tv_sec; /* 0(tv) */
201 1.2 tsutsui uu = (uint32_t)tv->tv_usec; /* 4(tv) */
202 1.1 pooka
203 1.1 pooka
204 1.2 tsutsui free = 10 * 1000 * 1000 * (uint64_t)su;
205 1.2 tsutsui free += uu * 10;
206 1.1 pooka
207 1.2 tsutsui tc->FreeRunning = free;
208 1.1 pooka splx(s);
209 1.1 pooka
210 1.1 pooka #if 0
211 1.2 tsutsui /*
212 1.1 pooka Should compile to something like this:
213 1.1 pooka 80260c84 <eclock_settime>:
214 1.1 pooka 80260c84: 27bdffc0 addiu sp,sp,-64
215 1.1 pooka 80260c88: afbf0038 sw ra,56(sp)
216 1.1 pooka 80260c8c: afb40030 sw s4,48(sp)
217 1.1 pooka 80260c90: afb3002c sw s3,44(sp)
218 1.1 pooka 80260c94: afb20028 sw s2,40(sp)
219 1.1 pooka 80260c98: afb10024 sw s1,36(sp)
220 1.1 pooka 80260c9c: afb00020 sw s0,32(sp)
221 1.1 pooka 80260ca0: afb50034 sw s5,52(sp)
222 1.1 pooka 80260ca4: 8c820000 lw v0,0(a0)
223 1.1 pooka 80260ca8: 00a09021 move s2,a1
224 1.1 pooka 80260cac: 8c55003c lw s5,60(v0) //s5=tc
225 1.1 pooka 80260cb0: 0c004122 jal 80010488 <_splraise>
226 1.1 pooka 80260cb4: 3404ff00 li a0,0xff00
227 1.1 pooka 80260cb8: 8e540000 lw s4,0(s2) //s4=tv->tv_sec=us
228 1.1 pooka 80260cbc: 3c060098 lui a2,0x98
229 1.1 pooka 80260cc0: 34c69680 ori a2,a2,0x9680 //a2=10000000
230 1.1 pooka 80260cc4: 02860019 multu s4,a2 //free=us*10000000
231 1.1 pooka 80260cc8: 8e530004 lw s3,4(s2) //s3=uu
232 1.1 pooka 80260ccc: 00402021 move a0,v0 //s=splhigh()
233 1.1 pooka 80260cd0: 001328c0 sll a1,s3,0x3
234 1.1 pooka 80260cd4: 00131040 sll v0,s3,0x1
235 1.1 pooka 80260cd8: 00451021 addu v0,v0,a1
236 1.1 pooka 80260cdc: 00401821 move v1,v0 //v1 = uu*10
237 1.1 pooka 80260ce0: 00001021 move v0,zero
238 1.1 pooka 80260ce4: 00003812 mflo a3 //a3=low(free)
239 1.1 pooka 80260ce8: 00e38821 addu s1,a3,v1 //s1=low(free)+(uu*10)
240 1.1 pooka 80260cec: 0227282b sltu a1,s1,a3 //a1=overflow bit
241 1.1 pooka 80260cf0: 00003010 mfhi a2 //a2=high(free)
242 1.1 pooka 80260cf4: 00c28021 addu s0,a2,v0 //s0=a2=high(free) [useless, v0=0]
243 1.1 pooka 80260cf8: 00b08021 addu s0,a1,s0 //s0+=overflow bit
244 1.1 pooka 80260cfc: aeb1000c sw s1,12(s5)
245 1.1 pooka 80260d00: aeb00008 sw s0,8(s5)
246 1.1 pooka 80260d04: 0c00413f jal 800104fc <_splset>
247 1.1 pooka 80260d08: 00000000 nop
248 1.2 tsutsui */
249 1.2 tsutsui #endif
250 1.1 pooka
251 1.2 tsutsui #if 0
252 1.2 tsutsui printf("est: s x%" PRIx64 " u x%lx (%d %d), free %" PRId64 "\n",
253 1.2 tsutsui tv->tv_sec, tv->tv_usec, su, uu, free);
254 1.1 pooka #endif
255 1.1 pooka
256 1.2 tsutsui return 0;
257 1.1 pooka }
258 1.1 pooka
259 1.1 pooka static int
260 1.1 pooka eclock_ebus_intr(void *cookie, void *f)
261 1.1 pooka {
262 1.1 pooka struct eclock_softc *sc = cookie;
263 1.2 tsutsui struct _Tc *tc = sc->sc_dp;
264 1.2 tsutsui struct clockframe *cf = f;
265 1.2 tsutsui volatile uint32_t x;
266 1.2 tsutsui
267 1.2 tsutsui x = tc->Control;
268 1.2 tsutsui tc->DownCounterHigh = 0;
269 1.2 tsutsui tc->DownCounter = sc->reload;
270 1.1 pooka
271 1.2 tsutsui hardclock(cf);
272 1.2 tsutsui emips_clock_evcnt.ev_count++;
273 1.1 pooka
274 1.2 tsutsui return 0;
275 1.1 pooka }
276 1.1 pooka
277 1.1 pooka static u_int
278 1.1 pooka eclock_counter(struct timecounter *tc)
279 1.1 pooka {
280 1.1 pooka struct eclock_softc *sc = tc->tc_priv;
281 1.2 tsutsui struct _Tc *Tc = sc->sc_dp;
282 1.2 tsutsui
283 1.2 tsutsui return (u_int)Tc->FreeRunning; /* NB: chops to 32bits */
284 1.1 pooka }
285 1.1 pooka
286 1.1 pooka
287 1.1 pooka static int
288 1.2 tsutsui eclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
289 1.1 pooka {
290 1.1 pooka struct ebus_attach_args *ia = aux;
291 1.2 tsutsui struct _Tc *mc = (struct _Tc *)ia->ia_vaddr;
292 1.1 pooka
293 1.1 pooka if (strcmp("eclock", ia->ia_name) != 0)
294 1.2 tsutsui return 0;
295 1.2 tsutsui if ((mc == NULL) ||
296 1.2 tsutsui (mc->Tag != PMTTAG_TIMER))
297 1.2 tsutsui return 0;
298 1.1 pooka
299 1.2 tsutsui return 1;
300 1.1 pooka }
301 1.1 pooka
302 1.1 pooka static void
303 1.2 tsutsui eclock_ebus_attach(device_t parent, device_t self, void *aux)
304 1.1 pooka {
305 1.2 tsutsui struct ebus_attach_args *ia = aux;
306 1.1 pooka struct eclock_softc *sc = (struct eclock_softc *)self;
307 1.1 pooka
308 1.2 tsutsui sc->sc_dp = (struct _Tc *)ia->ia_vaddr;
309 1.1 pooka
310 1.2 tsutsui /* NB: We are chopping our 64bit free-running down to 32bits */
311 1.2 tsutsui sc->sc_tc.tc_get_timecount = eclock_counter;
312 1.2 tsutsui sc->sc_tc.tc_poll_pps = 0;
313 1.2 tsutsui sc->sc_tc.tc_counter_mask = 0xffffffff;
314 1.2 tsutsui sc->sc_tc.tc_frequency = 10*1000*1000; /* 10 MHz */
315 1.2 tsutsui sc->sc_tc.tc_name = "eclock"; /* BUGBUG is it unique per instance?? */
316 1.2 tsutsui sc->sc_tc.tc_quality = 2000; /* uhu? */
317 1.2 tsutsui sc->sc_tc.tc_priv = sc;
318 1.2 tsutsui sc->sc_tc.tc_next = NULL;
319 1.1 pooka
320 1.1 pooka #if DEBUG
321 1.2 tsutsui printf(" virt=%p ", (void *)sc->sc_dp);
322 1.1 pooka #endif
323 1.1 pooka printf(": eMIPS clock\n");
324 1.1 pooka
325 1.1 pooka /* Turn interrupts off, just in case. */
326 1.2 tsutsui sc->sc_dp->Control &= ~(TCCT_INT_ENABLE|TCCT_INTERRUPT);
327 1.1 pooka
328 1.1 pooka ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_CLOCK,
329 1.1 pooka eclock_ebus_intr, sc);
330 1.1 pooka
331 1.1 pooka #ifdef EVCNT_COUNTERS
332 1.1 pooka evcnt_attach_dynamic(&clock_intr_evcnt, EVCNT_TYPE_INTR, NULL,
333 1.1 pooka sc->sc_dev->dv_xname, "intr");
334 1.1 pooka #endif
335 1.1 pooka
336 1.1 pooka #ifdef __HAVE_GENERIC_TODR
337 1.2 tsutsui clockdev = self;
338 1.2 tsutsui memset(&sc->sc_todr,0,sizeof sc->sc_todr);
339 1.2 tsutsui sc->sc_todr.cookie = sc;
340 1.2 tsutsui sc->sc_todr.todr_gettime = eclock_gettime;
341 1.2 tsutsui sc->sc_todr.todr_settime = eclock_settime;
342 1.2 tsutsui todr_attach(&sc->sc_todr);
343 1.1 pooka #endif
344 1.1 pooka
345 1.2 tsutsui tc_init(&sc->sc_tc);
346 1.1 pooka }
347