2 * BCM2835 (Raspberry Pi / Pi 2) Aux block (mini UART and SPI).
3 * Copyright (c) 2015, Microsoft
4 * Written by Andrew Baumann
5 * Based on pl011.c, copyright terms below:
7 * Arm PrimeCell PL011 UART
9 * Copyright (c) 2006 CodeSourcery.
10 * Written by Paul Brook
12 * This code is licensed under the GPL.
14 * At present only the core UART functions (data path for tx/rx) are
15 * implemented. The following features/registers are unimplemented:
16 * - Line/modem control
23 #include "qemu/osdep.h"
24 #include "hw/char/bcm2835_aux.h"
27 #define AUX_ENABLES 0x4
28 #define AUX_MU_IO_REG 0x40
29 #define AUX_MU_IER_REG 0x44
30 #define AUX_MU_IIR_REG 0x48
31 #define AUX_MU_LCR_REG 0x4c
32 #define AUX_MU_MCR_REG 0x50
33 #define AUX_MU_LSR_REG 0x54
34 #define AUX_MU_MSR_REG 0x58
35 #define AUX_MU_SCRATCH 0x5c
36 #define AUX_MU_CNTL_REG 0x60
37 #define AUX_MU_STAT_REG 0x64
38 #define AUX_MU_BAUD_REG 0x68
40 /* bits in IER/IIR registers */
44 static void bcm2835_aux_update(BCM2835AuxState
*s
)
46 /* signal an interrupt if either:
47 * 1. rx interrupt is enabled and we have a non-empty rx fifo, or
48 * 2. the tx interrupt is enabled (since we instantly drain the tx fifo)
51 if ((s
->ier
& RX_INT
) && s
->read_count
!= 0) {
54 if (s
->ier
& TX_INT
) {
57 qemu_set_irq(s
->irq
, s
->iir
!= 0);
60 static uint64_t bcm2835_aux_read(void *opaque
, hwaddr offset
, unsigned size
)
62 BCM2835AuxState
*s
= opaque
;
70 return 1; /* mini UART permanently enabled */
73 /* "DLAB bit set means access baudrate register" is NYI */
74 c
= s
->read_fifo
[s
->read_pos
];
75 if (s
->read_count
> 0) {
77 if (++s
->read_pos
== BCM2835_AUX_RX_FIFO_LEN
) {
82 qemu_chr_accept_input(s
->chr
);
84 bcm2835_aux_update(s
);
88 /* "DLAB bit set means access baudrate register" is NYI */
89 return 0xc0 | s
->ier
; /* FIFO enables always read 1 */
92 res
= 0xc0; /* FIFO enables */
93 /* The spec is unclear on what happens when both tx and rx
94 * interrupts are active, besides that this cannot occur. At
95 * present, we choose to prioritise the rx interrupt, since
96 * the tx fifo is always empty. */
97 if (s
->read_count
!= 0) {
108 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_LCR_REG unsupported\n", __func__
);
112 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_MCR_REG unsupported\n", __func__
);
116 res
= 0x60; /* tx idle, empty */
117 if (s
->read_count
!= 0) {
123 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_MSR_REG unsupported\n", __func__
);
127 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_SCRATCH unsupported\n", __func__
);
130 case AUX_MU_CNTL_REG
:
131 return 0x3; /* tx, rx enabled */
133 case AUX_MU_STAT_REG
:
134 res
= 0x30e; /* space in the output buffer, empty tx fifo, idle tx/rx */
135 if (s
->read_count
> 0) {
136 res
|= 0x1; /* data in input buffer */
137 assert(s
->read_count
< BCM2835_AUX_RX_FIFO_LEN
);
138 res
|= ((uint32_t)s
->read_count
) << 16; /* rx fifo fill level */
142 case AUX_MU_BAUD_REG
:
143 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_BAUD_REG unsupported\n", __func__
);
147 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
153 static void bcm2835_aux_write(void *opaque
, hwaddr offset
, uint64_t value
,
156 BCM2835AuxState
*s
= opaque
;
162 qemu_log_mask(LOG_UNIMP
, "%s: unsupported attempt to enable SPI "
163 "or disable UART\n", __func__
);
168 /* "DLAB bit set means access baudrate register" is NYI */
171 qemu_chr_fe_write(s
->chr
, &ch
, 1);
176 /* "DLAB bit set means access baudrate register" is NYI */
177 s
->ier
= value
& (TX_INT
| RX_INT
);
178 bcm2835_aux_update(s
);
188 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_LCR_REG unsupported\n", __func__
);
192 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_MCR_REG unsupported\n", __func__
);
196 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_SCRATCH unsupported\n", __func__
);
199 case AUX_MU_CNTL_REG
:
200 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_CNTL_REG unsupported\n", __func__
);
203 case AUX_MU_BAUD_REG
:
204 qemu_log_mask(LOG_UNIMP
, "%s: AUX_MU_BAUD_REG unsupported\n", __func__
);
208 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
212 bcm2835_aux_update(s
);
215 static int bcm2835_aux_can_receive(void *opaque
)
217 BCM2835AuxState
*s
= opaque
;
219 return s
->read_count
< BCM2835_AUX_RX_FIFO_LEN
;
222 static void bcm2835_aux_put_fifo(void *opaque
, uint8_t value
)
224 BCM2835AuxState
*s
= opaque
;
227 slot
= s
->read_pos
+ s
->read_count
;
228 if (slot
>= BCM2835_AUX_RX_FIFO_LEN
) {
229 slot
-= BCM2835_AUX_RX_FIFO_LEN
;
231 s
->read_fifo
[slot
] = value
;
233 if (s
->read_count
== BCM2835_AUX_RX_FIFO_LEN
) {
236 bcm2835_aux_update(s
);
239 static void bcm2835_aux_receive(void *opaque
, const uint8_t *buf
, int size
)
241 bcm2835_aux_put_fifo(opaque
, *buf
);
244 static const MemoryRegionOps bcm2835_aux_ops
= {
245 .read
= bcm2835_aux_read
,
246 .write
= bcm2835_aux_write
,
247 .endianness
= DEVICE_NATIVE_ENDIAN
,
248 .valid
.min_access_size
= 4,
249 .valid
.max_access_size
= 4,
252 static const VMStateDescription vmstate_bcm2835_aux
= {
253 .name
= TYPE_BCM2835_AUX
,
255 .minimum_version_id
= 1,
256 .fields
= (VMStateField
[]) {
257 VMSTATE_UINT8_ARRAY(read_fifo
, BCM2835AuxState
,
258 BCM2835_AUX_RX_FIFO_LEN
),
259 VMSTATE_UINT8(read_pos
, BCM2835AuxState
),
260 VMSTATE_UINT8(read_count
, BCM2835AuxState
),
261 VMSTATE_UINT8(ier
, BCM2835AuxState
),
262 VMSTATE_UINT8(iir
, BCM2835AuxState
),
263 VMSTATE_END_OF_LIST()
267 static void bcm2835_aux_init(Object
*obj
)
269 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
270 BCM2835AuxState
*s
= BCM2835_AUX(obj
);
272 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_aux_ops
, s
,
273 TYPE_BCM2835_AUX
, 0x100);
274 sysbus_init_mmio(sbd
, &s
->iomem
);
275 sysbus_init_irq(sbd
, &s
->irq
);
278 static void bcm2835_aux_realize(DeviceState
*dev
, Error
**errp
)
280 BCM2835AuxState
*s
= BCM2835_AUX(dev
);
283 qemu_chr_add_handlers(s
->chr
, bcm2835_aux_can_receive
,
284 bcm2835_aux_receive
, NULL
, s
);
288 static Property bcm2835_aux_props
[] = {
289 DEFINE_PROP_CHR("chardev", BCM2835AuxState
, chr
),
290 DEFINE_PROP_END_OF_LIST(),
293 static void bcm2835_aux_class_init(ObjectClass
*oc
, void *data
)
295 DeviceClass
*dc
= DEVICE_CLASS(oc
);
297 dc
->realize
= bcm2835_aux_realize
;
298 dc
->vmsd
= &vmstate_bcm2835_aux
;
299 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
300 dc
->props
= bcm2835_aux_props
;
303 static const TypeInfo bcm2835_aux_info
= {
304 .name
= TYPE_BCM2835_AUX
,
305 .parent
= TYPE_SYS_BUS_DEVICE
,
306 .instance_size
= sizeof(BCM2835AuxState
),
307 .instance_init
= bcm2835_aux_init
,
308 .class_init
= bcm2835_aux_class_init
,
311 static void bcm2835_aux_register_types(void)
313 type_register_static(&bcm2835_aux_info
);
316 type_init(bcm2835_aux_register_types
)