xref: /qemu/hw/intc/heathrow_pic.c (revision 64552b6be4758d3a774f7787b294543ccebd5358)
1e68b9b2bSbellard /*
23cbee15bSj_mayer  * Heathrow PIC support (OldWorld PowerMac)
3e68b9b2bSbellard  *
43cbee15bSj_mayer  * Copyright (c) 2005-2007 Fabrice Bellard
53cbee15bSj_mayer  * Copyright (c) 2007 Jocelyn Mayer
6e68b9b2bSbellard  *
7e68b9b2bSbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
8e68b9b2bSbellard  * of this software and associated documentation files (the "Software"), to deal
9e68b9b2bSbellard  * in the Software without restriction, including without limitation the rights
10e68b9b2bSbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11e68b9b2bSbellard  * copies of the Software, and to permit persons to whom the Software is
12e68b9b2bSbellard  * furnished to do so, subject to the following conditions:
13e68b9b2bSbellard  *
14e68b9b2bSbellard  * The above copyright notice and this permission notice shall be included in
15e68b9b2bSbellard  * all copies or substantial portions of the Software.
16e68b9b2bSbellard  *
17e68b9b2bSbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e68b9b2bSbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e68b9b2bSbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20e68b9b2bSbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e68b9b2bSbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e68b9b2bSbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23e68b9b2bSbellard  * THE SOFTWARE.
24e68b9b2bSbellard  */
250b8fa32fSMarkus Armbruster 
2690191d07SPeter Maydell #include "qemu/osdep.h"
2783c9f4caSPaolo Bonzini #include "hw/hw.h"
2883c9f4caSPaolo Bonzini #include "hw/ppc/mac.h"
290b8fa32fSMarkus Armbruster #include "qemu/module.h"
30086df4f3SMark Cave-Ayland #include "hw/intc/heathrow_pic.h"
31*64552b6bSMarkus Armbruster #include "hw/irq.h"
32ec7c2709SMark Cave-Ayland #include "trace.h"
33e68b9b2bSbellard 
34086df4f3SMark Cave-Ayland static inline int heathrow_check_irq(HeathrowPICState *pic)
35e68b9b2bSbellard {
36e68b9b2bSbellard     return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
37e68b9b2bSbellard }
38e68b9b2bSbellard 
39e68b9b2bSbellard /* update the CPU irq state */
40086df4f3SMark Cave-Ayland static void heathrow_update_irq(HeathrowState *s)
41e68b9b2bSbellard {
42086df4f3SMark Cave-Ayland     if (heathrow_check_irq(&s->pics[0]) ||
43086df4f3SMark Cave-Ayland             heathrow_check_irq(&s->pics[1])) {
443cbee15bSj_mayer         qemu_irq_raise(s->irqs[0]);
45e68b9b2bSbellard     } else {
463cbee15bSj_mayer         qemu_irq_lower(s->irqs[0]);
47e68b9b2bSbellard     }
48e68b9b2bSbellard }
49e68b9b2bSbellard 
50086df4f3SMark Cave-Ayland static void heathrow_write(void *opaque, hwaddr addr,
5123c5e4caSAvi Kivity                            uint64_t value, unsigned size)
52e68b9b2bSbellard {
53086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
54086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
55e68b9b2bSbellard     unsigned int n;
56e68b9b2bSbellard 
57e68b9b2bSbellard     n = ((addr & 0xfff) - 0x10) >> 4;
58ec7c2709SMark Cave-Ayland     trace_heathrow_write(addr, n, value);
59e68b9b2bSbellard     if (n >= 2)
60e68b9b2bSbellard         return;
61e68b9b2bSbellard     pic = &s->pics[n];
62e68b9b2bSbellard     switch(addr & 0xf) {
63e68b9b2bSbellard     case 0x04:
64e68b9b2bSbellard         pic->mask = value;
65086df4f3SMark Cave-Ayland         heathrow_update_irq(s);
66e68b9b2bSbellard         break;
67e68b9b2bSbellard     case 0x08:
68e68b9b2bSbellard         /* do not reset level triggered IRQs */
69e68b9b2bSbellard         value &= ~pic->level_triggered;
70e68b9b2bSbellard         pic->events &= ~value;
71086df4f3SMark Cave-Ayland         heathrow_update_irq(s);
72e68b9b2bSbellard         break;
73e68b9b2bSbellard     default:
74e68b9b2bSbellard         break;
75e68b9b2bSbellard     }
76e68b9b2bSbellard }
77e68b9b2bSbellard 
78086df4f3SMark Cave-Ayland static uint64_t heathrow_read(void *opaque, hwaddr addr,
7923c5e4caSAvi Kivity                               unsigned size)
80e68b9b2bSbellard {
81086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
82086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
83e68b9b2bSbellard     unsigned int n;
84e68b9b2bSbellard     uint32_t value;
85e68b9b2bSbellard 
86e68b9b2bSbellard     n = ((addr & 0xfff) - 0x10) >> 4;
87e68b9b2bSbellard     if (n >= 2) {
88e68b9b2bSbellard         value = 0;
89e68b9b2bSbellard     } else {
90e68b9b2bSbellard         pic = &s->pics[n];
91e68b9b2bSbellard         switch(addr & 0xf) {
92e68b9b2bSbellard         case 0x0:
93e68b9b2bSbellard             value = pic->events;
94e68b9b2bSbellard             break;
95e68b9b2bSbellard         case 0x4:
96e68b9b2bSbellard             value = pic->mask;
97e68b9b2bSbellard             break;
98e68b9b2bSbellard         case 0xc:
99e68b9b2bSbellard             value = pic->levels;
100e68b9b2bSbellard             break;
101e68b9b2bSbellard         default:
102e68b9b2bSbellard             value = 0;
103e68b9b2bSbellard             break;
104e68b9b2bSbellard         }
105e68b9b2bSbellard     }
106ec7c2709SMark Cave-Ayland     trace_heathrow_read(addr, n, value);
107e68b9b2bSbellard     return value;
108e68b9b2bSbellard }
109e68b9b2bSbellard 
110086df4f3SMark Cave-Ayland static const MemoryRegionOps heathrow_ops = {
111086df4f3SMark Cave-Ayland     .read = heathrow_read,
112086df4f3SMark Cave-Ayland     .write = heathrow_write,
1130157644cSAlexander Graf     .endianness = DEVICE_LITTLE_ENDIAN,
114e68b9b2bSbellard };
115e68b9b2bSbellard 
116086df4f3SMark Cave-Ayland static void heathrow_set_irq(void *opaque, int num, int level)
117e68b9b2bSbellard {
118086df4f3SMark Cave-Ayland     HeathrowState *s = opaque;
119086df4f3SMark Cave-Ayland     HeathrowPICState *pic;
120e68b9b2bSbellard     unsigned int irq_bit;
121ec7c2709SMark Cave-Ayland     int last_level;
122e68b9b2bSbellard 
123e68b9b2bSbellard     pic = &s->pics[1 - (num >> 5)];
124e68b9b2bSbellard     irq_bit = 1 << (num & 0x1f);
125ec7c2709SMark Cave-Ayland     last_level = (pic->levels & irq_bit) ? 1 : 0;
126ec7c2709SMark Cave-Ayland 
127e68b9b2bSbellard     if (level) {
128e68b9b2bSbellard         pic->events |= irq_bit & ~pic->level_triggered;
129e68b9b2bSbellard         pic->levels |= irq_bit;
130e68b9b2bSbellard     } else {
131e68b9b2bSbellard         pic->levels &= ~irq_bit;
132e68b9b2bSbellard     }
133ec7c2709SMark Cave-Ayland 
134ec7c2709SMark Cave-Ayland     if (last_level != level) {
135ec7c2709SMark Cave-Ayland         trace_heathrow_set_irq(num, level);
136ec7c2709SMark Cave-Ayland     }
137ec7c2709SMark Cave-Ayland 
138086df4f3SMark Cave-Ayland     heathrow_update_irq(s);
139e68b9b2bSbellard }
140e68b9b2bSbellard 
1414acd38ceSJuan Quintela static const VMStateDescription vmstate_heathrow_pic_one = {
1424acd38ceSJuan Quintela     .name = "heathrow_pic_one",
1434acd38ceSJuan Quintela     .version_id = 0,
1444acd38ceSJuan Quintela     .minimum_version_id = 0,
1454acd38ceSJuan Quintela     .fields = (VMStateField[]) {
146086df4f3SMark Cave-Ayland         VMSTATE_UINT32(events, HeathrowPICState),
147086df4f3SMark Cave-Ayland         VMSTATE_UINT32(mask, HeathrowPICState),
148086df4f3SMark Cave-Ayland         VMSTATE_UINT32(levels, HeathrowPICState),
149086df4f3SMark Cave-Ayland         VMSTATE_UINT32(level_triggered, HeathrowPICState),
1504acd38ceSJuan Quintela         VMSTATE_END_OF_LIST()
1519b64997fSblueswir1     }
1524acd38ceSJuan Quintela };
1539b64997fSblueswir1 
154086df4f3SMark Cave-Ayland static const VMStateDescription vmstate_heathrow = {
1554acd38ceSJuan Quintela     .name = "heathrow_pic",
1564acd38ceSJuan Quintela     .version_id = 1,
1574acd38ceSJuan Quintela     .minimum_version_id = 1,
1584acd38ceSJuan Quintela     .fields = (VMStateField[]) {
159086df4f3SMark Cave-Ayland         VMSTATE_STRUCT_ARRAY(pics, HeathrowState, 2, 1,
160086df4f3SMark Cave-Ayland                              vmstate_heathrow_pic_one, HeathrowPICState),
1614acd38ceSJuan Quintela         VMSTATE_END_OF_LIST()
1629b64997fSblueswir1     }
1634acd38ceSJuan Quintela };
1649b64997fSblueswir1 
165086df4f3SMark Cave-Ayland static void heathrow_reset(DeviceState *d)
1666e6b7363Sblueswir1 {
167086df4f3SMark Cave-Ayland     HeathrowState *s = HEATHROW(d);
1686e6b7363Sblueswir1 
1696e6b7363Sblueswir1     s->pics[0].level_triggered = 0;
1706e6b7363Sblueswir1     s->pics[1].level_triggered = 0x1ff00000;
1716e6b7363Sblueswir1 }
1726e6b7363Sblueswir1 
173086df4f3SMark Cave-Ayland static void heathrow_init(Object *obj)
174086df4f3SMark Cave-Ayland {
175086df4f3SMark Cave-Ayland     HeathrowState *s = HEATHROW(obj);
176c2964600SMark Cave-Ayland     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
177086df4f3SMark Cave-Ayland 
178a5ed75feSMark Cave-Ayland     /* only 1 CPU */
179a5ed75feSMark Cave-Ayland     qdev_init_gpio_out(DEVICE(obj), s->irqs, 1);
180a5ed75feSMark Cave-Ayland 
181a5ed75feSMark Cave-Ayland     qdev_init_gpio_in(DEVICE(obj), heathrow_set_irq, HEATHROW_NUM_IRQS);
182a5ed75feSMark Cave-Ayland 
183086df4f3SMark Cave-Ayland     memory_region_init_io(&s->mem, OBJECT(s), &heathrow_ops, s,
184086df4f3SMark Cave-Ayland                           "heathrow-pic", 0x1000);
185c2964600SMark Cave-Ayland     sysbus_init_mmio(sbd, &s->mem);
186086df4f3SMark Cave-Ayland }
187086df4f3SMark Cave-Ayland 
188086df4f3SMark Cave-Ayland static void heathrow_class_init(ObjectClass *oc, void *data)
189086df4f3SMark Cave-Ayland {
190086df4f3SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(oc);
191086df4f3SMark Cave-Ayland 
192086df4f3SMark Cave-Ayland     dc->reset = heathrow_reset;
193086df4f3SMark Cave-Ayland     dc->vmsd = &vmstate_heathrow;
194086df4f3SMark Cave-Ayland     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
195086df4f3SMark Cave-Ayland }
196086df4f3SMark Cave-Ayland 
197086df4f3SMark Cave-Ayland static const TypeInfo heathrow_type_info = {
198086df4f3SMark Cave-Ayland     .name = TYPE_HEATHROW,
199086df4f3SMark Cave-Ayland     .parent = TYPE_SYS_BUS_DEVICE,
200086df4f3SMark Cave-Ayland     .instance_size = sizeof(HeathrowState),
201086df4f3SMark Cave-Ayland     .instance_init = heathrow_init,
202086df4f3SMark Cave-Ayland     .class_init = heathrow_class_init,
203086df4f3SMark Cave-Ayland };
204086df4f3SMark Cave-Ayland 
205086df4f3SMark Cave-Ayland static void heathrow_register_types(void)
206086df4f3SMark Cave-Ayland {
207086df4f3SMark Cave-Ayland     type_register_static(&heathrow_type_info);
208086df4f3SMark Cave-Ayland }
209086df4f3SMark Cave-Ayland 
210086df4f3SMark Cave-Ayland type_init(heathrow_register_types)
211