xref: /openbsd/sys/dev/acpi/acpitimer.c (revision fc61954a)
1 /* $OpenBSD: acpitimer.c,v 1.11 2015/03/14 03:38:46 jsg 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 acpitimermatch(struct device *parent, void *match, void *aux)
59 {
60 	struct acpi_attach_args *aa = aux;
61 	struct cfdata *cf = match;
62 
63 	/* sanity */
64 	if (aa->aaa_name == NULL ||
65 	    strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 ||
66 	    aa->aaa_table != NULL)
67 		return (0);
68 
69 	return (1);
70 }
71 
72 void
73 acpitimerattach(struct device *parent, struct device *self, void *aux)
74 {
75 	struct acpitimer_softc *sc = (struct acpitimer_softc *) self;
76 	struct acpi_softc *psc = (struct acpi_softc *) parent;
77 	int rc;
78 
79 	if (psc->sc_fadt->hdr_revision >= 3 &&
80 	    psc->sc_fadt->x_pm_tmr_blk.address != 0)
81 		rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0,
82 		    psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
83 	else
84 		rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk,
85 		    psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot);
86 	if (rc) {
87 		printf(": can't map i/o space\n");
88 		return;
89 	}
90 
91 	printf(": %d Hz, %d bits\n", ACPI_FREQUENCY,
92 	    psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24);
93 
94 	if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT)
95 		acpi_timecounter.tc_counter_mask = 0xffffffffU;
96 	acpi_timecounter.tc_priv = sc;
97 	acpi_timecounter.tc_name = sc->sc_dev.dv_xname;
98 	tc_init(&acpi_timecounter);
99 }
100 
101 
102 u_int
103 acpi_get_timecount(struct timecounter *tc)
104 {
105 	struct acpitimer_softc *sc = tc->tc_priv;
106 	u_int u1, u2, u3;
107 
108 	u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
109 	u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
110 	do {
111 		u1 = u2;
112 		u2 = u3;
113 		u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
114 	} while (u1 > u2 || u2 > u3);
115 
116 	return (u2);
117 }
118