/* $NetBSD: dz.c,v 1.44 2024/02/02 15:44:43 tsutsui Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ralph Campbell and Rick Macklem.
*
* 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.
*/
/*
* Copyright (c) 1996 Ken C. Wellsch. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ralph Campbell and Rick Macklem.
*
* 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. 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, Berkeley and its contributors.
* 4. 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.
*/
/*
* The DZ series doesn't interrupt on carrier transitions,
* so we have to use a timer to watch it.
*/
int dz_timer; /* true if timer started */
struct callout dzscan_ch;
static struct cnm_state dz_cnm_state;
void
dzattach(struct dz_softc *sc, struct evcnt *parent_evcnt, int consline)
{
int n;
/* Initialize our softc structure. */
for (n = 0; n < sc->sc_type; n++) {
sc->sc_dz[n].dz_sc = sc;
sc->sc_dz[n].dz_line = n;
sc->sc_dz[n].dz_tty = tty_alloc();
}
/* Alas no interrupt on modem bit changes, so we manually scan */
if (dz_timer == 0) {
dz_timer = 1;
callout_init(&dzscan_ch, 0);
callout_reset(&dzscan_ch, hz, dzscan, NULL);
}
aprint_normal("\n");
}
/*
* Switch to POLLED mode.
* Some simple measurements indicated that even on
* one port, by freeing the scanner in the controller
* by either providing a character or turning off
* the port when output is complete, the transmitter
* was ready to accept more output when polled again.
* With just two ports running the game "worms,"
* almost every interrupt serviced both transmitters!
* Each UART is double buffered, so if the scanner
* is quick enough and timing works out, we can even
* feed the same port twice.
*
* Ragge 980517:
* Do not need to turn off interrupts, already at interrupt level.
* Remove the pdma stuff; no great need of it right now.
*/
for (;;) {
csr = dz_read2(sc, sc->sc_dr.dr_csr);
if ((csr & DZ_CSR_TX_READY) == 0)
break;
/* Just send out a char if we have one */
/* As long as we can fill the chip buffer, we just loop here */
if (cl->c_cc) {
tp->t_state |= TS_BUSY;
ch = getc(cl);
dz_write1(sc, sc->sc_dr.dr_tbuf, ch);
continue;
}
/* Nothing to send; clear the scan bit */
/* Clear xmit scanner bit; dzstart may set it again */
tcr = dz_read2(sc, sc->sc_dr.dr_tcrw);
tcr &= 255;
tcr &= ~(1 << line);
dz_write1(sc, sc->sc_dr.dr_tcr, tcr);
if (sc->sc_dz[line].dz_catch)
continue;
int
dzopen(dev_t dev, int flag, int mode, struct lwp *l)
{
const int line = DZ_PORT(minor(dev));
struct dz_softc *sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
struct tty *tp;
int error = 0;
if (sc == NULL || line >= sc->sc_type)
return ENXIO;
/* if some other device is using the line, it's busy */
if (sc->sc_dz[line].dz_catch)
return EBUSY;
static unsigned
dzmctl(struct dz_softc *sc, int line, int bits, int how)
{
unsigned status;
unsigned mbits;
unsigned bit;
int s;
s = spltty();
mbits = 0;
bit = (1 << line);
/* external signals as seen from the port */
status = dz_read1(sc, sc->sc_dr.dr_dcd) | sc->sc_dsr;
if (status & bit)
mbits |= DML_DCD;
status = dz_read1(sc, sc->sc_dr.dr_ring);
if (status & bit)
mbits |= DML_RI;
/* internal signals/state delivered to port */
status = dz_read1(sc, sc->sc_dr.dr_dtr);
if (status & bit)
mbits |= DML_DTR;
if (sc->sc_brk & bit)
mbits |= DML_BRK;
/*
* This is called by timeout() periodically.
* Check to see if modem status bits have changed.
*/
static void
dzscan(void *arg)
{
struct dz_softc *sc;
struct tty *tp;
int n, bit, port;
unsigned int csr;
unsigned int tmp;
int s;
s = spltty();
for (n = 0; n < dz_cd.cd_ndevs; n++) {
if ((sc = device_lookup_private(&dz_cd, n)) == NULL)
continue;
for (port = 0; port < sc->sc_type; port++) {
tp = sc->sc_dz[port].dz_tty;
bit = (1 << port);
/*
* If the RX interrupt rate is this high, switch
* the controller to Silo Alarm - which means don't
* interrupt until the RX silo has 16 characters in
* it (the silo is 64 characters in all).
* Avoid oscillating SA on and off by not turning
* if off unless the rate is appropriately low.
*/
csr = dz_read2(sc, sc->sc_dr.dr_csr);
tmp = csr;
if (sc->sc_rxint > 16*10)
csr |= DZ_CSR_SAE;
else if (sc->sc_rxint < 10)
csr &= ~DZ_CSR_SAE;
if (csr != tmp)
dz_write2(sc, sc->sc_dr.dr_csr, csr);
sc->sc_rxint = 0;
/*
* Called after an ubareset. The DZ card is reset, but the only thing
* that must be done is to start the receiver and transmitter again.
* No DMA setup to care about.
*/
void
dzreset(device_t dev)
{
struct dz_softc *sc = device_private(dev);
struct tty *tp;
int i;
for (i = 0; i < sc->sc_type; i++) {
tp = sc->sc_dz[i].dz_tty;