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