if_ep_isa.c revision 69788
1198157Srrs/*
2198157Srrs * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3198157Srrs * All rights reserved.
4198157Srrs *
5198157Srrs * Redistribution and use in source and binary forms, with or without
6198157Srrs * modification, are permitted provided that the following conditions
7198157Srrs * are met:
8198157Srrs * 1. Redistributions of source code must retain the above copyright
9198157Srrs *    notice, this list of conditions and the following disclaimer.
10198157Srrs * 2. Redistributions in binary form must reproduce the above copyright
11198157Srrs *    notice, this list of conditions and the following disclaimer in the
12198157Srrs *    documentation and/or other materials provided with the distribution.
13198157Srrs * 3. All advertising materials mentioning features or use of this software
14198157Srrs *    must display the following acknowledgement:
15198157Srrs *      This product includes software developed by Herb Peyerl.
16198157Srrs * 4. The name of Herb Peyerl may not be used to endorse or promote products
17198157Srrs *    derived from this software without specific prior written permission.
18198157Srrs *
19198157Srrs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20198157Srrs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21198157Srrs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22198157Srrs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23198157Srrs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24198157Srrs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25198157Srrs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26198157Srrs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27198157Srrs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28198157Srrs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29198157Srrs *
30198157Srrs * $FreeBSD: head/sys/dev/ep/if_ep_isa.c 69788 2000-12-09 04:25:07Z nyan $
31198157Srrs */
32198157Srrs
33198157Srrs#include <sys/param.h>
34198157Srrs#include <sys/systm.h>
35198157Srrs#include <sys/kernel.h>
36198157Srrs#include <sys/socket.h>
37198157Srrs
38198626Srrs#include <sys/module.h>
39198157Srrs#include <sys/bus.h>
40198157Srrs
41198626Srrs#include <machine/bus.h>
42198157Srrs#include <machine/resource.h>
43198157Srrs#include <sys/rman.h>
44198626Srrs
45198157Srrs#include <net/if.h>
46198157Srrs#include <net/if_arp.h>
47198157Srrs#include <net/if_media.h>
48198626Srrs
49198157Srrs
50198626Srrs#include <isa/isavar.h>
51198626Srrs
52198626Srrs#include <dev/ep/if_epreg.h>
53198157Srrs#include <dev/ep/if_epvar.h>
54198157Srrs#include <i386/isa/elink.h>
55198626Srrs
56198626Srrsstatic u_int16_t	get_eeprom_data	(int, int);
57198626Srrs
58198626Srrsstatic void		ep_isa_identify	(driver_t *, device_t);
59198626Srrsstatic int		ep_isa_probe	(device_t);
60198157Srrsstatic int		ep_isa_attach	(device_t);
61198157Srrs
62198157Srrsstruct isa_ident {
63198157Srrs	u_int32_t	id;
64198157Srrs	char *		name;
65198157Srrs};
66198157Srrsconst char * ep_isa_match_id (u_int32_t, struct isa_ident *);
67198157Srrs
68198157Srrs#define ISA_ID_3C509_XXX   0x0506d509
69198157Srrs#define ISA_ID_3C509_TP    0x506d5090
70198157Srrs#define ISA_ID_3C509_BNC   0x506d5091
71198157Srrs#define ISA_ID_3C509_COMBO 0x506d5094
72198157Srrs#define ISA_ID_3C509_TPO   0x506d5095
73198157Srrs#define ISA_ID_3C509_TPC   0x506d5098
74198626Srrs#ifdef PC98
75198157Srrs#define ISA_ID_3C569B_COMBO 0x506d5694
76198626Srrs#define ISA_ID_3C569B_TPO   0x506d5695
77198626Srrs#endif
78198626Srrs
79198626Srrsstatic struct isa_ident ep_isa_devs[] = {
80198157Srrs	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
81198157Srrs	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
82198157Srrs	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
83198626Srrs	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
84198626Srrs	{ ISA_ID_3C509_TPC,	"3Com 3C509-TPC EtherLink III" },
85198626Srrs#ifdef PC98
86198626Srrs	{ ISA_ID_3C569B_COMBO,	"3Com 3C569B-J-Combo EtherLink III" },
87198626Srrs	{ ISA_ID_3C569B_TPO,	"3Com 3C569B-J-TPO EtherLink III" },
88198157Srrs#endif
89198157Srrs	{ 0,			NULL },
90198157Srrs};
91198157Srrs
92198157Srrsstatic struct isa_pnp_id ep_ids[] = {
93198157Srrs	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
94198157Srrs	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
95198157Srrs	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
96198157Srrs	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
97198157Srrs	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
98198157Srrs	{ 0xf780d041,	NULL }, /* PNP80f7 */
99198157Srrs	{ 0,		NULL },
100198157Srrs};
101198157Srrs
102198157Srrs/*
103198626Srrs * We get eeprom data from the id_port given an offset into the eeprom.
104198157Srrs * Basically; after the ID_sequence is sent to all of the cards; they enter
105198626Srrs * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
106198626Srrs * the eeprom data.  We then read the port 16 times and with every read; the
107198626Srrs * cards check for contention (ie: if one card writes a 0 bit and another
108198157Srrs * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
109198157Srrs * compares the data on the bus; if there is a difference then that card goes
110198157Srrs * into ID_WAIT state again). In the meantime; one bit of data is returned in
111198626Srrs * the AX register which is conveniently returned to us by inb().  Hence; we
112198626Srrs * read 16 times getting one bit of data with each read.
113198626Srrs */
114198626Srrs
115198626Srrsstatic u_int16_t
116198157Srrsget_eeprom_data(id_port, offset)
117198157Srrs	int	id_port;
118198157Srrs	int	offset;
119198157Srrs{
120198157Srrs	int		i;
121198157Srrs	u_int16_t	data = 0;
122198157Srrs
123198157Srrs	outb(id_port, EEPROM_CMD_RD|offset);
124198157Srrs	DELAY(BIT_DELAY_MULTIPLE * 1000);
125198157Srrs	for (i = 0; i < 16; i++) {
126198157Srrs		DELAY(50);
127198157Srrs		data = (data << 1) | (inw(id_port) & 1);
128	}
129	return (data);
130}
131
132const char *
133ep_isa_match_id (id, isa_devs)
134	u_int32_t	      id;
135	struct isa_ident *      isa_devs;
136{
137	struct isa_ident *      i = isa_devs;
138	while(i->name != NULL) {
139	       if (id == i->id)
140		      return (i->name);
141	       i++;
142	}
143	/*
144	 * If we see a card that is likely to be a 3c509
145	 * return something so that it will work; be annoying
146	 * so that the user will tell us about it though.
147	 */
148	if ((id >> 4) == ISA_ID_3C509_XXX) {
149		return ("Unknown 3c509; notify maintainer!");
150	}
151	return (NULL);
152}
153
154static void
155ep_isa_identify (driver_t *driver, device_t parent)
156{
157	int		tag = EP_LAST_TAG;
158	int		found = 0;
159	int		i;
160	int		j;
161	const char *	desc;
162	u_int16_t	data;
163	u_int32_t	irq;
164	u_int32_t	ioport;
165	u_int32_t	isa_id;
166	device_t	child;
167
168	outb(ELINK_ID_PORT, 0);
169	outb(ELINK_ID_PORT, 0);
170	elink_idseq(ELINK_509_POLY);
171	elink_reset();
172
173	DELAY(DELAY_MULTIPLE * 10000);
174
175	for (i = 0; i < EP_MAX_BOARDS; i++) {
176
177		outb(ELINK_ID_PORT, 0);
178		outb(ELINK_ID_PORT, 0);
179		elink_idseq(ELINK_509_POLY);
180		DELAY(400);
181
182		/* For the first probe, clear all
183		 * board's tag registers.
184		 * Otherwise kill off already-found
185		 * boards. -- linux 3c509.c
186		 */
187		if (i == 0) {
188			outb(ELINK_ID_PORT, 0xd0);
189		} else {
190			outb(ELINK_ID_PORT, 0xd8);
191		}
192
193		/* Get out of loop if we're out of cards. */
194		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
195		if (data != MFG_ID) {
196			break;
197		}
198
199		/* resolve contention using the Ethernet address */
200		for (j = 0; j < 3; j++) {
201			(void)get_eeprom_data(ELINK_ID_PORT, j);
202		}
203
204		/*
205		 * Construct an 'isa_id' in 'EISA'
206		 * format.
207		 */
208		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
209		isa_id = (htons(data) << 16);
210		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
211		isa_id |= htons(data);
212
213		/* Find known ISA boards */
214		desc = ep_isa_match_id(isa_id, ep_isa_devs);
215		if (!desc) {
216			if (bootverbose) {
217				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
218						isa_id);
219			}
220			continue;
221		}
222
223		/* Retreive IRQ */
224		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
225		irq = (data >> 12);
226
227		/* Retreive IOPORT */
228		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
229#ifdef PC98
230		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
231#else
232		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
233#endif
234
235		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
236			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
237					desc, ioport);
238			/* Set the adaptor tag so that the next card can be found. */
239			outb(ELINK_ID_PORT, tag--);
240			continue;
241		}
242
243		/* Test for an adapter with PnP support. */
244		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
245		if (data == CAP_ISA) {
246			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
247			if (data & ICW1_IAS_PNP) {
248				if (bootverbose) {
249					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
250					  	      desc, ioport);
251				}
252				/* Set the adaptor tag so that the next card can be found. */
253				outb(ELINK_ID_PORT, tag--);
254				continue;
255			}
256		}
257
258		/* Set the adaptor tag so that the next card can be found. */
259		outb(ELINK_ID_PORT, tag--);
260
261		/* Activate the adaptor at the EEPROM location. */
262		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
263
264		/* Test for an adapter in TEST mode. */
265		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
266		data = inw(ioport + EP_W0_EEPROM_COMMAND);
267		if (data & EEPROM_TST_MODE) {
268			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
269					desc, ioport);
270			continue;
271		}
272
273		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
274		device_set_desc_copy(child, desc);
275		device_set_driver(child, driver);
276		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
277		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
278
279		if (bootverbose) {
280			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
281					desc, ioport, ioport + EP_IOSIZE, irq);
282		}
283
284		found++;
285	}
286
287	return;
288}
289
290static int
291ep_isa_probe (device_t dev)
292{
293	int	error = 0;
294
295	/* Check isapnp ids */
296	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
297
298	/* If the card had a PnP ID that didn't match any we know about */
299	if (error == ENXIO) {
300	       return (error);
301	}
302
303	/* If we had some other problem. */
304	if (!(error == 0 || error == ENOENT)) {
305		return (error);
306	}
307
308	/* If we have the resources we need then we're good to go. */
309	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
310	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
311		return (0);
312	}
313
314	return (ENXIO);
315}
316
317static int
318ep_isa_attach (device_t dev)
319{
320	struct ep_softc *	sc = device_get_softc(dev);
321	int			error = 0;
322
323	if ((error = ep_alloc(dev))) {
324		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
325		goto bad;
326	}
327
328	ep_get_media(sc);
329
330	GO_WINDOW(0);
331	SET_IRQ(BASE, rman_get_start(sc->irq));
332
333	if ((error = ep_attach(sc))) {
334		device_printf(dev, "ep_attach() failed! (%d)\n", error);
335		goto bad;
336	}
337
338	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
339				   sc, &sc->ep_intrhand))) {
340		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
341		goto bad;
342	}
343
344	return (0);
345bad:
346	ep_free(dev);
347	return (error);
348}
349
350static device_method_t ep_isa_methods[] = {
351	/* Device interface */
352	DEVMETHOD(device_identify,	ep_isa_identify),
353	DEVMETHOD(device_probe,		ep_isa_probe),
354	DEVMETHOD(device_attach,	ep_isa_attach),
355
356	{ 0, 0 }
357};
358
359static driver_t ep_isa_driver = {
360	"ep",
361	ep_isa_methods,
362	sizeof(struct ep_softc),
363};
364
365extern devclass_t ep_devclass;
366
367DRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
368