mdio.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/mdio.h: definitions for MDIO (clause 45) transceivers
  4. * Copyright 2006-2009 Solarflare Communications Inc.
  5. */
  6. #ifndef __LINUX_MDIO_H__
  7. #define __LINUX_MDIO_H__
  8. #include <uapi/linux/mdio.h>
  9. #include <linux/mod_devicetable.h>
  10. /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
  11. * IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips.
  12. */
  13. #define MII_ADDR_C45 (1<<30)
  14. #define MII_DEVADDR_C45_SHIFT 16
  15. #define MII_REGADDR_C45_MASK GENMASK(15, 0)
  16. struct gpio_desc;
  17. struct mii_bus;
  18. struct reset_control;
  19. /* Multiple levels of nesting are possible. However typically this is
  20. * limited to nested DSA like layer, a MUX layer, and the normal
  21. * user. Instead of trying to handle the general case, just define
  22. * these cases.
  23. */
  24. enum mdio_mutex_lock_class {
  25. MDIO_MUTEX_NORMAL,
  26. MDIO_MUTEX_MUX,
  27. MDIO_MUTEX_NESTED,
  28. };
  29. struct mdio_device {
  30. struct device dev;
  31. struct mii_bus *bus;
  32. char modalias[MDIO_NAME_SIZE];
  33. int (*bus_match)(struct device *dev, struct device_driver *drv);
  34. void (*device_free)(struct mdio_device *mdiodev);
  35. void (*device_remove)(struct mdio_device *mdiodev);
  36. /* Bus address of the MDIO device (0-31) */
  37. int addr;
  38. int flags;
  39. struct gpio_desc *reset_gpio;
  40. struct reset_control *reset_ctrl;
  41. unsigned int reset_assert_delay;
  42. unsigned int reset_deassert_delay;
  43. };
  44. #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
  45. /* struct mdio_driver_common: Common to all MDIO drivers */
  46. struct mdio_driver_common {
  47. struct device_driver driver;
  48. int flags;
  49. };
  50. #define MDIO_DEVICE_FLAG_PHY 1
  51. #define to_mdio_common_driver(d) \
  52. container_of(d, struct mdio_driver_common, driver)
  53. /* struct mdio_driver: Generic MDIO driver */
  54. struct mdio_driver {
  55. struct mdio_driver_common mdiodrv;
  56. /*
  57. * Called during discovery. Used to set
  58. * up device-specific structures, if any
  59. */
  60. int (*probe)(struct mdio_device *mdiodev);
  61. /* Clears up any memory if needed */
  62. void (*remove)(struct mdio_device *mdiodev);
  63. /* Quiesces the device on system shutdown, turns off interrupts etc */
  64. void (*shutdown)(struct mdio_device *mdiodev);
  65. };
  66. #define to_mdio_driver(d) \
  67. container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
  68. /* device driver data */
  69. static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
  70. {
  71. dev_set_drvdata(&mdio->dev, data);
  72. }
  73. static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
  74. {
  75. return dev_get_drvdata(&mdio->dev);
  76. }
  77. void mdio_device_free(struct mdio_device *mdiodev);
  78. struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
  79. int mdio_device_register(struct mdio_device *mdiodev);
  80. void mdio_device_remove(struct mdio_device *mdiodev);
  81. void mdio_device_reset(struct mdio_device *mdiodev, int value);
  82. int mdio_driver_register(struct mdio_driver *drv);
  83. void mdio_driver_unregister(struct mdio_driver *drv);
  84. int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
  85. static inline bool mdio_phy_id_is_c45(int phy_id)
  86. {
  87. return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
  88. }
  89. static inline __u16 mdio_phy_id_prtad(int phy_id)
  90. {
  91. return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
  92. }
  93. static inline __u16 mdio_phy_id_devad(int phy_id)
  94. {
  95. return phy_id & MDIO_PHY_ID_DEVAD;
  96. }
  97. /**
  98. * struct mdio_if_info - Ethernet controller MDIO interface
  99. * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
  100. * @mmds: Mask of MMDs expected to be present in the PHY. This must be
  101. * non-zero unless @prtad = %MDIO_PRTAD_NONE.
  102. * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
  103. * MII register access will be passed through with @devad =
  104. * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
  105. * commonly used clause 22 registers will be translated into
  106. * clause 45 registers.
  107. * @dev: Net device structure
  108. * @mdio_read: Register read function; returns value or negative error code
  109. * @mdio_write: Register write function; returns 0 or negative error code
  110. */
  111. struct mdio_if_info {
  112. int prtad;
  113. u32 mmds;
  114. unsigned mode_support;
  115. struct net_device *dev;
  116. int (*mdio_read)(struct net_device *dev, int prtad, int devad,
  117. u16 addr);
  118. int (*mdio_write)(struct net_device *dev, int prtad, int devad,
  119. u16 addr, u16 val);
  120. };
  121. #define MDIO_PRTAD_NONE (-1)
  122. #define MDIO_DEVAD_NONE (-1)
  123. #define MDIO_SUPPORTS_C22 1
  124. #define MDIO_SUPPORTS_C45 2
  125. #define MDIO_EMULATE_C22 4
  126. struct ethtool_cmd;
  127. struct ethtool_pauseparam;
  128. extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
  129. extern int mdio_set_flag(const struct mdio_if_info *mdio,
  130. int prtad, int devad, u16 addr, int mask,
  131. bool sense);
  132. extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
  133. extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
  134. extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
  135. struct ethtool_cmd *ecmd,
  136. u32 npage_adv, u32 npage_lpa);
  137. extern void
  138. mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
  139. struct ethtool_link_ksettings *cmd,
  140. u32 npage_adv, u32 npage_lpa);
  141. /**
  142. * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
  143. * @mdio: MDIO interface
  144. * @ecmd: Ethtool request structure
  145. *
  146. * Since the CSRs for auto-negotiation using next pages are not fully
  147. * standardised, this function does not attempt to decode them. Use
  148. * mdio45_ethtool_gset_npage() to specify advertisement bits from next
  149. * pages.
  150. */
  151. static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
  152. struct ethtool_cmd *ecmd)
  153. {
  154. mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
  155. }
  156. /**
  157. * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
  158. * @mdio: MDIO interface
  159. * @cmd: Ethtool request structure
  160. *
  161. * Since the CSRs for auto-negotiation using next pages are not fully
  162. * standardised, this function does not attempt to decode them. Use
  163. * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
  164. * from next pages.
  165. */
  166. static inline void
  167. mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
  168. struct ethtool_link_ksettings *cmd)
  169. {
  170. mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
  171. }
  172. extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
  173. struct mii_ioctl_data *mii_data, int cmd);
  174. /**
  175. * mmd_eee_cap_to_ethtool_sup_t
  176. * @eee_cap: value of the MMD EEE Capability register
  177. *
  178. * A small helper function that translates MMD EEE Capability (3.20) bits
  179. * to ethtool supported settings.
  180. */
  181. static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
  182. {
  183. u32 supported = 0;
  184. if (eee_cap & MDIO_EEE_100TX)
  185. supported |= SUPPORTED_100baseT_Full;
  186. if (eee_cap & MDIO_EEE_1000T)
  187. supported |= SUPPORTED_1000baseT_Full;
  188. if (eee_cap & MDIO_EEE_10GT)
  189. supported |= SUPPORTED_10000baseT_Full;
  190. if (eee_cap & MDIO_EEE_1000KX)
  191. supported |= SUPPORTED_1000baseKX_Full;
  192. if (eee_cap & MDIO_EEE_10GKX4)
  193. supported |= SUPPORTED_10000baseKX4_Full;
  194. if (eee_cap & MDIO_EEE_10GKR)
  195. supported |= SUPPORTED_10000baseKR_Full;
  196. return supported;
  197. }
  198. /**
  199. * mmd_eee_adv_to_ethtool_adv_t
  200. * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
  201. *
  202. * A small helper function that translates the MMD EEE Advertisment (7.60)
  203. * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
  204. * settings.
  205. */
  206. static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
  207. {
  208. u32 adv = 0;
  209. if (eee_adv & MDIO_EEE_100TX)
  210. adv |= ADVERTISED_100baseT_Full;
  211. if (eee_adv & MDIO_EEE_1000T)
  212. adv |= ADVERTISED_1000baseT_Full;
  213. if (eee_adv & MDIO_EEE_10GT)
  214. adv |= ADVERTISED_10000baseT_Full;
  215. if (eee_adv & MDIO_EEE_1000KX)
  216. adv |= ADVERTISED_1000baseKX_Full;
  217. if (eee_adv & MDIO_EEE_10GKX4)
  218. adv |= ADVERTISED_10000baseKX4_Full;
  219. if (eee_adv & MDIO_EEE_10GKR)
  220. adv |= ADVERTISED_10000baseKR_Full;
  221. return adv;
  222. }
  223. /**
  224. * ethtool_adv_to_mmd_eee_adv_t
  225. * @adv: the ethtool advertisement settings
  226. *
  227. * A small helper function that translates ethtool advertisement settings
  228. * to EEE advertisements for the MMD EEE Advertisement (7.60) and
  229. * MMD EEE Link Partner Ability (7.61) registers.
  230. */
  231. static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
  232. {
  233. u16 reg = 0;
  234. if (adv & ADVERTISED_100baseT_Full)
  235. reg |= MDIO_EEE_100TX;
  236. if (adv & ADVERTISED_1000baseT_Full)
  237. reg |= MDIO_EEE_1000T;
  238. if (adv & ADVERTISED_10000baseT_Full)
  239. reg |= MDIO_EEE_10GT;
  240. if (adv & ADVERTISED_1000baseKX_Full)
  241. reg |= MDIO_EEE_1000KX;
  242. if (adv & ADVERTISED_10000baseKX4_Full)
  243. reg |= MDIO_EEE_10GKX4;
  244. if (adv & ADVERTISED_10000baseKR_Full)
  245. reg |= MDIO_EEE_10GKR;
  246. return reg;
  247. }
  248. /**
  249. * linkmode_adv_to_mii_10gbt_adv_t
  250. * @advertising: the linkmode advertisement settings
  251. *
  252. * A small helper function that translates linkmode advertisement
  253. * settings to phy autonegotiation advertisements for the C45
  254. * 10GBASE-T AN CONTROL (7.32) register.
  255. */
  256. static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
  257. {
  258. u32 result = 0;
  259. if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  260. advertising))
  261. result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
  262. if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
  263. advertising))
  264. result |= MDIO_AN_10GBT_CTRL_ADV5G;
  265. if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  266. advertising))
  267. result |= MDIO_AN_10GBT_CTRL_ADV10G;
  268. return result;
  269. }
  270. /**
  271. * mii_10gbt_stat_mod_linkmode_lpa_t
  272. * @advertising: target the linkmode advertisement settings
  273. * @lpa: value of the C45 10GBASE-T AN STATUS register
  274. *
  275. * A small helper function that translates C45 10GBASE-T AN STATUS register bits
  276. * to linkmode advertisement settings. Other bits in advertising aren't changed.
  277. */
  278. static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
  279. u32 lpa)
  280. {
  281. linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
  282. advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
  283. linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
  284. advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
  285. linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  286. advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
  287. }
  288. int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
  289. int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  290. int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
  291. u16 mask, u16 set);
  292. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
  293. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
  294. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  295. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  296. int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
  297. u16 set);
  298. static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
  299. {
  300. return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
  301. }
  302. static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
  303. u16 regnum)
  304. {
  305. return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
  306. }
  307. static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
  308. u16 regnum, u16 val)
  309. {
  310. return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
  311. val);
  312. }
  313. static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
  314. u16 regnum)
  315. {
  316. return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
  317. }
  318. static inline int mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
  319. u16 regnum, u16 val)
  320. {
  321. return mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum), val);
  322. }
  323. int mdiobus_register_device(struct mdio_device *mdiodev);
  324. int mdiobus_unregister_device(struct mdio_device *mdiodev);
  325. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
  326. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
  327. /**
  328. * mdio_module_driver() - Helper macro for registering mdio drivers
  329. * @_mdio_driver: driver to register
  330. *
  331. * Helper macro for MDIO drivers which do not do anything special in module
  332. * init/exit. Each module may only use this macro once, and calling it
  333. * replaces module_init() and module_exit().
  334. */
  335. #define mdio_module_driver(_mdio_driver) \
  336. static int __init mdio_module_init(void) \
  337. { \
  338. return mdio_driver_register(&_mdio_driver); \
  339. } \
  340. module_init(mdio_module_init); \
  341. static void __exit mdio_module_exit(void) \
  342. { \
  343. mdio_driver_unregister(&_mdio_driver); \
  344. } \
  345. module_exit(mdio_module_exit)
  346. #endif /* __LINUX_MDIO_H__ */