Statistics
| Branch: | Revision:

root / hw / i2c.c @ 5b7f5327

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