puv3_intc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "qemu/osdep.h"
  12. #include "hw/irq.h"
  13. #include "hw/sysbus.h"
  14. #undef DEBUG_PUV3
  15. #include "hw/unicore32/puv3.h"
  16. #include "qemu/module.h"
  17. #define TYPE_PUV3_INTC "puv3_intc"
  18. #define PUV3_INTC(obj) OBJECT_CHECK(PUV3INTCState, (obj), TYPE_PUV3_INTC)
  19. typedef struct PUV3INTCState {
  20. SysBusDevice parent_obj;
  21. MemoryRegion iomem;
  22. qemu_irq parent_irq;
  23. uint32_t reg_ICMR;
  24. uint32_t reg_ICPR;
  25. } PUV3INTCState;
  26. /* Update interrupt status after enabled or pending bits have been changed. */
  27. static void puv3_intc_update(PUV3INTCState *s)
  28. {
  29. if (s->reg_ICMR & s->reg_ICPR) {
  30. qemu_irq_raise(s->parent_irq);
  31. } else {
  32. qemu_irq_lower(s->parent_irq);
  33. }
  34. }
  35. /* Process a change in an external INTC input. */
  36. static void puv3_intc_handler(void *opaque, int irq, int level)
  37. {
  38. PUV3INTCState *s = opaque;
  39. DPRINTF("irq 0x%x, level 0x%x\n", irq, level);
  40. if (level) {
  41. s->reg_ICPR |= (1 << irq);
  42. } else {
  43. s->reg_ICPR &= ~(1 << irq);
  44. }
  45. puv3_intc_update(s);
  46. }
  47. static uint64_t puv3_intc_read(void *opaque, hwaddr offset,
  48. unsigned size)
  49. {
  50. PUV3INTCState *s = opaque;
  51. uint32_t ret = 0;
  52. switch (offset) {
  53. case 0x04: /* INTC_ICMR */
  54. ret = s->reg_ICMR;
  55. break;
  56. case 0x0c: /* INTC_ICIP */
  57. ret = s->reg_ICPR; /* the same value with ICPR */
  58. break;
  59. default:
  60. DPRINTF("Bad offset %x\n", (int)offset);
  61. }
  62. DPRINTF("offset 0x%x, value 0x%x\n", offset, ret);
  63. return ret;
  64. }
  65. static void puv3_intc_write(void *opaque, hwaddr offset,
  66. uint64_t value, unsigned size)
  67. {
  68. PUV3INTCState *s = opaque;
  69. DPRINTF("offset 0x%x, value 0x%x\n", offset, value);
  70. switch (offset) {
  71. case 0x00: /* INTC_ICLR */
  72. case 0x14: /* INTC_ICCR */
  73. break;
  74. case 0x04: /* INTC_ICMR */
  75. s->reg_ICMR = value;
  76. break;
  77. default:
  78. DPRINTF("Bad offset 0x%x\n", (int)offset);
  79. return;
  80. }
  81. puv3_intc_update(s);
  82. }
  83. static const MemoryRegionOps puv3_intc_ops = {
  84. .read = puv3_intc_read,
  85. .write = puv3_intc_write,
  86. .impl = {
  87. .min_access_size = 4,
  88. .max_access_size = 4,
  89. },
  90. .endianness = DEVICE_NATIVE_ENDIAN,
  91. };
  92. static void puv3_intc_realize(DeviceState *dev, Error **errp)
  93. {
  94. PUV3INTCState *s = PUV3_INTC(dev);
  95. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  96. qdev_init_gpio_in(dev, puv3_intc_handler, PUV3_IRQS_NR);
  97. sysbus_init_irq(sbd, &s->parent_irq);
  98. s->reg_ICMR = 0;
  99. s->reg_ICPR = 0;
  100. memory_region_init_io(&s->iomem, OBJECT(s), &puv3_intc_ops, s, "puv3_intc",
  101. PUV3_REGS_OFFSET);
  102. sysbus_init_mmio(sbd, &s->iomem);
  103. }
  104. static void puv3_intc_class_init(ObjectClass *klass, void *data)
  105. {
  106. DeviceClass *dc = DEVICE_CLASS(klass);
  107. dc->realize = puv3_intc_realize;
  108. }
  109. static const TypeInfo puv3_intc_info = {
  110. .name = TYPE_PUV3_INTC,
  111. .parent = TYPE_SYS_BUS_DEVICE,
  112. .instance_size = sizeof(PUV3INTCState),
  113. .class_init = puv3_intc_class_init,
  114. };
  115. static void puv3_intc_register_type(void)
  116. {
  117. type_register_static(&puv3_intc_info);
  118. }
  119. type_init(puv3_intc_register_type)