root / hw / i2c.c @ ee6847d1
History | View | Annotate | Download (3.8 kB)
1 |
/*
|
---|---|
2 |
* QEMU I2C bus interface.
|
3 |
*
|
4 |
* Copyright (c) 2007 CodeSourcery.
|
5 |
* Written by Paul Brook
|
6 |
*
|
7 |
* This code is licenced under the LGPL.
|
8 |
*/
|
9 |
|
10 |
#include "i2c.h" |
11 |
|
12 |
struct i2c_bus
|
13 |
{ |
14 |
BusState qbus; |
15 |
i2c_slave *current_dev; |
16 |
i2c_slave *dev; |
17 |
int saved_address;
|
18 |
}; |
19 |
|
20 |
static struct BusInfo i2c_bus_info = { |
21 |
.name = "I2C",
|
22 |
.size = sizeof(i2c_bus),
|
23 |
.props = (Property[]) { |
24 |
{ |
25 |
.name = "address",
|
26 |
.info = &qdev_prop_uint32, |
27 |
.offset = offsetof(struct i2c_slave, address),
|
28 |
}, |
29 |
{/* end of list */}
|
30 |
} |
31 |
}; |
32 |
|
33 |
static void i2c_bus_save(QEMUFile *f, void *opaque) |
34 |
{ |
35 |
i2c_bus *bus = (i2c_bus *)opaque; |
36 |
|
37 |
qemu_put_byte(f, bus->current_dev ? bus->current_dev->address : -1);
|
38 |
} |
39 |
|
40 |
static int i2c_bus_load(QEMUFile *f, void *opaque, int version_id) |
41 |
{ |
42 |
i2c_bus *bus = (i2c_bus *)opaque; |
43 |
|
44 |
if (version_id != 1) |
45 |
return -EINVAL;
|
46 |
|
47 |
/* The bus is loaded before attached devices, so load and save the
|
48 |
current device id. Devices will check themselves as loaded. */
|
49 |
bus->saved_address = (int8_t) qemu_get_byte(f); |
50 |
bus->current_dev = NULL;
|
51 |
|
52 |
return 0; |
53 |
} |
54 |
|
55 |
/* Create a new I2C bus. */
|
56 |
i2c_bus *i2c_init_bus(DeviceState *parent, const char *name) |
57 |
{ |
58 |
i2c_bus *bus; |
59 |
|
60 |
bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name)); |
61 |
register_savevm("i2c_bus", -1, 1, i2c_bus_save, i2c_bus_load, bus); |
62 |
return bus;
|
63 |
} |
64 |
|
65 |
void i2c_set_slave_address(i2c_slave *dev, int address) |
66 |
{ |
67 |
dev->address = address; |
68 |
} |
69 |
|
70 |
/* Return nonzero if bus is busy. */
|
71 |
int i2c_bus_busy(i2c_bus *bus)
|
72 |
{ |
73 |
return bus->current_dev != NULL; |
74 |
} |
75 |
|
76 |
/* Returns non-zero if the address is not valid. */
|
77 |
/* TODO: Make this handle multiple masters. */
|
78 |
int i2c_start_transfer(i2c_bus *bus, int address, int recv) |
79 |
{ |
80 |
DeviceState *qdev; |
81 |
i2c_slave *slave = NULL;
|
82 |
|
83 |
LIST_FOREACH(qdev, &bus->qbus.children, sibling) { |
84 |
slave = I2C_SLAVE_FROM_QDEV(qdev); |
85 |
if (slave->address == address)
|
86 |
break;
|
87 |
} |
88 |
|
89 |
if (!slave)
|
90 |
return 1; |
91 |
|
92 |
/* If the bus is already busy, assume this is a repeated
|
93 |
start condition. */
|
94 |
bus->current_dev = slave; |
95 |
slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); |
96 |
return 0; |
97 |
} |
98 |
|
99 |
void i2c_end_transfer(i2c_bus *bus)
|
100 |
{ |
101 |
i2c_slave *dev = bus->current_dev; |
102 |
|
103 |
if (!dev)
|
104 |
return;
|
105 |
|
106 |
dev->info->event(dev, I2C_FINISH); |
107 |
|
108 |
bus->current_dev = NULL;
|
109 |
} |
110 |
|
111 |
int i2c_send(i2c_bus *bus, uint8_t data)
|
112 |
{ |
113 |
i2c_slave *dev = bus->current_dev; |
114 |
|
115 |
if (!dev)
|
116 |
return -1; |
117 |
|
118 |
return dev->info->send(dev, data);
|
119 |
} |
120 |
|
121 |
int i2c_recv(i2c_bus *bus)
|
122 |
{ |
123 |
i2c_slave *dev = bus->current_dev; |
124 |
|
125 |
if (!dev)
|
126 |
return -1; |
127 |
|
128 |
return dev->info->recv(dev);
|
129 |
} |
130 |
|
131 |
void i2c_nack(i2c_bus *bus)
|
132 |
{ |
133 |
i2c_slave *dev = bus->current_dev; |
134 |
|
135 |
if (!dev)
|
136 |
return;
|
137 |
|
138 |
dev->info->event(dev, I2C_NACK); |
139 |
} |
140 |
|
141 |
void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
|
142 |
{ |
143 |
qemu_put_byte(f, dev->address); |
144 |
} |
145 |
|
146 |
void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
|
147 |
{ |
148 |
i2c_bus *bus; |
149 |
bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
150 |
dev->address = qemu_get_byte(f); |
151 |
if (bus->saved_address == dev->address) {
|
152 |
bus->current_dev = dev; |
153 |
} |
154 |
} |
155 |
|
156 |
static void i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base) |
157 |
{ |
158 |
I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev); |
159 |
i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); |
160 |
|
161 |
s->info = info; |
162 |
|
163 |
info->init(s); |
164 |
} |
165 |
|
166 |
void i2c_register_slave(I2CSlaveInfo *info)
|
167 |
{ |
168 |
assert(info->qdev.size >= sizeof(i2c_slave));
|
169 |
info->qdev.init = i2c_slave_qdev_init; |
170 |
info->qdev.bus_info = &i2c_bus_info; |
171 |
qdev_register(&info->qdev); |
172 |
} |
173 |
|
174 |
DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, int addr) |
175 |
{ |
176 |
DeviceState *dev; |
177 |
|
178 |
dev = qdev_create(&bus->qbus, name); |
179 |
qdev_prop_set_uint32(dev, "address", addr);
|
180 |
qdev_init(dev); |
181 |
return dev;
|
182 |
} |