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