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