Home | History | Annotate | Line # | Download | only in dev
xlcom.c revision 1.8
      1 /* 	$NetBSD: xlcom.c,v 1.8 2011/04/24 16:26:55 rmind 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.8 2011/04/24 16:26:55 rmind 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 = tty_alloc();
    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 	int 			error, s;
    398 
    399 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    400 	if (sc == NULL)
    401 		return (ENXIO);
    402 
    403 	tp = sc->sc_tty;
    404 
    405 	s = spltty(); 							/* { */
    406 
    407 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
    408 	    tp) != 0) {
    409 	    	error = EBUSY;
    410 	    	goto fail;
    411 	}
    412 
    413 	/* Is this the first open? */
    414 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    415 		tp->t_dev = dev;
    416 
    417 		/* Values hardwired at synthesis time. XXXFreza: xparam.h */
    418 		tp->t_ispeed = tp->t_ospeed = B38400;
    419 		tp->t_cflag = CLOCAL | CREAD | CS8;
    420 		tp->t_iflag = TTYDEF_IFLAG;
    421 		tp->t_oflag = TTYDEF_OFLAG;
    422 		tp->t_lflag = TTYDEF_LFLAG;
    423 
    424 		ttychars(tp);
    425 		ttsetwater(tp);
    426 
    427 		/* Enable interrupt. */
    428 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
    429 		    CNTL_INTR_EN);
    430 	}
    431 
    432 	error = ttyopen(tp, DIALOUT(dev), (flags & O_NONBLOCK));
    433 	if (error)
    434 		goto fail;
    435 
    436 	error = (tp->t_linesw->l_open)(dev, tp);
    437 	if (error)
    438 		goto fail;
    439 
    440 	splx(s); 							/* } */
    441 	return (0);
    442 
    443  fail:
    444 	/* XXXFreza: Shutdown if nobody else has the device open. */
    445 	splx(s);
    446 	return (error);
    447 }
    448 
    449 static int
    450 xlcom_read(dev_t dev, struct uio *uio, int flag)
    451 {
    452 	struct xlcom_softc 	*sc;
    453 	struct tty 		*tp;
    454 
    455 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    456 	if (sc == NULL)
    457 		return (ENXIO);
    458 	tp = sc->sc_tty;
    459 
    460 	return (tp->t_linesw->l_read)(tp, uio, flag);
    461 }
    462 
    463 static int
    464 xlcom_write(dev_t dev, struct uio *uio, int flag)
    465 {
    466 	struct xlcom_softc 	*sc;
    467 	struct tty 		*tp;
    468 
    469 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    470 	if (sc == NULL)
    471 		return (ENXIO);
    472 	tp = sc->sc_tty;
    473 
    474 	return (tp->t_linesw->l_write)(tp, uio, flag);
    475 }
    476 
    477 static int
    478 xlcom_poll(dev_t dev, int events, struct lwp *l)
    479 {
    480 	struct xlcom_softc 	*sc;
    481 	struct tty 		*tp;
    482 
    483 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    484 	if (sc == NULL)
    485 		return (ENXIO);
    486 	tp = sc->sc_tty;
    487 
    488 	return (tp->t_linesw->l_poll)(tp, events, l);
    489 }
    490 
    491 static struct tty *
    492 xlcom_tty(dev_t dev)
    493 {
    494 	struct xlcom_softc 	*sc;
    495 	struct tty 		*tp;
    496 
    497 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    498 	if (sc == NULL)
    499 		return (NULL);
    500 	tp = sc->sc_tty;
    501 
    502 	return (tp);
    503 }
    504 
    505 static int
    506 xlcom_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    507 {
    508 	struct xlcom_softc 	*sc;
    509 	struct tty 		*tp;
    510 	int 			error;
    511 
    512 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    513 	if (sc == NULL)
    514 		return (ENXIO);
    515 	tp = sc->sc_tty;
    516 
    517 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    518 	if (error != EPASSTHROUGH)
    519 		return (error);
    520 
    521 	error = ttioctl(tp, cmd, data, flag, l);
    522 	if (error != EPASSTHROUGH)
    523 		return (error);
    524 
    525 	/* XXXFreza: error = 0; switch (cmd); return error; */
    526 
    527 	return (error);
    528 }
    529 
    530 static int
    531 xlcom_close(dev_t dev, int flag, int mode, struct lwp *l)
    532 {
    533 	struct xlcom_softc 	*sc;
    534 	struct tty 		*tp;
    535 
    536 	sc = device_lookup_private(&xlcom_cd, minor(dev));
    537 	if (sc == NULL)
    538 		return (ENXIO);
    539 	tp = sc->sc_tty;
    540 
    541 	(tp->t_linesw->l_close)(tp, flag);
    542 	ttyclose(tp);
    543 
    544 	/* Is this the last close? XXXFreza: hum? */
    545 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    546 	}
    547 
    548 	return (0);
    549 }
    550 
    551 static void
    552 xlcom_stop(struct tty *tp, int flag)
    553 {
    554 	struct xlcom_softc 	*sc;
    555 	int 			s;
    556 
    557 	sc = device_lookup_private(&xlcom_cd, UNIT(tp->t_dev));
    558 	if (sc == NULL)
    559 		return ;
    560 
    561 	s = splserial();
    562 	if (tp->t_state & TS_BUSY) {
    563 		/* XXXFreza: make sure we stop xmitting at next chunk */
    564 
    565 		if (! (tp->t_state & TS_TTSTOP))
    566 			tp->t_state |= TS_FLUSH;
    567 	}
    568 	splx(s);
    569 }
    570 
    571 /*
    572  * Terminal line.
    573  */
    574 static int
    575 xlcom_param(struct tty *tp, struct termios *t)
    576 {
    577 	t->c_cflag &= ~HUPCL;
    578 
    579 	if (tp->t_ospeed == t->c_ospeed &&
    580 	    tp->t_ispeed == t->c_ispeed &&
    581 	    tp->t_cflag  == t->c_cflag)
    582 	    	return (0);
    583 
    584 	return (EINVAL);
    585 }
    586 
    587 static void
    588 xlcom_start(struct tty *tp)
    589 {
    590 	struct xlcom_softc 	*sc;
    591 	int 			s1, s2;
    592 
    593 	sc = device_lookup_private(&xlcom_cd, UNIT(tp->t_dev));
    594 	if (sc == NULL)
    595 		return ;
    596 
    597 	s1 = spltty();
    598 
    599 	if (tp->t_state & (TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    600 		splx(s1);
    601 		return ;
    602 	}
    603 
    604 	if (!ttypull(tp)) {
    605 		splx(s1);
    606 		return;
    607 	}
    608 
    609 	tp->t_state |= TS_BUSY;
    610 	splx(s1);
    611 
    612 	s2 = splserial();
    613 	sc->sc_tba = tp->t_outq.c_cf;
    614 	sc->sc_tbc = ndqb(&tp->t_outq, 0);
    615 	xlcom_send_chunk(sc);
    616 	splx(s2);
    617 }
    618 
    619 static void
    620 xlcom_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
    621 {
    622 	/* Wait while the fifo drains. */
    623 	while (! (bus_space_read_4(iot, ioh, XLCOM_STAT) & STAT_TX_EMPTY))
    624 		;
    625 
    626 	bus_space_write_4(iot, ioh, XLCOM_CNTL, CNTL_RX_CLEAR | CNTL_TX_CLEAR);
    627 }
    628 
    629 static int
    630 xlcom_busy_getc(bus_space_tag_t t, bus_space_handle_t h)
    631 {
    632 	while (! (bus_space_read_4(t, h, XLCOM_STAT) & STAT_RX_DATA))
    633 		;
    634 
    635 	return (bus_space_read_4(t, h, XLCOM_RX_FIFO));
    636 }
    637 
    638 static void
    639 xlcom_busy_putc(bus_space_tag_t t, bus_space_handle_t h, int c)
    640 {
    641 	while (bus_space_read_4(t, h, XLCOM_STAT) & STAT_TX_FULL)
    642 		;
    643 
    644 	bus_space_write_4(t, h, XLCOM_TX_FIFO, c);
    645 }
    646 
    647 /*
    648  * Console on UartLite.
    649  */
    650 void
    651 nullcnprobe(struct consdev *cn)
    652 {
    653 }
    654 
    655 void
    656 xlcom_cninit(struct consdev *cn)
    657 {
    658 	if (bus_space_map(consdev_iot, CONS_ADDR, XLCOM_SIZE, 0, &consdev_ioh))
    659 		panic("xlcom_cninit: could not map consdev_ioh");
    660 
    661 	xlcom_reset(consdev_iot, consdev_ioh);
    662 }
    663 
    664 static int
    665 xlcom_cngetc(dev_t dev)
    666 {
    667 	return (xlcom_busy_getc(consdev_iot, consdev_ioh));
    668 }
    669 
    670 static void
    671 xlcom_cnputc(dev_t dev, int c)
    672 {
    673 	xlcom_busy_putc(consdev_iot, consdev_ioh, c);
    674 }
    675 
    676 /*
    677  * Remote GDB (aka "kgdb") interface.
    678  */
    679 #if defined(KGDB)
    680 
    681 static int
    682 xlcom_kgdb_getc(void *arg)
    683 {
    684 	return (xlcom_busy_getc(kgdb_iot, kgdb_ioh));
    685 }
    686 
    687 static void
    688 xlcom_kgdb_putc(void *arg, int c)
    689 {
    690 	xlcom_busy_putc(kgdb_iot, kgdb_ioh, c);
    691 }
    692 
    693 void
    694 xlcom_kgdbinit(void)
    695 {
    696 	if (bus_space_map(kgdb_iot, KGDB_ADDR, XLCOM_SIZE, 0, &kgdb_ioh))
    697 		panic("xlcom_kgdbinit: could not map kgdb_ioh");
    698 
    699 	xlcom_reset(kgdb_iot, kgdb_ioh);
    700 
    701 	kgdb_attach(xlcom_kgdb_getc, xlcom_kgdb_putc, NULL);
    702 	kgdb_dev = 123; /* arbitrary strictly positive value */
    703 }
    704 
    705 #endif /* KGDB */
    706