slavio_misc.c 12.2 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU Sparc SLAVIO aux io port emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2005 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24

P
pbrook 已提交
25
#include "sysemu.h"
26
#include "sysbus.h"
27
#include "trace.h"
B
bellard 已提交
28 29 30 31 32 33 34 35 36 37

/*
 * This is the auxio port, chip control and system control part of
 * chip STP2001 (Slave I/O), also produced as NCR89C105. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
 *
 * This also includes the PMC CPU idle controller.
 */

typedef struct MiscState {
38
    SysBusDevice busdev;
P
pbrook 已提交
39
    qemu_irq irq;
40
    qemu_irq fdc_tc;
41
    uint32_t dummy;
B
bellard 已提交
42 43
    uint8_t config;
    uint8_t aux1, aux2;
44
    uint8_t diag, mctrl;
45
    uint8_t sysctrl;
46
    uint16_t leds;
B
bellard 已提交
47 48
} MiscState;

49 50 51 52 53
typedef struct APCState {
    SysBusDevice busdev;
    qemu_irq cpu_halt;
} APCState;

B
blueswir1 已提交
54
#define MISC_SIZE 1
55
#define SYSCTRL_SIZE 4
B
bellard 已提交
56

B
blueswir1 已提交
57 58
#define AUX1_TC        0x02

59 60 61 62 63 64 65 66 67
#define AUX2_PWROFF    0x01
#define AUX2_PWRINTCLR 0x02
#define AUX2_PWRFAIL   0x20

#define CFG_PWRINTEN   0x08

#define SYS_RESET      0x01
#define SYS_RESETSTAT  0x02

B
bellard 已提交
68 69 70 71
static void slavio_misc_update_irq(void *opaque)
{
    MiscState *s = opaque;

72
    if ((s->aux2 & AUX2_PWRFAIL) && (s->config & CFG_PWRINTEN)) {
73
        trace_slavio_misc_update_irq_raise();
P
pbrook 已提交
74
        qemu_irq_raise(s->irq);
B
bellard 已提交
75
    } else {
76
        trace_slavio_misc_update_irq_lower();
P
pbrook 已提交
77
        qemu_irq_lower(s->irq);
B
bellard 已提交
78 79 80
    }
}

81
static void slavio_misc_reset(DeviceState *d)
B
bellard 已提交
82
{
83
    MiscState *s = container_of(d, MiscState, busdev.qdev);
B
bellard 已提交
84

B
bellard 已提交
85
    // Diagnostic and system control registers not cleared in reset
B
bellard 已提交
86 87 88
    s->config = s->aux1 = s->aux2 = s->mctrl = 0;
}

89
static void slavio_set_power_fail(void *opaque, int irq, int power_failing)
B
bellard 已提交
90 91 92
{
    MiscState *s = opaque;

93
    trace_slavio_set_power_fail(power_failing, s->config);
94 95
    if (power_failing && (s->config & CFG_PWRINTEN)) {
        s->aux2 |= AUX2_PWRFAIL;
B
bellard 已提交
96
    } else {
97
        s->aux2 &= ~AUX2_PWRFAIL;
B
bellard 已提交
98 99 100 101
    }
    slavio_misc_update_irq(s);
}

A
Anthony Liguori 已提交
102
static void slavio_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
103 104 105 106
                                  uint32_t val)
{
    MiscState *s = opaque;

107
    trace_slavio_cfg_mem_writeb(val & 0xff);
108 109 110 111
    s->config = val & 0xff;
    slavio_misc_update_irq(s);
}

A
Anthony Liguori 已提交
112
static uint32_t slavio_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
113 114 115 116 117
{
    MiscState *s = opaque;
    uint32_t ret = 0;

    ret = s->config;
118
    trace_slavio_cfg_mem_readb(ret);
119 120 121
    return ret;
}

122
static CPUReadMemoryFunc * const slavio_cfg_mem_read[3] = {
123 124 125 126 127
    slavio_cfg_mem_readb,
    NULL,
    NULL,
};

128
static CPUWriteMemoryFunc * const slavio_cfg_mem_write[3] = {
129 130 131 132 133
    slavio_cfg_mem_writeb,
    NULL,
    NULL,
};

