dw_eth_mac.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #ifndef _DW_GMAC_182x_H_
  17. #define _DW_GMAC_182x_H_
  18. #include "cvi_eth_phy.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #ifndef __aligned
  23. #define __aligned(x) __attribute__((aligned(x)))
  24. #endif
  25. typedef void *eth_mac_handle_t;
  26. #define CSI_ETH_MAC_CONFIGURE (0x01) ///< Configure MAC; arg = configuration
  27. #define CSI_ETH_MAC_CONTROL_TX (0x02) ///< Transmitter; arg: 0=disabled (default), 1=enabled
  28. #define CSI_ETH_MAC_CONTROL_RX (0x03) ///< Receiver; arg: 0=disabled (default), 1=enabled
  29. #define CSI_ETH_MAC_FLUSH (0x04) ///< Flush buffer; arg = CSI_ETH_MAC_FLUSH_...
  30. #define CSI_ETH_MAC_SLEEP (0x05) ///< Sleep mode; arg: 1=enter and wait for Magic packet, 0=exit
  31. #define CSI_ETH_MAC_VLAN_FILTER (0x06) ///< VLAN Filter for received frames; arg15..0: VLAN Tag; arg16: optional CSI_ETH_MAC_VLAN_FILTER_ID_ONLY; 0=disabled (default)
  32. #define DRV_ETH_MAC_ADJUST_LINK (0x07) ///< Adjust MAC link state according to phy state; arg: phy handle
  33. #define DRV_ETH_MAC_CONTROL_IRQ (0x08) ///< Interrupt request; arg: 0=disable, 1=enable
  34. #define DW_GMAC_DMA_ALIGN 128
  35. #ifndef _DW_ETH_H
  36. #define _DW_ETH_H
  37. #define CVI_CONFIG_SYS_HZ 1000
  38. #define CVI_CONFIG_TX_DESCR_NUM 16
  39. #define CVI_CONFIG_RX_DESCR_NUM 16
  40. #define CVI_CONFIG_ETH_BUFSIZE 2048
  41. #define CVI_TX_TOTAL_BUFSIZE (CVI_CONFIG_ETH_BUFSIZE * CVI_CONFIG_TX_DESCR_NUM)
  42. #define CVI_RX_TOTAL_BUFSIZE (CVI_CONFIG_ETH_BUFSIZE * CVI_CONFIG_RX_DESCR_NUM)
  43. #define CVI_CONFIG_MACRESET_TIMEOUT (3 * CVI_CONFIG_SYS_HZ)
  44. #define CVI_CONFIG_MDIO_TIMEOUT (3 * CVI_CONFIG_SYS_HZ)
  45. struct dw_gmac_mac_regs {
  46. volatile uint32_t conf; /* 0x00 */
  47. volatile uint32_t framefilt; /* 0x04 */
  48. volatile uint32_t hashtablehigh; /* 0x08 */
  49. volatile uint32_t hashtablelow; /* 0x0c */
  50. volatile uint32_t miiaddr; /* 0x10 */
  51. volatile uint32_t miidata; /* 0x14 */
  52. volatile uint32_t flowcontrol; /* 0x18 */
  53. volatile uint32_t vlantag; /* 0x1c */
  54. volatile uint32_t version; /* 0x20 */
  55. volatile uint32_t reserved_1[5];
  56. volatile uint32_t intreg; /* 0x38 */
  57. volatile uint32_t intmask; /* 0x3c */
  58. volatile uint32_t macaddr0hi; /* 0x40 */
  59. volatile uint32_t macaddr0lo; /* 0x44 */
  60. };
  61. /* MAC configuration register definitions */
  62. #define CVI_FRAMEBURSTENABLE (1 << 21)
  63. #define CVI_MII_PORTSELECT (1 << 15)
  64. #define CVI_FES_100 (1 << 14)
  65. #define CVI_DISABLERXOWN (1 << 13)
  66. #define CVI_FULLDPLXMODE (1 << 11)
  67. #define CVI_RXENABLE (1 << 2)
  68. #define CVI_TXENABLE (1 << 3)
  69. /* MII address register definitions */
  70. #define CVI_MII_BUSY (1 << 0)
  71. #define CVI_MII_WRITE (1 << 1)
  72. #define CVI_MII_CLKRANGE_60_100M (0)
  73. #define CVI_MII_CLKRANGE_100_150M (0x4)
  74. #define CVI_MII_CLKRANGE_20_35M (0x8)
  75. #define CVI_MII_CLKRANGE_35_60M (0xC)
  76. #define CVI_MII_CLKRANGE_150_250M (0x10)
  77. #define CVI_MII_CLKRANGE_250_300M (0x14)
  78. #define CVI_MIIADDRSHIFT (11)
  79. #define CVI_MIIREGSHIFT (6)
  80. #define CVI_MII_REGMSK (0x1F << 6)
  81. #define CVI_MII_ADDRMSK (0x1F << 11)
  82. typedef uint32_t reg_type;
  83. struct dw_gmac_dma_regs
  84. {
  85. volatile reg_type busmode; /* 0x00 */
  86. volatile reg_type txpolldemand; /* 0x04 */
  87. volatile reg_type rxpolldemand; /* 0x08 */
  88. volatile reg_type rxdesclistaddr; /* 0x0c */
  89. volatile reg_type txdesclistaddr; /* 0x10 */
  90. volatile reg_type status; /* 0x14 */
  91. volatile reg_type opmode; /* 0x18 */
  92. volatile reg_type intenable; /* 0x1c */
  93. volatile reg_type discardedcount; /* 0x20 */
  94. volatile reg_type wdtforri; /* 0x24 */
  95. //volatile reg_type reserved1[2];
  96. volatile reg_type axibus; /* 0x28 */
  97. volatile reg_type reserved2[7];
  98. volatile reg_type currhosttxdesc; /* 0x48 */
  99. volatile reg_type currhostrxdesc; /* 0x4c */
  100. volatile reg_type currhosttxbuffaddr; /* 0x50 */
  101. volatile reg_type currhostrxbuffaddr; /* 0x54 */
  102. };
  103. /* Operation mode definitions */
  104. #define CVI_RXSTART (1 << 1)
  105. #define CVI_TXSECONDFRAME (1 << 2)
  106. #define CVI_TXSTART (1 << 13)
  107. #define CVI_FLUSHTXFIFO (1 << 20)
  108. #define CVI_STOREFORWARD (1 << 21)
  109. #define CVI_DW_DMA_BASE_OFFSET (0x1000)
  110. /* Default DMA Burst length */
  111. #ifndef CONFIG_DW_GMAC_DEFAULT_DMA_PBL
  112. #define CONFIG_DW_GMAC_DEFAULT_DMA_PBL 8
  113. #endif
  114. /* Status definitions */
  115. #define CVI_DMA_STATUS_ERI 0x00004000 /* Early Receive Interrupt */
  116. #define CVI_DMA_STATUS_RI 0x00000040 /* Receive Interrupt */
  117. #define CVI_DMA_STATUS_TI 0x00000001 /* Transmit Interrupt */
  118. /* Bus mode register definitions */
  119. #define CVI_DMAMAC_SRST (1 << 0)
  120. #define CVI_RXHIGHPRIO (1 << 1)
  121. #define CVI_FIXEDBURST (1 << 16)
  122. #define CVI_PRIORXTX_11 (0 << 14)
  123. #define CVI_PRIORXTX_21 (1 << 14)
  124. #define CVI_PRIORXTX_31 (2 << 14)
  125. #define CVI_PRIORXTX_41 (3 << 14)
  126. #define CVI_DMA_PBL (CONFIG_DW_GMAC_DEFAULT_DMA_PBL<<8)
  127. /* Poll demand definitions */
  128. #define CVI_POLL_DATA (0xFFFFFFFF)
  129. /* Descriptior related definitions */
  130. #define CVI_MAC_MAX_FRAME_SZ (1600)
  131. struct dmamacdescr
  132. {
  133. unsigned int txrx_status;
  134. unsigned int dmamac_cntl;
  135. unsigned int dmamac_addr;
  136. unsigned int dmamac_next;
  137. } __attribute__((aligned(DW_GMAC_DMA_ALIGN)));
  138. /*
  139. * txrx_status definitions
  140. */
  141. /* tx status bits definitions */
  142. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  143. #define CVI_DESC_TXSTS_OWNBYDMA (1 << 31)
  144. #define CVI_DESC_TXSTS_TXINT (1 << 30)
  145. #define CVI_DESC_TXSTS_TXLAST (1 << 29)
  146. #define CVI_DESC_TXSTS_TXFIRST (1 << 28)
  147. #define CVI_DESC_TXSTS_TXCRCDIS (1 << 27)
  148. #define CVI_DESC_TXSTS_TXPADDIS (1 << 26)
  149. #define CVI_DESC_TXSTS_TXCHECKINSCTRL (3 << 22)
  150. #define CVI_DESC_TXSTS_TXRINGEND (1 << 21)
  151. #define CVI_DESC_TXSTS_TXCHAIN (1 << 20)
  152. #define CVI_DESC_TXSTS_MSK (0x1FFFF << 0)
  153. #else
  154. #define CVI_DESC_TXSTS_OWNBYDMA (1 << 31)
  155. #define CVI_DESC_TXSTS_MSK (0x1FFFF << 0)
  156. #endif
  157. /* rx status bits definitions */
  158. #define CVI_DESC_RXSTS_OWNBYDMA (1 << 31)
  159. #define CVI_DESC_RXSTS_DAFILTERFAIL (1 << 30)
  160. #define CVI_DESC_RXSTS_FRMLENMSK (0x3FFF << 16)
  161. #define CVI_DESC_RXSTS_FRMLENSHFT (16)
  162. #define CVI_DESC_RXSTS_ERROR (1 << 15)
  163. #define CVI_DESC_RXSTS_RXTRUNCATED (1 << 14)
  164. #define CVI_DESC_RXSTS_SAFILTERFAIL (1 << 13)
  165. #define CVI_DESC_RXSTS_RXIPC_GIANTFRAME (1 << 12)
  166. #define CVI_DESC_RXSTS_RXDAMAGED (1 << 11)
  167. #define CVI_DESC_RXSTS_RXVLANTAG (1 << 10)
  168. #define CVI_DESC_RXSTS_RXFIRST (1 << 9)
  169. #define CVI_DESC_RXSTS_RXLAST (1 << 8)
  170. #define CVI_DESC_RXSTS_RXIPC_GIANT (1 << 7)
  171. #define CVI_DESC_RXSTS_RXCOLLISION (1 << 6)
  172. #define CVI_DESC_RXSTS_RXFRAMEETHER (1 << 5)
  173. #define CVI_DESC_RXSTS_RXWATCHDOG (1 << 4)
  174. #define CVI_DESC_RXSTS_RXMIIERROR (1 << 3)
  175. #define CVI_DESC_RXSTS_RXDRIBBLING (1 << 2)
  176. #define CVI_DESC_RXSTS_RXCRC (1 << 1)
  177. /*
  178. * dmamac_cntl definitions
  179. */
  180. /* tx control bits definitions */
  181. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  182. #define CVI_DESC_TXCTRL_SIZE1MASK (0x1FFF << 0)
  183. #define CVI_DESC_TXCTRL_SIZE1SHFT (0)
  184. #define CVI_DESC_TXCTRL_SIZE2MASK (0x1FFF << 16)
  185. #define CVI_DESC_TXCTRL_SIZE2SHFT (16)
  186. #else
  187. #define CVI_DESC_TXCTRL_TXINT (1 << 31)
  188. #define CVI_DESC_TXCTRL_TXLAST (1 << 30)
  189. #define CVI_DESC_TXCTRL_TXFIRST (1 << 29)
  190. #define CVI_DESC_TXCTRL_TXCHECKINSCTRL (3 << 27)
  191. #define CVI_DESC_TXCTRL_TXCRCDIS (1 << 26)
  192. #define CVI_DESC_TXCTRL_TXRINGEND (1 << 25)
  193. #define CVI_DESC_TXCTRL_TXCHAIN (1 << 24)
  194. #define CVI_DESC_TXCTRL_SIZE1MASK (0x7FF << 0)
  195. #define CVI_DESC_TXCTRL_SIZE1SHFT (0)
  196. #define CVI_DESC_TXCTRL_SIZE2MASK (0x7FF << 11)
  197. #define CVI_DESC_TXCTRL_SIZE2SHFT (11)
  198. #endif
  199. /* rx control bits definitions */
  200. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  201. #define CVI_DESC_RXCTRL_RXINTDIS (1 << 31)
  202. #define CVI_DESC_RXCTRL_RXRINGEND (1 << 15)
  203. #define CVI_DESC_RXCTRL_RXCHAIN (1 << 14)
  204. #define CVI_DESC_RXCTRL_SIZE1MASK (0x1FFF << 0)
  205. #define CVI_DESC_RXCTRL_SIZE1SHFT (0)
  206. #define CVI_DESC_RXCTRL_SIZE2MASK (0x1FFF << 16)
  207. #define CVI_DESC_RXCTRL_SIZE2SHFT (16)
  208. #else
  209. #define CVI_DESC_RXCTRL_RXINTDIS (1 << 31)
  210. #define CVI_DESC_RXCTRL_RXRINGEND (1 << 25)
  211. #define CVI_DESC_RXCTRL_RXCHAIN (1 << 24)
  212. #define CVI_DESC_RXCTRL_SIZE1MASK (0x7FF << 0)
  213. #define CVI_DESC_RXCTRL_SIZE1SHFT (0)
  214. #define CVI_DESC_RXCTRL_SIZE2MASK (0x7FF << 11)
  215. #define CVI_DESC_RXCTRL_SIZE2SHFT (11)
  216. #endif
  217. struct dw_gmac_priv
  218. {
  219. struct dmamacdescr tx_mac_descrtable[CVI_CONFIG_TX_DESCR_NUM] __aligned(DW_GMAC_DMA_ALIGN);
  220. struct dmamacdescr rx_mac_descrtable[CVI_CONFIG_RX_DESCR_NUM] __aligned(DW_GMAC_DMA_ALIGN);
  221. char txbuffs[CVI_TX_TOTAL_BUFSIZE] __aligned(DW_GMAC_DMA_ALIGN);
  222. char rxbuffs[CVI_RX_TOTAL_BUFSIZE] __aligned(DW_GMAC_DMA_ALIGN);
  223. uint32_t interface;
  224. uint32_t max_speed;
  225. uint32_t tx_currdescnum;
  226. uint32_t rx_currdescnum;
  227. struct dw_gmac_mac_regs *mac_regs_p;
  228. struct dw_gmac_dma_regs *dma_regs_p;
  229. };
  230. #ifdef CONFIG_DM_ETH
  231. int designware_eth_ofdata_to_platdata(struct udevice *dev);
  232. int designware_eth_probe(struct udevice *dev);
  233. extern const struct eth_ops designware_eth_ops;
  234. struct dw_eth_pdata
  235. {
  236. struct eth_pdata eth_pdata;
  237. u32 reset_delays[3];
  238. };
  239. int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr);
  240. int designware_eth_enable(struct dw_eth_dev *priv);
  241. int designware_eth_send(struct udevice *dev, void *packet, int length);
  242. int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp);
  243. int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
  244. int length);
  245. void designware_eth_stop(struct udevice *dev);
  246. int designware_eth_write_hwaddr(struct udevice *dev);
  247. #endif
  248. #endif
  249. typedef struct
  250. {
  251. eth_phy_dev_t *phy_dev;
  252. rt_ubase_t base;
  253. uint8_t irq;
  254. uint8_t mac_addr[6];
  255. struct dw_gmac_priv *priv_unalign;
  256. struct dw_gmac_priv *priv;
  257. } gmac_dev_t;
  258. /**
  259. \brief Ethernet MAC Address
  260. */
  261. typedef struct eth_mac_addr
  262. {
  263. uint8_t b[6]; ///< MAC Address (6 bytes), MSB first
  264. } eth_mac_addr_t;
  265. static inline void *memalign(uint32_t align, uint32_t size, void **mem_unalign)
  266. {
  267. void *mem;
  268. uint32_t offset;
  269. *mem_unalign = (void *)rt_malloc(size + align);
  270. if (!*mem_unalign) {
  271. return NULL;
  272. }
  273. offset = *(uint32_t *)mem_unalign % align;
  274. if (offset == 0) {
  275. mem = (struct eqos_priv *)*mem_unalign;
  276. } else {
  277. mem = (struct eqos_priv *)(*mem_unalign + (align - offset));
  278. }
  279. return mem;
  280. }
  281. /**
  282. \brief Write Ethernet PHY Register through Management Interface.
  283. \param[in] handle ethernet handle
  284. \param[in] phy_addr 5-bit device address
  285. \param[in] reg_addr 5-bit register address
  286. \param[in] data 16-bit data to write
  287. \return error code
  288. */
  289. int32_t dw_eth_mac_phy_write(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t data);
  290. /**
  291. \brief Control Ethernet Interface.
  292. \param[in] handle ethernet handle
  293. \param[in] control Operation
  294. \param[in] arg Argument of operation (optional)
  295. \return error code
  296. */
  297. int32_t cvi_eth_mac_control(eth_mac_handle_t handle, uint32_t control, uint32_t arg);
  298. /**
  299. \brief Get Ethernet MAC Address.
  300. \param[in] handle ethernet handle
  301. \param[in] mac Pointer to address
  302. \return error code
  303. */
  304. int32_t cvi_eth_mac_get_macaddr(eth_mac_handle_t handle, eth_mac_addr_t *mac);
  305. /**
  306. \brief Set Ethernet MAC Address.
  307. \param[in] handle ethernet handle
  308. \param[in] mac Pointer to address
  309. \return error code
  310. */
  311. int32_t cvi_eth_mac_set_macaddr(eth_mac_handle_t handle, const eth_mac_addr_t *mac);
  312. /**
  313. \brief Connect phy device to mac device.
  314. \param[in] handle_mac mac handle
  315. \param[in] handle_phy phy handle
  316. */
  317. void dw_eth_mac_connect_phy(eth_mac_handle_t handle_mac, eth_phy_handle_t handle_phy);
  318. /**
  319. \brief Read Ethernet PHY Register through Management Interface.
  320. \param[in] handle ethernet handle
  321. \param[in] phy_addr 5-bit device address
  322. \param[in] reg_addr 5-bit register address
  323. \param[out] data Pointer where the result is written to
  324. \return error code
  325. */
  326. int32_t dw_eth_mac_phy_read(eth_mac_handle_t handle, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data);
  327. /**
  328. \brief Send Ethernet frame.
  329. \param[in] handle ethernet handle
  330. \param[in] frame Pointer to frame buffer with data to send
  331. \param[in] len Frame buffer length in bytes
  332. \return error code
  333. */
  334. int32_t cvi_eth_mac_send_frame(eth_mac_handle_t handle, const uint8_t *frame, uint32_t len);
  335. /**
  336. \brief Read data of received Ethernet frame.
  337. \param[in] handle ethernet handle
  338. \param[in] frame Pointer to frame buffer for data to read into
  339. \param[in] len Frame buffer length in bytes
  340. \return number of data bytes read or execution status
  341. - value >= 0: number of data bytes read
  342. - value < 0: error occurred, value is execution status as defined with execution_status
  343. */
  344. int32_t cvi_eth_mac_read_frame(eth_mac_handle_t handle, uint8_t *frame, uint32_t len);
  345. /**
  346. \brief This function is used to initialize Ethernet device and register an event callback.
  347. \param[in] idx device id
  348. \param[in] cb callback to handle ethernet event
  349. \return return ethernet handle if success
  350. */
  351. eth_mac_handle_t cvi_eth_mac_init(rt_ubase_t base);
  352. #ifdef __cplusplus
  353. }
  354. #endif
  355. #endif /* _DW_GMAC_182x_H_ */