source: rtems/c/src/lib/libbsp/m68k/mcf5235/network/network.c @ e85eb70

4.104.114.95
Last change on this file since e85eb70 was e85eb70, checked in by Chris Johns <chrisj@…>, on 12/22/07 at 08:28:57

2007-12-14 Chris Johns <chrisj@…>

  • gdb-init: Make the first hb temporary.
  • network/network.c: Add support for reading the MAC address from the FEC if set by the boot monitor. dBug does not do this unless the network is used which is a shame.
  • Property mode set to 100644
File size: 25.9 KB
Line 
1/*
2 * RTEMS/TCPIP driver for MCF5235 Fast Ethernet Controller
3 *
4 * TO DO: Check network stack code -- force longword alignment of all tx mbufs?
5 */
6
7#include <bsp.h>
8#include <stdio.h>
9#include <errno.h>
10#include <stdarg.h>
11#include <string.h>
12#include <rtems.h>
13#include <rtems/error.h>
14#include <rtems/rtems_bsdnet.h>
15
16#include <sys/param.h>
17#include <sys/mbuf.h>
18#include <sys/socket.h>
19#include <sys/sockio.h>
20
21#include <net/ethernet.h>
22#include <net/if.h>
23
24#include <netinet/in.h>
25#include <netinet/if_ether.h>
26
27
28/*
29 * Number of interfaces supported by this driver
30 */
31#define NIFACES 1
32
33#define FEC_INTC0_TX_VECTOR (64+23)
34#define FEC_INTC0_RX_VECTOR (64+27)
35
36/*
37 * Default number of buffer descriptors set aside for this driver.
38 * The number of transmit buffer descriptors has to be quite large
39 * since a single frame often uses three or more buffer descriptors.
40 */
41#define RX_BUF_COUNT     32
42#define TX_BUF_COUNT     20
43#define TX_BD_PER_BUF    3
44
45#define INET_ADDR_MAX_BUF_SIZE (sizeof "255.255.255.255")
46
47/*
48 * RTEMS event used by interrupt handler to signal daemons.
49 * This must *not* be the same event used by the TCP/IP task synchronization.
50 */
51#define TX_INTERRUPT_EVENT RTEMS_EVENT_1
52#define RX_INTERRUPT_EVENT RTEMS_EVENT_1
53
54/*
55 * RTEMS event used to start transmit daemon.
56 * This must not be the same as INTERRUPT_EVENT.
57 */
58#define START_TRANSMIT_EVENT RTEMS_EVENT_2
59
60/*
61 * Receive buffer size -- Allow for a full ethernet packet plus CRC (1518).
62 * Round off to nearest multiple of RBUF_ALIGN.
63 */
64#define MAX_MTU_SIZE    1518
65#define RBUF_ALIGN      4
66#define RBUF_SIZE       ((MAX_MTU_SIZE + RBUF_ALIGN) & ~RBUF_ALIGN)
67
68#if (MCLBYTES < RBUF_SIZE)
69    #error "Driver must have MCLBYTES > RBUF_SIZE"
70#endif
71
72typedef struct mcf5235BufferDescriptor_ {
73    volatile uint16_t           status;
74    uint16_t                    length;
75    volatile void               *buffer;
76} mcf5235BufferDescriptor_t;
77
78/*
79 * Per-device data
80 */
81struct mcf5235_enet_struct {
82    struct arpcom               arpcom;
83    struct mbuf                 **rxMbuf;
84    struct mbuf                 **txMbuf;
85    int                         acceptBroadcast;
86    int                         rxBdCount;
87    int                         txBdCount;
88    int                         txBdHead;
89    int                         txBdTail;
90    int                         txBdActiveCount;
91    mcf5235BufferDescriptor_t  *rxBdBase;
92    mcf5235BufferDescriptor_t  *txBdBase;
93    rtems_id                    rxDaemonTid;
94    rtems_id                    txDaemonTid;
95
96    /*
97     * Statistics
98     */
99    unsigned long   rxInterrupts;
100    unsigned long   txInterrupts;
101    unsigned long   txRawWait;
102    unsigned long   txRealign;
103};
104static struct mcf5235_enet_struct enet_driver[NIFACES];
105
106static rtems_isr
107mcf5235_fec_rx_interrupt_handler( rtems_vector_number v )
108{
109    MCF5235_FEC_EIR = MCF5235_FEC_EIR_RXF;
110    MCF5235_FEC_EIMR &= ~MCF5235_FEC_EIMR_RXF;   
111    enet_driver[0].rxInterrupts++;
112    rtems_event_send(enet_driver[0].rxDaemonTid, RX_INTERRUPT_EVENT);
113}
114
115static rtems_isr
116mcf5235_fec_tx_interrupt_handler( rtems_vector_number v )
117{
118    MCF5235_FEC_EIR = MCF5235_FEC_EIR_TXF;
119    MCF5235_FEC_EIMR &= ~MCF5235_FEC_EIMR_TXF;   
120    enet_driver[0].txInterrupts++;
121    rtems_event_send(enet_driver[0].txDaemonTid, TX_INTERRUPT_EVENT);
122}
123
124/*
125 * Allocate buffer descriptors from (non-cached) on-chip static RAM
126 * Ensure 128-bit (16-byte) alignment
127 */
128static mcf5235BufferDescriptor_t *
129mcf5235_bd_allocate(unsigned int count)
130{
131    extern char __SRAMBASE[];
132    static mcf5235BufferDescriptor_t *bdp = (mcf5235BufferDescriptor_t *)__SRAMBASE;
133    mcf5235BufferDescriptor_t *p = bdp;
134
135    bdp += count;
136    if ((int)bdp & 0xF)
137        bdp = (mcf5235BufferDescriptor_t *)((char *)bdp + (16 - ((int)bdp & 0xF)));
138    return p;
139}
140
141#if UNUSED
142/*
143 * Read MII register
144 * Busy-waits, but transfer time should be short!
145 */
146static int
147getMII(int phyNumber, int regNumber)
148{
149    MCF5235_FEC_MMFR = (0x1 << 30)       |
150                       (0x2 << 28)       |
151                       (phyNumber << 23) |
152                       (regNumber << 18) |
153                       (0x2 << 16);
154    while ((MCF5235_FEC_EIR & MCF5235_FEC_EIR_MII) == 0);
155    MCF5235_FEC_EIR = MCF5235_FEC_EIR_MII;
156    return MCF5235_FEC_MMFR & 0xFFFF;
157}
158#endif
159
160/*
161 * Write MII register
162 * Busy-waits, but transfer time should be short!
163 */
164static void
165setMII(int phyNumber, int regNumber, int value)
166{
167    MCF5235_FEC_MMFR = (0x1 << 30)       |
168                       (0x1 << 28)       |
169                       (phyNumber << 23) |
170                       (regNumber << 18) |
171                       (0x2 << 16)       |
172                       (value & 0xFFFF);
173    while ((MCF5235_FEC_EIR & MCF5235_FEC_EIR_MII) == 0);
174    MCF5235_FEC_EIR = MCF5235_FEC_EIR_MII;
175}
176
177static void
178mcf5235_fec_initialize_hardware(struct mcf5235_enet_struct *sc)
179{
180    int i;
181    const unsigned char *hwaddr = 0;
182    rtems_status_code status;
183    rtems_isr_entry old_handler;
184    uint32_t clock_speed = get_CPU_clock_speed();
185
186    /*
187     * Issue reset to FEC
188     */
189    MCF5235_FEC_ECR = MCF5235_FEC_ECR_RESET;
190    rtems_task_wake_after(1);
191    MCF5235_FEC_ECR = 0;
192
193    /*
194     * Configuration of I/O ports is done outside of this function
195     */
196#if 0
197    imm->gpio.pbcnt |= MCF5235_GPIO_PBCNT_SET_FEC;        /* Set up port b FEC pins */
198#endif
199
200    /*
201     * Set our physical address
202     */
203    hwaddr = sc->arpcom.ac_enaddr;
204    MCF5235_FEC_PALR = (hwaddr[0] << 24) | (hwaddr[1] << 16) |
205                       (hwaddr[2] << 8)  | (hwaddr[3] << 0);
206    MCF5235_FEC_PAUR = (hwaddr[4] << 24) | (hwaddr[5] << 16);
207
208
209    /*
210     * Clear the hash table
211     */
212    MCF5235_FEC_GAUR = 0;
213    MCF5235_FEC_GALR = 0;
214
215    /*
216     * Set up receive buffer size
217     */
218    MCF5235_FEC_EMRBR = 1520; /* Standard Ethernet */
219
220    /*
221     * Allocate mbuf pointers
222     */
223    sc->rxMbuf = malloc(sc->rxBdCount * sizeof *sc->rxMbuf, M_MBUF, M_NOWAIT);
224    sc->txMbuf = malloc(sc->txBdCount * sizeof *sc->txMbuf, M_MBUF, M_NOWAIT);
225    if (!sc->rxMbuf || !sc->txMbuf)
226        rtems_panic("No memory for mbuf pointers");
227
228    /*
229     * Set receiver and transmitter buffer descriptor bases
230     */
231    sc->rxBdBase = mcf5235_bd_allocate(sc->rxBdCount);
232    sc->txBdBase = mcf5235_bd_allocate(sc->txBdCount);
233    MCF5235_FEC_ERDSR = (int)sc->rxBdBase;
234    MCF5235_FEC_ETDSR = (int)sc->txBdBase;
235
236    /*
237     * Set up Receive Control Register:
238     *   Not promiscuous
239     *   MII mode
240     *   Full duplex
241     *   No loopback
242     */
243    MCF5235_FEC_RCR = MCF5235_FEC_RCR_MAX_FL(MAX_MTU_SIZE) |
244                      MCF5235_FEC_RCR_MII_MODE;
245
246    /*
247     * Set up Transmit Control Register:
248     *   Full duplex
249     *   No heartbeat
250     */
251    MCF5235_FEC_TCR = MCF5235_FEC_TCR_FDEN;
252
253    /*
254     * Initialize statistic counters
255     */
256    MCF5235_FEC_MIBC = MCF5235_FEC_MIBC_MIB_DISABLE;
257    {
258    vuint32 *vuip = &MCF5235_FEC_RMON_T_DROP;
259    while (vuip <= &MCF5235_FEC_IEEE_R_OCTETS_OK)
260        *vuip++ = 0;
261    }
262    MCF5235_FEC_MIBC = 0;
263
264    /*
265     * Set MII speed to <= 2.5 MHz
266     */
267    i = (clock_speed + 5000000 - 1) / 5000000;
268    MCF5235_FEC_MSCR = MCF5235_FEC_MSCR_MII_SPEED(i);
269
270    /*
271     * Set PHYS to 100 Mb/s, full duplex
272     */
273    setMII(1, 0, 0x2100);
274
275    /*
276     * Set up receive buffer descriptors
277     */
278    for (i = 0 ; i < sc->rxBdCount ; i++)
279        (sc->rxBdBase + i)->status = 0;
280
281    /*
282     * Set up transmit buffer descriptors
283     */
284    for (i = 0 ; i < sc->txBdCount ; i++) {
285        sc->txBdBase[i].status = 0;
286        sc->txMbuf[i] = NULL;
287    }
288    sc->txBdHead = sc->txBdTail = 0;
289    sc->txBdActiveCount = 0;
290
291    /*
292     * Set up interrupts
293     */
294    status = rtems_interrupt_catch( mcf5235_fec_tx_interrupt_handler, FEC_INTC0_TX_VECTOR, &old_handler );
295    if (status != RTEMS_SUCCESSFUL)
296        rtems_panic ("Can't attach MCF5235 FEC TX interrupt handler: %s\n",
297                     rtems_status_text(status));
298    status = rtems_interrupt_catch(mcf5235_fec_rx_interrupt_handler, FEC_INTC0_RX_VECTOR, &old_handler);
299    if (status != RTEMS_SUCCESSFUL)
300        rtems_panic ("Can't attach MCF5235 FEC RX interrupt handler: %s\n",
301                     rtems_status_text(status));
302    MCF5235_INTC0_ICR23 = MCF5235_INTC_ICR_IL(FEC_IRQ_LEVEL) |
303                          MCF5235_INTC_ICR_IP(FEC_IRQ_TX_PRIORITY);
304    MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT23 | MCF5235_INTC0_IMRL_MASKALL);
305    MCF5235_INTC0_ICR27 = MCF5235_INTC_ICR_IL(FEC_IRQ_LEVEL) |
306                          MCF5235_INTC_ICR_IP(FEC_IRQ_RX_PRIORITY);
307    MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT27 | MCF5235_INTC0_IMRL_MASKALL);
308}
309
310/*
311 * Get the MAC address from the hardware.
312 */
313static void
314fec_get_mac_address(volatile struct mcf5235_enet_struct *sc, unsigned char* hwaddr)
315{
316  unsigned long addr;
317
318  addr = MCF5235_FEC_PALR;
319
320  hwaddr[0] = (addr >> 24) & 0xff;
321  hwaddr[1] = (addr >> 16) & 0xff;
322  hwaddr[2] = (addr >>  8) & 0xff;
323  hwaddr[3] = (addr >>  0) & 0xff;
324 
325  addr = MCF5235_FEC_PAUR;
326 
327  hwaddr[4] = (addr >> 24) & 0xff;
328  hwaddr[5] = (addr >> 16) & 0xff;
329}
330                   
331
332/*
333 * Soak up buffer descriptors that have been sent.
334 */
335static void
336fec_retire_tx_bd(volatile struct mcf5235_enet_struct *sc )
337{
338    struct mbuf *m, *n;
339
340    while ((sc->txBdActiveCount != 0)
341        && ((sc->txBdBase[sc->txBdTail].status & MCF5235_FEC_TxBD_R) == 0)) {
342        m = sc->txMbuf[sc->txBdTail];
343        MFREE(m, n);
344        if (++sc->txBdTail == sc->txBdCount)
345            sc->txBdTail = 0;
346        sc->txBdActiveCount--;
347    }
348}
349
350static void
351fec_rxDaemon (void *arg)
352{
353    volatile struct mcf5235_enet_struct *sc = (volatile struct mcf5235_enet_struct *)arg;
354    struct ifnet *ifp = (struct ifnet* )&sc->arpcom.ac_if;
355    struct mbuf *m;
356    volatile uint16_t status;
357    volatile mcf5235BufferDescriptor_t *rxBd;
358    int rxBdIndex;
359
360    /*
361     * Allocate space for incoming packets and start reception
362     */
363    for (rxBdIndex = 0 ; ;) {
364        rxBd = sc->rxBdBase + rxBdIndex;
365        MGETHDR(m, M_WAIT, MT_DATA);
366        MCLGET(m, M_WAIT);
367        m->m_pkthdr.rcvif = ifp;
368        sc->rxMbuf[rxBdIndex] = m;
369        rxBd->buffer = mtod(m, void *);
370        rxBd->status = MCF5235_FEC_RxBD_E;
371        if (++rxBdIndex == sc->rxBdCount) {
372            rxBd->status |= MCF5235_FEC_RxBD_W;
373            break;
374        }
375    }
376
377    /*
378     * Input packet handling loop
379     */
380    /* Indicate we have some ready buffers available */
381    MCF5235_FEC_RDAR = MCF5235_FEC_RDAR_R_DES_ACTIVE;
382
383    rxBdIndex = 0;
384    for (;;) {
385        rxBd = sc->rxBdBase + rxBdIndex;
386
387        /*
388         * Wait for packet if there's not one ready
389         */
390        if ((status = rxBd->status) & MCF5235_FEC_RxBD_E) {
391            /*
392             * Clear old events.
393             */
394            MCF5235_FEC_EIR = MCF5235_FEC_EIR_RXF;
395
396            /*
397             * Wait for packet to arrive.
398             * Check the buffer descriptor before waiting for the event.
399             * This catches the case when a packet arrives between the
400             * `if' above, and the clearing of the RXF bit in the EIR.
401             */
402            while ((status = rxBd->status) & MCF5235_FEC_RxBD_E) {
403                rtems_event_set events;
404                int level;
405
406                rtems_interrupt_disable(level);
407                MCF5235_FEC_EIMR |= MCF5235_FEC_EIMR_RXF;
408                rtems_interrupt_enable(level);
409                rtems_bsdnet_event_receive (RX_INTERRUPT_EVENT,
410                                            RTEMS_WAIT|RTEMS_EVENT_ANY,
411                                            RTEMS_NO_TIMEOUT,
412                                            &events);
413            }
414        }
415
416        /*
417         * Check that packet is valid
418         */
419        if (status & MCF5235_FEC_RxBD_L) {
420            /*
421             * Pass the packet up the chain.
422             * FIXME: Packet filtering hook could be done here.
423             */
424            struct ether_header *eh;
425            int len = rxBd->length - sizeof(uint32_t);;
426
427            /*
428             * Invalidate the cache and push the packet up.
429             * The cache is so small that it's more efficient to just
430             * invalidate the whole thing unless the packet is very small.
431             */
432            m = sc->rxMbuf[rxBdIndex];
433            if (len < 128)
434                rtems_cache_invalidate_multiple_data_lines(m->m_data, len);
435            else
436                rtems_cache_invalidate_entire_data();
437            m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
438            eh = mtod(m, struct ether_header *);
439            m->m_data += sizeof(struct ether_header);
440            ether_input(ifp, eh, m);
441
442            /*
443             * Allocate a new mbuf
444             */
445            MGETHDR(m, M_WAIT, MT_DATA);
446            MCLGET(m, M_WAIT);
447            m->m_pkthdr.rcvif = ifp;
448            sc->rxMbuf[rxBdIndex] = m;
449            rxBd->buffer = mtod(m, void *);
450        }
451
452        /*
453         * Reenable the buffer descriptor
454         */
455        rxBd->status = (status & MCF5235_FEC_RxBD_W) | MCF5235_FEC_RxBD_E;
456        MCF5235_FEC_RDAR = MCF5235_FEC_RDAR_R_DES_ACTIVE;
457
458        /*
459         * Move to next buffer descriptor
460         */
461        if (++rxBdIndex == sc->rxBdCount)
462            rxBdIndex = 0;
463    }
464}
465
466static void
467fec_sendpacket(struct ifnet *ifp, struct mbuf *m)
468{
469    struct mcf5235_enet_struct *sc = ifp->if_softc;
470    volatile mcf5235BufferDescriptor_t *firstTxBd, *txBd;
471    uint16_t status;
472    int nAdded;
473
474   /*
475     * Free up buffer descriptors
476     */
477    fec_retire_tx_bd(sc);
478
479    /*
480     * Set up the transmit buffer descriptors.
481     * No need to pad out short packets since the
482     * hardware takes care of that automatically.
483     * No need to copy the packet to a contiguous buffer
484     * since the hardware is capable of scatter/gather DMA.
485     */
486    nAdded = 0;
487    firstTxBd = sc->txBdBase + sc->txBdHead;
488   
489    for (;;) {
490        /*
491         * Wait for buffer descriptor to become available
492         */
493        if ((sc->txBdActiveCount + nAdded)  == sc->txBdCount) {
494            /*
495             * Clear old events.
496             */
497            MCF5235_FEC_EIR = MCF5235_FEC_EIR_TXF;
498
499            /*
500             * Wait for buffer descriptor to become available.
501             * Check for buffer descriptors before waiting for the event.
502             * This catches the case when a buffer became available between
503             * the `if' above, and the clearing of the TXF bit in the EIR.
504             */
505            fec_retire_tx_bd(sc);
506            while ((sc->txBdActiveCount + nAdded) == sc->txBdCount) {
507                rtems_event_set events;
508                int level;
509
510                rtems_interrupt_disable(level);
511                MCF5235_FEC_EIMR |= MCF5235_FEC_EIMR_TXF;   
512                rtems_interrupt_enable(level);
513                sc->txRawWait++;
514                rtems_bsdnet_event_receive(TX_INTERRUPT_EVENT,
515                                           RTEMS_WAIT|RTEMS_EVENT_ANY,
516                                           RTEMS_NO_TIMEOUT,
517                                           &events);
518                fec_retire_tx_bd(sc);
519            }
520        }
521   
522        /*
523         * Don't set the READY flag on the first fragment
524         * until the whole packet has been readied.
525         */
526        status = nAdded ? MCF5235_FEC_TxBD_R : 0;
527   
528        /*
529         * The IP fragmentation routine in ip_output
530         * can produce fragments with zero length.
531         */
532        txBd = sc->txBdBase + sc->txBdHead;
533        if (m->m_len) {
534            char *p = mtod(m, char *);
535            /*
536             * Stupid FEC can't handle misaligned data!
537             * Given the way that mbuf's are layed out it should be
538             * safe to shuffle the data down like this.....
539             * Perhaps this code could be improved with a "Duff's Device".
540             */
541            if ((int)p & 0x3) {
542                int l = m->m_len;
543                char *dest = p - ((int)p & 0x3);
544                uint16_t *o = (uint16_t *)dest, *i = (uint16_t *)p;
545                while (l > 0) {
546                    *o++ = *i++;
547                    l -= sizeof(uint16_t);
548                }
549                p = dest;
550                sc->txRealign++;
551            }
552            txBd->buffer = p;
553            txBd->length = m->m_len;
554            sc->txMbuf[sc->txBdHead] = m;
555            nAdded++;
556            if (++sc->txBdHead == sc->txBdCount) {
557                status |= MCF5235_FEC_TxBD_W;
558                sc->txBdHead = 0;
559            }
560            m = m->m_next;
561        }
562        else {
563            /*
564             * Just toss empty mbufs
565             */
566            struct mbuf *n;
567            MFREE(m, n);
568            m = n;
569        }
570        if (m == NULL) {
571          if (nAdded) {
572            txBd->status = status | MCF5235_FEC_TxBD_R
573                                  | MCF5235_FEC_TxBD_L
574                                  | MCF5235_FEC_TxBD_TC;
575            if (nAdded > 1)
576                firstTxBd->status |= MCF5235_FEC_TxBD_R;
577            MCF5235_FEC_TDAR = MCF5235_FEC_TDAR_X_DES_ACTIVE;
578            sc->txBdActiveCount += nAdded;
579          }
580          break;
581        }
582        txBd->status = status;
583    }
584}
585
586void
587fec_txDaemon(void *arg)
588{
589    struct mcf5235_enet_struct *sc = (struct mcf5235_enet_struct *)arg;
590    struct ifnet *ifp = &sc->arpcom.ac_if;
591    struct mbuf *m;
592    rtems_event_set events;
593
594    for (;;) {
595        /*
596         * Wait for packet
597         */
598        rtems_bsdnet_event_receive(START_TRANSMIT_EVENT,
599                                    RTEMS_EVENT_ANY | RTEMS_WAIT,
600                                    RTEMS_NO_TIMEOUT,
601                                    &events);
602
603        /*
604         * Send packets till queue is empty
605         */
606        for (;;) {
607            /*
608             * Get the next mbuf chain to transmit.
609             */
610            IF_DEQUEUE(&ifp->if_snd, m);
611            if (!m)
612                break;
613            fec_sendpacket(ifp, m);
614        }
615        ifp->if_flags &= ~IFF_OACTIVE;
616    }
617}
618
619
620/*
621 * Send packet (caller provides header).
622 */
623static void
624mcf5235_enet_start(struct ifnet *ifp)
625{
626    struct mcf5235_enet_struct *sc = ifp->if_softc;
627
628    rtems_event_send(sc->txDaemonTid, START_TRANSMIT_EVENT);
629    ifp->if_flags |= IFF_OACTIVE;
630}
631
632static void
633fec_init(void *arg)
634{
635    struct mcf5235_enet_struct *sc = arg;
636    struct ifnet *ifp = &sc->arpcom.ac_if;
637
638    if (sc->txDaemonTid == 0) {
639        /*
640         * Set up hardware
641         */
642        mcf5235_fec_initialize_hardware(sc);
643
644        /*
645         * Start driver tasks
646         */
647        sc->txDaemonTid = rtems_bsdnet_newproc("FECtx", 4096, fec_txDaemon, sc);
648        sc->rxDaemonTid = rtems_bsdnet_newproc("FECrx", 4096, fec_rxDaemon, sc);
649    }
650
651    /*
652     * Set flags appropriately
653     */
654    if (ifp->if_flags & IFF_PROMISC)
655        MCF5235_FEC_RCR |= MCF5235_FEC_RCR_PROM;
656    else
657        MCF5235_FEC_RCR &= ~MCF5235_FEC_RCR_PROM;
658
659    /*
660     * Tell the world that we're running.
661     */
662    ifp->if_flags |= IFF_RUNNING;
663
664    /*
665     * Enable receiver and transmitter
666     */
667    MCF5235_FEC_ECR = MCF5235_FEC_ECR_ETHER_EN;
668}
669
670
671static void
672fec_stop(struct mcf5235_enet_struct *sc)
673{
674    struct ifnet *ifp = &sc->arpcom.ac_if;
675
676    ifp->if_flags &= ~IFF_RUNNING;
677
678    /*
679     * Shut down receiver and transmitter
680     */
681    MCF5235_FEC_ECR = 0x0;
682}
683
684/*
685 * Show interface statistics
686 */
687static void
688enet_stats(struct mcf5235_enet_struct *sc)
689{
690    printf("  Rx Interrupts:%-10lu",   sc->rxInterrupts);
691    printf("Rx Packet Count:%-10lu",   MCF5235_FEC_RMON_R_PACKETS);
692    printf("   Rx Broadcast:%-10lu\n", MCF5235_FEC_RMON_R_BC_PKT);
693    printf("   Rx Multicast:%-10lu",   MCF5235_FEC_RMON_R_MC_PKT);
694    printf("CRC/Align error:%-10lu",   MCF5235_FEC_RMON_R_CRC_ALIGN);
695    printf("   Rx Undersize:%-10lu\n", MCF5235_FEC_RMON_R_UNDERSIZE);
696    printf("    Rx Oversize:%-10lu",   MCF5235_FEC_RMON_R_OVERSIZE);
697    printf("    Rx Fragment:%-10lu",   MCF5235_FEC_RMON_R_FRAG);
698    printf("      Rx Jabber:%-10lu\n", MCF5235_FEC_RMON_R_JAB);
699    printf("          Rx 64:%-10lu",   MCF5235_FEC_RMON_R_P64);
700    printf("      Rx 65-127:%-10lu",   MCF5235_FEC_RMON_R_P65T0127);
701    printf("     Rx 128-255:%-10lu\n", MCF5235_FEC_RMON_R_P128TO255);
702    printf("     Rx 256-511:%-10lu",   MCF5235_FEC_RMON_R_P256TO511);
703    printf("    Rx 511-1023:%-10lu",   MCF5235_FEC_RMON_R_P512TO1023);
704    printf("   Rx 1024-2047:%-10lu\n", MCF5235_FEC_RMON_R_P1024TO2047);
705    printf("      Rx >=2048:%-10lu",   MCF5235_FEC_RMON_R_GTE2048);
706    printf("      Rx Octets:%-10lu",   MCF5235_FEC_RMON_R_OCTETS);
707    printf("     Rx Dropped:%-10lu\n", MCF5235_FEC_IEEE_R_DROP);
708    printf("    Rx frame OK:%-10lu",   MCF5235_FEC_IEEE_R_FRAME_OK);
709    printf("   Rx CRC error:%-10lu",   MCF5235_FEC_IEEE_R_CRC);
710    printf(" Rx Align error:%-10lu\n", MCF5235_FEC_IEEE_R_ALIGN);
711    printf("  FIFO Overflow:%-10lu",   MCF5235_FEC_IEEE_R_MACERR);
712    printf("Rx Pause Frames:%-10lu",   MCF5235_FEC_IEEE_R_FDXFC);
713    printf("   Rx Octets OK:%-10lu\n", MCF5235_FEC_IEEE_R_OCTETS_OK);
714    printf("  Tx Interrupts:%-10lu",   sc->txInterrupts);
715    printf("Tx Output Waits:%-10lu",   sc->txRawWait);
716    printf("Tx Realignments:%-10lu\n",   sc->txRealign);
717    printf(" Tx Unaccounted:%-10lu", MCF5235_FEC_RMON_T_DROP);
718    printf("Tx Packet Count:%-10lu",   MCF5235_FEC_RMON_T_PACKETS);
719    printf("   Tx Broadcast:%-10lu\n",   MCF5235_FEC_RMON_T_BC_PKT);
720    printf("   Tx Multicast:%-10lu", MCF5235_FEC_RMON_T_MC_PKT);
721    printf("CRC/Align error:%-10lu",   MCF5235_FEC_RMON_T_CRC_ALIGN);
722    printf("   Tx Undersize:%-10lu\n",   MCF5235_FEC_RMON_T_UNDERSIZE);
723    printf("    Tx Oversize:%-10lu", MCF5235_FEC_RMON_T_OVERSIZE);
724    printf("    Tx Fragment:%-10lu",   MCF5235_FEC_RMON_T_FRAG);
725    printf("      Tx Jabber:%-10lu\n",   MCF5235_FEC_RMON_T_JAB);
726    printf("  Tx Collisions:%-10lu", MCF5235_FEC_RMON_T_COL);
727    printf("          Tx 64:%-10lu",   MCF5235_FEC_RMON_T_P64);
728    printf("      Tx 65-127:%-10lu\n",   MCF5235_FEC_RMON_T_P65TO127);
729    printf("     Tx 128-255:%-10lu", MCF5235_FEC_RMON_T_P128TO255);
730    printf("     Tx 256-511:%-10lu",   MCF5235_FEC_RMON_T_P256TO511);
731    printf("    Tx 511-1023:%-10lu\n",   MCF5235_FEC_RMON_T_P512TO1023);
732    printf("   Tx 1024-2047:%-10lu", MCF5235_FEC_RMON_T_P1024TO2047);
733    printf("      Tx >=2048:%-10lu",   MCF5235_FEC_RMON_T_P_GTE2048);
734    printf("      Tx Octets:%-10lu\n",   MCF5235_FEC_RMON_T_OCTETS);
735    printf("     Tx Dropped:%-10lu", MCF5235_FEC_IEEE_T_DROP);
736    printf("    Tx Frame OK:%-10lu",   MCF5235_FEC_IEEE_T_FRAME_OK);
737    printf(" Tx 1 Collision:%-10lu\n",   MCF5235_FEC_IEEE_T_1COL);
738    printf("Tx >1 Collision:%-10lu", MCF5235_FEC_IEEE_T_MCOL);
739    printf("    Tx Deferred:%-10lu",   MCF5235_FEC_IEEE_T_DEF);
740    printf(" Late Collision:%-10lu\n",   MCF5235_FEC_IEEE_T_LCOL);
741    printf(" Excessive Coll:%-10lu", MCF5235_FEC_IEEE_T_EXCOL);
742    printf("  FIFO Underrun:%-10lu",   MCF5235_FEC_IEEE_T_MACERR);
743    printf("  Carrier Error:%-10lu\n",   MCF5235_FEC_IEEE_T_CSERR);
744    printf("   Tx SQE Error:%-10lu", MCF5235_FEC_IEEE_T_SQE);
745    printf("Tx Pause Frames:%-10lu",   MCF5235_FEC_IEEE_T_FDXFC);
746    printf("   Tx Octets OK:%-10lu\n", MCF5235_FEC_IEEE_T_OCTETS_OK);
747}
748
749static int
750fec_ioctl(struct ifnet *ifp, ioctl_command_t command, caddr_t data)
751{
752    struct mcf5235_enet_struct *sc = ifp->if_softc;
753    int error = 0;
754
755    switch (command) {
756        case SIOCGIFADDR:
757        case SIOCSIFADDR:
758            ether_ioctl(ifp, command, data);
759            break;
760
761        case SIOCSIFFLAGS:
762            switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
763                case IFF_RUNNING:
764                    fec_stop(sc);
765                    break;
766
767                case IFF_UP:
768                    fec_init(sc);
769                    break;
770
771                case IFF_UP | IFF_RUNNING:
772                    fec_stop(sc);
773                    fec_init(sc);
774                    break;
775
776                default:
777                    break;
778            }
779            break;
780
781        case SIO_RTEMS_SHOW_STATS:
782            enet_stats(sc);
783            break;
784
785            /*
786             * FIXME: All sorts of multicast commands need to be added here!
787             */
788        default:
789            error = EINVAL;
790            break;
791    }
792    return error;
793}
794
795int
796rtems_fec_driver_attach(struct rtems_bsdnet_ifconfig *config, int attaching )
797{
798    struct mcf5235_enet_struct *sc;
799    struct ifnet *ifp;
800    int mtu;
801    int unitNumber;
802    char *unitName;
803    unsigned char *hwaddr;
804
805    /*
806     * Parse driver name
807     */
808    if ((unitNumber = rtems_bsdnet_parse_driver_name (config, &unitName)) < 0)
809        return 0;
810
811    /*
812     * Is driver free?
813     */
814    if ((unitNumber < 0) || (unitNumber >= NIFACES)) {
815        printf("mcf5235: bad FEC unit number.\n");
816        return 0;
817    }
818    sc = &enet_driver[unitNumber];
819    ifp = &sc->arpcom.ac_if;
820    if (ifp->if_softc != NULL) {
821        printf("mcf5235: driver already in use.\n");
822        return 0;
823    }
824
825    /*
826     * Process options
827     */
828    if (config->hardware_address)
829      memcpy(sc->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);
830    else
831      fec_get_mac_address(sc, sc->arpcom.ac_enaddr);
832
833    hwaddr = config->hardware_address;
834    printf("%s%d: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
835           unitName, unitNumber,
836           hwaddr[0], hwaddr[1], hwaddr[2],
837           hwaddr[3], hwaddr[4], hwaddr[5]);
838
839    if (config->mtu)
840        mtu = config->mtu;
841    else
842        mtu = ETHERMTU;
843    if (config->rbuf_count)
844        sc->rxBdCount = config->rbuf_count;
845    else
846        sc->rxBdCount = RX_BUF_COUNT;
847    if (config->xbuf_count)
848        sc->txBdCount = config->xbuf_count;
849    else
850        sc->txBdCount = TX_BUF_COUNT * TX_BD_PER_BUF;
851
852    sc->acceptBroadcast = !config->ignore_broadcast;
853
854    /*
855     * Set up network interface values
856     */
857    ifp->if_softc = sc;
858    ifp->if_unit = unitNumber;
859    ifp->if_name = unitName;
860    ifp->if_mtu = mtu;
861    ifp->if_init = fec_init;
862    ifp->if_ioctl = fec_ioctl;
863    ifp->if_start = mcf5235_enet_start;
864    ifp->if_output = ether_output;
865    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
866    if (ifp->if_snd.ifq_maxlen == 0)
867        ifp->if_snd.ifq_maxlen = ifqmaxlen;
868
869    /*
870     * Attach the interface
871     */
872    if_attach(ifp);
873    ether_ifattach(ifp);
874    return 1;
875};
876
Note: See TracBrowser for help on using the repository browser.