Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * linux/mdio.h: definitions for MDIO (clause 45) transceivers
0004  * Copyright 2006-2009 Solarflare Communications Inc.
0005  */
0006 #ifndef __LINUX_MDIO_H__
0007 #define __LINUX_MDIO_H__
0008 
0009 #include <uapi/linux/mdio.h>
0010 #include <linux/bitfield.h>
0011 #include <linux/mod_devicetable.h>
0012 
0013 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
0014  * IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips.
0015  */
0016 #define MII_ADDR_C45        (1<<30)
0017 #define MII_DEVADDR_C45_SHIFT   16
0018 #define MII_DEVADDR_C45_MASK    GENMASK(20, 16)
0019 #define MII_REGADDR_C45_MASK    GENMASK(15, 0)
0020 
0021 struct gpio_desc;
0022 struct mii_bus;
0023 struct reset_control;
0024 
0025 /* Multiple levels of nesting are possible. However typically this is
0026  * limited to nested DSA like layer, a MUX layer, and the normal
0027  * user. Instead of trying to handle the general case, just define
0028  * these cases.
0029  */
0030 enum mdio_mutex_lock_class {
0031     MDIO_MUTEX_NORMAL,
0032     MDIO_MUTEX_MUX,
0033     MDIO_MUTEX_NESTED,
0034 };
0035 
0036 struct mdio_device {
0037     struct device dev;
0038 
0039     struct mii_bus *bus;
0040     char modalias[MDIO_NAME_SIZE];
0041 
0042     int (*bus_match)(struct device *dev, struct device_driver *drv);
0043     void (*device_free)(struct mdio_device *mdiodev);
0044     void (*device_remove)(struct mdio_device *mdiodev);
0045 
0046     /* Bus address of the MDIO device (0-31) */
0047     int addr;
0048     int flags;
0049     struct gpio_desc *reset_gpio;
0050     struct reset_control *reset_ctrl;
0051     unsigned int reset_assert_delay;
0052     unsigned int reset_deassert_delay;
0053 };
0054 
0055 static inline struct mdio_device *to_mdio_device(const struct device *dev)
0056 {
0057     return container_of(dev, struct mdio_device, dev);
0058 }
0059 
0060 /* struct mdio_driver_common: Common to all MDIO drivers */
0061 struct mdio_driver_common {
0062     struct device_driver driver;
0063     int flags;
0064 };
0065 #define MDIO_DEVICE_FLAG_PHY        1
0066 
0067 static inline struct mdio_driver_common *
0068 to_mdio_common_driver(const struct device_driver *driver)
0069 {
0070     return container_of(driver, struct mdio_driver_common, driver);
0071 }
0072 
0073 /* struct mdio_driver: Generic MDIO driver */
0074 struct mdio_driver {
0075     struct mdio_driver_common mdiodrv;
0076 
0077     /*
0078      * Called during discovery.  Used to set
0079      * up device-specific structures, if any
0080      */
0081     int (*probe)(struct mdio_device *mdiodev);
0082 
0083     /* Clears up any memory if needed */
0084     void (*remove)(struct mdio_device *mdiodev);
0085 
0086     /* Quiesces the device on system shutdown, turns off interrupts etc */
0087     void (*shutdown)(struct mdio_device *mdiodev);
0088 };
0089 
0090 static inline struct mdio_driver *
0091 to_mdio_driver(const struct device_driver *driver)
0092 {
0093     return container_of(to_mdio_common_driver(driver), struct mdio_driver,
0094                 mdiodrv);
0095 }
0096 
0097 /* device driver data */
0098 static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
0099 {
0100     dev_set_drvdata(&mdio->dev, data);
0101 }
0102 
0103 static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
0104 {
0105     return dev_get_drvdata(&mdio->dev);
0106 }
0107 
0108 void mdio_device_free(struct mdio_device *mdiodev);
0109 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
0110 int mdio_device_register(struct mdio_device *mdiodev);
0111 void mdio_device_remove(struct mdio_device *mdiodev);
0112 void mdio_device_reset(struct mdio_device *mdiodev, int value);
0113 int mdio_driver_register(struct mdio_driver *drv);
0114 void mdio_driver_unregister(struct mdio_driver *drv);
0115 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
0116 
0117 static inline bool mdio_phy_id_is_c45(int phy_id)
0118 {
0119     return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
0120 }
0121 
0122 static inline __u16 mdio_phy_id_prtad(int phy_id)
0123 {
0124     return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
0125 }
0126 
0127 static inline __u16 mdio_phy_id_devad(int phy_id)
0128 {
0129     return phy_id & MDIO_PHY_ID_DEVAD;
0130 }
0131 
0132 /**
0133  * struct mdio_if_info - Ethernet controller MDIO interface
0134  * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
0135  * @mmds: Mask of MMDs expected to be present in the PHY.  This must be
0136  *  non-zero unless @prtad = %MDIO_PRTAD_NONE.
0137  * @mode_support: MDIO modes supported.  If %MDIO_SUPPORTS_C22 is set then
0138  *  MII register access will be passed through with @devad =
0139  *  %MDIO_DEVAD_NONE.  If %MDIO_EMULATE_C22 is set then access to
0140  *  commonly used clause 22 registers will be translated into
0141  *  clause 45 registers.
0142  * @dev: Net device structure
0143  * @mdio_read: Register read function; returns value or negative error code
0144  * @mdio_write: Register write function; returns 0 or negative error code
0145  */
0146 struct mdio_if_info {
0147     int prtad;
0148     u32 mmds;
0149     unsigned mode_support;
0150 
0151     struct net_device *dev;
0152     int (*mdio_read)(struct net_device *dev, int prtad, int devad,
0153              u16 addr);
0154     int (*mdio_write)(struct net_device *dev, int prtad, int devad,
0155               u16 addr, u16 val);
0156 };
0157 
0158 #define MDIO_PRTAD_NONE         (-1)
0159 #define MDIO_DEVAD_NONE         (-1)
0160 #define MDIO_SUPPORTS_C22       1
0161 #define MDIO_SUPPORTS_C45       2
0162 #define MDIO_EMULATE_C22        4
0163 
0164 struct ethtool_cmd;
0165 struct ethtool_pauseparam;
0166 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
0167 extern int mdio_set_flag(const struct mdio_if_info *mdio,
0168              int prtad, int devad, u16 addr, int mask,
0169              bool sense);
0170 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
0171 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
0172 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
0173                       struct ethtool_cmd *ecmd,
0174                       u32 npage_adv, u32 npage_lpa);
0175 extern void
0176 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
0177                    struct ethtool_link_ksettings *cmd,
0178                    u32 npage_adv, u32 npage_lpa);
0179 
0180 /**
0181  * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
0182  * @mdio: MDIO interface
0183  * @ecmd: Ethtool request structure
0184  *
0185  * Since the CSRs for auto-negotiation using next pages are not fully
0186  * standardised, this function does not attempt to decode them.  Use
0187  * mdio45_ethtool_gset_npage() to specify advertisement bits from next
0188  * pages.
0189  */
0190 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
0191                        struct ethtool_cmd *ecmd)
0192 {
0193     mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
0194 }
0195 
0196 /**
0197  * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
0198  * @mdio: MDIO interface
0199  * @cmd: Ethtool request structure
0200  *
0201  * Since the CSRs for auto-negotiation using next pages are not fully
0202  * standardised, this function does not attempt to decode them.  Use
0203  * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
0204  * from next pages.
0205  */
0206 static inline void
0207 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
0208                  struct ethtool_link_ksettings *cmd)
0209 {
0210     mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
0211 }
0212 
0213 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
0214               struct mii_ioctl_data *mii_data, int cmd);
0215 
0216 /**
0217  * mmd_eee_cap_to_ethtool_sup_t
0218  * @eee_cap: value of the MMD EEE Capability register
0219  *
0220  * A small helper function that translates MMD EEE Capability (3.20) bits
0221  * to ethtool supported settings.
0222  */
0223 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
0224 {
0225     u32 supported = 0;
0226 
0227     if (eee_cap & MDIO_EEE_100TX)
0228         supported |= SUPPORTED_100baseT_Full;
0229     if (eee_cap & MDIO_EEE_1000T)
0230         supported |= SUPPORTED_1000baseT_Full;
0231     if (eee_cap & MDIO_EEE_10GT)
0232         supported |= SUPPORTED_10000baseT_Full;
0233     if (eee_cap & MDIO_EEE_1000KX)
0234         supported |= SUPPORTED_1000baseKX_Full;
0235     if (eee_cap & MDIO_EEE_10GKX4)
0236         supported |= SUPPORTED_10000baseKX4_Full;
0237     if (eee_cap & MDIO_EEE_10GKR)
0238         supported |= SUPPORTED_10000baseKR_Full;
0239 
0240     return supported;
0241 }
0242 
0243 /**
0244  * mmd_eee_adv_to_ethtool_adv_t
0245  * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
0246  *
0247  * A small helper function that translates the MMD EEE Advertisment (7.60)
0248  * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
0249  * settings.
0250  */
0251 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
0252 {
0253     u32 adv = 0;
0254 
0255     if (eee_adv & MDIO_EEE_100TX)
0256         adv |= ADVERTISED_100baseT_Full;
0257     if (eee_adv & MDIO_EEE_1000T)
0258         adv |= ADVERTISED_1000baseT_Full;
0259     if (eee_adv & MDIO_EEE_10GT)
0260         adv |= ADVERTISED_10000baseT_Full;
0261     if (eee_adv & MDIO_EEE_1000KX)
0262         adv |= ADVERTISED_1000baseKX_Full;
0263     if (eee_adv & MDIO_EEE_10GKX4)
0264         adv |= ADVERTISED_10000baseKX4_Full;
0265     if (eee_adv & MDIO_EEE_10GKR)
0266         adv |= ADVERTISED_10000baseKR_Full;
0267 
0268     return adv;
0269 }
0270 
0271 /**
0272  * ethtool_adv_to_mmd_eee_adv_t
0273  * @adv: the ethtool advertisement settings
0274  *
0275  * A small helper function that translates ethtool advertisement settings
0276  * to EEE advertisements for the MMD EEE Advertisement (7.60) and
0277  * MMD EEE Link Partner Ability (7.61) registers.
0278  */
0279 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
0280 {
0281     u16 reg = 0;
0282 
0283     if (adv & ADVERTISED_100baseT_Full)
0284         reg |= MDIO_EEE_100TX;
0285     if (adv & ADVERTISED_1000baseT_Full)
0286         reg |= MDIO_EEE_1000T;
0287     if (adv & ADVERTISED_10000baseT_Full)
0288         reg |= MDIO_EEE_10GT;
0289     if (adv & ADVERTISED_1000baseKX_Full)
0290         reg |= MDIO_EEE_1000KX;
0291     if (adv & ADVERTISED_10000baseKX4_Full)
0292         reg |= MDIO_EEE_10GKX4;
0293     if (adv & ADVERTISED_10000baseKR_Full)
0294         reg |= MDIO_EEE_10GKR;
0295 
0296     return reg;
0297 }
0298 
0299 /**
0300  * linkmode_adv_to_mii_10gbt_adv_t
0301  * @advertising: the linkmode advertisement settings
0302  *
0303  * A small helper function that translates linkmode advertisement
0304  * settings to phy autonegotiation advertisements for the C45
0305  * 10GBASE-T AN CONTROL (7.32) register.
0306  */
0307 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
0308 {
0309     u32 result = 0;
0310 
0311     if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
0312                   advertising))
0313         result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
0314     if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
0315                   advertising))
0316         result |= MDIO_AN_10GBT_CTRL_ADV5G;
0317     if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
0318                   advertising))
0319         result |= MDIO_AN_10GBT_CTRL_ADV10G;
0320 
0321     return result;
0322 }
0323 
0324 /**
0325  * mii_10gbt_stat_mod_linkmode_lpa_t
0326  * @advertising: target the linkmode advertisement settings
0327  * @lpa: value of the C45 10GBASE-T AN STATUS register
0328  *
0329  * A small helper function that translates C45 10GBASE-T AN STATUS register bits
0330  * to linkmode advertisement settings. Other bits in advertising aren't changed.
0331  */
0332 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
0333                              u32 lpa)
0334 {
0335     linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
0336              advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
0337     linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
0338              advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
0339     linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
0340              advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
0341 }
0342 
0343 /**
0344  * mii_t1_adv_l_mod_linkmode_t
0345  * @advertising: target the linkmode advertisement settings
0346  * @lpa: value of the BASE-T1 Autonegotiation Advertisement [15:0] Register
0347  *
0348  * A small helper function that translates BASE-T1 Autonegotiation
0349  * Advertisement [15:0] Register bits to linkmode advertisement settings.
0350  * Other bits in advertising aren't changed.
0351  */
0352 static inline void mii_t1_adv_l_mod_linkmode_t(unsigned long *advertising, u32 lpa)
0353 {
0354     linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising,
0355              lpa & MDIO_AN_T1_ADV_L_PAUSE_CAP);
0356     linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising,
0357              lpa & MDIO_AN_T1_ADV_L_PAUSE_ASYM);
0358 }
0359 
0360 /**
0361  * mii_t1_adv_m_mod_linkmode_t
0362  * @advertising: target the linkmode advertisement settings
0363  * @lpa: value of the BASE-T1 Autonegotiation Advertisement [31:16] Register
0364  *
0365  * A small helper function that translates BASE-T1 Autonegotiation
0366  * Advertisement [31:16] Register bits to linkmode advertisement settings.
0367  * Other bits in advertising aren't changed.
0368  */
0369 static inline void mii_t1_adv_m_mod_linkmode_t(unsigned long *advertising, u32 lpa)
0370 {
0371     linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
0372              advertising, lpa & MDIO_AN_T1_ADV_M_B10L);
0373 }
0374 
0375 /**
0376  * linkmode_adv_to_mii_t1_adv_l_t
0377  * @advertising: the linkmode advertisement settings
0378  *
0379  * A small helper function that translates linkmode advertisement
0380  * settings to phy autonegotiation advertisements for the
0381  * BASE-T1 Autonegotiation Advertisement [15:0] Register.
0382  */
0383 static inline u32 linkmode_adv_to_mii_t1_adv_l_t(unsigned long *advertising)
0384 {
0385     u32 result = 0;
0386 
0387     if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising))
0388         result |= MDIO_AN_T1_ADV_L_PAUSE_CAP;
0389     if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising))
0390         result |= MDIO_AN_T1_ADV_L_PAUSE_ASYM;
0391 
0392     return result;
0393 }
0394 
0395 /**
0396  * linkmode_adv_to_mii_t1_adv_m_t
0397  * @advertising: the linkmode advertisement settings
0398  *
0399  * A small helper function that translates linkmode advertisement
0400  * settings to phy autonegotiation advertisements for the
0401  * BASE-T1 Autonegotiation Advertisement [31:16] Register.
0402  */
0403 static inline u32 linkmode_adv_to_mii_t1_adv_m_t(unsigned long *advertising)
0404 {
0405     u32 result = 0;
0406 
0407     if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, advertising))
0408         result |= MDIO_AN_T1_ADV_M_B10L;
0409 
0410     return result;
0411 }
0412 
0413 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
0414 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
0415 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
0416                  u16 mask, u16 set);
0417 
0418 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
0419 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
0420 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
0421 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
0422 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
0423            u16 set);
0424 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
0425                u16 mask, u16 set);
0426 
0427 static inline int mdiodev_read(struct mdio_device *mdiodev, u32 regnum)
0428 {
0429     return mdiobus_read(mdiodev->bus, mdiodev->addr, regnum);
0430 }
0431 
0432 static inline int mdiodev_write(struct mdio_device *mdiodev, u32 regnum,
0433                 u16 val)
0434 {
0435     return mdiobus_write(mdiodev->bus, mdiodev->addr, regnum, val);
0436 }
0437 
0438 static inline int mdiodev_modify(struct mdio_device *mdiodev, u32 regnum,
0439                  u16 mask, u16 set)
0440 {
0441     return mdiobus_modify(mdiodev->bus, mdiodev->addr, regnum, mask, set);
0442 }
0443 
0444 static inline int mdiodev_modify_changed(struct mdio_device *mdiodev,
0445                      u32 regnum, u16 mask, u16 set)
0446 {
0447     return mdiobus_modify_changed(mdiodev->bus, mdiodev->addr, regnum,
0448                       mask, set);
0449 }
0450 
0451 static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
0452 {
0453     return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
0454 }
0455 
0456 static inline u16 mdiobus_c45_regad(u32 regnum)
0457 {
0458     return FIELD_GET(MII_REGADDR_C45_MASK, regnum);
0459 }
0460 
0461 static inline u16 mdiobus_c45_devad(u32 regnum)
0462 {
0463     return FIELD_GET(MII_DEVADDR_C45_MASK, regnum);
0464 }
0465 
0466 static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
0467                      u16 regnum)
0468 {
0469     return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
0470 }
0471 
0472 static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
0473                       u16 regnum, u16 val)
0474 {
0475     return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
0476                    val);
0477 }
0478 
0479 static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
0480                    u16 regnum)
0481 {
0482     return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
0483 }
0484 
0485 static inline int mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
0486                     u16 regnum, u16 val)
0487 {
0488     return mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum), val);
0489 }
0490 
0491 int mdiobus_register_device(struct mdio_device *mdiodev);
0492 int mdiobus_unregister_device(struct mdio_device *mdiodev);
0493 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
0494 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
0495 
0496 /**
0497  * mdio_module_driver() - Helper macro for registering mdio drivers
0498  * @_mdio_driver: driver to register
0499  *
0500  * Helper macro for MDIO drivers which do not do anything special in module
0501  * init/exit. Each module may only use this macro once, and calling it
0502  * replaces module_init() and module_exit().
0503  */
0504 #define mdio_module_driver(_mdio_driver)                \
0505 static int __init mdio_module_init(void)                \
0506 {                                   \
0507     return mdio_driver_register(&_mdio_driver);         \
0508 }                                   \
0509 module_init(mdio_module_init);                      \
0510 static void __exit mdio_module_exit(void)               \
0511 {                                   \
0512     mdio_driver_unregister(&_mdio_driver);              \
0513 }                                   \
0514 module_exit(mdio_module_exit)
0515 
0516 #endif /* __LINUX_MDIO_H__ */