]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/enet/fsl_enet_driver.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / drivers / enet / fsl_enet_driver.h
1 /*
2  * Copyright (c) 2013 - 2014, 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 __FSL_ENET_DRIVER_H__
31 #define __FSL_ENET_DRIVER_H__
32
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include "fsl_enet_hal.h"
36 #include "fsl_os_abstraction.h"
37
38 #ifndef MBED_NO_ENET
39
40 /*!
41  * @addtogroup enet_driver
42  * @{
43  */
44
45 /*******************************************************************************
46  * Definitions
47
48  ******************************************************************************/
49 /*! @brief Defines the approach: ENET interrupt handler do receive */
50 #define ENET_RECEIVE_ALL_INTERRUPT  0
51
52 /*! @brief Defines the statistic enable macro.*/
53 #define ENET_ENABLE_DETAIL_STATS    0
54
55 /*! @brief Defines the alignment operation.*/
56 #define ENET_ALIGN(x,align)        ((unsigned int)((x) + ((align)-1)) & (unsigned int)(~(unsigned int)((align)- 1)))
57
58 #if FSL_FEATURE_ENET_SUPPORT_PTP
59 /*! @brief Defines the PTP IOCTL macro.*/
60 typedef enum _enet_ptp_ioctl
61 {
62     kEnetPtpGetRxTimestamp = 0,    /*!< ENET PTP gets receive timestamp*/
63     kEnetPtpGetTxTimestamp,        /*!< ENET PTP gets transmit timestamp*/
64     kEnetPtpGetCurrentTime,        /*!< ENET PTP gets current time*/
65     kEnetPtpSetCurrentTime,        /*!< ENET PTP sets current time*/
66     kEnetPtpFlushTimestamp,        /*!< ENET PTP flushes timestamp*/
67     kEnetPtpCorrectTime,           /*!< ENET PTP time correction*/
68     kEnetPtpSendEthernetPtpV2,     /*!< ENET PTPv2 sends Ethernet frame*/
69     kEnetPtpReceiveEthernetPtpV2   /*!< ENET PTPv2 receives with Ethernet frame*/
70 } enet_ptp_ioctl_t;
71
72 /*! @brief Defines the PTP message buffer number.*/
73 typedef enum _enet_ptp_buffer_number
74 {
75     kEnetPtpL2bufferNumber = 10,  /*!< PTP layer2 frame buffer number*/
76     kEnetPtpRingNumber = 25       /*!< PTP Ring buffer number*/
77 } enet_ptp_buffer_number_t;
78
79 /*! @brief Defines the ENET PTP message related constant.*/
80 typedef enum _enet_ptp_event_type
81 {
82      kEnetPtpSourcePortIdLen = 10,  /*!< PTP message sequence id length*/
83      kEnetPtpEventMsgType = 3,      /*!< PTP event message type*/
84      kEnetPtpEventPort = 319,       /*!< PTP event port number*/
85      kEnetPtpGnrlPort = 320         /*!< PTP general port number*/
86 } enet_ptp_event_type_t;
87
88 /*! @brief Defines all ENET PTP content offsets in the IPv4 PTP UDP/IP multicast message.*/
89 typedef enum _enet_ipv4_ptp_content_offset
90 {
91     kEnetPtpIpVersionOffset = 0xe,   /*!< IPv4 PTP message IP version offset*/
92     kEnetPtpUdpProtocolOffset = 0x17,/*!< IPv4 PTP message UDP protocol offset*/
93     kEnetPtpUdpPortOffset = 0x24,    /*!< IPv4 PTP message UDP port offset*/
94     kEnetPtpUdpMsgTypeOffset = 0x2a, /*!< IPv4 PTP message UDP message type offset*/
95     kEnetPtpUdpVersionoffset = 0x2b, /*!< IPv4 PTP message UDP version offset*/
96     kEnetPtpUdpClockIdOffset = 0x3e, /*!< IPv4 PTP message UDP clock id offset*/
97     kEnetPtpUdpSequenIdOffset = 0x48,/*!< IPv4 PTP message UDP sequence id offset*/
98     kEnetPtpUdpCtlOffset = 0x4a      /*!< IPv4 PTP message UDP control offset*/
99 } enet_ipv4_ptp_content_offset_t;
100
101 /*! @brief Defines all ENET PTP content offset in THE IPv6 PTP UDP/IP multicast message.*/
102 typedef enum _enet_ipv6_ptp_content_offset
103 {
104     kEnetPtpIpv6UdpProtocolOffset = 0x14,  /*!< IPv6 PTP message UDP protocol offset*/
105     kEnetPtpIpv6UdpPortOffset = 0x38,      /*!< IPv6 PTP message UDP port offset*/
106     kEnetPtpIpv6UdpMsgTypeOffset = 0x3e,   /*!< IPv6 PTP message UDP message type offset*/
107     kEnetPtpIpv6UdpVersionOffset = 0x3f,   /*!< IPv6 PTP message UDP version offset*/
108     kEnetPtpIpv6UdpClockIdOffset = 0x52,   /*!< IPv6 PTP message UDP clock id offset*/
109     kEnetPtpIpv6UdpSequenceIdOffset = 0x5c,/*!< IPv6 PTP message UDP sequence id offset*/
110     kEnetPtpIpv6UdpCtlOffset = 0x5e        /*!< IPv6 PTP message UDP control offset*/
111 } enet_ipv6_ptp_content_offset_t;
112
113 /*! @brief Defines all ENET PTP content offset in the PTP Layer2 Ethernet message.*/
114 typedef enum _enet_ethernet_ptp_content_offset
115 {
116     kEnetPtpEtherPktTypeOffset = 0x0c,   /*!< PTPv2 message Ethernet packet type offset*/
117     kEnetPtpEtherMsgTypeOffset = 0x0e,   /*!< PTPv2 message Ethernet message type offset*/
118     kEnetPtpEtherVersionOffset = 0x0f,   /*!< PTPv2 message Ethernet version type offset*/
119     kEnetPtpEtherClockIdOffset = 0x22,   /*!< PTPv2 message Ethernet clock id offset*/
120     kEnetPtpEtherSequenceIdOffset = 0x2c,/*!< PTPv2 message Ethernet sequence id offset*/
121     kEnetPtpEtherCtlOffset = 0x2e        /*!< PTPv2 message Ethernet control offset*/
122 } enet_ethernet_ptp_content_offset_t;
123
124 /*! @brief Defines the 1588 timer parameters.*/
125 typedef enum _enet_ptp_timer_wrap_period
126 {
127     kEnetPtpAtperVaule = 1000000000, /*!< PTP timer wrap around one second */
128     kEnetBaseIncreaseUnit = 2        /*!< PTP timer adjusts clock and increases value to 2*/
129 } enet_ptp_timer_wrap_period_t;
130 #endif
131
132 /*! @brief Defines the interrupt source index for the interrupt vector change table.*/
133 typedef enum _enet_interrupt_number
134 {
135     kEnetTstimerInt = 0, /*!< Timestamp interrupt*/
136     kEnetTsAvailInt, /*!< TS-avail interrupt*/
137     kEnetWakeUpInt,  /*!< Wakeup interrupt*/
138     kEnetPlrInt,     /*!< Plr interrupt*/
139     kEnetUnInt,      /*!< Un interrupt*/
140     kEnetRlInt,      /*!< RL interrupt*/
141     kEnetLcInt,      /*!< LC interrupt*/
142     kEnetEberrInt,   /*!< Eberr interrupt*/
143     kEnetMiiInt,     /*!< MII interrupt*/
144     kEnetRxbInt ,    /*!< Receive byte interrupt*/
145     kEnetRxfInt,    /*!< Receive frame interrupt*/
146     kEnetTxbInt,    /*!< Transmit byte interrupt*/
147     kEnetTxfInt,    /*!< Transmit frame interrupt*/
148     kEnetGraInt,    /*!< Gra interrupt*/
149     kEnetBabtInt,   /*!< Babt interrupt*/
150     kEnetBabrInt,   /*!< Babr interrupt*/
151     kEnetIntNum     /*!< Interrupt number*/
152 } enet_interrupt_number_t;
153
154 /*! @brief Defines the ENET main constant.*/
155 typedef enum _enet_frame_max
156 {
157     kEnetMaxTimeout = 0x10000,    /*!< Maximum timeout*/
158     kEnetMaxFrameSize = 1518,     /*!< Maximum frame size*/
159     kEnetMaxFrameVlanSize = 1522, /*!< Maximum VLAN frame size*/
160     kEnetMaxFrameDateSize = 1500, /*!< Maximum frame data size*/
161     kEnetMaxFrameBdNumbers = 7,   /*!< Maximum buffer descriptor numbers of a frame*/
162     kEnetFrameFcsLen = 4,         /*!< FCS length*/
163     kEnetEthernetHeadLen = 14     /*!< Ethernet Frame header length*/
164 } enet_frame_max_t;
165
166 /*! @brief Defines the CRC data for a hash value calculation.*/
167 typedef enum _enet_crc_parameter
168 {
169     kEnetCrcData = 0xFFFFFFFFU,  /*!< CRC-32 maximum data */
170     kEnetCrcOffset = 8,          /*!< CRC-32 offset2*/
171     kEnetCrcMask1 = 0x3F         /*!< CRC-32 mask*/
172 } enet_crc_parameter_t;
173
174 /*! @brief Defines the ENET protocol type and main parameters.*/
175 typedef enum _enet_protocol_type
176 {
177     kEnetProtocolIeee8023 = 0x88F7,  /*!< Packet type Ethernet ieee802.3*/
178     kEnetProtocolIpv4 = 0x0800,      /*!< Packet type IPv4*/
179     kEnetProtocolIpv6 = 0x86dd,      /*!< Packet type IPv6*/
180     kEnetProtocol8021QVlan = 0x8100, /*!< Packet type VLAN*/
181     kEnetPacketUdpVersion = 0x11,    /*!< UDP protocol type*/
182     kEnetPacketIpv4Version = 0x4,    /*!< Packet IP version IPv4*/
183     kEnetPacketIpv6Version = 0x6     /*!< Packet IP version IPv6*/
184 } enet_protocol_type_t;
185
186 /*! @brief Defines the ENET MAC control Configure*/
187 typedef enum _enet_mac_control_flag
188 {
189     kEnetSleepModeEnable = 0x1, /*!< ENET control sleep mode Enable*/
190     kEnetPayloadlenCheckEnable = 0x2, /*!< ENET receive payload length check Enable*/
191     kEnetRxFlowControlEnable = 0x4, /*!< ENET flow control, receiver detects PAUSE frames and stops transmitting data when a PAUSE frame is detected*/
192     kEnetRxCrcFwdEnable = 0x8, /*!< Received frame crc is stripped from the frame*/
193     kEnetRxPauseFwdEnable = 0x10,/*!< Pause frames are forwarded to the user application*/
194     kEnetRxPadRemoveEnable = 0x20, /*!< Padding is removed from received frames*/
195     kEnetRxBcRejectEnable = 0x40, /*!< Broadcast frame reject*/
196     kEnetRxPromiscuousEnable = 0x80, /*!< Promiscuous mode enabled*/
197     kEnetRxMiiLoopback = 0x100, /*!< MAC MII loopback mode*/
198 } enet_mac_control_flag_t;
199
200 /*! @brief Defines the multicast group structure for the ENET device. */
201 typedef struct ENETMulticastGroup
202 {
203     enetMacAddr groupAdddr;        /*!< Multicast group address*/
204     uint32_t hash;                 /*!< Hash value of the multicast group address*/
205     struct ENETMulticastGroup *next; /*!< Pointer of the next group structure*/
206     struct ENETMulticastGroup *prv;  /*!< Pointer of the previous structure*/
207 } enet_multicast_group_t;
208
209 /*! @brief Defines the receive buffer descriptor configure structure.*/
210 typedef struct ENETRxBdConfig
211 {
212     uint8_t *rxBdPtrAlign;      /*!< Aligned receive buffer descriptor pointer */
213     uint8_t *rxBufferAlign;     /*!< Aligned receive data buffer pointer */
214     uint8_t *rxLargeBufferAlign; /*!< Aligned receive large data buffer pointer*/
215     uint8_t rxBdNum;             /*!< Aligned receive buffer descriptor pointer*/
216     uint8_t rxBufferNum;         /*!< Receive buffer number*/
217     uint8_t rxLargeBufferNum;    /*!< Large receive buffer number*/
218     uint32_t rxLargeBufferSizeAlign; /*!< Aligned large receive buffer size*/
219 }enet_rxbd_config_t;
220
221 /*! @brief Defines the transmit buffer descriptor configure structure.*/
222 typedef struct ENETTxBdConfig
223 {
224     uint8_t *txBdPtrAlign;      /*!< Aligned transmit buffer descriptor pointer*/
225     uint8_t *txBufferAlign;     /*!< Aligned transmit buffer descriptor pointer*/
226     uint8_t txBufferNum;         /*!< Transmit buffer number*/
227     uint32_t txBufferSizeAlign;  /*!< Aligned transmit buffer size*/
228 }enet_txbd_config_t;
229
230 /*! @brief Defines the basic configuration structure for the ENET device.*/
231 typedef struct ENETMacConfig
232 {
233     uint16_t rxBufferSize;  /*!< Receive buffer size*/
234     uint16_t rxLargeBufferNumber; /*!< Receive large buffer number; Needed only when the BD size is smaller than the maximum frame length.*/
235     uint16_t rxBdNumber;    /*!< Receive buffer descriptor number*/
236     uint16_t txBdNumber;    /*!< Transmit buffer descriptor number*/
237     enetMacAddr macAddr;    /*!< MAC hardware address*/
238     enet_config_rmii_t rmiiCfgMode;/*!< RMII configure mode*/
239     enet_config_speed_t speed;     /*!< Speed configuration*/
240     enet_config_duplex_t duplex;   /*!< Duplex configuration*/
241     /*!< Mac control configure, it is recommended to use enet_mac_control_flag_t
242        it is special control set for loop mode, sleep mode, crc forward/terminate etc*/
243     uint32_t macCtlConfigure; 
244     bool isTxAccelEnabled;/*!< Switcher to enable transmit accelerator*/
245     bool isRxAccelEnabled;/*!< Switcher to enable receive accelerator*/
246     bool isStoreAndFwEnabled;  /*!< Switcher to enable store and forward*/
247     enet_config_rx_accelerator_t rxAcceler; /*!< Receive accelerator configure*/
248     enet_config_tx_accelerator_t txAcceler; /*!< Transmit accelerator configure*/
249     bool isVlanEnabled;    /*!< Switcher to enable VLAN frame*/
250     bool isPhyAutoDiscover;/*!< Switcher to use PHY auto discover*/
251     uint32_t miiClock;     /*!< MII speed*/
252 #if FSL_FEATURE_ENET_SUPPORT_PTP
253     uint16_t ptpRingBufferNumber; /*!< PTP ring buffer number*/
254     bool isSlaveModeEnabled;      /*!< PTP timer configuration*/
255 #endif
256 } enet_mac_config_t;
257
258 /*! @brief Defines the basic configuration for PHY.*/
259 typedef struct ENETPhyConfig
260 {
261     uint8_t phyAddr;    /*!< PHY address*/
262     bool isLoopEnabled; /*!< Switcher to enable the HY loop mode*/
263 } enet_phy_config_t;
264
265 #if FSL_FEATURE_ENET_SUPPORT_PTP
266 /*! @brief Defines the  ENET Mac PTP timestamp structure.*/
267 typedef struct ENETMacPtpTime
268 {
269     uint64_t second;     /*!< Second*/
270     uint32_t nanosecond; /*!< Nanosecond*/
271 } enet_mac_ptp_time_t;
272
273 /*! @brief Defines the ENET PTP timer drift structure.*/
274 typedef struct ENETPtpDrift
275 {
276     int32_t drift;    /*!< Drift for the PTP timer to adjust*/
277 } enet_ptp_drift_t;
278
279 /*! @brief Defines the ENET MAC PTP time parameter.*/
280 typedef struct ENETPtpMasterTimeData
281 {
282     uint8_t masterPtpInstance;/*!< PTP master timer instance*/
283     uint64_t second;          /*!< PTP master timer second */
284 } enet_ptp_master_time_data_t;
285
286 /*! @brief Defines the structure for the ENET PTP message data and timestamp data.*/
287 typedef struct ENETMacPtpTsData
288 {
289     uint8_t version;              /*!< PTP version*/
290     uint8_t sourcePortId[kEnetPtpSourcePortIdLen];/*!< PTP source port ID*/
291     uint16_t sequenceId;          /*!< PTP sequence ID*/
292     uint8_t messageType;          /*!< PTP message type*/
293     enet_mac_ptp_time_t timeStamp;/*!< PTP timestamp*/
294 } enet_mac_ptp_ts_data_t;
295
296 /*! @brief Defines the ENET PTP ring buffer structure for the PTP message timestamp store.*/
297 typedef struct ENETMacPtpTsRing
298 {
299     uint32_t front; /*!< The first index of the ring*/
300     uint32_t end;   /*!< The end index of the ring*/
301     uint32_t size;  /*!< The size of the ring*/
302     enet_mac_ptp_ts_data_t *ptpTsDataPtr;/*!< PTP message data structure*/
303 } enet_mac_ptp_ts_ring_t;
304
305 /*! @brief Defines the ENET packet for the PTP version2 message using the layer2 Ethernet frame.*/
306 typedef struct ENETPtpL2packet
307 {
308     uint8_t packet[kEnetMaxFrameDateSize]; /*!< Buffer for ptpv2 message*/
309     uint16_t length;                       /*!< PTP message length*/
310 } enet_ptp_l2packet_t;
311
312 /*! @brief Defines the ENET PTPv2 packet queue using the layer2 Ethernet frame.*/
313 typedef struct ENETPtpL2queue
314 {
315     enet_ptp_l2packet_t l2Packet[kEnetPtpL2bufferNumber]; /*!< PTP layer2 packet*/
316     uint16_t writeIdex;          /*!< Queue write index*/
317     uint16_t readIdx;            /*!< Queue read index*/
318 } enet_ptp_l2queue_t;
319
320 /*! @brief Defines the ENET PTP layer2 Ethernet frame structure.*/
321 typedef struct ENETPtpL2Ethernet
322 {
323     uint8_t *ptpMsg;     /*!< PTP message*/
324     uint16_t length;     /*!< Length of the PTP message*/
325     enetMacAddr hwAddr;  /*!< Destination hardware address*/
326 } enet_ptp_l2_ethernet_t;
327
328 /*! @brief Defines the ENET PTP buffer structure for all 1588 data.*/
329 typedef struct ENETPrivatePtpBuffer
330 {
331     enet_mac_ptp_ts_ring_t rxTimeStamp;/*!< Data structure for receive message*/
332     enet_mac_ptp_ts_ring_t txTimeStamp;/*!< Data structure for transmit timestamp*/
333     enet_ptp_l2queue_t *l2QueuePtr;    /*!< Data structure for layer2 Ethernet queue*/
334     uint64_t masterSecond;             /*!< PTP time second when it's master time*/
335 } enet_private_ptp_buffer_t;
336 #endif
337
338 /*! @brief Defines the ENET header structure. */
339 typedef struct ENETEthernetHeader
340 {
341     enetMacAddr destAddr;  /*!< Destination address */
342     enetMacAddr sourceAddr;/*!< Source address*/
343     uint16_t type;         /*!< Protocol type*/
344 } enet_ethernet_header_t;
345
346 /*! @brief Defines the ENET VLAN frame header structure. */
347 typedef struct ENET8021vlanHeader
348 {
349     enetMacAddr destAddr;  /*!< Destination address */
350     enetMacAddr sourceAddr;/*!< Source address*/
351     uint16_t tpidtag;      /*!< ENET 8021tag header tag region*/
352     uint16_t othertag;     /*!< ENET 8021tag header type region*/
353     uint16_t type;         /*!< Protocol type*/
354 } enet_8021vlan_header_t;
355
356 /*! @brief Defines the ENET MAC context structure for the buffer address, buffer descriptor address, etc.*/
357 typedef struct ENETMacContext
358 {
359     uint8_t *rxBufferPtr;   /*!< Receive buffer pointer*/
360     uint8_t *rxLargeBufferPtr; /*!< Receive large buffer descriptor*/
361     uint8_t *txBufferPtr;   /*!< Transmit buffer pointer*/
362     uint8_t *rxBdBasePtr;   /*!< Receive buffer descriptor base address pointer*/
363     uint8_t *rxBdCurPtr;    /*!< Current receive buffer descriptor pointer*/
364     uint8_t *rxBdDirtyPtr;  /*!< Receive dirty buffer descriptor*/
365     uint8_t *txBdBasePtr;   /*!< Transmit buffer descriptor base address pointer*/
366     uint8_t *txBdCurPtr;    /*!< Current transmit buffer descriptor pointer*/
367     uint8_t *txBdDirtyPtr;  /*!< Last cleaned transmit buffer descriptor pointer*/
368     bool  isTxFull;         /*!< Transmit buffer descriptor full*/
369     bool  isRxFull;         /*!< Receive buffer descriptor full*/
370     uint32_t bufferdescSize;         /*!< ENET buffer descriptor size*/
371     uint16_t rxBufferSizeAligned;      /*!< Receive buffer alignment size*/
372 #if FSL_FEATURE_ENET_SUPPORT_PTP
373     enet_private_ptp_buffer_t privatePtp;/*!< PTP private buffer*/
374 #endif
375 } enet_mac_context_t;
376
377 /*! @brief Defines the ENET packets statistic structure.*/
378 typedef struct ENETMacStats
379 {
380     uint32_t statsRxTotal;   /*!< Total number of receive packets*/
381     uint32_t statsRxMissed;  /*!< Total number of receive packets*/
382     uint32_t statsRxDiscard; /*!< Receive discarded with error */
383     uint32_t statsRxError;   /*!< Receive discarded with error packets*/
384     uint32_t statsTxTotal;   /*!< Total number of transmit packets*/
385     uint32_t statsTxMissed;  /*!< Transmit missed*/
386     uint32_t statsTxDiscard; /*!< Transmit discarded with error */
387     uint32_t statsTxError;   /*!< Transmit error*/
388     uint32_t statsRxAlign;   /*!< Receive non-octet alignment*/
389     uint32_t statsRxFcs;     /*!< Receive CRC error*/
390     uint32_t statsRxTruncate;/*!< Receive truncate*/
391     uint32_t statsRxLengthGreater;  /*!< Receive length greater than RCR[MAX_FL] */
392     uint32_t statsRxCollision;      /*!< Receive collision*/
393     uint32_t statsRxOverRun;        /*!< Receive over run*/
394     uint32_t statsTxOverFlow;       /*!< Transmit overflow*/
395     uint32_t statsTxLateCollision;  /*!< Transmit late collision*/
396     uint32_t statsTxExcessCollision;/*!< Transmit excess collision*/
397     uint32_t statsTxUnderFlow;      /*!< Transmit under flow*/
398     uint32_t statsTxLarge;          /*!< Transmit large packet*/
399     uint32_t statsTxSmall;          /*!< Transmit small packet*/
400 } enet_stats_t;
401
402 /*! @brief Defines the ENET MAC packet buffer structure.*/
403 typedef struct ENETMacPacketBuffer
404 {
405     uint8_t *data;
406     uint16_t length;
407 } enet_mac_packet_buffer_t;
408
409 #if ENET_RECEIVE_ALL_INTERRUPT
410 typedef uint32_t (* enet_netif_callback_t)(void *enetPtr, enet_mac_packet_buffer_t *packetBuffer);
411 #endif
412
413 /*! @brief Defines the ENET device data structure for the ENET.*/
414 typedef struct ENETDevIf
415 {
416     struct ENETDevIf *next; /*!< Next device structure address*/
417     void *netIfPtr;           /*!< Store the connected  upper layer in the structure*/
418 #if ENET_RECEIVE_ALL_INTERRUPT
419     void *enetNetifService;   /*!< Service function*/
420 #endif
421     enet_multicast_group_t *multiGroupPtr; /*!< Multicast group chain*/
422     uint32_t deviceNumber;    /*!< Device number*/
423     bool isInitialized;       /*!< Device initialized*/
424     uint16_t maxFrameSize;  /*!< MAC maximum frame size*/
425     enet_mac_config_t *macCfgPtr;/*!< MAC configuration structure*/
426     enet_phy_config_t *phyCfgPtr;/*!< PHY configuration structure*/
427     const struct ENETMacApi *macApiPtr;   /*!< MAC application interface structure*/
428     void *phyApiPtr;             /*!< PHY application interface structure*/
429     enet_mac_context_t *macContextPtr; /*!< MAC context pointer*/
430 #if ENET_ENABLE_DETAIL_STATS
431     enet_stats_t stats;                /*!< Packets statistic*/
432 #endif
433 #if ENET_RECEIVE_ALL_INTERRUPT
434     enet_netif_callback_t  enetNetifcall;  /*!< Receive callback function to the upper layer*/
435 #else
436     event_object_t enetReceiveSync;     /*!< Receive sync signal*/
437 #endif
438     lock_object_t enetContextSync;     /*!< Sync signal*/
439 } enet_dev_if_t;
440
441 /*! @brief Defines the basic application for the ENET device.*/
442 typedef struct ENETMacApi
443 {
444     uint32_t (* enet_mac_init)(enet_dev_if_t * enetIfPtr, enet_rxbd_config_t *rxbdCfg, enet_txbd_config_t *txbdCfg);/*!< MAC initialize interface*/
445     uint32_t (* enet_mac_deinit)(enet_dev_if_t * enetIfPtr);/*!< MAC close interface*/
446     uint32_t (* enet_mac_send)(enet_dev_if_t * enetIfPtr, uint8_t *packet, uint32_t size);/*!< MAC send packets*/
447 #if !ENET_RECEIVE_ALL_INTERRUPT
448     uint32_t (* enet_mac_receive)(enet_dev_if_t * enetIfPtr, enet_mac_packet_buffer_t *packBuffer);/*!< MAC receive interface*/
449 #endif
450     uint32_t (* enet_mii_read)(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr);/*!< MII reads PHY*/
451     uint32_t (* enet_mii_write)(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t data);/*!< MII writes PHY*/
452     uint32_t (* enet_add_multicast_group)(uint32_t instance, enet_multicast_group_t *multiGroupPtr, uint8_t *groupAddr);/*!< Add multicast group*/
453     uint32_t (* enet_leave_multicast_group)(uint32_t instance, enet_multicast_group_t *multiGroupPtr, uint8_t *groupAddr);/*!< Leave multicast group*/
454 } enet_mac_api_t;
455
456 /*******************************************************************
457 * Global variables
458  
459 ***********************************************************************/
460 extern const enet_mac_api_t g_enetMacApi;
461
462 /*******************************************************************************
463  * API 
464  ******************************************************************************/
465
466 #if defined(__cplusplus)
467 extern "C" {
468 #endif
469
470 /*! 
471   * @name ENET Driver
472   * @{
473   */
474
475
476 #if FSL_FEATURE_ENET_SUPPORT_PTP
477 /*!
478  * @brief Initializes the ENET PTP context structure with the basic configuration.
479  *
480  * @param macContextPtr The pointer to the ENET MAC macContext structure.
481  * @return The execution status.
482  */
483 uint32_t enet_ptp_init(enet_private_ptp_buffer_t *privatePtpPtr, uint32_t ptpRxBufferNum, enet_mac_ptp_ts_data_t *ptpTsRxDataPtr, uint32_t ptpTxBufferNum, enet_mac_ptp_ts_data_t *ptpTsTxDataPtr);
484   
485 /*!
486  * @brief Initializes the ENET PTP timer with the basic configuration.
487  *
488  * After the PTP starts, the 1588 timer also starts running. If the user wants the 1588 timer
489  * as the slave, enable the isSlaveEnabled flag.
490  *
491  * @param instance The ENET instance number.
492  * @param ptpCfgPtr The pointer to the basic PTP timer configuration structure.
493  * @return The execution status.
494  */
495 uint32_t enet_ptp_start(uint32_t instance, bool isSlaveEnabled);
496
497 /*!
498  * @brief Parses the ENET packet. 
499  *
500  * Parses the ENET message and checks if it is a PTP message. If it is a PTP message,
501  * the message is stored in the PTP information structure. Message parsing 
502  * decides whether timestamp processing is done after that.
503  *
504  * @param packet The ENET packet.
505  * @param ptpTsPtr The pointer to the PTP data structure.
506  * @param isPtpMsg The PTP message flag.
507  * @param isFastEnabled The fast operation flag. If set, only check if it is a ptp message
508  *        and doesn't store any ptp message.
509  * @return The execution status.
510  */
511 uint32_t enet_ptp_parse(uint8_t *packet, enet_mac_ptp_ts_data_t *ptpTsPtr, bool *isPtpMsg, bool isFastEnabled);
512
513 /*!
514  * @brief Gets the current value of the ENET PTP time.
515  *
516  * @param ptpTimerPtr The PTP timer structure.
517  * @return The execution status.
518  */
519 uint32_t enet_ptp_get_time(enet_mac_ptp_time_t *ptpTimerPtr);
520
521 /*!
522  * @brief Sets the current value of the ENET PTP time.
523  *
524  * @param ptpTimerPtr The PTP timer structure.
525  * @return The execution status.
526  */
527 uint32_t enet_ptp_set_time(enet_mac_ptp_time_t *ptpTimerPtr);
528
529 /*!
530  * @brief Adjusts the ENET PTP time.
531  *
532  * @param instance The ENET instance number.
533  * @param drift The PTP timer drift value.
534  * @return The execution status.
535  */
536 uint32_t enet_ptp_correction_time(uint32_t instance, int32_t drift);
537
538
539 /*!
540  * @brief Stores the transmit timestamp.
541  *
542  * @param ptpBuffer The PTP buffer pointer.
543  * @param bdPtr The current transmit buffer descriptor.
544  * @return The execution status.
545  */     
546 uint32_t enet_ptp_store_tx_timestamp(enet_private_ptp_buffer_t *ptpBuffer,void *bdPtr);
547
548 /*!
549  * @brief Stores receive timestamp.
550  *
551  * @param ptpBuffer The PTP buffer pointer.
552  * @param packet The current receive packet.
553  * @param bdPtr The current receive buffer descriptor.
554  * @return The execution status.
555  */
556 uint32_t enet_ptp_store_rx_timestamp(enet_private_ptp_buffer_t *ptpBuffer, uint8_t *packet, void *bdPtr);
557
558 /*!
559  * @brief Initializes the buffer queue for the PTP layer2 Ethernet packets.
560  *
561  * @param ptpBuffer The PTP buffer pointer.
562  * @return The execution status.
563  */
564 uint32_t enet_ptp_l2queue_init(enet_private_ptp_buffer_t *ptpBuffer, enet_ptp_l2queue_t *ptpL2QuePtr);
565
566 /*!
567  * @brief Adds the PTP layer2 Ethernet packet to the PTP Ethernet packet queue.
568  *
569  * @param ptpQuePtr The ENET private ptp layer2 buffer queue structure pointer.
570  * @param packet The packet buffer pointer.
571  * @param length The packet length. 
572  * @return The execution status.
573  */
574 uint32_t enet_ptp_service_l2packet(enet_ptp_l2queue_t * ptpQuePtr, uint8_t *packet, uint16_t length);
575
576 /*!
577  * @brief Sends the PTP layer2 Ethernet packet to the Net.
578  *
579  * @param enetIfPtr The ENET context structure.
580  * @param paramPtr The buffer from upper layer. 
581  * @return The execution status.
582  */
583 uint32_t enet_ptp_send_l2packet(enet_dev_if_t * enetIfPtr, void *paramPtr);
584
585 /*!
586  * @brief Receives the PTP layer2 Ethernet packet from the Net.
587  *
588  * @param enetIfPtr The ENET context structure.
589  * @param paramPtr The buffer receive from net and will send to upper layer. 
590  * @return The execution status.
591  */
592 uint32_t enet_ptp_receive_l2packet(enet_dev_if_t * enetIfPtr,void *paramPtr);
593
594 /*!
595  * @brief Provides the handler for the 1588 stack to do PTP IOCTL.
596  *
597  * @param enetIfPtr The ENET context structure.
598  * @param commandId The command id.
599  * @param inOutPtr The data buffer. 
600  * @return The execution status.
601  */
602 uint32_t enet_ptp_ioctl(enet_dev_if_t * enetIfPtr, uint32_t commandId, void *inOutPtr);
603
604 /*!
605  * @brief Stops the ENET PTP timer.
606  *
607  * @param instance The ENET instance number.
608  * @return The execution status.
609  */
610 uint32_t enet_ptp_stop(uint32_t instance);
611
612 /*!
613  * @brief Checks whether the PTP ring buffer is full.
614  *
615  * @param ptpTsRingPtr The ENET PTP timestamp ring.
616  * @return True if the PTP ring buffer is full. Otherwise, false.
617  */
618 bool enet_ptp_ring_is_full(enet_mac_ptp_ts_ring_t *ptpTsRingPtr);
619
620 /*!
621  * @brief Updates the latest ring buffers.
622  *
623  * Adds the PTP message data to the PTP ring buffers and increases the 
624  * PTP ring buffer index.
625  *
626  * @param ptpTsRingPtr The ENET PTP timestamp ring.
627  * @param data The PTP data buffer.
628  * @return The execution status.
629  */
630 uint32_t enet_ptp_ring_update(enet_mac_ptp_ts_ring_t *ptpTsRingPtr, enet_mac_ptp_ts_data_t *data);
631
632 /*!
633  * @brief Searches the element in ring buffers with the message ID and Clock ID.
634  *
635  * @param ptpTsRingPtr The ENET PTP timestamp ring.
636  * @param data The PTP data buffer.
637  * @return The execution status.
638  */
639 uint32_t enet_ptp_ring_search(enet_mac_ptp_ts_ring_t *ptpTsRingPtr, enet_mac_ptp_ts_data_t *data);
640
641 /*!
642  * @brief Calculates the ENET PTP ring buffer index.
643  *
644  * @param size The ring size.
645  * @param curIdx The current ring index.
646  * @param offset The offset index.
647  * @return The execution status.
648  */
649 static inline uint32_t enet_ptp_ring_index(uint32_t size, uint32_t curIdx, uint32_t offset)
650 {
651     return ((curIdx + offset) % size);
652 }
653
654 /*!
655  * @brief Frees all ring buffers.
656  *
657  * @param enetContextPtr The ENET MAC context buffer.
658  * @return The execution status.
659  */
660 uint32_t enet_ptp_deinit(enet_mac_context_t *enetContextPtr);
661
662 /*!
663  * @brief The ENET PTP time interrupt handler.
664  *
665  * @param enetIfPtr The ENET context structure pointer.
666  */
667 void enet_mac_ts_isr(void *enetIfPtr);
668 #endif
669 /*!
670  * @brief(R)MII Read function.
671  *
672  * @param instance The ENET instance number.
673  * @param phyAddr The PHY address.
674  * @param phyReg The PHY register.
675  * @param dataPtr The data read from MII.
676  * @return The execution status.
677  */
678 uint32_t enet_mii_read(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr);
679
680 /*!
681  * @brief(R)MII Read function.
682  *
683  * @param instance The ENET instance number.
684  * @param phyAddr The PHY address.
685  * @param phyReg The PHY register.
686  * @param data The data write to MII.
687  * @return The execution status.
688  */
689 uint32_t enet_mii_write(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t data);
690
691 /*!
692  * @brief Initializes  ENET buffer descriptors.
693  *
694  * @param enetIfPtr The ENET context structure.
695  * @return The execution status.
696  */
697 uint32_t enet_mac_bd_init(enet_dev_if_t * enetIfPtr);
698
699 /*!
700  * @brief Initializes the ENET MAC MII(MDC/MDIO) interface.
701  *
702  * @param enetIfPtr The ENET context structure.
703  * @return The execution status.
704  */
705 uint32_t enet_mac_mii_init(enet_dev_if_t * enetIfPtr);
706
707 /*!
708  * @brief Initialize the ENET receive buffer descriptors.
709  *
710  * If you open ENET_RECEIVE_ALL_INTERRUPT to do receive 
711  * data buffer numbers can be the same as the receive descriptor numbers. 
712  * But if you close ENET_RECEIVE_ALL_INTERRUPT and choose polling receive 
713  * frames please make sure the receive data buffers are more than 
714  * buffer descriptor numbers to guarantee a good performance.
715  *
716  * @param enetIfPtr The ENET context structure.
717  * @param rxbdCfg The receive buffer descriptor configuration. 
718  * @return The execution status.
719  */
720 uint32_t enet_mac_rxbd_init(enet_dev_if_t * enetIfPtr, enet_rxbd_config_t *rxbdCfg);
721
722 /*!
723  * @brief Deinitialize the ENET receive buffer descriptors.
724  *
725  * Deinitialize the ENET receive buffer descriptors. 
726  *
727  * @param enetIfPtr The ENET context structure.
728  * @return The execution status.
729  */ 
730
731 uint32_t enet_mac_rxbd_deinit(enet_dev_if_t * enetIfPtr);
732
733 /*!
734  * @brief Initialize the ENET transmit buffer descriptors.
735  *
736  * @param enetIfPtr The ENET context structure.
737  * @param txbdCfg The transmit buffer descriptor configuration. 
738  * @return The execution status.
739  */
740 uint32_t enet_mac_txbd_init(enet_dev_if_t * enetIfPtr, enet_txbd_config_t *txbdCfg);
741
742 /*!
743  * @brief Deinitialize the ENET transmit buffer descriptors.
744  *
745  * Deinitialize the ENET transmit buffer descriptors. 
746  *
747  * @param enetIfPtr The ENET context structure.
748  * @return The execution status.
749  */ 
750 uint32_t enet_mac_txbd_deinit(enet_dev_if_t * enetIfPtr);
751
752 /*!
753  * @brief Initializes ENET MAC FIFO and accelerator with the basic configuration.
754  *
755  * @param enetIfPtr The ENET context structure.
756  * @return The execution status.
757  */
758 uint32_t enet_mac_configure_fifo_accel(enet_dev_if_t * enetIfPtr);
759
760 /*!
761  * @brief the ENET controller with the basic configuration.
762  *
763  * @param enetIfPtr The ENET context structure.
764  * @return The execution status.
765  */
766 uint32_t enet_mac_configure_controller(enet_dev_if_t * enetIfPtr);
767
768 /*!
769  * @brief Deinit the ENET device.
770  *
771  * @param enetIfPtr The ENET context structure.
772  * @return The execution status.
773  */
774 uint32_t enet_mac_deinit(enet_dev_if_t * enetIfPtr);
775
776 #if !ENET_RECEIVE_ALL_INTERRUPT
777 /*!
778  * @brief Updates the receive buffer descriptor.
779  *
780  * This updates the used receive buffer descriptor ring to
781  * ensure that the used BDS is correctly used again. It  cleans 
782  * the status region and sets the control region of the used receive buffer 
783  * descriptor. If the isBufferUpdate flag is set, the data buffer in the
784  * buffer descriptor is updated.
785  *
786  * @param enetIfPtr The ENET context structure.
787  * @param isBufferUpdate The data buffer update flag.
788  * @return The execution status.
789  */
790 uint32_t enet_mac_update_rxbd(enet_dev_if_t * enetIfPtr, bool isBufferUpdate);
791 #else
792 /*!
793  * @brief Updates the receive buffer descriptor.
794  *
795  * Clears the status region and sets the control region of the current receive buffer 
796  * descriptor to ensure that it is  used correctly again. It  increases the buffer 
797  * descriptor index to the next buffer descriptor.
798  *
799  * @param enetIfPtr The ENET context structure.
800  * @return The execution status.
801  */
802 uint32_t enet_mac_update_rxbd(enet_dev_if_t * enetIfPtr);
803 #endif
804 /*!
805  * @brief Processes the ENET receive frame error statistics.
806  *
807  * This interface gets the error statistics of the received frame.
808  * Because the error information is in the last BD of a frame, this interface
809  * should be called when processing the last BD of a frame.
810  *
811  * @param enetIfPtr The ENET context structure.
812  * @param data The current control and status data of the buffer descriptor.
813  * @return The frame error status.
814  *         - True if the frame has an error. 
815  *         - False if the frame does not have an error.
816  */
817 bool enet_mac_rx_error_stats(enet_dev_if_t * enetIfPtr, uint32_t data);
818
819 /*!
820  * @brief Processes the ENET transmit frame statistics.
821  *
822  * This interface gets the error statistics of the transmit frame.
823  * Because the error information is in the last BD of a frame, this interface
824  * should be called when processing the last BD of a frame.
825  *
826  * @param enetIfPtr The ENET context structure.
827  * @param curBd The current buffer descriptor.
828  */
829 void enet_mac_tx_error_stats(enet_dev_if_t * enetIfPtr,void *curBd);
830
831 /*!
832  * @brief ENET transmit buffer descriptor cleanup.
833  *
834  * First, store the transmit frame error statistic and PTP timestamp of the transmitted packets. 
835  * Second, clean up the used transmit buffer descriptors.
836  * If the PTP 1588 feature is open, this interface  captures the 1588 timestamp. 
837  * It is called by the transmit interrupt handler.
838  *
839  * @param enetIfPtr The ENET context structure.
840  * @return The execution status.
841  */
842 uint32_t enet_mac_tx_cleanup(enet_dev_if_t * enetIfPtr);
843 #if !ENET_RECEIVE_ALL_INTERRUPT
844 /*!
845  * @brief Receives ENET packets.
846  *
847  * @param enetIfPtr The ENET context structure.
848  * @param packBuffer The received data buffer.
849  * @return The execution status.
850  */
851 uint32_t enet_mac_receive(enet_dev_if_t * enetIfPtr, enet_mac_packet_buffer_t *packBuffer);
852 #else
853 /*!
854  * @brief Receives ENET packets.
855  *
856  * @param enetIfPtr The ENET context structure.
857  * @return The execution status.
858  */
859 uint32_t enet_mac_receive(enet_dev_if_t * enetIfPtr);
860 #endif
861 /*!
862  * @brief Transmits ENET packets.
863  *
864  * @param enetIfPtr The ENET context structure.
865  * @param packet The frame to be transmitted.
866  * @param size The frame size.
867  * @return The execution status.
868  */
869 uint32_t enet_mac_send(enet_dev_if_t * enetIfPtr, uint8_t *packet, uint32_t size);
870
871 /*!
872  * @brief The ENET receive interrupt handler.
873  *
874  * @param enetIfPtr The ENET context structure pointer.
875  */
876 void enet_mac_rx_isr(void *enetIfPtr);
877
878 /*!
879  * @brief The ENET transmit interrupt handler.
880  *
881  * @param enetIfPtr The ENET context structure pointer.
882  */
883 void enet_mac_tx_isr(void *enetIfPtr);
884
885 /*!
886  * @brief Calculates the CRC hash value.
887  *
888  * @param address The ENET MAC hardware address.
889  * @param crcVlaue The calculated CRC value of the Mac address.
890  */
891 void enet_mac_calculate_crc32(enetMacAddr address, uint32_t *crcValue);
892
893 /*!
894  * @brief Adds the ENET device to a multicast group.
895  *
896  * @param instance The ENET instance number.
897  * @param multiGroupPtr The ENET multicast group structure.
898  * @param address The ENET MAC hardware address.
899  * @return The execution status.
900  */
901 uint32_t enet_mac_add_multicast_group(uint32_t instance, enet_multicast_group_t *multiGroupPtr, enetMacAddr address);
902
903 /*!
904  * @brief Moves the ENET device from a multicast group.
905  *
906  * @param instance The ENET instance number.
907  * @param multiGroupPtr The ENET multicast group structure.
908  * @param address The ENET MAC hardware address.
909  * @return The execution status.
910  */
911 uint32_t enet_mac_leave_multicast_group(uint32_t instance, enet_multicast_group_t *multiGroupPtr, enetMacAddr address);
912
913 /*!
914  * @brief Initializes the ENET with the basic configuration.
915  *
916  * @param enetIfPtr The pointer to the basic configuration structure.
917  * @return The execution status.
918  */
919 uint32_t enet_mac_init(enet_dev_if_t * enetIfPtr, enet_rxbd_config_t *rxbdCfg,
920                             enet_txbd_config_t *txbdCfg);
921
922 /*!
923  * @brief Enqueues a data buffer to the buffer queue.
924  *
925  * @param queue The buffer queue.
926  * @param buffer The buffer to add to the buffer queue.
927  */
928 void enet_mac_enqueue_buffer( void **queue, void *buffer);
929
930 /*!
931  * @brief Dequeues a buffer from the buffer queue.
932  *
933  * @param queue The buffer queue.
934  * @return The dequeued data buffer.
935  */
936 void *enet_mac_dequeue_buffer( void **queue);
937
938 /* @} */
939
940 #if defined(__cplusplus)
941 }
942 #endif
943
944 #endif
945
946 /*! @}*/
947
948 #endif /* __FSL_ENET_DRIVER_H__ */
949 /*******************************************************************************
950  * EOF
951  ******************************************************************************/
952