Statistics
| Branch: | Revision:

root / hw / ppc405_uc.c @ d8c6d07f

History | View | Annotate | Download (65.8 kB)

1 8ecc7913 j_mayer
/*
2 8ecc7913 j_mayer
 * QEMU PowerPC 405 embedded processors emulation
3 5fafdf24 ths
 *
4 8ecc7913 j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
5 5fafdf24 ths
 *
6 8ecc7913 j_mayer
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 8ecc7913 j_mayer
 * of this software and associated documentation files (the "Software"), to deal
8 8ecc7913 j_mayer
 * in the Software without restriction, including without limitation the rights
9 8ecc7913 j_mayer
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 8ecc7913 j_mayer
 * copies of the Software, and to permit persons to whom the Software is
11 8ecc7913 j_mayer
 * furnished to do so, subject to the following conditions:
12 8ecc7913 j_mayer
 *
13 8ecc7913 j_mayer
 * The above copyright notice and this permission notice shall be included in
14 8ecc7913 j_mayer
 * all copies or substantial portions of the Software.
15 8ecc7913 j_mayer
 *
16 8ecc7913 j_mayer
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 8ecc7913 j_mayer
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 8ecc7913 j_mayer
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 8ecc7913 j_mayer
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 8ecc7913 j_mayer
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 8ecc7913 j_mayer
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 8ecc7913 j_mayer
 * THE SOFTWARE.
23 8ecc7913 j_mayer
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "ppc.h"
26 04f20795 j_mayer
#include "ppc405.h"
27 488cb996 Gerd Hoffmann
#include "serial.h"
28 1de7afc9 Paolo Bonzini
#include "qemu/timer.h"
29 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
30 1de7afc9 Paolo Bonzini
#include "qemu/log.h"
31 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
32 8ecc7913 j_mayer
33 8ecc7913 j_mayer
#define DEBUG_OPBA
34 8ecc7913 j_mayer
#define DEBUG_SDRAM
35 8ecc7913 j_mayer
#define DEBUG_GPIO
36 8ecc7913 j_mayer
#define DEBUG_SERIAL
37 8ecc7913 j_mayer
#define DEBUG_OCM
38 9c02f1a2 j_mayer
//#define DEBUG_I2C
39 9c02f1a2 j_mayer
#define DEBUG_GPT
40 9c02f1a2 j_mayer
#define DEBUG_MAL
41 8ecc7913 j_mayer
#define DEBUG_CLOCKS
42 aae9366a j_mayer
//#define DEBUG_CLOCKS_LL
43 8ecc7913 j_mayer
44 e2684c0b Andreas Färber
ram_addr_t ppc405_set_bootinfo (CPUPPCState *env, ppc4xx_bd_info_t *bd,
45 b8d3f5d1 j_mayer
                                uint32_t flags)
46 04f20795 j_mayer
{
47 c227f099 Anthony Liguori
    ram_addr_t bdloc;
48 04f20795 j_mayer
    int i, n;
49 04f20795 j_mayer
50 04f20795 j_mayer
    /* We put the bd structure at the top of memory */
51 be58fc7c j_mayer
    if (bd->bi_memsize >= 0x01000000UL)
52 c227f099 Anthony Liguori
        bdloc = 0x01000000UL - sizeof(struct ppc4xx_bd_info_t);
53 be58fc7c j_mayer
    else
54 c227f099 Anthony Liguori
        bdloc = bd->bi_memsize - sizeof(struct ppc4xx_bd_info_t);
55 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x00, bd->bi_memstart);
56 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x04, bd->bi_memsize);
57 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x08, bd->bi_flashstart);
58 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x0C, bd->bi_flashsize);
59 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x10, bd->bi_flashoffset);
60 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x14, bd->bi_sramstart);
61 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x18, bd->bi_sramsize);
62 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x1C, bd->bi_bootflags);
63 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x20, bd->bi_ipaddr);
64 db663d0f Alexander Graf
    for (i = 0; i < 6; i++) {
65 5c130f65 pbrook
        stb_phys(bdloc + 0x24 + i, bd->bi_enetaddr[i]);
66 db663d0f Alexander Graf
    }
67 db663d0f Alexander Graf
    stw_be_phys(bdloc + 0x2A, bd->bi_ethspeed);
68 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x2C, bd->bi_intfreq);
69 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x30, bd->bi_busfreq);
70 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x34, bd->bi_baudrate);
71 db663d0f Alexander Graf
    for (i = 0; i < 4; i++) {
72 5c130f65 pbrook
        stb_phys(bdloc + 0x38 + i, bd->bi_s_version[i]);
73 db663d0f Alexander Graf
    }
74 4508d81a Blue Swirl
    for (i = 0; i < 32; i++) {
75 4508d81a Blue Swirl
        stb_phys(bdloc + 0x3C + i, bd->bi_r_version[i]);
76 4508d81a Blue Swirl
    }
77 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x5C, bd->bi_plb_busfreq);
78 db663d0f Alexander Graf
    stl_be_phys(bdloc + 0x60, bd->bi_pci_busfreq);
79 db663d0f Alexander Graf
    for (i = 0; i < 6; i++) {
80 5c130f65 pbrook
        stb_phys(bdloc + 0x64 + i, bd->bi_pci_enetaddr[i]);
81 db663d0f Alexander Graf
    }
82 04f20795 j_mayer
    n = 0x6A;
83 b8d3f5d1 j_mayer
    if (flags & 0x00000001) {
84 04f20795 j_mayer
        for (i = 0; i < 6; i++)
85 5c130f65 pbrook
            stb_phys(bdloc + n++, bd->bi_pci_enetaddr2[i]);
86 04f20795 j_mayer
    }
87 db663d0f Alexander Graf
    stl_be_phys(bdloc + n, bd->bi_opbfreq);
88 04f20795 j_mayer
    n += 4;
89 04f20795 j_mayer
    for (i = 0; i < 2; i++) {
90 db663d0f Alexander Graf
        stl_be_phys(bdloc + n, bd->bi_iic_fast[i]);
91 04f20795 j_mayer
        n += 4;
92 04f20795 j_mayer
    }
93 04f20795 j_mayer
94 04f20795 j_mayer
    return bdloc;
95 04f20795 j_mayer
}
96 04f20795 j_mayer
97 8ecc7913 j_mayer
/*****************************************************************************/
98 8ecc7913 j_mayer
/* Shared peripherals */
99 8ecc7913 j_mayer
100 8ecc7913 j_mayer
/*****************************************************************************/
101 8ecc7913 j_mayer
/* Peripheral local bus arbitrer */
102 8ecc7913 j_mayer
enum {
103 8ecc7913 j_mayer
    PLB0_BESR = 0x084,
104 8ecc7913 j_mayer
    PLB0_BEAR = 0x086,
105 8ecc7913 j_mayer
    PLB0_ACR  = 0x087,
106 8ecc7913 j_mayer
};
107 8ecc7913 j_mayer
108 c227f099 Anthony Liguori
typedef struct ppc4xx_plb_t ppc4xx_plb_t;
109 c227f099 Anthony Liguori
struct ppc4xx_plb_t {
110 8ecc7913 j_mayer
    uint32_t acr;
111 8ecc7913 j_mayer
    uint32_t bear;
112 8ecc7913 j_mayer
    uint32_t besr;
113 8ecc7913 j_mayer
};
114 8ecc7913 j_mayer
115 73b01960 Alexander Graf
static uint32_t dcr_read_plb (void *opaque, int dcrn)
116 8ecc7913 j_mayer
{
117 c227f099 Anthony Liguori
    ppc4xx_plb_t *plb;
118 73b01960 Alexander Graf
    uint32_t ret;
119 8ecc7913 j_mayer
120 8ecc7913 j_mayer
    plb = opaque;
121 8ecc7913 j_mayer
    switch (dcrn) {
122 8ecc7913 j_mayer
    case PLB0_ACR:
123 8ecc7913 j_mayer
        ret = plb->acr;
124 8ecc7913 j_mayer
        break;
125 8ecc7913 j_mayer
    case PLB0_BEAR:
126 8ecc7913 j_mayer
        ret = plb->bear;
127 8ecc7913 j_mayer
        break;
128 8ecc7913 j_mayer
    case PLB0_BESR:
129 8ecc7913 j_mayer
        ret = plb->besr;
130 8ecc7913 j_mayer
        break;
131 8ecc7913 j_mayer
    default:
132 8ecc7913 j_mayer
        /* Avoid gcc warning */
133 8ecc7913 j_mayer
        ret = 0;
134 8ecc7913 j_mayer
        break;
135 8ecc7913 j_mayer
    }
136 8ecc7913 j_mayer
137 8ecc7913 j_mayer
    return ret;
138 8ecc7913 j_mayer
}
139 8ecc7913 j_mayer
140 73b01960 Alexander Graf
static void dcr_write_plb (void *opaque, int dcrn, uint32_t val)
141 8ecc7913 j_mayer
{
142 c227f099 Anthony Liguori
    ppc4xx_plb_t *plb;
143 8ecc7913 j_mayer
144 8ecc7913 j_mayer
    plb = opaque;
145 8ecc7913 j_mayer
    switch (dcrn) {
146 8ecc7913 j_mayer
    case PLB0_ACR:
147 9c02f1a2 j_mayer
        /* We don't care about the actual parameters written as
148 9c02f1a2 j_mayer
         * we don't manage any priorities on the bus
149 9c02f1a2 j_mayer
         */
150 9c02f1a2 j_mayer
        plb->acr = val & 0xF8000000;
151 8ecc7913 j_mayer
        break;
152 8ecc7913 j_mayer
    case PLB0_BEAR:
153 8ecc7913 j_mayer
        /* Read only */
154 8ecc7913 j_mayer
        break;
155 8ecc7913 j_mayer
    case PLB0_BESR:
156 8ecc7913 j_mayer
        /* Write-clear */
157 8ecc7913 j_mayer
        plb->besr &= ~val;
158 8ecc7913 j_mayer
        break;
159 8ecc7913 j_mayer
    }
160 8ecc7913 j_mayer
}
161 8ecc7913 j_mayer
162 8ecc7913 j_mayer
static void ppc4xx_plb_reset (void *opaque)
163 8ecc7913 j_mayer
{
164 c227f099 Anthony Liguori
    ppc4xx_plb_t *plb;
165 8ecc7913 j_mayer
166 8ecc7913 j_mayer
    plb = opaque;
167 8ecc7913 j_mayer
    plb->acr = 0x00000000;
168 8ecc7913 j_mayer
    plb->bear = 0x00000000;
169 8ecc7913 j_mayer
    plb->besr = 0x00000000;
170 8ecc7913 j_mayer
}
171 8ecc7913 j_mayer
172 e2684c0b Andreas Färber
static void ppc4xx_plb_init(CPUPPCState *env)
173 8ecc7913 j_mayer
{
174 c227f099 Anthony Liguori
    ppc4xx_plb_t *plb;
175 8ecc7913 j_mayer
176 7267c094 Anthony Liguori
    plb = g_malloc0(sizeof(ppc4xx_plb_t));
177 487414f1 aliguori
    ppc_dcr_register(env, PLB0_ACR, plb, &dcr_read_plb, &dcr_write_plb);
178 487414f1 aliguori
    ppc_dcr_register(env, PLB0_BEAR, plb, &dcr_read_plb, &dcr_write_plb);
179 487414f1 aliguori
    ppc_dcr_register(env, PLB0_BESR, plb, &dcr_read_plb, &dcr_write_plb);
180 a08d4367 Jan Kiszka
    qemu_register_reset(ppc4xx_plb_reset, plb);
181 8ecc7913 j_mayer
}
182 8ecc7913 j_mayer
183 8ecc7913 j_mayer
/*****************************************************************************/
184 8ecc7913 j_mayer
/* PLB to OPB bridge */
185 8ecc7913 j_mayer
enum {
186 8ecc7913 j_mayer
    POB0_BESR0 = 0x0A0,
187 8ecc7913 j_mayer
    POB0_BESR1 = 0x0A2,
188 8ecc7913 j_mayer
    POB0_BEAR  = 0x0A4,
189 8ecc7913 j_mayer
};
190 8ecc7913 j_mayer
191 c227f099 Anthony Liguori
typedef struct ppc4xx_pob_t ppc4xx_pob_t;
192 c227f099 Anthony Liguori
struct ppc4xx_pob_t {
193 8ecc7913 j_mayer
    uint32_t bear;
194 5a1972c8 Stefan Weil
    uint32_t besr0;
195 5a1972c8 Stefan Weil
    uint32_t besr1;
196 8ecc7913 j_mayer
};
197 8ecc7913 j_mayer
198 73b01960 Alexander Graf
static uint32_t dcr_read_pob (void *opaque, int dcrn)
199 8ecc7913 j_mayer
{
200 c227f099 Anthony Liguori
    ppc4xx_pob_t *pob;
201 73b01960 Alexander Graf
    uint32_t ret;
202 8ecc7913 j_mayer
203 8ecc7913 j_mayer
    pob = opaque;
204 8ecc7913 j_mayer
    switch (dcrn) {
205 8ecc7913 j_mayer
    case POB0_BEAR:
206 8ecc7913 j_mayer
        ret = pob->bear;
207 8ecc7913 j_mayer
        break;
208 8ecc7913 j_mayer
    case POB0_BESR0:
209 5a1972c8 Stefan Weil
        ret = pob->besr0;
210 5a1972c8 Stefan Weil
        break;
211 8ecc7913 j_mayer
    case POB0_BESR1:
212 5a1972c8 Stefan Weil
        ret = pob->besr1;
213 8ecc7913 j_mayer
        break;
214 8ecc7913 j_mayer
    default:
215 8ecc7913 j_mayer
        /* Avoid gcc warning */
216 8ecc7913 j_mayer
        ret = 0;
217 8ecc7913 j_mayer
        break;
218 8ecc7913 j_mayer
    }
219 8ecc7913 j_mayer
220 8ecc7913 j_mayer
    return ret;
221 8ecc7913 j_mayer
}
222 8ecc7913 j_mayer
223 73b01960 Alexander Graf
static void dcr_write_pob (void *opaque, int dcrn, uint32_t val)
224 8ecc7913 j_mayer
{
225 c227f099 Anthony Liguori
    ppc4xx_pob_t *pob;
226 8ecc7913 j_mayer
227 8ecc7913 j_mayer
    pob = opaque;
228 8ecc7913 j_mayer
    switch (dcrn) {
229 8ecc7913 j_mayer
    case POB0_BEAR:
230 8ecc7913 j_mayer
        /* Read only */
231 8ecc7913 j_mayer
        break;
232 8ecc7913 j_mayer
    case POB0_BESR0:
233 5a1972c8 Stefan Weil
        /* Write-clear */
234 5a1972c8 Stefan Weil
        pob->besr0 &= ~val;
235 5a1972c8 Stefan Weil
        break;
236 8ecc7913 j_mayer
    case POB0_BESR1:
237 8ecc7913 j_mayer
        /* Write-clear */
238 5a1972c8 Stefan Weil
        pob->besr1 &= ~val;
239 8ecc7913 j_mayer
        break;
240 8ecc7913 j_mayer
    }
241 8ecc7913 j_mayer
}
242 8ecc7913 j_mayer
243 8ecc7913 j_mayer
static void ppc4xx_pob_reset (void *opaque)
244 8ecc7913 j_mayer
{
245 c227f099 Anthony Liguori
    ppc4xx_pob_t *pob;
246 8ecc7913 j_mayer
247 8ecc7913 j_mayer
    pob = opaque;
248 8ecc7913 j_mayer
    /* No error */
249 8ecc7913 j_mayer
    pob->bear = 0x00000000;
250 5a1972c8 Stefan Weil
    pob->besr0 = 0x0000000;
251 5a1972c8 Stefan Weil
    pob->besr1 = 0x0000000;
252 8ecc7913 j_mayer
}
253 8ecc7913 j_mayer
254 e2684c0b Andreas Färber
static void ppc4xx_pob_init(CPUPPCState *env)
255 8ecc7913 j_mayer
{
256 c227f099 Anthony Liguori
    ppc4xx_pob_t *pob;
257 8ecc7913 j_mayer
258 7267c094 Anthony Liguori
    pob = g_malloc0(sizeof(ppc4xx_pob_t));
259 487414f1 aliguori
    ppc_dcr_register(env, POB0_BEAR, pob, &dcr_read_pob, &dcr_write_pob);
260 487414f1 aliguori
    ppc_dcr_register(env, POB0_BESR0, pob, &dcr_read_pob, &dcr_write_pob);
261 487414f1 aliguori
    ppc_dcr_register(env, POB0_BESR1, pob, &dcr_read_pob, &dcr_write_pob);
262 a08d4367 Jan Kiszka
    qemu_register_reset(ppc4xx_pob_reset, pob);
263 8ecc7913 j_mayer
}
264 8ecc7913 j_mayer
265 8ecc7913 j_mayer
/*****************************************************************************/
266 8ecc7913 j_mayer
/* OPB arbitrer */
267 c227f099 Anthony Liguori
typedef struct ppc4xx_opba_t ppc4xx_opba_t;
268 c227f099 Anthony Liguori
struct ppc4xx_opba_t {
269 9074e0e3 Avi Kivity
    MemoryRegion io;
270 8ecc7913 j_mayer
    uint8_t cr;
271 8ecc7913 j_mayer
    uint8_t pr;
272 8ecc7913 j_mayer
};
273 8ecc7913 j_mayer
274 a8170e5e Avi Kivity
static uint32_t opba_readb (void *opaque, hwaddr addr)
275 8ecc7913 j_mayer
{
276 c227f099 Anthony Liguori
    ppc4xx_opba_t *opba;
277 8ecc7913 j_mayer
    uint32_t ret;
278 8ecc7913 j_mayer
279 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
280 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
281 8ecc7913 j_mayer
#endif
282 8ecc7913 j_mayer
    opba = opaque;
283 802670e6 Blue Swirl
    switch (addr) {
284 8ecc7913 j_mayer
    case 0x00:
285 8ecc7913 j_mayer
        ret = opba->cr;
286 8ecc7913 j_mayer
        break;
287 8ecc7913 j_mayer
    case 0x01:
288 8ecc7913 j_mayer
        ret = opba->pr;
289 8ecc7913 j_mayer
        break;
290 8ecc7913 j_mayer
    default:
291 8ecc7913 j_mayer
        ret = 0x00;
292 8ecc7913 j_mayer
        break;
293 8ecc7913 j_mayer
    }
294 8ecc7913 j_mayer
295 8ecc7913 j_mayer
    return ret;
296 8ecc7913 j_mayer
}
297 8ecc7913 j_mayer
298 8ecc7913 j_mayer
static void opba_writeb (void *opaque,
299 a8170e5e Avi Kivity
                         hwaddr addr, uint32_t value)
300 8ecc7913 j_mayer
{
301 c227f099 Anthony Liguori
    ppc4xx_opba_t *opba;
302 8ecc7913 j_mayer
303 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
304 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
305 90e189ec Blue Swirl
           value);
306 8ecc7913 j_mayer
#endif
307 8ecc7913 j_mayer
    opba = opaque;
308 802670e6 Blue Swirl
    switch (addr) {
309 8ecc7913 j_mayer
    case 0x00:
310 8ecc7913 j_mayer
        opba->cr = value & 0xF8;
311 8ecc7913 j_mayer
        break;
312 8ecc7913 j_mayer
    case 0x01:
313 8ecc7913 j_mayer
        opba->pr = value & 0xFF;
314 8ecc7913 j_mayer
        break;
315 8ecc7913 j_mayer
    default:
316 8ecc7913 j_mayer
        break;
317 8ecc7913 j_mayer
    }
318 8ecc7913 j_mayer
}
319 8ecc7913 j_mayer
320 a8170e5e Avi Kivity
static uint32_t opba_readw (void *opaque, hwaddr addr)
321 8ecc7913 j_mayer
{
322 8ecc7913 j_mayer
    uint32_t ret;
323 8ecc7913 j_mayer
324 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
325 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
326 8ecc7913 j_mayer
#endif
327 8ecc7913 j_mayer
    ret = opba_readb(opaque, addr) << 8;
328 8ecc7913 j_mayer
    ret |= opba_readb(opaque, addr + 1);
329 8ecc7913 j_mayer
330 8ecc7913 j_mayer
    return ret;
331 8ecc7913 j_mayer
}
332 8ecc7913 j_mayer
333 8ecc7913 j_mayer
static void opba_writew (void *opaque,
334 a8170e5e Avi Kivity
                         hwaddr addr, uint32_t value)
335 8ecc7913 j_mayer
{
336 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
337 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
338 90e189ec Blue Swirl
           value);
339 8ecc7913 j_mayer
#endif
340 8ecc7913 j_mayer
    opba_writeb(opaque, addr, value >> 8);
341 8ecc7913 j_mayer
    opba_writeb(opaque, addr + 1, value);
