[BACK]Return to com.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / ic

Annotation of src/sys/dev/ic/com.c, Revision 1.295.2.3

1.295.2.2  rmind       1: /* $NetBSD$ */
1.38      cgd         2:
1.1       cgd         3: /*-
1.269     ad          4:  * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
1.146     mycroft     5:  * All rights reserved.
1.99      mycroft     6:  *
1.146     mycroft     7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Charles M. Hannum.
1.99      mycroft     9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
1.146     mycroft    19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
1.99      mycroft    30:  */
                     31:
                     32: /*
1.1       cgd        33:  * Copyright (c) 1991 The Regents of the University of California.
                     34:  * All rights reserved.
                     35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.217     agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.38      cgd        60:  *     @(#)com.c       7.5 (Berkeley) 5/16/91
1.1       cgd        61:  */
                     62:
                     63: /*
1.99      mycroft    64:  * COM driver, uses National Semiconductor NS16450/NS16550AF UART
1.116     fvdl       65:  * Supports automatic hardware flow control on StarTech ST16C650A UART
1.1       cgd        66:  */
1.191     lukem      67:
                     68: #include <sys/cdefs.h>
1.295.2.2  rmind      69: __KERNEL_RCSID(0, "$NetBSD$");
1.145     jonathan   70:
1.185     lukem      71: #include "opt_com.h"
1.145     jonathan   72: #include "opt_ddb.h"
1.185     lukem      73: #include "opt_kgdb.h"
1.213     martin     74: #include "opt_lockdebug.h"
                     75: #include "opt_multiprocessor.h"
1.224     simonb     76: #include "opt_ntp.h"
1.115     explorer   77:
                     78: #include "rnd.h"
                     79: #if NRND > 0 && defined(RND_COM)
                     80: #include <sys/rnd.h>
                     81: #endif
                     82:
1.227     thorpej    83: /* The COM16650 option was renamed to COM_16650. */
                     84: #ifdef COM16650
                     85: #error Obsolete COM16650 option; use COM_16650 instead.
                     86: #endif
                     87:
1.186     uwe        88: /*
                     89:  * Override cnmagic(9) macro before including <sys/systm.h>.
                     90:  * We need to know if cn_check_magic triggered debugger, so set a flag.
                     91:  * Callers of cn_check_magic must declare int cn_trapped = 0;
                     92:  * XXX: this is *ugly*!
                     93:  */
                     94: #define cn_trap()                              \
                     95:        do {                                    \
                     96:                console_debugger();             \
                     97:                cn_trapped = 1;                 \
                     98:        } while (/* CONSTCOND */ 0)
                     99:
1.14      mycroft   100: #include <sys/param.h>
                    101: #include <sys/systm.h>
                    102: #include <sys/ioctl.h>
                    103: #include <sys/select.h>
1.234     ws        104: #include <sys/poll.h>
1.14      mycroft   105: #include <sys/tty.h>
                    106: #include <sys/proc.h>
                    107: #include <sys/conf.h>
                    108: #include <sys/file.h>
                    109: #include <sys/uio.h>
                    110: #include <sys/kernel.h>
                    111: #include <sys/syslog.h>
1.21      mycroft   112: #include <sys/device.h>
1.127     mycroft   113: #include <sys/malloc.h>
1.144     jonathan  114: #include <sys/timepps.h>
1.149     thorpej   115: #include <sys/vnode.h>
1.243     elad      116: #include <sys/kauth.h>
1.263     ad        117: #include <sys/intr.h>
1.14      mycroft   118:
1.265     ad        119: #include <sys/bus.h>
1.14      mycroft   120:
1.113     thorpej   121: #include <dev/ic/comreg.h>
                    122: #include <dev/ic/comvar.h>
1.60      cgd       123: #include <dev/ic/ns16550reg.h>
1.116     fvdl      124: #include <dev/ic/st16650reg.h>
1.65      christos  125: #ifdef COM_HAYESP
                    126: #include <dev/ic/hayespreg.h>
                    127: #endif
1.62      mycroft   128: #define        com_lcr com_cfcr
1.106     drochner  129: #include <dev/cons.h>
1.14      mycroft   130:
1.247     gdamore   131: #ifdef COM_REGMAP
                    132: #define        CSR_WRITE_1(r, o, v)    \
                    133:        bus_space_write_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
                    134: #define        CSR_READ_1(r, o)        \
                    135:        bus_space_read_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
                    136: #define        CSR_WRITE_2(r, o, v)    \
                    137:        bus_space_write_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
                    138: #define        CSR_READ_2(r, o)        \
                    139:        bus_space_read_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
                    140: #define        CSR_WRITE_MULTI(r, o, p, n)     \
                    141:        bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], p, n)
                    142: #else
                    143: #define        CSR_WRITE_1(r, o, v)    \
                    144:        bus_space_write_1((r)->cr_iot, (r)->cr_ioh, o, v)
                    145: #define        CSR_READ_1(r, o)        \
                    146:        bus_space_read_1((r)->cr_iot, (r)->cr_ioh, o)
                    147: #define        CSR_WRITE_2(r, o, v)    \
                    148:        bus_space_write_2((r)->cr_iot, (r)->cr_ioh, o, v)
                    149: #define        CSR_READ_2(r, o)        \
                    150:        bus_space_read_2((r)->cr_iot, (r)->cr_ioh, o)
                    151: #define        CSR_WRITE_MULTI(r, o, p, n)     \
                    152:        bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, o, p, n)
1.65      christos  153: #endif
1.102     thorpej   154:
1.247     gdamore   155:
1.197     simonb    156: static void com_enable_debugport(struct com_softc *);
1.186     uwe       157:
1.197     simonb    158: void   com_config(struct com_softc *);
                    159: void   com_shutdown(struct com_softc *);
1.210     thorpej   160: int    comspeed(long, long, int);
1.197     simonb    161: static u_char  cflag2lcr(tcflag_t);
                    162: int    comparam(struct tty *, struct termios *);
                    163: void   comstart(struct tty *);
                    164: int    comhwiflow(struct tty *, int);
                    165:
                    166: void   com_loadchannelregs(struct com_softc *);
                    167: void   com_hwiflow(struct com_softc *);
                    168: void   com_break(struct com_softc *, int);
                    169: void   com_modem(struct com_softc *, int);
                    170: void   tiocm_to_com(struct com_softc *, u_long, int);
                    171: int    com_to_tiocm(struct com_softc *);
                    172: void   com_iflush(struct com_softc *);
1.80      christos  173:
1.247     gdamore   174: int    com_common_getc(dev_t, struct com_regs *);
1.289     dyoung    175: static void    com_common_putc(dev_t, struct com_regs *, int);
1.102     thorpej   176:
1.247     gdamore   177: int    cominit(struct com_regs *, int, int, int, tcflag_t);
1.187     simonb    178:
1.289     dyoung    179: static int comcnreattach(void);
                    180:
1.197     simonb    181: int    comcngetc(dev_t);
                    182: void   comcnputc(dev_t, int);
                    183: void   comcnpollc(dev_t, int);
1.80      christos  184:
1.99      mycroft   185: #define        integrate       static inline
1.197     simonb    186: void   comsoft(void *);
                    187: integrate void com_rxsoft(struct com_softc *, struct tty *);
                    188: integrate void com_txsoft(struct com_softc *, struct tty *);
                    189: integrate void com_stsoft(struct com_softc *, struct tty *);
                    190: integrate void com_schedrx(struct com_softc *);
                    191: void   comdiag(void *);
1.127     mycroft   192:
1.130     thorpej   193: extern struct cfdriver com_cd;
1.76      thorpej   194:
1.199     gehenna   195: dev_type_open(comopen);
                    196: dev_type_close(comclose);
                    197: dev_type_read(comread);
                    198: dev_type_write(comwrite);
                    199: dev_type_ioctl(comioctl);
                    200: dev_type_stop(comstop);
                    201: dev_type_tty(comtty);
                    202: dev_type_poll(compoll);
                    203:
1.289     dyoung    204: static struct comcons_info comcons_info;
                    205:
                    206: /*
                    207:  * Following are all routines needed for COM to act as console
                    208:  */
                    209: static struct consdev comcons = {
                    210:        NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL, NULL, NULL,
                    211:        NODEV, CN_NORMAL
                    212: };
                    213:
                    214:
1.199     gehenna   215: const struct cdevsw com_cdevsw = {
                    216:        comopen, comclose, comread, comwrite, comioctl,
1.200     jdolecek  217:        comstop, comtty, compoll, nommap, ttykqfilter, D_TTY
1.199     gehenna   218: };
                    219:
1.127     mycroft   220: /*
                    221:  * Make this an option variable one can patch.
                    222:  * But be warned:  this must be a power of 2!
                    223:  */
                    224: u_int com_rbuf_size = COM_RING_SIZE;
                    225:
                    226: /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
                    227: u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
                    228: u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
                    229:
1.247     gdamore   230: static int comconsattached;
1.186     uwe       231: static struct cnm_state com_cnm_state;
1.99      mycroft   232:
1.1       cgd       233: #ifdef KGDB
1.102     thorpej   234: #include <sys/kgdb.h>
1.106     drochner  235:
1.247     gdamore   236: static struct com_regs comkgdbregs;
1.106     drochner  237: static int com_kgdb_attached;
1.102     thorpej   238:
1.197     simonb    239: int    com_kgdb_getc(void *);
                    240: void   com_kgdb_putc(void *, int);
1.102     thorpej   241: #endif /* KGDB */
1.1       cgd       242:
1.247     gdamore   243: #ifdef COM_REGMAP
                    244: /* initializer for typical 16550-ish hardware */
                    245: #define        COM_REG_16550   { \
                    246:        com_data, com_data, com_dlbl, com_dlbh, com_ier, com_iir, com_fifo, \
                    247:        com_efr, com_lcr, com_mcr, com_lsr, com_msr }
                    248:
                    249: const bus_size_t com_std_map[16] = COM_REG_16550;
                    250: #endif /* COM_REGMAP */
                    251:
1.149     thorpej   252: #define        COMUNIT_MASK    0x7ffff
                    253: #define        COMDIALOUT_MASK 0x80000
                    254:
                    255: #define        COMUNIT(x)      (minor(x) & COMUNIT_MASK)
                    256: #define        COMDIALOUT(x)   (minor(x) & COMDIALOUT_MASK)
                    257:
                    258: #define        COM_ISALIVE(sc) ((sc)->enabled != 0 && \
1.276     cube      259:                         device_is_active((sc)->sc_dev))
1.1       cgd       260:
1.160     thorpej   261: #define        BR      BUS_SPACE_BARRIER_READ
                    262: #define        BW      BUS_SPACE_BARRIER_WRITE
1.247     gdamore   263: #define COM_BARRIER(r, f) \
                    264:        bus_space_barrier((r)->cr_iot, (r)->cr_ioh, 0, (r)->cr_nports, (f))
1.160     thorpej   265:
1.210     thorpej   266: /*ARGSUSED*/
1.21      mycroft   267: int
1.256     christos  268: comspeed(long speed, long frequency, int type)
1.1       cgd       269: {
1.21      mycroft   270: #define        divrnd(n, q)    (((n)*2/(q)+1)/2)       /* divide and round off */
                    271:
                    272:        int x, err;
1.281     matt      273:        int divisor = 16;
                    274:
                    275:        if ((type == COM_TYPE_OMAP) && (speed > 230400)) {
                    276:            divisor = 13;
                    277:        }
1.21      mycroft   278:
1.99      mycroft   279: #if 0
1.21      mycroft   280:        if (speed == 0)
1.99      mycroft   281:                return (0);
                    282: #endif
                    283:        if (speed <= 0)
                    284:                return (-1);
1.281     matt      285:        x = divrnd(frequency / divisor, speed);
1.21      mycroft   286:        if (x <= 0)
1.99      mycroft   287:                return (-1);
1.281     matt      288:        err = divrnd(((quad_t)frequency) * 1000 / divisor, speed * x) - 1000;
1.21      mycroft   289:        if (err < 0)
                    290:                err = -err;
                    291:        if (err > COM_TOLERANCE)
1.99      mycroft   292:                return (-1);
                    293:        return (x);
1.21      mycroft   294:
1.172     thorpej   295: #undef divrnd
1.21      mycroft   296: }
                    297:
1.99      mycroft   298: #ifdef COM_DEBUG
1.101     mycroft   299: int    com_debug = 0;
                    300:
1.235     kleink    301: void comstatus(struct com_softc *, const char *);
1.99      mycroft   302: void
1.235     kleink    303: comstatus(struct com_softc *sc, const char *str)
1.99      mycroft   304: {
                    305:        struct tty *tp = sc->sc_tty;
                    306:
1.277     cube      307:        aprint_normal_dev(sc->sc_dev,
                    308:            "%s %cclocal  %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
                    309:            str,
1.218     christos  310:            ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
                    311:            ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
                    312:            ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
                    313:            ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
                    314:            sc->sc_tx_stopped ? '+' : '-');
1.99      mycroft   315:
1.277     cube      316:        aprint_normal_dev(sc->sc_dev,
                    317:            "%s %ccrtscts %ccts %cts_ttstop  %crts rx_flags=0x%x\n",
                    318:            str,
1.218     christos  319:            ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
                    320:            ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
                    321:            ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
                    322:            ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
1.101     mycroft   323:            sc->sc_rx_flags);
1.99      mycroft   324: }
                    325: #endif
                    326:
1.21      mycroft   327: int
1.247     gdamore   328: com_probe_subr(struct com_regs *regs)
1.21      mycroft   329: {
                    330:
1.1       cgd       331:        /* force access to id reg */
1.247     gdamore   332:        CSR_WRITE_1(regs, COM_REG_LCR, LCR_8BITS);
                    333:        CSR_WRITE_1(regs, COM_REG_IIR, 0);
                    334:        if ((CSR_READ_1(regs, COM_REG_LCR) != LCR_8BITS) ||
                    335:            (CSR_READ_1(regs, COM_REG_IIR) & 0x38))
1.99      mycroft   336:                return (0);
1.21      mycroft   337:
1.99      mycroft   338:        return (1);
1.1       cgd       339: }
                    340:
