xref: /openbmc/qemu/hw/ssi/ssi.c (revision 0d5fae3e)
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 
20 struct SSIBus {
21     BusState parent_obj;
22 };
23 
24 #define TYPE_SSI_BUS "SSI"
25 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
26 
27 static const TypeInfo ssi_bus_info = {
28     .name = TYPE_SSI_BUS,
29     .parent = TYPE_BUS,
30     .instance_size = sizeof(SSIBus),
31 };
32 
33 static void ssi_cs_default(void *opaque, int n, int level)
34 {
35     SSISlave *s = SSI_SLAVE(opaque);
36     bool cs = !!level;
37     assert(n == 0);
38     if (s->cs != cs) {
39         SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
40         if (ssc->set_cs) {
41             ssc->set_cs(s, cs);
42         }
43     }
44     s->cs = cs;
45 }
46 
47 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
48 {
49     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
50 
51     if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
52             (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
53             ssc->cs_polarity == SSI_CS_NONE) {
54         return ssc->transfer(dev, val);
55     }
56     return 0;
57 }
58 
59 static void ssi_slave_realize(DeviceState *dev, Error **errp)
60 {
61     SSISlave *s = SSI_SLAVE(dev);
62     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
63 
64     if (ssc->transfer_raw == ssi_transfer_raw_default &&
65             ssc->cs_polarity != SSI_CS_NONE) {
66         qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
67     }
68 
69     ssc->realize(s, errp);
70 }
71 
72 static void ssi_slave_class_init(ObjectClass *klass, void *data)
73 {
74     SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
75     DeviceClass *dc = DEVICE_CLASS(klass);
76 
77     dc->realize = ssi_slave_realize;
78     dc->bus_type = TYPE_SSI_BUS;
79     if (!ssc->transfer_raw) {
80         ssc->transfer_raw = ssi_transfer_raw_default;
81     }
82 }
83 
84 static const TypeInfo ssi_slave_info = {
85     .name = TYPE_SSI_SLAVE,
86     .parent = TYPE_DEVICE,
87     .class_init = ssi_slave_class_init,
88     .class_size = sizeof(SSISlaveClass),
89     .abstract = true,
90 };
91 
92 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
93 {
94     return qdev_create(BUS(bus), name);
95 }
96 
97 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
98 {
99     DeviceState *dev = ssi_create_slave_no_init(bus, name);
100 
101     qdev_init_nofail(dev);
102     return dev;
103 }
104 
105 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
106 {
107     BusState *bus;
108     bus = qbus_create(TYPE_SSI_BUS, parent, name);
109     return SSI_BUS(bus);
110 }
111 
112 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
113 {
114     BusState *b = BUS(bus);
115     BusChild *kid;
116     SSISlaveClass *ssc;
117     uint32_t r = 0;
118 
119     QTAILQ_FOREACH(kid, &b->children, sibling) {
120         SSISlave *slave = SSI_SLAVE(kid->child);
121         ssc = SSI_SLAVE_GET_CLASS(slave);
122         r |= ssc->transfer_raw(slave, val);
123     }
124 
125     return r;
126 }
127 
128 const VMStateDescription vmstate_ssi_slave = {
129     .name = "SSISlave",
130     .version_id = 1,
131     .minimum_version_id = 1,
132     .fields = (VMStateField[]) {
133         VMSTATE_BOOL(cs, SSISlave),
134         VMSTATE_END_OF_LIST()
135     }
136 };
137 
138 static void ssi_slave_register_types(void)
139 {
140     type_register_static(&ssi_bus_info);
141     type_register_static(&ssi_slave_info);
142 }
143 
144 type_init(ssi_slave_register_types)
145 
146 typedef struct SSIAutoConnectArg {
147     qemu_irq **cs_linep;
148     SSIBus *bus;
149 } SSIAutoConnectArg;
150 
151 static int ssi_auto_connect_slave(Object *child, void *opaque)
152 {
153     SSIAutoConnectArg *arg = opaque;
154     SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
155     qemu_irq cs_line;
156 
157     if (!dev) {
158         return 0;
159     }
160 
161     cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0);
162     qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus));
163     **arg->cs_linep = cs_line;
164     (*arg->cs_linep)++;
165     return 0;
166 }
167 
168 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
169                              SSIBus *bus)
170 {
171     SSIAutoConnectArg arg = {
172         .cs_linep = &cs_line,
173         .bus = bus
174     };
175 
176     object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);
177 }
178