2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 * Basic interrupt driven driver for the EMAC peripheral. This driver is not
\r
55 * reentrant as with uIP the buffers are only ever accessed from a single task.
\r
57 * The simple buffer management used within uIP allows the EMAC driver to also
\r
58 * be simplistic. The driver contained within the lwIP demo is more
\r
66 + Corrected the byte order when writing the MAC address to the MAC.
\r
67 + Support added for MII interfaces. Previously only RMII was supported.
\r
71 + The MII interface is now the default.
\r
72 + Modified the initialisation sequence slightly to allow auto init more
\r
77 + Also read the EMAC_RSR register in the EMAC ISR as a work around the
\r
78 the EMAC bug that can reset the RX bit in EMAC_ISR register before the
\r
83 + Corrected the Rx frame length mask when obtaining the length from the
\r
88 /* Standard includes. */
\r
91 /* Scheduler includes. */
\r
92 #include "FreeRTOS.h"
\r
99 /* Hardware specific includes. */
\r
104 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0
\r
105 to use an MII interface. */
\r
106 #define USE_RMII_INTERFACE 0
\r
108 /* The buffer addresses written into the descriptors must be aligned so the
\r
109 last few bits are zero. These bits have special meaning for the EMAC
\r
110 peripheral and cannot be used as part of the address. */
\r
111 #define emacADDRESS_MASK ( ( unsigned portLONG ) 0xFFFFFFFC )
\r
113 /* Bit used within the address stored in the descriptor to mark the last
\r
114 descriptor in the array. */
\r
115 #define emacRX_WRAP_BIT ( ( unsigned portLONG ) 0x02 )
\r
117 /* Bit used within the Tx descriptor status to indicate whether the
\r
118 descriptor is under the control of the EMAC or the software. */
\r
119 #define emacTX_BUF_USED ( ( unsigned portLONG ) 0x80000000 )
\r
121 /* A short delay is used to wait for a buffer to become available, should
\r
122 one not be immediately available when trying to transmit a frame. */
\r
123 #define emacBUFFER_WAIT_DELAY ( 2 )
\r
124 #define emacMAX_WAIT_CYCLES ( configTICK_RATE_HZ / 40 )
\r
126 /* Misc defines. */
\r
127 #define emacINTERRUPT_LEVEL ( 5 )
\r
128 #define emacNO_DELAY ( 0 )
\r
129 #define emacTOTAL_FRAME_HEADER_SIZE ( 54 )
\r
130 #define emacPHY_INIT_DELAY ( 5000 / portTICK_RATE_MS )
\r
131 #define emacRESET_KEY ( ( unsigned portLONG ) 0xA5000000 )
\r
132 #define emacRESET_LENGTH ( ( unsigned portLONG ) ( 0x01 << 8 ) )
\r
134 /* The Atmel header file only defines the TX frame length mask. */
\r
135 #define emacRX_LENGTH_FRAME ( 0xfff )
\r
137 /*-----------------------------------------------------------*/
\r
140 * Prototype for the EMAC interrupt asm wrapper.
\r
142 extern void vEMACISREntry( void );
\r
145 * Prototype for the EMAC interrupt function - called by the asm wrapper.
\r
147 __arm void vEMACISR( void );
\r
150 * Initialise both the Tx and Rx descriptors used by the EMAC.
\r
152 static void prvSetupDescriptors(void);
\r
155 * Write our MAC address into the EMAC. The MAC address is set as one of the
\r
158 static void prvSetupMACAddress( void );
\r
161 * Configure the EMAC and AIC for EMAC interrupts.
\r
163 static void prvSetupEMACInterrupt( void );
\r
166 * Some initialisation functions taken from the Atmel EMAC sample code.
\r
168 static void vReadPHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG *pulValue );
\r
169 #if USE_RMII_INTERFACE != 1
\r
170 static void vWritePHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG ulValue);
\r
172 static portBASE_TYPE xGetLinkSpeed( void );
\r
173 static portBASE_TYPE prvProbePHY( void );
\r
175 /*-----------------------------------------------------------*/
\r
177 /* Buffer written to by the EMAC DMA. Must be aligned as described by the
\r
178 comment above the emacADDRESS_MASK definition. */
\r
179 #pragma data_alignment=8
\r
180 static volatile portCHAR pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ];
\r
182 /* Buffer read by the EMAC DMA. Must be aligned as described by he comment
\r
183 above the emacADDRESS_MASK definition. */
\r
184 #pragma data_alignment=8
\r
185 static portCHAR pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ];
\r
187 /* Descriptors used to communicate between the program and the EMAC peripheral.
\r
188 These descriptors hold the locations and state of the Rx and Tx buffers. */
\r
189 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];
\r
190 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];
\r
192 /* The IP and Ethernet addresses are read from the uIP setup. */
\r
193 const portCHAR cMACAddress[ 6 ] = { UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5 };
\r
194 const unsigned char ucIPAddress[ 4 ] = { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
\r
196 /* The semaphore used by the EMAC ISR to wake the EMAC task. */
\r
197 static xSemaphoreHandle xSemaphore = NULL;
\r
199 /*-----------------------------------------------------------*/
\r
201 xSemaphoreHandle xEMACInit( void )
\r
203 /* Code supplied by Atmel (modified) --------------------*/
\r
205 /* disable pull up on RXDV => PHY normal mode (not in test mode),
\r
206 PHY has internal pull down. */
\r
207 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;
\r
209 #if USE_RMII_INTERFACE != 1
\r
210 /* PHY has internal pull down : set MII mode. */
\r
211 AT91C_BASE_PIOB->PIO_PPUDR= 1 << 16;
\r
214 /* clear PB18 <=> PHY powerdown. */
\r
215 AT91F_PIO_CfgOutput( AT91C_BASE_PIOB, 1 << 18 ) ;
\r
216 AT91F_PIO_ClearOutput( AT91C_BASE_PIOB, 1 << 18) ;
\r
218 /* After PHY power up, hardware reset. */
\r
219 AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;
\r
220 AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;
\r
222 /* Wait for hardware reset end. */
\r
223 while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )
\r
229 /* EMAC IO init for EMAC-PHY com. Remove EF100 config. */
\r
230 AT91F_EMAC_CfgPIO();
\r
232 /* Enable com between EMAC PHY.
\r
234 Enable management port. */
\r
235 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
237 /* MDC = MCK/32. */
\r
238 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
240 /* Wait for PHY auto init end (rather crude delay!). */
\r
241 vTaskDelay( emacPHY_INIT_DELAY );
\r
243 /* PHY configuration. */
\r
244 #if USE_RMII_INTERFACE != 1
\r
246 unsigned portLONG ulControl;
\r
248 /* PHY has internal pull down : disable MII isolate. */
\r
249 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
250 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );
\r
251 ulControl &= ~BMCR_ISOLATE;
\r
252 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );
\r
256 /* Disable management port again. */
\r
257 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
259 #if USE_RMII_INTERFACE != 1
\r
260 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */
\r
261 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;
\r
263 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator
\r
265 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
\r
268 /* End of code supplied by Atmel ------------------------*/
\r
270 /* Setup the buffers and descriptors. */
\r
271 prvSetupDescriptors();
\r
273 /* Load our MAC address into the EMAC. */
\r
274 prvSetupMACAddress();
\r
276 /* Try to connect. */
\r
277 if( prvProbePHY() )
\r
279 /* Enable the interrupt! */
\r
280 prvSetupEMACInterrupt();
\r
285 /*-----------------------------------------------------------*/
\r
287 portLONG lEMACSend( void )
\r
289 static unsigned portBASE_TYPE uxTxBufferIndex = 0;
\r
290 portBASE_TYPE xWaitCycles = 0;
\r
291 portLONG lReturn = pdPASS;
\r
292 portCHAR *pcBuffer;
\r
294 /* Is a buffer available? */
\r
295 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )
\r
297 /* There is no room to write the Tx data to the Tx buffer. Wait a
\r
298 short while, then try again. */
\r
300 if( xWaitCycles > emacMAX_WAIT_CYCLES )
\r
308 vTaskDelay( emacBUFFER_WAIT_DELAY );
\r
312 /* lReturn will only be pdPASS if a buffer is available. */
\r
313 if( lReturn == pdPASS )
\r
315 /* Copy the headers into the Tx buffer. These will be in the uIP buffer. */
\r
316 pcBuffer = ( portCHAR * ) xTxDescriptors[ uxTxBufferIndex ].addr;
\r
317 memcpy( ( void * ) pcBuffer, ( void * ) uip_buf, emacTOTAL_FRAME_HEADER_SIZE );
\r
318 if( uip_len > emacTOTAL_FRAME_HEADER_SIZE )
\r
320 memcpy( ( void * ) &( pcBuffer[ emacTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - emacTOTAL_FRAME_HEADER_SIZE ) );
\r
324 portENTER_CRITICAL();
\r
326 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )
\r
328 /* Fill out the necessary in the descriptor to get the data sent. */
\r
329 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( uip_len & ( unsigned portLONG ) AT91C_LENGTH_FRAME )
\r
330 | AT91C_LAST_BUFFER
\r
331 | AT91C_TRANSMIT_WRAP;
\r
332 uxTxBufferIndex = 0;
\r
336 /* Fill out the necessary in the descriptor to get the data sent. */
\r
337 xTxDescriptors[ uxTxBufferIndex ].U_Status.status = ( uip_len & ( unsigned portLONG ) AT91C_LENGTH_FRAME )
\r
338 | AT91C_LAST_BUFFER;
\r
342 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
\r
344 portEXIT_CRITICAL();
\r
349 /*-----------------------------------------------------------*/
\r
351 unsigned portLONG ulEMACPoll( void )
\r
353 static unsigned portBASE_TYPE ulNextRxBuffer = 0;
\r
354 unsigned portLONG ulSectionLength = 0, ulLengthSoFar = 0, ulEOF = pdFALSE;
\r
355 portCHAR *pcSource;
\r
357 /* Skip any fragments. */
\r
358 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )
\r
360 /* Mark the buffer as free again. */
\r
361 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
363 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
365 ulNextRxBuffer = 0;
\r
369 /* Is there a packet ready? */
\r
371 while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !ulSectionLength )
\r
373 pcSource = ( portCHAR * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );
\r
374 ulSectionLength = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & emacRX_LENGTH_FRAME;
\r
376 if( ulSectionLength == 0 )
\r
378 /* The frame is longer than the buffer pointed to by this
\r
379 descriptor so copy the entire buffer to uIP - then move onto
\r
380 the next descriptor to get the rest of the frame. */
\r
381 if( ( ulLengthSoFar + ETH_RX_BUFFER_SIZE ) <= UIP_BUFSIZE )
\r
383 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ETH_RX_BUFFER_SIZE );
\r
384 ulLengthSoFar += ETH_RX_BUFFER_SIZE;
\r
389 /* This is the last section of the frame. Copy the section to
\r
391 if( ulSectionLength < UIP_BUFSIZE )
\r
393 /* The section length holds the length of the entire frame.
\r
394 ulLengthSoFar holds the length of the frame sections already
\r
395 copied to uIP, so the length of the final section is
\r
396 ulSectionLength - ulLengthSoFar; */
\r
397 if( ulSectionLength > ulLengthSoFar )
\r
399 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ( ulSectionLength - ulLengthSoFar ) );
\r
403 /* Is this the last buffer for the frame? If not why? */
\r
404 ulEOF = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_EOF;
\r
407 /* Mark the buffer as free again. */
\r
408 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );
\r
410 /* Increment to the next buffer, wrapping if necessary. */
\r
412 if( ulNextRxBuffer >= NB_RX_BUFFERS )
\r
414 ulNextRxBuffer = 0;
\r
418 /* If we obtained data but for some reason did not find the end of the
\r
419 frame then discard the data as it must contain an error. */
\r
422 ulSectionLength = 0;
\r
425 return ulSectionLength;
\r
427 /*-----------------------------------------------------------*/
\r
429 static void prvSetupDescriptors(void)
\r
431 unsigned portBASE_TYPE xIndex;
\r
432 unsigned portLONG ulAddress;
\r
434 /* Initialise xRxDescriptors descriptor. */
\r
435 for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )
\r
437 /* Calculate the address of the nth buffer within the array. */
\r
438 ulAddress = ( unsigned portLONG )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );
\r
440 /* Write the buffer address into the descriptor. The DMA will place
\r
441 the data at this address when this descriptor is being used. Mask off
\r
442 the bottom bits of the address as these have special meaning. */
\r
443 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
446 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
447 to the first buffer. */
\r
448 xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;
\r
450 /* Initialise xTxDescriptors. */
\r
451 for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )
\r
453 /* Calculate the address of the nth buffer within the array. */
\r
454 ulAddress = ( unsigned portLONG )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );
\r
456 /* Write the buffer address into the descriptor. The DMA will read
\r
457 data from here when the descriptor is being used. */
\r
458 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;
\r
459 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;
\r
462 /* The last buffer has the wrap bit set so the EMAC knows to wrap back
\r
463 to the first buffer. */
\r
464 xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;
\r
466 /* Tell the EMAC where to find the descriptors. */
\r
467 AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned portLONG ) xRxDescriptors;
\r
468 AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned portLONG ) xTxDescriptors;
\r
470 /* Clear all the bits in the receive status register. */
\r
471 AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );
\r
473 /* Enable the copy of data into the buffers, ignore broadcasts,
\r
474 and don't copy FCS. */
\r
475 AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);
\r
477 /* Enable Rx and Tx, plus the stats register. */
\r
478 AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );
\r
480 /*-----------------------------------------------------------*/
\r
482 static void prvSetupMACAddress( void )
\r
484 /* Must be written SA1L then SA1H. */
\r
485 AT91C_BASE_EMAC->EMAC_SA1L = ( ( unsigned portLONG ) cMACAddress[ 3 ] << 24 ) |
\r
486 ( ( unsigned portLONG ) cMACAddress[ 2 ] << 16 ) |
\r
487 ( ( unsigned portLONG ) cMACAddress[ 1 ] << 8 ) |
\r
490 AT91C_BASE_EMAC->EMAC_SA1H = ( ( unsigned portLONG ) cMACAddress[ 5 ] << 8 ) |
\r
493 /*-----------------------------------------------------------*/
\r
495 static void prvSetupEMACInterrupt( void )
\r
497 /* Create the semaphore used to trigger the EMAC task. */
\r
498 vSemaphoreCreateBinary( xSemaphore );
\r
501 /* We start by 'taking' the semaphore so the ISR can 'give' it when the
\r
502 first interrupt occurs. */
\r
503 xSemaphoreTake( xSemaphore, emacNO_DELAY );
\r
504 portENTER_CRITICAL();
\r
506 /* We want to interrupt on Rx events. */
\r
507 AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP;
\r
509 /* Enable the interrupts in the AIC. */
\r
510 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISREntry );
\r
511 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_EMAC );
\r
513 portEXIT_CRITICAL();
\r
516 /*-----------------------------------------------------------*/
\r
518 __arm void vEMACISR( void )
\r
520 volatile unsigned portLONG ulIntStatus, ulRxStatus;
\r
521 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
523 ulIntStatus = AT91C_BASE_EMAC->EMAC_ISR;
\r
524 ulRxStatus = AT91C_BASE_EMAC->EMAC_RSR;
\r
526 if( ( ulIntStatus & AT91C_EMAC_RCOMP ) || ( ulRxStatus & AT91C_EMAC_REC ) )
\r
528 /* A frame has been received, signal the uIP task so it can process
\r
529 the Rx descriptors. */
\r
530 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
531 AT91C_BASE_EMAC->EMAC_RSR = AT91C_EMAC_REC;
\r
534 /* If a task was woken by either a character being received or a character
\r
535 being transmitted then we may need to switch to another task. */
\r
536 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
538 /* Clear the interrupt. */
\r
539 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
541 /*-----------------------------------------------------------*/
\r
546 * The following functions are initialisation functions taken from the Atmel
\r
547 * EMAC sample code.
\r
550 static portBASE_TYPE prvProbePHY( void )
\r
552 unsigned portLONG ulPHYId1, ulPHYId2, ulStatus;
\r
553 portBASE_TYPE xReturn = pdPASS;
\r
555 /* Code supplied by Atmel (reformatted) -----------------*/
\r
557 /* Enable management port */
\r
558 AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
\r
559 AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;
\r
561 /* Read the PHY ID. */
\r
562 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );
\r
563 vReadPHY( AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );
\r
568 Bits 3:0 Revision Number Four bit manufacturer
\92s revision number.
\r
569 0001 stands for Rev. A, etc.
\r
571 if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )
\r
573 /* Did not expect this ID. */
\r
578 ulStatus = xGetLinkSpeed();
\r
580 if( ulStatus != pdPASS )
\r
586 /* Disable management port */
\r
587 AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
\r
589 /* End of code supplied by Atmel ------------------------*/
\r
593 /*-----------------------------------------------------------*/
\r
595 static void vReadPHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG *pulValue )
\r
597 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
599 AT91C_BASE_EMAC->EMAC_MAN = (AT91C_EMAC_SOF & (0x01<<30))
\r
600 | (2 << 16) | (2 << 28)
\r
601 | ((ucPHYAddress & 0x1f) << 23)
\r
602 | (ucAddress << 18);
\r
604 /* Wait until IDLE bit in Network Status register is cleared. */
\r
605 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
610 *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff );
\r
612 /* End of code supplied by Atmel ------------------------*/
\r
614 /*-----------------------------------------------------------*/
\r
616 #if USE_RMII_INTERFACE != 1
\r
617 static void vWritePHY( unsigned portCHAR ucPHYAddress, unsigned portCHAR ucAddress, unsigned portLONG ulValue )
\r
619 /* Code supplied by Atmel (reformatted) ----------------------*/
\r
621 AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))
\r
622 | (2 << 16) | (1 << 28)
\r
623 | ((ucPHYAddress & 0x1f) << 23)
\r
624 | (ucAddress << 18))
\r
625 | (ulValue & 0xffff);
\r
627 /* Wait until IDLE bit in Network Status register is cleared */
\r
628 while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )
\r
633 /* End of code supplied by Atmel ------------------------*/
\r
636 /*-----------------------------------------------------------*/
\r
638 static portBASE_TYPE xGetLinkSpeed( void )
\r
640 unsigned portLONG ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;
\r
642 /* Code supplied by Atmel (reformatted) -----------------*/
\r
644 /* Link status is latched, so read twice to get current value */
\r
645 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
646 vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);
\r
648 if( !( ulBMSR & BMSR_LSTATUS ) )
\r
654 vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);
\r
655 if (ulBMCR & BMCR_ANENABLE)
\r
657 /* AutoNegotiation is enabled. */
\r
658 if (!(ulBMSR & BMSR_ANEGCOMPLETE))
\r
660 /* Auto-negotiation in progress. */
\r
664 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);
\r
665 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )
\r
667 ulSpeed = SPEED_100;
\r
671 ulSpeed = SPEED_10;
\r
674 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )
\r
676 ulDuplex = DUPLEX_FULL;
\r
680 ulDuplex = DUPLEX_HALF;
\r
685 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;
\r
686 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;
\r
689 /* Update the MAC */
\r
690 ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );
\r
691 if( ulSpeed == SPEED_100 )
\r
693 if( ulDuplex == DUPLEX_FULL )
\r
695 /* 100 Full Duplex */
\r
696 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;
\r
700 /* 100 Half Duplex */
\r
701 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;
\r
706 if (ulDuplex == DUPLEX_FULL)
\r
708 /* 10 Full Duplex */
\r
709 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;
\r
713 /* 10 Half Duplex */
\r
714 AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;
\r
718 /* End of code supplied by Atmel ------------------------*/
\r