xref: /netbsd-src/sys/dev/isa/if_ep_isa.c (revision 2ed6366cda243ece801ac78e1a856b6eeb476d6e)
1 /*	$NetBSD: if_ep_isa.c,v 1.47 2019/12/27 04:50:41 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
35  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by Herb Peyerl.
49  * 4. The name of Herb Peyerl may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: if_ep_isa.c,v 1.47 2019/12/27 04:50:41 msaitoh Exp $");
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/mbuf.h>
70 #include <sys/socket.h>
71 #include <sys/ioctl.h>
72 #include <sys/errno.h>
73 #include <sys/syslog.h>
74 #include <sys/select.h>
75 #include <sys/device.h>
76 #include <sys/queue.h>
77 
78 #include <net/if.h>
79 #include <net/if_dl.h>
80 #include <net/if_ether.h>
81 #include <net/if_media.h>
82 
83 #include <sys/cpu.h>
84 #include <sys/bus.h>
85 #include <sys/intr.h>
86 
87 #include <dev/mii/miivar.h>
88 
89 #include <dev/ic/elink3var.h>
90 #include <dev/ic/elink3reg.h>
91 
92 #include <dev/isa/isavar.h>
93 #include <dev/isa/elink.h>
94 
95 int ep_isa_probe(device_t , cfdata_t , void *);
96 void ep_isa_attach(device_t , device_t , void *);
97 
98 CFATTACH_DECL_NEW(ep_isa, sizeof(struct ep_softc),
99     ep_isa_probe, ep_isa_attach, NULL, NULL);
100 
101 static	void epaddcard(int, int, int, int);
102 
103 /*
104  * This keeps track of which ISAs have been through an ep probe sequence.
105  * A simple static variable isn't enough, since it's conceivable that
106  * a system might have more than one ISA bus.
107  *
108  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
109  * for "isa0".
110  */
111 struct ep_isa_done_probe {
112 	LIST_ENTRY(ep_isa_done_probe)	er_link;
113 	int				er_bus;
114 };
115 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
116 static int ep_isa_probes_initialized;
117 
118 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
119 
120 static struct epcard {
121 	int	bus;
122 	int	iobase;
123 	int	irq;
124 	char	available;
125 	long	model;
126 } epcards[MAXEPCARDS];
127 static int nepcards;
128 
129 static void
epaddcard(int bus,int iobase,int irq,int model)130 epaddcard(int bus, int iobase, int irq, int model)
131 {
132 
133 	if (nepcards >= MAXEPCARDS)
134 		return;
135 	epcards[nepcards].bus = bus;
136 	epcards[nepcards].iobase = iobase;
137 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
138 	epcards[nepcards].model = model;
139 	epcards[nepcards].available = 1;
140 	nepcards++;
141 }
142 
143 /*
144  * 3c509 cards on the ISA bus are probed in ethernet address order.
145  * The probe sequence requires careful orchestration, and we'd like
146  * to allow the irq and base address to be wildcarded. So, we
147  * probe all the cards the first time epprobe() is called. On subsequent
148  * calls we look for matching cards.
149  */
150 int
ep_isa_probe(device_t parent,cfdata_t match,void * aux)151 ep_isa_probe(device_t parent, cfdata_t match, void *aux)
152 {
153 	struct isa_attach_args *ia = aux;
154 	bus_space_tag_t iot = ia->ia_iot;
155 	bus_space_handle_t ioh;
156 	int slot, iobase, irq, i;
157 	u_int16_t vendor, model, eeprom_addr_cfg;
158 	struct ep_isa_done_probe *er;
159 	int bus = device_unit(parent);
160 
161 	if (ISA_DIRECT_CONFIG(ia))
162 		return (0);
163 
164 	if (ep_isa_probes_initialized == 0) {
165 		LIST_INIT(&ep_isa_all_probes);
166 		ep_isa_probes_initialized = 1;
167 	}
168 
169 	/*
170 	 * Probe this bus if we haven't done so already.
171 	 */
172 	for (er = ep_isa_all_probes.lh_first; er != NULL;
173 	    er = er->er_link.le_next)
174 		if (er->er_bus == device_unit(parent))
175 			goto bus_probed;
176 
177 	/*
178 	 * Mark this bus so we don't probe it again.
179 	 */
180 	er = malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_WAITOK);
181 	er->er_bus = bus;
182 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
183 
184 	/*
185 	 * Map the Etherlink ID port for the probe sequence.
186 	 */
187 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
188 		printf("ep_isa_probe: can't map Etherlink ID port\n");
189 		return 0;
190 	}
191 
192 	for (slot = 0; slot < MAXEPCARDS; slot++) {
193 		elink_reset(iot, ioh, device_unit(parent));
194 		elink_idseq(iot, ioh, ELINK_509_POLY);
195 
196 		/* Untag all the adapters so they will talk to us. */
197 		if (slot == 0)
198 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
199 
200 		vendor = bswap16(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
201 		if (vendor != MFG_ID)
202 			continue;
203 
204 		model = bswap16(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
205 		/*
206 		 * XXX: Add a new product id to check for other cards
207 		 * (3c515?) and fix the check in ep_isa_attach.
208 		 */
209 		if ((model & 0xfff0) != PROD_ID_3C509) {
210 #ifndef trusted
211 			printf(
212 			 "ep_isa_probe: ignoring model %04x\n", model);
213 #endif
214 			continue;
215 			}
216 
217 		eeprom_addr_cfg = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
218 		iobase = (eeprom_addr_cfg & 0x1f) * 0x10 + 0x200;
219 
220 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
221 		irq >>= 12;
222 
223 		/* XXX Should ignore card if non-ISA(EISA) io address? -chb */
224 
225 		/*
226 		 * Don't attach a 3c509 in PnP mode.
227 		 * PnP mode was added with the 3C509B.
228 		 * Check some EEPROM registers to make sure this is really
229 		 * a 3C509B and test whether it is in PnP mode.
230 		 */
231 		if ((model & 0xfff0) == PROD_ID_3C509) {
232 			u_int16_t cksum, eepromrev, eeprom_cap, eeprom_hi;
233 
234 
235 			/*
236 			 * Fetch all the info we need to ascertain whether
237 			 * the card is  PnP capable and in PnP mode.
238 			 * Skip over PnP cards.
239 			 */
240 
241 			/* secondary configurable data checksum */
242 			cksum = epreadeeprom(iot, ioh, EEPROM_CHECKSUM_EL3)
243 			    & 0xFF;
244 			for (i = EEPROM_CONFIG_HIGH;
245 			    i < EEPROM_CHECKSUM_EL3; i++) {
246 				cksum ^= epreadeeprom(iot, ioh, i);
247 			}
248 			cksum = (cksum & 0xFF) ^ ((cksum >> 8) & 0xFF);
249 
250 			eepromrev = epreadeeprom(iot, ioh, EEPROM_SSI);
251 			eeprom_hi = epreadeeprom(iot, ioh, EEPROM_CONFIG_HIGH);
252 			eeprom_cap = epreadeeprom(iot, ioh, EEPROM_CAP);
253 
254 			/*
255 			 * Stop card responding to contention in future.
256 			 * (NB: stops rsponse to all reads from ID port.)
257 			 */
258 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
259 
260 			if (cksum != 0) {
261 #if 0
262 				printf("ep_isa_probe: cksum mismatch 0x%02x\n",
263 				    (int)cksum);
264 #endif
265 			}
266 			else if ((eepromrev & 0xF) < 1) {
267 				/* 3C509B is adapter revision level 1. */
268 #if 0
269 				printf("ep_isa_probe: revision level 0\n");
270 #endif
271 			}
272 			else if (eeprom_cap != 0x2083) {
273 #if 0
274 				printf("ep_isa_probe: capabilities word mismatch0x%03x\n",
275 				    (int)epreadeeprom(iot, ioh, EEPROM_CAP));
276 #endif
277 			}
278 			else
279 			  /*
280 			   * we have a 3c509B with PnP capabilities.
281 			   * Test partly documented bits which toggle when
282 			   * in PnP mode.
283 			   */
284 			if ((eeprom_hi & 0x8) != 0 || ((eeprom_hi & 0xc) == 0 &&
285 			    (eeprom_addr_cfg & 0x80) != 0)) {
286 				printf("3COM 3C509B Ethernet card in PnP mode\n");
287 				continue;
288 			}
289 		}
290 
291 		/*
292 		 * XXX: this should probably not be done here
293 		 * because it enables the drq/irq lines from
294 		 * the board. Perhaps it should be done after
295 		 * we have checked for irq/drq collisions?
296 		 *
297 		 * According to the 3COM docs, this does not enable
298 		 * the irq lines. -chb
299 		 */
300 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
301 
302 		epaddcard(bus, iobase, irq, model);
303 	}
304 	/* XXX should we sort by ethernet address? */
305 
306 	bus_space_unmap(iot, ioh, 1);
307 
308 bus_probed:
309 
310 	if (ia->ia_nio < 1)
311 		return (0);
312 	if (ia->ia_nirq < 1)
313 		return (0);
314 
315 	for (i = 0; i < nepcards; i++) {
316 		if (epcards[i].bus != bus)
317 			continue;
318 		if (epcards[i].available == 0)
319 			continue;
320 
321 		if (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
322 		    ia->ia_io[0].ir_addr != epcards[i].iobase)
323 			continue;
324 
325 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
326 		    ia->ia_irq[0].ir_irq != epcards[i].irq)
327 			continue;
328 
329 		goto good;
330 	}
331 	return 0;
332 
333 good:
334 	epcards[i].available = 0;
335 
336 	ia->ia_nio = 1;
337 	ia->ia_io[0].ir_addr = epcards[i].iobase;
338 	ia->ia_io[0].ir_size = 0x10;
339 
340 	ia->ia_nirq = 1;
341 	ia->ia_irq[0].ir_irq = epcards[i].irq;
342 
343 	ia->ia_niomem = 0;
344 	ia->ia_ndrq = 0;
345 
346 	ia->ia_aux = (void *)epcards[i].model;
347 	return 1;
348 }
349 
350 void
ep_isa_attach(device_t parent,device_t self,void * aux)351 ep_isa_attach(device_t parent, device_t self, void *aux)
352 {
353 	struct ep_softc *sc = device_private(self);
354 	struct isa_attach_args *ia = aux;
355 	bus_space_tag_t iot = ia->ia_iot;
356 	bus_space_handle_t ioh;
357 	int chipset;
358 
359 	/* Map i/o space. */
360 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, 0x10, 0, &ioh)) {
361 		aprint_error(": can't map i/o space\n");
362 		return;
363 	}
364 
365 	sc->sc_dev = self;
366 	sc->sc_iot = iot;
367 	sc->sc_ioh = ioh;
368 	sc->bustype = ELINK_BUS_ISA;
369 
370 	sc->enable = NULL;
371 	sc->disable = NULL;
372 	sc->enabled = 1;
373 
374 	chipset = (int)(long)ia->ia_aux;
375 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
376 		aprint_normal(": 3Com 3C509 Ethernet\n");
377 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
378 	} else {
379 		/*
380 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
381 		 * at the moment only for a 3c509.
382 		 */
383 		aprint_error(": unknown 3Com Ethernet card: %04x\n", chipset);
384 		return;
385 	}
386 
387 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
388 	    IST_EDGE, IPL_NET, epintr, sc);
389 }
390