1.65      christos  341: int
1.247     gdamore   342: comprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
1.64      christos  343: {
1.247     gdamore   344:        struct com_regs regs;
1.64      christos  345:
1.247     gdamore   346:        regs.cr_iot = iot;
                    347:        regs.cr_ioh = ioh;
                    348: #ifdef COM_REGMAP
1.287     yamt      349:        memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
1.247     gdamore   350: #endif
1.64      christos  351:
1.247     gdamore   352:        return com_probe_subr(&regs);
1.64      christos  353: }
                    354:
1.264     ad        355: /*
                    356:  * No locking in this routine; it is only called during attach,
                    357:  * or with the port already locked.
                    358:  */
1.104     drochner  359: static void
1.197     simonb    360: com_enable_debugport(struct com_softc *sc)
1.104     drochner  361: {
1.263     ad        362:
1.104     drochner  363:        /* Turn on line break interrupt, set carrier. */
                    364:        sc->sc_ier = IER_ERXRDY;
1.209     thorpej   365:        if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       366:                sc->sc_ier |= IER_EUART | IER_ERXTOUT;
1.247     gdamore   367:        CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
1.104     drochner  368:        SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
1.247     gdamore   369:        CSR_WRITE_1(&sc->sc_regs, COM_REG_MCR, sc->sc_mcr);
1.104     drochner  370: }
1.1       cgd       371:
1.29      mycroft   372: void
1.197     simonb    373: com_attach_subr(struct com_softc *sc)
1.29      mycroft   374: {
1.247     gdamore   375:        struct com_regs *regsp = &sc->sc_regs;
1.127     mycroft   376:        struct tty *tp;
1.227     thorpej   377: #ifdef COM_16650
1.116     fvdl      378:        u_int8_t lcr;
1.118     fvdl      379: #endif
1.208     scw       380:        const char *fifo_msg = NULL;
1.117     mycroft   381:
1.257     uwe       382:        aprint_naive("\n");
                    383:
1.260     ad        384:        callout_init(&sc->sc_diag_callout, 0);
1.267     ad        385:        mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
1.170     thorpej   386:
1.117     mycroft   387:        /* Disable interrupts before configuring the device. */
1.209     thorpej   388:        if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       389:                sc->sc_ier = IER_EUART;
                    390:        else
                    391:                sc->sc_ier = 0;
1.1       cgd       392:
1.247     gdamore   393:        CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
                    394:
1.295.2.1  rmind     395:        if (bus_space_is_equal(regsp->cr_iot, comcons_info.regs.cr_iot) &&
1.289     dyoung    396:            regsp->cr_iobase == comcons_info.regs.cr_iobase) {
1.105     drochner  397:                comconsattached = 1;
                    398:
1.289     dyoung    399:                if (cn_tab == NULL && comcnreattach() != 0) {
1.294     tsutsui   400:                        printf("can't re-init serial console @%lx\n",
                    401:                            (u_long)comcons_info.regs.cr_iobase);
1.289     dyoung    402:                }
                    403:
1.96      mycroft   404:                /* Make sure the console is always "hardwired". */
1.226     thorpej   405:                delay(10000);                   /* wait for output to finish */
1.75      cgd       406:                SET(sc->sc_hwflags, COM_HW_CONSOLE);
1.99      mycroft   407:                SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
1.75      cgd       408:        }
1.26      cgd       409:
1.247     gdamore   410:        /* Probe for FIFO */
                    411:        switch (sc->sc_type) {
                    412:        case COM_TYPE_HAYESP:
                    413:                goto fifodone;
                    414:
                    415:        case COM_TYPE_AU1x00:
                    416:                sc->sc_fifolen = 16;
                    417:                fifo_msg = "Au1X00 UART, working fifo";
                    418:                SET(sc->sc_hwflags, COM_HW_FIFO);
                    419:                goto fifodelay;
1.281     matt      420:
1.286     matt      421:        case COM_TYPE_16550_NOERS:
                    422:                sc->sc_fifolen = 16;
                    423:                fifo_msg = "ns16650, no ERS, working fifo";
                    424:                SET(sc->sc_hwflags, COM_HW_FIFO);
                    425:                goto fifodelay;
                    426:
1.281     matt      427:        case COM_TYPE_OMAP:
                    428:                sc->sc_fifolen = 64;
                    429:                fifo_msg = "OMAP UART, working fifo";
                    430:                SET(sc->sc_hwflags, COM_HW_FIFO);
                    431:                goto fifodelay;
                    432:        }
1.99      mycroft   433:
                    434:        sc->sc_fifolen = 1;
1.1       cgd       435:        /* look for a NS 16550AF UART with FIFOs */
1.247     gdamore   436:        CSR_WRITE_1(regsp, COM_REG_FIFO,
1.21      mycroft   437:            FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
1.20      mycroft   438:        delay(100);
1.247     gdamore   439:        if (ISSET(CSR_READ_1(regsp, COM_REG_IIR), IIR_FIFO_MASK)
1.99      mycroft   440:            == IIR_FIFO_MASK)
1.247     gdamore   441:                if (ISSET(CSR_READ_1(regsp, COM_REG_FIFO), FIFO_TRIGGER_14)
1.99      mycroft   442:                    == FIFO_TRIGGER_14) {
1.62      mycroft   443:                        SET(sc->sc_hwflags, COM_HW_FIFO);
1.116     fvdl      444:
1.227     thorpej   445: #ifdef COM_16650
1.116     fvdl      446:                        /*
                    447:                         * IIR changes into the EFR if LCR is set to LCR_EERS
                    448:                         * on 16650s. We also know IIR != 0 at this point.
                    449:                         * Write 0 into the EFR, and read it. If the result
                    450:                         * is 0, we have a 16650.
                    451:                         *
                    452:                         * Older 16650s were broken; the test to detect them
                    453:                         * is taken from the Linux driver. Apparently
                    454:                         * setting DLAB enable gives access to the EFR on
                    455:                         * these chips.
                    456:                         */
1.286     matt      457:                        lcr = CSR_READ_1(regsp, COM_REG_LCR);
                    458:                        CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
                    459:                        CSR_WRITE_1(regsp, COM_REG_EFR, 0);
                    460:                        if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
                    461:                                CSR_WRITE_1(regsp, COM_REG_LCR,
                    462:                                    lcr | LCR_DLAB);
1.247     gdamore   463:                                if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
1.286     matt      464:                                        CLR(sc->sc_hwflags, COM_HW_FIFO);
                    465:                                        sc->sc_fifolen = 0;
                    466:                                } else {
                    467:                                        SET(sc->sc_hwflags, COM_HW_FLOW);
                    468:                                        sc->sc_fifolen = 32;
                    469:                                }
1.118     fvdl      470:                        } else
                    471: #endif
1.116     fvdl      472:                                sc->sc_fifolen = 16;
                    473:
1.227     thorpej   474: #ifdef COM_16650
1.286     matt      475:                        CSR_WRITE_1(regsp, COM_REG_LCR, lcr);
1.119     drochner  476:                        if (sc->sc_fifolen == 0)
1.208     scw       477:                                fifo_msg = "st16650, broken fifo";
1.119     drochner  478:                        else if (sc->sc_fifolen == 32)
1.208     scw       479:                                fifo_msg = "st16650a, working fifo";
1.119     drochner  480:                        else
1.118     fvdl      481: #endif
1.208     scw       482:                                fifo_msg = "ns16550a, working fifo";
1.21      mycroft   483:                } else
1.208     scw       484:                        fifo_msg = "ns16550, broken fifo";
1.21      mycroft   485:        else
1.208     scw       486:                fifo_msg = "ns8250 or ns16450, no fifo";
1.247     gdamore   487:        CSR_WRITE_1(regsp, COM_REG_FIFO, 0);
                    488: fifodelay:
1.208     scw       489:        /*
                    490:         * Some chips will clear down both Tx and Rx FIFOs when zero is
                    491:         * written to com_fifo. If this chip is the console, writing zero
                    492:         * results in some of the chip/FIFO description being lost, so delay
                    493:         * printing it until now.
                    494:         */
                    495:        delay(10);
                    496:        aprint_normal(": %s\n", fifo_msg);
1.166     soda      497:        if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
                    498:                sc->sc_fifolen = 1;
1.276     cube      499:                aprint_normal_dev(sc->sc_dev, "txfifo disabled\n");
1.166     soda      500:        }
1.247     gdamore   501:
                    502: fifodone:
1.21      mycroft   503:
1.295.2.3! rmind     504:        tp = tty_alloc();
1.127     mycroft   505:        tp->t_oproc = comstart;
                    506:        tp->t_param = comparam;
                    507:        tp->t_hwiflow = comhwiflow;
                    508:
                    509:        sc->sc_tty = tp;
1.147     thorpej   510:        sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
1.182     sommerfe  511:        sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
                    512:        sc->sc_rbavail = com_rbuf_size;
1.147     thorpej   513:        if (sc->sc_rbuf == NULL) {
1.276     cube      514:                aprint_error_dev(sc->sc_dev,
                    515:                    "unable to allocate ring buffer\n");
1.147     thorpej   516:                return;
                    517:        }
1.127     mycroft   518:        sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
1.147     thorpej   519:
                    520:        tty_attach(tp);
                    521:
1.99      mycroft   522:        if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
                    523:                SET(sc->sc_mcr, MCR_IENABLE);
1.30      mycroft   524:
1.96      mycroft   525:        if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1.106     drochner  526:                int maj;
                    527:
                    528:                /* locate the major number */
1.199     gehenna   529:                maj = cdevsw_lookup_major(&com_cdevsw);
1.106     drochner  530:
1.242     thorpej   531:                tp->t_dev = cn_tab->cn_dev = makedev(maj,
1.276     cube      532:                                                     device_unit(sc->sc_dev));
1.131     marc      533:
1.276     cube      534:                aprint_normal_dev(sc->sc_dev, "console\n");
1.96      mycroft   535:        }
                    536:
1.1       cgd       537: #ifdef KGDB
1.102     thorpej   538:        /*
                    539:         * Allow kgdb to "take over" this port.  If this is
1.206     briggs    540:         * not the console and is the kgdb device, it has
                    541:         * exclusive use.  If it's the console _and_ the
                    542:         * kgdb device, it doesn't.
1.102     thorpej   543:         */
1.295.2.1  rmind     544:        if (bus_space_is_equal(regsp->cr_iot, comkgdbregs.cr_iot) &&
1.247     gdamore   545:            regsp->cr_iobase == comkgdbregs.cr_iobase) {
1.206     briggs    546:                if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
                    547:                        com_kgdb_attached = 1;
1.106     drochner  548:
1.206     briggs    549:                        SET(sc->sc_hwflags, COM_HW_KGDB);
                    550:                }
1.276     cube      551:                aprint_normal_dev(sc->sc_dev, "kgdb\n");
1.103     drochner  552:        }
1.99      mycroft   553: #endif
                    554:
1.263     ad        555:        sc->sc_si = softint_establish(SOFTINT_SERIAL, comsoft, sc);
1.115     explorer  556:
                    557: #if NRND > 0 && defined(RND_COM)
1.277     cube      558:        rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
1.155     explorer  559:                          RND_TYPE_TTY, 0);
1.115     explorer  560: #endif
1.131     marc      561:
                    562:        /* if there are no enable/disable functions, assume the device
                    563:           is always enabled */
                    564:        if (!sc->enable)
                    565:                sc->enabled = 1;
                    566:
                    567:        com_config(sc);
1.132     cgd       568:
                    569:        SET(sc->sc_hwflags, COM_HW_DEV_OK);
1.131     marc      570: }
                    571:
                    572: void
1.197     simonb    573: com_config(struct com_softc *sc)
1.131     marc      574: {
1.247     gdamore   575:        struct com_regs *regsp = &sc->sc_regs;
1.131     marc      576:
                    577:        /* Disable interrupts before configuring the device. */
1.209     thorpej   578:        if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       579:                sc->sc_ier = IER_EUART;
                    580:        else
                    581:                sc->sc_ier = 0;
1.247     gdamore   582:        CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
                    583:        (void) CSR_READ_1(regsp, COM_REG_IIR);
1.131     marc      584:
                    585: #ifdef COM_HAYESP
                    586:        /* Look for a Hayes ESP board. */
1.209     thorpej   587:        if (sc->sc_type == COM_TYPE_HAYESP) {
1.131     marc      588:
                    589:                /* Set 16550 compatibility mode */
1.258     cube      590:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
1.131     marc      591:                                  HAYESP_SETMODE);
1.258     cube      592:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      593:                                  HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
                    594:                                  HAYESP_MODE_SCALE);
                    595:
                    596:                /* Set RTS/CTS flow control */
1.258     cube      597:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
1.131     marc      598:                                  HAYESP_SETFLOWTYPE);
1.258     cube      599:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      600:                                  HAYESP_FLOW_RTS);
1.258     cube      601:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      602:                                  HAYESP_FLOW_CTS);
                    603:
                    604:                /* Set flow control levels */
1.258     cube      605:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
1.131     marc      606:                                  HAYESP_SETRXFLOW);
1.258     cube      607:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      608:                                  HAYESP_HIBYTE(HAYESP_RXHIWMARK));
1.258     cube      609:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      610:                                  HAYESP_LOBYTE(HAYESP_RXHIWMARK));
1.258     cube      611:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      612:                                  HAYESP_HIBYTE(HAYESP_RXLOWMARK));
1.258     cube      613:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.131     marc      614:                                  HAYESP_LOBYTE(HAYESP_RXLOWMARK));
                    615:        }
                    616: #endif
                    617:
1.186     uwe       618:        if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE|COM_HW_KGDB))
1.131     marc      619:                com_enable_debugport(sc);
1.1       cgd       620: }
                    621:
1.292     dyoung    622: #if 0
                    623: static int
                    624: comcngetc_detached(dev_t dev)
                    625: {
                    626:        return 0;
                    627: }
                    628:
                    629: static void
                    630: comcnputc_detached(dev_t dev, int c)
                    631: {
                    632: }
                    633: #endif
                    634:
