xref: /openbsd-src/sys/dev/isa/if_lc_isa.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /*	$OpenBSD: if_lc_isa.c,v 1.13 2021/03/07 06:17:03 jsg Exp $ */
2 /*	$NetBSD: if_lc_isa.c,v 1.10 2001/06/13 10:46:03 wiz Exp $ */
3 
4 /*-
5  * Copyright (c) 1994, 1995, 1997 Matt Thomas <matt@3am-software.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * DEC EtherWORKS 3 Ethernet Controllers
30  *
31  * Written by Matt Thomas
32  *
33  *   This driver supports the LEMAC (DE203, DE204, and DE205) cards.
34  */
35 
36 #include "bpfilter.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/errno.h>
44 #include <sys/syslog.h>
45 #include <sys/selinfo.h>
46 #include <sys/device.h>
47 #include <sys/queue.h>
48 
49 #include <net/if.h>
50 #include <net/if_media.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 
55 #if NBPFILTER > 0
56 #include <net/bpf.h>
57 #endif
58 
59 #include <machine/cpu.h>
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62 
63 #include <dev/ic/lemacreg.h>
64 #include <dev/ic/lemacvar.h>
65 
66 #include <dev/isa/isavar.h>
67 
68 extern struct cfdriver lc_cd;
69 
70 int	lemac_isa_find(struct lemac_softc *, struct isa_attach_args *,
71     int);
72 int	lemac_isa_probe(struct device *, void *, void *);
73 void	lemac_isa_attach(struct device *, struct device *, void *);
74 
75 struct cfattach lc_isa_ca = {
76 	sizeof(struct lemac_softc), lemac_isa_probe, lemac_isa_attach
77 };
78 
79 int
80 lemac_isa_find(struct lemac_softc *sc, struct isa_attach_args *ia, int attach)
81 {
82 	bus_addr_t maddr;
83 	bus_size_t msize;
84 	int rv = 0, irq;
85 
86 	/*
87 	 * Disallow wildcarded i/o addresses.
88 	 */
89 	if (ia->ia_iobase == IOBASEUNK)
90 		return 0;
91 
92 	/*
93 	 * Make sure this is a valid LEMAC address.
94 	 */
95 	if (ia->ia_iobase & (LEMAC_IOSIZE - 1))
96 		return 0;
97 
98 	sc->sc_iot = ia->ia_iot;
99 
100 	/*
101 	 * Map the LEMAC's port space for the probe sequence.
102 	 */
103 	ia->ia_iosize = LEMAC_IOSIZE;
104 
105 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0,
106 	    &sc->sc_ioh)) {
107 		if (attach)
108 			printf(": can't map i/o space\n");
109 		return (0);
110 	}
111 
112 	/*
113 	 * Read the Ethernet address from the EEPROM.
114 	 * It must start with one of the DEC OUIs and pass the
115 	 * DEC ethernet checksum test.
116 	 */
117 	if (lemac_port_check(sc->sc_iot, sc->sc_ioh) == 0)
118 		goto outio;
119 
120 	/*
121 	 * Get information about memory space and attempt to map it.
122 	 */
123 	lemac_info_get(sc->sc_iot, sc->sc_ioh, &maddr, &msize, &irq);
124 
125 	if (ia->ia_maddr != maddr && ia->ia_maddr != MADDRUNK)
126 		goto outio;
127 
128 	if (maddr != 0 && msize != 0) {
129 		sc->sc_memt = ia->ia_memt;
130 		if (bus_space_map(ia->ia_memt, maddr, msize, 0,
131 		    &sc->sc_memh)) {
132 			if (attach)
133 				printf(": can't map mem space\n");
134 			goto outio;
135 		}
136 	}
137 
138 	/*
139 	 * Double-check IRQ configuration.
140 	 */
141 	if (ia->ia_irq != irq && ia->ia_irq != IRQUNK)
142 		printf("%s: overriding IRQ %d to %d\n", sc->sc_dv.dv_xname,
143 		    ia->ia_irq, irq);
144 
145 	if (attach) {
146 		lemac_ifattach(sc);
147 
148 		sc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE,
149 		    IPL_NET, lemac_intr, sc, sc->sc_dv.dv_xname);
150 	}
151 
152 	/*
153 	 * I guess we've found one.
154 	 */
155 	rv = 1;
156 
157 	ia->ia_maddr = maddr;
158 	ia->ia_msize = msize;
159 	ia->ia_irq = irq;
160 
161 	if (maddr != 0 && msize != 0 && (rv == 0 || !attach))
162 		bus_space_unmap(sc->sc_memt, sc->sc_memh, msize);
163 outio:
164 	if (rv == 0 || !attach)
165 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LEMAC_IOSIZE);
166 	return (rv);
167 }
168 
169 int
170 lemac_isa_probe(struct device *parent, void *match, void *aux)
171 {
172 	struct isa_attach_args *ia = aux;
173 	struct cfdata *cf = ((struct device *)match)->dv_cfdata;
174 	struct lemac_softc sc;
175 
176 	snprintf(sc.sc_dv.dv_xname, sizeof sc.sc_dv.dv_xname, "%s%d",
177 	    lc_cd.cd_name, cf->cf_unit);
178 
179 	return (lemac_isa_find(&sc, ia, 0));
180 }
181 
182 void
183 lemac_isa_attach(struct device *parent, struct device *self, void *aux)
184 {
185 	struct lemac_softc *sc = (void *)self;
186 	struct isa_attach_args *ia = aux;
187 
188 	lemac_isa_find(sc, ia, 1);
189 }
190