A
Anthony Liguori 已提交
134
static void slavio_diag_mem_writeb(void *opaque, target_phys_addr_t addr,
135
                                   uint32_t val)
B
bellard 已提交
136 137 138
{
    MiscState *s = opaque;

139
    trace_slavio_diag_mem_writeb(val & 0xff);
140
    s->diag = val & 0xff;
B
bellard 已提交
141 142
}

A
Anthony Liguori 已提交
143
static uint32_t slavio_diag_mem_readb(void *opaque, target_phys_addr_t addr)
B
bellard 已提交
144 145 146 147
{
    MiscState *s = opaque;
    uint32_t ret = 0;

148
    ret = s->diag;
149
    trace_slavio_diag_mem_readb(ret);
150 151 152
    return ret;
}

153
static CPUReadMemoryFunc * const slavio_diag_mem_read[3] = {
154 155 156 157 158
    slavio_diag_mem_readb,
    NULL,
    NULL,
};

159
static CPUWriteMemoryFunc * const slavio_diag_mem_write[3] = {
160 161 162 163 164
    slavio_diag_mem_writeb,
    NULL,
    NULL,
};

A
Anthony Liguori 已提交
165
static void slavio_mdm_mem_writeb(void *opaque, target_phys_addr_t addr,
166 167 168 169
                                  uint32_t val)
{
    MiscState *s = opaque;

170
    trace_slavio_mdm_mem_writeb(val & 0xff);
171 172 173
    s->mctrl = val & 0xff;
}

A
Anthony Liguori 已提交
174
static uint32_t slavio_mdm_mem_readb(void *opaque, target_phys_addr_t addr)
175 176 177 178 179
{
    MiscState *s = opaque;
    uint32_t ret = 0;

    ret = s->mctrl;
180
    trace_slavio_mdm_mem_readb(ret);
B
bellard 已提交
181 182 183
    return ret;
}

184
static CPUReadMemoryFunc * const slavio_mdm_mem_read[3] = {
185
    slavio_mdm_mem_readb,
186 187
    NULL,
    NULL,
B
bellard 已提交
188 189
};

190
static CPUWriteMemoryFunc * const slavio_mdm_mem_write[3] = {
191
    slavio_mdm_mem_writeb,
192 193
    NULL,
    NULL,
B
bellard 已提交
194 195
};

A
Anthony Liguori 已提交
196
static void slavio_aux1_mem_writeb(void *opaque, target_phys_addr_t addr,
197 198 199 200
                                   uint32_t val)
{
    MiscState *s = opaque;

201
    trace_slavio_aux1_mem_writeb(val & 0xff);
B
blueswir1 已提交
202 203 204 205 206 207 208 209
    if (val & AUX1_TC) {
        // Send a pulse to floppy terminal count line
        if (s->fdc_tc) {
            qemu_irq_raise(s->fdc_tc);
            qemu_irq_lower(s->fdc_tc);
        }
        val &= ~AUX1_TC;
    }
210 211 212
    s->aux1 = val & 0xff;
}

A
Anthony Liguori 已提交
213
static uint32_t slavio_aux1_mem_readb(void *opaque, target_phys_addr_t addr)
214 215 216 217 218
{
    MiscState *s = opaque;
    uint32_t ret = 0;

    ret = s->aux1;
219
    trace_slavio_aux1_mem_readb(ret);
220 221 222
    return ret;
}

223
static CPUReadMemoryFunc * const slavio_aux1_mem_read[3] = {
224 225 226 227 228
    slavio_aux1_mem_readb,
    NULL,
    NULL,
};

229
static CPUWriteMemoryFunc * const slavio_aux1_mem_write[3] = {
230 231 232 233 234
    slavio_aux1_mem_writeb,
    NULL,
    NULL,
};

A
Anthony Liguori 已提交
235
static void slavio_aux2_mem_writeb(void *opaque, target_phys_addr_t addr,
236 237 238 239 240
                                   uint32_t val)
{
    MiscState *s = opaque;

    val &= AUX2_PWRINTCLR | AUX2_PWROFF;
241
    trace_slavio_aux2_mem_writeb(val & 0xff);
242 243 244 245 246 247 248 249 250
    val |= s->aux2 & AUX2_PWRFAIL;
    if (val & AUX2_PWRINTCLR) // Clear Power Fail int
        val &= AUX2_PWROFF;
    s->aux2 = val;
    if (val & AUX2_PWROFF)
        qemu_system_shutdown_request();
    slavio_misc_update_irq(s);
}

