if_cpsw 46.4 KB
Newer Older
Robert Sprowson's avatar
Robert Sprowson committed
1
/*	$NetBSD: if_cpsw.c,v 1.24 2019/05/29 05:05:24 msaitoh Exp $	*/
ROOL's avatar
ROOL committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

/*
 * Copyright (c) 2013 Jonathan A. Kollasch
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef RISCOS
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/mbuf.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/ethernet.h>

#include "AsmUtils/irqs.h"
#include "bus.h"
#include "glue.h"

#include "mii.h"
#include "miivar.h"

#include "if_cpswreg.h"
#else
#include <sys/cdefs.h>
Robert Sprowson's avatar
Robert Sprowson committed
83
__KERNEL_RCSID(1, "$NetBSD: if_cpsw.c,v 1.24 2019/05/29 05:05:24 msaitoh Exp $");
ROOL's avatar
ROOL committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/intr.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/kernel.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#include <net/bpf.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>

#include <arch/arm/omap/omap2_obiovar.h>
#include <arch/arm/omap/if_cpswreg.h>
#include <arch/arm/omap/sitara_cmreg.h>
#include <arch/arm/omap/sitara_cm.h>
#endif

#define CPSW_TXFRAGS	16

#ifdef RISCOS
/* Tone this down a bit to save RAM, there's never many descriptors in flight */
#define CPSW_CPPI_RAM_TXDESCS_SIZE 0x100 /* 16 */
#define CPSW_CPPI_RAM_RXDESCS_SIZE 0x400 /* 64 */
#else
#define CPSW_CPPI_RAM_SIZE (0x2000)
#define CPSW_CPPI_RAM_TXDESCS_SIZE (CPSW_CPPI_RAM_SIZE/2)
#define CPSW_CPPI_RAM_RXDESCS_SIZE \
    (CPSW_CPPI_RAM_SIZE - CPSW_CPPI_RAM_TXDESCS_SIZE)
#endif
#define CPSW_CPPI_RAM_TXDESCS_BASE (CPSW_CPPI_RAM_OFFSET + 0x0000)
#define CPSW_CPPI_RAM_RXDESCS_BASE \
    (CPSW_CPPI_RAM_OFFSET + CPSW_CPPI_RAM_TXDESCS_SIZE)

#define CPSW_NTXDESCS (CPSW_CPPI_RAM_TXDESCS_SIZE/sizeof(struct cpsw_cpdma_bd))
#define CPSW_NRXDESCS (CPSW_CPPI_RAM_RXDESCS_SIZE/sizeof(struct cpsw_cpdma_bd))

CTASSERT(powerof2(CPSW_NTXDESCS));
CTASSERT(powerof2(CPSW_NRXDESCS));

#define CPSW_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)

#define TXDESC_NEXT(x) cpsw_txdesc_adjust((x), 1)
#define TXDESC_PREV(x) cpsw_txdesc_adjust((x), -1)

#define RXDESC_NEXT(x) cpsw_rxdesc_adjust((x), 1)
#define RXDESC_PREV(x) cpsw_rxdesc_adjust((x), -1)

struct cpsw_ring_data {
	bus_dmamap_t tx_dm[CPSW_NTXDESCS];
	struct mbuf *tx_mb[CPSW_NTXDESCS];
	bus_dmamap_t rx_dm[CPSW_NRXDESCS];
	struct mbuf *rx_mb[CPSW_NRXDESCS];
};

struct cpsw_softc {
	device_t sc_dev;
	bus_space_tag_t sc_bst;
	bus_space_handle_t sc_bsh;
	bus_size_t sc_bss;
	bus_dma_tag_t sc_bdt;
	bus_space_handle_t sc_bsh_txdescs;
	bus_space_handle_t sc_bsh_rxdescs;
	bus_addr_t sc_txdescs_pa;
	bus_addr_t sc_rxdescs_pa;
	struct ethercom sc_ec;
	struct mii_data sc_mii;
	bool sc_phy_has_1000t;
	bool sc_attached;
	callout_t sc_tick_ch;
	void *sc_ih;
	struct cpsw_ring_data *sc_rdp;
	volatile u_int sc_txnext;
	volatile u_int sc_txhead;
	volatile u_int sc_rxhead;
	void *sc_rxthih;
	void *sc_rxih;
	void *sc_txih;
	void *sc_miscih;
	void *sc_txpad;
	bus_dmamap_t sc_txpad_dm;
#define sc_txpad_pa sc_txpad_dm->dm_segs[0].ds_addr
	uint8_t sc_enaddr[ETHER_ADDR_LEN];
	volatile bool sc_txrun;
	volatile bool sc_rxrun;
	volatile bool sc_txeoq;
	volatile bool sc_rxeoq;
};

static int cpsw_match(device_t, cfdata_t, void *);
static void cpsw_attach(device_t, device_t, void *);
static int cpsw_detach(device_t, int);

static void cpsw_start(struct ifnet *);
static int cpsw_ioctl(struct ifnet *, u_long, void *);
static void cpsw_watchdog(struct ifnet *);
static int cpsw_init(struct ifnet *);
static void cpsw_stop(struct ifnet *, int);

Robert Sprowson's avatar
Robert Sprowson committed
190 191
static int cpsw_mii_readreg(device_t, int, int, uint16_t *);
static int cpsw_mii_writereg(device_t, int, int, uint16_t);
ROOL's avatar
ROOL committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static void cpsw_mii_statchg(struct ifnet *);

static int cpsw_new_rxbuf(struct cpsw_softc * const, const u_int);
static void cpsw_tick(void *);

static int cpsw_rxthintr(void *);
static int cpsw_rxintr(void *);
static int cpsw_txintr(void *);
static int cpsw_miscintr(void *);

/* ALE support */
#define CPSW_MAX_ALE_ENTRIES	1024

static int cpsw_ale_update_addresses(struct cpsw_softc *, int purge);

CFATTACH_DECL_NEW(cpsw, sizeof(struct cpsw_softc),
    cpsw_match, cpsw_attach, cpsw_detach, NULL);

#ifdef RISCOS
static void cpsw_ale_read_entry(struct cpsw_softc *, uint16_t, uint32_t *);
static void cpsw_ale_write_entry(struct cpsw_softc *, uint16_t, const uint32_t *);
#else
#undef KERNHIST
#include <sys/kernhist.h>
KERNHIST_DEFINE(cpswhist);
#endif

#ifdef KERNHIST
#define KERNHIST_CALLED_5(NAME, i, j, k, l) \
do { \
Robert Sprowson's avatar
Robert Sprowson committed
222 223
	_kernhist_call = atomic_inc_32_nv(&_kernhist_cnt); \
	KERNHIST_LOG(NAME, "called! %jx %jx %jx %jx", i, j, k, l); \
ROOL's avatar
ROOL committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
} while (/*CONSTCOND*/ 0)
#else
#define KERNHIST_CALLED_5(NAME, i, j, k, l)
#endif

static inline u_int
cpsw_txdesc_adjust(u_int x, int y)
{
	return (((x) + y) & (CPSW_NTXDESCS - 1));
}

static inline u_int
cpsw_rxdesc_adjust(u_int x, int y)
{
	return (((x) + y) & (CPSW_NRXDESCS - 1));
}

static inline uint32_t
cpsw_read_4(struct cpsw_softc * const sc, bus_size_t const offset)
{
	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, offset);
}

static inline void
cpsw_write_4(struct cpsw_softc * const sc, bus_size_t const offset,
    uint32_t const value)
{
	bus_space_write_4(sc->sc_bst, sc->sc_bsh, offset, value);
}

static inline void
cpsw_set_txdesc_next(struct cpsw_softc * const sc, const u_int i, uint32_t n)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i + 0;

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
260
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, n, 0);
ROOL's avatar
ROOL committed
261 262 263 264 265 266 267 268 269 270

	bus_space_write_4(sc->sc_bst, sc->sc_bsh_txdescs, o, n);
}

static inline void
cpsw_set_rxdesc_next(struct cpsw_softc * const sc, const u_int i, uint32_t n)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i + 0;

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
271
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, n, 0);
ROOL's avatar
ROOL committed
272 273 274 275 276 277 278 279 280 281 282 283 284

	bus_space_write_4(sc->sc_bst, sc->sc_bsh_rxdescs, o, n);
}

static inline void
cpsw_get_txdesc(struct cpsw_softc * const sc, const u_int i,
    struct cpsw_cpdma_bd * const bdp)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i;
	uint32_t * const dp = bdp->word;
	const bus_size_t c = __arraycount(bdp->word);

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
285
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, (uintptr_t)bdp, 0);
ROOL's avatar
ROOL committed
286 287

	bus_space_read_region_4(sc->sc_bst, sc->sc_bsh_txdescs, o, dp, c);
