if_ep_isa.c revision 54196
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 54196 1999-12-06 08:31:47Z 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[] = {
7652549Smdodd	{ ISA_ID_3C509_TP,	"3Com EtherLink III (3c509-TP)" },
7752549Smdodd	{ ISA_ID_3C509_BNC,	"3Com EtherLink III (3c509-BNC)" },
7852549Smdodd	{ ISA_ID_3C509_COMBO,	"3Com EtherLink III (3c509-Combo)" },
7952549Smdodd	{ ISA_ID_3C509_TPO,	"3Com EtherLink III (3c509-TPO)" },
8052549Smdodd	{ 0,			NULL },
8152549Smdodd};
8251673Smdodd
8352549Smdoddstatic struct isa_pnp_id ep_ids[] = {
8452549Smdodd	{ 0x90506d50,		NULL },	/* TCM5090 */
8552549Smdodd	{ 0x91506d50,		NULL },	/* TCM5091 */
8652549Smdodd	{ 0x94506d50,		NULL },	/* TCM5094 */
8752549Smdodd	{ 0x95506d50,		NULL },	/* TCM5095 */
8852549Smdodd	{ 0xf780d041,		NULL }, /* PNP80f7 */
8952549Smdodd	{ 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
10552549Smdoddstatic int
10652549Smdoddget_eeprom_data(id_port, offset)
10752549Smdodd    int id_port;
10852549Smdodd    int offset;
10952549Smdodd{
11052549Smdodd    int i, data = 0;
11152549Smdodd    outb(id_port, 0x80 + offset);
11252549Smdodd    for (i = 0; i < 16; i++) {
11352549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
11452549Smdodd	data = (data << 1) | (inw(id_port) & 1);
11551673Smdodd    }
11652549Smdodd    return (data);
11752549Smdodd}
11851673Smdodd
11952549Smdoddconst char *
12052549Smdoddep_isa_match_id (id, isa_devs)
12152549Smdodd	u_int32_t	      id;
12252549Smdodd	struct isa_ident *      isa_devs;
12352549Smdodd{
12452549Smdodd	struct isa_ident *      i = isa_devs;
12552549Smdodd	while(i->name != NULL) {
12652549Smdodd	       if (id == i->id)
12752549Smdodd		      return (i->name);
12852549Smdodd	       i++;
12952549Smdodd	}
13052549Smdodd	return (NULL);
13152549Smdodd}
13251673Smdodd
13352549Smdoddstatic void
13452549Smdoddep_isa_identify (driver_t *driver, device_t parent)
13552549Smdodd{
13652549Smdodd	int		tag = EP_LAST_TAG;
13752549Smdodd	int		found = 0;
13852549Smdodd	int		i;
13952549Smdodd	int		j;
14052549Smdodd	const char *	desc;
14154196Smdodd	u_int16_t	data;
14252549Smdodd	u_int32_t	irq;
14352549Smdodd	u_int32_t	ioport;
14452549Smdodd	u_int32_t	isa_id;
14552549Smdodd	device_t	child;
14651673Smdodd
14752549Smdodd	outb(ELINK_ID_PORT, 0);
14852549Smdodd	outb(ELINK_ID_PORT, 0);
14952549Smdodd	elink_idseq(ELINK_509_POLY);
15052549Smdodd	elink_reset();
15151673Smdodd
15252549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
15351673Smdodd
15452549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
15551673Smdodd
15652549Smdodd		outb(ELINK_ID_PORT, 0);
15752549Smdodd		outb(ELINK_ID_PORT, 0);
15854196Smdodd		elink_idseq(ELINK_509_POLY);
15954196Smdodd		DELAY(400);
16051673Smdodd
16152549Smdodd		/* For the first probe, clear all
16252549Smdodd		 * board's tag registers.
16352549Smdodd		 * Otherwise kill off already-found
16452549Smdodd		 * boards. -- linux 3c509.c
16552549Smdodd		 */
16652549Smdodd		if (i == 0) {
16752549Smdodd			outb(ELINK_ID_PORT, 0xd0);
16852549Smdodd		} else {
16952549Smdodd			outb(ELINK_ID_PORT, 0xd8);
17052549Smdodd		}
17151673Smdodd
17254196Smdodd		/* Get out of loop if we're out of cards. */
17354196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
17454196Smdodd		if (data != MFG_ID) {
17554196Smdodd			break;
17654196Smdodd		}
17754196Smdodd
17854196Smdodd		/* resolve contention using the Ethernet address */
17954196Smdodd		for (j = 0; j < 3; j++) {
18054196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
18154196Smdodd		}
18254196Smdodd
18352549Smdodd		/*
18452549Smdodd		 * Construct an 'isa_id' in 'EISA'
18552549Smdodd		 * format.
18652549Smdodd		 */
18752549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
18852549Smdodd		isa_id = (htons(data) << 16);
18952549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
19052549Smdodd		isa_id |= htons(data);
19151673Smdodd
19252549Smdodd		/* Find known ISA boards */
19352549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
19452549Smdodd		if (!desc) {
19552549Smdodd			if (bootverbose) {
19652549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
19752549Smdodd						isa_id);
19852549Smdodd			}
19952549Smdodd			break;
20052549Smdodd		}
20151673Smdodd
20252549Smdodd		/* Retreive IRQ */
20352549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
20452549Smdodd		irq = (data >> 12);
20551673Smdodd
20652549Smdodd		/* Retreive IOPORT */
20752549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
20852549Smdodd#ifdef PC98
20954196Smdodd		ioport = (((data & 0x1f) * 0x100) + 0x40d0);
21052549Smdodd#else
21154196Smdodd		ioport = (((data & 0x1f) << 4) + 0x200);
21252549Smdodd#endif
21351673Smdodd
21454196Smdodd		/* Test for an adapter with PnP support. */
21554196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
21654196Smdodd		if (data == CAP_ISA) {
21754196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
21854196Smdodd			if (data & ICW1_IAS_PNP) {
21954196Smdodd				if (bootverbose) {
22054196Smdodd					device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n",
22154196Smdodd					  	      ioport);
22254196Smdodd				}
22354196Smdodd				/* Set the adaptor tag so that the next card can be found. */
22454196Smdodd				outb(ELINK_ID_PORT, tag--);
22554196Smdodd				continue;
22654196Smdodd			}
22754196Smdodd		}
22854196Smdodd
22952549Smdodd		/* Set the adaptor tag so that the next card can be found. */
23052549Smdodd		outb(ELINK_ID_PORT, tag--);
23151673Smdodd
23252549Smdodd		/* Activate the adaptor at the EEPROM location. */
23354196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
23451673Smdodd
23554196Smdodd		/* Test for an adapter in TEST mode. */
23654196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
23752549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
23852549Smdodd		if (data & EEPROM_TST_MODE) {
23954196Smdodd			device_printf(parent, "if_ep: Adapter at 0x%03x in TEST mode!  Erase pencil mark.\n",
24054196Smdodd			  	      ioport);
24152549Smdodd			continue;
24252549Smdodd		}
24351673Smdodd
24452549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
24552549Smdodd		device_set_desc_copy(child, desc);
24652549Smdodd		device_set_driver(child, driver);
24752549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
24852549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
24951673Smdodd
25052549Smdodd		if (bootverbose) {
25152549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
25252549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
25352549Smdodd		}
25451673Smdodd
25552549Smdodd		found++;
25652549Smdodd	}
25751673Smdodd
25852549Smdodd	return;
25952549Smdodd}
26051673Smdodd
26152549Smdoddstatic int
26252549Smdoddep_isa_probe (device_t dev)
26352549Smdodd{
26452549Smdodd	int	error = 0;
26551673Smdodd
26652549Smdodd	/* Check isapnp ids */
26752549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
26851673Smdodd
26952549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
27052549Smdodd	if (error == ENXIO) {
27152549Smdodd	       return (error);
27252549Smdodd	}
27351673Smdodd
27452549Smdodd	/* If we had some other problem. */
27552549Smdodd	if (!(error == 0 || error == ENOENT)) {
27652549Smdodd		return (error);
27752549Smdodd	}
27851673Smdodd
27952549Smdodd	/* If we have the resources we need then we're good to go. */
28052549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
28152549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
28252549Smdodd		return (0);
28352549Smdodd	}
28451673Smdodd
28552549Smdodd	return (ENXIO);
28652549Smdodd}
28751673Smdodd
28852549Smdoddstatic int
28952549Smdoddep_isa_attach (device_t dev)
29052549Smdodd{
29152549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
29252549Smdodd	int			error = 0;
29351673Smdodd
29452549Smdodd	if ((error = ep_alloc(dev))) {
29552549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
29652549Smdodd		goto bad;
29752549Smdodd	}
29851673Smdodd
29952549Smdodd	ep_get_media(sc);
30051673Smdodd
30152549Smdodd	GO_WINDOW(0);
30252549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
30351673Smdodd
30452549Smdodd	if ((error = ep_attach(sc))) {
30552549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
30652549Smdodd		goto bad;
30752549Smdodd	}
30851673Smdodd
30952549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
31052549Smdodd				   sc, &sc->ep_intrhand))) {
31152549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
31252549Smdodd		goto bad;
31352549Smdodd	}
31451673Smdodd
31552549Smdodd	return (0);
31652549Smdoddbad:
31752549Smdodd	ep_free(dev);
31852549Smdodd	return (error);
31951673Smdodd}
32051673Smdodd
32152549Smdoddstatic device_method_t ep_isa_methods[] = {
32252549Smdodd	/* Device interface */
32352549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
32452549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
32552549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
32651673Smdodd
32752549Smdodd	{ 0, 0 }
32852549Smdodd};
32951673Smdodd
33052549Smdoddstatic driver_t ep_isa_driver = {
33152549Smdodd	"ep",
33252549Smdodd	ep_isa_methods,
33352549Smdodd	sizeof(struct ep_softc),
33452549Smdodd};
33551673Smdodd
33652549Smdoddextern devclass_t ep_devclass;
33751673Smdodd
33852549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
339