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