Statistics
| Branch: | Revision:

root / hw / bitbang_i2c.c @ 57c83dac

History | View | Annotate | Download (6 kB)

1 3ead03bd Andrzej Zaborowski
/*
2 3ead03bd Andrzej Zaborowski
 * Bit-Bang i2c emulation extracted from
3 3ead03bd Andrzej Zaborowski
 * Marvell MV88W8618 / Freecom MusicPal emulation.
4 3ead03bd Andrzej Zaborowski
 *
5 3ead03bd Andrzej Zaborowski
 * Copyright (c) 2008 Jan Kiszka
6 3ead03bd Andrzej Zaborowski
 *
7 8e31bf38 Matthew Fernandez
 * This code is licensed under the GNU GPL v2.
8 6b620ca3 Paolo Bonzini
 *
9 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
10 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
11 3ead03bd Andrzej Zaborowski
 */
12 3ead03bd Andrzej Zaborowski
#include "hw.h"
13 3cd035d8 Paul Brook
#include "bitbang_i2c.h"
14 3ead03bd Andrzej Zaborowski
#include "sysbus.h"
15 3ead03bd Andrzej Zaborowski
16 3cd035d8 Paul Brook
//#define DEBUG_BITBANG_I2C
17 3cd035d8 Paul Brook
18 3cd035d8 Paul Brook
#ifdef DEBUG_BITBANG_I2C
19 3cd035d8 Paul Brook
#define DPRINTF(fmt, ...) \
20 3cd035d8 Paul Brook
do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
21 3cd035d8 Paul Brook
#else
22 3cd035d8 Paul Brook
#define DPRINTF(fmt, ...) do {} while(0)
23 3cd035d8 Paul Brook
#endif
24 3cd035d8 Paul Brook
25 3ead03bd Andrzej Zaborowski
typedef enum bitbang_i2c_state {
26 3ead03bd Andrzej Zaborowski
    STOPPED = 0,
27 3ead03bd Andrzej Zaborowski
    SENDING_BIT7,
28 3ead03bd Andrzej Zaborowski
    SENDING_BIT6,
29 3ead03bd Andrzej Zaborowski
    SENDING_BIT5,
30 3ead03bd Andrzej Zaborowski
    SENDING_BIT4,
31 3ead03bd Andrzej Zaborowski
    SENDING_BIT3,
32 3ead03bd Andrzej Zaborowski
    SENDING_BIT2,
33 3ead03bd Andrzej Zaborowski
    SENDING_BIT1,
34 3ead03bd Andrzej Zaborowski
    SENDING_BIT0,
35 3ead03bd Andrzej Zaborowski
    WAITING_FOR_ACK,
36 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT7,
37 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT6,
38 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT5,
39 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT4,
40 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT3,
41 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT2,
42 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT1,
43 3ead03bd Andrzej Zaborowski
    RECEIVING_BIT0,
44 2eb9f241 Marcus Comstedt
    SENDING_ACK,
45 2eb9f241 Marcus Comstedt
    SENT_NACK
46 3ead03bd Andrzej Zaborowski
} bitbang_i2c_state;
47 3ead03bd Andrzej Zaborowski
48 3cd035d8 Paul Brook
struct bitbang_i2c_interface {
49 3ead03bd Andrzej Zaborowski
    i2c_bus *bus;
50 3ead03bd Andrzej Zaborowski
    bitbang_i2c_state state;
51 3ead03bd Andrzej Zaborowski
    int last_data;
52 3ead03bd Andrzej Zaborowski
    int last_clock;
53 3cd035d8 Paul Brook
    int device_out;
54 3ead03bd Andrzej Zaborowski
    uint8_t buffer;
55 3ead03bd Andrzej Zaborowski
    int current_addr;
56 3cd035d8 Paul Brook
};
57 3ead03bd Andrzej Zaborowski
58 3ead03bd Andrzej Zaborowski
static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c)
59 3ead03bd Andrzej Zaborowski
{
60 3cd035d8 Paul Brook
    DPRINTF("STOP\n");
61 3ead03bd Andrzej Zaborowski
    if (i2c->current_addr >= 0)
62 3ead03bd Andrzej Zaborowski
        i2c_end_transfer(i2c->bus);
63 3ead03bd Andrzej Zaborowski
    i2c->current_addr = -1;
64 3ead03bd Andrzej Zaborowski
    i2c->state = STOPPED;
65 3ead03bd Andrzej Zaborowski
}
66 3ead03bd Andrzej Zaborowski
67 3cd035d8 Paul Brook
/* Set device data pin.  */
68 3cd035d8 Paul Brook
static int bitbang_i2c_ret(bitbang_i2c_interface *i2c, int level)
69 3cd035d8 Paul Brook
{
70 3cd035d8 Paul Brook
    i2c->device_out = level;
71 3cd035d8 Paul Brook
    //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
72 3cd035d8 Paul Brook
    return level & i2c->last_data;
73 3cd035d8 Paul Brook
}
74 3cd035d8 Paul Brook
75 3cd035d8 Paul Brook
/* Leave device data pin unodified.  */
76 3cd035d8 Paul Brook
static int bitbang_i2c_nop(bitbang_i2c_interface *i2c)
77 3cd035d8 Paul Brook
{
78 3cd035d8 Paul Brook
    return bitbang_i2c_ret(i2c, i2c->device_out);
79 3cd035d8 Paul Brook
}
80 3cd035d8 Paul Brook
81 3cd035d8 Paul Brook
/* Returns data line level.  */
82 3cd035d8 Paul Brook
int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
83 3ead03bd Andrzej Zaborowski
{
84 3ead03bd Andrzej Zaborowski
    int data;
85 3ead03bd Andrzej Zaborowski
86 3cd035d8 Paul Brook
    if (level != 0 && level != 1) {
87 3cd035d8 Paul Brook
        abort();
88 3cd035d8 Paul Brook
    }
89 3ead03bd Andrzej Zaborowski
90 3cd035d8 Paul Brook
    if (line == BITBANG_I2C_SDA) {
91 3cd035d8 Paul Brook
        if (level == i2c->last_data) {
92 3cd035d8 Paul Brook
            return bitbang_i2c_nop(i2c);
93 3cd035d8 Paul Brook
        }
94 3cd035d8 Paul Brook
        i2c->last_data = level;
95 3cd035d8 Paul Brook
        if (i2c->last_clock == 0) {
96 3cd035d8 Paul Brook
            return bitbang_i2c_nop(i2c);
97 3cd035d8 Paul Brook
        }
98 3cd035d8 Paul Brook
        if (level == 0) {
99 3cd035d8 Paul Brook
            DPRINTF("START\n");
100 3cd035d8 Paul Brook
            /* START condition.  */
101 3ead03bd Andrzej Zaborowski
            i2c->state = SENDING_BIT7;
102 3cd035d8 Paul Brook
            i2c->current_addr = -1;
103 3cd035d8 Paul Brook
        } else {
104 3cd035d8 Paul Brook
            /* STOP condition.  */
105 3ead03bd Andrzej Zaborowski
            bitbang_i2c_enter_stop(i2c);
106 3cd035d8 Paul Brook
        }
107 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 1);
108 3cd035d8 Paul Brook
    }
