/*	$NetBSD: if_le.c,v 1.7 2008/04/04 12:25:06 tsutsui Exp $	*/

/*
 * Copyright (c) 1997, 1999
 *	Matthias Drochner.  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 ``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 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.
 *
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_le.c,v 1.7 2008/04/04 12:25:06 tsutsui Exp $");

#include "opt_inet.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/device.h>

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

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif

#include <machine/cpu.h>
#include <machine/pte.h>

#include <machine/autoconf.h>

#include <dev/ic/lancereg.h>
#include <dev/ic/lancevar.h>
#include <dev/ic/am79900reg.h>
#include <dev/ic/am79900var.h>

#include <cesfic/cesfic/isr.h>

int lematch(device_t, cfdata_t, void *);
void leattach(device_t, device_t, void *);

CFATTACH_DECL_NEW(le, sizeof(struct am79900_softc),
    lematch, leattach, NULL, NULL);

int	leintr(void *);

void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
uint16_t lerdcsr(struct lance_softc *, uint16_t);  

static char *lebase, *lemembase;

void
lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
{

	*(volatile int *)(lebase + 4) = port;
	*(volatile int *)(lebase + 0) = val;
}

uint16_t
lerdcsr(struct lance_softc *sc, uint16_t port)
{
	uint16_t val;

	*(volatile int *)(lebase + 4) = port;
	val = *(volatile int *)(lebase + 0);
	return (val);
}

int
lematch(device_t parent, cfdata_t cf, void *aux)
{

	return (1);
}

/*
 * Interface exists: make available by filling in network interface
 * record.  System will initialize the interface when it is ready
 * to accept packets.
 */
extern void sic_enable_int(int, int, int, int, int);
static char hwa[6] = {0x00,0x80,0xa2,0x00,0x30,0x23};
void
leattach(device_t parent, device_t self, void *aux)
{
	struct lance_softc *sc = device_private(self);
	int i;

	mainbus_map(0x4c000000, 0x10000, 0, (void *)&lemembase);
	mainbus_map(0x48000000, 0x1000, 0, (void *)&lebase);

	sc->sc_dev = self;
	sc->sc_mem = lemembase;
	sc->sc_conf3 = LE_C3_BSWP;
	sc->sc_addr = 0x4c000000;
	sc->sc_memsize = 64 * 1024;

	if (cesfic_getetheraddr(sc->sc_enaddr)) {
		/* fallback */
		for (i = 0; i < sizeof(sc->sc_enaddr); i++) {
			sc->sc_enaddr[i] = hwa[i];
		}
	}

	sc->sc_copytodesc = lance_copytobuf_contig;
	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
	sc->sc_copytobuf = lance_copytobuf_contig;
	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
	sc->sc_zerobuf = lance_zerobuf_contig;

	sc->sc_rdcsr = lerdcsr;
	sc->sc_wrcsr = lewrcsr;
	sc->sc_hwinit = NULL;

	lewrcsr(sc, 4, 0x44);
	am79900_config((struct am79900_softc *)sc);

	/* Establish the interrupt handler. */
	(void) isrlink(leintr, sc, 3, ISRPRI_NET);
	sic_enable_int(17, 0, 1, 3, 0);
}

int
leintr(void *arg)
{
	struct lance_softc *sc = arg;
	uint16_t isr4;

	isr4 = lerdcsr(sc, 4);
	if (isr4 & 0x08) {
		lewrcsr(sc, 4, isr4);
		return (1);
	}

	return (am79900_intr(sc));
}