Robert Sprowson's avatar
Robert Sprowson committed
288
	KERNHIST_LOG(cpswhist, "%08jx %08jx %08jx %08jx\n",
ROOL's avatar
ROOL committed
289 290 291 292 293 294 295 296 297 298 299 300
	    dp[0], dp[1], dp[2], dp[3]);
}

static inline void
cpsw_set_txdesc(struct cpsw_softc * const sc, const u_int i,
    struct cpsw_cpdma_bd * const bdp)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i;
	uint32_t * const dp = bdp->word;
	const bus_size_t c = __arraycount(bdp->word);

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
301 302
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, (uintptr_t)bdp, 0);
	KERNHIST_LOG(cpswhist, "%08jx %08jx %08jx %08jx\n",
ROOL's avatar
ROOL committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316
	    dp[0], dp[1], dp[2], dp[3]);

	bus_space_write_region_4(sc->sc_bst, sc->sc_bsh_txdescs, o, dp, c);
}

static inline void
cpsw_get_rxdesc(struct cpsw_softc * const sc, const u_int i,
    struct cpsw_cpdma_bd * const bdp)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i;
	uint32_t * const dp = bdp->word;
	const bus_size_t c = __arraycount(bdp->word);

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
317
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, (uintptr_t)bdp, 0);
ROOL's avatar
ROOL committed
318 319 320

	bus_space_read_region_4(sc->sc_bst, sc->sc_bsh_rxdescs, o, dp, c);

Robert Sprowson's avatar
Robert Sprowson committed
321
	KERNHIST_LOG(cpswhist, "%08jx %08jx %08jx %08jx\n",
ROOL's avatar
ROOL committed
322 323 324 325 326 327 328 329 330 331 332 333
	    dp[0], dp[1], dp[2], dp[3]);
}

static inline void
cpsw_set_rxdesc(struct cpsw_softc * const sc, const u_int i,
    struct cpsw_cpdma_bd * const bdp)
{
	const bus_size_t o = sizeof(struct cpsw_cpdma_bd) * i;
	uint32_t * const dp = bdp->word;
	const bus_size_t c = __arraycount(bdp->word);

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
334 335
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, i, (uintptr_t)bdp, 0);
	KERNHIST_LOG(cpswhist, "%08jx %08jx %08jx %08jx\n",
ROOL's avatar
ROOL committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	    dp[0], dp[1], dp[2], dp[3]);

	bus_space_write_region_4(sc->sc_bst, sc->sc_bsh_rxdescs, o, dp, c);
}

static inline bus_addr_t
cpsw_txdesc_paddr(struct cpsw_softc * const sc, u_int x)
{
	KASSERT(x < CPSW_NTXDESCS);
	return sc->sc_txdescs_pa + sizeof(struct cpsw_cpdma_bd) * x;
}

static inline bus_addr_t
cpsw_rxdesc_paddr(struct cpsw_softc * const sc, u_int x)
{
	KASSERT(x < CPSW_NRXDESCS);
	return sc->sc_rxdescs_pa + sizeof(struct cpsw_cpdma_bd) * x;
}


static int
cpsw_match(device_t parent, cfdata_t cf, void *aux)
{
	struct obio_attach_args * const oa = aux;
#ifdef TI_AM335X
	if (oa->obio_addr == 0x4a100000 && oa->obio_size >= 0x4000)
		return 1;
#endif
	return 0;
}

static bool
cpsw_phy_has_1000t(struct cpsw_softc * const sc)
{
#ifndef RISCOS
	struct ifmedia_entry *ifm;

	TAILQ_FOREACH(ifm, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_T)
			return true;
	}
#endif
	return false;
}

static int
cpsw_detach(device_t self, int flags)
{
	struct cpsw_softc * const sc = device_private(self);
	struct ifnet *ifp = &sc->sc_ec.ec_if;
	u_int i;

	/* Succeed now if there's no work to do. */
	if (!sc->sc_attached)
		return 0;

	sc->sc_attached = false;

	/* Stop the interface. Callouts are stopped in it. */
	cpsw_stop(ifp, 1);

	/* Destroy our callout. */
	callout_destroy(&sc->sc_tick_ch);

	/* Let go of the interrupts */
	intr_disestablish(sc->sc_rxthih);
	intr_disestablish(sc->sc_rxih);
	intr_disestablish(sc->sc_txih);
	intr_disestablish(sc->sc_miscih);

	/* Delete all media. */
	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);

	ether_ifdetach(ifp);
	if_detach(ifp);

	/* Free the packet padding buffer */
	kmem_free(sc->sc_txpad, ETHER_MIN_LEN);
	bus_dmamap_destroy(sc->sc_bdt, sc->sc_txpad_dm);

	/* Destroy all the descriptors */
	for (i = 0; i < CPSW_NTXDESCS; i++)
		bus_dmamap_destroy(sc->sc_bdt, sc->sc_rdp->tx_dm[i]);
	for (i = 0; i < CPSW_NRXDESCS; i++)
		bus_dmamap_destroy(sc->sc_bdt, sc->sc_rdp->rx_dm[i]);
	kmem_free(sc->sc_rdp, sizeof(*sc->sc_rdp));

	/* Unmap */
	bus_space_unmap(sc->sc_bst, sc->sc_bsh, sc->sc_bss);


	return 0;
}

static void
cpsw_attach(device_t parent, device_t self, void *aux)
{
	struct obio_attach_args * const oa = aux;
	struct cpsw_softc * const sc = device_private(self);
	prop_dictionary_t dict = device_properties(self);
	struct ethercom * const ec = &sc->sc_ec;
	struct ifnet * const ifp = &ec->ec_if;
	int error;
	u_int i;

	KERNHIST_INIT(cpswhist, 4096);

	sc->sc_dev = self;

Robert Sprowson's avatar
Robert Sprowson committed
445
	aprint_normal(": TI Layer 2 3-Port Switch\n");
ROOL's avatar
ROOL committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	aprint_naive("\n");

	callout_init(&sc->sc_tick_ch, 0);
	callout_setfunc(&sc->sc_tick_ch, cpsw_tick, sc);

#ifdef RISCOS
	/* Need one per MAC, so do this in cpsw_init() instead */
#else
	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
	if (eaprop == NULL) {
		/* grab mac_id0 from AM335x control module */
		uint32_t reg_lo, reg_hi;

		if (sitara_cm_reg_read_4(OMAP2SCM_MAC_ID0_LO, &reg_lo) == 0 &&
		    sitara_cm_reg_read_4(OMAP2SCM_MAC_ID0_HI, &reg_hi) == 0) {
			sc->sc_enaddr[0] = (reg_hi >>  0) & 0xff;
			sc->sc_enaddr[1] = (reg_hi >>  8) & 0xff;
			sc->sc_enaddr[2] = (reg_hi >> 16) & 0xff;
			sc->sc_enaddr[3] = (reg_hi >> 24) & 0xff;
			sc->sc_enaddr[4] = (reg_lo >>  0) & 0xff;
			sc->sc_enaddr[5] = (reg_lo >>  8) & 0xff;
		} else {
			aprint_error_dev(sc->sc_dev,
			    "using fake station address\n");
			/* 'N' happens to have the Local bit set */
			sc->sc_enaddr[0] = 'N';
			sc->sc_enaddr[1] = 'e';
			sc->sc_enaddr[2] = 't';
			sc->sc_enaddr[3] = 'B';
			sc->sc_enaddr[4] = 'S';
			sc->sc_enaddr[5] = 'D';
		}
	} else {
		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
		memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
		    ETHER_ADDR_LEN);
	}
