xref: /openbsd-src/sys/dev/isa/if_ne_isa.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: if_ne_isa.c,v 1.14 2013/08/07 01:06:32 bluhm 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/selinfo.h>
44 #include <sys/device.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/if_ether.h>
55 #endif
56 
57 #if NBPFILTER > 0
58 #include <net/bpf.h>
59 #endif
60 
61 #include <machine/intr.h>
62 #include <machine/bus.h>
63 
64 #include <dev/ic/dp8390reg.h>
65 #include <dev/ic/dp8390var.h>
66 
67 #include <dev/ic/ne2000reg.h>
68 #include <dev/ic/ne2000var.h>
69 
70 #include <dev/ic/rtl80x9reg.h>
71 #include <dev/ic/rtl80x9var.h>
72 
73 #include <dev/isa/isavar.h>
74 
75 int	ne_isa_match(struct device *, void *, void *);
76 void	ne_isa_attach(struct device *, struct device *, void *);
77 
78 struct ne_isa_softc {
79 	struct	ne2000_softc sc_ne2000;		/* real "ne2000" softc */
80 
81 	/* ISA-specific goo. */
82 	void	*sc_ih;				/* interrupt cookie */
83 };
84 
85 struct cfattach ne_isa_ca = {
86 	sizeof(struct ne_isa_softc), ne_isa_match, ne_isa_attach
87 };
88 
89 int
90 ne_isa_match(struct device *parent, void *match, void *aux)
91 {
92 	struct ne_isa_softc *isc = match;
93 	struct ne2000_softc *nsc = &isc->sc_ne2000;
94 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
95 	struct isa_attach_args *ia = aux;
96 	bus_space_tag_t nict = ia->ia_iot;
97 	bus_space_handle_t nich;
98 	bus_space_tag_t asict;
99 	bus_space_handle_t asich;
100 
101 	/* Disallow wildcarded values. */
102 	if (ia->ia_irq ==  -1 /* ISACF_IRQ_DEFAULT */)
103 		return (0);
104 	if (ia->ia_iobase == -1 /* ISACF_PORT_DEFAULT */)
105 		return (0);
106 
107 	/* Make sure this is a valid NE[12]000 i/o address. */
108 	if ((ia->ia_iobase & 0x1f) != 0)
109 		return (0);
110 
111 	/* Map i/o space. */
112 	if (bus_space_map(nict, ia->ia_iobase, NE2000_NPORTS, 0, &nich))
113 		return (0);
114 
115 	asict = nict;
116 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
117 	    NE2000_ASIC_NPORTS, &asich))
118 		goto out;
119 
120 	dsc->sc_regt = nict;
121 	dsc->sc_regh = nich;
122 
123 	nsc->sc_asict = asict;
124 	nsc->sc_asich = asich;
125 
126 	/* Look for an NE2000-compatible card. */
127 	nsc->sc_type = ne2000_detect(nsc);
128 
129 	if (nsc->sc_type)
130 		ia->ia_iosize = NE2000_NPORTS;
131 
132  out:
133 	bus_space_unmap(nict, nich, NE2000_NPORTS);
134 	return (nsc->sc_type);
135 }
136 
137 void
138 ne_isa_attach(struct device *parent, struct device *self, void *aux)
139 {
140 	struct ne_isa_softc *isc = (struct ne_isa_softc *)self;
141 	struct ne2000_softc *nsc = &isc->sc_ne2000;
142 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
143 	struct isa_attach_args *ia = aux;
144 	bus_space_tag_t nict = ia->ia_iot;
145 	bus_space_handle_t nich;
146 	bus_space_tag_t asict = nict;
147 	bus_space_handle_t asich;
148 	const char *typestr;
149 
150 	/* Map i/o space. */
151 	if (bus_space_map(nict, ia->ia_iobase, NE2000_NPORTS, 0, &nich)) {
152 		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
153 		return;
154 	}
155 
156 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
157 	    NE2000_ASIC_NPORTS, &asich)) {
158 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
159 		return;
160 	}
161 
162 	dsc->sc_regt = nict;
163 	dsc->sc_regh = nich;
164 
165 	nsc->sc_asict = asict;
166 	nsc->sc_asich = asich;
167 
168 	switch (nsc->sc_type) {
169 	case NE2000_TYPE_NE1000:
170 		typestr = "NE1000";
171 		break;
172 
173 	case NE2000_TYPE_NE2000:
174 		typestr = "NE2000";
175 		/*
176 		 * Check for a RealTek 8019.
177 		 */
178 		bus_space_write_1(nict, nich, ED_P0_CR,
179 		    ED_CR_PAGE_0 | ED_CR_STP);
180 		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
181 								RTL0_8019ID0 &&
182 		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
183 								RTL0_8019ID1) {
184 			typestr = "NE2000 (RTL8019)";
185 			dsc->sc_mediachange = rtl80x9_mediachange;
186 			dsc->sc_mediastatus = rtl80x9_mediastatus;
187 			dsc->init_card = rtl80x9_init_card;
188 			dsc->sc_media_init = rtl80x9_media_init;
189 		}
190 		break;
191 
192 	default:
193 		printf(": where did the card go?!\n");
194 		return;
195 	}
196 
197 	printf(", %s", typestr);
198 
199 	/* This interface is always enabled. */
200 	dsc->sc_enabled = 1;
201 
202 	/*
203 	 * Do generic NE2000 attach.  This will read the station address
204 	 * from the EEPROM.
205 	 */
206 	ne2000_attach(nsc, NULL);
207 
208 	/* Establish the interrupt handler. */
209 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
210 	    IPL_NET, dp8390_intr, dsc, dsc->sc_dev.dv_xname);
211 	if (isc->sc_ih == NULL)
212 		printf(": couldn't establish interrupt handler\n");
213 }
214