blob: cfec1d9cd158402b35cf116f6b43236ed3e703f4 [file] [log] [blame]
bellardb8174932006-09-10 19:25:12 +00001/*
2 * QEMU Crystal CS4231 audio chip emulation
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Blue Swirlfa28ec52009-07-16 13:47:45 +000024
Blue Swirlfa28ec52009-07-16 13:47:45 +000025#include "sysbus.h"
Blue Swirl97bf4852010-10-31 09:24:14 +000026#include "trace.h"
bellardb8174932006-09-10 19:25:12 +000027
28/*
29 * In addition to Crystal CS4231 there is a DMA controller on Sparc.
30 */
blueswir1e64d7d52008-12-02 17:47:02 +000031#define CS_SIZE 0x40
bellardb8174932006-09-10 19:25:12 +000032#define CS_REGS 16
33#define CS_DREGS 32
34#define CS_MAXDREG (CS_DREGS - 1)
35
36typedef struct CSState {
Blue Swirlfa28ec52009-07-16 13:47:45 +000037 SysBusDevice busdev;
Avi Kivitydf1820432011-11-09 16:10:07 +020038 MemoryRegion iomem;
Blue Swirlfa28ec52009-07-16 13:47:45 +000039 qemu_irq irq;
bellardb8174932006-09-10 19:25:12 +000040 uint32_t regs[CS_REGS];
41 uint8_t dregs[CS_DREGS];
bellardb8174932006-09-10 19:25:12 +000042} CSState;
43
44#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
45#define CS_VER 0xa0
46#define CS_CDC_VER 0x8a
47
Blue Swirl82d4c6e2009-10-24 16:20:32 +000048static void cs_reset(DeviceState *d)
bellardb8174932006-09-10 19:25:12 +000049{
Blue Swirl82d4c6e2009-10-24 16:20:32 +000050 CSState *s = container_of(d, CSState, busdev.qdev);
bellardb8174932006-09-10 19:25:12 +000051
52 memset(s->regs, 0, CS_REGS * 4);
53 memset(s->dregs, 0, CS_DREGS);
54 s->dregs[12] = CS_CDC_VER;
55 s->dregs[25] = CS_VER;
56}
57
Avi Kivitydf1820432011-11-09 16:10:07 +020058static uint64_t cs_mem_read(void *opaque, target_phys_addr_t addr,
59 unsigned size)
bellardb8174932006-09-10 19:25:12 +000060{
61 CSState *s = opaque;
62 uint32_t saddr, ret;
63
blueswir1e64d7d52008-12-02 17:47:02 +000064 saddr = addr >> 2;
bellardb8174932006-09-10 19:25:12 +000065 switch (saddr) {
66 case 1:
67 switch (CS_RAP(s)) {
68 case 3: // Write only
69 ret = 0;
70 break;
71 default:
72 ret = s->dregs[CS_RAP(s)];
73 break;
74 }
Blue Swirl97bf4852010-10-31 09:24:14 +000075 trace_cs4231_mem_readl_dreg(CS_RAP(s), ret);
blueswir1f930d072007-10-06 11:28:21 +000076 break;
bellardb8174932006-09-10 19:25:12 +000077 default:
78 ret = s->regs[saddr];
Blue Swirl97bf4852010-10-31 09:24:14 +000079 trace_cs4231_mem_readl_reg(saddr, ret);
blueswir1f930d072007-10-06 11:28:21 +000080 break;
bellardb8174932006-09-10 19:25:12 +000081 }
82 return ret;
83}
84
Avi Kivitydf1820432011-11-09 16:10:07 +020085static void cs_mem_write(void *opaque, target_phys_addr_t addr,
86 uint64_t val, unsigned size)
bellardb8174932006-09-10 19:25:12 +000087{
88 CSState *s = opaque;
89 uint32_t saddr;
90
blueswir1e64d7d52008-12-02 17:47:02 +000091 saddr = addr >> 2;
Blue Swirl97bf4852010-10-31 09:24:14 +000092 trace_cs4231_mem_writel_reg(saddr, s->regs[saddr], val);
bellardb8174932006-09-10 19:25:12 +000093 switch (saddr) {
94 case 1:
Blue Swirl97bf4852010-10-31 09:24:14 +000095 trace_cs4231_mem_writel_dreg(CS_RAP(s), s->dregs[CS_RAP(s)], val);
bellardb8174932006-09-10 19:25:12 +000096 switch(CS_RAP(s)) {
97 case 11:
98 case 25: // Read only
99 break;
100 case 12:
101 val &= 0x40;
102 val |= CS_CDC_VER; // Codec version
103 s->dregs[CS_RAP(s)] = val;
104 break;
105 default:
106 s->dregs[CS_RAP(s)] = val;
107 break;
108 }
109 break;
110 case 2: // Read only
111 break;
112 case 4:
Blue Swirl82d4c6e2009-10-24 16:20:32 +0000113 if (val & 1) {
114 cs_reset(&s->busdev.qdev);
115 }
bellardb8174932006-09-10 19:25:12 +0000116 val &= 0x7f;
117 s->regs[saddr] = val;
118 break;
119 default:
120 s->regs[saddr] = val;
blueswir1f930d072007-10-06 11:28:21 +0000121 break;
bellardb8174932006-09-10 19:25:12 +0000122 }
123}
124
Avi Kivitydf1820432011-11-09 16:10:07 +0200125static const MemoryRegionOps cs_mem_ops = {
126 .read = cs_mem_read,
127 .write = cs_mem_write,
128 .endianness = DEVICE_NATIVE_ENDIAN,
bellardb8174932006-09-10 19:25:12 +0000129};
130
Blue Swirl82d4c6e2009-10-24 16:20:32 +0000131static const VMStateDescription vmstate_cs4231 = {
132 .name ="cs4231",
133 .version_id = 1,
134 .minimum_version_id = 1,
135 .minimum_version_id_old = 1,
136 .fields = (VMStateField []) {
137 VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
138 VMSTATE_UINT8_ARRAY(dregs, CSState, CS_DREGS),
139 VMSTATE_END_OF_LIST()
140 }
141};
bellardb8174932006-09-10 19:25:12 +0000142
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200143static int cs4231_init1(SysBusDevice *dev)
bellardb8174932006-09-10 19:25:12 +0000144{
Blue Swirlfa28ec52009-07-16 13:47:45 +0000145 CSState *s = FROM_SYSBUS(CSState, dev);
bellardb8174932006-09-10 19:25:12 +0000146
Avi Kivitydf1820432011-11-09 16:10:07 +0200147 memory_region_init_io(&s->iomem, &cs_mem_ops, s, "cs4321", CS_SIZE);
Avi Kivity750ecd42011-11-27 11:38:10 +0200148 sysbus_init_mmio(dev, &s->iomem);
Blue Swirlfa28ec52009-07-16 13:47:45 +0000149 sysbus_init_irq(dev, &s->irq);
bellardb8174932006-09-10 19:25:12 +0000150
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200151 return 0;
bellardb8174932006-09-10 19:25:12 +0000152}
Blue Swirlfa28ec52009-07-16 13:47:45 +0000153
Anthony Liguori999e12b2012-01-24 13:12:29 -0600154static Property cs4231_properties[] = {
155 {.name = NULL},
156};
157
158static void cs4231_class_init(ObjectClass *klass, void *data)
159{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600160 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600161 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
162
163 k->init = cs4231_init1;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600164 dc->reset = cs_reset;
165 dc->vmsd = &vmstate_cs4231;
166 dc->props = cs4231_properties;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600167}
168
Anthony Liguori39bffca2011-12-07 21:34:16 -0600169static TypeInfo cs4231_info = {
170 .name = "SUNW,CS4231",
171 .parent = TYPE_SYS_BUS_DEVICE,
172 .instance_size = sizeof(CSState),
173 .class_init = cs4231_class_init,
Blue Swirlfa28ec52009-07-16 13:47:45 +0000174};
175
Andreas Färber83f7d432012-02-09 15:20:55 +0100176static void cs4231_register_types(void)
Blue Swirlfa28ec52009-07-16 13:47:45 +0000177{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600178 type_register_static(&cs4231_info);
Blue Swirlfa28ec52009-07-16 13:47:45 +0000179}
180
Andreas Färber83f7d432012-02-09 15:20:55 +0100181type_init(cs4231_register_types)