2012-04-20 19:38:52 +04:00
|
|
|
/*
|
2020-06-17 10:25:29 +03:00
|
|
|
* ARM SBCon two-wire serial bus interface (I2C bitbang)
|
|
|
|
* a.k.a. ARM Versatile I2C controller
|
2012-04-20 19:38:52 +04:00
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
|
|
|
* Copyright (c) 2012 Oskar Andero <oskar.andero@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is derived from hw/realview.c by Paul Brook
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-06-17 10:25:29 +03:00
|
|
|
#include "hw/i2c/arm_sbcon_i2c.h"
|
2020-06-17 10:25:27 +03:00
|
|
|
#include "hw/registerfields.h"
|
2015-12-15 15:16:16 +03:00
|
|
|
#include "qemu/log.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"
|
2012-04-20 19:38:52 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
typedef ArmSbconI2CState VersatileI2CState;
|
2020-09-01 00:07:33 +03:00
|
|
|
DECLARE_INSTANCE_CHECKER(VersatileI2CState, VERSATILE_I2C,
|
|
|
|
TYPE_VERSATILE_I2C)
|
2013-07-26 20:28:26 +04:00
|
|
|
|
|
|
|
|
2012-04-20 19:38:52 +04:00
|
|
|
|
2020-06-17 10:25:27 +03:00
|
|
|
REG32(CONTROL_GET, 0)
|
|
|
|
REG32(CONTROL_SET, 0)
|
|
|
|
REG32(CONTROL_CLR, 4)
|
|
|
|
|
2020-06-17 10:25:28 +03:00
|
|
|
#define SCL BIT(0)
|
|
|
|
#define SDA BIT(1)
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
|
2012-04-20 19:38:52 +04:00
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
VersatileI2CState *s = (VersatileI2CState *)opaque;
|
|
|
|
|
2020-06-17 10:25:27 +03:00
|
|
|
switch (offset) {
|
|
|
|
case A_CONTROL_SET:
|
2012-04-20 19:38:52 +04:00
|
|
|
return (s->out & 1) | (s->in << 1);
|
2020-06-17 10:25:27 +03:00
|
|
|
default:
|
2012-10-30 11:45:11 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Bad offset 0x%x\n", __func__, (int)offset);
|
2012-04-20 19:38:52 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void versatile_i2c_write(void *opaque, hwaddr offset,
|
2012-04-20 19:38:52 +04:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
|
|
|
VersatileI2CState *s = (VersatileI2CState *)opaque;
|
|
|
|
|
|
|
|
switch (offset) {
|
2020-06-17 10:25:27 +03:00
|
|
|
case A_CONTROL_SET:
|
2012-04-20 19:38:52 +04:00
|
|
|
s->out |= value & 3;
|
|
|
|
break;
|
2020-06-17 10:25:27 +03:00
|
|
|
case A_CONTROL_CLR:
|
2012-04-20 19:38:52 +04:00
|
|
|
s->out &= ~value;
|
|
|
|
break;
|
|
|
|
default:
|
2012-10-30 11:45:11 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Bad offset 0x%x\n", __func__, (int)offset);
|
2012-04-20 19:38:52 +04:00
|
|
|
}
|
2020-06-17 10:25:28 +03:00
|
|
|
bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SCL, (s->out & SCL) != 0);
|
|
|
|
s->in = bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SDA, (s->out & SDA) != 0);
|
2012-04-20 19:38:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps versatile_i2c_ops = {
|
|
|
|
.read = versatile_i2c_read,
|
|
|
|
.write = versatile_i2c_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2016-06-14 17:59:13 +03:00
|
|
|
static void versatile_i2c_init(Object *obj)
|
2012-04-20 19:38:52 +04:00
|
|
|
{
|
2016-06-14 17:59:13 +03:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
VersatileI2CState *s = VERSATILE_I2C(obj);
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
2013-08-03 02:18:51 +04:00
|
|
|
I2CBus *bus;
|
2012-04-20 19:38:52 +04:00
|
|
|
|
2013-07-26 20:28:26 +04:00
|
|
|
bus = i2c_init_bus(dev, "i2c");
|
2019-07-02 19:38:44 +03:00
|
|
|
bitbang_i2c_init(&s->bitbang, bus);
|
2016-06-14 17:59:13 +03:00
|
|
|
memory_region_init_io(&s->iomem, obj, &versatile_i2c_ops, s,
|
2020-06-17 10:25:29 +03:00
|
|
|
"arm_sbcon_i2c", 0x1000);
|
2013-07-26 20:28:26 +04:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
2012-04-20 19:38:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo versatile_i2c_info = {
|
2013-07-26 20:28:26 +04:00
|
|
|
.name = TYPE_VERSATILE_I2C,
|
2012-04-20 19:38:52 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(VersatileI2CState),
|
2016-06-14 17:59:13 +03:00
|
|
|
.instance_init = versatile_i2c_init,
|
2012-04-20 19:38:52 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void versatile_i2c_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&versatile_i2c_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(versatile_i2c_register_types)
|