Home | History | Annotate | Line # | Download | only in dev
xlcom.c revision 1.5
      1 /* 	$NetBSD: xlcom.c,v 1.5 2007/12/02 20:46:53 ad 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.5 2007/12/02 20:46:53 ad 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 	struct device 		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 	xlcom_open, xlcom_close, xlcom_read, xlcom_write, xlcom_ioctl,
    142 	xlcom_stop, xlcom_tty, xlcom_poll, nommap, ttykqfilter, D_TTY
    143 };
    144 
    145 extern struct cfdriver xlcom_cd;
    146 
    147 /* Terminal line. */
    148 static int 	xlcom_param(struct tty *, struct termios *);
    149 static void 	xlcom_start(struct tty *);
    150 
    151 /* Generic device. */
    152 static void 	xlcom_attach(struct device *, struct device *, void *);
    153 
    154 CFATTACH_DECL(xlcom, sizeof(struct xlcom_softc),
    155     xcvbus_child_match, xlcom_attach, NULL, NULL);
    156 
    157 
    158 static void
    159 xlcom_attach(struct device *parent, struct device *self, void *aux)
    160 {
    161 	struct xcvbus_attach_args 	*vaa = aux;
    162 	struct xlcom_softc 		*sc = (struct xlcom_softc *)self;
    163 	struct tty 			*tp;
    164 	dev_t 				dev;
    165 
    166 	printf(": UartLite serial port\n");
    167 
    168 #if defined(KGDB)
    169 	/* We don't want to share kgdb port with the user. */
    170 	if (sc->sc_iot == kgdb_iot && sc->sc_ioh == kgdb_ioh) {
    171 		printf("%s: already in use by kgdb\n", device_xname(self));
    172 		return;
    173 	}
    174 #endif /* KGDB */
    175 
    176 	if ((sc->sc_ih = intr_establish(vaa->vaa_intr, IST_LEVEL, IPL_SERIAL,
    177 	    xlcom_intr, sc)) == NULL) {
    178 		printf("%s: could not establish interrupt\n",
    179 		    device_xname(self));
    180 		return ;
    181 	}
    182 
    183 	dev = makedev(cdevsw_lookup_major(&xlcom_cdevsw), device_unit(self));
    184 	if (cn_tab == &consdev_xlcom) {
    185 		cn_init_magic(&xlcom_cnm_state);
    186 		cn_set_magic("\x23\x2e"); 		/* #. */
    187 		cn_tab->cn_dev = dev;
    188 
    189 		sc->sc_iot = consdev_iot;
    190 		sc->sc_ioh = consdev_ioh;
    191 
    192 		printf("%s: console\n", sc->sc_dev.dv_xname);
    193 	} else {
    194 		sc->sc_iot = vaa->vaa_iot;
    195 
    196 		if (bus_space_map(vaa->vaa_iot, vaa->vaa_addr, XLCOM_SIZE, 0,
    197 		    &sc->sc_ioh) != 0) {
    198 			printf("%s: could not map registers\n",
    199 			    device_xname(self));
    200 			return ;
    201 		}
    202 
    203 		/* Reset FIFOs. */
    204 		xlcom_reset(sc->sc_iot, sc->sc_ioh);
    205 	}
    206 
    207 	sc->sc_tbc = 0;
    208 	sc->sc_tba = NULL;
    209 
    210 	sc->sc_rput = sc->sc_rget = 0;
    211 	sc->sc_ravail = XLCOM_RXBUF_SIZE;
    212 
    213 	sc->sc_rx_soft = softint_establish(SOFTINT_SERIAL, xlcom_rx_soft, sc);
    214 	sc->sc_tx_soft = softint_establish(SOFTINT_SERIAL, xlcom_tx_soft, sc);
    215 
    216 	if (sc->sc_rx_soft == NULL || sc->sc_tx_soft == NULL) {
    217 		printf("%s: could not establish Rx or Tx softintr\n",
    218 		    sc->sc_dev.dv_xname);
    219 		return ;
    220 	}
    221 
    222 	tp = ttymalloc();
    223 	tp->t_dev = dev;
    224 	tp->t_oproc = xlcom_start;
    225 	tp->t_param = xlcom_param;
    226 	tp->t_hwiflow = NULL; 				/* No HW flow control */
    227 	tty_attach(tp);
    228 
    229 	/* XXX anything else to do for console early? */
    230 	if (cn_tab == &consdev_xlcom) {
    231 		/* Before first open, so that we can enter ddb(4). */
    232 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
    233 		    CNTL_INTR_EN);
    234 	}
    235 
    236 	sc->sc_tty = tp;
    237 }
    238 
    239 /*
    240  * Misc hooks.
    241  */
    242 static void
    243 xlcom_tx_soft(void *arg)
    244 {
    245 	struct xlcom_softc 	*sc = (struct xlcom_softc *)arg;
    246 	struct tty 		*tp = sc->sc_tty;
    247 
    248 	if (tp->t_state & TS_FLUSH)
    249 		tp->t_state &= ~TS_FLUSH;
    250 	else
    251 		ndflush(&tp->t_outq,
    252 		    (int)(sc->sc_tba - tp->t_outq.c_cf));
    253 	(tp->t_linesw->l_start)(tp);
    254 }
    255 
    256 static void
    257 xlcom_rx_soft(void *arg)
    258 {
    259 	struct xlcom_softc 	*sc = (struct xlcom_softc *)arg;
    260 	struct tty 		*tp = sc->sc_tty;
    261 	int 			(*rint)(int, struct tty *);
    262 	u_short 		c;
    263 	int 			d;
    264 
    265 	/*
    266 	 * XXX: we don't do any synchronization, rput may change below
    267 	 * XXX: our hands -- it doesn't seem to be troublesome as long
    268 	 * XXX: as "sc->sc_rget = sc->sc_rput" is atomic.
    269 	 */
    270 	rint = tp->t_linesw->l_rint;
    271 
    272 	/* Run until we catch our tail. */
    273 	while (sc->sc_rput != sc->sc_rget) {
    274 		c = sc->sc_rbuf[sc->sc_rget];
    275 
    276 		next(sc->sc_rget);
    277 		sc->sc_ravail++;
    278 
    279 		d = (c & 0xff) |
    280 		    ((c & XLCOM_CHAR_PE) != 0 ? TTY_PE : 0) |
    281 		    ((c & XLCOM_CHAR_FE) != 0 ? TTY_FE : 0);
    282 
    283 		/*
    284 		 * Drop the rest of data if discipline runs out of buffer
    285 		 * space. We'd use flow control here, if we had any.
    286 		 */
    287 		if ((rint)(d, tp) == -1) {
    288 			sc->sc_rget = sc->sc_rput;
    289 			return ;
    290 		}
    291 	}
    292 }
    293 
    294 static void
    295 xlcom_send_chunk(struct xlcom_softc *sc)
    296 {
    297 	uint32_t 		stat;
    298 
    299 	/* Chunk flushed, no more data available. */
    300 	if (sc->sc_tbc <= 0) {
    301 		return ;
    302 	}
    303 
    304 	/* Run as long as we have space and data. */
    305 	while (sc->sc_tbc > 0) {
    306 		stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    307 		if (stat & STAT_TX_FULL)
    308 			break;
    309 
    310 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_TX_FIFO,
    311 		    *sc->sc_tba);
    312 
    313 		sc->sc_tbc--;
    314 		sc->sc_tba++;
    315 	}
    316 
    317 	/* Try to grab more data while FIFO drains. */
    318 	if (sc->sc_tbc == 0) {
    319 		sc->sc_tty->t_state &= ~TS_BUSY;
    320 		softint_schedule(sc->sc_tx_soft);
    321 	}
    322 }
    323 
    324 static void
    325 xlcom_recv_chunk(struct xlcom_softc *sc)
    326 {
    327 	uint32_t 		stat;
    328 	u_short 		c;
    329 	u_int 			n;
    330 
    331 	n = sc->sc_ravail;
    332 	stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    333 
    334 	/* Run as long as we have data and space. */
    335 	while ((stat & STAT_RX_DATA) != 0 && sc->sc_ravail > 0) {
    336 		c = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_RX_FIFO);
    337 
    338 		cn_check_magic(sc->sc_tty->t_dev, c, xlcom_cnm_state);
    339 
    340 		/* XXX: Should we pass rx-overrun upstream too? */
    341 		c |= ((stat & STAT_PARITY_ERR) != 0 ? XLCOM_CHAR_PE : 0) |
    342 		     ((stat & STAT_FRAME_ERR) != 0 ? XLCOM_CHAR_FE : 0);
    343 		sc->sc_rbuf[sc->sc_rput] = c;
    344 		sc->sc_ravail--;
    345 
    346 		next(sc->sc_rput);
    347 		stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    348 	}
    349 
    350 	/* Shedule completion hook if we received any. */
    351 	if (n != sc->sc_ravail)
    352 		softint_schedule(sc->sc_rx_soft);
    353 }
    354 
    355 static int
    356 xlcom_intr(void *arg)
    357 {
    358 	struct xlcom_softc 	*sc = arg;
    359 	uint32_t 		stat;
    360 
    361 	/*
    362 	 * Xilinx DS422, "OPB UART Lite v1.00b"
    363 	 *
    364 	 * If interrupts are enabled, an interrupt is generated when one
    365 	 * of the following conditions is true:
    366 	 */
    367 	stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
    368 
    369 	/*
    370 	 * 1. When there exists any valid character in the receive FIFO,
    371 	 *    the interrupt stays active until the receive FIFO is empty.
    372 	 *    This is a level interrupt.
    373 	 */
    374 	if (stat & STAT_RX_DATA)
    375 		xlcom_recv_chunk(sc);
    376 
    377 	/*
    378 	 * 2. When the transmit FIFO goes from not empty to empty, such
    379 	 *    as when the last character in the transmit FIFO is transmitted,
    380 	 *    the interrupt is only active one clock cycle. This is an
    381 	 *    edge interrupt.
    382 	 */
    383 	if (stat & STAT_TX_EMPTY)
    384 		xlcom_send_chunk(sc);
    385 
    386 	return (0);
    387 }
    388 
    389 /*
    390  * Character device.
    391  */
    392 static int
    393 xlcom_open(dev_t dev, int flags, int mode, struct lwp *l)
    394 {
    395 	struct xlcom_softc 	*sc;
    396 	struct tty 		*tp;
    397 	struct proc 		*p;
    398 	int 			error, s;
    399 
    400 	sc = device_lookup(&xlcom_cd, minor(dev));
    401 	if (sc == NULL)
    402 		return (ENXIO);
    403 
    404 	tp = sc->sc_tty;
    405 	p = l->l_proc;
    406 
    407 	s = spltty(); 							/* { */
    408 
    409 	if ((tp->t_state & TS_ISOPEN) && (tp->t_state & TS_XCLUDE) &&
    410 	    kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    411 	    &p->p_acflag) != 0) {
    412 	    	error = EBUSY;
    413 	    	goto fail;
    414 	}
    415 
    416 	/* Is this the first open? */
    417 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    418 		tp->t_dev = dev;
    419 
    420 		/* Values hardwired at synthesis time. XXXFreza: xparam.h */
    421 		tp->t_ispeed = tp->t_ospeed = B38400;
    422 		tp->t_cflag = CLOCAL | CREAD | CS8;
    423 		tp->t_iflag = TTYDEF_IFLAG;
    424 		tp->t_oflag = TTYDEF_OFLAG;
    425 		tp->t_lflag = TTYDEF_LFLAG;
    426 
    427 		ttychars(tp);
    428 		ttsetwater(tp);
    429 
    430 		/* Enable interrupt. */
    431 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
    432 		    CNTL_INTR_EN);
    433 	}
    434 
    435 	error = ttyopen(tp, DIALOUT(dev), (flags & O_NONBLOCK));
    436 	if (error)
    437 		goto fail;
    438 
    439 	error = (tp->t_linesw->l_open)(dev, tp);
    440 	if (error)
    441 		goto fail;
    442 
    443 	splx(s); 							/* } */
    444 	return (0);
    445 
    446  fail:
    447 	/* XXXFreza: Shutdown if nobody else has the device open. */
    448 	splx(s);
    449 	return (error);
    450 }
    451 
    452 static int
    453 xlcom_read(dev_t dev, struct uio *uio, int flag)
    454 {
    455 	struct xlcom_softc 	*sc;
    456 	struct tty 		*tp;
    457 
    458 	sc = device_lookup(&xlcom_cd, minor(dev));
    459 	if (sc == NULL)
    460 		return (ENXIO);
    461 	tp = sc->sc_tty;
    462 
    463 	return (tp->t_linesw->l_read)(tp, uio, flag);
    464 }
    465 
    466 static int
    467 xlcom_write(dev_t dev, struct uio *uio, int flag)
    468 {
    469 	struct xlcom_softc 	*sc;
    470 	struct tty 		*tp;
    471 
    472 	sc = device_lookup(&xlcom_cd, minor(dev));
    473 	if (sc == NULL)
    474 		return (ENXIO);
    475 	tp = sc->sc_tty;
    476 
    477 	return (tp->t_linesw->l_write)(tp, uio, flag);
    478 }
    479 
    480 static int
    481 xlcom_poll(dev_t dev, int events, struct lwp *l)
    482 {
    483 	struct xlcom_softc 	*sc;
    484 	struct tty 		*tp;
    485 
    486 	sc = device_lookup(&xlcom_cd, minor(dev));
    487 	if (sc == NULL)
    488 		return (ENXIO);
    489 	tp = sc->sc_tty;
    490 
    491 	return (tp->t_linesw->l_poll)(tp, events, l);
    492 }
    493 
    494 static struct tty *
    495 xlcom_tty(dev_t dev)
    496 {
    497 	struct xlcom_softc 	*sc;
    498 	struct tty 		*tp;
    499 
    500 	sc = device_lookup(&xlcom_cd, minor(dev));
    501 	if (sc == NULL)
    502 		return (NULL);
    503 	tp = sc->sc_tty;
    504 
    505 	return (tp);
    506 }
    507 
    508 static int
    509 xlcom_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    510 {
    511 	struct xlcom_softc 	*sc;
    512 	struct tty 		*tp;
    513 	int 			error;
    514 
    515 	sc = device_lookup(&xlcom_cd, minor(dev));
    516 	if (sc == NULL)
    517 		return (ENXIO);
    518 	tp = sc->sc_tty;
    519 
    520 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    521 	if (error != EPASSTHROUGH)
    522 		return (error);
    523 
    524 	error = ttioctl(tp, cmd, data, flag, l);
    525 	if (error != EPASSTHROUGH)
    526 		return (error);
    527 
    528 	/* XXXFreza: error = 0; switch (cmd); return error; */
    529 
    530 	return (error);
    531 }
    532 
    533 static int
    534 xlcom_close(dev_t dev, int flag, int mode, struct lwp *l)
    535 {
    536 	struct xlcom_softc 	*sc;
    537 	struct tty 		*tp;
    538 
    539 	sc = device_lookup(&xlcom_cd, minor(dev));
    540 	if (sc == NULL)
    541 		return (ENXIO);
    542 	tp = sc->sc_tty;
    543 
    544 	(tp->t_linesw->l_close)(tp, flag);
    545 	ttyclose(tp);
    546 
    547 	/* Is this the last close? XXXFreza: hum? */
    548 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    549 	}
    550 
    551 	return (0);
    552 }
    553 
    554 static void
    555 xlcom_stop(struct tty *tp, int flag)
    556 {
    557 	struct xlcom_softc 	*sc;
    558 	int 			s;
    559 
    560 	sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
    561 	if (sc == NULL)
    562 		return ;
    563 
    564 	s = splserial();
    565 	if (tp->t_state & TS_BUSY) {
    566 		/* XXXFreza: make sure we stop xmitting at next chunk */
    567 
    568 		if (! (tp->t_state & TS_TTSTOP))
    569 			tp->t_state |= TS_FLUSH;
    570 	}
    571 	splx(s);
    572 }
    573 
    574 /*
    575  * Terminal line.
    576  */
    577 static int
    578 xlcom_param(struct tty *tp, struct termios *t)
    579 {
    580 	t->c_cflag &= ~HUPCL;
    581 
    582 	if (tp->t_ospeed == t->c_ospeed &&
    583 	    tp->t_ispeed == t->c_ispeed &&
    584 	    tp->t_cflag  == t->c_cflag)
    585 	    	return (0);
    586 
    587 	return (EINVAL);
    588 }
    589 
    590 static void
    591 xlcom_start(struct tty *tp)
    592 {
    593 	struct xlcom_softc 	*sc;
    594 	int 			s1, s2;
    595 
    596 	sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
    597 	if (sc == NULL)
    598 		return ;
    599 
    600 	s1 = spltty();
    601 
    602 	if (tp->t_state & (TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    603 		splx(s1);
    604 		return ;
    605 	}
    606 
    607 	if (!ttypull(tp)) {
    608 		splx(s1);
    609 		return;
    610 	}
    611 
    612 	tp->t_state |= TS_BUSY;
    613 	splx(s1);
    614 
    615 	s2 = splserial();
    616 	sc->sc_tba = tp->t_outq.c_cf;
    617 	sc->sc_tbc = ndqb(&tp->t_outq, 0);
    618 	xlcom_send_chunk(sc);
    619 	splx(s2);
    620 }
    621 
    622 static void
    623 xlcom_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
    624 {
    625 	/* Wait while the fifo drains. */
    626 	while (! (bus_space_read_4(iot, ioh, XLCOM_STAT) & STAT_TX_EMPTY))
    627 		;
    628 
    629 	bus_space_write_4(iot, ioh, XLCOM_CNTL, CNTL_RX_CLEAR | CNTL_TX_CLEAR);
    630 }
    631 
    632 static int
    633 xlcom_busy_getc(bus_space_tag_t t, bus_space_handle_t h)
    634 {
    635 	while (! (bus_space_read_4(t, h, XLCOM_STAT) & STAT_RX_DATA))
    636 		;
    637 
    638 	return (bus_space_read_4(t, h, XLCOM_RX_FIFO));
    639 }
    640 
    641 static void
    642 xlcom_busy_putc(bus_space_tag_t t, bus_space_handle_t h, int c)
    643 {
    644 	while (bus_space_read_4(t, h, XLCOM_STAT) & STAT_TX_FULL)
    645 		;
    646 
    647 	bus_space_write_4(t, h, XLCOM_TX_FIFO, c);
    648 }
    649 
    650 /*
    651  * Console on UartLite.
    652  */
    653 void
    654 nullcnprobe(struct consdev *cn)
    655 {
    656 }
    657 
    658 void
    659 xlcom_cninit(struct consdev *cn)
    660 {
    661 	if (bus_space_map(consdev_iot, CONS_ADDR, XLCOM_SIZE, 0, &consdev_ioh))
    662 		panic("xlcom_cninit: could not map consdev_ioh");
    663 
    664 	xlcom_reset(consdev_iot, consdev_ioh);
    665 }
    666 
    667 static int
    668 xlcom_cngetc(dev_t dev)
    669 {
    670 	return (xlcom_busy_getc(consdev_iot, consdev_ioh));
    671 }
    672 
    673 static void
    674 xlcom_cnputc(dev_t dev, int c)
    675 {
    676 	xlcom_busy_putc(consdev_iot, consdev_ioh, c);
    677 }
    678 
    679 /*
    680  * Remote GDB (aka "kgdb") interface.
    681  */
    682 #if defined(KGDB)
    683 
    684 static int
    685 xlcom_kgdb_getc(void *arg)
    686 {
    687 	return (xlcom_busy_getc(kgdb_iot, kgdb_ioh));
    688 }
    689 
    690 static void
    691 xlcom_kgdb_putc(void *arg, int c)
    692 {
    693 	xlcom_busy_putc(kgdb_iot, kgdb_ioh, c);
    694 }
    695 
    696 void
    697 xlcom_kgdbinit(void)
    698 {
    699 	if (bus_space_map(kgdb_iot, KGDB_ADDR, XLCOM_SIZE, 0, &kgdb_ioh))
    700 		panic("xlcom_kgdbinit: could not map kgdb_ioh");
    701 
    702 	xlcom_reset(kgdb_iot, kgdb_ioh);
    703 
    704 	kgdb_attach(xlcom_kgdb_getc, xlcom_kgdb_putc, NULL);
    705 	kgdb_dev = 123; /* arbitrary strictly positive value */
    706 }
    707 
    708 #endif /* KGDB */
    709