A
Anthony Liguori 已提交
251
static uint32_t slavio_aux2_mem_readb(void *opaque, target_phys_addr_t addr)
252 253 254 255 256
{
    MiscState *s = opaque;
    uint32_t ret = 0;

    ret = s->aux2;
257
    trace_slavio_aux2_mem_readb(ret);
258 259 260
    return ret;
}

261
static CPUReadMemoryFunc * const slavio_aux2_mem_read[3] = {
262 263 264 265 266
    slavio_aux2_mem_readb,
    NULL,
    NULL,
};

267
static CPUWriteMemoryFunc * const slavio_aux2_mem_write[3] = {
268 269 270 271 272
    slavio_aux2_mem_writeb,
    NULL,
    NULL,
};

A
Anthony Liguori 已提交
273
static void apc_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
274
{
275
    APCState *s = opaque;
276

277
    trace_apc_mem_writeb(val & 0xff);
B
blueswir1 已提交
278
    qemu_irq_raise(s->cpu_halt);
279 280
}

A
Anthony Liguori 已提交
281
static uint32_t apc_mem_readb(void *opaque, target_phys_addr_t addr)
282 283 284
{
    uint32_t ret = 0;

285
    trace_apc_mem_readb(ret);
286 287 288
    return ret;
}

289
static CPUReadMemoryFunc * const apc_mem_read[3] = {
290 291 292 293 294
    apc_mem_readb,
    NULL,
    NULL,
};

295
static CPUWriteMemoryFunc * const apc_mem_write[3] = {
296 297 298 299 300
    apc_mem_writeb,
    NULL,
    NULL,
};

A
Anthony Liguori 已提交
301
static uint32_t slavio_sysctrl_mem_readl(void *opaque, target_phys_addr_t addr)
302 303
{
    MiscState *s = opaque;
304
    uint32_t ret = 0;
305

306
    switch (addr) {
307 308 309 310 311 312
    case 0:
        ret = s->sysctrl;
        break;
    default:
        break;
    }
313
    trace_slavio_sysctrl_mem_readl(ret);
314 315 316
    return ret;
}

A
Anthony Liguori 已提交
317
static void slavio_sysctrl_mem_writel(void *opaque, target_phys_addr_t addr,
318 319 320 321
                                      uint32_t val)
{
    MiscState *s = opaque;

322
    trace_slavio_sysctrl_mem_writel(val);
323
    switch (addr) {
324
    case 0:
325 326
        if (val & SYS_RESET) {
            s->sysctrl = SYS_RESETSTAT;
327 328 329 330 331 332 333 334
            qemu_system_reset_request();
        }
        break;
    default:
        break;
    }
}

335
static CPUReadMemoryFunc * const slavio_sysctrl_mem_read[3] = {
336 337
    NULL,
    NULL,
338 339 340
    slavio_sysctrl_mem_readl,
};

341
static CPUWriteMemoryFunc * const slavio_sysctrl_mem_write[3] = {
342 343
    NULL,
    NULL,
344 345 346
    slavio_sysctrl_mem_writel,
};

A
Anthony Liguori 已提交
347
static uint32_t slavio_led_mem_readw(void *opaque, target_phys_addr_t addr)
348 349
{
    MiscState *s = opaque;
350
    uint32_t ret = 0;
351

352
    switch (addr) {
353 354 355 356 357 358
    case 0:
        ret = s->leds;
        break;
    default:
        break;
    }
359
    trace_slavio_led_mem_readw(ret);
360 361 362
    return ret;
}

A
Anthony Liguori 已提交
363
static void slavio_led_mem_writew(void *opaque, target_phys_addr_t addr,
364 365 366 367
                                  uint32_t val)
{
    MiscState *s = opaque;

368
    trace_slavio_led_mem_readw(val & 0xffff);
369
    switch (addr) {
370
    case 0:
B
blueswir1 已提交
371
        s->leds = val;
372 373 374 375 376 377
        break;
    default:
        break;
    }
}

378
static CPUReadMemoryFunc * const slavio_led_mem_read[3] = {
379 380 381
    NULL,
    slavio_led_mem_readw,
    NULL,
382 383
};

