]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/ATSAM4E/instance/gmac.h
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2822 1d2547de-c912-0410...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / ATSAM4E / instance / gmac.h
1  /**\r
2  * \file\r
3  *\r
4  * \brief GMAC (Ethernet MAC) driver for SAM.\r
5  *\r
6  * Copyright (c) 2013 Atmel Corporation. All rights reserved.\r
7  *\r
8  * \asf_license_start\r
9  *\r
10  * \page License\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions are met:\r
14  *\r
15  * 1. Redistributions of source code must retain the above copyright notice,\r
16  *    this list of conditions and the following disclaimer.\r
17  *\r
18  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
19  *    this list of conditions and the following disclaimer in the documentation\r
20  *    and/or other materials provided with the distribution.\r
21  *\r
22  * 3. The name of Atmel may not be used to endorse or promote products derived\r
23  *    from this software without specific prior written permission.\r
24  *\r
25  * 4. This software may only be redistributed and used in connection with an\r
26  *    Atmel microcontroller product.\r
27  *\r
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
38  * POSSIBILITY OF SUCH DAMAGE.\r
39  *\r
40  * \asf_license_stop\r
41  *\r
42  */\r
43 \r
44 #ifndef GMAC_H_INCLUDED\r
45 #define GMAC_H_INCLUDED\r
46 \r
47 #include "compiler.h"\r
48 #include "component/gmac.h"\r
49 \r
50 /// @cond 0\r
51 /**INDENT-OFF**/\r
52 #ifdef __cplusplus\r
53 extern "C" {\r
54 #endif\r
55 /**INDENT-ON**/\r
56 /// @endcond\r
57 \r
58 /** The buffer addresses written into the descriptors must be aligned, so the\r
59     last few bits are zero.  These bits have special meaning for the GMAC\r
60     peripheral and cannot be used as part of the address. */\r
61 #define GMAC_RXD_ADDR_MASK      0xFFFFFFFC\r
62 #define GMAC_RXD_WRAP         (1ul << 1)  /**< Wrap bit */\r
63 #define GMAC_RXD_OWNERSHIP    (1ul << 0)  /**< Ownership bit */\r
64 \r
65 #define GMAC_RXD_BROADCAST    (1ul << 31) /**< Broadcast detected */\r
66 #define GMAC_RXD_MULTIHASH    (1ul << 30) /**< Multicast hash match */\r
67 #define GMAC_RXD_UNIHASH      (1ul << 29) /**< Unicast hash match */\r
68 #define GMAC_RXD_ADDR_FOUND      (1ul << 27) /**< Specific address match found */\r
69 #define GMAC_RXD_ADDR        (3ul << 25) /**< Address match */\r
70 #define GMAC_RXD_RXCOEN        (1ul << 24) /**< RXCOEN related function */\r
71 #define GMAC_RXD_TYPE         (3ul << 22) /**< Type ID match */\r
72 #define GMAC_RXD_VLAN         (1ul << 21) /**< VLAN tag detected */\r
73 #define GMAC_RXD_PRIORITY     (1ul << 20) /**< Priority tag detected */\r
74 #define GMAC_RXD_PRIORITY_MASK  (3ul << 17) /**< VLAN priority */\r
75 #define GMAC_RXD_CFI          (1ul << 16) /**< Concatenation Format Indicator only if bit 21 is set */\r
76 #define GMAC_RXD_EOF          (1ul << 15) /**< End of frame */\r
77 #define GMAC_RXD_SOF          (1ul << 14) /**< Start of frame */\r
78 #define GMAC_RXD_FCS          (1ul << 13) /**< Frame check sequence */\r
79 #define GMAC_RXD_OFFSET_MASK                /**< Receive buffer offset */\r
80 #define GMAC_RXD_LEN_MASK       (0xFFF)     /**< Length of frame including FCS (if selected) */\r
81 #define GMAC_RXD_LENJUMBO_MASK  (0x3FFF)    /**< Jumbo frame length */\r
82 \r
83 #define GMAC_TXD_USED         (1ul << 31) /**< Frame is transmitted */\r
84 #define GMAC_TXD_WRAP         (1ul << 30) /**< Last descriptor */\r
85 #define GMAC_TXD_ERROR        (1ul << 29) /**< Retry limit exceeded, error */\r
86 #define GMAC_TXD_UNDERRUN     (1ul << 28) /**< Transmit underrun */\r
87 #define GMAC_TXD_EXHAUSTED    (1ul << 27) /**< Buffer exhausted */\r
88 #define GMAC_TXD_LATE    (1ul << 26) /**< Late collision,transmit  error  */\r
89 #define GMAC_TXD_CHECKSUM_ERROR   (7ul << 20) /**< Checksum error */\r
90 #define GMAC_TXD_NOCRC        (1ul << 16) /**< No CRC */\r
91 #define GMAC_TXD_LAST         (1ul << 15) /**< Last buffer in frame */\r
92 #define GMAC_TXD_LEN_MASK       (0x1FFF)     /**< Length of buffer */\r
93 \r
94 /** The MAC can support frame lengths up to 1536 bytes */\r
95 #define GMAC_FRAME_LENTGH_MAX       1536\r
96 \r
97 #define GMAC_RX_UNITSIZE            128     /**< Fixed size for RX buffer  */\r
98 #define GMAC_TX_UNITSIZE            1518    /**< Size for ETH frame length */\r
99 \r
100 /** GMAC clock speed */\r
101 #define GMAC_MCK_SPEED_240MHZ        (240*1000*1000)\r
102 #define GMAC_MCK_SPEED_160MHZ        (160*1000*1000)\r
103 #define GMAC_MCK_SPEED_120MHZ        (120*1000*1000)\r
104 #define GMAC_MCK_SPEED_80MHZ          (80*1000*1000)\r
105 #define GMAC_MCK_SPEED_40MHZ          (40*1000*1000)\r
106 #define GMAC_MCK_SPEED_20MHZ          (20*1000*1000)\r
107 \r
108 /** GMAC maintain code default value*/\r
109 #define GMAC_MAN_CODE_VALUE    (10)\r
110 \r
111 /** GMAC maintain start of frame default value*/\r
112 #define GMAC_MAN_SOF_VALUE     (1)\r
113 \r
114 /** GMAC maintain read/write*/\r
115 #define GMAC_MAN_RW_TYPE       (2)\r
116 \r
117 /** GMAC maintain read only*/\r
118 #define GMAC_MAN_READ_ONLY     (1)\r
119 \r
120 /** GMAC address length */\r
121 #define GMAC_ADDR_LENGTH       (6)\r
122 \r
123 \r
124 #define GMAC_DUPLEX_HALF 0\r
125 #define GMAC_DUPLEX_FULL 1\r
126 \r
127 #define GMAC_SPEED_10M      0\r
128 #define GMAC_SPEED_100M     1\r
129 \r
130 /**\r
131  * \brief Return codes for GMAC APIs.\r
132  */\r
133 typedef enum {\r
134         GMAC_OK = 0,         /** 0  Operation OK */\r
135         GMAC_TIMEOUT = 1,    /** 1  GMAC operation timeout */\r
136         GMAC_TX_BUSY,        /** 2  TX in progress */\r
137         GMAC_RX_NULL,        /** 3  No data received */\r
138         GMAC_SIZE_TOO_SMALL, /** 4  Buffer size not enough */\r
139         GMAC_PARAM,          /** 5  Parameter error, TX packet invalid or RX size too small */\r
140         GMAC_INVALID = 0xFF, /* Invalid */\r
141 } gmac_status_t;\r
142 \r
143 /**\r
144  * \brief Media Independent Interface (MII) type.\r
145  */\r
146 typedef enum {\r
147         GMAC_PHY_MII = 0,         /** MII mode */\r
148         GMAC_PHY_RMII = 1,    /** Reduced MII mode */\r
149         GMAC_PHY_INVALID = 0xFF, /* Invalid mode*/\r
150 } gmac_mii_mode_t;\r
151 \r
152 /** Receive buffer descriptor struct */\r
153 COMPILER_PACK_SET(8)\r
154 typedef struct gmac_rx_descriptor {\r
155         union gmac_rx_addr {\r
156                 uint32_t val;\r
157                 struct gmac_rx_addr_bm {\r
158                         uint32_t b_ownership:1, /**< User clear, GMAC sets this to 1 once it has successfully written a frame to memory */\r
159                         b_wrap:1,   /**< Marks last descriptor in receive buffer */\r
160                         addr_dw:30; /**< Address in number of DW */\r
161                 } bm;\r
162         } addr; /**< Address, Wrap & Ownership */\r
163         union gmac_rx_status {\r
164                 uint32_t val;\r
165                 struct gmac_rx_status_bm {\r
166                         uint32_t len:13,       /**  0..12  Length of frame including FCS */\r
167                         b_fcs:1,               /**  13     Receive buffer offset,  bits 13:12 of frame length for jumbo frame */\r
168                         b_sof:1,               /**  14     Start of frame */\r
169                         b_eof:1,               /**  15     End of frame */\r
170                         b_cfi:1,               /**  16     Concatenation Format Indicator */\r
171                         vlan_priority:3,       /**  17..19 VLAN priority (if VLAN detected) */\r
172                         b_priority_detected:1, /**  20     Priority tag detected */\r
173                         b_vlan_detected:1,     /**  21     VLAN tag detected */\r
174                         b_type_id_match:2,     /**  22..23 Type ID match */\r
175                         b_checksumoffload:1,   /**  24     Checksum offload specific function */\r
176                         b_addrmatch:2,         /**  25..26 Address register match */\r
177                         b_ext_addr_match:1,    /**  27     External address match found */\r
178                         reserved:1,            /**  28     */\r
179                         b_uni_hash_match:1,    /**  29     Unicast hash match */\r
180                         b_multi_hash_match:1,  /**  30     Multicast hash match */\r
181                         b_boardcast_detect:1;  /**  31     Global broadcast address detected */\r
182                 } bm;\r
183         } status;\r
184 } gmac_rx_descriptor_t;\r
185 \r
186 /** Transmit buffer descriptor struct */\r
187 COMPILER_PACK_SET(8)\r
188 typedef struct gmac_tx_descriptor {\r
189         uint32_t addr;\r
190         union gmac_tx_status {\r
191                 uint32_t val;\r
192                 struct gmac_tx_status_bm {\r
193                         uint32_t len:14,     /**  0..13 Length of buffer */\r
194                         reserved:1,          /** 14            */\r
195                         b_last_buffer:1,     /** 15     Last buffer (in the current frame) */\r
196                         b_no_crc:1,          /** 16     No CRC */\r
197                         reserved1:3,         /** 17..19        */\r
198                         b_checksumoffload:3, /** 20..22 Transmit checksum generation offload errors */\r
199                         reserved2:3,         /** 23..25        */\r
200                         b_lco:1,             /** 26     Late collision, transmit error detected */\r
201                         b_exhausted:1,       /** 27     Buffer exhausted in mid frame */\r
202                         b_underrun:1,        /** 28     Transmit underrun */\r
203                         b_error:1,           /** 29     Retry limit exceeded, error detected */\r
204                         b_wrap:1,            /** 30     Marks last descriptor in TD list */\r
205                         b_used:1;            /** 31     User clear, GMAC sets this to 1 once a frame has been successfully transmitted */\r
206                 } bm;\r
207         } status;\r
208 } gmac_tx_descriptor_t;\r
209 \r
210 COMPILER_PACK_RESET()\r
211 \r
212 /**\r
213  * \brief Input parameters when initializing the gmac module mode.\r
214  */\r
215 typedef struct gmac_options {\r
216         /*  Enable/Disable CopyAllFrame */\r
217         uint8_t uc_copy_all_frame;\r
218         /* Enable/Disable NoBroadCast */\r
219         uint8_t uc_no_boardcast;\r
220         /* MAC address */\r
221         uint8_t uc_mac_addr[GMAC_ADDR_LENGTH];\r
222 } gmac_options_t;\r
223 \r
224 /** TX callback */\r
225 typedef void (*gmac_dev_tx_cb_t) (uint32_t ul_status, uint8_t *puc_buffer);\r
226 /** RX callback */\r
227 typedef void (*gmac_dev_rx_cb_t) (uint32_t ul_status);\r
228 /** Wakeup callback */\r
229 typedef void (*gmac_dev_wakeup_cb_t) (void);\r
230 \r
231 /**\r
232  * GMAC driver structure.\r
233  */\r
234 typedef struct gmac_device {\r
235 \r
236         /** Pointer to HW register base */\r
237         Gmac *p_hw;\r
238         /**\r
239          * Pointer to allocated TX buffer.\r
240          * Section 3.6 of AMBA 2.0 spec states that burst should not cross\r
241          * 1K Boundaries.\r
242          * Receive buffer manager writes are burst of 2 words => 3 lsb bits\r
243          * of the address shall be set to 0.\r
244          */\r
245         uint8_t *p_tx_buffer;\r
246         /** Pointer to allocated RX buffer */\r
247         uint8_t *p_rx_buffer;\r
248         /** Pointer to Rx TDs (must be 8-byte aligned) */\r
249         gmac_rx_descriptor_t *p_rx_dscr;\r
250         /** Pointer to Tx TDs (must be 8-byte aligned) */\r
251         gmac_tx_descriptor_t *p_tx_dscr;\r
252         /** Optional callback to be invoked once a frame has been received */\r
253         gmac_dev_rx_cb_t func_rx_cb;\r
254 #if( GMAC_USES_WAKEUP_CALLBACK )\r
255         /** Optional callback to be invoked once several TDs have been released */\r
256         gmac_dev_wakeup_cb_t func_wakeup_cb;\r
257 #endif\r
258 #if( GMAC_USES_TX_CALLBACK != 0 )\r
259         /** Optional callback list to be invoked once TD has been processed */\r
260         gmac_dev_tx_cb_t *func_tx_cb_list;\r
261 #endif\r
262         /** RX TD list size */\r
263         uint32_t ul_rx_list_size;\r
264         /** RX index for current processing TD */\r
265         uint32_t ul_rx_idx;\r
266         /** TX TD list size */\r
267         uint32_t ul_tx_list_size;\r
268         /** Circular buffer head pointer by upper layer (buffer to be sent) */\r
269         int32_t l_tx_head;\r
270         /** Circular buffer tail pointer incremented by handlers (buffer sent) */\r
271         int32_t l_tx_tail;\r
272 \r
273         /** Number of free TD before wakeup callback is invoked */\r
274         uint32_t uc_wakeup_threshold;\r
275 } gmac_device_t;\r
276 \r
277 /**\r
278  * \brief Write network control value.\r
279  *\r
280  * \param p_gmac   Pointer to the GMAC instance.\r
281  * \param ul_ncr   Network control value.\r
282  */\r
283 static inline void gmac_network_control(Gmac* p_gmac, uint32_t ul_ncr)\r
284 {\r
285         p_gmac->GMAC_NCR = ul_ncr;\r
286 }\r
287 \r
288 /**\r
289  * \brief Get network control value.\r
290  *\r
291  * \param p_gmac   Pointer to the GMAC instance.\r
292  */\r
293 \r
294 static inline uint32_t gmac_get_network_control(Gmac* p_gmac)\r
295 {\r
296         return p_gmac->GMAC_NCR;\r
297 }\r
298 \r
299 /**\r
300  * \brief Enable/Disable GMAC receive.\r
301  *\r
302  * \param p_gmac   Pointer to the GMAC instance.\r
303  * \param uc_enable   0 to disable GMAC receiver, else to enable it.\r
304  */\r
305 static inline void gmac_enable_receive(Gmac* p_gmac, uint8_t uc_enable)\r
306 {\r
307         if (uc_enable) {\r
308                 p_gmac->GMAC_NCR |= GMAC_NCR_RXEN;\r
309         } else {\r
310                 p_gmac->GMAC_NCR &= ~GMAC_NCR_RXEN;\r
311         }\r
312 }\r
313 \r
314 /**\r
315  * \brief Enable/Disable GMAC transmit.\r
316  *\r
317  * \param p_gmac   Pointer to the GMAC instance.\r
318  * \param uc_enable   0 to disable GMAC transmit, else to enable it.\r
319  */\r
320 static inline void gmac_enable_transmit(Gmac* p_gmac, uint8_t uc_enable)\r
321 {\r
322         if (uc_enable) {\r
323                 p_gmac->GMAC_NCR |= GMAC_NCR_TXEN;\r
324         } else {\r
325                 p_gmac->GMAC_NCR &= ~GMAC_NCR_TXEN;\r
326         }\r
327 }\r
328 \r
329 /**\r
330  * \brief Enable/Disable GMAC management.\r
331  *\r
332  * \param p_gmac   Pointer to the GMAC instance.\r
333  * \param uc_enable   0 to disable GMAC management, else to enable it.\r
334  */\r
335 static inline void gmac_enable_management(Gmac* p_gmac, uint8_t uc_enable)\r
336 {\r
337         if (uc_enable) {\r
338                 p_gmac->GMAC_NCR |= GMAC_NCR_MPE;\r
339         } else {\r
340                 p_gmac->GMAC_NCR &= ~GMAC_NCR_MPE;\r
341         }\r
342 }\r
343 \r
344 /**\r
345  * \brief Clear all statistics registers.\r
346  *\r
347  * \param p_gmac   Pointer to the GMAC instance.\r
348  */\r
349 static inline void gmac_clear_statistics(Gmac* p_gmac)\r
350 {\r
351         p_gmac->GMAC_NCR |= GMAC_NCR_CLRSTAT;\r
352 }\r
353 \r
354 /**\r
355  * \brief Increase all statistics registers.\r
356  *\r
357  * \param p_gmac   Pointer to the GMAC instance.\r
358  */\r
359 static inline void gmac_increase_statistics(Gmac* p_gmac)\r
360 {\r
361         p_gmac->GMAC_NCR |= GMAC_NCR_INCSTAT;\r
362 }\r
363 \r
364 /**\r
365  * \brief Enable/Disable statistics registers writing.\r
366  *\r
367  * \param p_gmac   Pointer to the GMAC instance.\r
368  * \param uc_enable   0 to disable the statistics registers writing, else to enable it.\r
369  */\r
370 static inline void gmac_enable_statistics_write(Gmac* p_gmac,\r
371                 uint8_t uc_enable)\r
372 {\r
373         if (uc_enable) {\r
374                 p_gmac->GMAC_NCR |= GMAC_NCR_WESTAT;\r
375         } else {\r
376                 p_gmac->GMAC_NCR &= ~GMAC_NCR_WESTAT;\r
377         }\r
378 }\r
379 \r
380 /**\r
381  * \brief In half-duplex mode, forces collisions on all received frames.\r
382  *\r
383  * \param p_gmac   Pointer to the GMAC instance.\r
384  * \param uc_enable   0 to disable the back pressure, else to enable it.\r
385  */\r
386 static inline void gmac_enable_back_pressure(Gmac* p_gmac, uint8_t uc_enable)\r
387 {\r
388         if (uc_enable) {\r
389                 p_gmac->GMAC_NCR |= GMAC_NCR_BP;\r
390         } else {\r
391                 p_gmac->GMAC_NCR &= ~GMAC_NCR_BP;\r
392         }\r
393 }\r
394 \r
395 /**\r
396  * \brief Start transmission.\r
397  *\r
398  * \param p_gmac   Pointer to the GMAC instance.\r
399  */\r
400 static inline void gmac_start_transmission(Gmac* p_gmac)\r
401 {\r
402         p_gmac->GMAC_NCR |= GMAC_NCR_TSTART;\r
403 }\r
404 \r
405 /**\r
406  * \brief Halt transmission.\r
407  *\r
408  * \param p_gmac   Pointer to the GMAC instance.\r
409  */\r
410 static inline void gmac_halt_transmission(Gmac* p_gmac)\r
411 {\r
412         p_gmac->GMAC_NCR |= GMAC_NCR_THALT;\r
413 }\r
414 \r
415 /**\r
416  * \brief Transmit pause frame.\r
417  *\r
418  * \param p_gmac   Pointer to the GMAC instance.\r
419  */\r
420 static inline void gmac_tx_pause_frame(Gmac* p_gmac)\r
421 {\r
422         p_gmac->GMAC_NCR |= GMAC_NCR_TXPF;\r
423 }\r
424 \r
425 /**\r
426  * \brief Transmit zero quantum pause frame.\r
427  *\r
428  * \param p_gmac   Pointer to the GMAC instance.\r
429  */\r
430 static inline void gmac_tx_pause_zero_quantum_frame(Gmac* p_gmac)\r
431 {\r
432         p_gmac->GMAC_NCR |= GMAC_NCR_TXZQPF;\r
433 }\r
434 \r
435 /**\r
436  * \brief Read snapshot.\r
437  *\r
438  * \param p_gmac   Pointer to the GMAC instance.\r
439  */\r
440 static inline void gmac_read_snapshot(Gmac* p_gmac)\r
441 {\r
442         p_gmac->GMAC_NCR |= GMAC_NCR_RDS;\r
443 }\r
444 \r
445 /**\r
446  * \brief Store receivetime stamp to memory.\r
447  *\r
448  * \param p_gmac   Pointer to the GMAC instance.\r
449  * \param uc_enable   0 to normal operation, else to enable the store.\r
450  */\r
451 static inline void gmac_store_rx_time_stamp(Gmac* p_gmac, uint8_t uc_enable)\r
452 {\r
453         if (uc_enable) {\r
454                 p_gmac->GMAC_NCR |= GMAC_NCR_SRTSM;\r
455         } else {\r
456                 p_gmac->GMAC_NCR &= ~GMAC_NCR_SRTSM;\r
457         }\r
458 }\r
459 \r
460 /**\r
461  * \brief Enable PFC priority-based pause reception.\r
462  *\r
463  * \param p_gmac   Pointer to the GMAC instance.\r
464  * \param uc_enable   1 to set the reception, 0 to disable.\r
465  */\r
466 static inline void gmac_enable_pfc_pause_frame(Gmac* p_gmac, uint8_t uc_enable)\r
467 {\r
468         if (uc_enable) {\r
469                 p_gmac->GMAC_NCR |= GMAC_NCR_ENPBPR;\r
470         } else {\r
471                 p_gmac->GMAC_NCR &= ~GMAC_NCR_ENPBPR;\r
472         }\r
473 }\r
474 \r
475 /**\r
476  * \brief Transmit PFC priority-based pause reception.\r
477  *\r
478  * \param p_gmac   Pointer to the GMAC instance.\r
479  */\r
480 static inline void gmac_transmit_pfc_pause_frame(Gmac* p_gmac)\r
481 {\r
482                 p_gmac->GMAC_NCR |= GMAC_NCR_TXPBPF;\r
483 }\r
484 \r
485 /**\r
486  * \brief Flush next packet.\r
487  *\r
488  * \param p_gmac   Pointer to the GMAC instance.\r
489  */\r
490 static inline void gmac_flush_next_packet(Gmac* p_gmac)\r
491 {\r
492                 p_gmac->GMAC_NCR |= GMAC_NCR_FNP;\r
493 }\r
494 \r
495 /**\r
496  * \brief Set up network configuration register.\r
497  *\r
498  * \param p_gmac   Pointer to the GMAC instance.\r
499   * \param ul_cfg   Network configuration value.\r
500  */\r
501 static inline void gmac_set_configure(Gmac* p_gmac, uint32_t ul_cfg)\r
502 {\r
503         p_gmac->GMAC_NCFGR = ul_cfg;\r
504 }\r
505 \r
506 /**\r
507  * \brief Get network configuration.\r
508  *\r
509  * \param p_gmac   Pointer to the GMAC instance.\r
510  *\r
511  * \return Network configuration.\r
512  */\r
513 static inline uint32_t gmac_get_configure(Gmac* p_gmac)\r
514 {\r
515         return p_gmac->GMAC_NCFGR;\r
516 }\r
517 \r
518 \r
519 /* Get and set DMA Configuration Register */\r
520 static inline void gmac_set_dma(Gmac* p_gmac, uint32_t ul_cfg)\r
521 {\r
522         p_gmac->GMAC_DCFGR = ul_cfg;\r
523 }\r
524 \r
525 static inline uint32_t gmac_get_dma(Gmac* p_gmac)\r
526 {\r
527         return p_gmac->GMAC_DCFGR;\r
528 }\r
529 \r
530 /**\r
531  * \brief Set speed.\r
532  *\r
533  * \param p_gmac   Pointer to the GMAC instance.\r
534  * \param uc_speed 1 to indicate 100Mbps, 0 to 10Mbps.\r
535  */\r
536 static inline void gmac_set_speed(Gmac* p_gmac, uint8_t uc_speed)\r
537 {\r
538         if (uc_speed) {\r
539                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_SPD;\r
540         } else {\r
541                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_SPD;\r
542         }\r
543 }\r
544 \r
545 /**\r
546  * \brief Enable/Disable Full-Duplex mode.\r
547  *\r
548  * \param p_gmac   Pointer to the GMAC instance.\r
549  * \param uc_enable   0 to disable the Full-Duplex mode, else to enable it.\r
550  */\r
551 static inline void gmac_enable_full_duplex(Gmac* p_gmac, uint8_t uc_enable)\r
552 {\r
553         if (uc_enable) {\r
554                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_FD;\r
555         } else {\r
556                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_FD;\r
557         }\r
558 }\r
559 \r
560 /**\r
561  * \brief Enable/Disable Copy(Receive) All Valid Frames.\r
562  *\r
563  * \param p_gmac   Pointer to the GMAC instance.\r
564  * \param uc_enable   0 to disable copying all valid frames, else to enable it.\r
565  */\r
566 static inline void gmac_enable_copy_all(Gmac* p_gmac, uint8_t uc_enable)\r
567 {\r
568         if (uc_enable) {\r
569                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_CAF;\r
570         } else {\r
571                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_CAF;\r
572         }\r
573 }\r
574 \r
575 /**\r
576  * \brief Enable/Disable jumbo frames (up to 10240 bytes).\r
577  *\r
578  * \param p_gmac   Pointer to the GMAC instance.\r
579  * \param uc_enable   0 to disable the jumbo frames, else to enable it.\r
580  */\r
581 static inline void gmac_enable_jumbo_frames(Gmac* p_gmac, uint8_t uc_enable)\r
582 {\r
583         if (uc_enable) {\r
584                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_JFRAME;\r
585         } else {\r
586                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_JFRAME;\r
587         }\r
588 }\r
589 \r
590 /**\r
591  * \brief Disable/Enable broadcast receiving.\r
592  *\r
593  * \param p_gmac   Pointer to the GMAC instance.\r
594  * \param uc_enable   1 to disable the broadcast, else to enable it.\r
595  */\r
596 static inline void gmac_disable_broadcast(Gmac* p_gmac, uint8_t uc_enable)\r
597 {\r
598         if (uc_enable) {\r
599                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_NBC;\r
600         } else {\r
601                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_NBC;\r
602         }\r
603 }\r
604 \r
605 /**\r
606  * \brief Enable/Disable multicast hash.\r
607  *\r
608  * \param p_gmac   Pointer to the GMAC instance.\r
609  * \param uc_enable   0 to disable the multicast hash, else to enable it.\r
610  */\r
611 static inline void gmac_enable_multicast_hash(Gmac* p_gmac, uint8_t uc_enable)\r
612 {\r
613         if (uc_enable) {\r
614                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;\r
615         } else {\r
616                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;\r
617         }\r
618 }\r
619 \r
620 /**\r
621  * \brief Enable/Disable big frames (over 1518, up to 1536).\r
622  *\r
623  * \param p_gmac   Pointer to the GMAC instance.\r
624  * \param uc_enable   0 to disable big frames else to enable it.\r
625  */\r
626 static inline void gmac_enable_big_frame(Gmac* p_gmac, uint8_t uc_enable)\r
627 {\r
628         if (uc_enable) {\r
629                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_MAXFS;\r
630         } else {\r
631                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_MAXFS;\r
632         }\r
633 }\r
634 \r
635 /**\r
636  * \brief Set MDC clock divider.\r
637  *\r
638  * \param p_gmac   Pointer to the GMAC instance.\r
639  * \param ul_mck   GMAC MCK.\r
640  *\r
641  * \return GMAC_OK if successfully.\r
642  */\r
643 static inline uint8_t gmac_set_mdc_clock(Gmac* p_gmac, uint32_t ul_mck)\r
644 {\r
645         uint32_t ul_clk;\r
646 \r
647         if (ul_mck > GMAC_MCK_SPEED_240MHZ) {\r
648                 return GMAC_INVALID;\r
649         } else if (ul_mck > GMAC_MCK_SPEED_160MHZ) {\r
650                 ul_clk = GMAC_NCFGR_CLK_MCK_96;\r
651         } else if (ul_mck > GMAC_MCK_SPEED_120MHZ) {\r
652                 ul_clk = GMAC_NCFGR_CLK_MCK_64;\r
653         } else if (ul_mck > GMAC_MCK_SPEED_80MHZ) {\r
654                 ul_clk = GMAC_NCFGR_CLK_MCK_48;\r
655         } else if (ul_mck > GMAC_MCK_SPEED_40MHZ) {\r
656                 ul_clk = GMAC_NCFGR_CLK_MCK_32;\r
657         } else if (ul_mck > GMAC_MCK_SPEED_20MHZ) {\r
658                 ul_clk = GMAC_NCFGR_CLK_MCK_16;\r
659         } else {\r
660                 ul_clk = GMAC_NCFGR_CLK_MCK_8;\r
661         }\r
662         ;\r
663         p_gmac->GMAC_NCFGR = (p_gmac->GMAC_NCFGR & ~GMAC_NCFGR_CLK_Msk) | ul_clk;\r
664         return GMAC_OK;\r
665 }\r
666 \r
667 /**\r
668  * \brief Enable/Disable retry test.\r
669  *\r
670  * \param p_gmac   Pointer to the GMAC instance.\r
671  * \param uc_enable   0 to disable the GMAC receiver, else to enable it.\r
672  */\r
673 static inline void gmac_enable_retry_test(Gmac* p_gmac, uint8_t uc_enable)\r
674 {\r
675         if (uc_enable) {\r
676                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_RTY;\r
677         } else {\r
678                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_RTY;\r
679         }\r
680 }\r
681 \r
682 /**\r
683  * \brief Enable/Disable pause (when a valid pause frame is received).\r
684  *\r
685  * \param p_gmac   Pointer to the GMAC instance.\r
686  * \param uc_enable   0 to disable pause frame, else to enable it.\r
687  */\r
688 static inline void gmac_enable_pause_frame(Gmac* p_gmac, uint8_t uc_enable)\r
689 {\r
690         if (uc_enable) {\r
691                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_PEN;\r
692         } else {\r
693                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_PEN;\r
694         }\r
695 }\r
696 \r
697 /**\r
698  * \brief Set receive buffer offset to 0 ~ 3.\r
699  *\r
700  * \param p_gmac   Pointer to the GMAC instance.\r
701  */\r
702 static inline void gmac_set_rx_buffer_offset(Gmac* p_gmac, uint8_t uc_offset)\r
703 {\r
704         p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_RXBUFO_Msk;\r
705         p_gmac->GMAC_NCFGR |= GMAC_NCFGR_RXBUFO(uc_offset);\r
706 }\r
707 \r
708 /**\r
709  * \brief Enable/Disable receive length field checking.\r
710  *\r
711  * \param p_gmac   Pointer to the GMAC instance.\r
712  * \param uc_enable   0 to disable receive length field checking, else to enable it.\r
713  */\r
714 static inline void gmac_enable_rx_length_check(Gmac* p_gmac, uint8_t uc_enable)\r
715 {\r
716         if (uc_enable) {\r
717                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_LFERD;\r
718         } else {\r
719                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_LFERD;\r
720         }\r
721 }\r
722 \r
723 /**\r
724  * \brief Enable/Disable discarding FCS field of received frames.\r
725  *\r
726  * \param p_gmac   Pointer to the GMAC instance.\r
727  * \param uc_enable   0 to disable discarding FCS field of received frames, else to enable it.\r
728  */\r
729 static inline void gmac_enable_discard_fcs(Gmac* p_gmac, uint8_t uc_enable)\r
730 {\r
731         if (uc_enable) {\r
732                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_RFCS;\r
733         } else {\r
734                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_RFCS;\r
735         }\r
736 }\r
737 \r
738 \r
739 /**\r
740  * \brief Enable/Disable frames to be received in half-duplex mode\r
741  * while transmitting.\r
742  *\r
743  * \param p_gmac   Pointer to the GMAC instance.\r
744  * \param uc_enable   0 to disable the received in half-duplex mode, else to enable it.\r
745  */\r
746 static inline void gmac_enable_efrhd(Gmac* p_gmac, uint8_t uc_enable)\r
747 {\r
748         if (uc_enable) {\r
749                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_EFRHD;\r
750         } else {\r
751                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_EFRHD;\r
752         }\r
753 }\r
754 \r
755 /**\r
756  * \brief Enable/Disable ignore RX FCS.\r
757  *\r
758  * \param p_gmac   Pointer to the GMAC instance.\r
759  * \param uc_enable   0 to disable ignore RX FCS, else to enable it.\r
760  */\r
761 static inline void gmac_enable_ignore_rx_fcs(Gmac* p_gmac, uint8_t uc_enable)\r
762 {\r
763         if (uc_enable) {\r
764                 p_gmac->GMAC_NCFGR |= GMAC_NCFGR_IRXFCS;\r
765         } else {\r
766                 p_gmac->GMAC_NCFGR &= ~GMAC_NCFGR_IRXFCS;\r
767         }\r
768 }\r
769 \r
770 /**\r
771  * \brief Get Network Status.\r
772  *\r
773  * \param p_gmac   Pointer to the GMAC instance.\r
774  *\r
775  * \return Network status.\r
776  */\r
777 static inline uint32_t gmac_get_status(Gmac* p_gmac)\r
778 {\r
779         return p_gmac->GMAC_NSR;\r
780 }\r
781 \r
782 /**\r
783  * \brief Get MDIO IN pin status.\r
784  *\r
785  * \param p_gmac   Pointer to the GMAC instance.\r
786  *\r
787  * \return MDIO IN pin status.\r
788  */\r
789 static inline uint8_t gmac_get_MDIO(Gmac* p_gmac)\r
790 {\r
791         return ((p_gmac->GMAC_NSR & GMAC_NSR_MDIO) > 0);\r
792 }\r
793 \r
794 /**\r
795  * \brief Check if PHY is idle.\r
796  *\r
797  * \param p_gmac   Pointer to the GMAC instance.\r
798  *\r
799  * \return  1 if PHY is idle.\r
800  */\r
801 static inline uint8_t gmac_is_phy_idle(Gmac* p_gmac)\r
802 {\r
803         return ((p_gmac->GMAC_NSR & GMAC_NSR_IDLE) > 0);\r
804 }\r
805 \r
806 /**\r
807  * \brief Return transmit status.\r
808  *\r
809  * \param p_gmac   Pointer to the GMAC instance.\r
810  *\r
811  * \return  Transmit status.\r
812  */\r
813 static inline uint32_t gmac_get_tx_status(Gmac* p_gmac)\r
814 {\r
815         return p_gmac->GMAC_TSR;\r
816 }\r
817 \r
818 /**\r
819  * \brief Clear transmit status.\r
820  *\r
821  * \param p_gmac   Pointer to the GMAC instance.\r
822  * \param ul_status   Transmit status.\r
823  */\r
824 static inline void gmac_clear_tx_status(Gmac* p_gmac, uint32_t ul_status)\r
825 {\r
826         p_gmac->GMAC_TSR = ul_status;\r
827 }\r
828 \r
829 /**\r
830  * \brief Return receive status.\r
831  *\r
832  * \param p_gmac   Pointer to the GMAC instance.\r
833  */\r
834 static inline uint32_t gmac_get_rx_status(Gmac* p_gmac)\r
835 {\r
836         return p_gmac->GMAC_RSR;\r
837 }\r
838 \r
839 /**\r
840  * \brief Clear receive status.\r
841  *\r
842  * \param p_gmac   Pointer to the GMAC instance.\r
843  * \param ul_status   Receive status.\r
844  */\r
845 static inline void gmac_clear_rx_status(Gmac* p_gmac, uint32_t ul_status)\r
846 {\r
847         p_gmac->GMAC_RSR = ul_status;\r
848 }\r
849 \r
850 /**\r
851  * \brief Set Rx Queue.\r
852  *\r
853  * \param p_gmac   Pointer to the GMAC instance.\r
854  * \param ul_addr   Rx queue address.\r
855  */\r
856 static inline void gmac_set_rx_queue(Gmac* p_gmac, uint32_t ul_addr)\r
857 {\r
858         p_gmac->GMAC_RBQB = GMAC_RBQB_ADDR_Msk & ul_addr;\r
859 }\r
860 \r
861 /**\r
862  * \brief Get Rx Queue Address.\r
863  *\r
864  * \param p_gmac   Pointer to the GMAC instance.\r
865  *\r
866  * \return  Rx queue address.\r
867  */\r
868 static inline uint32_t gmac_get_rx_queue(Gmac* p_gmac)\r
869 {\r
870         return p_gmac->GMAC_RBQB;\r
871 }\r
872 \r
873 /**\r
874  * \brief Set Tx Queue.\r
875  *\r
876  * \param p_gmac   Pointer to the GMAC instance.\r
877  * \param ul_addr  Tx queue address.\r
878  */\r
879 static inline void gmac_set_tx_queue(Gmac* p_gmac, uint32_t ul_addr)\r
880 {\r
881         p_gmac->GMAC_TBQB = GMAC_TBQB_ADDR_Msk & ul_addr;\r
882 }\r
883 \r
884 /**\r
885  * \brief Get Tx Queue.\r
886  *\r
887  * \param p_gmac   Pointer to the GMAC instance.\r
888  *\r
889  * \return  Rx queue address.\r
890  */\r
891 static inline uint32_t gmac_get_tx_queue(Gmac* p_gmac)\r
892 {\r
893         return p_gmac->GMAC_TBQB;\r
894 }\r
895 \r
896 /**\r
897  * \brief Enable interrupt(s).\r
898  *\r
899  * \param p_gmac   Pointer to the GMAC instance.\r
900  * \param ul_source   Interrupt source(s) to be enabled.\r
901  */\r
902 static inline void gmac_enable_interrupt(Gmac* p_gmac, uint32_t ul_source)\r
903 {\r
904         p_gmac->GMAC_IER = ul_source;\r
905 }\r
906 \r
907 /**\r
908  * \brief Disable interrupt(s).\r
909  *\r
910  * \param p_gmac   Pointer to the GMAC instance.\r
911  * \param ul_source   Interrupt source(s) to be disabled.\r
912  */\r
913 static inline void gmac_disable_interrupt(Gmac* p_gmac, uint32_t ul_source)\r
914 {\r
915         p_gmac->GMAC_IDR = ul_source;\r
916 }\r
917 \r
918 /**\r
919  * \brief Return interrupt status.\r
920  *\r
921  * \param p_gmac   Pointer to the GMAC instance.\r
922  *\r
923  * \return Interrupt status.\r
924  */\r
925 static inline uint32_t gmac_get_interrupt_status(Gmac* p_gmac)\r
926 {\r
927         return p_gmac->GMAC_ISR;\r
928 }\r
929 \r
930 /**\r
931  * \brief Return interrupt mask.\r
932  *\r
933  * \param p_gmac   Pointer to the GMAC instance.\r
934  *\r
935  * \return Interrupt mask.\r
936  */\r
937 static inline uint32_t gmac_get_interrupt_mask(Gmac* p_gmac)\r
938 {\r
939         return p_gmac->GMAC_IMR;\r
940 }\r
941 \r
942 /**\r
943  * \brief Execute PHY maintenance command.\r
944  *\r
945  * \param p_gmac   Pointer to the GMAC instance.\r
946  * \param uc_phy_addr   PHY address.\r
947  * \param uc_reg_addr   Register address.\r
948  * \param uc_rw   1 to Read, 0 to write.\r
949  * \param us_data   Data to be performed, write only.\r
950  */\r
951 static inline void gmac_maintain_phy(Gmac* p_gmac,\r
952                 uint8_t uc_phy_addr, uint8_t uc_reg_addr, uint8_t uc_rw,\r
953                 uint16_t us_data)\r
954 {\r
955         /* Wait until bus idle */\r
956         while ((p_gmac->GMAC_NSR & GMAC_NSR_IDLE) == 0);\r
957         /* Write maintain register */\r
958         p_gmac->GMAC_MAN = GMAC_MAN_WTN(GMAC_MAN_CODE_VALUE)\r
959                         | GMAC_MAN_CLTTO\r
960                         | GMAC_MAN_PHYA(uc_phy_addr)\r
961                         | GMAC_MAN_REGA(uc_reg_addr)\r
962                         | GMAC_MAN_OP((uc_rw ? GMAC_MAN_RW_TYPE : GMAC_MAN_READ_ONLY))\r
963                         | GMAC_MAN_DATA(us_data);\r
964 }\r
965 \r
966 /**\r
967  * \brief Get PHY maintenance data returned.\r
968  *\r
969  * \param p_gmac   Pointer to the GMAC instance.\r
970  *\r
971  * \return Get PHY data.\r
972  */\r
973 static inline uint16_t gmac_get_phy_data(Gmac* p_gmac)\r
974 {\r
975         /* Wait until bus idle */\r
976         while ((p_gmac->GMAC_NSR & GMAC_NSR_IDLE) == 0);\r
977         /* Return data */\r
978         return (uint16_t) (p_gmac->GMAC_MAN & GMAC_MAN_DATA_Msk);\r
979 }\r
980 \r
981 /**\r
982  * \brief Set Hash.\r
983  *\r
984  * \param p_gmac   Pointer to the GMAC instance.\r
985  * \param ul_hash_top   Hash top.\r
986  * \param ul_hash_bottom   Hash bottom.\r
987  */\r
988 static inline void gmac_set_hash(Gmac* p_gmac, uint32_t ul_hash_top,\r
989                 uint32_t ul_hash_bottom)\r
990 {\r
991         p_gmac->GMAC_HRB = ul_hash_bottom;\r
992         p_gmac->GMAC_HRT = ul_hash_top;\r
993 }\r
994 \r
995 /**\r
996  * \brief Set 64 bits Hash.\r
997  *\r
998  * \param p_gmac   Pointer to the GMAC instance.\r
999  * \param ull_hash   64 bits hash value.\r
1000  */\r
1001 static inline void gmac_set_hash64(Gmac* p_gmac, uint64_t ull_hash)\r
1002 {\r
1003         p_gmac->GMAC_HRB = (uint32_t) ull_hash;\r
1004         p_gmac->GMAC_HRT = (uint32_t) (ull_hash >> 32);\r
1005 }\r
1006 \r
1007 /**\r
1008  * \brief Set MAC Address.\r
1009  *\r
1010  * \param p_gmac   Pointer to the GMAC instance.\r
1011  * \param uc_index  GMAC specific address register index.\r
1012  * \param p_mac_addr  GMAC address.\r
1013  */\r
1014 static inline void gmac_set_address(Gmac* p_gmac, uint8_t uc_index,\r
1015                 uint8_t* p_mac_addr)\r
1016 {\r
1017         p_gmac->GMAC_SA[uc_index].GMAC_SAB = (p_mac_addr[3] << 24)\r
1018                         | (p_mac_addr[2] << 16)\r
1019                         | (p_mac_addr[1] << 8)\r
1020                         | (p_mac_addr[0]);\r
1021         p_gmac->GMAC_SA[uc_index].GMAC_SAT = (p_mac_addr[5] << 8)\r
1022                         | (p_mac_addr[4]);\r
1023 }\r
1024 \r
1025 /**\r
1026  * \brief Set MAC Address via 2 dword.\r
1027   *\r
1028  * \param p_gmac   Pointer to the GMAC instance.\r
1029  * \param uc_index  GMAC specific address register index.\r
1030  * \param ul_mac_top  GMAC top address.\r
1031  * \param ul_mac_bottom  GMAC bottom address.\r
1032  */\r
1033 static inline void gmac_set_address32(Gmac* p_gmac, uint8_t uc_index,\r
1034                 uint32_t ul_mac_top, uint32_t ul_mac_bottom)\r
1035 {\r
1036         p_gmac->GMAC_SA[uc_index].GMAC_SAB = ul_mac_bottom;\r
1037         p_gmac->GMAC_SA[uc_index].GMAC_SAT = ul_mac_top;\r
1038 }\r
1039 \r
1040 /**\r
1041  * \brief Set MAC Address via int64.\r
1042  *\r
1043  * \param p_gmac   Pointer to the GMAC instance.\r
1044  * \param uc_index  GMAC specific address register index.\r
1045  * \param ull_mac  64-bit GMAC address.\r
1046  */\r
1047 static inline void gmac_set_address64(Gmac* p_gmac, uint8_t uc_index,\r
1048                 uint64_t ull_mac)\r
1049 {\r
1050         p_gmac->GMAC_SA[uc_index].GMAC_SAB = (uint32_t) ull_mac;\r
1051         p_gmac->GMAC_SA[uc_index].GMAC_SAT = (uint32_t) (ull_mac >> 32);\r
1052 }\r
1053 \r
1054 /**\r
1055  * \brief Select media independent interface mode.\r
1056  *\r
1057  * \param p_gmac   Pointer to the GMAC instance.\r
1058  * \param mode   Media independent interface mode.\r
1059  */\r
1060 static inline void gmac_select_mii_mode(Gmac* p_gmac, gmac_mii_mode_t mode)\r
1061 {\r
1062         switch (mode) {\r
1063                 case GMAC_PHY_MII:\r
1064                 case GMAC_PHY_RMII:\r
1065                         p_gmac->GMAC_UR |= GMAC_UR_RMIIMII;\r
1066                 break;\r
1067 \r
1068                 default:\r
1069                         p_gmac->GMAC_UR &= ~GMAC_UR_RMIIMII;\r
1070                 break;\r
1071         }\r
1072 }\r
1073 \r
1074 uint8_t gmac_phy_read(Gmac* p_gmac, uint8_t uc_phy_address, uint8_t uc_address,\r
1075                 uint32_t* p_value);\r
1076 uint8_t gmac_phy_write(Gmac* p_gmac, uint8_t uc_phy_address,\r
1077                 uint8_t uc_address, uint32_t ul_value);\r
1078 void gmac_dev_init(Gmac* p_gmac, gmac_device_t* p_gmac_dev,\r
1079                 gmac_options_t* p_opt);\r
1080 uint32_t gmac_dev_read(gmac_device_t* p_gmac_dev, uint8_t* p_frame,\r
1081                 uint32_t ul_frame_size, uint32_t* p_rcv_size);\r
1082 uint32_t gmac_dev_write(gmac_device_t* p_gmac_dev, void *p_buffer,\r
1083                 uint32_t ul_size, gmac_dev_tx_cb_t func_tx_cb);\r
1084 uint32_t gmac_dev_get_tx_load(gmac_device_t* p_gmac_dev);\r
1085 void gmac_dev_set_rx_callback(gmac_device_t* p_gmac_dev,\r
1086                 gmac_dev_rx_cb_t func_rx_cb);\r
1087 uint8_t gmac_dev_set_tx_wakeup_callback(gmac_device_t* p_gmac_dev,\r
1088                 gmac_dev_wakeup_cb_t func_wakeup, uint8_t uc_threshold);\r
1089 void gmac_dev_reset(gmac_device_t* p_gmac_dev);\r
1090 void gmac_handler(gmac_device_t* p_gmac_dev);\r
1091 \r
1092 /// @cond 0\r
1093 /**INDENT-OFF**/\r
1094 #ifdef __cplusplus\r
1095 }\r
1096 #endif\r
1097 /**INDENT-ON**/\r
1098 /// @endcond\r
1099 \r
1100 /**\r
1101  * \page gmac_quickstart Quickstart guide for GMAC driver.\r
1102  *\r
1103  * This is the quickstart guide for the \ref gmac_group "Ethernet MAC",\r
1104  * with step-by-step instructions on how to configure and use the driver in a\r
1105  * selection of use cases.\r
1106  *\r
1107  * The use cases contain several code fragments. The code fragments in the\r
1108  * steps for setup can be copied into a custom initialization function, while\r
1109  * the steps for usage can be copied into, e.g., the main application function.\r
1110  *\r
1111  * \section gmac_basic_use_case Basic use case\r
1112  * In the basic use case, the GMAC driver are configured for:\r
1113  * - PHY component KSZ8051MNL is used\r
1114  * - GMAC uses MII mode\r
1115  * - The number of receive buffer is 16\r
1116  * - The number of transfer buffer is 8\r
1117  * - MAC address is set to 00-04-25-1c-a0-02\r
1118  * - IP address is set to 192.168.0.2\r
1119  * - IP address is set to 192.168.0.2\r
1120  * - Gateway is set to 192.168.0.1\r
1121  * - Network mask is 255.255.255.0\r
1122  * - PHY operation max retry count is 1000000\r
1123  * - GMAC is configured to not support copy all frame and support broadcast\r
1124  * - The data will be read from the ethernet\r
1125  *\r
1126  * \section gmac_basic_use_case_setup Setup steps\r
1127  *\r
1128  * \subsection gmac_basic_use_case_setup_prereq Prerequisites\r
1129  * -# \ref sysclk_group "System Clock Management (sysclock)"\r
1130  * -# \ref pmc_group "Power Management Controller (pmc)"\r
1131  * -# \ref ksz8051mnl_ethernet_phy_group "PHY component (KSZ8051MNL)"\r
1132  *\r
1133  * \subsection gmac_basic_use_case_setup_code Example code\r
1134  * Content of conf_eth.h\r
1135  * \code\r
1136  * #define GMAC_RX_BUFFERS                               16\r
1137  * #define GMAC_TX_BUFFERS                               8\r
1138  * #define MAC_PHY_RETRY_MAX                             1000000\r
1139  * #define ETHERNET_CONF_ETHADDR0                        0x00\r
1140  * #define ETHERNET_CONF_ETHADDR0                        0x00\r
1141  * #define ETHERNET_CONF_ETHADDR1                        0x04\r
1142  * #define ETHERNET_CONF_ETHADDR2                        0x25\r
1143  * #define ETHERNET_CONF_ETHADDR3                        0x1C\r
1144  * #define ETHERNET_CONF_ETHADDR4                        0xA0\r
1145  * #define ETHERNET_CONF_ETHADDR5                        0x02\r
1146  * #define ETHERNET_CONF_IPADDR0                         192\r
1147  * #define ETHERNET_CONF_IPADDR1                         168\r
1148  * #define ETHERNET_CONF_IPADDR2                         0\r
1149  * #define ETHERNET_CONF_IPADDR3                         2\r
1150  * #define ETHERNET_CONF_GATEWAY_ADDR0                   192\r
1151  * #define ETHERNET_CONF_GATEWAY_ADDR1                   168\r
1152  * #define ETHERNET_CONF_GATEWAY_ADDR2                   0\r
1153  * #define ETHERNET_CONF_GATEWAY_ADDR3                   1\r
1154  * #define ETHERNET_CONF_NET_MASK0                       255\r
1155  * #define ETHERNET_CONF_NET_MASK1                       255\r
1156  * #define ETHERNET_CONF_NET_MASK2                       255\r
1157  * #define ETHERNET_CONF_NET_MASK3                       0\r
1158  * #define ETH_PHY_MODE                                  ETH_PHY_MODE\r
1159  * \endcode\r
1160  *\r
1161  * A specific gmac device and the receive data buffer must be defined; another ul_frm_size should be defined\r
1162  * to trace the actual size of the data received.\r
1163  * \code\r
1164  * static gmac_device_t gs_gmac_dev;\r
1165  * static volatile uint8_t gs_uc_eth_buffer[GMAC_FRAME_LENTGH_MAX];\r
1166  *\r
1167  * uint32_t ul_frm_size;\r
1168  * \endcode\r
1169  *\r
1170  * Add to application C-file:\r
1171  * \code\r
1172  *   void gmac_init(void)\r
1173  *   {\r
1174  *       sysclk_init();\r
1175  *\r
1176  *       board_init();\r
1177  *\r
1178  *       pmc_enable_periph_clk(ID_GMAC);\r
1179  *\r
1180  *       gmac_option.uc_copy_all_frame = 0;\r
1181  *       gmac_option.uc_no_boardcast = 0;\r
1182  *       memcpy(gmac_option.uc_mac_addr, gs_uc_mac_address, sizeof(gs_uc_mac_address));\r
1183  *       gs_gmac_dev.p_hw = GMAC;\r
1184  *\r
1185  *       gmac_dev_init(GMAC, &gs_gmac_dev, &gmac_option);\r
1186  *\r
1187  *       NVIC_EnableIRQ(GMAC_IRQn);\r
1188  *\r
1189  *       ethernet_phy_init(GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz());\r
1190  *\r
1191  *       ethernet_phy_auto_negotiate(GMAC, BOARD_GMAC_PHY_ADDR);\r
1192  *\r
1193  *       ethernet_phy_set_link(GMAC, BOARD_GMAC_PHY_ADDR, 1);\r
1194  * \endcode\r
1195  *\r
1196  * \subsection gmac_basic_use_case_setup_flow Workflow\r
1197  * - Ensure that conf_eth.h is present and contains the\r
1198  * following configuration symbol. This configuration file is used\r
1199  * by the driver and should not be included by the application.\r
1200  * -# Define the receiving buffer size used in the internal GMAC driver.\r
1201  * The buffer size used for RX is GMAC_RX_BUFFERS * 128.\r
1202  * If it was supposed receiving a large number of frame, the\r
1203  * GMAC_RX_BUFFERS should be set higher. E.g., the application wants to accept\r
1204  * a ping echo test of 2048, the GMAC_RX_BUFFERS should be set at least\r
1205  * (2048/128)=16, and as there are additional frames coming, a preferred\r
1206  * number is 24 depending on a normal Ethernet throughput.\r
1207  *   - \code\r
1208  *        #define GMAC_RX_BUFFERS                               16\r
1209  *   \endcode\r
1210  * -# Define the transmitting buffer size used in the internal GMAC driver.\r
1211  * The buffer size used for TX is GMAC_TX_BUFFERS * 1518.\r
1212  *   - \code\r
1213  *        #define GMAC_TX_BUFFERS                               8\r
1214  *   \endcode\r
1215  * -# Define maximum retry time for a PHY read/write operation.\r
1216  *   - \code\r
1217  *        #define MAC_PHY_RETRY_MAX                             1000000\r
1218  *   \endcode\r
1219  * -# Define the MAC address. 00:04:25:1C:A0:02 is the address reserved\r
1220  * for ATMEL, application should always change this address to its' own.\r
1221  *   - \code\r
1222  *        #define ETHERNET_CONF_ETHADDR0                        0x00\r
1223  *        #define ETHERNET_CONF_ETHADDR1                        0x04\r
1224  *        #define ETHERNET_CONF_ETHADDR2                        0x25\r
1225  *        #define ETHERNET_CONF_ETHADDR3                        0x1C\r
1226  *        #define ETHERNET_CONF_ETHADDR4                        0xA0\r
1227  *        #define ETHERNET_CONF_ETHADDR5                        0x02\r
1228  *   \endcode\r
1229  * -# Define the IP address configration used in the application. When DHCP\r
1230  *  is enabled, this configuration is not effected.\r
1231  *   - \code\r
1232  *        #define ETHERNET_CONF_IPADDR0                         192\r
1233  *        #define ETHERNET_CONF_IPADDR1                         168\r
1234  *        #define ETHERNET_CONF_IPADDR2                         0\r
1235  *        #define ETHERNET_CONF_IPADDR3                         2\r
1236  *        #define ETHERNET_CONF_GATEWAY_ADDR0                   192\r
1237  *        #define ETHERNET_CONF_GATEWAY_ADDR1                   168\r
1238  *        #define ETHERNET_CONF_GATEWAY_ADDR2                   0\r
1239  *        #define ETHERNET_CONF_GATEWAY_ADDR3                   1\r
1240  *        #define ETHERNET_CONF_NET_MASK0                       255\r
1241  *        #define ETHERNET_CONF_NET_MASK1                       255\r
1242  *        #define ETHERNET_CONF_NET_MASK2                       255\r
1243  *        #define ETHERNET_CONF_NET_MASK3                       0\r
1244  *   \endcode\r
1245  * -# Configure the PHY maintainance interface.\r
1246  *   - \code\r
1247  *        #define ETH_PHY_MODE                                  GMAC_PHY_MII\r
1248  *   \endcode\r
1249  * -# Enable the system clock:\r
1250  *   - \code sysclk_init(); \endcode\r
1251  * -# Enable PIO configurations for GMAC:\r
1252  *   - \code board_init(); \endcode\r
1253  * -# Enable PMC clock for GMAC:\r
1254  *   - \code pmc_enable_periph_clk(ID_GMAC); \endcode\r
1255  * -# Set the GMAC options; it's set to copy all frame and support broadcast:\r
1256  *   - \code\r
1257  *         gmac_option.uc_copy_all_frame = 0;\r
1258  *         gmac_option.uc_no_boardcast = 0;\r
1259  *         memcpy(gmac_option.uc_mac_addr, gs_uc_mac_address, sizeof(gs_uc_mac_address));\r
1260  *         gs_gmac_dev.p_hw = GMAC;\r
1261  * \endcode\r
1262  * -# Initialize GMAC device with the filled option:\r
1263  *   - \code\r
1264  *         gmac_dev_init(GMAC, &gs_gmac_dev, &gmac_option);\r
1265  * \endcode\r
1266  * -# Enable the interrupt service for GMAC:\r
1267  *   - \code\r
1268  *         NVIC_EnableIRQ(GMAC_IRQn);\r
1269  * \endcode\r
1270  * -# Initialize the PHY component:\r
1271  *   - \code\r
1272  *         ethernet_phy_init(GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz());\r
1273  * \endcode\r
1274   * -# The link will be established based on auto negotiation.\r
1275  *   - \code\r
1276  *         ethernet_phy_auto_negotiate(GMAC, BOARD_GMAC_PHY_ADDR);\r
1277  * \endcode\r
1278  * -# Establish the ethernet link; the network can be worked from now on:\r
1279  *   - \code\r
1280  *         ethernet_phy_set_link(GMAC, BOARD_GMAC_PHY_ADDR, 1);\r
1281  * \endcode\r
1282  *\r
1283  * \section gmac_basic_use_case_usage Usage steps\r
1284  * \subsection gmac_basic_use_case_usage_code Example code\r
1285  * Add to, e.g., main loop in application C-file:\r
1286  * \code\r
1287  *    gmac_dev_read(&gs_gmac_dev, (uint8_t *) gs_uc_eth_buffer, sizeof(gs_uc_eth_buffer), &ul_frm_size));\r
1288  * \endcode\r
1289  *\r
1290  * \subsection gmac_basic_use_case_usage_flow Workflow\r
1291  * -# Start reading the data from the ethernet:\r
1292  *   - \code gmac_dev_read(&gs_gmac_dev, (uint8_t *) gs_uc_eth_buffer, sizeof(gs_uc_eth_buffer), &ul_frm_size)); \endcode\r
1293  */\r
1294 \r
1295 #       define GMAC_STATS 0\r
1296 \r
1297 #if( GMAC_STATS != 0 )\r
1298 \r
1299         /* Here below some code to study the types and\r
1300         frequencies of  GMAC interrupts. */\r
1301         #define GMAC_IDX_RXUBR 0\r
1302         #define GMAC_IDX_TUR   1\r
1303         #define GMAC_IDX_RLEX  2\r
1304         #define GMAC_IDX_TFC   3\r
1305         #define GMAC_IDX_RCOMP 4\r
1306         #define GMAC_IDX_TCOMP 5\r
1307         #define GMAC_IDX_ROVR  6\r
1308         #define GMAC_IDX_HRESP 7\r
1309         #define GMAC_IDX_PFNZ  8\r
1310         #define GMAC_IDX_PTZ   9\r
1311 \r
1312         struct SGmacStats {\r
1313                 unsigned recvCount;\r
1314                 unsigned rovrCount;\r
1315                 unsigned bnaCount;\r
1316                 unsigned sendCount;\r
1317                 unsigned sovrCount;\r
1318                 unsigned incompCount;\r
1319                 unsigned truncCount;\r
1320 \r
1321                 unsigned intStatus[10];\r
1322         };\r
1323         extern struct SGmacStats gmacStats;\r
1324 \r
1325         struct SIntPair {\r
1326                 const char *name;\r
1327                 unsigned mask;\r
1328                 int index;\r
1329         };\r
1330 \r
1331         #define MK_PAIR( NAME )   #NAME, GMAC_IER_##NAME, GMAC_IDX_##NAME\r
1332         static const struct SIntPair intPairs[] = {\r
1333                 { MK_PAIR( RXUBR ) }, /* Enable receive used bit read interrupt. */\r
1334                 { MK_PAIR( TUR   ) }, /* Enable transmit underrun interrupt. */\r
1335                 { MK_PAIR( RLEX  ) }, /* Enable retry limit  exceeded interrupt. */\r
1336                 { MK_PAIR( TFC   ) }, /* Enable transmit buffers exhausted in mid-frame interrupt. */\r
1337                 { MK_PAIR( RCOMP ) }, /* Receive complete */\r
1338                 { MK_PAIR( TCOMP ) }, /* Enable transmit complete interrupt. */\r
1339                 { MK_PAIR( ROVR  ) }, /* Enable receive overrun interrupt. */\r
1340                 { MK_PAIR( HRESP ) }, /* Enable Hresp not OK interrupt. */\r
1341                 { MK_PAIR( PFNZ  ) }, /* Enable pause frame received interrupt. */\r
1342                 { MK_PAIR( PTZ   ) }  /* Enable pause time zero interrupt. */\r
1343         };\r
1344 \r
1345         void gmac_show_irq_counts ();\r
1346 \r
1347 #endif\r
1348 \r
1349 #endif /* GMAC_H_INCLUDED */\r