Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / gpio / omap_gpio.c
bloba47a2167a670419c4038401ed38079e7641980f3
1 /*
2 * TI OMAP processors GPIO emulation.
4 * Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
5 * Copyright (C) 2007-2009 Nokia Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/log.h"
23 #include "hw/irq.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/arm/omap.h"
26 #include "hw/sysbus.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
29 #include "qapi/error.h"
31 struct omap_gpio_s {
32 qemu_irq irq;
33 qemu_irq handler[16];
35 uint16_t inputs;
36 uint16_t outputs;
37 uint16_t dir;
38 uint16_t edge;
39 uint16_t mask;
40 uint16_t ints;
41 uint16_t pins;
44 struct Omap1GpioState {
45 SysBusDevice parent_obj;
47 MemoryRegion iomem;
48 int mpu_model;
49 void *clk;
50 struct omap_gpio_s omap1;
53 /* General-Purpose I/O of OMAP1 */
54 static void omap_gpio_set(void *opaque, int line, int level)
56 Omap1GpioState *p = opaque;
57 struct omap_gpio_s *s = &p->omap1;
58 uint16_t prev = s->inputs;
60 if (level)
61 s->inputs |= 1 << line;
62 else
63 s->inputs &= ~(1 << line);
65 if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
66 (1 << line) & s->dir & ~s->mask) {
67 s->ints |= 1 << line;
68 qemu_irq_raise(s->irq);
72 static uint64_t omap_gpio_read(void *opaque, hwaddr addr,
73 unsigned size)
75 struct omap_gpio_s *s = opaque;
76 int offset = addr & OMAP_MPUI_REG_MASK;
78 if (size != 2) {
79 return omap_badwidth_read16(opaque, addr);
82 switch (offset) {
83 case 0x00: /* DATA_INPUT */
84 return s->inputs & s->pins;
86 case 0x04: /* DATA_OUTPUT */
87 return s->outputs;
89 case 0x08: /* DIRECTION_CONTROL */
90 return s->dir;
92 case 0x0c: /* INTERRUPT_CONTROL */
93 return s->edge;
95 case 0x10: /* INTERRUPT_MASK */
96 return s->mask;
98 case 0x14: /* INTERRUPT_STATUS */
99 return s->ints;
101 case 0x18: /* PIN_CONTROL (not in OMAP310) */
102 OMAP_BAD_REG(addr);
103 return s->pins;
106 OMAP_BAD_REG(addr);
107 return 0;
110 static void omap_gpio_write(void *opaque, hwaddr addr,
111 uint64_t value, unsigned size)
113 struct omap_gpio_s *s = opaque;
114 int offset = addr & OMAP_MPUI_REG_MASK;
115 uint16_t diff;
116 int ln;
118 if (size != 2) {
119 omap_badwidth_write16(opaque, addr, value);
120 return;
123 switch (offset) {
124 case 0x00: /* DATA_INPUT */
125 OMAP_RO_REG(addr);
126 return;
128 case 0x04: /* DATA_OUTPUT */
129 diff = (s->outputs ^ value) & ~s->dir;
130 s->outputs = value;
131 while ((ln = ctz32(diff)) != 32) {
132 if (s->handler[ln])
133 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
134 diff &= ~(1 << ln);
136 break;
138 case 0x08: /* DIRECTION_CONTROL */
139 diff = s->outputs & (s->dir ^ value);
140 s->dir = value;
142 value = s->outputs & ~s->dir;
143 while ((ln = ctz32(diff)) != 32) {
144 if (s->handler[ln])
145 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
146 diff &= ~(1 << ln);
148 break;
150 case 0x0c: /* INTERRUPT_CONTROL */
151 s->edge = value;
152 break;
154 case 0x10: /* INTERRUPT_MASK */
155 s->mask = value;
156 break;
158 case 0x14: /* INTERRUPT_STATUS */
159 s->ints &= ~value;
160 if (!s->ints)
161 qemu_irq_lower(s->irq);
162 break;
164 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
165 OMAP_BAD_REG(addr);
166 s->pins = value;
167 break;
169 default:
170 OMAP_BAD_REG(addr);
171 return;
175 /* *Some* sources say the memory region is 32-bit. */
176 static const MemoryRegionOps omap_gpio_ops = {
177 .read = omap_gpio_read,
178 .write = omap_gpio_write,
179 .endianness = DEVICE_NATIVE_ENDIAN,
182 static void omap_gpio_reset(struct omap_gpio_s *s)
184 s->inputs = 0;
185 s->outputs = ~0;
186 s->dir = ~0;
187 s->edge = ~0;
188 s->mask = ~0;
189 s->ints = 0;
190 s->pins = ~0;
193 static void omap_gpif_reset(DeviceState *dev)
195 Omap1GpioState *s = OMAP1_GPIO(dev);
197 omap_gpio_reset(&s->omap1);
200 static void omap_gpio_init(Object *obj)
202 DeviceState *dev = DEVICE(obj);
203 Omap1GpioState *s = OMAP1_GPIO(obj);
204 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
206 qdev_init_gpio_in(dev, omap_gpio_set, 16);
207 qdev_init_gpio_out(dev, s->omap1.handler, 16);
208 sysbus_init_irq(sbd, &s->omap1.irq);
209 memory_region_init_io(&s->iomem, obj, &omap_gpio_ops, &s->omap1,
210 "omap.gpio", 0x1000);
211 sysbus_init_mmio(sbd, &s->iomem);
214 static void omap_gpio_realize(DeviceState *dev, Error **errp)
216 Omap1GpioState *s = OMAP1_GPIO(dev);
218 if (!s->clk) {
219 error_setg(errp, "omap-gpio: clk not connected");
223 void omap_gpio_set_clk(Omap1GpioState *gpio, omap_clk clk)
225 gpio->clk = clk;
228 static Property omap_gpio_properties[] = {
229 DEFINE_PROP_INT32("mpu_model", Omap1GpioState, mpu_model, 0),
230 DEFINE_PROP_END_OF_LIST(),
233 static void omap_gpio_class_init(ObjectClass *klass, void *data)
235 DeviceClass *dc = DEVICE_CLASS(klass);
237 dc->realize = omap_gpio_realize;
238 device_class_set_legacy_reset(dc, omap_gpif_reset);
239 device_class_set_props(dc, omap_gpio_properties);
240 /* Reason: pointer property "clk" */
241 dc->user_creatable = false;
244 static const TypeInfo omap_gpio_info = {
245 .name = TYPE_OMAP1_GPIO,
246 .parent = TYPE_SYS_BUS_DEVICE,
247 .instance_size = sizeof(Omap1GpioState),
248 .instance_init = omap_gpio_init,
249 .class_init = omap_gpio_class_init,
252 static void omap_gpio_register_types(void)
254 type_register_static(&omap_gpio_info);
257 type_init(omap_gpio_register_types)