hw/audio: Fix crashes when devices are used on ISA bus without DMA
[qemu/ar7.git] / hw / ssi / ssi.c
blob7eaaf565fd67e2bf2b0dffef981bcfe0e3082877
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"
18 struct SSIBus {
19 BusState parent_obj;
22 #define TYPE_SSI_BUS "SSI"
23 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
25 static const TypeInfo ssi_bus_info = {
26 .name = TYPE_SSI_BUS,
27 .parent = TYPE_BUS,
28 .instance_size = sizeof(SSIBus),
31 static void ssi_cs_default(void *opaque, int n, int level)
33 SSISlave *s = SSI_SLAVE(opaque);
34 bool cs = !!level;
35 assert(n == 0);
36 if (s->cs != cs) {
37 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
38 if (ssc->set_cs) {
39 ssc->set_cs(s, cs);
42 s->cs = cs;
45 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
47 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
49 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
50 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
51 ssc->cs_polarity == SSI_CS_NONE) {
52 return ssc->transfer(dev, val);
54 return 0;
57 static void ssi_slave_realize(DeviceState *dev, Error **errp)
59 SSISlave *s = SSI_SLAVE(dev);
60 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
62 if (ssc->transfer_raw == ssi_transfer_raw_default &&
63 ssc->cs_polarity != SSI_CS_NONE) {
64 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
67 ssc->realize(s, errp);
70 static void ssi_slave_class_init(ObjectClass *klass, void *data)
72 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
73 DeviceClass *dc = DEVICE_CLASS(klass);
75 dc->realize = ssi_slave_realize;
76 dc->bus_type = TYPE_SSI_BUS;
77 if (!ssc->transfer_raw) {
78 ssc->transfer_raw = ssi_transfer_raw_default;
82 static const TypeInfo ssi_slave_info = {
83 .name = TYPE_SSI_SLAVE,
84 .parent = TYPE_DEVICE,
85 .class_init = ssi_slave_class_init,
86 .class_size = sizeof(SSISlaveClass),
87 .abstract = true,
90 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
92 return qdev_create(BUS(bus), name);
95 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
97 DeviceState *dev = ssi_create_slave_no_init(bus, name);
99 qdev_init_nofail(dev);
100 return dev;
103 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
105 BusState *bus;
106 bus = qbus_create(TYPE_SSI_BUS, parent, name);
107 return SSI_BUS(bus);
110 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
112 BusState *b = BUS(bus);
113 BusChild *kid;
114 SSISlaveClass *ssc;
115 uint32_t r = 0;
117 QTAILQ_FOREACH(kid, &b->children, sibling) {
118 SSISlave *slave = SSI_SLAVE(kid->child);
119 ssc = SSI_SLAVE_GET_CLASS(slave);
120 r |= ssc->transfer_raw(slave, val);
123 return r;
126 const VMStateDescription vmstate_ssi_slave = {
127 .name = "SSISlave",
128 .version_id = 1,
129 .minimum_version_id = 1,
130 .fields = (VMStateField[]) {
131 VMSTATE_BOOL(cs, SSISlave),
132 VMSTATE_END_OF_LIST()
136 static void ssi_slave_register_types(void)
138 type_register_static(&ssi_bus_info);
139 type_register_static(&ssi_slave_info);
142 type_init(ssi_slave_register_types)
144 typedef struct SSIAutoConnectArg {
145 qemu_irq **cs_linep;
146 SSIBus *bus;
147 } SSIAutoConnectArg;
149 static int ssi_auto_connect_slave(Object *child, void *opaque)
151 SSIAutoConnectArg *arg = opaque;
152 SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
153 qemu_irq cs_line;
155 if (!dev) {
156 return 0;
159 cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0);
160 qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus));
161 **arg->cs_linep = cs_line;
162 (*arg->cs_linep)++;
163 return 0;
166 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
167 SSIBus *bus)
169 SSIAutoConnectArg arg = {
170 .cs_linep = &cs_line,
171 .bus = bus
174 object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);