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