xref: /openbsd/sys/dev/isa/if_lc_isa.c (revision db3296cf)
1 /*	$OpenBSD: if_lc_isa.c,v 1.3 2003/04/27 11:22:53 ho 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/select.h>
46 #include <sys/device.h>
47 #include <sys/queue.h>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/if_ether.h>
59 #endif
60 
61 #ifdef NS
62 #include <netns/ns.h>
63 #include <netns/ns_if.h>
64 #endif
65 
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #include <net/bpfdesc.h>
69 #endif
70 
71 #include <machine/cpu.h>
72 #include <machine/bus.h>
73 #include <machine/intr.h>
74 
75 #include <dev/ic/lemacreg.h>
76 #include <dev/ic/lemacvar.h>
77 
78 #include <dev/isa/isavar.h>
79 
80 extern struct cfdriver lc_cd;
81 
82 int	lemac_isa_find(struct lemac_softc *, struct isa_attach_args *,
83     int);
84 int	lemac_isa_probe(struct device *, void *, void *);
85 void	lemac_isa_attach(struct device *, struct device *, void *);
86 
87 struct cfattach lc_isa_ca = {
88 	sizeof(struct lemac_softc), lemac_isa_probe, lemac_isa_attach
89 };
90 
91 int
92 lemac_isa_find(sc, ia, attach)
93 	struct lemac_softc *sc;
94 	struct isa_attach_args *ia;
95 	int attach;
96 {
97 	bus_addr_t maddr;
98 	bus_addr_t msize;
99 	int rv = 0, irq;
100 
101 	/*
102 	 * Disallow wildcarded i/o addresses.
103 	 */
104 	if (ia->ia_iobase == IOBASEUNK)
105 		return 0;
106 
107 	/*
108 	 * Make sure this is a valid LEMAC address.
109 	 */
110 	if (ia->ia_iobase & (LEMAC_IOSIZE - 1))
111 		return 0;
112 
113 	sc->sc_iot = ia->ia_iot;
114 
115 	/*
116 	 * Map the LEMAC's port space for the probe sequence.
117 	 */
118 	ia->ia_iosize = LEMAC_IOSIZE;
119 
120 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0,
121 	    &sc->sc_ioh)) {
122 		if (attach)
123 			printf(": can't map i/o space\n");
124 		return (0);
125 	}
126 
127 	/*
128 	 * Read the Ethernet address from the EEPROM.
129 	 * It must start with one of the DEC OUIs and pass the
130 	 * DEC ethernet checksum test.
131 	 */
132 	if (lemac_port_check(sc->sc_iot, sc->sc_ioh) == 0)
133 		goto outio;
134 
135 	/*
136 	 * Get information about memory space and attempt to map it.
137 	 */
138 	lemac_info_get(sc->sc_iot, sc->sc_ioh, &maddr, &msize, &irq);
139 
140 	if (ia->ia_maddr != maddr && ia->ia_maddr != MADDRUNK)
141 		goto outio;
142 
143 	if (maddr != 0 && msize != 0) {
144 		sc->sc_memt = ia->ia_memt;
145 		if (bus_space_map(ia->ia_memt, maddr, msize, 0,
146 		    &sc->sc_memh)) {
147 			if (attach)
148 				printf(": can't map mem space\n");
149 			goto outio;
150 		}
151 	}
152 
153 	/*
154 	 * Double-check IRQ configuration.
155 	 */
156 	if (ia->ia_irq != irq && ia->ia_irq != IRQUNK)
157 		printf("%s: overriding IRQ %d to %d\n", sc->sc_dv.dv_xname,
158 		    ia->ia_irq, irq);
159 
160 	if (attach) {
161 		sc->sc_ats = shutdownhook_establish(lemac_shutdown, sc);
162 		if (sc->sc_ats == NULL)
163 			printf(
164 			    "\n%s: warning: can't establish shutdown hook\n",
165 			    sc->sc_dv.dv_xname);
166 
167 		lemac_ifattach(sc);
168 
169 		sc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE,
170 		    IPL_NET, lemac_intr, sc, sc->sc_dv.dv_xname);
171 	}
172 
173 	/*
174 	 * I guess we've found one.
175 	 */
176 	rv = 1;
177 
178 	ia->ia_maddr = maddr;
179 	ia->ia_msize = msize;
180 	ia->ia_irq = irq;
181 
182 	if (maddr != 0 && msize != 0 && (rv == 0 || !attach))
183 		bus_space_unmap(sc->sc_memt, sc->sc_memh, msize);
184 outio:
185 	if (rv == 0 || !attach)
186 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, LEMAC_IOSIZE);
187 	return (rv);
188 }
189 
190 int
191 lemac_isa_probe(parent, match, aux)
192 	struct device *parent;
193 	void *match;
194 	void *aux;
195 {
196 	struct isa_attach_args *ia = aux;
197 	struct cfdata *cf = match;
198 	struct lemac_softc sc;
199 
200 	snprintf(sc.sc_dv.dv_xname, sizeof sc.sc_dv.dv_xname, "%s%d",
201 	    lc_cd.cd_name, cf->cf_unit);
202 
203 	return (lemac_isa_find(&sc, ia, 0));
204 }
205 
206 void
207 lemac_isa_attach(parent, self, aux)
208 	struct device *parent;
209 	struct device *self;
210 	void *aux;
211 {
212 	struct lemac_softc *sc = (void *)self;
213 	struct isa_attach_args *ia = aux;
214 
215 	lemac_isa_find(sc, ia, 1);
216 }
217