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