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