1 /*	$OpenBSD: if_ne_isapnp.c,v 1.6 2004/05/12 06:35:11 tedu Exp $	*/
2 /*	$NetBSD: if_ne_isapnp.c,v 1.7 1998/07/23 19:30:45 christos 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 and Matt Thomas of the 3am Software Foundry.
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 #endif
80 
81 #include <machine/intr.h>
82 #include <machine/bus.h>
83 
84 #include <dev/ic/dp8390reg.h>
85 #include <dev/ic/dp8390var.h>
86 
87 #include <dev/ic/ne2000reg.h>
88 #include <dev/ic/ne2000var.h>
89 
90 #include <dev/ic/rtl80x9reg.h>
91 #include <dev/ic/rtl80x9var.h>
92 
93 #include <dev/isa/isavar.h>
94 
95 #include <dev/isa/isapnpreg.h>
96 #ifdef __NetBSD__
97 #include <dev/isa/isapnpvar.h>
98 #include <dev/isa/isapnpdevs.h>
99 #endif
100 
101 static int ne_isapnp_match(struct device *, void *, void *);
102 static void ne_isapnp_attach(struct device *, struct device *, void *);
103 
104 struct ne_isapnp_softc {
105 	struct	ne2000_softc sc_ne2000;		/* real "ne2000" softc */
106 
107 	/* ISA-specific goo. */
108 	void	*sc_ih;				/* interrupt cookie */
109 };
110 
111 struct cfattach ne_isapnp_ca = {
112 	sizeof(struct ne_isapnp_softc), ne_isapnp_match, ne_isapnp_attach
113 };
114 
115 static int
ne_isapnp_match(parent,match,aux)116 ne_isapnp_match(parent, match, aux)
117 	struct device *parent;
118 	void *match, *aux;
119 {
120 	return 1;
121 }
122 
123 static void
ne_isapnp_attach(struct device * parent,struct device * self,void * aux)124 ne_isapnp_attach(
125 	struct device *parent,
126 	struct device *self,
127 	void *aux)
128 {
129 	struct ne_isapnp_softc * const isc = (struct ne_isapnp_softc *)self;
130 	struct ne2000_softc * const nsc = &isc->sc_ne2000;
131 	struct dp8390_softc * const dsc = &nsc->sc_dp8390;
132 	struct isa_attach_args * const ipa = aux;
133 	bus_space_tag_t nict;
134 	bus_space_handle_t nich;
135 	bus_space_tag_t asict;
136 	bus_space_handle_t asich;
137 	const char *typestr;
138 	int netype;
139 
140 	printf("\n");
141 
142 	nict = ipa->ia_iot;
143 	nich = ipa->ipa_io[0].h;
144 
145 	asict = nict;
146 
147 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
148 	    NE2000_ASIC_NPORTS, &asich)) {
149 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
150 		return;
151 	}
152 
153 	dsc->sc_regt = nict;
154 	dsc->sc_regh = nich;
155 
156 	nsc->sc_asict = asict;
157 	nsc->sc_asich = asich;
158 
159 	/*
160 	 * Detect it again, so we can print some information about the
161 	 * interface.
162 	 */
163 	netype = ne2000_detect(nsc);
164 	switch (netype) {
165 	case NE2000_TYPE_NE1000:
166 		typestr = "NE1000";
167 		break;
168 
169 	case NE2000_TYPE_NE2000:
170 		typestr = "NE2000";
171 		/*
172 		 * Check for a RealTek 8019.
173 		 */
174 		bus_space_write_1(nict, nich, ED_P0_CR,
175 		    ED_CR_PAGE_0 | ED_CR_STP);
176 		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
177 								RTL0_8019ID0 &&
178 		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
179 								RTL0_8019ID1) {
180 			typestr = "NE2000 (RTL8019)";
181 			dsc->sc_mediachange = rtl80x9_mediachange;
182 			dsc->sc_mediastatus = rtl80x9_mediastatus;
183 			dsc->init_card = rtl80x9_init_card;
184 			dsc->sc_media_init = rtl80x9_media_init;
185 		}
186 		break;
187 
188 	default:
189 		printf("%s: where did the card go?!\n", dsc->sc_dev.dv_xname);
190 		return;
191 	}
192 
193 	printf("%s: %s Ethernet\n", dsc->sc_dev.dv_xname, typestr);
194 
195 	/* This interface is always enabled. */
196 	dsc->sc_enabled = 1;
197 
198 	/*
199 	 * Do generic NE2000 attach.  This will read the station address
200 	 * from the EEPROM.
201 	 */
202 	ne2000_attach(nsc, NULL);
203 
204 	/* Establish the interrupt handler. */
205 	isc->sc_ih = isa_intr_establish(ipa->ia_ic, ipa->ipa_irq[0].num,
206 	    IST_EDGE, IPL_NET, dp8390_intr, dsc,
207 	    dsc->sc_dev.dv_xname);
208 	if (isc->sc_ih == NULL)
209 		printf("%s: couldn't establish interrupt handler\n",
210 		    dsc->sc_dev.dv_xname);
211 }
212