2 * QEMU SMBus device emulation.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced 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 DPRINTF("Quick Command %d\n", recv
);
42 dev
->quick_cmd(dev
, recv
);
45 static void smbus_do_write(SMBusDevice
*dev
)
47 if (dev
->data_len
== 0) {
48 smbus_do_quick_cmd(dev
, 0);
49 } else if (dev
->data_len
== 1) {
50 DPRINTF("Send Byte\n");
52 dev
->send_byte(dev
, dev
->data_buf
[0]);
55 dev
->command
= dev
->data_buf
[0];
56 DPRINTF("Command %d len %d\n", dev
->command
, dev
->data_len
- 1);
57 if (dev
->write_data
) {
58 dev
->write_data(dev
, dev
->command
, dev
->data_buf
+ 1,
64 static void smbus_i2c_event(i2c_slave
*s
, enum i2c_event event
)
66 SMBusDevice
*dev
= (SMBusDevice
*)s
;
71 DPRINTF("Incoming data\n");
72 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_RECV_BYTE
;
87 case SMBUS_WRITE_DATA
:
88 if (dev
->data_len
== 0) {
89 BADF("Read after write with no data\n");
90 dev
->mode
= SMBUS_CONFUSED
;
92 if (dev
->data_len
> 1) {
95 dev
->command
= dev
->data_buf
[0];
96 DPRINTF("%02x: Command %d\n", dev
->i2c
.address
,
99 DPRINTF("Read mode\n");
101 dev
->mode
= SMBUS_READ_DATA
;
105 BADF("Unexpected recv start condition in state %d\n", dev
->mode
);
106 dev
->mode
= SMBUS_CONFUSED
;
113 case SMBUS_WRITE_DATA
:
116 case SMBUS_RECV_BYTE
:
117 smbus_do_quick_cmd(dev
, 1);
119 case SMBUS_READ_DATA
:
120 BADF("Unexpected stop during receive\n");
126 dev
->mode
= SMBUS_IDLE
;
135 case SMBUS_READ_DATA
:
136 dev
->mode
= SMBUS_DONE
;
139 BADF("Unexpected NACK in state %d\n", dev
->mode
);
140 dev
->mode
= SMBUS_CONFUSED
;
146 static int smbus_i2c_recv(i2c_slave
*s
)
148 SMBusDevice
*dev
= (SMBusDevice
*)s
;
152 case SMBUS_RECV_BYTE
:
153 if (dev
->receive_byte
) {
154 ret
= dev
->receive_byte(dev
);
158 DPRINTF("Receive Byte %02x\n", ret
);
159 dev
->mode
= SMBUS_DONE
;
161 case SMBUS_READ_DATA
:
162 if (dev
->read_data
) {
163 ret
= dev
->read_data(dev
, dev
->command
, dev
->data_len
);
168 DPRINTF("Read data %02x\n", ret
);
171 BADF("Unexpected read in state %d\n", dev
->mode
);
172 dev
->mode
= SMBUS_CONFUSED
;
179 static int smbus_i2c_send(i2c_slave
*s
, uint8_t data
)
181 SMBusDevice
*dev
= (SMBusDevice
*)s
;
183 case SMBUS_WRITE_DATA
:
184 DPRINTF("Write data %02x\n", data
);
185 dev
->data_buf
[dev
->data_len
++] = data
;
188 BADF("Unexpected write in state %d\n", dev
->mode
);
194 SMBusDevice
*smbus_device_init(i2c_bus
*bus
, int address
, int size
)
198 if (size
< sizeof(SMBusDevice
))
199 hw_error("SMBus struct too small");
201 dev
= (SMBusDevice
*)i2c_slave_init(bus
, address
, size
);
202 dev
->i2c
.event
= smbus_i2c_event
;
203 dev
->i2c
.recv
= smbus_i2c_recv
;
204 dev
->i2c
.send
= smbus_i2c_send
;
209 /* Master device commands. */
210 void smbus_quick_command(i2c_bus
*bus
, int addr
, int read
)
212 i2c_start_transfer(bus
, addr
, read
);
213 i2c_end_transfer(bus
);
216 uint8_t smbus_receive_byte(i2c_bus
*bus
, int addr
)
220 i2c_start_transfer(bus
, addr
, 1);
221 data
= i2c_recv(bus
);
223 i2c_end_transfer(bus
);
227 void smbus_send_byte(i2c_bus
*bus
, int addr
, uint8_t data
)
229 i2c_start_transfer(bus
, addr
, 0);
231 i2c_end_transfer(bus
);
234 uint8_t smbus_read_byte(i2c_bus
*bus
, int addr
, uint8_t command
)
237 i2c_start_transfer(bus
, addr
, 0);
238 i2c_send(bus
, command
);
239 i2c_start_transfer(bus
, addr
, 1);
240 data
= i2c_recv(bus
);
242 i2c_end_transfer(bus
);
246 void smbus_write_byte(i2c_bus
*bus
, int addr
, uint8_t command
, uint8_t data
)
248 i2c_start_transfer(bus
, addr
, 0);
249 i2c_send(bus
, command
);
251 i2c_end_transfer(bus
);
254 uint16_t smbus_read_word(i2c_bus
*bus
, int addr
, uint8_t command
)
257 i2c_start_transfer(bus
, addr
, 0);
258 i2c_send(bus
, command
);
259 i2c_start_transfer(bus
, addr
, 1);
260 data
= i2c_recv(bus
);
261 data
|= i2c_recv(bus
) << 8;
263 i2c_end_transfer(bus
);
267 void smbus_write_word(i2c_bus
*bus
, int addr
, uint8_t command
, uint16_t data
)
269 i2c_start_transfer(bus
, addr
, 0);
270 i2c_send(bus
, command
);
271 i2c_send(bus
, data
& 0xff);
272 i2c_send(bus
, data
>> 8);
273 i2c_end_transfer(bus
);
276 int smbus_read_block(i2c_bus
*bus
, int addr
, uint8_t command
, uint8_t *data
)
281 i2c_start_transfer(bus
, addr
, 0);
282 i2c_send(bus
, command
);
283 i2c_start_transfer(bus
, addr
, 1);
287 for (i
= 0; i
< len
; i
++)
288 data
[i
] = i2c_recv(bus
);
290 i2c_end_transfer(bus
);
294 void smbus_write_block(i2c_bus
*bus
, int addr
, uint8_t command
, uint8_t *data
,
302 i2c_start_transfer(bus
, addr
, 0);
303 i2c_send(bus
, command
);
305 for (i
= 0; i
< len
; i
++)
306 i2c_send(bus
, data
[i
]);
307 i2c_end_transfer(bus
);