1d0027533SBill Paul /* $NetBSD: miivar.h,v 1.8 1999/04/23 04:24:32 thorpej Exp $ */ 2d0027533SBill Paul 3d0027533SBill Paul /*- 4718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5718cf2ccSPedro F. Giffuni * 6d0027533SBill Paul * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 7d0027533SBill Paul * All rights reserved. 8d0027533SBill Paul * 9d0027533SBill Paul * This code is derived from software contributed to The NetBSD Foundation 10d0027533SBill Paul * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 11d0027533SBill Paul * NASA Ames Research Center. 12d0027533SBill Paul * 13d0027533SBill Paul * Redistribution and use in source and binary forms, with or without 14d0027533SBill Paul * modification, are permitted provided that the following conditions 15d0027533SBill Paul * are met: 16d0027533SBill Paul * 1. Redistributions of source code must retain the above copyright 17d0027533SBill Paul * notice, this list of conditions and the following disclaimer. 18d0027533SBill Paul * 2. Redistributions in binary form must reproduce the above copyright 19d0027533SBill Paul * notice, this list of conditions and the following disclaimer in the 20d0027533SBill Paul * documentation and/or other materials provided with the distribution. 21d0027533SBill Paul * 22d0027533SBill Paul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23d0027533SBill Paul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24d0027533SBill Paul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25d0027533SBill Paul * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26d0027533SBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27d0027533SBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28d0027533SBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29d0027533SBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30d0027533SBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31d0027533SBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32d0027533SBill Paul * POSSIBILITY OF SUCH DAMAGE. 33d0027533SBill Paul * 34c3aac50fSPeter Wemm * $FreeBSD$ 35d0027533SBill Paul */ 36d0027533SBill Paul 37d0027533SBill Paul #ifndef _DEV_MII_MIIVAR_H_ 38d0027533SBill Paul #define _DEV_MII_MIIVAR_H_ 39d0027533SBill Paul 40d0027533SBill Paul #include <sys/queue.h> 4162d76917SMarcel Moolenaar #include <net/if_var.h> /* XXX driver API temporary */ 42d0027533SBill Paul 439174eab4SKornel Duleba #include "opt_platform.h" 449174eab4SKornel Duleba 459174eab4SKornel Duleba #ifdef FDT 469174eab4SKornel Duleba #include <dev/ofw/openfirm.h> 479174eab4SKornel Duleba #include <dev/ofw/ofw_bus.h> 489174eab4SKornel Duleba #include <dev/ofw/ofw_bus_subr.h> 499174eab4SKornel Duleba #endif 509174eab4SKornel Duleba 51d0027533SBill Paul /* 523fcb7a53SMarius Strobl * Media Independent Interface data structure defintions 53d0027533SBill Paul */ 54d0027533SBill Paul 55d0027533SBill Paul struct mii_softc; 56d0027533SBill Paul 57d0027533SBill Paul /* 58d0027533SBill Paul * A network interface driver has one of these structures in its softc. 59d0027533SBill Paul * It is the interface from the network interface driver to the MII 60d0027533SBill Paul * layer. 61d0027533SBill Paul */ 62d0027533SBill Paul struct mii_data { 63d0027533SBill Paul struct ifmedia mii_media; /* media information */ 6462d76917SMarcel Moolenaar if_t mii_ifp; /* pointer back to network interface */ 65d0027533SBill Paul 66d0027533SBill Paul /* 67d0027533SBill Paul * For network interfaces with multiple PHYs, a list of all 68d0027533SBill Paul * PHYs is required so they can all be notified when a media 69d0027533SBill Paul * request is made. 70d0027533SBill Paul */ 71e3975643SJake Burkholder LIST_HEAD(mii_listhead, mii_softc) mii_phys; 723fcb7a53SMarius Strobl u_int mii_instance; 73d0027533SBill Paul 74d0027533SBill Paul /* 75d0027533SBill Paul * PHY driver fills this in with active media status. 76d0027533SBill Paul */ 773fcb7a53SMarius Strobl u_int mii_media_status; 783fcb7a53SMarius Strobl u_int mii_media_active; 79d0027533SBill Paul }; 80d0027533SBill Paul typedef struct mii_data mii_data_t; 81d0027533SBill Paul 82d0027533SBill Paul /* 833fcb7a53SMarius Strobl * Functions provided by the PHY to perform various functions. 84d0027533SBill Paul */ 853fcb7a53SMarius Strobl struct mii_phy_funcs { 863fcb7a53SMarius Strobl int (*pf_service)(struct mii_softc *, struct mii_data *, int); 873fcb7a53SMarius Strobl void (*pf_status)(struct mii_softc *); 883fcb7a53SMarius Strobl void (*pf_reset)(struct mii_softc *); 893fcb7a53SMarius Strobl }; 90d0027533SBill Paul 91d0027533SBill Paul /* 92d0027533SBill Paul * Requests that can be made to the downcall. 93d0027533SBill Paul */ 94d0027533SBill Paul #define MII_TICK 1 /* once-per-second tick */ 95d0027533SBill Paul #define MII_MEDIACHG 2 /* user changed media; perform the switch */ 96d0027533SBill Paul #define MII_POLLSTAT 3 /* user requested media status; fill it in */ 97d0027533SBill Paul 98d0027533SBill Paul /* 99d0027533SBill Paul * Each PHY driver's softc has one of these as the first member. 100d0027533SBill Paul * XXX This would be better named "phy_softc", but this is the name 101d0027533SBill Paul * XXX BSDI used, and we would like to have the same interface. 102d0027533SBill Paul */ 103d0027533SBill Paul struct mii_softc { 104d0027533SBill Paul device_t mii_dev; /* generic device glue */ 105d0027533SBill Paul 106e3975643SJake Burkholder LIST_ENTRY(mii_softc) mii_list; /* entry on parent's PHY list */ 107d0027533SBill Paul 1083fcb7a53SMarius Strobl uint32_t mii_mpd_oui; /* the PHY's OUI (MII_OUI())*/ 1093fcb7a53SMarius Strobl uint32_t mii_mpd_model; /* the PHY's model (MII_MODEL())*/ 1103fcb7a53SMarius Strobl uint32_t mii_mpd_rev; /* the PHY's revision (MII_REV())*/ 1113fcb7a53SMarius Strobl u_int mii_capmask; /* capability mask for BMSR */ 1123fcb7a53SMarius Strobl u_int mii_phy; /* our MII address */ 1133fcb7a53SMarius Strobl u_int mii_offset; /* first PHY, second PHY, etc. */ 1143fcb7a53SMarius Strobl u_int mii_inst; /* instance for ifmedia */ 115d0027533SBill Paul 1163fcb7a53SMarius Strobl /* Our PHY functions. */ 1173fcb7a53SMarius Strobl const struct mii_phy_funcs *mii_funcs; 1183fcb7a53SMarius Strobl 119d0027533SBill Paul struct mii_data *mii_pdata; /* pointer to parent's mii_data */ 120d0027533SBill Paul 1213fcb7a53SMarius Strobl u_int mii_flags; /* misc. flags; see below */ 1223fcb7a53SMarius Strobl u_int mii_capabilities; /* capabilities from BMSR */ 1233fcb7a53SMarius Strobl u_int mii_extcapabilities; /* extended capabilities */ 1243fcb7a53SMarius Strobl u_int mii_ticks; /* MII_TICK counter */ 1253fcb7a53SMarius Strobl u_int mii_anegticks; /* ticks before retrying aneg */ 1263fcb7a53SMarius Strobl u_int mii_media_active; /* last active media */ 1273fcb7a53SMarius Strobl u_int mii_media_status; /* last active status */ 128b38de28aSKornel Duleba u_int mii_maxspeed; /* Max speed supported by this PHY */ 129d0027533SBill Paul }; 130d0027533SBill Paul typedef struct mii_softc mii_softc_t; 131d0027533SBill Paul 132d0027533SBill Paul /* mii_flags */ 133a55fb8a4SMarius Strobl #define MIIF_INITDONE 0x00000001 /* has been initialized (mii_data) */ 134a55fb8a4SMarius Strobl #define MIIF_NOISOLATE 0x00000002 /* do not isolate the PHY */ 1353fcb7a53SMarius Strobl #if 0 136a55fb8a4SMarius Strobl #define MIIF_NOLOOP 0x00000004 /* no loopback capability */ 1373fcb7a53SMarius Strobl #endif 138efd4fc3fSMarius Strobl #define MIIF_DOINGAUTO 0x00000008 /* doing autonegotiation (mii_softc) */ 139a55fb8a4SMarius Strobl #define MIIF_AUTOTSLEEP 0x00000010 /* use tsleep(), not callout() */ 140a55fb8a4SMarius Strobl #define MIIF_HAVEFIBER 0x00000020 /* from parent: has fiber interface */ 141a55fb8a4SMarius Strobl #define MIIF_HAVE_GTCR 0x00000040 /* has 100base-T2/1000base-T CR */ 142a55fb8a4SMarius Strobl #define MIIF_IS_1000X 0x00000080 /* is a 1000BASE-X device */ 143a55fb8a4SMarius Strobl #define MIIF_DOPAUSE 0x00000100 /* advertise PAUSE capability */ 144a55fb8a4SMarius Strobl #define MIIF_IS_HPNA 0x00000200 /* is a HomePNA device */ 145a55fb8a4SMarius Strobl #define MIIF_FORCEANEG 0x00000400 /* force auto-negotiation */ 146349eddbdSMike Karels #define MIIF_RX_DELAY 0x00000800 /* add RX delay */ 147349eddbdSMike Karels #define MIIF_TX_DELAY 0x00001000 /* add TX delay */ 148efd4fc3fSMarius Strobl #define MIIF_NOMANPAUSE 0x00100000 /* no manual PAUSE selection */ 1496882cb0cSMarius Strobl #define MIIF_FORCEPAUSE 0x00200000 /* force PAUSE advertisement */ 150a55fb8a4SMarius Strobl #define MIIF_MACPRIV0 0x01000000 /* private to the MAC driver */ 151a55fb8a4SMarius Strobl #define MIIF_MACPRIV1 0x02000000 /* private to the MAC driver */ 152a55fb8a4SMarius Strobl #define MIIF_MACPRIV2 0x04000000 /* private to the MAC driver */ 153a55fb8a4SMarius Strobl #define MIIF_PHYPRIV0 0x10000000 /* private to the PHY driver */ 154a55fb8a4SMarius Strobl #define MIIF_PHYPRIV1 0x20000000 /* private to the PHY driver */ 155a55fb8a4SMarius Strobl #define MIIF_PHYPRIV2 0x40000000 /* private to the PHY driver */ 156b7dee1dbSPoul-Henning Kamp 1571c47c79eSOleg Bulyzhin /* Default mii_anegticks values */ 1581c47c79eSOleg Bulyzhin #define MII_ANEGTICKS 5 1591c47c79eSOleg Bulyzhin #define MII_ANEGTICKS_GIGE 17 1601c47c79eSOleg Bulyzhin 161b7dee1dbSPoul-Henning Kamp #define MIIF_INHERIT_MASK (MIIF_NOISOLATE|MIIF_NOLOOP|MIIF_AUTOTSLEEP) 162d0027533SBill Paul 163d0027533SBill Paul /* 164a55fb8a4SMarius Strobl * Special `locators' passed to mii_attach(). If one of these is not 165a55fb8a4SMarius Strobl * an `any' value, we look for *that* PHY and configure it. If both 1664a446e3eSMarius Strobl * are not `any', that is an error, and mii_attach() will fail. 167a55fb8a4SMarius Strobl */ 168a55fb8a4SMarius Strobl #define MII_OFFSET_ANY -1 169a55fb8a4SMarius Strobl #define MII_PHY_ANY -1 170a55fb8a4SMarius Strobl 171a55fb8a4SMarius Strobl /* 1720f75981aSIan Lepore * Constants used to describe the type of attachment between MAC and PHY. 1730f75981aSIan Lepore */ 1740f75981aSIan Lepore enum mii_contype { 1750f75981aSIan Lepore MII_CONTYPE_UNKNOWN, /* Must be have value 0. */ 1760f75981aSIan Lepore 1770f75981aSIan Lepore MII_CONTYPE_MII, 1780f75981aSIan Lepore MII_CONTYPE_GMII, 1790f75981aSIan Lepore MII_CONTYPE_SGMII, 1800f75981aSIan Lepore MII_CONTYPE_QSGMII, 1810f75981aSIan Lepore MII_CONTYPE_TBI, 1820f75981aSIan Lepore MII_CONTYPE_REVMII, /* Reverse MII */ 1830f75981aSIan Lepore MII_CONTYPE_RMII, 1840f75981aSIan Lepore MII_CONTYPE_RGMII, /* Delays provided by MAC or PCB */ 1850f75981aSIan Lepore MII_CONTYPE_RGMII_ID, /* Rx and tx delays provided by PHY */ 1860f75981aSIan Lepore MII_CONTYPE_RGMII_RXID, /* Only rx delay provided by PHY */ 1870f75981aSIan Lepore MII_CONTYPE_RGMII_TXID, /* Only tx delay provided by PHY */ 1880f75981aSIan Lepore MII_CONTYPE_RTBI, 1890f75981aSIan Lepore MII_CONTYPE_SMII, 1900f75981aSIan Lepore MII_CONTYPE_XGMII, 1910f75981aSIan Lepore MII_CONTYPE_TRGMII, 1920f75981aSIan Lepore MII_CONTYPE_2000BX, 1930f75981aSIan Lepore MII_CONTYPE_2500BX, 1940f75981aSIan Lepore MII_CONTYPE_RXAUI, 1950f75981aSIan Lepore 1960f75981aSIan Lepore MII_CONTYPE_COUNT /* Add new types before this line. */ 1970f75981aSIan Lepore }; 1980f75981aSIan Lepore typedef enum mii_contype mii_contype_t; 1990f75981aSIan Lepore 2000f75981aSIan Lepore static inline bool 2010f75981aSIan Lepore mii_contype_is_rgmii(mii_contype_t con) 2020f75981aSIan Lepore { 2030f75981aSIan Lepore 2040f75981aSIan Lepore return (con >= MII_CONTYPE_RGMII && con <= MII_CONTYPE_RGMII_TXID); 2050f75981aSIan Lepore } 2060f75981aSIan Lepore 2070f75981aSIan Lepore /* 208d0027533SBill Paul * Used to attach a PHY to a parent. 209d0027533SBill Paul */ 210d0027533SBill Paul struct mii_attach_args { 211d0027533SBill Paul struct mii_data *mii_data; /* pointer to parent data */ 2123fcb7a53SMarius Strobl u_int mii_phyno; /* MII address */ 2133fcb7a53SMarius Strobl u_int mii_offset; /* first PHY, second PHY, etc. */ 2143fcb7a53SMarius Strobl uint32_t mii_id1; /* PHY ID register 1 */ 2153fcb7a53SMarius Strobl uint32_t mii_id2; /* PHY ID register 2 */ 2163fcb7a53SMarius Strobl u_int mii_capmask; /* capability mask for BMSR */ 2179174eab4SKornel Duleba #ifdef FDT 2189174eab4SKornel Duleba struct ofw_bus_devinfo obd; 2199174eab4SKornel Duleba struct resource_list rl; 2209174eab4SKornel Duleba #endif 2219174eab4SKornel Duleba 222d0027533SBill Paul }; 223d0027533SBill Paul typedef struct mii_attach_args mii_attach_args_t; 224d0027533SBill Paul 225b7dee1dbSPoul-Henning Kamp /* 226875525d5SPoul-Henning Kamp * Used to match a PHY. 227875525d5SPoul-Henning Kamp */ 228875525d5SPoul-Henning Kamp struct mii_phydesc { 2293fcb7a53SMarius Strobl uint32_t mpd_oui; /* the PHY's OUI */ 2303fcb7a53SMarius Strobl uint32_t mpd_model; /* the PHY's model */ 231875525d5SPoul-Henning Kamp const char *mpd_name; /* the PHY's name */ 232875525d5SPoul-Henning Kamp }; 233a1039f82SWarner Losh #define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \ 234a1039f82SWarner Losh MII_STR_ ## a ## _ ## b } 235a1039f82SWarner Losh #define MII_PHY_END { 0, 0, NULL } 236875525d5SPoul-Henning Kamp 237664a31e4SPeter Wemm #ifdef _KERNEL 238d0027533SBill Paul 239d0027533SBill Paul #define PHY_READ(p, r) \ 240d0027533SBill Paul MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r)) 241d0027533SBill Paul 242d0027533SBill Paul #define PHY_WRITE(p, r, v) \ 243d0027533SBill Paul MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v)) 244d0027533SBill Paul 2453fcb7a53SMarius Strobl #define PHY_SERVICE(p, d, o) \ 2463fcb7a53SMarius Strobl (*(p)->mii_funcs->pf_service)((p), (d), (o)) 2473fcb7a53SMarius Strobl 2483fcb7a53SMarius Strobl #define PHY_STATUS(p) \ 2493fcb7a53SMarius Strobl (*(p)->mii_funcs->pf_status)(p) 2503fcb7a53SMarius Strobl 2513fcb7a53SMarius Strobl #define PHY_RESET(p) \ 2523fcb7a53SMarius Strobl (*(p)->mii_funcs->pf_reset)(p) 2533fcb7a53SMarius Strobl 254a55fb8a4SMarius Strobl enum miibus_device_ivars { 255a55fb8a4SMarius Strobl MIIBUS_IVAR_FLAGS 256a55fb8a4SMarius Strobl }; 257a55fb8a4SMarius Strobl 258a55fb8a4SMarius Strobl /* 259a55fb8a4SMarius Strobl * Simplified accessors for miibus 260a55fb8a4SMarius Strobl */ 261a55fb8a4SMarius Strobl #define MIIBUS_ACCESSOR(var, ivar, type) \ 262a55fb8a4SMarius Strobl __BUS_ACCESSOR(miibus, var, MIIBUS, ivar, type) 263a55fb8a4SMarius Strobl 2643fcb7a53SMarius Strobl MIIBUS_ACCESSOR(flags, FLAGS, u_int) 265a55fb8a4SMarius Strobl 266d0027533SBill Paul extern devclass_t miibus_devclass; 2679174eab4SKornel Duleba DECLARE_CLASS(miibus_driver); 2689174eab4SKornel Duleba 2699174eab4SKornel Duleba #ifdef FDT 2709174eab4SKornel Duleba extern devclass_t miibus_fdt_devclass; 2719174eab4SKornel Duleba DECLARE_CLASS(miibus_fdt_driver); 2729174eab4SKornel Duleba #endif 2739174eab4SKornel Duleba 274d0027533SBill Paul 27562d76917SMarcel Moolenaar int mii_attach(device_t, device_t *, if_t, ifm_change_cb_t, 276a55fb8a4SMarius Strobl ifm_stat_cb_t, int, int, int, int); 277e51a25f8SAlfred Perlstein int mii_mediachg(struct mii_data *); 278e51a25f8SAlfred Perlstein void mii_tick(struct mii_data *); 279e51a25f8SAlfred Perlstein void mii_pollstat(struct mii_data *); 28078c8c3dbSPoul-Henning Kamp void mii_phy_add_media(struct mii_softc *); 281d0027533SBill Paul 282fd94424cSPoul-Henning Kamp int mii_phy_auto(struct mii_softc *); 283279fe8d1SPoul-Henning Kamp int mii_phy_detach(device_t dev); 284efd4fc3fSMarius Strobl u_int mii_phy_flowstatus(struct mii_softc *); 285e51a25f8SAlfred Perlstein void mii_phy_reset(struct mii_softc *); 286b7dee1dbSPoul-Henning Kamp void mii_phy_setmedia(struct mii_softc *sc); 287e51a25f8SAlfred Perlstein void mii_phy_update(struct mii_softc *, int); 288e51a25f8SAlfred Perlstein int mii_phy_tick(struct mii_softc *); 2897e310d2dSGleb Smirnoff int mii_phy_mac_match(struct mii_softc *, const char *); 2907e310d2dSGleb Smirnoff int mii_dev_mac_match(device_t, const char *); 2917e310d2dSGleb Smirnoff void *mii_phy_mac_softc(struct mii_softc *); 2927e310d2dSGleb Smirnoff void *mii_dev_mac_softc(device_t); 293d0027533SBill Paul 29408576af0SWarner Losh const struct mii_phydesc * mii_phy_match(const struct mii_attach_args *ma, 29508576af0SWarner Losh const struct mii_phydesc *mpd); 29608576af0SWarner Losh const struct mii_phydesc * mii_phy_match_gen(const struct mii_attach_args *ma, 29708576af0SWarner Losh const struct mii_phydesc *mpd, size_t endlen); 298a35b9333SMarius Strobl int mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv); 2993fcb7a53SMarius Strobl void mii_phy_dev_attach(device_t dev, u_int flags, 3003fcb7a53SMarius Strobl const struct mii_phy_funcs *mpf, int add_media); 301875525d5SPoul-Henning Kamp 3029174eab4SKornel Duleba device_attach_t miibus_attach; 3039174eab4SKornel Duleba 304e51a25f8SAlfred Perlstein void ukphy_status(struct mii_softc *); 3053fcb7a53SMarius Strobl 3063fcb7a53SMarius Strobl u_int mii_oui(u_int, u_int); 3073fcb7a53SMarius Strobl #define MII_OUI(id1, id2) mii_oui(id1, id2) 3083fcb7a53SMarius Strobl #define MII_MODEL(id2) (((id2) & IDR2_MODEL) >> 4) 3093fcb7a53SMarius Strobl #define MII_REV(id2) ((id2) & IDR2_REV) 3103fcb7a53SMarius Strobl 311664a31e4SPeter Wemm #endif /* _KERNEL */ 312d0027533SBill Paul 313d0027533SBill Paul #endif /* _DEV_MII_MIIVAR_H_ */ 314