1.149     thorpej   635: int
1.274     dyoung    636: com_detach(device_t self, int flags)
1.149     thorpej   637: {
1.274     dyoung    638:        struct com_softc *sc = device_private(self);
1.149     thorpej   639:        int maj, mn;
                    640:
1.289     dyoung    641:        if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
                    642:                return EBUSY;
                    643:
                    644:         if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE) &&
                    645:            (flags & DETACH_SHUTDOWN) != 0)
1.272     dyoung    646:                return EBUSY;
                    647:
1.289     dyoung    648:        if (sc->disable != NULL && sc->enabled != 0) {
                    649:                (*sc->disable)(sc);
                    650:                sc->enabled = 0;
                    651:        }
                    652:
                    653:         if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
                    654:                comconsattached = 0;
                    655:                cn_tab = NULL;
                    656:        }
                    657:
1.149     thorpej   658:        /* locate the major number */
1.199     gehenna   659:        maj = cdevsw_lookup_major(&com_cdevsw);
1.149     thorpej   660:
                    661:        /* Nuke the vnodes for any open instances. */
1.242     thorpej   662:        mn = device_unit(self);
1.149     thorpej   663:        vdevgone(maj, mn, mn, VCHR);
                    664:
                    665:        mn |= COMDIALOUT_MASK;
                    666:        vdevgone(maj, mn, mn, VCHR);
                    667:
1.196     christos  668:        if (sc->sc_rbuf == NULL) {
                    669:                /*
                    670:                 * Ring buffer allocation failed in the com_attach_subr,
                    671:                 * only the tty is allocated, and nothing else.
                    672:                 */
1.295.2.3! rmind     673:                tty_free(sc->sc_tty);
1.196     christos  674:                return 0;
                    675:        }
1.232     perry     676:
1.149     thorpej   677:        /* Free the receive buffer. */
                    678:        free(sc->sc_rbuf, M_DEVBUF);
                    679:
                    680:        /* Detach and free the tty. */
                    681:        tty_detach(sc->sc_tty);
1.295.2.3! rmind     682:        tty_free(sc->sc_tty);
1.149     thorpej   683:
                    684:        /* Unhook the soft interrupt handler. */
1.263     ad        685:        softint_disestablish(sc->sc_si);
1.149     thorpej   686:
                    687: #if NRND > 0 && defined(RND_COM)
                    688:        /* Unhook the entropy source. */
                    689:        rnd_detach_source(&sc->rnd_source);
                    690: #endif
1.273     dyoung    691:        callout_destroy(&sc->sc_diag_callout);
1.149     thorpej   692:
1.271     ad        693:        /* Destroy the lock. */
                    694:        mutex_destroy(&sc->sc_lock);
                    695:
1.149     thorpej   696:        return (0);
                    697: }
                    698:
1.141     mycroft   699: void
1.197     simonb    700: com_shutdown(struct com_softc *sc)
1.141     mycroft   701: {
                    702:        struct tty *tp = sc->sc_tty;
                    703:
1.263     ad        704:        mutex_spin_enter(&sc->sc_lock);
1.141     mycroft   705:
                    706:        /* If we were asserting flow control, then deassert it. */
                    707:        SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
                    708:        com_hwiflow(sc);
                    709:
                    710:        /* Clear any break condition set with TIOCSBRK. */
                    711:        com_break(sc, 0);
                    712:
                    713:        /*
                    714:         * Hang up if necessary.  Wait a bit, so the other side has time to
                    715:         * notice even if we immediately open the port again.
1.175     sommerfe  716:         * Avoid tsleeping above splhigh().
1.141     mycroft   717:         */
                    718:        if (ISSET(tp->t_cflag, HUPCL)) {
                    719:                com_modem(sc, 0);
1.263     ad        720:                mutex_spin_exit(&sc->sc_lock);
                    721:                /* XXX will only timeout */
                    722:                (void) kpause(ttclos, false, hz, NULL);
                    723:                mutex_spin_enter(&sc->sc_lock);
1.141     mycroft   724:        }
                    725:
                    726:        /* Turn off interrupts. */
1.208     scw       727:        if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1.141     mycroft   728:                sc->sc_ier = IER_ERXRDY; /* interrupt on break */
1.209     thorpej   729:                if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       730:                        sc->sc_ier |= IER_ERXTOUT;
                    731:        } else
1.141     mycroft   732:                sc->sc_ier = 0;
1.208     scw       733:
1.209     thorpej   734:        if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       735:                sc->sc_ier |= IER_EUART;
                    736:
1.247     gdamore   737:        CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
1.141     mycroft   738:
1.269     ad        739:        mutex_spin_exit(&sc->sc_lock);
                    740:
1.141     mycroft   741:        if (sc->disable) {
                    742: #ifdef DIAGNOSTIC
                    743:                if (!sc->enabled)
                    744:                        panic("com_shutdown: not enabled?");
                    745: #endif
                    746:                (*sc->disable)(sc);
                    747:                sc->enabled = 0;
                    748:        }
                    749: }
                    750:
1.21      mycroft   751: int
1.256     christos  752: comopen(dev_t dev, int flag, int mode, struct lwp *l)
1.1       cgd       753: {
1.21      mycroft   754:        struct com_softc *sc;
                    755:        struct tty *tp;
1.263     ad        756:        int s;
1.142     mycroft   757:        int error;
1.173     thorpej   758:
1.276     cube      759:        sc = device_lookup_private(&com_cd, COMUNIT(dev));
1.173     thorpej   760:        if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
1.177     eeh       761:                sc->sc_rbuf == NULL)
1.99      mycroft   762:                return (ENXIO);
1.21      mycroft   763:
1.276     cube      764:        if (!device_is_active(sc->sc_dev))
1.149     thorpej   765:                return (ENXIO);
                    766:
1.102     thorpej   767: #ifdef KGDB
                    768:        /*
                    769:         * If this is the kgdb port, no other use is permitted.
                    770:         */
                    771:        if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
                    772:                return (EBUSY);
                    773: #endif
                    774:
1.120     mycroft   775:        tp = sc->sc_tty;
1.21      mycroft   776:
1.253     elad      777:        if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
1.99      mycroft   778:                return (EBUSY);
                    779:
                    780:        s = spltty();
                    781:
                    782:        /*
                    783:         * Do the following iff this is a first open.
                    784:         */
1.141     mycroft   785:        if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1.99      mycroft   786:                struct termios t;
                    787:
1.127     mycroft   788:                tp->t_dev = dev;
                    789:
1.120     mycroft   790:
1.131     marc      791:                if (sc->enable) {
                    792:                        if ((*sc->enable)(sc)) {
1.134     enami     793:                                splx(s);
1.276     cube      794:                                aprint_error_dev(sc->sc_dev,
                    795:                                    "device enable failed\n");
1.131     marc      796:                                return (EIO);
                    797:                        }
1.269     ad        798:                        mutex_spin_enter(&sc->sc_lock);
1.131     marc      799:                        sc->enabled = 1;
                    800:                        com_config(sc);
1.269     ad        801:                } else {
                    802:                        mutex_spin_enter(&sc->sc_lock);
1.131     marc      803:                }
                    804:
1.99      mycroft   805:                /* Turn on interrupts. */
1.295.2.3! rmind     806:                sc->sc_ier = IER_ERXRDY | IER_ERLS;
        !           807:                if (!ISSET(tp->t_cflag, CLOCAL))
        !           808:                        sc->sc_ier |= IER_EMSC;
        !           809:
1.209     thorpej   810:                if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw       811:                        sc->sc_ier |= IER_EUART | IER_ERXTOUT;
1.247     gdamore   812:                CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
1.99      mycroft   813:
                    814:                /* Fetch the current modem control status, needed later. */
1.247     gdamore   815:                sc->sc_msr = CSR_READ_1(&sc->sc_regs, COM_REG_MSR);
1.99      mycroft   816:
1.144     jonathan  817:                /* Clear PPS capture state on first open. */
1.279     ad        818:                mutex_spin_enter(&timecounter_lock);
1.244     kardel    819:                memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
                    820:                sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
                    821:                pps_init(&sc->sc_pps_state);
1.279     ad        822:                mutex_spin_exit(&timecounter_lock);
1.144     jonathan  823:
1.263     ad        824:                mutex_spin_exit(&sc->sc_lock);
1.99      mycroft   825:
                    826:                /*
                    827:                 * Initialize the termios status to the defaults.  Add in the
                    828:                 * sticky bits from TIOCSFLAGS.
                    829:                 */
1.98      mycroft   830:                if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1.289     dyoung    831:                        t.c_ospeed = comcons_info.rate;
                    832:                        t.c_cflag = comcons_info.cflag;
1.98      mycroft   833:                } else {
1.99      mycroft   834:                        t.c_ospeed = TTYDEF_SPEED;
                    835:                        t.c_cflag = TTYDEF_CFLAG;
1.98      mycroft   836:                }
1.237     dsl       837:                t.c_ispeed = t.c_ospeed;
1.99      mycroft   838:                if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
                    839:                        SET(t.c_cflag, CLOCAL);
                    840:                if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
                    841:                        SET(t.c_cflag, CRTSCTS);
                    842:                if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
                    843:                        SET(t.c_cflag, MDMBUF);
1.129     mycroft   844:                /* Make sure comparam() will do something. */
                    845:                tp->t_ospeed = 0;
1.120     mycroft   846:                (void) comparam(tp, &t);
1.99      mycroft   847:                tp->t_iflag = TTYDEF_IFLAG;
                    848:                tp->t_oflag = TTYDEF_OFLAG;
1.16      ws        849:                tp->t_lflag = TTYDEF_LFLAG;
1.99      mycroft   850:                ttychars(tp);
1.1       cgd       851:                ttsetwater(tp);
1.21      mycroft   852:
1.263     ad        853:                mutex_spin_enter(&sc->sc_lock);
1.136     mycroft   854:
1.99      mycroft   855:                /*
                    856:                 * Turn on DTR.  We must always do this, even if carrier is not
                    857:                 * present, because otherwise we'd have to use TIOCSDTR
1.121     mycroft   858:                 * immediately after setting CLOCAL, which applications do not
                    859:                 * expect.  We always assert DTR while the device is open
                    860:                 * unless explicitly requested to deassert it.
1.99      mycroft   861:                 */
                    862:                com_modem(sc, 1);
1.65      christos  863:
1.99      mycroft   864:                /* Clear the input ring, and unblock. */
1.127     mycroft   865:                sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
                    866:                sc->sc_rbavail = com_rbuf_size;
1.99      mycroft   867:                com_iflush(sc);
1.101     mycroft   868:                CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
                    869:                com_hwiflow(sc);
1.65      christos  870:
1.99      mycroft   871: #ifdef COM_DEBUG
1.101     mycroft   872:                if (com_debug)
                    873:                        comstatus(sc, "comopen  ");
1.65      christos  874: #endif
1.21      mycroft   875:
1.263     ad        876:                mutex_spin_exit(&sc->sc_lock);
1.99      mycroft   877:        }
1.232     perry     878:
1.143     mycroft   879:        splx(s);
1.21      mycroft   880:
1.143     mycroft   881:        error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
                    882:        if (error)
                    883:                goto bad;
1.141     mycroft   884:
1.181     eeh       885:        error = (*tp->t_linesw->l_open)(dev, tp);
1.139     enami     886:        if (error)
1.141     mycroft   887:                goto bad;
1.139     enami     888:
1.141     mycroft   889:        return (0);
1.139     enami     890:
1.141     mycroft   891: bad:
                    892:        if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
                    893:                /*
                    894:                 * We failed to open the device, and nobody else had it opened.
                    895:                 * Clean up the state as appropriate.
                    896:                 */
                    897:                com_shutdown(sc);
                    898:        }
1.139     enami     899:
1.99      mycroft   900:        return (error);
1.1       cgd       901: }
1.232     perry     902:
1.21      mycroft   903: int
1.256     christos  904: comclose(dev_t dev, int flag, int mode, struct lwp *l)
1.1       cgd       905: {
1.276     cube      906:        struct com_softc *sc =
                    907:            device_lookup_private(&com_cd, COMUNIT(dev));
1.50      mycroft   908:        struct tty *tp = sc->sc_tty;
1.57      mycroft   909:
                    910:        /* XXX This is for cons.c. */
1.62      mycroft   911:        if (!ISSET(tp->t_state, TS_ISOPEN))
1.99      mycroft   912:                return (0);
1.21      mycroft   913:
1.181     eeh       914:        (*tp->t_linesw->l_close)(tp, flag);
1.1       cgd       915:        ttyclose(tp);
1.99      mycroft   916:
1.149     thorpej   917:        if (COM_ISALIVE(sc) == 0)
                    918:                return (0);
                    919:
1.143     mycroft   920:        if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
                    921:                /*
                    922:                 * Although we got a last close, the device may still be in
                    923:                 * use; e.g. if this was the dialout node, and there are still
                    924:                 * processes waiting for carrier on the non-dialout node.
                    925:                 */
                    926:                com_shutdown(sc);
                    927:        }
1.120     mycroft   928:
1.99      mycroft   929:        return (0);
1.1       cgd       930: }
1.232     perry     931:
1.21      mycroft   932: int
1.197     simonb    933: comread(dev_t dev, struct uio *uio, int flag)
1.1       cgd       934: {
1.276     cube      935:        struct com_softc *sc =
                    936:            device_lookup_private(&com_cd, COMUNIT(dev));
1.52      mycroft   937:        struct tty *tp = sc->sc_tty;
1.149     thorpej   938:
                    939:        if (COM_ISALIVE(sc) == 0)
                    940:                return (EIO);
1.232     perry     941:
1.181     eeh       942:        return ((*tp->t_linesw->l_read)(tp, uio, flag));
1.1       cgd       943: }
1.232     perry     944:
1.21      mycroft   945: int
1.197     simonb    946: comwrite(dev_t dev, struct uio *uio, int flag)
1.1       cgd       947: {
1.276     cube      948:        struct com_softc *sc =
                    949:            device_lookup_private(&com_cd, COMUNIT(dev));
1.52      mycroft   950:        struct tty *tp = sc->sc_tty;
1.149     thorpej   951:
                    952:        if (COM_ISALIVE(sc) == 0)
                    953:                return (EIO);
1.232     perry     954:
1.181     eeh       955:        return ((*tp->t_linesw->l_write)(tp, uio, flag));
1.184     scw       956: }
                    957:
                    958: int
