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 "qapi/error.h"
41 #define TYPE_OMAP1_GPIO "omap-gpio"
42 #define OMAP1_GPIO(obj) \
43 OBJECT_CHECK(struct omap_gpif_s, (obj), TYPE_OMAP1_GPIO)
46 SysBusDevice parent_obj
;
51 struct omap_gpio_s omap1
;
54 /* General-Purpose I/O of OMAP1 */
55 static void omap_gpio_set(void *opaque
, int line
, int level
)
57 struct omap_gpio_s
*s
= &((struct omap_gpif_s
*) opaque
)->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
= (struct omap_gpio_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
= (struct omap_gpio_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 struct omap2_gpio_s
{
213 #define TYPE_OMAP2_GPIO "omap2-gpio"
214 #define OMAP2_GPIO(obj) \
215 OBJECT_CHECK(struct omap2_gpif_s, (obj), TYPE_OMAP2_GPIO)
217 struct omap2_gpif_s
{
218 SysBusDevice parent_obj
;
225 struct omap2_gpio_s
*modules
;
231 /* General-Purpose Interface of OMAP2/3 */
232 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s
*s
,
235 qemu_set_irq(s
->irq
[line
], s
->ints
[line
] & s
->mask
[line
]);
238 static void omap2_gpio_module_wake(struct omap2_gpio_s
*s
, int line
)
240 if (!(s
->config
[0] & (1 << 2))) /* ENAWAKEUP */
242 if (!(s
->config
[0] & (3 << 3))) /* Force Idle */
244 if (!(s
->wumask
& (1 << line
)))
247 qemu_irq_raise(s
->wkup
);
250 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s
*s
,
257 while ((ln
= ctz32(diff
)) != 32) {
258 qemu_set_irq(s
->handler
[ln
], (s
->outputs
>> ln
) & 1);
263 static void omap2_gpio_module_level_update(struct omap2_gpio_s
*s
, int line
)
265 s
->ints
[line
] |= s
->dir
&
266 ((s
->inputs
& s
->level
[1]) | (~s
->inputs
& s
->level
[0]));
267 omap2_gpio_module_int_update(s
, line
);
270 static inline void omap2_gpio_module_int(struct omap2_gpio_s
*s
, int line
)
272 s
->ints
[0] |= 1 << line
;
273 omap2_gpio_module_int_update(s
, 0);
274 s
->ints
[1] |= 1 << line
;
275 omap2_gpio_module_int_update(s
, 1);
276 omap2_gpio_module_wake(s
, line
);
279 static void omap2_gpio_set(void *opaque
, int line
, int level
)
281 struct omap2_gpif_s
*p
= opaque
;
282 struct omap2_gpio_s
*s
= &p
->modules
[line
>> 5];
286 if (s
->dir
& (1 << line
) & ((~s
->inputs
& s
->edge
[0]) | s
->level
[1]))
287 omap2_gpio_module_int(s
, line
);
288 s
->inputs
|= 1 << line
;
290 if (s
->dir
& (1 << line
) & ((s
->inputs
& s
->edge
[1]) | s
->level
[0]))
291 omap2_gpio_module_int(s
, line
);
292 s
->inputs
&= ~(1 << line
);
296 static void omap2_gpio_module_reset(struct omap2_gpio_s
*s
)
314 static uint32_t omap2_gpio_module_read(void *opaque
, hwaddr addr
)
316 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
319 case 0x00: /* GPIO_REVISION */
322 case 0x10: /* GPIO_SYSCONFIG */
325 case 0x14: /* GPIO_SYSSTATUS */
328 case 0x18: /* GPIO_IRQSTATUS1 */
331 case 0x1c: /* GPIO_IRQENABLE1 */
332 case 0x60: /* GPIO_CLEARIRQENABLE1 */
333 case 0x64: /* GPIO_SETIRQENABLE1 */
336 case 0x20: /* GPIO_WAKEUPENABLE */
337 case 0x80: /* GPIO_CLEARWKUENA */
338 case 0x84: /* GPIO_SETWKUENA */
341 case 0x28: /* GPIO_IRQSTATUS2 */
344 case 0x2c: /* GPIO_IRQENABLE2 */
345 case 0x70: /* GPIO_CLEARIRQENABLE2 */
346 case 0x74: /* GPIO_SETIREQNEABLE2 */
349 case 0x30: /* GPIO_CTRL */
352 case 0x34: /* GPIO_OE */
355 case 0x38: /* GPIO_DATAIN */
358 case 0x3c: /* GPIO_DATAOUT */
359 case 0x90: /* GPIO_CLEARDATAOUT */
360 case 0x94: /* GPIO_SETDATAOUT */
363 case 0x40: /* GPIO_LEVELDETECT0 */
366 case 0x44: /* GPIO_LEVELDETECT1 */
369 case 0x48: /* GPIO_RISINGDETECT */
372 case 0x4c: /* GPIO_FALLINGDETECT */
375 case 0x50: /* GPIO_DEBOUNCENABLE */
378 case 0x54: /* GPIO_DEBOUNCINGTIME */
386 static void omap2_gpio_module_write(void *opaque
, hwaddr addr
,
389 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
394 case 0x00: /* GPIO_REVISION */
395 case 0x14: /* GPIO_SYSSTATUS */
396 case 0x38: /* GPIO_DATAIN */
400 case 0x10: /* GPIO_SYSCONFIG */
401 if (((value
>> 3) & 3) == 3)
402 fprintf(stderr
, "%s: bad IDLEMODE value\n", __FUNCTION__
);
404 omap2_gpio_module_reset(s
);
405 s
->config
[0] = value
& 0x1d;
408 case 0x18: /* GPIO_IRQSTATUS1 */
409 if (s
->ints
[0] & value
) {
410 s
->ints
[0] &= ~value
;
411 omap2_gpio_module_level_update(s
, 0);
415 case 0x1c: /* GPIO_IRQENABLE1 */
417 omap2_gpio_module_int_update(s
, 0);
420 case 0x20: /* GPIO_WAKEUPENABLE */
424 case 0x28: /* GPIO_IRQSTATUS2 */
425 if (s
->ints
[1] & value
) {
426 s
->ints
[1] &= ~value
;
427 omap2_gpio_module_level_update(s
, 1);
431 case 0x2c: /* GPIO_IRQENABLE2 */
433 omap2_gpio_module_int_update(s
, 1);
436 case 0x30: /* GPIO_CTRL */
437 s
->config
[1] = value
& 7;
440 case 0x34: /* GPIO_OE */
441 diff
= s
->outputs
& (s
->dir
^ value
);
444 value
= s
->outputs
& ~s
->dir
;
445 while ((ln
= ctz32(diff
)) != 32) {
447 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
450 omap2_gpio_module_level_update(s
, 0);
451 omap2_gpio_module_level_update(s
, 1);
454 case 0x3c: /* GPIO_DATAOUT */
455 omap2_gpio_module_out_update(s
, s
->outputs
^ value
);
458 case 0x40: /* GPIO_LEVELDETECT0 */
460 omap2_gpio_module_level_update(s
, 0);
461 omap2_gpio_module_level_update(s
, 1);
464 case 0x44: /* GPIO_LEVELDETECT1 */
466 omap2_gpio_module_level_update(s
, 0);
467 omap2_gpio_module_level_update(s
, 1);
470 case 0x48: /* GPIO_RISINGDETECT */
474 case 0x4c: /* GPIO_FALLINGDETECT */
478 case 0x50: /* GPIO_DEBOUNCENABLE */
482 case 0x54: /* GPIO_DEBOUNCINGTIME */
486 case 0x60: /* GPIO_CLEARIRQENABLE1 */
487 s
->mask
[0] &= ~value
;
488 omap2_gpio_module_int_update(s
, 0);
491 case 0x64: /* GPIO_SETIRQENABLE1 */
493 omap2_gpio_module_int_update(s
, 0);
496 case 0x70: /* GPIO_CLEARIRQENABLE2 */
497 s
->mask
[1] &= ~value
;
498 omap2_gpio_module_int_update(s
, 1);
501 case 0x74: /* GPIO_SETIREQNEABLE2 */
503 omap2_gpio_module_int_update(s
, 1);
506 case 0x80: /* GPIO_CLEARWKUENA */
510 case 0x84: /* GPIO_SETWKUENA */
514 case 0x90: /* GPIO_CLEARDATAOUT */
515 omap2_gpio_module_out_update(s
, s
->outputs
& value
);
518 case 0x94: /* GPIO_SETDATAOUT */
519 omap2_gpio_module_out_update(s
, ~s
->outputs
& value
);
528 static uint64_t omap2_gpio_module_readp(void *opaque
, hwaddr addr
,
531 return omap2_gpio_module_read(opaque
, addr
& ~3) >> ((addr
& 3) << 3);
534 static void omap2_gpio_module_writep(void *opaque
, hwaddr addr
,
535 uint64_t value
, unsigned size
)
538 uint32_t mask
= 0xffff;
541 omap2_gpio_module_write(opaque
, addr
, value
);
546 case 0x00: /* GPIO_REVISION */
547 case 0x14: /* GPIO_SYSSTATUS */
548 case 0x38: /* GPIO_DATAIN */
552 case 0x10: /* GPIO_SYSCONFIG */
553 case 0x1c: /* GPIO_IRQENABLE1 */
554 case 0x20: /* GPIO_WAKEUPENABLE */
555 case 0x2c: /* GPIO_IRQENABLE2 */
556 case 0x30: /* GPIO_CTRL */
557 case 0x34: /* GPIO_OE */
558 case 0x3c: /* GPIO_DATAOUT */
559 case 0x40: /* GPIO_LEVELDETECT0 */
560 case 0x44: /* GPIO_LEVELDETECT1 */
561 case 0x48: /* GPIO_RISINGDETECT */
562 case 0x4c: /* GPIO_FALLINGDETECT */
563 case 0x50: /* GPIO_DEBOUNCENABLE */
564 case 0x54: /* GPIO_DEBOUNCINGTIME */
565 cur
= omap2_gpio_module_read(opaque
, addr
& ~3) &
566 ~(mask
<< ((addr
& 3) << 3));
569 case 0x18: /* GPIO_IRQSTATUS1 */
570 case 0x28: /* GPIO_IRQSTATUS2 */
571 case 0x60: /* GPIO_CLEARIRQENABLE1 */
572 case 0x64: /* GPIO_SETIRQENABLE1 */
573 case 0x70: /* GPIO_CLEARIRQENABLE2 */
574 case 0x74: /* GPIO_SETIREQNEABLE2 */
575 case 0x80: /* GPIO_CLEARWKUENA */
576 case 0x84: /* GPIO_SETWKUENA */
577 case 0x90: /* GPIO_CLEARDATAOUT */
578 case 0x94: /* GPIO_SETDATAOUT */
579 value
<<= (addr
& 3) << 3;
580 omap2_gpio_module_write(opaque
, addr
, cur
| value
);
589 static const MemoryRegionOps omap2_gpio_module_ops
= {
590 .read
= omap2_gpio_module_readp
,
591 .write
= omap2_gpio_module_writep
,
592 .valid
.min_access_size
= 1,
593 .valid
.max_access_size
= 4,
594 .endianness
= DEVICE_NATIVE_ENDIAN
,
597 static void omap_gpif_reset(DeviceState
*dev
)
599 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
601 omap_gpio_reset(&s
->omap1
);
604 static void omap2_gpif_reset(DeviceState
*dev
)
606 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
609 for (i
= 0; i
< s
->modulecount
; i
++) {
610 omap2_gpio_module_reset(&s
->modules
[i
]);
616 static uint64_t omap2_gpif_top_read(void *opaque
, hwaddr addr
,
619 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
622 case 0x00: /* IPGENERICOCPSPL_REVISION */
625 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
628 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
631 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
634 case 0x40: /* IPGENERICOCPSPL_GPO */
637 case 0x50: /* IPGENERICOCPSPL_GPI */
645 static void omap2_gpif_top_write(void *opaque
, hwaddr addr
,
646 uint64_t value
, unsigned size
)
648 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
651 case 0x00: /* IPGENERICOCPSPL_REVISION */
652 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
653 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
654 case 0x50: /* IPGENERICOCPSPL_GPI */
658 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
659 if (value
& (1 << 1)) /* SOFTRESET */
660 omap2_gpif_reset(DEVICE(s
));
661 s
->autoidle
= value
& 1;
664 case 0x40: /* IPGENERICOCPSPL_GPO */
674 static const MemoryRegionOps omap2_gpif_top_ops
= {
675 .read
= omap2_gpif_top_read
,
676 .write
= omap2_gpif_top_write
,
677 .endianness
= DEVICE_NATIVE_ENDIAN
,
680 static void omap_gpio_init(Object
*obj
)
682 DeviceState
*dev
= DEVICE(obj
);
683 struct omap_gpif_s
*s
= OMAP1_GPIO(obj
);
684 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
686 qdev_init_gpio_in(dev
, omap_gpio_set
, 16);
687 qdev_init_gpio_out(dev
, s
->omap1
.handler
, 16);
688 sysbus_init_irq(sbd
, &s
->omap1
.irq
);
689 memory_region_init_io(&s
->iomem
, obj
, &omap_gpio_ops
, &s
->omap1
,
690 "omap.gpio", 0x1000);
691 sysbus_init_mmio(sbd
, &s
->iomem
);
694 static void omap_gpio_realize(DeviceState
*dev
, Error
**errp
)
696 struct omap_gpif_s
*s
= OMAP1_GPIO(dev
);
699 error_setg(errp
, "omap-gpio: clk not connected");
703 static void omap2_gpio_realize(DeviceState
*dev
, Error
**errp
)
705 struct omap2_gpif_s
*s
= OMAP2_GPIO(dev
);
706 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
710 error_setg(errp
, "omap2-gpio: iclk not connected");
714 s
->modulecount
= s
->mpu_model
< omap2430
? 4
715 : s
->mpu_model
< omap3430
? 5
718 if (s
->mpu_model
< omap3430
) {
719 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &omap2_gpif_top_ops
, s
,
720 "omap2.gpio", 0x1000);
721 sysbus_init_mmio(sbd
, &s
->iomem
);
724 s
->modules
= g_new0(struct omap2_gpio_s
, s
->modulecount
);
725 s
->handler
= g_new0(qemu_irq
, s
->modulecount
* 32);
726 qdev_init_gpio_in(dev
, omap2_gpio_set
, s
->modulecount
* 32);
727 qdev_init_gpio_out(dev
, s
->handler
, s
->modulecount
* 32);
729 for (i
= 0; i
< s
->modulecount
; i
++) {
730 struct omap2_gpio_s
*m
= &s
->modules
[i
];
733 error_setg(errp
, "omap2-gpio: fclk%d not connected", i
);
737 m
->revision
= (s
->mpu_model
< omap3430
) ? 0x18 : 0x25;
738 m
->handler
= &s
->handler
[i
* 32];
739 sysbus_init_irq(sbd
, &m
->irq
[0]); /* mpu irq */
740 sysbus_init_irq(sbd
, &m
->irq
[1]); /* dsp irq */
741 sysbus_init_irq(sbd
, &m
->wkup
);
742 memory_region_init_io(&m
->iomem
, OBJECT(dev
), &omap2_gpio_module_ops
, m
,
743 "omap.gpio-module", 0x1000);
744 sysbus_init_mmio(sbd
, &m
->iomem
);
748 /* Using qdev pointer properties for the clocks is not ideal.
749 * qdev should support a generic means of defining a 'port' with
750 * an arbitrary interface for connecting two devices. Then we
751 * could reframe the omap clock API in terms of clock ports,
752 * and get some type safety. For now the best qdev provides is
753 * passing an arbitrary pointer.
754 * (It's not possible to pass in the string which is the clock
755 * name, because this device does not have the necessary information
756 * (ie the struct omap_mpu_state_s*) to do the clockname to pointer
760 static Property omap_gpio_properties
[] = {
761 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s
, mpu_model
, 0),
762 DEFINE_PROP_PTR("clk", struct omap_gpif_s
, clk
),
763 DEFINE_PROP_END_OF_LIST(),
766 static void omap_gpio_class_init(ObjectClass
*klass
, void *data
)
768 DeviceClass
*dc
= DEVICE_CLASS(klass
);
770 dc
->realize
= omap_gpio_realize
;
771 dc
->reset
= omap_gpif_reset
;
772 dc
->props
= omap_gpio_properties
;
773 /* Reason: pointer property "clk" */
774 dc
->user_creatable
= false;
777 static const TypeInfo omap_gpio_info
= {
778 .name
= TYPE_OMAP1_GPIO
,
779 .parent
= TYPE_SYS_BUS_DEVICE
,
780 .instance_size
= sizeof(struct omap_gpif_s
),
781 .instance_init
= omap_gpio_init
,
782 .class_init
= omap_gpio_class_init
,
785 static Property omap2_gpio_properties
[] = {
786 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s
, mpu_model
, 0),
787 DEFINE_PROP_PTR("iclk", struct omap2_gpif_s
, iclk
),
788 DEFINE_PROP_PTR("fclk0", struct omap2_gpif_s
, fclk
[0]),
789 DEFINE_PROP_PTR("fclk1", struct omap2_gpif_s
, fclk
[1]),
790 DEFINE_PROP_PTR("fclk2", struct omap2_gpif_s
, fclk
[2]),
791 DEFINE_PROP_PTR("fclk3", struct omap2_gpif_s
, fclk
[3]),
792 DEFINE_PROP_PTR("fclk4", struct omap2_gpif_s
, fclk
[4]),
793 DEFINE_PROP_PTR("fclk5", struct omap2_gpif_s
, fclk
[5]),
794 DEFINE_PROP_END_OF_LIST(),
797 static void omap2_gpio_class_init(ObjectClass
*klass
, void *data
)
799 DeviceClass
*dc
= DEVICE_CLASS(klass
);
801 dc
->realize
= omap2_gpio_realize
;
802 dc
->reset
= omap2_gpif_reset
;
803 dc
->props
= omap2_gpio_properties
;
804 /* Reason: pointer properties "iclk", "fclk0", ..., "fclk5" */
805 dc
->user_creatable
= false;
808 static const TypeInfo omap2_gpio_info
= {
809 .name
= TYPE_OMAP2_GPIO
,
810 .parent
= TYPE_SYS_BUS_DEVICE
,
811 .instance_size
= sizeof(struct omap2_gpif_s
),
812 .class_init
= omap2_gpio_class_init
,
815 static void omap_gpio_register_types(void)
817 type_register_static(&omap_gpio_info
);
818 type_register_static(&omap2_gpio_info
);
821 type_init(omap_gpio_register_types
)