Home | History | Annotate | Line # | Download | only in dev
if_gm.c revision 1.5
      1 /*	$NetBSD: if_gm.c,v 1.5 2000/04/02 12:03:04 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 <vm/vm.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 
    111 int gmac_ioctl __P((struct ifnet *, u_long, caddr_t));
    112 void gmac_watchdog __P((struct ifnet *));
    113 
    114 int gmac_mediachange __P((struct ifnet *));
    115 void gmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
    116 int gmac_mii_readreg __P((struct device *, int, int));
    117 void gmac_mii_writereg __P((struct device *, int, int, int));
    118 void gmac_mii_statchg __P((struct device *));
    119 void gmac_mii_tick __P((void *));
    120 
    121 struct cfattach gm_ca = {
    122 	sizeof(struct gmac_softc), gmac_match, gmac_attach
    123 };
    124 
    125 int
    126 gmac_match(parent, match, aux)
    127 	struct device *parent;
    128 	struct cfdata *match;
    129 	void *aux;
    130 {
    131 	struct pci_attach_args *pa = aux;
    132 
    133 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    134 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC)
    135 		return 1;
    136 
    137 	return 0;
    138 }
    139 
    140 void
    141 gmac_attach(parent, self, aux)
    142 	struct device *parent, *self;
    143 	void *aux;
    144 {
    145 	struct gmac_softc *sc = (void *)self;
    146 	struct pci_attach_args *pa = aux;
    147 	struct ifnet *ifp = &sc->sc_if;
    148 	struct mii_data *mii = &sc->sc_mii;
    149 	pci_intr_handle_t ih;
    150 	const char *intrstr = NULL;
    151 	int node, i;
    152 	char *p;
    153 	struct gmac_dma *dp;
    154 	u_int32_t reg[10];
    155 	u_char laddr[6];
    156 
    157 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    158 	if (node == 0) {
    159 		printf(": cannot find gmac node\n");
    160 		return;
    161 	}
    162 
    163 	OF_getprop(node, "local-mac-address", laddr, sizeof laddr);
    164 	OF_getprop(node, "assigned-addresses", reg, sizeof reg);
    165 
    166 	bcopy(laddr, sc->sc_laddr, sizeof laddr);
    167 	sc->sc_reg = reg[2];
    168 
    169 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    170 	    pa->pa_intrline, &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 	ifp->if_flags |= IFF_ALLMULTI;
    231 
    232 	mii->mii_ifp = ifp;
    233 	mii->mii_readreg = gmac_mii_readreg;
    234 	mii->mii_writereg = gmac_mii_writereg;
    235 	mii->mii_statchg = gmac_mii_statchg;
    236 
    237 	ifmedia_init(&mii->mii_media, 0, gmac_mediachange, gmac_mediastatus);
    238 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    239 
    240 	/* Choose a default media. */
    241 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    242 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    243 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    244 	} else
    245 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    246 
    247 	if_attach(ifp);
    248 	ether_ifattach(ifp, laddr);
    249 
    250 #if NBPFILTER > 0
    251 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    252 #endif
    253 }
    254 
    255 u_int
    256 gmac_read_reg(sc, reg)
    257 	struct gmac_softc *sc;
    258 	int reg;
    259 {
    260 	return in32rb(sc->sc_reg + reg);
    261 }
    262 
    263 void
    264 gmac_write_reg(sc, reg, val)
    265 	struct gmac_softc *sc;
    266 	int reg;
    267 	u_int val;
    268 {
    269 	out32rb(sc->sc_reg + reg, val);
    270 }
    271 
    272 void
    273 gmac_start_txdma(sc)
    274 	struct gmac_softc *sc;
    275 {
    276 	u_int x;
    277 
    278 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    279 	x |= 1;
    280 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    281 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    282 	x |= 1;
    283 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    284 }
    285 
    286 void
    287 gmac_start_rxdma(sc)
    288 	struct gmac_softc *sc;
    289 {
    290 	u_int x;
    291 
    292 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    293 	x |= 1;
    294 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    295 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    296 	x |= 1;
    297 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    298 }
    299 
    300 void
    301 gmac_stop_txdma(sc)
    302 	struct gmac_softc *sc;
    303 {
    304 	u_int x;
    305 
    306 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    307 	x &= ~1;
    308 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    309 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    310 	x &= ~1;
    311 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    312 }
    313 
    314 void
    315 gmac_stop_rxdma(sc)
    316 	struct gmac_softc *sc;
    317 {
    318 	u_int x;
    319 
    320 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    321 	x &= ~1;
    322 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    323 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    324 	x &= ~1;
    325 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    326 }
    327 
    328 int
    329 gmac_intr(v)
    330 	void *v;
    331 {
    332 	struct gmac_softc *sc = v;
    333 	u_int status;
    334 
    335 	status = gmac_read_reg(sc, GMAC_STATUS) & 0xff;
    336 	if (status == 0)
    337 		return 0;
    338 
    339 	if (status & GMAC_INT_RXDONE)
    340 		gmac_rint(sc);
    341 
    342 	if (status & GMAC_INT_TXEMPTY)
    343 		gmac_tint(sc);
    344 
    345 	return 1;
    346 }
    347 
    348 void
    349 gmac_tint(sc)
    350 	struct gmac_softc *sc;
    351 {
    352 	struct ifnet *ifp = &sc->sc_if;
    353 
    354 	ifp->if_flags &= ~IFF_OACTIVE;
    355 	ifp->if_timer = 0;
    356 	gmac_start(ifp);
    357 }
    358 
    359 void
    360 gmac_rint(sc)
    361 	struct gmac_softc *sc;
    362 {
    363 	struct ifnet *ifp = &sc->sc_if;
    364 	volatile struct gmac_dma *dp;
    365 	struct mbuf *m;
    366 	int i, len;
    367 	u_int cmd;
    368 
    369 	for (i = sc->sc_rxlast;; i++) {
    370 		if (i == NRXBUF)
    371 			i = 0;
    372 
    373 		dp = &sc->sc_rxlist[i];
    374 		cmd = le32toh(dp->cmd);
    375 		if (cmd & GMAC_OWN)
    376 			break;
    377 		len = (cmd >> 16) & GMAC_LEN_MASK;
    378 		len -= 4;	/* CRC */
    379 
    380 		if (le32toh(dp->cmd_hi) & 0x40000000) {
    381 			ifp->if_ierrors++;
    382 			goto next;
    383 		}
    384 
    385 		m = gmac_get(sc, sc->sc_rxbuf[i], len);
    386 		if (m == NULL) {
    387 			ifp->if_ierrors++;
    388 			goto next;
    389 		}
    390 
    391 #if NBPFILTER > 0
    392 		/*
    393 		 * Check if there's a BPF listener on this interface.
    394 		 * If so, hand off the raw packet to BPF.
    395 		 */
    396 		if (ifp->if_bpf)
    397 			bpf_tap(ifp->if_bpf, sc->sc_rxbuf[i], len);
    398 #endif
    399 		(*ifp->if_input)(ifp, m);
    400 		ifp->if_ipackets++;
    401 
    402 next:
    403 		dp->cmd_hi = 0;
    404 		__asm __volatile ("sync");
    405 		dp->cmd = htole32(GMAC_OWN);
    406 	}
    407 	sc->sc_rxlast = i;
    408 }
    409 
    410 struct mbuf *
    411 gmac_get(sc, pkt, totlen)
    412 	struct gmac_softc *sc;
    413 	caddr_t pkt;
    414 	int totlen;
    415 {
    416 	struct mbuf *m;
    417 	struct mbuf *top, **mp;
    418 	int len;
    419 
    420 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    421 	if (m == 0)
    422 		return 0;
    423 	m->m_pkthdr.rcvif = &sc->sc_if;
    424 	m->m_pkthdr.len = totlen;
    425 	len = MHLEN;
    426 	top = 0;
    427 	mp = &top;
    428 
    429 	while (totlen > 0) {
    430 		if (top) {
    431 			MGET(m, M_DONTWAIT, MT_DATA);
    432 			if (m == 0) {
    433 				m_freem(top);
    434 				return 0;
    435 			}
    436 			len = MLEN;
    437 		}
    438 		if (totlen >= MINCLSIZE) {
    439 			MCLGET(m, M_DONTWAIT);
    440 			if ((m->m_flags & M_EXT) == 0) {
    441 				m_free(m);
    442 				m_freem(top);
    443 				return 0;
    444 			}
    445 			len = MCLBYTES;
    446 		}
    447 		m->m_len = len = min(totlen, len);
    448 		bcopy(pkt, mtod(m, caddr_t), len);
    449 		pkt += len;
    450 		totlen -= len;
    451 		*mp = m;
    452 		mp = &m->m_next;
    453 	}
    454 
    455 	return top;
    456 }
    457 
    458 void
    459 gmac_start(ifp)
    460 	struct ifnet *ifp;
    461 {
    462 	struct gmac_softc *sc = ifp->if_softc;
    463 	struct mbuf *m;
    464 	caddr_t buff;
    465 	int i, tlen;
    466 	volatile struct gmac_dma *dp;
    467 
    468 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    469 		return;
    470 
    471 	for (;;) {
    472 		if (ifp->if_flags & IFF_OACTIVE)
    473 			break;
    474 
    475 		IF_DEQUEUE(&ifp->if_snd, m);
    476 		if (m == 0)
    477 			break;
    478 
    479 		/* 5 seconds to watch for failing to transmit */
    480 		ifp->if_timer = 5;
    481 		ifp->if_opackets++;		/* # of pkts */
    482 
    483 		i = sc->sc_txnext;
    484 		buff = sc->sc_txbuf[i];
    485 		tlen = gmac_put(sc, buff, m);
    486 
    487 		dp = &sc->sc_txlist[i];
    488 		dp->cmd_hi = 0;
    489 		dp->address_hi = 0;
    490 		dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP);
    491 
    492 		i++;
    493 		if (i == NTXBUF)
    494 			i = 0;
    495 		__asm __volatile ("sync");
    496 
    497 		gmac_write_reg(sc, GMAC_TXDMAKICK, i);
    498 		sc->sc_txnext = i;
    499 
    500 #if NBPFILTER > 0
    501 		/*
    502 		 * If BPF is listening on this interface, let it see the
    503 		 * packet before we commit it to the wire.
    504 		 */
    505 		if (ifp->if_bpf)
    506 			bpf_tap(ifp->if_bpf, buff, tlen);
    507 #endif
    508 
    509 		i++;
    510 		if (i == NTXBUF)
    511 			i = 0;
    512 		if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) {
    513 			ifp->if_flags |= IFF_OACTIVE;
    514 			break;
    515 		}
    516 	}
    517 }
    518 
    519 int
    520 gmac_put(sc, buff, m)
    521 	struct gmac_softc *sc;
    522 	caddr_t buff;
    523 	struct mbuf *m;
    524 {
    525 	struct mbuf *n;
    526 	int len, tlen = 0;
    527 
    528 	for (; m; m = n) {
    529 		len = m->m_len;
    530 		if (len == 0) {
    531 			MFREE(m, n);
    532 			continue;
    533 		}
    534 		bcopy(mtod(m, caddr_t), buff, len);
    535 		buff += len;
    536 		tlen += len;
    537 		MFREE(m, n);
    538 	}
    539 	if (tlen > 2048)
    540 		panic("%s: gmac_put packet overflow", sc->sc_dev.dv_xname);
    541 
    542 	return tlen;
    543 }
    544 
    545 void
    546 gmac_reset(sc)
    547 	struct gmac_softc *sc;
    548 {
    549 	int i, s;
    550 
    551 	s = splnet();
    552 
    553 	gmac_stop_txdma(sc);
    554 	gmac_stop_rxdma(sc);
    555 
    556 	gmac_write_reg(sc, GMAC_SOFTWARERESET, 3);
    557 	for (i = 10; i > 0; i--) {
    558 		delay(300000);				/* XXX long delay */
    559 		if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0)
    560 			break;
    561 	}
    562 	if (i == 0)
    563 		printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
    564 
    565 	sc->sc_txnext = 0;
    566 	sc->sc_rxlast = 0;
    567 	for (i = 0; i < NRXBUF; i++)
    568 		sc->sc_rxlist[i].cmd = htole32(GMAC_OWN);
    569 	__asm __volatile ("sync");
    570 
    571 	gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0);
    572 	gmac_write_reg(sc, GMAC_TXDMADESCBASELO,
    573 		       vtophys((vaddr_t)sc->sc_txlist));
    574 	gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0);
    575 	gmac_write_reg(sc, GMAC_RXDMADESCBASELO,
    576 		       vtophys((vaddr_t)sc->sc_rxlist));
    577 	gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF);
    578 
    579 	splx(s);
    580 }
    581 
    582 void
    583 gmac_stop(sc)
    584 	struct gmac_softc *sc;
    585 {
    586 	struct ifnet *ifp = &sc->sc_if;
    587 	int s;
    588 
    589 	s = splnet();
    590 
    591 	callout_stop(&sc->sc_tick_ch);
    592 	mii_down(&sc->sc_mii);
    593 
    594 	gmac_stop_txdma(sc);
    595 	gmac_stop_rxdma(sc);
    596 
    597 	gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff);
    598 
    599 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
    600 	ifp->if_timer = 0;
    601 
    602 	splx(s);
    603 }
    604 
    605 void
    606 gmac_init_mac(sc)
    607 	struct gmac_softc *sc;
    608 {
    609 	int i, tb;
    610 	char *laddr = sc->sc_laddr;
    611 
    612 	__asm ("mftb %0" : "=r"(tb));
    613 	gmac_write_reg(sc, GMAC_RANDOMSEED, tb);
    614 
    615 	/* init-mii */
    616 	gmac_write_reg(sc, GMAC_DATAPATHMODE, 4);
    617 	gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000);
    618 
    619 	gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00);
    620 	gmac_write_reg(sc, GMAC_RXDMACONFIG, 0);
    621 	gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0);
    622 	gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0);
    623 	gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8);
    624 	gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4);
    625 	gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN);
    626 	gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN);
    627 	gmac_write_reg(sc, GMAC_PASIZE, 7);
    628 	gmac_write_reg(sc, GMAC_JAMSIZE, 4);
    629 	gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10);
    630 	gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808);
    631 
    632 	gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]);
    633 	gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]);
    634 	gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]);
    635 	gmac_write_reg(sc, GMAC_MACADDRESS3, 0);
    636 	gmac_write_reg(sc, GMAC_MACADDRESS4, 0);
    637 	gmac_write_reg(sc, GMAC_MACADDRESS5, 0);
    638 	gmac_write_reg(sc, GMAC_MACADDRESS6, 1);
    639 	gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200);
    640 	gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180);
    641 	gmac_write_reg(sc, GMAC_MACADDRFILT0, 0);
    642 	gmac_write_reg(sc, GMAC_MACADDRFILT1, 0);
    643 	gmac_write_reg(sc, GMAC_MACADDRFILT2, 0);
    644 	gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0);
    645 	gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0);
    646 
    647 	for (i = 0; i < 0x6c; i+= 4)
    648 		gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0);
    649 
    650 	gmac_write_reg(sc, GMAC_SLOTTIME, 0x40);
    651 
    652 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    653 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    654 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    655 	} else {
    656 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    657 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    658 	}
    659 
    660 	if (0)	/* g-bit? */
    661 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    662 	else
    663 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    664 }
    665 
    666 void
    667 gmac_init(sc)
    668 	struct gmac_softc *sc;
    669 {
    670 	struct ifnet *ifp = &sc->sc_if;
    671 	u_int x;
    672 	int i;
    673 
    674 	gmac_stop_txdma(sc);
    675 	gmac_stop_rxdma(sc);
    676 
    677 	gmac_init_mac(sc);
    678 
    679 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    680 	if (ifp->if_flags & IFF_PROMISC)
    681 		x |= GMAC_RXMAC_PR;
    682 	else
    683 		x &= ~GMAC_RXMAC_PR;
    684 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    685 
    686 	gmac_start_txdma(sc);
    687 	gmac_start_rxdma(sc);
    688 
    689 	gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE));
    690 
    691 	ifp->if_flags |= IFF_RUNNING;
    692 	ifp->if_flags &= ~IFF_OACTIVE;
    693 	ifp->if_timer = 0;
    694 
    695 	callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc);
    696 
    697 	gmac_start(ifp);
    698 }
    699 
    700 int
    701 gmac_ioctl(ifp, cmd, data)
    702 	struct ifnet *ifp;
    703 	u_long cmd;
    704 	caddr_t data;
    705 {
    706 	struct gmac_softc *sc = ifp->if_softc;
    707 	struct ifaddr *ifa = (struct ifaddr *)data;
    708 	struct ifreq *ifr = (struct ifreq *)data;
    709 	int s, error = 0;
    710 
    711 	s = splnet();
    712 
    713 	switch (cmd) {
    714 
    715 	case SIOCSIFADDR:
    716 		ifp->if_flags |= IFF_UP;
    717 
    718 		switch (ifa->ifa_addr->sa_family) {
    719 #ifdef INET
    720 		case AF_INET:
    721 			gmac_init(sc);
    722 			arp_ifinit(ifp, ifa);
    723 			break;
    724 #endif
    725 #ifdef NS
    726 		case AF_NS:
    727 		    {
    728 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    729 
    730 			if (ns_nullhost(*ina))
    731 				ina->x_host =
    732 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    733 			else {
    734 				bcopy(ina->x_host.c_host,
    735 				    LLADDR(ifp->if_sadl),
    736 				    sizeof(sc->sc_enaddr));
    737 			}
    738 			/* Set new address. */
    739 			gmac_init(sc);
    740 			break;
    741 		    }
    742 #endif
    743 		default:
    744 			gmac_init(sc);
    745 			break;
    746 		}
    747 		break;
    748 
    749 	case SIOCSIFFLAGS:
    750 		if ((ifp->if_flags & IFF_UP) == 0 &&
    751 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    752 			/*
    753 			 * If interface is marked down and it is running, then
    754 			 * stop it.
    755 			 */
    756 			gmac_stop(sc);
    757 			ifp->if_flags &= ~IFF_RUNNING;
    758 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    759 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    760 			/*
    761 			 * If interface is marked up and it is stopped, then
    762 			 * start it.
    763 			 */
    764 			gmac_init(sc);
    765 		} else {
    766 			/*
    767 			 * Reset the interface to pick up changes in any other
    768 			 * flags that affect hardware registers.
    769 			 */
    770 			gmac_reset(sc);
    771 			gmac_init(sc);
    772 		}
    773 #ifdef GMAC_DEBUG
    774 		if (ifp->if_flags & IFF_DEBUG)
    775 			sc->sc_flags |= GMAC_DEBUGFLAG;
    776 #endif
    777 		break;
    778 
    779 	case SIOCADDMULTI:
    780 	case SIOCDELMULTI:
    781 		error = (cmd == SIOCADDMULTI) ?
    782 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    783 		    ether_delmulti(ifr, &sc->sc_ethercom);
    784 
    785 		if (error == ENETRESET) {
    786 			/*
    787 			 * Multicast list has changed; set the hardware filter
    788 			 * accordingly.
    789 			 */
    790 			gmac_init(sc);
    791 			/* gmac_setladrf(sc); */
    792 			error = 0;
    793 		}
    794 		break;
    795 
    796 	case SIOCGIFMEDIA:
    797 	case SIOCSIFMEDIA:
    798 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    799 		break;
    800 
    801 	default:
    802 		error = EINVAL;
    803 	}
    804 
    805 	splx(s);
    806 	return error;
    807 }
    808 
    809 void
    810 gmac_watchdog(ifp)
    811 	struct ifnet *ifp;
    812 {
    813 	struct gmac_softc *sc = ifp->if_softc;
    814 
    815 	printf("%s: device timeout\n", ifp->if_xname);
    816 	ifp->if_oerrors++;
    817 
    818 	gmac_reset(sc);
    819 	gmac_init(sc);
    820 }
    821 
    822 int
    823 gmac_mediachange(ifp)
    824 	struct ifnet *ifp;
    825 {
    826 	struct gmac_softc *sc = ifp->if_softc;
    827 
    828 	return mii_mediachg(&sc->sc_mii);
    829 }
    830 
    831 void
    832 gmac_mediastatus(ifp, ifmr)
    833 	struct ifnet *ifp;
    834 	struct ifmediareq *ifmr;
    835 {
    836 	struct gmac_softc *sc = ifp->if_softc;
    837 
    838 	mii_pollstat(&sc->sc_mii);
    839 
    840 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    841 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    842 }
    843 
    844 int
    845 gmac_mii_readreg(dev, phy, reg)
    846 	struct device *dev;
    847 	int phy, reg;
    848 {
    849 	struct gmac_softc *sc = (void *)dev;
    850 	int i;
    851 
    852 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    853 		0x60020000 | (phy << 23) | (reg << 18));
    854 
    855 	for (i = 1000; i >= 0; i -= 10) {
    856 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    857 			break;
    858 		delay(10);
    859 	}
    860 	if (i < 0) {
    861 		printf("%s: gmac_mii_readreg: timeout\n", sc->sc_dev.dv_xname);
    862 		return 0;
    863 	}
    864 
    865 	return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff;
    866 }
    867 
    868 void
    869 gmac_mii_writereg(dev, phy, reg, val)
    870 	struct device *dev;
    871 	int phy, reg, val;
    872 {
    873 	struct gmac_softc *sc = (void *)dev;
    874 	int i;
    875 
    876 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    877 		0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff));
    878 
    879 	for (i = 1000; i >= 0; i -= 10) {
    880 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    881 			break;
    882 		delay(10);
    883 	}
    884 	if (i < 0)
    885 		printf("%s: gmac_mii_writereg: timeout\n", sc->sc_dev.dv_xname);
    886 }
    887 
    888 void
    889 gmac_mii_statchg(dev)
    890 	struct device *dev;
    891 {
    892 	struct gmac_softc *sc = (void *)dev;
    893 
    894 	gmac_stop_txdma(sc);
    895 	gmac_stop_rxdma(sc);
    896 
    897 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    898 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    899 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    900 	} else {
    901 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    902 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    903 	}
    904 
    905 	if (0)	/* g-bit? */
    906 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    907 	else
    908 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    909 
    910 	gmac_start_txdma(sc);
    911 	gmac_start_rxdma(sc);
    912 }
    913 
    914 void
    915 gmac_mii_tick(v)
    916 	void *v;
    917 {
    918 	struct gmac_softc *sc = v;
    919 	int s;
    920 
    921 	s = splnet();
    922 	mii_tick(&sc->sc_mii);
    923 	splx(s);
    924 
    925 	callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc);
    926 }
    927