stellaris.c 37.6 KB
Newer Older
P
pbrook 已提交
1
/*
A
aurel32 已提交
2
 * Luminary Micro Stellaris peripherals
P
pbrook 已提交
3 4 5 6 7 8 9
 *
 * Copyright (c) 2006 CodeSourcery.
 * Written by Paul Brook
 *
 * This code is licenced under the GPL.
 */

P
Paul Brook 已提交
10
#include "sysbus.h"
P
Paul Brook 已提交
11
#include "ssi.h"
P
pbrook 已提交
12 13 14 15
#include "arm-misc.h"
#include "devices.h"
#include "qemu-timer.h"
#include "i2c.h"
P
pbrook 已提交
16
#include "net.h"
P
pbrook 已提交
17
#include "boards.h"
P
pbrook 已提交
18

P
pbrook 已提交
19 20 21 22 23 24 25 26 27 28 29 30
#define GPIO_A 0
#define GPIO_B 1
#define GPIO_C 2
#define GPIO_D 3
#define GPIO_E 4
#define GPIO_F 5
#define GPIO_G 6

#define BP_OLED_I2C  0x01
#define BP_OLED_SSI  0x02
#define BP_GAMEPAD   0x04

P
pbrook 已提交
31 32 33 34 35 36 37 38 39
typedef const struct {
    const char *name;
    uint32_t did0;
    uint32_t did1;
    uint32_t dc0;
    uint32_t dc1;
    uint32_t dc2;
    uint32_t dc3;
    uint32_t dc4;
P
pbrook 已提交
40
    uint32_t peripherals;
P
pbrook 已提交
41 42 43 44 45
} stellaris_board_info;

/* General purpose timer module.  */

typedef struct gptm_state {
P
Paul Brook 已提交
46
    SysBusDevice busdev;
P
pbrook 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    uint32_t config;
    uint32_t mode[2];
    uint32_t control;
    uint32_t state;
    uint32_t mask;
    uint32_t load[2];
    uint32_t match[2];
    uint32_t prescale[2];
    uint32_t match_prescale[2];
    uint32_t rtc;
    int64_t tick[2];
    struct gptm_state *opaque[2];
    QEMUTimer *timer[2];
    /* The timers have an alternate output used to trigger the ADC.  */
    qemu_irq trigger;
    qemu_irq irq;
} gptm_state;

static void gptm_update_irq(gptm_state *s)
{
    int level;
    level = (s->state & s->mask) != 0;
    qemu_set_irq(s->irq, level);
}

static void gptm_stop(gptm_state *s, int n)
{
    qemu_del_timer(s->timer[n]);
}

static void gptm_reload(gptm_state *s, int n, int reset)
{
    int64_t tick;
    if (reset)
81
        tick = qemu_get_clock_ns(vm_clock);
P
pbrook 已提交
82 83 84 85 86 87 88
    else
        tick = s->tick[n];

    if (s->config == 0) {
        /* 32-bit CountDown.  */
        uint32_t count;
        count = s->load[0] | (s->load[1] << 16);
P
pbrook 已提交
89
        tick += (int64_t)count * system_clock_scale;
P
pbrook 已提交
90 91
    } else if (s->config == 1) {
        /* 32-bit RTC.  1Hz tick.  */
92
        tick += get_ticks_per_sec();
P
pbrook 已提交
93 94 95
    } else if (s->mode[n] == 0xa) {
        /* PWM mode.  Not implemented.  */
    } else {
P
Paul Brook 已提交
96
        hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
P
pbrook 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    }
    s->tick[n] = tick;
    qemu_mod_timer(s->timer[n], tick);
}

static void gptm_tick(void *opaque)
{
    gptm_state **p = (gptm_state **)opaque;
    gptm_state *s;
    int n;

    s = *p;
    n = p - s->opaque;
    if (s->config == 0) {
        s->state |= 1;
        if ((s->control & 0x20)) {
            /* Output trigger.  */
P
Paul Brook 已提交
114
	    qemu_irq_pulse(s->trigger);
P
pbrook 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }
        if (s->mode[0] & 1) {
            /* One-shot.  */
            s->control &= ~1;
        } else {
            /* Periodic.  */
            gptm_reload(s, 0, 0);
        }
    } else if (s->config == 1) {
        /* RTC.  */
        uint32_t match;
        s->rtc++;
        match = s->match[0] | (s->match[1] << 16);
        if (s->rtc > match)
            s->rtc = 0;
        if (s->rtc == 0) {
            s->state |= 8;
        }
        gptm_reload(s, 0, 0);
    } else if (s->mode[n] == 0xa) {
        /* PWM mode.  Not implemented.  */
    } else {
P
Paul Brook 已提交
137
        hw_error("TODO: 16-bit timer mode 0x%x\n", s->mode[n]);
P
pbrook 已提交
138 139 140 141
    }
    gptm_update_irq(s);
}

A
Anthony Liguori 已提交
142
static uint32_t gptm_read(void *opaque, target_phys_addr_t offset)
P
pbrook 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
    gptm_state *s = (gptm_state *)opaque;

    switch (offset) {
    case 0x00: /* CFG */
        return s->config;
    case 0x04: /* TAMR */
        return s->mode[0];
    case 0x08: /* TBMR */
        return s->mode[1];
    case 0x0c: /* CTL */
        return s->control;
    case 0x18: /* IMR */
        return s->mask;
    case 0x1c: /* RIS */
        return s->state;
    case 0x20: /* MIS */
        return s->state & s->mask;
    case 0x24: /* CR */
        return 0;
    case 0x28: /* TAILR */
        return s->load[0] | ((s->config < 4) ? (s->load[1] << 16) : 0);
    case 0x2c: /* TBILR */
        return s->load[1];
    case 0x30: /* TAMARCHR */
        return s->match[0] | ((s->config < 4) ? (s->match[1] << 16) : 0);
    case 0x34: /* TBMATCHR */
        return s->match[1];
    case 0x38: /* TAPR */
        return s->prescale[0];
    case 0x3c: /* TBPR */
        return s->prescale[1];
    case 0x40: /* TAPMR */
        return s->match_prescale[0];
    case 0x44: /* TBPMR */
        return s->match_prescale[1];
    case 0x48: /* TAR */
        if (s->control == 1)
            return s->rtc;
    case 0x4c: /* TBR */
P
Paul Brook 已提交
183
        hw_error("TODO: Timer value read\n");
P
pbrook 已提交
184
    default:
P
Paul Brook 已提交
185
        hw_error("gptm_read: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
186 187 188 189
        return 0;
    }
}

