1 /* $OpenBSD: acpitimer.c,v 1.10 2012/08/16 18:41:17 tedu 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 #include <sys/timetc.h> 23 24 #include <machine/bus.h> 25 26 #include <dev/acpi/acpireg.h> 27 #include <dev/acpi/acpivar.h> 28 29 int acpitimermatch(struct device *, void *, void *); 30 void acpitimerattach(struct device *, struct device *, void *); 31 32 u_int acpi_get_timecount(struct timecounter *tc); 33 34 static struct timecounter acpi_timecounter = { 35 acpi_get_timecount, /* get_timecount */ 36 0, /* no poll_pps */ 37 0x00ffffff, /* counter_mask (24 bits) */ 38 ACPI_FREQUENCY, /* frequency */ 39 0, /* name */ 40 1000 /* quality */ 41 }; 42 43 struct acpitimer_softc { 44 struct device sc_dev; 45 46 bus_space_tag_t sc_iot; 47 bus_space_handle_t sc_ioh; 48 }; 49 50 struct cfattach acpitimer_ca = { 51 sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach 52 }; 53 54 struct cfdriver acpitimer_cd = { 55 NULL, "acpitimer", DV_DULL 56 }; 57 58 int 59 acpitimermatch(struct device *parent, void *match, void *aux) 60 { 61 struct acpi_attach_args *aa = aux; 62 struct cfdata *cf = match; 63 64 /* sanity */ 65 if (aa->aaa_name == NULL || 66 strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 || 67 aa->aaa_table != NULL) 68 return (0); 69 70 return (1); 71 } 72 73 void 74 acpitimerattach(struct device *parent, struct device *self, void *aux) 75 { 76 struct acpitimer_softc *sc = (struct acpitimer_softc *) self; 77 struct acpi_softc *psc = (struct acpi_softc *) parent; 78 int rc; 79 80 if (psc->sc_fadt->hdr_revision >= 3 && 81 psc->sc_fadt->x_pm_tmr_blk.address != 0) 82 rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0, 83 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 84 else 85 rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk, 86 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 87 if (rc) { 88 printf(": can't map i/o space\n"); 89 return; 90 } 91 92 printf(": %d Hz, %d bits\n", ACPI_FREQUENCY, 93 psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24); 94 95 if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT) 96 acpi_timecounter.tc_counter_mask = 0xffffffffU; 97 acpi_timecounter.tc_priv = sc; 98 acpi_timecounter.tc_name = sc->sc_dev.dv_xname; 99 tc_init(&acpi_timecounter); 100 } 101 102 103 u_int 104 acpi_get_timecount(struct timecounter *tc) 105 { 106 struct acpitimer_softc *sc = tc->tc_priv; 107 u_int u1, u2, u3; 108 109 u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 110 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 111 do { 112 u1 = u2; 113 u2 = u3; 114 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 115 } while (u1 > u2 || u2 > u3); 116 117 return (u2); 118 } 119