xref: /openbmc/qemu/hw/ssi/ssi.c (revision f7160f32)
1 /*
2  * QEMU Synchronous Serial Interface support
3  *
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
8  *
9  * This code is licensed under the GNU GPL v2.
10  *
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.
13  */
14 
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 
21 struct SSIBus {
22     BusState parent_obj;
23 };
24 
25 #define TYPE_SSI_BUS "SSI"
26 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
27 
28 static const TypeInfo ssi_bus_info = {
29     .name = TYPE_SSI_BUS,
30     .parent = TYPE_BUS,
31     .instance_size = sizeof(SSIBus),
32 };
33 
34 static void ssi_cs_default(void *opaque, int n, int level)
35 {
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);
43         }
44     }
45     s->cs = cs;
46 }
47 
48 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
49 {
50     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
51 
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);
56     }
57     return 0;
58 }
59 
60 static void ssi_slave_realize(DeviceState *dev, Error **errp)
61 {
62     SSISlave *s = SSI_SLAVE(dev);
63     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
64 
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);
68     }
69 
70     ssc->realize(s, errp);
71 }
72 
73 static void ssi_slave_class_init(ObjectClass *klass, void *data)
74 {
75     SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
76     DeviceClass *dc = DEVICE_CLASS(klass);
77 
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;
82     }
83 }
84 
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,
91 };
92 
93 bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)
94 {
95     return qdev_realize_and_unref(dev, &bus->parent_obj, errp);
96 }
97 
98 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
99 {
100     DeviceState *dev = qdev_new(name);
101 
102     ssi_realize_and_unref(dev, bus, &error_fatal);
103     return dev;
104 }
105 
106 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
107 {
108     BusState *bus;
109     bus = qbus_create(TYPE_SSI_BUS, parent, name);
110     return SSI_BUS(bus);
111 }
112 
113 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
114 {
115     BusState *b = BUS(bus);
116     BusChild *kid;
117     SSISlaveClass *ssc;
118     uint32_t r = 0;
119 
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);
124     }
125 
126     return r;
127 }
128 
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()
136     }
137 };
138 
139 static void ssi_slave_register_types(void)
140 {
141     type_register_static(&ssi_bus_info);
142     type_register_static(&ssi_slave_info);
143 }
144 
145 type_init(ssi_slave_register_types)
146