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