xref: /openbsd/sys/dev/isa/if_ep_isa.c (revision ffccd32b)
1 /*	$OpenBSD: if_ep_isa.c,v 1.33 2023/09/11 08:41:26 mvs Exp $	*/
2 /*	$NetBSD: if_ep_isa.c,v 1.5 1996/05/12 23:52:36 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Jason R. Thorpe <thorpej@beer.org>
6  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Herb Peyerl.
20  * 4. The name of Herb Peyerl may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Note: Most of the code here was written by Theo de Raadt originally,
35  * ie. all the mechanics of probing for all cards on first call and then
36  * searching for matching devices on subsequent calls.
37  */
38 
39 #include "bpfilter.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/errno.h>
47 #include <sys/syslog.h>
48 #include <sys/timeout.h>
49 #include <sys/device.h>
50 #include <sys/queue.h>
51 
52 #include <net/if.h>
53 #include <net/if_media.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
57 
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 
62 #include <machine/cpu.h>
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 
66 #include <dev/mii/mii.h>
67 #include <dev/mii/miivar.h>
68 
69 #include <dev/ic/elink3var.h>
70 #include <dev/ic/elink3reg.h>
71 
72 #include <dev/isa/isavar.h>
73 #include <dev/isa/elink.h>
74 
75 int ep_isa_probe(struct device *, void *, void *);
76 void ep_isa_attach(struct device *, struct device *, void *);
77 
78 const struct cfattach ep_isa_ca = {
79 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
80 };
81 
82 static	void epaddcard(int, int, int, u_short);
83 
84 /*
85  * This keeps track of which ISAs have been through an ep probe sequence.
86  * A simple static variable isn't enough, since it's conceivable that
87  * a system might have more than one ISA bus.
88  *
89  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
90  * for "isa0".
91  */
92 struct ep_isa_done_probe {
93 	LIST_ENTRY(ep_isa_done_probe)	er_link;
94 	int				er_bus;
95 };
96 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
97 static int ep_isa_probes_initialized;
98 
99 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
100 
101 static struct epcard {
102 	int	bus;
103 	int	iobase;
104 	int	irq;
105 	char	available;
106 	u_short	model;
107 } epcards[MAXEPCARDS];
108 static int nepcards;
109 
110 static void
epaddcard(int bus,int iobase,int irq,u_short model)111 epaddcard(int bus, int iobase, int irq, u_short model)
112 {
113 	if (nepcards >= MAXEPCARDS)
114 		return;
115 	epcards[nepcards].bus = bus;
116 	epcards[nepcards].iobase = iobase;
117 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
118 	epcards[nepcards].available = 1;
119 	epcards[nepcards].model = model;
120 	nepcards++;
121 }
122 
123 /*
124  * 3c509 cards on the ISA bus are probed in ethernet address order.
125  * The probe sequence requires careful orchestration, and we'd like
126  * to allow the irq and base address to be wildcarded. So, we
127  * probe all the cards the first time epprobe() is called. On subsequent
128  * calls we look for matching cards.
129  */
130 int
ep_isa_probe(struct device * parent,void * match,void * aux)131 ep_isa_probe(struct device *parent, void *match, void *aux)
132 {
133 	struct isa_attach_args *ia = aux;
134 	bus_space_tag_t iot = ia->ia_iot;
135 	bus_space_handle_t ioh;
136 	int slot, iobase, irq, i, pnp;
137 	u_int16_t vendor, model;
138 	struct ep_isa_done_probe *er;
139 	int bus = parent->dv_unit;
140 
141 	if (ep_isa_probes_initialized == 0) {
142 		LIST_INIT(&ep_isa_all_probes);
143 		ep_isa_probes_initialized = 1;
144 	}
145 
146 	/*
147 	 * Probe this bus if we haven't done so already.
148 	 */
149 	LIST_FOREACH(er, &ep_isa_all_probes, er_link)
150 		if (er->er_bus == parent->dv_unit)
151 			goto bus_probed;
152 
153 	/*
154 	 * Mark this bus so we don't probe it again.
155 	 */
156 	er = (struct ep_isa_done_probe *)
157 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
158 	if (er == NULL)
159 		panic("ep_isa_probe: can't allocate state storage");
160 
161 	er->er_bus = bus;
162 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
163 
164 	/*
165 	 * Map the Etherlink ID port for the probe sequence.
166 	 */
167 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
168 		printf("ep_isa_probe: can't map Etherlink ID port\n");
169 		return 0;
170 	}
171 
172 	for (slot = 0; slot < MAXEPCARDS; slot++) {
173 		elink_reset(iot, ioh, parent->dv_unit);
174 		elink_idseq(iot, ioh, ELINK_509_POLY);
175 
176 		/* Untag all the adapters so they will talk to us. */
177 		if (slot == 0)
178 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
179 
180 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
181 		if (vendor != MFG_ID)
182 			continue;
183 
184 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
185 		if ((model & 0xfff0) != PROD_ID_3C509) {
186 #ifndef trusted
187 			printf("ep_isa_probe: ignoring model %04x\n",
188 			    model);
189 #endif
190 			continue;
191 		}
192 
193 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
194 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
195 
196 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
197 		irq >>= 12;
198 
199 		pnp = epreadeeprom(iot, ioh, EEPROM_PNP) & 8;
200 
201 		/* so card will not respond to contention again */
202 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
203 
204 		if ((model & 0xfff0) == PROD_ID_3C509 && pnp != 0)
205 			continue;
206 
207 		/*
208 		 * XXX: this should probably not be done here
209 		 * because it enables the drq/irq lines from
210 		 * the board. Perhaps it should be done after
211 		 * we have checked for irq/drq collisions?
212 		 */
213 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
214 		epaddcard(bus, iobase, irq, model);
215 	}
216 	/* XXX should we sort by ethernet address? */
217 
218 	bus_space_unmap(iot, ioh, 1);
219 
220  bus_probed:
221 
222 	for (i = 0; i < nepcards; i++) {
223 		if (epcards[i].bus != bus)
224 			continue;
225 		if (epcards[i].available == 0)
226 			continue;
227 		if (ia->ia_iobase != IOBASEUNK &&
228 		    ia->ia_iobase != epcards[i].iobase)
229 			continue;
230 		if (ia->ia_irq != IRQUNK &&
231 		    ia->ia_irq != epcards[i].irq)
232 			continue;
233 		goto good;
234 	}
235 	return 0;
236 
237 good:
238 	epcards[i].available = 0;
239 	ia->ia_iobase = epcards[i].iobase;
240 	ia->ia_irq = epcards[i].irq;
241 	ia->ia_iosize = 0x10;
242 	ia->ia_msize = 0;
243 	ia->ia_aux = (void *)(long)(epcards[i].model);
244 	return 1;
245 }
246 
247 void
ep_isa_attach(struct device * parent,struct device * self,void * aux)248 ep_isa_attach(struct device *parent, struct device *self, void *aux)
249 {
250 	struct ep_softc *sc = (void *)self;
251 	struct isa_attach_args *ia = aux;
252 	bus_space_tag_t iot = ia->ia_iot;
253 	bus_space_handle_t ioh;
254 	int chipset;
255 
256 	/* Map i/o space. */
257 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
258 		panic("ep_isa_attach: can't map i/o space");
259 
260 	sc->sc_iot = iot;
261 	sc->sc_ioh = ioh;
262 	sc->bustype = EP_BUS_ISA;
263 
264 	printf(":");
265 
266 	chipset = (int)(long)ia->ia_aux;
267 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
268 		epconfig(sc, EP_CHIPSET_3C509, NULL);
269 	} else {
270 		/*
271 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
272 		 * at the moment only for a 3c509.
273 		 */
274 		epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
275 	}
276 
277 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
278 	    IPL_NET, epintr, sc, sc->sc_dev.dv_xname);
279 }
280