if_le_isa.c revision 1.12 1 /* $NetBSD: if_le_isa.c,v 1.12 1997/09/10 03:05:39 mycroft 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 u_int8_t csr;
195 #if 0
196 u_int32_t sum, rom_sum;
197 u_int8_t x;
198 #endif
199 int i;
200
201 /* Map i/o space. */
202 if (bus_space_map(iot, iobase, 16, 0, &ioh))
203 return 0;
204
205 if (ia->ia_maddr == MADDRUNK || ia->ia_msize == -1)
206 goto bad;
207
208 switch (ia->ia_msize) {
209 case 65536:
210 csr = DEPCA_CSR_SHE;
211 break;
212 case 32768:
213 csr = DEPCA_CSR_SHE | DEPCA_CSR_LOW32K;
214 break;
215 default:
216 goto bad;
217 }
218
219 /* Map card RAM. */
220 if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
221 goto bad;
222
223 /* Just needed to check mapability; don't need it anymore. */
224 bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
225
226 lesc->sc_iot = iot;
227 lesc->sc_ioh = ioh;
228 lesc->sc_rap = DEPCA_RAP;
229 lesc->sc_rdp = DEPCA_RDP;
230 lesc->sc_card = DEPCA;
231
232 if (lance_isa_probe(sc) == 0)
233 goto bad;
234
235 bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_DUM);
236
237 /*
238 * Extract the physical MAC address from the ROM.
239 *
240 * The address PROM is 32 bytes wide, and we access it through
241 * a single I/O port. On each read, it rotates to the next
242 * position. We find the ethernet address by looking for a
243 * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff,
244 * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the
245 * ethernet address and a checksum).
246 *
247 * It appears that the PROM can be at one of two locations, so
248 * we just try both.
249 */
250 port = DEPCA_ADP;
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 port = DEPCA_ADP + 1;
262 for (i = 0; i < 32; i++)
263 if (bus_space_read_1(iot, ioh, port) == 0xff &&
264 bus_space_read_1(iot, ioh, port) == 0x00 &&
265 bus_space_read_1(iot, ioh, port) == 0x55 &&
266 bus_space_read_1(iot, ioh, port) == 0xaa &&
267 bus_space_read_1(iot, ioh, port) == 0xff &&
268 bus_space_read_1(iot, ioh, port) == 0x00 &&
269 bus_space_read_1(iot, ioh, port) == 0x55 &&
270 bus_space_read_1(iot, ioh, port) == 0xaa)
271 goto found;
272 printf("%s: address not found\n", sc->sc_dev.dv_xname);
273 goto bad;
274
275 found:
276 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
277 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, port);
278
279 #if 0
280 sum =
281 (sc->sc_enaddr[0] << 2) +
282 (sc->sc_enaddr[1] << 10) +
283 (sc->sc_enaddr[2] << 1) +
284 (sc->sc_enaddr[3] << 9) +
285 (sc->sc_enaddr[4] << 0) +
286 (sc->sc_enaddr[5] << 8);
287 sum = (sum & 0xffff) + (sum >> 16);
288 sum = (sum & 0xffff) + (sum >> 16);
289
290 rom_sum = bus_space_read_1(iot, ioh, port);
291 rom_sum |= bus_space_read_1(iot, ioh, port) << 8;
292
293 if (sum != rom_sum) {
294 printf("%s: checksum mismatch; calculated %04x != read %04x",
295 sc->sc_dev.dv_xname, sum, rom_sum);
296 goto bad;
297 }
298 #endif
299
300 /*
301 * XXX INDIRECT BROKENNESS!
302 * XXX Should always unmap, and re-map in if_le_isa_attach().
303 */
304
305 bus_space_write_1(iot, ioh, DEPCA_CSR, DEPCA_CSR_DUM | csr);
306
307 ia->ia_iosize = 16;
308 ia->ia_drq = DRQUNK;
309 return 1;
310
311 bad:
312 bus_space_unmap(iot, ioh, 16);
313 return 0;
314 }
315
316 int
317 ne2100_isa_probe(lesc, ia)
318 struct le_softc *lesc;
319 struct isa_attach_args *ia;
320 {
321 struct am7990_softc *sc = &lesc->sc_am7990;
322 int iobase = ia->ia_iobase;
323 bus_space_tag_t iot = ia->ia_iot;
324 bus_space_handle_t ioh;
325 int i;
326
327 /* Map i/o space. */
328 if (bus_space_map(iot, iobase, 24, 0, &ioh))
329 return 0;
330
331 lesc->sc_iot = iot;
332 lesc->sc_ioh = ioh;
333 lesc->sc_rap = NE2100_RAP;
334 lesc->sc_rdp = NE2100_RDP;
335 lesc->sc_card = NE2100;
336
337 if (lance_isa_probe(sc) == 0) {
338 bus_space_unmap(iot, ioh, 24);
339 return 0;
340 }
341
342 /*
343 * Extract the physical MAC address from the ROM.
344 */
345 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
346 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i);
347
348 /*
349 * XXX INDIRECT BROKENNESS!
350 * XXX Should always unmap, and re-map in if_le_isa_attach().
351 */
352
353 ia->ia_iosize = 24;
354 return 1;
355 }
356
357 int
358 bicc_isa_probe(lesc, ia)
359 struct le_softc *lesc;
360 struct isa_attach_args *ia;
361 {
362 struct am7990_softc *sc = &lesc->sc_am7990;
363 int iobase = ia->ia_iobase;
364 bus_space_tag_t iot = ia->ia_iot;
365 bus_space_handle_t ioh;
366 int i;
367
368 /* Map i/o space. */
369 if (bus_space_map(iot, iobase, 16, 0, &ioh))
370 return 0;
371
372 lesc->sc_iot = iot;
373 lesc->sc_ioh = ioh;
374 lesc->sc_rap = BICC_RAP;
375 lesc->sc_rdp = BICC_RDP;
376 lesc->sc_card = BICC;
377
378 if (lance_isa_probe(sc) == 0) {
379 bus_space_unmap(iot, ioh, 16);
380 return 0;
381 }
382
383 /*
384 * Extract the physical MAC address from the ROM.
385 */
386 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
387 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i * 2);
388
389 /*
390 * XXX INDIRECT BROKENNESS!
391 * XXX Should always unmap, and re-map in if_le_isa_attach().
392 */
393
394 ia->ia_iosize = 16;
395 return 1;
396 }
397
398 /*
399 * Determine which chip is present on the card.
400 */
401 int
402 lance_isa_probe(sc)
403 struct am7990_softc *sc;
404 {
405
406 /* Stop the LANCE chip and put it in a known state. */
407 le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
408 delay(100);
409
410 if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP)
411 return 0;
412
413 le_isa_wrcsr(sc, LE_CSR3, sc->sc_conf3);
414 return 1;
415 }
416
417 void
418 le_isa_attach(parent, self, aux)
419 struct device *parent, *self;
420 void *aux;
421 {
422 struct le_softc *lesc = (void *)self;
423 struct am7990_softc *sc = &lesc->sc_am7990;
424 struct isa_attach_args *ia = aux;
425 bus_space_tag_t iot = ia->ia_iot;
426 bus_space_tag_t memt = ia->ia_memt;
427 bus_dma_tag_t dmat = ia->ia_dmat;
428 bus_space_handle_t memh;
429 bus_dma_segment_t seg;
430 int rseg;
431
432 printf(": %s Ethernet\n", card_type[lesc->sc_card]);
433
434 lesc->sc_iot = iot;
435 lesc->sc_memt = memt;
436 lesc->sc_dmat = dmat;
437
438 /* XXX SHOULD RE-MAP I/O SPACE HERE. */
439
440 if (lesc->sc_card == DEPCA) {
441 u_char val;
442 int i;
443
444 /* Map shared memory. */
445 if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize,
446 0, &memh))
447 panic("le_isa_attach: can't map shared memory");
448
449 lesc->sc_memh = memh;
450
451 val = 0xff;
452 for (;;) {
453 bus_space_set_region_1(memt, memh, 0, val,
454 ia->ia_msize);
455 for (i = 0; i < ia->ia_msize; i++)
456 if (bus_space_read_1(memt, memh, 1) != val) {
457 printf("%s: failed to clear memory\n",
458 sc->sc_dev.dv_xname);
459 return;
460 }
461 if (val == 0x00)
462 break;
463 val -= 0x55;
464 }
465
466 sc->sc_conf3 = LE_C3_ACON;
467 sc->sc_mem = 0; /* Not used. */
468 sc->sc_addr = 0;
469 sc->sc_memsize = ia->ia_msize;
470 } else {
471 /*
472 * Allocate a DMA area for the card.
473 */
474 if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, NBPG, 0, &seg, 1,
475 &rseg, BUS_DMA_NOWAIT)) {
476 printf("%s: couldn't allocate memory for card\n",
477 sc->sc_dev.dv_xname);
478 return;
479 }
480 if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
481 (caddr_t *)&sc->sc_mem,
482 BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC)) {
483 printf("%s: couldn't map memory for card\n",
484 sc->sc_dev.dv_xname);
485 return;
486 }
487
488 /*
489 * Create and load the DMA map for the DMA area.
490 */
491 if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
492 LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
493 printf("%s: couldn't create DMA map\n",
494 sc->sc_dev.dv_xname);
495 bus_dmamem_free(dmat, &seg, rseg);
496 return;
497 }
498 if (bus_dmamap_load(dmat, lesc->sc_dmam,
499 sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
500 printf("%s: coundn't load DMA map\n",
501 sc->sc_dev.dv_xname);
502 bus_dmamem_free(dmat, &seg, rseg);
503 return;
504 }
505
506 sc->sc_conf3 = 0;
507 sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
508 sc->sc_memsize = LE_ISA_MEMSIZE;
509 }
510
511 if (lesc->sc_card == DEPCA) {
512 sc->sc_copytodesc = depca_copytobuf;
513 sc->sc_copyfromdesc = depca_copyfrombuf;
514 sc->sc_copytobuf = depca_copytobuf;
515 sc->sc_copyfrombuf = depca_copyfrombuf;
516 sc->sc_zerobuf = depca_zerobuf;
517 } else {
518 sc->sc_copytodesc = am7990_copytobuf_contig;
519 sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
520 sc->sc_copytobuf = am7990_copytobuf_contig;
521 sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
522 sc->sc_zerobuf = am7990_zerobuf_contig;
523 }
524
525 sc->sc_rdcsr = le_isa_rdcsr;
526 sc->sc_wrcsr = le_isa_wrcsr;
527 sc->sc_hwinit = NULL;
528
529 printf("%s", sc->sc_dev.dv_xname);
530 am7990_config(sc);
531
532 if (ia->ia_drq != DRQUNK)
533 isa_dmacascade(parent, ia->ia_drq);
534
535 lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
536 IPL_NET, le_isa_intredge, sc);
537 }
538
539 /*
540 * Controller interrupt.
541 */
542 int
543 le_isa_intredge(arg)
544 void *arg;
545 {
546
547 if (am7990_intr(arg) == 0)
548 return (0);
549 for (;;)
550 if (am7990_intr(arg) == 0)
551 return (1);
552 }
553
554 /*
555 * DEPCA shared memory access functions.
556 */
557
558 void
559 depca_copytobuf(sc, from, boff, len)
560 struct am7990_softc *sc;
561 void *from;
562 int boff, len;
563 {
564 struct le_softc *lesc = (struct le_softc *)sc;
565
566 bus_space_write_region_1(lesc->sc_memt, lesc->sc_memh, boff,
567 from, len);
568 }
569
570 void
571 depca_copyfrombuf(sc, to, boff, len)
572 struct am7990_softc *sc;
573 void *to;
574 int boff, len;
575 {
576 struct le_softc *lesc = (struct le_softc *)sc;
577
578 bus_space_read_region_1(lesc->sc_memt, lesc->sc_memh, boff,
579 to, len);
580 }
581
582 void
583 depca_zerobuf(sc, boff, len)
584 struct am7990_softc *sc;
585 int boff, len;
586 {
587 struct le_softc *lesc = (struct le_softc *)sc;
588
589 #if 0 /* XXX !! */
590 bus_space_set_region_1(lesc->sc_memt, lesc->sc_memh, boff,
591 0x00, len);
592 #else
593 for (; len != 0; boff++, len--)
594 bus_space_write_1(lesc->sc_memt, lesc->sc_memh, boff, 0x00);
595 #endif
596 }
597