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