#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.26 2023/12/12 23:38:11 andvar Exp $");
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/tty.h>
#include <sys/time.h>
#include <sys/syslog.h>
#include <sys/intr.h>
#include <machine/autoconf.h>
#include <machine/promlib.h>
#include <machine/cpu.h>
#include <machine/eeprom.h>
#include <machine/psl.h>
#include <machine/z8530var.h>
#include <dev/cons.h>
#include <dev/ic/z8530reg.h>
#include <dev/sun/kbd_ms_ttyvar.h>
#include <ddb/db_active.h>
#include <ddb/db_output.h>
#include <sun2/dev/cons.h>
#include "ioconf.h"
#include "kbd.h"
#include "ms.h"
int zs_def_cflag = (CREAD | CS8 | HUPCL);
void *zs_conschan_get, *zs_conschan_put;
static uint8_t zs_init_reg[16] = {
0,
0,
#ifdef ZS_INIT_IVECT
ZS_INIT_IVECT,
#else
0,
#endif
ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
0,
0,
0,
#ifdef ZS_INIT_IVECT
ZSWR9_MASTER_IE,
#else
ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
#endif
0,
ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
((PCLK/32)/9600)-2,
0,
ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
ZSWR15_BREAK_IE,
};
static int zscngetc(dev_t);
static void zscnputc(dev_t, int);
static void zscnpollc(dev_t, int);
struct consdev zs_consdev = {
NULL,
NULL,
zscngetc,
zscnputc,
zscnpollc,
NULL,
};
static int zs_print(void *, const char *name);
int zscheckintr(void *);
static int zshard(void *);
static void zssoft(void *);
static int zs_get_speed(struct zs_chanstate *);
void
zs_attach(struct zsc_softc *zsc, struct zsdevice *zsd, int pri)
{
struct zsc_attach_args zsc_args;
struct zs_chanstate *cs;
int channel;
memset(&zsc_args, 0, sizeof zsc_args);
if (zsd == NULL) {
aprint_error(": configuration incomplete\n");
return;
}
#if 0
aprint_normal(" softpri %d\n", _IPL_SOFT_LEVEL3);
#else
aprint_normal("\n");
#endif
for (channel = 0; channel < 2; channel++) {
struct zschan *zc;
device_t child;
zsc_args.channel = channel;
zsc_args.hwflags = 0;
cs = &zsc->zsc_cs_store[channel];
zsc->zsc_cs[channel] = cs;
zs_lock_init(cs);
cs->cs_channel = channel;
cs->cs_private = NULL;
cs->cs_ops = &zsops_null;
cs->cs_brg_clk = PCLK / 16;
zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
zsc_args.consdev = NULL;
zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
zsc->zsc_node,
channel);
if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
zsc_args.consdev = &zs_consdev;
}
if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
zs_conschan_get = zc;
}
if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
zs_conschan_put = zc;
}
cs->cs_reg_csr = &zc->zc_csr;
cs->cs_reg_data = &zc->zc_data;
memcpy(cs->cs_creg, zs_init_reg, 16);
memcpy(cs->cs_preg, zs_init_reg, 16);
cs->cs_defspeed = zs_get_speed(cs);
cs->cs_defcflag = zs_def_cflag;
cs->cs_rr0_dcd = ZSRR0_DCD;
cs->cs_rr0_cts = 0;
cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
cs->cs_wr5_rts = 0;
if (channel == 0) {
zs_write_reg(cs, 9, 0);
}
if ((child = config_found(zsc->zsc_dev, (void *)&zsc_args,
zs_print, CFARGS_NONE)) == NULL) {
uint8_t reset = (channel == 0) ?
ZSWR9_A_RESET : ZSWR9_B_RESET;
zs_lock_chan(cs);
zs_write_reg(cs, 9, reset);
zs_unlock_chan(cs);
}
#if (NKBD > 0) || (NMS > 0)
if (child
&& device_is_a(child, "zstty")) {
struct kbd_ms_tty_attach_args kma;
struct zstty_softc {
device_t zst_dev;
struct tty *zst_tty;
struct zs_chanstate *zst_cs;
} *zst = device_private(child);
struct tty *tp;
kma.kmta_tp = tp = zst->zst_tty;
if (tp != NULL) {
kma.kmta_dev = tp->t_dev;
kma.kmta_consdev = zsc_args.consdev;
switch(zs_peripheral_type(zsc->zsc_promunit,
zsc->zsc_node,
channel)) {
case ZS_PERIPHERAL_SUNKBD:
#if (NKBD > 0)
kma.kmta_name = "keyboard";
config_found(child, (void *)&kma, NULL,
CFARGS_NONE);
#endif
break;
case ZS_PERIPHERAL_SUNMS:
#if (NMS > 0)
kma.kmta_name = "mouse";
config_found(child, (void *)&kma, NULL,
CFARGS_NONE);
#endif
break;
default:
break;
}
}
}
#endif
}
bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc);
if ((zsc->zsc_softintr = softint_establish(SOFTINT_SERIAL,
zssoft, zsc)) == NULL)
panic("%s: could not establish soft interrupt", __func__);
evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
device_xname(zsc->zsc_dev), "intr");
cs = zsc->zsc_cs[0];
zs_lock_chan(cs);
zs_write_reg(cs, 2, zs_init_reg[2]);
zs_write_reg(cs, 9, zs_init_reg[9]);
zs_unlock_chan(cs);
}
static int
zs_print(void *aux, const char *name)
{
struct zsc_attach_args *args = aux;
if (name != NULL)
aprint_normal("%s: ", name);
if (args->channel != -1)
aprint_normal(" channel %d", args->channel);
return (UNCONF);
}
static int
zshard(void *arg)
{
struct zsc_softc *zsc = arg;
int rval;
uint8_t rr3;
rval = 0;
while ((rr3 = zsc_intr_hard(zsc))) {
rval |= rr3;
zsc->zsc_intrcnt.ev_count++;
}
if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
(zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
zsc->zsc_softintr) {
softint_schedule(zsc->zsc_softintr);
}
return (rval);
}
int
zscheckintr(void *arg)
{
struct zsc_softc *zsc;
int unit, rval;
rval = 0;
for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
zsc = device_lookup_private(&zs_cd, unit);
if (zsc == NULL)
continue;
rval = (zshard((void *)zsc) || rval);
}
return (rval);
}
static void
zssoft(void *arg)
{
struct zsc_softc *zsc = arg;
int s;
s = spltty();
(void)zsc_intr_soft(zsc);
splx(s);
}
static int
zs_get_speed(struct zs_chanstate *cs)
{
int tconst;
tconst = zs_read_reg(cs, 12);
tconst |= zs_read_reg(cs, 13) << 8;
return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
}
int
zs_set_speed(struct zs_chanstate *cs, int bps)
{
int tconst, real_bps;
if (bps == 0)
return (0);
#ifdef DIAGNOSTIC
if (cs->cs_brg_clk == 0)
panic("zs_set_speed");
#endif
tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
if (tconst < 0)
return (EINVAL);
real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
if (real_bps != bps)
return (EINVAL);
cs->cs_preg[12] = tconst;
cs->cs_preg[13] = tconst >> 8;
return (0);
}
int
zs_set_modes(struct zs_chanstate *cs, int cflag )
{
zs_lock_chan(cs);
cs->cs_rr0_pps = 0;
if ((cflag & (CLOCAL | MDMBUF)) != 0) {
cs->cs_rr0_dcd = 0;
if ((cflag & MDMBUF) == 0)
cs->cs_rr0_pps = ZSRR0_DCD;
} else
cs->cs_rr0_dcd = ZSRR0_DCD;
if ((cflag & CRTSCTS) != 0) {
cs->cs_wr5_dtr = ZSWR5_DTR;
cs->cs_wr5_rts = ZSWR5_RTS;
cs->cs_rr0_cts = ZSRR0_CTS;
} else if ((cflag & CDTRCTS) != 0) {
cs->cs_wr5_dtr = 0;
cs->cs_wr5_rts = ZSWR5_DTR;
cs->cs_rr0_cts = ZSRR0_CTS;
} else if ((cflag & MDMBUF) != 0) {
cs->cs_wr5_dtr = 0;
cs->cs_wr5_rts = ZSWR5_DTR;
cs->cs_rr0_cts = ZSRR0_DCD;
} else {
cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
cs->cs_wr5_rts = 0;
cs->cs_rr0_cts = 0;
}
zs_unlock_chan(cs);
return (0);
}
uint8_t
zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
{
uint8_t val;
*cs->cs_reg_csr = reg;
ZS_DELAY();
val = *cs->cs_reg_csr;
ZS_DELAY();
return (val);
}
void
zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
{
*cs->cs_reg_csr = reg;
ZS_DELAY();
*cs->cs_reg_csr = val;
ZS_DELAY();
}
uint8_t
zs_read_csr(struct zs_chanstate *cs)
{
uint8_t val;
val = *cs->cs_reg_csr;
ZS_DELAY();
return (val);
}
void
zs_write_csr(struct zs_chanstate *cs, uint8_t val)
{
*cs->cs_reg_csr = val;
ZS_DELAY();
}
uint8_t
zs_read_data(struct zs_chanstate *cs)
{
uint8_t val;
val = *cs->cs_reg_data;
ZS_DELAY();
return (val);
}
void
zs_write_data(struct zs_chanstate *cs, uint8_t val)
{
*cs->cs_reg_data = val;
ZS_DELAY();
}
extern void Debugger(void);
void
zs_abort(struct zs_chanstate *cs)
{
volatile struct zschan *zc = zs_conschan_get;
int rr0;
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while (rr0 & ZSRR0_BREAK);
#if defined(KGDB)
zskgdb(cs);
#elif defined(DDB)
if (!db_active)
Debugger();
else
callrom();
#else
printf("stopping on keyboard abort\n");
callrom();
#endif
}
int
zs_getc(void *arg)
{
volatile struct zschan *zc = arg;
int s, c, rr0;
s = splhigh();
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while ((rr0 & ZSRR0_RX_READY) == 0);
c = zc->zc_data;
ZS_DELAY();
splx(s);
return (c);
}
void
zs_putc(void *arg, int c)
{
volatile struct zschan *zc = arg;
int s, rr0;
s = splhigh();
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while ((rr0 & ZSRR0_TX_READY) == 0);
zc->zc_data = c;
delay(2);
splx(s);
}
static int
zscngetc(dev_t dev)
{
return (zs_getc(zs_conschan_get));
}
static void
zscnputc(dev_t dev, int c)
{
zs_putc(zs_conschan_put, c);
}
int swallow_zsintrs;
static void
zscnpollc(dev_t dev, int on)
{
if (on) swallow_zsintrs++;
else swallow_zsintrs--;
}