xref: /openbsd/sys/dev/isa/if_ep_isa.c (revision ffccd32b)
1*ffccd32bSmvs /*	$OpenBSD: if_ep_isa.c,v 1.33 2023/09/11 08:41:26 mvs Exp $	*/
2f1a5e2beSderaadt /*	$NetBSD: if_ep_isa.c,v 1.5 1996/05/12 23:52:36 mycroft Exp $	*/
3aeabe1beSderaadt 
4aeabe1beSderaadt /*
54196a412Sderaadt  * Copyright (c) 1996 Jason R. Thorpe <thorpej@beer.org>
6f1a5e2beSderaadt  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
7aeabe1beSderaadt  * All rights reserved.
8aeabe1beSderaadt  *
9aeabe1beSderaadt  * Redistribution and use in source and binary forms, with or without
10aeabe1beSderaadt  * modification, are permitted provided that the following conditions
11aeabe1beSderaadt  * are met:
12aeabe1beSderaadt  * 1. Redistributions of source code must retain the above copyright
13aeabe1beSderaadt  *    notice, this list of conditions and the following disclaimer.
14aeabe1beSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
15aeabe1beSderaadt  *    notice, this list of conditions and the following disclaimer in the
16aeabe1beSderaadt  *    documentation and/or other materials provided with the distribution.
17aeabe1beSderaadt  * 3. All advertising materials mentioning features or use of this software
18aeabe1beSderaadt  *    must display the following acknowledgement:
19aeabe1beSderaadt  *      This product includes software developed by Herb Peyerl.
20aeabe1beSderaadt  * 4. The name of Herb Peyerl may not be used to endorse or promote products
21aeabe1beSderaadt  *    derived from this software without specific prior written permission.
22aeabe1beSderaadt  *
23aeabe1beSderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24aeabe1beSderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25aeabe1beSderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26aeabe1beSderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27aeabe1beSderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28aeabe1beSderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29aeabe1beSderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30aeabe1beSderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31aeabe1beSderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32aeabe1beSderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
336be574e5Sderaadt  *
346be574e5Sderaadt  * Note: Most of the code here was written by Theo de Raadt originally,
356be574e5Sderaadt  * ie. all the mechanics of probing for all cards on first call and then
366be574e5Sderaadt  * searching for matching devices on subsequent calls.
37aeabe1beSderaadt  */
38aeabe1beSderaadt 
39aeabe1beSderaadt #include "bpfilter.h"
40aeabe1beSderaadt 
41aeabe1beSderaadt #include <sys/param.h>
424196a412Sderaadt #include <sys/systm.h>
43aeabe1beSderaadt #include <sys/mbuf.h>
44aeabe1beSderaadt #include <sys/socket.h>
45aeabe1beSderaadt #include <sys/ioctl.h>
46aeabe1beSderaadt #include <sys/errno.h>
47aeabe1beSderaadt #include <sys/syslog.h>
4806a9f16dSbrad #include <sys/timeout.h>
49aeabe1beSderaadt #include <sys/device.h>
504196a412Sderaadt #include <sys/queue.h>
51aeabe1beSderaadt 
52aeabe1beSderaadt #include <net/if.h>
53784e2db4Smaja #include <net/if_media.h>
54aeabe1beSderaadt 
55aeabe1beSderaadt #include <netinet/in.h>
56aeabe1beSderaadt #include <netinet/if_ether.h>
57aeabe1beSderaadt 
58aeabe1beSderaadt #if NBPFILTER > 0
59aeabe1beSderaadt #include <net/bpf.h>
60aeabe1beSderaadt #endif
61aeabe1beSderaadt 
62aeabe1beSderaadt #include <machine/cpu.h>
63b9a85264Sniklas #include <machine/bus.h>
64f1a5e2beSderaadt #include <machine/intr.h>
65aeabe1beSderaadt 
665b5c4e1aSaaron #include <dev/mii/mii.h>
675b5c4e1aSaaron #include <dev/mii/miivar.h>
685b5c4e1aSaaron 
69aeabe1beSderaadt #include <dev/ic/elink3var.h>
70aeabe1beSderaadt #include <dev/ic/elink3reg.h>
71aeabe1beSderaadt 
72aeabe1beSderaadt #include <dev/isa/isavar.h>
73aeabe1beSderaadt #include <dev/isa/elink.h>
74aeabe1beSderaadt 
75c4071fd1Smillert int ep_isa_probe(struct device *, void *, void *);
76c4071fd1Smillert void ep_isa_attach(struct device *, struct device *, void *);
77aeabe1beSderaadt 
78471aeecfSnaddy const struct cfattach ep_isa_ca = {
79aeabe1beSderaadt 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
80aeabe1beSderaadt };
81aeabe1beSderaadt 
82c4071fd1Smillert static	void epaddcard(int, int, int, u_short);
83aeabe1beSderaadt 
844196a412Sderaadt /*
854196a412Sderaadt  * This keeps track of which ISAs have been through an ep probe sequence.
864196a412Sderaadt  * A simple static variable isn't enough, since it's conceivable that
874196a412Sderaadt  * a system might have more than one ISA bus.
884196a412Sderaadt  *
894196a412Sderaadt  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
904196a412Sderaadt  * for "isa0".
914196a412Sderaadt  */
924196a412Sderaadt struct ep_isa_done_probe {
934196a412Sderaadt 	LIST_ENTRY(ep_isa_done_probe)	er_link;
944196a412Sderaadt 	int				er_bus;
954196a412Sderaadt };
964196a412Sderaadt static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
974196a412Sderaadt static int ep_isa_probes_initialized;
984196a412Sderaadt 
994196a412Sderaadt #define MAXEPCARDS	20	/* if you have more than 20, you lose */
100aeabe1beSderaadt 
101aeabe1beSderaadt static struct epcard {
1024196a412Sderaadt 	int	bus;
103aeabe1beSderaadt 	int	iobase;
104aeabe1beSderaadt 	int	irq;
105aeabe1beSderaadt 	char	available;
106050e531cSderaadt 	u_short	model;
107aeabe1beSderaadt } epcards[MAXEPCARDS];
108aeabe1beSderaadt static int nepcards;
109aeabe1beSderaadt 
110aeabe1beSderaadt static void
epaddcard(int bus,int iobase,int irq,u_short model)111ab2e8934Sbrad epaddcard(int bus, int iobase, int irq, u_short model)
112aeabe1beSderaadt {
113aeabe1beSderaadt 	if (nepcards >= MAXEPCARDS)
114aeabe1beSderaadt 		return;
1154196a412Sderaadt 	epcards[nepcards].bus = bus;
116aeabe1beSderaadt 	epcards[nepcards].iobase = iobase;
117aeabe1beSderaadt 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
118aeabe1beSderaadt 	epcards[nepcards].available = 1;
119050e531cSderaadt 	epcards[nepcards].model = model;
120aeabe1beSderaadt 	nepcards++;
121aeabe1beSderaadt }
122aeabe1beSderaadt 
123aeabe1beSderaadt /*
124aeabe1beSderaadt  * 3c509 cards on the ISA bus are probed in ethernet address order.
125aeabe1beSderaadt  * The probe sequence requires careful orchestration, and we'd like
126a9ef5032Skrw  * to allow the irq and base address to be wildcarded. So, we
127aeabe1beSderaadt  * probe all the cards the first time epprobe() is called. On subsequent
128aeabe1beSderaadt  * calls we look for matching cards.
129aeabe1beSderaadt  */
130aeabe1beSderaadt int
ep_isa_probe(struct device * parent,void * match,void * aux)131ab2e8934Sbrad ep_isa_probe(struct device *parent, void *match, void *aux)
132aeabe1beSderaadt {
133aeabe1beSderaadt 	struct isa_attach_args *ia = aux;
134b9a85264Sniklas 	bus_space_tag_t iot = ia->ia_iot;
135f724e6e4Sderaadt 	bus_space_handle_t ioh;
136f724e6e4Sderaadt 	int slot, iobase, irq, i, pnp;
1374196a412Sderaadt 	u_int16_t vendor, model;
1384196a412Sderaadt 	struct ep_isa_done_probe *er;
1394196a412Sderaadt 	int bus = parent->dv_unit;
140aeabe1beSderaadt 
1414196a412Sderaadt 	if (ep_isa_probes_initialized == 0) {
1424196a412Sderaadt 		LIST_INIT(&ep_isa_all_probes);
1434196a412Sderaadt 		ep_isa_probes_initialized = 1;
1444196a412Sderaadt 	}
1454196a412Sderaadt 
1464196a412Sderaadt 	/*
1474196a412Sderaadt 	 * Probe this bus if we haven't done so already.
1484196a412Sderaadt 	 */
1499d08f8e5Smiod 	LIST_FOREACH(er, &ep_isa_all_probes, er_link)
1504196a412Sderaadt 		if (er->er_bus == parent->dv_unit)
1514196a412Sderaadt 			goto bus_probed;
1524196a412Sderaadt 
1534196a412Sderaadt 	/*
1544196a412Sderaadt 	 * Mark this bus so we don't probe it again.
1554196a412Sderaadt 	 */
1564196a412Sderaadt 	er = (struct ep_isa_done_probe *)
1574196a412Sderaadt 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
1584196a412Sderaadt 	if (er == NULL)
1594196a412Sderaadt 		panic("ep_isa_probe: can't allocate state storage");
1604196a412Sderaadt 
1614196a412Sderaadt 	er->er_bus = bus;
1624196a412Sderaadt 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
1634196a412Sderaadt 
1644196a412Sderaadt 	/*
1654196a412Sderaadt 	 * Map the Etherlink ID port for the probe sequence.
1664196a412Sderaadt 	 */
167b9a85264Sniklas 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
1684196a412Sderaadt 		printf("ep_isa_probe: can't map Etherlink ID port\n");
1694196a412Sderaadt 		return 0;
1704196a412Sderaadt 	}
171aeabe1beSderaadt 
172aeabe1beSderaadt 	for (slot = 0; slot < MAXEPCARDS; slot++) {
173b9a85264Sniklas 		elink_reset(iot, ioh, parent->dv_unit);
174b9a85264Sniklas 		elink_idseq(iot, ioh, ELINK_509_POLY);
175aeabe1beSderaadt 
176aeabe1beSderaadt 		/* Untag all the adapters so they will talk to us. */
177aeabe1beSderaadt 		if (slot == 0)
178b9a85264Sniklas 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
179aeabe1beSderaadt 
180b9a85264Sniklas 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
181aeabe1beSderaadt 		if (vendor != MFG_ID)
182aeabe1beSderaadt 			continue;
183aeabe1beSderaadt 
184b9a85264Sniklas 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
185c3ae1c22Sniklas 		if ((model & 0xfff0) != PROD_ID_3C509) {
186aeabe1beSderaadt #ifndef trusted
1872c7926dbSderaadt 			printf("ep_isa_probe: ignoring model %04x\n",
1882c7926dbSderaadt 			    model);
189aeabe1beSderaadt #endif
190aeabe1beSderaadt 			continue;
191aeabe1beSderaadt 		}
192aeabe1beSderaadt 
193b9a85264Sniklas 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
194aeabe1beSderaadt 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
195aeabe1beSderaadt 
196b9a85264Sniklas 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
197aeabe1beSderaadt 		irq >>= 12;
198aeabe1beSderaadt 
199f724e6e4Sderaadt 		pnp = epreadeeprom(iot, ioh, EEPROM_PNP) & 8;
200f724e6e4Sderaadt 
201aeabe1beSderaadt 		/* so card will not respond to contention again */
202b9a85264Sniklas 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
203aeabe1beSderaadt 
204f724e6e4Sderaadt 		if ((model & 0xfff0) == PROD_ID_3C509 && pnp != 0)
205f724e6e4Sderaadt 			continue;
206f724e6e4Sderaadt 
207aeabe1beSderaadt 		/*
208aeabe1beSderaadt 		 * XXX: this should probably not be done here
209aeabe1beSderaadt 		 * because it enables the drq/irq lines from
210aeabe1beSderaadt 		 * the board. Perhaps it should be done after
211aeabe1beSderaadt 		 * we have checked for irq/drq collisions?
212aeabe1beSderaadt 		 */
213b9a85264Sniklas 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
214050e531cSderaadt 		epaddcard(bus, iobase, irq, model);
215aeabe1beSderaadt 	}
216aeabe1beSderaadt 	/* XXX should we sort by ethernet address? */
2174196a412Sderaadt 
218b9a85264Sniklas 	bus_space_unmap(iot, ioh, 1);
2194196a412Sderaadt 
2204196a412Sderaadt  bus_probed:
221aeabe1beSderaadt 
222aeabe1beSderaadt 	for (i = 0; i < nepcards; i++) {
2234196a412Sderaadt 		if (epcards[i].bus != bus)
2244196a412Sderaadt 			continue;
225aeabe1beSderaadt 		if (epcards[i].available == 0)
226aeabe1beSderaadt 			continue;
227aeabe1beSderaadt 		if (ia->ia_iobase != IOBASEUNK &&
228aeabe1beSderaadt 		    ia->ia_iobase != epcards[i].iobase)
229aeabe1beSderaadt 			continue;
230aeabe1beSderaadt 		if (ia->ia_irq != IRQUNK &&
231aeabe1beSderaadt 		    ia->ia_irq != epcards[i].irq)
232aeabe1beSderaadt 			continue;
233aeabe1beSderaadt 		goto good;
234aeabe1beSderaadt 	}
235aeabe1beSderaadt 	return 0;
236aeabe1beSderaadt 
237aeabe1beSderaadt good:
238aeabe1beSderaadt 	epcards[i].available = 0;
239aeabe1beSderaadt 	ia->ia_iobase = epcards[i].iobase;
240aeabe1beSderaadt 	ia->ia_irq = epcards[i].irq;
241aeabe1beSderaadt 	ia->ia_iosize = 0x10;
242aeabe1beSderaadt 	ia->ia_msize = 0;
243050e531cSderaadt 	ia->ia_aux = (void *)(long)(epcards[i].model);
244aeabe1beSderaadt 	return 1;
245aeabe1beSderaadt }
246aeabe1beSderaadt 
247aeabe1beSderaadt void
ep_isa_attach(struct device * parent,struct device * self,void * aux)248ab2e8934Sbrad ep_isa_attach(struct device *parent, struct device *self, void *aux)
249aeabe1beSderaadt {
250aeabe1beSderaadt 	struct ep_softc *sc = (void *)self;
251aeabe1beSderaadt 	struct isa_attach_args *ia = aux;
252b9a85264Sniklas 	bus_space_tag_t iot = ia->ia_iot;
253b9a85264Sniklas 	bus_space_handle_t ioh;
254c3ae1c22Sniklas 	int chipset;
255aeabe1beSderaadt 
2564196a412Sderaadt 	/* Map i/o space. */
257b9a85264Sniklas 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
2584196a412Sderaadt 		panic("ep_isa_attach: can't map i/o space");
2594196a412Sderaadt 
260b9a85264Sniklas 	sc->sc_iot = iot;
2614196a412Sderaadt 	sc->sc_ioh = ioh;
262aeabe1beSderaadt 	sc->bustype = EP_BUS_ISA;
263aeabe1beSderaadt 
26441265143Sderaadt 	printf(":");
26541265143Sderaadt 
266c3ae1c22Sniklas 	chipset = (int)(long)ia->ia_aux;
267c3ae1c22Sniklas 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
268b7e0ee72Sfgsch 		epconfig(sc, EP_CHIPSET_3C509, NULL);
269c3ae1c22Sniklas 	} else {
270c3ae1c22Sniklas 		/*
271c3ae1c22Sniklas 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
272c3ae1c22Sniklas 		 * at the moment only for a 3c509.
273c3ae1c22Sniklas 		 */
274b7e0ee72Sfgsch 		epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
275c3ae1c22Sniklas 	}
276aeabe1beSderaadt 
277aeabe1beSderaadt 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
278aeabe1beSderaadt 	    IPL_NET, epintr, sc, sc->sc_dev.dv_xname);
279aeabe1beSderaadt }
280