/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Gordon W. Ross.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Zilog Z8530 Dual UART driver (machine-dependent part)
*
* Runs two serial lines per chip using slave drivers.
* Plain tty/async lines use the zs_async slave.
* Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
*/
/*
* Some warts needed by z8530tty.c -
* The default parity REALLY needs to be the same as the PROM uses,
* or you can not see messages done with printf during boot-up...
*/
int zs_def_cflag = (CREAD | CS8 | HUPCL);
/*
* The Sun provides a 4.9152 MHz clock to the ZS chips.
*/
#define PCLK (9600 * 512) /* PCLK pin input clock rate */
#define ZS_DELAY() (CPU_ISSUN4C ? (0) : delay(2))
/* The layout of this is hardware-dependent (padding, order). */
struct zschan {
volatile uint8_t zc_csr; /* ctrl,status, and indirect access */
uint8_t zc_xxx0;
volatile uint8_t zc_data; /* data */
uint8_t zc_xxx1;
};
struct zsdevice {
/* Yes, they are backwards. */
struct zschan zs_chan_b;
struct zschan zs_chan_a;
};
/* ZS channel used as the console device (if any) */
void *zs_conschan_get, *zs_conschan_put;
/*
* For machines with zs on mainbus (all sun4c models), we expect
* the device registers to be mapped by the PROM.
*/
zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
}
if (uoba->uoba_isobio4 == 0) {
struct sbus_attach_args *sa = &uoba->uoba_sbus;
void *va;
struct zs_chanstate *cs;
int channel;
if (sa->sa_nintr == 0) {
aprint_error(": no interrupt lines\n");
return;
}
/*
* Some sun4m models (Javastations) may not map the zs device.
*/
if (sa->sa_npromvaddrs > 0)
va = (void *)sa->sa_promvaddr;
else {
bus_space_handle_t bh;
/*
* Check if power state can be set, e.g. Tadpole 3GX
*/
if (prom_getpropint(sa->sa_node, "pwr-on-auxio2", 0)) {
aprint_normal(": powered via auxio2");
for (channel = 0; channel < 2; channel++) {
cs = &zsc->zsc_cs_store[channel];
cs->enable = zs_enable;
cs->disable = zs_disable;
}
}
/*
* As for zs on mainbus, we require a PROM mapping.
*/
if (bus_space_map(oba->oba_bustag,
paddr,
sizeof(struct zsdevice),
BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
&bh) != 0) {
aprint_error(": cannot map zs registers\n");
return;
}
zsc->zsc_bustag = oba->oba_bustag;
zsc->zsc_dmatag = oba->oba_dmatag;
/*
* Find prom unit by physical address
* We're just comparing the address (not the iospace) here
*/
paddr = BUS_ADDR_PADDR(paddr);
if (cpuinfo.cpu_type == CPUTYP_4_100)
/*
* On the sun4/100, the top-most 4 bits are zero
* on obio addresses; force them to 1's for the
* sake of the comparison here.
*/
paddr |= 0xf0000000;
zsc->zsc_promunit =
(paddr == 0xf1000000) ? 0 :
(paddr == 0xf0000000) ? 1 :
(paddr == 0xe0000000) ? 2 : -2;
#if NWSKBD == 0
/* Not using wscons console, so always set console flags.*/
zsc_args.hwflags = hwflags;
if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
zsc_args.consdev = &zs_consdev;
}
#else
/* If we are unit 1, then this is the "real" console.
* Remember this in order to set up the keyboard and
* mouse line disciplines for SUN4 machines below.
* Also, don't set the console flags, otherwise we
* tell zstty_attach() to attach as console.
* XXX
* is this still necessary? sparc64 passes the console flags to
* zstty etc.
*/
if (zsc->zsc_promunit == 1) {
if ((hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0 &&
!channel) {
#if (NKBD > 0) || (NMS > 0)
ch0_is_cons = 1;
#endif
}
} else {
zsc_args.hwflags = hwflags;
if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
zsc_args.consdev = &zs_consdev;
}
}
#endif
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;
}
/* Childs need to set cn_dev, etc */
/* Make these correspond to cs_defcflag (-crtscts) */
cs->cs_rr0_dcd = ZSRR0_DCD;
cs->cs_rr0_cts = 0;
cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
cs->cs_wr5_rts = 0;
/*
* Clear the master interrupt enable.
* The INTENA is common to both channels,
* so just do it on the A channel.
*/
if (channel == 0) {
zs_write_reg(cs, 9, 0);
}
/*
* Look for a child driver for this channel.
* The child attach will setup the hardware.
*/
child = config_found(zsc->zsc_dev, &zsc_args, zs_print,
CFARGS_NONE);
if (child == NULL) {
/* No sub-driver. Just reset it. */
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 this was a zstty it has a keyboard
* property on it we need to attach the
* sunkbd and sunms line disciplines.
* There are no properties on SUN4 machines.
* For them, check if we have set the
* ch0_is_cons variable above.
*/
if ((child != NULL) &&
(device_is_a(child, "zstty")) && (
(CPU_ISSUN4 && ch0_is_cons) || (!CPU_ISSUN4 &&
(prom_getproplen(zsc->zsc_node, "keyboard") == 0))))
{
struct kbd_ms_tty_attach_args kma;
struct tty *tp = zstty_get_tty_from_dev(child);
kma.kmta_tp = tp;
kma.kmta_dev = tp->t_dev;
/*
* we need to pass a consdev since that's how kbd knows
* it's the console keyboard
*/
if (hwflags & ZS_HWFLAG_CONSOLE_INPUT) {
kma.kmta_consdev = &zs_consdev;
} else
kma.kmta_consdev = zsc_args.consdev;
/*
* Set the master interrupt enable and interrupt vector.
* (common to both channels, do it on A)
*/
cs = zsc->zsc_cs[0];
zs_lock_chan(cs);
/* interrupt vector */
zs_write_reg(cs, 2, zs_init_reg[2]);
/* master interrupt control (enable) */
zs_write_reg(cs, 9, zs_init_reg[9]);
zs_unlock_chan(cs);
#if 0
/*
* XXX: L1A hack - We would like to be able to break into
* the debugger during the rest of autoconfiguration, so
* lower interrupts just enough to let zs interrupts in.
* This is done after both zs devices are attached.
*/
if (zsc->zsc_promunit == 1) {
aprint_debug("zs1: enabling zs interrupts\n");
(void)splfd(); /* XXX: splzs - 1 */
}
#endif
if (args->channel != -1)
aprint_normal(" channel %d", args->channel);
return (UNCONF);
}
/*
* Our ZS chips all share a common interrupt level,
* but we establish zshard handler per each ZS chips
* to avoid holding unnecessary locks in interrupt context.
*/
static int
zshard(void *arg)
{
struct zsc_softc *zsc = arg;
int rr3, rval;
rval = 0;
rr3 = zsc_intr_hard(zsc);
/* Count up the interrupts. */
if (rr3) {
rval = rr3;
zsc->zsc_intrcnt.ev_count++;
}
if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
softint_schedule(zsc->zsc_sicookie);
return (rval);
}
/*
* Compute the current baud rate given a ZS channel.
*/
static int
zs_get_speed(struct zs_chanstate *cs)
{
int tconst;
/*
* MD functions for setting the baud rate and control modes.
* bps - in bits per second
*/
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);
/* Convert back to make sure we can do it. */
real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
/* XXX - Allow some tolerance here? */
if (real_bps != bps)
return (EINVAL);
/****************************************************************
* Console support functions (Sun specific!)
* Note: this code is allowed to know about the layout of
* the chip registers, and uses that to keep things simple.
* XXX - I think I like the mvme167 code better. -gwr
****************************************************************/
/*
* Handle user request to enter kernel debugger.
*/
void
zs_abort(struct zs_chanstate *cs)
{
struct zschan *zc = zs_conschan_get;
int rr0;
/* Wait for end of break to avoid PROM abort. */
/* XXX - Limit the wait? */
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while (rr0 & ZSRR0_BREAK);
/* Temporarily direct interrupts at ourselves */
s = splhigh();
omid = setitr(cpuinfo.mid);
/* Wait for a character to arrive. */
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while ((rr0 & ZSRR0_RX_READY) == 0);
c = zc->zc_data;
ZS_DELAY();
setitr(omid);
splx(s);
/*
* This is used by the kd driver to read scan codes,
* so don't translate '\r' ==> '\n' here...
*/
return (c);
}
/*
* Polled output char.
*/
void
zs_putc(void *arg, int c)
{
struct zschan *zc = arg;
int s, rr0;
u_int omid;
/* Temporarily direct interrupts at ourselves */
s = splhigh();
omid = setitr(cpuinfo.mid);
/* Wait for transmitter to become ready. */
do {
rr0 = zc->zc_csr;
ZS_DELAY();
} while ((rr0 & ZSRR0_TX_READY) == 0);
/*
* Send the next character.
* Now you'd think that this could be followed by a ZS_DELAY()
* just like all the other chip accesses, but it turns out that
* the `transmit-ready' interrupt isn't de-asserted until
* some period of time after the register write completes
* (more than a couple instructions). So to avoid stray
* interrupts we put in the 2us delay regardless of CPU model.
*/
zc->zc_data = c;
delay(2);
/*
* Polled console output putchar.
*/
static void
zscnputc(dev_t dev, int c)
{
zs_putc(zs_conschan_put, c);
}
static void
zscnpollc(dev_t dev, int on)
{
/* No action needed */
}
static int
zs_console_flags(int promunit, int node, int channel)
{
int cookie, flags = 0;
switch (prom_version()) {
case PROM_OLDMON:
case PROM_OBP_V0:
/*
* Use `promunit' and `channel' to derive the PROM
* stdio handles that correspond to this device.
*/
if (promunit == 0)
cookie = PROMDEV_TTYA + channel;
else if (promunit == 1 && channel == 0)
cookie = PROMDEV_KBD;
else
cookie = -1;
if (cookie == prom_stdin())
flags |= ZS_HWFLAG_CONSOLE_INPUT;
/*
* Prevent the keyboard from matching the output device
* (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
*/
if (cookie != PROMDEV_KBD && cookie == prom_stdout())
flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
break;
case PROM_OBP_V2:
case PROM_OBP_V3:
case PROM_OPENFIRM:
/*
* Match the nodes and device arguments prepared by
* consinit() against our device node and channel.
* (The device argument is the part of the OBP path
* following the colon, as in `/obio/zs@0,100000:a')
*/
/* Default to channel 0 if there are no explicit prom args */
cookie = 0;
if (node == prom_stdin_node) {
if (prom_stdin_args[0] != '\0')
/* Translate (a,b) -> (0,1) */
cookie = prom_stdin_args[0] - 'a';
if (channel == cookie)
flags |= ZS_HWFLAG_CONSOLE_INPUT;
}
if (node == prom_stdout_node) {
if (prom_stdout_args[0] != '\0')
/* Translate (a,b) -> (0,1) */
cookie = prom_stdout_args[0] - 'a';
if (channel == cookie)
flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
}
break;
default:
break;
}
return (flags);
}
/*
* Power management hooks for zsopen() and zsclose().
* We use them to power on/off the ports, if necessary.
*/
int
zs_enable(struct zs_chanstate *cs)
{