if_ep_isa.c revision 54200
1226031Sstas/*
2226031Sstas * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas * 3. All advertising materials mentioning features or use of this software
14226031Sstas *    must display the following acknowledgement:
15226031Sstas *      This product includes software developed by Herb Peyerl.
16226031Sstas * 4. The name of Herb Peyerl may not be used to endorse or promote products
17226031Sstas *    derived from this software without specific prior written permission.
18226031Sstas *
19226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20226031Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21226031Sstas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22226031Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23226031Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24226031Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25226031Sstas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26226031Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27226031Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28226031Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29226031Sstas *
30226031Sstas * $FreeBSD: head/sys/dev/ep/if_ep_isa.c 54200 1999-12-06 09:16:13Z mdodd $
31226031Sstas */
32226031Sstas
33226031Sstas#include <sys/param.h>
34226031Sstas#include <sys/systm.h>
35226031Sstas#include <sys/kernel.h>
36226031Sstas#include <sys/socket.h>
37226031Sstas
38226031Sstas#include <sys/module.h>
39226031Sstas#include <sys/bus.h>
40226031Sstas
41226031Sstas#include <machine/bus.h>
42226031Sstas#include <machine/resource.h>
43226031Sstas#include <sys/rman.h>
44226031Sstas
45226031Sstas#include <net/if.h>
46226031Sstas#include <net/if_arp.h>
47226031Sstas#include <net/if_media.h>
48226031Sstas
49226031Sstas#include <machine/clock.h>
50226031Sstas
51226031Sstas#include <isa/isavar.h>
52226031Sstas#include <isa/pnpvar.h>
53226031Sstas
54226031Sstas#include <dev/ep/if_epreg.h>
55226031Sstas#include <dev/ep/if_epvar.h>
56226031Sstas#include <i386/isa/elink.h>
57226031Sstas
58226031Sstasstatic u_int16_t	get_eeprom_data	(int, int);
59226031Sstas
60226031Sstasstatic void		ep_isa_identify	(driver_t *, device_t);
61226031Sstasstatic int		ep_isa_probe	(device_t);
62226031Sstasstatic int		ep_isa_attach	(device_t);
63226031Sstas
64226031Sstasstruct isa_ident {
65226031Sstas	u_int32_t	id;
66226031Sstas	char *		name;
67226031Sstas};
68226031Sstasconst char * ep_isa_match_id (u_int32_t, struct isa_ident *);
69226031Sstas
70226031Sstas#define ISA_ID_3C509_TP    0x506d5090
71226031Sstas#define ISA_ID_3C509_BNC   0x506d5091
72226031Sstas#define ISA_ID_3C509_COMBO 0x506d5094
73226031Sstas#define ISA_ID_3C509_TPO   0x506d5095
74226031Sstas
75226031Sstasstatic struct isa_ident ep_isa_devs[] = {
76226031Sstas	{ ISA_ID_3C509_TP,	"3Com 3C509-TP EtherLink III" },
77226031Sstas	{ ISA_ID_3C509_BNC,	"3Com 3C509-BNC EtherLink III" },
78226031Sstas	{ ISA_ID_3C509_COMBO,	"3Com 3C509-Combo EtherLink III" },
79226031Sstas	{ ISA_ID_3C509_TPO,	"3Com 3C509-TPO EtherLink III" },
80226031Sstas	{ 0,			NULL },
81226031Sstas};
82226031Sstas
83226031Sstasstatic struct isa_pnp_id ep_ids[] = {
84226031Sstas	{ 0x90506d50,	"3Com 3C509B-TP EtherLink III (PnP)" },	/* TCM5090 */
85226031Sstas	{ 0x91506d50,	"3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
86226031Sstas	{ 0x94506d50,	"3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
87226031Sstas	{ 0x95506d50,	"3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
88226031Sstas	{ 0xf780d041,	NULL }, /* PNP80f7 */
89226031Sstas	{ 0,		NULL },
90226031Sstas};
91226031Sstas
92226031Sstas/*
93226031Sstas * We get eeprom data from the id_port given an offset into the eeprom.
94226031Sstas * Basically; after the ID_sequence is sent to all of the cards; they enter
95226031Sstas * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
96226031Sstas * the eeprom data.  We then read the port 16 times and with every read; the
97226031Sstas * cards check for contention (ie: if one card writes a 0 bit and another
98226031Sstas * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
99226031Sstas * compares the data on the bus; if there is a difference then that card goes
100226031Sstas * into ID_WAIT state again). In the meantime; one bit of data is returned in
101226031Sstas * the AX register which is conveniently returned to us by inb().  Hence; we
102226031Sstas * read 16 times getting one bit of data with each read.
103226031Sstas */
104226031Sstas
105226031Sstasstatic u_int16_t
106226031Sstasget_eeprom_data(id_port, offset)
107226031Sstas	int	id_port;
108226031Sstas	int	offset;
109226031Sstas{
110226031Sstas	int		i;
111226031Sstas	u_int16_t	data = 0;
112226031Sstas
113226031Sstas	outb(id_port, EEPROM_CMD_RD|offset);
114226031Sstas	DELAY(BIT_DELAY_MULTIPLE * 1000);
115226031Sstas	for (i = 0; i < 16; i++) {
116226031Sstas		DELAY(50);
117226031Sstas		data = (data << 1) | (inw(id_port) & 1);
118226031Sstas	}
119226031Sstas	return (data);
120226031Sstas}
121226031Sstas
122226031Sstasconst char *
123226031Sstasep_isa_match_id (id, isa_devs)
124226031Sstas	u_int32_t	      id;
125226031Sstas	struct isa_ident *      isa_devs;
126226031Sstas{
127226031Sstas	struct isa_ident *      i = isa_devs;
128226031Sstas	while(i->name != NULL) {
129226031Sstas	       if (id == i->id)
130226031Sstas		      return (i->name);
131226031Sstas	       i++;
132226031Sstas	}
133226031Sstas	return (NULL);
134226031Sstas}
135226031Sstas
136226031Sstasstatic void
137226031Sstasep_isa_identify (driver_t *driver, device_t parent)
138226031Sstas{
139226031Sstas	int		tag = EP_LAST_TAG;
140226031Sstas	int		found = 0;
141226031Sstas	int		i;
142226031Sstas	int		j;
143226031Sstas	const char *	desc;
144226031Sstas	u_int16_t	data;
145226031Sstas	u_int32_t	irq;
146226031Sstas	u_int32_t	ioport;
147226031Sstas	u_int32_t	isa_id;
148226031Sstas	device_t	child;
149226031Sstas
150226031Sstas	outb(ELINK_ID_PORT, 0);
151226031Sstas	outb(ELINK_ID_PORT, 0);
152226031Sstas	elink_idseq(ELINK_509_POLY);
153226031Sstas	elink_reset();
154226031Sstas
155226031Sstas	DELAY(DELAY_MULTIPLE * 10000);
156226031Sstas
157226031Sstas	for (i = 0; i < EP_MAX_BOARDS; i++) {
158226031Sstas
159226031Sstas		outb(ELINK_ID_PORT, 0);
160226031Sstas		outb(ELINK_ID_PORT, 0);
161226031Sstas		elink_idseq(ELINK_509_POLY);
162226031Sstas		DELAY(400);
163226031Sstas
164226031Sstas		/* For the first probe, clear all
165226031Sstas		 * board's tag registers.
166226031Sstas		 * Otherwise kill off already-found
167226031Sstas		 * boards. -- linux 3c509.c
168226031Sstas		 */
169226031Sstas		if (i == 0) {
170226031Sstas			outb(ELINK_ID_PORT, 0xd0);
171226031Sstas		} else {
172226031Sstas			outb(ELINK_ID_PORT, 0xd8);
173226031Sstas		}
174226031Sstas
175226031Sstas		/* Get out of loop if we're out of cards. */
176226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
177226031Sstas		if (data != MFG_ID) {
178226031Sstas			break;
179226031Sstas		}
180226031Sstas
181226031Sstas		/* resolve contention using the Ethernet address */
182226031Sstas		for (j = 0; j < 3; j++) {
183226031Sstas			(void)get_eeprom_data(ELINK_ID_PORT, j);
184226031Sstas		}
185226031Sstas
186226031Sstas		/*
187226031Sstas		 * Construct an 'isa_id' in 'EISA'
188226031Sstas		 * format.
189226031Sstas		 */
190226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
191226031Sstas		isa_id = (htons(data) << 16);
192226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
193226031Sstas		isa_id |= htons(data);
194226031Sstas
195226031Sstas		/* Find known ISA boards */
196226031Sstas		desc = ep_isa_match_id(isa_id, ep_isa_devs);
197226031Sstas		if (!desc) {
198226031Sstas			if (bootverbose) {
199226031Sstas				device_printf(parent, "if_ep: unknown ID 0x%08x\n",
200226031Sstas						isa_id);
201226031Sstas			}
202226031Sstas			break;
203226031Sstas		}
204226031Sstas
205226031Sstas		/* Retreive IRQ */
206226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
207226031Sstas		irq = (data >> 12);
208226031Sstas
209226031Sstas		/* Retreive IOPORT */
210226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
211226031Sstas#ifdef PC98
212226031Sstas		ioport = (((data & 0x1f) * 0x100) + 0x40d0);
213226031Sstas#else
214226031Sstas		ioport = (((data & 0x1f) << 4) + 0x200);
215226031Sstas#endif
216226031Sstas
217226031Sstas		/* Test for an adapter with PnP support. */
218226031Sstas		data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
219226031Sstas		if (data == CAP_ISA) {
220226031Sstas			data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
221226031Sstas			if (data & ICW1_IAS_PNP) {
222226031Sstas				if (bootverbose) {
223226031Sstas					device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n",
224226031Sstas					  	      ioport);
225226031Sstas				}
226226031Sstas				/* Set the adaptor tag so that the next card can be found. */
227226031Sstas				outb(ELINK_ID_PORT, tag--);
228226031Sstas				continue;
229226031Sstas			}
230226031Sstas		}
231226031Sstas
232226031Sstas		/* Set the adaptor tag so that the next card can be found. */
233226031Sstas		outb(ELINK_ID_PORT, tag--);
234226031Sstas
235226031Sstas		/* Activate the adaptor at the EEPROM location. */
236226031Sstas		outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
237226031Sstas
238226031Sstas		/* Test for an adapter in TEST mode. */
239226031Sstas		outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
240226031Sstas		data = inw(ioport + EP_W0_EEPROM_COMMAND);
241226031Sstas		if (data & EEPROM_TST_MODE) {
242226031Sstas			device_printf(parent, "if_ep: Adapter at 0x%03x in TEST mode!  Erase pencil mark.\n",
243226031Sstas			  	      ioport);
244226031Sstas			continue;
245226031Sstas		}
246226031Sstas
247226031Sstas		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
248226031Sstas		device_set_desc_copy(child, desc);
249226031Sstas		device_set_driver(child, driver);
250226031Sstas		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
251226031Sstas		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
252226031Sstas
253226031Sstas		if (bootverbose) {
254226031Sstas			device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
255226031Sstas					desc, ioport, ioport + EP_IOSIZE, irq);
256226031Sstas		}
257226031Sstas
258226031Sstas		found++;
259226031Sstas	}
260226031Sstas
261226031Sstas	return;
262226031Sstas}
263226031Sstas
264226031Sstasstatic int
265226031Sstasep_isa_probe (device_t dev)
266226031Sstas{
267226031Sstas	int	error = 0;
268226031Sstas
269226031Sstas	/* Check isapnp ids */
270226031Sstas	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
271226031Sstas
272226031Sstas	/* If the card had a PnP ID that didn't match any we know about */
273226031Sstas	if (error == ENXIO) {
274226031Sstas	       return (error);
275226031Sstas	}
276226031Sstas
277226031Sstas	/* If we had some other problem. */
278226031Sstas	if (!(error == 0 || error == ENOENT)) {
279226031Sstas		return (error);
280226031Sstas	}
281226031Sstas
282226031Sstas	/* If we have the resources we need then we're good to go. */
283226031Sstas	if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
284226031Sstas	    (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
285226031Sstas		return (0);
286226031Sstas	}
287226031Sstas
288226031Sstas	return (ENXIO);
289226031Sstas}
290226031Sstas
291226031Sstasstatic int
292226031Sstasep_isa_attach (device_t dev)
293226031Sstas{
294226031Sstas	struct ep_softc *	sc = device_get_softc(dev);
295226031Sstas	int			error = 0;
296226031Sstas
297226031Sstas	if ((error = ep_alloc(dev))) {
298226031Sstas		device_printf(dev, "ep_alloc() failed! (%d)\n", error);
299226031Sstas		goto bad;
300226031Sstas	}
301226031Sstas
302226031Sstas	ep_get_media(sc);
303226031Sstas
304226031Sstas	GO_WINDOW(0);
305226031Sstas	SET_IRQ(BASE, rman_get_start(sc->irq));
306226031Sstas
307226031Sstas	if ((error = ep_attach(sc))) {
308226031Sstas		device_printf(dev, "ep_attach() failed! (%d)\n", error);
309226031Sstas		goto bad;
310226031Sstas	}
311226031Sstas
312226031Sstas	if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
313226031Sstas				   sc, &sc->ep_intrhand))) {
314226031Sstas		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
315226031Sstas		goto bad;
316226031Sstas	}
317226031Sstas
318226031Sstas	return (0);
319226031Sstasbad:
320226031Sstas	ep_free(dev);
321226031Sstas	return (error);
322226031Sstas}
323226031Sstas
324226031Sstasstatic device_method_t ep_isa_methods[] = {
325226031Sstas	/* Device interface */
326226031Sstas	DEVMETHOD(device_identify,	ep_isa_identify),
327226031Sstas	DEVMETHOD(device_probe,		ep_isa_probe),
328226031Sstas	DEVMETHOD(device_attach,	ep_isa_attach),
329226031Sstas
330226031Sstas	{ 0, 0 }
331226031Sstas};
332226031Sstas
333226031Sstasstatic driver_t ep_isa_driver = {
334226031Sstas	"ep",
335226031Sstas	ep_isa_methods,
336226031Sstas	sizeof(struct ep_softc),
337226031Sstas};
338226031Sstas
339226031Sstasextern devclass_t ep_devclass;
340226031Sstas
341226031SstasDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);
342226031Sstas