[3/6] add ti cpsw driver file part 3

yao0718 29171383 at qq.com
Tue Jul 5 07:30:42 UTC 2016


+
+/*
+ *
+ * Init/Shutdown.
+ *
+ */
+
+static int
+cpsw_ports_down(struct cpsw_softc *sc)
+{
+	struct cpswp_softc *psc;
+	struct ifnet *ifp1, *ifp2;
+
+	if (!sc->dualemac)
+		return (1);
+	psc = device_get_softc(sc->port[0].dev);
+	ifp1 = psc->ifp;
+	psc = device_get_softc(sc->port[1].dev);
+	ifp2 = psc->ifp;
+	if ((ifp1->if_flags & IFF_UP) == 0 && (ifp2->if_flags & IFF_UP) == 0)
+		return (1);
+
+	return (0);
+}
+
+static void
+cpswp_init(void *arg)
+{
+	struct cpswp_softc *sc = arg;
+
+	CPSWP_DEBUGF(sc, (""));
+	CPSW_PORT_LOCK(sc);
+	cpswp_init_locked(arg);
+	CPSW_PORT_UNLOCK(sc);
+}
+
+static void
+cpswp_init_locked(void *arg)
+{
+	struct cpswp_softc *sc = arg;
+	struct ifnet *ifp;
+	uint32_t reg;
+
+	CPSWP_DEBUGF(sc, (""));
+	CPSW_PORT_LOCK_ASSERT(sc);
+	ifp = sc->ifp;
+	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
+		return;
+
+	getbinuptime(&sc->init_uptime);
+
+	if (!sc->swsc->rx.running && !sc->swsc->tx.running) {
+		/* Reset the controller. */
+		cpsw_reset(sc->swsc);
+		cpsw_init(sc->swsc);
+	}
+
+	/* Set Slave Mapping. */
+	cpsw_write_4(sc->swsc, CPSW_SL_RX_PRI_MAP(sc->unit), 0x76543210);
+	cpsw_write_4(sc->swsc, CPSW_PORT_P_TX_PRI_MAP(sc->unit + 1),
+	    0x33221100);
+	cpsw_write_4(sc->swsc, CPSW_SL_RX_MAXLEN(sc->unit), 0x5f2);
+	/* Enable MAC RX/TX modules. */
+	/* TODO: Docs claim that IFCTL_B and IFCTL_A do the same thing? */
+	/* Huh?  Docs call bit 0 "Loopback" some places, "FullDuplex" others. */
+	reg = cpsw_read_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit));
+	reg |= CPSW_SL_MACTL_GMII_ENABLE;
+	cpsw_write_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit), reg);
+
+	/* Initialize ALE: set port to forwarding(3), initialize addrs */
+	cpsw_write_4(sc->swsc, CPSW_ALE_PORTCTL(sc->unit + 1), 3);
+	cpswp_ale_update_addresses(sc, 1);
+
+	if (sc->swsc->dualemac) {
+		cpsw_write_4(sc->swsc, CPSW_PORT_P_VLAN(0),
+		    1 & 0xfff);
+		/* Set Port VID. */
+		cpsw_write_4(sc->swsc, CPSW_PORT_P_VLAN(sc->unit + 1),
+		    sc->vlan & 0xfff);
+		cpsw_ale_update_vlan_table(sc->swsc, sc->vlan,
+		    (1 << (sc->unit + 1)) | (1 << 0),
+		    (1 << (sc->unit + 1)) | (1 << 0));
+	}
+    
+	mii_mediachg(sc->mii);
+	callout_reset(&sc->mii_callout, hz, cpswp_tick, sc);
+	ifp->if_drv_flags |= IFF_DRV_RUNNING;
+	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
+}
+
+static int
+cpsw_shutdown(device_t dev)
+{
+	struct cpsw_softc *sc;
+	struct cpswp_softc *psc;
+	int i;
+
+ 	sc = device_get_softc(dev);
+	CPSW_DEBUGF(sc, (""));
+	for (i = 0; i < CPSW_PORTS; i++) {
+		if (!sc->dualemac && i != sc->active_slave)
+			continue;
+		psc = device_get_softc(sc->port[i].dev);
+		CPSW_PORT_LOCK(psc);
+		cpswp_stop_locked(psc);
+		CPSW_PORT_UNLOCK(psc);
+	}
+
+	return (0);
+}
+
+static void
+cpsw_rx_teardown_locked(struct cpsw_softc *sc)
+{
+	struct ifnet *ifp;
+	struct mbuf *received, *next;
+	int i = 0;
+
+	CPSW_DEBUGF(sc, ("starting RX teardown"));
+	cpsw_write_4(sc, CPSW_CPDMA_RX_TEARDOWN, 0);
+	for (;;) {
+		received = cpsw_rx_dequeue(sc);
+		CPSW_GLOBAL_UNLOCK(sc);
+		while (received != NULL) {
+			next = received->m_nextpkt;
+			received->m_nextpkt = NULL;		
+			ifp = received->m_pkthdr.rcvif;
+			(*ifp->if_input)(ifp, received);
+			received = next;
+		}
+		CPSW_GLOBAL_LOCK(sc);
+		if (!sc->rx.running) {
+			CPSW_DEBUGF(sc,
+			    ("finished RX teardown (%d retries)", i));
+			return;
+		}
+		if (++i > 10) {
+			device_printf(sc->dev,
+			    "Unable to cleanly shutdown receiver\n");
+			return;
+		}
+		DELAY(10);
+	}
+}
+
+static void
+cpsw_tx_teardown_locked(struct cpsw_softc *sc)
+{
+	int i = 0;
+
+	CPSW_DEBUGF(sc, ("starting TX teardown"));
+	cpsw_write_4(sc, CPSW_CPDMA_TX_TEARDOWN, 0);
+	cpsw_tx_dequeue(sc);
+	while (sc->tx.running && ++i < 10) {
+		DELAY(10);
+		cpsw_tx_dequeue(sc);
+	}
+	if (sc->tx.running) {
+		device_printf(sc->dev,
+		    "Unable to cleanly shutdown transmitter\n");
+	}
+	CPSW_DEBUGF(sc, ("finished TX teardown (%d retries, %d idle buffers)",
+	    i, sc->tx.active_queue_len));
+}
+
+static void
+cpswp_stop_locked(struct cpswp_softc *sc)
+{
+	struct ifnet *ifp;
+	uint32_t reg;
+
+	ifp = sc->ifp;
+	CPSWP_DEBUGF(sc, (""));
+	CPSW_PORT_LOCK_ASSERT(sc);
+
+	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
+		return;
+
+	/* Disable interface */
+	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
+	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
+
+	/* Stop ticker */
+	callout_stop(&sc->mii_callout);
+
+	/* Tear down the RX/TX queues. */
+	if (cpsw_ports_down(sc->swsc)) {
+		CPSW_GLOBAL_LOCK(sc->swsc);
+		cpsw_rx_teardown_locked(sc->swsc);
+		cpsw_tx_teardown_locked(sc->swsc);
+		CPSW_GLOBAL_UNLOCK(sc->swsc);
+	}
+
+	/* Stop MAC RX/TX modules. */
+	reg = cpsw_read_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit));
+	reg &= ~CPSW_SL_MACTL_GMII_ENABLE;
+	cpsw_write_4(sc->swsc, CPSW_SL_MACCONTROL(sc->unit), reg);
+
+	if (cpsw_ports_down(sc->swsc)) {
+		/* Capture stats before we reset controller. */
+#ifndef __rtems__
+		cpsw_stats_collect(sc->swsc);
+#endif
+		cpsw_reset(sc->swsc);
+		cpsw_init(sc->swsc);
+	}
+}
+
+/*
+ *  Suspend/Resume.
+ */
+
+static int
+cpsw_suspend(device_t dev)
+{
+	struct cpsw_softc *sc;
+	struct cpswp_softc *psc;
+	int i;
+
+	sc = device_get_softc(dev);
+	CPSW_DEBUGF(sc, (""));
+	for (i = 0; i < CPSW_PORTS; i++) {
+		if (!sc->dualemac && i != sc->active_slave)
+			continue;
+		psc = device_get_softc(sc->port[i].dev);
+		CPSW_PORT_LOCK(psc);
+		cpswp_stop_locked(psc);
+		CPSW_PORT_UNLOCK(psc);
+	}
+
+	return (0);
+}
+
+static int
+cpsw_resume(device_t dev)
+{
+	struct cpsw_softc *sc;
+
+	sc  = device_get_softc(dev);
+	CPSW_DEBUGF(sc, ("UNIMPLEMENTED"));
+
+	return (0);
+}
+
+/*
+ *
+ *  IOCTL
+ *
+ */
+
+static void
+cpsw_set_promisc(struct cpswp_softc *sc, int set)
+{
+	uint32_t reg;
+
+	/*
+	 * Enabling promiscuous mode requires ALE_BYPASS to be enabled.
+	 * That disables the ALE forwarding logic and causes every
+	 * packet to be sent only to the host port.  In bypass mode,
+	 * the ALE processes host port transmit packets the same as in
+	 * normal mode.
+	 */
+	reg = cpsw_read_4(sc->swsc, CPSW_ALE_CONTROL);
+	reg &= ~CPSW_ALE_CTL_BYPASS;
+	if (set)
+		reg |= CPSW_ALE_CTL_BYPASS;
+	cpsw_write_4(sc->swsc, CPSW_ALE_CONTROL, reg);
+}
+
+static void
+cpsw_set_allmulti(struct cpswp_softc *sc, int set)
+{
+	if (set) {
+		printf("All-multicast mode unimplemented\n");
+	}
+}
+
+static int
+cpswp_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
+{
+	struct cpswp_softc *sc;
+	struct ifreq *ifr;
+	int error;
+	uint32_t changed;
+
+	error = 0;
+	sc = ifp->if_softc;
+	ifr = (struct ifreq *)data;
+
+	switch (command) {
+	case SIOCSIFFLAGS:
+		CPSW_PORT_LOCK(sc);
+		if (ifp->if_flags & IFF_UP) {
+			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
+				changed = ifp->if_flags ^ sc->if_flags;
+				CPSWP_DEBUGF(sc,
+				    ("SIOCSIFFLAGS: UP & RUNNING (changed=0x%x)",
+				    changed));
+				if (changed & IFF_PROMISC)
+					cpsw_set_promisc(sc,
+					    ifp->if_flags & IFF_PROMISC);
+				if (changed & IFF_ALLMULTI)
+					cpsw_set_allmulti(sc,
+					    ifp->if_flags & IFF_ALLMULTI);
+			} else {
+				CPSWP_DEBUGF(sc,
+				    ("SIOCSIFFLAGS: UP but not RUNNING; starting up"));
+				cpswp_init_locked(sc);
+			}
+		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
+			CPSWP_DEBUGF(sc,
+			    ("SIOCSIFFLAGS: not UP but RUNNING; shutting down"));
+			cpswp_stop_locked(sc);
+		}
+
+		sc->if_flags = ifp->if_flags;
+		CPSW_PORT_UNLOCK(sc);
+		break;
+	case SIOCADDMULTI:
+		cpswp_ale_update_addresses(sc, 0);
+		break;
+	case SIOCDELMULTI:
+		/* Ugh.  DELMULTI doesn't provide the specific address
+		   being removed, so the best we can do is remove
+		   everything and rebuild it all. */
+		cpswp_ale_update_addresses(sc, 1);
+		break;
+	case SIOCGIFMEDIA:
+	case SIOCSIFMEDIA:
+		error = ifmedia_ioctl(ifp, ifr, &sc->mii->mii_media, command);
+		break;
+	default:
+		error = ether_ioctl(ifp, command, data);
+	}
+	return (error);
+}
+
+/*
+ *
+ * MIIBUS
+ *
+ */
+static int
+cpswp_miibus_ready(struct cpsw_softc *sc, uint32_t reg)
+{
+	uint32_t r, retries = CPSW_MIIBUS_RETRIES;
+
+	while (--retries) {
+		r = cpsw_read_4(sc, reg);
+		if ((r & MDIO_PHYACCESS_GO) == 0)
+			return (1);
+		DELAY(CPSW_MIIBUS_DELAY);
+	}
+
+	return (0);
+}
+
+static int
+cpswp_miibus_readreg(device_t dev, int phy, int reg)
+{
+	struct cpswp_softc *sc;
+	uint32_t cmd, r;
+
+	sc = device_get_softc(dev);
+	if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
+		device_printf(dev, "MDIO not ready to read\n");
+		return (0);
+	}
+
+	/* Set GO, reg, phy */
+	cmd = MDIO_PHYACCESS_GO | (reg & 0x1F) << 21 | (phy & 0x1F) << 16;
+	cpsw_write_4(sc->swsc, sc->phyaccess, cmd);
+
+	if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
+		device_printf(dev, "MDIO timed out during read\n");
+		return (0);
+	}
+
+	r = cpsw_read_4(sc->swsc, sc->phyaccess);
+	if ((r & MDIO_PHYACCESS_ACK) == 0) {
+		device_printf(dev, "Failed to read from PHY.\n");
+		r = 0;
+	}
+	return (r & 0xFFFF);
+}
+
+static int
+cpswp_miibus_writereg(device_t dev, int phy, int reg, int value)
+{
+	struct cpswp_softc *sc;
+	uint32_t cmd;
+
+	sc = device_get_softc(dev);
+	if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
+		device_printf(dev, "MDIO not ready to write\n");
+		return (0);
+	}
+
+	/* Set GO, WRITE, reg, phy, and value */
+	cmd = MDIO_PHYACCESS_GO | MDIO_PHYACCESS_WRITE |
+	    (reg & 0x1F) << 21 | (phy & 0x1F) << 16 | (value & 0xFFFF);
+	cpsw_write_4(sc->swsc, sc->phyaccess, cmd);
+
+	if (!cpswp_miibus_ready(sc->swsc, sc->phyaccess)) {
+		device_printf(dev, "MDIO timed out during write\n");
+		return (0);
+	}
+
+	if ((cpsw_read_4(sc->swsc, sc->phyaccess) & MDIO_PHYACCESS_ACK) == 0)
+		device_printf(dev, "Failed to write to PHY.\n");
+
+	return (0);
+}
+
+static void
+cpswp_miibus_statchg(device_t dev)
+{
+	struct cpswp_softc *sc;
+	uint32_t mac_control, reg;
+
+	sc = device_get_softc(dev);
+	CPSWP_DEBUGF(sc, (""));
+
+	reg = CPSW_SL_MACCONTROL(sc->unit);
+	mac_control = cpsw_read_4(sc->swsc, reg);
+	mac_control &= ~(CPSW_SL_MACTL_GIG | CPSW_SL_MACTL_IFCTL_A |
+	    CPSW_SL_MACTL_IFCTL_B | CPSW_SL_MACTL_FULLDUPLEX);
+
+	switch(IFM_SUBTYPE(sc->mii->mii_media_active)) {
+	case IFM_1000_SX:
+	case IFM_1000_LX:
+	case IFM_1000_CX:
+	case IFM_1000_T:
+		mac_control |= CPSW_SL_MACTL_GIG;
+		break;
+
+	case IFM_100_TX:
+		mac_control |= CPSW_SL_MACTL_IFCTL_A;
+		break;
+	}
+	if (sc->mii->mii_media_active & IFM_FDX)
+		mac_control |= CPSW_SL_MACTL_FULLDUPLEX;
+
+	cpsw_write_4(sc->swsc, reg, mac_control);
+}
+
+/*
+ *
+ * Transmit/Receive Packets.
+ *
+ */
+static void
+cpsw_intr_rx(void *arg)
+{
+	struct cpsw_softc *sc = arg;
+	struct ifnet *ifp;
+	struct mbuf *received, *next;
+     
+	//cpsw_ale_dump_table(sc);
+	CPSW_RX_LOCK(sc);
+	received = cpsw_rx_dequeue(sc);
+	cpsw_rx_enqueue(sc);
+	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, 1);
+	CPSW_RX_UNLOCK(sc);
+	
+	while (received != NULL) {
+		next = received->m_nextpkt;
+		received->m_nextpkt = NULL;
+		ifp = received->m_pkthdr.rcvif;
+		(*ifp->if_input)(ifp, received);
+		received = next;
+	}
+}
+
+static struct mbuf *
+cpsw_rx_dequeue(struct cpsw_softc *sc)
+{
+	struct cpsw_cpdma_bd bd;
+	struct cpsw_slot *slot;
+	struct cpswp_softc *psc;
+	struct mbuf *mb_head, *mb_tail;
+	int port, removed = 0;
+
+	mb_head = mb_tail = NULL;
+
+	/* Pull completed packets off hardware RX queue. */
+	while ((slot = STAILQ_FIRST(&sc->rx.active)) != NULL) {
+		cpsw_cpdma_read_bd(sc, slot, &bd);
+		if (bd.flags & CPDMA_BD_OWNER)
+			break; /* Still in use by hardware */
+
+		CPSW_DEBUGF(sc, ("Removing received packet from RX queue"));
+		++removed;
+		STAILQ_REMOVE_HEAD(&sc->rx.active, next);
+		STAILQ_INSERT_TAIL(&sc->rx.avail, slot, next);
+#ifndef __rtems__
+		bus_dmamap_sync(sc->mbuf_dtag, slot->dmamap, BUS_DMASYNC_POSTREAD);
+		bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
+#else
+        rtems_cache_invalidate_multiple_data_lines(slot->mbuf->m_data, slot->mbuf->m_len);       
+#endif
+		if (bd.flags & CPDMA_BD_TDOWNCMPLT) {
+			CPSW_DEBUGF(sc, ("RX teardown in progress"));
+			m_freem(slot->mbuf);
+			slot->mbuf = NULL;
+			cpsw_write_cp(sc, &sc->rx, 0xfffffffc);
+			sc->rx.running = 0;
+			break;
+		}
+
+		cpsw_write_cp_slot(sc, &sc->rx, slot);
+
+		port = (bd.flags & CPDMA_BD_PORT_MASK) - 1;
+		KASSERT(port >= 0 && port <= 1,
+		    ("patcket received with invalid port: %d", port));
+		psc = device_get_softc(sc->port[port].dev);
+
+		/* Set up mbuf */
+		/* TODO: track SOP/EOP bits to assemble a full mbuf
+		   out of received fragments. */
+		slot->mbuf->m_data += bd.bufoff;
+		slot->mbuf->m_len = bd.pktlen - 4;
+		slot->mbuf->m_pkthdr.len = bd.pktlen - 4;
+		slot->mbuf->m_flags |= M_PKTHDR;
+		slot->mbuf->m_pkthdr.rcvif = psc->ifp;
+		slot->mbuf->m_nextpkt = NULL;
+
+		if ((psc->ifp->if_capenable & IFCAP_RXCSUM) != 0) {
+			/* check for valid CRC by looking into pkt_err[5:4] */
+			if ((bd.flags & CPDMA_BD_PKT_ERR_MASK) == 0) {
+				slot->mbuf->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
+				slot->mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
+				slot->mbuf->m_pkthdr.csum_data = 0xffff;
+			}
+		}
+
+		/* Add mbuf to packet list to be returned. */
+		if (mb_tail) {
+			mb_tail->m_nextpkt = slot->mbuf;
+		} else {
+			mb_head = slot->mbuf;
+		}
+		mb_tail = slot->mbuf;
+		slot->mbuf = NULL;
+	}
+
+	if (removed != 0) {
+		sc->rx.queue_removes += removed;
+		sc->rx.active_queue_len -= removed;
+		sc->rx.avail_queue_len += removed;
+		if (sc->rx.avail_queue_len > sc->rx.max_avail_queue_len)
+			sc->rx.max_avail_queue_len = sc->rx.avail_queue_len;
+	}
+	return (mb_head);
+}
+
+static void
+cpsw_rx_enqueue(struct cpsw_softc *sc)
+{
+	bus_dma_segment_t seg[1];
+	struct cpsw_cpdma_bd bd;
+	struct cpsw_slots tmpqueue = STAILQ_HEAD_INITIALIZER(tmpqueue);
+	struct cpsw_slot *slot, *prev_slot = NULL;
+	struct cpsw_slot *last_old_slot, *first_new_slot;
+	int error, nsegs, added = 0;
+
+	/* Register new mbufs with hardware. */
+	while ((slot = STAILQ_FIRST(&sc->rx.avail)) != NULL) {
+		if (slot->mbuf == NULL) {
+			slot->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
+			if (slot->mbuf == NULL) {
+				device_printf(sc->dev,
+				    "Unable to fill RX queue\n");
+				break;
+			}
+			slot->mbuf->m_len =
+			    slot->mbuf->m_pkthdr.len =
+			    slot->mbuf->m_ext.ext_size;
+		}
+#ifndef __rtems__
+		error = bus_dmamap_load_mbuf_sg(sc->mbuf_dtag, slot->dmamap,
+		    slot->mbuf, seg, &nsegs, BUS_DMA_NOWAIT);
+
+		KASSERT(nsegs == 1, ("More than one segment (nsegs=%d)", nsegs));
+		KASSERT(error == 0, ("DMA error (error=%d)", error));
+		if (error != 0 || nsegs != 1) {
+			device_printf(sc->dev,
+			    "%s: Can't prep RX buf for DMA (nsegs=%d, error=%d)\n",
+			    __func__, nsegs, error);
+			bus_dmamap_unload(sc->mbuf_dtag, slot->dmamap);
+			m_freem(slot->mbuf);
+			slot->mbuf = NULL;
+			break;
+		}
+#endif
+#ifndef __rtems__
+		bus_dmamap_sync(sc->mbuf_dtag, slot->dmamap, BUS_DMASYNC_PREREAD);
+#else
+        rtems_cache_invalidate_multiple_data_lines(slot->mbuf->m_data, slot->mbuf->m_len);
+                seg->ds_addr = mtod(slot->mbuf, bus_addr_t);     
+#endif
+		/* Create and submit new rx descriptor*/
+		bd.next = 0;
+		bd.bufptr = seg->ds_addr;
+		bd.bufoff = 0;
+		bd.buflen = MCLBYTES - 1;
+		bd.pktlen = bd.buflen;
+		bd.flags = CPDMA_BD_OWNER;
+		cpsw_cpdma_write_bd(sc, slot, &bd);
+		++added;
+
+		if (prev_slot != NULL)
+			cpsw_cpdma_write_bd_next(sc, prev_slot, slot);
+		prev_slot = slot;
+		STAILQ_REMOVE_HEAD(&sc->rx.avail, next);
+		sc->rx.avail_queue_len--;
+		STAILQ_INSERT_TAIL(&tmpqueue, slot, next);
+	}
+
+	if (added == 0)
+		return;
+
+	CPSW_DEBUGF(sc, ("Adding %d buffers to RX queue", added));
+
+	/* Link new entries to hardware RX queue. */
+	last_old_slot = STAILQ_LAST(&sc->rx.active, cpsw_slot, next);
+	first_new_slot = STAILQ_FIRST(&tmpqueue);
+	STAILQ_CONCAT(&sc->rx.active, &tmpqueue);
+	if (first_new_slot == NULL) {
+		return;
+	} else if (last_old_slot == NULL) {
+		/* Start a fresh queue. */
+		cpsw_write_hdp_slot(sc, &sc->rx, first_new_slot);
+	} else {
+		/* Add buffers to end of current queue. */
+		cpsw_cpdma_write_bd_next(sc, last_old_slot, first_new_slot);
+		/* If underrun, restart queue. */
+		if (cpsw_cpdma_read_bd_flags(sc, last_old_slot) & CPDMA_BD_EOQ) {
+			cpsw_write_hdp_slot(sc, &sc->rx, first_new_slot);
+		}
+	}
+	sc->rx.queue_adds += added;
+	sc->rx.active_queue_len += added;
+	if (sc->rx.active_queue_len > sc->rx.max_active_queue_len) {
+		sc->rx.max_active_queue_len = sc->rx.active_queue_len;
+	}
+}
+
+static void
+cpswp_start(struct ifnet *ifp)
+{
+	struct cpswp_softc *sc = ifp->if_softc;
+
+	CPSW_TX_LOCK(sc->swsc);
+	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && sc->swsc->tx.running) {
+		cpswp_tx_enqueue(sc);
+		cpsw_tx_dequeue(sc->swsc);
+	}
+	//cpsw_ale_dump_table(sc->swsc);
+	CPSW_TX_UNLOCK(sc->swsc);
+}





