ppc405_uc.c 65.3 KB
Newer Older
1 2
/*
 * QEMU PowerPC 405 embedded processors emulation
3
 *
4
 * Copyright (c) 2007 Jocelyn Mayer
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
24 25
#include "hw.h"
#include "ppc.h"
26
#include "ppc405.h"
P
pbrook 已提交
27 28 29
#include "pc.h"
#include "qemu-timer.h"
#include "sysemu.h"
B
blueswir1 已提交
30
#include "qemu-log.h"
31 32 33 34 35 36

#define DEBUG_OPBA
#define DEBUG_SDRAM
#define DEBUG_GPIO
#define DEBUG_SERIAL
#define DEBUG_OCM
37 38 39
//#define DEBUG_I2C
#define DEBUG_GPT
#define DEBUG_MAL
40
#define DEBUG_CLOCKS
41
//#define DEBUG_CLOCKS_LL
42

A
Anthony Liguori 已提交
43
ram_addr_t ppc405_set_bootinfo (CPUState *env, ppc4xx_bd_info_t *bd,
44
                                uint32_t flags)
45
{
A
Anthony Liguori 已提交
46
    ram_addr_t bdloc;
47 48 49
    int i, n;

    /* We put the bd structure at the top of memory */
50
    if (bd->bi_memsize >= 0x01000000UL)
A
Anthony Liguori 已提交
51
        bdloc = 0x01000000UL - sizeof(struct ppc4xx_bd_info_t);
52
    else
A
Anthony Liguori 已提交
53
        bdloc = bd->bi_memsize - sizeof(struct ppc4xx_bd_info_t);
54 55 56 57 58 59 60 61 62 63
    stl_be_phys(bdloc + 0x00, bd->bi_memstart);
    stl_be_phys(bdloc + 0x04, bd->bi_memsize);
    stl_be_phys(bdloc + 0x08, bd->bi_flashstart);
    stl_be_phys(bdloc + 0x0C, bd->bi_flashsize);
    stl_be_phys(bdloc + 0x10, bd->bi_flashoffset);
    stl_be_phys(bdloc + 0x14, bd->bi_sramstart);
    stl_be_phys(bdloc + 0x18, bd->bi_sramsize);
    stl_be_phys(bdloc + 0x1C, bd->bi_bootflags);
    stl_be_phys(bdloc + 0x20, bd->bi_ipaddr);
    for (i = 0; i < 6; i++) {
P
pbrook 已提交
64
        stb_phys(bdloc + 0x24 + i, bd->bi_enetaddr[i]);
65 66 67 68 69 70
    }
    stw_be_phys(bdloc + 0x2A, bd->bi_ethspeed);
    stl_be_phys(bdloc + 0x2C, bd->bi_intfreq);
    stl_be_phys(bdloc + 0x30, bd->bi_busfreq);
    stl_be_phys(bdloc + 0x34, bd->bi_baudrate);
    for (i = 0; i < 4; i++) {
P
pbrook 已提交
71
        stb_phys(bdloc + 0x38 + i, bd->bi_s_version[i]);
72
    }
B
Blue Swirl 已提交
73 74 75
    for (i = 0; i < 32; i++) {
        stb_phys(bdloc + 0x3C + i, bd->bi_r_version[i]);
    }
76 77 78
    stl_be_phys(bdloc + 0x5C, bd->bi_plb_busfreq);
    stl_be_phys(bdloc + 0x60, bd->bi_pci_busfreq);
    for (i = 0; i < 6; i++) {
P
pbrook 已提交
79
        stb_phys(bdloc + 0x64 + i, bd->bi_pci_enetaddr[i]);
80
    }
81
    n = 0x6A;
82
    if (flags & 0x00000001) {
83
        for (i = 0; i < 6; i++)
P
pbrook 已提交
84
            stb_phys(bdloc + n++, bd->bi_pci_enetaddr2[i]);
85
    }
86
    stl_be_phys(bdloc + n, bd->bi_opbfreq);
87 88
    n += 4;
    for (i = 0; i < 2; i++) {
89
        stl_be_phys(bdloc + n, bd->bi_iic_fast[i]);
90 91 92 93 94 95
        n += 4;
    }

    return bdloc;
}

96 97 98 99 100 101 102 103 104 105 106
/*****************************************************************************/
/* Shared peripherals */

/*****************************************************************************/
/* Peripheral local bus arbitrer */
enum {
    PLB0_BESR = 0x084,
    PLB0_BEAR = 0x086,
    PLB0_ACR  = 0x087,
};

A
Anthony Liguori 已提交
107 108
typedef struct ppc4xx_plb_t ppc4xx_plb_t;
struct ppc4xx_plb_t {
109 110 111 112 113
    uint32_t acr;
    uint32_t bear;
    uint32_t besr;
};

