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