1.238     christos  959: compoll(dev_t dev, int events, struct lwp *l)
1.184     scw       960: {
1.276     cube      961:        struct com_softc *sc =
                    962:            device_lookup_private(&com_cd, COMUNIT(dev));
1.184     scw       963:        struct tty *tp = sc->sc_tty;
                    964:
                    965:        if (COM_ISALIVE(sc) == 0)
1.234     ws        966:                return (POLLHUP);
1.232     perry     967:
1.238     christos  968:        return ((*tp->t_linesw->l_poll)(tp, events, l));
1.1       cgd       969: }
1.50      mycroft   970:
                    971: struct tty *
1.197     simonb    972: comtty(dev_t dev)
1.50      mycroft   973: {
1.276     cube      974:        struct com_softc *sc =
                    975:            device_lookup_private(&com_cd, COMUNIT(dev));
1.52      mycroft   976:        struct tty *tp = sc->sc_tty;
1.50      mycroft   977:
1.52      mycroft   978:        return (tp);
1.50      mycroft   979: }
1.111     christos  980:
1.21      mycroft   981: int
1.259     christos  982: comioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1.1       cgd       983: {
1.273     dyoung    984:        struct com_softc *sc;
                    985:        struct tty *tp;
1.21      mycroft   986:        int error;
                    987:
1.276     cube      988:        sc = device_lookup_private(&com_cd, COMUNIT(dev));
1.273     dyoung    989:        if (sc == NULL)
                    990:                return ENXIO;
1.149     thorpej   991:        if (COM_ISALIVE(sc) == 0)
                    992:                return (EIO);
                    993:
1.273     dyoung    994:        tp = sc->sc_tty;
                    995:
1.238     christos  996:        error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1.194     atatat    997:        if (error != EPASSTHROUGH)
1.99      mycroft   998:                return (error);
                    999:
1.238     christos 1000:        error = ttioctl(tp, cmd, data, flag, l);
1.194     atatat   1001:        if (error != EPASSTHROUGH)
1.99      mycroft  1002:                return (error);
1.138     mycroft  1003:
                   1004:        error = 0;
1.249     elad     1005:        switch (cmd) {
                   1006:        case TIOCSFLAGS:
1.254     elad     1007:                error = kauth_authorize_device_tty(l->l_cred,
                   1008:                    KAUTH_DEVICE_TTY_PRIVSET, tp);
1.249     elad     1009:                break;
                   1010:        default:
                   1011:                /* nothing */
                   1012:                break;
                   1013:        }
                   1014:        if (error) {
                   1015:                return error;
                   1016:        }
1.1       cgd      1017:
1.263     ad       1018:        mutex_spin_enter(&sc->sc_lock);
1.136     mycroft  1019:
1.1       cgd      1020:        switch (cmd) {
                   1021:        case TIOCSBRK:
1.99      mycroft  1022:                com_break(sc, 1);
1.1       cgd      1023:                break;
1.99      mycroft  1024:
1.1       cgd      1025:        case TIOCCBRK:
1.99      mycroft  1026:                com_break(sc, 0);
1.1       cgd      1027:                break;
1.99      mycroft  1028:
1.1       cgd      1029:        case TIOCSDTR:
1.99      mycroft  1030:                com_modem(sc, 1);
1.1       cgd      1031:                break;
1.99      mycroft  1032:
1.1       cgd      1033:        case TIOCCDTR:
1.99      mycroft  1034:                com_modem(sc, 0);
1.1       cgd      1035:                break;
1.99      mycroft  1036:
                   1037:        case TIOCGFLAGS:
                   1038:                *(int *)data = sc->sc_swflags;
                   1039:                break;
                   1040:
                   1041:        case TIOCSFLAGS:
                   1042:                sc->sc_swflags = *(int *)data;
                   1043:                break;
                   1044:
1.1       cgd      1045:        case TIOCMSET:
                   1046:        case TIOCMBIS:
                   1047:        case TIOCMBIC:
1.153     mycroft  1048:                tiocm_to_com(sc, cmd, *(int *)data);
1.111     christos 1049:                break;
                   1050:
1.153     mycroft  1051:        case TIOCMGET:
                   1052:                *(int *)data = com_to_tiocm(sc);
1.111     christos 1053:                break;
1.144     jonathan 1054:
1.244     kardel   1055:        case PPS_IOC_CREATE:
                   1056:        case PPS_IOC_DESTROY:
                   1057:        case PPS_IOC_GETPARAMS:
                   1058:        case PPS_IOC_SETPARAMS:
                   1059:        case PPS_IOC_GETCAP:
                   1060:        case PPS_IOC_FETCH:
                   1061: #ifdef PPS_SYNC
                   1062:        case PPS_IOC_KCBIND:
                   1063: #endif
1.279     ad       1064:                mutex_spin_enter(&timecounter_lock);
1.244     kardel   1065:                error = pps_ioctl(cmd, data, &sc->sc_pps_state);
1.279     ad       1066:                mutex_spin_exit(&timecounter_lock);
1.244     kardel   1067:                break;
1.224     simonb   1068:
1.144     jonathan 1069:        case TIOCDCDTIMESTAMP:  /* XXX old, overloaded  API used by xntpd v3 */
1.279     ad       1070:                mutex_spin_enter(&timecounter_lock);
1.244     kardel   1071: #ifndef PPS_TRAILING_EDGE
                   1072:                TIMESPEC_TO_TIMEVAL((struct timeval *)data,
                   1073:                    &sc->sc_pps_state.ppsinfo.assert_timestamp);
                   1074: #else
                   1075:                TIMESPEC_TO_TIMEVAL((struct timeval *)data,
                   1076:                    &sc->sc_pps_state.ppsinfo.clear_timestamp);
                   1077: #endif
1.279     ad       1078:                mutex_spin_exit(&timecounter_lock);
1.144     jonathan 1079:                break;
                   1080:
1.99      mycroft  1081:        default:
1.194     atatat   1082:                error = EPASSTHROUGH;
1.136     mycroft  1083:                break;
1.21      mycroft  1084:        }
1.22      cgd      1085:
1.263     ad       1086:        mutex_spin_exit(&sc->sc_lock);
1.136     mycroft  1087:
1.99      mycroft  1088: #ifdef COM_DEBUG
1.101     mycroft  1089:        if (com_debug)
1.99      mycroft  1090:                comstatus(sc, "comioctl ");
                   1091: #endif
                   1092:
1.136     mycroft  1093:        return (error);
1.99      mycroft  1094: }
                   1095:
1.101     mycroft  1096: integrate void
1.197     simonb   1097: com_schedrx(struct com_softc *sc)
1.101     mycroft  1098: {
                   1099:
                   1100:        sc->sc_rx_ready = 1;
                   1101:
                   1102:        /* Wake up the poller. */
1.263     ad       1103:        softint_schedule(sc->sc_si);
1.101     mycroft  1104: }
                   1105:
1.99      mycroft  1106: void
1.197     simonb   1107: com_break(struct com_softc *sc, int onoff)
1.99      mycroft  1108: {
                   1109:
                   1110:        if (onoff)
                   1111:                SET(sc->sc_lcr, LCR_SBREAK);
                   1112:        else
                   1113:                CLR(sc->sc_lcr, LCR_SBREAK);
1.22      cgd      1114:
1.99      mycroft  1115:        if (!sc->sc_heldchange) {
                   1116:                if (sc->sc_tx_busy) {
                   1117:                        sc->sc_heldtbc = sc->sc_tbc;
                   1118:                        sc->sc_tbc = 0;
                   1119:                        sc->sc_heldchange = 1;
                   1120:                } else
                   1121:                        com_loadchannelregs(sc);
1.22      cgd      1122:        }
1.99      mycroft  1123: }
1.22      cgd      1124:
1.99      mycroft  1125: void
1.197     simonb   1126: com_modem(struct com_softc *sc, int onoff)
1.99      mycroft  1127: {
1.22      cgd      1128:
1.153     mycroft  1129:        if (sc->sc_mcr_dtr == 0)
                   1130:                return;
                   1131:
1.99      mycroft  1132:        if (onoff)
                   1133:                SET(sc->sc_mcr, sc->sc_mcr_dtr);
                   1134:        else
                   1135:                CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1.22      cgd      1136:
1.99      mycroft  1137:        if (!sc->sc_heldchange) {
                   1138:                if (sc->sc_tx_busy) {
                   1139:                        sc->sc_heldtbc = sc->sc_tbc;
                   1140:                        sc->sc_tbc = 0;
                   1141:                        sc->sc_heldchange = 1;
                   1142:                } else
                   1143:                        com_loadchannelregs(sc);
1.22      cgd      1144:        }
1.153     mycroft  1145: }
                   1146:
                   1147: void
1.197     simonb   1148: tiocm_to_com(struct com_softc *sc, u_long how, int ttybits)
1.153     mycroft  1149: {
                   1150:        u_char combits;
                   1151:
                   1152:        combits = 0;
                   1153:        if (ISSET(ttybits, TIOCM_DTR))
                   1154:                SET(combits, MCR_DTR);
                   1155:        if (ISSET(ttybits, TIOCM_RTS))
                   1156:                SET(combits, MCR_RTS);
1.232     perry    1157:
1.153     mycroft  1158:        switch (how) {
                   1159:        case TIOCMBIC:
                   1160:                CLR(sc->sc_mcr, combits);
                   1161:                break;
                   1162:
                   1163:        case TIOCMBIS:
                   1164:                SET(sc->sc_mcr, combits);
                   1165:                break;
                   1166:
                   1167:        case TIOCMSET:
                   1168:                CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
                   1169:                SET(sc->sc_mcr, combits);
                   1170:                break;
                   1171:        }
                   1172:
                   1173:        if (!sc->sc_heldchange) {
                   1174:                if (sc->sc_tx_busy) {
                   1175:                        sc->sc_heldtbc = sc->sc_tbc;
                   1176:                        sc->sc_tbc = 0;
                   1177:                        sc->sc_heldchange = 1;
                   1178:                } else
                   1179:                        com_loadchannelregs(sc);
                   1180:        }
                   1181: }
                   1182:
                   1183: int
1.197     simonb   1184: com_to_tiocm(struct com_softc *sc)
1.153     mycroft  1185: {
                   1186:        u_char combits;
                   1187:        int ttybits = 0;
                   1188:
                   1189:        combits = sc->sc_mcr;
                   1190:        if (ISSET(combits, MCR_DTR))
                   1191:                SET(ttybits, TIOCM_DTR);
                   1192:        if (ISSET(combits, MCR_RTS))
                   1193:                SET(ttybits, TIOCM_RTS);
                   1194:
                   1195:        combits = sc->sc_msr;
                   1196:        if (ISSET(combits, MSR_DCD))
                   1197:                SET(ttybits, TIOCM_CD);
                   1198:        if (ISSET(combits, MSR_CTS))
                   1199:                SET(ttybits, TIOCM_CTS);
                   1200:        if (ISSET(combits, MSR_DSR))
                   1201:                SET(ttybits, TIOCM_DSR);
                   1202:        if (ISSET(combits, MSR_RI | MSR_TERI))
                   1203:                SET(ttybits, TIOCM_RI);
                   1204:
1.228     mycroft  1205:        if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
1.153     mycroft  1206:                SET(ttybits, TIOCM_LE);
                   1207:
                   1208:        return (ttybits);
1.1       cgd      1209: }
                   1210:
1.106     drochner 1211: static u_char
1.197     simonb   1212: cflag2lcr(tcflag_t cflag)
1.106     drochner 1213: {
                   1214:        u_char lcr = 0;
                   1215:
                   1216:        switch (ISSET(cflag, CSIZE)) {
1.127     mycroft  1217:        case CS5:
1.106     drochner 1218:                SET(lcr, LCR_5BITS);
                   1219:                break;
1.127     mycroft  1220:        case CS6:
1.106     drochner 1221:                SET(lcr, LCR_6BITS);
                   1222:                break;
1.127     mycroft  1223:        case CS7:
1.106     drochner 1224:                SET(lcr, LCR_7BITS);
                   1225:                break;
1.127     mycroft  1226:        case CS8:
1.106     drochner 1227:                SET(lcr, LCR_8BITS);
                   1228:                break;
                   1229:        }
                   1230:        if (ISSET(cflag, PARENB)) {
                   1231:                SET(lcr, LCR_PENAB);
                   1232:                if (!ISSET(cflag, PARODD))
                   1233:                        SET(lcr, LCR_PEVEN);
                   1234:        }
                   1235:        if (ISSET(cflag, CSTOPB))
                   1236:                SET(lcr, LCR_STOPB);
                   1237:
1.110     enami    1238:        return (lcr);
1.106     drochner 1239: }
                   1240:
