xref: /netbsd-src/sys/dev/isa/if_le_isa.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: if_le_isa.c,v 1.11 1997/06/06 23:43:52 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 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  * 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  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
42  * Copyright (c) 1992, 1993
43  *	The Regents of the University of California.  All rights reserved.
44  *
45  * This code is derived from software contributed to Berkeley by
46  * Ralph Campbell and Rick Macklem.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed by the University of
59  *	California, Berkeley and its contributors.
60  * 4. Neither the name of the University nor the names of its contributors
61  *    may be used to endorse or promote products derived from this software
62  *    without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74  * SUCH DAMAGE.
75  *
76  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
77  */
78 
79 #include "bpfilter.h"
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/mbuf.h>
84 #include <sys/syslog.h>
85 #include <sys/socket.h>
86 #include <sys/device.h>
87 
88 #include <net/if.h>
89 #include <net/if_ether.h>
90 #include <net/if_media.h>
91 
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_inarp.h>
95 #endif
96 
97 #include <vm/vm.h>
98 
99 #include <machine/cpu.h>
100 #include <machine/intr.h>
101 #include <machine/bus.h>
102 
103 #include <dev/isa/isareg.h>
104 #include <dev/isa/isavar.h>
105 #include <dev/isa/isadmavar.h>
106 
107 #include <dev/ic/am7990reg.h>
108 #include <dev/ic/am7990var.h>
109 
110 #include <dev/isa/if_levar.h>
111 
112 static char *card_type[] =
113     { "unknown", "BICC Isolan", "NE2100", "DEPCA", "PCnet-ISA" };
114 
115 int le_isa_probe __P((struct device *, void *, void *));
116 void le_isa_attach __P((struct device *, struct device *, void *));
117 
118 struct cfattach le_isa_ca = {
119 	sizeof(struct le_softc), le_isa_probe, le_isa_attach
120 };
121 
122 int depca_isa_probe __P((struct le_softc *, struct isa_attach_args *));
123 int ne2100_isa_probe __P((struct le_softc *, struct isa_attach_args *));
124 int bicc_isa_probe __P((struct le_softc *, struct isa_attach_args *));
125 int lance_isa_probe __P((struct am7990_softc *));
126 
127 int le_isa_intredge __P((void *));
128 
129 hide void le_isa_wrcsr __P((struct am7990_softc *, u_int16_t, u_int16_t));
130 hide u_int16_t le_isa_rdcsr __P((struct am7990_softc *, u_int16_t));
131 
132 void	depca_copytobuf __P((struct am7990_softc *, void *, int, int));
133 void	depca_copyfrombuf __P((struct am7990_softc *, void *, int, int));
134 void	depca_zerobuf __P((struct am7990_softc *, int, int));
135 
136 #define	LE_ISA_MEMSIZE	16384
137 
138 hide void
139 le_isa_wrcsr(sc, port, val)
140 	struct am7990_softc *sc;
141 	u_int16_t port, val;
142 {
143 	struct le_softc *lesc = (struct le_softc *)sc;
144 	bus_space_tag_t iot = lesc->sc_iot;
145 	bus_space_handle_t ioh = lesc->sc_ioh;
146 
147 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
148 	bus_space_write_2(iot, ioh, lesc->sc_rdp, val);
149 }
150 
151 hide u_int16_t
152 le_isa_rdcsr(sc, port)
153 	struct am7990_softc *sc;
154 	u_int16_t port;
155 {
156 	struct le_softc *lesc = (struct le_softc *)sc;
157 	bus_space_tag_t iot = lesc->sc_iot;
158 	bus_space_handle_t ioh = lesc->sc_ioh;
159 	u_int16_t val;
160 
161 	bus_space_write_2(iot, ioh, lesc->sc_rap, port);
162 	val = bus_space_read_2(iot, ioh, lesc->sc_rdp);
163 	return (val);
164 }
165 
166 int
167 le_isa_probe(parent, match, aux)
168 	struct device *parent;
169 	void *match, *aux;
170 {
171 	struct le_softc *lesc = match;
172 	struct isa_attach_args *ia = aux;
173 
174 	if (bicc_isa_probe(lesc, ia))
175 		return (1);
176 	if (ne2100_isa_probe(lesc, ia))
177 		return (1);
178 	if (depca_isa_probe(lesc, ia))
179 		return (1);
180 
181 	return (0);
182 }
183 
184 int
185 depca_isa_probe(lesc, ia)
186 	struct le_softc *lesc;
187 	struct isa_attach_args *ia;
188 {
189 	struct am7990_softc *sc = &lesc->sc_am7990;
190 	int iobase = ia->ia_iobase, port;
191 	bus_space_tag_t iot = ia->ia_iot;
192 	bus_space_handle_t ioh;
193 	bus_space_handle_t memh;
194 #if 0
195 	u_int32_t sum, rom_sum;
196 	u_int8_t x;
197 #endif
198 	int i;
199 
200 	/* Map i/o space. */
201 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
202 		return 0;
203 
204 	if (ia->ia_maddr == MADDRUNK || ia->ia_msize == -1)
205 		goto bad;
206 
207 	/* Map card RAM. */
208 	if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize,
209 	    0, &memh))
210 		goto bad;
211 
212 	/* Just needed to check mapability; don't need it anymore. */
213 	bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
214 
215 	lesc->sc_iot = iot;
216 	lesc->sc_ioh = ioh;
217 	lesc->sc_rap = DEPCA_RAP;
218 	lesc->sc_rdp = DEPCA_RDP;
219 	lesc->sc_card = DEPCA;
220 
221 	if (lance_isa_probe(sc) == 0)
222 		goto bad;
223 
224 	bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_DUM);
225 
226 	/*
227 	 * Extract the physical MAC address from the ROM.
228 	 *
229 	 * The address PROM is 32 bytes wide, and we access it through
230 	 * a single I/O port.  On each read, it rotates to the next
231 	 * position.  We find the ethernet address by looking for a
232 	 * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
233 	 * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
234 	 * ethernet address and a checksum).
235 	 *
236 	 * It appears that the PROM can be at one of two locations, so
237 	 * we just try both.
238 	 */
239 	port = DEPCA_ADP;
240 	for (i = 0; i < 32; i++)
241 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
242 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
243 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
244 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
245 		    bus_space_read_1(iot, ioh, port) == 0xff &&
246 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
247 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
248 		    bus_space_read_1(iot, ioh, port) == 0xaa)
249 			goto found;
250 	port = DEPCA_ADP + 1;
251 	for (i = 0; i < 32; i++)
252 		if (bus_space_read_1(iot, ioh, port) == 0xff &&
253 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
254 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
255 		    bus_space_read_1(iot, ioh, port) == 0xaa &&
256 		    bus_space_read_1(iot, ioh, port) == 0xff &&
257 		    bus_space_read_1(iot, ioh, port) == 0x00 &&
258 		    bus_space_read_1(iot, ioh, port) == 0x55 &&
259 		    bus_space_read_1(iot, ioh, port) == 0xaa)
260 			goto found;
261 	printf("%s: address not found\n", sc->sc_dev.dv_xname);
262 	goto bad;
263 
264 found:
265 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
266 		sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, port);
267 
268 #if 0
269 	sum =
270 	    (sc->sc_enaddr[0] <<  2) +
271 	    (sc->sc_enaddr[1] << 10) +
272 	    (sc->sc_enaddr[2] <<  1) +
273 	    (sc->sc_enaddr[3] <<  9) +
274 	    (sc->sc_enaddr[4] <<  0) +
275 	    (sc->sc_enaddr[5] <<  8);
276 	sum = (sum & 0xffff) + (sum >> 16);
277 	sum = (sum & 0xffff) + (sum >> 16);
278 
279 	rom_sum = bus_space_read_1(iot, ioh, port);
280 	rom_sum |= bus_space_read_1(iot, ioh, port) << 8;
281 
282 	if (sum != rom_sum) {
283 		printf("%s: checksum mismatch; calculated %04x != read %04x",
284 		    sc->sc_dev.dv_xname, sum, rom_sum);
285 		goto bad;
286 	}
287 #endif
288 
289 	bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_NORMAL);
290 
291 	/*
292 	 * XXX INDIRECT BROKENNESS!
293 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
294 	 */
295 
296 	ia->ia_iosize = 16;
297 	ia->ia_drq = DRQUNK;
298 	return 1;
299 
300  bad:
301 	bus_space_unmap(iot, ioh, 16);
302 	return 0;
303 }
304 
305 int
306 ne2100_isa_probe(lesc, ia)
307 	struct le_softc *lesc;
308 	struct isa_attach_args *ia;
309 {
310 	struct am7990_softc *sc = &lesc->sc_am7990;
311 	int iobase = ia->ia_iobase;
312 	bus_space_tag_t iot = ia->ia_iot;
313 	bus_space_handle_t ioh;
314 	int i;
315 
316 	/* Map i/o space. */
317 	if (bus_space_map(iot, iobase, 24, 0, &ioh))
318 		return 0;
319 
320 	lesc->sc_iot = iot;
321 	lesc->sc_ioh = ioh;
322 	lesc->sc_rap = NE2100_RAP;
323 	lesc->sc_rdp = NE2100_RDP;
324 	lesc->sc_card = NE2100;
325 
326 	if (lance_isa_probe(sc) == 0) {
327 		bus_space_unmap(iot, ioh, 24);
328 		return 0;
329 	}
330 
331 	/*
332 	 * Extract the physical MAC address from the ROM.
333 	 */
334 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
335 		sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i);
336 
337 	/*
338 	 * XXX INDIRECT BROKENNESS!
339 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
340 	 */
341 
342 	ia->ia_iosize = 24;
343 	return 1;
344 }
345 
346 int
347 bicc_isa_probe(lesc, ia)
348 	struct le_softc *lesc;
349 	struct isa_attach_args *ia;
350 {
351 	struct am7990_softc *sc = &lesc->sc_am7990;
352 	int iobase = ia->ia_iobase;
353 	bus_space_tag_t iot = ia->ia_iot;
354 	bus_space_handle_t ioh;
355 	int i;
356 
357 	/* Map i/o space. */
358 	if (bus_space_map(iot, iobase, 16, 0, &ioh))
359 		return 0;
360 
361 	lesc->sc_iot = iot;
362 	lesc->sc_ioh = ioh;
363 	lesc->sc_rap = BICC_RAP;
364 	lesc->sc_rdp = BICC_RDP;
365 	lesc->sc_card = BICC;
366 
367 	if (lance_isa_probe(sc) == 0) {
368 		bus_space_unmap(iot, ioh, 16);
369 		return 0;
370 	}
371 
372 	/*
373 	 * Extract the physical MAC address from the ROM.
374 	 */
375 	for (i = 0; i < sizeof(sc->sc_enaddr); i++)
376 		sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i * 2);
377 
378 	/*
379 	 * XXX INDIRECT BROKENNESS!
380 	 * XXX Should always unmap, and re-map in if_le_isa_attach().
381 	 */
382 
383 	ia->ia_iosize = 16;
384 	return 1;
385 }
386 
387 /*
388  * Determine which chip is present on the card.
389  */
390 int
391 lance_isa_probe(sc)
392 	struct am7990_softc *sc;
393 {
394 
395 	/* Stop the LANCE chip and put it in a known state. */
396 	le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
397 	delay(100);
398 
399 	if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP)
400 		return 0;
401 
402 	le_isa_wrcsr(sc, LE_CSR3, sc->sc_conf3);
403 	return 1;
404 }
405 
406 void
407 le_isa_attach(parent, self, aux)
408 	struct device *parent, *self;
409 	void *aux;
410 {
411 	struct le_softc *lesc = (void *)self;
412 	struct am7990_softc *sc = &lesc->sc_am7990;
413 	struct isa_attach_args *ia = aux;
414 	bus_space_tag_t iot = ia->ia_iot;
415 	bus_space_tag_t memt = ia->ia_memt;
416 	bus_dma_tag_t dmat = ia->ia_dmat;
417 	bus_space_handle_t memh;
418 	bus_dma_segment_t seg;
419 	int rseg;
420 
421 	printf(": %s Ethernet\n", card_type[lesc->sc_card]);
422 
423 	lesc->sc_iot = iot;
424 	lesc->sc_memt = memt;
425 	lesc->sc_dmat = dmat;
426 
427 	/* XXX SHOULD RE-MAP I/O SPACE HERE. */
428 
429 	if (lesc->sc_card == DEPCA) {
430 		u_char val;
431 		int i;
432 
433 		/* Map shared memory. */
434 		if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize,
435 		    0, &memh))
436 			panic("le_isa_attach: can't map shared memory");
437 
438 		lesc->sc_memh = memh;
439 
440 		val = 0xff;
441 		for (;;) {
442 			bus_space_set_region_1(memt, memh, 0, val,
443 			    ia->ia_msize);
444 			for (i = 0; i < ia->ia_msize; i++)
445 				if (bus_space_read_1(memt, memh, 1) != val) {
446 					printf("%s: failed to clear memory\n",
447 					    sc->sc_dev.dv_xname);
448 					return;
449 				}
450 			if (val == 0x00)
451 				break;
452 			val -= 0x55;
453 		}
454 
455 		sc->sc_conf3 = LE_C3_ACON;
456 		sc->sc_mem = 0;			/* Not used. */
457 		sc->sc_addr = 0;
458 		sc->sc_memsize = ia->ia_msize;
459 	} else {
460 		/*
461 		 * Allocate a DMA area for the card.
462 		 */
463 		if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, NBPG, 0, &seg, 1,
464 		    &rseg, BUS_DMA_NOWAIT)) {
465 			printf("%s: couldn't allocate memory for card\n",
466 			    sc->sc_dev.dv_xname);
467 			return;
468 		}
469 		if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
470 		    (caddr_t *)&sc->sc_mem,
471 		    BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC)) {
472 			printf("%s: couldn't map memory for card\n",
473 			    sc->sc_dev.dv_xname);
474 			return;
475 		}
476 
477 		/*
478 		 * Create and load the DMA map for the DMA area.
479 		 */
480 		if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
481 		    LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
482 			printf("%s: couldn't create DMA map\n",
483 			    sc->sc_dev.dv_xname);
484 			bus_dmamem_free(dmat, &seg, rseg);
485 			return;
486 		}
487 		if (bus_dmamap_load(dmat, lesc->sc_dmam,
488 		    sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
489 			printf("%s: coundn't load DMA map\n",
490 			    sc->sc_dev.dv_xname);
491 			bus_dmamem_free(dmat, &seg, rseg);
492 			return;
493 		}
494 
495 		sc->sc_conf3 = 0;
496 		sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
497 		sc->sc_memsize = LE_ISA_MEMSIZE;
498 	}
499 
500 	if (lesc->sc_card == DEPCA) {
501 		sc->sc_copytodesc = depca_copytobuf;
502 		sc->sc_copyfromdesc = depca_copyfrombuf;
503 		sc->sc_copytobuf = depca_copytobuf;
504 		sc->sc_copyfrombuf = depca_copyfrombuf;
505 		sc->sc_zerobuf = depca_zerobuf;
506 	} else {
507 		sc->sc_copytodesc = am7990_copytobuf_contig;
508 		sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
509 		sc->sc_copytobuf = am7990_copytobuf_contig;
510 		sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
511 		sc->sc_zerobuf = am7990_zerobuf_contig;
512 	}
513 
514 	sc->sc_rdcsr = le_isa_rdcsr;
515 	sc->sc_wrcsr = le_isa_wrcsr;
516 	sc->sc_hwinit = NULL;
517 
518 	printf("%s", sc->sc_dev.dv_xname);
519 	am7990_config(sc);
520 
521 	if (ia->ia_drq != DRQUNK)
522 		isa_dmacascade(parent, ia->ia_drq);
523 
524 	lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
525 	    IPL_NET, le_isa_intredge, sc);
526 }
527 
528 /*
529  * Controller interrupt.
530  */
531 int
532 le_isa_intredge(arg)
533 	void *arg;
534 {
535 
536 	if (am7990_intr(arg) == 0)
537 		return (0);
538 	for (;;)
539 		if (am7990_intr(arg) == 0)
540 			return (1);
541 }
542 
543 /*
544  * DEPCA shared memory access functions.
545  */
546 
547 void
548 depca_copytobuf(sc, from, boff, len)
549 	struct am7990_softc *sc;
550 	void *from;
551 	int boff, len;
552 {
553 	struct le_softc *lesc = (struct le_softc *)sc;
554 
555 	bus_space_write_region_1(lesc->sc_memt, lesc->sc_memh, boff,
556 	    from, len);
557 }
558 
559 void
560 depca_copyfrombuf(sc, to, boff, len)
561 	struct am7990_softc *sc;
562 	void *to;
563 	int boff, len;
564 {
565 	struct le_softc *lesc = (struct le_softc *)sc;
566 
567 	bus_space_read_region_1(lesc->sc_memt, lesc->sc_memh, boff,
568 	    to, len);
569 }
570 
571 void
572 depca_zerobuf(sc, boff, len)
573 	struct am7990_softc *sc;
574 	int boff, len;
575 {
576 	struct le_softc *lesc = (struct le_softc *)sc;
577 
578 #if 0	/* XXX !! */
579 	bus_space_set_region_1(lesc->sc_memt, lesc->sc_memh, boff,
580 	    0x00, len);
581 #else
582 	for (; len != 0; boff++, len--)
583 		bus_space_write_1(lesc->sc_memt, lesc->sc_memh, boff, 0x00);
584 #endif
585 }
586