#endif

	sc->sc_rxthih = intr_establish(oa->obio_intrbase + CPSW_INTROFF_RXTH,
	    IPL_VM, IST_LEVEL, cpsw_rxthintr, sc);
	sc->sc_rxih = intr_establish(oa->obio_intrbase + CPSW_INTROFF_RX,
	    IPL_VM, IST_LEVEL, cpsw_rxintr, sc);
	sc->sc_txih = intr_establish(oa->obio_intrbase + CPSW_INTROFF_TX,
	    IPL_VM, IST_LEVEL, cpsw_txintr, sc);
	sc->sc_miscih = intr_establish(oa->obio_intrbase + CPSW_INTROFF_MISC,
	    IPL_VM, IST_LEVEL, cpsw_miscintr, sc);

	sc->sc_bst = oa->obio_iot;
	sc->sc_bss = oa->obio_size;
	sc->sc_bdt = oa->obio_dmat;

	error = bus_space_map(sc->sc_bst, oa->obio_addr, oa->obio_size, 0,
	    &sc->sc_bsh);
	if (error) {
		aprint_error_dev(sc->sc_dev,
			"can't map registers: %d\n", error);
		return;
	}

	sc->sc_txdescs_pa = oa->obio_addr + CPSW_CPPI_RAM_TXDESCS_BASE;
	error = bus_space_subregion(sc->sc_bst, sc->sc_bsh,
	    CPSW_CPPI_RAM_TXDESCS_BASE, CPSW_CPPI_RAM_TXDESCS_SIZE,
	    &sc->sc_bsh_txdescs);
	if (error) {
		aprint_error_dev(sc->sc_dev,
			"can't subregion tx ring SRAM: %d\n", error);
		return;
	}
	aprint_debug_dev(sc->sc_dev, "txdescs at %p\n",
	    (void *)sc->sc_bsh_txdescs);

	sc->sc_rxdescs_pa = oa->obio_addr + CPSW_CPPI_RAM_RXDESCS_BASE;
	error = bus_space_subregion(sc->sc_bst, sc->sc_bsh,
	    CPSW_CPPI_RAM_RXDESCS_BASE, CPSW_CPPI_RAM_RXDESCS_SIZE,
	    &sc->sc_bsh_rxdescs);
	if (error) {
		aprint_error_dev(sc->sc_dev,
			"can't subregion rx ring SRAM: %d\n", error);
		return;
	}
	aprint_debug_dev(sc->sc_dev, "rxdescs at %p\n",
	    (void *)sc->sc_bsh_rxdescs);

	sc->sc_rdp = kmem_alloc(sizeof(*sc->sc_rdp), KM_SLEEP);

	for (i = 0; i < CPSW_NTXDESCS; i++) {
		if ((error = bus_dmamap_create(sc->sc_bdt, MCLBYTES,
		    CPSW_TXFRAGS, MCLBYTES, 0, 0,
		    &sc->sc_rdp->tx_dm[i])) != 0) {
			aprint_error_dev(sc->sc_dev,
			    "unable to create tx DMA map: %d\n", error);
		}
		sc->sc_rdp->tx_mb[i] = NULL;
	}

	for (i = 0; i < CPSW_NRXDESCS; i++) {
		if ((error = bus_dmamap_create(sc->sc_bdt, MCLBYTES, 1,
		    MCLBYTES, 0, 0, &sc->sc_rdp->rx_dm[i])) != 0) {
			aprint_error_dev(sc->sc_dev,
			    "unable to create rx DMA map: %d\n", error);
		}
		sc->sc_rdp->rx_mb[i] = NULL;
	}

	sc->sc_txpad = kmem_zalloc(ETHER_MIN_LEN, KM_SLEEP);
	bus_dmamap_create(sc->sc_bdt, ETHER_MIN_LEN, 1, ETHER_MIN_LEN, 0,
	    BUS_DMA_WAITOK, &sc->sc_txpad_dm);
	bus_dmamap_load(sc->sc_bdt, sc->sc_txpad_dm, sc->sc_txpad,
	    ETHER_MIN_LEN, NULL, BUS_DMA_WAITOK|BUS_DMA_WRITE);
	bus_dmamap_sync(sc->sc_bdt, sc->sc_txpad_dm, 0, ETHER_MIN_LEN,
	    BUS_DMASYNC_PREWRITE);

	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
	    ether_sprintf(sc->sc_enaddr));

	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
	ifp->if_softc = sc;
	ifp->if_capabilities = 0;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	ifp->if_start = cpsw_start;
	ifp->if_ioctl = cpsw_ioctl;
	ifp->if_init = cpsw_init;
	ifp->if_stop = cpsw_stop;
	ifp->if_watchdog = cpsw_watchdog;
	IFQ_SET_READY(&ifp->if_snd);

	cpsw_stop(ifp, 0);

	sc->sc_mii.mii_ifp = ifp;
	sc->sc_mii.mii_readreg = cpsw_mii_readreg;
	sc->sc_mii.mii_writereg = cpsw_mii_writereg;
	sc->sc_mii.mii_statchg = cpsw_mii_statchg;

	sc->sc_ec.ec_mii = &sc->sc_mii;
	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
	    ether_mediastatus);

	/* Initialize MDIO */
	cpsw_write_4(sc, MDIOCONTROL,
	    MDIOCTL_ENABLE | MDIOCTL_FAULTENB | MDIOCTL_CLKDIV(0xff));
	/* Clear ALE */
	cpsw_write_4(sc, CPSW_ALE_CONTROL, ALECTL_CLEAR_TABLE);

	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 0, 0);
	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
		aprint_error_dev(self, "no PHY found!\n");
		sc->sc_phy_has_1000t = false;
		ifmedia_add(&sc->sc_mii.mii_media,
		    IFM_ETHER|IFM_MANUAL, 0, NULL);
		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
	} else {
		sc->sc_phy_has_1000t = cpsw_phy_has_1000t(sc);
		if (sc->sc_phy_has_1000t) {
			aprint_normal_dev(sc->sc_dev, "1000baseT PHY found. "
			    "Setting RGMII Mode\n");
			/*
			 * Select the Interface RGMII Mode in the Control
			 * Module
			 */
			sitara_cm_reg_write_4(CPSW_GMII_SEL,
			    GMIISEL_GMII2_SEL(RGMII_MODE) |
			    GMIISEL_GMII1_SEL(RGMII_MODE));
		}

		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
	}

	if_attach(ifp);
Robert Sprowson's avatar
Robert Sprowson committed
616
	if_deferred_start_init(ifp, NULL);
ROOL's avatar
ROOL committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	ether_ifattach(ifp, sc->sc_enaddr);

	/* The attach is successful. */
	sc->sc_attached = true;

	return;
}

static void
cpsw_start(struct ifnet *ifp)
{
	struct cpsw_softc * const sc = ifp->if_softc;
	struct cpsw_ring_data * const rdp = sc->sc_rdp;
	struct cpsw_cpdma_bd bd;
	uint32_t * const dw = bd.word;
	struct mbuf *m;
	bus_dmamap_t dm;
	u_int eopi __diagused = ~0;
	u_int seg;
	u_int txfree;
	int txstart = -1;
	int error;
	bool pad;
	u_int mlen;

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
643
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, 0, 0, 0);
ROOL's avatar
ROOL committed
644 645 646 647 648 649 650 651 652 653 654

	if (__predict_false((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) !=
	    IFF_RUNNING)) {
		return;
	}

	if (sc->sc_txnext >= sc->sc_txhead)
		txfree = CPSW_NTXDESCS - 1 + sc->sc_txhead - sc->sc_txnext;
	else
		txfree = sc->sc_txhead - sc->sc_txnext - 1;

Robert Sprowson's avatar
Robert Sprowson committed
655
	KERNHIST_LOG(cpswhist, "start txf %jx txh %jx txn %jx txr %jx\n",
ROOL's avatar
ROOL committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	    txfree, sc->sc_txhead, sc->sc_txnext, sc->sc_txrun);

	while (txfree > 0) {
		IFQ_POLL(&ifp->if_snd, m);
		if (m == NULL)
			break;

		dm = rdp->tx_dm[sc->sc_txnext];

		error = bus_dmamap_load_mbuf(sc->sc_bdt, dm, m, BUS_DMA_NOWAIT);
#ifndef RISCOS
		if (error == EFBIG) {
			device_printf(sc->sc_dev, "won't fit\n");
			IFQ_DEQUEUE(&ifp->if_snd, m);
			m_freem(m);
			ifp->if_oerrors++;
			continue;
		} else if (error != 0) {
			device_printf(sc->sc_dev, "error\n");
			break;
		}
#else
		/* The decision to free is at DCI4 level */
		if (error != 0) return;
#endif

		if (dm->dm_nsegs + 1 >= txfree) {
			ifp->if_flags |= IFF_OACTIVE;
			bus_dmamap_unload(sc->sc_bdt, dm);
			break;
		}

		mlen = m_length(m);
		pad = mlen < CPSW_PAD_LEN;

		KASSERT(rdp->tx_mb[sc->sc_txnext] == NULL);
#ifndef RISCOS
		rdp->tx_mb[sc->sc_txnext] = m;
#else
		/* DCI4 requires the mbuf is freed almost immediately since we
		 * don't need the data any more. Abuse the tx_mb field instead to hold
		 * a reference that's more useful during transmit complete.
		 */
		rdp->tx_mb[sc->sc_txnext] = (struct mbuf *)ifp->if_sndunit;
#endif
		IFQ_DEQUEUE(&ifp->if_snd, m);

		bus_dmamap_sync(sc->sc_bdt, dm, 0, dm->dm_mapsize,
		    BUS_DMASYNC_PREWRITE);

		if (txstart == -1)
			txstart = sc->sc_txnext;
		eopi = sc->sc_txnext;
		for (seg = 0; seg < dm->dm_nsegs; seg++) {
			dw[0] = cpsw_txdesc_paddr(sc,
			    TXDESC_NEXT(sc->sc_txnext));
			dw[1] = dm->dm_segs[seg].ds_addr;
			dw[2] = dm->dm_segs[seg].ds_len;
#ifndef RISCOS
			dw[3] = 0;
#else
			dw[3] = CPDMA_BD_TO_PORT_EN | ((ifp->if_sndport + 1) << 16);
#endif

			if (seg == 0)
				dw[3] |= CPDMA_BD_SOP | CPDMA_BD_OWNER |
				    MAX(mlen, CPSW_PAD_LEN);

			if ((seg == dm->dm_nsegs - 1) && !pad)
				dw[3] |= CPDMA_BD_EOP;

			cpsw_set_txdesc(sc, sc->sc_txnext, &bd);
			txfree--;
			eopi = sc->sc_txnext;
			sc->sc_txnext = TXDESC_NEXT(sc->sc_txnext);
		}
		if (pad) {
			dw[0] = cpsw_txdesc_paddr(sc,
			    TXDESC_NEXT(sc->sc_txnext));
			dw[1] = sc->sc_txpad_pa;
			dw[2] = CPSW_PAD_LEN - mlen;
			dw[3] = CPDMA_BD_EOP;

			cpsw_set_txdesc(sc, sc->sc_txnext, &bd);
			txfree--;
			eopi = sc->sc_txnext;
			sc->sc_txnext = TXDESC_NEXT(sc->sc_txnext);
		}

Robert Sprowson's avatar
Robert Sprowson committed
745
		bpf_mtap(ifp, m, BPF_D_OUT);
ROOL's avatar
ROOL committed
746 747 748 749 750 751 752
	}

	if (txstart >= 0) {
		ifp->if_timer = 5;
		/* terminate the new chain */
		KASSERT(eopi == TXDESC_PREV(sc->sc_txnext));
		cpsw_set_txdesc_next(sc, TXDESC_PREV(sc->sc_txnext), 0);
Robert Sprowson's avatar
Robert Sprowson committed
753
		KERNHIST_LOG(cpswhist, "CP %jx HDP %jx s %jx e %jx\n",
ROOL's avatar
ROOL committed
754 755 756 757 758 759 760 761 762 763 764 765
		    cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0)),
		    cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(0)), txstart, eopi);
		/* link the new chain on */
		cpsw_set_txdesc_next(sc, TXDESC_PREV(txstart),
		    cpsw_txdesc_paddr(sc, txstart));
		if (sc->sc_txeoq) {
			/* kick the dma engine */
			sc->sc_txeoq = false;
			cpsw_write_4(sc, CPSW_CPDMA_TX_HDP(0),
			    cpsw_txdesc_paddr(sc, txstart));
		}
	}
