synopsys_emac.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * File : rthw.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "synopsys_emac.h"
  21. #include "gd32f4xx_enet.h"
  22. /* The state of enet initialization */
  23. volatile uint32_t enet_init_state = 0;
  24. /* Global pointers on Tx and Rx descriptor used to track transmit and receive descriptors */
  25. extern EMAC_DMADESCTypeDef *DMATxDescToSet;
  26. extern EMAC_DMADESCTypeDef *DMARxDescToGet;
  27. /**
  28. * Initializes the ETHERNET peripheral according to the specified
  29. */
  30. rt_uint32_t EMAC_init(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint32_t SystemCoreClock)
  31. {
  32. /*-------------------------------- Reset ethernet -------------------------------*/
  33. enet_deinit();
  34. enet_software_reset();
  35. /* configure the parameters which are usually less cared for enet initialization */
  36. enet_initpara_config(HALFDUPLEX_OPTION, ENET_CARRIERSENSE_DISABLE|ENET_RECEIVEOWN_ENABLE|ENET_RETRYTRANSMISSION_DISABLE|ENET_BACKOFFLIMIT_10|ENET_DEFERRALCHECK_DISABLE);
  37. /*-------------------------------- Initialize ENET ------------------------------*/
  38. enet_init_state = enet_init(ENET_AUTO_NEGOTIATION, ENET_AUTOCHECKSUM_DROP_FAILFRAMES, ENET_BROADCAST_FRAMES_PASS);
  39. /* Return Ethernet configuration success */
  40. return EMAC_SUCCESS;
  41. }
  42. /**
  43. * Enables or disables the specified ETHERNET DMA interrupts.
  44. */
  45. void EMAC_INT_config(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint32_t EMAC_DMA_IT, rt_bool_t NewState)
  46. {
  47. if (NewState)
  48. {
  49. /* Enable the selected ETHERNET DMA interrupts */
  50. ETHERNET_MAC->IER |= EMAC_DMA_IT;
  51. }
  52. else
  53. {
  54. /* Disable the selected ETHERNET DMA interrupts */
  55. ETHERNET_MAC->IER &=(~(rt_uint32_t)EMAC_DMA_IT);
  56. }
  57. }
  58. /**
  59. * Configures the selected MAC address.
  60. */
  61. void EMAC_MAC_Addr_config(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint32_t MacAddr, rt_uint8_t *Addr)
  62. {
  63. rt_uint32_t value;
  64. /* Calculate the selectecd MAC address high register */
  65. value = ((rt_uint32_t)Addr[5] << 8) | (rt_uint32_t)Addr[4];
  66. /* Load the selectecd MAC address high register */
  67. //(*(volatile rt_uint32_t *) (EMAC_MAC_ADDR_HBASE + MacAddr)) = value;
  68. ETHERNET_MAC->MARs[MacAddr].MARH = value;
  69. /* Calculate the selectecd MAC address low register */
  70. value = ((rt_uint32_t)Addr[3] << 24) | ((rt_uint32_t)Addr[2] << 16) | ((rt_uint32_t)Addr[1] << 8) | Addr[0];
  71. /* Load the selectecd MAC address low register */
  72. //(*(volatile rt_uint32_t *) (EMAC_MAC_ADDR_LBASE + MacAddr)) = value;
  73. ETHERNET_MAC->MARs[MacAddr].MARL = value;
  74. }
  75. /**
  76. * Enables or disables the MAC transmission.
  77. */
  78. void EMAC_MACTransmissionCmd(struct rt_synopsys_eth * ETHERNET_MAC, rt_bool_t NewState)
  79. {
  80. if (NewState)
  81. {
  82. /* Enable the MAC transmission */
  83. ETHERNET_MAC->MCR |= EMAC_MACCR_TE;
  84. }
  85. else
  86. {
  87. /* Disable the MAC transmission */
  88. ETHERNET_MAC->MCR &= ~EMAC_MACCR_TE;
  89. }
  90. }
  91. /**
  92. * Clears the ETHERNET transmit FIFO.
  93. */
  94. void EMAC_FlushTransmitFIFO(struct rt_synopsys_eth * ETHERNET_MAC)
  95. {
  96. /* Set the Flush Transmit FIFO bit */
  97. ETHERNET_MAC->OMR |= EMAC_DMAOMR_FTF;
  98. }
  99. /**
  100. * Enables or disables the MAC reception.
  101. */
  102. void EMAC_MACReceptionCmd(struct rt_synopsys_eth * ETHERNET_MAC, rt_bool_t NewState)
  103. {
  104. if (NewState)
  105. {
  106. /* Enable the MAC reception */
  107. ETHERNET_MAC->MCR |= EMAC_MACCR_RE;
  108. }
  109. else
  110. {
  111. /* Disable the MAC reception */
  112. ETHERNET_MAC->MCR &= ~EMAC_MACCR_RE;
  113. }
  114. }
  115. /**
  116. * Enables or disables the DMA transmission.
  117. */
  118. void EMAC_DMATransmissionCmd(struct rt_synopsys_eth * ETHERNET_MAC, rt_bool_t NewState)
  119. {
  120. if (NewState)
  121. {
  122. /* Enable the DMA transmission */
  123. ETHERNET_MAC->OMR |= EMAC_DMAOMR_ST;
  124. }
  125. else
  126. {
  127. /* Disable the DMA transmission */
  128. ETHERNET_MAC->OMR &= ~EMAC_DMAOMR_ST;
  129. }
  130. }
  131. /**
  132. * Enables or disables the DMA reception.
  133. */
  134. void EMAC_DMAReceptionCmd(struct rt_synopsys_eth * ETHERNET_MAC, rt_bool_t NewState)
  135. {
  136. if (NewState)
  137. {
  138. /* Enable the DMA reception */
  139. ETHERNET_MAC->OMR |= EMAC_DMAOMR_SR;
  140. }
  141. else
  142. {
  143. /* Disable the DMA reception */
  144. ETHERNET_MAC->OMR &= ~EMAC_DMAOMR_SR;
  145. }
  146. }
  147. /**
  148. * Enables ENET MAC and DMA reception/transmission
  149. */
  150. void EMAC_start(struct rt_synopsys_eth * ETHERNET_MAC)
  151. {
  152. /* Enable transmit state machine of the MAC for transmission on the MII */
  153. EMAC_MACTransmissionCmd(ETHERNET_MAC, RT_TRUE);
  154. /* Flush Transmit FIFO */
  155. enet_txfifo_flush();
  156. /* Enable receive state machine of the MAC for reception from the MII */
  157. EMAC_MACReceptionCmd(ETHERNET_MAC, RT_TRUE);
  158. /* Start DMA transmission */
  159. EMAC_DMATransmissionCmd(ETHERNET_MAC, RT_TRUE);
  160. /* Start DMA reception */
  161. EMAC_DMAReceptionCmd(ETHERNET_MAC, RT_TRUE);
  162. }
  163. /**
  164. * Clears the ETHERNET's DMA interrupt pending bit.
  165. */
  166. void EMAC_clear_pending(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint32_t pending)
  167. {
  168. /* Clear the selected ETHERNET DMA IT */
  169. ETHERNET_MAC->SR = (rt_uint32_t) pending;
  170. }
  171. /**
  172. * Resumes the DMA Transmission by writing to the DmaRxPollDemand register
  173. * (the data written could be anything). This forces the DMA to resume reception.
  174. */
  175. void EMAC_resume_reception(struct rt_synopsys_eth * ETHERNET_MAC)
  176. {
  177. ETHERNET_MAC->RPDR = 0;
  178. }
  179. /**
  180. * Resumes the DMA Transmission by writing to the DmaTxPollDemand register
  181. * (the data written could be anything). This forces the DMA to resume transmission.
  182. */
  183. void EMAC_resume_transmission(struct rt_synopsys_eth * ETHERNET_MAC)
  184. {
  185. ETHERNET_MAC->TPDR = 0;
  186. }
  187. /**
  188. * Read a PHY register
  189. */
  190. rt_uint16_t EMAC_PHY_read(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint16_t PHYAddress, rt_uint16_t PHYReg)
  191. {
  192. rt_uint32_t value = 0;
  193. volatile rt_uint32_t timeout = 0;
  194. /* Get the ETHERNET MACMIIAR value */
  195. value = ETHERNET_MAC->GAR;
  196. /* Keep only the CSR Clock Range CR[2:0] bits value */
  197. value &= ~MACMIIAR_CR_MASK;
  198. /* Prepare the MII address register value */
  199. value |=(((rt_uint32_t)PHYAddress<<11) & EMAC_MACMIIAR_PA); /* Set the PHY device address */
  200. value |=(((rt_uint32_t)PHYReg<<6) & EMAC_MACMIIAR_MR); /* Set the PHY register address */
  201. value &= ~EMAC_MACMIIAR_MW; /* Set the read mode */
  202. value |= EMAC_MACMIIAR_MB; /* Set the MII Busy bit */
  203. /* Write the result value into the MII Address register */
  204. ETHERNET_MAC->GAR = value;
  205. /* Check for the Busy flag */
  206. do
  207. {
  208. timeout++;
  209. value = ETHERNET_MAC->GAR;
  210. }
  211. while ((value & EMAC_MACMIIAR_MB) && (timeout < (rt_uint32_t)PHY_READ_TO));
  212. /* Return ERROR in case of timeout */
  213. if(timeout == PHY_READ_TO)
  214. {
  215. return (rt_uint16_t)EMAC_ERROR;
  216. }
  217. /* Return data register value */
  218. return (rt_uint16_t)(ETHERNET_MAC->GDR);
  219. }
  220. /**
  221. * Write to a PHY register
  222. */
  223. rt_uint32_t EMAC_PHY_write(struct rt_synopsys_eth * ETHERNET_MAC, rt_uint16_t PHYAddress, rt_uint16_t PHYReg, rt_uint16_t PHYValue)
  224. {
  225. rt_uint32_t value = 0;
  226. volatile rt_uint32_t timeout = 0;
  227. /* Get the ETHERNET MACMIIAR value */
  228. value = ETHERNET_MAC->GAR;
  229. /* Keep only the CSR Clock Range CR[2:0] bits value */
  230. value &= ~MACMIIAR_CR_MASK;
  231. /* Prepare the MII register address value */
  232. value |=(((rt_uint32_t)PHYAddress<<11) & EMAC_MACMIIAR_PA); /* Set the PHY device address */
  233. value |=(((rt_uint32_t)PHYReg<<6) & EMAC_MACMIIAR_MR); /* Set the PHY register address */
  234. value |= EMAC_MACMIIAR_MW; /* Set the write mode */
  235. value |= EMAC_MACMIIAR_MB; /* Set the MII Busy bit */
  236. /* Give the value to the MII data register */
  237. ETHERNET_MAC->GDR = PHYValue;
  238. /* Write the result value into the MII Address register */
  239. ETHERNET_MAC->GAR = value;
  240. /* Check for the Busy flag */
  241. do
  242. {
  243. timeout++;
  244. value = ETHERNET_MAC->GAR;
  245. }
  246. while ((value & EMAC_MACMIIAR_MB) && (timeout < (rt_uint32_t)PHY_WRITE_TO));
  247. /* Return ERROR in case of timeout */
  248. if(timeout == PHY_WRITE_TO)
  249. {
  250. return EMAC_ERROR;
  251. }
  252. /* Return SUCCESS */
  253. return EMAC_SUCCESS;
  254. }