Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/arm/rockchip Add Rockchip ethernet driver, untested.



details:   https://anonhg.NetBSD.org/src/rev/ee194c7b769b
branches:  trunk
changeset: 335347:ee194c7b769b
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun Jan 04 03:53:02 2015 +0000

description:
Add Rockchip ethernet driver, untested.

diffstat:

 sys/arch/arm/rockchip/files.rockchip     |    8 +-
 sys/arch/arm/rockchip/rockchip_emac.c    |  849 +++++++++++++++++++++++++++++++
 sys/arch/arm/rockchip/rockchip_emacreg.h |  139 +++++
 3 files changed, 995 insertions(+), 1 deletions(-)

diffs (truncated from 1022 to 300 lines):

diff -r 2d257d4969aa -r ee194c7b769b sys/arch/arm/rockchip/files.rockchip
--- a/sys/arch/arm/rockchip/files.rockchip      Sun Jan 04 01:52:24 2015 +0000
+++ b/sys/arch/arm/rockchip/files.rockchip      Sun Jan 04 03:53:02 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.rockchip,v 1.8 2015/01/03 13:26:31 jmcneill Exp $
+#      $NetBSD: files.rockchip,v 1.9 2015/01/04 03:53:02 jmcneill Exp $
 #
 # Configuration info for Rockchip ARM Peripherals
 #
@@ -45,6 +45,11 @@
 attach  dwctwo at obio with rkdwctwo
 file    arch/arm/rockchip/rockchip_dwctwo.c    rkdwctwo        needs-flag
 
+# VMAC Ethernet Controller
+device rkemac: arp, ether, ifnet, mii
+attach rkemac at obio
+file   arch/arm/rockchip/rockchip_emac.c       rkemac
+
 # Console parameters
 defparam opt_rockchip.h                        CONADDR
 defparam opt_rockchip.h                        CONSPEED
@@ -56,3 +61,4 @@
 # Debugging parameters
 defflag opt_rockchip.h                 ROCKCHIP_CLOCK_DEBUG
 defflag opt_rkiic.h                    RKIIC_DEBUG
