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.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
12 #include "qemu/osdep.h"
14 #include "bitbang_i2c.h"
15 #include "hw/sysbus.h"
17 //#define DEBUG_BITBANG_I2C
19 #ifdef DEBUG_BITBANG_I2C
20 #define DPRINTF(fmt, ...) \
21 do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
23 #define DPRINTF(fmt, ...) do {} while(0)
26 typedef enum bitbang_i2c_state
{
49 struct bitbang_i2c_interface
{
51 bitbang_i2c_state state
;
59 static void bitbang_i2c_enter_stop(bitbang_i2c_interface
*i2c
)
62 if (i2c
->current_addr
>= 0)
63 i2c_end_transfer(i2c
->bus
);
64 i2c
->current_addr
= -1;
68 /* Set device data pin. */
69 static int bitbang_i2c_ret(bitbang_i2c_interface
*i2c
, int level
)
71 i2c
->device_out
= level
;
72 //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
73 return level
& i2c
->last_data
;
76 /* Leave device data pin unodified. */
77 static int bitbang_i2c_nop(bitbang_i2c_interface
*i2c
)
79 return bitbang_i2c_ret(i2c
, i2c
->device_out
);
82 /* Returns data line level. */
83 int bitbang_i2c_set(bitbang_i2c_interface
*i2c
, int line
, int level
)
87 if (level
!= 0 && level
!= 1) {
91 if (line
== BITBANG_I2C_SDA
) {
92 if (level
== i2c
->last_data
) {
93 return bitbang_i2c_nop(i2c
);
95 i2c
->last_data
= level
;
96 if (i2c
->last_clock
== 0) {
97 return bitbang_i2c_nop(i2c
);
101 /* START condition. */
102 i2c
->state
= SENDING_BIT7
;
103 i2c
->current_addr
= -1;
105 /* STOP condition. */
106 bitbang_i2c_enter_stop(i2c
);
108 return bitbang_i2c_ret(i2c
, 1);
111 data
= i2c
->last_data
;
112 if (i2c
->last_clock
== level
) {
113 return bitbang_i2c_nop(i2c
);
115 i2c
->last_clock
= level
;
117 /* State is set/read at the start of the clock pulse.
118 release the data line at the end. */
119 return bitbang_i2c_ret(i2c
, 1);
121 switch (i2c
->state
) {
124 return bitbang_i2c_ret(i2c
, 1);
126 case SENDING_BIT7
... SENDING_BIT0
:
127 i2c
->buffer
= (i2c
->buffer
<< 1) | data
;
128 /* will end up in WAITING_FOR_ACK */
130 return bitbang_i2c_ret(i2c
, 1);
132 case WAITING_FOR_ACK
:
133 if (i2c
->current_addr
< 0) {
134 i2c
->current_addr
= i2c
->buffer
;
135 DPRINTF("Address 0x%02x\n", i2c
->current_addr
);
136 i2c_start_transfer(i2c
->bus
, i2c
->current_addr
>> 1,
137 i2c
->current_addr
& 1);
139 DPRINTF("Sent 0x%02x\n", i2c
->buffer
);
140 i2c_send(i2c
->bus
, i2c
->buffer
);
142 if (i2c
->current_addr
& 1) {
143 i2c
->state
= RECEIVING_BIT7
;
145 i2c
->state
= SENDING_BIT7
;
147 return bitbang_i2c_ret(i2c
, 0);
150 i2c
->buffer
= i2c_recv(i2c
->bus
);
151 DPRINTF("RX byte 0x%02x\n", i2c
->buffer
);
152 /* Fall through... */
153 case RECEIVING_BIT6
... RECEIVING_BIT0
:
154 data
= i2c
->buffer
>> 7;
155 /* will end up in SENDING_ACK */
158 return bitbang_i2c_ret(i2c
, data
);
161 i2c
->state
= RECEIVING_BIT7
;
164 i2c
->state
= SENT_NACK
;
169 return bitbang_i2c_ret(i2c
, 1);
174 bitbang_i2c_interface
*bitbang_i2c_init(I2CBus
*bus
)
176 bitbang_i2c_interface
*s
;
178 s
= g_malloc0(sizeof(bitbang_i2c_interface
));
188 /* GPIO interface. */
190 #define TYPE_GPIO_I2C "gpio_i2c"
191 #define GPIO_I2C(obj) OBJECT_CHECK(GPIOI2CState, (obj), TYPE_GPIO_I2C)
193 typedef struct GPIOI2CState
{
194 SysBusDevice parent_obj
;
196 MemoryRegion dummy_iomem
;
197 bitbang_i2c_interface
*bitbang
;
202 static void bitbang_i2c_gpio_set(void *opaque
, int irq
, int level
)
204 GPIOI2CState
*s
= opaque
;
206 level
= bitbang_i2c_set(s
->bitbang
, irq
, level
);
207 if (level
!= s
->last_level
) {
208 s
->last_level
= level
;
209 qemu_set_irq(s
->out
, level
);
213 static int gpio_i2c_init(SysBusDevice
*sbd
)
215 DeviceState
*dev
= DEVICE(sbd
);
216 GPIOI2CState
*s
= GPIO_I2C(dev
);
219 memory_region_init(&s
->dummy_iomem
, OBJECT(s
), "gpio_i2c", 0);
220 sysbus_init_mmio(sbd
, &s
->dummy_iomem
);
222 bus
= i2c_init_bus(dev
, "i2c");
223 s
->bitbang
= bitbang_i2c_init(bus
);
225 qdev_init_gpio_in(dev
, bitbang_i2c_gpio_set
, 2);
226 qdev_init_gpio_out(dev
, &s
->out
, 1);
231 static void gpio_i2c_class_init(ObjectClass
*klass
, void *data
)
233 DeviceClass
*dc
= DEVICE_CLASS(klass
);
234 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
236 k
->init
= gpio_i2c_init
;
237 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
238 dc
->desc
= "Virtual GPIO to I2C bridge";
241 static const TypeInfo gpio_i2c_info
= {
242 .name
= TYPE_GPIO_I2C
,
243 .parent
= TYPE_SYS_BUS_DEVICE
,
244 .instance_size
= sizeof(GPIOI2CState
),
245 .class_init
= gpio_i2c_class_init
,
248 static void bitbang_i2c_register_types(void)
250 type_register_static(&gpio_i2c_info
);
253 type_init(bitbang_i2c_register_types
)