summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/mii
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2015-03-27 11:13:25 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2015-04-01 09:15:29 +0200
commit710d2a1e1d2da5e7d0b3fabfe0c8aaa127c47507 (patch)
treec0ac270405d7d0c2305a6d476930746f2375b4f4 /freebsd/sys/dev/mii
parentFix maxid and maxcpus sysctls (diff)
downloadrtems-libbsd-710d2a1e1d2da5e7d0b3fabfe0c8aaa127c47507.tar.bz2
mii: Add phy devices
Diffstat (limited to 'freebsd/sys/dev/mii')
-rw-r--r--freebsd/sys/dev/mii/micphy.c226
-rw-r--r--freebsd/sys/dev/mii/ukphy.c160
-rw-r--r--freebsd/sys/dev/mii/ukphy_subr.c131
3 files changed, 517 insertions, 0 deletions
diff --git a/freebsd/sys/dev/mii/micphy.c b/freebsd/sys/dev/mii/micphy.c
new file mode 100644
index 00000000..77e123c5
--- /dev/null
+++ b/freebsd/sys/dev/mii/micphy.c
@@ -0,0 +1,226 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
+/*-
+ * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
+ * All rights reserved.
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * 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.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Micrel KSZ9021 Gigabit Ethernet Transceiver
+ */
+
+#include <rtems/bsd/sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/socket.h>
+#include <sys/errno.h>
+#include <sys/module.h>
+#include <sys/bus.h>
+#include <sys/malloc.h>
+
+#include <machine/bus.h>
+
+#include <net/if.h>
+#include <net/if_media.h>
+
+#include <dev/mii/mii.h>
+#include <dev/mii/miivar.h>
+#include <rtems/bsd/local/miidevs.h>
+
+#include <rtems/bsd/local/miibus_if.h>
+
+#ifndef __rtems__
+#include <dev/fdt/fdt_common.h>
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif /* __rtems__ */
+
+#define MII_KSZPHY_EXTREG 0x0b
+#define KSZPHY_EXTREG_WRITE (1 << 15)
+#define MII_KSZPHY_EXTREG_WRITE 0x0c
+#define MII_KSZPHY_EXTREG_READ 0x0d
+#define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
+#define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
+#define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
+
+#define PS_TO_REG(p) ((p) / 200)
+
+static int micphy_probe(device_t);
+static int micphy_attach(device_t);
+static int micphy_service(struct mii_softc *, struct mii_data *, int);
+
+static device_method_t micphy_methods[] = {
+ /* device interface */
+ DEVMETHOD(device_probe, micphy_probe),
+ DEVMETHOD(device_attach, micphy_attach),
+ DEVMETHOD(device_detach, mii_phy_detach),
+ DEVMETHOD(device_shutdown, bus_generic_shutdown),
+ DEVMETHOD_END
+};
+
+static devclass_t micphy_devclass;
+
+static driver_t micphy_driver = {
+ "micphy",
+ micphy_methods,
+ sizeof(struct mii_softc)
+};
+
+DRIVER_MODULE(micphy, miibus, micphy_driver, micphy_devclass, 0, 0);
+
+static const struct mii_phydesc micphys[] = {
+ MII_PHY_DESC(MICREL, KSZ9021),
+ MII_PHY_END
+};
+
+static const struct mii_phy_funcs micphy_funcs = {
+ micphy_service,
+ ukphy_status,
+ mii_phy_reset
+};
+
+static void
+micphy_write(struct mii_softc *sc, uint32_t reg, uint32_t val)
+{
+
+ PHY_WRITE(sc, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | reg);
+ PHY_WRITE(sc, MII_KSZPHY_EXTREG_WRITE, val);
+}
+
+#ifndef __rtems__
+static int
+ksz9021_load_values(struct mii_softc *sc, phandle_t node, uint32_t reg,
+ char *field1, char *field2,
+ char *field3, char *field4)
+{
+ pcell_t dts_value[1];
+ int len;
+ int val;
+
+ val = 0;
+
+ if ((len = OF_getproplen(node, field1)) > 0) {
+ OF_getencprop(node, field1, dts_value, len);
+ val = PS_TO_REG(dts_value[0]);
+ }
+
+ if ((len = OF_getproplen(node, field2)) > 0) {
+ OF_getencprop(node, field2, dts_value, len);
+ val |= PS_TO_REG(dts_value[0]) << 4;
+ }
+
+ if ((len = OF_getproplen(node, field3)) > 0) {
+ OF_getencprop(node, field3, dts_value, len);
+ val |= PS_TO_REG(dts_value[0]) << 8;
+ }
+
+ if ((len = OF_getproplen(node, field4)) > 0) {
+ OF_getencprop(node, field4, dts_value, len);
+ val |= PS_TO_REG(dts_value[0]) << 12;
+ }
+
+ micphy_write(sc, reg, val);
+
+ return (0);
+}
+#endif /* __rtems__ */
+
+static int
+micphy_probe(device_t dev)
+{
+
+ return (mii_phy_dev_probe(dev, micphys, BUS_PROBE_DEFAULT));
+}
+
+static int
+micphy_attach(device_t dev)
+{
+ struct mii_softc *sc;
+#ifndef __rtems__
+ phandle_t node;
+ device_t miibus;
+ device_t parent;
+#endif /* __rtems__ */
+
+ sc = device_get_softc(dev);
+
+ mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &micphy_funcs, 1);
+ mii_phy_setmedia(sc);
+
+#ifndef __rtems__
+ miibus = device_get_parent(dev);
+ parent = device_get_parent(miibus);
+
+ if ((node = ofw_bus_get_node(parent)) == -1)
+ return (ENXIO);
+
+ ksz9021_load_values(sc, node, MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
+ "txen-skew-ps", "txc-skew-ps",
+ "rxdv-skew-ps", "rxc-skew-ps");
+
+ ksz9021_load_values(sc, node, MII_KSZPHY_RX_DATA_PAD_SKEW,
+ "rxd0-skew-ps", "rxd1-skew-ps",
+ "rxd2-skew-ps", "rxd3-skew-ps");
+
+ ksz9021_load_values(sc, node, MII_KSZPHY_TX_DATA_PAD_SKEW,
+ "txd0-skew-ps", "txd1-skew-ps",
+ "txd2-skew-ps", "txd3-skew-ps");
+#endif /* __rtems__ */
+
+ return (0);
+}
+
+static int
+micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
+{
+
+ switch (cmd) {
+ case MII_POLLSTAT:
+ break;
+
+ case MII_MEDIACHG:
+ mii_phy_setmedia(sc);
+ break;
+
+ case MII_TICK:
+ if (mii_phy_tick(sc) == EJUSTRETURN)
+ return (0);
+ break;
+ }
+
+ /* Update the media status. */
+ PHY_STATUS(sc);
+
+ /* Callback if something changed. */
+ mii_phy_update(sc, cmd);
+ return (0);
+}
diff --git a/freebsd/sys/dev/mii/ukphy.c b/freebsd/sys/dev/mii/ukphy.c
new file mode 100644
index 00000000..960c797f
--- /dev/null
+++ b/freebsd/sys/dev/mii/ukphy.c
@@ -0,0 +1,160 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
+/* $NetBSD: ukphy.c,v 1.2 1999/04/23 04:24:32 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, and by Frank van der Linden.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+/*-
+ * Copyright (c) 1997 Manuel Bouyer. 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 <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * driver for generic unknown PHYs
+ */
+
+#include <rtems/bsd/sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/socket.h>
+#include <sys/errno.h>
+#include <sys/module.h>
+#include <sys/bus.h>
+
+#include <net/if.h>
+#include <net/if_media.h>
+
+#include <dev/mii/mii.h>
+#include <dev/mii/miivar.h>
+
+#include <rtems/bsd/local/miibus_if.h>
+
+static int ukphy_probe(device_t);
+static int ukphy_attach(device_t);
+
+static device_method_t ukphy_methods[] = {
+ /* device interface */
+ DEVMETHOD(device_probe, ukphy_probe),
+ DEVMETHOD(device_attach, ukphy_attach),
+ DEVMETHOD(device_detach, mii_phy_detach),
+ DEVMETHOD(device_shutdown, bus_generic_shutdown),
+ DEVMETHOD_END
+};
+
+static devclass_t ukphy_devclass;
+
+static driver_t ukphy_driver = {
+ "ukphy",
+ ukphy_methods,
+ sizeof(struct mii_softc)
+};
+
+DRIVER_MODULE(ukphy, miibus, ukphy_driver, ukphy_devclass, 0, 0);
+
+static int ukphy_service(struct mii_softc *, struct mii_data *, int);
+
+static const struct mii_phy_funcs ukphy_funcs = {
+ ukphy_service,
+ ukphy_status,
+ mii_phy_reset
+};
+
+static int
+ukphy_probe(device_t dev)
+{
+
+ /*
+ * We know something is here, so always match at a low priority.
+ */
+ device_set_desc(dev, "Generic IEEE 802.3u media interface");
+ return (BUS_PROBE_GENERIC);
+}
+
+static int
+ukphy_attach(device_t dev)
+{
+ struct mii_softc *sc;
+
+ sc = device_get_softc(dev);
+
+ mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &ukphy_funcs, 1);
+ mii_phy_setmedia(sc);
+
+ return (0);
+}
+
+static int
+ukphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
+{
+
+ switch (cmd) {
+ case MII_POLLSTAT:
+ break;
+
+ case MII_MEDIACHG:
+ mii_phy_setmedia(sc);
+ break;
+
+ case MII_TICK:
+ if (mii_phy_tick(sc) == EJUSTRETURN)
+ return (0);
+ break;
+ }
+
+ /* Update the media status. */
+ PHY_STATUS(sc);
+
+ /* Callback if something changed. */
+ mii_phy_update(sc, cmd);
+ return (0);
+}
diff --git a/freebsd/sys/dev/mii/ukphy_subr.c b/freebsd/sys/dev/mii/ukphy_subr.c
new file mode 100644
index 00000000..b53ee861
--- /dev/null
+++ b/freebsd/sys/dev/mii/ukphy_subr.c
@@ -0,0 +1,131 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
+/* $NetBSD: ukphy_subr.c,v 1.2 1998/11/05 04:08:02 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, and by Frank van der Linden.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Subroutines shared by the ukphy driver and other PHY drivers.
+ */
+
+#include <rtems/bsd/sys/param.h>
+#include <sys/systm.h>
+#include <sys/socket.h>
+#include <sys/module.h>
+#include <sys/bus.h>
+
+#include <net/if.h>
+#include <net/if_media.h>
+
+#include <dev/mii/mii.h>
+#include <dev/mii/miivar.h>
+
+#include <rtems/bsd/local/miibus_if.h>
+
+/*
+ * Media status subroutine. If a PHY driver does media detection simply
+ * by decoding the NWay autonegotiation, use this routine.
+ */
+void
+ukphy_status(struct mii_softc *phy)
+{
+ struct mii_data *mii = phy->mii_pdata;
+ struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
+ int bmsr, bmcr, anlpar, gtcr, gtsr;
+
+ mii->mii_media_status = IFM_AVALID;
+ mii->mii_media_active = IFM_ETHER;
+
+ bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
+ if (bmsr & BMSR_LINK)
+ mii->mii_media_status |= IFM_ACTIVE;
+
+ bmcr = PHY_READ(phy, MII_BMCR);
+ if (bmcr & BMCR_ISO) {
+ mii->mii_media_active |= IFM_NONE;
+ mii->mii_media_status = 0;
+ return;
+ }
+
+ if (bmcr & BMCR_LOOP)
+ mii->mii_media_active |= IFM_LOOP;
+
+ if (bmcr & BMCR_AUTOEN) {
+ /*
+ * NWay autonegotiation takes the highest-order common
+ * bit of the ANAR and ANLPAR (i.e. best media advertised
+ * both by us and our link partner).
+ */
+ if ((bmsr & BMSR_ACOMP) == 0) {
+ /* Erg, still trying, I guess... */
+ mii->mii_media_active |= IFM_NONE;
+ return;
+ }
+
+ anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
+ if ((phy->mii_flags & MIIF_HAVE_GTCR) != 0 &&
+ (phy->mii_extcapabilities &
+ (EXTSR_1000THDX | EXTSR_1000TFDX)) != 0) {
+ gtcr = PHY_READ(phy, MII_100T2CR);
+ gtsr = PHY_READ(phy, MII_100T2SR);
+ } else
+ gtcr = gtsr = 0;
+
+ if ((gtcr & GTCR_ADV_1000TFDX) && (gtsr & GTSR_LP_1000TFDX))
+ mii->mii_media_active |= IFM_1000_T|IFM_FDX;
+ else if ((gtcr & GTCR_ADV_1000THDX) &&
+ (gtsr & GTSR_LP_1000THDX))
+ mii->mii_media_active |= IFM_1000_T|IFM_HDX;
+ else if (anlpar & ANLPAR_TX_FD)
+ mii->mii_media_active |= IFM_100_TX|IFM_FDX;
+ else if (anlpar & ANLPAR_T4)
+ mii->mii_media_active |= IFM_100_T4|IFM_HDX;
+ else if (anlpar & ANLPAR_TX)
+ mii->mii_media_active |= IFM_100_TX|IFM_HDX;
+ else if (anlpar & ANLPAR_10_FD)
+ mii->mii_media_active |= IFM_10_T|IFM_FDX;
+ else if (anlpar & ANLPAR_10)
+ mii->mii_media_active |= IFM_10_T|IFM_HDX;
+ else
+ mii->mii_media_active |= IFM_NONE;
+
+ if ((mii->mii_media_active & IFM_1000_T) != 0 &&
+ (gtsr & GTSR_MS_RES) != 0)
+ mii->mii_media_active |= IFM_ETH_MASTER;
+
+ if ((mii->mii_media_active & IFM_FDX) != 0)
+ mii->mii_media_active |= mii_phy_flowstatus(phy);
+ } else
+ mii->mii_media_active = ife->ifm_media;
+}