hw/riscv/boot.c: use MachineState in riscv_load_kernel()
[qemu.git] / hw / char / nrf51_uart.c
blob3c6f982de97db3a5bada35c95d92daca3e17a8b8
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 "hw/qdev-properties-system.h"
22 #include "migration/vmstate.h"
23 #include "trace.h"
25 static void nrf51_uart_update_irq(NRF51UARTState *s)
27 bool irq = false;
29 irq |= (s->reg[R_UART_RXDRDY] &&
30 (s->reg[R_UART_INTEN] & R_UART_INTEN_RXDRDY_MASK));
31 irq |= (s->reg[R_UART_TXDRDY] &&
32 (s->reg[R_UART_INTEN] & R_UART_INTEN_TXDRDY_MASK));
33 irq |= (s->reg[R_UART_ERROR] &&
34 (s->reg[R_UART_INTEN] & R_UART_INTEN_ERROR_MASK));
35 irq |= (s->reg[R_UART_RXTO] &&
36 (s->reg[R_UART_INTEN] & R_UART_INTEN_RXTO_MASK));
38 qemu_set_irq(s->irq, irq);
41 static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)
43 NRF51UARTState *s = NRF51_UART(opaque);
44 uint64_t r;
46 if (!s->enabled) {
47 return 0;
50 switch (addr) {
51 case A_UART_RXD:
52 r = s->rx_fifo[s->rx_fifo_pos];
53 if (s->rx_started && s->rx_fifo_len) {
54 s->rx_fifo_pos = (s->rx_fifo_pos + 1) % UART_FIFO_LENGTH;
55 s->rx_fifo_len--;
56 if (s->rx_fifo_len) {
57 s->reg[R_UART_RXDRDY] = 1;
58 nrf51_uart_update_irq(s);
60 qemu_chr_fe_accept_input(&s->chr);
62 break;
63 case A_UART_INTENSET:
64 case A_UART_INTENCLR:
65 case A_UART_INTEN:
66 r = s->reg[R_UART_INTEN];
67 break;
68 default:
69 r = s->reg[addr / 4];
70 break;
73 trace_nrf51_uart_read(addr, r, size);
75 return r;
78 static gboolean uart_transmit(void *do_not_use, GIOCondition cond, void *opaque)
80 NRF51UARTState *s = NRF51_UART(opaque);
81 int r;
82 uint8_t c = s->reg[R_UART_TXD];
84 s->watch_tag = 0;
86 r = qemu_chr_fe_write(&s->chr, &c, 1);
87 if (r <= 0) {
88 s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
89 uart_transmit, s);
90 if (!s->watch_tag) {
91 /* The hardware has no transmit error reporting,
92 * so silently drop the byte
94 goto buffer_drained;
96 return FALSE;
99 buffer_drained:
100 s->reg[R_UART_TXDRDY] = 1;
101 s->pending_tx_byte = false;
102 return FALSE;
105 static void uart_cancel_transmit(NRF51UARTState *s)
107 if (s->watch_tag) {
108 g_source_remove(s->watch_tag);
109 s->watch_tag = 0;
113 static void uart_write(void *opaque, hwaddr addr,
114 uint64_t value, unsigned int size)
116 NRF51UARTState *s = NRF51_UART(opaque);
118 trace_nrf51_uart_write(addr, value, size);
120 if (!s->enabled && (addr != A_UART_ENABLE)) {
121 return;
124 switch (addr) {
125 case A_UART_TXD:
126 if (!s->pending_tx_byte && s->tx_started) {
127 s->reg[R_UART_TXD] = value;
128 s->pending_tx_byte = true;
129 uart_transmit(NULL, G_IO_OUT, s);
131 break;
132 case A_UART_INTEN:
133 s->reg[R_UART_INTEN] = value;
134 break;
135 case A_UART_INTENSET:
136 s->reg[R_UART_INTEN] |= value;
137 break;
138 case A_UART_INTENCLR:
139 s->reg[R_UART_INTEN] &= ~value;
140 break;
141 case A_UART_TXDRDY ... A_UART_RXTO:
142 s->reg[addr / 4] = value;
143 break;
144 case A_UART_ERRORSRC:
145 s->reg[addr / 4] &= ~value;
146 break;
147 case A_UART_RXD:
148 break;
149 case A_UART_RXDRDY:
150 if (value == 0) {
151 s->reg[R_UART_RXDRDY] = 0;
153 break;
154 case A_UART_STARTTX:
155 if (value == 1) {
156 s->tx_started = true;
158 break;
159 case A_UART_STARTRX:
160 if (value == 1) {
161 s->rx_started = true;
163 break;
164 case A_UART_ENABLE:
165 if (value) {
166 if (value == 4) {
167 s->enabled = true;
169 break;
171 s->enabled = false;
172 value = 1;
173 /* fall through */
174 case A_UART_SUSPEND:
175 case A_UART_STOPTX:
176 if (value == 1) {
177 s->tx_started = false;
179 /* fall through */
180 case A_UART_STOPRX:
181 if (addr != A_UART_STOPTX && value == 1) {
182 s->rx_started = false;
183 s->reg[R_UART_RXTO] = 1;
185 break;
186 default:
187 s->reg[addr / 4] = value;
188 break;
190 nrf51_uart_update_irq(s);
193 static const MemoryRegionOps uart_ops = {
194 .read = uart_read,
195 .write = uart_write,
196 .endianness = DEVICE_LITTLE_ENDIAN,
199 static void nrf51_uart_reset(DeviceState *dev)
201 NRF51UARTState *s = NRF51_UART(dev);
203 s->pending_tx_byte = 0;
205 uart_cancel_transmit(s);
207 memset(s->reg, 0, sizeof(s->reg));
209 s->reg[R_UART_PSELRTS] = 0xFFFFFFFF;
210 s->reg[R_UART_PSELTXD] = 0xFFFFFFFF;
211 s->reg[R_UART_PSELCTS] = 0xFFFFFFFF;
212 s->reg[R_UART_PSELRXD] = 0xFFFFFFFF;
213 s->reg[R_UART_BAUDRATE] = 0x4000000;
215 s->rx_fifo_len = 0;
216 s->rx_fifo_pos = 0;
217 s->rx_started = false;
218 s->tx_started = false;
219 s->enabled = false;
222 static void uart_receive(void *opaque, const uint8_t *buf, int size)
225 NRF51UARTState *s = NRF51_UART(opaque);
226 int i;
228 if (size == 0 || s->rx_fifo_len >= UART_FIFO_LENGTH) {
229 return;
232 for (i = 0; i < size; i++) {
233 uint32_t pos = (s->rx_fifo_pos + s->rx_fifo_len) % UART_FIFO_LENGTH;
234 s->rx_fifo[pos] = buf[i];
235 s->rx_fifo_len++;
238 s->reg[R_UART_RXDRDY] = 1;
239 nrf51_uart_update_irq(s);
242 static int uart_can_receive(void *opaque)
244 NRF51UARTState *s = NRF51_UART(opaque);
246 return s->rx_started ? (UART_FIFO_LENGTH - s->rx_fifo_len) : 0;
249 static void uart_event(void *opaque, QEMUChrEvent event)
251 NRF51UARTState *s = NRF51_UART(opaque);
253 if (event == CHR_EVENT_BREAK) {
254 s->reg[R_UART_ERRORSRC] |= 3;
255 s->reg[R_UART_ERROR] = 1;
256 nrf51_uart_update_irq(s);
260 static void nrf51_uart_realize(DeviceState *dev, Error **errp)
262 NRF51UARTState *s = NRF51_UART(dev);
264 qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
265 uart_event, NULL, s, NULL, true);
268 static void nrf51_uart_init(Object *obj)
270 NRF51UARTState *s = NRF51_UART(obj);
271 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
273 memory_region_init_io(&s->iomem, obj, &uart_ops, s,
274 "nrf51_soc.uart", UART_SIZE);
275 sysbus_init_mmio(sbd, &s->iomem);
276 sysbus_init_irq(sbd, &s->irq);
279 static int nrf51_uart_post_load(void *opaque, int version_id)
281 NRF51UARTState *s = NRF51_UART(opaque);
283 if (s->pending_tx_byte) {
284 s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
285 uart_transmit, s);
288 return 0;
291 static const VMStateDescription nrf51_uart_vmstate = {
292 .name = "nrf51_soc.uart",
293 .post_load = nrf51_uart_post_load,
294 .fields = (VMStateField[]) {
295 VMSTATE_UINT32_ARRAY(reg, NRF51UARTState, 0x56C),
296 VMSTATE_UINT8_ARRAY(rx_fifo, NRF51UARTState, UART_FIFO_LENGTH),
297 VMSTATE_UINT32(rx_fifo_pos, NRF51UARTState),
298 VMSTATE_UINT32(rx_fifo_len, NRF51UARTState),
299 VMSTATE_BOOL(rx_started, NRF51UARTState),
300 VMSTATE_BOOL(tx_started, NRF51UARTState),
301 VMSTATE_BOOL(pending_tx_byte, NRF51UARTState),
302 VMSTATE_BOOL(enabled, NRF51UARTState),
303 VMSTATE_END_OF_LIST()
307 static Property nrf51_uart_properties[] = {
308 DEFINE_PROP_CHR("chardev", NRF51UARTState, chr),
309 DEFINE_PROP_END_OF_LIST(),
312 static void nrf51_uart_class_init(ObjectClass *klass, void *data)
314 DeviceClass *dc = DEVICE_CLASS(klass);
316 dc->reset = nrf51_uart_reset;
317 dc->realize = nrf51_uart_realize;
318 device_class_set_props(dc, nrf51_uart_properties);
319 dc->vmsd = &nrf51_uart_vmstate;
322 static const TypeInfo nrf51_uart_info = {
323 .name = TYPE_NRF51_UART,
324 .parent = TYPE_SYS_BUS_DEVICE,
325 .instance_size = sizeof(NRF51UARTState),
326 .instance_init = nrf51_uart_init,
327 .class_init = nrf51_uart_class_init
330 static void nrf51_uart_register_types(void)
332 type_register_static(&nrf51_uart_info);
335 type_init(nrf51_uart_register_types)