342 8ecc7913 j_mayer
}
343 8ecc7913 j_mayer
344 a8170e5e Avi Kivity
static uint32_t opba_readl (void *opaque, hwaddr addr)
345 8ecc7913 j_mayer
{
346 8ecc7913 j_mayer
    uint32_t ret;
347 8ecc7913 j_mayer
348 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
349 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
350 8ecc7913 j_mayer
#endif
351 8ecc7913 j_mayer
    ret = opba_readb(opaque, addr) << 24;
352 8ecc7913 j_mayer
    ret |= opba_readb(opaque, addr + 1) << 16;
353 8ecc7913 j_mayer
354 8ecc7913 j_mayer
    return ret;
355 8ecc7913 j_mayer
}
356 8ecc7913 j_mayer
357 8ecc7913 j_mayer
static void opba_writel (void *opaque,
358 a8170e5e Avi Kivity
                         hwaddr addr, uint32_t value)
359 8ecc7913 j_mayer
{
360 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
361 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
362 90e189ec Blue Swirl
           value);
363 8ecc7913 j_mayer
#endif
364 8ecc7913 j_mayer
    opba_writeb(opaque, addr, value >> 24);
365 8ecc7913 j_mayer
    opba_writeb(opaque, addr + 1, value >> 16);
366 8ecc7913 j_mayer
}
367 8ecc7913 j_mayer
368 9074e0e3 Avi Kivity
static const MemoryRegionOps opba_ops = {
369 9074e0e3 Avi Kivity
    .old_mmio = {
370 9074e0e3 Avi Kivity
        .read = { opba_readb, opba_readw, opba_readl, },
371 9074e0e3 Avi Kivity
        .write = { opba_writeb, opba_writew, opba_writel, },
372 9074e0e3 Avi Kivity
    },
373 9074e0e3 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
374 8ecc7913 j_mayer
};
375 8ecc7913 j_mayer
376 8ecc7913 j_mayer
static void ppc4xx_opba_reset (void *opaque)
377 8ecc7913 j_mayer
{
378 c227f099 Anthony Liguori
    ppc4xx_opba_t *opba;
379 8ecc7913 j_mayer
380 8ecc7913 j_mayer
    opba = opaque;
381 8ecc7913 j_mayer
    opba->cr = 0x00; /* No dynamic priorities - park disabled */
382 8ecc7913 j_mayer
    opba->pr = 0x11;
383 8ecc7913 j_mayer
}
384 8ecc7913 j_mayer
385 a8170e5e Avi Kivity
static void ppc4xx_opba_init(hwaddr base)
386 8ecc7913 j_mayer
{
387 c227f099 Anthony Liguori
    ppc4xx_opba_t *opba;
388 8ecc7913 j_mayer
389 7267c094 Anthony Liguori
    opba = g_malloc0(sizeof(ppc4xx_opba_t));
390 8ecc7913 j_mayer
#ifdef DEBUG_OPBA
391 90e189ec Blue Swirl
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
392 8ecc7913 j_mayer
#endif
393 9074e0e3 Avi Kivity
    memory_region_init_io(&opba->io, &opba_ops, opba, "opba", 0x002);
394 9074e0e3 Avi Kivity
    memory_region_add_subregion(get_system_memory(), base, &opba->io);
395 802670e6 Blue Swirl
    qemu_register_reset(ppc4xx_opba_reset, opba);
396 8ecc7913 j_mayer
}
397 8ecc7913 j_mayer
398 8ecc7913 j_mayer
/*****************************************************************************/
399 8ecc7913 j_mayer
/* Code decompression controller */
400 8ecc7913 j_mayer
/* XXX: TODO */
401 8ecc7913 j_mayer
402 8ecc7913 j_mayer
/*****************************************************************************/
403 8ecc7913 j_mayer
/* Peripheral controller */
404 c227f099 Anthony Liguori
typedef struct ppc4xx_ebc_t ppc4xx_ebc_t;
405 c227f099 Anthony Liguori
struct ppc4xx_ebc_t {
406 8ecc7913 j_mayer
    uint32_t addr;
407 8ecc7913 j_mayer
    uint32_t bcr[8];
408 8ecc7913 j_mayer
    uint32_t bap[8];
409 8ecc7913 j_mayer
    uint32_t bear;
410 8ecc7913 j_mayer
    uint32_t besr0;
411 8ecc7913 j_mayer
    uint32_t besr1;
412 8ecc7913 j_mayer
    uint32_t cfg;
413 8ecc7913 j_mayer
};
414 8ecc7913 j_mayer
415 8ecc7913 j_mayer
enum {
416 8ecc7913 j_mayer
    EBC0_CFGADDR = 0x012,
417 8ecc7913 j_mayer
    EBC0_CFGDATA = 0x013,
418 8ecc7913 j_mayer
};
419 8ecc7913 j_mayer
420 73b01960 Alexander Graf
static uint32_t dcr_read_ebc (void *opaque, int dcrn)
421 8ecc7913 j_mayer
{
422 c227f099 Anthony Liguori
    ppc4xx_ebc_t *ebc;
423 73b01960 Alexander Graf
    uint32_t ret;
424 8ecc7913 j_mayer
425 8ecc7913 j_mayer
    ebc = opaque;
426 8ecc7913 j_mayer
    switch (dcrn) {
427 8ecc7913 j_mayer
    case EBC0_CFGADDR:
428 8ecc7913 j_mayer
        ret = ebc->addr;
429 8ecc7913 j_mayer
        break;
430 8ecc7913 j_mayer
    case EBC0_CFGDATA:
431 8ecc7913 j_mayer
        switch (ebc->addr) {
432 8ecc7913 j_mayer
        case 0x00: /* B0CR */
433 8ecc7913 j_mayer
            ret = ebc->bcr[0];
434 8ecc7913 j_mayer
            break;
435 8ecc7913 j_mayer
        case 0x01: /* B1CR */
436 8ecc7913 j_mayer
            ret = ebc->bcr[1];
437 8ecc7913 j_mayer
            break;
438 8ecc7913 j_mayer
        case 0x02: /* B2CR */
439 8ecc7913 j_mayer
            ret = ebc->bcr[2];
440 8ecc7913 j_mayer
            break;
441 8ecc7913 j_mayer
        case 0x03: /* B3CR */
442 8ecc7913 j_mayer
            ret = ebc->bcr[3];
443 8ecc7913 j_mayer
            break;
444 8ecc7913 j_mayer
        case 0x04: /* B4CR */
445 8ecc7913 j_mayer
            ret = ebc->bcr[4];
446 8ecc7913 j_mayer
            break;
447 8ecc7913 j_mayer
        case 0x05: /* B5CR */
448 8ecc7913 j_mayer
            ret = ebc->bcr[5];
449 8ecc7913 j_mayer
            break;
450 8ecc7913 j_mayer
        case 0x06: /* B6CR */
451 8ecc7913 j_mayer
            ret = ebc->bcr[6];
452 8ecc7913 j_mayer
            break;
453 8ecc7913 j_mayer
        case 0x07: /* B7CR */
454 8ecc7913 j_mayer
            ret = ebc->bcr[7];
455 8ecc7913 j_mayer
            break;
456 8ecc7913 j_mayer
        case 0x10: /* B0AP */
457 8ecc7913 j_mayer
            ret = ebc->bap[0];
458 8ecc7913 j_mayer
            break;
459 8ecc7913 j_mayer
        case 0x11: /* B1AP */
460 8ecc7913 j_mayer
            ret = ebc->bap[1];
461 8ecc7913 j_mayer
            break;
462 8ecc7913 j_mayer
        case 0x12: /* B2AP */
463 8ecc7913 j_mayer
            ret = ebc->bap[2];
464 8ecc7913 j_mayer
            break;
465 8ecc7913 j_mayer
        case 0x13: /* B3AP */
466 8ecc7913 j_mayer
            ret = ebc->bap[3];
467 8ecc7913 j_mayer
            break;
468 8ecc7913 j_mayer
        case 0x14: /* B4AP */
469 8ecc7913 j_mayer
            ret = ebc->bap[4];
470 8ecc7913 j_mayer
            break;
471 8ecc7913 j_mayer
        case 0x15: /* B5AP */
472 8ecc7913 j_mayer
            ret = ebc->bap[5];
473 8ecc7913 j_mayer
            break;
474 8ecc7913 j_mayer
        case 0x16: /* B6AP */
475 8ecc7913 j_mayer
            ret = ebc->bap[6];
476 8ecc7913 j_mayer
            break;
477 8ecc7913 j_mayer
        case 0x17: /* B7AP */
478 8ecc7913 j_mayer
            ret = ebc->bap[7];
479 8ecc7913 j_mayer
            break;
480 8ecc7913 j_mayer
        case 0x20: /* BEAR */
481 8ecc7913 j_mayer
            ret = ebc->bear;
482 8ecc7913 j_mayer
            break;
483 8ecc7913 j_mayer
        case 0x21: /* BESR0 */
484 8ecc7913 j_mayer
            ret = ebc->besr0;
485 8ecc7913 j_mayer
            break;
486 8ecc7913 j_mayer
        case 0x22: /* BESR1 */
487 8ecc7913 j_mayer
            ret = ebc->besr1;
488 8ecc7913 j_mayer
            break;
489 8ecc7913 j_mayer
        case 0x23: /* CFG */
490 8ecc7913 j_mayer
            ret = ebc->cfg;
491 8ecc7913 j_mayer
            break;
492 8ecc7913 j_mayer
        default:
493 8ecc7913 j_mayer
            ret = 0x00000000;
494 8ecc7913 j_mayer
            break;
495 8ecc7913 j_mayer
        }
496 9fad3eb7 Blue Swirl
        break;
497 8ecc7913 j_mayer
    default:
498 8ecc7913 j_mayer
        ret = 0x00000000;
499 8ecc7913 j_mayer
        break;
500 8ecc7913 j_mayer
    }
501 8ecc7913 j_mayer
502 8ecc7913 j_mayer
    return ret;
503 8ecc7913 j_mayer
}
504 8ecc7913 j_mayer
505 73b01960 Alexander Graf
static void dcr_write_ebc (void *opaque, int dcrn, uint32_t val)
506 8ecc7913 j_mayer
{
507 c227f099 Anthony Liguori
    ppc4xx_ebc_t *ebc;
508 8ecc7913 j_mayer
509 8ecc7913 j_mayer
    ebc = opaque;
510 8ecc7913 j_mayer
    switch (dcrn) {
511 8ecc7913 j_mayer
    case EBC0_CFGADDR:
512 8ecc7913 j_mayer
        ebc->addr = val;
513 8ecc7913 j_mayer
        break;
514 8ecc7913 j_mayer
    case EBC0_CFGDATA:
515 8ecc7913 j_mayer
        switch (ebc->addr) {
516 8ecc7913 j_mayer
        case 0x00: /* B0CR */
517 8ecc7913 j_mayer
            break;
518 8ecc7913 j_mayer
        case 0x01: /* B1CR */
519 8ecc7913 j_mayer
            break;
520 8ecc7913 j_mayer
        case 0x02: /* B2CR */
521 8ecc7913 j_mayer
            break;
522 8ecc7913 j_mayer
        case 0x03: /* B3CR */
523 8ecc7913 j_mayer
            break;
524 8ecc7913 j_mayer
        case 0x04: /* B4CR */
525 8ecc7913 j_mayer
            break;
526 8ecc7913 j_mayer
        case 0x05: /* B5CR */
527 8ecc7913 j_mayer
            break;
528 8ecc7913 j_mayer
        case 0x06: /* B6CR */
529 8ecc7913 j_mayer
            break;
530 8ecc7913 j_mayer
        case 0x07: /* B7CR */
531 8ecc7913 j_mayer
            break;
532 8ecc7913 j_mayer
        case 0x10: /* B0AP */
533 8ecc7913 j_mayer
            break;
534 8ecc7913 j_mayer
        case 0x11: /* B1AP */
535 8ecc7913 j_mayer
            break;
536 8ecc7913 j_mayer
        case 0x12: /* B2AP */
537 8ecc7913 j_mayer
            break;
538 8ecc7913 j_mayer
        case 0x13: /* B3AP */
539 8ecc7913 j_mayer
            break;
540 8ecc7913 j_mayer
        case 0x14: /* B4AP */
541 8ecc7913 j_mayer
            break;
542 8ecc7913 j_mayer
        case 0x15: /* B5AP */
543 8ecc7913 j_mayer
            break;
544 8ecc7913 j_mayer
        case 0x16: /* B6AP */
545 8ecc7913 j_mayer
            break;
546 8ecc7913 j_mayer
        case 0x17: /* B7AP */
547 8ecc7913 j_mayer
            break;
548 8ecc7913 j_mayer
        case 0x20: /* BEAR */
549 8ecc7913 j_mayer
            break;
550 8ecc7913 j_mayer
        case 0x21: /* BESR0 */
551 8ecc7913 j_mayer
            break;
552 8ecc7913 j_mayer
        case 0x22: /* BESR1 */
553 8ecc7913 j_mayer
            break;
554 8ecc7913 j_mayer
        case 0x23: /* CFG */
555 8ecc7913 j_mayer
            break;
556 8ecc7913 j_mayer
        default:
557 8ecc7913 j_mayer
            break;
558 8ecc7913 j_mayer
        }
559 8ecc7913 j_mayer
        break;
560 8ecc7913 j_mayer
    default:
561 8ecc7913 j_mayer
        break;
562 8ecc7913 j_mayer
    }
563 8ecc7913 j_mayer
}
564 8ecc7913 j_mayer
565 8ecc7913 j_mayer
static void ebc_reset (void *opaque)
566 8ecc7913 j_mayer
{
567 c227f099 Anthony Liguori
    ppc4xx_ebc_t *ebc;
568 8ecc7913 j_mayer
    int i;
569 8ecc7913 j_mayer
570 8ecc7913 j_mayer
    ebc = opaque;
571 8ecc7913 j_mayer
    ebc->addr = 0x00000000;
572 8ecc7913 j_mayer
    ebc->bap[0] = 0x7F8FFE80;
573 8ecc7913 j_mayer
    ebc->bcr[0] = 0xFFE28000;
574 8ecc7913 j_mayer
    for (i = 0; i < 8; i++) {
575 8ecc7913 j_mayer
        ebc->bap[i] = 0x00000000;
576 8ecc7913 j_mayer
        ebc->bcr[i] = 0x00000000;
577 8ecc7913 j_mayer
    }
578 8ecc7913 j_mayer
    ebc->besr0 = 0x00000000;
579 8ecc7913 j_mayer
    ebc->besr1 = 0x00000000;
580 9c02f1a2 j_mayer
    ebc->cfg = 0x80400000;
581 8ecc7913 j_mayer
}
582 8ecc7913 j_mayer
583 e2684c0b Andreas Färber
static void ppc405_ebc_init(CPUPPCState *env)
584 8ecc7913 j_mayer
{
585 c227f099 Anthony Liguori
    ppc4xx_ebc_t *ebc;
586 8ecc7913 j_mayer
587 7267c094 Anthony Liguori
    ebc = g_malloc0(sizeof(ppc4xx_ebc_t));
588 a08d4367 Jan Kiszka
    qemu_register_reset(&ebc_reset, ebc);
589 487414f1 aliguori
    ppc_dcr_register(env, EBC0_CFGADDR,
590 487414f1 aliguori
                     ebc, &dcr_read_ebc, &dcr_write_ebc);
591 487414f1 aliguori
    ppc_dcr_register(env, EBC0_CFGDATA,
592 487414f1 aliguori
                     ebc, &dcr_read_ebc, &dcr_write_ebc);
593 8ecc7913 j_mayer
}
594 8ecc7913 j_mayer
595 8ecc7913 j_mayer
/*****************************************************************************/
596 8ecc7913 j_mayer
/* DMA controller */
597 8ecc7913 j_mayer
enum {
598 8ecc7913 j_mayer
    DMA0_CR0 = 0x100,
599 8ecc7913 j_mayer
    DMA0_CT0 = 0x101,
600 8ecc7913 j_mayer
    DMA0_DA0 = 0x102,
601 8ecc7913 j_mayer
    DMA0_SA0 = 0x103,
602 8ecc7913 j_mayer
    DMA0_SG0 = 0x104,
603 8ecc7913 j_mayer
    DMA0_CR1 = 0x108,
604 8ecc7913 j_mayer
    DMA0_CT1 = 0x109,
605 8ecc7913 j_mayer
    DMA0_DA1 = 0x10A,
606 8ecc7913 j_mayer
    DMA0_SA1 = 0x10B,
607 8ecc7913 j_mayer
    DMA0_SG1 = 0x10C,
608 8ecc7913 j_mayer
    DMA0_CR2 = 0x110,
609 8ecc7913 j_mayer
    DMA0_CT2 = 0x111,
610 8ecc7913 j_mayer
    DMA0_DA2 = 0x112,
611 8ecc7913 j_mayer
    DMA0_SA2 = 0x113,
612 8ecc7913 j_mayer
    DMA0_SG2 = 0x114,
613 8ecc7913 j_mayer
    DMA0_CR3 = 0x118,
614 8ecc7913 j_mayer
    DMA0_CT3 = 0x119,
615 8ecc7913 j_mayer
    DMA0_DA3 = 0x11A,
616 8ecc7913 j_mayer
    DMA0_SA3 = 0x11B,
617 8ecc7913 j_mayer
    DMA0_SG3 = 0x11C,
618 8ecc7913 j_mayer
    DMA0_SR  = 0x120,
619 8ecc7913 j_mayer
    DMA0_SGC = 0x123,
620 8ecc7913 j_mayer
    DMA0_SLP = 0x125,
621 8ecc7913 j_mayer
    DMA0_POL = 0x126,
622 8ecc7913 j_mayer
};
623 8ecc7913 j_mayer
624 c227f099 Anthony Liguori
typedef struct ppc405_dma_t ppc405_dma_t;
625 c227f099 Anthony Liguori
struct ppc405_dma_t {
626 8ecc7913 j_mayer
    qemu_irq irqs[4];
627 8ecc7913 j_mayer
    uint32_t cr[4];
628 8ecc7913 j_mayer
    uint32_t ct[4];
629 8ecc7913 j_mayer
    uint32_t da[4];
630 8ecc7913 j_mayer
    uint32_t sa[4];
631 8ecc7913 j_mayer
    uint32_t sg[4];
632 8ecc7913 j_mayer
    uint32_t sr;
633 8ecc7913 j_mayer
    uint32_t sgc;
634 8ecc7913 j_mayer
    uint32_t slp;
635 8ecc7913 j_mayer
    uint32_t pol;
636 8ecc7913 j_mayer
};
637 8ecc7913 j_mayer
638 73b01960 Alexander Graf
static uint32_t dcr_read_dma (void *opaque, int dcrn)
639 8ecc7913 j_mayer
{
640 8ecc7913 j_mayer
    return 0;
641 8ecc7913 j_mayer
}
642 8ecc7913 j_mayer
643 73b01960 Alexander Graf
static void dcr_write_dma (void *opaque, int dcrn, uint32_t val)
644 8ecc7913 j_mayer
{
645 8ecc7913 j_mayer
}
646 8ecc7913 j_mayer
647 8ecc7913 j_mayer
static void ppc405_dma_reset (void *opaque)
648 8ecc7913 j_mayer
{
649 c227f099 Anthony Liguori
    ppc405_dma_t *dma;
650 8ecc7913 j_mayer
    int i;
651 8ecc7913 j_mayer
652 8ecc7913 j_mayer
    dma = opaque;
653 8ecc7913 j_mayer
    for (i = 0; i < 4; i++) {
654 8ecc7913 j_mayer
        dma->cr[i] = 0x00000000;
655 8ecc7913 j_mayer
        dma->ct[i] = 0x00000000;
656 8ecc7913 j_mayer
        dma->da[i] = 0x00000000;
657 8ecc7913 j_mayer
        dma->sa[i] = 0x00000000;
658 8ecc7913 j_mayer
        dma->sg[i] = 0x00000000;
659 8ecc7913 j_mayer
    }
660 8ecc7913 j_mayer
    dma->sr = 0x00000000;
661 8ecc7913 j_mayer
    dma->sgc = 0x00000000;
662 8ecc7913 j_mayer
    dma->slp = 0x7C000000;
663 8ecc7913 j_mayer
    dma->pol = 0x00000000;
664 8ecc7913 j_mayer
}
665 8ecc7913 j_mayer
666 e2684c0b Andreas Färber
static void ppc405_dma_init(CPUPPCState *env, qemu_irq irqs[4])
667 8ecc7913 j_mayer
{
668 c227f099 Anthony Liguori
    ppc405_dma_t *dma;
669 8ecc7913 j_mayer
670 7267c094 Anthony Liguori
    dma = g_malloc0(sizeof(ppc405_dma_t));
671 487414f1 aliguori
    memcpy(dma->irqs, irqs, 4 * sizeof(qemu_irq));
672 a08d4367 Jan Kiszka
    qemu_register_reset(&ppc405_dma_reset, dma);
673 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CR0,
674 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
675 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CT0,
676 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
677 487414f1 aliguori
    ppc_dcr_register(env, DMA0_DA0,
678 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
679 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SA0,
680 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
681 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SG0,
682 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
683 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CR1,
684 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
685 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CT1,
686 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
687 487414f1 aliguori
    ppc_dcr_register(env, DMA0_DA1,
688 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
689 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SA1,
690 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
691 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SG1,
692 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
693 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CR2,
694 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
695 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CT2,
696 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
697 487414f1 aliguori
    ppc_dcr_register(env, DMA0_DA2,
698 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
699 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SA2,
700 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
701 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SG2,
702 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
703 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CR3,
704 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
705 487414f1 aliguori
    ppc_dcr_register(env, DMA0_CT3,
706 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
707 487414f1 aliguori
    ppc_dcr_register(env, DMA0_DA3,
708 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
709 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SA3,
710 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
711 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SG3,
712 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
713 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SR,
714 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
715 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SGC,
716 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
717 487414f1 aliguori
    ppc_dcr_register(env, DMA0_SLP,
718 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
719 487414f1 aliguori
    ppc_dcr_register(env, DMA0_POL,
720 487414f1 aliguori
                     dma, &dcr_read_dma, &dcr_write_dma);
721 8ecc7913 j_mayer
}
722 8ecc7913 j_mayer
723 8ecc7913 j_mayer
/*****************************************************************************/
724 8ecc7913 j_mayer
/* GPIO */
725 c227f099 Anthony Liguori
typedef struct ppc405_gpio_t ppc405_gpio_t;
726 c227f099 Anthony Liguori
struct ppc405_gpio_t {
727 9074e0e3 Avi Kivity
    MemoryRegion io;
728 8ecc7913 j_mayer
    uint32_t or;
729 8ecc7913 j_mayer
    uint32_t tcr;
730 8ecc7913 j_mayer
    uint32_t osrh;
731 8ecc7913 j_mayer
    uint32_t osrl;
732 8ecc7913 j_mayer
    uint32_t tsrh;
733 8ecc7913 j_mayer
    uint32_t tsrl;
734 8ecc7913 j_mayer
    uint32_t odr;
735 8ecc7913 j_mayer
    uint32_t ir;
736 8ecc7913 j_mayer
    uint32_t rr1;
737 8ecc7913 j_mayer
    uint32_t isr1h;
738 8ecc7913 j_mayer
    uint32_t isr1l;
739 8ecc7913 j_mayer
};
740 8ecc7913 j_mayer
741 a8170e5e Avi Kivity
static uint32_t ppc405_gpio_readb (void *opaque, hwaddr addr)
742 8ecc7913 j_mayer
{
743 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
744 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
745 8ecc7913 j_mayer
#endif
746 8ecc7913 j_mayer
747 8ecc7913 j_mayer
    return 0;
748 8ecc7913 j_mayer
}
749 8ecc7913 j_mayer
750 8ecc7913 j_mayer
static void ppc405_gpio_writeb (void *opaque,
751 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
752 8ecc7913 j_mayer
{
753 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
754 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
755 90e189ec Blue Swirl
           value);
756 8ecc7913 j_mayer
#endif
757 8ecc7913 j_mayer
}
758 8ecc7913 j_mayer
759 a8170e5e Avi Kivity
static uint32_t ppc405_gpio_readw (void *opaque, hwaddr addr)
760 8ecc7913 j_mayer
{
761 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
762 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
763 8ecc7913 j_mayer
#endif
764 8ecc7913 j_mayer
765 8ecc7913 j_mayer
    return 0;
766 8ecc7913 j_mayer
}
767 8ecc7913 j_mayer
768 8ecc7913 j_mayer
static void ppc405_gpio_writew (void *opaque,
769 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
770 8ecc7913 j_mayer
{
771 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
772 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
773 90e189ec Blue Swirl
           value);
774 8ecc7913 j_mayer
#endif
775 8ecc7913 j_mayer
}
776 8ecc7913 j_mayer
777 a8170e5e Avi Kivity
static uint32_t ppc405_gpio_readl (void *opaque, hwaddr addr)
778 8ecc7913 j_mayer
{
779 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
780 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
781 8ecc7913 j_mayer
#endif
782 8ecc7913 j_mayer
783 8ecc7913 j_mayer
    return 0;
784 8ecc7913 j_mayer
}
785 8ecc7913 j_mayer
786 8ecc7913 j_mayer
static void ppc405_gpio_writel (void *opaque,
787 a8170e5e Avi Kivity
                                hwaddr addr, uint32_t value)
788 8ecc7913 j_mayer
{
789 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
790 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
791 90e189ec Blue Swirl
           value);
792 8ecc7913 j_mayer
#endif
793 8ecc7913 j_mayer
}
794 8ecc7913 j_mayer
795 9074e0e3 Avi Kivity
static const MemoryRegionOps ppc405_gpio_ops = {
796 9074e0e3 Avi Kivity
    .old_mmio = {
797 9074e0e3 Avi Kivity
        .read = { ppc405_gpio_readb, ppc405_gpio_readw, ppc405_gpio_readl, },
798 9074e0e3 Avi Kivity
        .write = { ppc405_gpio_writeb, ppc405_gpio_writew, ppc405_gpio_writel, },
799 9074e0e3 Avi Kivity
    },
800 9074e0e3 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
801 8ecc7913 j_mayer
};
802 8ecc7913 j_mayer
803 8ecc7913 j_mayer
static void ppc405_gpio_reset (void *opaque)
804 8ecc7913 j_mayer
{
805 8ecc7913 j_mayer
}
806 8ecc7913 j_mayer
807 a8170e5e Avi Kivity
static void ppc405_gpio_init(hwaddr base)
808 8ecc7913 j_mayer
{
809 c227f099 Anthony Liguori
    ppc405_gpio_t *gpio;
810 8ecc7913 j_mayer
811 7267c094 Anthony Liguori
    gpio = g_malloc0(sizeof(ppc405_gpio_t));
812 8ecc7913 j_mayer
#ifdef DEBUG_GPIO
813 90e189ec Blue Swirl
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
814 8ecc7913 j_mayer
#endif
815 9074e0e3 Avi Kivity
    memory_region_init_io(&gpio->io, &ppc405_gpio_ops, gpio, "pgio", 0x038);
816 9074e0e3 Avi Kivity
    memory_region_add_subregion(get_system_memory(), base, &gpio->io);
817 802670e6 Blue Swirl
    qemu_register_reset(&ppc405_gpio_reset, gpio);
818 8ecc7913 j_mayer
}
819 8ecc7913 j_mayer
820 8ecc7913 j_mayer
/*****************************************************************************/
821 8ecc7913 j_mayer
/* On Chip Memory */
822 8ecc7913 j_mayer
enum {
823 8ecc7913 j_mayer
    OCM0_ISARC   = 0x018,
824 8ecc7913 j_mayer
    OCM0_ISACNTL = 0x019,
825 8ecc7913 j_mayer
    OCM0_DSARC   = 0x01A,
826 8ecc7913 j_mayer
    OCM0_DSACNTL = 0x01B,
827 8ecc7913 j_mayer
};
828 8ecc7913 j_mayer
829 c227f099 Anthony Liguori
typedef struct ppc405_ocm_t ppc405_ocm_t;
830 c227f099 Anthony Liguori
struct ppc405_ocm_t {
831 9074e0e3 Avi Kivity
    MemoryRegion ram;
832 9074e0e3 Avi Kivity
    MemoryRegion isarc_ram;
833 9074e0e3 Avi Kivity
    MemoryRegion dsarc_ram;
834 8ecc7913 j_mayer
    uint32_t isarc;
835 8ecc7913 j_mayer
    uint32_t isacntl;
836 8ecc7913 j_mayer
    uint32_t dsarc;
837 8ecc7913 j_mayer
    uint32_t dsacntl;
838 8ecc7913 j_mayer
};
839 8ecc7913 j_mayer
840 c227f099 Anthony Liguori
static void ocm_update_mappings (ppc405_ocm_t *ocm,
841 8ecc7913 j_mayer
                                 uint32_t isarc, uint32_t isacntl,
842 8ecc7913 j_mayer
                                 uint32_t dsarc, uint32_t dsacntl)
843 8ecc7913 j_mayer
{
844 8ecc7913 j_mayer
#ifdef DEBUG_OCM
845 aae9366a j_mayer
    printf("OCM update ISA %08" PRIx32 " %08" PRIx32 " (%08" PRIx32
846 aae9366a j_mayer
           " %08" PRIx32 ") DSA %08" PRIx32 " %08" PRIx32
847 aae9366a j_mayer
           " (%08" PRIx32 " %08" PRIx32 ")\n",
848 8ecc7913 j_mayer
           isarc, isacntl, dsarc, dsacntl,
849 8ecc7913 j_mayer
           ocm->isarc, ocm->isacntl, ocm->dsarc, ocm->dsacntl);
850 8ecc7913 j_mayer
#endif
851 8ecc7913 j_mayer
    if (ocm->isarc != isarc ||
852 8ecc7913 j_mayer
        (ocm->isacntl & 0x80000000) != (isacntl & 0x80000000)) {
853 8ecc7913 j_mayer
        if (ocm->isacntl & 0x80000000) {
854 8ecc7913 j_mayer
            /* Unmap previously assigned memory region */
855 aae9366a j_mayer
            printf("OCM unmap ISA %08" PRIx32 "\n", ocm->isarc);
856 9074e0e3 Avi Kivity
            memory_region_del_subregion(get_system_memory(), &ocm->isarc_ram);
857 8ecc7913 j_mayer
        }
858 8ecc7913 j_mayer
        if (isacntl & 0x80000000) {
859 8ecc7913 j_mayer
            /* Map new instruction memory region */
860 8ecc7913 j_mayer
#ifdef DEBUG_OCM
861 aae9366a j_mayer
            printf("OCM map ISA %08" PRIx32 "\n", isarc);
862 8ecc7913 j_mayer
#endif
863 9074e0e3 Avi Kivity
            memory_region_add_subregion(get_system_memory(), isarc,
864 9074e0e3 Avi Kivity
                                        &ocm->isarc_ram);
865 8ecc7913 j_mayer
        }
866 8ecc7913 j_mayer
    }
867 8ecc7913 j_mayer
    if (ocm->dsarc != dsarc ||
868 8ecc7913 j_mayer
        (ocm->dsacntl & 0x80000000) != (dsacntl & 0x80000000)) {
869 8ecc7913 j_mayer
        if (ocm->dsacntl & 0x80000000) {
870 8ecc7913 j_mayer
            /* Beware not to unmap the region we just mapped */
871 8ecc7913 j_mayer
            if (!(isacntl & 0x80000000) || ocm->dsarc != isarc) {
872 8ecc7913 j_mayer
                /* Unmap previously assigned memory region */
873 8ecc7913 j_mayer
#ifdef DEBUG_OCM
874 aae9366a j_mayer
                printf("OCM unmap DSA %08" PRIx32 "\n", ocm->dsarc);
875 8ecc7913 j_mayer
#endif
876 9074e0e3 Avi Kivity
                memory_region_del_subregion(get_system_memory(),
877 9074e0e3 Avi Kivity
                                            &ocm->dsarc_ram);
878 8ecc7913 j_mayer
            }
879 8ecc7913 j_mayer
        }
880 8ecc7913 j_mayer
        if (dsacntl & 0x80000000) {
881 8ecc7913 j_mayer
            /* Beware not to remap the region we just mapped */
882 8ecc7913 j_mayer
            if (!(isacntl & 0x80000000) || dsarc != isarc) {
883 8ecc7913 j_mayer
                /* Map new data memory region */
884 8ecc7913 j_mayer
#ifdef DEBUG_OCM
885 aae9366a j_mayer
                printf("OCM map DSA %08" PRIx32 "\n", dsarc);
886 8ecc7913 j_mayer
#endif
887 9074e0e3 Avi Kivity
                memory_region_add_subregion(get_system_memory(), dsarc,
888 9074e0e3 Avi Kivity
                                            &ocm->dsarc_ram);
889 8ecc7913 j_mayer
            }
890 8ecc7913 j_mayer
        }
891 8ecc7913 j_mayer
    }
892 8ecc7913 j_mayer
}
893 8ecc7913 j_mayer
894 73b01960 Alexander Graf
static uint32_t dcr_read_ocm (void *opaque, int dcrn)
895 8ecc7913 j_mayer
{
896 c227f099 Anthony Liguori
    ppc405_ocm_t *ocm;
897 73b01960 Alexander Graf
    uint32_t ret;
898 8ecc7913 j_mayer
899 8ecc7913 j_mayer
    ocm = opaque;
900 8ecc7913 j_mayer
    switch (dcrn) {
901 8ecc7913 j_mayer
    case OCM0_ISARC:
902 8ecc7913 j_mayer
        ret = ocm->isarc;
903 8ecc7913 j_mayer
        break;
904 8ecc7913 j_mayer
    case OCM0_ISACNTL:
905 8ecc7913 j_mayer
        ret = ocm->isacntl;
906 8ecc7913 j_mayer
        break;
907 8ecc7913 j_mayer
    case OCM0_DSARC:
908 8ecc7913 j_mayer
        ret = ocm->dsarc;
909 8ecc7913 j_mayer
        break;
910 8ecc7913 j_mayer
    case OCM0_DSACNTL:
911 8ecc7913 j_mayer
        ret = ocm->dsacntl;
912 8ecc7913 j_mayer
        break;
913 8ecc7913 j_mayer
    default:
914 8ecc7913 j_mayer
        ret = 0;
915 8ecc7913 j_mayer
        break;
916 8ecc7913 j_mayer
    }
917 8ecc7913 j_mayer
918 8ecc7913 j_mayer
    return ret;
919 8ecc7913 j_mayer
}
920 8ecc7913 j_mayer
921 73b01960 Alexander Graf
static void dcr_write_ocm (void *opaque, int dcrn, uint32_t val)
922 8ecc7913 j_mayer
{
923 c227f099 Anthony Liguori
    ppc405_ocm_t *ocm;
924 8ecc7913 j_mayer
    uint32_t isarc, dsarc, isacntl, dsacntl;
925 8ecc7913 j_mayer
926 8ecc7913 j_mayer
    ocm = opaque;
927 8ecc7913 j_mayer
    isarc = ocm->isarc;
928 8ecc7913 j_mayer
    dsarc = ocm->dsarc;
929 8ecc7913 j_mayer
    isacntl = ocm->isacntl;
930 8ecc7913 j_mayer
    dsacntl = ocm->dsacntl;
931 8ecc7913 j_mayer
    switch (dcrn) {
932 8ecc7913 j_mayer
    case OCM0_ISARC:
933 8ecc7913 j_mayer
        isarc = val & 0xFC000000;
934 8ecc7913 j_mayer
        break;
935 8ecc7913 j_mayer
    case OCM0_ISACNTL:
936 8ecc7913 j_mayer
        isacntl = val & 0xC0000000;
937 8ecc7913 j_mayer
        break;
938 8ecc7913 j_mayer
    case OCM0_DSARC:
939 8ecc7913 j_mayer
        isarc = val & 0xFC000000;
940 8ecc7913 j_mayer
        break;
941 8ecc7913 j_mayer
    case OCM0_DSACNTL:
942 8ecc7913 j_mayer
        isacntl = val & 0xC0000000;
943 8ecc7913 j_mayer
        break;
944 8ecc7913 j_mayer
    }
945 8ecc7913 j_mayer
    ocm_update_mappings(ocm, isarc, isacntl, dsarc, dsacntl);
946 8ecc7913 j_mayer
    ocm->isarc = isarc;
947 8ecc7913 j_mayer
    ocm->dsarc = dsarc;
948 8ecc7913 j_mayer
    ocm->isacntl = isacntl;
949 8ecc7913 j_mayer
    ocm->dsacntl = dsacntl;
950 8ecc7913 j_mayer
}
951 8ecc7913 j_mayer
952 8ecc7913 j_mayer
static void ocm_reset (void *opaque)
953 8ecc7913 j_mayer
{
954 c227f099 Anthony Liguori
    ppc405_ocm_t *ocm;
955 8ecc7913 j_mayer
    uint32_t isarc, dsarc, isacntl, dsacntl;
956 8ecc7913 j_mayer
957 8ecc7913 j_mayer
    ocm = opaque;
958 8ecc7913 j_mayer
    isarc = 0x00000000;
959 8ecc7913 j_mayer
    isacntl = 0x00000000;
960 8ecc7913 j_mayer
    dsarc = 0x00000000;
961 8ecc7913 j_mayer
    dsacntl = 0x00000000;
962 8ecc7913 j_mayer
    ocm_update_mappings(ocm, isarc, isacntl, dsarc, dsacntl);
963 8ecc7913 j_mayer
    ocm->isarc = isarc;
964 8ecc7913 j_mayer
    ocm->dsarc = dsarc;
965 8ecc7913 j_mayer
    ocm->isacntl = isacntl;
966 8ecc7913 j_mayer
    ocm->dsacntl = dsacntl;
967 8ecc7913 j_mayer
}
968 8ecc7913 j_mayer
969 e2684c0b Andreas Färber
static void ppc405_ocm_init(CPUPPCState *env)
970 8ecc7913 j_mayer
{
971 c227f099 Anthony Liguori
    ppc405_ocm_t *ocm;
972 8ecc7913 j_mayer
973 7267c094 Anthony Liguori
    ocm = g_malloc0(sizeof(ppc405_ocm_t));
974 9074e0e3 Avi Kivity
    /* XXX: Size is 4096 or 0x04000000 */
975 c5705a77 Avi Kivity
    memory_region_init_ram(&ocm->isarc_ram, "ppc405.ocm", 4096);
976 c5705a77 Avi Kivity
    vmstate_register_ram_global(&ocm->isarc_ram);
977 9074e0e3 Avi Kivity
    memory_region_init_alias(&ocm->dsarc_ram, "ppc405.dsarc", &ocm->isarc_ram,
978 9074e0e3 Avi Kivity
                             0, 4096);
979 a08d4367 Jan Kiszka
    qemu_register_reset(&ocm_reset, ocm);
980 487414f1 aliguori
    ppc_dcr_register(env, OCM0_ISARC,
981 487414f1 aliguori
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
982 487414f1 aliguori
    ppc_dcr_register(env, OCM0_ISACNTL,
983 487414f1 aliguori
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
984 487414f1 aliguori
    ppc_dcr_register(env, OCM0_DSARC,
985 487414f1 aliguori
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
986 487414f1 aliguori
    ppc_dcr_register(env, OCM0_DSACNTL,
987 487414f1 aliguori
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
988 8ecc7913 j_mayer
}
989 8ecc7913 j_mayer
990 8ecc7913 j_mayer
/*****************************************************************************/
991 8ecc7913 j_mayer
/* I2C controller */
992 c227f099 Anthony Liguori
typedef struct ppc4xx_i2c_t ppc4xx_i2c_t;
993 c227f099 Anthony Liguori
struct ppc4xx_i2c_t {
994 9c02f1a2 j_mayer
    qemu_irq irq;
995 9074e0e3 Avi Kivity
    MemoryRegion iomem;
996 8ecc7913 j_mayer
    uint8_t mdata;
997 8ecc7913 j_mayer
    uint8_t lmadr;
998 8ecc7913 j_mayer
    uint8_t hmadr;
999 8ecc7913 j_mayer
    uint8_t cntl;
1000 8ecc7913 j_mayer
    uint8_t mdcntl;
1001 8ecc7913 j_mayer
    uint8_t sts;
1002 8ecc7913 j_mayer
    uint8_t extsts;
1003 8ecc7913 j_mayer
    uint8_t sdata;
1004 8ecc7913 j_mayer
    uint8_t lsadr;
1005 8ecc7913 j_mayer
    uint8_t hsadr;
1006 8ecc7913 j_mayer
    uint8_t clkdiv;
1007 8ecc7913 j_mayer
    uint8_t intrmsk;
1008 8ecc7913 j_mayer
    uint8_t xfrcnt;
1009 8ecc7913 j_mayer
    uint8_t xtcntlss;
1010 8ecc7913 j_mayer
    uint8_t directcntl;
1011 8ecc7913 j_mayer
};
1012 8ecc7913 j_mayer
1013 a8170e5e Avi Kivity
static uint32_t ppc4xx_i2c_readb (void *opaque, hwaddr addr)
1014 8ecc7913 j_mayer
{
1015 c227f099 Anthony Liguori
    ppc4xx_i2c_t *i2c;
1016 8ecc7913 j_mayer
    uint32_t ret;
1017 8ecc7913 j_mayer
1018 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1019 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1020 8ecc7913 j_mayer
#endif
1021 8ecc7913 j_mayer
    i2c = opaque;
1022 802670e6 Blue Swirl
    switch (addr) {
1023 8ecc7913 j_mayer
    case 0x00:
1024 8ecc7913 j_mayer
        //        i2c_readbyte(&i2c->mdata);
1025 8ecc7913 j_mayer
        ret = i2c->mdata;
1026 8ecc7913 j_mayer
        break;
1027 8ecc7913 j_mayer
    case 0x02:
1028 8ecc7913 j_mayer
        ret = i2c->sdata;
1029 8ecc7913 j_mayer
        break;
1030 8ecc7913 j_mayer
    case 0x04:
1031 8ecc7913 j_mayer
        ret = i2c->lmadr;
1032 8ecc7913 j_mayer
        break;
1033 8ecc7913 j_mayer
    case 0x05:
1034 8ecc7913 j_mayer
        ret = i2c->hmadr;
1035 8ecc7913 j_mayer
        break;
1036 8ecc7913 j_mayer
    case 0x06:
1037 8ecc7913 j_mayer
        ret = i2c->cntl;
1038 8ecc7913 j_mayer
        break;
1039 8ecc7913 j_mayer
    case 0x07:
1040 8ecc7913 j_mayer
        ret = i2c->mdcntl;
1041 8ecc7913 j_mayer
        break;
1042 8ecc7913 j_mayer
    case 0x08:
1043 8ecc7913 j_mayer
        ret = i2c->sts;
1044 8ecc7913 j_mayer
        break;
1045 8ecc7913 j_mayer
    case 0x09:
1046 8ecc7913 j_mayer
        ret = i2c->extsts;
1047 8ecc7913 j_mayer
        break;
1048 8ecc7913 j_mayer
    case 0x0A:
1049 8ecc7913 j_mayer
        ret = i2c->lsadr;
1050 8ecc7913 j_mayer
        break;
1051 8ecc7913 j_mayer
    case 0x0B:
1052 8ecc7913 j_mayer
        ret = i2c->hsadr;
1053 8ecc7913 j_mayer
        break;
1054 8ecc7913 j_mayer
    case 0x0C:
1055 8ecc7913 j_mayer
        ret = i2c->clkdiv;
1056 8ecc7913 j_mayer
        break;
1057 8ecc7913 j_mayer
    case 0x0D:
1058 8ecc7913 j_mayer
        ret = i2c->intrmsk;
1059 8ecc7913 j_mayer
        break;
1060 8ecc7913 j_mayer
    case 0x0E:
1061 8ecc7913 j_mayer
        ret = i2c->xfrcnt;
1062 8ecc7913 j_mayer
        break;
1063 8ecc7913 j_mayer
    case 0x0F:
1064 8ecc7913 j_mayer
        ret = i2c->xtcntlss;
1065 8ecc7913 j_mayer
        break;
1066 8ecc7913 j_mayer
    case 0x10:
1067 8ecc7913 j_mayer
        ret = i2c->directcntl;
1068 8ecc7913 j_mayer
        break;
1069 8ecc7913 j_mayer
    default:
1070 8ecc7913 j_mayer
        ret = 0x00;
1071 8ecc7913 j_mayer
        break;
1072 8ecc7913 j_mayer
    }
1073 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1074 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " %02" PRIx32 "\n", __func__, addr, ret);
1075 8ecc7913 j_mayer
#endif
1076 8ecc7913 j_mayer
1077 8ecc7913 j_mayer
    return ret;
1078 8ecc7913 j_mayer
}
1079 8ecc7913 j_mayer
1080 8ecc7913 j_mayer
static void ppc4xx_i2c_writeb (void *opaque,
1081 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1082 8ecc7913 j_mayer
{
1083 c227f099 Anthony Liguori
    ppc4xx_i2c_t *i2c;
1084 8ecc7913 j_mayer
1085 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1086 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1087 90e189ec Blue Swirl
           value);
1088 8ecc7913 j_mayer
#endif
1089 8ecc7913 j_mayer
    i2c = opaque;
1090 802670e6 Blue Swirl
    switch (addr) {
1091 8ecc7913 j_mayer
    case 0x00:
1092 8ecc7913 j_mayer
        i2c->mdata = value;
1093 8ecc7913 j_mayer
        //        i2c_sendbyte(&i2c->mdata);
1094 8ecc7913 j_mayer
        break;
1095 8ecc7913 j_mayer
    case 0x02:
1096 8ecc7913 j_mayer
        i2c->sdata = value;
1097 8ecc7913 j_mayer
        break;
1098 8ecc7913 j_mayer
    case 0x04:
1099 8ecc7913 j_mayer
        i2c->lmadr = value;
1100 8ecc7913 j_mayer
        break;
1101 8ecc7913 j_mayer
    case 0x05:
1102 8ecc7913 j_mayer
        i2c->hmadr = value;
1103 8ecc7913 j_mayer
        break;
1104 8ecc7913 j_mayer
    case 0x06:
1105 8ecc7913 j_mayer
        i2c->cntl = value;
1106 8ecc7913 j_mayer
        break;
1107 8ecc7913 j_mayer
    case 0x07:
1108 8ecc7913 j_mayer
        i2c->mdcntl = value & 0xDF;
1109 8ecc7913 j_mayer
        break;
1110 8ecc7913 j_mayer
    case 0x08:
1111 8ecc7913 j_mayer
        i2c->sts &= ~(value & 0x0A);
1112 8ecc7913 j_mayer
        break;
1113 8ecc7913 j_mayer
    case 0x09:
1114 8ecc7913 j_mayer
        i2c->extsts &= ~(value & 0x8F);
1115 8ecc7913 j_mayer
        break;
1116 8ecc7913 j_mayer
    case 0x0A:
1117 8ecc7913 j_mayer
        i2c->lsadr = value;
1118 8ecc7913 j_mayer
        break;
1119 8ecc7913 j_mayer
    case 0x0B:
1120 8ecc7913 j_mayer
        i2c->hsadr = value;
1121 8ecc7913 j_mayer
        break;
1122 8ecc7913 j_mayer
    case 0x0C:
1123 8ecc7913 j_mayer
        i2c->clkdiv = value;
1124 8ecc7913 j_mayer
        break;
1125 8ecc7913 j_mayer
    case 0x0D:
1126 8ecc7913 j_mayer
        i2c->intrmsk = value;
1127 8ecc7913 j_mayer
        break;
1128 8ecc7913 j_mayer
    case 0x0E:
1129 8ecc7913 j_mayer
        i2c->xfrcnt = value & 0x77;
1130 8ecc7913 j_mayer
        break;
1131 8ecc7913 j_mayer
    case 0x0F:
1132 8ecc7913 j_mayer
        i2c->xtcntlss = value;
1133 8ecc7913 j_mayer
        break;
1134 8ecc7913 j_mayer
    case 0x10:
1135 8ecc7913 j_mayer
        i2c->directcntl = value & 0x7;
1136 8ecc7913 j_mayer
        break;
1137 8ecc7913 j_mayer
    }
1138 8ecc7913 j_mayer
}
1139 8ecc7913 j_mayer
1140 a8170e5e Avi Kivity
static uint32_t ppc4xx_i2c_readw (void *opaque, hwaddr addr)
1141 8ecc7913 j_mayer
{
1142 8ecc7913 j_mayer
    uint32_t ret;
1143 8ecc7913 j_mayer
1144 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1145 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1146 8ecc7913 j_mayer
#endif
1147 8ecc7913 j_mayer
    ret = ppc4xx_i2c_readb(opaque, addr) << 8;
1148 8ecc7913 j_mayer
    ret |= ppc4xx_i2c_readb(opaque, addr + 1);
1149 8ecc7913 j_mayer
1150 8ecc7913 j_mayer
    return ret;
1151 8ecc7913 j_mayer
}
1152 8ecc7913 j_mayer
1153 8ecc7913 j_mayer
static void ppc4xx_i2c_writew (void *opaque,
1154 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1155 8ecc7913 j_mayer
{
1156 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1157 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1158 90e189ec Blue Swirl
           value);
1159 8ecc7913 j_mayer
#endif
1160 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr, value >> 8);
1161 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr + 1, value);
1162 8ecc7913 j_mayer
}
1163 8ecc7913 j_mayer
1164 a8170e5e Avi Kivity
static uint32_t ppc4xx_i2c_readl (void *opaque, hwaddr addr)
1165 8ecc7913 j_mayer
{
1166 8ecc7913 j_mayer
    uint32_t ret;
1167 8ecc7913 j_mayer
1168 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1169 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1170 8ecc7913 j_mayer
#endif
1171 8ecc7913 j_mayer
    ret = ppc4xx_i2c_readb(opaque, addr) << 24;
1172 8ecc7913 j_mayer
    ret |= ppc4xx_i2c_readb(opaque, addr + 1) << 16;
1173 8ecc7913 j_mayer
    ret |= ppc4xx_i2c_readb(opaque, addr + 2) << 8;
1174 8ecc7913 j_mayer
    ret |= ppc4xx_i2c_readb(opaque, addr + 3);
1175 8ecc7913 j_mayer
1176 8ecc7913 j_mayer
    return ret;
1177 8ecc7913 j_mayer
}
1178 8ecc7913 j_mayer
1179 8ecc7913 j_mayer
static void ppc4xx_i2c_writel (void *opaque,
1180 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1181 8ecc7913 j_mayer
{
1182 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1183 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1184 90e189ec Blue Swirl
           value);
1185 8ecc7913 j_mayer
#endif
1186 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr, value >> 24);
1187 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr + 1, value >> 16);
1188 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr + 2, value >> 8);
1189 8ecc7913 j_mayer
    ppc4xx_i2c_writeb(opaque, addr + 3, value);
1190 8ecc7913 j_mayer
}
1191 8ecc7913 j_mayer
1192 9074e0e3 Avi Kivity
static const MemoryRegionOps i2c_ops = {
1193 9074e0e3 Avi Kivity
    .old_mmio = {
1194 9074e0e3 Avi Kivity
        .read = { ppc4xx_i2c_readb, ppc4xx_i2c_readw, ppc4xx_i2c_readl, },
1195 9074e0e3 Avi Kivity
        .write = { ppc4xx_i2c_writeb, ppc4xx_i2c_writew, ppc4xx_i2c_writel, },
1196 9074e0e3 Avi Kivity
    },
1197 9074e0e3 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1198 8ecc7913 j_mayer
};
1199 8ecc7913 j_mayer
1200 8ecc7913 j_mayer
static void ppc4xx_i2c_reset (void *opaque)
1201 8ecc7913 j_mayer
{
1202 c227f099 Anthony Liguori
    ppc4xx_i2c_t *i2c;
1203 8ecc7913 j_mayer
1204 8ecc7913 j_mayer
    i2c = opaque;
1205 8ecc7913 j_mayer
    i2c->mdata = 0x00;
1206 8ecc7913 j_mayer
    i2c->sdata = 0x00;
1207 8ecc7913 j_mayer
    i2c->cntl = 0x00;
1208 8ecc7913 j_mayer
    i2c->mdcntl = 0x00;
1209 8ecc7913 j_mayer
    i2c->sts = 0x00;
1210 8ecc7913 j_mayer
    i2c->extsts = 0x00;
1211 8ecc7913 j_mayer
    i2c->clkdiv = 0x00;
1212 8ecc7913 j_mayer
    i2c->xfrcnt = 0x00;
1213 8ecc7913 j_mayer
    i2c->directcntl = 0x0F;
1214 8ecc7913 j_mayer
}
1215 8ecc7913 j_mayer
1216 a8170e5e Avi Kivity
static void ppc405_i2c_init(hwaddr base, qemu_irq irq)
1217 8ecc7913 j_mayer
{
1218 c227f099 Anthony Liguori
    ppc4xx_i2c_t *i2c;
1219 8ecc7913 j_mayer
1220 7267c094 Anthony Liguori
    i2c = g_malloc0(sizeof(ppc4xx_i2c_t));
1221 487414f1 aliguori
    i2c->irq = irq;
1222 8ecc7913 j_mayer
#ifdef DEBUG_I2C
1223 90e189ec Blue Swirl
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
1224 8ecc7913 j_mayer
#endif
1225 9074e0e3 Avi Kivity
    memory_region_init_io(&i2c->iomem, &i2c_ops, i2c, "i2c", 0x011);
1226 9074e0e3 Avi Kivity
    memory_region_add_subregion(get_system_memory(), base, &i2c->iomem);
1227 a08d4367 Jan Kiszka
    qemu_register_reset(ppc4xx_i2c_reset, i2c);
1228 8ecc7913 j_mayer
}
1229 8ecc7913 j_mayer
1230 8ecc7913 j_mayer
/*****************************************************************************/
1231 9c02f1a2 j_mayer
/* General purpose timers */
1232 c227f099 Anthony Liguori
typedef struct ppc4xx_gpt_t ppc4xx_gpt_t;
1233 c227f099 Anthony Liguori
struct ppc4xx_gpt_t {
1234 9074e0e3 Avi Kivity
    MemoryRegion iomem;
1235 9c02f1a2 j_mayer
    int64_t tb_offset;
1236 9c02f1a2 j_mayer
    uint32_t tb_freq;
1237 9c02f1a2 j_mayer
    struct QEMUTimer *timer;
1238 9c02f1a2 j_mayer
    qemu_irq irqs[5];
1239 9c02f1a2 j_mayer
    uint32_t oe;
1240 9c02f1a2 j_mayer
    uint32_t ol;
1241 9c02f1a2 j_mayer
    uint32_t im;
1242 9c02f1a2 j_mayer
    uint32_t is;
1243 9c02f1a2 j_mayer
    uint32_t ie;
1244 9c02f1a2 j_mayer
    uint32_t comp[5];
1245 9c02f1a2 j_mayer
    uint32_t mask[5];
1246 9c02f1a2 j_mayer
};
1247 9c02f1a2 j_mayer
1248 a8170e5e Avi Kivity
static uint32_t ppc4xx_gpt_readb (void *opaque, hwaddr addr)
1249 9c02f1a2 j_mayer
{
1250 9c02f1a2 j_mayer
#ifdef DEBUG_GPT
1251 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1252 9c02f1a2 j_mayer
#endif
1253 9c02f1a2 j_mayer
    /* XXX: generate a bus fault */
1254 9c02f1a2 j_mayer
    return -1;
1255 9c02f1a2 j_mayer
}
1256 9c02f1a2 j_mayer
1257 9c02f1a2 j_mayer
static void ppc4xx_gpt_writeb (void *opaque,
1258 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1259 9c02f1a2 j_mayer
{
1260 9c02f1a2 j_mayer
#ifdef DEBUG_I2C
1261 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1262 90e189ec Blue Swirl
           value);
1263 9c02f1a2 j_mayer
#endif
1264 9c02f1a2 j_mayer
    /* XXX: generate a bus fault */
1265 9c02f1a2 j_mayer
}
1266 9c02f1a2 j_mayer
1267 a8170e5e Avi Kivity
static uint32_t ppc4xx_gpt_readw (void *opaque, hwaddr addr)
1268 9c02f1a2 j_mayer
{
1269 9c02f1a2 j_mayer
#ifdef DEBUG_GPT
1270 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1271 9c02f1a2 j_mayer
#endif
1272 9c02f1a2 j_mayer
    /* XXX: generate a bus fault */
1273 9c02f1a2 j_mayer
    return -1;
1274 9c02f1a2 j_mayer
}
1275 9c02f1a2 j_mayer
1276 9c02f1a2 j_mayer
static void ppc4xx_gpt_writew (void *opaque,
1277 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1278 9c02f1a2 j_mayer
{
1279 9c02f1a2 j_mayer
#ifdef DEBUG_I2C
1280 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1281 90e189ec Blue Swirl
           value);
1282 9c02f1a2 j_mayer
#endif
1283 9c02f1a2 j_mayer
    /* XXX: generate a bus fault */
1284 9c02f1a2 j_mayer
}
1285 9c02f1a2 j_mayer
1286 c227f099 Anthony Liguori
static int ppc4xx_gpt_compare (ppc4xx_gpt_t *gpt, int n)
1287 9c02f1a2 j_mayer
{
1288 9c02f1a2 j_mayer
    /* XXX: TODO */
1289 9c02f1a2 j_mayer
    return 0;
1290 9c02f1a2 j_mayer
}
1291 9c02f1a2 j_mayer
1292 c227f099 Anthony Liguori
static void ppc4xx_gpt_set_output (ppc4xx_gpt_t *gpt, int n, int level)
1293 9c02f1a2 j_mayer
{
1294 9c02f1a2 j_mayer
    /* XXX: TODO */
1295 9c02f1a2 j_mayer
}
1296 9c02f1a2 j_mayer
1297 c227f099 Anthony Liguori
static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t *gpt)
1298 9c02f1a2 j_mayer
{
1299 9c02f1a2 j_mayer
    uint32_t mask;
1300 9c02f1a2 j_mayer
    int i;
1301 9c02f1a2 j_mayer
1302 9c02f1a2 j_mayer
    mask = 0x80000000;
1303 9c02f1a2 j_mayer
    for (i = 0; i < 5; i++) {
1304 9c02f1a2 j_mayer
        if (gpt->oe & mask) {
1305 9c02f1a2 j_mayer
            /* Output is enabled */
1306 9c02f1a2 j_mayer
            if (ppc4xx_gpt_compare(gpt, i)) {
1307 9c02f1a2 j_mayer
                /* Comparison is OK */
1308 9c02f1a2 j_mayer
                ppc4xx_gpt_set_output(gpt, i, gpt->ol & mask);
1309 9c02f1a2 j_mayer
            } else {
1310 9c02f1a2 j_mayer
                /* Comparison is KO */
1311 9c02f1a2 j_mayer
                ppc4xx_gpt_set_output(gpt, i, gpt->ol & mask ? 0 : 1);
1312 9c02f1a2 j_mayer
            }
1313 9c02f1a2 j_mayer
        }
1314 9c02f1a2 j_mayer
        mask = mask >> 1;
1315 9c02f1a2 j_mayer
    }
1316 9c02f1a2 j_mayer
}
1317 9c02f1a2 j_mayer
1318 c227f099 Anthony Liguori
static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t *gpt)
1319 9c02f1a2 j_mayer
{
1320 9c02f1a2 j_mayer
    uint32_t mask;
1321 9c02f1a2 j_mayer
    int i;
1322 9c02f1a2 j_mayer
1323 9c02f1a2 j_mayer
    mask = 0x00008000;
1324 9c02f1a2 j_mayer
    for (i = 0; i < 5; i++) {
1325 9c02f1a2 j_mayer
        if (gpt->is & gpt->im & mask)
1326 9c02f1a2 j_mayer
            qemu_irq_raise(gpt->irqs[i]);
1327 9c02f1a2 j_mayer
        else
1328 9c02f1a2 j_mayer
            qemu_irq_lower(gpt->irqs[i]);
1329 9c02f1a2 j_mayer
        mask = mask >> 1;
1330 9c02f1a2 j_mayer
    }
1331 9c02f1a2 j_mayer
}
1332 9c02f1a2 j_mayer
1333 c227f099 Anthony Liguori
static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t *gpt)
1334 9c02f1a2 j_mayer
{
1335 9c02f1a2 j_mayer
    /* XXX: TODO */
1336 9c02f1a2 j_mayer
}
1337 9c02f1a2 j_mayer
1338 a8170e5e Avi Kivity
static uint32_t ppc4xx_gpt_readl (void *opaque, hwaddr addr)
1339 9c02f1a2 j_mayer
{
1340 c227f099 Anthony Liguori
    ppc4xx_gpt_t *gpt;
1341 9c02f1a2 j_mayer
    uint32_t ret;
1342 9c02f1a2 j_mayer
    int idx;
1343 9c02f1a2 j_mayer
1344 9c02f1a2 j_mayer
#ifdef DEBUG_GPT
1345 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1346 9c02f1a2 j_mayer
#endif
1347 9c02f1a2 j_mayer
    gpt = opaque;
1348 802670e6 Blue Swirl
    switch (addr) {
1349 9c02f1a2 j_mayer
    case 0x00:
1350 9c02f1a2 j_mayer
        /* Time base counter */
1351 74475455 Paolo Bonzini
        ret = muldiv64(qemu_get_clock_ns(vm_clock) + gpt->tb_offset,
1352 6ee093c9 Juan Quintela
                       gpt->tb_freq, get_ticks_per_sec());
1353 9c02f1a2 j_mayer
        break;
1354 9c02f1a2 j_mayer
    case 0x10:
1355 9c02f1a2 j_mayer
        /* Output enable */
1356 9c02f1a2 j_mayer
        ret = gpt->oe;
1357 9c02f1a2 j_mayer
        break;
1358 9c02f1a2 j_mayer
    case 0x14:
1359 9c02f1a2 j_mayer
        /* Output level */
1360 9c02f1a2 j_mayer
        ret = gpt->ol;
1361 9c02f1a2 j_mayer
        break;
1362 9c02f1a2 j_mayer
    case 0x18:
1363 9c02f1a2 j_mayer
        /* Interrupt mask */
1364 9c02f1a2 j_mayer
        ret = gpt->im;
1365 9c02f1a2 j_mayer
        break;
1366 9c02f1a2 j_mayer
    case 0x1C:
1367 9c02f1a2 j_mayer
    case 0x20:
1368 9c02f1a2 j_mayer
        /* Interrupt status */
1369 9c02f1a2 j_mayer
        ret = gpt->is;
1370 9c02f1a2 j_mayer
        break;
1371 9c02f1a2 j_mayer
    case 0x24:
1372 9c02f1a2 j_mayer
        /* Interrupt enable */
1373 9c02f1a2 j_mayer
        ret = gpt->ie;
1374 9c02f1a2 j_mayer
        break;
1375 9c02f1a2 j_mayer
    case 0x80 ... 0x90:
1376 9c02f1a2 j_mayer
        /* Compare timer */
1377 802670e6 Blue Swirl
        idx = (addr - 0x80) >> 2;
1378 9c02f1a2 j_mayer
        ret = gpt->comp[idx];
1379 9c02f1a2 j_mayer
        break;
1380 9c02f1a2 j_mayer
    case 0xC0 ... 0xD0:
1381 9c02f1a2 j_mayer
        /* Compare mask */
1382 802670e6 Blue Swirl
        idx = (addr - 0xC0) >> 2;
1383 9c02f1a2 j_mayer
        ret = gpt->mask[idx];
1384 9c02f1a2 j_mayer
        break;
1385 9c02f1a2 j_mayer
    default:
1386 9c02f1a2 j_mayer
        ret = -1;
1387 9c02f1a2 j_mayer
        break;
1388 9c02f1a2 j_mayer
    }
1389 9c02f1a2 j_mayer
1390 9c02f1a2 j_mayer
    return ret;
1391 9c02f1a2 j_mayer
}
1392 9c02f1a2 j_mayer
1393 9c02f1a2 j_mayer
static void ppc4xx_gpt_writel (void *opaque,
1394 a8170e5e Avi Kivity
                               hwaddr addr, uint32_t value)
