Line data Source code
1 : /* $OpenBSD: acpitimer.c,v 1.12 2017/10/06 13:33:53 mikeb Exp $ */
2 : /*
3 : * Copyright (c) 2005 Thorsten Lockert <tholo@sigmasoft.com>
4 : *
5 : * Permission to use, copy, modify, and distribute this software for any
6 : * purpose with or without fee is hereby granted, provided that the above
7 : * copyright notice and this permission notice appear in all copies.
8 : *
9 : * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 : * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 : * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 : * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 : */
17 :
18 : #include <sys/param.h>
19 : #include <sys/systm.h>
20 : #include <sys/device.h>
21 : #include <sys/timetc.h>
22 :
23 : #include <machine/bus.h>
24 :
25 : #include <dev/acpi/acpireg.h>
26 : #include <dev/acpi/acpivar.h>
27 :
28 : int acpitimermatch(struct device *, void *, void *);
29 : void acpitimerattach(struct device *, struct device *, void *);
30 :
31 : u_int acpi_get_timecount(struct timecounter *tc);
32 :
33 : static struct timecounter acpi_timecounter = {
34 : acpi_get_timecount, /* get_timecount */
35 : 0, /* no poll_pps */
36 : 0x00ffffff, /* counter_mask (24 bits) */
37 : ACPI_FREQUENCY, /* frequency */
38 : 0, /* name */
39 : 1000 /* quality */
40 : };
41 :
42 : struct acpitimer_softc {
43 : struct device sc_dev;
44 :
45 : bus_space_tag_t sc_iot;
46 : bus_space_handle_t sc_ioh;
47 : };
48 :
49 : struct cfattach acpitimer_ca = {
50 : sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach
51 : };
52 :
53 : struct cfdriver acpitimer_cd = {
54 : NULL, "acpitimer", DV_DULL
55 : };
56 :
57 : int
58 0 : acpitimermatch(struct device *parent, void *match, void *aux)
59 : {
60 0 : struct acpi_attach_args *aa = aux;
61 0 : struct cfdata *cf = match;
62 :
63 : /* sanity */
64 0 : if (aa->aaa_name == NULL ||
65 0 : strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 ||
66 0 : aa->aaa_table != NULL)
67 0 : return (0);
68 :
69 0 : return (1);
70 0 : }
71 :
72 : void
73 0 : acpitimerattach(struct device *parent, struct device *self, void *aux)
74 : {
75 0 : struct acpitimer_softc *sc = (struct acpitimer_softc *) self;
76 0 : struct acpi_softc *psc = (struct acpi_softc *) parent;
77 : int rc;
78 :
79 0 : if (psc->sc_fadt->hdr_revision >= 3 &&
80 0 : psc->sc_fadt->x_pm_tmr_blk.address != 0)
81 0 : rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0,
82 0 : psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
83 : else
84 0 : rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk,
85 0 : psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
86 0 : if (rc) {
87 0 : printf(": can't map i/o space\n");
88 0 : return;
89 : }
90 :
91 0 : printf(": %d Hz, %d bits\n", ACPI_FREQUENCY,
92 0 : psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24);
93 :
94 0 : if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT)
95 0 : acpi_timecounter.tc_counter_mask = 0xffffffffU;
96 0 : acpi_timecounter.tc_priv = sc;
97 0 : acpi_timecounter.tc_name = sc->sc_dev.dv_xname;
98 0 : tc_init(&acpi_timecounter);
99 : #if defined(__amd64__)
100 : extern void cpu_recalibrate_tsc(struct timecounter *);
101 0 : cpu_recalibrate_tsc(&acpi_timecounter);
102 : #endif
103 0 : }
104 :
105 :
106 : u_int
107 0 : acpi_get_timecount(struct timecounter *tc)
108 : {
109 0 : struct acpitimer_softc *sc = tc->tc_priv;
110 : u_int u1, u2, u3;
111 :
112 0 : u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
113 0 : u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
114 0 : do {
115 : u1 = u2;
116 : u2 = u3;
117 0 : u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
118 0 : } while (u1 > u2 || u2 > u3);
119 :
120 0 : return (u2);
121 : }
|