109 3cd035d8 Paul Brook
110 3cd035d8 Paul Brook
    data = i2c->last_data;
111 3cd035d8 Paul Brook
    if (i2c->last_clock == level) {
112 3cd035d8 Paul Brook
        return bitbang_i2c_nop(i2c);
113 3cd035d8 Paul Brook
    }
114 3cd035d8 Paul Brook
    i2c->last_clock = level;
115 3cd035d8 Paul Brook
    if (level == 0) {
116 3cd035d8 Paul Brook
        /* State is set/read at the start of the clock pulse.
117 3cd035d8 Paul Brook
           release the data line at the end.  */
118 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 1);
119 3cd035d8 Paul Brook
    }
120 3cd035d8 Paul Brook
    switch (i2c->state) {
121 3cd035d8 Paul Brook
    case STOPPED:
122 2eb9f241 Marcus Comstedt
    case SENT_NACK:
123 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 1);
124 3ead03bd Andrzej Zaborowski
125 3ead03bd Andrzej Zaborowski
    case SENDING_BIT7 ... SENDING_BIT0:
126 3cd035d8 Paul Brook
        i2c->buffer = (i2c->buffer << 1) | data;
127 3cd035d8 Paul Brook
        /* will end up in WAITING_FOR_ACK */
128 3cd035d8 Paul Brook
        i2c->state++; 
