ACPI: Add GPIO Connection Descriptor
[qemu.git] / hw / ssi / ssi.c
blob2aab79ba7f8ae266c96512831d1d38a7c27efdf2
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 "hw/ssi.h"
17 struct SSIBus {
18 BusState parent_obj;
21 #define TYPE_SSI_BUS "SSI"
22 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
24 static const TypeInfo ssi_bus_info = {
25 .name = TYPE_SSI_BUS,
26 .parent = TYPE_BUS,
27 .instance_size = sizeof(SSIBus),
30 static void ssi_cs_default(void *opaque, int n, int level)
32 SSISlave *s = SSI_SLAVE(opaque);
33 bool cs = !!level;
34 assert(n == 0);
35 if (s->cs != cs) {
36 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
37 if (ssc->set_cs) {
38 ssc->set_cs(s, cs);
41 s->cs = cs;
44 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
46 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
48 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
49 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
50 ssc->cs_polarity == SSI_CS_NONE) {
51 return ssc->transfer(dev, val);
53 return 0;
56 static int ssi_slave_init(DeviceState *dev)
58 SSISlave *s = SSI_SLAVE(dev);
59 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
61 if (ssc->transfer_raw == ssi_transfer_raw_default &&
62 ssc->cs_polarity != SSI_CS_NONE) {
63 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
66 return ssc->init(s);
69 static void ssi_slave_class_init(ObjectClass *klass, void *data)
71 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
72 DeviceClass *dc = DEVICE_CLASS(klass);
74 dc->init = ssi_slave_init;
75 dc->bus_type = TYPE_SSI_BUS;
76 if (!ssc->transfer_raw) {
77 ssc->transfer_raw = ssi_transfer_raw_default;
81 static const TypeInfo ssi_slave_info = {
82 .name = TYPE_SSI_SLAVE,
83 .parent = TYPE_DEVICE,
84 .class_init = ssi_slave_class_init,
85 .class_size = sizeof(SSISlaveClass),
86 .abstract = true,
89 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
91 return qdev_create(BUS(bus), name);
94 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
96 DeviceState *dev = ssi_create_slave_no_init(bus, name);
98 qdev_init_nofail(dev);
99 return dev;
102 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
104 BusState *bus;
105 bus = qbus_create(TYPE_SSI_BUS, parent, name);
106 return SSI_BUS(bus);
109 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
111 BusState *b = BUS(bus);
112 BusChild *kid;
113 SSISlaveClass *ssc;
114 uint32_t r = 0;
116 QTAILQ_FOREACH(kid, &b->children, sibling) {
117 SSISlave *slave = SSI_SLAVE(kid->child);
118 ssc = SSI_SLAVE_GET_CLASS(slave);
119 r |= ssc->transfer_raw(slave, val);
122 return r;
125 const VMStateDescription vmstate_ssi_slave = {
126 .name = "SSISlave",
127 .version_id = 1,
128 .minimum_version_id = 1,
129 .fields = (VMStateField[]) {
130 VMSTATE_BOOL(cs, SSISlave),
131 VMSTATE_END_OF_LIST()
135 static void ssi_slave_register_types(void)
137 type_register_static(&ssi_bus_info);
138 type_register_static(&ssi_slave_info);
141 type_init(ssi_slave_register_types)
143 typedef struct SSIAutoConnectArg {
144 qemu_irq **cs_linep;
145 SSIBus *bus;
146 } SSIAutoConnectArg;
148 static int ssi_auto_connect_slave(Object *child, void *opaque)
150 SSIAutoConnectArg *arg = opaque;
151 SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
152 qemu_irq cs_line;
154 if (!dev) {
155 return 0;
158 cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0);
159 qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus));
160 **arg->cs_linep = cs_line;
161 (*arg->cs_linep)++;
162 return 0;
165 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
166 SSIBus *bus)
168 SSIAutoConnectArg arg = {
169 .cs_linep = &cs_line,
170 .bus = bus
173 object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);