mdio.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * linux/mdio.h: definitions for MDIO (clause 45) transceivers
  3. * Copyright 2006-2009 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #ifndef __LINUX_MDIO_H__
  10. #define __LINUX_MDIO_H__
  11. #include <uapi/linux/mdio.h>
  12. #include <linux/mod_devicetable.h>
  13. struct gpio_desc;
  14. struct mii_bus;
  15. /* Multiple levels of nesting are possible. However typically this is
  16. * limited to nested DSA like layer, a MUX layer, and the normal
  17. * user. Instead of trying to handle the general case, just define
  18. * these cases.
  19. */
  20. enum mdio_mutex_lock_class {
  21. MDIO_MUTEX_NORMAL,
  22. MDIO_MUTEX_MUX,
  23. MDIO_MUTEX_NESTED,
  24. };
  25. struct mdio_device {
  26. struct device dev;
  27. const struct dev_pm_ops *pm_ops;
  28. struct mii_bus *bus;
  29. char modalias[MDIO_NAME_SIZE];
  30. int (*bus_match)(struct device *dev, struct device_driver *drv);
  31. void (*device_free)(struct mdio_device *mdiodev);
  32. void (*device_remove)(struct mdio_device *mdiodev);
  33. /* Bus address of the MDIO device (0-31) */
  34. int addr;
  35. int flags;
  36. struct gpio_desc *reset;
  37. };
  38. #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
  39. /* struct mdio_driver_common: Common to all MDIO drivers */
  40. struct mdio_driver_common {
  41. struct device_driver driver;
  42. int flags;
  43. };
  44. #define MDIO_DEVICE_FLAG_PHY 1
  45. #define to_mdio_common_driver(d) \
  46. container_of(d, struct mdio_driver_common, driver)
  47. /* struct mdio_driver: Generic MDIO driver */
  48. struct mdio_driver {
  49. struct mdio_driver_common mdiodrv;
  50. /*
  51. * Called during discovery. Used to set
  52. * up device-specific structures, if any
  53. */
  54. int (*probe)(struct mdio_device *mdiodev);
  55. /* Clears up any memory if needed */
  56. void (*remove)(struct mdio_device *mdiodev);
  57. };
  58. #define to_mdio_driver(d) \
  59. container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
  60. void mdio_device_free(struct mdio_device *mdiodev);
  61. struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
  62. int mdio_device_register(struct mdio_device *mdiodev);
  63. void mdio_device_remove(struct mdio_device *mdiodev);
  64. void mdio_device_reset(struct mdio_device *mdiodev, int value);
  65. int mdio_driver_register(struct mdio_driver *drv);
  66. void mdio_driver_unregister(struct mdio_driver *drv);
  67. int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
  68. static inline bool mdio_phy_id_is_c45(int phy_id)
  69. {
  70. return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
  71. }
  72. static inline __u16 mdio_phy_id_prtad(int phy_id)
  73. {
  74. return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
  75. }
  76. static inline __u16 mdio_phy_id_devad(int phy_id)
  77. {
  78. return phy_id & MDIO_PHY_ID_DEVAD;
  79. }
  80. /**
  81. * struct mdio_if_info - Ethernet controller MDIO interface
  82. * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
  83. * @mmds: Mask of MMDs expected to be present in the PHY. This must be
  84. * non-zero unless @prtad = %MDIO_PRTAD_NONE.
  85. * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
  86. * MII register access will be passed through with @devad =
  87. * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
  88. * commonly used clause 22 registers will be translated into
  89. * clause 45 registers.
  90. * @dev: Net device structure
  91. * @mdio_read: Register read function; returns value or negative error code
  92. * @mdio_write: Register write function; returns 0 or negative error code
  93. */
  94. struct mdio_if_info {
  95. int prtad;
  96. u32 mmds;
  97. unsigned mode_support;
  98. struct net_device *dev;
  99. int (*mdio_read)(struct net_device *dev, int prtad, int devad,
  100. u16 addr);
  101. int (*mdio_write)(struct net_device *dev, int prtad, int devad,
  102. u16 addr, u16 val);
  103. };
  104. #define MDIO_PRTAD_NONE (-1)
  105. #define MDIO_DEVAD_NONE (-1)
  106. #define MDIO_SUPPORTS_C22 1
  107. #define MDIO_SUPPORTS_C45 2
  108. #define MDIO_EMULATE_C22 4
  109. struct ethtool_cmd;
  110. struct ethtool_pauseparam;
  111. extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
  112. extern int mdio_set_flag(const struct mdio_if_info *mdio,
  113. int prtad, int devad, u16 addr, int mask,
  114. bool sense);
  115. extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
  116. extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
  117. extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
  118. struct ethtool_cmd *ecmd,
  119. u32 npage_adv, u32 npage_lpa);
  120. extern void
  121. mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
  122. struct ethtool_link_ksettings *cmd,
  123. u32 npage_adv, u32 npage_lpa);
  124. /**
  125. * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
  126. * @mdio: MDIO interface
  127. * @ecmd: Ethtool request structure
  128. *
  129. * Since the CSRs for auto-negotiation using next pages are not fully
  130. * standardised, this function does not attempt to decode them. Use
  131. * mdio45_ethtool_gset_npage() to specify advertisement bits from next
  132. * pages.
  133. */
  134. static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
  135. struct ethtool_cmd *ecmd)
  136. {
  137. mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
  138. }
  139. /**
  140. * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
  141. * @mdio: MDIO interface
  142. * @cmd: Ethtool request structure
  143. *
  144. * Since the CSRs for auto-negotiation using next pages are not fully
  145. * standardised, this function does not attempt to decode them. Use
  146. * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
  147. * from next pages.
  148. */
  149. static inline void
  150. mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
  151. struct ethtool_link_ksettings *cmd)
  152. {
  153. mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
  154. }
  155. extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
  156. struct mii_ioctl_data *mii_data, int cmd);
  157. /**
  158. * mmd_eee_cap_to_ethtool_sup_t
  159. * @eee_cap: value of the MMD EEE Capability register
  160. *
  161. * A small helper function that translates MMD EEE Capability (3.20) bits
  162. * to ethtool supported settings.
  163. */
  164. static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
  165. {
  166. u32 supported = 0;
  167. if (eee_cap & MDIO_EEE_100TX)
  168. supported |= SUPPORTED_100baseT_Full;
  169. if (eee_cap & MDIO_EEE_1000T)
  170. supported |= SUPPORTED_1000baseT_Full;
  171. if (eee_cap & MDIO_EEE_10GT)
  172. supported |= SUPPORTED_10000baseT_Full;
  173. if (eee_cap & MDIO_EEE_1000KX)
  174. supported |= SUPPORTED_1000baseKX_Full;
  175. if (eee_cap & MDIO_EEE_10GKX4)
  176. supported |= SUPPORTED_10000baseKX4_Full;
  177. if (eee_cap & MDIO_EEE_10GKR)
  178. supported |= SUPPORTED_10000baseKR_Full;
  179. return supported;
  180. }
  181. /**
  182. * mmd_eee_adv_to_ethtool_adv_t
  183. * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
  184. *
  185. * A small helper function that translates the MMD EEE Advertisment (7.60)
  186. * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
  187. * settings.
  188. */
  189. static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
  190. {
  191. u32 adv = 0;
  192. if (eee_adv & MDIO_EEE_100TX)
  193. adv |= ADVERTISED_100baseT_Full;
  194. if (eee_adv & MDIO_EEE_1000T)
  195. adv |= ADVERTISED_1000baseT_Full;
  196. if (eee_adv & MDIO_EEE_10GT)
  197. adv |= ADVERTISED_10000baseT_Full;
  198. if (eee_adv & MDIO_EEE_1000KX)
  199. adv |= ADVERTISED_1000baseKX_Full;
  200. if (eee_adv & MDIO_EEE_10GKX4)
  201. adv |= ADVERTISED_10000baseKX4_Full;
  202. if (eee_adv & MDIO_EEE_10GKR)
  203. adv |= ADVERTISED_10000baseKR_Full;
  204. return adv;
  205. }
  206. /**
  207. * ethtool_adv_to_mmd_eee_adv_t
  208. * @adv: the ethtool advertisement settings
  209. *
  210. * A small helper function that translates ethtool advertisement settings
  211. * to EEE advertisements for the MMD EEE Advertisement (7.60) and
  212. * MMD EEE Link Partner Ability (7.61) registers.
  213. */
  214. static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
  215. {
  216. u16 reg = 0;
  217. if (adv & ADVERTISED_100baseT_Full)
  218. reg |= MDIO_EEE_100TX;
  219. if (adv & ADVERTISED_1000baseT_Full)
  220. reg |= MDIO_EEE_1000T;
  221. if (adv & ADVERTISED_10000baseT_Full)
  222. reg |= MDIO_EEE_10GT;
  223. if (adv & ADVERTISED_1000baseKX_Full)
  224. reg |= MDIO_EEE_1000KX;
  225. if (adv & ADVERTISED_10000baseKX4_Full)
  226. reg |= MDIO_EEE_10GKX4;
  227. if (adv & ADVERTISED_10000baseKR_Full)
  228. reg |= MDIO_EEE_10GKR;
  229. return reg;
  230. }
  231. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
  232. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
  233. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  234. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  235. int mdiobus_register_device(struct mdio_device *mdiodev);
  236. int mdiobus_unregister_device(struct mdio_device *mdiodev);
  237. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
  238. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
  239. /**
  240. * mdio_module_driver() - Helper macro for registering mdio drivers
  241. *
  242. * Helper macro for MDIO drivers which do not do anything special in module
  243. * init/exit. Each module may only use this macro once, and calling it
  244. * replaces module_init() and module_exit().
  245. */
  246. #define mdio_module_driver(_mdio_driver) \
  247. static int __init mdio_module_init(void) \
  248. { \
  249. return mdio_driver_register(&_mdio_driver); \
  250. } \
  251. module_init(mdio_module_init); \
  252. static void __exit mdio_module_exit(void) \
  253. { \
  254. mdio_driver_unregister(&_mdio_driver); \
  255. } \
  256. module_exit(mdio_module_exit)
  257. #endif /* __LINUX_MDIO_H__ */