cvi_eth_phy.h 15 KB

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