A
Anthony Liguori 已提交
190
static void gptm_write(void *opaque, target_phys_addr_t offset, uint32_t value)
P
pbrook 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
{
    gptm_state *s = (gptm_state *)opaque;
    uint32_t oldval;

    /* The timers should be disabled before changing the configuration.
       We take advantage of this and defer everything until the timer
       is enabled.  */
    switch (offset) {
    case 0x00: /* CFG */
        s->config = value;
        break;
    case 0x04: /* TAMR */
        s->mode[0] = value;
        break;
    case 0x08: /* TBMR */
        s->mode[1] = value;
        break;
    case 0x0c: /* CTL */
        oldval = s->control;
        s->control = value;
        /* TODO: Implement pause.  */
        if ((oldval ^ value) & 1) {
            if (value & 1) {
                gptm_reload(s, 0, 1);
            } else {
                gptm_stop(s, 0);
            }
        }
        if (((oldval ^ value) & 0x100) && s->config >= 4) {
            if (value & 0x100) {
                gptm_reload(s, 1, 1);
            } else {
                gptm_stop(s, 1);
            }
        }
        break;
    case 0x18: /* IMR */
        s->mask = value & 0x77;
        gptm_update_irq(s);
        break;
    case 0x24: /* CR */
        s->state &= ~value;
        break;
    case 0x28: /* TAILR */
        s->load[0] = value & 0xffff;
        if (s->config < 4) {
            s->load[1] = value >> 16;
        }
        break;
    case 0x2c: /* TBILR */
        s->load[1] = value & 0xffff;
        break;
    case 0x30: /* TAMARCHR */
        s->match[0] = value & 0xffff;
        if (s->config < 4) {
            s->match[1] = value >> 16;
        }
        break;
    case 0x34: /* TBMATCHR */
        s->match[1] = value >> 16;
        break;
    case 0x38: /* TAPR */
        s->prescale[0] = value;
        break;
    case 0x3c: /* TBPR */
        s->prescale[1] = value;
        break;
    case 0x40: /* TAPMR */
        s->match_prescale[0] = value;
        break;
    case 0x44: /* TBPMR */
        s->match_prescale[0] = value;
        break;
    default:
P
Paul Brook 已提交
265
        hw_error("gptm_write: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
266 267 268 269
    }
    gptm_update_irq(s);
}

270
static CPUReadMemoryFunc * const gptm_readfn[] = {
P
pbrook 已提交
271 272 273 274 275
   gptm_read,
   gptm_read,
   gptm_read
};

276
static CPUWriteMemoryFunc * const gptm_writefn[] = {
P
pbrook 已提交
277 278 279 280 281
   gptm_write,
   gptm_write,
   gptm_write
};

J
Juan Quintela 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
static const VMStateDescription vmstate_stellaris_gptm = {
    .name = "stellaris_gptm",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT32(config, gptm_state),
        VMSTATE_UINT32_ARRAY(mode, gptm_state, 2),
        VMSTATE_UINT32(control, gptm_state),
        VMSTATE_UINT32(state, gptm_state),
        VMSTATE_UINT32(mask, gptm_state),
        VMSTATE_UINT32(mode[0], gptm_state),
        VMSTATE_UINT32(mode[0], gptm_state),
        VMSTATE_UINT32_ARRAY(load, gptm_state, 2),
        VMSTATE_UINT32_ARRAY(match, gptm_state, 2),
        VMSTATE_UINT32_ARRAY(prescale, gptm_state, 2),
        VMSTATE_UINT32_ARRAY(match_prescale, gptm_state, 2),
        VMSTATE_UINT32(rtc, gptm_state),
        VMSTATE_INT64_ARRAY(tick, gptm_state, 2),
        VMSTATE_TIMER_ARRAY(timer, gptm_state, 2),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
305

306
static int stellaris_gptm_init(SysBusDevice *dev)
P
pbrook 已提交
307 308
{
    int iomemtype;
P
Paul Brook 已提交
309
    gptm_state *s = FROM_SYSBUS(gptm_state, dev);
P
pbrook 已提交
310

P
Paul Brook 已提交
311 312
    sysbus_init_irq(dev, &s->irq);
    qdev_init_gpio_out(&dev->qdev, &s->trigger, 1);
P
pbrook 已提交
313

314
    iomemtype = cpu_register_io_memory(gptm_readfn,
315 316
                                       gptm_writefn, s,
                                       DEVICE_NATIVE_ENDIAN);
P
Paul Brook 已提交
317 318 319
    sysbus_init_mmio(dev, 0x1000, iomemtype);

    s->opaque[0] = s->opaque[1] = s;
320 321
    s->timer[0] = qemu_new_timer_ns(vm_clock, gptm_tick, &s->opaque[0]);
    s->timer[1] = qemu_new_timer_ns(vm_clock, gptm_tick, &s->opaque[1]);
J
Juan Quintela 已提交
322
    vmstate_register(&dev->qdev, -1, &vmstate_stellaris_gptm, s);
323
    return 0;
P
pbrook 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
}


/* System controller.  */

typedef struct {
    uint32_t pborctl;
    uint32_t ldopctl;
    uint32_t int_status;
    uint32_t int_mask;
    uint32_t resc;
    uint32_t rcc;
    uint32_t rcgc[3];
    uint32_t scgc[3];
    uint32_t dcgc[3];
    uint32_t clkvclr;
    uint32_t ldoarst;
P
pbrook 已提交
341 342
    uint32_t user0;
    uint32_t user1;
P
pbrook 已提交
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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    qemu_irq irq;
    stellaris_board_info *board;
} ssys_state;

static void ssys_update(ssys_state *s)
{
  qemu_set_irq(s->irq, (s->int_status & s->int_mask) != 0);
}

static uint32_t pllcfg_sandstorm[16] = {
    0x31c0, /* 1 Mhz */
    0x1ae0, /* 1.8432 Mhz */
    0x18c0, /* 2 Mhz */
    0xd573, /* 2.4576 Mhz */
    0x37a6, /* 3.57954 Mhz */
    0x1ae2, /* 3.6864 Mhz */
    0x0c40, /* 4 Mhz */
    0x98bc, /* 4.906 Mhz */
    0x935b, /* 4.9152 Mhz */
    0x09c0, /* 5 Mhz */
    0x4dee, /* 5.12 Mhz */
    0x0c41, /* 6 Mhz */
    0x75db, /* 6.144 Mhz */
    0x1ae6, /* 7.3728 Mhz */
    0x0600, /* 8 Mhz */
    0x585b /* 8.192 Mhz */
};

static uint32_t pllcfg_fury[16] = {
    0x3200, /* 1 Mhz */
    0x1b20, /* 1.8432 Mhz */
    0x1900, /* 2 Mhz */
    0xf42b, /* 2.4576 Mhz */
    0x37e3, /* 3.57954 Mhz */
    0x1b21, /* 3.6864 Mhz */
    0x0c80, /* 4 Mhz */
    0x98ee, /* 4.906 Mhz */
    0xd5b4, /* 4.9152 Mhz */
    0x0a00, /* 5 Mhz */
    0x4e27, /* 5.12 Mhz */
    0x1902, /* 6 Mhz */
    0xec1c, /* 6.144 Mhz */
    0x1b23, /* 7.3728 Mhz */
    0x0640, /* 8 Mhz */
    0xb11c /* 8.192 Mhz */
};

A
Anthony Liguori 已提交
390
static uint32_t ssys_read(void *opaque, target_phys_addr_t offset)
P
pbrook 已提交
391 392 393 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
{
    ssys_state *s = (ssys_state *)opaque;

    switch (offset) {
    case 0x000: /* DID0 */
        return s->board->did0;
    case 0x004: /* DID1 */
        return s->board->did1;
    case 0x008: /* DC0 */
        return s->board->dc0;
    case 0x010: /* DC1 */
        return s->board->dc1;
    case 0x014: /* DC2 */
        return s->board->dc2;
    case 0x018: /* DC3 */
        return s->board->dc3;
    case 0x01c: /* DC4 */
        return s->board->dc4;
    case 0x030: /* PBORCTL */
        return s->pborctl;
    case 0x034: /* LDOPCTL */
        return s->ldopctl;
    case 0x040: /* SRCR0 */
        return 0;
    case 0x044: /* SRCR1 */
        return 0;
    case 0x048: /* SRCR2 */
        return 0;
    case 0x050: /* RIS */
        return s->int_status;
    case 0x054: /* IMC */
        return s->int_mask;
    case 0x058: /* MISC */
        return s->int_status & s->int_mask;
    case 0x05c: /* RESC */
        return s->resc;
    case 0x060: /* RCC */
        return s->rcc;
    case 0x064: /* PLLCFG */
        {
            int xtal;
            xtal = (s->rcc >> 6) & 0xf;
            if (s->board->did0 & (1 << 16)) {
                return pllcfg_fury[xtal];
            } else {
                return pllcfg_sandstorm[xtal];
            }
        }
    case 0x100: /* RCGC0 */
        return s->rcgc[0];
    case 0x104: /* RCGC1 */
        return s->rcgc[1];
    case 0x108: /* RCGC2 */
        return s->rcgc[2];
    case 0x110: /* SCGC0 */
        return s->scgc[0];
    case 0x114: /* SCGC1 */
        return s->scgc[1];
    case 0x118: /* SCGC2 */
        return s->scgc[2];
    case 0x120: /* DCGC0 */
        return s->dcgc[0];
    case 0x124: /* DCGC1 */
        return s->dcgc[1];
    case 0x128: /* DCGC2 */
        return s->dcgc[2];
    case 0x150: /* CLKVCLR */
        return s->clkvclr;
    case 0x160: /* LDOARST */
        return s->ldoarst;
P
pbrook 已提交
461 462 463 464
    case 0x1e0: /* USER0 */
        return s->user0;
    case 0x1e4: /* USER1 */
        return s->user1;
P
pbrook 已提交
465
    default:
P
Paul Brook 已提交
466
        hw_error("ssys_read: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
467 468 469 470
        return 0;
    }
}

P
pbrook 已提交
471 472 473 474 475
static void ssys_calculate_system_clock(ssys_state *s)
{
    system_clock_scale = 5 * (((s->rcc >> 23) & 0xf) + 1);
}

A
Anthony Liguori 已提交
476
static void ssys_write(void *opaque, target_phys_addr_t offset, uint32_t value)
P
pbrook 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
{
    ssys_state *s = (ssys_state *)opaque;

    switch (offset) {
    case 0x030: /* PBORCTL */
        s->pborctl = value & 0xffff;
        break;
    case 0x034: /* LDOPCTL */
        s->ldopctl = value & 0x1f;
        break;
    case 0x040: /* SRCR0 */
    case 0x044: /* SRCR1 */
    case 0x048: /* SRCR2 */
        fprintf(stderr, "Peripheral reset not implemented\n");
        break;
    case 0x054: /* IMC */
        s->int_mask = value & 0x7f;
        break;
    case 0x058: /* MISC */
        s->int_status &= ~value;
        break;
    case 0x05c: /* RESC */
        s->resc = value & 0x3f;
        break;
    case 0x060: /* RCC */
        if ((s->rcc & (1 << 13)) != 0 && (value & (1 << 13)) == 0) {
            /* PLL enable.  */
            s->int_status |= (1 << 6);
        }
        s->rcc = value;
P
pbrook 已提交
507
        ssys_calculate_system_clock(s);
P
pbrook 已提交
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
        break;
    case 0x100: /* RCGC0 */
        s->rcgc[0] = value;
        break;
    case 0x104: /* RCGC1 */
        s->rcgc[1] = value;
        break;
    case 0x108: /* RCGC2 */
        s->rcgc[2] = value;
        break;
    case 0x110: /* SCGC0 */
        s->scgc[0] = value;
        break;
    case 0x114: /* SCGC1 */
        s->scgc[1] = value;
        break;
    case 0x118: /* SCGC2 */
        s->scgc[2] = value;
        break;
    case 0x120: /* DCGC0 */
        s->dcgc[0] = value;
        break;
    case 0x124: /* DCGC1 */
        s->dcgc[1] = value;
        break;
    case 0x128: /* DCGC2 */
        s->dcgc[2] = value;
        break;
    case 0x150: /* CLKVCLR */
        s->clkvclr = value;
        break;
    case 0x160: /* LDOARST */
        s->ldoarst = value;
        break;
    default:
P
Paul Brook 已提交
543
        hw_error("ssys_write: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
544 545 546 547
    }
    ssys_update(s);
}

548
static CPUReadMemoryFunc * const ssys_readfn[] = {
P
pbrook 已提交
549 550 551 552 553
   ssys_read,
   ssys_read,
   ssys_read
};

554
static CPUWriteMemoryFunc * const ssys_writefn[] = {
P
pbrook 已提交
555 556 557 558 559
   ssys_write,
   ssys_write,
   ssys_write
};

560
static void ssys_reset(void *opaque)
P
pbrook 已提交
561 562 563 564 565 566 567 568 569 570
{
    ssys_state *s = (ssys_state *)opaque;

    s->pborctl = 0x7ffd;
    s->rcc = 0x078e3ac0;
    s->rcgc[0] = 1;
    s->scgc[0] = 1;
    s->dcgc[0] = 1;
}

J
Juan Quintela 已提交
571
static int stellaris_sys_post_load(void *opaque, int version_id)
P
pbrook 已提交
572
{
J
Juan Quintela 已提交
573
    ssys_state *s = opaque;
P
pbrook 已提交
574 575 576 577 578 579

    ssys_calculate_system_clock(s);

    return 0;
}

J
Juan Quintela 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
static const VMStateDescription vmstate_stellaris_sys = {
    .name = "stellaris_sys",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .post_load = stellaris_sys_post_load,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT32(pborctl, ssys_state),
        VMSTATE_UINT32(ldopctl, ssys_state),
        VMSTATE_UINT32(int_mask, ssys_state),
        VMSTATE_UINT32(int_status, ssys_state),
        VMSTATE_UINT32(resc, ssys_state),
        VMSTATE_UINT32(rcc, ssys_state),
        VMSTATE_UINT32_ARRAY(rcgc, ssys_state, 3),
        VMSTATE_UINT32_ARRAY(scgc, ssys_state, 3),
        VMSTATE_UINT32_ARRAY(dcgc, ssys_state, 3),
        VMSTATE_UINT32(clkvclr, ssys_state),
        VMSTATE_UINT32(ldoarst, ssys_state),
        VMSTATE_END_OF_LIST()
    }
};

602 603 604
static int stellaris_sys_init(uint32_t base, qemu_irq irq,
                              stellaris_board_info * board,
                              uint8_t *macaddr)
P
pbrook 已提交
605 606 607 608 609 610 611
{
    int iomemtype;
    ssys_state *s;

    s = (ssys_state *)qemu_mallocz(sizeof(ssys_state));
    s->irq = irq;
    s->board = board;
P
pbrook 已提交
612 613 614
    /* Most devices come preprogrammed with a MAC address in the user data. */
    s->user0 = macaddr[0] | (macaddr[1] << 8) | (macaddr[2] << 16);
    s->user1 = macaddr[3] | (macaddr[4] << 8) | (macaddr[5] << 16);
P
pbrook 已提交
615

616
    iomemtype = cpu_register_io_memory(ssys_readfn,
617 618
                                       ssys_writefn, s,
                                       DEVICE_NATIVE_ENDIAN);
P
pbrook 已提交
619 620
    cpu_register_physical_memory(base, 0x00001000, iomemtype);
    ssys_reset(s);
J
Juan Quintela 已提交
621
    vmstate_register(NULL, -1, &vmstate_stellaris_sys, s);
622
    return 0;
P
pbrook 已提交
623 624 625 626 627 628
}


/* I2C controller.  */

typedef struct {
P
Paul Brook 已提交
629
    SysBusDevice busdev;
P
pbrook 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    i2c_bus *bus;
    qemu_irq irq;
    uint32_t msa;
    uint32_t mcs;
    uint32_t mdr;
    uint32_t mtpr;
    uint32_t mimr;
    uint32_t mris;
    uint32_t mcr;
} stellaris_i2c_state;

#define STELLARIS_I2C_MCS_BUSY    0x01
#define STELLARIS_I2C_MCS_ERROR   0x02
#define STELLARIS_I2C_MCS_ADRACK  0x04
#define STELLARIS_I2C_MCS_DATACK  0x08
#define STELLARIS_I2C_MCS_ARBLST  0x10
#define STELLARIS_I2C_MCS_IDLE    0x20
#define STELLARIS_I2C_MCS_BUSBSY  0x40

A
Anthony Liguori 已提交
649
static uint32_t stellaris_i2c_read(void *opaque, target_phys_addr_t offset)
P
pbrook 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
{
    stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;

    switch (offset) {
    case 0x00: /* MSA */
        return s->msa;
    case 0x04: /* MCS */
        /* We don't emulate timing, so the controller is never busy.  */
        return s->mcs | STELLARIS_I2C_MCS_IDLE;
    case 0x08: /* MDR */
        return s->mdr;
    case 0x0c: /* MTPR */
        return s->mtpr;
    case 0x10: /* MIMR */
        return s->mimr;
    case 0x14: /* MRIS */
        return s->mris;
    case 0x18: /* MMIS */
        return s->mris & s->mimr;
    case 0x20: /* MCR */
        return s->mcr;
    default:
P
Paul Brook 已提交
672
        hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
673 674 675 676 677 678 679 680 681 682 683 684
        return 0;
    }
}

static void stellaris_i2c_update(stellaris_i2c_state *s)
{
    int level;

    level = (s->mris & s->mimr) != 0;
    qemu_set_irq(s->irq, level);
}

A
Anthony Liguori 已提交
685
static void stellaris_i2c_write(void *opaque, target_phys_addr_t offset,
P
pbrook 已提交
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 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
                                uint32_t value)
{
    stellaris_i2c_state *s = (stellaris_i2c_state *)opaque;

    switch (offset) {
    case 0x00: /* MSA */
        s->msa = value & 0xff;
        break;
    case 0x04: /* MCS */
        if ((s->mcr & 0x10) == 0) {
            /* Disabled.  Do nothing.  */
            break;
        }
        /* Grab the bus if this is starting a transfer.  */
        if ((value & 2) && (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
            if (i2c_start_transfer(s->bus, s->msa >> 1, s->msa & 1)) {
                s->mcs |= STELLARIS_I2C_MCS_ARBLST;
            } else {
                s->mcs &= ~STELLARIS_I2C_MCS_ARBLST;
                s->mcs |= STELLARIS_I2C_MCS_BUSBSY;
            }
        }
        /* If we don't have the bus then indicate an error.  */
        if (!i2c_bus_busy(s->bus)
                || (s->mcs & STELLARIS_I2C_MCS_BUSBSY) == 0) {
            s->mcs |= STELLARIS_I2C_MCS_ERROR;
            break;
        }
        s->mcs &= ~STELLARIS_I2C_MCS_ERROR;
        if (value & 1) {
            /* Transfer a byte.  */
            /* TODO: Handle errors.  */
            if (s->msa & 1) {
                /* Recv */
                s->mdr = i2c_recv(s->bus) & 0xff;
            } else {
                /* Send */
                i2c_send(s->bus, s->mdr);
            }
            /* Raise an interrupt.  */
            s->mris |= 1;
        }
        if (value & 4) {
            /* Finish transfer.  */
            i2c_end_transfer(s->bus);
            s->mcs &= ~STELLARIS_I2C_MCS_BUSBSY;
        }
        break;
    case 0x08: /* MDR */
        s->mdr = value & 0xff;
        break;
    case 0x0c: /* MTPR */
        s->mtpr = value & 0xff;
        break;
    case 0x10: /* MIMR */
        s->mimr = 1;
        break;
    case 0x1c: /* MICR */
        s->mris &= ~value;
        break;
    case 0x20: /* MCR */
        if (value & 1)
P
Paul Brook 已提交
748
            hw_error(
P
pbrook 已提交
749 750
                      "stellaris_i2c_write: Loopback not implemented\n");
        if (value & 0x20)
P
Paul Brook 已提交
751
            hw_error(
P
pbrook 已提交
752 753 754 755
                      "stellaris_i2c_write: Slave mode not implemented\n");
        s->mcr = value & 0x31;
        break;
    default:
P
Paul Brook 已提交
756
        hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
P
pbrook 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
                  (int)offset);
    }
    stellaris_i2c_update(s);
}

static void stellaris_i2c_reset(stellaris_i2c_state *s)
{
    if (s->mcs & STELLARIS_I2C_MCS_BUSBSY)
        i2c_end_transfer(s->bus);

    s->msa = 0;
    s->mcs = 0;
    s->mdr = 0;
    s->mtpr = 1;
    s->mimr = 0;
    s->mris = 0;
    s->mcr = 0;
    stellaris_i2c_update(s);
}

777
static CPUReadMemoryFunc * const stellaris_i2c_readfn[] = {
P
pbrook 已提交
778 779 780 781 782
   stellaris_i2c_read,
   stellaris_i2c_read,
   stellaris_i2c_read
};

783
static CPUWriteMemoryFunc * const stellaris_i2c_writefn[] = {
P
pbrook 已提交
784 785 786 787 788
   stellaris_i2c_write,
   stellaris_i2c_write,
   stellaris_i2c_write
};

J
Juan Quintela 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
static const VMStateDescription vmstate_stellaris_i2c = {
    .name = "stellaris_i2c",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT32(msa, stellaris_i2c_state),
        VMSTATE_UINT32(mcs, stellaris_i2c_state),
        VMSTATE_UINT32(mdr, stellaris_i2c_state),
        VMSTATE_UINT32(mtpr, stellaris_i2c_state),
        VMSTATE_UINT32(mimr, stellaris_i2c_state),
        VMSTATE_UINT32(mris, stellaris_i2c_state),
        VMSTATE_UINT32(mcr, stellaris_i2c_state),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
805

806
static int stellaris_i2c_init(SysBusDevice * dev)
P
pbrook 已提交
807
{
P
Paul Brook 已提交
808
    stellaris_i2c_state *s = FROM_SYSBUS(stellaris_i2c_state, dev);
P
Paul Brook 已提交
809
    i2c_bus *bus;
P
pbrook 已提交
810 811
    int iomemtype;

P
Paul Brook 已提交
812
    sysbus_init_irq(dev, &s->irq);
P
Paul Brook 已提交
813
    bus = i2c_init_bus(&dev->qdev, "i2c");
P
pbrook 已提交
814 815
    s->bus = bus;

816
    iomemtype = cpu_register_io_memory(stellaris_i2c_readfn,
817 818
                                       stellaris_i2c_writefn, s,
                                       DEVICE_NATIVE_ENDIAN);
P
Paul Brook 已提交
819
    sysbus_init_mmio(dev, 0x1000, iomemtype);
P
pbrook 已提交
820 821
    /* ??? For now we only implement the master interface.  */
    stellaris_i2c_reset(s);
J
Juan Quintela 已提交
822
    vmstate_register(&dev->qdev, -1, &vmstate_stellaris_i2c, s);
823
    return 0;
P
pbrook 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
}

/* Analogue to Digital Converter.  This is only partially implemented,
   enough for applications that use a combined ADC and timer tick.  */

#define STELLARIS_ADC_EM_CONTROLLER 0
#define STELLARIS_ADC_EM_COMP       1
#define STELLARIS_ADC_EM_EXTERNAL   4
#define STELLARIS_ADC_EM_TIMER      5
#define STELLARIS_ADC_EM_PWM0       6
#define STELLARIS_ADC_EM_PWM1       7
#define STELLARIS_ADC_EM_PWM2       8

#define STELLARIS_ADC_FIFO_EMPTY    0x0100
#define STELLARIS_ADC_FIFO_FULL     0x1000

typedef struct
{
P
Paul Brook 已提交
842
    SysBusDevice busdev;
P
pbrook 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856
    uint32_t actss;
    uint32_t ris;
    uint32_t im;
    uint32_t emux;
    uint32_t ostat;
    uint32_t ustat;
    uint32_t sspri;
    uint32_t sac;
    struct {
        uint32_t state;
        uint32_t data[16];
    } fifo[4];
    uint32_t ssmux[4];
    uint32_t ssctl[4];
P
pbrook 已提交
857
    uint32_t noise;
858
    qemu_irq irq[4];
P
pbrook 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
} stellaris_adc_state;

static uint32_t stellaris_adc_fifo_read(stellaris_adc_state *s, int n)
{
    int tail;

    tail = s->fifo[n].state & 0xf;
    if (s->fifo[n].state & STELLARIS_ADC_FIFO_EMPTY) {
        s->ustat |= 1 << n;
    } else {
        s->fifo[n].state = (s->fifo[n].state & ~0xf) | ((tail + 1) & 0xf);
        s->fifo[n].state &= ~STELLARIS_ADC_FIFO_FULL;
        if (tail + 1 == ((s->fifo[n].state >> 4) & 0xf))
            s->fifo[n].state |= STELLARIS_ADC_FIFO_EMPTY;
    }
    return s->fifo[n].data[tail];
}

static void stellaris_adc_fifo_write(stellaris_adc_state *s, int n,
                                     uint32_t value)
{
    int head;

882 883
    /* TODO: Real hardware has limited size FIFOs.  We have a full 16 entry 
       FIFO fir each sequencer.  */
P
pbrook 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    head = (s->fifo[n].state >> 4) & 0xf;
    if (s->fifo[n].state & STELLARIS_ADC_FIFO_FULL) {
        s->ostat |= 1 << n;
        return;
    }
    s->fifo[n].data[head] = value;
    head = (head + 1) & 0xf;
    s->fifo[n].state &= ~STELLARIS_ADC_FIFO_EMPTY;
    s->fifo[n].state = (s->fifo[n].state & ~0xf0) | (head << 4);
    if ((s->fifo[n].state & 0xf) == head)
        s->fifo[n].state |= STELLARIS_ADC_FIFO_FULL;
}

static void stellaris_adc_update(stellaris_adc_state *s)
{
    int level;
900
    int n;
P
pbrook 已提交
901

902 903 904 905
    for (n = 0; n < 4; n++) {
        level = (s->ris & s->im & (1 << n)) != 0;
        qemu_set_irq(s->irq[n], level);
    }
P
pbrook 已提交
906 907 908 909 910
}

static void stellaris_adc_trigger(void *opaque, int irq, int level)
{
    stellaris_adc_state *s = (stellaris_adc_state *)opaque;
911
    int n;
P
pbrook 已提交
912

913 914 915 916
    for (n = 0; n < 4; n++) {
        if ((s->actss & (1 << n)) == 0) {
            continue;
        }
P
pbrook 已提交
917

918 919 920 921 922 923 924 925 926 927 928 929
        if (((s->emux >> (n * 4)) & 0xff) != 5) {
            continue;
        }

        /* Some applications use the ADC as a random number source, so introduce
           some variation into the signal.  */
        s->noise = s->noise * 314159 + 1;
        /* ??? actual inputs not implemented.  Return an arbitrary value.  */
        stellaris_adc_fifo_write(s, n, 0x200 + ((s->noise >> 16) & 7));
        s->ris |= (1 << n);
        stellaris_adc_update(s);
    }
P
pbrook 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942
}

static void stellaris_adc_reset(stellaris_adc_state *s)
{
    int n;

    for (n = 0; n < 4; n++) {
        s->ssmux[n] = 0;
        s->ssctl[n] = 0;
        s->fifo[n].state = STELLARIS_ADC_FIFO_EMPTY;
    }
}

A
Anthony Liguori 已提交
943
static uint32_t stellaris_adc_read(void *opaque, target_phys_addr_t offset)
P
pbrook 已提交
944 945 946 947 948 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
{
    stellaris_adc_state *s = (stellaris_adc_state *)opaque;

    /* TODO: Implement this.  */
    if (offset >= 0x40 && offset < 0xc0) {
        int n;
        n = (offset - 0x40) >> 5;
        switch (offset & 0x1f) {
        case 0x00: /* SSMUX */
            return s->ssmux[n];
        case 0x04: /* SSCTL */
            return s->ssctl[n];
        case 0x08: /* SSFIFO */
            return stellaris_adc_fifo_read(s, n);
        case 0x0c: /* SSFSTAT */
            return s->fifo[n].state;
        default:
            break;
        }
    }
    switch (offset) {
    case 0x00: /* ACTSS */
        return s->actss;
    case 0x04: /* RIS */
        return s->ris;
    case 0x08: /* IM */
        return s->im;
    case 0x0c: /* ISC */
        return s->ris & s->im;
    case 0x10: /* OSTAT */
        return s->ostat;
    case 0x14: /* EMUX */
        return s->emux;
    case 0x18: /* USTAT */
        return s->ustat;
    case 0x20: /* SSPRI */
        return s->sspri;
    case 0x30: /* SAC */
        return s->sac;
    default:
P
Paul Brook 已提交
984
        hw_error("strllaris_adc_read: Bad offset 0x%x\n",
P
pbrook 已提交
985 986 987 988 989
                  (int)offset);
        return 0;
    }
}

A
Anthony Liguori 已提交
990
static void stellaris_adc_write(void *opaque, target_phys_addr_t offset,
P
pbrook 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
                                uint32_t value)
{
    stellaris_adc_state *s = (stellaris_adc_state *)opaque;

    /* TODO: Implement this.  */
    if (offset >= 0x40 && offset < 0xc0) {
        int n;
        n = (offset - 0x40) >> 5;
        switch (offset & 0x1f) {
        case 0x00: /* SSMUX */
            s->ssmux[n] = value & 0x33333333;
            return;
        case 0x04: /* SSCTL */
            if (value != 6) {
P
Paul Brook 已提交
1005
                hw_error("ADC: Unimplemented sequence %x\n",
P
pbrook 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                          value);
            }
            s->ssctl[n] = value;
            return;
        default:
            break;
        }
    }
    switch (offset) {
    case 0x00: /* ACTSS */
        s->actss = value & 0xf;
        break;
    case 0x08: /* IM */
        s->im = value;
        break;
    case 0x0c: /* ISC */
        s->ris &= ~value;
        break;
    case 0x10: /* OSTAT */
        s->ostat &= ~value;
        break;
    case 0x14: /* EMUX */
        s->emux = value;
        break;
    case 0x18: /* USTAT */
        s->ustat &= ~value;
        break;
    case 0x20: /* SSPRI */
        s->sspri = value;
        break;
    case 0x28: /* PSSI */
P
Paul Brook 已提交
1037
        hw_error("Not implemented:  ADC sample initiate\n");
P
pbrook 已提交
1038 1039 1040 1041 1042
        break;
    case 0x30: /* SAC */
        s->sac = value;
        break;
    default:
P
Paul Brook 已提交
1043
        hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset);
P
pbrook 已提交
1044 1045 1046 1047
    }
    stellaris_adc_update(s);
}

1048
static CPUReadMemoryFunc * const stellaris_adc_readfn[] = {
P
pbrook 已提交
1049 1050 1051 1052 1053
   stellaris_adc_read,
   stellaris_adc_read,
   stellaris_adc_read
};

1054
static CPUWriteMemoryFunc * const stellaris_adc_writefn[] = {
P
pbrook 已提交
1055 1056 1057 1058 1059
   stellaris_adc_write,
   stellaris_adc_write,
   stellaris_adc_write
};

P
pbrook 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 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
static void stellaris_adc_save(QEMUFile *f, void *opaque)
{
    stellaris_adc_state *s = (stellaris_adc_state *)opaque;
    int i;
    int j;

    qemu_put_be32(f, s->actss);
    qemu_put_be32(f, s->ris);
    qemu_put_be32(f, s->im);
    qemu_put_be32(f, s->emux);
    qemu_put_be32(f, s->ostat);
    qemu_put_be32(f, s->ustat);
    qemu_put_be32(f, s->sspri);
    qemu_put_be32(f, s->sac);
    for (i = 0; i < 4; i++) {
        qemu_put_be32(f, s->fifo[i].state);
        for (j = 0; j < 16; j++) {
            qemu_put_be32(f, s->fifo[i].data[j]);
        }
        qemu_put_be32(f, s->ssmux[i]);
        qemu_put_be32(f, s->ssctl[i]);
    }
    qemu_put_be32(f, s->noise);
}

static int stellaris_adc_load(QEMUFile *f, void *opaque, int version_id)
{
    stellaris_adc_state *s = (stellaris_adc_state *)opaque;
    int i;
    int j;

    if (version_id != 1)
        return -EINVAL;

    s->actss = qemu_get_be32(f);
    s->ris = qemu_get_be32(f);
    s->im = qemu_get_be32(f);
    s->emux = qemu_get_be32(f);
    s->ostat = qemu_get_be32(f);
    s->ustat = qemu_get_be32(f);
    s->sspri = qemu_get_be32(f);
    s->sac = qemu_get_be32(f);
    for (i = 0; i < 4; i++) {
        s->fifo[i].state = qemu_get_be32(f);
        for (j = 0; j < 16; j++) {
            s->fifo[i].data[j] = qemu_get_be32(f);
        }
        s->ssmux[i] = qemu_get_be32(f);
        s->ssctl[i] = qemu_get_be32(f);
    }
    s->noise = qemu_get_be32(f);

    return 0;
}

1115
static int stellaris_adc_init(SysBusDevice *dev)
P
pbrook 已提交
1116
{
P
Paul Brook 已提交
1117
    stellaris_adc_state *s = FROM_SYSBUS(stellaris_adc_state, dev);
P
pbrook 已提交
1118
    int iomemtype;
1119
    int n;
P
pbrook 已提交
1120

1121
    for (n = 0; n < 4; n++) {
P
Paul Brook 已提交
1122
        sysbus_init_irq(dev, &s->irq[n]);
1123
    }
P
pbrook 已提交
1124

1125
    iomemtype = cpu_register_io_memory(stellaris_adc_readfn,
1126 1127
                                       stellaris_adc_writefn, s,
                                       DEVICE_NATIVE_ENDIAN);
P
Paul Brook 已提交
1128
    sysbus_init_mmio(dev, 0x1000, iomemtype);
P
pbrook 已提交
1129
    stellaris_adc_reset(s);
P
Paul Brook 已提交
1130
    qdev_init_gpio_in(&dev->qdev, stellaris_adc_trigger, 1);
A
Alex Williamson 已提交
1131
    register_savevm(&dev->qdev, "stellaris_adc", -1, 1,
P
pbrook 已提交
1132
                    stellaris_adc_save, stellaris_adc_load, s);
1133
    return 0;
P
pbrook 已提交
1134 1135
}

P
pbrook 已提交
1136 1137 1138 1139 1140 1141 1142 1143
/* Some boards have both an OLED controller and SD card connected to
   the same SSI port, with the SD card chip select connected to a
   GPIO pin.  Technically the OLED chip select is connected to the SSI
   Fss pin.  We do not bother emulating that as both devices should
   never be selected simultaneously, and our OLED controller ignores stray
   0xff commands that occur when deselecting the SD card.  */

typedef struct {
P
Paul Brook 已提交
1144
    SSISlave ssidev;
P
pbrook 已提交
1145 1146
    qemu_irq irq;
    int current_dev;
P
Paul Brook 已提交
1147
    SSIBus *bus[2];
P
pbrook 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156
} stellaris_ssi_bus_state;

static void stellaris_ssi_bus_select(void *opaque, int irq, int level)
{
    stellaris_ssi_bus_state *s = (stellaris_ssi_bus_state *)opaque;

    s->current_dev = level;
}

P
Paul Brook 已提交
1157
static uint32_t stellaris_ssi_bus_transfer(SSISlave *dev, uint32_t val)
P
pbrook 已提交
1158
{
P
Paul Brook 已提交
1159
    stellaris_ssi_bus_state *s = FROM_SSI_SLAVE(stellaris_ssi_bus_state, dev);
P
pbrook 已提交
1160

P
Paul Brook 已提交
1161
    return ssi_transfer(s->bus[s->current_dev], val);
P
pbrook 已提交
1162 1163
}

J
Juan Quintela 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
static const VMStateDescription vmstate_stellaris_ssi_bus = {
    .name = "stellaris_ssi_bus",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_INT32(current_dev, stellaris_ssi_bus_state),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
1174

1175
static int stellaris_ssi_bus_init(SSISlave *dev)
P
pbrook 已提交
1176
{
P
Paul Brook 已提交
1177 1178
    stellaris_ssi_bus_state *s = FROM_SSI_SLAVE(stellaris_ssi_bus_state, dev);

P
Paul Brook 已提交
1179 1180
    s->bus[0] = ssi_create_bus(&dev->qdev, "ssi0");
    s->bus[1] = ssi_create_bus(&dev->qdev, "ssi1");
P
Paul Brook 已提交
1181 1182
    qdev_init_gpio_in(&dev->qdev, stellaris_ssi_bus_select, 1);

J
Juan Quintela 已提交
1183
    vmstate_register(&dev->qdev, -1, &vmstate_stellaris_ssi_bus, s);
1184
    return 0;
P
pbrook 已提交
1185 1186
}

P
pbrook 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
/* Board init.  */
static stellaris_board_info stellaris_boards[] = {
  { "LM3S811EVB",
    0,
    0x0032000e,
    0x001f001f, /* dc0 */
    0x001132bf,
    0x01071013,
    0x3f0f01ff,
    0x0000001f,
P
pbrook 已提交
1197
    BP_OLED_I2C
P
pbrook 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206
  },
  { "LM3S6965EVB",
    0x10010002,
    0x1073402e,
    0x00ff007f, /* dc0 */
    0x001133ff,
    0x030f5317,
    0x0f0f87ff,
    0x5000007f,
P
pbrook 已提交
1207
    BP_OLED_SSI | BP_GAMEPAD
P
pbrook 已提交
1208 1209 1210 1211
  }
};

static void stellaris_init(const char *kernel_filename, const char *cpu_model,
1212
                           stellaris_board_info *board)
P
pbrook 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221
{
    static const int uart_irq[] = {5, 6, 33, 34};
    static const int timer_irq[] = {19, 21, 23, 35};
    static const uint32_t gpio_addr[7] =
      { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
        0x40024000, 0x40025000, 0x40026000};
    static const int gpio_irq[7] = {0, 1, 2, 3, 4, 30, 31};

    qemu_irq *pic;
P
Paul Brook 已提交
1222 1223 1224
    DeviceState *gpio_dev[7];
    qemu_irq gpio_in[7][8];
    qemu_irq gpio_out[7][8];
P
pbrook 已提交
1225 1226 1227 1228
    qemu_irq adc;
    int sram_size;
    int flash_size;
    i2c_bus *i2c;
P
Paul Brook 已提交
1229
    DeviceState *dev;
P
pbrook 已提交
1230
    int i;
P
Paul Brook 已提交
1231
    int j;
P
pbrook 已提交
1232 1233 1234 1235 1236 1237

    flash_size = ((board->dc0 & 0xffff) + 1) << 1;
    sram_size = (board->dc0 >> 18) + 1;
    pic = armv7m_init(flash_size, sram_size, kernel_filename, cpu_model);

    if (board->dc1 & (1 << 16)) {
P
Paul Brook 已提交
1238 1239 1240
        dev = sysbus_create_varargs("stellaris-adc", 0x40038000,
                                    pic[14], pic[15], pic[16], pic[17], NULL);
        adc = qdev_get_gpio_in(dev, 0);
P
pbrook 已提交
1241 1242 1243 1244 1245
    } else {
        adc = NULL;
    }
    for (i = 0; i < 4; i++) {
        if (board->dc2 & (0x10000 << i)) {
P
Paul Brook 已提交
1246 1247 1248 1249 1250 1251
            dev = sysbus_create_simple("stellaris-gptm",
                                       0x40030000 + i * 0x1000,
                                       pic[timer_irq[i]]);
            /* TODO: This is incorrect, but we get away with it because
               the ADC output is only ever pulsed.  */
            qdev_connect_gpio_out(dev, 0, adc);
P
pbrook 已提交
1252 1253 1254
        }
    }

P
pbrook 已提交
1255
    stellaris_sys_init(0x400fe000, pic[28], board, nd_table[0].macaddr);
P
pbrook 已提交
1256 1257 1258

    for (i = 0; i < 7; i++) {
        if (board->dc4 & (1 << i)) {
1259
            gpio_dev[i] = sysbus_create_simple("pl061_luminary", gpio_addr[i],
P
Paul Brook 已提交
1260 1261 1262 1263 1264
                                               pic[gpio_irq[i]]);
            for (j = 0; j < 8; j++) {
                gpio_in[i][j] = qdev_get_gpio_in(gpio_dev[i], j);
                gpio_out[i][j] = NULL;
            }
P
pbrook 已提交
1265 1266 1267 1268
        }
    }

    if (board->dc2 & (1 << 12)) {
P
Paul Brook 已提交
1269
        dev = sysbus_create_simple("stellaris-i2c", 0x40020000, pic[8]);
P
Paul Brook 已提交
1270
        i2c = (i2c_bus *)qdev_get_child_bus(dev, "i2c");
P
pbrook 已提交
1271
        if (board->peripherals & BP_OLED_I2C) {
P
Paul Brook 已提交
1272
            i2c_create_slave(i2c, "ssd0303", 0x3d);
P
pbrook 已提交
1273 1274 1275 1276 1277
        }
    }

    for (i = 0; i < 4; i++) {
        if (board->dc2 & (1 << i)) {
P
Paul Brook 已提交
1278 1279
            sysbus_create_simple("pl011_luminary", 0x4000c000 + i * 0x1000,
                                 pic[uart_irq[i]]);
P
pbrook 已提交
1280 1281 1282
        }
    }
    if (board->dc2 & (1 << 4)) {
P
Paul Brook 已提交
1283
        dev = sysbus_create_simple("pl022", 0x40008000, pic[7]);
P
pbrook 已提交
1284
        if (board->peripherals & BP_OLED_SSI) {
P
Paul Brook 已提交
1285 1286
            DeviceState *mux;
            void *bus;
P
pbrook 已提交
1287

P
Paul Brook 已提交
1288 1289 1290
            bus = qdev_get_child_bus(dev, "ssi");
            mux = ssi_create_slave(bus, "evb6965-ssi");
            gpio_out[GPIO_D][0] = qdev_get_gpio_in(mux, 0);
P
pbrook 已提交
1291

P
Paul Brook 已提交
1292
            bus = qdev_get_child_bus(mux, "ssi0");
1293
            ssi_create_slave(bus, "ssi-sd");
P
Paul Brook 已提交
1294 1295 1296 1297

            bus = qdev_get_child_bus(mux, "ssi1");
            dev = ssi_create_slave(bus, "ssd0323");
            gpio_out[GPIO_C][7] = qdev_get_gpio_in(dev, 0);
P
pbrook 已提交
1298 1299 1300

            /* Make sure the select pin is high.  */
            qemu_irq_raise(gpio_out[GPIO_D][0]);
P
pbrook 已提交
1301 1302
        }
    }
P
Paul Brook 已提交
1303 1304 1305 1306 1307 1308
    if (board->dc4 & (1 << 28)) {
        DeviceState *enet;

        qemu_check_nic_model(&nd_table[0], "stellaris");

        enet = qdev_create(NULL, "stellaris_enet");
1309
        qdev_set_nic_properties(enet, &nd_table[0]);
M
Markus Armbruster 已提交
1310
        qdev_init_nofail(enet);
P
Paul Brook 已提交
1311 1312 1313
        sysbus_mmio_map(sysbus_from_qdev(enet), 0, 0x40048000);
        sysbus_connect_irq(sysbus_from_qdev(enet), 0, pic[42]);
    }
P
pbrook 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
    if (board->peripherals & BP_GAMEPAD) {
        qemu_irq gpad_irq[5];
        static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };

        gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
        gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
        gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
        gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
        gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */

        stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
    }
P
Paul Brook 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334
    for (i = 0; i < 7; i++) {
        if (board->dc4 & (1 << i)) {
            for (j = 0; j < 8; j++) {
                if (gpio_out[i][j]) {
                    qdev_connect_gpio_out(gpio_dev[i], j, gpio_out[i][j]);
                }
            }
        }
    }
P
pbrook 已提交
1335 1336 1337
}

/* FIXME: Figure out how to generate these from stellaris_boards.  */
A
Anthony Liguori 已提交
1338
static void lm3s811evb_init(ram_addr_t ram_size,
1339
                     const char *boot_device,
P
pbrook 已提交
1340 1341 1342
                     const char *kernel_filename, const char *kernel_cmdline,
                     const char *initrd_filename, const char *cpu_model)
{
1343
    stellaris_init(kernel_filename, cpu_model, &stellaris_boards[0]);
P
pbrook 已提交
1344 1345
}

A
Anthony Liguori 已提交
1346
static void lm3s6965evb_init(ram_addr_t ram_size,
1347
                     const char *boot_device,
P
pbrook 已提交
1348 1349 1350
                     const char *kernel_filename, const char *kernel_cmdline,
                     const char *initrd_filename, const char *cpu_model)
{
1351
    stellaris_init(kernel_filename, cpu_model, &stellaris_boards[1]);
P
pbrook 已提交
1352 1353
}

1354
static QEMUMachine lm3s811evb_machine = {
1355 1356 1357
    .name = "lm3s811evb",
    .desc = "Stellaris LM3S811EVB",
    .init = lm3s811evb_init,
P
pbrook 已提交
1358 1359
};

1360
static QEMUMachine lm3s6965evb_machine = {
1361 1362 1363
    .name = "lm3s6965evb",
    .desc = "Stellaris LM3S6965EVB",
    .init = lm3s6965evb_init,
P
pbrook 已提交
1364
};
P
Paul Brook 已提交
1365

1366 1367 1368 1369 1370 1371 1372 1373
static void stellaris_machine_init(void)
{
    qemu_register_machine(&lm3s811evb_machine);
    qemu_register_machine(&lm3s6965evb_machine);
}

machine_init(stellaris_machine_init);

P
Paul Brook 已提交
1374
static SSISlaveInfo stellaris_ssi_bus_info = {
1375 1376
    .qdev.name = "evb6965-ssi",
    .qdev.size = sizeof(stellaris_ssi_bus_state),
P
Paul Brook 已提交
1377 1378 1379 1380
    .init = stellaris_ssi_bus_init,
    .transfer = stellaris_ssi_bus_transfer
};

P
Paul Brook 已提交
1381 1382 1383 1384
static void stellaris_register_devices(void)
{
    sysbus_register_dev("stellaris-i2c", sizeof(stellaris_i2c_state),
                        stellaris_i2c_init);
P
Paul Brook 已提交
1385 1386 1387 1388
    sysbus_register_dev("stellaris-gptm", sizeof(gptm_state),
                        stellaris_gptm_init);
    sysbus_register_dev("stellaris-adc", sizeof(stellaris_adc_state),
                        stellaris_adc_init);
1389
    ssi_register_slave(&stellaris_ssi_bus_info);
P
Paul Brook 已提交
1390 1391 1392
}

device_init(stellaris_register_devices)