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-2024 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.4.4
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  //Set event flag
1046  if(nicDriverInterface1 != NULL)
1047  {
1048  nicDriverInterface1->nicEvent = TRUE;
1049  }
1050  else if(nicDriverInterface2 != NULL)
1051  {
1052  nicDriverInterface2->nicEvent = TRUE;
1053  }
1054 
1055  //Notify the TCP/IP stack of the event
1056  flag |= osSetEventFromIsr(&netEvent);
1057  }
1058 
1059  //Write the DMA end of interrupt vector
1061 
1062  //Interrupt service routine epilogue
1063  osExitIsr(flag);
1064 }
1065 
1066 
1067 /**
1068  * @brief AM335x Ethernet MAC event handler
1069  * @param[in] interface Underlying network interface
1070  **/
1071 
1073 {
1074  static uint32_t buffer[AM335X_ETH_RX_BUFFER_SIZE / 4];
1075  error_t error;
1076  size_t n;
1077  uint32_t temp;
1078 
1079  //Process all pending packets
1080  do
1081  {
1082  //Current buffer available for reading?
1083  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_OWNER) == 0)
1084  {
1085  //SOP and EOP flags should be set
1086  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_SOP) != 0 &&
1087  (rxCurBufferDesc->word3 & CPSW_RX_WORD3_EOP) != 0)
1088  {
1089  //Make sure no error occurred
1090  if((rxCurBufferDesc->word3 & CPSW_RX_WORD3_PKT_ERROR) == 0)
1091  {
1092  //Check the port on which the packet was received
1093  switch(rxCurBufferDesc->word3 & CPSW_RX_WORD3_FROM_PORT)
1094  {
1095  //Port 1?
1097  interface = nicDriverInterface1;
1098  break;
1099  //Port 1?
1101  interface = nicDriverInterface2;
1102  break;
1103  //Invalid port number?
1104  default:
1105  interface = NULL;
1106  break;
1107  }
1108 
1109  //Retrieve the length of the frame
1110  n = rxCurBufferDesc->word3 & CPSW_RX_WORD3_PACKET_LENGTH;
1111  //Limit the number of data to read
1113 
1114  //Sanity check
1115  if(interface != NULL)
1116  {
1117  //Copy data from the receive buffer
1118  osMemcpy(buffer, (uint8_t *) rxCurBufferDesc->word1, (n + 3) & ~3UL);
1119 
1120  //Packet successfully received
1121  error = NO_ERROR;
1122  }
1123  else
1124  {
1125  //The port number is invalid
1126  error = ERROR_INVALID_PACKET;
1127  }
1128  }
1129  else
1130  {
1131  //The received packet contains an error
1132  error = ERROR_INVALID_PACKET;
1133  }
1134  }
1135  else
1136  {
1137  //The packet is not valid
1138  error = ERROR_INVALID_PACKET;
1139  }
1140 
1141  //Mark the end of the queue with a NULL pointer
1142  rxCurBufferDesc->word0 = (uint32_t) NULL;
1143  //Restore the length of the buffer
1144  rxCurBufferDesc->word2 = AM335X_ETH_RX_BUFFER_SIZE;
1145  //Give the ownership of the descriptor back to the DMA
1146  rxCurBufferDesc->word3 = CPSW_RX_WORD3_OWNER;
1147 
1148  //Link the current descriptor to the previous descriptor
1149  rxCurBufferDesc->prev->word0 = (uint32_t) rxCurBufferDesc;
1150 
1151  //Read the status flags of the previous descriptor
1152  temp = rxCurBufferDesc->prev->word3 & (CPSW_RX_WORD3_SOP |
1154 
1155  //Misqueued buffer condition?
1157  {
1158  //The host acts on the misqueued buffer condition by writing the added
1159  //buffer descriptor address to the appropriate RX DMA head descriptor
1160  //pointer
1161  CPSW_CPDMA_STATERAM_RX_HDP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
1162  }
1163 
1164  //Write the RX completion pointer
1165  CPSW_CPDMA_STATERAM_RX_CP_R(CPSW_CH0) = (uint32_t) rxCurBufferDesc;
1166 
1167  //Point to the next descriptor in the list
1168  rxCurBufferDesc = rxCurBufferDesc->next;
1169  }
1170  else
1171  {
1172  //No more data in the receive buffer
1173  error = ERROR_BUFFER_EMPTY;
1174  }
1175 
1176  //Check whether a valid packet has been received
1177  if(!error)
1178  {
1179  NetRxAncillary ancillary;
1180 
1181  //Additional options can be passed to the stack along with the packet
1182  ancillary = NET_DEFAULT_RX_ANCILLARY;
1183 
1184  //Pass the packet to the upper layer
1185  nicProcessPacket(interface, (uint8_t *) buffer, n, &ancillary);
1186  }
1187 
1188  //No more data in the receive buffer?
1189  } while(error != ERROR_BUFFER_EMPTY);
1190 
1191  //Re-enable RX interrupts
1193 }
1194 
1195 
1196 /**
1197  * @brief Send a packet (port 1)
1198  * @param[in] interface Underlying network interface
1199  * @param[in] buffer Multi-part buffer containing the data to send
1200  * @param[in] offset Offset to the first data byte
1201  * @param[in] ancillary Additional options passed to the stack along with
1202  * the packet
1203  * @return Error code
1204  **/
1205 
1207  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
1208 {
1209  static uint32_t temp[AM335X_ETH_TX_BUFFER_SIZE / 4];
1210  size_t length;
1211  uint32_t value;
1212 
1213  //Retrieve the length of the packet
1214  length = netBufferGetLength(buffer) - offset;
1215 
1216  //Check the frame length
1218  {
1219  //The transmitter can accept another packet
1220  osSetEvent(&interface->nicTxEvent);
1221  //Report an error
1222  return ERROR_INVALID_LENGTH;
1223  }
1224 
1225  //Make sure the current buffer is available for writing
1226  if((txCurBufferDesc1->word3 & CPSW_TX_WORD3_OWNER) != 0)
1227  {
1228  return ERROR_FAILURE;
1229  }
1230 
1231  //Mark the end of the queue with a NULL pointer
1232  txCurBufferDesc1->word0 = (uint32_t) NULL;
1233 
1234  //Copy user data to the transmit buffer
1235  netBufferRead(temp, buffer, offset, length);
1236  osMemcpy((uint8_t *) txCurBufferDesc1->word1, temp, (length + 3) & ~3UL);
1237 
1238  //Set the length of the buffer
1239  txCurBufferDesc1->word2 = length & CPSW_TX_WORD2_BUFFER_LENGTH;
1240 
1241  //Set the length of the packet
1243  //Set SOP and EOP flags as the data fits in a single buffer
1245  //Redirect the packet to the relevant port number
1247 
1248  //Give the ownership of the descriptor to the DMA
1249  txCurBufferDesc1->word3 = CPSW_TX_WORD3_OWNER | value;
1250 
1251  //Link the current descriptor to the previous descriptor
1252  txCurBufferDesc1->prev->word0 = (uint32_t) txCurBufferDesc1;
1253 
1254  //Read the status flags of the previous descriptor
1255  value = txCurBufferDesc1->prev->word3 & (CPSW_TX_WORD3_SOP |
1257 
1258  //Misqueued buffer condition?
1260  {
1261  //Clear the misqueued buffer condition
1262  txCurBufferDesc1->prev->word3 = 0;
1263 
1264  //The host corrects the misqueued buffer condition by writing the
1265  //misqueued packet’s buffer descriptor address to the appropriate
1266  //TX DMA head descriptor pointer
1267  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH1) = (uint32_t) txCurBufferDesc1;
1268  }
1269 
1270  //Point to the next descriptor in the list
1271  txCurBufferDesc1 = txCurBufferDesc1->next;
1272 
1273  //Check whether the next buffer is available for writing
1274  if((txCurBufferDesc1->word3 & CPSW_TX_WORD3_OWNER) == 0)
1275  {
1276  //The transmitter can accept another packet
1277  osSetEvent(&interface->nicTxEvent);
1278  }
1279 
1280  //Data successfully written
1281  return NO_ERROR;
1282 }
1283 
1284 
1285 /**
1286  * @brief Send a packet (port 2)
1287  * @param[in] interface Underlying network interface
1288  * @param[in] buffer Multi-part buffer containing the data to send
1289  * @param[in] offset Offset to the first data byte
1290  * @param[in] ancillary Additional options passed to the stack along with
1291  * the packet
1292  * @return Error code
1293  **/
1294 
1296  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
1297 {
1298  static uint32_t temp[AM335X_ETH_TX_BUFFER_SIZE / 4];
1299  size_t length;
1300  uint32_t value;
1301 
1302  //Retrieve the length of the packet
1303  length = netBufferGetLength(buffer) - offset;
1304 
1305  //Check the frame length
1307  {
1308  //The transmitter can accept another packet
1309  osSetEvent(&interface->nicTxEvent);
1310  //Report an error
1311  return ERROR_INVALID_LENGTH;
1312  }
1313 
1314  //Make sure the current buffer is available for writing
1315  if((txCurBufferDesc2->word3 & CPSW_TX_WORD3_OWNER) != 0)
1316  {
1317  return ERROR_FAILURE;
1318  }
1319 
1320  //Mark the end of the queue with a NULL pointer
1321  txCurBufferDesc2->word0 = (uint32_t) NULL;
1322 
1323  //Copy user data to the transmit buffer
1324  netBufferRead(temp, buffer, offset, length);
1325  osMemcpy((uint8_t *) txCurBufferDesc2->word1, temp, (length + 3) & ~3UL);
1326 
1327  //Set the length of the buffer
1328  txCurBufferDesc2->word2 = length & CPSW_TX_WORD2_BUFFER_LENGTH;
1329 
1330  //Set the length of the packet
1332  //Set SOP and EOP flags as the data fits in a single buffer
1334  //Redirect the packet to the relevant port number
1336 
1337  //Give the ownership of the descriptor to the DMA
1338  txCurBufferDesc2->word3 = CPSW_TX_WORD3_OWNER | value;
1339 
1340  //Link the current descriptor to the previous descriptor
1341  txCurBufferDesc2->prev->word0 = (uint32_t) txCurBufferDesc2;
1342 
1343  //Read the status flags of the previous descriptor
1344  value = txCurBufferDesc2->prev->word3 & (CPSW_TX_WORD3_SOP |
1346 
1347  //Misqueued buffer condition?
1349  {
1350  //Clear the misqueued buffer condition
1351  txCurBufferDesc2->prev->word3 = 0;
1352 
1353  //The host corrects the misqueued buffer condition by writing the
1354  //misqueued packet’s buffer descriptor address to the appropriate
1355  //TX DMA head descriptor pointer
1356  CPSW_CPDMA_STATERAM_TX_HDP_R(CPSW_CH2) = (uint32_t) txCurBufferDesc2;
1357  }
1358 
1359  //Point to the next descriptor in the list
1360  txCurBufferDesc2 = txCurBufferDesc2->next;
1361 
1362  //Check whether the next buffer is available for writing
1363  if((txCurBufferDesc2->word3 & CPSW_TX_WORD3_OWNER) == 0)
1364  {
1365  //The transmitter can accept another packet
1366  osSetEvent(&interface->nicTxEvent);
1367  }
1368 
1369  //Data successfully written
1370  return NO_ERROR;
1371 }
1372 
1373 
1374 /**
1375  * @brief Configure MAC address filtering
1376  * @param[in] interface Underlying network interface
1377  * @return Error code
1378  **/
1379 
1381 {
1382  uint_t i;
1383  uint_t port;
1384  MacFilterEntry *entry;
1385 
1386  //Debug message
1387  TRACE_DEBUG("Updating AM335x ALE table...\r\n");
1388 
1389  //Select the relevant port number
1390  if(interface == nicDriverInterface1)
1391  {
1392  port = CPSW_PORT1;
1393  }
1394  else if(interface == nicDriverInterface2)
1395  {
1396  port = CPSW_PORT2;
1397  }
1398  else
1399  {
1400  port = CPSW_PORT0;
1401  }
1402 
1403  //The MAC address filter contains the list of MAC addresses to accept when
1404  //receiving an Ethernet frame
1405  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
1406  {
1407  //Point to the current entry
1408  entry = &interface->macAddrFilter[i];
1409 
1410  //Check whether the ALE table should be updated for the current multicast
1411  //address
1412  if(!macCompAddr(&entry->addr, &MAC_UNSPECIFIED_ADDR))
1413  {
1414  if(entry->addFlag)
1415  {
1416  //Add VLAN/multicast address entry to the ALE table
1418  }
1419  else if(entry->deleteFlag)
1420  {
1421  //Remove VLAN/multicast address entry from the ALE table
1423  }
1424  }
1425  }
1426 
1427  //Successful processing
1428  return NO_ERROR;
1429 }
1430 
1431 
1432 /**
1433  * @brief Adjust MAC configuration parameters for proper operation
1434  * @param[in] interface Underlying network interface
1435  * @return Error code
1436  **/
1437 
1439 {
1440  uint32_t config = 0;
1441 
1442  //Read MAC control register
1443  if(interface == nicDriverInterface1)
1444  {
1445  config = CPSW_SL1_MACCTRL_R;
1446  }
1447  else if(interface == nicDriverInterface2)
1448  {
1449  config = CPSW_SL2_MACCTRL_R;
1450  }
1451 
1452  //1000BASE-T operation mode?
1453  if(interface->linkSpeed == NIC_LINK_SPEED_1GBPS)
1454  {
1455  config |= CPSW_SL_MACCTRL_GIG_MASK;
1456  config &= ~(CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK);
1457  }
1458  //100BASE-TX operation mode?
1459  else if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
1460  {
1461  config &= ~CPSW_SL_MACCTRL_GIG_MASK;
1462  config |= CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK;
1463  }
1464  //10BASE-T operation mode?
1465  else
1466  {
1467  config &= ~CPSW_SL_MACCTRL_GIG_MASK;
1468  config &= ~(CPSW_SL_MACCTRL_IFCTL_A_MASK | CPSW_SL_MACCTRL_IFCTL_B_MASK);
1469  }
1470 
1471  //Half-duplex or full-duplex mode?
1472  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
1473  {
1474  config |= CPSW_SL_MACCTRL_FULLDUPLEX_MASK;
1475  }
1476  else
1477  {
1478  config &= ~CPSW_SL_MACCTRL_FULLDUPLEX_MASK;
1479  }
1480 
1481  //Update MAC control register
1482  if(interface == nicDriverInterface1)
1483  {
1484  CPSW_SL1_MACCTRL_R = config;
1485  }
1486  else if(interface == nicDriverInterface2)
1487  {
1488  CPSW_SL2_MACCTRL_R = config;
1489  }
1490 
1491  //Successful processing
1492  return NO_ERROR;
1493 }
1494 
1495 
1496 /**
1497  * @brief Write PHY register
1498  * @param[in] opcode Access type (2 bits)
1499  * @param[in] phyAddr PHY address (5 bits)
1500  * @param[in] regAddr Register address (5 bits)
1501  * @param[in] data Register value
1502  **/
1503 
1504 void am335xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
1505  uint8_t regAddr, uint16_t data)
1506 {
1507  uint32_t temp;
1508 
1509  //Valid opcode?
1510  if(opcode == SMI_OPCODE_WRITE)
1511  {
1512  //Set up a write operation
1513  temp = MDIO_USERACCESS_GO_MASK | MDIO_USERACCESS_WRITE;
1514  //PHY address
1515  temp |= (phyAddr << MDIO_USERACCESS_PHYADR_SHIFT) & MDIO_USERACCESS_PHYADR_MASK;
1516  //Register address
1517  temp |= (regAddr << MDIO_USERACCESS_REGADR_SHIFT) & MDIO_USERACCESS_REGADR_MASK;
1518  //Register value
1519  temp |= data & MDIO_USERACCESS_DATA_MASK;
1520 
1521  //Start a write operation
1522  MDIO_USERACCESS_R(0) = temp;
1523  //Wait for the write to complete
1524  while((MDIO_USERACCESS_R(0) & MDIO_USERACCESS_GO_MASK) != 0)
1525  {
1526  }
1527  }
1528  else
1529  {
1530  //The MAC peripheral only supports standard Clause 22 opcodes
1531  }
1532 }
1533 
1534 
1535 /**
1536  * @brief Read PHY register
1537  * @param[in] opcode Access type (2 bits)
1538  * @param[in] phyAddr PHY address (5 bits)
1539  * @param[in] regAddr Register address (5 bits)
1540  * @return Register value
1541  **/
1542 
1543 uint16_t am335xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1544  uint8_t regAddr)
1545 {
1546  uint16_t data;
1547  uint32_t temp;
1548 
1549  //Valid opcode?
1550  if(opcode == SMI_OPCODE_READ)
1551  {
1552  //Set up a read operation
1553  temp = MDIO_USERACCESS_GO_MASK | MDIO_USERACCESS_READ;
1554  //PHY address
1555  temp |= (phyAddr << MDIO_USERACCESS_PHYADR_SHIFT) & MDIO_USERACCESS_PHYADR_MASK;
1556  //Register address
1557  temp |= (regAddr << MDIO_USERACCESS_REGADR_SHIFT) & MDIO_USERACCESS_REGADR_MASK;
1558 
1559  //Start a read operation
1560  MDIO_USERACCESS_R(0) = temp;
1561  //Wait for the read to complete
1562  while((MDIO_USERACCESS_R(0) & MDIO_USERACCESS_GO_MASK) != 0)
1563  {
1564  }
1565 
1566  //Get register value
1567  data = MDIO_USERACCESS_R(0) & MDIO_USERACCESS_DATA_MASK;
1568  }
1569  else
1570  {
1571  //The MAC peripheral only supports standard Clause 22 opcodes
1572  data = 0;
1573  }
1574 
1575  //Return the value of the PHY register
1576  return data;
1577 }
1578 
1579 
1580 /**
1581  * @brief Write an ALE table entry
1582  * @param[in] index Entry index
1583  * @param[in] entry Pointer to the ALE table entry
1584  **/
1585 
1586 void am335xEthWriteEntry(uint_t index, const Am335xAleEntry *entry)
1587 {
1588  //Copy the content of the entry to be written
1589  CPSW_ALE_TBLW_R(2) = entry->word2;
1590  CPSW_ALE_TBLW_R(1) = entry->word1;
1591  CPSW_ALE_TBLW_R(0) = entry->word0;
1592 
1593  //Write the ALE entry at the specified index
1594  CPSW_ALE_TBLCTL_R = CPSW_ALE_TBLCTL_WRITE_RDZ_MASK | index;
1595 }
1596 
1597 
1598 /**
1599  * @brief Read an ALE table entry
1600  * @param[in] index Entry index
1601  * @param[out] entry Pointer to the ALE table entry
1602  **/
1603 
1605 {
1606  //Read the ALE entry at the specified index
1607  CPSW_ALE_TBLCTL_R = index;
1608 
1609  //Copy the content of the entry
1610  entry->word2 = CPSW_ALE_TBLW_R(2);
1611  entry->word1 = CPSW_ALE_TBLW_R(1);
1612  entry->word0 = CPSW_ALE_TBLW_R(0);
1613 }
1614 
1615 
1616 /**
1617  * @brief Find a free entry in the ALE table
1618  * @return Index of the first free entry
1619  **/
1620 
1622 {
1623  uint_t index;
1624  uint32_t type;
1625  Am335xAleEntry entry;
1626 
1627  //Loop through the ALE table entries
1628  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1629  {
1630  //Read the current entry
1631  am335xEthReadEntry(index, &entry);
1632 
1633  //Retrieve the type of the ALE entry
1635 
1636  //Free entry?
1638  {
1639  //Exit immediately
1640  break;
1641  }
1642  }
1643 
1644  //Return the index of the entry
1645  return index;
1646 }
1647 
1648 
1649 /**
1650  * @brief Search the ALE table for the specified VLAN entry
1651  * @param[in] vlanId VLAN identifier
1652  * @return Index of the matching entry
1653  **/
1654 
1656 {
1657  uint_t index;
1658  uint32_t value;
1659  Am335xAleEntry entry;
1660 
1661  //Loop through the ALE table entries
1662  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1663  {
1664  //Read the current entry
1665  am335xEthReadEntry(index, &entry);
1666 
1667  //Retrieve the type of the ALE entry
1669 
1670  //Check the type of the ALE entry
1672  {
1673  //Get the VLAN identifier
1675 
1676  //Compare the VLAN identifier
1677  if(value == CPSW_ALE_WORD1_VLAN_ID(vlanId))
1678  {
1679  //Matching ALE entry found
1680  break;
1681  }
1682  }
1683  }
1684 
1685  //Return the index of the entry
1686  return index;
1687 }
1688 
1689 
1690 /**
1691  * @brief Search the ALE table for the specified VLAN/address entry
1692  * @param[in] vlanId VLAN identifier
1693  * @param[in] macAddr MAC address
1694  * @return Index of the matching entry
1695  **/
1696 
1698 {
1699  uint_t index;
1700  uint32_t value;
1701  Am335xAleEntry entry;
1702 
1703  //Loop through the ALE table entries
1704  for(index = 0; index < CPSW_ALE_MAX_ENTRIES; index++)
1705  {
1706  //Read the current entry
1707  am335xEthReadEntry(index, &entry);
1708 
1709  //Retrieve the type of the ALE entry
1711 
1712  //Check the type of the ALE entry
1714  {
1715  //Get the VLAN identifier
1717 
1718  //Compare the VLAN identifier
1719  if(value == CPSW_ALE_WORD1_VLAN_ID(vlanId))
1720  {
1721  //Compare the MAC address
1722  if(macAddr->b[0] == (uint8_t) (entry.word1 >> 8) &&
1723  macAddr->b[1] == (uint8_t) (entry.word1 >> 0) &&
1724  macAddr->b[2] == (uint8_t) (entry.word0 >> 24) &&
1725  macAddr->b[3] == (uint8_t) (entry.word0 >> 16) &&
1726  macAddr->b[4] == (uint8_t) (entry.word0 >> 8) &&
1727  macAddr->b[5] == (uint8_t) (entry.word0 >> 0))
1728  {
1729  //Matching ALE entry found
1730  break;
1731  }
1732  }
1733  }
1734  }
1735 
1736  //Return the index of the entry
1737  return index;
1738 }
1739 
1740 
1741 /**
1742  * @brief Add a VLAN entry in the ALE table
1743  * @param[in] port Port number
1744  * @param[in] vlanId VLAN identifier
1745  * @return Error code
1746  **/
1747 
1749 {
1750  error_t error;
1751  uint_t index;
1752  Am335xAleEntry entry;
1753 
1754  //Ensure that there are no duplicate address entries in the ALE table
1755  index = am335xEthFindVlanEntry(vlanId);
1756 
1757  //No matching entry found?
1758  if(index >= CPSW_ALE_MAX_ENTRIES)
1759  {
1760  //Find a free entry in the ALE table
1761  index = am335xEthFindFreeEntry();
1762  }
1763 
1764  //Sanity check
1765  if(index < CPSW_ALE_MAX_ENTRIES)
1766  {
1767  //Set up a VLAN table entry
1768  entry.word2 = 0;
1770  entry.word0 = 0;
1771 
1772  //Set VLAN identifier
1773  entry.word1 |= CPSW_ALE_WORD1_VLAN_ID(vlanId);
1774 
1775  //Force the packet VLAN tag to be removed on egress
1778 
1779  //Set VLAN member list
1780  entry.word0 |= CPSW_ALE_WORD0_VLAN_MEMBER_LIST(1 << port) |
1782 
1783  //Add a new entry to the ALE table
1784  am335xEthWriteEntry(index, &entry);
1785 
1786  //Successful processing
1787  error = NO_ERROR;
1788  }
1789  else
1790  {
1791  //The ALE table is full
1792  error = ERROR_FAILURE;
1793  }
1794 
1795  //Return status code
1796  return error;
1797 }
1798 
1799 
1800 /**
1801  * @brief Add a VLAN/address entry in the ALE table
1802  * @param[in] port Port number
1803  * @param[in] vlanId VLAN identifier
1804  * @param[in] macAddr MAC address
1805  * @return Error code
1806  **/
1807 
1809 {
1810  error_t error;
1811  uint_t index;
1812  Am335xAleEntry entry;
1813 
1814  //Ensure that there are no duplicate address entries in the ALE table
1815  index = am335xEthFindVlanAddrEntry(vlanId, macAddr);
1816 
1817  //No matching entry found?
1818  if(index >= CPSW_ALE_MAX_ENTRIES)
1819  {
1820  //Find a free entry in the ALE table
1821  index = am335xEthFindFreeEntry();
1822  }
1823 
1824  //Sanity check
1825  if(index < CPSW_ALE_MAX_ENTRIES)
1826  {
1827  //Set up a VLAN/address table entry
1828  entry.word2 = 0;
1830  entry.word0 = 0;
1831 
1832  //Multicast address?
1833  if(macIsMulticastAddr(macAddr))
1834  {
1835  //Set port mask
1836  entry.word2 |= CPSW_ALE_WORD2_SUPER |
1839 
1840  //Set multicast forward state
1842  }
1843 
1844  //Set VLAN identifier
1845  entry.word1 |= CPSW_ALE_WORD1_VLAN_ID(vlanId);
1846 
1847  //Copy the upper 16 bits of the unicast address
1848  entry.word1 |= (macAddr->b[0] << 8) | macAddr->b[1];
1849 
1850  //Copy the lower 32 bits of the unicast address
1851  entry.word0 |= (macAddr->b[2] << 24) | (macAddr->b[3] << 16) |
1852  (macAddr->b[4] << 8) | macAddr->b[5];
1853 
1854  //Add a new entry to the ALE table
1855  am335xEthWriteEntry(index, &entry);
1856 
1857  //Successful processing
1858  error = NO_ERROR;
1859  }
1860  else
1861  {
1862  //The ALE table is full
1863  error = ERROR_FAILURE;
1864  }
1865 
1866  //Return status code
1867  return error;
1868 }
1869 
1870 
1871 /**
1872  * @brief Remove a VLAN/address entry from the ALE table
1873  * @param[in] port Port number
1874  * @param[in] vlanId VLAN identifier
1875  * @param[in] macAddr MAC address
1876  * @return Error code
1877  **/
1878 
1880 {
1881  error_t error;
1882  uint_t index;
1883  Am335xAleEntry entry;
1884 
1885  //Search the ALE table for the specified VLAN/address entry
1886  index = am335xEthFindVlanAddrEntry(vlanId, macAddr);
1887 
1888  //Matching ALE entry found?
1889  if(index < CPSW_ALE_MAX_ENTRIES)
1890  {
1891  //Clear the contents of the entry
1892  entry.word2 = 0;
1893  entry.word1 = 0;
1894  entry.word0 = 0;
1895 
1896  //Update the ALE table
1897  am335xEthWriteEntry(index, &entry);
1898 
1899  //Successful processing
1900  error = NO_ERROR;
1901  }
1902  else
1903  {
1904  //Entry not found
1905  error = ERROR_NOT_FOUND;
1906  }
1907 
1908  //Return status code
1909  return error;
1910 }
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:188
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:53
@ ERROR_NOT_FOUND
Definition: error.h:147
#define netEvent
Definition: net_legacy.h:196
#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:946
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:222
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:265
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:392
#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:141
#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:104
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:140
#define NetInterface
Definition: net.h:36
#define CONTROL_CONF_MII1_TXD2_R
#define CPSW_ALE_TBLW_R(n)
MacAddr addr
MAC address.
Definition: ethernet.h:263
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:141
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:95
uint8_t length
Definition: tcp.h:368
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:195
#define CPSW_TX_WORD3_EOQ
#define CPSW_CPDMA_STATERAM_RX_HDP_R(n)
uint16_t port
Definition: dns_common.h:267
#define TRACE_DEBUG(...)
Definition: debug.h:107
#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:262
#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:369
#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:266
#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:50
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:53
@ 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