2007-09-17 01:08:06 +04:00
|
|
|
/*
|
2006-04-09 05:32:52 +04:00
|
|
|
* Arm PrimeCell PL011 UART
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the GPL.
|
2006-04-09 05:32:52 +04:00
|
|
|
*/
|
|
|
|
|
2019-02-21 21:17:46 +03:00
|
|
|
/*
|
|
|
|
* QEMU interface:
|
|
|
|
* + sysbus MMIO region 0: device registers
|
|
|
|
* + sysbus IRQ 0: UARTINTR (combined interrupt line)
|
|
|
|
* + sysbus IRQ 1: UARTRXINTR (receive FIFO interrupt line)
|
|
|
|
* + sysbus IRQ 2: UARTTXINTR (transmit FIFO interrupt line)
|
|
|
|
* + sysbus IRQ 3: UARTRTINTR (receive timeout interrupt line)
|
|
|
|
* + sysbus IRQ 4: UARTMSINTR (momem status interrupt line)
|
|
|
|
* + sysbus IRQ 5: UARTEINTR (error interrupt line)
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2023-02-20 14:51:08 +03:00
|
|
|
#include "qapi/error.h"
|
2019-02-21 21:17:46 +03:00
|
|
|
#include "hw/char/pl011.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2020-10-10 16:57:58 +03:00
|
|
|
#include "hw/qdev-clock.h"
|
2023-02-20 14:51:08 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2020-12-12 01:05:12 +03:00
|
|
|
#include "hw/qdev-properties-system.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2017-01-26 17:26:44 +03:00
|
|
|
#include "chardev/char-fe.h"
|
2021-08-06 17:47:00 +03:00
|
|
|
#include "chardev/char-serial.h"
|
2015-12-15 15:16:16 +03:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2016-10-12 20:54:36 +03:00
|
|
|
#include "trace.h"
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2023-02-20 14:51:08 +03:00
|
|
|
DeviceState *pl011_create(hwaddr addr, qemu_irq irq, Chardev *chr)
|
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
|
|
|
|
dev = qdev_new("pl011");
|
|
|
|
s = SYS_BUS_DEVICE(dev);
|
|
|
|
qdev_prop_set_chr(dev, "chardev", chr);
|
|
|
|
sysbus_realize_and_unref(s, &error_fatal);
|
|
|
|
sysbus_mmio_map(s, 0, addr);
|
|
|
|
sysbus_connect_irq(s, 0, irq);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2023-05-22 11:31:40 +03:00
|
|
|
/* Flag Register, UARTFR */
|
2024-02-27 08:48:55 +03:00
|
|
|
#define PL011_FLAG_RI 0x100
|
2006-04-09 05:32:52 +04:00
|
|
|
#define PL011_FLAG_TXFE 0x80
|
|
|
|
#define PL011_FLAG_RXFF 0x40
|
|
|
|
#define PL011_FLAG_TXFF 0x20
|
|
|
|
#define PL011_FLAG_RXFE 0x10
|
2024-02-27 08:48:55 +03:00
|
|
|
#define PL011_FLAG_DCD 0x04
|
|
|
|
#define PL011_FLAG_DSR 0x02
|
|
|
|
#define PL011_FLAG_CTS 0x01
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2023-05-22 11:37:30 +03:00
|
|
|
/* Data Register, UARTDR */
|
|
|
|
#define DR_BE (1 << 10)
|
|
|
|
|
2019-02-21 21:17:46 +03:00
|
|
|
/* Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC */
|
|
|
|
#define INT_OE (1 << 10)
|
|
|
|
#define INT_BE (1 << 9)
|
|
|
|
#define INT_PE (1 << 8)
|
|
|
|
#define INT_FE (1 << 7)
|
|
|
|
#define INT_RT (1 << 6)
|
|
|
|
#define INT_TX (1 << 5)
|
|
|
|
#define INT_RX (1 << 4)
|
|
|
|
#define INT_DSR (1 << 3)
|
|
|
|
#define INT_DCD (1 << 2)
|
|
|
|
#define INT_CTS (1 << 1)
|
|
|
|
#define INT_RI (1 << 0)
|
|
|
|
#define INT_E (INT_OE | INT_BE | INT_PE | INT_FE)
|
|
|
|
#define INT_MS (INT_RI | INT_DSR | INT_DCD | INT_CTS)
|
|
|
|
|
2023-05-22 11:37:30 +03:00
|
|
|
/* Line Control Register, UARTLCR_H */
|
|
|
|
#define LCR_FEN (1 << 4)
|
|
|
|
#define LCR_BRK (1 << 0)
|
|
|
|
|
2024-02-27 08:48:55 +03:00
|
|
|
/* Control Register, UARTCR */
|
|
|
|
#define CR_OUT2 (1 << 13)
|
|
|
|
#define CR_OUT1 (1 << 12)
|
|
|
|
#define CR_RTS (1 << 11)
|
|
|
|
#define CR_DTR (1 << 10)
|
2024-07-18 18:23:42 +03:00
|
|
|
#define CR_TXE (1 << 8)
|
2024-02-27 08:48:55 +03:00
|
|
|
#define CR_LBE (1 << 7)
|
2024-07-18 18:23:42 +03:00
|
|
|
#define CR_UARTEN (1 << 0)
|
2024-02-27 08:48:55 +03:00
|
|
|
|
2024-07-02 18:57:52 +03:00
|
|
|
/* Integer Baud Rate Divider, UARTIBRD */
|
|
|
|
#define IBRD_MASK 0x3f
|
|
|
|
|
|
|
|
/* Fractional Baud Rate Divider, UARTFBRD */
|
|
|
|
#define FBRD_MASK 0xffff
|
|
|
|
|
2009-05-15 01:35:07 +04:00
|
|
|
static const unsigned char pl011_id_arm[8] =
|
|
|
|
{ 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
|
|
|
|
static const unsigned char pl011_id_luminary[8] =
|
|
|
|
{ 0x11, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2023-05-22 11:31:40 +03:00
|
|
|
static const char *pl011_regname(hwaddr offset)
|
|
|
|
{
|
|
|
|
static const char *const rname[] = {
|
|
|
|
[0] = "DR", [1] = "RSR", [6] = "FR", [8] = "ILPR", [9] = "IBRD",
|
|
|
|
[10] = "FBRD", [11] = "LCRH", [12] = "CR", [13] = "IFLS", [14] = "IMSC",
|
|
|
|
[15] = "RIS", [16] = "MIS", [17] = "ICR", [18] = "DMACR",
|
|
|
|
};
|
|
|
|
unsigned idx = offset >> 2;
|
|
|
|
|
|
|
|
if (idx < ARRAY_SIZE(rname) && rname[idx]) {
|
|
|
|
return rname[idx];
|
|
|
|
}
|
|
|
|
if (idx >= 0x3f8 && idx <= 0x400) {
|
|
|
|
return "ID";
|
|
|
|
}
|
|
|
|
return "UNKN";
|
|
|
|
}
|
|
|
|
|
2019-02-21 21:17:46 +03:00
|
|
|
/* Which bits in the interrupt status matter for each outbound IRQ line ? */
|
|
|
|
static const uint32_t irqmask[] = {
|
|
|
|
INT_E | INT_MS | INT_RT | INT_TX | INT_RX, /* combined IRQ */
|
|
|
|
INT_RX,
|
|
|
|
INT_TX,
|
|
|
|
INT_RT,
|
|
|
|
INT_MS,
|
|
|
|
INT_E,
|
|
|
|
};
|
|
|
|
|
2013-07-25 01:13:57 +04:00
|
|
|
static void pl011_update(PL011State *s)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
uint32_t flags;
|
2019-02-21 21:17:46 +03:00
|
|
|
int i;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2006-04-09 05:32:52 +04:00
|
|
|
flags = s->int_level & s->int_enabled;
|
2016-10-12 20:54:36 +03:00
|
|
|
trace_pl011_irq_state(flags != 0);
|
2019-02-21 21:17:46 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
|
|
|
|
qemu_set_irq(s->irq[i], (flags & irqmask[i]) != 0);
|
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2024-07-18 18:47:28 +03:00
|
|
|
static bool pl011_loopback_enabled(PL011State *s)
|
|
|
|
{
|
|
|
|
return !!(s->cr & CR_LBE);
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:23:00 +03:00
|
|
|
static bool pl011_is_fifo_enabled(PL011State *s)
|
|
|
|
{
|
2023-05-22 11:37:30 +03:00
|
|
|
return (s->lcr & LCR_FEN) != 0;
|
2023-01-23 19:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned pl011_get_fifo_depth(PL011State *s)
|
|
|
|
{
|
|
|
|
/* Note: FIFO depth is expected to be power-of-2 */
|
|
|
|
return pl011_is_fifo_enabled(s) ? PL011_FIFO_DEPTH : 1;
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:52:06 +03:00
|
|
|
static inline void pl011_reset_rx_fifo(PL011State *s)
|
2023-01-23 19:23:03 +03:00
|
|
|
{
|
|
|
|
s->read_count = 0;
|
|
|
|
s->read_pos = 0;
|
|
|
|
|
|
|
|
/* Reset FIFO flags */
|
2023-05-22 16:52:06 +03:00
|
|
|
s->flags &= ~PL011_FLAG_RXFF;
|
|
|
|
s->flags |= PL011_FLAG_RXFE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void pl011_reset_tx_fifo(PL011State *s)
|
|
|
|
{
|
|
|
|
/* Reset FIFO flags */
|
|
|
|
s->flags &= ~PL011_FLAG_TXFF;
|
|
|
|
s->flags |= PL011_FLAG_TXFE;
|
2023-01-23 19:23:03 +03:00
|
|
|
}
|
|
|
|
|
2024-07-18 18:31:30 +03:00
|
|
|
static void pl011_fifo_rx_put(void *opaque, uint32_t value)
|
2024-07-18 18:21:46 +03:00
|
|
|
{
|
|
|
|
PL011State *s = (PL011State *)opaque;
|
|
|
|
int slot;
|
|
|
|
unsigned pipe_depth;
|
|
|
|
|
|
|
|
pipe_depth = pl011_get_fifo_depth(s);
|
|
|
|
slot = (s->read_pos + s->read_count) & (pipe_depth - 1);
|
|
|
|
s->read_fifo[slot] = value;
|
|
|
|
s->read_count++;
|
|
|
|
s->flags &= ~PL011_FLAG_RXFE;
|
2024-07-18 18:31:30 +03:00
|
|
|
trace_pl011_fifo_rx_put(value, s->read_count);
|
2024-07-18 18:21:46 +03:00
|
|
|
if (s->read_count == pipe_depth) {
|
2024-07-18 18:31:30 +03:00
|
|
|
trace_pl011_fifo_rx_full();
|
2024-07-18 18:21:46 +03:00
|
|
|
s->flags |= PL011_FLAG_RXFF;
|
|
|
|
}
|
|
|
|
if (s->read_count == s->read_trigger) {
|
|
|
|
s->int_level |= INT_RX;
|
|
|
|
pl011_update(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:47:28 +03:00
|
|
|
static void pl011_loopback_tx(PL011State *s, uint32_t value)
|
|
|
|
{
|
|
|
|
if (!pl011_loopback_enabled(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Caveat:
|
|
|
|
*
|
|
|
|
* In real hardware, TX loopback happens at the serial-bit level
|
|
|
|
* and then reassembled by the RX logics back into bytes and placed
|
|
|
|
* into the RX fifo. That is, loopback happens after TX fifo.
|
|
|
|
*
|
|
|
|
* Because the real hardware TX fifo is time-drained at the frame
|
|
|
|
* rate governed by the configured serial format, some loopback
|
|
|
|
* bytes in TX fifo may still be able to get into the RX fifo
|
|
|
|
* that could be full at times while being drained at software
|
|
|
|
* pace.
|
|
|
|
*
|
|
|
|
* In such scenario, the RX draining pace is the major factor
|
|
|
|
* deciding which loopback bytes get into the RX fifo, unless
|
|
|
|
* hardware flow-control is enabled.
|
|
|
|
*
|
|
|
|
* For simplicity, the above described is not emulated.
|
|
|
|
*/
|
2024-07-18 18:31:30 +03:00
|
|
|
pl011_fifo_rx_put(s, value);
|
2024-07-18 18:47:28 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 11:41:04 +03:00
|
|
|
static void pl011_write_txdata(PL011State *s, uint8_t data)
|
|
|
|
{
|
2024-07-18 18:23:42 +03:00
|
|
|
if (!(s->cr & CR_UARTEN)) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"PL011 data written to disabled UART\n");
|
|
|
|
}
|
|
|
|
if (!(s->cr & CR_TXE)) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"PL011 data written to disabled TX UART\n");
|
|
|
|
}
|
2023-05-22 11:41:04 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX this blocks entire thread. Rewrite to use
|
|
|
|
* qemu_chr_fe_write and background I/O callbacks
|
|
|
|
*/
|
|
|
|
qemu_chr_fe_write_all(&s->chr, &data, 1);
|
|
|
|
pl011_loopback_tx(s, data);
|
|
|
|
s->int_level |= INT_TX;
|
|
|
|
pl011_update(s);
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:41:44 +03:00
|
|
|
static uint32_t pl011_read_rxdata(PL011State *s)
|
|
|
|
{
|
|
|
|
uint32_t c;
|
|
|
|
|
|
|
|
s->flags &= ~PL011_FLAG_RXFF;
|
|
|
|
c = s->read_fifo[s->read_pos];
|
|
|
|
if (s->read_count > 0) {
|
|
|
|
s->read_count--;
|
|
|
|
s->read_pos = (s->read_pos + 1) & (pl011_get_fifo_depth(s) - 1);
|
|
|
|
}
|
|
|
|
if (s->read_count == 0) {
|
|
|
|
s->flags |= PL011_FLAG_RXFE;
|
|
|
|
}
|
|
|
|
if (s->read_count == s->read_trigger - 1) {
|
|
|
|
s->int_level &= ~INT_RX;
|
|
|
|
}
|
|
|
|
trace_pl011_read_fifo(s->read_count);
|
|
|
|
s->rsr = c >> 8;
|
|
|
|
pl011_update(s);
|
|
|
|
qemu_chr_fe_accept_input(&s->chr);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t pl011_read(void *opaque, hwaddr offset,
|
2011-10-10 19:08:49 +04:00
|
|
|
unsigned size)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-25 01:13:57 +04:00
|
|
|
PL011State *s = (PL011State *)opaque;
|
2016-10-12 20:54:36 +03:00
|
|
|
uint64_t r;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* UARTDR */
|
2024-07-18 18:41:44 +03:00
|
|
|
r = pl011_read_rxdata(s);
|
2016-10-12 20:54:36 +03:00
|
|
|
break;
|
2014-03-18 22:18:40 +04:00
|
|
|
case 1: /* UARTRSR */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->rsr;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 6: /* UARTFR */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->flags;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 8: /* UARTILPR */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->ilpr;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 9: /* UARTIBRD */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->ibrd;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 10: /* UARTFBRD */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->fbrd;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 11: /* UARTLCR_H */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->lcr;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 12: /* UARTCR */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->cr;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 13: /* UARTIFLS */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->ifl;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 14: /* UARTIMSC */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->int_enabled;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 15: /* UARTRIS */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->int_level;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 16: /* UARTMIS */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->int_level & s->int_enabled;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
case 18: /* UARTDMACR */
|
2016-10-12 20:54:36 +03:00
|
|
|
r = s->dmacr;
|
|
|
|
break;
|
|
|
|
case 0x3f8 ... 0x400:
|
|
|
|
r = s->id[(offset - 0xfe0) >> 2];
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
default:
|
2012-10-18 17:11:40 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
2019-02-21 21:17:46 +03:00
|
|
|
"pl011_read: Bad offset 0x%x\n", (int)offset);
|
2016-10-12 20:54:36 +03:00
|
|
|
r = 0;
|
|
|
|
break;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
2016-10-12 20:54:36 +03:00
|
|
|
|
2023-05-22 11:31:40 +03:00
|
|
|
trace_pl011_read(offset, r, pl011_regname(offset));
|
2016-10-12 20:54:36 +03:00
|
|
|
return r;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:13:57 +04:00
|
|
|
static void pl011_set_read_trigger(PL011State *s)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
/* The docs say the RX interrupt is triggered when the FIFO exceeds
|
|
|
|
the threshold. However linux only reads the FIFO in response to an
|
|
|
|
interrupt. Triggering the interrupt when the FIFO is non-empty seems
|
|
|
|
to make things work. */
|
2023-05-22 11:37:30 +03:00
|
|
|
if (s->lcr & LCR_FEN)
|
2006-04-09 05:32:52 +04:00
|
|
|
s->read_trigger = (s->ifl >> 1) & 0x1c;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
s->read_trigger = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:57:58 +03:00
|
|
|
static unsigned int pl011_get_baudrate(const PL011State *s)
|
|
|
|
{
|
|
|
|
uint64_t clk;
|
|
|
|
|
2022-10-06 13:19:48 +03:00
|
|
|
if (s->ibrd == 0) {
|
2020-10-10 16:57:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
clk = clock_get_hz(s->clk);
|
|
|
|
return (clk / ((s->ibrd << 6) + s->fbrd)) << 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl011_trace_baudrate_change(const PL011State *s)
|
|
|
|
{
|
|
|
|
trace_pl011_baudrate_change(pl011_get_baudrate(s),
|
|
|
|
clock_get_hz(s->clk),
|
|
|
|
s->ibrd, s->fbrd);
|
|
|
|
}
|
|
|
|
|
2024-02-27 08:48:55 +03:00
|
|
|
static void pl011_loopback_mdmctrl(PL011State *s)
|
|
|
|
{
|
|
|
|
uint32_t cr, fr, il;
|
|
|
|
|
|
|
|
if (!pl011_loopback_enabled(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loopback software-driven modem control outputs to modem status inputs:
|
|
|
|
* FR.RI <= CR.Out2
|
|
|
|
* FR.DCD <= CR.Out1
|
|
|
|
* FR.CTS <= CR.RTS
|
|
|
|
* FR.DSR <= CR.DTR
|
|
|
|
*
|
|
|
|
* The loopback happens immediately even if this call is triggered
|
|
|
|
* by setting only CR.LBE.
|
|
|
|
*
|
|
|
|
* CTS/RTS updates due to enabled hardware flow controls are not
|
|
|
|
* dealt with here.
|
|
|
|
*/
|
|
|
|
cr = s->cr;
|
|
|
|
fr = s->flags & ~(PL011_FLAG_RI | PL011_FLAG_DCD |
|
|
|
|
PL011_FLAG_DSR | PL011_FLAG_CTS);
|
|
|
|
fr |= (cr & CR_OUT2) ? PL011_FLAG_RI : 0;
|
|
|
|
fr |= (cr & CR_OUT1) ? PL011_FLAG_DCD : 0;
|
|
|
|
fr |= (cr & CR_RTS) ? PL011_FLAG_CTS : 0;
|
|
|
|
fr |= (cr & CR_DTR) ? PL011_FLAG_DSR : 0;
|
|
|
|
|
|
|
|
/* Change interrupts based on updated FR */
|
|
|
|
il = s->int_level & ~(INT_DSR | INT_DCD | INT_CTS | INT_RI);
|
|
|
|
il |= (fr & PL011_FLAG_DSR) ? INT_DSR : 0;
|
|
|
|
il |= (fr & PL011_FLAG_DCD) ? INT_DCD : 0;
|
|
|
|
il |= (fr & PL011_FLAG_CTS) ? INT_CTS : 0;
|
|
|
|
il |= (fr & PL011_FLAG_RI) ? INT_RI : 0;
|
|
|
|
|
|
|
|
s->flags = fr;
|
|
|
|
s->int_level = il;
|
|
|
|
pl011_update(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl011_loopback_break(PL011State *s, int brk_enable)
|
|
|
|
{
|
|
|
|
if (brk_enable) {
|
|
|
|
pl011_loopback_tx(s, DR_BE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pl011_write(void *opaque, hwaddr offset,
|
2011-10-10 19:08:49 +04:00
|
|
|
uint64_t value, unsigned size)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-25 01:13:57 +04:00
|
|
|
PL011State *s = (PL011State *)opaque;
|
2006-04-09 05:32:52 +04:00
|
|
|
unsigned char ch;
|
|
|
|
|
2023-05-22 11:31:40 +03:00
|
|
|
trace_pl011_write(offset, value, pl011_regname(offset));
|
2016-10-12 20:54:36 +03:00
|
|
|
|
2006-04-09 05:32:52 +04:00
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* UARTDR */
|
|
|
|
ch = value;
|
2023-05-22 11:41:04 +03:00
|
|
|
pl011_write_txdata(s, ch);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
2014-03-18 22:18:40 +04:00
|
|
|
case 1: /* UARTRSR/UARTECR */
|
|
|
|
s->rsr = 0;
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
2007-11-11 03:04:49 +03:00
|
|
|
case 6: /* UARTFR */
|
|
|
|
/* Writes to Flag register are ignored. */
|
|
|
|
break;
|
2023-05-22 11:31:40 +03:00
|
|
|
case 8: /* UARTILPR */
|
2006-04-09 05:32:52 +04:00
|
|
|
s->ilpr = value;
|
|
|
|
break;
|
|
|
|
case 9: /* UARTIBRD */
|
2024-07-02 18:57:52 +03:00
|
|
|
s->ibrd = value & IBRD_MASK;
|
2020-10-10 16:57:58 +03:00
|
|
|
pl011_trace_baudrate_change(s);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
case 10: /* UARTFBRD */
|
2024-07-02 18:57:52 +03:00
|
|
|
s->fbrd = value & FBRD_MASK;
|
2020-10-10 16:57:58 +03:00
|
|
|
pl011_trace_baudrate_change(s);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
case 11: /* UARTLCR_H */
|
2014-03-18 22:18:39 +04:00
|
|
|
/* Reset the FIFO state on FIFO enable or disable */
|
2023-05-22 11:37:30 +03:00
|
|
|
if ((s->lcr ^ value) & LCR_FEN) {
|
2023-05-22 16:52:06 +03:00
|
|
|
pl011_reset_rx_fifo(s);
|
|
|
|
pl011_reset_tx_fifo(s);
|
2014-03-18 22:18:39 +04:00
|
|
|
}
|
2023-05-22 11:37:30 +03:00
|
|
|
if ((s->lcr ^ value) & LCR_BRK) {
|
|
|
|
int break_enable = value & LCR_BRK;
|
2021-08-06 17:47:00 +03:00
|
|
|
qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
|
|
|
|
&break_enable);
|
2024-02-27 08:48:55 +03:00
|
|
|
pl011_loopback_break(s, break_enable);
|
2021-08-06 17:47:00 +03:00
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
s->lcr = value;
|
|
|
|
pl011_set_read_trigger(s);
|
|
|
|
break;
|
|
|
|
case 12: /* UARTCR */
|
2024-02-27 08:48:55 +03:00
|
|
|
/* ??? Need to implement the enable bit. */
|
2006-04-09 05:32:52 +04:00
|
|
|
s->cr = value;
|
2024-02-27 08:48:55 +03:00
|
|
|
pl011_loopback_mdmctrl(s);
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
case 13: /* UARTIFS */
|
|
|
|
s->ifl = value;
|
|
|
|
pl011_set_read_trigger(s);
|
|
|
|
break;
|
|
|
|
case 14: /* UARTIMSC */
|
|
|
|
s->int_enabled = value;
|
|
|
|
pl011_update(s);
|
|
|
|
break;
|
|
|
|
case 17: /* UARTICR */
|
|
|
|
s->int_level &= ~value;
|
|
|
|
pl011_update(s);
|
|
|
|
break;
|
|
|
|
case 18: /* UARTDMACR */
|
|
|
|
s->dmacr = value;
|
2012-10-18 17:11:40 +04:00
|
|
|
if (value & 3) {
|
|
|
|
qemu_log_mask(LOG_UNIMP, "pl011: DMA not implemented\n");
|
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-18 17:11:40 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
2019-02-21 21:17:46 +03:00
|
|
|
"pl011_write: Bad offset 0x%x\n", (int)offset);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-12 02:48:58 +04:00
|
|
|
static int pl011_can_receive(void *opaque)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-25 01:13:57 +04:00
|
|
|
PL011State *s = (PL011State *)opaque;
|
2016-10-12 20:54:36 +03:00
|
|
|
int r;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2023-01-23 19:23:00 +03:00
|
|
|
r = s->read_count < pl011_get_fifo_depth(s);
|
2016-10-12 20:54:36 +03:00
|
|
|
trace_pl011_can_receive(s->lcr, s->read_count, r);
|
|
|
|
return r;
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2008-04-08 23:51:43 +04:00
|
|
|
static void pl011_receive(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
2024-02-27 08:48:55 +03:00
|
|
|
/*
|
|
|
|
* In loopback mode, the RX input signal is internally disconnected
|
|
|
|
* from the entire receiving logics; thus, all inputs are ignored,
|
|
|
|
* and BREAK detection on RX input signal is also not performed.
|
|
|
|
*/
|
|
|
|
if (pl011_loopback_enabled(opaque)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 18:31:30 +03:00
|
|
|
pl011_fifo_rx_put(opaque, *buf);
|
2008-04-08 23:51:43 +04:00
|
|
|
}
|
|
|
|
|
chardev: Use QEMUChrEvent enum in IOEventHandler typedef
The Chardev events are listed in the QEMUChrEvent enum.
By using the enum in the IOEventHandler typedef we:
- make the IOEventHandler type more explicit (this handler
process out-of-band information, while the IOReadHandler
is in-band),
- help static code analyzers.
This patch was produced with the following spatch script:
@match@
expression backend, opaque, context, set_open;
identifier fd_can_read, fd_read, fd_event, be_change;
@@
qemu_chr_fe_set_handlers(backend, fd_can_read, fd_read, fd_event,
be_change, opaque, context, set_open);
@depends on match@
identifier opaque, event;
identifier match.fd_event;
@@
static
-void fd_event(void *opaque, int event)
+void fd_event(void *opaque, QEMUChrEvent event)
{
...
}
Then the typedef was modified manually in
include/chardev/char-fe.h.
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Acked-by: Corey Minyard <cminyard@mvista.com>
Acked-by: Cornelia Huck <cohuck@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20191218172009.8868-15-philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-12-18 20:20:09 +03:00
|
|
|
static void pl011_event(void *opaque, QEMUChrEvent event)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2024-02-27 08:48:55 +03:00
|
|
|
if (event == CHR_EVENT_BREAK && !pl011_loopback_enabled(opaque)) {
|
2024-07-18 18:31:30 +03:00
|
|
|
pl011_fifo_rx_put(opaque, DR_BE);
|
2023-05-22 11:37:30 +03:00
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:45:34 +03:00
|
|
|
static void pl011_clock_update(void *opaque, ClockEvent event)
|
2020-10-10 16:57:58 +03:00
|
|
|
{
|
|
|
|
PL011State *s = PL011(opaque);
|
|
|
|
|
|
|
|
pl011_trace_baudrate_change(s);
|
|
|
|
}
|
|
|
|
|
2011-10-10 19:08:49 +04:00
|
|
|
static const MemoryRegionOps pl011_ops = {
|
|
|
|
.read = pl011_read,
|
|
|
|
.write = pl011_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2023-07-10 18:55:56 +03:00
|
|
|
.impl.min_access_size = 4,
|
|
|
|
.impl.max_access_size = 4,
|
2006-04-09 05:32:52 +04:00
|
|
|
};
|
|
|
|
|
2021-03-18 05:38:01 +03:00
|
|
|
static bool pl011_clock_needed(void *opaque)
|
|
|
|
{
|
|
|
|
PL011State *s = PL011(opaque);
|
|
|
|
|
|
|
|
return s->migrate_clk;
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:57:58 +03:00
|
|
|
static const VMStateDescription vmstate_pl011_clock = {
|
|
|
|
.name = "pl011/clock",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2021-03-18 05:38:01 +03:00
|
|
|
.needed = pl011_clock_needed,
|
2023-12-21 06:16:06 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2020-10-10 16:57:58 +03:00
|
|
|
VMSTATE_CLOCK(clk, PL011State),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-23 19:23:01 +03:00
|
|
|
static int pl011_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
PL011State* s = opaque;
|
|
|
|
|
|
|
|
/* Sanity-check input state */
|
|
|
|
if (s->read_pos >= ARRAY_SIZE(s->read_fifo) ||
|
|
|
|
s->read_count > ARRAY_SIZE(s->read_fifo)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pl011_is_fifo_enabled(s) && s->read_count > 0 && s->read_pos > 0) {
|
|
|
|
/*
|
|
|
|
* Older versions of PL011 didn't ensure that the single
|
|
|
|
* character in the FIFO in FIFO-disabled mode is in
|
|
|
|
* element 0 of the array; convert to follow the current
|
|
|
|
* code's assumptions.
|
|
|
|
*/
|
|
|
|
s->read_fifo[0] = s->read_fifo[s->read_pos];
|
|
|
|
s->read_pos = 0;
|
|
|
|
}
|
|
|
|
|
2024-07-02 18:57:52 +03:00
|
|
|
s->ibrd &= IBRD_MASK;
|
|
|
|
s->fbrd &= FBRD_MASK;
|
|
|
|
|
2023-01-23 19:23:01 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-02 03:50:33 +03:00
|
|
|
static const VMStateDescription vmstate_pl011 = {
|
|
|
|
.name = "pl011",
|
2014-03-18 22:18:40 +04:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2023-01-23 19:23:01 +03:00
|
|
|
.post_load = pl011_post_load,
|
2023-12-21 06:16:06 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2023-11-10 09:21:20 +03:00
|
|
|
VMSTATE_UNUSED(sizeof(uint32_t)),
|
2013-07-25 01:13:57 +04:00
|
|
|
VMSTATE_UINT32(flags, PL011State),
|
|
|
|
VMSTATE_UINT32(lcr, PL011State),
|
2014-03-18 22:18:40 +04:00
|
|
|
VMSTATE_UINT32(rsr, PL011State),
|
2013-07-25 01:13:57 +04:00
|
|
|
VMSTATE_UINT32(cr, PL011State),
|
|
|
|
VMSTATE_UINT32(dmacr, PL011State),
|
|
|
|
VMSTATE_UINT32(int_enabled, PL011State),
|
|
|
|
VMSTATE_UINT32(int_level, PL011State),
|
2023-01-23 19:23:00 +03:00
|
|
|
VMSTATE_UINT32_ARRAY(read_fifo, PL011State, PL011_FIFO_DEPTH),
|
2013-07-25 01:13:57 +04:00
|
|
|
VMSTATE_UINT32(ilpr, PL011State),
|
|
|
|
VMSTATE_UINT32(ibrd, PL011State),
|
|
|
|
VMSTATE_UINT32(fbrd, PL011State),
|
|
|
|
VMSTATE_UINT32(ifl, PL011State),
|
|
|
|
VMSTATE_INT32(read_pos, PL011State),
|
|
|
|
VMSTATE_INT32(read_count, PL011State),
|
|
|
|
VMSTATE_INT32(read_trigger, PL011State),
|
2010-12-02 03:50:33 +03:00
|
|
|
VMSTATE_END_OF_LIST()
|
2020-10-10 16:57:58 +03:00
|
|
|
},
|
2023-12-21 06:16:06 +03:00
|
|
|
.subsections = (const VMStateDescription * const []) {
|
2020-10-10 16:57:58 +03:00
|
|
|
&vmstate_pl011_clock,
|
|
|
|
NULL
|
2010-12-02 03:50:33 +03:00
|
|
|
}
|
|
|
|
};
|
2008-07-02 20:48:32 +04:00
|
|
|
|
2016-06-06 18:59:31 +03:00
|
|
|
static Property pl011_properties[] = {
|
|
|
|
DEFINE_PROP_CHR("chardev", PL011State, chr),
|
2021-03-18 05:38:01 +03:00
|
|
|
DEFINE_PROP_BOOL("migrate-clk", PL011State, migrate_clk, true),
|
2016-06-06 18:59:31 +03:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
static void pl011_init(Object *obj)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-25 01:29:17 +04:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
|
|
|
PL011State *s = PL011(obj);
|
2019-02-21 21:17:46 +03:00
|
|
|
int i;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->iomem, OBJECT(s), &pl011_ops, s, "pl011", 0x1000);
|
2013-07-25 01:29:17 +04:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
2019-02-21 21:17:46 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
|
|
|
|
sysbus_init_irq(sbd, &s->irq[i]);
|
|
|
|
}
|
2009-05-15 01:35:07 +04:00
|
|
|
|
2021-02-19 17:45:34 +03:00
|
|
|
s->clk = qdev_init_clock_in(DEVICE(obj), "clk", pl011_clock_update, s,
|
|
|
|
ClockUpdate);
|
2020-10-10 16:57:58 +03:00
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
s->id = pl011_id_arm;
|
2009-05-15 01:35:07 +04:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
static void pl011_realize(DeviceState *dev, Error **errp)
|
2009-05-15 01:35:07 +04:00
|
|
|
{
|
2013-07-25 01:29:17 +04:00
|
|
|
PL011State *s = PL011(dev);
|
|
|
|
|
2016-10-22 12:52:59 +03:00
|
|
|
qemu_chr_fe_set_handlers(&s->chr, pl011_can_receive, pl011_receive,
|
2017-07-06 15:08:49 +03:00
|
|
|
pl011_event, NULL, s, NULL, true);
|
2009-05-15 01:35:07 +04:00
|
|
|
}
|
|
|
|
|
2023-01-23 19:23:02 +03:00
|
|
|
static void pl011_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
PL011State *s = PL011(dev);
|
|
|
|
|
|
|
|
s->lcr = 0;
|
|
|
|
s->rsr = 0;
|
|
|
|
s->dmacr = 0;
|
|
|
|
s->int_enabled = 0;
|
|
|
|
s->int_level = 0;
|
|
|
|
s->ilpr = 0;
|
|
|
|
s->ibrd = 0;
|
|
|
|
s->fbrd = 0;
|
|
|
|
s->read_trigger = 1;
|
|
|
|
s->ifl = 0x12;
|
|
|
|
s->cr = 0x300;
|
2023-01-23 19:23:03 +03:00
|
|
|
s->flags = 0;
|
2023-05-22 16:52:06 +03:00
|
|
|
pl011_reset_rx_fifo(s);
|
|
|
|
pl011_reset_tx_fifo(s);
|
2023-01-23 19:23:02 +03:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
static void pl011_class_init(ObjectClass *oc, void *data)
|
2012-01-24 23:12:29 +04:00
|
|
|
{
|
2013-07-25 01:29:17 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
dc->realize = pl011_realize;
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, pl011_reset);
|
2013-07-25 01:29:17 +04:00
|
|
|
dc->vmsd = &vmstate_pl011;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, pl011_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pl011_arm_info = {
|
2013-07-25 01:29:17 +04:00
|
|
|
.name = TYPE_PL011,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2013-07-25 01:13:57 +04:00
|
|
|
.instance_size = sizeof(PL011State),
|
2013-07-25 01:29:17 +04:00
|
|
|
.instance_init = pl011_init,
|
|
|
|
.class_init = pl011_class_init,
|
2012-01-24 23:12:29 +04:00
|
|
|
};
|
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
static void pl011_luminary_init(Object *obj)
|
2012-01-24 23:12:29 +04:00
|
|
|
{
|
2013-07-25 01:29:17 +04:00
|
|
|
PL011State *s = PL011(obj);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-07-25 01:29:17 +04:00
|
|
|
s->id = pl011_id_luminary;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pl011_luminary_info = {
|
2019-02-21 21:17:46 +03:00
|
|
|
.name = TYPE_PL011_LUMINARY,
|
2013-07-25 01:29:17 +04:00
|
|
|
.parent = TYPE_PL011,
|
|
|
|
.instance_init = pl011_luminary_init,
|
2012-01-24 23:12:29 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void pl011_register_types(void)
|
2009-05-15 01:35:07 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&pl011_arm_info);
|
|
|
|
type_register_static(&pl011_luminary_info);
|
2009-05-15 01:35:07 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(pl011_register_types)
|