2010-05-31 19:54:13 +04:00
|
|
|
/*
|
|
|
|
* TI OMAP processors GPIO emulation.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
|
|
|
|
* Copyright (C) 2007-2009 Nokia Corporation
|
|
|
|
*
|
|
|
|
* 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 or
|
|
|
|
* (at your option) version 3 of the License.
|
|
|
|
*
|
|
|
|
* 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:28 +03:00
|
|
|
#include "qemu/osdep.h"
|
2022-02-07 11:27:56 +03:00
|
|
|
#include "qemu/log.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/arm/omap.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2015-12-17 19:35:10 +03:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2016-06-14 17:59:13 +03:00
|
|
|
#include "qapi/error.h"
|
2011-07-29 19:35:17 +04:00
|
|
|
|
2010-05-31 19:54:13 +04:00
|
|
|
struct omap_gpio_s {
|
|
|
|
qemu_irq irq;
|
|
|
|
qemu_irq handler[16];
|
|
|
|
|
|
|
|
uint16_t inputs;
|
|
|
|
uint16_t outputs;
|
|
|
|
uint16_t dir;
|
|
|
|
uint16_t edge;
|
|
|
|
uint16_t mask;
|
|
|
|
uint16_t ints;
|
|
|
|
uint16_t pins;
|
|
|
|
};
|
|
|
|
|
2023-01-09 17:02:56 +03:00
|
|
|
struct Omap1GpioState {
|
2013-07-26 19:14:40 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-11-24 16:32:34 +04:00
|
|
|
MemoryRegion iomem;
|
2011-07-29 19:35:17 +04:00
|
|
|
int mpu_model;
|
|
|
|
void *clk;
|
|
|
|
struct omap_gpio_s omap1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* General-Purpose I/O of OMAP1 */
|
2010-05-31 19:54:13 +04:00
|
|
|
static void omap_gpio_set(void *opaque, int line, int level)
|
|
|
|
{
|
2023-01-09 17:02:56 +03:00
|
|
|
Omap1GpioState *p = opaque;
|
2023-01-09 17:02:54 +03:00
|
|
|
struct omap_gpio_s *s = &p->omap1;
|
2010-05-31 19:54:13 +04:00
|
|
|
uint16_t prev = s->inputs;
|
|
|
|
|
|
|
|
if (level)
|
|
|
|
s->inputs |= 1 << line;
|
|
|
|
else
|
|
|
|
s->inputs &= ~(1 << line);
|
|
|
|
|
|
|
|
if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
|
|
|
|
(1 << line) & s->dir & ~s->mask) {
|
|
|
|
s->ints |= 1 << line;
|
|
|
|
qemu_irq_raise(s->irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t omap_gpio_read(void *opaque, hwaddr addr,
|
2011-11-24 16:32:34 +04:00
|
|
|
unsigned size)
|
2010-05-31 19:54:13 +04:00
|
|
|
{
|
2023-01-09 17:02:55 +03:00
|
|
|
struct omap_gpio_s *s = opaque;
|
2010-05-31 19:54:13 +04:00
|
|
|
int offset = addr & OMAP_MPUI_REG_MASK;
|
|
|
|
|
2011-11-24 16:32:34 +04:00
|
|
|
if (size != 2) {
|
|
|
|
return omap_badwidth_read16(opaque, addr);
|
|
|
|
}
|
|
|
|
|
2010-05-31 19:54:13 +04:00
|
|
|
switch (offset) {
|
|
|
|
case 0x00: /* DATA_INPUT */
|
|
|
|
return s->inputs & s->pins;
|
|
|
|
|
|
|
|
case 0x04: /* DATA_OUTPUT */
|
|
|
|
return s->outputs;
|
|
|
|
|
|
|
|
case 0x08: /* DIRECTION_CONTROL */
|
|
|
|
return s->dir;
|
|
|
|
|
|
|
|
case 0x0c: /* INTERRUPT_CONTROL */
|
|
|
|
return s->edge;
|
|
|
|
|
|
|
|
case 0x10: /* INTERRUPT_MASK */
|
|
|
|
return s->mask;
|
|
|
|
|
|
|
|
case 0x14: /* INTERRUPT_STATUS */
|
|
|
|
return s->ints;
|
|
|
|
|
|
|
|
case 0x18: /* PIN_CONTROL (not in OMAP310) */
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return s->pins;
|
|
|
|
}
|
|
|
|
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void omap_gpio_write(void *opaque, hwaddr addr,
|
2011-11-24 16:32:34 +04:00
|
|
|
uint64_t value, unsigned size)
|
2010-05-31 19:54:13 +04:00
|
|
|
{
|
2023-01-09 17:02:55 +03:00
|
|
|
struct omap_gpio_s *s = opaque;
|
2010-05-31 19:54:13 +04:00
|
|
|
int offset = addr & OMAP_MPUI_REG_MASK;
|
|
|
|
uint16_t diff;
|
|
|
|
int ln;
|
|
|
|
|
2011-11-24 16:32:34 +04:00
|
|
|
if (size != 2) {
|
2015-03-08 21:21:13 +03:00
|
|
|
omap_badwidth_write16(opaque, addr, value);
|
|
|
|
return;
|
2011-11-24 16:32:34 +04:00
|
|
|
}
|
|
|
|
|
2010-05-31 19:54:13 +04:00
|
|
|
switch (offset) {
|
|
|
|
case 0x00: /* DATA_INPUT */
|
|
|
|
OMAP_RO_REG(addr);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 0x04: /* DATA_OUTPUT */
|
|
|
|
diff = (s->outputs ^ value) & ~s->dir;
|
|
|
|
s->outputs = value;
|
2015-03-23 18:29:27 +03:00
|
|
|
while ((ln = ctz32(diff)) != 32) {
|
2010-05-31 19:54:13 +04:00
|
|
|
if (s->handler[ln])
|
|
|
|
qemu_set_irq(s->handler[ln], (value >> ln) & 1);
|
|
|
|
diff &= ~(1 << ln);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x08: /* DIRECTION_CONTROL */
|
|
|
|
diff = s->outputs & (s->dir ^ value);
|
|
|
|
s->dir = value;
|
|
|
|
|
|
|
|
value = s->outputs & ~s->dir;
|
2015-03-23 18:29:27 +03:00
|
|
|
while ((ln = ctz32(diff)) != 32) {
|
2010-05-31 19:54:13 +04:00
|
|
|
if (s->handler[ln])
|
|
|
|
qemu_set_irq(s->handler[ln], (value >> ln) & 1);
|
|
|
|
diff &= ~(1 << ln);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0c: /* INTERRUPT_CONTROL */
|
|
|
|
s->edge = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x10: /* INTERRUPT_MASK */
|
|
|
|
s->mask = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x14: /* INTERRUPT_STATUS */
|
|
|
|
s->ints &= ~value;
|
|
|
|
if (!s->ints)
|
|
|
|
qemu_irq_lower(s->irq);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
s->pins = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
OMAP_BAD_REG(addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *Some* sources say the memory region is 32-bit. */
|
2011-11-24 16:32:34 +04:00
|
|
|
static const MemoryRegionOps omap_gpio_ops = {
|
|
|
|
.read = omap_gpio_read,
|
|
|
|
.write = omap_gpio_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2010-05-31 19:54:13 +04:00
|
|
|
};
|
|
|
|
|
2011-07-29 19:35:17 +04:00
|
|
|
static void omap_gpio_reset(struct omap_gpio_s *s)
|
2010-05-31 19:54:13 +04:00
|
|
|
{
|
|
|
|
s->inputs = 0;
|
|
|
|
s->outputs = ~0;
|
|
|
|
s->dir = ~0;
|
|
|
|
s->edge = ~0;
|
|
|
|
s->mask = ~0;
|
|
|
|
s->ints = 0;
|
|
|
|
s->pins = ~0;
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:35:17 +04:00
|
|
|
static void omap_gpif_reset(DeviceState *dev)
|
2010-05-31 19:54:14 +04:00
|
|
|
{
|
2023-01-09 17:02:56 +03:00
|
|
|
Omap1GpioState *s = OMAP1_GPIO(dev);
|
2013-07-26 19:14:40 +04:00
|
|
|
|
2011-07-29 19:35:17 +04:00
|
|
|
omap_gpio_reset(&s->omap1);
|
2010-05-31 19:54:14 +04:00
|
|
|
}
|
|
|
|
|
2016-06-14 17:59:13 +03:00
|
|
|
static void omap_gpio_init(Object *obj)
|
2010-05-31 19:54:14 +04:00
|
|
|
{
|
2016-06-14 17:59:13 +03:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
2023-01-09 17:02:56 +03:00
|
|
|
Omap1GpioState *s = OMAP1_GPIO(obj);
|
2016-06-14 17:59:13 +03:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
2013-07-26 19:14:40 +04:00
|
|
|
|
|
|
|
qdev_init_gpio_in(dev, omap_gpio_set, 16);
|
|
|
|
qdev_init_gpio_out(dev, s->omap1.handler, 16);
|
|
|
|
sysbus_init_irq(sbd, &s->omap1.irq);
|
2016-06-14 17:59:13 +03:00
|
|
|
memory_region_init_io(&s->iomem, obj, &omap_gpio_ops, &s->omap1,
|
2011-11-24 16:32:34 +04:00
|
|
|
"omap.gpio", 0x1000);
|
2013-07-26 19:14:40 +04:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
2011-07-29 19:35:17 +04:00
|
|
|
}
|
2010-05-31 19:54:14 +04:00
|
|
|
|
2016-06-14 17:59:13 +03:00
|
|
|
static void omap_gpio_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
2023-01-09 17:02:56 +03:00
|
|
|
Omap1GpioState *s = OMAP1_GPIO(dev);
|
2016-06-14 17:59:13 +03:00
|
|
|
|
|
|
|
if (!s->clk) {
|
|
|
|
error_setg(errp, "omap-gpio: clk not connected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:02:56 +03:00
|
|
|
void omap_gpio_set_clk(Omap1GpioState *gpio, omap_clk clk)
|
2019-10-18 16:26:19 +03:00
|
|
|
{
|
|
|
|
gpio->clk = clk;
|
|
|
|
}
|
2010-05-31 19:54:14 +04:00
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property omap_gpio_properties[] = {
|
2023-01-09 17:02:56 +03:00
|
|
|
DEFINE_PROP_INT32("mpu_model", Omap1GpioState, mpu_model, 0),
|
2012-01-24 23:12:29 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
2011-07-29 19:35:17 +04:00
|
|
|
};
|
2010-05-31 19:54:14 +04:00
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void omap_gpio_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
|
|
|
|
2016-06-14 17:59:13 +03:00
|
|
|
dc->realize = omap_gpio_realize;
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, omap_gpif_reset);
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, omap_gpio_properties);
|
2013-11-29 13:43:44 +04:00
|
|
|
/* Reason: pointer property "clk" */
|
2017-05-03 23:35:44 +03:00
|
|
|
dc->user_creatable = false;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo omap_gpio_info = {
|
2013-07-26 19:14:40 +04:00
|
|
|
.name = TYPE_OMAP1_GPIO,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2023-01-09 17:02:56 +03:00
|
|
|
.instance_size = sizeof(Omap1GpioState),
|
2016-06-14 17:59:13 +03:00
|
|
|
.instance_init = omap_gpio_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = omap_gpio_class_init,
|
2012-01-24 23:12:29 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void omap_gpio_register_types(void)
|
2010-05-31 19:54:14 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&omap_gpio_info);
|
2010-05-31 19:54:14 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(omap_gpio_register_types)
|