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