cvi_eth_phy.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2019-2020 AlibabaGroup Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file phy.h
  6. * @brief header file for generic PHY Driver
  7. * @version V1.0
  8. * @date 21 March 2019
  9. ******************************************************************************/
  10. #ifndef _ETH_PHY_H_
  11. #define _ETH_PHY_H_
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include <stdint.h>
  16. #include "mmio.h"
  17. /**
  18. \brief Ethernet link speed
  19. */
  20. #define CSI_ETH_SPEED_10M (0) ///< 10 Mbps link speed
  21. #define CSI_ETH_SPEED_100M (1) ///< 100 Mbps link speed
  22. #define CSI_ETH_SPEED_1G (2) ///< 1 Gpbs link speed
  23. /**
  24. \brief Ethernet duplex mode
  25. */
  26. #define CSI_ETH_DUPLEX_HALF (0) ///< Half duplex link
  27. #define CSI_ETH_DUPLEX_FULL (1) ///< Full duplex link
  28. typedef void *eth_phy_handle_t;
  29. typedef enum eth_power_state
  30. {
  31. CSI_ETH_POWER_OFF, ///< Power off: no operation possible
  32. CSI_ETH_POWER_LOW, ///< Low Power mode: retain state, detect and signal wake-up events
  33. CSI_ETH_POWER_FULL ///< Power on: full operation at maximum performance
  34. } eth_power_state_t;
  35. typedef int32_t (*csi_eth_phy_read_t)(uint8_t phy_addr, uint8_t reg_addr, uint16_t *data); ///< Read Ethernet PHY Register.
  36. typedef int32_t (*csi_eth_phy_write_t)(uint8_t phy_addr, uint8_t reg_addr, uint16_t data); ///< Write Ethernet PHY Register.
  37. typedef volatile struct eth_link_info
  38. {
  39. uint32_t speed : 2; ///< Link speed: 0= 10 MBit, 1= 100 MBit, 2= 1 GBit
  40. uint32_t duplex : 1; ///< Duplex mode: 0= Half, 1= Full
  41. uint32_t autoneg : 1; ///< Set the interface to Auto Negotiation mode of transmission parameters
  42. uint32_t loopback : 1; ///< Set the interface into a Loop-back test mode
  43. uint32_t isolation : 1; ///< Set to indicate electrical isolation of PHY interface from MII/RMII interface
  44. uint32_t reserved : 26;
  45. } eth_link_info_t;
  46. typedef struct
  47. {
  48. csi_eth_phy_read_t phy_read;
  49. csi_eth_phy_write_t phy_write;
  50. eth_link_info_t link_info;
  51. } eth_phy_priv_t;
  52. typedef enum eth_link_state
  53. {
  54. ETH_LINK_DOWN, ///< Link is down
  55. ETH_LINK_UP ///< Link is up
  56. } eth_link_state_t;
  57. /* Basic mode control register */
  58. #define CVI_BMCR_RESV (0x003f)
  59. #define CVI_BMCR_SPEED1000 (0x0040)
  60. #define CVI_BMCR_CTST (0x0080)
  61. #define CVI_BMCR_FULLDPLX (0x0100)
  62. #define CVI_BMCR_ANRESTART (0x0200)
  63. #define CVI_BMCR_ISOLATE (0x0400)
  64. #define CVI_BMCR_PDOWN (0x0800)
  65. #define CVI_BMCR_ANENABLE (0x1000)
  66. #define CVI_BMCR_SPEED100 (0x2000)
  67. #define CVI_BMCR_LOOPBACK (0x4000)
  68. #define CVI_BMCR_RESET (0x8000)
  69. #define BMCR_SPEED10 (0x0000)
  70. /* Generic MII registers */
  71. #define CVI_MII_BMCR (0x00)
  72. #define CVI_MII_BMSR (0x01)
  73. #define CVI_MII_PHYSID1 (0x02)
  74. #define CVI_MII_PHYSID2 (0x03)
  75. #define CVI_MII_ADVERTISE (0x04)
  76. #define CVI_MII_LPA (0x05)
  77. #define CVI_MII_EXPANSION (0x06)
  78. #define CVI_MII_CTRL1000 (0x09)
  79. #define CVI_MII_STAT1000 (0x0a)
  80. #define MII_MMD_CTRL (0x0d)
  81. #define MII_MMD_DATA (0x0e)
  82. #define CVI_MII_ESTATUS (0x0f)
  83. #define CVI_MII_DCOUNTER (0x12)
  84. #define CVI_MII_FCSCOUNTER (0x13)
  85. #define CVI_MII_NWAYTEST (0x14)
  86. #define CVI_MII_RERRCOUNTER (0x15)
  87. #define CVI_MII_SREVISION (0x16)
  88. #define CVI_MII_RESV1 (0x17)
  89. #define CVI_MII_LBRERROR (0x18)
  90. #define CVI_MII_PHYADDR (0x19)
  91. #define CVI_MII_RESV2 (0x1a)
  92. #define CVI_MII_TPISTATUS (0x1b)
  93. #define CVI_MII_NCONFIG (0x1c)
  94. /* Advertisement control register. */
  95. #define CVI_ADVERTISE_CSMA (0x0001)
  96. #define CVI_ADVERTISE_SLCT (0x001f)
  97. #define CVI_ADVERTISE_10HALF (0x0020)
  98. #define CVI_ADVERTISE_1000XFULL (0x0020)
  99. #define CVI_ADVERTISE_10FULL (0x0040)
  100. #define CVI_ADVERTISE_1000XHALF (0x0040)
  101. #define CVI_ADVERTISE_100HALF (0x0080)
  102. #define CVI_ADVERTISE_1000XPAUSE (0x0080)
  103. #define CVI_ADVERTISE_100FULL (0x0100)
  104. #define CVI_ADVERTISE_1000XPSE_ASYM (0x0100)
  105. #define CVI_ADVERTISE_100BASE4 (0x0200)
  106. #define CVI_ADVERTISE_PAUSE_CAP (0x0400)
  107. #define CVI_ADVERTISE_PAUSE_ASYM (0x0800)
  108. #define CVI_ADVERTISE_RESV (0x1000)
  109. #define CVI_ADVERTISE_RFAULT (0x2000)
  110. #define CVI_ADVERTISE_LPACK (0x4000)
  111. #define CVI_ADVERTISE_NPAGE (0x8000)
  112. /* Basic mode status register. */
  113. #define CVI_BMSR_ERCAP (0x0001)
  114. #define CVI_BMSR_JCD (0x0002)
  115. #define CVI_BMSR_LSTATUS (0x0004)
  116. #define CVI_BMSR_ANEGCAPABLE (0x0008)
  117. #define CVI_BMSR_RFAULT (0x0010)
  118. #define CVI_BMSR_ANEGCOMPLETE (0x0020)
  119. #define CVI_BMSR_RESV (0x00c0)
  120. #define CVI_BMSR_ESTATEN (0x0100)
  121. #define CVI_BMSR_100HALF2 (0x0200)
  122. #define CVI_BMSR_100FULL2 (0x0400)
  123. #define CVI_BMSR_10HALF (0x0800)
  124. #define CVI_BMSR_10FULL (0x1000)
  125. #define CVI_BMSR_100HALF (0x2000)
  126. #define CVI_BMSR_100FULL (0x4000)
  127. #define CVI_BMSR_100BASE4 (0x8000)
  128. #define CVI_ADVERTISE_FULL (CVI_ADVERTISE_100FULL | CVI_ADVERTISE_10FULL | \
  129. CVI_ADVERTISE_CSMA)
  130. #define CVI_ADVERTISE_ALL (CVI_ADVERTISE_10HALF | CVI_ADVERTISE_10FULL | \
  131. CVI_ADVERTISE_100HALF | CVI_ADVERTISE_100FULL)
  132. /* Link partner ability register. */
  133. #define CVI_LPA_SLCT (0x001f) /* Same as advertise selector */
  134. #define CVI_LPA_10HALF (0x0020) /* Can do 10mbps half-duplex */
  135. #define CVI_LPA_1000XFULL (0x0020) /* Can do 1000BASE-X full-duplex */
  136. #define CVI_LPA_10FULL (0x0040) /* Can do 10mbps full-duplex */
  137. #define CVI_LPA_1000XHALF (0x0040) /* Can do 1000BASE-X half-duplex */
  138. #define CVI_LPA_100HALF (0x0080) /* Can do 100mbps half-duplex */
  139. #define CVI_LPA_1000XPAUSE (0x0080) /* Can do 1000BASE-X pause */
  140. #define CVI_LPA_100FULL (0x0100) /* Can do 100mbps full-duplex */
  141. #define CVI_LPA_1000XPAUSE_ASYM (0x0100) /* Can do 1000BASE-X pause asym */
  142. #define CVI_LPA_100BASE4 (0x0200) /* Can do 100mbps 4k packets */
  143. #define CVI_LPA_PAUSE_CAP (0x0400) /* Can pause */
  144. #define CVI_LPA_PAUSE_ASYM (0x0800) /* Can pause asymetrically */
  145. #define CVI_LPA_RESV (0x1000) /* Unused */
  146. #define CVI_LPA_RFAULT (0x2000) /* Link partner faulted */
  147. #define CVI_LPA_LPACK (0x4000) /* Link partner acked us */
  148. #define CVI_LPA_NPAGE (0x8000) /* Next page bit */
  149. #define CVI_LPA_DUPLEX (CVI_LPA_10FULL | CVI_LPA_100FULL)
  150. #define CVI_LPA_100 (CVI_LPA_100FULL | CVI_LPA_100HALF | CVI_LPA_100BASE4)
  151. /* Expansion register for auto-negotiation. */
  152. #define CVI_EXPANSION_NWAY (0x0001) /* Can do N-way auto-nego */
  153. #define CVI_EXPANSION_LCWP (0x0002) /* Got new RX page code word */
  154. #define CVI_EXPANSION_ENABLENPAGE (0x0004) /* This enables npage words */
  155. #define CVI_EXPANSION_NPCAPABLE (0x0008) /* Link partner supports npage */
  156. #define CVI_EXPANSION_MFAULTS (0x0010) /* Multiple faults detected */
  157. #define CVI_EXPANSION_RESV (0xffe0) /* Unused */
  158. #define CVI_ESTATUS_1000_XFULL (0x8000) /* Can do 1000BX Full */
  159. #define CVI_ESTATUS_1000_XHALF (0x4000) /* Can do 1000BX Half */
  160. #define CVI_ESTATUS_1000_TFULL (0x2000) /* Can do 1000BT Full */
  161. #define CVI_ESTATUS_1000_THALF (0x1000) /* Can do 1000BT Half */
  162. /* N-way test register. */
  163. #define CVI_NWAYTEST_RESV1 (0x00ff) /* Unused */
  164. #define CVI_NWAYTEST_LOOPBACK (0x0100) /* Enable loopback for N-way */
  165. #define CVI_NWAYTEST_RESV2 (0xfe00) /* Unused */
  166. /* 1000BASE-T Control register */
  167. #define CVI_ADVERTISE_1000FULL 0x0200 /* Advertise 1000BASE-T full duplex */
  168. #define CVI_ADVERTISE_1000HALF 0x0100 /* Advertise 1000BASE-T half duplex */
  169. #define CTL1000_AS_MASTER 0x0800
  170. #define CTL1000_ENABLE_MASTER 0x1000
  171. /* 1000BASE-T Status register */
  172. #define CVI_LPA_1000LOCALRXOK 0x2000 /* Link partner local receiver status */
  173. #define CVI_LPA_1000REMRXOK 0x1000 /* Link partner remote receiver status */
  174. #define CVI_LPA_1000FULL 0x0800 /* Link partner 1000BASE-T full duplex */
  175. #define CVI_LPA_1000HALF 0x0400 /* Link partner 1000BASE-T half duplex */
  176. /* Flow control flags */
  177. #define CVI_FLOW_CTRL_TX 0x01
  178. #define CVI_FLOW_CTRL_RX 0x02
  179. /* MMD Access Control register fields */
  180. #define CVI_MII_MMD_CTRL_DEVAD_MASK 0x1f /* Mask MMD DEVAD*/
  181. #define CVI_MII_MMD_CTRL_ADDR 0x0000 /* Address */
  182. #define CVI_MII_MMD_CTRL_NOINCR 0x4000 /* no post increment */
  183. #define CVI_MII_MMD_CTRL_INCR_RDWT 0x8000 /* post increment on reads & writes */
  184. #define CVI_MII_MMD_CTRL_INCR_ON_WT 0xC000 /* post increment on writes only */
  185. /* Indicates what features are advertised by the interface. */
  186. #define CVI_ADVERTISED_10baseT_Half (1 << 0)
  187. #define CVI_ADVERTISED_10baseT_Full (1 << 1)
  188. #define CVI_ADVERTISED_100baseT_Half (1 << 2)
  189. #define CVI_ADVERTISED_100baseT_Full (1 << 3)
  190. #define CVI_ADVERTISED_1000baseT_Half (1 << 4)
  191. #define CVI_ADVERTISED_1000baseT_Full (1 << 5)
  192. #define CVI_ADVERTISED_Autoneg (1 << 6)
  193. #define CVI_ADVERTISED_TP (1 << 7)
  194. #define CVI_ADVERTISED_AUI (1 << 8)
  195. #define CVI_ADVERTISED_MII (1 << 9)
  196. #define CVI_ADVERTISED_FIBRE (1 << 10)
  197. #define CVI_ADVERTISED_BNC (1 << 11)
  198. #define CVI_ADVERTISED_10000baseT_Full (1 << 12)
  199. #define CVI_ADVERTISED_Pause (1 << 13)
  200. #define CVI_ADVERTISED_Asym_Pause (1 << 14)
  201. #define CVI_ADVERTISED_2500baseX_Full (1 << 15)
  202. #define CVI_ADVERTISED_Backplane (1 << 16)
  203. #define CVI_ADVERTISED_1000baseKX_Full (1 << 17)
  204. #define CVI_ADVERTISED_10000baseKX4_Full (1 << 18)
  205. #define CVI_ADVERTISED_10000baseKR_Full (1 << 19)
  206. #define CVI_ADVERTISED_10000baseR_FEC (1 << 20)
  207. #define CVI_ADVERTISED_1000baseX_Half (1 << 21)
  208. #define CVI_ADVERTISED_1000baseX_Full (1 << 22)
  209. /* Indicates what features are supported by the interface. */
  210. #define CVI_SUPPORTED_10baseT_Half (1 << 0)
  211. #define CVI_SUPPORTED_10baseT_Full (1 << 1)
  212. #define CVI_SUPPORTED_100baseT_Half (1 << 2)
  213. #define CVI_SUPPORTED_100baseT_Full (1 << 3)
  214. #define CVI_SUPPORTED_1000baseT_Half (1 << 4)
  215. #define CVI_SUPPORTED_1000baseT_Full (1 << 5)
  216. #define CVI_SUPPORTED_Autoneg (1 << 6)
  217. #define CVI_SUPPORTED_TP (1 << 7)
  218. #define CVI_SUPPORTED_AUI (1 << 8)
  219. #define CVI_SUPPORTED_MII (1 << 9)
  220. #define CVI_SUPPORTED_FIBRE (1 << 10)
  221. #define CVI_SUPPORTED_BNC (1 << 11)
  222. #define CVI_SUPPORTED_10000baseT_Full (1 << 12)
  223. #define CVI_SUPPORTED_Pause (1 << 13)
  224. #define CVI_SUPPORTED_Asym_Pause (1 << 14)
  225. #define CVI_SUPPORTED_2500baseX_Full (1 << 15)
  226. #define CVI_SUPPORTED_Backplane (1 << 16)
  227. #define CVI_SUPPORTED_1000baseKX_Full (1 << 17)
  228. #define CVI_SUPPORTED_10000baseKX4_Full (1 << 18)
  229. #define CVI_SUPPORTED_10000baseKR_Full (1 << 19)
  230. #define CVI_SUPPORTED_10000baseR_FEC (1 << 20)
  231. #define CVI_SUPPORTED_1000baseX_Half (1 << 21)
  232. #define CVI_SUPPORTED_1000baseX_Full (1 << 22)
  233. /* PHY features */
  234. #define CVI_PHY_DEFAULT_FEATURES (CVI_SUPPORTED_Autoneg | \
  235. CVI_SUPPORTED_TP | \
  236. CVI_SUPPORTED_MII)
  237. #define CVI_PHY_10BT_FEATURES (CVI_SUPPORTED_10baseT_Half | \
  238. CVI_SUPPORTED_10baseT_Full)
  239. #define CVI_PHY_100BT_FEATURES (CVI_SUPPORTED_100baseT_Half | \
  240. CVI_SUPPORTED_100baseT_Full)
  241. #define CVI_PHY_1000BT_FEATURES (CVI_SUPPORTED_1000baseT_Half | \
  242. CVI_SUPPORTED_1000baseT_Full)
  243. #define CVI_PHY_BASIC_FEATURES (CVI_PHY_10BT_FEATURES | \
  244. CVI_PHY_100BT_FEATURES | \
  245. CVI_PHY_DEFAULT_FEATURES)
  246. #define CVI_PHY_GBIT_FEATURES (CVI_PHY_BASIC_FEATURES | \
  247. CVI_PHY_1000BT_FEATURES)
  248. #define CVI_PHY_ANEG_TIMEOUT 5000 /* in ms */
  249. typedef enum {
  250. LOOPBACK_XMII2MAC,
  251. LOOPBACK_PCS2MAC,
  252. LOOPBACK_PMA2MAC,
  253. LOOPBACK_RMII2PHY,
  254. } phy_loopback_mode_t;
  255. /* phy interface mode */
  256. typedef enum {
  257. PHY_IF_MODE_MII,
  258. PHY_IF_MODE_GMII,
  259. PHY_IF_MODE_SGMII,
  260. PHY_IF_MODE_TBI,
  261. PHY_IF_MODE_RMII,
  262. PHY_IF_MODE_RGMII,
  263. PHY_IF_MODE_RGMII_ID,
  264. PHY_IF_MODE_RGMII_RXID,
  265. PHY_IF_MODE_RGMII_TXID,
  266. PHY_IF_MODE_RTBI,
  267. PHY_IF_MODE_NONE, /* Last One */
  268. PHY_IF_MODE_COUNT,
  269. } phy_if_mode_t;
  270. typedef struct {
  271. eth_phy_priv_t *priv;
  272. eth_link_state_t link_state;
  273. uint32_t supported;
  274. uint32_t advertising;
  275. /*
  276. * platform specific
  277. */
  278. uint32_t phy_addr;
  279. phy_if_mode_t interface;
  280. /*
  281. * driver specific
  282. */
  283. uint32_t phy_id;
  284. uint32_t mask;
  285. uint32_t features;
  286. int8_t name[20];
  287. /* config() should be called before calling start() */
  288. int32_t (*config)(eth_phy_handle_t *phy_dev);
  289. int32_t (*start)(eth_phy_handle_t *phy_dev);
  290. int32_t (*stop)(eth_phy_handle_t *phy_dev);
  291. int32_t (*loopback)(eth_phy_handle_t *phy_dev);
  292. int32_t (*update_link)(eth_phy_handle_t *phy_dev);
  293. } eth_phy_dev_t;
  294. /* ethernet phy config */
  295. #define ETH_PHY_BASE 0x03009000
  296. #define ETH_PHY_INIT_MASK 0xFFFFFFF9
  297. #define ETH_PHY_SHUTDOWN (1 << 1)
  298. #define ETH_PHY_POWERUP 0xFFFFFFFD
  299. #define ETH_PHY_RESET 0xFFFFFFFB
  300. #define ETH_PHY_RESET_N (1 << 2)
  301. #define ETH_PHY_LED_LOW_ACTIVE (1 << 3)
  302. int generic_phy_config_aneg(eth_phy_dev_t *dev);
  303. int generic_phy_restart_aneg(eth_phy_dev_t *dev);
  304. int generic_phy_update_link(eth_phy_dev_t *dev);
  305. int32_t eth_phy_read(eth_phy_priv_t *priv, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data);
  306. int32_t eth_phy_write(eth_phy_priv_t *priv, uint8_t phy_addr, uint8_t reg_addr, uint16_t data);
  307. int32_t eth_phy_reset(eth_phy_handle_t handle);
  308. int32_t eth_phy_config(eth_phy_handle_t handle);
  309. int32_t eth_phy_start(eth_phy_handle_t handle);
  310. int32_t eth_phy_update_link(eth_phy_handle_t handle);
  311. int32_t genphy_config(eth_phy_dev_t *phy_dev);
  312. int32_t genphy_update_link(eth_phy_dev_t *phy_dev);
  313. /*
  314. * ffs: find first bit set. This is defined the same way as
  315. * the libc and compiler builtin ffs routines, therefore
  316. * differs in spirit from the above ffz (man ffs).
  317. */
  318. // static inline int32_t ffs(int32_t x)
  319. // {
  320. // int32_t r = 1;
  321. // if (!x)
  322. // return 0;
  323. // if (!(x & 0xffff)) {
  324. // x >>= 16;
  325. // r += 16;
  326. // }
  327. // if (!(x & 0xff)) {
  328. // x >>= 8;
  329. // r += 8;
  330. // }
  331. // if (!(x & 0xf)) {
  332. // x >>= 4;
  333. // r += 4;
  334. // }
  335. // if (!(x & 3)) {
  336. // x >>= 2;
  337. // r += 2;
  338. // }
  339. // if (!(x & 1)) {
  340. // x >>= 1;
  341. // r += 1;
  342. // }
  343. // return r;
  344. // }
  345. #ifdef __cplusplus
  346. }
  347. #endif
  348. #endif /* _ETH_PHY_H_ */