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