2
0

puv3_intc.c 3.6 KB

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