pcie_aer: Fix help message of pcie_aer_inject_error command
[qemu/ar7.git] / hw / char / nrf51_uart.c
blobd1fef77acde6440241ee701721783dafb11402c8
1 /*
2 * nRF51 SoC UART emulation
4 * See nRF51 Series Reference Manual, "29 Universal Asynchronous
5 * Receiver/Transmitter" for hardware specifications:
6 * http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
8 * Copyright (c) 2018 Julia Suvorova <jusual@mail.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 or
12 * (at your option) any later version.
15 #include "qemu/osdep.h"
16 #include "qemu/log.h"
17 #include "qemu/module.h"
18 #include "hw/char/nrf51_uart.h"
19 #include "hw/irq.h"
20 #include "hw/qdev-properties.h"
21 #include "migration/vmstate.h"
22 #include "trace.h"
24 static void nrf51_uart_update_irq(NRF51UARTState *s)
26 bool irq = false;
28 irq |= (s->reg[R_UART_RXDRDY] &&
29 (s->reg[R_UART_INTEN] & R_UART_INTEN_RXDRDY_MASK));
30 irq |= (s->reg[R_UART_TXDRDY] &&
31 (s->reg[R_UART_INTEN] & R_UART_INTEN_TXDRDY_MASK));
32 irq |= (s->reg[R_UART_ERROR] &&
33 (s->reg[R_UART_INTEN] & R_UART_INTEN_ERROR_MASK));
34 irq |= (s->reg[R_UART_RXTO] &&
35 (s->reg[R_UART_INTEN] & R_UART_INTEN_RXTO_MASK));
37 qemu_set_irq(s->irq, irq);
40 static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
42 NRF51UARTState *s = NRF51_UART(opaque);
43 uint64_t r;
45 if (!s->enabled) {
46 return 0;
49 switch (addr) {
50 case A_UART_RXD:
51 r = s->rx_fifo[s->rx_fifo_pos];
52 if (s->rx_started && s->rx_fifo_len) {
53 s->rx_fifo_pos = (s->rx_fifo_pos + 1) % UART_FIFO_LENGTH;
54 s->rx_fifo_len--;
55 if (s->rx_fifo_len) {
56 s->reg[R_UART_RXDRDY] = 1;
57 nrf51_uart_update_irq(s);
59 qemu_chr_fe_accept_input(&s->chr);
61 break;
62 case A_UART_INTENSET:
63 case A_UART_INTENCLR:
64 case A_UART_INTEN:
65 r = s->reg[R_UART_INTEN];
66 break;
67 default:
68 r = s->reg[addr / 4];
69 break;
72 trace_nrf51_uart_read(addr, r, size);
74 return r;
77 static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
79 NRF51UARTState *s = NRF51_UART(opaque);
80 int r;
81 uint8_t c = s->reg[R_UART_TXD];
83 s->watch_tag = 0;
85 r = qemu_chr_fe_write(&s->chr, &c, 1);
86 if (r <= 0) {
87 s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
88 uart_transmit, s);
89 if (!s->watch_tag) {
90 /* The hardware has no transmit error reporting,
91 * so silently drop the byte
93 goto buffer_drained;
95 return FALSE;
98 buffer_drained:
99 s->reg[R_UART_TXDRDY] = 1;
100 s->pending_tx_byte = false;
101 return FALSE;
104 static void uart_cancel_transmit(NRF51UARTState *s)
106 if (s->watch_tag) {
107 g_source_remove(s->watch_tag);
108 s->watch_tag = 0;
112 static void uart_write(void *opaque, hwaddr addr,
113 uint64_t value, unsigned int size)
115 NRF51UARTState *s = NRF51_UART(opaque);
117 trace_nrf51_uart_write(addr, value, size);
119 if (!s->enabled && (addr != A_UART_ENABLE)) {
120 return;
123 switch (addr) {
124 case A_UART_TXD:
125 if (!s->pending_tx_byte && s->tx_started) {
126 s->reg[R_UART_TXD] = value;
127 s->pending_tx_byte = true;
128 uart_transmit(NULL, G_IO_OUT, s);
130 break;
131 case A_UART_INTEN:
132 s->reg[R_UART_INTEN] = value;
133 break;
134 case A_UART_INTENSET:
135 s->reg[R_UART_INTEN] |= value;
136 break;
137 case A_UART_INTENCLR:
138 s->reg[R_UART_INTEN] &= ~value;
139 break;
140 case A_UART_TXDRDY ... A_UART_RXTO:
141 s->reg[addr / 4] = value;
142 break;
143 case A_UART_ERRORSRC:
144 s->reg[addr / 4] &= ~value;
145 break;
146 case A_UART_RXD:
147 break;
148 case A_UART_RXDRDY:
149 if (value == 0) {
150 s->reg[R_UART_RXDRDY] = 0;
152 break;
153 case A_UART_STARTTX:
154 if (value == 1) {
155 s->tx_started = true;
157 break;
158 case A_UART_STARTRX:
159 if (value == 1) {
160 s->rx_started = true;
162 break;
163 case A_UART_ENABLE:
164 if (value) {
165 if (value == 4) {
166 s->enabled = true;
168 break;
170 s->enabled = false;
171 value = 1;
172 /* fall through */
173 case A_UART_SUSPEND:
174 case A_UART_STOPTX:
175 if (value == 1) {
176 s->tx_started = false;
178 /* fall through */
179 case A_UART_STOPRX:
180 if (addr != A_UART_STOPTX && value == 1) {
181 s->rx_started = false;
182 s->reg[R_UART_RXTO] = 1;
184 break;
185 default:
186 s->reg[addr / 4] = value;
187 break;
189 nrf51_uart_update_irq(s);
192 static const MemoryRegionOps uart_ops = {
193 .read = uart_read,
194 .write = uart_write,
195 .endianness = DEVICE_LITTLE_ENDIAN,
198 static void nrf51_uart_reset(DeviceState *dev)
200 NRF51UARTState *s = NRF51_UART(dev);
202 s->pending_tx_byte = 0;
204 uart_cancel_transmit(s);
206 memset(s->reg, 0, sizeof(s->reg));
208 s->reg[R_UART_PSELRTS] = 0xFFFFFFFF;
209 s->reg[R_UART_PSELTXD] = 0xFFFFFFFF;
210 s->reg[R_UART_PSELCTS] = 0xFFFFFFFF;
211 s->reg[R_UART_PSELRXD] = 0xFFFFFFFF;
212 s->reg[R_UART_BAUDRATE] = 0x4000000;
214 s->rx_fifo_len = 0;
215 s->rx_fifo_pos = 0;
216 s->rx_started = false;
217 s->tx_started = false;
218 s->enabled = false;
221 static void uart_receive(void *opaque, const uint8_t *buf, int size)
224 NRF51UARTState *s = NRF51_UART(opaque);
225 int i;
227 if (size == 0 || s->rx_fifo_len >= UART_FIFO_LENGTH) {
228 return;
231 for (i = 0; i < size; i++) {
232 uint32_t pos = (s->rx_fifo_pos + s->rx_fifo_len) % UART_FIFO_LENGTH;
233 s->rx_fifo[pos] = buf[i];
234 s->rx_fifo_len++;
237 s->reg[R_UART_RXDRDY] = 1;
238 nrf51_uart_update_irq(s);
241 static int uart_can_receive(void *opaque)
243 NRF51UARTState *s = NRF51_UART(opaque);
245 return s->rx_started ? (UART_FIFO_LENGTH - s->rx_fifo_len) : 0;
248 static void uart_event(void *opaque, QEMUChrEvent event)
250 NRF51UARTState *s = NRF51_UART(opaque);
252 if (event == CHR_EVENT_BREAK) {
253 s->reg[R_UART_ERRORSRC] |= 3;
254 s->reg[R_UART_ERROR] = 1;
255 nrf51_uart_update_irq(s);
259 static void nrf51_uart_realize(DeviceState *dev, Error **errp)
261 NRF51UARTState *s = NRF51_UART(dev);
263 qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
264 uart_event, NULL, s, NULL, true);
267 static void nrf51_uart_init(Object *obj)
269 NRF51UARTState *s = NRF51_UART(obj);
270 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
272 memory_region_init_io(&s->iomem, obj, &uart_ops, s,
273 "nrf51_soc.uart", UART_SIZE);
274 sysbus_init_mmio(sbd, &s->iomem);
275 sysbus_init_irq(sbd, &s->irq);
278 static int nrf51_uart_post_load(void *opaque, int version_id)
280 NRF51UARTState *s = NRF51_UART(opaque);
282 if (s->pending_tx_byte) {
283 s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
284 uart_transmit, s);
287 return 0;
290 static const VMStateDescription nrf51_uart_vmstate = {
291 .name = "nrf51_soc.uart",
292 .post_load = nrf51_uart_post_load,
293 .fields = (VMStateField[]) {
294 VMSTATE_UINT32_ARRAY(reg, NRF51UARTState, 0x56C),
295 VMSTATE_UINT8_ARRAY(rx_fifo, NRF51UARTState, UART_FIFO_LENGTH),
296 VMSTATE_UINT32(rx_fifo_pos, NRF51UARTState),
297 VMSTATE_UINT32(rx_fifo_len, NRF51UARTState),
298 VMSTATE_BOOL(rx_started, NRF51UARTState),
299 VMSTATE_BOOL(tx_started, NRF51UARTState),
300 VMSTATE_BOOL(pending_tx_byte, NRF51UARTState),
301 VMSTATE_BOOL(enabled, NRF51UARTState),
302 VMSTATE_END_OF_LIST()
306 static Property nrf51_uart_properties[] = {
307 DEFINE_PROP_CHR("chardev", NRF51UARTState, chr),
308 DEFINE_PROP_END_OF_LIST(),
311 static void nrf51_uart_class_init(ObjectClass *klass, void *data)
313 DeviceClass *dc = DEVICE_CLASS(klass);
315 dc->reset = nrf51_uart_reset;
316 dc->realize = nrf51_uart_realize;
317 device_class_set_props(dc, nrf51_uart_properties);
318 dc->vmsd = &nrf51_uart_vmstate;
321 static const TypeInfo nrf51_uart_info = {
322 .name = TYPE_NRF51_UART,
323 .parent = TYPE_SYS_BUS_DEVICE,
324 .instance_size = sizeof(NRF51UARTState),
325 .instance_init = nrf51_uart_init,
326 .class_init = nrf51_uart_class_init
329 static void nrf51_uart_register_types(void)
331 type_register_static(&nrf51_uart_info);
334 type_init(nrf51_uart_register_types)