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/qdev-properties.h"
24 #include "hw/arm/omap.h"
25 #include "hw/sysbus.h"
26 #include "qemu/error-report.h"
27 #include "qemu/module.h"
28 #include "qapi/error.h"
44 SysBusDevice parent_obj
;
49 struct omap_gpio_s omap1
;
52 /* General-Purpose I/O of OMAP1 */
53 static void omap_gpio_set(void *opaque
, int line
, int level
)
55 struct omap_gpio_s
*s
= &((struct omap_gpif_s
*) opaque
)->omap1
;
56 uint16_t prev
= s
->inputs
;
59 s
->inputs
|= 1 << line
;
61 s
->inputs
&= ~(1 << line
);
63 if (((s
->edge
& s
->inputs
& ~prev
) | (~s
->edge
& ~s
->inputs
& prev
)) &
64 (1 << line
) & s
->dir
& ~s
->mask
) {
66 qemu_irq_raise(s
->irq
);
70 static uint64_t omap_gpio_read(void *opaque
, hwaddr addr
,
73 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
74 int offset
= addr
& OMAP_MPUI_REG_MASK
;
77 return omap_badwidth_read16(opaque
, addr
);
81 case 0x00: /* DATA_INPUT */
82 return s
->inputs
& s
->pins
;
84 case 0x04: /* DATA_OUTPUT */
87 case 0x08: /* DIRECTION_CONTROL */
90 case 0x0c: /* INTERRUPT_CONTROL */
93 case 0x10: /* INTERRUPT_MASK */
96 case 0x14: /* INTERRUPT_STATUS */
99 case 0x18: /* PIN_CONTROL (not in OMAP310) */
108 static void omap_gpio_write(void *opaque
, hwaddr addr
,
109 uint64_t value
, unsigned size
)
111 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
112 int offset
= addr
& OMAP_MPUI_REG_MASK
;
117 omap_badwidth_write16(opaque
, addr
, value
);
122 case 0x00: /* DATA_INPUT */
126 case 0x04: /* DATA_OUTPUT */
127 diff
= (s
->outputs
^ value
) & ~s
->dir
;
129 while ((ln
= ctz32(diff
)) != 32) {
131 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
136 case 0x08: /* DIRECTION_CONTROL */
137 diff
= s
->outputs
& (s
->dir
^ value
);
140 value
= s
->outputs
& ~s
->dir
;
141 while ((ln
= ctz32(diff
)) != 32) {
143 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
148 case 0x0c: /* INTERRUPT_CONTROL */
152 case 0x10: /* INTERRUPT_MASK */
156 case 0x14: /* INTERRUPT_STATUS */
159 qemu_irq_lower(s
->irq
);
162 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
173 /* *Some* sources say the memory region is 32-bit. */
174 static const MemoryRegionOps omap_gpio_ops
= {
175 .read
= omap_gpio_read
,
176 .write
= omap_gpio_write
,
177 .endianness
= DEVICE_NATIVE_ENDIAN
,
180 static void omap_gpio_reset(struct omap_gpio_s
*s
)
191 struct omap2_gpio_s
{
211 struct omap2_gpif_s
{
212 SysBusDevice parent_obj
;
219 struct omap2_gpio_s
*modules
;
225 /* General-Purpose Interface of OMAP2/3 */
226 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s
*s
,
229 qemu_set_irq(s
->irq
[line
], s
->ints
[line
] & s
->mask
[line
]);
232 static void omap2_gpio_module_wake(struct omap2_gpio_s
*s
, int line
)
234 if (!(s
->config
[0] & (1 << 2))) /* ENAWAKEUP */
236 if (!(s
->config
[0] & (3 << 3))) /* Force Idle */
238 if (!(s
->wumask
& (1 << line
)))
241 qemu_irq_raise(s
->wkup
);
244 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s
*s
,
251 while ((ln
= ctz32(diff
)) != 32) {
252 qemu_set_irq(s
->handler
[ln
], (s
->outputs
>> ln
) & 1);
257 static void omap2_gpio_module_level_update(struct omap2_gpio_s
*s
, int line
)
259 s
->ints
[line
] |= s
->dir
&
260 ((s
->inputs
& s
->level
[1]) | (~s
->inputs
& s
->level
[0]));
261 omap2_gpio_module_int_update(s
, line
);
264 static inline void omap2_gpio_module_int(struct omap2_gpio_s
*s
, int line
)
266 s
->ints
[0] |= 1 << line
;
267 omap2_gpio_module_int_update(s
, 0);
268 s
->ints
[1] |= 1 << line
;
269 omap2_gpio_module_int_update(s
, 1);
270 omap2_gpio_module_wake(s
, line
);
273 static void omap2_gpio_set(void *opaque
, int line
, int level
)
275 struct omap2_gpif_s
*p
= opaque
;
276 struct omap2_gpio_s
*s
= &p
->modules
[line
>> 5];
280 if (s
->dir
& (1 << line
) & ((~s
->inputs
& s
->edge
[0]) | s
->level
[1]))
281 omap2_gpio_module_int(s
, line
);
282 s
->inputs
|= 1 << line
;
284 if (s
->dir
& (1 << line
) & ((s
->inputs
& s
->edge
[1]) | s
->level
[0]))
285 omap2_gpio_module_int(s
, line
);
286 s
->inputs
&= ~(1 << line
);
290 static void omap2_gpio_module_reset(struct omap2_gpio_s
*s
)
308 static uint32_t omap2_gpio_module_read(void *opaque
, hwaddr addr
)
310 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
313 case 0x00: /* GPIO_REVISION */
316 case 0x10: /* GPIO_SYSCONFIG */
319 case 0x14: /* GPIO_SYSSTATUS */
322 case 0x18: /* GPIO_IRQSTATUS1 */
325 case 0x1c: /* GPIO_IRQENABLE1 */
326 case 0x60: /* GPIO_CLEARIRQENABLE1 */
327 case 0x64: /* GPIO_SETIRQENABLE1 */
330 case 0x20: /* GPIO_WAKEUPENABLE */
331 case 0x80: /* GPIO_CLEARWKUENA */
332 case 0x84: /* GPIO_SETWKUENA */
335 case 0x28: /* GPIO_IRQSTATUS2 */
338 case 0x2c: /* GPIO_IRQENABLE2 */
339 case 0x70: /* GPIO_CLEARIRQENABLE2 */
340 case 0x74: /* GPIO_SETIREQNEABLE2 */
343 case 0x30: /* GPIO_CTRL */
346 case 0x34: /* GPIO_OE */
349 case 0x38: /* GPIO_DATAIN */
352 case 0x3c: /* GPIO_DATAOUT */
353 case 0x90: /* GPIO_CLEARDATAOUT */
354 case 0x94: /* GPIO_SETDATAOUT */
357 case 0x40: /* GPIO_LEVELDETECT0 */
360 case 0x44: /* GPIO_LEVELDETECT1 */
363 case 0x48: /* GPIO_RISINGDETECT */
366 case 0x4c: /* GPIO_FALLINGDETECT */
369 case 0x50: /* GPIO_DEBOUNCENABLE */
372 case 0x54: /* GPIO_DEBOUNCINGTIME */
380 static void omap2_gpio_module_write(void *opaque
, hwaddr addr
,
383 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
388 case 0x00: /* GPIO_REVISION */
389 case 0x14: /* GPIO_SYSSTATUS */
390 case 0x38: /* GPIO_DATAIN */
394 case 0x10: /* GPIO_SYSCONFIG */
395 if (((value
>> 3) & 3) == 3)
396 fprintf(stderr
, "%s: bad IDLEMODE value\n", __func__
);
398 omap2_gpio_module_reset(s
);
399 s
->config
[0] = value
& 0x1d;
402 case 0x18: /* GPIO_IRQSTATUS1 */
403 if (s
->ints
[0] & value
) {
404 s
->ints
[0] &= ~value
;
405 omap2_gpio_module_level_update(s
, 0);
409 case 0x1c: /* GPIO_IRQENABLE1 */
411 omap2_gpio_module_int_update(s
, 0);
414 case 0x20: /* GPIO_WAKEUPENABLE */
418 case 0x28: /* GPIO_IRQSTATUS2 */
419 if (s
->ints
[1] & value
) {
420 s
->ints
[1] &= ~value
;
421 omap2_gpio_module_level_update(s
, 1);
425 case 0x2c: /* GPIO_IRQENABLE2 */
427 omap2_gpio_module_int_update(s
, 1);
430 case 0x30: /* GPIO_CTRL */
431 s
->config
[1] = value
& 7;
434 case 0x34: /* GPIO_OE */
435 diff
= s
->outputs
& (s
->dir
^ value
);
438 value
= s
->outputs
& ~s
->dir
;
439 while ((ln
= ctz32(diff
)) != 32) {
441 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
444 omap2_gpio_module_level_update(s
, 0);
445 omap2_gpio_module_level_update(s
, 1);
448 case 0x3c: /* GPIO_DATAOUT */
449 omap2_gpio_module_out_update(s
, s
->outputs
^ value
);
452 case 0x40: /* GPIO_LEVELDETECT0 */
454 omap2_gpio_module_level_update(s
, 0);
455 omap2_gpio_module_level_update(s
, 1);
458 case 0x44: /* GPIO_LEVELDETECT1 */
460 omap2_gpio_module_level_update(s
, 0);
461 omap2_gpio_module_level_update(s
, 1);
464 case 0x48: /* GPIO_RISINGDETECT */
468 case 0x4c: /* GPIO_FALLINGDETECT */
472 case 0x50: /* GPIO_DEBOUNCENABLE */
476 case 0x54: /* GPIO_DEBOUNCINGTIME */
480 case 0x60: /* GPIO_CLEARIRQENABLE1 */
481 s
->mask
[0] &= ~value
;
482 omap2_gpio_module_int_update(s
, 0);
485 case 0x64: /* GPIO_SETIRQENABLE1 */
487 omap2_gpio_module_int_update(s
, 0);
490 case 0x70: /* GPIO_CLEARIRQENABLE2 */
491 s
->mask
[1] &= ~value
;
492 omap2_gpio_module_int_update(s
, 1);
495 case 0x74: /* GPIO_SETIREQNEABLE2 */
497 omap2_gpio_module_int_update(s
, 1);
500 case 0x80: /* GPIO_CLEARWKUENA */
504 case 0x84: /* GPIO_SETWKUENA */
508 case 0x90: /* GPIO_CLEARDATAOUT */
509 omap2_gpio_module_out_update(s
, s
->outputs
& value
);
512 case 0x94: /* GPIO_SETDATAOUT */
513 omap2_gpio_module_out_update(s
, ~s
->outputs
& value
);
522 static uint64_t omap2_gpio_module_readp(void *opaque
, hwaddr addr
,
525 return omap2_gpio_module_read(opaque
, addr
& ~3) >> ((addr
& 3) << 3);
528 static void omap2_gpio_module_writep(void *opaque
, hwaddr addr
,
529 uint64_t value
, unsigned size
)
532 uint32_t mask
= 0xffff;
535 omap2_gpio_module_write(opaque
, addr
, value
);
540 case 0x00: /* GPIO_REVISION */
541 case 0x14: /* GPIO_SYSSTATUS */
542 case 0x38: /* GPIO_DATAIN */
546 case 0x10: /* GPIO_SYSCONFIG */
547 case 0x1c: /* GPIO_IRQENABLE1 */
548 case 0x20: /* GPIO_WAKEUPENABLE */
549 case 0x2c: /* GPIO_IRQENABLE2 */
550 case 0x30: /* GPIO_CTRL */
551 case 0x34: /* GPIO_OE */
552 case 0x3c: /* GPIO_DATAOUT */
553 case 0x40: /* GPIO_LEVELDETECT0 */
554 case 0x44: /* GPIO_LEVELDETECT1 */
555 case 0x48: /* GPIO_RISINGDETECT */
556 case 0x4c: /* GPIO_FALLINGDETECT */
557 case 0x50: /* GPIO_DEBOUNCENABLE */
558 case 0x54: /* GPIO_DEBOUNCINGTIME */
559 cur
= omap2_gpio_module_read(opaque
, addr
& ~3) &
560 ~(mask
<< ((addr
& 3) << 3));
563 case 0x18: /* GPIO_IRQSTATUS1 */
564 case 0x28: /* GPIO_IRQSTATUS2 */
565 case 0x60: /* GPIO_CLEARIRQENABLE1 */
566 case 0x64: /* GPIO_SETIRQENABLE1 */
567 case 0x70: /* GPIO_CLEARIRQENABLE2 */
568 case 0x74: /* GPIO_SETIREQNEABLE2 */
569 case 0x80: /* GPIO_CLEARWKUENA */
570 case 0x84: /* GPIO_SETWKUENA */
571 case 0x90: /* GPIO_CLEARDATAOUT */
572 case 0x94: /* GPIO_SETDATAOUT */
573 value
<<= (addr
& 3) << 3;
574 omap2_gpio_module_write(opaque
, addr
, cur
| value
);
583 static const MemoryRegionOps omap2_gpio_module_ops
= {
584 .read
= omap2_gpio_module_readp
,
585 .write
= omap2_gpio_module_writep
,
586 .valid
.min_access_size
= 1,
587 .valid
.max_access_size
= 4,
588 .endianness
= DEVICE_NATIVE_ENDIAN
,
591 static void omap_gpif_reset(DeviceState
*dev
)
593 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
595 omap_gpio_reset(&s
->omap1
);
598 static void omap2_gpif_reset(DeviceState
*dev
)
600 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
603 for (i
= 0; i
< s
->modulecount
; i
++) {
604 omap2_gpio_module_reset(&s
->modules
[i
]);
610 static uint64_t omap2_gpif_top_read(void *opaque
, hwaddr addr
,
613 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
616 case 0x00: /* IPGENERICOCPSPL_REVISION */
619 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
622 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
625 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
628 case 0x40: /* IPGENERICOCPSPL_GPO */
631 case 0x50: /* IPGENERICOCPSPL_GPI */
639 static void omap2_gpif_top_write(void *opaque
, hwaddr addr
,
640 uint64_t value
, unsigned size
)
642 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
645 case 0x00: /* IPGENERICOCPSPL_REVISION */
646 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
647 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
648 case 0x50: /* IPGENERICOCPSPL_GPI */
652 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
653 if (value
& (1 << 1)) /* SOFTRESET */
654 omap2_gpif_reset(DEVICE(s
));
655 s
->autoidle
= value
& 1;
658 case 0x40: /* IPGENERICOCPSPL_GPO */
668 static const MemoryRegionOps omap2_gpif_top_ops
= {
669 .read
= omap2_gpif_top_read
,
670 .write
= omap2_gpif_top_write
,
671 .endianness
= DEVICE_NATIVE_ENDIAN
,
674 static void omap_gpio_init(Object
*obj
)
676 DeviceState
*dev
= DEVICE(obj
);
677 struct omap_gpif_s
*s
= OMAP1_GPIO(obj
);
678 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
680 qdev_init_gpio_in(dev
, omap_gpio_set
, 16);
681 qdev_init_gpio_out(dev
, s
->omap1
.handler
, 16);
682 sysbus_init_irq(sbd
, &s
->omap1
.irq
);
683 memory_region_init_io(&s
->iomem
, obj
, &omap_gpio_ops
, &s
->omap1
,
684 "omap.gpio", 0x1000);
685 sysbus_init_mmio(sbd
, &s
->iomem
);
688 static void omap_gpio_realize(DeviceState
*dev
, Error
**errp
)
690 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
693 error_setg(errp
, "omap-gpio: clk not connected");
697 static void omap2_gpio_realize(DeviceState
*dev
, Error
**errp
)
699 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
700 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
704 error_setg(errp
, "omap2-gpio: iclk not connected");
708 s
->modulecount
= s
->mpu_model
< omap2430
? 4
709 : s
->mpu_model
< omap3430
? 5
712 if (s
->mpu_model
< omap3430
) {
713 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap2_gpif_top_ops
, s
,
714 "omap2.gpio", 0x1000);
715 sysbus_init_mmio(sbd
, &s
->iomem
);
718 s
->modules
= g_new0(struct omap2_gpio_s
, s
->modulecount
);
719 s
->handler
= g_new0(qemu_irq
, s
->modulecount
* 32);
720 qdev_init_gpio_in(dev
, omap2_gpio_set
, s
->modulecount
* 32);
721 qdev_init_gpio_out(dev
, s
->handler
, s
->modulecount
* 32);
723 for (i
= 0; i
< s
->modulecount
; i
++) {
724 struct omap2_gpio_s
*m
= &s
->modules
[i
];
727 error_setg(errp
, "omap2-gpio: fclk%d not connected", i
);
731 m
->revision
= (s
->mpu_model
< omap3430
) ? 0x18 : 0x25;
732 m
->handler
= &s
->handler
[i
* 32];
733 sysbus_init_irq(sbd
, &m
->irq
[0]); /* mpu irq */
734 sysbus_init_irq(sbd
, &m
->irq
[1]); /* dsp irq */
735 sysbus_init_irq(sbd
, &m
->wkup
);
736 memory_region_init_io(&m
->iomem
, OBJECT(dev
), &omap2_gpio_module_ops
, m
,
737 "omap.gpio-module", 0x1000);
738 sysbus_init_mmio(sbd
, &m
->iomem
);
742 void omap_gpio_set_clk(omap_gpif
*gpio
, omap_clk clk
)
747 static Property omap_gpio_properties
[] = {
748 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s
, mpu_model
, 0),
749 DEFINE_PROP_END_OF_LIST(),
752 static void omap_gpio_class_init(ObjectClass
*klass
, void *data
)
754 DeviceClass
*dc
= DEVICE_CLASS(klass
);
756 dc
->realize
= omap_gpio_realize
;
757 dc
->reset
= omap_gpif_reset
;
758 device_class_set_props(dc
, omap_gpio_properties
);
759 /* Reason: pointer property "clk" */
760 dc
->user_creatable
= false;
763 static const TypeInfo omap_gpio_info
= {
764 .name
= TYPE_OMAP1_GPIO
,
765 .parent
= TYPE_SYS_BUS_DEVICE
,
766 .instance_size
= sizeof(struct omap_gpif_s
),
767 .instance_init
= omap_gpio_init
,
768 .class_init
= omap_gpio_class_init
,
771 void omap2_gpio_set_iclk(omap2_gpif
*gpio
, omap_clk clk
)
776 void omap2_gpio_set_fclk(omap2_gpif
*gpio
, uint8_t i
, omap_clk clk
)
782 static Property omap2_gpio_properties
[] = {
783 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s
, mpu_model
, 0),
784 DEFINE_PROP_END_OF_LIST(),
787 static void omap2_gpio_class_init(ObjectClass
*klass
, void *data
)
789 DeviceClass
*dc
= DEVICE_CLASS(klass
);
791 dc
->realize
= omap2_gpio_realize
;
792 dc
->reset
= omap2_gpif_reset
;
793 device_class_set_props(dc
, omap2_gpio_properties
);
794 /* Reason: pointer properties "iclk", "fclk0", ..., "fclk5" */
795 dc
->user_creatable
= false;
798 static const TypeInfo omap2_gpio_info
= {
799 .name
= TYPE_OMAP2_GPIO
,
800 .parent
= TYPE_SYS_BUS_DEVICE
,
801 .instance_size
= sizeof(struct omap2_gpif_s
),
802 .class_init
= omap2_gpio_class_init
,
805 static void omap_gpio_register_types(void)
807 type_register_static(&omap_gpio_info
);
808 type_register_static(&omap2_gpio_info
);
811 type_init(omap_gpio_register_types
)