2 * Syborg interrupt controller.
4 * Copyright (c) 2008 CodeSourcery
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
28 //#define DEBUG_SYBORG_INT
30 #ifdef DEBUG_SYBORG_INT
31 #define DPRINTF(fmt, ...) \
32 do { printf("syborg_int: " fmt , ## __VA_ARGS__); } while (0)
33 #define BADF(fmt, ...) \
34 do { fprintf(stderr, "syborg_int: error: " fmt , ## __VA_ARGS__); \
37 #define DPRINTF(fmt, ...) do {} while(0)
38 #define BADF(fmt, ...) \
39 do { fprintf(stderr, "syborg_int: error: " fmt , ## __VA_ARGS__);} while (0)
43 INT_STATUS
= 1, /* number of pending interrupts */
44 INT_CURRENT
= 2, /* next interrupt to be serviced */
60 syborg_int_flags
*flags
;
64 static void syborg_int_update(SyborgIntState
*s
)
66 DPRINTF("pending %d\n", s
->pending_count
);
67 qemu_set_irq(s
->parent_irq
, s
->pending_count
> 0);
70 static void syborg_int_set_irq(void *opaque
, int irq
, int level
)
72 SyborgIntState
*s
= (SyborgIntState
*)opaque
;
74 if (s
->flags
[irq
].level
== level
)
77 s
->flags
[irq
].level
= level
;
78 if (s
->flags
[irq
].enabled
) {
87 static uint32_t syborg_int_read(void *opaque
, target_phys_addr_t offset
)
89 SyborgIntState
*s
= (SyborgIntState
*)opaque
;
93 switch (offset
>> 2) {
97 DPRINTF("read status=%d\n", s
->pending_count
);
98 return s
->pending_count
;
101 for (i
= 0; i
< s
->num_irqs
; i
++) {
102 if (s
->flags
[i
].level
& s
->flags
[i
].enabled
) {
103 DPRINTF("read current=%d\n", i
);
107 DPRINTF("read current=none\n");
111 cpu_abort(cpu_single_env
, "syborg_int_read: Bad offset %x\n",
117 static void syborg_int_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
119 SyborgIntState
*s
= (SyborgIntState
*)opaque
;
123 DPRINTF("syborg_int_write offset=%d val=%d\n", (int)offset
, (int)value
);
124 switch (offset
>> 2) {
125 case INT_DISABLE_ALL
:
126 s
->pending_count
= 0;
127 for (i
= 0; i
< s
->num_irqs
; i
++)
128 s
->flags
[i
].enabled
= 0;
132 if (value
>= s
->num_irqs
)
134 if (s
->flags
[value
].enabled
) {
135 if (s
->flags
[value
].enabled
)
137 s
->flags
[value
].enabled
= 0;
142 if (value
>= s
->num_irqs
)
144 if (!(s
->flags
[value
].enabled
)) {
145 if(s
->flags
[value
].level
)
147 s
->flags
[value
].enabled
= 1;
152 cpu_abort(cpu_single_env
, "syborg_int_write: Bad offset %x\n",
156 syborg_int_update(s
);
159 static CPUReadMemoryFunc
* const syborg_int_readfn
[] = {
165 static CPUWriteMemoryFunc
* const syborg_int_writefn
[] = {
171 static void syborg_int_save(QEMUFile
*f
, void *opaque
)
173 SyborgIntState
*s
= (SyborgIntState
*)opaque
;
176 qemu_put_be32(f
, s
->num_irqs
);
177 qemu_put_be32(f
, s
->pending_count
);
178 for (i
= 0; i
< s
->num_irqs
; i
++) {
179 qemu_put_be32(f
, s
->flags
[i
].enabled
180 | ((unsigned)s
->flags
[i
].level
<< 1));
184 static int syborg_int_load(QEMUFile
*f
, void *opaque
, int version_id
)
186 SyborgIntState
*s
= (SyborgIntState
*)opaque
;
193 val
= qemu_get_be32(f
);
194 if (val
!= s
->num_irqs
)
196 s
->pending_count
= qemu_get_be32(f
);
197 for (i
= 0; i
< s
->num_irqs
; i
++) {
198 val
= qemu_get_be32(f
);
199 s
->flags
[i
].enabled
= val
& 1;
200 s
->flags
[i
].level
= (val
>> 1) & 1;
205 static int syborg_int_init(SysBusDevice
*dev
)
207 SyborgIntState
*s
= FROM_SYSBUS(SyborgIntState
, dev
);
210 sysbus_init_irq(dev
, &s
->parent_irq
);
211 qdev_init_gpio_in(&dev
->qdev
, syborg_int_set_irq
, s
->num_irqs
);
212 iomemtype
= cpu_register_io_memory(syborg_int_readfn
,
213 syborg_int_writefn
, s
);
214 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
215 s
->flags
= qemu_mallocz(s
->num_irqs
* sizeof(syborg_int_flags
));
217 register_savevm("syborg_int", -1, 1, syborg_int_save
, syborg_int_load
, s
);
221 static SysBusDeviceInfo syborg_int_info
= {
222 .init
= syborg_int_init
,
223 .qdev
.name
= "syborg,interrupt",
224 .qdev
.size
= sizeof(SyborgIntState
),
225 .qdev
.props
= (Property
[]) {
226 DEFINE_PROP_UINT32("num-interrupts", SyborgIntState
, num_irqs
, 64),
227 DEFINE_PROP_END_OF_LIST(),
231 static void syborg_interrupt_register_devices(void)
233 sysbus_register_withprop(&syborg_int_info
);
236 device_init(syborg_interrupt_register_devices
)