puv3_intc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * INTC device simulation in PKUnity SoC
  3. *
  4. * Copyright (C) 2010-2012 Guan Xuetao
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation, or any later version.
  9. * See the COPYING file in the top-level directory.
  10. */
  11. #include "sysbus.h"
  12. #undef DEBUG_PUV3
  13. #include "puv3.h"
  14. typedef struct {
  15. SysBusDevice busdev;
  16. MemoryRegion iomem;
  17. qemu_irq parent_irq;
  18. uint32_t reg_ICMR;
  19. uint32_t reg_ICPR;
  20. } PUV3INTCState;
  21. /* Update interrupt status after enabled or pending bits have been changed. */
  22. static void puv3_intc_update(PUV3INTCState *s)
  23. {
  24. if (s->reg_ICMR & s->reg_ICPR) {
  25. qemu_irq_raise(s->parent_irq);
  26. } else {
  27. qemu_irq_lower(s->parent_irq);
  28. }
  29. }
  30. /* Process a change in an external INTC input. */
  31. static void puv3_intc_handler(void *opaque, int irq, int level)
  32. {
  33. PUV3INTCState *s = opaque;
  34. DPRINTF("irq 0x%x, level 0x%x\n", irq, level);
  35. if (level) {
  36. s->reg_ICPR |= (1 << irq);
  37. } else {
  38. s->reg_ICPR &= ~(1 << irq);
  39. }
  40. puv3_intc_update(s);
  41. }
  42. static uint64_t puv3_intc_read(void *opaque, hwaddr offset,
  43. unsigned size)
  44. {
  45. PUV3INTCState *s = opaque;
  46. uint32_t ret = 0;
  47. switch (offset) {
  48. case 0x04: /* INTC_ICMR */
  49. ret = s->reg_ICMR;
  50. break;
  51. case 0x0c: /* INTC_ICIP */
  52. ret = s->reg_ICPR; /* the same value with ICPR */
  53. break;
  54. default:
  55. DPRINTF("Bad offset %x\n", (int)offset);
  56. }
  57. DPRINTF("offset 0x%x, value 0x%x\n", offset, ret);
  58. return ret;
  59. }
  60. static void puv3_intc_write(void *opaque, hwaddr offset,
  61. uint64_t value, unsigned size)
  62. {
  63. PUV3INTCState *s = opaque;
  64. DPRINTF("offset 0x%x, value 0x%x\n", offset, value);
  65. switch (offset) {
  66. case 0x00: /* INTC_ICLR */
  67. case 0x14: /* INTC_ICCR */
  68. break;
  69. case 0x04: /* INTC_ICMR */
  70. s->reg_ICMR = value;
  71. break;
  72. default:
  73. DPRINTF("Bad offset 0x%x\n", (int)offset);
  74. return;
  75. }
  76. puv3_intc_update(s);
  77. }
  78. static const MemoryRegionOps puv3_intc_ops = {
  79. .read = puv3_intc_read,
  80. .write = puv3_intc_write,
  81. .impl = {
  82. .min_access_size = 4,
  83. .max_access_size = 4,
  84. },
  85. .endianness = DEVICE_NATIVE_ENDIAN,
  86. };
  87. static int puv3_intc_init(SysBusDevice *dev)
  88. {
  89. PUV3INTCState *s = FROM_SYSBUS(PUV3INTCState, dev);
  90. qdev_init_gpio_in(&s->busdev.qdev, puv3_intc_handler, PUV3_IRQS_NR);
  91. sysbus_init_irq(&s->busdev, &s->parent_irq);
  92. s->reg_ICMR = 0;
  93. s->reg_ICPR = 0;
  94. memory_region_init_io(&s->iomem, &puv3_intc_ops, s, "puv3_intc",
  95. PUV3_REGS_OFFSET);
  96. sysbus_init_mmio(dev, &s->iomem);
  97. return 0;
  98. }
  99. static void puv3_intc_class_init(ObjectClass *klass, void *data)
  100. {
  101. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  102. sdc->init = puv3_intc_init;
  103. }
  104. static const TypeInfo puv3_intc_info = {
  105. .name = "puv3_intc",
  106. .parent = TYPE_SYS_BUS_DEVICE,
  107. .instance_size = sizeof(PUV3INTCState),
  108. .class_init = puv3_intc_class_init,
  109. };
  110. static void puv3_intc_register_type(void)
  111. {
  112. type_register_static(&puv3_intc_info);
  113. }
  114. type_init(puv3_intc_register_type)