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.
13 #include "qemu/osdep.h"
15 #include "hw/i2c/bitbang_i2c.h"
16 #include "hw/sysbus.h"
17 #include "qemu/module.h"
19 //#define DEBUG_BITBANG_I2C
21 #ifdef DEBUG_BITBANG_I2C
22 #define DPRINTF(fmt, ...) \
23 do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
25 #define DPRINTF(fmt, ...) do {} while(0)
28 static void bitbang_i2c_enter_stop(bitbang_i2c_interface
*i2c
)
31 if (i2c
->current_addr
>= 0)
32 i2c_end_transfer(i2c
->bus
);
33 i2c
->current_addr
= -1;
37 /* Set device data pin. */
38 static int bitbang_i2c_ret(bitbang_i2c_interface
*i2c
, int level
)
40 i2c
->device_out
= level
;
41 //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
42 return level
& i2c
->last_data
;
45 /* Leave device data pin unodified. */
46 static int bitbang_i2c_nop(bitbang_i2c_interface
*i2c
)
48 return bitbang_i2c_ret(i2c
, i2c
->device_out
);
51 /* Returns data line level. */
52 int bitbang_i2c_set(bitbang_i2c_interface
*i2c
, int line
, int level
)
56 if (level
!= 0 && level
!= 1) {
60 if (line
== BITBANG_I2C_SDA
) {
61 if (level
== i2c
->last_data
) {
62 return bitbang_i2c_nop(i2c
);
64 i2c
->last_data
= level
;
65 if (i2c
->last_clock
== 0) {
66 return bitbang_i2c_nop(i2c
);
70 /* START condition. */
71 i2c
->state
= SENDING_BIT7
;
72 i2c
->current_addr
= -1;
75 bitbang_i2c_enter_stop(i2c
);
77 return bitbang_i2c_ret(i2c
, 1);
80 data
= i2c
->last_data
;
81 if (i2c
->last_clock
== level
) {
82 return bitbang_i2c_nop(i2c
);
84 i2c
->last_clock
= level
;
86 /* State is set/read at the start of the clock pulse.
87 release the data line at the end. */
88 return bitbang_i2c_ret(i2c
, 1);
93 return bitbang_i2c_ret(i2c
, 1);
95 case SENDING_BIT7
... SENDING_BIT0
:
96 i2c
->buffer
= (i2c
->buffer
<< 1) | data
;
97 /* will end up in WAITING_FOR_ACK */
99 return bitbang_i2c_ret(i2c
, 1);
101 case WAITING_FOR_ACK
:
105 if (i2c
->current_addr
< 0) {
106 i2c
->current_addr
= i2c
->buffer
;
107 DPRINTF("Address 0x%02x\n", i2c
->current_addr
);
108 ret
= i2c_start_transfer(i2c
->bus
, i2c
->current_addr
>> 1,
109 i2c
->current_addr
& 1);
111 DPRINTF("Sent 0x%02x\n", i2c
->buffer
);
112 ret
= i2c_send(i2c
->bus
, i2c
->buffer
);
115 /* NACK (either addressing a nonexistent device, or the
116 * device we were sending to decided to NACK us).
118 DPRINTF("Got NACK\n");
119 bitbang_i2c_enter_stop(i2c
);
120 return bitbang_i2c_ret(i2c
, 1);
122 if (i2c
->current_addr
& 1) {
123 i2c
->state
= RECEIVING_BIT7
;
125 i2c
->state
= SENDING_BIT7
;
127 return bitbang_i2c_ret(i2c
, 0);
130 i2c
->buffer
= i2c_recv(i2c
->bus
);
131 DPRINTF("RX byte 0x%02x\n", i2c
->buffer
);
132 /* Fall through... */
133 case RECEIVING_BIT6
... RECEIVING_BIT0
:
134 data
= i2c
->buffer
>> 7;
135 /* will end up in SENDING_ACK */
138 return bitbang_i2c_ret(i2c
, data
);
141 i2c
->state
= RECEIVING_BIT7
;
144 i2c
->state
= SENT_NACK
;
149 return bitbang_i2c_ret(i2c
, 1);
154 void bitbang_i2c_init(bitbang_i2c_interface
*s
, I2CBus
*bus
)
162 /* GPIO interface. */
164 #define TYPE_GPIO_I2C "gpio_i2c"
165 #define GPIO_I2C(obj) OBJECT_CHECK(GPIOI2CState, (obj), TYPE_GPIO_I2C)
167 typedef struct GPIOI2CState
{
168 SysBusDevice parent_obj
;
170 MemoryRegion dummy_iomem
;
171 bitbang_i2c_interface bitbang
;
176 static void bitbang_i2c_gpio_set(void *opaque
, int irq
, int level
)
178 GPIOI2CState
*s
= opaque
;
180 level
= bitbang_i2c_set(&s
->bitbang
, irq
, level
);
181 if (level
!= s
->last_level
) {
182 s
->last_level
= level
;
183 qemu_set_irq(s
->out
, level
);
187 static void gpio_i2c_init(Object
*obj
)
189 DeviceState
*dev
= DEVICE(obj
);
190 GPIOI2CState
*s
= GPIO_I2C(obj
);
191 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
194 memory_region_init(&s
->dummy_iomem
, obj
, "gpio_i2c", 0);
195 sysbus_init_mmio(sbd
, &s
->dummy_iomem
);
197 bus
= i2c_init_bus(dev
, "i2c");
198 bitbang_i2c_init(&s
->bitbang
, bus
);
200 qdev_init_gpio_in(dev
, bitbang_i2c_gpio_set
, 2);
201 qdev_init_gpio_out(dev
, &s
->out
, 1);
204 static void gpio_i2c_class_init(ObjectClass
*klass
, void *data
)
206 DeviceClass
*dc
= DEVICE_CLASS(klass
);
208 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
209 dc
->desc
= "Virtual GPIO to I2C bridge";
212 static const TypeInfo gpio_i2c_info
= {
213 .name
= TYPE_GPIO_I2C
,
214 .parent
= TYPE_SYS_BUS_DEVICE
,
215 .instance_size
= sizeof(GPIOI2CState
),
216 .instance_init
= gpio_i2c_init
,
217 .class_init
= gpio_i2c_class_init
,
220 static void bitbang_i2c_register_types(void)
222 type_register_static(&gpio_i2c_info
);
225 type_init(bitbang_i2c_register_types
)