1 /* $NetBSD: if_le.c,v 1.43 2008/04/28 20:23:12 martin Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (c) 1997 Bernd Ernesti. All rights reserved.
35 * Copyright (c) 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Ralph Campbell and Rick Macklem.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed for the NetBSD Project
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.43 2008/04/28 20:23:12 martin Exp $");
81 #include <sys/param.h>
82 #include <sys/systm.h>
84 #include <sys/syslog.h>
85 #include <sys/socket.h>
86 #include <sys/device.h>
89 #include <net/if_ether.h>
90 #include <net/if_media.h>
93 #include <netinet/in.h>
94 #include <netinet/if_inarp.h>
97 #include <machine/cpu.h>
99 #include <amiga/amiga/device.h>
100 #include <amiga/amiga/isr.h>
102 #include <dev/ic/lancereg.h>
103 #include <dev/ic/lancevar.h>
104 #include <dev/ic/am7990reg.h>
105 #include <dev/ic/am7990var.h>
107 #include <amiga/dev/zbusvar.h>
108 #include <amiga/dev/if_levar.h>
110 int le_zbus_match(device_t
, cfdata_t
, void *);
111 void le_zbus_attach(device_t
, device_t
, void *);
113 CFATTACH_DECL_NEW(le_zbus
, sizeof(struct le_softc
),
114 le_zbus_match
, le_zbus_attach
, NULL
, NULL
);
116 #if defined(_KERNEL_OPT)
124 #define integrate static inline
128 hide
void lepcnet_reset(struct lance_softc
*);
129 hide
void lewrcsr(struct lance_softc
*, u_int16_t
, u_int16_t
);
130 hide u_int16_t
lerdcsr(struct lance_softc
*, u_int16_t
);
132 hide u_int16_t
ariadne_swapreg(u_int16_t
);
133 hide
void ariadne_wrcsr(struct lance_softc
*, u_int16_t
, u_int16_t
);
134 hide u_int16_t
ariadne_rdcsr(struct lance_softc
*, u_int16_t
);
135 hide
void ariadne_wribcr(struct lance_softc
*, u_int16_t
, u_int16_t
);
136 integrate
void ariadne_copytodesc_word(struct lance_softc
*, void *, int, int);
137 integrate
void ariadne_copyfromdesc_word(struct lance_softc
*, void *,
139 integrate
void ariadne_copytobuf_word(struct lance_softc
*, void *, int, int);
140 integrate
void ariadne_copyfrombuf_word(struct lance_softc
*, void *, int, int);
141 integrate
void ariadne_zerobuf_word(struct lance_softc
*, int, int);
142 void ariadne_autoselect(struct lance_softc
*, int);
143 int ariadne_mediachange(struct lance_softc
*);
144 void ariadne_hwinit(struct lance_softc
*);
147 * Media types supported by the Ariadne.
149 int lemedia_ariadne
[] = {
150 IFM_ETHER
| IFM_10_T
,
151 IFM_ETHER
| IFM_10_2
,
152 IFM_ETHER
| IFM_AUTO
,
154 #define NLEMEDIA_ARIADNE __arraycount(lemedia_ariadne)
158 ariadne_swapreg(u_int16_t val
)
161 return (((val
& 0xff) << 8 ) | (( val
>> 8) & 0xff));
165 ariadne_wrcsr(struct lance_softc
*sc
, u_int16_t port
, u_int16_t val
)
167 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
169 ler1
->ler1_rap
= ariadne_swapreg(port
);
170 ler1
->ler1_rdp
= ariadne_swapreg(val
);
174 ariadne_rdcsr(struct lance_softc
*sc
, u_int16_t port
)
176 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
179 ler1
->ler1_rap
= ariadne_swapreg(port
);
180 val
= ariadne_swapreg(ler1
->ler1_rdp
);
185 ariadne_wribcr(struct lance_softc
*sc
, u_int16_t port
, u_int16_t val
)
187 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
189 ler1
->ler1_rap
= ariadne_swapreg(port
);
190 ler1
->ler1_idp
= ariadne_swapreg(val
);
194 lewrcsr(struct lance_softc
*sc
, u_int16_t port
, u_int16_t val
)
196 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
198 ler1
->ler1_rap
= port
;
199 ler1
->ler1_rdp
= val
;
203 lerdcsr(struct lance_softc
*sc
, u_int16_t port
)
205 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
208 ler1
->ler1_rap
= port
;
209 val
= ler1
->ler1_rdp
;
214 lepcnet_reset(struct lance_softc
*sc
)
216 struct lereg1
*ler1
= ((struct le_softc
*)sc
)->sc_r1
;
219 dummy
= ler1
->ler1_reset
; /* Reset PCNet-ISA */
223 ariadne_autoselect(struct lance_softc
*sc
, int on
)
227 * on = 0: autoselect disabled
228 * on = 1: autoselect enabled
231 ariadne_wribcr(sc
, LE_BCR_MC
, 0x0000);
233 ariadne_wribcr(sc
, LE_BCR_MC
, LE_MC_ASEL
);
237 ariadne_mediachange(struct lance_softc
*sc
)
239 struct ifmedia
*ifm
= &sc
->sc_media
;
241 if (IFM_TYPE(ifm
->ifm_media
) != IFM_ETHER
)
245 * Switch to the selected media. If autoselect is
246 * set, switch it on otherwise disable it. We'll
247 * switch to the other media when we detect loss of
250 switch (IFM_SUBTYPE(ifm
->ifm_media
)) {
252 sc
->sc_initmodemedia
= 1;
253 lance_init(&sc
->sc_ethercom
.ec_if
);
257 sc
->sc_initmodemedia
= 0;
258 lance_init(&sc
->sc_ethercom
.ec_if
);
262 sc
->sc_initmodemedia
= 2;
274 ariadne_hwinit(struct lance_softc
*sc
)
278 * Re-program LEDs to match meaning used on the Ariadne board.
280 ariadne_wribcr(sc
, LE_BCR_LED1
, 0x0090);
281 ariadne_wribcr(sc
, LE_BCR_LED2
, 0x0081);
282 ariadne_wribcr(sc
, LE_BCR_LED3
, 0x0084);
285 * Enabel/Disable auto selection
287 if (sc
->sc_initmodemedia
== 2)
288 ariadne_autoselect(sc
, 1);
290 ariadne_autoselect(sc
, 0);
294 le_zbus_match(device_t parent
, cfdata_t cfp
, void *aux
)
296 struct zbus_args
*zap
= aux
;
298 /* Commodore ethernet card */
299 if (zap
->manid
== 514 && zap
->prodid
== 112)
302 /* Ameristar ethernet card */
303 if (zap
->manid
== 1053 && zap
->prodid
== 1)
306 /* Ariadne ethernet card */
307 if (zap
->manid
== 2167 && zap
->prodid
== 201)
314 le_zbus_attach(device_t parent
, device_t self
, void *aux
)
316 struct le_softc
*lesc
= device_private(self
);
317 struct lance_softc
*sc
= &lesc
->sc_am7990
.lsc
;
318 struct zbus_args
*zap
= aux
;
323 /* This has no effect on PCnet-ISA LANCE chips */
324 sc
->sc_conf3
= LE_C3_BSWP
;
327 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
329 switch (zap
->manid
) {
332 sc
->sc_memsize
= 32768;
333 sc
->sc_enaddr
[0] = 0x00;
334 sc
->sc_enaddr
[1] = 0x80;
335 sc
->sc_enaddr
[2] = 0x10;
336 lesc
->sc_r1
= (struct lereg1
*)(0x4000 + (int)zap
->va
);
337 sc
->sc_mem
= (void *)(0x8000 + (int)zap
->va
);
338 sc
->sc_addr
= 0x8000;
339 sc
->sc_copytodesc
= lance_copytobuf_contig
;
340 sc
->sc_copyfromdesc
= lance_copyfrombuf_contig
;
341 sc
->sc_copytobuf
= lance_copytobuf_contig
;
342 sc
->sc_copyfrombuf
= lance_copyfrombuf_contig
;
343 sc
->sc_zerobuf
= lance_zerobuf_contig
;
344 sc
->sc_rdcsr
= lerdcsr
;
345 sc
->sc_wrcsr
= lewrcsr
;
346 sc
->sc_hwreset
= NULL
;
347 sc
->sc_hwinit
= NULL
;
352 sc
->sc_memsize
= 32768;
353 sc
->sc_enaddr
[0] = 0x00;
354 sc
->sc_enaddr
[1] = 0x00;
355 sc
->sc_enaddr
[2] = 0x9f;
356 lesc
->sc_r1
= (struct lereg1
*)(0x4000 + (int)zap
->va
);
357 sc
->sc_mem
= (void *)(0x8000 + (int)zap
->va
);
358 sc
->sc_addr
= 0x8000;
359 sc
->sc_copytodesc
= lance_copytobuf_contig
;
360 sc
->sc_copyfromdesc
= lance_copyfrombuf_contig
;
361 sc
->sc_copytobuf
= lance_copytobuf_contig
;
362 sc
->sc_copyfrombuf
= lance_copyfrombuf_contig
;
363 sc
->sc_zerobuf
= lance_zerobuf_contig
;
364 sc
->sc_rdcsr
= lerdcsr
;
365 sc
->sc_wrcsr
= lewrcsr
;
366 sc
->sc_hwreset
= NULL
;
367 sc
->sc_hwinit
= NULL
;
372 sc
->sc_memsize
= 32768;
373 sc
->sc_enaddr
[0] = 0x00;
374 sc
->sc_enaddr
[1] = 0x60;
375 sc
->sc_enaddr
[2] = 0x30;
376 lesc
->sc_r1
= (struct lereg1
*)(0x0370 + (int)zap
->va
);
377 sc
->sc_mem
= (void *)(0x8000 + (int)zap
->va
);
378 sc
->sc_addr
= 0x8000;
379 sc
->sc_copytodesc
= ariadne_copytodesc_word
;
380 sc
->sc_copyfromdesc
= ariadne_copyfromdesc_word
;
381 sc
->sc_copytobuf
= ariadne_copytobuf_word
;
382 sc
->sc_copyfrombuf
= ariadne_copyfrombuf_word
;
383 sc
->sc_zerobuf
= ariadne_zerobuf_word
;
384 sc
->sc_rdcsr
= ariadne_rdcsr
;
385 sc
->sc_wrcsr
= ariadne_wrcsr
;
386 sc
->sc_hwreset
= lepcnet_reset
;
387 sc
->sc_hwinit
= ariadne_hwinit
;
388 sc
->sc_mediachange
= ariadne_mediachange
;
389 sc
->sc_supmedia
= lemedia_ariadne
;
390 sc
->sc_nsupmedia
= NLEMEDIA_ARIADNE
;
391 sc
->sc_defaultmedia
= IFM_ETHER
| IFM_AUTO
;
392 sc
->sc_initmodemedia
= 2;
396 panic("le_zbus_attach: bad manid");
400 * Serial number for board is used as host ID.
402 ser
= (u_long
)zap
->serno
;
403 sc
->sc_enaddr
[3] = (ser
>> 16) & 0xff;
404 sc
->sc_enaddr
[4] = (ser
>> 8) & 0xff;
405 sc
->sc_enaddr
[5] = (ser
) & 0xff;
407 am7990_config(&lesc
->sc_am7990
);
409 lesc
->sc_isr
.isr_intr
= am7990_intr
;
410 lesc
->sc_isr
.isr_arg
= sc
;
411 lesc
->sc_isr
.isr_ipl
= 2;
412 add_isr(&lesc
->sc_isr
);
417 ariadne_copytodesc_word(struct lance_softc
*sc
, void *from
, int boff
, int len
)
420 volatile u_short
*b2
= (u_short
*)((u_char
*)sc
->sc_mem
+ boff
);
422 for (len
>>= 1; len
> 0; len
--)
423 *b2
++ = ariadne_swapreg(*b1
++);
427 ariadne_copyfromdesc_word(struct lance_softc
*sc
, void *to
, int boff
, int len
)
429 volatile u_short
*b1
= (u_short
*)((u_char
*)sc
->sc_mem
+ boff
);
432 for (len
>>= 1; len
> 0; len
--)
433 *b2
++ = ariadne_swapreg(*b1
++);
436 #define isodd(n) ((n) & 1)
439 ariadne_copytobuf_word(struct lance_softc
*sc
, void *from
, int boff
, int len
)
442 volatile u_char
*a2
= (u_char
*)sc
->sc_mem
+ boff
;
444 volatile u_short
*b2
;
447 if (len
> 0 && isodd(boff
)) {
448 /* adjust source pointer */
449 b1
= (u_short
*)(a1
+ 1);
450 /* compute aligned destination pointer */
451 b2
= (volatile u_short
*)(a2
+ 1);
452 /* copy first unaligned byte to buf */
453 b2
[-1] = (b2
[-1] & 0xff00) | *a1
;
456 /* destination is aligned or length is zero */
458 b2
= (volatile u_short
*)a2
;
461 /* copy full words with aligned destination */
462 for (i
= len
>> 1; i
> 0; i
--)
465 /* copy remaining byte */
467 *b2
= (*b2
& 0x00ff) | (*(u_char
*)b1
) << 8;
471 ariadne_copyfrombuf_word(struct lance_softc
*sc
, void *to
, int boff
, int len
)
473 volatile u_char
*a1
= (u_char
*)sc
->sc_mem
+ boff
;
475 volatile u_short
*b1
;
479 if (len
> 0 && isodd(boff
)) {
480 /* compute aligned source pointer */
481 b1
= (volatile u_short
*)(a1
+ 1);
482 /* adjust destination pointer (possibly unaligned) */
483 b2
= (u_short
*)(a2
+ 1);
484 /* copy first unaligned byte from buf */
488 /* source is aligned or length is zero */
489 b1
= (volatile u_short
*)a1
;
493 /* copy full words with aligned source */
494 for (i
= len
>> 1; i
> 0; i
--)
497 /* copy remaining byte */
499 *(u_char
*)b2
= *b1
>> 8;
503 ariadne_zerobuf_word(struct lance_softc
*sc
, int boff
, int len
)
505 volatile u_char
*a1
= (u_char
*)sc
->sc_mem
+ boff
;
506 volatile u_short
*b1
;
509 if (len
> 0 && isodd(boff
)) {
510 b1
= (volatile u_short
*)(a1
+ 1);
514 b1
= (volatile u_short
*)a1
;
517 for (i
= len
>> 1; i
> 0; i
--)