1395 9c02f1a2 j_mayer
{
1396 c227f099 Anthony Liguori
    ppc4xx_gpt_t *gpt;
1397 9c02f1a2 j_mayer
    int idx;
1398 9c02f1a2 j_mayer
1399 9c02f1a2 j_mayer
#ifdef DEBUG_I2C
1400 90e189ec Blue Swirl
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
1401 90e189ec Blue Swirl
           value);
1402 9c02f1a2 j_mayer
#endif
1403 9c02f1a2 j_mayer
    gpt = opaque;
1404 802670e6 Blue Swirl
    switch (addr) {
1405 9c02f1a2 j_mayer
    case 0x00:
1406 9c02f1a2 j_mayer
        /* Time base counter */
1407 6ee093c9 Juan Quintela
        gpt->tb_offset = muldiv64(value, get_ticks_per_sec(), gpt->tb_freq)
1408 74475455 Paolo Bonzini
            - qemu_get_clock_ns(vm_clock);
1409 9c02f1a2 j_mayer
        ppc4xx_gpt_compute_timer(gpt);
1410 9c02f1a2 j_mayer
        break;
1411 9c02f1a2 j_mayer
    case 0x10:
1412 9c02f1a2 j_mayer
        /* Output enable */
1413 9c02f1a2 j_mayer
        gpt->oe = value & 0xF8000000;
1414 9c02f1a2 j_mayer
        ppc4xx_gpt_set_outputs(gpt);
1415 9c02f1a2 j_mayer
        break;
1416 9c02f1a2 j_mayer
    case 0x14:
1417 9c02f1a2 j_mayer
        /* Output level */
1418 9c02f1a2 j_mayer
        gpt->ol = value & 0xF8000000;
1419 9c02f1a2 j_mayer
        ppc4xx_gpt_set_outputs(gpt);
1420 9c02f1a2 j_mayer
        break;
1421 9c02f1a2 j_mayer
    case 0x18:
1422 9c02f1a2 j_mayer
        /* Interrupt mask */
1423 9c02f1a2 j_mayer
        gpt->im = value & 0x0000F800;
1424 9c02f1a2 j_mayer
        break;
1425 9c02f1a2 j_mayer
    case 0x1C:
1426 9c02f1a2 j_mayer
        /* Interrupt status set */
1427 9c02f1a2 j_mayer
        gpt->is |= value & 0x0000F800;
1428 9c02f1a2 j_mayer
        ppc4xx_gpt_set_irqs(gpt);
1429 9c02f1a2 j_mayer
        break;
1430 9c02f1a2 j_mayer
    case 0x20:
1431 9c02f1a2 j_mayer
        /* Interrupt status clear */
1432 9c02f1a2 j_mayer
        gpt->is &= ~(value & 0x0000F800);
1433 9c02f1a2 j_mayer
        ppc4xx_gpt_set_irqs(gpt);
1434 9c02f1a2 j_mayer
        break;
1435 9c02f1a2 j_mayer
    case 0x24:
1436 9c02f1a2 j_mayer
        /* Interrupt enable */
1437 9c02f1a2 j_mayer
        gpt->ie = value & 0x0000F800;
1438 9c02f1a2 j_mayer
        ppc4xx_gpt_set_irqs(gpt);
1439 9c02f1a2 j_mayer
        break;
1440 9c02f1a2 j_mayer
    case 0x80 ... 0x90:
1441 9c02f1a2 j_mayer
        /* Compare timer */
1442 802670e6 Blue Swirl
        idx = (addr - 0x80) >> 2;
1443 9c02f1a2 j_mayer
        gpt->comp[idx] = value & 0xF8000000;
1444 9c02f1a2 j_mayer
        ppc4xx_gpt_compute_timer(gpt);
1445 9c02f1a2 j_mayer
        break;
1446 9c02f1a2 j_mayer
    case 0xC0 ... 0xD0:
1447 9c02f1a2 j_mayer
        /* Compare mask */
1448 802670e6 Blue Swirl
        idx = (addr - 0xC0) >> 2;
1449 9c02f1a2 j_mayer
        gpt->mask[idx] = value & 0xF8000000;
1450 9c02f1a2 j_mayer
        ppc4xx_gpt_compute_timer(gpt);
1451 9c02f1a2 j_mayer
        break;
1452 9c02f1a2 j_mayer
    }
