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