Home | History | Annotate | Line # | Download | only in dev
xlcom.c revision 1.10
      1 /* 	$NetBSD: xlcom.c,v 1.10 2014/03/16 05:20:24 dholland Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006 Jachym Holecek
      5  * All rights reserved.
      6  *
      7  * Written for DFC Design, s.r.o.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  *
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  *
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: xlcom.c,v 1.10 2014/03/16 05:20:24 dholland Exp $");
     34 
     35 #include "opt_kgdb.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/file.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/kauth.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/tty.h>
     47 #include <sys/time.h>
     48 #include <sys/syslog.h>
     49 #include <sys/intr.h>
     50 #include <sys/bus.h>
     51 
     52 #if defined(KGDB)
     53 #include <sys/kgdb.h>
     54 #endif /* KGDB */
     55 
     56 #include <dev/cons.h>
     57 
     58 #include <evbppc/virtex/virtex.h>
     59 #include <evbppc/virtex/dev/xcvbusvar.h>
     60 #include <evbppc/virtex/dev/xlcomreg.h>
     61 
     62 
     63 #define XLCOM_UNIT_MASK 	0x7f
     64 #define XLCOM_DIALOUT_MASK 	0x80
     65 
     66 #define UNIT(dev) 		(minor(dev) & XLCOM_UNIT_MASK)
     67 #define DIALOUT(dev) 		(minor(dev) & XLCOM_DIALOUT_MASK)
     68 
     69 #define XLCOM_CHAR_PE 		0x8000 	/* Parity error flag */
     70 #define XLCOM_CHAR_FE 		0x4000 	/* Frame error flag */
     71 #define next(idx) 		(void)((idx) = ((idx) + 1) % XLCOM_RXBUF_SIZE)
     72 
     73 #define XLCOM_RXBUF_SIZE 	1024
     74 
     75 struct xlcom_softc {
     76 	device_t 		sc_dev;
     77 	struct tty 		*sc_tty;
     78 	void 			*sc_ih;
     79 
     80 	bus_space_tag_t 	sc_iot;
     81 	bus_space_handle_t 	sc_ioh;
     82 
     83 	/* Deffered execution context. */
     84 	void 			*sc_rx_soft;
     85 	void 			*sc_tx_soft;
     86 
     87 	/* Receive buffer */
     88 	u_short 		sc_rbuf[XLCOM_RXBUF_SIZE];
     89 	volatile u_int 		sc_rput;
     90 	volatile u_int 		sc_rget;
     91 	volatile u_int 		sc_ravail;
     92 
     93  	/* Transmit buffer */
     94 	u_char 			*sc_tba;
     95 	u_int 			sc_tbc;
     96 };
     97 
     98 static int 	xlcom_intr(void *);
     99 static void 	xlcom_rx_soft(void *);
    100 static void 	xlcom_tx_soft(void *);
    101 static void 	xlcom_reset(bus_space_tag_t, bus_space_handle_t);
    102 static int 	xlcom_busy_getc(bus_space_tag_t, bus_space_handle_t);
    103 static void 	xlcom_busy_putc(bus_space_tag_t, bus_space_handle_t, int);
    104 
    105 /* System console interface. */
    106 static int 	xlcom_cngetc(dev_t);
    107 static void 	xlcom_cnputc(dev_t, int);
    108 void 		xlcom_cninit(struct consdev *);
    109 
    110 #if defined(KGDB)
    111 
    112 void 		xlcom_kgdbinit(void);
    113 static void 	xlcom_kgdb_putc(void *, int);
    114 static int 	xlcom_kgdb_getc(void *);
    115 
    116 #endif /* KGDB */
    117 
    118 static struct cnm_state 	xlcom_cnm_state;
    119 
    120 struct consdev consdev_xlcom = {
    121 	.cn_probe 	= nullcnprobe,
    122 	.cn_init 	= xlcom_cninit,
    123 	.cn_getc 	= xlcom_cngetc,
    124 	.cn_putc 	= xlcom_cnputc,
    125 	.cn_pollc 	= nullcnpollc,
    126 	.cn_pri 	= CN_REMOTE
    127 };
    128 
    129 /* Character device. */
    130 static dev_type_open(xlcom_open);
    131 static dev_type_read(xlcom_read);
    132 static dev_type_write(xlcom_write);
    133 static dev_type_ioctl(xlcom_ioctl);
    134 static dev_type_poll(xlcom_poll);
    135 static dev_type_close(xlcom_close);
    136 
    137 static dev_type_tty(xlcom_tty);
    138 static dev_type_stop(xlcom_stop);
    139 
    140 const struct cdevsw xlcom_cdevsw = {
    141 	.d_open = xlcom_open,
    142 	.d_close = xlcom_close,
    143 	.d_read = xlcom_read,
    144 	.d_write = xlcom_write,
    145 	.d_ioctl = xlcom_ioctl,
    146 	.d_stop = xlcom_stop,
    147 	.d_tty = xlcom_tty,
    148 	.d_poll = xlcom_poll,
    149 	.d_mmap = nommap,
    150 	.d_kqfilter = ttykqfilter,
    151 	.d_flag = D_TTY
    152 };
    153 
    154 extern struct cfdriver xlcom_cd;
    155 
    156 /* Terminal line. */
    157 static int 	xlcom_param(struct tty *, struct termios *);
    158 static void 	xlcom_start(struct tty *);
    159 
    160 /* Generic device. */
    161 static void 	xlcom_attach(device_t, device_t, void *);
    162 
    163 CFATTACH_DECL_NEW(xlcom, sizeof(struct xlcom_softc),
    164     xcvbus_child_match, xlcom_attach, NULL, NULL);
    165 
    166 
    167 static void
    168 xlcom_attach(device_t parent, device_t self, void *aux)
    169 {
    170 	struct xcvbus_attach_args 	*vaa = aux;
    171 	struct xlcom_softc 		*sc = device_private(self);
    172 	struct tty 			*tp;
    173 	dev_t 				dev;
    174 
    175 	aprint_normal(": UartLite serial port\n");
    176 
    177 	sc->sc_dev = self;
    178 
    179 #if defined(KGDB)
    180 	/* We don't want to share kgdb port with the user. */
    181 	if (sc->sc_iot == kgdb_iot && sc->sc_ioh == kgdb_ioh) {
    182 		aprint_error_dev(self, "already in use by kgdb\n");
    183 		return;
    184 	}
    185 #endif /* KGDB */
    186 
    187 	if ((sc->sc_ih = intr_establish(vaa->vaa_intr, IST_LEVEL, IPL_SERIAL,
    188 	    xlcom_intr, sc)) == NULL) {
    189 		aprint_error_dev(self, "could not establish interrupt\n");
    190 		return ;
    191 	}
    192 
    193 	dev = makedev(cdevsw_lookup_major(&xlcom_cdevsw), device_unit(self));
    194 	if (cn_tab == &consdev_xlcom) {
    195 		cn_init_magic(&xlcom_cnm_state);
    196 		cn_set_magic("\x23\x2e"); 		/* #. */
    197 		cn_tab->cn_dev = dev;
    198 
    199 		sc->sc_iot = consdev_iot;
    200 		sc->sc_ioh = consdev_ioh;
    201 
    202 		aprint_normal_dev(self, "console\n");
    203 	} else {
    204 		sc->sc_iot = vaa->vaa_iot;
    205 
    206 		if (bus_space_map(vaa->vaa_iot, vaa->vaa_addr, XLCOM_SIZE, 0,
    207 		    &sc->sc_ioh) != 0) {
    208 			aprint_error_dev(self, "could not map registers\n");
    209 			return;
    210 		}
    211 
    212 		/* Reset FIFOs. */
    213 		xlcom_reset(sc->sc_iot, sc->sc_ioh);
    214 	}
    215 
    216 	sc->sc_tbc = 0;
    217 	sc->sc_tba = NULL;
    218 
    219 	sc->sc_rput = sc->sc_rget = 0;
    220 	sc->sc_ravail = XLCOM_RXBUF_SIZE;
    221 
    222 	sc->sc_rx_soft = softint_establish(SOFTINT_SERIAL, xlcom_rx_soft, sc);
    223 	sc->sc_tx_soft = softint_establish(SOFTINT_SERIAL, xlcom_tx_soft, sc);
    224 
    225 	if (sc->sc_rx_soft == NULL || sc->sc_tx_soft == NULL) {
    226 		aprint_error_dev(self,
    227 		    "could not establish Rx or Tx softintr\n");
    228 		return;
    229 	}
    230 
    231 	tp = tty_alloc();
    232 	tp->t_dev = dev;
    233 	tp->t_oproc = xlcom_start;
    234 	tp->t_param = xlcom_param;
    235 	tp->t_hwiflow = NULL; 				/* No HW flow control */
    236 	tty_attach(tp);
    237 
    238 	/* XXX anything else to do for console early? */
    239 	if (cn_tab == &consdev_xlcom) {
    240 		/* Before first open, so that we can enter ddb(4). */
    241 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
    242 		    CNTL_INTR_EN);
    243 	}
    244 
    245 	sc->sc_tty = tp;
    246 }
    247 
    248 /*
    249  * Misc hooks.
    250  */
    251 static void
    252 xlcom_tx_soft(void *arg)
    253 {
    254 	struct xlcom_softc 	*sc = (struct xlcom_softc *)arg;
    255 	struct tty 		*tp = sc->sc_tty;
    256 
    257 	if (tp->t_state & TS_FLUSH)
    258 		tp->t_state &= ~TS_FLUSH;
    259 	else
    260 		ndflush(&tp->t_outq,
    261 		    (int)(sc->sc_tba - tp->t_outq.c_cf));
    262 	(tp->t_linesw->l_start)(tp);
    263 }
    264 
    265 static void
    266 xlcom_rx_soft(void *arg)
    267 {
    268 	struct xlcom_softc 	*sc = (struct xlcom_softc *)arg;
    269 	struct tty 		*tp = sc->sc_tty;
    270 	int 			(*rint)(int, struct tty *);
    271 	u_short 		c;
    272 	int 			d;
    273 
    274 	/*
    275 	 * XXX: we don't do any synchronization, rput may change below
    276 	 * XXX: our hands -- it doesn't seem to be troublesome as long
    277 	 * XXX: as "sc->sc_rget = sc->sc_rput" is atomic.
    278 	 */
    279 	rint = tp->t_linesw->l_rint;
    280 
    281 	/* Run until we catch our tail. */
    282 	while (sc->sc_rput != sc->sc_rget) {
    283 		c = sc->sc_rbuf[sc->sc_rget];
    284 
    285 		next(sc->sc_rget);
    286 		sc->sc_ravail++;
    287 
    288 		d = (c & 0xff) |
    289 		    ((c & XLCOM_CHAR_PE) != 0 ? TTY_PE : 0) |
    290 		    ((c & XLCOM_CHAR_FE) != 0 ? TTY_FE : 0);
    291 
    292 		/*
    293 		 * Drop the rest of data if discipline runs out of buffer
    294 		 * space. We'd use flow control here, if we had any.
    295 		 */
    296 		if ((rint)(d, tp) == -1) {
    297 			sc->sc_rget = sc->sc_rput;
    298 			return ;
    299 		}
    300 	}
    301 }
    302 
    303 static void
    304 xlcom_send_chunk(struct xlcom_softc *sc)
    305 {
    306 	uint32_t 		stat;
    307 
    308 	/* Chunk flushed, no more data available. */
    309 	if (sc->sc_tbc <= 0) {
    310 		return ;
    311 	}
    312 
    313 	/* Run as long as we have space and data. */
    314 	while (sc->sc_tbc > 0) {
    315 		stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    316 		if (stat & STAT_TX_FULL)
    317 			break;
    318 
    319 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_TX_FIFO,
    320 		    *sc->sc_tba);
    321 
    322 		sc->sc_tbc--;
    323 		sc->sc_tba++;
    324 	}
    325 
    326 	/* Try to grab more data while FIFO drains. */
    327 	if (sc->sc_tbc == 0) {
    328 		sc->sc_tty->t_state &= ~TS_BUSY;
    329 		softint_schedule(sc->sc_tx_soft);
    330 	}
    331 }
    332 
    333 static void
    334 xlcom_recv_chunk(struct xlcom_softc *sc)
    335 {
    336 	uint32_t 		stat;
    337 	u_short 		c;
    338 	u_int 			n;
    339 
    340 	n = sc->sc_ravail;
    341 	stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    342 
    343 	/* Run as long as we have data and space. */
    344 	while ((stat & STAT_RX_DATA) != 0 && sc->sc_ravail > 0) {
    345 		c = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_RX_FIFO);
    346 
    347 		cn_check_magic(sc->sc_tty->t_dev, c, xlcom_cnm_state);
    348 
    349 		/* XXX: Should we pass rx-overrun upstream too? */
    350 		c |= ((stat & STAT_PARITY_ERR) != 0 ? XLCOM_CHAR_PE : 0) |
    351 		     ((stat & STAT_FRAME_ERR) != 0 ? XLCOM_CHAR_FE : 0);
    352 		sc->sc_rbuf[sc->sc_rput] = c;
    353 		sc->sc_ravail--;
    354 
    355 		next(sc->sc_rput);
    356 		stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    357 	}
    358 
    359 	/* Shedule completion hook if we received any. */
    360 	if (n != sc->sc_ravail)
    361 		softint_schedule(sc->sc_rx_soft);
    362 }
    363 
    364 static int
    365 xlcom_intr(void *arg)
    366 {
    367 	struct xlcom_softc 	*sc = arg;
    368 	uint32_t 		stat;
    369 
    370 	/*
    371 	 * Xilinx DS422, "OPB UART Lite v1.00b"
    372 	 *
    373 	 * If interrupts are enabled, an interrupt is generated when one
    374 	 * of the following conditions is true:
    375 	 */
    376 	stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    377 
    378 	/*
    379 	 * 1. When there exists any valid character in the receive FIFO,
    380 	 *    the interrupt stays active until the receive FIFO is empty.
    381 	 *    This is a level interrupt.
    382 	 */
    383 	if (stat & STAT_RX_DATA)
    384 		xlcom_recv_chunk(sc);
    385 
    386 	/*
    387 	 * 2. When the transmit FIFO goes from not empty to empty, such
    388 	 *    as when the last character in the transmit FIFO is transmitted,
    389 	 *    the interrupt is only active one clock cycle. This is an
    390 	 *    edge interrupt.
    391 	 */
    392 	if (stat & STAT_TX_EMPTY)
    393 		xlcom_send_chunk(sc);
    394 
    395 	return (0);
    396 }
    397 
    398 /*
    399  * Character device.
    400  */
    401 static int
    402 xlcom_open(dev_t dev, int flags, int mode, struct lwp *l)
    403 {
    404 	struct xlcom_softc 	*sc;
    405 	struct tty 		*tp;
    406 	int 			error, s;
    407 
    408 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    409 	if (sc == NULL)
    410 		return (ENXIO);
    411 
    412 	tp = sc->sc_tty;
    413 
    414 	s = spltty(); 							/* { */
    415 
    416 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
    417 	    tp) != 0) {
    418 	    	error = EBUSY;
    419 	    	goto fail;
    420 	}
    421 
    422 	/* Is this the first open? */
    423 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    424 		tp->t_dev = dev;
    425 
    426 		/* Values hardwired at synthesis time. XXXFreza: xparam.h */
    427 		tp->t_ispeed = tp->t_ospeed = B38400;
    428 		tp->t_cflag = CLOCAL | CREAD | CS8;
    429 		tp->t_iflag = TTYDEF_IFLAG;
    430 		tp->t_oflag = TTYDEF_OFLAG;
    431 		tp->t_lflag = TTYDEF_LFLAG;
    432 
    433 		ttychars(tp);
    434 		ttsetwater(tp);
    435 
    436 		/* Enable interrupt. */
    437 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
    438 		    CNTL_INTR_EN);
    439 	}
    440 
    441 	error = ttyopen(tp, DIALOUT(dev), (flags & O_NONBLOCK));
    442 	if (error)
    443 		goto fail;
    444 
    445 	error = (tp->t_linesw->l_open)(dev, tp);
    446 	if (error)
    447 		goto fail;
    448 
    449 	splx(s); 							/* } */
    450 	return (0);
    451 
    452  fail:
    453 	/* XXXFreza: Shutdown if nobody else has the device open. */
    454 	splx(s);
    455 	return (error);
    456 }
    457 
    458 static int
    459 xlcom_read(dev_t dev, struct uio *uio, int flag)
    460 {
    461 	struct xlcom_softc 	*sc;
    462 	struct tty 		*tp;
    463 
    464 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    465 	if (sc == NULL)
    466 		return (ENXIO);
    467 	tp = sc->sc_tty;
    468 
    469 	return (tp->t_linesw->l_read)(tp, uio, flag);
    470 }
    471 
    472 static int
    473 xlcom_write(dev_t dev, struct uio *uio, int flag)
    474 {
    475 	struct xlcom_softc 	*sc;
    476 	struct tty 		*tp;
    477 
    478 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    479 	if (sc == NULL)
    480 		return (ENXIO);
    481 	tp = sc->sc_tty;
    482 
    483 	return (tp->t_linesw->l_write)(tp, uio, flag);
    484 }
    485 
    486 static int
    487 xlcom_poll(dev_t dev, int events, struct lwp *l)
    488 {
    489 	struct xlcom_softc 	*sc;
    490 	struct tty 		*tp;
    491 
    492 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    493 	if (sc == NULL)
    494 		return (ENXIO);
    495 	tp = sc->sc_tty;
    496 
    497 	return (tp->t_linesw->l_poll)(tp, events, l);
    498 }
    499 
    500 static struct tty *
    501 xlcom_tty(dev_t dev)
    502 {
    503 	struct xlcom_softc 	*sc;
    504 	struct tty 		*tp;
    505 
    506 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    507 	if (sc == NULL)
    508 		return (NULL);
    509 	tp = sc->sc_tty;
    510 
    511 	return (tp);
    512 }
    513 
    514 static int
    515 xlcom_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    516 {
    517 	struct xlcom_softc 	*sc;
    518 	struct tty 		*tp;
    519 	int 			error;
    520 
    521 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    522 	if (sc == NULL)
    523 		return (ENXIO);
    524 	tp = sc->sc_tty;
    525 
    526 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    527 	if (error != EPASSTHROUGH)
    528 		return (error);
    529 
    530 	error = ttioctl(tp, cmd, data, flag, l);
    531 	if (error != EPASSTHROUGH)
    532 		return (error);
    533 
    534 	/* XXXFreza: error = 0; switch (cmd); return error; */
    535 
    536 	return (error);
    537 }
    538 
    539 static int
    540 xlcom_close(dev_t dev, int flag, int mode, struct lwp *l)
    541 {
    542 	struct xlcom_softc 	*sc;
    543 	struct tty 		*tp;
    544 
    545 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    546 	if (sc == NULL)
    547 		return (ENXIO);
    548 	tp = sc->sc_tty;
    549 
    550 	(tp->t_linesw->l_close)(tp, flag);
    551 	ttyclose(tp);
    552 
    553 	/* Is this the last close? XXXFreza: hum? */
    554 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    555 	}
    556 
    557 	return (0);
    558 }
    559 
    560 static void
    561 xlcom_stop(struct tty *tp, int flag)
    562 {
    563 	struct xlcom_softc 	*sc;
    564 	int 			s;
    565 
    566 	sc = device_lookup_private(&xlcom_cd, UNIT(tp->t_dev));
    567 	if (sc == NULL)
    568 		return ;
    569 
    570 	s = splserial();
    571 	if (tp->t_state & TS_BUSY) {
    572 		/* XXXFreza: make sure we stop xmitting at next chunk */
    573 
    574 		if (! (tp->t_state & TS_TTSTOP))
    575 			tp->t_state |= TS_FLUSH;
    576 	}
    577 	splx(s);
    578 }
    579 
    580 /*
    581  * Terminal line.
    582  */
    583 static int
    584 xlcom_param(struct tty *tp, struct termios *t)
    585 {
    586 	t->c_cflag &= ~HUPCL;
    587 
    588 	if (tp->t_ospeed == t->c_ospeed &&
    589 	    tp->t_ispeed == t->c_ispeed &&
    590 	    tp->t_cflag  == t->c_cflag)
    591 	    	return (0);
    592 
    593 	return (EINVAL);
    594 }
    595 
    596 static void
    597 xlcom_start(struct tty *tp)
    598 {
    599 	struct xlcom_softc 	*sc;
    600 	int 			s1, s2;
    601 
    602 	sc = device_lookup_private(&xlcom_cd, UNIT(tp->t_dev));
    603 	if (sc == NULL)
    604 		return ;
    605 
    606 	s1 = spltty();
    607 
    608 	if (tp->t_state & (TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    609 		splx(s1);
    610 		return ;
    611 	}
    612 
    613 	if (!ttypull(tp)) {
    614 		splx(s1);
    615 		return;
    616 	}
    617 
    618 	tp->t_state |= TS_BUSY;
    619 	splx(s1);
    620 
    621 	s2 = splserial();
    622 	sc->sc_tba = tp->t_outq.c_cf;
    623 	sc->sc_tbc = ndqb(&tp->t_outq, 0);
    624 	xlcom_send_chunk(sc);
    625 	splx(s2);
    626 }
    627 
    628 static void
    629 xlcom_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
    630 {
    631 	/* Wait while the fifo drains. */
    632 	while (! (bus_space_read_4(iot, ioh, XLCOM_STAT) & STAT_TX_EMPTY))
    633 		;
    634 
    635 	bus_space_write_4(iot, ioh, XLCOM_CNTL, CNTL_RX_CLEAR | CNTL_TX_CLEAR);
    636 }
    637 
    638 static int
    639 xlcom_busy_getc(bus_space_tag_t t, bus_space_handle_t h)
    640 {
    641 	while (! (bus_space_read_4(t, h, XLCOM_STAT) & STAT_RX_DATA))
    642 		;
    643 
    644 	return (bus_space_read_4(t, h, XLCOM_RX_FIFO));
    645 }
    646 
    647 static void
    648 xlcom_busy_putc(bus_space_tag_t t, bus_space_handle_t h, int c)
    649 {
    650 	while (bus_space_read_4(t, h, XLCOM_STAT) & STAT_TX_FULL)
    651 		;
    652 
    653 	bus_space_write_4(t, h, XLCOM_TX_FIFO, c);
    654 }
    655 
    656 /*
    657  * Console on UartLite.
    658  */
    659 void
    660 nullcnprobe(struct consdev *cn)
    661 {
    662 }
    663 
    664 void
    665 xlcom_cninit(struct consdev *cn)
    666 {
    667 	if (bus_space_map(consdev_iot, CONS_ADDR, XLCOM_SIZE, 0, &consdev_ioh))
    668 		panic("xlcom_cninit: could not map consdev_ioh");
    669 
    670 	xlcom_reset(consdev_iot, consdev_ioh);
    671 }
    672 
    673 static int
    674 xlcom_cngetc(dev_t dev)
    675 {
    676 	return (xlcom_busy_getc(consdev_iot, consdev_ioh));
    677 }
    678 
    679 static void
    680 xlcom_cnputc(dev_t dev, int c)
    681 {
    682 	xlcom_busy_putc(consdev_iot, consdev_ioh, c);
    683 }
    684 
    685 /*
    686  * Remote GDB (aka "kgdb") interface.
    687  */
    688 #if defined(KGDB)
    689 
    690 static int
    691 xlcom_kgdb_getc(void *arg)
    692 {
    693 	return (xlcom_busy_getc(kgdb_iot, kgdb_ioh));
    694 }
    695 
    696 static void
    697 xlcom_kgdb_putc(void *arg, int c)
    698 {
    699 	xlcom_busy_putc(kgdb_iot, kgdb_ioh, c);
    700 }
    701 
    702 void
    703 xlcom_kgdbinit(void)
    704 {
    705 	if (bus_space_map(kgdb_iot, KGDB_ADDR, XLCOM_SIZE, 0, &kgdb_ioh))
    706 		panic("xlcom_kgdbinit: could not map kgdb_ioh");
    707 
    708 	xlcom_reset(kgdb_iot, kgdb_ioh);
    709 
    710 	kgdb_attach(xlcom_kgdb_getc, xlcom_kgdb_putc, NULL);
    711 	kgdb_dev = 123; /* arbitrary strictly positive value */
    712 }
    713 
    714 #endif /* KGDB */
    715