etraxfs_pic.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * QEMU ETRAX Interrupt Controller.
  3. *
  4. * Copyright (c) 2008 Edgar E. Iglesias, Axis Communications AB.
  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 "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "qemu/module.h"
  27. #include "hw/irq.h"
  28. #include "hw/qdev-properties.h"
  29. //#include "pc.h"
  30. //#include "etraxfs.h"
  31. #define D(x)
  32. #define R_RW_MASK 0
  33. #define R_R_VECT 1
  34. #define R_R_MASKED_VECT 2
  35. #define R_R_NMI 3
  36. #define R_R_GURU 4
  37. #define R_MAX 5
  38. #define TYPE_ETRAX_FS_PIC "etraxfs,pic"
  39. #define ETRAX_FS_PIC(obj) \
  40. OBJECT_CHECK(struct etrax_pic, (obj), TYPE_ETRAX_FS_PIC)
  41. struct etrax_pic
  42. {
  43. SysBusDevice parent_obj;
  44. MemoryRegion mmio;
  45. void *interrupt_vector;
  46. qemu_irq parent_irq;
  47. qemu_irq parent_nmi;
  48. uint32_t regs[R_MAX];
  49. };
  50. static void pic_update(struct etrax_pic *fs)
  51. {
  52. uint32_t vector = 0;
  53. int i;
  54. fs->regs[R_R_MASKED_VECT] = fs->regs[R_R_VECT] & fs->regs[R_RW_MASK];
  55. /* The ETRAX interrupt controller signals interrupts to the core
  56. through an interrupt request wire and an irq vector bus. If
  57. multiple interrupts are simultaneously active it chooses vector
  58. 0x30 and lets the sw choose the priorities. */
  59. if (fs->regs[R_R_MASKED_VECT]) {
  60. uint32_t mv = fs->regs[R_R_MASKED_VECT];
  61. for (i = 0; i < 31; i++) {
  62. if (mv & 1) {
  63. vector = 0x31 + i;
  64. /* Check for multiple interrupts. */
  65. if (mv > 1)
  66. vector = 0x30;
  67. break;
  68. }
  69. mv >>= 1;
  70. }
  71. }
  72. if (fs->interrupt_vector) {
  73. /* hack alert: ptr property */
  74. *(uint32_t*)(fs->interrupt_vector) = vector;
  75. }
  76. qemu_set_irq(fs->parent_irq, !!vector);
  77. }
  78. static uint64_t
  79. pic_read(void *opaque, hwaddr addr, unsigned int size)
  80. {
  81. struct etrax_pic *fs = opaque;
  82. uint32_t rval;
  83. rval = fs->regs[addr >> 2];
  84. D(printf("%s %x=%x\n", __func__, addr, rval));
  85. return rval;
  86. }
  87. static void pic_write(void *opaque, hwaddr addr,
  88. uint64_t value, unsigned int size)
  89. {
  90. struct etrax_pic *fs = opaque;
  91. D(printf("%s addr=%x val=%x\n", __func__, addr, value));
  92. if (addr == R_RW_MASK) {
  93. fs->regs[R_RW_MASK] = value;
  94. pic_update(fs);
  95. }
  96. }
  97. static const MemoryRegionOps pic_ops = {
  98. .read = pic_read,
  99. .write = pic_write,
  100. .endianness = DEVICE_NATIVE_ENDIAN,
  101. .valid = {
  102. .min_access_size = 4,
  103. .max_access_size = 4
  104. }
  105. };
  106. static void nmi_handler(void *opaque, int irq, int level)
  107. {
  108. struct etrax_pic *fs = (void *)opaque;
  109. uint32_t mask;
  110. mask = 1 << irq;
  111. if (level)
  112. fs->regs[R_R_NMI] |= mask;
  113. else
  114. fs->regs[R_R_NMI] &= ~mask;
  115. qemu_set_irq(fs->parent_nmi, !!fs->regs[R_R_NMI]);
  116. }
  117. static void irq_handler(void *opaque, int irq, int level)
  118. {
  119. struct etrax_pic *fs = (void *)opaque;
  120. if (irq >= 30) {
  121. nmi_handler(opaque, irq, level);
  122. return;
  123. }
  124. irq -= 1;
  125. fs->regs[R_R_VECT] &= ~(1 << irq);
  126. fs->regs[R_R_VECT] |= (!!level << irq);
  127. pic_update(fs);
  128. }
  129. static void etraxfs_pic_init(Object *obj)
  130. {
  131. DeviceState *dev = DEVICE(obj);
  132. struct etrax_pic *s = ETRAX_FS_PIC(obj);
  133. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  134. qdev_init_gpio_in(dev, irq_handler, 32);
  135. sysbus_init_irq(sbd, &s->parent_irq);
  136. sysbus_init_irq(sbd, &s->parent_nmi);
  137. memory_region_init_io(&s->mmio, obj, &pic_ops, s,
  138. "etraxfs-pic", R_MAX * 4);
  139. sysbus_init_mmio(sbd, &s->mmio);
  140. }
  141. static Property etraxfs_pic_properties[] = {
  142. DEFINE_PROP_PTR("interrupt_vector", struct etrax_pic, interrupt_vector),
  143. DEFINE_PROP_END_OF_LIST(),
  144. };
  145. static void etraxfs_pic_class_init(ObjectClass *klass, void *data)
  146. {
  147. DeviceClass *dc = DEVICE_CLASS(klass);
  148. dc->props = etraxfs_pic_properties;
  149. /*
  150. * Note: pointer property "interrupt_vector" may remain null, thus
  151. * no need for dc->user_creatable = false;
  152. */
  153. }
  154. static const TypeInfo etraxfs_pic_info = {
  155. .name = TYPE_ETRAX_FS_PIC,
  156. .parent = TYPE_SYS_BUS_DEVICE,
  157. .instance_size = sizeof(struct etrax_pic),
  158. .instance_init = etraxfs_pic_init,
  159. .class_init = etraxfs_pic_class_init,
  160. };
  161. static void etraxfs_pic_register_types(void)
  162. {
  163. type_register_static(&etraxfs_pic_info);
  164. }
  165. type_init(etraxfs_pic_register_types)