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