drv_tsw.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2025 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-3-31 Jiading Initial version
  9. */
  10. #include <rtdevice.h>
  11. #ifdef BSP_USING_TSW
  12. #include <rtdbg.h>
  13. #include "drv_tsw.h"
  14. #include "board.h"
  15. #if defined(BSP_USING_TSW_PHY_RTL8211)
  16. #include "hpm_rtl8211.h"
  17. #endif
  18. ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(TSW_SOC_DATA_BUS_WIDTH) uint8_t send_buff[TSW_SEND_DESC_COUNT][TSW_SEND_BUFF_LEN];
  19. ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(TSW_SOC_DATA_BUS_WIDTH) uint8_t recv_buff[TSW_RECV_DESC_COUNT][TSW_RECV_BUFF_LEN];
  20. ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(TSW_SOC_DATA_BUS_WIDTH) tsw_frame_t frame[TSW_FRAME_BUFF_COUNT];
  21. struct eth_device eth0_dev;
  22. static tsw_device tsw0_dev;
  23. static tsw_buff_config_t tsw_rx_buff_cfg = {.buffer = (uint32_t)recv_buff,
  24. .count = TSW_RECV_DESC_COUNT,
  25. .size = TSW_RECV_BUFF_LEN
  26. };
  27. static tsw_buff_config_t tsw_tx_buff_cfg = {.buffer = (uint32_t)send_buff,
  28. .count = TSW_SEND_DESC_COUNT,
  29. .size = TSW_SEND_BUFF_LEN
  30. };
  31. static hpm_tsw_t tsw = {.name = "E0",
  32. .base = BOARD_TSW,
  33. .port = BOARD_TSW_PORT,
  34. .irq_num = IRQn_TSW_0,
  35. .inf = BOARD_TSW_PORT_ITF,
  36. .eth_dev = &eth0_dev,
  37. .tsw_dev = &tsw0_dev,
  38. .rx_buff_cfg = &tsw_rx_buff_cfg,
  39. .tx_buff_cfg = &tsw_tx_buff_cfg,
  40. .tx_delay = BOARD_TSW_PORT3_RGMII_TX_DLY,
  41. .rx_delay = BOARD_TSW_PORT3_RGMII_RX_DLY
  42. };
  43. mac_init_t mac_init[] = {
  44. {MAC0_ADDR0, MAC0_ADDR1, MAC0_ADDR2, MAC0_ADDR3, MAC0_ADDR4, MAC0_ADDR5}
  45. };
  46. static hpm_tsw_t *s_geths[] = {
  47. &tsw
  48. };
  49. ATTR_WEAK rt_err_t tsw_get_mac_address(TSW_Type *ptr, uint8_t *mac)
  50. {
  51. /* load MAC address from MACRO definitions */
  52. SMEMCPY(mac, &mac_init[0], TSW_MAC_COUNT);
  53. return RT_EOK;
  54. }
  55. static rt_err_t hpm_tsw_init(tsw_device *init)
  56. {
  57. tsw_dma_config_t config;
  58. uint8_t *ptx, *prx;
  59. rtl8211_config_t phy_config;
  60. /* Set RGMII clock delay */
  61. if (init->media_interface == tsw_port_phy_itf_rgmii) {
  62. tsw_set_port_clock_delay(init->instance, init->port, init->tx_delay, init->rx_delay);
  63. }
  64. /* Disable all MACs(TX/RX) */
  65. tsw_ep_disable_all_mac_ctrl(init->instance, tsw_mac_type_emac);
  66. /* Set MAC Address */
  67. tsw_ep_set_mac_addr(init->instance, init->port, init->mac, true);
  68. /* Set MAC Mode: GMII, CLKSEL: REFCLK */
  69. tsw_ep_set_mac_mode(init->instance, init->port, init->media_interface == tsw_port_phy_itf_rgmii ? tsw_mac_mode_gmii : tsw_mac_mode_mii);
  70. /* Set port PHY interface */
  71. tsw_set_port_interface(init->instance, init->port, init->media_interface);
  72. /* Enable all MACs(TX/RX) */
  73. tsw_ep_enable_all_mac_ctrl(init->instance, tsw_mac_type_emac);
  74. /* Clear CAM */
  75. tsw_clear_cam(init->instance);
  76. /* Wait for CAM clearing completion */
  77. rt_thread_mdelay(10);
  78. /* Enable VLAN-ID 1 at all ports */
  79. tsw_set_cam_vlan_port(init->instance);
  80. /* Get the default DMA config */
  81. tsw_get_default_dma_config(&config);
  82. /* Initialize DMA for sending */
  83. tsw_init_send(init->instance, &config);
  84. ptx = (uint8_t *)(init->tx_buff_cfg->buffer);
  85. for (uint8_t i = 0; i < init->tx_buff_cfg->count; i++) {
  86. ptx[i * init->tx_buff_cfg->size] = init->port + 1;
  87. }
  88. /* Initialize DMA for receiving */
  89. config.irq = true;
  90. tsw_init_recv(init->instance, &config);
  91. prx = (uint8_t *)(init->rx_buff_cfg->buffer);
  92. for (uint8_t i = 0; i < init->rx_buff_cfg->count; i++) {
  93. tsw_commit_recv_desc(init->instance, &prx[i * init->rx_buff_cfg->size], init->rx_buff_cfg->size, i);
  94. }
  95. /* Enable TSW IRQ */
  96. intc_m_enable_irq(init->irq_number);
  97. return RT_EOK;
  98. }
  99. static rt_err_t rt_hpm_tsw_init(rt_device_t dev)
  100. {
  101. uint8_t mac[TSW_MAC_COUNT];
  102. tsw_device *tsw_dev = (tsw_device *)dev->user_data;
  103. /* Initialize GPIOs */
  104. board_init_tsw_pins(tsw_dev->instance);
  105. /* Reset an TSW PHY */
  106. board_reset_tsw_phy(tsw_dev->instance, tsw_dev->port);
  107. /* Get MAC address */
  108. tsw_get_mac_address(tsw_dev->instance, mac);
  109. /* Set mac0 address */
  110. memcpy(tsw_dev->mac, mac, TSW_MAC_COUNT);
  111. /* Initialize MAC and DMA */
  112. if (hpm_tsw_init(tsw_dev) == 0) {
  113. LOG_D("Ethernet control initialize successfully\n");
  114. return RT_EOK;
  115. } else {
  116. LOG_D("Ethernet control initialize unsuccessfully\n");
  117. return -RT_ERROR;
  118. }
  119. }
  120. static rt_err_t rt_hpm_eth_open(rt_device_t dev, rt_uint16_t oflag)
  121. {
  122. return RT_EOK;
  123. }
  124. static rt_err_t rt_hpm_tsw_close(rt_device_t dev)
  125. {
  126. return RT_EOK;
  127. }
  128. static rt_ssize_t rt_hpm_tsw_read(rt_device_t dev, rt_off_t pos, void * buffer, rt_size_t size)
  129. {
  130. return 0;
  131. }
  132. static rt_ssize_t rt_hpm_tsw_write(rt_device_t dev, rt_off_t pos, const void * buffer, rt_size_t size)
  133. {
  134. return 0;
  135. }
  136. static rt_err_t rt_hpm_tsw_control(rt_device_t dev, int cmd, void * args)
  137. {
  138. uint8_t *mac = (uint8_t *)args;
  139. tsw_device *tsw_dev = (tsw_device *)dev->user_data;
  140. switch (cmd) {
  141. case NIOCTL_GADDR:
  142. if (args != NULL) {
  143. tsw_get_mac_address(tsw_dev->instance, (uint8_t *)mac);
  144. SMEMCPY(args, mac, TSW_MAC_COUNT);
  145. } else {
  146. return -RT_ERROR;
  147. }
  148. break;
  149. default:
  150. break;
  151. }
  152. return RT_EOK;
  153. }
  154. static rt_err_t rt_hpm_tsw_tx(rt_device_t dev, struct pbuf * p)
  155. {
  156. struct pbuf *q;
  157. static uint32_t i = 0;
  158. uint32_t id;
  159. uint32_t length = TSW_SOC_SWITCH_HEADER_LEN;
  160. tsw_device *tsw_dev = (tsw_device *)dev->user_data;
  161. uint8_t *ptx = ((uint8_t *)tsw_dev->tx_buff_cfg->buffer);
  162. id = i++ % TSW_SOC_DMA_MAX_DESC_COUNT;
  163. for (q = p; q != NULL; q = q->next) {
  164. SMEMCPY(&ptx[id * tsw_dev->tx_buff_cfg->size + TSW_SOC_SWITCH_HEADER_LEN], q->payload, q->len);
  165. length += q->len;
  166. }
  167. tsw_send_frame(tsw_dev->instance, &ptx[id * tsw_dev->tx_buff_cfg->size], length, id);
  168. return ERR_OK;
  169. }
  170. static struct pbuf *rt_hpm_tsw_rx(rt_device_t dev)
  171. {
  172. struct pbuf *p = NULL, *q;
  173. static uint8_t idx = 0;
  174. tsw_device *tsw_dev = (tsw_device *)dev->user_data;
  175. uint8_t *prx = (uint8_t *)tsw_dev->rx_buff_cfg->buffer;
  176. if (frame[idx].length > TSW_SOC_SWITCH_HEADER_LEN) {
  177. /* Allocate a pbuf chain of pbufs from the lwIP buffer pool */
  178. p = pbuf_alloc(PBUF_RAW, frame[idx].length - TSW_SOC_SWITCH_HEADER_LEN, PBUF_POOL);
  179. if (p != NULL) {
  180. for (q = p; q != NULL; q = q->next) {
  181. /* pass the buffer to pbuf */
  182. frame[idx].buffer = &prx[frame[idx].id * tsw_dev->rx_buff_cfg->size];
  183. SMEMCPY(q->payload, &frame[idx].buffer[TSW_SOC_SWITCH_HEADER_LEN], q->len);
  184. frame[idx].length = 0;
  185. idx++;
  186. idx %= TSW_FRAME_BUFF_COUNT;
  187. }
  188. }
  189. }
  190. return p;
  191. }
  192. static void eth_rx_callback(struct eth_device* dev)
  193. {
  194. rt_err_t result;
  195. result = eth_device_ready(dev);
  196. if (result != RT_EOK) {
  197. LOG_I("Receive callback error = %d\n", result);
  198. }
  199. }
  200. void isr_tsw(hpm_tsw_t *obj)
  201. {
  202. static int idx = 0;
  203. uint8_t *prx = (uint8_t *)obj->tsw_dev->rx_buff_cfg->buffer;
  204. tsw_recv_frame(obj->base, &frame[idx]);
  205. tsw_commit_recv_desc(obj->base, &prx[idx * obj->tsw_dev->rx_buff_cfg->size], TSW_RECV_BUFF_LEN, idx);
  206. idx++;
  207. idx %= TSW_FRAME_BUFF_COUNT;
  208. eth_rx_callback(obj->eth_dev);
  209. }
  210. SDK_DECLARE_EXT_ISR_M(IRQn_TSW_0, isr_tsw_port_cpu)
  211. void isr_tsw_port_cpu(void)
  212. {
  213. isr_tsw(&tsw);
  214. }
  215. int rt_hw_tsw_init(void)
  216. {
  217. rt_err_t err = RT_ERROR;
  218. for (uint32_t i = 0; i < ARRAY_SIZE(s_geths); i++) {
  219. /* Set TX & RX buffer config */
  220. s_geths[i]->tsw_dev->tx_buff_cfg = s_geths[i]->tx_buff_cfg;
  221. s_geths[i]->tsw_dev->rx_buff_cfg = s_geths[i]->rx_buff_cfg;
  222. /* Set media interface */
  223. s_geths[i]->tsw_dev->media_interface = s_geths[i]->inf;
  224. /* Set instance */
  225. s_geths[i]->tsw_dev->instance = s_geths[i]->base;
  226. /* Set Port */
  227. s_geths[i]->tsw_dev->port = s_geths[i]->port;
  228. /* Set TX/RX delay */
  229. s_geths[i]->tsw_dev->tx_delay = s_geths[i]->tx_delay;
  230. s_geths[i]->tsw_dev->rx_delay = s_geths[i]->rx_delay;
  231. /* Set IRQ number */
  232. s_geths[i]->tsw_dev->irq_number = s_geths[i]->irq_num;
  233. /* Set the parent parameters */
  234. s_geths[i]->eth_dev->parent.init = rt_hpm_tsw_init;
  235. s_geths[i]->eth_dev->parent.close = rt_hpm_tsw_close;
  236. s_geths[i]->eth_dev->parent.read = rt_hpm_tsw_read;
  237. s_geths[i]->eth_dev->parent.write = rt_hpm_tsw_write;
  238. s_geths[i]->eth_dev->parent.control = rt_hpm_tsw_control;
  239. s_geths[i]->eth_dev->parent.user_data = s_geths[i]->tsw_dev;
  240. s_geths[i]->eth_dev->eth_rx = rt_hpm_tsw_rx;
  241. s_geths[i]->eth_dev->eth_tx = rt_hpm_tsw_tx;
  242. err = eth_device_init(s_geths[i]->eth_dev, s_geths[i]->name);
  243. if (RT_EOK == err) {
  244. LOG_D("Ethernet device %d initialize successfully!\n", i);
  245. } else {
  246. LOG_D("Ethernet device %d initialize unsuccessfully!\n");
  247. return err;
  248. }
  249. }
  250. return err;
  251. }
  252. INIT_DEVICE_EXPORT(rt_hw_tsw_init);
  253. #endif /* BSP_USING_TSW */