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