hw/nvme: fix controller hot unplugging
[qemu/ar7.git] / hw / ssi / ssi.c
blobe5d7ce95237762b666d0fa81cea26694c2321269
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 OBJECT_DECLARE_SIMPLE_TYPE(SSIBus, SSI_BUS)
29 static const TypeInfo ssi_bus_info = {
30 .name = TYPE_SSI_BUS,
31 .parent = TYPE_BUS,
32 .instance_size = sizeof(SSIBus),
35 static void ssi_cs_default(void *opaque, int n, int level)
37 SSIPeripheral *s = SSI_PERIPHERAL(opaque);
38 bool cs = !!level;
39 assert(n == 0);
40 if (s->cs != cs) {
41 SSIPeripheralClass *ssc = SSI_PERIPHERAL_GET_CLASS(s);
42 if (ssc->set_cs) {
43 ssc->set_cs(s, cs);
46 s->cs = cs;
49 static uint32_t ssi_transfer_raw_default(SSIPeripheral *dev, uint32_t val)
51 SSIPeripheralClass *ssc = SSI_PERIPHERAL_GET_CLASS(dev);
53 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
54 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
55 ssc->cs_polarity == SSI_CS_NONE) {
56 return ssc->transfer(dev, val);
58 return 0;
61 static void ssi_peripheral_realize(DeviceState *dev, Error **errp)
63 SSIPeripheral *s = SSI_PERIPHERAL(dev);
64 SSIPeripheralClass *ssc = SSI_PERIPHERAL_GET_CLASS(s);
66 if (ssc->transfer_raw == ssi_transfer_raw_default &&
67 ssc->cs_polarity != SSI_CS_NONE) {
68 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
71 ssc->realize(s, errp);
74 static void ssi_peripheral_class_init(ObjectClass *klass, void *data)
76 SSIPeripheralClass *ssc = SSI_PERIPHERAL_CLASS(klass);
77 DeviceClass *dc = DEVICE_CLASS(klass);
79 dc->realize = ssi_peripheral_realize;
80 dc->bus_type = TYPE_SSI_BUS;
81 if (!ssc->transfer_raw) {
82 ssc->transfer_raw = ssi_transfer_raw_default;
86 static const TypeInfo ssi_peripheral_info = {
87 .name = TYPE_SSI_PERIPHERAL,
88 .parent = TYPE_DEVICE,
89 .class_init = ssi_peripheral_class_init,
90 .class_size = sizeof(SSIPeripheralClass),
91 .abstract = true,
94 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)
96 return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
99 DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name)
101 DeviceState *dev = qdev_new(name);
103 ssi_realize_and_unref(dev, bus, &error_fatal);
104 return dev;
107 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
109 BusState *bus;
110 bus = qbus_create(TYPE_SSI_BUS, parent, name);
111 return SSI_BUS(bus);
114 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
116 BusState *b = BUS(bus);
117 BusChild *kid;
118 SSIPeripheralClass *ssc;
119 uint32_t r = 0;
121 QTAILQ_FOREACH(kid, &b->children, sibling) {
122 SSIPeripheral *peripheral = SSI_PERIPHERAL(kid->child);
123 ssc = SSI_PERIPHERAL_GET_CLASS(peripheral);
124 r |= ssc->transfer_raw(peripheral, val);
127 return r;
130 const VMStateDescription vmstate_ssi_peripheral = {
131 .name = "SSISlave",
132 .version_id = 1,
133 .minimum_version_id = 1,
134 .fields = (VMStateField[]) {
135 VMSTATE_BOOL(cs, SSIPeripheral),
136 VMSTATE_END_OF_LIST()
140 static void ssi_peripheral_register_types(void)
142 type_register_static(&ssi_bus_info);
143 type_register_static(&ssi_peripheral_info);
146 type_init(ssi_peripheral_register_types)