SMBus qdev conversion

Signed-off-by: Paul Brook <paul@codesourcery.com>
This commit is contained in:
Paul Brook 2009-05-14 22:35:08 +01:00
parent fd1eb2ea42
commit 1ea96673c6
5 changed files with 78 additions and 51 deletions

View File

@ -911,7 +911,11 @@ void mips_malta_init (ram_addr_t ram_size,
eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */ eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
/* TODO: Populate SPD eeprom data. */ /* TODO: Populate SPD eeprom data. */
smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256)); DeviceState *eeprom;
eeprom = qdev_create(smbus, "smbus-eeprom");
qdev_set_prop_int(eeprom, "address", 0x50 + i);
qdev_set_prop_ptr(eeprom, "data", eeprom_buf + (i * 256));
qdev_init(eeprom);
} }
pit = pit_init(0x40, i8259[0]); pit = pit_init(0x40, i8259[0]);
DMA_init(0); DMA_init(0);

View File

@ -1109,7 +1109,11 @@ static void pc_init1(ram_addr_t ram_size,
/* TODO: Populate SPD eeprom data. */ /* TODO: Populate SPD eeprom data. */
smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]); smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256)); DeviceState *eeprom;
eeprom = qdev_create(smbus, "smbus-eeprom");
qdev_set_prop_int(eeprom, "address", 0x50 + i);
qdev_set_prop_ptr(eeprom, "data", eeprom_buf + (i * 256));
qdev_init(eeprom);
} }
} }

View File

