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