Statistics
| Branch: | Revision:

root / hw / cs4231.c @ 4f4cc0ef

History | View | Annotate | Download (4.9 kB)

1 b8174937 bellard
/*
2 b8174937 bellard
 * QEMU Crystal CS4231 audio chip emulation
3 b8174937 bellard
 *
4 b8174937 bellard
 * Copyright (c) 2006 Fabrice Bellard
5 b8174937 bellard
 *
6 b8174937 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 b8174937 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 b8174937 bellard
 * in the Software without restriction, including without limitation the rights
9 b8174937 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 b8174937 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 b8174937 bellard
 * furnished to do so, subject to the following conditions:
12 b8174937 bellard
 *
13 b8174937 bellard
 * The above copyright notice and this permission notice shall be included in
14 b8174937 bellard
 * all copies or substantial portions of the Software.
15 b8174937 bellard
 *
16 b8174937 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 b8174937 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 b8174937 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 b8174937 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 b8174937 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 b8174937 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 b8174937 bellard
 * THE SOFTWARE.
23 b8174937 bellard
 */
24 fa28ec52 Blue Swirl
25 87ecb68b pbrook
#include "sun4m.h"
26 fa28ec52 Blue Swirl
#include "sysbus.h"
27 b8174937 bellard
28 b8174937 bellard
/* debug CS4231 */
29 b8174937 bellard
//#define DEBUG_CS
30 b8174937 bellard
31 b8174937 bellard
/*
32 b8174937 bellard
 * In addition to Crystal CS4231 there is a DMA controller on Sparc.
33 b8174937 bellard
 */
