2 * QEMU model of the UART on the SiFive E300 and U500 series SOCs.
4 * Copyright (c) 2016 Stefan O'Rear
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
22 #include "hw/sysbus.h"
23 #include "chardev/char.h"
24 #include "chardev/char-fe.h"
27 #include "hw/riscv/sifive_uart.h"
30 * Not yet implemented:
32 * Transmit FIFO using "qemu/fifo8.h"
35 /* Returns the state of the IP (interrupt pending) register */
36 static uint64_t uart_ip(SiFiveUARTState *s)
40 uint64_t txcnt = SIFIVE_UART_GET_TXCNT(s->txctrl);
41 uint64_t rxcnt = SIFIVE_UART_GET_RXCNT(s->rxctrl);
44 ret |= SIFIVE_UART_IP_TXWM;
46 if (s->rx_fifo_len > rxcnt) {
47 ret |= SIFIVE_UART_IP_RXWM;
53 static void update_irq(SiFiveUARTState *s)
56 if ((s->ie & SIFIVE_UART_IE_TXWM) ||
57 ((s->ie & SIFIVE_UART_IE_RXWM) && s->rx_fifo_len)) {
61 qemu_irq_raise(s->irq);
63 qemu_irq_lower(s->irq);
68 uart_read(void *opaque, hwaddr addr, unsigned int size)
70 SiFiveUARTState *s = opaque;
73 case SIFIVE_UART_RXFIFO:
76 memmove(s->rx_fifo, s->rx_fifo + 1, s->rx_fifo_len - 1);
78 qemu_chr_fe_accept_input(&s->chr);
84 case SIFIVE_UART_TXFIFO:
85 return 0; /* Should check tx fifo */
90 case SIFIVE_UART_TXCTRL:
92 case SIFIVE_UART_RXCTRL:
98 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read: addr=0x%x\n",
104 uart_write(void *opaque, hwaddr addr,
105 uint64_t val64, unsigned int size)
107 SiFiveUARTState *s = opaque;
108 uint32_t value = val64;
109 unsigned char ch = value;
112 case SIFIVE_UART_TXFIFO:
113 qemu_chr_fe_write(&s->chr, &ch, 1);
120 case SIFIVE_UART_TXCTRL:
123 case SIFIVE_UART_RXCTRL:
126 case SIFIVE_UART_DIV:
130 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%x v=0x%x\n",
131 __func__, (int)addr, (int)value);
134 static const MemoryRegionOps uart_ops = {
137 .endianness = DEVICE_NATIVE_ENDIAN,
139 .min_access_size = 4,
144 static void uart_rx(void *opaque, const uint8_t *buf, int size)
146 SiFiveUARTState *s = opaque;
149 if (s->rx_fifo_len >= sizeof(s->rx_fifo)) {
150 printf("WARNING: UART dropped char.\n");
153 s->rx_fifo[s->rx_fifo_len++] = *buf;
158 static int uart_can_rx(void *opaque)
160 SiFiveUARTState *s = opaque;
162 return s->rx_fifo_len < sizeof(s->rx_fifo);
165 static void uart_event(void *opaque, int event)
169 static int uart_be_change(void *opaque)
171 SiFiveUARTState *s = opaque;
173 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event,
174 uart_be_change, s, NULL, true);
180 * Create UART device.
182 SiFiveUARTState *sifive_uart_create(MemoryRegion *address_space, hwaddr base,
183 Chardev *chr, qemu_irq irq)
185 SiFiveUARTState *s = g_malloc0(sizeof(SiFiveUARTState));
187 qemu_chr_fe_init(&s->chr, chr, &error_abort);
188 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event,
189 uart_be_change, s, NULL, true);
190 memory_region_init_io(&s->mmio, NULL, &uart_ops, s,
191 TYPE_SIFIVE_UART, SIFIVE_UART_MAX);
192 memory_region_add_subregion(address_space, base, &s->mmio);