xref: /openbmc/qemu/hw/i2c/core.c (revision 0fa758c3a069bc59a0d903d69028971c46d1a119)
1 /*
2  * QEMU I2C bus interface.
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the LGPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 
13 typedef struct I2CNode I2CNode;
14 
15 struct I2CNode {
16     I2CSlave *elt;
17     QLIST_ENTRY(I2CNode) next;
18 };
19 
20 #define I2C_BROADCAST 0x00
21 
22 struct I2CBus
23 {
24     BusState qbus;
25     QLIST_HEAD(, I2CNode) current_devs;
26     uint8_t saved_address;
27     bool broadcast;
28 };
29 
30 static Property i2c_props[] = {
31     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
32     DEFINE_PROP_END_OF_LIST(),
33 };
34 
35 #define TYPE_I2C_BUS "i2c-bus"
36 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
37 
38 static const TypeInfo i2c_bus_info = {
39     .name = TYPE_I2C_BUS,
40     .parent = TYPE_BUS,
41     .instance_size = sizeof(I2CBus),
42 };
43 
44 static void i2c_bus_pre_save(void *opaque)
45 {
46     I2CBus *bus = opaque;
47 
48     bus->saved_address = -1;
49     if (!QLIST_EMPTY(&bus->current_devs)) {
50         if (!bus->broadcast) {
51             bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
52         } else {
53             bus->saved_address = I2C_BROADCAST;
54         }
55     }
56 }
57 
58 static const VMStateDescription vmstate_i2c_bus = {
59     .name = "i2c_bus",
60     .version_id = 1,
61     .minimum_version_id = 1,
62     .pre_save = i2c_bus_pre_save,
63     .fields = (VMStateField[]) {
64         VMSTATE_UINT8(saved_address, I2CBus),
65         VMSTATE_END_OF_LIST()
66     }
67 };
68 
69 /* Create a new I2C bus.  */
70 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
71 {
72     I2CBus *bus;
73 
74     bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
75     QLIST_INIT(&bus->current_devs);
76     vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
77     return bus;
78 }
79 
80 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
81 {
82     dev->address = address;
83 }
84 
85 /* Return nonzero if bus is busy.  */
86 int i2c_bus_busy(I2CBus *bus)
87 {
88     return !QLIST_EMPTY(&bus->current_devs);
89 }
90 
91 /* Returns non-zero if the address is not valid.  */
92 /* TODO: Make this handle multiple masters.  */
93 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
94 {
95     BusChild *kid;
96     I2CSlaveClass *sc;
97     I2CNode *node;
98 
99     if (address == I2C_BROADCAST) {
100         /*
101          * This is a broadcast, the current_devs will be all the devices of the
102          * bus.
103          */
104         bus->broadcast = true;
105     }
106 
107     /*
108      * If there are already devices in the list, that means we are in
109      * the middle of a transaction and we shouldn't rescan the bus.
110      *
111      * This happens with any SMBus transaction, even on a pure I2C
112      * device.  The interface does a transaction start without
113      * terminating the previous transaction.
114      */
115     if (QLIST_EMPTY(&bus->current_devs)) {
116         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
117             DeviceState *qdev = kid->child;
118             I2CSlave *candidate = I2C_SLAVE(qdev);
119             if ((candidate->address == address) || (bus->broadcast)) {
120                 node = g_malloc(sizeof(struct I2CNode));
121                 node->elt = candidate;
122                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
123                 if (!bus->broadcast) {
124                     break;
125                 }
126             }
127         }
128     }
129 
130     if (QLIST_EMPTY(&bus->current_devs)) {
131         return 1;
132     }
133 
134     QLIST_FOREACH(node, &bus->current_devs, next) {
135         sc = I2C_SLAVE_GET_CLASS(node->elt);
136         /* If the bus is already busy, assume this is a repeated
137            start condition.  */
138         if (sc->event) {
139             sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
140         }
141     }
142     return 0;
143 }
144 
145 void i2c_end_transfer(I2CBus *bus)
146 {
147     I2CSlaveClass *sc;
148     I2CNode *node, *next;
149 
150     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
151         sc = I2C_SLAVE_GET_CLASS(node->elt);
152         if (sc->event) {
153             sc->event(node->elt, I2C_FINISH);
154         }
155         QLIST_REMOVE(node, next);
156         g_free(node);
157     }
158     bus->broadcast = false;
159 }
160 
161 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
162 {
163     I2CSlaveClass *sc;
164     I2CNode *node;
165     int ret = 0;
166 
167     if (send) {
168         QLIST_FOREACH(node, &bus->current_devs, next) {
169             sc = I2C_SLAVE_GET_CLASS(node->elt);
170             if (sc->send) {
171                 ret = ret || sc->send(node->elt, *data);
172             } else {
173                 ret = -1;
174             }
175         }
176         return ret ? -1 : 0;
177     } else {
178         if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
179             return -1;
180         }
181 
182         sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
183         if (sc->recv) {
184             ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
185             if (ret < 0) {
186                 return ret;
187             } else {
188                 *data = ret;
189                 return 0;
190             }
191         }
192         return -1;
193     }
194 }
195 
196 int i2c_send(I2CBus *bus, uint8_t data)
197 {
198     return i2c_send_recv(bus, &data, true);
199 }
200 
201 int i2c_recv(I2CBus *bus)
202 {
203     uint8_t data;
204     int ret = i2c_send_recv(bus, &data, false);
205 
206     return ret < 0 ? ret : data;
207 }
208 
209 void i2c_nack(I2CBus *bus)
210 {
211     I2CSlaveClass *sc;
212     I2CNode *node;
213 
214     if (QLIST_EMPTY(&bus->current_devs)) {
215         return;
216     }
217 
218     QLIST_FOREACH(node, &bus->current_devs, next) {
219         sc = I2C_SLAVE_GET_CLASS(node->elt);
220         if (sc->event) {
221             sc->event(node->elt, I2C_NACK);
222         }
223     }
224 }
225 
226 static int i2c_slave_post_load(void *opaque, int version_id)
227 {
228     I2CSlave *dev = opaque;
229     I2CBus *bus;
230     I2CNode *node;
231 
232     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
233     if ((bus->saved_address == dev->address) ||
234         (bus->saved_address == I2C_BROADCAST)) {
235         node = g_malloc(sizeof(struct I2CNode));
236         node->elt = dev;
237         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
238     }
239     return 0;
240 }
241 
242 const VMStateDescription vmstate_i2c_slave = {
243     .name = "I2CSlave",
244     .version_id = 1,
245     .minimum_version_id = 1,
246     .post_load = i2c_slave_post_load,
247     .fields = (VMStateField[]) {
248         VMSTATE_UINT8(address, I2CSlave),
249         VMSTATE_END_OF_LIST()
250     }
251 };
252 
253 static int i2c_slave_qdev_init(DeviceState *dev)
254 {
255     I2CSlave *s = I2C_SLAVE(dev);
256     I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
257 
258     return sc->init(s);
259 }
260 
261 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
262 {
263     DeviceState *dev;
264 
265     dev = qdev_create(&bus->qbus, name);
266     qdev_prop_set_uint8(dev, "address", addr);
267     qdev_init_nofail(dev);
268     return dev;
269 }
270 
271 static void i2c_slave_class_init(ObjectClass *klass, void *data)
272 {
273     DeviceClass *k = DEVICE_CLASS(klass);
274     k->init = i2c_slave_qdev_init;
275     set_bit(DEVICE_CATEGORY_MISC, k->categories);
276     k->bus_type = TYPE_I2C_BUS;
277     k->props = i2c_props;
278 }
279 
280 static const TypeInfo i2c_slave_type_info = {
281     .name = TYPE_I2C_SLAVE,
282     .parent = TYPE_DEVICE,
283     .instance_size = sizeof(I2CSlave),
284     .abstract = true,
285     .class_size = sizeof(I2CSlaveClass),
286     .class_init = i2c_slave_class_init,
287 };
288 
289 static void i2c_slave_register_types(void)
290 {
291     type_register_static(&i2c_bus_info);
292     type_register_static(&i2c_slave_type_info);
293 }
294 
295 type_init(i2c_slave_register_types)
296