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