#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.48 2026/04/23 02:54:38 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <hp300/dev/dmavar.h>
#include <hp300/dev/hpibvar.h>
#include <machine/cpu.h>
#include <machine/hp300spu.h>
#include "ioconf.h"
static int hpibbusmatch(device_t, cfdata_t, void *);
static void hpibbusattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(hpibbus, sizeof(struct hpibbus_softc),
hpibbusmatch, hpibbusattach, NULL, NULL);
static void hpibbus_attach_children(struct hpibbus_softc *);
static int hpibbussubmatch(device_t, cfdata_t, const int *ldesc, void *);
static int hpibbusprint(void *, const char *);
static void hpibstart(void *);
static void hpibdone(void *);
int hpibtimeout = 100000;
int hpibidtimeout = 10000;
int hpibdmathresh = 3;
static int
hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
{
return 1;
}
static void
hpibbusattach(device_t parent, device_t self, void *aux)
{
struct hpibbus_softc *sc = device_private(self);
struct hpibdev_attach_args *ha = aux;
sc->sc_dev = self;
aprint_normal("\n");
sc->sc_ops = ha->ha_ops;
sc->sc_type = ha->ha_type;
sc->sc_ba = ha->ha_ba;
*(ha->ha_softcpp) = sc;
hpibreset(device_unit(self));
sc->sc_dq = kmem_alloc(sizeof(struct dmaqueue), KM_SLEEP);
sc->sc_dq->dq_softc = sc;
sc->sc_dq->dq_start = hpibstart;
sc->sc_dq->dq_done = hpibdone;
TAILQ_INIT(&sc->sc_queue);
hpibbus_attach_children(sc);
}
static void
hpibbus_attach_children(struct hpibbus_softc *sc)
{
struct hpibbus_attach_args ha;
int id, slave, punit;
int i;
for (slave = 0; slave < HPIB_NSLAVES; slave++) {
for (i = 0; i < 3; i++) {
id = hpibid(device_unit(sc->sc_dev), slave);
if ((id & 0x200) != 0)
break;
delay(10000);
}
for (punit = 0; punit < HPIB_NPUNITS; punit++) {
ha.ha_id = id;
ha.ha_slave = slave;
ha.ha_punit = punit;
config_found(sc->sc_dev, &ha, hpibbusprint,
CFARGS(.submatch = hpibbussubmatch));
}
}
}
static int
hpibbussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct hpibbus_attach_args *ha = aux;
if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
cf->hpibbuscf_slave != ha->ha_slave)
return 0;
if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
cf->hpibbuscf_punit != ha->ha_punit)
return 0;
return config_match(parent, cf, aux);
}
static int
hpibbusprint(void *aux, const char *pnp)
{
struct hpibbus_attach_args *ha = aux;
if (pnp != NULL) {
if (ha->ha_id == 0 || ha->ha_punit != 0 )
return QUIET;
printf("HP-IB device (id %04X) at %s", ha->ha_id, pnp);
}
aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
return UNCONF;
}
int
hpibdevprint(void *aux, const char *pnp)
{
if (pnp != NULL)
aprint_normal("hpibbus at %s", pnp);
return UNCONF;
}
void
hpibreset(int unit)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
(*sc->sc_ops->hpib_reset)(sc);
}
int
hpibreq(device_t pdev, struct hpibqueue *hq)
{
struct hpibbus_softc *sc = device_private(pdev);
int s;
s = splhigh();
TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
splx(s);
if (TAILQ_FIRST(&sc->sc_queue) == hq)
return 1;
return 0;
}
void
hpibfree(device_t pdev, struct hpibqueue *hq)
{
struct hpibbus_softc *sc = device_private(pdev);
int s;
s = splhigh();
TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
splx(s);
if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
(*hq->hq_start)(hq->hq_softc);
}
int
hpibid(int unit, int slave)
{
short id;
int ohpibtimeout;
ohpibtimeout = hpibtimeout;
hpibtimeout = hpibidtimeout * (cpuspeed_khz / 8000);
if (hpibrecv(unit, 31, slave, &id, 2) != 2)
id = 0;
hpibtimeout = ohpibtimeout;
return id;
}
int
hpibsend(int unit, int slave, int sec, void *addr, int cnt)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
}
int
hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
}
int
hpibpptest(int unit, int slave)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
}
void
hpibppclear(int unit)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
sc->sc_flags &= ~HPIBF_PPOLL;
}
void
hpibawait(int unit)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
sc->sc_flags |= HPIBF_PPOLL;
(*sc->sc_ops->hpib_ppwatch)(sc);
}
int
hpibswait(int unit, int slave)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
int timo = hpibtimeout;
int mask, (*ppoll)(struct hpibbus_softc *);
ppoll = sc->sc_ops->hpib_ppoll;
mask = 0x80 >> slave;
while (((*ppoll)(sc) & mask) == 0) {
if (--timo == 0) {
printf("%s: swait timeout\n", device_xname(sc->sc_dev));
return -1;
}
}
return 0;
}
int
hpibustart(int unit)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
if (sc->sc_type == HPIBA)
sc->sc_dq->dq_chan = DMA0;
else
sc->sc_dq->dq_chan = DMA0 | DMA1;
if (dmareq(sc->sc_dq))
return 1;
return 0;
}
static void
hpibstart(void *arg)
{
struct hpibbus_softc *sc = arg;
struct hpibqueue *hq;
hq = TAILQ_FIRST(&sc->sc_queue);
(*hq->hq_go)(hq->hq_softc);
}
void
hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
{
struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
}
static void
hpibdone(void *arg)
{
struct hpibbus_softc *sc = arg;
(*sc->sc_ops->hpib_done)(sc);
}
int
hpibintr(void *arg)
{
struct hpibbus_softc *sc = arg;
return (sc->sc_ops->hpib_intr)(arg);
}