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"
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 struct omap_gpio_s
*s
= &((struct omap_gpif_s
*) opaque
)->omap1
;
57 uint16_t prev
= s
->inputs
;
60 s
->inputs
|= 1 << line
;
62 s
->inputs
&= ~(1 << line
);
64 if (((s
->edge
& s
->inputs
& ~prev
) | (~s
->edge
& ~s
->inputs
& prev
)) &
65 (1 << line
) & s
->dir
& ~s
->mask
) {
67 qemu_irq_raise(s
->irq
);
71 static uint64_t omap_gpio_read(void *opaque
, hwaddr addr
,
74 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
75 int offset
= addr
& OMAP_MPUI_REG_MASK
;
78 return omap_badwidth_read16(opaque
, addr
);
82 case 0x00: /* DATA_INPUT */
83 return s
->inputs
& s
->pins
;
85 case 0x04: /* DATA_OUTPUT */
88 case 0x08: /* DIRECTION_CONTROL */
91 case 0x0c: /* INTERRUPT_CONTROL */
94 case 0x10: /* INTERRUPT_MASK */
97 case 0x14: /* INTERRUPT_STATUS */
100 case 0x18: /* PIN_CONTROL (not in OMAP310) */
109 static void omap_gpio_write(void *opaque
, hwaddr addr
,
110 uint64_t value
, unsigned size
)
112 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
113 int offset
= addr
& OMAP_MPUI_REG_MASK
;
118 omap_badwidth_write16(opaque
, addr
, value
);
123 case 0x00: /* DATA_INPUT */
127 case 0x04: /* DATA_OUTPUT */
128 diff
= (s
->outputs
^ value
) & ~s
->dir
;
130 while ((ln
= ctz32(diff
)) != 32) {
132 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
137 case 0x08: /* DIRECTION_CONTROL */
138 diff
= s
->outputs
& (s
->dir
^ value
);
141 value
= s
->outputs
& ~s
->dir
;
142 while ((ln
= ctz32(diff
)) != 32) {
144 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
149 case 0x0c: /* INTERRUPT_CONTROL */
153 case 0x10: /* INTERRUPT_MASK */
157 case 0x14: /* INTERRUPT_STATUS */
160 qemu_irq_lower(s
->irq
);
163 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
174 /* *Some* sources say the memory region is 32-bit. */
175 static const MemoryRegionOps omap_gpio_ops
= {
176 .read
= omap_gpio_read
,
177 .write
= omap_gpio_write
,
178 .endianness
= DEVICE_NATIVE_ENDIAN
,
181 static void omap_gpio_reset(struct omap_gpio_s
*s
)
192 struct omap2_gpio_s
{
212 struct omap2_gpif_s
{
213 SysBusDevice parent_obj
;
220 struct omap2_gpio_s
*modules
;
226 /* General-Purpose Interface of OMAP2/3 */
227 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s
*s
,
230 qemu_set_irq(s
->irq
[line
], s
->ints
[line
] & s
->mask
[line
]);
233 static void omap2_gpio_module_wake(struct omap2_gpio_s
*s
, int line
)
235 if (!(s
->config
[0] & (1 << 2))) /* ENAWAKEUP */
237 if (!(s
->config
[0] & (3 << 3))) /* Force Idle */
239 if (!(s
->wumask
& (1 << line
)))
242 qemu_irq_raise(s
->wkup
);
245 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s
*s
,
252 while ((ln
= ctz32(diff
)) != 32) {
253 qemu_set_irq(s
->handler
[ln
], (s
->outputs
>> ln
) & 1);
258 static void omap2_gpio_module_level_update(struct omap2_gpio_s
*s
, int line
)
260 s
->ints
[line
] |= s
->dir
&
261 ((s
->inputs
& s
->level
[1]) | (~s
->inputs
& s
->level
[0]));
262 omap2_gpio_module_int_update(s
, line
);
265 static inline void omap2_gpio_module_int(struct omap2_gpio_s
*s
, int line
)
267 s
->ints
[0] |= 1 << line
;
268 omap2_gpio_module_int_update(s
, 0);
269 s
->ints
[1] |= 1 << line
;
270 omap2_gpio_module_int_update(s
, 1);
271 omap2_gpio_module_wake(s
, line
);
274 static void omap2_gpio_set(void *opaque
, int line
, int level
)
276 struct omap2_gpif_s
*p
= opaque
;
277 struct omap2_gpio_s
*s
= &p
->modules
[line
>> 5];
281 if (s
->dir
& (1 << line
) & ((~s
->inputs
& s
->edge
[0]) | s
->level
[1]))
282 omap2_gpio_module_int(s
, line
);
283 s
->inputs
|= 1 << line
;
285 if (s
->dir
& (1 << line
) & ((s
->inputs
& s
->edge
[1]) | s
->level
[0]))
286 omap2_gpio_module_int(s
, line
);
287 s
->inputs
&= ~(1 << line
);
291 static void omap2_gpio_module_reset(struct omap2_gpio_s
*s
)
309 static uint32_t omap2_gpio_module_read(void *opaque
, hwaddr addr
)
311 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
314 case 0x00: /* GPIO_REVISION */
317 case 0x10: /* GPIO_SYSCONFIG */
320 case 0x14: /* GPIO_SYSSTATUS */
323 case 0x18: /* GPIO_IRQSTATUS1 */
326 case 0x1c: /* GPIO_IRQENABLE1 */
327 case 0x60: /* GPIO_CLEARIRQENABLE1 */
328 case 0x64: /* GPIO_SETIRQENABLE1 */
331 case 0x20: /* GPIO_WAKEUPENABLE */
332 case 0x80: /* GPIO_CLEARWKUENA */
333 case 0x84: /* GPIO_SETWKUENA */
336 case 0x28: /* GPIO_IRQSTATUS2 */
339 case 0x2c: /* GPIO_IRQENABLE2 */
340 case 0x70: /* GPIO_CLEARIRQENABLE2 */
341 case 0x74: /* GPIO_SETIREQNEABLE2 */
344 case 0x30: /* GPIO_CTRL */
347 case 0x34: /* GPIO_OE */
350 case 0x38: /* GPIO_DATAIN */
353 case 0x3c: /* GPIO_DATAOUT */
354 case 0x90: /* GPIO_CLEARDATAOUT */
355 case 0x94: /* GPIO_SETDATAOUT */
358 case 0x40: /* GPIO_LEVELDETECT0 */
361 case 0x44: /* GPIO_LEVELDETECT1 */
364 case 0x48: /* GPIO_RISINGDETECT */
367 case 0x4c: /* GPIO_FALLINGDETECT */
370 case 0x50: /* GPIO_DEBOUNCENABLE */
373 case 0x54: /* GPIO_DEBOUNCINGTIME */
381 static void omap2_gpio_module_write(void *opaque
, hwaddr addr
,
384 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
389 case 0x00: /* GPIO_REVISION */
390 case 0x14: /* GPIO_SYSSTATUS */
391 case 0x38: /* GPIO_DATAIN */
395 case 0x10: /* GPIO_SYSCONFIG */
396 if (((value
>> 3) & 3) == 3) {
397 qemu_log_mask(LOG_GUEST_ERROR
,
398 "%s: Illegal IDLEMODE value: 3\n", __func__
);
401 omap2_gpio_module_reset(s
);
402 s
->config
[0] = value
& 0x1d;
405 case 0x18: /* GPIO_IRQSTATUS1 */
406 if (s
->ints
[0] & value
) {
407 s
->ints
[0] &= ~value
;
408 omap2_gpio_module_level_update(s
, 0);
412 case 0x1c: /* GPIO_IRQENABLE1 */
414 omap2_gpio_module_int_update(s
, 0);
417 case 0x20: /* GPIO_WAKEUPENABLE */
421 case 0x28: /* GPIO_IRQSTATUS2 */
422 if (s
->ints
[1] & value
) {
423 s
->ints
[1] &= ~value
;
424 omap2_gpio_module_level_update(s
, 1);
428 case 0x2c: /* GPIO_IRQENABLE2 */
430 omap2_gpio_module_int_update(s
, 1);
433 case 0x30: /* GPIO_CTRL */
434 s
->config
[1] = value
& 7;
437 case 0x34: /* GPIO_OE */
438 diff
= s
->outputs
& (s
->dir
^ value
);
441 value
= s
->outputs
& ~s
->dir
;
442 while ((ln
= ctz32(diff
)) != 32) {
444 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
447 omap2_gpio_module_level_update(s
, 0);
448 omap2_gpio_module_level_update(s
, 1);
451 case 0x3c: /* GPIO_DATAOUT */
452 omap2_gpio_module_out_update(s
, s
->outputs
^ value
);
455 case 0x40: /* GPIO_LEVELDETECT0 */
457 omap2_gpio_module_level_update(s
, 0);
458 omap2_gpio_module_level_update(s
, 1);
461 case 0x44: /* GPIO_LEVELDETECT1 */
463 omap2_gpio_module_level_update(s
, 0);
464 omap2_gpio_module_level_update(s
, 1);
467 case 0x48: /* GPIO_RISINGDETECT */
471 case 0x4c: /* GPIO_FALLINGDETECT */
475 case 0x50: /* GPIO_DEBOUNCENABLE */
479 case 0x54: /* GPIO_DEBOUNCINGTIME */
483 case 0x60: /* GPIO_CLEARIRQENABLE1 */
484 s
->mask
[0] &= ~value
;
485 omap2_gpio_module_int_update(s
, 0);
488 case 0x64: /* GPIO_SETIRQENABLE1 */
490 omap2_gpio_module_int_update(s
, 0);
493 case 0x70: /* GPIO_CLEARIRQENABLE2 */
494 s
->mask
[1] &= ~value
;
495 omap2_gpio_module_int_update(s
, 1);
498 case 0x74: /* GPIO_SETIREQNEABLE2 */
500 omap2_gpio_module_int_update(s
, 1);
503 case 0x80: /* GPIO_CLEARWKUENA */
507 case 0x84: /* GPIO_SETWKUENA */
511 case 0x90: /* GPIO_CLEARDATAOUT */
512 omap2_gpio_module_out_update(s
, s
->outputs
& value
);
515 case 0x94: /* GPIO_SETDATAOUT */
516 omap2_gpio_module_out_update(s
, ~s
->outputs
& value
);
525 static uint64_t omap2_gpio_module_readp(void *opaque
, hwaddr addr
,
528 return omap2_gpio_module_read(opaque
, addr
& ~3) >> ((addr
& 3) << 3);
531 static void omap2_gpio_module_writep(void *opaque
, hwaddr addr
,
532 uint64_t value
, unsigned size
)
535 uint32_t mask
= 0xffff;
538 omap2_gpio_module_write(opaque
, addr
, value
);
543 case 0x00: /* GPIO_REVISION */
544 case 0x14: /* GPIO_SYSSTATUS */
545 case 0x38: /* GPIO_DATAIN */
549 case 0x10: /* GPIO_SYSCONFIG */
550 case 0x1c: /* GPIO_IRQENABLE1 */
551 case 0x20: /* GPIO_WAKEUPENABLE */
552 case 0x2c: /* GPIO_IRQENABLE2 */
553 case 0x30: /* GPIO_CTRL */
554 case 0x34: /* GPIO_OE */
555 case 0x3c: /* GPIO_DATAOUT */
556 case 0x40: /* GPIO_LEVELDETECT0 */
557 case 0x44: /* GPIO_LEVELDETECT1 */
558 case 0x48: /* GPIO_RISINGDETECT */
559 case 0x4c: /* GPIO_FALLINGDETECT */
560 case 0x50: /* GPIO_DEBOUNCENABLE */
561 case 0x54: /* GPIO_DEBOUNCINGTIME */
562 cur
= omap2_gpio_module_read(opaque
, addr
& ~3) &
563 ~(mask
<< ((addr
& 3) << 3));
566 case 0x18: /* GPIO_IRQSTATUS1 */
567 case 0x28: /* GPIO_IRQSTATUS2 */
568 case 0x60: /* GPIO_CLEARIRQENABLE1 */
569 case 0x64: /* GPIO_SETIRQENABLE1 */
570 case 0x70: /* GPIO_CLEARIRQENABLE2 */
571 case 0x74: /* GPIO_SETIREQNEABLE2 */
572 case 0x80: /* GPIO_CLEARWKUENA */
573 case 0x84: /* GPIO_SETWKUENA */
574 case 0x90: /* GPIO_CLEARDATAOUT */
575 case 0x94: /* GPIO_SETDATAOUT */
576 value
<<= (addr
& 3) << 3;
577 omap2_gpio_module_write(opaque
, addr
, cur
| value
);
586 static const MemoryRegionOps omap2_gpio_module_ops
= {
587 .read
= omap2_gpio_module_readp
,
588 .write
= omap2_gpio_module_writep
,
589 .valid
.min_access_size
= 1,
590 .valid
.max_access_size
= 4,
591 .endianness
= DEVICE_NATIVE_ENDIAN
,
594 static void omap_gpif_reset(DeviceState
*dev
)
596 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
598 omap_gpio_reset(&s
->omap1
);
601 static void omap2_gpif_reset(DeviceState
*dev
)
603 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
606 for (i
= 0; i
< s
->modulecount
; i
++) {
607 omap2_gpio_module_reset(&s
->modules
[i
]);
613 static uint64_t omap2_gpif_top_read(void *opaque
, hwaddr addr
,
616 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
619 case 0x00: /* IPGENERICOCPSPL_REVISION */
622 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
625 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
628 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
631 case 0x40: /* IPGENERICOCPSPL_GPO */
634 case 0x50: /* IPGENERICOCPSPL_GPI */
642 static void omap2_gpif_top_write(void *opaque
, hwaddr addr
,
643 uint64_t value
, unsigned size
)
645 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
648 case 0x00: /* IPGENERICOCPSPL_REVISION */
649 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
650 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
651 case 0x50: /* IPGENERICOCPSPL_GPI */
655 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
656 if (value
& (1 << 1)) /* SOFTRESET */
657 omap2_gpif_reset(DEVICE(s
));
658 s
->autoidle
= value
& 1;
661 case 0x40: /* IPGENERICOCPSPL_GPO */
671 static const MemoryRegionOps omap2_gpif_top_ops
= {
672 .read
= omap2_gpif_top_read
,
673 .write
= omap2_gpif_top_write
,
674 .endianness
= DEVICE_NATIVE_ENDIAN
,
677 static void omap_gpio_init(Object
*obj
)
679 DeviceState
*dev
= DEVICE(obj
);
680 struct omap_gpif_s
*s
= OMAP1_GPIO(obj
);
681 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
683 qdev_init_gpio_in(dev
, omap_gpio_set
, 16);
684 qdev_init_gpio_out(dev
, s
->omap1
.handler
, 16);
685 sysbus_init_irq(sbd
, &s
->omap1
.irq
);
686 memory_region_init_io(&s
->iomem
, obj
, &omap_gpio_ops
, &s
->omap1
,
687 "omap.gpio", 0x1000);
688 sysbus_init_mmio(sbd
, &s
->iomem
);
691 static void omap_gpio_realize(DeviceState
*dev
, Error
**errp
)
693 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
696 error_setg(errp
, "omap-gpio: clk not connected");
700 static void omap2_gpio_realize(DeviceState
*dev
, Error
**errp
)
702 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
703 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
707 error_setg(errp
, "omap2-gpio: iclk not connected");
711 s
->modulecount
= s
->mpu_model
< omap2430
? 4
712 : s
->mpu_model
< omap3430
? 5
715 if (s
->mpu_model
< omap3430
) {
716 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap2_gpif_top_ops
, s
,
717 "omap2.gpio", 0x1000);
718 sysbus_init_mmio(sbd
, &s
->iomem
);
721 s
->modules
= g_new0(struct omap2_gpio_s
, s
->modulecount
);
722 s
->handler
= g_new0(qemu_irq
, s
->modulecount
* 32);
723 qdev_init_gpio_in(dev
, omap2_gpio_set
, s
->modulecount
* 32);
724 qdev_init_gpio_out(dev
, s
->handler
, s
->modulecount
* 32);
726 for (i
= 0; i
< s
->modulecount
; i
++) {
727 struct omap2_gpio_s
*m
= &s
->modules
[i
];
730 error_setg(errp
, "omap2-gpio: fclk%d not connected", i
);
734 m
->revision
= (s
->mpu_model
< omap3430
) ? 0x18 : 0x25;
735 m
->handler
= &s
->handler
[i
* 32];
736 sysbus_init_irq(sbd
, &m
->irq
[0]); /* mpu irq */
737 sysbus_init_irq(sbd
, &m
->irq
[1]); /* dsp irq */
738 sysbus_init_irq(sbd
, &m
->wkup
);
739 memory_region_init_io(&m
->iomem
, OBJECT(dev
), &omap2_gpio_module_ops
, m
,
740 "omap.gpio-module", 0x1000);
741 sysbus_init_mmio(sbd
, &m
->iomem
);
745 void omap_gpio_set_clk(omap_gpif
*gpio
, omap_clk clk
)
750 static Property omap_gpio_properties
[] = {
751 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s
, mpu_model
, 0),
752 DEFINE_PROP_END_OF_LIST(),
755 static void omap_gpio_class_init(ObjectClass
*klass
, void *data
)
757 DeviceClass
*dc
= DEVICE_CLASS(klass
);
759 dc
->realize
= omap_gpio_realize
;
760 dc
->reset
= omap_gpif_reset
;
761 device_class_set_props(dc
, omap_gpio_properties
);
762 /* Reason: pointer property "clk" */
763 dc
->user_creatable
= false;
766 static const TypeInfo omap_gpio_info
= {
767 .name
= TYPE_OMAP1_GPIO
,
768 .parent
= TYPE_SYS_BUS_DEVICE
,
769 .instance_size
= sizeof(struct omap_gpif_s
),
770 .instance_init
= omap_gpio_init
,
771 .class_init
= omap_gpio_class_init
,
774 void omap2_gpio_set_iclk(omap2_gpif
*gpio
, omap_clk clk
)
779 void omap2_gpio_set_fclk(omap2_gpif
*gpio
, uint8_t i
, omap_clk clk
)
785 static Property omap2_gpio_properties
[] = {
786 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s
, mpu_model
, 0),
787 DEFINE_PROP_END_OF_LIST(),
790 static void omap2_gpio_class_init(ObjectClass
*klass
, void *data
)
792 DeviceClass
*dc
= DEVICE_CLASS(klass
);
794 dc
->realize
= omap2_gpio_realize
;
795 dc
->reset
= omap2_gpif_reset
;
796 device_class_set_props(dc
, omap2_gpio_properties
);
797 /* Reason: pointer properties "iclk", "fclk0", ..., "fclk5" */
798 dc
->user_creatable
= false;
801 static const TypeInfo omap2_gpio_info
= {
802 .name
= TYPE_OMAP2_GPIO
,
803 .parent
= TYPE_SYS_BUS_DEVICE
,
804 .instance_size
= sizeof(struct omap2_gpif_s
),
805 .class_init
= omap2_gpio_class_init
,
808 static void omap_gpio_register_types(void)
810 type_register_static(&omap_gpio_info
);
811 type_register_static(&omap2_gpio_info
);
814 type_init(omap_gpio_register_types
)