uboot/arch/powerpc/cpu/mpc8260/ether_fcc.c
<<
>>
Prefs
   1/*
   2 * MPC8260 FCC Fast Ethernet
   3 *
   4 * Copyright (c) 2000 MontaVista Software, Inc.   Dan Malek (dmalek@jlc.net)
   5 *
   6 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   7 * Marius Groeger <mgroeger@sysgo.de>
   8 *
   9 * SPDX-License-Identifier:     GPL-2.0+
  10 */
  11
  12/*
  13 * MPC8260 FCC Fast Ethernet
  14 * Basic ET HW initialization and packet RX/TX routines
  15 *
  16 * This code will not perform the IO port configuration. This should be
  17 * done in the iop_conf_t structure specific for the board.
  18 *
  19 * TODO:
  20 * add a PHY driver to do the negotiation
  21 * reflect negotiation results in FPSMR
  22 * look for ways to configure the board specific stuff elsewhere, eg.
  23 *    config_xxx.h or the board directory
  24 */
  25
  26#include <common.h>
  27#include <console.h>
  28#include <malloc.h>
  29#include <asm/cpm_8260.h>
  30#include <mpc8260.h>
  31#include <command.h>
  32#include <config.h>
  33#include <net.h>
  34
  35#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  36#include <miiphy.h>
  37#endif
  38
  39DECLARE_GLOBAL_DATA_PTR;
  40
  41#if defined(CONFIG_ETHER_ON_FCC) && defined(CONFIG_CMD_NET)
  42
  43static struct ether_fcc_info_s
  44{
  45        int ether_index;
  46        int proff_enet;
  47        ulong cpm_cr_enet_sblock;
  48        ulong cpm_cr_enet_page;
  49        ulong cmxfcr_mask;
  50        ulong cmxfcr_value;
  51}
  52        ether_fcc_info[] =
  53{
  54#ifdef CONFIG_ETHER_ON_FCC1
  55{
  56        0,
  57        PROFF_FCC1,
  58        CPM_CR_FCC1_SBLOCK,
  59        CPM_CR_FCC1_PAGE,
  60        CONFIG_SYS_CMXFCR_MASK1,
  61        CONFIG_SYS_CMXFCR_VALUE1
  62},
  63#endif
  64
  65#ifdef CONFIG_ETHER_ON_FCC2
  66{
  67        1,
  68        PROFF_FCC2,
  69        CPM_CR_FCC2_SBLOCK,
  70        CPM_CR_FCC2_PAGE,
  71        CONFIG_SYS_CMXFCR_MASK2,
  72        CONFIG_SYS_CMXFCR_VALUE2
  73},
  74#endif
  75
  76#ifdef CONFIG_ETHER_ON_FCC3
  77{
  78        2,
  79        PROFF_FCC3,
  80        CPM_CR_FCC3_SBLOCK,
  81        CPM_CR_FCC3_PAGE,
  82        CONFIG_SYS_CMXFCR_MASK3,
  83        CONFIG_SYS_CMXFCR_VALUE3
  84},
  85#endif
  86};
  87
  88/*---------------------------------------------------------------------*/
  89
  90/* Maximum input DMA size.  Must be a should(?) be a multiple of 4. */
  91#define PKT_MAXDMA_SIZE         1520
  92
  93/* The FCC stores dest/src/type, data, and checksum for receive packets. */
  94#define PKT_MAXBUF_SIZE         1518
  95#define PKT_MINBUF_SIZE         64
  96
  97/* Maximum input buffer size.  Must be a multiple of 32. */
  98#define PKT_MAXBLR_SIZE         1536
  99
 100#define TOUT_LOOP 1000000
 101
 102#define TX_BUF_CNT 2
 103#ifdef __GNUC__
 104static char txbuf[TX_BUF_CNT][PKT_MAXBLR_SIZE] __attribute__ ((aligned(8)));
 105#else
 106#error "txbuf must be 64-bit aligned"
 107#endif
 108
 109static uint rxIdx;      /* index of the current RX buffer */
 110static uint txIdx;      /* index of the current TX buffer */
 111
 112/*
 113 * FCC Ethernet Tx and Rx buffer descriptors.
 114 * Provide for Double Buffering
 115 * Note: PKTBUFSRX is defined in net.h
 116 */
 117
 118typedef volatile struct rtxbd {
 119    cbd_t rxbd[PKTBUFSRX];
 120    cbd_t txbd[TX_BUF_CNT];
 121} RTXBD;
 122
 123/*  Good news: the FCC supports external BDs! */
 124#ifdef __GNUC__
 125static RTXBD rtx __attribute__ ((aligned(8)));
 126#else
 127#error "rtx must be 64-bit aligned"
 128#endif
 129
 130static int fec_send(struct eth_device *dev, void *packet, int length)
 131{
 132    int i;
 133    int result = 0;
 134
 135    if (length <= 0) {
 136        printf("fec: bad packet size: %d\n", length);
 137        goto out;
 138    }
 139
 140    for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
 141        if (i >= TOUT_LOOP) {
 142            puts ("fec: tx buffer not ready\n");
 143            goto out;
 144        }
 145    }
 146
 147    rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
 148    rtx.txbd[txIdx].cbd_datlen = length;
 149    rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |
 150                               BD_ENET_TX_WRAP);
 151
 152    for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
 153        if (i >= TOUT_LOOP) {
 154            puts ("fec: tx error\n");
 155            goto out;
 156        }
 157    }
 158
 159#ifdef ET_DEBUG
 160    printf("cycles: %d status: %04x\n", i, rtx.txbd[txIdx].cbd_sc);
 161#endif
 162
 163    /* return only status bits */
 164    result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
 165
 166out:
 167    return result;
 168}
 169
 170static int fec_recv(struct eth_device* dev)
 171{
 172    int length;
 173
 174    for (;;)
 175    {
 176        if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
 177            length = -1;
 178            break;     /* nothing received - leave for() loop */
 179        }
 180        length = rtx.rxbd[rxIdx].cbd_datlen;
 181
 182        if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
 183            printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
 184        }
 185        else {
 186            /* Pass the packet up to the protocol layers. */
 187            net_process_received_packet(net_rx_packets[rxIdx], length - 4);
 188        }
 189
 190
 191        /* Give the buffer back to the FCC. */
 192        rtx.rxbd[rxIdx].cbd_datlen = 0;
 193
 194        /* wrap around buffer index when necessary */
 195        if ((rxIdx + 1) >= PKTBUFSRX) {
 196            rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
 197            rxIdx = 0;
 198        }
 199        else {
 200            rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
 201            rxIdx++;
 202        }
 203    }
 204    return length;
 205}
 206
 207
 208static int fec_init(struct eth_device* dev, bd_t *bis)
 209{
 210    struct ether_fcc_info_s * info = dev->priv;
 211    int i;
 212    volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
 213    volatile cpm8260_t *cp = &(immr->im_cpm);
 214    fcc_enet_t *pram_ptr;
 215    unsigned long mem_addr;
 216
 217#if 0
 218    mii_discover_phy();
 219#endif
 220
 221    /* 28.9 - (1-2): ioports have been set up already */
 222
 223    /* 28.9 - (3): connect FCC's tx and rx clocks */
 224    immr->im_cpmux.cmx_uar = 0;
 225    immr->im_cpmux.cmx_fcr = (immr->im_cpmux.cmx_fcr & ~info->cmxfcr_mask) |
 226                                                        info->cmxfcr_value;
 227
 228    /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
 229    immr->im_fcc[info->ether_index].fcc_gfmr =
 230      FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
 231
 232    /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet */
 233    immr->im_fcc[info->ether_index].fcc_fpsmr = CONFIG_SYS_FCC_PSMR | FCC_PSMR_ENCRC;
 234
 235    /* 28.9 - (6): FDSR: Ethernet Syn */
 236    immr->im_fcc[info->ether_index].fcc_fdsr = 0xD555;
 237
 238    /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
 239    rxIdx = 0;
 240    txIdx = 0;
 241
 242    /* Setup Receiver Buffer Descriptors */
 243    for (i = 0; i < PKTBUFSRX; i++)
 244    {
 245      rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
 246      rtx.rxbd[i].cbd_datlen = 0;
 247      rtx.rxbd[i].cbd_bufaddr = (uint)net_rx_packets[i];
 248    }
 249    rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
 250
 251    /* Setup Ethernet Transmitter Buffer Descriptors */
 252    for (i = 0; i < TX_BUF_CNT; i++)
 253    {
 254      rtx.txbd[i].cbd_sc = (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
 255      rtx.txbd[i].cbd_datlen = 0;
 256      rtx.txbd[i].cbd_bufaddr = (uint)&txbuf[i][0];
 257    }
 258    rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
 259
 260    /* 28.9 - (7): initialise parameter ram */
 261    pram_ptr = (fcc_enet_t *)&(immr->im_dprambase[info->proff_enet]);
 262
 263    /* clear whole structure to make sure all reserved fields are zero */
 264    memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
 265
 266    /*
 267     * common Parameter RAM area
 268     *
 269     * Allocate space in the reserved FCC area of DPRAM for the
 270     * internal buffers.  No one uses this space (yet), so we
 271     * can do this.  Later, we will add resource management for
 272     * this area.
 273     */
 274    mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
 275    pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
 276    pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
 277    /*
 278     * Set maximum bytes per receive buffer.
 279     * It must be a multiple of 32.
 280     */
 281    pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
 282    pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
 283                                       CONFIG_SYS_CPMFCR_RAMTYPE) << 24;
 284    pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
 285    pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
 286                                       CONFIG_SYS_CPMFCR_RAMTYPE) << 24;
 287    pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
 288
 289    /* protocol-specific area */
 290    pram_ptr->fen_cmask = 0xdebb20e3;   /* CRC mask */
 291    pram_ptr->fen_cpres = 0xffffffff;   /* CRC preset */
 292    pram_ptr->fen_retlim = 15;          /* Retry limit threshold */
 293    pram_ptr->fen_mflr = PKT_MAXBUF_SIZE;   /* maximum frame length register */
 294    /*
 295     * Set Ethernet station address.
 296     *
 297     * This is supplied in the board information structure, so we
 298     * copy that into the controller.
 299     * So, far we have only been given one Ethernet address. We make
 300     * it unique by setting a few bits in the upper byte of the
 301     * non-static part of the address.
 302     */
 303#define ea eth_get_ethaddr()
 304    pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
 305    pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
 306    pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
 307#undef ea
 308    pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
 309    /* pad pointer. use tiptr since we don't need a specific padding char */
 310    pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
 311    pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE;      /* maximum DMA1 length */
 312    pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE;      /* maximum DMA2 length */
 313    pram_ptr->fen_rfthr = 1;
 314    pram_ptr->fen_rfcnt = 1;
 315#if 0
 316    printf("pram_ptr->fen_genfcc.fcc_rbase %08lx\n",
 317        pram_ptr->fen_genfcc.fcc_rbase);
 318    printf("pram_ptr->fen_genfcc.fcc_tbase %08lx\n",
 319        pram_ptr->fen_genfcc.fcc_tbase);
 320#endif
 321
 322    /* 28.9 - (8): clear out events in FCCE */
 323    immr->im_fcc[info->ether_index].fcc_fcce = ~0x0;
 324
 325    /* 28.9 - (9): FCCM: mask all events */
 326    immr->im_fcc[info->ether_index].fcc_fccm = 0;
 327
 328    /* 28.9 - (10-12): we don't use ethernet interrupts */
 329
 330    /* 28.9 - (13)
 331     *
 332     * Let's re-initialize the channel now.  We have to do it later
 333     * than the manual describes because we have just now finished
 334     * the BD initialization.
 335     */
 336    cp->cp_cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
 337                            info->cpm_cr_enet_sblock,
 338                            0x0c,
 339                            CPM_CR_INIT_TRX) | CPM_CR_FLG;
 340    do {
 341        __asm__ __volatile__ ("eieio");
 342    } while (cp->cp_cpcr & CPM_CR_FLG);
 343
 344    /* 28.9 - (14): enable tx/rx in gfmr */
 345    immr->im_fcc[info->ether_index].fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
 346
 347    return 1;
 348}
 349
 350static void fec_halt(struct eth_device* dev)
 351{
 352    struct ether_fcc_info_s * info = dev->priv;
 353    volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
 354
 355    /* write GFMR: disable tx/rx */
 356    immr->im_fcc[info->ether_index].fcc_gfmr &=
 357                                                ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
 358}
 359
 360int fec_initialize(bd_t *bis)
 361{
 362        struct eth_device* dev;
 363        int i;
 364
 365        for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
 366        {
 367                dev = (struct eth_device*) malloc(sizeof *dev);
 368                memset(dev, 0, sizeof *dev);
 369
 370                sprintf(dev->name, "FCC%d",
 371                        ether_fcc_info[i].ether_index + 1);
 372                dev->priv   = &ether_fcc_info[i];
 373                dev->init   = fec_init;
 374                dev->halt   = fec_halt;
 375                dev->send   = fec_send;
 376                dev->recv   = fec_recv;
 377
 378                eth_register(dev);
 379
 380#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) \
 381                && defined(CONFIG_BITBANGMII)
 382                miiphy_register(dev->name,
 383                                bb_miiphy_read, bb_miiphy_write);
 384#endif
 385        }
 386
 387        return 1;
 388}
 389
 390#ifdef CONFIG_ETHER_LOOPBACK_TEST
 391
 392#define ELBT_BUFSZ      1024    /* must be multiple of 32 */
 393
 394#define ELBT_CRCSZ      4
 395
 396#define ELBT_NRXBD      4       /* must be at least 2 */
 397#define ELBT_NTXBD      4
 398
 399#define ELBT_MAXRXERR   32
 400#define ELBT_MAXTXERR   32
 401
 402#define ELBT_CLSWAIT    1000    /* msec to wait for further input frames */
 403
 404typedef
 405        struct {
 406                uint off;
 407                char *lab;
 408        }
 409elbt_prdesc;
 410
 411typedef
 412        struct {
 413                uint _l, _f, m, bc, mc, lg, no, sh, cr, ov, cl;
 414                uint badsrc, badtyp, badlen, badbit;
 415        }
 416elbt_rxeacc;
 417
 418static elbt_prdesc rxeacc_descs[] = {
 419        { offsetof(elbt_rxeacc, _l),            "Not Last in Frame"     },
 420        { offsetof(elbt_rxeacc, _f),            "Not First in Frame"    },
 421        { offsetof(elbt_rxeacc, m),             "Address Miss"          },
 422        { offsetof(elbt_rxeacc, bc),            "Broadcast Address"     },
 423        { offsetof(elbt_rxeacc, mc),            "Multicast Address"     },
 424        { offsetof(elbt_rxeacc, lg),            "Frame Length Violation"},
 425        { offsetof(elbt_rxeacc, no),            "Non-Octet Alignment"   },
 426        { offsetof(elbt_rxeacc, sh),            "Short Frame"           },
 427        { offsetof(elbt_rxeacc, cr),            "CRC Error"             },
 428        { offsetof(elbt_rxeacc, ov),            "Overrun"               },
 429        { offsetof(elbt_rxeacc, cl),            "Collision"             },
 430        { offsetof(elbt_rxeacc, badsrc),        "Bad Src Address"       },
 431        { offsetof(elbt_rxeacc, badtyp),        "Bad Frame Type"        },
 432        { offsetof(elbt_rxeacc, badlen),        "Bad Frame Length"      },
 433        { offsetof(elbt_rxeacc, badbit),        "Data Compare Errors"   },
 434};
 435static int rxeacc_ndesc = sizeof (rxeacc_descs) / sizeof (rxeacc_descs[0]);
 436
 437typedef
 438        struct {
 439                uint def, hb, lc, rl, rc, un, csl;
 440        }
 441elbt_txeacc;
 442
 443static elbt_prdesc txeacc_descs[] = {
 444        { offsetof(elbt_txeacc, def),           "Defer Indication"      },
 445        { offsetof(elbt_txeacc, hb),            "Heartbeat"             },
 446        { offsetof(elbt_txeacc, lc),            "Late Collision"        },
 447        { offsetof(elbt_txeacc, rl),            "Retransmission Limit"  },
 448        { offsetof(elbt_txeacc, rc),            "Retry Count"           },
 449        { offsetof(elbt_txeacc, un),            "Underrun"              },
 450        { offsetof(elbt_txeacc, csl),           "Carrier Sense Lost"    },
 451};
 452static int txeacc_ndesc = sizeof (txeacc_descs) / sizeof (txeacc_descs[0]);
 453
 454typedef
 455        struct {
 456                uchar rxbufs[ELBT_NRXBD][ELBT_BUFSZ];
 457                uchar txbufs[ELBT_NTXBD][ELBT_BUFSZ];
 458                cbd_t rxbd[ELBT_NRXBD];
 459                cbd_t txbd[ELBT_NTXBD];
 460                enum { Idle, Running, Closing, Closed } state;
 461                int proff, page, sblock;
 462                uint clstime, nsent, ntxerr, nrcvd, nrxerr;
 463                ushort rxerrs[ELBT_MAXRXERR], txerrs[ELBT_MAXTXERR];
 464                elbt_rxeacc rxeacc;
 465                elbt_txeacc txeacc;
 466        } __attribute__ ((aligned(8)))
 467elbt_chan;
 468
 469static uchar patbytes[ELBT_NTXBD] = {
 470        0xff, 0xaa, 0x55, 0x00
 471};
 472static uint patwords[ELBT_NTXBD] = {
 473        0xffffffff, 0xaaaaaaaa, 0x55555555, 0x00000000
 474};
 475
 476#ifdef __GNUC__
 477static elbt_chan elbt_chans[3] __attribute__ ((aligned(8)));
 478#else
 479#error "elbt_chans must be 64-bit aligned"
 480#endif
 481
 482#define CPM_CR_GRACEFUL_STOP_TX ((ushort)0x0005)
 483
 484static elbt_prdesc epram_descs[] = {
 485        { offsetof(fcc_enet_t, fen_crcec),      "CRC Errors"            },
 486        { offsetof(fcc_enet_t, fen_alec),       "Alignment Errors"      },
 487        { offsetof(fcc_enet_t, fen_disfc),      "Discarded Frames"      },
 488        { offsetof(fcc_enet_t, fen_octc),       "Octets"                },
 489        { offsetof(fcc_enet_t, fen_colc),       "Collisions"            },
 490        { offsetof(fcc_enet_t, fen_broc),       "Broadcast Frames"      },
 491        { offsetof(fcc_enet_t, fen_mulc),       "Multicast Frames"      },
 492        { offsetof(fcc_enet_t, fen_uspc),       "Undersize Frames"      },
 493        { offsetof(fcc_enet_t, fen_frgc),       "Fragments"             },
 494        { offsetof(fcc_enet_t, fen_ospc),       "Oversize Frames"       },
 495        { offsetof(fcc_enet_t, fen_jbrc),       "Jabbers"               },
 496        { offsetof(fcc_enet_t, fen_p64c),       "64 Octet Frames"       },
 497        { offsetof(fcc_enet_t, fen_p65c),       "65-127 Octet Frames"   },
 498        { offsetof(fcc_enet_t, fen_p128c),      "128-255 Octet Frames"  },
 499        { offsetof(fcc_enet_t, fen_p256c),      "256-511 Octet Frames"  },
 500        { offsetof(fcc_enet_t, fen_p512c),      "512-1023 Octet Frames" },
 501        { offsetof(fcc_enet_t, fen_p1024c),     "1024-1518 Octet Frames"},
 502};
 503static int epram_ndesc = sizeof (epram_descs) / sizeof (epram_descs[0]);
 504
 505/*
 506 * given an elbt_prdesc array and an array of base addresses, print
 507 * each prdesc down the screen with the values fetched from each
 508 * base address across the screen
 509 */
 510static void
 511print_desc (elbt_prdesc descs[], int ndesc, uchar *bases[], int nbase)
 512{
 513        elbt_prdesc *dp = descs, *edp = dp + ndesc;
 514        int i;
 515
 516        printf ("%32s", "");
 517
 518        for (i = 0; i < nbase; i++)
 519                printf ("  Channel %d", i);
 520
 521        putc ('\n');
 522
 523        while (dp < edp) {
 524
 525                printf ("%-32s", dp->lab);
 526
 527                for (i = 0; i < nbase; i++) {
 528                        uint val = *(uint *)(bases[i] + dp->off);
 529
 530                        printf (" %10u", val);
 531                }
 532
 533                putc ('\n');
 534
 535                dp++;
 536        }
 537}
 538
 539/*
 540 * return number of bits that are set in a value; value contains
 541 * nbits (right-justified) bits.
 542 */
 543static uint __inline__
 544nbs (uint value, uint nbits)
 545{
 546        uint cnt = 0;
 547#if 1
 548        uint pos = sizeof (uint) * 8;
 549
 550        __asm__ __volatile__ ("\
 551        mtctr   %2\n\
 5521:      rlwnm.  %2,%1,%4,31,31\n\
 553        beq     2f\n\
 554        addi    %0,%0,1\n\
 5552:      subi    %4,%4,1\n\
 556        bdnz    1b"
 557        : "=r"(cnt)
 558        : "r"(value), "r"(nbits), "r"(cnt), "r"(pos)
 559        : "ctr", "cc" );
 560#else
 561        uint mask = 1;
 562
 563        do {
 564                if (value & mask)
 565                        cnt++;
 566                mask <<= 1;
 567        } while (--nbits);
 568#endif
 569
 570        return (cnt);
 571}
 572
 573static ulong
 574badbits (uchar *bp, int n, ulong pat)
 575{
 576        ulong *lp, cnt = 0;
 577        int nl;
 578
 579        while (n > 0 && ((ulong)bp & (sizeof (ulong) - 1)) != 0) {
 580                uchar diff;
 581
 582                diff = *bp++ ^ (uchar)pat;
 583
 584                if (diff)
 585                        cnt += nbs ((ulong)diff, 8);
 586
 587                n--;
 588        }
 589
 590        lp = (ulong *)bp;
 591        nl = n / sizeof (ulong);
 592        n -= nl * sizeof (ulong);
 593
 594        while (nl > 0) {
 595                ulong diff;
 596
 597                diff = *lp++ ^ pat;
 598
 599                if (diff)
 600                        cnt += nbs (diff, 32);
 601
 602                nl--;
 603        }
 604
 605        bp = (uchar *)lp;
 606
 607        while (n > 0) {
 608                uchar diff;
 609
 610                diff = *bp++ ^ (uchar)pat;
 611
 612                if (diff)
 613                        cnt += nbs ((ulong)diff, 8);
 614
 615                n--;
 616        }
 617
 618        return (cnt);
 619}
 620
 621static inline unsigned short
 622swap16 (unsigned short x)
 623{
 624        return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
 625}
 626
 627/* broadcast is not an error - we send them like that */
 628#define BD_ENET_RX_ERRS (BD_ENET_RX_STATS & ~BD_ENET_RX_BC)
 629
 630void
 631eth_loopback_test (void)
 632{
 633        volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
 634        volatile cpm8260_t *cp = &(immr->im_cpm);
 635        int c, nclosed;
 636        ulong runtime, nmsec;
 637        uchar *bases[3];
 638
 639        puts ("FCC Ethernet External loopback test\n");
 640
 641        eth_getenv_enetaddr("ethaddr", net_ethaddr);
 642
 643        /*
 644         * global initialisations for all FCC channels
 645         */
 646
 647        /* 28.9 - (1-2): ioports have been set up already */
 648
 649#if defined(CONFIG_SACSng)
 650        /*
 651         * Attention: this is board-specific
 652         * 1, FCC2
 653         */
 654#       define FCC_START_LOOP 1
 655#       define FCC_END_LOOP   1
 656
 657        /*
 658         * Attention: this is board-specific
 659         * - FCC2 Rx-CLK is CLK13
 660         * - FCC2 Tx-CLK is CLK14
 661         */
 662
 663        /* 28.9 - (3): connect FCC's tx and rx clocks */
 664        immr->im_cpmux.cmx_uar = 0;
 665        immr->im_cpmux.cmx_fcr = CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14;
 666#else
 667#error "eth_loopback_test not supported on your board"
 668#endif
 669
 670        puts ("Initialise FCC channels:");
 671
 672        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
 673                elbt_chan *ecp = &elbt_chans[c];
 674                volatile fcc_t *fcp = &immr->im_fcc[c];
 675                volatile fcc_enet_t *fpp;
 676                int i;
 677                ulong addr;
 678
 679                /*
 680                 * initialise channel data
 681                 */
 682
 683                printf (" %d", c);
 684
 685                memset ((void *)ecp, 0, sizeof (*ecp));
 686
 687                ecp->state = Idle;
 688
 689                switch (c) {
 690
 691                case 0: /* FCC1 */
 692                        ecp->proff = PROFF_FCC1;
 693                        ecp->page = CPM_CR_FCC1_PAGE;
 694                        ecp->sblock = CPM_CR_FCC1_SBLOCK;
 695                        break;
 696
 697                case 1: /* FCC2 */
 698                        ecp->proff = PROFF_FCC2;
 699                        ecp->page = CPM_CR_FCC2_PAGE;
 700                        ecp->sblock = CPM_CR_FCC2_SBLOCK;
 701                        break;
 702
 703                case 2: /* FCC3 */
 704                        ecp->proff = PROFF_FCC3;
 705                        ecp->page = CPM_CR_FCC3_PAGE;
 706                        ecp->sblock = CPM_CR_FCC3_SBLOCK;
 707                        break;
 708                }
 709
 710                /*
 711                 * set up tx buffers and bds
 712                 */
 713
 714                for (i = 0; i < ELBT_NTXBD; i++) {
 715                        cbd_t *bdp = &ecp->txbd[i];
 716                        uchar *bp = &ecp->txbufs[i][0];
 717
 718                        bdp->cbd_bufaddr = (uint)bp;
 719                        /* room for crc */
 720                        bdp->cbd_datlen = ELBT_BUFSZ - ELBT_CRCSZ;
 721                        bdp->cbd_sc = BD_ENET_TX_READY | BD_ENET_TX_PAD | \
 722                                BD_ENET_TX_LAST | BD_ENET_TX_TC;
 723
 724                        memset((void *)bp, patbytes[i], ELBT_BUFSZ);
 725                        net_set_ether(bp, net_bcast_ethaddr, 0x8000);
 726                }
 727                ecp->txbd[ELBT_NTXBD - 1].cbd_sc |= BD_ENET_TX_WRAP;
 728
 729                /*
 730                 * set up rx buffers and bds
 731                 */
 732
 733                for (i = 0; i < ELBT_NRXBD; i++) {
 734                    cbd_t *bdp = &ecp->rxbd[i];
 735                    uchar *bp = &ecp->rxbufs[i][0];
 736
 737                    bdp->cbd_bufaddr = (uint)bp;
 738                    bdp->cbd_datlen = 0;
 739                    bdp->cbd_sc = BD_ENET_RX_EMPTY;
 740
 741                    memset ((void *)bp, 0, ELBT_BUFSZ);
 742                }
 743                ecp->rxbd[ELBT_NRXBD - 1].cbd_sc |= BD_ENET_RX_WRAP;
 744
 745                /*
 746                 * set up the FCC channel hardware
 747                 */
 748
 749                /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
 750                fcp->fcc_gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
 751
 752                /* 28.9 - (5): FPSMR: fd, enet CRC, Promis, RMON, Rx SHort */
 753                fcp->fcc_fpsmr = FCC_PSMR_FDE | FCC_PSMR_LPB | \
 754                        FCC_PSMR_ENCRC | FCC_PSMR_PRO | \
 755                        FCC_PSMR_MON | FCC_PSMR_RSH;
 756
 757                /* 28.9 - (6): FDSR: Ethernet Syn */
 758                fcp->fcc_fdsr = 0xD555;
 759
 760                /* 29.9 - (7): initialise parameter ram */
 761                fpp = (fcc_enet_t *)&(immr->im_dprambase[ecp->proff]);
 762
 763                /* clear whole struct to make sure all resv fields are zero */
 764                memset ((void *)fpp, 0, sizeof (fcc_enet_t));
 765
 766                /*
 767                 * common Parameter RAM area
 768                 *
 769                 * Allocate space in the reserved FCC area of DPRAM for the
 770                 * internal buffers.  No one uses this space (yet), so we
 771                 * can do this.  Later, we will add resource management for
 772                 * this area.
 773                 */
 774                addr = CPM_FCC_SPECIAL_BASE + (c * 64);
 775                fpp->fen_genfcc.fcc_riptr = addr;
 776                fpp->fen_genfcc.fcc_tiptr = addr + 32;
 777
 778                /*
 779                 * Set maximum bytes per receive buffer.
 780                 * It must be a multiple of 32.
 781                 * buffers are in 60x bus memory.
 782                 */
 783                fpp->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
 784                fpp->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
 785                fpp->fen_genfcc.fcc_rbase = (unsigned int)(&ecp->rxbd[0]);
 786                fpp->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
 787                fpp->fen_genfcc.fcc_tbase = (unsigned int)(&ecp->txbd[0]);
 788
 789                /* protocol-specific area */
 790                fpp->fen_cmask = 0xdebb20e3;    /* CRC mask */
 791                fpp->fen_cpres = 0xffffffff;    /* CRC preset */
 792                fpp->fen_retlim = 15;           /* Retry limit threshold */
 793                fpp->fen_mflr = PKT_MAXBUF_SIZE;/* max frame length register */
 794
 795                /*
 796                 * Set Ethernet station address.
 797                 *
 798                 * This is supplied in the board information structure, so we
 799                 * copy that into the controller.
 800                 * So, far we have only been given one Ethernet address. We use
 801                 * the same address for all channels
 802                 */
 803                fpp->fen_paddrh = (net_ethaddr[5] << 8) + net_ethaddr[4];
 804                fpp->fen_paddrm = (net_ethaddr[3] << 8) + net_ethaddr[2];
 805                fpp->fen_paddrl = (net_ethaddr[1] << 8) + net_ethaddr[0];
 806
 807                fpp->fen_minflr = PKT_MINBUF_SIZE; /* min frame len register */
 808                /*
 809                 * pad pointer. use tiptr since we don't need
 810                 * a specific padding char
 811                 */
 812                fpp->fen_padptr = fpp->fen_genfcc.fcc_tiptr;
 813                fpp->fen_maxd1 = PKT_MAXDMA_SIZE;       /* max DMA1 length */
 814                fpp->fen_maxd2 = PKT_MAXDMA_SIZE;       /* max DMA2 length */
 815                fpp->fen_rfthr = 1;
 816                fpp->fen_rfcnt = 1;
 817
 818                /* 28.9 - (8): clear out events in FCCE */
 819                fcp->fcc_fcce = ~0x0;
 820
 821                /* 28.9 - (9): FCCM: mask all events */
 822                fcp->fcc_fccm = 0;
 823
 824                /* 28.9 - (10-12): we don't use ethernet interrupts */
 825
 826                /* 28.9 - (13)
 827                 *
 828                 * Let's re-initialize the channel now.  We have to do it later
 829                 * than the manual describes because we have just now finished
 830                 * the BD initialization.
 831                 */
 832                cp->cp_cpcr = mk_cr_cmd (ecp->page, ecp->sblock, \
 833                        0x0c, CPM_CR_INIT_TRX) | CPM_CR_FLG;
 834                do {
 835                        __asm__ __volatile__ ("eieio");
 836                } while (cp->cp_cpcr & CPM_CR_FLG);
 837        }
 838
 839        puts (" done\nStarting test... (Ctrl-C to Finish)\n");
 840
 841        /*
 842         * Note: don't want serial output from here until the end of the
 843         * test - the delays would probably stuff things up.
 844         */
 845
 846        clear_ctrlc ();
 847        runtime = get_timer (0);
 848
 849        do {
 850                nclosed = 0;
 851
 852                for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
 853                        volatile fcc_t *fcp = &immr->im_fcc[c];
 854                        elbt_chan *ecp = &elbt_chans[c];
 855                        int i;
 856
 857                        switch (ecp->state) {
 858
 859                        case Idle:
 860                                /*
 861                                 * set the channel Running ...
 862                                 */
 863
 864                                /* 28.9 - (14): enable tx/rx in gfmr */
 865                                fcp->fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
 866
 867                                ecp->state = Running;
 868                                break;
 869
 870                        case Running:
 871                                /*
 872                                 * (while Running only) check for
 873                                 * termination of the test
 874                                 */
 875
 876                                (void)ctrlc ();
 877
 878                                if (had_ctrlc ()) {
 879                                        /*
 880                                         * initiate a "graceful stop transmit"
 881                                         * on the channel
 882                                         */
 883                                        cp->cp_cpcr = mk_cr_cmd (ecp->page, \
 884                                                ecp->sblock, 0x0c, \
 885                                                CPM_CR_GRACEFUL_STOP_TX) | \
 886                                                CPM_CR_FLG;
 887                                        do {
 888                                                __asm__ __volatile__ ("eieio");
 889                                        } while (cp->cp_cpcr & CPM_CR_FLG);
 890
 891                                        ecp->clstime = get_timer (0);
 892                                        ecp->state = Closing;
 893                                }
 894                                /* fall through ... */
 895
 896                        case Closing:
 897                                /*
 898                                 * (while Running or Closing) poll the channel:
 899                                 * - check for any non-READY tx buffers and
 900                                 *   make them ready
 901                                 * - check for any non-EMPTY rx buffers and
 902                                 *   check that they were received correctly,
 903                                 *   adjust counters etc, then make empty
 904                                 */
 905
 906                                for (i = 0; i < ELBT_NTXBD; i++) {
 907                                        cbd_t *bdp = &ecp->txbd[i];
 908                                        ushort sc = bdp->cbd_sc;
 909
 910                                        if ((sc & BD_ENET_TX_READY) != 0)
 911                                                continue;
 912
 913                                        /*
 914                                         * this frame has finished
 915                                         * transmitting
 916                                         */
 917                                        ecp->nsent++;
 918
 919                                        if (sc & BD_ENET_TX_STATS) {
 920                                                ulong n;
 921
 922                                                /*
 923                                                 * we had an error on
 924                                                 * the transmission
 925                                                 */
 926                                                n = ecp->ntxerr++;
 927                                                if (n < ELBT_MAXTXERR)
 928                                                        ecp->txerrs[n] = sc;
 929
 930                                                if (sc & BD_ENET_TX_DEF)
 931                                                        ecp->txeacc.def++;
 932                                                if (sc & BD_ENET_TX_HB)
 933                                                        ecp->txeacc.hb++;
 934                                                if (sc & BD_ENET_TX_LC)
 935                                                        ecp->txeacc.lc++;
 936                                                if (sc & BD_ENET_TX_RL)
 937                                                        ecp->txeacc.rl++;
 938                                                if (sc & BD_ENET_TX_RCMASK)
 939                                                        ecp->txeacc.rc++;
 940                                                if (sc & BD_ENET_TX_UN)
 941                                                        ecp->txeacc.un++;
 942                                                if (sc & BD_ENET_TX_CSL)
 943                                                        ecp->txeacc.csl++;
 944
 945                                                bdp->cbd_sc &= \
 946                                                        ~BD_ENET_TX_STATS;
 947                                        }
 948
 949                                        if (ecp->state == Closing)
 950                                                ecp->clstime = get_timer (0);
 951
 952                                        /* make it ready again */
 953                                        bdp->cbd_sc |= BD_ENET_TX_READY;
 954                                }
 955
 956                                for (i = 0; i < ELBT_NRXBD; i++) {
 957                                        cbd_t *bdp = &ecp->rxbd[i];
 958                                        ushort sc = bdp->cbd_sc, mask;
 959
 960                                        if ((sc & BD_ENET_RX_EMPTY) != 0)
 961                                                continue;
 962
 963                                        /* we have a new frame in this buffer */
 964                                        ecp->nrcvd++;
 965
 966                                        mask = BD_ENET_RX_LAST|BD_ENET_RX_FIRST;
 967                                        if ((sc & mask) != mask) {
 968                                                /* somethings wrong here ... */
 969                                                if (!(sc & BD_ENET_RX_LAST))
 970                                                        ecp->rxeacc._l++;
 971                                                if (!(sc & BD_ENET_RX_FIRST))
 972                                                        ecp->rxeacc._f++;
 973                                        }
 974
 975                                        if (sc & BD_ENET_RX_ERRS) {
 976                                                ulong n;
 977
 978                                                /*
 979                                                 * we had some sort of error
 980                                                 * on the frame
 981                                                 */
 982                                                n = ecp->nrxerr++;
 983                                                if (n < ELBT_MAXRXERR)
 984                                                        ecp->rxerrs[n] = sc;
 985
 986                                                if (sc & BD_ENET_RX_MISS)
 987                                                        ecp->rxeacc.m++;
 988                                                if (sc & BD_ENET_RX_BC)
 989                                                        ecp->rxeacc.bc++;
 990                                                if (sc & BD_ENET_RX_MC)
 991                                                        ecp->rxeacc.mc++;
 992                                                if (sc & BD_ENET_RX_LG)
 993                                                        ecp->rxeacc.lg++;
 994                                                if (sc & BD_ENET_RX_NO)
 995                                                        ecp->rxeacc.no++;
 996                                                if (sc & BD_ENET_RX_SH)
 997                                                        ecp->rxeacc.sh++;
 998                                                if (sc & BD_ENET_RX_CR)
 999                                                        ecp->rxeacc.cr++;
1000                                                if (sc & BD_ENET_RX_OV)
1001                                                        ecp->rxeacc.ov++;
1002                                                if (sc & BD_ENET_RX_CL)
1003                                                        ecp->rxeacc.cl++;
1004
1005                                                bdp->cbd_sc &= \
1006                                                        ~BD_ENET_RX_ERRS;
1007                                        }
1008                                        else {
1009                                                ushort datlen = bdp->cbd_datlen;
1010                                                struct ethernet_hdr *ehp;
1011                                                ushort prot;
1012                                                int ours, tb, n, nbytes;
1013
1014                                                ehp = (struct ethernet_hdr *) \
1015                                                        &ecp->rxbufs[i][0];
1016
1017                                                ours = memcmp (ehp->et_src, \
1018                                                        net_ethaddr, 6);
1019
1020                                                prot = swap16 (ehp->et_protlen);
1021                                                tb = prot & 0x8000;
1022                                                n = prot & 0x7fff;
1023
1024                                                nbytes = ELBT_BUFSZ -
1025                                                        ETHER_HDR_SIZE -
1026                                                        ELBT_CRCSZ;
1027
1028                                                /* check the frame is correct */
1029                                                if (datlen != ELBT_BUFSZ)
1030                                                        ecp->rxeacc.badlen++;
1031                                                else if (!ours)
1032                                                        ecp->rxeacc.badsrc++;
1033                                                else if (!tb || n >= ELBT_NTXBD)
1034                                                        ecp->rxeacc.badtyp++;
1035                                                else {
1036                                                        ulong patword = \
1037                                                                patwords[n];
1038                                                        uint nbb;
1039
1040                                                        nbb = badbits(
1041                                                            ((uchar *)&ehp) +
1042                                                            ETHER_HDR_SIZE,
1043                                                            nbytes, patword);
1044
1045                                                        ecp->rxeacc.badbit += \
1046                                                                nbb;
1047                                                }
1048                                        }
1049
1050                                        if (ecp->state == Closing)
1051                                            ecp->clstime = get_timer (0);
1052
1053                                        /* make it empty again */
1054                                        bdp->cbd_sc |= BD_ENET_RX_EMPTY;
1055                                }
1056
1057                                if (ecp->state != Closing)
1058                                        break;
1059
1060                                /*
1061                                 * (while Closing) check to see if
1062                                 * waited long enough
1063                                 */
1064
1065                                if (get_timer (ecp->clstime) >= ELBT_CLSWAIT) {
1066                                        /* write GFMR: disable tx/rx */
1067                                        fcp->fcc_gfmr &= \
1068                                                ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
1069                                        ecp->state = Closed;
1070                                }
1071
1072                                break;
1073
1074                        case Closed:
1075                                nclosed++;
1076                                break;
1077                        }
1078                }
1079
1080        } while (nclosed < (FCC_END_LOOP - FCC_START_LOOP + 1));
1081
1082        runtime = get_timer (runtime);
1083        if (runtime <= ELBT_CLSWAIT) {
1084                printf ("Whoops! somehow elapsed time (%ld) is wrong (<= %d)\n",
1085                        runtime, ELBT_CLSWAIT);
1086                return;
1087        }
1088        nmsec = runtime - ELBT_CLSWAIT;
1089
1090        printf ("Test Finished in %ldms (plus %dms close wait period)!\n\n",
1091                nmsec, ELBT_CLSWAIT);
1092
1093        /*
1094         * now print stats
1095         */
1096
1097        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
1098                elbt_chan *ecp = &elbt_chans[c];
1099                uint rxpps, txpps, nerr;
1100
1101                rxpps = (ecp->nrcvd * 1000) / nmsec;
1102                txpps = (ecp->nsent * 1000) / nmsec;
1103
1104                printf ("Channel %d: %d rcvd (%d pps, %d rxerrs), "
1105                        "%d sent (%d pps, %d txerrs)\n\n", c,
1106                        ecp->nrcvd, rxpps, ecp->nrxerr,
1107                        ecp->nsent, txpps, ecp->ntxerr);
1108
1109                if ((nerr = ecp->nrxerr) > 0) {
1110                        ulong i;
1111
1112                        printf ("\tFirst %d rx errs:", nerr);
1113                        for (i = 0; i < nerr; i++)
1114                                printf (" %04x", ecp->rxerrs[i]);
1115                        putc ('\n');
1116                }
1117
1118                if ((nerr = ecp->ntxerr) > 0) {
1119                        ulong i;
1120
1121                        printf ("\tFirst %d tx errs:", nerr);
1122                        for (i = 0; i < nerr; i++)
1123                                printf (" %04x", ecp->txerrs[i]);
1124                        putc ('\n');
1125                }
1126        }
1127
1128        puts ("Receive Error Counts:\n");
1129        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1130                bases[c] = (uchar *)&elbt_chans[c].rxeacc;
1131        print_desc (rxeacc_descs, rxeacc_ndesc, bases, 3);
1132
1133        puts ("\nTransmit Error Counts:\n");
1134        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1135                bases[c] = (uchar *)&elbt_chans[c].txeacc;
1136        print_desc (txeacc_descs, txeacc_ndesc, bases, 3);
1137
1138        puts ("\nRMON(-like) Counters:\n");
1139        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1140                bases[c] = (uchar *)&immr->im_dprambase[elbt_chans[c].proff];
1141        print_desc (epram_descs, epram_ndesc, bases, 3);
1142}
1143
1144#endif /* CONFIG_ETHER_LOOPBACK_TEST */
1145
1146#endif
1147