Home | History | Annotate | Line # | Download | only in at91
at91emac.c revision 1.23
      1 /*	$Id: at91emac.c,v 1.23 2019/01/22 03:42:24 msaitoh Exp $	*/
      2 /*	$NetBSD: at91emac.c,v 1.23 2019/01/22 03:42:24 msaitoh Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2007 Embedtronics Oy
      6  * All rights reserved.
      7  *
      8  * Based on arch/arm/ep93xx/epe.c
      9  *
     10  * Copyright (c) 2004 Jesse Off
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: at91emac.c,v 1.23 2019/01/22 03:42:24 msaitoh Exp $");
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/kernel.h>
     43 #include <sys/proc.h>
     44 #include <sys/malloc.h>
     45 #include <sys/time.h>
     46 #include <sys/device.h>
     47 #include <uvm/uvm_extern.h>
     48 
     49 #include <sys/bus.h>
     50 #include <machine/intr.h>
     51 
     52 #include <arm/cpufunc.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_types.h>
     57 #include <net/if_media.h>
     58 #include <net/if_ether.h>
     59 #include <net/bpf.h>
     60 
     61 #include <dev/mii/mii.h>
     62 #include <dev/mii/miivar.h>
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/if_inarp.h>
     70 #endif
     71 
     72 #include <arm/at91/at91var.h>
     73 #include <arm/at91/at91emacreg.h>
     74 #include <arm/at91/at91emacvar.h>
     75 
     76 #define DEFAULT_MDCDIV	32
     77 
     78 #ifndef EMAC_FAST
     79 #define EMAC_FAST
     80 #endif
     81 
     82 #ifndef EMAC_FAST
     83 #define EMAC_READ(x) \
     84 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
     85 #define EMAC_WRITE(x, y) \
     86 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
     87 #else
     88 #define EMAC_READ(x) ETHREG(x)
     89 #define EMAC_WRITE(x, y) ETHREG(x) = (y)
     90 #endif /* ! EMAC_FAST */
     91 
     92 static int	emac_match(device_t, cfdata_t, void *);
     93 static void	emac_attach(device_t, device_t, void *);
     94 static void	emac_init(struct emac_softc *);
     95 static int      emac_intr(void* arg);
     96 static int	emac_gctx(struct emac_softc *);
     97 static int	emac_mediachange(struct ifnet *);
     98 static void	emac_mediastatus(struct ifnet *, struct ifmediareq *);
     99 int		emac_mii_readreg (device_t, int, int, uint16_t *);
    100 int		emac_mii_writereg (device_t, int, int, uint16_t);
    101 void		emac_statchg (struct ifnet *);
    102 void		emac_tick (void *);
    103 static int	emac_ifioctl (struct ifnet *, u_long, void *);
    104 static void	emac_ifstart (struct ifnet *);
    105 static void	emac_ifwatchdog (struct ifnet *);
    106 static int	emac_ifinit (struct ifnet *);
    107 static void	emac_ifstop (struct ifnet *, int);
    108 static void	emac_setaddr (struct ifnet *);
    109 
    110 CFATTACH_DECL_NEW(at91emac, sizeof(struct emac_softc),
    111     emac_match, emac_attach, NULL, NULL);
    112 
    113 #ifdef	EMAC_DEBUG
    114 int emac_debug = EMAC_DEBUG;
    115 #define	DPRINTFN(n,fmt)	if (emac_debug >= (n)) printf fmt
    116 #else
    117 #define	DPRINTFN(n,fmt)
    118 #endif
    119 
    120 static int
    121 emac_match(device_t parent, cfdata_t match, void *aux)
    122 {
    123 	if (strcmp(match->cf_name, "at91emac") == 0)
    124 		return 2;
    125 	return 0;
    126 }
    127 
    128 static void
    129 emac_attach(device_t parent, device_t self, void *aux)
    130 {
    131 	struct emac_softc		*sc = device_private(self);
    132 	struct at91bus_attach_args	*sa = aux;
    133 	prop_data_t			enaddr;
    134 	uint32_t			u;
    135 
    136 	printf("\n");
    137 	sc->sc_dev = self;
    138 	sc->sc_iot = sa->sa_iot;
    139 	sc->sc_pid = sa->sa_pid;
    140 	sc->sc_dmat = sa->sa_dmat;
    141 
    142 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
    143 		panic("%s: Cannot map registers", device_xname(self));
    144 
    145 	/* enable peripheral clock */
    146 	at91_peripheral_clock(sc->sc_pid, 1);
    147 
    148 	/* configure emac: */
    149 	EMAC_WRITE(ETH_CTL, 0);			// disable everything
    150 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    151 	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
    152 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    153 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
    154 	//(void)EMAC_READ(ETH_ISR);
    155 	u = EMAC_READ(ETH_TSR);
    156 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    157 				  | ETH_TSR_IDLE | ETH_TSR_RLE
    158 				  | ETH_TSR_COL|ETH_TSR_OVR)));
    159 	u = EMAC_READ(ETH_RSR);
    160 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
    161 
    162 	/* Fetch the Ethernet address from property if set. */
    163 	enaddr = prop_dictionary_get(device_properties(self), "mac-address");
    164 
    165 	if (enaddr != NULL) {
    166 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
    167 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
    168 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
    169 		       ETHER_ADDR_LEN);
    170 	} else {
    171 		static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
    172 		  0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
    173 		};
    174 		memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
    175 	}
    176 
    177         at91_intr_establish(sc->sc_pid, IPL_NET, INTR_HIGH_LEVEL, emac_intr, sc);
    178 	emac_init(sc);
    179 }
    180 
    181 static int
    182 emac_gctx(struct emac_softc *sc)
    183 {
    184 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    185 	uint32_t tsr;
    186 
    187 	tsr = EMAC_READ(ETH_TSR);
    188 	if (!(tsr & ETH_TSR_BNQ)) {
    189 		// no space left
    190 		return 0;
    191 	}
    192 
    193 	// free sent frames
    194 	while (sc->txqc > (tsr & ETH_TSR_IDLE ? 0 : 1)) {
    195 		int i = sc->txqi % TX_QLEN;
    196 		bus_dmamap_sync(sc->sc_dmat, sc->txq[i].m_dmamap, 0,
    197 				sc->txq[i].m->m_pkthdr.len, BUS_DMASYNC_POSTWRITE);
    198 		bus_dmamap_unload(sc->sc_dmat, sc->txq[i].m_dmamap);
    199 		m_freem(sc->txq[i].m);
    200 		DPRINTFN(2,("%s: freed idx #%i mbuf %p (txqc=%i)\n", __FUNCTION__, i, sc->txq[i].m, sc->txqc));
    201 		sc->txq[i].m = NULL;
    202 		sc->txqi = (i + 1) % TX_QLEN;
    203 		sc->txqc--;
    204 	}
    205 
    206 	// mark we're free
    207 	if (ifp->if_flags & IFF_OACTIVE) {
    208 		ifp->if_flags &= ~IFF_OACTIVE;
    209 		/* Disable transmit-buffer-free interrupt */
    210 		/*EMAC_WRITE(ETH_IDR, ETH_ISR_TBRE);*/
    211 	}
    212 
    213 	return 1;
    214 }
    215 
    216 static int
    217 emac_intr(void *arg)
    218 {
    219 	struct emac_softc *sc = (struct emac_softc *)arg;
    220 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    221 	uint32_t imr, isr, ctl;
    222 	int bi;
    223 
    224 	imr = ~EMAC_READ(ETH_IMR);
    225 	if (!(imr & (ETH_ISR_RCOM|ETH_ISR_TBRE|ETH_ISR_TIDLE|ETH_ISR_RBNA|ETH_ISR_ROVR))) {
    226 		// interrupt not enabled, can't be us
    227 		return 0;
    228 	}
    229 
    230 	isr = EMAC_READ(ETH_ISR) & imr;
    231 #ifdef EMAC_DEBUG
    232 	uint32_t rsr =
    233 #endif
    234 	EMAC_READ(ETH_RSR);		// get receive status register
    235 
    236 	DPRINTFN(2, ("%s: isr=0x%08X rsr=0x%08X imr=0x%08X\n", __FUNCTION__, isr, rsr, imr));
    237 
    238 	if (isr & ETH_ISR_RBNA) {		// out of receive buffers
    239 		EMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear interrupt
    240 		ctl = EMAC_READ(ETH_CTL);		// get current control register value
    241 		EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);	// disable receiver
    242 		EMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear BNA bit
    243 		EMAC_WRITE(ETH_CTL, ctl |  ETH_CTL_RE);	// re-enable receiver
    244 		ifp->if_ierrors++;
    245 		ifp->if_ipackets++;
    246 		DPRINTFN(1,("%s: out of receive buffers\n", __FUNCTION__));
    247 	}
    248 	if (isr & ETH_ISR_ROVR) {
    249 		EMAC_WRITE(ETH_RSR, ETH_RSR_OVR);	// clear interrupt
    250 		ifp->if_ierrors++;
    251 		ifp->if_ipackets++;
    252 		DPRINTFN(1,("%s: receive overrun\n", __FUNCTION__));
    253 	}
    254 
    255 	if (isr & ETH_ISR_RCOM) {			// packet has been received!
    256 		uint32_t nfo;
    257 		// @@@ if memory is NOT coherent, then we're in trouble @@@@
    258 //		bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    259 //		printf("## RDSC[%i].ADDR=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Addr);
    260 		DPRINTFN(2,("#2 RDSC[%i].INFO=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Info));
    261 		while (sc->RDSC[(bi = sc->rxqi % RX_QLEN)].Addr & ETH_RDSC_F_USED) {
    262 			int fl;
    263 			struct mbuf *m;
    264 
    265 			nfo = sc->RDSC[bi].Info;
    266 		  	fl = (nfo & ETH_RDSC_I_LEN) - 4;
    267 			DPRINTFN(2,("## nfo=0x%08X\n", nfo));
    268 
    269 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    270 			if (m != NULL) MCLGET(m, M_DONTWAIT);
    271 			if (m != NULL && (m->m_flags & M_EXT)) {
    272 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
    273 						MCLBYTES, BUS_DMASYNC_POSTREAD);
    274 				bus_dmamap_unload(sc->sc_dmat,
    275 					sc->rxq[bi].m_dmamap);
    276 				m_set_rcvif(sc->rxq[bi].m, ifp);
    277 				sc->rxq[bi].m->m_pkthdr.len =
    278 					sc->rxq[bi].m->m_len = fl;
    279 				DPRINTFN(2,("received %u bytes packet\n", fl));
    280 				if_percpuq_enqueue(ifp->if_percpuq, sc->rxq[bi].m);
    281 				if (mtod(m, intptr_t) & 3) {
    282 					m_adj(m, mtod(m, intptr_t) & 3);
    283 				}
    284 				sc->rxq[bi].m = m;
    285 				bus_dmamap_load(sc->sc_dmat,
    286 					sc->rxq[bi].m_dmamap,
    287 					m->m_ext.ext_buf, MCLBYTES,
    288 					NULL, BUS_DMA_NOWAIT);
    289 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
    290 						MCLBYTES, BUS_DMASYNC_PREREAD);
    291 				sc->RDSC[bi].Info = 0;
    292 				sc->RDSC[bi].Addr =
    293 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr
    294 					| (bi == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
    295 			} else {
    296 				/* Drop packets until we can get replacement
    297 				 * empty mbufs for the RXDQ.
    298 				 */
    299 				if (m != NULL) {
    300 					m_freem(m);
    301 				}
    302 				ifp->if_ierrors++;
    303 			}
    304 			sc->rxqi++;
    305 		}
    306 //		bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    307 	}
    308 
    309 	if (emac_gctx(sc) > 0)
    310 		if_schedule_deferred_start(ifp);
    311 #if 0 // reloop
    312 	irq = EMAC_READ(IntStsC);
    313 	if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
    314 		goto begin;
    315 #endif
    316 
    317 	return (1);
    318 }
    319 
    320 
    321 static void
    322 emac_init(struct emac_softc *sc)
    323 {
    324 	bus_dma_segment_t segs;
    325 	void *addr;
    326 	int rsegs, err, i;
    327 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    328 	uint32_t u;
    329 #if 0
    330 	int mdcdiv = DEFAULT_MDCDIV;
    331 #endif
    332 
    333 	callout_init(&sc->emac_tick_ch, 0);
    334 
    335 	// ok...
    336 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
    337 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    338 	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
    339 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    340 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
    341 //	(void)EMAC_READ(ETH_ISR);
    342 	u = EMAC_READ(ETH_TSR);
    343 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    344 				  | ETH_TSR_IDLE | ETH_TSR_RLE
    345 				  | ETH_TSR_COL|ETH_TSR_OVR)));
    346 	u = EMAC_READ(ETH_RSR);
    347 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
    348 
    349 	/* configure EMAC */
    350 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    351 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);
    352 #if 0
    353 	if (device_cfdata(sc->sc_dev)->cf_flags)
    354 		mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
    355 #endif
    356 	/* set ethernet address */
    357 	EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
    358 		   | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
    359 		   | (sc->sc_enaddr[0]));
    360 	EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
    361 		   | (sc->sc_enaddr[4]));
    362 	EMAC_WRITE(ETH_SA2L, 0);
    363 	EMAC_WRITE(ETH_SA2H, 0);
    364 	EMAC_WRITE(ETH_SA3L, 0);
    365 	EMAC_WRITE(ETH_SA3H, 0);
    366 	EMAC_WRITE(ETH_SA4L, 0);
    367 	EMAC_WRITE(ETH_SA4H, 0);
    368 
    369 	/* Allocate a page of memory for receive queue descriptors */
    370 	sc->rbqlen = (ETH_RDSC_SIZE * (RX_QLEN + 1) * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
    371 	sc->rbqlen *= PAGE_SIZE;
    372 	DPRINTFN(1,("%s: rbqlen=%i\n", __FUNCTION__, sc->rbqlen));
    373 
    374 	err = bus_dmamem_alloc(sc->sc_dmat, sc->rbqlen, 0,
    375 		MAX(16384, PAGE_SIZE),	// see EMAC errata why forced to 16384 byte boundary
    376 		&segs, 1, &rsegs, BUS_DMA_WAITOK);
    377 	if (err == 0) {
    378 		DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
    379 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->rbqlen,
    380 			&sc->rbqpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
    381 	}
    382 	if (err == 0) {
    383 		DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
    384 		err = bus_dmamap_create(sc->sc_dmat, sc->rbqlen, 1,
    385 			sc->rbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
    386 			&sc->rbqpage_dmamap);
    387 	}
    388 	if (err == 0) {
    389 		DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
    390 		err = bus_dmamap_load(sc->sc_dmat, sc->rbqpage_dmamap,
    391 			sc->rbqpage, sc->rbqlen, NULL, BUS_DMA_WAITOK);
    392 	}
    393 	if (err != 0) {
    394 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
    395 	}
    396 	sc->rbqpage_dsaddr = sc->rbqpage_dmamap->dm_segs[0].ds_addr;
    397 
    398 	memset(sc->rbqpage, 0, sc->rbqlen);
    399 
    400 	/* Set up pointers to start of each queue in kernel addr space.
    401 	 * Each descriptor queue or status queue entry uses 2 words
    402 	 */
    403 	sc->RDSC = (void*)sc->rbqpage;
    404 
    405 	/* Populate the RXQ with mbufs */
    406 	sc->rxqi = 0;
    407 	for(i = 0; i < RX_QLEN; i++) {
    408 		struct mbuf *m;
    409 
    410 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, PAGE_SIZE,
    411 			BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
    412 		if (err) {
    413 			panic("%s: dmamap_create failed: %i\n", __FUNCTION__, err);
    414 		}
    415 		MGETHDR(m, M_WAIT, MT_DATA);
    416 		MCLGET(m, M_WAIT);
    417 		sc->rxq[i].m = m;
    418 		if (mtod(m, intptr_t) & 3) {
    419 			m_adj(m, mtod(m, intptr_t) & 3);
    420 		}
    421 		err = bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
    422 			m->m_ext.ext_buf, MCLBYTES, NULL,
    423 			BUS_DMA_WAITOK);
    424 		if (err) {
    425 			panic("%s: dmamap_load failed: %i\n", __FUNCTION__, err);
    426 		}
    427 		sc->RDSC[i].Addr = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr
    428 			| (i == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
    429 		sc->RDSC[i].Info = 0;
    430 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
    431 			MCLBYTES, BUS_DMASYNC_PREREAD);
    432 	}
    433 
    434 	/* prepare transmit queue */
    435 	for (i = 0; i < TX_QLEN; i++) {
    436 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
    437 					(BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
    438 					&sc->txq[i].m_dmamap);
    439 		if (err)
    440 			panic("ARGH #1");
    441 		sc->txq[i].m = NULL;
    442 	}
    443 
    444 	/* Program each queue's start addr, cur addr, and len registers
    445 	 * with the physical addresses.
    446 	 */
    447 	bus_dmamap_sync(sc->sc_dmat, sc->rbqpage_dmamap, 0, sc->rbqlen,
    448 			 BUS_DMASYNC_PREREAD);
    449 	addr = (void *)sc->rbqpage_dmamap->dm_segs[0].ds_addr;
    450 	EMAC_WRITE(ETH_RBQP, (uint32_t)addr);
    451 
    452 	/* Divide HCLK by 32 for MDC clock */
    453 	sc->sc_mii.mii_ifp = ifp;
    454 	sc->sc_mii.mii_readreg = emac_mii_readreg;
    455 	sc->sc_mii.mii_writereg = emac_mii_writereg;
    456 	sc->sc_mii.mii_statchg = emac_statchg;
    457 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, emac_mediachange,
    458 		emac_mediastatus);
    459 	mii_attach((device_t )sc, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    460 		MII_OFFSET_ANY, 0);
    461 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    462 
    463 	// enable / disable interrupts
    464 
    465 #if 0
    466 	// enable / disable interrupts
    467 	EMAC_WRITE(ETH_IDR, -1);
    468 	EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
    469 		   | ETH_ISR_RBNA | ETH_ISR_ROVR);
    470 //	(void)EMAC_READ(ETH_ISR); // why
    471 
    472 	// enable transmitter / receiver
    473 	EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
    474 		   | ETH_CTL_CSR | ETH_CTL_MPE);
    475 #endif
    476 	/*
    477 	 * We can support 802.1Q VLAN-sized frames.
    478 	 */
    479 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    480 
    481         strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    482         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
    483         ifp->if_ioctl = emac_ifioctl;
    484         ifp->if_start = emac_ifstart;
    485         ifp->if_watchdog = emac_ifwatchdog;
    486         ifp->if_init = emac_ifinit;
    487         ifp->if_stop = emac_ifstop;
    488         ifp->if_timer = 0;
    489 	ifp->if_softc = sc;
    490         IFQ_SET_READY(&ifp->if_snd);
    491         if_attach(ifp);
    492 	if_deferred_start_init(ifp, NULL);
    493         ether_ifattach(ifp, (sc)->sc_enaddr);
    494 }
    495 
    496 static int
    497 emac_mediachange(struct ifnet *ifp)
    498 {
    499 	if (ifp->if_flags & IFF_UP)
    500 		emac_ifinit(ifp);
    501 	return (0);
    502 }
    503 
    504 static void
    505 emac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    506 {
    507 	struct emac_softc *sc = ifp->if_softc;
    508 
    509 	mii_pollstat(&sc->sc_mii);
    510 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    511 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    512 }
    513 
    514 
    515 int
    516 emac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
    517 {
    518 #ifndef EMAC_FAST
    519 	struct emac_softc *sc = device_private(self);
    520 #endif
    521 
    522 	EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_RD
    523 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
    524 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
    525 			     | ETH_MAN_CODE_IEEE802_3));
    526 	while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
    527 	*val = EMAC_READ(ETH_MAN) & ETH_MAN_DATA;
    528 
    529 	return 0;
    530 }
    531 
    532 int
    533 emac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
    534 {
    535 #ifndef EMAC_FAST
    536 	struct emac_softc *sc = device_private(self);
    537 #endif
    538 
    539 	EMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_WR
    540 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
    541 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
    542 			     | ETH_MAN_CODE_IEEE802_3
    543 			     | (val & ETH_MAN_DATA)));
    544 	while (!(EMAC_READ(ETH_SR) & ETH_SR_IDLE)) ;
    545 
    546 	return 0;
    547 }
    548 
    549 
    550 void
    551 emac_statchg(struct ifnet *ifp)
    552 {
    553         struct emac_softc *sc = ifp->if_softc;
    554         uint32_t reg;
    555 
    556         /*
    557          * We must keep the MAC and the PHY in sync as
    558          * to the status of full-duplex!
    559          */
    560 	reg = EMAC_READ(ETH_CFG);
    561         if (sc->sc_mii.mii_media_active & IFM_FDX)
    562                 reg |= ETH_CFG_FD;
    563         else
    564                 reg &= ~ETH_CFG_FD;
    565 	EMAC_WRITE(ETH_CFG, reg);
    566 }
    567 
    568 void
    569 emac_tick(void *arg)
    570 {
    571 	struct emac_softc* sc = (struct emac_softc *)arg;
    572 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    573 	int s;
    574 	uint32_t misses;
    575 
    576 	ifp->if_collisions += EMAC_READ(ETH_SCOL) + EMAC_READ(ETH_MCOL);
    577 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
    578 	misses = EMAC_READ(ETH_DRFC);
    579 	if (misses > 0)
    580 		printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
    581 
    582 	s = splnet();
    583 	if (emac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    584 		emac_ifstart(ifp);
    585 	}
    586 	splx(s);
    587 
    588 	mii_tick(&sc->sc_mii);
    589 	callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
    590 }
    591 
    592 
    593 static int
    594 emac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    595 {
    596 	struct emac_softc *sc = ifp->if_softc;
    597 	struct ifreq *ifr = (struct ifreq *)data;
    598 	int s, error;
    599 
    600 	s = splnet();
    601 	switch(cmd) {
    602 	case SIOCSIFMEDIA:
    603 	case SIOCGIFMEDIA:
    604 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    605 		break;
    606 	default:
    607 		error = ether_ioctl(ifp, cmd, data);
    608 		if (error == ENETRESET) {
    609 			if (ifp->if_flags & IFF_RUNNING)
    610 				emac_setaddr(ifp);
    611 			error = 0;
    612 		}
    613 	}
    614 	splx(s);
    615 	return error;
    616 }
    617 
    618 static void
    619 emac_ifstart(struct ifnet *ifp)
    620 {
    621 	struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
    622 	struct mbuf *m;
    623 	bus_dma_segment_t *segs;
    624 	int s, bi, err, nsegs;
    625 
    626 	s = splnet();
    627 start:
    628 	if (emac_gctx(sc) == 0) {
    629 		/* Enable transmit-buffer-free interrupt */
    630 		EMAC_WRITE(ETH_IER, ETH_ISR_TBRE);
    631 		ifp->if_flags |= IFF_OACTIVE;
    632 		ifp->if_timer = 10;
    633 		splx(s);
    634 		return;
    635 	}
    636 
    637 	ifp->if_timer = 0;
    638 
    639 	IFQ_POLL(&ifp->if_snd, m);
    640 	if (m == NULL) {
    641 		splx(s);
    642 		return;
    643 	}
    644 //more:
    645 	bi = (sc->txqi + sc->txqc) % TX_QLEN;
    646 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    647 		BUS_DMA_NOWAIT)) ||
    648 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
    649 		sc->txq[bi].m_dmamap->dm_nsegs > 1) {
    650 		/* Copy entire mbuf chain to new single */
    651 		struct mbuf *mn;
    652 
    653 		if (err == 0)
    654 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
    655 
    656 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
    657 		if (mn == NULL) goto stop;
    658 		if (m->m_pkthdr.len > MHLEN) {
    659 			MCLGET(mn, M_DONTWAIT);
    660 			if ((mn->m_flags & M_EXT) == 0) {
    661 				m_freem(mn);
    662 				goto stop;
    663 			}
    664 		}
    665 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
    666 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
    667 		IFQ_DEQUEUE(&ifp->if_snd, m);
    668 		m_freem(m);
    669 		m = mn;
    670 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    671 			BUS_DMA_NOWAIT);
    672 	} else {
    673 		IFQ_DEQUEUE(&ifp->if_snd, m);
    674 	}
    675 
    676 	bpf_mtap(ifp, m, BPF_D_OUT);
    677 
    678 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
    679 	segs = sc->txq[bi].m_dmamap->dm_segs;
    680 	if (nsegs > 1) {
    681 		panic("#### ARGH #2");
    682 	}
    683 
    684 	sc->txq[bi].m = m;
    685 	sc->txqc++;
    686 
    687 	DPRINTFN(2,("%s: start sending idx #%i mbuf %p (txqc=%i, phys %p), len=%u\n", __FUNCTION__, bi, sc->txq[bi].m, sc->txqc, (void*)segs->ds_addr,
    688 		       (unsigned)m->m_pkthdr.len));
    689 #ifdef	DIAGNOSTIC
    690 	if (sc->txqc > TX_QLEN) {
    691 		panic("%s: txqc %i > %i", __FUNCTION__, sc->txqc, TX_QLEN);
    692 	}
    693 #endif
    694 
    695 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
    696 		sc->txq[bi].m_dmamap->dm_mapsize,
    697 		BUS_DMASYNC_PREWRITE);
    698 
    699 	EMAC_WRITE(ETH_TAR, segs->ds_addr);
    700 	EMAC_WRITE(ETH_TCR, m->m_pkthdr.len);
    701 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    702 		goto start;
    703 stop:
    704 
    705 	splx(s);
    706 	return;
    707 }
    708 
    709 static void
    710 emac_ifwatchdog(struct ifnet *ifp)
    711 {
    712 	struct emac_softc *sc = (struct emac_softc *)ifp->if_softc;
    713 
    714 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    715 		return;
    716        	printf("%s: device timeout, CTL = 0x%08x, CFG = 0x%08x\n",
    717 		device_xname(sc->sc_dev), EMAC_READ(ETH_CTL), EMAC_READ(ETH_CFG));
    718 }
    719 
    720 static int
    721 emac_ifinit(struct ifnet *ifp)
    722 {
    723 	struct emac_softc *sc = ifp->if_softc;
    724 	int s = splnet();
    725 
    726 	callout_stop(&sc->emac_tick_ch);
    727 
    728 	// enable interrupts
    729 	EMAC_WRITE(ETH_IDR, -1);
    730 	EMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
    731 		   | ETH_ISR_RBNA | ETH_ISR_ROVR);
    732 
    733 	// enable transmitter / receiver
    734 	EMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
    735 		   | ETH_CTL_CSR | ETH_CTL_MPE);
    736 
    737 	mii_mediachg(&sc->sc_mii);
    738 	callout_reset(&sc->emac_tick_ch, hz, emac_tick, sc);
    739         ifp->if_flags |= IFF_RUNNING;
    740 	splx(s);
    741 	return 0;
    742 }
    743 
    744 static void
    745 emac_ifstop(struct ifnet *ifp, int disable)
    746 {
    747 //	uint32_t u;
    748 	struct emac_softc *sc = ifp->if_softc;
    749 
    750 #if 0
    751 	EMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
    752 	EMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    753 //	EMAC_WRITE(ETH_RBQP, 0);		// clear receive
    754 	EMAC_WRITE(ETH_CFG, ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    755 	EMAC_WRITE(ETH_TCR, 0);			// send nothing
    756 //	(void)EMAC_READ(ETH_ISR);
    757 	u = EMAC_READ(ETH_TSR);
    758 	EMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    759 				  | ETH_TSR_IDLE | ETH_TSR_RLE
    760 				  | ETH_TSR_COL|ETH_TSR_OVR)));
    761 	u = EMAC_READ(ETH_RSR);
    762 	EMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR|ETH_RSR_REC|ETH_RSR_BNA)));
    763 #endif
    764 	callout_stop(&sc->emac_tick_ch);
    765 
    766 	/* Down the MII. */
    767 	mii_down(&sc->sc_mii);
    768 
    769 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    770 	ifp->if_timer = 0;
    771 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
    772 }
    773 
    774 static void
    775 emac_setaddr(struct ifnet *ifp)
    776 {
    777 	struct emac_softc *sc = ifp->if_softc;
    778 	struct ethercom *ac = &sc->sc_ec;
    779 	struct ether_multi *enm;
    780 	struct ether_multistep step;
    781 	uint8_t ias[3][ETHER_ADDR_LEN];
    782 	uint32_t h, nma = 0, hashes[2] = { 0, 0 };
    783 	uint32_t ctl = EMAC_READ(ETH_CTL);
    784 	uint32_t cfg = EMAC_READ(ETH_CFG);
    785 
    786 	/* disable receiver temporarily */
    787 	EMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
    788 
    789 	cfg &= ~(ETH_CFG_MTI | ETH_CFG_UNI | ETH_CFG_CAF | ETH_CFG_UNI);
    790 
    791 	if (ifp->if_flags & IFF_PROMISC) {
    792 		cfg |=  ETH_CFG_CAF;
    793 	} else {
    794 		cfg &= ~ETH_CFG_CAF;
    795 	}
    796 
    797 	// ETH_CFG_BIG?
    798 
    799 	ifp->if_flags &= ~IFF_ALLMULTI;
    800 
    801 	ETHER_FIRST_MULTI(step, ac, enm);
    802 	while (enm != NULL) {
    803 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    804 			/*
    805 			 * We must listen to a range of multicast addresses.
    806 			 * For now, just accept all multicasts, rather than
    807 			 * trying to set only those filter bits needed to match
    808 			 * the range.  (At this time, the only use of address
    809 			 * ranges is for IP multicast routing, for which the
    810 			 * range is big enough to require all bits set.)
    811 			 */
    812 			cfg |= ETH_CFG_CAF;
    813 			hashes[0] = 0xffffffffUL;
    814 			hashes[1] = 0xffffffffUL;
    815 			ifp->if_flags |= IFF_ALLMULTI;
    816 			nma = 0;
    817 			break;
    818 		}
    819 
    820 		if (nma < 3) {
    821 			/* We can program 3 perfect address filters for mcast */
    822 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
    823 		} else {
    824 			/*
    825 			 * XXX: Datasheet is not very clear here, I'm not sure
    826 			 * if I'm doing this right.  --joff
    827 			 */
    828 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    829 
    830 			/* Just want the 6 most-significant bits. */
    831 			h = h >> 26;
    832 
    833 			hashes[ h / 32 ] |=  (1 << (h % 32));
    834 			cfg |= ETH_CFG_MTI;
    835 		}
    836 		ETHER_NEXT_MULTI(step, enm);
    837 		nma++;
    838 	}
    839 
    840 	// program...
    841 	DPRINTFN(1,("%s: en0 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    842 		    sc->sc_enaddr[0], sc->sc_enaddr[1], sc->sc_enaddr[2],
    843 		    sc->sc_enaddr[3], sc->sc_enaddr[4], sc->sc_enaddr[5]));
    844 	EMAC_WRITE(ETH_SA1L, (sc->sc_enaddr[3] << 24)
    845 		   | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
    846 		   | (sc->sc_enaddr[0]));
    847 	EMAC_WRITE(ETH_SA1H, (sc->sc_enaddr[5] << 8)
    848 		   | (sc->sc_enaddr[4]));
    849 	if (nma > 1) {
    850 		DPRINTFN(1,("%s: en1 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    851 		       ias[0][0], ias[0][1], ias[0][2],
    852 		       ias[0][3], ias[0][4], ias[0][5]));
    853 		EMAC_WRITE(ETH_SA2L, (ias[0][3] << 24)
    854 			   | (ias[0][2] << 16) | (ias[0][1] << 8)
    855 			   | (ias[0][0]));
    856 		EMAC_WRITE(ETH_SA2H, (ias[0][4] << 8)
    857 			   | (ias[0][5]));
    858 	}
    859 	if (nma > 2) {
    860 		DPRINTFN(1,("%s: en2 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    861 		       ias[1][0], ias[1][1], ias[1][2],
    862 		       ias[1][3], ias[1][4], ias[1][5]));
    863 		EMAC_WRITE(ETH_SA3L, (ias[1][3] << 24)
    864 			   | (ias[1][2] << 16) | (ias[1][1] << 8)
    865 			   | (ias[1][0]));
    866 		EMAC_WRITE(ETH_SA3H, (ias[1][4] << 8)
    867 			   | (ias[1][5]));
    868 	}
    869 	if (nma > 3) {
    870 		DPRINTFN(1,("%s: en3 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    871 		       ias[2][0], ias[2][1], ias[2][2],
    872 		       ias[2][3], ias[2][4], ias[2][5]));
    873 		EMAC_WRITE(ETH_SA3L, (ias[2][3] << 24)
    874 			   | (ias[2][2] << 16) | (ias[2][1] << 8)
    875 			   | (ias[2][0]));
    876 		EMAC_WRITE(ETH_SA3H, (ias[2][4] << 8)
    877 			   | (ias[2][5]));
    878 	}
    879 	EMAC_WRITE(ETH_HSH, hashes[0]);
    880 	EMAC_WRITE(ETH_HSL, hashes[1]);
    881 	EMAC_WRITE(ETH_CFG, cfg);
    882 	EMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
    883 }
    884