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