From: KONRAD Frederic <fred.kon...@greensocs.com> This does a write to every slaves when the I2C bus get a write to address 0.
Signed-off-by: KONRAD Frederic <fred.kon...@greensocs.com> --- hw/i2c/core.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/hw/i2c/core.c b/hw/i2c/core.c index e0f92de..db9413f 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -14,6 +14,7 @@ struct I2CBus BusState qbus; I2CSlave *current_dev; uint8_t saved_address; + bool broadcast; }; static Property i2c_props[] = { @@ -55,6 +56,7 @@ static const VMStateDescription vmstate_i2c_bus = { .post_load = i2c_bus_post_load, .fields = (VMStateField[]) { VMSTATE_UINT8(saved_address, I2CBus), + VMSTATE_BOOL(broadcast, I2CBus), VMSTATE_END_OF_LIST() } }; @@ -66,6 +68,7 @@ I2CBus *i2c_init_bus(DeviceState *parent, const char *name) bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name)); vmstate_register(NULL, -1, &vmstate_i2c_bus, bus); + return bus; } @@ -88,6 +91,19 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) I2CSlave *slave = NULL; I2CSlaveClass *sc; + if (address == 0x00) { + /* This is a broadcast. */ + bus->broadcast = true; + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + I2CSlave *dev = I2C_SLAVE(kid->child); + sc = I2C_SLAVE_GET_CLASS(dev); + if (sc->event) { + sc->event(dev, recv ? I2C_START_RECV : I2C_START_SEND); + } + } + return 0; + } + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { DeviceState *qdev = kid->child; I2CSlave *candidate = I2C_SLAVE(qdev); @@ -113,9 +129,22 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) void i2c_end_transfer(I2CBus *bus) { + BusChild *kid; I2CSlave *dev = bus->current_dev; I2CSlaveClass *sc; + if (bus->broadcast) { + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + I2CSlave *dev = I2C_SLAVE(kid->child); + sc = I2C_SLAVE_GET_CLASS(dev); + if (sc->event) { + sc->event(dev, I2C_FINISH); + } + } + bus->broadcast = false; + return; + } + if (!dev) { return; } @@ -130,8 +159,22 @@ void i2c_end_transfer(I2CBus *bus) int i2c_send(I2CBus *bus, uint8_t data) { + BusChild *kid; I2CSlave *dev = bus->current_dev; I2CSlaveClass *sc; + int ret = 0; + + if (bus->broadcast) { + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + I2CSlave *dev = I2C_SLAVE(kid->child); + sc = I2C_SLAVE_GET_CLASS(dev); + bus->broadcast = true; + if (sc->send) { + ret |= sc->send(dev, data); + } + } + return ret; + } if (!dev) { return -1; @@ -150,7 +193,7 @@ int i2c_recv(I2CBus *bus) I2CSlave *dev = bus->current_dev; I2CSlaveClass *sc; - if (!dev) { + if ((!dev) || (bus->broadcast)) { return -1; } -- 1.9.0