]>
Commit | Line | Data |
---|---|---|
17628bc6 EI |
1 | /* |
2 | * QEMU Xilinx OPB Interrupt Controller. | |
3 | * | |
4 | * Copyright (c) 2009 Edgar E. Iglesias. | |
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 | ||
90191d07 | 25 | #include "qemu/osdep.h" |
83c9f4ca PB |
26 | #include "hw/sysbus.h" |
27 | #include "hw/hw.h" | |
17628bc6 EI |
28 | |
29 | #define D(x) | |
30 | ||
31 | #define R_ISR 0 | |
32 | #define R_IPR 1 | |
33 | #define R_IER 2 | |
34 | #define R_IAR 3 | |
35 | #define R_SIE 4 | |
36 | #define R_CIE 5 | |
37 | #define R_IVR 6 | |
38 | #define R_MER 7 | |
39 | #define R_MAX 8 | |
40 | ||
cc3e064e AF |
41 | #define TYPE_XILINX_INTC "xlnx.xps-intc" |
42 | #define XILINX_INTC(obj) OBJECT_CHECK(struct xlx_pic, (obj), TYPE_XILINX_INTC) | |
43 | ||
17628bc6 EI |
44 | struct xlx_pic |
45 | { | |
cc3e064e AF |
46 | SysBusDevice parent_obj; |
47 | ||
010f3f5f | 48 | MemoryRegion mmio; |
17628bc6 EI |
49 | qemu_irq parent_irq; |
50 | ||
51 | /* Configuration reg chosen at synthesis-time. QEMU populates | |
52 | the bits at board-setup. */ | |
53 | uint32_t c_kind_of_intr; | |
54 | ||
55 | /* Runtime control registers. */ | |
56 | uint32_t regs[R_MAX]; | |
45fdd3bf PC |
57 | /* state of the interrupt input pins */ |
58 | uint32_t irq_pin_state; | |
17628bc6 EI |
59 | }; |
60 | ||
61 | static void update_irq(struct xlx_pic *p) | |
62 | { | |
63 | uint32_t i; | |
45fdd3bf PC |
64 | |
65 | /* level triggered interrupt */ | |
66 | if (p->regs[R_MER] & 2) { | |
67 | p->regs[R_ISR] |= p->irq_pin_state & ~p->c_kind_of_intr; | |
68 | } | |
69 | ||
17628bc6 EI |
70 | /* Update the pending register. */ |
71 | p->regs[R_IPR] = p->regs[R_ISR] & p->regs[R_IER]; | |
72 | ||
73 | /* Update the vector register. */ | |
74 | for (i = 0; i < 32; i++) { | |
0bc60bd7 | 75 | if (p->regs[R_IPR] & (1U << i)) { |
17628bc6 | 76 | break; |
0bc60bd7 | 77 | } |
17628bc6 EI |
78 | } |
79 | if (i == 32) | |
80 | i = ~0; | |
81 | ||
82 | p->regs[R_IVR] = i; | |
5c9f4336 | 83 | qemu_set_irq(p->parent_irq, (p->regs[R_MER] & 1) && p->regs[R_IPR]); |
17628bc6 EI |
84 | } |
85 | ||
010f3f5f | 86 | static uint64_t |
a8170e5e | 87 | pic_read(void *opaque, hwaddr addr, unsigned int size) |
17628bc6 EI |
88 | { |
89 | struct xlx_pic *p = opaque; | |
90 | uint32_t r = 0; | |
91 | ||
92 | addr >>= 2; | |
93 | switch (addr) | |
94 | { | |
95 | default: | |
96 | if (addr < ARRAY_SIZE(p->regs)) | |
97 | r = p->regs[addr]; | |
98 | break; | |
99 | ||
100 | } | |
101 | D(printf("%s %x=%x\n", __func__, addr * 4, r)); | |
102 | return r; | |
103 | } | |
104 | ||
105 | static void | |
a8170e5e | 106 | pic_write(void *opaque, hwaddr addr, |
010f3f5f | 107 | uint64_t val64, unsigned int size) |
17628bc6 EI |
108 | { |
109 | struct xlx_pic *p = opaque; | |
010f3f5f | 110 | uint32_t value = val64; |
17628bc6 EI |
111 | |
112 | addr >>= 2; | |
113 | D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value)); | |
114 | switch (addr) | |
115 | { | |
116 | case R_IAR: | |
117 | p->regs[R_ISR] &= ~value; /* ACK. */ | |
118 | break; | |
119 | case R_SIE: | |
120 | p->regs[R_IER] |= value; /* Atomic set ie. */ | |
121 | break; | |
122 | case R_CIE: | |
123 | p->regs[R_IER] &= ~value; /* Atomic clear ie. */ | |
124 | break; | |
12f7fb60 GR |
125 | case R_MER: |
126 | p->regs[R_MER] = value & 0x3; | |
127 | break; | |
fa96d614 PC |
128 | case R_ISR: |
129 | if ((p->regs[R_MER] & 2)) { | |
130 | break; | |
131 | } | |
132 | /* fallthrough */ | |
17628bc6 EI |
133 | default: |
134 | if (addr < ARRAY_SIZE(p->regs)) | |
135 | p->regs[addr] = value; | |
136 | break; | |
137 | } | |
138 | update_irq(p); | |
139 | } | |
140 | ||
010f3f5f EI |
141 | static const MemoryRegionOps pic_ops = { |
142 | .read = pic_read, | |
143 | .write = pic_write, | |
144 | .endianness = DEVICE_NATIVE_ENDIAN, | |
145 | .valid = { | |
146 | .min_access_size = 4, | |
147 | .max_access_size = 4 | |
148 | } | |
17628bc6 EI |
149 | }; |
150 | ||
151 | static void irq_handler(void *opaque, int irq, int level) | |
152 | { | |
153 | struct xlx_pic *p = opaque; | |
154 | ||
45fdd3bf PC |
155 | /* edge triggered interrupt */ |
156 | if (p->c_kind_of_intr & (1 << irq) && p->regs[R_MER] & 2) { | |
157 | p->regs[R_ISR] |= (level << irq); | |
158 | } | |
159 | ||
160 | p->irq_pin_state &= ~(1 << irq); | |
161 | p->irq_pin_state |= level << irq; | |
17628bc6 EI |
162 | update_irq(p); |
163 | } | |
164 | ||
a373cdb5 | 165 | static void xilinx_intc_init(Object *obj) |
17628bc6 | 166 | { |
a373cdb5 | 167 | struct xlx_pic *p = XILINX_INTC(obj); |
17628bc6 | 168 | |
a373cdb5 PC |
169 | qdev_init_gpio_in(DEVICE(obj), irq_handler, 32); |
170 | sysbus_init_irq(SYS_BUS_DEVICE(obj), &p->parent_irq); | |
17628bc6 | 171 | |
a373cdb5 | 172 | memory_region_init_io(&p->mmio, obj, &pic_ops, p, "xlnx.xps-intc", |
1437c94b | 173 | R_MAX * 4); |
a373cdb5 | 174 | sysbus_init_mmio(SYS_BUS_DEVICE(obj), &p->mmio); |
17628bc6 EI |
175 | } |
176 | ||
999e12bb AL |
177 | static Property xilinx_intc_properties[] = { |
178 | DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0), | |
179 | DEFINE_PROP_END_OF_LIST(), | |
180 | }; | |
181 | ||
182 | static void xilinx_intc_class_init(ObjectClass *klass, void *data) | |
183 | { | |
39bffca2 | 184 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 185 | |
39bffca2 | 186 | dc->props = xilinx_intc_properties; |
999e12bb AL |
187 | } |
188 | ||
8c43a6f0 | 189 | static const TypeInfo xilinx_intc_info = { |
cc3e064e | 190 | .name = TYPE_XILINX_INTC, |
39bffca2 AL |
191 | .parent = TYPE_SYS_BUS_DEVICE, |
192 | .instance_size = sizeof(struct xlx_pic), | |
a373cdb5 | 193 | .instance_init = xilinx_intc_init, |
39bffca2 | 194 | .class_init = xilinx_intc_class_init, |
ee6847d1 GH |
195 | }; |
196 | ||
83f7d43a | 197 | static void xilinx_intc_register_types(void) |
17628bc6 | 198 | { |
39bffca2 | 199 | type_register_static(&xilinx_intc_info); |
17628bc6 EI |
200 | } |
201 | ||
83f7d43a | 202 | type_init(xilinx_intc_register_types) |