2007-09-16 23:08:06 +02:00
|
|
|
/*
|
2007-06-03 13:13:39 +02:00
|
|
|
* ColdFire UART emulation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 CodeSourcery.
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the GPL
|
2007-06-03 13:13:39 +02:00
|
|
|
*/
|
2016-01-26 19:17:30 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/m68k/mcf.h"
|
2013-04-08 16:55:25 +02:00
|
|
|
#include "sysemu/char.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2016-10-22 11:52:52 +02:00
|
|
|
#include "qapi/error.h"
|
2007-06-03 13:13:39 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2011-11-24 14:31:13 +01:00
|
|
|
MemoryRegion iomem;
|
2007-06-03 13:13:39 +02:00
|
|
|
uint8_t mr[2];
|
|
|
|
uint8_t sr;
|
|
|
|
uint8_t isr;
|
|
|
|
uint8_t imr;
|
|
|
|
uint8_t bg1;
|
|
|
|
uint8_t bg2;
|
|
|
|
uint8_t fifo[4];
|
|
|
|
uint8_t tb;
|
|
|
|
int current_mr;
|
|
|
|
int fifo_len;
|
|
|
|
int tx_enabled;
|
|
|
|
int rx_enabled;
|
|
|
|
qemu_irq irq;
|
2016-10-22 11:52:52 +02:00
|
|
|
CharBackend chr;
|
2007-06-03 13:13:39 +02:00
|
|
|
} mcf_uart_state;
|
|
|
|
|
|
|
|
/* UART Status Register bits. */
|
|
|
|
#define MCF_UART_RxRDY 0x01
|
|
|
|
#define MCF_UART_FFULL 0x02
|
|
|
|
#define MCF_UART_TxRDY 0x04
|
|
|
|
#define MCF_UART_TxEMP 0x08
|
|
|
|
#define MCF_UART_OE 0x10
|
|
|
|
#define MCF_UART_PE 0x20
|
|
|
|
#define MCF_UART_FE 0x40
|
|
|
|
#define MCF_UART_RB 0x80
|
|
|
|
|
|
|
|
/* Interrupt flags. */
|
|
|
|
#define MCF_UART_TxINT 0x01
|
|
|
|
#define MCF_UART_RxINT 0x02
|
|
|
|
#define MCF_UART_DBINT 0x04
|
|
|
|
#define MCF_UART_COSINT 0x80
|
|
|
|
|
|
|
|
/* UMR1 flags. */
|
|
|
|
#define MCF_UART_BC0 0x01
|
|
|
|
#define MCF_UART_BC1 0x02
|
|
|
|
#define MCF_UART_PT 0x04
|
|
|
|
#define MCF_UART_PM0 0x08
|
|
|
|
#define MCF_UART_PM1 0x10
|
|
|
|
#define MCF_UART_ERR 0x20
|
|
|
|
#define MCF_UART_RxIRQ 0x40
|
|
|
|
#define MCF_UART_RxRTS 0x80
|
|
|
|
|
|
|
|
static void mcf_uart_update(mcf_uart_state *s)
|
|
|
|
{
|
|
|
|
s->isr &= ~(MCF_UART_TxINT | MCF_UART_RxINT);
|
|
|
|
if (s->sr & MCF_UART_TxRDY)
|
|
|
|
s->isr |= MCF_UART_TxINT;
|
|
|
|
if ((s->sr & ((s->mr[0] & MCF_UART_RxIRQ)
|
|
|
|
? MCF_UART_FFULL : MCF_UART_RxRDY)) != 0)
|
|
|
|
s->isr |= MCF_UART_RxINT;
|
|
|
|
|
|
|
|
qemu_set_irq(s->irq, (s->isr & s->imr) != 0);
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
uint64_t mcf_uart_read(void *opaque, hwaddr addr,
|
2011-11-24 14:31:13 +01:00
|
|
|
unsigned size)
|
2007-06-03 13:13:39 +02:00
|
|
|
{
|
|
|
|
mcf_uart_state *s = (mcf_uart_state *)opaque;
|
|
|
|
switch (addr & 0x3f) {
|
|
|
|
case 0x00:
|
|
|
|
return s->mr[s->current_mr];
|
|
|
|
case 0x04:
|
|
|
|
return s->sr;
|
|
|
|
case 0x0c:
|
|
|
|
{
|
|
|
|
uint8_t val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (s->fifo_len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
val = s->fifo[0];
|
|
|
|
s->fifo_len--;
|
|
|
|
for (i = 0; i < s->fifo_len; i++)
|
|
|
|
s->fifo[i] = s->fifo[i + 1];
|
|
|
|
s->sr &= ~MCF_UART_FFULL;
|
|
|
|
if (s->fifo_len == 0)
|
|
|
|
s->sr &= ~MCF_UART_RxRDY;
|
|
|
|
mcf_uart_update(s);
|
2016-10-22 11:52:53 +02:00
|
|
|
qemu_chr_fe_accept_input(s->chr.chr);
|
2007-06-03 13:13:39 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
case 0x10:
|
|
|
|
/* TODO: Implement IPCR. */
|
|
|
|
return 0;
|
|
|
|
case 0x14:
|
|
|
|
return s->isr;
|
|
|
|
case 0x18:
|
|
|
|
return s->bg1;
|
|
|
|
case 0x1c:
|
|
|
|
return s->bg2;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update TxRDY flag and set data if present and enabled. */
|
|
|
|
static void mcf_uart_do_tx(mcf_uart_state *s)
|
|
|
|
{
|
|
|
|
if (s->tx_enabled && (s->sr & MCF_UART_TxEMP) == 0) {
|
2016-10-22 11:52:52 +02:00
|
|
|
if (s->chr.chr) {
|
2016-09-06 15:56:04 +02:00
|
|
|
/* XXX this blocks entire thread. Rewrite to use
|
|
|
|
* qemu_chr_fe_write and background I/O callbacks */
|
2016-10-22 11:52:52 +02:00
|
|
|
qemu_chr_fe_write_all(s->chr.chr, (unsigned char *)&s->tb, 1);
|
|
|
|
}
|
2007-06-03 13:13:39 +02:00
|
|
|
s->sr |= MCF_UART_TxEMP;
|
|
|
|
}
|
|
|
|
if (s->tx_enabled) {
|
|
|
|
s->sr |= MCF_UART_TxRDY;
|
|
|
|
} else {
|
|
|
|
s->sr &= ~MCF_UART_TxRDY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcf_do_command(mcf_uart_state *s, uint8_t cmd)
|
|
|
|
{
|
|
|
|
/* Misc command. */
|
2015-06-24 13:55:51 +02:00
|
|
|
switch ((cmd >> 4) & 7) {
|
2007-06-03 13:13:39 +02:00
|
|
|
case 0: /* No-op. */
|
|
|
|
break;
|
|
|
|
case 1: /* Reset mode register pointer. */
|
|
|
|
s->current_mr = 0;
|
|
|
|
break;
|
|
|
|
case 2: /* Reset receiver. */
|
|
|
|
s->rx_enabled = 0;
|
|
|
|
s->fifo_len = 0;
|
|
|
|
s->sr &= ~(MCF_UART_RxRDY | MCF_UART_FFULL);
|
|
|
|
break;
|
|
|
|
case 3: /* Reset transmitter. */
|
|
|
|
s->tx_enabled = 0;
|
|
|
|
s->sr |= MCF_UART_TxEMP;
|
|
|
|
s->sr &= ~MCF_UART_TxRDY;
|
|
|
|
break;
|
|
|
|
case 4: /* Reset error status. */
|
|
|
|
break;
|
|
|
|
case 5: /* Reset break-change interrupt. */
|
|
|
|
s->isr &= ~MCF_UART_DBINT;
|
|
|
|
break;
|
|
|
|
case 6: /* Start break. */
|
|
|
|
case 7: /* Stop break. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmitter command. */
|
|
|
|
switch ((cmd >> 2) & 3) {
|
|
|
|
case 0: /* No-op. */
|
|
|
|
break;
|
|
|
|
case 1: /* Enable. */
|
|
|
|
s->tx_enabled = 1;
|
|
|
|
mcf_uart_do_tx(s);
|
|
|
|
break;
|
|
|
|
case 2: /* Disable. */
|
|
|
|
s->tx_enabled = 0;
|
|
|
|
mcf_uart_do_tx(s);
|
|
|
|
break;
|
|
|
|
case 3: /* Reserved. */
|
|
|
|
fprintf(stderr, "mcf_uart: Bad TX command\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receiver command. */
|
|
|
|
switch (cmd & 3) {
|
|
|
|
case 0: /* No-op. */
|
|
|
|
break;
|
|
|
|
case 1: /* Enable. */
|
|
|
|
s->rx_enabled = 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
s->rx_enabled = 0;
|
|
|
|
break;
|
|
|
|
case 3: /* Reserved. */
|
|
|
|
fprintf(stderr, "mcf_uart: Bad RX command\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
void mcf_uart_write(void *opaque, hwaddr addr,
|
2011-11-24 14:31:13 +01:00
|
|
|
uint64_t val, unsigned size)
|
2007-06-03 13:13:39 +02:00
|
|
|
{
|
|
|
|
mcf_uart_state *s = (mcf_uart_state *)opaque;
|
|
|
|
switch (addr & 0x3f) {
|
|
|
|
case 0x00:
|
|
|
|
s->mr[s->current_mr] = val;
|
|
|
|
s->current_mr = 1;
|
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
/* CSR is ignored. */
|
|
|
|
break;
|
|
|
|
case 0x08: /* Command Register. */
|
|
|
|
mcf_do_command(s, val);
|
|
|
|
break;
|
|
|
|
case 0x0c: /* Transmit Buffer. */
|
|
|
|
s->sr &= ~MCF_UART_TxEMP;
|
|
|
|
s->tb = val;
|
|
|
|
mcf_uart_do_tx(s);
|
|
|
|
break;
|
|
|
|
case 0x10:
|
|
|
|
/* ACR is ignored. */
|
|
|
|
break;
|
|
|
|
case 0x14:
|
|
|
|
s->imr = val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mcf_uart_update(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcf_uart_reset(mcf_uart_state *s)
|
|
|
|
{
|
|
|
|
s->fifo_len = 0;
|
|
|
|
s->mr[0] = 0;
|
|
|
|
s->mr[1] = 0;
|
|
|
|
s->sr = MCF_UART_TxEMP;
|
|
|
|
s->tx_enabled = 0;
|
|
|
|
s->rx_enabled = 0;
|
|
|
|
s->isr = 0;
|
|
|
|
s->imr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcf_uart_push_byte(mcf_uart_state *s, uint8_t data)
|
|
|
|
{
|
|
|
|
/* Break events overwrite the last byte if the fifo is full. */
|
|
|
|
if (s->fifo_len == 4)
|
|
|
|
s->fifo_len--;
|
|
|
|
|
|
|
|
s->fifo[s->fifo_len] = data;
|
|
|
|
s->fifo_len++;
|
|
|
|
s->sr |= MCF_UART_RxRDY;
|
|
|
|
if (s->fifo_len == 4)
|
|
|
|
s->sr |= MCF_UART_FFULL;
|
|
|
|
|
|
|
|
mcf_uart_update(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcf_uart_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
mcf_uart_state *s = (mcf_uart_state *)opaque;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case CHR_EVENT_BREAK:
|
|
|
|
s->isr |= MCF_UART_DBINT;
|
|
|
|
mcf_uart_push_byte(s, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcf_uart_can_receive(void *opaque)
|
|
|
|
{
|
|
|
|
mcf_uart_state *s = (mcf_uart_state *)opaque;
|
|
|
|
|
|
|
|
return s->rx_enabled && (s->sr & MCF_UART_FFULL) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mcf_uart_receive(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
mcf_uart_state *s = (mcf_uart_state *)opaque;
|
|
|
|
|
|
|
|
mcf_uart_push_byte(s, buf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *mcf_uart_init(qemu_irq irq, CharDriverState *chr)
|
|
|
|
{
|
|
|
|
mcf_uart_state *s;
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
s = g_malloc0(sizeof(mcf_uart_state));
|
2007-06-03 13:13:39 +02:00
|
|
|
s->irq = irq;
|
|
|
|
if (chr) {
|
2016-10-22 11:52:52 +02:00
|
|
|
qemu_chr_fe_init(&s->chr, chr, &error_abort);
|
2013-03-27 20:29:40 +01:00
|
|
|
qemu_chr_fe_claim_no_fail(chr);
|
2007-06-03 13:13:39 +02:00
|
|
|
qemu_chr_add_handlers(chr, mcf_uart_can_receive, mcf_uart_receive,
|
|
|
|
mcf_uart_event, s);
|
|
|
|
}
|
|
|
|
mcf_uart_reset(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-11-24 14:31:13 +01:00
|
|
|
static const MemoryRegionOps mcf_uart_ops = {
|
|
|
|
.read = mcf_uart_read,
|
|
|
|
.write = mcf_uart_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2007-06-03 13:13:39 +02:00
|
|
|
};
|
|
|
|
|
2011-11-24 14:31:13 +01:00
|
|
|
void mcf_uart_mm_init(MemoryRegion *sysmem,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr base,
|
2011-11-24 14:31:13 +01:00
|
|
|
qemu_irq irq,
|
2007-06-03 13:13:39 +02:00
|
|
|
CharDriverState *chr)
|
|
|
|
{
|
|
|
|
mcf_uart_state *s;
|
|
|
|
|
|
|
|
s = mcf_uart_init(irq, chr);
|
2013-06-06 11:41:28 +02:00
|
|
|
memory_region_init_io(&s->iomem, NULL, &mcf_uart_ops, s, "uart", 0x40);
|
2011-11-24 14:31:13 +01:00
|
|
|
memory_region_add_subregion(sysmem, base, &s->iomem);
|
2007-06-03 13:13:39 +02:00
|
|
|
}
|