acpitimer.c 3.2 KB

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