if_fea.c revision 38505
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.9 1998/02/20 13:11:46 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 = (pdq_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
209    if (sc == NULL) {
210	printf("fea%d: malloc failed!\n", sc->sc_if.if_unit);
211	return -1;
212    }
213    pdqs_eisa[ed->unit] = sc;
214
215    bzero(sc, sizeof(pdq_softc_t));	/* Zero out the softc*/
216    sc->sc_if.if_name = "fea";
217    sc->sc_if.if_unit = ed->unit;
218
219    if ((iospace = ed->ioconf.ioaddrs.lh_first) == NULL) {
220	printf("fea%d: no iospace??\n", sc->sc_if.if_unit);
221	return -1;
222    }
223    if ((mspace = ed->ioconf.maddrs.lh_first) == NULL) {
224	printf("fea%d: no memory space??\n", sc->sc_if.if_unit);
225	return -1;
226    }
227
228    sc->sc_iobase = (pdq_bus_ioport_t) iospace->addr;
229    sc->sc_membase = (pdq_bus_memaddr_t) pmap_mapdev(mspace->addr, mspace->size);
230    if (sc->sc_membase == NULL) {
231	printf("fea%d: failed to map memory 0x%lx-0x%lx!\n",
232	       sc->sc_if.if_unit, mspace->addr, mspace->addr + mspace->size - 1);
233	return -1;
234    }
235
236    eisa_reg_start(ed);
237    if (eisa_reg_iospace(ed, iospace)) {
238	printf("fea%d: failed to register iospace 0x%lx-0x%lx!\n",
239	       sc->sc_if.if_unit, iospace->addr, iospace->addr + iospace->size - 1);
240	return -1;
241    }
242    if (eisa_reg_mspace(ed, mspace)) {
243	printf("fea%d: failed to register memory 0x%lx-0x%lx!\n",
244	       sc->sc_if.if_unit, mspace->addr, mspace->addr + mspace->size - 1);
245	return -1;
246    }
247
248    if (eisa_reg_intr(ed, irq, pdq_eisa_interrupt, sc, &net_imask, 1)) {
249	printf("fea%d: interrupt registration failed\n", sc->sc_if.if_unit);
250	return -1;
251    }
252
253    eisa_reg_end(ed);
254
255    pdq_eisa_devinit(sc);
256    sc->sc_pdq = pdq_initialize(PDQ_BUS_EISA, sc->sc_membase,
257				sc->sc_if.if_name, sc->sc_if.if_unit,
258				(void *) sc, PDQ_DEFEA);
259    if (sc->sc_pdq == NULL) {
260	printf("fea%d: initialization failed\n", sc->sc_if.if_unit);
261	return -1;
262    }
263
264    if (eisa_enable_intr(ed, irq)) {
265	printf("fea%d: failed to enable interrupt\n", sc->sc_if.if_unit);
266	return -1;
267    }
268
269    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
270    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
271    at_shutdown(pdq_eisa_shutdown, (void *) sc, SHUTDOWN_POST_SYNC);
272
273    return 0;
274}
275
276static void
277pdq_eisa_shutdown(
278    int howto,
279    void *sc)
280{
281    pdq_hwreset(((pdq_softc_t *)sc)->sc_pdq);
282}
283#endif /* __FreeBSD__ */
284
285#if defined(__bsdi__)
286static int
287pdq_eisa_probe(
288    struct device *parent,
289    struct cfdata *cf,
290    void *aux)
291{
292    struct isa_attach_args *ia = (struct isa_attach_args *) aux;
293    int slot;
294    pdq_uint32_t irq, maddr, msize;
295
296    if (isa_bustype != BUS_EISA)
297	return 0;
298
299    if ((slot = eisa_match(cf, ia)) == 0)
300	return 0;
301    ia->ia_iobase = slot << 12;
302    ia->ia_iosize = EISA_NPORT;
303    eisa_slotalloc(slot);
304
305    pdq_eisa_subprobe(PDQ_BUS_EISA, ia->ia_iobase, &maddr, &msize, &irq);
306    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
307	printf("fea%d: error: desired IRQ of %d does not match device's actual IRQ (%d),\n",
308	       cf->cf_unit,
309	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
310	return 0;
311    }
312    if (ia->ia_irq == IRQUNK) {
313	if ((ia->ia_irq = isa_irqalloc(irq)) == 0) {
314	    if ((ia->ia_irq = isa_irqalloc(IRQ9|IRQ10|IRQ11|IRQ15)) == 0) {
315		printf("fea%d: error: IRQ %d is already in use\n", cf->cf_unit,
316		       ffs(irq) - 1);
317		return 0;
318	    }
319	    irq = PDQ_OS_IORD_8(PDQ_BUS_EISA, ia->ia_iobase, PDQ_EISA_IO_CONFIG_STAT_0) & ~3;
320	    switch (ia->ia_irq) {
321		case IRQ9:  irq |= 0;
322		case IRQ10: irq |= 1;
323		case IRQ11: irq |= 2;
324		case IRQ15: irq |= 3;
325	    }
326	    PDQ_OS_IOWR_8(PDQ_BUS_EISA, ia->ia_iobase, PDQ_EISA_IO_CONFIG_STAT_0, irq);
327	}
328    }
329    if (maddr == 0) {
330	printf("fea%d: error: memory not enabled! ECU reconfiguration required\n",
331	       cf->cf_unit);
332	return 0;
333    }
334
335    /* EISA bus masters don't use host DMA channels */
336    ia->ia_drq = DRQNONE;
337
338    ia->ia_maddr = (caddr_t) maddr;
339    ia->ia_msize = msize;
340    return 1;
341}
342
343static void
344pdq_eisa_attach(
345    struct device *parent,
346    struct device *self,
347    void *aux)
348{
349    pdq_softc_t *sc = (pdq_softc_t *) self;
350    register struct isa_attach_args *ia = (struct isa_attach_args *) aux;
351    register struct ifnet *ifp = &sc->sc_if;
352
353    sc->sc_if.if_unit = sc->sc_dev.dv_unit;
354    sc->sc_if.if_name = "fea";
355    sc->sc_if.if_flags = 0;
356
357    sc->sc_iobase = ia->ia_iobase;
358
359    pdq_eisa_devinit(sc);
360    sc->sc_pdq = pdq_initialize(PDQ_BUS_EISA,
361				(pdq_bus_memaddr_t) ISA_HOLE_VADDR(ia->ia_maddr),
362				sc->sc_if.if_name, sc->sc_if.if_unit,
363				(void *) sc, PDQ_DEFEA);
364    if (sc->sc_pdq == NULL) {
365	printf("fea%d: initialization failed\n", sc->sc_if.if_unit);
366	return;
367    }
368
369    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
370
371    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
372
373    isa_establish(&sc->sc_id, &sc->sc_dev);
374
375    sc->sc_ih.ih_fun = pdq_interrupt;
376    sc->sc_ih.ih_arg = (void *) sc->sc_pdq;
377    intr_establish(ia->ia_irq, &sc->sc_ih, DV_NET);
378
379    sc->sc_ats.func = (void (*)(void *)) pdq_hwreset;
380    sc->sc_ats.arg = (void *) sc->sc_pdq;
381    atshutdown(&sc->sc_ats, ATSH_ADD);
382}
383
384static char *pdq_eisa_ids[] = {
385    "DEC3001",	/* 0x0130A310 */
386    "DEC3002",	/* 0x0230A310 */
387    "DEC3003",	/* 0x0330A310 */
388    "DEC3004",	/* 0x0430A310 */
389};
390
391struct cfdriver feacd = {
392    0, "fea", pdq_eisa_probe, pdq_eisa_attach, DV_IFNET, sizeof(pdq_softc_t),
393    pdq_eisa_ids
394};
395#endif /* __bsdi__ */
396
397#if defined(__NetBSD__)
398static int
399pdq_eisa_match(
400    struct device *parent,
401    void *match,
402    void *aux)
403{
404    const struct eisa_attach_args * const ea = (struct eisa_attach_args *) aux;
405
406    if (strncmp(ea->ea_idstring, "DEC300", 6) == 0)
407	return 1;
408
409    return 0;
410}
411
412static void
413pdq_eisa_attach(
414    struct device *parent,
415    struct device *self,
416    void *aux)
417{
418    pdq_softc_t * const sc = (pdq_softc_t *) self;
419    struct eisa_attach_args * const ea = (struct eisa_attach_args *) aux;
420    pdq_uint32_t irq, maddr, msize;
421    eisa_intr_handle_t ih;
422    const char *intrstr;
423
424    sc->sc_bc = ea->ea_bc;
425    bcopy(sc->sc_dev.dv_xname, sc->sc_if.if_xname, IFNAMSIZ);
426    sc->sc_if.if_flags = 0;
427    sc->sc_if.if_softc = sc;
428
429    if (bus_io_map(sc->sc_bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &sc->sc_iobase)) {
430	printf("\n%s: failed to map I/O!\n", sc->sc_dev.dv_xname);
431	return;
432    }
433
434    pdq_eisa_subprobe(sc->sc_bc, sc->sc_iobase, &maddr, &msize, &irq);
435
436#if !defined(PDQ_IOMAPPED)
437    if (maddr == 0 || msize == 0) {
438	printf("\n%s: error: memory not enabled! ECU reconfiguration required\n",
439	       sc->sc_dev.dv_xname);
440	return;
441    }
442
443    if (bus_mem_map(sc->sc_bc, maddr, msize, 0, &sc->sc_membase)) {
444	bus_io_unmap(sc->sc_bc, sc->sc_iobase, EISA_SLOT_SIZE);
445	printf("\n%s: failed to map memory (0x%x-0x%x)!\n",
446	       sc->sc_dev.dv_xname, maddr, maddr + msize - 1);
447	return;
448    }
449#endif
450    pdq_eisa_devinit(sc);
451    sc->sc_pdq = pdq_initialize(sc->sc_bc, sc->sc_membase,
452				sc->sc_if.if_xname, 0,
453				(void *) sc, PDQ_DEFEA);
454    if (sc->sc_pdq == NULL) {
455	printf("%s: initialization failed\n", sc->sc_dev.dv_xname);
456	return;
457    }
458
459    bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
460
461    pdq_ifattach(sc, pdq_eisa_ifwatchdog);
462
463    if (eisa_intr_map(ea->ea_ec, irq, &ih)) {
464	printf("%s: couldn't map interrupt (%d)\n", sc->sc_dev.dv_xname, irq);
465	return;
466    }
467    intrstr = eisa_intr_string(ea->ea_ec, ih);
468    sc->sc_ih = eisa_intr_establish(ea->ea_ec, ih, IST_LEVEL, IPL_NET,
469				    (int (*)(void *)) pdq_interrupt, sc->sc_pdq);
470    if (sc->sc_ih == NULL) {
471	printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
472	if (intrstr != NULL)
473	    printf(" at %s", intrstr);
474	printf("\n");
475	return;
476    }
477    sc->sc_ats = shutdownhook_establish((void (*)(void *)) pdq_hwreset, sc->sc_pdq);
478    if (sc->sc_ats == NULL)
479	printf("%s: warning: couldn't establish shutdown hook\n", self->dv_xname);
480#if !defined(PDQ_IOMAPPED)
481    printf("%s: using iomem 0x%x-0x%x\n", sc->sc_dev.dv_xname, maddr, maddr + msize - 1);
482#endif
483    if (intrstr != NULL)
484	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
485}
486
487struct cfattach fea_ca = {
488    sizeof(pdq_softc_t), pdq_eisa_match, pdq_eisa_attach
489};
490
491struct cfdriver fea_cd = {
492    0, "fea", DV_IFNET
493};
494#endif
495