core.c 6.6 KB
Newer Older
1
/*
P
pbrook 已提交
2 3 4 5 6
 * QEMU I2C bus interface.
 *
 * Copyright (c) 2007 CodeSourcery.
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the LGPL.
P
pbrook 已提交
8 9
 */

P
Peter Maydell 已提交
10
#include "qemu/osdep.h"
P
Paolo Bonzini 已提交
11
#include "hw/i2c/i2c.h"
P
pbrook 已提交
12

K
KONRAD Frederic 已提交
13 14 15 16 17 18 19
typedef struct I2CNode I2CNode;

struct I2CNode {
    I2CSlave *elt;
    QLIST_ENTRY(I2CNode) next;
};

A
Andreas Färber 已提交
20
struct I2CBus
P
pbrook 已提交
21
{
P
Paul Brook 已提交
22
    BusState qbus;
K
KONRAD Frederic 已提交
23
    QLIST_HEAD(, I2CNode) current_devs;
24
    uint8_t saved_address;
K
KONRAD Frederic 已提交
25
    bool broadcast;
P
pbrook 已提交
26 27
};

28 29 30 31 32
static Property i2c_props[] = {
    DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
    DEFINE_PROP_END_OF_LIST(),
};

33
#define TYPE_I2C_BUS "i2c-bus"
A
Andreas Färber 已提交
34
#define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS)
35 36 37 38

static const TypeInfo i2c_bus_info = {
    .name = TYPE_I2C_BUS,
    .parent = TYPE_BUS,
A
Andreas Färber 已提交
39
    .instance_size = sizeof(I2CBus),
40 41
};

J
Juan Quintela 已提交
42
static void i2c_bus_pre_save(void *opaque)
P
pbrook 已提交
43
{
A
Andreas Färber 已提交
44
    I2CBus *bus = opaque;
P
pbrook 已提交
45

K
KONRAD Frederic 已提交
46 47 48 49 50 51
    bus->saved_address = -1;
    if (!QLIST_EMPTY(&bus->current_devs)) {
        if (!bus->broadcast) {
            bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
        }
    }
P
pbrook 已提交
52 53
}

J
Juan Quintela 已提交
54 55 56 57 58
static const VMStateDescription vmstate_i2c_bus = {
    .name = "i2c_bus",
    .version_id = 1,
    .minimum_version_id = 1,
    .pre_save = i2c_bus_pre_save,
59
    .fields = (VMStateField[]) {
A
Andreas Färber 已提交
60
        VMSTATE_UINT8(saved_address, I2CBus),
K
KONRAD Frederic 已提交
61
        VMSTATE_BOOL(broadcast, I2CBus),
J
Juan Quintela 已提交
62 63 64 65
        VMSTATE_END_OF_LIST()
    }
};

P
pbrook 已提交
66
/* Create a new I2C bus.  */
A
Andreas Färber 已提交
67
I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
P
pbrook 已提交
68
{
A
Andreas Färber 已提交
69
    I2CBus *bus;
P
pbrook 已提交
70

A
Andreas Färber 已提交
71
    bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
K
KONRAD Frederic 已提交
72
    QLIST_INIT(&bus->current_devs);
A
Alex Williamson 已提交
73
    vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
P
pbrook 已提交
74 75 76
    return bus;
}

77
void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
P
pbrook 已提交
78 79 80 81 82
{
    dev->address = address;
}

/* Return nonzero if bus is busy.  */
A
Andreas Färber 已提交
83
int i2c_bus_busy(I2CBus *bus)
P
pbrook 已提交
84
{
K
KONRAD Frederic 已提交
85
    return !QLIST_EMPTY(&bus->current_devs);
P
pbrook 已提交
86 87
}

