drv_xmac.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define FREERTOS_XMAC_INIT_ERROR FT_CODE_ERR(ErrModPort, 0, 0x1)
  30. #define FREERTOS_XMAC_PARAM_ERROR FT_CODE_ERR(ErrModPort, 0, 0x2)
  31. #define FREERTOS_XMAC_NO_VALID_SPACE FT_CODE_ERR(ErrModPort, 0, 0x3)
  32. #define FXMAX_RX_BDSPACE_LENGTH 0x20000 /* default set 128KB*/
  33. #define FXMAX_TX_BDSPACE_LENGTH 0x20000 /* default set 128KB*/
  34. #define FXMAX_RX_PBUFS_LENGTH 16
  35. #define FXMAX_TX_PBUFS_LENGTH 16
  36. #define FXMAX_MAX_HARDWARE_ADDRESS_LENGTH 6
  37. /* configuration */
  38. #define FXMAC_OS_CONFIG_JUMBO BIT(0)
  39. #define FXMAC_OS_CONFIG_MULTICAST_ADDRESS_FILITER BIT(1) /* Allow multicast address filtering */
  40. #define FXMAC_OS_CONFIG_COPY_ALL_FRAMES BIT(2) /* enable copy all frames */
  41. #define FXMAC_OS_CONFIG_CLOSE_FCS_CHECK BIT(3) /* close fcs check */
  42. #define FXMAC_OS_CONFIG_RX_POLL_RECV BIT(4) /* select poll mode */
  43. /* Phy */
  44. #define FXMAC_PHY_SPEED_10M 10
  45. #define FXMAC_PHY_SPEED_100M 100
  46. #define FXMAC_PHY_SPEED_1000M 1000
  47. #define FXMAC_PHY_HALF_DUPLEX 0
  48. #define FXMAC_PHY_FULL_DUPLEX 1
  49. #define MAX_FRAME_SIZE_JUMBO (FXMAC_MTU_JUMBO + FXMAC_HDR_SIZE + FXMAC_TRL_SIZE)
  50. /* Byte alignment of BDs */
  51. #define BD_ALIGNMENT (FXMAC_DMABD_MINIMUM_ALIGNMENT*2)
  52. /* frame queue */
  53. #define PQ_QUEUE_SIZE 4096
  54. #define LINK_THREAD_STACK_LENGTH 0x20400
  55. typedef struct
  56. {
  57. uintptr data[PQ_QUEUE_SIZE];
  58. int head, tail, len;
  59. } PqQueue;
  60. typedef enum
  61. {
  62. FXMAC_OS_INTERFACE_SGMII = 0,
  63. FXMAC_OS_INTERFACE_RMII,
  64. FXMAC_OS_INTERFACE_RGMII,
  65. FXMAC_OS_INTERFACE_LENGTH
  66. } FXmacRtThreadInterface;
  67. typedef struct
  68. {
  69. u8 rx_bdspace[FXMAX_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 接收bd 缓冲区 */
  70. u8 tx_bdspace[FXMAX_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 发送bd 缓冲区 */
  71. uintptr rx_pbufs_storage[FXMAX_RX_PBUFS_LENGTH];
  72. uintptr tx_pbufs_storage[FXMAX_TX_PBUFS_LENGTH];
  73. } FXmacNetifBuffer;
  74. typedef struct
  75. {
  76. u32 instance_id;
  77. FXmacRtThreadInterface interface;
  78. u32 autonegotiation; /* 1 is autonegotiation ,0 is manually set */
  79. u32 phy_speed; /* FXMAC_PHY_SPEED_XXX */
  80. u32 phy_duplex; /* FXMAC_PHY_XXX_DUPLEX */
  81. } FXmacOsControl;
  82. typedef struct
  83. {
  84. struct eth_device parent; /* inherit from ethernet device */
  85. FXmac instance; /* Xmac controller */
  86. FXmacOsControl mac_config;
  87. FXmacNetifBuffer buffer; /* DMA buffer */
  88. /* queue to store overflow packets */
  89. PqQueue recv_q;
  90. PqQueue send_q;
  91. /* configuration */
  92. u32 config;
  93. u32 is_link_up;
  94. rt_uint8_t hwaddr[FXMAX_MAX_HARDWARE_ADDRESS_LENGTH]; /* MAC address */
  95. struct rt_thread _link_thread; /* link detect thread */
  96. rt_uint8_t _link_thread_stack[LINK_THREAD_STACK_LENGTH];/* link detect thread stack*/
  97. } FXmacOs;
  98. enum lwip_port_link_status
  99. {
  100. ETH_LINK_UNDEFINED = 0,
  101. ETH_LINK_UP,
  102. ETH_LINK_DOWN,
  103. ETH_LINK_NEGOTIATING
  104. };
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif // !
  109. #endif