xref: /netbsd-src/sys/arch/x68k/dev/if_ne_intio.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: if_ne_intio.c,v 1.18 2015/05/20 09:17:18 ozaki-r 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.18 2015/05/20 09:17:18 ozaki-r 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 #if BPFILTER > 0
59 #include <net/bpf.h>
60 #include <net/bpfdesc.h>
61 #endif
62 
63 #include <machine/bus.h>
64 #include <machine/cpu.h>
65 
66 #include <dev/ic/dp8390reg.h>
67 #include <dev/ic/dp8390var.h>
68 #include <dev/ic/ne2000reg.h>
69 #include <dev/ic/ne2000var.h>
70 
71 #include <arch/x68k/dev/intiovar.h>
72 
73 #define NE_INTIO_ADDR  (0xece300)
74 #define NE_INTIO_ADDR2 (0xeceb00)
75 #define NE_INTIO_INTR  (0xf9)
76 #define NE_INTIO_INTR2 (0xf8)
77 
78 static int  ne_intio_match(device_t, cfdata_t, void *);
79 static void ne_intio_attach(device_t, device_t, void *);
80 
81 #define ne_intio_softc ne2000_softc
82 
83 CFATTACH_DECL_NEW(ne_intio, sizeof(struct ne_intio_softc),
84     ne_intio_match, ne_intio_attach, NULL, NULL);
85 
86 static int
87 ne_intio_match(device_t parent, cfdata_t cf, void *aux)
88 {
89 	struct intio_attach_args *ia = aux;
90 	bus_space_tag_t iot = ia->ia_bst;
91 	bus_space_handle_t ioh;
92 	bus_space_tag_t asict;
93 	bus_space_handle_t asich;
94 	int rv = 0;
95 
96 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
97 		ia->ia_addr = NE_INTIO_ADDR;
98 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
99 		ia->ia_intr = NE_INTIO_INTR;
100 
101 	/* fixed parameters */
102 	if (!(ia->ia_addr == NE_INTIO_ADDR  && ia->ia_intr == NE_INTIO_INTR ) &&
103 	    !(ia->ia_addr == NE_INTIO_ADDR2 && ia->ia_intr == NE_INTIO_INTR2)  )
104 		return 0;
105 
106 	/* Make sure this is a valid NE2000 I/O address */
107 	if ((ia->ia_addr & 0x1f) != 0)
108 		return 0;
109 
110 	/* Check whether the board is inserted or not */
111 	if (badaddr((void *)IIOV(ia->ia_addr)))
112 		return 0;
113 
114 	/* Map I/O space */
115 	if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS*2,
116 			BUS_SPACE_MAP_SHIFTED_EVEN, &ioh))
117 		return 0;
118 
119 	asict = iot;
120 	if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET*2,
121 			NE2000_ASIC_NPORTS*2, &asich))
122 		goto out;
123 
124 	/* Look for an NE2000 compatible card */
125 	rv = ne2000_detect(iot, ioh, asict, asich);
126 
127  out:
128 	bus_space_unmap(iot, ioh, NE2000_NPORTS);
129 	return (rv != 0) ? 1 : 0;
130 }
131 
132 static void
133 ne_intio_attach(device_t parent, device_t self, void *aux)
134 {
135 	struct ne_intio_softc *sc = device_private(self);
136 	struct dp8390_softc *dsc = &sc->sc_dp8390;
137 	struct intio_attach_args *ia = aux;
138 	bus_space_tag_t iot = ia->ia_bst;
139 	bus_space_handle_t ioh;
140 	bus_space_tag_t asict;
141 	bus_space_handle_t asich;
142 	const char *typestr;
143 	int netype;
144 
145 	dsc->sc_dev = self;
146 	aprint_normal(": Nereid Ethernet\n");
147 
148 	/* Map I/O space */
149 	if (bus_space_map(iot, ia->ia_addr, NE2000_NPORTS*2,
150 			BUS_SPACE_MAP_SHIFTED_EVEN, &ioh)){
151 		aprint_error_dev(self, "can't map I/O space\n");
152 		return;
153 	}
154 
155 	asict = iot;
156 	if (bus_space_subregion(iot, ioh, NE2000_ASIC_OFFSET*2,
157 			NE2000_ASIC_NPORTS*2, &asich)) {
158 		aprint_error_dev(self, "can't subregion I/O space\n");
159 		return;
160 	}
161 
162 	dsc->sc_regt = iot;
163 	dsc->sc_regh = ioh;
164 
165 	sc->sc_asict = asict;
166 	sc->sc_asich = asich;
167 
168 	/*
169 	 * detect it again, so we can print some information about
170 	 * the interface.
171 	 * XXX: Should I check NE1000 or NE2000 for Nereid?
172 	 */
173 	netype = ne2000_detect(iot, ioh, asict, asich);
174 	switch (netype) {
175 	case NE2000_TYPE_NE1000:
176 		typestr = "NE1000";
177 		break;
178 
179 	case NE2000_TYPE_NE2000:
180 		typestr = "NE2000";
181 		break;
182 
183 	case NE2000_TYPE_RTL8019:
184 		typestr = "NE2000 (RTL8019)";
185 		break;
186 
187 	default:
188 		aprint_error_dev(self, "where did the card go?!\n");
189 		return;
190 	}
191 
192 	aprint_normal_dev(self, "%s Ethernet\n", typestr);
193 
194 	/* This interface is always enabled */
195 	dsc->sc_enabled = 1;
196 
197 	/*
198 	 * Do generic NE2000 attach.
199 	 * This will read the mac address from the EEPROM.
200 	 */
201 	ne2000_attach(sc, NULL);
202 
203 	/* Establish the interrupt handler */
204 	if (intio_intr_establish(ia->ia_intr, "ne", dp8390_intr, dsc))
205 		aprint_error_dev(self,
206 		    "couldn't establish interrupt handler\n");
207 }
208