1453 9c02f1a2 j_mayer
}
1454 9c02f1a2 j_mayer
1455 9074e0e3 Avi Kivity
static const MemoryRegionOps gpt_ops = {
1456 9074e0e3 Avi Kivity
    .old_mmio = {
1457 9074e0e3 Avi Kivity
        .read = { ppc4xx_gpt_readb, ppc4xx_gpt_readw, ppc4xx_gpt_readl, },
1458 9074e0e3 Avi Kivity
        .write = { ppc4xx_gpt_writeb, ppc4xx_gpt_writew, ppc4xx_gpt_writel, },
1459 9074e0e3 Avi Kivity
    },
1460 9074e0e3 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1461 9c02f1a2 j_mayer
};
1462 9c02f1a2 j_mayer
1463 9c02f1a2 j_mayer
static void ppc4xx_gpt_cb (void *opaque)
1464 9c02f1a2 j_mayer
{
1465 c227f099 Anthony Liguori
    ppc4xx_gpt_t *gpt;
1466 9c02f1a2 j_mayer
1467 9c02f1a2 j_mayer
    gpt = opaque;
1468 9c02f1a2 j_mayer
    ppc4xx_gpt_set_irqs(gpt);
1469 9c02f1a2 j_mayer
    ppc4xx_gpt_set_outputs(gpt);
1470 9c02f1a2 j_mayer
    ppc4xx_gpt_compute_timer(gpt);
1471 9c02f1a2 j_mayer
}
1472 9c02f1a2 j_mayer
1473 9c02f1a2 j_mayer
static void ppc4xx_gpt_reset (void *opaque)
1474 9c02f1a2 j_mayer
{
1475 c227f099 Anthony Liguori
    ppc4xx_gpt_t *gpt;
1476 9c02f1a2 j_mayer
    int i;
1477 9c02f1a2 j_mayer
1478 9c02f1a2 j_mayer
    gpt = opaque;
1479 9c02f1a2 j_mayer
    qemu_del_timer(gpt->timer);
1480 9c02f1a2 j_mayer
    gpt->oe = 0x00000000;
1481 9c02f1a2 j_mayer
    gpt->ol = 0x00000000;
1482 9c02f1a2 j_mayer
    gpt->im = 0x00000000;
1483 9c02f1a2 j_mayer
    gpt->is = 0x00000000;
1484 9c02f1a2 j_mayer
    gpt->ie = 0x00000000;
1485 9c02f1a2 j_mayer
    for (i = 0; i < 5; i++) {
1486 9c02f1a2 j_mayer
        gpt->comp[i] = 0x00000000;
1487 9c02f1a2 j_mayer
        gpt->mask[i] = 0x00000000;
1488 9c02f1a2 j_mayer
    }
1489 9c02f1a2 j_mayer
}
1490 9c02f1a2 j_mayer
1491 a8170e5e Avi Kivity
static void ppc4xx_gpt_init(hwaddr base, qemu_irq irqs[5])
1492 9c02f1a2 j_mayer
{
1493 c227f099 Anthony Liguori
    ppc4xx_gpt_t *gpt;
1494 9c02f1a2 j_mayer
    int i;
1495 9c02f1a2 j_mayer
1496 7267c094 Anthony Liguori
    gpt = g_malloc0(sizeof(ppc4xx_gpt_t));
1497 802670e6 Blue Swirl
    for (i = 0; i < 5; i++) {
1498 487414f1 aliguori
        gpt->irqs[i] = irqs[i];
1499 802670e6 Blue Swirl
    }
1500 74475455 Paolo Bonzini
    gpt->timer = qemu_new_timer_ns(vm_clock, &ppc4xx_gpt_cb, gpt);
1501 9c02f1a2 j_mayer
#ifdef DEBUG_GPT
1502 90e189ec Blue Swirl
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
1503 9c02f1a2 j_mayer
#endif
1504 9074e0e3 Avi Kivity
    memory_region_init_io(&gpt->iomem, &gpt_ops, gpt, "gpt", 0x0d4);
1505 9074e0e3 Avi Kivity
    memory_region_add_subregion(get_system_memory(), base, &gpt->iomem);
1506 a08d4367 Jan Kiszka
    qemu_register_reset(ppc4xx_gpt_reset, gpt);
1507 9c02f1a2 j_mayer
}
1508 9c02f1a2 j_mayer
1509 9c02f1a2 j_mayer
/*****************************************************************************/
1510 9c02f1a2 j_mayer
/* MAL */
1511 9c02f1a2 j_mayer
enum {
1512 9c02f1a2 j_mayer
    MAL0_CFG      = 0x180,
1513 9c02f1a2 j_mayer
    MAL0_ESR      = 0x181,
1514 9c02f1a2 j_mayer
    MAL0_IER      = 0x182,
1515 9c02f1a2 j_mayer
    MAL0_TXCASR   = 0x184,
1516 9c02f1a2 j_mayer
    MAL0_TXCARR   = 0x185,
1517 9c02f1a2 j_mayer
    MAL0_TXEOBISR = 0x186,
1518 9c02f1a2 j_mayer
    MAL0_TXDEIR   = 0x187,
1519 9c02f1a2 j_mayer
    MAL0_RXCASR   = 0x190,
1520 9c02f1a2 j_mayer
    MAL0_RXCARR   = 0x191,
1521 9c02f1a2 j_mayer
    MAL0_RXEOBISR = 0x192,
1522 9c02f1a2 j_mayer
    MAL0_RXDEIR   = 0x193,
1523 9c02f1a2 j_mayer
    MAL0_TXCTP0R  = 0x1A0,
1524 9c02f1a2 j_mayer
    MAL0_TXCTP1R  = 0x1A1,
1525 9c02f1a2 j_mayer
    MAL0_TXCTP2R  = 0x1A2,
1526 9c02f1a2 j_mayer
    MAL0_TXCTP3R  = 0x1A3,
1527 9c02f1a2 j_mayer
    MAL0_RXCTP0R  = 0x1C0,
1528 9c02f1a2 j_mayer
    MAL0_RXCTP1R  = 0x1C1,
1529 9c02f1a2 j_mayer
    MAL0_RCBS0    = 0x1E0,
1530 9c02f1a2 j_mayer
    MAL0_RCBS1    = 0x1E1,
1531 9c02f1a2 j_mayer
};
1532 9c02f1a2 j_mayer
1533 c227f099 Anthony Liguori
typedef struct ppc40x_mal_t ppc40x_mal_t;
1534 c227f099 Anthony Liguori
struct ppc40x_mal_t {
1535 9c02f1a2 j_mayer
    qemu_irq irqs[4];
1536 9c02f1a2 j_mayer
    uint32_t cfg;
1537 9c02f1a2 j_mayer
    uint32_t esr;
1538 9c02f1a2 j_mayer
    uint32_t ier;
1539 9c02f1a2 j_mayer
    uint32_t txcasr;
1540 9c02f1a2 j_mayer
    uint32_t txcarr;
1541 9c02f1a2 j_mayer
    uint32_t txeobisr;
1542 9c02f1a2 j_mayer
    uint32_t txdeir;
1543 9c02f1a2 j_mayer
    uint32_t rxcasr;
1544 9c02f1a2 j_mayer
    uint32_t rxcarr;
1545 9c02f1a2 j_mayer
    uint32_t rxeobisr;
1546 9c02f1a2 j_mayer
    uint32_t rxdeir;
1547 9c02f1a2 j_mayer
    uint32_t txctpr[4];
1548 9c02f1a2 j_mayer
    uint32_t rxctpr[2];
1549 9c02f1a2 j_mayer
    uint32_t rcbs[2];
1550 9c02f1a2 j_mayer
};
1551 9c02f1a2 j_mayer
1552 9c02f1a2 j_mayer
static void ppc40x_mal_reset (void *opaque);
1553 9c02f1a2 j_mayer
1554 73b01960 Alexander Graf
static uint32_t dcr_read_mal (void *opaque, int dcrn)
1555 9c02f1a2 j_mayer
{
1556 c227f099 Anthony Liguori
    ppc40x_mal_t *mal;
1557 73b01960 Alexander Graf
    uint32_t ret;
1558 9c02f1a2 j_mayer
1559 9c02f1a2 j_mayer
    mal = opaque;
1560 9c02f1a2 j_mayer
    switch (dcrn) {
1561 9c02f1a2 j_mayer
    case MAL0_CFG:
1562 9c02f1a2 j_mayer
        ret = mal->cfg;
1563 9c02f1a2 j_mayer
        break;
1564 9c02f1a2 j_mayer
    case MAL0_ESR:
1565 9c02f1a2 j_mayer
        ret = mal->esr;
1566 9c02f1a2 j_mayer
        break;
1567 9c02f1a2 j_mayer
    case MAL0_IER:
1568 9c02f1a2 j_mayer
        ret = mal->ier;
1569 9c02f1a2 j_mayer
        break;
1570 9c02f1a2 j_mayer
    case MAL0_TXCASR:
1571 9c02f1a2 j_mayer
        ret = mal->txcasr;
1572 9c02f1a2 j_mayer
        break;
1573 9c02f1a2 j_mayer
    case MAL0_TXCARR:
1574 9c02f1a2 j_mayer
        ret = mal->txcarr;
1575 9c02f1a2 j_mayer
        break;
1576 9c02f1a2 j_mayer
    case MAL0_TXEOBISR:
1577 9c02f1a2 j_mayer
        ret = mal->txeobisr;
1578 9c02f1a2 j_mayer
        break;
1579 9c02f1a2 j_mayer
    case MAL0_TXDEIR:
1580 9c02f1a2 j_mayer
        ret = mal->txdeir;
1581 9c02f1a2 j_mayer
        break;
1582 9c02f1a2 j_mayer
    case MAL0_RXCASR:
1583 9c02f1a2 j_mayer
        ret = mal->rxcasr;
1584 9c02f1a2 j_mayer
        break;
1585 9c02f1a2 j_mayer
    case MAL0_RXCARR:
1586 9c02f1a2 j_mayer
        ret = mal->rxcarr;
1587 9c02f1a2 j_mayer
        break;
1588 9c02f1a2 j_mayer
    case MAL0_RXEOBISR:
1589 9c02f1a2 j_mayer
        ret = mal->rxeobisr;
1590 9c02f1a2 j_mayer
        break;
1591 9c02f1a2 j_mayer
    case MAL0_RXDEIR:
1592 9c02f1a2 j_mayer
        ret = mal->rxdeir;
1593 9c02f1a2 j_mayer
        break;
1594 9c02f1a2 j_mayer
    case MAL0_TXCTP0R:
1595 9c02f1a2 j_mayer
        ret = mal->txctpr[0];
1596 9c02f1a2 j_mayer
        break;
1597 9c02f1a2 j_mayer
    case MAL0_TXCTP1R:
1598 9c02f1a2 j_mayer
        ret = mal->txctpr[1];
1599 9c02f1a2 j_mayer
        break;
1600 9c02f1a2 j_mayer
    case MAL0_TXCTP2R:
1601 9c02f1a2 j_mayer
        ret = mal->txctpr[2];
1602 9c02f1a2 j_mayer
        break;
1603 9c02f1a2 j_mayer
    case MAL0_TXCTP3R:
1604 9c02f1a2 j_mayer
        ret = mal->txctpr[3];
1605 9c02f1a2 j_mayer
        break;
1606 9c02f1a2 j_mayer
    case MAL0_RXCTP0R:
1607 9c02f1a2 j_mayer
        ret = mal->rxctpr[0];
1608 9c02f1a2 j_mayer
        break;
1609 9c02f1a2 j_mayer
    case MAL0_RXCTP1R:
1610 9c02f1a2 j_mayer
        ret = mal->rxctpr[1];
1611 9c02f1a2 j_mayer
        break;
1612 9c02f1a2 j_mayer
    case MAL0_RCBS0:
1613 9c02f1a2 j_mayer
        ret = mal->rcbs[0];
1614 9c02f1a2 j_mayer
        break;
1615 9c02f1a2 j_mayer
    case MAL0_RCBS1:
1616 9c02f1a2 j_mayer
        ret = mal->rcbs[1];
1617 9c02f1a2 j_mayer
        break;
1618 9c02f1a2 j_mayer
    default:
1619 9c02f1a2 j_mayer
        ret = 0;
1620 9c02f1a2 j_mayer
        break;
1621 9c02f1a2 j_mayer
    }
1622 9c02f1a2 j_mayer
1623 9c02f1a2 j_mayer
    return ret;
1624 9c02f1a2 j_mayer
}
1625 9c02f1a2 j_mayer
1626 73b01960 Alexander Graf
static void dcr_write_mal (void *opaque, int dcrn, uint32_t val)
1627 9c02f1a2 j_mayer
{
1628 c227f099 Anthony Liguori
    ppc40x_mal_t *mal;
1629 9c02f1a2 j_mayer
    int idx;
1630 9c02f1a2 j_mayer
1631 9c02f1a2 j_mayer
    mal = opaque;
1632 9c02f1a2 j_mayer
    switch (dcrn) {
1633 9c02f1a2 j_mayer
    case MAL0_CFG:
1634 9c02f1a2 j_mayer
        if (val & 0x80000000)
1635 9c02f1a2 j_mayer
            ppc40x_mal_reset(mal);
1636 9c02f1a2 j_mayer
        mal->cfg = val & 0x00FFC087;
1637 9c02f1a2 j_mayer
        break;
1638 9c02f1a2 j_mayer
    case MAL0_ESR:
1639 9c02f1a2 j_mayer
        /* Read/clear */
1640 9c02f1a2 j_mayer
        mal->esr &= ~val;
1641 9c02f1a2 j_mayer
        break;
1642 9c02f1a2 j_mayer
    case MAL0_IER:
1643 9c02f1a2 j_mayer
        mal->ier = val & 0x0000001F;
1644 9c02f1a2 j_mayer
        break;
1645 9c02f1a2 j_mayer
    case MAL0_TXCASR:
1646 9c02f1a2 j_mayer
        mal->txcasr = val & 0xF0000000;
1647 9c02f1a2 j_mayer
        break;
1648 9c02f1a2 j_mayer
    case MAL0_TXCARR:
1649 9c02f1a2 j_mayer
        mal->txcarr = val & 0xF0000000;
1650 9c02f1a2 j_mayer
        break;
1651 9c02f1a2 j_mayer
    case MAL0_TXEOBISR:
1652 9c02f1a2 j_mayer
        /* Read/clear */
1653 9c02f1a2 j_mayer
        mal->txeobisr &= ~val;
1654 9c02f1a2 j_mayer
        break;
1655 9c02f1a2 j_mayer
    case MAL0_TXDEIR:
1656 9c02f1a2 j_mayer
        /* Read/clear */
1657 9c02f1a2 j_mayer
        mal->txdeir &= ~val;
1658 9c02f1a2 j_mayer
        break;
1659 9c02f1a2 j_mayer
    case MAL0_RXCASR:
1660 9c02f1a2 j_mayer
        mal->rxcasr = val & 0xC0000000;
1661 9c02f1a2 j_mayer
        break;
1662 9c02f1a2 j_mayer
    case MAL0_RXCARR:
1663 9c02f1a2 j_mayer
        mal->rxcarr = val & 0xC0000000;
1664 9c02f1a2 j_mayer
        break;
1665 9c02f1a2 j_mayer
    case MAL0_RXEOBISR:
1666 9c02f1a2 j_mayer
        /* Read/clear */
1667 9c02f1a2 j_mayer
        mal->rxeobisr &= ~val;
1668 9c02f1a2 j_mayer
        break;
1669 9c02f1a2 j_mayer
    case MAL0_RXDEIR:
1670 9c02f1a2 j_mayer
        /* Read/clear */
1671 9c02f1a2 j_mayer
        mal->rxdeir &= ~val;
1672 9c02f1a2 j_mayer
        break;
1673 9c02f1a2 j_mayer
    case MAL0_TXCTP0R:
1674 9c02f1a2 j_mayer
        idx = 0;
1675 9c02f1a2 j_mayer
        goto update_tx_ptr;
1676 9c02f1a2 j_mayer
    case MAL0_TXCTP1R:
1677 9c02f1a2 j_mayer
        idx = 1;
1678 9c02f1a2 j_mayer
        goto update_tx_ptr;
1679 9c02f1a2 j_mayer
    case MAL0_TXCTP2R:
1680 9c02f1a2 j_mayer
        idx = 2;
1681 9c02f1a2 j_mayer
        goto update_tx_ptr;
1682 9c02f1a2 j_mayer
    case MAL0_TXCTP3R:
1683 9c02f1a2 j_mayer
        idx = 3;
1684 9c02f1a2 j_mayer
    update_tx_ptr:
1685 9c02f1a2 j_mayer
        mal->txctpr[idx] = val;
1686 9c02f1a2 j_mayer
        break;
1687 9c02f1a2 j_mayer
    case MAL0_RXCTP0R:
1688 9c02f1a2 j_mayer
        idx = 0;
1689 9c02f1a2 j_mayer
        goto update_rx_ptr;
1690 9c02f1a2 j_mayer
    case MAL0_RXCTP1R:
1691 9c02f1a2 j_mayer
        idx = 1;
1692 9c02f1a2 j_mayer
    update_rx_ptr:
1693 9c02f1a2 j_mayer
        mal->rxctpr[idx] = val;
1694 9c02f1a2 j_mayer
        break;
1695 9c02f1a2 j_mayer
    case MAL0_RCBS0:
1696 9c02f1a2 j_mayer
        idx = 0;
1697 9c02f1a2 j_mayer
        goto update_rx_size;
1698 9c02f1a2 j_mayer
    case MAL0_RCBS1:
1699 9c02f1a2 j_mayer
        idx = 1;
1700 9c02f1a2 j_mayer
    update_rx_size:
1701 9c02f1a2 j_mayer
        mal->rcbs[idx] = val & 0x000000FF;
1702 9c02f1a2 j_mayer
        break;
1703 9c02f1a2 j_mayer
    }
1704 9c02f1a2 j_mayer
}
1705 9c02f1a2 j_mayer
1706 9c02f1a2 j_mayer
static void ppc40x_mal_reset (void *opaque)
1707 9c02f1a2 j_mayer
{
1708 c227f099 Anthony Liguori
    ppc40x_mal_t *mal;
1709 9c02f1a2 j_mayer
1710 9c02f1a2 j_mayer
    mal = opaque;
1711 9c02f1a2 j_mayer
    mal->cfg = 0x0007C000;
1712 9c02f1a2 j_mayer
    mal->esr = 0x00000000;
1713 9c02f1a2 j_mayer
    mal->ier = 0x00000000;
1714 9c02f1a2 j_mayer
    mal->rxcasr = 0x00000000;
1715 9c02f1a2 j_mayer
    mal->rxdeir = 0x00000000;
1716 9c02f1a2 j_mayer
    mal->rxeobisr = 0x00000000;
1717 9c02f1a2 j_mayer
    mal->txcasr = 0x00000000;
1718 9c02f1a2 j_mayer
    mal->txdeir = 0x00000000;
1719 9c02f1a2 j_mayer
    mal->txeobisr = 0x00000000;
1720 9c02f1a2 j_mayer
}
1721 9c02f1a2 j_mayer
1722 e2684c0b Andreas Färber
static void ppc405_mal_init(CPUPPCState *env, qemu_irq irqs[4])
1723 9c02f1a2 j_mayer
{
1724 c227f099 Anthony Liguori
    ppc40x_mal_t *mal;
1725 9c02f1a2 j_mayer
    int i;
1726 9c02f1a2 j_mayer
1727 7267c094 Anthony Liguori
    mal = g_malloc0(sizeof(ppc40x_mal_t));
1728 487414f1 aliguori
    for (i = 0; i < 4; i++)
1729 487414f1 aliguori
        mal->irqs[i] = irqs[i];
1730 a08d4367 Jan Kiszka
    qemu_register_reset(&ppc40x_mal_reset, mal);
1731 487414f1 aliguori
    ppc_dcr_register(env, MAL0_CFG,
1732 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1733 487414f1 aliguori
    ppc_dcr_register(env, MAL0_ESR,
1734 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1735 487414f1 aliguori
    ppc_dcr_register(env, MAL0_IER,
1736 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1737 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCASR,
1738 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1739 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCARR,
1740 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1741 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXEOBISR,
1742 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1743 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXDEIR,
1744 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1745 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXCASR,
1746 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1747 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXCARR,
1748 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1749 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXEOBISR,
1750 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1751 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXDEIR,
1752 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1753 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCTP0R,
1754 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1755 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCTP1R,
1756 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1757 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCTP2R,
1758 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1759 487414f1 aliguori
    ppc_dcr_register(env, MAL0_TXCTP3R,
1760 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1761 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXCTP0R,
1762 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1763 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RXCTP1R,
1764 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1765 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RCBS0,
1766 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1767 487414f1 aliguori
    ppc_dcr_register(env, MAL0_RCBS1,
1768 487414f1 aliguori
                     mal, &dcr_read_mal, &dcr_write_mal);
1769 9c02f1a2 j_mayer
}
1770 9c02f1a2 j_mayer
1771 9c02f1a2 j_mayer
/*****************************************************************************/
1772 8ecc7913 j_mayer
/* SPR */
1773 e2684c0b Andreas Färber
void ppc40x_core_reset (CPUPPCState *env)
1774 8ecc7913 j_mayer
{
1775 8ecc7913 j_mayer
    target_ulong dbsr;
1776 8ecc7913 j_mayer
1777 8ecc7913 j_mayer
    printf("Reset PowerPC core\n");
1778 fc0b2c0f Alexander Graf
    cpu_interrupt(env, CPU_INTERRUPT_RESET);
1779 8ecc7913 j_mayer
    dbsr = env->spr[SPR_40x_DBSR];
1780 8ecc7913 j_mayer
    dbsr &= ~0x00000300;
1781 8ecc7913 j_mayer
    dbsr |= 0x00000100;
1782 8ecc7913 j_mayer
    env->spr[SPR_40x_DBSR] = dbsr;
1783 8ecc7913 j_mayer
}
1784 8ecc7913 j_mayer
1785 e2684c0b Andreas Färber
void ppc40x_chip_reset (CPUPPCState *env)
1786 8ecc7913 j_mayer
{
1787 8ecc7913 j_mayer
    target_ulong dbsr;
1788 8ecc7913 j_mayer
1789 8ecc7913 j_mayer
    printf("Reset PowerPC chip\n");
1790 fc0b2c0f Alexander Graf
    cpu_interrupt(env, CPU_INTERRUPT_RESET);
1791 8ecc7913 j_mayer
    /* XXX: TODO reset all internal peripherals */
1792 8ecc7913 j_mayer
    dbsr = env->spr[SPR_40x_DBSR];
1793 8ecc7913 j_mayer
    dbsr &= ~0x00000300;
1794 04f20795 j_mayer
    dbsr |= 0x00000200;
1795 8ecc7913 j_mayer
    env->spr[SPR_40x_DBSR] = dbsr;
1796 8ecc7913 j_mayer
}
1797 8ecc7913 j_mayer
1798 e2684c0b Andreas Färber
void ppc40x_system_reset (CPUPPCState *env)
1799 8ecc7913 j_mayer
{
1800 8ecc7913 j_mayer
    printf("Reset PowerPC system\n");
1801 8ecc7913 j_mayer
    qemu_system_reset_request();
1802 8ecc7913 j_mayer
}
1803 8ecc7913 j_mayer
1804 e2684c0b Andreas Färber
void store_40x_dbcr0 (CPUPPCState *env, uint32_t val)
1805 8ecc7913 j_mayer
{
1806 8ecc7913 j_mayer
    switch ((val >> 28) & 0x3) {
1807 8ecc7913 j_mayer
    case 0x0:
1808 8ecc7913 j_mayer
        /* No action */
1809 8ecc7913 j_mayer
        break;
1810 8ecc7913 j_mayer
    case 0x1:
1811 8ecc7913 j_mayer
        /* Core reset */
1812 8ecc7913 j_mayer
        ppc40x_core_reset(env);
1813 8ecc7913 j_mayer
        break;
1814 8ecc7913 j_mayer
    case 0x2:
1815 8ecc7913 j_mayer
        /* Chip reset */
1816 8ecc7913 j_mayer
        ppc40x_chip_reset(env);
1817 8ecc7913 j_mayer
        break;
1818 8ecc7913 j_mayer
    case 0x3:
1819 8ecc7913 j_mayer
        /* System reset */
1820 8ecc7913 j_mayer
        ppc40x_system_reset(env);
1821 8ecc7913 j_mayer
        break;
1822 8ecc7913 j_mayer
    }
1823 8ecc7913 j_mayer
}
1824 8ecc7913 j_mayer
1825 8ecc7913 j_mayer
/*****************************************************************************/
1826 8ecc7913 j_mayer
/* PowerPC 405CR */
1827 8ecc7913 j_mayer
enum {
1828 8ecc7913 j_mayer
    PPC405CR_CPC0_PLLMR  = 0x0B0,
1829 8ecc7913 j_mayer
    PPC405CR_CPC0_CR0    = 0x0B1,
1830 8ecc7913 j_mayer
    PPC405CR_CPC0_CR1    = 0x0B2,
1831 8ecc7913 j_mayer
    PPC405CR_CPC0_PSR    = 0x0B4,
1832 8ecc7913 j_mayer
    PPC405CR_CPC0_JTAGID = 0x0B5,
1833 8ecc7913 j_mayer
    PPC405CR_CPC0_ER     = 0x0B9,
1834 8ecc7913 j_mayer
    PPC405CR_CPC0_FR     = 0x0BA,
1835 8ecc7913 j_mayer
    PPC405CR_CPC0_SR     = 0x0BB,
1836 8ecc7913 j_mayer
};
1837 8ecc7913 j_mayer
1838 04f20795 j_mayer
enum {
1839 04f20795 j_mayer
    PPC405CR_CPU_CLK   = 0,
1840 04f20795 j_mayer
    PPC405CR_TMR_CLK   = 1,
1841 04f20795 j_mayer
    PPC405CR_PLB_CLK   = 2,
1842 04f20795 j_mayer
    PPC405CR_SDRAM_CLK = 3,
1843 04f20795 j_mayer
    PPC405CR_OPB_CLK   = 4,
1844 04f20795 j_mayer
    PPC405CR_EXT_CLK   = 5,
1845 04f20795 j_mayer
    PPC405CR_UART_CLK  = 6,
1846 04f20795 j_mayer
    PPC405CR_CLK_NB    = 7,
1847 04f20795 j_mayer
};
1848 04f20795 j_mayer
1849 c227f099 Anthony Liguori
typedef struct ppc405cr_cpc_t ppc405cr_cpc_t;
1850 c227f099 Anthony Liguori
struct ppc405cr_cpc_t {
1851 c227f099 Anthony Liguori
    clk_setup_t clk_setup[PPC405CR_CLK_NB];
1852 8ecc7913 j_mayer
    uint32_t sysclk;
1853 8ecc7913 j_mayer
    uint32_t psr;
1854 8ecc7913 j_mayer
    uint32_t cr0;
1855 8ecc7913 j_mayer
    uint32_t cr1;
1856 8ecc7913 j_mayer
    uint32_t jtagid;
1857 8ecc7913 j_mayer
    uint32_t pllmr;
1858 8ecc7913 j_mayer
    uint32_t er;
1859 8ecc7913 j_mayer
    uint32_t fr;
1860 8ecc7913 j_mayer
};
1861 8ecc7913 j_mayer
1862 c227f099 Anthony Liguori
static void ppc405cr_clk_setup (ppc405cr_cpc_t *cpc)
1863 8ecc7913 j_mayer
{
1864 8ecc7913 j_mayer
    uint64_t VCO_out, PLL_out;
1865 8ecc7913 j_mayer
    uint32_t CPU_clk, TMR_clk, SDRAM_clk, PLB_clk, OPB_clk, EXT_clk, UART_clk;
1866 8ecc7913 j_mayer
    int M, D0, D1, D2;
1867 8ecc7913 j_mayer
1868 8ecc7913 j_mayer
    D0 = ((cpc->pllmr >> 26) & 0x3) + 1; /* CBDV */
1869 8ecc7913 j_mayer
    if (cpc->pllmr & 0x80000000) {
1870 8ecc7913 j_mayer
        D1 = (((cpc->pllmr >> 20) - 1) & 0xF) + 1; /* FBDV */
1871 8ecc7913 j_mayer
        D2 = 8 - ((cpc->pllmr >> 16) & 0x7); /* FWDVA */
1872 8ecc7913 j_mayer
        M = D0 * D1 * D2;
1873 8ecc7913 j_mayer
        VCO_out = cpc->sysclk * M;
1874 8ecc7913 j_mayer
        if (VCO_out < 400000000 || VCO_out > 800000000) {
1875 8ecc7913 j_mayer
            /* PLL cannot lock */
1876 8ecc7913 j_mayer
            cpc->pllmr &= ~0x80000000;
1877 8ecc7913 j_mayer
            goto bypass_pll;
1878 8ecc7913 j_mayer
        }
1879 8ecc7913 j_mayer
        PLL_out = VCO_out / D2;
1880 8ecc7913 j_mayer
    } else {
1881 8ecc7913 j_mayer
        /* Bypass PLL */
1882 8ecc7913 j_mayer
    bypass_pll:
1883 8ecc7913 j_mayer
        M = D0;
1884 8ecc7913 j_mayer
        PLL_out = cpc->sysclk * M;
1885 8ecc7913 j_mayer
    }
1886 8ecc7913 j_mayer
    CPU_clk = PLL_out;
1887 8ecc7913 j_mayer
    if (cpc->cr1 & 0x00800000)
1888 8ecc7913 j_mayer
        TMR_clk = cpc->sysclk; /* Should have a separate clock */
1889 8ecc7913 j_mayer
    else
1890 8ecc7913 j_mayer
        TMR_clk = CPU_clk;
1891 8ecc7913 j_mayer
    PLB_clk = CPU_clk / D0;
1892 8ecc7913 j_mayer
    SDRAM_clk = PLB_clk;
1893 8ecc7913 j_mayer
    D0 = ((cpc->pllmr >> 10) & 0x3) + 1;
1894 8ecc7913 j_mayer
    OPB_clk = PLB_clk / D0;
1895 8ecc7913 j_mayer
    D0 = ((cpc->pllmr >> 24) & 0x3) + 2;
1896 8ecc7913 j_mayer
    EXT_clk = PLB_clk / D0;
1897 8ecc7913 j_mayer
    D0 = ((cpc->cr0 >> 1) & 0x1F) + 1;
1898 8ecc7913 j_mayer
    UART_clk = CPU_clk / D0;
1899 8ecc7913 j_mayer
    /* Setup CPU clocks */
1900 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_CPU_CLK], CPU_clk);
1901 8ecc7913 j_mayer
    /* Setup time-base clock */
1902 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_TMR_CLK], TMR_clk);
1903 8ecc7913 j_mayer
    /* Setup PLB clock */
1904 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_PLB_CLK], PLB_clk);
1905 8ecc7913 j_mayer
    /* Setup SDRAM clock */
