root / hw / i2c.c @ 8d0eb050
History | View | Annotate | Download (3.9 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 | 0ff596d0 | pbrook | * This code is licenced 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 | 0ff596d0 | pbrook | i2c_slave *current_dev; |
16 | 0ff596d0 | pbrook | i2c_slave *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 | 5b7f5327 | Juan Quintela | DEFINE_PROP_UINT8("address", struct i2c_slave, 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 | 8d0eb050 | Juan Quintela | vmstate_register(-1, &vmstate_i2c_bus, bus);
|
66 | 0ff596d0 | pbrook | return bus;
|
67 | 0ff596d0 | pbrook | } |
68 | 0ff596d0 | pbrook | |
69 | 5b7f5327 | Juan Quintela | void i2c_set_slave_address(i2c_slave *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 | 02e2da45 | Paul Brook | i2c_slave *slave = NULL;
|
86 | 0ff596d0 | pbrook | |
87 | 72cf2d4f | Blue Swirl | QLIST_FOREACH(qdev, &bus->qbus.children, sibling) { |
88 | 02e2da45 | Paul Brook | slave = I2C_SLAVE_FROM_QDEV(qdev); |
89 | 02e2da45 | Paul Brook | if (slave->address == address)
|
90 | 0ff596d0 | pbrook | break;
|
91 | 0ff596d0 | pbrook | } |
92 | 0ff596d0 | pbrook | |
93 | 02e2da45 | Paul Brook | if (!slave)
|
94 | 0ff596d0 | pbrook | return 1; |
95 | 0ff596d0 | pbrook | |
96 | 0ff596d0 | pbrook | /* If the bus is already busy, assume this is a repeated
|
97 | 0ff596d0 | pbrook | start condition. */
|
98 | 02e2da45 | Paul Brook | bus->current_dev = slave; |
99 | 02e2da45 | Paul Brook | slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); |
100 | 0ff596d0 | pbrook | return 0; |
101 | 0ff596d0 | pbrook | } |
102 | 0ff596d0 | pbrook | |
103 | 0ff596d0 | pbrook | void i2c_end_transfer(i2c_bus *bus)
|
104 | 0ff596d0 | pbrook | { |
105 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
106 | 0ff596d0 | pbrook | |
107 | 0ff596d0 | pbrook | if (!dev)
|
108 | 0ff596d0 | pbrook | return;
|
109 | 0ff596d0 | pbrook | |
110 | fe8de492 | Paul Brook | dev->info->event(dev, I2C_FINISH); |
111 | 0ff596d0 | pbrook | |
112 | 0ff596d0 | pbrook | bus->current_dev = NULL;
|
113 | 0ff596d0 | pbrook | } |
114 | 0ff596d0 | pbrook | |
115 | 0ff596d0 | pbrook | int i2c_send(i2c_bus *bus, uint8_t data)
|
116 | 0ff596d0 | pbrook | { |
117 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
118 | 0ff596d0 | pbrook | |
119 | 0ff596d0 | pbrook | if (!dev)
|
120 | 0ff596d0 | pbrook | return -1; |
121 | 0ff596d0 | pbrook | |
122 | fe8de492 | Paul Brook | return dev->info->send(dev, data);
|
123 | 0ff596d0 | pbrook | } |
124 | 0ff596d0 | pbrook | |
125 | 0ff596d0 | pbrook | int i2c_recv(i2c_bus *bus)
|
126 | 0ff596d0 | pbrook | { |
127 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
128 | 0ff596d0 | pbrook | |
129 | 0ff596d0 | pbrook | if (!dev)
|
130 | 0ff596d0 | pbrook | return -1; |
131 | 0ff596d0 | pbrook | |
132 | fe8de492 | Paul Brook | return dev->info->recv(dev);
|
133 | 0ff596d0 | pbrook | } |
134 | 0ff596d0 | pbrook | |
135 | 0ff596d0 | pbrook | void i2c_nack(i2c_bus *bus)
|
136 | 0ff596d0 | pbrook | { |
137 | 0ff596d0 | pbrook | i2c_slave *dev = bus->current_dev; |
138 | 0ff596d0 | pbrook | |
139 | 0ff596d0 | pbrook | if (!dev)
|
140 | 0ff596d0 | pbrook | return;
|
141 | 0ff596d0 | pbrook | |
142 | fe8de492 | Paul Brook | dev->info->event(dev, I2C_NACK); |
143 | 0ff596d0 | pbrook | } |
144 | 0ff596d0 | pbrook | |
145 | aa941b94 | balrog | void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
|
146 | aa941b94 | balrog | { |
147 | 5b7f5327 | Juan Quintela | qemu_put_8s(f, &dev->address); |
148 | aa941b94 | balrog | } |
149 | aa941b94 | balrog | |
150 | aa941b94 | balrog | void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
|
151 | aa941b94 | balrog | { |
152 | fe8de492 | Paul Brook | i2c_bus *bus; |
153 | 02e2da45 | Paul Brook | bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
154 | 5b7f5327 | Juan Quintela | qemu_get_8s(f, &dev->address); |
155 | fe8de492 | Paul Brook | if (bus->saved_address == dev->address) {
|
156 | fe8de492 | Paul Brook | bus->current_dev = dev; |
157 | fe8de492 | Paul Brook | } |
158 | fe8de492 | Paul Brook | } |
159 | fe8de492 | Paul Brook | |
160 | 81a322d4 | Gerd Hoffmann | static int i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base) |
161 | fe8de492 | Paul Brook | { |
162 | 02e2da45 | Paul Brook | I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev); |
163 | fe8de492 | Paul Brook | i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); |
164 | fe8de492 | Paul Brook | |
165 | fe8de492 | Paul Brook | s->info = info; |
166 | fe8de492 | Paul Brook | |
167 | 81a322d4 | Gerd Hoffmann | return info->init(s);
|
168 | fe8de492 | Paul Brook | } |
169 | fe8de492 | Paul Brook | |
170 | 074f2fff | Gerd Hoffmann | void i2c_register_slave(I2CSlaveInfo *info)
|
171 | fe8de492 | Paul Brook | { |
172 | 074f2fff | Gerd Hoffmann | assert(info->qdev.size >= sizeof(i2c_slave));
|
173 | 02e2da45 | Paul Brook | info->qdev.init = i2c_slave_qdev_init; |
174 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &i2c_bus_info; |
175 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
176 | fe8de492 | Paul Brook | } |
177 | fe8de492 | Paul Brook | |
178 | 5b7f5327 | Juan Quintela | DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr) |
179 | fe8de492 | Paul Brook | { |
180 | fe8de492 | Paul Brook | DeviceState *dev; |
181 | fe8de492 | Paul Brook | |
182 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
183 | 5b7f5327 | Juan Quintela | qdev_prop_set_uint8(dev, "address", addr);
|
184 | fe8de492 | Paul Brook | qdev_init(dev); |
185 | fe8de492 | Paul Brook | return dev;
|
186 | aa941b94 | balrog | } |