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