88
/* Returns non-zero if the address is not valid.  */
P
pbrook 已提交
89
/* TODO: Make this handle multiple masters.  */
A
Andreas Färber 已提交
90
int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
P
pbrook 已提交
91
{
A
Anthony Liguori 已提交
92
    BusChild *kid;
93
    I2CSlaveClass *sc;
K
KONRAD Frederic 已提交
94 95 96 97 98 99 100 101 102
    I2CNode *node;

    if (address == 0x00) {
        /*
         * This is a broadcast, the current_devs will be all the devices of the
         * bus.
         */
        bus->broadcast = true;
    }
P
pbrook 已提交
103

A
Anthony Liguori 已提交
104 105
    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
        DeviceState *qdev = kid->child;
106
        I2CSlave *candidate = I2C_SLAVE(qdev);
K
KONRAD Frederic 已提交
107 108 109 110 111 112 113
        if ((candidate->address == address) || (bus->broadcast)) {
            node = g_malloc(sizeof(struct I2CNode));
            node->elt = candidate;
            QLIST_INSERT_HEAD(&bus->current_devs, node, next);
            if (!bus->broadcast) {
                break;
            }
J
Juha Riihimäki 已提交
114
        }
P
pbrook 已提交
115 116
    }

K
KONRAD Frederic 已提交
117
    if (QLIST_EMPTY(&bus->current_devs)) {
P
pbrook 已提交
118
        return 1;
119
    }
P
pbrook 已提交
120

K
KONRAD Frederic 已提交
121 122 123 124 125 126 127
    QLIST_FOREACH(node, &bus->current_devs, next) {
        sc = I2C_SLAVE_GET_CLASS(node->elt);
        /* If the bus is already busy, assume this is a repeated
           start condition.  */
        if (sc->event) {
            sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
        }
128
    }
P
pbrook 已提交
129 130 131
    return 0;
}

A
Andreas Färber 已提交
132
void i2c_end_transfer(I2CBus *bus)
P
pbrook 已提交
133
{
134
    I2CSlaveClass *sc;
K
KONRAD Frederic 已提交
135
    I2CNode *node, *next;
P
pbrook 已提交
136

K
KONRAD Frederic 已提交
137
    if (QLIST_EMPTY(&bus->current_devs)) {
P
pbrook 已提交
138
        return;
139
    }
P
pbrook 已提交
140

K
KONRAD Frederic 已提交
141 142 143 144 145 146 147
    QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
        sc = I2C_SLAVE_GET_CLASS(node->elt);
        if (sc->event) {
            sc->event(node->elt, I2C_FINISH);
        }
        QLIST_REMOVE(node, next);
        g_free(node);
148
    }
K
KONRAD Frederic 已提交
149
    bus->broadcast = false;
P
pbrook 已提交
150 151
}

152
int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
P
pbrook 已提交
153
{
154
    I2CSlaveClass *sc;
K
KONRAD Frederic 已提交
155 156 157
    I2CNode *node;
    int ret = 0;

158 159 160 161 162 163 164 165 166 167 168 169 170
    if (send) {
        QLIST_FOREACH(node, &bus->current_devs, next) {
            sc = I2C_SLAVE_GET_CLASS(node->elt);
            if (sc->send) {
                ret = ret || sc->send(node->elt, *data);
            } else {
                ret = -1;
            }
        }
        return ret ? -1 : 0;
    } else {
        if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
            return -1;
K
KONRAD Frederic 已提交
171
        }
172 173 174 175 176 177 178 179 180 181 182 183

        sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
        if (sc->recv) {
            ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
            if (ret < 0) {
                return ret;
            } else {
                *data = ret;
                return 0;
            }
        }
        return -1;
184
    }
P
pbrook 已提交
185 186
}

187
int i2c_send(I2CBus *bus, uint8_t data)
P
pbrook 已提交
188
{
189 190
    return i2c_send_recv(bus, &data, true);
}
P
pbrook 已提交
191

192 193 194 195
int i2c_recv(I2CBus *bus)
{
    uint8_t data;
    int ret = i2c_send_recv(bus, &data, false);
196

197
    return ret < 0 ? ret : data;
P
pbrook 已提交
198 199
}

