xref: /qemu/hw/i2c/core.c (revision db437ca6dfc8db3d91ee75babc4b1fee8c95e9ab)
10ff596d0Spbrook /*
20ff596d0Spbrook  * QEMU I2C bus interface.
30ff596d0Spbrook  *
40ff596d0Spbrook  * Copyright (c) 2007 CodeSourcery.
50ff596d0Spbrook  * Written by Paul Brook
60ff596d0Spbrook  *
78e31bf38SMatthew Fernandez  * This code is licensed under the LGPL.
80ff596d0Spbrook  */
90ff596d0Spbrook 
100430891cSPeter Maydell #include "qemu/osdep.h"
110d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
12a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
13d6454270SMarkus Armbruster #include "migration/vmstate.h"
143e80f690SMarkus Armbruster #include "qapi/error.h"
150b8fa32fSMarkus Armbruster #include "qemu/module.h"
1608bb9b34SPhilippe Mathieu-Daudé #include "trace.h"
170ff596d0Spbrook 
1871ae65e5SIgor Mammedov #define I2C_BROADCAST 0x00
1971ae65e5SIgor Mammedov 
203cb75a7cSPaolo Bonzini static Property i2c_props[] = {
213cb75a7cSPaolo Bonzini     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
223cb75a7cSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
233cb75a7cSPaolo Bonzini };
243cb75a7cSPaolo Bonzini 
250d936928SAnthony Liguori static const TypeInfo i2c_bus_info = {
260d936928SAnthony Liguori     .name = TYPE_I2C_BUS,
270d936928SAnthony Liguori     .parent = TYPE_BUS,
28a5c82852SAndreas Färber     .instance_size = sizeof(I2CBus),
2910c4c98aSGerd Hoffmann };
3010c4c98aSGerd Hoffmann 
3144b1ff31SDr. David Alan Gilbert static int i2c_bus_pre_save(void *opaque)
32c701b35bSpbrook {
33a5c82852SAndreas Färber     I2CBus *bus = opaque;
34c701b35bSpbrook 
352293c27fSKONRAD Frederic     bus->saved_address = -1;
362293c27fSKONRAD Frederic     if (!QLIST_EMPTY(&bus->current_devs)) {
372293c27fSKONRAD Frederic         if (!bus->broadcast) {
382293c27fSKONRAD Frederic             bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
3971ae65e5SIgor Mammedov         } else {
4071ae65e5SIgor Mammedov             bus->saved_address = I2C_BROADCAST;
41c701b35bSpbrook         }
422293c27fSKONRAD Frederic     }
4344b1ff31SDr. David Alan Gilbert 
4444b1ff31SDr. David Alan Gilbert     return 0;
45c701b35bSpbrook }
46c701b35bSpbrook 
478d0eb050SJuan Quintela static const VMStateDescription vmstate_i2c_bus = {
488d0eb050SJuan Quintela     .name = "i2c_bus",
498d0eb050SJuan Quintela     .version_id = 1,
508d0eb050SJuan Quintela     .minimum_version_id = 1,
518d0eb050SJuan Quintela     .pre_save = i2c_bus_pre_save,
528d0eb050SJuan Quintela     .fields = (VMStateField[]) {
53a5c82852SAndreas Färber         VMSTATE_UINT8(saved_address, I2CBus),
548d0eb050SJuan Quintela         VMSTATE_END_OF_LIST()
558d0eb050SJuan Quintela     }
568d0eb050SJuan Quintela };
578d0eb050SJuan Quintela 
580ff596d0Spbrook /* Create a new I2C bus.  */
59a5c82852SAndreas Färber I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
600ff596d0Spbrook {
61a5c82852SAndreas Färber     I2CBus *bus;
620ff596d0Spbrook 
63fef7fbc9SAndreas Färber     bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
642293c27fSKONRAD Frederic     QLIST_INIT(&bus->current_devs);
651df2c9a2SPeter Xu     vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_i2c_bus, bus);
660ff596d0Spbrook     return bus;
670ff596d0Spbrook }
680ff596d0Spbrook 
699e07bdf8SAnthony Liguori void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
700ff596d0Spbrook {
710ff596d0Spbrook     dev->address = address;
720ff596d0Spbrook }
730ff596d0Spbrook 
740ff596d0Spbrook /* Return nonzero if bus is busy.  */
75a5c82852SAndreas Färber int i2c_bus_busy(I2CBus *bus)
760ff596d0Spbrook {
772293c27fSKONRAD Frederic     return !QLIST_EMPTY(&bus->current_devs);
780ff596d0Spbrook }
790ff596d0Spbrook 
800ff596d0Spbrook /* TODO: Make this handle multiple masters.  */
81d307c28cSCorey Minyard /*
82d307c28cSCorey Minyard  * Start or continue an i2c transaction.  When this is called for the
83d307c28cSCorey Minyard  * first time or after an i2c_end_transfer(), if it returns an error
84d307c28cSCorey Minyard  * the bus transaction is terminated (or really never started).  If
85d307c28cSCorey Minyard  * this is called after another i2c_start_transfer() without an
86d307c28cSCorey Minyard  * intervening i2c_end_transfer(), and it returns an error, the
87d307c28cSCorey Minyard  * transaction will not be terminated.  The caller must do it.
88d307c28cSCorey Minyard  *
89d307c28cSCorey Minyard  * This corresponds with the way real hardware works.  The SMBus
90d307c28cSCorey Minyard  * protocol uses a start transfer to switch from write to read mode
91d307c28cSCorey Minyard  * without releasing the bus.  If that fails, the bus is still
92d307c28cSCorey Minyard  * in a transaction.
93d307c28cSCorey Minyard  */
94a5c82852SAndreas Färber int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
950ff596d0Spbrook {
960866aca1SAnthony Liguori     BusChild *kid;
97b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
982293c27fSKONRAD Frederic     I2CNode *node;
99d307c28cSCorey Minyard     bool bus_scanned = false;
1002293c27fSKONRAD Frederic 
10171ae65e5SIgor Mammedov     if (address == I2C_BROADCAST) {
1022293c27fSKONRAD Frederic         /*
1032293c27fSKONRAD Frederic          * This is a broadcast, the current_devs will be all the devices of the
1042293c27fSKONRAD Frederic          * bus.
1052293c27fSKONRAD Frederic          */
1062293c27fSKONRAD Frederic         bus->broadcast = true;
1072293c27fSKONRAD Frederic     }
1080ff596d0Spbrook 
1090fa758c3SCorey Minyard     /*
1100fa758c3SCorey Minyard      * If there are already devices in the list, that means we are in
1110fa758c3SCorey Minyard      * the middle of a transaction and we shouldn't rescan the bus.
1120fa758c3SCorey Minyard      *
1130fa758c3SCorey Minyard      * This happens with any SMBus transaction, even on a pure I2C
1140fa758c3SCorey Minyard      * device.  The interface does a transaction start without
1150fa758c3SCorey Minyard      * terminating the previous transaction.
1160fa758c3SCorey Minyard      */
1170fa758c3SCorey Minyard     if (QLIST_EMPTY(&bus->current_devs)) {
1180866aca1SAnthony Liguori         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
1190866aca1SAnthony Liguori             DeviceState *qdev = kid->child;
1208aae84a1SAndreas Färber             I2CSlave *candidate = I2C_SLAVE(qdev);
1212293c27fSKONRAD Frederic             if ((candidate->address == address) || (bus->broadcast)) {
1222293c27fSKONRAD Frederic                 node = g_malloc(sizeof(struct I2CNode));
1232293c27fSKONRAD Frederic                 node->elt = candidate;
1242293c27fSKONRAD Frederic                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
1252293c27fSKONRAD Frederic                 if (!bus->broadcast) {
1260ff596d0Spbrook                     break;
1270ff596d0Spbrook                 }
128b3a21988SJuha Riihimäki             }
1292293c27fSKONRAD Frederic         }
130d307c28cSCorey Minyard         bus_scanned = true;
1310fa758c3SCorey Minyard     }
1320ff596d0Spbrook 
1332293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
1340ff596d0Spbrook         return 1;
135b5ea9327SAnthony Liguori     }
1360ff596d0Spbrook 
1372293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
13808bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
139d307c28cSCorey Minyard         int rv;
140d307c28cSCorey Minyard 
14108bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
1420ff596d0Spbrook         /* If the bus is already busy, assume this is a repeated
1430ff596d0Spbrook            start condition.  */
144d307c28cSCorey Minyard 
145b5ea9327SAnthony Liguori         if (sc->event) {
14608bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("start", s->address);
14708bb9b34SPhilippe Mathieu-Daudé             rv = sc->event(s, recv ? I2C_START_RECV : I2C_START_SEND);
148d307c28cSCorey Minyard             if (rv && !bus->broadcast) {
149d307c28cSCorey Minyard                 if (bus_scanned) {
150d307c28cSCorey Minyard                     /* First call, terminate the transfer. */
151d307c28cSCorey Minyard                     i2c_end_transfer(bus);
152d307c28cSCorey Minyard                 }
153d307c28cSCorey Minyard                 return rv;
154d307c28cSCorey Minyard             }
1552293c27fSKONRAD Frederic         }
156b5ea9327SAnthony Liguori     }
1570ff596d0Spbrook     return 0;
1580ff596d0Spbrook }
1590ff596d0Spbrook 
160a5c82852SAndreas Färber void i2c_end_transfer(I2CBus *bus)
1610ff596d0Spbrook {
162b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1632293c27fSKONRAD Frederic     I2CNode *node, *next;
1640ff596d0Spbrook 
1652293c27fSKONRAD Frederic     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
16608bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
16708bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
168b5ea9327SAnthony Liguori         if (sc->event) {
16908bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("finish", s->address);
17008bb9b34SPhilippe Mathieu-Daudé             sc->event(s, I2C_FINISH);
171b5ea9327SAnthony Liguori         }
1722293c27fSKONRAD Frederic         QLIST_REMOVE(node, next);
1732293c27fSKONRAD Frederic         g_free(node);
1742293c27fSKONRAD Frederic     }
1752293c27fSKONRAD Frederic     bus->broadcast = false;
1760ff596d0Spbrook }
1770ff596d0Spbrook 
178056fca7bSPeter Crosthwaite int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
1790ff596d0Spbrook {
180b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
18108bb9b34SPhilippe Mathieu-Daudé     I2CSlave *s;
1822293c27fSKONRAD Frederic     I2CNode *node;
1832293c27fSKONRAD Frederic     int ret = 0;
1840ff596d0Spbrook 
185056fca7bSPeter Crosthwaite     if (send) {
1862293c27fSKONRAD Frederic         QLIST_FOREACH(node, &bus->current_devs, next) {
18708bb9b34SPhilippe Mathieu-Daudé             s = node->elt;
18808bb9b34SPhilippe Mathieu-Daudé             sc = I2C_SLAVE_GET_CLASS(s);
189b5ea9327SAnthony Liguori             if (sc->send) {
19008bb9b34SPhilippe Mathieu-Daudé                 trace_i2c_send(s->address, *data);
19108bb9b34SPhilippe Mathieu-Daudé                 ret = ret || sc->send(s, *data);
1922293c27fSKONRAD Frederic             } else {
1932293c27fSKONRAD Frederic                 ret = -1;
194b5ea9327SAnthony Liguori             }
1952293c27fSKONRAD Frederic         }
1962293c27fSKONRAD Frederic         return ret ? -1 : 0;
197056fca7bSPeter Crosthwaite     } else {
1982ac4c5f4SCorey Minyard         ret = 0xff;
1992ac4c5f4SCorey Minyard         if (!QLIST_EMPTY(&bus->current_devs) && !bus->broadcast) {
2002293c27fSKONRAD Frederic             sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
201b5ea9327SAnthony Liguori             if (sc->recv) {
20208bb9b34SPhilippe Mathieu-Daudé                 s = QLIST_FIRST(&bus->current_devs)->elt;
20308bb9b34SPhilippe Mathieu-Daudé                 ret = sc->recv(s);
20408bb9b34SPhilippe Mathieu-Daudé                 trace_i2c_recv(s->address, ret);
2052ac4c5f4SCorey Minyard             }
2062ac4c5f4SCorey Minyard         }
207056fca7bSPeter Crosthwaite         *data = ret;
208056fca7bSPeter Crosthwaite         return 0;
209056fca7bSPeter Crosthwaite     }
210b5ea9327SAnthony Liguori }
211056fca7bSPeter Crosthwaite 
212056fca7bSPeter Crosthwaite int i2c_send(I2CBus *bus, uint8_t data)
213056fca7bSPeter Crosthwaite {
214056fca7bSPeter Crosthwaite     return i2c_send_recv(bus, &data, true);
215056fca7bSPeter Crosthwaite }
216056fca7bSPeter Crosthwaite 
2172ac4c5f4SCorey Minyard uint8_t i2c_recv(I2CBus *bus)
218056fca7bSPeter Crosthwaite {
2192ac4c5f4SCorey Minyard     uint8_t data = 0xff;
220056fca7bSPeter Crosthwaite 
2212ac4c5f4SCorey Minyard     i2c_send_recv(bus, &data, false);
2222ac4c5f4SCorey Minyard     return data;
223056fca7bSPeter Crosthwaite }
2240ff596d0Spbrook 
225a5c82852SAndreas Färber void i2c_nack(I2CBus *bus)
2260ff596d0Spbrook {
227b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
2282293c27fSKONRAD Frederic     I2CNode *node;
2290ff596d0Spbrook 
2302293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
2310ff596d0Spbrook         return;
232b5ea9327SAnthony Liguori     }
2330ff596d0Spbrook 
2342293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
2352293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
236b5ea9327SAnthony Liguori         if (sc->event) {
23708bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("nack", node->elt->address);
2382293c27fSKONRAD Frederic             sc->event(node->elt, I2C_NACK);
2392293c27fSKONRAD Frederic         }
240b5ea9327SAnthony Liguori     }
2410ff596d0Spbrook }
2420ff596d0Spbrook 
243bcbe8068SJuan Quintela static int i2c_slave_post_load(void *opaque, int version_id)
244bcbe8068SJuan Quintela {
2459e07bdf8SAnthony Liguori     I2CSlave *dev = opaque;
246a5c82852SAndreas Färber     I2CBus *bus;
2472293c27fSKONRAD Frederic     I2CNode *node;
2482293c27fSKONRAD Frederic 
249fef7fbc9SAndreas Färber     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
25071ae65e5SIgor Mammedov     if ((bus->saved_address == dev->address) ||
25171ae65e5SIgor Mammedov         (bus->saved_address == I2C_BROADCAST)) {
2522293c27fSKONRAD Frederic         node = g_malloc(sizeof(struct I2CNode));
2532293c27fSKONRAD Frederic         node->elt = dev;
2542293c27fSKONRAD Frederic         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
255bcbe8068SJuan Quintela     }
256bcbe8068SJuan Quintela     return 0;
257bcbe8068SJuan Quintela }
258bcbe8068SJuan Quintela 
2591894839fSJuan Quintela const VMStateDescription vmstate_i2c_slave = {
2609e07bdf8SAnthony Liguori     .name = "I2CSlave",
261bcbe8068SJuan Quintela     .version_id = 1,
262bcbe8068SJuan Quintela     .minimum_version_id = 1,
263bcbe8068SJuan Quintela     .post_load = i2c_slave_post_load,
264bcbe8068SJuan Quintela     .fields = (VMStateField[]) {
2659e07bdf8SAnthony Liguori         VMSTATE_UINT8(address, I2CSlave),
266bcbe8068SJuan Quintela         VMSTATE_END_OF_LIST()
267bcbe8068SJuan Quintela     }
268bcbe8068SJuan Quintela };
269bcbe8068SJuan Quintela 
270*db437ca6SPhilippe Mathieu-Daudé I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
271fe8de492SPaul Brook {
272fe8de492SPaul Brook     DeviceState *dev;
273fe8de492SPaul Brook 
2743e80f690SMarkus Armbruster     dev = qdev_new(name);
2755b7f5327SJuan Quintela     qdev_prop_set_uint8(dev, "address", addr);
276*db437ca6SPhilippe Mathieu-Daudé     return I2C_SLAVE(dev);
277d88c42ffSPhilippe Mathieu-Daudé }
278d88c42ffSPhilippe Mathieu-Daudé 
279d88c42ffSPhilippe Mathieu-Daudé bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
280d88c42ffSPhilippe Mathieu-Daudé {
281d88c42ffSPhilippe Mathieu-Daudé     return qdev_realize_and_unref(dev, &bus->qbus, errp);
282d88c42ffSPhilippe Mathieu-Daudé }
283d88c42ffSPhilippe Mathieu-Daudé 
284d88c42ffSPhilippe Mathieu-Daudé DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
285d88c42ffSPhilippe Mathieu-Daudé {
286*db437ca6SPhilippe Mathieu-Daudé     I2CSlave *dev = i2c_slave_new(name, addr);
287d88c42ffSPhilippe Mathieu-Daudé 
288*db437ca6SPhilippe Mathieu-Daudé     i2c_realize_and_unref(DEVICE(dev), bus, &error_fatal);
289d88c42ffSPhilippe Mathieu-Daudé 
290*db437ca6SPhilippe Mathieu-Daudé     return DEVICE(dev);
291aa941b94Sbalrog }
292b5ea9327SAnthony Liguori 
29339bffca2SAnthony Liguori static void i2c_slave_class_init(ObjectClass *klass, void *data)
29439bffca2SAnthony Liguori {
29539bffca2SAnthony Liguori     DeviceClass *k = DEVICE_CLASS(klass);
296125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_MISC, k->categories);
2970d936928SAnthony Liguori     k->bus_type = TYPE_I2C_BUS;
2984f67d30bSMarc-André Lureau     device_class_set_props(k, i2c_props);
29939bffca2SAnthony Liguori }
30039bffca2SAnthony Liguori 
3018c43a6f0SAndreas Färber static const TypeInfo i2c_slave_type_info = {
302b5ea9327SAnthony Liguori     .name = TYPE_I2C_SLAVE,
303b5ea9327SAnthony Liguori     .parent = TYPE_DEVICE,
304b5ea9327SAnthony Liguori     .instance_size = sizeof(I2CSlave),
305b5ea9327SAnthony Liguori     .abstract = true,
306b5ea9327SAnthony Liguori     .class_size = sizeof(I2CSlaveClass),
30739bffca2SAnthony Liguori     .class_init = i2c_slave_class_init,
308b5ea9327SAnthony Liguori };
309b5ea9327SAnthony Liguori 
31083f7d43aSAndreas Färber static void i2c_slave_register_types(void)
311b5ea9327SAnthony Liguori {
3120d936928SAnthony Liguori     type_register_static(&i2c_bus_info);
313b5ea9327SAnthony Liguori     type_register_static(&i2c_slave_type_info);
314b5ea9327SAnthony Liguori }
315b5ea9327SAnthony Liguori 
31683f7d43aSAndreas Färber type_init(i2c_slave_register_types)
317