129 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 1);
130 3ead03bd Andrzej Zaborowski
131 3ead03bd Andrzej Zaborowski
    case WAITING_FOR_ACK:
132 3cd035d8 Paul Brook
        if (i2c->current_addr < 0) {
133 3cd035d8 Paul Brook
            i2c->current_addr = i2c->buffer;
134 3cd035d8 Paul Brook
            DPRINTF("Address 0x%02x\n", i2c->current_addr);
135 3cd035d8 Paul Brook
            i2c_start_transfer(i2c->bus, i2c->current_addr >> 1,
136 3cd035d8 Paul Brook
                               i2c->current_addr & 1);
137 3cd035d8 Paul Brook
        } else {
138 3cd035d8 Paul Brook
            DPRINTF("Sent 0x%02x\n", i2c->buffer);
139 3cd035d8 Paul Brook
            i2c_send(i2c->bus, i2c->buffer);
140 3cd035d8 Paul Brook
        }
141 3cd035d8 Paul Brook
        if (i2c->current_addr & 1) {
142 3cd035d8 Paul Brook
            i2c->state = RECEIVING_BIT7;
143 3cd035d8 Paul Brook
        } else {
144 3cd035d8 Paul Brook
            i2c->state = SENDING_BIT7;
145 3cd035d8 Paul Brook
        }
146 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 0);
147 3cd035d8 Paul Brook
148 3cd035d8 Paul Brook
    case RECEIVING_BIT7:
149 3cd035d8 Paul Brook
        i2c->buffer = i2c_recv(i2c->bus);
150 3cd035d8 Paul Brook
        DPRINTF("RX byte 0x%02x\n", i2c->buffer);
151 3cd035d8 Paul Brook
        /* Fall through... */
152 3cd035d8 Paul Brook
    case RECEIVING_BIT6 ... RECEIVING_BIT0:
153 3cd035d8 Paul Brook
        data = i2c->buffer >> 7;
154 3cd035d8 Paul Brook
        /* will end up in SENDING_ACK */
155 3cd035d8 Paul Brook
        i2c->state++;
156 3cd035d8 Paul Brook
        i2c->buffer <<= 1;
157 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, data);
158 3ead03bd Andrzej Zaborowski
159 3ead03bd Andrzej Zaborowski
    case SENDING_ACK:
160 3cd035d8 Paul Brook
        i2c->state = RECEIVING_BIT7;
161 3cd035d8 Paul Brook
        if (data != 0) {
162 3cd035d8 Paul Brook
            DPRINTF("NACKED\n");
163 2eb9f241 Marcus Comstedt
            i2c->state = SENT_NACK;
164 3cd035d8 Paul Brook
            i2c_nack(i2c->bus);
165 3cd035d8 Paul Brook
        } else {
166 3cd035d8 Paul Brook
            DPRINTF("ACKED\n");
167 3cd035d8 Paul Brook
        }
168 3cd035d8 Paul Brook
        return bitbang_i2c_ret(i2c, 1);
169 3ead03bd Andrzej Zaborowski
    }
170 3cd035d8 Paul Brook
    abort();
171 3cd035d8 Paul Brook
}
172 3cd035d8 Paul Brook
173 3cd035d8 Paul Brook
bitbang_i2c_interface *bitbang_i2c_init(i2c_bus *bus)
174 3cd035d8 Paul Brook
{
175 3cd035d8 Paul Brook
    bitbang_i2c_interface *s;
176 3cd035d8 Paul Brook
177 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(bitbang_i2c_interface));
178 3cd035d8 Paul Brook
179 3cd035d8 Paul Brook
    s->bus = bus;
180 3cd035d8 Paul Brook
    s->last_data = 1;
181 3cd035d8 Paul Brook
    s->last_clock = 1;
182 3cd035d8 Paul Brook
    s->device_out = 1;
183 3cd035d8 Paul Brook
184 3cd035d8 Paul Brook
    return s;
