#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ie_vme.c,v 1.24 2024/12/20 23:52:00 tsutsui Exp $");
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_ether.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#include <netinet/if_inarp.h>
#endif
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <machine/dvma.h>
#include <machine/idprom.h>
#include <machine/vmparam.h>
#include "i82586.h"
#include "if_iereg.h"
#include "if_ievar.h"
static void ie_vmereset(struct ie_softc *);
static void ie_vmeattend(struct ie_softc *);
static void ie_vmerun(struct ie_softc *);
static void *wmemcpy(void *, const void *, size_t);
static void *wmemset(void *, int, size_t);
static int ie_vme_match(device_t, cfdata_t, void *);
static void ie_vme_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(ie_vme, sizeof(struct ie_softc),
ie_vme_match, ie_vme_attach, NULL, NULL);
static int
ie_vme_match(device_t parent, cfdata_t cf, void *args)
{
struct confargs *ca = args;
if (ca->ca_paddr == -1)
return 0;
if (bus_peek(ca->ca_bustype, ca->ca_paddr, 2) == -1)
return 0;
if (ca->ca_intpri == -1)
ca->ca_intpri = 3;
return 1;
}
void
ie_vme_attach(device_t parent, device_t self, void *args)
{
struct ie_softc *sc = device_private(self);
struct confargs *ca = args;
volatile struct ievme *iev;
u_long rampaddr;
int lcv, off;
sc->sc_dev = self;
sc->hard_type = IE_VME;
sc->reset_586 = ie_vmereset;
sc->chan_attn = ie_vmeattend;
sc->run_586 = ie_vmerun;
sc->sc_memcpy = wmemcpy;
sc->sc_memset = wmemset;
sc->sc_msize = 0x10000;
sc->sc_reg = bus_mapin(ca->ca_bustype, ca->ca_paddr,
sizeof(struct ievme));
iev = (volatile struct ievme *) sc->sc_reg;
rampaddr = ca->ca_paddr & 0xfff00000;
rampaddr |= ((iev->status & IEVME_HADDR) << 16);
sc->sc_maddr = bus_mapin(ca->ca_bustype, rampaddr, sc->sc_msize);
sc->sc_iobase = sc->sc_maddr;
iev->pectrl |= IEVME_PARACK;
for (lcv = 0; lcv < IEVME_MAPSZ; lcv++)
iev->pgmap[lcv] = IEVME_SBORDR | IEVME_OBMEM | lcv;
(sc->sc_memset)(sc->sc_maddr, 0, sc->sc_msize);
off = IE_SCP_ADDR & 0xFFFF;
sc->scp = (volatile void *)((char *)sc->sc_maddr + off);
sc->buf_area = sc->sc_maddr;
sc->buf_area_sz = off;
idprom_etheraddr(sc->sc_addr);
ie_attach(sc);
isr_add_vectored(ie_intr, sc, ca->ca_intpri, ca->ca_intvec);
}
void
ie_vmeattend(struct ie_softc *sc)
{
volatile struct ievme *iev = (struct ievme *)sc->sc_reg;
iev->status |= IEVME_ATTEN;
iev->status &= ~IEVME_ATTEN;
}
void
ie_vmereset(struct ie_softc *sc)
{
volatile struct ievme *iev = (struct ievme *)sc->sc_reg;
iev->status = IEVME_RESET;
delay(20);
iev->status = (IEVME_ONAIR | IEVME_IENAB | IEVME_PEINT);
}
void
ie_vmerun(struct ie_softc *sc)
{
}
static void *
wmemset(void *vb, int val, size_t l)
{
uint8_t *b = vb;
uint8_t *be = b + l;
uint16_t *sp;
if (l == 0)
return vb;
if ((uint32_t)b & 1)
*b++ = val;
if (b != be && ((uint32_t)be & 1) != 0) {
be--;
*be = val;
}
sp = (uint16_t *)b;
while (sp != (uint16_t *)be)
*sp++ = val;
return vb;
}
static void *
wmemcpy(void *dst, const void *src, size_t l)
{
const uint8_t *b1e, *b1 = src;
uint8_t *b2 = dst;
const uint16_t *sp;
int bstore = 0;
if (l == 0)
return dst;
if ((uint32_t)b1 & 1) {
*b2++ = *b1++;
l--;
}
sp = (const uint16_t *)b1;
b1e = b1 + l;
if (l & 1)
b1e--;
bstore = (uint32_t)b2 & 1;
while (sp < (const uint16_t *)b1e) {
if (bstore) {
b2[1] = *sp & 0xff;
b2[0] = *sp >> 8;
} else
*((uint16_t *)b2) = *sp;
sp++;
b2 += 2;
}
if (l & 1)
*b2 = *b1e;
return dst;
}