2 * Copyright (c) 2012 NetApp, Inc.
3 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <dev/ic/ns16550.h>
47 #include "uart_emul.h"
49 #define COM1_BASE 0x3F8
51 #define COM2_BASE 0x2F8
54 #define DEFAULT_RCLK 1843200
55 #define DEFAULT_BAUD 9600
57 #define FCR_RX_MASK 0xC0
62 #define MSR_DELTA_MASK 0x0f
65 #define REG_SCR com_scr
70 static bool uart_stdio; /* stdio in use for i/o */
71 static struct termios tio_stdio_orig;
78 { COM1_BASE, COM1_IRQ, false},
79 { COM2_BASE, COM2_IRQ, false},
82 #define UART_NLDEVS (sizeof(uart_lres) / sizeof(uart_lres[0]))
86 int rindex; /* index to read from */
87 int windex; /* index to write to */
88 int num; /* number of characters in the fifo */
89 int size; /* size of the fifo */
94 int fd; /* tty device file descriptor */
95 struct termios tio_orig, tio_new; /* I/O Terminals */
99 pthread_mutex_t mtx; /* protects all softc elements */
100 uint8_t data; /* Data register (R/W) */
101 uint8_t ier; /* Interrupt enable register (R/W) */
102 uint8_t lcr; /* Line control register (R/W) */
103 uint8_t mcr; /* Modem control register (R/W) */
104 uint8_t lsr; /* Line status register (R/W) */
105 uint8_t msr; /* Modem status register (R/W) */
106 uint8_t fcr; /* FIFO control register (W) */
107 uint8_t scr; /* Scratch register (R/W) */
109 uint8_t dll; /* Baudrate divisor latch LSB */
110 uint8_t dlh; /* Baudrate divisor latch MSB */
116 bool thre_int_pending; /* THRE interrupt pending */
119 uart_intr_func_t intr_assert;
120 uart_intr_func_t intr_deassert;
123 static void uart_drain(int fd, enum ev_type ev, void *arg);
129 tcsetattr(STDIN_FILENO, TCSANOW, &tio_stdio_orig);
133 ttyopen(struct ttyfd *tf)
136 tcgetattr(tf->fd, &tf->tio_orig);
138 tf->tio_new = tf->tio_orig;
139 cfmakeraw(&tf->tio_new);
140 tf->tio_new.c_cflag |= CLOCAL;
141 tcsetattr(tf->fd, TCSANOW, &tf->tio_new);
143 if (tf->fd == STDIN_FILENO) {
144 tio_stdio_orig = tf->tio_orig;
150 ttyread(struct ttyfd *tf)
154 if (read(tf->fd, &rb, 1) == 1)
161 ttywrite(struct ttyfd *tf, unsigned char wb)
164 (void)write(tf->fd, &wb, 1);
168 rxfifo_reset(struct uart_softc *sc, int size)
176 bzero(fifo, sizeof(struct fifo));
179 if (sc->tty.opened) {
181 * Flush any unread input from the tty buffer.
184 nread = read(sc->tty.fd, flushbuf, sizeof(flushbuf));
185 if (nread != sizeof(flushbuf))
190 * Enable mevent to trigger when new characters are available
193 error = mevent_enable(sc->mev);
199 rxfifo_available(struct uart_softc *sc)
204 return (fifo->num < fifo->size);
208 rxfifo_putchar(struct uart_softc *sc, uint8_t ch)
215 if (fifo->num < fifo->size) {
216 fifo->buf[fifo->windex] = ch;
217 fifo->windex = (fifo->windex + 1) % fifo->size;
219 if (!rxfifo_available(sc)) {
220 if (sc->tty.opened) {
222 * Disable mevent callback if the FIFO is full.
224 error = mevent_disable(sc->mev);
234 rxfifo_getchar(struct uart_softc *sc)
237 int c, error, wasfull;
242 if (!rxfifo_available(sc))
244 c = fifo->buf[fifo->rindex];
245 fifo->rindex = (fifo->rindex + 1) % fifo->size;
248 if (sc->tty.opened) {
249 error = mevent_enable(sc->mev);
259 rxfifo_numchars(struct uart_softc *sc)
261 struct fifo *fifo = &sc->rxfifo;
267 uart_opentty(struct uart_softc *sc)
271 sc->mev = mevent_add(sc->tty.fd, EVF_READ, uart_drain, sc);
272 assert(sc->mev != NULL);
276 modem_status(uint8_t mcr)
280 if (mcr & MCR_LOOPBACK) {
282 * In the loopback mode certain bits from the MCR are
283 * reflected back into MSR.
296 * Always assert DCD and DSR so tty open doesn't block
297 * even if CLOCAL is turned off.
299 msr = MSR_DCD | MSR_DSR;
301 assert((msr & MSR_DELTA_MASK) == 0);
307 * The IIR returns a prioritized interrupt reason:
308 * - receive data available
309 * - transmit holding register empty
310 * - modem status change
312 * Return an interrupt reason if one is available.
315 uart_intr_reason(struct uart_softc *sc)
318 if ((sc->lsr & LSR_OE) != 0 && (sc->ier & IER_ERLS) != 0)
320 else if (rxfifo_numchars(sc) > 0 && (sc->ier & IER_ERXRDY) != 0)
322 else if (sc->thre_int_pending && (sc->ier & IER_ETXRDY) != 0)
324 else if ((sc->msr & MSR_DELTA_MASK) != 0 && (sc->ier & IER_EMSC) != 0)
331 uart_reset(struct uart_softc *sc)
335 divisor = DEFAULT_RCLK / DEFAULT_BAUD / 16;
337 sc->dlh = divisor >> 16;
338 sc->msr = modem_status(sc->mcr);
340 rxfifo_reset(sc, 1); /* no fifo until enabled by software */
344 * Toggle the COM port's intr pin depending on whether or not we have an
345 * interrupt condition to report to the processor.
348 uart_toggle_intr(struct uart_softc *sc)
352 intr_reason = uart_intr_reason(sc);
354 if (intr_reason == IIR_NOPEND)
355 (*sc->intr_deassert)(sc->arg);
357 (*sc->intr_assert)(sc->arg);
361 uart_drain(int fd, enum ev_type ev, void *arg)
363 struct uart_softc *sc;
368 assert(fd == sc->tty.fd);
369 assert(ev == EVF_READ);
372 * This routine is called in the context of the mevent thread
373 * to take out the softc lock to protect against concurrent
374 * access from a vCPU i/o exit
376 pthread_mutex_lock(&sc->mtx);
378 if ((sc->mcr & MCR_LOOPBACK) != 0) {
379 (void) ttyread(&sc->tty);
381 while (rxfifo_available(sc) &&
382 ((ch = ttyread(&sc->tty)) != -1)) {
383 rxfifo_putchar(sc, ch);
385 uart_toggle_intr(sc);
388 pthread_mutex_unlock(&sc->mtx);
392 uart_write(struct uart_softc *sc, int offset, uint8_t value)
397 pthread_mutex_lock(&sc->mtx);
400 * Take care of the special case DLAB accesses first
402 if ((sc->lcr & LCR_DLAB) != 0) {
403 if (offset == REG_DLL) {
408 if (offset == REG_DLH) {
416 if (sc->mcr & MCR_LOOPBACK) {
417 if (rxfifo_putchar(sc, value) != 0)
419 } else if (sc->tty.opened) {
420 ttywrite(&sc->tty, value);
421 } /* else drop on floor */
422 sc->thre_int_pending = true;
426 * Apply mask so that bits 4-7 are 0
427 * Also enables bits 0-3 only if they're 1
429 sc->ier = value & 0x0F;
433 * When moving from FIFO and 16450 mode and vice versa,
434 * the FIFO contents are reset.
436 if ((sc->fcr & FCR_ENABLE) ^ (value & FCR_ENABLE)) {
437 fifosz = (value & FCR_ENABLE) ? FIFOSZ : 1;
438 rxfifo_reset(sc, fifosz);
442 * The FCR_ENABLE bit must be '1' for the programming
443 * of other FCR bits to be effective.
445 if ((value & FCR_ENABLE) == 0) {
448 if ((value & FCR_RCV_RST) != 0)
449 rxfifo_reset(sc, FIFOSZ);
452 (FCR_ENABLE | FCR_DMA | FCR_RX_MASK);
459 /* Apply mask so that bits 5-7 are 0 */
460 sc->mcr = value & 0x1F;
461 msr = modem_status(sc->mcr);
464 * Detect if there has been any change between the
465 * previous and the new value of MSR. If there is
466 * then assert the appropriate MSR delta bit.
468 if ((msr & MSR_CTS) ^ (sc->msr & MSR_CTS))
470 if ((msr & MSR_DSR) ^ (sc->msr & MSR_DSR))
472 if ((msr & MSR_DCD) ^ (sc->msr & MSR_DCD))
474 if ((sc->msr & MSR_RI) != 0 && (msr & MSR_RI) == 0)
478 * Update the value of MSR while retaining the delta
481 sc->msr &= MSR_DELTA_MASK;
486 * Line status register is not meant to be written to
487 * during normal operation.
492 * As far as I can tell MSR is a read-only register.
503 uart_toggle_intr(sc);
504 pthread_mutex_unlock(&sc->mtx);
508 uart_read(struct uart_softc *sc, int offset)
510 uint8_t iir, intr_reason, reg;
512 pthread_mutex_lock(&sc->mtx);
515 * Take care of the special case DLAB accesses first
517 if ((sc->lcr & LCR_DLAB) != 0) {
518 if (offset == REG_DLL) {
523 if (offset == REG_DLH) {
531 reg = rxfifo_getchar(sc);
537 iir = (sc->fcr & FCR_ENABLE) ? IIR_FIFO_MASK : 0;
539 intr_reason = uart_intr_reason(sc);
542 * Deal with side effects of reading the IIR register
544 if (intr_reason == IIR_TXRDY)
545 sc->thre_int_pending = false;
558 /* Transmitter is always ready for more data */
559 sc->lsr |= LSR_TEMT | LSR_THRE;
561 /* Check for new receive data */
562 if (rxfifo_numchars(sc) > 0)
563 sc->lsr |= LSR_RXRDY;
565 sc->lsr &= ~LSR_RXRDY;
569 /* The LSR_OE bit is cleared on LSR read */
574 * MSR delta bits are cleared on read
577 sc->msr &= ~MSR_DELTA_MASK;
588 uart_toggle_intr(sc);
589 pthread_mutex_unlock(&sc->mtx);
595 uart_legacy_alloc(int which, int *baseaddr, int *irq)
598 if (which < 0 || which >= UART_NLDEVS || uart_lres[which].inuse)
601 uart_lres[which].inuse = true;
602 *baseaddr = uart_lres[which].baseaddr;
603 *irq = uart_lres[which].irq;
609 uart_init(uart_intr_func_t intr_assert, uart_intr_func_t intr_deassert,
612 struct uart_softc *sc;
614 sc = calloc(1, sizeof(struct uart_softc));
617 sc->intr_assert = intr_assert;
618 sc->intr_deassert = intr_deassert;
620 pthread_mutex_init(&sc->mtx, NULL);
628 uart_tty_backend(struct uart_softc *sc, const char *opts)
635 fd = open(opts, O_RDWR | O_NONBLOCK);
636 if (fd > 0 && isatty(fd)) {
638 sc->tty.opened = true;
646 uart_set_backend(struct uart_softc *sc, const char *opts)
655 if (strcmp("stdio", opts) == 0) {
657 sc->tty.fd = STDIN_FILENO;
658 sc->tty.opened = true;
662 } else if (uart_tty_backend(sc, opts) == 0) {
666 /* Make the backend file descriptor non-blocking */
668 retval = fcntl(sc->tty.fd, F_SETFL, O_NONBLOCK);