Commit fe8de49258d2351472ad395b85966f7204f22a01
1 parent
b47b50fa
I2C qdev support
Signed-off-by: Paul Brook <paul@codesourcery.com>
Showing
2 changed files
with
67 additions
and
10 deletions
hw/i2c.c
| ... | ... | @@ -7,7 +7,6 @@ |
| 7 | 7 | * This code is licenced under the LGPL. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -#include "hw.h" | |
| 11 | 10 | #include "i2c.h" |
| 12 | 11 | |
| 13 | 12 | struct i2c_bus |
| ... | ... | @@ -61,7 +60,7 @@ i2c_slave *i2c_slave_init(i2c_bus *bus, int address, int size) |
| 61 | 60 | dev->address = address; |
| 62 | 61 | dev->next = bus->dev; |
| 63 | 62 | bus->dev = dev; |
| 64 | - dev->bus = bus; | |
| 63 | + dev->qdev.bus = bus; | |
| 65 | 64 | |
| 66 | 65 | return dev; |
| 67 | 66 | } |
| ... | ... | @@ -94,7 +93,7 @@ int i2c_start_transfer(i2c_bus *bus, int address, int recv) |
| 94 | 93 | /* If the bus is already busy, assume this is a repeated |
| 95 | 94 | start condition. */ |
| 96 | 95 | bus->current_dev = dev; |
| 97 | - dev->event(dev, recv ? I2C_START_RECV : I2C_START_SEND); | |
| 96 | + dev->info->event(dev, recv ? I2C_START_RECV : I2C_START_SEND); | |
| 98 | 97 | return 0; |
| 99 | 98 | } |
| 100 | 99 | |
| ... | ... | @@ -105,7 +104,7 @@ void i2c_end_transfer(i2c_bus *bus) |
| 105 | 104 | if (!dev) |
| 106 | 105 | return; |
| 107 | 106 | |
| 108 | - dev->event(dev, I2C_FINISH); | |
| 107 | + dev->info->event(dev, I2C_FINISH); | |
| 109 | 108 | |
| 110 | 109 | bus->current_dev = NULL; |
| 111 | 110 | } |
| ... | ... | @@ -117,7 +116,7 @@ int i2c_send(i2c_bus *bus, uint8_t data) |
| 117 | 116 | if (!dev) |
| 118 | 117 | return -1; |
| 119 | 118 | |
| 120 | - return dev->send(dev, data); | |
| 119 | + return dev->info->send(dev, data); | |
| 121 | 120 | } |
| 122 | 121 | |
| 123 | 122 | int i2c_recv(i2c_bus *bus) |
| ... | ... | @@ -127,7 +126,7 @@ int i2c_recv(i2c_bus *bus) |
| 127 | 126 | if (!dev) |
| 128 | 127 | return -1; |
| 129 | 128 | |
| 130 | - return dev->recv(dev); | |
| 129 | + return dev->info->recv(dev); | |
| 131 | 130 | } |
| 132 | 131 | |
| 133 | 132 | void i2c_nack(i2c_bus *bus) |
| ... | ... | @@ -137,7 +136,7 @@ void i2c_nack(i2c_bus *bus) |
| 137 | 136 | if (!dev) |
| 138 | 137 | return; |
| 139 | 138 | |
| 140 | - dev->event(dev, I2C_NACK); | |
| 139 | + dev->info->event(dev, I2C_NACK); | |
| 141 | 140 | } |
| 142 | 141 | |
| 143 | 142 | void i2c_slave_save(QEMUFile *f, i2c_slave *dev) |
| ... | ... | @@ -147,7 +146,44 @@ void i2c_slave_save(QEMUFile *f, i2c_slave *dev) |
| 147 | 146 | |
| 148 | 147 | void i2c_slave_load(QEMUFile *f, i2c_slave *dev) |
| 149 | 148 | { |
| 149 | + i2c_bus *bus; | |
| 150 | + bus = qdev_get_bus(&dev->qdev); | |
| 150 | 151 | dev->address = qemu_get_byte(f); |
| 151 | - if (dev->bus->saved_address == dev->address) | |
| 152 | - dev->bus->current_dev = dev; | |
| 152 | + if (bus->saved_address == dev->address) { | |
| 153 | + bus->current_dev = dev; | |
| 154 | + } | |
| 155 | +} | |
| 156 | + | |
| 157 | +static void i2c_slave_qdev_init(DeviceState *dev, void *opaque) | |
| 158 | +{ | |
| 159 | + I2CSlaveInfo *info = opaque; | |
| 160 | + i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); | |
| 161 | + | |
| 162 | + s->info = info; | |
| 163 | + s->bus = qdev_get_bus(dev); | |
| 164 | + s->address = qdev_get_prop_int(dev, "address", 0); | |
| 165 | + s->next = s->bus->dev; | |
| 166 | + s->bus->dev = s; | |
| 167 | + | |
| 168 | + s->event = info->event; | |
| 169 | + s->recv = info->recv; | |
| 170 | + s->send = info->send; | |
| 171 | + | |
| 172 | + info->init(s); | |
| 173 | +} | |
| 174 | + | |
| 175 | +void i2c_register_slave(const char *name, int size, I2CSlaveInfo *info) | |
| 176 | +{ | |
| 177 | + assert(size >= sizeof(i2c_slave)); | |
| 178 | + qdev_register(name, size, i2c_slave_qdev_init, info); | |
| 179 | +} | |
| 180 | + | |
| 181 | +DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, int addr) | |
| 182 | +{ | |
| 183 | + DeviceState *dev; | |
| 184 | + | |
| 185 | + dev = qdev_create(bus, name); | |
| 186 | + qdev_set_prop_int(dev, "address", addr); | |
| 187 | + qdev_init(dev); | |
| 188 | + return dev; | |
| 153 | 189 | } | ... | ... |
hw/i2c.h
| 1 | 1 | #ifndef QEMU_I2C_H |
| 2 | 2 | #define QEMU_I2C_H |
| 3 | 3 | |
| 4 | +#include "qdev.h" | |
| 5 | + | |
| 4 | 6 | /* The QEMU I2C implementation only supports simple transfers that complete |
| 5 | 7 | immediately. It does not support slave devices that need to be able to |
| 6 | 8 | defer their response (eg. CPU slave interfaces where the data is supplied |
| ... | ... | @@ -20,9 +22,21 @@ typedef int (*i2c_recv_cb)(i2c_slave *s); |
| 20 | 22 | /* Notify the slave of a bus state change. */ |
| 21 | 23 | typedef void (*i2c_event_cb)(i2c_slave *s, enum i2c_event event); |
| 22 | 24 | |
| 25 | +typedef void (*i2c_slave_initfn)(i2c_slave *dev); | |
| 26 | + | |
| 27 | +typedef struct { | |
| 28 | + /* Callbacks provided by the device. */ | |
| 29 | + i2c_slave_initfn init; | |
| 30 | + i2c_event_cb event; | |
| 31 | + i2c_recv_cb recv; | |
| 32 | + i2c_send_cb send; | |
| 33 | +} I2CSlaveInfo; | |
| 34 | + | |
| 23 | 35 | struct i2c_slave |
| 24 | 36 | { |
| 25 | - /* Callbacks to be set by the device. */ | |
| 37 | + DeviceState qdev; | |
| 38 | + I2CSlaveInfo *info; | |
| 39 | + /* FIXME: These 3 should go away once all devices have been converted. */ | |
| 26 | 40 | i2c_event_cb event; |
| 27 | 41 | i2c_recv_cb recv; |
| 28 | 42 | i2c_send_cb send; |
| ... | ... | @@ -45,6 +59,13 @@ int i2c_recv(i2c_bus *bus); |
| 45 | 59 | void i2c_slave_save(QEMUFile *f, i2c_slave *dev); |
| 46 | 60 | void i2c_slave_load(QEMUFile *f, i2c_slave *dev); |
| 47 | 61 | |
| 62 | +#define I2C_SLAVE_FROM_QDEV(dev) DO_UPCAST(i2c_slave, qdev, dev) | |
| 63 | +#define FROM_I2C_SLAVE(type, dev) DO_UPCAST(type, i2c, dev) | |
| 64 | + | |
| 65 | +void i2c_register_slave(const char *name, int size, I2CSlaveInfo *type); | |
| 66 | + | |
| 67 | +DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, int addr); | |
| 68 | + | |
| 48 | 69 | /* max111x.c */ |
| 49 | 70 | typedef struct MAX111xState MAX111xState; |
| 50 | 71 | uint32_t max111x_read(void *opaque); | ... | ... |