Merge remote-tracking branch 'remotes/afaerber/tags/qom-devices-for-peter' into staging
[qemu/ar7.git] / hw / intc / etraxfs_pic.c
blobbd588681ef5e10e2a9c2dd743f25f805119734fd
1 /*
2 * QEMU ETRAX Interrupt Controller.
4 * Copyright (c) 2008 Edgar E. Iglesias, Axis Communications AB.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "hw/sysbus.h"
26 #include "hw/hw.h"
27 //#include "pc.h"
28 //#include "etraxfs.h"
30 #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
39 #define TYPE_ETRAX_FS_PIC "etraxfs,pic"
40 #define ETRAX_FS_PIC(obj) \
41 OBJECT_CHECK(struct etrax_pic, (obj), TYPE_ETRAX_FS_PIC)
43 struct etrax_pic
45 SysBusDevice parent_obj;
47 MemoryRegion mmio;
48 void *interrupt_vector;
49 qemu_irq parent_irq;
50 qemu_irq parent_nmi;
51 uint32_t regs[R_MAX];
54 static void pic_update(struct etrax_pic *fs)
56 uint32_t vector = 0;
57 int i;
59 fs->regs[R_R_MASKED_VECT] = fs->regs[R_R_VECT] & fs->regs[R_RW_MASK];
61 /* The ETRAX interrupt controller signals interrupts to the core
62 through an interrupt request wire and an irq vector bus. If
63 multiple interrupts are simultaneously active it chooses vector
64 0x30 and lets the sw choose the priorities. */
65 if (fs->regs[R_R_MASKED_VECT]) {
66 uint32_t mv = fs->regs[R_R_MASKED_VECT];
67 for (i = 0; i < 31; i++) {
68 if (mv & 1) {
69 vector = 0x31 + i;
70 /* Check for multiple interrupts. */
71 if (mv > 1)
72 vector = 0x30;
73 break;
75 mv >>= 1;
79 if (fs->interrupt_vector) {
80 /* hack alert: ptr property */
81 *(uint32_t*)(fs->interrupt_vector) = vector;
83 qemu_set_irq(fs->parent_irq, !!vector);
86 static uint64_t
87 pic_read(void *opaque, hwaddr addr, unsigned int size)
89 struct etrax_pic *fs = opaque;
90 uint32_t rval;
92 rval = fs->regs[addr >> 2];
93 D(printf("%s %x=%x\n", __func__, addr, rval));
94 return rval;
97 static void pic_write(void *opaque, hwaddr addr,
98 uint64_t value, unsigned int size)
100 struct etrax_pic *fs = opaque;
101 D(printf("%s addr=%x val=%x\n", __func__, addr, value));
103 if (addr == R_RW_MASK) {
104 fs->regs[R_RW_MASK] = value;
105 pic_update(fs);
109 static const MemoryRegionOps pic_ops = {
110 .read = pic_read,
111 .write = pic_write,
112 .endianness = DEVICE_NATIVE_ENDIAN,
113 .valid = {
114 .min_access_size = 4,
115 .max_access_size = 4
119 static void nmi_handler(void *opaque, int irq, int level)
121 struct etrax_pic *fs = (void *)opaque;
122 uint32_t mask;
124 mask = 1 << irq;
125 if (level)
126 fs->regs[R_R_NMI] |= mask;
127 else
128 fs->regs[R_R_NMI] &= ~mask;
130 qemu_set_irq(fs->parent_nmi, !!fs->regs[R_R_NMI]);
133 static void irq_handler(void *opaque, int irq, int level)
135 struct etrax_pic *fs = (void *)opaque;
137 if (irq >= 30) {
138 nmi_handler(opaque, irq, level);
139 return;
142 irq -= 1;
143 fs->regs[R_R_VECT] &= ~(1 << irq);
144 fs->regs[R_R_VECT] |= (!!level << irq);
145 pic_update(fs);
148 static int etraxfs_pic_init(SysBusDevice *sbd)
150 DeviceState *dev = DEVICE(sbd);
151 struct etrax_pic *s = ETRAX_FS_PIC(dev);
153 qdev_init_gpio_in(dev, irq_handler, 32);
154 sysbus_init_irq(sbd, &s->parent_irq);
155 sysbus_init_irq(sbd, &s->parent_nmi);
157 memory_region_init_io(&s->mmio, OBJECT(s), &pic_ops, s,
158 "etraxfs-pic", R_MAX * 4);
159 sysbus_init_mmio(sbd, &s->mmio);
160 return 0;
163 static Property etraxfs_pic_properties[] = {
164 DEFINE_PROP_PTR("interrupt_vector", struct etrax_pic, interrupt_vector),
165 DEFINE_PROP_END_OF_LIST(),
168 static void etraxfs_pic_class_init(ObjectClass *klass, void *data)
170 DeviceClass *dc = DEVICE_CLASS(klass);
171 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
173 k->init = etraxfs_pic_init;
174 dc->props = etraxfs_pic_properties;
176 * Note: pointer property "interrupt_vector" may remain null, thus
177 * no need for dc->cannot_instantiate_with_device_add_yet = true;
181 static const TypeInfo etraxfs_pic_info = {
182 .name = TYPE_ETRAX_FS_PIC,
183 .parent = TYPE_SYS_BUS_DEVICE,
184 .instance_size = sizeof(struct etrax_pic),
185 .class_init = etraxfs_pic_class_init,
188 static void etraxfs_pic_register_types(void)
190 type_register_static(&etraxfs_pic_info);
193 type_init(etraxfs_pic_register_types)