Home | History | Annotate | Line # | Download | only in dev
if_gm.c revision 1.12
      1 /*	$NetBSD: if_gm.c,v 1.12 2001/04/24 11:04:11 tsubai Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_inet.h"
     30 #include "opt_ns.h"
     31 #include "bpfilter.h"
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/kernel.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/socket.h>
     39 #include <sys/systm.h>
     40 #include <sys/callout.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_media.h>
     47 
     48 #if NBPFILTER > 0
     49 #include <net/bpf.h>
     50 #endif
     51 
     52 #ifdef INET
     53 #include <netinet/in.h>
     54 #include <netinet/if_inarp.h>
     55 #endif
     56 
     57 #include <dev/mii/mii.h>
     58 #include <dev/mii/miivar.h>
     59 
     60 #include <dev/pci/pcivar.h>
     61 #include <dev/pci/pcireg.h>
     62 #include <dev/pci/pcidevs.h>
     63 
     64 #include <dev/ofw/openfirm.h>
     65 #include <macppc/dev/if_gmreg.h>
     66 #include <machine/pio.h>
     67 
     68 #define NTXBUF 4
     69 #define NRXBUF 32
     70 
     71 struct gmac_softc {
     72 	struct device sc_dev;
     73 	struct ethercom sc_ethercom;
     74 	vaddr_t sc_reg;
     75 	struct gmac_dma *sc_txlist;
     76 	struct gmac_dma *sc_rxlist;
     77 	int sc_txnext;
     78 	int sc_rxlast;
     79 	caddr_t sc_txbuf[NTXBUF];
     80 	caddr_t sc_rxbuf[NRXBUF];
     81 	struct mii_data sc_mii;
     82 	struct callout sc_tick_ch;
     83 	char sc_laddr[6];
     84 };
     85 
     86 #define sc_if sc_ethercom.ec_if
     87 
     88 int gmac_match __P((struct device *, struct cfdata *, void *));
     89 void gmac_attach __P((struct device *, struct device *, void *));
     90 
     91 static __inline u_int gmac_read_reg __P((struct gmac_softc *, int));
     92 static __inline void gmac_write_reg __P((struct gmac_softc *, int, u_int));
     93 
     94 static __inline void gmac_start_txdma __P((struct gmac_softc *));
     95 static __inline void gmac_start_rxdma __P((struct gmac_softc *));
     96 static __inline void gmac_stop_txdma __P((struct gmac_softc *));
     97 static __inline void gmac_stop_rxdma __P((struct gmac_softc *));
     98 
     99 int gmac_intr __P((void *));
    100 void gmac_tint __P((struct gmac_softc *));
    101 void gmac_rint __P((struct gmac_softc *));
    102 struct mbuf * gmac_get __P((struct gmac_softc *, caddr_t, int));
    103 void gmac_start __P((struct ifnet *));
    104 int gmac_put __P((struct gmac_softc *, caddr_t, struct mbuf *));
    105 
    106 void gmac_stop __P((struct gmac_softc *));
    107 void gmac_reset __P((struct gmac_softc *));
    108 void gmac_init __P((struct gmac_softc *));
    109 void gmac_init_mac __P((struct gmac_softc *));
    110 void gmac_setladrf __P((struct gmac_softc *));
    111 
    112 int gmac_ioctl __P((struct ifnet *, u_long, caddr_t));
    113 void gmac_watchdog __P((struct ifnet *));
    114 
    115 int gmac_mediachange __P((struct ifnet *));
    116 void gmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
    117 int gmac_mii_readreg __P((struct device *, int, int));
    118 void gmac_mii_writereg __P((struct device *, int, int, int));
    119 void gmac_mii_statchg __P((struct device *));
    120 void gmac_mii_tick __P((void *));
    121 
    122 struct cfattach gm_ca = {
    123 	sizeof(struct gmac_softc), gmac_match, gmac_attach
    124 };
    125 
    126 int
    127 gmac_match(parent, match, aux)
    128 	struct device *parent;
    129 	struct cfdata *match;
    130 	void *aux;
    131 {
    132 	struct pci_attach_args *pa = aux;
    133 
    134 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    135 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC)
    136 		return 1;
    137 
    138 	return 0;
    139 }
    140 
    141 void
    142 gmac_attach(parent, self, aux)
    143 	struct device *parent, *self;
    144 	void *aux;
    145 {
    146 	struct gmac_softc *sc = (void *)self;
    147 	struct pci_attach_args *pa = aux;
    148 	struct ifnet *ifp = &sc->sc_if;
    149 	struct mii_data *mii = &sc->sc_mii;
    150 	pci_intr_handle_t ih;
    151 	const char *intrstr = NULL;
    152 	int node, i;
    153 	char *p;
    154 	struct gmac_dma *dp;
    155 	u_int32_t reg[10];
    156 	u_char laddr[6];
    157 
    158 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    159 	if (node == 0) {
    160 		printf(": cannot find gmac node\n");
    161 		return;
    162 	}
    163 
    164 	OF_getprop(node, "local-mac-address", laddr, sizeof laddr);
    165 	OF_getprop(node, "assigned-addresses", reg, sizeof reg);
    166 
    167 	bcopy(laddr, sc->sc_laddr, sizeof laddr);
    168 	sc->sc_reg = reg[2];
    169 
    170 	if (pci_intr_map(pa, &ih)) {
    171 		printf(": unable to map interrupt\n");
    172 		return;
    173 	}
    174 	intrstr = pci_intr_string(pa->pa_pc, ih);
    175 
    176 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, gmac_intr, sc) == NULL) {
    177 		printf(": unable to establish interrupt");
    178 		if (intrstr)
    179 			printf(" at %s", intrstr);
    180 		printf("\n");
    181 		return;
    182 	}
    183 
    184 	/* Setup packet buffers and dma descriptors. */
    185 	p = malloc((NRXBUF + NTXBUF) * 2048 + 3 * 0x800, M_DEVBUF, M_NOWAIT);
    186 	if (p == NULL) {
    187 		printf(": cannot malloc buffers\n");
    188 		return;
    189 	}
    190 	p = (void *)roundup((vaddr_t)p, 0x800);
    191 	bzero(p, 2048 * (NRXBUF + NTXBUF) + 2 * 0x800);
    192 
    193 	sc->sc_rxlist = (void *)p;
    194 	p += 0x800;
    195 	sc->sc_txlist = (void *)p;
    196 	p += 0x800;
    197 
    198 	dp = sc->sc_rxlist;
    199 	for (i = 0; i < NRXBUF; i++) {
    200 		sc->sc_rxbuf[i] = p;
    201 		dp->address = htole32(vtophys((vaddr_t)p));
    202 		dp->cmd = htole32(GMAC_OWN);
    203 		dp++;
    204 		p += 2048;
    205 	}
    206 
    207 	dp = sc->sc_txlist;
    208 	for (i = 0; i < NTXBUF; i++) {
    209 		sc->sc_txbuf[i] = p;
    210 		dp->address = htole32(vtophys((vaddr_t)p));
    211 		dp++;
    212 		p += 2048;
    213 	}
    214 
    215 	printf(": Ethernet address %s\n", ether_sprintf(laddr));
    216 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    217 
    218 	callout_init(&sc->sc_tick_ch);
    219 
    220 	gmac_reset(sc);
    221 	gmac_init_mac(sc);
    222 
    223 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    224 	ifp->if_softc = sc;
    225 	ifp->if_ioctl = gmac_ioctl;
    226 	ifp->if_start = gmac_start;
    227 	ifp->if_watchdog = gmac_watchdog;
    228 	ifp->if_flags =
    229 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    230 
    231 	mii->mii_ifp = ifp;
    232 	mii->mii_readreg = gmac_mii_readreg;
    233 	mii->mii_writereg = gmac_mii_writereg;
    234 	mii->mii_statchg = gmac_mii_statchg;
    235 
    236 	ifmedia_init(&mii->mii_media, 0, gmac_mediachange, gmac_mediastatus);
    237 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    238 
    239 	/* Choose a default media. */
    240 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    241 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    242 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    243 	} else
    244 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    245 
    246 	if_attach(ifp);
    247 	ether_ifattach(ifp, laddr);
    248 }
    249 
    250 u_int
    251 gmac_read_reg(sc, reg)
    252 	struct gmac_softc *sc;
    253 	int reg;
    254 {
    255 	return in32rb(sc->sc_reg + reg);
    256 }
    257 
    258 void
    259 gmac_write_reg(sc, reg, val)
    260 	struct gmac_softc *sc;
    261 	int reg;
    262 	u_int val;
    263 {
    264 	out32rb(sc->sc_reg + reg, val);
    265 }
    266 
    267 void
    268 gmac_start_txdma(sc)
    269 	struct gmac_softc *sc;
    270 {
    271 	u_int x;
    272 
    273 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    274 	x |= 1;
    275 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    276 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    277 	x |= 1;
    278 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    279 }
    280 
    281 void
    282 gmac_start_rxdma(sc)
    283 	struct gmac_softc *sc;
    284 {
    285 	u_int x;
    286 
    287 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    288 	x |= 1;
    289 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    290 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    291 	x |= 1;
    292 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    293 }
    294 
    295 void
    296 gmac_stop_txdma(sc)
    297 	struct gmac_softc *sc;
    298 {
    299 	u_int x;
    300 
    301 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    302 	x &= ~1;
    303 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    304 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    305 	x &= ~1;
    306 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    307 }
    308 
    309 void
    310 gmac_stop_rxdma(sc)
    311 	struct gmac_softc *sc;
    312 {
    313 	u_int x;
    314 
    315 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    316 	x &= ~1;
    317 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    318 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    319 	x &= ~1;
    320 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    321 }
    322 
    323 int
    324 gmac_intr(v)
    325 	void *v;
    326 {
    327 	struct gmac_softc *sc = v;
    328 	u_int status;
    329 
    330 	status = gmac_read_reg(sc, GMAC_STATUS) & 0xff;
    331 	if (status == 0)
    332 		return 0;
    333 
    334 	if (status & GMAC_INT_RXDONE)
    335 		gmac_rint(sc);
    336 
    337 	if (status & GMAC_INT_TXEMPTY)
    338 		gmac_tint(sc);
    339 
    340 	return 1;
    341 }
    342 
    343 void
    344 gmac_tint(sc)
    345 	struct gmac_softc *sc;
    346 {
    347 	struct ifnet *ifp = &sc->sc_if;
    348 
    349 	ifp->if_flags &= ~IFF_OACTIVE;
    350 	ifp->if_timer = 0;
    351 	gmac_start(ifp);
    352 }
    353 
    354 void
    355 gmac_rint(sc)
    356 	struct gmac_softc *sc;
    357 {
    358 	struct ifnet *ifp = &sc->sc_if;
    359 	volatile struct gmac_dma *dp;
    360 	struct mbuf *m;
    361 	int i, j, len;
    362 	u_int cmd;
    363 
    364 	for (i = sc->sc_rxlast;; i++) {
    365 		if (i == NRXBUF)
    366 			i = 0;
    367 
    368 		dp = &sc->sc_rxlist[i];
    369 		cmd = le32toh(dp->cmd);
    370 		if (cmd & GMAC_OWN)
    371 			break;
    372 		len = (cmd >> 16) & GMAC_LEN_MASK;
    373 		len -= 4;	/* CRC */
    374 
    375 		if (le32toh(dp->cmd_hi) & 0x40000000) {
    376 			ifp->if_ierrors++;
    377 			goto next;
    378 		}
    379 
    380 		m = gmac_get(sc, sc->sc_rxbuf[i], len);
    381 		if (m == NULL) {
    382 			ifp->if_ierrors++;
    383 			goto next;
    384 		}
    385 
    386 #if NBPFILTER > 0
    387 		/*
    388 		 * Check if there's a BPF listener on this interface.
    389 		 * If so, hand off the raw packet to BPF.
    390 		 */
    391 		if (ifp->if_bpf)
    392 			bpf_tap(ifp->if_bpf, sc->sc_rxbuf[i], len);
    393 #endif
    394 		(*ifp->if_input)(ifp, m);
    395 		ifp->if_ipackets++;
    396 
    397 next:
    398 		dp->cmd_hi = 0;
    399 		__asm __volatile ("sync");
    400 		dp->cmd = htole32(GMAC_OWN);
    401 	}
    402 	sc->sc_rxlast = i;
    403 
    404 	/* XXX Make sure free buffers have GMAC_OWN. */
    405 	i++;
    406 	for (j = 1; j < NRXBUF; j++) {
    407 		if (i == NRXBUF)
    408 			i = 0;
    409 		dp = &sc->sc_rxlist[i++];
    410 		dp->cmd = htole32(GMAC_OWN);
    411 	}
    412 }
    413 
    414 struct mbuf *
    415 gmac_get(sc, pkt, totlen)
    416 	struct gmac_softc *sc;
    417 	caddr_t pkt;
    418 	int totlen;
    419 {
    420 	struct mbuf *m;
    421 	struct mbuf *top, **mp;
    422 	int len;
    423 
    424 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    425 	if (m == 0)
    426 		return 0;
    427 	m->m_pkthdr.rcvif = &sc->sc_if;
    428 	m->m_pkthdr.len = totlen;
    429 	len = MHLEN;
    430 	top = 0;
    431 	mp = &top;
    432 
    433 	while (totlen > 0) {
    434 		if (top) {
    435 			MGET(m, M_DONTWAIT, MT_DATA);
    436 			if (m == 0) {
    437 				m_freem(top);
    438 				return 0;
    439 			}
    440 			len = MLEN;
    441 		}
    442 		if (totlen >= MINCLSIZE) {
    443 			MCLGET(m, M_DONTWAIT);
    444 			if ((m->m_flags & M_EXT) == 0) {
    445 				m_free(m);
    446 				m_freem(top);
    447 				return 0;
    448 			}
    449 			len = MCLBYTES;
    450 		}
    451 		m->m_len = len = min(totlen, len);
    452 		bcopy(pkt, mtod(m, caddr_t), len);
    453 		pkt += len;
    454 		totlen -= len;
    455 		*mp = m;
    456 		mp = &m->m_next;
    457 	}
    458 
    459 	return top;
    460 }
    461 
    462 void
    463 gmac_start(ifp)
    464 	struct ifnet *ifp;
    465 {
    466 	struct gmac_softc *sc = ifp->if_softc;
    467 	struct mbuf *m;
    468 	caddr_t buff;
    469 	int i, tlen;
    470 	volatile struct gmac_dma *dp;
    471 
    472 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    473 		return;
    474 
    475 	for (;;) {
    476 		if (ifp->if_flags & IFF_OACTIVE)
    477 			break;
    478 
    479 		IF_DEQUEUE(&ifp->if_snd, m);
    480 		if (m == 0)
    481 			break;
    482 
    483 		/* 5 seconds to watch for failing to transmit */
    484 		ifp->if_timer = 5;
    485 		ifp->if_opackets++;		/* # of pkts */
    486 
    487 		i = sc->sc_txnext;
    488 		buff = sc->sc_txbuf[i];
    489 		tlen = gmac_put(sc, buff, m);
    490 
    491 		dp = &sc->sc_txlist[i];
    492 		dp->cmd_hi = 0;
    493 		dp->address_hi = 0;
    494 		dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP);
    495 
    496 		i++;
    497 		if (i == NTXBUF)
    498 			i = 0;
    499 		__asm __volatile ("sync");
    500 
    501 		gmac_write_reg(sc, GMAC_TXDMAKICK, i);
    502 		sc->sc_txnext = i;
    503 
    504 #if NBPFILTER > 0
    505 		/*
    506 		 * If BPF is listening on this interface, let it see the
    507 		 * packet before we commit it to the wire.
    508 		 */
    509 		if (ifp->if_bpf)
    510 			bpf_tap(ifp->if_bpf, buff, tlen);
    511 #endif
    512 
    513 		i++;
    514 		if (i == NTXBUF)
    515 			i = 0;
    516 		if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) {
    517 			ifp->if_flags |= IFF_OACTIVE;
    518 			break;
    519 		}
    520 	}
    521 }
    522 
    523 int
    524 gmac_put(sc, buff, m)
    525 	struct gmac_softc *sc;
    526 	caddr_t buff;
    527 	struct mbuf *m;
    528 {
    529 	struct mbuf *n;
    530 	int len, tlen = 0;
    531 
    532 	for (; m; m = n) {
    533 		len = m->m_len;
    534 		if (len == 0) {
    535 			MFREE(m, n);
    536 			continue;
    537 		}
    538 		bcopy(mtod(m, caddr_t), buff, len);
    539 		buff += len;
    540 		tlen += len;
    541 		MFREE(m, n);
    542 	}
    543 	if (tlen > 2048)
    544 		panic("%s: gmac_put packet overflow", sc->sc_dev.dv_xname);
    545 
    546 	return tlen;
    547 }
    548 
    549 void
    550 gmac_reset(sc)
    551 	struct gmac_softc *sc;
    552 {
    553 	int i, s;
    554 
    555 	s = splnet();
    556 
    557 	gmac_stop_txdma(sc);
    558 	gmac_stop_rxdma(sc);
    559 
    560 	gmac_write_reg(sc, GMAC_SOFTWARERESET, 3);
    561 	for (i = 10; i > 0; i--) {
    562 		delay(300000);				/* XXX long delay */
    563 		if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0)
    564 			break;
    565 	}
    566 	if (i == 0)
    567 		printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
    568 
    569 	sc->sc_txnext = 0;
    570 	sc->sc_rxlast = 0;
    571 	for (i = 0; i < NRXBUF; i++)
    572 		sc->sc_rxlist[i].cmd = htole32(GMAC_OWN);
    573 	__asm __volatile ("sync");
    574 
    575 	gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0);
    576 	gmac_write_reg(sc, GMAC_TXDMADESCBASELO,
    577 		       vtophys((vaddr_t)sc->sc_txlist));
    578 	gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0);
    579 	gmac_write_reg(sc, GMAC_RXDMADESCBASELO,
    580 		       vtophys((vaddr_t)sc->sc_rxlist));
    581 	gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF);
    582 
    583 	splx(s);
    584 }
    585 
    586 void
    587 gmac_stop(sc)
    588 	struct gmac_softc *sc;
    589 {
    590 	struct ifnet *ifp = &sc->sc_if;
    591 	int s;
    592 
    593 	s = splnet();
    594 
    595 	callout_stop(&sc->sc_tick_ch);
    596 	mii_down(&sc->sc_mii);
    597 
    598 	gmac_stop_txdma(sc);
    599 	gmac_stop_rxdma(sc);
    600 
    601 	gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff);
    602 
    603 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
    604 	ifp->if_timer = 0;
    605 
    606 	splx(s);
    607 }
    608 
    609 void
    610 gmac_init_mac(sc)
    611 	struct gmac_softc *sc;
    612 {
    613 	int i, tb;
    614 	char *laddr = sc->sc_laddr;
    615 
    616 	__asm ("mftb %0" : "=r"(tb));
    617 	gmac_write_reg(sc, GMAC_RANDOMSEED, tb);
    618 
    619 	/* init-mii */
    620 	gmac_write_reg(sc, GMAC_DATAPATHMODE, 4);
    621 	gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000);
    622 
    623 	gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00);
    624 	gmac_write_reg(sc, GMAC_RXDMACONFIG, 0);
    625 	gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0);
    626 	gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0);
    627 	gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8);
    628 	gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4);
    629 	gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN);
    630 	gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN);
    631 	gmac_write_reg(sc, GMAC_PASIZE, 7);
    632 	gmac_write_reg(sc, GMAC_JAMSIZE, 4);
    633 	gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10);
    634 	gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808);
    635 
    636 	gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]);
    637 	gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]);
    638 	gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]);
    639 	gmac_write_reg(sc, GMAC_MACADDRESS3, 0);
    640 	gmac_write_reg(sc, GMAC_MACADDRESS4, 0);
    641 	gmac_write_reg(sc, GMAC_MACADDRESS5, 0);
    642 	gmac_write_reg(sc, GMAC_MACADDRESS6, 1);
    643 	gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200);
    644 	gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180);
    645 	gmac_write_reg(sc, GMAC_MACADDRFILT0, 0);
    646 	gmac_write_reg(sc, GMAC_MACADDRFILT1, 0);
    647 	gmac_write_reg(sc, GMAC_MACADDRFILT2, 0);
    648 	gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0);
    649 	gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0);
    650 
    651 	for (i = 0; i < 0x6c; i += 4)
    652 		gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0);
    653 
    654 	gmac_write_reg(sc, GMAC_SLOTTIME, 0x40);
    655 
    656 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    657 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    658 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    659 	} else {
    660 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    661 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    662 	}
    663 
    664 	if (0)	/* g-bit? */
    665 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    666 	else
    667 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    668 }
    669 
    670 void
    671 gmac_setladrf(sc)
    672 	struct gmac_softc *sc;
    673 {
    674 	struct ifnet *ifp = &sc->sc_if;
    675 	struct ether_multi *enm;
    676 	struct ether_multistep step;
    677 	struct ethercom *ec = &sc->sc_ethercom;
    678 	u_int32_t crc;
    679 	u_int32_t hash[16];
    680 	u_int v;
    681 	int i;
    682 
    683 	/* Clear hash table */
    684 	for (i = 0; i < 16; i++)
    685 		hash[i] = 0;
    686 
    687 	/* Get current RX configuration */
    688 	v = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    689 
    690 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    691 		/* Turn on promiscuous mode; turn off the hash filter */
    692 		v |= GMAC_RXMAC_PR;
    693 		v &= ~GMAC_RXMAC_HEN;
    694 		ifp->if_flags |= IFF_ALLMULTI;
    695 		goto chipit;
    696 	}
    697 
    698 	/* Turn off promiscuous mode; turn on the hash filter */
    699 	v &= ~GMAC_RXMAC_PR;
    700 	v |= GMAC_RXMAC_HEN;
    701 
    702 	/*
    703 	 * Set up multicast address filter by passing all multicast addresses
    704 	 * through a crc generator, and then using the high order 8 bits as an
    705 	 * index into the 256 bit logical address filter.  The high order bit
    706 	 * selects the word, while the rest of the bits select the bit within
    707 	 * the word.
    708 	 */
    709 
    710 	ETHER_FIRST_MULTI(step, ec, enm);
    711 	while (enm != NULL) {
    712 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
    713 			/*
    714 			 * We must listen to a range of multicast addresses.
    715 			 * For now, just accept all multicasts, rather than
    716 			 * trying to set only those filter bits needed to match
    717 			 * the range.  (At this time, the only use of address
    718 			 * ranges is for IP multicast routing, for which the
    719 			 * range is big enough to require all bits set.)
    720 			 */
    721 			for (i = 0; i < 16; i++)
    722 				hash[i] = 0xffff;
    723 			ifp->if_flags |= IFF_ALLMULTI;
    724 			goto chipit;
    725 		}
    726 
    727 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    728 
    729 		/* Just want the 8 most significant bits. */
    730 		crc >>= 24;
    731 
    732 		/* Set the corresponding bit in the filter. */
    733 		hash[crc >> 4] |= 1 << (crc & 0xf);
    734 
    735 		ETHER_NEXT_MULTI(step, enm);
    736 	}
    737 
    738 	ifp->if_flags &= ~IFF_ALLMULTI;
    739 
    740 chipit:
    741 	/* Now load the hash table into the chip */
    742 	for (i = 0; i < 16; i++)
    743 		gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]);
    744 
    745 	gmac_write_reg(sc, GMAC_RXMACCONFIG, v);
    746 }
    747 
    748 void
    749 gmac_init(sc)
    750 	struct gmac_softc *sc;
    751 {
    752 	struct ifnet *ifp = &sc->sc_if;
    753 
    754 	gmac_stop_txdma(sc);
    755 	gmac_stop_rxdma(sc);
    756 
    757 	gmac_init_mac(sc);
    758 	gmac_setladrf(sc);
    759 
    760 	gmac_start_txdma(sc);
    761 	gmac_start_rxdma(sc);
    762 
    763 	gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE));
    764 
    765 	ifp->if_flags |= IFF_RUNNING;
    766 	ifp->if_flags &= ~IFF_OACTIVE;
    767 	ifp->if_timer = 0;
    768 
    769 	callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc);
    770 
    771 	gmac_start(ifp);
    772 }
    773 
    774 int
    775 gmac_ioctl(ifp, cmd, data)
    776 	struct ifnet *ifp;
    777 	u_long cmd;
    778 	caddr_t data;
    779 {
    780 	struct gmac_softc *sc = ifp->if_softc;
    781 	struct ifaddr *ifa = (struct ifaddr *)data;
    782 	struct ifreq *ifr = (struct ifreq *)data;
    783 	int s, error = 0;
    784 
    785 	s = splnet();
    786 
    787 	switch (cmd) {
    788 
    789 	case SIOCSIFADDR:
    790 		ifp->if_flags |= IFF_UP;
    791 
    792 		switch (ifa->ifa_addr->sa_family) {
    793 #ifdef INET
    794 		case AF_INET:
    795 			gmac_init(sc);
    796 			arp_ifinit(ifp, ifa);
    797 			break;
    798 #endif
    799 #ifdef NS
    800 		case AF_NS:
    801 		    {
    802 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    803 
    804 			if (ns_nullhost(*ina))
    805 				ina->x_host =
    806 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    807 			else {
    808 				bcopy(ina->x_host.c_host,
    809 				    LLADDR(ifp->if_sadl),
    810 				    sizeof(sc->sc_enaddr));
    811 			}
    812 			/* Set new address. */
    813 			gmac_init(sc);
    814 			break;
    815 		    }
    816 #endif
    817 		default:
    818 			gmac_init(sc);
    819 			break;
    820 		}
    821 		break;
    822 
    823 	case SIOCSIFFLAGS:
    824 		if ((ifp->if_flags & IFF_UP) == 0 &&
    825 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    826 			/*
    827 			 * If interface is marked down and it is running, then
    828 			 * stop it.
    829 			 */
    830 			gmac_stop(sc);
    831 			ifp->if_flags &= ~IFF_RUNNING;
    832 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    833 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    834 			/*
    835 			 * If interface is marked up and it is stopped, then
    836 			 * start it.
    837 			 */
    838 			gmac_init(sc);
    839 		} else {
    840 			/*
    841 			 * Reset the interface to pick up changes in any other
    842 			 * flags that affect hardware registers.
    843 			 */
    844 			gmac_reset(sc);
    845 			gmac_init(sc);
    846 		}
    847 #ifdef GMAC_DEBUG
    848 		if (ifp->if_flags & IFF_DEBUG)
    849 			sc->sc_flags |= GMAC_DEBUGFLAG;
    850 #endif
    851 		break;
    852 
    853 	case SIOCADDMULTI:
    854 	case SIOCDELMULTI:
    855 		error = (cmd == SIOCADDMULTI) ?
    856 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    857 		    ether_delmulti(ifr, &sc->sc_ethercom);
    858 
    859 		if (error == ENETRESET) {
    860 			/*
    861 			 * Multicast list has changed; set the hardware filter
    862 			 * accordingly.
    863 			 */
    864 			gmac_init(sc);
    865 			/* gmac_setladrf(sc); */
    866 			error = 0;
    867 		}
    868 		break;
    869 
    870 	case SIOCGIFMEDIA:
    871 	case SIOCSIFMEDIA:
    872 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    873 		break;
    874 
    875 	default:
    876 		error = EINVAL;
    877 	}
    878 
    879 	splx(s);
    880 	return error;
    881 }
    882 
    883 void
    884 gmac_watchdog(ifp)
    885 	struct ifnet *ifp;
    886 {
    887 	struct gmac_softc *sc = ifp->if_softc;
    888 
    889 	printf("%s: device timeout\n", ifp->if_xname);
    890 	ifp->if_oerrors++;
    891 
    892 	gmac_reset(sc);
    893 	gmac_init(sc);
    894 }
    895 
    896 int
    897 gmac_mediachange(ifp)
    898 	struct ifnet *ifp;
    899 {
    900 	struct gmac_softc *sc = ifp->if_softc;
    901 
    902 	return mii_mediachg(&sc->sc_mii);
    903 }
    904 
    905 void
    906 gmac_mediastatus(ifp, ifmr)
    907 	struct ifnet *ifp;
    908 	struct ifmediareq *ifmr;
    909 {
    910 	struct gmac_softc *sc = ifp->if_softc;
    911 
    912 	mii_pollstat(&sc->sc_mii);
    913 
    914 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    915 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    916 }
    917 
    918 int
    919 gmac_mii_readreg(dev, phy, reg)
    920 	struct device *dev;
    921 	int phy, reg;
    922 {
    923 	struct gmac_softc *sc = (void *)dev;
    924 	int i;
    925 
    926 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    927 		0x60020000 | (phy << 23) | (reg << 18));
    928 
    929 	for (i = 1000; i >= 0; i -= 10) {
    930 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    931 			break;
    932 		delay(10);
    933 	}
    934 	if (i < 0) {
    935 		printf("%s: gmac_mii_readreg: timeout\n", sc->sc_dev.dv_xname);
    936 		return 0;
    937 	}
    938 
    939 	return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff;
    940 }
    941 
    942 void
    943 gmac_mii_writereg(dev, phy, reg, val)
    944 	struct device *dev;
    945 	int phy, reg, val;
    946 {
    947 	struct gmac_softc *sc = (void *)dev;
    948 	int i;
    949 
    950 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    951 		0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff));
    952 
    953 	for (i = 1000; i >= 0; i -= 10) {
    954 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    955 			break;
    956 		delay(10);
    957 	}
    958 	if (i < 0)
    959 		printf("%s: gmac_mii_writereg: timeout\n", sc->sc_dev.dv_xname);
    960 }
    961 
    962 void
    963 gmac_mii_statchg(dev)
    964 	struct device *dev;
    965 {
    966 	struct gmac_softc *sc = (void *)dev;
    967 
    968 	gmac_stop_txdma(sc);
    969 	gmac_stop_rxdma(sc);
    970 
    971 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    972 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    973 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    974 	} else {
    975 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    976 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    977 	}
    978 
    979 	if (0)	/* g-bit? */
    980 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    981 	else
    982 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    983 
    984 	gmac_start_txdma(sc);
    985 	gmac_start_rxdma(sc);
    986 }
    987 
    988 void
    989 gmac_mii_tick(v)
    990 	void *v;
    991 {
    992 	struct gmac_softc *sc = v;
    993 	int s;
    994 
    995 	s = splnet();
    996 	mii_tick(&sc->sc_mii);
    997 	splx(s);
    998 
    999 	callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc);
   1000 }
   1001