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 */ 25*0b8fa32fSMarkus Armbruster 2690191d07SPeter Maydell #include "qemu/osdep.h" 2783c9f4caSPaolo Bonzini #include "hw/hw.h" 2883c9f4caSPaolo Bonzini #include "hw/ppc/mac.h" 29*0b8fa32fSMarkus Armbruster #include "qemu/module.h" 30086df4f3SMark Cave-Ayland #include "hw/intc/heathrow_pic.h" 31ec7c2709SMark Cave-Ayland #include "trace.h" 32e68b9b2bSbellard 33086df4f3SMark Cave-Ayland static inline int heathrow_check_irq(HeathrowPICState *pic) 34e68b9b2bSbellard { 35e68b9b2bSbellard return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask; 36e68b9b2bSbellard } 37e68b9b2bSbellard 38e68b9b2bSbellard /* update the CPU irq state */ 39086df4f3SMark Cave-Ayland static void heathrow_update_irq(HeathrowState *s) 40e68b9b2bSbellard { 41086df4f3SMark Cave-Ayland if (heathrow_check_irq(&s->pics[0]) || 42086df4f3SMark Cave-Ayland heathrow_check_irq(&s->pics[1])) { 433cbee15bSj_mayer qemu_irq_raise(s->irqs[0]); 44e68b9b2bSbellard } else { 453cbee15bSj_mayer qemu_irq_lower(s->irqs[0]); 46e68b9b2bSbellard } 47e68b9b2bSbellard } 48e68b9b2bSbellard 49086df4f3SMark Cave-Ayland static void heathrow_write(void *opaque, hwaddr addr, 5023c5e4caSAvi Kivity uint64_t value, unsigned size) 51e68b9b2bSbellard { 52086df4f3SMark Cave-Ayland HeathrowState *s = opaque; 53086df4f3SMark Cave-Ayland HeathrowPICState *pic; 54e68b9b2bSbellard unsigned int n; 55e68b9b2bSbellard 56e68b9b2bSbellard n = ((addr & 0xfff) - 0x10) >> 4; 57ec7c2709SMark Cave-Ayland trace_heathrow_write(addr, n, value); 58e68b9b2bSbellard if (n >= 2) 59e68b9b2bSbellard return; 60e68b9b2bSbellard pic = &s->pics[n]; 61e68b9b2bSbellard switch(addr & 0xf) { 62e68b9b2bSbellard case 0x04: 63e68b9b2bSbellard pic->mask = value; 64086df4f3SMark Cave-Ayland heathrow_update_irq(s); 65e68b9b2bSbellard break; 66e68b9b2bSbellard case 0x08: 67e68b9b2bSbellard /* do not reset level triggered IRQs */ 68e68b9b2bSbellard value &= ~pic->level_triggered; 69e68b9b2bSbellard pic->events &= ~value; 70086df4f3SMark Cave-Ayland heathrow_update_irq(s); 71e68b9b2bSbellard break; 72e68b9b2bSbellard default: 73e68b9b2bSbellard break; 74e68b9b2bSbellard } 75e68b9b2bSbellard } 76e68b9b2bSbellard 77086df4f3SMark Cave-Ayland static uint64_t heathrow_read(void *opaque, hwaddr addr, 7823c5e4caSAvi Kivity unsigned size) 79e68b9b2bSbellard { 80086df4f3SMark Cave-Ayland HeathrowState *s = opaque; 81086df4f3SMark Cave-Ayland HeathrowPICState *pic; 82e68b9b2bSbellard unsigned int n; 83e68b9b2bSbellard uint32_t value; 84e68b9b2bSbellard 85e68b9b2bSbellard n = ((addr & 0xfff) - 0x10) >> 4; 86e68b9b2bSbellard if (n >= 2) { 87e68b9b2bSbellard value = 0; 88e68b9b2bSbellard } else { 89e68b9b2bSbellard pic = &s->pics[n]; 90e68b9b2bSbellard switch(addr & 0xf) { 91e68b9b2bSbellard case 0x0: 92e68b9b2bSbellard value = pic->events; 93e68b9b2bSbellard break; 94e68b9b2bSbellard case 0x4: 95e68b9b2bSbellard value = pic->mask; 96e68b9b2bSbellard break; 97e68b9b2bSbellard case 0xc: 98e68b9b2bSbellard value = pic->levels; 99e68b9b2bSbellard break; 100e68b9b2bSbellard default: 101e68b9b2bSbellard value = 0; 102e68b9b2bSbellard break; 103e68b9b2bSbellard } 104e68b9b2bSbellard } 105ec7c2709SMark Cave-Ayland trace_heathrow_read(addr, n, value); 106e68b9b2bSbellard return value; 107e68b9b2bSbellard } 108e68b9b2bSbellard 109086df4f3SMark Cave-Ayland static const MemoryRegionOps heathrow_ops = { 110086df4f3SMark Cave-Ayland .read = heathrow_read, 111086df4f3SMark Cave-Ayland .write = heathrow_write, 1120157644cSAlexander Graf .endianness = DEVICE_LITTLE_ENDIAN, 113e68b9b2bSbellard }; 114e68b9b2bSbellard 115086df4f3SMark Cave-Ayland static void heathrow_set_irq(void *opaque, int num, int level) 116e68b9b2bSbellard { 117086df4f3SMark Cave-Ayland HeathrowState *s = opaque; 118086df4f3SMark Cave-Ayland HeathrowPICState *pic; 119e68b9b2bSbellard unsigned int irq_bit; 120ec7c2709SMark Cave-Ayland int last_level; 121e68b9b2bSbellard 122e68b9b2bSbellard pic = &s->pics[1 - (num >> 5)]; 123e68b9b2bSbellard irq_bit = 1 << (num & 0x1f); 124ec7c2709SMark Cave-Ayland last_level = (pic->levels & irq_bit) ? 1 : 0; 125ec7c2709SMark Cave-Ayland 126e68b9b2bSbellard if (level) { 127e68b9b2bSbellard pic->events |= irq_bit & ~pic->level_triggered; 128e68b9b2bSbellard pic->levels |= irq_bit; 129e68b9b2bSbellard } else { 130e68b9b2bSbellard pic->levels &= ~irq_bit; 131e68b9b2bSbellard } 132ec7c2709SMark Cave-Ayland 133ec7c2709SMark Cave-Ayland if (last_level != level) { 134ec7c2709SMark Cave-Ayland trace_heathrow_set_irq(num, level); 135ec7c2709SMark Cave-Ayland } 136ec7c2709SMark Cave-Ayland 137086df4f3SMark Cave-Ayland heathrow_update_irq(s); 138e68b9b2bSbellard } 139e68b9b2bSbellard 1404acd38ceSJuan Quintela static const VMStateDescription vmstate_heathrow_pic_one = { 1414acd38ceSJuan Quintela .name = "heathrow_pic_one", 1424acd38ceSJuan Quintela .version_id = 0, 1434acd38ceSJuan Quintela .minimum_version_id = 0, 1444acd38ceSJuan Quintela .fields = (VMStateField[]) { 145086df4f3SMark Cave-Ayland VMSTATE_UINT32(events, HeathrowPICState), 146086df4f3SMark Cave-Ayland VMSTATE_UINT32(mask, HeathrowPICState), 147086df4f3SMark Cave-Ayland VMSTATE_UINT32(levels, HeathrowPICState), 148086df4f3SMark Cave-Ayland VMSTATE_UINT32(level_triggered, HeathrowPICState), 1494acd38ceSJuan Quintela VMSTATE_END_OF_LIST() 1509b64997fSblueswir1 } 1514acd38ceSJuan Quintela }; 1529b64997fSblueswir1 153086df4f3SMark Cave-Ayland static const VMStateDescription vmstate_heathrow = { 1544acd38ceSJuan Quintela .name = "heathrow_pic", 1554acd38ceSJuan Quintela .version_id = 1, 1564acd38ceSJuan Quintela .minimum_version_id = 1, 1574acd38ceSJuan Quintela .fields = (VMStateField[]) { 158086df4f3SMark Cave-Ayland VMSTATE_STRUCT_ARRAY(pics, HeathrowState, 2, 1, 159086df4f3SMark Cave-Ayland vmstate_heathrow_pic_one, HeathrowPICState), 1604acd38ceSJuan Quintela VMSTATE_END_OF_LIST() 1619b64997fSblueswir1 } 1624acd38ceSJuan Quintela }; 1639b64997fSblueswir1 164086df4f3SMark Cave-Ayland static void heathrow_reset(DeviceState *d) 1656e6b7363Sblueswir1 { 166086df4f3SMark Cave-Ayland HeathrowState *s = HEATHROW(d); 1676e6b7363Sblueswir1 1686e6b7363Sblueswir1 s->pics[0].level_triggered = 0; 1696e6b7363Sblueswir1 s->pics[1].level_triggered = 0x1ff00000; 1706e6b7363Sblueswir1 } 1716e6b7363Sblueswir1 172086df4f3SMark Cave-Ayland static void heathrow_init(Object *obj) 173086df4f3SMark Cave-Ayland { 174086df4f3SMark Cave-Ayland HeathrowState *s = HEATHROW(obj); 175c2964600SMark Cave-Ayland SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 176086df4f3SMark Cave-Ayland 177a5ed75feSMark Cave-Ayland /* only 1 CPU */ 178a5ed75feSMark Cave-Ayland qdev_init_gpio_out(DEVICE(obj), s->irqs, 1); 179a5ed75feSMark Cave-Ayland 180a5ed75feSMark Cave-Ayland qdev_init_gpio_in(DEVICE(obj), heathrow_set_irq, HEATHROW_NUM_IRQS); 181a5ed75feSMark Cave-Ayland 182086df4f3SMark Cave-Ayland memory_region_init_io(&s->mem, OBJECT(s), &heathrow_ops, s, 183086df4f3SMark Cave-Ayland "heathrow-pic", 0x1000); 184c2964600SMark Cave-Ayland sysbus_init_mmio(sbd, &s->mem); 185086df4f3SMark Cave-Ayland } 186086df4f3SMark Cave-Ayland 187086df4f3SMark Cave-Ayland static void heathrow_class_init(ObjectClass *oc, void *data) 188086df4f3SMark Cave-Ayland { 189086df4f3SMark Cave-Ayland DeviceClass *dc = DEVICE_CLASS(oc); 190086df4f3SMark Cave-Ayland 191086df4f3SMark Cave-Ayland dc->reset = heathrow_reset; 192086df4f3SMark Cave-Ayland dc->vmsd = &vmstate_heathrow; 193086df4f3SMark Cave-Ayland set_bit(DEVICE_CATEGORY_MISC, dc->categories); 194086df4f3SMark Cave-Ayland } 195086df4f3SMark Cave-Ayland 196086df4f3SMark Cave-Ayland static const TypeInfo heathrow_type_info = { 197086df4f3SMark Cave-Ayland .name = TYPE_HEATHROW, 198086df4f3SMark Cave-Ayland .parent = TYPE_SYS_BUS_DEVICE, 199086df4f3SMark Cave-Ayland .instance_size = sizeof(HeathrowState), 200086df4f3SMark Cave-Ayland .instance_init = heathrow_init, 201086df4f3SMark Cave-Ayland .class_init = heathrow_class_init, 202086df4f3SMark Cave-Ayland }; 203086df4f3SMark Cave-Ayland 204086df4f3SMark Cave-Ayland static void heathrow_register_types(void) 205086df4f3SMark Cave-Ayland { 206086df4f3SMark Cave-Ayland type_register_static(&heathrow_type_info); 207086df4f3SMark Cave-Ayland } 208086df4f3SMark Cave-Ayland 209086df4f3SMark Cave-Ayland type_init(heathrow_register_types) 210