if_fea.c revision 41611
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_fea.c,v 1.10 1998/08/24 02:28:15 bde Exp $
25 */
26
27/*
28 * DEC PDQ FDDI Controller
29 *
30 *	This module support the DEFEA EISA FDDI Controller.
31 */
32
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/socket.h>
36#if defined(__bsdi__) || defined(__NetBSD__)
37#include <sys/device.h>
38#endif
39
40#include <net/if.h>
41#include <net/if_arp.h>
42
43#if defined(__FreeBSD__)
44#include <i386/eisa/eisaconf.h>
45#include <dev/pdq/pdqvar.h>
46#include <dev/pdq/pdqreg.h>
47#elif defined(__bsdi__)
48#include <i386/isa/isa.h>
49#include <i386/isa/icu.h>
50#include <i386/isa/dma.h>
51#include <i386/isa/isavar.h>
52#include <i386/eisa/eisa.h>
53#include <dev/pdq/pdqvar.h>
54#include <dev/pdq/pdqreg.h>
55#elif defined(__NetBSD__)
56#include <machine/cpu.h>
57#include <machine/bus.h>
58
59#include <dev/ic/pdqvar.h>
60#include <dev/ic/pdqreg.h>
61
62#include <dev/eisa/eisareg.h>
63#include <dev/eisa/eisavar.h>
64#include <dev/eisa/eisadevs.h>
65#endif
66
67/*
68 *
69 */
70
71#define	DEFEA_IRQS		0x0000FBA9U
72
73#if defined(__FreeBSD__)
74static pdq_softc_t *pdqs_eisa[16];
75#define	PDQ_EISA_UNIT_TO_SOFTC(unit)	(pdqs_eisa[unit])
76#define	DEFEA_INTRENABLE		0x8	/* level interrupt */
77#define	pdq_eisa_ifwatchdog		NULL
78#define	DEFEA_DECODE_IRQ(n)		((DEFEA_IRQS >> ((n) << 2)) & 0x0f)
79
80#elif defined(__bsdi__)
81extern struct cfdriver feacd;
82#define	PDQ_EISA_UNIT_TO_SOFTC(unit)	((pdq_softc_t *)feacd.cd_devs[unit])
83#define	DEFEA_INTRENABLE		0x28	/* edge interrupt */
84static const int pdq_eisa_irqs[4] = { IRQ9, IRQ10, IRQ11, IRQ15 };
85#define	DEFEA_DECODE_IRQ(n)		(pdq_eisa_irqs[(n)])
86
87#elif defined(__NetBSD__)
88#define	DEFEA_INTRENABLE		0x8	/* level interrupt */
89#define	pdq_eisa_ifwatchdog		NULL
90#define	DEFEA_DECODE_IRQ(n)		((DEFEA_IRQS >> ((n) << 2)) & 0x0f)
91
92#else
93#error unknown system
94#endif
95
96#ifndef pdq_eisa_ifwatchdog
97static ifnet_ret_t
98pdq_eisa_ifwatchdog(
99    int unit)
100{
101    pdq_ifwatchdog(&PDQ_EISA_UNIT_TO_SOFTC(unit)->sc_if);
102}
103#endif
104
105static void
106pdq_eisa_subprobe(
107    pdq_bus_t bc,
108    pdq_bus_ioport_t iobase,
109    pdq_uint32_t *maddr,
110    pdq_uint32_t *msize,
111    pdq_uint32_t *irq)
112{
113    if (irq != NULL)
114	*irq = DEFEA_DECODE_IRQ(PDQ_OS_IORD_8(bc, iobase, PDQ_EISA_IO_CONFIG_STAT_0) & 3);
115    *maddr = (PDQ_OS_IORD_8(bc, iobase, PDQ_EISA_MEM_ADD_CMP_0) << 8)
116	| (PDQ_OS_IORD_8(bc, iobase, PDQ_EISA_MEM_ADD_CMP_1) << 16);
117    *msize = (PDQ_OS_IORD_8(bc, iobase, PDQ_EISA_MEM_ADD_MASK_0) + 4) << 8;
118}
119
120static void
121pdq_eisa_devinit(
122    pdq_softc_t *sc)
123{
124    pdq_uint8_t data;
125
126    /*
127     * Do the standard initialization for the DEFEA registers.
128     */
129    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_FUNCTION_CTRL, 0x23);
130    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_IO_CMP_1_1, (sc->sc_iobase >> 8) & 0xF0);
131    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_IO_CMP_0_1, (sc->sc_iobase >> 8) & 0xF0);
132    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_SLOT_CTRL, 0x01);
133    data = PDQ_OS_IORD_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_BURST_HOLDOFF);
134#if defined(PDQ_IOMAPPED)
135    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_BURST_HOLDOFF, data & ~1);
136#else
137    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_BURST_HOLDOFF, data | 1);
138#endif
139    data = PDQ_OS_IORD_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_IO_CONFIG_STAT_0);
140    PDQ_OS_IOWR_8(sc->sc_bc, sc->sc_iobase, PDQ_EISA_IO_CONFIG_STAT_0, data | DEFEA_INTRENABLE);
141}
142
143#if defined(__FreeBSD__)
144static void pdq_eisa_shutdown(int howto, void *sc);
145static int pdq_eisa_probe(void);
146static int pdq_eisa_attach(struct eisa_device *ed);
147
148static unsigned long pdq_eisa_unit;
149
150static struct eisa_driver pdq_eisa_driver = {
151    "fea", pdq_eisa_probe, pdq_eisa_attach, NULL, &pdq_eisa_unit
152};
153
154DATA_SET(eisadriver_set, pdq_eisa_driver);
155
156
157static const char *
158pdq_eisa_match(
159    eisa_id_t type)
160{
161    if ((type >> 8) == 0x10a330)
162	return ("DEC DEFEA EISA FDDI Controller");
163    return NULL;
164}
165
166static int
167pdq_eisa_probe(
168    void)
169{
170    struct eisa_device *ed = NULL;
171    int count;
172
173    for (count = 0; (ed = eisa_match_dev(ed, pdq_eisa_match)) != NULL; count++) {
174	pdq_bus_ioport_t iobase = ed->ioconf.slot * EISA_SLOT_SIZE;
175	pdq_uint32_t irq, maddr, msize;
176
177	eisa_add_iospace(ed, iobase, 0x200, RESVADDR_NONE);
178	pdq_eisa_subprobe(PDQ_BUS_EISA, iobase, &maddr, &msize, &irq);
179	eisa_add_mspace(ed, maddr, msize, RESVADDR_NONE);
180	eisa_add_intr(ed, irq);
181	eisa_registerdev(ed, &pdq_eisa_driver);
182    }
183    return count;
184}
185
186static void
187pdq_eisa_interrupt(
188    void *arg)
189{
190    pdq_softc_t * const sc = (pdq_softc_t *) arg;
191    (void) pdq_interrupt(sc->sc_pdq);
192}
193
194static int
195pdq_eisa_attach(
196    struct eisa_device *ed)
197{
198    pdq_softc_t *sc;
199    resvaddr_t *iospace;
200    resvaddr_t *mspace;
201    int irq;
202
203    if (TAILQ_FIRST(&ed->ioconf.irqs) == NULL)
204	return (-1);
205
206    irq = TAILQ_FIRST(&ed->ioconf.irqs)->irq_no;
207
208    sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
209    pdqs_eisa[ed->unit] = sc;
210
211    bzero(sc, sizeof(pdq_softc_t));	/* Zero out the softc*/
212    sc->sc_if.if_name = "fea";
213    sc->sc_if.if_unit = ed->unit;
214
215    if ((iospace = ed->ioconf.ioaddrs.lh_first) == NULL) {
216	printf("fea%d: no iospace??\n", sc->sc_if.if_unit);
217	return -1;
218    }
219    if ((mspace = ed->ioconf.maddrs.lh_first) == NULL) {
220	printf("fea%d: no memory space??\n", sc->sc_if.if_unit);
221	return -1;
222    }
223
224    sc->sc_iobase = (pdq_bus_ioport_t) iospace->addr;
225    sc->sc_membase = (pdq_bus_memaddr_t) pmap_mapdev(mspace->addr, mspace->size);
226    if (sc->sc_membase == NULL) {
227	printf("fea%d: failed to map memory 0x%lx-0x%lx!\n",
228	       sc->sc_if.if_unit, mspace->addr, mspace->addr + mspace->size - 1);
229	return -1;
230    }
231
232    eisa_reg_start(ed);
233    if (eisa_reg_iospace(ed, iospace)) {
234	printf("fea%d: failed to register iospace 0x%lx-0x%lx!\n",
235	       sc->sc_if.if_unit, iospace->addr, iospace->addr + iospace->size - 1);
236	return -1;
237    }
238    if (eisa_reg_mspace(ed, mspace)) {
239	printf("fea%d: failed to register memory 0x%lx-0x%lx!\n",
240	       sc->sc_if.if_unit, mspace->addr, mspace->addr + mspace->size - 1);
241	return -1;
242    }
243
244    if (eisa_reg_intr(ed, irq, pdq_eisa_interrupt, sc, &net_imask, 1)) {
245	printf("fea%d: interrupt registration failed\n", sc->sc_if.if_unit);
246	return -1;
247    }
248
249    eisa_reg_end(ed);
250
251    pdq_eisa_devinit(sc);
252    sc->sc_pdq = pdq_initialize(PDQ_BUS_EISA, sc->sc_membase,
253				sc->sc_if.if_name, sc->sc_if.if_unit,
254				(void *) sc, PDQ_DEFEA);
255    if (sc->sc_pdq == NULL) {
256	printf("fea%d: initialization failed\n", sc->sc_if.if_unit);
257	return -1;
258    }
259
260    if (eisa_enable_intr(ed, irq)) {
261	printf("fea%d: failed to enable interrupt\n", sc->sc_if.if_unit);
262	return -1;
263    }
264
265    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
266    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
267    at_shutdown(pdq_eisa_shutdown, (void *) sc, SHUTDOWN_POST_SYNC);
268
269    return 0;
270}
271
272static void
273pdq_eisa_shutdown(
274    int howto,
275    void *sc)
276{
277    pdq_hwreset(((pdq_softc_t *)sc)->sc_pdq);
278}
279#endif /* __FreeBSD__ */
280
281#if defined(__bsdi__)
282static int
283pdq_eisa_probe(
284    struct device *parent,
285    struct cfdata *cf,
286    void *aux)
287{
288    struct isa_attach_args *ia = (struct isa_attach_args *) aux;
289    int slot;
290    pdq_uint32_t irq, maddr, msize;
291
292    if (isa_bustype != BUS_EISA)
293	return 0;
294
295    if ((slot = eisa_match(cf, ia)) == 0)
296	return 0;
297    ia->ia_iobase = slot << 12;
298    ia->ia_iosize = EISA_NPORT;
299    eisa_slotalloc(slot);
300
301    pdq_eisa_subprobe(PDQ_BUS_EISA, ia->ia_iobase, &maddr, &msize, &irq);
302    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
303	printf("fea%d: error: desired IRQ of %d does not match device's actual IRQ (%d),\n",
304	       cf->cf_unit,
305	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
306	return 0;
307    }
308    if (ia->ia_irq == IRQUNK) {
309	if ((ia->ia_irq = isa_irqalloc(irq)) == 0) {
310	    if ((ia->ia_irq = isa_irqalloc(IRQ9|IRQ10|IRQ11|IRQ15)) == 0) {
311		printf("fea%d: error: IRQ %d is already in use\n", cf->cf_unit,
312		       ffs(irq) - 1);
313		return 0;
314	    }
315	    irq = PDQ_OS_IORD_8(PDQ_BUS_EISA, ia->ia_iobase, PDQ_EISA_IO_CONFIG_STAT_0) & ~3;
316	    switch (ia->ia_irq) {
317		case IRQ9:  irq |= 0;
318		case IRQ10: irq |= 1;
319		case IRQ11: irq |= 2;
320		case IRQ15: irq |= 3;
321	    }
322	    PDQ_OS_IOWR_8(PDQ_BUS_EISA, ia->ia_iobase, PDQ_EISA_IO_CONFIG_STAT_0, irq);
323	}
324    }
325    if (maddr == 0) {
326	printf("fea%d: error: memory not enabled! ECU reconfiguration required\n",
327	       cf->cf_unit);
328	return 0;
329    }
330
331    /* EISA bus masters don't use host DMA channels */
332    ia->ia_drq = DRQNONE;
333
334    ia->ia_maddr = (caddr_t) maddr;
335    ia->ia_msize = msize;
336    return 1;
337}
338
339static void
340pdq_eisa_attach(
341    struct device *parent,
342    struct device *self,
343    void *aux)
344{
345    pdq_softc_t *sc = (pdq_softc_t *) self;
346    register struct isa_attach_args *ia = (struct isa_attach_args *) aux;
347    register struct ifnet *ifp = &sc->sc_if;
348
349    sc->sc_if.if_unit = sc->sc_dev.dv_unit;
350    sc->sc_if.if_name = "fea";
351    sc->sc_if.if_flags = 0;
352
353    sc->sc_iobase = ia->ia_iobase;
354
355    pdq_eisa_devinit(sc);
356    sc->sc_pdq = pdq_initialize(PDQ_BUS_EISA,
357				(pdq_bus_memaddr_t) ISA_HOLE_VADDR(ia->ia_maddr),
358				sc->sc_if.if_name, sc->sc_if.if_unit,
359				(void *) sc, PDQ_DEFEA);
360    if (sc->sc_pdq == NULL) {
361	printf("fea%d: initialization failed\n", sc->sc_if.if_unit);
362	return;
363    }
364
365    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
366
367    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
368
369    isa_establish(&sc->sc_id, &sc->sc_dev);
370
371    sc->sc_ih.ih_fun = pdq_interrupt;
372    sc->sc_ih.ih_arg = (void *) sc->sc_pdq;
373    intr_establish(ia->ia_irq, &sc->sc_ih, DV_NET);
374
375    sc->sc_ats.func = (void (*)(void *)) pdq_hwreset;
376    sc->sc_ats.arg = (void *) sc->sc_pdq;
377    atshutdown(&sc->sc_ats, ATSH_ADD);
378}
379
380static char *pdq_eisa_ids[] = {
381    "DEC3001",	/* 0x0130A310 */
382    "DEC3002",	/* 0x0230A310 */
383    "DEC3003",	/* 0x0330A310 */
384    "DEC3004",	/* 0x0430A310 */
385};
386
387struct cfdriver feacd = {
388    0, "fea", pdq_eisa_probe, pdq_eisa_attach, DV_IFNET, sizeof(pdq_softc_t),
389    pdq_eisa_ids
390};
391#endif /* __bsdi__ */
392
393#if defined(__NetBSD__)
394static int
395pdq_eisa_match(
396    struct device *parent,
397    void *match,
398    void *aux)
399{
400    const struct eisa_attach_args * const ea = (struct eisa_attach_args *) aux;
401
402    if (strncmp(ea->ea_idstring, "DEC300", 6) == 0)
403	return 1;
404
405    return 0;
406}
407
408static void
409pdq_eisa_attach(
410    struct device *parent,
411    struct device *self,
412    void *aux)
413{
414    pdq_softc_t * const sc = (pdq_softc_t *) self;
415    struct eisa_attach_args * const ea = (struct eisa_attach_args *) aux;
416    pdq_uint32_t irq, maddr, msize;
417    eisa_intr_handle_t ih;
418    const char *intrstr;
419
420    sc->sc_bc = ea->ea_bc;
421    bcopy(sc->sc_dev.dv_xname, sc->sc_if.if_xname, IFNAMSIZ);
422    sc->sc_if.if_flags = 0;
423    sc->sc_if.if_softc = sc;
424
425    if (bus_io_map(sc->sc_bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &sc->sc_iobase)) {
426	printf("\n%s: failed to map I/O!\n", sc->sc_dev.dv_xname);
427	return;
428    }
429
430    pdq_eisa_subprobe(sc->sc_bc, sc->sc_iobase, &maddr, &msize, &irq);
431
432#if !defined(PDQ_IOMAPPED)
433    if (maddr == 0 || msize == 0) {
434	printf("\n%s: error: memory not enabled! ECU reconfiguration required\n",
435	       sc->sc_dev.dv_xname);
436	return;
437    }
438
439    if (bus_mem_map(sc->sc_bc, maddr, msize, 0, &sc->sc_membase)) {
440	bus_io_unmap(sc->sc_bc, sc->sc_iobase, EISA_SLOT_SIZE);
441	printf("\n%s: failed to map memory (0x%x-0x%x)!\n",
442	       sc->sc_dev.dv_xname, maddr, maddr + msize - 1);
443	return;
444    }
445#endif
446    pdq_eisa_devinit(sc);
447    sc->sc_pdq = pdq_initialize(sc->sc_bc, sc->sc_membase,
448				sc->sc_if.if_xname, 0,
449				(void *) sc, PDQ_DEFEA);
450    if (sc->sc_pdq == NULL) {
451	printf("%s: initialization failed\n", sc->sc_dev.dv_xname);
452	return;
453    }
454
455    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
456
457    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
458
459    if (eisa_intr_map(ea->ea_ec, irq, &ih)) {
460	printf("%s: couldn't map interrupt (%d)\n", sc->sc_dev.dv_xname, irq);
461	return;
462    }
463    intrstr = eisa_intr_string(ea->ea_ec, ih);
464    sc->sc_ih = eisa_intr_establish(ea->ea_ec, ih, IST_LEVEL, IPL_NET,
465				    (int (*)(void *)) pdq_interrupt, sc->sc_pdq);
466    if (sc->sc_ih == NULL) {
467	printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
468	if (intrstr != NULL)
469	    printf(" at %s", intrstr);
470	printf("\n");
471	return;
472    }
473    sc->sc_ats = shutdownhook_establish((void (*)(void *)) pdq_hwreset, sc->sc_pdq);
474    if (sc->sc_ats == NULL)
475	printf("%s: warning: couldn't establish shutdown hook\n", self->dv_xname);
476#if !defined(PDQ_IOMAPPED)
477    printf("%s: using iomem 0x%x-0x%x\n", sc->sc_dev.dv_xname, maddr, maddr + msize - 1);
478#endif
479    if (intrstr != NULL)
480	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
481}
482
483struct cfattach fea_ca = {
484    sizeof(pdq_softc_t), pdq_eisa_match, pdq_eisa_attach
485};
486
487struct cfdriver fea_cd = {
488    0, "fea", DV_IFNET
489};
490#endif
491