source: rtems-libbsd/rtemsbsd/sys/dev/ffec/if_ffec_mcf548x.c @ 0717607

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 0717607 was 0717607, checked in by Christian Mauderer <Christian.Mauderer@…>, on 08/05/16 at 10:00:47

if_ffec_mcf548x: Init when interface is set to UP.

Call the driver initialization function if the interface is set to UP.

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