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

P
pbrook 已提交
10
#include "i2c.h"
P
pbrook 已提交
11 12 13

struct i2c_bus
{
P
Paul Brook 已提交
14
    BusState qbus;
P
pbrook 已提交
15 16
    i2c_slave *current_dev;
    i2c_slave *dev;
17
    uint8_t saved_address;
P
pbrook 已提交
18 19
};

20 21 22
static struct BusInfo i2c_bus_info = {
    .name = "I2C",
    .size = sizeof(i2c_bus),
G
Gerd Hoffmann 已提交
23
    .props = (Property[]) {
24
        DEFINE_PROP_UINT8("address", struct i2c_slave, address, 0),
25
        DEFINE_PROP_END_OF_LIST(),
G
Gerd Hoffmann 已提交
26
    }
27 28
};

J
Juan Quintela 已提交
29
static void i2c_bus_pre_save(void *opaque)
P
pbrook 已提交
30
{
J
Juan Quintela 已提交
31
    i2c_bus *bus = opaque;
P
pbrook 已提交
32

J
Juan Quintela 已提交
33
    bus->saved_address = bus->current_dev ? bus->current_dev->address : -1;
P
pbrook 已提交
34 35
}

J
Juan Quintela 已提交
36
static int i2c_bus_post_load(void *opaque, int version_id)
P
pbrook 已提交
37
{
J
Juan Quintela 已提交
38
    i2c_bus *bus = opaque;
P
pbrook 已提交
39 40 41 42 43 44 45

    /* The bus is loaded before attached devices, so load and save the
       current device id.  Devices will check themselves as loaded.  */
    bus->current_dev = NULL;
    return 0;
}

J
Juan Quintela 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
static const VMStateDescription vmstate_i2c_bus = {
    .name = "i2c_bus",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .pre_save = i2c_bus_pre_save,
    .post_load = i2c_bus_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_UINT8(saved_address, i2c_bus),
        VMSTATE_END_OF_LIST()
    }
};

P
pbrook 已提交
59
/* Create a new I2C bus.  */
P
Paul Brook 已提交
60
i2c_bus *i2c_init_bus(DeviceState *parent, const char *name)
P
pbrook 已提交
61 62 63
{
    i2c_bus *bus;

64
    bus = FROM_QBUS(i2c_bus, qbus_create(&i2c_bus_info, parent, name));
J
Juan Quintela 已提交
65
    vmstate_register(-1, &vmstate_i2c_bus, bus);
P
pbrook 已提交
66 67 68
    return bus;
}

69
void i2c_set_slave_address(i2c_slave *dev, uint8_t address)
P
pbrook 已提交
70 71 72 73 74 75 76 77 78 79
{
    dev->address = address;
}

/* Return nonzero if bus is busy.  */
int i2c_bus_busy(i2c_bus *bus)
{
    return bus->current_dev != NULL;
}

80
/* Returns non-zero if the address is not valid.  */
P
pbrook 已提交
81
/* TODO: Make this handle multiple masters.  */
82
int i2c_start_transfer(i2c_bus *bus, uint8_t address, int recv)
P
pbrook 已提交
83
{
P
Paul Brook 已提交
84 85
    DeviceState *qdev;
    i2c_slave *slave = NULL;
P
pbrook 已提交
86

B
Blue Swirl 已提交
87
    QLIST_FOREACH(qdev, &bus->qbus.children, sibling) {
P
Paul Brook 已提交
88 89
        slave = I2C_SLAVE_FROM_QDEV(qdev);
        if (slave->address == address)
P
pbrook 已提交
90 91 92
            break;
    }

P
Paul Brook 已提交
93
    if (!slave)
P
pbrook 已提交
94 95 96 97
        return 1;

    /* If the bus is already busy, assume this is a repeated
       start condition.  */
P
Paul Brook 已提交
98 99
    bus->current_dev = slave;
    slave->info->event(slave, recv ? I2C_START_RECV : I2C_START_SEND);
P
pbrook 已提交
100 101 102 103 104 105 106 107 108 109
    return 0;
}

