if_ep_isa.c revision 112824
151673Smdodd/*
251673Smdodd * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
351673Smdodd * All rights reserved.
451673Smdodd *
551673Smdodd * Redistribution and use in source and binary forms, with or without
651673Smdodd * modification, are permitted provided that the following conditions
751673Smdodd * are met:
851673Smdodd * 1. Redistributions of source code must retain the above copyright
951673Smdodd *    notice, this list of conditions and the following disclaimer.
1051673Smdodd * 2. Redistributions in binary form must reproduce the above copyright
1151673Smdodd *    notice, this list of conditions and the following disclaimer in the
1251673Smdodd *    documentation and/or other materials provided with the distribution.
1351673Smdodd * 3. All advertising materials mentioning features or use of this software
1451673Smdodd *    must display the following acknowledgement:
1551673Smdodd *      This product includes software developed by Herb Peyerl.
1651673Smdodd * 4. The name of Herb Peyerl may not be used to endorse or promote products
1751673Smdodd *    derived from this software without specific prior written permission.
1851673Smdodd *
1951673Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2051673Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2151673Smdodd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2251673Smdodd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2351673Smdodd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2451673Smdodd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2551673Smdodd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2651673Smdodd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2751673Smdodd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2851673Smdodd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2951673Smdodd *
3051673Smdodd * $FreeBSD: head/sys/dev/ep/if_ep_isa.c 112824 2003-03-29 21:57:32Z mdodd $
3151673Smdodd */
3251673Smdodd
3351673Smdodd#include <sys/param.h>
3452549Smdodd#include <sys/systm.h>
3551673Smdodd#include <sys/kernel.h>
3651673Smdodd#include <sys/socket.h>
3751673Smdodd
3852549Smdodd#include <sys/module.h>
3952549Smdodd#include <sys/bus.h>
4052549Smdodd
4152549Smdodd#include <machine/bus.h>
4252549Smdodd#include <machine/resource.h>
4352549Smdodd#include <sys/rman.h>
4452549Smdodd
4551673Smdodd#include <net/if.h>
4652549Smdodd#include <net/if_arp.h>
4752549Smdodd#include <net/if_media.h>
4851673Smdodd
4952549Smdodd#include <isa/isavar.h>
5051673Smdodd
5151673Smdodd#include <dev/ep/if_epreg.h>
5251673Smdodd#include <dev/ep/if_epvar.h>
53110835Smdodd
54110835Smdodd#ifdef __i386__
5551673Smdodd#include <i386/isa/elink.h>
56110835Smdodd#endif
5751673Smdodd
58111292Smarcel#ifdef __i386__
5954200Smdoddstatic u_int16_t	get_eeprom_data	(int, int);
6054200Smdoddstatic void		ep_isa_identify	(driver_t *, device_t);
61110835Smdodd#endif
6254200Smdoddstatic int		ep_isa_probe	(device_t);
6354200Smdoddstatic int		ep_isa_attach	(device_t);
64112824Smdoddstatic int		ep_eeprom_cksum (struct ep_softc *);
6551673Smdodd
6652549Smdoddstruct isa_ident {
6752549Smdodd	u_int32_t	id;
6852549Smdodd	char *		name;
6951673Smdodd};
7052549Smdoddconst char * ep_isa_match_id (u_int32_t, struct isa_ident *);
7151673Smdodd
7256428Smdodd#define ISA_ID_3C509_XXX   0x0506d509
7352549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7452549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7552549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
7652549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
7756428Smdodd#define ISA_ID_3C509_TPC   0x506d5098
7869788Snyan#ifdef PC98
7969788Snyan#define ISA_ID_3C569B_COMBO 0x506d5694
8069788Snyan#define ISA_ID_3C569B_TPO   0x506d5695
8169788Snyan#endif
8251673Smdodd
83111292Smarcel#ifdef __i386__
8452549Smdoddstatic struct isa_ident ep_isa_devs[] = {
8554197Smdodd	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
8654197Smdodd	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
8754197Smdodd	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
8854197Smdodd	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
8956428Smdodd	{ ISA_ID_3C509_TPC,	"3Com 3C509-TPC EtherLink III" },
9069788Snyan#ifdef PC98
9169788Snyan	{ ISA_ID_3C569B_COMBO,	"3Com 3C569B-J-Combo EtherLink III" },
9269788Snyan	{ ISA_ID_3C569B_TPO,	"3Com 3C569B-J-TPO EtherLink III" },
9369788Snyan#endif
9452549Smdodd	{ 0,			NULL },
9552549Smdodd};
96111292Smarcel#endif
9751673Smdodd
9852549Smdoddstatic struct isa_pnp_id ep_ids[] = {
9954197Smdodd	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
10054197Smdodd	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
10154197Smdodd	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
10254197Smdodd	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
10356428Smdodd	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
10454197Smdodd	{ 0xf780d041,	NULL }, /* PNP80f7 */
10554197Smdodd	{ 0,		NULL },
10652549Smdodd};
10751673Smdodd
10852549Smdodd/*
10952549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
11052549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
11152549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
11252549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
11352549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
11452549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
11552549Smdodd * compares the data on the bus; if there is a difference then that card goes
11652549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
11752549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
11852549Smdodd * read 16 times getting one bit of data with each read.
11952549Smdodd */
120111292Smarcel#ifdef __i386__
12154198Smdoddstatic u_int16_t
12252549Smdoddget_eeprom_data(id_port, offset)
12354198Smdodd	int	id_port;
12454198Smdodd	int	offset;
12552549Smdodd{
12654198Smdodd	int		i;
12754198Smdodd	u_int16_t	data = 0;
12854198Smdodd
12954198Smdodd	outb(id_port, EEPROM_CMD_RD|offset);
13052549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
13154198Smdodd	for (i = 0; i < 16; i++) {
13254198Smdodd		DELAY(50);
13354198Smdodd		data = (data << 1) | (inw(id_port) & 1);
13454198Smdodd	}
13554198Smdodd	return (data);
13652549Smdodd}
137111292Smarcel#endif
13851673Smdodd
13952549Smdoddconst char *
14052549Smdoddep_isa_match_id (id, isa_devs)
14152549Smdodd	u_int32_t	      id;
14252549Smdodd	struct isa_ident *      isa_devs;
14352549Smdodd{
14452549Smdodd	struct isa_ident *      i = isa_devs;
14552549Smdodd	while(i->name != NULL) {
14652549Smdodd	       if (id == i->id)
14752549Smdodd		      return (i->name);
14852549Smdodd	       i++;
14952549Smdodd	}
15056428Smdodd	/*
15156428Smdodd	 * If we see a card that is likely to be a 3c509
15256428Smdodd	 * return something so that it will work; be annoying
15356428Smdodd	 * so that the user will tell us about it though.
15456428Smdodd	 */
15556429Smdodd	if ((id >> 4) == ISA_ID_3C509_XXX) {
15656428Smdodd		return ("Unknown 3c509; notify maintainer!");
15756428Smdodd	}
15852549Smdodd	return (NULL);
15952549Smdodd}
16051673Smdodd
161110835Smdodd#ifdef __i386__
16252549Smdoddstatic void
16352549Smdoddep_isa_identify (driver_t *driver, device_t parent)
16452549Smdodd{
16552549Smdodd	int		tag = EP_LAST_TAG;
16652549Smdodd	int		found = 0;
16752549Smdodd	int		i;
16852549Smdodd	int		j;
16952549Smdodd	const char *	desc;
17054196Smdodd	u_int16_t	data;
17152549Smdodd	u_int32_t	irq;
17252549Smdodd	u_int32_t	ioport;
17352549Smdodd	u_int32_t	isa_id;
17452549Smdodd	device_t	child;
17551673Smdodd
17652549Smdodd	outb(ELINK_ID_PORT, 0);
17752549Smdodd	outb(ELINK_ID_PORT, 0);
17852549Smdodd	elink_idseq(ELINK_509_POLY);
17952549Smdodd	elink_reset();
18051673Smdodd
18152549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
18251673Smdodd
18352549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
18451673Smdodd
18552549Smdodd		outb(ELINK_ID_PORT, 0);
18652549Smdodd		outb(ELINK_ID_PORT, 0);
18754196Smdodd		elink_idseq(ELINK_509_POLY);
18854196Smdodd		DELAY(400);
18951673Smdodd
19052549Smdodd		/* For the first probe, clear all
19152549Smdodd		 * board's tag registers.
19252549Smdodd		 * Otherwise kill off already-found
19352549Smdodd		 * boards. -- linux 3c509.c
19452549Smdodd		 */
19552549Smdodd		if (i == 0) {
19652549Smdodd			outb(ELINK_ID_PORT, 0xd0);
19752549Smdodd		} else {
19852549Smdodd			outb(ELINK_ID_PORT, 0xd8);
19952549Smdodd		}
20051673Smdodd
20154196Smdodd		/* Get out of loop if we're out of cards. */
20254196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
20354196Smdodd		if (data != MFG_ID) {
20454196Smdodd			break;
20554196Smdodd		}
20654196Smdodd
20754196Smdodd		/* resolve contention using the Ethernet address */
20854196Smdodd		for (j = 0; j < 3; j++) {
20954196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
21054196Smdodd		}
21154196Smdodd
21252549Smdodd		/*
21352549Smdodd		 * Construct an 'isa_id' in 'EISA'
21452549Smdodd		 * format.
21552549Smdodd		 */
21652549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
21752549Smdodd		isa_id = (htons(data) << 16);
21852549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
21952549Smdodd		isa_id |= htons(data);
22051673Smdodd
22152549Smdodd		/* Find known ISA boards */
22252549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
22352549Smdodd		if (!desc) {
22452549Smdodd			if (bootverbose) {
22552549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
22652549Smdodd						isa_id);
22752549Smdodd			}
22856429Smdodd			continue;
22952549Smdodd		}
23051673Smdodd
23152549Smdodd		/* Retreive IRQ */
23252549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
23352549Smdodd		irq = (data >> 12);
23451673Smdodd
23552549Smdodd		/* Retreive IOPORT */
23652549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
23752549Smdodd#ifdef PC98
23863379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
23952549Smdodd#else
24063379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
24152549Smdodd#endif
24251673Smdodd
24363379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
24463379Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
24563379Smdodd					desc, ioport);
24663379Smdodd			/* Set the adaptor tag so that the next card can be found. */
24763379Smdodd			outb(ELINK_ID_PORT, tag--);
24863379Smdodd			continue;
24963379Smdodd		}
25063379Smdodd
25154196Smdodd		/* Test for an adapter with PnP support. */
25254196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
25354196Smdodd		if (data == CAP_ISA) {
25454196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
25554196Smdodd			if (data & ICW1_IAS_PNP) {
25654196Smdodd				if (bootverbose) {
25756429Smdodd					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
25856429Smdodd					  	      desc, ioport);
25954196Smdodd				}
26054196Smdodd				/* Set the adaptor tag so that the next card can be found. */
26154196Smdodd				outb(ELINK_ID_PORT, tag--);
26254196Smdodd				continue;
26354196Smdodd			}
26454196Smdodd		}
26554196Smdodd
26652549Smdodd		/* Set the adaptor tag so that the next card can be found. */
26752549Smdodd		outb(ELINK_ID_PORT, tag--);
26851673Smdodd
26952549Smdodd		/* Activate the adaptor at the EEPROM location. */
27054196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
27151673Smdodd
27254196Smdodd		/* Test for an adapter in TEST mode. */
27354196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
27452549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
27552549Smdodd		if (data & EEPROM_TST_MODE) {
27656429Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
27756429Smdodd					desc, ioport);
27852549Smdodd			continue;
27952549Smdodd		}
28051673Smdodd
28152549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
28252549Smdodd		device_set_desc_copy(child, desc);
28352549Smdodd		device_set_driver(child, driver);
28452549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
28552549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
28651673Smdodd
28752549Smdodd		if (bootverbose) {
28852549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
28952549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
29052549Smdodd		}
29151673Smdodd
29252549Smdodd		found++;
29352549Smdodd	}
29451673Smdodd
29552549Smdodd	return;
29652549Smdodd}
297110835Smdodd#endif
29851673Smdodd
29952549Smdoddstatic int
30052549Smdoddep_isa_probe (device_t dev)
30152549Smdodd{
30252549Smdodd	int	error = 0;
30351673Smdodd
30452549Smdodd	/* Check isapnp ids */
30552549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
30651673Smdodd
30752549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
30852549Smdodd	if (error == ENXIO) {
30952549Smdodd	       return (error);
31052549Smdodd	}
31151673Smdodd
31252549Smdodd	/* If we had some other problem. */
31352549Smdodd	if (!(error == 0 || error == ENOENT)) {
31452549Smdodd		return (error);
31552549Smdodd	}
31651673Smdodd
31752549Smdodd	/* If we have the resources we need then we're good to go. */
31852549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
31952549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
32052549Smdodd		return (0);
32152549Smdodd	}
32251673Smdodd
32352549Smdodd	return (ENXIO);
32452549Smdodd}
32551673Smdodd
32652549Smdoddstatic int
32752549Smdoddep_isa_attach (device_t dev)
32852549Smdodd{
32952549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
33052549Smdodd	int			error = 0;
33151673Smdodd
33252549Smdodd	if ((error = ep_alloc(dev))) {
33352549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
33452549Smdodd		goto bad;
33552549Smdodd	}
33651673Smdodd
33752549Smdodd	ep_get_media(sc);
33851673Smdodd
33952549Smdodd	GO_WINDOW(0);
34052549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
34151673Smdodd
34252549Smdodd	if ((error = ep_attach(sc))) {
34352549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
34452549Smdodd		goto bad;
34552549Smdodd	}
34651673Smdodd
347112824Smdodd	error = ep_eeprom_cksum(sc);
348112824Smdodd	if (error) {
349112824Smdodd		device_printf(sc->dev, "Invalid EEPROM checksum!\n");
350112824Smdodd		goto bad;
351112824Smdodd	}
352112824Smdodd
35352549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
35452549Smdodd				   sc, &sc->ep_intrhand))) {
35552549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
35652549Smdodd		goto bad;
35752549Smdodd	}
35851673Smdodd
35952549Smdodd	return (0);
36052549Smdoddbad:
36152549Smdodd	ep_free(dev);
36252549Smdodd	return (error);
36351673Smdodd}
36451673Smdodd
365112824Smdoddstatic int
366112824Smdoddep_eeprom_cksum (sc)
367112824Smdodd	struct ep_softc *	sc;
368112824Smdodd{
369112824Smdodd	int                     i;
370112824Smdodd	int                     error;
371112824Smdodd	u_int16_t               val;
372112824Smdodd	u_int16_t               cksum;
373112824Smdodd	u_int8_t                cksum_high = 0;
374112824Smdodd	u_int8_t                cksum_low = 0;
375112824Smdodd
376112824Smdodd	error = get_e(sc, 0x0f, &val);
377112824Smdodd	if (error)
378112824Smdodd	       return (ENXIO);
379112824Smdodd	cksum = val;
380112824Smdodd
381112824Smdodd	for (i = 0; i < 0x0f; i++) {
382112824Smdodd		error = get_e(sc, i, &val);
383112824Smdodd		if (error)
384112824Smdodd			return (ENXIO);
385112824Smdodd		switch (i) {
386112824Smdodd			case 0x08:
387112824Smdodd			case 0x09:
388112824Smdodd			case 0x0d:
389112824Smdodd				cksum_low ^= (u_int8_t)(val & 0x00ff) ^
390112824Smdodd					     (u_int8_t)((val & 0xff00) >> 8);
391112824Smdodd				break;
392112824Smdodd			default:
393112824Smdodd				cksum_high ^= (u_int8_t)(val & 0x00ff) ^
394112824Smdodd					      (u_int8_t)((val & 0xff00) >> 8);
395112824Smdodd				break;
396112824Smdodd		}
397112824Smdodd	}
398112824Smdodd	return (cksum != ((u_int16_t)cksum_low | (u_int16_t)(cksum_high << 8)));
399112824Smdodd}
400112824Smdodd
40152549Smdoddstatic device_method_t ep_isa_methods[] = {
40252549Smdodd	/* Device interface */
403110835Smdodd#ifdef __i386__
40452549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
405110835Smdodd#endif
40652549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
40752549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
40851673Smdodd
40952549Smdodd	{ 0, 0 }
41052549Smdodd};
41151673Smdodd
41252549Smdoddstatic driver_t ep_isa_driver = {
41352549Smdodd	"ep",
41452549Smdodd	ep_isa_methods,
41552549Smdodd	sizeof(struct ep_softc),
41652549Smdodd};
41751673Smdodd
41852549Smdoddextern devclass_t ep_devclass;
41951673Smdodd
42052549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
421112794Smdodd#ifdef __i386__
422112794SmdoddMODULE_DEPEND(ep, elink, 1, 1, 1);
423112794Smdodd#endif
424