Robert Sprowson's avatar
Robert Sprowson committed
766
	KERNHIST_LOG(cpswhist, "end txf %jx txh %jx txn %jx txr %jx\n",
ROOL's avatar
ROOL committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	    txfree, sc->sc_txhead, sc->sc_txnext, sc->sc_txrun);
}

static int
cpsw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	const int s = splnet();
	int error = 0;
#ifdef RISCOS
	struct cpsw_softc * const sc = ifp->if_softc;
	uint8_t  port;
	bool     promiscuous = FALSE, bcast = TRUE;
	u_int    i;
	uint16_t arg;
	uint32_t reg, ale_entry[3];

	switch (cmd) {
	case SIOCSETERRLVL:
		arg = *(uint16_t *)data;
		port = arg >> 8;
		ifp->mac[port].if_passerrors = (arg & 0xFF) != 0;
		reg = cpsw_read_4(sc, CPSW_SL_MACCONTROL(port));
		if (ifp->mac[port].if_passerrors)
			reg |= (SLMACCTL_RX_CSF_EN | SLMACCTL_RX_CEF_EN);
		else
			reg &= ~(SLMACCTL_RX_CSF_EN | SLMACCTL_RX_CEF_EN);
		cpsw_write_4(sc, CPSW_SL_MACCONTROL(port), reg);
		break;
	case SIOCSETPROMISC:
		arg = *(uint16_t *)data;
		port = arg >> 8;
		ifp->mac[port].if_matchmode = arg & 0xFF;
		switch (ifp->mac[port].if_matchmode) {
			case 0: bcast = FALSE; break; /* Specific */
			case 1:                       /* Specific + broadcast */
			case 2: bcast = TRUE;  break; /* Specific + multi + broadcast */
			case 3:                break; /* All */
		}
		/* Enable or disable broadcast (+multicast) in static entry 1 */
		cpsw_ale_read_entry(sc, 1, ale_entry);
		if (bcast)
			ale_entry[2] |= ((1 << port) << 2); /* ie. b66 = port 0 */ 
		else
			ale_entry[2] &= ~((1 << port) << 2);
		cpsw_ale_write_entry(sc, 1, ale_entry);
		/* Promiscuity is implemented by turning off the ALE for all */
		for (i = 0; i < CPSW_ETH_PORTS; i++) {
			promiscuous = promiscuous || (ifp->mac[i].if_matchmode == 3);
		}
		reg = cpsw_read_4(sc, CPSW_ALE_CONTROL);
		if (promiscuous)
			reg = reg | ALECTL_BYPASS;
		else
			reg = reg & ~ALECTL_BYPASS;
		cpsw_write_4(sc, CPSW_ALE_CONTROL, reg); 
		break;
#else

	switch (cmd) {
#endif
	default:
		error = ether_ioctl(ifp, cmd, data);
		if (error == ENETRESET) {
			error = 0;
		}
		break;
	}

	splx(s);

	return error;
}

static void
cpsw_watchdog(struct ifnet *ifp)
{
	struct cpsw_softc *sc = ifp->if_softc;

	device_printf(sc->sc_dev, "device timeout\n");

	ifp->if_oerrors++;
	cpsw_init(ifp);
	cpsw_start(ifp);
}

static int
cpsw_mii_wait(struct cpsw_softc * const sc, int reg)
{
	u_int tries;

Robert Sprowson's avatar
Robert Sprowson committed
857
	for (tries = 0; tries < 1000; tries++) {
ROOL's avatar
ROOL committed
858 859 860 861 862 863 864 865
		if ((cpsw_read_4(sc, reg) & __BIT(31)) == 0)
			return 0;
		delay(1);
	}
	return ETIMEDOUT;
}

static int
Robert Sprowson's avatar
Robert Sprowson committed
866
cpsw_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
ROOL's avatar
ROOL committed
867 868 869 870 871
{
	struct cpsw_softc * const sc = device_private(dev);
	uint32_t v;

	if (cpsw_mii_wait(sc, MDIOUSERACCESS0) != 0)
Robert Sprowson's avatar
Robert Sprowson committed
872
		return -1;
ROOL's avatar
ROOL committed
873 874 875 876 877

	cpsw_write_4(sc, MDIOUSERACCESS0, (1 << 31) |
	    ((reg & 0x1F) << 21) | ((phy & 0x1F) << 16));

	if (cpsw_mii_wait(sc, MDIOUSERACCESS0) != 0)
Robert Sprowson's avatar
Robert Sprowson committed
878
		return -1;
ROOL's avatar
ROOL committed
879 880

	v = cpsw_read_4(sc, MDIOUSERACCESS0);
Robert Sprowson's avatar
Robert Sprowson committed
881 882
	if (v & __BIT(29)) {
		*val = v & 0xffff;
ROOL's avatar
ROOL committed
883
		return 0;
Robert Sprowson's avatar
Robert Sprowson committed
884 885 886
	}

	return -1;
ROOL's avatar
ROOL committed
887 888
}

Robert Sprowson's avatar
Robert Sprowson committed
889 890
static int
cpsw_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
ROOL's avatar
ROOL committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
{
	struct cpsw_softc * const sc = device_private(dev);
	uint32_t v;

	KASSERT((val & 0xffff0000UL) == 0);

	if (cpsw_mii_wait(sc, MDIOUSERACCESS0) != 0)
		goto out;

	cpsw_write_4(sc, MDIOUSERACCESS0, (1 << 31) | (1 << 30) |
	    ((reg & 0x1F) << 21) | ((phy & 0x1F) << 16) | val);

	if (cpsw_mii_wait(sc, MDIOUSERACCESS0) != 0)
		goto out;

	v = cpsw_read_4(sc, MDIOUSERACCESS0);
Robert Sprowson's avatar
Robert Sprowson committed
907
	if ((v & __BIT(29)) == 0) {
ROOL's avatar
ROOL committed
908 909
out:
		device_printf(sc->sc_dev, "%s error\n", __func__);
Robert Sprowson's avatar
Robert Sprowson committed
910 911
		return -1;
	}
ROOL's avatar
ROOL committed
912

Robert Sprowson's avatar
Robert Sprowson committed
913
	return 0;
ROOL's avatar
ROOL committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
}

static void
cpsw_mii_statchg(struct ifnet *ifp)
{
	return;
}

