linux-old/arch/ppc/8xx_io/enet.c
<<
>>
Prefs
   1/*
   2 * Ethernet driver for Motorola MPC8xx.
   3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
   4 *
   5 * I copied the basic skeleton from the lance driver, because I did not
   6 * know how to write the Linux driver, but I did know how the LANCE worked.
   7 *
   8 * This version of the driver is somewhat selectable for the different
   9 * processor/board combinations.  It works for the boards I know about
  10 * now, and should be easily modified to include others.  Some of the
  11 * configuration information is contained in <asm/commproc.h> and the
  12 * remainder is here.
  13 *
  14 * Buffer descriptors are kept in the CPM dual port RAM, and the frame
  15 * buffers are in the host memory.
  16 *
  17 * Right now, I am very watseful with the buffers.  I allocate memory
  18 * pages and then divide them into 2K frame buffers.  This way I know I
  19 * have buffers large enough to hold one frame within one buffer descriptor.
  20 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  21 * will be much more memory efficient and will easily handle lots of
  22 * small packets.
  23 *
  24 */
  25#include <linux/config.h>
  26#include <linux/kernel.h>
  27#include <linux/sched.h>
  28#include <linux/string.h>
  29#include <linux/ptrace.h>
  30#include <linux/errno.h>
  31#include <linux/ioport.h>
  32#include <linux/slab.h>
  33#include <linux/interrupt.h>
  34#include <linux/pci.h>
  35#include <linux/init.h>
  36#include <linux/delay.h>
  37#include <linux/netdevice.h>
  38#include <linux/etherdevice.h>
  39#include <linux/skbuff.h>
  40#include <linux/spinlock.h>
  41
  42#include <asm/8xx_immap.h>
  43#include <asm/pgtable.h>
  44#include <asm/mpc8xx.h>
  45#include <asm/bitops.h>
  46#include <asm/uaccess.h>
  47#include <asm/commproc.h>
  48#include <asm/irq.h>
  49
  50/*
  51 *                              Theory of Operation
  52 *
  53 * The MPC8xx CPM performs the Ethernet processing on SCC1.  It can use
  54 * an aribtrary number of buffers on byte boundaries, but must have at
  55 * least two receive buffers to prevent constant overrun conditions.
  56 *
  57 * The buffer descriptors are allocated from the CPM dual port memory
  58 * with the data buffers allocated from host memory, just like all other
  59 * serial communication protocols.  The host memory buffers are allocated
  60 * from the free page pool, and then divided into smaller receive and
  61 * transmit buffers.  The size of the buffers should be a power of two,
  62 * since that nicely divides the page.  This creates a ring buffer
  63 * structure similar to the LANCE and other controllers.
  64 *
  65 * Like the LANCE driver:
  66 * The driver runs as two independent, single-threaded flows of control.  One
  67 * is the send-packet routine, which enforces single-threaded use by the
  68 * cep->tx_busy flag.  The other thread is the interrupt handler, which is
  69 * single threaded by the hardware and other software.
  70 *
  71 * The send packet thread has partial control over the Tx ring and the
  72 * 'cep->tx_busy' flag.  It sets the tx_busy flag whenever it's queuing a Tx
  73 * packet. If the next queue slot is empty, it clears the tx_busy flag when
  74 * finished otherwise it sets the 'lp->tx_full' flag.
  75 *
  76 * The MBX has a control register external to the MPC8xx that has some
  77 * control of the Ethernet interface.  Information is in the manual for
  78 * your board.
  79 *
  80 * The RPX boards have an external control/status register.  Consult the
  81 * programming documents for details unique to your board.
  82 *
  83 * For the TQM8xx(L) modules, there is no control register interface.
  84 * All functions are directly controlled using I/O pins.  See <asm/commproc.h>.
  85 */
  86
  87/* The transmitter timeout
  88 */
  89#define TX_TIMEOUT      (2*HZ)
  90
  91/* The number of Tx and Rx buffers.  These are allocated from the page
  92 * pool.  The code may assume these are power of two, so it is best
  93 * to keep them that size.
  94 * We don't need to allocate pages for the transmitter.  We just use
  95 * the skbuffer directly.
  96 */
  97#ifdef CONFIG_ENET_BIG_BUFFERS
  98#define CPM_ENET_RX_PAGES       32
  99#define CPM_ENET_RX_FRSIZE      2048
 100#define CPM_ENET_RX_FRPPG       (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
 101#define RX_RING_SIZE            (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
 102#define TX_RING_SIZE            64      /* Must be power of two */
 103#define TX_RING_MOD_MASK        63      /*   for this to work */
 104#else
 105#define CPM_ENET_RX_PAGES       4
 106#define CPM_ENET_RX_FRSIZE      2048
 107#define CPM_ENET_RX_FRPPG       (PAGE_SIZE / CPM_ENET_RX_FRSIZE)
 108#define RX_RING_SIZE            (CPM_ENET_RX_FRPPG * CPM_ENET_RX_PAGES)
 109#define TX_RING_SIZE            8       /* Must be power of two */
 110#define TX_RING_MOD_MASK        7       /*   for this to work */
 111#endif
 112
 113/* The CPM stores dest/src/type, data, and checksum for receive packets.
 114 */
 115#define PKT_MAXBUF_SIZE         1518
 116#define PKT_MINBUF_SIZE         64
 117#define PKT_MAXBLR_SIZE         1520
 118
 119/* The CPM buffer descriptors track the ring buffers.  The rx_bd_base and
 120 * tx_bd_base always point to the base of the buffer descriptors.  The
 121 * cur_rx and cur_tx point to the currently available buffer.
 122 * The dirty_tx tracks the current buffer that is being sent by the
 123 * controller.  The cur_tx and dirty_tx are equal under both completely
 124 * empty and completely full conditions.  The empty/ready indicator in
 125 * the buffer descriptor determines the actual condition.
 126 */
 127struct scc_enet_private {
 128        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 129        struct  sk_buff* tx_skbuff[TX_RING_SIZE];
 130        ushort  skb_cur;
 131        ushort  skb_dirty;
 132
 133        /* CPM dual port RAM relative addresses.
 134        */
 135        cbd_t   *rx_bd_base;            /* Address of Rx and Tx buffers. */
 136        cbd_t   *tx_bd_base;
 137        cbd_t   *cur_rx, *cur_tx;               /* The next free ring entry */
 138        cbd_t   *dirty_tx;      /* The ring entries to be free()ed. */
 139        scc_t   *sccp;
 140
 141        /* Virtual addresses for the receive buffers because we can't
 142         * do a __va() on them anymore.
 143         */
 144        unsigned char *rx_vaddr[RX_RING_SIZE];
 145        struct  net_device_stats stats;
 146        uint    tx_free;
 147        spinlock_t lock;
 148};
 149
 150static int scc_enet_open(struct net_device *dev);
 151static int scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
 152static int scc_enet_rx(struct net_device *dev);
 153static int scc_enet_close(struct net_device *dev);
 154static struct net_device_stats *scc_enet_get_stats(struct net_device *dev);
 155static void set_multicast_list(struct net_device *dev);
 156
 157/* Get this from various configuration locations (depends on board).
 158*/
 159/*static        ushort  my_enet_addr[] = { 0x0800, 0x3e26, 0x1559 };*/
 160
 161/* Typically, 860(T) boards use SCC1 for Ethernet, and other 8xx boards
 162 * use SCC2. Some even may use SCC3.
 163 * This is easily extended if necessary.
 164 */
 165#if defined(CONFIG_SCC3_ENET)
 166#define CPM_CR_ENET     CPM_CR_CH_SCC3
 167#define PROFF_ENET      PROFF_SCC3
 168#define SCC_ENET        2               /* Index, not number! */
 169#define CPMVEC_ENET     CPMVEC_SCC3
 170#elif defined(CONFIG_SCC2_ENET)
 171#define CPM_CR_ENET     CPM_CR_CH_SCC2
 172#define PROFF_ENET      PROFF_SCC2
 173#define SCC_ENET        1               /* Index, not number! */
 174#define CPMVEC_ENET     CPMVEC_SCC2
 175#elif defined(CONFIG_SCC1_ENET)
 176#define CPM_CR_ENET     CPM_CR_CH_SCC1
 177#define PROFF_ENET      PROFF_SCC1
 178#define SCC_ENET        0               /* Index, not number! */
 179#define CPMVEC_ENET     CPMVEC_SCC1
 180#else
 181#error CONFIG_SCCx_ENET not defined
 182#endif
 183
 184static int
 185scc_enet_open(struct net_device *dev)
 186{
 187
 188        /* I should reset the ring buffers here, but I don't yet know
 189         * a simple way to do that.
 190         */
 191
 192        netif_start_queue(dev);
 193        return 0;                                       /* Always succeed */
 194}
 195
 196static int
 197scc_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
 198{
 199        struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
 200        volatile cbd_t  *bdp;
 201
 202        /* Fill in a Tx ring entry */
 203        bdp = cep->cur_tx;
 204
 205#ifndef final_version
 206        if (!cep->tx_free || (bdp->cbd_sc & BD_ENET_TX_READY)) {
 207                /* Ooops.  All transmit buffers are full.  Bail out.
 208                 * This should not happen, since the tx queue should be stopped.
 209                 */
 210                printk("%s: tx queue full!.\n", dev->name);
 211                return 1;
 212        }
 213#endif
 214
 215        /* Clear all of the status flags.
 216         */
 217        bdp->cbd_sc &= ~BD_ENET_TX_STATS;
 218
 219        /* If the frame is short, tell CPM to pad it.
 220        */
 221        if (skb->len <= ETH_ZLEN)
 222                bdp->cbd_sc |= BD_ENET_TX_PAD;
 223        else
 224                bdp->cbd_sc &= ~BD_ENET_TX_PAD;
 225
 226        /* Set buffer length and buffer pointer.
 227        */
 228        bdp->cbd_datlen = skb->len;
 229        bdp->cbd_bufaddr = __pa(skb->data);
 230
 231        /* Save skb pointer.
 232        */
 233        cep->tx_skbuff[cep->skb_cur] = skb;
 234
 235        cep->stats.tx_bytes += skb->len;
 236        cep->skb_cur = (cep->skb_cur+1) & TX_RING_MOD_MASK;
 237
 238        /* Push the data cache so the CPM does not get stale memory
 239         * data.
 240         */
 241        flush_dcache_range((unsigned long)(skb->data),
 242                                        (unsigned long)(skb->data + skb->len));
 243
 244        spin_lock_irq(&cep->lock);
 245
 246        /* Send it on its way.  Tell CPM its ready, interrupt when done,
 247         * its the last BD of the frame, and to put the CRC on the end.
 248         */
 249        bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR | BD_ENET_TX_LAST | BD_ENET_TX_TC);
 250
 251        dev->trans_start = jiffies;
 252
 253        /* If this was the last BD in the ring, start at the beginning again.
 254        */
 255        if (bdp->cbd_sc & BD_ENET_TX_WRAP)
 256                bdp = cep->tx_bd_base;
 257        else
 258                bdp++;
 259
 260        if (!--cep->tx_free)
 261                netif_stop_queue(dev);
 262
 263        cep->cur_tx = (cbd_t *)bdp;
 264
 265        spin_unlock_irq(&cep->lock);
 266
 267        return 0;
 268}
 269
 270static void
 271scc_enet_timeout(struct net_device *dev)
 272{
 273        struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
 274
 275        printk("%s: transmit timed out.\n", dev->name);
 276        cep->stats.tx_errors++;
 277#ifndef final_version
 278        {
 279                int     i;
 280                cbd_t   *bdp;
 281                printk(" Ring data dump: cur_tx %p tx_free %d cur_rx %p.\n",
 282                       cep->cur_tx, cep->tx_free,
 283                       cep->cur_rx);
 284                bdp = cep->tx_bd_base;
 285                for (i = 0 ; i < TX_RING_SIZE; i++, bdp++)
 286                        printk("%04x %04x %08x\n",
 287                               bdp->cbd_sc,
 288                               bdp->cbd_datlen,
 289                               bdp->cbd_bufaddr);
 290                bdp = cep->rx_bd_base;
 291                for (i = 0 ; i < RX_RING_SIZE; i++, bdp++)
 292                        printk("%04x %04x %08x\n",
 293                               bdp->cbd_sc,
 294                               bdp->cbd_datlen,
 295                               bdp->cbd_bufaddr);
 296        }
 297#endif
 298        if (cep->tx_free)
 299                netif_wake_queue(dev);
 300}
 301
 302/* The interrupt handler.
 303 * This is called from the CPM handler, not the MPC core interrupt.
 304 */
 305static void
 306scc_enet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 307{
 308        struct  net_device *dev = dev_id;
 309        volatile struct scc_enet_private *cep;
 310        volatile cbd_t  *bdp;
 311        ushort  int_events;
 312        int     must_restart;
 313
 314        cep = (struct scc_enet_private *)dev->priv;
 315
 316        /* Get the interrupt events that caused us to be here.
 317        */
 318        int_events = cep->sccp->scc_scce;
 319        cep->sccp->scc_scce = int_events;
 320        must_restart = 0;
 321
 322        /* Handle receive event in its own function.
 323        */
 324        if (int_events & SCCE_ENET_RXF)
 325                scc_enet_rx(dev_id);
 326
 327        /* Check for a transmit error.  The manual is a little unclear
 328         * about this, so the debug code until I get it figured out.  It
 329         * appears that if TXE is set, then TXB is not set.  However,
 330         * if carrier sense is lost during frame transmission, the TXE
 331         * bit is set, "and continues the buffer transmission normally."
 332         * I don't know if "normally" implies TXB is set when the buffer
 333         * descriptor is closed.....trial and error :-).
 334         */
 335
 336        /* Transmit OK, or non-fatal error.  Update the buffer descriptors.
 337        */
 338        if (int_events & (SCCE_ENET_TXE | SCCE_ENET_TXB)) {
 339            spin_lock(&cep->lock);
 340            bdp = cep->dirty_tx;
 341            while ((bdp->cbd_sc&BD_ENET_TX_READY)==0) {
 342                if (cep->tx_free == TX_RING_SIZE)
 343                    break;
 344
 345                if (bdp->cbd_sc & BD_ENET_TX_HB)        /* No heartbeat */
 346                        cep->stats.tx_heartbeat_errors++;
 347                if (bdp->cbd_sc & BD_ENET_TX_LC)        /* Late collision */
 348                        cep->stats.tx_window_errors++;
 349                if (bdp->cbd_sc & BD_ENET_TX_RL)        /* Retrans limit */
 350                        cep->stats.tx_aborted_errors++;
 351                if (bdp->cbd_sc & BD_ENET_TX_UN)        /* Underrun */
 352                        cep->stats.tx_fifo_errors++;
 353                if (bdp->cbd_sc & BD_ENET_TX_CSL)       /* Carrier lost */
 354                        cep->stats.tx_carrier_errors++;
 355
 356
 357                /* No heartbeat or Lost carrier are not really bad errors.
 358                 * The others require a restart transmit command.
 359                 */
 360                if (bdp->cbd_sc &
 361                    (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
 362                        must_restart = 1;
 363                        cep->stats.tx_errors++;
 364                }
 365
 366                cep->stats.tx_packets++;
 367
 368                /* Deferred means some collisions occurred during transmit,
 369                 * but we eventually sent the packet OK.
 370                 */
 371                if (bdp->cbd_sc & BD_ENET_TX_DEF)
 372                        cep->stats.collisions++;
 373
 374                /* Free the sk buffer associated with this last transmit.
 375                */
 376                dev_kfree_skb_irq(cep->tx_skbuff[cep->skb_dirty]);
 377                cep->skb_dirty = (cep->skb_dirty + 1) & TX_RING_MOD_MASK;
 378
 379                /* Update pointer to next buffer descriptor to be transmitted.
 380                */
 381                if (bdp->cbd_sc & BD_ENET_TX_WRAP)
 382                        bdp = cep->tx_bd_base;
 383                else
 384                        bdp++;
 385
 386                /* I don't know if we can be held off from processing these
 387                 * interrupts for more than one frame time.  I really hope
 388                 * not.  In such a case, we would now want to check the
 389                 * currently available BD (cur_tx) and determine if any
 390                 * buffers between the dirty_tx and cur_tx have also been
 391                 * sent.  We would want to process anything in between that
 392                 * does not have BD_ENET_TX_READY set.
 393                 */
 394
 395                /* Since we have freed up a buffer, the ring is no longer
 396                 * full.
 397                 */
 398                if (!cep->tx_free++) {
 399                        if (netif_queue_stopped(dev))
 400                                netif_wake_queue(dev);
 401                }
 402
 403                cep->dirty_tx = (cbd_t *)bdp;
 404            }
 405
 406            if (must_restart) {
 407                volatile cpm8xx_t *cp;
 408
 409                /* Some transmit errors cause the transmitter to shut
 410                 * down.  We now issue a restart transmit.  Since the
 411                 * errors close the BD and update the pointers, the restart
 412                 * _should_ pick up without having to reset any of our
 413                 * pointers either.
 414                 */
 415                cp = cpmp;
 416                cp->cp_cpcr =
 417                    mk_cr_cmd(CPM_CR_ENET, CPM_CR_RESTART_TX) | CPM_CR_FLG;
 418                while (cp->cp_cpcr & CPM_CR_FLG);
 419            }
 420            spin_unlock(&cep->lock);
 421        }
 422
 423        /* Check for receive busy, i.e. packets coming but no place to
 424         * put them.
 425         */
 426        if (int_events & SCCE_ENET_BSY)
 427                cep->stats.rx_dropped++;
 428
 429        return;
 430}
 431
 432/* During a receive, the cur_rx points to the current incoming buffer.
 433 * When we update through the ring, if the next incoming buffer has
 434 * not been given to the system, we just set the empty indicator,
 435 * effectively tossing the packet.
 436 */
 437static int
 438scc_enet_rx(struct net_device *dev)
 439{
 440        struct  scc_enet_private *cep;
 441        volatile cbd_t  *bdp;
 442        struct  sk_buff *skb;
 443        ushort  pkt_len;
 444
 445        cep = (struct scc_enet_private *)dev->priv;
 446
 447        /* First, grab all of the stats for the incoming packet.
 448         * These get messed up if we get called due to a busy condition.
 449         */
 450        bdp = cep->cur_rx;
 451
 452for (;;) {
 453        if (bdp->cbd_sc & BD_ENET_RX_EMPTY)
 454                break;
 455
 456#ifndef final_version
 457        /* Since we have allocated space to hold a complete frame, both
 458         * the first and last indicators should be set.
 459         */
 460        if ((bdp->cbd_sc & (BD_ENET_RX_FIRST | BD_ENET_RX_LAST)) !=
 461                (BD_ENET_RX_FIRST | BD_ENET_RX_LAST))
 462                        printk("CPM ENET: rcv is not first+last\n");
 463#endif
 464
 465        /* Frame too long or too short.
 466        */
 467        if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
 468                cep->stats.rx_length_errors++;
 469        if (bdp->cbd_sc & BD_ENET_RX_NO)        /* Frame alignment */
 470                cep->stats.rx_frame_errors++;
 471        if (bdp->cbd_sc & BD_ENET_RX_CR)        /* CRC Error */
 472                cep->stats.rx_crc_errors++;
 473        if (bdp->cbd_sc & BD_ENET_RX_OV)        /* FIFO overrun */
 474                cep->stats.rx_crc_errors++;
 475
 476        /* Report late collisions as a frame error.
 477         * On this error, the BD is closed, but we don't know what we
 478         * have in the buffer.  So, just drop this frame on the floor.
 479         */
 480        if (bdp->cbd_sc & BD_ENET_RX_CL) {
 481                cep->stats.rx_frame_errors++;
 482        }
 483        else {
 484
 485                /* Process the incoming frame.
 486                */
 487                cep->stats.rx_packets++;
 488                pkt_len = bdp->cbd_datlen;
 489                cep->stats.rx_bytes += pkt_len;
 490
 491                /* This does 16 byte alignment, much more than we need.
 492                 * The packet length includes FCS, but we don't want to
 493                 * include that when passing upstream as it messes up
 494                 * bridging applications.
 495                 */
 496                skb = dev_alloc_skb(pkt_len-4);
 497
 498                if (skb == NULL) {
 499                        printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 500                        cep->stats.rx_dropped++;
 501                }
 502                else {
 503                        skb->dev = dev;
 504                        skb_put(skb,pkt_len-4); /* Make room */
 505                        eth_copy_and_sum(skb,
 506                                cep->rx_vaddr[bdp - cep->rx_bd_base],
 507                                pkt_len-4, 0);
 508                        skb->protocol=eth_type_trans(skb,dev);
 509                        netif_rx(skb);
 510                }
 511        }
 512
 513        /* Clear the status flags for this buffer.
 514        */
 515        bdp->cbd_sc &= ~BD_ENET_RX_STATS;
 516
 517        /* Mark the buffer empty.
 518        */
 519        bdp->cbd_sc |= BD_ENET_RX_EMPTY;
 520
 521        /* Update BD pointer to next entry.
 522        */
 523        if (bdp->cbd_sc & BD_ENET_RX_WRAP)
 524                bdp = cep->rx_bd_base;
 525        else
 526                bdp++;
 527
 528   }
 529        cep->cur_rx = (cbd_t *)bdp;
 530
 531        return 0;
 532}
 533
 534static int
 535scc_enet_close(struct net_device *dev)
 536{
 537        /* Don't know what to do yet.
 538        */
 539        netif_stop_queue(dev);
 540
 541        return 0;
 542}
 543
 544static struct net_device_stats *scc_enet_get_stats(struct net_device *dev)
 545{
 546        struct scc_enet_private *cep = (struct scc_enet_private *)dev->priv;
 547
 548        return &cep->stats;
 549}
 550
 551/* Set or clear the multicast filter for this adaptor.
 552 * Skeleton taken from sunlance driver.
 553 * The CPM Ethernet implementation allows Multicast as well as individual
 554 * MAC address filtering.  Some of the drivers check to make sure it is
 555 * a group multicast address, and discard those that are not.  I guess I
 556 * will do the same for now, but just remove the test if you want
 557 * individual filtering as well (do the upper net layers want or support
 558 * this kind of feature?).
 559 */
 560
 561static void set_multicast_list(struct net_device *dev)
 562{
 563        struct  scc_enet_private *cep;
 564        struct  dev_mc_list *dmi;
 565        u_char  *mcptr, *tdptr;
 566        volatile scc_enet_t *ep;
 567        int     i, j;
 568        cep = (struct scc_enet_private *)dev->priv;
 569
 570        /* Get pointer to SCC area in parameter RAM.
 571        */
 572        ep = (scc_enet_t *)dev->base_addr;
 573
 574        if (dev->flags&IFF_PROMISC) {
 575
 576                /* Log any net taps. */
 577                printk("%s: Promiscuous mode enabled.\n", dev->name);
 578                cep->sccp->scc_pmsr |= SCC_PMSR_PRO;
 579        } else {
 580
 581                cep->sccp->scc_pmsr &= ~SCC_PMSR_PRO;
 582
 583                if (dev->flags & IFF_ALLMULTI) {
 584                        /* Catch all multicast addresses, so set the
 585                         * filter to all 1's.
 586                         */
 587                        ep->sen_gaddr1 = 0xffff;
 588                        ep->sen_gaddr2 = 0xffff;
 589                        ep->sen_gaddr3 = 0xffff;
 590                        ep->sen_gaddr4 = 0xffff;
 591                }
 592                else {
 593                        /* Clear filter and add the addresses in the list.
 594                        */
 595                        ep->sen_gaddr1 = 0;
 596                        ep->sen_gaddr2 = 0;
 597                        ep->sen_gaddr3 = 0;
 598                        ep->sen_gaddr4 = 0;
 599
 600                        dmi = dev->mc_list;
 601
 602                        for (i=0; i<dev->mc_count; i++, dmi = dmi->next) {
 603
 604                                /* Only support group multicast for now.
 605                                */
 606                                if (!(dmi->dmi_addr[0] & 1))
 607                                        continue;
 608
 609                                /* The address in dmi_addr is LSB first,
 610                                 * and taddr is MSB first.  We have to
 611                                 * copy bytes MSB first from dmi_addr.
 612                                 */
 613                                mcptr = (u_char *)dmi->dmi_addr + 5;
 614                                tdptr = (u_char *)&ep->sen_taddrh;
 615                                for (j=0; j<6; j++)
 616                                        *tdptr++ = *mcptr--;
 617
 618                                /* Ask CPM to run CRC and set bit in
 619                                 * filter mask.
 620                                 */
 621                                cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_SET_GADDR) | CPM_CR_FLG;
 622                                /* this delay is necessary here -- Cort */
 623                                udelay(10);
 624                                while (cpmp->cp_cpcr & CPM_CR_FLG);
 625                        }
 626                }
 627        }
 628}
 629
 630/* Initialize the CPM Ethernet on SCC.  If EPPC-Bug loaded us, or performed
 631 * some other network I/O, a whole bunch of this has already been set up.
 632 * It is no big deal if we do it again, we just have to disable the
 633 * transmit and receive to make sure we don't catch the CPM with some
 634 * inconsistent control information.
 635 */
 636int __init scc_enet_init(void)
 637{
 638        struct net_device *dev;
 639        struct scc_enet_private *cep;
 640        int i, j, k;
 641        unsigned char   *eap, *ba;
 642        dma_addr_t      mem_addr;
 643        bd_t            *bd;
 644        volatile        cbd_t           *bdp;
 645        volatile        cpm8xx_t        *cp;
 646        volatile        scc_t           *sccp;
 647        volatile        scc_enet_t      *ep;
 648        volatile        immap_t         *immap;
 649
 650        cp = cpmp;      /* Get pointer to Communication Processor */
 651
 652        immap = (immap_t *)(mfspr(IMMR) & 0xFFFF0000);  /* and to internal registers */
 653
 654        bd = (bd_t *)__res;
 655
 656        /* Allocate some private information.
 657        */
 658        cep = (struct scc_enet_private *)kmalloc(sizeof(*cep), GFP_KERNEL);
 659        if (cep == NULL)
 660                return -ENOMEM;
 661
 662        __clear_user(cep,sizeof(*cep));
 663        spin_lock_init(&cep->lock);
 664
 665        /* Create an Ethernet device instance.
 666        */
 667        dev = init_etherdev(0, 0);
 668
 669        /* Get pointer to SCC area in parameter RAM.
 670        */
 671        ep = (scc_enet_t *)(&cp->cp_dparam[PROFF_ENET]);
 672
 673        /* And another to the SCC register area.
 674        */
 675        sccp = (volatile scc_t *)(&cp->cp_scc[SCC_ENET]);
 676        cep->sccp = (scc_t *)sccp;              /* Keep the pointer handy */
 677
 678        /* Disable receive and transmit in case EPPC-Bug started it.
 679        */
 680        sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
 681
 682        /* Cookbook style from the MPC860 manual.....
 683         * Not all of this is necessary if EPPC-Bug has initialized
 684         * the network.
 685         * So far we are lucky, all board configurations use the same
 686         * pins, or at least the same I/O Port for these functions.....
 687         * It can't last though......
 688         */
 689
 690#if (defined(PA_ENET_RXD) && defined(PA_ENET_TXD))
 691        /* Configure port A pins for Txd and Rxd.
 692        */
 693        immap->im_ioport.iop_papar |=  (PA_ENET_RXD | PA_ENET_TXD);
 694        immap->im_ioport.iop_padir &= ~(PA_ENET_RXD | PA_ENET_TXD);
 695        immap->im_ioport.iop_paodr &=                ~PA_ENET_TXD;
 696#elif (defined(PB_ENET_RXD) && defined(PB_ENET_TXD))
 697        /* Configure port B pins for Txd and Rxd.
 698        */
 699        immap->im_cpm.cp_pbpar |=  (PB_ENET_RXD | PB_ENET_TXD);
 700        immap->im_cpm.cp_pbdir &= ~(PB_ENET_RXD | PB_ENET_TXD);
 701        immap->im_cpm.cp_pbodr &=                ~PB_ENET_TXD;
 702#else
 703#error Exactly ONE pair of PA_ENET_[RT]XD, PB_ENET_[RT]XD must be defined
 704#endif
 705
 706#if defined(PC_ENET_LBK)
 707        /* Configure port C pins to disable External Loopback
 708         */
 709        immap->im_ioport.iop_pcpar &= ~PC_ENET_LBK;
 710        immap->im_ioport.iop_pcdir |=  PC_ENET_LBK;
 711        immap->im_ioport.iop_pcso  &= ~PC_ENET_LBK;
 712        immap->im_ioport.iop_pcdat &= ~PC_ENET_LBK;     /* Disable Loopback */
 713#endif  /* PC_ENET_LBK */
 714
 715        /* Configure port C pins to enable CLSN and RENA.
 716        */
 717        immap->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
 718        immap->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
 719        immap->im_ioport.iop_pcso  |=  (PC_ENET_CLSN | PC_ENET_RENA);
 720
 721        /* Configure port A for TCLK and RCLK.
 722        */
 723        immap->im_ioport.iop_papar |=  (PA_ENET_TCLK | PA_ENET_RCLK);
 724        immap->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
 725
 726        /* Configure Serial Interface clock routing.
 727         * First, clear all SCC bits to zero, then set the ones we want.
 728         */
 729        cp->cp_sicr &= ~SICR_ENET_MASK;
 730        cp->cp_sicr |=  SICR_ENET_CLKRT;
 731
 732        /* Manual says set SDDR, but I can't find anything with that
 733         * name.  I think it is a misprint, and should be SDCR.  This
 734         * has already been set by the communication processor initialization.
 735         */
 736
 737        /* Allocate space for the buffer descriptors in the DP ram.
 738         * These are relative offsets in the DP ram address space.
 739         * Initialize base addresses for the buffer descriptors.
 740         */
 741        i = m8xx_cpm_dpalloc(sizeof(cbd_t) * RX_RING_SIZE);
 742        ep->sen_genscc.scc_rbase = i;
 743        cep->rx_bd_base = (cbd_t *)&cp->cp_dpmem[i];
 744
 745        i = m8xx_cpm_dpalloc(sizeof(cbd_t) * TX_RING_SIZE);
 746        ep->sen_genscc.scc_tbase = i;
 747        cep->tx_bd_base = (cbd_t *)&cp->cp_dpmem[i];
 748
 749        cep->dirty_tx = cep->cur_tx = cep->tx_bd_base;
 750        cep->tx_free = TX_RING_SIZE;
 751        cep->cur_rx = cep->rx_bd_base;
 752
 753        /* Issue init Rx BD command for SCC.
 754         * Manual says to perform an Init Rx parameters here.  We have
 755         * to perform both Rx and Tx because the SCC may have been
 756         * already running.
 757         * In addition, we have to do it later because we don't yet have
 758         * all of the BD control/status set properly.
 759        cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_RX) | CPM_CR_FLG;
 760        while (cp->cp_cpcr & CPM_CR_FLG);
 761         */
 762
 763        /* Initialize function code registers for big-endian.
 764        */
 765        ep->sen_genscc.scc_rfcr = SCC_EB;
 766        ep->sen_genscc.scc_tfcr = SCC_EB;
 767
 768        /* Set maximum bytes per receive buffer.
 769         * This appears to be an Ethernet frame size, not the buffer
 770         * fragment size.  It must be a multiple of four.
 771         */
 772        ep->sen_genscc.scc_mrblr = PKT_MAXBLR_SIZE;
 773
 774        /* Set CRC preset and mask.
 775        */
 776        ep->sen_cpres = 0xffffffff;
 777        ep->sen_cmask = 0xdebb20e3;
 778
 779        ep->sen_crcec = 0;      /* CRC Error counter */
 780        ep->sen_alec = 0;       /* alignment error counter */
 781        ep->sen_disfc = 0;      /* discard frame counter */
 782
 783        ep->sen_pads = 0x8888;  /* Tx short frame pad character */
 784        ep->sen_retlim = 15;    /* Retry limit threshold */
 785
 786        ep->sen_maxflr = PKT_MAXBUF_SIZE;   /* maximum frame length register */
 787        ep->sen_minflr = PKT_MINBUF_SIZE;  /* minimum frame length register */
 788
 789        ep->sen_maxd1 = PKT_MAXBLR_SIZE;        /* maximum DMA1 length */
 790        ep->sen_maxd2 = PKT_MAXBLR_SIZE;        /* maximum DMA2 length */
 791
 792        /* Clear hash tables.
 793        */
 794        ep->sen_gaddr1 = 0;
 795        ep->sen_gaddr2 = 0;
 796        ep->sen_gaddr3 = 0;
 797        ep->sen_gaddr4 = 0;
 798        ep->sen_iaddr1 = 0;
 799        ep->sen_iaddr2 = 0;
 800        ep->sen_iaddr3 = 0;
 801        ep->sen_iaddr4 = 0;
 802
 803        /* Set Ethernet station address.
 804         */
 805        eap = (unsigned char *)&(ep->sen_paddrh);
 806        for (i=5; i>=0; i--)
 807                *eap++ = dev->dev_addr[i] = bd->bi_enetaddr[i];
 808
 809        ep->sen_pper = 0;       /* 'cause the book says so */
 810        ep->sen_taddrl = 0;     /* temp address (LSB) */
 811        ep->sen_taddrm = 0;
 812        ep->sen_taddrh = 0;     /* temp address (MSB) */
 813
 814        /* Now allocate the host memory pages and initialize the
 815         * buffer descriptors.
 816         */
 817        bdp = cep->tx_bd_base;
 818        for (i=0; i<TX_RING_SIZE; i++) {
 819
 820                /* Initialize the BD for every fragment in the page.
 821                */
 822                bdp->cbd_sc = 0;
 823                bdp->cbd_bufaddr = 0;
 824                bdp++;
 825        }
 826
 827        /* Set the last buffer to wrap.
 828        */
 829        bdp--;
 830        bdp->cbd_sc |= BD_SC_WRAP;
 831
 832        bdp = cep->rx_bd_base;
 833        k = 0;
 834        for (i=0; i<CPM_ENET_RX_PAGES; i++) {
 835
 836                /* Allocate a page.
 837                */
 838                ba = (unsigned char *)consistent_alloc(GFP_KERNEL, PAGE_SIZE, &mem_addr);
 839
 840                /* Initialize the BD for every fragment in the page.
 841                */
 842                for (j=0; j<CPM_ENET_RX_FRPPG; j++) {
 843                        bdp->cbd_sc = BD_ENET_RX_EMPTY | BD_ENET_RX_INTR;
 844                        bdp->cbd_bufaddr = mem_addr;
 845                        cep->rx_vaddr[k++] = ba;
 846                        mem_addr += CPM_ENET_RX_FRSIZE;
 847                        ba += CPM_ENET_RX_FRSIZE;
 848                        bdp++;
 849                }
 850        }
 851
 852        /* Set the last buffer to wrap.
 853        */
 854        bdp--;
 855        bdp->cbd_sc |= BD_SC_WRAP;
 856
 857        /* Let's re-initialize the channel now.  We have to do it later
 858         * than the manual describes because we have just now finished
 859         * the BD initialization.
 860         */
 861        cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET, CPM_CR_INIT_TRX) | CPM_CR_FLG;
 862        while (cp->cp_cpcr & CPM_CR_FLG);
 863
 864        cep->skb_cur = cep->skb_dirty = 0;
 865
 866        sccp->scc_scce = 0xffff;        /* Clear any pending events */
 867
 868        /* Enable interrupts for transmit error, complete frame
 869         * received, and any transmit buffer we have also set the
 870         * interrupt flag.
 871         */
 872        sccp->scc_sccm = (SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
 873
 874        /* Install our interrupt handler.
 875        */
 876        if ((request_irq(CPM_IRQ_OFFSET + CPMVEC_ENET, scc_enet_interrupt,
 877                        0, cpm_int_name[CPMVEC_ENET], dev)) != 0)
 878                panic("Could not allocate SCC ethernet IRQ!");
 879
 880        /* Set GSMR_H to enable all normal operating modes.
 881         * Set GSMR_L to enable Ethernet to MC68160.
 882         */
 883        sccp->scc_gsmrh = 0;
 884        sccp->scc_gsmrl = (SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 | SCC_GSMRL_MODE_ENET);
 885
 886        /* Set sync/delimiters.
 887        */
 888        sccp->scc_dsr = 0xd555;
 889
 890        /* Set processing mode.  Use Ethernet CRC, catch broadcast, and
 891         * start frame search 22 bit times after RENA.
 892         */
 893        sccp->scc_pmsr = (SCC_PMSR_ENCRC | SCC_PMSR_NIB22);
 894
 895        /* It is now OK to enable the Ethernet transmitter.
 896         * Unfortunately, there are board implementation differences here.
 897         */
 898#if   (!defined (PB_ENET_TENA) &&  defined (PC_ENET_TENA))
 899        immap->im_ioport.iop_pcpar |=  PC_ENET_TENA;
 900        immap->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
 901#elif ( defined (PB_ENET_TENA) && !defined (PC_ENET_TENA))
 902        cp->cp_pbpar |= PB_ENET_TENA;
 903        cp->cp_pbdir |= PB_ENET_TENA;
 904#else
 905#error Configuration Error: define exactly ONE of PB_ENET_TENA, PC_ENET_TENA
 906#endif
 907
 908#if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
 909        /* And while we are here, set the configuration to enable ethernet.
 910        */
 911        *((volatile uint *)RPX_CSR_ADDR) &= ~BCSR0_ETHLPBK;
 912        *((volatile uint *)RPX_CSR_ADDR) |=
 913                        (BCSR0_ETHEN | BCSR0_COLTESTDIS | BCSR0_FULLDPLXDIS);
 914#endif
 915
 916#ifdef CONFIG_BSEIP
 917        /* BSE uses port B and C for PHY control.
 918        */
 919        cp->cp_pbpar &= ~(PB_BSE_POWERUP | PB_BSE_FDXDIS);
 920        cp->cp_pbdir |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
 921        cp->cp_pbdat |= (PB_BSE_POWERUP | PB_BSE_FDXDIS);
 922
 923        immap->im_ioport.iop_pcpar &= ~PC_BSE_LOOPBACK;
 924        immap->im_ioport.iop_pcdir |= PC_BSE_LOOPBACK;
 925        immap->im_ioport.iop_pcso &= ~PC_BSE_LOOPBACK;
 926        immap->im_ioport.iop_pcdat &= ~PC_BSE_LOOPBACK;
 927#endif
 928
 929#ifdef CONFIG_FADS
 930        cp->cp_pbpar |= PB_ENET_TENA;
 931        cp->cp_pbdir |= PB_ENET_TENA;
 932
 933        /* Enable the EEST PHY.
 934        */
 935        *((volatile uint *)BCSR1) &= ~BCSR1_ETHEN;
 936#endif
 937
 938        dev->base_addr = (unsigned long)ep;
 939        dev->priv = cep;
 940#if 0
 941        dev->name = "CPM_ENET";
 942#endif
 943
 944        /* The CPM Ethernet specific entries in the device structure. */
 945        dev->open = scc_enet_open;
 946        dev->hard_start_xmit = scc_enet_start_xmit;
 947        dev->tx_timeout = scc_enet_timeout;
 948        dev->watchdog_timeo = TX_TIMEOUT;
 949        dev->stop = scc_enet_close;
 950        dev->get_stats = scc_enet_get_stats;
 951        dev->set_multicast_list = set_multicast_list;
 952
 953        /* And last, enable the transmit and receive processing.
 954        */
 955        sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
 956
 957        printk("%s: CPM ENET Version 0.2 on SCC%d, ", dev->name, SCC_ENET+1);
 958        for (i=0; i<5; i++)
 959                printk("%02x:", dev->dev_addr[i]);
 960        printk("%02x\n", dev->dev_addr[5]);
 961
 962        return 0;
 963}
 964
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.