1/*	$NetBSD: if_ep_mca.c,v 1.21 2008/08/27 05:33:47 christos 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 * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
34 * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *      This product includes software developed by Herb Peyerl.
48 * 4. The name of Herb Peyerl may not be used to endorse or promote products
49 *    derived from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63/*
64 * Driver for 3Com 3c529 cards.
65 *
66 * If you encouter sucky performance, try kernel without DEBUG/DIAGNOSTIC.
67 * This helped on my test machine to change the performance of the card
68 * from like 5KB/s to like 800 KB/s.
69 */
70
71#include <sys/cdefs.h>
72__KERNEL_RCSID(0, "$NetBSD: if_ep_mca.c,v 1.21 2008/08/27 05:33:47 christos Exp $");
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/mbuf.h>
77#include <sys/socket.h>
78#include <sys/ioctl.h>
79#include <sys/errno.h>
80#include <sys/device.h>
81
82#include <net/if.h>
83#include <net/if_ether.h>
84#include <net/if_media.h>
85
86#include <sys/bus.h>
87
88#include <dev/mii/miivar.h>
89
90#include <dev/ic/elink3var.h>
91#include <dev/ic/elink3reg.h>
92
93#include <dev/mca/mcavar.h>
94#include <dev/mca/mcadevs.h>
95
96/*
97 * MCA constants.
98 */
99#define MCA_CBIO		0x200	/* Configuration Base IO Address */
100#define MCA_IOSZ		0x10	/* I/O space size */
101
102int ep_mca_match(device_t , cfdata_t , void *);
103void ep_mca_attach(device_t , device_t , void *);
104
105CFATTACH_DECL_NEW(ep_mca, sizeof(struct ep_softc),
106    ep_mca_match, ep_mca_attach, NULL, NULL);
107
108const struct ep_mca_product {
109	u_int32_t	epp_prodid;	/* MCA product ID */
110	const char	*epp_name;	/* device name */
111} ep_mca_products[] = {
112	{ MCA_PRODUCT_3C529,	"3C529 Ethernet Adapter" },
113	{ MCA_PRODUCT_3C529_TP,	"3c529-TP Ethernet Adapter" },
114	{ MCA_PRODUCT_3C529_TM, "3c529 Ethernet Adapter (test mode)" },
115	{ MCA_PRODUCT_3C529_2T, "3c529 Ethernet Adapter (10base2/T)" },
116	{ MCA_PRODUCT_3C529_T,	"3c529 Ethernet Adapter (10baseT)"   },
117
118	{ 0,			NULL },
119};
120
121static const struct ep_mca_product *ep_mca_lookup
122   (const struct mca_attach_args *);
123
124static const struct ep_mca_product *
125ep_mca_lookup(const struct mca_attach_args *ma)
126{
127	const struct ep_mca_product *epp;
128
129	for (epp = ep_mca_products; epp->epp_name != NULL; epp++)
130		if (ma->ma_id == epp->epp_prodid)
131			return (epp);
132
133	return (NULL);
134}
135
136int
137ep_mca_match(device_t parent, cfdata_t match, void *aux)
138{
139	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
140
141	if (ep_mca_lookup(ma) != NULL)
142		return (1);
143
144	return (0);
145}
146
147void
148ep_mca_attach(device_t parent, device_t self, void *aux)
149{
150	struct ep_softc *sc = device_private(self);
151	struct mca_attach_args *ma = aux;
152	bus_space_handle_t ioh;
153	int pos4, pos5, iobase, irq, media;
154	const struct ep_mca_product *epp;
155
156	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
157	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
158
159	/*
160	 * POS register 2: (adf pos0)
161	 * 7 6 5 4 3 2 1 0
162	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
163	 *
164	 * POS register 3: (adf pos1)
165	 *
166	 * 7 6 5 4 3 2 1 0
167	 * \________/
168	 *          \_______ Boot ROM Address Range: 0=disabled
169	 *                     X=0xc2000-0xc3fff + (x * 0x2000)
170	 *
171	 * POS register 4: (adf pos2)
172	 *
173	 * 7 6 5 4 3 2 1 0
174	 * \________/  \_/
175	 *          \    \__ Transceiver Type: 00=on-board (RJ45), 01=ext(AUI)
176	 *           \______ I/O Address Range: 0x200-0x20f + ((x>>2) * 0x400)
177	 *
178	 * POS register 5: (adf pos3)
179	 *
180	 * 7 6 5 4 3 2 1 0
181	 *          \____/
182	 *               \__ Interrupt level
183	 */
184
185	iobase = MCA_CBIO + (((pos4 & 0xfc) >> 2) * 0x400);
186	irq = (pos5 & 0x0f);
187
188	sc->sc_dev = self;
189	/* map the pio registers */
190	if (bus_space_map(ma->ma_iot, iobase, MCA_IOSZ, 0, &ioh)) {
191		aprint_error_dev(sc->sc_dev, "unable to map i/o space\n");
192		return;
193	}
194
195	sc->sc_iot = ma->ma_iot;
196	sc->sc_ioh = ioh;
197
198	epp = ep_mca_lookup(ma);
199	if (epp == NULL) {
200		printf("\n");
201		panic("ep_mca_attach: impossible");
202	}
203
204	printf(" slot %d irq %d: 3Com %s\n", ma->ma_slot + 1,
205		irq, epp->epp_name);
206
207	sc->enable = NULL;
208	sc->disable = NULL;
209	sc->enabled = 1;
210
211	sc->bustype = ELINK_BUS_MCA;
212	sc->ep_flags = 0;
213
214	if (epconfig(sc, ELINK_CHIPSET_3C509, NULL))
215		return;
216
217	/* Map and establish the interrupt. */
218	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, epintr, sc);
219	if (sc->sc_ih == NULL) {
220		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt handler\n");
221		return;
222	}
223
224	/*
225	 * Set default media to be same as the one selected in POS.
226	 */
227	switch (pos4 & 0x03) {
228	case 0x00:
229		/* on-board RJ-45 */
230		media = IFM_10_T;
231		break;
232	case 0x01:
233		/* external AUI */
234		media = IFM_10_5;
235		break;
236	case 0x02:
237		/* undefined, should never be set */
238		media = IFM_NONE;
239		break;
240	case 0x03:
241		/* BNC */
242		media = IFM_10_2;
243		break;
244	default:
245		aprint_error_dev(sc->sc_dev, " cannot determine media\n");
246		return;
247	}
248	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|media);
249}
250