root / hw / i2c.c @ 9077f01b
History | View | Annotate | Download (5 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 0ff596d0 | pbrook | * QEMU I2C bus interface.
|
3 | 0ff596d0 | pbrook | *
|
4 | 0ff596d0 | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | 0ff596d0 | pbrook | * Written by Paul Brook
|
6 | 0ff596d0 | pbrook | *
|
7 | 8e31bf38 | Matthew Fernandez | * This code is licensed under the LGPL.
|
8 | 0ff596d0 | pbrook | */
|
9 | 0ff596d0 | pbrook | |
10 | 87ecb68b | pbrook | #include "i2c.h" |
11 | 0ff596d0 | pbrook | |
12 | 0ff596d0 | pbrook | struct i2c_bus
|
13 | 0ff596d0 | pbrook | { |
14 | 02e2da45 | Paul Brook | BusState qbus; |
15 | 9e07bdf8 | Anthony Liguori | I2CSlave *current_dev; |
16 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev; |
17 | 5b7f5327 | Juan Quintela | uint8_t saved_address; |
18 | 0ff596d0 | pbrook | }; |
19 | 0ff596d0 | pbrook | |
20 | 10c4c98a | Gerd Hoffmann | static struct BusInfo i2c_bus_info = { |
21 | 10c4c98a | Gerd Hoffmann | .name = "I2C",
|
22 | 10c4c98a | Gerd Hoffmann | .size = sizeof(i2c_bus),
|
23 | ee6847d1 | Gerd Hoffmann | .props = (Property[]) { |
24 | 9e07bdf8 | Anthony Liguori | DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0), |
25 | 368eb5d4 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
26 | ee6847d1 | Gerd Hoffmann | } |
27 | 10c4c98a | Gerd Hoffmann | }; |
28 | 10c4c98a | Gerd Hoffmann | |
29 | 8d0eb050 | Juan Quintela | static void i2c_bus_pre_save(void *opaque) |
30 | c701b35b | pbrook | { |
31 | 8d0eb050 | Juan Quintela | i2c_bus *bus = opaque; |
32 | c701b35b | pbrook | |
33 | 8d0eb050 | Juan Quintela | bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
|
34 | c701b35b | pbrook | } |
35 | c701b35b | pbrook | |
36 | 8d0eb050 | Juan Quintela | static int i2c_bus_post_load(void *opaque, int version_id) |
37 | c701b35b | pbrook | { |
38 | 8d0eb050 | Juan Quintela | i2c_bus *bus = opaque; |
39 | c701b35b | pbrook | |
40 | c701b35b | pbrook | /* The bus is loaded before attached devices, so load and save the
|
41 | c701b35b | pbrook | current device id. Devices will check themselves as loaded. */
|
42 | c701b35b | pbrook | bus->current_dev = NULL;
|
43 | c701b35b | pbrook | return 0; |
44 | c701b35b | pbrook | } |
45 | c701b35b | pbrook | |
46 | 8d0eb050 | Juan Quintela | static const VMStateDescription vmstate_i2c_bus = { |
47 | 8d0eb050 | Juan Quintela | .name = "i2c_bus",
|
48 | 8d0eb050 | Juan Quintela | .version_id = 1,
|
49 | 8d0eb050 | Juan Quintela | .minimum_version_id = 1,
|
50 | 8d0eb050 | Juan Quintela | .minimum_version_id_old = 1,
|
51 | 8d0eb050 | Juan Quintela | .pre_save = i2c_bus_pre_save, |
52 | 8d0eb050 | Juan Quintela | .post_load = i2c_bus_post_load, |
53 | 8d0eb050 | Juan Quintela | .fields = (VMStateField []) { |
54 | 8d0eb050 | Juan Quintela | VMSTATE_UINT8(saved_address, i2c_bus), |
55 | 8d0eb050 | Juan Quintela | VMSTATE_END_OF_LIST() |
56 | 8d0eb050 | Juan Quintela | } |
57 | 8d0eb050 | Juan Quintela | }; |
58 | 8d0eb050 | Juan Quintela | |
59 | 0ff596d0 | pbrook | /* Create a new I2C bus. */
|
60 | 02e2da45 | Paul Brook | i2c_bus *i2c_init_bus(DeviceState *parent, const char *name) |
61 | 0ff596d0 | pbrook | { |
62 | 0ff596d0 | pbrook | i2c_bus *bus; |
63 | 0ff596d0 | pbrook | |
64 | 10c4c98a | Gerd Hoffmann | bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name)); |
65 | 0be71e32 | Alex Williamson | vmstate_register(NULL, -1, &vmstate_i2c_bus, bus); |
66 | 0ff596d0 | pbrook | return bus;
|
67 | 0ff596d0 | pbrook | } |
68 | 0ff596d0 | pbrook | |
69 | 9e07bdf8 | Anthony Liguori | void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
|
70 | 0ff596d0 | pbrook | { |
71 | 0ff596d0 | pbrook | dev->address = address; |
72 | 0ff596d0 | pbrook | } |
73 | 0ff596d0 | pbrook | |
74 | 0ff596d0 | pbrook | /* Return nonzero if bus is busy. */
|
75 | 0ff596d0 | pbrook | int i2c_bus_busy(i2c_bus *bus)
|
76 | 0ff596d0 | pbrook | { |
77 | 0ff596d0 | pbrook | return bus->current_dev != NULL; |
78 | 0ff596d0 | pbrook | } |
79 | 0ff596d0 | pbrook | |
80 | 4a2c8ac2 | balrog | /* Returns non-zero if the address is not valid. */
|
81 | 0ff596d0 | pbrook | /* TODO: Make this handle multiple masters. */
|
82 | 5b7f5327 | Juan Quintela | int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv) |
83 | 0ff596d0 | pbrook | { |
84 | 02e2da45 | Paul Brook | DeviceState *qdev; |
85 | 9e07bdf8 | Anthony Liguori | I2CSlave *slave = NULL;
|
86 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc; |
87 | 0ff596d0 | pbrook | |
88 | d8bb00d6 | Paolo Bonzini | QTAILQ_FOREACH(qdev, &bus->qbus.children, sibling) { |
89 | 9e07bdf8 | Anthony Liguori | I2CSlave *candidate = I2C_SLAVE_FROM_QDEV(qdev); |
90 | b3a21988 | Juha Riihimäki | if (candidate->address == address) {
|
91 | b3a21988 | Juha Riihimäki | slave = candidate; |
92 | 0ff596d0 | pbrook | break;
|
93 | b3a21988 | Juha Riihimäki | } |
94 | 0ff596d0 | pbrook | } |
95 | 0ff596d0 | pbrook | |
96 | b5ea9327 | Anthony Liguori | if (!slave) {
|
97 | 0ff596d0 | pbrook | return 1; |
98 | b5ea9327 | Anthony Liguori | } |
99 | 0ff596d0 | pbrook | |
100 | b5ea9327 | Anthony Liguori | sc = I2C_SLAVE_GET_CLASS(slave); |
101 | 0ff596d0 | pbrook | /* If the bus is already busy, assume this is a repeated
|
102 | 0ff596d0 | pbrook | start condition. */
|
103 | 02e2da45 | Paul Brook | bus->current_dev = slave; |
104 | b5ea9327 | Anthony Liguori | if (sc->event) {
|
105 | b5ea9327 | Anthony Liguori | sc->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); |
106 | b5ea9327 | Anthony Liguori | } |
107 | 0ff596d0 | pbrook | return 0; |
108 | 0ff596d0 | pbrook | } |
109 | 0ff596d0 | pbrook | |
110 | 0ff596d0 | pbrook | void i2c_end_transfer(i2c_bus *bus)
|
111 | 0ff596d0 | pbrook | { |
112 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev = bus->current_dev; |
113 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc; |
114 | 0ff596d0 | pbrook | |
115 | b5ea9327 | Anthony Liguori | if (!dev) {
|
116 | 0ff596d0 | pbrook | return;
|
117 | b5ea9327 | Anthony Liguori | } |
118 | 0ff596d0 | pbrook | |
119 | b5ea9327 | Anthony Liguori | sc = I2C_SLAVE_GET_CLASS(dev); |
120 | b5ea9327 | Anthony Liguori | if (sc->event) {
|
121 | b5ea9327 | Anthony Liguori | sc->event(dev, I2C_FINISH); |
122 | b5ea9327 | Anthony Liguori | } |
123 | 0ff596d0 | pbrook | |
124 | 0ff596d0 | pbrook | bus->current_dev = NULL;
|
125 | 0ff596d0 | pbrook | } |
126 | 0ff596d0 | pbrook | |
127 | 0ff596d0 | pbrook | int i2c_send(i2c_bus *bus, uint8_t data)
|
128 | 0ff596d0 | pbrook | { |
129 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev = bus->current_dev; |
130 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc; |
131 | 0ff596d0 | pbrook | |
132 | b5ea9327 | Anthony Liguori | if (!dev) {
|
133 | 0ff596d0 | pbrook | return -1; |
134 | b5ea9327 | Anthony Liguori | } |
135 | 0ff596d0 | pbrook | |
136 | b5ea9327 | Anthony Liguori | sc = I2C_SLAVE_GET_CLASS(dev); |
137 | b5ea9327 | Anthony Liguori | if (sc->send) {
|
138 | b5ea9327 | Anthony Liguori | return sc->send(dev, data);
|
139 | b5ea9327 | Anthony Liguori | } |
140 | b5ea9327 | Anthony Liguori | |
141 | b5ea9327 | Anthony Liguori | return -1; |
142 | 0ff596d0 | pbrook | } |
143 | 0ff596d0 | pbrook | |
144 | 0ff596d0 | pbrook | int i2c_recv(i2c_bus *bus)
|
145 | 0ff596d0 | pbrook | { |
146 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev = bus->current_dev; |
147 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc; |
148 | 0ff596d0 | pbrook | |
149 | b5ea9327 | Anthony Liguori | if (!dev) {
|
150 | 0ff596d0 | pbrook | return -1; |
151 | b5ea9327 | Anthony Liguori | } |
152 | b5ea9327 | Anthony Liguori | |
153 | b5ea9327 | Anthony Liguori | sc = I2C_SLAVE_GET_CLASS(dev); |
154 | b5ea9327 | Anthony Liguori | if (sc->recv) {
|
155 | b5ea9327 | Anthony Liguori | return sc->recv(dev);
|
156 | b5ea9327 | Anthony Liguori | } |
157 | 0ff596d0 | pbrook | |
158 | b5ea9327 | Anthony Liguori | return -1; |
159 | 0ff596d0 | pbrook | } |
160 | 0ff596d0 | pbrook | |
161 | 0ff596d0 | pbrook | void i2c_nack(i2c_bus *bus)
|
162 | 0ff596d0 | pbrook | { |
163 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev = bus->current_dev; |
164 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc; |
165 | 0ff596d0 | pbrook | |
166 | b5ea9327 | Anthony Liguori | if (!dev) {
|
167 | 0ff596d0 | pbrook | return;
|
168 | b5ea9327 | Anthony Liguori | } |
169 | 0ff596d0 | pbrook | |
170 | b5ea9327 | Anthony Liguori | sc = I2C_SLAVE_GET_CLASS(dev); |
171 | b5ea9327 | Anthony Liguori | if (sc->event) {
|
172 | b5ea9327 | Anthony Liguori | sc->event(dev, I2C_NACK); |
173 | b5ea9327 | Anthony Liguori | } |
174 | 0ff596d0 | pbrook | } |
175 | 0ff596d0 | pbrook | |
176 | bcbe8068 | Juan Quintela | static int i2c_slave_post_load(void *opaque, int version_id) |
177 | aa941b94 | balrog | { |
178 | 9e07bdf8 | Anthony Liguori | I2CSlave *dev = opaque; |
179 | fe8de492 | Paul Brook | i2c_bus *bus; |
180 | 02e2da45 | Paul Brook | bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
181 | fe8de492 | Paul Brook | if (bus->saved_address == dev->address) {
|
182 | fe8de492 | Paul Brook | bus->current_dev = dev; |
183 | fe8de492 | Paul Brook | } |
184 | bcbe8068 | Juan Quintela | return 0; |
185 | bcbe8068 | Juan Quintela | } |
186 | bcbe8068 | Juan Quintela | |
187 | 1894839f | Juan Quintela | const VMStateDescription vmstate_i2c_slave = {
|
188 | 9e07bdf8 | Anthony Liguori | .name = "I2CSlave",
|
189 | bcbe8068 | Juan Quintela | .version_id = 1,
|
190 | bcbe8068 | Juan Quintela | .minimum_version_id = 1,
|
191 | bcbe8068 | Juan Quintela | .minimum_version_id_old = 1,
|
192 | bcbe8068 | Juan Quintela | .post_load = i2c_slave_post_load, |
193 | bcbe8068 | Juan Quintela | .fields = (VMStateField []) { |
194 | 9e07bdf8 | Anthony Liguori | VMSTATE_UINT8(address, I2CSlave), |
195 | bcbe8068 | Juan Quintela | VMSTATE_END_OF_LIST() |
196 | bcbe8068 | Juan Quintela | } |
197 | bcbe8068 | Juan Quintela | }; |
198 | bcbe8068 | Juan Quintela | |
199 | d307af79 | Anthony Liguori | static int i2c_slave_qdev_init(DeviceState *dev) |
200 | fe8de492 | Paul Brook | { |
201 | 9e07bdf8 | Anthony Liguori | I2CSlave *s = I2C_SLAVE_FROM_QDEV(dev); |
202 | b5ea9327 | Anthony Liguori | I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s); |
203 | fe8de492 | Paul Brook | |
204 | b5ea9327 | Anthony Liguori | return sc->init(s);
|
205 | b5ea9327 | Anthony Liguori | } |
206 | fe8de492 | Paul Brook | |
207 | 5b7f5327 | Juan Quintela | DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr) |
208 | fe8de492 | Paul Brook | { |
209 | fe8de492 | Paul Brook | DeviceState *dev; |
210 | fe8de492 | Paul Brook | |
211 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
212 | 5b7f5327 | Juan Quintela | qdev_prop_set_uint8(dev, "address", addr);
|
213 | e23a1b33 | Markus Armbruster | qdev_init_nofail(dev); |
214 | fe8de492 | Paul Brook | return dev;
|
215 | aa941b94 | balrog | } |
216 | b5ea9327 | Anthony Liguori | |
217 | 39bffca2 | Anthony Liguori | static void i2c_slave_class_init(ObjectClass *klass, void *data) |
218 | 39bffca2 | Anthony Liguori | { |
219 | 39bffca2 | Anthony Liguori | DeviceClass *k = DEVICE_CLASS(klass); |
220 | 39bffca2 | Anthony Liguori | k->init = i2c_slave_qdev_init; |
221 | 39bffca2 | Anthony Liguori | k->bus_info = &i2c_bus_info; |
222 | 39bffca2 | Anthony Liguori | } |
223 | 39bffca2 | Anthony Liguori | |
224 | b5ea9327 | Anthony Liguori | static TypeInfo i2c_slave_type_info = {
|
225 | b5ea9327 | Anthony Liguori | .name = TYPE_I2C_SLAVE, |
226 | b5ea9327 | Anthony Liguori | .parent = TYPE_DEVICE, |
227 | b5ea9327 | Anthony Liguori | .instance_size = sizeof(I2CSlave),
|
228 | b5ea9327 | Anthony Liguori | .abstract = true,
|
229 | b5ea9327 | Anthony Liguori | .class_size = sizeof(I2CSlaveClass),
|
230 | 39bffca2 | Anthony Liguori | .class_init = i2c_slave_class_init, |
231 | b5ea9327 | Anthony Liguori | }; |
232 | b5ea9327 | Anthony Liguori | |
233 | 83f7d43a | Andreas Färber | static void i2c_slave_register_types(void) |
234 | b5ea9327 | Anthony Liguori | { |
235 | b5ea9327 | Anthony Liguori | type_register_static(&i2c_slave_type_info); |
236 | b5ea9327 | Anthony Liguori | } |
237 | b5ea9327 | Anthony Liguori | |
238 | 83f7d43a | Andreas Färber | type_init(i2c_slave_register_types) |