pxa2xx.c 68.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Intel XScale PXA255/270 processor support.
 *
 * Copyright (c) 2006 Openedhand Ltd.
 * Written by Andrzej Zaborowski <balrog@zabor.org>
 *
 * This code is licenced under the GPL.
 */

P
Paul Brook 已提交
10
#include "sysbus.h"
P
pbrook 已提交
11 12 13 14
#include "pxa.h"
#include "sysemu.h"
#include "pc.h"
#include "i2c.h"
P
Paul Brook 已提交
15
#include "ssi.h"
P
pbrook 已提交
16
#include "qemu-char.h"
B
Blue Swirl 已提交
17
#include "blockdev.h"
18 19

static struct {
A
Anthony Liguori 已提交
20
    target_phys_addr_t io_base;
21 22 23 24 25 26 27 28 29 30 31 32 33 34
    int irqn;
} pxa255_serial[] = {
    { 0x40100000, PXA2XX_PIC_FFUART },
    { 0x40200000, PXA2XX_PIC_BTUART },
    { 0x40700000, PXA2XX_PIC_STUART },
    { 0x41600000, PXA25X_PIC_HWUART },
    { 0, 0 }
}, pxa270_serial[] = {
    { 0x40100000, PXA2XX_PIC_FFUART },
    { 0x40200000, PXA2XX_PIC_BTUART },
    { 0x40700000, PXA2XX_PIC_STUART },
    { 0, 0 }
};

B
bellard 已提交
35
typedef struct PXASSPDef {
A
Anthony Liguori 已提交
36
    target_phys_addr_t io_base;
37
    int irqn;
B
bellard 已提交
38 39 40 41
} PXASSPDef;

#if 0
static PXASSPDef pxa250_ssp[] = {
42 43
    { 0x41000000, PXA2XX_PIC_SSP },
    { 0, 0 }
B
bellard 已提交
44 45 46 47
};
#endif

static PXASSPDef pxa255_ssp[] = {
48 49 50
    { 0x41000000, PXA2XX_PIC_SSP },
    { 0x41400000, PXA25X_PIC_NSSP },
    { 0, 0 }
B
bellard 已提交
51 52 53 54
};

#if 0
static PXASSPDef pxa26x_ssp[] = {
55 56 57 58
    { 0x41000000, PXA2XX_PIC_SSP },
    { 0x41400000, PXA25X_PIC_NSSP },
    { 0x41500000, PXA26X_PIC_ASSP },
    { 0, 0 }
B
bellard 已提交
59 60 61 62
};
#endif

static PXASSPDef pxa27x_ssp[] = {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    { 0x41000000, PXA2XX_PIC_SSP },
    { 0x41700000, PXA27X_PIC_SSP2 },
    { 0x41900000, PXA2XX_PIC_SSP3 },
    { 0, 0 }
};

#define PMCR	0x00	/* Power Manager Control register */
#define PSSR	0x04	/* Power Manager Sleep Status register */
#define PSPR	0x08	/* Power Manager Scratch-Pad register */
#define PWER	0x0c	/* Power Manager Wake-Up Enable register */
#define PRER	0x10	/* Power Manager Rising-Edge Detect Enable register */
#define PFER	0x14	/* Power Manager Falling-Edge Detect Enable register */
#define PEDR	0x18	/* Power Manager Edge-Detect Status register */
#define PCFR	0x1c	/* Power Manager General Configuration register */
#define PGSR0	0x20	/* Power Manager GPIO Sleep-State register 0 */
#define PGSR1	0x24	/* Power Manager GPIO Sleep-State register 1 */
#define PGSR2	0x28	/* Power Manager GPIO Sleep-State register 2 */
#define PGSR3	0x2c	/* Power Manager GPIO Sleep-State register 3 */
#define RCSR	0x30	/* Reset Controller Status register */
#define PSLR	0x34	/* Power Manager Sleep Configuration register */
#define PTSR	0x38	/* Power Manager Standby Configuration register */
#define PVCR	0x40	/* Power Manager Voltage Change Control register */
#define PUCR	0x4c	/* Power Manager USIM Card Control/Status register */
#define PKWR	0x50	/* Power Manager Keyboard Wake-Up Enable register */
#define PKSR	0x54	/* Power Manager Keyboard Level-Detect Status */
#define PCMD0	0x80	/* Power Manager I2C Command register File 0 */
#define PCMD31	0xfc	/* Power Manager I2C Command register File 31 */

