7425ae62305dcb5456bb391709aba09d0c952428
[dragonfly.git] / sys / platform / pc32 / 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  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/time.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/bus.h>
41
42 #if 0
43
44 #include <bus/pci/pcivar.h>
45
46 static unsigned piix_get_timecount(struct timecounter *tc);
47
48 static u_int32_t piix_timecounter_address;
49 static u_int piix_freq = 14318182/4;
50
51 static struct timecounter piix_timecounter = {
52         piix_get_timecount,
53         0,
54         0xffffff,
55         0,
56         "PIIX"
57 };
58
59 SYSCTL_OPAQUE(_debug, OID_AUTO, piix_timecounter, CTLFLAG_RD,
60         &piix_timecounter, sizeof(piix_timecounter), "S,timecounter", "");
61
62 static int
63 sysctl_machdep_piix_freq(SYSCTL_HANDLER_ARGS)
64 {
65         int error;
66         u_int freq;
67
68         if (piix_timecounter.tc_frequency == 0)
69                 return (EOPNOTSUPP);
70         freq = piix_freq;
71         error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
72         if (error == 0 && req->newptr != NULL) {
73                 piix_freq = freq;
74                 piix_timecounter.tc_frequency = piix_freq;
75                 update_timecounter(&piix_timecounter);
76         }
77         return (error);
78 }
79
80 SYSCTL_PROC(_machdep, OID_AUTO, piix_freq, CTLTYPE_INT | CTLFLAG_RW,
81     0, sizeof(u_int), sysctl_machdep_piix_freq, "I", "");
82
83 static unsigned
84 piix_get_timecount(struct timecounter *tc)
85 {
86         unsigned u1, u2, u3;
87
88         u2 = inl(piix_timecounter_address);
89         u3 = inl(piix_timecounter_address);
90         do {
91                 u1 = u2;
92                 u2 = u3;
93                 u3 = inl(piix_timecounter_address);
94         } while (u1 > u2 || u2 > u3);
95         return (u2);
96 }
97
98 static int
99 piix_probe (device_t dev)
100 {
101         u_int32_t       d;
102
103         switch (pci_get_devid(dev)) {
104         case 0x71138086:
105                 d = pci_read_config(dev, 0x4, 2);
106                 if (!(d & 1))
107                         return 0;       /* IO space not mapped */
108                 d = pci_read_config(dev, 0x40, 4);
109                 piix_timecounter_address = (d & 0xffc0) + 8;
110                 piix_timecounter.tc_frequency = piix_freq;
111                 init_timecounter(&piix_timecounter);
112                 return (ENXIO);
113         };
114         return (ENXIO);
115 }
116
117 static int
118 piix_attach (device_t dev)
119 {
120         
121         return 0;
122 }
123
124 static device_method_t piix_methods[] = {
125         /* Device interface */
126         DEVMETHOD(device_probe,         piix_probe),
127         DEVMETHOD(device_attach,        piix_attach),
128         DEVMETHOD_END
129 };
130
131 static driver_t piix_driver = {
132         "piix",
133         piix_methods,
134         1,
135 };
136
137 static devclass_t piix_devclass;
138
139 DRIVER_MODULE(piix, pci, piix_driver, piix_devclass, NULL, NULL);
140
141 #endif
142