+defflag opt_rkemac.h                   RKEMAC_DEBUG
diff -r 2d257d4969aa -r ee194c7b769b sys/arch/arm/rockchip/rockchip_emac.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/rockchip/rockchip_emac.c     Sun Jan 04 03:53:02 2015 +0000
@@ -0,0 +1,849 @@
+/* $NetBSD: rockchip_emac.c,v 1.1 2015/01/04 03:53:02 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "opt_rkemac.h"
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: rockchip_emac.c,v 1.1 2015/01/04 03:53:02 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/cprng.h>
+
+#include <arm/rockchip/rockchip_reg.h>
+#include <arm/rockchip/rockchip_var.h>
+
+#include <net/if.h>
+#include <net/if_ether.h>
+#include <net/if_media.h>
+#include <net/bpf.h>
+
+#include <dev/mii/miivar.h>
+
+#include <arm/rockchip/rockchip_emacreg.h>
+
+#define RKEMAC_ENABLE_INTR     \
+       (EMAC_ENABLE_TXINT|EMAC_ENABLE_RXINT|EMAC_ENABLE_ERR)
+
+#define RKEMAC_RX_RING_COUNT   128
+#define RKEMAC_TX_RING_COUNT   128
+
+#define RKEMAC_MAX_PACKET      1536
+#define RKEMAC_POLLRATE                200
+
+#define RX_DESC_OFFSET(n)      \
+       ((n) * sizeof(struct rkemac_rxdesc))
+#define RX_NEXT(n)             (((n) + 1) & (RKEMAC_RX_RING_COUNT - 1))
+       
+#define TX_DESC_OFFSET(n)      \
+       ((RKEMAC_RX_RING_COUNT+(n)) * sizeof(struct rkemac_txdesc))
+#define TX_NEXT(n)             (((n) + 1) & (RKEMAC_TX_RING_COUNT - 1))
+
+struct rkemac_rxdata {
+       bus_dmamap_t rd_map;
+       struct mbuf *rd_m;
+};
+
+struct rkemac_txdata {
+       bus_dmamap_t td_map;
+       bus_dmamap_t td_active;
+       struct mbuf *td_m;
+};
+
+struct rkemac_txring {
+       bus_addr_t t_physaddr;
+       struct rkemac_txdesc *t_desc;
+       struct rkemac_txdata t_data[RKEMAC_TX_RING_COUNT];
+       int t_cur, t_next, t_queued;
+};
+
+struct rkemac_rxring {
+       bus_addr_t r_physaddr;
+       struct rkemac_rxdesc *r_desc;
+       struct rkemac_rxdata r_data[RKEMAC_RX_RING_COUNT];
+       int r_cur, r_next;
+};
+
+struct rkemac_softc {
+       device_t sc_dev;
+       bus_space_tag_t sc_bst;
+       bus_space_handle_t sc_bsh;
+       bus_dma_tag_t sc_dmat;
+       void *sc_ih;
+
+       struct ethercom sc_ec;
+       struct mii_data sc_mii;
+       callout_t sc_mii_tick;
+       kmutex_t sc_lock;
+
+       bus_dmamap_t sc_ring_dmamap;
+       bus_dma_segment_t sc_ring_dmaseg;
+       struct rkemac_txring sc_txq;
+       struct rkemac_rxring sc_rxq;
+};
+
+static int     rkemac_match(device_t, cfdata_t, void *);
+static void    rkemac_attach(device_t, device_t, void *);
+
+static int     rkemac_dma_init(struct rkemac_softc *);
+
+static int     rkemac_intr(void *);
+
+static int     rkemac_mii_readreg(device_t, int, int);
+static void    rkemac_mii_writereg(device_t, int, int, int);
+static void    rkemac_mii_statchg(struct ifnet *);
+
+static int     rkemac_init(struct ifnet *);
+static void    rkemac_start(struct ifnet *);
+static void    rkemac_stop(struct ifnet *, int);
+static int     rkemac_ioctl(struct ifnet *, u_long, void *);
+static void    rkemac_watchdog(struct ifnet *);
+static void    rkemac_tick(void *);
+
+static int     rkemac_queue(struct rkemac_softc *, struct mbuf *);
+static void    rkemac_txdesc_sync(struct rkemac_softc *, int, int, int);
+static void    rkemac_txintr(struct rkemac_softc *);
+static void    rkemac_rxintr(struct rkemac_softc *);
+
+CFATTACH_DECL_NEW(rkemac, sizeof(struct rkemac_softc),
+       rkemac_match, rkemac_attach, NULL, NULL);
+
+#define EMAC_WRITE(sc, reg, val) \
+    bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
+#define EMAC_READ(sc, reg) \
+    bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
+
+static int
+rkemac_match(device_t parent, cfdata_t cf, void *aux)
+{
+       return 1;
+}
+
+static void
+rkemac_attach(device_t parent, device_t self, void *aux)
+{
+       struct rkemac_softc *sc = device_private(self);
+       struct obio_attach_args * const obio = aux;
+       prop_dictionary_t cfg = device_properties(self);
+       struct mii_data *mii = &sc->sc_mii;
+       struct ifnet *ifp = &sc->sc_ec.ec_if;
+       uint8_t enaddr[ETHER_ADDR_LEN];
+       prop_data_t ea;
+
+       sc->sc_dev = self;
+       sc->sc_bst = obio->obio_bst;
+       sc->sc_dmat = obio->obio_dmat;
+       bus_space_subregion(obio->obio_bst, obio->obio_bsh, obio->obio_offset,
+           obio->obio_size, &sc->sc_bsh);
+       mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
+
+       callout_init(&sc->sc_mii_tick, 0);
+       callout_setfunc(&sc->sc_mii_tick, rkemac_tick, sc);
+
+       aprint_naive("\n");
+       aprint_normal(": Ethernet controller\n");
+
+#ifdef RKEMAC_DEBUG
+       aprint_normal_dev(sc->sc_dev, "ID %#x\n",
+           EMAC_READ(sc, EMAC_ID_REG));
+#endif
+
+       sc->sc_ih = intr_establish(obio->obio_intr, IPL_NET,
+           IST_LEVEL, rkemac_intr, sc);
+       if (sc->sc_ih == NULL) {
+               aprint_error_dev(self, "couldn't establish interrupt\n");
+               return;
+       }
+
+       if ((ea = prop_dictionary_get(cfg, "mac-address")) != NULL) {
+               KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
+               KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
+               memcpy(enaddr, prop_data_data_nocopy(ea), ETHER_ADDR_LEN);
+       } else {
+               uint32_t addrl, addrh;
+
+               addrl = EMAC_READ(sc, EMAC_ADDRL_REG);
+               addrh = EMAC_READ(sc, EMAC_ADDRH_REG);
+
+               if (addrl == 0 && addrh == 0) {
+                       /* fake MAC address */
+                       addrl = 0x00f2 | (cprng_strong32() << 16);
+                       addrh = cprng_strong32();
+               }
+
+               enaddr[0] = addrl & 0xff;
+               enaddr[1] = (addrl >> 8) & 0xff;
+               enaddr[2] = (addrl >> 16) & 0xff;
+               enaddr[3] = (addrl >> 24) & 0xff;
+               enaddr[4] = addrh & 0xff;
+               enaddr[5] = (addrh >> 8) & 0xff;
+       }
+
+       aprint_normal_dev(sc->sc_dev, "Ethernet address: %s\n",
+           ether_sprintf(enaddr));
+
+       EMAC_WRITE(sc, EMAC_CONTROL_REG, 0);
+       EMAC_WRITE(sc, EMAC_ENABLE_REG, 0);
+       EMAC_WRITE(sc, EMAC_STAT_REG, EMAC_READ(sc, EMAC_STAT_REG));
+
+       rkemac_dma_init(sc);
+
+       ifp->if_softc = sc;
+       strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
+       ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
+       ifp->if_ioctl = rkemac_ioctl;
+       ifp->if_start = rkemac_start;
+       ifp->if_init = rkemac_init;
+       ifp->if_stop = rkemac_stop;
+       ifp->if_watchdog = rkemac_watchdog;
+       IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
+       IFQ_SET_READY(&ifp->if_snd);
+
+       sc->sc_ec.ec_mii = mii;
+       ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
+       mii->mii_ifp = ifp;
+       mii->mii_readreg = rkemac_mii_readreg;
+       mii->mii_writereg = rkemac_mii_writereg;
+       mii->mii_statchg = rkemac_mii_statchg;
+       mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
+
+       if (LIST_EMPTY(&mii->mii_phys)) {
+               aprint_error_dev(sc->sc_dev, "no PHY found!\n");
+               return;
+       }
+
+       ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
+
+       if_attach(ifp);
+       ether_ifattach(ifp, enaddr);
+
+       EMAC_WRITE(sc, EMAC_ENABLE_REG, RKEMAC_ENABLE_INTR);
+}
+
+static int
+rkemac_dma_init(struct rkemac_softc *sc)
+{
+       size_t descsize = RKEMAC_RX_RING_COUNT * sizeof(struct rkemac_rxdesc) +
+                         RKEMAC_TX_RING_COUNT * sizeof(struct rkemac_txdesc);
+       bus_addr_t physaddr;
+       int error, nsegs;
+       void *descs;
+
+       /*
+        * Allocate TX / RX descriptors
+        */
+       error = bus_dmamap_create(sc->sc_dmat, descsize, 1, descsize, 0,
+           BUS_DMA_NOWAIT, &sc->sc_ring_dmamap);
+       if (error)
+               return error;
+       error = bus_dmamem_alloc(sc->sc_dmat, descsize, 8, 0,



Home | Main Index | Thread Index | Old Index