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