]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/EMAC/SAM7_EMAC.c
Update version number in readiness for V10.2.0 release.
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / EMAC / SAM7_EMAC.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Interrupt driven driver for the EMAC peripheral.  This driver is not\r
30  * reentrant, re-entrancy is handled by a semaphore at the network interface\r
31  * level. \r
32  */\r
33 \r
34 \r
35 /*\r
36 Changes from V3.2.2\r
37 \r
38         + Corrected the byte order when writing the MAC address to the MAC.\r
39         + Support added for MII interfaces.  Previously only RMII was supported.\r
40 \r
41 Changes from V3.2.3\r
42 \r
43         + The MII interface is now the default.\r
44         + Modified the initialisation sequence slightly to allow auto init more\r
45           time to complete.\r
46 \r
47 Changes from V4.0.1\r
48 \r
49         + Made the function vClearEMACTxBuffer() more robust by moving the index\r
50           manipulation into the if() statement.  This allows the tx interrupt to\r
51           execute even when there is no data to handle.\r
52 \r
53 Changes from V4.0.4\r
54 \r
55         + Corrected the Rx frame length mask when obtaining the length from the\r
56           rx descriptor.\r
57 */\r
58 \r
59 \r
60 /* Standard includes. */\r
61 #include <string.h>\r
62 \r
63 /* Scheduler includes. */\r
64 #include "FreeRTOS.h"\r
65 #include "semphr.h"\r
66 #include "task.h"\r
67 \r
68 /* Demo app includes. */\r
69 #include "SAM7_EMAC.h"\r
70 \r
71 /* Hardware specific includes. */\r
72 #include "Emac.h"\r
73 #include "mii.h"\r
74 #include "AT91SAM7X256.h"\r
75 \r
76 \r
77 /* USE_RMII_INTERFACE must be defined as 1 to use an RMII interface, or 0\r
78 to use an MII interface. */\r
79 #define USE_RMII_INTERFACE 0\r
80 \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 long ) 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 long ) 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 long ) 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                     ( ( portBASE_TYPE ) ( configTICK_RATE_HZ / 40 ) )\r
99 \r
100 /* The time to block waiting for input. */\r
101 #define emacBLOCK_TIME_WAITING_FOR_INPUT        ( ( TickType_t ) 100 )\r
102 \r
103 /* Peripheral setup for the EMAC. */\r
104 #define emacPERIPHERAL_A_SETUP          ( ( unsigned long ) AT91C_PB2_ETX0                      ) | \\r
105                                                                         ( ( unsigned long ) AT91C_PB12_ETXER            ) | \\r
106                                                                         ( ( unsigned long ) AT91C_PB16_ECOL                     ) | \\r
107                                                                         ( ( unsigned long ) AT91C_PB11_ETX3                     ) | \\r
108                                                                         ( ( unsigned long ) AT91C_PB6_ERX1                      ) | \\r
109                                                                         ( ( unsigned long ) AT91C_PB15_ERXDV            ) | \\r
110                                                                         ( ( unsigned long ) AT91C_PB13_ERX2                     ) | \\r
111                                                                         ( ( unsigned long ) AT91C_PB3_ETX1                      ) | \\r
112                                                                         ( ( unsigned long ) AT91C_PB8_EMDC                      ) | \\r
113                                                                         ( ( unsigned long ) AT91C_PB5_ERX0                      ) | \\r
114                                                                         ( ( unsigned long ) AT91C_PB14_ERX3                     ) | \\r
115                                                                         ( ( unsigned long ) AT91C_PB4_ECRS_ECRSDV       ) | \\r
116                                                                         ( ( unsigned long ) AT91C_PB1_ETXEN                     ) | \\r
117                                                                         ( ( unsigned long ) AT91C_PB10_ETX2                     ) | \\r
118                                                                         ( ( unsigned long ) AT91C_PB0_ETXCK_EREFCK      ) | \\r
119                                                                         ( ( unsigned long ) AT91C_PB9_EMDIO                     ) | \\r
120                                                                         ( ( unsigned long ) AT91C_PB7_ERXER                     ) | \\r
121                                                                         ( ( unsigned long ) AT91C_PB17_ERXCK            );\r
122 \r
123 /* Misc defines. */\r
124 #define emacINTERRUPT_LEVEL                     ( 5 )\r
125 #define emacNO_DELAY                            ( 0 )\r
126 #define emacTOTAL_FRAME_HEADER_SIZE     ( 54 )\r
127 #define emacPHY_INIT_DELAY                      ( 5000 / portTICK_PERIOD_MS )\r
128 #define emacRESET_KEY                           ( ( unsigned long ) 0xA5000000 )\r
129 #define emacRESET_LENGTH                        ( ( unsigned long ) ( 0x01 << 8 ) )\r
130 \r
131 /* The Atmel header file only defines the TX frame length mask. */\r
132 #define emacRX_LENGTH_FRAME                     ( 0xfff )\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* Buffer written to by the EMAC DMA.  Must be aligned as described by the\r
137 comment above the emacADDRESS_MASK definition. */\r
138 static volatile char pcRxBuffer[ NB_RX_BUFFERS * ETH_RX_BUFFER_SIZE ] __attribute__ ((aligned (8)));\r
139 \r
140 /* Buffer read by the EMAC DMA.  Must be aligned as described by the comment\r
141 above the emacADDRESS_MASK definition. */\r
142 static char pcTxBuffer[ NB_TX_BUFFERS * ETH_TX_BUFFER_SIZE ] __attribute__ ((aligned (8)));\r
143 \r
144 /* Descriptors used to communicate between the program and the EMAC peripheral.\r
145 These descriptors hold the locations and state of the Rx and Tx buffers. */\r
146 static volatile AT91S_TxTdDescriptor xTxDescriptors[ NB_TX_BUFFERS ];\r
147 static volatile AT91S_RxTdDescriptor xRxDescriptors[ NB_RX_BUFFERS ];\r
148 \r
149 /* The IP and Ethernet addresses are read from the header files. */\r
150 const char cMACAddress[ 6 ] = { emacETHADDR0, emacETHADDR1, emacETHADDR2, emacETHADDR3, emacETHADDR4, emacETHADDR5 };\r
151 const unsigned char ucIPAddress[ 4 ]  = { emacIPADDR0, emacIPADDR1, emacIPADDR2, emacIPADDR3 };\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /* See the header file for descriptions of public functions. */\r
156 \r
157 /*\r
158  * Prototype for the EMAC interrupt function.\r
159  */\r
160 void vEMACISR_Wrapper( void ) __attribute__ ((naked));\r
161 \r
162 /*\r
163  * Initialise both the Tx and Rx descriptors used by the EMAC.\r
164  */\r
165 static void prvSetupDescriptors(void);\r
166 \r
167 /*\r
168  * Write our MAC address into the EMAC.  \r
169  */\r
170 static void prvSetupMACAddress( void );\r
171 \r
172 /*\r
173  * Configure the EMAC and AIC for EMAC interrupts.\r
174  */\r
175 static void prvSetupEMACInterrupt( void );\r
176 \r
177 /*\r
178  * Some initialisation functions taken from the Atmel EMAC sample code.\r
179  */\r
180 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue );\r
181 static portBASE_TYPE xGetLinkSpeed( void );\r
182 static portBASE_TYPE prvProbePHY( void );\r
183 #if USE_RMII_INTERFACE != 1\r
184         static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue);\r
185 #endif\r
186 \r
187 \r
188 /* The semaphore used by the EMAC ISR to wake the EMAC task. */\r
189 static SemaphoreHandle_t xSemaphore = NULL;\r
190 \r
191 /* Holds the index to the next buffer from which data will be read. */\r
192 static volatile unsigned long ulNextRxBuffer = 0;\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* See the header file for descriptions of public functions. */\r
197 long lEMACSend( char *pcFrom, unsigned long ulLength, long lEndOfFrame )\r
198 {\r
199 static unsigned portBASE_TYPE uxTxBufferIndex = 0;\r
200 portBASE_TYPE xWaitCycles = 0;\r
201 long lReturn = pdPASS;\r
202 char *pcBuffer;\r
203 unsigned long ulLastBuffer, ulDataBuffered = 0, ulDataRemainingToSend, ulLengthToSend;\r
204 \r
205         /* If the length of data to be transmitted is greater than each individual\r
206         transmit buffer then the data will be split into more than one buffer.\r
207         Loop until the entire length has been buffered. */\r
208         while( ulDataBuffered < ulLength )\r
209         {\r
210                 /* Is a buffer available? */\r
211                 while( !( xTxDescriptors[ uxTxBufferIndex ].U_Status.status & AT91C_TRANSMIT_OK ) )\r
212                 {\r
213                         /* There is no room to write the Tx data to the Tx buffer.  Wait a\r
214                         short while, then try again. */\r
215                         xWaitCycles++;\r
216                         if( xWaitCycles > emacMAX_WAIT_CYCLES )\r
217                         {\r
218                                 /* Give up. */\r
219                                 lReturn = pdFAIL;\r
220                                 break;\r
221                         }\r
222                         else\r
223                         {\r
224                                 vTaskDelay( emacBUFFER_WAIT_DELAY );\r
225                         }\r
226                 }\r
227         \r
228                 /* lReturn will only be pdPASS if a buffer is available. */\r
229                 if( lReturn == pdPASS )\r
230                 {\r
231                         portENTER_CRITICAL();\r
232                         {\r
233                                 /* Get the address of the buffer from the descriptor, then copy \r
234                                 the data into the buffer. */\r
235                                 pcBuffer = ( char * ) xTxDescriptors[ uxTxBufferIndex ].addr;\r
236 \r
237                                 /* How much can we write to the buffer? */\r
238                                 ulDataRemainingToSend = ulLength - ulDataBuffered;\r
239                                 if( ulDataRemainingToSend <= ETH_TX_BUFFER_SIZE )\r
240                                 {\r
241                                         /* We can write all the remaining bytes. */\r
242                                         ulLengthToSend = ulDataRemainingToSend;\r
243                                 }\r
244                                 else\r
245                                 {\r
246                                         /* We can not write more than ETH_TX_BUFFER_SIZE in one go. */\r
247                                         ulLengthToSend = ETH_TX_BUFFER_SIZE;\r
248                                 }\r
249 \r
250                                 /* Copy the data into the buffer. */\r
251                                 memcpy( ( void * ) pcBuffer, ( void * ) &( pcFrom[ ulDataBuffered ] ), ulLengthToSend );\r
252                                 ulDataBuffered += ulLengthToSend;\r
253 \r
254                                 /* Is this the last data for the frame? */\r
255                                 if( lEndOfFrame && ( ulDataBuffered >= ulLength ) )\r
256                                 {\r
257                                         /* No more data remains for this frame so we can start the \r
258                                         transmission. */\r
259                                         ulLastBuffer = AT91C_LAST_BUFFER;\r
260                                 }\r
261                                 else\r
262                                 {\r
263                                         /* More data to come for this frame. */\r
264                                         ulLastBuffer = 0;\r
265                                 }\r
266         \r
267                                 /* Fill out the necessary in the descriptor to get the data sent, \r
268                                 then move to the next descriptor, wrapping if necessary. */\r
269                                 if( uxTxBufferIndex >= ( NB_TX_BUFFERS - 1 ) )\r
270                                 {                               \r
271                                         xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )\r
272                                                                                                                                                         | ulLastBuffer\r
273                                                                                                                                                         | AT91C_TRANSMIT_WRAP;\r
274                                         uxTxBufferIndex = 0;\r
275                                 }\r
276                                 else\r
277                                 {\r
278                                         xTxDescriptors[ uxTxBufferIndex ].U_Status.status =     ( ulLengthToSend & ( unsigned long ) AT91C_LENGTH_FRAME )\r
279                                                                                                                                                         | ulLastBuffer;\r
280                                         uxTxBufferIndex++;\r
281                                 }\r
282         \r
283                                 /* If this is the last buffer to be sent for this frame we can\r
284                                 start the transmission. */\r
285                                 if( ulLastBuffer )\r
286                                 {\r
287                                         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;\r
288                                 }\r
289                         }\r
290                         portEXIT_CRITICAL();\r
291                 }\r
292                 else\r
293                 {\r
294                         break;\r
295                 }\r
296         }\r
297 \r
298         return lReturn;\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 /* See the header file for descriptions of public functions. */\r
303 unsigned long ulEMACInputLength( void )\r
304 {\r
305 register unsigned long ulIndex, ulLength = 0;\r
306 \r
307         /* Skip any fragments.  We are looking for the first buffer that contains\r
308         data and has the SOF (start of frame) bit set. */\r
309         while( ( xRxDescriptors[ ulNextRxBuffer ].addr & AT91C_OWNERSHIP_BIT ) && !( xRxDescriptors[ ulNextRxBuffer ].U_Status.status & AT91C_SOF ) )\r
310         {\r
311                 /* Ignoring this buffer.  Mark it as free again. */\r
312                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );              \r
313                 ulNextRxBuffer++;\r
314                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
315                 {\r
316                         ulNextRxBuffer = 0;\r
317                 }\r
318         }\r
319 \r
320         /* We are going to walk through the descriptors that make up this frame, \r
321         but don't want to alter ulNextRxBuffer as this would prevent vEMACRead()\r
322         from finding the data.  Therefore use a copy of ulNextRxBuffer instead. */\r
323         ulIndex = ulNextRxBuffer;\r
324 \r
325         /* Walk through the descriptors until we find the last buffer for this \r
326         frame.  The last buffer will give us the length of the entire frame. */\r
327         while( ( xRxDescriptors[ ulIndex ].addr & AT91C_OWNERSHIP_BIT ) && !ulLength )\r
328         {\r
329                 ulLength = xRxDescriptors[ ulIndex ].U_Status.status & emacRX_LENGTH_FRAME;\r
330 \r
331                 /* Increment to the next buffer, wrapping if necessary. */\r
332                 ulIndex++;\r
333                 if( ulIndex >= NB_RX_BUFFERS )\r
334                 {\r
335                         ulIndex = 0;\r
336                 }\r
337         }\r
338 \r
339         return ulLength;\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 /* See the header file for descriptions of public functions. */\r
344 void vEMACRead( char *pcTo, unsigned long ulSectionLength, unsigned long ulTotalFrameLength )\r
345 {\r
346 static unsigned long ulSectionBytesReadSoFar = 0, ulBufferPosition = 0, ulFameBytesReadSoFar = 0;\r
347 static char *pcSource;\r
348 register unsigned long ulBytesRemainingInBuffer, ulRemainingSectionBytes;\r
349 \r
350         /* Read ulSectionLength bytes from the Rx buffers.  This is not necessarily any\r
351         correspondence between the length of our Rx buffers, and the length of the\r
352         data we are returning or the length of the data being requested.  Therefore, \r
353         between calls  we have to remember not only which buffer we are currently \r
354         processing, but our position within that buffer.  This would be greatly\r
355         simplified if PBUF_POOL_BUFSIZE could be guaranteed to be greater than\r
356         the size of each Rx buffer, and that memory fragmentation did not occur.\r
357         \r
358         This function should only be called after a call to ulEMACInputLength().\r
359         This will ensure ulNextRxBuffer is set to the correct buffer. */\r
360 \r
361 \r
362 \r
363         /* vEMACRead is called with pcTo set to NULL to indicate that we are about\r
364         to read a new frame.  Any fragments remaining in the frame we were \r
365         processing during the last call should be dropped. */\r
366         if( pcTo == NULL )\r
367         {\r
368                 /* How many bytes are indicated as being in this buffer?  If none then\r
369                 the buffer is completely full and the frame is contained within more\r
370                 than one buffer. */\r
371 \r
372                 /* Reset our state variables ready for the next read from this buffer. */\r
373         pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
374         ulFameBytesReadSoFar = ( unsigned long ) 0;\r
375                 ulBufferPosition = ( unsigned long ) 0;\r
376         }\r
377         else\r
378         {\r
379                 /* Loop until we have obtained the required amount of data. */\r
380         ulSectionBytesReadSoFar = 0;\r
381                 while( ulSectionBytesReadSoFar < ulSectionLength )\r
382                 {\r
383                         /* We may have already read some data from this buffer.  How much\r
384                         data remains in the buffer? */\r
385                         ulBytesRemainingInBuffer = ( ETH_RX_BUFFER_SIZE - ulBufferPosition );\r
386 \r
387                         /* How many more bytes do we need to read before we have the \r
388                         required amount of data? */\r
389             ulRemainingSectionBytes = ulSectionLength - ulSectionBytesReadSoFar;\r
390 \r
391                         /* Do we want more data than remains in the buffer? */\r
392                         if( ulRemainingSectionBytes > ulBytesRemainingInBuffer )\r
393                         {\r
394                                 /* We want more data than remains in the buffer so we can \r
395                                 write the remains of the buffer to the destination, then move\r
396                                 onto the next buffer to get the rest. */\r
397                                 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulBytesRemainingInBuffer );\r
398                                 ulSectionBytesReadSoFar += ulBytesRemainingInBuffer;\r
399                 ulFameBytesReadSoFar += ulBytesRemainingInBuffer;\r
400 \r
401                                 /* Mark the buffer as free again. */\r
402                                 xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
403 \r
404                                 /* Move onto the next buffer. */\r
405                                 ulNextRxBuffer++;\r
406                                 if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
407                                 {\r
408                                         ulNextRxBuffer = ( unsigned long ) 0;\r
409                                 }\r
410 \r
411                                 /* Reset the variables for the new buffer. */\r
412                                 pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
413                                 ulBufferPosition = ( unsigned long ) 0;\r
414                         }\r
415                         else\r
416                         {\r
417                                 /* We have enough data in this buffer to send back.  Read out\r
418                                 enough data and remember how far we read up to. */\r
419                                 memcpy( &( pcTo[ ulSectionBytesReadSoFar ] ), &( pcSource[ ulBufferPosition ] ), ulRemainingSectionBytes );\r
420 \r
421                                 /* There may be more data in this buffer yet.  Increment our \r
422                                 position in this buffer past the data we have just read. */\r
423                                 ulBufferPosition += ulRemainingSectionBytes;\r
424                                 ulSectionBytesReadSoFar += ulRemainingSectionBytes;\r
425                 ulFameBytesReadSoFar += ulRemainingSectionBytes;\r
426 \r
427                                 /* Have we now finished with this buffer? */\r
428                                 if( ( ulBufferPosition >= ETH_RX_BUFFER_SIZE ) || ( ulFameBytesReadSoFar >= ulTotalFrameLength ) )\r
429                                 {\r
430                                         /* Mark the buffer as free again. */\r
431                                         xRxDescriptors[ ulNextRxBuffer ].addr &= ~( AT91C_OWNERSHIP_BIT );\r
432 \r
433                                         /* Move onto the next buffer. */\r
434                                         ulNextRxBuffer++;\r
435                                         if( ulNextRxBuffer >= NB_RX_BUFFERS )\r
436                                         {\r
437                                                 ulNextRxBuffer = 0;\r
438                                         }\r
439         \r
440                                         pcSource = ( char * )( xRxDescriptors[ ulNextRxBuffer ].addr & emacADDRESS_MASK );\r
441                                         ulBufferPosition = 0;\r
442                                 }\r
443                         }\r
444                 }\r
445         }\r
446 }\r
447 /*-----------------------------------------------------------*/\r
448 \r
449 /* See the header file for descriptions of public functions. */\r
450 SemaphoreHandle_t xEMACInit( void )\r
451 {\r
452         /* Code supplied by Atmel -------------------------------*/\r
453 \r
454         /* Disable pull up on RXDV => PHY normal mode (not in test mode),\r
455         PHY has internal pull down. */\r
456         AT91C_BASE_PIOB->PIO_PPUDR = 1 << 15;\r
457 \r
458         #if USE_RMII_INTERFACE != 1\r
459                 /* PHY has internal pull down : set MII mode. */\r
460                 AT91C_BASE_PIOB->PIO_PPUDR = 1 << 16;\r
461         #endif\r
462 \r
463         /* Clear PB18 <=> PHY powerdown. */\r
464         AT91C_BASE_PIOB->PIO_PER = 1 << 18;\r
465         AT91C_BASE_PIOB->PIO_OER = 1 << 18;\r
466         AT91C_BASE_PIOB->PIO_CODR = 1 << 18;\r
467 \r
468         /* After PHY power up, hardware reset. */\r
469         AT91C_BASE_RSTC->RSTC_RMR = emacRESET_KEY | emacRESET_LENGTH;\r
470         AT91C_BASE_RSTC->RSTC_RCR = emacRESET_KEY | AT91C_RSTC_EXTRST;\r
471 \r
472         /* Wait for hardware reset end. */\r
473         while( !( AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL ) )\r
474         {\r
475                 __asm volatile ( "NOP" );\r
476         }\r
477     __asm volatile ( "NOP" );\r
478 \r
479         /* Setup the pins. */\r
480         AT91C_BASE_PIOB->PIO_ASR = emacPERIPHERAL_A_SETUP;\r
481         AT91C_BASE_PIOB->PIO_PDR = emacPERIPHERAL_A_SETUP;\r
482 \r
483         /* Enable com between EMAC PHY.\r
484 \r
485         Enable management port. */\r
486         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
487 \r
488         /* MDC = MCK/32. */\r
489         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;     \r
490 \r
491         /* Wait for PHY auto init end (rather crude delay!). */\r
492         vTaskDelay( emacPHY_INIT_DELAY );\r
493 \r
494         /* PHY configuration. */\r
495         #if USE_RMII_INTERFACE != 1\r
496         {\r
497                 unsigned long ulControl;\r
498 \r
499                 /* PHY has internal pull down : disable MII isolate. */\r
500                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
501                 vReadPHY( AT91C_PHY_ADDR, MII_BMCR, &ulControl );\r
502                 ulControl &= ~BMCR_ISOLATE;\r
503                 vWritePHY( AT91C_PHY_ADDR, MII_BMCR, ulControl );\r
504         }\r
505         #endif\r
506 \r
507         /* Disable management port again. */\r
508         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;\r
509 \r
510         #if USE_RMII_INTERFACE != 1\r
511                 /* Enable EMAC in MII mode, enable clock ERXCK and ETXCK. */\r
512                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN ;\r
513         #else\r
514                 /* Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator\r
515                 on ERFCK). */\r
516                 AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;\r
517         #endif\r
518 \r
519         /* End of code supplied by Atmel ------------------------*/\r
520 \r
521         /* Setup the buffers and descriptors. */\r
522         prvSetupDescriptors();\r
523         \r
524         /* Load our MAC address into the EMAC. */\r
525         prvSetupMACAddress();\r
526 \r
527         /* Are we connected? */\r
528         if( prvProbePHY() )\r
529         {\r
530                 /* Enable the interrupt! */\r
531                 portENTER_CRITICAL();\r
532                 {\r
533                         prvSetupEMACInterrupt();\r
534                         vPassEMACSemaphore( xSemaphore );\r
535                 }\r
536                 portEXIT_CRITICAL();\r
537         }\r
538 \r
539         return xSemaphore;\r
540 }\r
541 /*-----------------------------------------------------------*/\r
542 \r
543 /* See the header file for descriptions of public functions. */\r
544 void vClearEMACTxBuffer( void )\r
545 {\r
546 static unsigned portBASE_TYPE uxNextBufferToClear = 0;\r
547 \r
548         /* Called on Tx interrupt events to reset the AT91C_TRANSMIT_OK bit in each \r
549         Tx buffer within the frame just transmitted.  This marks all the buffers\r
550         as available again.\r
551 \r
552         The first buffer in the frame should have the bit set automatically. */\r
553         if( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_TRANSMIT_OK )\r
554         {\r
555                 /* Loop through the other buffers in the frame. */\r
556                 while( !( xTxDescriptors[ uxNextBufferToClear ].U_Status.status & AT91C_LAST_BUFFER ) )\r
557                 {\r
558                         uxNextBufferToClear++;\r
559 \r
560                         if( uxNextBufferToClear >= NB_TX_BUFFERS )\r
561                         {\r
562                                 uxNextBufferToClear = 0;\r
563                         }\r
564 \r
565                         xTxDescriptors[ uxNextBufferToClear ].U_Status.status |= AT91C_TRANSMIT_OK;\r
566                 }\r
567 \r
568                 /* Start with the next buffer the next time a Tx interrupt is called. */\r
569                 uxNextBufferToClear++;\r
570 \r
571                 /* Do we need to wrap back to the first buffer? */\r
572                 if( uxNextBufferToClear >= NB_TX_BUFFERS )\r
573                 {\r
574                         uxNextBufferToClear = 0;\r
575                 }\r
576         }\r
577 }\r
578 /*-----------------------------------------------------------*/\r
579 \r
580 static void prvSetupDescriptors(void)\r
581 {\r
582 unsigned portBASE_TYPE xIndex;\r
583 unsigned long ulAddress;\r
584 \r
585         /* Initialise xRxDescriptors descriptor. */\r
586         for( xIndex = 0; xIndex < NB_RX_BUFFERS; ++xIndex )\r
587         {\r
588                 /* Calculate the address of the nth buffer within the array. */\r
589                 ulAddress = ( unsigned long )( pcRxBuffer + ( xIndex * ETH_RX_BUFFER_SIZE ) );\r
590 \r
591                 /* Write the buffer address into the descriptor.  The DMA will place\r
592                 the data at this address when this descriptor is being used.  Mask off\r
593                 the bottom bits of the address as these have special meaning. */\r
594                 xRxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
595         }       \r
596 \r
597         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
598         to the first buffer. */\r
599         xRxDescriptors[ NB_RX_BUFFERS - 1 ].addr |= emacRX_WRAP_BIT;\r
600 \r
601         /* Initialise xTxDescriptors. */\r
602         for( xIndex = 0; xIndex < NB_TX_BUFFERS; ++xIndex )\r
603         {\r
604                 /* Calculate the address of the nth buffer within the array. */\r
605                 ulAddress = ( unsigned long )( pcTxBuffer + ( xIndex * ETH_TX_BUFFER_SIZE ) );\r
606 \r
607                 /* Write the buffer address into the descriptor.  The DMA will read\r
608                 data from here when the descriptor is being used. */\r
609                 xTxDescriptors[ xIndex ].addr = ulAddress & emacADDRESS_MASK;\r
610                 xTxDescriptors[ xIndex ].U_Status.status = AT91C_TRANSMIT_OK;\r
611         }       \r
612 \r
613         /* The last buffer has the wrap bit set so the EMAC knows to wrap back\r
614         to the first buffer. */\r
615         xTxDescriptors[ NB_TX_BUFFERS - 1 ].U_Status.status = AT91C_TRANSMIT_WRAP | AT91C_TRANSMIT_OK;\r
616 \r
617         /* Tell the EMAC where to find the descriptors. */\r
618         AT91C_BASE_EMAC->EMAC_RBQP = ( unsigned long ) xRxDescriptors;\r
619         AT91C_BASE_EMAC->EMAC_TBQP = ( unsigned long ) xTxDescriptors;\r
620         \r
621         /* Clear all the bits in the receive status register. */\r
622         AT91C_BASE_EMAC->EMAC_RSR = ( AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA );\r
623 \r
624         /* Enable the copy of data into the buffers, ignore broadcasts, \r
625         and don't copy FCS. */\r
626         AT91C_BASE_EMAC->EMAC_NCFGR |= ( AT91C_EMAC_CAF | AT91C_EMAC_NBC | AT91C_EMAC_DRFCS);\r
627 \r
628         /* Enable Rx and Tx, plus the stats register. */\r
629         AT91C_BASE_EMAC->EMAC_NCR |= ( AT91C_EMAC_TE | AT91C_EMAC_RE | AT91C_EMAC_WESTAT );\r
630 }       \r
631 /*-----------------------------------------------------------*/\r
632 \r
633 static void prvSetupMACAddress( void )\r
634 {\r
635         /* Must be written SA1L then SA1H. */\r
636         AT91C_BASE_EMAC->EMAC_SA1L =    ( ( unsigned long ) cMACAddress[ 3 ] << 24 ) |\r
637                                                                         ( ( unsigned long ) cMACAddress[ 2 ] << 16 ) |\r
638                                                                         ( ( unsigned long ) cMACAddress[ 1 ] << 8  ) |\r
639                                                                         cMACAddress[ 0 ];\r
640 \r
641         AT91C_BASE_EMAC->EMAC_SA1H =    ( ( unsigned long ) cMACAddress[ 5 ] << 8 ) |\r
642                                                                         cMACAddress[ 4 ];\r
643 }\r
644 /*-----------------------------------------------------------*/\r
645 \r
646 static void prvSetupEMACInterrupt( void )\r
647 {\r
648         /* Create the semaphore used to trigger the EMAC task. */\r
649         vSemaphoreCreateBinary( xSemaphore );\r
650         if( xSemaphore )\r
651         {\r
652                 /* We start by 'taking' the semaphore so the ISR can 'give' it when the\r
653                 first interrupt occurs. */\r
654                 xSemaphoreTake( xSemaphore, emacNO_DELAY );\r
655                 portENTER_CRITICAL();\r
656                 {\r
657                         /* We want to interrupt on Rx and Tx events. */\r
658                         AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;\r
659 \r
660                         /* Enable the interrupts in the AIC. */\r
661                         AT91F_AIC_ConfigureIt( AT91C_ID_EMAC, emacINTERRUPT_LEVEL, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vEMACISR_Wrapper );\r
662             AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_EMAC;\r
663                 }\r
664                 portEXIT_CRITICAL();\r
665         }\r
666 }\r
667 \r
668 \r
669 \r
670 \r
671 \r
672 /*\r
673  * The following functions are initialisation functions taken from the Atmel\r
674  * EMAC sample code.\r
675  */\r
676 \r
677 \r
678 static portBASE_TYPE prvProbePHY( void )\r
679 {\r
680 unsigned long ulPHYId1, ulPHYId2, ulStatus;\r
681 portBASE_TYPE xReturn = pdPASS;\r
682         \r
683         /* Code supplied by Atmel (reformatted) -----------------*/\r
684 \r
685         /* Enable management port */\r
686         AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;    \r
687         AT91C_BASE_EMAC->EMAC_NCFGR |= ( 2 ) << 10;\r
688 \r
689         /* Read the PHY ID. */\r
690         vReadPHY( AT91C_PHY_ADDR, MII_PHYSID1, &ulPHYId1 );\r
691         vReadPHY(AT91C_PHY_ADDR, MII_PHYSID2, &ulPHYId2 );\r
692 \r
693         /* AMD AM79C875:\r
694                         PHY_ID1 = 0x0022\r
695                         PHY_ID2 = 0x5541\r
696                         Bits 3:0 Revision Number Four bit manufacturer?s revision number.\r
697                                 0001 stands for Rev. A, etc.\r
698         */\r
699         if( ( ( ulPHYId1 << 16 ) | ( ulPHYId2 & 0xfff0 ) ) != MII_DM9161_ID )\r
700         {\r
701                 /* Did not expect this ID. */\r
702                 xReturn = pdFAIL;\r
703         }\r
704         else\r
705         {\r
706                 ulStatus = xGetLinkSpeed();\r
707 \r
708                 if( ulStatus != pdPASS )\r
709                 {\r
710                         xReturn = pdFAIL;\r
711                 }\r
712         }\r
713 \r
714         /* Disable management port */\r
715         AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;   \r
716 \r
717         /* End of code supplied by Atmel ------------------------*/\r
718 \r
719         return xReturn;\r
720 }\r
721 /*-----------------------------------------------------------*/\r
722 \r
723 static void vReadPHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long *pulValue )\r
724 {\r
725         /* Code supplied by Atmel (reformatted) ----------------------*/\r
726 \r
727         AT91C_BASE_EMAC->EMAC_MAN =     (AT91C_EMAC_SOF & (0x01<<30))\r
728                                                                         | (2 << 16) | (2 << 28)\r
729                                                                         | ((ucPHYAddress & 0x1f) << 23)\r
730                                                                         | (ucAddress << 18);\r
731 \r
732         /* Wait until IDLE bit in Network Status register is cleared. */\r
733         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
734         {\r
735                 __asm( "NOP" );\r
736         }\r
737 \r
738         *pulValue = ( AT91C_BASE_EMAC->EMAC_MAN & 0x0000ffff ); \r
739 \r
740         /* End of code supplied by Atmel ------------------------*/\r
741 }\r
742 /*-----------------------------------------------------------*/\r
743 \r
744 #if USE_RMII_INTERFACE != 1\r
745 static void vWritePHY( unsigned char ucPHYAddress, unsigned char ucAddress, unsigned long ulValue )\r
746 {\r
747         /* Code supplied by Atmel (reformatted) ----------------------*/\r
748 \r
749         AT91C_BASE_EMAC->EMAC_MAN = (( AT91C_EMAC_SOF & (0x01<<30))\r
750                                                                 | (2 << 16) | (1 << 28)\r
751                                                                 | ((ucPHYAddress & 0x1f) << 23)\r
752                                                                 | (ucAddress << 18))\r
753                                                                 | (ulValue & 0xffff);\r
754 \r
755         /* Wait until IDLE bit in Network Status register is cleared */\r
756         while( !( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE ) )\r
757         {\r
758                 __asm( "NOP" );\r
759         };\r
760 \r
761         /* End of code supplied by Atmel ------------------------*/\r
762 }\r
763 #endif\r
764 /*-----------------------------------------------------------*/\r
765 \r
766 static portBASE_TYPE xGetLinkSpeed( void )\r
767 {\r
768         unsigned long ulBMSR, ulBMCR, ulLPA, ulMACCfg, ulSpeed, ulDuplex;\r
769 \r
770         /* Code supplied by Atmel (reformatted) -----------------*/\r
771 \r
772         /* Link status is latched, so read twice to get current value */\r
773         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
774         vReadPHY(AT91C_PHY_ADDR, MII_BMSR, &ulBMSR);\r
775 \r
776         if( !( ulBMSR & BMSR_LSTATUS ) )\r
777         {       \r
778                 /* No Link. */\r
779                 return pdFAIL;\r
780         }\r
781 \r
782         vReadPHY(AT91C_PHY_ADDR, MII_BMCR, &ulBMCR);\r
783         if (ulBMCR & BMCR_ANENABLE)\r
784         {                               \r
785                 /* AutoNegotiation is enabled. */\r
786                 if (!(ulBMSR & BMSR_ANEGCOMPLETE))\r
787                 {\r
788                         /* Auto-negotitation in progress. */\r
789                         return pdFAIL;                          \r
790                 }               \r
791 \r
792                 vReadPHY(AT91C_PHY_ADDR, MII_LPA, &ulLPA);\r
793                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_100HALF ) )\r
794                 {\r
795                         ulSpeed = SPEED_100;\r
796                 }\r
797                 else\r
798                 {\r
799                         ulSpeed = SPEED_10;\r
800                 }\r
801 \r
802                 if( ( ulLPA & LPA_100FULL ) || ( ulLPA & LPA_10FULL ) )\r
803                 {\r
804                         ulDuplex = DUPLEX_FULL;\r
805                 }\r
806                 else\r
807                 {\r
808                         ulDuplex = DUPLEX_HALF;\r
809                 }\r
810         }\r
811         else\r
812         {\r
813                 ulSpeed = ( ulBMCR & BMCR_SPEED100 ) ? SPEED_100 : SPEED_10;\r
814                 ulDuplex = ( ulBMCR & BMCR_FULLDPLX ) ? DUPLEX_FULL : DUPLEX_HALF;\r
815         }\r
816 \r
817         /* Update the MAC */\r
818         ulMACCfg = AT91C_BASE_EMAC->EMAC_NCFGR & ~( AT91C_EMAC_SPD | AT91C_EMAC_FD );\r
819         if( ulSpeed == SPEED_100 )\r
820         {\r
821                 if( ulDuplex == DUPLEX_FULL )\r
822                 {\r
823                         /* 100 Full Duplex */\r
824                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD | AT91C_EMAC_FD;\r
825                 }\r
826                 else\r
827                 {                                       \r
828                         /* 100 Half Duplex */\r
829                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_SPD;\r
830                 }\r
831         }\r
832         else\r
833         {\r
834                 if (ulDuplex == DUPLEX_FULL)\r
835                 {\r
836                         /* 10 Full Duplex */\r
837                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg | AT91C_EMAC_FD;\r
838                 }\r
839                 else\r
840                 {                       /* 10 Half Duplex */\r
841                         AT91C_BASE_EMAC->EMAC_NCFGR = ulMACCfg;\r
842                 }\r
843         }\r
844 \r
845         /* End of code supplied by Atmel ------------------------*/\r
846 \r
847         return pdPASS;\r
848 }\r
849 /*-----------------------------------------------------------*/\r
850 \r
851 void vEMACWaitForInput( void )\r
852 {\r
853         /* Just wait until we are signled from an ISR that data is available, or\r
854         we simply time out. */\r
855         xSemaphoreTake( xSemaphore, emacBLOCK_TIME_WAITING_FOR_INPUT );\r
856 }\r