xref: /openbsd/sys/dev/isa/if_ep_isa.c (revision 78b63d65)
1 /*	$OpenBSD: if_ep_isa.c,v 1.19 2000/05/29 18:04:08 aaron 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/select.h>
49 #include <sys/device.h>
50 #include <sys/queue.h>
51 
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 #include <net/netisr.h>
56 #include <net/if_media.h>
57 
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_ether.h>
64 #endif
65 
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #include <net/bpfdesc.h>
69 #endif
70 
71 #include <machine/cpu.h>
72 #include <machine/bus.h>
73 #include <machine/intr.h>
74 
75 #include <dev/mii/mii.h>
76 #include <dev/mii/miivar.h>
77 
78 #include <dev/ic/elink3var.h>
79 #include <dev/ic/elink3reg.h>
80 
81 #include <dev/isa/isavar.h>
82 #include <dev/isa/elink.h>
83 
84 int ep_isa_probe __P((struct device *, void *, void *));
85 void ep_isa_attach __P((struct device *, struct device *, void *));
86 
87 struct cfattach ep_isa_ca = {
88 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
89 };
90 
91 static	void epaddcard __P((int, int, int, u_short));
92 
93 /*
94  * This keeps track of which ISAs have been through an ep probe sequence.
95  * A simple static variable isn't enough, since it's conceivable that
96  * a system might have more than one ISA bus.
97  *
98  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
99  * for "isa0".
100  */
101 struct ep_isa_done_probe {
102 	LIST_ENTRY(ep_isa_done_probe)	er_link;
103 	int				er_bus;
104 };
105 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
106 static int ep_isa_probes_initialized;
107 
108 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
109 
110 static struct epcard {
111 	int	bus;
112 	int	iobase;
113 	int	irq;
114 	char	available;
115 	u_short	model;
116 } epcards[MAXEPCARDS];
117 static int nepcards;
118 
119 static void
120 epaddcard(bus, iobase, irq, model)
121 	int bus, iobase, irq;
122 	u_short model;
123 {
124 
125 	if (nepcards >= MAXEPCARDS)
126 		return;
127 	epcards[nepcards].bus = bus;
128 	epcards[nepcards].iobase = iobase;
129 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
130 	epcards[nepcards].available = 1;
131 	epcards[nepcards].model = model;
132 	nepcards++;
133 }
134 
135 /*
136  * 3c509 cards on the ISA bus are probed in ethernet address order.
137  * The probe sequence requires careful orchestration, and we'd like
138  * like to allow the irq and base address to be wildcarded. So, we
139  * probe all the cards the first time epprobe() is called. On subsequent
140  * calls we look for matching cards.
141  */
142 int
143 ep_isa_probe(parent, match, aux)
144 	struct device *parent;
145 	void *match, *aux;
146 {
147 	struct isa_attach_args *ia = aux;
148 	bus_space_tag_t iot = ia->ia_iot;
149 	bus_space_handle_t ioh;
150 	int slot, iobase, irq, i, pnp;
151 	u_int16_t vendor, model;
152 	struct ep_isa_done_probe *er;
153 	int bus = parent->dv_unit;
154 
155 	if (ep_isa_probes_initialized == 0) {
156 		LIST_INIT(&ep_isa_all_probes);
157 		ep_isa_probes_initialized = 1;
158 	}
159 
160 	/*
161 	 * Probe this bus if we haven't done so already.
162 	 */
163 	for (er = ep_isa_all_probes.lh_first; er != NULL;
164 	    er = er->er_link.le_next)
165 		if (er->er_bus == parent->dv_unit)
166 			goto bus_probed;
167 
168 	/*
169 	 * Mark this bus so we don't probe it again.
170 	 */
171 	er = (struct ep_isa_done_probe *)
172 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
173 	if (er == NULL)
174 		panic("ep_isa_probe: can't allocate state storage");
175 
176 	er->er_bus = bus;
177 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
178 
179 	/*
180 	 * Map the Etherlink ID port for the probe sequence.
181 	 */
182 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
183 		printf("ep_isa_probe: can't map Etherlink ID port\n");
184 		return 0;
185 	}
186 
187 	for (slot = 0; slot < MAXEPCARDS; slot++) {
188 		elink_reset(iot, ioh, parent->dv_unit);
189 		elink_idseq(iot, ioh, ELINK_509_POLY);
190 
191 		/* Untag all the adapters so they will talk to us. */
192 		if (slot == 0)
193 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
194 
195 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
196 		if (vendor != MFG_ID)
197 			continue;
198 
199 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
200 		if ((model & 0xfff0) != PROD_ID_3C509) {
201 #ifndef trusted
202 			printf("ep_isa_probe: ignoring model %04x\n",
203 			    model);
204 #endif
205 			continue;
206 		}
207 
208 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
209 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
210 
211 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
212 		irq >>= 12;
213 
214 		pnp = epreadeeprom(iot, ioh, EEPROM_PNP) & 8;
215 
216 		/* so card will not respond to contention again */
217 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
218 
219 		if ((model & 0xfff0) == PROD_ID_3C509 && pnp != 0)
220 			continue;
221 
222 		/*
223 		 * XXX: this should probably not be done here
224 		 * because it enables the drq/irq lines from
225 		 * the board. Perhaps it should be done after
226 		 * we have checked for irq/drq collisions?
227 		 */
228 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
229 		epaddcard(bus, iobase, irq, model);
230 	}
231 	/* XXX should we sort by ethernet address? */
232 
233 	bus_space_unmap(iot, ioh, 1);
234 
235  bus_probed:
236 
237 	for (i = 0; i < nepcards; i++) {
238 		if (epcards[i].bus != bus)
239 			continue;
240 		if (epcards[i].available == 0)
241 			continue;
242 		if (ia->ia_iobase != IOBASEUNK &&
243 		    ia->ia_iobase != epcards[i].iobase)
244 			continue;
245 		if (ia->ia_irq != IRQUNK &&
246 		    ia->ia_irq != epcards[i].irq)
247 			continue;
248 		goto good;
249 	}
250 	return 0;
251 
252 good:
253 	epcards[i].available = 0;
254 	ia->ia_iobase = epcards[i].iobase;
255 	ia->ia_irq = epcards[i].irq;
256 	ia->ia_iosize = 0x10;
257 	ia->ia_msize = 0;
258 	ia->ia_aux = (void *)(long)(epcards[i].model);
259 	return 1;
260 }
261 
262 void
263 ep_isa_attach(parent, self, aux)
264 	struct device *parent, *self;
265 	void *aux;
266 {
267 	struct ep_softc *sc = (void *)self;
268 	struct isa_attach_args *ia = aux;
269 	bus_space_tag_t iot = ia->ia_iot;
270 	bus_space_handle_t ioh;
271 	int chipset;
272 
273 	/* Map i/o space. */
274 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
275 		panic("ep_isa_attach: can't map i/o space");
276 
277 	sc->sc_iot = iot;
278 	sc->sc_ioh = ioh;
279 	sc->bustype = EP_BUS_ISA;
280 
281 	printf(":");
282 
283 	chipset = (int)(long)ia->ia_aux;
284 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
285 		epconfig(sc, EP_CHIPSET_3C509, NULL);
286 	} else {
287 		/*
288 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
289 		 * at the moment only for a 3c509.
290 		 */
291 		epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
292 	}
293 
294 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
295 	    IPL_NET, epintr, sc, sc->sc_dev.dv_xname);
296 }
297