xref: /netbsd-src/sys/arch/x68k/dev/if_ne_neptune.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: if_ne_neptune.c,v 1.19 2010/01/19 22:06:23 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_ne_neptune.c,v 1.19 2010/01/19 22:06:23 pooka Exp $");
35 
36 #include "opt_inet.h"
37 #include "opt_ns.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <sys/errno.h>
45 #include <sys/syslog.h>
46 #include <sys/select.h>
47 #include <sys/device.h>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 
54 #ifdef INET
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59 #include <netinet/if_inarp.h>
60 #endif
61 
62 #ifdef NS
63 #include <netns/ns.h>
64 #include <netns/ns_if.h>
65 #endif
66 
67 #include <machine/bus.h>
68 
69 #include <dev/ic/dp8390reg.h>
70 #include <dev/ic/dp8390var.h>
71 
72 #include <dev/ic/ne2000reg.h>
73 #include <dev/ic/ne2000var.h>
74 
75 #include <dev/ic/rtl80x9reg.h>
76 #include <dev/ic/rtl80x9var.h>
77 
78 #include <arch/x68k/dev/neptunevar.h>
79 
80 static int ne_neptune_match(device_t, cfdata_t, void *);
81 static void ne_neptune_attach(device_t, device_t, void *);
82 
83 #define ne_neptune_softc ne2000_softc
84 
85 CFATTACH_DECL_NEW(ne_neptune, sizeof(struct ne_neptune_softc),
86     ne_neptune_match, ne_neptune_attach, NULL, NULL);
87 
88 int
89 ne_neptune_match(device_t parent, cfdata_t match, void *aux)
90 {
91 	struct neptune_attach_args *na = aux;
92 	bus_space_tag_t nict = na->na_bst;
93 	bus_space_handle_t nich;
94 	bus_space_tag_t asict;
95 	bus_space_handle_t asich;
96 	int rv = 0;
97 
98 	if (na->na_addr == NEPTUNECF_ADDR_DEFAULT)
99 		na->na_addr = 0x300;
100 
101 	/* Make sure this is a valid NE[12]000 i/o address. */
102 	if ((na->na_addr & 0x1f) != 0)
103 		return (0);
104 
105 	/* Map i/o space. */
106 	if (bus_space_map(nict, na->na_addr, NE2000_NPORTS*2, 0, &nich))
107 		return (0);
108 
109 	asict = nict;
110 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
111 	    NE2000_ASIC_NPORTS*2, &asich))
112 		goto out;
113 
114 	/* Look for an NE2000-compatible card. */
115 	rv = ne2000_detect(nict, nich, asict, asich);
116 
117  out:
118 	bus_space_unmap(nict, nich, NE2000_NPORTS);
119 	return (rv != 0) ? 1 : 0;
120 }
121 
122 void
123 ne_neptune_attach(device_t parent, device_t self, void *aux)
124 {
125 	struct ne_neptune_softc *nsc = device_private(self);
126 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
127 	struct neptune_attach_args *na = aux;
128 	bus_space_tag_t nict = na->na_bst;
129 	bus_space_handle_t nich;
130 	bus_space_tag_t asict = nict;
131 	bus_space_handle_t asich;
132 	const char *typestr;
133 	int netype;
134 
135 	dsc->sc_dev = self;
136 	aprint_normal("\n");
137 
138 	/* Map i/o space. */
139 	if (bus_space_map(nict, na->na_addr, NE2000_NPORTS*2, 0, &nich)) {
140 		aprint_error_dev(self, "can't map i/o space\n");
141 		return;
142 	}
143 
144 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
145 	    NE2000_ASIC_NPORTS*2, &asich)) {
146 		aprint_error_dev(self, "can't subregion i/o space\n");
147 		return;
148 	}
149 
150 	dsc->sc_regt = nict;
151 	dsc->sc_regh = nich;
152 
153 	nsc->sc_asict = asict;
154 	nsc->sc_asich = asich;
155 
156 	/*
157 	 * Detect it again, so we can print some information about the
158 	 * interface.
159 	 */
160 	netype = ne2000_detect(nict, nich, asict, asich);
161 	switch (netype) {
162 	case NE2000_TYPE_NE1000:
163 		typestr = "NE1000";
164 		break;
165 
166 	case NE2000_TYPE_NE2000:
167 		typestr = "NE2000";
168 		/*
169 		 * Check for a Realtek 8019.
170 		 */
171 		bus_space_write_1(nict, nich, ED_P0_CR,
172 		    ED_CR_PAGE_0 | ED_CR_STP);
173 		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
174 								RTL0_8019ID0 &&
175 		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
176 								RTL0_8019ID1) {
177 			typestr = "NE2000 (RTL8019)";
178 			dsc->sc_mediachange = rtl80x9_mediachange;
179 			dsc->sc_mediastatus = rtl80x9_mediastatus;
180 			dsc->init_card = rtl80x9_init_card;
181 			dsc->sc_media_init = rtl80x9_media_init;
182 		}
183 		break;
184 
185 	default:
186 		aprint_error_dev(self, "where did the card go?!\n");
187 		return;
188 	}
189 
190 	aprint_normal_dev(self, "%s Ethernet\n", typestr);
191 
192 	/* This interface is always enabled. */
193 	dsc->sc_enabled = 1;
194 
195 	/*
196 	 * Do generic NE2000 attach.  This will read the station address
197 	 * from the EEPROM.
198 	 */
199 	ne2000_attach(nsc, NULL);
200 
201 	/* Establish the interrupt handler. */
202 	if (neptune_intr_establish(na->na_intr, "ne", dp8390_intr, dsc))
203 		aprint_error_dev(self,
204 		    "couldn't establish interrupt handler\n");
205 }
206