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"
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"
44 struct Omap1GpioState
{
45 SysBusDevice parent_obj
;
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
;
61 s
->inputs
|= 1 << line
;
63 s
->inputs
&= ~(1 << line
);
65 if (((s
->edge
& s
->inputs
& ~prev
) | (~s
->edge
& ~s
->inputs
& prev
)) &
66 (1 << line
) & s
->dir
& ~s
->mask
) {
68 qemu_irq_raise(s
->irq
);
72 static uint64_t omap_gpio_read(void *opaque
, hwaddr addr
,
75 struct omap_gpio_s
*s
= opaque
;
76 int offset
= addr
& OMAP_MPUI_REG_MASK
;
79 return omap_badwidth_read16(opaque
, addr
);
83 case 0x00: /* DATA_INPUT */
84 return s
->inputs
& s
->pins
;
86 case 0x04: /* DATA_OUTPUT */
89 case 0x08: /* DIRECTION_CONTROL */
92 case 0x0c: /* INTERRUPT_CONTROL */
95 case 0x10: /* INTERRUPT_MASK */
98 case 0x14: /* INTERRUPT_STATUS */
101 case 0x18: /* PIN_CONTROL (not in OMAP310) */
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
;
119 omap_badwidth_write16(opaque
, addr
, value
);
124 case 0x00: /* DATA_INPUT */
128 case 0x04: /* DATA_OUTPUT */
129 diff
= (s
->outputs
^ value
) & ~s
->dir
;
131 while ((ln
= ctz32(diff
)) != 32) {
133 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
138 case 0x08: /* DIRECTION_CONTROL */
139 diff
= s
->outputs
& (s
->dir
^ value
);
142 value
= s
->outputs
& ~s
->dir
;
143 while ((ln
= ctz32(diff
)) != 32) {
145 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
150 case 0x0c: /* INTERRUPT_CONTROL */
154 case 0x10: /* INTERRUPT_MASK */
158 case 0x14: /* INTERRUPT_STATUS */
161 qemu_irq_lower(s
->irq
);
164 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
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
)
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
);
219 error_setg(errp
, "omap-gpio: clk not connected");
223 void omap_gpio_set_clk(Omap1GpioState
*gpio
, omap_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
)