xref: /netbsd-src/sys/dev/acpi/acpi_timer.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: acpi_timer.c,v 1.21 2011/08/08 11:27:44 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Matthias Drochner <drochner@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.21 2011/08/08 11:27:44 jmcneill 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 
44 static struct timecounter acpi_timecounter = {
45 	acpitimer_read_safe,
46 	0,
47 	0x00ffffff,
48 	PM_TIMER_FREQUENCY,
49 	"ACPI-Safe",
50 	900,
51 	NULL,
52 	NULL,
53 };
54 
55 int
56 acpitimer_init(struct acpi_softc *sc)
57 {
58 	ACPI_STATUS rv;
59 	uint32_t bits;
60 	int i, j;
61 
62 	rv = AcpiGetTimerResolution(&bits);
63 
64 	if (ACPI_FAILURE(rv))
65 		return -1;
66 
67 	if (bits == 32)
68 		acpi_timecounter.tc_counter_mask = 0xffffffff;
69 
70 	for (i = j = 0; i < 10; i++)
71 		j += acpitimer_test();
72 
73 	if (j >= 10) {
74 		acpi_timecounter.tc_name = "ACPI-Fast";
75 		acpi_timecounter.tc_get_timecount = acpitimer_read_fast;
76 		acpi_timecounter.tc_quality = 1000;
77 	}
78 
79 	tc_init(&acpi_timecounter);
80 
81 	aprint_debug_dev(sc->sc_dev, "%s %d-bit timer\n",
82 	    acpi_timecounter.tc_name, bits);
83 
84 	return 0;
85 }
86 
87 int
88 acpitimer_detach(void)
89 {
90 
91 	return tc_detach(&acpi_timecounter);
92 }
93 
94 u_int
95 acpitimer_read_fast(struct timecounter *tc)
96 {
97 	uint32_t t;
98 
99 	(void)AcpiGetTimer(&t);
100 
101 	return t;
102 }
103 
104 /*
105  * Some chipsets (PIIX4 variants) do not latch correctly;
106  * there is a chance that a transition is hit.
107  */
108 u_int
109 acpitimer_read_safe(struct timecounter *tc)
110 {
111 	uint32_t t1, t2, t3;
112 
113 	(void)AcpiGetTimer(&t2);
114 	(void)AcpiGetTimer(&t3);
115 
116 	do {
117 		t1 = t2;
118 		t2 = t3;
119 
120 		(void)AcpiGetTimer(&t3);
121 
122 	} while ((t1 > t2) || (t2 > t3));
123 
124 	return t2;
125 }
126 
127 uint32_t
128 acpitimer_delta(uint32_t end, uint32_t start)
129 {
130 	const u_int mask = acpi_timecounter.tc_counter_mask;
131 	uint32_t delta;
132 
133 	if (end >= start)
134 		delta = end - start;
135 	else
136 		delta = ((mask - start) + end + 1) & mask;
137 
138 	return delta;
139 }
140 
141 #define N 2000
142 
143 static int
144 acpitimer_test(void)
145 {
146 	uint32_t last, this, delta;
147 	int minl, maxl, n;
148 
149 	minl = 10000000;
150 	maxl = 0;
151 
152 	acpi_md_OsDisableInterrupt();
153 
154 	(void)AcpiGetTimer(&last);
155 
156 	for (n = 0; n < N; n++) {
157 
158 		(void)AcpiGetTimer(&this);
159 
160 		delta = acpitimer_delta(this, last);
161 
162 		if (delta > maxl)
163 			maxl = delta;
164 		else if (delta < minl)
165 			minl = delta;
166 
167 		last = this;
168 	}
169 
170 	acpi_md_OsEnableInterrupt();
171 
172 	if (maxl - minl > 2 )
173 		n = 0;
174 	else if (minl < 0 || maxl == 0)
175 		n = 0;
176 	else
177 		n = 1;
178 
179 	return n;
180 }
181