/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)zs.c 8.1 (Berkeley) 7/19/93
*/
/*-
* Copyright (c) 1995 The NetBSD Foundation, Inc. (Atari modifications)
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Leo Weppelman.
*
* 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 (ZSCC) driver.
*
* Runs two tty ports (modem2 and serial2) on zs0.
*
* This driver knows far too much about chip to usage mappings.
*/
/*
* Software state per found chip.
*/
struct zs_softc {
device_t sc_dev; /* base device */
struct zs_chanstate *sc_cs[2]; /* chan A and B software state */
struct zs_chanstate sc_cs_store[2];
void *sc_sicookie; /* for callback */
};
/*
* Define the machine dependent clock frequencies
* If BRgen feeds sender/receiver we always use a
* divisor 16; therefore the division by 16 can as
* well be done here.
*/
static const u_long zs_freqs_tt[] = {
/*
* Atari TT, RTxCB is generated by TT-MFP timer C,
* which is set to 307.2 kHz during initialisation
* and never changed afterwards.
*/
PCLK/16, /* BRgen, PCLK, divisor 16 */
229500, /* BRgen, RTxCA, divisor 16 */
3672000, /* RTxCA, from PCLK4 */
0, /* TRxCA, external */
/* Interrupt handlers. */
static int zshard(void *);
static int zssoft(void *);
static int zsrint(struct zs_chanstate *, struct zschan *);
static int zsxint(struct zs_chanstate *, struct zschan *);
static int zssint(struct zs_chanstate *, struct zschan *);
/* Routines called from other code. */
static void zsstart(struct tty *);
/* Routines purely local to this driver. */
static void zsoverrun(int, long *, const char *);
static int zsparam(struct tty *, struct termios *);
static int zsbaudrate(int, int, int *, int *, int *, int *);
static int zs_modem(struct zs_chanstate *, int, int);
static void zs_loadchannelregs(struct zschan *, uint8_t *);
static void zs_shutdown(struct zs_chanstate *);
static int
zsmatch(device_t parent, cfdata_t cf, void *aux)
{
static int zs_matched = 0;
/*
* Open a zs serial port.
*/
static int
zsopen(dev_t dev, int flags, int mode, struct lwp *l)
{
struct tty *tp;
struct zs_chanstate *cs;
struct zs_softc *sc;
int unit = ZS_UNIT(dev);
int zs = unit >> 1;
int error, s;
if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
return EBUSY;
s = spltty();
/*
* Do the following iff this is a first open.
*/
if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
if (tp->t_ispeed == 0) {
tp->t_iflag = TTYDEF_IFLAG;
tp->t_oflag = TTYDEF_OFLAG;
tp->t_cflag = TTYDEF_CFLAG;
tp->t_lflag = TTYDEF_LFLAG;
tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
}
ttychars(tp);
ttsetwater(tp);
(void)zsparam(tp, &tp->t_termios);
/*
* Turn on DTR. We must always do this, even if carrier is not
* present, because otherwise we'd have to use TIOCSDTR
* immediately after setting CLOCAL, which applications do not
* expect. We always assert DTR while the device is open
* unless explicitly requested to deassert it.
*/
zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
/* May never get a status intr. if DCD already on. -gwr */
if (((cs->cs_rr0 = cs->cs_zc->zc_csr) & ZSRR0_DCD) != 0)
tp->t_state |= TS_CARR_ON;
if (cs->cs_softcar)
tp->t_state |= TS_CARR_ON;
}
error = tp->t_linesw->l_open(dev, tp);
if (error)
goto bad;
return 0;
bad:
if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
/*
* We failed to open the device, and nobody else had it opened.
* Clean up the state as appropriate.
*/
zs_shutdown(cs);
}
return error;
}
/*
* Close a zs serial port.
*/
static int
zsclose(dev_t dev, int flags, int mode, struct lwp *l)
{
struct zs_chanstate *cs;
struct tty *tp;
struct zs_softc *sc;
int unit = ZS_UNIT(dev);
if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
/*
* Although we got a last close, the device may still be in
* use; e.g. if this was the dialout node, and there are still
* processes waiting for carrier on the non-dialout node.
*/
zs_shutdown(cs);
}
return 0;
}
/*
* Read/write zs serial port.
*/
static int
zsread(dev_t dev, struct uio *uio, int flags)
{
struct zs_chanstate *cs;
struct zs_softc *sc;
struct tty *tp;
int unit;
unit = ZS_UNIT(dev);
sc = device_lookup_private(&zs_cd, unit >> 1);
cs = sc->sc_cs[unit & 1];
tp = cs->cs_ttyp;
return (*tp->t_linesw->l_read)(tp, uio, flags);
}
static int
zswrite(dev_t dev, struct uio *uio, int flags)
{
struct zs_chanstate *cs;
struct zs_softc *sc;
struct tty *tp;
int unit;
unit = ZS_UNIT(dev);
sc = device_lookup_private(&zs_cd, unit >> 1);
cs = sc->sc_cs[unit & 1];
tp = cs->cs_ttyp;
unit = ZS_UNIT(dev);
sc = device_lookup_private(&zs_cd, unit >> 1);
cs = sc->sc_cs[unit & 1];
return cs->cs_ttyp;
}
/*
* ZS hardware interrupt. Scan all ZS channels. NB: we know here that
* channels are kept in (A,B) pairs.
*
* Do just a little, then get out; set a software interrupt if more
* work is needed.
*
* We deliberately ignore the vectoring Zilog gives us, and match up
* only the number of `reset interrupt under service' operations, not
* the order.
*/
int
zshard(void *arg)
{
struct zs_softc *sc;
struct zs_chanstate *cs0, *cs1;
struct zschan *zc;
int intflags, v, i;
uint8_t rr3;
static int
zsxint(struct zs_chanstate *cs, struct zschan *zc)
{
int i = cs->cs_tbc;
if (i == 0) {
zc->zc_csr = ZSWR0_RESET_TXINT;
zc->zc_csr = ZSWR0_CLR_INTR;
return ZRING_MAKE(ZRING_XINT, 0);
}
cs->cs_tbc = i - 1;
zc->zc_data = *cs->cs_tba++;
zc->zc_csr = ZSWR0_CLR_INTR;
return 0;
}
static int
zssint(struct zs_chanstate *cs, struct zschan *zc)
{
int rr0;
rr0 = zc->zc_csr;
zc->zc_csr = ZSWR0_RESET_STATUS;
zc->zc_csr = ZSWR0_CLR_INTR;
/*
* The chip's hardware flow control is, as noted in zsreg.h,
* busted---if the DCD line goes low the chip shuts off the
* receiver (!). If we want hardware CTS flow control but do
* not have it, and carrier is now on, turn HFC on; if we have
* HFC now but carrier has gone low, turn it off.
*/
if (rr0 & ZSRR0_DCD) {
if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
(cs->cs_creg[3] & ZSWR3_HFC) == 0) {
cs->cs_creg[3] |= ZSWR3_HFC;
ZS_WRITE(zc, 3, cs->cs_creg[3]);
}
} else {
if (cs->cs_creg[3] & ZSWR3_HFC) {
cs->cs_creg[3] &= ~ZSWR3_HFC;
ZS_WRITE(zc, 3, cs->cs_creg[3]);
}
}
return ZRING_MAKE(ZRING_SINT, rr0);
}
/*
* Print out a ring or fifo overrun error message.
*/
static void
zsoverrun(int unit, long *ptime, const char *what)
{
time_t cur_sec = time_second;
/*
* ZS software interrupt. Scan all channels for deferred interrupts.
*/
int
zssoft(void *arg)
{
struct zs_softc *sc;
struct zs_chanstate *cs;
struct zschan *zc;
struct linesw *line;
struct tty *tp;
int chan, get, n, c, cc, s;
int retval = 0;
sc = arg;
s = spltty();
for (chan = 0; chan < 2; chan++) {
cs = sc->sc_cs[chan];
get = cs->cs_rbget;
again:
n = cs->cs_rbput; /* atomic */
if (get == n) /* nothing more on this line */
continue;
retval = 1;
zc = cs->cs_zc;
tp = cs->cs_ttyp;
line = tp->t_linesw;
/*
* Compute the number of interrupts in the receive ring.
* If the count is overlarge, we lost some events, and
* must advance to the first valid one. It may get
* overwritten if more data are arriving, but this is
* too expensive to check and gains nothing (we already
* lost out; all we can do at this point is trade one
* kind of loss for another).
*/
n -= get;
if (n > ZLRB_RING_SIZE) {
zsoverrun(chan, &cs->cs_rotime, "ring");
get += n - ZLRB_RING_SIZE;
n = ZLRB_RING_SIZE;
}
while (--n >= 0) {
/* race to keep ahead of incoming interrupts */
c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
switch (ZRING_TYPE(c)) {
case ZRING_RINT:
c = ZRING_VALUE(c);
if ((c & ZSRR1_DO) != 0)
zsoverrun(chan, &cs->cs_fotime, "fifo");
cc = c >> 8;
if ((c & ZSRR1_FE) != 0)
cc |= TTY_FE;
if ((c & ZSRR1_PE) != 0)
cc |= TTY_PE;
line->l_rint(cc, tp);
break;
case ZRING_XINT:
/*
* Transmit done: change registers and resume,
* or clear BUSY.
*/
if (cs->cs_heldchange) {
int sps;
case ZRING_SINT:
/*
* Status line change. HFC bit is run in
* hardware interrupt, to avoid locking
* at splzs here.
*/
c = ZRING_VALUE(c);
if (((c ^ cs->cs_rr0) & ZSRR0_DCD) != 0) {
cc = (c & ZSRR0_DCD) != 0;
if (line->l_modem(tp, cc) == 0)
zs_modem(cs,
ZSWR5_RTS | ZSWR5_DTR,
cc ? DMBIS : DMBIC);
}
cs->cs_rr0 = c;
break;
/*
* can have `local' or `softcar', and `rtscts' or `mdmbuf'
# defaulting to software flow control.
*/
if ((userbits & TIOCFLAG_SOFTCAR) != 0 &&
(userbits & TIOCFLAG_CLOCAL) != 0)
return EINVAL;
if ((userbits & TIOCFLAG_MDMBUF) != 0)
/* don't support this (yet?) */
return ENODEV;
s = splzs();
if ((userbits & TIOCFLAG_SOFTCAR) != 0) {
cs->cs_softcar = 1; /* turn on softcar */
cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
} else if ((userbits & TIOCFLAG_CLOCAL) != 0) {
cs->cs_softcar = 0; /* turn off softcar */
cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
cs->cs_creg[15] |= ZSWR15_DCD_IE;
ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
tp->t_termios.c_cflag |= CLOCAL;
}
if ((userbits & TIOCFLAG_CRTSCTS) != 0) {
cs->cs_preg[15] |= ZSWR15_CTS_IE;
cs->cs_creg[15] |= ZSWR15_CTS_IE;
ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
cs->cs_preg[3] |= ZSWR3_HFC;
cs->cs_creg[3] |= ZSWR3_HFC;
ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
tp->t_termios.c_cflag |= CRTSCTS;
} else {
/* no mdmbuf, so we must want software flow control */
cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
cs->cs_preg[3] &= ~ZSWR3_HFC;
cs->cs_creg[3] &= ~ZSWR3_HFC;
ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
tp->t_termios.c_cflag &= ~CRTSCTS;
}
splx(s);
break;
}
case TIOCSDTR:
zs_modem(cs, ZSWR5_DTR, DMBIS);
break;
case TIOCCDTR:
zs_modem(cs, ZSWR5_DTR, DMBIC);
break;
case TIOCMGET:
zs_modem(cs, 0, DMGET);
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
default:
return EPASSTHROUGH;
}
return 0;
}
/*
* Start or restart transmission.
*/
static void
zsstart(struct tty *tp)
{
struct zs_chanstate *cs;
int s, nch;
int unit = ZS_UNIT(tp->t_dev);
struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
cs = sc->sc_cs[unit & 1];
s = spltty();
/*
* If currently active or delaying, no need to do anything.
*/
if ((tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) != 0)
goto out;
/*
* If there are sleepers, and output has drained below low
* water mark, awaken.
*/
ttypull(tp);
/*
* Hang up if necessary. Wait a bit, so the other side has time to
* notice even if we immediately open the port again.
*/
if ((tp->t_cflag & HUPCL) != 0) {
zs_modem(cs, 0, DMSET);
(void)tsleep((void *)cs, TTIPRI, ttclos, hz);
}
/* Clear any break condition set with TIOCSBRK. */
if ((cs->cs_creg[5] & ZSWR5_BREAK) != 0) {
cs->cs_preg[5] &= ~ZSWR5_BREAK;
cs->cs_creg[5] &= ~ZSWR5_BREAK;
ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
}
/*
* Drop all lines and cancel interrupts
*/
zs_loadchannelregs(cs->cs_zc, zs_init_regs);
splx(s);
}
/*
* Set ZS tty parameters from termios.
*
* This routine makes use of the fact that only registers
* 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
*/
static int
zsparam(struct tty *tp, struct termios *t)
{
int unit = ZS_UNIT(tp->t_dev);
struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
struct zs_chanstate *cs = sc->sc_cs[unit & 1];
int cdiv = 0; /* XXX gcc4 -Wuninitialized */
int clkm = 0; /* XXX gcc4 -Wuninitialized */
int brgm = 0; /* XXX gcc4 -Wuninitialized */
int tcon = 0; /* XXX gcc4 -Wuninitialized */
int tmp, tmp5, cflag, s;
/*
* Block interrupts so that state will not
* be altered until we are done setting it up.
*/
s = splzs();
cs->cs_preg[4] = cdiv;
cs->cs_preg[11] = clkm;
cs->cs_preg[12] = tcon;
cs->cs_preg[13] = tcon >> 8;
cs->cs_preg[14] = brgm;
cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
cs->cs_preg[10] = ZSWR10_NRZ;
cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
/*
* Output hardware flow control on the chip is horrendous: if
* carrier detect drops, the receiver is disabled. Hence we
* can only do this when the carrier is on.
*/
if ((cflag & CCTS_OFLOW) != 0 &&
(cs->cs_zc->zc_csr & ZSRR0_DCD) != 0)
tmp |= ZSWR3_HFC;
cs->cs_preg[3] = tmp;
cs->cs_preg[5] = tmp5;
/*
* If nothing is being transmitted, set up new current values,
* else mark them as pending.
*/
if (cs->cs_heldchange == 0) {
if ((cs->cs_ttyp->t_state & TS_BUSY) != 0) {
cs->cs_heldtbc = cs->cs_tbc;
cs->cs_tbc = 0;
cs->cs_heldchange = 1;
} else {
memcpy((void *)cs->cs_creg, (void *)cs->cs_preg, 16);
zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
}
}
splx(s);
return 0;
}
/*
* search for the best matching baudrate
*/
static int
zsbaudrate(int unit, int wanted, int *divisor, int *clockmode, int *brgenmode,
int *timeconst)
{
int bestdiff, bestbps, source;
/*
* Raise or lower modem control (DTR/RTS) signals. If a character is
* in transmission, the change is deferred.
*/
static int
zs_modem(struct zs_chanstate *cs, int bits, int how)
{
int s, mbits;
bits &= ZSWR5_DTR | ZSWR5_RTS;
s = splzs();
mbits = cs->cs_preg[5] & (ZSWR5_DTR | ZSWR5_RTS);
switch (how) {
case DMSET:
mbits = bits;
break;
case DMBIS:
mbits |= bits;
break;
case DMBIC:
mbits &= ~bits;
break;
case DMGET:
splx(s);
return mbits;
}
/*
* Write the given register set to the given zs channel in the proper order.
* The channel must not be transmitting at the time. The receiver will
* be disabled for the time it takes to write all the registers.
*/
static void
zs_loadchannelregs(struct zschan *zc, uint8_t *reg)
{
int i;