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