if_ep_isa.c revision 56428
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 56428 2000-01-23 08:23:38Z 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	 */
14256428Smdodd	if ((id >> 8) == 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			}
21452549Smdodd			break;
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
22454196Smdodd		ioport = (((data & 0x1f) * 0x100) + 0x40d0);
22552549Smdodd#else
22654196Smdodd		ioport = (((data & 0x1f) << 4) + 0x200);
22752549Smdodd#endif
22851673Smdodd
22954196Smdodd		/* Test for an adapter with PnP support. */
23054196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
23154196Smdodd		if (data == CAP_ISA) {
23254196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
23354196Smdodd			if (data & ICW1_IAS_PNP) {
23454196Smdodd				if (bootverbose) {
23554196Smdodd					device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n",
23654196Smdodd					  	      ioport);
23754196Smdodd				}
23854196Smdodd				/* Set the adaptor tag so that the next card can be found. */
23954196Smdodd				outb(ELINK_ID_PORT, tag--);
24054196Smdodd				continue;
24154196Smdodd			}
24254196Smdodd		}
24354196Smdodd
24452549Smdodd		/* Set the adaptor tag so that the next card can be found. */
24552549Smdodd		outb(ELINK_ID_PORT, tag--);
24651673Smdodd
24752549Smdodd		/* Activate the adaptor at the EEPROM location. */
24854196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
24951673Smdodd
25054196Smdodd		/* Test for an adapter in TEST mode. */
25154196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
25252549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
25352549Smdodd		if (data & EEPROM_TST_MODE) {
25454196Smdodd			device_printf(parent, "if_ep: Adapter at 0x%03x in TEST mode!  Erase pencil mark.\n",
25554196Smdodd			  	      ioport);
25652549Smdodd			continue;
25752549Smdodd		}
25851673Smdodd
25952549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
26052549Smdodd		device_set_desc_copy(child, desc);
26152549Smdodd		device_set_driver(child, driver);
26252549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
26352549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
26451673Smdodd
26552549Smdodd		if (bootverbose) {
26652549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
26752549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
26852549Smdodd		}
26951673Smdodd
27052549Smdodd		found++;
27152549Smdodd	}
27251673Smdodd
27352549Smdodd	return;
27452549Smdodd}
27551673Smdodd
27652549Smdoddstatic int
27752549Smdoddep_isa_probe (device_t dev)
27852549Smdodd{
27952549Smdodd	int	error = 0;
28051673Smdodd
28152549Smdodd	/* Check isapnp ids */
28252549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
28351673Smdodd
28452549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
28552549Smdodd	if (error == ENXIO) {
28652549Smdodd	       return (error);
28752549Smdodd	}
28851673Smdodd
28952549Smdodd	/* If we had some other problem. */
29052549Smdodd	if (!(error == 0 || error == ENOENT)) {
29152549Smdodd		return (error);
29252549Smdodd	}
29351673Smdodd
29452549Smdodd	/* If we have the resources we need then we're good to go. */
29552549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
29652549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
29752549Smdodd		return (0);
29852549Smdodd	}
29951673Smdodd
30052549Smdodd	return (ENXIO);
30152549Smdodd}
30251673Smdodd
30352549Smdoddstatic int
30452549Smdoddep_isa_attach (device_t dev)
30552549Smdodd{
30652549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
30752549Smdodd	int			error = 0;
30851673Smdodd
30952549Smdodd	if ((error = ep_alloc(dev))) {
31052549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
31152549Smdodd		goto bad;
31252549Smdodd	}
31351673Smdodd
31452549Smdodd	ep_get_media(sc);
31551673Smdodd
31652549Smdodd	GO_WINDOW(0);
31752549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
31851673Smdodd
31952549Smdodd	if ((error = ep_attach(sc))) {
32052549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
32152549Smdodd		goto bad;
32252549Smdodd	}
32351673Smdodd
32452549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
32552549Smdodd				   sc, &sc->ep_intrhand))) {
32652549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
32752549Smdodd		goto bad;
32852549Smdodd	}
32951673Smdodd
33052549Smdodd	return (0);
33152549Smdoddbad:
33252549Smdodd	ep_free(dev);
33352549Smdodd	return (error);
33451673Smdodd}
33551673Smdodd
33652549Smdoddstatic device_method_t ep_isa_methods[] = {
33752549Smdodd	/* Device interface */
33852549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
33952549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
34052549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
34151673Smdodd
34252549Smdodd	{ 0, 0 }
34352549Smdodd};
34451673Smdodd
34552549Smdoddstatic driver_t ep_isa_driver = {
34652549Smdodd	"ep",
34752549Smdodd	ep_isa_methods,
34852549Smdodd	sizeof(struct ep_softc),
34952549Smdodd};
35051673Smdodd
35152549Smdoddextern devclass_t ep_devclass;
35251673Smdodd
35352549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
354