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 "hw/qdev-properties-system.h"
22 #include "migration/vmstate.h"
25 static void nrf51_uart_update_irq(NRF51UARTState
*s
)
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
);
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
;
57 s
->reg
[R_UART_RXDRDY
] = 1;
58 nrf51_uart_update_irq(s
);
60 qemu_chr_fe_accept_input(&s
->chr
);
66 r
= s
->reg
[R_UART_INTEN
];
73 trace_nrf51_uart_read(addr
, r
, size
);
78 static gboolean
uart_transmit(void *do_not_use
, GIOCondition cond
, void *opaque
)
80 NRF51UARTState
*s
= NRF51_UART(opaque
);
82 uint8_t c
= s
->reg
[R_UART_TXD
];
86 r
= qemu_chr_fe_write(&s
->chr
, &c
, 1);
88 s
->watch_tag
= qemu_chr_fe_add_watch(&s
->chr
, G_IO_OUT
| G_IO_HUP
,
91 /* The hardware has no transmit error reporting,
92 * so silently drop the byte
100 s
->reg
[R_UART_TXDRDY
] = 1;
101 s
->pending_tx_byte
= false;
105 static void uart_cancel_transmit(NRF51UARTState
*s
)
108 g_source_remove(s
->watch_tag
);
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
)) {
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
);
133 s
->reg
[R_UART_INTEN
] = value
;
135 case A_UART_INTENSET
:
136 s
->reg
[R_UART_INTEN
] |= value
;
138 case A_UART_INTENCLR
:
139 s
->reg
[R_UART_INTEN
] &= ~value
;
141 case A_UART_TXDRDY
... A_UART_RXTO
:
142 s
->reg
[addr
/ 4] = value
;
144 case A_UART_ERRORSRC
:
145 s
->reg
[addr
/ 4] &= ~value
;
151 s
->reg
[R_UART_RXDRDY
] = 0;
156 s
->tx_started
= true;
161 s
->rx_started
= true;
177 s
->tx_started
= false;
181 if (addr
!= A_UART_STOPTX
&& value
== 1) {
182 s
->rx_started
= false;
183 s
->reg
[R_UART_RXTO
] = 1;
187 s
->reg
[addr
/ 4] = value
;
190 nrf51_uart_update_irq(s
);
193 static const MemoryRegionOps uart_ops
= {
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;
217 s
->rx_started
= false;
218 s
->tx_started
= false;
222 static void uart_receive(void *opaque
, const uint8_t *buf
, int size
)
225 NRF51UARTState
*s
= NRF51_UART(opaque
);
228 if (size
== 0 || s
->rx_fifo_len
>= UART_FIFO_LENGTH
) {
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
];
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
,
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
)