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"
21 #include "hw/sysbus.h"
22 #include "chardev/char.h"
23 #include "chardev/char-fe.h"
24 #include "target/riscv/cpu.h"
25 #include "hw/riscv/sifive_uart.h"
28 * Not yet implemented:
30 * Transmit FIFO using "qemu/fifo8.h"
31 * SIFIVE_UART_IE_TXWM interrupts
32 * SIFIVE_UART_IE_RXWM interrupts must honor fifo watermark
33 * Rx FIFO watermark interrupt trigger threshold
34 * Tx FIFO watermark interrupt trigger threshold.
37 static void update_irq(SiFiveUARTState *s)
40 if ((s->ie & SIFIVE_UART_IE_RXWM) && s->rx_fifo_len) {
44 qemu_irq_raise(s->irq);
46 qemu_irq_lower(s->irq);
51 uart_read(void *opaque, hwaddr addr, unsigned int size)
53 SiFiveUARTState *s = opaque;
56 case SIFIVE_UART_RXFIFO:
59 memmove(s->rx_fifo, s->rx_fifo + 1, s->rx_fifo_len - 1);
61 qemu_chr_fe_accept_input(&s->chr);
67 case SIFIVE_UART_TXFIFO:
68 return 0; /* Should check tx fifo */
72 return s->rx_fifo_len ? SIFIVE_UART_IP_RXWM : 0;
73 case SIFIVE_UART_TXCTRL:
75 case SIFIVE_UART_RXCTRL:
81 hw_error("%s: bad read: addr=0x%x\n",
87 uart_write(void *opaque, hwaddr addr,
88 uint64_t val64, unsigned int size)
90 SiFiveUARTState *s = opaque;
91 uint32_t value = val64;
92 unsigned char ch = value;
95 case SIFIVE_UART_TXFIFO:
96 qemu_chr_fe_write(&s->chr, &ch, 1);
102 case SIFIVE_UART_TXCTRL:
105 case SIFIVE_UART_RXCTRL:
108 case SIFIVE_UART_DIV:
112 hw_error("%s: bad write: addr=0x%x v=0x%x\n",
113 __func__, (int)addr, (int)value);
116 static const MemoryRegionOps uart_ops = {
119 .endianness = DEVICE_NATIVE_ENDIAN,
121 .min_access_size = 4,
126 static void uart_rx(void *opaque, const uint8_t *buf, int size)
128 SiFiveUARTState *s = opaque;
131 if (s->rx_fifo_len >= sizeof(s->rx_fifo)) {
132 printf("WARNING: UART dropped char.\n");
135 s->rx_fifo[s->rx_fifo_len++] = *buf;
140 static int uart_can_rx(void *opaque)
142 SiFiveUARTState *s = opaque;
144 return s->rx_fifo_len < sizeof(s->rx_fifo);
147 static void uart_event(void *opaque, int event)
151 static int uart_be_change(void *opaque)
153 SiFiveUARTState *s = opaque;
155 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event,
156 uart_be_change, s, NULL, true);
162 * Create UART device.
164 SiFiveUARTState *sifive_uart_create(MemoryRegion *address_space, hwaddr base,
165 Chardev *chr, qemu_irq irq)
167 SiFiveUARTState *s = g_malloc0(sizeof(SiFiveUARTState));
169 qemu_chr_fe_init(&s->chr, chr, &error_abort);
170 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event,
171 uart_be_change, s, NULL, true);
172 memory_region_init_io(&s->mmio, NULL, &uart_ops, s,
173 TYPE_SIFIVE_UART, SIFIVE_UART_MAX);
174 memory_region_add_subregion(address_space, base, &s->mmio);