No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / acpi / acpi_timer.c
blobb41a9575c5c0317838be1279478041787959218d
1 /* $NetBSD: acpi_timer.c,v 1.13 2009/03/15 15:42:55 cegger Exp $ */
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.13 2009/03/15 15:42:55 cegger Exp $");
6 #include <sys/types.h>
7 #include <sys/systm.h>
8 #include <sys/time.h>
9 #include <sys/timetc.h>
10 #include <dev/acpi/acpica.h>
11 #include <dev/acpi/acpi_timer.h>
12 #include <machine/acpi_machdep.h>
14 static int acpitimer_test(void);
15 static uint32_t acpitimer_delta(uint32_t, uint32_t);
16 static u_int acpitimer_read_safe(struct timecounter *);
17 static u_int acpitimer_read_fast(struct timecounter *);
19 static struct timecounter acpi_timecounter = {
20 acpitimer_read_safe,
22 0x00ffffff,
23 PM_TIMER_FREQUENCY,
24 "ACPI-Safe",
25 900,
26 NULL,
27 NULL,
30 int
31 acpitimer_init(void)
33 uint32_t bits;
34 int i, j;
35 ACPI_STATUS res;
37 res = AcpiGetTimerResolution(&bits);
38 if (res != AE_OK)
39 return (-1);
41 if (bits == 32)
42 acpi_timecounter.tc_counter_mask = 0xffffffff;
44 j = 0;
45 for (i = 0; i < 10; i++)
46 j += acpitimer_test();
48 if (j >= 10) {
49 acpi_timecounter.tc_name = "ACPI-Fast";
50 acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
51 acpi_timecounter.tc_quality = 1000;
54 tc_init(&acpi_timecounter);
55 aprint_verbose("%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
57 return (0);
60 int
61 acpitimer_detach(void)
63 return tc_detach(&acpi_timecounter);
66 static u_int
67 acpitimer_read_fast(struct timecounter *tc)
69 uint32_t t;
71 AcpiGetTimer(&t);
72 return (t);
76 * Some chipsets (PIIX4 variants) do not latch correctly; there
77 * is a chance that a transition is hit.
79 static u_int
80 acpitimer_read_safe(struct timecounter *tc)
82 uint32_t t1, t2, t3;
84 AcpiGetTimer(&t2);
85 AcpiGetTimer(&t3);
86 do {
87 t1 = t2;
88 t2 = t3;
89 AcpiGetTimer(&t3);
90 } while ((t1 > t2) || (t2 > t3));
91 return (t2);
94 static uint32_t
95 acpitimer_delta(uint32_t end, uint32_t start)
97 uint32_t delta;
98 u_int mask = acpi_timecounter.tc_counter_mask;
100 if (end >= start)
101 delta = end - start;
102 else
103 delta = ((mask - start) + end + 1) & mask;
105 return (delta);
108 #define N 2000
109 static int
110 acpitimer_test(void)
112 uint32_t last, this, delta;
113 int minl, maxl, n;
115 minl = 10000000;
116 maxl = 0;
118 acpi_md_OsDisableInterrupt();
119 AcpiGetTimer(&last);
120 for (n = 0; n < N; n++) {
121 AcpiGetTimer(&this);
122 delta = acpitimer_delta(this, last);
123 if (delta > maxl)
124 maxl = delta;
125 else if (delta < minl)
126 minl = delta;
127 last = this;
129 acpi_md_OsEnableInterrupt();
131 if (maxl - minl > 2 )
132 n = 0;
133 else if (minl < 0 || maxl == 0)
134 n = 0;
135 else
136 n = 1;
138 return (n);