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