#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ie_obio.c,v 1.19 2023/12/20 05:13:35 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <uvm/uvm_extern.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <machine/autoconf.h>
#include <machine/idprom.h>
#include <dev/ic/i82586reg.h>
#include <dev/ic/i82586var.h>
struct ieob {
u_char obctrl;
};
#define IEOB_NORSET 0x80
#define IEOB_ONAIR 0x40
#define IEOB_ATTEN 0x20
#define IEOB_IENAB 0x10
#define IEOB_XXXXX 0x08
#define IEOB_XCVRL2 0x04
#define IEOB_BUSERR 0x02
#define IEOB_INT 0x01
#define IEOB_ADBASE 0x000000
static void ie_obreset(struct ie_softc *, int);
static void ie_obattend(struct ie_softc *, int);
static void ie_obrun(struct ie_softc *);
int ie_obio_match(device_t, cfdata_t, void *);
void ie_obio_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(ie_obio, sizeof(struct ie_softc),
ie_obio_match, ie_obio_attach, NULL, NULL);
static int media[] = {
IFM_ETHER | IFM_10_2,
};
#define NMEDIA __arraycount(media)
void
ie_obreset(struct ie_softc *sc, int what)
{
volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
ieo->obctrl = 0;
delay(100);
ieo->obctrl = IEOB_NORSET;
}
void
ie_obattend(struct ie_softc *sc, int why)
{
volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
ieo->obctrl |= IEOB_ATTEN;
ieo->obctrl &= ~IEOB_ATTEN;
}
void
ie_obrun(struct ie_softc *sc)
{
volatile struct ieob *ieo = (struct ieob *) sc->sc_reg;
ieo->obctrl |= (IEOB_ONAIR|IEOB_IENAB|IEOB_NORSET);
}
void ie_obio_memcopyin(struct ie_softc *, void *, int, size_t);
void ie_obio_memcopyout(struct ie_softc *, const void *, int, size_t);
void
ie_obio_memcopyin(struct ie_softc *sc, void *p, int offset, size_t size)
{
bus_space_copyin(sc->bt, sc->bh, offset, p, size);
}
void
ie_obio_memcopyout(struct ie_softc *sc, const void *p, int offset, size_t size)
{
bus_space_copyout(sc->bt, sc->bh, offset, p, size);
}
uint16_t ie_obio_read16(struct ie_softc *, int);
void ie_obio_write16(struct ie_softc *, int, uint16_t);
void ie_obio_write24(struct ie_softc *, int, int);
uint16_t
ie_obio_read16(struct ie_softc *sc, int offset)
{
uint16_t v = bus_space_read_2(sc->bt, sc->bh, offset);
return (((v&0xff)<<8) | ((v>>8)&0xff));
}
void
ie_obio_write16(struct ie_softc *sc, int offset, uint16_t v)
{
v = (((v&0xff)<<8) | ((v>>8)&0xff));
bus_space_write_2(sc->bt, sc->bh, offset, v);
}
void
ie_obio_write24(struct ie_softc *sc, int offset, int addr)
{
u_char *f = (u_char *)&addr;
uint16_t v0, v1;
u_char *t;
t = (u_char *)&v0;
t[0] = f[3]; t[1] = f[2];
bus_space_write_2(sc->bt, sc->bh, offset, v0);
t = (u_char *)&v1;
t[0] = f[1]; t[1] = 0;
bus_space_write_2(sc->bt, sc->bh, offset+2, v1);
}
int
ie_obio_match(device_t parent, cfdata_t cf, void *aux)
{
struct obio_attach_args *oba = aux;
bus_space_handle_t bh;
int matched;
uint8_t ctrl;
if (oba->oba_paddr == -1)
return 0;
if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
sizeof(struct ieob), 0, &bh))
return 0;
matched = (!bus_space_poke_1(oba->oba_bustag, bh, 0, IEOB_NORSET) &&
!bus_space_peek_1(oba->oba_bustag, bh, 0, &ctrl) &&
(ctrl & (IEOB_ONAIR|IEOB_IENAB)) == 0);
bus_space_unmap(oba->oba_bustag, bh, sizeof(struct ieob));
if (!matched)
return 0;
if (oba->oba_pri == -1)
oba->oba_pri = 3;
return 1;
}
void
ie_obio_attach(device_t parent, device_t self, void *aux)
{
struct obio_attach_args *oba = aux;
struct ie_softc *sc = device_private(self);
bus_dma_tag_t dmatag = oba->oba_dmatag;
bus_space_handle_t bh;
bus_dma_segment_t seg;
int rseg;
int error;
paddr_t pa;
bus_size_t memsize;
u_long iebase;
uint8_t myaddr[ETHER_ADDR_LEN];
sc->sc_dev = self;
sc->bt = oba->oba_bustag;
sc->hwreset = ie_obreset;
sc->chan_attn = ie_obattend;
sc->hwinit = ie_obrun;
sc->memcopyout = ie_obio_memcopyout;
sc->memcopyin = ie_obio_memcopyin;
sc->ie_bus_barrier = NULL;
sc->ie_bus_read16 = ie_obio_read16;
sc->ie_bus_write16 = ie_obio_write16;
sc->ie_bus_write24 = ie_obio_write24;
sc->sc_msize = memsize = 65536;
if (bus_space_map(oba->oba_bustag, oba->oba_paddr, sizeof(struct ieob),
0, &bh))
panic("ie_obio_attach: can't map regs");
sc->sc_reg = (void *)bh;
if ((error = bus_dmamap_create(dmatag, memsize, 1, memsize, 0,
BUS_DMA_NOWAIT|BUS_DMA_24BIT, &sc->sc_dmamap)) != 0) {
printf("%s: DMA map create error %d\n",
device_xname(self), error);
return;
}
if ((error = bus_dmamem_alloc(dmatag, memsize, 64*1024, 0,
&seg, 1, &rseg, BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
printf("%s: DMA memory allocation error %d\n",
device_xname(self), error);
return;
}
if ((error = bus_dmamem_map(dmatag, &seg, rseg, memsize,
(void **)&sc->sc_maddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
printf("%s: DMA buffer map error %d\n",
device_xname(self), error);
bus_dmamem_free(dmatag, &seg, rseg);
return;
}
if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
sc->sc_maddr, memsize, NULL, BUS_DMA_NOWAIT)) != 0) {
printf("%s: DMA buffer map load error %d\n",
device_xname(self), error);
bus_dmamem_unmap(dmatag, sc->sc_maddr, memsize);
bus_dmamem_free(dmatag, &seg, rseg);
return;
}
w16zero(sc->sc_maddr, memsize);
sc->bh = (bus_space_handle_t)(sc->sc_maddr);
if (pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_maddr, &pa) == false)
panic("ie pmap_extract");
pmap_enter(pmap_kernel(), m68k_trunc_page(IEOB_ADBASE+IE_SCP_ADDR),
pa | PMAP_NC ,
VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
sc->iscp = 0;
sc->scb = IE_ISCP_SZ;
sc->scp = IE_SCP_ADDR & PGOFSET;
iebase = (u_long)sc->sc_dmamap->dm_segs[0].ds_addr -
(u_long)IEOB_ADBASE;
ie_obio_write16(sc, IE_ISCP_SCB(sc->iscp), sc->scb);
ie_obio_write24(sc, IE_ISCP_BASE(sc->iscp), iebase);
ie_obio_write24(sc, IE_SCP_ISCP(sc->scp), iebase + sc->iscp);
sc->buf_area = PAGE_SIZE;
sc->buf_area_sz = memsize - PAGE_SIZE;
if (i82586_proberam(sc) == 0) {
printf(": memory probe failed\n");
return;
}
idprom_etheraddr(myaddr);
i82586_attach(sc, "onboard", myaddr, media, NMEDIA, media[0]);
bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_NET, 0,
i82586_intr, sc);
}