------------------ Original ------------------
From:  "yao0718";<29171383 at qq.com>;
Date:  Tue, Jul 5, 2016 03:17 PM
To:  "devel"<devel at rtems.org>; 

Subject:  Re:  [3/6] add ti cpsw driver file part 1



diff -ruN ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpsw.c ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpsw.c
--- ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpsw.c	1970-01-01 08:00:00.000000000 +0800
+++ ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpsw.c	2016-07-05 09:22:41.794327800 +0800
@@ -0,0 +1,2772 @@
+#include <machine/rtems-bsd-kernel-space.h>
+/*-
+ * Copyright (c) 2012 Damjan Marion <dmarion at Freebsd.org>
+ * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ */
+
+/*
+ * TI Common Platform Ethernet Switch (CPSW) Driver
+ * Found in TI8148 "DaVinci" and AM335x "Sitara" SoCs.
+ *
+ * This controller is documented in the AM335x Technical Reference
+ * Manual, in the TMS320DM814x DaVinci Digital Video Processors TRM
+ * and in the TMS320C6452 3 Port Switch Ethernet Subsystem TRM.
+ *
+ * It is basically a single Ethernet port (port 0) wired internally to
+ * a 3-port store-and-forward switch connected to two independent
+ * "sliver" controllers (port 1 and port 2).  You can operate the
+ * controller in a variety of different ways by suitably configuring
+ * the slivers and the Address Lookup Engine (ALE) that routes packets
+ * between the ports.
+ *
+ * This code was developed and tested on a BeagleBone with
+ * an AM335x SoC.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <rtems/bsd/sys/param.h>
+#include <sys/systm.h>
+#include <sys/endian.h>
+#include <sys/mbuf.h>
+#ifndef __rtems__
+#include <sys/lock.h>
+#else
+#include <rtems/bsd/sys/lock.h>
+#endif
+#include <sys/mutex.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/socket.h>
+#include <sys/sysctl.h>
+
+#include <net/ethernet.h>
+#include <net/bpf.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <net/if_dl.h>
+#include <net/if_media.h>
+#include <net/if_types.h>
+#include <net/if_var.h>
+#include <net/if_vlan_var.h>
+
+#include <netinet/in_systm.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+
+#include <sys/sockio.h>
+#include <sys/bus.h>
+#include <machine/bus.h>
+#include <sys/rman.h>
+#include <machine/resource.h>
+
+#include <dev/mii/mii.h>
+#include <dev/mii/miivar.h>
+#ifndef __rtems__
+#include <dev/fdt/fdt_common.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+#include "if_cpswreg.h"
+#include "if_cpswvar.h"
+#ifndef __rtems__ 
+#include <arm/ti/ti_scm.h>
+#endif
+#include <rtems/bsd/local/miibus_if.h>
+#include <sys/malloc.h>
+
+/* Device probe/attach/detach. */
+static int cpsw_probe(device_t);
+static void cpsw_init_slots(struct cpsw_softc *);
+static int cpsw_attach(device_t);
+static void cpsw_free_slot(struct cpsw_softc *, struct cpsw_slot *);
+static int cpsw_detach(device_t);
+
+/* Device probe/attach/detach. */
+static int cpswp_probe(device_t);
+static int cpswp_attach(device_t);
+static int cpswp_detach(device_t);
+#ifndef __rtems__ 
+static phandle_t cpsw_get_node(device_t, device_t);
+#endif
+/* Device Init/shutdown. */
+static int cpsw_shutdown(device_t);
+static void cpswp_init(void *);
+static void cpswp_init_locked(void *);
+static void cpswp_stop_locked(struct cpswp_softc *);
+
+/* Device Suspend/Resume. */
+static int cpsw_suspend(device_t);
+static int cpsw_resume(device_t);
+
+/* Ioctl. */
+static int cpswp_ioctl(struct ifnet *, u_long command, caddr_t data);
+
+static int cpswp_miibus_readreg(device_t, int phy, int reg);
+static int cpswp_miibus_writereg(device_t, int phy, int reg, int value);
+static void cpswp_miibus_statchg(device_t);
+
+/* Send/Receive packets. */
+static void cpsw_intr_rx(void *arg);
+static struct mbuf *cpsw_rx_dequeue(struct cpsw_softc *);
+static void cpsw_rx_enqueue(struct cpsw_softc *);
+static void cpswp_start(struct ifnet *);
+static void cpswp_tx_enqueue(struct cpswp_softc *);
+static int cpsw_tx_dequeue(struct cpsw_softc *);
+
+/* Misc interrupts and watchdog. */
+static void cpsw_intr_rx_thresh(void *);
+static void cpsw_intr_misc(void *);
+static void cpswp_tick(void *);
+static void cpswp_ifmedia_sts(struct ifnet *, struct ifmediareq *);
+static int cpswp_ifmedia_upd(struct ifnet *);
+static void cpsw_tx_watchdog(void *);
+
+/* ALE support */
+static void cpsw_ale_read_entry(struct cpsw_softc *, uint16_t, uint32_t *);
+static void cpsw_ale_write_entry(struct cpsw_softc *, uint16_t, uint32_t *);
+static int cpsw_ale_mc_entry_set(struct cpsw_softc *, uint8_t, int, uint8_t *);
+static void cpsw_ale_dump_table(struct cpsw_softc *);
+static int cpsw_ale_update_vlan_table(struct cpsw_softc *, int, int, int);
+static int cpswp_ale_update_addresses(struct cpswp_softc *, int);
+
+/* Statistics and sysctls. */
+#ifndef __rtems__
+static void cpsw_add_sysctls(struct cpsw_softc *);
+static void cpsw_stats_collect(struct cpsw_softc *);
+static int cpsw_stats_sysctl(SYSCTL_HANDLER_ARGS);
+#endif
+
+/*
+ * Arbitrary limit on number of segments in an mbuf to be transmitted.
+ * Packets with more segments than this will be defragmented before
+ * they are queued.
+ */
+#define	CPSW_TXFRAGS		8
+
+/* Shared resources. */
+static device_method_t cpsw_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,		cpsw_probe),
+	DEVMETHOD(device_attach,	cpsw_attach),
+	DEVMETHOD(device_detach,	cpsw_detach),
+	DEVMETHOD(device_shutdown,	cpsw_shutdown),
+	DEVMETHOD(device_suspend,	cpsw_suspend),
+	DEVMETHOD(device_resume,	cpsw_resume),
+	/* OFW methods */
+#ifndef __rtems__
+	DEVMETHOD(ofw_bus_get_node,	cpsw_get_node),
+#endif	
+	DEVMETHOD_END
+};
+
+static driver_t cpsw_driver = {
+	"cpsw",
+	cpsw_methods,
+	sizeof(struct cpsw_softc),
+};
+
+static devclass_t cpsw_devclass;
+
+#ifndef __rtems__
+DRIVER_MODULE(cpsw, simplebus, cpsw_driver, cpsw_devclass, 0, 0);
+#else
+DRIVER_MODULE(cpsw, nexus, cpsw_driver, cpsw_devclass, 0, 0);
+#endif
+/* Port/Slave resources. */
+static device_method_t cpswp_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,		cpswp_probe),
+	DEVMETHOD(device_attach,	cpswp_attach),
+	DEVMETHOD(device_detach,	cpswp_detach),
+	/* MII interface */
+	DEVMETHOD(miibus_readreg,	cpswp_miibus_readreg),
+	DEVMETHOD(miibus_writereg,	cpswp_miibus_writereg),
+	DEVMETHOD(miibus_statchg,	cpswp_miibus_statchg),
+	DEVMETHOD_END
+};
+
+static driver_t cpswp_driver = {
+	"cpswp",
+	cpswp_methods,
+	sizeof(struct cpswp_softc),
+};
+
+static devclass_t cpswp_devclass;
+
+DRIVER_MODULE(cpswp, cpsw, cpswp_driver, cpswp_devclass, 0, 0);
+DRIVER_MODULE(miibus, cpswp, miibus_driver, miibus_devclass, 0, 0);
+MODULE_DEPEND(cpsw, ether, 1, 1, 1);
+MODULE_DEPEND(cpswp, miibus, 1, 1, 1);
+
+static uint32_t slave_mdio_addr[] = { 0x4a100200, 0x4a100300 };
+
+static struct resource_spec irq_res_spec[] = {
+#ifdef __rtems__
+	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
+#endif
+	{ SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
+	{ SYS_RES_IRQ, 1, RF_ACTIVE | RF_SHAREABLE },
+	{ SYS_RES_IRQ, 2, RF_ACTIVE | RF_SHAREABLE },
+	{ SYS_RES_IRQ, 3, RF_ACTIVE | RF_SHAREABLE },
+	{ -1, 0 }
+};
+
+/* Number of entries here must match size of stats
+ * array in struct cpswp_softc. */
+static struct cpsw_stat {
+	int	reg;
+	char *oid;
+} cpsw_stat_sysctls[CPSW_SYSCTL_COUNT] = {
+	{0x00, "GoodRxFrames"},
+	{0x04, "BroadcastRxFrames"},
+	{0x08, "MulticastRxFrames"},
+	{0x0C, "PauseRxFrames"},
+	{0x10, "RxCrcErrors"},
+	{0x14, "RxAlignErrors"},
+	{0x18, "OversizeRxFrames"},
+	{0x1c, "RxJabbers"},
+	{0x20, "ShortRxFrames"},
+	{0x24, "RxFragments"},
+	{0x30, "RxOctets"},
+	{0x34, "GoodTxFrames"},
+	{0x38, "BroadcastTxFrames"},
+	{0x3c, "MulticastTxFrames"},
+	{0x40, "PauseTxFrames"},
+	{0x44, "DeferredTxFrames"},
+	{0x48, "CollisionsTxFrames"},
+	{0x4c, "SingleCollisionTxFrames"},
+	{0x50, "MultipleCollisionTxFrames"},
+	{0x54, "ExcessiveCollisions"},
+	{0x58, "LateCollisions"},
+	{0x5c, "TxUnderrun"},
+	{0x60, "CarrierSenseErrors"},
+	{0x64, "TxOctets"},
+	{0x68, "RxTx64OctetFrames"},
+	{0x6c, "RxTx65to127OctetFrames"},
+	{0x70, "RxTx128to255OctetFrames"},
+	{0x74, "RxTx256to511OctetFrames"},
+	{0x78, "RxTx512to1024OctetFrames"},
+	{0x7c, "RxTx1024upOctetFrames"},
+	{0x80, "NetOctets"},
+	{0x84, "RxStartOfFrameOverruns"},
+	{0x88, "RxMiddleOfFrameOverruns"},
+	{0x8c, "RxDmaOverruns"}
+};
+
+/*
+ * Basic debug support.
+ */
+
+#define	IF_DEBUG(_sc)		if ((_sc)->if_flags & IFF_DEBUG)
+
+static void
+cpsw_debugf_head(const char *funcname)
+{
+	int t = (int)(time_second % (24 * 60 * 60));
+
+	printf("%02d:%02d:%02d %s ", t / (60 * 60), (t / 60) % 60, t % 60, funcname);
+}
+
+#include <machine/stdarg.h>
+static void
+cpsw_debugf(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	vprintf(fmt, ap);
+	va_end(ap);
+	printf("\n");
+
+}
+
+#define	CPSW_DEBUGF(_sc, a) do {					\
+	if (sc->debug) {						\
+		cpsw_debugf_head(__func__);				\
+		cpsw_debugf a;						\
+	}								\
+} while (0)
+
+#define	CPSWP_DEBUGF(_sc, a) do {					\
+	IF_DEBUG((_sc)) {						\
+		cpsw_debugf_head(__func__);				\
+		cpsw_debugf a;						\
+	}								\
+} while (0)
+
+
+/*
+ * Locking macros
+ */
+#define	CPSW_TX_LOCK(sc) do {						\
+		mtx_assert(&(sc)->rx.lock, MA_NOTOWNED);		\
+		mtx_lock(&(sc)->tx.lock);				\
+} while (0)
+
+#define	CPSW_TX_UNLOCK(sc)	mtx_unlock(&(sc)->tx.lock)
+#define	CPSW_TX_LOCK_ASSERT(sc)	mtx_assert(&(sc)->tx.lock, MA_OWNED)
+
+#define	CPSW_RX_LOCK(sc) do {						\
+		mtx_assert(&(sc)->tx.lock, MA_NOTOWNED);		\
+		mtx_lock(&(sc)->rx.lock);				\
+} while (0)
+
+#define	CPSW_RX_UNLOCK(sc)		mtx_unlock(&(sc)->rx.lock)
+#define	CPSW_RX_LOCK_ASSERT(sc)	mtx_assert(&(sc)->rx.lock, MA_OWNED)
+
+#define	CPSW_GLOBAL_LOCK(sc) do {					\
+		if ((mtx_owned(&(sc)->tx.lock) ? 1 : 0) !=		\
+		    (mtx_owned(&(sc)->rx.lock) ? 1 : 0)) {		\
+			panic("cpsw deadlock possibility detection!");	\
+		}							\
+		mtx_lock(&(sc)->tx.lock);				\
+		mtx_lock(&(sc)->rx.lock);				\
+} while (0)
+
+#define	CPSW_GLOBAL_UNLOCK(sc) do {					\
+		CPSW_RX_UNLOCK(sc);					\
+		CPSW_TX_UNLOCK(sc);					\
+} while (0)
+
+#define	CPSW_GLOBAL_LOCK_ASSERT(sc) do {				\
+		CPSW_TX_LOCK_ASSERT(sc);				\
+		CPSW_RX_LOCK_ASSERT(sc);				\
+} while (0)
+
+#define CPSW_PORT_LOCK(_sc) do {					\
+		mtx_assert(&(_sc)->lock, MA_NOTOWNED);			\
+		mtx_lock(&(_sc)->lock);					\
+} while (0)
+
+#define	CPSW_PORT_UNLOCK(_sc)	mtx_unlock(&(_sc)->lock)
+#define	CPSW_PORT_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->lock, MA_OWNED)
+
+/*
+ * Read/Write macros
+ */
+#define	cpsw_read_4(_sc, _reg)		bus_read_4((_sc)->irq_res[0], (_reg))
+#define	cpsw_write_4(_sc, _reg, _val)					\
+	bus_write_4((_sc)->irq_res[0], (_reg), (_val))
+
+#define BUS_SPACE_PHYSADDR(res, offs) \
+        ((u_int)(rman_get_start(res)+(offs)))
+#define CONTROL_MOD_BASE 0x44E10000
+#define CPSW_GMII_SEL			         (0x0650)
+/* GMII_SEL Register Field */
+#define GMIISEL_RMII2_IO_CLK_EN	             (1UL<<(7))
+#define GMIISEL_RMII1_IO_CLK_EN	             (1UL<<(6))
+#define GMIISEL_RGMII2_IDMODE	             (1UL<<(5))
+#define GMIISEL_RGMII1_IDMODE	             (1UL<<(4))
+#define GMIISEL_GMII2_SEL(val)	             ((0x3 & (val)) << 2)
+#define GMIISEL_GMII1_SEL(val)	             ((0x3 & (val)) << 0)
+#define GMII_MODE	                              0
+#define RMII_MODE	                              1
+#define RGMII_MODE	                          2
+
+
+#define cm_read(a)    (*(volatile uint32_t *)(a))
+#define cm_write(a,v) (*(volatile uint32_t *)(a) = (v))
+
+
+
+
+
+
+#define	cpsw_cpdma_bd_offset(i)	(CPSW_CPPI_RAM_OFFSET + ((i)*16))
+
+#define	cpsw_cpdma_bd_paddr(sc, slot)					\
+	BUS_SPACE_PHYSADDR(sc->irq_res[0], slot->bd_offset)
+#define	cpsw_cpdma_read_bd(sc, slot, val)				\
+	bus_read_region_4(sc->irq_res[0], slot->bd_offset, (uint32_t *) val, 4)
+#define	cpsw_cpdma_write_bd(sc, slot, val)				\
+	bus_write_region_4(sc->irq_res[0], slot->bd_offset, (uint32_t *) val, 4)
+#define	cpsw_cpdma_write_bd_next(sc, slot, next_slot)			\
+	cpsw_write_4(sc, slot->bd_offset, cpsw_cpdma_bd_paddr(sc, next_slot))
+#define	cpsw_cpdma_read_bd_flags(sc, slot)				\
+	bus_read_2(sc->irq_res[0], slot->bd_offset + 14)
+#define	cpsw_write_hdp_slot(sc, queue, slot)				\
+	cpsw_write_4(sc, (queue)->hdp_offset, cpsw_cpdma_bd_paddr(sc, slot))
+#define	CP_OFFSET (CPSW_CPDMA_TX_CP(0) - CPSW_CPDMA_TX_HDP(0))
+#define	cpsw_read_cp(sc, queue)						\
+	cpsw_read_4(sc, (queue)->hdp_offset + CP_OFFSET) 
+#define	cpsw_write_cp(sc, queue, val)					\
+	cpsw_write_4(sc, (queue)->hdp_offset + CP_OFFSET, (val))
+#define	cpsw_write_cp_slot(sc, queue, slot)				\
+	cpsw_write_cp(sc, queue, cpsw_cpdma_bd_paddr(sc, slot))
+
+#if 0
+/* XXX temporary function versions for debugging. */
+static void
+cpsw_write_hdp_slotX(struct cpsw_softc *sc, struct cpsw_queue *queue, struct cpsw_slot *slot)
+{
+	uint32_t reg = queue->hdp_offset;
+	uint32_t v = cpsw_cpdma_bd_paddr(sc, slot);
+	CPSW_DEBUGF(("HDP <=== 0x%08x (was 0x%08x)", v, cpsw_read_4(sc, reg)));
+	cpsw_write_4(sc, reg, v);
+}
+static void
+cpsw_write_cp_slotX(struct cpsw_softc *sc, struct cpsw_queue *queue, struct cpsw_slot *slot)
+{
+	uint32_t v = cpsw_cpdma_bd_paddr(sc, slot);
+	CPSW_DEBUGF(("CP <=== 0x%08x (expecting 0x%08x)", v, cpsw_read_cp(sc, queue)));
+	cpsw_write_cp(sc, queue, v);
+}
+#endif
+
+/*
+ * Expanded dump routines for verbose debugging.
+ */
+static void
+cpsw_dump_slot(struct cpsw_softc *sc, struct cpsw_slot *slot)
+{
+	static const char *flags[] = {"SOP", "EOP", "Owner", "EOQ",
+	    "TDownCmplt", "PassCRC", "Long", "Short", "MacCtl", "Overrun",
+	    "PktErr1", "PortEn/PktErr0", "RxVlanEncap", "Port2", "Port1",
+	    "Port0"};
+	struct cpsw_cpdma_bd bd;
+	const char *sep;
+	int i;
+
+	cpsw_cpdma_read_bd(sc, slot, &bd);
+	printf("BD Addr: 0x%08x   Next: 0x%08x\n", cpsw_cpdma_bd_paddr(sc, slot), bd.next);
+	printf("  BufPtr: 0x%08x   BufLen: 0x%08x\n", bd.bufptr, bd.buflen);
+	printf("  BufOff: 0x%08x   PktLen: 0x%08x\n", bd.bufoff, bd.pktlen);
+	printf("  Flags: ");
+	sep = "";
+	for (i = 0; i < 16; ++i) {
+		if (bd.flags & (1 << (15 - i))) {
+			printf("%s%s", sep, flags[i]);
+			sep = ",";
+		}
+	}
+	printf("\n");
+	if (slot->mbuf) {
+		printf("  Ether:  %14D\n",
+		    (char *)(slot->mbuf->m_data), " ");
+		printf("  Packet: %16D\n",
+		    (char *)(slot->mbuf->m_data) + 14, " ");
+	}
+}
+
+#define	CPSW_DUMP_SLOT(cs, slot) do {				\
+	IF_DEBUG(sc) {						\
+		cpsw_dump_slot(sc, slot);			\
+	}							\
+} while (0)
+
+static void
+cpsw_dump_queue(struct cpsw_softc *sc, struct cpsw_slots *q)
+{
+	struct cpsw_slot *slot;
+	int i = 0;
+	int others = 0;
+
+	STAILQ_FOREACH(slot, q, next) {
+		if (i > 4)
+			++others;
+		else
+			cpsw_dump_slot(sc, slot);
+		++i;
+	}
+	if (others)
+		printf(" ... and %d more.\n", others);
+	printf("\n");
+}





