xref: /netbsd-src/sys/arch/x68k/dev/if_ne_intio.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: if_ne_intio.c,v 1.17 2011/10/16 03:10:18 isaki Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Tetsuya Isaki. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Ethernet part of Nereid Ethernet/USB/Memory board
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ne_intio.c,v 1.17 2011/10/16 03:10:18 isaki Exp $");
34 
35 #include "opt_inet.h"
36 #include "opt_ns.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/select.h>
43 #include <sys/device.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_ether.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/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/if_inarp.h>
56 #endif
57 
58 #ifdef NS
59 #include <netns/ns.h>
60 #include <netns/ns_if.h>
61 #endif
62 
63 #if BPFILTER > 0
64 #include <net/bpf.h>
65 #include <net/bpfdesc.h>
66 #endif
67 
68 #include <machine/bus.h>
69 #include <machine/cpu.h>
70 
71 #include <dev/ic/dp8390reg.h>
72 #include <dev/ic/dp8390var.h>
73 #include <dev/ic/ne2000reg.h>
74 #include <dev/ic/ne2000var.h>
75 
76 #include <arch/x68k/dev/intiovar.h>
77 
78 #define NE_INTIO_ADDR  (0xece300)
79 #define NE_INTIO_ADDR2 (0xeceb00)
80 #define NE_INTIO_INTR  (0xf9)
81 #define NE_INTIO_INTR2 (0xf8)
82 
83 static int  ne_intio_match(device_t, cfdata_t, void *);
84 static void ne_intio_attach(device_t, device_t, void *);
85 
86 #define ne_intio_softc ne2000_softc
87 
88 CFATTACH_DECL_NEW(ne_intio, sizeof(struct ne_intio_softc),
89     ne_intio_match, ne_intio_attach, NULL, NULL);
90 
91 static int
92 ne_intio_match(device_t parent, cfdata_t cf, void *aux)
93 {
94 	struct intio_attach_args *ia = aux;
95 	bus_space_tag_t iot = ia->ia_bst;
96 	bus_space_handle_t ioh;
97 	bus_space_tag_t asict;
98 	bus_space_handle_t asich;
99 	int rv = 0;
100 
101 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
102 		ia->ia_addr = NE_INTIO_ADDR;
103 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
104 		ia->ia_intr = NE_INTIO_INTR;
105 
106 	/* fixed parameters */
107 	if (!(ia->ia_addr == NE_INTIO_ADDR  && ia->ia_intr == NE_INTIO_INTR ) &&
108 	    !(ia->ia_addr == NE_INTIO_ADDR2 && ia->ia_intr == NE_INTIO_INTR2)  )
109 		return 0;
110 
111 	/* Make sure this is a valid NE2000 I/O address */
112 	if ((ia->ia_addr & 0x1f) != 0)
113 		return 0;
114 
115 	/* Check whether the board is inserted or not */
116 	if (badaddr((void *)IIOV(ia->ia_addr)))
117 		return 0;
118 
119 	/* Map I/O space */
120 	if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS*2,
121 			BUS_SPACE_MAP_SHIFTED_EVEN, &ioh))
122 		return 0;
123 
124 	asict = iot;
125 	if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET*2,
126 			NE2000_ASIC_NPORTS*2, &asich))
127 		goto out;
128 
129 	/* Look for an NE2000 compatible card */
130 	rv = ne2000_detect(iot, ioh, asict, asich);
131 
132  out:
133 	bus_space_unmap(iot, ioh, NE2000_NPORTS);
134 	return (rv != 0) ? 1 : 0;
135 }
136 
137 static void
138 ne_intio_attach(device_t parent, device_t self, void *aux)
139 {
140 	struct ne_intio_softc *sc = device_private(self);
141 	struct dp8390_softc *dsc = &sc->sc_dp8390;
142 	struct intio_attach_args *ia = aux;
143 	bus_space_tag_t iot = ia->ia_bst;
144 	bus_space_handle_t ioh;
145 	bus_space_tag_t asict;
146 	bus_space_handle_t asich;
147 	const char *typestr;
148 	int netype;
149 
150 	dsc->sc_dev = self;
151 	aprint_normal(": Nereid Ethernet\n");
152 
153 	/* Map I/O space */
154 	if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS*2,
155 			BUS_SPACE_MAP_SHIFTED_EVEN, &ioh)){
156 		aprint_error_dev(self, "can't map I/O space\n");
157 		return;
158 	}
159 
160 	asict = iot;
161 	if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET*2,
162 			NE2000_ASIC_NPORTS*2, &asich)) {
163 		aprint_error_dev(self, "can't subregion I/O space\n");
164 		return;
165 	}
166 
167 	dsc->sc_regt = iot;
168 	dsc->sc_regh = ioh;
169 
170 	sc->sc_asict = asict;
171 	sc->sc_asich = asich;
172 
173 	/*
174 	 * detect it again, so we can print some information about
175 	 * the interface.
176 	 * XXX: Should I check NE1000 or NE2000 for Nereid?
177 	 */
178 	netype = ne2000_detect(iot, ioh, asict, asich);
179 	switch (netype) {
180 	case NE2000_TYPE_NE1000:
181 		typestr = "NE1000";
182 		break;
183 
184 	case NE2000_TYPE_NE2000:
185 		typestr = "NE2000";
186 		break;
187 
188 	case NE2000_TYPE_RTL8019:
189 		typestr = "NE2000 (RTL8019)";
190 		break;
191 
192 	default:
193 		aprint_error_dev(self, "where did the card go?!\n");
194 		return;
195 	}
196 
197 	aprint_normal_dev(self, "%s Ethernet\n", typestr);
198 
199 	/* This interface is always enabled */
200 	dsc->sc_enabled = 1;
201 
202 	/*
203 	 * Do generic NE2000 attach.
204 	 * This will read the mac address from the EEPROM.
205 	 */
206 	ne2000_attach(sc, NULL);
207 
208 	/* Establish the interrupt handler */
209 	if (intio_intr_establish(ia->ia_intr, "ne", dp8390_intr, dsc))
210 		aprint_error_dev(self,
211 		    "couldn't establish interrupt handler\n");
212 }
213