Merge from vendor branch BIND:
[dragonfly.git] / sys / i386 / i386 / mp_clock.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * Just when we thought life were beautiful, reality pops its grim face over
10  * the edge again:
11  *
12  * ] 20. ACPI Timer Errata
13  * ]
14  * ]   Problem: The power management timer may return improper result when
15  * ]   read. Although the timer value settles properly after incrementing,
16  * ]   while incrementing there is a 3nS window every 69.8nS where the
17  * ]   timer value is indeterminate (a 4.2% chance that the data will be
18  * ]   incorrect when read). As a result, the ACPI free running count up
19  * ]   timer specification is violated due to erroneous reads.  Implication:
20  * ]   System hangs due to the "inaccuracy" of the timer when used by
21  * ]   software for time critical events and delays.
22  * ] 
23  * ] Workaround: Read the register twice and compare.
24  * ] Status: This will not be fixed in the PIIX4 or PIIX4E.
25  *
26  * The counter is in other words not latched to the PCI bus clock when
27  * read.  Notice the workaround isn't:  We need to read until we have
28  * three monotonic samples and then use the middle one, otherwise we are
29  * not protected against the fact that the bits can be wrong in two
30  * directions.  If we only cared about monosity two reads would be enough.
31  *
32  * $FreeBSD: src/sys/i386/i386/mp_clock.c,v 1.4.2.2 2000/09/30 02:49:32 ps Exp $
33  * $DragonFly: src/sys/i386/i386/Attic/mp_clock.c,v 1.4 2004/01/30 05:42:16 dillon Exp $
34  *
35  */
36
37 /* #include "opt_bus.h" */
38 /* #include "opt_pci.h" */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/bus.h>
46
47 #include <bus/pci/pcivar.h>
48
49 #if 0
50
51 static unsigned piix_get_timecount(struct timecounter *tc);
52
53 static u_int32_t piix_timecounter_address;
54 static u_int piix_freq = 14318182/4;
55
56 static struct timecounter piix_timecounter = {
57         piix_get_timecount,
58         0,
59         0xffffff,
60         0,
61         "PIIX"
62 };
63
64 SYSCTL_OPAQUE(_debug, OID_AUTO, piix_timecounter, CTLFLAG_RD,
65         &piix_timecounter, sizeof(piix_timecounter), "S,timecounter", "");
66
67 static int
68 sysctl_machdep_piix_freq(SYSCTL_HANDLER_ARGS)
69 {
70         int error;
71         u_int freq;
72
73         if (piix_timecounter.tc_frequency == 0)
74                 return (EOPNOTSUPP);
75         freq = piix_freq;
76         error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
77         if (error == 0 && req->newptr != NULL) {
78                 piix_freq = freq;
79                 piix_timecounter.tc_frequency = piix_freq;
80                 update_timecounter(&piix_timecounter);
81         }
82         return (error);
83 }
84
85 SYSCTL_PROC(_machdep, OID_AUTO, piix_freq, CTLTYPE_INT | CTLFLAG_RW,
86     0, sizeof(u_int), sysctl_machdep_piix_freq, "I", "");
87
88 static unsigned
89 piix_get_timecount(struct timecounter *tc)
90 {
91         unsigned u1, u2, u3;
92
93         u2 = inl(piix_timecounter_address);
94         u3 = inl(piix_timecounter_address);
95         do {
96                 u1 = u2;
97                 u2 = u3;
98                 u3 = inl(piix_timecounter_address);
99         } while (u1 > u2 || u2 > u3);
100         return (u2);
101 }
102
103 static int
104 piix_probe (device_t dev)
105 {
106         u_int32_t       d;
107
108         switch (pci_get_devid(dev)) {
109         case 0x71138086:
110                 d = pci_read_config(dev, 0x4, 2);
111                 if (!(d & 1))
112                         return 0;       /* IO space not mapped */
113                 d = pci_read_config(dev, 0x40, 4);
114                 piix_timecounter_address = (d & 0xffc0) + 8;
115                 piix_timecounter.tc_frequency = piix_freq;
116                 init_timecounter(&piix_timecounter);
117                 return (ENXIO);
118         };
119         return (ENXIO);
120 }
121
122 static int
123 piix_attach (device_t dev)
124 {
125         
126         return 0;
127 }
128
129 static device_method_t piix_methods[] = {
130         /* Device interface */
131         DEVMETHOD(device_probe,         piix_probe),
132         DEVMETHOD(device_attach,        piix_attach),
133         { 0, 0 }
134 };
135
136 static driver_t piix_driver = {
137         "piix",
138         piix_methods,
139         1,
140 };
141
142 static devclass_t piix_devclass;
143
144 DRIVER_MODULE(piix, pci, piix_driver, piix_devclass, 0, 0);
145
146 #endif
147