1906 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_SDRAM_CLK], SDRAM_clk);
1907 8ecc7913 j_mayer
    /* Setup OPB clock */
1908 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_OPB_CLK], OPB_clk);
1909 8ecc7913 j_mayer
    /* Setup external clock */
1910 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_EXT_CLK], EXT_clk);
1911 8ecc7913 j_mayer
    /* Setup UART clock */
1912 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405CR_UART_CLK], UART_clk);
1913 8ecc7913 j_mayer
}
1914 8ecc7913 j_mayer
1915 73b01960 Alexander Graf
static uint32_t dcr_read_crcpc (void *opaque, int dcrn)
1916 8ecc7913 j_mayer
{
1917 c227f099 Anthony Liguori
    ppc405cr_cpc_t *cpc;
1918 73b01960 Alexander Graf
    uint32_t ret;
1919 8ecc7913 j_mayer
1920 8ecc7913 j_mayer
    cpc = opaque;
1921 8ecc7913 j_mayer
    switch (dcrn) {
1922 8ecc7913 j_mayer
    case PPC405CR_CPC0_PLLMR:
1923 8ecc7913 j_mayer
        ret = cpc->pllmr;
1924 8ecc7913 j_mayer
        break;
1925 8ecc7913 j_mayer
    case PPC405CR_CPC0_CR0:
1926 8ecc7913 j_mayer
        ret = cpc->cr0;
1927 8ecc7913 j_mayer
        break;
1928 8ecc7913 j_mayer
    case PPC405CR_CPC0_CR1:
1929 8ecc7913 j_mayer
        ret = cpc->cr1;
1930 8ecc7913 j_mayer
        break;
1931 8ecc7913 j_mayer
    case PPC405CR_CPC0_PSR:
1932 8ecc7913 j_mayer
        ret = cpc->psr;
1933 8ecc7913 j_mayer
        break;
1934 8ecc7913 j_mayer
    case PPC405CR_CPC0_JTAGID:
1935 8ecc7913 j_mayer
        ret = cpc->jtagid;
1936 8ecc7913 j_mayer
        break;
1937 8ecc7913 j_mayer
    case PPC405CR_CPC0_ER:
1938 8ecc7913 j_mayer
        ret = cpc->er;
1939 8ecc7913 j_mayer
        break;
1940 8ecc7913 j_mayer
    case PPC405CR_CPC0_FR:
1941 8ecc7913 j_mayer
        ret = cpc->fr;
1942 8ecc7913 j_mayer
        break;
1943 8ecc7913 j_mayer
    case PPC405CR_CPC0_SR:
1944 8ecc7913 j_mayer
        ret = ~(cpc->er | cpc->fr) & 0xFFFF0000;
1945 8ecc7913 j_mayer
        break;
1946 8ecc7913 j_mayer
    default:
1947 8ecc7913 j_mayer
        /* Avoid gcc warning */
1948 8ecc7913 j_mayer
        ret = 0;
1949 8ecc7913 j_mayer
        break;
1950 8ecc7913 j_mayer
    }
1951 8ecc7913 j_mayer
1952 8ecc7913 j_mayer
    return ret;
1953 8ecc7913 j_mayer
}
1954 8ecc7913 j_mayer
1955 73b01960 Alexander Graf
static void dcr_write_crcpc (void *opaque, int dcrn, uint32_t val)
1956 8ecc7913 j_mayer
{
1957 c227f099 Anthony Liguori
    ppc405cr_cpc_t *cpc;
1958 8ecc7913 j_mayer
1959 8ecc7913 j_mayer
    cpc = opaque;
1960 8ecc7913 j_mayer
    switch (dcrn) {
1961 8ecc7913 j_mayer
    case PPC405CR_CPC0_PLLMR:
1962 8ecc7913 j_mayer
        cpc->pllmr = val & 0xFFF77C3F;
1963 8ecc7913 j_mayer
        break;
1964 8ecc7913 j_mayer
    case PPC405CR_CPC0_CR0:
1965 8ecc7913 j_mayer
        cpc->cr0 = val & 0x0FFFFFFE;
1966 8ecc7913 j_mayer
        break;
1967 8ecc7913 j_mayer
    case PPC405CR_CPC0_CR1:
1968 8ecc7913 j_mayer
        cpc->cr1 = val & 0x00800000;
1969 8ecc7913 j_mayer
        break;
1970 8ecc7913 j_mayer
    case PPC405CR_CPC0_PSR:
1971 8ecc7913 j_mayer
        /* Read-only */
1972 8ecc7913 j_mayer
        break;
1973 8ecc7913 j_mayer
    case PPC405CR_CPC0_JTAGID:
1974 8ecc7913 j_mayer
        /* Read-only */
1975 8ecc7913 j_mayer
        break;
1976 8ecc7913 j_mayer
    case PPC405CR_CPC0_ER:
1977 8ecc7913 j_mayer
        cpc->er = val & 0xBFFC0000;
1978 8ecc7913 j_mayer
        break;
1979 8ecc7913 j_mayer
    case PPC405CR_CPC0_FR:
1980 8ecc7913 j_mayer
        cpc->fr = val & 0xBFFC0000;
1981 8ecc7913 j_mayer
        break;
1982 8ecc7913 j_mayer
    case PPC405CR_CPC0_SR:
1983 8ecc7913 j_mayer
        /* Read-only */
1984 8ecc7913 j_mayer
        break;
1985 8ecc7913 j_mayer
    }
1986 8ecc7913 j_mayer
}
1987 8ecc7913 j_mayer
1988 8ecc7913 j_mayer
static void ppc405cr_cpc_reset (void *opaque)
1989 8ecc7913 j_mayer
{
1990 c227f099 Anthony Liguori
    ppc405cr_cpc_t *cpc;
1991 8ecc7913 j_mayer
    int D;
1992 8ecc7913 j_mayer
1993 8ecc7913 j_mayer
    cpc = opaque;
1994 8ecc7913 j_mayer
    /* Compute PLLMR value from PSR settings */
1995 8ecc7913 j_mayer
    cpc->pllmr = 0x80000000;
1996 8ecc7913 j_mayer
    /* PFWD */
1997 8ecc7913 j_mayer
    switch ((cpc->psr >> 30) & 3) {
1998 8ecc7913 j_mayer
    case 0:
1999 8ecc7913 j_mayer
        /* Bypass */
2000 8ecc7913 j_mayer
        cpc->pllmr &= ~0x80000000;
2001 8ecc7913 j_mayer
        break;
2002 8ecc7913 j_mayer
    case 1:
2003 8ecc7913 j_mayer
        /* Divide by 3 */
2004 8ecc7913 j_mayer
        cpc->pllmr |= 5 << 16;
2005 8ecc7913 j_mayer
        break;
2006 8ecc7913 j_mayer
    case 2:
2007 8ecc7913 j_mayer
        /* Divide by 4 */
2008 8ecc7913 j_mayer
        cpc->pllmr |= 4 << 16;
2009 8ecc7913 j_mayer
        break;
2010 8ecc7913 j_mayer
    case 3:
2011 8ecc7913 j_mayer
        /* Divide by 6 */
2012 8ecc7913 j_mayer
        cpc->pllmr |= 2 << 16;
2013 8ecc7913 j_mayer
        break;
2014 8ecc7913 j_mayer
    }
2015 8ecc7913 j_mayer
    /* PFBD */
2016 8ecc7913 j_mayer
    D = (cpc->psr >> 28) & 3;
2017 8ecc7913 j_mayer
    cpc->pllmr |= (D + 1) << 20;
2018 8ecc7913 j_mayer
    /* PT   */
2019 8ecc7913 j_mayer
    D = (cpc->psr >> 25) & 7;
2020 8ecc7913 j_mayer
    switch (D) {
2021 8ecc7913 j_mayer
    case 0x2:
2022 8ecc7913 j_mayer
        cpc->pllmr |= 0x13;
2023 8ecc7913 j_mayer
        break;
2024 8ecc7913 j_mayer
    case 0x4:
2025 8ecc7913 j_mayer
        cpc->pllmr |= 0x15;
2026 8ecc7913 j_mayer
        break;
2027 8ecc7913 j_mayer
    case 0x5:
2028 8ecc7913 j_mayer
        cpc->pllmr |= 0x16;
2029 8ecc7913 j_mayer
        break;
2030 8ecc7913 j_mayer
    default:
2031 8ecc7913 j_mayer
        break;
2032 8ecc7913 j_mayer
    }
2033 8ecc7913 j_mayer
    /* PDC  */
2034 8ecc7913 j_mayer
    D = (cpc->psr >> 23) & 3;
2035 8ecc7913 j_mayer
    cpc->pllmr |= D << 26;
2036 8ecc7913 j_mayer
    /* ODP  */
2037 8ecc7913 j_mayer
    D = (cpc->psr >> 21) & 3;
2038 8ecc7913 j_mayer
    cpc->pllmr |= D << 10;
2039 8ecc7913 j_mayer
    /* EBPD */
2040 8ecc7913 j_mayer
    D = (cpc->psr >> 17) & 3;
2041 8ecc7913 j_mayer
    cpc->pllmr |= D << 24;
2042 8ecc7913 j_mayer
    cpc->cr0 = 0x0000003C;
2043 8ecc7913 j_mayer
    cpc->cr1 = 0x2B0D8800;
2044 8ecc7913 j_mayer
    cpc->er = 0x00000000;
2045 8ecc7913 j_mayer
    cpc->fr = 0x00000000;
2046 8ecc7913 j_mayer
    ppc405cr_clk_setup(cpc);
2047 8ecc7913 j_mayer
}
2048 8ecc7913 j_mayer
2049 c227f099 Anthony Liguori
static void ppc405cr_clk_init (ppc405cr_cpc_t *cpc)
2050 8ecc7913 j_mayer
{
2051 8ecc7913 j_mayer
    int D;
2052 8ecc7913 j_mayer
2053 8ecc7913 j_mayer
    /* XXX: this should be read from IO pins */
2054 8ecc7913 j_mayer
    cpc->psr = 0x00000000; /* 8 bits ROM */
2055 8ecc7913 j_mayer
    /* PFWD */
2056 8ecc7913 j_mayer
    D = 0x2; /* Divide by 4 */
2057 8ecc7913 j_mayer
    cpc->psr |= D << 30;
2058 8ecc7913 j_mayer
    /* PFBD */
2059 8ecc7913 j_mayer
    D = 0x1; /* Divide by 2 */
2060 8ecc7913 j_mayer
    cpc->psr |= D << 28;
2061 8ecc7913 j_mayer
    /* PDC */
2062 8ecc7913 j_mayer
    D = 0x1; /* Divide by 2 */
2063 8ecc7913 j_mayer
    cpc->psr |= D << 23;
2064 8ecc7913 j_mayer
    /* PT */
2065 8ecc7913 j_mayer
    D = 0x5; /* M = 16 */
2066 8ecc7913 j_mayer
    cpc->psr |= D << 25;
2067 8ecc7913 j_mayer
    /* ODP */
2068 8ecc7913 j_mayer
    D = 0x1; /* Divide by 2 */
2069 8ecc7913 j_mayer
    cpc->psr |= D << 21;
2070 8ecc7913 j_mayer
    /* EBDP */
2071 8ecc7913 j_mayer
    D = 0x2; /* Divide by 4 */
2072 8ecc7913 j_mayer
    cpc->psr |= D << 17;
2073 8ecc7913 j_mayer
}
2074 8ecc7913 j_mayer
2075 e2684c0b Andreas Färber
static void ppc405cr_cpc_init (CPUPPCState *env, clk_setup_t clk_setup[7],
2076 8ecc7913 j_mayer
                               uint32_t sysclk)
