edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 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 | |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 25 | #include "sysbus.h" |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 26 | #include "hw.h" |
Paul Brook | 1ad2134 | 2009-05-19 16:17:58 +0100 | [diff] [blame] | 27 | //#include "pc.h" |
| 28 | //#include "etraxfs.h" |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 29 | |
| 30 | #define D(x) |
| 31 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 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 |
Edgar E. Iglesias | 8d13fcc | 2009-05-05 12:38:39 +0200 | [diff] [blame] | 38 | |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 39 | struct etrax_pic |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 40 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 41 | SysBusDevice busdev; |
Edgar E. Iglesias | 5dd25f3 | 2011-08-11 13:47:44 +0200 | [diff] [blame] | 42 | MemoryRegion mmio; |
Gerd Hoffmann | ddde095 | 2009-08-03 17:35:24 +0200 | [diff] [blame] | 43 | void *interrupt_vector; |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 44 | qemu_irq parent_irq; |
| 45 | qemu_irq parent_nmi; |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 46 | uint32_t regs[R_MAX]; |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 49 | static void pic_update(struct etrax_pic *fs) |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 50 | { |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 51 | uint32_t vector = 0; |
| 52 | int i; |
edgar_igl | 70ea255 | 2009-01-07 13:30:41 +0000 | [diff] [blame] | 53 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 54 | fs->regs[R_R_MASKED_VECT] = fs->regs[R_R_VECT] & fs->regs[R_RW_MASK]; |
edgar_igl | 70ea255 | 2009-01-07 13:30:41 +0000 | [diff] [blame] | 55 | |
Dong Xu Wang | 66a0a2c | 2011-11-29 16:52:39 +0800 | [diff] [blame] | 56 | /* The ETRAX interrupt controller signals interrupts to the core |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 57 | through an interrupt request wire and an irq vector bus. If |
| 58 | multiple interrupts are simultaneously active it chooses vector |
| 59 | 0x30 and lets the sw choose the priorities. */ |
| 60 | if (fs->regs[R_R_MASKED_VECT]) { |
| 61 | uint32_t mv = fs->regs[R_R_MASKED_VECT]; |
| 62 | for (i = 0; i < 31; i++) { |
| 63 | if (mv & 1) { |
| 64 | vector = 0x31 + i; |
| 65 | /* Check for multiple interrupts. */ |
| 66 | if (mv > 1) |
| 67 | vector = 0x30; |
| 68 | break; |
| 69 | } |
| 70 | mv >>= 1; |
| 71 | } |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 72 | } |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 73 | |
| 74 | if (fs->interrupt_vector) { |
Gerd Hoffmann | ddde095 | 2009-08-03 17:35:24 +0200 | [diff] [blame] | 75 | /* hack alert: ptr property */ |
| 76 | *(uint32_t*)(fs->interrupt_vector) = vector; |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 77 | } |
| 78 | qemu_set_irq(fs->parent_irq, !!vector); |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Edgar E. Iglesias | 5dd25f3 | 2011-08-11 13:47:44 +0200 | [diff] [blame] | 81 | static uint64_t |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 82 | pic_read(void *opaque, hwaddr addr, unsigned int size) |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 83 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 84 | struct etrax_pic *fs = opaque; |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 85 | uint32_t rval; |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 86 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 87 | rval = fs->regs[addr >> 2]; |
| 88 | D(printf("%s %x=%x\n", __func__, addr, rval)); |
| 89 | return rval; |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 92 | static void pic_write(void *opaque, hwaddr addr, |
Edgar E. Iglesias | 5dd25f3 | 2011-08-11 13:47:44 +0200 | [diff] [blame] | 93 | uint64_t value, unsigned int size) |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 94 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 95 | struct etrax_pic *fs = opaque; |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 96 | D(printf("%s addr=%x val=%x\n", __func__, addr, value)); |
Edgar E. Iglesias | 8d13fcc | 2009-05-05 12:38:39 +0200 | [diff] [blame] | 97 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 98 | if (addr == R_RW_MASK) { |
| 99 | fs->regs[R_RW_MASK] = value; |
| 100 | pic_update(fs); |
| 101 | } |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Edgar E. Iglesias | 5dd25f3 | 2011-08-11 13:47:44 +0200 | [diff] [blame] | 104 | static const MemoryRegionOps pic_ops = { |
| 105 | .read = pic_read, |
| 106 | .write = pic_write, |
| 107 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 108 | .valid = { |
| 109 | .min_access_size = 4, |
| 110 | .max_access_size = 4 |
| 111 | } |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
edgar_igl | 5ef98b4 | 2008-06-09 23:33:30 +0000 | [diff] [blame] | 114 | static void nmi_handler(void *opaque, int irq, int level) |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 115 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 116 | struct etrax_pic *fs = (void *)opaque; |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 117 | uint32_t mask; |
edgar_igl | 5ef98b4 | 2008-06-09 23:33:30 +0000 | [diff] [blame] | 118 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 119 | mask = 1 << irq; |
| 120 | if (level) |
| 121 | fs->regs[R_R_NMI] |= mask; |
| 122 | else |
| 123 | fs->regs[R_R_NMI] &= ~mask; |
edgar_igl | 5ef98b4 | 2008-06-09 23:33:30 +0000 | [diff] [blame] | 124 | |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 125 | qemu_set_irq(fs->parent_nmi, !!fs->regs[R_R_NMI]); |
edgar_igl | 5ef98b4 | 2008-06-09 23:33:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Edgar E. Iglesias | 73cfd29 | 2009-05-16 00:23:15 +0200 | [diff] [blame] | 128 | static void irq_handler(void *opaque, int irq, int level) |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 129 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 130 | struct etrax_pic *fs = (void *)opaque; |
Edgar E. Iglesias | 73cfd29 | 2009-05-16 00:23:15 +0200 | [diff] [blame] | 131 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 132 | if (irq >= 30) |
| 133 | return nmi_handler(opaque, irq, level); |
Edgar E. Iglesias | 73cfd29 | 2009-05-16 00:23:15 +0200 | [diff] [blame] | 134 | |
Edgar E. Iglesias | 979d98c | 2009-05-16 12:28:33 +0200 | [diff] [blame] | 135 | irq -= 1; |
| 136 | fs->regs[R_R_VECT] &= ~(1 << irq); |
| 137 | fs->regs[R_R_VECT] |= (!!level << irq); |
| 138 | pic_update(fs); |
edgar_igl | 5ef98b4 | 2008-06-09 23:33:30 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 141 | static int etraxfs_pic_init(SysBusDevice *dev) |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 142 | { |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 143 | struct etrax_pic *s = FROM_SYSBUS(typeof (*s), dev); |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 144 | |
Paul Brook | 067a3dd | 2009-05-26 14:56:11 +0100 | [diff] [blame] | 145 | qdev_init_gpio_in(&dev->qdev, irq_handler, 32); |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 146 | sysbus_init_irq(dev, &s->parent_irq); |
| 147 | sysbus_init_irq(dev, &s->parent_nmi); |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 148 | |
Edgar E. Iglesias | 5dd25f3 | 2011-08-11 13:47:44 +0200 | [diff] [blame] | 149 | memory_region_init_io(&s->mmio, &pic_ops, s, "etraxfs-pic", R_MAX * 4); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 150 | sysbus_init_mmio(dev, &s->mmio); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 151 | return 0; |
edgar_igl | e62b5b1 | 2008-03-14 01:04:24 +0000 | [diff] [blame] | 152 | } |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 153 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 154 | static Property etraxfs_pic_properties[] = { |
| 155 | DEFINE_PROP_PTR("interrupt_vector", struct etrax_pic, interrupt_vector), |
| 156 | DEFINE_PROP_END_OF_LIST(), |
| 157 | }; |
| 158 | |
| 159 | static void etraxfs_pic_class_init(ObjectClass *klass, void *data) |
| 160 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 161 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 162 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
| 163 | |
| 164 | k->init = etraxfs_pic_init; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 165 | dc->props = etraxfs_pic_properties; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 168 | static TypeInfo etraxfs_pic_info = { |
| 169 | .name = "etraxfs,pic", |
| 170 | .parent = TYPE_SYS_BUS_DEVICE, |
| 171 | .instance_size = sizeof(struct etrax_pic), |
| 172 | .class_init = etraxfs_pic_class_init, |
Gerd Hoffmann | ee6847d | 2009-07-15 13:43:31 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 175 | static void etraxfs_pic_register_types(void) |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 176 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 177 | type_register_static(&etraxfs_pic_info); |
Edgar E. Iglesias | fd6dc90 | 2009-05-18 22:24:22 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 180 | type_init(etraxfs_pic_register_types) |