if_ep_isa.c revision 110835
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 110835 2003-02-13 23:01:59Z 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
5854200Smdoddstatic u_int16_t	get_eeprom_data	(int, int);
5951673Smdodd
60110835Smdodd#ifdef __i386__
6154200Smdoddstatic void		ep_isa_identify	(driver_t *, device_t);
62110835Smdodd#endif
6354200Smdoddstatic int		ep_isa_probe	(device_t);
6454200Smdoddstatic int		ep_isa_attach	(device_t);
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
8352549Smdoddstatic struct isa_ident ep_isa_devs[] = {
8454197Smdodd	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
8554197Smdodd	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
8654197Smdodd	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
8754197Smdodd	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
8856428Smdodd	{ ISA_ID_3C509_TPC,	"3Com 3C509-TPC EtherLink III" },
8969788Snyan#ifdef PC98
9069788Snyan	{ ISA_ID_3C569B_COMBO,	"3Com 3C569B-J-Combo EtherLink III" },
9169788Snyan	{ ISA_ID_3C569B_TPO,	"3Com 3C569B-J-TPO EtherLink III" },
9269788Snyan#endif
9352549Smdodd	{ 0,			NULL },
9452549Smdodd};
9551673Smdodd
9652549Smdoddstatic struct isa_pnp_id ep_ids[] = {
9754197Smdodd	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
9854197Smdodd	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
9954197Smdodd	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
10054197Smdodd	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
10156428Smdodd	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
10254197Smdodd	{ 0xf780d041,	NULL }, /* PNP80f7 */
10354197Smdodd	{ 0,		NULL },
10452549Smdodd};
10551673Smdodd
10652549Smdodd/*
10752549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
10852549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
10952549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
11052549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
11152549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
11252549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
11352549Smdodd * compares the data on the bus; if there is a difference then that card goes
11452549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
11552549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
11652549Smdodd * read 16 times getting one bit of data with each read.
11752549Smdodd */
11851673Smdodd
11954198Smdoddstatic u_int16_t
12052549Smdoddget_eeprom_data(id_port, offset)
12154198Smdodd	int	id_port;
12254198Smdodd	int	offset;
12352549Smdodd{
12454198Smdodd	int		i;
12554198Smdodd	u_int16_t	data = 0;
12654198Smdodd
12754198Smdodd	outb(id_port, EEPROM_CMD_RD|offset);
12852549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
12954198Smdodd	for (i = 0; i < 16; i++) {
13054198Smdodd		DELAY(50);
13154198Smdodd		data = (data << 1) | (inw(id_port) & 1);
13254198Smdodd	}
13354198Smdodd	return (data);
13452549Smdodd}
13551673Smdodd
13652549Smdoddconst char *
13752549Smdoddep_isa_match_id (id, isa_devs)
13852549Smdodd	u_int32_t	      id;
13952549Smdodd	struct isa_ident *      isa_devs;
14052549Smdodd{
14152549Smdodd	struct isa_ident *      i = isa_devs;
14252549Smdodd	while(i->name != NULL) {
14352549Smdodd	       if (id == i->id)
14452549Smdodd		      return (i->name);
14552549Smdodd	       i++;
14652549Smdodd	}
14756428Smdodd	/*
14856428Smdodd	 * If we see a card that is likely to be a 3c509
14956428Smdodd	 * return something so that it will work; be annoying
15056428Smdodd	 * so that the user will tell us about it though.
15156428Smdodd	 */
15256429Smdodd	if ((id >> 4) == ISA_ID_3C509_XXX) {
15356428Smdodd		return ("Unknown 3c509; notify maintainer!");
15456428Smdodd	}
15552549Smdodd	return (NULL);
15652549Smdodd}
15751673Smdodd
158110835Smdodd#ifdef __i386__
15952549Smdoddstatic void
16052549Smdoddep_isa_identify (driver_t *driver, device_t parent)
16152549Smdodd{
16252549Smdodd	int		tag = EP_LAST_TAG;
16352549Smdodd	int		found = 0;
16452549Smdodd	int		i;
16552549Smdodd	int		j;
16652549Smdodd	const char *	desc;
16754196Smdodd	u_int16_t	data;
16852549Smdodd	u_int32_t	irq;
16952549Smdodd	u_int32_t	ioport;
17052549Smdodd	u_int32_t	isa_id;
17152549Smdodd	device_t	child;
17251673Smdodd
17352549Smdodd	outb(ELINK_ID_PORT, 0);
17452549Smdodd	outb(ELINK_ID_PORT, 0);
17552549Smdodd	elink_idseq(ELINK_509_POLY);
17652549Smdodd	elink_reset();
17751673Smdodd
17852549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
17951673Smdodd
18052549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
18151673Smdodd
18252549Smdodd		outb(ELINK_ID_PORT, 0);
18352549Smdodd		outb(ELINK_ID_PORT, 0);
18454196Smdodd		elink_idseq(ELINK_509_POLY);
18554196Smdodd		DELAY(400);
18651673Smdodd
18752549Smdodd		/* For the first probe, clear all
18852549Smdodd		 * board's tag registers.
18952549Smdodd		 * Otherwise kill off already-found
19052549Smdodd		 * boards. -- linux 3c509.c
19152549Smdodd		 */
19252549Smdodd		if (i == 0) {
19352549Smdodd			outb(ELINK_ID_PORT, 0xd0);
19452549Smdodd		} else {
19552549Smdodd			outb(ELINK_ID_PORT, 0xd8);
19652549Smdodd		}
19751673Smdodd
19854196Smdodd		/* Get out of loop if we're out of cards. */
19954196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
20054196Smdodd		if (data != MFG_ID) {
20154196Smdodd			break;
20254196Smdodd		}
20354196Smdodd
20454196Smdodd		/* resolve contention using the Ethernet address */
20554196Smdodd		for (j = 0; j < 3; j++) {
20654196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
20754196Smdodd		}
20854196Smdodd
20952549Smdodd		/*
21052549Smdodd		 * Construct an 'isa_id' in 'EISA'
21152549Smdodd		 * format.
21252549Smdodd		 */
21352549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
21452549Smdodd		isa_id = (htons(data) << 16);
21552549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
21652549Smdodd		isa_id |= htons(data);
21751673Smdodd
21852549Smdodd		/* Find known ISA boards */
21952549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
22052549Smdodd		if (!desc) {
22152549Smdodd			if (bootverbose) {
22252549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
22352549Smdodd						isa_id);
22452549Smdodd			}
22556429Smdodd			continue;
22652549Smdodd		}
22751673Smdodd
22852549Smdodd		/* Retreive IRQ */
22952549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
23052549Smdodd		irq = (data >> 12);
23151673Smdodd
23252549Smdodd		/* Retreive IOPORT */
23352549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
23452549Smdodd#ifdef PC98
23563379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
23652549Smdodd#else
23763379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
23852549Smdodd#endif
23951673Smdodd
24063379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
24163379Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
24263379Smdodd					desc, ioport);
24363379Smdodd			/* Set the adaptor tag so that the next card can be found. */
24463379Smdodd			outb(ELINK_ID_PORT, tag--);
24563379Smdodd			continue;
24663379Smdodd		}
24763379Smdodd
24854196Smdodd		/* Test for an adapter with PnP support. */
24954196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
25054196Smdodd		if (data == CAP_ISA) {
25154196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
25254196Smdodd			if (data & ICW1_IAS_PNP) {
25354196Smdodd				if (bootverbose) {
25456429Smdodd					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
25556429Smdodd					  	      desc, ioport);
25654196Smdodd				}
25754196Smdodd				/* Set the adaptor tag so that the next card can be found. */
25854196Smdodd				outb(ELINK_ID_PORT, tag--);
25954196Smdodd				continue;
26054196Smdodd			}
26154196Smdodd		}
26254196Smdodd
26352549Smdodd		/* Set the adaptor tag so that the next card can be found. */
26452549Smdodd		outb(ELINK_ID_PORT, tag--);
26551673Smdodd
26652549Smdodd		/* Activate the adaptor at the EEPROM location. */
26754196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
26851673Smdodd
26954196Smdodd		/* Test for an adapter in TEST mode. */
27054196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
27152549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
27252549Smdodd		if (data & EEPROM_TST_MODE) {
27356429Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
27456429Smdodd					desc, ioport);
27552549Smdodd			continue;
27652549Smdodd		}
27751673Smdodd
27852549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
27952549Smdodd		device_set_desc_copy(child, desc);
28052549Smdodd		device_set_driver(child, driver);
28152549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
28252549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
28351673Smdodd
28452549Smdodd		if (bootverbose) {
28552549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
28652549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
28752549Smdodd		}
28851673Smdodd
28952549Smdodd		found++;
29052549Smdodd	}
29151673Smdodd
29252549Smdodd	return;
29352549Smdodd}
294110835Smdodd#endif
29551673Smdodd
29652549Smdoddstatic int
29752549Smdoddep_isa_probe (device_t dev)
29852549Smdodd{
29952549Smdodd	int	error = 0;
30051673Smdodd
30152549Smdodd	/* Check isapnp ids */
30252549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
30351673Smdodd
30452549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
30552549Smdodd	if (error == ENXIO) {
30652549Smdodd	       return (error);
30752549Smdodd	}
30851673Smdodd
30952549Smdodd	/* If we had some other problem. */
31052549Smdodd	if (!(error == 0 || error == ENOENT)) {
31152549Smdodd		return (error);
31252549Smdodd	}
31351673Smdodd
31452549Smdodd	/* If we have the resources we need then we're good to go. */
31552549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
31652549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
31752549Smdodd		return (0);
31852549Smdodd	}
31951673Smdodd
32052549Smdodd	return (ENXIO);
32152549Smdodd}
32251673Smdodd
32352549Smdoddstatic int
32452549Smdoddep_isa_attach (device_t dev)
32552549Smdodd{
32652549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
32752549Smdodd	int			error = 0;
32851673Smdodd
32952549Smdodd	if ((error = ep_alloc(dev))) {
33052549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
33152549Smdodd		goto bad;
33252549Smdodd	}
33351673Smdodd
33452549Smdodd	ep_get_media(sc);
33551673Smdodd
33652549Smdodd	GO_WINDOW(0);
33752549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
33851673Smdodd
33952549Smdodd	if ((error = ep_attach(sc))) {
34052549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
34152549Smdodd		goto bad;
34252549Smdodd	}
34351673Smdodd
34452549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
34552549Smdodd				   sc, &sc->ep_intrhand))) {
34652549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
34752549Smdodd		goto bad;
34852549Smdodd	}
34951673Smdodd
35052549Smdodd	return (0);
35152549Smdoddbad:
35252549Smdodd	ep_free(dev);
35352549Smdodd	return (error);
35451673Smdodd}
35551673Smdodd
35652549Smdoddstatic device_method_t ep_isa_methods[] = {
35752549Smdodd	/* Device interface */
358110835Smdodd#ifdef __i386__
35952549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
360110835Smdodd#endif
36152549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
36252549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
36351673Smdodd
36452549Smdodd	{ 0, 0 }
36552549Smdodd};
36651673Smdodd
36752549Smdoddstatic driver_t ep_isa_driver = {
36852549Smdodd	"ep",
36952549Smdodd	ep_isa_methods,
37052549Smdodd	sizeof(struct ep_softc),
37152549Smdodd};
37251673Smdodd
37352549Smdoddextern devclass_t ep_devclass;
37451673Smdodd
37552549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
376