static int
cpsw_new_rxbuf(struct cpsw_softc * const sc, const u_int i)
{
	struct cpsw_ring_data * const rdp = sc->sc_rdp;
	const u_int h = RXDESC_PREV(i);
	struct cpsw_cpdma_bd bd;
	uint32_t * const dw = bd.word;
	struct mbuf *m;
	int error = ENOBUFS;

#ifndef RISCOS
	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == NULL) {
		goto reuse;
	}

	MCLGET(m, M_DONTWAIT);
	if ((m->m_flags & M_EXT) == 0) {
		m_freem(m);
		goto reuse;
	}

	/* We have a new buffer, prepare it for the ring. */

	if (rdp->rx_mb[i] != NULL)
		bus_dmamap_unload(sc->sc_bdt, rdp->rx_dm[i]);

	m->m_len = m->m_pkthdr.len = MCLBYTES;

	rdp->rx_mb[i] = m;

	error = bus_dmamap_load_mbuf(sc->sc_bdt, rdp->rx_dm[i], rdp->rx_mb[i],
	    BUS_DMA_READ|BUS_DMA_NOWAIT);
	if (error) {
		device_printf(sc->sc_dev, "can't load rx DMA map %d: %d\n",
		    i, error);
	}
#else
	/* Copy to the DCI4 mbuf chain during cpsw_rxintr() */
	m = rdp->rx_mb[i] = NULL;
#endif

	bus_dmamap_sync(sc->sc_bdt, rdp->rx_dm[i],
	    0, rdp->rx_dm[i]->dm_mapsize, BUS_DMASYNC_PREREAD);

	error = 0;

reuse:
	/* (re-)setup the descriptor */
	dw[0] = 0;
	dw[1] = rdp->rx_dm[i]->dm_segs[0].ds_addr;
	dw[2] = MIN(0x7ff, rdp->rx_dm[i]->dm_segs[0].ds_len);
	dw[3] = CPDMA_BD_OWNER;

	cpsw_set_rxdesc(sc, i, &bd);
	/* and link onto ring */
	cpsw_set_rxdesc_next(sc, h, cpsw_rxdesc_paddr(sc, i));

	return error;
}

