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