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