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