1.21      mycroft  1241: int
1.197     simonb   1242: comparam(struct tty *tp, struct termios *t)
1.1       cgd      1243: {
1.276     cube     1244:        struct com_softc *sc =
                   1245:            device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1.188     enami    1246:        int ospeed;
1.62      mycroft  1247:        u_char lcr;
1.21      mycroft  1248:
1.149     thorpej  1249:        if (COM_ISALIVE(sc) == 0)
                   1250:                return (EIO);
                   1251:
1.188     enami    1252: #ifdef COM_HAYESP
1.209     thorpej  1253:        if (sc->sc_type == COM_TYPE_HAYESP) {
1.188     enami    1254:                int prescaler, speed;
                   1255:
                   1256:                /*
                   1257:                 * Calculate UART clock prescaler.  It should be in
                   1258:                 * range of 0 .. 3.
                   1259:                 */
                   1260:                for (prescaler = 0, speed = t->c_ospeed; prescaler < 4;
                   1261:                    prescaler++, speed /= 2)
1.210     thorpej  1262:                        if ((ospeed = comspeed(speed, sc->sc_frequency,
                   1263:                                               sc->sc_type)) > 0)
1.188     enami    1264:                                break;
                   1265:
                   1266:                if (prescaler == 4)
                   1267:                        return (EINVAL);
                   1268:                sc->sc_prescaler = prescaler;
                   1269:        } else
                   1270: #endif
1.210     thorpej  1271:        ospeed = comspeed(t->c_ospeed, sc->sc_frequency, sc->sc_type);
1.188     enami    1272:
1.127     mycroft  1273:        /* Check requested parameters. */
1.99      mycroft  1274:        if (ospeed < 0)
                   1275:                return (EINVAL);
                   1276:        if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
                   1277:                return (EINVAL);
1.21      mycroft  1278:
1.99      mycroft  1279:        /*
                   1280:         * For the console, always force CLOCAL and !HUPCL, so that the port
                   1281:         * is always active.
                   1282:         */
                   1283:        if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
                   1284:            ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
                   1285:                SET(t->c_cflag, CLOCAL);
                   1286:                CLR(t->c_cflag, HUPCL);
1.62      mycroft  1287:        }
1.129     mycroft  1288:
                   1289:        /*
                   1290:         * If there were no changes, don't do anything.  This avoids dropping
                   1291:         * input and improves performance when all we did was frob things like
                   1292:         * VMIN and VTIME.
                   1293:         */
                   1294:        if (tp->t_ospeed == t->c_ospeed &&
                   1295:            tp->t_cflag == t->c_cflag)
                   1296:                return (0);
1.126     mycroft  1297:
                   1298:        lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
                   1299:
1.263     ad       1300:        mutex_spin_enter(&sc->sc_lock);
1.126     mycroft  1301:
                   1302:        sc->sc_lcr = lcr;
1.36      mycroft  1303:
                   1304:        /*
1.99      mycroft  1305:         * If we're not in a mode that assumes a connection is present, then
                   1306:         * ignore carrier changes.
1.36      mycroft  1307:         */
1.99      mycroft  1308:        if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
                   1309:                sc->sc_msr_dcd = 0;
                   1310:        else
                   1311:                sc->sc_msr_dcd = MSR_DCD;
                   1312:        /*
                   1313:         * Set the flow control pins depending on the current flow control
                   1314:         * mode.
                   1315:         */
                   1316:        if (ISSET(t->c_cflag, CRTSCTS)) {
                   1317:                sc->sc_mcr_dtr = MCR_DTR;
                   1318:                sc->sc_mcr_rts = MCR_RTS;
                   1319:                sc->sc_msr_cts = MSR_CTS;
1.116     fvdl     1320:                sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1.99      mycroft  1321:        } else if (ISSET(t->c_cflag, MDMBUF)) {
                   1322:                /*
                   1323:                 * For DTR/DCD flow control, make sure we don't toggle DTR for
                   1324:                 * carrier detection.
                   1325:                 */
                   1326:                sc->sc_mcr_dtr = 0;
                   1327:                sc->sc_mcr_rts = MCR_DTR;
                   1328:                sc->sc_msr_cts = MSR_DCD;
1.116     fvdl     1329:                sc->sc_efr = 0;
1.99      mycroft  1330:        } else {
                   1331:                /*
                   1332:                 * If no flow control, then always set RTS.  This will make
                   1333:                 * the other side happy if it mistakenly thinks we're doing
                   1334:                 * RTS/CTS flow control.
                   1335:                 */
                   1336:                sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
                   1337:                sc->sc_mcr_rts = 0;
                   1338:                sc->sc_msr_cts = 0;
1.116     fvdl     1339:                sc->sc_efr = 0;
1.99      mycroft  1340:                if (ISSET(sc->sc_mcr, MCR_DTR))
                   1341:                        SET(sc->sc_mcr, MCR_RTS);
                   1342:                else
                   1343:                        CLR(sc->sc_mcr, MCR_RTS);
                   1344:        }
                   1345:        sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
                   1346:
1.95      mycroft  1347: #if 0
1.99      mycroft  1348:        if (ospeed == 0)
                   1349:                CLR(sc->sc_mcr, sc->sc_mcr_dtr);
                   1350:        else
                   1351:                SET(sc->sc_mcr, sc->sc_mcr_dtr);
1.66      mycroft  1352: #endif
                   1353:
1.99      mycroft  1354:        sc->sc_dlbl = ospeed;
                   1355:        sc->sc_dlbh = ospeed >> 8;
1.66      mycroft  1356:
1.99      mycroft  1357:        /*
                   1358:         * Set the FIFO threshold based on the receive speed.
                   1359:         *
                   1360:         *  * If it's a low speed, it's probably a mouse or some other
                   1361:         *    interactive device, so set the threshold low.
                   1362:         *  * If it's a high speed, trim the trigger level down to prevent
                   1363:         *    overflows.
                   1364:         *  * Otherwise set it a bit higher.
                   1365:         */
1.209     thorpej  1366:        if (sc->sc_type == COM_TYPE_HAYESP)
1.99      mycroft  1367:                sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1.278     tsutsui  1368:        else if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
                   1369:                if (t->c_ospeed <= 1200)
                   1370:                        sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_1;
                   1371:                else if (t->c_ospeed <= 38400)
                   1372:                        sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_8;
                   1373:                else
                   1374:                        sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_4;
                   1375:        } else
1.99      mycroft  1376:                sc->sc_fifo = 0;
1.21      mycroft  1377:
1.127     mycroft  1378:        /* And copy to tty. */
1.240     dsl      1379:        tp->t_ispeed = t->c_ospeed;
1.57      mycroft  1380:        tp->t_ospeed = t->c_ospeed;
                   1381:        tp->t_cflag = t->c_cflag;
1.25      cgd      1382:
1.99      mycroft  1383:        if (!sc->sc_heldchange) {
                   1384:                if (sc->sc_tx_busy) {
                   1385:                        sc->sc_heldtbc = sc->sc_tbc;
                   1386:                        sc->sc_tbc = 0;
                   1387:                        sc->sc_heldchange = 1;
                   1388:                } else
                   1389:                        com_loadchannelregs(sc);
                   1390:        }
                   1391:
1.124     mycroft  1392:        if (!ISSET(t->c_cflag, CHWFLOW)) {
1.125     mycroft  1393:                /* Disable the high water mark. */
                   1394:                sc->sc_r_hiwat = 0;
                   1395:                sc->sc_r_lowat = 0;
1.124     mycroft  1396:                if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
                   1397:                        CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
                   1398:                        com_schedrx(sc);
                   1399:                }
                   1400:                if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
                   1401:                        CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
                   1402:                        com_hwiflow(sc);
                   1403:                }
1.125     mycroft  1404:        } else {
1.127     mycroft  1405:                sc->sc_r_hiwat = com_rbuf_hiwat;
                   1406:                sc->sc_r_lowat = com_rbuf_lowat;
1.124     mycroft  1407:        }
                   1408:
1.263     ad       1409:        mutex_spin_exit(&sc->sc_lock);
1.99      mycroft  1410:
1.25      cgd      1411:        /*
1.99      mycroft  1412:         * Update the tty layer's idea of the carrier bit, in case we changed
1.124     mycroft  1413:         * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
                   1414:         * explicit request.
1.25      cgd      1415:         */
1.181     eeh      1416:        (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1.99      mycroft  1417:
                   1418: #ifdef COM_DEBUG
1.101     mycroft  1419:        if (com_debug)
                   1420:                comstatus(sc, "comparam ");
1.99      mycroft  1421: #endif
                   1422:
                   1423:        if (!ISSET(t->c_cflag, CHWFLOW)) {
                   1424:                if (sc->sc_tx_stopped) {
                   1425:                        sc->sc_tx_stopped = 0;
                   1426:                        comstart(tp);
                   1427:                }
1.21      mycroft  1428:        }
1.1       cgd      1429:
1.99      mycroft  1430:        return (0);
                   1431: }
                   1432:
                   1433: void
1.197     simonb   1434: com_iflush(struct com_softc *sc)
1.99      mycroft  1435: {
1.247     gdamore  1436:        struct com_regs *regsp = &sc->sc_regs;
1.131     marc     1437: #ifdef DIAGNOSTIC
                   1438:        int reg;
                   1439: #endif
                   1440:        int timo;
1.99      mycroft  1441:
1.131     marc     1442: #ifdef DIAGNOSTIC
                   1443:        reg = 0xffff;
                   1444: #endif
                   1445:        timo = 50000;
1.99      mycroft  1446:        /* flush any pending I/O */
1.247     gdamore  1447:        while (ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)
1.131     marc     1448:            && --timo)
                   1449: #ifdef DIAGNOSTIC
                   1450:                reg =
                   1451: #else
                   1452:                    (void)
                   1453: #endif
1.247     gdamore  1454:                    CSR_READ_1(regsp, COM_REG_RXDATA);
1.131     marc     1455: #ifdef DIAGNOSTIC
                   1456:        if (!timo)
1.276     cube     1457:                aprint_error_dev(sc->sc_dev, "com_iflush timeout %02x\n", reg);
1.131     marc     1458: #endif
1.99      mycroft  1459: }
                   1460:
                   1461: void
1.197     simonb   1462: com_loadchannelregs(struct com_softc *sc)
1.99      mycroft  1463: {
1.247     gdamore  1464:        struct com_regs *regsp = &sc->sc_regs;
1.99      mycroft  1465:
                   1466:        /* XXXXX necessary? */
                   1467:        com_iflush(sc);
                   1468:
1.209     thorpej  1469:        if (sc->sc_type == COM_TYPE_PXA2x0)
1.247     gdamore  1470:                CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
1.208     scw      1471:        else
1.247     gdamore  1472:                CSR_WRITE_1(regsp, COM_REG_IER, 0);
1.99      mycroft  1473:
1.281     matt     1474:        if (sc->sc_type == COM_TYPE_OMAP) {
                   1475:                /* disable before changing settings */
                   1476:                CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_DISABLE);
                   1477:        }
                   1478:
1.116     fvdl     1479:        if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1.286     matt     1480:                KASSERT(sc->sc_type != COM_TYPE_AU1x00);
                   1481:                KASSERT(sc->sc_type != COM_TYPE_16550_NOERS);
                   1482:                /* no EFR on alchemy */
                   1483:                CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
1.295.2.2  rmind    1484:                CSR_WRITE_1(regsp, COM_REG_EFR, sc->sc_efr);
1.116     fvdl     1485:        }
1.247     gdamore  1486:        if (sc->sc_type == COM_TYPE_AU1x00) {
                   1487:                /* alchemy has single separate 16-bit clock divisor register */
                   1488:                CSR_WRITE_2(regsp, COM_REG_DLBL, sc->sc_dlbl +
                   1489:                    (sc->sc_dlbh << 8));
                   1490:        } else {
                   1491:                CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr | LCR_DLAB);
                   1492:                CSR_WRITE_1(regsp, COM_REG_DLBL, sc->sc_dlbl);
                   1493:                CSR_WRITE_1(regsp, COM_REG_DLBH, sc->sc_dlbh);
                   1494:        }
                   1495:        CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr);
                   1496:        CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active = sc->sc_mcr);
                   1497:        CSR_WRITE_1(regsp, COM_REG_FIFO, sc->sc_fifo);
1.188     enami    1498: #ifdef COM_HAYESP
1.209     thorpej  1499:        if (sc->sc_type == COM_TYPE_HAYESP) {
1.258     cube     1500:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
1.188     enami    1501:                    HAYESP_SETPRESCALER);
1.258     cube     1502:                bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1.188     enami    1503:                    sc->sc_prescaler);
                   1504:        }
                   1505: #endif
1.281     matt     1506:        if (sc->sc_type == COM_TYPE_OMAP) {
                   1507:                /* setup the fifos.  the FCR value is not used as long
                   1508:                   as SCR[6] and SCR[7] are 0, which they are at reset
                   1509:                   and we never touch the SCR register */
                   1510:                uint8_t rx_fifo_trig = 40;
                   1511:                uint8_t tx_fifo_trig = 60;
                   1512:                uint8_t rx_start = 8;
                   1513:                uint8_t rx_halt = 60;
                   1514:                uint8_t tlr_value = ((rx_fifo_trig>>2) << 4) | (tx_fifo_trig>>2);
                   1515:                uint8_t tcr_value = ((rx_start>>2) << 4) | (rx_halt>>2);
                   1516:
                   1517:                /* enable access to TCR & TLR */
                   1518:                CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr | MCR_TCR_TLR);
                   1519:
                   1520:                /* write tcr and tlr values */
                   1521:                CSR_WRITE_1(regsp, COM_REG_TLR, tlr_value);
                   1522:                CSR_WRITE_1(regsp, COM_REG_TCR, tcr_value);
                   1523:
                   1524:                /* disable access to TCR & TLR */
                   1525:                CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr);
                   1526:
                   1527:                /* enable again, but mode is based on speed */
                   1528:                if (sc->sc_tty->t_termios.c_ospeed > 230400) {
                   1529:                        CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_13X);
                   1530:                } else {
                   1531:                        CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_16X);
                   1532:                }
                   1533:        }
1.99      mycroft  1534:
1.247     gdamore  1535:        CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1.99      mycroft  1536: }
                   1537:
                   1538: int
1.197     simonb   1539: comhwiflow(struct tty *tp, int block)
1.99      mycroft  1540: {
1.276     cube     1541:        struct com_softc *sc =
                   1542:            device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1.99      mycroft  1543:
1.149     thorpej  1544:        if (COM_ISALIVE(sc) == 0)
                   1545:                return (0);
                   1546:
1.99      mycroft  1547:        if (sc->sc_mcr_rts == 0)
                   1548:                return (0);
                   1549:
1.263     ad       1550:        mutex_spin_enter(&sc->sc_lock);
1.232     perry    1551:
1.99      mycroft  1552:        if (block) {
1.101     mycroft  1553:                if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
                   1554:                        SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
                   1555:                        com_hwiflow(sc);
                   1556:                }
1.99      mycroft  1557:        } else {
1.101     mycroft  1558:                if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
                   1559:                        CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
                   1560:                        com_schedrx(sc);
                   1561:                }
                   1562:                if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
                   1563:                        CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
                   1564:                        com_hwiflow(sc);
                   1565:                }
