]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/webserver/SAM7_EMAC.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / webserver / SAM7_EMAC.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     This file is part of the FreeRTOS distribution.\r
5 \r
6     FreeRTOS is free software; you can redistribute it and/or modify it    under\r
7     the terms of the GNU General Public License (version 2) as published by the\r
8     Free Software Foundation and modified by the FreeRTOS exception.\r
9     **NOTE** The exception to the GPL is included to allow you to distribute a\r
10     combined work that includes FreeRTOS without being obliged to provide the\r
11     source code for proprietary components outside of the FreeRTOS kernel.\r
12     Alternative commercial license and support terms are also available upon\r
13     request.  See the licensing section of http://www.FreeRTOS.org for full\r
14     license details.\r
15 \r
16     FreeRTOS is distributed in the hope that it will be useful,    but WITHOUT\r
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19     more details.\r
20 \r
21     You should have received a copy of the GNU General Public License along\r
22     with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23     Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26     ***************************************************************************\r
27     *                                                                         *\r
28     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /* Standard includes. */\r
50 #include <string.h>\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "semphr.h"\r
55 #include "task.h"\r
56 \r
57 /* Demo application includes. */\r
58 #include "SAM7_EMAC.h"\r
59 \r
60 /* uIP includes. */\r
61 #include "uip.h"\r
62 \r
63 /* Hardware specific includes. */\r
64 #include "Emac.h"\r
65 #include "mii.h"\r
66 #include "AT91SAM7X256.h"\r
67 \r
68 \r
69 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0\r
70 to use an MII interface. */\r
71 #define USE_RMII_INTERFACE 0\r
72 \r
73 /* The buffer addresses written into the descriptors must be aligned so the\r
74 last few bits are zero.  These bits have special meaning for the EMAC\r
75 peripheral and cannot be used as part of the address. */\r
76 #define emacADDRESS_MASK                        ( ( unsigned long ) 0xFFFFFFFC )\r
77 \r
78 /* Bit used within the address stored in the descriptor to mark the last\r
79 descriptor in the array. */\r
80 #define emacRX_WRAP_BIT                         ( ( unsigned long ) 0x02 )\r
81 \r
82 /* Bit used within the Tx descriptor status to indicate whether the\r
83 descriptor is under the control of the EMAC or the software. */\r
84 #define emacTX_BUF_USED                         ( ( unsigned long ) 0x80000000 )\r
85 \r
86 /* A short delay is used to wait for a buffer to become available, should\r
87 one not be immediately available when trying to transmit a frame. */\r
88 #define emacBUFFER_WAIT_DELAY           ( 2 )\r
89 #define emacMAX_WAIT_CYCLES                     ( configTICK_RATE_HZ / 40 )\r
90 \r
91 /* Misc defines. */\r
92 #define emacINTERRUPT_LEVEL                     ( 5 )\r
93 #define emacNO_DELAY                            ( 0 )\r
94 #define emacTOTAL_FRAME_HEADER_SIZE     ( 54 )\r
95 #define emacPHY_INIT_DELAY                      ( 5000 / portTICK_RATE_MS )\r
96 #define emacRESET_KEY                           ( ( unsigned long ) 0xA5000000 )\r
97 #define emacRESET_LENGTH                        ( ( unsigned long ) ( 0x01 << 8 ) )\r
98 \r
99 /* The Atmel header file only defines the TX frame length mask. */\r
100 #define emacRX_LENGTH_FRAME                     ( 0xfff )\r
101 \r
102 /* Peripheral setup for the EMAC. */\r
103 #define emacPERIPHERAL_A_SETUP          ( ( unsigned long ) AT91C_PB2_ETX0                      ) | \\r
104                                                                         ( ( unsigned long ) AT91C_PB12_ETXER            ) | \\r
105                                                                         ( ( unsigned long ) AT91C_PB16_ECOL                     ) | \\r
106                                                                         ( ( unsigned long ) AT91C_PB11_ETX3                     ) | \\r
107                                                                         ( ( unsigned long ) AT91C_PB6_ERX1                      ) | \\r
108                                                                         ( ( unsigned long ) AT91C_PB15_ERXDV            ) | \\r
109                                                                         ( ( unsigned long ) AT91C_PB13_ERX2                     ) | \\r
110                                                                         ( ( unsigned long ) AT91C_PB3_ETX1                      ) | \\r
111                                                                         ( ( unsigned long ) AT91C_PB8_EMDC                      ) | \\r
112                                                                         ( ( unsigned long ) AT91C_PB5_ERX0                      ) | \\r
113                                                                         ( ( unsigned long ) AT91C_PB14_ERX3                     ) | \\r
114                                                                         ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV       ) | \\r
115                                                                         ( ( unsigned long ) AT91C_PB1_ETXEN                     ) | \\r
116                                                                         ( ( unsigned long ) AT91C_PB10_ETX2                     ) | \\r
117                                                                         ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK      ) | \\r
118                                                                         ( ( unsigned long ) AT91C_PB9_EMDIO                     ) | \\r
119                                                                         ( ( unsigned long ) AT91C_PB7_ERXER                     ) | \\r
120                                                                         ( ( unsigned long ) AT91C_PB17_ERXCK            );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * Prototype for the EMAC interrupt function - called by the asm wrapper.\r
126  */\r
127 extern void vEMACISR_Wrapper( void ) __attribute__((naked));\r
128 \r
129 /*\r
130  * Initialise both the Tx and Rx descriptors used by the EMAC.\r
131  */\r
132 static void prvSetupDescriptors(void);\r
133 \r
134 /*\r
135  * Write our MAC address into the EMAC.  The MAC address is set as one of the\r
136  * uip options.\r
137  */\r
138 static void prvSetupMACAddress( void );\r
139 \r
140 /*\r
141  * Configure the EMAC and AIC for EMAC interrupts.\r
142  */\r
143 static void prvSetupEMACInterrupt( void );\r
144 \r
145 /*\r
146  * Some initialisation functions taken from the Atmel EMAC sample code.\r
147  */\r
148 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );\r
149 #if USE_RMII_INTERFACE != 1\r
150         static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);\r
151 #endif\r
152 static portBASE_TYPE xGetLinkSpeed( void );\r
153 static portBASE_TYPE prvProbePHY( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /* Buffer written to by the EMAC DMA.  Must be aligned as described by the\r
158 comment above the emacADDRESS_MASK definition. */\r
159 #pragma data_alignment=8\r
160 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ];\r
161 \r
162 /* Buffer read by the EMAC DMA.  Must be aligned as described by he comment\r
163 above the emacADDRESS_MASK definition. */\r
164 #pragma data_alignment=8\r
165 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ];\r
166 \r
167 /* Descriptors used to communicate between the program and the EMAC peripheral.\r
168 These descriptors hold the locations and state of the Rx and Tx buffers. */\r
169 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];\r
170 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];\r
171 \r
172 /* The IP and Ethernet addresses are read from the uIP setup. */\r
173 const char cMACAddress[ 6 ] = { uipMAC_ADDR0, uipMAC_ADDR1, uipMAC_ADDR2, uipMAC_ADDR3, uipMAC_ADDR4, uipMAC_ADDR5 };\r
174 const unsigned char ucIPAddress[ 4 ]  = { uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 };\r
175 \r
176 /* The semaphore used by the EMAC ISR to wake the EMAC task. */\r
177 static xSemaphoreHandle xSemaphore = NULL;\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 xSemaphoreHandle xEMACInit( void )\r
182 {\r
183         /* Code supplied by Atmel -------------------------------*/\r
184 \r
185         /* Disable pull up on RXDV => PHY normal mode (not in test mode),\r
186         PHY has internal pull down. */\r
187         AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;\r
188 \r
189         #if USE_RMII_INTERFACE != 1\r
190                 /* PHY has internal pull down : set MII mode. */\r
191                 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;\r
192         #endif\r
193 \r
194         /* Clear PB18 <=> PHY powerdown. */\r
195         AT91C_BASE_PIOB->PIO_PER = 1 << 18;\r
196         AT91C_BASE_PIOB->PIO_OER = 1 << 18;\r
197         AT91C_BASE_PIOB->PIO_CODR = 1 << 18;\r
198 \r
199         /* After PHY power up, hardware reset. */\r
200         AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;\r
201         AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;\r
202 \r
203         /* Wait for hardware reset end. */\r
204         while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )\r
205         {\r
206                 __asm volatile ( "NOP" );\r
207         }\r
208     __asm volatile ( "NOP" );\r
209 \r
210         /* Setup the pins. */\r
211         AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;\r
212         AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;\r
213 \r
214         /* Enable com between EMAC PHY.\r
215 \r
216         Enable management port. */\r
217         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
218 \r
219         /* MDC = MCK/32. */\r
220         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;     \r
221 \r
222         /* Wait for PHY auto init end (rather crude delay!). */\r
223         vTaskDelay( emacPHY_INIT_DELAY );\r
224 \r
225         /* PHY configuration. */\r
226         #if USE_RMII_INTERFACE != 1\r
227         {\r
228                 unsigned long ulControl;\r
229 \r
230                 /* PHY has internal pull down : disable MII isolate. */\r
231                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
232                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
233                 ulControl &= ~BMCR_ISOLATE;\r
234                 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );\r
235         }\r
236         #endif\r
237 \r
238         /* Disable management port again. */\r
239         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;\r
240 \r
241         #if USE_RMII_INTERFACE != 1\r
242                 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */\r
243                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;\r
244         #else\r
245                 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator\r
246                 on ERFCK). */\r
247                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;\r
248         #endif\r
249 \r
250         /* End of code supplied by Atmel ------------------------*/\r
251 \r
252         /* Setup the buffers and descriptors. */\r
253         prvSetupDescriptors();\r
254         \r
255         /* Load our MAC address into the EMAC. */\r
256         prvSetupMACAddress();\r
257 \r
258         /* Are we connected? */\r
259         if( prvProbePHY() )\r
260         {\r
261                 /* Enable the interrupt! */\r
262                 portENTER_CRITICAL();\r
263                 {\r
264                         prvSetupEMACInterrupt();\r
265                         vPassEMACSemaphore( xSemaphore );\r
266                 }\r
267                 portEXIT_CRITICAL();\r
268         }\r
269 \r
270         return xSemaphore;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 long lEMACSend( void )\r
275 {\r
276 static unsigned portBASE_TYPE uxTxBufferIndex = 0;\r
277 portBASE_TYPE xWaitCycles = 0;\r
278 long lReturn = pdPASS;\r
279 char *pcBuffer;\r
280 \r
281         /* Is a buffer available? */\r
282         while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )\r
283         {\r
284                 /* There is no room to write the Tx data to the Tx buffer.  Wait a\r
285                 short while, then try again. */\r
286                 xWaitCycles++;\r
287                 if( xWaitCycles > emacMAX_WAIT_CYCLES )\r
288                 {\r
289                         /* Give up. */\r
290                         lReturn = pdFAIL;\r
291                         break;\r
292                 }\r
293                 else\r
294                 {\r
295                         vTaskDelay( emacBUFFER_WAIT_DELAY );\r
296                 }\r
297         }\r
298 \r
299         /* lReturn will only be pdPASS if a buffer is available. */\r
300         if( lReturn == pdPASS )\r
301         {\r
302                 /* Copy the headers into the Tx buffer.  These will be in the uIP buffer. */\r
303                 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;\r
304                 memcpy( ( void * ) pcBuffer, ( void * ) uip_buf, emacTOTAL_FRAME_HEADER_SIZE );\r
305 \r
306                 /* If there is room, also copy in the application data if any. */\r
307                 if( ( uip_len > emacTOTAL_FRAME_HEADER_SIZE ) && ( uip_len <= ( ETH_TX_BUFFER_SIZE - emacTOTAL_FRAME_HEADER_SIZE ) ) )\r
308                 {\r
309                         memcpy( ( void * ) &( pcBuffer[ emacTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - emacTOTAL_FRAME_HEADER_SIZE ) );\r
310                 }\r
311 \r
312                 /* Send. */     \r
313                 portENTER_CRITICAL();\r
314                 {\r
315                         if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )\r
316                         {\r
317                                 /* Fill out the necessary in the descriptor to get the data sent. */\r
318                                 xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )\r
319                                                                                                                                                 | AT91C_LAST_BUFFER\r
320                                                                                                                                                 | AT91C_TRANSMIT_WRAP;\r
321                                 uxTxBufferIndex = 0;\r
322                         }\r
323                         else\r
324                         {\r
325                                 /* Fill out the necessary in the descriptor to get the data sent. */\r
326                                 xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( uip_len & ( unsigned long ) AT91C_LENGTH_FRAME )\r
327                                                                                                                                                 | AT91C_LAST_BUFFER;\r
328                                 uxTxBufferIndex++;\r
329                         }\r
330         \r
331                         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;\r
332                 }\r
333                 portEXIT_CRITICAL();\r
334         }\r
335 \r
336         return lReturn;\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 unsigned long ulEMACPoll( void )\r
341 {\r
342 static unsigned portBASE_TYPE ulNextRxBuffer = 0;\r
343 unsigned long ulSectionLength = 0, ulLengthSoFar = 0, ulEOF = pdFALSE;\r
344 char *pcSource;\r
345 \r
346         /* Skip any fragments. */\r
347         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )\r
348         {\r
349                 /* Mark the buffer as free again. */\r
350                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );              \r
351                 ulNextRxBuffer++;\r
352                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
353                 {\r
354                         ulNextRxBuffer = 0;\r
355                 }\r
356         }\r
357 \r
358         /* Is there a packet ready? */\r
359 \r
360         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !ulSectionLength )\r
361         {\r
362                 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
363                 ulSectionLength = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & emacRX_LENGTH_FRAME;\r
364 \r
365                 if( ulSectionLength == 0 )\r
366                 {\r
367                         /* The frame is longer than the buffer pointed to by this\r
368                         descriptor so copy the entire buffer to uIP - then move onto\r
369                         the next descriptor to get the rest of the frame. */\r
370                         if( ( ulLengthSoFar + ETH_RX_BUFFER_SIZE ) <= UIP_BUFSIZE )\r
371                         {\r
372                                 memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ETH_RX_BUFFER_SIZE );\r
373                                 ulLengthSoFar += ETH_RX_BUFFER_SIZE;\r
374                         }                       \r
375                 }\r
376                 else\r
377                 {\r
378                         /* This is the last section of the frame.  Copy the section to\r
379                         uIP. */\r
380                         if( ulSectionLength < UIP_BUFSIZE )\r
381                         {\r
382                                 /* The section length holds the length of the entire frame.\r
383                                 ulLengthSoFar holds the length of the frame sections already\r
384                                 copied to uIP, so the length of the final section is\r
385                                 ulSectionLength - ulLengthSoFar; */\r
386                                 if( ulSectionLength > ulLengthSoFar )\r
387                                 {\r
388                                         memcpy( &( uip_buf[ ulLengthSoFar ] ), pcSource, ( ulSectionLength - ulLengthSoFar ) );\r
389                                 }\r
390                         }                       \r
391 \r
392                         /* Is this the last buffer for the frame?  If not why? */\r
393                         ulEOF = xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_EOF;\r
394                 }\r
395 \r
396                 /* Mark the buffer as free again. */\r
397                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
398 \r
399                 /* Increment to the next buffer, wrapping if necessary. */\r
400                 ulNextRxBuffer++;\r
401                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
402                 {\r
403                         ulNextRxBuffer = 0;\r
404                 }\r
405         }\r
406 \r
407         /* If we obtained data but for some reason did not find the end of the\r
408         frame then discard the data as it must contain an error. */\r
409         if( !ulEOF )\r
410         {\r
411                 ulSectionLength = 0;\r
412         }\r
413 \r
414         return ulSectionLength;\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static void prvSetupDescriptors(void)\r
419 {\r
420 unsigned portBASE_TYPE xIndex;\r
421 unsigned long ulAddress;\r
422 \r
423         /* Initialise xRxDescriptors descriptor. */\r
424         for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )\r
425         {\r
426                 /* Calculate the address of the nth buffer within the array. */\r
427                 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );\r
428 \r
429                 /* Write the buffer address into the descriptor.  The DMA will place\r
430                 the data at this address when this descriptor is being used.  Mask off\r
431                 the bottom bits of the address as these have special meaning. */\r
432                 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
433         }       \r
434 \r
435         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
436         to the first buffer. */\r
437         xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;\r
438 \r
439         /* Initialise xTxDescriptors. */\r
440         for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )\r
441         {\r
442                 /* Calculate the address of the nth buffer within the array. */\r
443                 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );\r
444 \r
445                 /* Write the buffer address into the descriptor.  The DMA will read\r
446                 data from here when the descriptor is being used. */\r
447                 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
448                 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;\r
449         }       \r
450 \r
451         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
452         to the first buffer. */\r
453         xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;\r
454 \r
455         /* Tell the EMAC where to find the descriptors. */\r
456         AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;\r
457         AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;\r
458         \r
459         /* Clear all the bits in the receive status register. */\r
460         AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );\r
461 \r
462         /* Enable the copy of data into the buffers, ignore broadcasts,\r
463         and don't copy FCS. */\r
464         AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);\r
465 \r
466         /* Enable Rx and Tx, plus the stats register. */\r
467         AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );\r
468 }       \r
469 /*-----------------------------------------------------------*/\r
470 \r
471 static void prvSetupMACAddress( void )\r
472 {\r
473         /* Must be written SA1L then SA1H. */\r
474         AT91C_BASE_EMAC->EMAC_SA1L =    ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |\r
475                                                                         ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |\r
476                                                                         ( ( unsigned long ) cMACAddress[ 1 ] << 8  ) |\r
477                                                                         cMACAddress[ 0 ];\r
478 \r
479         AT91C_BASE_EMAC->EMAC_SA1H =    ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |\r
480                                                                         cMACAddress[ 4 ];\r
481 }\r
482 /*-----------------------------------------------------------*/\r
483 \r
484 static void prvSetupEMACInterrupt( void )\r
485 {\r
486         /* Create the semaphore used to trigger the EMAC task. */\r
487         vSemaphoreCreateBinary( xSemaphore );\r
488         if( xSemaphore )\r
489         {\r
490                 /* We start by 'taking' the semaphore so the ISR can 'give' it when the\r
491                 first interrupt occurs. */\r
492                 xSemaphoreTake( xSemaphore, emacNO_DELAY );\r
493                 portENTER_CRITICAL();\r
494                 {\r
495                         /* We want to interrupt on Rx events. */\r
496                         AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP;\r
497 \r
498                         /* Enable the interrupts in the AIC. */\r
499                         AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );\r
500                         AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;\r
501                 }\r
502                 portEXIT_CRITICAL();\r
503         }\r
504 }\r
505 /*-----------------------------------------------------------*/\r
506 \r
507 \r
508 \r
509 \r
510 /*\r
511  * The following functions are initialisation functions taken from the Atmel\r
512  * EMAC sample code.\r
513  */\r
514 \r
515 static portBASE_TYPE prvProbePHY( void )\r
516 {\r
517 unsigned long ulPHYId1, ulPHYId2, ulStatus;\r
518 portBASE_TYPE xReturn = pdPASS;\r
519         \r
520         /* Code supplied by Atmel (reformatted) -----------------*/\r
521 \r
522         /* Enable management port */\r
523         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
524         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;\r
525 \r
526         /* Read the PHY ID. */\r
527         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );\r
528         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );\r
529 \r
530         /* AMD AM79C875:\r
531                         PHY_ID1 = 0x0022\r
532                         PHY_ID2 = 0x5541\r
533                         Bits 3:0 Revision Number Four bit manufacturer\92s revision number.\r
534                                 0001 stands for Rev. A, etc.\r
535         */\r
536         if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )\r
537         {\r
538                 /* Did not expect this ID. */\r
539                 xReturn = pdFAIL;\r
540         }\r
541         else\r
542         {\r
543                 ulStatus = xGetLinkSpeed();\r
544 \r
545                 if( ulStatus != pdPASS )\r
546                 {\r
547                         xReturn = pdFAIL;\r
548                 }\r
549         }\r
550 \r
551         /* Disable management port */\r
552         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;   \r
553 \r
554         /* End of code supplied by Atmel ------------------------*/\r
555 \r
556         return xReturn;\r
557 }\r
558 /*-----------------------------------------------------------*/\r
559 \r
560 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )\r
561 {\r
562         /* Code supplied by Atmel (reformatted) ----------------------*/\r
563 \r
564         AT91C_BASE_EMAC->EMAC_MAN =     (AT91C_EMAC_SOF & (0x01<<30))\r
565                                                                         | (2 << 16) | (2 << 28)\r
566                                                                         | ((ucPHYAddress & 0x1f) << 23)\r
567                                                                         | (ucAddress << 18);\r
568 \r
569         /* Wait until IDLE bit in Network Status register is cleared. */\r
570         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
571         {\r
572                 __asm( "NOP" );\r
573         }\r
574 \r
575         *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff ); \r
576 \r
577         /* End of code supplied by Atmel ------------------------*/\r
578 }\r
579 /*-----------------------------------------------------------*/\r
580 \r
581 #if USE_RMII_INTERFACE != 1\r
582 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )\r
583 {\r
584         /* Code supplied by Atmel (reformatted) ----------------------*/\r
585 \r
586         AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))\r
587                                                                 | (2 << 16) | (1 << 28)\r
588                                                                 | ((ucPHYAddress & 0x1f) << 23)\r
589                                                                 | (ucAddress << 18))\r
590                                                                 | (ulValue & 0xffff);\r
591 \r
592         /* Wait until IDLE bit in Network Status register is cleared */\r
593         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
594         {\r
595                 __asm( "NOP" );\r
596         };\r
597 \r
598         /* End of code supplied by Atmel ------------------------*/\r
599 }\r
600 #endif\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 static portBASE_TYPE xGetLinkSpeed( void )\r
604 {\r
605         unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;\r
606 \r
607         /* Code supplied by Atmel (reformatted) -----------------*/\r
608 \r
609         /* Link status is latched, so read twice to get current value */\r
610         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
611         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
612 \r
613         if( !( ulBMSR & BMSR_LSTATUS ) )\r
614         {       \r
615                 /* No Link. */\r
616                 return pdFAIL;\r
617         }\r
618 \r
619         vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);\r
620         if (ulBMCR & BMCR_ANENABLE)\r
621         {                               \r
622                 /* AutoNegotiation is enabled. */\r
623                 if (!(ulBMSR & BMSR_ANEGCOMPLETE))\r
624                 {\r
625                         /* Auto-negotiation in progress. */\r
626                         return pdFAIL;                          \r
627                 }               \r
628 \r
629                 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);\r
630                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )\r
631                 {\r
632                         ulSpeed = SPEED_100;\r
633                 }\r
634                 else\r
635                 {\r
636                         ulSpeed = SPEED_10;\r
637                 }\r
638 \r
639                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )\r
640                 {\r
641                         ulDuplex = DUPLEX_FULL;\r
642                 }\r
643                 else\r
644                 {\r
645                         ulDuplex = DUPLEX_HALF;\r
646                 }\r
647         }\r
648         else\r
649         {\r
650                 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;\r
651                 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;\r
652         }\r
653 \r
654         /* Update the MAC */\r
655         ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );\r
656         if( ulSpeed == SPEED_100 )\r
657         {\r
658                 if( ulDuplex == DUPLEX_FULL )\r
659                 {\r
660                         /* 100 Full Duplex */\r
661                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;\r
662                 }\r
663                 else\r
664                 {                                       \r
665                         /* 100 Half Duplex */\r
666                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;\r
667                 }\r
668         }\r
669         else\r
670         {\r
671                 if (ulDuplex == DUPLEX_FULL)\r
672                 {\r
673                         /* 10 Full Duplex */\r
674                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;\r
675                 }\r
676                 else\r
677                 {\r
678                         /* 10 Half Duplex */\r
679                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;\r
680                 }\r
681         }\r
682 \r
683         /* End of code supplied by Atmel ------------------------*/\r
684 \r
685         return pdPASS;\r
686 }\r