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