A
Andreas Färber 已提交
200
void i2c_nack(I2CBus *bus)
P
pbrook 已提交
201
{
202
    I2CSlaveClass *sc;
K
KONRAD Frederic 已提交
203
    I2CNode *node;
P
pbrook 已提交
204

K
KONRAD Frederic 已提交
205
    if (QLIST_EMPTY(&bus->current_devs)) {
P
pbrook 已提交
206
        return;
207
    }
P
pbrook 已提交
208

K
KONRAD Frederic 已提交
209 210 211 212 213
    QLIST_FOREACH(node, &bus->current_devs, next) {
        sc = I2C_SLAVE_GET_CLASS(node->elt);
        if (sc->event) {
            sc->event(node->elt, I2C_NACK);
        }
214
    }
P
pbrook 已提交
215 216
}

J
Juan Quintela 已提交
217
static int i2c_slave_post_load(void *opaque, int version_id)
218
{
219
    I2CSlave *dev = opaque;
A
Andreas Färber 已提交
220
    I2CBus *bus;
K
KONRAD Frederic 已提交
221 222
    I2CNode *node;

A
Andreas Färber 已提交
223
    bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
K
KONRAD Frederic 已提交
224 225 226 227
    if ((bus->saved_address == dev->address) || (bus->broadcast)) {
        node = g_malloc(sizeof(struct I2CNode));
        node->elt = dev;
        QLIST_INSERT_HEAD(&bus->current_devs, node, next);
P
Paul Brook 已提交
228
    }
J
Juan Quintela 已提交
229 230 231
    return 0;
}

J
Juan Quintela 已提交
232
const VMStateDescription vmstate_i2c_slave = {
233
    .name = "I2CSlave",
J
Juan Quintela 已提交
234 235 236
    .version_id = 1,
    .minimum_version_id = 1,
    .post_load = i2c_slave_post_load,
237
    .fields = (VMStateField[]) {
238
        VMSTATE_UINT8(address, I2CSlave),
J
Juan Quintela 已提交
239 240 241 242
        VMSTATE_END_OF_LIST()
    }
};

A
Anthony Liguori 已提交
243
static int i2c_slave_qdev_init(DeviceState *dev)
P
Paul Brook 已提交
244
{
245
    I2CSlave *s = I2C_SLAVE(dev);
246
    I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
P
Paul Brook 已提交
247

248 249
    return sc->init(s);
}
P
Paul Brook 已提交
250

A
Andreas Färber 已提交
251
DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
P
Paul Brook 已提交
252 253 254
{
    DeviceState *dev;

P
Paul Brook 已提交
255
    dev = qdev_create(&bus->qbus, name);
256
    qdev_prop_set_uint8(dev, "address", addr);
M
Markus Armbruster 已提交
257
    qdev_init_nofail(dev);
P
Paul Brook 已提交
258
    return dev;
259
}
260

261 262 263 264
static void i2c_slave_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *k = DEVICE_CLASS(klass);
    k->init = i2c_slave_qdev_init;
265
    set_bit(DEVICE_CATEGORY_MISC, k->categories);
266
    k->bus_type = TYPE_I2C_BUS;
267
    k->props = i2c_props;
268 269
}

270
static const TypeInfo i2c_slave_type_info = {
271 272 273 274 275
    .name = TYPE_I2C_SLAVE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(I2CSlave),
    .abstract = true,
    .class_size = sizeof(I2CSlaveClass),
276
    .class_init = i2c_slave_class_init,
277 278
};

A
Andreas Färber 已提交
279
static void i2c_slave_register_types(void)
280
{
281
    type_register_static(&i2c_bus_info);
282 283 284
    type_register_static(&i2c_slave_type_info);
}

A
Andreas Färber 已提交
285
type_init(i2c_slave_register_types)