2077 8ecc7913 j_mayer
{
2078 c227f099 Anthony Liguori
    ppc405cr_cpc_t *cpc;
2079 8ecc7913 j_mayer
2080 7267c094 Anthony Liguori
    cpc = g_malloc0(sizeof(ppc405cr_cpc_t));
2081 487414f1 aliguori
    memcpy(cpc->clk_setup, clk_setup,
2082 c227f099 Anthony Liguori
           PPC405CR_CLK_NB * sizeof(clk_setup_t));
2083 487414f1 aliguori
    cpc->sysclk = sysclk;
2084 487414f1 aliguori
    cpc->jtagid = 0x42051049;
2085 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_PSR, cpc,
2086 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2087 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_CR0, cpc,
2088 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2089 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_CR1, cpc,
2090 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2091 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_JTAGID, cpc,
2092 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2093 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_PLLMR, cpc,
2094 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2095 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_ER, cpc,
2096 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2097 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_FR, cpc,
2098 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2099 487414f1 aliguori
    ppc_dcr_register(env, PPC405CR_CPC0_SR, cpc,
2100 487414f1 aliguori
                     &dcr_read_crcpc, &dcr_write_crcpc);
2101 487414f1 aliguori
    ppc405cr_clk_init(cpc);
2102 a08d4367 Jan Kiszka
    qemu_register_reset(ppc405cr_cpc_reset, cpc);
2103 8ecc7913 j_mayer
}
2104 8ecc7913 j_mayer
2105 e2684c0b Andreas Färber
CPUPPCState *ppc405cr_init(MemoryRegion *address_space_mem,
2106 52ce55a1 Richard Henderson
                        MemoryRegion ram_memories[4],
2107 a8170e5e Avi Kivity
                        hwaddr ram_bases[4],
2108 a8170e5e Avi Kivity
                        hwaddr ram_sizes[4],
2109 52ce55a1 Richard Henderson
                        uint32_t sysclk, qemu_irq **picp,
2110 52ce55a1 Richard Henderson
                        int do_init)
