No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / macppc / dev / am79c950.c
blob58859bff4694a28cd9f443a088e86a0b6d7125a1
1 /* $NetBSD: am79c950.c,v 1.26 2009/03/14 15:36:09 dsl Exp $ */
3 /*-
4 * Copyright (c) 1997 David Huang <khym@bga.com>
5 * All rights reserved.
7 * Portions of this code are based on code by Denton Gentry <denny1@home.com>,
8 * Charles M. Hannum, Yanagisawa Takeshi <yanagisw@aa.ap.titech.ac.jp>, and
9 * Jason R. Thorpe.
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. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Driver for the AMD Am79C940 (MACE) ethernet chip, used for onboard
34 * ethernet on the Centris/Quadra 660av and Quadra 840av.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: am79c950.c,v 1.26 2009/03/14 15:36:09 dsl Exp $");
40 #include "opt_inet.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/buf.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/syslog.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
53 #include <uvm/uvm_extern.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/if_inarp.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #endif
68 #include "bpfilter.h"
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #include <net/bpfdesc.h>
72 #endif
74 #include <machine/bus.h>
76 #include <macppc/dev/am79c950reg.h>
77 #include <macppc/dev/if_mcvar.h>
79 hide void mcwatchdog(struct ifnet *);
80 hide int mcinit(struct mc_softc *sc);
81 hide int mcstop(struct mc_softc *sc);
82 hide int mcioctl(struct ifnet *ifp, u_long cmd, void *data);
83 hide void mcstart(struct ifnet *ifp);
84 hide void mcreset(struct mc_softc *sc);
86 integrate u_int maceput(struct mc_softc *sc, struct mbuf *m0);
87 integrate void mc_tint(struct mc_softc *sc);
88 integrate void mace_read(struct mc_softc *, uint8_t *, int);
89 integrate struct mbuf *mace_get(struct mc_softc *, uint8_t *, int);
90 static void mace_calcladrf(struct ethercom *ac, u_int8_t *af);
91 static inline u_int16_t ether_cmp(void *, void *);
92 static int mc_mediachange(struct ifnet *);
93 static void mc_mediastatus(struct ifnet *, struct ifmediareq *);
96 * Compare two Ether/802 addresses for equality, inlined and
97 * unrolled for speed. Use this like memcmp().
99 * XXX: Add <machine/inlines.h> for stuff like this?
100 * XXX: or maybe add it to libkern.h instead?
102 * "I'd love to have an inline assembler version of this."
103 * XXX: Who wanted that? mycroft? I wrote one, but this
104 * version in C is as good as hand-coded assembly. -gwr
106 * Please do NOT tweak this without looking at the actual
107 * assembly code generated before and after your tweaks!
109 static inline u_int16_t
110 ether_cmp(void *one, void *two)
112 register u_int16_t *a = (u_short *) one;
113 register u_int16_t *b = (u_short *) two;
114 register u_int16_t diff;
116 #ifdef m68k
118 * The post-increment-pointer form produces the best
119 * machine code for m68k. This was carefully tuned
120 * so it compiles to just 8 short (2-byte) op-codes!
122 diff = *a++ - *b++;
123 diff |= *a++ - *b++;
124 diff |= *a++ - *b++;
125 #else
127 * Most modern CPUs do better with a single expresion.
128 * Note that short-cut evaluation is NOT helpful here,
129 * because it just makes the code longer, not faster!
131 diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
132 #endif
134 return (diff);
137 #define ETHER_CMP ether_cmp
140 * Interface exists: make available by filling in network interface
141 * record. System will initialize the interface when it is ready
142 * to accept packets.
145 mcsetup(struct mc_softc *sc, u_int8_t *lladdr)
147 struct ifnet *ifp = &sc->sc_if;
149 /* reset the chip and disable all interrupts */
150 NIC_PUT(sc, MACE_BIUCC, SWRST);
151 DELAY(100);
152 NIC_PUT(sc, MACE_IMR, ~0);
154 memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
155 printf(": address %s\n", ether_sprintf(lladdr));
157 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
158 ifp->if_softc = sc;
159 ifp->if_ioctl = mcioctl;
160 ifp->if_start = mcstart;
161 ifp->if_flags =
162 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
163 ifp->if_watchdog = mcwatchdog;
165 /* initialize ifmedia structures */
166 ifmedia_init(&sc->sc_media, 0, mc_mediachange, mc_mediastatus);
167 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
168 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
170 if_attach(ifp);
171 ether_ifattach(ifp, lladdr);
173 return (0);
176 hide int
177 mcioctl(struct ifnet *ifp, u_long cmd, void *data)
179 struct mc_softc *sc = ifp->if_softc;
180 struct ifaddr *ifa;
181 struct ifreq *ifr;
183 int s = splnet(), err = 0;
185 switch (cmd) {
187 case SIOCINITIFADDR:
188 ifa = (struct ifaddr *)data;
189 ifp->if_flags |= IFF_UP;
190 mcinit(sc);
191 switch (ifa->ifa_addr->sa_family) {
192 #ifdef INET
193 case AF_INET:
194 arp_ifinit(ifp, ifa);
195 break;
196 #endif
197 default:
198 break;
200 break;
202 case SIOCSIFFLAGS:
203 if ((err = ifioctl_common(ifp, cmd, data)) != 0)
204 break;
205 /* XXX see the comment in ed_ioctl() about code re-use */
206 if ((ifp->if_flags & IFF_UP) == 0 &&
207 (ifp->if_flags & IFF_RUNNING) != 0) {
209 * If interface is marked down and it is running,
210 * then stop it.
212 mcstop(sc);
213 ifp->if_flags &= ~IFF_RUNNING;
214 } else if ((ifp->if_flags & IFF_UP) != 0 &&
215 (ifp->if_flags & IFF_RUNNING) == 0) {
217 * If interface is marked up and it is stopped,
218 * then start it.
220 (void)mcinit(sc);
221 } else {
223 * reset the interface to pick up any other changes
224 * in flags
226 mcreset(sc);
227 mcstart(ifp);
229 break;
231 case SIOCADDMULTI:
232 case SIOCDELMULTI:
233 if ((err = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
235 * Multicast list has changed; set the hardware
236 * filter accordingly. But remember UP flag!
238 if (ifp->if_flags & IFF_RUNNING)
239 mcreset(sc);
240 err = 0;
242 break;
244 case SIOCGIFMEDIA:
245 case SIOCSIFMEDIA:
246 ifr = (struct ifreq *) data;
247 err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
248 break;
250 default:
251 err = ether_ioctl(ifp, cmd, data);
252 break;
254 splx(s);
255 return (err);
259 * Encapsulate a packet of type family for the local net.
261 hide void
262 mcstart(struct ifnet *ifp)
264 struct mc_softc *sc = ifp->if_softc;
265 struct mbuf *m;
267 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
268 return;
270 while (1) {
271 if (ifp->if_flags & IFF_OACTIVE)
272 return;
274 IF_DEQUEUE(&ifp->if_snd, m);
275 if (m == 0)
276 return;
278 #if NBPFILTER > 0
280 * If bpf is listening on this interface, let it
281 * see the packet before we commit it to the wire.
283 if (ifp->if_bpf)
284 bpf_mtap(ifp->if_bpf, m);
285 #endif
288 * Copy the mbuf chain into the transmit buffer.
290 ifp->if_flags |= IFF_OACTIVE;
291 maceput(sc, m);
293 ifp->if_opackets++; /* # of pkts */
298 * reset and restart the MACE. Called in case of fatal
299 * hardware/software errors.
301 hide void
302 mcreset(struct mc_softc *sc)
304 mcstop(sc);
305 mcinit(sc);
308 hide int
309 mcinit(struct mc_softc *sc)
311 int s;
312 u_int8_t maccc, ladrf[8];
314 if (sc->sc_if.if_flags & IFF_RUNNING)
315 /* already running */
316 return (0);
318 s = splnet();
320 NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
321 NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
322 NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
323 NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
325 NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
327 /* set MAC address */
328 NIC_PUT(sc, MACE_IAC, ADDRCHG);
329 while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
331 NIC_PUT(sc, MACE_IAC, PHYADDR);
332 bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
333 sc->sc_enaddr, ETHER_ADDR_LEN);
335 /* set logical address filter */
336 mace_calcladrf(&sc->sc_ethercom, ladrf);
338 NIC_PUT(sc, MACE_IAC, ADDRCHG);
339 while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
341 NIC_PUT(sc, MACE_IAC, LOGADDR);
342 bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
343 ladrf, 8);
345 NIC_PUT(sc, MACE_XMTFC, APADXMT);
347 * No need to autostrip padding on receive... Ethernet frames
348 * don't have a length field, unlike 802.3 frames, so the MACE
349 * can't figure out the length of the packet anyways.
351 NIC_PUT(sc, MACE_RCVFC, 0);
353 maccc = ENXMT | ENRCV;
354 if (sc->sc_if.if_flags & IFF_PROMISC)
355 maccc |= PROM;
357 NIC_PUT(sc, MACE_MACCC, maccc);
359 if (sc->sc_bus_init)
360 (*sc->sc_bus_init)(sc);
363 * Enable all interrupts except receive, since we use the DMA
364 * completion interrupt for that.
366 NIC_PUT(sc, MACE_IMR, RCVINTM);
368 /* flag interface as "running" */
369 sc->sc_if.if_flags |= IFF_RUNNING;
370 sc->sc_if.if_flags &= ~IFF_OACTIVE;
372 splx(s);
373 return (0);
377 * close down an interface and free its buffers
378 * Called on final close of device, or if mcinit() fails
379 * part way through.
381 hide int
382 mcstop(struct mc_softc *sc)
384 int s = splnet();
386 NIC_PUT(sc, MACE_BIUCC, SWRST);
387 DELAY(100);
389 sc->sc_if.if_timer = 0;
390 sc->sc_if.if_flags &= ~IFF_RUNNING;
392 splx(s);
393 return (0);
397 * Called if any Tx packets remain unsent after 5 seconds,
398 * In all cases we just reset the chip, and any retransmission
399 * will be handled by higher level protocol timeouts.
401 hide void
402 mcwatchdog(struct ifnet *ifp)
404 struct mc_softc *sc = ifp->if_softc;
406 printf("mcwatchdog: resetting chip\n");
407 mcreset(sc);
411 * stuff packet into MACE (at splnet)
413 integrate u_int
414 maceput(struct mc_softc *sc, struct mbuf *m)
416 struct mbuf *n;
417 u_int len, totlen = 0;
418 u_char *buff;
420 buff = sc->sc_txbuf;
422 for (; m; m = n) {
423 u_char *data = mtod(m, u_char *);
424 len = m->m_len;
425 totlen += len;
426 memcpy(buff, data, len);
427 buff += len;
428 MFREE(m, n);
431 if (totlen > PAGE_SIZE)
432 panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
434 #if 0
435 if (totlen < ETHERMIN + sizeof(struct ether_header)) {
436 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
437 memset(sc->sc_txbuf + totlen, 0, pad);
438 totlen = ETHERMIN + sizeof(struct ether_header);
440 #endif
442 (*sc->sc_putpacket)(sc, totlen);
444 sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
445 return (totlen);
449 mcintr(void *arg)
451 struct mc_softc *sc = arg;
452 u_int8_t ir;
454 ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
455 if (ir == 0)
456 return 0;
458 if (ir & JAB) {
459 #ifdef MCDEBUG
460 printf("%s: jabber error\n", sc->sc_dev.dv_xname);
461 #endif
462 sc->sc_if.if_oerrors++;
465 if (ir & BABL) {
466 #ifdef MCDEBUG
467 printf("%s: babble\n", sc->sc_dev.dv_xname);
468 #endif
469 sc->sc_if.if_oerrors++;
472 if (ir & CERR) {
473 printf("%s: collision error\n", sc->sc_dev.dv_xname);
474 sc->sc_if.if_collisions++;
478 * Pretend we have carrier; if we don't this will be cleared
479 * shortly.
481 sc->sc_havecarrier = 1;
483 if (ir & XMTINT)
484 mc_tint(sc);
486 if (ir & RCVINT)
487 mc_rint(sc);
489 return 1;
492 integrate void
493 mc_tint(struct mc_softc *sc)
495 u_int8_t xmtrc, xmtfs;
497 xmtrc = NIC_GET(sc, MACE_XMTRC);
498 xmtfs = NIC_GET(sc, MACE_XMTFS);
500 if ((xmtfs & XMTSV) == 0)
501 return;
503 if (xmtfs & UFLO) {
504 printf("%s: underflow\n", sc->sc_dev.dv_xname);
505 mcreset(sc);
506 return;
509 if (xmtfs & LCOL) {
510 printf("%s: late collision\n", sc->sc_dev.dv_xname);
511 sc->sc_if.if_oerrors++;
512 sc->sc_if.if_collisions++;
515 if (xmtfs & MORE)
516 /* Real number is unknown. */
517 sc->sc_if.if_collisions += 2;
518 else if (xmtfs & ONE)
519 sc->sc_if.if_collisions++;
520 else if (xmtfs & RTRY) {
521 sc->sc_if.if_collisions += 16;
522 sc->sc_if.if_oerrors++;
525 if (xmtfs & LCAR) {
526 sc->sc_havecarrier = 0;
527 printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
528 sc->sc_if.if_oerrors++;
531 sc->sc_if.if_flags &= ~IFF_OACTIVE;
532 sc->sc_if.if_timer = 0;
533 mcstart(&sc->sc_if);
536 void
537 mc_rint(struct mc_softc *sc)
539 #define rxf sc->sc_rxframe
540 u_int len;
542 len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
544 #ifdef MCDEBUG
545 if (rxf.rx_rcvsts & 0xf0)
546 printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
547 sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
548 rxf.rx_rntpc, rxf.rx_rcvcc);
549 #endif
551 if (rxf.rx_rcvsts & OFLO) {
552 printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
553 sc->sc_if.if_ierrors++;
554 return;
557 if (rxf.rx_rcvsts & CLSN)
558 sc->sc_if.if_collisions++;
560 if (rxf.rx_rcvsts & FRAM) {
561 #ifdef MCDEBUG
562 printf("%s: framing error\n", sc->sc_dev.dv_xname);
563 #endif
564 sc->sc_if.if_ierrors++;
565 return;
568 if (rxf.rx_rcvsts & FCS) {
569 #ifdef MCDEBUG
570 printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
571 #endif
572 sc->sc_if.if_ierrors++;
573 return;
576 mace_read(sc, rxf.rx_frame, len);
577 #undef rxf
580 integrate void
581 mace_read(struct mc_softc *sc, uint8_t *pkt, int len)
583 struct ifnet *ifp = &sc->sc_if;
584 struct mbuf *m;
586 if (len <= sizeof(struct ether_header) ||
587 len > ETHERMTU + sizeof(struct ether_header)) {
588 #ifdef MCDEBUG
589 printf("%s: invalid packet size %d; dropping\n",
590 sc->sc_dev.dv_xname, len);
591 #endif
592 ifp->if_ierrors++;
593 return;
596 m = mace_get(sc, pkt, len);
597 if (m == NULL) {
598 ifp->if_ierrors++;
599 return;
602 ifp->if_ipackets++;
604 #if NBPFILTER > 0
605 /* Pass this up to any BPF listeners. */
606 if (ifp->if_bpf)
607 bpf_mtap(ifp->if_bpf, m);
608 #endif
610 /* Pass the packet up. */
611 (*ifp->if_input)(ifp, m);
615 * Pull data off an interface.
616 * Len is length of data, with local net header stripped.
617 * We copy the data into mbufs. When full cluster sized units are present
618 * we copy into clusters.
620 integrate struct mbuf *
621 mace_get(struct mc_softc *sc, uint8_t *pkt, int totlen)
623 register struct mbuf *m;
624 struct mbuf *top, **mp;
625 int len;
627 MGETHDR(m, M_DONTWAIT, MT_DATA);
628 if (m == 0)
629 return (0);
630 m->m_pkthdr.rcvif = &sc->sc_if;
631 m->m_pkthdr.len = totlen;
632 len = MHLEN;
633 top = 0;
634 mp = &top;
636 while (totlen > 0) {
637 if (top) {
638 MGET(m, M_DONTWAIT, MT_DATA);
639 if (m == 0) {
640 m_freem(top);
641 return 0;
643 len = MLEN;
645 if (totlen >= MINCLSIZE) {
646 MCLGET(m, M_DONTWAIT);
647 if ((m->m_flags & M_EXT) == 0) {
648 m_free(m);
649 m_freem(top);
650 return 0;
652 len = MCLBYTES;
654 m->m_len = len = min(totlen, len);
655 memcpy(mtod(m, void *), pkt, len);
656 pkt += len;
657 totlen -= len;
658 *mp = m;
659 mp = &m->m_next;
662 return (top);
666 * Go through the list of multicast addresses and calculate the logical
667 * address filter.
669 void
670 mace_calcladrf(struct ethercom *ac, u_int8_t *af)
672 struct ifnet *ifp = &ac->ec_if;
673 struct ether_multi *enm;
674 register u_char *cp, c;
675 register u_int32_t crc;
676 register int i, len;
677 struct ether_multistep step;
680 * Set up multicast address filter by passing all multicast addresses
681 * through a crc generator, and then using the high order 6 bits as an
682 * index into the 64 bit logical address filter. The high order bit
683 * selects the word, while the rest of the bits select the bit within
684 * the word.
687 *((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
689 ETHER_FIRST_MULTI(step, ac, enm);
690 while (enm != NULL) {
691 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
693 * We must listen to a range of multicast addresses.
694 * For now, just accept all multicasts, rather than
695 * trying to set only those filter bits needed to match
696 * the range. (At this time, the only use of address
697 * ranges is for IP multicast routing, for which the
698 * range is big enough to require all bits set.)
700 goto allmulti;
703 cp = enm->enm_addrlo;
704 crc = 0xffffffff;
705 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
706 c = *cp++;
707 for (i = 8; --i >= 0;) {
708 if ((crc & 0x01) ^ (c & 0x01)) {
709 crc >>= 1;
710 crc ^= 0xedb88320;
711 } else
712 crc >>= 1;
713 c >>= 1;
716 /* Just want the 6 most significant bits. */
717 crc >>= 26;
719 /* Set the corresponding bit in the filter. */
720 af[crc >> 3] |= 1 << (crc & 7);
722 ETHER_NEXT_MULTI(step, enm);
724 ifp->if_flags &= ~IFF_ALLMULTI;
725 return;
727 allmulti:
728 ifp->if_flags |= IFF_ALLMULTI;
729 *((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
733 mc_mediachange(struct ifnet *ifp)
735 return EINVAL;
738 void
739 mc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
741 struct mc_softc *sc = ifp->if_softc;
743 if ((ifp->if_flags & IFF_UP) == 0)
744 return;
746 if (sc->sc_havecarrier)
747 ifmr->ifm_status |= IFM_ACTIVE;