2007-10-08 21:26:33 +08:00
|
|
|
/*
|
|
|
|
* QEMU ETRAX System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/sysbus.h"
|
2013-04-08 22:55:25 +08:00
|
|
|
#include "sysemu/char.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/log.h"
|
2007-10-08 21:26:33 +08:00
|
|
|
|
2008-03-02 01:25:33 +08:00
|
|
|
#define D(x)
|
|
|
|
|
2009-05-05 19:13:18 +08:00
|
|
|
#define RW_TR_CTRL (0x00 / 4)
|
|
|
|
#define RW_TR_DMA_EN (0x04 / 4)
|
|
|
|
#define RW_REC_CTRL (0x08 / 4)
|
|
|
|
#define RW_DOUT (0x1c / 4)
|
|
|
|
#define RS_STAT_DIN (0x20 / 4)
|
|
|
|
#define R_STAT_DIN (0x24 / 4)
|
|
|
|
#define RW_INTR_MASK (0x2c / 4)
|
|
|
|
#define RW_ACK_INTR (0x30 / 4)
|
|
|
|
#define R_INTR (0x34 / 4)
|
|
|
|
#define R_MASKED_INTR (0x38 / 4)
|
|
|
|
#define R_MAX (0x3c / 4)
|
2007-10-08 21:26:33 +08:00
|
|
|
|
2008-05-03 06:21:55 +08:00
|
|
|
#define STAT_DAV 16
|
|
|
|
#define STAT_TR_IDLE 22
|
|
|
|
#define STAT_TR_RDY 24
|
|
|
|
|
2013-07-25 03:52:26 +08:00
|
|
|
#define TYPE_ETRAX_FS_SERIAL "etraxfs,serial"
|
|
|
|
#define ETRAX_SERIAL(obj) \
|
|
|
|
OBJECT_CHECK(ETRAXSerial, (obj), TYPE_ETRAX_FS_SERIAL)
|
|
|
|
|
|
|
|
typedef struct ETRAXSerial {
|
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-08-11 19:47:45 +08:00
|
|
|
MemoryRegion mmio;
|
2009-05-16 07:44:24 +08:00
|
|
|
CharDriverState *chr;
|
|
|
|
qemu_irq irq;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2009-05-16 07:44:24 +08:00
|
|
|
int pending_tx;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2010-06-15 00:41:12 +08:00
|
|
|
uint8_t rx_fifo[16];
|
|
|
|
unsigned int rx_fifo_pos;
|
|
|
|
unsigned int rx_fifo_len;
|
|
|
|
|
2009-05-16 07:44:24 +08:00
|
|
|
/* Control registers. */
|
|
|
|
uint32_t regs[R_MAX];
|
2013-07-25 03:52:26 +08:00
|
|
|
} ETRAXSerial;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2013-07-25 03:52:26 +08:00
|
|
|
static void ser_update_irq(ETRAXSerial *s)
|
2008-05-03 06:21:55 +08:00
|
|
|
{
|
2009-05-05 19:13:18 +08:00
|
|
|
|
2010-06-15 00:41:12 +08:00
|
|
|
if (s->rx_fifo_len) {
|
|
|
|
s->regs[R_INTR] |= 8;
|
|
|
|
} else {
|
|
|
|
s->regs[R_INTR] &= ~8;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->regs[R_MASKED_INTR] = s->regs[R_INTR] & s->regs[RW_INTR_MASK];
|
2009-05-16 07:44:24 +08:00
|
|
|
qemu_set_irq(s->irq, !!s->regs[R_MASKED_INTR]);
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2011-08-11 19:47:45 +08:00
|
|
|
static uint64_t
|
2012-10-23 18:30:10 +08:00
|
|
|
ser_read(void *opaque, hwaddr addr, unsigned int size)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = opaque;
|
2009-05-16 07:44:24 +08:00
|
|
|
uint32_t r = 0;
|
|
|
|
|
|
|
|
addr >>= 2;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case R_STAT_DIN:
|
2010-06-15 00:41:12 +08:00
|
|
|
r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 15];
|
|
|
|
if (s->rx_fifo_len) {
|
|
|
|
r |= 1 << STAT_DAV;
|
|
|
|
}
|
|
|
|
r |= 1 << STAT_TR_RDY;
|
|
|
|
r |= 1 << STAT_TR_IDLE;
|
2009-05-16 07:44:24 +08:00
|
|
|
break;
|
|
|
|
case RS_STAT_DIN:
|
2010-06-15 00:41:12 +08:00
|
|
|
r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 15];
|
|
|
|
if (s->rx_fifo_len) {
|
|
|
|
r |= 1 << STAT_DAV;
|
|
|
|
s->rx_fifo_len--;
|
|
|
|
}
|
|
|
|
r |= 1 << STAT_TR_RDY;
|
|
|
|
r |= 1 << STAT_TR_IDLE;
|
2009-05-16 07:44:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = s->regs[addr];
|
2011-08-11 15:17:28 +08:00
|
|
|
D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr, r));
|
2009-05-16 07:44:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-23 18:30:10 +08:00
|
|
|
ser_write(void *opaque, hwaddr addr,
|
2011-08-11 19:47:45 +08:00
|
|
|
uint64_t val64, unsigned int size)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = opaque;
|
2011-08-11 19:47:45 +08:00
|
|
|
uint32_t value = val64;
|
|
|
|
unsigned char ch = val64;
|
2009-05-16 07:44:24 +08:00
|
|
|
|
2011-08-11 15:17:28 +08:00
|
|
|
D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr, value));
|
2009-05-16 07:44:24 +08:00
|
|
|
addr >>= 2;
|
|
|
|
switch (addr)
|
|
|
|
{
|
|
|
|
case RW_DOUT:
|
2011-08-16 00:17:28 +08:00
|
|
|
qemu_chr_fe_write(s->chr, &ch, 1);
|
2010-06-15 00:41:12 +08:00
|
|
|
s->regs[R_INTR] |= 3;
|
2009-05-16 07:44:24 +08:00
|
|
|
s->pending_tx = 1;
|
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
case RW_ACK_INTR:
|
2010-06-15 00:41:12 +08:00
|
|
|
if (s->pending_tx) {
|
|
|
|
value &= ~1;
|
2009-05-16 07:44:24 +08:00
|
|
|
s->pending_tx = 0;
|
2011-08-11 15:17:28 +08:00
|
|
|
D(qemu_log("fixedup value=%x r_intr=%x\n",
|
|
|
|
value, s->regs[R_INTR]));
|
2009-05-16 07:44:24 +08:00
|
|
|
}
|
2010-06-15 00:41:12 +08:00
|
|
|
s->regs[addr] = value;
|
|
|
|
s->regs[R_INTR] &= ~value;
|
|
|
|
D(printf("r_intr=%x\n", s->regs[R_INTR]));
|
2009-05-16 07:44:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s->regs[addr] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ser_update_irq(s);
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
|
|
|
|
2011-08-11 19:47:45 +08:00
|
|
|
static const MemoryRegionOps ser_ops = {
|
|
|
|
.read = ser_read,
|
|
|
|
.write = ser_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4
|
|
|
|
}
|
2007-10-08 21:26:33 +08:00
|
|
|
};
|
|
|
|
|
2008-05-03 06:21:55 +08:00
|
|
|
static void serial_receive(void *opaque, const uint8_t *buf, int size)
|
2007-10-08 21:26:33 +08:00
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = opaque;
|
2010-06-15 00:41:12 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Got a byte. */
|
|
|
|
if (s->rx_fifo_len >= 16) {
|
2011-08-11 15:17:28 +08:00
|
|
|
qemu_log("WARNING: UART dropped char.\n");
|
2010-06-15 00:41:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
s->rx_fifo[s->rx_fifo_pos] = buf[i];
|
|
|
|
s->rx_fifo_pos++;
|
|
|
|
s->rx_fifo_pos &= 15;
|
|
|
|
s->rx_fifo_len++;
|
|
|
|
}
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2009-05-16 07:44:24 +08:00
|
|
|
ser_update_irq(s);
|
2008-05-03 06:21:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int serial_can_receive(void *opaque)
|
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = opaque;
|
2009-05-16 07:44:24 +08:00
|
|
|
int r;
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2009-05-16 07:44:24 +08:00
|
|
|
/* Is the receiver enabled? */
|
2010-06-15 00:41:12 +08:00
|
|
|
if (!(s->regs[RW_REC_CTRL] & (1 << 3))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-03 06:21:55 +08:00
|
|
|
|
2010-06-15 00:41:12 +08:00
|
|
|
r = sizeof(s->rx_fifo) - s->rx_fifo_len;
|
2009-05-16 07:44:24 +08:00
|
|
|
return r;
|
2008-05-03 06:21:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-30 16:52:22 +08:00
|
|
|
static void etraxfs_ser_reset(DeviceState *d)
|
2008-05-03 06:21:55 +08:00
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = ETRAX_SERIAL(d);
|
2009-05-16 07:44:24 +08:00
|
|
|
|
|
|
|
/* transmitter begins ready and idle. */
|
|
|
|
s->regs[RS_STAT_DIN] |= (1 << STAT_TR_RDY);
|
|
|
|
s->regs[RS_STAT_DIN] |= (1 << STAT_TR_IDLE);
|
|
|
|
|
2011-06-30 16:52:22 +08:00
|
|
|
s->regs[RW_REC_CTRL] = 0x10000;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int etraxfs_ser_init(SysBusDevice *dev)
|
|
|
|
{
|
2013-07-25 03:52:26 +08:00
|
|
|
ETRAXSerial *s = ETRAX_SERIAL(dev);
|
2011-06-30 16:52:22 +08:00
|
|
|
|
2009-05-16 07:44:24 +08:00
|
|
|
sysbus_init_irq(dev, &s->irq);
|
2013-06-07 09:25:08 +08:00
|
|
|
memory_region_init_io(&s->mmio, OBJECT(s), &ser_ops, s,
|
|
|
|
"etraxfs-serial", R_MAX * 4);
|
2011-11-27 17:38:10 +08:00
|
|
|
sysbus_init_mmio(dev, &s->mmio);
|
2011-08-11 19:47:45 +08:00
|
|
|
|
2015-03-25 16:29:20 +08:00
|
|
|
/* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */
|
2011-12-23 05:29:25 +08:00
|
|
|
s->chr = qemu_char_get_next_serial();
|
2013-07-25 03:52:26 +08:00
|
|
|
if (s->chr) {
|
2009-05-16 07:44:24 +08:00
|
|
|
qemu_chr_add_handlers(s->chr,
|
2013-07-25 03:52:26 +08:00
|
|
|
serial_can_receive, serial_receive,
|
|
|
|
serial_event, s);
|
|
|
|
}
|
2009-08-14 16:36:05 +08:00
|
|
|
return 0;
|
2007-10-08 21:26:33 +08:00
|
|
|
}
|
2009-05-16 07:40:46 +08:00
|
|
|
|
2012-01-25 03:12:29 +08:00
|
|
|
static void etraxfs_ser_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-25 03:12:29 +08:00
|
|
|
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = etraxfs_ser_init;
|
2011-12-08 11:34:16 +08:00
|
|
|
dc->reset = etraxfs_ser_reset;
|
sysbus: Make devices picking up backends unavailable with -device
Device models aren't supposed to go on fishing expeditions for
backends. They should expose suitable properties for the user to set.
For onboard devices, board code sets them.
A number of sysbus devices pick up block backends in their init() /
instance_init() methods with drive_get_next() instead: sl-nand,
milkymist-memcard, pl181, generic-sdhci.
Likewise, a number of sysbus devices pick up character backends in
their init() / realize() methods with qemu_char_get_next_serial():
cadence_uart, digic-uart, etraxfs,serial, lm32-juart, lm32-uart,
milkymist-uart, pl011, stm32f2xx-usart, xlnx.xps-uartlite.
All these mistakes are already marked FIXME. See the commit that
added these FIXMEs for a more detailed explanation of what's wrong.
Fortunately, only machines ppce500 and pseries-* support -device with
sysbus devices, and none of the devices above is supported with these
machines.
Set cannot_instantiate_with_device_add_yet to preserve our luck.
Cc: Andrzej Zaborowski <balrogg@gmail.com>
Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
Cc: Antony Pavlov <antonynpavlov@gmail.com>
Cc: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>
Cc: Michael Walle <michael@walle.cc>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-03-24 02:34:40 +08:00
|
|
|
/* Reason: init() method uses qemu_char_get_next_serial() */
|
|
|
|
dc->cannot_instantiate_with_device_add_yet = true;
|
2012-01-25 03:12:29 +08:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:19:07 +08:00
|
|
|
static const TypeInfo etraxfs_ser_info = {
|
2013-07-25 03:52:26 +08:00
|
|
|
.name = TYPE_ETRAX_FS_SERIAL,
|
2011-12-08 11:34:16 +08:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2013-07-25 03:52:26 +08:00
|
|
|
.instance_size = sizeof(ETRAXSerial),
|
2011-12-08 11:34:16 +08:00
|
|
|
.class_init = etraxfs_ser_class_init,
|
2011-06-30 16:52:22 +08:00
|
|
|
};
|
|
|
|
|
2012-02-09 22:20:55 +08:00
|
|
|
static void etraxfs_serial_register_types(void)
|
2009-05-16 07:40:46 +08:00
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
type_register_static(&etraxfs_ser_info);
|
2009-05-16 07:40:46 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 22:20:55 +08:00
|
|
|
type_init(etraxfs_serial_register_types)
|