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"
23 #include "hw/arm/omap.h"
24 #include "hw/sysbus.h"
25 #include "qemu/error-report.h"
26 #include "qemu/module.h"
27 #include "qapi/error.h"
42 #define TYPE_OMAP1_GPIO "omap-gpio"
43 #define OMAP1_GPIO(obj) \
44 OBJECT_CHECK(struct omap_gpif_s, (obj), TYPE_OMAP1_GPIO)
47 SysBusDevice parent_obj
;
52 struct omap_gpio_s omap1
;
55 /* General-Purpose I/O of OMAP1 */
56 static void omap_gpio_set(void *opaque
, int line
, int level
)
58 struct omap_gpio_s
*s
= &((struct omap_gpif_s
*) opaque
)->omap1
;
59 uint16_t prev
= s
->inputs
;
62 s
->inputs
|= 1 << line
;
64 s
->inputs
&= ~(1 << line
);
66 if (((s
->edge
& s
->inputs
& ~prev
) | (~s
->edge
& ~s
->inputs
& prev
)) &
67 (1 << line
) & s
->dir
& ~s
->mask
) {
69 qemu_irq_raise(s
->irq
);
73 static uint64_t omap_gpio_read(void *opaque
, hwaddr addr
,
76 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
77 int offset
= addr
& OMAP_MPUI_REG_MASK
;
80 return omap_badwidth_read16(opaque
, addr
);
84 case 0x00: /* DATA_INPUT */
85 return s
->inputs
& s
->pins
;
87 case 0x04: /* DATA_OUTPUT */
90 case 0x08: /* DIRECTION_CONTROL */
93 case 0x0c: /* INTERRUPT_CONTROL */
96 case 0x10: /* INTERRUPT_MASK */
99 case 0x14: /* INTERRUPT_STATUS */
102 case 0x18: /* PIN_CONTROL (not in OMAP310) */
111 static void omap_gpio_write(void *opaque
, hwaddr addr
,
112 uint64_t value
, unsigned size
)
114 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
115 int offset
= addr
& OMAP_MPUI_REG_MASK
;
120 omap_badwidth_write16(opaque
, addr
, value
);
125 case 0x00: /* DATA_INPUT */
129 case 0x04: /* DATA_OUTPUT */
130 diff
= (s
->outputs
^ value
) & ~s
->dir
;
132 while ((ln
= ctz32(diff
)) != 32) {
134 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
139 case 0x08: /* DIRECTION_CONTROL */
140 diff
= s
->outputs
& (s
->dir
^ value
);
143 value
= s
->outputs
& ~s
->dir
;
144 while ((ln
= ctz32(diff
)) != 32) {
146 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
151 case 0x0c: /* INTERRUPT_CONTROL */
155 case 0x10: /* INTERRUPT_MASK */
159 case 0x14: /* INTERRUPT_STATUS */
162 qemu_irq_lower(s
->irq
);
165 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
176 /* *Some* sources say the memory region is 32-bit. */
177 static const MemoryRegionOps omap_gpio_ops
= {
178 .read
= omap_gpio_read
,
179 .write
= omap_gpio_write
,
180 .endianness
= DEVICE_NATIVE_ENDIAN
,
183 static void omap_gpio_reset(struct omap_gpio_s
*s
)
194 struct omap2_gpio_s
{
214 #define TYPE_OMAP2_GPIO "omap2-gpio"
215 #define OMAP2_GPIO(obj) \
216 OBJECT_CHECK(struct omap2_gpif_s, (obj), TYPE_OMAP2_GPIO)
218 struct omap2_gpif_s
{
219 SysBusDevice parent_obj
;
226 struct omap2_gpio_s
*modules
;
232 /* General-Purpose Interface of OMAP2/3 */
233 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s
*s
,
236 qemu_set_irq(s
->irq
[line
], s
->ints
[line
] & s
->mask
[line
]);
239 static void omap2_gpio_module_wake(struct omap2_gpio_s
*s
, int line
)
241 if (!(s
->config
[0] & (1 << 2))) /* ENAWAKEUP */
243 if (!(s
->config
[0] & (3 << 3))) /* Force Idle */
245 if (!(s
->wumask
& (1 << line
)))
248 qemu_irq_raise(s
->wkup
);
251 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s
*s
,
258 while ((ln
= ctz32(diff
)) != 32) {
259 qemu_set_irq(s
->handler
[ln
], (s
->outputs
>> ln
) & 1);
264 static void omap2_gpio_module_level_update(struct omap2_gpio_s
*s
, int line
)
266 s
->ints
[line
] |= s
->dir
&
267 ((s
->inputs
& s
->level
[1]) | (~s
->inputs
& s
->level
[0]));
268 omap2_gpio_module_int_update(s
, line
);
271 static inline void omap2_gpio_module_int(struct omap2_gpio_s
*s
, int line
)
273 s
->ints
[0] |= 1 << line
;
274 omap2_gpio_module_int_update(s
, 0);
275 s
->ints
[1] |= 1 << line
;
276 omap2_gpio_module_int_update(s
, 1);
277 omap2_gpio_module_wake(s
, line
);
280 static void omap2_gpio_set(void *opaque
, int line
, int level
)
282 struct omap2_gpif_s
*p
= opaque
;
283 struct omap2_gpio_s
*s
= &p
->modules
[line
>> 5];
287 if (s
->dir
& (1 << line
) & ((~s
->inputs
& s
->edge
[0]) | s
->level
[1]))
288 omap2_gpio_module_int(s
, line
);
289 s
->inputs
|= 1 << line
;
291 if (s
->dir
& (1 << line
) & ((s
->inputs
& s
->edge
[1]) | s
->level
[0]))
292 omap2_gpio_module_int(s
, line
);
293 s
->inputs
&= ~(1 << line
);
297 static void omap2_gpio_module_reset(struct omap2_gpio_s
*s
)
315 static uint32_t omap2_gpio_module_read(void *opaque
, hwaddr addr
)
317 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
320 case 0x00: /* GPIO_REVISION */
323 case 0x10: /* GPIO_SYSCONFIG */
326 case 0x14: /* GPIO_SYSSTATUS */
329 case 0x18: /* GPIO_IRQSTATUS1 */
332 case 0x1c: /* GPIO_IRQENABLE1 */
333 case 0x60: /* GPIO_CLEARIRQENABLE1 */
334 case 0x64: /* GPIO_SETIRQENABLE1 */
337 case 0x20: /* GPIO_WAKEUPENABLE */
338 case 0x80: /* GPIO_CLEARWKUENA */
339 case 0x84: /* GPIO_SETWKUENA */
342 case 0x28: /* GPIO_IRQSTATUS2 */
345 case 0x2c: /* GPIO_IRQENABLE2 */
346 case 0x70: /* GPIO_CLEARIRQENABLE2 */
347 case 0x74: /* GPIO_SETIREQNEABLE2 */
350 case 0x30: /* GPIO_CTRL */
353 case 0x34: /* GPIO_OE */
356 case 0x38: /* GPIO_DATAIN */
359 case 0x3c: /* GPIO_DATAOUT */
360 case 0x90: /* GPIO_CLEARDATAOUT */
361 case 0x94: /* GPIO_SETDATAOUT */
364 case 0x40: /* GPIO_LEVELDETECT0 */
367 case 0x44: /* GPIO_LEVELDETECT1 */
370 case 0x48: /* GPIO_RISINGDETECT */
373 case 0x4c: /* GPIO_FALLINGDETECT */
376 case 0x50: /* GPIO_DEBOUNCENABLE */
379 case 0x54: /* GPIO_DEBOUNCINGTIME */
387 static void omap2_gpio_module_write(void *opaque
, hwaddr addr
,
390 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
395 case 0x00: /* GPIO_REVISION */
396 case 0x14: /* GPIO_SYSSTATUS */
397 case 0x38: /* GPIO_DATAIN */
401 case 0x10: /* GPIO_SYSCONFIG */
402 if (((value
>> 3) & 3) == 3)
403 fprintf(stderr
, "%s: bad IDLEMODE value\n", __func__
);
405 omap2_gpio_module_reset(s
);
406 s
->config
[0] = value
& 0x1d;
409 case 0x18: /* GPIO_IRQSTATUS1 */
410 if (s
->ints
[0] & value
) {
411 s
->ints
[0] &= ~value
;
412 omap2_gpio_module_level_update(s
, 0);
416 case 0x1c: /* GPIO_IRQENABLE1 */
418 omap2_gpio_module_int_update(s
, 0);
421 case 0x20: /* GPIO_WAKEUPENABLE */
425 case 0x28: /* GPIO_IRQSTATUS2 */
426 if (s
->ints
[1] & value
) {
427 s
->ints
[1] &= ~value
;
428 omap2_gpio_module_level_update(s
, 1);
432 case 0x2c: /* GPIO_IRQENABLE2 */
434 omap2_gpio_module_int_update(s
, 1);
437 case 0x30: /* GPIO_CTRL */
438 s
->config
[1] = value
& 7;
441 case 0x34: /* GPIO_OE */
442 diff
= s
->outputs
& (s
->dir
^ value
);
445 value
= s
->outputs
& ~s
->dir
;
446 while ((ln
= ctz32(diff
)) != 32) {
448 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
451 omap2_gpio_module_level_update(s
, 0);
452 omap2_gpio_module_level_update(s
, 1);
455 case 0x3c: /* GPIO_DATAOUT */
456 omap2_gpio_module_out_update(s
, s
->outputs
^ value
);
459 case 0x40: /* GPIO_LEVELDETECT0 */
461 omap2_gpio_module_level_update(s
, 0);
462 omap2_gpio_module_level_update(s
, 1);
465 case 0x44: /* GPIO_LEVELDETECT1 */
467 omap2_gpio_module_level_update(s
, 0);
468 omap2_gpio_module_level_update(s
, 1);
471 case 0x48: /* GPIO_RISINGDETECT */
475 case 0x4c: /* GPIO_FALLINGDETECT */
479 case 0x50: /* GPIO_DEBOUNCENABLE */
483 case 0x54: /* GPIO_DEBOUNCINGTIME */
487 case 0x60: /* GPIO_CLEARIRQENABLE1 */
488 s
->mask
[0] &= ~value
;
489 omap2_gpio_module_int_update(s
, 0);
492 case 0x64: /* GPIO_SETIRQENABLE1 */
494 omap2_gpio_module_int_update(s
, 0);
497 case 0x70: /* GPIO_CLEARIRQENABLE2 */
498 s
->mask
[1] &= ~value
;
499 omap2_gpio_module_int_update(s
, 1);
502 case 0x74: /* GPIO_SETIREQNEABLE2 */
504 omap2_gpio_module_int_update(s
, 1);
507 case 0x80: /* GPIO_CLEARWKUENA */
511 case 0x84: /* GPIO_SETWKUENA */
515 case 0x90: /* GPIO_CLEARDATAOUT */
516 omap2_gpio_module_out_update(s
, s
->outputs
& value
);
519 case 0x94: /* GPIO_SETDATAOUT */
520 omap2_gpio_module_out_update(s
, ~s
->outputs
& value
);
529 static uint64_t omap2_gpio_module_readp(void *opaque
, hwaddr addr
,
532 return omap2_gpio_module_read(opaque
, addr
& ~3) >> ((addr
& 3) << 3);
535 static void omap2_gpio_module_writep(void *opaque
, hwaddr addr
,
536 uint64_t value
, unsigned size
)
539 uint32_t mask
= 0xffff;
542 omap2_gpio_module_write(opaque
, addr
, value
);
547 case 0x00: /* GPIO_REVISION */
548 case 0x14: /* GPIO_SYSSTATUS */
549 case 0x38: /* GPIO_DATAIN */
553 case 0x10: /* GPIO_SYSCONFIG */
554 case 0x1c: /* GPIO_IRQENABLE1 */
555 case 0x20: /* GPIO_WAKEUPENABLE */
556 case 0x2c: /* GPIO_IRQENABLE2 */
557 case 0x30: /* GPIO_CTRL */
558 case 0x34: /* GPIO_OE */
559 case 0x3c: /* GPIO_DATAOUT */
560 case 0x40: /* GPIO_LEVELDETECT0 */
561 case 0x44: /* GPIO_LEVELDETECT1 */
562 case 0x48: /* GPIO_RISINGDETECT */
563 case 0x4c: /* GPIO_FALLINGDETECT */
564 case 0x50: /* GPIO_DEBOUNCENABLE */
565 case 0x54: /* GPIO_DEBOUNCINGTIME */
566 cur
= omap2_gpio_module_read(opaque
, addr
& ~3) &
567 ~(mask
<< ((addr
& 3) << 3));
570 case 0x18: /* GPIO_IRQSTATUS1 */
571 case 0x28: /* GPIO_IRQSTATUS2 */
572 case 0x60: /* GPIO_CLEARIRQENABLE1 */
573 case 0x64: /* GPIO_SETIRQENABLE1 */
574 case 0x70: /* GPIO_CLEARIRQENABLE2 */
575 case 0x74: /* GPIO_SETIREQNEABLE2 */
576 case 0x80: /* GPIO_CLEARWKUENA */
577 case 0x84: /* GPIO_SETWKUENA */
578 case 0x90: /* GPIO_CLEARDATAOUT */
579 case 0x94: /* GPIO_SETDATAOUT */
580 value
<<= (addr
& 3) << 3;
581 omap2_gpio_module_write(opaque
, addr
, cur
| value
);
590 static const MemoryRegionOps omap2_gpio_module_ops
= {
591 .read
= omap2_gpio_module_readp
,
592 .write
= omap2_gpio_module_writep
,
593 .valid
.min_access_size
= 1,
594 .valid
.max_access_size
= 4,
595 .endianness
= DEVICE_NATIVE_ENDIAN
,
598 static void omap_gpif_reset(DeviceState
*dev
)
600 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
602 omap_gpio_reset(&s
->omap1
);
605 static void omap2_gpif_reset(DeviceState
*dev
)
607 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
610 for (i
= 0; i
< s
->modulecount
; i
++) {
611 omap2_gpio_module_reset(&s
->modules
[i
]);
617 static uint64_t omap2_gpif_top_read(void *opaque
, hwaddr addr
,
620 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
623 case 0x00: /* IPGENERICOCPSPL_REVISION */
626 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
629 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
632 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
635 case 0x40: /* IPGENERICOCPSPL_GPO */
638 case 0x50: /* IPGENERICOCPSPL_GPI */
646 static void omap2_gpif_top_write(void *opaque
, hwaddr addr
,
647 uint64_t value
, unsigned size
)
649 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
652 case 0x00: /* IPGENERICOCPSPL_REVISION */
653 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
654 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
655 case 0x50: /* IPGENERICOCPSPL_GPI */
659 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
660 if (value
& (1 << 1)) /* SOFTRESET */
661 omap2_gpif_reset(DEVICE(s
));
662 s
->autoidle
= value
& 1;
665 case 0x40: /* IPGENERICOCPSPL_GPO */
675 static const MemoryRegionOps omap2_gpif_top_ops
= {
676 .read
= omap2_gpif_top_read
,
677 .write
= omap2_gpif_top_write
,
678 .endianness
= DEVICE_NATIVE_ENDIAN
,
681 static void omap_gpio_init(Object
*obj
)
683 DeviceState
*dev
= DEVICE(obj
);
684 struct omap_gpif_s
*s
= OMAP1_GPIO(obj
);
685 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
687 qdev_init_gpio_in(dev
, omap_gpio_set
, 16);
688 qdev_init_gpio_out(dev
, s
->omap1
.handler
, 16);
689 sysbus_init_irq(sbd
, &s
->omap1
.irq
);
690 memory_region_init_io(&s
->iomem
, obj
, &omap_gpio_ops
, &s
->omap1
,
691 "omap.gpio", 0x1000);
692 sysbus_init_mmio(sbd
, &s
->iomem
);
695 static void omap_gpio_realize(DeviceState
*dev
, Error
**errp
)
697 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
700 error_setg(errp
, "omap-gpio: clk not connected");
704 static void omap2_gpio_realize(DeviceState
*dev
, Error
**errp
)
706 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
707 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
711 error_setg(errp
, "omap2-gpio: iclk not connected");
715 s
->modulecount
= s
->mpu_model
< omap2430
? 4
716 : s
->mpu_model
< omap3430
? 5
719 if (s
->mpu_model
< omap3430
) {
720 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap2_gpif_top_ops
, s
,
721 "omap2.gpio", 0x1000);
722 sysbus_init_mmio(sbd
, &s
->iomem
);
725 s
->modules
= g_new0(struct omap2_gpio_s
, s
->modulecount
);
726 s
->handler
= g_new0(qemu_irq
, s
->modulecount
* 32);
727 qdev_init_gpio_in(dev
, omap2_gpio_set
, s
->modulecount
* 32);
728 qdev_init_gpio_out(dev
, s
->handler
, s
->modulecount
* 32);
730 for (i
= 0; i
< s
->modulecount
; i
++) {
731 struct omap2_gpio_s
*m
= &s
->modules
[i
];
734 error_setg(errp
, "omap2-gpio: fclk%d not connected", i
);
738 m
->revision
= (s
->mpu_model
< omap3430
) ? 0x18 : 0x25;
739 m
->handler
= &s
->handler
[i
* 32];
740 sysbus_init_irq(sbd
, &m
->irq
[0]); /* mpu irq */
741 sysbus_init_irq(sbd
, &m
->irq
[1]); /* dsp irq */
742 sysbus_init_irq(sbd
, &m
->wkup
);
743 memory_region_init_io(&m
->iomem
, OBJECT(dev
), &omap2_gpio_module_ops
, m
,
744 "omap.gpio-module", 0x1000);
745 sysbus_init_mmio(sbd
, &m
->iomem
);
749 /* Using qdev pointer properties for the clocks is not ideal.
750 * qdev should support a generic means of defining a 'port' with
751 * an arbitrary interface for connecting two devices. Then we
752 * could reframe the omap clock API in terms of clock ports,
753 * and get some type safety. For now the best qdev provides is
754 * passing an arbitrary pointer.
755 * (It's not possible to pass in the string which is the clock
756 * name, because this device does not have the necessary information
757 * (ie the struct omap_mpu_state_s*) to do the clockname to pointer
761 static Property omap_gpio_properties
[] = {
762 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s
, mpu_model
, 0),
763 DEFINE_PROP_PTR("clk", struct omap_gpif_s
, clk
),
764 DEFINE_PROP_END_OF_LIST(),
767 static void omap_gpio_class_init(ObjectClass
*klass
, void *data
)
769 DeviceClass
*dc
= DEVICE_CLASS(klass
);
771 dc
->realize
= omap_gpio_realize
;
772 dc
->reset
= omap_gpif_reset
;
773 dc
->props
= omap_gpio_properties
;
774 /* Reason: pointer property "clk" */
775 dc
->user_creatable
= false;
778 static const TypeInfo omap_gpio_info
= {
779 .name
= TYPE_OMAP1_GPIO
,
780 .parent
= TYPE_SYS_BUS_DEVICE
,
781 .instance_size
= sizeof(struct omap_gpif_s
),
782 .instance_init
= omap_gpio_init
,
783 .class_init
= omap_gpio_class_init
,
786 static Property omap2_gpio_properties
[] = {
787 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s
, mpu_model
, 0),
788 DEFINE_PROP_PTR("iclk", struct omap2_gpif_s
, iclk
),
789 DEFINE_PROP_PTR("fclk0", struct omap2_gpif_s
, fclk
[0]),
790 DEFINE_PROP_PTR("fclk1", struct omap2_gpif_s
, fclk
[1]),
791 DEFINE_PROP_PTR("fclk2", struct omap2_gpif_s
, fclk
[2]),
792 DEFINE_PROP_PTR("fclk3", struct omap2_gpif_s
, fclk
[3]),
793 DEFINE_PROP_PTR("fclk4", struct omap2_gpif_s
, fclk
[4]),
794 DEFINE_PROP_PTR("fclk5", struct omap2_gpif_s
, fclk
[5]),
795 DEFINE_PROP_END_OF_LIST(),
798 static void omap2_gpio_class_init(ObjectClass
*klass
, void *data
)
800 DeviceClass
*dc
= DEVICE_CLASS(klass
);
802 dc
->realize
= omap2_gpio_realize
;
803 dc
->reset
= omap2_gpif_reset
;
804 dc
->props
= omap2_gpio_properties
;
805 /* Reason: pointer properties "iclk", "fclk0", ..., "fclk5" */
806 dc
->user_creatable
= false;
809 static const TypeInfo omap2_gpio_info
= {
810 .name
= TYPE_OMAP2_GPIO
,
811 .parent
= TYPE_SYS_BUS_DEVICE
,
812 .instance_size
= sizeof(struct omap2_gpif_s
),
813 .class_init
= omap2_gpio_class_init
,
816 static void omap_gpio_register_types(void)
818 type_register_static(&omap_gpio_info
);
819 type_register_static(&omap2_gpio_info
);
822 type_init(omap_gpio_register_types
)