cs4231.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * QEMU Crystal CS4231 audio chip emulation
 *
 * Copyright (c) 2006 Fabrice Bellard
 *
 * 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.
 */
B
Blue Swirl 已提交
24

P
pbrook 已提交
25
#include "sun4m.h"
B
Blue Swirl 已提交
26
#include "sysbus.h"
27 28 29 30 31 32 33

/* debug CS4231 */
//#define DEBUG_CS

/*
 * In addition to Crystal CS4231 there is a DMA controller on Sparc.
 */
B
blueswir1 已提交
34
#define CS_SIZE 0x40
35 36 37 38 39
#define CS_REGS 16
#define CS_DREGS 32
#define CS_MAXDREG (CS_DREGS - 1)

typedef struct CSState {
B
Blue Swirl 已提交
40 41
    SysBusDevice busdev;
    qemu_irq irq;
42 43 44 45 46 47 48 49 50
    uint32_t regs[CS_REGS];
    uint8_t dregs[CS_DREGS];
} CSState;

#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
#define CS_VER 0xa0
#define CS_CDC_VER 0x8a

#ifdef DEBUG_CS
51 52
#define DPRINTF(fmt, ...)                                       \
    do { printf("CS: " fmt , ## __VA_ARGS__); } while (0)
53
#else
54
#define DPRINTF(fmt, ...)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#endif

static void cs_reset(void *opaque)
{
    CSState *s = opaque;

    memset(s->regs, 0, CS_REGS * 4);
    memset(s->dregs, 0, CS_DREGS);
    s->dregs[12] = CS_CDC_VER;
    s->dregs[25] = CS_VER;
}

static uint32_t cs_mem_readl(void *opaque, target_phys_addr_t addr)
{
    CSState *s = opaque;
    uint32_t saddr, ret;

B
blueswir1 已提交
72
    saddr = addr >> 2;
73 74 75 76 77 78 79 80 81 82 83
    switch (saddr) {
    case 1:
        switch (CS_RAP(s)) {
        case 3: // Write only
            ret = 0;
            break;
        default:
            ret = s->dregs[CS_RAP(s)];
            break;
        }
        DPRINTF("read dreg[%d]: 0x%8.8x\n", CS_RAP(s), ret);
B
blueswir1 已提交
84
        break;
85 86 87
    default:
        ret = s->regs[saddr];
        DPRINTF("read reg[%d]: 0x%8.8x\n", saddr, ret);
B
blueswir1 已提交
88
        break;
89 90 91 92 93 94 95 96 97
    }
    return ret;
}

static void cs_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
{
    CSState *s = opaque;
    uint32_t saddr;

B
blueswir1 已提交
98
    saddr = addr >> 2;
99 100 101
    DPRINTF("write reg[%d]: 0x%8.8x -> 0x%8.8x\n", saddr, s->regs[saddr], val);
    switch (saddr) {
    case 1:
B
blueswir1 已提交
102 103
        DPRINTF("write dreg[%d]: 0x%2.2x -> 0x%2.2x\n", CS_RAP(s),
                s->dregs[CS_RAP(s)], val);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        switch(CS_RAP(s)) {
        case 11:
        case 25: // Read only
            break;
        case 12:
            val &= 0x40;
            val |= CS_CDC_VER; // Codec version
            s->dregs[CS_RAP(s)] = val;
            break;
        default:
            s->dregs[CS_RAP(s)] = val;
            break;
        }
        break;
    case 2: // Read only
        break;
    case 4:
        if (val & 1)
            cs_reset(s);
        val &= 0x7f;
        s->regs[saddr] = val;
        break;
    default:
        s->regs[saddr] = val;
B
blueswir1 已提交
128
        break;
129 130 131 132 133 134 135 136 137 138 139 140 141 142 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
    }
}

static CPUReadMemoryFunc *cs_mem_read[3] = {
    cs_mem_readl,
    cs_mem_readl,
    cs_mem_readl,
};

static CPUWriteMemoryFunc *cs_mem_write[3] = {
    cs_mem_writel,
    cs_mem_writel,
    cs_mem_writel,
};

static void cs_save(QEMUFile *f, void *opaque)
{
    CSState *s = opaque;
    unsigned int i;

    for (i = 0; i < CS_REGS; i++)
        qemu_put_be32s(f, &s->regs[i]);

    qemu_put_buffer(f, s->dregs, CS_DREGS);
}

static int cs_load(QEMUFile *f, void *opaque, int version_id)
{
    CSState *s = opaque;
    unsigned int i;

    if (version_id > 1)
        return -EINVAL;

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

    qemu_get_buffer(f, s->dregs, CS_DREGS);
    return 0;
}

B
Blue Swirl 已提交
170
static void cs4231_init1(SysBusDevice *dev)
171
{
B
Blue Swirl 已提交
172 173
    int io;
    CSState *s = FROM_SYSBUS(CSState, dev);
174

B
Blue Swirl 已提交
175 176 177
    io = cpu_register_io_memory(cs_mem_read, cs_mem_write, s);
    sysbus_init_mmio(dev, CS_SIZE, io);
    sysbus_init_irq(dev, &s->irq);
178

B
Blue Swirl 已提交
179
    register_savevm("cs4231", -1, 1, cs_save, cs_load, s);
180
    qemu_register_reset(cs_reset, s);
181 182
    cs_reset(s);
}
B
Blue Swirl 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

static SysBusDeviceInfo cs4231_info = {
    .init = cs4231_init1,
    .qdev.name  = "SUNW,CS4231",
    .qdev.size  = sizeof(CSState),
    .qdev.props = (DevicePropList[]) {
        {.name = NULL}
    }
};

static void cs4231_register_devices(void)
{
    sysbus_register_withprop(&cs4231_info);
}

device_init(cs4231_register_devices)