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