2 * QEMU SMBus device emulation.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
10 /* TODO: Implement PEC. */
16 //#define DEBUG_SMBUS 1
19 #define DPRINTF(fmt, ...) \
20 do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
38 static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
40 SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
42 DPRINTF("Quick Command %d\n", recv);
44 t->quick_cmd(dev, recv);
47 static void smbus_do_write(SMBusDevice *dev)
49 SMBusDeviceInfo *t = container_of(dev->i2c.info, SMBusDeviceInfo, i2c);
51 if (dev->data_len == 0) {
52 smbus_do_quick_cmd(dev, 0);
53 } else if (dev->data_len == 1) {
54 DPRINTF("Send Byte\n");
56 t->send_byte(dev, dev->data_buf[0]);
59 dev->command = dev->data_buf[0];
60 DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
62 t->write_data(dev, dev->command, dev->data_buf + 1,
68 static void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
70 SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
76 DPRINTF("Incoming data\n");
77 dev->mode = SMBUS_WRITE_DATA;
80 BADF("Unexpected send start condition in state %d\n", dev->mode);
81 dev->mode = SMBUS_CONFUSED;
89 DPRINTF("Read mode\n");
90 dev->mode = SMBUS_RECV_BYTE;
92 case SMBUS_WRITE_DATA:
93 if (dev->data_len == 0) {
94 BADF("Read after write with no data\n");
95 dev->mode = SMBUS_CONFUSED;
97 if (dev->data_len > 1) {
100 dev->command = dev->data_buf[0];
101 DPRINTF("%02x: Command %d\n", dev->i2c.address,
104 DPRINTF("Read mode\n");
106 dev->mode = SMBUS_READ_DATA;
110 BADF("Unexpected recv start condition in state %d\n", dev->mode);
111 dev->mode = SMBUS_CONFUSED;
118 case SMBUS_WRITE_DATA:
121 case SMBUS_RECV_BYTE:
122 smbus_do_quick_cmd(dev, 1);
124 case SMBUS_READ_DATA:
125 BADF("Unexpected stop during receive\n");
131 dev->mode = SMBUS_IDLE;
140 case SMBUS_READ_DATA:
141 dev->mode = SMBUS_DONE;
144 BADF("Unexpected NACK in state %d\n", dev->mode);
145 dev->mode = SMBUS_CONFUSED;
151 static int smbus_i2c_recv(i2c_slave *s)
153 SMBusDeviceInfo *t = container_of(s->info, SMBusDeviceInfo, i2c);
154 SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
158 case SMBUS_RECV_BYTE:
159 if (t->receive_byte) {
160 ret = t->receive_byte(dev);
164 DPRINTF("Receive Byte %02x\n", ret);
165 dev->mode = SMBUS_DONE;
167 case SMBUS_READ_DATA:
169 ret = t->read_data(dev, dev->command, dev->data_len);
174 DPRINTF("Read data %02x\n", ret);
177 BADF("Unexpected read in state %d\n", dev->mode);
178 dev->mode = SMBUS_CONFUSED;
185 static int smbus_i2c_send(i2c_slave *s, uint8_t data)
187 SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, s);
190 case SMBUS_WRITE_DATA:
191 DPRINTF("Write data %02x\n", data);
192 dev->data_buf[dev->data_len++] = data;
195 BADF("Unexpected write in state %d\n", dev->mode);
201 static int smbus_device_init(i2c_slave *i2c)
203 SMBusDeviceInfo *t = container_of(i2c->info, SMBusDeviceInfo, i2c);
204 SMBusDevice *dev = FROM_I2C_SLAVE(SMBusDevice, i2c);
209 void smbus_register_device(SMBusDeviceInfo *info)
211 assert(info->i2c.qdev.size >= sizeof(SMBusDevice));
212 info->i2c.init = smbus_device_init;
213 info->i2c.event = smbus_i2c_event;
214 info->i2c.recv = smbus_i2c_recv;
215 info->i2c.send = smbus_i2c_send;
216 i2c_register_slave(&info->i2c);
219 /* Master device commands. */
220 void smbus_quick_command(i2c_bus *bus, uint8_t addr, int read)
222 i2c_start_transfer(bus, addr, read);
223 i2c_end_transfer(bus);
226 uint8_t smbus_receive_byte(i2c_bus *bus, uint8_t addr)
230 i2c_start_transfer(bus, addr, 1);
231 data = i2c_recv(bus);
233 i2c_end_transfer(bus);
237 void smbus_send_byte(i2c_bus *bus, uint8_t addr, uint8_t data)
239 i2c_start_transfer(bus, addr, 0);
241 i2c_end_transfer(bus);
244 uint8_t smbus_read_byte(i2c_bus *bus, uint8_t addr, uint8_t command)
247 i2c_start_transfer(bus, addr, 0);
248 i2c_send(bus, command);
249 i2c_start_transfer(bus, addr, 1);
250 data = i2c_recv(bus);
252 i2c_end_transfer(bus);
256 void smbus_write_byte(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t data)
258 i2c_start_transfer(bus, addr, 0);
259 i2c_send(bus, command);
261 i2c_end_transfer(bus);
264 uint16_t smbus_read_word(i2c_bus *bus, uint8_t addr, uint8_t command)
267 i2c_start_transfer(bus, addr, 0);
268 i2c_send(bus, command);
269 i2c_start_transfer(bus, addr, 1);
270 data = i2c_recv(bus);
271 data |= i2c_recv(bus) << 8;
273 i2c_end_transfer(bus);
277 void smbus_write_word(i2c_bus *bus, uint8_t addr, uint8_t command, uint16_t data)
279 i2c_start_transfer(bus, addr, 0);
280 i2c_send(bus, command);
281 i2c_send(bus, data & 0xff);
282 i2c_send(bus, data >> 8);
283 i2c_end_transfer(bus);
286 int smbus_read_block(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t *data)
291 i2c_start_transfer(bus, addr, 0);
292 i2c_send(bus, command);
293 i2c_start_transfer(bus, addr, 1);
297 for (i = 0; i < len; i++)
298 data[i] = i2c_recv(bus);
300 i2c_end_transfer(bus);
304 void smbus_write_block(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t *data,
312 i2c_start_transfer(bus, addr, 0);
313 i2c_send(bus, command);
315 for (i = 0; i < len; i++)
316 i2c_send(bus, data[i]);
317 i2c_end_transfer(bus);