monitor: simplify functions for getting a dup'd fdset entry
[qemu/ar7.git] / hw / ssi / ssi.c
blobfaf7633e70eb37aed42a5671657c9f7fa52866d9
1 /*
2 * QEMU Synchronous Serial Interface support
4 * Copyright (c) 2009 CodeSourcery.
5 * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com)
6 * Copyright (c) 2012 PetaLogix Pty Ltd.
7 * Written by Paul Brook
9 * This code is licensed under the GNU GPL v2.
11 * Contributions after 2012-01-13 are licensed under the terms of the
12 * GNU GPL, version 2 or (at your option) any later version.
15 #include "qemu/osdep.h"
16 #include "hw/ssi/ssi.h"
17 #include "migration/vmstate.h"
18 #include "qemu/module.h"
19 #include "qapi/error.h"
20 #include "qom/object.h"
22 struct SSIBus {
23 BusState parent_obj;
26 #define TYPE_SSI_BUS "SSI"
27 DECLARE_INSTANCE_CHECKER(SSIBus, SSI_BUS,
28 TYPE_SSI_BUS)
30 static const TypeInfo ssi_bus_info = {
31 .name = TYPE_SSI_BUS,
32 .parent = TYPE_BUS,
33 .instance_size = sizeof(SSIBus),
36 static void ssi_cs_default(void *opaque, int n, int level)
38 SSISlave *s = SSI_SLAVE(opaque);
39 bool cs = !!level;
40 assert(n == 0);
41 if (s->cs != cs) {
42 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
43 if (ssc->set_cs) {
44 ssc->set_cs(s, cs);
47 s->cs = cs;
50 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
52 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
54 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
55 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
56 ssc->cs_polarity == SSI_CS_NONE) {
57 return ssc->transfer(dev, val);
59 return 0;
62 static void ssi_slave_realize(DeviceState *dev, Error **errp)
64 SSISlave *s = SSI_SLAVE(dev);
65 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
67 if (ssc->transfer_raw == ssi_transfer_raw_default &&
68 ssc->cs_polarity != SSI_CS_NONE) {
69 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
72 ssc->realize(s, errp);
75 static void ssi_slave_class_init(ObjectClass *klass, void *data)
77 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
78 DeviceClass *dc = DEVICE_CLASS(klass);
80 dc->realize = ssi_slave_realize;
81 dc->bus_type = TYPE_SSI_BUS;
82 if (!ssc->transfer_raw) {
83 ssc->transfer_raw = ssi_transfer_raw_default;
87 static const TypeInfo ssi_slave_info = {
88 .name = TYPE_SSI_SLAVE,
89 .parent = TYPE_DEVICE,
90 .class_init = ssi_slave_class_init,
91 .class_size = sizeof(SSISlaveClass),
92 .abstract = true,
95 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)
97 return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
100 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
102 DeviceState *dev = qdev_new(name);
104 ssi_realize_and_unref(dev, bus, &error_fatal);
105 return dev;
108 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
110 BusState *bus;
111 bus = qbus_create(TYPE_SSI_BUS, parent, name);
112 return SSI_BUS(bus);
115 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
117 BusState *b = BUS(bus);
118 BusChild *kid;
119 SSISlaveClass *ssc;
120 uint32_t r = 0;
122 QTAILQ_FOREACH(kid, &b->children, sibling) {
123 SSISlave *slave = SSI_SLAVE(kid->child);
124 ssc = SSI_SLAVE_GET_CLASS(slave);
125 r |= ssc->transfer_raw(slave, val);
128 return r;
131 const VMStateDescription vmstate_ssi_slave = {
132 .name = "SSISlave",
133 .version_id = 1,
134 .minimum_version_id = 1,
135 .fields = (VMStateField[]) {
136 VMSTATE_BOOL(cs, SSISlave),
137 VMSTATE_END_OF_LIST()
141 static void ssi_slave_register_types(void)
143 type_register_static(&ssi_bus_info);
144 type_register_static(&ssi_slave_info);
147 type_init(ssi_slave_register_types)