Home | History | Annotate | Line # | Download | only in dev
if_aumac.c revision 1.43.14.2
      1  1.43.14.2  pgoyette /* $NetBSD: if_aumac.c,v 1.43.14.2 2019/01/26 22:00:03 pgoyette Exp $ */
      2        1.1    simonb 
      3        1.1    simonb /*
      4        1.1    simonb  * Copyright (c) 2001 Wasabi Systems, Inc.
      5        1.1    simonb  * All rights reserved.
      6        1.1    simonb  *
      7        1.1    simonb  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8        1.1    simonb  *
      9        1.1    simonb  * Redistribution and use in source and binary forms, with or without
     10        1.1    simonb  * modification, are permitted provided that the following conditions
     11        1.1    simonb  * are met:
     12        1.1    simonb  * 1. Redistributions of source code must retain the above copyright
     13        1.1    simonb  *    notice, this list of conditions and the following disclaimer.
     14        1.1    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1    simonb  *    notice, this list of conditions and the following disclaimer in the
     16        1.1    simonb  *    documentation and/or other materials provided with the distribution.
     17        1.1    simonb  * 3. All advertising materials mentioning features or use of this software
     18        1.1    simonb  *    must display the following acknowledgement:
     19        1.1    simonb  *	This product includes software developed for the NetBSD Project by
     20        1.1    simonb  *	Wasabi Systems, Inc.
     21        1.1    simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22        1.1    simonb  *    or promote products derived from this software without specific prior
     23        1.1    simonb  *    written permission.
     24        1.1    simonb  *
     25        1.1    simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26        1.1    simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27        1.1    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28        1.1    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29        1.1    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30        1.1    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31        1.1    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32        1.1    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33        1.1    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34        1.1    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35        1.1    simonb  * POSSIBILITY OF SUCH DAMAGE.
     36        1.1    simonb  */
     37        1.1    simonb 
     38        1.1    simonb /*
     39        1.1    simonb  * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media
     40        1.1    simonb  * Access Controller.
     41        1.1    simonb  *
     42        1.1    simonb  * TODO:
     43        1.1    simonb  *
     44        1.1    simonb  *	Better Rx buffer management; we want to get new Rx buffers
     45        1.1    simonb  *	to the chip more quickly than we currently do.
     46        1.1    simonb  */
     47        1.1    simonb 
     48        1.1    simonb #include <sys/cdefs.h>
     49  1.43.14.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: if_aumac.c,v 1.43.14.2 2019/01/26 22:00:03 pgoyette Exp $");
     50       1.34       tls 
     51        1.1    simonb 
     52        1.1    simonb 
     53        1.1    simonb #include <sys/param.h>
     54       1.31      matt #include <sys/bus.h>
     55        1.1    simonb #include <sys/callout.h>
     56       1.31      matt #include <sys/device.h>
     57       1.31      matt #include <sys/endian.h>
     58       1.31      matt #include <sys/errno.h>
     59       1.31      matt #include <sys/intr.h>
     60       1.31      matt #include <sys/ioctl.h>
     61       1.31      matt #include <sys/kernel.h>
     62        1.1    simonb #include <sys/mbuf.h>
     63        1.1    simonb #include <sys/malloc.h>
     64        1.1    simonb #include <sys/socket.h>
     65        1.1    simonb 
     66       1.29  uebayasi #include <uvm/uvm.h>		/* for PAGE_SIZE */
     67        1.1    simonb 
     68        1.1    simonb #include <net/if.h>
     69        1.1    simonb #include <net/if_dl.h>
     70        1.1    simonb #include <net/if_media.h>
     71        1.1    simonb #include <net/if_ether.h>
     72        1.1    simonb 
     73        1.1    simonb #include <net/bpf.h>
     74       1.39  riastrad #include <sys/rndsource.h>
     75        1.1    simonb 
     76        1.1    simonb #include <dev/mii/mii.h>
     77        1.1    simonb #include <dev/mii/miivar.h>
     78        1.1    simonb 
     79        1.1    simonb #include <mips/alchemy/include/aureg.h>
     80        1.1    simonb #include <mips/alchemy/include/auvar.h>
     81        1.1    simonb #include <mips/alchemy/include/aubusvar.h>
     82        1.1    simonb #include <mips/alchemy/dev/if_aumacreg.h>
     83        1.1    simonb 
     84        1.1    simonb /*
     85        1.1    simonb  * The Au1X00 MAC has 4 transmit and receive descriptors.  Each buffer
     86        1.1    simonb  * must consist of a single DMA segment, and must be aligned to a 2K
     87        1.1    simonb  * boundary.  Therefore, this driver does not perform DMA directly
     88        1.1    simonb  * to/from mbufs.  Instead, we copy the data to/from buffers allocated
     89        1.1    simonb  * at device attach time.
     90        1.1    simonb  *
     91        1.1    simonb  * We also skip the bus_dma dance.  The MAC is built in to the CPU, so
     92        1.1    simonb  * there's little point in not making assumptions based on the CPU type.
     93        1.1    simonb  * We also program the Au1X00 cache to be DMA coherent, so the buffers
     94        1.1    simonb  * are accessed via KSEG0 addresses.
     95        1.1    simonb  */
     96        1.1    simonb #define	AUMAC_NTXDESC		4
     97        1.1    simonb #define	AUMAC_NTXDESC_MASK	(AUMAC_NTXDESC - 1)
     98        1.1    simonb 
     99        1.1    simonb #define	AUMAC_NRXDESC		4
    100        1.1    simonb #define	AUMAC_NRXDESC_MASK	(AUMAC_NRXDESC - 1)
    101        1.1    simonb 
    102        1.1    simonb #define	AUMAC_NEXTTX(x)		(((x) + 1) & AUMAC_NTXDESC_MASK)
    103        1.1    simonb #define	AUMAC_NEXTRX(x)		(((x) + 1) & AUMAC_NRXDESC_MASK)
    104        1.1    simonb 
    105        1.1    simonb #define	AUMAC_TXBUF_OFFSET	0
    106        1.1    simonb #define	AUMAC_RXBUF_OFFSET	(MAC_BUFLEN * AUMAC_NTXDESC)
    107        1.1    simonb #define	AUMAC_BUFSIZE		(MAC_BUFLEN * (AUMAC_NTXDESC + AUMAC_NRXDESC))
    108        1.1    simonb 
    109        1.1    simonb struct aumac_buf {
    110       1.20    simonb 	vaddr_t buf_vaddr;		/* virtual address of buffer */
    111        1.1    simonb 	bus_addr_t buf_paddr;		/* DMA address of buffer */
    112        1.1    simonb };
    113        1.1    simonb 
    114        1.1    simonb /*
    115        1.1    simonb  * Software state per device.
    116        1.1    simonb  */
    117        1.1    simonb struct aumac_softc {
    118       1.33  kiyohara 	device_t sc_dev;		/* generic device information */
    119        1.1    simonb 	bus_space_tag_t sc_st;		/* bus space tag */
    120        1.1    simonb 	bus_space_handle_t sc_mac_sh;	/* MAC space handle */
    121        1.1    simonb 	bus_space_handle_t sc_macen_sh;	/* MAC enable space handle */
    122        1.1    simonb 	bus_space_handle_t sc_dma_sh;	/* DMA space handle */
    123        1.1    simonb 	struct ethercom sc_ethercom;	/* Ethernet common data */
    124        1.1    simonb 	void *sc_sdhook;		/* shutdown hook */
    125        1.1    simonb 
    126       1.35  kiyohara 	int sc_irq;
    127        1.1    simonb 	void *sc_ih;			/* interrupt cookie */
    128        1.1    simonb 
    129        1.1    simonb 	struct mii_data sc_mii;		/* MII/media information */
    130        1.1    simonb 
    131        1.1    simonb 	struct callout sc_tick_ch;	/* tick callout */
    132        1.1    simonb 
    133        1.1    simonb 	/* Transmit and receive buffers */
    134        1.1    simonb 	struct aumac_buf sc_txbufs[AUMAC_NTXDESC];
    135        1.1    simonb 	struct aumac_buf sc_rxbufs[AUMAC_NRXDESC];
    136       1.19  christos 	void *sc_bufaddr;
    137        1.1    simonb 
    138        1.1    simonb 	int sc_txfree;			/* number of free Tx descriptors */
    139        1.1    simonb 	int sc_txnext;			/* next Tx descriptor to use */
    140        1.1    simonb 	int sc_txdirty;			/* first dirty Tx descriptor */
    141        1.1    simonb 
    142        1.1    simonb 	int sc_rxptr;			/* next ready Rx descriptor */
    143        1.1    simonb 
    144       1.32       tls 	krndsource_t rnd_source;
    145       1.15    simonb 
    146        1.1    simonb #ifdef AUMAC_EVENT_COUNTERS
    147        1.1    simonb 	struct evcnt sc_ev_txstall;	/* Tx stalled */
    148        1.1    simonb 	struct evcnt sc_ev_rxstall;	/* Rx stalled */
    149        1.1    simonb 	struct evcnt sc_ev_txintr;	/* Tx interrupts */
    150        1.1    simonb 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    151        1.1    simonb #endif
    152        1.1    simonb 
    153        1.1    simonb 	uint32_t sc_control;		/* MAC_CONTROL contents */
    154        1.1    simonb 	uint32_t sc_flowctrl;		/* MAC_FLOWCTRL contents */
    155        1.1    simonb };
    156        1.1    simonb 
    157        1.1    simonb #ifdef AUMAC_EVENT_COUNTERS
    158        1.1    simonb #define	AUMAC_EVCNT_INCR(ev)	(ev)->ev_count++
    159        1.8    simonb #else
    160        1.8    simonb #define	AUMAC_EVCNT_INCR(ev)	/* nothing */
    161        1.1    simonb #endif
    162        1.1    simonb 
    163        1.1    simonb #define	AUMAC_INIT_RXDESC(sc, x)					\
    164        1.1    simonb do {									\
    165        1.1    simonb 	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
    166        1.1    simonb 	    MACDMA_RX_STAT((x)), 0);					\
    167        1.1    simonb 	bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh,			\
    168        1.1    simonb 	    MACDMA_RX_ADDR((x)),					\
    169        1.1    simonb 	    (sc)->sc_rxbufs[(x)].buf_paddr | RX_ADDR_EN);		\
    170        1.1    simonb } while (/*CONSTCOND*/0)
    171        1.1    simonb 
    172        1.1    simonb static void	aumac_start(struct ifnet *);
    173        1.1    simonb static void	aumac_watchdog(struct ifnet *);
    174       1.19  christos static int	aumac_ioctl(struct ifnet *, u_long, void *);
    175        1.1    simonb static int	aumac_init(struct ifnet *);
    176        1.1    simonb static void	aumac_stop(struct ifnet *, int);
    177        1.1    simonb 
    178        1.1    simonb static void	aumac_shutdown(void *);
    179        1.1    simonb 
    180        1.1    simonb static void	aumac_tick(void *);
    181        1.1    simonb 
    182        1.1    simonb static void	aumac_set_filter(struct aumac_softc *);
    183        1.1    simonb 
    184        1.1    simonb static void	aumac_powerup(struct aumac_softc *);
    185        1.1    simonb static void	aumac_powerdown(struct aumac_softc *);
    186        1.1    simonb 
    187        1.1    simonb static int	aumac_intr(void *);
    188       1.15    simonb static int	aumac_txintr(struct aumac_softc *);
    189       1.15    simonb static int	aumac_rxintr(struct aumac_softc *);
    190        1.1    simonb 
    191  1.43.14.2  pgoyette static int	aumac_mii_readreg(device_t, int, int, uint16_t *);
    192  1.43.14.2  pgoyette static int	aumac_mii_writereg(device_t, int, int, uint16_t);
    193       1.37      matt static void	aumac_mii_statchg(struct ifnet *);
    194        1.1    simonb static int	aumac_mii_wait(struct aumac_softc *, const char *);
    195        1.1    simonb 
    196       1.33  kiyohara static int	aumac_match(device_t, struct cfdata *, void *);
    197       1.33  kiyohara static void	aumac_attach(device_t, device_t, void *);
    198        1.1    simonb 
    199        1.1    simonb int	aumac_copy_small = 0;
    200        1.1    simonb 
    201       1.33  kiyohara CFATTACH_DECL_NEW(aumac, sizeof(struct aumac_softc),
    202        1.6   thorpej     aumac_match, aumac_attach, NULL, NULL);
    203        1.1    simonb 
    204        1.1    simonb static int
    205       1.33  kiyohara aumac_match(device_t parent, struct cfdata *cf, void *aux)
    206        1.1    simonb {
    207        1.1    simonb 	struct aubus_attach_args *aa = aux;
    208        1.1    simonb 
    209        1.3   thorpej 	if (strcmp(aa->aa_name, cf->cf_name) == 0)
    210        1.1    simonb 		return (1);
    211        1.1    simonb 
    212        1.1    simonb 	return (0);
    213        1.1    simonb }
    214        1.1    simonb 
    215        1.1    simonb static void
    216       1.33  kiyohara aumac_attach(device_t parent, device_t self, void *aux)
    217        1.1    simonb {
    218       1.17   thorpej 	const uint8_t *enaddr;
    219       1.17   thorpej 	prop_data_t ea;
    220       1.33  kiyohara 	struct aumac_softc *sc = device_private(self);
    221        1.1    simonb 	struct aubus_attach_args *aa = aux;
    222        1.1    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    223        1.1    simonb 	struct pglist pglist;
    224        1.1    simonb 	paddr_t bufaddr;
    225       1.20    simonb 	vaddr_t vbufaddr;
    226        1.1    simonb 	int i;
    227        1.1    simonb 
    228       1.21        ad 	callout_init(&sc->sc_tick_ch, 0);
    229        1.1    simonb 
    230       1.33  kiyohara 	aprint_normal(": Au1X00 10/100 Ethernet\n");
    231       1.33  kiyohara 	aprint_naive("\n");
    232        1.1    simonb 
    233       1.33  kiyohara 	sc->sc_dev = self;
    234        1.1    simonb 	sc->sc_st = aa->aa_st;
    235        1.1    simonb 
    236        1.1    simonb 	/* Get the MAC address. */
    237       1.33  kiyohara 	ea = prop_dictionary_get(device_properties(self), "mac-address");
    238       1.17   thorpej 	if (ea == NULL) {
    239       1.33  kiyohara 		aprint_error_dev(self, "unable to get mac-addr property\n");
    240        1.1    simonb 		return;
    241        1.1    simonb 	}
    242       1.17   thorpej 	KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
    243       1.17   thorpej 	KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
    244       1.17   thorpej 	enaddr = prop_data_data_nocopy(ea);
    245        1.1    simonb 
    246       1.33  kiyohara 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
    247        1.1    simonb 
    248        1.1    simonb 	/* Map the device. */
    249        1.1    simonb 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_BASE],
    250        1.1    simonb 	    MACx_SIZE, 0, &sc->sc_mac_sh) != 0) {
    251       1.33  kiyohara 		aprint_error_dev(self, "unable to map MAC registers\n");
    252        1.1    simonb 		return;
    253        1.1    simonb 	}
    254        1.1    simonb 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_ENABLE],
    255        1.1    simonb 	    MACENx_SIZE, 0, &sc->sc_macen_sh) != 0) {
    256       1.33  kiyohara 		aprint_error_dev(self, "unable to map MACEN registers\n");
    257        1.1    simonb 		return;
    258        1.1    simonb 	}
    259        1.1    simonb 	if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_DMA_BASE],
    260        1.1    simonb 	    MACx_DMA_SIZE, 0, &sc->sc_dma_sh) != 0) {
    261       1.33  kiyohara 		aprint_error_dev(self, "unable to map MACDMA registers\n");
    262        1.1    simonb 		return;
    263        1.1    simonb 	}
    264        1.1    simonb 
    265        1.1    simonb 	/* Make sure the MAC is powered off. */
    266        1.1    simonb 	aumac_powerdown(sc);
    267        1.1    simonb 
    268        1.1    simonb 	/* Hook up the interrupt handler. */
    269        1.2    simonb 	sc->sc_ih = au_intr_establish(aa->aa_irq[0], 1, IPL_NET, IST_LEVEL,
    270        1.1    simonb 	    aumac_intr, sc);
    271        1.1    simonb 	if (sc->sc_ih == NULL) {
    272       1.33  kiyohara 		aprint_error_dev(self,
    273       1.33  kiyohara 		    "unable to register interrupt handler\n");
    274        1.1    simonb 		return;
    275        1.1    simonb 	}
    276       1.35  kiyohara 	sc->sc_irq = aa->aa_irq[0];
    277       1.35  kiyohara 	au_intr_disable(sc->sc_irq);
    278        1.1    simonb 
    279        1.1    simonb 	/*
    280        1.1    simonb 	 * Allocate space for the transmit and receive buffers.
    281        1.1    simonb 	 */
    282        1.1    simonb 	if (uvm_pglistalloc(AUMAC_BUFSIZE, 0, ctob(physmem), PAGE_SIZE, 0,
    283        1.1    simonb 	    &pglist, 1, 0))
    284        1.1    simonb 		return;
    285        1.1    simonb 
    286       1.13      yamt 	bufaddr = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
    287       1.20    simonb 	vbufaddr = MIPS_PHYS_TO_KSEG0(bufaddr);
    288        1.1    simonb 
    289        1.1    simonb 	for (i = 0; i < AUMAC_NTXDESC; i++) {
    290        1.1    simonb 		int offset = AUMAC_TXBUF_OFFSET + (i * MAC_BUFLEN);
    291        1.1    simonb 
    292        1.1    simonb 		sc->sc_txbufs[i].buf_vaddr = vbufaddr + offset;
    293        1.1    simonb 		sc->sc_txbufs[i].buf_paddr = bufaddr + offset;
    294        1.1    simonb 	}
    295        1.1    simonb 
    296        1.1    simonb 	for (i = 0; i < AUMAC_NRXDESC; i++) {
    297        1.1    simonb 		int offset = AUMAC_RXBUF_OFFSET + (i * MAC_BUFLEN);
    298        1.1    simonb 
    299        1.1    simonb 		sc->sc_rxbufs[i].buf_vaddr = vbufaddr + offset;
    300        1.1    simonb 		sc->sc_rxbufs[i].buf_paddr = bufaddr + offset;
    301        1.1    simonb 	}
    302        1.1    simonb 
    303        1.1    simonb 	/*
    304        1.1    simonb 	 * Power up the MAC before accessing any MAC registers (including
    305        1.1    simonb 	 * MII configuration.
    306        1.1    simonb 	 */
    307        1.1    simonb 	aumac_powerup(sc);
    308        1.1    simonb 
    309        1.1    simonb 	/*
    310        1.1    simonb 	 * Initialize the media structures and probe the MII.
    311        1.1    simonb 	 */
    312        1.1    simonb 	sc->sc_mii.mii_ifp = ifp;
    313        1.1    simonb 	sc->sc_mii.mii_readreg = aumac_mii_readreg;
    314        1.1    simonb 	sc->sc_mii.mii_writereg = aumac_mii_writereg;
    315        1.1    simonb 	sc->sc_mii.mii_statchg = aumac_mii_statchg;
    316       1.24    dyoung 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    317       1.24    dyoung 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
    318       1.24    dyoung 	    ether_mediastatus);
    319        1.1    simonb 
    320       1.33  kiyohara 	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    321        1.1    simonb 	    MII_OFFSET_ANY, 0);
    322        1.1    simonb 
    323        1.1    simonb 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    324        1.1    simonb 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    325        1.1    simonb 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    326        1.1    simonb 	} else
    327        1.1    simonb 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    328        1.1    simonb 
    329       1.33  kiyohara 	strcpy(ifp->if_xname, device_xname(self));
    330        1.1    simonb 	ifp->if_softc = sc;
    331        1.1    simonb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    332        1.1    simonb 	ifp->if_ioctl = aumac_ioctl;
    333        1.1    simonb 	ifp->if_start = aumac_start;
    334        1.1    simonb 	ifp->if_watchdog = aumac_watchdog;
    335        1.1    simonb 	ifp->if_init = aumac_init;
    336        1.1    simonb 	ifp->if_stop = aumac_stop;
    337        1.1    simonb 	IFQ_SET_READY(&ifp->if_snd);
    338        1.1    simonb 
    339        1.1    simonb 	/* Attach the interface. */
    340        1.1    simonb 	if_attach(ifp);
    341       1.42     ozaki 	if_deferred_start_init(ifp, NULL);
    342        1.1    simonb 	ether_ifattach(ifp, enaddr);
    343        1.1    simonb 
    344       1.34       tls 	rnd_attach_source(&sc->rnd_source, device_xname(self),
    345       1.38       tls 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    346       1.16    simonb 
    347        1.1    simonb #ifdef AUMAC_EVENT_COUNTERS
    348        1.1    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
    349       1.33  kiyohara 	    NULL, device_xname(self), "txstall");
    350        1.1    simonb 	evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC,
    351       1.33  kiyohara 	    NULL, device_xname(self), "rxstall");
    352        1.1    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC,
    353       1.33  kiyohara 	    NULL, device_xname(self), "txintr");
    354        1.1    simonb 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC,
    355       1.33  kiyohara 	    NULL, device_xname(self), "rxintr");
    356        1.1    simonb #endif
    357        1.1    simonb 
    358        1.1    simonb 	/* Make sure the interface is shutdown during reboot. */
    359        1.1    simonb 	sc->sc_sdhook = shutdownhook_establish(aumac_shutdown, sc);
    360        1.1    simonb 	if (sc->sc_sdhook == NULL)
    361       1.33  kiyohara 		aprint_error_dev(self,
    362       1.33  kiyohara 		    "WARNING: unable to establish shutdown hook\n");
    363        1.1    simonb 	return;
    364        1.1    simonb }
    365        1.1    simonb 
    366        1.1    simonb /*
    367        1.1    simonb  * aumac_shutdown:
    368        1.1    simonb  *
    369        1.1    simonb  *	Make sure the interface is stopped at reboot time.
    370        1.1    simonb  */
    371        1.1    simonb static void
    372        1.1    simonb aumac_shutdown(void *arg)
    373        1.1    simonb {
    374        1.1    simonb 	struct aumac_softc *sc = arg;
    375        1.1    simonb 
    376        1.1    simonb 	aumac_stop(&sc->sc_ethercom.ec_if, 1);
    377        1.1    simonb 
    378        1.1    simonb 	/*
    379        1.1    simonb 	 * XXX aumac_stop leaves device powered up at the moment
    380        1.1    simonb 	 * XXX but this still isn't enough to keep yamon happy... :-(
    381        1.1    simonb 	 */
    382        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, 0);
    383        1.1    simonb }
    384        1.1    simonb 
    385        1.1    simonb /*
    386        1.1    simonb  * aumac_start:		[ifnet interface function]
    387        1.1    simonb  *
    388        1.1    simonb  *	Start packet transmission on the interface.
    389        1.1    simonb  */
    390        1.1    simonb static void
    391        1.1    simonb aumac_start(struct ifnet *ifp)
    392        1.1    simonb {
    393        1.1    simonb 	struct aumac_softc *sc = ifp->if_softc;
    394        1.1    simonb 	struct mbuf *m;
    395        1.1    simonb 	int nexttx;
    396        1.1    simonb 
    397        1.1    simonb 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    398        1.1    simonb 		return;
    399        1.1    simonb 
    400        1.1    simonb 	/*
    401        1.1    simonb 	 * Loop through the send queue, setting up transmit descriptors
    402        1.1    simonb 	 * unitl we drain the queue, or use up all available transmit
    403        1.1    simonb 	 * descriptors.
    404        1.1    simonb 	 */
    405        1.1    simonb 	for (;;) {
    406        1.1    simonb 		/* Grab a packet off the queue. */
    407        1.1    simonb 		IFQ_POLL(&ifp->if_snd, m);
    408        1.1    simonb 		if (m == NULL)
    409        1.1    simonb 			return;
    410        1.1    simonb 
    411        1.1    simonb 		/* Get a spare descriptor. */
    412        1.1    simonb 		if (sc->sc_txfree == 0) {
    413        1.1    simonb 			/* No more slots left; notify upper layer. */
    414        1.1    simonb 			ifp->if_flags |= IFF_OACTIVE;
    415        1.1    simonb 			AUMAC_EVCNT_INCR(&sc->sc_ev_txstall);
    416        1.1    simonb 			return;
    417        1.1    simonb 		}
    418        1.1    simonb 		nexttx = sc->sc_txnext;
    419        1.1    simonb 
    420        1.1    simonb 		IFQ_DEQUEUE(&ifp->if_snd, m);
    421        1.1    simonb 
    422        1.1    simonb 		/*
    423        1.1    simonb 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    424        1.1    simonb 		 */
    425        1.1    simonb 
    426        1.1    simonb 		m_copydata(m, 0, m->m_pkthdr.len,
    427       1.20    simonb 		    (void *)sc->sc_txbufs[nexttx].buf_vaddr);
    428        1.9    simonb 
    429        1.9    simonb 		/* Zero out the remainder of any short packets. */
    430        1.9    simonb 		if (m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
    431       1.20    simonb 			memset((char *)sc->sc_txbufs[nexttx].buf_vaddr +
    432        1.9    simonb 			    m->m_pkthdr.len, 0,
    433        1.9    simonb 			    ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len);
    434        1.1    simonb 
    435        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    436        1.1    simonb 		    MACDMA_TX_STAT(nexttx), 0);
    437        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    438        1.1    simonb 		    MACDMA_TX_LEN(nexttx),
    439        1.1    simonb 		    m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ?
    440        1.1    simonb 		    ETHER_MIN_LEN - ETHER_CRC_LEN : m->m_pkthdr.len);
    441        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    442        1.1    simonb 		    MACDMA_TX_ADDR(nexttx),
    443        1.1    simonb 		    sc->sc_txbufs[nexttx].buf_paddr | TX_ADDR_EN);
    444        1.1    simonb 		/* XXX - needed??  we should be coherent */
    445        1.1    simonb 		bus_space_barrier(sc->sc_st, sc->sc_dma_sh, 0 /* XXX */,
    446        1.1    simonb 		    0 /* XXX */, BUS_SPACE_BARRIER_WRITE);
    447        1.1    simonb 
    448        1.1    simonb 		/* Advance the Tx pointer. */
    449        1.1    simonb 		sc->sc_txfree--;
    450        1.1    simonb 		sc->sc_txnext = AUMAC_NEXTTX(nexttx);
    451        1.1    simonb 
    452        1.1    simonb 		/* Pass the packet to any BPF listeners. */
    453  1.43.14.1  pgoyette 		bpf_mtap(ifp, m, BPF_D_OUT);
    454        1.1    simonb 
    455        1.1    simonb 		m_freem(m);
    456        1.1    simonb 
    457        1.1    simonb 		/* Set a watchdog timer in case the chip flakes out. */
    458        1.1    simonb 		ifp->if_timer = 5;
    459        1.1    simonb 	}
    460        1.1    simonb 	/* NOTREACHED */
    461        1.1    simonb }
    462        1.1    simonb 
    463        1.1    simonb /*
    464        1.1    simonb  * aumac_watchdog:	[ifnet interface function]
    465        1.1    simonb  *
    466        1.1    simonb  *	Watchdog timer handler.
    467        1.1    simonb  */
    468        1.1    simonb static void
    469        1.1    simonb aumac_watchdog(struct ifnet *ifp)
    470        1.1    simonb {
    471        1.1    simonb 	struct aumac_softc *sc = ifp->if_softc;
    472        1.1    simonb 
    473       1.33  kiyohara 	printf("%s: device timeout\n", device_xname(sc->sc_dev));
    474        1.1    simonb 	(void) aumac_init(ifp);
    475        1.1    simonb 
    476        1.1    simonb 	/* Try to get more packets going. */
    477        1.1    simonb 	aumac_start(ifp);
    478        1.1    simonb }
    479        1.1    simonb 
    480        1.1    simonb /*
    481        1.1    simonb  * aumac_ioctl:		[ifnet interface function]
    482        1.1    simonb  *
    483        1.1    simonb  *	Handle control requests from the operator.
    484        1.1    simonb  */
    485        1.1    simonb static int
    486       1.19  christos aumac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    487        1.1    simonb {
    488        1.1    simonb 	struct aumac_softc *sc = ifp->if_softc;
    489        1.1    simonb 	int s, error;
    490        1.1    simonb 
    491        1.1    simonb 	s = splnet();
    492        1.1    simonb 
    493       1.24    dyoung 	error = ether_ioctl(ifp, cmd, data);
    494       1.24    dyoung 	if (error == ENETRESET) {
    495       1.24    dyoung 		/*
    496       1.24    dyoung 		 * Multicast list has changed; set the hardware filter
    497       1.24    dyoung 		 * accordingly.
    498       1.24    dyoung 		 */
    499       1.24    dyoung 		if (ifp->if_flags & IFF_RUNNING)
    500       1.24    dyoung 			aumac_set_filter(sc);
    501       1.36  kiyohara 		error = 0;
    502        1.1    simonb 	}
    503        1.1    simonb 
    504        1.1    simonb 	/* Try to get more packets going. */
    505        1.1    simonb 	aumac_start(ifp);
    506        1.1    simonb 
    507        1.1    simonb 	splx(s);
    508        1.1    simonb 	return (error);
    509        1.1    simonb }
    510        1.1    simonb 
    511        1.1    simonb /*
    512        1.1    simonb  * aumac_intr:
    513        1.1    simonb  *
    514        1.1    simonb  *	Interrupt service routine.
    515        1.1    simonb  */
    516        1.1    simonb static int
    517        1.1    simonb aumac_intr(void *arg)
    518        1.1    simonb {
    519        1.1    simonb 	struct aumac_softc *sc = arg;
    520       1.15    simonb 	int status;
    521        1.1    simonb 
    522        1.1    simonb 	/*
    523        1.1    simonb 	 * There aren't really any interrupt status bits on the
    524        1.1    simonb 	 * Au1X00 MAC, and each MAC has a dedicated interrupt
    525        1.1    simonb 	 * in the CPU's built-in interrupt controller.  Just
    526        1.1    simonb 	 * check for new incoming packets, and then Tx completions
    527        1.1    simonb 	 * (for status updating).
    528        1.1    simonb 	 */
    529        1.1    simonb 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0)
    530        1.1    simonb 		return (0);
    531        1.1    simonb 
    532       1.15    simonb 	status = aumac_rxintr(sc);
    533       1.15    simonb 	status += aumac_txintr(sc);
    534        1.1    simonb 
    535       1.34       tls 	rnd_add_uint32(&sc->rnd_source, status);
    536       1.15    simonb 
    537       1.15    simonb 	return status;
    538        1.1    simonb }
    539        1.1    simonb 
    540        1.1    simonb /*
    541        1.1    simonb  * aumac_txintr:
    542        1.1    simonb  *
    543        1.1    simonb  *	Helper; handle transmit interrupts.
    544        1.1    simonb  */
    545       1.15    simonb static int
    546        1.1    simonb aumac_txintr(struct aumac_softc *sc)
    547        1.1    simonb {
    548        1.1    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    549        1.1    simonb 	uint32_t stat;
    550        1.1    simonb 	int i;
    551       1.15    simonb 	int pkts = 0;
    552        1.1    simonb 
    553        1.1    simonb 	for (i = sc->sc_txdirty; sc->sc_txfree != AUMAC_NTXDESC;
    554        1.1    simonb 	     i = AUMAC_NEXTTX(i)) {
    555        1.1    simonb 		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    556        1.1    simonb 		     MACDMA_TX_ADDR(i)) & TX_ADDR_DN) == 0)
    557        1.1    simonb 			break;
    558       1.15    simonb 		pkts++;
    559        1.1    simonb 
    560        1.1    simonb 		/* ACK interrupt. */
    561        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    562        1.1    simonb 		    MACDMA_TX_ADDR(i), 0);
    563        1.1    simonb 
    564        1.1    simonb 		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    565        1.1    simonb 		    MACDMA_TX_STAT(i));
    566        1.1    simonb 
    567        1.1    simonb 		if (stat & TX_STAT_FA) {
    568        1.1    simonb 			/* XXX STATS */
    569        1.1    simonb 			ifp->if_oerrors++;
    570        1.1    simonb 		} else
    571        1.1    simonb 			ifp->if_opackets++;
    572        1.1    simonb 
    573        1.1    simonb 		if (stat & TX_STAT_EC)
    574        1.1    simonb 			ifp->if_collisions += 16;
    575        1.1    simonb 		else
    576        1.1    simonb 			ifp->if_collisions += TX_STAT_CC(stat);
    577        1.1    simonb 
    578        1.1    simonb 		sc->sc_txfree++;
    579        1.1    simonb 		ifp->if_flags &= ~IFF_OACTIVE;
    580        1.1    simonb 
    581        1.1    simonb 		/* Try to queue more packets. */
    582       1.42     ozaki 		if_schedule_deferred_start(ifp);
    583        1.1    simonb 	}
    584        1.1    simonb 
    585       1.15    simonb 	if (pkts)
    586        1.1    simonb 		AUMAC_EVCNT_INCR(&sc->sc_ev_txintr);
    587        1.1    simonb 
    588        1.1    simonb 	/* Update the dirty descriptor pointer. */
    589        1.1    simonb 	sc->sc_txdirty = i;
    590        1.1    simonb 
    591        1.1    simonb 	/*
    592        1.1    simonb 	 * If there are no more pending transmissions, cancel the watchdog
    593        1.1    simonb 	 * timer.
    594        1.1    simonb 	 */
    595        1.1    simonb 	if (sc->sc_txfree == AUMAC_NTXDESC)
    596        1.1    simonb 		ifp->if_timer = 0;
    597       1.15    simonb 
    598       1.15    simonb 	return pkts;
    599        1.1    simonb }
    600        1.1    simonb 
    601        1.1    simonb /*
    602        1.1    simonb  * aumac_rxintr:
    603        1.1    simonb  *
    604        1.1    simonb  *	Helper; handle receive interrupts.
    605        1.1    simonb  */
    606       1.15    simonb static int
    607        1.1    simonb aumac_rxintr(struct aumac_softc *sc)
    608        1.1    simonb {
    609        1.1    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    610        1.1    simonb 	struct mbuf *m;
    611        1.1    simonb 	uint32_t stat;
    612        1.1    simonb 	int i, len;
    613        1.1    simonb 	int pkts = 0;
    614        1.1    simonb 
    615        1.1    simonb 	for (i = sc->sc_rxptr;; i = AUMAC_NEXTRX(i)) {
    616        1.1    simonb 		if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    617        1.1    simonb 		     MACDMA_RX_ADDR(i)) & RX_ADDR_DN) == 0)
    618        1.1    simonb 			break;
    619        1.1    simonb 		pkts++;
    620        1.1    simonb 
    621        1.1    simonb 		stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    622        1.1    simonb 		    MACDMA_RX_STAT(i));
    623        1.1    simonb 
    624        1.1    simonb #define PRINTERR(str)							\
    625        1.1    simonb 	do {								\
    626        1.1    simonb 		error++;						\
    627       1.33  kiyohara 		printf("%s: %s\n", device_xname(sc->sc_dev), str);	\
    628        1.1    simonb 	} while (0)
    629        1.1    simonb 
    630        1.1    simonb 		if (stat & RX_STAT_ERRS) {
    631        1.1    simonb 			int error = 0;
    632        1.1    simonb 
    633       1.18   gdamore #if 0	/*
    634       1.18   gdamore 	 * Missed frames are a semi-frequent occurence with this hardware,
    635       1.18   gdamore 	 * and reporting of them just makes everything run slower and fills
    636       1.18   gdamore 	 * the system log.  Be silent.
    637       1.18   gdamore 	 *
    638       1.18   gdamore 	 * Additionally, this missed bit indicates an error with the previous
    639       1.18   gdamore 	 * packet, and not with this one!  So PRINTERR is definitely wrong
    640       1.18   gdamore 	 * here.
    641       1.18   gdamore 	 *
    642       1.18   gdamore 	 * These should probably all be converted to evcnt counters anyway.
    643       1.18   gdamore 	 */
    644        1.1    simonb 			if (stat & RX_STAT_MI)
    645        1.1    simonb 				PRINTERR("missed frame");
    646       1.18   gdamore #endif
    647        1.1    simonb 			if (stat & RX_STAT_UC)
    648        1.1    simonb 				PRINTERR("unknown control frame");
    649        1.1    simonb 			if (stat & RX_STAT_LE)
    650        1.1    simonb 				PRINTERR("short frame");
    651        1.1    simonb 			if (stat & RX_STAT_CR)
    652        1.1    simonb 				PRINTERR("CRC error");
    653        1.1    simonb 			if (stat & RX_STAT_ME)
    654        1.1    simonb 				PRINTERR("medium error");
    655        1.1    simonb 			if (stat & RX_STAT_CS)
    656        1.1    simonb 				PRINTERR("late collision");
    657        1.1    simonb 			if (stat & RX_STAT_FL)
    658        1.1    simonb 				PRINTERR("frame too big");
    659        1.1    simonb 			if (stat & RX_STAT_RF)
    660        1.1    simonb 				PRINTERR("runt frame (collision)");
    661        1.1    simonb 			if (stat & RX_STAT_WT)
    662        1.1    simonb 				PRINTERR("watch dog");
    663        1.1    simonb 			if (stat & RX_STAT_DB) {
    664        1.1    simonb 				if (stat & (RX_STAT_CS | RX_STAT_RF |
    665        1.1    simonb 				    RX_STAT_CR)) {
    666        1.1    simonb 					if (!error)
    667        1.1    simonb 						goto pktok;
    668        1.1    simonb 				} else
    669        1.1    simonb 					PRINTERR("dribbling bit");
    670        1.1    simonb 			}
    671        1.1    simonb #undef PRINTERR
    672        1.1    simonb 			ifp->if_ierrors++;
    673        1.1    simonb 
    674        1.1    simonb  dropit:
    675        1.1    simonb 			/* reuse the current descriptor */
    676        1.1    simonb 			AUMAC_INIT_RXDESC(sc, i);
    677        1.1    simonb 			continue;
    678        1.1    simonb 		}
    679        1.1    simonb  pktok:
    680        1.1    simonb 		len = RX_STAT_L(stat);
    681        1.1    simonb 
    682        1.1    simonb 		/*
    683        1.1    simonb 		 * The Au1X00 MAC includes the CRC with every packet;
    684        1.1    simonb 		 * trim it off here.
    685        1.1    simonb 		 */
    686        1.1    simonb 		len -= ETHER_CRC_LEN;
    687        1.1    simonb 
    688        1.1    simonb 		/*
    689        1.1    simonb 		 * Truncate the packet if it's too big to fit in
    690        1.1    simonb 		 * a single mbuf cluster.
    691        1.1    simonb 		 */
    692        1.1    simonb 		if (len > MCLBYTES - 2)
    693        1.1    simonb 			len = MCLBYTES - 2;
    694        1.1    simonb 
    695        1.1    simonb 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    696        1.1    simonb 		if (m == NULL) {
    697        1.1    simonb 			printf("%s: unable to allocate Rx mbuf\n",
    698       1.33  kiyohara 			    device_xname(sc->sc_dev));
    699        1.1    simonb 			goto dropit;
    700        1.1    simonb 		}
    701        1.1    simonb 		if (len > MHLEN - 2) {
    702        1.1    simonb 			MCLGET(m, M_DONTWAIT);
    703        1.1    simonb 			if ((m->m_flags & M_EXT) == 0) {
    704        1.1    simonb 				printf("%s: unable to allocate Rx cluster\n",
    705       1.33  kiyohara 				    device_xname(sc->sc_dev));
    706        1.1    simonb 				m_freem(m);
    707        1.1    simonb 				goto dropit;
    708        1.1    simonb 			}
    709        1.1    simonb 		}
    710        1.1    simonb 
    711        1.1    simonb 		m->m_data += 2;		/* align payload */
    712       1.19  christos 		memcpy(mtod(m, void *),
    713       1.20    simonb 		    (void *)sc->sc_rxbufs[i].buf_vaddr, len);
    714        1.1    simonb 		AUMAC_INIT_RXDESC(sc, i);
    715        1.1    simonb 
    716       1.41     ozaki 		m_set_rcvif(m, ifp);
    717        1.1    simonb 		m->m_pkthdr.len = m->m_len = len;
    718        1.1    simonb 
    719        1.1    simonb 		/* Pass it on. */
    720       1.40     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
    721        1.1    simonb 	}
    722        1.1    simonb 	if (pkts)
    723        1.1    simonb 		AUMAC_EVCNT_INCR(&sc->sc_ev_rxintr);
    724        1.1    simonb 	if (pkts == AUMAC_NRXDESC)
    725        1.1    simonb 		AUMAC_EVCNT_INCR(&sc->sc_ev_rxstall);
    726        1.1    simonb 
    727        1.1    simonb 	/* Update the receive pointer. */
    728        1.1    simonb 	sc->sc_rxptr = i;
    729       1.15    simonb 
    730       1.15    simonb 	return pkts;
    731        1.1    simonb }
    732        1.1    simonb 
    733        1.1    simonb /*
    734        1.1    simonb  * aumac_tick:
    735        1.1    simonb  *
    736        1.1    simonb  *	One second timer, used to tick the MII.
    737        1.1    simonb  */
    738        1.1    simonb static void
    739        1.1    simonb aumac_tick(void *arg)
    740        1.1    simonb {
    741        1.1    simonb 	struct aumac_softc *sc = arg;
    742        1.1    simonb 	int s;
    743        1.1    simonb 
    744        1.1    simonb 	s = splnet();
    745        1.1    simonb 	mii_tick(&sc->sc_mii);
    746        1.1    simonb 	splx(s);
    747        1.1    simonb 
    748        1.1    simonb 	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);
    749        1.1    simonb }
    750        1.1    simonb 
    751        1.1    simonb /*
    752        1.1    simonb  * aumac_init:		[ifnet interface function]
    753        1.1    simonb  *
    754        1.1    simonb  *	Initialize the interface.  Must be called at splnet().
    755        1.1    simonb  */
    756        1.1    simonb static int
    757        1.1    simonb aumac_init(struct ifnet *ifp)
    758        1.1    simonb {
    759        1.1    simonb 	struct aumac_softc *sc = ifp->if_softc;
    760        1.1    simonb 	int i, error = 0;
    761        1.1    simonb 
    762        1.1    simonb 	/* Cancel any pending I/O, reset MAC. */
    763        1.1    simonb 	aumac_stop(ifp, 0);
    764        1.1    simonb 
    765        1.1    simonb 	/* Set up the transmit ring. */
    766        1.1    simonb 	for (i = 0; i < AUMAC_NTXDESC; i++) {
    767        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    768        1.1    simonb 		    MACDMA_TX_STAT(i), 0);
    769        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    770        1.1    simonb 		    MACDMA_TX_LEN(i), 0);
    771        1.1    simonb 		bus_space_write_4(sc->sc_st, sc->sc_dma_sh,
    772        1.1    simonb 		    MACDMA_TX_ADDR(i), sc->sc_txbufs[i].buf_paddr);
    773        1.1    simonb 	}
    774        1.1    simonb 	sc->sc_txfree = AUMAC_NTXDESC;
    775        1.1    simonb 	sc->sc_txnext = TX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    776        1.1    simonb 	    MACDMA_TX_ADDR(0)));
    777        1.1    simonb 	sc->sc_txdirty = sc->sc_txnext;
    778        1.1    simonb 
    779        1.1    simonb 	/* Set up the receive ring. */
    780        1.1    simonb 	for (i = 0; i < AUMAC_NRXDESC; i++)
    781        1.1    simonb 			AUMAC_INIT_RXDESC(sc, i);
    782        1.1    simonb 	sc->sc_rxptr = RX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh,
    783        1.1    simonb 	    MACDMA_RX_ADDR(0)));
    784        1.1    simonb 
    785        1.1    simonb 	/*
    786        1.1    simonb 	 * Power up the MAC.
    787        1.1    simonb 	 */
    788        1.1    simonb 	aumac_powerup(sc);
    789        1.1    simonb 
    790        1.1    simonb 	sc->sc_control |= CONTROL_DO | CONTROL_TE | CONTROL_RE;
    791        1.1    simonb #if _BYTE_ORDER == _BIG_ENDIAN
    792        1.1    simonb 	sc->sc_control |= CONTROL_EM;
    793        1.1    simonb #endif
    794        1.1    simonb 
    795        1.1    simonb 	/* Set the media. */
    796       1.24    dyoung 	if ((error = ether_mediachange(ifp)) != 0)
    797       1.24    dyoung 		goto out;
    798        1.1    simonb 
    799        1.1    simonb 	/*
    800        1.1    simonb 	 * Set the receive filter.  This will actually start the transmit
    801        1.1    simonb 	 * and receive processes.
    802        1.1    simonb 	 */
    803        1.1    simonb 	aumac_set_filter(sc);
    804        1.1    simonb 
    805        1.1    simonb 	/* Start the one second clock. */
    806        1.1    simonb 	callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc);
    807        1.1    simonb 
    808        1.1    simonb 	/* ...all done! */
    809        1.1    simonb 	ifp->if_flags |= IFF_RUNNING;
    810        1.1    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
    811        1.1    simonb 
    812       1.35  kiyohara 	au_intr_enable(sc->sc_irq);
    813       1.24    dyoung out:
    814        1.1    simonb 	if (error)
    815       1.33  kiyohara 		printf("%s: interface not running\n", device_xname(sc->sc_dev));
    816        1.1    simonb 	return (error);
    817        1.1    simonb }
    818        1.1    simonb 
    819        1.1    simonb /*
    820        1.1    simonb  * aumac_stop:		[ifnet interface function]
    821        1.1    simonb  *
    822        1.1    simonb  *	Stop transmission on the interface.
    823        1.1    simonb  */
    824        1.1    simonb static void
    825        1.1    simonb aumac_stop(struct ifnet *ifp, int disable)
    826        1.1    simonb {
    827        1.1    simonb 	struct aumac_softc *sc = ifp->if_softc;
    828        1.1    simonb 
    829        1.1    simonb 	/* Stop the one-second clock. */
    830        1.1    simonb 	callout_stop(&sc->sc_tick_ch);
    831        1.1    simonb 
    832        1.1    simonb 	/* Down the MII. */
    833        1.1    simonb 	mii_down(&sc->sc_mii);
    834        1.1    simonb 
    835        1.1    simonb 	/* Stop the transmit and receive processes. */
    836        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 0);
    837        1.1    simonb 
    838        1.1    simonb 	/* Power down/reset the MAC. */
    839        1.1    simonb 	aumac_powerdown(sc);
    840        1.1    simonb 
    841       1.35  kiyohara 	au_intr_disable(sc->sc_irq);
    842       1.35  kiyohara 
    843        1.1    simonb 	/* Mark the interface as down and cancel the watchdog timer. */
    844        1.1    simonb 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    845        1.1    simonb 	ifp->if_timer = 0;
    846        1.1    simonb }
    847        1.1    simonb 
    848        1.1    simonb /*
    849        1.1    simonb  * aumac_powerdown:
    850        1.1    simonb  *
    851        1.1    simonb  *	Power down the MAC.
    852        1.1    simonb  */
    853        1.1    simonb static void
    854        1.1    simonb aumac_powerdown(struct aumac_softc *sc)
    855        1.1    simonb {
    856        1.1    simonb 
    857        1.1    simonb 	/* Disable the MAC clocks, and place the device in reset. */
    858        1.1    simonb 	// bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP);
    859        1.1    simonb 
    860        1.1    simonb 	// delay(10000);
    861        1.1    simonb }
    862        1.1    simonb 
    863        1.1    simonb /*
    864        1.1    simonb  * aumac_powerup:
    865        1.1    simonb  *
    866        1.1    simonb  *	Bring the device out of reset.
    867        1.1    simonb  */
    868        1.1    simonb static void
    869        1.1    simonb aumac_powerup(struct aumac_softc *sc)
    870        1.1    simonb {
    871        1.1    simonb 
    872        1.1    simonb 	/* Enable clocks to the MAC. */
    873        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP|MACEN_CE);
    874        1.1    simonb 
    875        1.1    simonb 	/* Enable MAC, coherent transactions, pass only valid frames. */
    876        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0,
    877        1.1    simonb 	    MACEN_E2|MACEN_E1|MACEN_E0|MACEN_CE);
    878        1.1    simonb 
    879        1.1    simonb 	delay(20000);
    880        1.1    simonb }
    881        1.1    simonb 
    882        1.1    simonb /*
    883        1.1    simonb  * aumac_set_filter:
    884        1.1    simonb  *
    885        1.1    simonb  *	Set up the receive filter.
    886        1.1    simonb  */
    887        1.1    simonb static void
    888        1.1    simonb aumac_set_filter(struct aumac_softc *sc)
    889        1.1    simonb {
    890        1.1    simonb 	struct ethercom *ec = &sc->sc_ethercom;
    891        1.1    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    892        1.1    simonb 	struct ether_multi *enm;
    893        1.1    simonb 	struct ether_multistep step;
    894       1.22    dyoung 	const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    895        1.1    simonb 	uint32_t mchash[2], crc;
    896        1.1    simonb 
    897        1.1    simonb 	sc->sc_control &= ~(CONTROL_PM | CONTROL_PR);
    898        1.1    simonb 
    899        1.1    simonb 	/* Stop the receiver. */
    900        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    901        1.1    simonb 	    sc->sc_control & ~CONTROL_RE);
    902        1.1    simonb 
    903        1.1    simonb 	if (ifp->if_flags & IFF_PROMISC) {
    904        1.1    simonb 		sc->sc_control |= CONTROL_PR;
    905        1.1    simonb 		goto allmulti;
    906        1.1    simonb 	}
    907        1.1    simonb 
    908        1.1    simonb 	/* Set the station address. */
    909        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRHIGH,
    910        1.1    simonb 	    enaddr[4] | (enaddr[5] << 8));
    911        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRLOW,
    912        1.1    simonb 	    enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
    913        1.1    simonb 	    (enaddr[3] << 24));
    914        1.1    simonb 
    915        1.1    simonb 	sc->sc_control |= CONTROL_HP;
    916        1.1    simonb 
    917        1.1    simonb 	mchash[0] = mchash[1] = 0;
    918        1.1    simonb 
    919        1.1    simonb 	/*
    920        1.1    simonb 	 * Set up the multicast address filter by passing all multicast
    921        1.1    simonb 	 * addresses through a CRC generator, and then using the high
    922        1.1    simonb 	 * order 6 bits as an index into the 64-bit multicast hash table.
    923        1.1    simonb 	 * The high order bits select the word, while the rest of the bits
    924        1.1    simonb 	 * select the bit within the word.
    925        1.1    simonb 	 */
    926        1.1    simonb 	ETHER_FIRST_MULTI(step, ec, enm);
    927        1.1    simonb 	while (enm != NULL) {
    928        1.1    simonb 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    929        1.1    simonb 			/*
    930        1.1    simonb 			 * We must listen to a range of multicast addresses.
    931        1.1    simonb 			 * For now, just accept all multicasts, rather than
    932        1.1    simonb 			 * trying to set only those filter bits needed to match
    933        1.1    simonb 			 * the range.  (At this time, the only use of address
    934        1.1    simonb 			 * ranges is for IP multicast routing, for which the
    935        1.1    simonb 			 * range is large enough to require all bits set.)
    936        1.1    simonb 			 */
    937        1.1    simonb 			goto allmulti;
    938        1.1    simonb 		}
    939        1.1    simonb 
    940        1.1    simonb 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
    941        1.1    simonb 
    942        1.1    simonb 		/* Just want the 6 most significant bits. */
    943        1.1    simonb 		crc >>= 26;
    944        1.1    simonb 
    945        1.1    simonb 		/* Set the corresponding bit in the filter. */
    946        1.1    simonb 		mchash[crc >> 5] |= 1U << (crc & 0x1f);
    947        1.1    simonb 
    948        1.1    simonb 		ETHER_NEXT_MULTI(step, enm);
    949        1.1    simonb 	}
    950        1.1    simonb 
    951        1.1    simonb 	ifp->if_flags &= ~IFF_ALLMULTI;
    952        1.1    simonb 
    953        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHHIGH,
    954        1.1    simonb 	    mchash[1]);
    955        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHLOW,
    956        1.1    simonb 	    mchash[0]);
    957        1.1    simonb 
    958        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    959        1.1    simonb 	    sc->sc_control);
    960        1.1    simonb 	return;
    961        1.1    simonb 
    962        1.1    simonb  allmulti:
    963        1.1    simonb 	sc->sc_control |= CONTROL_PM;
    964        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
    965        1.1    simonb 	    sc->sc_control);
    966        1.1    simonb }
    967        1.1    simonb 
    968        1.1    simonb /*
    969        1.1    simonb  * aumac_mii_wait:
    970        1.1    simonb  *
    971        1.1    simonb  *	Wait for the MII interface to not be busy.
    972        1.1    simonb  */
    973        1.1    simonb static int
    974        1.1    simonb aumac_mii_wait(struct aumac_softc *sc, const char *msg)
    975        1.1    simonb {
    976        1.1    simonb 	int i;
    977        1.1    simonb 
    978        1.1    simonb 	for (i = 0; i < 10000; i++) {
    979        1.1    simonb 		if ((bus_space_read_4(sc->sc_st, sc->sc_mac_sh,
    980        1.1    simonb 		     MAC_MIICTRL) & MIICTRL_MB) == 0)
    981        1.1    simonb 			return (0);
    982        1.1    simonb 		delay(10);
    983        1.1    simonb 	}
    984        1.1    simonb 
    985       1.33  kiyohara 	printf("%s: MII failed to %s\n", device_xname(sc->sc_dev), msg);
    986  1.43.14.2  pgoyette 	return ETIMEDOUT;
    987        1.1    simonb }
    988        1.1    simonb 
    989        1.1    simonb /*
    990        1.1    simonb  * aumac_mii_readreg:	[mii interface function]
    991        1.1    simonb  *
    992        1.1    simonb  *	Read a PHY register on the MII.
    993        1.1    simonb  */
    994        1.1    simonb static int
    995  1.43.14.2  pgoyette aumac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
    996        1.1    simonb {
    997       1.33  kiyohara 	struct aumac_softc *sc = device_private(self);
    998  1.43.14.2  pgoyette 	int rv;
    999        1.1    simonb 
   1000  1.43.14.2  pgoyette 	if ((rv = aumac_mii_wait(sc, "become ready")) != 0)
   1001  1.43.14.2  pgoyette 		return rv;
   1002        1.1    simonb 
   1003        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
   1004        1.1    simonb 	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg));
   1005        1.1    simonb 
   1006  1.43.14.2  pgoyette 	if ((rv = aumac_mii_wait(sc, "complete")) != 0)
   1007  1.43.14.2  pgoyette 		return rv;
   1008        1.1    simonb 
   1009  1.43.14.2  pgoyette 	*val = bus_space_read_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA)
   1010  1.43.14.2  pgoyette 	    & MIIDATA_MASK;
   1011  1.43.14.2  pgoyette 	return 0;
   1012        1.1    simonb }
   1013        1.1    simonb 
   1014        1.1    simonb /*
   1015        1.1    simonb  * aumac_mii_writereg:	[mii interface function]
   1016        1.1    simonb  *
   1017        1.1    simonb  *	Write a PHY register on the MII.
   1018        1.1    simonb  */
   1019  1.43.14.2  pgoyette static int
   1020  1.43.14.2  pgoyette aumac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   1021        1.1    simonb {
   1022       1.33  kiyohara 	struct aumac_softc *sc = device_private(self);
   1023  1.43.14.2  pgoyette 	int rv;
   1024        1.1    simonb 
   1025  1.43.14.2  pgoyette 	if ((rv = aumac_mii_wait(sc, "become ready")) != 0)
   1026  1.43.14.2  pgoyette 		return rv;
   1027        1.1    simonb 
   1028        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA, val);
   1029        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL,
   1030        1.1    simonb 	    MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg) | MIICTRL_MW);
   1031        1.1    simonb 
   1032  1.43.14.2  pgoyette 	return aumac_mii_wait(sc, "complete");
   1033        1.1    simonb }
   1034        1.1    simonb 
   1035        1.1    simonb /*
   1036        1.1    simonb  * aumac_mii_statchg:	[mii interface function]
   1037        1.1    simonb  *
   1038        1.1    simonb  *	Callback from MII layer when media changes.
   1039        1.1    simonb  */
   1040        1.1    simonb static void
   1041       1.37      matt aumac_mii_statchg(struct ifnet *ifp)
   1042        1.1    simonb {
   1043       1.37      matt 	struct aumac_softc *sc = ifp->if_softc;
   1044        1.1    simonb 
   1045        1.1    simonb 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
   1046        1.1    simonb 		sc->sc_control |= CONTROL_F;
   1047        1.1    simonb 	else
   1048        1.1    simonb 		sc->sc_control &= ~CONTROL_F;
   1049        1.1    simonb 
   1050        1.1    simonb 	bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL,
   1051        1.1    simonb 	    sc->sc_control);
   1052        1.1    simonb }
   1053