acpi_timer.c revision 1.5 1 /* $NetBSD: acpi_timer.c,v 1.5 2006/07/02 18:53:33 bouyer Exp $ */
2
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.5 2006/07/02 18:53:33 bouyer Exp $");
5
6 #include <sys/types.h>
7 #include <dev/acpi/acpi_timer.h>
8
9 #ifdef __HAVE_TIMECOUNTER
10
11 #include <sys/systm.h>
12 #include <sys/time.h>
13 #include <sys/timetc.h>
14 #include <dev/acpi/acpica.h>
15
16 static int acpitimer_test(void);
17 static uint32_t acpitimer_delta(uint32_t, uint32_t);
18 static u_int acpitimer_read_safe(struct timecounter *);
19 static u_int acpitimer_read_fast(struct timecounter *);
20
21 static struct timecounter acpi_timecounter = {
22 acpitimer_read_safe,
23 0,
24 0x00ffffff,
25 PM_TIMER_FREQUENCY,
26 "ACPI-Safe",
27 900
28 };
29
30 int
31 acpitimer_init()
32 {
33 uint32_t bits;
34 int i, j;
35 ACPI_STATUS res;
36
37 res = AcpiGetTimerResolution(&bits);
38 if (res != AE_OK)
39 return (-1);
40
41 if (bits == 32)
42 acpi_timecounter.tc_counter_mask = 0xffffffff;
43
44 j = 0;
45 for (i = 0; i < 10; i++)
46 j += acpitimer_test();
47
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;
52 }
53
54 tc_init(&acpi_timecounter);
55 aprint_normal("%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
56
57 return (0);
58 }
59
60 static u_int
61 acpitimer_read_fast(struct timecounter *tc)
62 {
63 uint32_t t;
64
65 AcpiGetTimer(&t);
66 return (t);
67 }
68
69 /*
70 * Some chipsets (PIIX4 variants) do not latch correctly; there
71 * is a chance that a transition is hit.
72 */
73 static u_int
74 acpitimer_read_safe(struct timecounter *tc)
75 {
76 uint32_t t1, t2, t3;
77
78 AcpiGetTimer(&t2);
79 AcpiGetTimer(&t3);
80 do {
81 t1 = t2;
82 t2 = t3;
83 AcpiGetTimer(&t3);
84 } while ((t1 > t2) || (t2 > t3));
85 return (t2);
86 }
87
88 static uint32_t
89 acpitimer_delta(uint32_t end, uint32_t start)
90 {
91 uint32_t delta;
92 u_int mask = acpi_timecounter.tc_counter_mask;
93
94 if (end >= start)
95 delta = end - start;
96 else
97 delta = ((mask - start) + end + 1) & mask;
98
99 return (delta);
100 }
101
102 #define N 2000
103 static int
104 acpitimer_test()
105 {
106 uint32_t last, this, delta;
107 int minl, maxl, n;
108
109 minl = 10000000;
110 maxl = 0;
111
112 disable_intr();
113 AcpiGetTimer(&last);
114 for (n = 0; n < N; n++) {
115 AcpiGetTimer(&this);
116 delta = acpitimer_delta(this, last);
117 if (delta > maxl)
118 maxl = delta;
119 else if (delta < minl)
120 minl = delta;
121 last = this;
122 }
123 enable_intr();
124
125 if (maxl - minl > 2 )
126 n = 0;
127 else if (minl < 0 || maxl == 0)
128 n = 0;
129 else
130 n = 1;
131
132 return (n);
133 }
134
135 #else
136
137 int
138 acpitimer_init()
139 {
140
141 return (0);
142 }
143
144 #endif
145