am335x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file am335x_eth_driver.c
3  * @brief Sitara AM335x Gigabit Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "soc_am335x.h"
36 #include "hw_types.h"
37 #include "hw_cm_per.h"
38 #include "hw_control_am335x.h"
39 #include "hw_cpsw_ale.h"
40 #include "hw_cpsw_cpdma.h"
41 #include "hw_cpsw_cpdma_stateram.h"
42 #include "hw_cpsw_port.h"
43 #include "hw_cpsw_sl.h"
44 #include "hw_cpsw_ss.h"
45 #include "hw_cpsw_wr.h"
46 #include "hw_mdio.h"
47 #include "interrupt.h"
48 #include "core/net.h"
50 #include "debug.h"
51 
52 //MDIO input clock frequency
53 #define MDIO_INPUT_CLK 125000000
54 //MDIO output clock frequency
55 #define MDIO_OUTPUT_CLK 1000000
56 
57 //Underlying network interface (port 1)
58 static NetInterface *nicDriverInterface1 = NULL;
59 //Underlying network interface (port 2)
60 static NetInterface *nicDriverInterface2 = NULL;
61 
62 //IAR EWARM compiler?
63 #if defined(__ICCARM__)
64 
65 //Transmit buffer (port 1)
66 #pragma data_alignment = 4
67 #pragma location = AM335X_ETH_RAM_SECTION
68 static uint8_t txBuffer1[AM335X_ETH_TX_BUFFER_COUNT][AM335X_ETH_TX_BUFFER_SIZE];
69 //Transmit buffer (port 2)
70 #pragma data_alignment = 4
71 #pragma location = AM335X_ETH_RAM_SECTION
72 static uint8_t txBuffer2[AM335X_ETH_TX_BUFFER_COUNT][AM335X_ETH_TX_BUFFER_SIZE];
73 //Receive buffer
74 #pragma data_alignment = 4
75 #pragma location = AM335X_ETH_RAM_SECTION
77 //Transmit buffer descriptors (port 1)
78 #pragma data_alignment = 4
79 #pragma location = AM335X_ETH_RAM_CPPI_SECTION
81 //Transmit buffer descriptors (port 2)
82 #pragma data_alignment = 4
83 #pragma location = AM335X_ETH_RAM_CPPI_SECTION
85 //Receive buffer descriptors
86 #pragma data_alignment = 4
87 #pragma location = AM335X_ETH_RAM_CPPI_SECTION
89 
90 //GCC compiler?
91 #else
92 
93 //Transmit buffer (port 1)
95  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_SECTION)));
96 //Transmit buffer (port 2)
98  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_SECTION)));
99 //Receive buffer
101  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_SECTION)));
102 //Transmit buffer descriptors (port 1)
104  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_CPPI_SECTION)));
105 //Transmit buffer descriptors (port 2)
107  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_CPPI_SECTION)));
108 //Receive buffer descriptors
110  __attribute__((aligned(4), __section__(AM335X_ETH_RAM_CPPI_SECTION)));
111 
112 #endif
113 
114 //Pointer to the current TX buffer descriptor (port1)
115 static Am335xTxBufferDesc *txCurBufferDesc1;
116 //Pointer to the current TX buffer descriptor (port 2)
117 static Am335xTxBufferDesc *txCurBufferDesc2;
118 //Pointer to the current RX buffer descriptor
119 static Am335xRxBufferDesc *rxCurBufferDesc;
120 
121 
122 /**
123  * @brief AM335x Ethernet MAC driver (port1)
124  **/
125 
127 {
129  ETH_MTU,
140  FALSE,
141  TRUE,
142  TRUE,
143  FALSE
144 };
145 
146 
147 /**
148  * @brief AM335x Ethernet MAC driver (port2)
149  **/
150 
152 {
154  ETH_MTU,
165  FALSE,
166  TRUE,
167  TRUE,
168  FALSE
169 };
170 
171 
172 /**
173  * @brief AM335x Ethernet MAC initialization (port 1)
174  * @param[in] interface Underlying network interface
175  * @return Error code
176  **/
177 
179 {
180  error_t error;
181  uint32_t temp;
182 
183  //Debug message
184  TRACE_INFO("Initializing AM335x Ethernet MAC (port 1)...\r\n");
185 
186  //Initialize CPSW instance
187  am335xEthInitInstance(interface);
188 
189  //Save underlying network interface
190  nicDriverInterface1 = interface;
191 
192  //PHY transceiver initialization
193  error = interface->phyDriver->init(interface);
194  //Any error to report?
195  if(error)
196  {
197  return error;
198  }
199 
200  //Unspecified MAC address?
201  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
202  {
203  //Use the factory preprogrammed MAC address
204  interface->macAddr.b[0] = CONTROL_MAC_ID_HI_R(0) >> CONTROL_MAC_ID0_HI_MACADDR_47_40_SHIFT;
205  interface->macAddr.b[1] = CONTROL_MAC_ID_HI_R(0) >> CONTROL_MAC_ID0_HI_MACADDR_39_32_SHIFT;
206  interface->macAddr.b[2] = CONTROL_MAC_ID_HI_R(0) >> CONTROL_MAC_ID0_HI_MACADDR_31_24_SHIFT;
207  interface->macAddr.b[3] = CONTROL_MAC_ID_HI_R(0) >> CONTROL_MAC_ID0_HI_MACADDR_23_16_SHIFT;
208  interface->macAddr.b[4] = CONTROL_MAC_ID_LO_R(0) >> CONTROL_MAC_ID0_LO_MACADDR_15_8_SHIFT;
209  interface->macAddr.b[5] = CONTROL_MAC_ID_LO_R(0) >> CONTROL_MAC_ID0_LO_MACADDR_7_0_SHIFT;
210 
211  //Generate the 64-bit interface identifier
212  macAddrToEui64(&interface->macAddr, &interface->eui64);
213  }
214 
215  //Set port state (forwarding)
216  temp = CPSW_ALE_PORTCTL_R(1) & ~CPSW_ALE_PORTCTL_PORT_STATE_MASK;
217  CPSW_ALE_PORTCTL_R(1) = temp | CPSW_ALE_PORTCTL_PORT_STATE_FORWARD;
218 
219  //Set the MAC address of the station
220  CPSW_PORT1_SA_HI_R = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
221  CPSW_PORT1_SA_LO_R = interface->macAddr.w[2];
222 
223  //Configure VLAN identifier and VLAN priority
224  CPSW_PORT1_VLAN_R = (0 << CPSW_PORT_P_VLAN_PRI_SHIFT) |
225  (CPSW_PORT1 << CPSW_PORT_P_VLAN_VID_SHIFT);
226 
227  //Add a VLAN entry in the ALE table
229 
230  //Add a VLAN/unicast address entry in the ALE table
231  am335xEthAddVlanAddrEntry(CPSW_PORT1, CPSW_PORT1, &interface->macAddr);
232 
233  //Enable CPSW statistics
234  CPSW_SS_STAT_PORT_EN_R |= CPSW_SS_STAT_PORT_EN_P1_MASK;
235 
236  //Enable TX and RX
237  CPSW_SL1_MACCTRL_R = CPSW_SL_MACCTRL_GMII_EN_MASK;
238 
239  //Accept any packets from the upper layer
240  osSetEvent(&interface->nicTxEvent);
241 
242  //Successful initialization
243  return NO_ERROR;
244 }
245 
246 
247 /**
248  * @brief AM335x Ethernet MAC initialization (port 2)
249  * @param[in] interface Underlying network interface
250  * @return Error code
251  **/
252 
254 {
255  error_t error;
256  uint32_t temp;
257 
258  //Debug message
259  TRACE_INFO("Initializing AM335x Ethernet MAC (port 2)...\r\n");
260 
261  //Initialize CPSW instance
262  am335xEthInitInstance(interface);
263 
264  //Save underlying network interface
265  nicDriverInterface2 = interface;
266 
267  //PHY transceiver initialization
268  error = interface->phyDriver->init(interface);
269  //Any error to report?
270  if(error)
271  {
272  return error;
273  }
274 
275  //Unspecified MAC address?
276  if(macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
277  {
278  //Use the factory preprogrammed MAC address
279  interface->macAddr.b[0] = CONTROL_MAC_ID_HI_R(1) >> CONTROL_MAC_ID1_HI_MACADDR_47_40_SHIFT;
280  interface->macAddr.b[1] = CONTROL_MAC_ID_HI_R(1) >> CONTROL_MAC_ID1_HI_MACADDR_39_32_SHIFT;
281  interface->macAddr.b[2] = CONTROL_MAC_ID_HI_R(1) >> CONTROL_MAC_ID1_HI_MACADDR_31_24_SHIFT;
282  interface->macAddr.b[3] = CONTROL_MAC_ID_HI_R(1) >> CONTROL_MAC_ID1_HI_MACADDR_23_16_SHIFT;
283  interface->macAddr.b[4] = CONTROL_MAC_ID_LO_R(1) >> CONTROL_MAC_ID1_LO_MACADDR_15_8_SHIFT;
284  interface->macAddr.b[5] = CONTROL_MAC_ID_LO_R(1) >> CONTROL_MAC_ID1_LO_MACADDR_7_0_SHIFT;
285 
286  //Generate the 64-bit interface identifier
287  macAddrToEui64(&interface->macAddr, &interface->eui64);
288  }
289 
290  //Set port state (forwarding)
291  temp = CPSW_ALE_PORTCTL_R(2) & ~CPSW_ALE_PORTCTL_PORT_STATE_MASK;
292  CPSW_ALE_PORTCTL_R(2) = temp | CPSW_ALE_PORTCTL_PORT_STATE_FORWARD;
293 
294  //Set the MAC address of the station
295  CPSW_PORT2_SA_HI_R = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
296  CPSW_PORT2_SA_LO_R = interface->macAddr.w[2];
297 
298  //Configure VLAN identifier and VLAN priority
299  CPSW_PORT2_VLAN_R = (0 << CPSW_PORT_P_VLAN_PRI_SHIFT) |
300  (CPSW_PORT2 << CPSW_PORT_P_VLAN_VID_SHIFT);
301 
302  //Add a VLAN entry in the ALE table
304 
305  //Add a VLAN/unicast address entry in the ALE table
306  am335xEthAddVlanAddrEntry(CPSW_PORT2, CPSW_PORT2, &interface->macAddr);
307 
308  //Enable CPSW statistics
309  CPSW_SS_STAT_PORT_EN_R |= CPSW_SS_STAT_PORT_EN_P2_MASK;
310 
311  //Enable TX and RX
312  CPSW_SL2_MACCTRL_R = CPSW_SL_MACCTRL_GMII_EN_MASK;
313 
314  //Accept any packets from the upper layer
315  osSetEvent(&interface->nicTxEvent);
316 
317  //Successful initialization
318  return NO_ERROR;
319 }
320 
321 
322 /**
323  * @brief Initialize CPSW instance
324  * @param[in] interface Underlying network interface
325  **/
326 
328 {
329  uint_t i;
330  uint32_t temp;
331 #ifdef ti_sysbios_BIOS___VERS
332  Hwi_Params hwiParams;
333 #endif
334 
335  //Initialization sequence is performed once
336  if(nicDriverInterface1 == NULL && nicDriverInterface2 == NULL)
337  {
338  //Select the interface mode (MII/RMII/RGMII) and configure pin muxing
339  am335xEthInitGpio(interface);
340 
341  //Enable the CPSW subsystem clocks
342  CM_PER_CPGMAC0_CLKCTRL_R = CM_PER_CPGMAC0_CLKCTRL_MODULEMODE_ENABLE;
343 
344  //Wait for the CPSW module to be fully functional
345  do
346  {
347  //Get module idle status
348  temp = (CM_PER_CPGMAC0_CLKCTRL_R & CM_PER_CPGMAC0_CLKCTRL_IDLEST) >>
349  CM_PER_CPGMAC0_CLKCTRL_IDLEST_SHIFT;
350 
351  //Keep looping as long as the module is not fully functional
352  } while(temp != CM_PER_CPGMAC0_CLKCTRL_IDLEST_FUNC);
353 
354  //Start a software forced wake-up transition
355  CM_PER_CPSW_CLKSTCTRL_R = CM_PER_CPSW_CLKSTCTRL_CLKTRCTRL_SW_WKUP;
356 
357  //Wait for the CPSW 125 MHz OCP clock to be active
358  do
359  {
360  //Get the state of the CPSW 125 MHz OCP clock
361  temp = (CM_PER_CPSW_CLKSTCTRL_R & CM_PER_CPSW_CLKSTCTRL_CLKACTIVITY_CPSW_125MHZ_GCLK) >>
362  CM_PER_CPSW_CLKSTCTRL_CLKACTIVITY_CPSW_125MHZ_GCLK_SHIFT;
363 
364  //Keep looping as long as the clock is inactive
365  } while(temp != CM_PER_CPSW_CLKSTCTRL_CLKACTIVITY_CPSW_125MHZ_GCLK_ACT);
366 
367  //Reset CPSW subsystem
368  CPSW_SS_SOFT_RESET_R = CPSW_SS_SOFT_RESET_MASK;
369  //Wait for the reset to complete
370  while((CPSW_SS_SOFT_RESET_R & CPSW_SS_SOFT_RESET_MASK) != 0)
371  {
372  }
373 
374  //Reset CPSW wrapper module
375  CPSW_WR_SOFT_RESET_R = CPSW_WR_SOFT_RESET_MASK;
376  //Wait for the reset to complete
377  while((CPSW_WR_SOFT_RESET_R & CPSW_WR_SOFT_RESET_MASK) != 0)
378  {
379  }
380 
381  //Reset CPSW sliver 1 logic
382  CPSW_SL1_SOFT_RESET_R = CPSW_SL_SOFT_RESET_MASK;
383  //Wait for the reset to complete
384  while((CPSW_SL1_SOFT_RESET_R & CPSW_SL_SOFT_RESET_MASK) != 0)
385  {
386  }
387 
388  //Reset CPSW sliver 2 logic
389  CPSW_SL2_SOFT_RESET_R = CPSW_SL_SOFT_RESET_MASK;
390  //Wait for the reset to complete
391  while((CPSW_SL2_SOFT_RESET_R & CPSW_SL_SOFT_RESET_MASK) != 0)
392  {
393  }
394 
395  //Reset CPSW CPDMA module
396  CPSW_CPDMA_SOFT_RESET_R = CPSW_CPDMA_SOFT_RESET_MASK;
397  //Wait for the reset to complete
398  while((CPSW_CPDMA_SOFT_RESET_R & CPSW_CPDMA_SOFT_RESET_MASK) != 0)
399  {
400  }
401 
402  //Initialize the HDPs and the CPs to NULL
403  for(i = CPSW_CH0; i <= CPSW_CH7; i++)
404  {
405  //TX head descriptor pointer
407  //TX completion pointer
409  //RX head descriptor pointer
411  //RX completion pointer
413  }
414 
415  //Enable ALE and clear ALE address table
416  CPSW_ALE_CTRL_R = CPSW_ALE_CTRL_EN_MASK | CPSW_ALE_CTRL_CLR_TBL_MASK;
417 
418  //For dual MAC mode, configure VLAN aware mode
419  CPSW_ALE_CTRL_R |= CPSW_ALE_CTRL_VLAN_AWARE_MASK;
420 
421  //Set dual MAC mode for port 0
422  temp = CPSW_PORT0_TX_IN_CTL_R & ~CPSW_PORT_P_TX_IN_CTL_SEL_MASK;
424 
425  //Set host port state (forwarding)
426  temp = CPSW_ALE_PORTCTL_R(0) & ~CPSW_ALE_PORTCTL_PORT_STATE_MASK;
427  CPSW_ALE_PORTCTL_R(0) = temp | CPSW_ALE_PORTCTL_PORT_STATE_FORWARD;
428 
429  //Enable CPSW statistics
430  CPSW_SS_STAT_PORT_EN_R = CPSW_SS_STAT_PORT_EN_P0_MASK;
431 
432  //Configure TX and RX buffer descriptors
433  am335xEthInitBufferDesc(interface);
434 
435  //Acknowledge TX and interrupts for proper interrupt pulsing
438 
439  //Enable channel 1 and 2 interrupts of the DMA engine
441  //Enable TX completion interrupts
442  CPSW_WR_C_TX_EN_R(CPSW_CORE0) |= (1 << CPSW_CH1) | (1 << CPSW_CH2);
443 
444  //Enable channel 0 interrupts of the DMA engine
446  //Enable RX completion interrupts
448 
449 #ifdef ti_sysbios_BIOS___VERS
450  //Configure TX interrupt
451  Hwi_Params_init(&hwiParams);
452  hwiParams.enableInt = FALSE;
453  hwiParams.priority = AM335X_ETH_IRQ_PRIORITY;
454 
455  //Register TX interrupt handler
456  Hwi_create(SYS_INT_3PGSWTXINT0, (Hwi_FuncPtr) am335xEthTxIrqHandler,
457  &hwiParams, NULL);
458 
459  //Configure RX interrupt
460  Hwi_Params_init(&hwiParams);
461  hwiParams.enableInt = FALSE;
462  hwiParams.priority = AM335X_ETH_IRQ_PRIORITY;
463 
464  //Register RX interrupt handler
465  Hwi_create(SYS_INT_3PGSWRXINT0, (Hwi_FuncPtr) am335xEthRxIrqHandler,
466  &hwiParams, NULL);
467 #else
468  //Register interrupt handlers
471 
472  //Configure TX interrupt priority
474  AINTC_HOSTINT_ROUTE_IRQ);
475 
476  //Configure RX interrupt priority
478  AINTC_HOSTINT_ROUTE_IRQ);
479 #endif
480 
481  //Enable the transmission and reception
482  CPSW_CPDMA_TX_CTRL_R = CPSW_CPDMA_TX_CTRL_EN_MASK;
483  CPSW_CPDMA_RX_CTRL_R = CPSW_CPDMA_RX_CTRL_EN_MASK;
484 
485  //Calculate the MDC clock divider to be used
486  temp = (MDIO_INPUT_CLK / MDIO_OUTPUT_CLK) - 1;
487 
488  //Initialize MDIO interface
489  MDIO_CTRL_R = MDIO_CTRL_EN_MASK | MDIO_CTRL_FAULTENB_MASK |
490  (temp & MDIO_CTRL_CLKDIV_MASK);
491  }
492 }
493 
494 
495 /**
496  * @brief GPIO configuration
497  * @param[in] interface Underlying network interface
498  **/
499 
500 __weak_func void am335xEthInitGpio(NetInterface *interface)
501 {
502 //BeagleBone Black board?
503 #if defined(USE_BEAGLEBONE_BLACK)
504  //Select MII interface mode for port 1
506 
507  //Configure MII1_TX_CLK (GPIO3_9)
508  CONTROL_CONF_MII1_TXCLK_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
509  //Configure MII1_TX_EN (GPIO3_3)
510  CONTROL_CONF_MII1_TXEN_R = CONTROL_CONF_MUXMODE(0);
511  //Configure MII1_TXD0 (GPIO0_28)
512  CONTROL_CONF_MII1_TXD0_R = CONTROL_CONF_MUXMODE(0);
513  //Configure MII1_TXD1 (GPIO0_21)
514  CONTROL_CONF_MII1_TXD1_R = CONTROL_CONF_MUXMODE(0);
515  //Configure MII1_TXD2 (GPIO0_17)
516  CONTROL_CONF_MII1_TXD2_R = CONTROL_CONF_MUXMODE(0);
517  //Configure MII1_TXD3 (GPIO0_16)
518  CONTROL_CONF_MII1_TXD3_R = CONTROL_CONF_MUXMODE(0);
519 
520  //Configure MII1_RX_CLK (GPIO3_10)
521  CONTROL_CONF_MII1_RXCLK_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
522  //Configure MII1_RXD0 (GPIO2_21)
523  CONTROL_CONF_MII1_RXD0_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
524  //Configure MII1_RXD1 (GPIO2_20)
525  CONTROL_CONF_MII1_RXD1_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
526  //Configure MII1_RXD2 (GPIO2_19)
527  CONTROL_CONF_MII1_RXD2_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
528  //Configure MII1_RXD3 (GPIO2_18)
529  CONTROL_CONF_MII1_RXD3_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
530  //Configure MII1_COL (GPIO3_0)
531  CONTROL_CONF_MII1_COL_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
532  //Configure MII1_CRS (GPIO3_1)
533  CONTROL_CONF_MII1_CRS_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
534  //Configure MII1_RX_ER (GPIO3_2)
535  CONTROL_CONF_MII1_RXERR_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
536  //Configure MII1_RX_DV (GPIO3_4)
537  CONTROL_CONF_MII1_RXDV_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
538 
539  //Configure MDIO (GPIO0_0)
540  CONTROL_CONF_MDIO_DATA_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_PULLUPSEL |
541  CONTROL_CONF_MUXMODE(0);
542 
543  //Configure MDC (GPIO0_1)
544  CONTROL_CONF_MDIO_CLK_R = CONTROL_CONF_PULLUPSEL | CONTROL_CONF_MUXMODE(0);
545 
546 //TMDSSK3358 board?
547 #elif defined(USE_TMDSSK3358)
548  //Select RGMII interface mode for both port 1 and port 2
551 
552  //Configure RGMII1_TCLK (GPIO3_9)
553  CONTROL_CONF_MII1_TXCLK_R = CONTROL_CONF_MUXMODE(2);
554  //Configure RGMII1_TCTL (GPIO3_3)
555  CONTROL_CONF_MII1_TXEN_R = CONTROL_CONF_MUXMODE(2);
556  //Configure RGMII1_TD0 (GPIO0_28)
557  CONTROL_CONF_MII1_TXD0_R = CONTROL_CONF_MUXMODE(2);
558  //Configure RGMII1_TD1 (GPIO0_21)
559  CONTROL_CONF_MII1_TXD1_R = CONTROL_CONF_MUXMODE(2);
560  //Configure RGMII1_TD2 (GPIO0_17)
561  CONTROL_CONF_MII1_TXD2_R = CONTROL_CONF_MUXMODE(2);
562  //Configure RGMII1_TD3 (GPIO0_16)
563  CONTROL_CONF_MII1_TXD3_R = CONTROL_CONF_MUXMODE(2);
564 
565  //Configure RGMII1_RCLK (GPIO3_10)
566  CONTROL_CONF_MII1_RXCLK_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
567  //Configure RGMII1_RCTL (GPIO3_4)
568  CONTROL_CONF_MII1_RXDV_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
569  //Configure RGMII1_RD0 (GPIO2_21)
570  CONTROL_CONF_MII1_RXD0_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
571  //Configure /RGMII1_RD1 (GPIO2_20)
572  CONTROL_CONF_MII1_RXD1_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
573  //Configure RGMII1_RD2 (GPIO2_19)
574  CONTROL_CONF_MII1_RXD2_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
575  //Configure RGMII1_RD3 (GPIO2_18)
576  CONTROL_CONF_MII1_RXD3_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
577 
578  //Configure RGMII2_TCLK (GPIO1_22/GPMC_A6)
579  CONTROL_CONF_GPMC_A_R(6) = CONTROL_CONF_MUXMODE(2);
580  //Configure RGMII2_TCTL (GPIO1_16/GPMC_A0)
581  CONTROL_CONF_GPMC_A_R(0) = CONTROL_CONF_MUXMODE(2);
582  //Configure RGMII2_TD0 (GPIO1_21/GPMC_A5)
583  CONTROL_CONF_GPMC_A_R(5) = CONTROL_CONF_MUXMODE(2);
584  //Configure RGMII2_TD1 (GPIO1_20/GPMC_A4)
585  CONTROL_CONF_GPMC_A_R(4) = CONTROL_CONF_MUXMODE(2);
586  //Configure RGMII2_TD2 (GPIO1_19/GPMC_A3)
587  CONTROL_CONF_GPMC_A_R(3) = CONTROL_CONF_MUXMODE(2);
588  //Configure RGMII2_TD3 (GPIO1_18/GPMC_A2)
589  CONTROL_CONF_GPMC_A_R(2) = CONTROL_CONF_MUXMODE(2);
590 
591  //Configure RGMII2_RCLK (GPIO1_23/GPMC_A7)
592  CONTROL_CONF_GPMC_A_R(7) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
593  //Configure RGMII2_RCTL (GPIO1_17/GPMC_A1)
594  CONTROL_CONF_GPMC_A_R(1) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
595  //Configure RGMII2_RD0 (GPIO1_27/GPMC_A11)
596  CONTROL_CONF_GPMC_A_R(11) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
597  //Configure RGMII2_RD1 (GPIO1_26/GPMC_A10)
598  CONTROL_CONF_GPMC_A_R(10) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
599  //Configure RGMII2_RD2 (GPIO1_25/GPMC_A9)
600  CONTROL_CONF_GPMC_A_R(9) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
601  //Configure RGMII2_RD3 (GPIO1_24/GPMC_A8)
602  CONTROL_CONF_GPMC_A_R(8) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
603 
604  //Configure MDIO (GPIO0_0)
605  CONTROL_CONF_MDIO_DATA_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_PULLUPSEL |
606  CONTROL_CONF_MUXMODE(0);
607 
608  //Configure MDC (GPIO0_1)
609  CONTROL_CONF_MDIO_CLK_R = CONTROL_CONF_PULLUPSEL | CONTROL_CONF_MUXMODE(0);
610 
611 //OSD3358-SM-RED board?
612 #elif defined(USE_OSD3358_SM_RED)
613  //Select RGMII interface mode for both port 1
615 
616  //Configure RGMII1_TCLK (GPIO3_9)
617  CONTROL_CONF_MII1_TXCLK_R = CONTROL_CONF_MUXMODE(2);
618  //Configure RGMII1_TCTL (GPIO3_3)
619  CONTROL_CONF_MII1_TXEN_R = CONTROL_CONF_MUXMODE(2);
620  //Configure RGMII1_TD0 (GPIO0_28)
621  CONTROL_CONF_MII1_TXD0_R = CONTROL_CONF_MUXMODE(2);
622  //Configure RGMII1_TD1 (GPIO0_21)
623  CONTROL_CONF_MII1_TXD1_R = CONTROL_CONF_MUXMODE(2);
624  //Configure RGMII1_TD2 (GPIO0_17)
625  CONTROL_CONF_MII1_TXD2_R = CONTROL_CONF_MUXMODE(2);
626  //Configure RGMII1_TD3 (GPIO0_16)
627  CONTROL_CONF_MII1_TXD3_R = CONTROL_CONF_MUXMODE(2);
628 
629  //Configure RGMII1_RCLK (GPIO3_10)
630  CONTROL_CONF_MII1_RXCLK_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
631  //Configure RGMII1_RCTL (GPIO3_4)
632  CONTROL_CONF_MII1_RXDV_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
633  //Configure RGMII1_RD0 (GPIO2_21)
634  CONTROL_CONF_MII1_RXD0_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
635  //Configure /RGMII1_RD1 (GPIO2_20)
636  CONTROL_CONF_MII1_RXD1_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
637  //Configure RGMII1_RD2 (GPIO2_19)
638  CONTROL_CONF_MII1_RXD2_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
639  //Configure RGMII1_RD3 (GPIO2_18)
640  CONTROL_CONF_MII1_RXD3_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
641 
642  //Configure MDIO (GPIO0_0)
643  CONTROL_CONF_MDIO_DATA_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_PULLUPSEL |
644  CONTROL_CONF_MUXMODE(0);
645 
646  //Configure MDC (GPIO0_1)
647  CONTROL_CONF_MDIO_CLK_R = CONTROL_CONF_PULLUPSEL | CONTROL_CONF_MUXMODE(0);
648 
649 //SBC DIVA board?
650 #elif defined(USE_SBC_DIVA)
651  //Select RMII interface mode for port 1 and RGMII interface mode for port 2
652  CONTROL_GMII_SEL_R = CONTROL_GMII_SEL_RMII1_IO_CLK_EN |
654 
655  //Configure RMII1_REF_CLK (GPIO0_29)
656  CONTROL_CONF_RMII1_REFCLK_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(0);
657 
658  //Configure RMII1_TX_EN (GPIO3_3)
659  CONTROL_CONF_MII1_TXEN_R = CONTROL_CONF_MUXMODE(1);
660  //Configure RMII1_TXD0 (GPIO0_28)
661  CONTROL_CONF_MII1_TXD0_R = CONTROL_CONF_MUXMODE(1);
662  //Configure RMII1_TXD1 (GPIO0_21)
663  CONTROL_CONF_MII1_TXD1_R = CONTROL_CONF_MUXMODE(1);
664 
665  //Configure RMII1_RXD0 (GPIO2.21)
666  CONTROL_CONF_MII1_RXD0_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(1);
667  //Configure RMII1_RXD1 (GPIO2.20)
668  CONTROL_CONF_MII1_RXD1_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(1);
669  //Configure RMII1_CRS_DV (GPIO3_1)
670  CONTROL_CONF_MII1_CRS_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(1);
671  //Configure RMII1_RX_ER (GPIO3_2)
672  CONTROL_CONF_MII1_RXERR_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(1);
673 
674  //Configure RGMII2_TCLK (GPIO1_22/GPMC_A6)
675  CONTROL_CONF_GPMC_A_R(6) = CONTROL_CONF_MUXMODE(2);
676  //Configure RGMII2_TCTL (GPIO1_16/GPMC_A0)
677  CONTROL_CONF_GPMC_A_R(0) = CONTROL_CONF_MUXMODE(2);
678  //Configure RGMII2_TD0 (GPIO1_21/GPMC_A5)
679  CONTROL_CONF_GPMC_A_R(5) = CONTROL_CONF_MUXMODE(2);
680  //Configure RGMII2_TD1 (GPIO1_20/GPMC_A4)
681  CONTROL_CONF_GPMC_A_R(4) = CONTROL_CONF_MUXMODE(2);
682  //Configure RGMII2_TD2 (GPIO1_19/GPMC_A3)
683  CONTROL_CONF_GPMC_A_R(3) = CONTROL_CONF_MUXMODE(2);
684  //Configure RGMII2_TD3 (GPIO1_18/GPMC_A2)
685  CONTROL_CONF_GPMC_A_R(2) = CONTROL_CONF_MUXMODE(2);
686 
687  //Configure RGMII2_RCLK (GPIO1_23/GPMC_A7)
688  CONTROL_CONF_GPMC_A_R(7) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
689  //Configure RGMII2_RCTL (GPIO1_17/GPMC_A1)
690  CONTROL_CONF_GPMC_A_R(1) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
691  //Configure RGMII2_RD0 (GPIO1_27/GPMC_A11)
692  CONTROL_CONF_GPMC_A_R(11) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
693  //Configure RGMII2_RD1 (GPIO1_26/GPMC_A10)
694  CONTROL_CONF_GPMC_A_R(10) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
695  //Configure RGMII2_RD2 (GPIO1_25/GPMC_A9)
696  CONTROL_CONF_GPMC_A_R(9) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
697  //Configure RGMII2_RD3 (GPIO1_24/GPMC_A8)
698  CONTROL_CONF_GPMC_A_R(8) = CONTROL_CONF_RXACTIVE | CONTROL_CONF_MUXMODE(2);
699 
700  //Configure MDIO (GPIO0_0)
701  CONTROL_CONF_MDIO_DATA_R = CONTROL_CONF_RXACTIVE | CONTROL_CONF_PULLUPSEL |
702  CONTROL_CONF_MUXMODE(0);
703 
704  //Configure MDC (GPIO0_1)
705  CONTROL_CONF_MDIO_CLK_R = CONTROL_CONF_PULLUPSEL | CONTROL_CONF_MUXMODE(0);
706 #endif
707 }
708 
709 
710 /**
711  * @brief Initialize buffer descriptor lists
712  * @param[in] interface Underlying network interface
713  **/
714 
716 {
717  uint_t i;
718  uint_t nextIndex;
719  uint_t prevIndex;
720 
721  //Initialize TX buffer descriptor list (port 1)
722  for(i = 0; i < AM335X_ETH_TX_BUFFER_COUNT; i++)
723  {
724  //Index of the next buffer
725  nextIndex = (i + 1) % AM335X_ETH_TX_BUFFER_COUNT;
726  //Index of the previous buffer
728 
729  //Next descriptor pointer
730  txBufferDesc1[i].word0 = (uint32_t) NULL;
731  //Buffer pointer
732  txBufferDesc1[i].word1 = (uint32_t) txBuffer1[i];
733  //Buffer offset and buffer length
734  txBufferDesc1[i].word2 = 0;
735  //Status flags and packet length
736  txBufferDesc1[i].word3 = 0;
737 
738  //Form a doubly linked list
739  txBufferDesc1[i].next = &txBufferDesc1[nextIndex];
740  txBufferDesc1[i].prev = &txBufferDesc1[prevIndex];
741  }
742 
743  //Point to the very first descriptor
744  txCurBufferDesc1 = &txBufferDesc1[0];
745 
746  //Mark the end of the queue
747  txCurBufferDesc1->prev->word3 = CPSW_TX_WORD3_SOP |
749 
750  //Initialize TX buffer descriptor list (port 2)
751  for(i = 0; i < AM335X_ETH_TX_BUFFER_COUNT; i++)
752  {
753  //Index of the next buffer
754  nextIndex = (i + 1) % AM335X_ETH_TX_BUFFER_COUNT;
755  //Index of the previous buffer
757 
758  //Next descriptor pointer
759  txBufferDesc2[i].word0 = (uint32_t) NULL;
760  //Buffer pointer
761  txBufferDesc2[i].word1 = (uint32_t) txBuffer2[i];
762  //Buffer offset and buffer length
763  txBufferDesc2[i].word2 = 0;
764  //Status flags and packet length
765  txBufferDesc2[i].word3 = 0;
766 
767  //Form a doubly linked list
768  txBufferDesc2[i].next = &txBufferDesc2[nextIndex];
769  txBufferDesc2[i].prev = &txBufferDesc2[prevIndex];
770  }
771 
772  //Point to the very first descriptor
773  txCurBufferDesc2 = &txBufferDesc2[0];
774 
775  //Mark the end of the queue
776  txCurBufferDesc2->prev->word3 = CPSW_TX_WORD3_SOP |
778 
779  //Initialize RX buffer descriptor list
780  for(i = 0; i < AM335X_ETH_RX_BUFFER_COUNT; i++)
781  {
782  //Index of the next buffer
783  nextIndex = (i + 1) % AM335X_ETH_RX_BUFFER_COUNT;
784  //Index of the previous buffer
786 
787  //Next descriptor pointer
788  rxBufferDesc[i].word0 = (uint32_t) &rxBufferDesc[nextIndex];
789  //Buffer pointer
790  rxBufferDesc[i].word1 = (uint32_t) rxBuffer[i];
791  //Buffer offset and buffer length
792  rxBufferDesc[i].word2 = AM335X_ETH_RX_BUFFER_SIZE;
793  //Status flags and packet length
794  rxBufferDesc[i].word3 = CPSW_RX_WORD3_OWNER;
795 
796  //Form a doubly linked list
797  rxBufferDesc[i].next = &rxBufferDesc[nextIndex];
798  rxBufferDesc[i].prev = &rxBufferDesc[prevIndex];
799  }
800 
801  //Point to the very first descriptor
802  rxCurBufferDesc = &rxBufferDesc[0];
803 
804  //Mark the end of the queue
805  rxCurBufferDesc->prev->word0 = (uint32_t) NULL;
806 
807  //Write the RX DMA head descriptor pointer
808  CPSW_CPDMA_STATERAM_RX_HDP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
809 }
810 
811 
812 /**
813  * @brief AM335x Ethernet MAC timer handler
814  *
815  * This routine is periodically called by the TCP/IP stack to handle periodic
816  * operations such as polling the link state
817  *
818  * @param[in] interface Underlying network interface
819  **/
820 
821 void am335xEthTick(NetInterface *interface)
822 {
823  //Valid Ethernet PHY or switch driver?
824  if(interface->phyDriver != NULL)
825  {
826  //Handle periodic operations
827  interface->phyDriver->tick(interface);
828  }
829  else if(interface->switchDriver != NULL)
830  {
831  //Handle periodic operations
832  interface->switchDriver->tick(interface);
833  }
834  else
835  {
836  //Just for sanity
837  }
838 
839  //Misqueued buffer condition?
840  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_OWNER) != 0)
841  {
843  {
844  //The host acts on the misqueued buffer condition by writing the added
845  //buffer descriptor address to the appropriate RX DMA head descriptor
846  //pointer
847  CPSW_CPDMA_STATERAM_RX_HDP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
848  }
849  }
850 }
851 
852 
853 /**
854  * @brief Enable interrupts
855  * @param[in] interface Underlying network interface
856  **/
857 
859 {
860 #ifdef ti_sysbios_BIOS___VERS
861  //Enable Ethernet MAC interrupts
862  Hwi_enableInterrupt(SYS_INT_3PGSWTXINT0);
863  Hwi_enableInterrupt(SYS_INT_3PGSWRXINT0);
864 #else
865  //Enable Ethernet MAC interrupts
866  IntSystemEnable(SYS_INT_3PGSWTXINT0);
867  IntSystemEnable(SYS_INT_3PGSWRXINT0);
868 #endif
869 
870 
871  //Valid Ethernet PHY or switch driver?
872  if(interface->phyDriver != NULL)
873  {
874  //Enable Ethernet PHY interrupts
875  interface->phyDriver->enableIrq(interface);
876  }
877  else if(interface->switchDriver != NULL)
878  {
879  //Enable Ethernet switch interrupts
880  interface->switchDriver->enableIrq(interface);
881  }
882  else
883  {
884  //Just for sanity
885  }
886 }
887 
888 
889 /**
890  * @brief Disable interrupts
891  * @param[in] interface Underlying network interface
892  **/
893 
895 {
896 #ifdef ti_sysbios_BIOS___VERS
897  //Disable Ethernet MAC interrupts
898  Hwi_disableInterrupt(SYS_INT_3PGSWTXINT0);
899  Hwi_disableInterrupt(SYS_INT_3PGSWRXINT0);
900 #else
901  //Disable Ethernet MAC interrupts
902  IntSystemDisable(SYS_INT_3PGSWTXINT0);
903  IntSystemDisable(SYS_INT_3PGSWRXINT0);
904 #endif
905 
906 
907  //Valid Ethernet PHY or switch driver?
908  if(interface->phyDriver != NULL)
909  {
910  //Disable Ethernet PHY interrupts
911  interface->phyDriver->disableIrq(interface);
912  }
913  else if(interface->switchDriver != NULL)
914  {
915  //Disable Ethernet switch interrupts
916  interface->switchDriver->disableIrq(interface);
917  }
918  else
919  {
920  //Just for sanity
921  }
922 }
923 
924 
925 /**
926  * @brief Ethernet MAC transmit interrupt
927  **/
928 
930 {
931  bool_t flag;
932  uint32_t status;
933  uint32_t temp;
935 
936  //Interrupt service routine prologue
937  osEnterIsr();
938 
939  //This flag will be set if a higher priority task must be woken
940  flag = FALSE;
941 
942  //Read the TX_STAT register to determine which channels caused the interrupt
944 
945  //Packet transmitted on channel 1?
946  if(status & (1 << CPSW_CH1))
947  {
948  //Point to the buffer descriptor
950 
951  //Read the status flags
952  temp = p->word3 & (CPSW_TX_WORD3_SOP | CPSW_TX_WORD3_EOP |
954 
955  //Misqueued buffer condition?
957  {
958  //Check whether the next descriptor pointer is non-zero
959  if(p->word0 != 0)
960  {
961  //The host corrects the misqueued buffer condition by writing the
962  //misqueued packet’s buffer descriptor address to the appropriate
963  //TX DMA head descriptor pointer
964  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH1) = (uint32_t) p->word0;
965  }
966  }
967 
968  //Write the TX completion pointer
969  CPSW_CPDMA_STATERAM_TX_CP_R(CPSW_CH1) = (uint32_t) p;
970 
971  //Check whether the TX buffer is available for writing
972  if((txCurBufferDesc1->word3 & CPSW_TX_WORD3_OWNER) == 0)
973  {
974  //Notify the TCP/IP stack that the transmitter is ready to send
975  flag |= osSetEventFromIsr(&nicDriverInterface1->nicTxEvent);
976  }
977  }
978 
979  //Packet transmitted on channel 2?
980  if(status & (1 << CPSW_CH2))
981  {
982  //Point to the buffer descriptor
984 
985  //Read the status flags
986  temp = p->word3 & (CPSW_TX_WORD3_SOP | CPSW_TX_WORD3_EOP |
988 
989  //Misqueued buffer condition?
991  {
992  //Check whether the next descriptor pointer is non-zero
993  if(p->word0 != 0)
994  {
995  //The host corrects the misqueued buffer condition by writing the
996  //misqueued packet’s buffer descriptor address to the appropriate
997  //TX DMA head descriptor pointer
998  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH2) = (uint32_t) p->word0;
999  }
1000  }
1001 
1002  //Write the TX completion pointer
1003  CPSW_CPDMA_STATERAM_TX_CP_R(CPSW_CH2) = (uint32_t) p;
1004 
1005  //Check whether the TX buffer is available for writing
1006  if((txCurBufferDesc2->word3 & CPSW_TX_WORD3_OWNER) == 0)
1007  {
1008  //Notify the TCP/IP stack that the transmitter is ready to send
1009  flag |= osSetEventFromIsr(&nicDriverInterface2->nicTxEvent);
1010  }
1011  }
1012 
1013  //Write the DMA end of interrupt vector
1015 
1016  //Interrupt service routine epilogue
1017  osExitIsr(flag);
1018 }
1019 
1020 
1021 /**
1022  * @brief Ethernet MAC receive interrupt
1023  **/
1024 
1026 {
1027  bool_t flag;
1028  uint32_t status;
1029 
1030  //Interrupt service routine prologue
1031  osEnterIsr();
1032 
1033  //This flag will be set if a higher priority task must be woken
1034  flag = FALSE;
1035 
1036  //Read the RX_STAT register to determine which channels caused the interrupt
1037  status = CPSW_WR_C_RX_STAT_R(CPSW_CORE0);
1038 
1039  //Packet received on channel 0?
1040  if(status & (1 << CPSW_CH0))
1041  {
1042  //Disable RX interrupts
1044 
1045  //Notify the TCP/IP stack of the event
1046  if(nicDriverInterface1 != NULL)
1047  {
1048  nicDriverInterface1->nicEvent = TRUE;
1049  flag |= osSetEventFromIsr(&nicDriverInterface1->netContext->event);
1050  }
1051  else if(nicDriverInterface2 != NULL)
1052  {
1053  nicDriverInterface2->nicEvent = TRUE;
1054  flag |= osSetEventFromIsr(&nicDriverInterface2->netContext->event);
1055  }
1056  }
1057 
1058  //Write the DMA end of interrupt vector
1060 
1061  //Interrupt service routine epilogue
1062  osExitIsr(flag);
1063 }
1064 
1065 
1066 /**
1067  * @brief AM335x Ethernet MAC event handler
1068  * @param[in] interface Underlying network interface
1069  **/
1070 
1072 {
1073  static uint32_t buffer[AM335X_ETH_RX_BUFFER_SIZE / 4];
1074  error_t error;
1075  size_t n;
1076  uint32_t temp;
1077 
1078  //Process all pending packets
1079  do
1080  {
1081  //Current buffer available for reading?
1082  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_OWNER) == 0)
1083  {
1084  //SOP and EOP flags should be set
1085  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_SOP) != 0 &&
1086  (rxCurBufferDesc->word3 & CPSW_RX_WORD3_EOP) != 0)
1087  {
1088  //Make sure no error occurred
1089  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_PKT_ERROR) == 0)
1090  {
1091  //Check the port on which the packet was received
1092  switch(rxCurBufferDesc->word3 & CPSW_RX_WORD3_FROM_PORT)
1093  {
1094  //Port 1?
1096  interface = nicDriverInterface1;
1097  break;
1098  //Port 1?
1100  interface = nicDriverInterface2;
1101  break;
1102  //Invalid port number?
1103  default:
1104  interface = NULL;
1105  break;
1106  }
1107 
1108  //Retrieve the length of the frame
1109  n = rxCurBufferDesc->word3 & CPSW_RX_WORD3_PACKET_LENGTH;
1110  //Limit the number of data to read
1112 
1113  //Sanity check
1114  if(interface != NULL)
1115  {
1116  //Copy data from the receive buffer
1117  osMemcpy(buffer, (uint8_t *) rxCurBufferDesc->word1, (n + 3) & ~3UL);
1118 
1119  //Packet successfully received
1120  error = NO_ERROR;
1121  }
1122  else
1123  {
1124  //The port number is invalid
1125  error = ERROR_INVALID_PACKET;
1126  }
1127  }
1128  else
1129  {
1130  //The received packet contains an error
1131  error = ERROR_INVALID_PACKET;
1132  }
1133  }
1134  else
1135  {
1136  //The packet is not valid
1137  error = ERROR_INVALID_PACKET;
1138  }
1139 
1140  //Mark the end of the queue with a NULL pointer
1141  rxCurBufferDesc->word0 = (uint32_t) NULL;
1142  //Restore the length of the buffer
1143  rxCurBufferDesc->word2 = AM335X_ETH_RX_BUFFER_SIZE;
1144  //Give the ownership of the descriptor back to the DMA
1145  rxCurBufferDesc->word3 = CPSW_RX_WORD3_OWNER;
1146 
1147  //Link the current descriptor to the previous descriptor
1148  rxCurBufferDesc->prev->word0 = (uint32_t) rxCurBufferDesc;
1149 
1150  //Read the status flags of the previous descriptor
1151  temp = rxCurBufferDesc->prev->word3 & (CPSW_RX_WORD3_SOP |
1153 
1154  //Misqueued buffer condition?
1156  {
1157  //The host acts on the misqueued buffer condition by writing the added
1158  //buffer descriptor address to the appropriate RX DMA head descriptor
1159  //pointer
1160  CPSW_CPDMA_STATERAM_RX_HDP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
1161  }
1162 
1163  //Write the RX completion pointer
1164  CPSW_CPDMA_STATERAM_RX_CP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
1165 
1166  //Point to the next descriptor in the list
1167  rxCurBufferDesc = rxCurBufferDesc->next;
1168  }
1169  else
1170  {
1171  //No more data in the receive buffer
1172  error = ERROR_BUFFER_EMPTY;
1173  }
1174 
1175  //Check whether a valid packet has been received
1176  if(!error)
1177  {
1178  NetRxAncillary ancillary;
1179 
1180  //Additional options can be passed to the stack along with the packet
1181  ancillary = NET_DEFAULT_RX_ANCILLARY;
1182 
1183  //Pass the packet to the upper layer
1184  nicProcessPacket(interface, (uint8_t *) buffer, n, &ancillary);
1185  }
1186 
1187  //No more data in the receive buffer?
1188  } while(error != ERROR_BUFFER_EMPTY);
1189 
1190  //Re-enable RX interrupts
1192 }
1193 
1194 
1195 /**
1196  * @brief Send a packet (port 1)
1197  * @param[in] interface Underlying network interface
1198  * @param[in] buffer Multi-part buffer containing the data to send
1199  * @param[in] offset Offset to the first data byte
1200  * @param[in] ancillary Additional options passed to the stack along with
1201  * the packet
1202  * @return Error code
1203  **/
1204 
1206  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
1207 {
1208  static uint32_t temp[AM335X_ETH_TX_BUFFER_SIZE / 4];
1209  size_t length;
1210  uint32_t value;
1211 
1212  //Retrieve the length of the packet
1213  length = netBufferGetLength(buffer) - offset;
1214 
1215  //Check the frame length
1217  {
1218  //The transmitter can accept another packet
1219  osSetEvent(&interface->nicTxEvent);
1220  //Report an error
1221  return ERROR_INVALID_LENGTH;
1222  }
1223 
1224  //Make sure the current buffer is available for writing
1225  if((txCurBufferDesc1->word3 & CPSW_TX_WORD3_OWNER) != 0)
1226  {
1227  return ERROR_FAILURE;
1228  }
1229 
1230  //Mark the end of the queue with a NULL pointer
1231  txCurBufferDesc1->word0 = (uint32_t) NULL;
1232 
1233  //Copy user data to the transmit buffer
1234  netBufferRead(temp, buffer, offset, length);
1235  osMemcpy((uint8_t *) txCurBufferDesc1->word1, temp, (length + 3) & ~3UL);
1236 
1237  //Set the length of the buffer
1238  txCurBufferDesc1->word2 = length & CPSW_TX_WORD2_BUFFER_LENGTH;
1239 
1240  //Set the length of the packet
1242  //Set SOP and EOP flags as the data fits in a single buffer
1244  //Redirect the packet to the relevant port number
1246 
1247  //Give the ownership of the descriptor to the DMA
1248  txCurBufferDesc1->word3 = CPSW_TX_WORD3_OWNER | value;
1249 
1250  //Link the current descriptor to the previous descriptor
1251  txCurBufferDesc1->prev->word0 = (uint32_t) txCurBufferDesc1;
1252 
1253  //Read the status flags of the previous descriptor
1254  value = txCurBufferDesc1->prev->word3 & (CPSW_TX_WORD3_SOP |
1256 
1257  //Misqueued buffer condition?
1259  {
1260  //Clear the misqueued buffer condition
1261  txCurBufferDesc1->prev->word3 = 0;
1262 
1263  //The host corrects the misqueued buffer condition by writing the
1264  //misqueued packet’s buffer descriptor address to the appropriate
1265  //TX DMA head descriptor pointer
1266  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH1) = (uint32_t) txCurBufferDesc1;
1267  }
1268 
1269  //Point to the next descriptor in the list
1270  txCurBufferDesc1 = txCurBufferDesc1->next;
1271 
1272  //Check whether the next buffer is available for writing
1273  if((txCurBufferDesc1->word3 & CPSW_TX_WORD3_OWNER) == 0)
1274  {
1275  //The transmitter can accept another packet
1276  osSetEvent(&interface->nicTxEvent);
1277  }
1278 
1279  //Data successfully written
1280  return NO_ERROR;
1281 }
1282 
1283 
1284 /**
1285  * @brief Send a packet (port 2)
1286  * @param[in] interface Underlying network interface
1287  * @param[in] buffer Multi-part buffer containing the data to send
1288  * @param[in] offset Offset to the first data byte
1289  * @param[in] ancillary Additional options passed to the stack along with
1290  * the packet
1291  * @return Error code
1292  **/
1293 
1295  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
1296 {
1297  static uint32_t temp[AM335X_ETH_TX_BUFFER_SIZE / 4];
1298  size_t length;
1299  uint32_t value;
1300 
1301  //Retrieve the length of the packet
1302  length = netBufferGetLength(buffer) - offset;
1303 
1304  //Check the frame length
1306  {
1307  //The transmitter can accept another packet
1308  osSetEvent(&interface->nicTxEvent);
1309  //Report an error
1310  return ERROR_INVALID_LENGTH;
1311  }
1312 
1313  //Make sure the current buffer is available for writing
1314  if((txCurBufferDesc2->word3 & CPSW_TX_WORD3_OWNER) != 0)
1315  {
1316  return ERROR_FAILURE;
1317  }
1318 
1319  //Mark the end of the queue with a NULL pointer
1320  txCurBufferDesc2->word0 = (uint32_t) NULL;
1321 
1322  //Copy user data to the transmit buffer
1323  netBufferRead(temp, buffer, offset, length);
1324  osMemcpy((uint8_t *) txCurBufferDesc2->word1, temp, (length + 3) & ~3UL);
1325 
1326  //Set the length of the buffer
1327  txCurBufferDesc2->word2 = length & CPSW_TX_WORD2_BUFFER_LENGTH;
1328 
1329  //Set the length of the packet
1331  //Set SOP and EOP flags as the data fits in a single buffer
1333  //Redirect the packet to the relevant port number
1335 
1336  //Give the ownership of the descriptor to the DMA
1337  txCurBufferDesc2->word3 = CPSW_TX_WORD3_OWNER | value;
1338 
1339  //Link the current descriptor to the previous descriptor
1340  txCurBufferDesc2->prev->word0 = (uint32_t) txCurBufferDesc2;
1341 
1342  //Read the status flags of the previous descriptor
1343  value = txCurBufferDesc2->prev->word3 & (CPSW_TX_WORD3_SOP |
1345 
1346  //Misqueued buffer condition?
1348  {
1349  //Clear the misqueued buffer condition
1350  txCurBufferDesc2->prev->word3 = 0;
1351 
1352  //The host corrects the misqueued buffer condition by writing the
1353  //misqueued packet’s buffer descriptor address to the appropriate
1354  //TX DMA head descriptor pointer
1355  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH2) = (uint32_t) txCurBufferDesc2;
1356  }
1357 
1358  //Point to the next descriptor in the list
1359  txCurBufferDesc2 = txCurBufferDesc2->next;
1360 
1361  //Check whether the next buffer is available for writing
1362  if((txCurBufferDesc2->word3 & CPSW_TX_WORD3_OWNER) == 0)
1363  {
1364  //The transmitter can accept another packet
1365  osSetEvent(&interface->nicTxEvent);
1366  }
1367 
1368  //Data successfully written
1369  return NO_ERROR;
1370 }
1371 
1372 
1373 /**
1374  * @brief Configure MAC address filtering
1375  * @param[in] interface Underlying network interface
1376  * @return Error code
1377  **/
1378 
1380 {
1381  uint_t i;
1382  uint_t port;
1383  MacFilterEntry *entry;
1384 
1385  //Debug message
1386  TRACE_DEBUG("Updating AM335x ALE table...\r\n");
1387 
1388  //Select the relevant port number
1389  if(interface == nicDriverInterface1)
1390  {
1391  port = CPSW_PORT1;
1392  }
1393  else if(interface == nicDriverInterface2)
1394  {
1395  port = CPSW_PORT2;
1396  }
1397  else
1398  {
1399  port = CPSW_PORT0;
1400  }
1401 
1402  //The MAC address filter contains the list of MAC addresses to accept when
1403  //receiving an Ethernet frame
1404  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
1405  {
1406  //Point to the current entry
1407  entry = &interface->macAddrFilter[i];
1408 
1409  //Check whether the ALE table should be updated for the current multicast
1410  //address
1411  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
1412  {
1413  if(entry->addFlag)
1414  {
1415  //Add VLAN/multicast address entry to the ALE table
1417  }
1418  else if(entry->deleteFlag)
1419  {
1420  //Remove VLAN/multicast address entry from the ALE table
1422  }
1423  }
1424  }
1425 
1426  //Successful processing
1427  return NO_ERROR;
1428 }
1429 
1430 
1431 /**
1432  * @brief Adjust MAC configuration parameters for proper operation
1433  * @param[in] interface Underlying network interface
1434  * @return Error code
1435  **/
1436 
1438 {
1439  uint32_t config = 0;
1440 
1441  //Read MAC control register
1442  if(interface == nicDriverInterface1)
1443  {
1444  config = CPSW_SL1_MACCTRL_R;
1445  }
1446  else if(interface == nicDriverInterface2)
1447  {
1448  config = CPSW_SL2_MACCTRL_R;
1449  }
1450 
1451  //1000BASE-T operation mode?
1452  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
1453  {
1454  config |= CPSW_SL_MACCTRL_GIG_MASK;
1455  config &= ~(CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK);
1456  }
1457  //100BASE-TX operation mode?
1458  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
1459  {
1460  config &= ~CPSW_SL_MACCTRL_GIG_MASK;
1461  config |= CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK;
1462  }
1463  //10BASE-T operation mode?
1464  else
1465  {
1466  config &= ~CPSW_SL_MACCTRL_GIG_MASK;
1467  config &= ~(CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK);
1468  }
1469 
1470  //Half-duplex or full-duplex mode?
1471  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
1472  {
1473  config |= CPSW_SL_MACCTRL_FULLDUPLEX_MASK;
1474  }
1475  else
1476  {
1477  config &= ~CPSW_SL_MACCTRL_FULLDUPLEX_MASK;
1478  }
1479 
1480  //Update MAC control register
1481  if(interface == nicDriverInterface1)
1482  {
1483  CPSW_SL1_MACCTRL_R = config;
1484  }
1485  else if(interface == nicDriverInterface2)
1486  {
1487  CPSW_SL2_MACCTRL_R = config;
1488  }
1489 
1490  //Successful processing
1491  return NO_ERROR;
1492 }
1493 
1494 
1495 /**
1496  * @brief Write PHY register
1497  * @param[in] opcode Access type (2 bits)
1498  * @param[in] phyAddr PHY address (5 bits)
1499  * @param[in] regAddr Register address (5 bits)
1500  * @param[in] data Register value
1501  **/
1502 
1503 void am335xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
1504  uint8_t regAddr, uint16_t data)
1505 {
1506  uint32_t temp;
1507 
1508  //Valid opcode?
1509  if(opcode == SMI_OPCODE_WRITE)
1510  {
1511  //Set up a write operation
1512  temp = MDIO_USERACCESS_GO_MASK | MDIO_USERACCESS_WRITE;
1513  //PHY address
1514  temp |= (phyAddr << MDIO_USERACCESS_PHYADR_SHIFT) & MDIO_USERACCESS_PHYADR_MASK;
1515  //Register address
1516  temp |= (regAddr << MDIO_USERACCESS_REGADR_SHIFT) & MDIO_USERACCESS_REGADR_MASK;
1517  //Register value
1518  temp |= data & MDIO_USERACCESS_DATA_MASK;
1519 
1520  //Start a write operation
1521  MDIO_USERACCESS_R(0) = temp;
1522  //Wait for the write to complete
1523  while((MDIO_USERACCESS_R(0) & MDIO_USERACCESS_GO_MASK) != 0)
1524  {
1525  }
1526  }
1527  else
1528  {
1529  //The MAC peripheral only supports standard Clause 22 opcodes
1530  }
1531 }
1532 
1533 
1534 /**
1535  * @brief Read PHY register
1536  * @param[in] opcode Access type (2 bits)
1537  * @param[in] phyAddr PHY address (5 bits)
1538  * @param[in] regAddr Register address (5 bits)
1539  * @return Register value
1540  **/
1541 
1542 uint16_t am335xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1543  uint8_t regAddr)
1544 {
1545  uint16_t data;
1546  uint32_t temp;
1547 
1548  //Valid opcode?
1549  if(opcode == SMI_OPCODE_READ)
1550  {
1551  //Set up a read operation
1552  temp = MDIO_USERACCESS_GO_MASK | MDIO_USERACCESS_READ;
1553  //PHY address
1554  temp |= (phyAddr << MDIO_USERACCESS_PHYADR_SHIFT) & MDIO_USERACCESS_PHYADR_MASK;
1555  //Register address
1556  temp |= (regAddr << MDIO_USERACCESS_REGADR_SHIFT) & MDIO_USERACCESS_REGADR_MASK;
1557 
1558  //Start a read operation
1559  MDIO_USERACCESS_R(0) = temp;
1560  //Wait for the read to complete
1561  while((MDIO_USERACCESS_R(0) & MDIO_USERACCESS_GO_MASK) != 0)
1562  {
1563  }
1564 
1565  //Get register value
1566  data = MDIO_USERACCESS_R(0) & MDIO_USERACCESS_DATA_MASK;
1567  }
1568  else
1569  {
1570  //The MAC peripheral only supports standard Clause 22 opcodes
1571  data = 0;
1572  }
1573 
1574  //Return the value of the PHY register
1575  return data;
1576 }
1577 
1578 
1579 /**
1580  * @brief Write an ALE table entry
1581  * @param[in] index Entry index
1582  * @param[in] entry Pointer to the ALE table entry
1583  **/
1584 
1585 void am335xEthWriteEntry(uint_t index, const Am335xAleEntry *entry)
1586 {
1587  //Copy the content of the entry to be written
1588  CPSW_ALE_TBLW_R(2) = entry->word2;
1589  CPSW_ALE_TBLW_R(1) = entry->word1;
1590  CPSW_ALE_TBLW_R(0) = entry->word0;
1591 
1592  //Write the ALE entry at the specified index
1593  CPSW_ALE_TBLCTL_R = CPSW_ALE_TBLCTL_WRITE_RDZ_MASK | index;
1594 }
1595 
1596 
1597 /**
1598  * @brief Read an ALE table entry
1599  * @param[in] index Entry index
1600  * @param[out] entry Pointer to the ALE table entry
1601  **/
1602 
1604 {
1605  //Read the ALE entry at the specified index
1606  CPSW_ALE_TBLCTL_R = index;
1607 
1608  //Copy the content of the entry
1609  entry->word2 = CPSW_ALE_TBLW_R(2);
1610  entry->word1 = CPSW_ALE_TBLW_R(1);
1611  entry->word0 = CPSW_ALE_TBLW_R(0);
1612 }
1613 
1614 
1615 /**
1616  * @brief Find a free entry in the ALE table
1617  * @return Index of the first free entry
1618  **/
1619 
1621 {
1622  uint_t index;
1623  uint32_t type;
1624  Am335xAleEntry entry;
1625 
1626  //Loop through the ALE table entries
1627  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1628  {
1629  //Read the current entry
1630  am335xEthReadEntry(index, &entry);
1631 
1632  //Retrieve the type of the ALE entry
1634 
1635  //Free entry?
1637  {
1638  //Exit immediately
1639  break;
1640  }
1641  }
1642 
1643  //Return the index of the entry
1644  return index;
1645 }
1646 
1647 
1648 /**
1649  * @brief Search the ALE table for the specified VLAN entry
1650  * @param[in] vlanId VLAN identifier
1651  * @return Index of the matching entry
1652  **/
1653 
1655 {
1656  uint_t index;
1657  uint32_t value;
1658  Am335xAleEntry entry;
1659 
1660  //Loop through the ALE table entries
1661  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1662  {
1663  //Read the current entry
1664  am335xEthReadEntry(index, &entry);
1665 
1666  //Retrieve the type of the ALE entry
1668 
1669  //Check the type of the ALE entry
1671  {
1672  //Get the VLAN identifier
1674 
1675  //Compare the VLAN identifier
1676  if(value == CPSW_ALE_WORD1_VLAN_ID(vlanId))
1677  {
1678  //Matching ALE entry found
1679  break;
1680  }
1681  }
1682  }
1683 
1684  //Return the index of the entry
1685  return index;
1686 }
1687 
1688 
1689 /**
1690  * @brief Search the ALE table for the specified VLAN/address entry
1691  * @param[in] vlanId VLAN identifier
1692  * @param[in] macAddr MAC address
1693  * @return Index of the matching entry
1694  **/
1695 
1697 {
1698  uint_t index;
1699  uint32_t value;
1700  Am335xAleEntry entry;
1701 
1702  //Loop through the ALE table entries
1703  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1704  {
1705  //Read the current entry
1706  am335xEthReadEntry(index, &entry);
1707 
1708  //Retrieve the type of the ALE entry
1710 
1711  //Check the type of the ALE entry
1713  {
1714  //Get the VLAN identifier
1716 
1717  //Compare the VLAN identifier
1718  if(value == CPSW_ALE_WORD1_VLAN_ID(vlanId))
1719  {
1720  //Compare the MAC address
1721  if(macAddr->b[0] == (uint8_t) (entry.word1 >> 8) &&
1722  macAddr->b[1] == (uint8_t) (entry.word1 >> 0) &&
1723  macAddr->b[2] == (uint8_t) (entry.word0 >> 24) &&
1724  macAddr->b[3] == (uint8_t) (entry.word0 >> 16) &&
1725  macAddr->b[4] == (uint8_t) (entry.word0 >> 8) &&
1726  macAddr->b[5] == (uint8_t) (entry.word0 >> 0))
1727  {
1728  //Matching ALE entry found
1729  break;
1730  }
1731  }
1732  }
1733  }
1734 
1735  //Return the index of the entry
1736  return index;
1737 }
1738 
1739 
1740 /**
1741  * @brief Add a VLAN entry in the ALE table
1742  * @param[in] port Port number
1743  * @param[in] vlanId VLAN identifier
1744  * @return Error code
1745  **/
1746 
1748 {
1749  error_t error;
1750  uint_t index;
1751  Am335xAleEntry entry;
1752 
1753  //Ensure that there are no duplicate address entries in the ALE table
1754  index = am335xEthFindVlanEntry(vlanId);
1755 
1756  //No matching entry found?
1757  if(index >= CPSW_ALE_MAX_ENTRIES)
1758  {
1759  //Find a free entry in the ALE table
1760  index = am335xEthFindFreeEntry();
1761  }
1762 
1763  //Sanity check
1764  if(index < CPSW_ALE_MAX_ENTRIES)
1765  {
1766  //Set up a VLAN table entry
1767  entry.word2 = 0;
1769  entry.word0 = 0;
1770 
1771  //Set VLAN identifier
1772  entry.word1 |= CPSW_ALE_WORD1_VLAN_ID(vlanId);
1773 
1774  //Force the packet VLAN tag to be removed on egress
1777 
1778  //Set VLAN member list
1779  entry.word0 |= CPSW_ALE_WORD0_VLAN_MEMBER_LIST(1 << port) |
1781 
1782  //Add a new entry to the ALE table
1783  am335xEthWriteEntry(index, &entry);
1784 
1785  //Successful processing
1786  error = NO_ERROR;
1787  }
1788  else
1789  {
1790  //The ALE table is full
1791  error = ERROR_FAILURE;
1792  }
1793 
1794  //Return status code
1795  return error;
1796 }
1797 
1798 
1799 /**
1800  * @brief Add a VLAN/address entry in the ALE table
1801  * @param[in] port Port number
1802  * @param[in] vlanId VLAN identifier
1803  * @param[in] macAddr MAC address
1804  * @return Error code
1805  **/
1806 
1808 {
1809  error_t error;
1810  uint_t index;
1811  Am335xAleEntry entry;
1812 
1813  //Ensure that there are no duplicate address entries in the ALE table
1814  index = am335xEthFindVlanAddrEntry(vlanId, macAddr);
1815 
1816  //No matching entry found?
1817  if(index >= CPSW_ALE_MAX_ENTRIES)
1818  {
1819  //Find a free entry in the ALE table
1820  index = am335xEthFindFreeEntry();
1821  }
1822 
1823  //Sanity check
1824  if(index < CPSW_ALE_MAX_ENTRIES)
1825  {
1826  //Set up a VLAN/address table entry
1827  entry.word2 = 0;
1829  entry.word0 = 0;
1830 
1831  //Multicast address?
1832  if(macIsMulticastAddr(macAddr))
1833  {
1834  //Set port mask
1835  entry.word2 |= CPSW_ALE_WORD2_SUPER |
1838 
1839  //Set multicast forward state
1841  }
1842 
1843  //Set VLAN identifier
1844  entry.word1 |= CPSW_ALE_WORD1_VLAN_ID(vlanId);
1845 
1846  //Copy the upper 16 bits of the unicast address
1847  entry.word1 |= (macAddr->b[0] << 8) | macAddr->b[1];
1848 
1849  //Copy the lower 32 bits of the unicast address
1850  entry.word0 |= (macAddr->b[2] << 24) | (macAddr->b[3] << 16) |
1851  (macAddr->b[4] << 8) | macAddr->b[5];
1852 
1853  //Add a new entry to the ALE table
1854  am335xEthWriteEntry(index, &entry);
1855 
1856  //Successful processing
1857  error = NO_ERROR;
1858  }
1859  else
1860  {
1861  //The ALE table is full
1862  error = ERROR_FAILURE;
1863  }
1864 
1865  //Return status code
1866  return error;
1867 }
1868 
1869 
1870 /**
1871  * @brief Remove a VLAN/address entry from the ALE table
1872  * @param[in] port Port number
1873  * @param[in] vlanId VLAN identifier
1874  * @param[in] macAddr MAC address
1875  * @return Error code
1876  **/
1877 
1879 {
1880  error_t error;
1881  uint_t index;
1882  Am335xAleEntry entry;
1883 
1884  //Search the ALE table for the specified VLAN/address entry
1885  index = am335xEthFindVlanAddrEntry(vlanId, macAddr);
1886 
1887  //Matching ALE entry found?
1888  if(index < CPSW_ALE_MAX_ENTRIES)
1889  {
1890  //Clear the contents of the entry
1891  entry.word2 = 0;
1892  entry.word1 = 0;
1893  entry.word0 = 0;
1894 
1895  //Update the ALE table
1896  am335xEthWriteEntry(index, &entry);
1897 
1898  //Successful processing
1899  error = NO_ERROR;
1900  }
1901  else
1902  {
1903  //Entry not found
1904  error = ERROR_NOT_FOUND;
1905  }
1906 
1907  //Return status code
1908  return error;
1909 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define CONTROL_CONF_MII1_CRS_R
#define CPSW_CPDMA_RX_CTRL_R
#define CPSW_CH2
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define CPSW_PORT0_TX_IN_CTL_R
#define CPSW_RX_WORD3_EOP
uint8_t opcode
Definition: dns_common.h:191
error_t am335xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define CPSW_PORT2_SA_HI_R
int bool_t
Definition: compiler_port.h:63
@ ERROR_NOT_FOUND
Definition: error.h:148
#define CONTROL_CONF_MDIO_DATA_R
#define CPSW_RX_WORD3_PKT_ERROR
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define CPSW_RX_WORD3_EOQ
void am335xEthInitInstance(NetInterface *interface)
Initialize CPSW instance.
#define CONTROL_CONF_MII1_RXCLK_R
#define CPSW_SL2_MACCTRL_R
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:953
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
#define CPSW_ALE_MAX_ENTRIES
#define CPSW_TX_WORD3_SOP
#define CPSW_WR_C_TX_STAT_R(n)
void am335xEthWriteEntry(uint_t index, const Am335xAleEntry *entry)
Write an ALE table entry.
#define MDIO_INPUT_CLK
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
const NicDriver am335xEthPort1Driver
AM335x Ethernet MAC driver (port1)
#define CPSW_ALE_WORD1_ENTRY_TYPE_VLAN_ADDR
uint8_t data[]
Definition: ethernet.h:224
const NicDriver am335xEthPort2Driver
AM335x Ethernet MAC driver (port2)
#define CPSW_ALE_WORD2_PORT_MASK(n)
#define CPSW_TX_WORD3_EOP
bool_t addFlag
Definition: ethernet.h:267
uint_t am335xEthFindFreeEntry(void)
Find a free entry in the ALE table.
#define CPSW_ALE_WORD2_SUPER
#define CPSW_ALE_WORD0_VLAN_MEMBER_LIST(n)
#define CM_PER_CPGMAC0_CLKCTRL_R
#define CPSW_ALE_TBLCTL_R
#define CPSW_SL1_SOFT_RESET_R
#define CPSW_CPDMA_EOI_VECTOR_R
uint8_t type
Definition: coap_common.h:176
ALE table entry.
#define CPSW_CORE0
#define CPSW_TX_WORD3_TO_PORT_EN
#define CONTROL_CONF_MII1_COL_R
#define CPSW_CPDMA_TX_CTRL_R
#define SYS_INT_3PGSWTXINT0
uint16_t am335xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define CPSW_SL2_SOFT_RESET_R
#define CPSW_CPDMA_STATERAM_RX_CP_R(n)
uint32_t word2
#define SYS_INT_3PGSWRXINT0
#define CPSW_TX_WORD3_PACKET_LENGTH
struct _Am335xRxBufferDesc * next
#define CONTROL_CONF_RMII1_REFCLK_R
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:418
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define CPSW_ALE_WORD1_ENTRY_TYPE_VLAN
error_t am335xEthInitPort2(NetInterface *interface)
AM335x Ethernet MAC initialization (port 2)
#define osExitIsr(flag)
#define CPSW_CPDMA_SOFT_RESET_R
#define CM_PER_CPSW_CLKSTCTRL_R
uint_t am335xEthFindVlanAddrEntry(uint_t vlanId, MacAddr *macAddr)
Search the ALE table for the specified VLAN/address entry.
void am335xEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define CPSW_CPDMA_EOI_VECTOR_TX_PULSE
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define CPSW_CH0
error_t am335xEthSendPacketPort2(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet (port 2)
#define CONTROL_CONF_MII1_RXD2_R
uint32_t word0
#define CPSW_CPDMA_EOI_VECTOR_RX_PULSE
#define FALSE
Definition: os_port.h:46
#define CPSW_SS_SOFT_RESET_R
#define CONTROL_CONF_MII1_TXD3_R
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define CPSW_SS_STAT_PORT_EN_R
error_t
Error codes.
Definition: error.h:43
#define AM335X_ETH_RAM_CPPI_SECTION
#define MDIO_USERACCESS_R(n)
#define CPSW_WR_C_RX_STAT_R(n)
RX buffer descriptor.
#define AM335X_ETH_TX_BUFFER_COUNT
void am335xEthRxIrqHandler(void)
Ethernet MAC receive interrupt.
#define CONTROL_GMII_SEL_GMII1_SEL_MII
#define CPSW_ALE_WORD1_VLAN_ID_MASK
__weak_func void am335xEthInitGpio(NetInterface *interface)
GPIO configuration.
#define CPSW_SL1_MACCTRL_R
#define CPSW_TX_WORD3_OWNER
#define CPSW_RX_WORD3_PACKET_LENGTH
#define CONTROL_GMII_SEL_GMII1_SEL_RGMII
#define CPSW_PORT_P_TX_IN_CTL_SEL_DUAL_MAC
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
error_t am335xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void am335xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define AM335X_ETH_RX_BUFFER_COUNT
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define CONTROL_CONF_MII1_TXCLK_R
#define CPSW_PORT2_SA_LO_R
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:141
#define NetInterface
Definition: net.h:40
#define CONTROL_CONF_MII1_TXD2_R
#define CPSW_ALE_TBLW_R(n)
MacAddr addr
MAC address.
Definition: ethernet.h:265
error_t am335xEthSendPacketPort1(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet (port 1)
#define CPSW_PORT2_VLAN_R
@ ERROR_INVALID_LENGTH
Definition: error.h:111
TX buffer descriptor.
error_t am335xEthAddVlanAddrEntry(uint_t port, uint_t vlanId, MacAddr *macAddr)
Add a VLAN/address entry in the ALE table.
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
struct _Am335xTxBufferDesc * prev
#define NetTxAncillary
Definition: net_misc.h:36
#define CONTROL_GMII_SEL_R
#define SMI_OPCODE_READ
Definition: nic.h:67
#define CPSW_ALE_WORD1_MCAST_FWD_STATE(n)
#define AM335X_ETH_RAM_SECTION
struct _Am335xRxBufferDesc * prev
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
error_t am335xEthAddVlanEntry(uint_t port, uint_t vlanId)
Add a VLAN entry in the ALE table.
#define CPSW_PORT1_VLAN_R
#define MIN(a, b)
Definition: os_port.h:63
#define CPSW_WR_SOFT_RESET_R
#define CPSW_CPDMA_STATERAM_TX_CP_R(n)
#define AM335X_ETH_TX_BUFFER_SIZE
#define rxBuffer
#define CPSW_WR_C_RX_EN_R(n)
uint32_t word1
MacAddr
Definition: ethernet.h:197
#define CPSW_TX_WORD3_EOQ
#define CPSW_CPDMA_STATERAM_RX_HDP_R(n)
uint16_t port
Definition: dns_common.h:270
#define TRACE_DEBUG(...)
Definition: debug.h:119
#define CPSW_ALE_WORD0_FORCE_UNTAG_EGRESS(n)
#define CONTROL_GMII_SEL_GMII1_SEL_RMII
#define CONTROL_CONF_MII1_RXD3_R
#define CPSW_ALE_CTRL_R
#define CPSW_RX_WORD3_SOP
#define CONTROL_CONF_MII1_RXD0_R
uint16_t regAddr
#define CONTROL_CONF_GPMC_A_R(n)
void am335xEthEnableIrq(NetInterface *interface)
Enable interrupts.
uint_t am335xEthFindVlanEntry(uint_t vlanId)
Search the ALE table for the specified VLAN entry.
void am335xEthEventHandler(NetInterface *interface)
AM335x Ethernet MAC event handler.
#define CPSW_RX_WORD3_FROM_PORT_2
#define CPSW_TX_WORD2_BUFFER_LENGTH
void am335xEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptor lists.
#define ETH_MTU
Definition: ethernet.h:116
#define CPSW_ALE_WORD1_ENTRY_TYPE_MASK
uint8_t n
#define CONTROL_MAC_ID_LO_R(n)
MAC filter table entry.
Definition: ethernet.h:264
#define CONTROL_MAC_ID_HI_R(n)
#define CPSW_CPDMA_STATERAM_TX_HDP_R(n)
#define CONTROL_CONF_MII1_TXD1_R
#define CPSW_TX_WORD3_TO_PORT_2
#define CPSW_CH7
void am335xEthReadEntry(uint_t index, Am335xAleEntry *entry)
Read an ALE table entry.
#define osEnterIsr()
#define CONTROL_CONF_MII1_TXD0_R
#define CPSW_PORT1_SA_LO_R
#define CPSW_RX_WORD3_OWNER
#define CPSW_PORT1_SA_HI_R
#define CPSW_CPDMA_TX_INTMASK_SET_R
#define CPSW_CPDMA_RX_INTMASK_SET_R
uint8_t value[]
Definition: tcp.h:376
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
#define CPSW_PORT2
#define CONTROL_CONF_MII1_TXEN_R
#define CPSW_WR_C_TX_EN_R(n)
#define CONTROL_CONF_MDIO_CLK_R
#define CPSW_PORT1
Sitara AM335x Gigabit Ethernet MAC driver.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define CPSW_ALE_WORD1_ENTRY_TYPE_FREE
#define CPSW_ALE_WORD1_VLAN_ID(n)
#define CPSW_RX_WORD3_FROM_PORT
#define CONTROL_CONF_MII1_RXD1_R
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void am335xEthTick(NetInterface *interface)
AM335x Ethernet MAC timer handler.
error_t am335xEthInitPort1(NetInterface *interface)
AM335x Ethernet MAC initialization (port 1)
bool_t deleteFlag
Definition: ethernet.h:268
#define CONTROL_GMII_SEL_GMII2_SEL_RGMII
#define CPSW_TX_WORD3_TO_PORT_1
#define AM335X_ETH_RX_BUFFER_SIZE
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
#define CPSW_RX_WORD3_FROM_PORT_1
#define CPSW_PORT0
NIC driver.
Definition: nic.h:286
#define MDIO_OUTPUT_CLK
#define CPSW_ALE_PORTCTL_R(n)
struct _Am335xTxBufferDesc * next
#define CONTROL_CONF_MII1_RXERR_R
error_t am335xEthDeleteVlanAddrEntry(uint_t port, uint_t vlanId, MacAddr *macAddr)
Remove a VLAN/address entry from the ALE table.
#define AM335X_ETH_IRQ_PRIORITY
#define CONTROL_CONF_MII1_RXDV_R
void am335xEthTxIrqHandler(void)
Ethernet MAC transmit interrupt.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define CPSW_CH1
#define MDIO_CTRL_R
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83