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 < ARRAY_SIZE(ether_fcc_info); 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                int retval;
 383                struct mii_dev *mdiodev = mdio_alloc();
 384                if (!mdiodev)
 385                        return -ENOMEM;
 386                strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
 387                mdiodev->read = bb_miiphy_read;
 388                mdiodev->write = bb_miiphy_write;
 389
 390                retval = mdio_register(mdiodev);
 391                if (retval < 0)
 392                        return retval;
 393#endif
 394        }
 395
 396        return 1;
 397}
 398
 399#ifdef CONFIG_ETHER_LOOPBACK_TEST
 400
 401#define ELBT_BUFSZ      1024    /* must be multiple of 32 */
 402
 403#define ELBT_CRCSZ      4
 404
 405#define ELBT_NRXBD      4       /* must be at least 2 */
 406#define ELBT_NTXBD      4
 407
 408#define ELBT_MAXRXERR   32
 409#define ELBT_MAXTXERR   32
 410
 411#define ELBT_CLSWAIT    1000    /* msec to wait for further input frames */
 412
 413typedef
 414        struct {
 415                uint off;
 416                char *lab;
 417        }
 418elbt_prdesc;
 419
 420typedef
 421        struct {
 422                uint _l, _f, m, bc, mc, lg, no, sh, cr, ov, cl;
 423                uint badsrc, badtyp, badlen, badbit;
 424        }
 425elbt_rxeacc;
 426
 427static elbt_prdesc rxeacc_descs[] = {
 428        { offsetof(elbt_rxeacc, _l),            "Not Last in Frame"     },
 429        { offsetof(elbt_rxeacc, _f),            "Not First in Frame"    },
 430        { offsetof(elbt_rxeacc, m),             "Address Miss"          },
 431        { offsetof(elbt_rxeacc, bc),            "Broadcast Address"     },
 432        { offsetof(elbt_rxeacc, mc),            "Multicast Address"     },
 433        { offsetof(elbt_rxeacc, lg),            "Frame Length Violation"},
 434        { offsetof(elbt_rxeacc, no),            "Non-Octet Alignment"   },
 435        { offsetof(elbt_rxeacc, sh),            "Short Frame"           },
 436        { offsetof(elbt_rxeacc, cr),            "CRC Error"             },
 437        { offsetof(elbt_rxeacc, ov),            "Overrun"               },
 438        { offsetof(elbt_rxeacc, cl),            "Collision"             },
 439        { offsetof(elbt_rxeacc, badsrc),        "Bad Src Address"       },
 440        { offsetof(elbt_rxeacc, badtyp),        "Bad Frame Type"        },
 441        { offsetof(elbt_rxeacc, badlen),        "Bad Frame Length"      },
 442        { offsetof(elbt_rxeacc, badbit),        "Data Compare Errors"   },
 443};
 444static int rxeacc_ndesc = ARRAY_SIZE(rxeacc_descs);
 445
 446typedef
 447        struct {
 448                uint def, hb, lc, rl, rc, un, csl;
 449        }
 450elbt_txeacc;
 451
 452static elbt_prdesc txeacc_descs[] = {
 453        { offsetof(elbt_txeacc, def),           "Defer Indication"      },
 454        { offsetof(elbt_txeacc, hb),            "Heartbeat"             },
 455        { offsetof(elbt_txeacc, lc),            "Late Collision"        },
 456        { offsetof(elbt_txeacc, rl),            "Retransmission Limit"  },
 457        { offsetof(elbt_txeacc, rc),            "Retry Count"           },
 458        { offsetof(elbt_txeacc, un),            "Underrun"              },
 459        { offsetof(elbt_txeacc, csl),           "Carrier Sense Lost"    },
 460};
 461static int txeacc_ndesc = ARRAY_SIZE(txeacc_descs);
 462
 463typedef
 464        struct {
 465                uchar rxbufs[ELBT_NRXBD][ELBT_BUFSZ];
 466                uchar txbufs[ELBT_NTXBD][ELBT_BUFSZ];
 467                cbd_t rxbd[ELBT_NRXBD];
 468                cbd_t txbd[ELBT_NTXBD];
 469                enum { Idle, Running, Closing, Closed } state;
 470                int proff, page, sblock;
 471                uint clstime, nsent, ntxerr, nrcvd, nrxerr;
 472                ushort rxerrs[ELBT_MAXRXERR], txerrs[ELBT_MAXTXERR];
 473                elbt_rxeacc rxeacc;
 474                elbt_txeacc txeacc;
 475        } __attribute__ ((aligned(8)))
 476elbt_chan;
 477
 478static uchar patbytes[ELBT_NTXBD] = {
 479        0xff, 0xaa, 0x55, 0x00
 480};
 481static uint patwords[ELBT_NTXBD] = {
 482        0xffffffff, 0xaaaaaaaa, 0x55555555, 0x00000000
 483};
 484
 485#ifdef __GNUC__
 486static elbt_chan elbt_chans[3] __attribute__ ((aligned(8)));
 487#else
 488#error "elbt_chans must be 64-bit aligned"
 489#endif
 490
 491#define CPM_CR_GRACEFUL_STOP_TX ((ushort)0x0005)
 492
 493static elbt_prdesc epram_descs[] = {
 494        { offsetof(fcc_enet_t, fen_crcec),      "CRC Errors"            },
 495        { offsetof(fcc_enet_t, fen_alec),       "Alignment Errors"      },
 496        { offsetof(fcc_enet_t, fen_disfc),      "Discarded Frames"      },
 497        { offsetof(fcc_enet_t, fen_octc),       "Octets"                },
 498        { offsetof(fcc_enet_t, fen_colc),       "Collisions"            },
 499        { offsetof(fcc_enet_t, fen_broc),       "Broadcast Frames"      },
 500        { offsetof(fcc_enet_t, fen_mulc),       "Multicast Frames"      },
 501        { offsetof(fcc_enet_t, fen_uspc),       "Undersize Frames"      },
 502        { offsetof(fcc_enet_t, fen_frgc),       "Fragments"             },
 503        { offsetof(fcc_enet_t, fen_ospc),       "Oversize Frames"       },
 504        { offsetof(fcc_enet_t, fen_jbrc),       "Jabbers"               },
 505        { offsetof(fcc_enet_t, fen_p64c),       "64 Octet Frames"       },
 506        { offsetof(fcc_enet_t, fen_p65c),       "65-127 Octet Frames"   },
 507        { offsetof(fcc_enet_t, fen_p128c),      "128-255 Octet Frames"  },
 508        { offsetof(fcc_enet_t, fen_p256c),      "256-511 Octet Frames"  },
 509        { offsetof(fcc_enet_t, fen_p512c),      "512-1023 Octet Frames" },
 510        { offsetof(fcc_enet_t, fen_p1024c),     "1024-1518 Octet Frames"},
 511};
 512static int epram_ndesc = ARRAY_SIZE(epram_descs);
 513
 514/*
 515 * given an elbt_prdesc array and an array of base addresses, print
 516 * each prdesc down the screen with the values fetched from each
 517 * base address across the screen
 518 */
 519static void
 520print_desc (elbt_prdesc descs[], int ndesc, uchar *bases[], int nbase)
 521{
 522        elbt_prdesc *dp = descs, *edp = dp + ndesc;
 523        int i;
 524
 525        printf ("%32s", "");
 526
 527        for (i = 0; i < nbase; i++)
 528                printf ("  Channel %d", i);
 529
 530        putc ('\n');
 531
 532        while (dp < edp) {
 533
 534                printf ("%-32s", dp->lab);
 535
 536                for (i = 0; i < nbase; i++) {
 537                        uint val = *(uint *)(bases[i] + dp->off);
 538
 539                        printf (" %10u", val);
 540                }
 541
 542                putc ('\n');
 543
 544                dp++;
 545        }
 546}
 547
 548/*
 549 * return number of bits that are set in a value; value contains
 550 * nbits (right-justified) bits.
 551 */
 552static uint __inline__
 553nbs (uint value, uint nbits)
 554{
 555        uint cnt = 0;
 556#if 1
 557        uint pos = sizeof (uint) * 8;
 558
 559        __asm__ __volatile__ ("\
 560        mtctr   %2\n\
 5611:      rlwnm.  %2,%1,%4,31,31\n\
 562        beq     2f\n\
 563        addi    %0,%0,1\n\
 5642:      subi    %4,%4,1\n\
 565        bdnz    1b"
 566        : "=r"(cnt)
 567        : "r"(value), "r"(nbits), "r"(cnt), "r"(pos)
 568        : "ctr", "cc" );
 569#else
 570        uint mask = 1;
 571
 572        do {
 573                if (value & mask)
 574                        cnt++;
 575                mask <<= 1;
 576        } while (--nbits);
 577#endif
 578
 579        return (cnt);
 580}
 581
 582static ulong
 583badbits (uchar *bp, int n, ulong pat)
 584{
 585        ulong *lp, cnt = 0;
 586        int nl;
 587
 588        while (n > 0 && ((ulong)bp & (sizeof (ulong) - 1)) != 0) {
 589                uchar diff;
 590
 591                diff = *bp++ ^ (uchar)pat;
 592
 593                if (diff)
 594                        cnt += nbs ((ulong)diff, 8);
 595
 596                n--;
 597        }
 598
 599        lp = (ulong *)bp;
 600        nl = n / sizeof (ulong);
 601        n -= nl * sizeof (ulong);
 602
 603        while (nl > 0) {
 604                ulong diff;
 605
 606                diff = *lp++ ^ pat;
 607
 608                if (diff)
 609                        cnt += nbs (diff, 32);
 610
 611                nl--;
 612        }
 613
 614        bp = (uchar *)lp;
 615
 616        while (n > 0) {
 617                uchar diff;
 618
 619                diff = *bp++ ^ (uchar)pat;
 620
 621                if (diff)
 622                        cnt += nbs ((ulong)diff, 8);
 623
 624                n--;
 625        }
 626
 627        return (cnt);
 628}
 629
 630static inline unsigned short
 631swap16 (unsigned short x)
 632{
 633        return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
 634}
 635
 636/* broadcast is not an error - we send them like that */
 637#define BD_ENET_RX_ERRS (BD_ENET_RX_STATS & ~BD_ENET_RX_BC)
 638
 639void
 640eth_loopback_test (void)
 641{
 642        volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
 643        volatile cpm8260_t *cp = &(immr->im_cpm);
 644        int c, nclosed;
 645        ulong runtime, nmsec;
 646        uchar *bases[3];
 647
 648        puts ("FCC Ethernet External loopback test\n");
 649
 650        eth_getenv_enetaddr("ethaddr", net_ethaddr);
 651
 652        /*
 653         * global initialisations for all FCC channels
 654         */
 655
 656        /* 28.9 - (1-2): ioports have been set up already */
 657
 658#if defined(CONFIG_SACSng)
 659        /*
 660         * Attention: this is board-specific
 661         * 1, FCC2
 662         */
 663#       define FCC_START_LOOP 1
 664#       define FCC_END_LOOP   1
 665
 666        /*
 667         * Attention: this is board-specific
 668         * - FCC2 Rx-CLK is CLK13
 669         * - FCC2 Tx-CLK is CLK14
 670         */
 671
 672        /* 28.9 - (3): connect FCC's tx and rx clocks */
 673        immr->im_cpmux.cmx_uar = 0;
 674        immr->im_cpmux.cmx_fcr = CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14;
 675#else
 676#error "eth_loopback_test not supported on your board"
 677#endif
 678
 679        puts ("Initialise FCC channels:");
 680
 681        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
 682                elbt_chan *ecp = &elbt_chans[c];
 683                volatile fcc_t *fcp = &immr->im_fcc[c];
 684                volatile fcc_enet_t *fpp;
 685                int i;
 686                ulong addr;
 687
 688                /*
 689                 * initialise channel data
 690                 */
 691
 692                printf (" %d", c);
 693
 694                memset ((void *)ecp, 0, sizeof (*ecp));
 695
 696                ecp->state = Idle;
 697
 698                switch (c) {
 699
 700                case 0: /* FCC1 */
 701                        ecp->proff = PROFF_FCC1;
 702                        ecp->page = CPM_CR_FCC1_PAGE;
 703                        ecp->sblock = CPM_CR_FCC1_SBLOCK;
 704                        break;
 705
 706                case 1: /* FCC2 */
 707                        ecp->proff = PROFF_FCC2;
 708                        ecp->page = CPM_CR_FCC2_PAGE;
 709                        ecp->sblock = CPM_CR_FCC2_SBLOCK;
 710                        break;
 711
 712                case 2: /* FCC3 */
 713                        ecp->proff = PROFF_FCC3;
 714                        ecp->page = CPM_CR_FCC3_PAGE;
 715                        ecp->sblock = CPM_CR_FCC3_SBLOCK;
 716                        break;
 717                }
 718
 719                /*
 720                 * set up tx buffers and bds
 721                 */
 722
 723                for (i = 0; i < ELBT_NTXBD; i++) {
 724                        cbd_t *bdp = &ecp->txbd[i];
 725                        uchar *bp = &ecp->txbufs[i][0];
 726
 727                        bdp->cbd_bufaddr = (uint)bp;
 728                        /* room for crc */
 729                        bdp->cbd_datlen = ELBT_BUFSZ - ELBT_CRCSZ;
 730                        bdp->cbd_sc = BD_ENET_TX_READY | BD_ENET_TX_PAD | \
 731                                BD_ENET_TX_LAST | BD_ENET_TX_TC;
 732
 733                        memset((void *)bp, patbytes[i], ELBT_BUFSZ);
 734                        net_set_ether(bp, net_bcast_ethaddr, 0x8000);
 735                }
 736                ecp->txbd[ELBT_NTXBD - 1].cbd_sc |= BD_ENET_TX_WRAP;
 737
 738                /*
 739                 * set up rx buffers and bds
 740                 */
 741
 742                for (i = 0; i < ELBT_NRXBD; i++) {
 743                    cbd_t *bdp = &ecp->rxbd[i];
 744                    uchar *bp = &ecp->rxbufs[i][0];
 745
 746                    bdp->cbd_bufaddr = (uint)bp;
 747                    bdp->cbd_datlen = 0;
 748                    bdp->cbd_sc = BD_ENET_RX_EMPTY;
 749
 750                    memset ((void *)bp, 0, ELBT_BUFSZ);
 751                }
 752                ecp->rxbd[ELBT_NRXBD - 1].cbd_sc |= BD_ENET_RX_WRAP;
 753
 754                /*
 755                 * set up the FCC channel hardware
 756                 */
 757
 758                /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
 759                fcp->fcc_gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
 760
 761                /* 28.9 - (5): FPSMR: fd, enet CRC, Promis, RMON, Rx SHort */
 762                fcp->fcc_fpsmr = FCC_PSMR_FDE | FCC_PSMR_LPB | \
 763                        FCC_PSMR_ENCRC | FCC_PSMR_PRO | \
 764                        FCC_PSMR_MON | FCC_PSMR_RSH;
 765
 766                /* 28.9 - (6): FDSR: Ethernet Syn */
 767                fcp->fcc_fdsr = 0xD555;
 768
 769                /* 29.9 - (7): initialise parameter ram */
 770                fpp = (fcc_enet_t *)&(immr->im_dprambase[ecp->proff]);
 771
 772                /* clear whole struct to make sure all resv fields are zero */
 773                memset ((void *)fpp, 0, sizeof (fcc_enet_t));
 774
 775                /*
 776                 * common Parameter RAM area
 777                 *
 778                 * Allocate space in the reserved FCC area of DPRAM for the
 779                 * internal buffers.  No one uses this space (yet), so we
 780                 * can do this.  Later, we will add resource management for
 781                 * this area.
 782                 */
 783                addr = CPM_FCC_SPECIAL_BASE + (c * 64);
 784                fpp->fen_genfcc.fcc_riptr = addr;
 785                fpp->fen_genfcc.fcc_tiptr = addr + 32;
 786
 787                /*
 788                 * Set maximum bytes per receive buffer.
 789                 * It must be a multiple of 32.
 790                 * buffers are in 60x bus memory.
 791                 */
 792                fpp->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
 793                fpp->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
 794                fpp->fen_genfcc.fcc_rbase = (unsigned int)(&ecp->rxbd[0]);
 795                fpp->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
 796                fpp->fen_genfcc.fcc_tbase = (unsigned int)(&ecp->txbd[0]);
 797
 798                /* protocol-specific area */
 799                fpp->fen_cmask = 0xdebb20e3;    /* CRC mask */
 800                fpp->fen_cpres = 0xffffffff;    /* CRC preset */
 801                fpp->fen_retlim = 15;           /* Retry limit threshold */
 802                fpp->fen_mflr = PKT_MAXBUF_SIZE;/* max frame length register */
 803
 804                /*
 805                 * Set Ethernet station address.
 806                 *
 807                 * This is supplied in the board information structure, so we
 808                 * copy that into the controller.
 809                 * So, far we have only been given one Ethernet address. We use
 810                 * the same address for all channels
 811                 */
 812                fpp->fen_paddrh = (net_ethaddr[5] << 8) + net_ethaddr[4];
 813                fpp->fen_paddrm = (net_ethaddr[3] << 8) + net_ethaddr[2];
 814                fpp->fen_paddrl = (net_ethaddr[1] << 8) + net_ethaddr[0];
 815
 816                fpp->fen_minflr = PKT_MINBUF_SIZE; /* min frame len register */
 817                /*
 818                 * pad pointer. use tiptr since we don't need
 819                 * a specific padding char
 820                 */
 821                fpp->fen_padptr = fpp->fen_genfcc.fcc_tiptr;
 822                fpp->fen_maxd1 = PKT_MAXDMA_SIZE;       /* max DMA1 length */
 823                fpp->fen_maxd2 = PKT_MAXDMA_SIZE;       /* max DMA2 length */
 824                fpp->fen_rfthr = 1;
 825                fpp->fen_rfcnt = 1;
 826
 827                /* 28.9 - (8): clear out events in FCCE */
 828                fcp->fcc_fcce = ~0x0;
 829
 830                /* 28.9 - (9): FCCM: mask all events */
 831                fcp->fcc_fccm = 0;
 832
 833                /* 28.9 - (10-12): we don't use ethernet interrupts */
 834
 835                /* 28.9 - (13)
 836                 *
 837                 * Let's re-initialize the channel now.  We have to do it later
 838                 * than the manual describes because we have just now finished
 839                 * the BD initialization.
 840                 */
 841                cp->cp_cpcr = mk_cr_cmd (ecp->page, ecp->sblock, \
 842                        0x0c, CPM_CR_INIT_TRX) | CPM_CR_FLG;
 843                do {
 844                        __asm__ __volatile__ ("eieio");
 845                } while (cp->cp_cpcr & CPM_CR_FLG);
 846        }
 847
 848        puts (" done\nStarting test... (Ctrl-C to Finish)\n");
 849
 850        /*
 851         * Note: don't want serial output from here until the end of the
 852         * test - the delays would probably stuff things up.
 853         */
 854
 855        clear_ctrlc ();
 856        runtime = get_timer (0);
 857
 858        do {
 859                nclosed = 0;
 860
 861                for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
 862                        volatile fcc_t *fcp = &immr->im_fcc[c];
 863                        elbt_chan *ecp = &elbt_chans[c];
 864                        int i;
 865
 866                        switch (ecp->state) {
 867
 868                        case Idle:
 869                                /*
 870                                 * set the channel Running ...
 871                                 */
 872
 873                                /* 28.9 - (14): enable tx/rx in gfmr */
 874                                fcp->fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
 875
 876                                ecp->state = Running;
 877                                break;
 878
 879                        case Running:
 880                                /*
 881                                 * (while Running only) check for
 882                                 * termination of the test
 883                                 */
 884
 885                                (void)ctrlc ();
 886
 887                                if (had_ctrlc ()) {
 888                                        /*
 889                                         * initiate a "graceful stop transmit"
 890                                         * on the channel
 891                                         */
 892                                        cp->cp_cpcr = mk_cr_cmd (ecp->page, \
 893                                                ecp->sblock, 0x0c, \
 894                                                CPM_CR_GRACEFUL_STOP_TX) | \
 895                                                CPM_CR_FLG;
 896                                        do {
 897                                                __asm__ __volatile__ ("eieio");
 898                                        } while (cp->cp_cpcr & CPM_CR_FLG);
 899
 900                                        ecp->clstime = get_timer (0);
 901                                        ecp->state = Closing;
 902                                }
 903                                /* fall through ... */
 904
 905                        case Closing:
 906                                /*
 907                                 * (while Running or Closing) poll the channel:
 908                                 * - check for any non-READY tx buffers and
 909                                 *   make them ready
 910                                 * - check for any non-EMPTY rx buffers and
 911                                 *   check that they were received correctly,
 912                                 *   adjust counters etc, then make empty
 913                                 */
 914
 915                                for (i = 0; i < ELBT_NTXBD; i++) {
 916                                        cbd_t *bdp = &ecp->txbd[i];
 917                                        ushort sc = bdp->cbd_sc;
 918
 919                                        if ((sc & BD_ENET_TX_READY) != 0)
 920                                                continue;
 921
 922                                        /*
 923                                         * this frame has finished
 924                                         * transmitting
 925                                         */
 926                                        ecp->nsent++;
 927
 928                                        if (sc & BD_ENET_TX_STATS) {
 929                                                ulong n;
 930
 931                                                /*
 932                                                 * we had an error on
 933                                                 * the transmission
 934                                                 */
 935                                                n = ecp->ntxerr++;
 936                                                if (n < ELBT_MAXTXERR)
 937                                                        ecp->txerrs[n] = sc;
 938
 939                                                if (sc & BD_ENET_TX_DEF)
 940                                                        ecp->txeacc.def++;
 941                                                if (sc & BD_ENET_TX_HB)
 942                                                        ecp->txeacc.hb++;
 943                                                if (sc & BD_ENET_TX_LC)
 944                                                        ecp->txeacc.lc++;
 945                                                if (sc & BD_ENET_TX_RL)
 946                                                        ecp->txeacc.rl++;
 947                                                if (sc & BD_ENET_TX_RCMASK)
 948                                                        ecp->txeacc.rc++;
 949                                                if (sc & BD_ENET_TX_UN)
 950                                                        ecp->txeacc.un++;
 951                                                if (sc & BD_ENET_TX_CSL)
 952                                                        ecp->txeacc.csl++;
 953
 954                                                bdp->cbd_sc &= \
 955                                                        ~BD_ENET_TX_STATS;
 956                                        }
 957
 958                                        if (ecp->state == Closing)
 959                                                ecp->clstime = get_timer (0);
 960
 961                                        /* make it ready again */
 962                                        bdp->cbd_sc |= BD_ENET_TX_READY;
 963                                }
 964
 965                                for (i = 0; i < ELBT_NRXBD; i++) {
 966                                        cbd_t *bdp = &ecp->rxbd[i];
 967                                        ushort sc = bdp->cbd_sc, mask;
 968
 969                                        if ((sc & BD_ENET_RX_EMPTY) != 0)
 970                                                continue;
 971
 972                                        /* we have a new frame in this buffer */
 973                                        ecp->nrcvd++;
 974
 975                                        mask = BD_ENET_RX_LAST|BD_ENET_RX_FIRST;
 976                                        if ((sc & mask) != mask) {
 977                                                /* somethings wrong here ... */
 978                                                if (!(sc & BD_ENET_RX_LAST))
 979                                                        ecp->rxeacc._l++;
 980                                                if (!(sc & BD_ENET_RX_FIRST))
 981                                                        ecp->rxeacc._f++;
 982                                        }
 983
 984                                        if (sc & BD_ENET_RX_ERRS) {
 985                                                ulong n;
 986
 987                                                /*
 988                                                 * we had some sort of error
 989                                                 * on the frame
 990                                                 */
 991                                                n = ecp->nrxerr++;
 992                                                if (n < ELBT_MAXRXERR)
 993                                                        ecp->rxerrs[n] = sc;
 994
 995                                                if (sc & BD_ENET_RX_MISS)
 996                                                        ecp->rxeacc.m++;
 997                                                if (sc & BD_ENET_RX_BC)
 998                                                        ecp->rxeacc.bc++;
 999                                                if (sc & BD_ENET_RX_MC)
1000                                                        ecp->rxeacc.mc++;
1001                                                if (sc & BD_ENET_RX_LG)
1002                                                        ecp->rxeacc.lg++;
1003                                                if (sc & BD_ENET_RX_NO)
1004                                                        ecp->rxeacc.no++;
1005                                                if (sc & BD_ENET_RX_SH)
1006                                                        ecp->rxeacc.sh++;
1007                                                if (sc & BD_ENET_RX_CR)
1008                                                        ecp->rxeacc.cr++;
1009                                                if (sc & BD_ENET_RX_OV)
1010                                                        ecp->rxeacc.ov++;
1011                                                if (sc & BD_ENET_RX_CL)
1012                                                        ecp->rxeacc.cl++;
1013
1014                                                bdp->cbd_sc &= \
1015                                                        ~BD_ENET_RX_ERRS;
1016                                        }
1017                                        else {
1018                                                ushort datlen = bdp->cbd_datlen;
1019                                                struct ethernet_hdr *ehp;
1020                                                ushort prot;
1021                                                int ours, tb, n, nbytes;
1022
1023                                                ehp = (struct ethernet_hdr *) \
1024                                                        &ecp->rxbufs[i][0];
1025
1026                                                ours = memcmp (ehp->et_src, \
1027                                                        net_ethaddr, 6);
1028
1029                                                prot = swap16 (ehp->et_protlen);
1030                                                tb = prot & 0x8000;
1031                                                n = prot & 0x7fff;
1032
1033                                                nbytes = ELBT_BUFSZ -
1034                                                        ETHER_HDR_SIZE -
1035                                                        ELBT_CRCSZ;
1036
1037                                                /* check the frame is correct */
1038                                                if (datlen != ELBT_BUFSZ)
1039                                                        ecp->rxeacc.badlen++;
1040                                                else if (!ours)
1041                                                        ecp->rxeacc.badsrc++;
1042                                                else if (!tb || n >= ELBT_NTXBD)
1043                                                        ecp->rxeacc.badtyp++;
1044                                                else {
1045                                                        ulong patword = \
1046                                                                patwords[n];
1047                                                        uint nbb;
1048
1049                                                        nbb = badbits(
1050                                                            ((uchar *)&ehp) +
1051                                                            ETHER_HDR_SIZE,
1052                                                            nbytes, patword);
1053
1054                                                        ecp->rxeacc.badbit += \
1055                                                                nbb;
1056                                                }
1057                                        }
1058
1059                                        if (ecp->state == Closing)
1060                                            ecp->clstime = get_timer (0);
1061
1062                                        /* make it empty again */
1063                                        bdp->cbd_sc |= BD_ENET_RX_EMPTY;
1064                                }
1065
1066                                if (ecp->state != Closing)
1067                                        break;
1068
1069                                /*
1070                                 * (while Closing) check to see if
1071                                 * waited long enough
1072                                 */
1073
1074                                if (get_timer (ecp->clstime) >= ELBT_CLSWAIT) {
1075                                        /* write GFMR: disable tx/rx */
1076                                        fcp->fcc_gfmr &= \
1077                                                ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
1078                                        ecp->state = Closed;
1079                                }
1080
1081                                break;
1082
1083                        case Closed:
1084                                nclosed++;
1085                                break;
1086                        }
1087                }
1088
1089        } while (nclosed < (FCC_END_LOOP - FCC_START_LOOP + 1));
1090
1091        runtime = get_timer (runtime);
1092        if (runtime <= ELBT_CLSWAIT) {
1093                printf ("Whoops! somehow elapsed time (%ld) is wrong (<= %d)\n",
1094                        runtime, ELBT_CLSWAIT);
1095                return;
1096        }
1097        nmsec = runtime - ELBT_CLSWAIT;
1098
1099        printf ("Test Finished in %ldms (plus %dms close wait period)!\n\n",
1100                nmsec, ELBT_CLSWAIT);
1101
1102        /*
1103         * now print stats
1104         */
1105
1106        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
1107                elbt_chan *ecp = &elbt_chans[c];
1108                uint rxpps, txpps, nerr;
1109
1110                rxpps = (ecp->nrcvd * 1000) / nmsec;
1111                txpps = (ecp->nsent * 1000) / nmsec;
1112
1113                printf ("Channel %d: %d rcvd (%d pps, %d rxerrs), "
1114                        "%d sent (%d pps, %d txerrs)\n\n", c,
1115                        ecp->nrcvd, rxpps, ecp->nrxerr,
1116                        ecp->nsent, txpps, ecp->ntxerr);
1117
1118                if ((nerr = ecp->nrxerr) > 0) {
1119                        ulong i;
1120
1121                        printf ("\tFirst %d rx errs:", nerr);
1122                        for (i = 0; i < nerr; i++)
1123                                printf (" %04x", ecp->rxerrs[i]);
1124                        putc ('\n');
1125                }
1126
1127                if ((nerr = ecp->ntxerr) > 0) {
1128                        ulong i;
1129
1130                        printf ("\tFirst %d tx errs:", nerr);
1131                        for (i = 0; i < nerr; i++)
1132                                printf (" %04x", ecp->txerrs[i]);
1133                        putc ('\n');
1134                }
1135        }
1136
1137        puts ("Receive Error Counts:\n");
1138        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1139                bases[c] = (uchar *)&elbt_chans[c].rxeacc;
1140        print_desc (rxeacc_descs, rxeacc_ndesc, bases, 3);
1141
1142        puts ("\nTransmit Error Counts:\n");
1143        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1144                bases[c] = (uchar *)&elbt_chans[c].txeacc;
1145        print_desc (txeacc_descs, txeacc_ndesc, bases, 3);
1146
1147        puts ("\nRMON(-like) Counters:\n");
1148        for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1149                bases[c] = (uchar *)&immr->im_dprambase[elbt_chans[c].proff];
1150        print_desc (epram_descs, epram_ndesc, bases, 3);
1151}
1152
1153#endif /* CONFIG_ETHER_LOOPBACK_TEST */
1154
1155#endif
1156