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 "hw/char/nrf51_uart.h"
20 static void nrf51_uart_update_irq(NRF51UARTState
*s
)
24 irq
|= (s
->reg
[R_UART_RXDRDY
] &&
25 (s
->reg
[R_UART_INTEN
] & R_UART_INTEN_RXDRDY_MASK
));
26 irq
|= (s
->reg
[R_UART_TXDRDY
] &&
27 (s
->reg
[R_UART_INTEN
] & R_UART_INTEN_TXDRDY_MASK
));
28 irq
|= (s
->reg
[R_UART_ERROR
] &&
29 (s
->reg
[R_UART_INTEN
] & R_UART_INTEN_ERROR_MASK
));
30 irq
|= (s
->reg
[R_UART_RXTO
] &&
31 (s
->reg
[R_UART_INTEN
] & R_UART_INTEN_RXTO_MASK
));
33 qemu_set_irq(s
->irq
, irq
);
36 static uint64_t uart_read(void *opaque
, hwaddr addr
, unsigned int size
)
38 NRF51UARTState
*s
= NRF51_UART(opaque
);
47 r
= s
->rx_fifo
[s
->rx_fifo_pos
];
48 if (s
->rx_started
&& s
->rx_fifo_len
) {
49 s
->rx_fifo_pos
= (s
->rx_fifo_pos
+ 1) % UART_FIFO_LENGTH
;
52 s
->reg
[R_UART_RXDRDY
] = 1;
53 nrf51_uart_update_irq(s
);
55 qemu_chr_fe_accept_input(&s
->chr
);
61 r
= s
->reg
[R_UART_INTEN
];
68 trace_nrf51_uart_read(addr
, r
, size
);
73 static gboolean
uart_transmit(GIOChannel
*chan
, GIOCondition cond
, void *opaque
)
75 NRF51UARTState
*s
= NRF51_UART(opaque
);
77 uint8_t c
= s
->reg
[R_UART_TXD
];
81 r
= qemu_chr_fe_write(&s
->chr
, &c
, 1);
83 s
->watch_tag
= qemu_chr_fe_add_watch(&s
->chr
, G_IO_OUT
| G_IO_HUP
,
86 /* The hardware has no transmit error reporting,
87 * so silently drop the byte
95 s
->reg
[R_UART_TXDRDY
] = 1;
96 s
->pending_tx_byte
= false;
100 static void uart_cancel_transmit(NRF51UARTState
*s
)
103 g_source_remove(s
->watch_tag
);
108 static void uart_write(void *opaque
, hwaddr addr
,
109 uint64_t value
, unsigned int size
)
111 NRF51UARTState
*s
= NRF51_UART(opaque
);
113 trace_nrf51_uart_write(addr
, value
, size
);
115 if (!s
->enabled
&& (addr
!= A_UART_ENABLE
)) {
121 if (!s
->pending_tx_byte
&& s
->tx_started
) {
122 s
->reg
[R_UART_TXD
] = value
;
123 s
->pending_tx_byte
= true;
124 uart_transmit(NULL
, G_IO_OUT
, s
);
128 s
->reg
[R_UART_INTEN
] = value
;
130 case A_UART_INTENSET
:
131 s
->reg
[R_UART_INTEN
] |= value
;
133 case A_UART_INTENCLR
:
134 s
->reg
[R_UART_INTEN
] &= ~value
;
136 case A_UART_TXDRDY
... A_UART_RXTO
:
137 s
->reg
[addr
/ 4] = value
;
139 case A_UART_ERRORSRC
:
140 s
->reg
[addr
/ 4] &= ~value
;
146 s
->reg
[R_UART_RXDRDY
] = 0;
151 s
->tx_started
= true;
156 s
->rx_started
= true;
172 s
->tx_started
= false;
176 if (addr
!= A_UART_STOPTX
&& value
== 1) {
177 s
->rx_started
= false;
178 s
->reg
[R_UART_RXTO
] = 1;
182 s
->reg
[addr
/ 4] = value
;
185 nrf51_uart_update_irq(s
);
188 static const MemoryRegionOps uart_ops
= {
191 .endianness
= DEVICE_LITTLE_ENDIAN
,
194 static void nrf51_uart_reset(DeviceState
*dev
)
196 NRF51UARTState
*s
= NRF51_UART(dev
);
198 s
->pending_tx_byte
= 0;
200 uart_cancel_transmit(s
);
202 memset(s
->reg
, 0, sizeof(s
->reg
));
204 s
->reg
[R_UART_PSELRTS
] = 0xFFFFFFFF;
205 s
->reg
[R_UART_PSELTXD
] = 0xFFFFFFFF;
206 s
->reg
[R_UART_PSELCTS
] = 0xFFFFFFFF;
207 s
->reg
[R_UART_PSELRXD
] = 0xFFFFFFFF;
208 s
->reg
[R_UART_BAUDRATE
] = 0x4000000;
212 s
->rx_started
= false;
213 s
->tx_started
= false;
217 static void uart_receive(void *opaque
, const uint8_t *buf
, int size
)
220 NRF51UARTState
*s
= NRF51_UART(opaque
);
223 if (size
== 0 || s
->rx_fifo_len
>= UART_FIFO_LENGTH
) {
227 for (i
= 0; i
< size
; i
++) {
228 uint32_t pos
= (s
->rx_fifo_pos
+ s
->rx_fifo_len
) % UART_FIFO_LENGTH
;
229 s
->rx_fifo
[pos
] = buf
[i
];
233 s
->reg
[R_UART_RXDRDY
] = 1;
234 nrf51_uart_update_irq(s
);
237 static int uart_can_receive(void *opaque
)
239 NRF51UARTState
*s
= NRF51_UART(opaque
);
241 return s
->rx_started
? (UART_FIFO_LENGTH
- s
->rx_fifo_len
) : 0;
244 static void uart_event(void *opaque
, int event
)
246 NRF51UARTState
*s
= NRF51_UART(opaque
);
248 if (event
== CHR_EVENT_BREAK
) {
249 s
->reg
[R_UART_ERRORSRC
] |= 3;
250 s
->reg
[R_UART_ERROR
] = 1;
251 nrf51_uart_update_irq(s
);
255 static void nrf51_uart_realize(DeviceState
*dev
, Error
**errp
)
257 NRF51UARTState
*s
= NRF51_UART(dev
);
259 qemu_chr_fe_set_handlers(&s
->chr
, uart_can_receive
, uart_receive
,
260 uart_event
, NULL
, s
, NULL
, true);
263 static void nrf51_uart_init(Object
*obj
)
265 NRF51UARTState
*s
= NRF51_UART(obj
);
266 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
268 memory_region_init_io(&s
->iomem
, obj
, &uart_ops
, s
,
269 "nrf51_soc.uart", UART_SIZE
);
270 sysbus_init_mmio(sbd
, &s
->iomem
);
271 sysbus_init_irq(sbd
, &s
->irq
);
274 static int nrf51_uart_post_load(void *opaque
, int version_id
)
276 NRF51UARTState
*s
= NRF51_UART(opaque
);
278 if (s
->pending_tx_byte
) {
279 s
->watch_tag
= qemu_chr_fe_add_watch(&s
->chr
, G_IO_OUT
| G_IO_HUP
,
286 static const VMStateDescription nrf51_uart_vmstate
= {
287 .name
= "nrf51_soc.uart",
288 .post_load
= nrf51_uart_post_load
,
289 .fields
= (VMStateField
[]) {
290 VMSTATE_UINT32_ARRAY(reg
, NRF51UARTState
, 0x56C),
291 VMSTATE_UINT8_ARRAY(rx_fifo
, NRF51UARTState
, UART_FIFO_LENGTH
),
292 VMSTATE_UINT32(rx_fifo_pos
, NRF51UARTState
),
293 VMSTATE_UINT32(rx_fifo_len
, NRF51UARTState
),
294 VMSTATE_BOOL(rx_started
, NRF51UARTState
),
295 VMSTATE_BOOL(tx_started
, NRF51UARTState
),
296 VMSTATE_BOOL(pending_tx_byte
, NRF51UARTState
),
297 VMSTATE_BOOL(enabled
, NRF51UARTState
),
298 VMSTATE_END_OF_LIST()
302 static Property nrf51_uart_properties
[] = {
303 DEFINE_PROP_CHR("chardev", NRF51UARTState
, chr
),
304 DEFINE_PROP_END_OF_LIST(),
307 static void nrf51_uart_class_init(ObjectClass
*klass
, void *data
)
309 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 dc
->reset
= nrf51_uart_reset
;
312 dc
->realize
= nrf51_uart_realize
;
313 dc
->props
= nrf51_uart_properties
;
314 dc
->vmsd
= &nrf51_uart_vmstate
;
317 static const TypeInfo nrf51_uart_info
= {
318 .name
= TYPE_NRF51_UART
,
319 .parent
= TYPE_SYS_BUS_DEVICE
,
320 .instance_size
= sizeof(NRF51UARTState
),
321 .instance_init
= nrf51_uart_init
,
322 .class_init
= nrf51_uart_class_init
325 static void nrf51_uart_register_types(void)
327 type_register_static(&nrf51_uart_info
);
330 type_init(nrf51_uart_register_types
)