static int
cpsw_init(struct ifnet *ifp)
{
	struct cpsw_softc * const sc = ifp->if_softc;
	struct mii_data * const mii = &sc->sc_mii;
	int i;

	cpsw_stop(ifp, 0);

	sc->sc_txnext = 0;
	sc->sc_txhead = 0;

	/* Reset wrapper */
	cpsw_write_4(sc, CPSW_WR_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
997 998
	while (cpsw_read_4(sc, CPSW_WR_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
999 1000 1001

	/* Reset SS */
	cpsw_write_4(sc, CPSW_SS_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
1002 1003
	while (cpsw_read_4(sc, CPSW_SS_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

	/* Clear table and enable ALE */
	cpsw_write_4(sc, CPSW_ALE_CONTROL,
	    ALECTL_ENABLE_ALE | ALECTL_CLEAR_TABLE);

	/* Reset and init Sliver port 1 and 2 */
	for (i = 0; i < CPSW_ETH_PORTS; i++) {
		uint32_t macctl;

		/* Reset */
		cpsw_write_4(sc, CPSW_SL_SOFT_RESET(i), 1);
Robert Sprowson's avatar
Robert Sprowson committed
1015 1016
		while (cpsw_read_4(sc, CPSW_SL_SOFT_RESET(i)) & 1)
			;
ROOL's avatar
ROOL committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
		/* Set Slave Mapping */
		cpsw_write_4(sc, CPSW_SL_RX_PRI_MAP(i), 0x76543210);
		cpsw_write_4(sc, CPSW_PORT_P_TX_PRI_MAP(i+1), 0x33221100);
		cpsw_write_4(sc, CPSW_SL_RX_MAXLEN(i), 0x5f2);
		/* Set MAC Address */
#ifndef RISCOS
		cpsw_write_4(sc, CPSW_PORT_P_SA_HI(i+1),
		    sc->sc_enaddr[0] | (sc->sc_enaddr[1] << 8) |
		    (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[3] << 24));
		cpsw_write_4(sc, CPSW_PORT_P_SA_LO(i+1),
		    sc->sc_enaddr[4] | (sc->sc_enaddr[5] << 8));
#else
		cpsw_write_4(sc, CPSW_PORT_P_SA_HI(i+1),
		    ifp->mac[i].enaddr[0] | (ifp->mac[i].enaddr[1] << 8) |
		    (ifp->mac[i].enaddr[2] << 16) | (ifp->mac[i].enaddr[3] << 24));
		cpsw_write_4(sc, CPSW_PORT_P_SA_LO(i+1),
		    ifp->mac[i].enaddr[4] | (ifp->mac[i].enaddr[5] << 8));
#endif

		/* Set MACCONTROL for ports 0,1 */
		macctl = SLMACCTL_FULLDUPLEX | SLMACCTL_GMII_EN |
#ifdef RISCOS
		    SLMACCTL_EXT_EN /* Get from in-band status over RGMII */ |     
#endif
		    SLMACCTL_IFCTL_A;
		if (sc->sc_phy_has_1000t)
			macctl |= SLMACCTL_GIG;
		cpsw_write_4(sc, CPSW_SL_MACCONTROL(i), macctl);

		/* Set ALE port to forwarding(3) */
		cpsw_write_4(sc, CPSW_ALE_PORTCTL(i+1), 3);
	}

	/* Set Host Port Mapping */
	cpsw_write_4(sc, CPSW_PORT_P0_CPDMA_TX_PRI_MAP, 0x76543210);
	cpsw_write_4(sc, CPSW_PORT_P0_CPDMA_RX_CH_MAP, 0);

	/* Set ALE port to forwarding(3) */
	cpsw_write_4(sc, CPSW_ALE_PORTCTL(0), 3);

	/* Initialize addrs */
	cpsw_ale_update_addresses(sc, 1);

	cpsw_write_4(sc, CPSW_SS_PTYPE, 0);
	cpsw_write_4(sc, CPSW_SS_STAT_PORT_EN, 7);

	cpsw_write_4(sc, CPSW_CPDMA_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
1064 1065
	while (cpsw_read_4(sc, CPSW_CPDMA_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

	for (i = 0; i < 8; i++) {
		cpsw_write_4(sc, CPSW_CPDMA_TX_HDP(i), 0);
		cpsw_write_4(sc, CPSW_CPDMA_RX_HDP(i), 0);
		cpsw_write_4(sc, CPSW_CPDMA_TX_CP(i), 0);
		cpsw_write_4(sc, CPSW_CPDMA_RX_CP(i), 0);
	}

	bus_space_set_region_4(sc->sc_bst, sc->sc_bsh_txdescs, 0, 0,
	    CPSW_CPPI_RAM_TXDESCS_SIZE/4);

	sc->sc_txhead = 0;
	sc->sc_txnext = 0;

	cpsw_write_4(sc, CPSW_CPDMA_RX_FREEBUFFER(0), 0);

	bus_space_set_region_4(sc->sc_bst, sc->sc_bsh_rxdescs, 0, 0,
	    CPSW_CPPI_RAM_RXDESCS_SIZE/4);
	/* Initialize RX Buffer Descriptors */
	cpsw_set_rxdesc_next(sc, RXDESC_PREV(0), 0);
	for (i = 0; i < CPSW_NRXDESCS; i++) {
		cpsw_new_rxbuf(sc, i);
	}
	sc->sc_rxhead = 0;

1091
#ifndef RISCOS
Robert Sprowson's avatar
Robert Sprowson committed
1092 1093
	/* turn off flow control */
	cpsw_write_4(sc, CPSW_SS_FLOW_CONTROL, 0);
1094
#endif
Robert Sprowson's avatar
Robert Sprowson committed
1095

ROOL's avatar
ROOL committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	/* align layer 3 header to 32-bit */
	cpsw_write_4(sc, CPSW_CPDMA_RX_BUFFER_OFFSET, ETHER_ALIGN);

	/* Clear all interrupt Masks */
	cpsw_write_4(sc, CPSW_CPDMA_RX_INTMASK_CLEAR, 0xFFFFFFFF);
	cpsw_write_4(sc, CPSW_CPDMA_TX_INTMASK_CLEAR, 0xFFFFFFFF);

	/* Enable TX & RX DMA */
	cpsw_write_4(sc, CPSW_CPDMA_TX_CONTROL, 1);
	cpsw_write_4(sc, CPSW_CPDMA_RX_CONTROL, 1);

	/* Enable TX and RX interrupt receive for core 0 */
	cpsw_write_4(sc, CPSW_WR_C_TX_EN(0), 1);
	cpsw_write_4(sc, CPSW_WR_C_RX_EN(0), 1);
	cpsw_write_4(sc, CPSW_WR_C_MISC_EN(0), 0x1F);

	/* Enable host Error Interrupt */
	cpsw_write_4(sc, CPSW_CPDMA_DMA_INTMASK_SET, 2);

	/* Enable interrupts for TX and RX Channel 0 */
	cpsw_write_4(sc, CPSW_CPDMA_TX_INTMASK_SET, 1);
	cpsw_write_4(sc, CPSW_CPDMA_RX_INTMASK_SET, 1);

	/* Ack stalled irqs */
	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_RXTH);
	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_RX);
	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_TX);
	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_MISC);

#ifndef RISCOS
	/* Initialize MDIO - ENABLE, PREAMBLE=0, FAULTENB, CLKDIV=0xFF */
	/* TODO Calculate MDCLK=CLK/(CLKDIV+1) */
	cpsw_write_4(sc, MDIOCONTROL,
	    MDIOCTL_ENABLE | MDIOCTL_FAULTENB | MDIOCTL_CLKDIV(0xff));
#else
	/* Initialize MDIO - ENABLE, PREAMBLE=0, FAULTENB, CLKDIV=100 (for 1.25MHz) */
	cpsw_write_4(sc, MDIOCONTROL,
	    MDIOCTL_ENABLE | MDIOCTL_FAULTENB | MDIOCTL_CLKDIV(100 - 1));
#endif

	mii_mediachg(mii);

	/* Write channel 0 RX HDP */
	cpsw_write_4(sc, CPSW_CPDMA_RX_HDP(0), cpsw_rxdesc_paddr(sc, 0));
	sc->sc_rxrun = true;
	sc->sc_rxeoq = false;

	sc->sc_txrun = true;
	sc->sc_txeoq = true;
	callout_schedule(&sc->sc_tick_ch, hz);
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;

	return 0;
}

static void
cpsw_stop(struct ifnet *ifp, int disable)
{
	struct cpsw_softc * const sc = ifp->if_softc;
	struct cpsw_ring_data * const rdp = sc->sc_rdp;
	u_int i;

	aprint_debug_dev(sc->sc_dev, "%s: ifp %p disable %d\n", __func__,
	    ifp, disable);

	if ((ifp->if_flags & IFF_RUNNING) == 0)
		return;

	callout_stop(&sc->sc_tick_ch);
	mii_down(&sc->sc_mii);

	cpsw_write_4(sc, CPSW_CPDMA_TX_INTMASK_CLEAR, 1);
	cpsw_write_4(sc, CPSW_CPDMA_RX_INTMASK_CLEAR, 1);
	cpsw_write_4(sc, CPSW_WR_C_TX_EN(0), 0x0);
	cpsw_write_4(sc, CPSW_WR_C_RX_EN(0), 0x0);
	cpsw_write_4(sc, CPSW_WR_C_MISC_EN(0), 0x0);

	cpsw_write_4(sc, CPSW_CPDMA_TX_TEARDOWN, 0);
	cpsw_write_4(sc, CPSW_CPDMA_RX_TEARDOWN, 0);
	i = 0;
	while ((sc->sc_txrun || sc->sc_rxrun) && i < 10000) {
		delay(10);
		if ((sc->sc_txrun == true) && cpsw_txintr(sc) == 0)
			sc->sc_txrun = false;
		if ((sc->sc_rxrun == true) && cpsw_rxintr(sc) == 0)
			sc->sc_rxrun = false;
		i++;
	}
	//printf("%s toredown complete in %u\n", __func__, i);

	/* Reset wrapper */
	cpsw_write_4(sc, CPSW_WR_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
1189 1190
	while (cpsw_read_4(sc, CPSW_WR_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
1191 1192 1193

	/* Reset SS */
	cpsw_write_4(sc, CPSW_SS_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
1194 1195
	while (cpsw_read_4(sc, CPSW_SS_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
1196 1197 1198

	for (i = 0; i < CPSW_ETH_PORTS; i++) {
		cpsw_write_4(sc, CPSW_SL_SOFT_RESET(i), 1);
Robert Sprowson's avatar
Robert Sprowson committed
1199 1200
		while (cpsw_read_4(sc, CPSW_SL_SOFT_RESET(i)) & 1)
			;
ROOL's avatar
ROOL committed
1201 1202 1203 1204
	}

	/* Reset CPDMA */
	cpsw_write_4(sc, CPSW_CPDMA_SOFT_RESET, 1);
Robert Sprowson's avatar
Robert Sprowson committed
1205 1206
	while (cpsw_read_4(sc, CPSW_CPDMA_SOFT_RESET) & 1)
		;
ROOL's avatar
ROOL committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	/* Release any queued transmit buffers. */
	for (i = 0; i < CPSW_NTXDESCS; i++) {
		bus_dmamap_unload(sc->sc_bdt, rdp->tx_dm[i]);
#ifndef RISCOS
		m_freem(rdp->tx_mb[i]);
#endif
		rdp->tx_mb[i] = NULL;
	}

	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
	ifp->if_timer = 0;

	if (!disable)
		return;

	for (i = 0; i < CPSW_NRXDESCS; i++) {
		bus_dmamap_unload(sc->sc_bdt, rdp->rx_dm[i]);
#ifndef RISCOS
		m_freem(rdp->rx_mb[i]);
#endif
		rdp->rx_mb[i] = NULL;
	}
}

static void
cpsw_tick(void *arg)
{
	struct cpsw_softc * const sc = arg;
	struct mii_data * const mii = &sc->sc_mii;
	const int s = splnet();

	mii_tick(mii);

	splx(s);

	callout_schedule(&sc->sc_tick_ch, hz);
}

static int
cpsw_rxthintr(void *arg)
{
	struct cpsw_softc * const sc = arg;

	/* this won't deassert the interrupt though */
	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_RXTH);

	return 1;
}

static int
cpsw_rxintr(void *arg)
{
	struct cpsw_softc * const sc = arg;
	struct ifnet * const ifp = &sc->sc_ec.ec_if;
	struct cpsw_ring_data * const rdp = sc->sc_rdp;
	struct cpsw_cpdma_bd bd;
	const uint32_t * const dw = bd.word;
	bus_dmamap_t dm;
	struct mbuf *m;
#ifdef RISCOS
	struct mbuf *top;
	uint8_t port, status;
#endif
	u_int i;
	u_int len, off;

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
1275
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, 0, 0, 0);
ROOL's avatar
ROOL committed
1276 1277 1278 1279 1280

	for (;;) {
		KASSERT(sc->sc_rxhead < CPSW_NRXDESCS);

		i = sc->sc_rxhead;
Robert Sprowson's avatar
Robert Sprowson committed
1281
		KERNHIST_LOG(cpswhist, "rxhead %jx CP %jx\n", i,
ROOL's avatar
ROOL committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		    cpsw_read_4(sc, CPSW_CPDMA_RX_CP(0)), 0, 0);
		dm = rdp->rx_dm[i];
		m = rdp->rx_mb[i];

		KASSERT(dm != NULL);
		KASSERT(m != NULL);

		cpsw_get_rxdesc(sc, i, &bd);

		if (ISSET(dw[3], CPDMA_BD_OWNER))
			break;

		if (ISSET(dw[3], CPDMA_BD_TDOWNCMPLT)) {
			sc->sc_rxrun = false;
			return 1;
		}
#ifdef RISCOS
		port   = __SHIFTOUT(dw[3], (uint32_t)__BITS(18, 16));
		status = __SHIFTOUT(dw[3], (uint32_t)__BITS(25, 20));
#endif

		if ((dw[3] & (CPDMA_BD_SOP|CPDMA_BD_EOP)) !=
		    (CPDMA_BD_SOP|CPDMA_BD_EOP)) {
			//Debugger();
		}

		bus_dmamap_sync(sc->sc_bdt, dm, 0, dm->dm_mapsize,
		    BUS_DMASYNC_POSTREAD);

		if (cpsw_new_rxbuf(sc, i) != 0) {
			/* drop current packet, reuse buffer for new */
			ifp->if_ierrors++;
#ifdef RISCOS
			glue_inc_rxerrors(port);
#endif
			goto next;
		}

		off = __SHIFTOUT(dw[2], (uint32_t)__BITS(26, 16));
		len = __SHIFTOUT(dw[3], (uint32_t)__BITS(10,  0));

		if (ISSET(dw[3], CPDMA_BD_PASSCRC))
			len -= ETHER_CRC_LEN;

#ifndef RISCOS
Robert Sprowson's avatar
Robert Sprowson committed
1327
		m_set_rcvif(m, ifp);
ROOL's avatar
ROOL committed
1328 1329 1330
		m->m_pkthdr.len = m->m_len = len;
		m->m_data += off;

Robert Sprowson's avatar
Robert Sprowson committed
1331
		if_percpuq_enqueue(ifp->if_percpuq, m);
ROOL's avatar
ROOL committed
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
#else
		/* Get a single for the header so it's easier to ditch later */
		top = ALLOC_S(ETHER_HDR_LEN, NULL);
		m = ALLOC(len - ETHER_HDR_LEN, NULL);
		if ((top == NULL) || (m == NULL)) {
			/* Exhaustion */
			glue_inc_rxerrors(port);
			m_freem(m);
			m_free(top);
			goto next;
		}
		top = CAT(top, m);

		/* Use the private fields to pass back the length, port & error status */
		top->m_pkthdr.len = len - ETHER_HDR_LEN;
		top->m_pkthdr.rcvif = (struct ifnet *)(status | (port << 8));

		/* Copy data from the uncached memory into the mbufs */
		IMPORT(top, len, (void *)(off + (uintptr_t)dm->dm_segs[0].ds_logical));
		m = top;

Robert Sprowson's avatar
Robert Sprowson committed
1353 1354 1355
		/* Hand off to filtering */
		glue_receive(ifp, m);
#endif
ROOL's avatar
ROOL committed
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

next:
		sc->sc_rxhead = RXDESC_NEXT(sc->sc_rxhead);
		if (ISSET(dw[3], CPDMA_BD_EOQ)) {
			sc->sc_rxeoq = true;
			break;
		} else {
			sc->sc_rxeoq = false;
		}
		cpsw_write_4(sc, CPSW_CPDMA_RX_CP(0),
		    cpsw_rxdesc_paddr(sc, i));
	}

	if (sc->sc_rxeoq) {
		device_printf(sc->sc_dev, "rxeoq\n");
		//Debugger();
	}

	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_RX);

	return 1;
}

static int
cpsw_txintr(void *arg)
{
	struct cpsw_softc * const sc = arg;
	struct ifnet * const ifp = &sc->sc_ec.ec_if;
	struct cpsw_ring_data * const rdp = sc->sc_rdp;
	struct cpsw_cpdma_bd bd;
	const uint32_t * const dw = bd.word;
	bool handled = false;
	uint32_t tx0_cp;
	u_int cpi;

	KERNHIST_FUNC(__func__);
Robert Sprowson's avatar
Robert Sprowson committed
1392
	KERNHIST_CALLED_5(cpswhist, (uintptr_t)sc, 0, 0, 0);
ROOL's avatar
ROOL committed
1393 1394 1395

	KASSERT(sc->sc_txrun);

Robert Sprowson's avatar
Robert Sprowson committed
1396
	KERNHIST_LOG(cpswhist, "before txnext %jx txhead %jx txrun %jx\n",
ROOL's avatar
ROOL committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	    sc->sc_txnext, sc->sc_txhead, sc->sc_txrun, 0);

	tx0_cp = cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0));

	if (tx0_cp == 0xfffffffc) {
		/* Teardown, ack it */
		cpsw_write_4(sc, CPSW_CPDMA_TX_CP(0), 0xfffffffc);
		cpsw_write_4(sc, CPSW_CPDMA_TX_HDP(0), 0);
		sc->sc_txrun = false;
		return 0;
	}

	for (;;) {
		tx0_cp = cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0));
		cpi = (tx0_cp - sc->sc_txdescs_pa) / sizeof(struct cpsw_cpdma_bd);
		KASSERT(sc->sc_txhead < CPSW_NTXDESCS);

Robert Sprowson's avatar
Robert Sprowson committed
1414 1415
		KERNHIST_LOG(cpswhist, "txnext %jx txhead %jx txrun %jx "
		    "cpi %jx\n",
ROOL's avatar
ROOL committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
		    sc->sc_txnext, sc->sc_txhead, sc->sc_txrun, cpi);

		cpsw_get_txdesc(sc, sc->sc_txhead, &bd);

		if (dw[2] == 0) {
			//Debugger();
		}

		if (ISSET(dw[3], CPDMA_BD_SOP) == 0)
			goto next;

		if (ISSET(dw[3], CPDMA_BD_OWNER)) {
			printf("pwned %x %x %x\n", cpi, sc->sc_txhead,
			    sc->sc_txnext);
			break;
		}

		if (ISSET(dw[3], CPDMA_BD_TDOWNCMPLT)) {
			sc->sc_txrun = false;
			return 1;
		}

		bus_dmamap_sync(sc->sc_bdt, rdp->tx_dm[sc->sc_txhead],
		    0, rdp->tx_dm[sc->sc_txhead]->dm_mapsize,
		    BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(sc->sc_bdt, rdp->tx_dm[sc->sc_txhead]);

#ifndef RISCOS
		m_freem(rdp->tx_mb[sc->sc_txhead]);
#else
		/* Retrieve the unit number stashed in the tx_mb member and update
		 * the stats. The mbuf was free'd once exported in cpsw_start().
		 */
		glue_transmitted((uint8_t)(uintptr_t)rdp->tx_mb[sc->sc_txhead], dw[2] & 0xFFFF, TRUE);
#endif
		rdp->tx_mb[sc->sc_txhead] = NULL;

		ifp->if_opackets++;

		handled = true;

		ifp->if_flags &= ~IFF_OACTIVE;

next:
		if (ISSET(dw[3], CPDMA_BD_EOP) && ISSET(dw[3], CPDMA_BD_EOQ)) {
			sc->sc_txeoq = true;
		}
		if (sc->sc_txhead == cpi) {
			cpsw_write_4(sc, CPSW_CPDMA_TX_CP(0),
			    cpsw_txdesc_paddr(sc, cpi));
			sc->sc_txhead = TXDESC_NEXT(sc->sc_txhead);
			break;
		}
		sc->sc_txhead = TXDESC_NEXT(sc->sc_txhead);
		if (ISSET(dw[3], CPDMA_BD_EOP) && ISSET(dw[3], CPDMA_BD_EOQ)) {
			sc->sc_txeoq = true;
			break;
		}
	}

	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_TX);

	if ((sc->sc_txnext != sc->sc_txhead) && sc->sc_txeoq) {
		if (cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(0)) == 0) {
			sc->sc_txeoq = false;
			cpsw_write_4(sc, CPSW_CPDMA_TX_HDP(0),
			    cpsw_txdesc_paddr(sc, sc->sc_txhead));
		}
	}

Robert Sprowson's avatar
Robert Sprowson committed
1486
	KERNHIST_LOG(cpswhist, "after txnext %jx txhead %jx txrun %jx\n",
ROOL's avatar
ROOL committed
1487
	    sc->sc_txnext, sc->sc_txhead, sc->sc_txrun, 0);
Robert Sprowson's avatar
Robert Sprowson committed
1488
	KERNHIST_LOG(cpswhist, "CP %jx HDP %jx\n",
ROOL's avatar
ROOL committed
1489 1490 1491 1492 1493 1494 1495
	    cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0)),
	    cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(0)), 0, 0);

	if (handled && sc->sc_txnext == sc->sc_txhead)
		ifp->if_timer = 0;

	if (handled)
Robert Sprowson's avatar
Robert Sprowson committed
1496 1497 1498
#ifndef RISCOS
		if_schedule_deferred_start(ifp);
#else
ROOL's avatar
ROOL committed
1499
		cpsw_start(ifp);
Robert Sprowson's avatar
Robert Sprowson committed
1500
#endif
ROOL's avatar
ROOL committed
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756

	return handled;
}