1.99      mycroft  1566:        }
1.179     sommerfe 1567:
1.263     ad       1568:        mutex_spin_exit(&sc->sc_lock);
1.99      mycroft  1569:        return (1);
                   1570: }
1.232     perry    1571:
1.99      mycroft  1572: /*
                   1573:  * (un)block input via hw flowcontrol
                   1574:  */
                   1575: void
1.197     simonb   1576: com_hwiflow(struct com_softc *sc)
1.99      mycroft  1577: {
1.247     gdamore  1578:        struct com_regs *regsp= &sc->sc_regs;
1.99      mycroft  1579:
                   1580:        if (sc->sc_mcr_rts == 0)
                   1581:                return;
                   1582:
1.101     mycroft  1583:        if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1.99      mycroft  1584:                CLR(sc->sc_mcr, sc->sc_mcr_rts);
                   1585:                CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
                   1586:        } else {
                   1587:                SET(sc->sc_mcr, sc->sc_mcr_rts);
                   1588:                SET(sc->sc_mcr_active, sc->sc_mcr_rts);
                   1589:        }
1.247     gdamore  1590:        CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active);
1.1       cgd      1591: }
1.21      mycroft  1592:
1.99      mycroft  1593:
1.12      deraadt  1594: void
1.197     simonb   1595: comstart(struct tty *tp)
1.1       cgd      1596: {
1.276     cube     1597:        struct com_softc *sc =
                   1598:            device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1.247     gdamore  1599:        struct com_regs *regsp = &sc->sc_regs;
1.21      mycroft  1600:        int s;
                   1601:
1.149     thorpej  1602:        if (COM_ISALIVE(sc) == 0)
                   1603:                return;
                   1604:
1.1       cgd      1605:        s = spltty();
1.178     eeh      1606:        if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1.70      mycroft  1607:                goto out;
1.178     eeh      1608:        if (sc->sc_tx_stopped)
1.127     mycroft  1609:                goto out;
1.266     ad       1610:        if (!ttypull(tp))
                   1611:                goto out;
1.99      mycroft  1612:
                   1613:        /* Grab the first contiguous region of buffer space. */
                   1614:        {
                   1615:                u_char *tba;
                   1616:                int tbc;
                   1617:
                   1618:                tba = tp->t_outq.c_cf;
                   1619:                tbc = ndqb(&tp->t_outq, 0);
                   1620:
1.263     ad       1621:                mutex_spin_enter(&sc->sc_lock);
1.99      mycroft  1622:
                   1623:                sc->sc_tba = tba;
                   1624:                sc->sc_tbc = tbc;
                   1625:        }
                   1626:
1.62      mycroft  1627:        SET(tp->t_state, TS_BUSY);
1.99      mycroft  1628:        sc->sc_tx_busy = 1;
1.64      christos 1629:
1.99      mycroft  1630:        /* Enable transmit completion interrupts if necessary. */
1.70      mycroft  1631:        if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
                   1632:                SET(sc->sc_ier, IER_ETXRDY);
1.247     gdamore  1633:                CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1.70      mycroft  1634:        }
1.99      mycroft  1635:
                   1636:        /* Output the first chunk of the contiguous buffer. */
1.195     thorpej  1637:        if (!ISSET(sc->sc_hwflags, COM_HW_NO_TXPRELOAD)) {
1.201     thorpej  1638:                u_int n;
1.99      mycroft  1639:
1.127     mycroft  1640:                n = sc->sc_tbc;
                   1641:                if (n > sc->sc_fifolen)
                   1642:                        n = sc->sc_fifolen;
1.247     gdamore  1643:                CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
1.99      mycroft  1644:                sc->sc_tbc -= n;
                   1645:                sc->sc_tba += n;
1.64      christos 1646:        }
1.233     tls      1647:
1.263     ad       1648:        mutex_spin_exit(&sc->sc_lock);
1.99      mycroft  1649: out:
1.1       cgd      1650:        splx(s);
1.99      mycroft  1651:        return;
1.1       cgd      1652: }
1.21      mycroft  1653:
1.1       cgd      1654: /*
                   1655:  * Stop output on a line.
                   1656:  */
1.85      mycroft  1657: void
1.256     christos 1658: comstop(struct tty *tp, int flag)
1.1       cgd      1659: {
1.276     cube     1660:        struct com_softc *sc =
                   1661:            device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1.1       cgd      1662:
1.263     ad       1663:        mutex_spin_enter(&sc->sc_lock);
1.99      mycroft  1664:        if (ISSET(tp->t_state, TS_BUSY)) {
                   1665:                /* Stop transmitting at the next chunk. */
                   1666:                sc->sc_tbc = 0;
                   1667:                sc->sc_heldtbc = 0;
1.62      mycroft  1668:                if (!ISSET(tp->t_state, TS_TTSTOP))
                   1669:                        SET(tp->t_state, TS_FLUSH);
1.99      mycroft  1670:        }
1.263     ad       1671:        mutex_spin_exit(&sc->sc_lock);
1.1       cgd      1672: }
                   1673:
1.33      mycroft  1674: void
1.197     simonb   1675: comdiag(void *arg)
1.33      mycroft  1676: {
                   1677:        struct com_softc *sc = arg;
1.99      mycroft  1678:        int overflows, floods;
1.33      mycroft  1679:
1.263     ad       1680:        mutex_spin_enter(&sc->sc_lock);
1.33      mycroft  1681:        overflows = sc->sc_overflows;
                   1682:        sc->sc_overflows = 0;
1.57      mycroft  1683:        floods = sc->sc_floods;
                   1684:        sc->sc_floods = 0;
1.99      mycroft  1685:        sc->sc_errors = 0;
1.263     ad       1686:        mutex_spin_exit(&sc->sc_lock);
1.57      mycroft  1687:
1.127     mycroft  1688:        log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1.276     cube     1689:            device_xname(sc->sc_dev),
1.57      mycroft  1690:            overflows, overflows == 1 ? "" : "s",
1.99      mycroft  1691:            floods, floods == 1 ? "" : "s");
1.57      mycroft  1692: }
                   1693:
1.99      mycroft  1694: integrate void
1.197     simonb   1695: com_rxsoft(struct com_softc *sc, struct tty *tp)
1.99      mycroft  1696: {
1.198     simonb   1697:        int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1.127     mycroft  1698:        u_char *get, *end;
                   1699:        u_int cc, scc;
                   1700:        u_char lsr;
                   1701:        int code;
1.57      mycroft  1702:
1.127     mycroft  1703:        end = sc->sc_ebuf;
1.99      mycroft  1704:        get = sc->sc_rbget;
1.127     mycroft  1705:        scc = cc = com_rbuf_size - sc->sc_rbavail;
1.99      mycroft  1706:
1.127     mycroft  1707:        if (cc == com_rbuf_size) {
1.99      mycroft  1708:                sc->sc_floods++;
                   1709:                if (sc->sc_errors++ == 0)
1.170     thorpej  1710:                        callout_reset(&sc->sc_diag_callout, 60 * hz,
                   1711:                            comdiag, sc);
1.99      mycroft  1712:        }
                   1713:
1.205     gson     1714:        /* If not yet open, drop the entire buffer content here */
                   1715:        if (!ISSET(tp->t_state, TS_ISOPEN)) {
                   1716:                get += cc << 1;
                   1717:                if (get >= end)
                   1718:                        get -= com_rbuf_size << 1;
                   1719:                cc = 0;
                   1720:        }
1.101     mycroft  1721:        while (cc) {
1.128     mycroft  1722:                code = get[0];
1.127     mycroft  1723:                lsr = get[1];
1.128     mycroft  1724:                if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
                   1725:                        if (ISSET(lsr, LSR_OE)) {
                   1726:                                sc->sc_overflows++;
                   1727:                                if (sc->sc_errors++ == 0)
1.170     thorpej  1728:                                        callout_reset(&sc->sc_diag_callout,
                   1729:                                            60 * hz, comdiag, sc);
1.128     mycroft  1730:                        }
1.127     mycroft  1731:                        if (ISSET(lsr, LSR_BI | LSR_FE))
                   1732:                                SET(code, TTY_FE);
                   1733:                        if (ISSET(lsr, LSR_PE))
                   1734:                                SET(code, TTY_PE);
                   1735:                }
                   1736:                if ((*rint)(code, tp) == -1) {
1.101     mycroft  1737:                        /*
                   1738:                         * The line discipline's buffer is out of space.
                   1739:                         */
                   1740:                        if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
                   1741:                                /*
                   1742:                                 * We're either not using flow control, or the
                   1743:                                 * line discipline didn't tell us to block for
                   1744:                                 * some reason.  Either way, we have no way to
                   1745:                                 * know when there's more space available, so
                   1746:                                 * just drop the rest of the data.
                   1747:                                 */
1.127     mycroft  1748:                                get += cc << 1;
                   1749:                                if (get >= end)
                   1750:                                        get -= com_rbuf_size << 1;
1.101     mycroft  1751:                                cc = 0;
                   1752:                        } else {
                   1753:                                /*
                   1754:                                 * Don't schedule any more receive processing
                   1755:                                 * until the line discipline tells us there's
                   1756:                                 * space available (through comhwiflow()).
                   1757:                                 * Leave the rest of the data in the input
                   1758:                                 * buffer.
                   1759:                                 */
                   1760:                                SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
                   1761:                        }
                   1762:                        break;
                   1763:                }
1.127     mycroft  1764:                get += 2;
                   1765:                if (get >= end)
                   1766:                        get = sc->sc_rbuf;
1.101     mycroft  1767:                cc--;
1.99      mycroft  1768:        }
                   1769:
1.101     mycroft  1770:        if (cc != scc) {
                   1771:                sc->sc_rbget = get;
1.263     ad       1772:                mutex_spin_enter(&sc->sc_lock);
1.232     perry    1773:
1.101     mycroft  1774:                cc = sc->sc_rbavail += scc - cc;
                   1775:                /* Buffers should be ok again, release possible block. */
                   1776:                if (cc >= sc->sc_r_lowat) {
                   1777:                        if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
                   1778:                                CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
                   1779:                                SET(sc->sc_ier, IER_ERXRDY);
1.208     scw      1780: #ifdef COM_PXA2X0
1.209     thorpej  1781:                                if (sc->sc_type == COM_TYPE_PXA2x0)
1.208     scw      1782:                                        SET(sc->sc_ier, IER_ERXTOUT);
                   1783: #endif
1.247     gdamore  1784:                                CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
1.101     mycroft  1785:                        }
                   1786:                        if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
                   1787:                                CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
                   1788:                                com_hwiflow(sc);
                   1789:                        }
                   1790:                }
1.263     ad       1791:                mutex_spin_exit(&sc->sc_lock);
1.57      mycroft  1792:        }
1.99      mycroft  1793: }
                   1794:
                   1795: integrate void
