Home | History | Annotate | Line # | Download | only in at91
at91dbgu.c revision 1.11
      1 /*	$Id: at91dbgu.c,v 1.11 2014/07/25 08:10:31 dholland Exp $	*/
      2 /*	$NetBSD: at91dbgu.c,v 1.11 2014/07/25 08:10:31 dholland Exp $ */
      3 
      4 /*
      5  *
      6  * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Jesse Off
     11  *
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by Ichiro FUKUHARA and Naoto Shimazaki.
     14  *
     15  * This code is derived from software contributed to The NetBSD Foundation
     16  * by IWAMOTO Toshihiro.
     17  *
     18  * This code is derived from software contributed to The NetBSD Foundation
     19  * by Charles M. Hannum.
     20  *
     21  * Redistribution and use in source and binary forms, with or without
     22  * modification, are permitted provided that the following conditions
     23  * are met:
     24  * 1. Redistributions of source code must retain the above copyright
     25  *    notice, this list of conditions and the following disclaimer.
     26  * 2. Redistributions in binary form must reproduce the above copyright
     27  *    notice, this list of conditions and the following disclaimer in the
     28  *    documentation and/or other materials provided with the distribution.
     29  * 3. All advertising materials mentioning features or use of this software
     30  *    must display the following acknowledgement:
     31  *        This product includes software developed by the NetBSD
     32  *        Foundation, Inc. and its contributors.
     33  * 4. Neither the name of The NetBSD Foundation nor the names of its
     34  *    contributors may be used to endorse or promote products derived
     35  *    from this software without specific prior written permission.
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     38  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     39  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     41  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     47  * POSSIBILITY OF SUCH DAMAGE.
     48  */
     49 
     50 /*
     51  * Copyright (c) 1991 The Regents of the University of California.
     52  * All rights reserved.
     53  *
     54  * Redistribution and use in source and binary forms, with or without
     55  * modification, are permitted provided that the following conditions
     56  * are met:
     57  * 1. Redistributions of source code must retain the above copyright
     58  *    notice, this list of conditions and the following disclaimer.
     59  * 2. Redistributions in binary form must reproduce the above copyright
     60  *    notice, this list of conditions and the following disclaimer in the
     61  *    documentation and/or other materials provided with the distribution.
     62  * 3. Neither the name of the University nor the names of its contributors
     63  *    may be used to endorse or promote products derived from this software
     64  *    without specific prior written permission.
     65  *
     66  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     76  * SUCH DAMAGE.
     77  *
     78  *      @(#)com.c       7.5 (Berkeley) 5/16/91
     79  */
     80 
     81 /*
     82  * TODO: hardware flow control
     83  */
     84 
     85 #include <sys/cdefs.h>
     86 __KERNEL_RCSID(0, "$NetBSD: at91dbgu.c,v 1.11 2014/07/25 08:10:31 dholland Exp $");
     87 
     88 #include "opt_ddb.h"
     89 #include "opt_kgdb.h"
     90 
     91 #include "rnd.h"
     92 #ifdef RND_COM
     93 #include <sys/rnd.h>
     94 #endif
     95 
     96 /*
     97  * Override cnmagic(9) macro before including <sys/systm.h>.
     98  * We need to know if cn_check_magic triggered debugger, so set a flag.
     99  * Callers of cn_check_magic must declare int cn_trapped = 0;
    100  * XXX: this is *ugly*!
    101  */
    102 #define cn_trap()				\
    103 	do {					\
    104 		console_debugger();		\
    105 		cn_trapped = 1;			\
    106 	} while (/* CONSTCOND */ 0)
    107 
    108 
    109 #include <sys/param.h>
    110 #include <sys/systm.h>
    111 #include <sys/types.h>
    112 #include <sys/conf.h>
    113 #include <sys/file.h>
    114 #include <sys/device.h>
    115 #include <sys/kernel.h>
    116 #include <sys/malloc.h>
    117 #include <sys/tty.h>
    118 #include <sys/uio.h>
    119 #include <sys/vnode.h>
    120 #include <sys/kauth.h>
    121 
    122 #include <machine/intr.h>
    123 #include <sys/bus.h>
    124 
    125 #include <arm/at91/at91reg.h>
    126 #include <arm/at91/at91var.h>
    127 #include <arm/at91/at91dbguvar.h>
    128 #include <arm/at91/at91dbgureg.h>
    129 
    130 #include <dev/cons.h>
    131 
    132 static int	at91dbgu_match(device_t, cfdata_t, void *);
    133 static void	at91dbgu_attach(device_t, device_t, void *);
    134 
    135 static int	at91dbgu_param(struct tty *, struct termios *);
    136 static void	at91dbgu_start(struct tty *);
    137 static int	at91dbgu_hwiflow(struct tty *, int);
    138 
    139 #if 0
    140 static u_int	cflag2lcrhi(tcflag_t);
    141 #endif
    142 static void	at91dbgu_iflush(struct at91dbgu_softc *);
    143 static void	at91dbgu_set(struct at91dbgu_softc *);
    144 
    145 int             at91dbgu_cn_getc(dev_t);
    146 void            at91dbgu_cn_putc(dev_t, int);
    147 void            at91dbgu_cn_pollc(dev_t, int);
    148 
    149 static void	at91dbgu_soft(void* arg);
    150 inline static void	at91dbgu_txsoft(struct at91dbgu_softc *, struct tty *);
    151 inline static void	at91dbgu_rxsoft(struct at91dbgu_softc *, struct tty *);
    152 
    153 void            at91dbgu_cn_probe(struct consdev *);
    154 void            at91dbgu_cn_init(struct consdev *);
    155 
    156 static struct at91dbgu_cons_softc {
    157 	bus_space_tag_t		sc_iot;
    158 	bus_space_handle_t	sc_ioh;
    159 	bus_addr_t		sc_hwbase;
    160 	int			sc_ospeed;
    161 	tcflag_t		sc_cflag;
    162 	int			sc_attached;
    163 
    164 	uint8_t			*sc_rx_ptr;
    165 	uint8_t			sc_rx_fifo[64];
    166 } dbgu_cn_sc;
    167 
    168 static struct cnm_state at91dbgu_cnm_state;
    169 
    170 CFATTACH_DECL_NEW(at91dbgu, sizeof(struct at91dbgu_softc),
    171 	      at91dbgu_match, at91dbgu_attach, NULL, NULL);
    172 
    173 extern struct cfdriver at91dbgu_cd;
    174 
    175 dev_type_open(at91dbgu_open);
    176 dev_type_close(at91dbgu_close);
    177 dev_type_read(at91dbgu_read);
    178 dev_type_write(at91dbgu_write);
    179 dev_type_ioctl(at91dbgu_ioctl);
    180 dev_type_stop(at91dbgu_stop);
    181 dev_type_tty(at91dbgu_tty);
    182 dev_type_poll(at91dbgu_poll);
    183 
    184 const struct cdevsw at91dbgu_cdevsw = {
    185 	.d_open = at91dbgu_open,
    186 	.d_close = at91dbgu_close,
    187 	.d_read = at91dbgu_read,
    188 	.d_write = at91dbgu_write,
    189 	.d_ioctl = at91dbgu_ioctl,
    190 	.d_stop = at91dbgu_stop,
    191 	.d_tty = at91dbgu_tty,
    192 	.d_poll = at91dbgu_poll,
    193 	.d_mmap = nommap,
    194 	.d_kqfilter = ttykqfilter,
    195 	.d_discard = nodiscard,
    196 	.d_flag = D_TTY
    197 };
    198 
    199 struct consdev at91dbgu_cons = {
    200 	at91dbgu_cn_probe, NULL, at91dbgu_cn_getc, at91dbgu_cn_putc, at91dbgu_cn_pollc, NULL,
    201 	NULL, NULL, NODEV, CN_REMOTE
    202 };
    203 
    204 #ifndef DEFAULT_COMSPEED
    205 #define DEFAULT_COMSPEED 115200
    206 #endif
    207 
    208 #define COMUNIT_MASK    0x7ffff
    209 #define COMDIALOUT_MASK 0x80000
    210 
    211 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
    212 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
    213 
    214 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && device_is_active((sc)->sc_dev))
    215 
    216 static int
    217 at91dbgu_match(device_t parent, cfdata_t match, void *aux)
    218 {
    219 	if (strcmp(match->cf_name, "at91dbgu") == 0)
    220 		return 2;
    221 	return 0;
    222 }
    223 
    224 static int
    225 dbgu_intr(void* arg);
    226 
    227 static void
    228 at91dbgu_attach(device_t parent, device_t self, void *aux)
    229 {
    230 	struct at91dbgu_softc *sc = device_private(self);
    231 	struct at91bus_attach_args *sa = aux;
    232 	struct tty *tp;
    233 
    234 	printf("\n");
    235 
    236 	sc->sc_dev = self;
    237 	bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh);
    238 	sc->sc_iot = sa->sa_iot;
    239 	sc->sc_hwbase = sa->sa_addr;
    240 
    241 	DBGUREG(DBGU_IDR) = -1;
    242 	at91_intr_establish(sa->sa_pid, IPL_SERIAL, INTR_HIGH_LEVEL, dbgu_intr, sc);
    243 	DBGU_INIT(AT91_MSTCLK, 115200U);
    244 
    245 	if (sc->sc_iot == dbgu_cn_sc.sc_iot
    246 	    && sc->sc_hwbase == dbgu_cn_sc.sc_hwbase) {
    247 		dbgu_cn_sc.sc_attached = 1;
    248 		/* Make sure the console is always "hardwired". */
    249 		delay(10000);	/* wait for output to finish */
    250 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
    251 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    252 		SET(sc->sc_ier, DBGU_INT_RXRDY);
    253 		DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; // @@@@@
    254 	}
    255 
    256 	tp = tty_alloc();
    257 	tp->t_oproc = at91dbgu_start;
    258 	tp->t_param = at91dbgu_param;
    259 	tp->t_hwiflow = at91dbgu_hwiflow;
    260 
    261 	sc->sc_tty = tp;
    262 	sc->sc_rbuf = malloc(AT91DBGU_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
    263 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    264 	sc->sc_rbavail = AT91DBGU_RING_SIZE;
    265 	if (sc->sc_rbuf == NULL) {
    266 		printf("%s: unable to allocate ring buffer\n",
    267 		    device_xname(sc->sc_dev));
    268 		return;
    269 	}
    270 	sc->sc_ebuf = sc->sc_rbuf + (AT91DBGU_RING_SIZE << 1);
    271 	sc->sc_tbc = 0;
    272 
    273 	tty_attach(tp);
    274 
    275 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    276 		int maj;
    277 
    278 		/* locate the major number */
    279 		maj = cdevsw_lookup_major(&at91dbgu_cdevsw);
    280 
    281 		cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
    282 
    283 		aprint_normal("%s: console (maj %u min %u cn_dev %#"PRIx64")\n",
    284 		    device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev),
    285 		    cn_tab->cn_dev);
    286 	}
    287 
    288 	sc->sc_si = softint_establish(SOFTINT_SERIAL, at91dbgu_soft, sc);
    289 
    290 #ifdef RND_COM
    291 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    292 			  RND_TYPE_TTY, 0);
    293 #endif
    294 
    295 	/* if there are no enable/disable functions, assume the device
    296 	   is always enabled */
    297 	if (!sc->enable)
    298 		sc->enabled = 1;
    299 
    300 	/* XXX configure register */
    301 	/* xxx_config(sc) */
    302 
    303 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
    304 }
    305 
    306 static int
    307 at91dbgu_param(struct tty *tp, struct termios *t)
    308 {
    309 	struct at91dbgu_softc *sc
    310 		= device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
    311 	int s;
    312 
    313 	if (COM_ISALIVE(sc) == 0)
    314 		return (EIO);
    315 
    316 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    317 		return (EINVAL);
    318 
    319 	/*
    320 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    321 	 * is always active.
    322 	 */
    323 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
    324 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    325 		SET(t->c_cflag, CLOCAL);
    326 		CLR(t->c_cflag, HUPCL);
    327 	}
    328 
    329 	/*
    330 	 * If there were no changes, don't do anything.  This avoids dropping
    331 	 * input and improves performance when all we did was frob things like
    332 	 * VMIN and VTIME.
    333 	 */
    334 	if (tp->t_ospeed == t->c_ospeed &&
    335 	    tp->t_cflag == t->c_cflag)
    336 		return (0);
    337 
    338 	s = splserial();
    339 
    340 	sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed;
    341 
    342 	/* And copy to tty. */
    343 	tp->t_ispeed = 0;
    344 	tp->t_ospeed = t->c_ospeed;
    345 	tp->t_cflag = t->c_cflag;
    346 	at91dbgu_set(sc);
    347 
    348 	splx(s);
    349 
    350 	/*
    351 	 * Update the tty layer's idea of the carrier bit.
    352 	 * We tell tty the carrier is always on.
    353 	 */
    354 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    355 
    356 #ifdef COM_DEBUG
    357 	if (com_debug)
    358 		comstatus(sc, "comparam ");
    359 #endif
    360 
    361 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    362 		if (sc->sc_tx_stopped) {
    363 			sc->sc_tx_stopped = 0;
    364 			at91dbgu_start(tp);
    365 		}
    366 	}
    367 
    368 	return (0);
    369 }
    370 
    371 static int
    372 at91dbgu_hwiflow(struct tty *tp, int block)
    373 {
    374 	return (0);
    375 }
    376 
    377 static void
    378 at91dbgu_filltx(struct at91dbgu_softc *sc)
    379 {
    380 #if 0
    381 	bus_space_tag_t iot = sc->sc_iot;
    382 	bus_space_handle_t ioh = sc->sc_ioh;
    383 #endif
    384 	int n;
    385 
    386 	n = 0;
    387         while (DBGUREG(DBGU_SR) & DBGU_SR_TXRDY) {
    388 		if (n >= sc->sc_tbc)
    389 			break;
    390 		DBGUREG(DBGU_THR) = *(sc->sc_tba + n) & 0xff;
    391 		n++;
    392         }
    393         sc->sc_tbc -= n;
    394         sc->sc_tba += n;
    395 }
    396 
    397 static void
    398 at91dbgu_start(struct tty *tp)
    399 {
    400 	struct at91dbgu_softc *sc
    401 		= device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
    402 	int s;
    403 
    404 	if (COM_ISALIVE(sc) == 0)
    405 		return;
    406 
    407 	s = spltty();
    408 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    409 		goto out;
    410 	if (sc->sc_tx_stopped)
    411 		goto out;
    412 	if (!ttypull(tp))
    413 		goto out;
    414 
    415 	/* Grab the first contiguous region of buffer space. */
    416 	{
    417 		u_char *tba;
    418 		int tbc;
    419 
    420 		tba = tp->t_outq.c_cf;
    421 		tbc = ndqb(&tp->t_outq, 0);
    422 
    423 		(void)splserial();
    424 
    425 		sc->sc_tba = tba;
    426 		sc->sc_tbc = tbc;
    427 	}
    428 
    429 	SET(tp->t_state, TS_BUSY);
    430 	sc->sc_tx_busy = 1;
    431 
    432 	/* Output the first chunk of the contiguous buffer. */
    433 	at91dbgu_filltx(sc);
    434 
    435 	SET(sc->sc_ier, DBGU_INT_TXRDY);
    436 	DBGUREG(DBGU_IER) = DBGU_INT_TXRDY;
    437 
    438 out:
    439 	splx(s);
    440 	return;
    441 }
    442 
    443 static void
    444 at91dbgu_break(struct at91dbgu_softc *sc, int onoff)
    445 {
    446 	// @@@ we must disconnect the TX pin from the DBGU and control
    447 	// @@@ the pin directly to support this...
    448 }
    449 
    450 static void
    451 at91dbgu_shutdown(struct at91dbgu_softc *sc)
    452 {
    453 	int s;
    454 
    455 	s = splserial();
    456 
    457 	/* Turn off interrupts. */
    458 	DBGUREG(DBGU_IDR) = -1;
    459 
    460 	/* Clear any break condition set with TIOCSBRK. */
    461 	at91dbgu_break(sc, 0);
    462 	at91dbgu_set(sc);
    463 
    464 	if (sc->disable) {
    465 #ifdef DIAGNOSTIC
    466 		if (!sc->enabled)
    467 			panic("at91dbgu_shutdown: not enabled?");
    468 #endif
    469 		(*sc->disable)(sc);
    470 		sc->enabled = 0;
    471 	}
    472 	splx(s);
    473 }
    474 
    475 int
    476 at91dbgu_open(dev_t dev, int flag, int mode, struct lwp *l)
    477 {
    478 	struct at91dbgu_softc *sc;
    479 	struct tty *tp;
    480 	int s, s2;
    481 	int error;
    482 
    483 	sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    484 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
    485 		sc->sc_rbuf == NULL)
    486 		return (ENXIO);
    487 
    488 	if (!device_is_active(sc->sc_dev))
    489 		return (ENXIO);
    490 
    491 #ifdef KGDB
    492 	/*
    493 	 * If this is the kgdb port, no other use is permitted.
    494 	 */
    495 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
    496 		return (EBUSY);
    497 #endif
    498 
    499 	tp = sc->sc_tty;
    500 
    501 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    502 		return (EBUSY);
    503 
    504 	s = spltty();
    505 
    506 	/*
    507 	 * Do the following iff this is a first open.
    508 	 */
    509 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    510 		struct termios t;
    511 
    512 		tp->t_dev = dev;
    513 
    514 		s2 = splserial();
    515 
    516 		if (sc->enable) {
    517 			if ((*sc->enable)(sc)) {
    518 				splx(s2);
    519 				splx(s);
    520 				printf("%s: device enable failed\n",
    521 				       device_xname(sc->sc_dev));
    522 				return (EIO);
    523 			}
    524 			sc->enabled = 1;
    525 #if 0
    526 /* XXXXXXXXXXXXXXX */
    527 			com_config(sc);
    528 #endif
    529 		}
    530 
    531 		/* Turn on interrupts. */
    532 		sc->sc_ier |= DBGU_INT_RXRDY;
    533 		DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
    534 
    535 #if 0
    536 		/* Fetch the current modem control status, needed later. */
    537 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
    538 
    539 		/* Clear PPS capture state on first open. */
    540 		sc->sc_ppsmask = 0;
    541 		sc->ppsparam.mode = 0;
    542 #endif
    543 
    544 		splx(s2);
    545 
    546 		/*
    547 		 * Initialize the termios status to the defaults.  Add in the
    548 		 * sticky bits from TIOCSFLAGS.
    549 		 */
    550 		t.c_ispeed = 0;
    551 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
    552 			t.c_ospeed = dbgu_cn_sc.sc_ospeed;
    553 			t.c_cflag = dbgu_cn_sc.sc_cflag;
    554 		} else {
    555 			t.c_ospeed = TTYDEF_SPEED;
    556 			t.c_cflag = TTYDEF_CFLAG;
    557 		}
    558 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    559 			SET(t.c_cflag, CLOCAL);
    560 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    561 			SET(t.c_cflag, CRTSCTS);
    562 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    563 			SET(t.c_cflag, MDMBUF);
    564 		/* Make sure at91dbgu_param() will do something. */
    565 		tp->t_ospeed = 0;
    566 		(void) at91dbgu_param(tp, &t);
    567 		tp->t_iflag = TTYDEF_IFLAG;
    568 		tp->t_oflag = TTYDEF_OFLAG;
    569 		tp->t_lflag = TTYDEF_LFLAG;
    570 		ttychars(tp);
    571 		ttsetwater(tp);
    572 
    573 		s2 = splserial();
    574 
    575 		/* Clear the input ring, and unblock. */
    576 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
    577 		sc->sc_rbavail = AT91DBGU_RING_SIZE;
    578 		at91dbgu_iflush(sc);
    579 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
    580 
    581 #ifdef COM_DEBUG
    582 		if (at91dbgu_debug)
    583 			comstatus(sc, "at91dbgu_open  ");
    584 #endif
    585 
    586 		splx(s2);
    587 	}
    588 
    589 	splx(s);
    590 
    591 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    592 	if (error)
    593 		goto bad;
    594 
    595 	error = (*tp->t_linesw->l_open)(dev, tp);
    596 	if (error)
    597 		goto bad;
    598 
    599 	return (0);
    600 
    601 bad:
    602 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    603 		/*
    604 		 * We failed to open the device, and nobody else had it opened.
    605 		 * Clean up the state as appropriate.
    606 		 */
    607 		at91dbgu_shutdown(sc);
    608 	}
    609 
    610 	return (error);
    611 }
    612 
    613 int
    614 at91dbgu_close(dev_t dev, int flag, int mode, struct lwp *l)
    615 {
    616 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    617 	struct tty *tp = sc->sc_tty;
    618 
    619 	/* XXX This is for cons.c. */
    620 	if (!ISSET(tp->t_state, TS_ISOPEN))
    621 		return (0);
    622 
    623 	(*tp->t_linesw->l_close)(tp, flag);
    624 	ttyclose(tp);
    625 
    626 	if (COM_ISALIVE(sc) == 0)
    627 		return (0);
    628 
    629 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    630 		/*
    631 		 * Although we got a last close, the device may still be in
    632 		 * use; e.g. if this was the dialout node, and there are still
    633 		 * processes waiting for carrier on the non-dialout node.
    634 		 */
    635 		at91dbgu_shutdown(sc);
    636 	}
    637 
    638 	return (0);
    639 }
    640 
    641 int
    642 at91dbgu_read(dev_t dev, struct uio *uio, int flag)
    643 {
    644 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    645 	struct tty *tp = sc->sc_tty;
    646 
    647 	if (COM_ISALIVE(sc) == 0)
    648 		return (EIO);
    649 
    650 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    651 }
    652 
    653 int
    654 at91dbgu_write(dev_t dev, struct uio *uio, int flag)
    655 {
    656 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    657 	struct tty *tp = sc->sc_tty;
    658 
    659 	if (COM_ISALIVE(sc) == 0)
    660 		return (EIO);
    661 
    662 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    663 }
    664 
    665 int
    666 at91dbgu_poll(dev_t dev, int events, struct lwp *l)
    667 {
    668 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    669 	struct tty *tp = sc->sc_tty;
    670 
    671 	if (COM_ISALIVE(sc) == 0)
    672 		return (EIO);
    673 
    674 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    675 }
    676 
    677 struct tty *
    678 at91dbgu_tty(dev_t dev)
    679 {
    680 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    681 	struct tty *tp = sc->sc_tty;
    682 
    683 	return (tp);
    684 }
    685 
    686 int
    687 at91dbgu_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    688 {
    689 	struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
    690 	struct tty *tp = sc->sc_tty;
    691 	int error;
    692 	int s;
    693 
    694 	if (COM_ISALIVE(sc) == 0)
    695 		return (EIO);
    696 
    697 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    698 	if (error != EPASSTHROUGH)
    699 		return (error);
    700 
    701 	error = ttioctl(tp, cmd, data, flag, l);
    702 	if (error != EPASSTHROUGH)
    703 		return (error);
    704 
    705 	error = 0;
    706 
    707 	s = splserial();
    708 
    709 	switch (cmd) {
    710 	case TIOCSBRK:
    711 		at91dbgu_break(sc, 1);
    712 		break;
    713 
    714 	case TIOCCBRK:
    715 		at91dbgu_break(sc, 0);
    716 		break;
    717 
    718 	case TIOCGFLAGS:
    719 		*(int *)data = sc->sc_swflags;
    720 		break;
    721 
    722 	case TIOCSFLAGS:
    723 		error = kauth_authorize_device_tty(l->l_cred,
    724 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    725 		if (error)
    726 			break;
    727 		sc->sc_swflags = *(int *)data;
    728 		break;
    729 
    730 	default:
    731 		error = EPASSTHROUGH;
    732 		break;
    733 	}
    734 
    735 	splx(s);
    736 
    737 	return (error);
    738 }
    739 
    740 /*
    741  * Stop output on a line.
    742  */
    743 void
    744 at91dbgu_stop(struct tty *tp, int flag)
    745 {
    746 	struct at91dbgu_softc *sc
    747 		= device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
    748 	int s;
    749 
    750 	s = splserial();
    751 	if (ISSET(tp->t_state, TS_BUSY)) {
    752 		/* Stop transmitting at the next chunk. */
    753 		sc->sc_tbc = 0;
    754 		if (!ISSET(tp->t_state, TS_TTSTOP))
    755 			SET(tp->t_state, TS_FLUSH);
    756 	}
    757 	splx(s);
    758 }
    759 
    760 #if 0
    761 static u_int
    762 cflag2lcrhi(tcflag_t cflag)
    763 {
    764 	uint32_t	mr;
    765 
    766 	switch (cflag & CSIZE) {
    767 	default:
    768 		mr = 0x0;
    769 		break;
    770 	}
    771 #if 0
    772 	mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
    773 	mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
    774 	mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
    775 	mr |= LinCtrlHigh_FEN;  /* FIFO always enabled */
    776 #endif
    777 	mr |= DBGU_MR_PAR_NONE;
    778 	return (mr);
    779 }
    780 #endif
    781 
    782 static void
    783 at91dbgu_iflush(struct at91dbgu_softc *sc)
    784 {
    785 #if 0
    786 	bus_space_tag_t iot = sc->sc_iot;
    787 	bus_space_handle_t ioh = sc->sc_ioh;
    788 #endif
    789 #ifdef DIAGNOSTIC
    790 	int reg;
    791 #endif
    792 	int timo;
    793 
    794 #ifdef DIAGNOSTIC
    795 	reg = 0xffff;
    796 #endif
    797 	timo = 50000;
    798 	/* flush any pending I/O */
    799 	while (DBGUREG(DBGU_SR) & DBGU_SR_RXRDY
    800 	       && --timo)
    801 #ifdef DIAGNOSTIC
    802 		reg =
    803 #else
    804 			(void)
    805 #endif
    806 			DBGUREG(DBGU_RHR);
    807 #ifdef DIAGNOSTIC
    808 	if (!timo)
    809 		printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev),
    810 		       reg);
    811 #endif
    812 }
    813 
    814 static void
    815 at91dbgu_set(struct at91dbgu_softc *sc)
    816 {
    817 	DBGUREG(DBGU_MR) = DBGU_MR_PAR_NONE;
    818 	DBGUREG(DBGU_BRGR) = sc->sc_brgr;
    819 	DBGUREG(DBGU_CR) = DBGU_CR_TXEN | DBGU_CR_RXEN; // @@@ just in case
    820 }
    821 
    822 void
    823 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
    824 		   int ospeed, tcflag_t cflag);
    825 void
    826 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
    827 		   int ospeed, tcflag_t cflag)
    828 {
    829 	cn_tab = &at91dbgu_cons;
    830 	cn_init_magic(&at91dbgu_cnm_state);
    831 	cn_set_magic("\047\001");
    832 
    833 	dbgu_cn_sc.sc_iot = iot;
    834 	dbgu_cn_sc.sc_ioh = ioh;
    835 	dbgu_cn_sc.sc_hwbase = iobase;
    836 	dbgu_cn_sc.sc_ospeed = ospeed;
    837 	dbgu_cn_sc.sc_cflag = cflag;
    838 
    839 	DBGU_INIT(AT91_MSTCLK, ospeed);
    840 }
    841 
    842 void at91dbgu_attach_cn(bus_space_tag_t iot, int ospeed, int cflag)
    843 {
    844 	bus_space_handle_t ioh;
    845 	bus_space_map(iot, AT91DBGU_BASE, AT91DBGU_SIZE, 0, &ioh);
    846 	at91dbgu_cn_attach(iot, AT91DBGU_BASE, ioh, ospeed, cflag);
    847 }
    848 
    849 void
    850 at91dbgu_cn_probe(struct consdev *cp)
    851 {
    852 	cp->cn_pri = CN_REMOTE;
    853 }
    854 
    855 void
    856 at91dbgu_cn_pollc(dev_t dev, int on)
    857 {
    858 	if (on) {
    859 		// enable polling mode
    860 		DBGUREG(DBGU_IDR) = DBGU_INT_RXRDY;
    861 	} else {
    862 		// disable polling mode
    863 		DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
    864 	}
    865 }
    866 
    867 void
    868 at91dbgu_cn_putc(dev_t dev, int c)
    869 {
    870 #if 0
    871 	bus_space_tag_t		iot = dbgu_cn_sc.sc_iot;
    872 	bus_space_handle_t	ioh = dbgu_cn_sc.sc_ioh;
    873 #endif
    874 	DBGU_PUTC(c);
    875 
    876 #ifdef DEBUG
    877 	if (c == '\r') {
    878 		int s = splserial();
    879 		while((DBGUREG(DBGU_SR) & DBGU_SR_TXEMPTY) == 0) {
    880 			splx(s);
    881 			s = splserial();
    882 		}
    883 		splx(s);
    884 	}
    885 #endif
    886 
    887 }
    888 
    889 int
    890 at91dbgu_cn_getc(dev_t dev)
    891 {
    892 	int			c, sr;
    893 	int			s;
    894 #if 0
    895 	bus_space_tag_t		iot = dbgu_cn_sc.sc_iot;
    896 	bus_space_handle_t	ioh = dbgu_cn_sc.sc_ioh;
    897 #endif
    898 
    899         s = splserial();
    900 
    901 	while ((c = DBGU_PEEKC()) == -1) {
    902 	  splx(s);
    903 	  s = splserial();
    904 	}
    905 		;
    906 	sr = DBGUREG(DBGU_SR);
    907 	if (ISSET(sr, DBGU_SR_FRAME) && c == 0) {
    908 		DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA;	// reset status bits
    909 		c = CNC_BREAK;
    910 	}
    911 #ifdef DDB
    912 	extern int db_active;
    913 	if (!db_active)
    914 #endif
    915 	{
    916 		int cn_trapped = 0; /* unused */
    917 
    918 		cn_check_magic(dev, c, at91dbgu_cnm_state);
    919 	}
    920 	splx(s);
    921 
    922 	c &= 0xff;
    923 
    924 	return (c);
    925 }
    926 
    927 inline static void
    928 at91dbgu_txsoft(struct at91dbgu_softc *sc, struct tty *tp)
    929 {
    930 	CLR(tp->t_state, TS_BUSY);
    931 	if (ISSET(tp->t_state, TS_FLUSH))
    932 		CLR(tp->t_state, TS_FLUSH);
    933         else
    934 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
    935 	(*tp->t_linesw->l_start)(tp);
    936 }
    937 
    938 inline static void
    939 at91dbgu_rxsoft(struct at91dbgu_softc *sc, struct tty *tp)
    940 {
    941 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
    942 	u_char *get, *end;
    943 	u_int cc, scc;
    944 	u_char sts;
    945 	int code;
    946 	int s;
    947 
    948 	end = sc->sc_ebuf;
    949 	get = sc->sc_rbget;
    950 	scc = cc = AT91DBGU_RING_SIZE - sc->sc_rbavail;
    951 #if 0
    952 	if (cc == AT91DBGU_RING_SIZE) {
    953 		sc->sc_floods++;
    954 		if (sc->sc_errors++ == 0)
    955 			callout_reset(&sc->sc_diag_callout, 60 * hz,
    956 			    comdiag, sc);
    957 	}
    958 #endif
    959 	while (cc) {
    960 		code = get[0];
    961 		sts = get[1];
    962 		if (ISSET(sts, DBGU_SR_PARE | DBGU_SR_FRAME | DBGU_SR_OVRE)) {
    963 #if 0
    964 			if (ISSET(lsr, DR_ROR)) {
    965 				sc->sc_overflows++;
    966 				if (sc->sc_errors++ == 0)
    967 					callout_reset(&sc->sc_diag_callout,
    968 					    60 * hz, comdiag, sc);
    969 			}
    970 #endif
    971 			if (ISSET(sts, (DBGU_SR_FRAME | DBGU_SR_OVRE)))
    972 				SET(code, TTY_FE);
    973 			if (ISSET(sts, DBGU_SR_PARE))
    974 				SET(code, TTY_PE);
    975 		}
    976 		if ((*rint)(code, tp) == -1) {
    977 			/*
    978 			 * The line discipline's buffer is out of space.
    979 			 */
    980 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
    981 				/*
    982 				 * We're either not using flow control, or the
    983 				 * line discipline didn't tell us to block for
    984 				 * some reason.  Either way, we have no way to
    985 				 * know when there's more space available, so
    986 				 * just drop the rest of the data.
    987 				 */
    988 				get += cc << 1;
    989 				if (get >= end)
    990 					get -= AT91DBGU_RING_SIZE << 1;
    991 				cc = 0;
    992 			} else {
    993 				/*
    994 				 * Don't schedule any more receive processing
    995 				 * until the line discipline tells us there's
    996 				 * space available (through comhwiflow()).
    997 				 * Leave the rest of the data in the input
    998 				 * buffer.
    999 				 */
   1000 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
   1001 			}
   1002 			break;
   1003 		}
   1004 		get += 2;
   1005 		if (get >= end)
   1006 			get = sc->sc_rbuf;
   1007 		cc--;
   1008 	}
   1009 
   1010 	if (cc != scc) {
   1011 		sc->sc_rbget = get;
   1012 		s = splserial();
   1013 
   1014 		cc = sc->sc_rbavail += scc - cc;
   1015 		/* Buffers should be ok again, release possible block. */
   1016 		if (cc >= 1) {
   1017 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
   1018 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1019 				DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
   1020 			}
   1021 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
   1022 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
   1023 #if 0
   1024 				com_hwiflow(sc);
   1025 #endif
   1026 			}
   1027 		}
   1028 		splx(s);
   1029 	}
   1030 }
   1031 
   1032 static void
   1033 at91dbgu_soft(void* arg)
   1034 {
   1035 	struct at91dbgu_softc *sc = arg;
   1036 
   1037 	if (COM_ISALIVE(sc) == 0)
   1038 		return;
   1039 
   1040 	if (sc->sc_rx_ready) {
   1041 		sc->sc_rx_ready = 0;
   1042 		at91dbgu_rxsoft(sc, sc->sc_tty);
   1043 	}
   1044 	if (sc->sc_tx_done) {
   1045 		sc->sc_tx_done = 0;
   1046 		at91dbgu_txsoft(sc, sc->sc_tty);
   1047 	}
   1048 }
   1049 
   1050 
   1051 static int
   1052 dbgu_intr(void* arg)
   1053 {
   1054 	struct at91dbgu_softc *sc = arg;
   1055 #if 0
   1056 	bus_space_tag_t iot = sc->sc_iot;
   1057 	bus_space_handle_t ioh = sc->sc_ioh;
   1058 #endif
   1059 	u_char *put, *end;
   1060 	u_int cc;
   1061 	uint32_t imr, sr;
   1062 	int c = 0;
   1063 	imr = DBGUREG(DBGU_IMR);
   1064 #if 0
   1065 	if (!imr)
   1066 		return 0;
   1067 #endif
   1068 	sr = DBGUREG(DBGU_SR);
   1069 	if (!(sr & imr)) {
   1070 		if (sr & DBGU_SR_RXRDY) {
   1071 //			printf("sr=0x%08x imr=0x%08x\n", sr, imr);
   1072 		}
   1073 		return 0;
   1074 	}
   1075 
   1076 	end = sc->sc_ebuf;
   1077 	put = sc->sc_rbput;
   1078 	cc = sc->sc_rbavail;
   1079 
   1080 	// ok, we DO have some interrupts to serve!
   1081 	if (sr & DBGU_SR_RXRDY) {
   1082 		int cn_trapped = 0;
   1083 
   1084 		c = DBGUREG(DBGU_RHR);
   1085 		if (ISSET(sr, (DBGU_SR_OVRE | DBGU_SR_FRAME | DBGU_SR_PARE)))
   1086 			DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA;
   1087 		if (ISSET(sr, DBGU_SR_FRAME) && c == 0) {
   1088 			c = CNC_BREAK;
   1089 		}
   1090 #ifdef DDB
   1091 		extern int db_active;
   1092 		if (!db_active)
   1093 #endif
   1094 			cn_check_magic(cn_tab->cn_dev, c, at91dbgu_cnm_state);
   1095 		if (!cn_trapped && cc) {
   1096 			put[0] = c & 0xff;
   1097 			put[1] = sr & 0xff;
   1098 			put += 2;
   1099 			if (put >= end)
   1100 				put = sc->sc_rbuf;
   1101 			cc--;
   1102 			/*
   1103 			 * Current string of incoming characters ended because
   1104 			 * no more data was available or we ran out of space.
   1105 			 * Schedule a receive event if any data was received.
   1106 			 * If we're out of space, turn off receive interrupts.
   1107 			 */
   1108 			sc->sc_rbput = put;
   1109 			sc->sc_rbavail = cc;
   1110 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
   1111 				sc->sc_rx_ready = 1;
   1112 
   1113 			/*
   1114 			 * See if we are in danger of overflowing a buffer. If
   1115 			 * so, use hardware flow control to ease the pressure.
   1116 			 */
   1117 
   1118 			/* but at91dbgu cannot (yet). X-( */
   1119 
   1120 			/*
   1121 			 * If we're out of space, disable receive interrupts
   1122 			 * until the queue has drained a bit.
   1123 			 */
   1124 			if (!cc) {
   1125 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
   1126 			}
   1127 		}
   1128 	}
   1129 
   1130 	/*
   1131 	 * Done handling any receive interrupts. See if data can be
   1132 	 * transmitted as well. Schedule tx done event if no data left
   1133 	 * and tty was marked busy.
   1134 	 */
   1135 
   1136 	if (ISSET(sr, DBGU_SR_TXRDY) && sc->sc_tbc > 0) {
   1137 		/* Output the next chunk of the contiguous buffer, if any. */
   1138 		at91dbgu_filltx(sc);
   1139 	} else {
   1140 		/* Disable transmit completion interrupts if necessary. */
   1141 		DBGUREG(DBGU_IDR) = DBGU_INT_TXRDY;
   1142 		if (sc->sc_tx_busy) {
   1143 			sc->sc_tx_busy = 0;
   1144 			sc->sc_tx_done = 1;
   1145 		}
   1146 	}
   1147 
   1148 	/* Wake up the poller. */
   1149 	softint_schedule(sc->sc_si);
   1150 #if 0
   1151 #ifdef RND_COM
   1152 	rnd_add_uint32(&sc->rnd_source, imr ^ sr ^ c);
   1153 #endif
   1154 #endif
   1155 
   1156 	return (1);
   1157 }
   1158