summaryrefslogtreecommitdiff
path: root/sys/dev/mca/if_ep_mca.c
blob: 33aa7d99579d9e7411d3d0cf89e718d83ff9192c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*	$NetBSD: if_ep_mca.c,v 1.24 2020/04/08 04:32:14 msaitoh Exp $	*/

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jaromir Dolecek.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
 * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Herb Peyerl.
 * 4. The name of Herb Peyerl may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Driver for 3Com 3c529 cards.
 *
 * If you encounter sucky performance, try kernel without DEBUG/DIAGNOSTIC.
 * This helped on my test machine to change the performance of the card
 * from like 5KB/s to like 800 KB/s.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ep_mca.c,v 1.24 2020/04/08 04:32:14 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/if_media.h>

#include <sys/bus.h>

#include <dev/mii/miivar.h>

#include <dev/ic/elink3var.h>
#include <dev/ic/elink3reg.h>

#include <dev/mca/mcavar.h>
#include <dev/mca/mcadevs.h>

/*
 * MCA constants.
 */
#define MCA_CBIO		0x200	/* Configuration Base IO Address */
#define MCA_IOSZ		0x10	/* I/O space size */

int ep_mca_match(device_t , cfdata_t , void *);
void ep_mca_attach(device_t , device_t , void *);

CFATTACH_DECL_NEW(ep_mca, sizeof(struct ep_softc),
    ep_mca_match, ep_mca_attach, NULL, NULL);

const struct ep_mca_product {
	u_int32_t	epp_prodid;	/* MCA product ID */
	const char	*epp_name;	/* device name */
} ep_mca_products[] = {
	{ MCA_PRODUCT_3C529,	"3C529 Ethernet Adapter" },
	{ MCA_PRODUCT_3C529_TP,	"3c529-TP Ethernet Adapter" },
	{ MCA_PRODUCT_3C529_TM, "3c529 Ethernet Adapter (test mode)" },
	{ MCA_PRODUCT_3C529_2T, "3c529 Ethernet Adapter (10base2/T)" },
	{ MCA_PRODUCT_3C529_T,	"3c529 Ethernet Adapter (10baseT)"   },

	{ 0,			NULL },
};

static const struct ep_mca_product *ep_mca_lookup
   (const struct mca_attach_args *);

static const struct ep_mca_product *
ep_mca_lookup(const struct mca_attach_args *ma)
{
	const struct ep_mca_product *epp;

	for (epp = ep_mca_products; epp->epp_name != NULL; epp++)
		if (ma->ma_id == epp->epp_prodid)
			return (epp);

	return (NULL);
}

int
ep_mca_match(device_t parent, cfdata_t match, void *aux)
{
	struct mca_attach_args *ma = (struct mca_attach_args *) aux;

	if (ep_mca_lookup(ma) != NULL)
		return (1);

	return (0);
}

void
ep_mca_attach(device_t parent, device_t self, void *aux)
{
	struct ep_softc *sc = device_private(self);
	struct mca_attach_args *ma = aux;
	bus_space_handle_t ioh;
	int pos4, pos5, iobase, irq, media;
	const struct ep_mca_product *epp;

	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);

	/*
	 * POS register 2: (adf pos0)
	 * 7 6 5 4 3 2 1 0
	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
	 *
	 * POS register 3: (adf pos1)
	 *
	 * 7 6 5 4 3 2 1 0
	 * \________/
	 *          \_______ Boot ROM Address Range: 0=disabled
	 *                     X=0xc2000-0xc3fff + (x * 0x2000)
	 *
	 * POS register 4: (adf pos2)
	 *
	 * 7 6 5 4 3 2 1 0
	 * \________/  \_/
	 *          \    \__ Transceiver Type: 00=on-board (RJ45), 01=ext(AUI)
	 *           \______ I/O Address Range: 0x200-0x20f + ((x>>2) * 0x400)
	 *
	 * POS register 5: (adf pos3)
	 *
	 * 7 6 5 4 3 2 1 0
	 *          \____/
	 *               \__ Interrupt level
	 */

	iobase = MCA_CBIO + (((pos4 & 0xfc) >> 2) * 0x400);
	irq = (pos5 & 0x0f);

	sc->sc_dev = self;
	/* map the pio registers */
	if (bus_space_map(ma->ma_iot, iobase, MCA_IOSZ, 0, &ioh)) {
		aprint_error_dev(sc->sc_dev, "unable to map i/o space\n");
		return;
	}

	sc->sc_iot = ma->ma_iot;
	sc->sc_ioh = ioh;

	epp = ep_mca_lookup(ma);
	if (epp == NULL) {
		printf("\n");
		panic("ep_mca_attach: impossible");
	}

	printf(" slot %d irq %d: 3Com %s\n", ma->ma_slot + 1,
		irq, epp->epp_name);

	sc->enable = NULL;
	sc->disable = NULL;
	sc->enabled = 1;

	sc->bustype = ELINK_BUS_MCA;
	sc->ep_flags = 0;

	if (epconfig(sc, ELINK_CHIPSET_3C509, NULL))
		return;

	/* Map and establish the interrupt. */
	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, epintr, sc);
	if (sc->sc_ih == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "couldn't establish interrupt handler\n");
		return;
	}

	/*
	 * Set default media to be same as the one selected in POS.
	 */
	switch (pos4 & 0x03) {
	case 0x00:
		/* on-board RJ-45 */
		media = IFM_10_T;
		break;
	case 0x01:
		/* external AUI */
		media = IFM_10_5;
		break;
	case 0x02:
		/* undefined, should never be set */
		media = IFM_NONE;
		break;
	case 0x03:
		/* BNC */
		media = IFM_10_2;
		break;
	default:
		aprint_error_dev(sc->sc_dev, " cannot determine media\n");
		return;
	}
	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|media);
}