A
Alexander Graf 已提交
114
static uint32_t dcr_read_plb (void *opaque, int dcrn)
115
{
A
Anthony Liguori 已提交
116
    ppc4xx_plb_t *plb;
A
Alexander Graf 已提交
117
    uint32_t ret;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    plb = opaque;
    switch (dcrn) {
    case PLB0_ACR:
        ret = plb->acr;
        break;
    case PLB0_BEAR:
        ret = plb->bear;
        break;
    case PLB0_BESR:
        ret = plb->besr;
        break;
    default:
        /* Avoid gcc warning */
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
139
static void dcr_write_plb (void *opaque, int dcrn, uint32_t val)
140
{
A
Anthony Liguori 已提交
141
    ppc4xx_plb_t *plb;
142 143 144 145

    plb = opaque;
    switch (dcrn) {
    case PLB0_ACR:
146 147 148 149
        /* We don't care about the actual parameters written as
         * we don't manage any priorities on the bus
         */
        plb->acr = val & 0xF8000000;
150 151 152 153 154 155 156 157 158 159 160 161 162
        break;
    case PLB0_BEAR:
        /* Read only */
        break;
    case PLB0_BESR:
        /* Write-clear */
        plb->besr &= ~val;
        break;
    }
}

static void ppc4xx_plb_reset (void *opaque)
{
A
Anthony Liguori 已提交
163
    ppc4xx_plb_t *plb;
164 165 166 167 168 169 170

    plb = opaque;
    plb->acr = 0x00000000;
    plb->bear = 0x00000000;
    plb->besr = 0x00000000;
}

B
Blue Swirl 已提交
171
static void ppc4xx_plb_init(CPUState *env)
172
{
A
Anthony Liguori 已提交
173
    ppc4xx_plb_t *plb;
174

175
    plb = g_malloc0(sizeof(ppc4xx_plb_t));
176 177 178
    ppc_dcr_register(env, PLB0_ACR, plb, &dcr_read_plb, &dcr_write_plb);
    ppc_dcr_register(env, PLB0_BEAR, plb, &dcr_read_plb, &dcr_write_plb);
    ppc_dcr_register(env, PLB0_BESR, plb, &dcr_read_plb, &dcr_write_plb);
179
    qemu_register_reset(ppc4xx_plb_reset, plb);
180 181 182 183 184 185 186 187 188 189
}

/*****************************************************************************/
/* PLB to OPB bridge */
enum {
    POB0_BESR0 = 0x0A0,
    POB0_BESR1 = 0x0A2,
    POB0_BEAR  = 0x0A4,
};

A
Anthony Liguori 已提交
190 191
typedef struct ppc4xx_pob_t ppc4xx_pob_t;
struct ppc4xx_pob_t {
192 193 194 195
    uint32_t bear;
    uint32_t besr[2];
};

A
Alexander Graf 已提交
196
static uint32_t dcr_read_pob (void *opaque, int dcrn)
197
{
A
Anthony Liguori 已提交
198
    ppc4xx_pob_t *pob;
A
Alexander Graf 已提交
199
    uint32_t ret;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    pob = opaque;
    switch (dcrn) {
    case POB0_BEAR:
        ret = pob->bear;
        break;
    case POB0_BESR0:
    case POB0_BESR1:
        ret = pob->besr[dcrn - POB0_BESR0];
        break;
    default:
        /* Avoid gcc warning */
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
219
static void dcr_write_pob (void *opaque, int dcrn, uint32_t val)
220
{
A
Anthony Liguori 已提交
221
    ppc4xx_pob_t *pob;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    pob = opaque;
    switch (dcrn) {
    case POB0_BEAR:
        /* Read only */
        break;
    case POB0_BESR0:
    case POB0_BESR1:
        /* Write-clear */
        pob->besr[dcrn - POB0_BESR0] &= ~val;
        break;
    }
}

static void ppc4xx_pob_reset (void *opaque)
{
A
Anthony Liguori 已提交
238
    ppc4xx_pob_t *pob;
239 240 241 242 243 244 245 246

    pob = opaque;
    /* No error */
    pob->bear = 0x00000000;
    pob->besr[0] = 0x0000000;
    pob->besr[1] = 0x0000000;
}

B
Blue Swirl 已提交
247
static void ppc4xx_pob_init(CPUState *env)
248
{
A
Anthony Liguori 已提交
249
    ppc4xx_pob_t *pob;
250

251
    pob = g_malloc0(sizeof(ppc4xx_pob_t));
252 253 254
    ppc_dcr_register(env, POB0_BEAR, pob, &dcr_read_pob, &dcr_write_pob);
    ppc_dcr_register(env, POB0_BESR0, pob, &dcr_read_pob, &dcr_write_pob);
    ppc_dcr_register(env, POB0_BESR1, pob, &dcr_read_pob, &dcr_write_pob);
255
    qemu_register_reset(ppc4xx_pob_reset, pob);
256 257 258 259
}

/*****************************************************************************/
/* OPB arbitrer */
A
Anthony Liguori 已提交
260 261
typedef struct ppc4xx_opba_t ppc4xx_opba_t;
struct ppc4xx_opba_t {
262 263 264 265
    uint8_t cr;
    uint8_t pr;
};

A
Anthony Liguori 已提交
266
static uint32_t opba_readb (void *opaque, target_phys_addr_t addr)
267
{
A
Anthony Liguori 已提交
268
    ppc4xx_opba_t *opba;
269 270 271
    uint32_t ret;

#ifdef DEBUG_OPBA
272
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
273 274
#endif
    opba = opaque;
B
Blue Swirl 已提交
275
    switch (addr) {
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    case 0x00:
        ret = opba->cr;
        break;
    case 0x01:
        ret = opba->pr;
        break;
    default:
        ret = 0x00;
        break;
    }

    return ret;
}

static void opba_writeb (void *opaque,
A
Anthony Liguori 已提交
291
                         target_phys_addr_t addr, uint32_t value)
292
{
A
Anthony Liguori 已提交
293
    ppc4xx_opba_t *opba;
294 295

#ifdef DEBUG_OPBA
296 297
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
298 299
#endif
    opba = opaque;
B
Blue Swirl 已提交
300
    switch (addr) {
301 302 303 304 305 306 307 308 309 310 311
    case 0x00:
        opba->cr = value & 0xF8;
        break;
    case 0x01:
        opba->pr = value & 0xFF;
        break;
    default:
        break;
    }
}

A
Anthony Liguori 已提交
312
static uint32_t opba_readw (void *opaque, target_phys_addr_t addr)
313 314 315 316
{
    uint32_t ret;

#ifdef DEBUG_OPBA
317
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
318 319 320 321 322 323 324 325
#endif
    ret = opba_readb(opaque, addr) << 8;
    ret |= opba_readb(opaque, addr + 1);

    return ret;
}

static void opba_writew (void *opaque,
A
Anthony Liguori 已提交
326
                         target_phys_addr_t addr, uint32_t value)
327 328
{
#ifdef DEBUG_OPBA
329 330
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
331 332 333 334 335
#endif
    opba_writeb(opaque, addr, value >> 8);
    opba_writeb(opaque, addr + 1, value);
}

A
Anthony Liguori 已提交
336
static uint32_t opba_readl (void *opaque, target_phys_addr_t addr)
337 338 339 340
{
    uint32_t ret;

#ifdef DEBUG_OPBA
341
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
342 343 344 345 346 347 348 349
#endif
    ret = opba_readb(opaque, addr) << 24;
    ret |= opba_readb(opaque, addr + 1) << 16;

    return ret;
}

static void opba_writel (void *opaque,
A
Anthony Liguori 已提交
350
                         target_phys_addr_t addr, uint32_t value)
351 352
{
#ifdef DEBUG_OPBA
353 354
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
355 356 357 358 359
#endif
    opba_writeb(opaque, addr, value >> 24);
    opba_writeb(opaque, addr + 1, value >> 16);
}

360
static CPUReadMemoryFunc * const opba_read[] = {
361 362 363 364 365
    &opba_readb,
    &opba_readw,
    &opba_readl,
};

366
static CPUWriteMemoryFunc * const opba_write[] = {
367 368 369 370 371 372 373
    &opba_writeb,
    &opba_writew,
    &opba_writel,
};

static void ppc4xx_opba_reset (void *opaque)
{
A
Anthony Liguori 已提交
374
    ppc4xx_opba_t *opba;
375 376 377 378 379 380

    opba = opaque;
    opba->cr = 0x00; /* No dynamic priorities - park disabled */
    opba->pr = 0x11;
}

A
Anthony Liguori 已提交
381
static void ppc4xx_opba_init(target_phys_addr_t base)
382
{
A
Anthony Liguori 已提交
383
    ppc4xx_opba_t *opba;
B
Blue Swirl 已提交
384
    int io;
385

386
    opba = g_malloc0(sizeof(ppc4xx_opba_t));
387
#ifdef DEBUG_OPBA
388
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
389
#endif
390 391
    io = cpu_register_io_memory(opba_read, opba_write, opba,
                                DEVICE_NATIVE_ENDIAN);
B
Blue Swirl 已提交
392 393
    cpu_register_physical_memory(base, 0x002, io);
    qemu_register_reset(ppc4xx_opba_reset, opba);
394 395 396 397 398 399 400 401
}

/*****************************************************************************/
/* Code decompression controller */
/* XXX: TODO */

/*****************************************************************************/
/* Peripheral controller */
A
Anthony Liguori 已提交
402 403
typedef struct ppc4xx_ebc_t ppc4xx_ebc_t;
struct ppc4xx_ebc_t {
404 405 406 407 408 409 410 411 412 413 414 415 416 417
    uint32_t addr;
    uint32_t bcr[8];
    uint32_t bap[8];
    uint32_t bear;
    uint32_t besr0;
    uint32_t besr1;
    uint32_t cfg;
};

enum {
    EBC0_CFGADDR = 0x012,
    EBC0_CFGDATA = 0x013,
};

A
Alexander Graf 已提交
418
static uint32_t dcr_read_ebc (void *opaque, int dcrn)
419
{
A
Anthony Liguori 已提交
420
    ppc4xx_ebc_t *ebc;
A
Alexander Graf 已提交
421
    uint32_t ret;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    ebc = opaque;
    switch (dcrn) {
    case EBC0_CFGADDR:
        ret = ebc->addr;
        break;
    case EBC0_CFGDATA:
        switch (ebc->addr) {
        case 0x00: /* B0CR */
            ret = ebc->bcr[0];
            break;
        case 0x01: /* B1CR */
            ret = ebc->bcr[1];
            break;
        case 0x02: /* B2CR */
            ret = ebc->bcr[2];
            break;
        case 0x03: /* B3CR */
            ret = ebc->bcr[3];
            break;
        case 0x04: /* B4CR */
            ret = ebc->bcr[4];
            break;
        case 0x05: /* B5CR */
            ret = ebc->bcr[5];
            break;
        case 0x06: /* B6CR */
            ret = ebc->bcr[6];
            break;
        case 0x07: /* B7CR */
            ret = ebc->bcr[7];
            break;
        case 0x10: /* B0AP */
            ret = ebc->bap[0];
            break;
        case 0x11: /* B1AP */
            ret = ebc->bap[1];
            break;
        case 0x12: /* B2AP */
            ret = ebc->bap[2];
            break;
        case 0x13: /* B3AP */
            ret = ebc->bap[3];
            break;
        case 0x14: /* B4AP */
            ret = ebc->bap[4];
            break;
        case 0x15: /* B5AP */
            ret = ebc->bap[5];
            break;
        case 0x16: /* B6AP */
            ret = ebc->bap[6];
            break;
        case 0x17: /* B7AP */
            ret = ebc->bap[7];
            break;
        case 0x20: /* BEAR */
            ret = ebc->bear;
            break;
        case 0x21: /* BESR0 */
            ret = ebc->besr0;
            break;
        case 0x22: /* BESR1 */
            ret = ebc->besr1;
            break;
        case 0x23: /* CFG */
            ret = ebc->cfg;
            break;
        default:
            ret = 0x00000000;
            break;
        }
494
        break;
495 496 497 498 499 500 501 502
    default:
        ret = 0x00000000;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
503
static void dcr_write_ebc (void *opaque, int dcrn, uint32_t val)
504
{
A
Anthony Liguori 已提交
505
    ppc4xx_ebc_t *ebc;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

    ebc = opaque;
    switch (dcrn) {
    case EBC0_CFGADDR:
        ebc->addr = val;
        break;
    case EBC0_CFGDATA:
        switch (ebc->addr) {
        case 0x00: /* B0CR */
            break;
        case 0x01: /* B1CR */
            break;
        case 0x02: /* B2CR */
            break;
        case 0x03: /* B3CR */
            break;
        case 0x04: /* B4CR */
            break;
        case 0x05: /* B5CR */
            break;
        case 0x06: /* B6CR */
            break;
        case 0x07: /* B7CR */
            break;
        case 0x10: /* B0AP */
            break;
        case 0x11: /* B1AP */
            break;
        case 0x12: /* B2AP */
            break;
        case 0x13: /* B3AP */
            break;
        case 0x14: /* B4AP */
            break;
        case 0x15: /* B5AP */
            break;
        case 0x16: /* B6AP */
            break;
        case 0x17: /* B7AP */
            break;
        case 0x20: /* BEAR */
            break;
        case 0x21: /* BESR0 */
            break;
        case 0x22: /* BESR1 */
            break;
        case 0x23: /* CFG */
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
}

static void ebc_reset (void *opaque)
{
A
Anthony Liguori 已提交
565
    ppc4xx_ebc_t *ebc;
566 567 568 569 570 571 572 573 574 575 576 577
    int i;

    ebc = opaque;
    ebc->addr = 0x00000000;
    ebc->bap[0] = 0x7F8FFE80;
    ebc->bcr[0] = 0xFFE28000;
    for (i = 0; i < 8; i++) {
        ebc->bap[i] = 0x00000000;
        ebc->bcr[i] = 0x00000000;
    }
    ebc->besr0 = 0x00000000;
    ebc->besr1 = 0x00000000;
578
    ebc->cfg = 0x80400000;
579 580
}

B
Blue Swirl 已提交
581
static void ppc405_ebc_init(CPUState *env)
582
{
A
Anthony Liguori 已提交
583
    ppc4xx_ebc_t *ebc;
584

585
    ebc = g_malloc0(sizeof(ppc4xx_ebc_t));
586
    qemu_register_reset(&ebc_reset, ebc);
587 588 589 590
    ppc_dcr_register(env, EBC0_CFGADDR,
                     ebc, &dcr_read_ebc, &dcr_write_ebc);
    ppc_dcr_register(env, EBC0_CFGDATA,
                     ebc, &dcr_read_ebc, &dcr_write_ebc);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
}

/*****************************************************************************/
/* DMA controller */
enum {
    DMA0_CR0 = 0x100,
    DMA0_CT0 = 0x101,
    DMA0_DA0 = 0x102,
    DMA0_SA0 = 0x103,
    DMA0_SG0 = 0x104,
    DMA0_CR1 = 0x108,
    DMA0_CT1 = 0x109,
    DMA0_DA1 = 0x10A,
    DMA0_SA1 = 0x10B,
    DMA0_SG1 = 0x10C,
    DMA0_CR2 = 0x110,
    DMA0_CT2 = 0x111,
    DMA0_DA2 = 0x112,
    DMA0_SA2 = 0x113,
    DMA0_SG2 = 0x114,
    DMA0_CR3 = 0x118,
    DMA0_CT3 = 0x119,
    DMA0_DA3 = 0x11A,
    DMA0_SA3 = 0x11B,
    DMA0_SG3 = 0x11C,
    DMA0_SR  = 0x120,
    DMA0_SGC = 0x123,
    DMA0_SLP = 0x125,
    DMA0_POL = 0x126,
};

A
Anthony Liguori 已提交
622 623
typedef struct ppc405_dma_t ppc405_dma_t;
struct ppc405_dma_t {
624 625 626 627 628 629 630 631 632 633 634 635
    qemu_irq irqs[4];
    uint32_t cr[4];
    uint32_t ct[4];
    uint32_t da[4];
    uint32_t sa[4];
    uint32_t sg[4];
    uint32_t sr;
    uint32_t sgc;
    uint32_t slp;
    uint32_t pol;
};

A
Alexander Graf 已提交
636
static uint32_t dcr_read_dma (void *opaque, int dcrn)
637 638 639 640
{
    return 0;
}

A
Alexander Graf 已提交
641
static void dcr_write_dma (void *opaque, int dcrn, uint32_t val)
642 643 644 645 646
{
}

static void ppc405_dma_reset (void *opaque)
{
A
Anthony Liguori 已提交
647
    ppc405_dma_t *dma;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    int i;

    dma = opaque;
    for (i = 0; i < 4; i++) {
        dma->cr[i] = 0x00000000;
        dma->ct[i] = 0x00000000;
        dma->da[i] = 0x00000000;
        dma->sa[i] = 0x00000000;
        dma->sg[i] = 0x00000000;
    }
    dma->sr = 0x00000000;
    dma->sgc = 0x00000000;
    dma->slp = 0x7C000000;
    dma->pol = 0x00000000;
}

B
Blue Swirl 已提交
664
static void ppc405_dma_init(CPUState *env, qemu_irq irqs[4])
665
{
A
Anthony Liguori 已提交
666
    ppc405_dma_t *dma;
667

668
    dma = g_malloc0(sizeof(ppc405_dma_t));
669
    memcpy(dma->irqs, irqs, 4 * sizeof(qemu_irq));
670
    qemu_register_reset(&ppc405_dma_reset, dma);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    ppc_dcr_register(env, DMA0_CR0,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CT0,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_DA0,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SA0,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SG0,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CR1,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CT1,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_DA1,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SA1,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SG1,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CR2,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CT2,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_DA2,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SA2,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SG2,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CR3,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_CT3,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_DA3,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SA3,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SG3,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SR,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SGC,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_SLP,
                     dma, &dcr_read_dma, &dcr_write_dma);
    ppc_dcr_register(env, DMA0_POL,
                     dma, &dcr_read_dma, &dcr_write_dma);
719 720 721 722
}

/*****************************************************************************/
/* GPIO */
A
Anthony Liguori 已提交
723 724
typedef struct ppc405_gpio_t ppc405_gpio_t;
struct ppc405_gpio_t {
725 726 727 728 729 730 731 732 733 734 735 736 737
    uint32_t or;
    uint32_t tcr;
    uint32_t osrh;
    uint32_t osrl;
    uint32_t tsrh;
    uint32_t tsrl;
    uint32_t odr;
    uint32_t ir;
    uint32_t rr1;
    uint32_t isr1h;
    uint32_t isr1l;
};

A
Anthony Liguori 已提交
738
static uint32_t ppc405_gpio_readb (void *opaque, target_phys_addr_t addr)
739 740
{
#ifdef DEBUG_GPIO
741
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
742 743 744 745 746 747
#endif

    return 0;
}

static void ppc405_gpio_writeb (void *opaque,
A
Anthony Liguori 已提交
748
                                target_phys_addr_t addr, uint32_t value)
749 750
{
#ifdef DEBUG_GPIO
751 752
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
753 754 755
#endif
}

A
Anthony Liguori 已提交
756
static uint32_t ppc405_gpio_readw (void *opaque, target_phys_addr_t addr)
757 758
{
#ifdef DEBUG_GPIO
759
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
760 761 762 763 764 765
#endif

    return 0;
}

static void ppc405_gpio_writew (void *opaque,
A
Anthony Liguori 已提交
766
                                target_phys_addr_t addr, uint32_t value)
767 768
{
#ifdef DEBUG_GPIO
769 770
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
771 772 773
#endif
}

A
Anthony Liguori 已提交
774
static uint32_t ppc405_gpio_readl (void *opaque, target_phys_addr_t addr)
775 776
{
#ifdef DEBUG_GPIO
777
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
778 779 780 781 782 783
#endif

    return 0;
}

static void ppc405_gpio_writel (void *opaque,
A
Anthony Liguori 已提交
784
                                target_phys_addr_t addr, uint32_t value)
785 786
{
#ifdef DEBUG_GPIO
787 788
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
789 790 791
#endif
}

792
static CPUReadMemoryFunc * const ppc405_gpio_read[] = {
793 794 795 796 797
    &ppc405_gpio_readb,
    &ppc405_gpio_readw,
    &ppc405_gpio_readl,
};

798
static CPUWriteMemoryFunc * const ppc405_gpio_write[] = {
799 800 801 802 803 804 805 806 807
    &ppc405_gpio_writeb,
    &ppc405_gpio_writew,
    &ppc405_gpio_writel,
};

static void ppc405_gpio_reset (void *opaque)
{
}

A
Anthony Liguori 已提交
808
static void ppc405_gpio_init(target_phys_addr_t base)
809
{
A
Anthony Liguori 已提交
810
    ppc405_gpio_t *gpio;
B
Blue Swirl 已提交
811
    int io;
812

813
    gpio = g_malloc0(sizeof(ppc405_gpio_t));
814
#ifdef DEBUG_GPIO
815
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
816
#endif
817 818
    io = cpu_register_io_memory(ppc405_gpio_read, ppc405_gpio_write, gpio,
                                DEVICE_NATIVE_ENDIAN);
B
Blue Swirl 已提交
819 820
    cpu_register_physical_memory(base, 0x038, io);
    qemu_register_reset(&ppc405_gpio_reset, gpio);
821 822 823 824 825 826 827 828 829 830 831
}

/*****************************************************************************/
/* On Chip Memory */
enum {
    OCM0_ISARC   = 0x018,
    OCM0_ISACNTL = 0x019,
    OCM0_DSARC   = 0x01A,
    OCM0_DSACNTL = 0x01B,
};

A
Anthony Liguori 已提交
832 833
typedef struct ppc405_ocm_t ppc405_ocm_t;
struct ppc405_ocm_t {
834 835 836 837 838 839 840
    target_ulong offset;
    uint32_t isarc;
    uint32_t isacntl;
    uint32_t dsarc;
    uint32_t dsacntl;
};

A
Anthony Liguori 已提交
841
static void ocm_update_mappings (ppc405_ocm_t *ocm,
842 843 844 845
                                 uint32_t isarc, uint32_t isacntl,
                                 uint32_t dsarc, uint32_t dsacntl)
{
#ifdef DEBUG_OCM
846 847 848
    printf("OCM update ISA %08" PRIx32 " %08" PRIx32 " (%08" PRIx32
           " %08" PRIx32 ") DSA %08" PRIx32 " %08" PRIx32
           " (%08" PRIx32 " %08" PRIx32 ")\n",
849 850 851 852 853 854 855
           isarc, isacntl, dsarc, dsacntl,
           ocm->isarc, ocm->isacntl, ocm->dsarc, ocm->dsacntl);
#endif
    if (ocm->isarc != isarc ||
        (ocm->isacntl & 0x80000000) != (isacntl & 0x80000000)) {
        if (ocm->isacntl & 0x80000000) {
            /* Unmap previously assigned memory region */
856
            printf("OCM unmap ISA %08" PRIx32 "\n", ocm->isarc);
857 858 859 860 861 862
            cpu_register_physical_memory(ocm->isarc, 0x04000000,
                                         IO_MEM_UNASSIGNED);
        }
        if (isacntl & 0x80000000) {
            /* Map new instruction memory region */
#ifdef DEBUG_OCM
863
            printf("OCM map ISA %08" PRIx32 "\n", isarc);
864 865 866 867 868 869 870 871 872 873 874 875
#endif
            cpu_register_physical_memory(isarc, 0x04000000,
                                         ocm->offset | IO_MEM_RAM);
        }
    }
    if (ocm->dsarc != dsarc ||
        (ocm->dsacntl & 0x80000000) != (dsacntl & 0x80000000)) {
        if (ocm->dsacntl & 0x80000000) {
            /* Beware not to unmap the region we just mapped */
            if (!(isacntl & 0x80000000) || ocm->dsarc != isarc) {
                /* Unmap previously assigned memory region */
#ifdef DEBUG_OCM
876
                printf("OCM unmap DSA %08" PRIx32 "\n", ocm->dsarc);
877 878 879 880 881 882 883 884 885 886
#endif
                cpu_register_physical_memory(ocm->dsarc, 0x04000000,
                                             IO_MEM_UNASSIGNED);
            }
        }
        if (dsacntl & 0x80000000) {
            /* Beware not to remap the region we just mapped */
            if (!(isacntl & 0x80000000) || dsarc != isarc) {
                /* Map new data memory region */
#ifdef DEBUG_OCM
887
                printf("OCM map DSA %08" PRIx32 "\n", dsarc);
888 889 890 891 892 893 894 895
#endif
                cpu_register_physical_memory(dsarc, 0x04000000,
                                             ocm->offset | IO_MEM_RAM);
            }
        }
    }
}

A
Alexander Graf 已提交
896
static uint32_t dcr_read_ocm (void *opaque, int dcrn)
897
{
A
Anthony Liguori 已提交
898
    ppc405_ocm_t *ocm;
A
Alexander Graf 已提交
899
    uint32_t ret;
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

    ocm = opaque;
    switch (dcrn) {
    case OCM0_ISARC:
        ret = ocm->isarc;
        break;
    case OCM0_ISACNTL:
        ret = ocm->isacntl;
        break;
    case OCM0_DSARC:
        ret = ocm->dsarc;
        break;
    case OCM0_DSACNTL:
        ret = ocm->dsacntl;
        break;
    default:
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
923
static void dcr_write_ocm (void *opaque, int dcrn, uint32_t val)
924
{
A
Anthony Liguori 已提交
925
    ppc405_ocm_t *ocm;
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    uint32_t isarc, dsarc, isacntl, dsacntl;

    ocm = opaque;
    isarc = ocm->isarc;
    dsarc = ocm->dsarc;
    isacntl = ocm->isacntl;
    dsacntl = ocm->dsacntl;
    switch (dcrn) {
    case OCM0_ISARC:
        isarc = val & 0xFC000000;
        break;
    case OCM0_ISACNTL:
        isacntl = val & 0xC0000000;
        break;
    case OCM0_DSARC:
        isarc = val & 0xFC000000;
        break;
    case OCM0_DSACNTL:
        isacntl = val & 0xC0000000;
        break;
    }
    ocm_update_mappings(ocm, isarc, isacntl, dsarc, dsacntl);
    ocm->isarc = isarc;
    ocm->dsarc = dsarc;
    ocm->isacntl = isacntl;
    ocm->dsacntl = dsacntl;
}

static void ocm_reset (void *opaque)
{
A
Anthony Liguori 已提交
956
    ppc405_ocm_t *ocm;
957 958 959 960 961 962 963 964 965 966 967 968 969 970
    uint32_t isarc, dsarc, isacntl, dsacntl;

    ocm = opaque;
    isarc = 0x00000000;
    isacntl = 0x00000000;
    dsarc = 0x00000000;
    dsacntl = 0x00000000;
    ocm_update_mappings(ocm, isarc, isacntl, dsarc, dsacntl);
    ocm->isarc = isarc;
    ocm->dsarc = dsarc;
    ocm->isacntl = isacntl;
    ocm->dsacntl = dsacntl;
}

B
Blue Swirl 已提交
971
static void ppc405_ocm_init(CPUState *env)
972
{
A
Anthony Liguori 已提交
973
    ppc405_ocm_t *ocm;
974

975
    ocm = g_malloc0(sizeof(ppc405_ocm_t));
976
    ocm->offset = qemu_ram_alloc(NULL, "ppc405.ocm", 4096);
977
    qemu_register_reset(&ocm_reset, ocm);
978 979 980 981 982 983 984 985
    ppc_dcr_register(env, OCM0_ISARC,
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
    ppc_dcr_register(env, OCM0_ISACNTL,
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
    ppc_dcr_register(env, OCM0_DSARC,
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
    ppc_dcr_register(env, OCM0_DSACNTL,
                     ocm, &dcr_read_ocm, &dcr_write_ocm);
986 987 988 989
}

/*****************************************************************************/
/* I2C controller */
A
Anthony Liguori 已提交
990 991
typedef struct ppc4xx_i2c_t ppc4xx_i2c_t;
struct ppc4xx_i2c_t {
992
    qemu_irq irq;
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    uint8_t mdata;
    uint8_t lmadr;
    uint8_t hmadr;
    uint8_t cntl;
    uint8_t mdcntl;
    uint8_t sts;
    uint8_t extsts;
    uint8_t sdata;
    uint8_t lsadr;
    uint8_t hsadr;
    uint8_t clkdiv;
    uint8_t intrmsk;
    uint8_t xfrcnt;
    uint8_t xtcntlss;
    uint8_t directcntl;
};

A
Anthony Liguori 已提交
1010
static uint32_t ppc4xx_i2c_readb (void *opaque, target_phys_addr_t addr)
1011
{
A
Anthony Liguori 已提交
1012
    ppc4xx_i2c_t *i2c;
1013 1014 1015
    uint32_t ret;

#ifdef DEBUG_I2C
1016
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1017 1018
#endif
    i2c = opaque;
B
Blue Swirl 已提交
1019
    switch (addr) {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    case 0x00:
        //        i2c_readbyte(&i2c->mdata);
        ret = i2c->mdata;
        break;
    case 0x02:
        ret = i2c->sdata;
        break;
    case 0x04:
        ret = i2c->lmadr;
        break;
    case 0x05:
        ret = i2c->hmadr;
        break;
    case 0x06:
        ret = i2c->cntl;
        break;
    case 0x07:
        ret = i2c->mdcntl;
        break;
    case 0x08:
        ret = i2c->sts;
        break;
    case 0x09:
        ret = i2c->extsts;
        break;
    case 0x0A:
        ret = i2c->lsadr;
        break;
    case 0x0B:
        ret = i2c->hsadr;
        break;
    case 0x0C:
        ret = i2c->clkdiv;
        break;
    case 0x0D:
        ret = i2c->intrmsk;
        break;
    case 0x0E:
        ret = i2c->xfrcnt;
        break;
    case 0x0F:
        ret = i2c->xtcntlss;
        break;
    case 0x10:
        ret = i2c->directcntl;
        break;
    default:
        ret = 0x00;
        break;
    }
#ifdef DEBUG_I2C
1071
    printf("%s: addr " TARGET_FMT_plx " %02" PRIx32 "\n", __func__, addr, ret);
1072 1073 1074 1075 1076 1077
#endif

    return ret;
}

static void ppc4xx_i2c_writeb (void *opaque,
A
Anthony Liguori 已提交
1078
                               target_phys_addr_t addr, uint32_t value)
1079
{
A
Anthony Liguori 已提交
1080
    ppc4xx_i2c_t *i2c;
1081 1082

#ifdef DEBUG_I2C
1083 1084
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1085 1086
#endif
    i2c = opaque;
B
Blue Swirl 已提交
1087
    switch (addr) {
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    case 0x00:
        i2c->mdata = value;
        //        i2c_sendbyte(&i2c->mdata);
        break;
    case 0x02:
        i2c->sdata = value;
        break;
    case 0x04:
        i2c->lmadr = value;
        break;
    case 0x05:
        i2c->hmadr = value;
        break;
    case 0x06:
        i2c->cntl = value;
        break;
    case 0x07:
        i2c->mdcntl = value & 0xDF;
        break;
    case 0x08:
        i2c->sts &= ~(value & 0x0A);
        break;
    case 0x09:
        i2c->extsts &= ~(value & 0x8F);
        break;
    case 0x0A:
        i2c->lsadr = value;
        break;
    case 0x0B:
        i2c->hsadr = value;
        break;
    case 0x0C:
        i2c->clkdiv = value;
        break;
    case 0x0D:
        i2c->intrmsk = value;
        break;
    case 0x0E:
        i2c->xfrcnt = value & 0x77;
        break;
    case 0x0F:
        i2c->xtcntlss = value;
        break;
    case 0x10:
        i2c->directcntl = value & 0x7;
        break;
    }
}

A
Anthony Liguori 已提交
1137
static uint32_t ppc4xx_i2c_readw (void *opaque, target_phys_addr_t addr)
1138 1139 1140 1141
{
    uint32_t ret;

#ifdef DEBUG_I2C
1142
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1143 1144 1145 1146 1147 1148 1149 1150
#endif
    ret = ppc4xx_i2c_readb(opaque, addr) << 8;
    ret |= ppc4xx_i2c_readb(opaque, addr + 1);

    return ret;
}

static void ppc4xx_i2c_writew (void *opaque,
A
Anthony Liguori 已提交
1151
                               target_phys_addr_t addr, uint32_t value)
1152 1153
{
#ifdef DEBUG_I2C
1154 1155
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1156 1157 1158 1159 1160
#endif
    ppc4xx_i2c_writeb(opaque, addr, value >> 8);
    ppc4xx_i2c_writeb(opaque, addr + 1, value);
}

A
Anthony Liguori 已提交
1161
static uint32_t ppc4xx_i2c_readl (void *opaque, target_phys_addr_t addr)
1162 1163 1164 1165
{
    uint32_t ret;

#ifdef DEBUG_I2C
1166
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
#endif
    ret = ppc4xx_i2c_readb(opaque, addr) << 24;
    ret |= ppc4xx_i2c_readb(opaque, addr + 1) << 16;
    ret |= ppc4xx_i2c_readb(opaque, addr + 2) << 8;
    ret |= ppc4xx_i2c_readb(opaque, addr + 3);

    return ret;
}

static void ppc4xx_i2c_writel (void *opaque,
A
Anthony Liguori 已提交
1177
                               target_phys_addr_t addr, uint32_t value)
1178 1179
{
#ifdef DEBUG_I2C
1180 1181
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1182 1183 1184 1185 1186 1187 1188
#endif
    ppc4xx_i2c_writeb(opaque, addr, value >> 24);
    ppc4xx_i2c_writeb(opaque, addr + 1, value >> 16);
    ppc4xx_i2c_writeb(opaque, addr + 2, value >> 8);
    ppc4xx_i2c_writeb(opaque, addr + 3, value);
}

1189
static CPUReadMemoryFunc * const i2c_read[] = {
1190 1191 1192 1193 1194
    &ppc4xx_i2c_readb,
    &ppc4xx_i2c_readw,
    &ppc4xx_i2c_readl,
};

1195
static CPUWriteMemoryFunc * const i2c_write[] = {
1196 1197 1198 1199 1200 1201 1202
    &ppc4xx_i2c_writeb,
    &ppc4xx_i2c_writew,
    &ppc4xx_i2c_writel,
};

static void ppc4xx_i2c_reset (void *opaque)
{
A
Anthony Liguori 已提交
1203
    ppc4xx_i2c_t *i2c;
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

    i2c = opaque;
    i2c->mdata = 0x00;
    i2c->sdata = 0x00;
    i2c->cntl = 0x00;
    i2c->mdcntl = 0x00;
    i2c->sts = 0x00;
    i2c->extsts = 0x00;
    i2c->clkdiv = 0x00;
    i2c->xfrcnt = 0x00;
    i2c->directcntl = 0x0F;
}

A
Anthony Liguori 已提交
1217
static void ppc405_i2c_init(target_phys_addr_t base, qemu_irq irq)
1218
{
A
Anthony Liguori 已提交
1219
    ppc4xx_i2c_t *i2c;
B
Blue Swirl 已提交
1220
    int io;
1221

1222
    i2c = g_malloc0(sizeof(ppc4xx_i2c_t));
1223
    i2c->irq = irq;
1224
#ifdef DEBUG_I2C
1225
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
1226
#endif
1227 1228
    io = cpu_register_io_memory(i2c_read, i2c_write, i2c,
                                DEVICE_NATIVE_ENDIAN);
B
Blue Swirl 已提交
1229
    cpu_register_physical_memory(base, 0x011, io);
1230
    qemu_register_reset(ppc4xx_i2c_reset, i2c);
1231 1232
}

1233 1234
/*****************************************************************************/
/* General purpose timers */
A
Anthony Liguori 已提交
1235 1236
typedef struct ppc4xx_gpt_t ppc4xx_gpt_t;
struct ppc4xx_gpt_t {
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    int64_t tb_offset;
    uint32_t tb_freq;
    struct QEMUTimer *timer;
    qemu_irq irqs[5];
    uint32_t oe;
    uint32_t ol;
    uint32_t im;
    uint32_t is;
    uint32_t ie;
    uint32_t comp[5];
    uint32_t mask[5];
};

A
Anthony Liguori 已提交
1250
static uint32_t ppc4xx_gpt_readb (void *opaque, target_phys_addr_t addr)
1251 1252
{
#ifdef DEBUG_GPT
1253
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1254 1255 1256 1257 1258 1259
#endif
    /* XXX: generate a bus fault */
    return -1;
}

static void ppc4xx_gpt_writeb (void *opaque,
A
Anthony Liguori 已提交
1260
                               target_phys_addr_t addr, uint32_t value)
1261 1262
{
#ifdef DEBUG_I2C
1263 1264
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1265 1266 1267 1268
#endif
    /* XXX: generate a bus fault */
}

A
Anthony Liguori 已提交
1269
static uint32_t ppc4xx_gpt_readw (void *opaque, target_phys_addr_t addr)
1270 1271
{
#ifdef DEBUG_GPT
1272
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1273 1274 1275 1276 1277 1278
#endif
    /* XXX: generate a bus fault */
    return -1;
}

static void ppc4xx_gpt_writew (void *opaque,
A
Anthony Liguori 已提交
1279
                               target_phys_addr_t addr, uint32_t value)
1280 1281
{
#ifdef DEBUG_I2C
1282 1283
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1284 1285 1286 1287
#endif
    /* XXX: generate a bus fault */
}

A
Anthony Liguori 已提交
1288
static int ppc4xx_gpt_compare (ppc4xx_gpt_t *gpt, int n)
1289 1290 1291 1292 1293
{
    /* XXX: TODO */
    return 0;
}

A
Anthony Liguori 已提交
1294
static void ppc4xx_gpt_set_output (ppc4xx_gpt_t *gpt, int n, int level)
1295 1296 1297 1298
{
    /* XXX: TODO */
}

A
Anthony Liguori 已提交
1299
static void ppc4xx_gpt_set_outputs (ppc4xx_gpt_t *gpt)
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
{
    uint32_t mask;
    int i;

    mask = 0x80000000;
    for (i = 0; i < 5; i++) {
        if (gpt->oe & mask) {
            /* Output is enabled */
            if (ppc4xx_gpt_compare(gpt, i)) {
                /* Comparison is OK */
                ppc4xx_gpt_set_output(gpt, i, gpt->ol & mask);
            } else {
                /* Comparison is KO */
                ppc4xx_gpt_set_output(gpt, i, gpt->ol & mask ? 0 : 1);
            }
        }
        mask = mask >> 1;
    }
}

A
Anthony Liguori 已提交
1320
static void ppc4xx_gpt_set_irqs (ppc4xx_gpt_t *gpt)
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
{
    uint32_t mask;
    int i;

    mask = 0x00008000;
    for (i = 0; i < 5; i++) {
        if (gpt->is & gpt->im & mask)
            qemu_irq_raise(gpt->irqs[i]);
        else
            qemu_irq_lower(gpt->irqs[i]);
        mask = mask >> 1;
    }
}

A
Anthony Liguori 已提交
1335
static void ppc4xx_gpt_compute_timer (ppc4xx_gpt_t *gpt)
1336 1337 1338 1339
{
    /* XXX: TODO */
}

A
Anthony Liguori 已提交
1340
static uint32_t ppc4xx_gpt_readl (void *opaque, target_phys_addr_t addr)
1341
{
A
Anthony Liguori 已提交
1342
    ppc4xx_gpt_t *gpt;
1343 1344 1345 1346
    uint32_t ret;
    int idx;

#ifdef DEBUG_GPT
1347
    printf("%s: addr " TARGET_FMT_plx "\n", __func__, addr);
1348 1349
#endif
    gpt = opaque;
B
Blue Swirl 已提交
1350
    switch (addr) {
1351 1352
    case 0x00:
        /* Time base counter */
1353
        ret = muldiv64(qemu_get_clock_ns(vm_clock) + gpt->tb_offset,
1354
                       gpt->tb_freq, get_ticks_per_sec());
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
        break;
    case 0x10:
        /* Output enable */
        ret = gpt->oe;
        break;
    case 0x14:
        /* Output level */
        ret = gpt->ol;
        break;
    case 0x18:
        /* Interrupt mask */
        ret = gpt->im;
        break;
    case 0x1C:
    case 0x20:
        /* Interrupt status */
        ret = gpt->is;
        break;
    case 0x24:
        /* Interrupt enable */
        ret = gpt->ie;
        break;
    case 0x80 ... 0x90:
        /* Compare timer */
B
Blue Swirl 已提交
1379
        idx = (addr - 0x80) >> 2;
1380 1381 1382 1383
        ret = gpt->comp[idx];
        break;
    case 0xC0 ... 0xD0:
        /* Compare mask */
B
Blue Swirl 已提交
1384
        idx = (addr - 0xC0) >> 2;
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
        ret = gpt->mask[idx];
        break;
    default:
        ret = -1;
        break;
    }

    return ret;
}

static void ppc4xx_gpt_writel (void *opaque,
A
Anthony Liguori 已提交
1396
                               target_phys_addr_t addr, uint32_t value)
1397
{
A
Anthony Liguori 已提交
1398
    ppc4xx_gpt_t *gpt;
1399 1400 1401
    int idx;

#ifdef DEBUG_I2C
1402 1403
    printf("%s: addr " TARGET_FMT_plx " val %08" PRIx32 "\n", __func__, addr,
           value);
1404 1405
#endif
    gpt = opaque;
B
Blue Swirl 已提交
1406
    switch (addr) {
1407 1408
    case 0x00:
        /* Time base counter */
1409
        gpt->tb_offset = muldiv64(value, get_ticks_per_sec(), gpt->tb_freq)
1410
            - qemu_get_clock_ns(vm_clock);
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
        ppc4xx_gpt_compute_timer(gpt);
        break;
    case 0x10:
        /* Output enable */
        gpt->oe = value & 0xF8000000;
        ppc4xx_gpt_set_outputs(gpt);
        break;
    case 0x14:
        /* Output level */
        gpt->ol = value & 0xF8000000;
        ppc4xx_gpt_set_outputs(gpt);
        break;
    case 0x18:
        /* Interrupt mask */
        gpt->im = value & 0x0000F800;
        break;
    case 0x1C:
        /* Interrupt status set */
        gpt->is |= value & 0x0000F800;
        ppc4xx_gpt_set_irqs(gpt);
        break;
    case 0x20:
        /* Interrupt status clear */
        gpt->is &= ~(value & 0x0000F800);
        ppc4xx_gpt_set_irqs(gpt);
        break;
    case 0x24:
        /* Interrupt enable */
        gpt->ie = value & 0x0000F800;
        ppc4xx_gpt_set_irqs(gpt);
        break;
    case 0x80 ... 0x90:
        /* Compare timer */
B
Blue Swirl 已提交
1444
        idx = (addr - 0x80) >> 2;
1445 1446 1447 1448 1449
        gpt->comp[idx] = value & 0xF8000000;
        ppc4xx_gpt_compute_timer(gpt);
        break;
    case 0xC0 ... 0xD0:
        /* Compare mask */
B
Blue Swirl 已提交
1450
        idx = (addr - 0xC0) >> 2;
1451 1452 1453 1454 1455 1456
        gpt->mask[idx] = value & 0xF8000000;
        ppc4xx_gpt_compute_timer(gpt);
        break;
    }
}

1457
static CPUReadMemoryFunc * const gpt_read[] = {
1458 1459 1460 1461 1462
    &ppc4xx_gpt_readb,
    &ppc4xx_gpt_readw,
    &ppc4xx_gpt_readl,
};

1463
static CPUWriteMemoryFunc * const gpt_write[] = {
1464 1465 1466 1467 1468 1469 1470
    &ppc4xx_gpt_writeb,
    &ppc4xx_gpt_writew,
    &ppc4xx_gpt_writel,
};

static void ppc4xx_gpt_cb (void *opaque)
{
A
Anthony Liguori 已提交
1471
    ppc4xx_gpt_t *gpt;
1472 1473 1474 1475 1476 1477 1478 1479 1480

    gpt = opaque;
    ppc4xx_gpt_set_irqs(gpt);
    ppc4xx_gpt_set_outputs(gpt);
    ppc4xx_gpt_compute_timer(gpt);
}

static void ppc4xx_gpt_reset (void *opaque)
{
A
Anthony Liguori 已提交
1481
    ppc4xx_gpt_t *gpt;
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    int i;

    gpt = opaque;
    qemu_del_timer(gpt->timer);
    gpt->oe = 0x00000000;
    gpt->ol = 0x00000000;
    gpt->im = 0x00000000;
    gpt->is = 0x00000000;
    gpt->ie = 0x00000000;
    for (i = 0; i < 5; i++) {
        gpt->comp[i] = 0x00000000;
        gpt->mask[i] = 0x00000000;
    }
}

A
Anthony Liguori 已提交
1497
static void ppc4xx_gpt_init(target_phys_addr_t base, qemu_irq irqs[5])
1498
{
A
Anthony Liguori 已提交
1499
    ppc4xx_gpt_t *gpt;
1500
    int i;
B
Blue Swirl 已提交
1501
    int io;
1502

1503
    gpt = g_malloc0(sizeof(ppc4xx_gpt_t));
B
Blue Swirl 已提交
1504
    for (i = 0; i < 5; i++) {
1505
        gpt->irqs[i] = irqs[i];
B
Blue Swirl 已提交
1506
    }
1507
    gpt->timer = qemu_new_timer_ns(vm_clock, &ppc4xx_gpt_cb, gpt);
1508
#ifdef DEBUG_GPT
1509
    printf("%s: offset " TARGET_FMT_plx "\n", __func__, base);
1510
#endif
1511
    io = cpu_register_io_memory(gpt_read, gpt_write, gpt, DEVICE_NATIVE_ENDIAN);
B
Blue Swirl 已提交
1512
    cpu_register_physical_memory(base, 0x0d4, io);
1513
    qemu_register_reset(ppc4xx_gpt_reset, gpt);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
}

/*****************************************************************************/
/* MAL */
enum {
    MAL0_CFG      = 0x180,
    MAL0_ESR      = 0x181,
    MAL0_IER      = 0x182,
    MAL0_TXCASR   = 0x184,
    MAL0_TXCARR   = 0x185,
    MAL0_TXEOBISR = 0x186,
    MAL0_TXDEIR   = 0x187,
    MAL0_RXCASR   = 0x190,
    MAL0_RXCARR   = 0x191,
    MAL0_RXEOBISR = 0x192,
    MAL0_RXDEIR   = 0x193,
    MAL0_TXCTP0R  = 0x1A0,
    MAL0_TXCTP1R  = 0x1A1,
    MAL0_TXCTP2R  = 0x1A2,
    MAL0_TXCTP3R  = 0x1A3,
    MAL0_RXCTP0R  = 0x1C0,
    MAL0_RXCTP1R  = 0x1C1,
    MAL0_RCBS0    = 0x1E0,
    MAL0_RCBS1    = 0x1E1,
};

A
Anthony Liguori 已提交
1540 1541
typedef struct ppc40x_mal_t ppc40x_mal_t;
struct ppc40x_mal_t {
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
    qemu_irq irqs[4];
    uint32_t cfg;
    uint32_t esr;
    uint32_t ier;
    uint32_t txcasr;
    uint32_t txcarr;
    uint32_t txeobisr;
    uint32_t txdeir;
    uint32_t rxcasr;
    uint32_t rxcarr;
    uint32_t rxeobisr;
    uint32_t rxdeir;
    uint32_t txctpr[4];
    uint32_t rxctpr[2];
    uint32_t rcbs[2];
};

static void ppc40x_mal_reset (void *opaque);

A
Alexander Graf 已提交
1561
static uint32_t dcr_read_mal (void *opaque, int dcrn)
1562
{
A
Anthony Liguori 已提交
1563
    ppc40x_mal_t *mal;
A
Alexander Graf 已提交
1564
    uint32_t ret;
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632

    mal = opaque;
    switch (dcrn) {
    case MAL0_CFG:
        ret = mal->cfg;
        break;
    case MAL0_ESR:
        ret = mal->esr;
        break;
    case MAL0_IER:
        ret = mal->ier;
        break;
    case MAL0_TXCASR:
        ret = mal->txcasr;
        break;
    case MAL0_TXCARR:
        ret = mal->txcarr;
        break;
    case MAL0_TXEOBISR:
        ret = mal->txeobisr;
        break;
    case MAL0_TXDEIR:
        ret = mal->txdeir;
        break;
    case MAL0_RXCASR:
        ret = mal->rxcasr;
        break;
    case MAL0_RXCARR:
        ret = mal->rxcarr;
        break;
    case MAL0_RXEOBISR:
        ret = mal->rxeobisr;
        break;
    case MAL0_RXDEIR:
        ret = mal->rxdeir;
        break;
    case MAL0_TXCTP0R:
        ret = mal->txctpr[0];
        break;
    case MAL0_TXCTP1R:
        ret = mal->txctpr[1];
        break;
    case MAL0_TXCTP2R:
        ret = mal->txctpr[2];
        break;
    case MAL0_TXCTP3R:
        ret = mal->txctpr[3];
        break;
    case MAL0_RXCTP0R:
        ret = mal->rxctpr[0];
        break;
    case MAL0_RXCTP1R:
        ret = mal->rxctpr[1];
        break;
    case MAL0_RCBS0:
        ret = mal->rcbs[0];
        break;
    case MAL0_RCBS1:
        ret = mal->rcbs[1];
        break;
    default:
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
1633
static void dcr_write_mal (void *opaque, int dcrn, uint32_t val)
1634
{
A
Anthony Liguori 已提交
1635
    ppc40x_mal_t *mal;
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
    int idx;

    mal = opaque;
    switch (dcrn) {
    case MAL0_CFG:
        if (val & 0x80000000)
            ppc40x_mal_reset(mal);
        mal->cfg = val & 0x00FFC087;
        break;
    case MAL0_ESR:
        /* Read/clear */
        mal->esr &= ~val;
        break;
    case MAL0_IER:
        mal->ier = val & 0x0000001F;
        break;
    case MAL0_TXCASR:
        mal->txcasr = val & 0xF0000000;
        break;
    case MAL0_TXCARR:
        mal->txcarr = val & 0xF0000000;
        break;
    case MAL0_TXEOBISR:
        /* Read/clear */
        mal->txeobisr &= ~val;
        break;
    case MAL0_TXDEIR:
        /* Read/clear */
        mal->txdeir &= ~val;
        break;
    case MAL0_RXCASR:
        mal->rxcasr = val & 0xC0000000;
        break;
    case MAL0_RXCARR:
        mal->rxcarr = val & 0xC0000000;
        break;
    case MAL0_RXEOBISR:
        /* Read/clear */
        mal->rxeobisr &= ~val;
        break;
    case MAL0_RXDEIR:
        /* Read/clear */
        mal->rxdeir &= ~val;
        break;
    case MAL0_TXCTP0R:
        idx = 0;
        goto update_tx_ptr;
    case MAL0_TXCTP1R:
        idx = 1;
        goto update_tx_ptr;
    case MAL0_TXCTP2R:
        idx = 2;
        goto update_tx_ptr;
    case MAL0_TXCTP3R:
        idx = 3;
    update_tx_ptr:
        mal->txctpr[idx] = val;
        break;
    case MAL0_RXCTP0R:
        idx = 0;
        goto update_rx_ptr;
    case MAL0_RXCTP1R:
        idx = 1;
    update_rx_ptr:
        mal->rxctpr[idx] = val;
        break;
    case MAL0_RCBS0:
        idx = 0;
        goto update_rx_size;
    case MAL0_RCBS1:
        idx = 1;
    update_rx_size:
        mal->rcbs[idx] = val & 0x000000FF;
        break;
    }
}

static void ppc40x_mal_reset (void *opaque)
{
A
Anthony Liguori 已提交
1715
    ppc40x_mal_t *mal;
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

    mal = opaque;
    mal->cfg = 0x0007C000;
    mal->esr = 0x00000000;
    mal->ier = 0x00000000;
    mal->rxcasr = 0x00000000;
    mal->rxdeir = 0x00000000;
    mal->rxeobisr = 0x00000000;
    mal->txcasr = 0x00000000;
    mal->txdeir = 0x00000000;
    mal->txeobisr = 0x00000000;
}

B
Blue Swirl 已提交
1729
static void ppc405_mal_init(CPUState *env, qemu_irq irqs[4])
1730
{
A
Anthony Liguori 已提交
1731
    ppc40x_mal_t *mal;
1732 1733
    int i;

1734
    mal = g_malloc0(sizeof(ppc40x_mal_t));
1735 1736
    for (i = 0; i < 4; i++)
        mal->irqs[i] = irqs[i];
1737
    qemu_register_reset(&ppc40x_mal_reset, mal);
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
    ppc_dcr_register(env, MAL0_CFG,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_ESR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_IER,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCASR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCARR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXEOBISR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXDEIR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXCASR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXCARR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXEOBISR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXDEIR,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCTP0R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCTP1R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCTP2R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_TXCTP3R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXCTP0R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RXCTP1R,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RCBS0,
                     mal, &dcr_read_mal, &dcr_write_mal);
    ppc_dcr_register(env, MAL0_RCBS1,
                     mal, &dcr_read_mal, &dcr_write_mal);
1776 1777
}

1778 1779 1780 1781 1782 1783 1784
/*****************************************************************************/
/* SPR */
void ppc40x_core_reset (CPUState *env)
{
    target_ulong dbsr;

    printf("Reset PowerPC core\n");
1785 1786 1787
    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
    /* XXX: TOFIX */
#if 0
1788
    cpu_reset(env);
1789 1790 1791
#else
    qemu_system_reset_request();
#endif
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
    dbsr = env->spr[SPR_40x_DBSR];
    dbsr &= ~0x00000300;
    dbsr |= 0x00000100;
    env->spr[SPR_40x_DBSR] = dbsr;
}

void ppc40x_chip_reset (CPUState *env)
{
    target_ulong dbsr;

    printf("Reset PowerPC chip\n");
1803 1804 1805
    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
    /* XXX: TOFIX */
#if 0
1806
    cpu_reset(env);
1807 1808 1809
#else
    qemu_system_reset_request();
#endif
1810 1811 1812
    /* XXX: TODO reset all internal peripherals */
    dbsr = env->spr[SPR_40x_DBSR];
    dbsr &= ~0x00000300;
1813
    dbsr |= 0x00000200;
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    env->spr[SPR_40x_DBSR] = dbsr;
}

void ppc40x_system_reset (CPUState *env)
{
    printf("Reset PowerPC system\n");
    qemu_system_reset_request();
}

void store_40x_dbcr0 (CPUState *env, uint32_t val)
{
    switch ((val >> 28) & 0x3) {
    case 0x0:
        /* No action */
        break;
    case 0x1:
        /* Core reset */
        ppc40x_core_reset(env);
        break;
    case 0x2:
        /* Chip reset */
        ppc40x_chip_reset(env);
        break;
    case 0x3:
        /* System reset */
        ppc40x_system_reset(env);
        break;
    }
}

/*****************************************************************************/
/* PowerPC 405CR */
enum {
    PPC405CR_CPC0_PLLMR  = 0x0B0,
    PPC405CR_CPC0_CR0    = 0x0B1,
    PPC405CR_CPC0_CR1    = 0x0B2,
    PPC405CR_CPC0_PSR    = 0x0B4,
    PPC405CR_CPC0_JTAGID = 0x0B5,
    PPC405CR_CPC0_ER     = 0x0B9,
    PPC405CR_CPC0_FR     = 0x0BA,
    PPC405CR_CPC0_SR     = 0x0BB,
};

1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
enum {
    PPC405CR_CPU_CLK   = 0,
    PPC405CR_TMR_CLK   = 1,
    PPC405CR_PLB_CLK   = 2,
    PPC405CR_SDRAM_CLK = 3,
    PPC405CR_OPB_CLK   = 4,
    PPC405CR_EXT_CLK   = 5,
    PPC405CR_UART_CLK  = 6,
    PPC405CR_CLK_NB    = 7,
};

A
Anthony Liguori 已提交
1868 1869 1870
typedef struct ppc405cr_cpc_t ppc405cr_cpc_t;
struct ppc405cr_cpc_t {
    clk_setup_t clk_setup[PPC405CR_CLK_NB];
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    uint32_t sysclk;
    uint32_t psr;
    uint32_t cr0;
    uint32_t cr1;
    uint32_t jtagid;
    uint32_t pllmr;
    uint32_t er;
    uint32_t fr;
};

A
Anthony Liguori 已提交
1881
static void ppc405cr_clk_setup (ppc405cr_cpc_t *cpc)
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
{
    uint64_t VCO_out, PLL_out;
    uint32_t CPU_clk, TMR_clk, SDRAM_clk, PLB_clk, OPB_clk, EXT_clk, UART_clk;
    int M, D0, D1, D2;

    D0 = ((cpc->pllmr >> 26) & 0x3) + 1; /* CBDV */
    if (cpc->pllmr & 0x80000000) {
        D1 = (((cpc->pllmr >> 20) - 1) & 0xF) + 1; /* FBDV */
        D2 = 8 - ((cpc->pllmr >> 16) & 0x7); /* FWDVA */
        M = D0 * D1 * D2;
        VCO_out = cpc->sysclk * M;
        if (VCO_out < 400000000 || VCO_out > 800000000) {
            /* PLL cannot lock */
            cpc->pllmr &= ~0x80000000;
            goto bypass_pll;
        }
        PLL_out = VCO_out / D2;
    } else {
        /* Bypass PLL */
    bypass_pll:
        M = D0;
        PLL_out = cpc->sysclk * M;
    }
    CPU_clk = PLL_out;
    if (cpc->cr1 & 0x00800000)
        TMR_clk = cpc->sysclk; /* Should have a separate clock */
    else
        TMR_clk = CPU_clk;
    PLB_clk = CPU_clk / D0;
    SDRAM_clk = PLB_clk;
    D0 = ((cpc->pllmr >> 10) & 0x3) + 1;
    OPB_clk = PLB_clk / D0;
    D0 = ((cpc->pllmr >> 24) & 0x3) + 2;
    EXT_clk = PLB_clk / D0;
    D0 = ((cpc->cr0 >> 1) & 0x1F) + 1;
    UART_clk = CPU_clk / D0;
    /* Setup CPU clocks */
1919
    clk_setup(&cpc->clk_setup[PPC405CR_CPU_CLK], CPU_clk);
1920
    /* Setup time-base clock */
1921
    clk_setup(&cpc->clk_setup[PPC405CR_TMR_CLK], TMR_clk);
1922
    /* Setup PLB clock */
1923
    clk_setup(&cpc->clk_setup[PPC405CR_PLB_CLK], PLB_clk);
1924
    /* Setup SDRAM clock */
1925
    clk_setup(&cpc->clk_setup[PPC405CR_SDRAM_CLK], SDRAM_clk);
1926
    /* Setup OPB clock */
1927
    clk_setup(&cpc->clk_setup[PPC405CR_OPB_CLK], OPB_clk);
1928
    /* Setup external clock */
1929
    clk_setup(&cpc->clk_setup[PPC405CR_EXT_CLK], EXT_clk);
1930
    /* Setup UART clock */
1931
    clk_setup(&cpc->clk_setup[PPC405CR_UART_CLK], UART_clk);
1932 1933
}

A
Alexander Graf 已提交
1934
static uint32_t dcr_read_crcpc (void *opaque, int dcrn)
1935
{
A
Anthony Liguori 已提交
1936
    ppc405cr_cpc_t *cpc;
A
Alexander Graf 已提交
1937
    uint32_t ret;
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973

    cpc = opaque;
    switch (dcrn) {
    case PPC405CR_CPC0_PLLMR:
        ret = cpc->pllmr;
        break;
    case PPC405CR_CPC0_CR0:
        ret = cpc->cr0;
        break;
    case PPC405CR_CPC0_CR1:
        ret = cpc->cr1;
        break;
    case PPC405CR_CPC0_PSR:
        ret = cpc->psr;
        break;
    case PPC405CR_CPC0_JTAGID:
        ret = cpc->jtagid;
        break;
    case PPC405CR_CPC0_ER:
        ret = cpc->er;
        break;
    case PPC405CR_CPC0_FR:
        ret = cpc->fr;
        break;
    case PPC405CR_CPC0_SR:
        ret = ~(cpc->er | cpc->fr) & 0xFFFF0000;
        break;
    default:
        /* Avoid gcc warning */
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
1974
static void dcr_write_crcpc (void *opaque, int dcrn, uint32_t val)
1975
{
A
Anthony Liguori 已提交
1976
    ppc405cr_cpc_t *cpc;
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008

    cpc = opaque;
    switch (dcrn) {
    case PPC405CR_CPC0_PLLMR:
        cpc->pllmr = val & 0xFFF77C3F;
        break;
    case PPC405CR_CPC0_CR0:
        cpc->cr0 = val & 0x0FFFFFFE;
        break;
    case PPC405CR_CPC0_CR1:
        cpc->cr1 = val & 0x00800000;
        break;
    case PPC405CR_CPC0_PSR:
        /* Read-only */
        break;
    case PPC405CR_CPC0_JTAGID:
        /* Read-only */
        break;
    case PPC405CR_CPC0_ER:
        cpc->er = val & 0xBFFC0000;
        break;
    case PPC405CR_CPC0_FR:
        cpc->fr = val & 0xBFFC0000;
        break;
    case PPC405CR_CPC0_SR:
        /* Read-only */
        break;
    }
}

static void ppc405cr_cpc_reset (void *opaque)
{
A
Anthony Liguori 已提交
2009
    ppc405cr_cpc_t *cpc;
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    int D;

    cpc = opaque;
    /* Compute PLLMR value from PSR settings */
    cpc->pllmr = 0x80000000;
    /* PFWD */
    switch ((cpc->psr >> 30) & 3) {
    case 0:
        /* Bypass */
        cpc->pllmr &= ~0x80000000;
        break;
    case 1:
        /* Divide by 3 */
        cpc->pllmr |= 5 << 16;
        break;
    case 2:
        /* Divide by 4 */
        cpc->pllmr |= 4 << 16;
        break;
    case 3:
        /* Divide by 6 */
        cpc->pllmr |= 2 << 16;
        break;
    }
    /* PFBD */
    D = (cpc->psr >> 28) & 3;
    cpc->pllmr |= (D + 1) << 20;
    /* PT   */
    D = (cpc->psr >> 25) & 7;
    switch (D) {
    case 0x2:
        cpc->pllmr |= 0x13;
        break;
    case 0x4:
        cpc->pllmr |= 0x15;
        break;
    case 0x5:
        cpc->pllmr |= 0x16;
        break;
    default:
        break;
    }
    /* PDC  */
    D = (cpc->psr >> 23) & 3;
    cpc->pllmr |= D << 26;
    /* ODP  */
    D = (cpc->psr >> 21) & 3;
    cpc->pllmr |= D << 10;
    /* EBPD */
    D = (cpc->psr >> 17) & 3;
    cpc->pllmr |= D << 24;
    cpc->cr0 = 0x0000003C;
    cpc->cr1 = 0x2B0D8800;
    cpc->er = 0x00000000;
    cpc->fr = 0x00000000;
    ppc405cr_clk_setup(cpc);
}

A
Anthony Liguori 已提交
2068
static void ppc405cr_clk_init (ppc405cr_cpc_t *cpc)
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
{
    int D;

    /* XXX: this should be read from IO pins */
    cpc->psr = 0x00000000; /* 8 bits ROM */
    /* PFWD */
    D = 0x2; /* Divide by 4 */
    cpc->psr |= D << 30;
    /* PFBD */
    D = 0x1; /* Divide by 2 */
    cpc->psr |= D << 28;
    /* PDC */
    D = 0x1; /* Divide by 2 */
    cpc->psr |= D << 23;
    /* PT */
    D = 0x5; /* M = 16 */
    cpc->psr |= D << 25;
    /* ODP */
    D = 0x1; /* Divide by 2 */
    cpc->psr |= D << 21;
    /* EBDP */
    D = 0x2; /* Divide by 4 */
    cpc->psr |= D << 17;
}

A
Anthony Liguori 已提交
2094
static void ppc405cr_cpc_init (CPUState *env, clk_setup_t clk_setup[7],
2095 2096
                               uint32_t sysclk)
{
A
Anthony Liguori 已提交
2097
    ppc405cr_cpc_t *cpc;
2098

2099
    cpc = g_malloc0(sizeof(ppc405cr_cpc_t));
2100
    memcpy(cpc->clk_setup, clk_setup,
A
Anthony Liguori 已提交
2101
           PPC405CR_CLK_NB * sizeof(clk_setup_t));
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
    cpc->sysclk = sysclk;
    cpc->jtagid = 0x42051049;
    ppc_dcr_register(env, PPC405CR_CPC0_PSR, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_CR0, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_CR1, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_JTAGID, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_PLLMR, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_ER, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_FR, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc_dcr_register(env, PPC405CR_CPC0_SR, cpc,
                     &dcr_read_crcpc, &dcr_write_crcpc);
    ppc405cr_clk_init(cpc);
2121
    qemu_register_reset(ppc405cr_cpc_reset, cpc);
2122 2123
}

A
Anthony Liguori 已提交
2124 2125
CPUState *ppc405cr_init (target_phys_addr_t ram_bases[4],
                         target_phys_addr_t ram_sizes[4],
2126
                         uint32_t sysclk, qemu_irq **picp,
P
pbrook 已提交
2127
                         int do_init)
2128
{
A
Anthony Liguori 已提交
2129
    clk_setup_t clk_setup[PPC405CR_CLK_NB];
2130 2131 2132 2133 2134
    qemu_irq dma_irqs[4];
    CPUState *env;
    qemu_irq *pic, *irqs;

    memset(clk_setup, 0, sizeof(clk_setup));
2135
    env = ppc4xx_init("405cr", &clk_setup[PPC405CR_CPU_CLK],
2136
                      &clk_setup[PPC405CR_TMR_CLK], sysclk);
2137 2138 2139 2140 2141 2142
    /* Memory mapped devices registers */
    /* PLB arbitrer */
    ppc4xx_plb_init(env);
    /* PLB to OPB bridge */
    ppc4xx_pob_init(env);
    /* OBP arbitrer */
B
Blue Swirl 已提交
2143
    ppc4xx_opba_init(0xef600600);
2144
    /* Universal interrupt controller */
2145
    irqs = g_malloc0(sizeof(qemu_irq) * PPCUIC_OUTPUT_NB);
2146
    irqs[PPCUIC_OUTPUT_INT] =
J
j_mayer 已提交
2147
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
2148
    irqs[PPCUIC_OUTPUT_CINT] =
J
j_mayer 已提交
2149
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
2150 2151 2152
    pic = ppcuic_init(env, irqs, 0x0C0, 0, 1);
    *picp = pic;
    /* SDRAM controller */
2153
    ppc4xx_sdram_init(env, pic[14], 1, ram_bases, ram_sizes, do_init);
2154 2155 2156
    /* External bus controller */
    ppc405_ebc_init(env);
    /* DMA controller */
2157 2158 2159 2160
    dma_irqs[0] = pic[26];
    dma_irqs[1] = pic[25];
    dma_irqs[2] = pic[24];
    dma_irqs[3] = pic[23];
2161 2162 2163
    ppc405_dma_init(env, dma_irqs);
    /* Serial ports */
    if (serial_hds[0] != NULL) {
B
Blue Swirl 已提交
2164
        serial_mm_init(0xef600300, 0, pic[0], PPC_SERIAL_MM_BAUDBASE,
B
Blue Swirl 已提交
2165
                       serial_hds[0], 1, 1);
2166 2167
    }
    if (serial_hds[1] != NULL) {
B
Blue Swirl 已提交
2168
        serial_mm_init(0xef600400, 0, pic[1], PPC_SERIAL_MM_BAUDBASE,
B
Blue Swirl 已提交
2169
                       serial_hds[1], 1, 1);
2170 2171
    }
    /* IIC controller */
B
Blue Swirl 已提交
2172
    ppc405_i2c_init(0xef600500, pic[2]);
2173
    /* GPIO */
B
Blue Swirl 已提交
2174
    ppc405_gpio_init(0xef600700);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
    /* CPU control */
    ppc405cr_cpc_init(env, clk_setup, sysclk);

    return env;
}

/*****************************************************************************/
/* PowerPC 405EP */
/* CPU control */
enum {
    PPC405EP_CPC0_PLLMR0 = 0x0F0,
    PPC405EP_CPC0_BOOT   = 0x0F1,
    PPC405EP_CPC0_EPCTL  = 0x0F3,
    PPC405EP_CPC0_PLLMR1 = 0x0F4,
    PPC405EP_CPC0_UCR    = 0x0F5,
    PPC405EP_CPC0_SRR    = 0x0F6,
    PPC405EP_CPC0_JTAGID = 0x0F7,
    PPC405EP_CPC0_PCI    = 0x0F9,
2193 2194 2195 2196 2197
#if 0
    PPC405EP_CPC0_ER     = xxx,
    PPC405EP_CPC0_FR     = xxx,
    PPC405EP_CPC0_SR     = xxx,
#endif
2198 2199
};

2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
enum {
    PPC405EP_CPU_CLK   = 0,
    PPC405EP_PLB_CLK   = 1,
    PPC405EP_OPB_CLK   = 2,
    PPC405EP_EBC_CLK   = 3,
    PPC405EP_MAL_CLK   = 4,
    PPC405EP_PCI_CLK   = 5,
    PPC405EP_UART0_CLK = 6,
    PPC405EP_UART1_CLK = 7,
    PPC405EP_CLK_NB    = 8,
};

A
Anthony Liguori 已提交
2212 2213
typedef struct ppc405ep_cpc_t ppc405ep_cpc_t;
struct ppc405ep_cpc_t {
2214
    uint32_t sysclk;
A
Anthony Liguori 已提交
2215
    clk_setup_t clk_setup[PPC405EP_CLK_NB];
2216 2217 2218 2219 2220 2221 2222
    uint32_t boot;
    uint32_t epctl;
    uint32_t pllmr[2];
    uint32_t ucr;
    uint32_t srr;
    uint32_t jtagid;
    uint32_t pci;
2223 2224 2225 2226
    /* Clock and power management */
    uint32_t er;
    uint32_t fr;
    uint32_t sr;
2227 2228
};

A
Anthony Liguori 已提交
2229
static void ppc405ep_compute_clocks (ppc405ep_cpc_t *cpc)
2230 2231 2232 2233 2234 2235 2236 2237 2238
{
    uint32_t CPU_clk, PLB_clk, OPB_clk, EBC_clk, MAL_clk, PCI_clk;
    uint32_t UART0_clk, UART1_clk;
    uint64_t VCO_out, PLL_out;
    int M, D;

    VCO_out = 0;
    if ((cpc->pllmr[1] & 0x80000000) && !(cpc->pllmr[1] & 0x40000000)) {
        M = (((cpc->pllmr[1] >> 20) - 1) & 0xF) + 1; /* FBMUL */
2239 2240 2241
#ifdef DEBUG_CLOCKS_LL
        printf("FBMUL %01" PRIx32 " %d\n", (cpc->pllmr[1] >> 20) & 0xF, M);
#endif
2242
        D = 8 - ((cpc->pllmr[1] >> 16) & 0x7); /* FWDA */
2243 2244 2245
#ifdef DEBUG_CLOCKS_LL
        printf("FWDA %01" PRIx32 " %d\n", (cpc->pllmr[1] >> 16) & 0x7, D);
#endif
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
        VCO_out = cpc->sysclk * M * D;
        if (VCO_out < 500000000UL || VCO_out > 1000000000UL) {
            /* Error - unlock the PLL */
            printf("VCO out of range %" PRIu64 "\n", VCO_out);
#if 0
            cpc->pllmr[1] &= ~0x80000000;
            goto pll_bypass;
#endif
        }
        PLL_out = VCO_out / D;
2256 2257
        /* Pretend the PLL is locked */
        cpc->boot |= 0x00000001;
2258 2259 2260 2261 2262
    } else {
#if 0
    pll_bypass:
#endif
        PLL_out = cpc->sysclk;
2263 2264 2265 2266
        if (cpc->pllmr[1] & 0x40000000) {
            /* Pretend the PLL is not locked */
            cpc->boot &= ~0x00000001;
        }
2267 2268 2269
    }
    /* Now, compute all other clocks */
    D = ((cpc->pllmr[0] >> 20) & 0x3) + 1; /* CCDV */
2270 2271
#ifdef DEBUG_CLOCKS_LL
    printf("CCDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 20) & 0x3, D);
2272 2273 2274
#endif
    CPU_clk = PLL_out / D;
    D = ((cpc->pllmr[0] >> 16) & 0x3) + 1; /* CBDV */
2275 2276
#ifdef DEBUG_CLOCKS_LL
    printf("CBDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 16) & 0x3, D);
2277 2278 2279
#endif
    PLB_clk = CPU_clk / D;
    D = ((cpc->pllmr[0] >> 12) & 0x3) + 1; /* OPDV */
2280 2281
#ifdef DEBUG_CLOCKS_LL
    printf("OPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 12) & 0x3, D);
2282 2283 2284
#endif
    OPB_clk = PLB_clk / D;
    D = ((cpc->pllmr[0] >> 8) & 0x3) + 2; /* EPDV */
2285 2286
#ifdef DEBUG_CLOCKS_LL
    printf("EPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 8) & 0x3, D);
2287 2288 2289
#endif
    EBC_clk = PLB_clk / D;
    D = ((cpc->pllmr[0] >> 4) & 0x3) + 1; /* MPDV */
2290 2291
#ifdef DEBUG_CLOCKS_LL
    printf("MPDV %01" PRIx32 " %d\n", (cpc->pllmr[0] >> 4) & 0x3, D);
2292 2293 2294
#endif
    MAL_clk = PLB_clk / D;
    D = (cpc->pllmr[0] & 0x3) + 1; /* PPDV */
2295 2296
#ifdef DEBUG_CLOCKS_LL
    printf("PPDV %01" PRIx32 " %d\n", cpc->pllmr[0] & 0x3, D);
2297 2298 2299
#endif
    PCI_clk = PLB_clk / D;
    D = ((cpc->ucr - 1) & 0x7F) + 1; /* U0DIV */
2300 2301
#ifdef DEBUG_CLOCKS_LL
    printf("U0DIV %01" PRIx32 " %d\n", cpc->ucr & 0x7F, D);
2302 2303 2304
#endif
    UART0_clk = PLL_out / D;
    D = (((cpc->ucr >> 8) - 1) & 0x7F) + 1; /* U1DIV */
2305 2306
#ifdef DEBUG_CLOCKS_LL
    printf("U1DIV %01" PRIx32 " %d\n", (cpc->ucr >> 8) & 0x7F, D);
2307 2308 2309
#endif
    UART1_clk = PLL_out / D;
#ifdef DEBUG_CLOCKS
2310
    printf("Setup PPC405EP clocks - sysclk %" PRIu32 " VCO %" PRIu64
2311
           " PLL out %" PRIu64 " Hz\n", cpc->sysclk, VCO_out, PLL_out);
2312 2313 2314
    printf("CPU %" PRIu32 " PLB %" PRIu32 " OPB %" PRIu32 " EBC %" PRIu32
           " MAL %" PRIu32 " PCI %" PRIu32 " UART0 %" PRIu32
           " UART1 %" PRIu32 "\n",
2315 2316 2317 2318
           CPU_clk, PLB_clk, OPB_clk, EBC_clk, MAL_clk, PCI_clk,
           UART0_clk, UART1_clk);
#endif
    /* Setup CPU clocks */
2319
    clk_setup(&cpc->clk_setup[PPC405EP_CPU_CLK], CPU_clk);
2320
    /* Setup PLB clock */
2321
    clk_setup(&cpc->clk_setup[PPC405EP_PLB_CLK], PLB_clk);
2322
    /* Setup OPB clock */
2323
    clk_setup(&cpc->clk_setup[PPC405EP_OPB_CLK], OPB_clk);
2324
    /* Setup external clock */
2325
    clk_setup(&cpc->clk_setup[PPC405EP_EBC_CLK], EBC_clk);
2326
    /* Setup MAL clock */
2327
    clk_setup(&cpc->clk_setup[PPC405EP_MAL_CLK], MAL_clk);
2328
    /* Setup PCI clock */
2329
    clk_setup(&cpc->clk_setup[PPC405EP_PCI_CLK], PCI_clk);
2330
    /* Setup UART0 clock */
2331
    clk_setup(&cpc->clk_setup[PPC405EP_UART0_CLK], UART0_clk);
2332
    /* Setup UART1 clock */
2333
    clk_setup(&cpc->clk_setup[PPC405EP_UART1_CLK], UART1_clk);
2334 2335
}

A
Alexander Graf 已提交
2336
static uint32_t dcr_read_epcpc (void *opaque, int dcrn)
2337
{
A
Anthony Liguori 已提交
2338
    ppc405ep_cpc_t *cpc;
A
Alexander Graf 已提交
2339
    uint32_t ret;
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375

    cpc = opaque;
    switch (dcrn) {
    case PPC405EP_CPC0_BOOT:
        ret = cpc->boot;
        break;
    case PPC405EP_CPC0_EPCTL:
        ret = cpc->epctl;
        break;
    case PPC405EP_CPC0_PLLMR0:
        ret = cpc->pllmr[0];
        break;
    case PPC405EP_CPC0_PLLMR1:
        ret = cpc->pllmr[1];
        break;
    case PPC405EP_CPC0_UCR:
        ret = cpc->ucr;
        break;
    case PPC405EP_CPC0_SRR:
        ret = cpc->srr;
        break;
    case PPC405EP_CPC0_JTAGID:
        ret = cpc->jtagid;
        break;
    case PPC405EP_CPC0_PCI:
        ret = cpc->pci;
        break;
    default:
        /* Avoid gcc warning */
        ret = 0;
        break;
    }

    return ret;
}

A
Alexander Graf 已提交
2376
static void dcr_write_epcpc (void *opaque, int dcrn, uint32_t val)
2377
{
A
Anthony Liguori 已提交
2378
    ppc405ep_cpc_t *cpc;
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414

    cpc = opaque;
    switch (dcrn) {
    case PPC405EP_CPC0_BOOT:
        /* Read-only register */
        break;
    case PPC405EP_CPC0_EPCTL:
        /* Don't care for now */
        cpc->epctl = val & 0xC00000F3;
        break;
    case PPC405EP_CPC0_PLLMR0:
        cpc->pllmr[0] = val & 0x00633333;
        ppc405ep_compute_clocks(cpc);
        break;
    case PPC405EP_CPC0_PLLMR1:
        cpc->pllmr[1] = val & 0xC0F73FFF;
        ppc405ep_compute_clocks(cpc);
        break;
    case PPC405EP_CPC0_UCR:
        /* UART control - don't care for now */
        cpc->ucr = val & 0x003F7F7F;
        break;
    case PPC405EP_CPC0_SRR:
        cpc->srr = val;
        break;
    case PPC405EP_CPC0_JTAGID:
        /* Read-only */
        break;
    case PPC405EP_CPC0_PCI:
        cpc->pci = val;
        break;
    }
}

static void ppc405ep_cpc_reset (void *opaque)
{
A
Anthony Liguori 已提交
2415
    ppc405ep_cpc_t *cpc = opaque;
2416 2417 2418 2419 2420 2421 2422 2423

    cpc->boot = 0x00000010;     /* Boot from PCI - IIC EEPROM disabled */
    cpc->epctl = 0x00000000;
    cpc->pllmr[0] = 0x00011010;
    cpc->pllmr[1] = 0x40000000;
    cpc->ucr = 0x00000000;
    cpc->srr = 0x00040000;
    cpc->pci = 0x00000000;
2424 2425 2426
    cpc->er = 0x00000000;
    cpc->fr = 0x00000000;
    cpc->sr = 0x00000000;
2427 2428 2429 2430
    ppc405ep_compute_clocks(cpc);
}

/* XXX: sysclk should be between 25 and 100 MHz */
A
Anthony Liguori 已提交
2431
static void ppc405ep_cpc_init (CPUState *env, clk_setup_t clk_setup[8],
2432 2433
                               uint32_t sysclk)
{
A
Anthony Liguori 已提交
2434
    ppc405ep_cpc_t *cpc;
2435

2436
    cpc = g_malloc0(sizeof(ppc405ep_cpc_t));
2437
    memcpy(cpc->clk_setup, clk_setup,
A
Anthony Liguori 已提交
2438
           PPC405EP_CLK_NB * sizeof(clk_setup_t));
2439 2440
    cpc->jtagid = 0x20267049;
    cpc->sysclk = sysclk;
2441
    qemu_register_reset(&ppc405ep_cpc_reset, cpc);
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
    ppc_dcr_register(env, PPC405EP_CPC0_BOOT, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_EPCTL, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_PLLMR0, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_PLLMR1, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_UCR, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_SRR, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_JTAGID, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_PCI, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
2458
#if 0
2459 2460 2461 2462 2463 2464
    ppc_dcr_register(env, PPC405EP_CPC0_ER, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_FR, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
    ppc_dcr_register(env, PPC405EP_CPC0_SR, cpc,
                     &dcr_read_epcpc, &dcr_write_epcpc);
2465
#endif
2466 2467
}

A
Anthony Liguori 已提交
2468 2469
CPUState *ppc405ep_init (target_phys_addr_t ram_bases[2],
                         target_phys_addr_t ram_sizes[2],
2470
                         uint32_t sysclk, qemu_irq **picp,
P
pbrook 已提交
2471
                         int do_init)
2472
{
A
Anthony Liguori 已提交
2473
    clk_setup_t clk_setup[PPC405EP_CLK_NB], tlb_clk_setup;
2474
    qemu_irq dma_irqs[4], gpt_irqs[5], mal_irqs[4];
2475 2476 2477 2478 2479
    CPUState *env;
    qemu_irq *pic, *irqs;

    memset(clk_setup, 0, sizeof(clk_setup));
    /* init CPUs */
2480
    env = ppc4xx_init("405ep", &clk_setup[PPC405EP_CPU_CLK],
2481 2482 2483
                      &tlb_clk_setup, sysclk);
    clk_setup[PPC405EP_CPU_CLK].cb = tlb_clk_setup.cb;
    clk_setup[PPC405EP_CPU_CLK].opaque = tlb_clk_setup.opaque;
2484 2485 2486 2487 2488 2489 2490
    /* Internal devices init */
    /* Memory mapped devices registers */
    /* PLB arbitrer */
    ppc4xx_plb_init(env);
    /* PLB to OPB bridge */
    ppc4xx_pob_init(env);
    /* OBP arbitrer */
B
Blue Swirl 已提交
2491
    ppc4xx_opba_init(0xef600600);
2492
    /* Universal interrupt controller */
2493
    irqs = g_malloc0(sizeof(qemu_irq) * PPCUIC_OUTPUT_NB);
2494
    irqs[PPCUIC_OUTPUT_INT] =
J
j_mayer 已提交
2495
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
2496
    irqs[PPCUIC_OUTPUT_CINT] =
J
j_mayer 已提交
2497
        ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
2498 2499 2500
    pic = ppcuic_init(env, irqs, 0x0C0, 0, 1);
    *picp = pic;
    /* SDRAM controller */
2501
	/* XXX 405EP has no ECC interrupt */
2502
    ppc4xx_sdram_init(env, pic[17], 2, ram_bases, ram_sizes, do_init);
2503 2504 2505
    /* External bus controller */
    ppc405_ebc_init(env);
    /* DMA controller */
2506 2507 2508 2509
    dma_irqs[0] = pic[5];
    dma_irqs[1] = pic[6];
    dma_irqs[2] = pic[7];
    dma_irqs[3] = pic[8];
2510 2511
    ppc405_dma_init(env, dma_irqs);
    /* IIC controller */
B
Blue Swirl 已提交
2512
    ppc405_i2c_init(0xef600500, pic[2]);
2513
    /* GPIO */
B
Blue Swirl 已提交
2514
    ppc405_gpio_init(0xef600700);
2515 2516
    /* Serial ports */
    if (serial_hds[0] != NULL) {
B
Blue Swirl 已提交
2517
        serial_mm_init(0xef600300, 0, pic[0], PPC_SERIAL_MM_BAUDBASE,
B
Blue Swirl 已提交
2518
                       serial_hds[0], 1, 1);
2519 2520
    }
    if (serial_hds[1] != NULL) {
B
Blue Swirl 已提交
2521
        serial_mm_init(0xef600400, 0, pic[1], PPC_SERIAL_MM_BAUDBASE,
B
Blue Swirl 已提交
2522
                       serial_hds[1], 1, 1);
2523 2524
    }
    /* OCM */
P
pbrook 已提交
2525
    ppc405_ocm_init(env);
2526
    /* GPT */
2527 2528 2529 2530 2531
    gpt_irqs[0] = pic[19];
    gpt_irqs[1] = pic[20];
    gpt_irqs[2] = pic[21];
    gpt_irqs[3] = pic[22];
    gpt_irqs[4] = pic[23];
B
Blue Swirl 已提交
2532
    ppc4xx_gpt_init(0xef600000, gpt_irqs);
2533
    /* PCI */
2534
    /* Uses pic[3], pic[16], pic[18] */
2535
    /* MAL */
2536 2537 2538 2539
    mal_irqs[0] = pic[11];
    mal_irqs[1] = pic[12];
    mal_irqs[2] = pic[13];
    mal_irqs[3] = pic[14];
2540 2541
    ppc405_mal_init(env, mal_irqs);
    /* Ethernet */
2542
    /* Uses pic[9], pic[15], pic[17] */
2543 2544 2545 2546 2547
    /* CPU control */
    ppc405ep_cpc_init(env, clk_setup, sysclk);

    return env;
}