emac_hal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "sdkconfig.h"
  16. #include "esp_attr.h"
  17. #include "soc/gpio_periph.h"
  18. #include "soc/rtc.h"
  19. #include "hal/emac.h"
  20. #define ETH_CRC_LENGTH (4)
  21. #if CONFIG_ETH_RMII_CLK_OUTPUT
  22. static void emac_config_apll_clock(void)
  23. {
  24. /* apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2) */
  25. rtc_xtal_freq_t rtc_xtal_freq = rtc_clk_xtal_freq_get();
  26. switch (rtc_xtal_freq) {
  27. case RTC_XTAL_FREQ_40M: // Recommended
  28. /* 50 MHz = 40MHz * (4 + 6) / (2 * (2 + 2) = 50.000 */
  29. /* sdm0 = 0, sdm1 = 0, sdm2 = 6, o_div = 2 */
  30. rtc_clk_apll_enable(true, 0, 0, 6, 2);
  31. break;
  32. case RTC_XTAL_FREQ_26M:
  33. /* 50 MHz = 26MHz * (4 + 15 + 118 / 256 + 39/65536) / ((3 + 2) * 2) = 49.999992 */
  34. /* sdm0 = 39, sdm1 = 118, sdm2 = 15, o_div = 3 */
  35. rtc_clk_apll_enable(true, 39, 118, 15, 3);
  36. break;
  37. case RTC_XTAL_FREQ_24M:
  38. /* 50 MHz = 24MHz * (4 + 12 + 255 / 256 + 255/65536) / ((2 + 2) * 2) = 49.499977 */
  39. /* sdm0 = 255, sdm1 = 255, sdm2 = 12, o_div = 2 */
  40. rtc_clk_apll_enable(true, 255, 255, 12, 2);
  41. break;
  42. default: // Assume we have a 40M xtal
  43. rtc_clk_apll_enable(true, 0, 0, 6, 2);
  44. break;
  45. }
  46. }
  47. #endif
  48. void emac_hal_init(emac_hal_context_t *hal, void *descriptors,
  49. uint8_t **rx_buf, uint8_t **tx_buf)
  50. {
  51. hal->dma_regs = &EMAC_DMA;
  52. hal->mac_regs = &EMAC_MAC;
  53. hal->ext_regs = &EMAC_EXT;
  54. hal->descriptors = descriptors;
  55. hal->rx_buf = rx_buf;
  56. hal->tx_buf = tx_buf;
  57. }
  58. void emac_hal_lowlevel_init(emac_hal_context_t *hal)
  59. {
  60. /* GPIO configuration */
  61. /* TX_EN to GPIO21 */
  62. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO21_U, FUNC_GPIO21_EMAC_TX_EN);
  63. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[21]);
  64. /* TXD0 to GPIO19 */
  65. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U, FUNC_GPIO19_EMAC_TXD0);
  66. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[19]);
  67. /* TXD1 to GPIO22 */
  68. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO22_U, FUNC_GPIO22_EMAC_TXD1);
  69. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[22]);
  70. /* RXD0 to GPIO25 */
  71. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO25_U, FUNC_GPIO25_EMAC_RXD0);
  72. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[25]);
  73. /* RXD1 to GPIO26 */
  74. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO26_U, FUNC_GPIO26_EMAC_RXD1);
  75. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[26]);
  76. /* CRS_DV to GPIO27 */
  77. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO27_U, FUNC_GPIO27_EMAC_RX_DV);
  78. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[27]);
  79. #if CONFIG_ETH_RMII_CLK_INPUT
  80. #if CONFIG_ETH_RMII_CLK_IN_GPIO == 0
  81. /* RMII clock (50MHz) input to GPIO0 */
  82. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_EMAC_TX_CLK);
  83. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[0]);
  84. #else
  85. #error "ESP32 EMAC only support input RMII clock to GPIO0"
  86. #endif
  87. #endif
  88. #if CONFIG_ETH_RMII_CLK_OUTPUT
  89. #if CONFIG_ETH_RMII_CLK_OUTPUT_GPIO0
  90. /* APLL clock output to GPIO0 (must be configured to 50MHz!) */
  91. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  92. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[0]);
  93. #elif CONFIG_ETH_RMII_CLK_OUT_GPIO == 16
  94. /* RMII CLK (50MHz) output to GPIO16 */
  95. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO16_U, FUNC_GPIO16_EMAC_CLK_OUT);
  96. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[16]);
  97. #elif CONFIG_ETH_RMII_CLK_OUT_GPIO == 17
  98. /* RMII CLK (50MHz) output to GPIO17 */
  99. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO17_U, FUNC_GPIO17_EMAC_CLK_OUT_180);
  100. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[17]);
  101. #endif
  102. #endif // CONFIG_ETH_RMII_CLK_OUTPUT
  103. /* Clock configuration */
  104. #if CONFIG_ETH_PHY_INTERFACE_MII
  105. hal->ext_regs->ex_phyinf_conf.phy_intf_sel = 0;
  106. hal->ext_regs->ex_clk_ctrl.mii_clk_rx_en = 1;
  107. hal->ext_regs->ex_clk_ctrl.mii_clk_tx_en = 1;
  108. #elif CONFIG_ETH_PHY_INTERFACE_RMII
  109. hal->ext_regs->ex_phyinf_conf.phy_intf_sel = 4;
  110. #if CONFIG_ETH_RMII_CLK_INPUT
  111. hal->ext_regs->ex_clk_ctrl.ext_en = 1;
  112. hal->ext_regs->ex_clk_ctrl.int_en = 0;
  113. hal->ext_regs->ex_oscclk_conf.clk_sel = 1;
  114. #elif CONFIG_ETH_RMII_CLK_OUTPUT
  115. hal->ext_regs->ex_clk_ctrl.ext_en = 0;
  116. hal->ext_regs->ex_clk_ctrl.int_en = 1;
  117. hal->ext_regs->ex_oscclk_conf.clk_sel = 0;
  118. emac_config_apll_clock();
  119. hal->ext_regs->ex_clkout_conf.div_num = 0;
  120. hal->ext_regs->ex_clkout_conf.h_div_num = 0;
  121. #if CONFIG_ETH_RMII_CLK_OUTPUT_GPIO0
  122. /* Choose the APLL clock to output on GPIO */
  123. REG_WRITE(PIN_CTRL, 6);
  124. #endif // CONFIG_RMII_CLK_OUTPUT_GPIO0
  125. #endif // CONFIG_ETH_RMII_CLK_INPUT
  126. #endif // CONFIG_ETH_PHY_INTERFACE_MII
  127. }
  128. void emac_hal_reset(emac_hal_context_t *hal)
  129. {
  130. hal->dma_regs->dmabusmode.sw_rst = 1;
  131. }
  132. bool emac_hal_is_reset_done(emac_hal_context_t *hal)
  133. {
  134. return hal->dma_regs->dmabusmode.sw_rst ? false : true;
  135. }
  136. void emac_hal_set_csr_clock_range(emac_hal_context_t *hal)
  137. {
  138. /* Tell MAC system clock Frequency, which will determin the frequency range of MDC(1MHz~2.5MHz) */
  139. if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ >= 20 && CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ < 35) {
  140. hal->mac_regs->emacgmiiaddr.miicsrclk = 2;
  141. } else if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ >= 35 && CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ < 60) {
  142. hal->mac_regs->emacgmiiaddr.miicsrclk = 3;
  143. } else if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ >= 60 && CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ < 100) {
  144. hal->mac_regs->emacgmiiaddr.miicsrclk = 0;
  145. } else if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ >= 100 && CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ < 150) {
  146. hal->mac_regs->emacgmiiaddr.miicsrclk = 1;
  147. } else if (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ > 150 && CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ < 250) {
  148. hal->mac_regs->emacgmiiaddr.miicsrclk = 4;
  149. } else {
  150. hal->mac_regs->emacgmiiaddr.miicsrclk = 5;
  151. }
  152. }
  153. void emac_hal_reset_desc_chain(emac_hal_context_t *hal)
  154. {
  155. /* reset DMA descriptors */
  156. hal->rx_desc = (eth_dma_rx_descriptor_t *)(hal->descriptors);
  157. hal->tx_desc = (eth_dma_tx_descriptor_t *)(hal->descriptors +
  158. sizeof(eth_dma_rx_descriptor_t) * CONFIG_ETH_DMA_RX_BUFFER_NUM);
  159. /* init rx chain */
  160. for (int i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) {
  161. /* Set Own bit of the Rx descriptor Status: DMA */
  162. hal->rx_desc[i].RDES0.Own = 1;
  163. /* Set Buffer1 size and Second Address Chained bit */
  164. hal->rx_desc[i].RDES1.SecondAddressChained = 1;
  165. hal->rx_desc[i].RDES1.ReceiveBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  166. /* Enable Ethernet DMA Rx Descriptor interrupt */
  167. hal->rx_desc[i].RDES1.DisableInterruptOnComplete = 0;
  168. /* point to the buffer */
  169. hal->rx_desc[i].Buffer1Addr = (uint32_t)(hal->rx_buf[i]);
  170. /* point to next descriptor */
  171. hal->rx_desc[i].Buffer2NextDescAddr = (uint32_t)(hal->rx_desc + i + 1);
  172. }
  173. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  174. hal->rx_desc[CONFIG_ETH_DMA_RX_BUFFER_NUM - 1].Buffer2NextDescAddr = (uint32_t)(hal->rx_desc);
  175. /* init tx chain */
  176. for (int i = 0; i < CONFIG_ETH_DMA_TX_BUFFER_NUM; i++) {
  177. /* Set Second Address Chained bit */
  178. hal->tx_desc[i].TDES0.SecondAddressChained = 1;
  179. hal->tx_desc[i].TDES1.TransmitBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  180. /* Enable Ethernet DMA Tx Descriptor interrupt */
  181. hal->tx_desc[1].TDES0.InterruptOnComplete = 1;
  182. /* Enable Transmit Timestamp */
  183. hal->tx_desc[i].TDES0.TransmitTimestampEnable = 1;
  184. /* point to the buffer */
  185. hal->tx_desc[i].Buffer1Addr = (uint32_t)(hal->tx_buf[i]);
  186. /* point to next descriptor */
  187. hal->tx_desc[i].Buffer2NextDescAddr = (uint32_t)(hal->tx_desc + i + 1);
  188. }
  189. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  190. hal->tx_desc[CONFIG_ETH_DMA_TX_BUFFER_NUM - 1].Buffer2NextDescAddr = (uint32_t)(hal->tx_desc);
  191. /* set base address of the first descriptor */
  192. hal->dma_regs->dmarxbaseaddr = (uint32_t)hal->rx_desc;
  193. hal->dma_regs->dmatxbaseaddr = (uint32_t)hal->tx_desc;
  194. }
  195. void emac_hal_init_mac_default(emac_hal_context_t *hal)
  196. {
  197. /* MACCR Configuration */
  198. typeof(hal->mac_regs->gmacconfig) maccr = hal->mac_regs->gmacconfig;
  199. /* Enable the watchdog on the receiver, frame longer than 2048 Bytes is not allowed */
  200. maccr.watchdog = EMAC_WATCHDOG_ENABLE;
  201. /* Enable the jabber timer on the transmitter, frame longer than 2048 Bytes is not allowed */
  202. maccr.jabber = EMAC_JABBER_ENABLE;
  203. /* minimum IFG between frames during transmission is 96 bit times */
  204. maccr.interframegap = EMAC_INTERFRAME_GAP_96BIT;
  205. /* Enable Carrier Sense During Transmission */
  206. maccr.disablecrs = EMAC_CARRIERSENSE_ENABLE;
  207. /* Select port: 10/100 Mbps */
  208. maccr.mii = EMAC_PORT_10_100MBPS;
  209. /* Select speed: here set default 100M, afterwards, will reset by auto-negotiation */
  210. maccr.fespeed = EMAC_SPEED_100M;
  211. /* Allow the reception of frames when the TX_EN signal is asserted in Half-Duplex mode */
  212. maccr.rxown = EMAC_RECEIVE_OWN_ENABLE;
  213. /* Disable internal loopback mode */
  214. maccr.loopback = EMAC_LOOPBACK_DISABLE;
  215. /* Select duplex mode: here set default full duplex, afterwards, will reset by auto-negotiation */
  216. maccr.duplex = EMAC_DUPLEX_FULL;
  217. /* Select the checksum mode for received frame payload's TCP/UDP/ICMP headers */
  218. maccr.rxipcoffload = EMAC_CHECKSUM_HW;
  219. /* Enable MAC retry transmission when a colision occurs in half duplex mode */
  220. maccr.retry = EMAC_RETRY_TRANSMISSION_ENABLE;
  221. /* MAC passes all incoming frames to host, without modifying them */
  222. maccr.padcrcstrip = EMAC_AUTO_PAD_CRC_STRIP_DISABLE;
  223. /* Set Back-Off limit time before retry a transmittion after a collision */
  224. maccr.backofflimit = EMAC_BACKOFF_LIMIT_10;
  225. /* Disable deferral check, MAC defers until the CRS signal goes inactive */
  226. maccr.deferralcheck = EMAC_DEFERRAL_CHECK_DISABLE;
  227. /* Set preamble length 7 Bytes */
  228. maccr.pltf = EMAC_PREAMBLE_LENGTH_7;
  229. hal->mac_regs->gmacconfig = maccr;
  230. /* MACFFR Configuration */
  231. typeof(hal->mac_regs->gmacff) macffr = hal->mac_regs->gmacff;
  232. /* Receiver module passes only those frames to the Application that pass the SA or DA address filter */
  233. macffr.receive_all = EMAC_RECEIVE_ALL_DISABLE;
  234. /* Disable source address filter */
  235. macffr.safe = EMAC_SOURCE_ADDR_FILTER_DISABLE;
  236. macffr.saif = 0;
  237. /* MAC blocks all control frames */
  238. macffr.pcf = EMAC_CONTROL_FRAME_BLOCKALL;
  239. /* AFM module passes all received broadcast frames and multicast frames */
  240. macffr.dbf = EMAC_RECEPT_BROADCAST_ENABLE;
  241. macffr.pam = 1;
  242. /* Address Check block operates in normal filtering mode for the DA address */
  243. macffr.daif = EMAC_DEST_ADDR_FILTER_NORMAL;
  244. /* Disable Promiscuous Mode */
  245. macffr.pmode = EMAC_PROMISCUOUS_DISABLE;
  246. hal->mac_regs->gmacff = macffr;
  247. }
  248. void emac_hal_enable_flow_ctrl(emac_hal_context_t *hal, bool enable)
  249. {
  250. /* MACFCR Configuration */
  251. typeof(hal->mac_regs->gmacfc) macfcr = hal->mac_regs->gmacfc;
  252. if (enable) {
  253. /* Pause time */
  254. macfcr.pause_time = EMAC_PAUSE_TIME;
  255. /* Enable generation of Zero-Quanta Pause Control frames */
  256. macfcr.dzpq = EMAC_ZERO_QUANTA_PAUSE_ENABLE;
  257. /* Threshold of the PAUSE to be checked for automatic retransmission of PAUSE Frame */
  258. macfcr.plt = EMAC_PAUSE_LOW_THRESHOLD_MINUS_28;
  259. /* Don't allow MAC detect Pause frames with MAC address0 unicast address and unique multicast address */
  260. macfcr.upfd = EMAC_UNICAST_PAUSE_DETECT_DISABLE;
  261. /* Enable MAC to decode the received Pause frame and disable its transmitter for a specific time */
  262. macfcr.rfce = EMAC_RECEIVE_FLOW_CONTROL_ENABLE;
  263. /* Enable MAC to transmit Pause frames in full duplex mode or the MAC back-pressure operation in half duplex mode */
  264. macfcr.tfce = EMAC_TRANSMIT_FLOW_CONTROL_ENABLE;
  265. } else {
  266. macfcr.val = 0;
  267. }
  268. hal->mac_regs->gmacfc = macfcr;
  269. }
  270. void emac_hal_init_dma_default(emac_hal_context_t *hal)
  271. {
  272. /* DMAOMR Configuration */
  273. typeof(hal->dma_regs->dmaoperation_mode) dmaomr = hal->dma_regs->dmaoperation_mode;
  274. /* Enable Dropping of TCP/IP Checksum Error Frames */
  275. dmaomr.dis_drop_tcpip_err_fram = EMAC_DROP_TCPIP_CHECKSUM_ERROR_ENABLE;
  276. /* Enable Receive Store Forward */
  277. dmaomr.rx_store_forward = EMAC_RECEIVE_STORE_FORWARD_ENABLE;
  278. /* Enable Flushing of Received Frames because of the unavailability of receive descriptors or buffers */
  279. dmaomr.dis_flush_recv_frames = EMAC_FLUSH_RECEIVED_FRAME_ENABLE;
  280. /* Enable Transmit Store Forward */
  281. dmaomr.tx_str_fwd = EMAC_TRANSMIT_STORE_FORWARD_ENABLE;
  282. /* Flush Transmit FIFO */
  283. dmaomr.flush_tx_fifo = 1;
  284. /* Transmit Threshold Control */
  285. dmaomr.tx_thresh_ctrl = EMAC_TRANSMIT_THRESHOLD_CONTROL_64;
  286. /* Disable Forward Error Frame */
  287. dmaomr.fwd_err_frame = EMAC_FORWARD_ERROR_FRAME_DISABLE;
  288. /* Disable forward undersized good frame */
  289. dmaomr.fwd_under_gf = EMAC_FORWARD_UNDERSIZED_GOOD_FRAME_DISABLE;
  290. /* Receive Threshold Control */
  291. dmaomr.rx_thresh_ctrl = EMAC_RECEIVE_THRESHOLD_CONTROL_64;
  292. /* Allow the DMA to process a second frame of Transmit data even before obtaining the status for the first frame */
  293. dmaomr.opt_second_frame = EMAC_OPERATE_SECOND_FRAME_ENABLE;
  294. hal->dma_regs->dmaoperation_mode = dmaomr;
  295. /* DMABMR Configuration */
  296. typeof(hal->dma_regs->dmabusmode) dmabmr = hal->dma_regs->dmabusmode;
  297. /* Enable Mixed Burst */
  298. dmabmr.dmamixedburst = EMAC_MIXED_BURST_ENABLE;
  299. /* Enable Address Aligned Beates */
  300. dmabmr.dmaaddralibea = EMAC_ADDR_ALIGN_BEATS_ENABLE;
  301. /* Use Separate PBL */
  302. dmabmr.use_sep_pbl = EMAC_USE_SEPARATE_PBL;
  303. /* Set Rx/Tx DMA Burst Length */
  304. dmabmr.rx_dma_pbl = EMAC_DMA_BURST_LENGTH_32BEAT;
  305. dmabmr.prog_burst_len = EMAC_DMA_BURST_LENGTH_32BEAT;
  306. /* Enable Enhanced Descriptor,8 Words(32 Bytes) */
  307. dmabmr.alt_desc_size = EMAC_ENHANCED_DESCRIPTOR_ENABLE;
  308. /* Specifies the number of word to skip between two unchained descriptors (Ring mode) */
  309. dmabmr.desc_skip_len = 0;
  310. /* DMA Arbitration Scheme */
  311. dmabmr.dma_arb_sch = EMAC_DMA_ARBITRATION_SCHEME_ROUNDROBIN;
  312. /* Set priority ratio in the weighted round-robin arbitration between Rx DMA and Tx DMA */
  313. dmabmr.pri_ratio = EMAC_DMA_ARBITRATION_ROUNDROBIN_RXTX_1_1;
  314. hal->dma_regs->dmabusmode = dmabmr;
  315. }
  316. void emac_hal_set_speed(emac_hal_context_t *hal, uint32_t speed)
  317. {
  318. hal->mac_regs->gmacconfig.fespeed = speed;
  319. }
  320. void emac_hal_set_duplex(emac_hal_context_t *hal, uint32_t duplex)
  321. {
  322. hal->mac_regs->gmacconfig.duplex = duplex;
  323. }
  324. void emac_hal_set_promiscuous(emac_hal_context_t *hal, bool enable)
  325. {
  326. if (enable) {
  327. hal->mac_regs->gmacff.pmode = 1;
  328. } else {
  329. hal->mac_regs->gmacff.pmode = 0;
  330. }
  331. }
  332. void emac_hal_send_pause_frame(emac_hal_context_t *hal, bool enable)
  333. {
  334. if (enable) {
  335. hal->ext_regs->ex_phyinf_conf.sbd_flowctrl = 1;
  336. } else {
  337. hal->ext_regs->ex_phyinf_conf.sbd_flowctrl = 0;
  338. }
  339. }
  340. bool emac_hal_is_mii_busy(emac_hal_context_t *hal)
  341. {
  342. return hal->mac_regs->emacgmiiaddr.miibusy ? true : false;
  343. }
  344. void emac_hal_set_phy_cmd(emac_hal_context_t *hal, uint32_t phy_addr, uint32_t phy_reg, bool write)
  345. {
  346. typeof(hal->mac_regs->emacgmiiaddr) macmiiar = hal->mac_regs->emacgmiiaddr;
  347. macmiiar.miidev = phy_addr;
  348. /* Set the PHY register address */
  349. macmiiar.miireg = phy_reg;
  350. if (write) {
  351. /* Set write mode */
  352. macmiiar.miiwrite = 1;
  353. } else {
  354. /* Set read mode */
  355. macmiiar.miiwrite = 0;
  356. }
  357. /* Set MII busy bit */
  358. macmiiar.miibusy = 1;
  359. /* Write the result value into the MII Address register */
  360. hal->mac_regs->emacgmiiaddr = macmiiar;
  361. }
  362. void emac_hal_set_phy_data(emac_hal_context_t *hal, uint32_t reg_value)
  363. {
  364. hal->mac_regs->emacmiidata.mii_data = reg_value;
  365. }
  366. uint32_t emac_hal_get_phy_data(emac_hal_context_t *hal)
  367. {
  368. return hal->mac_regs->emacmiidata.mii_data;
  369. }
  370. void emac_hal_set_address(emac_hal_context_t *hal, uint8_t *mac_addr)
  371. {
  372. /* Make sure mac address is unicast type */
  373. if (!(mac_addr[0] & 0x01)) {
  374. hal->mac_regs->emacaddr0high.address0_hi = (mac_addr[5] << 8) | mac_addr[4];
  375. hal->mac_regs->emacaddr0low = (mac_addr[3] << 24) | (mac_addr[2] << 16) | (mac_addr[1] << 8) | (mac_addr[0]);
  376. }
  377. }
  378. void emac_hal_start(emac_hal_context_t *hal)
  379. {
  380. typeof(hal->dma_regs->dmaoperation_mode) opm = hal->dma_regs->dmaoperation_mode;
  381. typeof(hal->mac_regs->gmacconfig) cfg = hal->mac_regs->gmacconfig;
  382. /* Enable Ethernet MAC and DMA Interrupt */
  383. hal->dma_regs->dmain_en.val = 0xFFFFFFFF;
  384. /* Flush Transmit FIFO */
  385. opm.flush_tx_fifo = 1;
  386. /* Start DMA transmission */
  387. opm.start_stop_transmission_command = 1;
  388. /* Start DMA reception */
  389. opm.start_stop_rx = 1;
  390. /* Enable transmit state machine of the MAC for transmission on the MII */
  391. cfg.tx = 1;
  392. /* Enable receive state machine of the MAC for reception from the MII */
  393. cfg.rx = 1;
  394. hal->dma_regs->dmaoperation_mode = opm;
  395. hal->mac_regs->gmacconfig = cfg;
  396. /* Clear all pending interrupts */
  397. hal->dma_regs->dmastatus.val = 0xFFFFFFFF;
  398. }
  399. void emac_hal_stop(emac_hal_context_t *hal)
  400. {
  401. typeof(hal->dma_regs->dmaoperation_mode) opm = hal->dma_regs->dmaoperation_mode;
  402. typeof(hal->mac_regs->gmacconfig) cfg = hal->mac_regs->gmacconfig;
  403. /* Flush Transmit FIFO */
  404. opm.flush_tx_fifo = 1;
  405. /* Stop DMA transmission */
  406. opm.start_stop_transmission_command = 0;
  407. /* Stop DMA reception */
  408. opm.start_stop_rx = 0;
  409. /* Disable receive state machine of the MAC for reception from the MII */
  410. cfg.rx = 0;
  411. /* Disable transmit state machine of the MAC for transmission on the MII */
  412. cfg.tx = 0;
  413. hal->dma_regs->dmaoperation_mode = opm;
  414. hal->mac_regs->gmacconfig = cfg;
  415. /* Disable Ethernet MAC and DMA Interrupt */
  416. hal->dma_regs->dmain_en.val = 0x0;
  417. }
  418. uint32_t emac_hal_get_tx_desc_owner(emac_hal_context_t *hal)
  419. {
  420. return hal->tx_desc->TDES0.Own;
  421. }
  422. uint32_t emac_hal_transmit_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t length)
  423. {
  424. /* Get the number of Tx buffers to use for the frame */
  425. uint32_t bufcount = 0;
  426. uint32_t lastlen = length;
  427. uint32_t sentout = 0;
  428. while (lastlen > CONFIG_ETH_DMA_BUFFER_SIZE) {
  429. lastlen -= CONFIG_ETH_DMA_BUFFER_SIZE;
  430. bufcount++;
  431. }
  432. if (lastlen) {
  433. bufcount++;
  434. }
  435. if (bufcount > CONFIG_ETH_DMA_TX_BUFFER_NUM) {
  436. goto err;
  437. }
  438. eth_dma_tx_descriptor_t *desc_iter = hal->tx_desc;
  439. /* A frame is transmitted in multiple descriptor */
  440. for (int i = 0; i < bufcount; i++) {
  441. /* Check if the descriptor is owned by the Ethernet DMA (when 1) or CPU (when 0) */
  442. if (desc_iter->TDES0.Own != EMAC_DMADESC_OWNER_CPU) {
  443. goto err;
  444. }
  445. /* Clear FIRST and LAST segment bits */
  446. desc_iter->TDES0.FirstSegment = 0;
  447. desc_iter->TDES0.LastSegment = 0;
  448. desc_iter->TDES0.InterruptOnComplete = 0;
  449. if (i == 0) {
  450. /* Setting the first segment bit */
  451. desc_iter->TDES0.FirstSegment = 1;
  452. }
  453. if (i == (bufcount - 1)) {
  454. /* Setting the last segment bit */
  455. desc_iter->TDES0.LastSegment = 1;
  456. /* Enable transmit interrupt */
  457. desc_iter->TDES0.InterruptOnComplete = 1;
  458. /* Program size */
  459. desc_iter->TDES1.TransmitBuffer1Size = lastlen;
  460. /* copy data from uplayer stack buffer */
  461. memcpy((void *)(desc_iter->Buffer1Addr), buf + i * CONFIG_ETH_DMA_BUFFER_SIZE, lastlen);
  462. sentout += lastlen;
  463. } else {
  464. /* Program size */
  465. desc_iter->TDES1.TransmitBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  466. /* copy data from uplayer stack buffer */
  467. memcpy((void *)(desc_iter->Buffer1Addr), buf + i * CONFIG_ETH_DMA_BUFFER_SIZE, CONFIG_ETH_DMA_BUFFER_SIZE);
  468. sentout += CONFIG_ETH_DMA_BUFFER_SIZE;
  469. }
  470. /* Point to next descriptor */
  471. desc_iter = (eth_dma_tx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  472. }
  473. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  474. for (int i = 0; i < bufcount; i++) {
  475. hal->tx_desc->TDES0.Own = EMAC_DMADESC_OWNER_DMA;
  476. hal->tx_desc = (eth_dma_tx_descriptor_t *)(hal->tx_desc->Buffer2NextDescAddr);
  477. }
  478. hal->dma_regs->dmatxpolldemand = 0;
  479. return sentout;
  480. err:
  481. return 0;
  482. }
  483. uint32_t emac_hal_receive_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t size, uint32_t *frames_remain, uint32_t *free_desc)
  484. {
  485. eth_dma_rx_descriptor_t *desc_iter = NULL;
  486. eth_dma_rx_descriptor_t *first_desc = NULL;
  487. uint32_t used_descs = 0;
  488. uint32_t seg_count = 0;
  489. uint32_t ret_len = 0;
  490. uint32_t copy_len = 0;
  491. uint32_t write_len = 0;
  492. uint32_t frame_count = 0;
  493. first_desc = hal->rx_desc;
  494. desc_iter = hal->rx_desc;
  495. /* Traverse descriptors owned by CPU */
  496. while ((desc_iter->RDES0.Own != EMAC_DMADESC_OWNER_DMA) && (used_descs < CONFIG_ETH_DMA_RX_BUFFER_NUM) && !frame_count) {
  497. used_descs++;
  498. seg_count++;
  499. /* Last segment in frame */
  500. if (desc_iter->RDES0.LastDescriptor) {
  501. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  502. ret_len = desc_iter->RDES0.FrameLength - ETH_CRC_LENGTH;
  503. /* packets larger than expected will be truncated */
  504. copy_len = ret_len > size ? size : ret_len;
  505. /* update unhandled frame count */
  506. frame_count++;
  507. }
  508. /* First segment in frame */
  509. if (desc_iter->RDES0.FirstDescriptor) {
  510. first_desc = desc_iter;
  511. }
  512. /* point to next descriptor */
  513. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  514. }
  515. /* there's at least one frame to process */
  516. if (frame_count) {
  517. /* check how many frames left to handle */
  518. while ((desc_iter->RDES0.Own != EMAC_DMADESC_OWNER_DMA) && (used_descs < CONFIG_ETH_DMA_RX_BUFFER_NUM)) {
  519. used_descs++;
  520. if (desc_iter->RDES0.LastDescriptor) {
  521. frame_count++;
  522. }
  523. /* point to next descriptor */
  524. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  525. }
  526. desc_iter = first_desc;
  527. for (int i = 0; i < seg_count - 1; i++) {
  528. used_descs--;
  529. write_len = copy_len < CONFIG_ETH_DMA_BUFFER_SIZE ? copy_len : CONFIG_ETH_DMA_BUFFER_SIZE;
  530. /* copy data to buffer */
  531. memcpy(buf, (void *)(desc_iter->Buffer1Addr), write_len);
  532. buf += write_len;
  533. copy_len -= write_len;
  534. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  535. desc_iter->RDES0.Own = EMAC_DMADESC_OWNER_DMA;
  536. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  537. }
  538. memcpy(buf, (void *)(desc_iter->Buffer1Addr), copy_len);
  539. desc_iter->RDES0.Own = EMAC_DMADESC_OWNER_DMA;
  540. /* update rxdesc */
  541. hal->rx_desc = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  542. /* poll rx demand */
  543. hal->dma_regs->dmarxpolldemand = 0;
  544. frame_count--;
  545. used_descs--;
  546. }
  547. *frames_remain = frame_count;
  548. *free_desc = CONFIG_ETH_DMA_RX_BUFFER_NUM - used_descs;
  549. return ret_len;
  550. }
  551. IRAM_ATTR void emac_hal_isr(void *arg)
  552. {
  553. emac_hal_context_t *hal = (emac_hal_context_t *)arg;
  554. typeof(hal->dma_regs->dmastatus) dma_status = hal->dma_regs->dmastatus;
  555. hal->dma_regs->dmastatus.val = dma_status.val;
  556. /* DMA Normal Interrupt */
  557. if (dma_status.norm_int_summ) {
  558. /* Transmit Interrupt */
  559. if (dma_status.trans_int) {
  560. emac_hal_tx_complete_cb(arg);
  561. }
  562. /* Transmit Buffer Unavailable */
  563. if (dma_status.trans_buf_unavail) {
  564. emac_hal_tx_unavail_cb(arg);
  565. }
  566. /* Receive Interrupt */
  567. if (dma_status.recv_int) {
  568. emac_hal_rx_complete_cb(arg);
  569. }
  570. /* Early Receive Interrupt */
  571. if (dma_status.early_recv_int) {
  572. emac_hal_rx_early_cb(arg);
  573. }
  574. }
  575. /* DMA Abnormal Interrupt */
  576. if (dma_status.abn_int_summ) {
  577. /* Transmit Process Stopped */
  578. if (dma_status.trans_proc_stop) {
  579. }
  580. /* Transmit Jabber Timeout */
  581. if (dma_status.trans_jabber_to) {
  582. }
  583. /* Receive FIFO Overflow */
  584. if (dma_status.recv_ovflow) {
  585. }
  586. /* Transmit Underflow */
  587. if (dma_status.trans_undflow) {
  588. }
  589. /* Receive Buffer Unavailable */
  590. if (dma_status.recv_buf_unavail) {
  591. emac_hal_rx_unavail_cb(arg);
  592. }
  593. /* Receive Process Stopped */
  594. if (dma_status.recv_proc_stop) {
  595. }
  596. /* Receive Watchdog Timeout */
  597. if (dma_status.recv_wdt_to) {
  598. }
  599. /* Early Transmit Interrupt */
  600. if (dma_status.early_trans_int) {
  601. }
  602. /* Fatal Bus Error */
  603. if (dma_status.fatal_bus_err_int) {
  604. }
  605. }
  606. }
  607. IRAM_ATTR __attribute__((weak)) void emac_hal_tx_complete_cb(void *arg)
  608. {
  609. // This is a weak function, do nothing by default
  610. // Upper code can rewrite this function
  611. // Note: you're in the interrupt context
  612. return;
  613. }
  614. IRAM_ATTR __attribute__((weak)) void emac_hal_tx_unavail_cb(void *arg)
  615. {
  616. // This is a weak function, do nothing by default
  617. // Upper code can rewrite this function
  618. // Note: you're in the interrupt context
  619. return;
  620. }
  621. IRAM_ATTR __attribute__((weak)) void emac_hal_rx_complete_cb(void *arg)
  622. {
  623. // This is a weak function, do nothing by default
  624. // Upper code can rewrite this function
  625. // Note: you're in the interrupt context
  626. return;
  627. }
  628. IRAM_ATTR __attribute__((weak)) void emac_hal_rx_early_cb(void *arg)
  629. {
  630. // This is a weak function, do nothing by default
  631. // Upper code can rewrite this function
  632. // Note: you're in the interrupt context
  633. return;
  634. }
  635. IRAM_ATTR __attribute__((weak)) void emac_hal_rx_unavail_cb(void *arg)
  636. {
  637. // This is a weak function, do nothing by default
  638. // Upper code can rewrite this function
  639. // Note: you're in the interrupt context
  640. return;
  641. }