xref: /netbsd-src/sys/dev/ic/hpet.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /* $NetBSD: hpet.c,v 1.2 2007/10/19 11:59:52 ad Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Nicolas Joly
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * High Precision Event Timer.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: hpet.c,v 1.2 2007/10/19 11:59:52 ad Exp $");
37 
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 
42 #include <sys/time.h>
43 #include <sys/timetc.h>
44 
45 #include <sys/bus.h>
46 
47 #include <dev/ic/hpetreg.h>
48 #include <dev/ic/hpetvar.h>
49 
50 static u_int	hpet_get_timecount(struct timecounter *);
51 
52 void
53 hpet_attach_subr(struct hpet_softc *sc) {
54 	struct timecounter *tc;
55 	uint32_t val;
56 
57 	tc = &sc->sc_tc;
58 
59 	tc->tc_name = sc->sc_dev.dv_xname;
60 	tc->tc_get_timecount = hpet_get_timecount;
61 	tc->tc_quality = 2000;
62 
63 	/* XXX Only 32-bits supported for now */
64 	val = bus_space_read_4(sc->sc_memt, sc->sc_memh, HPET_INFO);
65 	if ((val & HPET_INFO_64BITS) != 0) {
66 		aprint_normal("%s: Found 64-bits HPET, will only use lowest"
67 		    " 32-bits\n", sc->sc_dev.dv_xname);
68 	}
69 	tc->tc_counter_mask = 0xffffffff;
70 
71 	/* Get frequency */
72 	val = bus_space_read_4(sc->sc_memt, sc->sc_memh, HPET_PERIOD);
73 	tc->tc_frequency = 1000000000000000ULL / val;
74 
75 	/* Enable timer */
76 	val = bus_space_read_4(sc->sc_memt, sc->sc_memh, HPET_CONFIG);
77 	if ((val & HPET_CONFIG_ENABLE) == 0) {
78 		val |= HPET_CONFIG_ENABLE;
79 		bus_space_write_4(sc->sc_memt, sc->sc_memh, HPET_CONFIG, val);
80 	}
81 
82 	tc->tc_priv = sc;
83 	tc_init(tc);
84 }
85 
86 static u_int
87 hpet_get_timecount(struct timecounter *tc) {
88 	struct hpet_softc *sc = tc->tc_priv;
89 
90 	return bus_space_read_4(sc->sc_memt, sc->sc_memh, HPET_MCOUNT);
91 }
92 
93