fec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 __FEC_H__
  31. #define __FEC_H__
  32. #include "registers/regsfec.h"
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Definitions
  35. ////////////////////////////////////////////////////////////////////////////////
  36. //! @brief Definitions of the event bits.
  37. enum {
  38. FEC_EVENT_HBERR = 0x80000000,
  39. FEC_EVENT_BABR = BM_FEC_EIR_BABR,
  40. FEC_EVENT_BABT = BM_FEC_EIR_BABT,
  41. FEC_EVENT_GRA = BM_FEC_EIR_GRA,
  42. FEC_EVENT_TXF = BM_FEC_EIR_TXF,
  43. FEC_EVENT_TXB = BM_FEC_EIR_TXB,
  44. FEC_EVENT_RXF = BM_FEC_EIR_RXF,
  45. FEC_EVENT_RXB = BM_FEC_EIR_RXB,
  46. FEC_EVENT_MII = BM_FEC_EIR_MII,
  47. FEC_EVENT_EBERR = BM_FEC_EIR_EBERR,
  48. FEC_EVENT_LC = BM_FEC_EIR_LC,
  49. FEC_EVENT_RL = BM_FEC_EIR_RL,
  50. FEC_EVENT_UN = BM_FEC_EIR_UN,
  51. FEC_EVENT_TX = FEC_EVENT_TXF,
  52. FEC_EVENT_TX_ERR = (FEC_EVENT_BABT | FEC_EVENT_LC | FEC_EVENT_RL | FEC_EVENT_UN),
  53. FEC_EVENT_RX = FEC_EVENT_RXF,
  54. FEC_EVENT_ERR = (FEC_EVENT_HBERR | FEC_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_fec_bd imx_fec_bd_t;
  64. //! @brief Data structure for FEC device
  65. typedef struct imx_fec_priv_s {
  66. hw_fec_t *fec_reg; //!< the reister base address of FEC
  67. unsigned char phy_addr; //!< the address of PHY which associated with FEC controller
  68. uint32_t phy_id; //!< ID of the PHY
  69. unsigned char tx_busy; //!< 0:free, 1:transmitting frame
  70. unsigned char res[2];
  71. unsigned long status; //!< the status of FEC device:link-status etc.
  72. unsigned long tx_key; //!< save the key delivered from send function
  73. imx_fec_bd_t *rx_bd; //!< the receive buffer description ring
  74. imx_fec_bd_t *rx_cur; //!< the next recveive buffer description
  75. imx_fec_bd_t *tx_bd; //!< the transmit buffer description rign
  76. imx_fec_bd_t *tx_cur; //!< the next transmit buffer description
  77. // TODO: Add interrupt about fields
  78. // TODO: Add timer about fields
  79. } imx_fec_priv_t;
  80. //! @brief Definitions of the status field of imx_fec_priv_t.
  81. enum {
  82. FEC_STATUS_LINK_ON = 0x80000000,
  83. FEC_STATUS_FULL_DPLX = 0x40000000,
  84. FEC_STATUS_AUTO_NEG = 0x20000000,
  85. FEC_STATUS_10M = 0x8000000,
  86. FEC_STATUS_100M = 0x10000000,
  87. FEC_STATUS_1000M = 0x20000000
  88. };
  89. ////////////////////////////////////////////////////////////////////////////////
  90. // API
  91. ////////////////////////////////////////////////////////////////////////////////
  92. #if defined(__cplusplus)
  93. extern "C" {
  94. #endif
  95. /*!
  96. * @brief Enable FEC and start transfer.
  97. * @param dev a pointer of FEC interface(imx_fec_priv_t)
  98. * @param enaddr a pointer of MAC address
  99. *
  100. * @return none
  101. */
  102. void imx_fec_start(imx_fec_priv_t * dev, unsigned char *enaddr);
  103. /*!
  104. * @brief Disable FEC
  105. * @param dev a pointer of FEC interface(imx_fec_priv_t)
  106. *
  107. * @return none
  108. */
  109. void imx_fec_stop(imx_fec_priv_t * dev);
  110. /*!
  111. * @brief Initialize FEC PHY, like LAN8700, 8720, AR8031, etc
  112. * @param dev a pointer of FEC interface(imx_fec_priv_t)
  113. *
  114. * @return none
  115. */
  116. void imx_fec_phy_init(imx_fec_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 FEC_STATUS_x enums,
  121. * such as #FEC_STATUS_LINK_ON and #FEC_STATUS_100M.
  122. */
  123. uint32_t imx_fec_get_phy_status(imx_fec_priv_t * dev);
  124. /*!
  125. * @brief Initialize FEC interface, including buffer descriptor and MAC
  126. * @param dev a pointer of FEC interface(imx_fec_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_fec_init(imx_fec_priv_t * dev, unsigned long reg_base, int phy_addr);
  133. /*!
  134. * @brief Poll FEC events
  135. * @param dev a pointer of FEC interface(imx_fec_priv_t)
  136. *
  137. * @return event value
  138. */
  139. unsigned long imx_fec_poll(imx_fec_priv_t * dev);
  140. /*!
  141. * @brief Recieve FEC packet
  142. * @param dev a pointer of FEC interface(imx_fec_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_fec_recv(imx_fec_priv_t * dev, unsigned char *buf, int *length);
  151. /*!
  152. * @brief Transmit FEC packet
  153. * @param dev a pointer of FEC interface(imx_fec_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_fec_send(imx_fec_priv_t * dev, unsigned char *buf, int length, unsigned long key);
  161. #if defined(__cplusplus)
  162. }
  163. #endif
  164. #endif //__FEC_H__
  165. ////////////////////////////////////////////////////////////////////////////////
  166. // EOF
  167. ////////////////////////////////////////////////////////////////////////////////