2 * QEMU SMBus device emulation.
4 * This code is a helper for SMBus device emulation. It implements an
5 * I2C device inteface and runs the SMBus protocol from the device
6 * point of view and maps those to simple calls to emulate.
8 * Copyright (c) 2007 CodeSourcery.
9 * Written by Paul Brook
11 * This code is licensed under the LGPL.
14 /* TODO: Implement PEC. */
16 #include "qemu/osdep.h"
18 #include "hw/i2c/i2c.h"
19 #include "hw/i2c/smbus_slave.h"
21 //#define DEBUG_SMBUS 1
24 #define DPRINTF(fmt, ...) \
25 do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
26 #define BADF(fmt, ...) \
27 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
29 #define DPRINTF(fmt, ...) do {} while(0)
30 #define BADF(fmt, ...) \
31 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
42 static void smbus_do_quick_cmd(SMBusDevice
*dev
, int recv
)
44 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
46 DPRINTF("Quick Command %d\n", recv
);
48 sc
->quick_cmd(dev
, recv
);
52 static void smbus_do_write(SMBusDevice
*dev
)
54 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
56 DPRINTF("Command %d len %d\n", dev
->data_buf
[0], dev
->data_len
);
58 sc
->write_data(dev
, dev
->data_buf
, dev
->data_len
);
62 static int smbus_i2c_event(I2CSlave
*s
, enum i2c_event event
)
64 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
70 DPRINTF("Incoming data\n");
71 dev
->mode
= SMBUS_WRITE_DATA
;
75 BADF("Unexpected send start condition in state %d\n", dev
->mode
);
76 dev
->mode
= SMBUS_CONFUSED
;
84 DPRINTF("Read mode\n");
85 dev
->mode
= SMBUS_READ_DATA
;
88 case SMBUS_WRITE_DATA
:
89 if (dev
->data_len
== 0) {
90 BADF("Read after write with no data\n");
91 dev
->mode
= SMBUS_CONFUSED
;
94 DPRINTF("Read mode\n");
95 dev
->mode
= SMBUS_READ_DATA
;
100 BADF("Unexpected recv start condition in state %d\n", dev
->mode
);
101 dev
->mode
= SMBUS_CONFUSED
;
107 if (dev
->data_len
== 0) {
108 if (dev
->mode
== SMBUS_WRITE_DATA
|| dev
->mode
== SMBUS_READ_DATA
) {
109 smbus_do_quick_cmd(dev
, dev
->mode
== SMBUS_READ_DATA
);
113 case SMBUS_WRITE_DATA
:
117 case SMBUS_READ_DATA
:
118 BADF("Unexpected stop during receive\n");
126 dev
->mode
= SMBUS_IDLE
;
136 case SMBUS_READ_DATA
:
137 dev
->mode
= SMBUS_DONE
;
141 BADF("Unexpected NACK in state %d\n", dev
->mode
);
142 dev
->mode
= SMBUS_CONFUSED
;
150 static uint8_t smbus_i2c_recv(I2CSlave
*s
)
152 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
153 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
157 case SMBUS_READ_DATA
:
158 if (sc
->receive_byte
) {
159 ret
= sc
->receive_byte(dev
);
161 DPRINTF("Read data %02x\n", ret
);
165 BADF("Unexpected read in state %d\n", dev
->mode
);
166 dev
->mode
= SMBUS_CONFUSED
;
173 static int smbus_i2c_send(I2CSlave
*s
, uint8_t data
)
175 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
178 case SMBUS_WRITE_DATA
:
179 DPRINTF("Write data %02x\n", data
);
180 if (dev
->data_len
>= sizeof(dev
->data_buf
)) {
181 BADF("Too many bytes sent\n");
183 dev
->data_buf
[dev
->data_len
++] = data
;
188 BADF("Unexpected write in state %d\n", dev
->mode
);
195 static void smbus_device_class_init(ObjectClass
*klass
, void *data
)
197 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
199 sc
->event
= smbus_i2c_event
;
200 sc
->recv
= smbus_i2c_recv
;
201 sc
->send
= smbus_i2c_send
;
204 bool smbus_vmstate_needed(SMBusDevice
*dev
)
206 return dev
->mode
!= SMBUS_IDLE
;
209 const VMStateDescription vmstate_smbus_device
= {
210 .name
= TYPE_SMBUS_DEVICE
,
212 .minimum_version_id
= 1,
213 .fields
= (VMStateField
[]) {
214 VMSTATE_I2C_SLAVE(i2c
, SMBusDevice
),
215 VMSTATE_INT32(mode
, SMBusDevice
),
216 VMSTATE_INT32(data_len
, SMBusDevice
),
217 VMSTATE_UINT8_ARRAY(data_buf
, SMBusDevice
, SMBUS_DATA_MAX_LEN
),
218 VMSTATE_END_OF_LIST()
222 static const TypeInfo smbus_device_type_info
= {
223 .name
= TYPE_SMBUS_DEVICE
,
224 .parent
= TYPE_I2C_SLAVE
,
225 .instance_size
= sizeof(SMBusDevice
),
227 .class_size
= sizeof(SMBusDeviceClass
),
228 .class_init
= smbus_device_class_init
,
231 static void smbus_device_register_types(void)
233 type_register_static(&smbus_device_type_info
);
236 type_init(smbus_device_register_types
)