if_ep_isa.c revision 121492
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
31117700Smarkm#include <sys/cdefs.h>
32117700Smarkm__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_isa.c 121492 2003-10-25 04:09:49Z imp $");
33117700Smarkm
34119418Sobrien#include <sys/cdefs.h>
35119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep_isa.c 121492 2003-10-25 04:09:49Z imp $");
36119418Sobrien
3751673Smdodd#include <sys/param.h>
3852549Smdodd#include <sys/systm.h>
3951673Smdodd#include <sys/kernel.h>
4051673Smdodd#include <sys/socket.h>
4152549Smdodd#include <sys/module.h>
4252549Smdodd#include <sys/bus.h>
4352549Smdodd
4452549Smdodd#include <machine/bus.h>
4552549Smdodd#include <machine/resource.h>
46117700Smarkm#include <sys/rman.h>
4752549Smdodd
4851673Smdodd#include <net/if.h>
4952549Smdodd#include <net/if_arp.h>
50117700Smarkm#include <net/if_media.h>
5151673Smdodd
5252549Smdodd#include <isa/isavar.h>
5351673Smdodd
5451673Smdodd#include <dev/ep/if_epreg.h>
5551673Smdodd#include <dev/ep/if_epvar.h>
56110835Smdodd
57110835Smdodd#ifdef __i386__
5851673Smdodd#include <i386/isa/elink.h>
59110835Smdodd#endif
6051673Smdodd
61111292Smarcel#ifdef __i386__
62117700Smarkmstatic u_int16_t get_eeprom_data(int, int);
63117700Smarkmstatic void ep_isa_identify(driver_t *, device_t);
64110835Smdodd#endif
6551673Smdodd
66117700Smarkmstatic int ep_isa_probe(device_t);
67117700Smarkmstatic int ep_isa_attach(device_t);
68117700Smarkmstatic int ep_eeprom_cksum(struct ep_softc *);
69117700Smarkm
7052549Smdoddstruct isa_ident {
71117700Smarkm	u_int32_t id;
72117700Smarkm	char *name;
7351673Smdodd};
74117700Smarkmconst char *ep_isa_match_id(u_int32_t, struct isa_ident *);
7551673Smdodd
7656428Smdodd#define ISA_ID_3C509_XXX   0x0506d509
7752549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7852549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7952549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
8052549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
8156428Smdodd#define ISA_ID_3C509_TPC   0x506d5098
8269788Snyan#ifdef PC98
8369788Snyan#define ISA_ID_3C569B_COMBO 0x506d5694
8469788Snyan#define ISA_ID_3C569B_TPO   0x506d5695
8569788Snyan#endif
8651673Smdodd
87111292Smarcel#ifdef __i386__
8852549Smdoddstatic struct isa_ident ep_isa_devs[] = {
89117700Smarkm	{ISA_ID_3C509_TP, "3Com 3C509-TP EtherLink III"},
90117700Smarkm	{ISA_ID_3C509_BNC, "3Com 3C509-BNC EtherLink III"},
91117700Smarkm	{ISA_ID_3C509_COMBO, "3Com 3C509-Combo EtherLink III"},
92117700Smarkm	{ISA_ID_3C509_TPO, "3Com 3C509-TPO EtherLink III"},
93117700Smarkm	{ISA_ID_3C509_TPC, "3Com 3C509-TPC EtherLink III"},
9469788Snyan#ifdef PC98
95117700Smarkm	{ISA_ID_3C569B_COMBO, "3Com 3C569B-J-Combo EtherLink III"},
96117700Smarkm	{ISA_ID_3C569B_TPO, "3Com 3C569B-J-TPO EtherLink III"},
9769788Snyan#endif
98117700Smarkm	{0, NULL},
9952549Smdodd};
100111292Smarcel#endif
10151673Smdodd
10252549Smdoddstatic struct isa_pnp_id ep_ids[] = {
103117700Smarkm	{0x90506d50, "3Com 3C509B-TP EtherLink III (PnP)"},	/* TCM5090 */
104117700Smarkm	{0x91506d50, "3Com 3C509B-BNC EtherLink III (PnP)"},	/* TCM5091 */
105117700Smarkm	{0x94506d50, "3Com 3C509B-Combo EtherLink III (PnP)"},	/* TCM5094 */
106117700Smarkm	{0x95506d50, "3Com 3C509B-TPO EtherLink III (PnP)"},	/* TCM5095 */
107117700Smarkm	{0x98506d50, "3Com 3C509B-TPC EtherLink III (PnP)"},	/* TCM5098 */
108117700Smarkm	{0xf780d041, NULL},	/* PNP80f7 */
109117700Smarkm	{0, NULL},
11052549Smdodd};
11151673Smdodd
11252549Smdodd/*
11352549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
11452549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
11552549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
11652549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
11752549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
11852549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
11952549Smdodd * compares the data on the bus; if there is a difference then that card goes
12052549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
12152549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
12252549Smdodd * read 16 times getting one bit of data with each read.
12352549Smdodd */
124111292Smarcel#ifdef __i386__
12554198Smdoddstatic u_int16_t
126117700Smarkmget_eeprom_data(int id_port, int offset)
12752549Smdodd{
128117700Smarkm	int i;
129117700Smarkm	u_int16_t data = 0;
13054198Smdodd
131117700Smarkm	outb(id_port, EEPROM_CMD_RD | offset);
13252549Smdodd	DELAY(BIT_DELAY_MULTIPLE * 1000);
13354198Smdodd	for (i = 0; i < 16; i++) {
13454198Smdodd		DELAY(50);
13554198Smdodd		data = (data << 1) | (inw(id_port) & 1);
13654198Smdodd	}
13754198Smdodd	return (data);
13852549Smdodd}
139111292Smarcel#endif
14051673Smdodd
14152549Smdoddconst char *
142117700Smarkmep_isa_match_id(u_int32_t id, struct isa_ident *isa_devs)
14352549Smdodd{
144117700Smarkm	struct isa_ident *i = isa_devs;
145117700Smarkm
146117700Smarkm	while (i->name != NULL) {
147117700Smarkm		if (id == i->id)
148117700Smarkm			return (i->name);
149117700Smarkm		i++;
15052549Smdodd	}
15156428Smdodd	/*
15256428Smdodd	 * If we see a card that is likely to be a 3c509
15356428Smdodd	 * return something so that it will work; be annoying
15456428Smdodd	 * so that the user will tell us about it though.
15556428Smdodd	 */
156117700Smarkm	if ((id >> 4) == ISA_ID_3C509_XXX)
15756428Smdodd		return ("Unknown 3c509; notify maintainer!");
15852549Smdodd	return (NULL);
15952549Smdodd}
16051673Smdodd
161110835Smdodd#ifdef __i386__
16252549Smdoddstatic void
163117700Smarkmep_isa_identify(driver_t * driver, device_t parent)
16452549Smdodd{
165117700Smarkm	int tag = EP_LAST_TAG;
166117700Smarkm	int found = 0;
167117700Smarkm	int i;
168117700Smarkm	int j;
169117700Smarkm	const char *desc;
170117700Smarkm	u_int16_t data;
171117700Smarkm	u_int32_t irq;
172117700Smarkm	u_int32_t ioport;
173117700Smarkm	u_int32_t isa_id;
174117700Smarkm	device_t child;
17551673Smdodd
17652549Smdodd	outb(ELINK_ID_PORT, 0);
17752549Smdodd	outb(ELINK_ID_PORT, 0);
17852549Smdodd	elink_idseq(ELINK_509_POLY);
17952549Smdodd	elink_reset();
18051673Smdodd
18152549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
18251673Smdodd
18352549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
18451673Smdodd
18552549Smdodd		outb(ELINK_ID_PORT, 0);
18652549Smdodd		outb(ELINK_ID_PORT, 0);
18754196Smdodd		elink_idseq(ELINK_509_POLY);
18854196Smdodd		DELAY(400);
18951673Smdodd
190117700Smarkm		/*
191117700Smarkm		 * For the first probe, clear all board's tag registers.
192117700Smarkm		 * Otherwise kill off already-found boards. -- linux 3c509.c
19352549Smdodd		 */
194117700Smarkm		if (i == 0)
19552549Smdodd			outb(ELINK_ID_PORT, 0xd0);
196117700Smarkm		else
19752549Smdodd			outb(ELINK_ID_PORT, 0xd8);
19851673Smdodd
19954196Smdodd		/* Get out of loop if we're out of cards. */
20054196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
201117700Smarkm		if (data != MFG_ID)
20254196Smdodd			break;
20354196Smdodd		/* resolve contention using the Ethernet address */
204117700Smarkm		for (j = 0; j < 3; j++)
20554196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
20654196Smdodd
20752549Smdodd		/*
20852549Smdodd		 * Construct an 'isa_id' in 'EISA'
20952549Smdodd		 * format.
21052549Smdodd		 */
21152549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
21252549Smdodd		isa_id = (htons(data) << 16);
21352549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
21452549Smdodd		isa_id |= htons(data);
21551673Smdodd
21652549Smdodd		/* Find known ISA boards */
21752549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
21852549Smdodd		if (!desc) {
219117700Smarkm			if (bootverbose)
220119572Smarkm				device_printf(parent,
221119572Smarkm				    "if_ep: unknown ID 0x%08x\n", isa_id);
22256429Smdodd			continue;
22352549Smdodd		}
22452549Smdodd		/* Retreive IRQ */
22552549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
22652549Smdodd		irq = (data >> 12);
22751673Smdodd
22852549Smdodd		/* Retreive IOPORT */
22952549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
23052549Smdodd#ifdef PC98
23163379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
23252549Smdodd#else
23363379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
23452549Smdodd#endif
23551673Smdodd
23663379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
237119572Smarkm			device_printf(parent,
238119572Smarkm			    "if_ep: <%s> at port 0x%03x in EISA mode!\n",
239117700Smarkm			    desc, ioport);
240117700Smarkm			/*
241117700Smarkm			 * Set the adaptor tag so that the next card can be
242117700Smarkm			 * found.
243117700Smarkm			 */
24463379Smdodd			outb(ELINK_ID_PORT, tag--);
24563379Smdodd			continue;
24663379Smdodd		}
24754196Smdodd		/* Test for an adapter with PnP support. */
24854196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
24954196Smdodd		if (data == CAP_ISA) {
250119572Smarkm			data = get_eeprom_data(ELINK_ID_PORT,
251119572Smarkm			    EEPROM_INT_CONFIG_1);
25254196Smdodd			if (data & ICW1_IAS_PNP) {
253117700Smarkm				if (bootverbose)
254119572Smarkm					device_printf(parent,
255119572Smarkm					    "if_ep: <%s> at 0x%03x "
256119572Smarkm					    "in PnP mode!\n",
257117700Smarkm					    desc, ioport);
258117700Smarkm				/*
259117700Smarkm				 * Set the adaptor tag so that the next card
260117700Smarkm				 * can be found.
261117700Smarkm				 */
26254196Smdodd				outb(ELINK_ID_PORT, tag--);
26354196Smdodd				continue;
26454196Smdodd			}
26554196Smdodd		}
26652549Smdodd		/* Set the adaptor tag so that the next card can be found. */
26752549Smdodd		outb(ELINK_ID_PORT, tag--);
26851673Smdodd
26952549Smdodd		/* Activate the adaptor at the EEPROM location. */
27054196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
27151673Smdodd
27254196Smdodd		/* Test for an adapter in TEST mode. */
27354196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
27452549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
27552549Smdodd		if (data & EEPROM_TST_MODE) {
276119572Smarkm			device_printf(parent,
277119572Smarkm			    "if_ep: <%s> at port 0x%03x in TEST mode!"
278119572Smarkm			    "  Erase pencil mark.\n",
279117700Smarkm			    desc, ioport);
28052549Smdodd			continue;
28152549Smdodd		}
28252549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
28352549Smdodd		device_set_desc_copy(child, desc);
28452549Smdodd		device_set_driver(child, driver);
28552549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
28652549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
28751673Smdodd
288117700Smarkm		if (bootverbose)
289119572Smarkm			device_printf(parent,
290119572Smarkm			    "if_ep: <%s>"
291119572Smarkm			    " at port 0x%03x-0x%03x irq %d\n",
292117700Smarkm			    desc, ioport, ioport + EP_IOSIZE, irq);
29352549Smdodd		found++;
29452549Smdodd	}
29552549Smdodd}
296110835Smdodd#endif
29751673Smdodd
29852549Smdoddstatic int
299117700Smarkmep_isa_probe(device_t dev)
30052549Smdodd{
301117700Smarkm	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 */
307117700Smarkm	if (error == ENXIO)
308117700Smarkm		return (error);
30951673Smdodd
31052549Smdodd	/* If we had some other problem. */
311117700Smarkm	if (!(error == 0 || error == ENOENT))
31252549Smdodd		return (error);
31351673Smdodd
31452549Smdodd	/* If we have the resources we need then we're good to go. */
31552549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
316117700Smarkm	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0))
31752549Smdodd		return (0);
31851673Smdodd
31952549Smdodd	return (ENXIO);
32052549Smdodd}
32151673Smdodd
32252549Smdoddstatic int
323117700Smarkmep_isa_attach(device_t dev)
32452549Smdodd{
325117700Smarkm	struct ep_softc *sc = device_get_softc(dev);
326117700Smarkm	int error = 0;
32751673Smdodd
32852549Smdodd	if ((error = ep_alloc(dev))) {
32952549Smdodd		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
33052549Smdodd		goto bad;
33152549Smdodd	}
33252549Smdodd	ep_get_media(sc);
33351673Smdodd
33452549Smdodd	GO_WINDOW(0);
335121206Simp	SET_IRQ(sc, rman_get_start(sc->irq));
33651673Smdodd
33752549Smdodd	if ((error = ep_attach(sc))) {
33852549Smdodd		device_printf(dev, "ep_attach() failed! (%d)\n", error);
33952549Smdodd		goto bad;
34052549Smdodd	}
341112824Smdodd	error = ep_eeprom_cksum(sc);
342112824Smdodd	if (error) {
343112824Smdodd		device_printf(sc->dev, "Invalid EEPROM checksum!\n");
344112824Smdodd		goto bad;
345112824Smdodd	}
346121492Simp	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, ep_intr,
347117700Smarkm		    sc, &sc->ep_intrhand))) {
34852549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
34952549Smdodd		goto bad;
35052549Smdodd	}
35152549Smdodd	return (0);
35252549Smdoddbad:
35352549Smdodd	ep_free(dev);
35452549Smdodd	return (error);
35551673Smdodd}
35651673Smdodd
357112824Smdoddstatic int
358117700Smarkmep_eeprom_cksum(struct ep_softc *sc)
359112824Smdodd{
360117700Smarkm	int i;
361117700Smarkm	int error;
362117700Smarkm	u_int16_t val;
363117700Smarkm	u_int16_t cksum;
364117700Smarkm	u_int8_t cksum_high = 0;
365117700Smarkm	u_int8_t cksum_low = 0;
366112824Smdodd
367112824Smdodd	error = get_e(sc, 0x0f, &val);
368112824Smdodd	if (error)
369117700Smarkm		return (ENXIO);
370112824Smdodd	cksum = val;
371112824Smdodd
372112824Smdodd	for (i = 0; i < 0x0f; i++) {
373112824Smdodd		error = get_e(sc, i, &val);
374112824Smdodd		if (error)
375112824Smdodd			return (ENXIO);
376112824Smdodd		switch (i) {
377117700Smarkm		case 0x08:
378117700Smarkm		case 0x09:
379117700Smarkm		case 0x0d:
380117700Smarkm			cksum_low ^= (u_int8_t) (val & 0x00ff) ^
381119572Smarkm			    (u_int8_t)((val & 0xff00) >> 8);
382117700Smarkm			break;
383117700Smarkm		default:
384117700Smarkm			cksum_high ^= (u_int8_t) (val & 0x00ff) ^
385119572Smarkm			    (u_int8_t)((val & 0xff00) >> 8);
386117700Smarkm			break;
387112824Smdodd		}
388112824Smdodd	}
389119572Smarkm	return (cksum != ((u_int16_t)cksum_low | (u_int16_t)(cksum_high << 8)));
390112824Smdodd}
391112824Smdodd
39252549Smdoddstatic device_method_t ep_isa_methods[] = {
39352549Smdodd	/* Device interface */
394110835Smdodd#ifdef __i386__
395117700Smarkm	DEVMETHOD(device_identify, ep_isa_identify),
396110835Smdodd#endif
397117700Smarkm	DEVMETHOD(device_probe, ep_isa_probe),
398117700Smarkm	DEVMETHOD(device_attach, ep_isa_attach),
399117700Smarkm	DEVMETHOD(device_detach, ep_detach),
40051673Smdodd
401117700Smarkm	{0, 0}
40252549Smdodd};
40351673Smdodd
40452549Smdoddstatic driver_t ep_isa_driver = {
40552549Smdodd	"ep",
40652549Smdodd	ep_isa_methods,
40752549Smdodd	sizeof(struct ep_softc),
40852549Smdodd};
40951673Smdodd
41052549Smdoddextern devclass_t ep_devclass;
41151673Smdodd
41252549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
413112794Smdodd#ifdef __i386__
414112794SmdoddMODULE_DEPEND(ep, elink, 1, 1, 1);
415112794Smdodd#endif
416