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/>.
42 struct omap_gpio_s omap1
;
45 /* General-Purpose I/O of OMAP1 */
46 static void omap_gpio_set(void *opaque
, int line
, int level
)
48 struct omap_gpio_s
*s
= &((struct omap_gpif_s
*) opaque
)->omap1
;
49 uint16_t prev
= s
->inputs
;
52 s
->inputs
|= 1 << line
;
54 s
->inputs
&= ~(1 << line
);
56 if (((s
->edge
& s
->inputs
& ~prev
) | (~s
->edge
& ~s
->inputs
& prev
)) &
57 (1 << line
) & s
->dir
& ~s
->mask
) {
59 qemu_irq_raise(s
->irq
);
63 static uint32_t omap_gpio_read(void *opaque
, target_phys_addr_t addr
)
65 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
66 int offset
= addr
& OMAP_MPUI_REG_MASK
;
69 case 0x00: /* DATA_INPUT */
70 return s
->inputs
& s
->pins
;
72 case 0x04: /* DATA_OUTPUT */
75 case 0x08: /* DIRECTION_CONTROL */
78 case 0x0c: /* INTERRUPT_CONTROL */
81 case 0x10: /* INTERRUPT_MASK */
84 case 0x14: /* INTERRUPT_STATUS */
87 case 0x18: /* PIN_CONTROL (not in OMAP310) */
96 static void omap_gpio_write(void *opaque
, target_phys_addr_t addr
,
99 struct omap_gpio_s
*s
= (struct omap_gpio_s
*) opaque
;
100 int offset
= addr
& OMAP_MPUI_REG_MASK
;
105 case 0x00: /* DATA_INPUT */
109 case 0x04: /* DATA_OUTPUT */
110 diff
= (s
->outputs
^ value
) & ~s
->dir
;
112 while ((ln
= ffs(diff
))) {
115 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
120 case 0x08: /* DIRECTION_CONTROL */
121 diff
= s
->outputs
& (s
->dir
^ value
);
124 value
= s
->outputs
& ~s
->dir
;
125 while ((ln
= ffs(diff
))) {
128 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
133 case 0x0c: /* INTERRUPT_CONTROL */
137 case 0x10: /* INTERRUPT_MASK */
141 case 0x14: /* INTERRUPT_STATUS */
144 qemu_irq_lower(s
->irq
);
147 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
158 /* *Some* sources say the memory region is 32-bit. */
159 static CPUReadMemoryFunc
* const omap_gpio_readfn
[] = {
160 omap_badwidth_read16
,
162 omap_badwidth_read16
,
165 static CPUWriteMemoryFunc
* const omap_gpio_writefn
[] = {
166 omap_badwidth_write16
,
168 omap_badwidth_write16
,
171 static void omap_gpio_reset(struct omap_gpio_s
*s
)
182 struct omap2_gpio_s
{
201 struct omap2_gpif_s
{
207 struct omap2_gpio_s
*modules
;
213 /* General-Purpose Interface of OMAP2/3 */
214 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s
*s
,
217 qemu_set_irq(s
->irq
[line
], s
->ints
[line
] & s
->mask
[line
]);
220 static void omap2_gpio_module_wake(struct omap2_gpio_s
*s
, int line
)
222 if (!(s
->config
[0] & (1 << 2))) /* ENAWAKEUP */
224 if (!(s
->config
[0] & (3 << 3))) /* Force Idle */
226 if (!(s
->wumask
& (1 << line
)))
229 qemu_irq_raise(s
->wkup
);
232 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s
*s
,
239 while ((ln
= ffs(diff
))) {
241 qemu_set_irq(s
->handler
[ln
], (s
->outputs
>> ln
) & 1);
246 static void omap2_gpio_module_level_update(struct omap2_gpio_s
*s
, int line
)
248 s
->ints
[line
] |= s
->dir
&
249 ((s
->inputs
& s
->level
[1]) | (~s
->inputs
& s
->level
[0]));
250 omap2_gpio_module_int_update(s
, line
);
253 static inline void omap2_gpio_module_int(struct omap2_gpio_s
*s
, int line
)
255 s
->ints
[0] |= 1 << line
;
256 omap2_gpio_module_int_update(s
, 0);
257 s
->ints
[1] |= 1 << line
;
258 omap2_gpio_module_int_update(s
, 1);
259 omap2_gpio_module_wake(s
, line
);
262 static void omap2_gpio_set(void *opaque
, int line
, int level
)
264 struct omap2_gpif_s
*p
= opaque
;
265 struct omap2_gpio_s
*s
= &p
->modules
[line
>> 5];
269 if (s
->dir
& (1 << line
) & ((~s
->inputs
& s
->edge
[0]) | s
->level
[1]))
270 omap2_gpio_module_int(s
, line
);
271 s
->inputs
|= 1 << line
;
273 if (s
->dir
& (1 << line
) & ((s
->inputs
& s
->edge
[1]) | s
->level
[0]))
274 omap2_gpio_module_int(s
, line
);
275 s
->inputs
&= ~(1 << line
);
279 static void omap2_gpio_module_reset(struct omap2_gpio_s
*s
)
297 static uint32_t omap2_gpio_module_read(void *opaque
, target_phys_addr_t addr
)
299 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
302 case 0x00: /* GPIO_REVISION */
305 case 0x10: /* GPIO_SYSCONFIG */
308 case 0x14: /* GPIO_SYSSTATUS */
311 case 0x18: /* GPIO_IRQSTATUS1 */
314 case 0x1c: /* GPIO_IRQENABLE1 */
315 case 0x60: /* GPIO_CLEARIRQENABLE1 */
316 case 0x64: /* GPIO_SETIRQENABLE1 */
319 case 0x20: /* GPIO_WAKEUPENABLE */
320 case 0x80: /* GPIO_CLEARWKUENA */
321 case 0x84: /* GPIO_SETWKUENA */
324 case 0x28: /* GPIO_IRQSTATUS2 */
327 case 0x2c: /* GPIO_IRQENABLE2 */
328 case 0x70: /* GPIO_CLEARIRQENABLE2 */
329 case 0x74: /* GPIO_SETIREQNEABLE2 */
332 case 0x30: /* GPIO_CTRL */
335 case 0x34: /* GPIO_OE */
338 case 0x38: /* GPIO_DATAIN */
341 case 0x3c: /* GPIO_DATAOUT */
342 case 0x90: /* GPIO_CLEARDATAOUT */
343 case 0x94: /* GPIO_SETDATAOUT */
346 case 0x40: /* GPIO_LEVELDETECT0 */
349 case 0x44: /* GPIO_LEVELDETECT1 */
352 case 0x48: /* GPIO_RISINGDETECT */
355 case 0x4c: /* GPIO_FALLINGDETECT */
358 case 0x50: /* GPIO_DEBOUNCENABLE */
361 case 0x54: /* GPIO_DEBOUNCINGTIME */
369 static void omap2_gpio_module_write(void *opaque
, target_phys_addr_t addr
,
372 struct omap2_gpio_s
*s
= (struct omap2_gpio_s
*) opaque
;
377 case 0x00: /* GPIO_REVISION */
378 case 0x14: /* GPIO_SYSSTATUS */
379 case 0x38: /* GPIO_DATAIN */
383 case 0x10: /* GPIO_SYSCONFIG */
384 if (((value
>> 3) & 3) == 3)
385 fprintf(stderr
, "%s: bad IDLEMODE value\n", __FUNCTION__
);
387 omap2_gpio_module_reset(s
);
388 s
->config
[0] = value
& 0x1d;
391 case 0x18: /* GPIO_IRQSTATUS1 */
392 if (s
->ints
[0] & value
) {
393 s
->ints
[0] &= ~value
;
394 omap2_gpio_module_level_update(s
, 0);
398 case 0x1c: /* GPIO_IRQENABLE1 */
400 omap2_gpio_module_int_update(s
, 0);
403 case 0x20: /* GPIO_WAKEUPENABLE */
407 case 0x28: /* GPIO_IRQSTATUS2 */
408 if (s
->ints
[1] & value
) {
409 s
->ints
[1] &= ~value
;
410 omap2_gpio_module_level_update(s
, 1);
414 case 0x2c: /* GPIO_IRQENABLE2 */
416 omap2_gpio_module_int_update(s
, 1);
419 case 0x30: /* GPIO_CTRL */
420 s
->config
[1] = value
& 7;
423 case 0x34: /* GPIO_OE */
424 diff
= s
->outputs
& (s
->dir
^ value
);
427 value
= s
->outputs
& ~s
->dir
;
428 while ((ln
= ffs(diff
))) {
429 diff
&= ~(1 <<-- ln
);
430 qemu_set_irq(s
->handler
[ln
], (value
>> ln
) & 1);
433 omap2_gpio_module_level_update(s
, 0);
434 omap2_gpio_module_level_update(s
, 1);
437 case 0x3c: /* GPIO_DATAOUT */
438 omap2_gpio_module_out_update(s
, s
->outputs
^ value
);
441 case 0x40: /* GPIO_LEVELDETECT0 */
443 omap2_gpio_module_level_update(s
, 0);
444 omap2_gpio_module_level_update(s
, 1);
447 case 0x44: /* GPIO_LEVELDETECT1 */
449 omap2_gpio_module_level_update(s
, 0);
450 omap2_gpio_module_level_update(s
, 1);
453 case 0x48: /* GPIO_RISINGDETECT */
457 case 0x4c: /* GPIO_FALLINGDETECT */
461 case 0x50: /* GPIO_DEBOUNCENABLE */
465 case 0x54: /* GPIO_DEBOUNCINGTIME */
469 case 0x60: /* GPIO_CLEARIRQENABLE1 */
470 s
->mask
[0] &= ~value
;
471 omap2_gpio_module_int_update(s
, 0);
474 case 0x64: /* GPIO_SETIRQENABLE1 */
476 omap2_gpio_module_int_update(s
, 0);
479 case 0x70: /* GPIO_CLEARIRQENABLE2 */
480 s
->mask
[1] &= ~value
;
481 omap2_gpio_module_int_update(s
, 1);
484 case 0x74: /* GPIO_SETIREQNEABLE2 */
486 omap2_gpio_module_int_update(s
, 1);
489 case 0x80: /* GPIO_CLEARWKUENA */
493 case 0x84: /* GPIO_SETWKUENA */
497 case 0x90: /* GPIO_CLEARDATAOUT */
498 omap2_gpio_module_out_update(s
, s
->outputs
& value
);
501 case 0x94: /* GPIO_SETDATAOUT */
502 omap2_gpio_module_out_update(s
, ~s
->outputs
& value
);
511 static uint32_t omap2_gpio_module_readp(void *opaque
, target_phys_addr_t addr
)
513 return omap2_gpio_module_readp(opaque
, addr
) >> ((addr
& 3) << 3);
516 static void omap2_gpio_module_writep(void *opaque
, target_phys_addr_t addr
,
520 uint32_t mask
= 0xffff;
523 case 0x00: /* GPIO_REVISION */
524 case 0x14: /* GPIO_SYSSTATUS */
525 case 0x38: /* GPIO_DATAIN */
529 case 0x10: /* GPIO_SYSCONFIG */
530 case 0x1c: /* GPIO_IRQENABLE1 */
531 case 0x20: /* GPIO_WAKEUPENABLE */
532 case 0x2c: /* GPIO_IRQENABLE2 */
533 case 0x30: /* GPIO_CTRL */
534 case 0x34: /* GPIO_OE */
535 case 0x3c: /* GPIO_DATAOUT */
536 case 0x40: /* GPIO_LEVELDETECT0 */
537 case 0x44: /* GPIO_LEVELDETECT1 */
538 case 0x48: /* GPIO_RISINGDETECT */
539 case 0x4c: /* GPIO_FALLINGDETECT */
540 case 0x50: /* GPIO_DEBOUNCENABLE */
541 case 0x54: /* GPIO_DEBOUNCINGTIME */
542 cur
= omap2_gpio_module_read(opaque
, addr
& ~3) &
543 ~(mask
<< ((addr
& 3) << 3));
546 case 0x18: /* GPIO_IRQSTATUS1 */
547 case 0x28: /* GPIO_IRQSTATUS2 */
548 case 0x60: /* GPIO_CLEARIRQENABLE1 */
549 case 0x64: /* GPIO_SETIRQENABLE1 */
550 case 0x70: /* GPIO_CLEARIRQENABLE2 */
551 case 0x74: /* GPIO_SETIREQNEABLE2 */
552 case 0x80: /* GPIO_CLEARWKUENA */
553 case 0x84: /* GPIO_SETWKUENA */
554 case 0x90: /* GPIO_CLEARDATAOUT */
555 case 0x94: /* GPIO_SETDATAOUT */
556 value
<<= (addr
& 3) << 3;
557 omap2_gpio_module_write(opaque
, addr
, cur
| value
);
566 static CPUReadMemoryFunc
* const omap2_gpio_module_readfn
[] = {
567 omap2_gpio_module_readp
,
568 omap2_gpio_module_readp
,
569 omap2_gpio_module_read
,
572 static CPUWriteMemoryFunc
* const omap2_gpio_module_writefn
[] = {
573 omap2_gpio_module_writep
,
574 omap2_gpio_module_writep
,
575 omap2_gpio_module_write
,
578 static void omap_gpif_reset(DeviceState
*dev
)
580 struct omap_gpif_s
*s
= FROM_SYSBUS(struct omap_gpif_s
,
581 sysbus_from_qdev(dev
));
582 omap_gpio_reset(&s
->omap1
);
585 static void omap2_gpif_reset(DeviceState
*dev
)
588 struct omap2_gpif_s
*s
= FROM_SYSBUS(struct omap2_gpif_s
,
589 sysbus_from_qdev(dev
));
590 for (i
= 0; i
< s
->modulecount
; i
++) {
591 omap2_gpio_module_reset(&s
->modules
[i
]);
597 static uint32_t omap2_gpif_top_read(void *opaque
, target_phys_addr_t addr
)
599 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
602 case 0x00: /* IPGENERICOCPSPL_REVISION */
605 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
608 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
611 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
614 case 0x40: /* IPGENERICOCPSPL_GPO */
617 case 0x50: /* IPGENERICOCPSPL_GPI */
625 static void omap2_gpif_top_write(void *opaque
, target_phys_addr_t addr
,
628 struct omap2_gpif_s
*s
= (struct omap2_gpif_s
*) opaque
;
631 case 0x00: /* IPGENERICOCPSPL_REVISION */
632 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
633 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
634 case 0x50: /* IPGENERICOCPSPL_GPI */
638 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
639 if (value
& (1 << 1)) /* SOFTRESET */
640 omap2_gpif_reset(&s
->busdev
.qdev
);
641 s
->autoidle
= value
& 1;
644 case 0x40: /* IPGENERICOCPSPL_GPO */
654 static CPUReadMemoryFunc
* const omap2_gpif_top_readfn
[] = {
660 static CPUWriteMemoryFunc
* const omap2_gpif_top_writefn
[] = {
661 omap2_gpif_top_write
,
662 omap2_gpif_top_write
,
663 omap2_gpif_top_write
,
666 static int omap_gpio_init(SysBusDevice
*dev
)
668 struct omap_gpif_s
*s
= FROM_SYSBUS(struct omap_gpif_s
, dev
);
670 hw_error("omap-gpio: clk not connected\n");
672 qdev_init_gpio_in(&dev
->qdev
, omap_gpio_set
, 16);
673 qdev_init_gpio_out(&dev
->qdev
, s
->omap1
.handler
, 16);
674 sysbus_init_irq(dev
, &s
->omap1
.irq
);
675 sysbus_init_mmio(dev
, 0x1000,
676 cpu_register_io_memory(omap_gpio_readfn
,
679 DEVICE_NATIVE_ENDIAN
));
683 static int omap2_gpio_init(SysBusDevice
*dev
)
686 struct omap2_gpif_s
*s
= FROM_SYSBUS(struct omap2_gpif_s
, dev
);
688 hw_error("omap2-gpio: iclk not connected\n");
690 if (s
->mpu_model
< omap3430
) {
691 s
->modulecount
= (s
->mpu_model
< omap2430
) ? 4 : 5;
692 sysbus_init_mmio(dev
, 0x1000,
693 cpu_register_io_memory(omap2_gpif_top_readfn
,
694 omap2_gpif_top_writefn
, s
,
695 DEVICE_NATIVE_ENDIAN
));
699 s
->modules
= g_malloc0(s
->modulecount
* sizeof(struct omap2_gpio_s
));
700 s
->handler
= g_malloc0(s
->modulecount
* 32 * sizeof(qemu_irq
));
701 qdev_init_gpio_in(&dev
->qdev
, omap2_gpio_set
, s
->modulecount
* 32);
702 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, s
->modulecount
* 32);
703 for (i
= 0; i
< s
->modulecount
; i
++) {
704 struct omap2_gpio_s
*m
= &s
->modules
[i
];
706 hw_error("omap2-gpio: fclk%d not connected\n", i
);
708 m
->revision
= (s
->mpu_model
< omap3430
) ? 0x18 : 0x25;
709 m
->handler
= &s
->handler
[i
* 32];
710 sysbus_init_irq(dev
, &m
->irq
[0]); /* mpu irq */
711 sysbus_init_irq(dev
, &m
->irq
[1]); /* dsp irq */
712 sysbus_init_irq(dev
, &m
->wkup
);
713 sysbus_init_mmio(dev
, 0x1000,
714 cpu_register_io_memory(omap2_gpio_module_readfn
,
715 omap2_gpio_module_writefn
,
716 m
, DEVICE_NATIVE_ENDIAN
));
721 /* Using qdev pointer properties for the clocks is not ideal.
722 * qdev should support a generic means of defining a 'port' with
723 * an arbitrary interface for connecting two devices. Then we
724 * could reframe the omap clock API in terms of clock ports,
725 * and get some type safety. For now the best qdev provides is
726 * passing an arbitrary pointer.
727 * (It's not possible to pass in the string which is the clock
728 * name, because this device does not have the necessary information
729 * (ie the struct omap_mpu_state_s*) to do the clockname to pointer
733 static SysBusDeviceInfo omap_gpio_info
= {
734 .init
= omap_gpio_init
,
735 .qdev
.name
= "omap-gpio",
736 .qdev
.size
= sizeof(struct omap_gpif_s
),
737 .qdev
.reset
= omap_gpif_reset
,
738 .qdev
.props
= (Property
[]) {
739 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s
, mpu_model
, 0),
740 DEFINE_PROP_PTR("clk", struct omap_gpif_s
, clk
),
741 DEFINE_PROP_END_OF_LIST()
745 static SysBusDeviceInfo omap2_gpio_info
= {
746 .init
= omap2_gpio_init
,
747 .qdev
.name
= "omap2-gpio",
748 .qdev
.size
= sizeof(struct omap2_gpif_s
),
749 .qdev
.reset
= omap2_gpif_reset
,
750 .qdev
.props
= (Property
[]) {
751 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s
, mpu_model
, 0),
752 DEFINE_PROP_PTR("iclk", struct omap2_gpif_s
, iclk
),
753 DEFINE_PROP_PTR("fclk0", struct omap2_gpif_s
, fclk
[0]),
754 DEFINE_PROP_PTR("fclk1", struct omap2_gpif_s
, fclk
[1]),
755 DEFINE_PROP_PTR("fclk2", struct omap2_gpif_s
, fclk
[2]),
756 DEFINE_PROP_PTR("fclk3", struct omap2_gpif_s
, fclk
[3]),
757 DEFINE_PROP_PTR("fclk4", struct omap2_gpif_s
, fclk
[4]),
758 DEFINE_PROP_PTR("fclk5", struct omap2_gpif_s
, fclk
[5]),
759 DEFINE_PROP_END_OF_LIST()
763 static void omap_gpio_register_device(void)
765 sysbus_register_withprop(&omap_gpio_info
);
766 sysbus_register_withprop(&omap2_gpio_info
);
769 device_init(omap_gpio_register_device
)