netdev_hpmicro.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "hpm_clock_drv.h"
  7. #include "hpm_enet_drv.h"
  8. #include "hpm_enet_phy_common.h"
  9. #include "hpm_otp_drv.h"
  10. #include "hpm_l1c_drv.h"
  11. #include "board.h"
  12. #include "ec_master.h"
  13. #if defined(RGMII) && RGMII
  14. #define ENET_INF_TYPE enet_inf_rgmii
  15. #define ENET BOARD_ENET_RGMII
  16. #else
  17. #define ENET_INF_TYPE enet_inf_rmii
  18. #define ENET BOARD_ENET_RMII
  19. #endif
  20. #define __ENABLE_ENET_RECEIVE_INTERRUPT 1
  21. #define MAC_ADDR0 0x00
  22. #define MAC_ADDR1 0x80
  23. #define MAC_ADDR2 0xE1
  24. #define MAC_ADDR3 0x00
  25. #define MAC_ADDR4 0x00
  26. #define MAC_ADDR5 0x00
  27. #define ENET_TX_BUFF_COUNT CONFIG_EC_MAX_ENET_TXBUF_COUNT
  28. #define ENET_RX_BUFF_COUNT CONFIG_EC_MAX_ENET_RXBUF_COUNT
  29. #define ENET_RX_BUFF_SIZE ENET_MAX_FRAME_SIZE
  30. #define ENET_TX_BUFF_SIZE ENET_MAX_FRAME_SIZE
  31. ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
  32. __RW enet_rx_desc_t dma_rx_desc_tab[ENET_RX_BUFF_COUNT]; /* Ethernet Rx DMA Descriptor */
  33. ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(ENET_SOC_DESC_ADDR_ALIGNMENT)
  34. __RW enet_tx_desc_t dma_tx_desc_tab[ENET_TX_BUFF_COUNT]; /* Ethernet Tx DMA Descriptor */
  35. ATTR_PLACE_AT_FAST_RAM_BSS_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
  36. __RW uint8_t rx_buff[ENET_RX_BUFF_COUNT][ENET_RX_BUFF_SIZE]; /* Ethernet Receive Buffer */
  37. ATTR_PLACE_AT_FAST_RAM_BSS_WITH_ALIGNMENT(ENET_SOC_BUFF_ADDR_ALIGNMENT)
  38. __RW uint8_t tx_buff[ENET_TX_BUFF_COUNT][ENET_TX_BUFF_SIZE]; /* Ethernet Transmit Buffer */
  39. enet_desc_t desc;
  40. uint8_t mac[ENET_MAC];
  41. ec_netdev_t g_netdev;
  42. ATTR_WEAK void enet_get_mac_address(uint8_t *mac)
  43. {
  44. bool invalid = true;
  45. uint32_t uuid[(ENET_MAC + (ENET_MAC - 1)) / sizeof(uint32_t)];
  46. for (int i = 0; i < ARRAY_SIZE(uuid); i++) {
  47. uuid[i] = otp_read_from_shadow(OTP_SOC_UUID_IDX + i);
  48. if (uuid[i] != 0xFFFFFFFFUL && uuid[i] != 0) {
  49. invalid = false;
  50. }
  51. }
  52. if (invalid == true) {
  53. ec_memcpy(mac, &uuid, ENET_MAC);
  54. } else {
  55. mac[0] = MAC_ADDR0;
  56. mac[1] = MAC_ADDR1;
  57. mac[2] = MAC_ADDR2;
  58. mac[3] = MAC_ADDR3;
  59. mac[4] = MAC_ADDR4;
  60. mac[5] = MAC_ADDR5;
  61. }
  62. }
  63. hpm_stat_t enet_init(ENET_Type *ptr)
  64. {
  65. enet_int_config_t int_config = { .int_enable = 0, .int_mask = 0 };
  66. enet_mac_config_t enet_config;
  67. enet_tx_control_config_t enet_tx_control_config;
  68. #ifdef CONFIG_EC_PHY_CUSTOM
  69. #if defined(RGMII) && RGMII
  70. #if defined(__USE_DP83867) && __USE_DP83867
  71. dp83867_config_t phy_config;
  72. #else
  73. rtl8211_config_t phy_config;
  74. #endif
  75. #else
  76. #if defined(__USE_DP83848) && __USE_DP83848
  77. dp83848_config_t phy_config;
  78. #else
  79. rtl8201_config_t phy_config;
  80. #endif
  81. #endif
  82. #endif
  83. /* Initialize td, rd and the corresponding buffers */
  84. memset((uint8_t *)dma_tx_desc_tab, 0x00, sizeof(dma_tx_desc_tab));
  85. memset((uint8_t *)dma_rx_desc_tab, 0x00, sizeof(dma_rx_desc_tab));
  86. memset((uint8_t *)rx_buff, 0x00, sizeof(rx_buff));
  87. memset((uint8_t *)tx_buff, 0x00, sizeof(tx_buff));
  88. desc.tx_desc_list_head = (enet_tx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)dma_tx_desc_tab);
  89. desc.rx_desc_list_head = (enet_rx_desc_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)dma_rx_desc_tab);
  90. desc.tx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)tx_buff);
  91. desc.tx_buff_cfg.count = ENET_TX_BUFF_COUNT;
  92. desc.tx_buff_cfg.size = ENET_TX_BUFF_SIZE;
  93. desc.rx_buff_cfg.buffer = core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)rx_buff);
  94. desc.rx_buff_cfg.count = ENET_RX_BUFF_COUNT;
  95. desc.rx_buff_cfg.size = ENET_RX_BUFF_SIZE;
  96. /*Get a default control config for tx descriptor */
  97. enet_get_default_tx_control_config(ENET, &enet_tx_control_config);
  98. /* Set the control config for tx descriptor */
  99. ec_memcpy(&desc.tx_control_config, &enet_tx_control_config, sizeof(enet_tx_control_config_t));
  100. /* Get MAC address */
  101. enet_get_mac_address(mac);
  102. /* Set MAC0 address */
  103. enet_config.mac_addr_high[0] = mac[5] << 8 | mac[4];
  104. enet_config.mac_addr_low[0] = mac[3] << 24 | mac[2] << 16 | mac[1] << 8 | mac[0];
  105. enet_config.valid_max_count = 1;
  106. /* Set DMA PBL */
  107. enet_config.dma_pbl = board_get_enet_dma_pbl(ENET);
  108. /* Set SARC */
  109. enet_config.sarc = enet_sarc_replace_mac0;
  110. #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT
  111. /* Enable Enet IRQ */
  112. board_enable_enet_irq(ENET);
  113. /* Get the default interrupt config */
  114. enet_get_default_interrupt_config(ENET, &int_config);
  115. #endif
  116. /* Initialize enet controller */
  117. if (enet_controller_init(ptr, ENET_INF_TYPE, &desc, &enet_config, &int_config) != status_success) {
  118. return status_fail;
  119. }
  120. #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT
  121. /* Disable LPI interrupt */
  122. enet_disable_lpi_interrupt(ENET);
  123. #endif
  124. #ifdef CONFIG_EC_PHY_CUSTOM
  125. /* Initialize phy */
  126. #if defined(RGMII) && RGMII
  127. #if defined(__USE_DP83867) && __USE_DP83867
  128. dp83867_reset(ptr);
  129. #if defined(__DISABLE_AUTO_NEGO) && __DISABLE_AUTO_NEGO
  130. dp83867_set_mdi_crossover_mode(ENET, enet_phy_mdi_crossover_manual_mdix);
  131. #endif
  132. dp83867_basic_mode_default_config(ptr, &phy_config);
  133. if (dp83867_basic_mode_init(ptr, &phy_config) == true) {
  134. #else
  135. rtl8211_reset(ptr);
  136. rtl8211_basic_mode_default_config(ptr, &phy_config);
  137. if (rtl8211_basic_mode_init(ptr, &phy_config) == true) {
  138. #endif
  139. #else
  140. #if defined(__USE_DP83848) && __USE_DP83848
  141. dp83848_reset(ptr);
  142. dp83848_basic_mode_default_config(ptr, &phy_config);
  143. if (dp83848_basic_mode_init(ptr, &phy_config) == true) {
  144. #else
  145. rtl8201_reset(ptr);
  146. rtl8201_basic_mode_default_config(ptr, &phy_config);
  147. if (rtl8201_basic_mode_init(ptr, &phy_config) == true) {
  148. #endif
  149. #endif
  150. EC_LOG_DBG("Enet phy init passed !\n");
  151. } else {
  152. EC_LOG_DBG("Enet phy init failed !\n");
  153. return status_fail;
  154. }
  155. #endif
  156. return status_success;
  157. }
  158. ec_netdev_t *ec_netdev_low_level_init(uint8_t netdev_index)
  159. {
  160. /* Initialize GPIOs */
  161. board_init_enet_pins(ENET);
  162. /* Reset an enet PHY */
  163. board_reset_enet_phy(ENET);
  164. #if defined(RGMII) && RGMII
  165. /* Set RGMII clock delay */
  166. board_init_enet_rgmii_clock_delay(ENET);
  167. #else
  168. /* Set RMII reference clock */
  169. board_init_enet_rmii_reference_clock(ENET, BOARD_ENET_RMII_INT_REF_CLK);
  170. EC_LOG_DBG("Reference Clock: %s\n", BOARD_ENET_RMII_INT_REF_CLK ? "Internal Clock" : "External Clock");
  171. #endif
  172. /* Initialize MAC and DMA */
  173. if (enet_init(ENET) == 0) {
  174. } else {
  175. EC_LOG_DBG("Enet initialization fails !!!\n");
  176. while (1) {
  177. }
  178. }
  179. ec_memcpy(g_netdev.mac_addr, mac, ENET_MAC);
  180. for (uint32_t i = 0; i < ENET_TX_BUFF_COUNT; i++) {
  181. for (uint8_t j = 0; j < 6; j++) { // dst MAC
  182. EC_WRITE_U8(&tx_buff[i][j], 0xFF);
  183. }
  184. for (uint8_t j = 0; j < 6; j++) { // src MAC
  185. EC_WRITE_U8(&tx_buff[i][6 + j], mac[j]);
  186. }
  187. EC_WRITE_U16(&tx_buff[i][12], ec_htons(0x88a4));
  188. }
  189. return &g_netdev;
  190. }
  191. #ifndef CONFIG_EC_PHY_CUSTOM
  192. void ec_mdio_low_level_write(struct chry_phy_device *phydev, uint16_t phy_addr, uint16_t regnum, uint16_t val)
  193. {
  194. //ec_netdev_t *netdev = (ec_netdev_t *)phydev->user_data;
  195. enet_write_phy(ENET, phy_addr, regnum, val);
  196. }
  197. uint16_t ec_mdio_low_level_read(struct chry_phy_device *phydev, uint16_t phy_addr, uint16_t regnum)
  198. {
  199. //ec_netdev_t *netdev = (ec_netdev_t *)phydev->user_data;
  200. return enet_read_phy(ENET, phy_addr, regnum);
  201. }
  202. void ec_netdev_low_level_link_up(ec_netdev_t *netdev, struct chry_phy_status *status)
  203. {
  204. enet_line_speed_t line_speed = enet_line_speed_10mbps;
  205. switch (status->speed) {
  206. case 10:
  207. line_speed = enet_line_speed_10mbps;
  208. break;
  209. case 100:
  210. line_speed = enet_line_speed_100mbps;
  211. break;
  212. case 1000:
  213. line_speed = enet_line_speed_1000mbps;
  214. break;
  215. default:
  216. break;
  217. }
  218. if (status->link) {
  219. enet_set_line_speed(ENET, line_speed);
  220. enet_set_duplex_mode(ENET, status->duplex);
  221. } else {
  222. }
  223. }
  224. #else
  225. void ec_netdev_low_level_poll_link_state(ec_netdev_t *netdev)
  226. {
  227. static enet_phy_status_t last_status;
  228. enet_phy_status_t status = { 0 };
  229. enet_line_speed_t line_speed[] = { enet_line_speed_10mbps, enet_line_speed_100mbps, enet_line_speed_1000mbps };
  230. #if defined(RGMII) && RGMII
  231. #if defined(__USE_DP83867) && __USE_DP83867
  232. dp83867_get_phy_status(ENET, &status);
  233. #else
  234. rtl8211_get_phy_status(ENET, &status);
  235. #endif
  236. #else
  237. #if defined(__USE_DP83848) && __USE_DP83848
  238. dp83848_get_phy_status(ENET, &status);
  239. #else
  240. rtl8201_get_phy_status(ENET, &status);
  241. #endif
  242. #endif
  243. if (memcmp(&last_status, &status, sizeof(enet_phy_status_t)) != 0) {
  244. ec_memcpy(&last_status, &status, sizeof(enet_phy_status_t));
  245. if (status.enet_phy_link) {
  246. enet_set_line_speed(ENET, line_speed[status.enet_phy_speed]);
  247. enet_set_duplex_mode(ENET, status.enet_phy_duplex);
  248. netdev->link_state = true;
  249. } else {
  250. netdev->link_state = false;
  251. }
  252. }
  253. }
  254. #endif
  255. EC_FAST_CODE_SECTION uint8_t *ec_netdev_low_level_get_txbuf(ec_netdev_t *netdev)
  256. {
  257. __IO enet_tx_desc_t *dma_tx_desc;
  258. dma_tx_desc = desc.tx_desc_list_cur;
  259. EC_ASSERT_MSG(dma_tx_desc->tdes0_bm.own == 0, "No free tx buffer available\n");
  260. return (uint8_t *)dma_tx_desc->tdes2_bm.buffer1;
  261. }
  262. EC_FAST_CODE_SECTION int ec_netdev_low_level_output(ec_netdev_t *netdev, uint32_t size)
  263. {
  264. __IO enet_tx_desc_t *dma_tx_desc;
  265. dma_tx_desc = desc.tx_desc_list_cur;
  266. if (dma_tx_desc->tdes0_bm.own != 0) {
  267. return -1;
  268. }
  269. /* Prepare transmit descriptors to give to DMA*/
  270. enet_prepare_transmission_descriptors(ENET, &desc.tx_desc_list_cur, size + 4, desc.tx_buff_cfg.size);
  271. return 0;
  272. }
  273. EC_FAST_CODE_SECTION int ec_netdev_low_level_input(ec_netdev_t *netdev)
  274. {
  275. uint32_t len;
  276. uint8_t *buffer;
  277. enet_frame_t frame = { 0, 0, 0 };
  278. enet_rx_desc_t *dma_rx_desc;
  279. uint32_t i = 0;
  280. int ret = 0;
  281. /* Check and get a received frame */
  282. if (enet_check_received_frame(&desc.rx_desc_list_cur, &desc.rx_frame_info) == 1) {
  283. frame = enet_get_received_frame(&desc.rx_desc_list_cur, &desc.rx_frame_info);
  284. }
  285. /* Obtain the size of the packet and put it into the "len" variable. */
  286. len = frame.length;
  287. buffer = (uint8_t *)sys_address_to_core_local_mem(BOARD_RUNNING_CORE, (uint32_t)frame.buffer);
  288. if (len > 0) {
  289. ec_netdev_receive(netdev, buffer, len);
  290. /* Release descriptors to DMA */
  291. dma_rx_desc = frame.rx_desc;
  292. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  293. for (i = 0; i < desc.rx_frame_info.seg_count; i++) {
  294. dma_rx_desc->rdes0_bm.own = 1;
  295. dma_rx_desc = (enet_rx_desc_t *)(dma_rx_desc->rdes3_bm.next_desc);
  296. }
  297. /* Clear Segment_Count */
  298. desc.rx_frame_info.seg_count = 0;
  299. } else {
  300. ret = -1;
  301. }
  302. /* Resume Rx Process */
  303. enet_rx_resume(ENET);
  304. return ret;
  305. }
  306. #if defined(__ENABLE_ENET_RECEIVE_INTERRUPT) && __ENABLE_ENET_RECEIVE_INTERRUPT
  307. void isr_enet(ENET_Type *ptr)
  308. {
  309. uint32_t status;
  310. uint32_t rxgbfrmis;
  311. uint32_t intr_status;
  312. status = ptr->DMA_STATUS;
  313. rxgbfrmis = ptr->MMC_INTR_RX;
  314. intr_status = ptr->INTR_STATUS;
  315. if (ENET_DMA_STATUS_GLPII_GET(status)) {
  316. /* read LPI_CSR to clear interrupt status */
  317. ptr->LPI_CSR;
  318. }
  319. if (ENET_INTR_STATUS_RGSMIIIS_GET(intr_status)) {
  320. /* read XMII_CSR to clear interrupt status */
  321. ptr->XMII_CSR;
  322. }
  323. if (ENET_DMA_STATUS_RI_GET(status)) {
  324. ptr->DMA_STATUS |= ENET_DMA_STATUS_RI_MASK;
  325. while (ec_netdev_low_level_input(&g_netdev) == 0) {
  326. }
  327. }
  328. if (ENET_MMC_INTR_RX_RXCTRLFIS_GET(rxgbfrmis)) {
  329. ptr->RXFRAMECOUNT_GB;
  330. }
  331. }
  332. #ifdef HPM_ENET0_BASE
  333. void isr_enet0(void)
  334. {
  335. isr_enet(ENET);
  336. }
  337. SDK_DECLARE_EXT_ISR_M(IRQn_ENET0, isr_enet0)
  338. #endif
  339. #ifdef HPM_ENET1_BASE
  340. void isr_enet1(void)
  341. {
  342. isr_enet(ENET);
  343. }
  344. SDK_DECLARE_EXT_ISR_M(IRQn_ENET1, isr_enet1)
  345. #endif
  346. #endif
  347. #include "hpm_gptmr_drv.h"
  348. #define EC_HTIMER BOARD_GPTMR
  349. #define EC_HTIMER_CH BOARD_GPTMR_CHANNEL
  350. #define EC_HTIMER_IRQ BOARD_GPTMR_IRQ
  351. #define EC_HTIMER_CLK_NAME BOARD_GPTMR_CLK_NAME
  352. ec_htimer_cb g_ec_htimer_cb = NULL;
  353. void *g_ec_htimer_arg = NULL;
  354. void ec_htimer_isr(void)
  355. {
  356. if (gptmr_check_status(EC_HTIMER, GPTMR_CH_RLD_STAT_MASK(EC_HTIMER_CH))) {
  357. gptmr_clear_status(EC_HTIMER, GPTMR_CH_RLD_STAT_MASK(EC_HTIMER_CH));
  358. g_ec_htimer_cb(g_ec_htimer_arg);
  359. }
  360. }
  361. SDK_DECLARE_EXT_ISR_M(EC_HTIMER_IRQ, ec_htimer_isr);
  362. void ec_htimer_start(uint32_t us, ec_htimer_cb cb, void *arg)
  363. {
  364. uint32_t gptmr_freq;
  365. gptmr_channel_config_t config;
  366. g_ec_htimer_cb = cb;
  367. g_ec_htimer_arg = arg;
  368. gptmr_channel_get_default_config(EC_HTIMER, &config);
  369. clock_add_to_group(EC_HTIMER_CLK_NAME, 0);
  370. gptmr_freq = clock_get_frequency(EC_HTIMER_CLK_NAME);
  371. config.reload = gptmr_freq / 1000000 * us;
  372. gptmr_stop_counter(EC_HTIMER, EC_HTIMER_CH);
  373. gptmr_channel_config(EC_HTIMER, EC_HTIMER_CH, &config, false);
  374. gptmr_enable_irq(EC_HTIMER, GPTMR_CH_RLD_IRQ_MASK(EC_HTIMER_CH));
  375. intc_m_enable_irq_with_priority(EC_HTIMER_IRQ, 10);
  376. gptmr_channel_reset_count(EC_HTIMER, EC_HTIMER_CH);
  377. gptmr_start_counter(EC_HTIMER, EC_HTIMER_CH);
  378. }
  379. void ec_htimer_stop(void)
  380. {
  381. gptmr_stop_counter(EC_HTIMER, EC_HTIMER_CH);
  382. gptmr_disable_irq(EC_HTIMER, GPTMR_CH_RLD_IRQ_MASK(EC_HTIMER_CH));
  383. intc_m_disable_irq(EC_HTIMER_IRQ);
  384. }
  385. uint32_t ec_get_cpu_frequency(void)
  386. {
  387. return clock_get_frequency(clock_cpu0);
  388. }