Home | History | Annotate | Line # | Download | only in ep93xx
epe.c revision 1.27.2.2
      1 /*	$NetBSD: epe.c,v 1.27.2.2 2014/08/20 00:02:45 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Jesse Off
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: epe.c,v 1.27.2.2 2014/08/20 00:02:45 tls Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/kernel.h>
     37 #include <sys/proc.h>
     38 #include <sys/malloc.h>
     39 #include <sys/time.h>
     40 #include <sys/device.h>
     41 #include <uvm/uvm_extern.h>
     42 
     43 #include <sys/bus.h>
     44 #include <machine/intr.h>
     45 
     46 #include <arm/cpufunc.h>
     47 
     48 #include <arm/ep93xx/epsocvar.h>
     49 #include <arm/ep93xx/ep93xxvar.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_types.h>
     54 #include <net/if_media.h>
     55 #include <net/if_ether.h>
     56 
     57 #include <dev/mii/mii.h>
     58 #include <dev/mii/miivar.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.h>
     65 #include <netinet/if_inarp.h>
     66 #endif
     67 
     68 #ifdef NS
     69 #include <netns/ns.h>
     70 #include <netns/ns_if.h>
     71 #endif
     72 
     73 #include <net/bpf.h>
     74 #include <net/bpfdesc.h>
     75 
     76 #include <arm/ep93xx/ep93xxreg.h>
     77 #include <arm/ep93xx/epereg.h>
     78 #include <arm/ep93xx/epevar.h>
     79 
     80 #define DEFAULT_MDCDIV	32
     81 
     82 #ifndef EPE_FAST
     83 #define EPE_FAST
     84 #endif
     85 
     86 #ifndef EPE_FAST
     87 #define EPE_READ(x) \
     88 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
     89 #define EPE_WRITE(x, y) \
     90 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
     91 #define CTRLPAGE_DMASYNC(x, y, z) \
     92 	bus_dmamap_sync(sc->sc_dmat, sc->ctrlpage_dmamap, (x), (y), (z))
     93 #else
     94 #define EPE_READ(x) *(volatile uint32_t *) \
     95 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x))
     96 #define EPE_WRITE(x, y) *(volatile uint32_t *) \
     97 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x)) = y
     98 #define CTRLPAGE_DMASYNC(x, y, z)
     99 #endif /* ! EPE_FAST */
    100 
    101 static int	epe_match(device_t , cfdata_t, void *);
    102 static void	epe_attach(device_t, device_t, void *);
    103 static void	epe_init(struct epe_softc *);
    104 static int      epe_intr(void* arg);
    105 static int	epe_gctx(struct epe_softc *);
    106 static int	epe_mediachange(struct ifnet *);
    107 int		epe_mii_readreg (device_t, int, int);
    108 void		epe_mii_writereg (device_t, int, int, int);
    109 void		epe_statchg (struct ifnet *);
    110 void		epe_tick (void *);
    111 static int	epe_ifioctl (struct ifnet *, u_long, void *);
    112 static void	epe_ifstart (struct ifnet *);
    113 static void	epe_ifwatchdog (struct ifnet *);
    114 static int	epe_ifinit (struct ifnet *);
    115 static void	epe_ifstop (struct ifnet *, int);
    116 static void	epe_setaddr (struct ifnet *);
    117 
    118 CFATTACH_DECL_NEW(epe, sizeof(struct epe_softc),
    119     epe_match, epe_attach, NULL, NULL);
    120 
    121 static int
    122 epe_match(device_t parent, cfdata_t match, void *aux)
    123 {
    124 	return 2;
    125 }
    126 
    127 static void
    128 epe_attach(device_t parent, device_t self, void *aux)
    129 {
    130 	struct epe_softc		*sc = device_private(self);
    131 	struct epsoc_attach_args	*sa;
    132 	prop_data_t			 enaddr;
    133 
    134 	aprint_normal("\n");
    135 	sa = aux;
    136 	sc->sc_dev = self;
    137 	sc->sc_iot = sa->sa_iot;
    138 	sc->sc_intr = sa->sa_intr;
    139 	sc->sc_dmat = sa->sa_dmat;
    140 
    141 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
    142 		0, &sc->sc_ioh))
    143 		panic("%s: Cannot map registers", device_xname(self));
    144 
    145 	/* Fetch the Ethernet address from property if set. */
    146 	enaddr = prop_dictionary_get(device_properties(self), "mac-address");
    147 	if (enaddr != NULL) {
    148 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
    149 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
    150 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
    151 		       ETHER_ADDR_LEN);
    152 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
    153 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    154 					 sc->sc_enaddr, ETHER_ADDR_LEN);
    155 	}
    156 
    157         ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
    158 	epe_init(sc);
    159 }
    160 
    161 static int
    162 epe_gctx(struct epe_softc *sc)
    163 {
    164 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    165 	uint32_t *cur, ndq = 0;
    166 
    167 	/* Handle transmit completions */
    168 	cur = (uint32_t *)(EPE_READ(TXStsQCurAdd) -
    169 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
    170 
    171 	if (sc->TXStsQ_cur != cur) {
    172 		CTRLPAGE_DMASYNC(TX_QLEN * 2 * sizeof(uint32_t),
    173 			TX_QLEN * sizeof(uint32_t), BUS_DMASYNC_PREREAD);
    174 	} else {
    175 		return 0;
    176 	}
    177 
    178 	do {
    179 		uint32_t tbi = *sc->TXStsQ_cur & 0x7fff;
    180 		struct mbuf *m = sc->txq[tbi].m;
    181 
    182 		if ((*sc->TXStsQ_cur & TXStsQ_TxWE) == 0) {
    183 			ifp->if_oerrors++;
    184 		}
    185 		bus_dmamap_unload(sc->sc_dmat, sc->txq[tbi].m_dmamap);
    186 		m_freem(m);
    187 		do {
    188 			sc->txq[tbi].m = NULL;
    189 			ndq++;
    190 			tbi = (tbi + 1) % TX_QLEN;
    191 		} while (sc->txq[tbi].m == m);
    192 
    193 		ifp->if_opackets++;
    194 		sc->TXStsQ_cur++;
    195 		if (sc->TXStsQ_cur >= sc->TXStsQ + TX_QLEN) {
    196 			sc->TXStsQ_cur = sc->TXStsQ;
    197 		}
    198 	} while (sc->TXStsQ_cur != cur);
    199 
    200 	sc->TXDQ_avail += ndq;
    201 	if (ifp->if_flags & IFF_OACTIVE) {
    202 		ifp->if_flags &= ~IFF_OACTIVE;
    203 		/* Disable end-of-tx-chain interrupt */
    204 		EPE_WRITE(IntEn, IntEn_REOFIE);
    205 	}
    206 	return ndq;
    207 }
    208 
    209 static int
    210 epe_intr(void *arg)
    211 {
    212 	struct epe_softc *sc = (struct epe_softc *)arg;
    213 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    214 	uint32_t ndq = 0, irq, *cur;
    215 
    216 	irq = EPE_READ(IntStsC);
    217 begin:
    218 	cur = (uint32_t *)(EPE_READ(RXStsQCurAdd) -
    219 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
    220 	CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
    221 		RX_QLEN * 4 * sizeof(uint32_t),
    222 		BUS_DMASYNC_PREREAD);
    223 	while (sc->RXStsQ_cur != cur) {
    224 		if ((sc->RXStsQ_cur[0] & (RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) ==
    225 			(RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) {
    226 			uint32_t bi = (sc->RXStsQ_cur[1] >> 16) & 0x7fff;
    227 			uint32_t fl = sc->RXStsQ_cur[1] & 0xffff;
    228 			struct mbuf *m;
    229 
    230 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    231 			if (m != NULL) MCLGET(m, M_DONTWAIT);
    232 			if (m != NULL && (m->m_flags & M_EXT)) {
    233 				bus_dmamap_unload(sc->sc_dmat,
    234 					sc->rxq[bi].m_dmamap);
    235 				sc->rxq[bi].m->m_pkthdr.rcvif = ifp;
    236 				sc->rxq[bi].m->m_pkthdr.len =
    237 					sc->rxq[bi].m->m_len = fl;
    238 				bpf_mtap(ifp, sc->rxq[bi].m);
    239                                 (*ifp->if_input)(ifp, sc->rxq[bi].m);
    240 				sc->rxq[bi].m = m;
    241 				bus_dmamap_load(sc->sc_dmat,
    242 					sc->rxq[bi].m_dmamap,
    243 					m->m_ext.ext_buf, MCLBYTES,
    244 					NULL, BUS_DMA_NOWAIT);
    245 				sc->RXDQ[bi * 2] =
    246 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr;
    247 			} else {
    248 				/* Drop packets until we can get replacement
    249 				 * empty mbufs for the RXDQ.
    250 				 */
    251 				if (m != NULL) {
    252 					m_freem(m);
    253 				}
    254 				ifp->if_ierrors++;
    255 			}
    256 		} else {
    257 			ifp->if_ierrors++;
    258 		}
    259 
    260 		ndq++;
    261 
    262 		sc->RXStsQ_cur += 2;
    263 		if (sc->RXStsQ_cur >= sc->RXStsQ + (RX_QLEN * 2)) {
    264 			sc->RXStsQ_cur = sc->RXStsQ;
    265 		}
    266 	}
    267 
    268 	if (ndq > 0) {
    269 		ifp->if_ipackets += ndq;
    270 		CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
    271  			RX_QLEN * 4 * sizeof(uint32_t),
    272 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    273 		EPE_WRITE(RXStsEnq, ndq);
    274 		EPE_WRITE(RXDEnq, ndq);
    275 		ndq = 0;
    276 	}
    277 
    278 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    279 		epe_ifstart(ifp);
    280 	}
    281 
    282 	irq = EPE_READ(IntStsC);
    283 	if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
    284 		goto begin;
    285 
    286 	return (1);
    287 }
    288 
    289 
    290 static void
    291 epe_init(struct epe_softc *sc)
    292 {
    293 	bus_dma_segment_t segs;
    294 	char *addr;
    295 	int rsegs, err, i;
    296 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    297 	int mdcdiv = DEFAULT_MDCDIV;
    298 
    299 	callout_init(&sc->epe_tick_ch, 0);
    300 
    301 	/* Select primary Individual Address in Address Filter Pointer */
    302 	EPE_WRITE(AFP, 0);
    303 	/* Read ethernet MAC, should already be set by bootrom */
    304 	bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    305 		sc->sc_enaddr, ETHER_ADDR_LEN);
    306 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
    307 		ether_sprintf(sc->sc_enaddr));
    308 
    309 	/* Soft Reset the MAC */
    310 	EPE_WRITE(SelfCtl, SelfCtl_RESET);
    311 	while(EPE_READ(SelfCtl) & SelfCtl_RESET);
    312 
    313 	/* suggested magic initialization values from datasheet */
    314 	EPE_WRITE(RXBufThrshld, 0x800040);
    315 	EPE_WRITE(TXBufThrshld, 0x200010);
    316 	EPE_WRITE(RXStsThrshld, 0x40002);
    317 	EPE_WRITE(TXStsThrshld, 0x40002);
    318 	EPE_WRITE(RXDThrshld, 0x40002);
    319 	EPE_WRITE(TXDThrshld, 0x40002);
    320 
    321 	/* Allocate a page of memory for descriptor and status queues */
    322 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
    323 		&segs, 1, &rsegs, BUS_DMA_WAITOK);
    324 	if (err == 0) {
    325 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
    326 			&sc->ctrlpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
    327 	}
    328 	if (err == 0) {
    329 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    330 			0, BUS_DMA_WAITOK, &sc->ctrlpage_dmamap);
    331 	}
    332 	if (err == 0) {
    333 		err = bus_dmamap_load(sc->sc_dmat, sc->ctrlpage_dmamap,
    334 			sc->ctrlpage, PAGE_SIZE, NULL, BUS_DMA_WAITOK);
    335 	}
    336 	if (err != 0) {
    337 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
    338 	}
    339 	sc->ctrlpage_dsaddr = sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
    340 	memset(sc->ctrlpage, 0, PAGE_SIZE);
    341 
    342 	/* Set up pointers to start of each queue in kernel addr space.
    343 	 * Each descriptor queue or status queue entry uses 2 words
    344 	 */
    345 	sc->TXDQ = (uint32_t *)sc->ctrlpage;
    346 	sc->TXDQ_cur = sc->TXDQ;
    347 	sc->TXDQ_avail = TX_QLEN - 1;
    348 	sc->TXStsQ = &sc->TXDQ[TX_QLEN * 2];
    349 	sc->TXStsQ_cur = sc->TXStsQ;
    350 	sc->RXDQ = &sc->TXStsQ[TX_QLEN];
    351 	sc->RXStsQ = &sc->RXDQ[RX_QLEN * 2];
    352 	sc->RXStsQ_cur = sc->RXStsQ;
    353 
    354 	/* Program each queue's start addr, cur addr, and len registers
    355 	 * with the physical addresses.
    356 	 */
    357 	addr = (char *)sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
    358 	EPE_WRITE(TXDQBAdd, (uint32_t)addr);
    359 	EPE_WRITE(TXDQCurAdd, (uint32_t)addr);
    360 	EPE_WRITE(TXDQBLen, TX_QLEN * 2 * sizeof(uint32_t));
    361 
    362 	addr += (sc->TXStsQ - sc->TXDQ) * sizeof(uint32_t);
    363 	EPE_WRITE(TXStsQBAdd, (uint32_t)addr);
    364 	EPE_WRITE(TXStsQCurAdd, (uint32_t)addr);
    365 	EPE_WRITE(TXStsQBLen, TX_QLEN * sizeof(uint32_t));
    366 
    367 	addr += (sc->RXDQ - sc->TXStsQ) * sizeof(uint32_t);
    368 	EPE_WRITE(RXDQBAdd, (uint32_t)addr);
    369 	EPE_WRITE(RXDCurAdd, (uint32_t)addr);
    370 	EPE_WRITE(RXDQBLen, RX_QLEN * 2 * sizeof(uint32_t));
    371 
    372 	addr += (sc->RXStsQ - sc->RXDQ) * sizeof(uint32_t);
    373 	EPE_WRITE(RXStsQBAdd, (uint32_t)addr);
    374 	EPE_WRITE(RXStsQCurAdd, (uint32_t)addr);
    375 	EPE_WRITE(RXStsQBLen, RX_QLEN * 2 * sizeof(uint32_t));
    376 
    377 	/* Populate the RXDQ with mbufs */
    378 	for(i = 0; i < RX_QLEN; i++) {
    379 		struct mbuf *m;
    380 
    381 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, TX_QLEN/4, MCLBYTES, 0,
    382 			BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
    383 		MGETHDR(m, M_WAIT, MT_DATA);
    384 		MCLGET(m, M_WAIT);
    385 		sc->rxq[i].m = m;
    386 		bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
    387 			m->m_ext.ext_buf, MCLBYTES, NULL,
    388 			BUS_DMA_WAITOK);
    389 
    390 		sc->RXDQ[i * 2] = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr;
    391 		sc->RXDQ[i * 2 + 1] = (i << 16) | MCLBYTES;
    392 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
    393 			MCLBYTES, BUS_DMASYNC_PREREAD);
    394 	}
    395 
    396 	for(i = 0; i < TX_QLEN; i++) {
    397 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
    398 			(BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW),
    399 			&sc->txq[i].m_dmamap);
    400 		sc->txq[i].m = NULL;
    401 		sc->TXDQ[i * 2 + 1] = (i << 16);
    402 	}
    403 
    404 	/* Divide HCLK by 32 for MDC clock */
    405 	if (device_cfdata(sc->sc_dev)->cf_flags)
    406 		mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
    407 	EPE_WRITE(SelfCtl, (SelfCtl_MDCDIV(mdcdiv)|SelfCtl_PSPRS));
    408 
    409 	sc->sc_mii.mii_ifp = ifp;
    410 	sc->sc_mii.mii_readreg = epe_mii_readreg;
    411 	sc->sc_mii.mii_writereg = epe_mii_writereg;
    412 	sc->sc_mii.mii_statchg = epe_statchg;
    413 	sc->sc_ec.ec_mii = &sc->sc_mii;
    414 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, epe_mediachange,
    415 		ether_mediastatus);
    416 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    417 		MII_OFFSET_ANY, 0);
    418 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    419 
    420 	EPE_WRITE(BMCtl, BMCtl_RxEn|BMCtl_TxEn);
    421 	EPE_WRITE(IntEn, IntEn_REOFIE);
    422 	/* maximum valid max frame length */
    423 	EPE_WRITE(MaxFrmLen, (0x7ff << 16)|MHLEN);
    424 	/* wait for receiver ready */
    425 	while((EPE_READ(BMSts) & BMSts_RxAct) == 0)
    426 		continue;
    427 	/* enqueue the entries in RXStsQ and RXDQ */
    428 	CTRLPAGE_DMASYNC(0, sc->ctrlpage_dmamap->dm_mapsize,
    429 		BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    430 	EPE_WRITE(RXDEnq, RX_QLEN - 1);
    431 	EPE_WRITE(RXStsEnq, RX_QLEN - 1);
    432 
    433 	/*
    434 	 * We can support 802.1Q VLAN-sized frames.
    435 	 */
    436 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    437 
    438         strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    439         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
    440         ifp->if_ioctl = epe_ifioctl;
    441         ifp->if_start = epe_ifstart;
    442         ifp->if_watchdog = epe_ifwatchdog;
    443         ifp->if_init = epe_ifinit;
    444         ifp->if_stop = epe_ifstop;
    445         ifp->if_timer = 0;
    446 	ifp->if_softc = sc;
    447         IFQ_SET_READY(&ifp->if_snd);
    448         if_attach(ifp);
    449         ether_ifattach(ifp, (sc)->sc_enaddr);
    450 }
    451 
    452 static int
    453 epe_mediachange(struct ifnet *ifp)
    454 {
    455 	if (ifp->if_flags & IFF_UP)
    456 		epe_ifinit(ifp);
    457 	return (0);
    458 }
    459 
    460 int
    461 epe_mii_readreg(device_t self, int phy, int reg)
    462 {
    463 	uint32_t d, v;
    464 
    465 	d = EPE_READ(SelfCtl);
    466 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
    467 	EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg));
    468 	while(EPE_READ(MIISts) & MIISts_BUSY);
    469 	v = EPE_READ(MIIData);
    470 	EPE_WRITE(SelfCtl, d); /* restore old value */
    471 	return v;
    472 }
    473 
    474 void
    475 epe_mii_writereg(device_t self, int phy, int reg, int val)
    476 {
    477 	uint32_t d;
    478 
    479 	d = EPE_READ(SelfCtl);
    480 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
    481 	EPE_WRITE(MIIData, val);
    482 	EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg));
    483 	while(EPE_READ(MIISts) & MIISts_BUSY);
    484 	EPE_WRITE(SelfCtl, d); /* restore old value */
    485 }
    486 
    487 
    488 void
    489 epe_statchg(struct ifnet *ifp)
    490 {
    491         struct epe_softc *sc = ifp->if_softc;
    492         uint32_t reg;
    493 
    494         /*
    495          * We must keep the MAC and the PHY in sync as
    496          * to the status of full-duplex!
    497          */
    498         reg = EPE_READ(TestCtl);
    499         if (sc->sc_mii.mii_media_active & IFM_FDX)
    500                 reg |= TestCtl_MFDX;
    501         else
    502                 reg &= ~TestCtl_MFDX;
    503 	EPE_WRITE(TestCtl, reg);
    504 }
    505 
    506 void
    507 epe_tick(void *arg)
    508 {
    509 	struct epe_softc* sc = (struct epe_softc *)arg;
    510 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    511 	int s;
    512 	uint32_t misses;
    513 
    514 	ifp->if_collisions += EPE_READ(TXCollCnt);
    515 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
    516 	misses = EPE_READ(RXMissCnt);
    517 	if (misses > 0)
    518 		printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
    519 
    520 	s = splnet();
    521 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    522 		epe_ifstart(ifp);
    523 	}
    524 	splx(s);
    525 
    526 	mii_tick(&sc->sc_mii);
    527 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
    528 }
    529 
    530 
    531 static int
    532 epe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    533 {
    534 	int s, error;
    535 
    536 	s = splnet();
    537 	error = ether_ioctl(ifp, cmd, data);
    538 	if (error == ENETRESET) {
    539 		if (ifp->if_flags & IFF_RUNNING)
    540 			epe_setaddr(ifp);
    541 		error = 0;
    542 	}
    543 	splx(s);
    544 	return error;
    545 }
    546 
    547 static void
    548 epe_ifstart(struct ifnet *ifp)
    549 {
    550 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
    551 	struct mbuf *m;
    552 	bus_dma_segment_t *segs;
    553 	int s, bi, err, nsegs, ndq;
    554 
    555 	s = splnet();
    556 start:
    557 	ndq = 0;
    558 	if (sc->TXDQ_avail == 0) {
    559 		if (epe_gctx(sc) == 0) {
    560 			/* Enable End-Of-TX-Chain interrupt */
    561 			EPE_WRITE(IntEn, IntEn_REOFIE|IntEn_ECIE);
    562 			ifp->if_flags |= IFF_OACTIVE;
    563 			ifp->if_timer = 10;
    564 			splx(s);
    565 			return;
    566 		}
    567 	}
    568 
    569 	bi = sc->TXDQ_cur - sc->TXDQ;
    570 
    571 	IFQ_POLL(&ifp->if_snd, m);
    572 	if (m == NULL) {
    573 		splx(s);
    574 		return;
    575 	}
    576 more:
    577 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    578 		BUS_DMA_NOWAIT)) ||
    579 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
    580 		sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) {
    581 		/* Copy entire mbuf chain to new and 32-bit aligned storage */
    582 		struct mbuf *mn;
    583 
    584 		if (err == 0)
    585 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
    586 
    587 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
    588 		if (mn == NULL) goto stop;
    589 		if (m->m_pkthdr.len > (MHLEN & (~0x3))) {
    590 			MCLGET(mn, M_DONTWAIT);
    591 			if ((mn->m_flags & M_EXT) == 0) {
    592 				m_freem(mn);
    593 				goto stop;
    594 			}
    595 		}
    596 		mn->m_data = (void *)(((uint32_t)mn->m_data + 0x3) & (~0x3));
    597 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
    598 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
    599 		IFQ_DEQUEUE(&ifp->if_snd, m);
    600 		m_freem(m);
    601 		m = mn;
    602 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    603 			BUS_DMA_NOWAIT);
    604 	} else {
    605 		IFQ_DEQUEUE(&ifp->if_snd, m);
    606 	}
    607 
    608 	bpf_mtap(ifp, m);
    609 
    610 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
    611 	segs = sc->txq[bi].m_dmamap->dm_segs;
    612 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
    613 		sc->txq[bi].m_dmamap->dm_mapsize,
    614 		BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    615 
    616 	/* XXX: This driver hasn't been tested w/nsegs > 1 */
    617 	while (nsegs > 0) {
    618 		nsegs--;
    619 		sc->txq[bi].m = m;
    620 		sc->TXDQ[bi * 2] = segs->ds_addr;
    621 		if (nsegs == 0)
    622 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) |
    623 				(1 << 31);
    624 		else
    625 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16);
    626 		segs++;
    627 		bi = (bi + 1) % TX_QLEN;
    628 		ndq++;
    629 	}
    630 
    631 
    632 	/*
    633 	 * Enqueue another.  Don't do more than half the available
    634 	 * descriptors before telling the MAC about them
    635 	 */
    636 	if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) {
    637 		IFQ_POLL(&ifp->if_snd, m);
    638 		if (m != NULL) {
    639 			goto more;
    640 		}
    641 	}
    642 stop:
    643 	if (ndq > 0) {
    644 		sc->TXDQ_avail -= ndq;
    645 		sc->TXDQ_cur = &sc->TXDQ[bi];
    646 		CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(uint32_t),
    647 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    648 		EPE_WRITE(TXDEnq, ndq);
    649 	}
    650 
    651 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    652 		goto start;
    653 
    654 	splx(s);
    655 	return;
    656 }
    657 
    658 static void
    659 epe_ifwatchdog(struct ifnet *ifp)
    660 {
    661 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
    662 
    663 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    664 		return;
    665        	printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n",
    666 		device_xname(sc->sc_dev), EPE_READ(BMCtl), EPE_READ(BMSts));
    667 }
    668 
    669 static int
    670 epe_ifinit(struct ifnet *ifp)
    671 {
    672 	struct epe_softc *sc = ifp->if_softc;
    673 	int rc, s = splnet();
    674 
    675 	callout_stop(&sc->epe_tick_ch);
    676 	EPE_WRITE(RXCtl, RXCtl_IA0|RXCtl_BA|RXCtl_RCRCA|RXCtl_SRxON);
    677 	EPE_WRITE(TXCtl, TXCtl_STxON);
    678 	EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */
    679 
    680 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
    681 		rc = 0;
    682 	else if (rc != 0)
    683 		goto out;
    684 
    685 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
    686         ifp->if_flags |= IFF_RUNNING;
    687 out:
    688 	splx(s);
    689 	return 0;
    690 }
    691 
    692 static void
    693 epe_ifstop(struct ifnet *ifp, int disable)
    694 {
    695 	struct epe_softc *sc = ifp->if_softc;
    696 
    697 
    698 	EPE_WRITE(RXCtl, 0);
    699 	EPE_WRITE(TXCtl, 0);
    700 	EPE_WRITE(GIIntMsk, 0);
    701 	callout_stop(&sc->epe_tick_ch);
    702 
    703 	/* Down the MII. */
    704 	mii_down(&sc->sc_mii);
    705 
    706 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    707 	ifp->if_timer = 0;
    708 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
    709 }
    710 
    711 static void
    712 epe_setaddr(struct ifnet *ifp)
    713 {
    714 	struct epe_softc *sc = ifp->if_softc;
    715 	struct ethercom *ac = &sc->sc_ec;
    716 	struct ether_multi *enm;
    717 	struct ether_multistep step;
    718 	uint8_t ias[2][ETHER_ADDR_LEN];
    719 	uint32_t h, nma = 0, hashes[2] = { 0, 0 };
    720 	uint32_t rxctl = EPE_READ(RXCtl);
    721 
    722 	/* disable receiver temporarily */
    723 	EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON);
    724 
    725 	rxctl &= ~(RXCtl_MA|RXCtl_PA|RXCtl_IA2|RXCtl_IA3);
    726 
    727 	if (ifp->if_flags & IFF_PROMISC) {
    728 		rxctl |= RXCtl_PA;
    729 	}
    730 
    731 	ifp->if_flags &= ~IFF_ALLMULTI;
    732 
    733 	ETHER_FIRST_MULTI(step, ac, enm);
    734 	while (enm != NULL) {
    735 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    736 			/*
    737 			 * We must listen to a range of multicast addresses.
    738 			 * For now, just accept all multicasts, rather than
    739 			 * trying to set only those filter bits needed to match
    740 			 * the range.  (At this time, the only use of address
    741 			 * ranges is for IP multicast routing, for which the
    742 			 * range is big enough to require all bits set.)
    743 			 */
    744 			rxctl &= ~(RXCtl_IA2|RXCtl_IA3);
    745 			rxctl |= RXCtl_MA;
    746 			hashes[0] = 0xffffffffUL;
    747 			hashes[1] = 0xffffffffUL;
    748 			ifp->if_flags |= IFF_ALLMULTI;
    749 			break;
    750 		}
    751 
    752 		if (nma < 2) {
    753 			/* We can program 2 perfect address filters for mcast */
    754 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
    755 			rxctl |= (1 << (nma + 2));
    756 		} else {
    757 			/*
    758 			 * XXX: Datasheet is not very clear here, I'm not sure
    759 			 * if I'm doing this right.  --joff
    760 			 */
    761 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    762 
    763 			/* Just want the 6 most-significant bits. */
    764 			h = h >> 26;
    765 
    766 			hashes[ h / 32 ] |=  (1 << (h % 32));
    767 			rxctl |= RXCtl_MA;
    768 		}
    769 		ETHER_NEXT_MULTI(step, enm);
    770 		nma++;
    771 	}
    772 
    773 	EPE_WRITE(AFP, 0);
    774 	bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    775 		sc->sc_enaddr, ETHER_ADDR_LEN);
    776 	if (rxctl & RXCtl_IA2) {
    777 		EPE_WRITE(AFP, 2);
    778 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    779 			ias[0], ETHER_ADDR_LEN);
    780 	}
    781 	if (rxctl & RXCtl_IA3) {
    782 		EPE_WRITE(AFP, 3);
    783 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    784 			ias[1], ETHER_ADDR_LEN);
    785 	}
    786 	if (hashes[0] != 0 && hashes[1] != 0) {
    787 		EPE_WRITE(AFP, 7);
    788 		EPE_WRITE(HashTbl, hashes[0]);
    789 		EPE_WRITE(HashTbl + 4, hashes[1]);
    790 	}
    791 	EPE_WRITE(RXCtl, rxctl);
    792 }
    793