2009-05-14 23:35:09 +02:00
|
|
|
/*
|
|
|
|
* QEMU Synchronous Serial Interface support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 CodeSourcery.
|
2012-07-24 02:55:15 +02:00
|
|
|
* Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com)
|
|
|
|
* Copyright (c) 2012 PetaLogix Pty Ltd.
|
2009-05-14 23:35:09 +02:00
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the GNU GPL v2.
|
2012-01-13 17:44:23 +01:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-05-14 23:35:09 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:30 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-01-21 15:15:03 +01:00
|
|
|
#include "hw/ssi/ssi.h"
|
2009-05-14 23:35:09 +02:00
|
|
|
|
|
|
|
struct SSIBus {
|
2014-02-12 01:28:25 +01:00
|
|
|
BusState parent_obj;
|
2009-05-14 23:35:09 +02:00
|
|
|
};
|
|
|
|
|
2012-05-02 09:00:20 +02:00
|
|
|
#define TYPE_SSI_BUS "SSI"
|
|
|
|
#define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
|
|
|
|
|
|
|
|
static const TypeInfo ssi_bus_info = {
|
|
|
|
.name = TYPE_SSI_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(SSIBus),
|
2009-06-30 14:12:08 +02:00
|
|
|
};
|
|
|
|
|
2012-07-24 04:23:22 +02:00
|
|
|
static void ssi_cs_default(void *opaque, int n, int level)
|
|
|
|
{
|
|
|
|
SSISlave *s = SSI_SLAVE(opaque);
|
|
|
|
bool cs = !!level;
|
|
|
|
assert(n == 0);
|
|
|
|
if (s->cs != cs) {
|
|
|
|
SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
|
|
|
|
if (ssc->set_cs) {
|
|
|
|
ssc->set_cs(s, cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->cs = cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
|
|
|
|
{
|
|
|
|
SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
|
|
|
|
|
|
|
|
if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
|
|
|
|
(!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
|
|
|
|
ssc->cs_polarity == SSI_CS_NONE) {
|
|
|
|
return ssc->transfer(dev, val);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-09 22:02:56 +01:00
|
|
|
static int ssi_slave_init(DeviceState *dev)
|
2009-05-14 23:35:09 +02:00
|
|
|
{
|
2011-12-16 20:36:39 +01:00
|
|
|
SSISlave *s = SSI_SLAVE(dev);
|
|
|
|
SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
|
2009-05-14 23:35:09 +02:00
|
|
|
|
2012-07-24 04:23:22 +02:00
|
|
|
if (ssc->transfer_raw == ssi_transfer_raw_default &&
|
|
|
|
ssc->cs_polarity != SSI_CS_NONE) {
|
2014-05-20 08:31:33 +02:00
|
|
|
qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
|
2012-07-24 04:23:22 +02:00
|
|
|
}
|
|
|
|
|
2011-12-16 20:36:39 +01:00
|
|
|
return ssc->init(s);
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
static void ssi_slave_class_init(ObjectClass *klass, void *data)
|
2009-05-14 23:35:09 +02:00
|
|
|
{
|
2012-07-24 04:23:22 +02:00
|
|
|
SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-07-24 04:23:22 +02:00
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->init = ssi_slave_init;
|
2012-05-02 09:00:20 +02:00
|
|
|
dc->bus_type = TYPE_SSI_BUS;
|
2012-07-24 04:23:22 +02:00
|
|
|
if (!ssc->transfer_raw) {
|
|
|
|
ssc->transfer_raw = ssi_transfer_raw_default;
|
|
|
|
}
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo ssi_slave_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = TYPE_SSI_SLAVE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.class_init = ssi_slave_class_init,
|
|
|
|
.class_size = sizeof(SSISlaveClass),
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
2012-07-24 05:56:27 +02:00
|
|
|
DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
|
|
|
|
{
|
2014-02-12 01:28:25 +01:00
|
|
|
return qdev_create(BUS(bus), name);
|
2012-07-24 05:56:27 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 23:35:09 +02:00
|
|
|
DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
|
|
|
|
{
|
2012-07-24 05:56:27 +02:00
|
|
|
DeviceState *dev = ssi_create_slave_no_init(bus, name);
|
|
|
|
|
2009-10-07 01:15:58 +02:00
|
|
|
qdev_init_nofail(dev);
|
2009-05-14 23:35:09 +02:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2009-05-23 01:05:19 +02:00
|
|
|
SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
|
2009-05-14 23:35:09 +02:00
|
|
|
{
|
2009-05-23 01:05:19 +02:00
|
|
|
BusState *bus;
|
2012-05-02 09:00:20 +02:00
|
|
|
bus = qbus_create(TYPE_SSI_BUS, parent, name);
|
2013-06-07 14:45:17 +02:00
|
|
|
return SSI_BUS(bus);
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
|
|
|
|
{
|
2014-02-12 01:28:25 +01:00
|
|
|
BusState *b = BUS(bus);
|
2011-12-23 22:34:39 +01:00
|
|
|
BusChild *kid;
|
2011-12-16 20:36:39 +01:00
|
|
|
SSISlaveClass *ssc;
|
2012-07-24 02:55:15 +02:00
|
|
|
uint32_t r = 0;
|
2011-12-23 22:34:39 +01:00
|
|
|
|
2014-02-12 01:28:25 +01:00
|
|
|
QTAILQ_FOREACH(kid, &b->children, sibling) {
|
2012-07-24 02:55:15 +02:00
|
|
|
SSISlave *slave = SSI_SLAVE(kid->child);
|
|
|
|
ssc = SSI_SLAVE_GET_CLASS(slave);
|
2012-07-24 04:23:22 +02:00
|
|
|
r |= ssc->transfer_raw(slave, val);
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
2012-07-24 02:55:15 +02:00
|
|
|
|
|
|
|
return r;
|
2009-05-14 23:35:09 +02:00
|
|
|
}
|
2011-12-08 04:34:16 +01:00
|
|
|
|
2012-07-24 04:23:22 +02:00
|
|
|
const VMStateDescription vmstate_ssi_slave = {
|
|
|
|
.name = "SSISlave",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-05-13 17:09:35 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2012-07-24 04:23:22 +02:00
|
|
|
VMSTATE_BOOL(cs, SSISlave),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void ssi_slave_register_types(void)
|
2011-12-08 04:34:16 +01:00
|
|
|
{
|
2012-05-02 09:00:20 +02:00
|
|
|
type_register_static(&ssi_bus_info);
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&ssi_slave_info);
|
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(ssi_slave_register_types)
|
2012-10-01 04:34:37 +02:00
|
|
|
|
|
|
|
typedef struct SSIAutoConnectArg {
|
|
|
|
qemu_irq **cs_linep;
|
|
|
|
SSIBus *bus;
|
|
|
|
} SSIAutoConnectArg;
|
|
|
|
|
|
|
|
static int ssi_auto_connect_slave(Object *child, void *opaque)
|
|
|
|
{
|
|
|
|
SSIAutoConnectArg *arg = opaque;
|
|
|
|
SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
|
|
|
|
qemu_irq cs_line;
|
|
|
|
|
|
|
|
if (!dev) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-20 08:31:33 +02:00
|
|
|
cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0);
|
2014-02-12 01:28:25 +01:00
|
|
|
qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus));
|
2012-10-01 04:34:37 +02:00
|
|
|
**arg->cs_linep = cs_line;
|
|
|
|
(*arg->cs_linep)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
|
|
|
|
SSIBus *bus)
|
|
|
|
{
|
|
|
|
SSIAutoConnectArg arg = {
|
|
|
|
.cs_linep = &cs_line,
|
|
|
|
.bus = bus
|
|
|
|
};
|
|
|
|
|
|
|
|
object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);
|
|
|
|
}
|