1.197     simonb   1796: com_txsoft(struct com_softc *sc, struct tty *tp)
1.99      mycroft  1797: {
1.33      mycroft  1798:
1.99      mycroft  1799:        CLR(tp->t_state, TS_BUSY);
                   1800:        if (ISSET(tp->t_state, TS_FLUSH))
                   1801:                CLR(tp->t_state, TS_FLUSH);
                   1802:        else
                   1803:                ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1.181     eeh      1804:        (*tp->t_linesw->l_start)(tp);
1.99      mycroft  1805: }
1.57      mycroft  1806:
1.99      mycroft  1807: integrate void
1.197     simonb   1808: com_stsoft(struct com_softc *sc, struct tty *tp)
1.99      mycroft  1809: {
                   1810:        u_char msr, delta;
1.57      mycroft  1811:
1.263     ad       1812:        mutex_spin_enter(&sc->sc_lock);
1.99      mycroft  1813:        msr = sc->sc_msr;
                   1814:        delta = sc->sc_msr_delta;
                   1815:        sc->sc_msr_delta = 0;
1.263     ad       1816:        mutex_spin_exit(&sc->sc_lock);
1.57      mycroft  1817:
1.99      mycroft  1818:        if (ISSET(delta, sc->sc_msr_dcd)) {
                   1819:                /*
                   1820:                 * Inform the tty layer that carrier detect changed.
                   1821:                 */
1.181     eeh      1822:                (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1.99      mycroft  1823:        }
1.61      mycroft  1824:
1.99      mycroft  1825:        if (ISSET(delta, sc->sc_msr_cts)) {
                   1826:                /* Block or unblock output according to flow control. */
                   1827:                if (ISSET(msr, sc->sc_msr_cts)) {
                   1828:                        sc->sc_tx_stopped = 0;
1.181     eeh      1829:                        (*tp->t_linesw->l_start)(tp);
1.99      mycroft  1830:                } else {
                   1831:                        sc->sc_tx_stopped = 1;
1.61      mycroft  1832:                }
1.99      mycroft  1833:        }
                   1834:
                   1835: #ifdef COM_DEBUG
1.101     mycroft  1836:        if (com_debug)
1.127     mycroft  1837:                comstatus(sc, "com_stsoft");
1.99      mycroft  1838: #endif
                   1839: }
                   1840:
                   1841: void
1.197     simonb   1842: comsoft(void *arg)
1.99      mycroft  1843: {
                   1844:        struct com_softc *sc = arg;
                   1845:        struct tty *tp;
                   1846:
1.149     thorpej  1847:        if (COM_ISALIVE(sc) == 0)
1.131     marc     1848:                return;
                   1849:
1.261     ad       1850:        tp = sc->sc_tty;
1.99      mycroft  1851:
1.261     ad       1852:        if (sc->sc_rx_ready) {
                   1853:                sc->sc_rx_ready = 0;
                   1854:                com_rxsoft(sc, tp);
                   1855:        }
1.57      mycroft  1856:
1.261     ad       1857:        if (sc->sc_st_check) {
                   1858:                sc->sc_st_check = 0;
                   1859:                com_stsoft(sc, tp);
                   1860:        }
1.57      mycroft  1861:
1.261     ad       1862:        if (sc->sc_tx_done) {
                   1863:                sc->sc_tx_done = 0;
                   1864:                com_txsoft(sc, tp);
1.57      mycroft  1865:        }
1.21      mycroft  1866: }
1.140     ross     1867:
1.21      mycroft  1868: int
1.197     simonb   1869: comintr(void *arg)
1.21      mycroft  1870: {
1.49      cgd      1871:        struct com_softc *sc = arg;
1.247     gdamore  1872:        struct com_regs *regsp = &sc->sc_regs;
                   1873:
1.127     mycroft  1874:        u_char *put, *end;
                   1875:        u_int cc;
                   1876:        u_char lsr, iir;
1.55      mycroft  1877:
1.149     thorpej  1878:        if (COM_ISALIVE(sc) == 0)
1.131     marc     1879:                return (0);
                   1880:
1.288     cegger   1881:        KASSERT(regsp != NULL);
                   1882:
1.263     ad       1883:        mutex_spin_enter(&sc->sc_lock);
1.247     gdamore  1884:        iir = CSR_READ_1(regsp, COM_REG_IIR);
1.179     sommerfe 1885:        if (ISSET(iir, IIR_NOPEND)) {
1.263     ad       1886:                mutex_spin_exit(&sc->sc_lock);
1.55      mycroft  1887:                return (0);
1.179     sommerfe 1888:        }
1.21      mycroft  1889:
1.127     mycroft  1890:        end = sc->sc_ebuf;
1.99      mycroft  1891:        put = sc->sc_rbput;
                   1892:        cc = sc->sc_rbavail;
                   1893:
1.189     briggs   1894: again: do {
1.168     jonathan 1895:                u_char  msr, delta;
1.21      mycroft  1896:
1.247     gdamore  1897:                lsr = CSR_READ_1(regsp, COM_REG_LSR);
1.103     drochner 1898:                if (ISSET(lsr, LSR_BI)) {
1.186     uwe      1899:                        int cn_trapped = 0;
1.207     fvdl     1900:
1.186     uwe      1901:                        cn_check_magic(sc->sc_tty->t_dev,
                   1902:                                       CNC_BREAK, com_cnm_state);
                   1903:                        if (cn_trapped)
1.103     drochner 1904:                                continue;
1.206     briggs   1905: #if defined(KGDB) && !defined(DDB)
1.103     drochner 1906:                        if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
                   1907:                                kgdb_connect(1);
                   1908:                                continue;
                   1909:                        }
                   1910: #endif
1.102     thorpej  1911:                }
                   1912:
1.101     mycroft  1913:                if (ISSET(lsr, LSR_RCV_MASK) &&
                   1914:                    !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1.127     mycroft  1915:                        while (cc > 0) {
1.186     uwe      1916:                                int cn_trapped = 0;
1.247     gdamore  1917:                                put[0] = CSR_READ_1(regsp, COM_REG_RXDATA);
1.127     mycroft  1918:                                put[1] = lsr;
1.186     uwe      1919:                                cn_check_magic(sc->sc_tty->t_dev,
                   1920:                                               put[0], com_cnm_state);
1.229     mycroft  1921:                                if (cn_trapped)
                   1922:                                        goto next;
1.127     mycroft  1923:                                put += 2;
                   1924:                                if (put >= end)
                   1925:                                        put = sc->sc_rbuf;
                   1926:                                cc--;
1.229     mycroft  1927:                        next:
1.247     gdamore  1928:                                lsr = CSR_READ_1(regsp, COM_REG_LSR);
1.127     mycroft  1929:                                if (!ISSET(lsr, LSR_RCV_MASK))
                   1930:                                        break;
1.99      mycroft  1931:                        }
1.127     mycroft  1932:
1.99      mycroft  1933:                        /*
                   1934:                         * Current string of incoming characters ended because
1.127     mycroft  1935:                         * no more data was available or we ran out of space.
                   1936:                         * Schedule a receive event if any data was received.
                   1937:                         * If we're out of space, turn off receive interrupts.
1.99      mycroft  1938:                         */
                   1939:                        sc->sc_rbput = put;
                   1940:                        sc->sc_rbavail = cc;
1.101     mycroft  1941:                        if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
                   1942:                                sc->sc_rx_ready = 1;
1.127     mycroft  1943:
1.99      mycroft  1944:                        /*
                   1945:                         * See if we are in danger of overflowing a buffer. If
                   1946:                         * so, use hardware flow control to ease the pressure.
                   1947:                         */
1.101     mycroft  1948:                        if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1.99      mycroft  1949:                            cc < sc->sc_r_hiwat) {
1.101     mycroft  1950:                                SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
                   1951:                                com_hwiflow(sc);
1.99      mycroft  1952:                        }
1.127     mycroft  1953:
1.99      mycroft  1954:                        /*
1.101     mycroft  1955:                         * If we're out of space, disable receive interrupts
                   1956:                         * until the queue has drained a bit.
1.99      mycroft  1957:                         */
                   1958:                        if (!cc) {
1.101     mycroft  1959:                                SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1.208     scw      1960: #ifdef COM_PXA2X0
1.209     thorpej  1961:                                if (sc->sc_type == COM_TYPE_PXA2x0)
1.229     mycroft  1962:                                        CLR(sc->sc_ier, IER_ERXRDY|IER_ERXTOUT);
                   1963:                                else
1.208     scw      1964: #endif
1.229     mycroft  1965:                                        CLR(sc->sc_ier, IER_ERXRDY);
1.247     gdamore  1966:                                CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1.99      mycroft  1967:                        }
1.88      mycroft  1968:                } else {
1.228     mycroft  1969:                        if ((iir & (IIR_RXRDY|IIR_TXRDY)) == IIR_RXRDY) {
1.247     gdamore  1970:                                (void) CSR_READ_1(regsp, COM_REG_RXDATA);
1.88      mycroft  1971:                                continue;
                   1972:                        }
                   1973:                }
1.55      mycroft  1974:
1.247     gdamore  1975:                msr = CSR_READ_1(regsp, COM_REG_MSR);
1.99      mycroft  1976:                delta = msr ^ sc->sc_msr;
                   1977:                sc->sc_msr = msr;
1.244     kardel   1978:                if ((sc->sc_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) &&
                   1979:                    (delta & MSR_DCD)) {
1.279     ad       1980:                        mutex_spin_enter(&timecounter_lock);
1.244     kardel   1981:                        pps_capture(&sc->sc_pps_state);
                   1982:                        pps_event(&sc->sc_pps_state,
                   1983:                            (msr & MSR_DCD) ?
                   1984:                            PPS_CAPTUREASSERT :
                   1985:                            PPS_CAPTURECLEAR);
1.279     ad       1986:                        mutex_spin_exit(&timecounter_lock);
1.244     kardel   1987:                }
1.168     jonathan 1988:
1.167     jonathan 1989:                /*
                   1990:                 * Process normal status changes
                   1991:                 */
                   1992:                if (ISSET(delta, sc->sc_msr_mask)) {
                   1993:                        SET(sc->sc_msr_delta, delta);
1.99      mycroft  1994:
                   1995:                        /*
                   1996:                         * Stop output immediately if we lose the output
                   1997:                         * flow control signal or carrier detect.
                   1998:                         */
                   1999:                        if (ISSET(~msr, sc->sc_msr_mask)) {
                   2000:                                sc->sc_tbc = 0;
                   2001:                                sc->sc_heldtbc = 0;
1.69      mycroft  2002: #ifdef COM_DEBUG
1.101     mycroft  2003:                                if (com_debug)
                   2004:                                        comstatus(sc, "comintr  ");
1.69      mycroft  2005: #endif
1.99      mycroft  2006:                        }
1.55      mycroft  2007:
1.99      mycroft  2008:                        sc->sc_st_check = 1;
1.55      mycroft  2009:                }
1.225     enami    2010:        } while (!ISSET((iir =
1.247     gdamore  2011:            CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND) &&
1.225     enami    2012:            /*
                   2013:             * Since some device (e.g., ST16C1550) doesn't clear IIR_TXRDY
                   2014:             * by IIR read, so we can't do this way: `process all interrupts,
                   2015:             * then do TX if possble'.
                   2016:             */
                   2017:            (iir & IIR_IMASK) != IIR_TXRDY);
1.55      mycroft  2018:
1.99      mycroft  2019:        /*
1.225     enami    2020:         * Read LSR again, since there may be an interrupt between
                   2021:         * the last LSR read and IIR read above.
                   2022:         */
1.247     gdamore  2023:        lsr = CSR_READ_1(regsp, COM_REG_LSR);
1.225     enami    2024:
                   2025:        /*
                   2026:         * See if data can be transmitted as well.
                   2027:         * Schedule tx done event if no data left
1.99      mycroft  2028:         * and tty was marked busy.
                   2029:         */
                   2030:        if (ISSET(lsr, LSR_TXRDY)) {
                   2031:                /*
                   2032:                 * If we've delayed a parameter change, do it now, and restart
                   2033:                 * output.
                   2034:                 */
                   2035:                if (sc->sc_heldchange) {
                   2036:                        com_loadchannelregs(sc);
                   2037:                        sc->sc_heldchange = 0;
                   2038:                        sc->sc_tbc = sc->sc_heldtbc;
                   2039:                        sc->sc_heldtbc = 0;
                   2040:                }
1.127     mycroft  2041:
1.99      mycroft  2042:                /* Output the next chunk of the contiguous buffer, if any. */
                   2043:                if (sc->sc_tbc > 0) {
1.201     thorpej  2044:                        u_int n;
1.99      mycroft  2045:
1.127     mycroft  2046:                        n = sc->sc_tbc;
                   2047:                        if (n > sc->sc_fifolen)
                   2048:                                n = sc->sc_fifolen;
1.247     gdamore  2049:                        CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
1.99      mycroft  2050:                        sc->sc_tbc -= n;
                   2051:                        sc->sc_tba += n;
1.127     mycroft  2052:                } else {
                   2053:                        /* Disable transmit completion interrupts if necessary. */
                   2054:                        if (ISSET(sc->sc_ier, IER_ETXRDY)) {
                   2055:                                CLR(sc->sc_ier, IER_ETXRDY);
1.247     gdamore  2056:                                CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1.127     mycroft  2057:                        }
                   2058:                        if (sc->sc_tx_busy) {
                   2059:                                sc->sc_tx_busy = 0;
                   2060:                                sc->sc_tx_done = 1;
                   2061:                        }
1.62      mycroft  2062:                }
1.99      mycroft  2063:        }
1.189     briggs   2064:
1.247     gdamore  2065:        if (!ISSET((iir = CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND))
1.189     briggs   2066:                goto again;
                   2067:
1.263     ad       2068:        mutex_spin_exit(&sc->sc_lock);
1.62      mycroft  2069:
1.99      mycroft  2070:        /* Wake up the poller. */
1.263     ad       2071:        softint_schedule(sc->sc_si);
1.115     explorer 2072:
                   2073: #if NRND > 0 && defined(RND_COM)
                   2074:        rnd_add_uint32(&sc->rnd_source, iir | lsr);
                   2075: #endif
                   2076:
1.88      mycroft  2077:        return (1);
1.1       cgd      2078: }
                   2079:
                   2080: /*
1.102     thorpej  2081:  * The following functions are polled getc and putc routines, shared
                   2082:  * by the console and kgdb glue.
1.232     perry    2083:  *
1.186     uwe      2084:  * The read-ahead code is so that you can detect pending in-band
                   2085:  * cn_magic in polled mode while doing output rather than having to
                   2086:  * wait until the kernel decides it needs input.
1.102     thorpej  2087:  */
                   2088:
1.186     uwe      2089: #define MAX_READAHEAD  20
                   2090: static int com_readahead[MAX_READAHEAD];
                   2091: static int com_readaheadcount = 0;
1.174     jeffs    2092:
1.102     thorpej  2093: int
1.247     gdamore  2094: com_common_getc(dev_t dev, struct com_regs *regsp)
1.102     thorpej  2095: {
                   2096:        int s = splserial();
                   2097:        u_char stat, c;
                   2098:
1.174     jeffs    2099:        /* got a character from reading things earlier */
1.186     uwe      2100:        if (com_readaheadcount > 0) {
1.174     jeffs    2101:                int i;
                   2102:
1.186     uwe      2103:                c = com_readahead[0];
                   2104:                for (i = 1; i < com_readaheadcount; i++) {
                   2105:                        com_readahead[i-1] = com_readahead[i];
1.174     jeffs    2106:                }
1.186     uwe      2107:                com_readaheadcount--;
1.174     jeffs    2108:                splx(s);
                   2109:                return (c);
                   2110:        }
                   2111:
1.135     thorpej  2112:        /* block until a character becomes available */
1.247     gdamore  2113:        while (!ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY))
1.102     thorpej  2114:                ;
1.135     thorpej  2115:
1.247     gdamore  2116:        c = CSR_READ_1(regsp, COM_REG_RXDATA);
                   2117:        stat = CSR_READ_1(regsp, COM_REG_IIR);
1.186     uwe      2118:        {
                   2119:                int cn_trapped = 0; /* unused */
                   2120: #ifdef DDB
1.174     jeffs    2121:                extern int db_active;
1.186     uwe      2122:                if (!db_active)
                   2123: #endif
                   2124:                        cn_check_magic(dev, c, com_cnm_state);
1.174     jeffs    2125:        }
1.102     thorpej  2126:        splx(s);
                   2127:        return (c);
                   2128: }
                   2129:
1.289     dyoung   2130: static void
1.247     gdamore  2131: com_common_putc(dev_t dev, struct com_regs *regsp, int c)
1.102     thorpej  2132: {
                   2133:        int s = splserial();
1.204     simonb   2134:        int cin, stat, timo;
1.174     jeffs    2135:
1.232     perry    2136:        if (com_readaheadcount < MAX_READAHEAD
1.247     gdamore  2137:             && ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)) {
1.186     uwe      2138:                int cn_trapped = 0;
1.247     gdamore  2139:                cin = CSR_READ_1(regsp, COM_REG_RXDATA);
                   2140:                stat = CSR_READ_1(regsp, COM_REG_IIR);
1.186     uwe      2141:                cn_check_magic(dev, cin, com_cnm_state);
                   2142:                com_readahead[com_readaheadcount++] = cin;
1.174     jeffs    2143:        }
1.102     thorpej  2144:
                   2145:        /* wait for any pending transmission to finish */
