enet.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2011-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __ENET_H__
  31. #define __ENET_H__
  32. #include "registers/regsenet.h"
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Definitions
  35. ////////////////////////////////////////////////////////////////////////////////
  36. //! @brief Definitions of the event bits.
  37. enum {
  38. ENET_EVENT_HBERR = 0x80000000,
  39. ENET_EVENT_BABR = BM_ENET_EIR_BABR,
  40. ENET_EVENT_BABT = BM_ENET_EIR_BABT,
  41. ENET_EVENT_GRA = BM_ENET_EIR_GRA,
  42. ENET_EVENT_TXF = BM_ENET_EIR_TXF,
  43. ENET_EVENT_TXB = BM_ENET_EIR_TXB,
  44. ENET_EVENT_RXF = BM_ENET_EIR_RXF,
  45. ENET_EVENT_RXB = BM_ENET_EIR_RXB,
  46. ENET_EVENT_MII = BM_ENET_EIR_MII,
  47. ENET_EVENT_EBERR = BM_ENET_EIR_EBERR,
  48. ENET_EVENT_LC = BM_ENET_EIR_LC,
  49. ENET_EVENT_RL = BM_ENET_EIR_RL,
  50. ENET_EVENT_UN = BM_ENET_EIR_UN,
  51. ENET_EVENT_TX = ENET_EVENT_TXF,
  52. ENET_EVENT_TX_ERR = (ENET_EVENT_BABT | ENET_EVENT_LC | ENET_EVENT_RL | ENET_EVENT_UN),
  53. ENET_EVENT_RX = ENET_EVENT_RXF,
  54. ENET_EVENT_ERR = (ENET_EVENT_HBERR | ENET_EVENT_EBERR)
  55. };
  56. //! @brief MII type
  57. enum imx_mii_type {
  58. MII,
  59. RMII,
  60. RGMII,
  61. };
  62. // Forward declaration.
  63. typedef struct imx_enet_bd imx_enet_bd_t;
  64. //! @brief Data structure for ENET device
  65. typedef struct imx_enet_priv_s {
  66. hw_enet_t *enet_reg; //!< the reister base address of ENET
  67. uint8_t phy_addr; //!< the address of PHY which associated with ENET controller
  68. uint32_t phy_id; //!< ID of the PHY
  69. uint8_t tx_busy; //!< 0:free, 1:transmitting frame
  70. uint8_t res[2];
  71. uint32_t status; //!< the status of ENET device:link-status etc.
  72. uint32_t tx_key; //!< save the key delivered from send function
  73. imx_enet_bd_t *rx_bd; //!< the receive buffer description ring
  74. imx_enet_bd_t *rx_cur; //!< the next recveive buffer description
  75. imx_enet_bd_t *tx_bd; //!< the transmit buffer description rign
  76. imx_enet_bd_t *tx_cur; //!< the next transmit buffer description
  77. // TODO: Add interrupt about fields
  78. // TODO: Add timer about fields
  79. } imx_enet_priv_t;
  80. //! @brief Definitions of the status field of imx_enet_priv_t.
  81. enum {
  82. ENET_STATUS_LINK_ON = 0x80000000,
  83. ENET_STATUS_FULL_DPLX = 0x40000000,
  84. ENET_STATUS_AUTO_NEG = 0x20000000,
  85. ENET_STATUS_10M = 0x8000000,
  86. ENET_STATUS_100M = 0x10000000,
  87. ENET_STATUS_1000M = 0x20000000
  88. };
  89. ////////////////////////////////////////////////////////////////////////////////
  90. // API
  91. ////////////////////////////////////////////////////////////////////////////////
  92. #if defined(__cplusplus)
  93. extern "C" {
  94. #endif
  95. /*!
  96. * @brief Enable ENET and start transfer.
  97. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  98. * @param enaddr a pointer of MAC address
  99. *
  100. * @return none
  101. */
  102. void imx_enet_start(imx_enet_priv_t * dev, unsigned char *enaddr);
  103. /*!
  104. * @brief Disable ENET
  105. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  106. *
  107. * @return none
  108. */
  109. void imx_enet_stop(imx_enet_priv_t * dev);
  110. /*!
  111. * @brief Initialize ENET PHY, like LAN8700, 8720, AR8031, etc
  112. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  113. *
  114. * @return none
  115. */
  116. void imx_enet_phy_init(imx_enet_priv_t * dev);
  117. /*!
  118. * @brief Reads the current status from the PHY.
  119. * @param dev Pointer to the enet interface struct.
  120. * @return Current status of the PHY. This is a bitmask composed of the ENET_STATUS_x enums,
  121. * such as #ENET_STATUS_LINK_ON and #ENET_STATUS_100M.
  122. */
  123. uint32_t imx_enet_get_phy_status(imx_enet_priv_t * dev);
  124. /*!
  125. * @brief Initialize ENET interface, including buffer descriptor and MAC
  126. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  127. * @param reg_base base address of ethernet registers
  128. * @param phy_addr phy address, 0 or 1
  129. *
  130. * @return zero
  131. */
  132. int imx_enet_init(imx_enet_priv_t * dev, unsigned long reg_base, int phy_addr);
  133. /*!
  134. * @brief Poll ENET events
  135. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  136. *
  137. * @return event value
  138. */
  139. unsigned long imx_enet_poll(imx_enet_priv_t * dev);
  140. /*!
  141. * @brief Recieve ENET packet
  142. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  143. * @param buf a pointer of buffer for received packet
  144. * @param length the length of received packet
  145. *
  146. * @return 0 if succeeded,
  147. * -1 if failed
  148. *
  149. */
  150. int imx_enet_recv(imx_enet_priv_t * dev, unsigned char *buf, int *length);
  151. /*!
  152. * @brief Transmit ENET packet
  153. * @param dev a pointer of ENET interface(imx_enet_priv_t)
  154. * @param buf a pointer of buffer for packet to be sent
  155. * @param length the length of packet to be sent
  156. * @param key key
  157. *
  158. * @return zero
  159. */
  160. int imx_enet_send(imx_enet_priv_t * dev, unsigned char *buf, int length, unsigned long key);
  161. /*!
  162. * @brief Switch the PHY to external loopback mode for testing.
  163. */
  164. void imx_enet_phy_enable_external_loopback(imx_enet_priv_t * dev);
  165. #if defined(__cplusplus)
  166. }
  167. #endif
  168. #endif //__ENET_H__
  169. ////////////////////////////////////////////////////////////////////////////////
  170. // EOF
  171. ////////////////////////////////////////////////////////////////////////////////