if_ep_isa.c revision 111292
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 111292 2003-02-23 03:39:22Z marcel $
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
58111292Smarcel#ifdef __i386__
5954200Smdoddstatic u_int16_t	get_eeprom_data	(int, int);
6054200Smdoddstatic void		ep_isa_identify	(driver_t *, device_t);
61110835Smdodd#endif
6254200Smdoddstatic int		ep_isa_probe	(device_t);
6354200Smdoddstatic int		ep_isa_attach	(device_t);
6451673Smdodd
6552549Smdoddstruct isa_ident {
6652549Smdodd	u_int32_t	id;
6752549Smdodd	char *		name;
6851673Smdodd};
6952549Smdoddconst char * ep_isa_match_id (u_int32_t, struct isa_ident *);
7051673Smdodd
7156428Smdodd#define ISA_ID_3C509_XXX   0x0506d509
7252549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7352549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7452549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
7552549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
7656428Smdodd#define ISA_ID_3C509_TPC   0x506d5098
7769788Snyan#ifdef PC98
7869788Snyan#define ISA_ID_3C569B_COMBO 0x506d5694
7969788Snyan#define ISA_ID_3C569B_TPO   0x506d5695
8069788Snyan#endif
8151673Smdodd
82111292Smarcel#ifdef __i386__
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};
95111292Smarcel#endif
9651673Smdodd
9752549Smdoddstatic struct isa_pnp_id ep_ids[] = {
9854197Smdodd	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
9954197Smdodd	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
10054197Smdodd	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
10154197Smdodd	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
10256428Smdodd	{ 0x98506d50,	"3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
10354197Smdodd	{ 0xf780d041,	NULL }, /* PNP80f7 */
10454197Smdodd	{ 0,		NULL },
10552549Smdodd};
10651673Smdodd
10752549Smdodd/*
10852549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
10952549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
11052549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
11152549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
11252549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
11352549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
11452549Smdodd * compares the data on the bus; if there is a difference then that card goes
11552549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
11652549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
11752549Smdodd * read 16 times getting one bit of data with each read.
11852549Smdodd */
119111292Smarcel#ifdef __i386__
12054198Smdoddstatic u_int16_t
12152549Smdoddget_eeprom_data(id_port, offset)
12254198Smdodd	int	id_port;
12354198Smdodd	int	offset;
12452549Smdodd{
12554198Smdodd	int		i;
12654198Smdodd	u_int16_t	data = 0;
12754198Smdodd
12854198Smdodd	outb(id_port, EEPROM_CMD_RD|offset);
12952549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
13054198Smdodd	for (i = 0; i < 16; i++) {
13154198Smdodd		DELAY(50);
13254198Smdodd		data = (data << 1) | (inw(id_port) & 1);
13354198Smdodd	}
13454198Smdodd	return (data);
13552549Smdodd}
136111292Smarcel#endif
13751673Smdodd
13852549Smdoddconst char *
13952549Smdoddep_isa_match_id (id, isa_devs)
14052549Smdodd	u_int32_t	      id;
14152549Smdodd	struct isa_ident *      isa_devs;
14252549Smdodd{
14352549Smdodd	struct isa_ident *      i = isa_devs;
14452549Smdodd	while(i->name != NULL) {
14552549Smdodd	       if (id == i->id)
14652549Smdodd		      return (i->name);
14752549Smdodd	       i++;
14852549Smdodd	}
14956428Smdodd	/*
15056428Smdodd	 * If we see a card that is likely to be a 3c509
15156428Smdodd	 * return something so that it will work; be annoying
15256428Smdodd	 * so that the user will tell us about it though.
15356428Smdodd	 */
15456429Smdodd	if ((id >> 4) == ISA_ID_3C509_XXX) {
15556428Smdodd		return ("Unknown 3c509; notify maintainer!");
15656428Smdodd	}
15752549Smdodd	return (NULL);
15852549Smdodd}
15951673Smdodd
160110835Smdodd#ifdef __i386__
16152549Smdoddstatic void
16252549Smdoddep_isa_identify (driver_t *driver, device_t parent)
16352549Smdodd{
16452549Smdodd	int		tag = EP_LAST_TAG;
16552549Smdodd	int		found = 0;
16652549Smdodd	int		i;
16752549Smdodd	int		j;
16852549Smdodd	const char *	desc;
16954196Smdodd	u_int16_t	data;
17052549Smdodd	u_int32_t	irq;
17152549Smdodd	u_int32_t	ioport;
17252549Smdodd	u_int32_t	isa_id;
17352549Smdodd	device_t	child;
17451673Smdodd
17552549Smdodd	outb(ELINK_ID_PORT, 0);
17652549Smdodd	outb(ELINK_ID_PORT, 0);
17752549Smdodd	elink_idseq(ELINK_509_POLY);
17852549Smdodd	elink_reset();
17951673Smdodd
18052549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
18151673Smdodd
18252549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
18351673Smdodd
18452549Smdodd		outb(ELINK_ID_PORT, 0);
18552549Smdodd		outb(ELINK_ID_PORT, 0);
18654196Smdodd		elink_idseq(ELINK_509_POLY);
18754196Smdodd		DELAY(400);
18851673Smdodd
18952549Smdodd		/* For the first probe, clear all
19052549Smdodd		 * board's tag registers.
19152549Smdodd		 * Otherwise kill off already-found
19252549Smdodd		 * boards. -- linux 3c509.c
19352549Smdodd		 */
19452549Smdodd		if (i == 0) {
19552549Smdodd			outb(ELINK_ID_PORT, 0xd0);
19652549Smdodd		} else {
19752549Smdodd			outb(ELINK_ID_PORT, 0xd8);
19852549Smdodd		}
19951673Smdodd
20054196Smdodd		/* Get out of loop if we're out of cards. */
20154196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
20254196Smdodd		if (data != MFG_ID) {
20354196Smdodd			break;
20454196Smdodd		}
20554196Smdodd
20654196Smdodd		/* resolve contention using the Ethernet address */
20754196Smdodd		for (j = 0; j < 3; j++) {
20854196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
20954196Smdodd		}
21054196Smdodd
21152549Smdodd		/*
21252549Smdodd		 * Construct an 'isa_id' in 'EISA'
21352549Smdodd		 * format.
21452549Smdodd		 */
21552549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
21652549Smdodd		isa_id = (htons(data) << 16);
21752549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
21852549Smdodd		isa_id |= htons(data);
21951673Smdodd
22052549Smdodd		/* Find known ISA boards */
22152549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
22252549Smdodd		if (!desc) {
22352549Smdodd			if (bootverbose) {
22452549Smdodd				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
22552549Smdodd						isa_id);
22652549Smdodd			}
22756429Smdodd			continue;
22852549Smdodd		}
22951673Smdodd
23052549Smdodd		/* Retreive IRQ */
23152549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
23252549Smdodd		irq = (data >> 12);
23351673Smdodd
23452549Smdodd		/* Retreive IOPORT */
23552549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
23652549Smdodd#ifdef PC98
23763379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
23852549Smdodd#else
23963379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
24052549Smdodd#endif
24151673Smdodd
24263379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
24363379Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in EISA mode!\n",
24463379Smdodd					desc, ioport);
24563379Smdodd			/* Set the adaptor tag so that the next card can be found. */
24663379Smdodd			outb(ELINK_ID_PORT, tag--);
24763379Smdodd			continue;
24863379Smdodd		}
24963379Smdodd
25054196Smdodd		/* Test for an adapter with PnP support. */
25154196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
25254196Smdodd		if (data == CAP_ISA) {
25354196Smdodd			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
25454196Smdodd			if (data & ICW1_IAS_PNP) {
25554196Smdodd				if (bootverbose) {
25656429Smdodd					device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
25756429Smdodd					  	      desc, ioport);
25854196Smdodd				}
25954196Smdodd				/* Set the adaptor tag so that the next card can be found. */
26054196Smdodd				outb(ELINK_ID_PORT, tag--);
26154196Smdodd				continue;
26254196Smdodd			}
26354196Smdodd		}
26454196Smdodd
26552549Smdodd		/* Set the adaptor tag so that the next card can be found. */
26652549Smdodd		outb(ELINK_ID_PORT, tag--);
26751673Smdodd
26852549Smdodd		/* Activate the adaptor at the EEPROM location. */
26954196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
27051673Smdodd
27154196Smdodd		/* Test for an adapter in TEST mode. */
27254196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
27352549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
27452549Smdodd		if (data & EEPROM_TST_MODE) {
27556429Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
27656429Smdodd					desc, ioport);
27752549Smdodd			continue;
27852549Smdodd		}
27951673Smdodd
28052549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
28152549Smdodd		device_set_desc_copy(child, desc);
28252549Smdodd		device_set_driver(child, driver);
28352549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
28452549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
28551673Smdodd
28652549Smdodd		if (bootverbose) {
28752549Smdodd			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
28852549Smdodd					desc, ioport, ioport + EP_IOSIZE, irq);
28952549Smdodd		}
29051673Smdodd
29152549Smdodd		found++;
29252549Smdodd	}
29351673Smdodd
29452549Smdodd	return;
29552549Smdodd}
296110835Smdodd#endif
29751673Smdodd
29852549Smdoddstatic int
29952549Smdoddep_isa_probe (device_t dev)
30052549Smdodd{
30152549Smdodd	int	error = 0;
30251673Smdodd
30352549Smdodd	/* Check isapnp ids */
30452549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
30551673Smdodd
30652549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
30752549Smdodd	if (error == ENXIO) {
30852549Smdodd	       return (error);
30952549Smdodd	}
31051673Smdodd
31152549Smdodd	/* If we had some other problem. */
31252549Smdodd	if (!(error == 0 || error == ENOENT)) {
31352549Smdodd		return (error);
31452549Smdodd	}
31551673Smdodd
31652549Smdodd	/* If we have the resources we need then we're good to go. */
31752549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
31852549Smdodd	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
31952549Smdodd		return (0);
32052549Smdodd	}
32151673Smdodd
32252549Smdodd	return (ENXIO);
32352549Smdodd}
32451673Smdodd
32552549Smdoddstatic int
32652549Smdoddep_isa_attach (device_t dev)
32752549Smdodd{
32852549Smdodd	struct ep_softc *	sc = device_get_softc(dev);
32952549Smdodd	int			error = 0;
33051673Smdodd
33152549Smdodd	if ((error = ep_alloc(dev))) {
33252549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
33352549Smdodd		goto bad;
33452549Smdodd	}
33551673Smdodd
33652549Smdodd	ep_get_media(sc);
33751673Smdodd
33852549Smdodd	GO_WINDOW(0);
33952549Smdodd	SET_IRQ(BASE, rman_get_start(sc->irq));
34051673Smdodd
34152549Smdodd	if ((error = ep_attach(sc))) {
34252549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
34352549Smdodd		goto bad;
34452549Smdodd	}
34551673Smdodd
34652549Smdodd	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
34752549Smdodd				   sc, &sc->ep_intrhand))) {
34852549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
34952549Smdodd		goto bad;
35052549Smdodd	}
35151673Smdodd
35252549Smdodd	return (0);
35352549Smdoddbad:
35452549Smdodd	ep_free(dev);
35552549Smdodd	return (error);
35651673Smdodd}
35751673Smdodd
35852549Smdoddstatic device_method_t ep_isa_methods[] = {
35952549Smdodd	/* Device interface */
360110835Smdodd#ifdef __i386__
36152549Smdodd	DEVMETHOD(device_identify,	ep_isa_identify),
362110835Smdodd#endif
36352549Smdodd	DEVMETHOD(device_probe,		ep_isa_probe),
36452549Smdodd	DEVMETHOD(device_attach,	ep_isa_attach),
36551673Smdodd
36652549Smdodd	{ 0, 0 }
36752549Smdodd};
36851673Smdodd
36952549Smdoddstatic driver_t ep_isa_driver = {
37052549Smdodd	"ep",
37152549Smdodd	ep_isa_methods,
37252549Smdodd	sizeof(struct ep_softc),
37352549Smdodd};
37451673Smdodd
37552549Smdoddextern devclass_t ep_devclass;
37651673Smdodd
37752549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
378