------------------ Original ------------------
From:  "yao0718";<29171383 at qq.com>;
Date:  Tue, Jul 5, 2016 03:15 PM
To:  "devel"<devel at rtems.org>; 

Subject:  Re: [2/6] add ti cpsw head file



diff -ruN ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswreg.h ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswreg.h
--- ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswreg.h	1970-01-01 08:00:00.000000000 +0800
+++ ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswreg.h	2016-03-18 11:38:45.324075400 +0800
@@ -0,0 +1,178 @@
+/*-
+ * Copyright (c) 2012 Damjan Marion <dmarion at Freebsd.org>
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef	_IF_CPSWREG_H
+#define	_IF_CPSWREG_H
+
+#define	CPSW_SS_OFFSET			0x0000
+#define	CPSW_SS_IDVER			(CPSW_SS_OFFSET + 0x00)
+#define	CPSW_SS_SOFT_RESET		(CPSW_SS_OFFSET + 0x08)
+#define	CPSW_SS_STAT_PORT_EN		(CPSW_SS_OFFSET + 0x0C)
+#define	CPSW_SS_PTYPE			(CPSW_SS_OFFSET + 0x10)
+#define	CPSW_SS_FLOW_CONTROL		(CPSW_SS_OFFSET + 0x24)
+
+#define	CPSW_PORT_OFFSET		0x0100
+#define	CPSW_PORT_P_MAX_BLKS(p)		(CPSW_PORT_OFFSET + 0x08 + ((p) * 0x100))
+#define	CPSW_PORT_P_BLK_CNT(p)		(CPSW_PORT_OFFSET + 0x0C + ((p) * 0x100))
+#define	CPSW_PORT_P_VLAN(p)		(CPSW_PORT_OFFSET + 0x14 + ((p) * 0x100))
+#define	CPSW_PORT_P_TX_PRI_MAP(p)	(CPSW_PORT_OFFSET + 0x118 + ((p-1) * 0x100))
+#define	CPSW_PORT_P0_CPDMA_TX_PRI_MAP	(CPSW_PORT_OFFSET + 0x01C)
+#define	CPSW_PORT_P0_CPDMA_RX_CH_MAP	(CPSW_PORT_OFFSET + 0x020)
+#define	CPSW_PORT_P_SA_LO(p)		(CPSW_PORT_OFFSET + 0x120 + ((p-1) * 0x100))
+#define	CPSW_PORT_P_SA_HI(p)		(CPSW_PORT_OFFSET + 0x124 + ((p-1) * 0x100))
+
+#define	CPSW_CPDMA_OFFSET		0x0800
+#define	CPSW_CPDMA_TX_CONTROL		(CPSW_CPDMA_OFFSET + 0x04)
+#define	CPSW_CPDMA_TX_TEARDOWN		(CPSW_CPDMA_OFFSET + 0x08)
+#define	CPSW_CPDMA_RX_CONTROL		(CPSW_CPDMA_OFFSET + 0x14)
+#define	CPSW_CPDMA_RX_TEARDOWN		(CPSW_CPDMA_OFFSET + 0x18)
+#define	CPSW_CPDMA_SOFT_RESET		(CPSW_CPDMA_OFFSET + 0x1c)
+#define	CPSW_CPDMA_DMACONTROL		(CPSW_CPDMA_OFFSET + 0x20)
+#define	CPSW_CPDMA_DMASTATUS		(CPSW_CPDMA_OFFSET + 0x24)
+#define	CPSW_CPDMA_RX_BUFFER_OFFSET	(CPSW_CPDMA_OFFSET + 0x28)
+#define	CPSW_CPDMA_TX_INTSTAT_RAW	(CPSW_CPDMA_OFFSET + 0x80)
+#define	CPSW_CPDMA_TX_INTSTAT_MASKED	(CPSW_CPDMA_OFFSET + 0x84)
+#define	CPSW_CPDMA_TX_INTMASK_SET	(CPSW_CPDMA_OFFSET + 0x88)
+#define	CPSW_CPDMA_TX_INTMASK_CLEAR	(CPSW_CPDMA_OFFSET + 0x8C)
+#define	CPSW_CPDMA_CPDMA_EOI_VECTOR	(CPSW_CPDMA_OFFSET + 0x94)
+#define	CPSW_CPDMA_RX_INTSTAT_RAW	(CPSW_CPDMA_OFFSET + 0xA0)
+#define	CPSW_CPDMA_RX_INTSTAT_MASKED	(CPSW_CPDMA_OFFSET + 0xA4)
+#define	CPSW_CPDMA_RX_INTMASK_SET	(CPSW_CPDMA_OFFSET + 0xA8)
+#define	CPSW_CPDMA_RX_INTMASK_CLEAR	(CPSW_CPDMA_OFFSET + 0xAc)
+#define	CPSW_CPDMA_DMA_INTSTAT_RAW	(CPSW_CPDMA_OFFSET + 0xB0)
+#define	CPSW_CPDMA_DMA_INTSTAT_MASKED	(CPSW_CPDMA_OFFSET + 0xB4)
+#define	CPSW_CPDMA_DMA_INTMASK_SET	(CPSW_CPDMA_OFFSET + 0xB8)
+#define	CPSW_CPDMA_DMA_INTMASK_CLEAR	(CPSW_CPDMA_OFFSET + 0xBC)
+#define	CPSW_CPDMA_RX_FREEBUFFER(p)	(CPSW_CPDMA_OFFSET + 0x0e0 + ((p) * 0x04))
+
+#define	CPSW_STATS_OFFSET		0x0900
+
+#define	CPSW_STATERAM_OFFSET		0x0A00
+#define	CPSW_CPDMA_TX_HDP(p)		(CPSW_STATERAM_OFFSET + 0x00 + ((p) * 0x04))
+#define	CPSW_CPDMA_RX_HDP(p)		(CPSW_STATERAM_OFFSET + 0x20 + ((p) * 0x04))
+#define	CPSW_CPDMA_TX_CP(p)		(CPSW_STATERAM_OFFSET + 0x40 + ((p) * 0x04))
+#define	CPSW_CPDMA_RX_CP(p)		(CPSW_STATERAM_OFFSET + 0x60 + ((p) * 0x04))
+
+#define	CPSW_CPTS_OFFSET		0x0C00
+
+#define	CPSW_ALE_OFFSET			0x0D00
+#define	CPSW_ALE_CONTROL		(CPSW_ALE_OFFSET + 0x08)
+#define	 CPSW_ALE_CTL_ENABLE		(1U << 31)
+#define	 CPSW_ALE_CTL_CLEAR_TBL		(1 << 30)
+#define	 CPSW_ALE_CTL_BYPASS		(1 << 4)
+#define	 CPSW_ALE_CTL_VLAN_AWARE	(1 << 2)
+#define	CPSW_ALE_TBLCTL			(CPSW_ALE_OFFSET + 0x20)
+#define	CPSW_ALE_TBLW2			(CPSW_ALE_OFFSET + 0x34)
+#define	CPSW_ALE_TBLW1			(CPSW_ALE_OFFSET + 0x38)
+#define	CPSW_ALE_TBLW0			(CPSW_ALE_OFFSET + 0x3C)
+#define	 ALE_MCAST(_a)			((_a[1] >> 8) & 1)
+#define	 ALE_MCAST_FWD			(3 << 30)
+#define	 ALE_PORTS(_a)			((_a[2] >> 2) & 7)
+#define	 ALE_TYPE(_a)			((_a[1] >> 28) & 3)
+#define	 ALE_TYPE_ADDR			1
+#define	 ALE_TYPE_VLAN			2
+#define	 ALE_TYPE_VLAN_ADDR		3
+#define	 ALE_VLAN(_a)			((_a[1] >> 16) & 0xfff)
+#define	 ALE_VLAN_REGFLOOD(_a)		((_a[0] >> 8) & 7)
+#define	 ALE_VLAN_UNREGFLOOD(_a)	((_a[0] >> 16) & 7)
+#define	 ALE_VLAN_UNTAG(_a)		((_a[0] >> 24) & 7)
+#define	 ALE_VLAN_MEMBERS(_a)		(_a[0] & 7)
+#define	CPSW_ALE_PORTCTL(p)		(CPSW_ALE_OFFSET + 0x40 + ((p) * 0x04))
+
+/* SL1 is at 0x0D80, SL2 is at 0x0DC0 */
+#define	CPSW_SL_OFFSET			0x0D80
+#define	CPSW_SL_MACCONTROL(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x04)
+#define	 CPSW_SL_MACTL_IFCTL_B		(1 << 16)
+#define	 CPSW_SL_MACTL_IFCTL_A		(1 << 15)
+#define	 CPSW_SL_MACTL_GIG		(1 << 7)
+#define	 CPSW_SL_MACTL_GMII_ENABLE	(1 << 5)
+#define	 CPSW_SL_MACTL_FULLDUPLEX	(1 << 0)
+#define	CPSW_SL_MACSTATUS(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x08)
+#define	CPSW_SL_SOFT_RESET(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x0C)
+#define	CPSW_SL_RX_MAXLEN(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x10)
+#define	CPSW_SL_RX_PAUSE(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x18)
+#define	CPSW_SL_TX_PAUSE(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x1C)
+#define	CPSW_SL_RX_PRI_MAP(p)		(CPSW_SL_OFFSET + (0x40 * (p)) + 0x24)
+
+#define	MDIO_OFFSET			0x1000
+#define	MDIOCONTROL			(MDIO_OFFSET + 0x04)
+#define	 MDIOCTL_ENABLE			(1 << 30)
+#define	 MDIOCTL_FAULTENB		(1 << 18)
+#define	MDIOLINKINTRAW			(MDIO_OFFSET + 0x10)
+#define	MDIOLINKINTMASKED		(MDIO_OFFSET + 0x14)
+#define	MDIOUSERACCESS0			(MDIO_OFFSET + 0x80)
+#define	MDIOUSERPHYSEL0			(MDIO_OFFSET + 0x84)
+#define	MDIOUSERACCESS1			(MDIO_OFFSET + 0x88)
+#define	MDIOUSERPHYSEL1			(MDIO_OFFSET + 0x8C)
+#define	 MDIO_PHYSEL_LINKINTENB		(1 << 6)
+#define	 MDIO_PHYACCESS_GO		(1U << 31)
+#define	 MDIO_PHYACCESS_WRITE		(1 << 30)
+#define	 MDIO_PHYACCESS_ACK		(1 << 29)
+
+#define	CPSW_WR_OFFSET			0x1200
+#define	CPSW_WR_SOFT_RESET		(CPSW_WR_OFFSET + 0x04)
+#define	CPSW_WR_CONTROL			(CPSW_WR_OFFSET + 0x08)
+#define	CPSW_WR_INT_CONTROL		(CPSW_WR_OFFSET + 0x0c)
+#define	CPSW_WR_C_RX_THRESH_EN(p)	(CPSW_WR_OFFSET + (0x10 * (p)) + 0x10)
+#define	CPSW_WR_C_RX_EN(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x14)
+#define	CPSW_WR_C_TX_EN(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x18)
+#define	CPSW_WR_C_MISC_EN(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x1C)
+#define	CPSW_WR_C_RX_THRESH_STAT(p)	(CPSW_WR_OFFSET + (0x10 * (p)) + 0x40)
+#define	CPSW_WR_C_RX_STAT(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x44)
+#define	CPSW_WR_C_TX_STAT(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x48)
+#define	CPSW_WR_C_MISC_STAT(p)		(CPSW_WR_OFFSET + (0x10 * (p)) + 0x4C)
+#define	 CPSW_WR_C_MISC_EVNT_PEND	(1 << 4)
+#define	 CPSW_WR_C_MISC_STAT_PEND	(1 << 3)
+#define	 CPSW_WR_C_MISC_HOST_PEND	(1 << 2)
+#define	 CPSW_WR_C_MISC_MDIOLINK	(1 << 1)
+#define	 CPSW_WR_C_MISC_MDIOUSER	(1 << 0)
+
+#define	CPSW_CPPI_RAM_OFFSET		0x2000
+#define	CPSW_CPPI_RAM_SIZE		0x2000
+
+#define	CPSW_MEMWINDOW_SIZE		0x4000
+
+#define	 CPDMA_BD_SOP			(1 << 15)
+#define	 CPDMA_BD_EOP			(1 << 14)
+#define	 CPDMA_BD_OWNER			(1 << 13)
+#define	 CPDMA_BD_EOQ			(1 << 12)
+#define	 CPDMA_BD_TDOWNCMPLT		(1 << 11)
+#define	 CPDMA_BD_PKT_ERR_MASK		(3 << 4)
+#define	 CPDMA_BD_TO_PORT		(1 << 4)
+#define	 CPDMA_BD_PORT_MASK		3
+
+struct cpsw_cpdma_bd {
+	volatile uint32_t next;
+	volatile uint32_t bufptr;
+	volatile uint16_t buflen;
+	volatile uint16_t bufoff;
+	volatile uint16_t pktlen;
+	volatile uint16_t flags;
+};
+
+#endif /*_IF_CPSWREG_H */


