<div><br></div><div><div>I port these code from the master freebsd branch on github,when I add cpsw driver, the master freebsd cpsw driver was not complete, so some code add my myself.</div><div><br></div><div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------ Original ------------------</div><div style="font-size: 12px;background:#efefef;padding:8px;"><div><b>From: </b> "Gedare Bloom";<gedare@rtems.org>;</div><div><b>Date: </b> Wed, Jul 13, 2016 10:41 PM</div><div><b>To: </b> "yao0718"<29171383@qq.com>; <wbr></div><div><b>Cc: </b> "devel"<devel@rtems.org>; <wbr></div><div><b>Subject: </b> Re: [1/6]add ar8031 or ar 8035 phy support</div></div><div><br></div>I think this should be the checked-out version of specific freebsd,<br>with the populated $FreeBSD$ tag? See what other files are using.<br><br>On Tue, Jul 5, 2016 at 3:14 AM, yao0718 <29171383@qq.com> wrote:<br>> diff -ruN ./rtems-libbsd/freebsd/sys/dev/mii/atphy.c<br>> ./am335x_bsp/rtems-libbsd/freebsd/sys/dev/mii/atphy.c<br>> --- ./rtems-libbsd/freebsd/sys/dev/mii/atphy.c 1970-01-01 08:00:00.000000000<br>> +0800<br>> +++ ./am335x_bsp/rtems-libbsd/freebsd/sys/dev/mii/atphy.c 2016-07-05<br>> 10:57:44.540734400 +0800<br>> @@ -0,0 +1,378 @@<br>> +#include <machine/rtems-bsd-kernel-space.h><br>> +<br>> +/*-<br>> + * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org><br>> + * All rights reserved.<br>> + *<br>> + * Redistribution and use in source and binary forms, with or without<br>> + * modification, are permitted provided that the following conditions<br>> + * are met:<br>> + * 1. Redistributions of source code must retain the above copyright<br>> + *    notice unmodified, this list of conditions, and the following<br>> + *    disclaimer.<br>> + * 2. Redistributions in binary form must reproduce the above copyright<br>> + *    notice, this list of conditions and the following disclaimer in the<br>> + *    documentation and/or other materials provided with the distribution.<br>> + *<br>> + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND<br>> + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR<br>> PURPOSE<br>> + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE<br>> + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>> CONSEQUENTIAL<br>> + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>> + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>> + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,<br>> STRICT<br>> + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY<br>> WAY<br>> + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>> + * SUCH DAMAGE.<br>> + */<br>> +<br>> +#include <sys/cdefs.h><br>> +__FBSDID("$FreeBSD$");<br>> +<br>> +/*<br>> + * Driver for the Attansic/Atheros F1 10/100/1000 PHY.<br>> + */<br>> +<br>> +#include <sys/param.h><br>> +#include <sys/systm.h><br>> +#include <sys/kernel.h><br>> +#include <sys/module.h><br>> +#include <sys/socket.h><br>> +#include <sys/bus.h><br>> +<br>> +#include <net/if.h><br>> +#include <net/if_media.h><br>> +<br>> +#include <dev/mii/mii.h><br>> +#include <dev/mii/miivar.h><br>> +#include <rtems/bsd/local/miidevs.h><br>> +<br>> +#include <dev/mii/atphyreg.h><br>> +<br>> +#include <rtems/bsd/local/miibus_if.h><br>> +<br>> +static int atphy_probe(device_t);<br>> +static int atphy_attach(device_t);<br>> +<br>> +static device_method_t atphy_methods[] = {<br>> + /* Device interface. */<br>> + DEVMETHOD(device_probe, atphy_probe),<br>> + DEVMETHOD(device_attach, atphy_attach),<br>> + DEVMETHOD(device_detach, mii_phy_detach),<br>> + DEVMETHOD(device_shutdown, bus_generic_shutdown),<br>> + DEVMETHOD_END<br>> +};<br>> +<br>> +static devclass_t atphy_devclass;<br>> +static driver_t atphy_driver = {<br>> + "atphy",<br>> + atphy_methods,<br>> + sizeof(struct mii_softc)<br>> +};<br>> +<br>> +DRIVER_MODULE(atphy, miibus, atphy_driver, atphy_devclass, 0, 0);<br>> +<br>> +static int atphy_service(struct mii_softc *, struct mii_data *, int);<br>> +static void atphy_status(struct mii_softc *);<br>> +static void atphy_reset(struct mii_softc *);<br>> +static uint16_t atphy_anar(struct ifmedia_entry *);<br>> +static int atphy_setmedia(struct mii_softc *, int);<br>> +<br>> +static const struct mii_phydesc atphys[] = {<br>> + MII_PHY_DESC(xxATHEROS, F1),<br>> + MII_PHY_DESC(xxATHEROS, F1_7),<br>> + MII_PHY_DESC(xxATHEROS, AR8021),<br>> +/* MII_PHY_DESC(xxATHEROS, AR8035),*/<br>> + MII_PHY_DESC(xxATHEROS, F2),<br>> + MII_PHY_END<br>> +};<br>> +<br>> +static const struct mii_phy_funcs atphy_funcs = {<br>> + atphy_service,<br>> + atphy_status,<br>> + atphy_reset<br>> +};<br>> +<br>> +static int<br>> +atphy_probe(device_t dev)<br>> +{<br>> +<br>> + return (mii_phy_dev_probe(dev, atphys, BUS_PROBE_DEFAULT));<br>> +}<br>> +<br>> +static int<br>> +atphy_attach(device_t dev)<br>> +{<br>> +<br>> + mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &atphy_funcs, 1);<br>> + return (0);<br>> +}<br>> +<br>> +static int<br>> +atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)<br>> +{<br>> + struct ifmedia_entry *ife = mii->mii_media.ifm_cur;<br>> + uint16_t anar, bmcr, bmsr;<br>> +<br>> + switch (cmd) {<br>> + case MII_POLLSTAT:<br>> + break;<br>> +<br>> + case MII_MEDIACHG:<br>> + if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO ||<br>> +    IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {<br>> + atphy_setmedia(sc, ife->ifm_media);<br>> + break;<br>> + }<br>> +<br>> + bmcr = 0;<br>> + switch (IFM_SUBTYPE(ife->ifm_media)) {<br>> + case IFM_100_TX:<br>> + bmcr = BMCR_S100;<br>> + break;<br>> + case IFM_10_T:<br>> + bmcr = BMCR_S10;<br>> + break;<br>> + case IFM_NONE:<br>> + bmcr = PHY_READ(sc, MII_BMCR);<br>> + /*<br>> + * XXX<br>> + * Due to an unknown reason powering down PHY resulted<br>> + * in unexpected results such as inaccessibility of<br>> + * hardware of freshly rebooted system. Disable<br>> + * powering down PHY until I got more information for<br>> + * Attansic/Atheros PHY hardwares.<br>> + */<br>> + PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);<br>> + goto done;<br>> + default:<br>> + return (EINVAL);<br>> + }<br>> +<br>> + anar = atphy_anar(ife);<br>> + if ((ife->ifm_media & IFM_FDX) != 0) {<br>> + bmcr |= BMCR_FDX;<br>> + if ((ife->ifm_media & IFM_FLOW) != 0 ||<br>> +    (sc->mii_flags & MIIF_FORCEPAUSE) != 0)<br>> + anar |= ANAR_PAUSE_TOWARDS;<br>> + }<br>> +<br>> + if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |<br>> +    EXTSR_1000THDX)) != 0)<br>> + PHY_WRITE(sc, MII_100T2CR, 0);<br>> + PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA);<br>> +<br>> + /*<br>> + * Reset the PHY so all changes take effect.<br>> + */<br>> + PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET | BMCR_AUTOEN |<br>> +    BMCR_STARTNEG);<br>> +done:<br>> + break;<br>> +<br>> + case MII_TICK:<br>> + /*<br>> + * Only used for autonegotiation.<br>> + */<br>> + if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {<br>> + sc->mii_ticks = 0;<br>> + break;<br>> + }<br>> +<br>> + /*<br>> + * Check for link.<br>> + * Read the status register twice; BMSR_LINK is latch-low.<br>> + */<br>> + bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);<br>> + if (bmsr & BMSR_LINK) {<br>> + sc->mii_ticks = 0;<br>> + break;<br>> + }<br>> +<br>> + /* Announce link loss right after it happens. */<br>> + if (sc->mii_ticks++ == 0)<br>> + break;<br>> + if (sc->mii_ticks <= sc->mii_anegticks)<br>> + return (0);<br>> +<br>> + sc->mii_ticks = 0;<br>> + atphy_setmedia(sc, ife->ifm_media);<br>> + break;<br>> + }<br>> +<br>> + /* Update the media status. */<br>> + PHY_STATUS(sc);<br>> +<br>> + /* Callback if something changed. */<br>> + mii_phy_update(sc, cmd);<br>> + return (0);<br>> +}<br>> +<br>> +static void<br>> +atphy_status(struct mii_softc *sc)<br>> +{<br>> + struct mii_data *mii = sc->mii_pdata;<br>> + uint32_t bmsr, bmcr, ssr;<br>> +<br>> + mii->mii_media_status = IFM_AVALID;<br>> + mii->mii_media_active = IFM_ETHER;<br>> +<br>> + bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);<br>> + if ((bmsr & BMSR_LINK) != 0)<br>> + mii->mii_media_status |= IFM_ACTIVE;<br>> +<br>> + bmcr = PHY_READ(sc, MII_BMCR);<br>> + if ((bmcr & BMCR_ISO) != 0) {<br>> + mii->mii_media_active |= IFM_NONE;<br>> + mii->mii_media_status = 0;<br>> + return;<br>> + }<br>> +<br>> + if ((bmcr & BMCR_LOOP) != 0)<br>> + mii->mii_media_active |= IFM_LOOP;<br>> +<br>> + ssr = PHY_READ(sc, ATPHY_SSR);<br>> + if ((ssr & ATPHY_SSR_SPD_DPLX_RESOLVED) == 0) {<br>> + /* Erg, still trying, I guess... */<br>> + mii->mii_media_active |= IFM_NONE;<br>> + return;<br>> + }<br>> +<br>> + switch (ssr & ATPHY_SSR_SPEED_MASK) {<br>> + case ATPHY_SSR_1000MBS:<br>> + mii->mii_media_active |= IFM_1000_T;<br>> + /*<br>> + * atphy(4) has a valid link so reset mii_ticks.<br>> + * Resetting mii_ticks is needed in order to<br>> + * detect link loss after auto-negotiation.<br>> + */<br>> + sc->mii_ticks = 0;<br>> + break;<br>> + case ATPHY_SSR_100MBS:<br>> + mii->mii_media_active |= IFM_100_TX;<br>> + sc->mii_ticks = 0;<br>> + break;<br>> + case ATPHY_SSR_10MBS:<br>> + mii->mii_media_active |= IFM_10_T;<br>> + sc->mii_ticks = 0;<br>> + break;<br>> + default:<br>> + mii->mii_media_active |= IFM_NONE;<br>> + return;<br>> + }<br>> +<br>> + if ((ssr & ATPHY_SSR_DUPLEX) != 0)<br>> + mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);<br>> + else<br>> + mii->mii_media_active |= IFM_HDX;<br>> +<br>> + if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&<br>> +    (PHY_READ(sc, MII_100T2SR) & GTSR_MS_RES) != 0)<br>> + mii->mii_media_active |= IFM_ETH_MASTER;<br>> +}<br>> +<br>> +static void<br>> +atphy_reset(struct mii_softc *sc)<br>> +{<br>> + struct ifmedia_entry *ife = sc->mii_pdata->mii_media.ifm_cur;<br>> + uint32_t reg;<br>> + int i;<br>> + //enable rgmii internal delay<br>> + PHY_WRITE(sc, 0xd, 0x3);<br>> + PHY_WRITE(sc, 0xe, 0x805d);<br>> + PHY_WRITE(sc, 0xd, 0x4003);<br>> +<br>> + reg = PHY_READ(sc, 0xe);<br>> + PHY_WRITE(sc, 0xe, reg & ~(1 << 8));<br>> + PHY_WRITE(sc, 0x1d,5);<br>> + PHY_WRITE(sc, 0x1e, (1<<8));<br>> +<br>> +#if 1<br>> + reg = PHY_READ(sc, ATPHY_SCR);<br>> + /* Enable automatic crossover. */<br>> + reg |= ATPHY_SCR_AUTO_X_MODE;<br>> + /* Disable power down. */<br>> + reg &= ~ATPHY_SCR_MAC_PDOWN;<br>> + /* Enable CRS on Tx. */<br>> + reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;<br>> + /* Auto correction for reversed cable polarity. */<br>> + reg |= ATPHY_SCR_POLARITY_REVERSAL;<br>> + PHY_WRITE(sc, ATPHY_SCR, reg);<br>> +<br>> + /* Workaround F1 bug to reset phy. */<br>> + atphy_setmedia(sc, ife == NULL ? IFM_AUTO : ife->ifm_media);<br>> +<br>> + for (i = 0; i < 1000; i++) {<br>> + DELAY(1);<br>> + if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0)<br>> + break;<br>> + }<br>> +#endif<br>> +}<br>> +<br>> +static uint16_t<br>> +atphy_anar(struct ifmedia_entry *ife)<br>> +{<br>> + uint16_t anar;<br>> +<br>> + anar = 0;<br>> + switch (IFM_SUBTYPE(ife->ifm_media)) {<br>> + case IFM_AUTO:<br>> + anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;<br>> + return (anar);<br>> + case IFM_1000_T:<br>> + return (anar);<br>> + case IFM_100_TX:<br>> + anar |= ANAR_TX;<br>> + break;<br>> + case IFM_10_T:<br>> + anar |= ANAR_10;<br>> + break;<br>> + default:<br>> + return (0);<br>> + }<br>> +<br>> + if ((ife->ifm_media & IFM_FDX) != 0) {<br>> + if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)<br>> + anar |= ANAR_TX_FD;<br>> + else<br>> + anar |= ANAR_10_FD;<br>> + }<br>> +<br>> + return (anar);<br>> +}<br>> +<br>> +static int<br>> +atphy_setmedia(struct mii_softc *sc, int media)<br>> +{<br>> + uint16_t anar;<br>> +<br>> + anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;<br>> + if ((IFM_SUBTYPE(media) == IFM_AUTO || (media & IFM_FDX) != 0) &&<br>> +    ((media & IFM_FLOW) != 0 ||<br>> +    (sc->mii_flags & MIIF_FORCEPAUSE) != 0))<br>> + anar |= ANAR_PAUSE_TOWARDS;<br>> + PHY_WRITE(sc, MII_ANAR, anar);<br>> + if ((sc->mii_extcapabilities &<br>> +     (EXTSR_1000TFDX | EXTSR_1000THDX)) != 0)<br>> + PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |<br>> +    GTCR_ADV_1000THDX);<br>> + else if (sc->mii_mpd_model == MII_MODEL_xxATHEROS_F1) {<br>> + /*<br>> + * AR8132 has 10/100 PHY and the PHY uses the same<br>> + * model number of F1 gigabit PHY.  The PHY has no<br>> + * ability to establish gigabit link so explicitly<br>> + * disable 1000baseT configuration for the PHY.<br>> + * Otherwise, there is a case that atphy(4) could<br>> + * not establish a link against gigabit link partner<br>> + * unless the link partner supports down-shifting.<br>> + */<br>> + PHY_WRITE(sc, MII_100T2CR, 0);<br>> + }<br>> + PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);<br>> +<br>> +<br>> +<br>> + return (EJUSTRETURN);<br>> +}<br>> diff -ruN ./rtems-libbsd/freebsd/sys/dev/mii/atphyreg.h<br>> ./am335x_bsp/rtems-libbsd/freebsd/sys/dev/mii/atphyreg.h<br>> --- ./rtems-libbsd/freebsd/sys/dev/mii/atphyreg.h 1970-01-01<br>> 08:00:00.000000000 +0800<br>> +++ ./am335x_bsp/rtems-libbsd/freebsd/sys/dev/mii/atphyreg.h 2016-03-08<br>> 14:27:47.000000000 +0800<br>> @@ -0,0 +1,63 @@<br>> +/*-<br>> + * Copyright (c) 2008, Pyun YongHyeon<br>> + * All rights reserved.<br>> + *<br>> + * Redistribution and use in source and binary forms, with or without<br>> + * modification, are permitted provided that the following conditions<br>> + * are met:<br>> + * 1. Redistributions of source code must retain the above copyright<br>> + *    notice unmodified, this list of conditions, and the following<br>> + *    disclaimer.<br>> + * 2. Redistributions in binary form must reproduce the above copyright<br>> + *    notice, this list of conditions and the following disclaimer in the<br>> + *    documentation and/or other materials provided with the distribution.<br>> + *<br>> + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND<br>> + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR<br>> PURPOSE<br>> + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE<br>> + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>> CONSEQUENTIAL<br>> + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS<br>> + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br>> + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,<br>> STRICT<br>> + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY<br>> WAY<br>> + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>> + * SUCH DAMAGE.<br>> + *<br>> + * $FreeBSD$<br>> + */<br>> +<br>> +#ifndef _DEV_MII_ATPHYREG_H_<br>> +#define _DEV_MII_ATPHYREG_H_<br>> +<br>> +/*<br>> + * Registers for the Attansic/Atheros Gigabit PHY.<br>> + */<br>> +<br>> +/* Special Control Register */<br>> +#define ATPHY_SCR 0x10<br>> +#define ATPHY_SCR_JABBER_DISABLE 0x0001<br>> +#define ATPHY_SCR_POLARITY_REVERSAL 0x0002<br>> +#define ATPHY_SCR_SQE_TEST 0x0004<br>> +#define ATPHY_SCR_MAC_PDOWN 0x0008<br>> +#define ATPHY_SCR_CLK125_DISABLE 0x0010<br>> +#define ATPHY_SCR_MDI_MANUAL_MODE 0x0000<br>> +#define ATPHY_SCR_MDIX_MANUAL_MODE 0x0020<br>> +#define ATPHY_SCR_AUTO_X_1000T 0x0040<br>> +#define ATPHY_SCR_AUTO_X_MODE 0x0060<br>> +#define ATPHY_SCR_10BT_EXT_ENABLE 0x0080<br>> +#define ATPHY_SCR_MII_5BIT_ENABLE 0x0100<br>> +#define ATPHY_SCR_SCRAMBLER_DISABLE 0x0200<br>> +#define ATPHY_SCR_FORCE_LINK_GOOD 0x0400<br>> +#define ATPHY_SCR_ASSERT_CRS_ON_TX 0x0800<br>> +<br>> +/* Special Status Register. */<br>> +#define ATPHY_SSR 0x11<br>> +#define ATPHY_SSR_SPD_DPLX_RESOLVED 0x0800<br>> +#define ATPHY_SSR_DUPLEX 0x2000<br>> +#define ATPHY_SSR_SPEED_MASK 0xC000<br>> +#define ATPHY_SSR_10MBS 0x0000<br>> +#define ATPHY_SSR_100MBS 0x4000<br>> +#define ATPHY_SSR_1000MBS 0x8000<br>> +<br>> +#endif /* _DEV_MII_ATPHYREG_H_ */<br>><br>><br>><br>> ------------------ Original ------------------<br>> From:  "yao0718";<29171383@qq.com>;<br>> Date:  Tue, Jul 5, 2016 03:11 PM<br>> To:  "devel"<devel@rtems.org>;<br>> Subject:  [0/6] ti cpsw driver port from freebsd for am335x<br>><br>> I add cpsw driver from freebsd and modify some code for my board, my board<br>> is not beagleblack, so i am not sure it can work fine in BB board,to reduce<br>> phy find process, I  set phy address 4 and 6 for which phy address on my<br>> board,my board has two eth port ,I set dualmac when attach;<br>> can somebody test it on beaglebone and merge to beaglebone  bsp?<br>><br>><br>> _______________________________________________<br>> devel mailing list<br>> devel@rtems.org<br>> http://lists.rtems.org/mailman/listinfo/devel<br></div>