Statistics
| Branch: | Revision:

root / hw / bitbang_i2c.c @ 2eb9f241

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