Statistics
| Branch: | Revision:

root / hw / heathrow_pic.c @ 3a9d8549

History | View | Annotate | Download (5.8 kB)

1 e68b9b2b bellard
/*
2 3cbee15b j_mayer
 * Heathrow PIC support (OldWorld PowerMac)
3 5fafdf24 ths
 *
4 3cbee15b j_mayer
 * Copyright (c) 2005-2007 Fabrice Bellard
5 3cbee15b j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
6 5fafdf24 ths
 *
7 e68b9b2b bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 e68b9b2b bellard
 * of this software and associated documentation files (the "Software"), to deal
9 e68b9b2b bellard
 * in the Software without restriction, including without limitation the rights
10 e68b9b2b bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 e68b9b2b bellard
 * copies of the Software, and to permit persons to whom the Software is
12 e68b9b2b bellard
 * furnished to do so, subject to the following conditions:
13 e68b9b2b bellard
 *
14 e68b9b2b bellard
 * The above copyright notice and this permission notice shall be included in
15 e68b9b2b bellard
 * all copies or substantial portions of the Software.
16 e68b9b2b bellard
 *
17 e68b9b2b bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 e68b9b2b bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 e68b9b2b bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 e68b9b2b bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 e68b9b2b bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 e68b9b2b bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 e68b9b2b bellard
 * THE SOFTWARE.
24 e68b9b2b bellard
 */
