if_ep_isa.c revision 54198
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 54198 1999-12-06 08:59:52Z 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
5852549Smdoddstatic int	get_eeprom_data	(int, int);
5951673Smdodd
6052549Smdoddstatic void	ep_isa_identify	(driver_t *, device_t);
6152549Smdoddstatic int	ep_isa_probe	(device_t);
6252549Smdoddstatic 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
7052549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7152549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7252549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
7352549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
7451673Smdodd
7552549Smdoddstatic struct isa_ident ep_isa_devs[] = {
7654197Smdodd	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
7754197Smdodd	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
7854197Smdodd	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
7954197Smdodd	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
8052549Smdodd	{ 0,			NULL },
8152549Smdodd};
8251673Smdodd
8352549Smdoddstatic struct isa_pnp_id ep_ids[] = {
8454197Smdodd	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
8554197Smdodd	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
8654197Smdodd	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
8754197Smdodd	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
8854197Smdodd	{ 0xf780d041,	NULL }, /* PNP80f7 */
8954197Smdodd	{ 0,		NULL },
9052549Smdodd};
9151673Smdodd
9252549Smdodd/*
9352549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
9452549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
9552549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
9652549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
9752549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
9852549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
9952549Smdodd * compares the data on the bus; if there is a difference then that card goes
10052549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
10152549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
10252549Smdodd * read 16 times getting one bit of data with each read.
10352549Smdodd */
10451673Smdodd
10554198Smdoddstatic u_int16_t
10652549Smdoddget_eeprom_data(id_port, offset)
10754198Smdodd	int	id_port;
10854198Smdodd	int	offset;
10952549Smdodd{
11054198Smdodd	int		i;
11154198Smdodd	u_int16_t	data = 0;
11254198Smdodd
11354198Smdodd	outb(id_port, EEPROM_CMD_RD|offset);
11452549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
11554198Smdodd	for (i = 0; i < 16; i++) {
11654198Smdodd		DELAY(50);
11754198Smdodd		data = (data << 1) | (inw(id_port) & 1);
11854198Smdodd	}
11954198Smdodd	return (data);
12052549Smdodd}
12151673Smdodd
12252549Smdoddconst char *
12352549Smdoddep_isa_match_id (id, isa_devs)
12452549Smdodd	u_int32_t	      id;
12552549Smdodd	struct isa_ident *      isa_devs;
12652549Smdodd{
12752549Smdodd	struct isa_ident *      i = isa_devs;
12852549Smdodd	while(i->name != NULL) {
12952549Smdodd	       if (id == i->id)
13052549Smdodd		      return (i->name);
13152549Smdodd	       i++;
13252549Smdodd	}
13352549Smdodd	return (NULL);
13452549Smdodd}
13551673Smdodd
13652549Smdoddstatic void
13752549Smdoddep_isa_identify (driver_t *driver, device_t parent)
13852549Smdodd{
13952549Smdodd	int		tag = EP_LAST_TAG;
14052549Smdodd	int		found = 0;
14152549Smdodd	int		i;
14252549Smdodd	int		j;
14352549Smdodd	const char *	desc;
14454196Smdodd	u_int16_t	data;
14552549Smdodd	u_int32_t	irq;
14652549Smdodd	u_int32_t	ioport;
14752549Smdodd	u_int32_t	isa_id;
14852549Smdodd	device_t	child;
14951673Smdodd
15052549Smdodd	outb(ELINK_ID_PORT, 0);
15152549Smdodd	outb(ELINK_ID_PORT, 0);
15252549Smdodd	elink_idseq(ELINK_509_POLY);
15352549Smdodd	elink_reset();
15451673Smdodd
15552549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
15651673Smdodd
15752549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
15851673Smdodd
15952549Smdodd		outb(ELINK_ID_PORT, 0);
16052549Smdodd		outb(ELINK_ID_PORT, 0);
16154196Smdodd		elink_idseq(ELINK_509_POLY);
16254196Smdodd		DELAY(400);
16351673Smdodd
16452549Smdodd		/* For the first probe, clear all
16552549Smdodd		 * board's tag registers.
16652549Smdodd		 * Otherwise kill off already-found
16752549Smdodd		 * boards. -- linux 3c509.c
16852549Smdodd		 */
16952549Smdodd		if (i == 0) {
17052549Smdodd			outb(ELINK_ID_PORT, 0xd0);
17152549Smdodd		} else {
17252549Smdodd			outb(ELINK_ID_PORT, 0xd8);
17352549Smdodd		}
17451673Smdodd
17554196Smdodd		/* Get out of loop if we're out of cards. */
17654196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
17754196Smdodd		if (data != MFG_ID) {
17854196Smdodd			break;
17954196Smdodd		}
18054196Smdodd
18154196Smdodd		/* resolve contention using the Ethernet address */
18254196Smdodd		for (j = 0; j < 3; j++) {
18354196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
18454196Smdodd		}
18554196Smdodd
18652549Smdodd		/*
18752549Smdodd		 * Construct an 'isa_id' in 'EISA'
18852549Smdodd		 * format.
18952549Smdodd		 */
19052549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
19152549Smdodd		isa_id = (htons(data) << 16);
19252549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
19352549Smdodd		isa_id |= htons(data);
19451673Smdodd
19552549Smdodd		/* Find known ISA boards */
19652549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
19752549Smdodd		if (!desc) {
19852549Smdodd			if (bootverbose) {
19952549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
20052549Smdodd						isa_id);
20152549Smdodd			}
20252549Smdodd			break;
20352549Smdodd		}
20451673Smdodd
20552549Smdodd		/* Retreive IRQ */
20652549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
20752549Smdodd		irq = (data >> 12);
20851673Smdodd
20952549Smdodd		/* Retreive IOPORT */
21052549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
21152549Smdodd#ifdef PC98
21254196Smdodd		ioport = (((data & 0x1f) * 0x100) + 0x40d0);
21352549Smdodd#else
21454196Smdodd		ioport = (((data & 0x1f) << 4) + 0x200);
21552549Smdodd#endif
21651673Smdodd
21754196Smdodd		/* Test for an adapter with PnP support. */
21854196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
21954196Smdodd		if (data == CAP_ISA) {
22054196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
22154196Smdodd			if (data & ICW1_IAS_PNP) {
22254196Smdodd				if (bootverbose) {
22354196Smdodd					device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n",
22454196Smdodd					  	      ioport);
22554196Smdodd				}
22654196Smdodd				/* Set the adaptor tag so that the next card can be found. */
22754196Smdodd				outb(ELINK_ID_PORT, tag--);
22854196Smdodd				continue;
22954196Smdodd			}
23054196Smdodd		}
23154196Smdodd
23252549Smdodd		/* Set the adaptor tag so that the next card can be found. */
23352549Smdodd		outb(ELINK_ID_PORT, tag--);
23451673Smdodd
23552549Smdodd		/* Activate the adaptor at the EEPROM location. */
23654196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
23751673Smdodd
23854196Smdodd		/* Test for an adapter in TEST mode. */
23954196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
24052549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
24152549Smdodd		if (data & EEPROM_TST_MODE) {
24254196Smdodd			device_printf(parent, "if_ep: Adapter at 0x%03x in TEST mode!  Erase pencil mark.\n",
24354196Smdodd			  	      ioport);
24452549Smdodd			continue;
24552549Smdodd		}
24651673Smdodd
24752549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
24852549Smdodd		device_set_desc_copy(child, desc);
24952549Smdodd		device_set_driver(child, driver);
25052549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
25152549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
25251673Smdodd
25352549Smdodd		if (bootverbose) {
25452549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
25552549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
25652549Smdodd		}
25751673Smdodd
25852549Smdodd		found++;
25952549Smdodd	}
26051673Smdodd
26152549Smdodd	return;
26252549Smdodd}
26351673Smdodd
26452549Smdoddstatic int
26552549Smdoddep_isa_probe (device_t dev)
26652549Smdodd{
26752549Smdodd	int	error = 0;
26851673Smdodd
26952549Smdodd	/* Check isapnp ids */
27052549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
27151673Smdodd
27252549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
27352549Smdodd	if (error == ENXIO) {
27452549Smdodd	       return (error);
27552549Smdodd	}
27651673Smdodd
27752549Smdodd	/* If we had some other problem. */
27852549Smdodd	if (!(error == 0 || error == ENOENT)) {
27952549Smdodd		return (error);
28052549Smdodd	}
28151673Smdodd
28252549Smdodd	/* If we have the resources we need then we're good to go. */
28352549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
28452549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
28552549Smdodd		return (0);
28652549Smdodd	}
28751673Smdodd
28852549Smdodd	return (ENXIO);
28952549Smdodd}
29051673Smdodd
29152549Smdoddstatic int
29252549Smdoddep_isa_attach (device_t dev)
29352549Smdodd{
29452549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
29552549Smdodd	int			error = 0;
29651673Smdodd
29752549Smdodd	if ((error = ep_alloc(dev))) {
29852549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
29952549Smdodd		goto bad;
30052549Smdodd	}
30151673Smdodd
30252549Smdodd	ep_get_media(sc);
30351673Smdodd
30452549Smdodd	GO_WINDOW(0);
30552549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
30651673Smdodd
30752549Smdodd	if ((error = ep_attach(sc))) {
30852549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
30952549Smdodd		goto bad;
31052549Smdodd	}
31151673Smdodd
31252549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
31352549Smdodd				   sc, &sc->ep_intrhand))) {
31452549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
31552549Smdodd		goto bad;
31652549Smdodd	}
31751673Smdodd
31852549Smdodd	return (0);
31952549Smdoddbad:
32052549Smdodd	ep_free(dev);
32152549Smdodd	return (error);
32251673Smdodd}
32351673Smdodd
32452549Smdoddstatic device_method_t ep_isa_methods[] = {
32552549Smdodd	/* Device interface */
32652549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
32752549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
32852549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
32951673Smdodd
33052549Smdodd	{ 0, 0 }
33152549Smdodd};
33251673Smdodd
33352549Smdoddstatic driver_t ep_isa_driver = {
33452549Smdodd	"ep",
33552549Smdodd	ep_isa_methods,
33652549Smdodd	sizeof(struct ep_softc),
33752549Smdodd};
33851673Smdodd
33952549Smdoddextern devclass_t ep_devclass;
34051673Smdodd
34152549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
342