185 3cd035d8 Paul Brook
}
186 3ead03bd Andrzej Zaborowski
187 3cd035d8 Paul Brook
/* GPIO interface.  */
188 3cd035d8 Paul Brook
typedef struct {
189 3cd035d8 Paul Brook
    SysBusDevice busdev;
190 cffac71b Avi Kivity
    MemoryRegion dummy_iomem;
191 3cd035d8 Paul Brook
    bitbang_i2c_interface *bitbang;
192 3cd035d8 Paul Brook
    int last_level;
193 3cd035d8 Paul Brook
    qemu_irq out;
194 3cd035d8 Paul Brook
} GPIOI2CState;
195 3cd035d8 Paul Brook
196 3cd035d8 Paul Brook
static void bitbang_i2c_gpio_set(void *opaque, int irq, int level)
197 3cd035d8 Paul Brook
{
198 3cd035d8 Paul Brook
    GPIOI2CState *s = opaque;
199 3cd035d8 Paul Brook
200 3cd035d8 Paul Brook
    level = bitbang_i2c_set(s->bitbang, irq, level);
201 3cd035d8 Paul Brook
    if (level != s->last_level) {
202 3cd035d8 Paul Brook
        s->last_level = level;
203 3cd035d8 Paul Brook
        qemu_set_irq(s->out, level);
204 3cd035d8 Paul Brook
    }
205 3ead03bd Andrzej Zaborowski
}
206 3ead03bd Andrzej Zaborowski
207 3cd035d8 Paul Brook
static int gpio_i2c_init(SysBusDevice *dev)
208 3ead03bd Andrzej Zaborowski
{
209 3cd035d8 Paul Brook
    GPIOI2CState *s = FROM_SYSBUS(GPIOI2CState, dev);
210 3ead03bd Andrzej Zaborowski
    i2c_bus *bus;
211 3ead03bd Andrzej Zaborowski
212 cffac71b Avi Kivity
    memory_region_init(&s->dummy_iomem, "gpio_i2c", 0);
213 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->dummy_iomem);
214 3ead03bd Andrzej Zaborowski
215 3ead03bd Andrzej Zaborowski
    bus = i2c_init_bus(&dev->qdev, "i2c");
216 3cd035d8 Paul Brook
    s->bitbang = bitbang_i2c_init(bus);
217 3ead03bd Andrzej Zaborowski
218 3ead03bd Andrzej Zaborowski
    qdev_init_gpio_in(&dev->qdev, bitbang_i2c_gpio_set, 2);
219 3ead03bd Andrzej Zaborowski
    qdev_init_gpio_out(&dev->qdev, &s->out, 1);
220 81a322d4 Gerd Hoffmann
221 81a322d4 Gerd Hoffmann
    return 0;
222 3ead03bd Andrzej Zaborowski
}
223 3ead03bd Andrzej Zaborowski
224 999e12bb Anthony Liguori
static void gpio_i2c_class_init(ObjectClass *klass, void *data)
225 999e12bb Anthony Liguori
{
226 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
227 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
228 999e12bb Anthony Liguori
229 999e12bb Anthony Liguori
    k->init = gpio_i2c_init;
230 39bffca2 Anthony Liguori
    dc->desc = "Virtual GPIO to I2C bridge";
231 999e12bb Anthony Liguori
}
232 999e12bb Anthony Liguori
233 39bffca2 Anthony Liguori
static TypeInfo gpio_i2c_info = {
234 39bffca2 Anthony Liguori
    .name          = "gpio_i2c",
235 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
236 39bffca2 Anthony Liguori
    .instance_size = sizeof(GPIOI2CState),
237 39bffca2 Anthony Liguori
    .class_init    = gpio_i2c_class_init,
238 3cd035d8 Paul Brook
};
239 3cd035d8 Paul Brook
240 3ead03bd Andrzej Zaborowski
static void bitbang_i2c_register(void)
241 3ead03bd Andrzej Zaborowski
{
242 39bffca2 Anthony Liguori
    type_register_static(&gpio_i2c_info);
243 3ead03bd Andrzej Zaborowski
}
244 3ead03bd Andrzej Zaborowski
245 3ead03bd Andrzej Zaborowski
device_init(bitbang_i2c_register)