1 /* $NetBSD: hpet.c,v 1.8 2009/09/30 15:22:11 njoly Exp $ */
4 * Copyright (c) 2006 Nicolas Joly
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 * High Precision Event Timer.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: hpet.c,v 1.8 2009/09/30 15:22:11 njoly Exp $");
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
43 #include <sys/timetc.h>
47 #include <dev/ic/hpetreg.h>
48 #include <dev/ic/hpetvar.h>
50 static u_int
hpet_get_timecount(struct timecounter
*);
51 static bool hpet_resume(device_t
, pmf_qual_t
);
54 hpet_detach(device_t dv
, int flags
)
56 struct hpet_softc
*sc
= device_private(dv
);
59 if ((rc
= tc_detach(&sc
->sc_tc
)) != 0)
62 pmf_device_deregister(dv
);
64 bus_space_write_4(sc
->sc_memt
, sc
->sc_memh
, HPET_CONFIG
, sc
->sc_config
);
70 hpet_attach_subr(device_t dv
)
72 struct hpet_softc
*sc
= device_private(dv
);
73 struct timecounter
*tc
;
78 tc
->tc_name
= device_xname(dv
);
79 tc
->tc_get_timecount
= hpet_get_timecount
;
80 tc
->tc_quality
= 2000;
82 tc
->tc_counter_mask
= 0xffffffff;
85 val
= bus_space_read_4(sc
->sc_memt
, sc
->sc_memh
, HPET_PERIOD
);
87 aprint_error_dev(dv
, "invalid timer period\n");
90 tc
->tc_frequency
= 1000000000000000ULL / val
;
93 val
= bus_space_read_4(sc
->sc_memt
, sc
->sc_memh
, HPET_CONFIG
);
95 if ((val
& HPET_CONFIG_ENABLE
) == 0) {
96 val
|= HPET_CONFIG_ENABLE
;
97 bus_space_write_4(sc
->sc_memt
, sc
->sc_memh
, HPET_CONFIG
, val
);
103 if (!pmf_device_register(dv
, NULL
, hpet_resume
))
104 aprint_error_dev(dv
, "couldn't establish power handler\n");
108 hpet_get_timecount(struct timecounter
*tc
)
110 struct hpet_softc
*sc
= tc
->tc_priv
;
112 return bus_space_read_4(sc
->sc_memt
, sc
->sc_memh
, HPET_MCOUNT_LO
);
116 hpet_resume(device_t dv
, pmf_qual_t qual
)
118 struct hpet_softc
*sc
= device_private(dv
);
121 val
= bus_space_read_4(sc
->sc_memt
, sc
->sc_memh
, HPET_CONFIG
);
122 val
|= HPET_CONFIG_ENABLE
;
123 bus_space_write_4(sc
->sc_memt
, sc
->sc_memh
, HPET_CONFIG
, val
);