#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: lpt_isa.c,v 1.70 2019/12/27 09:28:41 msaitoh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/device.h>
#include <sys/syslog.h>
#include <sys/bus.h>
#include <sys/intr.h>
#include <dev/isa/isavar.h>
#include <dev/ic/lptreg.h>
#include <dev/ic/lptvar.h>
#define LPTPRI (PZERO+8)
#define LPT_BSIZE 1024
#ifndef LPTDEBUG
#define LPRINTF(a)
#else
#define LPRINTF(a) if (lpt_isa_debug) printf a
int lpt_isa_debug = 0;
#endif
struct lpt_isa_softc {
struct lpt_softc sc_lpt;
int sc_irq;
isa_chipset_tag_t sc_ic;
};
int lpt_isa_probe(device_t, cfdata_t, void *);
static void lpt_isa_attach(device_t, device_t, void *);
static int lpt_isa_detach(device_t, int);
CFATTACH_DECL_NEW(lpt_isa, sizeof(struct lpt_isa_softc),
lpt_isa_probe, lpt_isa_attach, lpt_isa_detach, NULL);
int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
bus_size_t, u_char, u_char);
int
lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh,
bus_addr_t base, bus_size_t off, u_char data, u_char mask)
{
int timeout;
u_char temp;
data &= mask;
bus_space_write_1(iot, ioh, off, data);
timeout = 1000;
do {
delay(10);
temp = bus_space_read_1(iot, ioh, off) & mask;
} while (temp != data && --timeout);
LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
(unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
return (temp == data);
}
int
lpt_isa_probe(device_t parent, cfdata_t match, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_tag_t iot;
bus_space_handle_t ioh;
u_long base;
u_char mask, data;
int i, rv;
#ifdef LPT_DEBUG
#define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
goto out;} while (0)
#else
#define ABORT goto out
#endif
if (ia->ia_nio < 1)
return (0);
if (ISA_DIRECT_CONFIG(ia))
return (0);
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
return (0);
iot = ia->ia_iot;
base = ia->ia_io[0].ir_addr;
if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
return 0;
rv = 0;
mask = 0xff;
data = 0x55;
if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
ABORT;
data = 0xaa;
if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
ABORT;
for (i = 0; i < CHAR_BIT; i++) {
data = ~(1 << i);
if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
ABORT;
}
for (i = 0; i < CHAR_BIT; i++) {
data = (1 << i);
if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
ABORT;
}
bus_space_write_1(iot, ioh, lpt_data, 0);
bus_space_write_1(iot, ioh, lpt_control, 0);
ia->ia_io[0].ir_size = LPT_NPORTS;
ia->ia_niomem = 0;
ia->ia_ndrq = 0;
rv = 1;
out:
bus_space_unmap(iot, ioh, LPT_NPORTS);
return rv;
}
void
lpt_isa_attach(device_t parent, device_t self, void *aux)
{
struct lpt_isa_softc *sc = device_private(self);
struct lpt_softc *lsc = &sc->sc_lpt;
struct isa_attach_args *ia = aux;
bus_space_tag_t iot;
bus_space_handle_t ioh;
lsc->sc_dev = self;
if (ia->ia_nirq < 1 ||
ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
sc->sc_irq = -1;
aprint_normal(": polled\n");
} else {
sc->sc_irq = ia->ia_irq[0].ir_irq;
aprint_normal("\n");
}
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
iot = lsc->sc_iot = ia->ia_iot;
if (bus_space_map(iot, ia->ia_io[0].ir_addr, LPT_NPORTS, 0, &ioh)) {
aprint_normal_dev(self, "can't map i/o space\n");
return;
}
lsc->sc_ioh = ioh;
lpt_attach_subr(lsc);
sc->sc_ic = ia->ia_ic;
if (sc->sc_irq != -1)
lsc->sc_ih = isa_intr_establish_xname(sc->sc_ic, sc->sc_irq,
IST_EDGE, IPL_TTY, lptintr, lsc, device_xname(lsc->sc_dev));
}
static int
lpt_isa_detach(device_t self, int flags)
{
int rc;
struct lpt_isa_softc *sc = device_private(self);
struct lpt_softc *lsc = &sc->sc_lpt;
if ((rc = lpt_detach_subr(self, flags)) != 0)
return rc;
if (sc->sc_irq != -1)
isa_intr_disestablish(sc->sc_ic, lsc->sc_ih);
bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, LPT_NPORTS);
return 0;
}