source: rtems/c/src/lib/libbsp/m68k/genmcf548x/network/network.c @ d8d6a08

5
Last change on this file since d8d6a08 was d8d6a08, checked in by Sebastian Huber <sebastian.huber@…>, on 01/27/18 at 10:12:44

bsps: Move network define to source files

Define INSIDE_RTEMS_BSD_TCPIP_STACK in the network interface driver
source files to avoid some build system magic.

  • Property mode set to 100644
File size: 41.0 KB
Line 
1/*===============================================================*\
2| Project: RTEMS generic MCF548X BSP                              |
3+-----------------------------------------------------------------+
4| Partially based on the code references which are named below.   |
5| Adaptions, modifications, enhancements and any recent parts of  |
6| the code are:                                                   |
7|                    Copyright (c) 2009                           |
8|                    Embedded Brains GmbH                         |
9|                    Obere Lagerstr. 30                           |
10|                    D-82178 Puchheim                             |
11|                    Germany                                      |
12|                    rtems@embedded-brains.de                     |
13+-----------------------------------------------------------------+
14| The license and distribution terms for this file may be         |
15| found in the file LICENSE in this distribution or at            |
16|                                                                 |
17| http://www.rtems.org/license/LICENSE.                           |
18|                                                                 |
19+-----------------------------------------------------------------+
20| this file contains the networking driver                        |
21\*===============================================================*/
22/*
23 *  RTEMS/TCPIP driver for MCF548X FEC Ethernet
24 *
25 *  Modified for Motorola MPC5200 by Thomas Doerfler, <Thomas.Doerfler@imd-systems.de>
26 *  COPYRIGHT (c) 2003, IMD
27 *
28 *  Modified for Motorola IceCube (mgt5100) by Peter Rasmussen <prasmus@ipr-engineering.de>
29 *  COPYRIGHT (c) 2003, IPR Engineering
30 *
31 *  Parts of code are also under property of Driver Information Systems and based
32 *  on Motorola Proprietary Information.
33 *  COPYRIGHT (c) 2002 MOTOROLA INC.
34 *
35 *  Modified for Motorola MCF548X by Thomas Doerfler, <Thomas.Doerfler@imd-systems.de>
36 *  COPYRIGHT (c) 2009, IMD
37 *
38 */
39
40#define __INSIDE_RTEMS_BSD_TCPIP_STACK__
41
42#include <rtems.h>
43#include <rtems/error.h>
44#include <rtems/rtems_bsdnet.h>
45#include <stdio.h>
46#include <sys/param.h>
47#include <sys/mbuf.h>
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <net/if.h>
51#include <netinet/in.h>
52#include <netinet/if_ether.h>
53#include <net/if_var.h>
54
55#include <bsp.h>
56#include <bsp/irq-generic.h>
57#include <mcf548x/mcf548x.h>
58#include <rtems/rtems_mii_ioctl.h>
59#include <errno.h>
60
61/* freescale-api-specifics... */
62#include <mcf548x/MCD_dma.h>
63#include <mcf548x/mcdma_glue.h>
64
65/*
66 * Number of interfaces supported by this driver
67 */
68#define NIFACES 2
69
70#define FEC_WATCHDOG_TIMEOUT 5 /* check media every 5 seconds */
71
72#define DMA_BD_RX_NUM   32 /* Number of receive buffer descriptors      */
73#define DMA_BD_TX_NUM   32 /* Number of transmit buffer descriptors     */
74
75#define FEC_EVENT RTEMS_EVENT_0
76
77/*
78 * internal SRAM
79 * Layout:
80 * - RxBD channel 0
81 * - TxBD channel 0
82 * - RxBD channel 1
83 * - TxBD channel 1
84 * - DMA task memory
85 */
86extern char _SysSramBase[];
87#define SRAM_RXBD_BASE(base,chan) (((MCD_bufDescFec*)(base))    \
88  +((chan)                                                      \
89    *(DMA_BD_RX_NUM+DMA_BD_TX_NUM)))
90
91#define SRAM_TXBD_BASE(base,chan) (((MCD_bufDescFec*)(base))            \
92  +((chan)                                                              \
93    *(DMA_BD_RX_NUM+DMA_BD_TX_NUM)                                      \
94    +DMA_BD_RX_NUM))
95
96#define SRAM_DMA_BASE(base) ((void *)SRAM_RXBD_BASE(base,NIFACES+1))
97
98
99#undef ETH_DEBUG
100
101/*
102 * Default number of buffer descriptors set aside for this driver.
103 * The number of transmit buffer descriptors has to be quite large
104 * since a single frame often uses four or more buffer descriptors.
105 */
106#define RX_BUF_COUNT     DMA_BD_RX_NUM
107#define TX_BUF_COUNT     DMA_BD_TX_NUM
108#define TX_BD_PER_BUF    1
109
110#define INET_ADDR_MAX_BUF_SIZE (sizeof "255.255.255.255")
111
112#define MCF548X_FEC0_IRQ_VECTOR    (39+64)
113#define MCF548X_FEC1_IRQ_VECTOR    (38+64)
114
115#define MCF548X_FEC_IRQ_VECTOR(chan) (MCF548X_FEC0_IRQ_VECTOR           \
116                                      +(chan)*(MCF548X_FEC1_IRQ_VECTOR  \
117                                               -MCF548X_FEC0_IRQ_VECTOR))
118
119#define MCF548X_FEC_VECTOR2CHAN(vector) (((int)(vector)-MCF548X_FEC0_IRQ_VECTOR) \
120                                         /(MCF548X_FEC1_IRQ_VECTOR      \
121                                           -MCF548X_FEC0_IRQ_VECTOR))
122
123#define MCDMA_FEC_RX_CHAN(chan) (0 + NIFACES*(chan))
124#define MCDMA_FEC_TX_CHAN(chan) (1 + NIFACES*(chan))
125
126#define MCF548X_FEC0_RX_INITIATOR  (16)
127#define MCF548X_FEC1_RX_INITIATOR  (30)
128#define MCF548X_FEC_RX_INITIATOR(chan) (MCF548X_FEC0_RX_INITIATOR               \
129                                      +(chan)*(MCF548X_FEC1_RX_INITIATOR        \
130                                               -MCF548X_FEC0_RX_INITIATOR))
131#define MCF548X_FEC0_TX_INITIATOR  (17)
132#define MCF548X_FEC1_TX_INITIATOR  (31)
133#define MCF548X_FEC_TX_INITIATOR(chan) (MCF548X_FEC0_TX_INITIATOR               \
134                                      +(chan)*(MCF548X_FEC1_TX_INITIATOR        \
135                                               -MCF548X_FEC0_TX_INITIATOR))
136
137/* BD and parameters are stored in SRAM(refer to sdma.h) */
138#define MCF548X_FEC_BD_BASE    ETH_BD_BASE
139
140/* RBD bits definitions */
141#define MCF548X_FEC_RBD_EMPTY  0x8000   /* Buffer is empty */
142#define MCF548X_FEC_RBD_WRAP   0x2000   /* Last BD in ring */
143#define MCF548X_FEC_RBD_INT    0x1000   /* Interrupt */
144#define MCF548X_FEC_RBD_LAST   0x0800   /* Buffer is last in frame(useless) */
145#define MCF548X_FEC_RBD_MISS   0x0100   /* Miss bit for prom mode */
146#define MCF548X_FEC_RBD_BC     0x0080   /* The received frame is broadcast frame */
147#define MCF548X_FEC_RBD_MC     0x0040   /* The received frame is multicast frame */
148#define MCF548X_FEC_RBD_LG     0x0020   /* Frame length violation */
149#define MCF548X_FEC_RBD_NO     0x0010   /* Nonoctet align frame */
150#define MCF548X_FEC_RBD_SH     0x0008   /* Short frame, FEC does not support SH and this bit is always cleared */
151#define MCF548X_FEC_RBD_CR     0x0004   /* CRC error */
152#define MCF548X_FEC_RBD_OV     0x0002   /* Receive FIFO overrun */
153#define MCF548X_FEC_RBD_TR     0x0001   /* The receive frame is truncated */
154#define MCF548X_FEC_RBD_ERR    (MCF548X_FEC_RBD_LG  | \
155                                MCF548X_FEC_RBD_NO  | \
156                                MCF548X_FEC_RBD_CR  | \
157                                MCF548X_FEC_RBD_OV  | \
158                                MCF548X_FEC_RBD_TR)
159
160/* TBD bits definitions */
161#define MCF548X_FEC_TBD_READY  0x8000   /* Buffer is ready */
162#define MCF548X_FEC_TBD_WRAP   0x2000   /* Last BD in ring */
163#define MCF548X_FEC_TBD_INT    0x1000   /* Interrupt */
164#define MCF548X_FEC_TBD_LAST   0x0800   /* Buffer is last in frame */
165#define MCF548X_FEC_TBD_TC     0x0400   /* Transmit the CRC */
166#define MCF548X_FEC_TBD_ABC    0x0200   /* Append bad CRC */
167
168#define FEC_INTR_MASK_USED \
169(MCF548X_FEC_EIMR_LC   | MCF548X_FEC_EIMR_RL    | \
170 MCF548X_FEC_EIMR_XFUN | MCF548X_FEC_EIMR_XFERR | MCF548X_FEC_EIMR_RFERR)
171
172typedef enum {
173  FEC_STATE_RESTART_0,
174  FEC_STATE_RESTART_1,
175  FEC_STATE_NORMAL,
176} fec_state;
177
178/*
179 * Device data
180 */
181struct mcf548x_enet_struct {
182  struct arpcom           arpcom;
183  struct mbuf             **rxMbuf;
184  struct mbuf             **txMbuf;
185  int                     chan;
186  fec_state               state;
187  int                     acceptBroadcast;
188  int                     rxBdCount;
189  int                     txBdCount;
190  MCD_bufDescFec          *rxBd;
191  MCD_bufDescFec          *txBd;
192  int                     rxDmaChan; /* dma task */
193  int                     txDmaChan; /* dma task */
194  rtems_id                rxDaemonTid;
195  rtems_id                txDaemonTid;
196
197  /*
198   * MDIO/Phy info
199   */
200  struct rtems_mdio_info mdio_info;
201  int phy_default;
202  int phy_chan;    /* which fec channel services this phy access? */
203  int media_state; /* (last detected) state of media */
204
205  unsigned long           rxInterrupts;
206  unsigned long           rxNotLast;
207  unsigned long           rxGiant;
208  unsigned long           rxNonOctet;
209  unsigned long           rxBadCRC;
210  unsigned long           rxFIFOError;
211  unsigned long           rxCollision;
212
213  unsigned long           txInterrupts;
214  unsigned long           txDeferred;
215  unsigned long           txLateCollision;
216  unsigned long           txUnderrun;
217  unsigned long           txFIFOError;
218  unsigned long           txMisaligned;
219  unsigned long           rxNotFirst;
220  unsigned long           txRetryLimit;
221  };
222
223static struct mcf548x_enet_struct enet_driver[NIFACES];
224
225static void mcf548x_fec_restart(struct mcf548x_enet_struct *sc, rtems_id otherDaemon);
226
227static void fec_send_event(rtems_id task)
228{
229  rtems_bsdnet_event_send(task, FEC_EVENT);
230}
231
232static void fec_wait_for_event(void)
233{
234  rtems_event_set out;
235  rtems_bsdnet_event_receive(
236    FEC_EVENT,
237    RTEMS_EVENT_ANY | RTEMS_WAIT,
238    RTEMS_NO_TIMEOUT,
239    &out
240  );
241}
242
243static void mcf548x_fec_request_restart(struct mcf548x_enet_struct *sc)
244{
245  sc->state = FEC_STATE_RESTART_0;
246  fec_send_event(sc->txDaemonTid);
247  fec_send_event(sc->rxDaemonTid);
248}
249
250/*
251 * Function:    MCF548X_eth_addr_filter_set
252 *
253 * Description: Set individual address filter for unicast address and
254 *                              set physical address registers.
255 *
256 * Returns:             void
257 *
258 * Notes:
259 *
260 */
261static void mcf548x_eth_addr_filter_set(struct mcf548x_enet_struct *sc)  {
262  unsigned char *mac;
263  unsigned char currByte;                               /* byte for which to compute the CRC */
264  int           byte;                                   /* loop - counter */
265  int           bit;                                    /* loop - counter */
266  unsigned long crc = 0xffffffff;               /* initial value */
267  int chan     = sc->chan;
268
269 /*
270  * Get the mac address of ethernet controller
271  */
272  mac = (unsigned char *)(&sc->arpcom.ac_enaddr);
273
274 /*
275  * The algorithm used is the following:
276  * we loop on each of the six bytes of the provided address,
277  * and we compute the CRC by left-shifting the previous
278  * value by one position, so that each bit in the current
279  * byte of the address may contribute the calculation. If
280  * the latter and the MSB in the CRC are different, then
281  * the CRC value so computed is also ex-ored with the
282  * "polynomium generator". The current byte of the address
283  * is also shifted right by one bit at each iteration.
284  * This is because the CRC generatore in hardware is implemented
285  * as a shift-register with as many ex-ores as the radixes
286  * in the polynomium. This suggests that we represent the
287  * polynomiumm itsc as a 32-bit constant.
288  */
289  for(byte = 0; byte < 6; byte++)
290    {
291
292    currByte = mac[byte];
293
294    for(bit = 0; bit < 8; bit++)
295      {
296
297      if((currByte & 0x01) ^ (crc & 0x01))
298        {
299
300        crc >>= 1;
301        crc = crc ^ 0xedb88320;
302
303        }
304      else
305        {
306
307        crc >>= 1;
308
309        }
310
311      currByte >>= 1;
312
313      }
314
315    }
316
317    crc = crc >> 26;
318
319   /*
320    * Set individual hash table register
321    */
322    if(crc >= 32)
323      {
324
325        MCF548X_FEC_IAUR(chan) = (1 << (crc - 32));
326        MCF548X_FEC_IALR(chan) = 0;
327
328      }
329    else
330     {
331
332       MCF548X_FEC_IAUR(chan) = 0;
333       MCF548X_FEC_IALR(chan) = (1 << crc);
334
335     }
336
337   /*
338    * Set physical address
339    */
340    MCF548X_FEC_PALR(chan) = ((mac[0] << 24) +
341                              (mac[1] << 16) +
342                              (mac[2] <<  8) +
343                              mac[3]);
344    MCF548X_FEC_PAUR(chan) = ((mac[4] << 24)
345                              + (mac[5] << 16)) + 0x8808;
346
347   }
348
349
350/*
351 * Function:    mcf548x_eth_mii_read
352 *
353 * Description: Read a media independent interface (MII) register on an
354 *                              18-wire ethernet tranceiver (PHY). Please see your PHY
355 *                              documentation for the register map.
356 *
357 * Returns:             0 if ok
358 *
359 * Notes:
360 *
361 */
362int mcf548x_eth_mii_read(
363 int phyAddr,                          /* PHY number to access or -1       */
364 void *uarg,                           /* unit argument                    */
365 unsigned regAddr,                     /* register address                 */
366 uint32_t *retVal)                     /* ptr to read buffer               */
367{
368  struct mcf548x_enet_struct *sc = uarg;
369  int timeout = 0xffff;
370  int chan = sc->phy_chan;
371
372 /*
373  * reading from any PHY's register is done by properly
374  * programming the FEC's MII data register.
375  */
376  MCF548X_FEC_MMFR(chan) = (MCF548X_FEC_MMFR_ST_01    |
377                            MCF548X_FEC_MMFR_OP_READ  |
378                            MCF548X_FEC_MMFR_TA_10    |
379                            MCF548X_FEC_MMFR_PA(phyAddr) |
380                            MCF548X_FEC_MMFR_RA(regAddr));
381
382 /*
383  * wait for the related interrupt
384  */
385  while ((timeout--) && (!(MCF548X_FEC_EIR(chan) & MCF548X_FEC_EIR_MII)));
386
387  if(timeout == 0) {
388
389#ifdef ETH_DEBUG
390    iprintf ("Read MDIO failed..." "\r\n");
391#endif
392
393    return 1;
394
395  }
396
397 /*
398  * clear mii interrupt bit
399  */
400  MCF548X_FEC_EIR(chan) = MCF548X_FEC_EIR_MII;
401
402 /*
403  * it's now safe to read the PHY's register
404  */
405  *retVal = (unsigned short)  MCF548X_FEC_MMFR(chan);
406
407  return 0;
408
409}
410
411/*
412 * Function:    mcf548x_eth_mii_write
413 *
414 * Description: Write a media independent interface (MII) register on an
415 *                              18-wire ethernet tranceiver (PHY). Please see your PHY
416 *                              documentation for the register map.
417 *
418 * Returns:             Success (boolean)
419 *
420 * Notes:
421 *
422 */
423static int mcf548x_eth_mii_write(
424 int phyAddr,                          /* PHY number to access or -1       */
425 void *uarg,                           /* unit argument                    */
426 unsigned regAddr,                     /* register address                 */
427 uint32_t data)                        /* write data                       */
428{
429  struct mcf548x_enet_struct *sc = uarg;
430  int chan     = sc->phy_chan;
431  int timeout  = 0xffff;
432
433  MCF548X_FEC_MMFR(chan) = (MCF548X_FEC_MMFR_ST_01    |
434                            MCF548X_FEC_MMFR_OP_WRITE |
435                            MCF548X_FEC_MMFR_TA_10    |
436                            MCF548X_FEC_MMFR_PA(phyAddr) |
437                            MCF548X_FEC_MMFR_RA(regAddr) |
438                            MCF548X_FEC_MMFR_DATA(data));
439
440 /*
441  * wait for the MII interrupt
442  */
443  while ((timeout--) && (!(MCF548X_FEC_EIR(chan) & MCF548X_FEC_EIR_MII)));
444
445  if(timeout == 0)
446    {
447
448#ifdef ETH_DEBUG
449    iprintf ("Write MDIO failed..." "\r\n");
450#endif
451
452    return 1;
453
454    }
455
456 /*
457  * clear MII interrupt bit
458  */
459  MCF548X_FEC_EIR(chan) = MCF548X_FEC_EIR_MII;
460
461  return 0;
462
463  }
464
465
466/*
467 * Function:    mcf548x_fec_reset
468 *
469 * Description: Reset a running ethernet driver including the hardware
470 *                              FIFOs and the FEC.
471 *
472 * Returns:             Success (boolean)
473 *
474 * Notes:
475 *
476 */
477static void mcf548x_fec_reset(struct mcf548x_enet_struct *sc) {
478  volatile int delay;
479  int chan     = sc->chan;
480  /*
481   * Clear FIFO status registers
482   */
483  MCF548X_FEC_FECRFSR(chan) = ~0;
484  MCF548X_FEC_FECTFSR(chan) = ~0;
485
486  /*
487   * reset the FIFOs
488   */
489  MCF548X_FEC_FRST(chan) = 0x03000000;
490
491  for (delay = 0;delay < 16*4;delay++) {};
492
493  MCF548X_FEC_FRST(chan) = 0x01000000;
494
495  /*
496   * Issue a reset command to the FEC chip
497   */
498  MCF548X_FEC_ECR(chan) |= MCF548X_FEC_ECR_RESET;
499
500  /*
501   * wait at least 16 clock cycles
502   */
503  for (delay = 0;delay < 16*4;delay++) {};
504}
505
506
507/*
508 * Function:    mcf548x_fec_off
509 *
510 * Description: Stop the FEC and disable the ethernet SmartComm tasks.
511 *                              This function "turns off" the driver.
512 *
513 * Returns:             void
514 *
515 * Notes:
516 *
517 */
518void mcf548x_fec_off(struct mcf548x_enet_struct *sc)
519  {
520  int            counter = 0xffff;
521  int chan     = sc->chan;
522
523
524#if defined(ETH_DEBUG)
525  uint32_t phyStatus;
526  int i;
527
528  for(i = 0; i < 9; i++)
529    {
530
531    mcf548x_eth_mii_read(sc->phy_default, sc, i, &phyStatus);
532    iprintf ("Mii reg %d: 0x%04lx" "\r\n", i, phyStatus);
533
534    }
535
536  for(i = 16; i < 21; i++)
537    {
538
539    mcf548x_eth_mii_read(sc->phy_default, sc, i, &phyStatus);
540    iprintf ("Mii reg %d: 0x%04lx" "\r\n", i, phyStatus);
541
542    }
543  for(i = 0; i < 32; i++)
544    {
545
546    mcf548x_eth_mii_read(i, sc, 0, &phyStatus);
547    iprintf ("Mii Phy=%d, reg 0: 0x%04lx" "\r\n", i, phyStatus);
548
549    }
550#endif  /* ETH_DEBUG */
551
552 /*
553  * block FEC chip interrupts
554  */
555  MCF548X_FEC_EIMR(chan) = 0;
556
557 /*
558  * issue graceful stop command to the FEC transmitter if necessary
559  */
560  MCF548X_FEC_TCR(chan) |= MCF548X_FEC_TCR_GTS;
561
562 /*
563  * wait for graceful stop to register
564  * FIXME: add rtems_task_wake_after here, if it takes to long
565  */
566  while((counter--) && (!(  MCF548X_FEC_EIR(chan) & MCF548X_FEC_EIR_GRA)));
567
568  /*
569   * Disable the SmartDMA transmit and receive tasks.
570   */
571  MCD_killDma( sc->rxDmaChan );
572  MCD_killDma( sc->txDmaChan );
573 /*
574  * Disable transmit / receive interrupts
575  */
576  mcdma_glue_irq_disable(sc->txDmaChan);
577  mcdma_glue_irq_disable(sc->rxDmaChan);
578
579 /*
580  * Disable the Ethernet Controller
581  */
582  MCF548X_FEC_ECR(chan) &= ~(MCF548X_FEC_ECR_ETHER_EN);
583}
584
585/*
586 * MCF548X FEC interrupt handler
587 */
588void mcf548x_fec_irq_handler(rtems_vector_number vector)
589{
590  struct mcf548x_enet_struct *sc;
591  volatile uint32_t ievent;
592  int chan;
593
594  sc     = &(enet_driver[MCF548X_FEC_VECTOR2CHAN(vector)]);
595  chan   = sc->chan;
596  ievent = MCF548X_FEC_EIR(chan);
597
598  MCF548X_FEC_EIR(chan) = ievent;
599  /*
600   * check errors, update statistics
601   */
602  if (ievent & MCF548X_FEC_EIR_LC) {
603    sc->txLateCollision++;
604  }
605  if (ievent & MCF548X_FEC_EIR_RL) {
606    sc->txRetryLimit++;
607  }
608  if (ievent & MCF548X_FEC_EIR_XFUN) {
609    sc->txUnderrun++;
610  }
611  if (ievent & MCF548X_FEC_EIR_XFERR) {
612    sc->txFIFOError++;
613  }
614  if (ievent & MCF548X_FEC_EIR_RFERR) {
615    sc->rxFIFOError++;
616  }
617  /*
618   * fatal error ocurred?
619   */
620  if (ievent & (MCF548X_FEC_EIR_RFERR | MCF548X_FEC_EIR_XFERR)) {
621    MCF548X_FEC_EIMR(chan) &=~(MCF548X_FEC_EIMR_RFERR | MCF548X_FEC_EIMR_XFERR);
622    printk("fifo\n");
623    mcf548x_fec_request_restart(sc);
624  }
625}
626
627/*
628 * MCF548X DMA ethernet interrupt handler
629 */
630void mcf548x_mcdma_rx_irq_handler(void * param)
631{
632  struct mcf548x_enet_struct *sc = (struct mcf548x_enet_struct *)param;
633  /* Frame received? */
634  if(MCDMA_GET_PENDING(sc->rxDmaChan)) {
635    MCDMA_CLR_PENDING(sc->rxDmaChan);
636
637    mcdma_glue_irq_disable(sc->rxDmaChan);/*Disable receive ints*/
638    sc->rxInterrupts++;                 /* Rx int has occurred */
639    fec_send_event(sc->rxDaemonTid);
640  }
641}
642
643/*
644 * MCF548X DMA ethernet interrupt handler
645 */
646void mcf548x_mcdma_tx_irq_handler(void * param)
647{
648  struct mcf548x_enet_struct *sc = (struct mcf548x_enet_struct *)param;
649
650 /* Buffer transmitted or transmitter error? */
651  if(MCDMA_GET_PENDING(sc->txDmaChan)) {
652
653    MCDMA_CLR_PENDING(sc->txDmaChan);
654
655    mcdma_glue_irq_disable(sc->txDmaChan);/*Disable tx ints*/
656
657    sc->txInterrupts++; /* Tx int has occurred */
658
659    fec_send_event(sc->txDaemonTid);
660  }
661}
662
663/*
664 * Function:    mcf548x_fec_initialize_hardware
665 *
666 * Description: Configure the MCF548X FEC registers and enable the
667 *                              SmartComm tasks. This function "turns on" the driver.
668 *
669 * Returns:             void
670 *
671 * Notes:
672 *
673 */
674static void mcf548x_fec_initialize_hardware(struct mcf548x_enet_struct *sc)
675  {
676  int chan = sc->chan;
677
678 /*
679  * Reset mcf548x FEC
680  */
681  mcf548x_fec_reset(sc);
682
683 /*
684  * Clear FEC-Lite interrupt event register (IEVENT)
685  */
686  MCF548X_FEC_EIR(chan) = MCF548X_FEC_EIR_CLEAR_ALL;
687
688 /*
689  * Set interrupt mask register
690  */
691  MCF548X_FEC_EIMR(chan) = FEC_INTR_MASK_USED;
692  /*
693   * Set FEC-Lite receive control register (R_CNTRL)
694   * frame length=1518, MII mode for 18-wire-transceiver
695   */
696  MCF548X_FEC_RCR(chan) = (MCF548X_FEC_RCR_MAX_FL(ETHER_MAX_LEN)
697                           | MCF548X_FEC_RCR_FCE
698                           | MCF548X_FEC_RCR_MII_MODE);
699
700  /*
701   * Set FEC-Lite transmit control register (X_CNTRL)
702   * full-duplex, heartbeat disabled
703   */
704  MCF548X_FEC_TCR(chan) = MCF548X_FEC_TCR_FDEN;
705
706
707
708 /*
709  * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock(33Mhz)
710  * and do not drop the Preamble.
711  */
712  MCF548X_FEC_MSCR(chan) = MCF548X_FEC_MSCR_MII_SPEED(7); /* ipb_clk = 33 MHz */
713
714 /*
715  * Set Opcode/Pause Duration Register
716  */
717  MCF548X_FEC_PAUR(chan) = 0x00010020;
718
719  /*
720   * Set Rx FIFO alarm and granularity value
721   */
722  MCF548X_FEC_FECRFCR(chan) = (MCF548X_FEC_FECRFCR_FRM
723                               | MCF548X_FEC_FECRFCR_GR(0x7));
724  MCF548X_FEC_FECRFAR(chan) = MCF548X_FEC_FECRFAR_ALARM(256);
725
726  /*
727   * Set Tx FIFO granularity value
728   */
729  MCF548X_FEC_FECTFCR(chan) = (MCF548X_FEC_FECTFCR_FRM
730                               | MCF548X_FEC_FECTFCR_GR(7));
731
732  /*
733   * Set transmit fifo watermark register (X_WMRK), default = 64
734   */
735  MCF548X_FEC_FECTFAR(chan) = MCF548X_FEC_FECTFAR_ALARM(512);
736  MCF548X_FEC_FECTFWR(chan) = MCF548X_FEC_FECTFWR_X_WMRK_256;
737
738 /*
739  * Set individual address filter for unicast address
740  * and set physical address registers.
741  */
742  mcf548x_eth_addr_filter_set(sc);
743
744 /*
745  * Set multicast address filter
746  */
747  MCF548X_FEC_GAUR(chan) = 0x00000000;
748  MCF548X_FEC_GALR(chan) = 0x00000000;
749
750 /*
751  * enable CRC in finite state machine register
752  */
753  MCF548X_FEC_CTCWR(chan) = MCF548X_FEC_CTCWR_TFCW | MCF548X_FEC_CTCWR_CRC;
754  }
755
756
757/*
758 * Send packet (caller provides header).
759 */
760static void mcf548x_fec_tx_start(struct ifnet *ifp)
761  {
762
763  struct mcf548x_enet_struct *sc = ifp->if_softc;
764
765  ifp->if_flags |= IFF_OACTIVE;
766
767  fec_send_event(sc->txDaemonTid);
768
769  }
770
771static void fec_start_dma_and_controller(struct mcf548x_enet_struct *sc)
772{
773  int chan = sc->chan;
774  int mcdma_rc;
775      /*
776       * Enable the SmartDMA receive task.
777       */
778      mcdma_rc = MCD_startDma
779        (sc->rxDmaChan, /* the channel on which to run the DMA */
780         (void *)sc->rxBd, /* the address to move data from, or buffer-descriptor addr */
781         0,             /* the amount to increment the source address per transfer */
782         (void *)&MCF548X_FEC_FECRFDR(chan), /* the address to move data to */
783         0,             /* the amount to increment the destination address per transfer */
784         ETHER_MAX_LEN, /* the number of bytes to transfer independent of the transfer size */
785         0,             /* the number bytes in of each data movement (1, 2, or 4) */
786         MCF548X_FEC_RX_INITIATOR(chan), /* what device initiates the DMA */
787         2,  /* priority of the DMA */
788         0 /* flags describing the DMA */
789         | MCD_FECRX_DMA
790         | MCD_INTERRUPT
791         | MCD_TT_FLAGS_CW
792         | MCD_TT_FLAGS_RL
793         | MCD_TT_FLAGS_SP
794         ,
795         0 /* a description of byte swapping, bit swapping, and CRC actions */
796         | MCD_NO_CSUM
797         | MCD_NO_BYTE_SWAP
798         );
799      if (mcdma_rc != MCD_OK) {
800        rtems_panic("FEC: cannot start rx DMA");
801      }
802      mcdma_rc = MCD_startDma
803        (sc->txDmaChan, /* the channel on which to run the DMA */
804         (void *)sc->txBd, /* the address to move data from, or buffer-descriptor addr */
805         0,             /* the amount to increment the source address per transfer */
806         (void *)&MCF548X_FEC_FECTFDR(chan), /* the address to move data to */
807         0,             /* the amount to increment the destination address per transfer */
808         ETHER_MAX_LEN, /* the number of bytes to transfer independent of the transfer size */
809         0,             /* the number bytes in of each data movement (1, 2, or 4) */
810         MCF548X_FEC_TX_INITIATOR(chan), /* what device initiates the DMA */
811         1,  /* priority of the DMA */
812         0 /* flags describing the DMA */
813         | MCD_FECTX_DMA
814         | MCD_INTERRUPT
815         | MCD_TT_FLAGS_CW
816         | MCD_TT_FLAGS_RL
817         | MCD_TT_FLAGS_SP
818         ,
819         0 /* a description of byte swapping, bit swapping, and CRC actions */
820         | MCD_NO_CSUM
821         | MCD_NO_BYTE_SWAP
822         );
823      if (mcdma_rc != MCD_OK) {
824        rtems_panic("FEC: cannot start tx DMA");
825      }
826
827  /*
828   * Enable FEC-Lite controller
829   */
830  MCF548X_FEC_ECR(chan) |= MCF548X_FEC_ECR_ETHER_EN;
831}
832
833static void mcf548x_fec_restart(struct mcf548x_enet_struct *sc, rtems_id otherDaemon)
834{
835  if (sc->state == FEC_STATE_RESTART_1) {
836    mcf548x_fec_initialize_hardware(sc);
837    fec_start_dma_and_controller(sc);
838    sc->state = FEC_STATE_NORMAL;
839  } else {
840    sc->state = FEC_STATE_RESTART_1;
841  }
842
843  fec_send_event(otherDaemon);
844  while (sc->state != FEC_STATE_NORMAL) {
845    fec_wait_for_event();
846  }
847}
848
849static void fec_reset_bd_and_discard_tx_frames(
850  int bdCount,
851  MCD_bufDescFec *bdRing,
852  struct mbuf **mbufs
853)
854{
855  int bdIndex = 0;
856
857  for (bdIndex = 0; bdIndex < bdCount; ++bdIndex) {
858    bool bdIsLast = bdIndex == bdCount - 1;
859    struct mbuf *m = mbufs[bdIndex];
860
861    bdRing[bdIndex].statCtrl = bdIsLast ? MCF548X_FEC_TBD_WRAP : 0;
862
863    if (m != NULL) {
864      mbufs[bdIndex] = NULL;
865      m_free(m);
866    }
867  }
868}
869
870static void fec_reset_tx_dma(
871  int dmaChan,
872  int bdCount,
873  MCD_bufDescFec *bdRing,
874  struct mbuf **mbufs,
875  struct mbuf *m
876)
877{
878  if (m != NULL) {
879    m_freem(m);
880  }
881
882  MCD_killDma(dmaChan);
883
884  fec_reset_bd_and_discard_tx_frames(bdCount, bdRing, mbufs);
885}
886
887static struct mbuf *fec_next_fragment(
888  struct ifnet *ifp,
889  struct mbuf *m,
890  bool *isFirst
891)
892{
893  struct mbuf *n = NULL;
894
895  *isFirst = false;
896
897  while (true) {
898    if (m == NULL) {
899      IF_DEQUEUE(&ifp->if_snd, m);
900
901      if (m != NULL) {
902        *isFirst = true;
903      } else {
904        ifp->if_flags &= ~IFF_OACTIVE;
905
906        return NULL;
907      }
908    }
909
910    if (m->m_len > 0) {
911      break;
912    } else {
913      m = m_free(m);
914    }
915  }
916
917  n = m->m_next;
918  while (n != NULL && n->m_len <= 0) {
919    n = m_free(n);
920  }
921  m->m_next = n;
922
923  return m;
924}
925
926static bool fec_transmit(
927  struct ifnet *ifp,
928  int dmaChan,
929  int bdCount,
930  MCD_bufDescFec *bdRing,
931  struct mbuf **mbufs,
932  int *bdIndexPtr,
933  struct mbuf **mPtr,
934  MCD_bufDescFec **firstPtr
935)
936{
937  bool bdShortage = false;
938  int bdIndex = *bdIndexPtr;
939  struct mbuf *m = *mPtr;
940  MCD_bufDescFec *first = *firstPtr;
941
942  while (true) {
943    MCD_bufDescFec *bd = &bdRing[bdIndex];
944
945    MCDMA_CLR_PENDING(dmaChan);
946    if ((bd->statCtrl & MCF548X_FEC_TBD_READY) == 0) {
947      struct mbuf *done = mbufs[bdIndex];
948      bool isFirst = false;
949
950      if (done != NULL) {
951        m_free(done);
952        mbufs[bdIndex] = NULL;
953      }
954
955      m = fec_next_fragment(ifp, m, &isFirst);
956      if (m != NULL) {
957        bool bdIsLast = bdIndex == bdCount - 1;
958        u16 status = bdIsLast ? MCF548X_FEC_TBD_WRAP : 0;
959
960        bd->length = (u16) m->m_len;
961        bd->dataPointer = mtod(m, u32);
962
963        mbufs[bdIndex] = m;
964
965        rtems_cache_flush_multiple_data_lines(mtod(m, void *), m->m_len);
966
967        if (isFirst) {
968          first = bd;
969        } else {
970          status |= MCF548X_FEC_TBD_READY;
971        }
972
973        if (m->m_next != NULL) {
974          bd->statCtrl = status;
975        } else {
976          bd->statCtrl = status | MCF548X_FEC_TBD_INT | MCF548X_FEC_TBD_LAST;
977          first->statCtrl |= MCF548X_FEC_TBD_READY;
978          MCD_continDma(dmaChan);
979        }
980
981        m = m->m_next;
982      } else {
983        break;
984      }
985    } else {
986      bdShortage = true;
987      break;
988    }
989
990    if (bdIndex < bdCount - 1) {
991      ++bdIndex;
992    } else {
993      bdIndex = 0;
994    }
995  }
996
997  *bdIndexPtr = bdIndex;
998  *mPtr = m;
999  *firstPtr = first;
1000
1001  return bdShortage;
1002}
1003
1004static MCD_bufDescFec *fec_init_tx_dma(
1005  MCD_bufDescFec *bdRing,
1006  int bdCount
1007)
1008{
1009  int bdIndex;
1010
1011  for (bdIndex = 0; bdIndex < bdCount; ++bdIndex) {
1012    bool bdIsLast = bdIndex == bdCount - 1;
1013
1014    bdRing[bdIndex].dataPointer = 0;
1015    bdRing[bdIndex].length = 0;
1016    bdRing[bdIndex].statCtrl = bdIsLast ? MCF548X_FEC_RBD_WRAP : 0;
1017  }
1018
1019  return bdRing;
1020}
1021
1022static void mcf548x_fec_txDaemon(void *arg)
1023{
1024  struct mcf548x_enet_struct *sc = arg;
1025  struct ifnet *ifp = &sc->arpcom.ac_if;
1026  int dmaChan = sc->txDmaChan;
1027  int bdIndex = 0;
1028  int bdCount = sc->txBdCount;
1029  struct mbuf **mbufs = &sc->txMbuf[0];
1030  struct mbuf *m = NULL;
1031  MCD_bufDescFec *bdRing = fec_init_tx_dma(sc->txBd, bdCount);
1032  MCD_bufDescFec *first = NULL;
1033  bool bdShortage = false;
1034
1035  memset(mbufs, 0, bdCount * sizeof(*mbufs));
1036
1037  while (true) {
1038    if (bdShortage) {
1039      mcdma_glue_irq_enable(dmaChan);
1040    }
1041    fec_wait_for_event();
1042
1043    if (sc->state != FEC_STATE_NORMAL) {
1044      fec_reset_tx_dma(dmaChan, bdCount, bdRing, mbufs, m);
1045      mcf548x_fec_restart(sc, sc->rxDaemonTid);
1046      bdIndex = 0;
1047      m = NULL;
1048      first = NULL;
1049    }
1050
1051    bdShortage = fec_transmit(
1052      ifp,
1053      dmaChan,
1054      bdCount,
1055      bdRing,
1056      mbufs,
1057      &bdIndex,
1058      &m,
1059      &first
1060    );
1061  }
1062}
1063
1064static struct mbuf *fec_add_mbuf(
1065  int how,
1066  struct ifnet *ifp,
1067  MCD_bufDescFec *bd,
1068  bool bdIsLast
1069)
1070{
1071  struct mbuf *m;
1072
1073  MGETHDR(m, how, MT_DATA);
1074  if (m != NULL) {
1075    MCLGET(m, how);
1076    if ((m->m_flags & M_EXT) != 0) {
1077      m->m_pkthdr.rcvif = ifp;
1078
1079      rtems_cache_invalidate_multiple_data_lines(mtod(m, void *), ETHER_MAX_LEN);
1080
1081      bd->dataPointer = mtod(m, u32);
1082      bd->length = ETHER_MAX_LEN;
1083      bd->statCtrl = MCF548X_FEC_RBD_EMPTY
1084        | MCF548X_FEC_RBD_INT
1085        | (bdIsLast ? MCF548X_FEC_RBD_WRAP : 0);
1086    } else {
1087      m_free(m);
1088    }
1089  }
1090
1091  return m;
1092}
1093
1094static MCD_bufDescFec *fec_init_rx_dma(
1095  MCD_bufDescFec *bdRing,
1096  struct ifnet *ifp,
1097  int bdCount,
1098  struct mbuf **mbufs
1099)
1100{
1101  int bdIndex;
1102
1103  for (bdIndex = 0; bdIndex < bdCount; ++bdIndex) {
1104    bool bdIsLast = bdIndex == bdCount - 1;
1105
1106    mbufs[bdIndex] = fec_add_mbuf(M_WAIT, ifp, &bdRing[bdIndex], bdIsLast);
1107  }
1108
1109  return bdRing;
1110}
1111
1112static void fec_reset_rx_dma(
1113  int dmaChan,
1114  int bdCount,
1115  MCD_bufDescFec *bdRing
1116)
1117{
1118  int bdIndex;
1119
1120  MCD_killDma(dmaChan);
1121
1122  for (bdIndex = 0; bdIndex < bdCount - 1; ++bdIndex) {
1123    bdRing[bdIndex].length = ETHER_MAX_LEN;
1124    bdRing[bdIndex].statCtrl = MCF548X_FEC_RBD_EMPTY | MCF548X_FEC_RBD_INT;
1125  }
1126
1127  bdRing[bdIndex].length = ETHER_MAX_LEN;
1128  bdRing[bdIndex].statCtrl = MCF548X_FEC_RBD_EMPTY | MCF548X_FEC_RBD_INT | MCF548X_FEC_RBD_WRAP;
1129}
1130
1131static int fec_ether_input(
1132  struct ifnet *ifp,
1133  int dmaChan,
1134  int bdIndex,
1135  int bdCount,
1136  MCD_bufDescFec *bdRing,
1137  struct mbuf **mbufs
1138)
1139{
1140  while (true) {
1141    bool bdIsLast = bdIndex == bdCount - 1;
1142    MCD_bufDescFec *bd = &bdRing[bdIndex];
1143    struct mbuf *m = mbufs[bdIndex];
1144    struct mbuf *n;
1145    u16 status;
1146
1147    MCDMA_CLR_PENDING(dmaChan);
1148    status = bd->statCtrl;
1149
1150    if ((status & MCF548X_FEC_RBD_EMPTY) != 0) {
1151      break;
1152    }
1153
1154    n = fec_add_mbuf(0, ifp, bd, bdIsLast);
1155    if (n != NULL) {
1156      int len = bd->length - ETHER_HDR_LEN - ETHER_CRC_LEN;
1157      struct ether_header *eh = mtod(m, struct ether_header *);
1158
1159      m->m_len = len;
1160      m->m_pkthdr.len = len;
1161      m->m_data = mtod(m, char *) + ETHER_HDR_LEN;
1162
1163      ether_input(ifp, eh, m);
1164    } else {
1165      n = m;
1166    }
1167
1168    mbufs[bdIndex] = n;
1169
1170    if (bdIndex < bdCount - 1) {
1171      ++bdIndex;
1172    } else {
1173      bdIndex = 0;
1174    }
1175  }
1176
1177  return bdIndex;
1178}
1179
1180static void mcf548x_fec_rxDaemon(void *arg)
1181{
1182  struct mcf548x_enet_struct *sc = arg;
1183  struct ifnet *ifp = &sc->arpcom.ac_if;
1184  int dmaChan = sc->rxDmaChan;
1185  int bdIndex = 0;
1186  int bdCount = sc->rxBdCount;
1187  struct mbuf **mbufs = &sc->rxMbuf[0];
1188  MCD_bufDescFec *bdRing = fec_init_rx_dma(sc->rxBd, ifp, bdCount, mbufs);
1189
1190  while (true) {
1191    mcdma_glue_irq_enable(dmaChan);
1192    fec_wait_for_event();
1193
1194    bdIndex = fec_ether_input(ifp, dmaChan, bdIndex, bdCount, bdRing, mbufs);
1195
1196    if (sc->state != FEC_STATE_NORMAL) {
1197      fec_reset_rx_dma(dmaChan, bdCount, bdRing);
1198      mcf548x_fec_restart(sc, sc->txDaemonTid);
1199      bdIndex = 0;
1200    }
1201  }
1202}
1203
1204/*
1205 * Initialize and start the device
1206 */
1207static void mcf548x_fec_init(void *arg)
1208{
1209  struct mcf548x_enet_struct *sc = (struct mcf548x_enet_struct *)arg;
1210  struct ifnet *ifp = &sc->arpcom.ac_if;
1211  int chan = sc->chan;
1212  rtems_isr_entry old_handler;
1213  char *txTaskName = "FTx0";
1214  char *rxTaskName = "FRx0";
1215  if(sc->txDaemonTid == 0)
1216    {
1217      /*
1218       * Allocate a set of BDs
1219       */
1220      sc->rxBd =  SRAM_RXBD_BASE(_SysSramBase,chan);
1221      sc->txBd =  SRAM_TXBD_BASE(_SysSramBase,chan);
1222
1223      if(!sc->rxBd || !sc->txBd)
1224        rtems_panic ("No memory for BDs");
1225      /*
1226       * clear the BDs
1227       */
1228      memset((void *)sc->rxBd,0,sc->rxBdCount * sizeof *(sc->rxBd));
1229      memset((void *)sc->txBd,0,sc->txBdCount * sizeof *(sc->txBd));
1230      /*
1231       * Allocate a set of mbuf pointers
1232       */
1233      sc->rxMbuf =
1234        malloc(sc->rxBdCount * sizeof *sc->rxMbuf, M_MBUF, M_NOWAIT);
1235      sc->txMbuf =
1236        malloc(sc->txBdCount * sizeof *sc->txMbuf, M_MBUF, M_NOWAIT);
1237
1238      if(!sc->rxMbuf || !sc->txMbuf)
1239        rtems_panic ("No memory for mbuf pointers");
1240
1241      sc->txDmaChan = MCDMA_FEC_TX_CHAN(chan);
1242      sc->rxDmaChan = MCDMA_FEC_RX_CHAN(chan);
1243
1244      mcdma_glue_init(SRAM_DMA_BASE(_SysSramBase));
1245
1246      /*
1247       * Set up interrupts
1248       */
1249      mcdma_glue_irq_install(sc->rxDmaChan,
1250                             mcf548x_mcdma_rx_irq_handler,
1251                             sc);
1252      mcdma_glue_irq_install(sc->txDmaChan,
1253                             mcf548x_mcdma_tx_irq_handler,
1254                             sc);
1255      if(rtems_interrupt_catch(mcf548x_fec_irq_handler,
1256                               MCF548X_FEC_IRQ_VECTOR(chan),
1257                               &old_handler)) {
1258        rtems_panic ("Can't attach MFC54xx FEX interrupt handler\n");
1259      }
1260
1261      bsp_interrupt_vector_enable(MCF548X_IRQ_FEC(chan));
1262
1263      MCF548X_FEC_EIMR(chan) = FEC_INTR_MASK_USED;
1264
1265      /*
1266       * Start driver tasks
1267       */
1268      txTaskName[3] = '0'+chan;
1269      rxTaskName[3] = '0'+chan;
1270      sc->txDaemonTid = rtems_bsdnet_newproc(txTaskName, 4096,
1271                                             mcf548x_fec_txDaemon, sc);
1272      sc->rxDaemonTid = rtems_bsdnet_newproc(rxTaskName, 4096,
1273                                             mcf548x_fec_rxDaemon, sc);
1274    }
1275
1276  mcf548x_fec_request_restart(sc);
1277
1278  /*
1279   * Set flags appropriately
1280   */
1281  if(ifp->if_flags & IFF_PROMISC)
1282    MCF548X_FEC_RCR(chan) |=  MCF548X_FEC_RCR_PROM;
1283  else
1284    MCF548X_FEC_RCR(chan) &= ~MCF548X_FEC_RCR_PROM;
1285
1286  /*
1287   * init timer so the "watchdog function gets called periodically
1288   */
1289  ifp->if_timer    = 1;
1290  /*
1291   * Tell the world that we're running.
1292   */
1293  ifp->if_flags |= IFF_RUNNING;
1294}
1295
1296
1297static void enet_stats (struct mcf548x_enet_struct *sc)
1298{
1299  printf ("       Rx Interrupts:%-8lu", sc->rxInterrupts);
1300  printf ("        Rx Not First:%-8lu", sc->rxNotFirst);
1301  printf ("         Rx Not Last:%-8lu\n", sc->rxNotLast);
1302  printf ("            Rx Giant:%-8lu", sc->rxGiant);
1303  printf ("        Rx Non-octet:%-8lu", sc->rxNonOctet);
1304  printf ("          Rx Bad CRC:%-8lu\n", sc->rxBadCRC);
1305  printf ("       Rx FIFO Error:%-8lu", sc->rxFIFOError);
1306  printf ("        Rx Collision:%-8lu", sc->rxCollision);
1307
1308  printf ("       Tx Interrupts:%-8lu\n", sc->txInterrupts);
1309  printf ("         Tx Deferred:%-8lu", sc->txDeferred);
1310  printf ("   Tx Late Collision:%-8lu", sc->txLateCollision);
1311  printf (" Tx Retransmit Limit:%-8lu\n", sc->txRetryLimit);
1312  printf ("         Tx Underrun:%-8lu", sc->txUnderrun);
1313  printf ("       Tx FIFO Error:%-8lu", sc->txFIFOError);
1314  printf ("       Tx Misaligned:%-8lu\n", sc->txMisaligned);
1315
1316}
1317
1318int32_t mcf548x_fec_setMultiFilter(struct ifnet *ifp)
1319{
1320  /*struct mcf548x_enet_struct *sc = ifp->if_softc; */
1321  /* XXX anything to do? */
1322  return 0;
1323}
1324
1325
1326/*
1327 * Driver ioctl handler
1328 */
1329static int mcf548x_fec_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data)
1330  {
1331  struct mcf548x_enet_struct *sc = ifp->if_softc;
1332  int error = 0;
1333
1334  switch(command)
1335    {
1336
1337    case SIOCGIFMEDIA:
1338    case SIOCSIFMEDIA:
1339      rtems_mii_ioctl (&(sc->mdio_info),sc,command,(void *)data);
1340      break;
1341
1342    case SIOCGIFADDR:
1343    case SIOCSIFADDR:
1344
1345      ether_ioctl(ifp, command, data);
1346
1347      break;
1348
1349    case SIOCADDMULTI:
1350    case SIOCDELMULTI: {
1351      struct ifreq* ifr = (struct ifreq*) data;
1352      error = (command == SIOCADDMULTI)
1353                  ? ether_addmulti(ifr, &sc->arpcom)
1354                  : ether_delmulti(ifr, &sc->arpcom);
1355
1356       if (error == ENETRESET) {
1357         if (ifp->if_flags & IFF_RUNNING)
1358           error = mcf548x_fec_setMultiFilter(ifp);
1359         else
1360           error = 0;
1361       }
1362       break;
1363    }
1364
1365    case SIOCSIFFLAGS:
1366
1367      switch(ifp->if_flags & (IFF_UP | IFF_RUNNING))
1368        {
1369
1370        case IFF_RUNNING:
1371
1372          mcf548x_fec_off(sc);
1373
1374          break;
1375
1376        case IFF_UP:
1377
1378          mcf548x_fec_init(sc);
1379
1380          break;
1381
1382        case IFF_UP | IFF_RUNNING:
1383
1384          mcf548x_fec_off(sc);
1385          mcf548x_fec_init(sc);
1386
1387          break;
1388
1389        default:
1390          break;
1391
1392        }
1393
1394      break;
1395
1396    case SIO_RTEMS_SHOW_STATS:
1397
1398      enet_stats(sc);
1399
1400      break;
1401
1402   /*
1403    * FIXME: All sorts of multicast commands need to be added here!
1404    */
1405    default:
1406
1407    error = EINVAL;
1408
1409    break;
1410
1411    }
1412
1413  return error;
1414
1415  }
1416
1417
1418/*
1419 * init the PHY and adapt FEC settings
1420 */
1421int mcf548x_fec_mode_adapt(struct ifnet *ifp)
1422{
1423  int result = 0;
1424  struct mcf548x_enet_struct *sc = ifp->if_softc;
1425  int media = IFM_MAKEWORD( 0, 0, 0, sc->phy_default);
1426  int chan = sc->chan;
1427
1428  /*
1429   * fetch media status
1430   */
1431  result = mcf548x_fec_ioctl(ifp,SIOCGIFMEDIA,(caddr_t)&media);
1432  if (result != 0) {
1433    return result;
1434  }
1435  /*
1436   * status is unchanged? then do nothing
1437   */
1438  if (media == sc->media_state) {
1439    return 0;
1440  }
1441  /*
1442   * otherwise: for the first call, try to negotiate mode
1443   */
1444  if (sc->media_state == 0) {
1445    /*
1446     * set media status: set auto negotiation -> start auto-negotiation
1447     */
1448    media = IFM_MAKEWORD(0,IFM_AUTO,0,sc->phy_default);
1449    result = mcf548x_fec_ioctl(ifp,SIOCSIFMEDIA,(caddr_t)&media);
1450    if (result != 0) {
1451      return result;
1452    }
1453    /*
1454     * wait for auto-negotiation to terminate
1455     */
1456    do {
1457      media = IFM_MAKEWORD(0,0,0,sc->phy_default);
1458      result = mcf548x_fec_ioctl(ifp,SIOCGIFMEDIA,(caddr_t)&media);
1459      if (result != 0) {
1460        return result;
1461      }
1462    } while (IFM_NONE == IFM_SUBTYPE(media));
1463  }
1464
1465  /*
1466   * now set HW according to media results:
1467   */
1468
1469  /*
1470   * if we are half duplex then switch to half duplex
1471   */
1472  if (0 == (IFM_FDX & IFM_OPTIONS(media))) {
1473    MCF548X_FEC_TCR(chan) &= ~MCF548X_FEC_TCR_FDEN;
1474  }
1475  else {
1476    MCF548X_FEC_TCR(chan) |=  MCF548X_FEC_TCR_FDEN;
1477  }
1478  /*
1479   * store current media state for future compares
1480   */
1481  sc->media_state = media;
1482
1483  return 0;
1484}
1485
1486/*
1487 * periodically poll the PHY. if mode has changed,
1488 * then adjust the FEC settings
1489 */
1490static void mcf548x_fec_watchdog( struct ifnet *ifp)
1491{
1492  mcf548x_fec_mode_adapt(ifp);
1493  ifp->if_timer    = FEC_WATCHDOG_TIMEOUT;
1494}
1495
1496/*
1497 * Attach the MCF548X fec driver to the system
1498 */
1499int rtems_mcf548x_fec_driver_attach(struct rtems_bsdnet_ifconfig *config)
1500  {
1501  struct mcf548x_enet_struct *sc;
1502  struct ifnet *ifp;
1503  int    mtu;
1504  int    unitNumber;
1505  char   *unitName;
1506
1507 /*
1508  * Parse driver name
1509  */
1510  if((unitNumber = rtems_bsdnet_parse_driver_name(config, &unitName)) < 0)
1511    return 0;
1512
1513 /*
1514  * Is driver free?
1515  */
1516  if ((unitNumber <= 0) || (unitNumber > NIFACES))
1517    {
1518
1519    printf ("Bad FEC unit number.\n");
1520    return 0;
1521
1522    }
1523
1524  sc = &enet_driver[unitNumber - 1];
1525  sc->chan = unitNumber-1;
1526  ifp = &sc->arpcom.ac_if;
1527
1528  if(ifp->if_softc != NULL)
1529    {
1530
1531    printf ("Driver already in use.\n");
1532    return 0;
1533
1534    }
1535
1536  /*
1537   * Process options
1538   */
1539#if NVRAM_CONFIGURE == 1
1540
1541  /* Configure from NVRAM */
1542  if(addr = nvram->ipaddr)
1543    {
1544
1545    /* We have a non-zero entry, copy the value */
1546    if(pAddr = malloc(INET_ADDR_MAX_BUF_SIZE, 0, M_NOWAIT))
1547      config->ip_address = (char *)inet_ntop(AF_INET, &addr, pAddr, INET_ADDR_MAX_BUF_SIZE -1);
1548    else
1549      rtems_panic("Can't allocate ip_address buffer!\n");
1550
1551    }
1552
1553  if(addr = nvram->netmask)
1554    {
1555
1556    /* We have a non-zero entry, copy the value */
1557    if (pAddr = malloc (INET_ADDR_MAX_BUF_SIZE, 0, M_NOWAIT))
1558      config->ip_netmask = (char *)inet_ntop(AF_INET, &addr, pAddr, INET_ADDR_MAX_BUF_SIZE -1);
1559    else
1560      rtems_panic("Can't allocate ip_netmask buffer!\n");
1561
1562    }
1563
1564  /* Ethernet address requires special handling -- it must be copied into
1565   * the arpcom struct. The following if construct serves only to give the
1566   * User Area NVRAM parameter the highest priority.
1567   *
1568   * If the ethernet address is specified in NVRAM, go ahead and copy it.
1569   * (ETHER_ADDR_LEN = 6 bytes).
1570   */
1571  if(nvram->enaddr[0] || nvram->enaddr[1] || nvram->enaddr[2])
1572    {
1573
1574    /* Anything in the first three bytes indicates a non-zero entry, copy value */
1575        memcpy((void *)sc->arpcom.ac_enaddr, &nvram->enaddr, ETHER_ADDR_LEN);
1576
1577    }
1578  else
1579    if(config->hardware_address)
1580      {
1581
1582      /* There is no entry in NVRAM, but there is in the ifconfig struct, so use it. */
1583      memcpy((void *)sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
1584      }
1585
1586#else /* NVRAM_CONFIGURE != 1 */
1587
1588  if(config->hardware_address)
1589    {
1590
1591    memcpy(sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
1592
1593    }
1594
1595#endif /* NVRAM_CONFIGURE != 1 */
1596#ifdef HAS_UBOOT
1597  if ((sc->arpcom.ac_enaddr[0] == 0) &&
1598      (sc->arpcom.ac_enaddr[1] == 0) &&
1599      (sc->arpcom.ac_enaddr[2] == 0)) {
1600      memcpy(
1601        (void *)sc->arpcom.ac_enaddr,
1602        bsp_uboot_board_info.bi_enetaddr,
1603        ETHER_ADDR_LEN
1604      );
1605  }
1606#endif
1607#ifdef HAS_DBUG
1608  if ((sc->arpcom.ac_enaddr[0] == 0) &&
1609      (sc->arpcom.ac_enaddr[1] == 0) &&
1610      (sc->arpcom.ac_enaddr[2] == 0)) {
1611      memcpy(
1612        (void *)sc->arpcom.ac_enaddr,
1613        DBUG_SETTINGS.macaddr,
1614        ETHER_ADDR_LEN
1615      );
1616  }
1617#endif
1618  if ((sc->arpcom.ac_enaddr[0] == 0) &&
1619      (sc->arpcom.ac_enaddr[1] == 0) &&
1620      (sc->arpcom.ac_enaddr[2] == 0)) {
1621    /* There is no ethernet address provided, so it could be read
1622     * from the Ethernet protocol block of SCC1 in DPRAM.
1623     */
1624    rtems_panic("No Ethernet address specified!\n");
1625  }
1626  if(config->mtu)
1627    mtu = config->mtu;
1628  else
1629    mtu = ETHERMTU;
1630
1631  if(config->rbuf_count)
1632    sc->rxBdCount = config->rbuf_count;
1633  else
1634    sc->rxBdCount = RX_BUF_COUNT;
1635
1636  if(config->xbuf_count)
1637    sc->txBdCount = config->xbuf_count;
1638  else
1639    sc->txBdCount = TX_BUF_COUNT * TX_BD_PER_BUF;
1640
1641  sc->acceptBroadcast = !config->ignore_broadcast;
1642
1643  /*
1644   * setup info about mdio interface
1645   */
1646  sc->mdio_info.mdio_r   = mcf548x_eth_mii_read;
1647  sc->mdio_info.mdio_w   = mcf548x_eth_mii_write;
1648  sc->mdio_info.has_gmii = 0; /* we do not support gigabit IF */
1649
1650  /*
1651   * XXX: Although most hardware builders will assign the PHY addresses
1652   * like this, this should be more configurable
1653   */
1654  sc->phy_default = unitNumber-1;
1655  sc->phy_chan    = 0; /* assume all MII accesses are via FEC0 */
1656
1657 /*
1658  * Set up network interface values
1659  */
1660  ifp->if_softc   = sc;
1661  ifp->if_unit    = unitNumber;
1662  ifp->if_name    = unitName;
1663  ifp->if_mtu     = mtu;
1664  ifp->if_init    = mcf548x_fec_init;
1665  ifp->if_ioctl   = mcf548x_fec_ioctl;
1666  ifp->if_start   = mcf548x_fec_tx_start;
1667  ifp->if_output  = ether_output;
1668  ifp->if_watchdog =  mcf548x_fec_watchdog; /* XXX: timer is set in "init" */
1669  ifp->if_flags   = IFF_BROADCAST | IFF_MULTICAST;
1670  /*ifp->if_flags   = IFF_BROADCAST | IFF_SIMPLEX;*/
1671
1672  if(ifp->if_snd.ifq_maxlen == 0)
1673    ifp->if_snd.ifq_maxlen = ifqmaxlen;
1674
1675  /*
1676   * Attach the interface
1677   */
1678  if_attach(ifp);
1679
1680  ether_ifattach(ifp);
1681
1682  return 1;
1683  }
1684
1685
1686int rtems_mcf548x_fec_driver_attach_detach(struct rtems_bsdnet_ifconfig *config, int attaching)
1687{
1688  if (attaching) {
1689    return rtems_mcf548x_fec_driver_attach(config);
1690  }
1691  else {
1692    return 0;
1693  }
1694}
1695
1696
Note: See TracBrowser for help on using the repository browser.