phy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2011 Freescale Semiconductor, Inc.
  4. * Andy Fleming <afleming@gmail.com>
  5. *
  6. * This file pretty much stolen from Linux's mii.h/ethtool.h/phy.h
  7. */
  8. #ifndef _PHY_H
  9. #define _PHY_H
  10. #include <log.h>
  11. #include <phy_interface.h>
  12. #include <dm/ofnode.h>
  13. #include <dm/read.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include <linux/mii.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/mdio.h>
  19. struct udevice;
  20. #define PHY_FIXED_ID 0xa5a55a5a
  21. #define PHY_NCSI_ID 0xbeefcafe
  22. /*
  23. * There is no actual id for this.
  24. * This is just a dummy id for gmii2rgmmi converter.
  25. */
  26. #define PHY_GMII2RGMII_ID 0x5a5a5a5a
  27. #define PHY_MAX_ADDR 32
  28. #define PHY_FLAG_BROKEN_RESET (1 << 0) /* soft reset not supported */
  29. #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
  30. SUPPORTED_TP | \
  31. SUPPORTED_MII)
  32. #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
  33. SUPPORTED_10baseT_Full)
  34. #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
  35. SUPPORTED_100baseT_Full)
  36. #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
  37. SUPPORTED_1000baseT_Full)
  38. #define PHY_BASIC_FEATURES (PHY_10BT_FEATURES | \
  39. PHY_100BT_FEATURES | \
  40. PHY_DEFAULT_FEATURES)
  41. #define PHY_100BT1_FEATURES (SUPPORTED_TP | \
  42. SUPPORTED_MII | \
  43. SUPPORTED_100baseT_Full)
  44. #define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
  45. PHY_1000BT_FEATURES)
  46. #define PHY_10G_FEATURES (PHY_GBIT_FEATURES | \
  47. SUPPORTED_10000baseT_Full)
  48. #ifndef PHY_ANEG_TIMEOUT
  49. #define PHY_ANEG_TIMEOUT 4000
  50. #endif
  51. struct phy_device;
  52. #define MDIO_NAME_LEN 32
  53. struct mii_dev {
  54. struct list_head link;
  55. char name[MDIO_NAME_LEN];
  56. void *priv;
  57. int (*read)(struct mii_dev *bus, int addr, int devad, int reg);
  58. int (*write)(struct mii_dev *bus, int addr, int devad, int reg,
  59. u16 val);
  60. int (*reset)(struct mii_dev *bus);
  61. struct phy_device *phymap[PHY_MAX_ADDR];
  62. u32 phy_mask;
  63. };
  64. /* struct phy_driver: a structure which defines PHY behavior
  65. *
  66. * uid will contain a number which represents the PHY. During
  67. * startup, the driver will poll the PHY to find out what its
  68. * UID--as defined by registers 2 and 3--is. The 32-bit result
  69. * gotten from the PHY will be masked to
  70. * discard any bits which may change based on revision numbers
  71. * unimportant to functionality
  72. *
  73. */
  74. struct phy_driver {
  75. char *name;
  76. unsigned int uid;
  77. unsigned int mask;
  78. unsigned int mmds;
  79. u32 features;
  80. /* Called to do any driver startup necessities */
  81. /* Will be called during phy_connect */
  82. int (*probe)(struct phy_device *phydev);
  83. /* Called to configure the PHY, and modify the controller
  84. * based on the results. Should be called after phy_connect */
  85. int (*config)(struct phy_device *phydev);
  86. /* Called when starting up the controller */
  87. int (*startup)(struct phy_device *phydev);
  88. /* Called when bringing down the controller */
  89. int (*shutdown)(struct phy_device *phydev);
  90. int (*readext)(struct phy_device *phydev, int addr, int devad, int reg);
  91. int (*writeext)(struct phy_device *phydev, int addr, int devad, int reg,
  92. u16 val);
  93. /* Phy specific driver override for reading a MMD register */
  94. int (*read_mmd)(struct phy_device *phydev, int devad, int reg);
  95. /* Phy specific driver override for writing a MMD register */
  96. int (*write_mmd)(struct phy_device *phydev, int devad, int reg,
  97. u16 val);
  98. /* driver private data */
  99. ulong data;
  100. };
  101. struct phy_device {
  102. /* Information about the PHY type */
  103. /* And management functions */
  104. struct mii_dev *bus;
  105. struct phy_driver *drv;
  106. void *priv;
  107. struct udevice *dev;
  108. ofnode node;
  109. /* forced speed & duplex (no autoneg)
  110. * partner speed & duplex & pause (autoneg)
  111. */
  112. int speed;
  113. int duplex;
  114. /* The most recently read link state */
  115. int link;
  116. int port;
  117. phy_interface_t interface;
  118. u32 advertising;
  119. u32 supported;
  120. u32 mmds;
  121. int autoneg;
  122. int addr;
  123. int pause;
  124. int asym_pause;
  125. u32 phy_id;
  126. bool is_c45;
  127. u32 flags;
  128. };
  129. struct fixed_link {
  130. int phy_id;
  131. int duplex;
  132. int link_speed;
  133. int pause;
  134. int asym_pause;
  135. };
  136. /**
  137. * phy_init() - Initializes the PHY drivers
  138. * This function registers all available PHY drivers
  139. *
  140. * @return: 0 if OK, -ve on error
  141. */
  142. int phy_init(void);
  143. /**
  144. * phy_reset() - Resets the specified PHY
  145. * Issues a reset of the PHY and waits for it to complete
  146. *
  147. * @phydev: PHY to reset
  148. * @return: 0 if OK, -ve on error
  149. */
  150. int phy_reset(struct phy_device *phydev);
  151. /**
  152. * phy_find_by_mask() - Searches for a PHY on the specified MDIO bus
  153. * The function checks the PHY addresses flagged in phy_mask and returns a
  154. * phy_device pointer if it detects a PHY.
  155. * This function should only be called if just one PHY is expected to be present
  156. * in the set of addresses flagged in phy_mask. If multiple PHYs are present,
  157. * it is undefined which of these PHYs is returned.
  158. *
  159. * @bus: MII/MDIO bus to scan
  160. * @phy_mask: bitmap of PYH addresses to scan
  161. * @return: pointer to phy_device if a PHY is found, or NULL otherwise
  162. */
  163. struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask);
  164. #ifdef CONFIG_PHY_FIXED
  165. /**
  166. * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device
  167. * @node: OF node for the container of the fixed-link node
  168. *
  169. * Description: Creates a struct phy_device based on a fixed-link of_node
  170. * description. Can be used without phy_connect by drivers which do not expose
  171. * a UCLASS_ETH udevice.
  172. */
  173. struct phy_device *fixed_phy_create(ofnode node);
  174. #else
  175. static inline struct phy_device *fixed_phy_create(ofnode node)
  176. {
  177. return NULL;
  178. }
  179. #endif
  180. /**
  181. * phy_connect() - Creates a PHY device for the Ethernet interface
  182. * Creates a PHY device for the PHY at the given address, if one doesn't exist
  183. * already, and associates it with the Ethernet device.
  184. * The function may be called with addr <= 0, in this case addr value is ignored
  185. * and the bus is scanned to detect a PHY. Scanning should only be used if only
  186. * one PHY is expected to be present on the MDIO bus, otherwise it is undefined
  187. * which PHY is returned.
  188. *
  189. * @bus: MII/MDIO bus that hosts the PHY
  190. * @addr: PHY address on MDIO bus
  191. * @dev: Ethernet device to associate to the PHY
  192. * @interface: type of MAC-PHY interface
  193. * @return: pointer to phy_device if a PHY is found, or NULL otherwise
  194. */
  195. struct phy_device *phy_connect(struct mii_dev *bus, int addr,
  196. struct udevice *dev,
  197. phy_interface_t interface);
  198. /**
  199. * phy_device_create() - Create a PHY device
  200. *
  201. * @bus: MII/MDIO bus that hosts the PHY
  202. * @addr: PHY address on MDIO bus
  203. * @phy_id: where to store the ID retrieved
  204. * @is_c45: Device Identifiers if is_c45
  205. * @return: pointer to phy_device if a PHY is found, or NULL otherwise
  206. */
  207. struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
  208. u32 phy_id, bool is_c45);
  209. /**
  210. * phy_connect_phy_id() - Connect to phy device by reading PHY id
  211. * from phy node.
  212. *
  213. * @bus: MII/MDIO bus that hosts the PHY
  214. * @dev: Ethernet device to associate to the PHY
  215. * @return: pointer to phy_device if a PHY is found,
  216. * or NULL otherwise
  217. */
  218. struct phy_device *phy_connect_phy_id(struct mii_dev *bus, struct udevice *dev,
  219. int phyaddr);
  220. static inline ofnode phy_get_ofnode(struct phy_device *phydev)
  221. {
  222. if (ofnode_valid(phydev->node))
  223. return phydev->node;
  224. else
  225. return dev_ofnode(phydev->dev);
  226. }
  227. /**
  228. * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
  229. * condition is met or a timeout occurs
  230. *
  231. * @phydev: The phy_device struct
  232. * @devaddr: The MMD to read from
  233. * @regnum: The register on the MMD to read
  234. * @val: Variable to read the register into
  235. * @cond: Break condition (usually involving @val)
  236. * @sleep_us: Maximum time to sleep between reads in us (0
  237. * tight-loops). Should be less than ~20ms since usleep_range
  238. * is used (see Documentation/timers/timers-howto.rst).
  239. * @timeout_us: Timeout in us, 0 means never timeout
  240. * @sleep_before_read: if it is true, sleep @sleep_us before read.
  241. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  242. * case, the last read value at @args is stored in @val. Must not
  243. * be called from atomic context if sleep_us or timeout_us are used.
  244. */
  245. #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
  246. sleep_us, timeout_us, sleep_before_read) \
  247. ({ \
  248. int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
  249. sleep_us, timeout_us, \
  250. phydev, devaddr, regnum); \
  251. if (val < 0) \
  252. __ret = val; \
  253. if (__ret) \
  254. dev_err(phydev->dev, "%s failed: %d\n", __func__, __ret); \
  255. __ret; \
  256. })
  257. int phy_read(struct phy_device *phydev, int devad, int regnum);
  258. int phy_write(struct phy_device *phydev, int devad, int regnum, u16 val);
  259. void phy_mmd_start_indirect(struct phy_device *phydev, int devad, int regnum);
  260. int phy_read_mmd(struct phy_device *phydev, int devad, int regnum);
  261. int phy_write_mmd(struct phy_device *phydev, int devad, int regnum, u16 val);
  262. int phy_set_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
  263. int phy_clear_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
  264. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  265. u16 mask, u16 set);
  266. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  267. u16 mask, u16 set);
  268. int phy_startup(struct phy_device *phydev);
  269. int phy_config(struct phy_device *phydev);
  270. int phy_shutdown(struct phy_device *phydev);
  271. int phy_set_supported(struct phy_device *phydev, u32 max_speed);
  272. int phy_modify(struct phy_device *phydev, int devad, int regnum, u16 mask,
  273. u16 set);
  274. int genphy_config_aneg(struct phy_device *phydev);
  275. int genphy_restart_aneg(struct phy_device *phydev);
  276. int genphy_update_link(struct phy_device *phydev);
  277. int genphy_parse_link(struct phy_device *phydev);
  278. int genphy_config(struct phy_device *phydev);
  279. int genphy_startup(struct phy_device *phydev);
  280. int genphy_shutdown(struct phy_device *phydev);
  281. int gen10g_config(struct phy_device *phydev);
  282. int gen10g_startup(struct phy_device *phydev);
  283. int gen10g_shutdown(struct phy_device *phydev);
  284. int gen10g_discover_mmds(struct phy_device *phydev);
  285. /**
  286. * U_BOOT_PHY_DRIVER() - Declare a new U-Boot driver
  287. * @__name: name of the driver
  288. */
  289. #define U_BOOT_PHY_DRIVER(__name) \
  290. ll_entry_declare(struct phy_driver, __name, phy_driver)
  291. int board_phy_config(struct phy_device *phydev);
  292. int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
  293. /**
  294. * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
  295. * is RGMII (all variants)
  296. * @phydev: the phy_device struct
  297. * @return: true if MII bus is RGMII or false if it is not
  298. */
  299. static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  300. {
  301. switch (phydev->interface) {
  302. case PHY_INTERFACE_MODE_RGMII:
  303. case PHY_INTERFACE_MODE_RGMII_ID:
  304. case PHY_INTERFACE_MODE_RGMII_RXID:
  305. case PHY_INTERFACE_MODE_RGMII_TXID:
  306. return 1;
  307. default:
  308. return 0;
  309. }
  310. }
  311. bool phy_interface_is_ncsi(void);
  312. /* PHY UIDs for various PHYs that are referenced in external code */
  313. #define PHY_UID_CS4340 0x13e51002
  314. #define PHY_UID_CS4223 0x03e57003
  315. #define PHY_UID_TN2020 0x00a19410
  316. #define PHY_UID_IN112525_S03 0x02107440
  317. #endif