void i2c_end_transfer(i2c_bus *bus)
{
    i2c_slave *dev = bus->current_dev;

    if (!dev)
        return;

P
Paul Brook 已提交
110
    dev->info->event(dev, I2C_FINISH);
P
pbrook 已提交
111 112 113 114 115 116 117 118 119 120 121

    bus->current_dev = NULL;
}

int i2c_send(i2c_bus *bus, uint8_t data)
{
    i2c_slave *dev = bus->current_dev;

    if (!dev)
        return -1;

P
Paul Brook 已提交
122
    return dev->info->send(dev, data);
P
pbrook 已提交
123 124 125 126 127 128 129 130 131
}

int i2c_recv(i2c_bus *bus)
{
    i2c_slave *dev = bus->current_dev;

    if (!dev)
        return -1;

P
Paul Brook 已提交
132
    return dev->info->recv(dev);
P
pbrook 已提交
133 134 135 136 137 138 139 140 141
}

void i2c_nack(i2c_bus *bus)
{
    i2c_slave *dev = bus->current_dev;

    if (!dev)
        return;

P
Paul Brook 已提交
142
    dev->info->event(dev, I2C_NACK);
P
pbrook 已提交
143 144
}

J
Juan Quintela 已提交
145
static int i2c_slave_post_load(void *opaque, int version_id)
146
{
J
Juan Quintela 已提交
147
    i2c_slave *dev = opaque;
P
Paul Brook 已提交
148
    i2c_bus *bus;
P
Paul Brook 已提交
149
    bus = FROM_QBUS(i2c_bus, qdev_get_parent_bus(&dev->qdev));
P
Paul Brook 已提交
150 151 152
    if (bus->saved_address == dev->address) {
        bus->current_dev = dev;
    }
J
Juan Quintela 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    return 0;
}

static const VMStateDescription vmstate_i2c_slave = {
    .name = "i2c_slave",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .post_load = i2c_slave_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_UINT8(address, i2c_slave),
        VMSTATE_END_OF_LIST()
    }
};

void i2c_slave_save(QEMUFile *f, i2c_slave *dev)
{
    vmstate_save_state(f, &vmstate_i2c_slave, dev);
}

void i2c_slave_load(QEMUFile *f, i2c_slave *dev)
{
    vmstate_load_state(f, &vmstate_i2c_slave, dev,
                       vmstate_i2c_slave.version_id);
P
Paul Brook 已提交
177 178
}

179
static int i2c_slave_qdev_init(DeviceState *dev, DeviceInfo *base)
P
Paul Brook 已提交
180
{
P
Paul Brook 已提交
181
    I2CSlaveInfo *info = container_of(base, I2CSlaveInfo, qdev);
P
Paul Brook 已提交
182 183 184 185
    i2c_slave *s = I2C_SLAVE_FROM_QDEV(dev);

    s->info = info;

186
    return info->init(s);
P
Paul Brook 已提交
187 188
}

189
void i2c_register_slave(I2CSlaveInfo *info)
P
Paul Brook 已提交
190
{
191
    assert(info->qdev.size >= sizeof(i2c_slave));
P
Paul Brook 已提交
192
    info->qdev.init = i2c_slave_qdev_init;
193
    info->qdev.bus_info = &i2c_bus_info;
194
    qdev_register(&info->qdev);
P
Paul Brook 已提交
195 196
}

197
DeviceState *i2c_create_slave(i2c_bus *bus, const char *name, uint8_t addr)
P
Paul Brook 已提交
198 199 200
{
    DeviceState *dev;

P
Paul Brook 已提交
201
    dev = qdev_create(&bus->qbus, name);
202
    qdev_prop_set_uint8(dev, "address", addr);
P
Paul Brook 已提交
203 204
    qdev_init(dev);
    return dev;
205
}