phy.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-14 wangqiang the first version
  9. * 2022-08-17 xjy198903 add 1000M definition
  10. * 2024-10-08 zhujiale add phy v2.0
  11. */
  12. #ifndef __NET_PHY_H__
  13. #define __NET_PHY_H__
  14. #include <rtthread.h>
  15. #include <drivers/core/driver.h>
  16. #ifdef RT_USING_PHY_V2
  17. #include <ofw.h>
  18. #include <mdio.h>
  19. #include <general_phy.h>
  20. #define RT_PHY_FIXED_ID 0xa5a55a5a
  21. #define RT_PHY_NCSI_ID 0xbeefcafe
  22. /* Indicates what features are supported by the interface. */
  23. #define RT_SUPPORTED_10baseT_Half (1 << 0)
  24. #define RT_SUPPORTED_10baseT_Full (1 << 1)
  25. #define RT_SUPPORTED_100baseT_Half (1 << 2)
  26. #define RT_SUPPORTED_100baseT_Full (1 << 3)
  27. #define RT_SUPPORTED_1000baseT_Half (1 << 4)
  28. #define RT_SUPPORTED_1000baseT_Full (1 << 5)
  29. #define RT_SUPPORTED_Autoneg (1 << 6)
  30. #define RT_SUPPORTED_TP (1 << 7)
  31. #define RT_SUPPORTED_AUI (1 << 8)
  32. #define RT_SUPPORTED_MII (1 << 9)
  33. #define RT_SUPPORTED_FIBRE (1 << 10)
  34. #define RT_SUPPORTED_BNC (1 << 11)
  35. #define RT_SUPPORTED_10000baseT_Full (1 << 12)
  36. #define RT_SUPPORTED_Pause (1 << 13)
  37. #define RT_SUPPORTED_Asym_Pause (1 << 14)
  38. #define RT_SUPPORTED_2500baseX_Full (1 << 15)
  39. #define RT_SUPPORTED_Backplane (1 << 16)
  40. #define RT_SUPPORTED_1000baseKX_Full (1 << 17)
  41. #define RT_SUPPORTED_10000baseKX4_Full (1 << 18)
  42. #define RT_SUPPORTED_10000baseKR_Full (1 << 19)
  43. #define RT_SUPPORTED_10000baseR_FEC (1 << 20)
  44. #define RT_SUPPORTED_1000baseX_Half (1 << 21)
  45. #define RT_SUPPORTED_1000baseX_Full (1 << 22)
  46. #define RT_PHY_FLAG_BROKEN_RESET (1 << 0) /* soft reset not supported */
  47. #define RT_PHY_DEFAULT_FEATURES (RT_SUPPORTED_Autoneg | RT_SUPPORTED_TP | RT_SUPPORTED_MII)
  48. #define RT_PHY_10BT_FEATURES (RT_SUPPORTED_10baseT_Half | RT_SUPPORTED_10baseT_Full)
  49. #define RT_PHY_100BT_FEATURES (RT_SUPPORTED_100baseT_Half | RT_SUPPORTED_100baseT_Full)
  50. #define RT_PHY_1000BT_FEATURES (RT_SUPPORTED_1000baseT_Half | RT_SUPPORTED_1000baseT_Full)
  51. #define RT_PHY_BASIC_FEATURES (RT_PHY_10BT_FEATURES | RT_PHY_100BT_FEATURES | RT_PHY_DEFAULT_FEATURES)
  52. #define RT_PHY_GBIT_FEATURES (RT_PHY_BASIC_FEATURES | RT_PHY_1000BT_FEATURES)
  53. #define RT_PHY_10G_FEATURES (RT_PHY_GBIT_FEATURES | RT_SUPPORTED_10000baseT_Full)
  54. struct rt_phy_device
  55. {
  56. struct rt_device parent;
  57. struct mii_bus *bus;
  58. struct rt_phy_driver *drv;
  59. rt_uint32_t phy_id;
  60. rt_uint32_t mmds;
  61. int speed;
  62. int duplex;
  63. int link;
  64. int port;
  65. rt_uint32_t advertising;
  66. rt_uint32_t supported;
  67. rt_bool_t autoneg;
  68. int pause;
  69. rt_ubase_t addr;
  70. rt_bool_t is_c45;
  71. rt_uint32_t flags;
  72. rt_phy_interface interface;
  73. #ifdef RT_USING_OFW
  74. struct rt_ofw_node *node;
  75. #endif
  76. void *priv;
  77. };
  78. struct rt_phy_driver
  79. {
  80. struct rt_driver parent;
  81. char name[RT_NAME_MAX];
  82. rt_uint64_t uid;
  83. rt_uint64_t mask;
  84. rt_uint64_t mmds;
  85. rt_uint32_t features;
  86. int (*probe)(struct rt_phy_device *phydev);
  87. int (*config)(struct rt_phy_device *phydev);
  88. int (*startup)(struct rt_phy_device *phydev);
  89. int (*shutdown)(struct rt_phy_device *phydev);
  90. int (*read)(struct rt_phy_device *phydev, int addr, int devad, int reg);
  91. int (*write)(struct rt_phy_device *phydev, int addr, int devad, int reg,
  92. rt_uint16_t val);
  93. int (*read_mmd)(struct rt_phy_device *phydev, int devad, int reg);
  94. int (*write_mmd)(struct rt_phy_device *phydev, int devad, int reg,
  95. rt_uint16_t val);
  96. /* driver private data */
  97. void *data;
  98. };
  99. int rt_phy_read(struct rt_phy_device *phydev, int devad, int regnum);
  100. int rt_phy_write(struct rt_phy_device *phydev, int devad, int regnum, rt_uint16_t val);
  101. int rt_phy_read_mmd(struct rt_phy_device *phydev, int devad, int regnum);
  102. int rt_phy_write_mmd(struct rt_phy_device *phydev, int devad, int regnum, rt_uint16_t val);
  103. int rt_phy_reset(struct rt_phy_device *phydev);
  104. int rt_phy_startup(struct rt_phy_device *phydev);
  105. int rt_phy_config(struct rt_phy_device *phydev);
  106. int rt_phy_shutdown(struct rt_phy_device *phydev);
  107. int rt_phy_read_mmd(struct rt_phy_device *phydev, int devad, int regnum);
  108. int rt_phy_set_supported(struct rt_phy_device *phydev, rt_uint32_t max_speed);
  109. void rt_phy_mmd_start_indirect(struct rt_phy_device *phydev, int devad, int regnum);
  110. rt_err_t rt_phy_device_register(struct rt_phy_device *pdev);
  111. rt_err_t rt_phy_driver_register(struct rt_phy_driver *pdrv);
  112. rt_err_t rt_ofw_get_phyid(struct rt_ofw_node *np,rt_uint32_t *id);
  113. struct rt_phy_device *rt_phy_device_create(struct mii_bus *bus, int addr, rt_uint32_t phy_id, rt_bool_t is_c45);
  114. struct rt_phy_device *rt_phy_find_by_mask(struct mii_bus *bus, unsigned int phy_mask);
  115. struct rt_phy_device *rt_ofw_create_phy(struct mii_bus *bus, struct rt_ofw_node *np, int phyaddr);
  116. struct rt_phy_device *rt_phy_get_device(struct mii_bus *bus, struct rt_ofw_node *np, int addr, rt_phy_interface interface);
  117. #define RT_PHY_DEVICE_REGISTER(phy_dev) \
  118. static int rt_##phy_dev##_register(void) \
  119. { \
  120. rt_phy_device_register(&phy_dev); \
  121. return 0; \
  122. } \
  123. INIT_PREV_EXPORT(rt_##phy_dev##_register);
  124. #define RT_PHY_DRIVER_REGISTER(phy_drv) \
  125. static int rt_##phy_drv##_register(void) \
  126. { \
  127. rt_phy_driver_register(&phy_drv); \
  128. return 0; \
  129. } \
  130. INIT_PREV_EXPORT(rt_##phy_drv##_register);
  131. #endif
  132. #ifdef RT_USING_PHY
  133. #ifdef __cplusplus
  134. extern "C"
  135. {
  136. #endif
  137. struct rt_mdio_bus_ops
  138. {
  139. rt_bool_t (*init)(void *bus, rt_uint32_t src_clock_hz);
  140. rt_size_t (*read)(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size);
  141. rt_size_t (*write)(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size);
  142. rt_bool_t (*uninit)(void *bus);
  143. };
  144. struct rt_mdio_bus
  145. {
  146. void *hw_obj;
  147. char *name;
  148. struct rt_mdio_bus_ops *ops;
  149. };
  150. typedef struct rt_mdio_bus rt_mdio_t;
  151. /* Defines the PHY link speed. This is align with the speed for MAC. */
  152. #define PHY_SPEED_10M 0U /* PHY 10M speed. */
  153. #define PHY_SPEED_100M 1U /* PHY 100M speed. */
  154. #define PHY_SPEED_1000M 2U /* PHY 1000M speed. */
  155. /* Defines the PHY link duplex. */
  156. #define PHY_HALF_DUPLEX 0U /* PHY half duplex. */
  157. #define PHY_FULL_DUPLEX 1U /* PHY full duplex. */
  158. /*! @brief Defines the PHY loopback mode. */
  159. #define PHY_LOCAL_LOOP 0U /* PHY local loopback. */
  160. #define PHY_REMOTE_LOOP 1U /* PHY remote loopback. */
  161. #define PHY_STATUS_OK 0U
  162. #define PHY_STATUS_FAIL 1U
  163. #define PHY_STATUS_TIMEOUT 2U
  164. typedef struct rt_phy_msg
  165. {
  166. rt_uint32_t reg;
  167. rt_uint32_t value;
  168. }rt_phy_msg_t;
  169. typedef struct rt_phy_device
  170. {
  171. struct rt_device parent;
  172. struct rt_mdio_bus *bus;
  173. rt_uint32_t addr;
  174. struct rt_phy_ops *ops;
  175. }rt_phy_t;
  176. typedef rt_int32_t rt_phy_status;
  177. struct rt_phy_ops
  178. {
  179. rt_phy_status (*init)(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz);
  180. rt_phy_status (*read)(rt_phy_t *phy, rt_uint32_t reg, rt_uint32_t *data);
  181. rt_phy_status (*write)(rt_phy_t *phy, rt_uint32_t reg, rt_uint32_t data);
  182. rt_phy_status (*loopback)(rt_phy_t *phy, rt_uint32_t mode, rt_uint32_t speed, rt_bool_t enable);
  183. rt_phy_status (*get_link_status)(rt_phy_t *phy, rt_bool_t *status);
  184. rt_phy_status (*get_link_speed_duplex)(rt_phy_t *phy, rt_uint32_t *speed, rt_uint32_t *duplex);
  185. };
  186. rt_err_t rt_hw_phy_register(struct rt_phy_device *phy, const char *name);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif
  191. #endif