65a19244a2e14f93da4cc51bc1e96f53f5105660
[dragonfly.git] / sys / dev / netif / vx / if_vx_pci.c
1 /*
2  * Copyright (C) 1996 Naoki Hamada <nao@tom-yam.or.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/vx/if_vx_pci.c,v 1.21 2000/05/28 15:59:52 peter Exp $
30  * $DragonFly: src/sys/dev/netif/vx/if_vx_pci.c,v 1.11 2005/07/07 13:23:28 joerg Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37
38 #include <net/if.h>
39 #include <net/if_arp.h>
40
41 #include <machine/bus_pio.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
46
47 #include <bus/pci/pcidevs.h>
48 #include <bus/pci/pcivar.h>
49 #include <bus/pci/pcireg.h>
50
51 #include "if_vxreg.h"
52
53 static const struct vx_pci_type {
54         uint16_t         vx_vid;
55         uint16_t         vx_did;
56         const char      *vx_desc;
57 } vx_pci_types[] = {
58         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C590,
59                 "3Com 3c590 PCI Ethernet Adapter" },
60         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C595TX,
61                 "3Com 3c595-TX PCI Ethernet Adapter" },
62         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C595T4,
63                 "3Com 3c595-T4 PCI Ethernet Adapter" },
64         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C595MII,
65                 "3Com 3c595-MII PCI Ethernet Adapter" },
66
67         /*
68          * The (Fast) Etherlink XL adapters are now supported by
69          * the xl driver, which uses bus master DMA and is much
70          * faster. (And which also supports the 3c905B.
71          */
72 #ifdef VORTEX_ETHERLINK_XL
73         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C900TPO,
74                 "3Com 3c900-TPO Fast Etherlink XL" },
75         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C900COMBO,
76                 "3Com 3c900-COMBO Fast Etherlink XL" },
77         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C905TX,
78                 "3Com 3c905-TX Fast Etherlink XL" },
79         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C905T4,
80                 "3Com 3c905-T4 Fast Etherlink XL" },
81 #endif
82         { 0, 0, NULL }
83 };
84
85 static void vx_pci_shutdown(device_t);
86 static int vx_pci_probe(device_t);
87 static int vx_pci_attach(device_t);
88
89 static device_method_t vx_methods[] = {
90         /* Device interface */
91         DEVMETHOD(device_probe,         vx_pci_probe),
92         DEVMETHOD(device_attach,        vx_pci_attach),
93         DEVMETHOD(device_shutdown,      vx_pci_shutdown),
94
95         { 0, 0 }
96 };
97
98 static driver_t vx_driver = {
99         "vx",
100         vx_methods,
101         sizeof(struct vx_softc)
102 };
103
104 static devclass_t vx_devclass;
105
106 DRIVER_MODULE(if_vx, pci, vx_driver, vx_devclass, 0, 0);
107
108 static void
109 vx_pci_shutdown(device_t dev)
110 {
111    struct vx_softc      *sc;
112
113    sc = device_get_softc(dev);
114    vxstop(sc); 
115 }
116
117 static int
118 vx_pci_probe(device_t dev)
119 {
120         const struct vx_pci_type *t;
121         uint16_t vid, did;
122
123         vid = pci_get_vendor(dev);
124         did = pci_get_device(dev);
125         for (t = vx_pci_types; t->vx_desc != NULL; ++t) {
126                 if (vid == t->vx_vid && did == t->vx_did) {
127                         device_set_desc(dev, t->vx_desc);
128                         return 0;
129                 }
130         }
131         return ENXIO;
132 }
133
134 static int 
135 vx_pci_attach(device_t dev)
136 {
137     struct vx_softc *sc;
138     int rid;
139
140     sc = device_get_softc(dev);
141
142     rid = PCIR_MAPS;
143     sc->vx_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
144         RF_ACTIVE);
145
146     if (sc->vx_res == NULL)
147         goto bad;
148
149     sc->vx_btag = rman_get_bustag(sc->vx_res);
150     sc->vx_bhandle = rman_get_bushandle(sc->vx_res);
151
152     rid = 0;
153     sc->vx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
154         RF_SHAREABLE | RF_ACTIVE);
155
156     if (sc->vx_irq == NULL)
157         goto bad;
158
159     if (vxattach(dev) == 0) {
160         goto bad;
161     }
162
163     /* defect check for 3C590 */
164     if (pci_get_device(dev) == PCI_PRODUCT_3COM_3C590) {
165         GO_WINDOW(0);
166         if (vxbusyeeprom(sc))
167             goto bad;
168         CSR_WRITE_2(sc, VX_W0_EEPROM_COMMAND,
169             EEPROM_CMD_RD | EEPROM_SOFT_INFO_2);
170         if (vxbusyeeprom(sc))
171             goto bad;
172         if (!(CSR_READ_2(sc, VX_W0_EEPROM_DATA) & NO_RX_OVN_ANOMALY)) {
173             printf("Warning! Defective early revision adapter!\n");
174         }
175     }
176
177     if (bus_setup_intr(dev, sc->vx_irq, INTR_TYPE_NET,
178                        vxintr, sc, &sc->vx_intrhand, NULL)) {
179         ether_ifdetach(&sc->arpcom.ac_if);
180         goto bad;
181     }
182
183     return(0);
184
185 bad:
186     if (sc->vx_res != NULL)
187         bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->vx_res);
188     if (sc->vx_irq != NULL)
189         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vx_irq);
190     return(ENXIO);
191 }