xref: /openbsd-src/sys/dev/acpi/acpitimer.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: acpitimer.c,v 1.3 2005/12/07 03:47:44 marco 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/malloc.h>
22 #ifdef __HAVE_TIMECOUNTER
23 #include <sys/timetc.h>
24 #endif
25 
26 #include <machine/bus.h>
27 
28 #include <dev/acpi/acpireg.h>
29 #include <dev/acpi/acpivar.h>
30 
31 int acpitimermatch(struct device *, void *, void *);
32 void acpitimerattach(struct device *, struct device *, void *);
33 
34 #ifdef __HAVE_TIMECOUNTER
35 u_int acpi_get_timecount(struct timecounter *tc);
36 
37 static struct timecounter acpi_timecounter = {
38 	acpi_get_timecount,	/* get_timecount */
39 	0,			/* no poll_pps */
40 	0x00ffffff,		/* counter_mask (24 bits) */
41 	ACPI_FREQUENCY,		/* frequency */
42 	0,			/* name */
43 	1000			/* quality */
44 };
45 #endif
46 
47 struct acpitimer_softc {
48 	struct device		sc_dev;
49 
50 	bus_space_tag_t		sc_iot;
51 	bus_space_handle_t	sc_ioh;
52 };
53 
54 struct cfattach acpitimer_ca = {
55 	sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach
56 };
57 
58 struct cfdriver acpitimer_cd = {
59 	NULL, "acpitimer", DV_DULL
60 };
61 
62 int
63 acpitimermatch(struct device *parent, void *match, void *aux)
64 {
65 	struct acpi_attach_args *aa = aux;
66 	struct cfdata *cf = match;
67 
68 	/* sanity */
69 	if (aa->aaa_name == NULL ||
70 	    strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 ||
71 	    aa->aaa_table != NULL)
72 		return (0);
73 
74 	return (1);
75 }
76 
77 void
78 acpitimerattach(struct device *parent, struct device *self, void *aux)
79 {
80 	struct acpitimer_softc *sc = (struct acpitimer_softc *) self;
81 	struct acpi_softc *psc = (struct acpi_softc *) parent;
82 	struct acpi_attach_args *aa = aux;
83 	bus_addr_t address;
84 	bus_size_t size;
85 
86 	if (psc->sc_fadt->hdr_revision > 1) {
87 		switch (psc->sc_fadt->x_pm_tmr_blk.address_space_id) {
88 		case GAS_SYSTEM_MEMORY:
89 			sc->sc_iot = aa->aaa_memt;
90 			break;
91 
92 		case GAS_SYSTEM_IOSPACE:
93 			sc->sc_iot = aa->aaa_iot;
94 			break;
95 
96 #if 0
97 		case GAS_SYSTEM_PCI_CFG_SPACE:
98 			sc->sc_iot = aa->aaa_pcit;
99 			break;
100 
101 		case GAS_SYSTEM_SMBUS:
102 			sc->sc_iot = aa->aaa_smbust;
103 			break;
104 #endif
105 
106 		default:
107 			printf(": can't identify bus\n");
108 			return;
109 		}
110 		address = psc->sc_fadt->x_pm_tmr_blk.address;
111 	} else {
112 		sc->sc_iot = aa->aaa_iot;
113 		address = psc->sc_fadt->pm_tmr_blk;
114 	}
115 	size = psc->sc_fadt->pm_tmr_len;
116 
117 	if (bus_space_map(sc->sc_iot, address, size, 0, &sc->sc_ioh)) {
118 		printf(": can't map i/o space\n");
119 		return;
120 	}
121 
122 	printf(": %ld Hz, %d bits\n", ACPI_FREQUENCY,
123 	    psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24);
124 
125 #ifdef __HAVE_TIMECOUNTER
126 	if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT)
127 		acpi_timecounter.tc_counter_mask = 0xffffffffU;
128 	acpi_timecounter.tc_priv = sc;
129 	acpi_timecounter.tc_name = sc->sc_dev.dv_xname;
130 	tc_init(&acpi_timecounter);
131 #endif
132 }
133 
134 
135 #ifdef __HAVE_TIMECOUNTER
136 u_int
137 acpi_get_timecount(struct timecounter *tc)
138 {
139 	struct acpitimer_softc *sc = tc->tc_priv;
140 	u_int u1, u2, u3;
141 
142 	u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
143 	u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
144 	do {
145 		u1 = u2;
146 		u2 = u3;
147 		u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0);
148 	} while (u1 > u2 || u2 > u3);
149 	return (u2);
150 }
151 #endif
152