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