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