#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dz_ibus.c,v 1.14 2021/08/07 16:19:02 thorpej Exp $");
#include "dzkbd.h"
#include "dzms.h"
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <dev/cons.h>
#include <dev/tc/tcvar.h>
#include <dev/dec/dzreg.h>
#include <dev/dec/dzvar.h>
#include <dev/dec/dzkbdvar.h>
#include <pmax/ibus/ibusvar.h>
#include <pmax/pmax/pmaxtype.h>
#include <pmax/pmax/cons.h>
#define DZ_LINE_KBD 0
#define DZ_LINE_MOUSE 1
#define DZ_LINE_CONSOLE 2
#define DZ_LINE_AUX 3
int dz_ibus_match(device_t, cfdata_t, void *);
void dz_ibus_attach(device_t, device_t, void *);
int dz_ibus_intr(void *);
void dz_ibus_cnsetup(paddr_t);
int dz_ibus_cngetc(dev_t);
void dz_ibus_cnputc(dev_t, int);
void dz_ibus_cnpollc(dev_t, int);
int dz_ibus_getmajor(void);
int dz_ibus_print(void *, const char *);
int dzgetc(struct dz_linestate *);
void dzputc(struct dz_linestate *, int);
CFATTACH_DECL_NEW(dz_ibus, sizeof(struct dz_softc),
dz_ibus_match, dz_ibus_attach, NULL, NULL);
struct consdev dz_ibus_consdev = {
NULL, NULL, dz_ibus_cngetc, dz_ibus_cnputc,
dz_ibus_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
};
struct dzregs {
uint16_t csr;
uint16_t p0[3];
uint16_t rbuf;
uint16_t p1[3];
uint16_t tcr;
uint16_t p2[3];
uint16_t tdr;
uint16_t p3[3];
} volatile *dzcn;
int dz_ibus_iscn;
int dz_ibus_consln = -1;
int
dz_ibus_match(device_t parent, cfdata_t cf, void *aux)
{
struct ibus_attach_args *iba = aux;
if (strcmp(iba->ia_name, "dc") != 0 &&
strcmp(iba->ia_name, "mdc") != 0 &&
strcmp(iba->ia_name, "dc7085") != 0)
return (0);
if (badaddr((void *)iba->ia_addr, 2))
return (0);
return (1);
}
void
dz_ibus_attach(device_t parent, device_t self, void *aux)
{
struct ibus_attach_args *iba = aux;
struct dz_softc *sc = device_private(self);
volatile struct dzregs *dz;
#if NDZMS > 0 || NDZKBD > 0
struct dzkm_attach_args daa;
#endif
int i;
DELAY(100000);
sc->sc_dev = self;
sc->sc_iot = normal_memt;
sc->sc_ioh = (bus_space_handle_t)MIPS_PHYS_TO_KSEG1(iba->ia_addr);
sc->sc_dr.dr_csr = 0;
sc->sc_dr.dr_rbuf = 8;
sc->sc_dr.dr_dtr = 17;
sc->sc_dr.dr_break = 25;
sc->sc_dr.dr_tbuf = 24;
sc->sc_dr.dr_tcr = 16;
sc->sc_dr.dr_dcd = 25;
sc->sc_dr.dr_ring = 24;
sc->sc_dr.dr_firstreg = 0;
sc->sc_dr.dr_winsize = sizeof(struct dzregs);
sc->sc_type = DZ_DZV;
dz = (volatile struct dzregs *)sc->sc_ioh;
i = dz->tcr;
dz->csr = DZ_CSR_MSE | DZ_CSR_TXIE;
dz->tcr = 0;
wbflush();
DELAY(1000);
dz->tcr = 1;
wbflush();
DELAY(100000);
dz->tcr = i;
wbflush();
sc->sc_dsr = 0x0f;
aprint_normal(": DC-7085, 4 lines");
ibus_intr_establish(parent, (void *)iba->ia_cookie, IPL_TTY,
dz_ibus_intr, sc);
dzattach(sc, NULL, dz_ibus_consln);
DELAY(10000);
if (systype == DS_PMAX || systype == DS_3MAX) {
#if NDZKBD > 0
if (!dz_ibus_iscn)
dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8) |
DZ_LPR_8_BIT_CHAR | DZ_LINE_KBD;
daa.daa_line = DZ_LINE_KBD;
daa.daa_flags = (dz_ibus_iscn ? 0 : DZKBD_CONSOLE);
config_found(self, &daa, dz_ibus_print, CFARGS_NONE);
#endif
#if NDZMS > 0
dz->rbuf = DZ_LPR_RX_ENABLE | (DZ_LPR_B4800 << 8) |
DZ_LPR_8_BIT_CHAR | DZ_LPR_PARENB | DZ_LPR_OPAR |
DZ_LINE_MOUSE;
daa.daa_line = DZ_LINE_MOUSE;
daa.daa_flags = 0;
config_found(self, &daa, dz_ibus_print, CFARGS_NONE);
#endif
}
}
int
dz_ibus_getmajor(void)
{
extern const struct cdevsw dz_cdevsw;
static int cache = -1;
if (cache != -1)
return (cache);
return (cache = cdevsw_lookup_major(&dz_cdevsw));
}
int
dz_ibus_intr(void *cookie)
{
struct dz_softc *sc;
volatile struct dzregs *dzr;
unsigned csr;
sc = cookie;
dzr = (volatile struct dzregs *)sc->sc_ioh;
while (((csr = dzr->csr) & (DZ_CSR_RX_DONE | DZ_CSR_TX_READY)) != 0) {
if ((csr & DZ_CSR_RX_DONE) != 0)
dzrint(sc);
if ((csr & DZ_CSR_TX_READY) != 0)
dzxint(sc);
}
return (0);
}
void
dz_ibus_cnsetup(paddr_t addr)
{
dzcn = (void *)MIPS_PHYS_TO_KSEG1(addr);
}
void
dz_ibus_cnattach(int line)
{
switch (line) {
case 0:
line = DZ_LINE_KBD;
break;
case 4:
line = DZ_LINE_CONSOLE;
break;
default:
line = DZ_LINE_AUX;
break;
}
dz_ibus_iscn = 1;
dz_ibus_consln = line;
dzcn->csr = 0;
wbflush();
DELAY(1000);
dzcn->tcr = (1 << line);
wbflush();
DELAY(1000);
dzcn->csr = 0x20;
wbflush();
DELAY(1000);
cn_tab = &dz_ibus_consdev;
cn_tab->cn_pri = CN_REMOTE;
cn_tab->cn_dev = makedev(dz_ibus_getmajor(), line);
}
int
dz_ibus_cngetc(dev_t dev)
{
int c, line, s;
uint16_t rbuf;
c = 0;
line = minor(dev);
s = spltty();
do {
while ((dzcn->csr & DZ_CSR_RX_DONE) == 0)
DELAY(10);
DELAY(10);
rbuf = dzcn->rbuf;
if (((rbuf >> 8) & 3) != line)
continue;
c = rbuf & 0x7f;
} while (c == 17 || c == 19);
splx(s);
if (c == 13)
c = 10;
return (c);
}
void
dz_ibus_cnputc(dev_t dev, int ch)
{
int timeout, s;
uint16_t tcr;
s = spltty();
timeout = 1 << 15;
tcr = dzcn->tcr;
dzcn->tcr = (1 << minor(dev));
wbflush();
DELAY(10);
while ((dzcn->csr & 0x8000) == 0)
if (--timeout < 0)
break;
DELAY(10);
dzcn->tdr = ch;
timeout = 1 << 15;
wbflush();
DELAY(10);
while ((dzcn->csr & 0x8000) == 0)
if (--timeout < 0)
break;
DELAY(10);
dzcn->tcr = tcr;
wbflush();
DELAY(10);
splx(s);
}
void
dz_ibus_cnpollc(dev_t dev, int pollflag)
{
}
#if NDZKBD > 0 || NDZMS > 0
int
dz_ibus_print(void *aux, const char *pnp)
{
struct dzkm_attach_args *daa;
daa = aux;
if (pnp != NULL)
aprint_normal("lkkbd/vsms at %s", pnp);
aprint_normal(" line %d", daa->daa_line);
return (UNCONF);
}
int
dzgetc(struct dz_linestate *ls)
{
volatile struct dzregs *dzr;
int line, s;
uint16_t rbuf;
if (ls == NULL) {
line = DZ_LINE_KBD;
dzr = dzcn;
} else {
line = ls->dz_line;
dzr = (volatile struct dzregs *)ls->dz_sc->sc_ioh;
}
s = spltty();
for (;;) {
while ((dzr->csr & DZ_CSR_RX_DONE) == 0)
DELAY(10);
DELAY(10);
rbuf = dzr->rbuf;
DELAY(10);
if (((rbuf >> 8) & 3) == line)
return (rbuf & 0xff);
}
splx(s);
}
void
dzputc(struct dz_linestate *ls, int ch)
{
volatile struct dzregs *dzr;
int line;
uint16_t tcr;
int s;
if (ls != NULL && ls->dz_sc != NULL) {
line = ls->dz_line;
dzr = (volatile struct dzregs *)ls->dz_sc->sc_ioh;
s = spltty();
putc(ch, &ls->dz_tty->t_outq);
tcr = dzr->tcr;
if ((tcr & (1 << line)) == 0) {
dzr->tcr = tcr | (1 << line);
wbflush();
DELAY(10);
}
dzxint(ls->dz_sc);
splx(s);
return;
}
dz_ibus_cnputc(makedev(dz_ibus_getmajor(), DZ_LINE_KBD), ch);
}
#endif