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