qapi/error: Check format string argument in error_*prepend()
[qemu/armbru.git] / hw / ssi / ssi.c
bloba35d7ebb266893390ce78e971dc7710cf4e0d2ee
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"
21 struct SSIBus {
22 BusState parent_obj;
25 #define TYPE_SSI_BUS "SSI"
26 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
28 static const TypeInfo ssi_bus_info = {
29 .name = TYPE_SSI_BUS,
30 .parent = TYPE_BUS,
31 .instance_size = sizeof(SSIBus),
34 static void ssi_cs_default(void *opaque, int n, int level)
36 SSISlave *s = SSI_SLAVE(opaque);
37 bool cs = !!level;
38 assert(n == 0);
39 if (s->cs != cs) {
40 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
41 if (ssc->set_cs) {
42 ssc->set_cs(s, cs);
45 s->cs = cs;
48 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
50 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
52 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
53 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
54 ssc->cs_polarity == SSI_CS_NONE) {
55 return ssc->transfer(dev, val);
57 return 0;
60 static void ssi_slave_realize(DeviceState *dev, Error **errp)
62 SSISlave *s = SSI_SLAVE(dev);
63 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
65 if (ssc->transfer_raw == ssi_transfer_raw_default &&
66 ssc->cs_polarity != SSI_CS_NONE) {
67 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
70 ssc->realize(s, errp);
73 static void ssi_slave_class_init(ObjectClass *klass, void *data)
75 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
76 DeviceClass *dc = DEVICE_CLASS(klass);
78 dc->realize = ssi_slave_realize;
79 dc->bus_type = TYPE_SSI_BUS;
80 if (!ssc->transfer_raw) {
81 ssc->transfer_raw = ssi_transfer_raw_default;
85 static const TypeInfo ssi_slave_info = {
86 .name = TYPE_SSI_SLAVE,
87 .parent = TYPE_DEVICE,
88 .class_init = ssi_slave_class_init,
89 .class_size = sizeof(SSISlaveClass),
90 .abstract = true,
93 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)
95 return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
98 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
100 DeviceState *dev = qdev_new(name);
102 ssi_realize_and_unref(dev, bus, &error_fatal);
103 return dev;
106 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
108 BusState *bus;
109 bus = qbus_create(TYPE_SSI_BUS, parent, name);
110 return SSI_BUS(bus);
113 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
115 BusState *b = BUS(bus);
116 BusChild *kid;
117 SSISlaveClass *ssc;
118 uint32_t r = 0;
120 QTAILQ_FOREACH(kid, &b->children, sibling) {
121 SSISlave *slave = SSI_SLAVE(kid->child);
122 ssc = SSI_SLAVE_GET_CLASS(slave);
123 r |= ssc->transfer_raw(slave, val);
126 return r;
129 const VMStateDescription vmstate_ssi_slave = {
130 .name = "SSISlave",
131 .version_id = 1,
132 .minimum_version_id = 1,
133 .fields = (VMStateField[]) {
134 VMSTATE_BOOL(cs, SSISlave),
135 VMSTATE_END_OF_LIST()
139 static void ssi_slave_register_types(void)
141 type_register_static(&ssi_bus_info);
142 type_register_static(&ssi_slave_info);
145 type_init(ssi_slave_register_types)