Statistics
| Branch: | Revision:

root / hw / cs4231.c @ 5414dec6

History | View | Annotate | Download (4.7 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 fa28ec52 Blue Swirl
#include "sysbus.h"
26 97bf4851 Blue Swirl
#include "trace.h"
27 b8174937 bellard
28 b8174937 bellard
/*
29 b8174937 bellard
 * In addition to Crystal CS4231 there is a DMA controller on Sparc.
30 b8174937 bellard
 */
31 e64d7d59 blueswir1
#define CS_SIZE 0x40
32 b8174937 bellard
#define CS_REGS 16
33 b8174937 bellard
#define CS_DREGS 32
34 b8174937 bellard
#define CS_MAXDREG (CS_DREGS - 1)
35 b8174937 bellard
36 b8174937 bellard
typedef struct CSState {
37 fa28ec52 Blue Swirl
    SysBusDevice busdev;
38 df182043 Avi Kivity
    MemoryRegion iomem;
39 fa28ec52 Blue Swirl
    qemu_irq irq;
40 b8174937 bellard
    uint32_t regs[CS_REGS];
41 b8174937 bellard
    uint8_t dregs[CS_DREGS];
42 b8174937 bellard
} CSState;
43 b8174937 bellard
44 b8174937 bellard
#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
45 b8174937 bellard
#define CS_VER 0xa0
46 b8174937 bellard
#define CS_CDC_VER 0x8a
47 b8174937 bellard
48 82d4c6e6 Blue Swirl
static void cs_reset(DeviceState *d)
49 b8174937 bellard
{
50 82d4c6e6 Blue Swirl
    CSState *s = container_of(d, CSState, busdev.qdev);
51 b8174937 bellard
52 b8174937 bellard
    memset(s->regs, 0, CS_REGS * 4);
53 b8174937 bellard
    memset(s->dregs, 0, CS_DREGS);
54 b8174937 bellard
    s->dregs[12] = CS_CDC_VER;
55 b8174937 bellard
    s->dregs[25] = CS_VER;
56 b8174937 bellard
}
57 b8174937 bellard
58 df182043 Avi Kivity
static uint64_t cs_mem_read(void *opaque, target_phys_addr_t addr,
59 df182043 Avi Kivity
                            unsigned size)
60 b8174937 bellard
{
61 b8174937 bellard
    CSState *s = opaque;
62 b8174937 bellard
    uint32_t saddr, ret;
63 b8174937 bellard
64 e64d7d59 blueswir1
    saddr = addr >> 2;
65 b8174937 bellard
    switch (saddr) {
66 b8174937 bellard
    case 1:
67 b8174937 bellard
        switch (CS_RAP(s)) {
68 b8174937 bellard
        case 3: // Write only
69 b8174937 bellard
            ret = 0;
70 b8174937 bellard
            break;
71 b8174937 bellard
        default:
72 b8174937 bellard
            ret = s->dregs[CS_RAP(s)];
73 b8174937 bellard
            break;
74 b8174937 bellard
        }
75 97bf4851 Blue Swirl
        trace_cs4231_mem_readl_dreg(CS_RAP(s), ret);
76 f930d07e blueswir1
        break;
77 b8174937 bellard
    default:
78 b8174937 bellard
        ret = s->regs[saddr];
79 97bf4851 Blue Swirl
        trace_cs4231_mem_readl_reg(saddr, ret);
80 f930d07e blueswir1
        break;
81 b8174937 bellard
    }
82 b8174937 bellard
    return ret;
83 b8174937 bellard
}
84 b8174937 bellard
85 df182043 Avi Kivity
static void cs_mem_write(void *opaque, target_phys_addr_t addr,
86 df182043 Avi Kivity
                         uint64_t val, unsigned size)
87 b8174937 bellard
{
88 b8174937 bellard
    CSState *s = opaque;
89 b8174937 bellard
    uint32_t saddr;
90 b8174937 bellard
91 e64d7d59 blueswir1
    saddr = addr >> 2;
92 97bf4851 Blue Swirl
    trace_cs4231_mem_writel_reg(saddr, s->regs[saddr], val);
93 b8174937 bellard
    switch (saddr) {
94 b8174937 bellard
    case 1:
95 97bf4851 Blue Swirl
        trace_cs4231_mem_writel_dreg(CS_RAP(s), s->dregs[CS_RAP(s)], val);
96 b8174937 bellard
        switch(CS_RAP(s)) {
97 b8174937 bellard
        case 11:
98 b8174937 bellard
        case 25: // Read only
99 b8174937 bellard
            break;
100 b8174937 bellard
        case 12:
101 b8174937 bellard
            val &= 0x40;
102 b8174937 bellard
            val |= CS_CDC_VER; // Codec version
103 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
104 b8174937 bellard
            break;
105 b8174937 bellard
        default:
106 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
107 b8174937 bellard
            break;
108 b8174937 bellard
        }
109 b8174937 bellard
        break;
110 b8174937 bellard
    case 2: // Read only
111 b8174937 bellard
        break;
112 b8174937 bellard
    case 4:
113 82d4c6e6 Blue Swirl
        if (val & 1) {
114 82d4c6e6 Blue Swirl
            cs_reset(&s->busdev.qdev);
115 82d4c6e6 Blue Swirl
        }
116 b8174937 bellard
        val &= 0x7f;
117 b8174937 bellard
        s->regs[saddr] = val;
118 b8174937 bellard
        break;
119 b8174937 bellard
    default:
120 b8174937 bellard
        s->regs[saddr] = val;
121 f930d07e blueswir1
        break;
122 b8174937 bellard
    }
123 b8174937 bellard
}
124 b8174937 bellard
125 df182043 Avi Kivity
static const MemoryRegionOps cs_mem_ops = {
126 df182043 Avi Kivity
    .read = cs_mem_read,
127 df182043 Avi Kivity
    .write = cs_mem_write,
128 df182043 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
129 b8174937 bellard
};
130 b8174937 bellard
131 82d4c6e6 Blue Swirl
static const VMStateDescription vmstate_cs4231 = {
132 82d4c6e6 Blue Swirl
    .name ="cs4231",
133 82d4c6e6 Blue Swirl
    .version_id = 1,
134 82d4c6e6 Blue Swirl
    .minimum_version_id = 1,
135 82d4c6e6 Blue Swirl
    .minimum_version_id_old = 1,
136 82d4c6e6 Blue Swirl
    .fields      = (VMStateField []) {
137 82d4c6e6 Blue Swirl
        VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
138 82d4c6e6 Blue Swirl
        VMSTATE_UINT8_ARRAY(dregs, CSState, CS_DREGS),
139 82d4c6e6 Blue Swirl
        VMSTATE_END_OF_LIST()
140 82d4c6e6 Blue Swirl
    }
141 82d4c6e6 Blue Swirl
};
142 b8174937 bellard
143 81a322d4 Gerd Hoffmann
static int cs4231_init1(SysBusDevice *dev)
144 b8174937 bellard
{
145 fa28ec52 Blue Swirl
    CSState *s = FROM_SYSBUS(CSState, dev);
146 b8174937 bellard
147 df182043 Avi Kivity
    memory_region_init_io(&s->iomem, &cs_mem_ops, s, "cs4321", CS_SIZE);
148 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->iomem);
149 fa28ec52 Blue Swirl
    sysbus_init_irq(dev, &s->irq);
150 b8174937 bellard
151 81a322d4 Gerd Hoffmann
    return 0;
152 b8174937 bellard
}
153 fa28ec52 Blue Swirl
154 999e12bb Anthony Liguori
static Property cs4231_properties[] = {
155 999e12bb Anthony Liguori
    {.name = NULL},
156 999e12bb Anthony Liguori
};
157 999e12bb Anthony Liguori
158 999e12bb Anthony Liguori
static void cs4231_class_init(ObjectClass *klass, void *data)
159 999e12bb Anthony Liguori
{
160 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
161 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
162 999e12bb Anthony Liguori
163 999e12bb Anthony Liguori
    k->init = cs4231_init1;
164 39bffca2 Anthony Liguori
    dc->reset = cs_reset;
165 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_cs4231;
166 39bffca2 Anthony Liguori
    dc->props = cs4231_properties;
167 999e12bb Anthony Liguori
}
168 999e12bb Anthony Liguori
169 39bffca2 Anthony Liguori
static TypeInfo cs4231_info = {
170 39bffca2 Anthony Liguori
    .name          = "SUNW,CS4231",
171 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
172 39bffca2 Anthony Liguori
    .instance_size = sizeof(CSState),
173 39bffca2 Anthony Liguori
    .class_init    = cs4231_class_init,
174 fa28ec52 Blue Swirl
};
175 fa28ec52 Blue Swirl
176 83f7d43a Andreas Färber
static void cs4231_register_types(void)
177 fa28ec52 Blue Swirl
{
178 39bffca2 Anthony Liguori
    type_register_static(&cs4231_info);
179 fa28ec52 Blue Swirl
}
180 fa28ec52 Blue Swirl
181 83f7d43a Andreas Färber
type_init(cs4231_register_types)