2 * Bit-Bang i2c emulation extracted from
3 * Marvell MV88W8618 / Freecom MusicPal emulation.
5 * Copyright (c) 2008 Jan Kiszka
7 * This code is licensed under the GNU GPL v2.
10 #include "bitbang_i2c.h"
13 //#define DEBUG_BITBANG_I2C
15 #ifdef DEBUG_BITBANG_I2C
16 #define DPRINTF(fmt, ...) \
17 do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
19 #define DPRINTF(fmt, ...) do {} while(0)
22 typedef enum bitbang_i2c_state
{
45 struct bitbang_i2c_interface
{
47 bitbang_i2c_state state
;
55 static void bitbang_i2c_enter_stop(bitbang_i2c_interface
*i2c
)
58 if (i2c
->current_addr
>= 0)
59 i2c_end_transfer(i2c
->bus
);
60 i2c
->current_addr
= -1;
64 /* Set device data pin. */
65 static int bitbang_i2c_ret(bitbang_i2c_interface
*i2c
, int level
)
67 i2c
->device_out
= level
;
68 //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
69 return level
& i2c
->last_data
;
72 /* Leave device data pin unodified. */
73 static int bitbang_i2c_nop(bitbang_i2c_interface
*i2c
)
75 return bitbang_i2c_ret(i2c
, i2c
->device_out
);
78 /* Returns data line level. */
79 int bitbang_i2c_set(bitbang_i2c_interface
*i2c
, int line
, int level
)
83 if (level
!= 0 && level
!= 1) {
87 if (line
== BITBANG_I2C_SDA
) {
88 if (level
== i2c
->last_data
) {
89 return bitbang_i2c_nop(i2c
);
91 i2c
->last_data
= level
;
92 if (i2c
->last_clock
== 0) {
93 return bitbang_i2c_nop(i2c
);
97 /* START condition. */
98 i2c
->state
= SENDING_BIT7
;
99 i2c
->current_addr
= -1;
101 /* STOP condition. */
102 bitbang_i2c_enter_stop(i2c
);
104 return bitbang_i2c_ret(i2c
, 1);
107 data
= i2c
->last_data
;
108 if (i2c
->last_clock
== level
) {
109 return bitbang_i2c_nop(i2c
);
111 i2c
->last_clock
= level
;
113 /* State is set/read at the start of the clock pulse.
114 release the data line at the end. */
115 return bitbang_i2c_ret(i2c
, 1);
117 switch (i2c
->state
) {
120 return bitbang_i2c_ret(i2c
, 1);
122 case SENDING_BIT7
... SENDING_BIT0
:
123 i2c
->buffer
= (i2c
->buffer
<< 1) | data
;
124 /* will end up in WAITING_FOR_ACK */
126 return bitbang_i2c_ret(i2c
, 1);
128 case WAITING_FOR_ACK
:
129 if (i2c
->current_addr
< 0) {
130 i2c
->current_addr
= i2c
->buffer
;
131 DPRINTF("Address 0x%02x\n", i2c
->current_addr
);
132 i2c_start_transfer(i2c
->bus
, i2c
->current_addr
>> 1,
133 i2c
->current_addr
& 1);
135 DPRINTF("Sent 0x%02x\n", i2c
->buffer
);
136 i2c_send(i2c
->bus
, i2c
->buffer
);
138 if (i2c
->current_addr
& 1) {
139 i2c
->state
= RECEIVING_BIT7
;
141 i2c
->state
= SENDING_BIT7
;
143 return bitbang_i2c_ret(i2c
, 0);
146 i2c
->buffer
= i2c_recv(i2c
->bus
);
147 DPRINTF("RX byte 0x%02x\n", i2c
->buffer
);
148 /* Fall through... */
149 case RECEIVING_BIT6
... RECEIVING_BIT0
:
150 data
= i2c
->buffer
>> 7;
151 /* will end up in SENDING_ACK */
154 return bitbang_i2c_ret(i2c
, data
);
157 i2c
->state
= RECEIVING_BIT7
;
160 i2c
->state
= SENT_NACK
;
165 return bitbang_i2c_ret(i2c
, 1);
170 bitbang_i2c_interface
*bitbang_i2c_init(i2c_bus
*bus
)
172 bitbang_i2c_interface
*s
;
174 s
= qemu_mallocz(sizeof(bitbang_i2c_interface
));
184 /* GPIO interface. */
187 bitbang_i2c_interface
*bitbang
;
192 static void bitbang_i2c_gpio_set(void *opaque
, int irq
, int level
)
194 GPIOI2CState
*s
= opaque
;
196 level
= bitbang_i2c_set(s
->bitbang
, irq
, level
);
197 if (level
!= s
->last_level
) {
198 s
->last_level
= level
;
199 qemu_set_irq(s
->out
, level
);
203 static int gpio_i2c_init(SysBusDevice
*dev
)
205 GPIOI2CState
*s
= FROM_SYSBUS(GPIOI2CState
, dev
);
208 sysbus_init_mmio(dev
, 0x0, 0);
210 bus
= i2c_init_bus(&dev
->qdev
, "i2c");
211 s
->bitbang
= bitbang_i2c_init(bus
);
213 qdev_init_gpio_in(&dev
->qdev
, bitbang_i2c_gpio_set
, 2);
214 qdev_init_gpio_out(&dev
->qdev
, &s
->out
, 1);
219 static SysBusDeviceInfo gpio_i2c_info
= {
220 .init
= gpio_i2c_init
,
221 .qdev
.name
= "gpio_i2c",
222 .qdev
.desc
= "Virtual GPIO to I2C bridge",
223 .qdev
.size
= sizeof(GPIOI2CState
),
226 static void bitbang_i2c_register(void)
228 sysbus_register_withprop(&gpio_i2c_info
);
231 device_init(bitbang_i2c_register
)