source: rtems/c/src/lib/libbsp/m68k/uC5282/network/network.c @ 1066597

4.104.114.95
Last change on this file since 1066597 was 1066597, checked in by Joel Sherrill <joel.sherrill@…>, on 11/26/07 at 17:48:33

2007-11-26 Eric Norum <norume@…>

  • network/network.c: Fix LED configuration to match uCDIMM.
  • Property mode set to 100644
File size: 30.4 KB
Line 
1/*
2 * RTEMS driver for MCF5282 Fast Ethernet Controller
3 *
4 *  Author: W. Eric Norum <norume@aps.anl.gov>
5 *
6 *  COPYRIGHT (c) 2005.
7 *  On-Line Applications Research Corporation (OAR).
8 *
9 *  The license and distribution terms for this file may be
10 *  found in the file LICENSE in this distribution or at
11 *  http://www.rtems.com/license/LICENSE.
12 */
13
14#include <bsp.h>
15#include <stdio.h>
16#include <errno.h>
17#include <stdarg.h>
18#include <string.h>
19#include <rtems.h>
20#include <rtems/error.h>
21#include <rtems/rtems_bsdnet.h>
22
23#include <sys/param.h>
24#include <sys/mbuf.h>
25#include <sys/socket.h>
26#include <sys/sockio.h>
27
28#include <net/ethernet.h>
29#include <net/if.h>
30
31#include <netinet/in.h>
32#include <netinet/if_ether.h>
33
34
35/*
36 * Number of interfaces supported by this driver
37 */
38#define NIFACES 1
39
40#define FEC_INTC0_TX_VECTOR (64+23)
41#define FEC_INTC0_RX_VECTOR (64+27)
42#define MII_VECTOR (64+7)  /* IRQ7* pin connected to external transceiver */
43#define MII_EPPAR  MCF5282_EPORT_EPPAR_EPPA7_LEVEL
44#define MII_EPDDR  MCF5282_EPORT_EPDDR_EPDD7
45#define MII_EPIER  MCF5282_EPORT_EPIER_EPIE7
46#define MII_EPPDR  MCF5282_EPORT_EPPDR_EPPD7
47
48/*
49 * Default number of buffer descriptors set aside for this driver.
50 * The number of transmit buffer descriptors has to be quite large
51 * since a single frame often uses three or more buffer descriptors.
52 */
53#define RX_BUF_COUNT     32
54#define TX_BUF_COUNT     20
55#define TX_BD_PER_BUF    3
56
57#define INET_ADDR_MAX_BUF_SIZE (sizeof "255.255.255.255")
58
59/*
60 * RTEMS event used by interrupt handler to signal daemons.
61 * This must *not* be the same event used by the TCP/IP task synchronization.
62 */
63#define TX_INTERRUPT_EVENT RTEMS_EVENT_1
64#define RX_INTERRUPT_EVENT RTEMS_EVENT_1
65
66/*
67 * RTEMS event used to start transmit daemon.
68 * This must not be the same as INTERRUPT_EVENT.
69 */
70#define START_TRANSMIT_EVENT RTEMS_EVENT_2
71
72/*
73 * Receive buffer size -- Allow for a full ethernet packet plus CRC (1518).
74 * Round off to nearest multiple of RBUF_ALIGN.
75 */
76#define MAX_MTU_SIZE    1518
77#define RBUF_ALIGN      4
78#define RBUF_SIZE       ((MAX_MTU_SIZE + RBUF_ALIGN) & ~RBUF_ALIGN)
79
80#if (MCLBYTES < RBUF_SIZE)
81    #error "Driver must have MCLBYTES > RBUF_SIZE"
82#endif
83
84typedef struct mcf5282BufferDescriptor_ {
85    volatile uint16_t   status;
86    uint16_t                    length;
87    volatile void      *buffer;
88} mcf5282BufferDescriptor_t;
89
90/*
91 * Per-device data
92 */
93struct mcf5282_enet_struct {
94    struct arpcom               arpcom;
95    struct mbuf                 **rxMbuf;
96    struct mbuf                 **txMbuf;
97    int                         acceptBroadcast;
98    int                         rxBdCount;
99    int                         txBdCount;
100    int                         txBdHead;
101    int                         txBdTail;
102    int                         txBdActiveCount;
103    mcf5282BufferDescriptor_t  *rxBdBase;
104    mcf5282BufferDescriptor_t  *txBdBase;
105    rtems_id                    rxDaemonTid;
106    rtems_id                    txDaemonTid;
107
108    /*
109     * Statistics
110     */
111    unsigned long   rxInterrupts;
112    unsigned long   txInterrupts;
113    unsigned long   miiInterrupts;
114    unsigned long   txRawWait;
115    unsigned long   txRealign;
116    unsigned long   txRealignDrop;
117
118    /*
119     * Link parameters
120     */
121    int             force100Full;
122    uint16_t        mii_sr2;
123};
124static struct mcf5282_enet_struct enet_driver[NIFACES];
125
126/*
127 * Read MII register
128 * Busy-waits, but transfer time should be short!
129 */
130static int
131getMII(int phyNumber, int regNumber)
132{
133    MCF5282_FEC_MMFR = (0x1 << 30)       |
134                       (0x2 << 28)       |
135                       (phyNumber << 23) |
136                       (regNumber << 18) |
137                       (0x2 << 16);
138    while ((MCF5282_FEC_EIR & MCF5282_FEC_EIR_MII) == 0);
139    MCF5282_FEC_EIR = MCF5282_FEC_EIR_MII;
140    return MCF5282_FEC_MMFR & 0xFFFF;
141}
142
143/*
144 * Write MII register
145 * Busy-waits, but transfer time should be short!
146 */
147static void
148setMII(int phyNumber, int regNumber, int value)
149{
150    MCF5282_FEC_MMFR = (0x1 << 30)       |
151                       (0x1 << 28)       |
152                       (phyNumber << 23) |
153                       (regNumber << 18) |
154                       (0x2 << 16)       |
155                       (value & 0xFFFF);
156    while ((MCF5282_FEC_EIR & MCF5282_FEC_EIR_MII) == 0);
157    MCF5282_FEC_EIR = MCF5282_FEC_EIR_MII;
158}
159
160static rtems_isr
161mcf5282_fec_rx_interrupt_handler( rtems_vector_number v )
162{
163    MCF5282_FEC_EIR = MCF5282_FEC_EIR_RXF;
164    MCF5282_FEC_EIMR &= ~MCF5282_FEC_EIMR_RXF;   
165    enet_driver[0].rxInterrupts++;
166    rtems_event_send(enet_driver[0].rxDaemonTid, RX_INTERRUPT_EVENT);
167}
168
169static rtems_isr
170mcf5282_fec_tx_interrupt_handler( rtems_vector_number v )
171{
172    MCF5282_FEC_EIR = MCF5282_FEC_EIR_TXF;
173    MCF5282_FEC_EIMR &= ~MCF5282_FEC_EIMR_TXF;   
174    enet_driver[0].txInterrupts++;
175    rtems_event_send(enet_driver[0].txDaemonTid, TX_INTERRUPT_EVENT);
176}
177
178static rtems_isr
179mcf5282_mii_interrupt_handler( rtems_vector_number v )
180{
181    uint16 sr2;
182
183    enet_driver[0].miiInterrupts++;
184    getMII(1, 19); /* Read and clear interrupt status bits */
185    enet_driver[0].mii_sr2 = sr2 = getMII(1, 17);
186    if (((sr2 & 0x200) != 0)
187     && ((MCF5282_FEC_TCR & MCF5282_FEC_TCR_FDEN) == 0))
188        MCF5282_FEC_TCR |= MCF5282_FEC_TCR_FDEN;
189    else if (((sr2 & 0x200) == 0)
190          && ((MCF5282_FEC_TCR & MCF5282_FEC_TCR_FDEN) != 0))
191        MCF5282_FEC_TCR &= ~MCF5282_FEC_TCR_FDEN;
192}
193
194/*
195 * Allocate buffer descriptors from (non-cached) on-chip static RAM
196 * Ensure 128-bit (16-byte) alignment
197 * Allow some space at the beginning for other diagnostic counters
198 */
199static mcf5282BufferDescriptor_t *
200mcf5282_bd_allocate(unsigned int count)
201{
202    extern char __SRAMBASE[];
203    static mcf5282BufferDescriptor_t *bdp = (mcf5282BufferDescriptor_t *)(__SRAMBASE+16);
204    mcf5282BufferDescriptor_t *p = bdp;
205
206    bdp += count;
207    if ((int)bdp & 0xF)
208        bdp = (mcf5282BufferDescriptor_t *)((char *)bdp + (16 - ((int)bdp & 0xF)));
209    return p;
210}
211
212static void
213mcf5282_fec_initialize_hardware(struct mcf5282_enet_struct *sc)
214{
215    int i;
216    const unsigned char *hwaddr;
217    rtems_status_code status;
218    rtems_isr_entry old_handler;
219        uint32_t clock_speed = bsp_get_CPU_clock_speed();
220
221    /*
222     * Issue reset to FEC
223     */
224    MCF5282_FEC_ECR = MCF5282_FEC_ECR_RESET;
225    rtems_task_wake_after(2);
226    MCF5282_FEC_ECR = 0;
227
228    /*
229     * Configuration of I/O ports is done outside of this function
230     */
231#if 0
232    imm->gpio.pbcnt |= MCF5282_GPIO_PBCNT_SET_FEC;        /* Set up port b FEC pins */
233#endif
234
235    /*
236     * Set our physical address
237     */
238    hwaddr = sc->arpcom.ac_enaddr;
239    MCF5282_FEC_PALR = (hwaddr[0] << 24) | (hwaddr[1] << 16) |
240                       (hwaddr[2] << 8)  | (hwaddr[3] << 0);
241    MCF5282_FEC_PAUR = (hwaddr[4] << 24) | (hwaddr[5] << 16);
242
243
244    /*
245     * Clear the hash table
246     */
247    MCF5282_FEC_GAUR = 0;
248    MCF5282_FEC_GALR = 0;
249
250    /*
251     * Set up receive buffer size
252     */
253    MCF5282_FEC_EMRBR = 1520; /* Standard Ethernet */
254
255    /*
256     * Allocate mbuf pointers
257     */
258    sc->rxMbuf = malloc(sc->rxBdCount * sizeof *sc->rxMbuf, M_MBUF, M_NOWAIT);
259    sc->txMbuf = malloc(sc->txBdCount * sizeof *sc->txMbuf, M_MBUF, M_NOWAIT);
260    if (!sc->rxMbuf || !sc->txMbuf)
261        rtems_panic("No memory for mbuf pointers");
262
263    /*
264     * Set receiver and transmitter buffer descriptor bases
265     */
266    sc->rxBdBase = mcf5282_bd_allocate(sc->rxBdCount);
267    sc->txBdBase = mcf5282_bd_allocate(sc->txBdCount);
268    MCF5282_FEC_ERDSR = (int)sc->rxBdBase;
269    MCF5282_FEC_ETDSR = (int)sc->txBdBase;
270
271    /*
272     * Set up Receive Control Register:
273     *   Not promiscuous
274     *   MII mode
275     *   Full duplex
276     *   No loopback
277     */
278    MCF5282_FEC_RCR = MCF5282_FEC_RCR_MAX_FL(MAX_MTU_SIZE) |
279                      MCF5282_FEC_RCR_MII_MODE;
280
281    /*
282     * Set up Transmit Control Register:
283     *   Full duplex
284     *   No heartbeat
285     */
286    MCF5282_FEC_TCR = MCF5282_FEC_TCR_FDEN;
287
288    /*
289     * Initialize statistic counters
290     */
291    MCF5282_FEC_MIBC = MCF5282_FEC_MIBC_MIB_DISABLE;
292    {
293    vuint32 *vuip = &MCF5282_FEC_RMON_T_DROP;
294    while (vuip <= &MCF5282_FEC_IEEE_R_OCTETS_OK)
295        *vuip++ = 0;
296    }
297    MCF5282_FEC_MIBC = 0;
298
299    /*
300     * Set MII speed to <= 2.5 MHz
301     */
302    i = (clock_speed + 5000000 - 1) / 5000000;
303    MCF5282_FEC_MSCR = MCF5282_FEC_MSCR_MII_SPEED(i);
304
305    /*
306     * Set PHYS
307     *  LED1 receive status, LED2 link status, LEDs stretched
308     *  Advertise 100 Mb/s, full-duplex, IEEE-802.3
309     *  Turn off auto-negotiate
310     *  Enable speed-change, duplex-change and link-status-change interrupts
311     *  Set 100/full and perhaps auto-negotiate
312     */
313    setMII(1, 20, 0x24F2);
314    setMII(1,  4, 0x0181);
315    setMII(1,  0, 0x2100);
316    rtems_task_wake_after(2);
317    sc->mii_sr2 = getMII(1, 17);
318    setMII(1, 18, 0x0072);
319    if (!sc->force100Full)
320        setMII(1, 0, 0x3100);
321
322    /*
323     * Set up receive buffer descriptors
324     */
325    for (i = 0 ; i < sc->rxBdCount ; i++)
326        (sc->rxBdBase + i)->status = 0;
327
328    /*
329     * Set up transmit buffer descriptors
330     */
331    for (i = 0 ; i < sc->txBdCount ; i++) {
332        sc->txBdBase[i].status = 0;
333        sc->txMbuf[i] = NULL;
334    }
335    sc->txBdHead = sc->txBdTail = 0;
336    sc->txBdActiveCount = 0;
337
338    /*
339     * Set up interrupts
340     */
341    status = rtems_interrupt_catch( mcf5282_fec_tx_interrupt_handler, FEC_INTC0_TX_VECTOR, &old_handler );
342    if (status != RTEMS_SUCCESSFUL)
343        rtems_panic ("Can't attach MCF5282 FEC TX interrupt handler: %s\n",
344                                                 rtems_status_text(status));
345    bsp_allocate_interrupt(FEC_IRQ_LEVEL, FEC_IRQ_TX_PRIORITY);
346    MCF5282_INTC0_ICR23 = MCF5282_INTC_ICR_IL(FEC_IRQ_LEVEL) |
347                          MCF5282_INTC_ICR_IP(FEC_IRQ_TX_PRIORITY);
348    MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT23 | MCF5282_INTC_IMRL_MASKALL);
349
350    status = rtems_interrupt_catch(mcf5282_fec_rx_interrupt_handler, FEC_INTC0_RX_VECTOR, &old_handler);
351    if (status != RTEMS_SUCCESSFUL)
352        rtems_panic ("Can't attach MCF5282 FEC RX interrupt handler: %s\n",
353                                                 rtems_status_text(status));
354    bsp_allocate_interrupt(FEC_IRQ_LEVEL, FEC_IRQ_RX_PRIORITY);
355    MCF5282_INTC0_ICR27 = MCF5282_INTC_ICR_IL(FEC_IRQ_LEVEL) |
356                          MCF5282_INTC_ICR_IP(FEC_IRQ_RX_PRIORITY);
357    MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT27 | MCF5282_INTC_IMRL_MASKALL);
358
359    status = rtems_interrupt_catch(mcf5282_mii_interrupt_handler, MII_VECTOR, &old_handler);
360    if (status != RTEMS_SUCCESSFUL)
361        rtems_panic ("Can't attach MCF5282 FEC MII interrupt handler: %s\n",
362                                                 rtems_status_text(status));
363    MCF5282_EPORT_EPPAR &= ~MII_EPPAR;
364    MCF5282_EPORT_EPDDR &= ~MII_EPDDR;
365    MCF5282_EPORT_EPIER |=  MII_EPIER;
366    MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT7 | MCF5282_INTC_IMRL_MASKALL);
367}
368
369/*
370 * Soak up buffer descriptors that have been sent.
371 */
372static void
373fec_retire_tx_bd(volatile struct mcf5282_enet_struct *sc )
374{
375    struct mbuf *m, *n;
376    uint16_t status;
377
378    while ((sc->txBdActiveCount != 0)
379        && (((status = sc->txBdBase[sc->txBdTail].status) & MCF5282_FEC_TxBD_R) == 0)) {
380        if ((status & MCF5282_FEC_TxBD_TO1) == 0) {
381            m = sc->txMbuf[sc->txBdTail];
382            MFREE(m, n);
383        }
384        if (++sc->txBdTail == sc->txBdCount)
385            sc->txBdTail = 0;
386        sc->txBdActiveCount--;
387    }
388}
389
390static void
391fec_rxDaemon (void *arg)
392{
393    volatile struct mcf5282_enet_struct *sc = (volatile struct mcf5282_enet_struct *)arg;
394    struct ifnet *ifp = (struct ifnet* )&sc->arpcom.ac_if;
395    struct mbuf *m;
396    uint16_t status;
397    volatile mcf5282BufferDescriptor_t *rxBd;
398    int rxBdIndex;
399
400    /*
401     * Allocate space for incoming packets and start reception
402     */
403    for (rxBdIndex = 0 ; ;) {
404        rxBd = sc->rxBdBase + rxBdIndex;
405        MGETHDR(m, M_WAIT, MT_DATA);
406        MCLGET(m, M_WAIT);
407        m->m_pkthdr.rcvif = ifp;
408        sc->rxMbuf[rxBdIndex] = m;
409        rxBd->buffer = mtod(m, void *);
410        rxBd->status = MCF5282_FEC_RxBD_E;
411        if (++rxBdIndex == sc->rxBdCount) {
412            rxBd->status |= MCF5282_FEC_RxBD_W;
413            break;
414        }
415    }
416
417    /*
418     * Input packet handling loop
419     */
420    MCF5282_FEC_RDAR = 0;
421
422    rxBdIndex = 0;
423    for (;;) {
424        rxBd = sc->rxBdBase + rxBdIndex;
425
426        /*
427         * Wait for packet if there's not one ready
428         */
429        if ((status = rxBd->status) & MCF5282_FEC_RxBD_E) {
430            /*
431             * Clear old events.
432             */
433            MCF5282_FEC_EIR = MCF5282_FEC_EIR_RXF;
434
435            /*
436             * Wait for packet to arrive.
437             * Check the buffer descriptor before waiting for the event.
438             * This catches the case when a packet arrives between the
439             * `if' above, and the clearing of the RXF bit in the EIR.
440             */
441            while ((status = rxBd->status) & MCF5282_FEC_RxBD_E) {
442                rtems_event_set events;
443                int level;
444
445                rtems_interrupt_disable(level);
446                MCF5282_FEC_EIMR |= MCF5282_FEC_EIMR_RXF;
447                rtems_interrupt_enable(level);
448                rtems_bsdnet_event_receive (RX_INTERRUPT_EVENT,
449                                            RTEMS_WAIT|RTEMS_EVENT_ANY,
450                                            RTEMS_NO_TIMEOUT,
451                                            &events);
452            }
453        }
454
455        /*
456         * Check that packet is valid
457         */
458        if (status & MCF5282_FEC_RxBD_L) {
459            /*
460             * Pass the packet up the chain.
461             * FIXME: Packet filtering hook could be done here.
462             */
463            struct ether_header *eh;
464            int len = rxBd->length - sizeof(uint32_t);;
465
466            m = sc->rxMbuf[rxBdIndex];
467#ifdef RTEMS_MCF5282_BSP_ENABLE_DATA_CACHE
468            /*
469             * Invalidate the cache.  The cache is so small that it's
470             * more efficient to just invalidate the whole thing unless
471             * the packet is very small.
472             */
473            if (len < 128)
474                rtems_cache_invalidate_multiple_data_lines(m->m_data, len);
475            else
476                rtems_cache_invalidate_entire_data();
477#endif
478            m->m_len = m->m_pkthdr.len = len - sizeof(struct ether_header);
479            eh = mtod(m, struct ether_header *);
480            m->m_data += sizeof(struct ether_header);
481            ether_input(ifp, eh, m);
482
483            /*
484             * Allocate a new mbuf
485             */
486            MGETHDR(m, M_WAIT, MT_DATA);
487            MCLGET(m, M_WAIT);
488            m->m_pkthdr.rcvif = ifp;
489            sc->rxMbuf[rxBdIndex] = m;
490            rxBd->buffer = mtod(m, void *);
491        }
492
493        /*
494         * Reenable the buffer descriptor
495         */
496        rxBd->status = (status & MCF5282_FEC_RxBD_W) | MCF5282_FEC_RxBD_E;
497        MCF5282_FEC_RDAR = 0;
498
499        /*
500         * Move to next buffer descriptor
501         */
502        if (++rxBdIndex == sc->rxBdCount)
503            rxBdIndex = 0;
504    }
505}
506
507static void
508fec_sendpacket(struct ifnet *ifp, struct mbuf *m)
509{
510    struct mcf5282_enet_struct *sc = ifp->if_softc;
511    volatile mcf5282BufferDescriptor_t *firstTxBd, *txBd;
512    uint16_t status;
513    int nAdded;
514
515   /*
516     * Free up buffer descriptors
517     */
518    fec_retire_tx_bd(sc);
519
520    /*
521     * Set up the transmit buffer descriptors.
522     * No need to pad out short packets since the
523     * hardware takes care of that automatically.
524     * No need to copy the packet to a contiguous buffer
525     * since the hardware is capable of scatter/gather DMA.
526     */
527    nAdded = 0;
528    firstTxBd = sc->txBdBase + sc->txBdHead;
529   
530    while (m != NULL) {
531        /*
532         * Wait for buffer descriptor to become available
533         */
534        if ((sc->txBdActiveCount + nAdded)  == sc->txBdCount) {
535            /*
536             * Clear old events.
537             */
538            MCF5282_FEC_EIR = MCF5282_FEC_EIR_TXF;
539
540            /*
541             * Wait for buffer descriptor to become available.
542             * Check for buffer descriptors before waiting for the event.
543             * This catches the case when a buffer became available between
544             * the `if' above, and the clearing of the TXF bit in the EIR.
545             */
546            fec_retire_tx_bd(sc);
547            while ((sc->txBdActiveCount + nAdded) == sc->txBdCount) {
548                rtems_event_set events;
549                int level;
550
551                rtems_interrupt_disable(level);
552                MCF5282_FEC_EIMR |= MCF5282_FEC_EIMR_TXF;   
553                rtems_interrupt_enable(level);
554                sc->txRawWait++;
555                rtems_bsdnet_event_receive(TX_INTERRUPT_EVENT,
556                                           RTEMS_WAIT|RTEMS_EVENT_ANY,
557                                           RTEMS_NO_TIMEOUT,
558                                           &events);
559                fec_retire_tx_bd(sc);
560            }
561        }
562   
563        /*
564         * Don't set the READY flag on the first fragment
565         * until the whole packet has been readied.
566         */
567        status = nAdded ? MCF5282_FEC_TxBD_R : 0;
568   
569        /*
570         * The IP fragmentation routine in ip_output
571         * can produce fragments with zero length.
572         */
573        txBd = sc->txBdBase + sc->txBdHead;
574        if (m->m_len) {
575            char *p = mtod(m, char *);
576            int offset = (int)p & 0x3;
577            if (offset == 0) {
578                txBd->buffer = p;
579                txBd->length = m->m_len;
580                sc->txMbuf[sc->txBdHead] = m;
581                m = m->m_next;
582            }
583            else {
584                /*
585                 * Stupid FEC can't handle misaligned data!
586                 * Move offending bytes to a local buffer.
587                 * Use buffer descriptor TO1 bit to indicate this.
588                 */
589                int nmove = 4 - offset;
590                char *d = (char *)&sc->txMbuf[sc->txBdHead];
591                status |= MCF5282_FEC_TxBD_TO1;
592                sc->txRealign++;
593                if (nmove > m->m_len)
594                    nmove = m->m_len;
595                m->m_data += nmove;
596                m->m_len -= nmove;
597                txBd->buffer = d;
598                txBd->length = nmove;
599                while (nmove--)
600                    *d++ = *p++;
601                if (m->m_len == 0) {
602                    struct mbuf *n;
603                    sc->txRealignDrop++;
604                    MFREE(m, n);
605                    m = n;
606                }
607            }
608            nAdded++;
609            if (++sc->txBdHead == sc->txBdCount) {
610                status |= MCF5282_FEC_TxBD_W;
611                sc->txBdHead = 0;
612            }
613            txBd->status = status;
614        }
615        else {
616            /*
617             * Toss empty mbufs.
618             */
619            struct mbuf *n;
620            MFREE(m, n);
621            m = n;
622        }
623    }
624    if (nAdded) {
625        txBd->status = status | MCF5282_FEC_TxBD_R
626                              | MCF5282_FEC_TxBD_L
627                              | MCF5282_FEC_TxBD_TC;
628        if (nAdded > 1)
629            firstTxBd->status |= MCF5282_FEC_TxBD_R;
630        MCF5282_FEC_TDAR = 0;
631        sc->txBdActiveCount += nAdded;
632      }
633}
634
635void
636fec_txDaemon(void *arg)
637{
638    struct mcf5282_enet_struct *sc = (struct mcf5282_enet_struct *)arg;
639    struct ifnet *ifp = &sc->arpcom.ac_if;
640    struct mbuf *m;
641    rtems_event_set events;
642
643    for (;;) {
644        /*
645         * Wait for packet
646         */
647        rtems_bsdnet_event_receive(START_TRANSMIT_EVENT,
648                                    RTEMS_EVENT_ANY | RTEMS_WAIT,
649                                    RTEMS_NO_TIMEOUT,
650                                    &events);
651
652        /*
653         * Send packets till queue is empty
654         */
655        for (;;) {
656            /*
657             * Get the next mbuf chain to transmit.
658             */
659            IF_DEQUEUE(&ifp->if_snd, m);
660            if (!m)
661                break;
662            fec_sendpacket(ifp, m);
663        }
664        ifp->if_flags &= ~IFF_OACTIVE;
665    }
666}
667
668
669/*
670 * Send packet (caller provides header).
671 */
672static void
673mcf5282_enet_start(struct ifnet *ifp)
674{
675    struct mcf5282_enet_struct *sc = ifp->if_softc;
676
677    rtems_event_send(sc->txDaemonTid, START_TRANSMIT_EVENT);
678    ifp->if_flags |= IFF_OACTIVE;
679}
680
681static void
682fec_init(void *arg)
683{
684    struct mcf5282_enet_struct *sc = arg;
685    struct ifnet *ifp = &sc->arpcom.ac_if;
686
687    if (sc->txDaemonTid == 0) {
688        /*
689         * Set up hardware
690         */
691        mcf5282_fec_initialize_hardware(sc);
692
693        /*
694         * Start driver tasks
695         */
696        sc->txDaemonTid = rtems_bsdnet_newproc("FECtx", 4096, fec_txDaemon, sc);
697        sc->rxDaemonTid = rtems_bsdnet_newproc("FECrx", 4096, fec_rxDaemon, sc);
698    }
699
700    /*
701     * Set flags appropriately
702     */
703    if (ifp->if_flags & IFF_PROMISC)
704        MCF5282_FEC_RCR |= MCF5282_FEC_RCR_PROM;
705    else
706        MCF5282_FEC_RCR &= ~MCF5282_FEC_RCR_PROM;
707
708    /*
709     * Tell the world that we're running.
710     */
711    ifp->if_flags |= IFF_RUNNING;
712
713    /*
714     * Enable receiver and transmitter
715     */
716    MCF5282_FEC_ECR = MCF5282_FEC_ECR_ETHER_EN;
717}
718
719
720static void
721fec_stop(struct mcf5282_enet_struct *sc)
722{
723    struct ifnet *ifp = &sc->arpcom.ac_if;
724
725    ifp->if_flags &= ~IFF_RUNNING;
726
727    /*
728     * Shut down receiver and transmitter
729     */
730    MCF5282_FEC_ECR = 0x0;
731}
732
733/*
734 * Show interface statistics
735 */
736static void
737enet_stats(struct mcf5282_enet_struct *sc)
738{
739    printf("  Rx Interrupts:%-10lu",   sc->rxInterrupts);
740    printf("Rx Packet Count:%-10lu",   MCF5282_FEC_RMON_R_PACKETS);
741    printf("   Rx Broadcast:%-10lu\n", MCF5282_FEC_RMON_R_BC_PKT);
742    printf("   Rx Multicast:%-10lu",   MCF5282_FEC_RMON_R_MC_PKT);
743    printf("CRC/Align error:%-10lu",   MCF5282_FEC_RMON_R_CRC_ALIGN);
744    printf("   Rx Undersize:%-10lu\n", MCF5282_FEC_RMON_R_UNDERSIZE);
745    printf("    Rx Oversize:%-10lu",   MCF5282_FEC_RMON_R_OVERSIZE);
746    printf("    Rx Fragment:%-10lu",   MCF5282_FEC_RMON_R_FRAG);
747    printf("      Rx Jabber:%-10lu\n", MCF5282_FEC_RMON_R_JAB);
748    printf("          Rx 64:%-10lu",   MCF5282_FEC_RMON_R_P64);
749    printf("      Rx 65-127:%-10lu",   MCF5282_FEC_RMON_R_P65T0127);
750    printf("     Rx 128-255:%-10lu\n", MCF5282_FEC_RMON_R_P128TO255);
751    printf("     Rx 256-511:%-10lu",   MCF5282_FEC_RMON_R_P256TO511);
752    printf("    Rx 511-1023:%-10lu",   MCF5282_FEC_RMON_R_P512TO1023);
753    printf("   Rx 1024-2047:%-10lu\n", MCF5282_FEC_RMON_R_P1024TO2047);
754    printf("      Rx >=2048:%-10lu",   MCF5282_FEC_RMON_R_GTE2048);
755    printf("      Rx Octets:%-10lu",   MCF5282_FEC_RMON_R_OCTETS);
756    printf("     Rx Dropped:%-10lu\n", MCF5282_FEC_IEEE_R_DROP);
757    printf("    Rx frame OK:%-10lu",   MCF5282_FEC_IEEE_R_FRAME_OK);
758    printf("   Rx CRC error:%-10lu",   MCF5282_FEC_IEEE_R_CRC);
759    printf(" Rx Align error:%-10lu\n", MCF5282_FEC_IEEE_R_ALIGN);
760    printf("  FIFO Overflow:%-10lu",   MCF5282_FEC_IEEE_R_MACERR);
761    printf("Rx Pause Frames:%-10lu",   MCF5282_FEC_IEEE_R_FDXFC);
762    printf("   Rx Octets OK:%-10lu\n", MCF5282_FEC_IEEE_R_OCTETS_OK);
763    printf("  Tx Interrupts:%-10lu",   sc->txInterrupts);
764    printf("Tx Output Waits:%-10lu",   sc->txRawWait);
765    printf("Tx mbuf realign:%-10lu\n", sc->txRealign);
766    printf("Tx realign drop:%-10lu",   sc->txRealignDrop);
767    printf(" Tx Unaccounted:%-10lu",   MCF5282_FEC_RMON_T_DROP);
768    printf("Tx Packet Count:%-10lu\n", MCF5282_FEC_RMON_T_PACKETS);
769    printf("   Tx Broadcast:%-10lu",   MCF5282_FEC_RMON_T_BC_PKT);
770    printf("   Tx Multicast:%-10lu",   MCF5282_FEC_RMON_T_MC_PKT);
771    printf("CRC/Align error:%-10lu\n", MCF5282_FEC_RMON_T_CRC_ALIGN);
772    printf("   Tx Undersize:%-10lu",   MCF5282_FEC_RMON_T_UNDERSIZE);
773    printf("    Tx Oversize:%-10lu",   MCF5282_FEC_RMON_T_OVERSIZE);
774    printf("    Tx Fragment:%-10lu\n", MCF5282_FEC_RMON_T_FRAG);
775    printf("      Tx Jabber:%-10lu",   MCF5282_FEC_RMON_T_JAB);
776    printf("  Tx Collisions:%-10lu",   MCF5282_FEC_RMON_T_COL);
777    printf("          Tx 64:%-10lu\n", MCF5282_FEC_RMON_T_P64);
778    printf("      Tx 65-127:%-10lu",   MCF5282_FEC_RMON_T_P65TO127);
779    printf("     Tx 128-255:%-10lu",   MCF5282_FEC_RMON_T_P128TO255);
780    printf("     Tx 256-511:%-10lu\n", MCF5282_FEC_RMON_T_P256TO511);
781    printf("    Tx 511-1023:%-10lu",   MCF5282_FEC_RMON_T_P512TO1023);
782    printf("   Tx 1024-2047:%-10lu",   MCF5282_FEC_RMON_T_P1024TO2047);
783    printf("      Tx >=2048:%-10lu\n", MCF5282_FEC_RMON_T_P_GTE2048);
784    printf("      Tx Octets:%-10lu",   MCF5282_FEC_RMON_T_OCTETS);
785    printf("     Tx Dropped:%-10lu",   MCF5282_FEC_IEEE_T_DROP);
786    printf("    Tx Frame OK:%-10lu\n", MCF5282_FEC_IEEE_T_FRAME_OK);
787    printf(" Tx 1 Collision:%-10lu",   MCF5282_FEC_IEEE_T_1COL);
788    printf("Tx >1 Collision:%-10lu",   MCF5282_FEC_IEEE_T_MCOL);
789    printf("    Tx Deferred:%-10lu\n", MCF5282_FEC_IEEE_T_DEF);
790    printf(" Late Collision:%-10lu",   MCF5282_FEC_IEEE_T_LCOL);
791    printf(" Excessive Coll:%-10lu",   MCF5282_FEC_IEEE_T_EXCOL);
792    printf("  FIFO Underrun:%-10lu\n", MCF5282_FEC_IEEE_T_MACERR);
793    printf("  Carrier Error:%-10lu",   MCF5282_FEC_IEEE_T_CSERR);
794    printf("   Tx SQE Error:%-10lu",   MCF5282_FEC_IEEE_T_SQE);
795    printf("Tx Pause Frames:%-10lu\n", MCF5282_FEC_IEEE_T_FDXFC);
796    printf("   Tx Octets OK:%-10lu",   MCF5282_FEC_IEEE_T_OCTETS_OK);
797    printf(" MII interrupts:%-10lu\n", sc->miiInterrupts);
798    if ((sc->mii_sr2 & 0x400) == 0) {
799        printf("LINK DOWN!\n");
800    }
801    else {
802        printf("Link speed %d Mb/s, %s-duplex.\n",
803                                    sc->mii_sr2  & 0x4000 ? 100 : 10,
804                                    sc->mii_sr2 & 0x200 ? "full" : "half");
805    }
806    printf(" EIR:%8.8lx  ",  MCF5282_FEC_EIR);
807    printf("EIMR:%8.8lx  ",  MCF5282_FEC_EIMR);
808    printf("RDAR:%8.8lx  ",  MCF5282_FEC_RDAR);
809    printf("TDAR:%8.8lx\n",  MCF5282_FEC_TDAR);
810    printf(" ECR:%8.8lx  ",  MCF5282_FEC_ECR);
811    printf(" RCR:%8.8lx  ",  MCF5282_FEC_RCR);
812    printf(" TCR:%8.8lx\n",  MCF5282_FEC_TCR);
813    printf("FRBR:%8.8lx  ",  MCF5282_FEC_FRBR);
814    printf("FRSR:%8.8lx\n",  MCF5282_FEC_FRSR);
815    if (sc->txBdActiveCount != 0) {
816        int i, n;
817        /*
818         * Yes, there are races here with adding and retiring descriptors,
819         * but this diagnostic is more for when things have backed up.
820         */
821        printf("Transmit Buffer Descriptors (Tail %d, Head %d, Unretired %d):\n",
822                                                    sc->txBdTail,
823                                                    sc->txBdHead,
824                                                    sc->txBdActiveCount);
825        i = sc->txBdTail;
826        for (n = 0 ; n < sc->txBdCount ; n++) {
827            if ((sc->txBdBase[i].status & MCF5282_FEC_TxBD_R) != 0)
828                printf("  %3d: status:%4.4x  length:%-4d  buffer:%p\n",
829                                                    i,
830                                                    sc->txBdBase[i].status,
831                                                    sc->txBdBase[i].length,
832                                                    sc->txBdBase[i].buffer);
833            if (++i == sc->txBdCount)
834                i = 0;
835        }
836    }
837}
838
839static int
840fec_ioctl(struct ifnet *ifp, int command, caddr_t data)
841{
842    struct mcf5282_enet_struct *sc = ifp->if_softc;
843    int error = 0;
844
845    switch (command) {
846        case SIOCGIFADDR:
847        case SIOCSIFADDR:
848            ether_ioctl(ifp, command, data);
849            break;
850
851        case SIOCSIFFLAGS:
852            switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
853                case IFF_RUNNING:
854                    fec_stop(sc);
855                    break;
856
857                case IFF_UP:
858                    fec_init(sc);
859                    break;
860
861                case IFF_UP | IFF_RUNNING:
862                    fec_stop(sc);
863                    fec_init(sc);
864                    break;
865
866                default:
867                    break;
868            }
869            break;
870
871        case SIO_RTEMS_SHOW_STATS:
872            enet_stats(sc);
873            break;
874
875            /*
876             * FIXME: All sorts of multicast commands need to be added here!
877             */
878        default:
879            error = EINVAL;
880            break;
881    }
882    return error;
883}
884
885int
886rtems_fec_driver_attach(struct rtems_bsdnet_ifconfig *config, int attaching )
887{
888    struct mcf5282_enet_struct *sc;
889    struct ifnet *ifp;
890    int mtu;
891    int unitNumber;
892    char *unitName;
893    unsigned char *hwaddr;
894    const char *env;
895
896    /*
897     * Parse driver name
898     */
899    if ((unitNumber = rtems_bsdnet_parse_driver_name (config, &unitName)) < 0)
900        return 0;
901
902    /*
903     * Is driver free?
904     */
905    if ((unitNumber <= 0) || (unitNumber > NIFACES)) {
906        printf("Bad FEC unit number.\n");
907        return 0;
908    }
909    sc = &enet_driver[unitNumber - 1];
910    ifp = &sc->arpcom.ac_if;
911    if (ifp->if_softc != NULL) {
912        printf("Driver already in use.\n");
913        return 0;
914    }
915
916    /*
917     * Process options
918     */
919    if (config->hardware_address) {
920        hwaddr = config->hardware_address;
921    } else if ((hwaddr = bsp_gethwaddr(unitNumber - 1)) == NULL) {
922        /* Locally-administered address */
923        static const unsigned char defaultAddress[ETHER_ADDR_LEN] = {
924                                        0x06, 'R', 'T', 'E', 'M', 'S'};
925        printf ("WARNING -- No %s%d Ethernet address specified "
926                "-- Using default address.\n", unitName, unitNumber);
927        hwaddr = defaultAddress;
928    }
929    printf("%s%d: Ethernet address: %02x:%02x:%02x:%02x:%02x:%02x\n",
930                                            unitName, unitNumber,
931                                            hwaddr[0], hwaddr[1], hwaddr[2],
932                                            hwaddr[3], hwaddr[4], hwaddr[5]);
933    memcpy(sc->arpcom.ac_enaddr, hwaddr, ETHER_ADDR_LEN);
934
935    if (config->mtu)
936        mtu = config->mtu;
937    else
938        mtu = ETHERMTU;
939    if (config->rbuf_count)
940        sc->rxBdCount = config->rbuf_count;
941    else
942        sc->rxBdCount = RX_BUF_COUNT;
943    if (config->xbuf_count)
944        sc->txBdCount = config->xbuf_count;
945    else
946        sc->txBdCount = TX_BUF_COUNT * TX_BD_PER_BUF;
947
948    sc->acceptBroadcast = !config->ignore_broadcast;
949
950    /*
951     * Set up network interface values
952     */
953    ifp->if_softc = sc;
954    ifp->if_unit = unitNumber;
955    ifp->if_name = unitName;
956    ifp->if_mtu = mtu;
957    ifp->if_init = fec_init;
958    ifp->if_ioctl = fec_ioctl;
959    ifp->if_start = mcf5282_enet_start;
960    ifp->if_output = ether_output;
961    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
962    if (ifp->if_snd.ifq_maxlen == 0)
963        ifp->if_snd.ifq_maxlen = ifqmaxlen;
964
965    /*
966     * Check for environment overrides
967     */
968    if (((env = bsp_getbenv("IPADDR0_100FULL")) != NULL)
969     && ((*env == 'y') || (*env == 'Y')))
970        sc->force100Full = 1;
971
972    /*
973     * Attach the interface
974     */
975    if_attach(ifp);
976    ether_ifattach(ifp);
977    return 1;
978};
Note: See TracBrowser for help on using the repository browser.