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