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