static int
cpsw_miscintr(void *arg)
{
	struct cpsw_softc * const sc = arg;
	uint32_t miscstat;
	uint32_t dmastat;
	uint32_t stat;

	miscstat = cpsw_read_4(sc, CPSW_WR_C_MISC_STAT(0));
	device_printf(sc->sc_dev, "%s %x FIRE\n", __func__, miscstat);

#define CPSW_MISC_HOST_PEND __BIT32(2)
#define CPSW_MISC_STAT_PEND __BIT32(3)

	if (ISSET(miscstat, CPSW_MISC_HOST_PEND)) {
		/* Host Error */
		dmastat = cpsw_read_4(sc, CPSW_CPDMA_DMA_INTSTAT_MASKED);
		printf("CPSW_CPDMA_DMA_INTSTAT_MASKED %x\n", dmastat);

		printf("rxhead %02x\n", sc->sc_rxhead);

		stat = cpsw_read_4(sc, CPSW_CPDMA_DMASTATUS);
		printf("CPSW_CPDMA_DMASTATUS %x\n", stat);
		stat = cpsw_read_4(sc, CPSW_CPDMA_TX_HDP(0));
		printf("CPSW_CPDMA_TX0_HDP %x\n", stat);
		stat = cpsw_read_4(sc, CPSW_CPDMA_TX_CP(0));
		printf("CPSW_CPDMA_TX0_CP %x\n", stat);
		stat = cpsw_read_4(sc, CPSW_CPDMA_RX_HDP(0));
		printf("CPSW_CPDMA_RX0_HDP %x\n", stat);
		stat = cpsw_read_4(sc, CPSW_CPDMA_RX_CP(0));
		printf("CPSW_CPDMA_RX0_CP %x\n", stat);

		//Debugger();

		cpsw_write_4(sc, CPSW_CPDMA_DMA_INTMASK_CLEAR, dmastat);
		dmastat = cpsw_read_4(sc, CPSW_CPDMA_DMA_INTSTAT_MASKED);
		printf("CPSW_CPDMA_DMA_INTSTAT_MASKED %x\n", dmastat);
	}

	cpsw_write_4(sc, CPSW_CPDMA_CPDMA_EOI_VECTOR, CPSW_INTROFF_MISC);

	return 1;
}

