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