]>
Commit | Line | Data |
---|---|---|
5fafdf24 | 1 | /* |
0ff596d0 PB |
2 | * QEMU I2C bus interface. |
3 | * | |
4 | * Copyright (c) 2007 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
8e31bf38 | 7 | * This code is licensed under the LGPL. |
0ff596d0 PB |
8 | */ |
9 | ||
87ecb68b | 10 | #include "i2c.h" |
0ff596d0 PB |
11 | |
12 | struct i2c_bus | |
13 | { | |
02e2da45 | 14 | BusState qbus; |
0ff596d0 PB |
15 | i2c_slave *current_dev; |
16 | i2c_slave *dev; | |
5b7f5327 | 17 | uint8_t saved_address; |
0ff596d0 PB |
18 | }; |
19 | ||
10c4c98a GH |
20 | static struct BusInfo i2c_bus_info = { |
21 | .name = "I2C", | |
22 | .size = sizeof(i2c_bus), | |
ee6847d1 | 23 | .props = (Property[]) { |
5b7f5327 | 24 | DEFINE_PROP_UINT8("address", struct i2c_slave, address, 0), |
368eb5d4 | 25 | DEFINE_PROP_END_OF_LIST(), |
ee6847d1 | 26 | } |
10c4c98a GH |
27 | }; |
28 | ||
8d0eb050 | 29 | static void i2c_bus_pre_save(void *opaque) |
c701b35b | 30 | { |
8d0eb050 | 31 | i2c_bus *bus = opaque; |
c701b35b | 32 | |
8d0eb050 | 33 | bus->saved_address = bus->current_dev ? bus->current_dev->address : -1; |
c701b35b PB |
34 | } |
35 | ||
8d0eb050 | 36 | static int i2c_bus_post_load(void *opaque, int version_id) |
c701b35b | 37 | { |
8d0eb050 | 38 | i2c_bus *bus = opaque; |
c701b35b PB |
39 | |
40 | /* The bus is loaded before attached devices, so load and save the | |
41 | current device id. Devices will check themselves as loaded. */ | |
c701b35b | 42 | bus->current_dev = NULL; |
c701b35b PB |
43 | return 0; |
44 | } | |
45 | ||
8d0eb050 JQ |
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 | ||
0ff596d0 | 59 | /* Create a new I2C bus. */ |
02e2da45 | 60 | i2c_bus *i2c_init_bus(DeviceState *parent, const char *name) |
0ff596d0 PB |
61 | { |
62 | i2c_bus *bus; | |
63 | ||
10c4c98a | 64 | bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name)); |
0be71e32 | 65 | vmstate_register(NULL, -1, &vmstate_i2c_bus, bus); |
0ff596d0 PB |
66 | return bus; |
67 | } | |
68 | ||
5b7f5327 | 69 | void i2c_set_slave_address(i2c_slave *dev, uint8_t address) |
0ff596d0 PB |
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 | ||
4a2c8ac2 | 80 | /* Returns non-zero if the address is not valid. */ |
0ff596d0 | 81 | /* TODO: Make this handle multiple masters. */ |
5b7f5327 | 82 | int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv) |
0ff596d0 | 83 | { |
02e2da45 PB |
84 | DeviceState *qdev; |
85 | i2c_slave *slave = NULL; | |
0ff596d0 | 86 | |
72cf2d4f | 87 | QLIST_FOREACH(qdev, &bus->qbus.children, sibling) { |
b3a21988 JR |
88 | i2c_slave *candidate = I2C_SLAVE_FROM_QDEV(qdev); |
89 | if (candidate->address == address) { | |
90 | slave = candidate; | |
0ff596d0 | 91 | break; |
b3a21988 | 92 | } |
0ff596d0 PB |
93 | } |
94 | ||
02e2da45 | 95 | if (!slave) |
0ff596d0 PB |
96 | return 1; |
97 | ||
98 | /* If the bus is already busy, assume this is a repeated | |
99 | start condition. */ | |
02e2da45 PB |
100 | bus->current_dev = slave; |
101 | slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND); | |
0ff596d0 PB |
102 | return 0; |
103 | } | |
104 | ||
105 | void i2c_end_transfer(i2c_bus *bus) | |
106 | { | |
107 | i2c_slave *dev = bus->current_dev; | |
108 | ||
109 | if (!dev) | |
110 | return; | |
111 | ||
fe8de492 | 112 | dev->info->event(dev, I2C_FINISH); |
0ff596d0 PB |
113 | |
114 | bus->current_dev = NULL; | |
115 | } | |
116 | ||
117 | int i2c_send(i2c_bus *bus, uint8_t data) | |
118 | { | |
119 | i2c_slave *dev = bus->current_dev; | |
120 | ||
121 | if (!dev) | |
122 | return -1; | |
123 | ||
fe8de492 | 124 | return dev->info->send(dev, data); |
0ff596d0 PB |
125 | } |
126 | ||
127 | int i2c_recv(i2c_bus *bus) | |
128 | { | |
129 | i2c_slave *dev = bus->current_dev; | |
130 | ||
131 | if (!dev) | |
132 | return -1; | |
133 | ||
fe8de492 | 134 | return dev->info->recv(dev); |
0ff596d0 PB |
135 | } |
136 | ||
137 | void i2c_nack(i2c_bus *bus) | |
138 | { | |
139 | i2c_slave *dev = bus->current_dev; | |
140 | ||
141 | if (!dev) | |
142 | return; | |
143 | ||
fe8de492 | 144 | dev->info->event(dev, I2C_NACK); |
0ff596d0 PB |
145 | } |
146 | ||
bcbe8068 | 147 | static int i2c_slave_post_load(void *opaque, int version_id) |
aa941b94 | 148 | { |
bcbe8068 | 149 | i2c_slave *dev = opaque; |
fe8de492 | 150 | i2c_bus *bus; |
02e2da45 | 151 | bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev)); |
fe8de492 PB |
152 | if (bus->saved_address == dev->address) { |
153 | bus->current_dev = dev; | |
154 | } | |
bcbe8068 JQ |
155 | return 0; |
156 | } | |
157 | ||
1894839f | 158 | const VMStateDescription vmstate_i2c_slave = { |
bcbe8068 JQ |
159 | .name = "i2c_slave", |
160 | .version_id = 1, | |
161 | .minimum_version_id = 1, | |
162 | .minimum_version_id_old = 1, | |
163 | .post_load = i2c_slave_post_load, | |
164 | .fields = (VMStateField []) { | |
165 | VMSTATE_UINT8(address, i2c_slave), | |
166 | VMSTATE_END_OF_LIST() | |
167 | } | |
168 | }; | |
169 | ||
81a322d4 | 170 | static int i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base) |
fe8de492 | 171 | { |
02e2da45 | 172 | I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev); |
fe8de492 PB |
173 | i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev); |
174 | ||
175 | s->info = info; | |
fe8de492 | 176 | |
81a322d4 | 177 | return info->init(s); |
fe8de492 PB |
178 | } |
179 | ||
074f2fff | 180 | void i2c_register_slave(I2CSlaveInfo *info) |
fe8de492 | 181 | { |
074f2fff | 182 | assert(info->qdev.size >= sizeof(i2c_slave)); |
02e2da45 | 183 | info->qdev.init = i2c_slave_qdev_init; |
10c4c98a | 184 | info->qdev.bus_info = &i2c_bus_info; |
074f2fff | 185 | qdev_register(&info->qdev); |
fe8de492 PB |
186 | } |
187 | ||
5b7f5327 | 188 | DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr) |
fe8de492 PB |
189 | { |
190 | DeviceState *dev; | |
191 | ||
02e2da45 | 192 | dev = qdev_create(&bus->qbus, name); |
5b7f5327 | 193 | qdev_prop_set_uint8(dev, "address", addr); |
e23a1b33 | 194 | qdev_init_nofail(dev); |
fe8de492 | 195 | return dev; |
aa941b94 | 196 | } |