blob: 6e27ae8bd2b7e3e184013fbf29d7e0f7a48a4c77 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook0ff596d2007-05-23 00:03:59 +00002 * QEMU SMBus device emulation.
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the LGPL.
pbrook0ff596d2007-05-23 00:03:59 +00008 */
9
10/* TODO: Implement PEC. */
11
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010012#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010013#include "hw/i2c/i2c.h"
14#include "hw/i2c/smbus.h"
pbrook0ff596d2007-05-23 00:03:59 +000015
16//#define DEBUG_SMBUS 1
17
18#ifdef DEBUG_SMBUS
Blue Swirl001faf32009-05-13 17:53:17 +000019#define DPRINTF(fmt, ...) \
20do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
21#define BADF(fmt, ...) \
22do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
pbrook0ff596d2007-05-23 00:03:59 +000023#else
Blue Swirl001faf32009-05-13 17:53:17 +000024#define DPRINTF(fmt, ...) do {} while(0)
25#define BADF(fmt, ...) \
26do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
pbrook0ff596d2007-05-23 00:03:59 +000027#endif
28
29enum {
30 SMBUS_IDLE,
31 SMBUS_WRITE_DATA,
32 SMBUS_RECV_BYTE,
33 SMBUS_READ_DATA,
34 SMBUS_DONE,
35 SMBUS_CONFUSED = -1
36};
37
38static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
39{
Anthony Liguorib5ea9322011-12-04 20:39:20 -060040 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
Paul Brook1ea96672009-05-14 22:35:08 +010041
pbrook0ff596d2007-05-23 00:03:59 +000042 DPRINTF("Quick Command %d\n", recv);
Anthony Liguorib5ea9322011-12-04 20:39:20 -060043 if (sc->quick_cmd) {
44 sc->quick_cmd(dev, recv);
45 }
pbrook0ff596d2007-05-23 00:03:59 +000046}
47
48static void smbus_do_write(SMBusDevice *dev)
49{
Anthony Liguorib5ea9322011-12-04 20:39:20 -060050 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
Paul Brook1ea96672009-05-14 22:35:08 +010051
pbrook0ff596d2007-05-23 00:03:59 +000052 if (dev->data_len == 0) {
53 smbus_do_quick_cmd(dev, 0);
54 } else if (dev->data_len == 1) {
55 DPRINTF("Send Byte\n");
Anthony Liguorib5ea9322011-12-04 20:39:20 -060056 if (sc->send_byte) {
57 sc->send_byte(dev, dev->data_buf[0]);
pbrook0ff596d2007-05-23 00:03:59 +000058 }
59 } else {
60 dev->command = dev->data_buf[0];
61 DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
Anthony Liguorib5ea9322011-12-04 20:39:20 -060062 if (sc->write_data) {
63 sc->write_data(dev, dev->command, dev->data_buf + 1,
64 dev->data_len - 1);
pbrook0ff596d2007-05-23 00:03:59 +000065 }
66 }
67}
68
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060069static void smbus_i2c_event(I2CSlave *s, enum i2c_event event)
pbrook0ff596d2007-05-23 00:03:59 +000070{
Anthony Liguorib5ea9322011-12-04 20:39:20 -060071 SMBusDevice *dev = SMBUS_DEVICE(s);
Paul Brook1ea96672009-05-14 22:35:08 +010072
pbrook0ff596d2007-05-23 00:03:59 +000073 switch (event) {
74 case I2C_START_SEND:
75 switch (dev->mode) {
76 case SMBUS_IDLE:
77 DPRINTF("Incoming data\n");
78 dev->mode = SMBUS_WRITE_DATA;
79 break;
80 default:
81 BADF("Unexpected send start condition in state %d\n", dev->mode);
82 dev->mode = SMBUS_CONFUSED;
83 break;
84 }
85 break;
86
87 case I2C_START_RECV:
88 switch (dev->mode) {
89 case SMBUS_IDLE:
90 DPRINTF("Read mode\n");
91 dev->mode = SMBUS_RECV_BYTE;
92 break;
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;
97 } else {
98 if (dev->data_len > 1) {
99 smbus_do_write(dev);
100 } else {
101 dev->command = dev->data_buf[0];
102 DPRINTF("%02x: Command %d\n", dev->i2c.address,
103 dev->command);
104 }
105 DPRINTF("Read mode\n");
106 dev->data_len = 0;
107 dev->mode = SMBUS_READ_DATA;
108 }
109 break;
110 default:
111 BADF("Unexpected recv start condition in state %d\n", dev->mode);
112 dev->mode = SMBUS_CONFUSED;
113 break;
114 }
115 break;
116
117 case I2C_FINISH:
118 switch (dev->mode) {
119 case SMBUS_WRITE_DATA:
120 smbus_do_write(dev);
121 break;
122 case SMBUS_RECV_BYTE:
123 smbus_do_quick_cmd(dev, 1);
124 break;
125 case SMBUS_READ_DATA:
126 BADF("Unexpected stop during receive\n");
127 break;
128 default:
129 /* Nothing to do. */
130 break;
131 }
132 dev->mode = SMBUS_IDLE;
133 dev->data_len = 0;
134 break;
135
136 case I2C_NACK:
137 switch (dev->mode) {
138 case SMBUS_DONE:
139 /* Nothing to do. */
140 break;
141 case SMBUS_READ_DATA:
142 dev->mode = SMBUS_DONE;
143 break;
144 default:
145 BADF("Unexpected NACK in state %d\n", dev->mode);
146 dev->mode = SMBUS_CONFUSED;
147 break;
148 }
149 }
150}
151
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600152static int smbus_i2c_recv(I2CSlave *s)
pbrook0ff596d2007-05-23 00:03:59 +0000153{
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600154 SMBusDevice *dev = SMBUS_DEVICE(s);
155 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
pbrook0ff596d2007-05-23 00:03:59 +0000156 int ret;
157
158 switch (dev->mode) {
159 case SMBUS_RECV_BYTE:
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600160 if (sc->receive_byte) {
161 ret = sc->receive_byte(dev);
pbrook0ff596d2007-05-23 00:03:59 +0000162 } else {
163 ret = 0;
164 }
165 DPRINTF("Receive Byte %02x\n", ret);
166 dev->mode = SMBUS_DONE;
167 break;
168 case SMBUS_READ_DATA:
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600169 if (sc->read_data) {
170 ret = sc->read_data(dev, dev->command, dev->data_len);
pbrook0ff596d2007-05-23 00:03:59 +0000171 dev->data_len++;
172 } else {
173 ret = 0;
174 }
175 DPRINTF("Read data %02x\n", ret);
176 break;
177 default:
178 BADF("Unexpected read in state %d\n", dev->mode);
179 dev->mode = SMBUS_CONFUSED;
180 ret = 0;
181 break;
182 }
183 return ret;
184}
185
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600186static int smbus_i2c_send(I2CSlave *s, uint8_t data)
pbrook0ff596d2007-05-23 00:03:59 +0000187{
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600188 SMBusDevice *dev = SMBUS_DEVICE(s);
Paul Brook1ea96672009-05-14 22:35:08 +0100189
pbrook0ff596d2007-05-23 00:03:59 +0000190 switch (dev->mode) {
191 case SMBUS_WRITE_DATA:
192 DPRINTF("Write data %02x\n", data);
193 dev->data_buf[dev->data_len++] = data;
194 break;
195 default:
196 BADF("Unexpected write in state %d\n", dev->mode);
197 break;
198 }
199 return 0;
200}
201
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600202static int smbus_device_init(I2CSlave *i2c)
pbrook0ff596d2007-05-23 00:03:59 +0000203{
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600204 SMBusDevice *dev = SMBUS_DEVICE(i2c);
205 SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
pbrook0ff596d2007-05-23 00:03:59 +0000206
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600207 return sc->init(dev);
Paul Brook1ea96672009-05-14 22:35:08 +0100208}
balrog3f582262007-05-23 21:47:51 +0000209
pbrook0ff596d2007-05-23 00:03:59 +0000210/* Master device commands. */
Paolo Bonzini046a1842014-03-31 18:26:30 +0200211int smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
pbrook0ff596d2007-05-23 00:03:59 +0000212{
Paolo Bonzini046a1842014-03-31 18:26:30 +0200213 if (i2c_start_transfer(bus, addr, read)) {
214 return -1;
215 }
pbrook0ff596d2007-05-23 00:03:59 +0000216 i2c_end_transfer(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200217 return 0;
pbrook0ff596d2007-05-23 00:03:59 +0000218}
219
Paolo Bonzini285364e2014-03-31 18:26:29 +0200220int smbus_receive_byte(I2CBus *bus, uint8_t addr)
pbrook0ff596d2007-05-23 00:03:59 +0000221{
222 uint8_t data;
223
Paolo Bonzini046a1842014-03-31 18:26:30 +0200224 if (i2c_start_transfer(bus, addr, 1)) {
225 return -1;
226 }
pbrook0ff596d2007-05-23 00:03:59 +0000227 data = i2c_recv(bus);
228 i2c_nack(bus);
229 i2c_end_transfer(bus);
230 return data;
231}
232
Paolo Bonzini046a1842014-03-31 18:26:30 +0200233int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
pbrook0ff596d2007-05-23 00:03:59 +0000234{
Paolo Bonzini046a1842014-03-31 18:26:30 +0200235 if (i2c_start_transfer(bus, addr, 0)) {
236 return -1;
237 }
pbrook0ff596d2007-05-23 00:03:59 +0000238 i2c_send(bus, data);
239 i2c_end_transfer(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200240 return 0;
pbrook0ff596d2007-05-23 00:03:59 +0000241}
242
Paolo Bonzini285364e2014-03-31 18:26:29 +0200243int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
pbrook0ff596d2007-05-23 00:03:59 +0000244{
245 uint8_t data;
Paolo Bonzini046a1842014-03-31 18:26:30 +0200246 if (i2c_start_transfer(bus, addr, 0)) {
247 return -1;
248 }
pbrook0ff596d2007-05-23 00:03:59 +0000249 i2c_send(bus, command);
250 i2c_start_transfer(bus, addr, 1);
251 data = i2c_recv(bus);
252 i2c_nack(bus);
253 i2c_end_transfer(bus);
254 return data;
255}
256
Paolo Bonzini046a1842014-03-31 18:26:30 +0200257int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
pbrook0ff596d2007-05-23 00:03:59 +0000258{
Paolo Bonzini046a1842014-03-31 18:26:30 +0200259 if (i2c_start_transfer(bus, addr, 0)) {
260 return -1;
261 }
pbrook0ff596d2007-05-23 00:03:59 +0000262 i2c_send(bus, command);
263 i2c_send(bus, data);
264 i2c_end_transfer(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200265 return 0;
pbrook0ff596d2007-05-23 00:03:59 +0000266}
267
Paolo Bonzini285364e2014-03-31 18:26:29 +0200268int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
pbrook0ff596d2007-05-23 00:03:59 +0000269{
270 uint16_t data;
Paolo Bonzini046a1842014-03-31 18:26:30 +0200271 if (i2c_start_transfer(bus, addr, 0)) {
272 return -1;
273 }
pbrook0ff596d2007-05-23 00:03:59 +0000274 i2c_send(bus, command);
275 i2c_start_transfer(bus, addr, 1);
276 data = i2c_recv(bus);
277 data |= i2c_recv(bus) << 8;
278 i2c_nack(bus);
279 i2c_end_transfer(bus);
280 return data;
281}
282
Paolo Bonzini046a1842014-03-31 18:26:30 +0200283int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
pbrook0ff596d2007-05-23 00:03:59 +0000284{
Paolo Bonzini046a1842014-03-31 18:26:30 +0200285 if (i2c_start_transfer(bus, addr, 0)) {
286 return -1;
287 }
pbrook0ff596d2007-05-23 00:03:59 +0000288 i2c_send(bus, command);
289 i2c_send(bus, data & 0xff);
290 i2c_send(bus, data >> 8);
291 i2c_end_transfer(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200292 return 0;
pbrook0ff596d2007-05-23 00:03:59 +0000293}
294
Andreas Färbera5c82852013-08-03 00:18:51 +0200295int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
pbrook0ff596d2007-05-23 00:03:59 +0000296{
297 int len;
298 int i;
299
Paolo Bonzini046a1842014-03-31 18:26:30 +0200300 if (i2c_start_transfer(bus, addr, 0)) {
301 return -1;
302 }
pbrook0ff596d2007-05-23 00:03:59 +0000303 i2c_send(bus, command);
304 i2c_start_transfer(bus, addr, 1);
305 len = i2c_recv(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200306 if (len > 32) {
pbrook0ff596d2007-05-23 00:03:59 +0000307 len = 0;
Paolo Bonzini046a1842014-03-31 18:26:30 +0200308 }
309 for (i = 0; i < len; i++) {
pbrook0ff596d2007-05-23 00:03:59 +0000310 data[i] = i2c_recv(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200311 }
pbrook0ff596d2007-05-23 00:03:59 +0000312 i2c_nack(bus);
313 i2c_end_transfer(bus);
314 return len;
315}
316
Paolo Bonzini046a1842014-03-31 18:26:30 +0200317int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
318 int len)
pbrook0ff596d2007-05-23 00:03:59 +0000319{
320 int i;
321
322 if (len > 32)
323 len = 32;
324
Paolo Bonzini046a1842014-03-31 18:26:30 +0200325 if (i2c_start_transfer(bus, addr, 0)) {
326 return -1;
327 }
pbrook0ff596d2007-05-23 00:03:59 +0000328 i2c_send(bus, command);
329 i2c_send(bus, len);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200330 for (i = 0; i < len; i++) {
pbrook0ff596d2007-05-23 00:03:59 +0000331 i2c_send(bus, data[i]);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200332 }
pbrook0ff596d2007-05-23 00:03:59 +0000333 i2c_end_transfer(bus);
Paolo Bonzini046a1842014-03-31 18:26:30 +0200334 return 0;
pbrook0ff596d2007-05-23 00:03:59 +0000335}
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600336
337static void smbus_device_class_init(ObjectClass *klass, void *data)
338{
339 I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
340
341 sc->init = smbus_device_init;
342 sc->event = smbus_i2c_event;
343 sc->recv = smbus_i2c_recv;
344 sc->send = smbus_i2c_send;
345}
346
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100347static const TypeInfo smbus_device_type_info = {
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600348 .name = TYPE_SMBUS_DEVICE,
349 .parent = TYPE_I2C_SLAVE,
350 .instance_size = sizeof(SMBusDevice),
351 .abstract = true,
352 .class_size = sizeof(SMBusDeviceClass),
353 .class_init = smbus_device_class_init,
354};
355
Andreas Färber83f7d432012-02-09 15:20:55 +0100356static void smbus_device_register_types(void)
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600357{
358 type_register_static(&smbus_device_type_info);
359}
360
Andreas Färber83f7d432012-02-09 15:20:55 +0100361type_init(smbus_device_register_types)