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