2111 8ecc7913 j_mayer
{
2112 c227f099 Anthony Liguori
    clk_setup_t clk_setup[PPC405CR_CLK_NB];
2113 8ecc7913 j_mayer
    qemu_irq dma_irqs[4];
2114 2f9859fb Andreas Färber
    PowerPCCPU *cpu;
2115 e2684c0b Andreas Färber
    CPUPPCState *env;
2116 8ecc7913 j_mayer
    qemu_irq *pic, *irqs;
2117 8ecc7913 j_mayer
2118 8ecc7913 j_mayer
    memset(clk_setup, 0, sizeof(clk_setup));
2119 2f9859fb Andreas Färber
    cpu = ppc4xx_init("405cr", &clk_setup[PPC405CR_CPU_CLK],
2120 04f20795 j_mayer
                      &clk_setup[PPC405CR_TMR_CLK], sysclk);
2121 2f9859fb Andreas Färber
    env = &cpu->env;
2122 8ecc7913 j_mayer
    /* Memory mapped devices registers */
2123 8ecc7913 j_mayer
    /* PLB arbitrer */
2124 8ecc7913 j_mayer
    ppc4xx_plb_init(env);
2125 8ecc7913 j_mayer
    /* PLB to OPB bridge */
2126 8ecc7913 j_mayer
    ppc4xx_pob_init(env);
2127 8ecc7913 j_mayer
    /* OBP arbitrer */
2128 802670e6 Blue Swirl
    ppc4xx_opba_init(0xef600600);
2129 8ecc7913 j_mayer
    /* Universal interrupt controller */
2130 7267c094 Anthony Liguori
    irqs = g_malloc0(sizeof(qemu_irq) * PPCUIC_OUTPUT_NB);
2131 8ecc7913 j_mayer
    irqs[PPCUIC_OUTPUT_INT] =
2132 b48d7d69 j_mayer
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
2133 8ecc7913 j_mayer
    irqs[PPCUIC_OUTPUT_CINT] =
2134 b48d7d69 j_mayer
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
2135 8ecc7913 j_mayer
    pic = ppcuic_init(env, irqs, 0x0C0, 0, 1);
2136 8ecc7913 j_mayer
    *picp = pic;
2137 8ecc7913 j_mayer
    /* SDRAM controller */
2138 b6dcbe08 Avi Kivity
    ppc4xx_sdram_init(env, pic[14], 1, ram_memories,
2139 b6dcbe08 Avi Kivity
                      ram_bases, ram_sizes, do_init);
2140 8ecc7913 j_mayer
    /* External bus controller */
2141 8ecc7913 j_mayer
    ppc405_ebc_init(env);
2142 8ecc7913 j_mayer
    /* DMA controller */
2143 04f20795 j_mayer
    dma_irqs[0] = pic[26];
2144 04f20795 j_mayer
    dma_irqs[1] = pic[25];
2145 04f20795 j_mayer
    dma_irqs[2] = pic[24];
2146 04f20795 j_mayer
    dma_irqs[3] = pic[23];
2147 8ecc7913 j_mayer
    ppc405_dma_init(env, dma_irqs);
2148 8ecc7913 j_mayer
    /* Serial ports */
2149 8ecc7913 j_mayer
    if (serial_hds[0] != NULL) {
2150 52ce55a1 Richard Henderson
        serial_mm_init(address_space_mem, 0xef600300, 0, pic[0],
2151 39186d8a Richard Henderson
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[0],
2152 39186d8a Richard Henderson
                       DEVICE_BIG_ENDIAN);
2153 8ecc7913 j_mayer
    }
2154 8ecc7913 j_mayer
    if (serial_hds[1] != NULL) {
2155 52ce55a1 Richard Henderson
        serial_mm_init(address_space_mem, 0xef600400, 0, pic[1],
2156 39186d8a Richard Henderson
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[1],
2157 39186d8a Richard Henderson
                       DEVICE_BIG_ENDIAN);
2158 8ecc7913 j_mayer
    }
2159 8ecc7913 j_mayer
    /* IIC controller */
2160 802670e6 Blue Swirl
    ppc405_i2c_init(0xef600500, pic[2]);
2161 8ecc7913 j_mayer
    /* GPIO */
2162 802670e6 Blue Swirl
    ppc405_gpio_init(0xef600700);
2163 8ecc7913 j_mayer
    /* CPU control */
2164 8ecc7913 j_mayer
    ppc405cr_cpc_init(env, clk_setup, sysclk);
2165 8ecc7913 j_mayer
2166 8ecc7913 j_mayer
    return env;
2167 8ecc7913 j_mayer
}
2168 8ecc7913 j_mayer
2169 8ecc7913 j_mayer
/*****************************************************************************/
2170 8ecc7913 j_mayer
/* PowerPC 405EP */
2171 8ecc7913 j_mayer
/* CPU control */
2172 8ecc7913 j_mayer
enum {
2173 8ecc7913 j_mayer
    PPC405EP_CPC0_PLLMR0 = 0x0F0,
2174 8ecc7913 j_mayer
    PPC405EP_CPC0_BOOT   = 0x0F1,
2175 8ecc7913 j_mayer
    PPC405EP_CPC0_EPCTL  = 0x0F3,
2176 8ecc7913 j_mayer
    PPC405EP_CPC0_PLLMR1 = 0x0F4,
2177 8ecc7913 j_mayer
    PPC405EP_CPC0_UCR    = 0x0F5,
2178 8ecc7913 j_mayer
    PPC405EP_CPC0_SRR    = 0x0F6,
2179 8ecc7913 j_mayer
    PPC405EP_CPC0_JTAGID = 0x0F7,
2180 8ecc7913 j_mayer
    PPC405EP_CPC0_PCI    = 0x0F9,
2181 9c02f1a2 j_mayer
#if 0
2182 9c02f1a2 j_mayer
    PPC405EP_CPC0_ER     = xxx,
2183 9c02f1a2 j_mayer
    PPC405EP_CPC0_FR     = xxx,
2184 9c02f1a2 j_mayer
    PPC405EP_CPC0_SR     = xxx,
2185 9c02f1a2 j_mayer
#endif
2186 8ecc7913 j_mayer
};
2187 8ecc7913 j_mayer
2188 04f20795 j_mayer
enum {
2189 04f20795 j_mayer
    PPC405EP_CPU_CLK   = 0,
2190 04f20795 j_mayer
    PPC405EP_PLB_CLK   = 1,
2191 04f20795 j_mayer
    PPC405EP_OPB_CLK   = 2,
2192 04f20795 j_mayer
    PPC405EP_EBC_CLK   = 3,
2193 04f20795 j_mayer
    PPC405EP_MAL_CLK   = 4,
2194 04f20795 j_mayer
    PPC405EP_PCI_CLK   = 5,
2195 04f20795 j_mayer
    PPC405EP_UART0_CLK = 6,
2196 04f20795 j_mayer
    PPC405EP_UART1_CLK = 7,
2197 04f20795 j_mayer
    PPC405EP_CLK_NB    = 8,
2198 04f20795 j_mayer
};
2199 04f20795 j_mayer
2200 c227f099 Anthony Liguori
typedef struct ppc405ep_cpc_t ppc405ep_cpc_t;
2201 c227f099 Anthony Liguori
struct ppc405ep_cpc_t {
2202 8ecc7913 j_mayer
    uint32_t sysclk;
2203 c227f099 Anthony Liguori
    clk_setup_t clk_setup[PPC405EP_CLK_NB];
2204 8ecc7913 j_mayer
    uint32_t boot;
2205 8ecc7913 j_mayer
    uint32_t epctl;
2206 8ecc7913 j_mayer
    uint32_t pllmr[2];
2207 8ecc7913 j_mayer
    uint32_t ucr;
2208 8ecc7913 j_mayer
    uint32_t srr;
2209 8ecc7913 j_mayer
    uint32_t jtagid;
2210 8ecc7913 j_mayer
    uint32_t pci;
2211 9c02f1a2 j_mayer
    /* Clock and power management */
2212 9c02f1a2 j_mayer
    uint32_t er;
2213 9c02f1a2 j_mayer
    uint32_t fr;
2214 9c02f1a2 j_mayer
    uint32_t sr;
2215 8ecc7913 j_mayer
};
2216 8ecc7913 j_mayer
2217 c227f099 Anthony Liguori
static void ppc405ep_compute_clocks (ppc405ep_cpc_t *cpc)
2218 8ecc7913 j_mayer
{
2219 8ecc7913 j_mayer
    uint32_t CPU_clk, PLB_clk, OPB_clk, EBC_clk, MAL_clk, PCI_clk;
2220 8ecc7913 j_mayer
    uint32_t UART0_clk, UART1_clk;
2221 8ecc7913 j_mayer
    uint64_t VCO_out, PLL_out;
2222 8ecc7913 j_mayer
    int M, D;
2223 8ecc7913 j_mayer
2224 8ecc7913 j_mayer
    VCO_out = 0;
2225 8ecc7913 j_mayer
    if ((cpc->pllmr[1] & 0x80000000) && !(cpc->pllmr[1] & 0x40000000)) {
2226 8ecc7913 j_mayer
        M = (((cpc->pllmr[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2227 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2228 aae9366a j_mayer
        printf("FBMUL %01" PRIx32 " %d\n", (cpc->pllmr[1] >> 20) & 0xF, M);
2229 aae9366a j_mayer
#endif
2230 8ecc7913 j_mayer
        D = 8 - ((cpc->pllmr[1] >> 16) & 0x7); /* FWDA */
2231 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2232 aae9366a j_mayer
        printf("FWDA %01" PRIx32 " %d\n", (cpc->pllmr[1] >> 16) & 0x7, D);
2233 aae9366a j_mayer
#endif
2234 8ecc7913 j_mayer
        VCO_out = cpc->sysclk * M * D;
2235 8ecc7913 j_mayer
        if (VCO_out < 500000000UL || VCO_out > 1000000000UL) {
2236 8ecc7913 j_mayer
            /* Error - unlock the PLL */
2237 8ecc7913 j_mayer
            printf("VCO out of range %" PRIu64 "\n", VCO_out);
2238 8ecc7913 j_mayer
#if 0
2239 8ecc7913 j_mayer
            cpc->pllmr[1] &= ~0x80000000;
2240 8ecc7913 j_mayer
            goto pll_bypass;
2241 8ecc7913 j_mayer
#endif
2242 8ecc7913 j_mayer
        }
2243 8ecc7913 j_mayer
        PLL_out = VCO_out / D;
2244 9c02f1a2 j_mayer
        /* Pretend the PLL is locked */
2245 9c02f1a2 j_mayer
        cpc->boot |= 0x00000001;
2246 8ecc7913 j_mayer
    } else {
2247 8ecc7913 j_mayer
#if 0
2248 8ecc7913 j_mayer
    pll_bypass:
2249 8ecc7913 j_mayer
#endif
2250 8ecc7913 j_mayer
        PLL_out = cpc->sysclk;
2251 9c02f1a2 j_mayer
        if (cpc->pllmr[1] & 0x40000000) {
2252 9c02f1a2 j_mayer
            /* Pretend the PLL is not locked */
2253 9c02f1a2 j_mayer
            cpc->boot &= ~0x00000001;
2254 9c02f1a2 j_mayer
        }
2255 8ecc7913 j_mayer
    }
2256 8ecc7913 j_mayer
    /* Now, compute all other clocks */
2257 8ecc7913 j_mayer
    D = ((cpc->pllmr[0] >> 20) & 0x3) + 1; /* CCDV */
2258 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2259 aae9366a j_mayer
    printf("CCDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 20) & 0x3, D);
2260 8ecc7913 j_mayer
#endif
2261 8ecc7913 j_mayer
    CPU_clk = PLL_out / D;
2262 8ecc7913 j_mayer
    D = ((cpc->pllmr[0] >> 16) & 0x3) + 1; /* CBDV */
2263 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2264 aae9366a j_mayer
    printf("CBDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 16) & 0x3, D);
2265 8ecc7913 j_mayer
#endif
2266 8ecc7913 j_mayer
    PLB_clk = CPU_clk / D;
2267 8ecc7913 j_mayer
    D = ((cpc->pllmr[0] >> 12) & 0x3) + 1; /* OPDV */
2268 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2269 aae9366a j_mayer
    printf("OPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 12) & 0x3, D);
2270 8ecc7913 j_mayer
#endif
2271 8ecc7913 j_mayer
    OPB_clk = PLB_clk / D;
2272 8ecc7913 j_mayer
    D = ((cpc->pllmr[0] >> 8) & 0x3) + 2; /* EPDV */
2273 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2274 aae9366a j_mayer
    printf("EPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 8) & 0x3, D);
2275 8ecc7913 j_mayer
#endif
2276 8ecc7913 j_mayer
    EBC_clk = PLB_clk / D;
2277 8ecc7913 j_mayer
    D = ((cpc->pllmr[0] >> 4) & 0x3) + 1; /* MPDV */
2278 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2279 aae9366a j_mayer
    printf("MPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 4) & 0x3, D);
2280 8ecc7913 j_mayer
#endif
2281 8ecc7913 j_mayer
    MAL_clk = PLB_clk / D;
2282 8ecc7913 j_mayer
    D = (cpc->pllmr[0] & 0x3) + 1; /* PPDV */
2283 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2284 aae9366a j_mayer
    printf("PPDV %01" PRIx32 " %d\n", cpc->pllmr[0] & 0x3, D);
2285 8ecc7913 j_mayer
#endif
2286 8ecc7913 j_mayer
    PCI_clk = PLB_clk / D;
2287 8ecc7913 j_mayer
    D = ((cpc->ucr - 1) & 0x7F) + 1; /* U0DIV */
2288 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2289 aae9366a j_mayer
    printf("U0DIV %01" PRIx32 " %d\n", cpc->ucr & 0x7F, D);
2290 8ecc7913 j_mayer
#endif
2291 8ecc7913 j_mayer
    UART0_clk = PLL_out / D;
2292 8ecc7913 j_mayer
    D = (((cpc->ucr >> 8) - 1) & 0x7F) + 1; /* U1DIV */
2293 aae9366a j_mayer
#ifdef DEBUG_CLOCKS_LL
2294 aae9366a j_mayer
    printf("U1DIV %01" PRIx32 " %d\n", (cpc->ucr >> 8) & 0x7F, D);
2295 8ecc7913 j_mayer
#endif
2296 8ecc7913 j_mayer
    UART1_clk = PLL_out / D;
2297 8ecc7913 j_mayer
#ifdef DEBUG_CLOCKS
2298 aae9366a j_mayer
    printf("Setup PPC405EP clocks - sysclk %" PRIu32 " VCO %" PRIu64
2299 8ecc7913 j_mayer
           " PLL out %" PRIu64 " Hz\n", cpc->sysclk, VCO_out, PLL_out);
2300 aae9366a j_mayer
    printf("CPU %" PRIu32 " PLB %" PRIu32 " OPB %" PRIu32 " EBC %" PRIu32
2301 aae9366a j_mayer
           " MAL %" PRIu32 " PCI %" PRIu32 " UART0 %" PRIu32
2302 aae9366a j_mayer
           " UART1 %" PRIu32 "\n",
2303 8ecc7913 j_mayer
           CPU_clk, PLB_clk, OPB_clk, EBC_clk, MAL_clk, PCI_clk,
2304 8ecc7913 j_mayer
           UART0_clk, UART1_clk);
2305 8ecc7913 j_mayer
#endif
2306 8ecc7913 j_mayer
    /* Setup CPU clocks */
2307 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_CPU_CLK], CPU_clk);
2308 8ecc7913 j_mayer
    /* Setup PLB clock */
2309 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_PLB_CLK], PLB_clk);
2310 8ecc7913 j_mayer
    /* Setup OPB clock */
2311 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_OPB_CLK], OPB_clk);
2312 8ecc7913 j_mayer
    /* Setup external clock */
2313 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_EBC_CLK], EBC_clk);
2314 8ecc7913 j_mayer
    /* Setup MAL clock */
2315 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_MAL_CLK], MAL_clk);
2316 8ecc7913 j_mayer
    /* Setup PCI clock */
