169783Smsmith/*-
269783Smsmith * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
369783Smsmith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
469783Smsmith * Copyright (c) 2000 BSDi
569783Smsmith * All rights reserved.
669783Smsmith *
769783Smsmith * Redistribution and use in source and binary forms, with or without
869783Smsmith * modification, are permitted provided that the following conditions
969783Smsmith * are met:
1069783Smsmith * 1. Redistributions of source code must retain the above copyright
1169783Smsmith *    notice, this list of conditions and the following disclaimer.
1269783Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1369783Smsmith *    notice, this list of conditions and the following disclaimer in the
1469783Smsmith *    documentation and/or other materials provided with the distribution.
1569783Smsmith * 3. The name of the author may not be used to endorse or promote products
1669783Smsmith *    derived from this software without specific prior written permission.
1769783Smsmith *
1869783Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1969783Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2069783Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2169783Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2269783Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2369783Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2469783Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2569783Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2669783Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2769783Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2869783Smsmith * SUCH DAMAGE.
2969783Smsmith */
3069783Smsmith
31119418Sobrien#include <sys/cdefs.h>
32119418Sobrien__FBSDID("$FreeBSD$");
33119418Sobrien
3469783Smsmith#include <sys/param.h>
35129876Sphk#include <sys/kernel.h>
36129876Sphk#include <sys/malloc.h>
37129876Sphk#include <sys/module.h>
3869783Smsmith#include <sys/systm.h>
3969783Smsmith#include <sys/bus.h>
4069783Smsmith
41119285Simp#include <dev/pci/pcivar.h>
42119285Simp#include <dev/pci/pcireg.h>
4369783Smsmith
4469783Smsmith/*
4569783Smsmith * Chipset fixups.
4669783Smsmith *
4769783Smsmith * These routines are invoked during the probe phase for devices which
4869783Smsmith * typically don't have specific device drivers, but which require
4969783Smsmith * some cleaning up.
5069783Smsmith */
5169783Smsmith
5269783Smsmithstatic int	fixup_pci_probe(device_t dev);
5369783Smsmithstatic void	fixwsc_natoma(device_t dev);
54158881Sjhbstatic void	fixc1_nforce2(device_t dev);
5569783Smsmith
5669783Smsmithstatic device_method_t fixup_pci_methods[] = {
5769783Smsmith    /* Device interface */
5869783Smsmith    DEVMETHOD(device_probe,		fixup_pci_probe),
5969783Smsmith    DEVMETHOD(device_attach,		bus_generic_attach),
6069783Smsmith    { 0, 0 }
6169783Smsmith};
6269783Smsmith
6369783Smsmithstatic driver_t fixup_pci_driver = {
6469783Smsmith    "fixup_pci",
6569783Smsmith    fixup_pci_methods,
6669783Smsmith    0,
6769783Smsmith};
6869783Smsmith
6969783Smsmithstatic devclass_t fixup_pci_devclass;
7069783Smsmith
7169783SmsmithDRIVER_MODULE(fixup_pci, pci, fixup_pci_driver, fixup_pci_devclass, 0, 0);
7269783Smsmith
7369783Smsmithstatic int
7469783Smsmithfixup_pci_probe(device_t dev)
7569783Smsmith{
7669783Smsmith    switch (pci_get_devid(dev)) {
7769783Smsmith    case 0x12378086:		/* Intel 82440FX (Natoma) */
7869783Smsmith	fixwsc_natoma(dev);
7969783Smsmith	break;
80158881Sjhb    case 0x01e010de:		/* nVidia nForce2 */
81158881Sjhb	fixc1_nforce2(dev);
82158881Sjhb	break;
8369783Smsmith    }
8469783Smsmith    return(ENXIO);
8569783Smsmith}
8669783Smsmith
8769783Smsmithstatic void
8869783Smsmithfixwsc_natoma(device_t dev)
8969783Smsmith{
9069783Smsmith    int		pmccfg;
9169783Smsmith
9269783Smsmith    pmccfg = pci_read_config(dev, 0x50, 2);
9369783Smsmith#if defined(SMP)
9469783Smsmith    if (pmccfg & 0x8000) {
95254207Srpaulo	device_printf(dev, "correcting Natoma config for SMP\n");
9669783Smsmith	pmccfg &= ~0x8000;
9777649Speter	pci_write_config(dev, 0x50, pmccfg, 2);
9869783Smsmith    }
9969783Smsmith#else
10069783Smsmith    if ((pmccfg & 0x8000) == 0) {
101254207Srpaulo	device_printf(dev, "correcting Natoma config for non-SMP\n");
10269783Smsmith	pmccfg |= 0x8000;
10377649Speter	pci_write_config(dev, 0x50, pmccfg, 2);
10469783Smsmith    }
10569783Smsmith#endif
10669783Smsmith}
107158881Sjhb
108158881Sjhb/*
109158881Sjhb * Set the SYSTEM_IDLE_TIMEOUT to 80 ns on nForce2 systems to work
110158881Sjhb * around a hang that is triggered when the CPU generates a very fast
111158881Sjhb * CONNECT/HALT cycle sequence.  Specifically, the hang can result in
112158881Sjhb * the lapic timer being stopped.
113158881Sjhb *
114158881Sjhb * This requires changing the value for config register at offset 0x6c
115158881Sjhb * for the Host-PCI bridge at bus/dev/function 0/0/0:
116158881Sjhb *
117158881Sjhb * Chip	Current Value	New Value
118158881Sjhb * ----	----------	----------
119158881Sjhb * C17	0x1F0FFF01	0x1F01FF01
120158881Sjhb * C18D	0x9F0FFF01	0x9F01FF01
121158881Sjhb *
122158881Sjhb * We do this by always clearing the bits in 0x000e0000.
123158881Sjhb *
124158881Sjhb * See also: http://lkml.org/lkml/2004/5/3/157
125158881Sjhb */
126158881Sjhbstatic void
127158881Sjhbfixc1_nforce2(device_t dev)
128158881Sjhb{
129158881Sjhb	uint32_t val;
130158881Sjhb
131158881Sjhb	if (pci_get_bus(dev) == 0 && pci_get_slot(dev) == 0 &&
132158881Sjhb	    pci_get_function(dev) == 0) {
133158881Sjhb		val = pci_read_config(dev, 0x6c, 4);
134158881Sjhb		if (val & 0x000e0000) {
135254207Srpaulo			device_printf(dev,
136254207Srpaulo			    "correcting nForce2 C1 CPU disconnect hangs\n");
137158881Sjhb			val &= ~0x000e0000;
138158881Sjhb			pci_write_config(dev, 0x6c, val, 4);
139158881Sjhb		}
140158881Sjhb	}
141158881Sjhb}
142