2 * TI OMAP processors GPIO emulation.
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/>.
22 #include "hw/arm/omap.h"
23 #include "hw/sysbus.h"
43 struct omap_gpio_s omap1;
46 /* General-Purpose I/O of OMAP1 */
47 static void omap_gpio_set(void *opaque, int line, int level)
49 struct omap_gpio_s *s = &((struct omap_gpif_s *) opaque)->omap1;
50 uint16_t prev = s->inputs;
53 s->inputs |= 1 << line;
55 s->inputs &= ~(1 << line);
57 if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
58 (1 << line) & s->dir & ~s->mask) {
60 qemu_irq_raise(s->irq);
64 static uint64_t omap_gpio_read(void *opaque, hwaddr addr,
67 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
68 int offset = addr & OMAP_MPUI_REG_MASK;
71 return omap_badwidth_read16(opaque, addr);
75 case 0x00: /* DATA_INPUT */
76 return s->inputs & s->pins;
78 case 0x04: /* DATA_OUTPUT */
81 case 0x08: /* DIRECTION_CONTROL */
84 case 0x0c: /* INTERRUPT_CONTROL */
87 case 0x10: /* INTERRUPT_MASK */
90 case 0x14: /* INTERRUPT_STATUS */
93 case 0x18: /* PIN_CONTROL (not in OMAP310) */
102 static void omap_gpio_write(void *opaque, hwaddr addr,
103 uint64_t value, unsigned size)
105 struct omap_gpio_s *s = (struct omap_gpio_s *) opaque;
106 int offset = addr & OMAP_MPUI_REG_MASK;
111 return omap_badwidth_write16(opaque, addr, value);
115 case 0x00: /* DATA_INPUT */
119 case 0x04: /* DATA_OUTPUT */
120 diff = (s->outputs ^ value) & ~s->dir;
122 while ((ln = ffs(diff))) {
125 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
130 case 0x08: /* DIRECTION_CONTROL */
131 diff = s->outputs & (s->dir ^ value);
134 value = s->outputs & ~s->dir;
135 while ((ln = ffs(diff))) {
138 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
143 case 0x0c: /* INTERRUPT_CONTROL */
147 case 0x10: /* INTERRUPT_MASK */
151 case 0x14: /* INTERRUPT_STATUS */
154 qemu_irq_lower(s->irq);
157 case 0x18: /* PIN_CONTROL (not in OMAP310 TRM) */
168 /* *Some* sources say the memory region is 32-bit. */
169 static const MemoryRegionOps omap_gpio_ops = {
170 .read = omap_gpio_read,
171 .write = omap_gpio_write,
172 .endianness = DEVICE_NATIVE_ENDIAN,
175 static void omap_gpio_reset(struct omap_gpio_s *s)
186 struct omap2_gpio_s {
206 struct omap2_gpif_s {
213 struct omap2_gpio_s *modules;
219 /* General-Purpose Interface of OMAP2/3 */
220 static inline void omap2_gpio_module_int_update(struct omap2_gpio_s *s,
223 qemu_set_irq(s->irq[line], s->ints[line] & s->mask[line]);
226 static void omap2_gpio_module_wake(struct omap2_gpio_s *s, int line)
228 if (!(s->config[0] & (1 << 2))) /* ENAWAKEUP */
230 if (!(s->config[0] & (3 << 3))) /* Force Idle */
232 if (!(s->wumask & (1 << line)))
235 qemu_irq_raise(s->wkup);
238 static inline void omap2_gpio_module_out_update(struct omap2_gpio_s *s,
245 while ((ln = ffs(diff))) {
247 qemu_set_irq(s->handler[ln], (s->outputs >> ln) & 1);
252 static void omap2_gpio_module_level_update(struct omap2_gpio_s *s, int line)
254 s->ints[line] |= s->dir &
255 ((s->inputs & s->level[1]) | (~s->inputs & s->level[0]));
256 omap2_gpio_module_int_update(s, line);
259 static inline void omap2_gpio_module_int(struct omap2_gpio_s *s, int line)
261 s->ints[0] |= 1 << line;
262 omap2_gpio_module_int_update(s, 0);
263 s->ints[1] |= 1 << line;
264 omap2_gpio_module_int_update(s, 1);
265 omap2_gpio_module_wake(s, line);
268 static void omap2_gpio_set(void *opaque, int line, int level)
270 struct omap2_gpif_s *p = opaque;
271 struct omap2_gpio_s *s = &p->modules[line >> 5];
275 if (s->dir & (1 << line) & ((~s->inputs & s->edge[0]) | s->level[1]))
276 omap2_gpio_module_int(s, line);
277 s->inputs |= 1 << line;
279 if (s->dir & (1 << line) & ((s->inputs & s->edge[1]) | s->level[0]))
280 omap2_gpio_module_int(s, line);
281 s->inputs &= ~(1 << line);
285 static void omap2_gpio_module_reset(struct omap2_gpio_s *s)
303 static uint32_t omap2_gpio_module_read(void *opaque, hwaddr addr)
305 struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
308 case 0x00: /* GPIO_REVISION */
311 case 0x10: /* GPIO_SYSCONFIG */
314 case 0x14: /* GPIO_SYSSTATUS */
317 case 0x18: /* GPIO_IRQSTATUS1 */
320 case 0x1c: /* GPIO_IRQENABLE1 */
321 case 0x60: /* GPIO_CLEARIRQENABLE1 */
322 case 0x64: /* GPIO_SETIRQENABLE1 */
325 case 0x20: /* GPIO_WAKEUPENABLE */
326 case 0x80: /* GPIO_CLEARWKUENA */
327 case 0x84: /* GPIO_SETWKUENA */
330 case 0x28: /* GPIO_IRQSTATUS2 */
333 case 0x2c: /* GPIO_IRQENABLE2 */
334 case 0x70: /* GPIO_CLEARIRQENABLE2 */
335 case 0x74: /* GPIO_SETIREQNEABLE2 */
338 case 0x30: /* GPIO_CTRL */
341 case 0x34: /* GPIO_OE */
344 case 0x38: /* GPIO_DATAIN */
347 case 0x3c: /* GPIO_DATAOUT */
348 case 0x90: /* GPIO_CLEARDATAOUT */
349 case 0x94: /* GPIO_SETDATAOUT */
352 case 0x40: /* GPIO_LEVELDETECT0 */
355 case 0x44: /* GPIO_LEVELDETECT1 */
358 case 0x48: /* GPIO_RISINGDETECT */
361 case 0x4c: /* GPIO_FALLINGDETECT */
364 case 0x50: /* GPIO_DEBOUNCENABLE */
367 case 0x54: /* GPIO_DEBOUNCINGTIME */
375 static void omap2_gpio_module_write(void *opaque, hwaddr addr,
378 struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
383 case 0x00: /* GPIO_REVISION */
384 case 0x14: /* GPIO_SYSSTATUS */
385 case 0x38: /* GPIO_DATAIN */
389 case 0x10: /* GPIO_SYSCONFIG */
390 if (((value >> 3) & 3) == 3)
391 fprintf(stderr, "%s: bad IDLEMODE value\n", __FUNCTION__);
393 omap2_gpio_module_reset(s);
394 s->config[0] = value & 0x1d;
397 case 0x18: /* GPIO_IRQSTATUS1 */
398 if (s->ints[0] & value) {
399 s->ints[0] &= ~value;
400 omap2_gpio_module_level_update(s, 0);
404 case 0x1c: /* GPIO_IRQENABLE1 */
406 omap2_gpio_module_int_update(s, 0);
409 case 0x20: /* GPIO_WAKEUPENABLE */
413 case 0x28: /* GPIO_IRQSTATUS2 */
414 if (s->ints[1] & value) {
415 s->ints[1] &= ~value;
416 omap2_gpio_module_level_update(s, 1);
420 case 0x2c: /* GPIO_IRQENABLE2 */
422 omap2_gpio_module_int_update(s, 1);
425 case 0x30: /* GPIO_CTRL */
426 s->config[1] = value & 7;
429 case 0x34: /* GPIO_OE */
430 diff = s->outputs & (s->dir ^ value);
433 value = s->outputs & ~s->dir;
434 while ((ln = ffs(diff))) {
435 diff &= ~(1 <<-- ln);
436 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
439 omap2_gpio_module_level_update(s, 0);
440 omap2_gpio_module_level_update(s, 1);
443 case 0x3c: /* GPIO_DATAOUT */
444 omap2_gpio_module_out_update(s, s->outputs ^ value);
447 case 0x40: /* GPIO_LEVELDETECT0 */
449 omap2_gpio_module_level_update(s, 0);
450 omap2_gpio_module_level_update(s, 1);
453 case 0x44: /* GPIO_LEVELDETECT1 */
455 omap2_gpio_module_level_update(s, 0);
456 omap2_gpio_module_level_update(s, 1);
459 case 0x48: /* GPIO_RISINGDETECT */
463 case 0x4c: /* GPIO_FALLINGDETECT */
467 case 0x50: /* GPIO_DEBOUNCENABLE */
471 case 0x54: /* GPIO_DEBOUNCINGTIME */
475 case 0x60: /* GPIO_CLEARIRQENABLE1 */
476 s->mask[0] &= ~value;
477 omap2_gpio_module_int_update(s, 0);
480 case 0x64: /* GPIO_SETIRQENABLE1 */
482 omap2_gpio_module_int_update(s, 0);
485 case 0x70: /* GPIO_CLEARIRQENABLE2 */
486 s->mask[1] &= ~value;
487 omap2_gpio_module_int_update(s, 1);
490 case 0x74: /* GPIO_SETIREQNEABLE2 */
492 omap2_gpio_module_int_update(s, 1);
495 case 0x80: /* GPIO_CLEARWKUENA */
499 case 0x84: /* GPIO_SETWKUENA */
503 case 0x90: /* GPIO_CLEARDATAOUT */
504 omap2_gpio_module_out_update(s, s->outputs & value);
507 case 0x94: /* GPIO_SETDATAOUT */
508 omap2_gpio_module_out_update(s, ~s->outputs & value);
517 static uint32_t omap2_gpio_module_readp(void *opaque, hwaddr addr)
519 return omap2_gpio_module_read(opaque, addr & ~3) >> ((addr & 3) << 3);
522 static void omap2_gpio_module_writep(void *opaque, hwaddr addr,
526 uint32_t mask = 0xffff;
529 case 0x00: /* GPIO_REVISION */
530 case 0x14: /* GPIO_SYSSTATUS */
531 case 0x38: /* GPIO_DATAIN */
535 case 0x10: /* GPIO_SYSCONFIG */
536 case 0x1c: /* GPIO_IRQENABLE1 */
537 case 0x20: /* GPIO_WAKEUPENABLE */
538 case 0x2c: /* GPIO_IRQENABLE2 */
539 case 0x30: /* GPIO_CTRL */
540 case 0x34: /* GPIO_OE */
541 case 0x3c: /* GPIO_DATAOUT */
542 case 0x40: /* GPIO_LEVELDETECT0 */
543 case 0x44: /* GPIO_LEVELDETECT1 */
544 case 0x48: /* GPIO_RISINGDETECT */
545 case 0x4c: /* GPIO_FALLINGDETECT */
546 case 0x50: /* GPIO_DEBOUNCENABLE */
547 case 0x54: /* GPIO_DEBOUNCINGTIME */
548 cur = omap2_gpio_module_read(opaque, addr & ~3) &
549 ~(mask << ((addr & 3) << 3));
552 case 0x18: /* GPIO_IRQSTATUS1 */
553 case 0x28: /* GPIO_IRQSTATUS2 */
554 case 0x60: /* GPIO_CLEARIRQENABLE1 */
555 case 0x64: /* GPIO_SETIRQENABLE1 */
556 case 0x70: /* GPIO_CLEARIRQENABLE2 */
557 case 0x74: /* GPIO_SETIREQNEABLE2 */
558 case 0x80: /* GPIO_CLEARWKUENA */
559 case 0x84: /* GPIO_SETWKUENA */
560 case 0x90: /* GPIO_CLEARDATAOUT */
561 case 0x94: /* GPIO_SETDATAOUT */
562 value <<= (addr & 3) << 3;
563 omap2_gpio_module_write(opaque, addr, cur | value);
572 static const MemoryRegionOps omap2_gpio_module_ops = {
575 omap2_gpio_module_readp,
576 omap2_gpio_module_readp,
577 omap2_gpio_module_read,
580 omap2_gpio_module_writep,
581 omap2_gpio_module_writep,
582 omap2_gpio_module_write,
585 .endianness = DEVICE_NATIVE_ENDIAN,
588 static void omap_gpif_reset(DeviceState *dev)
590 struct omap_gpif_s *s = FROM_SYSBUS(struct omap_gpif_s,
591 SYS_BUS_DEVICE(dev));
592 omap_gpio_reset(&s->omap1);
595 static void omap2_gpif_reset(DeviceState *dev)
598 struct omap2_gpif_s *s = FROM_SYSBUS(struct omap2_gpif_s,
599 SYS_BUS_DEVICE(dev));
600 for (i = 0; i < s->modulecount; i++) {
601 omap2_gpio_module_reset(&s->modules[i]);
607 static uint64_t omap2_gpif_top_read(void *opaque, hwaddr addr,
610 struct omap2_gpif_s *s = (struct omap2_gpif_s *) opaque;
613 case 0x00: /* IPGENERICOCPSPL_REVISION */
616 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
619 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
622 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
625 case 0x40: /* IPGENERICOCPSPL_GPO */
628 case 0x50: /* IPGENERICOCPSPL_GPI */
636 static void omap2_gpif_top_write(void *opaque, hwaddr addr,
637 uint64_t value, unsigned size)
639 struct omap2_gpif_s *s = (struct omap2_gpif_s *) opaque;
642 case 0x00: /* IPGENERICOCPSPL_REVISION */
643 case 0x14: /* IPGENERICOCPSPL_SYSSTATUS */
644 case 0x18: /* IPGENERICOCPSPL_IRQSTATUS */
645 case 0x50: /* IPGENERICOCPSPL_GPI */
649 case 0x10: /* IPGENERICOCPSPL_SYSCONFIG */
650 if (value & (1 << 1)) /* SOFTRESET */
651 omap2_gpif_reset(&s->busdev.qdev);
652 s->autoidle = value & 1;
655 case 0x40: /* IPGENERICOCPSPL_GPO */
665 static const MemoryRegionOps omap2_gpif_top_ops = {
666 .read = omap2_gpif_top_read,
667 .write = omap2_gpif_top_write,
668 .endianness = DEVICE_NATIVE_ENDIAN,
671 static int omap_gpio_init(SysBusDevice *dev)
673 struct omap_gpif_s *s = FROM_SYSBUS(struct omap_gpif_s, dev);
675 hw_error("omap-gpio: clk not connected\n");
677 qdev_init_gpio_in(&dev->qdev, omap_gpio_set, 16);
678 qdev_init_gpio_out(&dev->qdev, s->omap1.handler, 16);
679 sysbus_init_irq(dev, &s->omap1.irq);
680 memory_region_init_io(&s->iomem, OBJECT(s), &omap_gpio_ops, &s->omap1,
681 "omap.gpio", 0x1000);
682 sysbus_init_mmio(dev, &s->iomem);
686 static int omap2_gpio_init(SysBusDevice *dev)
689 struct omap2_gpif_s *s = FROM_SYSBUS(struct omap2_gpif_s, dev);
691 hw_error("omap2-gpio: iclk not connected\n");
693 if (s->mpu_model < omap3430) {
694 s->modulecount = (s->mpu_model < omap2430) ? 4 : 5;
695 memory_region_init_io(&s->iomem, OBJECT(s), &omap2_gpif_top_ops, s,
696 "omap2.gpio", 0x1000);
697 sysbus_init_mmio(dev, &s->iomem);
701 s->modules = g_malloc0(s->modulecount * sizeof(struct omap2_gpio_s));
702 s->handler = g_malloc0(s->modulecount * 32 * sizeof(qemu_irq));
703 qdev_init_gpio_in(&dev->qdev, omap2_gpio_set, s->modulecount * 32);
704 qdev_init_gpio_out(&dev->qdev, s->handler, s->modulecount * 32);
705 for (i = 0; i < s->modulecount; i++) {
706 struct omap2_gpio_s *m = &s->modules[i];
708 hw_error("omap2-gpio: fclk%d not connected\n", i);
710 m->revision = (s->mpu_model < omap3430) ? 0x18 : 0x25;
711 m->handler = &s->handler[i * 32];
712 sysbus_init_irq(dev, &m->irq[0]); /* mpu irq */
713 sysbus_init_irq(dev, &m->irq[1]); /* dsp irq */
714 sysbus_init_irq(dev, &m->wkup);
715 memory_region_init_io(&m->iomem, OBJECT(s), &omap2_gpio_module_ops, m,
716 "omap.gpio-module", 0x1000);
717 sysbus_init_mmio(dev, &m->iomem);
722 /* Using qdev pointer properties for the clocks is not ideal.
723 * qdev should support a generic means of defining a 'port' with
724 * an arbitrary interface for connecting two devices. Then we
725 * could reframe the omap clock API in terms of clock ports,
726 * and get some type safety. For now the best qdev provides is
727 * passing an arbitrary pointer.
728 * (It's not possible to pass in the string which is the clock
729 * name, because this device does not have the necessary information
730 * (ie the struct omap_mpu_state_s*) to do the clockname to pointer
734 static Property omap_gpio_properties[] = {
735 DEFINE_PROP_INT32("mpu_model", struct omap_gpif_s, mpu_model, 0),
736 DEFINE_PROP_PTR("clk", struct omap_gpif_s, clk),
737 DEFINE_PROP_END_OF_LIST(),
740 static void omap_gpio_class_init(ObjectClass *klass, void *data)
742 DeviceClass *dc = DEVICE_CLASS(klass);
743 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
745 k->init = omap_gpio_init;
746 dc->reset = omap_gpif_reset;
747 dc->props = omap_gpio_properties;
750 static const TypeInfo omap_gpio_info = {
752 .parent = TYPE_SYS_BUS_DEVICE,
753 .instance_size = sizeof(struct omap_gpif_s),
754 .class_init = omap_gpio_class_init,
757 static Property omap2_gpio_properties[] = {
758 DEFINE_PROP_INT32("mpu_model", struct omap2_gpif_s, mpu_model, 0),
759 DEFINE_PROP_PTR("iclk", struct omap2_gpif_s, iclk),
760 DEFINE_PROP_PTR("fclk0", struct omap2_gpif_s, fclk[0]),
761 DEFINE_PROP_PTR("fclk1", struct omap2_gpif_s, fclk[1]),
762 DEFINE_PROP_PTR("fclk2", struct omap2_gpif_s, fclk[2]),
763 DEFINE_PROP_PTR("fclk3", struct omap2_gpif_s, fclk[3]),
764 DEFINE_PROP_PTR("fclk4", struct omap2_gpif_s, fclk[4]),
765 DEFINE_PROP_PTR("fclk5", struct omap2_gpif_s, fclk[5]),
766 DEFINE_PROP_END_OF_LIST(),
769 static void omap2_gpio_class_init(ObjectClass *klass, void *data)
771 DeviceClass *dc = DEVICE_CLASS(klass);
772 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
774 k->init = omap2_gpio_init;
775 dc->reset = omap2_gpif_reset;
776 dc->props = omap2_gpio_properties;
779 static const TypeInfo omap2_gpio_info = {
780 .name = "omap2-gpio",
781 .parent = TYPE_SYS_BUS_DEVICE,
782 .instance_size = sizeof(struct omap2_gpif_s),
783 .class_init = omap2_gpio_class_init,
786 static void omap_gpio_register_types(void)
788 type_register_static(&omap_gpio_info);
789 type_register_static(&omap2_gpio_info);
792 type_init(omap_gpio_register_types)