/*
 *
 * ALE support routines.
 *
 */

static void
cpsw_ale_entry_init(uint32_t *ale_entry)
{
	ale_entry[0] = ale_entry[1] = ale_entry[2] = 0;
}

static void
cpsw_ale_entry_set_mac(uint32_t *ale_entry, const uint8_t *mac)
{
	ale_entry[0] = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
	ale_entry[1] = mac[0] << 8 | mac[1];
}

static void
cpsw_ale_entry_set_bcast_mac(uint32_t *ale_entry)
{
	ale_entry[0] = 0xffffffff;
	ale_entry[1] = 0x0000ffff;
}

static void
cpsw_ale_entry_set(uint32_t *ale_entry, ale_entry_field_t field, uint32_t val)
{
	/* Entry type[61:60] is addr entry(1), Mcast fwd state[63:62] is fw(3)*/
	switch (field) {
	case ALE_ENTRY_TYPE:
		/* [61:60] */
		ale_entry[1] |= (val & 0x3) << 28;
		break;
	case ALE_MCAST_FWD_STATE:
		/* [63:62] */
		ale_entry[1] |= (val & 0x3) << 30;
		break;
	case ALE_PORT_MASK:
		/* [68:66] */
		ale_entry[2] |= (val & 0x7) << 2;
		break;
	case ALE_PORT_NUMBER:
		/* [67:66] */
		ale_entry[2] |= (val & 0x3) << 2;
		break;
	default:
		panic("Invalid ALE entry field: %d\n", field);
	}

	return;
}

static bool
cpsw_ale_entry_mac_match(const uint32_t *ale_entry, const uint8_t *mac)
{
	return (((ale_entry[1] >> 8) & 0xff) == mac[0]) &&
	    (((ale_entry[1] >> 0) & 0xff) == mac[1]) &&
	    (((ale_entry[0] >>24) & 0xff) == mac[2]) &&
	    (((ale_entry[0] >>16) & 0xff) == mac[3]) &&
	    (((ale_entry[0] >> 8) & 0xff) == mac[4]) &&
	    (((ale_entry[0] >> 0) & 0xff) == mac[5]);
}

static void
cpsw_ale_set_outgoing_mac(struct cpsw_softc *sc, int port, const uint8_t *mac)
{
	cpsw_write_4(sc, CPSW_PORT_P_SA_HI(port),
	    mac[3] << 24 | mac[2] << 16 | mac[1] << 8 | mac[0]);
	cpsw_write_4(sc, CPSW_PORT_P_SA_LO(port),
	    mac[5] << 8 | mac[4]);
}

static void
cpsw_ale_read_entry(struct cpsw_softc *sc, uint16_t idx, uint32_t *ale_entry)
{
	cpsw_write_4(sc, CPSW_ALE_TBLCTL, idx & 1023);
	ale_entry[0] = cpsw_read_4(sc, CPSW_ALE_TBLW0);
	ale_entry[1] = cpsw_read_4(sc, CPSW_ALE_TBLW1);
	ale_entry[2] = cpsw_read_4(sc, CPSW_ALE_TBLW2);
}

static void
cpsw_ale_write_entry(struct cpsw_softc *sc, uint16_t idx,
    const uint32_t *ale_entry)
{
	cpsw_write_4(sc, CPSW_ALE_TBLW0, ale_entry[0]);
	cpsw_write_4(sc, CPSW_ALE_TBLW1, ale_entry[1]);
	cpsw_write_4(sc, CPSW_ALE_TBLW2, ale_entry[2]);
	cpsw_write_4(sc, CPSW_ALE_TBLCTL, 1 << 31 | (idx & 1023));
}

static int
cpsw_ale_remove_all_mc_entries(struct cpsw_softc *sc)
{
	int i;
	uint32_t ale_entry[3];

	/* First two entries are link address and broadcast. */
	for (i = 2; i < CPSW_MAX_ALE_ENTRIES; i++) {
		cpsw_ale_read_entry(sc, i, ale_entry);
		if (((ale_entry[1] >> 28) & 3) == 1 && /* Address entry */
		    ((ale_entry[1] >> 8) & 1) == 1) { /* MCast link addr */
			ale_entry[0] = ale_entry[1] = ale_entry[2] = 0;
			cpsw_ale_write_entry(sc, i, ale_entry);
		}
	}
	return CPSW_MAX_ALE_ENTRIES;
}

static int
cpsw_ale_mc_entry_set(struct cpsw_softc *sc, uint8_t portmask, uint8_t *mac)
{
	int free_index = -1, matching_index = -1, i;
	uint32_t ale_entry[3];

	/* Find a matching entry or a free entry. */
	for (i = 0; i < CPSW_MAX_ALE_ENTRIES; i++) {
		cpsw_ale_read_entry(sc, i, ale_entry);

		/* Entry Type[61:60] is 0 for free entry */
		if (free_index < 0 && ((ale_entry[1] >> 28) & 3) == 0) {
			free_index = i;
		}

		if (cpsw_ale_entry_mac_match(ale_entry, mac)) {
			matching_index = i;
			break;
		}
	}

	if (matching_index < 0) {
		if (free_index < 0)
			return ENOMEM;
		i = free_index;
	}

	cpsw_ale_entry_init(ale_entry);

	cpsw_ale_entry_set_mac(ale_entry, mac);
	cpsw_ale_entry_set(ale_entry, ALE_ENTRY_TYPE, ALE_TYPE_ADDRESS);
	cpsw_ale_entry_set(ale_entry, ALE_MCAST_FWD_STATE, ALE_FWSTATE_FWONLY);
	cpsw_ale_entry_set(ale_entry, ALE_PORT_MASK, portmask);

	cpsw_ale_write_entry(sc, i, ale_entry);

	return 0;
}

static int
cpsw_ale_update_addresses(struct cpsw_softc *sc, int purge)
{
	uint8_t *mac = sc->sc_enaddr;
	uint32_t ale_entry[3];
	int i;
	struct ethercom * const ec = &sc->sc_ec;
	struct ether_multi *ifma;

#ifdef RISCOS
	/* Put MAC 1 in table entry 2 */
	cpsw_ale_entry_init(ale_entry);
	cpsw_ale_entry_set_mac(ale_entry, ec->ec_if.mac[1].enaddr);
	cpsw_ale_entry_set(ale_entry, ALE_ENTRY_TYPE, ALE_TYPE_ADDRESS);
	cpsw_ale_entry_set(ale_entry, ALE_PORT_NUMBER, 0);
	cpsw_ale_write_entry(sc, 2, ale_entry);

	/* Fall through with MAC 0 */
	mac = ec->ec_if.mac[0].enaddr;
#endif
	cpsw_ale_entry_init(ale_entry);
	/* Route incoming packets for our MAC address to Port 0 (host). */
	/* For simplicity, keep this entry at table index 0 in the ALE. */
	cpsw_ale_entry_set_mac(ale_entry, mac);
	cpsw_ale_entry_set(ale_entry, ALE_ENTRY_TYPE, ALE_TYPE_ADDRESS);
	cpsw_ale_entry_set(ale_entry, ALE_PORT_NUMBER, 0);
	cpsw_ale_write_entry(sc, 0, ale_entry);

	/* Set outgoing MAC Address for Ports 1 and 2. */
	for (i = CPSW_CPPI_PORTS; i < (CPSW_ETH_PORTS + CPSW_CPPI_PORTS); ++i)
#ifndef RISCOS
		cpsw_ale_set_outgoing_mac(sc, i, mac);
#else
		cpsw_ale_set_outgoing_mac(sc, i, ec->ec_if.mac[i - 1].enaddr);
#endif

	/* Keep the broadcast address at table entry 1. */
	cpsw_ale_entry_init(ale_entry);
	cpsw_ale_entry_set_bcast_mac(ale_entry);
	cpsw_ale_entry_set(ale_entry, ALE_ENTRY_TYPE, ALE_TYPE_ADDRESS);
	cpsw_ale_entry_set(ale_entry, ALE_MCAST_FWD_STATE, ALE_FWSTATE_FWONLY);
	cpsw_ale_entry_set(ale_entry, ALE_PORT_MASK, ALE_PORT_MASK_ALL);
	cpsw_ale_write_entry(sc, 1, ale_entry);

	/* SIOCDELMULTI doesn't specify the particular address
	   being removed, so we have to remove all and rebuild. */
	if (purge)
		cpsw_ale_remove_all_mc_entries(sc);

#ifndef RISCOS
	/* Set other multicast addrs desired. */
	LIST_FOREACH(ifma, &ec->ec_multiaddrs, enm_list) {
		cpsw_ale_mc_entry_set(sc, ALE_PORT_MASK_ALL, ifma->enm_addrlo);
	}
#endif

	return 0;
}