blob: 2977101e39ce838da49f43f0b098a5b51322060c [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;
38 qemu_irq irq;
bellardb8174932006-09-10 19:25:12 +000039 uint32_t regs[CS_REGS];
40 uint8_t dregs[CS_DREGS];
bellardb8174932006-09-10 19:25:12 +000041} CSState;
42
43#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
44#define CS_VER 0xa0
45#define CS_CDC_VER 0x8a
46
Blue Swirl82d4c6e2009-10-24 16:20:32 +000047static void cs_reset(DeviceState *d)
bellardb8174932006-09-10 19:25:12 +000048{
Blue Swirl82d4c6e2009-10-24 16:20:32 +000049 CSState *s = container_of(d, CSState, busdev.qdev);
bellardb8174932006-09-10 19:25:12 +000050
51 memset(s->regs, 0, CS_REGS * 4);
52 memset(s->dregs, 0, CS_DREGS);
53 s->dregs[12] = CS_CDC_VER;
54 s->dregs[25] = CS_VER;
55}
56
Anthony Liguoric227f092009-10-01 16:12:16 -050057static uint32_t cs_mem_readl(void *opaque, target_phys_addr_t addr)
bellardb8174932006-09-10 19:25:12 +000058{
59 CSState *s = opaque;
60 uint32_t saddr, ret;
61
blueswir1e64d7d52008-12-02 17:47:02 +000062 saddr = addr >> 2;
bellardb8174932006-09-10 19:25:12 +000063 switch (saddr) {
64 case 1:
65 switch (CS_RAP(s)) {
66 case 3: // Write only
67 ret = 0;
68 break;
69 default:
70 ret = s->dregs[CS_RAP(s)];
71 break;
72 }
Blue Swirl97bf4852010-10-31 09:24:14 +000073 trace_cs4231_mem_readl_dreg(CS_RAP(s), ret);
blueswir1f930d072007-10-06 11:28:21 +000074 break;
bellardb8174932006-09-10 19:25:12 +000075 default:
76 ret = s->regs[saddr];
Blue Swirl97bf4852010-10-31 09:24:14 +000077 trace_cs4231_mem_readl_reg(saddr, ret);
blueswir1f930d072007-10-06 11:28:21 +000078 break;
bellardb8174932006-09-10 19:25:12 +000079 }
80 return ret;
81}
82
Anthony Liguoric227f092009-10-01 16:12:16 -050083static void cs_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellardb8174932006-09-10 19:25:12 +000084{
85 CSState *s = opaque;
86 uint32_t saddr;
87
blueswir1e64d7d52008-12-02 17:47:02 +000088 saddr = addr >> 2;
Blue Swirl97bf4852010-10-31 09:24:14 +000089 trace_cs4231_mem_writel_reg(saddr, s->regs[saddr], val);
bellardb8174932006-09-10 19:25:12 +000090 switch (saddr) {
91 case 1:
Blue Swirl97bf4852010-10-31 09:24:14 +000092 trace_cs4231_mem_writel_dreg(CS_RAP(s), s->dregs[CS_RAP(s)], val);
bellardb8174932006-09-10 19:25:12 +000093 switch(CS_RAP(s)) {
94 case 11:
95 case 25: // Read only
96 break;
97 case 12:
98 val &= 0x40;
99 val |= CS_CDC_VER; // Codec version
100 s->dregs[CS_RAP(s)] = val;
101 break;
102 default:
103 s->dregs[CS_RAP(s)] = val;
104 break;
105 }
106 break;
107 case 2: // Read only
108 break;
109 case 4:
Blue Swirl82d4c6e2009-10-24 16:20:32 +0000110 if (val & 1) {
111 cs_reset(&s->busdev.qdev);
112 }
bellardb8174932006-09-10 19:25:12 +0000113 val &= 0x7f;
114 s->regs[saddr] = val;
115 break;
116 default:
117 s->regs[saddr] = val;
blueswir1f930d072007-10-06 11:28:21 +0000118 break;
bellardb8174932006-09-10 19:25:12 +0000119 }
120}
121
Blue Swirld60efc62009-08-25 18:29:31 +0000122static CPUReadMemoryFunc * const cs_mem_read[3] = {
bellardb8174932006-09-10 19:25:12 +0000123 cs_mem_readl,
124 cs_mem_readl,
125 cs_mem_readl,
126};
127
Blue Swirld60efc62009-08-25 18:29:31 +0000128static CPUWriteMemoryFunc * const cs_mem_write[3] = {
bellardb8174932006-09-10 19:25:12 +0000129 cs_mem_writel,
130 cs_mem_writel,
131 cs_mem_writel,
132};
133
Blue Swirl82d4c6e2009-10-24 16:20:32 +0000134static const VMStateDescription vmstate_cs4231 = {
135 .name ="cs4231",
136 .version_id = 1,
137 .minimum_version_id = 1,
138 .minimum_version_id_old = 1,
139 .fields = (VMStateField []) {
140 VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
141 VMSTATE_UINT8_ARRAY(dregs, CSState, CS_DREGS),
142 VMSTATE_END_OF_LIST()
143 }
144};
bellardb8174932006-09-10 19:25:12 +0000145
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200146static int cs4231_init1(SysBusDevice *dev)
bellardb8174932006-09-10 19:25:12 +0000147{
Blue Swirlfa28ec52009-07-16 13:47:45 +0000148 int io;
149 CSState *s = FROM_SYSBUS(CSState, dev);
bellardb8174932006-09-10 19:25:12 +0000150
Blue Swirlfa28ec52009-07-16 13:47:45 +0000151 io = cpu_register_io_memory(cs_mem_read, cs_mem_write, s);
152 sysbus_init_mmio(dev, CS_SIZE, io);
153 sysbus_init_irq(dev, &s->irq);
bellardb8174932006-09-10 19:25:12 +0000154
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200155 return 0;
bellardb8174932006-09-10 19:25:12 +0000156}
Blue Swirlfa28ec52009-07-16 13:47:45 +0000157
158static SysBusDeviceInfo cs4231_info = {
159 .init = cs4231_init1,
160 .qdev.name = "SUNW,CS4231",
161 .qdev.size = sizeof(CSState),
Blue Swirl82d4c6e2009-10-24 16:20:32 +0000162 .qdev.vmsd = &vmstate_cs4231,
163 .qdev.reset = cs_reset,
Gerd Hoffmannee6847d2009-07-15 13:43:31 +0200164 .qdev.props = (Property[]) {
Blue Swirlfa28ec52009-07-16 13:47:45 +0000165 {.name = NULL}
166 }
167};
168
169static void cs4231_register_devices(void)
170{
171 sysbus_register_withprop(&cs4231_info);
172}
173
174device_init(cs4231_register_devices)