if_ep_isa.c revision 54198
1/*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Herb Peyerl.
16 * 4. The name of Herb Peyerl may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD: head/sys/dev/ep/if_ep_isa.c 54198 1999-12-06 08:59:52Z mdodd $
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/socket.h>
37
38#include <sys/module.h>
39#include <sys/bus.h>
40
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/rman.h>
44
45#include <net/if.h>
46#include <net/if_arp.h>
47#include <net/if_media.h>
48
49#include <machine/clock.h>
50
51#include <isa/isavar.h>
52#include <isa/pnpvar.h>
53
54#include <dev/ep/if_epreg.h>
55#include <dev/ep/if_epvar.h>
56#include <i386/isa/elink.h>
57
58static int	get_eeprom_data	(int, int);
59
60static void	ep_isa_identify	(driver_t *, device_t);
61static int	ep_isa_probe	(device_t);
62static int	ep_isa_attach	(device_t);
63
64struct isa_ident {
65	u_int32_t	id;
66	char *		name;
67};
68const char * ep_isa_match_id (u_int32_t, struct isa_ident *);
69
70#define ISA_ID_3C509_TP    0x506d5090
71#define ISA_ID_3C509_BNC   0x506d5091
72#define ISA_ID_3C509_COMBO 0x506d5094
73#define ISA_ID_3C509_TPO   0x506d5095
74
75static struct isa_ident ep_isa_devs[] = {
76	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
77	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
78	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
79	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
80	{ 0,			NULL },
81};
82
83static struct isa_pnp_id ep_ids[] = {
84	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
85	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
86	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
87	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
88	{ 0xf780d041,	NULL }, /* PNP80f7 */
89	{ 0,		NULL },
90};
91
92/*
93 * We get eeprom data from the id_port given an offset into the eeprom.
94 * Basically; after the ID_sequence is sent to all of the cards; they enter
95 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
96 * the eeprom data.  We then read the port 16 times and with every read; the
97 * cards check for contention (ie: if one card writes a 0 bit and another
98 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
99 * compares the data on the bus; if there is a difference then that card goes
100 * into ID_WAIT state again). In the meantime; one bit of data is returned in
101 * the AX register which is conveniently returned to us by inb().  Hence; we
102 * read 16 times getting one bit of data with each read.
103 */
104
105static u_int16_t
106get_eeprom_data(id_port, offset)
107	int	id_port;
108	int	offset;
109{
110	int		i;
111	u_int16_t	data = 0;
112
113	outb(id_port, EEPROM_CMD_RD|offset);
114	DELAY(BIT_DELAY_MULTIPLE * 1000);
115	for (i = 0; i < 16; i++) {
116		DELAY(50);
117		data = (data << 1) | (inw(id_port) & 1);
118	}
119	return (data);
120}
121
122const char *
123ep_isa_match_id (id, isa_devs)
124	u_int32_t	      id;
125	struct isa_ident *      isa_devs;
126{
127	struct isa_ident *      i = isa_devs;
128	while(i->name != NULL) {
129	       if (id == i->id)
130		      return (i->name);
131	       i++;
132	}
133	return (NULL);
134}
135
136static void
137ep_isa_identify (driver_t *driver, device_t parent)
138{
139	int		tag = EP_LAST_TAG;
140	int		found = 0;
141	int		i;
142	int		j;
143	const char *	desc;
144	u_int16_t	data;
145	u_int32_t	irq;
146	u_int32_t	ioport;
147	u_int32_t	isa_id;
148	device_t	child;
149
150	outb(ELINK_ID_PORT, 0);
151	outb(ELINK_ID_PORT, 0);
152	elink_idseq(ELINK_509_POLY);
153	elink_reset();
154
155	DELAY(DELAY_MULTIPLE * 10000);
156
157	for (i = 0; i < EP_MAX_BOARDS; i++) {
158
159		outb(ELINK_ID_PORT, 0);
160		outb(ELINK_ID_PORT, 0);
161		elink_idseq(ELINK_509_POLY);
162		DELAY(400);
163
164		/* For the first probe, clear all
165		 * board's tag registers.
166		 * Otherwise kill off already-found
167		 * boards. -- linux 3c509.c
168		 */
169		if (i == 0) {
170			outb(ELINK_ID_PORT, 0xd0);
171		} else {
172			outb(ELINK_ID_PORT, 0xd8);
173		}
174
175		/* Get out of loop if we're out of cards. */
176		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
177		if (data != MFG_ID) {
178			break;
179		}
180
181		/* resolve contention using the Ethernet address */
182		for (j = 0; j < 3; j++) {
183			(void)get_eeprom_data(ELINK_ID_PORT, j);
184		}
185
186		/*
187		 * Construct an 'isa_id' in 'EISA'
188		 * format.
189		 */
190		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
191		isa_id = (htons(data) << 16);
192		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
193		isa_id |= htons(data);
194
195		/* Find known ISA boards */
196		desc = ep_isa_match_id(isa_id, ep_isa_devs);
197		if (!desc) {
198			if (bootverbose) {
199				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
200						isa_id);
201			}
202			break;
203		}
204
205		/* Retreive IRQ */
206		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
207		irq = (data >> 12);
208
209		/* Retreive IOPORT */
210		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
211#ifdef PC98
212		ioport = (((data & 0x1f) * 0x100) + 0x40d0);
213#else
214		ioport = (((data & 0x1f) << 4) + 0x200);
215#endif
216
217		/* Test for an adapter with PnP support. */
218		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
219		if (data == CAP_ISA) {
220			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
221			if (data & ICW1_IAS_PNP) {
222				if (bootverbose) {
223					device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n",
224					  	      ioport);
225				}
226				/* Set the adaptor tag so that the next card can be found. */
227				outb(ELINK_ID_PORT, tag--);
228				continue;
229			}
230		}
231
232		/* Set the adaptor tag so that the next card can be found. */
233		outb(ELINK_ID_PORT, tag--);
234
235		/* Activate the adaptor at the EEPROM location. */
236		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
237
238		/* Test for an adapter in TEST mode. */
239		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
240		data = inw(ioport + EP_W0_EEPROM_COMMAND);
241		if (data & EEPROM_TST_MODE) {
242			device_printf(parent, "if_ep: Adapter at 0x%03x in TEST mode!  Erase pencil mark.\n",
243			  	      ioport);
244			continue;
245		}
246
247		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
248		device_set_desc_copy(child, desc);
249		device_set_driver(child, driver);
250		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
251		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
252
253		if (bootverbose) {
254			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
255					desc, ioport, ioport + EP_IOSIZE, irq);
256		}
257
258		found++;
259	}
260
261	return;
262}
263
264static int
265ep_isa_probe (device_t dev)
266{
267	int	error = 0;
268
269	/* Check isapnp ids */
270	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
271
272	/* If the card had a PnP ID that didn't match any we know about */
273	if (error == ENXIO) {
274	       return (error);
275	}
276
277	/* If we had some other problem. */
278	if (!(error == 0 || error == ENOENT)) {
279		return (error);
280	}
281
282	/* If we have the resources we need then we're good to go. */
283	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
284	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
285		return (0);
286	}
287
288	return (ENXIO);
289}
290
291static int
292ep_isa_attach (device_t dev)
293{
294	struct ep_softc *	sc = device_get_softc(dev);
295	int			error = 0;
296
297	if ((error = ep_alloc(dev))) {
298		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
299		goto bad;
300	}
301
302	ep_get_media(sc);
303
304	GO_WINDOW(0);
305	SET_IRQ(BASE, rman_get_start(sc->irq));
306
307	if ((error = ep_attach(sc))) {
308		device_printf(dev, "ep_attach() failed! (%d)\n", error);
309		goto bad;
310	}
311
312	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
313				   sc, &sc->ep_intrhand))) {
314		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
315		goto bad;
316	}
317
318	return (0);
319bad:
320	ep_free(dev);
321	return (error);
322}
323
324static device_method_t ep_isa_methods[] = {
325	/* Device interface */
326	DEVMETHOD(device_identify,	ep_isa_identify),
327	DEVMETHOD(device_probe,		ep_isa_probe),
328	DEVMETHOD(device_attach,	ep_isa_attach),
329
330	{ 0, 0 }
331};
332
333static driver_t ep_isa_driver = {
334	"ep",
335	ep_isa_methods,
336	sizeof(struct ep_softc),
337};
338
339extern devclass_t ep_devclass;
340
341DRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
342