if_le_isa.c revision 1.13 1 /* $NetBSD: if_le_isa.c,v 1.13 1997/09/10 03:31:31 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 | DEPCA_CSR_IEN |
306 csr);
307
308 ia->ia_iosize = 16;
309 ia->ia_drq = DRQUNK;
310 return 1;
311
312 bad:
313 bus_space_unmap(iot, ioh, 16);
314 return 0;
315 }
316
317 int
318 ne2100_isa_probe(lesc, ia)
319 struct le_softc *lesc;
320 struct isa_attach_args *ia;
321 {
322 struct am7990_softc *sc = &lesc->sc_am7990;
323 int iobase = ia->ia_iobase;
324 bus_space_tag_t iot = ia->ia_iot;
325 bus_space_handle_t ioh;
326 int i;
327
328 /* Map i/o space. */
329 if (bus_space_map(iot, iobase, 24, 0, &ioh))
330 return 0;
331
332 lesc->sc_iot = iot;
333 lesc->sc_ioh = ioh;
334 lesc->sc_rap = NE2100_RAP;
335 lesc->sc_rdp = NE2100_RDP;
336 lesc->sc_card = NE2100;
337
338 if (lance_isa_probe(sc) == 0) {
339 bus_space_unmap(iot, ioh, 24);
340 return 0;
341 }
342
343 /*
344 * Extract the physical MAC address from the ROM.
345 */
346 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
347 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i);
348
349 /*
350 * XXX INDIRECT BROKENNESS!
351 * XXX Should always unmap, and re-map in if_le_isa_attach().
352 */
353
354 ia->ia_iosize = 24;
355 return 1;
356 }
357
358 int
359 bicc_isa_probe(lesc, ia)
360 struct le_softc *lesc;
361 struct isa_attach_args *ia;
362 {
363 struct am7990_softc *sc = &lesc->sc_am7990;
364 int iobase = ia->ia_iobase;
365 bus_space_tag_t iot = ia->ia_iot;
366 bus_space_handle_t ioh;
367 int i;
368
369 /* Map i/o space. */
370 if (bus_space_map(iot, iobase, 16, 0, &ioh))
371 return 0;
372
373 lesc->sc_iot = iot;
374 lesc->sc_ioh = ioh;
375 lesc->sc_rap = BICC_RAP;
376 lesc->sc_rdp = BICC_RDP;
377 lesc->sc_card = BICC;
378
379 if (lance_isa_probe(sc) == 0) {
380 bus_space_unmap(iot, ioh, 16);
381 return 0;
382 }
383
384 /*
385 * Extract the physical MAC address from the ROM.
386 */
387 for (i = 0; i < sizeof(sc->sc_enaddr); i++)
388 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, i * 2);
389
390 /*
391 * XXX INDIRECT BROKENNESS!
392 * XXX Should always unmap, and re-map in if_le_isa_attach().
393 */
394
395 ia->ia_iosize = 16;
396 return 1;
397 }
398
399 /*
400 * Determine which chip is present on the card.
401 */
402 int
403 lance_isa_probe(sc)
404 struct am7990_softc *sc;
405 {
406
407 /* Stop the LANCE chip and put it in a known state. */
408 le_isa_wrcsr(sc, LE_CSR0, LE_C0_STOP);
409 delay(100);
410
411 if (le_isa_rdcsr(sc, LE_CSR0) != LE_C0_STOP)
412 return 0;
413
414 le_isa_wrcsr(sc, LE_CSR3, sc->sc_conf3);
415 return 1;
416 }
417
418 void
419 le_isa_attach(parent, self, aux)
420 struct device *parent, *self;
421 void *aux;
422 {
423 struct le_softc *lesc = (void *)self;
424 struct am7990_softc *sc = &lesc->sc_am7990;
425 struct isa_attach_args *ia = aux;
426 bus_space_tag_t iot = ia->ia_iot;
427 bus_space_tag_t memt = ia->ia_memt;
428 bus_dma_tag_t dmat = ia->ia_dmat;
429 bus_space_handle_t memh;
430 bus_dma_segment_t seg;
431 int rseg;
432
433 printf(": %s Ethernet\n", card_type[lesc->sc_card]);
434
435 lesc->sc_iot = iot;
436 lesc->sc_memt = memt;
437 lesc->sc_dmat = dmat;
438
439 /* XXX SHOULD RE-MAP I/O SPACE HERE. */
440
441 if (lesc->sc_card == DEPCA) {
442 u_char val;
443 int i;
444
445 /* Map shared memory. */
446 if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize,
447 0, &memh))
448 panic("le_isa_attach: can't map shared memory");
449
450 lesc->sc_memh = memh;
451
452 val = 0xff;
453 for (;;) {
454 bus_space_set_region_1(memt, memh, 0, val,
455 ia->ia_msize);
456 for (i = 0; i < ia->ia_msize; i++)
457 if (bus_space_read_1(memt, memh, 1) != val) {
458 printf("%s: failed to clear memory\n",
459 sc->sc_dev.dv_xname);
460 return;
461 }
462 if (val == 0x00)
463 break;
464 val -= 0x55;
465 }
466
467 sc->sc_conf3 = LE_C3_ACON;
468 sc->sc_mem = 0; /* Not used. */
469 sc->sc_addr = 0;
470 sc->sc_memsize = ia->ia_msize;
471 } else {
472 /*
473 * Allocate a DMA area for the card.
474 */
475 if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, NBPG, 0, &seg, 1,
476 &rseg, BUS_DMA_NOWAIT)) {
477 printf("%s: couldn't allocate memory for card\n",
478 sc->sc_dev.dv_xname);
479 return;
480 }
481 if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE,
482 (caddr_t *)&sc->sc_mem,
483 BUS_DMA_NOWAIT|BUS_DMAMEM_NOSYNC)) {
484 printf("%s: couldn't map memory for card\n",
485 sc->sc_dev.dv_xname);
486 return;
487 }
488
489 /*
490 * Create and load the DMA map for the DMA area.
491 */
492 if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1,
493 LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) {
494 printf("%s: couldn't create DMA map\n",
495 sc->sc_dev.dv_xname);
496 bus_dmamem_free(dmat, &seg, rseg);
497 return;
498 }
499 if (bus_dmamap_load(dmat, lesc->sc_dmam,
500 sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) {
501 printf("%s: coundn't load DMA map\n",
502 sc->sc_dev.dv_xname);
503 bus_dmamem_free(dmat, &seg, rseg);
504 return;
505 }
506
507 sc->sc_conf3 = 0;
508 sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr;
509 sc->sc_memsize = LE_ISA_MEMSIZE;
510 }
511
512 if (lesc->sc_card == DEPCA) {
513 sc->sc_copytodesc = depca_copytobuf;
514 sc->sc_copyfromdesc = depca_copyfrombuf;
515 sc->sc_copytobuf = depca_copytobuf;
516 sc->sc_copyfrombuf = depca_copyfrombuf;
517 sc->sc_zerobuf = depca_zerobuf;
518 } else {
519 sc->sc_copytodesc = am7990_copytobuf_contig;
520 sc->sc_copyfromdesc = am7990_copyfrombuf_contig;
521 sc->sc_copytobuf = am7990_copytobuf_contig;
522 sc->sc_copyfrombuf = am7990_copyfrombuf_contig;
523 sc->sc_zerobuf = am7990_zerobuf_contig;
524 }
525
526 sc->sc_rdcsr = le_isa_rdcsr;
527 sc->sc_wrcsr = le_isa_wrcsr;
528 sc->sc_hwinit = NULL;
529
530 printf("%s", sc->sc_dev.dv_xname);
531 am7990_config(sc);
532
533 if (ia->ia_drq != DRQUNK)
534 isa_dmacascade(parent, ia->ia_drq);
535
536 lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
537 IPL_NET, le_isa_intredge, sc);
538 }
539
540 /*
541 * Controller interrupt.
542 */
543 int
544 le_isa_intredge(arg)
545 void *arg;
546 {
547
548 if (am7990_intr(arg) == 0)
549 return (0);
550 for (;;)
551 if (am7990_intr(arg) == 0)
552 return (1);
553 }
554
555 /*
556 * DEPCA shared memory access functions.
557 */
558
559 void
560 depca_copytobuf(sc, from, boff, len)
561 struct am7990_softc *sc;
562 void *from;
563 int boff, len;
564 {
565 struct le_softc *lesc = (struct le_softc *)sc;
566
567 bus_space_write_region_1(lesc->sc_memt, lesc->sc_memh, boff,
568 from, len);
569 }
570
571 void
572 depca_copyfrombuf(sc, to, boff, len)
573 struct am7990_softc *sc;
574 void *to;
575 int boff, len;
576 {
577 struct le_softc *lesc = (struct le_softc *)sc;
578
579 bus_space_read_region_1(lesc->sc_memt, lesc->sc_memh, boff,
580 to, len);
581 }
582
583 void
584 depca_zerobuf(sc, boff, len)
585 struct am7990_softc *sc;
586 int boff, len;
587 {
588 struct le_softc *lesc = (struct le_softc *)sc;
589
590 #if 0 /* XXX !! */
591 bus_space_set_region_1(lesc->sc_memt, lesc->sc_memh, boff,
592 0x00, len);
593 #else
594 for (; len != 0; boff++, len--)
595 bus_space_write_1(lesc->sc_memt, lesc->sc_memh, boff, 0x00);
596 #endif
597 }
598