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 "migration/vmstate.h"
23 #include "chardev/char.h"
24 #include "chardev/char-fe.h"
26 #include "hw/char/sifive_uart.h"
27 #include "hw/qdev-properties-system.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 sifive_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 sifive_uart_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 sifive_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
);
79 sifive_uart_update_irq(s
);
84 case SIFIVE_UART_TXFIFO
:
85 return 0; /* Should check tx fifo */
89 return sifive_uart_ip(s
);
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 sifive_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);
114 sifive_uart_update_irq(s
);
118 sifive_uart_update_irq(s
);
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 sifive_uart_ops
= {
135 .read
= sifive_uart_read
,
136 .write
= sifive_uart_write
,
137 .endianness
= DEVICE_NATIVE_ENDIAN
,
139 .min_access_size
= 4,
144 static void sifive_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
;
155 sifive_uart_update_irq(s
);
158 static int sifive_uart_can_rx(void *opaque
)
160 SiFiveUARTState
*s
= opaque
;
162 return s
->rx_fifo_len
< sizeof(s
->rx_fifo
);
165 static void sifive_uart_event(void *opaque
, QEMUChrEvent event
)
169 static int sifive_uart_be_change(void *opaque
)
171 SiFiveUARTState
*s
= opaque
;
173 qemu_chr_fe_set_handlers(&s
->chr
, sifive_uart_can_rx
, sifive_uart_rx
,
174 sifive_uart_event
, sifive_uart_be_change
, s
,
180 static Property sifive_uart_properties
[] = {
181 DEFINE_PROP_CHR("chardev", SiFiveUARTState
, chr
),
182 DEFINE_PROP_END_OF_LIST(),
185 static void sifive_uart_init(Object
*obj
)
187 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
188 SiFiveUARTState
*s
= SIFIVE_UART(obj
);
190 memory_region_init_io(&s
->mmio
, OBJECT(s
), &sifive_uart_ops
, s
,
191 TYPE_SIFIVE_UART
, SIFIVE_UART_MAX
);
192 sysbus_init_mmio(sbd
, &s
->mmio
);
193 sysbus_init_irq(sbd
, &s
->irq
);
196 static void sifive_uart_realize(DeviceState
*dev
, Error
**errp
)
198 SiFiveUARTState
*s
= SIFIVE_UART(dev
);
200 qemu_chr_fe_set_handlers(&s
->chr
, sifive_uart_can_rx
, sifive_uart_rx
,
201 sifive_uart_event
, sifive_uart_be_change
, s
,
206 static void sifive_uart_reset_enter(Object
*obj
, ResetType type
)
208 SiFiveUARTState
*s
= SIFIVE_UART(obj
);
217 static void sifive_uart_reset_hold(Object
*obj
)
219 SiFiveUARTState
*s
= SIFIVE_UART(obj
);
220 qemu_irq_lower(s
->irq
);
223 static const VMStateDescription vmstate_sifive_uart
= {
224 .name
= TYPE_SIFIVE_UART
,
226 .minimum_version_id
= 1,
227 .fields
= (VMStateField
[]) {
228 VMSTATE_UINT8_ARRAY(rx_fifo
, SiFiveUARTState
,
229 SIFIVE_UART_RX_FIFO_SIZE
),
230 VMSTATE_UINT8(rx_fifo_len
, SiFiveUARTState
),
231 VMSTATE_UINT32(ie
, SiFiveUARTState
),
232 VMSTATE_UINT32(ip
, SiFiveUARTState
),
233 VMSTATE_UINT32(txctrl
, SiFiveUARTState
),
234 VMSTATE_UINT32(rxctrl
, SiFiveUARTState
),
235 VMSTATE_UINT32(div
, SiFiveUARTState
),
236 VMSTATE_END_OF_LIST()
241 static void sifive_uart_class_init(ObjectClass
*oc
, void *data
)
243 DeviceClass
*dc
= DEVICE_CLASS(oc
);
244 ResettableClass
*rc
= RESETTABLE_CLASS(oc
);
246 dc
->realize
= sifive_uart_realize
;
247 dc
->vmsd
= &vmstate_sifive_uart
;
248 rc
->phases
.enter
= sifive_uart_reset_enter
;
249 rc
->phases
.hold
= sifive_uart_reset_hold
;
250 device_class_set_props(dc
, sifive_uart_properties
);
251 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
254 static const TypeInfo sifive_uart_info
= {
255 .name
= TYPE_SIFIVE_UART
,
256 .parent
= TYPE_SYS_BUS_DEVICE
,
257 .instance_size
= sizeof(SiFiveUARTState
),
258 .instance_init
= sifive_uart_init
,
259 .class_init
= sifive_uart_class_init
,
262 static void sifive_uart_register_types(void)
264 type_register_static(&sifive_uart_info
);
267 type_init(sifive_uart_register_types
)
270 * Create UART device.
272 SiFiveUARTState
*sifive_uart_create(MemoryRegion
*address_space
, hwaddr base
,
273 Chardev
*chr
, qemu_irq irq
)
278 dev
= qdev_new("riscv.sifive.uart");
279 s
= SYS_BUS_DEVICE(dev
);
280 qdev_prop_set_chr(dev
, "chardev", chr
);
281 sysbus_realize_and_unref(s
, &error_fatal
);
282 memory_region_add_subregion(address_space
, base
,
283 sysbus_mmio_get_region(s
, 0));
284 sysbus_connect_irq(s
, 0, irq
);
286 return SIFIVE_UART(dev
);