LCOV - code coverage report
Current view: top level - dev/mii - urlphy.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 90 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: urlphy.c,v 1.17 2015/03/14 03:38:48 jsg Exp $ */
       2             : /*      $NetBSD: urlphy.c,v 1.1 2002/03/28 21:07:53 ichiro Exp $        */
       3             : /*
       4             :  * Copyright (c) 2001, 2002
       5             :  *     Shingo WATANABE <nabe@nabechan.org>.  All rights reserved.
       6             :  *
       7             :  * Redistribution and use in source and binary forms, with or without
       8             :  * modification, are permitted provided that the following conditions
       9             :  * are met:
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  * 2. Redistributions in binary form must reproduce the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer in the
      14             :  *    documentation and/or other materials provided with the distribution.
      15             :  * 3. Neither the name of the author nor the names of any co-contributors
      16             :  *    may be used to endorse or promote products derived from this software
      17             :  *    without specific prior written permission.
      18             :  *
      19             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
      20             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      21             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      22             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      23             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      24             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      25             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      26             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      27             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      28             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      29             :  * SUCH DAMAGE.
      30             :  *
      31             :  */
      32             : 
      33             : /*
      34             :  * driver for Realtek RL8150L internal phy
      35             :  */
      36             : 
      37             : #include <sys/param.h>
      38             : #include <sys/systm.h>
      39             : #include <sys/kernel.h>
      40             : #include <sys/device.h>
      41             : #include <sys/socket.h>
      42             : 
      43             : #include <net/if.h>
      44             : #include <net/if_var.h>
      45             : #include <net/if_media.h>
      46             : 
      47             : #include <dev/mii/mii.h>
      48             : #include <dev/mii/miivar.h>
      49             : #include <dev/mii/urlphyreg.h>
      50             : 
      51             : #define URLPHY_DEBUG    0
      52             : #ifdef URLPHY_DEBUG
      53             : #define DPRINTF(x)      if (urlphydebug) printf x
      54             : #define DPRINTFN(n,x)   if (urlphydebug>(n)) printf x
      55             : int urlphydebug = URLPHY_DEBUG;
      56             : #else
      57             : #define DPRINTF(x)
      58             : #define DPRINTFN(n,x)
      59             : #endif
      60             : 
      61             : int urlphy_match(struct device *, void *, void *);
      62             : void urlphy_attach(struct device *, struct device *, void *);
      63             : 
      64             : struct cfattach urlphy_ca = {
      65             :         sizeof(struct mii_softc), urlphy_match, urlphy_attach, mii_phy_detach
      66             : };
      67             : 
      68             : struct cfdriver urlphy_cd = {
      69             :         NULL, "urlphy", DV_DULL
      70             : };
      71             : 
      72             : int urlphy_service(struct mii_softc *, struct mii_data *, int);
      73             : void urlphy_status(struct mii_softc *);
      74             : 
      75             : const struct mii_phy_funcs urlphy_funcs = {
      76             :         urlphy_service, urlphy_status, mii_phy_reset,
      77             : };
      78             : 
      79             : int
      80           0 : urlphy_match(struct device *parent, void *match, void *aux)
      81             : {
      82           0 :         struct mii_attach_args *ma = aux;
      83             : 
      84             :         /*
      85             :          * RTL8150 reports OUT == 0, MODEL == 0
      86             :          */
      87           0 :         if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
      88           0 :             MII_MODEL(ma->mii_id2) != 0)
      89           0 :                 return (0);
      90             : 
      91             :         /*
      92             :          * Make sure the parent is an 'url' device.
      93             :          */
      94           0 :         if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "url") != 0)
      95           0 :                 return (0);
      96             : 
      97           0 :         return (10);
      98           0 : }
      99             : 
     100             : void
     101           0 : urlphy_attach(struct device *parent, struct device *self, void *aux)
     102             : {
     103           0 :         struct mii_softc *sc = (struct mii_softc *)self;
     104           0 :         struct mii_attach_args *ma = aux;
     105           0 :         struct mii_data *mii = ma->mii_data;
     106             : 
     107           0 :         printf(": RTL internal phy\n");
     108             : 
     109           0 :         DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
     110             : 
     111           0 :         sc->mii_inst = mii->mii_instance;
     112           0 :         sc->mii_phy = ma->mii_phyno;
     113           0 :         sc->mii_funcs = &urlphy_funcs;
     114           0 :         sc->mii_pdata = mii;
     115           0 :         sc->mii_flags = ma->mii_flags;
     116           0 :         sc->mii_anegticks = MII_ANEGTICKS_GIGE;
     117             : 
     118           0 :         sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
     119             : 
     120           0 :         if (mii->mii_instance != 0) {
     121           0 :                 printf("%s: ignoring this PHY, non-zero instance\n",
     122           0 :                        sc->mii_dev.dv_xname);
     123           0 :                 return;
     124             :         }
     125           0 :         PHY_RESET(sc);
     126             : 
     127           0 :         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
     128           0 :         if (sc->mii_capabilities & BMSR_MEDIAMASK)
     129           0 :                 mii_phy_add_media(sc);
     130           0 : }
     131             : 
     132             : int
     133           0 : urlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
     134             : {
     135           0 :         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     136             :         int reg;
     137             : 
     138           0 :         DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
     139             : 
     140           0 :         if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
     141           0 :                 return (ENXIO);
     142             : 
     143           0 :         switch (cmd) {
     144             :         case MII_POLLSTAT:
     145             :                 /*
     146             :                  * If we're not polling our PHY instance, just return.
     147             :                  */
     148           0 :                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
     149           0 :                         return (0);
     150             :                 break;
     151             : 
     152             :         case MII_MEDIACHG:
     153             :                 /*
     154             :                  * If we're not currently selected, just return.
     155             :                  */
     156           0 :                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
     157           0 :                         return (0);
     158             : 
     159             :                 /* If the interface is not up, don't do anything. */
     160           0 :                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
     161             :                         break;
     162             : 
     163           0 :                 mii_phy_setmedia(sc);
     164           0 :                 break;
     165             : 
     166             :         case MII_TICK:
     167             :                 /*
     168             :                  * If we're not currently selected, just return.
     169             :                  */
     170           0 :                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
     171           0 :                         return (0);
     172             : 
     173             :                 /* Just bail now if the interface is down. */
     174           0 :                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
     175           0 :                         return (0);
     176             : 
     177             :                 /*
     178             :                  * If we're not doing autonegotiation, we don't need to do
     179             :                  * any extra work here.  However, we need to check the link
     180             :                  * status so we can generate an announcement if the status
     181             :                  * changes.
     182             :                  */
     183           0 :                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
     184           0 :                         return (0);
     185             : 
     186             :                 /* Read the status register twice; MSR_LINK is latch-low. */
     187           0 :                 reg = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
     188           0 :                 if (reg & URLPHY_MSR_LINK) {
     189           0 :                         sc->mii_ticks = 0;
     190           0 :                         break;
     191             :                 }
     192             : 
     193             :                 /*
     194             :                  * Only retry autonegotiation every mii_anegticks seconds.
     195             :                  */
     196           0 :                 if (++sc->mii_ticks <= sc->mii_anegticks)
     197           0 :                         return (0);
     198             : 
     199           0 :                 sc->mii_ticks = 0;
     200           0 :                 PHY_RESET(sc);
     201             : 
     202           0 :                 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
     203           0 :                         return (0);
     204             : 
     205             :                 break;
     206             : 
     207             :         case MII_DOWN:
     208           0 :                 mii_phy_down(sc);
     209           0 :                 return (0);
     210             :         }
     211             : 
     212             :         /* Update the media status. */
     213           0 :         mii_phy_status(sc);
     214             : 
     215             :         /* Callback if something changed. */
     216           0 :         mii_phy_update(sc, cmd);
     217             : 
     218           0 :         return (0);
     219           0 : }
     220             : 
     221             : void
     222           0 : urlphy_status(struct mii_softc *sc)
     223             : {
     224           0 :         struct mii_data *mii = sc->mii_pdata;
     225           0 :         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     226             :         int msr, bmsr, bmcr;
     227             : 
     228           0 :         DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
     229             : 
     230           0 :         mii->mii_media_status = IFM_AVALID;
     231           0 :         mii->mii_media_active = IFM_ETHER;
     232             : 
     233             :         /*
     234             :          * The link status bit is not exist in the BMSR register,
     235             :          * so we need to read the MSR register to get link status.
     236             :          */
     237           0 :         msr = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
     238           0 :         if (msr & URLPHY_MSR_LINK)
     239           0 :                 mii->mii_media_status |= IFM_ACTIVE;
     240             : 
     241           0 :         DPRINTF(("%s: %s: link %s\n", sc->mii_dev.dv_xname, __func__,
     242             :                  mii->mii_media_status & IFM_ACTIVE ? "up" : "down"));
     243             : 
     244           0 :         bmcr = PHY_READ(sc, MII_BMCR);
     245           0 :         if (bmcr & BMCR_AUTOEN) {
     246           0 :                 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
     247           0 :                 if ((bmsr & BMSR_ACOMP) == 0) {
     248             :                         /* Erg, still trying, I guess... */
     249           0 :                         mii->mii_media_active |= IFM_NONE;
     250           0 :                         return;
     251             :                 }
     252             : 
     253           0 :                 if (msr & URLPHY_MSR_SPEED_100)
     254           0 :                         mii->mii_media_active |= IFM_100_TX;
     255             :                 else
     256           0 :                         mii->mii_media_active |= IFM_10_T;
     257             : 
     258           0 :                 if (msr & URLPHY_MSR_DUPLEX)
     259           0 :                         mii->mii_media_active |= IFM_FDX;
     260             :                 else
     261           0 :                         mii->mii_media_active |= IFM_HDX;
     262             :         } else
     263           0 :                 mii->mii_media_active = ife->ifm_media;
     264           0 : }

Generated by: LCOV version 1.13