1 /* $OpenBSD: acpitimer.c,v 1.14 2021/02/23 04:44:31 cheloha 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 .tc_get_timecount = acpi_get_timecount, 35 .tc_poll_pps = 0, 36 .tc_counter_mask = 0x00ffffff, /* 24 bits */ 37 .tc_frequency = ACPI_FREQUENCY, 38 .tc_name = 0, 39 .tc_quality = 1000, 40 .tc_priv = NULL, 41 .tc_user = 0, 42 }; 43 44 struct acpitimer_softc { 45 struct device sc_dev; 46 47 bus_space_tag_t sc_iot; 48 bus_space_handle_t sc_ioh; 49 }; 50 51 struct cfattach acpitimer_ca = { 52 sizeof(struct acpitimer_softc), acpitimermatch, acpitimerattach 53 }; 54 55 struct cfdriver acpitimer_cd = { 56 NULL, "acpitimer", DV_DULL 57 }; 58 59 int 60 acpitimermatch(struct device *parent, void *match, void *aux) 61 { 62 struct acpi_attach_args *aa = aux; 63 struct cfdata *cf = match; 64 65 /* sanity */ 66 if (aa->aaa_name == NULL || 67 strcmp(aa->aaa_name, cf->cf_driver->cd_name) != 0 || 68 aa->aaa_table != NULL) 69 return (0); 70 71 return (1); 72 } 73 74 void 75 acpitimerattach(struct device *parent, struct device *self, void *aux) 76 { 77 struct acpitimer_softc *sc = (struct acpitimer_softc *) self; 78 struct acpi_softc *psc = (struct acpi_softc *) parent; 79 int rc; 80 81 if (psc->sc_fadt->hdr_revision >= 3 && 82 psc->sc_fadt->x_pm_tmr_blk.address != 0) 83 rc = acpi_map_address(psc, &psc->sc_fadt->x_pm_tmr_blk, 0, 84 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 85 else 86 rc = acpi_map_address(psc, NULL, psc->sc_fadt->pm_tmr_blk, 87 psc->sc_fadt->pm_tmr_len, &sc->sc_ioh, &sc->sc_iot); 88 if (rc) { 89 printf(": can't map i/o space\n"); 90 return; 91 } 92 93 printf(": %d Hz, %d bits\n", ACPI_FREQUENCY, 94 psc->sc_fadt->flags & FADT_TMR_VAL_EXT ? 32 : 24); 95 96 if (psc->sc_fadt->flags & FADT_TMR_VAL_EXT) 97 acpi_timecounter.tc_counter_mask = 0xffffffffU; 98 acpi_timecounter.tc_priv = sc; 99 acpi_timecounter.tc_name = sc->sc_dev.dv_xname; 100 tc_init(&acpi_timecounter); 101 #if defined(__amd64__) 102 extern void cpu_recalibrate_tsc(struct timecounter *); 103 cpu_recalibrate_tsc(&acpi_timecounter); 104 #endif 105 } 106 107 108 u_int 109 acpi_get_timecount(struct timecounter *tc) 110 { 111 struct acpitimer_softc *sc = tc->tc_priv; 112 u_int u1, u2, u3; 113 114 u2 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 115 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 116 do { 117 u1 = u2; 118 u2 = u3; 119 u3 = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0); 120 } while (u1 > u2 || u2 > u3); 121 122 return (u2); 123 } 124