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"
17 #include "hw/i2c/i2c.h"
18 #include "hw/i2c/smbus_slave.h"
19 #include "migration/vmstate.h"
20 #include "qemu/module.h"
22 //#define DEBUG_SMBUS 1
25 #define DPRINTF(fmt, ...) \
26 do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
43 static void smbus_do_quick_cmd(SMBusDevice
*dev
, int recv
)
45 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
47 DPRINTF("Quick Command %d\n", recv
);
49 sc
->quick_cmd(dev
, recv
);
53 static void smbus_do_write(SMBusDevice
*dev
)
55 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
57 DPRINTF("Command %d len %d\n", dev
->data_buf
[0], dev
->data_len
);
59 sc
->write_data(dev
, dev
->data_buf
, dev
->data_len
);
63 static int smbus_i2c_event(I2CSlave
*s
, enum i2c_event event
)
65 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
71 DPRINTF("Incoming data\n");
72 dev
->mode
= SMBUS_WRITE_DATA
;
76 BADF("Unexpected send start condition in state %d\n", dev
->mode
);
77 dev
->mode
= SMBUS_CONFUSED
;
85 DPRINTF("Read mode\n");
86 dev
->mode
= SMBUS_READ_DATA
;
89 case SMBUS_WRITE_DATA
:
90 if (dev
->data_len
== 0) {
91 BADF("Read after write with no data\n");
92 dev
->mode
= SMBUS_CONFUSED
;
95 DPRINTF("Read mode\n");
96 dev
->mode
= SMBUS_READ_DATA
;
101 BADF("Unexpected recv start condition in state %d\n", dev
->mode
);
102 dev
->mode
= SMBUS_CONFUSED
;
108 if (dev
->data_len
== 0) {
109 if (dev
->mode
== SMBUS_WRITE_DATA
|| dev
->mode
== SMBUS_READ_DATA
) {
110 smbus_do_quick_cmd(dev
, dev
->mode
== SMBUS_READ_DATA
);
114 case SMBUS_WRITE_DATA
:
118 case SMBUS_READ_DATA
:
119 BADF("Unexpected stop during receive\n");
127 dev
->mode
= SMBUS_IDLE
;
137 case SMBUS_READ_DATA
:
138 dev
->mode
= SMBUS_DONE
;
142 BADF("Unexpected NACK in state %d\n", dev
->mode
);
143 dev
->mode
= SMBUS_CONFUSED
;
151 static uint8_t smbus_i2c_recv(I2CSlave
*s
)
153 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
154 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
158 case SMBUS_READ_DATA
:
159 if (sc
->receive_byte
) {
160 ret
= sc
->receive_byte(dev
);
162 DPRINTF("Read data %02x\n", ret
);
166 BADF("Unexpected read in state %d\n", dev
->mode
);
167 dev
->mode
= SMBUS_CONFUSED
;
174 static int smbus_i2c_send(I2CSlave
*s
, uint8_t data
)
176 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
179 case SMBUS_WRITE_DATA
:
180 DPRINTF("Write data %02x\n", data
);
181 if (dev
->data_len
>= sizeof(dev
->data_buf
)) {
182 BADF("Too many bytes sent\n");
184 dev
->data_buf
[dev
->data_len
++] = data
;
189 BADF("Unexpected write in state %d\n", dev
->mode
);
196 static void smbus_device_class_init(ObjectClass
*klass
, void *data
)
198 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
200 sc
->event
= smbus_i2c_event
;
201 sc
->recv
= smbus_i2c_recv
;
202 sc
->send
= smbus_i2c_send
;
205 bool smbus_vmstate_needed(SMBusDevice
*dev
)
207 return dev
->mode
!= SMBUS_IDLE
;
210 const VMStateDescription vmstate_smbus_device
= {
211 .name
= TYPE_SMBUS_DEVICE
,
213 .minimum_version_id
= 1,
214 .fields
= (VMStateField
[]) {
215 VMSTATE_I2C_SLAVE(i2c
, SMBusDevice
),
216 VMSTATE_INT32(mode
, SMBusDevice
),
217 VMSTATE_INT32(data_len
, SMBusDevice
),
218 VMSTATE_UINT8_ARRAY(data_buf
, SMBusDevice
, SMBUS_DATA_MAX_LEN
),
219 VMSTATE_END_OF_LIST()
223 static const TypeInfo smbus_device_type_info
= {
224 .name
= TYPE_SMBUS_DEVICE
,
225 .parent
= TYPE_I2C_SLAVE
,
226 .instance_size
= sizeof(SMBusDevice
),
228 .class_size
= sizeof(SMBusDeviceClass
),
229 .class_init
= smbus_device_class_init
,
232 static void smbus_device_register_types(void)
234 type_register_static(&smbus_device_type_info
);
237 type_init(smbus_device_register_types
)