acpi_timer.c revision 1.17 1 /* $NetBSD: acpi_timer.c,v 1.17 2010/07/10 13:08:09 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Matthias Drochner <drochner (at) NetBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_timer.c,v 1.17 2010/07/10 13:08:09 jruoho Exp $");
31
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/time.h>
35 #include <sys/timetc.h>
36
37 #include <dev/acpi/acpivar.h>
38 #include <dev/acpi/acpi_timer.h>
39
40 #include <machine/acpi_machdep.h>
41
42 static int acpitimer_test(void);
43 static uint32_t acpitimer_delta(uint32_t, uint32_t);
44
45 static struct timecounter acpi_timecounter = {
46 acpitimer_read_safe,
47 0,
48 0x00ffffff,
49 PM_TIMER_FREQUENCY,
50 "ACPI-Safe",
51 900,
52 NULL,
53 NULL,
54 };
55
56 int
57 acpitimer_init(struct acpi_softc *sc)
58 {
59 ACPI_STATUS res;
60 uint32_t bits;
61 int i, j;
62
63 res = AcpiGetTimerResolution(&bits);
64
65 if (res != AE_OK)
66 return (-1);
67
68 if (bits == 32)
69 acpi_timecounter.tc_counter_mask = 0xffffffff;
70
71 for (i = j = 0; i < 10; i++)
72 j += acpitimer_test();
73
74 if (j >= 10) {
75 acpi_timecounter.tc_name = "ACPI-Fast";
76 acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
77 acpi_timecounter.tc_quality = 1000;
78 }
79
80 tc_init(&acpi_timecounter);
81
82 aprint_debug_dev(sc->sc_dev,
83 "%s %d-bit timer\n", acpi_timecounter.tc_name, bits);
84
85 return 0;
86 }
87
88 int
89 acpitimer_detach(void)
90 {
91
92 return tc_detach(&acpi_timecounter);
93 }
94
95 u_int
96 acpitimer_read_fast(struct timecounter *tc)
97 {
98 uint32_t t;
99
100 (void)AcpiGetTimer(&t);
101
102 return t;
103 }
104
105 /*
106 * Some chipsets (PIIX4 variants) do not latch correctly; there
107 * is a chance that a transition is hit.
108 */
109 u_int
110 acpitimer_read_safe(struct timecounter *tc)
111 {
112 uint32_t t1, t2, t3;
113
114 (void)AcpiGetTimer(&t2);
115 (void)AcpiGetTimer(&t3);
116
117 do {
118 t1 = t2;
119 t2 = t3;
120
121 (void)AcpiGetTimer(&t3);
122
123 } while ((t1 > t2) || (t2 > t3));
124
125 return t2;
126 }
127
128 static uint32_t
129 acpitimer_delta(uint32_t end, uint32_t start)
130 {
131 const u_int mask = acpi_timecounter.tc_counter_mask;
132 uint32_t delta;
133
134 if (end >= start)
135 delta = end - start;
136 else
137 delta = ((mask - start) + end + 1) & mask;
138
139 return delta;
140 }
141
142 #define N 2000
143
144 static int
145 acpitimer_test(void)
146 {
147 uint32_t last, this, delta;
148 int minl, maxl, n;
149
150 minl = 10000000;
151 maxl = 0;
152
153 acpi_md_OsDisableInterrupt();
154
155 (void)AcpiGetTimer(&last);
156
157 for (n = 0; n < N; n++) {
158
159 (void)AcpiGetTimer(&this);
160
161 delta = acpitimer_delta(this, last);
162
163 if (delta > maxl)
164 maxl = delta;
165 else if (delta < minl)
166 minl = delta;
167
168 last = this;
169 }
170
171 acpi_md_OsEnableInterrupt();
172
173 if (maxl - minl > 2 )
174 n = 0;
175 else if (minl < 0 || maxl == 0)
176 n = 0;
177 else
178 n = 1;
179
180 return n;
181 }
182