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"
17 #include "qemu/module.h"
18 #include "hw/char/nrf51_uart.h"
20 #include "hw/qdev-properties.h"
21 #include "migration/vmstate.h"
24 static void nrf51_uart_update_irq(NRF51UARTState
*s
)
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
);
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
;
56 s
->reg
[R_UART_RXDRDY
] = 1;
57 nrf51_uart_update_irq(s
);
59 qemu_chr_fe_accept_input(&s
->chr
);
65 r
= s
->reg
[R_UART_INTEN
];
72 trace_nrf51_uart_read(addr
, r
, size
);
77 static gboolean
uart_transmit(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
79 NRF51UARTState
*s
= NRF51_UART(opaque
);
81 uint8_t c
= s
->reg
[R_UART_TXD
];
85 r
= qemu_chr_fe_write(&s
->chr
, &c
, 1);
87 s
->watch_tag
= qemu_chr_fe_add_watch(&s
->chr
, G_IO_OUT
| G_IO_HUP
,
90 /* The hardware has no transmit error reporting,
91 * so silently drop the byte
99 s
->reg
[R_UART_TXDRDY
] = 1;
100 s
->pending_tx_byte
= false;
104 static void uart_cancel_transmit(NRF51UARTState
*s
)
107 g_source_remove(s
->watch_tag
);
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
)) {
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
);
132 s
->reg
[R_UART_INTEN
] = value
;
134 case A_UART_INTENSET
:
135 s
->reg
[R_UART_INTEN
] |= value
;
137 case A_UART_INTENCLR
:
138 s
->reg
[R_UART_INTEN
] &= ~value
;
140 case A_UART_TXDRDY
... A_UART_RXTO
:
141 s
->reg
[addr
/ 4] = value
;
143 case A_UART_ERRORSRC
:
144 s
->reg
[addr
/ 4] &= ~value
;
150 s
->reg
[R_UART_RXDRDY
] = 0;
155 s
->tx_started
= true;
160 s
->rx_started
= true;
176 s
->tx_started
= false;
180 if (addr
!= A_UART_STOPTX
&& value
== 1) {
181 s
->rx_started
= false;
182 s
->reg
[R_UART_RXTO
] = 1;
186 s
->reg
[addr
/ 4] = value
;
189 nrf51_uart_update_irq(s
);
192 static const MemoryRegionOps uart_ops
= {
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;
216 s
->rx_started
= false;
217 s
->tx_started
= false;
221 static void uart_receive(void *opaque
, const uint8_t *buf
, int size
)
224 NRF51UARTState
*s
= NRF51_UART(opaque
);
227 if (size
== 0 || s
->rx_fifo_len
>= UART_FIFO_LENGTH
) {
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
];
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
,
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
)