kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / netif / lnc / if_lnc_isa.c
1 /*      $NetBSD: if_le_isa.c,v 1.41 2005/12/24 20:27:41 perry Exp $     */
2 /*      $FreeBSD: src/sys/dev/le/if_le_isa.c,v 1.1 2006/05/17 21:25:22 marius Exp $     */
3
4 /*-
5  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
10  * Simulation Facility, NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the NetBSD
23  *      Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*-
42  * Copyright (c) 1992, 1993
43  *      The Regents of the University of California.  All rights reserved.
44  *
45  * This code is derived from software contributed to Berkeley by
46  * Ralph Campbell and Rick Macklem.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *      @(#)if_le.c     8.2 (Berkeley) 11/16/93
73  */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/bus.h>
78 #include <sys/endian.h>
79 #include <sys/kernel.h>
80 #include <sys/interrupt.h>
81 #include <sys/lock.h>
82 #include <sys/module.h>
83 #include <sys/resource.h>
84 #include <sys/rman.h>
85 #include <sys/socket.h>
86
87 #include <net/ethernet.h>
88 #include <net/if.h>
89 #include <net/if_media.h>
90 #include <net/if_arp.h>
91
92 #include <bus/isa/isavar.h>
93
94 #include <dev/netif/lnc/lancereg.h>
95 #include <dev/netif/lnc/lancevar.h>
96 #include <dev/netif/lnc/am7990var.h>
97
98 #define LE_ISA_MEMSIZE  (16*1024)
99 #define PCNET_RDP       0x10
100 #define PCNET_RAP       0x12
101
102 struct le_isa_softc {
103         struct am7990_softc     sc_am7990;      /* glue to MI code */
104
105         bus_size_t              sc_rap;         /* offsets to LANCE... */
106         bus_size_t              sc_rdp;         /* ...registers */
107
108         int                     sc_rrid;
109         struct resource         *sc_rres;
110         bus_space_tag_t         sc_regt;
111         bus_space_handle_t      sc_regh;
112
113         int                     sc_drid;
114         struct resource         *sc_dres;
115
116         int                     sc_irid;
117         struct resource         *sc_ires;
118         void                    *sc_ih;
119
120         bus_dma_tag_t           sc_pdmat;
121         bus_dma_tag_t           sc_dmat;
122         bus_dmamap_t            sc_dmam;
123 };
124
125 static device_probe_t le_isa_probe;
126 static device_attach_t le_isa_attach;
127 static device_detach_t le_isa_detach;
128 static device_resume_t le_isa_resume;
129 static device_suspend_t le_isa_suspend;
130
131 static device_method_t le_isa_methods[] = {
132         /* Device interface */
133         DEVMETHOD(device_probe,         le_isa_probe),
134         DEVMETHOD(device_attach,        le_isa_attach),
135         DEVMETHOD(device_detach,        le_isa_detach),
136         /* We can just use the suspend method here. */
137         DEVMETHOD(device_shutdown,      le_isa_suspend),
138         DEVMETHOD(device_suspend,       le_isa_suspend),
139         DEVMETHOD(device_resume,        le_isa_resume),
140
141         { 0, 0 }
142 };
143
144 DEFINE_CLASS_0(lnc, le_isa_driver, le_isa_methods, sizeof(struct le_isa_softc));
145 DRIVER_MODULE(lnc, isa, le_isa_driver, le_devclass, NULL, NULL);
146 MODULE_DEPEND(lnc, ether, 1, 1, 1);
147
148 struct le_isa_param {
149         const char      *name;
150         u_long          iosize;
151         bus_size_t      rap;
152         bus_size_t      rdp;
153         bus_size_t      macstart;
154         int             macstride;
155 } static const le_isa_params[] = {
156         { "BICC Isolan", 24, 0xe, 0xc, 0, 2 },
157         { "Novell NE2100", 16, 0x12, 0x10, 0, 1 }
158 };
159
160 static struct isa_pnp_id le_isa_ids[] = {
161         { 0x0322690e, "Cabletron E2200 Single Chip" },  /* CSI2203 */
162         { 0x0110490a, "Boca LANCard Combo" },           /* BRI1001 */
163         { 0x0100a60a, "Melco Inc. LGY-IV" },            /* BUF0001 */
164         { 0xd880d041, "Novell NE2100" },                /* PNP80D8 */
165         { 0x0082d041, "Cabletron E2100 Series DNI" },   /* PNP8200 */
166         { 0x3182d041, "AMD AM1500T/AM2100" },           /* PNP8231 */
167         { 0x8c82d041, "AMD PCnet-ISA" },                /* PNP828C */
168         { 0x8d82d041, "AMD PCnet-32" },                 /* PNP828D */
169         { 0xcefaedfe, "Racal InterLan EtherBlaster" },  /* _WMFACE */
170         { 0, NULL }
171 };
172
173 static void le_isa_wrcsr(struct lance_softc *, uint16_t, uint16_t);
174 static uint16_t le_isa_rdcsr(struct lance_softc *, uint16_t);
175 static bus_dmamap_callback_t le_isa_dma_callback;
176 static int le_isa_probe_legacy(device_t, const struct le_isa_param *);
177
178 static void
179 le_isa_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
180 {
181         struct le_isa_softc *lesc = (struct le_isa_softc *)sc;
182
183         bus_space_write_2(lesc->sc_regt, lesc->sc_regh, lesc->sc_rap, port);
184         bus_space_barrier(lesc->sc_regt, lesc->sc_regh, lesc->sc_rap, 2,
185             BUS_SPACE_BARRIER_WRITE);
186         bus_space_write_2(lesc->sc_regt, lesc->sc_regh, lesc->sc_rdp, val);
187 }
188
189 static uint16_t
190 le_isa_rdcsr(struct lance_softc *sc, uint16_t port)
191 {
192         struct le_isa_softc *lesc = (struct le_isa_softc *)sc;
193
194         bus_space_write_2(lesc->sc_regt, lesc->sc_regh, lesc->sc_rap, port);
195         bus_space_barrier(lesc->sc_regt, lesc->sc_regh, lesc->sc_rap, 2,
196             BUS_SPACE_BARRIER_WRITE);
197         return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, lesc->sc_rdp));
198 }
199
200 static void
201 le_isa_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
202 {
203         struct lance_softc *sc = (struct lance_softc *)xsc;
204
205         if (error != 0)
206                 return;
207         KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
208         sc->sc_addr = segs[0].ds_addr;
209 }
210
211 static int
212 le_isa_probe_legacy(device_t dev, const struct le_isa_param *leip)
213 {
214         struct le_isa_softc *lesc;
215         struct lance_softc *sc;
216         int error;
217
218         lesc = device_get_softc(dev);
219         sc = &lesc->sc_am7990.lsc;
220
221         lesc->sc_rrid = 0;
222         lesc->sc_rres = bus_alloc_resource(dev, SYS_RES_IOPORT, &lesc->sc_rrid,
223             0, ~0, leip->iosize, RF_ACTIVE);
224         if (lesc->sc_rres == NULL)
225                 return (ENXIO);
226         lesc->sc_regt = rman_get_bustag(lesc->sc_rres);
227         lesc->sc_regh = rman_get_bushandle(lesc->sc_rres);
228         lesc->sc_rap = leip->rap;
229         lesc->sc_rdp = leip->rdp;
230
231         /* Stop the chip and put it in a known state. */
232         le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
233         DELAY(100);
234         if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP) {
235                 error = ENXIO;
236                 goto fail;
237         }
238         le_isa_wrcsr(sc, LE_CSR3, 0);
239         error = 0;
240
241  fail:
242         bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres);
243         return (error);
244 }
245
246 static int
247 le_isa_probe(device_t dev)
248 {
249         int i;
250
251         switch (ISA_PNP_PROBE(device_get_parent(dev), dev, le_isa_ids)) {
252         case 0:
253                 return (-20);
254         case ENOENT:
255                 for (i = 0; i < sizeof(le_isa_params) /
256                     sizeof(le_isa_params[0]); i++) {
257                         if (le_isa_probe_legacy(dev, &le_isa_params[i]) == 0) {
258                                 device_set_desc(dev, le_isa_params[i].name);
259                                 return (-20);
260                         }
261                 }
262                 /* FALLTHROUGH */
263         case ENXIO:
264         default:
265                 return (ENXIO);
266         }
267 }
268
269 static int
270 le_isa_attach(device_t dev)
271 {
272         struct le_isa_softc *lesc;
273         struct lance_softc *sc;
274         bus_size_t macstart, rap, rdp;
275         int error, i, macstride;
276
277         lesc = device_get_softc(dev);
278         sc = &lesc->sc_am7990.lsc;
279
280         lesc->sc_rrid = 0;
281         switch (ISA_PNP_PROBE(device_get_parent(dev), dev, le_isa_ids)) {
282         case 0:
283                 lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
284                     &lesc->sc_rrid, RF_ACTIVE);
285                 rap = PCNET_RAP;
286                 rdp = PCNET_RDP;
287                 macstart = 0;
288                 macstride = 1;
289                 break;
290         case ENOENT:
291                 for (i = 0; i < sizeof(le_isa_params) /
292                     sizeof(le_isa_params[0]); i++) {
293                         if (le_isa_probe_legacy(dev, &le_isa_params[i]) == 0) {
294                                 lesc->sc_rres = bus_alloc_resource(dev,
295                                     SYS_RES_IOPORT, &lesc->sc_rrid, 0, ~0,
296                                     le_isa_params[i].iosize, RF_ACTIVE);
297                                 rap = le_isa_params[i].rap;
298                                 rdp = le_isa_params[i].rdp;
299                                 macstart = le_isa_params[i].macstart;
300                                 macstride = le_isa_params[i].macstride;
301                                 goto found;
302                         }
303                 }
304                 /* FALLTHROUGH */
305         case ENXIO:
306         default:
307                 device_printf(dev, "cannot determine chip\n");
308                 error = ENXIO;
309                 goto fail_mtx;
310         }
311
312  found:
313         if (lesc->sc_rres == NULL) {
314                 device_printf(dev, "cannot allocate registers\n");
315                 error = ENXIO;
316                 goto fail_mtx;
317         }
318         lesc->sc_regt = rman_get_bustag(lesc->sc_rres);
319         lesc->sc_regh = rman_get_bushandle(lesc->sc_rres);
320         lesc->sc_rap = rap;
321         lesc->sc_rdp = rdp;
322
323         lesc->sc_drid = 0;
324         if ((lesc->sc_dres = bus_alloc_resource_any(dev, SYS_RES_DRQ,
325             &lesc->sc_drid, RF_ACTIVE)) == NULL) {
326                 device_printf(dev, "cannot allocate DMA channel\n");
327                 error = ENXIO;
328                 goto fail_rres;
329         }
330
331         lesc->sc_irid = 0;
332         if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
333             &lesc->sc_irid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
334                 device_printf(dev, "cannot allocate interrupt\n");
335                 error = ENXIO;
336                 goto fail_dres;
337         }
338
339         error = bus_dma_tag_create(
340             NULL,                       /* parent */
341             1, 0,                       /* alignment, boundary */
342             BUS_SPACE_MAXADDR_24BIT,    /* lowaddr */
343             BUS_SPACE_MAXADDR,          /* highaddr */
344             NULL, NULL,                 /* filter, filterarg */
345             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
346             0,                          /* nsegments */
347             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
348             BUS_DMA_WAITOK,             /* flags */
349             &lesc->sc_pdmat);
350         if (error != 0) {
351                 device_printf(dev, "cannot allocate parent DMA tag\n");
352                 goto fail_ires;
353         }
354
355         sc->sc_memsize = LE_ISA_MEMSIZE;
356         /*
357          * For Am79C90, Am79C961 and Am79C961A the init block must be 2-byte
358          * aligned and the ring descriptors must be 8-byte aligned.
359          */
360         error = bus_dma_tag_create(
361             lesc->sc_pdmat,             /* parent */
362             8, 0,                       /* alignment, boundary */
363             BUS_SPACE_MAXADDR_24BIT,    /* lowaddr */
364             BUS_SPACE_MAXADDR,          /* highaddr */
365             NULL, NULL,                 /* filter, filterarg */
366             sc->sc_memsize,             /* maxsize */
367             1,                          /* nsegments */
368             sc->sc_memsize,             /* maxsegsize */
369             BUS_DMA_WAITOK,             /* flags */
370             &lesc->sc_dmat);
371         if (error != 0) {
372                 device_printf(dev, "cannot allocate buffer DMA tag\n");
373                 goto fail_pdtag;
374         }
375
376         error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
377             BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
378         if (error != 0) {
379                 device_printf(dev, "cannot allocate DMA buffer memory\n");
380                 goto fail_dtag;
381         }
382
383         sc->sc_addr = 0;
384         error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
385             sc->sc_memsize, le_isa_dma_callback, sc, 0);
386         if (error != 0 || sc->sc_addr == 0) {
387                 device_printf(dev, "cannot load DMA buffer map\n");
388                 goto fail_dmem;
389         }
390
391         isa_dmacascade(rman_get_start(lesc->sc_dres));
392
393         sc->sc_flags = 0;
394         sc->sc_conf3 = 0;
395
396         /*
397          * Extract the physical MAC address from the ROM.
398          */
399         for (i = 0; i < sizeof(sc->sc_enaddr); i++)
400                 sc->sc_enaddr[i] =  bus_space_read_1(lesc->sc_regt,
401                     lesc->sc_regh, macstart + i * macstride);
402
403         sc->sc_copytodesc = lance_copytobuf_contig;
404         sc->sc_copyfromdesc = lance_copyfrombuf_contig;
405         sc->sc_copytobuf = lance_copytobuf_contig;
406         sc->sc_copyfrombuf = lance_copyfrombuf_contig;
407         sc->sc_zerobuf = lance_zerobuf_contig;
408
409         sc->sc_rdcsr = le_isa_rdcsr;
410         sc->sc_wrcsr = le_isa_wrcsr;
411         sc->sc_hwreset = NULL;
412         sc->sc_hwinit = NULL;
413         sc->sc_hwintr = NULL;
414         sc->sc_nocarrier = NULL;
415         sc->sc_mediachange = NULL;
416         sc->sc_mediastatus = NULL;
417         sc->sc_supmedia = NULL;
418
419         error = am7990_config(&lesc->sc_am7990, device_get_name(dev),
420             device_get_unit(dev));
421         if (error != 0) {
422                 device_printf(dev, "cannot attach Am7990\n");
423                 goto fail_dmap;
424         }
425
426         error = bus_setup_intr(dev, lesc->sc_ires, INTR_MPSAFE,
427             am7990_intr, sc, &lesc->sc_ih, sc->ifp->if_serializer);
428         if (error != 0) {
429                 device_printf(dev, "cannot set up interrupt\n");
430                 goto fail_am7990;
431         }
432
433         sc->ifp->if_cpuid = ithread_cpuid(rman_get_start(lesc->sc_ires));
434         KKASSERT(sc->ifp->if_cpuid >= 0 && sc->ifp->if_cpuid < ncpus);
435
436         return (0);
437
438  fail_am7990:
439         am7990_detach(&lesc->sc_am7990);
440  fail_dmap:
441         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
442  fail_dmem:
443         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
444  fail_dtag:
445         bus_dma_tag_destroy(lesc->sc_dmat);
446  fail_pdtag:
447         bus_dma_tag_destroy(lesc->sc_pdmat);
448  fail_ires:
449         bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires);
450  fail_dres:
451         bus_release_resource(dev, SYS_RES_DRQ, lesc->sc_drid, lesc->sc_dres);
452  fail_rres:
453         bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres);
454  fail_mtx:
455         return (error);
456 }
457
458 static int
459 le_isa_detach(device_t dev)
460 {
461         struct le_isa_softc *lesc;
462         struct lance_softc *sc;
463
464         lesc = device_get_softc(dev);
465         sc = &lesc->sc_am7990.lsc;
466
467         if (device_is_attached(dev)) {
468                 lwkt_serialize_enter(sc->ifp->if_serializer);
469                 lance_stop(sc);
470                 bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
471                 lwkt_serialize_exit(sc->ifp->if_serializer);
472
473                 am7990_detach(&lesc->sc_am7990);
474         }
475
476         if (lesc->sc_ires)
477                 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires);
478         if (lesc->sc_dres)
479                 bus_release_resource(dev, SYS_RES_DRQ, lesc->sc_drid, lesc->sc_dres);
480         if (lesc->sc_rres)
481                 bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres);
482         if (lesc->sc_dmam) {
483                 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
484                 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
485         }
486         if (lesc->sc_dmat)
487                 bus_dma_tag_destroy(lesc->sc_dmat);
488         if (lesc->sc_pdmat)
489                 bus_dma_tag_destroy(lesc->sc_pdmat);
490
491         return (0);
492 }
493
494 static int
495 le_isa_suspend(device_t dev)
496 {
497         struct le_isa_softc *lesc;
498
499         lesc = device_get_softc(dev);
500
501         lance_suspend(&lesc->sc_am7990.lsc);
502
503         return (0);
504 }
505
506 static int
507 le_isa_resume(device_t dev)
508 {
509         struct le_isa_softc *lesc;
510
511         lesc = device_get_softc(dev);
512
513         lance_resume(&lesc->sc_am7990.lsc);
514
515         return (0);
516 }