xref: /netbsd-src/sys/dev/isa/if_we_isa.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_we_isa.c,v 1.19 2008/03/12 14:31:11 cube Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
42  * adapters.
43  *
44  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
45  *
46  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
47  * copied, distributed, and sold, in both source and binary form provided that
48  * the above copyright and these terms are retained.  Under no circumstances is
49  * the author responsible for the proper functioning of this software, nor does
50  * the author assume any responsibility for damages incurred with its use.
51  */
52 
53 /*
54  * Device driver for the Western Digital/SMC 8003 and 8013 series,
55  * and the SMC Elite Ultra (8216).
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: if_we_isa.c,v 1.19 2008/03/12 14:31:11 cube Exp $");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/device.h>
64 #include <sys/socket.h>
65 #include <sys/mbuf.h>
66 #include <sys/syslog.h>
67 
68 #include <net/if.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_media.h>
72 
73 #include <net/if_ether.h>
74 
75 #include <sys/bus.h>
76 #include <sys/bswap.h>
77 #include <sys/intr.h>
78 
79 #include <dev/isa/isareg.h>
80 #include <dev/isa/isavar.h>
81 
82 #include <dev/ic/dp8390reg.h>
83 #include <dev/ic/dp8390var.h>
84 #include <dev/ic/wereg.h>
85 #include <dev/ic/wevar.h>
86 
87 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
88 #define	bus_space_read_region_stream_2	bus_space_read_region_2
89 #define	bus_space_write_stream_2	bus_space_write_2
90 #define	bus_space_write_region_stream_2	bus_space_write_region_2
91 #endif
92 
93 int	we_isa_probe(device_t, cfdata_t , void *);
94 void	we_isa_attach(device_t, device_t, void *);
95 
96 CFATTACH_DECL_NEW(we_isa, sizeof(struct we_softc),
97     we_isa_probe, we_isa_attach, NULL, NULL);
98 
99 extern struct cfdriver we_cd;
100 
101 static const char *we_params(bus_space_tag_t, bus_space_handle_t,
102 		u_int8_t *, bus_size_t *, u_int8_t *, int *);
103 
104 static const int we_584_irq[] = {
105 	9, 3, 5, 7, 10, 11, 15, 4,
106 };
107 #define	NWE_584_IRQ	(sizeof(we_584_irq) / sizeof(we_584_irq[0]))
108 
109 static const int we_790_irq[] = {
110 	ISA_UNKNOWN_IRQ, 9, 3, 5, 7, 10, 11, 15,
111 };
112 #define	NWE_790_IRQ	(sizeof(we_790_irq) / sizeof(we_790_irq[0]))
113 
114 /*
115  * Delay needed when switching 16-bit access to shared memory.
116  */
117 #define	WE_DELAY(wsc) delay(3)
118 
119 /*
120  * Enable card RAM, and 16-bit access.
121  */
122 #define	WE_MEM_ENABLE(wsc) \
123 do { \
124 	if ((wsc)->sc_16bitp) \
125 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
126 		    WE_LAAR, (wsc)->sc_laar_proto | WE_LAAR_M16EN); \
127 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
128 	    WE_MSR, wsc->sc_msr_proto | WE_MSR_MENB); \
129 	WE_DELAY((wsc)); \
130 } while (0)
131 
132 /*
133  * Disable card RAM, and 16-bit access.
134  */
135 #define	WE_MEM_DISABLE(wsc) \
136 do { \
137 	bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
138 	    WE_MSR, (wsc)->sc_msr_proto); \
139 	if ((wsc)->sc_16bitp) \
140 		bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \
141 		    WE_LAAR, (wsc)->sc_laar_proto); \
142 	WE_DELAY((wsc)); \
143 } while (0)
144 
145 int
146 we_isa_probe(device_t parent, cfdata_t cf, void *aux)
147 {
148 	struct isa_attach_args *ia = aux;
149 	bus_space_tag_t asict, memt;
150 	bus_space_handle_t asich, memh;
151 	bus_size_t memsize;
152 	int asich_valid, memh_valid;
153 	int i, is790, rv = 0;
154 	u_int8_t x, type;
155 
156 	asict = ia->ia_iot;
157 	memt = ia->ia_memt;
158 
159 	asich_valid = memh_valid = 0;
160 
161 	if (ia->ia_nio < 1)
162 		return (0);
163 	if (ia->ia_niomem < 1)
164 		return (0);
165 	if (ia->ia_nirq < 1)
166 		return (0);
167 
168 	if (ISA_DIRECT_CONFIG(ia))
169 		return (0);
170 
171 	/* Disallow wildcarded i/o addresses. */
172 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
173 		return (0);
174 
175 	/* Disallow wildcarded mem address. */
176 	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
177 		return (0);
178 
179 	/* Attempt to map the device. */
180 	if (bus_space_map(asict, ia->ia_io[0].ir_addr, WE_NPORTS, 0, &asich))
181 		goto out;
182 	asich_valid = 1;
183 
184 #ifdef TOSH_ETHER
185 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_POW);
186 #endif
187 
188 	/*
189 	 * Attempt to do a checksum over the station address PROM.
190 	 * If it fails, it's probably not a WD/SMC board.  There is
191 	 * a problem with this, though.  Some clone WD8003E boards
192 	 * (e.g. Danpex) won't pass the checksum.  In this case,
193 	 * the checksum byte always seems to be 0.
194 	 */
195 	for (x = 0, i = 0; i < 8; i++)
196 		x += bus_space_read_1(asict, asich, WE_PROM + i);
197 
198 	if (x != WE_ROM_CHECKSUM_TOTAL) {
199 		/* Make sure it's an 8003E clone... */
200 		if (bus_space_read_1(asict, asich, WE_CARD_ID) !=
201 		    WE_TYPE_WD8003E)
202 			goto out;
203 
204 		/* Check the checksum byte. */
205 		if (bus_space_read_1(asict, asich, WE_PROM + 7) != 0)
206 			goto out;
207 	}
208 
209 	/*
210 	 * Reset the card to force it into a known state.
211 	 */
212 #ifdef TOSH_ETHER
213 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_RST | WE_MSR_POW);
214 #else
215 	bus_space_write_1(asict, asich, WE_MSR, WE_MSR_RST);
216 #endif
217 	delay(100);
218 
219 	bus_space_write_1(asict, asich, WE_MSR,
220 	    bus_space_read_1(asict, asich, WE_MSR) & ~WE_MSR_RST);
221 
222 	/* Wait in case the card is reading it's EEPROM. */
223 	delay(5000);
224 
225 	/*
226 	 * Get parameters.
227 	 */
228 	if (we_params(asict, asich, &type, &memsize, NULL, &is790) == NULL)
229 		goto out;
230 
231 	/* Allow user to override probed value. */
232 	if (ia->ia_iomem[0].ir_size)
233 		memsize = ia->ia_iomem[0].ir_size;
234 
235 	/* Attempt to map the memory space. */
236 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, memsize, 0, &memh))
237 		goto out;
238 	memh_valid = 1;
239 
240 	/*
241 	 * If possible, get the assigned interrupt number from the card
242 	 * and use it.
243 	 */
244 	if (is790) {
245 		u_int8_t hwr;
246 
247 		/* Assemble together the encoded interrupt number. */
248 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
249 		bus_space_write_1(asict, asich, WE790_HWR,
250 		    hwr | WE790_HWR_SWH);
251 
252 		x = bus_space_read_1(asict, asich, WE790_GCR);
253 		i = ((x & WE790_GCR_IR2) >> 4) |
254 		    ((x & (WE790_GCR_IR1|WE790_GCR_IR0)) >> 2);
255 		bus_space_write_1(asict, asich, WE790_HWR,
256 		    hwr & ~WE790_HWR_SWH);
257 
258 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
259 		    ia->ia_irq[0].ir_irq != we_790_irq[i])
260 			aprint_error("%s%d: overriding configured "
261 			    "IRQ %d to %d\n", we_cd.cd_name, cf->cf_unit,
262 			    ia->ia_irq[0].ir_irq, we_790_irq[i]);
263 		ia->ia_irq[0].ir_irq = we_790_irq[i];
264 	} else if (type & WE_SOFTCONFIG) {
265 		/* Assemble together the encoded interrupt number. */
266 		i = (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_IR2) |
267 		    ((bus_space_read_1(asict, asich, WE_IRR) &
268 		      (WE_IRR_IR0 | WE_IRR_IR1)) >> 5);
269 
270 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
271 		    ia->ia_irq[0].ir_irq != we_584_irq[i])
272 			aprint_error("%s%d: overriding configured "
273 			    "IRQ %d to %d\n", we_cd.cd_name, cf->cf_unit,
274 			    ia->ia_irq[0].ir_irq, we_584_irq[i]);
275 		ia->ia_irq[0].ir_irq = we_584_irq[i];
276 	}
277 
278 	/* So, we say we've found it! */
279 	ia->ia_nio = 1;
280 	ia->ia_io[0].ir_size = WE_NPORTS;
281 
282 	ia->ia_niomem = 1;
283 	ia->ia_iomem[0].ir_size = memsize;
284 
285 	ia->ia_nirq = 1;
286 
287 	ia->ia_ndrq = 0;
288 
289 	rv = 1;
290 
291  out:
292 	if (asich_valid)
293 		bus_space_unmap(asict, asich, WE_NPORTS);
294 	if (memh_valid)
295 		bus_space_unmap(memt, memh, memsize);
296 	return (rv);
297 }
298 
299 void
300 we_isa_attach(device_t parent, device_t self, void *aux)
301 {
302 	struct we_softc *wsc = device_private(self);
303 	struct dp8390_softc *sc = &wsc->sc_dp8390;
304 	struct isa_attach_args *ia = aux;
305 	bus_space_tag_t nict, asict, memt;
306 	bus_space_handle_t nich, asich, memh;
307 	const char *typestr;
308 
309 	aprint_normal("\n");
310 
311 	sc->sc_dev = self;
312 	nict = asict = ia->ia_iot;
313 	memt = ia->ia_memt;
314 
315 	/* Map the device. */
316 	if (bus_space_map(asict, ia->ia_io[0].ir_addr, WE_NPORTS, 0, &asich)) {
317 		aprint_error_dev(self, "can't map nic i/o space\n");
318 		return;
319 	}
320 
321 	if (bus_space_subregion(asict, asich, WE_NIC_OFFSET, WE_NIC_NPORTS,
322 	    &nich)) {
323 		aprint_error_dev(self, "can't subregion i/o space\n");
324 		return;
325 	}
326 
327 	typestr = we_params(asict, asich, &wsc->sc_type, NULL,
328 	    &wsc->sc_flags, &sc->is790);
329 	if (typestr == NULL) {
330 		aprint_error_dev(self, "where did the card go?\n");
331 		return;
332 	}
333 
334 	/*
335 	 * Map memory space.  Note we use the size that might have
336 	 * been overridden by the user.
337 	 */
338 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr,
339 	    ia->ia_iomem[0].ir_size, 0, &memh)) {
340 		aprint_error_dev(self, "can't map shared memory\n");
341 		return;
342 	}
343 
344 	wsc->sc_asict = asict;
345 	wsc->sc_asich = asich;
346 
347 	sc->sc_regt = nict;
348 	sc->sc_regh = nich;
349 
350 	sc->sc_buft = memt;
351 	sc->sc_bufh = memh;
352 
353 	wsc->sc_maddr = ia->ia_iomem[0].ir_addr;
354 	sc->mem_size = ia->ia_iomem[0].ir_size;
355 
356 	/* Interface is always enabled. */
357 	sc->sc_enabled = 1;
358 
359 	if (we_config(self, wsc, typestr))
360 		return;
361 
362 	/*
363 	 * Enable the configured interrupt.
364 	 */
365 	if (sc->is790)
366 		bus_space_write_1(asict, asich, WE790_ICR,
367 		    bus_space_read_1(asict, asich, WE790_ICR) |
368 		    WE790_ICR_EIL);
369 	else if (wsc->sc_type & WE_SOFTCONFIG)
370 		bus_space_write_1(asict, asich, WE_IRR,
371 		    bus_space_read_1(asict, asich, WE_IRR) | WE_IRR_IEN);
372 	else if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
373 		aprint_error_dev(self, "can't wildcard IRQ on a %s\n",
374 		    typestr);
375 		return;
376 	}
377 
378 	/* Establish interrupt handler. */
379 	wsc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
380 	    IST_EDGE, IPL_NET, dp8390_intr, sc);
381 	if (wsc->sc_ih == NULL)
382 		aprint_error_dev(self, "can't establish interrupt\n");
383 }
384 
385 static const char *
386 we_params(bus_space_tag_t asict, bus_space_handle_t asich, u_int8_t *typep,
387     bus_size_t *memsizep, u_int8_t *flagp, int *is790p)
388 {
389 	const char *typestr;
390 	bus_size_t memsize;
391 	int is16bit, is790;
392 	u_int8_t type;
393 
394 	memsize = 8192;
395 	is16bit = is790 = 0;
396 
397 	type = bus_space_read_1(asict, asich, WE_CARD_ID);
398 	switch (type) {
399 	case WE_TYPE_WD8003S:
400 		typestr = "WD8003S";
401 		break;
402 	case WE_TYPE_WD8003E:
403 		typestr = "WD8003E";
404 		break;
405 	case WE_TYPE_WD8003EB:
406 		typestr = "WD8003EB";
407 		break;
408 	case WE_TYPE_WD8003W:
409 		typestr = "WD8003W";
410 		break;
411 	case WE_TYPE_WD8013EBT:
412 		typestr = "WD8013EBT";
413 		memsize = 16384;
414 		is16bit = 1;
415 		break;
416 	case WE_TYPE_WD8013W:
417 		typestr = "WD8013W";
418 		memsize = 16384;
419 		is16bit = 1;
420 		break;
421 	case WE_TYPE_WD8013EP:		/* also WD8003EP */
422 		if (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) {
423 			is16bit = 1;
424 			memsize = 16384;
425 			typestr = "WD8013EP";
426 		} else
427 			typestr = "WD8003EP";
428 		break;
429 	case WE_TYPE_WD8013WC:
430 		typestr = "WD8013WC";
431 		memsize = 16384;
432 		is16bit = 1;
433 		break;
434 	case WE_TYPE_WD8013EBP:
435 		typestr = "WD8013EBP";
436 		memsize = 16384;
437 		is16bit = 1;
438 		break;
439 	case WE_TYPE_WD8013EPC:
440 		typestr = "WD8013EPC";
441 		memsize = 16384;
442 		is16bit = 1;
443 		break;
444 	case WE_TYPE_SMC8216C:
445 	case WE_TYPE_SMC8216T:
446 	    {
447 		u_int8_t hwr;
448 
449 		typestr = (type == WE_TYPE_SMC8216C) ?
450 		    "SMC8216/SMC8216C" : "SMC8216T";
451 
452 		hwr = bus_space_read_1(asict, asich, WE790_HWR);
453 		bus_space_write_1(asict, asich, WE790_HWR,
454 		    hwr | WE790_HWR_SWH);
455 		switch (bus_space_read_1(asict, asich, WE790_RAR) &
456 		    WE790_RAR_SZ64) {
457 		case WE790_RAR_SZ64:
458 			memsize = 65536;
459 			break;
460 		case WE790_RAR_SZ32:
461 			memsize = 32768;
462 			break;
463 		case WE790_RAR_SZ16:
464 			memsize = 16384;
465 			break;
466 		case WE790_RAR_SZ8:
467 			/* 8216 has 16K shared mem -- 8416 has 8K */
468 			typestr = (type == WE_TYPE_SMC8216C) ?
469 			    "SMC8416C/SMC8416BT" : "SMC8416T";
470 			memsize = 8192;
471 			break;
472 		}
473 		bus_space_write_1(asict, asich, WE790_HWR, hwr);
474 
475 		is16bit = 1;
476 		is790 = 1;
477 		break;
478 	    }
479 #ifdef TOSH_ETHER
480 	case WE_TYPE_TOSHIBA1:
481 		typestr = "Toshiba1";
482 		memsize = 32768;
483 		is16bit = 1;
484 		break;
485 	case WE_TYPE_TOSHIBA4:
486 		typestr = "Toshiba4";
487 		memsize = 32768;
488 		is16bit = 1;
489 		break;
490 #endif
491 	default:
492 		/* Not one we recognize. */
493 		return (NULL);
494 	}
495 
496 	/*
497 	 * Make some adjustments to initial values depending on what is
498 	 * found in the ICR.
499 	 */
500 	if (is16bit && (type != WE_TYPE_WD8013EBT) &&
501 #ifdef TOSH_ETHER
502 	    (type != WE_TYPE_TOSHIBA1 && type != WE_TYPE_TOSHIBA4) &&
503 #endif
504 	    (bus_space_read_1(asict, asich, WE_ICR) & WE_ICR_16BIT) == 0) {
505 		is16bit = 0;
506 		memsize = 8192;
507 	}
508 
509 #ifdef WE_DEBUG
510 	{
511 		int i;
512 
513 		printf("we_params: type = 0x%x, typestr = %s, is16bit = %d, "
514 		    "memsize = %ld\n", type, typestr, is16bit, (u_long)memsize);
515 		for (i = 0; i < 8; i++)
516 			printf("     %d -> 0x%x\n", i,
517 			    bus_space_read_1(asict, asich, i));
518 	}
519 #endif
520 
521 	if (typep != NULL)
522 		*typep = type;
523 	if (memsizep != NULL)
524 		*memsizep = memsize;
525 	if (flagp != NULL && is16bit)
526 		*flagp |= WE_16BIT_ENABLE;
527 	if (is790p != NULL)
528 		*is790p = is790;
529 	return (typestr);
530 }
531