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 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
42 DPRINTF("Quick Command %d\n", recv
);
44 sc
->quick_cmd(dev
, recv
);
48 static void smbus_do_write(SMBusDevice
*dev
)
50 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
52 if (dev
->data_len
== 0) {
53 smbus_do_quick_cmd(dev
, 0);
54 } else if (dev
->data_len
== 1) {
55 DPRINTF("Send Byte\n");
57 sc
->send_byte(dev
, dev
->data_buf
[0]);
60 dev
->command
= dev
->data_buf
[0];
61 DPRINTF("Command %d len %d\n", dev
->command
, dev
->data_len
- 1);
63 sc
->write_data(dev
, dev
->command
, dev
->data_buf
+ 1,
69 static void smbus_i2c_event(I2CSlave
*s
, enum i2c_event event
)
71 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
77 DPRINTF("Incoming data\n");
78 dev
->mode
= SMBUS_WRITE_DATA
;
81 BADF("Unexpected send start condition in state %d\n", dev
->mode
);
82 dev
->mode
= SMBUS_CONFUSED
;
90 DPRINTF("Read mode\n");
91 dev
->mode
= SMBUS_RECV_BYTE
;
93 case SMBUS_WRITE_DATA
:
94 if (dev
->data_len
== 0) {
95 BADF("Read after write with no data\n");
96 dev
->mode
= SMBUS_CONFUSED
;
98 if (dev
->data_len
> 1) {
101 dev
->command
= dev
->data_buf
[0];
102 DPRINTF("%02x: Command %d\n", dev
->i2c
.address
,
105 DPRINTF("Read mode\n");
107 dev
->mode
= SMBUS_READ_DATA
;
111 BADF("Unexpected recv start condition in state %d\n", dev
->mode
);
112 dev
->mode
= SMBUS_CONFUSED
;
119 case SMBUS_WRITE_DATA
:
122 case SMBUS_RECV_BYTE
:
123 smbus_do_quick_cmd(dev
, 1);
125 case SMBUS_READ_DATA
:
126 BADF("Unexpected stop during receive\n");
132 dev
->mode
= SMBUS_IDLE
;
141 case SMBUS_READ_DATA
:
142 dev
->mode
= SMBUS_DONE
;
145 BADF("Unexpected NACK in state %d\n", dev
->mode
);
146 dev
->mode
= SMBUS_CONFUSED
;
152 static int smbus_i2c_recv(I2CSlave
*s
)
154 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
155 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
159 case SMBUS_RECV_BYTE
:
160 if (sc
->receive_byte
) {
161 ret
= sc
->receive_byte(dev
);
165 DPRINTF("Receive Byte %02x\n", ret
);
166 dev
->mode
= SMBUS_DONE
;
168 case SMBUS_READ_DATA
:
170 ret
= sc
->read_data(dev
, dev
->command
, dev
->data_len
);
175 DPRINTF("Read data %02x\n", ret
);
178 BADF("Unexpected read in state %d\n", dev
->mode
);
179 dev
->mode
= SMBUS_CONFUSED
;
186 static int smbus_i2c_send(I2CSlave
*s
, uint8_t data
)
188 SMBusDevice
*dev
= SMBUS_DEVICE(s
);
191 case SMBUS_WRITE_DATA
:
192 DPRINTF("Write data %02x\n", data
);
193 dev
->data_buf
[dev
->data_len
++] = data
;
196 BADF("Unexpected write in state %d\n", dev
->mode
);
202 static int smbus_device_init(I2CSlave
*i2c
)
204 SMBusDevice
*dev
= SMBUS_DEVICE(i2c
);
205 SMBusDeviceClass
*sc
= SMBUS_DEVICE_GET_CLASS(dev
);
207 return sc
->init(dev
);
210 void smbus_register_device(DeviceInfo
*info
)
212 assert(info
->size
>= sizeof(SMBusDevice
));
213 i2c_register_slave_subclass(info
, TYPE_SMBUS_DEVICE
);
216 /* Master device commands. */
217 void smbus_quick_command(i2c_bus
*bus
, uint8_t addr
, int read
)
219 i2c_start_transfer(bus
, addr
, read
);
220 i2c_end_transfer(bus
);
223 uint8_t smbus_receive_byte(i2c_bus
*bus
, uint8_t addr
)
227 i2c_start_transfer(bus
, addr
, 1);
228 data
= i2c_recv(bus
);
230 i2c_end_transfer(bus
);
234 void smbus_send_byte(i2c_bus
*bus
, uint8_t addr
, uint8_t data
)
236 i2c_start_transfer(bus
, addr
, 0);
238 i2c_end_transfer(bus
);
241 uint8_t smbus_read_byte(i2c_bus
*bus
, uint8_t addr
, uint8_t command
)
244 i2c_start_transfer(bus
, addr
, 0);
245 i2c_send(bus
, command
);
246 i2c_start_transfer(bus
, addr
, 1);
247 data
= i2c_recv(bus
);
249 i2c_end_transfer(bus
);
253 void smbus_write_byte(i2c_bus
*bus
, uint8_t addr
, uint8_t command
, uint8_t data
)
255 i2c_start_transfer(bus
, addr
, 0);
256 i2c_send(bus
, command
);
258 i2c_end_transfer(bus
);
261 uint16_t smbus_read_word(i2c_bus
*bus
, uint8_t addr
, uint8_t command
)
264 i2c_start_transfer(bus
, addr
, 0);
265 i2c_send(bus
, command
);
266 i2c_start_transfer(bus
, addr
, 1);
267 data
= i2c_recv(bus
);
268 data
|= i2c_recv(bus
) << 8;
270 i2c_end_transfer(bus
);
274 void smbus_write_word(i2c_bus
*bus
, uint8_t addr
, uint8_t command
, uint16_t data
)
276 i2c_start_transfer(bus
, addr
, 0);
277 i2c_send(bus
, command
);
278 i2c_send(bus
, data
& 0xff);
279 i2c_send(bus
, data
>> 8);
280 i2c_end_transfer(bus
);
283 int smbus_read_block(i2c_bus
*bus
, uint8_t addr
, uint8_t command
, uint8_t *data
)
288 i2c_start_transfer(bus
, addr
, 0);
289 i2c_send(bus
, command
);
290 i2c_start_transfer(bus
, addr
, 1);
294 for (i
= 0; i
< len
; i
++)
295 data
[i
] = i2c_recv(bus
);
297 i2c_end_transfer(bus
);
301 void smbus_write_block(i2c_bus
*bus
, uint8_t addr
, uint8_t command
, uint8_t *data
,
309 i2c_start_transfer(bus
, addr
, 0);
310 i2c_send(bus
, command
);
312 for (i
= 0; i
< len
; i
++)
313 i2c_send(bus
, data
[i
]);
314 i2c_end_transfer(bus
);
317 static void smbus_device_class_init(ObjectClass
*klass
, void *data
)
319 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
321 sc
->init
= smbus_device_init
;
322 sc
->event
= smbus_i2c_event
;
323 sc
->recv
= smbus_i2c_recv
;
324 sc
->send
= smbus_i2c_send
;
327 static TypeInfo smbus_device_type_info
= {
328 .name
= TYPE_SMBUS_DEVICE
,
329 .parent
= TYPE_I2C_SLAVE
,
330 .instance_size
= sizeof(SMBusDevice
),
332 .class_size
= sizeof(SMBusDeviceClass
),
333 .class_init
= smbus_device_class_init
,
336 static void smbus_device_register_devices(void)
338 type_register_static(&smbus_device_type_info
);
341 device_init(smbus_device_register_devices
);