net: store guest timestamp in dump file instead of time since guest startup
[qemu/ar7.git] / hw / marvell_88w8618_audio.c
blob855b792b9d6183554619aaa75d479b929368859a
1 /*
2 * Marvell 88w8618 audio emulation extracted from
3 * Marvell MV88w8618 / Freecom MusicPal emulation.
5 * Copyright (c) 2008 Jan Kiszka
7 * This code is licensed under the GNU GPL v2.
8 */
9 #include "sysbus.h"
10 #include "hw.h"
11 #include "i2c.h"
12 #include "sysbus.h"
13 #include "audio/audio.h"
15 #define MP_AUDIO_SIZE 0x00001000
17 /* Audio register offsets */
18 #define MP_AUDIO_PLAYBACK_MODE 0x00
19 #define MP_AUDIO_CLOCK_DIV 0x18
20 #define MP_AUDIO_IRQ_STATUS 0x20
21 #define MP_AUDIO_IRQ_ENABLE 0x24
22 #define MP_AUDIO_TX_START_LO 0x28
23 #define MP_AUDIO_TX_THRESHOLD 0x2C
24 #define MP_AUDIO_TX_STATUS 0x38
25 #define MP_AUDIO_TX_START_HI 0x40
27 /* Status register and IRQ enable bits */
28 #define MP_AUDIO_TX_HALF (1 << 6)
29 #define MP_AUDIO_TX_FULL (1 << 7)
31 /* Playback mode bits */
32 #define MP_AUDIO_16BIT_SAMPLE (1 << 0)
33 #define MP_AUDIO_PLAYBACK_EN (1 << 7)
34 #define MP_AUDIO_CLOCK_24MHZ (1 << 9)
35 #define MP_AUDIO_MONO (1 << 14)
37 typedef struct mv88w8618_audio_state {
38 SysBusDevice busdev;
39 MemoryRegion iomem;
40 qemu_irq irq;
41 uint32_t playback_mode;
42 uint32_t status;
43 uint32_t irq_enable;
44 uint32_t phys_buf;
45 uint32_t target_buffer;
46 uint32_t threshold;
47 uint32_t play_pos;
48 uint32_t last_free;
49 uint32_t clock_div;
50 DeviceState *wm;
51 } mv88w8618_audio_state;
53 static void mv88w8618_audio_callback(void *opaque, int free_out, int free_in)
55 mv88w8618_audio_state *s = opaque;
56 int16_t *codec_buffer;
57 int8_t buf[4096];
58 int8_t *mem_buffer;
59 int pos, block_size;
61 if (!(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) {
62 return;
64 if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) {
65 free_out <<= 1;
67 if (!(s->playback_mode & MP_AUDIO_MONO)) {
68 free_out <<= 1;
70 block_size = s->threshold / 2;
71 if (free_out - s->last_free < block_size) {
72 return;
74 if (block_size > 4096) {
75 return;
77 cpu_physical_memory_read(s->target_buffer + s->play_pos, (void *)buf,
78 block_size);
79 mem_buffer = buf;
80 if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) {
81 if (s->playback_mode & MP_AUDIO_MONO) {
82 codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
83 for (pos = 0; pos < block_size; pos += 2) {
84 *codec_buffer++ = *(int16_t *)mem_buffer;
85 *codec_buffer++ = *(int16_t *)mem_buffer;
86 mem_buffer += 2;
88 } else {
89 memcpy(wm8750_dac_buffer(s->wm, block_size >> 2),
90 (uint32_t *)mem_buffer, block_size);
92 } else {
93 if (s->playback_mode & MP_AUDIO_MONO) {
94 codec_buffer = wm8750_dac_buffer(s->wm, block_size);
95 for (pos = 0; pos < block_size; pos++) {
96 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer);
97 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
99 } else {
100 codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
101 for (pos = 0; pos < block_size; pos += 2) {
102 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
103 *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
107 wm8750_dac_commit(s->wm);
109 s->last_free = free_out - block_size;
111 if (s->play_pos == 0) {
112 s->status |= MP_AUDIO_TX_HALF;
113 s->play_pos = block_size;
114 } else {
115 s->status |= MP_AUDIO_TX_FULL;
116 s->play_pos = 0;
119 if (s->status & s->irq_enable) {
120 qemu_irq_raise(s->irq);
124 static void mv88w8618_audio_clock_update(mv88w8618_audio_state *s)
126 int rate;
128 if (s->playback_mode & MP_AUDIO_CLOCK_24MHZ) {
129 rate = 24576000 / 64; /* 24.576MHz */
130 } else {
131 rate = 11289600 / 64; /* 11.2896MHz */
133 rate /= ((s->clock_div >> 8) & 0xff) + 1;
135 wm8750_set_bclk_in(s->wm, rate);
138 static uint64_t mv88w8618_audio_read(void *opaque, target_phys_addr_t offset,
139 unsigned size)
141 mv88w8618_audio_state *s = opaque;
143 switch (offset) {
144 case MP_AUDIO_PLAYBACK_MODE:
145 return s->playback_mode;
147 case MP_AUDIO_CLOCK_DIV:
148 return s->clock_div;
150 case MP_AUDIO_IRQ_STATUS:
151 return s->status;
153 case MP_AUDIO_IRQ_ENABLE:
154 return s->irq_enable;
156 case MP_AUDIO_TX_STATUS:
157 return s->play_pos >> 2;
159 default:
160 return 0;
164 static void mv88w8618_audio_write(void *opaque, target_phys_addr_t offset,
165 uint64_t value, unsigned size)
167 mv88w8618_audio_state *s = opaque;
169 switch (offset) {
170 case MP_AUDIO_PLAYBACK_MODE:
171 if (value & MP_AUDIO_PLAYBACK_EN &&
172 !(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) {
173 s->status = 0;
174 s->last_free = 0;
175 s->play_pos = 0;
177 s->playback_mode = value;
178 mv88w8618_audio_clock_update(s);
179 break;
181 case MP_AUDIO_CLOCK_DIV:
182 s->clock_div = value;
183 s->last_free = 0;
184 s->play_pos = 0;
185 mv88w8618_audio_clock_update(s);
186 break;
188 case MP_AUDIO_IRQ_STATUS:
189 s->status &= ~value;
190 break;
192 case MP_AUDIO_IRQ_ENABLE:
193 s->irq_enable = value;
194 if (s->status & s->irq_enable) {
195 qemu_irq_raise(s->irq);
197 break;
199 case MP_AUDIO_TX_START_LO:
200 s->phys_buf = (s->phys_buf & 0xFFFF0000) | (value & 0xFFFF);
201 s->target_buffer = s->phys_buf;
202 s->play_pos = 0;
203 s->last_free = 0;
204 break;
206 case MP_AUDIO_TX_THRESHOLD:
207 s->threshold = (value + 1) * 4;
208 break;
210 case MP_AUDIO_TX_START_HI:
211 s->phys_buf = (s->phys_buf & 0xFFFF) | (value << 16);
212 s->target_buffer = s->phys_buf;
213 s->play_pos = 0;
214 s->last_free = 0;
215 break;
219 static void mv88w8618_audio_reset(DeviceState *d)
221 mv88w8618_audio_state *s = FROM_SYSBUS(mv88w8618_audio_state,
222 sysbus_from_qdev(d));
224 s->playback_mode = 0;
225 s->status = 0;
226 s->irq_enable = 0;
227 s->clock_div = 0;
228 s->threshold = 0;
229 s->phys_buf = 0;
232 static const MemoryRegionOps mv88w8618_audio_ops = {
233 .read = mv88w8618_audio_read,
234 .write = mv88w8618_audio_write,
235 .endianness = DEVICE_NATIVE_ENDIAN,
238 static int mv88w8618_audio_init(SysBusDevice *dev)
240 mv88w8618_audio_state *s = FROM_SYSBUS(mv88w8618_audio_state, dev);
242 sysbus_init_irq(dev, &s->irq);
244 wm8750_data_req_set(s->wm, mv88w8618_audio_callback, s);
246 memory_region_init_io(&s->iomem, &mv88w8618_audio_ops, s,
247 "audio", MP_AUDIO_SIZE);
248 sysbus_init_mmio(dev, &s->iomem);
250 return 0;
253 static const VMStateDescription mv88w8618_audio_vmsd = {
254 .name = "mv88w8618_audio",
255 .version_id = 1,
256 .minimum_version_id = 1,
257 .minimum_version_id_old = 1,
258 .fields = (VMStateField[]) {
259 VMSTATE_UINT32(playback_mode, mv88w8618_audio_state),
260 VMSTATE_UINT32(status, mv88w8618_audio_state),
261 VMSTATE_UINT32(irq_enable, mv88w8618_audio_state),
262 VMSTATE_UINT32(phys_buf, mv88w8618_audio_state),
263 VMSTATE_UINT32(target_buffer, mv88w8618_audio_state),
264 VMSTATE_UINT32(threshold, mv88w8618_audio_state),
265 VMSTATE_UINT32(play_pos, mv88w8618_audio_state),
266 VMSTATE_UINT32(last_free, mv88w8618_audio_state),
267 VMSTATE_UINT32(clock_div, mv88w8618_audio_state),
268 VMSTATE_END_OF_LIST()
272 static SysBusDeviceInfo mv88w8618_audio_info = {
273 .init = mv88w8618_audio_init,
274 .qdev.name = "mv88w8618_audio",
275 .qdev.size = sizeof(mv88w8618_audio_state),
276 .qdev.reset = mv88w8618_audio_reset,
277 .qdev.vmsd = &mv88w8618_audio_vmsd,
278 .qdev.props = (Property[]) {
280 .name = "wm8750",
281 .info = &qdev_prop_ptr,
282 .offset = offsetof(mv88w8618_audio_state, wm),
284 {/* end of list */}
288 static void mv88w8618_register_devices(void)
290 sysbus_register_withprop(&mv88w8618_audio_info);
293 device_init(mv88w8618_register_devices)