root / hw / i2c.c @ 1062977b
History | View | Annotate | Download (4.2 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 |
uint8_t saved_address; |
18 |
}; |
19 |
|
20 |
static struct BusInfo i2c_bus_info = { |
21 |
.name = "I2C",
|
22 |
.size = sizeof(i2c_bus),
|
23 |
.props = (Property[]) { |
24 |
DEFINE_PROP_UINT8("address", struct i2c_slave, address, 0), |
25 |
DEFINE_PROP_END_OF_LIST(), |
26 |
} |
27 |
}; |
28 |
|
29 |
static void i2c_bus_pre_save(void *opaque) |
30 |
{ |
31 |
i2c_bus *bus = opaque; |
32 |
|
33 |
bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
|
34 |
} |
35 |
|
36 |
static int i2c_bus_post_load(void *opaque, int version_id) |
37 |
{ |
38 |
i2c_bus *bus = opaque; |
39 |
|
40 |
/* The bus is loaded before attached devices, so load and save the
|
41 |
current device id. Devices will check themselves as loaded. */
|
42 |
bus->current_dev = NULL;
|
43 |
return 0; |
44 |
} |
45 |
|
46 |
static const VMStateDescription vmstate_i2c_bus = { |
47 |
.name = "i2c_bus",
|
48 |
.version_id = 1,
|
49 |
.minimum_version_id = 1,
|
50 |
.minimum_version_id_old = 1,
|
51 |
.pre_save = i2c_bus_pre_save, |
52 |
.post_load = i2c_bus_post_load, |
53 |
.fields = (VMStateField []) { |
54 |
VMSTATE_UINT8(saved_address, i2c_bus), |
55 |
VMSTATE_END_OF_LIST() |
56 |
} |
57 |
}; |
58 |
|
59 |
/* Create a new I2C bus. */
|
60 |
i2c_bus *i2c_init_bus(DeviceState *parent, const char *name) |
61 |
{ |
62 |
i2c_bus *bus; |
63 |
|
64 |
bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name)); |
65 |
vmstate_register(-1, &vmstate_i2c_bus, bus);
|
66 |
return bus;
|
67 |
} |
68 |
|
69 |
void i2c_set_slave_address(i2c_slave *dev, uint8_t address)
|
70 |
{ |
71 |
dev->address = address; |
72 |
} |
73 |
|
74 |
/* Return nonzero if bus is busy. */
|
75 |
int i2c_bus_busy(i2c_bus *bus)
|
76 |
{ |
77 |
return bus->current_dev != NULL; |
78 |
} |
79 |
|
80 |
/* Returns non-zero if the address is not valid. */
|
81 |
/* TODO: Make this handle multiple masters. */
|
82 |
int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv) |
83 |
{ |
84 |
DeviceState *qdev; |
85 |
i2c_slave *slave = NULL;
|
86 |
|
87 |
QLIST_FOREACH(qdev, &bus->qbus.children, sibling) { |
88 |
slave = I2C_SLAVE_FROM_QDEV(qdev); |
89 |
if (slave->address == address)
|
90 |
break;
|
91 |
} |
92 |
|
93 |
if (!slave)
|
94 |
return 1; |
95 |
|
96 |
/* If the bus is already busy, assume this is a repeated
|
97 |
start condition. */
|
98 |
bus->current_dev = slave; |
99 |
slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); |
100 |
return 0; |
101 |
} |
102 |
|
103 |
void i2c_end_transfer(i2c_bus *bus)
|
104 |
{ |
105 |
i2c_slave *dev = bus->current_dev; |
106 |
|
107 |
if (!dev)
|
108 |
return;
|
109 |
|
110 |
dev->info->event(dev, I2C_FINISH); |
111 |
|
112 |
bus->current_dev = NULL;
|
113 |
} |
114 |
|
115 |
int i2c_send(i2c_bus *bus, uint8_t data)
|
116 |
{ |
117 |
i2c_slave *dev = bus->current_dev; |
118 |
|
119 |
if (!dev)
|
120 |
return -1; |
121 |
|
122 |
return dev->info->send(dev, data);
|
123 |
} |
124 |
|
125 |
int i2c_recv(i2c_bus *bus)
|
126 |
{ |
127 |
i2c_slave *dev = bus->current_dev; |
128 |
|
129 |
if (!dev)
|
130 |
return -1; |
131 |
|
132 |
return dev->info->recv(dev);
|
133 |
} |
134 |
|
135 |
void i2c_nack(i2c_bus *bus)
|
136 |
{ |
137 |
i2c_slave *dev = bus->current_dev; |
138 |
|
139 |
if (!dev)
|
140 |
return;
|
141 |
|
142 |
dev->info->event(dev, I2C_NACK); |
143 |
} |
144 |
|
145 |
static int i2c_slave_post_load(void *opaque, int version_id) |
146 |
{ |
147 |
i2c_slave *dev = opaque; |
148 |
i2c_bus *bus; |
149 |
bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
150 |
if (bus->saved_address == dev->address) {
|
151 |
bus->current_dev = dev; |
152 |
} |
153 |
return 0; |
154 |
} |
155 |
|
156 |
const VMStateDescription vmstate_i2c_slave = {
|
157 |
.name = "i2c_slave",
|
158 |
.version_id = 1,
|
159 |
.minimum_version_id = 1,
|
160 |
.minimum_version_id_old = 1,
|
161 |
.post_load = i2c_slave_post_load, |
162 |
.fields = (VMStateField []) { |
163 |
VMSTATE_UINT8(address, i2c_slave), |
164 |
VMSTATE_END_OF_LIST() |
165 |
} |
166 |
}; |
167 |
|
168 |
static int i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base) |
169 |
{ |
170 |
I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev); |
171 |
i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); |
172 |
|
173 |
s->info = info; |
174 |
|
175 |
return info->init(s);
|
176 |
} |
177 |
|
178 |
void i2c_register_slave(I2CSlaveInfo *info)
|
179 |
{ |
180 |
assert(info->qdev.size >= sizeof(i2c_slave));
|
181 |
info->qdev.init = i2c_slave_qdev_init; |
182 |
info->qdev.bus_info = &i2c_bus_info; |
183 |
qdev_register(&info->qdev); |
184 |
} |
185 |
|
186 |
DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr) |
187 |
{ |
188 |
DeviceState *dev; |
189 |
|
190 |
dev = qdev_create(&bus->qbus, name); |
191 |
qdev_prop_set_uint8(dev, "address", addr);
|
192 |
qdev_init_nofail(dev); |
193 |
return dev;
|
194 |
} |