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