2317 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_PCI_CLK], PCI_clk);
2318 8ecc7913 j_mayer
    /* Setup UART0 clock */
2319 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_UART0_CLK], UART0_clk);
2320 8ecc7913 j_mayer
    /* Setup UART1 clock */
2321 04f20795 j_mayer
    clk_setup(&cpc->clk_setup[PPC405EP_UART1_CLK], UART1_clk);
2322 8ecc7913 j_mayer
}
2323 8ecc7913 j_mayer
2324 73b01960 Alexander Graf
static uint32_t dcr_read_epcpc (void *opaque, int dcrn)
2325 8ecc7913 j_mayer
{
2326 c227f099 Anthony Liguori
    ppc405ep_cpc_t *cpc;
2327 73b01960 Alexander Graf
    uint32_t ret;
2328 8ecc7913 j_mayer
2329 8ecc7913 j_mayer
    cpc = opaque;
2330 8ecc7913 j_mayer
    switch (dcrn) {
2331 8ecc7913 j_mayer
    case PPC405EP_CPC0_BOOT:
2332 8ecc7913 j_mayer
        ret = cpc->boot;
2333 8ecc7913 j_mayer
        break;
2334 8ecc7913 j_mayer
    case PPC405EP_CPC0_EPCTL:
2335 8ecc7913 j_mayer
        ret = cpc->epctl;
2336 8ecc7913 j_mayer
        break;
2337 8ecc7913 j_mayer
    case PPC405EP_CPC0_PLLMR0:
2338 8ecc7913 j_mayer
        ret = cpc->pllmr[0];
2339 8ecc7913 j_mayer
        break;
2340 8ecc7913 j_mayer
    case PPC405EP_CPC0_PLLMR1:
2341 8ecc7913 j_mayer
        ret = cpc->pllmr[1];
2342 8ecc7913 j_mayer
        break;
2343 8ecc7913 j_mayer
    case PPC405EP_CPC0_UCR:
2344 8ecc7913 j_mayer
        ret = cpc->ucr;
2345 8ecc7913 j_mayer
        break;
2346 8ecc7913 j_mayer
    case PPC405EP_CPC0_SRR:
2347 8ecc7913 j_mayer
        ret = cpc->srr;
2348 8ecc7913 j_mayer
        break;
2349 8ecc7913 j_mayer
    case PPC405EP_CPC0_JTAGID:
2350 8ecc7913 j_mayer
        ret = cpc->jtagid;
2351 8ecc7913 j_mayer
        break;
2352 8ecc7913 j_mayer
    case PPC405EP_CPC0_PCI:
2353 8ecc7913 j_mayer
        ret = cpc->pci;
2354 8ecc7913 j_mayer
        break;
2355 8ecc7913 j_mayer
    default:
2356 8ecc7913 j_mayer
        /* Avoid gcc warning */
2357 8ecc7913 j_mayer
        ret = 0;
2358 8ecc7913 j_mayer
        break;
2359 8ecc7913 j_mayer
    }
2360 8ecc7913 j_mayer
2361 8ecc7913 j_mayer
    return ret;
2362 8ecc7913 j_mayer
}
2363 8ecc7913 j_mayer
2364 73b01960 Alexander Graf
static void dcr_write_epcpc (void *opaque, int dcrn, uint32_t val)
2365 8ecc7913 j_mayer
{
2366 c227f099 Anthony Liguori
    ppc405ep_cpc_t *cpc;
2367 8ecc7913 j_mayer
2368 8ecc7913 j_mayer
    cpc = opaque;
2369 8ecc7913 j_mayer
    switch (dcrn) {
2370 8ecc7913 j_mayer
    case PPC405EP_CPC0_BOOT:
2371 8ecc7913 j_mayer
        /* Read-only register */
2372 8ecc7913 j_mayer
        break;
2373 8ecc7913 j_mayer
    case PPC405EP_CPC0_EPCTL:
2374 8ecc7913 j_mayer
        /* Don't care for now */
2375 8ecc7913 j_mayer
        cpc->epctl = val & 0xC00000F3;
2376 8ecc7913 j_mayer
        break;
2377 8ecc7913 j_mayer
    case PPC405EP_CPC0_PLLMR0:
2378 8ecc7913 j_mayer
        cpc->pllmr[0] = val & 0x00633333;
2379 8ecc7913 j_mayer
        ppc405ep_compute_clocks(cpc);
2380 8ecc7913 j_mayer
        break;
2381 8ecc7913 j_mayer
    case PPC405EP_CPC0_PLLMR1:
2382 8ecc7913 j_mayer
        cpc->pllmr[1] = val & 0xC0F73FFF;
2383 8ecc7913 j_mayer
        ppc405ep_compute_clocks(cpc);
2384 8ecc7913 j_mayer
        break;
2385 8ecc7913 j_mayer
    case PPC405EP_CPC0_UCR:
2386 8ecc7913 j_mayer
        /* UART control - don't care for now */
2387 8ecc7913 j_mayer
        cpc->ucr = val & 0x003F7F7F;
2388 8ecc7913 j_mayer
        break;
2389 8ecc7913 j_mayer
    case PPC405EP_CPC0_SRR:
2390 8ecc7913 j_mayer
        cpc->srr = val;
2391 8ecc7913 j_mayer
        break;
2392 8ecc7913 j_mayer
    case PPC405EP_CPC0_JTAGID:
2393 8ecc7913 j_mayer
        /* Read-only */
2394 8ecc7913 j_mayer
        break;
2395 8ecc7913 j_mayer
    case PPC405EP_CPC0_PCI:
2396 8ecc7913 j_mayer
        cpc->pci = val;
2397 8ecc7913 j_mayer
        break;
2398 8ecc7913 j_mayer
    }
2399 8ecc7913 j_mayer
}
2400 8ecc7913 j_mayer
2401 8ecc7913 j_mayer
static void ppc405ep_cpc_reset (void *opaque)
2402 8ecc7913 j_mayer
{
2403 c227f099 Anthony Liguori
    ppc405ep_cpc_t *cpc = opaque;
2404 8ecc7913 j_mayer
2405 8ecc7913 j_mayer
    cpc->boot = 0x00000010;     /* Boot from PCI - IIC EEPROM disabled */
2406 8ecc7913 j_mayer
    cpc->epctl = 0x00000000;
2407 8ecc7913 j_mayer
    cpc->pllmr[0] = 0x00011010;
2408 8ecc7913 j_mayer
    cpc->pllmr[1] = 0x40000000;
2409 8ecc7913 j_mayer
    cpc->ucr = 0x00000000;
2410 8ecc7913 j_mayer
    cpc->srr = 0x00040000;
2411 8ecc7913 j_mayer
    cpc->pci = 0x00000000;
2412 9c02f1a2 j_mayer
    cpc->er = 0x00000000;
2413 9c02f1a2 j_mayer
    cpc->fr = 0x00000000;
2414 9c02f1a2 j_mayer
    cpc->sr = 0x00000000;
2415 8ecc7913 j_mayer
    ppc405ep_compute_clocks(cpc);
2416 8ecc7913 j_mayer
}
2417 8ecc7913 j_mayer
2418 8ecc7913 j_mayer
/* XXX: sysclk should be between 25 and 100 MHz */
2419 e2684c0b Andreas Färber
static void ppc405ep_cpc_init (CPUPPCState *env, clk_setup_t clk_setup[8],
2420 8ecc7913 j_mayer
                               uint32_t sysclk)
2421 8ecc7913 j_mayer
{
2422 c227f099 Anthony Liguori
    ppc405ep_cpc_t *cpc;
2423 8ecc7913 j_mayer
2424 7267c094 Anthony Liguori
    cpc = g_malloc0(sizeof(ppc405ep_cpc_t));
2425 487414f1 aliguori
    memcpy(cpc->clk_setup, clk_setup,
2426 c227f099 Anthony Liguori
           PPC405EP_CLK_NB * sizeof(clk_setup_t));
2427 487414f1 aliguori
    cpc->jtagid = 0x20267049;
2428 487414f1 aliguori
    cpc->sysclk = sysclk;
2429 a08d4367 Jan Kiszka
    qemu_register_reset(&ppc405ep_cpc_reset, cpc);
2430 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_BOOT, cpc,
2431 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2432 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_EPCTL, cpc,
2433 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2434 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_PLLMR0, cpc,
2435 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2436 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_PLLMR1, cpc,
2437 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2438 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_UCR, cpc,
2439 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2440 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_SRR, cpc,
2441 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2442 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_JTAGID, cpc,
2443 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2444 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_PCI, cpc,
2445 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2446 9c02f1a2 j_mayer
#if 0
2447 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_ER, cpc,
2448 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2449 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_FR, cpc,
2450 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2451 487414f1 aliguori
    ppc_dcr_register(env, PPC405EP_CPC0_SR, cpc,
2452 487414f1 aliguori
                     &dcr_read_epcpc, &dcr_write_epcpc);
2453 9c02f1a2 j_mayer
#endif
2454 8ecc7913 j_mayer
}
2455 8ecc7913 j_mayer
2456 e2684c0b Andreas Färber
CPUPPCState *ppc405ep_init(MemoryRegion *address_space_mem,
2457 52ce55a1 Richard Henderson
                        MemoryRegion ram_memories[2],
2458 a8170e5e Avi Kivity
                        hwaddr ram_bases[2],
2459 a8170e5e Avi Kivity
                        hwaddr ram_sizes[2],
2460 52ce55a1 Richard Henderson
                        uint32_t sysclk, qemu_irq **picp,
2461 52ce55a1 Richard Henderson
                        int do_init)
2462 8ecc7913 j_mayer
{
2463 c227f099 Anthony Liguori
    clk_setup_t clk_setup[PPC405EP_CLK_NB], tlb_clk_setup;
2464 9c02f1a2 j_mayer
    qemu_irq dma_irqs[4], gpt_irqs[5], mal_irqs[4];
2465 2f9859fb Andreas Färber
    PowerPCCPU *cpu;
2466 e2684c0b Andreas Färber
    CPUPPCState *env;
2467 8ecc7913 j_mayer
    qemu_irq *pic, *irqs;
2468 8ecc7913 j_mayer
2469 8ecc7913 j_mayer
    memset(clk_setup, 0, sizeof(clk_setup));
2470 8ecc7913 j_mayer
    /* init CPUs */
2471 2f9859fb Andreas Färber
    cpu = ppc4xx_init("405ep", &clk_setup[PPC405EP_CPU_CLK],
2472 9c02f1a2 j_mayer
                      &tlb_clk_setup, sysclk);
2473 2f9859fb Andreas Färber
    env = &cpu->env;
2474 9c02f1a2 j_mayer
    clk_setup[PPC405EP_CPU_CLK].cb = tlb_clk_setup.cb;
2475 9c02f1a2 j_mayer
    clk_setup[PPC405EP_CPU_CLK].opaque = tlb_clk_setup.opaque;
2476 8ecc7913 j_mayer
    /* Internal devices init */
2477 8ecc7913 j_mayer
    /* Memory mapped devices registers */
2478 8ecc7913 j_mayer
    /* PLB arbitrer */
2479 8ecc7913 j_mayer
    ppc4xx_plb_init(env);
2480 8ecc7913 j_mayer
    /* PLB to OPB bridge */
2481 8ecc7913 j_mayer
    ppc4xx_pob_init(env);
2482 8ecc7913 j_mayer
    /* OBP arbitrer */
2483 802670e6 Blue Swirl
    ppc4xx_opba_init(0xef600600);
2484 d715ea96 Alexander Graf
    /* Initialize timers */
2485 a34a92b9 Andreas Färber
    ppc_booke_timers_init(cpu, sysclk, 0);
2486 8ecc7913 j_mayer
    /* Universal interrupt controller */
2487 7267c094 Anthony Liguori
    irqs = g_malloc0(sizeof(qemu_irq) * PPCUIC_OUTPUT_NB);
2488 8ecc7913 j_mayer
    irqs[PPCUIC_OUTPUT_INT] =
2489 b48d7d69 j_mayer
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
2490 8ecc7913 j_mayer
    irqs[PPCUIC_OUTPUT_CINT] =
2491 b48d7d69 j_mayer
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
2492 8ecc7913 j_mayer
    pic = ppcuic_init(env, irqs, 0x0C0, 0, 1);
2493 8ecc7913 j_mayer
    *picp = pic;
2494 8ecc7913 j_mayer
    /* SDRAM controller */
2495 923e5e33 aurel32
        /* XXX 405EP has no ECC interrupt */
2496 b6dcbe08 Avi Kivity
    ppc4xx_sdram_init(env, pic[17], 2, ram_memories,
2497 b6dcbe08 Avi Kivity
                      ram_bases, ram_sizes, do_init);
2498 8ecc7913 j_mayer
    /* External bus controller */
2499 8ecc7913 j_mayer
    ppc405_ebc_init(env);
2500 8ecc7913 j_mayer
    /* DMA controller */
2501 923e5e33 aurel32
    dma_irqs[0] = pic[5];
2502 923e5e33 aurel32
    dma_irqs[1] = pic[6];
2503 923e5e33 aurel32
    dma_irqs[2] = pic[7];
2504 923e5e33 aurel32
    dma_irqs[3] = pic[8];
2505 8ecc7913 j_mayer
    ppc405_dma_init(env, dma_irqs);
2506 8ecc7913 j_mayer
    /* IIC controller */
2507 802670e6 Blue Swirl
    ppc405_i2c_init(0xef600500, pic[2]);
2508 8ecc7913 j_mayer
    /* GPIO */
2509 802670e6 Blue Swirl
    ppc405_gpio_init(0xef600700);
2510 8ecc7913 j_mayer
    /* Serial ports */
2511 8ecc7913 j_mayer
    if (serial_hds[0] != NULL) {
2512 52ce55a1 Richard Henderson
        serial_mm_init(address_space_mem, 0xef600300, 0, pic[0],
2513 39186d8a Richard Henderson
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[0],
2514 39186d8a Richard Henderson
                       DEVICE_BIG_ENDIAN);
2515 8ecc7913 j_mayer
    }
2516 8ecc7913 j_mayer
    if (serial_hds[1] != NULL) {
2517 52ce55a1 Richard Henderson
        serial_mm_init(address_space_mem, 0xef600400, 0, pic[1],
2518 39186d8a Richard Henderson
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[1],
2519 39186d8a Richard Henderson
                       DEVICE_BIG_ENDIAN);
2520 8ecc7913 j_mayer
    }
2521 8ecc7913 j_mayer
    /* OCM */
2522 5c130f65 pbrook
    ppc405_ocm_init(env);
2523 9c02f1a2 j_mayer
    /* GPT */
2524 923e5e33 aurel32
    gpt_irqs[0] = pic[19];
2525 923e5e33 aurel32
    gpt_irqs[1] = pic[20];
2526 923e5e33 aurel32
    gpt_irqs[2] = pic[21];
2527 923e5e33 aurel32
    gpt_irqs[3] = pic[22];
2528 923e5e33 aurel32
    gpt_irqs[4] = pic[23];
2529 802670e6 Blue Swirl
    ppc4xx_gpt_init(0xef600000, gpt_irqs);
2530 8ecc7913 j_mayer
    /* PCI */
2531 923e5e33 aurel32
    /* Uses pic[3], pic[16], pic[18] */
2532 9c02f1a2 j_mayer
    /* MAL */
2533 923e5e33 aurel32
    mal_irqs[0] = pic[11];
2534 923e5e33 aurel32
    mal_irqs[1] = pic[12];
2535 923e5e33 aurel32
    mal_irqs[2] = pic[13];
2536 923e5e33 aurel32
    mal_irqs[3] = pic[14];
2537 9c02f1a2 j_mayer
    ppc405_mal_init(env, mal_irqs);
2538 9c02f1a2 j_mayer
    /* Ethernet */
2539 923e5e33 aurel32
    /* Uses pic[9], pic[15], pic[17] */
2540 8ecc7913 j_mayer
    /* CPU control */
2541 8ecc7913 j_mayer
    ppc405ep_cpc_init(env, clk_setup, sysclk);
2542 8ecc7913 j_mayer
2543 8ecc7913 j_mayer
    return env;
2544 8ecc7913 j_mayer
}