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