1 /* $OpenBSD: acpitimer.c,v 1.9 2011/04/22 18:22:01 jordan 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 int rc; 83 84 if (psc->sc_fadt->hdr_revision >= 3 && 85 psc->sc_fadt->x_pm_tmr_blk.address != 0) 86 rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0, 87 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 88 else 89 rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk, 90 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 91 if (rc) { 92 printf(": can't map i/o space\n"); 93 return; 94 } 95 96 printf(": %d Hz, %d bits\n", ACPI_FREQUENCY, 97 psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24); 98 99 #ifdef __HAVE_TIMECOUNTER 100 if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT) 101 acpi_timecounter.tc_counter_mask = 0xffffffffU; 102 acpi_timecounter.tc_priv = sc; 103 acpi_timecounter.tc_name = sc->sc_dev.dv_xname; 104 tc_init(&acpi_timecounter); 105 #endif 106 } 107 108 109 #ifdef __HAVE_TIMECOUNTER 110 u_int 111 acpi_get_timecount(struct timecounter *tc) 112 { 113 struct acpitimer_softc *sc = tc->tc_priv; 114 u_int u1, u2, u3; 115 116 u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 117 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 118 do { 119 u1 = u2; 120 u2 = u3; 121 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 122 } while (u1 > u2 || u2 > u3); 123 124 return (u2); 125 } 126 #endif 127