xref: /openbmc/qemu/hw/ssi/ssi.c (revision 93dd625f)
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 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
94 {
95     DeviceState *dev = qdev_new(name);
96 
97     qdev_realize_and_unref(dev, &bus->parent_obj, &error_fatal);
98     return dev;
99 }
100 
101 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
102 {
103     BusState *bus;
104     bus = qbus_create(TYPE_SSI_BUS, parent, name);
105     return SSI_BUS(bus);
106 }
107 
108 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
109 {
110     BusState *b = BUS(bus);
111     BusChild *kid;
112     SSISlaveClass *ssc;
113     uint32_t r = 0;
114 
115     QTAILQ_FOREACH(kid, &b->children, sibling) {
116         SSISlave *slave = SSI_SLAVE(kid->child);
117         ssc = SSI_SLAVE_GET_CLASS(slave);
118         r |= ssc->transfer_raw(slave, val);
119     }
120 
121     return r;
122 }
123 
124 const VMStateDescription vmstate_ssi_slave = {
125     .name = "SSISlave",
126     .version_id = 1,
127     .minimum_version_id = 1,
128     .fields = (VMStateField[]) {
129         VMSTATE_BOOL(cs, SSISlave),
130         VMSTATE_END_OF_LIST()
131     }
132 };
133 
134 static void ssi_slave_register_types(void)
135 {
136     type_register_static(&ssi_bus_info);
137     type_register_static(&ssi_slave_info);
138 }
139 
140 type_init(ssi_slave_register_types)
141