1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "hw/sysbus.h"
25#include "bitbang_i2c.h"
26
27#define TYPE_VERSATILE_I2C "versatile_i2c"
28#define VERSATILE_I2C(obj) \
29 OBJECT_CHECK(VersatileI2CState, (obj), TYPE_VERSATILE_I2C)
30
31typedef struct VersatileI2CState {
32 SysBusDevice parent_obj;
33
34 MemoryRegion iomem;
35 bitbang_i2c_interface *bitbang;
36 int out;
37 int in;
38} VersatileI2CState;
39
40static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
41 unsigned size)
42{
43 VersatileI2CState *s = (VersatileI2CState *)opaque;
44
45 if (offset == 0) {
46 return (s->out & 1) | (s->in << 1);
47 } else {
48 qemu_log_mask(LOG_GUEST_ERROR,
49 "%s: Bad offset 0x%x\n", __func__, (int)offset);
50 return -1;
51 }
52}
53
54static void versatile_i2c_write(void *opaque, hwaddr offset,
55 uint64_t value, unsigned size)
56{
57 VersatileI2CState *s = (VersatileI2CState *)opaque;
58
59 switch (offset) {
60 case 0:
61 s->out |= value & 3;
62 break;
63 case 4:
64 s->out &= ~value;
65 break;
66 default:
67 qemu_log_mask(LOG_GUEST_ERROR,
68 "%s: Bad offset 0x%x\n", __func__, (int)offset);
69 }
70 bitbang_i2c_set(s->bitbang, BITBANG_I2C_SCL, (s->out & 1) != 0);
71 s->in = bitbang_i2c_set(s->bitbang, BITBANG_I2C_SDA, (s->out & 2) != 0);
72}
73
74static const MemoryRegionOps versatile_i2c_ops = {
75 .read = versatile_i2c_read,
76 .write = versatile_i2c_write,
77 .endianness = DEVICE_NATIVE_ENDIAN,
78};
79
80static int versatile_i2c_init(SysBusDevice *sbd)
81{
82 DeviceState *dev = DEVICE(sbd);
83 VersatileI2CState *s = VERSATILE_I2C(dev);
84 I2CBus *bus;
85
86 bus = i2c_init_bus(dev, "i2c");
87 s->bitbang = bitbang_i2c_init(bus);
88 memory_region_init_io(&s->iomem, OBJECT(s), &versatile_i2c_ops, s,
89 "versatile_i2c", 0x1000);
90 sysbus_init_mmio(sbd, &s->iomem);
91 return 0;
92}
93
94static void versatile_i2c_class_init(ObjectClass *klass, void *data)
95{
96 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
97
98 k->init = versatile_i2c_init;
99}
100
101static const TypeInfo versatile_i2c_info = {
102 .name = TYPE_VERSATILE_I2C,
103 .parent = TYPE_SYS_BUS_DEVICE,
104 .instance_size = sizeof(VersatileI2CState),
105 .class_init = versatile_i2c_class_init,
106};
107
108static void versatile_i2c_register_types(void)
109{
110 type_register_static(&versatile_i2c_info);
111}
112
113type_init(versatile_i2c_register_types)
114