2 * Bit-Bang i2c emulation extracted from
3 * Marvell MV88W8618 / Freecom MusicPal emulation.
5 * Copyright (c) 2008 Jan Kiszka
7 * This code is licenced 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
{
44 struct bitbang_i2c_interface
{
46 bitbang_i2c_state state
;
54 static void bitbang_i2c_enter_stop(bitbang_i2c_interface
*i2c
)
57 if (i2c
->current_addr
>= 0)
58 i2c_end_transfer(i2c
->bus
);
59 i2c
->current_addr
= -1;
63 /* Set device data pin. */
64 static int bitbang_i2c_ret(bitbang_i2c_interface
*i2c
, int level
)
66 i2c
->device_out
= level
;
67 //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
68 return level
& i2c
->last_data
;
71 /* Leave device data pin unodified. */
72 static int bitbang_i2c_nop(bitbang_i2c_interface
*i2c
)
74 return bitbang_i2c_ret(i2c
, i2c
->device_out
);
77 /* Returns data line level. */
78 int bitbang_i2c_set(bitbang_i2c_interface
*i2c
, int line
, int level
)
82 if (level
!= 0 && level
!= 1) {
86 if (line
== BITBANG_I2C_SDA
) {
87 if (level
== i2c
->last_data
) {
88 return bitbang_i2c_nop(i2c
);
90 i2c
->last_data
= level
;
91 if (i2c
->last_clock
== 0) {
92 return bitbang_i2c_nop(i2c
);
96 /* START condition. */
97 i2c
->state
= SENDING_BIT7
;
98 i2c
->current_addr
= -1;
100 /* STOP condition. */
101 bitbang_i2c_enter_stop(i2c
);
103 return bitbang_i2c_ret(i2c
, 1);
106 data
= i2c
->last_data
;
107 if (i2c
->last_clock
== level
) {
108 return bitbang_i2c_nop(i2c
);
110 i2c
->last_clock
= level
;
112 /* State is set/read at the start of the clock pulse.
113 release the data line at the end. */
114 return bitbang_i2c_ret(i2c
, 1);
116 switch (i2c
->state
) {
118 return bitbang_i2c_ret(i2c
, 1);
120 case SENDING_BIT7
... SENDING_BIT0
:
121 i2c
->buffer
= (i2c
->buffer
<< 1) | data
;
122 /* will end up in WAITING_FOR_ACK */
124 return bitbang_i2c_ret(i2c
, 1);
126 case WAITING_FOR_ACK
:
127 if (i2c
->current_addr
< 0) {
128 i2c
->current_addr
= i2c
->buffer
;
129 DPRINTF("Address 0x%02x\n", i2c
->current_addr
);
130 i2c_start_transfer(i2c
->bus
, i2c
->current_addr
>> 1,
131 i2c
->current_addr
& 1);
133 DPRINTF("Sent 0x%02x\n", i2c
->buffer
);
134 i2c_send(i2c
->bus
, i2c
->buffer
);
136 if (i2c
->current_addr
& 1) {
137 i2c
->state
= RECEIVING_BIT7
;
139 i2c
->state
= SENDING_BIT7
;
141 return bitbang_i2c_ret(i2c
, 0);
144 i2c
->buffer
= i2c_recv(i2c
->bus
);
145 DPRINTF("RX byte 0x%02x\n", i2c
->buffer
);
146 /* Fall through... */
147 case RECEIVING_BIT6
... RECEIVING_BIT0
:
148 data
= i2c
->buffer
>> 7;
149 /* will end up in SENDING_ACK */
152 return bitbang_i2c_ret(i2c
, data
);
155 i2c
->state
= RECEIVING_BIT7
;
162 return bitbang_i2c_ret(i2c
, 1);
167 bitbang_i2c_interface
*bitbang_i2c_init(i2c_bus
*bus
)
169 bitbang_i2c_interface
*s
;
171 s
= qemu_mallocz(sizeof(bitbang_i2c_interface
));
181 /* GPIO interface. */
184 bitbang_i2c_interface
*bitbang
;
189 static void bitbang_i2c_gpio_set(void *opaque
, int irq
, int level
)
191 GPIOI2CState
*s
= opaque
;
193 level
= bitbang_i2c_set(s
->bitbang
, irq
, level
);
194 if (level
!= s
->last_level
) {
195 s
->last_level
= level
;
196 qemu_set_irq(s
->out
, level
);
200 static int gpio_i2c_init(SysBusDevice
*dev
)
202 GPIOI2CState
*s
= FROM_SYSBUS(GPIOI2CState
, dev
);
205 sysbus_init_mmio(dev
, 0x0, 0);
207 bus
= i2c_init_bus(&dev
->qdev
, "i2c");
208 s
->bitbang
= bitbang_i2c_init(bus
);
210 qdev_init_gpio_in(&dev
->qdev
, bitbang_i2c_gpio_set
, 2);
211 qdev_init_gpio_out(&dev
->qdev
, &s
->out
, 1);
216 static SysBusDeviceInfo gpio_i2c_info
= {
217 .init
= gpio_i2c_init
,
218 .qdev
.name
= "gpio_i2c",
219 .qdev
.desc
= "Virtual GPIO to I2C bridge",
220 .qdev
.size
= sizeof(GPIOI2CState
),
223 static void bitbang_i2c_register(void)
225 sysbus_register_withprop(&gpio_i2c_info
);
228 device_init(bitbang_i2c_register
)