1.161     ross     2146:        timo = 150000;
1.247     gdamore  2147:        while (!ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_TXRDY) && --timo)
1.161     ross     2148:                continue;
1.135     thorpej  2149:
1.247     gdamore  2150:        CSR_WRITE_1(regsp, COM_REG_TXDATA, c);
                   2151:        COM_BARRIER(regsp, BR | BW);
1.160     thorpej  2152:
1.157     mycroft  2153:        splx(s);
1.102     thorpej  2154: }
                   2155:
                   2156: /*
1.165     drochner 2157:  * Initialize UART for use as console or KGDB line.
1.99      mycroft  2158:  */
1.106     drochner 2159: int
1.247     gdamore  2160: cominit(struct com_regs *regsp, int rate, int frequency, int type,
                   2161:     tcflag_t cflag)
1.1       cgd      2162: {
1.106     drochner 2163:
1.247     gdamore  2164:        if (bus_space_map(regsp->cr_iot, regsp->cr_iobase, regsp->cr_nports, 0,
                   2165:                &regsp->cr_ioh))
1.110     enami    2166:                return (ENOMEM); /* ??? */
1.1       cgd      2167:
1.281     matt     2168:        if (type == COM_TYPE_OMAP) {
                   2169:                /* disable before changing settings */
                   2170:                CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_DISABLE);
                   2171:        }
                   2172:
1.210     thorpej  2173:        rate = comspeed(rate, frequency, type);
1.295.2.3! rmind    2174:        if (__predict_true(rate != -1)) {
        !          2175:                if (type == COM_TYPE_AU1x00) {
        !          2176:                        CSR_WRITE_2(regsp, COM_REG_DLBL, rate);
        !          2177:                } else {
        !          2178:                        /* no EFR on alchemy */
        !          2179:                        if (type != COM_TYPE_16550_NOERS) {
        !          2180:                                CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
        !          2181:                                CSR_WRITE_1(regsp, COM_REG_EFR, 0);
        !          2182:                        }
        !          2183:                        CSR_WRITE_1(regsp, COM_REG_LCR, LCR_DLAB);
        !          2184:                        CSR_WRITE_1(regsp, COM_REG_DLBL, rate & 0xff);
        !          2185:                        CSR_WRITE_1(regsp, COM_REG_DLBH, rate >> 8);
        !          2186:                }
1.247     gdamore  2187:        }
                   2188:        CSR_WRITE_1(regsp, COM_REG_LCR, cflag2lcr(cflag));
                   2189:        CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS);
                   2190:        CSR_WRITE_1(regsp, COM_REG_FIFO,
1.87      mycroft  2191:            FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1.281     matt     2192:
                   2193:        if (type == COM_TYPE_OMAP) {
                   2194:                /* setup the fifos.  the FCR value is not used as long
                   2195:                   as SCR[6] and SCR[7] are 0, which they are at reset
                   2196:                   and we never touch the SCR register */
                   2197:                uint8_t rx_fifo_trig = 40;
                   2198:                uint8_t tx_fifo_trig = 60;
                   2199:                uint8_t rx_start = 8;
                   2200:                uint8_t rx_halt = 60;
                   2201:                uint8_t tlr_value = ((rx_fifo_trig>>2) << 4) | (tx_fifo_trig>>2);
                   2202:                uint8_t tcr_value = ((rx_start>>2) << 4) | (rx_halt>>2);
                   2203:
                   2204:                /* enable access to TCR & TLR */
                   2205:                CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS | MCR_TCR_TLR);
                   2206:
                   2207:                /* write tcr and tlr values */
                   2208:                CSR_WRITE_1(regsp, COM_REG_TLR, tlr_value);
                   2209:                CSR_WRITE_1(regsp, COM_REG_TCR, tcr_value);
                   2210:
                   2211:                /* disable access to TCR & TLR */
                   2212:                CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS);
                   2213:
                   2214:                /* enable again, but mode is based on speed */
                   2215:                if (rate > 230400) {
                   2216:                        CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_13X);
                   2217:                } else {
                   2218:                        CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_16X);
                   2219:                }
                   2220:        }
                   2221:
1.212     bsh      2222: #ifdef COM_PXA2X0
1.223     simonb   2223:        if (type == COM_TYPE_PXA2x0)
1.247     gdamore  2224:                CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
1.221     simonb   2225:        else
1.212     bsh      2226: #endif
1.247     gdamore  2227:                CSR_WRITE_1(regsp, COM_REG_IER, 0);
1.106     drochner 2228:
1.110     enami    2229:        return (0);
1.99      mycroft  2230: }
                   2231:
1.106     drochner 2232: int
1.247     gdamore  2233: comcnattach1(struct com_regs *regsp, int rate, int frequency, int type,
                   2234:     tcflag_t cflag)
1.99      mycroft  2235: {
1.106     drochner 2236:        int res;
                   2237:
1.289     dyoung   2238:        comcons_info.regs = *regsp;
1.247     gdamore  2239:
1.289     dyoung   2240:        res = cominit(&comcons_info.regs, rate, frequency, type, cflag);
1.110     enami    2241:        if (res)
                   2242:                return (res);
1.106     drochner 2243:
                   2244:        cn_tab = &comcons;
1.186     uwe      2245:        cn_init_magic(&com_cnm_state);
                   2246:        cn_set_magic("\047\001"); /* default magic is BREAK */
1.106     drochner 2247:
1.289     dyoung   2248:        comcons_info.frequency = frequency;
                   2249:        comcons_info.type = type;
                   2250:        comcons_info.rate = rate;
                   2251:        comcons_info.cflag = cflag;
1.99      mycroft  2252:
1.110     enami    2253:        return (0);
1.1       cgd      2254: }
                   2255:
1.80      christos 2256: int
1.247     gdamore  2257: comcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
                   2258:     int type, tcflag_t cflag)
                   2259: {
                   2260:        struct com_regs regs;
                   2261:
1.251     mrg      2262:        memset(&regs, 0, sizeof regs);
1.247     gdamore  2263:        regs.cr_iot = iot;
                   2264:        regs.cr_iobase = iobase;
                   2265:        regs.cr_nports = COM_NPORTS;
                   2266: #ifdef COM_REGMAP
                   2267:        memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
                   2268: #endif
                   2269:
                   2270:        return comcnattach1(&regs, rate, frequency, type, cflag);
                   2271: }
                   2272:
1.289     dyoung   2273: static int
                   2274: comcnreattach(void)
                   2275: {
                   2276:        return comcnattach1(&comcons_info.regs, comcons_info.rate,
                   2277:            comcons_info.frequency, comcons_info.type, comcons_info.cflag);
                   2278: }
                   2279:
1.247     gdamore  2280: int
1.197     simonb   2281: comcngetc(dev_t dev)
1.1       cgd      2282: {
1.197     simonb   2283:
1.289     dyoung   2284:        return (com_common_getc(dev, &comcons_info.regs));
1.1       cgd      2285: }
                   2286:
                   2287: /*
                   2288:  * Console kernel output character routine.
                   2289:  */
1.48      mycroft  2290: void
1.197     simonb   2291: comcnputc(dev_t dev, int c)
1.1       cgd      2292: {
1.197     simonb   2293:
1.289     dyoung   2294:        com_common_putc(dev, &comcons_info.regs, c);
1.37      mycroft  2295: }
                   2296:
                   2297: void
1.256     christos 2298: comcnpollc(dev_t dev, int on)
1.37      mycroft  2299: {
                   2300:
1.106     drochner 2301: }
                   2302:
                   2303: #ifdef KGDB
                   2304: int
1.247     gdamore  2305: com_kgdb_attach1(struct com_regs *regsp, int rate, int frequency, int type,
                   2306:     tcflag_t cflag)
1.106     drochner 2307: {
                   2308:        int res;
1.107     drochner 2309:
1.295.2.1  rmind    2310:        if (bus_space_is_equal(regsp->cr_iot, comcons_info.regs.cr_iot) &&
1.289     dyoung   2311:            regsp->cr_iobase == comcons_info.regs.cr_iobase) {
1.206     briggs   2312: #if !defined(DDB)
1.110     enami    2313:                return (EBUSY); /* cannot share with console */
1.206     briggs   2314: #else
1.247     gdamore  2315:                comkgdbregs = *regsp;
1.289     dyoung   2316:                comkgdbregs.cr_ioh = comcons_info.regs.cr_ioh;
1.206     briggs   2317: #endif
                   2318:        } else {
1.247     gdamore  2319:                comkgdbregs = *regsp;
                   2320:                res = cominit(&comkgdbregs, rate, frequency, type, cflag);
1.206     briggs   2321:                if (res)
                   2322:                        return (res);
1.190     fvdl     2323:
1.206     briggs   2324:                /*
                   2325:                 * XXXfvdl this shouldn't be needed, but the cn_magic goo
                   2326:                 * expects this to be initialized
                   2327:                 */
                   2328:                cn_init_magic(&com_cnm_state);
                   2329:                cn_set_magic("\047\001");
                   2330:        }
1.106     drochner 2331:
                   2332:        kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
                   2333:        kgdb_dev = 123; /* unneeded, only to satisfy some tests */
                   2334:
1.247     gdamore  2335:        return (0);
                   2336: }
                   2337:
                   2338: int
                   2339: com_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
                   2340:     int frequency, int type, tcflag_t cflag)
                   2341: {
                   2342:        struct com_regs regs;
                   2343:
                   2344:        regs.cr_iot = iot;
                   2345:        regs.cr_nports = COM_NPORTS;
                   2346:        regs.cr_iobase = iobase;
                   2347: #ifdef COM_REGMAP
                   2348:        memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
                   2349: #endif
1.106     drochner 2350:
1.247     gdamore  2351:        return com_kgdb_attach1(&regs, rate, frequency, type, cflag);
1.106     drochner 2352: }
                   2353:
                   2354: /* ARGSUSED */
                   2355: int
1.256     christos 2356: com_kgdb_getc(void *arg)
1.106     drochner 2357: {
1.197     simonb   2358:
1.247     gdamore  2359:        return (com_common_getc(NODEV, &comkgdbregs));
1.106     drochner 2360: }
                   2361:
                   2362: /* ARGSUSED */
                   2363: void
1.256     christos 2364: com_kgdb_putc(void *arg, int c)
1.106     drochner 2365: {
1.197     simonb   2366:
1.247     gdamore  2367:        com_common_putc(NODEV, &comkgdbregs, c);
1.106     drochner 2368: }
                   2369: #endif /* KGDB */
                   2370:
                   2371: /* helper function to identify the com ports used by
                   2372:  console or KGDB (and not yet autoconf attached) */
                   2373: int
1.197     simonb   2374: com_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
1.106     drochner 2375: {
                   2376:        bus_space_handle_t help;
                   2377:
1.110     enami    2378:        if (!comconsattached &&
1.295.2.1  rmind    2379:            bus_space_is_equal(iot, comcons_info.regs.cr_iot) &&
1.289     dyoung   2380:            iobase == comcons_info.regs.cr_iobase)
                   2381:                help = comcons_info.regs.cr_ioh;
1.106     drochner 2382: #ifdef KGDB
1.110     enami    2383:        else if (!com_kgdb_attached &&
1.295.2.1  rmind    2384:            bus_space_is_equal(iot, comkgdbregs.cr_iot) &&
                   2385:            iobase == comkgdbregs.cr_iobase)
1.247     gdamore  2386:                help = comkgdbregs.cr_ioh;
1.106     drochner 2387: #endif
                   2388:        else
1.110     enami    2389:                return (0);
1.106     drochner 2390:
1.110     enami    2391:        if (ioh)
                   2392:                *ioh = help;
                   2393:        return (1);
1.2       cgd      2394: }
1.245     perry    2395:
1.247     gdamore  2396: /*
                   2397:  * this routine exists to serve as a shutdown hook for systems that
                   2398:  * have firmware which doesn't interact properly with a com device in
                   2399:  * FIFO mode.
                   2400:  */
1.273     dyoung   2401: bool
                   2402: com_cleanup(device_t self, int how)
1.247     gdamore  2403: {
1.273     dyoung   2404:        struct com_softc *sc = device_private(self);
1.247     gdamore  2405:
                   2406:        if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
                   2407:                CSR_WRITE_1(&sc->sc_regs, COM_REG_FIFO, 0);
1.273     dyoung   2408:
                   2409:        return true;
                   2410: }
                   2411:
                   2412: bool
1.295     dyoung   2413: com_suspend(device_t self, const pmf_qual_t *qual)
1.273     dyoung   2414: {
                   2415:        struct com_softc *sc = device_private(self);
                   2416:
1.292     dyoung   2417: #if 0
                   2418:        if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE) && cn_tab == &comcons)
                   2419:                cn_tab = &comcons_suspend;
                   2420: #endif
                   2421:
1.273     dyoung   2422:        CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, 0);
                   2423:        (void)CSR_READ_1(&sc->sc_regs, COM_REG_IIR);
                   2424:
                   2425:        return true;
1.247     gdamore  2426: }
                   2427:
1.268     dyoung   2428: bool
1.295     dyoung   2429: com_resume(device_t self, const pmf_qual_t *qual)
1.245     perry    2430: {
1.273     dyoung   2431:        struct com_softc *sc = device_private(self);
1.245     perry    2432:
1.263     ad       2433:        mutex_spin_enter(&sc->sc_lock);
1.268     dyoung   2434:        com_loadchannelregs(sc);
1.263     ad       2435:        mutex_spin_exit(&sc->sc_lock);
1.268     dyoung   2436:
                   2437:        return true;
1.245     perry    2438: }

CVSweb <webmaster@jp.NetBSD.org>