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