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