xref: /openbmc/qemu/hw/ssi/ssi.c (revision a9ded601)
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 
18 struct SSIBus {
19     BusState parent_obj;
20 };
21 
22 #define TYPE_SSI_BUS "SSI"
23 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
24 
25 static const TypeInfo ssi_bus_info = {
26     .name = TYPE_SSI_BUS,
27     .parent = TYPE_BUS,
28     .instance_size = sizeof(SSIBus),
29 };
30 
31 static void ssi_cs_default(void *opaque, int n, int level)
32 {
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);
40         }
41     }
42     s->cs = cs;
43 }
44 
45 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
46 {
47     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
48 
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);
53     }
54     return 0;
55 }
56 
57 static void ssi_slave_realize(DeviceState *dev, Error **errp)
58 {
59     SSISlave *s = SSI_SLAVE(dev);
60     SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
61 
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);
65     }
66 
67     ssc->realize(s, errp);
68 }
69 
70 static void ssi_slave_class_init(ObjectClass *klass, void *data)
71 {
72     SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
73     DeviceClass *dc = DEVICE_CLASS(klass);
74 
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;
79     }
80 }
81 
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,
88 };
89 
90 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
91 {
92     return qdev_create(BUS(bus), name);
93 }
94 
95 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
96 {
97     DeviceState *dev = ssi_create_slave_no_init(bus, name);
98 
99     qdev_init_nofail(dev);
100     return dev;
101 }
102 
103 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
104 {
105     BusState *bus;
106     bus = qbus_create(TYPE_SSI_BUS, parent, name);
107     return SSI_BUS(bus);
108 }
109 
110 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
111 {
112     BusState *b = BUS(bus);
113     BusChild *kid;
114     SSISlaveClass *ssc;
115     uint32_t r = 0;
116 
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);
121     }
122 
123     return r;
124 }
125 
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()
133     }
134 };
135 
136 static void ssi_slave_register_types(void)
137 {
138     type_register_static(&ssi_bus_info);
139     type_register_static(&ssi_slave_info);
140 }
141 
142 type_init(ssi_slave_register_types)
143 
144 typedef struct SSIAutoConnectArg {
145     qemu_irq **cs_linep;
146     SSIBus *bus;
147 } SSIAutoConnectArg;
148 
149 static int ssi_auto_connect_slave(Object *child, void *opaque)
150 {
151     SSIAutoConnectArg *arg = opaque;
152     SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
153     qemu_irq cs_line;
154 
155     if (!dev) {
156         return 0;
157     }
158 
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;
164 }
165 
166 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
167                              SSIBus *bus)
168 {
169     SSIAutoConnectArg arg = {
170         .cs_linep = &cs_line,
171         .bus = bus
172     };
173 
174     object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);
175 }
176