A
Anthony Liguori 已提交
91
static uint32_t pxa2xx_pm_read(void *opaque, target_phys_addr_t addr)
92
{
P
Paul Brook 已提交
93
    PXA2xxState *s = (PXA2xxState *) opaque;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    switch (addr) {
    case PMCR ... PCMD31:
        if (addr & 3)
            goto fail;

        return s->pm_regs[addr >> 2];
    default:
    fail:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
109
static void pxa2xx_pm_write(void *opaque, target_phys_addr_t addr,
110 111
                uint32_t value)
{
P
Paul Brook 已提交
112
    PXA2xxState *s = (PXA2xxState *) opaque;
113 114 115 116 117 118 119 120 121 122 123 124 125 126

    switch (addr) {
    case PMCR:
        s->pm_regs[addr >> 2] &= 0x15 & ~(value & 0x2a);
        s->pm_regs[addr >> 2] |= value & 0x15;
        break;

    case PSSR:	/* Read-clean registers */
    case RCSR:
    case PKSR:
        s->pm_regs[addr >> 2] &= ~value;
        break;

    default:	/* Read-write registers */
B
Blue Swirl 已提交
127
        if (!(addr & 3)) {
128 129 130 131 132 133 134 135 136
            s->pm_regs[addr >> 2] = value;
            break;
        }

        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
}

137
static CPUReadMemoryFunc * const pxa2xx_pm_readfn[] = {
138 139 140 141 142
    pxa2xx_pm_read,
    pxa2xx_pm_read,
    pxa2xx_pm_read,
};

143
static CPUWriteMemoryFunc * const pxa2xx_pm_writefn[] = {
144 145 146 147 148
    pxa2xx_pm_write,
    pxa2xx_pm_write,
    pxa2xx_pm_write,
};

149 150
static void pxa2xx_pm_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
151
    PXA2xxState *s = (PXA2xxState *) opaque;
152 153 154 155 156 157 158 159
    int i;

    for (i = 0; i < 0x40; i ++)
        qemu_put_be32s(f, &s->pm_regs[i]);
}

static int pxa2xx_pm_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
160
    PXA2xxState *s = (PXA2xxState *) opaque;
161 162 163 164 165 166 167 168
    int i;

    for (i = 0; i < 0x40; i ++)
        qemu_get_be32s(f, &s->pm_regs[i]);

    return 0;
}

169 170 171 172 173
#define CCCR	0x00	/* Core Clock Configuration register */
#define CKEN	0x04	/* Clock Enable register */
#define OSCC	0x08	/* Oscillator Configuration register */
#define CCSR	0x0c	/* Core Clock Status register */

A
Anthony Liguori 已提交
174
static uint32_t pxa2xx_cm_read(void *opaque, target_phys_addr_t addr)
175
{
P
Paul Brook 已提交
176
    PXA2xxState *s = (PXA2xxState *) opaque;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    switch (addr) {
    case CCCR:
    case CKEN:
    case OSCC:
        return s->cm_regs[addr >> 2];

    case CCSR:
        return s->cm_regs[CCCR >> 2] | (3 << 28);

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
194
static void pxa2xx_cm_write(void *opaque, target_phys_addr_t addr,
195 196
                uint32_t value)
{
P
Paul Brook 已提交
197
    PXA2xxState *s = (PXA2xxState *) opaque;
198 199 200 201 202 203 204 205

    switch (addr) {
    case CCCR:
    case CKEN:
        s->cm_regs[addr >> 2] = value;
        break;

    case OSCC:
206
        s->cm_regs[addr >> 2] &= ~0x6c;
207
        s->cm_regs[addr >> 2] |= value & 0x6e;
208 209
        if ((value >> 1) & 1)			/* OON */
            s->cm_regs[addr >> 2] |= 1 << 0;	/* Oscillator is now stable */
210 211 212 213 214 215 216 217
        break;

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
}

218
static CPUReadMemoryFunc * const pxa2xx_cm_readfn[] = {
219 220 221 222 223
    pxa2xx_cm_read,
    pxa2xx_cm_read,
    pxa2xx_cm_read,
};

224
static CPUWriteMemoryFunc * const pxa2xx_cm_writefn[] = {
225 226 227 228 229
    pxa2xx_cm_write,
    pxa2xx_cm_write,
    pxa2xx_cm_write,
};

230 231
static void pxa2xx_cm_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
232
    PXA2xxState *s = (PXA2xxState *) opaque;
233 234 235 236 237 238 239 240 241 242
    int i;

    for (i = 0; i < 4; i ++)
        qemu_put_be32s(f, &s->cm_regs[i]);
    qemu_put_be32s(f, &s->clkcfg);
    qemu_put_be32s(f, &s->pmnc);
}

static int pxa2xx_cm_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
243
    PXA2xxState *s = (PXA2xxState *) opaque;
244 245 246 247 248 249 250 251 252 253
    int i;

    for (i = 0; i < 4; i ++)
        qemu_get_be32s(f, &s->cm_regs[i]);
    qemu_get_be32s(f, &s->clkcfg);
    qemu_get_be32s(f, &s->pmnc);

    return 0;
}

254 255
static uint32_t pxa2xx_clkpwr_read(void *opaque, int op2, int reg, int crm)
{
P
Paul Brook 已提交
256
    PXA2xxState *s = (PXA2xxState *) opaque;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    switch (reg) {
    case 6:	/* Clock Configuration register */
        return s->clkcfg;

    case 7:	/* Power Mode register */
        return 0;

    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
    return 0;
}

static void pxa2xx_clkpwr_write(void *opaque, int op2, int reg, int crm,
                uint32_t value)
{
P
Paul Brook 已提交
275
    PXA2xxState *s = (PXA2xxState *) opaque;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    static const char *pwrmode[8] = {
        "Normal", "Idle", "Deep-idle", "Standby",
        "Sleep", "reserved (!)", "reserved (!)", "Deep-sleep",
    };

    switch (reg) {
    case 6:	/* Clock Configuration register */
        s->clkcfg = value & 0xf;
        if (value & 2)
            printf("%s: CPU frequency change attempt\n", __FUNCTION__);
        break;

    case 7:	/* Power Mode register */
        if (value & 8)
            printf("%s: CPU voltage change attempt\n", __FUNCTION__);
        switch (value & 7) {
        case 0:
            /* Do nothing */
            break;

        case 1:
            /* Idle */
B
balrog 已提交
298
            if (!(s->cm_regs[CCCR >> 2] & (1 << 31))) {	/* CPDIS */
299 300 301 302 303 304 305 306 307 308 309 310
                cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
                break;
            }
            /* Fall through.  */

        case 2:
            /* Deep-Idle */
            cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
            s->pm_regs[RCSR >> 2] |= 0x8;	/* Set GPR */
            goto message;

        case 3:
311 312
            s->env->uncached_cpsr =
                    ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
313 314
            s->env->cp15.c1_sys = 0;
            s->env->cp15.c1_coproc = 0;
P
pbrook 已提交
315
            s->env->cp15.c2_base0 = 0;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
            s->env->cp15.c3 = 0;
            s->pm_regs[PSSR >> 2] |= 0x8;	/* Set STS */
            s->pm_regs[RCSR >> 2] |= 0x8;	/* Set GPR */

            /*
             * The scratch-pad register is almost universally used
             * for storing the return address on suspend.  For the
             * lack of a resuming bootloader, perform a jump
             * directly to that address.
             */
            memset(s->env->regs, 0, 4 * 15);
            s->env->regs[15] = s->pm_regs[PSPR >> 2];

#if 0
            buffer = 0xe59ff000;	/* ldr     pc, [pc, #0] */
            cpu_physical_memory_write(0, &buffer, 4);
            buffer = s->pm_regs[PSPR >> 2];
            cpu_physical_memory_write(8, &buffer, 4);
#endif

            /* Suspend */
            cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HALT);

            goto message;

        default:
        message:
            printf("%s: machine entered %s mode\n", __FUNCTION__,
                            pwrmode[value & 7]);
        }
        break;

    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
}

/* Performace Monitoring Registers */
#define CPPMNC		0	/* Performance Monitor Control register */
#define CPCCNT		1	/* Clock Counter register */
#define CPINTEN		4	/* Interrupt Enable register */
#define CPFLAG		5	/* Overflow Flag register */
#define CPEVTSEL	8	/* Event Selection register */

#define CPPMN0		0	/* Performance Count register 0 */
#define CPPMN1		1	/* Performance Count register 1 */
#define CPPMN2		2	/* Performance Count register 2 */
#define CPPMN3		3	/* Performance Count register 3 */

static uint32_t pxa2xx_perf_read(void *opaque, int op2, int reg, int crm)
{
P
Paul Brook 已提交
368
    PXA2xxState *s = (PXA2xxState *) opaque;
369 370 371 372 373 374

    switch (reg) {
    case CPPMNC:
        return s->pmnc;
    case CPCCNT:
        if (s->pmnc & 1)
375
            return qemu_get_clock_ns(vm_clock);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        else
            return 0;
    case CPINTEN:
    case CPFLAG:
    case CPEVTSEL:
        return 0;

    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
    return 0;
}

static void pxa2xx_perf_write(void *opaque, int op2, int reg, int crm,
                uint32_t value)
{
P
Paul Brook 已提交
393
    PXA2xxState *s = (PXA2xxState *) opaque;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 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

    switch (reg) {
    case CPPMNC:
        s->pmnc = value;
        break;

    case CPCCNT:
    case CPINTEN:
    case CPFLAG:
    case CPEVTSEL:
        break;

    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
}

static uint32_t pxa2xx_cp14_read(void *opaque, int op2, int reg, int crm)
{
    switch (crm) {
    case 0:
        return pxa2xx_clkpwr_read(opaque, op2, reg, crm);
    case 1:
        return pxa2xx_perf_read(opaque, op2, reg, crm);
    case 2:
        switch (reg) {
        case CPPMN0:
        case CPPMN1:
        case CPPMN2:
        case CPPMN3:
            return 0;
        }
        /* Fall through */
    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
    return 0;
}

static void pxa2xx_cp14_write(void *opaque, int op2, int reg, int crm,
                uint32_t value)
{
    switch (crm) {
    case 0:
        pxa2xx_clkpwr_write(opaque, op2, reg, crm, value);
        break;
    case 1:
        pxa2xx_perf_write(opaque, op2, reg, crm, value);
        break;
    case 2:
        switch (reg) {
        case CPPMN0:
        case CPPMN1:
        case CPPMN2:
        case CPPMN3:
            return;
        }
        /* Fall through */
    default:
        printf("%s: Bad register 0x%x\n", __FUNCTION__, reg);
        break;
    }
}

#define MDCNFG		0x00	/* SDRAM Configuration register */
#define MDREFR		0x04	/* SDRAM Refresh Control register */
#define MSC0		0x08	/* Static Memory Control register 0 */
#define MSC1		0x0c	/* Static Memory Control register 1 */
#define MSC2		0x10	/* Static Memory Control register 2 */
#define MECR		0x14	/* Expansion Memory Bus Config register */
#define SXCNFG		0x1c	/* Synchronous Static Memory Config register */
#define MCMEM0		0x28	/* PC Card Memory Socket 0 Timing register */
#define MCMEM1		0x2c	/* PC Card Memory Socket 1 Timing register */
#define MCATT0		0x30	/* PC Card Attribute Socket 0 register */
#define MCATT1		0x34	/* PC Card Attribute Socket 1 register */
#define MCIO0		0x38	/* PC Card I/O Socket 0 Timing register */
#define MCIO1		0x3c	/* PC Card I/O Socket 1 Timing register */
#define MDMRS		0x40	/* SDRAM Mode Register Set Config register */
#define BOOT_DEF	0x44	/* Boot-time Default Configuration register */
#define ARB_CNTL	0x48	/* Arbiter Control register */
#define BSCNTR0		0x4c	/* Memory Buffer Strength Control register 0 */
#define BSCNTR1		0x50	/* Memory Buffer Strength Control register 1 */
#define LCDBSCNTR	0x54	/* LCD Buffer Strength Control register */
#define MDMRSLP		0x58	/* Low Power SDRAM Mode Set Config register */
#define BSCNTR2		0x5c	/* Memory Buffer Strength Control register 2 */
#define BSCNTR3		0x60	/* Memory Buffer Strength Control register 3 */
#define SA1110		0x64	/* SA-1110 Memory Compatibility register */

A
Anthony Liguori 已提交
484
static uint32_t pxa2xx_mm_read(void *opaque, target_phys_addr_t addr)
485
{
P
Paul Brook 已提交
486
    PXA2xxState *s = (PXA2xxState *) opaque;
487 488 489 490 491 492 493 494 495 496 497 498 499

    switch (addr) {
    case MDCNFG ... SA1110:
        if ((addr & 3) == 0)
            return s->mm_regs[addr >> 2];

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
500
static void pxa2xx_mm_write(void *opaque, target_phys_addr_t addr,
501 502
                uint32_t value)
{
P
Paul Brook 已提交
503
    PXA2xxState *s = (PXA2xxState *) opaque;
504 505 506 507 508 509 510 511 512 513 514 515 516 517

    switch (addr) {
    case MDCNFG ... SA1110:
        if ((addr & 3) == 0) {
            s->mm_regs[addr >> 2] = value;
            break;
        }

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
}

518
static CPUReadMemoryFunc * const pxa2xx_mm_readfn[] = {
519 520 521 522 523
    pxa2xx_mm_read,
    pxa2xx_mm_read,
    pxa2xx_mm_read,
};

524
static CPUWriteMemoryFunc * const pxa2xx_mm_writefn[] = {
525 526 527 528 529
    pxa2xx_mm_write,
    pxa2xx_mm_write,
    pxa2xx_mm_write,
};

530 531
static void pxa2xx_mm_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
532
    PXA2xxState *s = (PXA2xxState *) opaque;
533 534 535 536 537 538 539 540
    int i;

    for (i = 0; i < 0x1a; i ++)
        qemu_put_be32s(f, &s->mm_regs[i]);
}

static int pxa2xx_mm_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
541
    PXA2xxState *s = (PXA2xxState *) opaque;
542 543 544 545 546 547 548 549
    int i;

    for (i = 0; i < 0x1a; i ++)
        qemu_get_be32s(f, &s->mm_regs[i]);

    return 0;
}

550
/* Synchronous Serial Ports */
P
Paul Brook 已提交
551 552
typedef struct {
    SysBusDevice busdev;
553 554
    qemu_irq irq;
    int enable;
P
Paul Brook 已提交
555
    SSIBus *bus;
556 557 558 559 560 561 562 563 564 565 566 567 568

    uint32_t sscr[2];
    uint32_t sspsp;
    uint32_t ssto;
    uint32_t ssitr;
    uint32_t sssr;
    uint8_t sstsa;
    uint8_t ssrsa;
    uint8_t ssacd;

    uint32_t rx_fifo[16];
    int rx_level;
    int rx_start;
P
Paul Brook 已提交
569
} PXA2xxSSPState;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 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

#define SSCR0	0x00	/* SSP Control register 0 */
#define SSCR1	0x04	/* SSP Control register 1 */
#define SSSR	0x08	/* SSP Status register */
#define SSITR	0x0c	/* SSP Interrupt Test register */
#define SSDR	0x10	/* SSP Data register */
#define SSTO	0x28	/* SSP Time-Out register */
#define SSPSP	0x2c	/* SSP Programmable Serial Protocol register */
#define SSTSA	0x30	/* SSP TX Time Slot Active register */
#define SSRSA	0x34	/* SSP RX Time Slot Active register */
#define SSTSS	0x38	/* SSP Time Slot Status register */
#define SSACD	0x3c	/* SSP Audio Clock Divider register */

/* Bitfields for above registers */
#define SSCR0_SPI(x)	(((x) & 0x30) == 0x00)
#define SSCR0_SSP(x)	(((x) & 0x30) == 0x10)
#define SSCR0_UWIRE(x)	(((x) & 0x30) == 0x20)
#define SSCR0_PSP(x)	(((x) & 0x30) == 0x30)
#define SSCR0_SSE	(1 << 7)
#define SSCR0_RIM	(1 << 22)
#define SSCR0_TIM	(1 << 23)
#define SSCR0_MOD	(1 << 31)
#define SSCR0_DSS(x)	(((((x) >> 16) & 0x10) | ((x) & 0xf)) + 1)
#define SSCR1_RIE	(1 << 0)
#define SSCR1_TIE	(1 << 1)
#define SSCR1_LBM	(1 << 2)
#define SSCR1_MWDS	(1 << 5)
#define SSCR1_TFT(x)	((((x) >> 6) & 0xf) + 1)
#define SSCR1_RFT(x)	((((x) >> 10) & 0xf) + 1)
#define SSCR1_EFWR	(1 << 14)
#define SSCR1_PINTE	(1 << 18)
#define SSCR1_TINTE	(1 << 19)
#define SSCR1_RSRE	(1 << 20)
#define SSCR1_TSRE	(1 << 21)
#define SSCR1_EBCEI	(1 << 29)
#define SSITR_INT	(7 << 5)
#define SSSR_TNF	(1 << 2)
#define SSSR_RNE	(1 << 3)
#define SSSR_TFS	(1 << 5)
#define SSSR_RFS	(1 << 6)
#define SSSR_ROR	(1 << 7)
#define SSSR_PINT	(1 << 18)
#define SSSR_TINT	(1 << 19)
#define SSSR_EOC	(1 << 20)
#define SSSR_TUR	(1 << 21)
#define SSSR_BCE	(1 << 23)
#define SSSR_RW		0x00bc0080

P
Paul Brook 已提交
618
static void pxa2xx_ssp_int_update(PXA2xxSSPState *s)
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
{
    int level = 0;

    level |= s->ssitr & SSITR_INT;
    level |= (s->sssr & SSSR_BCE)  &&  (s->sscr[1] & SSCR1_EBCEI);
    level |= (s->sssr & SSSR_TUR)  && !(s->sscr[0] & SSCR0_TIM);
    level |= (s->sssr & SSSR_EOC)  &&  (s->sssr & (SSSR_TINT | SSSR_PINT));
    level |= (s->sssr & SSSR_TINT) &&  (s->sscr[1] & SSCR1_TINTE);
    level |= (s->sssr & SSSR_PINT) &&  (s->sscr[1] & SSCR1_PINTE);
    level |= (s->sssr & SSSR_ROR)  && !(s->sscr[0] & SSCR0_RIM);
    level |= (s->sssr & SSSR_RFS)  &&  (s->sscr[1] & SSCR1_RIE);
    level |= (s->sssr & SSSR_TFS)  &&  (s->sscr[1] & SSCR1_TIE);
    qemu_set_irq(s->irq, !!level);
}

P
Paul Brook 已提交
634
static void pxa2xx_ssp_fifo_update(PXA2xxSSPState *s)
635 636 637
{
    s->sssr &= ~(0xf << 12);	/* Clear RFL */
    s->sssr &= ~(0xf << 8);	/* Clear TFL */
B
Blue Swirl 已提交
638
    s->sssr &= ~SSSR_TFS;
639 640 641 642 643 644 645 646 647 648 649
    s->sssr &= ~SSSR_TNF;
    if (s->enable) {
        s->sssr |= ((s->rx_level - 1) & 0xf) << 12;
        if (s->rx_level >= SSCR1_RFT(s->sscr[1]))
            s->sssr |= SSSR_RFS;
        else
            s->sssr &= ~SSSR_RFS;
        if (s->rx_level)
            s->sssr |= SSSR_RNE;
        else
            s->sssr &= ~SSSR_RNE;
B
Blue Swirl 已提交
650 651 652
        /* TX FIFO is never filled, so it is always in underrun
           condition if SSP is enabled */
        s->sssr |= SSSR_TFS;
653 654 655 656 657 658
        s->sssr |= SSSR_TNF;
    }

    pxa2xx_ssp_int_update(s);
}

A
Anthony Liguori 已提交
659
static uint32_t pxa2xx_ssp_read(void *opaque, target_phys_addr_t addr)
660
{
P
Paul Brook 已提交
661
    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
662 663 664 665 666 667 668 669 670 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
    uint32_t retval;

    switch (addr) {
    case SSCR0:
        return s->sscr[0];
    case SSCR1:
        return s->sscr[1];
    case SSPSP:
        return s->sspsp;
    case SSTO:
        return s->ssto;
    case SSITR:
        return s->ssitr;
    case SSSR:
        return s->sssr | s->ssitr;
    case SSDR:
        if (!s->enable)
            return 0xffffffff;
        if (s->rx_level < 1) {
            printf("%s: SSP Rx Underrun\n", __FUNCTION__);
            return 0xffffffff;
        }
        s->rx_level --;
        retval = s->rx_fifo[s->rx_start ++];
        s->rx_start &= 0xf;
        pxa2xx_ssp_fifo_update(s);
        return retval;
    case SSTSA:
        return s->sstsa;
    case SSRSA:
        return s->ssrsa;
    case SSTSS:
        return 0;
    case SSACD:
        return s->ssacd;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
704
static void pxa2xx_ssp_write(void *opaque, target_phys_addr_t addr,
705 706
                uint32_t value)
{
P
Paul Brook 已提交
707
    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

    switch (addr) {
    case SSCR0:
        s->sscr[0] = value & 0xc7ffffff;
        s->enable = value & SSCR0_SSE;
        if (value & SSCR0_MOD)
            printf("%s: Attempt to use network mode\n", __FUNCTION__);
        if (s->enable && SSCR0_DSS(value) < 4)
            printf("%s: Wrong data size: %i bits\n", __FUNCTION__,
                            SSCR0_DSS(value));
        if (!(value & SSCR0_SSE)) {
            s->sssr = 0;
            s->ssitr = 0;
            s->rx_level = 0;
        }
        pxa2xx_ssp_fifo_update(s);
        break;

    case SSCR1:
        s->sscr[1] = value;
        if (value & (SSCR1_LBM | SSCR1_EFWR))
            printf("%s: Attempt to use SSP test mode\n", __FUNCTION__);
        pxa2xx_ssp_fifo_update(s);
        break;

    case SSPSP:
        s->sspsp = value;
        break;

    case SSTO:
        s->ssto = value;
        break;

    case SSITR:
        s->ssitr = value & SSITR_INT;
        pxa2xx_ssp_int_update(s);
        break;

    case SSSR:
        s->sssr &= ~(value & SSSR_RW);
        pxa2xx_ssp_int_update(s);
        break;

    case SSDR:
        if (SSCR0_UWIRE(s->sscr[0])) {
            if (s->sscr[1] & SSCR1_MWDS)
                value &= 0xffff;
            else
                value &= 0xff;
        } else
            /* Note how 32bits overflow does no harm here */
            value &= (1 << SSCR0_DSS(s->sscr[0])) - 1;

        /* Data goes from here to the Tx FIFO and is shifted out from
         * there directly to the slave, no need to buffer it.
         */
        if (s->enable) {
P
Paul Brook 已提交
765 766
            uint32_t readval;
            readval = ssi_transfer(s->bus, value);
767
            if (s->rx_level < 0x10) {
P
Paul Brook 已提交
768 769
                s->rx_fifo[(s->rx_start + s->rx_level ++) & 0xf] = readval;
            } else {
770
                s->sssr |= SSSR_ROR;
P
Paul Brook 已提交
771
            }
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
        }
        pxa2xx_ssp_fifo_update(s);
        break;

    case SSTSA:
        s->sstsa = value;
        break;

    case SSRSA:
        s->ssrsa = value;
        break;

    case SSACD:
        s->ssacd = value;
        break;

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
}

794
static CPUReadMemoryFunc * const pxa2xx_ssp_readfn[] = {
795 796 797 798 799
    pxa2xx_ssp_read,
    pxa2xx_ssp_read,
    pxa2xx_ssp_read,
};

800
static CPUWriteMemoryFunc * const pxa2xx_ssp_writefn[] = {
801 802 803 804 805
    pxa2xx_ssp_write,
    pxa2xx_ssp_write,
    pxa2xx_ssp_write,
};

806 807
static void pxa2xx_ssp_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
808
    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    int i;

    qemu_put_be32(f, s->enable);

    qemu_put_be32s(f, &s->sscr[0]);
    qemu_put_be32s(f, &s->sscr[1]);
    qemu_put_be32s(f, &s->sspsp);
    qemu_put_be32s(f, &s->ssto);
    qemu_put_be32s(f, &s->ssitr);
    qemu_put_be32s(f, &s->sssr);
    qemu_put_8s(f, &s->sstsa);
    qemu_put_8s(f, &s->ssrsa);
    qemu_put_8s(f, &s->ssacd);

    qemu_put_byte(f, s->rx_level);
    for (i = 0; i < s->rx_level; i ++)
        qemu_put_byte(f, s->rx_fifo[(s->rx_start + i) & 0xf]);
}

static int pxa2xx_ssp_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
830
    PXA2xxSSPState *s = (PXA2xxSSPState *) opaque;
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    int i;

    s->enable = qemu_get_be32(f);

    qemu_get_be32s(f, &s->sscr[0]);
    qemu_get_be32s(f, &s->sscr[1]);
    qemu_get_be32s(f, &s->sspsp);
    qemu_get_be32s(f, &s->ssto);
    qemu_get_be32s(f, &s->ssitr);
    qemu_get_be32s(f, &s->sssr);
    qemu_get_8s(f, &s->sstsa);
    qemu_get_8s(f, &s->ssrsa);
    qemu_get_8s(f, &s->ssacd);

    s->rx_level = qemu_get_byte(f);
    s->rx_start = 0;
    for (i = 0; i < s->rx_level; i ++)
        s->rx_fifo[i] = qemu_get_byte(f);

    return 0;
}

853
static int pxa2xx_ssp_init(SysBusDevice *dev)
P
Paul Brook 已提交
854 855 856 857 858 859
{
    int iomemtype;
    PXA2xxSSPState *s = FROM_SYSBUS(PXA2xxSSPState, dev);

    sysbus_init_irq(dev, &s->irq);

860
    iomemtype = cpu_register_io_memory(pxa2xx_ssp_readfn,
861 862
                                       pxa2xx_ssp_writefn, s,
                                       DEVICE_NATIVE_ENDIAN);
P
Paul Brook 已提交
863
    sysbus_init_mmio(dev, 0x1000, iomemtype);
A
Alex Williamson 已提交
864
    register_savevm(&dev->qdev, "pxa2xx_ssp", -1, 0,
P
Paul Brook 已提交
865 866
                    pxa2xx_ssp_save, pxa2xx_ssp_load, s);

P
Paul Brook 已提交
867
    s->bus = ssi_create_bus(&dev->qdev, "ssi");
868
    return 0;
P
Paul Brook 已提交
869 870
}

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
/* Real-Time Clock */
#define RCNR		0x00	/* RTC Counter register */
#define RTAR		0x04	/* RTC Alarm register */
#define RTSR		0x08	/* RTC Status register */
#define RTTR		0x0c	/* RTC Timer Trim register */
#define RDCR		0x10	/* RTC Day Counter register */
#define RYCR		0x14	/* RTC Year Counter register */
#define RDAR1		0x18	/* RTC Wristwatch Day Alarm register 1 */
#define RYAR1		0x1c	/* RTC Wristwatch Year Alarm register 1 */
#define RDAR2		0x20	/* RTC Wristwatch Day Alarm register 2 */
#define RYAR2		0x24	/* RTC Wristwatch Year Alarm register 2 */
#define SWCR		0x28	/* RTC Stopwatch Counter register */
#define SWAR1		0x2c	/* RTC Stopwatch Alarm register 1 */
#define SWAR2		0x30	/* RTC Stopwatch Alarm register 2 */
#define RTCPICR		0x34	/* RTC Periodic Interrupt Counter register */
#define PIAR		0x38	/* RTC Periodic Interrupt Alarm register */

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
typedef struct {
    SysBusDevice busdev;
    uint32_t rttr;
    uint32_t rtsr;
    uint32_t rtar;
    uint32_t rdar1;
    uint32_t rdar2;
    uint32_t ryar1;
    uint32_t ryar2;
    uint32_t swar1;
    uint32_t swar2;
    uint32_t piar;
    uint32_t last_rcnr;
    uint32_t last_rdcr;
    uint32_t last_rycr;
    uint32_t last_swcr;
    uint32_t last_rtcpicr;
    int64_t last_hz;
    int64_t last_sw;
    int64_t last_pi;
    QEMUTimer *rtc_hz;
    QEMUTimer *rtc_rdal1;
    QEMUTimer *rtc_rdal2;
    QEMUTimer *rtc_swal1;
    QEMUTimer *rtc_swal2;
    QEMUTimer *rtc_pi;
    qemu_irq rtc_irq;
} PXA2xxRTCState;

static inline void pxa2xx_rtc_int_update(PXA2xxRTCState *s)
918
{
919
    qemu_set_irq(s->rtc_irq, !!(s->rtsr & 0x2553));
920 921
}

922
static void pxa2xx_rtc_hzupdate(PXA2xxRTCState *s)
923
{
924
    int64_t rt = qemu_get_clock_ms(rt_clock);
925 926 927 928 929 930 931
    s->last_rcnr += ((rt - s->last_hz) << 15) /
            (1000 * ((s->rttr & 0xffff) + 1));
    s->last_rdcr += ((rt - s->last_hz) << 15) /
            (1000 * ((s->rttr & 0xffff) + 1));
    s->last_hz = rt;
}

932
static void pxa2xx_rtc_swupdate(PXA2xxRTCState *s)
933
{
934
    int64_t rt = qemu_get_clock_ms(rt_clock);
935 936 937 938 939
    if (s->rtsr & (1 << 12))
        s->last_swcr += (rt - s->last_sw) / 10;
    s->last_sw = rt;
}

940
static void pxa2xx_rtc_piupdate(PXA2xxRTCState *s)
941
{
942
    int64_t rt = qemu_get_clock_ms(rt_clock);
943 944 945 946 947
    if (s->rtsr & (1 << 15))
        s->last_swcr += rt - s->last_pi;
    s->last_pi = rt;
}

948
static inline void pxa2xx_rtc_alarm_update(PXA2xxRTCState *s,
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
                uint32_t rtsr)
{
    if ((rtsr & (1 << 2)) && !(rtsr & (1 << 0)))
        qemu_mod_timer(s->rtc_hz, s->last_hz +
                (((s->rtar - s->last_rcnr) * 1000 *
                  ((s->rttr & 0xffff) + 1)) >> 15));
    else
        qemu_del_timer(s->rtc_hz);

    if ((rtsr & (1 << 5)) && !(rtsr & (1 << 4)))
        qemu_mod_timer(s->rtc_rdal1, s->last_hz +
                (((s->rdar1 - s->last_rdcr) * 1000 *
                  ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
    else
        qemu_del_timer(s->rtc_rdal1);

    if ((rtsr & (1 << 7)) && !(rtsr & (1 << 6)))
        qemu_mod_timer(s->rtc_rdal2, s->last_hz +
                (((s->rdar2 - s->last_rdcr) * 1000 *
                  ((s->rttr & 0xffff) + 1)) >> 15)); /* TODO: fixup */
    else
        qemu_del_timer(s->rtc_rdal2);

    if ((rtsr & 0x1200) == 0x1200 && !(rtsr & (1 << 8)))
        qemu_mod_timer(s->rtc_swal1, s->last_sw +
                        (s->swar1 - s->last_swcr) * 10); /* TODO: fixup */
    else
        qemu_del_timer(s->rtc_swal1);

    if ((rtsr & 0x1800) == 0x1800 && !(rtsr & (1 << 10)))
        qemu_mod_timer(s->rtc_swal2, s->last_sw +
                        (s->swar2 - s->last_swcr) * 10); /* TODO: fixup */
    else
        qemu_del_timer(s->rtc_swal2);

    if ((rtsr & 0xc000) == 0xc000 && !(rtsr & (1 << 13)))
        qemu_mod_timer(s->rtc_pi, s->last_pi +
                        (s->piar & 0xffff) - s->last_rtcpicr);
    else
        qemu_del_timer(s->rtc_pi);
}

static inline void pxa2xx_rtc_hz_tick(void *opaque)
{
993
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
994 995 996 997 998 999 1000
    s->rtsr |= (1 << 0);
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

static inline void pxa2xx_rtc_rdal1_tick(void *opaque)
{
1001
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1002 1003 1004 1005 1006 1007 1008
    s->rtsr |= (1 << 4);
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

static inline void pxa2xx_rtc_rdal2_tick(void *opaque)
{
1009
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1010 1011 1012 1013 1014 1015 1016
    s->rtsr |= (1 << 6);
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

static inline void pxa2xx_rtc_swal1_tick(void *opaque)
{
1017
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1018 1019 1020 1021 1022 1023 1024
    s->rtsr |= (1 << 8);
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

static inline void pxa2xx_rtc_swal2_tick(void *opaque)
{
1025
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1026 1027 1028 1029 1030 1031 1032
    s->rtsr |= (1 << 10);
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

static inline void pxa2xx_rtc_pi_tick(void *opaque)
{
1033
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1034 1035 1036 1037 1038 1039 1040
    s->rtsr |= (1 << 13);
    pxa2xx_rtc_piupdate(s);
    s->last_rtcpicr = 0;
    pxa2xx_rtc_alarm_update(s, s->rtsr);
    pxa2xx_rtc_int_update(s);
}

A
Anthony Liguori 已提交
1041
static uint32_t pxa2xx_rtc_read(void *opaque, target_phys_addr_t addr)
1042
{
1043
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

    switch (addr) {
    case RTTR:
        return s->rttr;
    case RTSR:
        return s->rtsr;
    case RTAR:
        return s->rtar;
    case RDAR1:
        return s->rdar1;
    case RDAR2:
        return s->rdar2;
    case RYAR1:
        return s->ryar1;
    case RYAR2:
        return s->ryar2;
    case SWAR1:
        return s->swar1;
    case SWAR2:
        return s->swar2;
    case PIAR:
        return s->piar;
    case RCNR:
1067
        return s->last_rcnr + ((qemu_get_clock_ms(rt_clock) - s->last_hz) << 15) /
1068 1069
                (1000 * ((s->rttr & 0xffff) + 1));
    case RDCR:
1070
        return s->last_rdcr + ((qemu_get_clock_ms(rt_clock) - s->last_hz) << 15) /
1071 1072 1073 1074 1075
                (1000 * ((s->rttr & 0xffff) + 1));
    case RYCR:
        return s->last_rycr;
    case SWCR:
        if (s->rtsr & (1 << 12))
1076
            return s->last_swcr + (qemu_get_clock_ms(rt_clock) - s->last_sw) / 10;
1077 1078 1079 1080 1081 1082 1083 1084 1085
        else
            return s->last_swcr;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
1086
static void pxa2xx_rtc_write(void *opaque, target_phys_addr_t addr,
1087 1088
                uint32_t value)
{
1089
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
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 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

    switch (addr) {
    case RTTR:
        if (!(s->rttr & (1 << 31))) {
            pxa2xx_rtc_hzupdate(s);
            s->rttr = value;
            pxa2xx_rtc_alarm_update(s, s->rtsr);
        }
        break;

    case RTSR:
        if ((s->rtsr ^ value) & (1 << 15))
            pxa2xx_rtc_piupdate(s);

        if ((s->rtsr ^ value) & (1 << 12))
            pxa2xx_rtc_swupdate(s);

        if (((s->rtsr ^ value) & 0x4aac) | (value & ~0xdaac))
            pxa2xx_rtc_alarm_update(s, value);

        s->rtsr = (value & 0xdaac) | (s->rtsr & ~(value & ~0xdaac));
        pxa2xx_rtc_int_update(s);
        break;

    case RTAR:
        s->rtar = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RDAR1:
        s->rdar1 = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RDAR2:
        s->rdar2 = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RYAR1:
        s->ryar1 = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RYAR2:
        s->ryar2 = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case SWAR1:
        pxa2xx_rtc_swupdate(s);
        s->swar1 = value;
        s->last_swcr = 0;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case SWAR2:
        s->swar2 = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case PIAR:
        s->piar = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RCNR:
        pxa2xx_rtc_hzupdate(s);
        s->last_rcnr = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RDCR:
        pxa2xx_rtc_hzupdate(s);
        s->last_rdcr = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RYCR:
        s->last_rycr = value;
        break;

    case SWCR:
        pxa2xx_rtc_swupdate(s);
        s->last_swcr = value;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    case RTCPICR:
        pxa2xx_rtc_piupdate(s);
        s->last_rtcpicr = value & 0xffff;
        pxa2xx_rtc_alarm_update(s, s->rtsr);
        break;

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
    }
}

1189
static CPUReadMemoryFunc * const pxa2xx_rtc_readfn[] = {
1190 1191 1192 1193 1194
    pxa2xx_rtc_read,
    pxa2xx_rtc_read,
    pxa2xx_rtc_read,
};

1195
static CPUWriteMemoryFunc * const pxa2xx_rtc_writefn[] = {
1196 1197 1198 1199 1200
    pxa2xx_rtc_write,
    pxa2xx_rtc_write,
    pxa2xx_rtc_write,
};

1201
static int pxa2xx_rtc_init(SysBusDevice *dev)
1202
{
1203
    PXA2xxRTCState *s = FROM_SYSBUS(PXA2xxRTCState, dev);
1204
    struct tm tm;
1205
    int wom;
1206
    int iomemtype;
1207 1208 1209 1210

    s->rttr = 0x7fff;
    s->rtsr = 0;

1211 1212 1213
    qemu_get_timedate(&tm, 0);
    wom = ((tm.tm_mday - 1) / 7) + 1;

A
aurel32 已提交
1214
    s->last_rcnr = (uint32_t) mktimegm(&tm);
1215 1216 1217 1218 1219 1220
    s->last_rdcr = (wom << 20) | ((tm.tm_wday + 1) << 17) |
            (tm.tm_hour << 12) | (tm.tm_min << 6) | tm.tm_sec;
    s->last_rycr = ((tm.tm_year + 1900) << 9) |
            ((tm.tm_mon + 1) << 5) | tm.tm_mday;
    s->last_swcr = (tm.tm_hour << 19) |
            (tm.tm_min << 13) | (tm.tm_sec << 7);
1221
    s->last_rtcpicr = 0;
1222 1223 1224 1225 1226 1227 1228 1229
    s->last_hz = s->last_sw = s->last_pi = qemu_get_clock_ms(rt_clock);

    s->rtc_hz    = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_hz_tick,    s);
    s->rtc_rdal1 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_rdal1_tick, s);
    s->rtc_rdal2 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_rdal2_tick, s);
    s->rtc_swal1 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_swal1_tick, s);
    s->rtc_swal2 = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_swal2_tick, s);
    s->rtc_pi    = qemu_new_timer_ms(rt_clock, pxa2xx_rtc_pi_tick,    s);
1230

1231 1232 1233 1234 1235 1236 1237
    sysbus_init_irq(dev, &s->rtc_irq);

    iomemtype = cpu_register_io_memory(pxa2xx_rtc_readfn,
                    pxa2xx_rtc_writefn, s, DEVICE_NATIVE_ENDIAN);
    sysbus_init_mmio(dev, 0x10000, iomemtype);

    return 0;
1238 1239
}

1240
static void pxa2xx_rtc_pre_save(void *opaque)
1241
{
1242
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1243

1244 1245 1246
    pxa2xx_rtc_hzupdate(s);
    pxa2xx_rtc_piupdate(s);
    pxa2xx_rtc_swupdate(s);
1247
}
1248

1249
static int pxa2xx_rtc_post_load(void *opaque, int version_id)
1250
{
1251
    PXA2xxRTCState *s = (PXA2xxRTCState *) opaque;
1252 1253 1254 1255 1256

    pxa2xx_rtc_alarm_update(s, s->rtsr);

    return 0;
}
1257

1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
static const VMStateDescription vmstate_pxa2xx_rtc_regs = {
    .name = "pxa2xx_rtc",
    .version_id = 0,
    .minimum_version_id = 0,
    .minimum_version_id_old = 0,
    .pre_save = pxa2xx_rtc_pre_save,
    .post_load = pxa2xx_rtc_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(rttr, PXA2xxRTCState),
        VMSTATE_UINT32(rtsr, PXA2xxRTCState),
        VMSTATE_UINT32(rtar, PXA2xxRTCState),
        VMSTATE_UINT32(rdar1, PXA2xxRTCState),
        VMSTATE_UINT32(rdar2, PXA2xxRTCState),
        VMSTATE_UINT32(ryar1, PXA2xxRTCState),
        VMSTATE_UINT32(ryar2, PXA2xxRTCState),
        VMSTATE_UINT32(swar1, PXA2xxRTCState),
        VMSTATE_UINT32(swar2, PXA2xxRTCState),
        VMSTATE_UINT32(piar, PXA2xxRTCState),
        VMSTATE_UINT32(last_rcnr, PXA2xxRTCState),
        VMSTATE_UINT32(last_rdcr, PXA2xxRTCState),
        VMSTATE_UINT32(last_rycr, PXA2xxRTCState),
        VMSTATE_UINT32(last_swcr, PXA2xxRTCState),
        VMSTATE_UINT32(last_rtcpicr, PXA2xxRTCState),
        VMSTATE_INT64(last_hz, PXA2xxRTCState),
        VMSTATE_INT64(last_sw, PXA2xxRTCState),
        VMSTATE_INT64(last_pi, PXA2xxRTCState),
        VMSTATE_END_OF_LIST(),
    },
};

static SysBusDeviceInfo pxa2xx_rtc_sysbus_info = {
    .init       = pxa2xx_rtc_init,
    .qdev.name  = "pxa2xx_rtc",
    .qdev.desc  = "PXA2xx RTC Controller",
    .qdev.size  = sizeof(PXA2xxRTCState),
    .qdev.vmsd  = &vmstate_pxa2xx_rtc_regs,
};

1296
/* I2C Interface */
P
Paul Brook 已提交
1297 1298 1299 1300 1301
typedef struct {
    i2c_slave i2c;
    PXA2xxI2CState *host;
} PXA2xxI2CSlaveState;

P
Paul Brook 已提交
1302
struct PXA2xxI2CState {
1303
    SysBusDevice busdev;
P
Paul Brook 已提交
1304
    PXA2xxI2CSlaveState *slave;
1305 1306
    i2c_bus *bus;
    qemu_irq irq;
1307 1308
    uint32_t offset;
    uint32_t region_size;
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

    uint16_t control;
    uint16_t status;
    uint8_t ibmr;
    uint8_t data;
};

#define IBMR	0x80	/* I2C Bus Monitor register */
#define IDBR	0x88	/* I2C Data Buffer register */
#define ICR	0x90	/* I2C Control register */
#define ISR	0x98	/* I2C Status register */
#define ISAR	0xa0	/* I2C Slave Address register */

P
Paul Brook 已提交
1322
static void pxa2xx_i2c_update(PXA2xxI2CState *s)
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
{
    uint16_t level = 0;
    level |= s->status & s->control & (1 << 10);		/* BED */
    level |= (s->status & (1 << 7)) && (s->control & (1 << 9));	/* IRF */
    level |= (s->status & (1 << 6)) && (s->control & (1 << 8));	/* ITE */
    level |= s->status & (1 << 9);				/* SAD */
    qemu_set_irq(s->irq, !!level);
}

/* These are only stubs now.  */
static void pxa2xx_i2c_event(i2c_slave *i2c, enum i2c_event event)
{
P
Paul Brook 已提交
1335 1336
    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
    PXA2xxI2CState *s = slave->host;
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

    switch (event) {
    case I2C_START_SEND:
        s->status |= (1 << 9);				/* set SAD */
        s->status &= ~(1 << 0);				/* clear RWM */
        break;
    case I2C_START_RECV:
        s->status |= (1 << 9);				/* set SAD */
        s->status |= 1 << 0;				/* set RWM */
        break;
    case I2C_FINISH:
        s->status |= (1 << 4);				/* set SSD */
        break;
    case I2C_NACK:
        s->status |= 1 << 1;				/* set ACKNAK */
        break;
    }
    pxa2xx_i2c_update(s);
}

static int pxa2xx_i2c_rx(i2c_slave *i2c)
{
P
Paul Brook 已提交
1359 1360
    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
    PXA2xxI2CState *s = slave->host;
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
        return 0;

    if (s->status & (1 << 0)) {			/* RWM */
        s->status |= 1 << 6;			/* set ITE */
    }
    pxa2xx_i2c_update(s);

    return s->data;
}

static int pxa2xx_i2c_tx(i2c_slave *i2c, uint8_t data)
{
P
Paul Brook 已提交
1374 1375
    PXA2xxI2CSlaveState *slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, i2c);
    PXA2xxI2CState *s = slave->host;
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    if ((s->control & (1 << 14)) || !(s->control & (1 << 6)))
        return 1;

    if (!(s->status & (1 << 0))) {		/* RWM */
        s->status |= 1 << 7;			/* set IRF */
        s->data = data;
    }
    pxa2xx_i2c_update(s);

    return 1;
}

A
Anthony Liguori 已提交
1388
static uint32_t pxa2xx_i2c_read(void *opaque, target_phys_addr_t addr)
1389
{
P
Paul Brook 已提交
1390
    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1391

1392
    addr -= s->offset;
1393 1394 1395 1396 1397 1398
    switch (addr) {
    case ICR:
        return s->control;
    case ISR:
        return s->status | (i2c_bus_busy(s->bus) << 2);
    case ISAR:
P
Paul Brook 已提交
1399
        return s->slave->i2c.address;
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    case IDBR:
        return s->data;
    case IBMR:
        if (s->status & (1 << 2))
            s->ibmr ^= 3;	/* Fake SCL and SDA pin changes */
        else
            s->ibmr = 0;
        return s->ibmr;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
1415
static void pxa2xx_i2c_write(void *opaque, target_phys_addr_t addr,
1416 1417
                uint32_t value)
{
P
Paul Brook 已提交
1418
    PXA2xxI2CState *s = (PXA2xxI2CState *) opaque;
1419 1420
    int ack;

1421
    addr -= s->offset;
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
    switch (addr) {
    case ICR:
        s->control = value & 0xfff7;
        if ((value & (1 << 3)) && (value & (1 << 6))) {	/* TB and IUE */
            /* TODO: slave mode */
            if (value & (1 << 0)) {			/* START condition */
                if (s->data & 1)
                    s->status |= 1 << 0;		/* set RWM */
                else
                    s->status &= ~(1 << 0);		/* clear RWM */
                ack = !i2c_start_transfer(s->bus, s->data >> 1, s->data & 1);
            } else {
                if (s->status & (1 << 0)) {		/* RWM */
                    s->data = i2c_recv(s->bus);
                    if (value & (1 << 2))		/* ACKNAK */
                        i2c_nack(s->bus);
                    ack = 1;
                } else
                    ack = !i2c_send(s->bus, s->data);
            }

            if (value & (1 << 1))			/* STOP condition */
                i2c_end_transfer(s->bus);

            if (ack) {
                if (value & (1 << 0))			/* START condition */
                    s->status |= 1 << 6;		/* set ITE */
                else
                    if (s->status & (1 << 0))		/* RWM */
                        s->status |= 1 << 7;		/* set IRF */
                    else
                        s->status |= 1 << 6;		/* set ITE */
                s->status &= ~(1 << 1);			/* clear ACKNAK */
            } else {
                s->status |= 1 << 6;			/* set ITE */
                s->status |= 1 << 10;			/* set BED */
                s->status |= 1 << 1;			/* set ACKNAK */
            }
        }
        if (!(value & (1 << 3)) && (value & (1 << 6)))	/* !TB and IUE */
            if (value & (1 << 4))			/* MA */
                i2c_end_transfer(s->bus);
        pxa2xx_i2c_update(s);
        break;

    case ISR:
        s->status &= ~(value & 0x07f0);
        pxa2xx_i2c_update(s);
        break;

    case ISAR:
P
Paul Brook 已提交
1473
        i2c_set_slave_address(&s->slave->i2c, value & 0x7f);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
        break;

    case IDBR:
        s->data = value & 0xff;
        break;

    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
    }
}

1485
static CPUReadMemoryFunc * const pxa2xx_i2c_readfn[] = {
1486 1487 1488 1489 1490
    pxa2xx_i2c_read,
    pxa2xx_i2c_read,
    pxa2xx_i2c_read,
};

1491
static CPUWriteMemoryFunc * const pxa2xx_i2c_writefn[] = {
1492 1493 1494 1495 1496
    pxa2xx_i2c_write,
    pxa2xx_i2c_write,
    pxa2xx_i2c_write,
};

J
Juan Quintela 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
static const VMStateDescription vmstate_pxa2xx_i2c_slave = {
    .name = "pxa2xx_i2c_slave",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_I2C_SLAVE(i2c, PXA2xxI2CSlaveState),
        VMSTATE_END_OF_LIST()
    }
};
1507

J
Juan Quintela 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
static const VMStateDescription vmstate_pxa2xx_i2c = {
    .name = "pxa2xx_i2c",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT16(control, PXA2xxI2CState),
        VMSTATE_UINT16(status, PXA2xxI2CState),
        VMSTATE_UINT8(ibmr, PXA2xxI2CState),
        VMSTATE_UINT8(data, PXA2xxI2CState),
        VMSTATE_STRUCT_POINTER(slave, PXA2xxI2CState,
1519
                               vmstate_pxa2xx_i2c_slave, PXA2xxI2CSlaveState *),
J
Juan Quintela 已提交
1520 1521 1522
        VMSTATE_END_OF_LIST()
    }
};
1523

1524
static int pxa2xx_i2c_slave_init(i2c_slave *i2c)
P
Paul Brook 已提交
1525 1526
{
    /* Nothing to do.  */
1527
    return 0;
P
Paul Brook 已提交
1528 1529 1530
}

static I2CSlaveInfo pxa2xx_i2c_slave_info = {
1531 1532
    .qdev.name = "pxa2xx-i2c-slave",
    .qdev.size = sizeof(PXA2xxI2CSlaveState),
P
Paul Brook 已提交
1533 1534 1535 1536 1537 1538
    .init = pxa2xx_i2c_slave_init,
    .event = pxa2xx_i2c_event,
    .recv = pxa2xx_i2c_rx,
    .send = pxa2xx_i2c_tx
};

A
Anthony Liguori 已提交
1539
PXA2xxI2CState *pxa2xx_i2c_init(target_phys_addr_t base,
1540
                qemu_irq irq, uint32_t region_size)
1541
{
P
Paul Brook 已提交
1542
    DeviceState *dev;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
    SysBusDevice *i2c_dev;
    PXA2xxI2CState *s;

    i2c_dev = sysbus_from_qdev(qdev_create(NULL, "pxa2xx_i2c"));
    qdev_prop_set_uint32(&i2c_dev->qdev, "size", region_size + 1);
    qdev_prop_set_uint32(&i2c_dev->qdev, "offset",
            base - (base & (~region_size) & TARGET_PAGE_MASK));

    qdev_init_nofail(&i2c_dev->qdev);

    sysbus_mmio_map(i2c_dev, 0, base & ~region_size);
    sysbus_connect_irq(i2c_dev, 0, irq);
P
Paul Brook 已提交
1555

1556
    s = FROM_SYSBUS(PXA2xxI2CState, i2c_dev);
P
pbrook 已提交
1557
    /* FIXME: Should the slave device really be on a separate bus?  */
P
Paul Brook 已提交
1558
    dev = i2c_create_slave(i2c_init_bus(NULL, "dummy"), "pxa2xx-i2c-slave", 0);
P
Paul Brook 已提交
1559 1560
    s->slave = FROM_I2C_SLAVE(PXA2xxI2CSlaveState, I2C_SLAVE_FROM_QDEV(dev));
    s->slave->host = s;
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570
    return s;
}

static int pxa2xx_i2c_initfn(SysBusDevice *dev)
{
    PXA2xxI2CState *s = FROM_SYSBUS(PXA2xxI2CState, dev);
    int iomemtype;

    s->bus = i2c_init_bus(&dev->qdev, "i2c");
1571

1572
    iomemtype = cpu_register_io_memory(pxa2xx_i2c_readfn,
1573
                    pxa2xx_i2c_writefn, s, DEVICE_NATIVE_ENDIAN);
1574 1575
    sysbus_init_mmio(dev, s->region_size, iomemtype);
    sysbus_init_irq(dev, &s->irq);
1576

1577
    return 0;
1578 1579
}

P
Paul Brook 已提交
1580
i2c_bus *pxa2xx_i2c_bus(PXA2xxI2CState *s)
1581 1582 1583 1584
{
    return s->bus;
}

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
static SysBusDeviceInfo pxa2xx_i2c_info = {
    .init       = pxa2xx_i2c_initfn,
    .qdev.name  = "pxa2xx_i2c",
    .qdev.desc  = "PXA2xx I2C Bus Controller",
    .qdev.size  = sizeof(PXA2xxI2CState),
    .qdev.vmsd  = &vmstate_pxa2xx_i2c,
    .qdev.props = (Property[]) {
        DEFINE_PROP_UINT32("size", PXA2xxI2CState, region_size, 0x10000),
        DEFINE_PROP_UINT32("offset", PXA2xxI2CState, offset, 0),
        DEFINE_PROP_END_OF_LIST(),
    },
};

1598
/* PXA Inter-IC Sound Controller */
P
Paul Brook 已提交
1599
static void pxa2xx_i2s_reset(PXA2xxI2SState *i2s)
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
{
    i2s->rx_len = 0;
    i2s->tx_len = 0;
    i2s->fifo_len = 0;
    i2s->clk = 0x1a;
    i2s->control[0] = 0x00;
    i2s->control[1] = 0x00;
    i2s->status = 0x00;
    i2s->mask = 0x00;
}

#define SACR_TFTH(val)	((val >> 8) & 0xf)
#define SACR_RFTH(val)	((val >> 12) & 0xf)
#define SACR_DREC(val)	(val & (1 << 3))
#define SACR_DPRL(val)	(val & (1 << 4))

P
Paul Brook 已提交
1616
static inline void pxa2xx_i2s_update(PXA2xxI2SState *i2s)
1617 1618 1619 1620 1621 1622 1623
{
    int rfs, tfs;
    rfs = SACR_RFTH(i2s->control[0]) < i2s->rx_len &&
            !SACR_DREC(i2s->control[1]);
    tfs = (i2s->tx_len || i2s->fifo_len < SACR_TFTH(i2s->control[0])) &&
            i2s->enable && !SACR_DPRL(i2s->control[1]);

1624 1625
    qemu_set_irq(i2s->rx_dma, rfs);
    qemu_set_irq(i2s->tx_dma, tfs);
1626 1627

    i2s->status &= 0xe0;
1628 1629
    if (i2s->fifo_len < 16 || !i2s->enable)
        i2s->status |= 1 << 0;			/* TNF */
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
    if (i2s->rx_len)
        i2s->status |= 1 << 1;			/* RNE */
    if (i2s->enable)
        i2s->status |= 1 << 2;			/* BSY */
    if (tfs)
        i2s->status |= 1 << 3;			/* TFS */
    if (rfs)
        i2s->status |= 1 << 4;			/* RFS */
    if (!(i2s->tx_len && i2s->enable))
        i2s->status |= i2s->fifo_len << 8;	/* TFL */
    i2s->status |= MAX(i2s->rx_len, 0xf) << 12;	/* RFL */

    qemu_set_irq(i2s->irq, i2s->status & i2s->mask);
}

#define SACR0	0x00	/* Serial Audio Global Control register */
#define SACR1	0x04	/* Serial Audio I2S/MSB-Justified Control register */
#define SASR0	0x0c	/* Serial Audio Interface and FIFO Status register */
#define SAIMR	0x14	/* Serial Audio Interrupt Mask register */
#define SAICR	0x18	/* Serial Audio Interrupt Clear register */
#define SADIV	0x60	/* Serial Audio Clock Divider register */
#define SADR	0x80	/* Serial Audio Data register */

A
Anthony Liguori 已提交
1653
static uint32_t pxa2xx_i2s_read(void *opaque, target_phys_addr_t addr)
1654
{
P
Paul Brook 已提交
1655
    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
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

    switch (addr) {
    case SACR0:
        return s->control[0];
    case SACR1:
        return s->control[1];
    case SASR0:
        return s->status;
    case SAIMR:
        return s->mask;
    case SAICR:
        return 0;
    case SADIV:
        return s->clk;
    case SADR:
        if (s->rx_len > 0) {
            s->rx_len --;
            pxa2xx_i2s_update(s);
            return s->codec_in(s->opaque);
        }
        return 0;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
1684
static void pxa2xx_i2s_write(void *opaque, target_phys_addr_t addr,
1685 1686
                uint32_t value)
{
P
Paul Brook 已提交
1687
    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    uint32_t *sample;

    switch (addr) {
    case SACR0:
        if (value & (1 << 3))				/* RST */
            pxa2xx_i2s_reset(s);
        s->control[0] = value & 0xff3d;
        if (!s->enable && (value & 1) && s->tx_len) {	/* ENB */
            for (sample = s->fifo; s->fifo_len > 0; s->fifo_len --, sample ++)
                s->codec_out(s->opaque, *sample);
            s->status &= ~(1 << 7);			/* I2SOFF */
        }
        if (value & (1 << 4))				/* EFWR */
            printf("%s: Attempt to use special function\n", __FUNCTION__);
1702
        s->enable = (value & 9) == 1;			/* ENB && !RST*/
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
        pxa2xx_i2s_update(s);
        break;
    case SACR1:
        s->control[1] = value & 0x0039;
        if (value & (1 << 5))				/* ENLBF */
            printf("%s: Attempt to use loopback function\n", __FUNCTION__);
        if (value & (1 << 4))				/* DPRL */
            s->fifo_len = 0;
        pxa2xx_i2s_update(s);
        break;
    case SAIMR:
        s->mask = value & 0x0078;
        pxa2xx_i2s_update(s);
        break;
    case SAICR:
        s->status &= ~(value & (3 << 5));
        pxa2xx_i2s_update(s);
        break;
    case SADIV:
        s->clk = value & 0x007f;
        break;
    case SADR:
        if (s->tx_len && s->enable) {
            s->tx_len --;
            pxa2xx_i2s_update(s);
            s->codec_out(s->opaque, value);
        } else if (s->fifo_len < 16) {
            s->fifo[s->fifo_len ++] = value;
            pxa2xx_i2s_update(s);
        }
        break;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
    }
}

1739
static CPUReadMemoryFunc * const pxa2xx_i2s_readfn[] = {
1740 1741 1742 1743 1744
    pxa2xx_i2s_read,
    pxa2xx_i2s_read,
    pxa2xx_i2s_read,
};

1745
static CPUWriteMemoryFunc * const pxa2xx_i2s_writefn[] = {
1746 1747 1748 1749 1750
    pxa2xx_i2s_write,
    pxa2xx_i2s_write,
    pxa2xx_i2s_write,
};

1751 1752
static void pxa2xx_i2s_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
1753
    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768

    qemu_put_be32s(f, &s->control[0]);
    qemu_put_be32s(f, &s->control[1]);
    qemu_put_be32s(f, &s->status);
    qemu_put_be32s(f, &s->mask);
    qemu_put_be32s(f, &s->clk);

    qemu_put_be32(f, s->enable);
    qemu_put_be32(f, s->rx_len);
    qemu_put_be32(f, s->tx_len);
    qemu_put_be32(f, s->fifo_len);
}

static int pxa2xx_i2s_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
1769
    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784

    qemu_get_be32s(f, &s->control[0]);
    qemu_get_be32s(f, &s->control[1]);
    qemu_get_be32s(f, &s->status);
    qemu_get_be32s(f, &s->mask);
    qemu_get_be32s(f, &s->clk);

    s->enable = qemu_get_be32(f);
    s->rx_len = qemu_get_be32(f);
    s->tx_len = qemu_get_be32(f);
    s->fifo_len = qemu_get_be32(f);

    return 0;
}

1785 1786
static void pxa2xx_i2s_data_req(void *opaque, int tx, int rx)
{
P
Paul Brook 已提交
1787
    PXA2xxI2SState *s = (PXA2xxI2SState *) opaque;
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
    uint32_t *sample;

    /* Signal FIFO errors */
    if (s->enable && s->tx_len)
        s->status |= 1 << 5;		/* TUR */
    if (s->enable && s->rx_len)
        s->status |= 1 << 6;		/* ROR */

    /* Should be tx - MIN(tx, s->fifo_len) but we don't really need to
     * handle the cases where it makes a difference.  */
    s->tx_len = tx - s->fifo_len;
    s->rx_len = rx;
    /* Note that is s->codec_out wasn't set, we wouldn't get called.  */
    if (s->enable)
        for (sample = s->fifo; s->fifo_len; s->fifo_len --, sample ++)
            s->codec_out(s->opaque, *sample);
    pxa2xx_i2s_update(s);
}

A
Anthony Liguori 已提交
1807
static PXA2xxI2SState *pxa2xx_i2s_init(target_phys_addr_t base,
1808
                qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma)
1809 1810
{
    int iomemtype;
P
Paul Brook 已提交
1811 1812
    PXA2xxI2SState *s = (PXA2xxI2SState *)
            qemu_mallocz(sizeof(PXA2xxI2SState));
1813 1814

    s->irq = irq;
1815 1816
    s->rx_dma = rx_dma;
    s->tx_dma = tx_dma;
1817 1818 1819 1820
    s->data_req = pxa2xx_i2s_data_req;

    pxa2xx_i2s_reset(s);

1821
    iomemtype = cpu_register_io_memory(pxa2xx_i2s_readfn,
1822
                    pxa2xx_i2s_writefn, s, DEVICE_NATIVE_ENDIAN);
1823
    cpu_register_physical_memory(base, 0x100000, iomemtype);
1824

A
Alex Williamson 已提交
1825
    register_savevm(NULL, "pxa2xx_i2s", base, 0,
1826 1827
                    pxa2xx_i2s_save, pxa2xx_i2s_load, s);

1828 1829 1830 1831
    return s;
}

/* PXA Fast Infra-red Communications Port */
P
Paul Brook 已提交
1832
struct PXA2xxFIrState {
1833
    qemu_irq irq;
1834 1835
    qemu_irq rx_dma;
    qemu_irq tx_dma;
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
    int enable;
    CharDriverState *chr;

    uint8_t control[3];
    uint8_t status[2];

    int rx_len;
    int rx_start;
    uint8_t rx_fifo[64];
};

P
Paul Brook 已提交
1847
static void pxa2xx_fir_reset(PXA2xxFIrState *s)
1848 1849 1850 1851 1852 1853 1854 1855 1856
{
    s->control[0] = 0x00;
    s->control[1] = 0x00;
    s->control[2] = 0x00;
    s->status[0] = 0x00;
    s->status[1] = 0x00;
    s->enable = 0;
}

P
Paul Brook 已提交
1857
static inline void pxa2xx_fir_update(PXA2xxFIrState *s)
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
{
    static const int tresh[4] = { 8, 16, 32, 0 };
    int intr = 0;
    if ((s->control[0] & (1 << 4)) &&			/* RXE */
                    s->rx_len >= tresh[s->control[2] & 3])	/* TRIG */
        s->status[0] |= 1 << 4;				/* RFS */
    else
        s->status[0] &= ~(1 << 4);			/* RFS */
    if (s->control[0] & (1 << 3))			/* TXE */
        s->status[0] |= 1 << 3;				/* TFS */
    else
        s->status[0] &= ~(1 << 3);			/* TFS */
    if (s->rx_len)
        s->status[1] |= 1 << 2;				/* RNE */
    else
        s->status[1] &= ~(1 << 2);			/* RNE */
    if (s->control[0] & (1 << 4))			/* RXE */
        s->status[1] |= 1 << 0;				/* RSY */
    else
        s->status[1] &= ~(1 << 0);			/* RSY */

    intr |= (s->control[0] & (1 << 5)) &&		/* RIE */
            (s->status[0] & (1 << 4));			/* RFS */
    intr |= (s->control[0] & (1 << 6)) &&		/* TIE */
            (s->status[0] & (1 << 3));			/* TFS */
    intr |= (s->control[2] & (1 << 4)) &&		/* TRAIL */
            (s->status[0] & (1 << 6));			/* EOC */
    intr |= (s->control[0] & (1 << 2)) &&		/* TUS */
            (s->status[0] & (1 << 1));			/* TUR */
    intr |= s->status[0] & 0x25;			/* FRE, RAB, EIF */

1889 1890
    qemu_set_irq(s->rx_dma, (s->status[0] >> 4) & 1);
    qemu_set_irq(s->tx_dma, (s->status[0] >> 3) & 1);
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902

    qemu_set_irq(s->irq, intr && s->enable);
}

#define ICCR0	0x00	/* FICP Control register 0 */
#define ICCR1	0x04	/* FICP Control register 1 */
#define ICCR2	0x08	/* FICP Control register 2 */
#define ICDR	0x0c	/* FICP Data register */
#define ICSR0	0x14	/* FICP Status register 0 */
#define ICSR1	0x18	/* FICP Status register 1 */
#define ICFOR	0x1c	/* FICP FIFO Occupancy Status register */

A
Anthony Liguori 已提交
1903
static uint32_t pxa2xx_fir_read(void *opaque, target_phys_addr_t addr)
1904
{
P
Paul Brook 已提交
1905
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
    uint8_t ret;

    switch (addr) {
    case ICCR0:
        return s->control[0];
    case ICCR1:
        return s->control[1];
    case ICCR2:
        return s->control[2];
    case ICDR:
        s->status[0] &= ~0x01;
        s->status[1] &= ~0x72;
        if (s->rx_len) {
            s->rx_len --;
            ret = s->rx_fifo[s->rx_start ++];
            s->rx_start &= 63;
            pxa2xx_fir_update(s);
            return ret;
        }
        printf("%s: Rx FIFO underrun.\n", __FUNCTION__);
        break;
    case ICSR0:
        return s->status[0];
    case ICSR1:
        return s->status[1] | (1 << 3);			/* TNF */
    case ICFOR:
        return s->rx_len;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
        break;
    }
    return 0;
}

A
Anthony Liguori 已提交
1940
static void pxa2xx_fir_write(void *opaque, target_phys_addr_t addr,
1941 1942
                uint32_t value)
{
P
Paul Brook 已提交
1943
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
1944 1945 1946 1947 1948 1949 1950
    uint8_t ch;

    switch (addr) {
    case ICCR0:
        s->control[0] = value;
        if (!(value & (1 << 4)))			/* RXE */
            s->rx_len = s->rx_start = 0;
B
Blue Swirl 已提交
1951 1952 1953
        if (!(value & (1 << 3))) {                      /* TXE */
            /* Nop */
        }
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
        s->enable = value & 1;				/* ITR */
        if (!s->enable)
            s->status[0] = 0;
        pxa2xx_fir_update(s);
        break;
    case ICCR1:
        s->control[1] = value;
        break;
    case ICCR2:
        s->control[2] = value & 0x3f;
        pxa2xx_fir_update(s);
        break;
    case ICDR:
        if (s->control[2] & (1 << 2))			/* TXP */
            ch = value;
        else
            ch = ~value;
        if (s->chr && s->enable && (s->control[0] & (1 << 3)))	/* TXE */
            qemu_chr_write(s->chr, &ch, 1);
        break;
    case ICSR0:
        s->status[0] &= ~(value & 0x66);
        pxa2xx_fir_update(s);
        break;
    case ICFOR:
        break;
    default:
        printf("%s: Bad register " REG_FMT "\n", __FUNCTION__, addr);
    }
}

1985
static CPUReadMemoryFunc * const pxa2xx_fir_readfn[] = {
1986 1987 1988 1989 1990
    pxa2xx_fir_read,
    pxa2xx_fir_read,
    pxa2xx_fir_read,
};

1991
static CPUWriteMemoryFunc * const pxa2xx_fir_writefn[] = {
1992 1993 1994 1995 1996 1997 1998
    pxa2xx_fir_write,
    pxa2xx_fir_write,
    pxa2xx_fir_write,
};

static int pxa2xx_fir_is_empty(void *opaque)
{
P
Paul Brook 已提交
1999
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2000 2001 2002 2003 2004
    return (s->rx_len < 64);
}

static void pxa2xx_fir_rx(void *opaque, const uint8_t *buf, int size)
{
P
Paul Brook 已提交
2005
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    if (!(s->control[0] & (1 << 4)))			/* RXE */
        return;

    while (size --) {
        s->status[1] |= 1 << 4;				/* EOF */
        if (s->rx_len >= 64) {
            s->status[1] |= 1 << 6;			/* ROR */
            break;
        }

        if (s->control[2] & (1 << 3))			/* RXP */
            s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = *(buf ++);
        else
            s->rx_fifo[(s->rx_start + s->rx_len ++) & 63] = ~*(buf ++);
    }

    pxa2xx_fir_update(s);
}

static void pxa2xx_fir_event(void *opaque, int event)
{
}

2029 2030
static void pxa2xx_fir_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
2031
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    int i;

    qemu_put_be32(f, s->enable);

    qemu_put_8s(f, &s->control[0]);
    qemu_put_8s(f, &s->control[1]);
    qemu_put_8s(f, &s->control[2]);
    qemu_put_8s(f, &s->status[0]);
    qemu_put_8s(f, &s->status[1]);

    qemu_put_byte(f, s->rx_len);
    for (i = 0; i < s->rx_len; i ++)
        qemu_put_byte(f, s->rx_fifo[(s->rx_start + i) & 63]);
}

static int pxa2xx_fir_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
2049
    PXA2xxFIrState *s = (PXA2xxFIrState *) opaque;
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    int i;

    s->enable = qemu_get_be32(f);

    qemu_get_8s(f, &s->control[0]);
    qemu_get_8s(f, &s->control[1]);
    qemu_get_8s(f, &s->control[2]);
    qemu_get_8s(f, &s->status[0]);
    qemu_get_8s(f, &s->status[1]);

    s->rx_len = qemu_get_byte(f);
    s->rx_start = 0;
    for (i = 0; i < s->rx_len; i ++)
        s->rx_fifo[i] = qemu_get_byte(f);

    return 0;
}

A
Anthony Liguori 已提交
2068
static PXA2xxFIrState *pxa2xx_fir_init(target_phys_addr_t base,
2069
                qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma,
2070 2071 2072
                CharDriverState *chr)
{
    int iomemtype;
P
Paul Brook 已提交
2073 2074
    PXA2xxFIrState *s = (PXA2xxFIrState *)
            qemu_mallocz(sizeof(PXA2xxFIrState));
2075 2076

    s->irq = irq;
2077 2078
    s->rx_dma = rx_dma;
    s->tx_dma = tx_dma;
2079 2080 2081 2082
    s->chr = chr;

    pxa2xx_fir_reset(s);

2083
    iomemtype = cpu_register_io_memory(pxa2xx_fir_readfn,
2084
                    pxa2xx_fir_writefn, s, DEVICE_NATIVE_ENDIAN);
2085
    cpu_register_physical_memory(base, 0x1000, iomemtype);
2086 2087 2088 2089 2090

    if (chr)
        qemu_chr_add_handlers(chr, pxa2xx_fir_is_empty,
                        pxa2xx_fir_rx, pxa2xx_fir_event, s);

A
Alex Williamson 已提交
2091 2092
    register_savevm(NULL, "pxa2xx_fir", 0, 0, pxa2xx_fir_save,
                    pxa2xx_fir_load, s);
2093

2094 2095 2096
    return s;
}

2097
static void pxa2xx_reset(void *opaque, int line, int level)
2098
{
P
Paul Brook 已提交
2099
    PXA2xxState *s = (PXA2xxState *) opaque;
2100

2101 2102 2103 2104 2105 2106 2107
    if (level && (s->pm_regs[PCFR >> 2] & 0x10)) {	/* GPR_EN */
        cpu_reset(s->env);
        /* TODO: reset peripherals */
    }
}

/* Initialise a PXA270 integrated chip (ARM based core).  */
P
Paul Brook 已提交
2108
PXA2xxState *pxa270_init(unsigned int sdram_size, const char *revision)
2109
{
P
Paul Brook 已提交
2110
    PXA2xxState *s;
2111
    int iomemtype, i;
G
Gerd Hoffmann 已提交
2112
    DriveInfo *dinfo;
P
Paul Brook 已提交
2113
    s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
2114

2115 2116 2117 2118
    if (revision && strncmp(revision, "pxa27", 5)) {
        fprintf(stderr, "Machine requires a PXA27x processor.\n");
        exit(1);
    }
B
bellard 已提交
2119 2120 2121 2122 2123 2124 2125 2126
    if (!revision)
        revision = "pxa270";
    
    s->env = cpu_init(revision);
    if (!s->env) {
        fprintf(stderr, "Unable to find CPU definition\n");
        exit(1);
    }
2127 2128
    s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];

2129 2130
    /* SDRAM & Internal Memory Storage */
    cpu_register_physical_memory(PXA2XX_SDRAM_BASE,
2131 2132
                    sdram_size, qemu_ram_alloc(NULL, "pxa270.sdram",
                                               sdram_size) | IO_MEM_RAM);
2133
    cpu_register_physical_memory(PXA2XX_INTERNAL_BASE,
2134 2135
                    0x40000, qemu_ram_alloc(NULL, "pxa270.internal",
                                            0x40000) | IO_MEM_RAM);
2136

2137 2138
    s->pic = pxa2xx_pic_init(0x40d00000, s->env);

2139 2140
    s->dma = pxa27x_dma_init(0x40000000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2141

2142 2143 2144 2145 2146 2147 2148
    sysbus_create_varargs("pxa27x-timer", 0x40a00000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
                    qdev_get_gpio_in(s->pic, PXA27X_PIC_OST_4_11),
                    NULL);
2149

2150 2151
    s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 121);

G
Gerd Hoffmann 已提交
2152 2153
    dinfo = drive_get(IF_SD, 0, 0);
    if (!dinfo) {
T
ths 已提交
2154 2155 2156
        fprintf(stderr, "qemu: missing SecureDigital device\n");
        exit(1);
    }
G
Gerd Hoffmann 已提交
2157
    s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
2158 2159 2160
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
                    qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
                    qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2161

2162 2163
    for (i = 0; pxa270_serial[i].io_base; i ++)
        if (serial_hds[i])
B
Blue Swirl 已提交
2164
#ifdef TARGET_WORDS_BIGENDIAN
2165
            serial_mm_init(pxa270_serial[i].io_base, 2,
2166 2167
                            qdev_get_gpio_in(s->pic, pxa270_serial[i].irqn),
                            14857000 / 16, serial_hds[i], 1, 1);
B
Blue Swirl 已提交
2168 2169
#else
            serial_mm_init(pxa270_serial[i].io_base, 2,
2170 2171
                            qdev_get_gpio_in(s->pic, pxa270_serial[i].irqn),
                            14857000 / 16, serial_hds[i], 1, 0);
B
Blue Swirl 已提交
2172
#endif
2173 2174 2175
        else
            break;
    if (serial_hds[i])
2176 2177
        s->fir = pxa2xx_fir_init(0x40800000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2178 2179 2180
                        qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
                        qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
                        serial_hds[i]);
2181

2182 2183
    s->lcd = pxa2xx_lcdc_init(0x44000000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2184

2185
    s->cm_base = 0x41300000;
B
balrog 已提交
2186
    s->cm_regs[CCCR >> 2] = 0x02000210;	/* 416.0 MHz */
2187
    s->clkcfg = 0x00000009;		/* Turbo mode active */
2188
    iomemtype = cpu_register_io_memory(pxa2xx_cm_readfn,
2189
                    pxa2xx_cm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2190
    cpu_register_physical_memory(s->cm_base, 0x1000, iomemtype);
A
Alex Williamson 已提交
2191
    register_savevm(NULL, "pxa2xx_cm", 0, 0, pxa2xx_cm_save, pxa2xx_cm_load, s);
2192 2193 2194 2195 2196 2197 2198

    cpu_arm_set_cp_io(s->env, 14, pxa2xx_cp14_read, pxa2xx_cp14_write, s);

    s->mm_base = 0x48000000;
    s->mm_regs[MDMRS >> 2] = 0x00020002;
    s->mm_regs[MDREFR >> 2] = 0x03ca4000;
    s->mm_regs[MECR >> 2] = 0x00000001;	/* Two PC Card sockets */
2199
    iomemtype = cpu_register_io_memory(pxa2xx_mm_readfn,
2200
                    pxa2xx_mm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2201
    cpu_register_physical_memory(s->mm_base, 0x1000, iomemtype);
A
Alex Williamson 已提交
2202
    register_savevm(NULL, "pxa2xx_mm", 0, 0, pxa2xx_mm_save, pxa2xx_mm_load, s);
2203

2204
    s->pm_base = 0x40f00000;
2205
    iomemtype = cpu_register_io_memory(pxa2xx_pm_readfn,
2206
                    pxa2xx_pm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2207
    cpu_register_physical_memory(s->pm_base, 0x100, iomemtype);
A
Alex Williamson 已提交
2208
    register_savevm(NULL, "pxa2xx_pm", 0, 0, pxa2xx_pm_save, pxa2xx_pm_load, s);
2209

2210
    for (i = 0; pxa27x_ssp[i].io_base; i ++);
P
Paul Brook 已提交
2211
    s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
2212
    for (i = 0; pxa27x_ssp[i].io_base; i ++) {
P
Paul Brook 已提交
2213 2214
        DeviceState *dev;
        dev = sysbus_create_simple("pxa2xx-ssp", pxa27x_ssp[i].io_base,
2215
                        qdev_get_gpio_in(s->pic, pxa27x_ssp[i].irqn));
P
Paul Brook 已提交
2216
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2217 2218
    }

2219
    if (usb_enabled) {
P
Paul Brook 已提交
2220
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
2221
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
2222 2223 2224 2225 2226
    }

    s->pcmcia[0] = pxa2xx_pcmcia_init(0x20000000);
    s->pcmcia[1] = pxa2xx_pcmcia_init(0x30000000);

2227 2228
    sysbus_create_simple("pxa2xx_rtc", 0x40900000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2229

2230 2231 2232 2233
    s->i2c[0] = pxa2xx_i2c_init(0x40301600,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
    s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2234

2235
    s->i2s = pxa2xx_i2s_init(0x40400000,
2236 2237 2238
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
                    qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
                    qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2239

2240 2241
    s->kp = pxa27x_keypad_init(0x41500000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_KEYPAD));
2242

2243
    /* GPIO1 resets the processor */
T
ths 已提交
2244
    /* The handler can be overridden by board-specific code */
2245
    qdev_connect_gpio_out(s->gpio, 1, s->reset);
2246 2247 2248 2249
    return s;
}

/* Initialise a PXA255 integrated chip (ARM based core).  */
P
Paul Brook 已提交
2250
PXA2xxState *pxa255_init(unsigned int sdram_size)
2251
{
P
Paul Brook 已提交
2252
    PXA2xxState *s;
2253
    int iomemtype, i;
G
Gerd Hoffmann 已提交
2254
    DriveInfo *dinfo;
B
bellard 已提交
2255

P
Paul Brook 已提交
2256
    s = (PXA2xxState *) qemu_mallocz(sizeof(PXA2xxState));
2257

B
bellard 已提交
2258 2259 2260 2261 2262
    s->env = cpu_init("pxa255");
    if (!s->env) {
        fprintf(stderr, "Unable to find CPU definition\n");
        exit(1);
    }
2263 2264
    s->reset = qemu_allocate_irqs(pxa2xx_reset, s, 1)[0];

2265
    /* SDRAM & Internal Memory Storage */
2266
    cpu_register_physical_memory(PXA2XX_SDRAM_BASE, sdram_size,
2267 2268
                    qemu_ram_alloc(NULL, "pxa255.sdram",
                                   sdram_size) | IO_MEM_RAM);
2269
    cpu_register_physical_memory(PXA2XX_INTERNAL_BASE, PXA2XX_INTERNAL_SIZE,
2270 2271
                    qemu_ram_alloc(NULL, "pxa255.internal",
                                   PXA2XX_INTERNAL_SIZE) | IO_MEM_RAM);
2272

2273 2274
    s->pic = pxa2xx_pic_init(0x40d00000, s->env);

2275 2276
    s->dma = pxa255_dma_init(0x40000000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_DMA));
2277

2278 2279 2280 2281 2282 2283
    sysbus_create_varargs("pxa25x-timer", 0x40a00000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 0),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 1),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 2),
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_OST_0 + 3),
                    NULL);
2284

2285
    s->gpio = pxa2xx_gpio_init(0x40e00000, s->env, s->pic, 85);
2286

G
Gerd Hoffmann 已提交
2287 2288
    dinfo = drive_get(IF_SD, 0, 0);
    if (!dinfo) {
T
ths 已提交
2289 2290 2291
        fprintf(stderr, "qemu: missing SecureDigital device\n");
        exit(1);
    }
G
Gerd Hoffmann 已提交
2292
    s->mmc = pxa2xx_mmci_init(0x41100000, dinfo->bdrv,
2293 2294 2295
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_MMC),
                    qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_MMCI),
                    qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_MMCI));
2296

2297
    for (i = 0; pxa255_serial[i].io_base; i ++)
B
Blue Swirl 已提交
2298 2299
        if (serial_hds[i]) {
#ifdef TARGET_WORDS_BIGENDIAN
2300
            serial_mm_init(pxa255_serial[i].io_base, 2,
2301 2302
                            qdev_get_gpio_in(s->pic, pxa255_serial[i].irqn),
                            14745600 / 16, serial_hds[i], 1, 1);
B
Blue Swirl 已提交
2303 2304
#else
            serial_mm_init(pxa255_serial[i].io_base, 2,
2305 2306
                            qdev_get_gpio_in(s->pic, pxa255_serial[i].irqn),
                            14745600 / 16, serial_hds[i], 1, 0);
B
Blue Swirl 已提交
2307 2308
#endif
        } else {
2309
            break;
B
Blue Swirl 已提交
2310
        }
2311
    if (serial_hds[i])
2312 2313
        s->fir = pxa2xx_fir_init(0x40800000,
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_ICP),
2314 2315 2316
                        qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_ICP),
                        qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_ICP),
                        serial_hds[i]);
2317

2318 2319
    s->lcd = pxa2xx_lcdc_init(0x44000000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_LCD));
2320

2321
    s->cm_base = 0x41300000;
B
balrog 已提交
2322
    s->cm_regs[CCCR >> 2] = 0x02000210;	/* 416.0 MHz */
2323
    s->clkcfg = 0x00000009;		/* Turbo mode active */
2324
    iomemtype = cpu_register_io_memory(pxa2xx_cm_readfn,
2325
                    pxa2xx_cm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2326
    cpu_register_physical_memory(s->cm_base, 0x1000, iomemtype);
A
Alex Williamson 已提交
2327
    register_savevm(NULL, "pxa2xx_cm", 0, 0, pxa2xx_cm_save, pxa2xx_cm_load, s);
2328 2329 2330 2331 2332 2333 2334

    cpu_arm_set_cp_io(s->env, 14, pxa2xx_cp14_read, pxa2xx_cp14_write, s);

    s->mm_base = 0x48000000;
    s->mm_regs[MDMRS >> 2] = 0x00020002;
    s->mm_regs[MDREFR >> 2] = 0x03ca4000;
    s->mm_regs[MECR >> 2] = 0x00000001;	/* Two PC Card sockets */
2335
    iomemtype = cpu_register_io_memory(pxa2xx_mm_readfn,
2336
                    pxa2xx_mm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2337
    cpu_register_physical_memory(s->mm_base, 0x1000, iomemtype);
A
Alex Williamson 已提交
2338
    register_savevm(NULL, "pxa2xx_mm", 0, 0, pxa2xx_mm_save, pxa2xx_mm_load, s);
2339

2340
    s->pm_base = 0x40f00000;
2341
    iomemtype = cpu_register_io_memory(pxa2xx_pm_readfn,
2342
                    pxa2xx_pm_writefn, s, DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
2343
    cpu_register_physical_memory(s->pm_base, 0x100, iomemtype);
A
Alex Williamson 已提交
2344
    register_savevm(NULL, "pxa2xx_pm", 0, 0, pxa2xx_pm_save, pxa2xx_pm_load, s);
2345

2346
    for (i = 0; pxa255_ssp[i].io_base; i ++);
P
Paul Brook 已提交
2347
    s->ssp = (SSIBus **)qemu_mallocz(sizeof(SSIBus *) * i);
2348
    for (i = 0; pxa255_ssp[i].io_base; i ++) {
P
Paul Brook 已提交
2349 2350
        DeviceState *dev;
        dev = sysbus_create_simple("pxa2xx-ssp", pxa255_ssp[i].io_base,
2351
                        qdev_get_gpio_in(s->pic, pxa255_ssp[i].irqn));
P
Paul Brook 已提交
2352
        s->ssp[i] = (SSIBus *)qdev_get_child_bus(dev, "ssi");
2353 2354
    }

2355
    if (usb_enabled) {
P
Paul Brook 已提交
2356
        sysbus_create_simple("sysbus-ohci", 0x4c000000,
2357
                        qdev_get_gpio_in(s->pic, PXA2XX_PIC_USBH1));
2358 2359 2360 2361 2362
    }

    s->pcmcia[0] = pxa2xx_pcmcia_init(0x20000000);
    s->pcmcia[1] = pxa2xx_pcmcia_init(0x30000000);

2363 2364
    sysbus_create_simple("pxa2xx_rtc", 0x40900000,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_RTCALARM));
2365

2366 2367 2368 2369
    s->i2c[0] = pxa2xx_i2c_init(0x40301600,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2C), 0xffff);
    s->i2c[1] = pxa2xx_i2c_init(0x40f00100,
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_PWRI2C), 0xff);
2370

2371
    s->i2s = pxa2xx_i2s_init(0x40400000,
2372 2373 2374
                    qdev_get_gpio_in(s->pic, PXA2XX_PIC_I2S),
                    qdev_get_gpio_in(s->dma, PXA2XX_RX_RQ_I2S),
                    qdev_get_gpio_in(s->dma, PXA2XX_TX_RQ_I2S));
2375 2376

    /* GPIO1 resets the processor */
T
ths 已提交
2377
    /* The handler can be overridden by board-specific code */
2378
    qdev_connect_gpio_out(s->gpio, 1, s->reset);
2379 2380
    return s;
}
P
Paul Brook 已提交
2381 2382 2383

static void pxa2xx_register_devices(void)
{
2384
    i2c_register_slave(&pxa2xx_i2c_slave_info);
P
Paul Brook 已提交
2385
    sysbus_register_dev("pxa2xx-ssp", sizeof(PXA2xxSSPState), pxa2xx_ssp_init);
2386
    sysbus_register_withprop(&pxa2xx_i2c_info);
2387
    sysbus_register_withprop(&pxa2xx_rtc_sysbus_info);
P
Paul Brook 已提交
2388 2389 2390
}

device_init(pxa2xx_register_devices)