@ -37,25 +37,29 @@ enum {
static void smbus_do_quick_cmd(SMBusDevice *dev, int recv) static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
{ {
SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
DPRINTF("Quick Command %d\n", recv); DPRINTF("Quick Command %d\n", recv);
if (dev->quick_cmd) if (t->quick_cmd)
dev->quick_cmd(dev, recv); t->quick_cmd(dev, recv);
} }
static void smbus_do_write(SMBusDevice *dev) static void smbus_do_write(SMBusDevice *dev)
{ {
SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
if (dev->data_len == 0) { if (dev->data_len == 0) {
smbus_do_quick_cmd(dev, 0); smbus_do_quick_cmd(dev, 0);
} else if (dev->data_len == 1) { } else if (dev->data_len == 1) {
DPRINTF("Send Byte\n"); DPRINTF("Send Byte\n");
if (dev->send_byte) { if (t->send_byte) {
dev->send_byte(dev, dev->data_buf[0]); t->send_byte(dev, dev->data_buf[0]);
} }
} else { } else {
dev->command = dev->data_buf[0]; dev->command = dev->data_buf[0];
DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1); DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
if (dev->write_data) { if (t->write_data) {
dev->write_data(dev, dev->command, dev->data_buf + 1, t->write_data(dev, dev->command, dev->data_buf + 1,
dev->data_len - 1); dev->data_len - 1);
} }
} }
@ -63,7 +67,8 @@ static void smbus_do_write(SMBusDevice *dev)
static void smbus_i2c_event(i2c_slave *s, enum i2c_event event) static void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
{ {
SMBusDevice *dev = (SMBusDevice *)s; SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
switch (event) { switch (event) {
case I2C_START_SEND: case I2C_START_SEND:
switch (dev->mode) { switch (dev->mode) {
@ -145,13 +150,14 @@ static void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
static int smbus_i2c_recv(i2c_slave *s) static int smbus_i2c_recv(i2c_slave *s)
{ {
SMBusDevice *dev = (SMBusDevice *)s; SMBusDeviceInfo *t = container_of(s->info, SMBusDeviceInfo, i2c);
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
int ret; int ret;
switch (dev->mode) { switch (dev->mode) {
case SMBUS_RECV_BYTE: case SMBUS_RECV_BYTE:
if (dev->receive_byte) { if (t->receive_byte) {
ret = dev->receive_byte(dev); ret = t->receive_byte(dev);
} else { } else {
ret = 0; ret = 0;
} }
@ -159,8 +165,8 @@ static int smbus_i2c_recv(i2c_slave *s)
dev->mode = SMBUS_DONE; dev->mode = SMBUS_DONE;
break; break;
case SMBUS_READ_DATA: case SMBUS_READ_DATA:
if (dev->read_data) { if (t->read_data) {
ret = dev->read_data(dev, dev->command, dev->data_len); ret = t->read_data(dev, dev->command, dev->data_len);
dev->data_len++; dev->data_len++;
} else { } else {
ret = 0; ret = 0;
@ -178,7 +184,8 @@ static int smbus_i2c_recv(i2c_slave *s)
static int smbus_i2c_send(i2c_slave *s, uint8_t data) static int smbus_i2c_send(i2c_slave *s, uint8_t data)
{ {
SMBusDevice *dev = (SMBusDevice *)s; SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
switch (dev->mode) { switch (dev->mode) {
case SMBUS_WRITE_DATA: case SMBUS_WRITE_DATA:
DPRINTF("Write data %02x\n", data); DPRINTF("Write data %02x\n", data);
@ -191,19 +198,22 @@ static int smbus_i2c_send(i2c_slave *s, uint8_t data)
return 0; return 0;
} }
SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size) static void smbus_device_init(i2c_slave *i2c)
{ {
SMBusDevice *dev; SMBusDeviceInfo *t = container_of(i2c->info, SMBusDeviceInfo, i2c);
SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, i2c);
if (size < sizeof(SMBusDevice)) t->init(dev);
hw_error("SMBus struct too small"); }
dev = (SMBusDevice *)i2c_slave_init(bus, address, size); void smbus_register_device(const char *name, int size, SMBusDeviceInfo *info)
dev->i2c.event = smbus_i2c_event; {
dev->i2c.recv = smbus_i2c_recv; assert(size >= sizeof(SMBusDevice));
dev->i2c.send = smbus_i2c_send; info->i2c.init = smbus_device_init;
info->i2c.event = smbus_i2c_event;
return dev; info->i2c.recv = smbus_i2c_recv;
info->i2c.send = smbus_i2c_send;
i2c_register_slave(name, size, &info->i2c);
} }
/* Master device commands. */ /* Master device commands. */

View File

@ -28,7 +28,16 @@ struct SMBusDevice {
/* The SMBus protocol is implemented on top of I2C. */ /* The SMBus protocol is implemented on top of I2C. */
i2c_slave i2c; i2c_slave i2c;
/* Callbacks set by the device. */ /* Remaining fields for internal use only. */
int mode;
int data_len;
uint8_t data_buf[34]; /* command + len + 32 bytes of data. */
uint8_t command;
};
typedef struct {
I2CSlaveInfo i2c;
void (*init)(SMBusDevice *dev);
void (*quick_cmd)(SMBusDevice *dev, uint8_t read); void (*quick_cmd)(SMBusDevice *dev, uint8_t read);
void (*send_byte)(SMBusDevice *dev, uint8_t val); void (*send_byte)(SMBusDevice *dev, uint8_t val);
uint8_t (*receive_byte)(SMBusDevice *dev); uint8_t (*receive_byte)(SMBusDevice *dev);
@ -42,16 +51,9 @@ struct SMBusDevice {
byte at a time. The device is responsible for adding the length byte at a time. The device is responsible for adding the length
byte on block reads. */ byte on block reads. */
uint8_t (*read_data)(SMBusDevice *dev, uint8_t cmd, int n); uint8_t (*read_data)(SMBusDevice *dev, uint8_t cmd, int n);
} SMBusDeviceInfo;
/* Remaining fields for internal use only. */ void smbus_register_device(const char *name, int size, SMBusDeviceInfo *info);
int mode;
int data_len;
uint8_t data_buf[34]; /* command + len + 32 bytes of data. */
uint8_t command;
};
/* Create a slave device. */
SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size);
/* Master device commands. */ /* Master device commands. */
void smbus_quick_command(i2c_bus *bus, int addr, int read); void smbus_quick_command(i2c_bus *bus, int addr, int read);
@ -64,6 +66,3 @@ void smbus_write_word(i2c_bus *bus, int addr, uint8_t command, uint16_t data);
int smbus_read_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data); int smbus_read_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data);
void smbus_write_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data, void smbus_write_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data,
int len); int len);
/* smbus_eeprom.c */
void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf);

View File

@ -29,7 +29,7 @@
//#define DEBUG //#define DEBUG
typedef struct SMBusEEPROMDevice { typedef struct SMBusEEPROMDevice {
SMBusDevice dev; SMBusDevice smbusdev;
uint8_t *data; uint8_t *data;
uint8_t offset; uint8_t offset;
} SMBusEEPROMDevice; } SMBusEEPROMDevice;
@ -95,18 +95,28 @@ static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
return eeprom_receive_byte(dev); return eeprom_receive_byte(dev);
} }
void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf) static void smbus_eeprom_init(SMBusDevice *dev)
{ {
SMBusEEPROMDevice *eeprom; SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
eeprom = (SMBusEEPROMDevice *)smbus_device_init(bus, addr, /* FIXME: Should be a blob rather than a ptr. */
sizeof(SMBusEEPROMDevice)); eeprom->data = qdev_get_prop_ptr(&dev->i2c.qdev, "data");
eeprom->dev.quick_cmd = eeprom_quick_cmd;
eeprom->dev.send_byte = eeprom_send_byte;
eeprom->dev.receive_byte = eeprom_receive_byte;
eeprom->dev.write_data = eeprom_write_data;
eeprom->dev.read_data = eeprom_read_data;
eeprom->data = buf;
eeprom->offset = 0; eeprom->offset = 0;
} }
static SMBusDeviceInfo smbus_eeprom_info = {
.init = smbus_eeprom_init,
.quick_cmd = eeprom_quick_cmd,
.send_byte = eeprom_send_byte,
.receive_byte = eeprom_receive_byte,
.write_data = eeprom_write_data,
.read_data = eeprom_read_data
};
static void smbus_eeprom_register_devices(void)
{
smbus_register_device("smbus-eeprom", sizeof(SMBusEEPROMDevice),
&smbus_eeprom_info);
}
device_init(smbus_eeprom_register_devices)