xref: /qemu/hw/i2c/core.c (revision 44b1ff319c4781c7ab13f7e119b3114a1e6a52e2)
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"
120ff596d0Spbrook 
132293c27fSKONRAD Frederic typedef struct I2CNode I2CNode;
142293c27fSKONRAD Frederic 
152293c27fSKONRAD Frederic struct I2CNode {
162293c27fSKONRAD Frederic     I2CSlave *elt;
172293c27fSKONRAD Frederic     QLIST_ENTRY(I2CNode) next;
182293c27fSKONRAD Frederic };
192293c27fSKONRAD Frederic 
2071ae65e5SIgor Mammedov #define I2C_BROADCAST 0x00
2171ae65e5SIgor Mammedov 
22a5c82852SAndreas Färber struct I2CBus
230ff596d0Spbrook {
2402e2da45SPaul Brook     BusState qbus;
252293c27fSKONRAD Frederic     QLIST_HEAD(, I2CNode) current_devs;
265b7f5327SJuan Quintela     uint8_t saved_address;
272293c27fSKONRAD Frederic     bool broadcast;
280ff596d0Spbrook };
290ff596d0Spbrook 
303cb75a7cSPaolo Bonzini static Property i2c_props[] = {
313cb75a7cSPaolo Bonzini     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
323cb75a7cSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
333cb75a7cSPaolo Bonzini };
343cb75a7cSPaolo Bonzini 
350d936928SAnthony Liguori #define TYPE_I2C_BUS "i2c-bus"
36a5c82852SAndreas Färber #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
370d936928SAnthony Liguori 
380d936928SAnthony Liguori static const TypeInfo i2c_bus_info = {
390d936928SAnthony Liguori     .name = TYPE_I2C_BUS,
400d936928SAnthony Liguori     .parent = TYPE_BUS,
41a5c82852SAndreas Färber     .instance_size = sizeof(I2CBus),
4210c4c98aSGerd Hoffmann };
4310c4c98aSGerd Hoffmann 
44*44b1ff31SDr. David Alan Gilbert static int i2c_bus_pre_save(void *opaque)
45c701b35bSpbrook {
46a5c82852SAndreas Färber     I2CBus *bus = opaque;
47c701b35bSpbrook 
482293c27fSKONRAD Frederic     bus->saved_address = -1;
492293c27fSKONRAD Frederic     if (!QLIST_EMPTY(&bus->current_devs)) {
502293c27fSKONRAD Frederic         if (!bus->broadcast) {
512293c27fSKONRAD Frederic             bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
5271ae65e5SIgor Mammedov         } else {
5371ae65e5SIgor Mammedov             bus->saved_address = I2C_BROADCAST;
54c701b35bSpbrook         }
552293c27fSKONRAD Frederic     }
56*44b1ff31SDr. David Alan Gilbert 
57*44b1ff31SDr. David Alan Gilbert     return 0;
58c701b35bSpbrook }
59c701b35bSpbrook 
608d0eb050SJuan Quintela static const VMStateDescription vmstate_i2c_bus = {
618d0eb050SJuan Quintela     .name = "i2c_bus",
628d0eb050SJuan Quintela     .version_id = 1,
638d0eb050SJuan Quintela     .minimum_version_id = 1,
648d0eb050SJuan Quintela     .pre_save = i2c_bus_pre_save,
658d0eb050SJuan Quintela     .fields = (VMStateField[]) {
66a5c82852SAndreas Färber         VMSTATE_UINT8(saved_address, I2CBus),
678d0eb050SJuan Quintela         VMSTATE_END_OF_LIST()
688d0eb050SJuan Quintela     }
698d0eb050SJuan Quintela };
708d0eb050SJuan Quintela 
710ff596d0Spbrook /* Create a new I2C bus.  */
72a5c82852SAndreas Färber I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
730ff596d0Spbrook {
74a5c82852SAndreas Färber     I2CBus *bus;
750ff596d0Spbrook 
76fef7fbc9SAndreas Färber     bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
772293c27fSKONRAD Frederic     QLIST_INIT(&bus->current_devs);
780be71e32SAlex Williamson     vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
790ff596d0Spbrook     return bus;
800ff596d0Spbrook }
810ff596d0Spbrook 
829e07bdf8SAnthony Liguori void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
830ff596d0Spbrook {
840ff596d0Spbrook     dev->address = address;
850ff596d0Spbrook }
860ff596d0Spbrook 
870ff596d0Spbrook /* Return nonzero if bus is busy.  */
88a5c82852SAndreas Färber int i2c_bus_busy(I2CBus *bus)
890ff596d0Spbrook {
902293c27fSKONRAD Frederic     return !QLIST_EMPTY(&bus->current_devs);
910ff596d0Spbrook }
920ff596d0Spbrook 
930ff596d0Spbrook /* TODO: Make this handle multiple masters.  */
94d307c28cSCorey Minyard /*
95d307c28cSCorey Minyard  * Start or continue an i2c transaction.  When this is called for the
96d307c28cSCorey Minyard  * first time or after an i2c_end_transfer(), if it returns an error
97d307c28cSCorey Minyard  * the bus transaction is terminated (or really never started).  If
98d307c28cSCorey Minyard  * this is called after another i2c_start_transfer() without an
99d307c28cSCorey Minyard  * intervening i2c_end_transfer(), and it returns an error, the
100d307c28cSCorey Minyard  * transaction will not be terminated.  The caller must do it.
101d307c28cSCorey Minyard  *
102d307c28cSCorey Minyard  * This corresponds with the way real hardware works.  The SMBus
103d307c28cSCorey Minyard  * protocol uses a start transfer to switch from write to read mode
104d307c28cSCorey Minyard  * without releasing the bus.  If that fails, the bus is still
105d307c28cSCorey Minyard  * in a transaction.
106d307c28cSCorey Minyard  */
107a5c82852SAndreas Färber int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
1080ff596d0Spbrook {
1090866aca1SAnthony Liguori     BusChild *kid;
110b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1112293c27fSKONRAD Frederic     I2CNode *node;
112d307c28cSCorey Minyard     bool bus_scanned = false;
1132293c27fSKONRAD Frederic 
11471ae65e5SIgor Mammedov     if (address == I2C_BROADCAST) {
1152293c27fSKONRAD Frederic         /*
1162293c27fSKONRAD Frederic          * This is a broadcast, the current_devs will be all the devices of the
1172293c27fSKONRAD Frederic          * bus.
1182293c27fSKONRAD Frederic          */
1192293c27fSKONRAD Frederic         bus->broadcast = true;
1202293c27fSKONRAD Frederic     }
1210ff596d0Spbrook 
1220fa758c3SCorey Minyard     /*
1230fa758c3SCorey Minyard      * If there are already devices in the list, that means we are in
1240fa758c3SCorey Minyard      * the middle of a transaction and we shouldn't rescan the bus.
1250fa758c3SCorey Minyard      *
1260fa758c3SCorey Minyard      * This happens with any SMBus transaction, even on a pure I2C
1270fa758c3SCorey Minyard      * device.  The interface does a transaction start without
1280fa758c3SCorey Minyard      * terminating the previous transaction.
1290fa758c3SCorey Minyard      */
1300fa758c3SCorey Minyard     if (QLIST_EMPTY(&bus->current_devs)) {
1310866aca1SAnthony Liguori         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
1320866aca1SAnthony Liguori             DeviceState *qdev = kid->child;
1338aae84a1SAndreas Färber             I2CSlave *candidate = I2C_SLAVE(qdev);
1342293c27fSKONRAD Frederic             if ((candidate->address == address) || (bus->broadcast)) {
1352293c27fSKONRAD Frederic                 node = g_malloc(sizeof(struct I2CNode));
1362293c27fSKONRAD Frederic                 node->elt = candidate;
1372293c27fSKONRAD Frederic                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
1382293c27fSKONRAD Frederic                 if (!bus->broadcast) {
1390ff596d0Spbrook                     break;
1400ff596d0Spbrook                 }
141b3a21988SJuha Riihimäki             }
1422293c27fSKONRAD Frederic         }
143d307c28cSCorey Minyard         bus_scanned = true;
1440fa758c3SCorey Minyard     }
1450ff596d0Spbrook 
1462293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
1470ff596d0Spbrook         return 1;
148b5ea9327SAnthony Liguori     }
1490ff596d0Spbrook 
1502293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
151d307c28cSCorey Minyard         int rv;
152d307c28cSCorey Minyard 
1532293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
1540ff596d0Spbrook         /* If the bus is already busy, assume this is a repeated
1550ff596d0Spbrook            start condition.  */
156d307c28cSCorey Minyard 
157b5ea9327SAnthony Liguori         if (sc->event) {
158d307c28cSCorey Minyard             rv = sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
159d307c28cSCorey Minyard             if (rv && !bus->broadcast) {
160d307c28cSCorey Minyard                 if (bus_scanned) {
161d307c28cSCorey Minyard                     /* First call, terminate the transfer. */
162d307c28cSCorey Minyard                     i2c_end_transfer(bus);
163d307c28cSCorey Minyard                 }
164d307c28cSCorey Minyard                 return rv;
165d307c28cSCorey Minyard             }
1662293c27fSKONRAD Frederic         }
167b5ea9327SAnthony Liguori     }
1680ff596d0Spbrook     return 0;
1690ff596d0Spbrook }
1700ff596d0Spbrook 
171a5c82852SAndreas Färber void i2c_end_transfer(I2CBus *bus)
1720ff596d0Spbrook {
173b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1742293c27fSKONRAD Frederic     I2CNode *node, *next;
1750ff596d0Spbrook 
1762293c27fSKONRAD Frederic     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
1772293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
178b5ea9327SAnthony Liguori         if (sc->event) {
1792293c27fSKONRAD Frederic             sc->event(node->elt, I2C_FINISH);
180b5ea9327SAnthony Liguori         }
1812293c27fSKONRAD Frederic         QLIST_REMOVE(node, next);
1822293c27fSKONRAD Frederic         g_free(node);
1832293c27fSKONRAD Frederic     }
1842293c27fSKONRAD Frederic     bus->broadcast = false;
1850ff596d0Spbrook }
1860ff596d0Spbrook 
187056fca7bSPeter Crosthwaite int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
1880ff596d0Spbrook {
189b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1902293c27fSKONRAD Frederic     I2CNode *node;
1912293c27fSKONRAD Frederic     int ret = 0;
1920ff596d0Spbrook 
193056fca7bSPeter Crosthwaite     if (send) {
1942293c27fSKONRAD Frederic         QLIST_FOREACH(node, &bus->current_devs, next) {
1952293c27fSKONRAD Frederic             sc = I2C_SLAVE_GET_CLASS(node->elt);
196b5ea9327SAnthony Liguori             if (sc->send) {
197056fca7bSPeter Crosthwaite                 ret = ret || sc->send(node->elt, *data);
1982293c27fSKONRAD Frederic             } else {
1992293c27fSKONRAD Frederic                 ret = -1;
200b5ea9327SAnthony Liguori             }
2012293c27fSKONRAD Frederic         }
2022293c27fSKONRAD Frederic         return ret ? -1 : 0;
203056fca7bSPeter Crosthwaite     } else {
2042293c27fSKONRAD Frederic         if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
2050ff596d0Spbrook             return -1;
206b5ea9327SAnthony Liguori         }
2070ff596d0Spbrook 
2082293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
209b5ea9327SAnthony Liguori         if (sc->recv) {
210056fca7bSPeter Crosthwaite             ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
211056fca7bSPeter Crosthwaite             if (ret < 0) {
212056fca7bSPeter Crosthwaite                 return ret;
213056fca7bSPeter Crosthwaite             } else {
214056fca7bSPeter Crosthwaite                 *data = ret;
215056fca7bSPeter Crosthwaite                 return 0;
216056fca7bSPeter Crosthwaite             }
217b5ea9327SAnthony Liguori         }
218b5ea9327SAnthony Liguori         return -1;
2190ff596d0Spbrook     }
220056fca7bSPeter Crosthwaite }
221056fca7bSPeter Crosthwaite 
222056fca7bSPeter Crosthwaite int i2c_send(I2CBus *bus, uint8_t data)
223056fca7bSPeter Crosthwaite {
224056fca7bSPeter Crosthwaite     return i2c_send_recv(bus, &data, true);
225056fca7bSPeter Crosthwaite }
226056fca7bSPeter Crosthwaite 
227056fca7bSPeter Crosthwaite int i2c_recv(I2CBus *bus)
228056fca7bSPeter Crosthwaite {
229056fca7bSPeter Crosthwaite     uint8_t data;
230056fca7bSPeter Crosthwaite     int ret = i2c_send_recv(bus, &data, false);
231056fca7bSPeter Crosthwaite 
232056fca7bSPeter Crosthwaite     return ret < 0 ? ret : data;
233056fca7bSPeter Crosthwaite }
2340ff596d0Spbrook 
235a5c82852SAndreas Färber void i2c_nack(I2CBus *bus)
2360ff596d0Spbrook {
237b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
2382293c27fSKONRAD Frederic     I2CNode *node;
2390ff596d0Spbrook 
2402293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
2410ff596d0Spbrook         return;
242b5ea9327SAnthony Liguori     }
2430ff596d0Spbrook 
2442293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
2452293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
246b5ea9327SAnthony Liguori         if (sc->event) {
2472293c27fSKONRAD Frederic             sc->event(node->elt, I2C_NACK);
2482293c27fSKONRAD Frederic         }
249b5ea9327SAnthony Liguori     }
2500ff596d0Spbrook }
2510ff596d0Spbrook 
252bcbe8068SJuan Quintela static int i2c_slave_post_load(void *opaque, int version_id)
253bcbe8068SJuan Quintela {
2549e07bdf8SAnthony Liguori     I2CSlave *dev = opaque;
255a5c82852SAndreas Färber     I2CBus *bus;
2562293c27fSKONRAD Frederic     I2CNode *node;
2572293c27fSKONRAD Frederic 
258fef7fbc9SAndreas Färber     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
25971ae65e5SIgor Mammedov     if ((bus->saved_address == dev->address) ||
26071ae65e5SIgor Mammedov         (bus->saved_address == I2C_BROADCAST)) {
2612293c27fSKONRAD Frederic         node = g_malloc(sizeof(struct I2CNode));
2622293c27fSKONRAD Frederic         node->elt = dev;
2632293c27fSKONRAD Frederic         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
264bcbe8068SJuan Quintela     }
265bcbe8068SJuan Quintela     return 0;
266bcbe8068SJuan Quintela }
267bcbe8068SJuan Quintela 
2681894839fSJuan Quintela const VMStateDescription vmstate_i2c_slave = {
2699e07bdf8SAnthony Liguori     .name = "I2CSlave",
270bcbe8068SJuan Quintela     .version_id = 1,
271bcbe8068SJuan Quintela     .minimum_version_id = 1,
272bcbe8068SJuan Quintela     .post_load = i2c_slave_post_load,
273bcbe8068SJuan Quintela     .fields = (VMStateField[]) {
2749e07bdf8SAnthony Liguori         VMSTATE_UINT8(address, I2CSlave),
275bcbe8068SJuan Quintela         VMSTATE_END_OF_LIST()
276bcbe8068SJuan Quintela     }
277bcbe8068SJuan Quintela };
278bcbe8068SJuan Quintela 
279d307af79SAnthony Liguori static int i2c_slave_qdev_init(DeviceState *dev)
280fe8de492SPaul Brook {
2818aae84a1SAndreas Färber     I2CSlave *s = I2C_SLAVE(dev);
282b5ea9327SAnthony Liguori     I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
283fe8de492SPaul Brook 
2849e41badeSAlastair D'Silva     if (sc->init) {
285b5ea9327SAnthony Liguori         return sc->init(s);
286fe8de492SPaul Brook     }
287fe8de492SPaul Brook 
2889e41badeSAlastair D'Silva     return 0;
2899e41badeSAlastair D'Silva }
2909e41badeSAlastair D'Silva 
291a5c82852SAndreas Färber DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
292fe8de492SPaul Brook {
293fe8de492SPaul Brook     DeviceState *dev;
294fe8de492SPaul Brook 
29502e2da45SPaul Brook     dev = qdev_create(&bus->qbus, name);
2965b7f5327SJuan Quintela     qdev_prop_set_uint8(dev, "address", addr);
297e23a1b33SMarkus Armbruster     qdev_init_nofail(dev);
298fe8de492SPaul Brook     return dev;
299aa941b94Sbalrog }
300b5ea9327SAnthony Liguori 
30139bffca2SAnthony Liguori static void i2c_slave_class_init(ObjectClass *klass, void *data)
30239bffca2SAnthony Liguori {
30339bffca2SAnthony Liguori     DeviceClass *k = DEVICE_CLASS(klass);
30439bffca2SAnthony Liguori     k->init = i2c_slave_qdev_init;
305125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_MISC, k->categories);
3060d936928SAnthony Liguori     k->bus_type = TYPE_I2C_BUS;
307bce54474SPaolo Bonzini     k->props = i2c_props;
30839bffca2SAnthony Liguori }
30939bffca2SAnthony Liguori 
3108c43a6f0SAndreas Färber static const TypeInfo i2c_slave_type_info = {
311b5ea9327SAnthony Liguori     .name = TYPE_I2C_SLAVE,
312b5ea9327SAnthony Liguori     .parent = TYPE_DEVICE,
313b5ea9327SAnthony Liguori     .instance_size = sizeof(I2CSlave),
314b5ea9327SAnthony Liguori     .abstract = true,
315b5ea9327SAnthony Liguori     .class_size = sizeof(I2CSlaveClass),
31639bffca2SAnthony Liguori     .class_init = i2c_slave_class_init,
317b5ea9327SAnthony Liguori };
318b5ea9327SAnthony Liguori 
31983f7d43aSAndreas Färber static void i2c_slave_register_types(void)
320b5ea9327SAnthony Liguori {
3210d936928SAnthony Liguori     type_register_static(&i2c_bus_info);
322b5ea9327SAnthony Liguori     type_register_static(&i2c_slave_type_info);
323b5ea9327SAnthony Liguori }
324b5ea9327SAnthony Liguori 
32583f7d43aSAndreas Färber type_init(i2c_slave_register_types)
326