xref: /qemu/hw/i2c/core.c (revision b21e2380376c470900fcadf47507f4d5ade75e85)
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 
639388d170SPeter Maydell     bus = I2C_BUS(qbus_new(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 
69c8665a59SPhilippe Mathieu-Daudé void i2c_slave_set_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 
803f9b3259SPatrick Venture bool i2c_scan_bus(I2CBus *bus, uint8_t address, bool broadcast,
813f9b3259SPatrick Venture                   I2CNodeList *current_devs)
823f9b3259SPatrick Venture {
833f9b3259SPatrick Venture     BusChild *kid;
843f9b3259SPatrick Venture 
853f9b3259SPatrick Venture     QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
863f9b3259SPatrick Venture         DeviceState *qdev = kid->child;
873f9b3259SPatrick Venture         I2CSlave *candidate = I2C_SLAVE(qdev);
883f9b3259SPatrick Venture         I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(candidate);
893f9b3259SPatrick Venture 
903f9b3259SPatrick Venture         if (sc->match_and_add(candidate, address, broadcast, current_devs)) {
913f9b3259SPatrick Venture             if (!broadcast) {
923f9b3259SPatrick Venture                 return true;
933f9b3259SPatrick Venture             }
943f9b3259SPatrick Venture         }
953f9b3259SPatrick Venture     }
963f9b3259SPatrick Venture 
973f9b3259SPatrick Venture     /*
983f9b3259SPatrick Venture      * If broadcast was true, and the list was full or empty, return true. If
993f9b3259SPatrick Venture      * broadcast was false, return false.
1003f9b3259SPatrick Venture      */
1013f9b3259SPatrick Venture     return broadcast;
1023f9b3259SPatrick Venture }
1033f9b3259SPatrick Venture 
1040ff596d0Spbrook /* TODO: Make this handle multiple masters.  */
105d307c28cSCorey Minyard /*
106d307c28cSCorey Minyard  * Start or continue an i2c transaction.  When this is called for the
107d307c28cSCorey Minyard  * first time or after an i2c_end_transfer(), if it returns an error
108d307c28cSCorey Minyard  * the bus transaction is terminated (or really never started).  If
109d307c28cSCorey Minyard  * this is called after another i2c_start_transfer() without an
110d307c28cSCorey Minyard  * intervening i2c_end_transfer(), and it returns an error, the
111d307c28cSCorey Minyard  * transaction will not be terminated.  The caller must do it.
112d307c28cSCorey Minyard  *
113d307c28cSCorey Minyard  * This corresponds with the way real hardware works.  The SMBus
114d307c28cSCorey Minyard  * protocol uses a start transfer to switch from write to read mode
115d307c28cSCorey Minyard  * without releasing the bus.  If that fails, the bus is still
116d307c28cSCorey Minyard  * in a transaction.
117265caf45SPhilippe Mathieu-Daudé  *
118265caf45SPhilippe Mathieu-Daudé  * @event must be I2C_START_RECV or I2C_START_SEND.
119d307c28cSCorey Minyard  */
120265caf45SPhilippe Mathieu-Daudé static int i2c_do_start_transfer(I2CBus *bus, uint8_t address,
121265caf45SPhilippe Mathieu-Daudé                                  enum i2c_event event)
1220ff596d0Spbrook {
123b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1242293c27fSKONRAD Frederic     I2CNode *node;
125d307c28cSCorey Minyard     bool bus_scanned = false;
1262293c27fSKONRAD Frederic 
12771ae65e5SIgor Mammedov     if (address == I2C_BROADCAST) {
1282293c27fSKONRAD Frederic         /*
1292293c27fSKONRAD Frederic          * This is a broadcast, the current_devs will be all the devices of the
1302293c27fSKONRAD Frederic          * bus.
1312293c27fSKONRAD Frederic          */
1322293c27fSKONRAD Frederic         bus->broadcast = true;
1332293c27fSKONRAD Frederic     }
1340ff596d0Spbrook 
1350fa758c3SCorey Minyard     /*
1360fa758c3SCorey Minyard      * If there are already devices in the list, that means we are in
1370fa758c3SCorey Minyard      * the middle of a transaction and we shouldn't rescan the bus.
1380fa758c3SCorey Minyard      *
1390fa758c3SCorey Minyard      * This happens with any SMBus transaction, even on a pure I2C
1400fa758c3SCorey Minyard      * device.  The interface does a transaction start without
1410fa758c3SCorey Minyard      * terminating the previous transaction.
1420fa758c3SCorey Minyard      */
1430fa758c3SCorey Minyard     if (QLIST_EMPTY(&bus->current_devs)) {
1443f9b3259SPatrick Venture         /* Disregard whether devices were found. */
1453f9b3259SPatrick Venture         (void)i2c_scan_bus(bus, address, bus->broadcast, &bus->current_devs);
146d307c28cSCorey Minyard         bus_scanned = true;
1470fa758c3SCorey Minyard     }
1480ff596d0Spbrook 
1492293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
1500ff596d0Spbrook         return 1;
151b5ea9327SAnthony Liguori     }
1520ff596d0Spbrook 
1532293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
15408bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
155d307c28cSCorey Minyard         int rv;
156d307c28cSCorey Minyard 
15708bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
1580ff596d0Spbrook         /* If the bus is already busy, assume this is a repeated
1590ff596d0Spbrook            start condition.  */
160d307c28cSCorey Minyard 
161b5ea9327SAnthony Liguori         if (sc->event) {
16208bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("start", s->address);
163265caf45SPhilippe Mathieu-Daudé             rv = sc->event(s, event);
164d307c28cSCorey Minyard             if (rv && !bus->broadcast) {
165d307c28cSCorey Minyard                 if (bus_scanned) {
166d307c28cSCorey Minyard                     /* First call, terminate the transfer. */
167d307c28cSCorey Minyard                     i2c_end_transfer(bus);
168d307c28cSCorey Minyard                 }
169d307c28cSCorey Minyard                 return rv;
170d307c28cSCorey Minyard             }
1712293c27fSKONRAD Frederic         }
172b5ea9327SAnthony Liguori     }
1730ff596d0Spbrook     return 0;
1740ff596d0Spbrook }
1750ff596d0Spbrook 
176265caf45SPhilippe Mathieu-Daudé int i2c_start_transfer(I2CBus *bus, uint8_t address, bool is_recv)
177265caf45SPhilippe Mathieu-Daudé {
178265caf45SPhilippe Mathieu-Daudé     return i2c_do_start_transfer(bus, address, is_recv
179265caf45SPhilippe Mathieu-Daudé                                                ? I2C_START_RECV
180265caf45SPhilippe Mathieu-Daudé                                                : I2C_START_SEND);
181265caf45SPhilippe Mathieu-Daudé }
182265caf45SPhilippe Mathieu-Daudé 
18390603c5bSPhilippe Mathieu-Daudé int i2c_start_recv(I2CBus *bus, uint8_t address)
18490603c5bSPhilippe Mathieu-Daudé {
18590603c5bSPhilippe Mathieu-Daudé     return i2c_do_start_transfer(bus, address, I2C_START_RECV);
18690603c5bSPhilippe Mathieu-Daudé }
18790603c5bSPhilippe Mathieu-Daudé 
18890603c5bSPhilippe Mathieu-Daudé int i2c_start_send(I2CBus *bus, uint8_t address)
18990603c5bSPhilippe Mathieu-Daudé {
19090603c5bSPhilippe Mathieu-Daudé     return i2c_do_start_transfer(bus, address, I2C_START_SEND);
19190603c5bSPhilippe Mathieu-Daudé }
19290603c5bSPhilippe Mathieu-Daudé 
193a5c82852SAndreas Färber void i2c_end_transfer(I2CBus *bus)
1940ff596d0Spbrook {
195b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1962293c27fSKONRAD Frederic     I2CNode *node, *next;
1970ff596d0Spbrook 
1982293c27fSKONRAD Frederic     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
19908bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
20008bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
201b5ea9327SAnthony Liguori         if (sc->event) {
20208bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("finish", s->address);
20308bb9b34SPhilippe Mathieu-Daudé             sc->event(s, I2C_FINISH);
204b5ea9327SAnthony Liguori         }
2052293c27fSKONRAD Frederic         QLIST_REMOVE(node, next);
2062293c27fSKONRAD Frederic         g_free(node);
2072293c27fSKONRAD Frederic     }
2082293c27fSKONRAD Frederic     bus->broadcast = false;
2090ff596d0Spbrook }
2100ff596d0Spbrook 
2112038a290SPhilippe Mathieu-Daudé int i2c_send(I2CBus *bus, uint8_t data)
2120ff596d0Spbrook {
213b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
21408bb9b34SPhilippe Mathieu-Daudé     I2CSlave *s;
2152293c27fSKONRAD Frederic     I2CNode *node;
2162293c27fSKONRAD Frederic     int ret = 0;
2170ff596d0Spbrook 
2182293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
21908bb9b34SPhilippe Mathieu-Daudé         s = node->elt;
22008bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
221b5ea9327SAnthony Liguori         if (sc->send) {
2222038a290SPhilippe Mathieu-Daudé             trace_i2c_send(s->address, data);
2232038a290SPhilippe Mathieu-Daudé             ret = ret || sc->send(s, data);
2242293c27fSKONRAD Frederic         } else {
2252293c27fSKONRAD Frederic             ret = -1;
226b5ea9327SAnthony Liguori         }
2272293c27fSKONRAD Frederic     }
228056fca7bSPeter Crosthwaite 
2292038a290SPhilippe Mathieu-Daudé     return ret ? -1 : 0;
230056fca7bSPeter Crosthwaite }
231056fca7bSPeter Crosthwaite 
2322ac4c5f4SCorey Minyard uint8_t i2c_recv(I2CBus *bus)
233056fca7bSPeter Crosthwaite {
2342ac4c5f4SCorey Minyard     uint8_t data = 0xff;
2352038a290SPhilippe Mathieu-Daudé     I2CSlaveClass *sc;
2362038a290SPhilippe Mathieu-Daudé     I2CSlave *s;
237056fca7bSPeter Crosthwaite 
2382038a290SPhilippe Mathieu-Daudé     if (!QLIST_EMPTY(&bus->current_devs) && !bus->broadcast) {
2392038a290SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
2402038a290SPhilippe Mathieu-Daudé         if (sc->recv) {
2412038a290SPhilippe Mathieu-Daudé             s = QLIST_FIRST(&bus->current_devs)->elt;
2422038a290SPhilippe Mathieu-Daudé             data = sc->recv(s);
2432038a290SPhilippe Mathieu-Daudé             trace_i2c_recv(s->address, data);
2442038a290SPhilippe Mathieu-Daudé         }
2452038a290SPhilippe Mathieu-Daudé     }
2462038a290SPhilippe Mathieu-Daudé 
2472ac4c5f4SCorey Minyard     return data;
248056fca7bSPeter Crosthwaite }
2490ff596d0Spbrook 
250a5c82852SAndreas Färber void i2c_nack(I2CBus *bus)
2510ff596d0Spbrook {
252b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
2532293c27fSKONRAD Frederic     I2CNode *node;
2540ff596d0Spbrook 
2552293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
2560ff596d0Spbrook         return;
257b5ea9327SAnthony Liguori     }
2580ff596d0Spbrook 
2592293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
2602293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
261b5ea9327SAnthony Liguori         if (sc->event) {
26208bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("nack", node->elt->address);
2632293c27fSKONRAD Frederic             sc->event(node->elt, I2C_NACK);
2642293c27fSKONRAD Frederic         }
265b5ea9327SAnthony Liguori     }
2660ff596d0Spbrook }
2670ff596d0Spbrook 
268bcbe8068SJuan Quintela static int i2c_slave_post_load(void *opaque, int version_id)
269bcbe8068SJuan Quintela {
2709e07bdf8SAnthony Liguori     I2CSlave *dev = opaque;
271a5c82852SAndreas Färber     I2CBus *bus;
2722293c27fSKONRAD Frederic     I2CNode *node;
2732293c27fSKONRAD Frederic 
274fef7fbc9SAndreas Färber     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
27571ae65e5SIgor Mammedov     if ((bus->saved_address == dev->address) ||
27671ae65e5SIgor Mammedov         (bus->saved_address == I2C_BROADCAST)) {
277*b21e2380SMarkus Armbruster         node = g_new(struct I2CNode, 1);
2782293c27fSKONRAD Frederic         node->elt = dev;
2792293c27fSKONRAD Frederic         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
280bcbe8068SJuan Quintela     }
281bcbe8068SJuan Quintela     return 0;
282bcbe8068SJuan Quintela }
283bcbe8068SJuan Quintela 
2841894839fSJuan Quintela const VMStateDescription vmstate_i2c_slave = {
2859e07bdf8SAnthony Liguori     .name = "I2CSlave",
286bcbe8068SJuan Quintela     .version_id = 1,
287bcbe8068SJuan Quintela     .minimum_version_id = 1,
288bcbe8068SJuan Quintela     .post_load = i2c_slave_post_load,
289bcbe8068SJuan Quintela     .fields = (VMStateField[]) {
2909e07bdf8SAnthony Liguori         VMSTATE_UINT8(address, I2CSlave),
291bcbe8068SJuan Quintela         VMSTATE_END_OF_LIST()
292bcbe8068SJuan Quintela     }
293bcbe8068SJuan Quintela };
294bcbe8068SJuan Quintela 
295db437ca6SPhilippe Mathieu-Daudé I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
296fe8de492SPaul Brook {
297fe8de492SPaul Brook     DeviceState *dev;
298fe8de492SPaul Brook 
2993e80f690SMarkus Armbruster     dev = qdev_new(name);
3005b7f5327SJuan Quintela     qdev_prop_set_uint8(dev, "address", addr);
301db437ca6SPhilippe Mathieu-Daudé     return I2C_SLAVE(dev);
302d88c42ffSPhilippe Mathieu-Daudé }
303d88c42ffSPhilippe Mathieu-Daudé 
3042616f572SPhilippe Mathieu-Daudé bool i2c_slave_realize_and_unref(I2CSlave *dev, I2CBus *bus, Error **errp)
305d88c42ffSPhilippe Mathieu-Daudé {
3062616f572SPhilippe Mathieu-Daudé     return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
307d88c42ffSPhilippe Mathieu-Daudé }
308d88c42ffSPhilippe Mathieu-Daudé 
3091373b15bSPhilippe Mathieu-Daudé I2CSlave *i2c_slave_create_simple(I2CBus *bus, const char *name, uint8_t addr)
310d88c42ffSPhilippe Mathieu-Daudé {
311db437ca6SPhilippe Mathieu-Daudé     I2CSlave *dev = i2c_slave_new(name, addr);
312d88c42ffSPhilippe Mathieu-Daudé 
3131373b15bSPhilippe Mathieu-Daudé     i2c_slave_realize_and_unref(dev, bus, &error_abort);
314d88c42ffSPhilippe Mathieu-Daudé 
3151373b15bSPhilippe Mathieu-Daudé     return dev;
316aa941b94Sbalrog }
317b5ea9327SAnthony Liguori 
318513ca82dSPatrick Venture static bool i2c_slave_match(I2CSlave *candidate, uint8_t address,
319513ca82dSPatrick Venture                             bool broadcast, I2CNodeList *current_devs)
320513ca82dSPatrick Venture {
321513ca82dSPatrick Venture     if ((candidate->address == address) || (broadcast)) {
322*b21e2380SMarkus Armbruster         I2CNode *node = g_new(struct I2CNode, 1);
323513ca82dSPatrick Venture         node->elt = candidate;
324513ca82dSPatrick Venture         QLIST_INSERT_HEAD(current_devs, node, next);
325513ca82dSPatrick Venture         return true;
326513ca82dSPatrick Venture     }
327513ca82dSPatrick Venture 
328513ca82dSPatrick Venture     /* Not found and not broadcast. */
329513ca82dSPatrick Venture     return false;
330513ca82dSPatrick Venture }
331513ca82dSPatrick Venture 
33239bffca2SAnthony Liguori static void i2c_slave_class_init(ObjectClass *klass, void *data)
33339bffca2SAnthony Liguori {
33439bffca2SAnthony Liguori     DeviceClass *k = DEVICE_CLASS(klass);
335513ca82dSPatrick Venture     I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
336125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_MISC, k->categories);
3370d936928SAnthony Liguori     k->bus_type = TYPE_I2C_BUS;
3384f67d30bSMarc-André Lureau     device_class_set_props(k, i2c_props);
339513ca82dSPatrick Venture     sc->match_and_add = i2c_slave_match;
34039bffca2SAnthony Liguori }
34139bffca2SAnthony Liguori 
3428c43a6f0SAndreas Färber static const TypeInfo i2c_slave_type_info = {
343b5ea9327SAnthony Liguori     .name = TYPE_I2C_SLAVE,
344b5ea9327SAnthony Liguori     .parent = TYPE_DEVICE,
345b5ea9327SAnthony Liguori     .instance_size = sizeof(I2CSlave),
346b5ea9327SAnthony Liguori     .abstract = true,
347b5ea9327SAnthony Liguori     .class_size = sizeof(I2CSlaveClass),
34839bffca2SAnthony Liguori     .class_init = i2c_slave_class_init,
349b5ea9327SAnthony Liguori };
350b5ea9327SAnthony Liguori 
35183f7d43aSAndreas Färber static void i2c_slave_register_types(void)
352b5ea9327SAnthony Liguori {
3530d936928SAnthony Liguori     type_register_static(&i2c_bus_info);
354b5ea9327SAnthony Liguori     type_register_static(&i2c_slave_type_info);
355b5ea9327SAnthony Liguori }
356b5ea9327SAnthony Liguori 
35783f7d43aSAndreas Färber type_init(i2c_slave_register_types)
358