if_ep_isa.c revision 67164
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 67164 2000-10-15 14:19:01Z phk $
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
50#include <isa/isavar.h>
51
52#include <dev/ep/if_epreg.h>
53#include <dev/ep/if_epvar.h>
54#include <i386/isa/elink.h>
55
56static u_int16_t	get_eeprom_data	(int, int);
57
58static void		ep_isa_identify	(driver_t *, device_t);
59static int		ep_isa_probe	(device_t);
60static int		ep_isa_attach	(device_t);
61
62struct isa_ident {
63	u_int32_t	id;
64	char *		name;
65};
66const char * ep_isa_match_id (u_int32_t, struct isa_ident *);
67
68#define ISA_ID_3C509_XXX   0x0506d509
69#define ISA_ID_3C509_TP    0x506d5090
70#define ISA_ID_3C509_BNC   0x506d5091
71#define ISA_ID_3C509_COMBO 0x506d5094
72#define ISA_ID_3C509_TPO   0x506d5095
73#define ISA_ID_3C509_TPC   0x506d5098
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	{ ISA_ID_3C509_TPC,	"3Com 3C509-TPC EtherLink III" },
81	{ 0,			NULL },
82};
83
84static struct isa_pnp_id ep_ids[] = {
85	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
86	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
87	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
88	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
89	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
90	{ 0xf780d041,	NULL }, /* PNP80f7 */
91	{ 0,		NULL },
92};
93
94/*
95 * We get eeprom data from the id_port given an offset into the eeprom.
96 * Basically; after the ID_sequence is sent to all of the cards; they enter
97 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
98 * the eeprom data.  We then read the port 16 times and with every read; the
99 * cards check for contention (ie: if one card writes a 0 bit and another
100 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
101 * compares the data on the bus; if there is a difference then that card goes
102 * into ID_WAIT state again). In the meantime; one bit of data is returned in
103 * the AX register which is conveniently returned to us by inb().  Hence; we
104 * read 16 times getting one bit of data with each read.
105 */
106
107static u_int16_t
108get_eeprom_data(id_port, offset)
109	int	id_port;
110	int	offset;
111{
112	int		i;
113	u_int16_t	data = 0;
114
115	outb(id_port, EEPROM_CMD_RD|offset);
116	DELAY(BIT_DELAY_MULTIPLE * 1000);
117	for (i = 0; i < 16; i++) {
118		DELAY(50);
119		data = (data << 1) | (inw(id_port) & 1);
120	}
121	return (data);
122}
123
124const char *
125ep_isa_match_id (id, isa_devs)
126	u_int32_t	      id;
127	struct isa_ident *      isa_devs;
128{
129	struct isa_ident *      i = isa_devs;
130	while(i->name != NULL) {
131	       if (id == i->id)
132		      return (i->name);
133	       i++;
134	}
135	/*
136	 * If we see a card that is likely to be a 3c509
137	 * return something so that it will work; be annoying
138	 * so that the user will tell us about it though.
139	 */
140	if ((id >> 4) == ISA_ID_3C509_XXX) {
141		return ("Unknown 3c509; notify maintainer!");
142	}
143	return (NULL);
144}
145
146static void
147ep_isa_identify (driver_t *driver, device_t parent)
148{
149	int		tag = EP_LAST_TAG;
150	int		found = 0;
151	int		i;
152	int		j;
153	const char *	desc;
154	u_int16_t	data;
155	u_int32_t	irq;
156	u_int32_t	ioport;
157	u_int32_t	isa_id;
158	device_t	child;
159
160	outb(ELINK_ID_PORT, 0);
161	outb(ELINK_ID_PORT, 0);
162	elink_idseq(ELINK_509_POLY);
163	elink_reset();
164
165	DELAY(DELAY_MULTIPLE * 10000);
166
167	for (i = 0; i < EP_MAX_BOARDS; i++) {
168
169		outb(ELINK_ID_PORT, 0);
170		outb(ELINK_ID_PORT, 0);
171		elink_idseq(ELINK_509_POLY);
172		DELAY(400);
173
174		/* For the first probe, clear all
175		 * board's tag registers.
176		 * Otherwise kill off already-found
177		 * boards. -- linux 3c509.c
178		 */
179		if (i == 0) {
180			outb(ELINK_ID_PORT, 0xd0);
181		} else {
182			outb(ELINK_ID_PORT, 0xd8);
183		}
184
185		/* Get out of loop if we're out of cards. */
186		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
187		if (data != MFG_ID) {
188			break;
189		}
190
191		/* resolve contention using the Ethernet address */
192		for (j = 0; j < 3; j++) {
193			(void)get_eeprom_data(ELINK_ID_PORT, j);
194		}
195
196		/*
197		 * Construct an 'isa_id' in 'EISA'
198		 * format.
199		 */
200		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
201		isa_id = (htons(data) << 16);
202		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
203		isa_id |= htons(data);
204
205		/* Find known ISA boards */
206		desc = ep_isa_match_id(isa_id, ep_isa_devs);
207		if (!desc) {
208			if (bootverbose) {
209				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
210						isa_id);
211			}
212			continue;
213		}
214
215		/* Retreive IRQ */
216		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
217		irq = (data >> 12);
218
219		/* Retreive IOPORT */
220		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
221#ifdef PC98
222		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
223#else
224		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
225#endif
226
227		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
228			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
229					desc, ioport);
230			/* Set the adaptor tag so that the next card can be found. */
231			outb(ELINK_ID_PORT, tag--);
232			continue;
233		}
234
235		/* Test for an adapter with PnP support. */
236		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
237		if (data == CAP_ISA) {
238			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
239			if (data & ICW1_IAS_PNP) {
240				if (bootverbose) {
241					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
242					  	      desc, ioport);
243				}
244				/* Set the adaptor tag so that the next card can be found. */
245				outb(ELINK_ID_PORT, tag--);
246				continue;
247			}
248		}
249
250		/* Set the adaptor tag so that the next card can be found. */
251		outb(ELINK_ID_PORT, tag--);
252
253		/* Activate the adaptor at the EEPROM location. */
254		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
255
256		/* Test for an adapter in TEST mode. */
257		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
258		data = inw(ioport + EP_W0_EEPROM_COMMAND);
259		if (data & EEPROM_TST_MODE) {
260			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
261					desc, ioport);
262			continue;
263		}
264
265		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
266		device_set_desc_copy(child, desc);
267		device_set_driver(child, driver);
268		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
269		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
270
271		if (bootverbose) {
272			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
273					desc, ioport, ioport + EP_IOSIZE, irq);
274		}
275
276		found++;
277	}
278
279	return;
280}
281
282static int
283ep_isa_probe (device_t dev)
284{
285	int	error = 0;
286
287	/* Check isapnp ids */
288	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
289
290	/* If the card had a PnP ID that didn't match any we know about */
291	if (error == ENXIO) {
292	       return (error);
293	}
294
295	/* If we had some other problem. */
296	if (!(error == 0 || error == ENOENT)) {
297		return (error);
298	}
299
300	/* If we have the resources we need then we're good to go. */
301	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
302	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
303		return (0);
304	}
305
306	return (ENXIO);
307}
308
309static int
310ep_isa_attach (device_t dev)
311{
312	struct ep_softc *	sc = device_get_softc(dev);
313	int			error = 0;
314
315	if ((error = ep_alloc(dev))) {
316		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
317		goto bad;
318	}
319
320	ep_get_media(sc);
321
322	GO_WINDOW(0);
323	SET_IRQ(BASE, rman_get_start(sc->irq));
324
325	if ((error = ep_attach(sc))) {
326		device_printf(dev, "ep_attach() failed! (%d)\n", error);
327		goto bad;
328	}
329
330	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
331				   sc, &sc->ep_intrhand))) {
332		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
333		goto bad;
334	}
335
336	return (0);
337bad:
338	ep_free(dev);
339	return (error);
340}
341
342static device_method_t ep_isa_methods[] = {
343	/* Device interface */
344	DEVMETHOD(device_identify,	ep_isa_identify),
345	DEVMETHOD(device_probe,		ep_isa_probe),
346	DEVMETHOD(device_attach,	ep_isa_attach),
347
348	{ 0, 0 }
349};
350
351static driver_t ep_isa_driver = {
352	"ep",
353	ep_isa_methods,
354	sizeof(struct ep_softc),
355};
356
357extern devclass_t ep_devclass;
358
359DRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
360