if_ep_isa.c revision 63379
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 63379 2000-07-18 06:37:08Z 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
4951673Smdodd#include <machine/clock.h>
5051673Smdodd
5152549Smdodd#include <isa/isavar.h>
5252549Smdodd#include <isa/pnpvar.h>
5351673Smdodd
5451673Smdodd#include <dev/ep/if_epreg.h>
5551673Smdodd#include <dev/ep/if_epvar.h>
5651673Smdodd#include <i386/isa/elink.h>
5751673Smdodd
5854200Smdoddstatic u_int16_t	get_eeprom_data	(int, int);
5951673Smdodd
6054200Smdoddstatic void		ep_isa_identify	(driver_t *, device_t);
6154200Smdoddstatic int		ep_isa_probe	(device_t);
6254200Smdoddstatic int		ep_isa_attach	(device_t);
6351673Smdodd
6452549Smdoddstruct isa_ident {
6552549Smdodd	u_int32_t	id;
6652549Smdodd	char *		name;
6751673Smdodd};
6852549Smdoddconst char * ep_isa_match_id (u_int32_t, struct isa_ident *);
6951673Smdodd
7056428Smdodd#define ISA_ID_3C509_XXX   0x0506d509
7152549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7252549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7352549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
7452549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
7556428Smdodd#define ISA_ID_3C509_TPC   0x506d5098
7651673Smdodd
7752549Smdoddstatic struct isa_ident ep_isa_devs[] = {
7854197Smdodd	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
7954197Smdodd	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
8054197Smdodd	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
8154197Smdodd	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
8256428Smdodd	{ ISA_ID_3C509_TPC,	"3Com 3C509-TPC EtherLink III" },
8352549Smdodd	{ 0,			NULL },
8452549Smdodd};
8551673Smdodd
8652549Smdoddstatic struct isa_pnp_id ep_ids[] = {
8754197Smdodd	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
8854197Smdodd	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
8954197Smdodd	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
9054197Smdodd	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
9156428Smdodd	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
9254197Smdodd	{ 0xf780d041,	NULL }, /* PNP80f7 */
9354197Smdodd	{ 0,		NULL },
9452549Smdodd};
9551673Smdodd
9652549Smdodd/*
9752549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
9852549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
9952549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
10052549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
10152549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
10252549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
10352549Smdodd * compares the data on the bus; if there is a difference then that card goes
10452549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
10552549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
10652549Smdodd * read 16 times getting one bit of data with each read.
10752549Smdodd */
10851673Smdodd
10954198Smdoddstatic u_int16_t
11052549Smdoddget_eeprom_data(id_port, offset)
11154198Smdodd	int	id_port;
11254198Smdodd	int	offset;
11352549Smdodd{
11454198Smdodd	int		i;
11554198Smdodd	u_int16_t	data = 0;
11654198Smdodd
11754198Smdodd	outb(id_port, EEPROM_CMD_RD|offset);
11852549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
11954198Smdodd	for (i = 0; i < 16; i++) {
12054198Smdodd		DELAY(50);
12154198Smdodd		data = (data << 1) | (inw(id_port) & 1);
12254198Smdodd	}
12354198Smdodd	return (data);
12452549Smdodd}
12551673Smdodd
12652549Smdoddconst char *
12752549Smdoddep_isa_match_id (id, isa_devs)
12852549Smdodd	u_int32_t	      id;
12952549Smdodd	struct isa_ident *      isa_devs;
13052549Smdodd{
13152549Smdodd	struct isa_ident *      i = isa_devs;
13252549Smdodd	while(i->name != NULL) {
13352549Smdodd	       if (id == i->id)
13452549Smdodd		      return (i->name);
13552549Smdodd	       i++;
13652549Smdodd	}
13756428Smdodd	/*
13856428Smdodd	 * If we see a card that is likely to be a 3c509
13956428Smdodd	 * return something so that it will work; be annoying
14056428Smdodd	 * so that the user will tell us about it though.
14156428Smdodd	 */
14256429Smdodd	if ((id >> 4) == ISA_ID_3C509_XXX) {
14356428Smdodd		return ("Unknown 3c509; notify maintainer!");
14456428Smdodd	}
14552549Smdodd	return (NULL);
14652549Smdodd}
14751673Smdodd
14852549Smdoddstatic void
14952549Smdoddep_isa_identify (driver_t *driver, device_t parent)
15052549Smdodd{
15152549Smdodd	int		tag = EP_LAST_TAG;
15252549Smdodd	int		found = 0;
15352549Smdodd	int		i;
15452549Smdodd	int		j;
15552549Smdodd	const char *	desc;
15654196Smdodd	u_int16_t	data;
15752549Smdodd	u_int32_t	irq;
15852549Smdodd	u_int32_t	ioport;
15952549Smdodd	u_int32_t	isa_id;
16052549Smdodd	device_t	child;
16151673Smdodd
16252549Smdodd	outb(ELINK_ID_PORT, 0);
16352549Smdodd	outb(ELINK_ID_PORT, 0);
16452549Smdodd	elink_idseq(ELINK_509_POLY);
16552549Smdodd	elink_reset();
16651673Smdodd
16752549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
16851673Smdodd
16952549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
17051673Smdodd
17152549Smdodd		outb(ELINK_ID_PORT, 0);
17252549Smdodd		outb(ELINK_ID_PORT, 0);
17354196Smdodd		elink_idseq(ELINK_509_POLY);
17454196Smdodd		DELAY(400);
17551673Smdodd
17652549Smdodd		/* For the first probe, clear all
17752549Smdodd		 * board's tag registers.
17852549Smdodd		 * Otherwise kill off already-found
17952549Smdodd		 * boards. -- linux 3c509.c
18052549Smdodd		 */
18152549Smdodd		if (i == 0) {
18252549Smdodd			outb(ELINK_ID_PORT, 0xd0);
18352549Smdodd		} else {
18452549Smdodd			outb(ELINK_ID_PORT, 0xd8);
18552549Smdodd		}
18651673Smdodd
18754196Smdodd		/* Get out of loop if we're out of cards. */
18854196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
18954196Smdodd		if (data != MFG_ID) {
19054196Smdodd			break;
19154196Smdodd		}
19254196Smdodd
19354196Smdodd		/* resolve contention using the Ethernet address */
19454196Smdodd		for (j = 0; j < 3; j++) {
19554196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
19654196Smdodd		}
19754196Smdodd
19852549Smdodd		/*
19952549Smdodd		 * Construct an 'isa_id' in 'EISA'
20052549Smdodd		 * format.
20152549Smdodd		 */
20252549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
20352549Smdodd		isa_id = (htons(data) << 16);
20452549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
20552549Smdodd		isa_id |= htons(data);
20651673Smdodd
20752549Smdodd		/* Find known ISA boards */
20852549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
20952549Smdodd		if (!desc) {
21052549Smdodd			if (bootverbose) {
21152549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
21252549Smdodd						isa_id);
21352549Smdodd			}
21456429Smdodd			continue;
21552549Smdodd		}
21651673Smdodd
21752549Smdodd		/* Retreive IRQ */
21852549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
21952549Smdodd		irq = (data >> 12);
22051673Smdodd
22152549Smdodd		/* Retreive IOPORT */
22252549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
22352549Smdodd#ifdef PC98
22463379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
22552549Smdodd#else
22663379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
22752549Smdodd#endif
22851673Smdodd
22963379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
23063379Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
23163379Smdodd					desc, ioport);
23263379Smdodd			/* Set the adaptor tag so that the next card can be found. */
23363379Smdodd			outb(ELINK_ID_PORT, tag--);
23463379Smdodd			continue;
23563379Smdodd		}
23663379Smdodd
23754196Smdodd		/* Test for an adapter with PnP support. */
23854196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
23954196Smdodd		if (data == CAP_ISA) {
24054196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
24154196Smdodd			if (data & ICW1_IAS_PNP) {
24254196Smdodd				if (bootverbose) {
24356429Smdodd					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
24456429Smdodd					  	      desc, ioport);
24554196Smdodd				}
24654196Smdodd				/* Set the adaptor tag so that the next card can be found. */
24754196Smdodd				outb(ELINK_ID_PORT, tag--);
24854196Smdodd				continue;
24954196Smdodd			}
25054196Smdodd		}
25154196Smdodd
25252549Smdodd		/* Set the adaptor tag so that the next card can be found. */
25352549Smdodd		outb(ELINK_ID_PORT, tag--);
25451673Smdodd
25552549Smdodd		/* Activate the adaptor at the EEPROM location. */
25654196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
25751673Smdodd
25854196Smdodd		/* Test for an adapter in TEST mode. */
25954196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
26052549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
26152549Smdodd		if (data & EEPROM_TST_MODE) {
26256429Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
26356429Smdodd					desc, ioport);
26452549Smdodd			continue;
26552549Smdodd		}
26651673Smdodd
26752549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
26852549Smdodd		device_set_desc_copy(child, desc);
26952549Smdodd		device_set_driver(child, driver);
27052549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
27152549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
27251673Smdodd
27352549Smdodd		if (bootverbose) {
27452549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
27552549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
27652549Smdodd		}
27751673Smdodd
27852549Smdodd		found++;
27952549Smdodd	}
28051673Smdodd
28152549Smdodd	return;
28252549Smdodd}
28351673Smdodd
28452549Smdoddstatic int
28552549Smdoddep_isa_probe (device_t dev)
28652549Smdodd{
28752549Smdodd	int	error = 0;
28851673Smdodd
28952549Smdodd	/* Check isapnp ids */
29052549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
29151673Smdodd
29252549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
29352549Smdodd	if (error == ENXIO) {
29452549Smdodd	       return (error);
29552549Smdodd	}
29651673Smdodd
29752549Smdodd	/* If we had some other problem. */
29852549Smdodd	if (!(error == 0 || error == ENOENT)) {
29952549Smdodd		return (error);
30052549Smdodd	}
30151673Smdodd
30252549Smdodd	/* If we have the resources we need then we're good to go. */
30352549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
30452549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
30552549Smdodd		return (0);
30652549Smdodd	}
30751673Smdodd
30852549Smdodd	return (ENXIO);
30952549Smdodd}
31051673Smdodd
31152549Smdoddstatic int
31252549Smdoddep_isa_attach (device_t dev)
31352549Smdodd{
31452549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
31552549Smdodd	int			error = 0;
31651673Smdodd
31752549Smdodd	if ((error = ep_alloc(dev))) {
31852549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
31952549Smdodd		goto bad;
32052549Smdodd	}
32151673Smdodd
32252549Smdodd	ep_get_media(sc);
32351673Smdodd
32452549Smdodd	GO_WINDOW(0);
32552549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
32651673Smdodd
32752549Smdodd	if ((error = ep_attach(sc))) {
32852549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
32952549Smdodd		goto bad;
33052549Smdodd	}
33151673Smdodd
33252549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
33352549Smdodd				   sc, &sc->ep_intrhand))) {
33452549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
33552549Smdodd		goto bad;
33652549Smdodd	}
33751673Smdodd
33852549Smdodd	return (0);
33952549Smdoddbad:
34052549Smdodd	ep_free(dev);
34152549Smdodd	return (error);
34251673Smdodd}
34351673Smdodd
34452549Smdoddstatic device_method_t ep_isa_methods[] = {
34552549Smdodd	/* Device interface */
34652549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
34752549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
34852549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
34951673Smdodd
35052549Smdodd	{ 0, 0 }
35152549Smdodd};
35251673Smdodd
35352549Smdoddstatic driver_t ep_isa_driver = {
35452549Smdodd	"ep",
35552549Smdodd	ep_isa_methods,
35652549Smdodd	sizeof(struct ep_softc),
35752549Smdodd};
35851673Smdodd
35952549Smdoddextern devclass_t ep_devclass;
36051673Smdodd
36152549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
362