xilinx_intc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * QEMU Xilinx OPB Interrupt Controller.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "sysbus.h"
  25. #include "hw.h"
  26. #define D(x)
  27. #define R_ISR 0
  28. #define R_IPR 1
  29. #define R_IER 2
  30. #define R_IAR 3
  31. #define R_SIE 4
  32. #define R_CIE 5
  33. #define R_IVR 6
  34. #define R_MER 7
  35. #define R_MAX 8
  36. struct xlx_pic
  37. {
  38. SysBusDevice busdev;
  39. qemu_irq parent_irq;
  40. /* Configuration reg chosen at synthesis-time. QEMU populates
  41. the bits at board-setup. */
  42. uint32_t c_kind_of_intr;
  43. /* Runtime control registers. */
  44. uint32_t regs[R_MAX];
  45. };
  46. static void update_irq(struct xlx_pic *p)
  47. {
  48. uint32_t i;
  49. /* Update the pending register. */
  50. p->regs[R_IPR] = p->regs[R_ISR] & p->regs[R_IER];
  51. /* Update the vector register. */
  52. for (i = 0; i < 32; i++) {
  53. if (p->regs[R_IPR] & (1 << i))
  54. break;
  55. }
  56. if (i == 32)
  57. i = ~0;
  58. p->regs[R_IVR] = i;
  59. if ((p->regs[R_MER] & 1) && p->regs[R_IPR]) {
  60. qemu_irq_raise(p->parent_irq);
  61. } else {
  62. qemu_irq_lower(p->parent_irq);
  63. }
  64. }
  65. static uint32_t pic_readl (void *opaque, target_phys_addr_t addr)
  66. {
  67. struct xlx_pic *p = opaque;
  68. uint32_t r = 0;
  69. addr >>= 2;
  70. switch (addr)
  71. {
  72. default:
  73. if (addr < ARRAY_SIZE(p->regs))
  74. r = p->regs[addr];
  75. break;
  76. }
  77. D(printf("%s %x=%x\n", __func__, addr * 4, r));
  78. return r;
  79. }
  80. static void
  81. pic_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  82. {
  83. struct xlx_pic *p = opaque;
  84. addr >>= 2;
  85. D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
  86. switch (addr)
  87. {
  88. case R_IAR:
  89. p->regs[R_ISR] &= ~value; /* ACK. */
  90. break;
  91. case R_SIE:
  92. p->regs[R_IER] |= value; /* Atomic set ie. */
  93. break;
  94. case R_CIE:
  95. p->regs[R_IER] &= ~value; /* Atomic clear ie. */
  96. break;
  97. default:
  98. if (addr < ARRAY_SIZE(p->regs))
  99. p->regs[addr] = value;
  100. break;
  101. }
  102. update_irq(p);
  103. }
  104. static CPUReadMemoryFunc * const pic_read[] = {
  105. NULL, NULL,
  106. &pic_readl,
  107. };
  108. static CPUWriteMemoryFunc * const pic_write[] = {
  109. NULL, NULL,
  110. &pic_writel,
  111. };
  112. static void irq_handler(void *opaque, int irq, int level)
  113. {
  114. struct xlx_pic *p = opaque;
  115. if (!(p->regs[R_MER] & 2)) {
  116. qemu_irq_lower(p->parent_irq);
  117. return;
  118. }
  119. /* Update source flops. Don't clear unless level triggered.
  120. Edge triggered interrupts only go away when explicitely acked to
  121. the interrupt controller. */
  122. if (!(p->c_kind_of_intr & (1 << irq)) || level) {
  123. p->regs[R_ISR] &= ~(1 << irq);
  124. p->regs[R_ISR] |= (level << irq);
  125. }
  126. update_irq(p);
  127. }
  128. static int xilinx_intc_init(SysBusDevice *dev)
  129. {
  130. struct xlx_pic *p = FROM_SYSBUS(typeof (*p), dev);
  131. int pic_regs;
  132. qdev_init_gpio_in(&dev->qdev, irq_handler, 32);
  133. sysbus_init_irq(dev, &p->parent_irq);
  134. pic_regs = cpu_register_io_memory(pic_read, pic_write, p, DEVICE_NATIVE_ENDIAN);
  135. sysbus_init_mmio(dev, R_MAX * 4, pic_regs);
  136. return 0;
  137. }
  138. static SysBusDeviceInfo xilinx_intc_info = {
  139. .init = xilinx_intc_init,
  140. .qdev.name = "xilinx,intc",
  141. .qdev.size = sizeof(struct xlx_pic),
  142. .qdev.props = (Property[]) {
  143. DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0),
  144. DEFINE_PROP_END_OF_LIST(),
  145. }
  146. };
  147. static void xilinx_intc_register(void)
  148. {
  149. sysbus_register_withprop(&xilinx_intc_info);
  150. }
  151. device_init(xilinx_intc_register)