#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.15 2023/10/24 19:05:07 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/kgdb.h>
#include <dev/ofw/openfirm.h>
#include <dev/ic/z8530reg.h>
#include <machine/z8530var.h>
static void zs_setparam(struct zs_chanstate *, int, int);
static void zskgdb(struct zs_chanstate *);
struct zsops zsops_kgdb;
static u_char zs_kgdb_regs[16] = {
0,
0,
0,
ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
0,
0,
0,
ZSWR9_MASTER_IE,
0,
ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
((PCLK/32)/38400)-2,
0,
ZSWR14_BAUD_ENA,
ZSWR15_BREAK_IE,
};
static void
zs_setparam(struct zs_chanstate *cs, int iena, int rate)
{
int s, tconst;
memcpy(cs->cs_preg, zs_kgdb_regs, 16);
if (iena) {
cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
}
tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
cs->cs_preg[12] = tconst;
cs->cs_preg[13] = tconst >> 8;
s = splhigh();
zs_loadchannelregs(cs);
splx(s);
}
#ifndef KGDB_DEVNAME
#define KGDB_DEVNAME "scca"
#endif
const char *zs_kgdb_devname = KGDB_DEVNAME;
void
zs_kgdb_init(void)
{
extern const struct cdevsw zstty_cdevsw;
struct zs_chanstate cs;
volatile struct zschan *zc;
int escc, escc_ch, obio, zs_offset;
int channel = 0;
u_int32_t reg[5];
char name[16];
if ((escc_ch = OF_finddevice(zs_kgdb_devname)) == -1)
return;
memset(name, 0, sizeof(name));
if (OF_getprop(escc_ch, "device_type", name, sizeof(name)) == -1)
return;
if (strcmp(name, "serial") != 0)
return;
memset(name, 0, sizeof(name));
if (OF_getprop(escc_ch, "name", name, sizeof(name)) == -1)
return;
if (strcmp(name, "ch-b") == 0)
channel = 1;
if (OF_getprop(escc_ch, "reg", reg, sizeof(reg)) < 4)
return;
zs_offset = reg[0];
escc = OF_parent(escc_ch);
obio = OF_parent(escc);
if (OF_getprop(obio, "assigned-addresses", reg, sizeof(reg)) < 12)
return;
zc = (struct zschan *)(reg[2] + zs_offset);
kgdb_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), channel);
printf("zs_kgdb_init: attaching tty%02d at %d baud\n",
channel, kgdb_rate);
memset((void *)&cs, 0, sizeof(cs));
cs.cs_channel = channel;
cs.cs_brg_clk = PCLK / 16;
cs.cs_reg_csr = &zc->zc_csr;
cs.cs_reg_data = &zc->zc_data;
zs_setparam(&cs, 0, kgdb_rate);
kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
}
int
zs_check_kgdb(struct zs_chanstate *cs, int dev)
{
if (dev != kgdb_dev)
return (0);
cs->cs_private = NULL;
cs->cs_ops = &zsops_kgdb;
zs_setparam(cs, 1, kgdb_rate);
return (1);
}
static void
zskgdb(struct zs_chanstate *cs)
{
int unit = minor(kgdb_dev);
printf("zstty%d: kgdb interrupt\n", unit);
kgdb_connect(1);
}
static void zs_kgdb_rxint(struct zs_chanstate *);
static void zs_kgdb_stint(struct zs_chanstate *, int);
static void zs_kgdb_txint(struct zs_chanstate *);
static void zs_kgdb_softint(struct zs_chanstate *);
int kgdb_input_lost;
static void
zs_kgdb_rxint(struct zs_chanstate *cs)
{
register u_char c, rr1;
rr1 = zs_read_reg(cs, 1);
c = zs_read_data(cs);
if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
zs_write_csr(cs, ZSWR0_RESET_ERRORS);
}
if (c == KGDB_START) {
zskgdb(cs);
} else {
kgdb_input_lost++;
}
}
static void
zs_kgdb_txint(register struct zs_chanstate *cs)
{
zs_write_csr(cs, ZSWR0_RESET_TXINT);
}
static void
zs_kgdb_stint(register struct zs_chanstate *cs, int force)
{
register int rr0;
rr0 = zs_read_csr(cs);
zs_write_csr(cs, ZSWR0_RESET_STATUS);
if (rr0 & ZSRR0_BREAK) {
zskgdb(cs);
}
}
static void
zs_kgdb_softint(struct zs_chanstate *cs)
{
printf("zs_kgdb_softint?\n");
}
struct zsops zsops_kgdb = {
zs_kgdb_rxint,
zs_kgdb_stint,
zs_kgdb_txint,
zs_kgdb_softint,
};