1139749Simp/*-
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$");
33117700Smarkm
3451673Smdodd#include <sys/param.h>
3552549Smdodd#include <sys/systm.h>
3651673Smdodd#include <sys/kernel.h>
3751673Smdodd#include <sys/socket.h>
3852549Smdodd#include <sys/module.h>
3952549Smdodd#include <sys/bus.h>
4052549Smdodd
4152549Smdodd#include <machine/bus.h>
4252549Smdodd#include <machine/resource.h>
43117700Smarkm#include <sys/rman.h>
4452549Smdodd
4551673Smdodd#include <net/if.h>
4652549Smdodd#include <net/if_arp.h>
47117700Smarkm#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__
59140523Simpstatic uint16_t get_eeprom_data(int, int);
60117700Smarkmstatic void ep_isa_identify(driver_t *, device_t);
61110835Smdodd#endif
6251673Smdodd
63117700Smarkmstatic int ep_isa_probe(device_t);
64117700Smarkmstatic int ep_isa_attach(device_t);
65117700Smarkmstatic int ep_eeprom_cksum(struct ep_softc *);
66117700Smarkm
6752549Smdoddstruct isa_ident {
68140523Simp	uint32_t id;
69117700Smarkm	char *name;
7051673Smdodd};
71140523Simpconst char *ep_isa_match_id(uint32_t, struct isa_ident *);
7251673Smdodd
7356428Smdodd#define ISA_ID_3C509_XXX   0x0506d509
7452549Smdodd#define ISA_ID_3C509_TP    0x506d5090
7552549Smdodd#define ISA_ID_3C509_BNC   0x506d5091
7652549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094
7752549Smdodd#define ISA_ID_3C509_TPO   0x506d5095
7856428Smdodd#define ISA_ID_3C509_TPC   0x506d5098
7969788Snyan#ifdef PC98
8069788Snyan#define ISA_ID_3C569B_COMBO 0x506d5694
8169788Snyan#define ISA_ID_3C569B_TPO   0x506d5695
8269788Snyan#endif
8351673Smdodd
84111292Smarcel#ifdef __i386__
8552549Smdoddstatic struct isa_ident ep_isa_devs[] = {
86117700Smarkm	{ISA_ID_3C509_TP, "3Com 3C509-TP EtherLink III"},
87117700Smarkm	{ISA_ID_3C509_BNC, "3Com 3C509-BNC EtherLink III"},
88117700Smarkm	{ISA_ID_3C509_COMBO, "3Com 3C509-Combo EtherLink III"},
89117700Smarkm	{ISA_ID_3C509_TPO, "3Com 3C509-TPO EtherLink III"},
90117700Smarkm	{ISA_ID_3C509_TPC, "3Com 3C509-TPC EtherLink III"},
9169788Snyan#ifdef PC98
92117700Smarkm	{ISA_ID_3C569B_COMBO, "3Com 3C569B-J-Combo EtherLink III"},
93117700Smarkm	{ISA_ID_3C569B_TPO, "3Com 3C569B-J-TPO EtherLink III"},
9469788Snyan#endif
95117700Smarkm	{0, NULL},
9652549Smdodd};
97111292Smarcel#endif
9851673Smdodd
9952549Smdoddstatic struct isa_pnp_id ep_ids[] = {
100117700Smarkm	{0x90506d50, "3Com 3C509B-TP EtherLink III (PnP)"},	/* TCM5090 */
101117700Smarkm	{0x91506d50, "3Com 3C509B-BNC EtherLink III (PnP)"},	/* TCM5091 */
102117700Smarkm	{0x94506d50, "3Com 3C509B-Combo EtherLink III (PnP)"},	/* TCM5094 */
103117700Smarkm	{0x95506d50, "3Com 3C509B-TPO EtherLink III (PnP)"},	/* TCM5095 */
104117700Smarkm	{0x98506d50, "3Com 3C509B-TPC EtherLink III (PnP)"},	/* TCM5098 */
105117700Smarkm	{0xf780d041, NULL},	/* PNP80f7 */
106117700Smarkm	{0, NULL},
10752549Smdodd};
10851673Smdodd
10952549Smdodd/*
11052549Smdodd * We get eeprom data from the id_port given an offset into the eeprom.
11152549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter
11252549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
11352549Smdodd * the eeprom data.  We then read the port 16 times and with every read; the
11452549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another
11552549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
11652549Smdodd * compares the data on the bus; if there is a difference then that card goes
11752549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in
11852549Smdodd * the AX register which is conveniently returned to us by inb().  Hence; we
11952549Smdodd * read 16 times getting one bit of data with each read.
12052549Smdodd */
121111292Smarcel#ifdef __i386__
122140523Simpstatic uint16_t
123117700Smarkmget_eeprom_data(int id_port, int offset)
12452549Smdodd{
125117700Smarkm	int i;
126140523Simp	uint16_t data = 0;
12754198Smdodd
128117700Smarkm	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 *
139140523Simpep_isa_match_id(uint32_t id, struct isa_ident *isa_devs)
14052549Smdodd{
141117700Smarkm	struct isa_ident *i = isa_devs;
142117700Smarkm
143117700Smarkm	while (i->name != NULL) {
144117700Smarkm		if (id == i->id)
145117700Smarkm			return (i->name);
146117700Smarkm		i++;
14752549Smdodd	}
14856428Smdodd	/*
14956428Smdodd	 * If we see a card that is likely to be a 3c509
15056428Smdodd	 * return something so that it will work; be annoying
15156428Smdodd	 * so that the user will tell us about it though.
15256428Smdodd	 */
153117700Smarkm	if ((id >> 4) == ISA_ID_3C509_XXX)
15456428Smdodd		return ("Unknown 3c509; notify maintainer!");
15552549Smdodd	return (NULL);
15652549Smdodd}
15751673Smdodd
158110835Smdodd#ifdef __i386__
15952549Smdoddstatic void
160117700Smarkmep_isa_identify(driver_t * driver, device_t parent)
16152549Smdodd{
162117700Smarkm	int tag = EP_LAST_TAG;
163117700Smarkm	int found = 0;
164117700Smarkm	int i;
165117700Smarkm	int j;
166117700Smarkm	const char *desc;
167140523Simp	uint16_t data;
168140523Simp	uint32_t irq;
169140523Simp	uint32_t ioport;
170140523Simp	uint32_t isa_id;
171117700Smarkm	device_t child;
17251673Smdodd
17352549Smdodd	outb(ELINK_ID_PORT, 0);
17452549Smdodd	outb(ELINK_ID_PORT, 0);
17552549Smdodd	elink_idseq(ELINK_509_POLY);
17652549Smdodd	elink_reset();
17751673Smdodd
17852549Smdodd	DELAY(DELAY_MULTIPLE * 10000);
17951673Smdodd
18052549Smdodd	for (i = 0; i < EP_MAX_BOARDS; i++) {
18151673Smdodd
18252549Smdodd		outb(ELINK_ID_PORT, 0);
18352549Smdodd		outb(ELINK_ID_PORT, 0);
18454196Smdodd		elink_idseq(ELINK_509_POLY);
18554196Smdodd		DELAY(400);
18651673Smdodd
187117700Smarkm		/*
188117700Smarkm		 * For the first probe, clear all board's tag registers.
189117700Smarkm		 * Otherwise kill off already-found boards. -- linux 3c509.c
19052549Smdodd		 */
191117700Smarkm		if (i == 0)
19252549Smdodd			outb(ELINK_ID_PORT, 0xd0);
193117700Smarkm		else
19452549Smdodd			outb(ELINK_ID_PORT, 0xd8);
19551673Smdodd
19654196Smdodd		/* Get out of loop if we're out of cards. */
19754196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
198117700Smarkm		if (data != MFG_ID)
19954196Smdodd			break;
20054196Smdodd		/* resolve contention using the Ethernet address */
201117700Smarkm		for (j = 0; j < 3; j++)
20254196Smdodd			(void)get_eeprom_data(ELINK_ID_PORT, j);
20354196Smdodd
20452549Smdodd		/*
20552549Smdodd		 * Construct an 'isa_id' in 'EISA'
20652549Smdodd		 * format.
20752549Smdodd		 */
20852549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
20952549Smdodd		isa_id = (htons(data) << 16);
21052549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
21152549Smdodd		isa_id |= htons(data);
21251673Smdodd
21352549Smdodd		/* Find known ISA boards */
21452549Smdodd		desc = ep_isa_match_id(isa_id, ep_isa_devs);
21552549Smdodd		if (!desc) {
216117700Smarkm			if (bootverbose)
217119572Smarkm				device_printf(parent,
218147717Simp				    "unknown ID 0x%08x\n", isa_id);
21956429Smdodd			continue;
22052549Smdodd		}
22152549Smdodd		/* Retreive IRQ */
22252549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
22352549Smdodd		irq = (data >> 12);
22451673Smdodd
22552549Smdodd		/* Retreive IOPORT */
22652549Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
22752549Smdodd#ifdef PC98
22863379Smdodd		ioport = (((data & ADDR_CFG_MASK) * 0x100) + 0x40d0);
22952549Smdodd#else
23063379Smdodd		ioport = (((data & ADDR_CFG_MASK) << 4) + 0x200);
23152549Smdodd#endif
23251673Smdodd
23363379Smdodd		if ((data & ADDR_CFG_MASK) == ADDR_CFG_EISA) {
234119572Smarkm			device_printf(parent,
235147717Simp			    "<%s> at port 0x%03x in EISA mode!\n",
236117700Smarkm			    desc, ioport);
237117700Smarkm			/*
238117700Smarkm			 * Set the adaptor tag so that the next card can be
239117700Smarkm			 * found.
240117700Smarkm			 */
24163379Smdodd			outb(ELINK_ID_PORT, tag--);
24263379Smdodd			continue;
24363379Smdodd		}
24454196Smdodd		/* Test for an adapter with PnP support. */
24554196Smdodd		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
24654196Smdodd		if (data == CAP_ISA) {
247119572Smarkm			data = get_eeprom_data(ELINK_ID_PORT,
248119572Smarkm			    EEPROM_INT_CONFIG_1);
24954196Smdodd			if (data & ICW1_IAS_PNP) {
250117700Smarkm				if (bootverbose)
251119572Smarkm					device_printf(parent,
252147717Simp					    "<%s> at 0x%03x "
253119572Smarkm					    "in PnP mode!\n",
254117700Smarkm					    desc, ioport);
255117700Smarkm				/*
256117700Smarkm				 * Set the adaptor tag so that the next card
257117700Smarkm				 * can be found.
258117700Smarkm				 */
25954196Smdodd				outb(ELINK_ID_PORT, tag--);
26054196Smdodd				continue;
26154196Smdodd			}
26254196Smdodd		}
26352549Smdodd		/* Set the adaptor tag so that the next card can be found. */
26452549Smdodd		outb(ELINK_ID_PORT, tag--);
26551673Smdodd
26652549Smdodd		/* Activate the adaptor at the EEPROM location. */
26754196Smdodd		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
26851673Smdodd
26954196Smdodd		/* Test for an adapter in TEST mode. */
27054196Smdodd		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
27152549Smdodd		data = inw(ioport + EP_W0_EEPROM_COMMAND);
27252549Smdodd		if (data & EEPROM_TST_MODE) {
273119572Smarkm			device_printf(parent,
274147717Simp			    "<%s> at port 0x%03x in TEST mode!"
275119572Smarkm			    "  Erase pencil mark.\n",
276117700Smarkm			    desc, ioport);
27752549Smdodd			continue;
27852549Smdodd		}
27952549Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
28052549Smdodd		device_set_desc_copy(child, desc);
28152549Smdodd		device_set_driver(child, driver);
28252549Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
28352549Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
28451673Smdodd
285117700Smarkm		if (bootverbose)
286119572Smarkm			device_printf(parent,
287147717Simp			    "<%s>"
288119572Smarkm			    " at port 0x%03x-0x%03x irq %d\n",
289117700Smarkm			    desc, ioport, ioport + EP_IOSIZE, irq);
29052549Smdodd		found++;
29152549Smdodd	}
29252549Smdodd}
293110835Smdodd#endif
29451673Smdodd
29552549Smdoddstatic int
296117700Smarkmep_isa_probe(device_t dev)
29752549Smdodd{
298117700Smarkm	int error = 0;
29951673Smdodd
30052549Smdodd	/* Check isapnp ids */
30152549Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
30251673Smdodd
30352549Smdodd	/* If the card had a PnP ID that didn't match any we know about */
304117700Smarkm	if (error == ENXIO)
305117700Smarkm		return (error);
30651673Smdodd
30752549Smdodd	/* If we had some other problem. */
308117700Smarkm	if (!(error == 0 || error == ENOENT))
30952549Smdodd		return (error);
31051673Smdodd
31152549Smdodd	/* If we have the resources we need then we're good to go. */
31252549Smdodd	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
313117700Smarkm	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0))
31452549Smdodd		return (0);
31551673Smdodd
31652549Smdodd	return (ENXIO);
31752549Smdodd}
31851673Smdodd
31952549Smdoddstatic int
320117700Smarkmep_isa_attach(device_t dev)
32152549Smdodd{
322117700Smarkm	struct ep_softc *sc = device_get_softc(dev);
323117700Smarkm	int error = 0;
32451673Smdodd
325147717Simp	if ((error = ep_alloc(dev)))
32652549Smdodd		goto bad;
32752549Smdodd	ep_get_media(sc);
32851673Smdodd
329121588Simp	GO_WINDOW(sc, 0);
330121206Simp	SET_IRQ(sc, rman_get_start(sc->irq));
33151673Smdodd
332147717Simp	if ((error = ep_attach(sc)))
33352549Smdodd		goto bad;
334112824Smdodd	error = ep_eeprom_cksum(sc);
335112824Smdodd	if (error) {
336112824Smdodd		device_printf(sc->dev, "Invalid EEPROM checksum!\n");
337112824Smdodd		goto bad;
338112824Smdodd	}
339166901Spiso	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
340166901Spiso	    NULL, ep_intr, sc, &sc->ep_intrhand))) {
34152549Smdodd		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
34252549Smdodd		goto bad;
34352549Smdodd	}
34452549Smdodd	return (0);
34552549Smdoddbad:
34652549Smdodd	ep_free(dev);
34752549Smdodd	return (error);
34851673Smdodd}
34951673Smdodd
350112824Smdoddstatic int
351117700Smarkmep_eeprom_cksum(struct ep_softc *sc)
352112824Smdodd{
353117700Smarkm	int i;
354117700Smarkm	int error;
355140523Simp	uint16_t val;
356140523Simp	uint16_t cksum;
357140523Simp	uint8_t cksum_high = 0;
358140523Simp	uint8_t cksum_low = 0;
359112824Smdodd
360147607Simp	error = ep_get_e(sc, 0x0f, &val);
361112824Smdodd	if (error)
362117700Smarkm		return (ENXIO);
363112824Smdodd	cksum = val;
364112824Smdodd
365112824Smdodd	for (i = 0; i < 0x0f; i++) {
366147607Simp		error = ep_get_e(sc, i, &val);
367112824Smdodd		if (error)
368112824Smdodd			return (ENXIO);
369112824Smdodd		switch (i) {
370117700Smarkm		case 0x08:
371117700Smarkm		case 0x09:
372117700Smarkm		case 0x0d:
373140523Simp			cksum_low ^= (uint8_t) (val & 0x00ff) ^
374140523Simp			    (uint8_t)((val & 0xff00) >> 8);
375117700Smarkm			break;
376117700Smarkm		default:
377140523Simp			cksum_high ^= (uint8_t) (val & 0x00ff) ^
378140523Simp			    (uint8_t)((val & 0xff00) >> 8);
379117700Smarkm			break;
380112824Smdodd		}
381112824Smdodd	}
382140523Simp	return (cksum != ((uint16_t)cksum_low | (uint16_t)(cksum_high << 8)));
383112824Smdodd}
384112824Smdodd
38552549Smdoddstatic device_method_t ep_isa_methods[] = {
38652549Smdodd	/* Device interface */
387110835Smdodd#ifdef __i386__
388117700Smarkm	DEVMETHOD(device_identify, ep_isa_identify),
389110835Smdodd#endif
390117700Smarkm	DEVMETHOD(device_probe, ep_isa_probe),
391117700Smarkm	DEVMETHOD(device_attach, ep_isa_attach),
392117700Smarkm	DEVMETHOD(device_detach, ep_detach),
39351673Smdodd
394246128Ssbz	DEVMETHOD_END
39552549Smdodd};
39651673Smdodd
39752549Smdoddstatic driver_t ep_isa_driver = {
39852549Smdodd	"ep",
39952549Smdodd	ep_isa_methods,
40052549Smdodd	sizeof(struct ep_softc),
40152549Smdodd};
40251673Smdodd
40352549Smdoddextern devclass_t ep_devclass;
40451673Smdodd
40552549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
406112794Smdodd#ifdef __i386__
407112794SmdoddMODULE_DEPEND(ep, elink, 1, 1, 1);
408112794Smdodd#endif
409