1 /* $NetBSD: acpi_timer.c,v 1.20 2011/01/04 04:28:48 jruoho 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.20 2011/01/04 04:28:48 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 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 const uint32_t flags = AcpiGbl_FADT.Flags; 59 ACPI_STATUS rv; 60 uint32_t bits; 61 int i, j; 62 63 rv = AcpiGetTimerResolution(&bits); 64 65 if (ACPI_FAILURE(rv)) 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, "%s %d-bit timer\n", 83 acpi_timecounter.tc_name, bits); 84 85 if ((flags & ACPI_FADT_PLATFORM_CLOCK) == 0) 86 aprint_debug_dev(sc->sc_dev, 87 "warning: timer may be unreliable\n"); 88 89 return 0; 90 } 91 92 int 93 acpitimer_detach(void) 94 { 95 96 return tc_detach(&acpi_timecounter); 97 } 98 99 u_int 100 acpitimer_read_fast(struct timecounter *tc) 101 { 102 uint32_t t; 103 104 (void)AcpiGetTimer(&t); 105 106 return t; 107 } 108 109 /* 110 * Some chipsets (PIIX4 variants) do not latch correctly; 111 * there is a chance that a transition is hit. 112 */ 113 u_int 114 acpitimer_read_safe(struct timecounter *tc) 115 { 116 uint32_t t1, t2, t3; 117 118 (void)AcpiGetTimer(&t2); 119 (void)AcpiGetTimer(&t3); 120 121 do { 122 t1 = t2; 123 t2 = t3; 124 125 (void)AcpiGetTimer(&t3); 126 127 } while ((t1 > t2) || (t2 > t3)); 128 129 return t2; 130 } 131 132 uint32_t 133 acpitimer_delta(uint32_t end, uint32_t start) 134 { 135 const u_int mask = acpi_timecounter.tc_counter_mask; 136 uint32_t delta; 137 138 if (end >= start) 139 delta = end - start; 140 else 141 delta = ((mask - start) + end + 1) & mask; 142 143 return delta; 144 } 145 146 #define N 2000 147 148 static int 149 acpitimer_test(void) 150 { 151 uint32_t last, this, delta; 152 int minl, maxl, n; 153 154 minl = 10000000; 155 maxl = 0; 156 157 acpi_md_OsDisableInterrupt(); 158 159 (void)AcpiGetTimer(&last); 160 161 for (n = 0; n < N; n++) { 162 163 (void)AcpiGetTimer(&this); 164 165 delta = acpitimer_delta(this, last); 166 167 if (delta > maxl) 168 maxl = delta; 169 else if (delta < minl) 170 minl = delta; 171 172 last = this; 173 } 174 175 acpi_md_OsEnableInterrupt(); 176 177 if (maxl - minl > 2 ) 178 n = 0; 179 else if (minl < 0 || maxl == 0) 180 n = 0; 181 else 182 n = 1; 183 184 return n; 185 } 186