2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/pci/pci_compat.c,v 1.35.2.1 2001/10/14 21:14:14 luigi Exp $
27 * $DragonFly: src/sys/bus/pci/pci_compat.c,v 1.14 2007/05/13 18:33:56 swildner Exp $
33 /* for compatibility to FreeBSD-2.2 and 3.x versions of PCI code */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
45 #include <machine/smp.h>
46 #include <sys/interrupt.h>
48 #include <sys/pciio.h>
51 #include <bus/pci/pci_cfgreg.h>
53 struct pci_compat_driver {
55 struct pci_device *dvp;
58 /* ------------------------------------------------------------------------- */
61 pci_conf_read(pcici_t cfg, u_long reg)
63 return (pci_read_config(cfg->dev, reg, 4));
67 pci_conf_write(pcici_t cfg, u_long reg, u_long data)
69 pci_write_config(cfg->dev, reg, data, 4);
73 pci_cfgwrite(pcicfgregs *cfg, int reg, int data, int bytes)
75 pci_cfgregwrite(cfg->bus, cfg->slot, cfg->func, reg, data, bytes);
79 pci_map_port(pcici_t cfg, u_long reg, pci_port_t* pa)
85 res = bus_alloc_resource(cfg->dev, SYS_RES_IOPORT, &rid,
88 *pa = rman_get_start(res);
95 pci_map_mem(pcici_t cfg, u_long reg, vm_offset_t* va, vm_offset_t* pa)
101 res = bus_alloc_resource(cfg->dev, SYS_RES_MEMORY, &rid,
102 0, ~0, 1, RF_ACTIVE);
104 *pa = rman_get_start(res);
105 *va = (vm_offset_t) rman_get_virtual(res);
112 pci_map_int(pcici_t cfg, pci_inthand_t *handler, void *arg)
114 return (pci_map_int_right(cfg, handler, arg, 0));
118 pci_map_int_right(pcici_t cfg, pci_inthand_t *handler, void *arg, u_int intflags)
122 if (cfg->intpin != 0) {
123 int irq = cfg->intline;
125 struct resource *res;
127 int resflags = RF_SHAREABLE|RF_ACTIVE;
130 if (intflags & INTR_CLOCK)
132 if (intflags & INTR_EXCL)
133 resflags &= ~RF_SHAREABLE;
135 res = bus_alloc_resource(cfg->dev, SYS_RES_IRQ, &rid,
136 irq, irq, 1, resflags);
138 kprintf("pci_map_int: can't allocate interrupt\n");
143 * This is ugly. Translate the mask into an interrupt type.
146 error = BUS_SETUP_INTR(device_get_parent(cfg->dev), cfg->dev,
147 res, flags, handler, arg, &ih, NULL);
153 * XXX this apic stuff looks totally busted. It should
154 * move to the nexus code which actually registers the
163 pci_unmap_int(pcici_t cfg)
165 return (0); /* not supported, yet, since cfg doesn't know about idesc */
169 pci_get_parent_from_tag(pcici_t tag)
171 return (pcici_t)pci_devlist_get_parent(tag);
175 pci_get_bus_from_tag(pcici_t tag)
181 * A simple driver to wrap the old pci driver mechanism for back-compat.
185 pci_compat_probe(device_t dev)
187 struct pci_device *dvp;
188 struct pci_devinfo *dinfo;
193 dinfo = device_get_ivars(dev);
195 dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
198 * Do the wrapped probe.
201 if (dvp && dvp->pd_probe) {
202 name = dvp->pd_probe(cfg, (cfg->device << 16) + cfg->vendor);
204 device_set_desc_copy(dev, name);
205 /* Allow newbus drivers to match "better" */
214 pci_compat_attach(device_t dev)
216 struct pci_device *dvp;
217 struct pci_devinfo *dinfo;
221 dinfo = device_get_ivars(dev);
223 dvp = ((struct pci_compat_driver *)device_get_driver(dev))->dvp;
225 unit = device_get_unit(dev);
226 if (unit > *dvp->pd_count)
227 *dvp->pd_count = unit;
229 dvp->pd_attach(cfg, unit);
230 device_printf(dev, "driver is using old-style compatibility shims\n");
234 static device_method_t pci_compat_methods[] = {
235 /* Device interface */
236 DEVMETHOD(device_probe, pci_compat_probe),
237 DEVMETHOD(device_attach, pci_compat_attach),
243 * Create a new style driver around each old pci driver.
246 compat_pci_handler(module_t mod, int type, void *data)
248 struct pci_device *dvp = (struct pci_device *)data;
249 struct pci_compat_driver *driver;
250 devclass_t pci_devclass = devclass_find("pci");
254 driver = kmalloc(sizeof(struct pci_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
255 driver->driver.name = dvp->pd_name;
256 driver->driver.methods = pci_compat_methods;
257 driver->driver.size = sizeof(struct pci_devinfo *);
259 devclass_add_driver(pci_devclass, (driver_t *)driver);
262 kprintf("%s: module unload not supported!\n", dvp->pd_name);