34 e64d7d59 blueswir1
#define CS_SIZE 0x40
35 b8174937 bellard
#define CS_REGS 16
36 b8174937 bellard
#define CS_DREGS 32
37 b8174937 bellard
#define CS_MAXDREG (CS_DREGS - 1)
38 b8174937 bellard
39 b8174937 bellard
typedef struct CSState {
40 fa28ec52 Blue Swirl
    SysBusDevice busdev;
41 fa28ec52 Blue Swirl
    qemu_irq irq;
42 b8174937 bellard
    uint32_t regs[CS_REGS];
43 b8174937 bellard
    uint8_t dregs[CS_DREGS];
44 b8174937 bellard
} CSState;
45 b8174937 bellard
46 b8174937 bellard
#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
47 b8174937 bellard
#define CS_VER 0xa0
48 b8174937 bellard
#define CS_CDC_VER 0x8a
49 b8174937 bellard
50 b8174937 bellard
#ifdef DEBUG_CS
51 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
52 001faf32 Blue Swirl
    do { printf("CS: " fmt , ## __VA_ARGS__); } while (0)
53 b8174937 bellard
#else
54 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)
55 b8174937 bellard
#endif
56 b8174937 bellard
57 b8174937 bellard
static void cs_reset(void *opaque)
58 b8174937 bellard
{
59 b8174937 bellard
    CSState *s = opaque;
60 b8174937 bellard
61 b8174937 bellard
    memset(s->regs, 0, CS_REGS * 4);
62 b8174937 bellard
    memset(s->dregs, 0, CS_DREGS);
63 b8174937 bellard
    s->dregs[12] = CS_CDC_VER;
64 b8174937 bellard
    s->dregs[25] = CS_VER;
65 b8174937 bellard
}
66 b8174937 bellard
67 b8174937 bellard
static uint32_t cs_mem_readl(void *opaque, target_phys_addr_t addr)
68 b8174937 bellard
{
69 b8174937 bellard
    CSState *s = opaque;
70 b8174937 bellard
    uint32_t saddr, ret;
71 b8174937 bellard
72 e64d7d59 blueswir1
    saddr = addr >> 2;
73 b8174937 bellard
    switch (saddr) {
74 b8174937 bellard
    case 1:
75 b8174937 bellard
        switch (CS_RAP(s)) {
76 b8174937 bellard
        case 3: // Write only
77 b8174937 bellard
            ret = 0;
78 b8174937 bellard
            break;
79 b8174937 bellard
        default:
80 b8174937 bellard
            ret = s->dregs[CS_RAP(s)];
81 b8174937 bellard
            break;
82 b8174937 bellard
        }
83 b8174937 bellard
        DPRINTF("read dreg[%d]: 0x%8.8x\n", CS_RAP(s), ret);
84 f930d07e blueswir1
        break;
85 b8174937 bellard
    default:
86 b8174937 bellard
        ret = s->regs[saddr];
87 b8174937 bellard
        DPRINTF("read reg[%d]: 0x%8.8x\n", saddr, ret);
88 f930d07e blueswir1
        break;
89 b8174937 bellard
    }
90 b8174937 bellard
    return ret;
91 b8174937 bellard
}
92 b8174937 bellard
93 b8174937 bellard
static void cs_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
94 b8174937 bellard
{
95 b8174937 bellard
    CSState *s = opaque;
96 b8174937 bellard
    uint32_t saddr;
97 b8174937 bellard
98 e64d7d59 blueswir1
    saddr = addr >> 2;
99 b8174937 bellard
    DPRINTF("write reg[%d]: 0x%8.8x -> 0x%8.8x\n", saddr, s->regs[saddr], val);
100 b8174937 bellard
    switch (saddr) {
101 b8174937 bellard
    case 1:
102 77f193da blueswir1
        DPRINTF("write dreg[%d]: 0x%2.2x -> 0x%2.2x\n", CS_RAP(s),
103 77f193da blueswir1
                s->dregs[CS_RAP(s)], val);
104 b8174937 bellard
        switch(CS_RAP(s)) {
105 b8174937 bellard
        case 11:
106 b8174937 bellard
        case 25: // Read only
107 b8174937 bellard
            break;
108 b8174937 bellard
        case 12:
109 b8174937 bellard
            val &= 0x40;
110 b8174937 bellard
            val |= CS_CDC_VER; // Codec version
111 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
112 b8174937 bellard
            break;
113 b8174937 bellard
        default:
114 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
115 b8174937 bellard
            break;
116 b8174937 bellard
        }
117 b8174937 bellard
        break;
118 b8174937 bellard
    case 2: // Read only
119 b8174937 bellard
        break;
120 b8174937 bellard
    case 4:
121 b8174937 bellard
        if (val & 1)
122 b8174937 bellard
            cs_reset(s);
123 b8174937 bellard
        val &= 0x7f;
124 b8174937 bellard
        s->regs[saddr] = val;
125 b8174937 bellard
        break;
126 b8174937 bellard
    default:
127 b8174937 bellard
        s->regs[saddr] = val;
128 f930d07e blueswir1
        break;
129 b8174937 bellard
    }
130 b8174937 bellard
}
131 b8174937 bellard
132 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const cs_mem_read[3] = {
133 b8174937 bellard
    cs_mem_readl,
134 b8174937 bellard
    cs_mem_readl,
135 b8174937 bellard
    cs_mem_readl,
136 b8174937 bellard
};
137 b8174937 bellard
138 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const cs_mem_write[3] = {
139 b8174937 bellard
    cs_mem_writel,
140 b8174937 bellard
    cs_mem_writel,
141 b8174937 bellard
    cs_mem_writel,
142 b8174937 bellard
};
143 b8174937 bellard
144 b8174937 bellard
static void cs_save(QEMUFile *f, void *opaque)
145 b8174937 bellard
{
146 b8174937 bellard
    CSState *s = opaque;
147 b8174937 bellard
    unsigned int i;
148 b8174937 bellard
149 b8174937 bellard
    for (i = 0; i < CS_REGS; i++)
150 b8174937 bellard
        qemu_put_be32s(f, &s->regs[i]);
151 b8174937 bellard
152 b8174937 bellard
    qemu_put_buffer(f, s->dregs, CS_DREGS);
153 b8174937 bellard
}
154 b8174937 bellard
155 b8174937 bellard
static int cs_load(QEMUFile *f, void *opaque, int version_id)
156 b8174937 bellard
{
157 b8174937 bellard
    CSState *s = opaque;
158 b8174937 bellard
    unsigned int i;
159 b8174937 bellard
160 b8174937 bellard
    if (version_id > 1)
161 b8174937 bellard
        return -EINVAL;
162 b8174937 bellard
163 b8174937 bellard
    for (i = 0; i < CS_REGS; i++)
164 b8174937 bellard
        qemu_get_be32s(f, &s->regs[i]);
165 b8174937 bellard
166 b8174937 bellard
    qemu_get_buffer(f, s->dregs, CS_DREGS);
167 b8174937 bellard
    return 0;
168 b8174937 bellard
}
169 b8174937 bellard
170 81a322d4 Gerd Hoffmann
static int cs4231_init1(SysBusDevice *dev)
171 b8174937 bellard
{
172 fa28ec52 Blue Swirl
    int io;
173 fa28ec52 Blue Swirl
    CSState *s = FROM_SYSBUS(CSState, dev);
174 b8174937 bellard
175 fa28ec52 Blue Swirl
    io = cpu_register_io_memory(cs_mem_read, cs_mem_write, s);
176 fa28ec52 Blue Swirl
    sysbus_init_mmio(dev, CS_SIZE, io);
177 fa28ec52 Blue Swirl
    sysbus_init_irq(dev, &s->irq);
178 b8174937 bellard
179 fa28ec52 Blue Swirl
    register_savevm("cs4231", -1, 1, cs_save, cs_load, s);
180 a08d4367 Jan Kiszka
    qemu_register_reset(cs_reset, s);
181 b8174937 bellard
    cs_reset(s);
182 81a322d4 Gerd Hoffmann
    return 0;
183 b8174937 bellard
}
184 fa28ec52 Blue Swirl
185 fa28ec52 Blue Swirl
static SysBusDeviceInfo cs4231_info = {
186 fa28ec52 Blue Swirl
    .init = cs4231_init1,
187 fa28ec52 Blue Swirl
    .qdev.name  = "SUNW,CS4231",
188 fa28ec52 Blue Swirl
    .qdev.size  = sizeof(CSState),
189 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
190 fa28ec52 Blue Swirl
        {.name = NULL}
191 fa28ec52 Blue Swirl
    }
192 fa28ec52 Blue Swirl
};
193 fa28ec52 Blue Swirl
194 fa28ec52 Blue Swirl
static void cs4231_register_devices(void)
195 fa28ec52 Blue Swirl
{
196 fa28ec52 Blue Swirl
    sysbus_register_withprop(&cs4231_info);
197 fa28ec52 Blue Swirl
}
198 fa28ec52 Blue Swirl
199 fa28ec52 Blue Swirl
device_init(cs4231_register_devices)