69d01e94323ac851c52aa28b4a34315b9d4b29cd
[dragonfly.git] / sys / dev / netif / lnc / if_lnc_isa.c
1 /*
2  * Copyright (c) 1994-2000
3  *      Paul Richards. 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  *    verbatim and that no modifications are made prior to this
11  *    point in the file.
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 Paul Richards 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 PAUL RICHARDS ``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 PAUL RICHARDS 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  *
30  * $FreeBSD: src/sys/dev/lnc/if_lnc_isa.c,v 1.12 2001/07/04 13:00:19 nyan Exp $
31  * $DragonFly: src/sys/dev/netif/lnc/if_lnc_isa.c,v 1.7 2005/11/28 17:13:43 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/serialize.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45 #include <sys/thread2.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50
51 #include <bus/isa/isavar.h>
52
53 #include <dev/netif/lnc/if_lncvar.h>
54 #include <dev/netif/lnc/if_lncreg.h>
55
56 static int      lnc_isa_detach(device_t);
57
58 static struct isa_pnp_id lnc_pnp_ids[] = {
59         {0,     NULL}
60 };
61
62 static int
63 lnc_legacy_probe(device_t dev)
64 {
65         struct lnc_softc *sc = device_get_softc(dev);
66
67         sc->portrid = 0;
68         sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->portrid,
69             RF_ACTIVE);
70
71         if (! sc->portres) {
72                 device_printf(dev, "Failed to allocate I/O ports\n");
73                 return (ENXIO);
74         }
75
76         sc->lnc_btag = rman_get_bustag(sc->portres);
77         sc->lnc_bhandle = rman_get_bushandle(sc->portres);
78
79         /*
80          * There isn't any way to determine if a NIC is a BICC. Basically, if
81          * the lance probe succeeds using the i/o addresses of the BICC then
82          * we assume it's a BICC.
83          *
84          */
85         sc->rap = BICC_RAP;
86         sc->rdp = BICC_RDP;
87         sc->nic.mem_mode = DMA_FIXED;
88         /* XXX Should set BICC_IOSIZE et al somewhere to alloc
89            resources correctly */
90
91         if ((sc->nic.ic = lance_probe(sc))) {
92                 device_set_desc(dev, "BICC Isolan");
93                 sc->nic.ident = BICC;
94                 lnc_isa_detach(dev);
95                 return (0);
96         } else {
97             /* It's not a BICC so try the standard NE2100 ports */
98             sc->rap = PCNET_RAP;
99             sc->rdp = PCNET_RDP;
100             if ((sc->nic.ic = lance_probe(sc))) {
101                 sc->nic.ident = NE2100;
102                 device_set_desc(dev, "NE2100");
103                 lnc_isa_detach(dev);
104                 return (0);
105             } else {
106                 lnc_isa_detach(dev);
107                 return (ENXIO);
108             }
109         }
110 }
111
112 static int
113 lnc_isa_probe(device_t dev)
114 {
115         int pnp;
116
117         pnp = ISA_PNP_PROBE(device_get_parent(dev), dev, lnc_pnp_ids);
118         if (pnp == ENOENT) {
119                 /* It's not a PNP card, see if we support it by probing it */
120                 return (lnc_legacy_probe(dev));
121         } else if (pnp == ENXIO) {
122                 return (ENXIO);
123         } else {
124                 /* Found PNP card we support */
125                 return (0);
126         }
127 }
128
129 static void
130 lnc_alloc_callback(void *arg, bus_dma_segment_t *seg, int nseg, int error)
131 {
132         /* Do nothing */
133         return;
134 }
135
136 static int
137 lnc_isa_attach(device_t dev)
138 {
139         lnc_softc_t *sc = device_get_softc(dev);
140         int error = 0;
141         bus_size_t lnc_mem_size;
142
143         /*
144          * The probe routines can allocate resources and to make our
145          * live easier, bzero the softc now.
146          */
147         bzero(sc, sizeof(*sc));
148
149         sc->portrid = 0;
150         sc->portres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->portrid,
151             RF_ACTIVE);
152
153         if (!sc->portres) {
154                 device_printf(dev, "Failed to allocate I/O ports\n");
155                 error = ENXIO;
156                 goto fail;
157         }
158
159         sc->drqrid = 0;
160         sc->drqres = bus_alloc_resource_any(dev, SYS_RES_DRQ, &sc->drqrid,
161             RF_ACTIVE);
162
163         if (!sc->drqres) {
164                 device_printf(dev, "Failed to allocate DMA channel\n");
165                 error = ENXIO;
166                 goto fail;
167         }
168
169         if (isa_get_irq(dev) == -1)
170                 bus_set_resource(dev, SYS_RES_IRQ, 0, 10, 1);
171
172         sc->irqrid = 0;
173         sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
174             RF_ACTIVE);
175
176         if (! sc->irqres) {
177                 device_printf(dev, "Failed to allocate irq\n");
178                 error = ENXIO;
179                 goto fail;
180         }
181
182         /* XXX temp setting for nic */
183         sc->nic.mem_mode = DMA_FIXED;
184         sc->nrdre  = NRDRE;
185         sc->ntdre  = NTDRE;
186
187         if (sc->nic.ident == NE2100) {
188             sc->rap = PCNET_RAP;
189             sc->rdp = PCNET_RDP;
190             sc->bdp = PCNET_BDP;
191         } else {
192             sc->rap = BICC_RAP;
193             sc->rdp = BICC_RDP;
194         }
195
196         /* Create a DMA tag describing the ring memory we need */
197
198         lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
199                          sizeof(struct host_ring_entry));
200
201         lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) +
202                         (NDESC(sc->ntdre) * TRANSBUFSIZE);
203
204         error = bus_dma_tag_create(NULL,                /* parent */
205                                    4,                   /* alignement */
206                                    0,                   /* boundary */
207                                    BUS_SPACE_MAXADDR_24BIT,     /* lowaddr */
208                                    BUS_SPACE_MAXADDR,   /* highaddr */
209                                    NULL, NULL,          /* filter, filterarg */
210                                    lnc_mem_size,        /* segsize */
211                                    1,                   /* nsegments */
212                                    BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
213                                    BUS_DMA_ALLOCNOW,    /* flags */
214                                    &sc->dmat);
215
216         if (error) {
217                 device_printf(dev, "Can't create DMA tag\n");
218                 goto fail;
219         }
220
221         error = bus_dmamem_alloc(sc->dmat, (void **)&sc->recv_ring,
222                                BUS_DMA_WAITOK, &sc->dmamap);
223
224         if (error) {
225                 device_printf(dev, "Couldn't allocate memory\n");
226                 goto fail;
227         }
228
229         error = bus_dmamap_load(sc->dmat, sc->dmamap, sc->recv_ring,
230                                 lnc_mem_size, lnc_alloc_callback,
231                                 sc->recv_ring, 0);
232
233         if (error) {
234                 device_printf(dev, "Couldn't load DMA map\n");
235                 goto fail;
236         }
237
238         isa_dmacascade(rman_get_start(sc->drqres));
239
240         /* Call generic attach code */
241         if (! lnc_attach_common(dev)) {
242                 device_printf(dev, "Generic attach code failed\n");
243                 error = ENXIO;
244                 goto fail;
245         }
246
247         error = bus_setup_intr(dev, sc->irqres, INTR_NETSAFE, lncintr,
248                                sc, &sc->intrhand, 
249                                sc->arpcom.ac_if.if_serializer);
250         if (error) {
251                 device_printf(dev, "Failed to setup irq handler\n");
252                 ether_ifdetach(&sc->arpcom.ac_if);
253                 goto fail;
254         }
255
256         return (0);
257
258 fail:
259         lnc_isa_detach(dev);
260         return(error);
261 }
262
263 static int
264 lnc_isa_detach(device_t dev)
265 {
266         lnc_softc_t *sc = device_get_softc(dev);
267
268         lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
269
270         if (device_is_attached(dev)) {
271                 ether_ifdetach(&sc->arpcom.ac_if);
272                 lnc_stop(sc);
273         }
274
275         if (sc->intrhand)
276                 bus_teardown_intr(dev, sc->irqres, sc->intrhand);
277
278         if (sc->irqres)
279                 bus_release_resource(dev, SYS_RES_IRQ, sc->irqrid, sc->irqres);
280         if (sc->portres)
281                 bus_release_resource(dev, SYS_RES_IOPORT,
282                                      sc->portrid, sc->portres);
283         if (sc->drqres)
284                 bus_release_resource(dev, SYS_RES_DRQ, sc->drqrid, sc->drqres);
285         if (sc->dmamap) {
286                 bus_dmamap_unload(sc->dmat, sc->dmamap);
287                 bus_dmamem_free(sc->dmat, sc->recv_ring, sc->dmamap);
288         }
289         if (sc->dmat)
290                 bus_dma_tag_destroy(sc->dmat);
291
292         lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
293         return (0);
294 }
295
296 static device_method_t lnc_isa_methods[] = {
297 /*      DEVMETHOD(device_identify,      lnc_isa_identify), */
298         DEVMETHOD(device_probe,         lnc_isa_probe),
299         DEVMETHOD(device_attach,        lnc_isa_attach),
300         DEVMETHOD(device_detach,        lnc_isa_detach),
301 #ifdef notyet
302         DEVMETHOD(device_suspend,       lnc_isa_suspend),
303         DEVMETHOD(device_resume,        lnc_isa_resume),
304         DEVMETHOD(device_shutdown,      lnc_isa_shutdown),
305 #endif
306         { 0, 0 }
307 };
308
309 static driver_t lnc_isa_driver = {
310         "lnc",
311         lnc_isa_methods,
312         sizeof(struct lnc_softc),
313 };
314
315 DRIVER_MODULE(if_lnc, isa, lnc_isa_driver, lnc_devclass, 0, 0);