2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 NetApp, Inc.
5 * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <dev/ic/ns16550.h>
33 #include <machine/vmm_snapshot.h>
44 #include "uart_backend.h"
45 #include "uart_emul.h"
47 #define COM1_BASE 0x3F8
49 #define COM2_BASE 0x2F8
51 #define COM3_BASE 0x3E8
53 #define COM4_BASE 0x2E8
56 #define DEFAULT_RCLK 1843200
57 #define DEFAULT_BAUD 115200
59 #define FCR_RX_MASK 0xC0
64 #define MSR_DELTA_MASK 0x0f
67 #define REG_SCR com_scr
75 { COM1_BASE, COM1_IRQ, false},
76 { COM2_BASE, COM2_IRQ, false},
77 { COM3_BASE, COM3_IRQ, false},
78 { COM4_BASE, COM4_IRQ, false},
81 #define UART_NLDEVS (sizeof(uart_lres) / sizeof(uart_lres[0]))
83 struct uart_ns16550_softc {
84 struct uart_softc *backend;
86 pthread_mutex_t mtx; /* protects all softc elements */
87 uint8_t data; /* Data register (R/W) */
88 uint8_t ier; /* Interrupt enable register (R/W) */
89 uint8_t lcr; /* Line control register (R/W) */
90 uint8_t mcr; /* Modem control register (R/W) */
91 uint8_t lsr; /* Line status register (R/W) */
92 uint8_t msr; /* Modem status register (R/W) */
93 uint8_t fcr; /* FIFO control register (W) */
94 uint8_t scr; /* Scratch register (R/W) */
96 uint8_t dll; /* Baudrate divisor latch LSB */
97 uint8_t dlh; /* Baudrate divisor latch MSB */
99 bool thre_int_pending; /* THRE interrupt pending */
102 uart_intr_func_t intr_assert;
103 uart_intr_func_t intr_deassert;
107 modem_status(uint8_t mcr)
111 if (mcr & MCR_LOOPBACK) {
113 * In the loopback mode certain bits from the MCR are
114 * reflected back into MSR.
127 * Always assert DCD and DSR so tty open doesn't block
128 * even if CLOCAL is turned off.
130 msr = MSR_DCD | MSR_DSR;
132 assert((msr & MSR_DELTA_MASK) == 0);
138 * The IIR returns a prioritized interrupt reason:
139 * - receive data available
140 * - transmit holding register empty
141 * - modem status change
143 * Return an interrupt reason if one is available.
146 uart_intr_reason(struct uart_ns16550_softc *sc)
149 if ((sc->lsr & LSR_OE) != 0 && (sc->ier & IER_ERLS) != 0)
151 else if (uart_rxfifo_numchars(sc->backend) > 0 &&
152 (sc->ier & IER_ERXRDY) != 0)
154 else if (sc->thre_int_pending && (sc->ier & IER_ETXRDY) != 0)
156 else if ((sc->msr & MSR_DELTA_MASK) != 0 && (sc->ier & IER_EMSC) != 0)
163 uart_reset(struct uart_ns16550_softc *sc)
167 divisor = DEFAULT_RCLK / DEFAULT_BAUD / 16;
169 sc->dlh = divisor >> 16;
170 sc->msr = modem_status(sc->mcr);
172 uart_rxfifo_reset(sc->backend, 1);
176 * Toggle the COM port's intr pin depending on whether or not we have an
177 * interrupt condition to report to the processor.
180 uart_toggle_intr(struct uart_ns16550_softc *sc)
184 intr_reason = uart_intr_reason(sc);
186 if (intr_reason == IIR_NOPEND)
187 (*sc->intr_deassert)(sc->arg);
189 (*sc->intr_assert)(sc->arg);
193 uart_drain(int fd __unused, enum ev_type ev, void *arg)
195 struct uart_ns16550_softc *sc;
200 assert(ev == EVF_READ);
203 * This routine is called in the context of the mevent thread
204 * to take out the softc lock to protect against concurrent
205 * access from a vCPU i/o exit
207 pthread_mutex_lock(&sc->mtx);
209 loopback = (sc->mcr & MCR_LOOPBACK) != 0;
210 uart_rxfifo_drain(sc->backend, loopback);
212 uart_toggle_intr(sc);
214 pthread_mutex_unlock(&sc->mtx);
218 uart_ns16550_write(struct uart_ns16550_softc *sc, int offset, uint8_t value)
223 pthread_mutex_lock(&sc->mtx);
226 * Take care of the special case DLAB accesses first
228 if ((sc->lcr & LCR_DLAB) != 0) {
229 if (offset == REG_DLL) {
234 if (offset == REG_DLH) {
242 if (uart_rxfifo_putchar(sc->backend, value,
243 (sc->mcr & MCR_LOOPBACK) != 0))
245 sc->thre_int_pending = true;
248 /* Set pending when IER_ETXRDY is raised (edge-triggered). */
249 if ((sc->ier & IER_ETXRDY) == 0 && (value & IER_ETXRDY) != 0)
250 sc->thre_int_pending = true;
252 * Apply mask so that bits 4-7 are 0
253 * Also enables bits 0-3 only if they're 1
255 sc->ier = value & 0x0F;
259 * When moving from FIFO and 16450 mode and vice versa,
260 * the FIFO contents are reset.
262 if ((sc->fcr & FCR_ENABLE) ^ (value & FCR_ENABLE)) {
263 fifosz = (value & FCR_ENABLE) ?
264 uart_rxfifo_size(sc->backend) : 1;
265 uart_rxfifo_reset(sc->backend, fifosz);
269 * The FCR_ENABLE bit must be '1' for the programming
270 * of other FCR bits to be effective.
272 if ((value & FCR_ENABLE) == 0) {
275 if ((value & FCR_RCV_RST) != 0)
276 uart_rxfifo_reset(sc->backend,
277 uart_rxfifo_size(sc->backend));
280 (FCR_ENABLE | FCR_DMA | FCR_RX_MASK);
287 /* Apply mask so that bits 5-7 are 0 */
288 sc->mcr = value & 0x1F;
289 msr = modem_status(sc->mcr);
292 * Detect if there has been any change between the
293 * previous and the new value of MSR. If there is
294 * then assert the appropriate MSR delta bit.
296 if ((msr & MSR_CTS) ^ (sc->msr & MSR_CTS))
298 if ((msr & MSR_DSR) ^ (sc->msr & MSR_DSR))
300 if ((msr & MSR_DCD) ^ (sc->msr & MSR_DCD))
302 if ((sc->msr & MSR_RI) != 0 && (msr & MSR_RI) == 0)
306 * Update the value of MSR while retaining the delta
309 sc->msr &= MSR_DELTA_MASK;
314 * Line status register is not meant to be written to
315 * during normal operation.
320 * As far as I can tell MSR is a read-only register.
331 uart_toggle_intr(sc);
332 pthread_mutex_unlock(&sc->mtx);
336 uart_ns16550_read(struct uart_ns16550_softc *sc, int offset)
338 uint8_t iir, intr_reason, reg;
340 pthread_mutex_lock(&sc->mtx);
343 * Take care of the special case DLAB accesses first
345 if ((sc->lcr & LCR_DLAB) != 0) {
346 if (offset == REG_DLL) {
351 if (offset == REG_DLH) {
359 reg = uart_rxfifo_getchar(sc->backend);
365 iir = (sc->fcr & FCR_ENABLE) ? IIR_FIFO_MASK : 0;
367 intr_reason = uart_intr_reason(sc);
370 * Deal with side effects of reading the IIR register
372 if (intr_reason == IIR_TXRDY)
373 sc->thre_int_pending = false;
386 /* Transmitter is always ready for more data */
387 sc->lsr |= LSR_TEMT | LSR_THRE;
389 /* Check for new receive data */
390 if (uart_rxfifo_numchars(sc->backend) > 0)
391 sc->lsr |= LSR_RXRDY;
393 sc->lsr &= ~LSR_RXRDY;
397 /* The LSR_OE bit is cleared on LSR read */
402 * MSR delta bits are cleared on read
405 sc->msr &= ~MSR_DELTA_MASK;
416 uart_toggle_intr(sc);
417 pthread_mutex_unlock(&sc->mtx);
423 uart_legacy_alloc(int which, int *baseaddr, int *irq)
426 if (which < 0 || which >= (int)UART_NLDEVS || uart_lres[which].inuse)
429 uart_lres[which].inuse = true;
430 *baseaddr = uart_lres[which].baseaddr;
431 *irq = uart_lres[which].irq;
436 struct uart_ns16550_softc *
437 uart_ns16550_init(uart_intr_func_t intr_assert, uart_intr_func_t intr_deassert,
440 struct uart_ns16550_softc *sc;
442 sc = calloc(1, sizeof(struct uart_ns16550_softc));
445 sc->intr_assert = intr_assert;
446 sc->intr_deassert = intr_deassert;
447 sc->backend = uart_init();
449 pthread_mutex_init(&sc->mtx, NULL);
457 uart_ns16550_tty_open(struct uart_ns16550_softc *sc, const char *device)
459 return (uart_tty_open(sc->backend, device, uart_drain, sc));
462 #ifdef BHYVE_SNAPSHOT
464 uart_ns16550_snapshot(struct uart_ns16550_softc *sc,
465 struct vm_snapshot_meta *meta)
469 SNAPSHOT_VAR_OR_LEAVE(sc->data, meta, ret, done);
470 SNAPSHOT_VAR_OR_LEAVE(sc->ier, meta, ret, done);
471 SNAPSHOT_VAR_OR_LEAVE(sc->lcr, meta, ret, done);
472 SNAPSHOT_VAR_OR_LEAVE(sc->mcr, meta, ret, done);
473 SNAPSHOT_VAR_OR_LEAVE(sc->lsr, meta, ret, done);
474 SNAPSHOT_VAR_OR_LEAVE(sc->msr, meta, ret, done);
475 SNAPSHOT_VAR_OR_LEAVE(sc->fcr, meta, ret, done);
476 SNAPSHOT_VAR_OR_LEAVE(sc->scr, meta, ret, done);
478 SNAPSHOT_VAR_OR_LEAVE(sc->dll, meta, ret, done);
479 SNAPSHOT_VAR_OR_LEAVE(sc->dlh, meta, ret, done);
481 ret = uart_rxfifo_snapshot(sc->backend, meta);
483 sc->thre_int_pending = 1;