2 * ARM Versatile I2C controller
4 * Copyright (c) 2006-2007 CodeSourcery.
7 * This file is derived from hw/realview.c by Paul Brook
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include "hw/sysbus.h"
26 #include "hw/i2c/bitbang_i2c.h"
27 #include "hw/registerfields.h"
29 #include "qemu/module.h"
31 #define TYPE_VERSATILE_I2C "versatile_i2c"
32 #define VERSATILE_I2C(obj) \
33 OBJECT_CHECK(VersatileI2CState, (obj), TYPE_VERSATILE_I2C)
35 typedef struct VersatileI2CState {
36 SysBusDevice parent_obj;
39 bitbang_i2c_interface bitbang;
51 static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
54 VersatileI2CState *s = (VersatileI2CState *)opaque;
58 return (s->out & 1) | (s->in << 1);
60 qemu_log_mask(LOG_GUEST_ERROR,
61 "%s: Bad offset 0x%x\n", __func__, (int)offset);
66 static void versatile_i2c_write(void *opaque, hwaddr offset,
67 uint64_t value, unsigned size)
69 VersatileI2CState *s = (VersatileI2CState *)opaque;
79 qemu_log_mask(LOG_GUEST_ERROR,
80 "%s: Bad offset 0x%x\n", __func__, (int)offset);
82 bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SCL, (s->out & SCL) != 0);
83 s->in = bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SDA, (s->out & SDA) != 0);
86 static const MemoryRegionOps versatile_i2c_ops = {
87 .read = versatile_i2c_read,
88 .write = versatile_i2c_write,
89 .endianness = DEVICE_NATIVE_ENDIAN,
92 static void versatile_i2c_init(Object *obj)
94 DeviceState *dev = DEVICE(obj);
95 VersatileI2CState *s = VERSATILE_I2C(obj);
96 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
99 bus = i2c_init_bus(dev, "i2c");
100 bitbang_i2c_init(&s->bitbang, bus);
101 memory_region_init_io(&s->iomem, obj, &versatile_i2c_ops, s,
102 "versatile_i2c", 0x1000);
103 sysbus_init_mmio(sbd, &s->iomem);
106 static const TypeInfo versatile_i2c_info = {
107 .name = TYPE_VERSATILE_I2C,
108 .parent = TYPE_SYS_BUS_DEVICE,
109 .instance_size = sizeof(VersatileI2CState),
110 .instance_init = versatile_i2c_init,
113 static void versatile_i2c_register_types(void)
115 type_register_static(&versatile_i2c_info);
118 type_init(versatile_i2c_register_types)