xref: /openbsd-src/sys/dev/isa/if_ne_isa.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: if_ne_isa.c,v 1.5 2001/03/12 05:36:59 aaron Exp $	*/
2 /*	$NetBSD: if_ne_isa.c,v 1.6 1998/07/05 06:49:13 jonathan Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include "bpfilter.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52 
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #ifdef __NetBSD__
56 #include <net/if_ether.h>
57 #endif
58 #include <net/if_media.h>
59 
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #ifdef __NetBSD__
66 #include <netinet/if_inarp.h>
67 #else
68 #include <netinet/if_ether.h>
69 #endif
70 #endif
71 
72 #ifdef NS
73 #include <netns/ns.h>
74 #include <netns/ns_if.h>
75 #endif
76 
77 #if NBPFILTER > 0
78 #include <net/bpf.h>
79 #include <net/bpfdesc.h>
80 #endif
81 
82 #include <machine/intr.h>
83 #include <machine/bus.h>
84 
85 #include <dev/ic/dp8390reg.h>
86 #include <dev/ic/dp8390var.h>
87 
88 #include <dev/ic/ne2000reg.h>
89 #include <dev/ic/ne2000var.h>
90 
91 #include <dev/ic/rtl80x9reg.h>
92 #include <dev/ic/rtl80x9var.h>
93 
94 #include <dev/isa/isavar.h>
95 
96 int	ne_isa_match __P((struct device *, void *, void *));
97 void	ne_isa_attach __P((struct device *, struct device *, void *));
98 
99 struct ne_isa_softc {
100 	struct	ne2000_softc sc_ne2000;		/* real "ne2000" softc */
101 
102 	/* ISA-specific goo. */
103 	void	*sc_ih;				/* interrupt cookie */
104 };
105 
106 struct cfattach ne_isa_ca = {
107 	sizeof(struct ne_isa_softc), ne_isa_match, ne_isa_attach
108 };
109 
110 int
111 ne_isa_match(parent, match, aux)
112 	struct device *parent;
113 	void *match, *aux;
114 {
115 	struct ne_isa_softc *isc = match;
116 	struct ne2000_softc *nsc = &isc->sc_ne2000;
117 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
118 	struct isa_attach_args *ia = aux;
119 	bus_space_tag_t nict = ia->ia_iot;
120 	bus_space_handle_t nich;
121 	bus_space_tag_t asict;
122 	bus_space_handle_t asich;
123 
124 	/* Disallow wildcarded values. */
125 	if (ia->ia_irq ==  -1 /* ISACF_IRQ_DEFAULT */)
126 		return (0);
127 	if (ia->ia_iobase == -1 /* ISACF_PORT_DEFAULT */)
128 		return (0);
129 
130 	/* Make sure this is a valid NE[12]000 i/o address. */
131 	if ((ia->ia_iobase & 0x1f) != 0)
132 		return (0);
133 
134 	/* Map i/o space. */
135 	if (bus_space_map(nict, ia->ia_iobase, NE2000_NPORTS, 0, &nich))
136 		return (0);
137 
138 	asict = nict;
139 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
140 	    NE2000_ASIC_NPORTS, &asich))
141 		goto out;
142 
143 	dsc->sc_regt = nict;
144 	dsc->sc_regh = nich;
145 
146 	nsc->sc_asict = asict;
147 	nsc->sc_asich = asich;
148 
149 	/* Look for an NE2000-compatible card. */
150 	nsc->sc_type = ne2000_detect(nsc);
151 
152 	if (nsc->sc_type)
153 		ia->ia_iosize = NE2000_NPORTS;
154 
155  out:
156 	bus_space_unmap(nict, nich, NE2000_NPORTS);
157 	return (nsc->sc_type);
158 }
159 
160 void
161 ne_isa_attach(parent, self, aux)
162 	struct device *parent, *self;
163 	void *aux;
164 {
165 	struct ne_isa_softc *isc = (struct ne_isa_softc *)self;
166 	struct ne2000_softc *nsc = &isc->sc_ne2000;
167 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
168 	struct isa_attach_args *ia = aux;
169 	bus_space_tag_t nict = ia->ia_iot;
170 	bus_space_handle_t nich;
171 	bus_space_tag_t asict = nict;
172 	bus_space_handle_t asich;
173 	const char *typestr;
174 
175 	printf("\n");
176 
177 	/* Map i/o space. */
178 	if (bus_space_map(nict, ia->ia_iobase, NE2000_NPORTS, 0, &nich)) {
179 		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
180 		return;
181 	}
182 
183 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
184 	    NE2000_ASIC_NPORTS, &asich)) {
185 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
186 		return;
187 	}
188 
189 	dsc->sc_regt = nict;
190 	dsc->sc_regh = nich;
191 
192 	nsc->sc_asict = asict;
193 	nsc->sc_asich = asich;
194 
195 	switch (nsc->sc_type) {
196 	case NE2000_TYPE_NE1000:
197 		typestr = "NE1000";
198 		break;
199 
200 	case NE2000_TYPE_NE2000:
201 		typestr = "NE2000";
202 		/*
203 		 * Check for a RealTek 8019.
204 		 */
205 		bus_space_write_1(nict, nich, ED_P0_CR,
206 		    ED_CR_PAGE_0 | ED_CR_STP);
207 		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
208 								RTL0_8019ID0 &&
209 		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
210 								RTL0_8019ID1) {
211 			typestr = "NE2000 (RTL8019)";
212 			dsc->sc_mediachange = rtl80x9_mediachange;
213 			dsc->sc_mediastatus = rtl80x9_mediastatus;
214 			dsc->init_card = rtl80x9_init_card;
215 			dsc->sc_media_init = rtl80x9_media_init;
216 		}
217 		break;
218 
219 	default:
220 		printf("%s: where did the card go?!\n", dsc->sc_dev.dv_xname);
221 		return;
222 	}
223 
224 	printf("%s: %s Ethernet\n", dsc->sc_dev.dv_xname, typestr);
225 
226 	/* This interface is always enabled. */
227 	dsc->sc_enabled = 1;
228 
229 	/*
230 	 * Do generic NE2000 attach.  This will read the station address
231 	 * from the EEPROM.
232 	 */
233 	ne2000_attach(nsc, NULL);
234 
235 	/* Establish the interrupt handler. */
236 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
237 	    IPL_NET, dp8390_intr, dsc, dsc->sc_dev.dv_xname);
238 	if (isc->sc_ih == NULL)
239 		printf("%s: couldn't establish interrupt handler\n",
240 		    dsc->sc_dev.dv_xname);
241 }
242