Use pcidevs.h.
[dragonfly.git] / sys / dev / netif / fpa / if_fpa.c
1 /*-
2  * Copyright (c) 1995, 1996 Matt Thomas <matt@3am-software.com>
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. The name of the author may not be used to endorse or promote products
11  *    derived from this software withough specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/dev/pdq/if_fpa.c,v 1.13 1999/08/28 00:50:50 peter Exp $
25  * $DragonFly: src/sys/dev/netif/fpa/Attic/if_fpa.c,v 1.14 2006/08/01 18:03:19 swildner Exp $
26  */
27
28 /*
29  * DEC PDQ FDDI Controller; code for BSD derived operating systems
30  *
31  *   This module supports the DEC DEFPA PCI FDDI Controller
32  */
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #if defined(__bsdi__) || defined(__NetBSD__)
38 #include <sys/device.h>
39 #endif
40 #include <sys/serialize.h>
41
42 #include <net/if.h>
43
44 #if defined(__DragonFly__) || defined(__FreeBSD__)
45 #include "use_fpa.h"
46 #include <sys/eventhandler.h>
47 #include <net/ethernet.h>
48 #include <net/if_arp.h>
49 #include <bus/pci/pcidevs.h>
50 #include <bus/pci/pcivar.h>
51 #include <dev/netif/pdq_layer/pdqvar.h>
52 #include <dev/netif/pdq_layer/pdqreg.h>
53 #elif defined(__bsdi__)
54 #include <i386/isa/isavar.h>
55 #include <i386/icu/icu.h>
56 #ifndef DRQNONE
57 #define DRQNONE         0
58 #endif
59 #if _BSDI_VERSION < 199401
60 #define IRQSHARE        0
61 #endif
62 #elif defined(__NetBSD__)
63 #include <dev/pci/pcivar.h>
64 #include <dev/ic/pdqvar.h>
65 #include <dev/ic/pdqreg.h>
66 #endif /* __NetBSD__ */
67
68 #define PCI_VENDORID(x)         ((x) & 0xFFFF)
69 #define PCI_CHIPID(x)           (((x) >> 16) & 0xFFFF)
70
71 #define DEFPA_LATENCY   0x88
72
73 #define PCI_CFLT        0x0C    /* Configuration Latency */
74 #define PCI_CBMA        0x10    /* Configuration Base Memory Address */
75 #define PCI_CBIO        0x14    /* Configuration Base I/O Address */
76
77 #if defined(__DragonFly__) || defined(__FreeBSD__)
78 #if NFPA < 4
79 #undef NFPA
80 #define NFPA    4
81 #endif
82 static pdq_softc_t *pdqs_pci[NFPA];
83 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     (pdqs_pci[unit])
84 #if BSD >= 199506
85 #define pdq_pci_ifwatchdog              NULL
86 #endif
87
88 #elif defined(__bsdi__)
89 extern struct cfdriver fpacd;
90 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     ((pdq_softc_t *)fpacd.cd_devs[unit])
91
92 #elif defined(__NetBSD__)
93 extern struct cfattach fpa_ca;
94 extern struct cfdriver fpa_cd;
95 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     ((pdq_softc_t *)fpa_cd.cd_devs[unit])
96 #define pdq_pci_ifwatchdog              NULL
97 #endif
98
99 #ifndef pdq_pci_ifwatchdog
100 static ifnet_ret_t
101 pdq_pci_ifwatchdog(
102     int unit)
103 {
104     pdq_ifwatchdog(&PDQ_PCI_UNIT_TO_SOFTC(unit)->sc_if);
105 }
106 #endif
107
108 #if defined(__DragonFly__) || (defined(__FreeBSD__) && BSD >= 199506)
109 static void
110 pdq_pci_ifintr(
111     void *arg)
112 {
113     pdq_softc_t *sc = arg;
114
115     lwkt_serialize_enter(sc->sc_if.if_serializer);
116     pdq_interrupt(sc->sc_pdq);
117     lwkt_serialize_exit(sc->sc_if.if_serializer);
118 }
119 #else
120 static int
121 pdq_pci_ifintr(
122     void *arg)
123 {
124     pdq_softc_t * const sc = (pdq_softc_t *) arg;
125 #if defined(__DragonFly__) || defined(__FreeBSD__)
126     return pdq_interrupt(sc->sc_pdq);
127 #elif defined(__bsdi__) || defined(__NetBSD__)
128     (void) pdq_interrupt(sc->sc_pdq);
129     return 1;
130 #endif
131 }
132 #endif /* __FreeBSD && BSD */
133
134 #if defined(__DragonFly__) || defined(__FreeBSD__)
135 static void pdq_pci_shutdown(void *, int);
136
137 /*
138  * This is the PCI configuration support.  Since the PDQ is available
139  * on both EISA and PCI boards, one must be careful in how defines the
140  * PDQ in the config file.
141  */
142 static const char *
143 pdq_pci_probe(
144     pcici_t config_id,
145     pcidi_t device_id)
146 {
147     if (PCI_VENDORID(device_id) == PCI_VENDOR_DEC &&
148             PCI_CHIPID(device_id) == PCI_PRODUCT_DEC_DEFPA)
149         return "Digital DEFPA PCI FDDI Controller";
150     return NULL;
151 }
152
153 static void
154 pdq_pci_attach(
155     pcici_t config_id,
156     int unit)
157 {
158     pdq_softc_t *sc;
159     vm_offset_t va_csrs, pa_csrs;
160     pdq_uint32_t data;
161
162     if (unit == NFPA) {
163         printf("fpa%d: not configured; kernel is built for only %d device%s.\n",
164                unit, NFPA, NFPA == 1 ? "" : "s");
165         return;
166     }
167
168     data = pci_conf_read(config_id, PCI_CFLT);
169     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
170         data &= ~0xFF00;
171         data |= DEFPA_LATENCY << 8;
172         pci_conf_write(config_id, PCI_CFLT, data);
173     }
174
175     sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
176     if (!pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs)) {
177         free((void *) sc, M_DEVBUF);
178         return;
179     }
180
181     if_initname(&(sc->sc_if), "fpa", unit);
182     sc->sc_membase = (pdq_bus_memaddr_t) va_csrs;
183     sc->sc_pdq = pdq_initialize(PDQ_BUS_PCI, sc->sc_membase,
184                                 sc->sc_if.if_dname, sc->sc_if.if_dunit,
185                                 (void *) sc, PDQ_DEFPA);
186     if (sc->sc_pdq == NULL) {
187         free((void *) sc, M_DEVBUF);
188         return;
189     }
190     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
191     pdqs_pci[unit] = sc;
192     pdq_ifattach(sc, pdq_pci_ifwatchdog);
193     pci_map_int(config_id, pdq_pci_ifintr, (void*) sc);
194     EVENTHANDLER_REGISTER(shutdown_post_sync, pdq_pci_shutdown, sc,
195                           SHUTDOWN_PRI_DEFAULT);
196
197 }
198
199 static void
200 pdq_pci_shutdown(void *arg, int howto)
201 {
202     pdq_softc_t *sc = arg;
203
204     lwkt_serialize_enter(sc->sc_if.if_serializer);
205     pdq_hwreset(sc->sc_pdq);
206     lwkt_serialize_exit(sc->sc_if.if_serializer);
207 }
208
209 static u_long pdq_pci_count;
210
211 static struct pci_device fpadevice = {
212     "fpa",
213     pdq_pci_probe,
214     pdq_pci_attach,
215     &pdq_pci_count,
216     NULL
217 };
218
219 COMPAT_PCI_DRIVER (if_fpa, fpadevice);
220
221 #elif defined(__bsdi__)
222
223 static int
224 pdq_pci_match(
225     pci_devaddr_t *pa)
226 {
227     int irq;
228     int id;
229
230     id = pci_inl(pa, PCI_VENDOR_ID);
231     if (PCI_VENDORID(id) != PCI_VENDOR_DEC || PCI_CHIPID(id) != PCI_PRODUCT_DEC_DEFPA)
232         return 0;
233
234     irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
235     if (irq == 0 || irq >= 16)
236         return 0;
237
238     return 1;
239 }
240
241 int
242 pdq_pci_probe(
243     struct device *parent,
244     struct cfdata *cf,
245     void *aux)
246 {
247     struct isa_attach_args *ia = (struct isa_attach_args *) aux;
248     pdq_uint32_t irq, data;
249     pci_devaddr_t *pa;
250
251     pa = pci_scan(pdq_pci_match);
252     if (pa == NULL)
253         return 0;
254
255     irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
256
257     if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
258         printf("fpa%d: error: desired IRQ of %d does not match device's actual IRQ of %d\n",
259                cf->cf_unit,
260                ffs(ia->ia_irq) - 1, ffs(irq) - 1);
261         return 0;
262     }
263     if (ia->ia_irq == IRQUNK) {
264         (void) isa_irqalloc(irq);
265         ia->ia_irq = irq;
266     }
267
268     /* PCI bus masters don't use host DMA channels */
269     ia->ia_drq = DRQNONE;
270
271     /* Get the memory base address; assume the BIOS set it up correctly */
272     ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
273     pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
274     ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
275     pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
276
277     /* Disable I/O space access */
278     pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
279     ia->ia_iobase = 0;
280     ia->ia_iosize = 0;
281
282     /* Make sure the latency timer is what the DEFPA likes */
283     data = pci_inl(pa, PCI_CFLT);
284     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
285         data &= ~0xFF00;
286         data |= DEFPA_LATENCY << 8;
287         pci_outl(pa, PCI_CFLT, data);
288     }
289     ia->ia_irq |= IRQSHARE;
290
291     return 1;
292 }
293
294 void
295 pdq_pci_attach(
296     struct device *parent,
297     struct device *self,
298     void *aux)
299 {
300     pdq_softc_t *sc = (pdq_softc_t *) self;
301     struct isa_attach_args *ia = (struct isa_attach_args *) aux;
302     struct ifnet *ifp = &sc->sc_if;
303     int i;
304
305     if_initname(&(sc->sc_if), "fpa", sc->sc_dev.dv_unit);
306     sc->sc_if.if_flags = 0;
307     sc->sc_membase = (pdq_bus_memaddr_t) mapphys((vm_offset_t)ia->ia_maddr, ia->ia_msize);
308
309     sc->sc_pdq = pdq_initialize(PDQ_BUS_PCI, sc->sc_membase,
310                                 sc->sc_if.if_dname, sc->sc_if.if_dunit,
311                                 (void *) sc, PDQ_DEFPA);
312     if (sc->sc_pdq == NULL) {
313         printf("%s: initialization failed\n", sc->sc_if.if_xname);
314         return;
315     }
316
317     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
318
319     pdq_ifattach(sc, pdq_pci_ifwatchdog);
320
321     isa_establish(&sc->sc_id, &sc->sc_dev);
322
323     sc->sc_ih.ih_fun = pdq_pci_ifintr;
324     sc->sc_ih.ih_arg = (void *)sc;
325     intr_establish(ia->ia_irq, &sc->sc_ih, DV_NET);
326
327     sc->sc_ats.func = (void (*)(void *)) pdq_hwreset;
328     sc->sc_ats.arg = (void *) sc->sc_pdq;
329     atshutdown(&sc->sc_ats, ATSH_ADD);
330 }
331
332 struct cfdriver fpacd = {
333     0, "fpa", pdq_pci_probe, pdq_pci_attach,
334 #if _BSDI_VERSION >= 199401
335     DV_IFNET,
336 #endif
337     sizeof(pdq_softc_t)
338 };
339
340 #elif defined(__NetBSD__)
341
342 static int
343 pdq_pci_match(
344     struct device *parent,
345     void *match,
346     void *aux)
347 {
348     struct pci_attach_args *pa = (struct pci_attach_args *) aux;
349
350     if (PCI_VENDORID(pa->pa_id) != PCI_VENDOR_DEC)
351         return 0;
352     if (PCI_CHIPID(pa->pa_id) == PCI_PRODUCT_DEC_DEFPA)
353         return 1;
354
355     return 0;
356 }
357
358 static void
359 pdq_pci_attach(
360     struct device * const parent,
361     struct device * const self,
362     void * const aux)
363 {
364     pdq_softc_t * const sc = (pdq_softc_t *) self;
365     struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
366     pdq_uint32_t data;
367     pci_intr_handle_t intrhandle;
368     const char *intrstr;
369 #ifdef PDQ_IOMAPPED
370     bus_io_addr_t iobase;
371     bus_io_size_t iosize;
372 #else
373     bus_mem_addr_t membase;
374     bus_mem_size_t memsize;
375 #endif
376
377     data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CFLT);
378     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
379         data &= ~0xFF00;
380         data |= DEFPA_LATENCY << 8;
381         pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_CFLT, data);
382     }
383
384     sc->sc_bc = pa->pa_bc;
385     bcopy(sc->sc_dev.dv_xname, sc->sc_if.if_xname, IFNAMSIZ);
386     sc->sc_if.if_flags = 0;
387     sc->sc_if.if_softc = sc;
388
389 #ifdef PDQ_IOMAPPED
390     if (pci_io_find(pa->pa_pc, pa->pa_tag, PCI_CBIO, &iobase, &iosize)
391             || bus_io_map(pa->pa_bc, iobase, iosize, &sc->sc_iobase))
392         return;
393 #else
394     if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_CBMA, &membase, &memsize, NULL)
395             || bus_mem_map(pa->pa_bc, membase, memsize, 0, &sc->sc_membase))
396         return;
397 #endif
398
399     sc->sc_pdq = pdq_initialize(sc->sc_bc, sc->sc_membase,
400                                 sc->sc_if.if_xname, 0,
401                                 (void *) sc, PDQ_DEFPA);
402     if (sc->sc_pdq == NULL) {
403         printf("%s: initialization failed\n", sc->sc_dev.dv_xname);
404         return;
405     }
406
407     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
408     pdq_ifattach(sc, pdq_pci_ifwatchdog);
409
410     if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
411                      pa->pa_intrline, &intrhandle)) {
412         printf("%s: couldn't map interrupt\n", self->dv_xname);
413         return;
414     }
415     intrstr = pci_intr_string(pa->pa_pc, intrhandle);
416     sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET, pdq_pci_ifintr, sc);
417     if (sc->sc_ih == NULL) {
418         printf("%s: couldn't establish interrupt", self->dv_xname);
419         if (intrstr != NULL)
420             printf(" at %s", intrstr);
421         printf("\n");
422         return;
423     }
424
425     sc->sc_ats = shutdownhook_establish((void (*)(void *)) pdq_hwreset, sc->sc_pdq);
426     if (sc->sc_ats == NULL)
427         printf("%s: warning: couldn't establish shutdown hook\n", self->dv_xname);
428     if (intrstr != NULL)
429         printf("%s: interrupting at %s\n", self->dv_xname, intrstr);
430 }
431
432 struct cfattach fpa_ca = {
433     sizeof(pdq_softc_t), pdq_pci_match, pdq_pci_attach
434 };
435
436 struct cfdriver fpa_cd = {
437     0, "fpa", DV_IFNET
438 };
439
440 #endif /* __NetBSD__ */