drv_xmac.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-07-07 liuzhihong first commit
  11. * 2023-07-14 liuzhihong support RT-Smart
  12. */
  13. #ifndef __DRV_XMAC_H__
  14. #define __DRV_XMAC_H__
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #ifdef BSP_USING_ETH
  18. #include <netif/ethernetif.h>
  19. #include "fxmac.h"
  20. #include "fkernel.h"
  21. #include "ferror_code.h"
  22. #include "fassert.h"
  23. #include "fxmac_bdring.h"
  24. #include "eth_ieee_reg.h"
  25. #include "fcpu_info.h"
  26. #include "fxmac_phy.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #define FREERTOS_XMAC_INIT_ERROR FT_CODE_ERR(ErrModPort, 0, 0x1)
  31. #define FREERTOS_XMAC_PARAM_ERROR FT_CODE_ERR(ErrModPort, 0, 0x2)
  32. #define FREERTOS_XMAC_NO_VALID_SPACE FT_CODE_ERR(ErrModPort, 0, 0x3)
  33. #define FXMAX_RX_BDSPACE_LENGTH 0x20000 /* default set 128KB*/
  34. #define FXMAX_TX_BDSPACE_LENGTH 0x20000 /* default set 128KB*/
  35. #define FXMAX_RX_PBUFS_LENGTH 16
  36. #define FXMAX_TX_PBUFS_LENGTH 16
  37. #define FXMAX_MAX_HARDWARE_ADDRESS_LENGTH 6
  38. #define XMAC_PHY_RESET_ENABLE 1
  39. #define XMAC_PHY_RESET_DISABLE 0
  40. /* configuration */
  41. #define FXMAC_OS_CONFIG_JUMBO BIT(0)
  42. #define FXMAC_OS_CONFIG_MULTICAST_ADDRESS_FILITER BIT(1) /* Allow multicast address filtering */
  43. #define FXMAC_OS_CONFIG_COPY_ALL_FRAMES BIT(2) /* enable copy all frames */
  44. #define FXMAC_OS_CONFIG_CLOSE_FCS_CHECK BIT(3) /* close fcs check */
  45. #define FXMAC_OS_CONFIG_RX_POLL_RECV BIT(4) /* select poll mode */
  46. /* Phy */
  47. #define FXMAC_PHY_SPEED_10M 10
  48. #define FXMAC_PHY_SPEED_100M 100
  49. #define FXMAC_PHY_SPEED_1000M 1000
  50. #define FXMAC_PHY_HALF_DUPLEX 0
  51. #define FXMAC_PHY_FULL_DUPLEX 1
  52. #define MAX_FRAME_SIZE_JUMBO (FXMAC_MTU_JUMBO + FXMAC_HDR_SIZE + FXMAC_TRL_SIZE)
  53. /* Byte alignment of BDs */
  54. #define BD_ALIGNMENT (FXMAC_DMABD_MINIMUM_ALIGNMENT*2)
  55. /* frame queue */
  56. #define PQ_QUEUE_SIZE 4096
  57. #define LINK_THREAD_STACK_LENGTH 0x20400
  58. typedef struct
  59. {
  60. uintptr data[PQ_QUEUE_SIZE];
  61. int head, tail, len;
  62. } PqQueue;
  63. typedef enum
  64. {
  65. FXMAC_OS_INTERFACE_SGMII = 0,
  66. FXMAC_OS_INTERFACE_RMII,
  67. FXMAC_OS_INTERFACE_RGMII,
  68. FXMAC_OS_INTERFACE_LENGTH
  69. } FXmacRtThreadInterface;
  70. typedef struct
  71. {
  72. u8 rx_bdspace[FXMAX_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 接收bd 缓冲区 */
  73. u8 tx_bdspace[FXMAX_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 发送bd 缓冲区 */
  74. uintptr rx_pbufs_storage[FXMAX_RX_PBUFS_LENGTH];
  75. uintptr tx_pbufs_storage[FXMAX_TX_PBUFS_LENGTH];
  76. } FXmacNetifBuffer;
  77. typedef struct
  78. {
  79. u32 instance_id;
  80. FXmacRtThreadInterface interface;
  81. u32 autonegotiation; /* 1 is autonegotiation ,0 is manually set */
  82. u32 phy_speed; /* FXMAC_PHY_SPEED_XXX */
  83. u32 phy_duplex; /* FXMAC_PHY_XXX_DUPLEX */
  84. } FXmacOsControl;
  85. typedef struct
  86. {
  87. struct eth_device parent; /* inherit from ethernet device */
  88. FXmac instance; /* Xmac controller */
  89. FXmacOsControl mac_config;
  90. FXmacNetifBuffer buffer; /* DMA buffer */
  91. /* queue to store overflow packets */
  92. PqQueue recv_q;
  93. PqQueue send_q;
  94. /* configuration */
  95. u32 config;
  96. u32 is_link_up;
  97. rt_uint8_t hwaddr[FXMAX_MAX_HARDWARE_ADDRESS_LENGTH]; /* MAC address */
  98. struct rt_thread _link_thread; /* link detect thread */
  99. rt_uint8_t _link_thread_stack[LINK_THREAD_STACK_LENGTH];/* link detect thread stack*/
  100. } FXmacOs;
  101. enum lwip_port_link_status
  102. {
  103. ETH_LINK_UNDEFINED = 0,
  104. ETH_LINK_UP,
  105. ETH_LINK_DOWN,
  106. ETH_LINK_NEGOTIATING
  107. };
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif // !
  112. #endif