1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qemu-common.h"
23#include "qapi/error.h"
24
25#include "hw/sysbus.h"
26#include "cpu.h"
27
28#define TYPE_ALTERA_IIC "altera,iic"
29#define ALTERA_IIC(obj) \
30 OBJECT_CHECK(AlteraIIC, (obj), TYPE_ALTERA_IIC)
31
32typedef struct AlteraIIC {
33 SysBusDevice parent_obj;
34 void *cpu;
35 qemu_irq parent_irq;
36} AlteraIIC;
37
38static void update_irq(AlteraIIC *pv)
39{
40 CPUNios2State *env = &((Nios2CPU *)(pv->cpu))->env;
41
42 qemu_set_irq(pv->parent_irq,
43 env->regs[CR_IPENDING] & env->regs[CR_IENABLE]);
44}
45
46static void irq_handler(void *opaque, int irq, int level)
47{
48 AlteraIIC *pv = opaque;
49 CPUNios2State *env = &((Nios2CPU *)(pv->cpu))->env;
50
51 env->regs[CR_IPENDING] &= ~(1 << irq);
52 env->regs[CR_IPENDING] |= !!level << irq;
53
54 update_irq(pv);
55}
56
57static void altera_iic_init(Object *obj)
58{
59 AlteraIIC *pv = ALTERA_IIC(obj);
60
61 qdev_init_gpio_in(DEVICE(pv), irq_handler, 32);
62 sysbus_init_irq(SYS_BUS_DEVICE(obj), &pv->parent_irq);
63}
64
65static void altera_iic_realize(DeviceState *dev, Error **errp)
66{
67 struct AlteraIIC *pv = ALTERA_IIC(dev);
68 Error *err = NULL;
69
70 pv->cpu = object_property_get_link(OBJECT(dev), "cpu", &err);
71 if (!pv->cpu) {
72 error_setg(errp, "altera,iic: CPU link not found: %s",
73 error_get_pretty(err));
74 return;
75 }
76}
77
78static void altera_iic_class_init(ObjectClass *klass, void *data)
79{
80 DeviceClass *dc = DEVICE_CLASS(klass);
81
82
83 dc->user_creatable = false;
84 dc->realize = altera_iic_realize;
85}
86
87static TypeInfo altera_iic_info = {
88 .name = "altera,iic",
89 .parent = TYPE_SYS_BUS_DEVICE,
90 .instance_size = sizeof(AlteraIIC),
91 .instance_init = altera_iic_init,
92 .class_init = altera_iic_class_init,
93};
94
95static void altera_iic_register(void)
96{
97 type_register_static(&altera_iic_info);
98}
99
100type_init(altera_iic_register)
101