]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ColdFire_MCF52233_Eclipse/RTOSDemo/webserver/FEC.c
Change version numbers in preparation for V7.6.0 release.
[freertos] / FreeRTOS / Demo / ColdFire_MCF52233_Eclipse / RTOSDemo / webserver / FEC.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Kernel includes. */\r
67 #include "FreeRTOS.h"\r
68 #include "semphr.h"\r
69 #include "task.h"\r
70 \r
71 /* Hardware includes. */\r
72 #include "fecbd.h"\r
73 #include "mii.h"\r
74 #include "eth_phy.h"\r
75 #include "eth.h"\r
76 \r
77 /* uIP includes. */\r
78 #include "uip.h"\r
79 #include "uip_arp.h"\r
80 \r
81 /* Delay between polling the PHY to see if a link has been established. */\r
82 #define fecLINK_DELAY                                                   ( 500 / portTICK_RATE_MS )\r
83 \r
84 /* Delay to wait for an MII access. */\r
85 #define fecMII_DELAY                                                    ( 10 / portTICK_RATE_MS )\r
86 #define fecMAX_POLLS                                                    ( 20 )\r
87 \r
88 /* Constants used to delay while waiting for a tx descriptor to be free. */\r
89 #define fecMAX_WAIT_FOR_TX_BUFFER                                               ( 200 / portTICK_RATE_MS )\r
90 \r
91 /* We only use a single Tx descriptor which can lead to Txed packets being sent\r
92 twice (due to a bug in the FEC silicon).  However, in this case the bug is used\r
93 to our advantage in that it means the uip-split mechanism is not required. */\r
94 #define fecNUM_FEC_TX_BUFFERS                                   ( 1 )\r
95 #define fecTX_BUFFER_TO_USE                                             ( 0 )\r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* The semaphore used to wake the uIP task when data arrives. */\r
99 xSemaphoreHandle xFECSemaphore = NULL, xTxSemaphore = NULL;\r
100 \r
101 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
102 point to one of the Rx buffers to effect a zero copy policy. */\r
103 unsigned portCHAR *uip_buf;\r
104 \r
105 /* The DMA descriptors.  This is a char array to allow us to align it correctly. */\r
106 static unsigned portCHAR xFECTxDescriptors_unaligned[ ( fecNUM_FEC_TX_BUFFERS * sizeof( FECBD ) ) + 16 ];\r
107 static unsigned portCHAR xFECRxDescriptors_unaligned[ ( configNUM_FEC_RX_BUFFERS * sizeof( FECBD ) ) + 16 ];\r
108 static FECBD *xFECTxDescriptors;\r
109 static FECBD *xFECRxDescriptors;\r
110 \r
111 /* The DMA buffers.  These are char arrays to allow them to be aligned correctly. */\r
112 static unsigned portCHAR ucFECRxBuffers[ ( configNUM_FEC_RX_BUFFERS * configFEC_BUFFER_SIZE ) + 16 ];\r
113 static unsigned portBASE_TYPE uxNextRxBuffer = 0, uxIndexToBufferOwner = 0;\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * Enable all the required interrupts in the FEC and in the interrupt controller.\r
119  */\r
120 static void prvEnableFECInterrupts( void );\r
121 \r
122 /*\r
123  * Reset the FEC if we get into an unrecoverable state.\r
124  */\r
125 static void prvResetFEC( portBASE_TYPE xCalledFromISR );\r
126 \r
127 /********************************************************************/\r
128 \r
129 /*\r
130  * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE\r
131  *\r
132  * Write a value to a PHY's MII register.\r
133  *\r
134  * Parameters:\r
135  *  ch          FEC channel\r
136  *  phy_addr    Address of the PHY.\r
137  *  reg_addr    Address of the register in the PHY.\r
138  *  data        Data to be written to the PHY register.\r
139  *\r
140  * Return Values:\r
141  *  0 on failure\r
142  *  1 on success.\r
143  *\r
144  * Please refer to your PHY manual for registers and their meanings.\r
145  * mii_write() polls for the FEC's MII interrupt event and clears it.\r
146  * If after a suitable amount of time the event isn't triggered, a\r
147  * value of 0 is returned.\r
148  */\r
149 static int fec_mii_write( int phy_addr, int reg_addr, int data )\r
150 {\r
151 int timeout, iReturn;\r
152 uint32 eimr;\r
153 \r
154     /* Clear the MII interrupt bit */\r
155     MCF_FEC_EIR = MCF_FEC_EIR_MII;\r
156 \r
157     /* Mask the MII interrupt */\r
158     eimr = MCF_FEC_EIMR;\r
159     MCF_FEC_EIMR &= ~MCF_FEC_EIMR_MII;\r
160 \r
161     /* Write to the MII Management Frame Register to kick-off the MII write */\r
162     MCF_FEC_MMFR = MCF_FEC_MMFR_ST_01 | MCF_FEC_MMFR_OP_WRITE | MCF_FEC_MMFR_PA(phy_addr) | MCF_FEC_MMFR_RA(reg_addr) | MCF_FEC_MMFR_TA_10 | MCF_FEC_MMFR_DATA( data );\r
163 \r
164     /* Poll for the MII interrupt (interrupt should be masked) */\r
165     for( timeout = 0; timeout < fecMAX_POLLS; timeout++ )\r
166     {\r
167         if( MCF_FEC_EIR & MCF_FEC_EIR_MII )\r
168         {\r
169                         break;\r
170         }\r
171         else\r
172         {\r
173                 vTaskDelay( fecMII_DELAY );\r
174         }\r
175     }\r
176 \r
177     if( timeout == fecMAX_POLLS )\r
178     {\r
179         iReturn = 0;\r
180     }\r
181     else\r
182     {\r
183                 iReturn = 1;\r
184     }\r
185 \r
186         /* Clear the MII interrupt bit */\r
187         MCF_FEC_EIR = MCF_FEC_EIR_MII;\r
188 \r
189         /* Restore the EIMR */\r
190         MCF_FEC_EIMR = eimr;\r
191 \r
192     return iReturn;\r
193 }\r
194 \r
195 /********************************************************************/\r
196 /*\r
197  * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE\r
198  *\r
199  * Read a value from a PHY's MII register.\r
200  *\r
201  * Parameters:\r
202  *  ch          FEC channel\r
203  *  phy_addr    Address of the PHY.\r
204  *  reg_addr    Address of the register in the PHY.\r
205  *  data        Pointer to storage for the Data to be read\r
206  *              from the PHY register (passed by reference)\r
207  *\r
208  * Return Values:\r
209  *  0 on failure\r
210  *  1 on success.\r
211  *\r
212  * Please refer to your PHY manual for registers and their meanings.\r
213  * mii_read() polls for the FEC's MII interrupt event and clears it.\r
214  * If after a suitable amount of time the event isn't triggered, a\r
215  * value of 0 is returned.\r
216  */\r
217 static int fec_mii_read( int phy_addr, int reg_addr, unsigned portSHORT* data )\r
218 {\r
219 int timeout, iReturn;\r
220 uint32 eimr;\r
221 \r
222     /* Clear the MII interrupt bit */\r
223     MCF_FEC_EIR = MCF_FEC_EIR_MII;\r
224 \r
225     /* Mask the MII interrupt */\r
226     eimr = MCF_FEC_EIMR;\r
227     MCF_FEC_EIMR &= ~MCF_FEC_EIMR_MII;\r
228 \r
229     /* Write to the MII Management Frame Register to kick-off the MII read */\r
230     MCF_FEC_MMFR = MCF_FEC_MMFR_ST_01 | MCF_FEC_MMFR_OP_READ | MCF_FEC_MMFR_PA(phy_addr) | MCF_FEC_MMFR_RA(reg_addr) | MCF_FEC_MMFR_TA_10;\r
231 \r
232     /* Poll for the MII interrupt (interrupt should be masked) */\r
233     for( timeout = 0; timeout < fecMAX_POLLS; timeout++ )\r
234     {\r
235         if (MCF_FEC_EIR & MCF_FEC_EIR_MII)\r
236         {\r
237             break;\r
238         }\r
239         else\r
240         {\r
241                 vTaskDelay( fecMII_DELAY );\r
242         }\r
243     }\r
244 \r
245     if( timeout == fecMAX_POLLS )\r
246     {\r
247         iReturn = 0;\r
248     }\r
249     else\r
250     {\r
251                 *data = (uint16)(MCF_FEC_MMFR & 0x0000FFFF);\r
252                 iReturn = 1;\r
253     }\r
254 \r
255         /* Clear the MII interrupt bit */\r
256         MCF_FEC_EIR = MCF_FEC_EIR_MII;\r
257 \r
258         /* Restore the EIMR */\r
259         MCF_FEC_EIMR = eimr;\r
260 \r
261     return iReturn;\r
262 }\r
263 \r
264 \r
265 /********************************************************************/\r
266 /*\r
267  * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE\r
268  *\r
269  * Generate the hash table settings for the given address\r
270  *\r
271  * Parameters:\r
272  *  addr    48-bit (6 byte) Address to generate the hash for\r
273  *\r
274  * Return Value:\r
275  *  The 6 most significant bits of the 32-bit CRC result\r
276  */\r
277 static unsigned portCHAR fec_hash_address( const unsigned portCHAR* addr )\r
278 {\r
279 unsigned portLONG crc;\r
280 unsigned portCHAR byte;\r
281 int i, j;\r
282 \r
283         crc = 0xFFFFFFFF;\r
284         for(i=0; i<6; ++i)\r
285         {\r
286                 byte = addr[i];\r
287                 for(j=0; j<8; ++j)\r
288                 {\r
289                         if((byte & 0x01)^(crc & 0x01))\r
290                         {\r
291                                 crc >>= 1;\r
292                                 crc = crc ^ 0xEDB88320;\r
293                         }\r
294                         else\r
295                         {\r
296                                 crc >>= 1;\r
297                         }\r
298 \r
299                         byte >>= 1;\r
300                 }\r
301         }\r
302 \r
303         return (unsigned portCHAR)(crc >> 26);\r
304 }\r
305 \r
306 /********************************************************************/\r
307 /*\r
308  * FUNCTION ADAPTED FROM FREESCALE SUPPLIED SOURCE\r
309  *\r
310  * Set the Physical (Hardware) Address and the Individual Address\r
311  * Hash in the selected FEC\r
312  *\r
313  * Parameters:\r
314  *  ch  FEC channel\r
315  *  pa  Physical (Hardware) Address for the selected FEC\r
316  */\r
317 static void fec_set_address( const unsigned portCHAR *pa )\r
318 {\r
319         unsigned portCHAR crc;\r
320 \r
321         /*\r
322         * Set the Physical Address\r
323         */\r
324         /* Set the source address for the controller */\r
325         MCF_FEC_PALR = ( pa[ 0 ] << 24 ) | ( pa[ 1 ] << 16 ) | ( pa[ 2 ] << 8 ) | ( pa[ 3 ] << 0 );\r
326         MCF_FEC_PAUR = ( pa[ 4 ] << 24 ) | ( pa[ 5 ] << 16 );\r
327 \r
328         /*\r
329         * Calculate and set the hash for given Physical Address\r
330         * in the  Individual Address Hash registers\r
331         */\r
332         crc = fec_hash_address( pa );\r
333         if( crc >= 32 )\r
334         {\r
335                 MCF_FEC_IAUR |= (unsigned portLONG)(1 << (crc - 32));\r
336         }\r
337         else\r
338         {\r
339                 MCF_FEC_IALR |= (unsigned portLONG)(1 << crc);\r
340         }\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 static void prvInitialiseFECBuffers( void )\r
345 {\r
346 unsigned portBASE_TYPE ux;\r
347 unsigned portCHAR *pcBufPointer;\r
348 \r
349         /* Correctly align the Tx descriptor pointer. */\r
350         pcBufPointer = &( xFECTxDescriptors_unaligned[ 0 ] );\r
351         while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )\r
352         {\r
353                 pcBufPointer++;\r
354         }\r
355 \r
356         xFECTxDescriptors = ( FECBD * ) pcBufPointer;\r
357 \r
358         /* Likewise the Rx descriptor pointer. */\r
359         pcBufPointer = &( xFECRxDescriptors_unaligned[ 0 ] );\r
360         while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )\r
361         {\r
362                 pcBufPointer++;\r
363         }\r
364 \r
365         xFECRxDescriptors = ( FECBD * ) pcBufPointer;\r
366 \r
367 \r
368         /* Setup the Tx buffers and descriptors.  There is no separate Tx buffer\r
369         to point to (the Rx buffers are actually used) so the data member is\r
370         set to NULL for now. */\r
371         for( ux = 0; ux < fecNUM_FEC_TX_BUFFERS; ux++ )\r
372         {\r
373                 xFECTxDescriptors[ ux ].status = TX_BD_TC;\r
374                 xFECTxDescriptors[ ux ].data = NULL;\r
375                 xFECTxDescriptors[ ux ].length = 0;\r
376         }\r
377 \r
378         /* Setup the Rx buffers and descriptors, having first ensured correct\r
379         alignment. */\r
380         pcBufPointer = &( ucFECRxBuffers[ 0 ] );\r
381         while( ( ( unsigned portLONG ) pcBufPointer & 0x0fUL ) != 0 )\r
382         {\r
383                 pcBufPointer++;\r
384         }\r
385 \r
386         for( ux = 0; ux < configNUM_FEC_RX_BUFFERS; ux++ )\r
387         {\r
388             xFECRxDescriptors[ ux ].status = RX_BD_E;\r
389             xFECRxDescriptors[ ux ].length = configFEC_BUFFER_SIZE;\r
390             xFECRxDescriptors[ ux ].data = pcBufPointer;\r
391             pcBufPointer += configFEC_BUFFER_SIZE;\r
392         }\r
393 \r
394         /* Set the wrap bit in the last descriptors to form a ring. */\r
395         xFECTxDescriptors[ fecNUM_FEC_TX_BUFFERS - 1 ].status |= TX_BD_W;\r
396         xFECRxDescriptors[ configNUM_FEC_RX_BUFFERS - 1 ].status |= RX_BD_W;\r
397 \r
398         uxNextRxBuffer = 0;\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r
402 void vFECInit( void )\r
403 {\r
404 unsigned portSHORT usData;\r
405 struct uip_eth_addr xAddr;\r
406 unsigned portBASE_TYPE ux;\r
407 \r
408 /* The MAC address is set at the foot of FreeRTOSConfig.h. */\r
409 const unsigned portCHAR ucMACAddress[6] =\r
410 {\r
411         configMAC_0, configMAC_1,configMAC_2, configMAC_3, configMAC_4, configMAC_5\r
412 };\r
413 \r
414         /* Create the semaphore used by the ISR to wake the uIP task. */\r
415         vSemaphoreCreateBinary( xFECSemaphore );\r
416 \r
417         /* Create the semaphore used to unblock any tasks that might be waiting\r
418         for a Tx descriptor. */\r
419         vSemaphoreCreateBinary( xTxSemaphore );\r
420 \r
421         /* Initialise all the buffers and descriptors used by the DMA. */\r
422         prvInitialiseFECBuffers();\r
423 \r
424         for( usData = 0; usData < 6; usData++ )\r
425         {\r
426                 xAddr.addr[ usData ] = ucMACAddress[ usData ];\r
427         }\r
428         uip_setethaddr( xAddr );\r
429 \r
430         /* Set the Reset bit and clear the Enable bit */\r
431         MCF_FEC_ECR = MCF_FEC_ECR_RESET;\r
432 \r
433         /* Wait at least 8 clock cycles */\r
434         for( usData = 0; usData < 10; usData++ )\r
435         {\r
436                 asm( "NOP" );\r
437         }\r
438 \r
439         /* Set MII speed to 2.5MHz. */\r
440         MCF_FEC_MSCR = MCF_FEC_MSCR_MII_SPEED( ( ( ( configCPU_CLOCK_HZ / 1000000 ) / 5 ) + 1 ) );\r
441 \r
442         /* Initialize PLDPAR to enable Ethernet LEDs. */\r
443         MCF_GPIO_PLDPAR =  MCF_GPIO_PLDPAR_ACTLED_ACTLED | MCF_GPIO_PLDPAR_LINKLED_LINKLED | MCF_GPIO_PLDPAR_SPDLED_SPDLED\r
444                                          | MCF_GPIO_PLDPAR_DUPLED_DUPLED | MCF_GPIO_PLDPAR_COLLED_COLLED | MCF_GPIO_PLDPAR_RXLED_RXLED\r
445                                          | MCF_GPIO_PLDPAR_TXLED_TXLED;\r
446 \r
447         /* Initialize Port TA to enable Axcel control. */\r
448         MCF_GPIO_PTAPAR = 0x00;\r
449         MCF_GPIO_DDRTA  = 0x0F;\r
450         MCF_GPIO_PORTTA = 0x04;\r
451 \r
452         /* Set phy address to zero. */\r
453         MCF_EPHY_EPHYCTL1 = MCF_EPHY_EPHYCTL1_PHYADD( 0 );\r
454 \r
455         /* Enable EPHY module with PHY clocks disabled.  Do not turn on PHY clocks\r
456         until both FEC and EPHY are completely setup (see Below). */\r
457         MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0_DIS100 | MCF_EPHY_EPHYCTL0_DIS10);\r
458 \r
459         /* Enable auto_neg at start-up */\r
460         MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0 & (MCF_EPHY_EPHYCTL0_ANDIS));\r
461 \r
462         /* Enable EPHY module. */\r
463         MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0_EPHYEN | MCF_EPHY_EPHYCTL0);\r
464 \r
465         /* Let PHY PLLs be determined by PHY. */\r
466         MCF_EPHY_EPHYCTL0 = (uint8)(MCF_EPHY_EPHYCTL0  & ~(MCF_EPHY_EPHYCTL0_DIS100 | MCF_EPHY_EPHYCTL0_DIS10));\r
467 \r
468         /* Settle. */\r
469         vTaskDelay( fecLINK_DELAY );\r
470 \r
471         /* Can we talk to the PHY? */\r
472         do\r
473         {\r
474                 vTaskDelay( fecLINK_DELAY );\r
475                 usData = 0;\r
476                 fec_mii_read( configPHY_ADDRESS, PHY_PHYIDR1, &usData );\r
477 \r
478         } while( usData == 0xffff );\r
479 \r
480         do\r
481         {\r
482                 /* Start auto negotiate. */\r
483                 fec_mii_write( configPHY_ADDRESS, PHY_BMCR, ( PHY_BMCR_AN_RESTART | PHY_BMCR_AN_ENABLE ) );\r
484 \r
485                 /* Wait for auto negotiate to complete. */\r
486                 do\r
487                 {\r
488                         ux++;\r
489                         if( ux > 10 )\r
490                         {\r
491                                 /* Hardware bug workaround!  Force 100Mbps half duplex. */\r
492                                 while( !fec_mii_read( configPHY_ADDRESS, 0, &usData ) ){};\r
493                                 usData &= ~0x2000;                                                      /* 10Mbps */\r
494                                 usData &= ~0x0100;                                                      /* Half Duplex */\r
495                                 usData &= ~0x1000;                                                      /* Manual Mode */\r
496                                 while( !fec_mii_write( configPHY_ADDRESS, 0, usData ) ){};\r
497                                 while( !fec_mii_write( configPHY_ADDRESS, 0, (usData|0x0200) )){}; /* Force re-negotiate */\r
498                                 break;\r
499                         }\r
500                         vTaskDelay( fecLINK_DELAY );\r
501                         fec_mii_read( configPHY_ADDRESS, PHY_BMSR, &usData );\r
502 \r
503                 } while( !( usData & PHY_BMSR_AN_COMPLETE ) );\r
504 \r
505         } while( 0 ); //while( !( usData & PHY_BMSR_LINK ) );\r
506 \r
507         /* When we get here we have a link - find out what has been negotiated. */\r
508         fec_mii_read( configPHY_ADDRESS, PHY_ANLPAR, &usData );\r
509 \r
510         if( ( usData & PHY_ANLPAR_100BTX_FDX ) || ( usData & PHY_ANLPAR_100BTX ) )\r
511         {\r
512                 /* Speed is 100. */\r
513         }\r
514         else\r
515         {\r
516                 /* Speed is 10. */\r
517         }\r
518 \r
519         if( ( usData & PHY_ANLPAR_100BTX_FDX ) || ( usData & PHY_ANLPAR_10BTX_FDX ) )\r
520         {\r
521                 MCF_FEC_RCR &= (unsigned portLONG)~MCF_FEC_RCR_DRT;\r
522                 MCF_FEC_TCR |= MCF_FEC_TCR_FDEN;\r
523         }\r
524         else\r
525         {\r
526                 MCF_FEC_RCR |= MCF_FEC_RCR_DRT;\r
527                 MCF_FEC_TCR &= (unsigned portLONG)~MCF_FEC_TCR_FDEN;\r
528         }\r
529 \r
530         /* Clear the Individual and Group Address Hash registers */\r
531         MCF_FEC_IALR = 0;\r
532         MCF_FEC_IAUR = 0;\r
533         MCF_FEC_GALR = 0;\r
534         MCF_FEC_GAUR = 0;\r
535 \r
536         /* Set the Physical Address for the selected FEC */\r
537         fec_set_address( ucMACAddress );\r
538 \r
539         /* Set Rx Buffer Size */\r
540         MCF_FEC_EMRBR = (unsigned portSHORT)configFEC_BUFFER_SIZE;\r
541 \r
542         /* Point to the start of the circular Rx buffer descriptor queue */\r
543         MCF_FEC_ERDSR = ( volatile unsigned portLONG ) &( xFECRxDescriptors[ 0 ] );\r
544 \r
545         /* Point to the start of the circular Tx buffer descriptor queue */\r
546         MCF_FEC_ETSDR = ( volatile unsigned portLONG ) &( xFECTxDescriptors[ 0 ] );\r
547 \r
548         /* Mask all FEC interrupts */\r
549         MCF_FEC_EIMR = ( unsigned portLONG ) -1;\r
550 \r
551         /* Clear all FEC interrupt events */\r
552         MCF_FEC_EIR = ( unsigned portLONG ) -1;\r
553 \r
554         /* Initialize the Receive Control Register */\r
555         MCF_FEC_RCR = MCF_FEC_RCR_MAX_FL(ETH_MAX_FRM) | MCF_FEC_RCR_FCE;\r
556 \r
557         MCF_FEC_RCR |= MCF_FEC_RCR_MII_MODE;\r
558 \r
559         #if( configUSE_PROMISCUOUS_MODE == 1 )\r
560         {\r
561                 MCF_FEC_RCR |= MCF_FEC_RCR_PROM;\r
562         }\r
563         #endif\r
564 \r
565         prvEnableFECInterrupts();\r
566 \r
567         /* Finally... enable. */\r
568         MCF_FEC_ECR = MCF_FEC_ECR_ETHER_EN;\r
569         MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;\r
570 }\r
571 /*-----------------------------------------------------------*/\r
572 \r
573 static void prvEnableFECInterrupts( void )\r
574 {\r
575 const unsigned portBASE_TYPE uxFirstFECVector = 23, uxLastFECVector = 35;\r
576 unsigned portBASE_TYPE ux;\r
577 \r
578 #if configFEC_INTERRUPT_PRIORITY > configMAX_SYSCALL_INTERRUPT_PRIORITY\r
579         #error configFEC_INTERRUPT_PRIORITY must be less than or equal to configMAX_SYSCALL_INTERRUPT_PRIORITY\r
580 #endif\r
581 \r
582         /* Set the priority of each of the FEC interrupts. */\r
583         for( ux = uxFirstFECVector; ux <= uxLastFECVector; ux++ )\r
584         {\r
585                 MCF_INTC0_ICR( ux ) = MCF_INTC_ICR_IL( configFEC_INTERRUPT_PRIORITY );\r
586         }\r
587 \r
588         /* Enable the FEC interrupts in the mask register */\r
589         MCF_INTC0_IMRH &= ~( MCF_INTC_IMRH_INT_MASK33 | MCF_INTC_IMRH_INT_MASK34 | MCF_INTC_IMRH_INT_MASK35 );\r
590         MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK25 | MCF_INTC_IMRL_INT_MASK26 | MCF_INTC_IMRL_INT_MASK27\r
591                                                 | MCF_INTC_IMRL_INT_MASK28 | MCF_INTC_IMRL_INT_MASK29 | MCF_INTC_IMRL_INT_MASK30\r
592                                                 | MCF_INTC_IMRL_INT_MASK31 | MCF_INTC_IMRL_INT_MASK23 | MCF_INTC_IMRL_INT_MASK24\r
593                                                 | MCF_INTC_IMRL_MASKALL );\r
594 \r
595         /* Clear any pending FEC interrupt events */\r
596         MCF_FEC_EIR = MCF_FEC_EIR_CLEAR_ALL;\r
597 \r
598         /* Unmask all FEC interrupts */\r
599         MCF_FEC_EIMR = MCF_FEC_EIMR_UNMASK_ALL;\r
600 }\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 static void prvResetFEC( portBASE_TYPE xCalledFromISR )\r
604 {\r
605 portBASE_TYPE x;\r
606 \r
607         /* A critical section is used unless this function is being called from\r
608         an ISR. */\r
609         if( xCalledFromISR == pdFALSE )\r
610         {\r
611                 taskENTER_CRITICAL();\r
612         }\r
613 \r
614         {\r
615                 /* Reset all buffers and descriptors. */\r
616                 prvInitialiseFECBuffers();\r
617 \r
618                 /* Set the Reset bit and clear the Enable bit */\r
619                 MCF_FEC_ECR = MCF_FEC_ECR_RESET;\r
620 \r
621                 /* Wait at least 8 clock cycles */\r
622                 for( x = 0; x < 10; x++ )\r
623                 {\r
624                         asm( "NOP" );\r
625                 }\r
626 \r
627                 /* Re-enable. */\r
628                 MCF_FEC_ECR = MCF_FEC_ECR_ETHER_EN;\r
629                 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;\r
630         }\r
631 \r
632         if( xCalledFromISR == pdFALSE )\r
633         {\r
634                 taskEXIT_CRITICAL();\r
635         }\r
636 }\r
637 /*-----------------------------------------------------------*/\r
638 \r
639 unsigned short usFECGetRxedData( void )\r
640 {\r
641 unsigned portSHORT usLen;\r
642 \r
643         /* Obtain the size of the packet and put it into the "len" variable. */\r
644         usLen = xFECRxDescriptors[ uxNextRxBuffer ].length;\r
645 \r
646         if( ( usLen != 0 ) && ( ( xFECRxDescriptors[ uxNextRxBuffer ].status & RX_BD_E ) == 0 ) )\r
647         {\r
648                 uip_buf = xFECRxDescriptors[ uxNextRxBuffer ].data;\r
649         }\r
650         else\r
651         {\r
652                 usLen = 0;\r
653         }\r
654 \r
655         return usLen;\r
656 }\r
657 /*-----------------------------------------------------------*/\r
658 \r
659 void vFECRxProcessingCompleted( void )\r
660 {\r
661         /* Free the descriptor as the buffer it points to is no longer in use. */\r
662         xFECRxDescriptors[ uxNextRxBuffer ].status |= RX_BD_E;\r
663         MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;\r
664         uxNextRxBuffer++;\r
665         if( uxNextRxBuffer >= configNUM_FEC_RX_BUFFERS )\r
666         {\r
667                 uxNextRxBuffer = 0;\r
668         }\r
669 }\r
670 /*-----------------------------------------------------------*/\r
671 \r
672 void vFECSendData( void )\r
673 {\r
674         /* Ensure no Tx frames are outstanding. */\r
675         if( xSemaphoreTake( xTxSemaphore, fecMAX_WAIT_FOR_TX_BUFFER ) == pdPASS )\r
676         {\r
677                 /* Get a DMA buffer into which we can write the data to send. */\r
678                 if( xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].status & TX_BD_R )\r
679                 {\r
680                         /*** ERROR didn't expect this.  Sledge hammer error handling. ***/\r
681                         prvResetFEC( pdFALSE );\r
682 \r
683                         /* Make sure we leave the semaphore in the expected state as nothing\r
684                         is being transmitted this will not happen in the Tx ISR. */\r
685                         xSemaphoreGive( xTxSemaphore );\r
686                 }\r
687                 else\r
688                 {\r
689                         /* Setup the buffer descriptor for transmission.  The data being\r
690                         sent is actually stored in one of the Rx descriptor buffers,\r
691                         pointed to by uip_buf. */\r
692                         xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].length = uip_len;\r
693                         xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].status |= ( TX_BD_R | TX_BD_L );\r
694                         xFECTxDescriptors[ fecTX_BUFFER_TO_USE ].data = uip_buf;\r
695 \r
696                         /* Remember which Rx descriptor owns the buffer we are sending. */\r
697                         uxIndexToBufferOwner = uxNextRxBuffer;\r
698 \r
699                         /* We have finished with this Rx descriptor now. */\r
700                         uxNextRxBuffer++;\r
701                         if( uxNextRxBuffer >= configNUM_FEC_RX_BUFFERS )\r
702                         {\r
703                                 uxNextRxBuffer = 0;\r
704                         }\r
705 \r
706                         /* Continue the Tx DMA (in case it was waiting for a new TxBD) */\r
707                         MCF_FEC_TDAR = MCF_FEC_TDAR_X_DES_ACTIVE;\r
708                 }\r
709         }\r
710         else\r
711         {\r
712                 /* Gave up waiting.  Free the buffer back to the DMA. */\r
713                 vFECRxProcessingCompleted();\r
714         }\r
715 }\r
716 /*-----------------------------------------------------------*/\r
717 \r
718 void vFEC_ISR( void )\r
719 {\r
720 unsigned portLONG ulEvent;\r
721 portBASE_TYPE xHighPriorityTaskWoken = pdFALSE;\r
722 \r
723         /* This handler is called in response to any of the many separate FEC\r
724         interrupt. */\r
725 \r
726         /* Find the cause of the interrupt, then clear the interrupt. */\r
727         ulEvent = MCF_FEC_EIR & MCF_FEC_EIMR;\r
728         MCF_FEC_EIR = ulEvent;\r
729 \r
730         if( ( ulEvent & MCF_FEC_EIR_RXB ) || ( ulEvent & MCF_FEC_EIR_RXF ) )\r
731         {\r
732                 /* A packet has been received.  Wake the handler task. */\r
733                 xSemaphoreGiveFromISR( xFECSemaphore, &xHighPriorityTaskWoken );\r
734         }\r
735 \r
736         if( ulEvent & ( MCF_FEC_EIR_UN | MCF_FEC_EIR_RL | MCF_FEC_EIR_LC | MCF_FEC_EIR_EBERR | MCF_FEC_EIR_BABT | MCF_FEC_EIR_BABR | MCF_FEC_EIR_HBERR ) )\r
737         {\r
738                 /* Sledge hammer error handling. */\r
739                 prvResetFEC( pdTRUE );\r
740         }\r
741 \r
742         if( ( ulEvent & MCF_FEC_EIR_TXF ) || ( ulEvent & MCF_FEC_EIR_TXB ) )\r
743         {\r
744                 /* The buffer being sent is pointed to by an Rx descriptor, now the\r
745                 buffer has been sent we can mark the Rx descriptor as free again. */\r
746                 xFECRxDescriptors[ uxIndexToBufferOwner ].status |= RX_BD_E;\r
747                 MCF_FEC_RDAR = MCF_FEC_RDAR_R_DES_ACTIVE;\r
748                 xSemaphoreGiveFromISR( xTxSemaphore, &xHighPriorityTaskWoken );\r
749         }\r
750 \r
751         portEND_SWITCHING_ISR( xHighPriorityTaskWoken );\r
752 }\r
753 /*-----------------------------------------------------------*/\r
754 \r
755 /* Install the many different interrupt vectors, all of which call the same\r
756 handler function. */\r
757 void __attribute__ ((interrupt)) __cs3_isr_interrupt_87( void ) { vFEC_ISR(); }\r
758 void __attribute__ ((interrupt)) __cs3_isr_interrupt_88( void ) { vFEC_ISR(); }\r
759 void __attribute__ ((interrupt)) __cs3_isr_interrupt_89( void ) { vFEC_ISR(); }\r
760 void __attribute__ ((interrupt)) __cs3_isr_interrupt_90( void ) { vFEC_ISR(); }\r
761 void __attribute__ ((interrupt)) __cs3_isr_interrupt_91( void ) { vFEC_ISR(); }\r
762 void __attribute__ ((interrupt)) __cs3_isr_interrupt_92( void ) { vFEC_ISR(); }\r
763 void __attribute__ ((interrupt)) __cs3_isr_interrupt_93( void ) { vFEC_ISR(); }\r
764 void __attribute__ ((interrupt)) __cs3_isr_interrupt_94( void ) { vFEC_ISR(); }\r
765 void __attribute__ ((interrupt)) __cs3_isr_interrupt_95( void ) { vFEC_ISR(); }\r
766 void __attribute__ ((interrupt)) __cs3_isr_interrupt_96( void ) { vFEC_ISR(); }\r
767 void __attribute__ ((interrupt)) __cs3_isr_interrupt_97( void ) { vFEC_ISR(); }\r
768 void __attribute__ ((interrupt)) __cs3_isr_interrupt_98( void ) { vFEC_ISR(); }\r
769 void __attribute__ ((interrupt)) __cs3_isr_interrupt_99( void ) { vFEC_ISR(); }\r
770 \r
771 \r