25 87ecb68b pbrook
#include "hw.h"
26 3cbee15b j_mayer
#include "ppc_mac.h"
27 e68b9b2b bellard
28 ea026b2f blueswir1
/* debug PIC */
29 ea026b2f blueswir1
//#define DEBUG_PIC
30 ea026b2f blueswir1
31 ea026b2f blueswir1
#ifdef DEBUG_PIC
32 001faf32 Blue Swirl
#define PIC_DPRINTF(fmt, ...)                                   \
33 001faf32 Blue Swirl
    do { printf("PIC: " fmt , ## __VA_ARGS__); } while (0)
34 ea026b2f blueswir1
#else
35 001faf32 Blue Swirl
#define PIC_DPRINTF(fmt, ...)
36 ea026b2f blueswir1
#endif
37 e68b9b2b bellard
38 e68b9b2b bellard
typedef struct HeathrowPIC {
39 e68b9b2b bellard
    uint32_t events;
40 e68b9b2b bellard
    uint32_t mask;
41 e68b9b2b bellard
    uint32_t levels;
42 e68b9b2b bellard
    uint32_t level_triggered;
43 e68b9b2b bellard
} HeathrowPIC;
44 e68b9b2b bellard
45 d537cf6c pbrook
typedef struct HeathrowPICS {
46 e68b9b2b bellard
    HeathrowPIC pics[2];
47 3cbee15b j_mayer
    qemu_irq *irqs;
48 d537cf6c pbrook
} HeathrowPICS;
49 e68b9b2b bellard
50 e68b9b2b bellard
static inline int check_irq(HeathrowPIC *pic)
51 e68b9b2b bellard
{
52 e68b9b2b bellard
    return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
53 e68b9b2b bellard
}
54 e68b9b2b bellard
55 e68b9b2b bellard
/* update the CPU irq state */
56 e68b9b2b bellard
static void heathrow_pic_update(HeathrowPICS *s)
57 e68b9b2b bellard
{
58 e68b9b2b bellard
    if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
59 3cbee15b j_mayer
        qemu_irq_raise(s->irqs[0]);
60 e68b9b2b bellard
    } else {
61 3cbee15b j_mayer
        qemu_irq_lower(s->irqs[0]);
62 e68b9b2b bellard
    }
63 e68b9b2b bellard
}
64 e68b9b2b bellard
65 c227f099 Anthony Liguori
static void pic_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
66 e68b9b2b bellard
{
67 e68b9b2b bellard
    HeathrowPICS *s = opaque;
68 e68b9b2b bellard
    HeathrowPIC *pic;
69 e68b9b2b bellard
    unsigned int n;
70 e68b9b2b bellard
71 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
72 ea026b2f blueswir1
    PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
73 e68b9b2b bellard
    if (n >= 2)
74 e68b9b2b bellard
        return;
75 e68b9b2b bellard
    pic = &s->pics[n];
76 e68b9b2b bellard
    switch(addr & 0xf) {
77 e68b9b2b bellard
    case 0x04:
78 e68b9b2b bellard
        pic->mask = value;
79 e68b9b2b bellard
        heathrow_pic_update(s);
80 e68b9b2b bellard
        break;
81 e68b9b2b bellard
    case 0x08:
82 e68b9b2b bellard
        /* do not reset level triggered IRQs */
83 e68b9b2b bellard
        value &= ~pic->level_triggered;
84 e68b9b2b bellard
        pic->events &= ~value;
85 e68b9b2b bellard
        heathrow_pic_update(s);
86 e68b9b2b bellard
        break;
87 e68b9b2b bellard
    default:
88 e68b9b2b bellard
        break;
89 e68b9b2b bellard
    }
90 e68b9b2b bellard
}
91 e68b9b2b bellard
92 c227f099 Anthony Liguori
static uint32_t pic_readl (void *opaque, target_phys_addr_t addr)
93 e68b9b2b bellard
{
94 e68b9b2b bellard
    HeathrowPICS *s = opaque;
95 e68b9b2b bellard
    HeathrowPIC *pic;
96 e68b9b2b bellard
    unsigned int n;
97 e68b9b2b bellard
    uint32_t value;
98 3b46e624 ths
99 e68b9b2b bellard
    n = ((addr & 0xfff) - 0x10) >> 4;
100 e68b9b2b bellard
    if (n >= 2) {
101 e68b9b2b bellard
        value = 0;
102 e68b9b2b bellard
    } else {
103 e68b9b2b bellard
        pic = &s->pics[n];
104 e68b9b2b bellard
        switch(addr & 0xf) {
105 e68b9b2b bellard
        case 0x0:
106 e68b9b2b bellard
            value = pic->events;
107 e68b9b2b bellard
            break;
108 e68b9b2b bellard
        case 0x4:
109 e68b9b2b bellard
            value = pic->mask;
110 e68b9b2b bellard
            break;
111 e68b9b2b bellard
        case 0xc:
112 e68b9b2b bellard
            value = pic->levels;
113 e68b9b2b bellard
            break;
114 e68b9b2b bellard
        default:
115 e68b9b2b bellard
            value = 0;
116 e68b9b2b bellard
            break;
117 e68b9b2b bellard
        }
118 e68b9b2b bellard
    }
119 ea026b2f blueswir1
    PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
120 e68b9b2b bellard
    return value;
121 e68b9b2b bellard
}
122 e68b9b2b bellard
123 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const pic_write[] = {
124 e68b9b2b bellard
    &pic_writel,
125 e68b9b2b bellard
    &pic_writel,
126 e68b9b2b bellard
    &pic_writel,
127 e68b9b2b bellard
};
128 e68b9b2b bellard
129 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const pic_read[] = {
130 e68b9b2b bellard
    &pic_readl,
131 e68b9b2b bellard
    &pic_readl,
132 e68b9b2b bellard
    &pic_readl,
133 e68b9b2b bellard
};
134 e68b9b2b bellard
135 e68b9b2b bellard
136 d537cf6c pbrook
static void heathrow_pic_set_irq(void *opaque, int num, int level)
137 e68b9b2b bellard
{
138 e68b9b2b bellard
    HeathrowPICS *s = opaque;
139 e68b9b2b bellard
    HeathrowPIC *pic;
140 e68b9b2b bellard
    unsigned int irq_bit;
141 e68b9b2b bellard
142 e68b9b2b bellard
#if defined(DEBUG)
143 e68b9b2b bellard
    {
144 e68b9b2b bellard
        static int last_level[64];
145 e68b9b2b bellard
        if (last_level[num] != level) {
146 ea026b2f blueswir1
            PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
147 e68b9b2b bellard
            last_level[num] = level;
148 e68b9b2b bellard
        }
149 e68b9b2b bellard
    }
150 e68b9b2b bellard
#endif
151 e68b9b2b bellard
    pic = &s->pics[1 - (num >> 5)];
152 e68b9b2b bellard
    irq_bit = 1 << (num & 0x1f);
153 e68b9b2b bellard
    if (level) {
154 e68b9b2b bellard
        pic->events |= irq_bit & ~pic->level_triggered;
155 e68b9b2b bellard
        pic->levels |= irq_bit;
156 e68b9b2b bellard
    } else {
157 e68b9b2b bellard
        pic->levels &= ~irq_bit;
158 e68b9b2b bellard
    }
159 e68b9b2b bellard
    heathrow_pic_update(s);
160 e68b9b2b bellard
}
161 e68b9b2b bellard
162 4acd38ce Juan Quintela
static const VMStateDescription vmstate_heathrow_pic_one = {
163 4acd38ce Juan Quintela
    .name = "heathrow_pic_one",
164 4acd38ce Juan Quintela
    .version_id = 0,
165 4acd38ce Juan Quintela
    .minimum_version_id = 0,
166 4acd38ce Juan Quintela
    .minimum_version_id_old = 0,
167 4acd38ce Juan Quintela
    .fields      = (VMStateField[]) {
168 4acd38ce Juan Quintela
        VMSTATE_UINT32(events, HeathrowPIC),
169 4acd38ce Juan Quintela
        VMSTATE_UINT32(mask, HeathrowPIC),
170 4acd38ce Juan Quintela
        VMSTATE_UINT32(levels, HeathrowPIC),
171 4acd38ce Juan Quintela
        VMSTATE_UINT32(level_triggered, HeathrowPIC),
172 4acd38ce Juan Quintela
        VMSTATE_END_OF_LIST()
173 4acd38ce Juan Quintela
    }
174 4acd38ce Juan Quintela
};
175 9b64997f blueswir1
176 4acd38ce Juan Quintela
static const VMStateDescription vmstate_heathrow_pic = {
177 4acd38ce Juan Quintela
    .name = "heathrow_pic",
178 4acd38ce Juan Quintela
    .version_id = 1,
179 4acd38ce Juan Quintela
    .minimum_version_id = 1,
180 4acd38ce Juan Quintela
    .minimum_version_id_old = 1,
181 4acd38ce Juan Quintela
    .fields      = (VMStateField[]) {
182 4acd38ce Juan Quintela
        VMSTATE_STRUCT_ARRAY(pics, HeathrowPICS, 2, 1,
183 4acd38ce Juan Quintela
                             vmstate_heathrow_pic_one, HeathrowPIC),
184 4acd38ce Juan Quintela
        VMSTATE_END_OF_LIST()
185 4acd38ce Juan Quintela
    }
186 4acd38ce Juan Quintela
};
187 9b64997f blueswir1
188 6e6b7363 blueswir1
static void heathrow_pic_reset_one(HeathrowPIC *s)
189 6e6b7363 blueswir1
{
190 6e6b7363 blueswir1
    memset(s, '\0', sizeof(HeathrowPIC));
191 6e6b7363 blueswir1
}
192 6e6b7363 blueswir1
193 6e6b7363 blueswir1
static void heathrow_pic_reset(void *opaque)
194 6e6b7363 blueswir1
{
195 6e6b7363 blueswir1
    HeathrowPICS *s = opaque;
196 6e6b7363 blueswir1
197 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[0]);
198 6e6b7363 blueswir1
    heathrow_pic_reset_one(&s->pics[1]);
199 6e6b7363 blueswir1
200 6e6b7363 blueswir1
    s->pics[0].level_triggered = 0;
201 6e6b7363 blueswir1
    s->pics[1].level_triggered = 0x1ff00000;
202 6e6b7363 blueswir1
}
203 6e6b7363 blueswir1
204 3cbee15b j_mayer
qemu_irq *heathrow_pic_init(int *pmem_index,
205 3cbee15b j_mayer
                            int nb_cpus, qemu_irq **irqs)
206 e68b9b2b bellard
{
207 e68b9b2b bellard
    HeathrowPICS *s;
208 3b46e624 ths
209 e68b9b2b bellard
    s = qemu_mallocz(sizeof(HeathrowPICS));
210 3cbee15b j_mayer
    /* only 1 CPU */
211 3cbee15b j_mayer
    s->irqs = irqs[0];
212 2507c12a Alexander Graf
    *pmem_index = cpu_register_io_memory(pic_read, pic_write, s,
213 b093c1a3 Alexander Graf
                                         DEVICE_LITTLE_ENDIAN);
214 3cbee15b j_mayer
215 4acd38ce Juan Quintela
    vmstate_register(NULL, -1, &vmstate_heathrow_pic, s);
216 a08d4367 Jan Kiszka
    qemu_register_reset(heathrow_pic_reset, s);
217 d537cf6c pbrook
    return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
218 e68b9b2b bellard
}