diff -ruN ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswvar.h ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswvar.h
--- ./rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswvar.h	1970-01-01 08:00:00.000000000 +0800
+++ ./am335x_bsp/rtems-libbsd/freebsd/sys/arm/ti/cpsw/if_cpswvar.h	2016-07-02 21:43:26.205209800 +0800
@@ -0,0 +1,147 @@
+/*-
+ * Copyright (c) 2012 Damjan Marion <dmarion at Freebsd.org>
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef	_IF_CPSWVAR_H
+#define	_IF_CPSWVAR_H
+
+#define	CPSW_PORTS		2
+#define	CPSW_INTR_COUNT		4
+
+/* MII BUS  */
+#define	CPSW_MIIBUS_RETRIES	10
+#define	CPSW_MIIBUS_DELAY	2000
+
+#define	CPSW_MAX_ALE_ENTRIES	1024
+
+#define	CPSW_SYSCTL_COUNT	34
+
+#define CPSW_RESEMBLE_BUFFER_LEN  2048
+#define CPSW_RESEMBLE_BUFFER_ALIGN 4
+struct cpsw_slot {
+	uint32_t bd_offset;  /* Offset of corresponding BD within CPPI RAM. */
+	bus_dmamap_t dmamap;
+	struct mbuf *mbuf;
+/* yao0718 add  why cppi dma can not send when address misalign 32 bits*/
+    uint8_t  *resemble;
+	uint8_t  *align_res;
+	STAILQ_ENTRY(cpsw_slot) next;
+};
+STAILQ_HEAD(cpsw_slots, cpsw_slot);
+
+struct cpsw_queue {
+	struct mtx	lock;
+	int		running;
+	struct cpsw_slots active;
+	struct cpsw_slots avail;
+	uint32_t	queue_adds; /* total bufs added */
+	uint32_t	queue_removes; /* total bufs removed */
+	uint32_t	queue_removes_at_last_tick; /* Used by watchdog */
+	int		queue_slots;
+	int		active_queue_len;
+	int		max_active_queue_len;
+	int		avail_queue_len;
+	int		max_avail_queue_len;
+	int		longest_chain; /* Largest # segments in a single packet. */
+	int		hdp_offset;
+};
+
+struct cpsw_port {
+	device_t	dev;
+	int		phy;
+	int		vlan;
+};
+
+struct cpsw_softc {
+	device_t	dev;
+	int		active_slave;
+	int		debug;
+	int		dualemac;
+#ifndef __rtems__
+	phandle_t	node;
+#endif
+	struct bintime	attach_uptime; /* system uptime when attach happened. */
+	struct cpsw_port port[2];
+
+	/* RX and TX buffer tracking */
+	struct cpsw_queue rx, tx;
+
+	/* We expect 1 memory resource and 4 interrupts from the device tree. */
+	void		*ih_cookie[CPSW_INTR_COUNT];
+#ifndef __rtems__
+	int		mem_rid;
+	struct resource	*mem_res;
+	struct resource	*irq_res[CPSW_INTR_COUNT];
+#else
+    struct resource	*irq_res[1+CPSW_INTR_COUNT];
+#endif
+	/* An mbuf full of nulls for TX padding. */
+	bus_dmamap_t null_mbuf_dmamap;
+	struct mbuf *null_mbuf;
+	bus_addr_t null_mbuf_paddr;
+
+	bus_dma_tag_t	mbuf_dtag;
+
+	struct {
+		int resets;
+		int timer;
+		struct callout  callout;
+	} watchdog;
+
+	/* 64-bit versions of 32-bit hardware statistics counters */
+	uint64_t shadow_stats[CPSW_SYSCTL_COUNT];
+
+	/* CPPI STATERAM has 512 slots for building TX/RX queues. */
+	/* TODO: Size here supposedly varies with different versions
+	   of the controller.  Check DaVinci specs and find a good
+	   way to adjust this.  One option is to have a separate
+	   Device Tree parameter for number slots; another option
+	   is to calculate it from the memory size in the device tree. */
+	struct cpsw_slot _slots[CPSW_CPPI_RAM_SIZE / sizeof(struct cpsw_cpdma_bd)];
+	struct cpsw_slots avail;
+};
+
+struct cpswp_softc {
+	device_t	dev;
+	device_t	miibus;
+	device_t	pdev;
+	int		media_status;
+	int		unit;
+	int		vlan;
+	struct bintime	init_uptime; /* system uptime when init happened. */
+	struct callout	mii_callout;
+	struct cpsw_softc *swsc;
+	struct ifnet	*ifp;
+	struct mii_data	*mii;
+	struct mtx	lock;
+	uint32_t	if_flags;
+	uint32_t	phy;
+	uint32_t	phyaccess;
+	uint32_t	physel;
+};
+
+#endif /*_IF_CPSWVAR_H */





------------------ Original ------------------
From:  "yao0718";<29171383 at qq.com>;
Date:  Tue, Jul 5, 2016 03:11 PM
To:  "devel"<devel at rtems.org>; 

Subject:  [0/6] ti cpsw driver port from freebsd for am335x 



I add cpsw driver from freebsd and modify some code for my board, my board is not beagleblack, so i am not sure it can work fine in BB board,to reduce phy find process, I  set phy address 4 and 6 for which phy address on my board,my board has two eth port ,I set dualmac when attach;
can somebody test it on beaglebone and merge to beaglebone  bsp?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20160705/4dd7be78/attachment-0002.html>


More information about the devel mailing list