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. */
12 #include "qemu/osdep.h"
14 #include "hw/i2c/i2c.h"
15 #include "hw/i2c/smbus.h"
17 //#define DEBUG_SMBUS 1
20 #define DPRINTF(fmt, ...) \
21 do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
26 #define BADF(fmt, ...) \
27 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
39 static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
41 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
43 DPRINTF("Quick Command %d\n", recv);
45 sc->quick_cmd(dev, recv);
49 static void smbus_do_write(SMBusDevice *dev)
51 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
53 if (dev->data_len == 0) {
54 smbus_do_quick_cmd(dev, 0);
55 } else if (dev->data_len == 1) {
56 DPRINTF("Send Byte\n");
58 sc->send_byte(dev, dev->data_buf[0]);
61 dev->command = dev->data_buf[0];
62 DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
64 sc->write_data(dev, dev->command, dev->data_buf + 1,
70 static int smbus_i2c_event(I2CSlave *s, enum i2c_event event)
72 SMBusDevice *dev = SMBUS_DEVICE(s);
78 DPRINTF("Incoming data\n");
79 dev->mode = SMBUS_WRITE_DATA;
82 BADF("Unexpected send start condition in state %d\n", dev->mode);
83 dev->mode = SMBUS_CONFUSED;
91 DPRINTF("Read mode\n");
92 dev->mode = SMBUS_RECV_BYTE;
94 case SMBUS_WRITE_DATA:
95 if (dev->data_len == 0) {
96 BADF("Read after write with no data\n");
97 dev->mode = SMBUS_CONFUSED;
99 if (dev->data_len > 1) {
102 dev->command = dev->data_buf[0];
103 DPRINTF("%02x: Command %d\n", dev->i2c.address,
106 DPRINTF("Read mode\n");
108 dev->mode = SMBUS_READ_DATA;
112 BADF("Unexpected recv start condition in state %d\n", dev->mode);
113 dev->mode = SMBUS_CONFUSED;
120 case SMBUS_WRITE_DATA:
123 case SMBUS_RECV_BYTE:
124 smbus_do_quick_cmd(dev, 1);
126 case SMBUS_READ_DATA:
127 BADF("Unexpected stop during receive\n");
133 dev->mode = SMBUS_IDLE;
142 case SMBUS_READ_DATA:
143 dev->mode = SMBUS_DONE;
146 BADF("Unexpected NACK in state %d\n", dev->mode);
147 dev->mode = SMBUS_CONFUSED;
155 static int smbus_i2c_recv(I2CSlave *s)
157 SMBusDevice *dev = SMBUS_DEVICE(s);
158 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
162 case SMBUS_RECV_BYTE:
163 if (sc->receive_byte) {
164 ret = sc->receive_byte(dev);
168 DPRINTF("Receive Byte %02x\n", ret);
169 dev->mode = SMBUS_DONE;
171 case SMBUS_READ_DATA:
173 ret = sc->read_data(dev, dev->command, dev->data_len);
178 DPRINTF("Read data %02x\n", ret);
181 BADF("Unexpected read in state %d\n", dev->mode);
182 dev->mode = SMBUS_CONFUSED;
189 static int smbus_i2c_send(I2CSlave *s, uint8_t data)
191 SMBusDevice *dev = SMBUS_DEVICE(s);
194 case SMBUS_WRITE_DATA:
195 DPRINTF("Write data %02x\n", data);
196 if (dev->data_len >= sizeof(dev->data_buf)) {
197 BADF("Too many bytes sent\n");
199 dev->data_buf[dev->data_len++] = data;
203 BADF("Unexpected write in state %d\n", dev->mode);
209 /* Master device commands. */
210 int smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
212 if (i2c_start_transfer(bus, addr, read)) {
215 i2c_end_transfer(bus);
219 int smbus_receive_byte(I2CBus *bus, uint8_t addr)
223 if (i2c_start_transfer(bus, addr, 1)) {
226 data = i2c_recv(bus);
228 i2c_end_transfer(bus);
232 int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
234 if (i2c_start_transfer(bus, addr, 0)) {
238 i2c_end_transfer(bus);
242 int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
245 if (i2c_start_transfer(bus, addr, 0)) {
248 i2c_send(bus, command);
249 if (i2c_start_transfer(bus, addr, 1)) {
250 i2c_end_transfer(bus);
253 data = i2c_recv(bus);
255 i2c_end_transfer(bus);
259 int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
261 if (i2c_start_transfer(bus, addr, 0)) {
264 i2c_send(bus, command);
266 i2c_end_transfer(bus);
270 int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
273 if (i2c_start_transfer(bus, addr, 0)) {
276 i2c_send(bus, command);
277 if (i2c_start_transfer(bus, addr, 1)) {
278 i2c_end_transfer(bus);
281 data = i2c_recv(bus);
282 data |= i2c_recv(bus) << 8;
284 i2c_end_transfer(bus);
288 int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
290 if (i2c_start_transfer(bus, addr, 0)) {
293 i2c_send(bus, command);
294 i2c_send(bus, data & 0xff);
295 i2c_send(bus, data >> 8);
296 i2c_end_transfer(bus);
300 int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
301 int len, bool recv_len, bool send_cmd)
307 if (i2c_start_transfer(bus, addr, 0)) {
310 i2c_send(bus, command);
312 if (i2c_start_transfer(bus, addr, 1)) {
314 i2c_end_transfer(bus);
319 rlen = i2c_recv(bus);
326 for (i = 0; i < rlen; i++) {
327 data[i] = i2c_recv(bus);
330 i2c_end_transfer(bus);
334 int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
335 int len, bool send_len)
342 if (i2c_start_transfer(bus, addr, 0)) {
345 i2c_send(bus, command);
349 for (i = 0; i < len; i++) {
350 i2c_send(bus, data[i]);
352 i2c_end_transfer(bus);
356 static void smbus_device_class_init(ObjectClass *klass, void *data)
358 I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
360 sc->event = smbus_i2c_event;
361 sc->recv = smbus_i2c_recv;
362 sc->send = smbus_i2c_send;
365 static const TypeInfo smbus_device_type_info = {
366 .name = TYPE_SMBUS_DEVICE,
367 .parent = TYPE_I2C_SLAVE,
368 .instance_size = sizeof(SMBusDevice),
370 .class_size = sizeof(SMBusDeviceClass),
371 .class_init = smbus_device_class_init,
374 static void smbus_device_register_types(void)
376 type_register_static(&smbus_device_type_info);
379 type_init(smbus_device_register_types)