384
static CPUWriteMemoryFunc * const slavio_led_mem_write[3] = {
385 386 387
    NULL,
    slavio_led_mem_writew,
    NULL,
388 389
};

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static const VMStateDescription vmstate_misc = {
    .name ="slavio_misc",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT32(dummy, MiscState),
        VMSTATE_UINT8(config, MiscState),
        VMSTATE_UINT8(aux1, MiscState),
        VMSTATE_UINT8(aux2, MiscState),
        VMSTATE_UINT8(diag, MiscState),
        VMSTATE_UINT8(mctrl, MiscState),
        VMSTATE_UINT8(sysctrl, MiscState),
        VMSTATE_END_OF_LIST()
    }
};
B
bellard 已提交
406

407
static int apc_init1(SysBusDevice *dev)
408 409 410
{
    APCState *s = FROM_SYSBUS(APCState, dev);
    int io;
B
bellard 已提交
411

412 413 414
    sysbus_init_irq(dev, &s->cpu_halt);

    /* Power management (APC) XXX: not a Slavio device */
415 416
    io = cpu_register_io_memory(apc_mem_read, apc_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
417
    sysbus_init_mmio(dev, MISC_SIZE, io);
418
    return 0;
419 420
}

421
static int slavio_misc_init1(SysBusDevice *dev)
422 423 424 425 426 427 428 429 430 431
{
    MiscState *s = FROM_SYSBUS(MiscState, dev);
    int io;

    sysbus_init_irq(dev, &s->irq);
    sysbus_init_irq(dev, &s->fdc_tc);

    /* 8 bit registers */
    /* Slavio control */
    io = cpu_register_io_memory(slavio_cfg_mem_read,
432 433
                                slavio_cfg_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
434 435 436 437
    sysbus_init_mmio(dev, MISC_SIZE, io);

    /* Diagnostics */
    io = cpu_register_io_memory(slavio_diag_mem_read,
438 439
                                slavio_diag_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
440 441 442 443
    sysbus_init_mmio(dev, MISC_SIZE, io);

    /* Modem control */
    io = cpu_register_io_memory(slavio_mdm_mem_read,
444 445
                                slavio_mdm_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
446 447 448 449 450
    sysbus_init_mmio(dev, MISC_SIZE, io);

    /* 16 bit registers */
    /* ss600mp diag LEDs */
    io = cpu_register_io_memory(slavio_led_mem_read,
451 452
                                slavio_led_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
453 454 455 456 457
    sysbus_init_mmio(dev, MISC_SIZE, io);

    /* 32 bit registers */
    /* System control */
    io = cpu_register_io_memory(slavio_sysctrl_mem_read,
458 459
                                slavio_sysctrl_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
460 461 462 463
    sysbus_init_mmio(dev, SYSCTRL_SIZE, io);

    /* AUX 1 (Misc System Functions) */
    io = cpu_register_io_memory(slavio_aux1_mem_read,
464 465
                                slavio_aux1_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
466 467 468 469
    sysbus_init_mmio(dev, MISC_SIZE, io);

    /* AUX 2 (Software Powerdown Control) */
    io = cpu_register_io_memory(slavio_aux2_mem_read,
470 471
                                slavio_aux2_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
472 473
    sysbus_init_mmio(dev, MISC_SIZE, io);

474 475
    qdev_init_gpio_in(&dev->qdev, slavio_set_power_fail, 1);

476
    return 0;
477
}
478

479 480 481 482
static SysBusDeviceInfo slavio_misc_info = {
    .init = slavio_misc_init1,
    .qdev.name  = "slavio_misc",
    .qdev.size  = sizeof(MiscState),
483 484
    .qdev.vmsd  = &vmstate_misc,
    .qdev.reset  = slavio_misc_reset,
485 486 487 488 489 490 491 492 493 494 495 496
};

static SysBusDeviceInfo apc_info = {
    .init = apc_init1,
    .qdev.name  = "apc",
    .qdev.size  = sizeof(MiscState),
};

static void slavio_misc_register_devices(void)
{
    sysbus_register_withprop(&slavio_misc_info);
    sysbus_register_withprop(&apc_info);
B
bellard 已提交
497
}
498 499

device_init(slavio_misc_register_devices)