xref: /netbsd-src/sys/dev/mca/if_ne_mca.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_ne_mca.c,v 1.14 2008/03/12 14:31:11 cube Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jaromir Dolecek.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Driver for Novell NE/2 Ethernet Adapter (and clones).
41  *
42  * According to Linux ne2 driver, Arco and Compex card should be also
43  * supported by this driver. However, NetBSD driver was only tested
44  * with the Novell adapter so far.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_ne_mca.c,v 1.14 2008/03/12 14:31:11 cube Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/errno.h>
54 #include <sys/device.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/if_media.h>
61 #include <net/if_ether.h>
62 
63 #include <sys/bus.h>
64 
65 #include <dev/ic/dp8390reg.h>
66 #include <dev/ic/dp8390var.h>
67 
68 #include <dev/ic/ne2000reg.h>
69 #include <dev/ic/ne2000var.h>
70 
71 #include <dev/ic/rtl80x9reg.h>
72 #include <dev/ic/rtl80x9var.h>
73 
74 #include <dev/mca/mcadevs.h>
75 #include <dev/mca/mcavar.h>
76 
77 #define	NE2_NPORTS	0x30
78 
79 struct ne_mca_softc {
80 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
81 
82 	/* MCA-specific goo */
83 	void		*sc_ih;		/* interrupt handle */
84 };
85 
86 int	ne_mca_match(device_t, cfdata_t , void *);
87 void	ne_mca_attach(device_t, device_t, void *);
88 
89 CFATTACH_DECL_NEW(ne_mca, sizeof(struct ne_mca_softc),
90     ne_mca_match, ne_mca_attach, NULL, NULL);
91 
92 static const struct ne_mca_products {
93 	u_int32_t ne_id;
94 	const char *ne_name;
95 } ne_mca_products[] = {
96 	{ MCA_PRODUCT_ARCOAE,	"Arco Electronics AE/2 Ethernet Adapter" },
97 	{ MCA_PRODUCT_NE2,	"Novell NE/2 Ethernet Adapter" },
98 	{ MCA_PRODUCT_CENET16, "Compex Inc. PS/2 ENET16-MC/P Microchannel Ad."},
99 	{ 0, NULL }
100 };
101 
102 static const struct ne_mca_products *ne_mca_lookup(int id);
103 
104 static const struct ne_mca_products *
105 ne_mca_lookup(int id)
106 {
107 	const struct ne_mca_products *np;
108 
109 	for(np = ne_mca_products; np->ne_name; np++)
110 		if (id == np->ne_id)
111 			return (np);
112 
113 	return (NULL);
114 }
115 
116 int
117 ne_mca_match(device_t parent, cfdata_t cf, void *aux)
118 {
119 	struct mca_attach_args *ma = aux;
120 
121 	if (ne_mca_lookup(ma->ma_id))
122 		return (1);
123 
124 	return (0);
125 }
126 
127 /* These values were taken from NE/2 ADF file */
128 static const int ne_mca_irq[] = {
129 	3, 4, 5, 9
130 };
131 static const int ne_mca_iobase[] = {
132 	0, 0x1000, 0x2020, 0x8020, 0xa0a0, 0xb0b0, 0xc0c0, 0xc3d0
133 };
134 
135 void
136 ne_mca_attach(device_t parent, device_t self, void *aux)
137 {
138 	struct ne_mca_softc *psc = device_private(self);
139 	struct ne2000_softc *nsc = &psc->sc_ne2000;
140 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
141 	struct mca_attach_args *ma = aux;
142 	bus_space_tag_t nict;
143 	bus_space_handle_t nich;
144 	bus_space_tag_t asict;
145 	bus_space_handle_t asich;
146 	int pos2, iobase, irq;
147 	const struct ne_mca_products *np;
148 
149 	dsc->sc_dev = self;
150 
151 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
152 
153 	/*
154 	 * POS register 2: (adf pos0)
155 	 *
156 	 * 7 6 5 4 3 2 1 0
157 	 *   \_/ | \___/ \__ enable: 0=adapter disabled, 1=adapter enabled
158 	 *    |  |     \____ I/O, Mem: 001=0x1000-0x102f 010=0x2020-0x204f
159 	 *    |  |             011=0x8020-0x804f 100=0xa0a0-0xa0cf
160 	 *    |  |             101=0xb0b0-0xb0df 110=0xc0c0-0xc0ef
161 	 *     \  \            111=0xc3d0-0xc3ff
162 	 *      \  \________ Boot Rom: 1=disabled 0=enabled
163 	 *       \__________ Interrupt level: 00=3 01=4 10=5 11=9
164 	 */
165 
166 	np = ne_mca_lookup(ma->ma_id);
167 
168 	iobase = ne_mca_iobase[(pos2 & 0x0e) >> 1];
169 	irq = ne_mca_irq[(pos2 & 0x60) >> 5];
170 
171 	aprint_normal(" slot %d irq %d: %s\n", ma->ma_slot + 1, irq,
172 	    np->ne_name);
173 
174 	nict = ma->ma_iot;
175 
176 	/* Map the device. */
177 	if (bus_space_map(nict, iobase, NE2_NPORTS, 0, &nich)) {
178 		aprint_error_dev(self, "can't map i/o space\n");
179 		return;
180 	}
181 
182 	asict = nict;
183 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
184 	    NE2000_ASIC_NPORTS, &asich)) {
185 		aprint_error_dev(self, "can't subregion i/o space\n");
186 		return;
187 	}
188 
189 	dsc->sc_regt = nict;
190 	dsc->sc_regh = nich;
191 
192 	nsc->sc_asict = asict;
193 	nsc->sc_asich = asich;
194 
195 	/* This interface is always enabled. */
196 	dsc->sc_enabled = 1;
197 
198 	dsc->sc_mediachange	= NULL;
199 	dsc->sc_mediastatus	= NULL;
200 	dsc->sc_media_init	= NULL;
201 	dsc->init_card		= NULL;
202 
203 	/*
204 	 * This is necessary for NE/2. Hopefully the other clones also work
205 	 * this way.
206 	 */
207 	nsc->sc_type = NE2000_TYPE_AX88190;
208 
209 	/*
210 	 * Do generic NE2000 attach.  This will read the station address
211 	 * from the EEPROM.
212 	 */
213 	if (ne2000_attach(nsc, NULL))
214 		return;
215 
216 	/* establish interrupt handler */
217 	psc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, dp8390_intr,
218 			dsc);
219 	if (psc->sc_ih == NULL) {
220 		aprint_error_dev(self,
221 		    "couldn't establish interrupt handler\n");
222 		return;
223 	}
224 }
225