2009-08-23 17:38:50 +04:00
|
|
|
/*
|
|
|
|
* Bit-Bang i2c emulation extracted from
|
|
|
|
* Marvell MV88W8618 / Freecom MusicPal emulation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Jan Kiszka
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the GNU GPL v2.
|
2012-01-13 20:44:23 +04:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-08-23 17:38:50 +04:00
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-01-26 21:17:30 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-06-20 13:55:23 +03:00
|
|
|
#include "hw/i2c/bitbang_i2c.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2023-01-11 11:50:15 +03:00
|
|
|
#include "trace.h"
|
2009-08-23 17:38:50 +04:00
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
|
2023-01-11 11:50:15 +03:00
|
|
|
/* bitbang_i2c_state enum to name */
|
|
|
|
static const char * const sname[] = {
|
|
|
|
#define NAME(e) [e] = stringify(e)
|
|
|
|
NAME(STOPPED),
|
|
|
|
[SENDING_BIT7] = "SENDING_BIT7 (START)",
|
|
|
|
NAME(SENDING_BIT6),
|
|
|
|
NAME(SENDING_BIT5),
|
|
|
|
NAME(SENDING_BIT4),
|
|
|
|
NAME(SENDING_BIT3),
|
|
|
|
NAME(SENDING_BIT2),
|
|
|
|
NAME(SENDING_BIT1),
|
|
|
|
NAME(SENDING_BIT0),
|
|
|
|
NAME(WAITING_FOR_ACK),
|
|
|
|
[RECEIVING_BIT7] = "RECEIVING_BIT7 (ACK)",
|
|
|
|
NAME(RECEIVING_BIT6),
|
|
|
|
NAME(RECEIVING_BIT5),
|
|
|
|
NAME(RECEIVING_BIT4),
|
|
|
|
NAME(RECEIVING_BIT3),
|
|
|
|
NAME(RECEIVING_BIT2),
|
|
|
|
NAME(RECEIVING_BIT1),
|
|
|
|
NAME(RECEIVING_BIT0),
|
|
|
|
NAME(SENDING_ACK),
|
|
|
|
NAME(SENT_NACK)
|
|
|
|
#undef NAME
|
|
|
|
};
|
|
|
|
|
2023-01-11 11:50:14 +03:00
|
|
|
static void bitbang_i2c_set_state(bitbang_i2c_interface *i2c,
|
|
|
|
bitbang_i2c_state state)
|
|
|
|
{
|
2023-01-11 11:50:15 +03:00
|
|
|
trace_bitbang_i2c_state(sname[i2c->state], sname[state]);
|
2023-01-11 11:50:14 +03:00
|
|
|
i2c->state = state;
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:38:50 +04:00
|
|
|
static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c)
|
|
|
|
{
|
|
|
|
if (i2c->current_addr >= 0)
|
|
|
|
i2c_end_transfer(i2c->bus);
|
|
|
|
i2c->current_addr = -1;
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, STOPPED);
|
2009-08-23 17:38:50 +04:00
|
|
|
}
|
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
/* Set device data pin. */
|
|
|
|
static int bitbang_i2c_ret(bitbang_i2c_interface *i2c, int level)
|
|
|
|
{
|
2023-01-11 11:50:16 +03:00
|
|
|
trace_bitbang_i2c_data(i2c->last_clock, i2c->last_data,
|
|
|
|
i2c->device_out, level);
|
2009-11-21 02:37:15 +03:00
|
|
|
i2c->device_out = level;
|
2023-01-11 11:50:16 +03:00
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
return level & i2c->last_data;
|
|
|
|
}
|
|
|
|
|
2023-07-28 03:45:24 +03:00
|
|
|
/* Leave device data pin unmodified. */
|
2009-11-21 02:37:15 +03:00
|
|
|
static int bitbang_i2c_nop(bitbang_i2c_interface *i2c)
|
|
|
|
{
|
|
|
|
return bitbang_i2c_ret(i2c, i2c->device_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns data line level. */
|
|
|
|
int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
|
2009-08-23 17:38:50 +04:00
|
|
|
{
|
|
|
|
int data;
|
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
if (level != 0 && level != 1) {
|
|
|
|
abort();
|
|
|
|
}
|
2009-08-23 17:38:50 +04:00
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
if (line == BITBANG_I2C_SDA) {
|
|
|
|
if (level == i2c->last_data) {
|
|
|
|
return bitbang_i2c_nop(i2c);
|
|
|
|
}
|
|
|
|
i2c->last_data = level;
|
|
|
|
if (i2c->last_clock == 0) {
|
|
|
|
return bitbang_i2c_nop(i2c);
|
|
|
|
}
|
|
|
|
if (level == 0) {
|
|
|
|
/* START condition. */
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, SENDING_BIT7);
|
2009-11-21 02:37:15 +03:00
|
|
|
i2c->current_addr = -1;
|
|
|
|
} else {
|
|
|
|
/* STOP condition. */
|
2009-08-23 17:38:50 +04:00
|
|
|
bitbang_i2c_enter_stop(i2c);
|
2009-11-21 02:37:15 +03:00
|
|
|
}
|
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
data = i2c->last_data;
|
|
|
|
if (i2c->last_clock == level) {
|
|
|
|
return bitbang_i2c_nop(i2c);
|
|
|
|
}
|
|
|
|
i2c->last_clock = level;
|
|
|
|
if (level == 0) {
|
|
|
|
/* State is set/read at the start of the clock pulse.
|
|
|
|
release the data line at the end. */
|
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
|
|
|
}
|
|
|
|
switch (i2c->state) {
|
|
|
|
case STOPPED:
|
2011-05-28 18:55:52 +04:00
|
|
|
case SENT_NACK:
|
2009-11-21 02:37:15 +03:00
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
2009-08-23 17:38:50 +04:00
|
|
|
|
|
|
|
case SENDING_BIT7 ... SENDING_BIT0:
|
2009-11-21 02:37:15 +03:00
|
|
|
i2c->buffer = (i2c->buffer << 1) | data;
|
|
|
|
/* will end up in WAITING_FOR_ACK */
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, i2c->state + 1);
|
2009-11-21 02:37:15 +03:00
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
2009-08-23 17:38:50 +04:00
|
|
|
|
|
|
|
case WAITING_FOR_ACK:
|
2016-10-24 21:12:29 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
if (i2c->current_addr < 0) {
|
|
|
|
i2c->current_addr = i2c->buffer;
|
2023-01-11 11:50:16 +03:00
|
|
|
trace_bitbang_i2c_addr(i2c->current_addr);
|
2016-10-24 21:12:29 +03:00
|
|
|
ret = i2c_start_transfer(i2c->bus, i2c->current_addr >> 1,
|
|
|
|
i2c->current_addr & 1);
|
2009-11-21 02:37:15 +03:00
|
|
|
} else {
|
2023-01-11 11:50:16 +03:00
|
|
|
trace_bitbang_i2c_send(i2c->buffer);
|
2016-10-24 21:12:29 +03:00
|
|
|
ret = i2c_send(i2c->bus, i2c->buffer);
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
/* NACK (either addressing a nonexistent device, or the
|
|
|
|
* device we were sending to decided to NACK us).
|
|
|
|
*/
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, SENT_NACK);
|
2016-10-24 21:12:29 +03:00
|
|
|
bitbang_i2c_enter_stop(i2c);
|
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
2009-11-21 02:37:15 +03:00
|
|
|
}
|
|
|
|
if (i2c->current_addr & 1) {
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, RECEIVING_BIT7);
|
2009-11-21 02:37:15 +03:00
|
|
|
} else {
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, SENDING_BIT7);
|
2009-11-21 02:37:15 +03:00
|
|
|
}
|
|
|
|
return bitbang_i2c_ret(i2c, 0);
|
2016-10-24 21:12:29 +03:00
|
|
|
}
|
2009-11-21 02:37:15 +03:00
|
|
|
case RECEIVING_BIT7:
|
|
|
|
i2c->buffer = i2c_recv(i2c->bus);
|
2023-01-11 11:50:16 +03:00
|
|
|
trace_bitbang_i2c_recv(i2c->buffer);
|
2009-11-21 02:37:15 +03:00
|
|
|
/* Fall through... */
|
|
|
|
case RECEIVING_BIT6 ... RECEIVING_BIT0:
|
|
|
|
data = i2c->buffer >> 7;
|
|
|
|
/* will end up in SENDING_ACK */
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, i2c->state + 1);
|
2009-11-21 02:37:15 +03:00
|
|
|
i2c->buffer <<= 1;
|
|
|
|
return bitbang_i2c_ret(i2c, data);
|
2009-08-23 17:38:50 +04:00
|
|
|
|
|
|
|
case SENDING_ACK:
|
2009-11-21 02:37:15 +03:00
|
|
|
if (data != 0) {
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, SENT_NACK);
|
2009-11-21 02:37:15 +03:00
|
|
|
i2c_nack(i2c->bus);
|
|
|
|
} else {
|
2023-01-11 11:50:14 +03:00
|
|
|
bitbang_i2c_set_state(i2c, RECEIVING_BIT7);
|
2009-11-21 02:37:15 +03:00
|
|
|
}
|
|
|
|
return bitbang_i2c_ret(i2c, 1);
|
2009-08-23 17:38:50 +04:00
|
|
|
}
|
2009-11-21 02:37:15 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:38:44 +03:00
|
|
|
void bitbang_i2c_init(bitbang_i2c_interface *s, I2CBus *bus)
|
2009-11-21 02:37:15 +03:00
|
|
|
{
|
|
|
|
s->bus = bus;
|
|
|
|
s->last_data = 1;
|
|
|
|
s->last_clock = 1;
|
|
|
|
s->device_out = 1;
|
|
|
|
}
|
2009-08-23 17:38:50 +04:00
|
|
|
|
2009-11-21 02:37:15 +03:00
|
|
|
/* GPIO interface. */
|
2013-07-26 20:13:46 +04:00
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(GPIOI2CState, GPIO_I2C)
|
2013-07-26 20:13:46 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct GPIOI2CState {
|
2023-01-11 11:50:13 +03:00
|
|
|
/*< private >*/
|
2013-07-26 20:13:46 +04:00
|
|
|
SysBusDevice parent_obj;
|
2023-01-11 11:50:13 +03:00
|
|
|
/*< public >*/
|
2013-07-26 20:13:46 +04:00
|
|
|
|
2019-07-02 19:38:44 +03:00
|
|
|
bitbang_i2c_interface bitbang;
|
2009-11-21 02:37:15 +03:00
|
|
|
int last_level;
|
|
|
|
qemu_irq out;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2009-11-21 02:37:15 +03:00
|
|
|
|
|
|
|
static void bitbang_i2c_gpio_set(void *opaque, int irq, int level)
|
|
|
|
{
|
|
|
|
GPIOI2CState *s = opaque;
|
|
|
|
|
2019-07-02 19:38:44 +03:00
|
|
|
level = bitbang_i2c_set(&s->bitbang, irq, level);
|
2009-11-21 02:37:15 +03:00
|
|
|
if (level != s->last_level) {
|
|
|
|
s->last_level = level;
|
|
|
|
qemu_set_irq(s->out, level);
|
|
|
|
}
|
2009-08-23 17:38:50 +04:00
|
|
|
}
|
|
|
|
|
2016-06-14 17:59:13 +03:00
|
|
|
static void gpio_i2c_init(Object *obj)
|
2009-08-23 17:38:50 +04:00
|
|
|
{
|
2016-06-14 17:59:13 +03:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
GPIOI2CState *s = GPIO_I2C(obj);
|
2013-08-03 02:18:51 +04:00
|
|
|
I2CBus *bus;
|
2009-08-23 17:38:50 +04:00
|
|
|
|
2013-07-26 20:13:46 +04:00
|
|
|
bus = i2c_init_bus(dev, "i2c");
|
2019-07-02 19:38:44 +03:00
|
|
|
bitbang_i2c_init(&s->bitbang, bus);
|
2009-08-23 17:38:50 +04:00
|
|
|
|
2013-07-26 20:13:46 +04:00
|
|
|
qdev_init_gpio_in(dev, bitbang_i2c_gpio_set, 2);
|
|
|
|
qdev_init_gpio_out(dev, &s->out, 1);
|
2009-08-23 17:38:50 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void gpio_i2c_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->desc = "Virtual GPIO to I2C bridge";
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo gpio_i2c_info = {
|
2013-07-26 20:13:46 +04:00
|
|
|
.name = TYPE_GPIO_I2C,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(GPIOI2CState),
|
2016-06-14 17:59:13 +03:00
|
|
|
.instance_init = gpio_i2c_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = gpio_i2c_class_init,
|
2009-11-21 02:37:15 +03:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void bitbang_i2c_register_types(void)
|
2009-08-23 17:38:50 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&gpio_i2c_info);
|
2009-08-23 17:38:50 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(bitbang_i2c_register_types)
|