pci: Put back PCI Express related bits
[dragonfly.git] / sys / bus / pci / fixup_pci.c
1 /*-
2  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000 BSDi
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  * __FBSDID("$FreeBSD: src/sys/dev/pci/fixup_pci.c,v 1.7.8.1 2009/04/15 03:14:26 kensmith Exp $");
30  */
31
32 #include <sys/cdefs.h>
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/types.h>
41
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44
45 /*
46  * Chipset fixups.
47  *
48  * These routines are invoked during the probe phase for devices which
49  * typically don't have specific device drivers, but which require
50  * some cleaning up.
51  */
52
53 static int      fixup_pci_probe(device_t dev);
54 static void     fixwsc_natoma(device_t dev);
55 static void     fixc1_nforce2(device_t dev);
56
57 static device_method_t fixup_pci_methods[] = {
58     /* Device interface */
59     DEVMETHOD(device_probe,             fixup_pci_probe),
60     DEVMETHOD(device_attach,            bus_generic_attach),
61     { 0, 0 }
62 };
63
64 static driver_t fixup_pci_driver = {
65     "fixup_pci",
66     fixup_pci_methods,
67     0,
68 };
69
70 static devclass_t fixup_pci_devclass;
71
72 DRIVER_MODULE(fixup_pci, pci, fixup_pci_driver, fixup_pci_devclass, 0, 0);
73
74 static int
75 fixup_pci_probe(device_t dev)
76 {
77     switch (pci_get_devid(dev)) {
78     case 0x12378086:            /* Intel 82440FX (Natoma) */
79         fixwsc_natoma(dev);
80         break;
81     case 0x01e010de:            /* nVidia nForce2 */
82         fixc1_nforce2(dev);
83         break;
84     }
85     return(ENXIO);
86 }
87
88 static void
89 fixwsc_natoma(device_t dev)
90 {
91     int         pmccfg;
92
93     pmccfg = pci_read_config(dev, 0x50, 2);
94 #if defined(SMP)
95     if (pmccfg & 0x8000) {
96         kprintf("Correcting Natoma config for SMP\n");
97         pmccfg &= ~0x8000;
98         pci_write_config(dev, 0x50, pmccfg, 2);
99     }
100 #else
101     if ((pmccfg & 0x8000) == 0) {
102         kprintf("Correcting Natoma config for non-SMP\n");
103         pmccfg |= 0x8000;
104         pci_write_config(dev, 0x50, pmccfg, 2);
105     }
106 #endif
107 }
108
109 /*
110  * Set the SYSTEM_IDLE_TIMEOUT to 80 ns on nForce2 systems to work
111  * around a hang that is triggered when the CPU generates a very fast
112  * CONNECT/HALT cycle sequence.  Specifically, the hang can result in
113  * the lapic timer being stopped.
114  *
115  * This requires changing the value for config register at offset 0x6c
116  * for the Host-PCI bridge at bus/dev/function 0/0/0:
117  *
118  * Chip Current Value   New Value
119  * ---- ----------      ----------
120  * C17  0x1F0FFF01      0x1F01FF01
121  * C18D 0x9F0FFF01      0x9F01FF01
122  *
123  * We do this by always clearing the bits in 0x000e0000.
124  *
125  * See also: http://lkml.org/lkml/2004/5/3/157
126  */
127 static void
128 fixc1_nforce2(device_t dev)
129 {
130         uint32_t val;
131
132         if (pci_get_bus(dev) == 0 && pci_get_slot(dev) == 0 &&
133             pci_get_function(dev) == 0) {
134                 val = pci_read_config(dev, 0x6c, 4);
135                 if (val & 0x000e0000) {
136                         kprintf("Correcting nForce2 C1 CPU disconnect hangs\n");
137                         val &= ~0x000e0000;
138                         pci_write_config(dev, 0x6c, val, 4);
139                 }
140         }
141 }