/*-
* Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved.
*
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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) 1998, 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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.
*/
/*
* Copyright (c) 1991 The Regents of the University of California.
* All rights reserved.
*
* 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.
*
* @(#)com.c 7.5 (Berkeley) 5/16/91
*/
/*
* SH internal serial driver
*
* This code is derived from both z8530tty.c and com.c
*/
/* Buffer size for character buffer */
#define SCIF_RING_SIZE 2048
static unsigned int scif_rbuf_size = SCIF_RING_SIZE;
/* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
static unsigned int scif_rbuf_hiwat = (SCIF_RING_SIZE * 1) / 4;
static unsigned int scif_rbuf_lowat = (SCIF_RING_SIZE * 3) / 4;
#ifdef SCIFCN_SPEED
unsigned int scifcn_speed = SCIFCN_SPEED;
#else
unsigned int scifcn_speed = 9600;
#endif
#define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
/* XXX: uwe
* Prepare for bus_spacification. The difference in access widths is
* still handled by the magic definitions in scifreg.h
*/
#define scif_smr_read() SHREG_SCSMR2
#define scif_smr_write(v) (SHREG_SCSMR2 = (v))
/* Output the first chunk of the contiguous buffer. */
{
int n;
int maxchars;
int i;
n = sc->sc_tbc;
maxchars = sc->sc_fifolen
- ((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
if (n > maxchars)
n = maxchars;
for (i = 0; i < n; i++) {
scif_putc(*(sc->sc_tba));
sc->sc_tba++;
}
sc->sc_tbc -= n;
}
out:
splx(s);
return;
}
/*
* Set SCIF tty parameters from termios.
* XXX - Should just copy the whole termios after
* making sure all the changes could be done.
*/
static int
scifparam(struct tty *tp, struct termios *t)
{
struct scif_softc *sc;
int ospeed = t->c_ospeed;
int s;
sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
if (!device_is_active(sc->sc_dev))
return (EIO);
/* Check requested parameters. */
if (ospeed < 0)
return (EINVAL);
if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
return (EINVAL);
/*
* For the console, always force CLOCAL and !HUPCL, so that the port
* is always active.
*/
if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
SET(t->c_cflag, CLOCAL);
CLR(t->c_cflag, HUPCL);
}
/*
* If there were no changes, don't do anything. This avoids dropping
* input and improves performance when all we did was frob things like
* VMIN and VTIME.
*/
if (tp->t_ospeed == t->c_ospeed &&
tp->t_cflag == t->c_cflag)
return (0);
/*
* Set the flow control pins depending on the current flow control
* mode.
*/
if (ISSET(t->c_cflag, CRTSCTS)) {
scif_fcr_write(scif_fcr_read() | SCFCR2_MCE);
} else {
scif_fcr_write(scif_fcr_read() & ~SCFCR2_MCE);
}
/*
* Set the FIFO threshold based on the receive speed.
*
* * If it's a low speed, it's probably a mouse or some other
* interactive device, so set the threshold low.
* * If it's a high speed, trim the trigger level down to prevent
* overflows.
* * Otherwise set it a bit higher.
*/
#if 0
/* XXX (msaitoh) */
if (ISSET(sc->sc_hwflags, SCIF_HW_HAYESP))
sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
else if (ISSET(sc->sc_hwflags, SCIF_HW_FIFO))
sc->sc_fifo = FIFO_ENABLE |
(t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
else
sc->sc_fifo = 0;
#endif
/* And copy to tty. */
tp->t_ispeed = 0;
tp->t_ospeed = t->c_ospeed;
tp->t_cflag = t->c_cflag;
s = splserial();
if (ISSET(tp->t_state, TS_BUSY)) {
/* Stop transmitting at the next chunk. */
sc->sc_tbc = 0;
sc->sc_heldtbc = 0;
if (!ISSET(tp->t_state, TS_TTSTOP))
SET(tp->t_state, TS_FLUSH);
}
splx(s);
}
static void
scifdiag(void *arg)
{
struct scif_softc *sc = arg;
int overflows, floods;
int s;
static void
scif_rxsoft(struct scif_softc *sc, struct tty *tp)
{
int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
u_char *get, *end;
u_int cc, scc;
u_char ssr2;
int code;
int s;
end = sc->sc_ebuf;
get = sc->sc_rbget;
scc = cc = scif_rbuf_size - sc->sc_rbavail;
if (cc == scif_rbuf_size) {
sc->sc_floods++;
if (sc->sc_errors++ == 0)
callout_reset(&sc->sc_diag_ch, 60 * hz, scifdiag, sc);
}
while (cc) {
code = get[0];
ssr2 = get[1];
if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) {
if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER))
SET(code, TTY_FE);
if (ISSET(ssr2, SCSSR2_PER))
SET(code, TTY_PE);
}
if ((*rint)(code, tp) == -1) {
/*
* The line discipline's buffer is out of space.
*/
if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
/*
* We're either not using flow control, or the
* line discipline didn't tell us to block for
* some reason. Either way, we have no way to
* know when there's more space available, so
* just drop the rest of the data.
*/
get += cc << 1;
if (get >= end)
get -= scif_rbuf_size << 1;
cc = 0;
} else {
/*
* Don't schedule any more receive processing
* until the line discipline tells us there's
* space available (through scifhwiflow()).
* Leave the rest of the data in the input
* buffer.
*/
SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
}
break;
}
get += 2;
if (get >= end)
get = sc->sc_rbuf;
cc--;
}
if (cc != scc) {
sc->sc_rbget = get;
s = splserial();
cc = sc->sc_rbavail += scc - cc;
/* Buffers should be ok again, release possible block. */
if (cc >= sc->sc_r_lowat) {
if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
scif_scr_write(scif_scr_read() | SCSCR2_RIE);
}
#if 0
if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
scif_hwiflow(sc);
}
#endif
}
splx(s);
}
}
if (sc->sc_tx_done) {
sc->sc_tx_done = 0;
scif_txsoft(sc, tp);
}
}
static int
scifintr(void *arg)
{
struct scif_softc *sc = arg;
u_char *put, *end;
u_int cc;
u_short ssr2;
int count;
if (!device_is_active(sc->sc_dev))
return (0);
end = sc->sc_ebuf;
put = sc->sc_rbput;
cc = sc->sc_rbavail;
do {
ssr2 = scif_ssr_read();
if (ISSET(ssr2, SCSSR2_BRK)) {
scif_ssr_write(scif_ssr_read()
& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_DR));
#ifdef DDB
if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
console_debugger();
}
#endif /* DDB */
#ifdef KGDB
if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB)) {
kgdb_connect(1);
}
#endif /* KGDB */
}
count = scif_fdr_read() & SCFDR2_RECVCNT;
if (count != 0) {
for (;;) {
u_char c = scif_frdr_read();
u_char err = (u_char)(scif_ssr_read() & 0x00ff);
scif_ssr_write(scif_ssr_read()
& ~(SCSSR2_ER | SCSSR2_RDF | SCSSR2_DR));
#ifdef SH4
if (CPU_IS_SH4)
scif_lsr_write(scif_lsr_read()
& ~SCLSR2_ORER);
#endif
if ((cc > 0) && (count > 0)) {
put[0] = c;
put[1] = err;
put += 2;
if (put >= end)
put = sc->sc_rbuf;
cc--;
count--;
} else
break;
}
/*
* Current string of incoming characters ended because
* no more data was available or we ran out of space.
* Schedule a receive event if any data was received.
* If we're out of space, turn off receive interrupts.
*/
sc->sc_rbput = put;
sc->sc_rbavail = cc;
if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
sc->sc_rx_ready = 1;
/*
* See if we are in danger of overflowing a buffer. If
* so, use hardware flow control to ease the pressure.
*/
if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
cc < sc->sc_r_hiwat) {
SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
#if 0
scif_hwiflow(sc);
#endif
}
/*
* If we're out of space, disable receive interrupts
* until the queue has drained a bit.
*/
if (!cc) {
SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
scif_scr_write(scif_scr_read() & ~SCSCR2_RIE);
}
} else {
if (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR)) {
scif_scr_write(scif_scr_read()
& ~(SCSCR2_TIE | SCSCR2_RIE));
delay(10);
scif_scr_write(scif_scr_read()
| SCSCR2_TIE | SCSCR2_RIE);
continue;
}
}
} while (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR));
/*
* Stop output immediately if we lose the output
* flow control signal or carrier detect.
*/
if (ISSET(~msr, sc->sc_msr_mask)) {
sc->sc_tbc = 0;
sc->sc_heldtbc = 0;
DPRINTF(("%s: scifintr\n", device_xname(sc->sc_dev)));
}
sc->sc_st_check = 1;
}
#endif
/*
* Done handling any receive interrupts. See if data can be
* transmitted as well. Schedule tx done event if no data left
* and tty was marked busy.
*/
if (((scif_fdr_read() & SCFDR2_TXCNT) >> 8) != 16) { /* XXX (msaitoh) */
/*
* If we've delayed a parameter change, do it now, and restart
* output.
*/
if (sc->sc_heldchange) {
sc->sc_heldchange = 0;
sc->sc_tbc = sc->sc_heldtbc;
sc->sc_heldtbc = 0;
}
/* Output the next chunk of the contiguous buffer, if any. */
if (sc->sc_tbc > 0) {
int n;
int maxchars;
int i;
n = sc->sc_tbc;
maxchars = sc->sc_fifolen -
((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
if (n > maxchars)
n = maxchars;
for (i = 0; i < n; i++) {
scif_putc(*(sc->sc_tba));
sc->sc_tba++;
}
sc->sc_tbc -= n;
} else {
/* Disable transmit completion interrupts if necessary. */
#if 0
if (ISSET(sc->sc_ier, IER_ETXRDY))
#endif
scif_scr_write(scif_scr_read() & ~SCSCR2_TIE);