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.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
12 #include "qemu/osdep.h"
13 #include "hw/sysbus.h"
15 #include "hw/i2c/i2c.h"
16 #include "audio/audio.h"
18 #define MP_AUDIO_SIZE 0x00001000
20 /* Audio register offsets */
21 #define MP_AUDIO_PLAYBACK_MODE 0x00
22 #define MP_AUDIO_CLOCK_DIV 0x18
23 #define MP_AUDIO_IRQ_STATUS 0x20
24 #define MP_AUDIO_IRQ_ENABLE 0x24
25 #define MP_AUDIO_TX_START_LO 0x28
26 #define MP_AUDIO_TX_THRESHOLD 0x2C
27 #define MP_AUDIO_TX_STATUS 0x38
28 #define MP_AUDIO_TX_START_HI 0x40
30 /* Status register and IRQ enable bits */
31 #define MP_AUDIO_TX_HALF (1 << 6)
32 #define MP_AUDIO_TX_FULL (1 << 7)
34 /* Playback mode bits */
35 #define MP_AUDIO_16BIT_SAMPLE (1 << 0)
36 #define MP_AUDIO_PLAYBACK_EN (1 << 7)
37 #define MP_AUDIO_CLOCK_24MHZ (1 << 9)
38 #define MP_AUDIO_MONO (1 << 14)
40 #define TYPE_MV88W8618_AUDIO "mv88w8618_audio"
41 #define MV88W8618_AUDIO(obj) \
42 OBJECT_CHECK(mv88w8618_audio_state, (obj), TYPE_MV88W8618_AUDIO)
44 typedef struct mv88w8618_audio_state
{
45 SysBusDevice parent_obj
;
49 uint32_t playback_mode
;
53 uint32_t target_buffer
;
59 } mv88w8618_audio_state
;
61 static void mv88w8618_audio_callback(void *opaque
, int free_out
, int free_in
)
63 mv88w8618_audio_state
*s
= opaque
;
64 int16_t *codec_buffer
;
69 if (!(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
)) {
72 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
) {
75 if (!(s
->playback_mode
& MP_AUDIO_MONO
)) {
78 block_size
= s
->threshold
/ 2;
79 if (free_out
- s
->last_free
< block_size
) {
82 if (block_size
> 4096) {
85 cpu_physical_memory_read(s
->target_buffer
+ s
->play_pos
, buf
, block_size
);
87 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
) {
88 if (s
->playback_mode
& MP_AUDIO_MONO
) {
89 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
90 for (pos
= 0; pos
< block_size
; pos
+= 2) {
91 *codec_buffer
++ = *(int16_t *)mem_buffer
;
92 *codec_buffer
++ = *(int16_t *)mem_buffer
;
96 memcpy(wm8750_dac_buffer(s
->wm
, block_size
>> 2),
97 (uint32_t *)mem_buffer
, block_size
);
100 if (s
->playback_mode
& MP_AUDIO_MONO
) {
101 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
);
102 for (pos
= 0; pos
< block_size
; pos
++) {
103 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
);
104 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
107 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
108 for (pos
= 0; pos
< block_size
; pos
+= 2) {
109 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
110 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
114 wm8750_dac_commit(s
->wm
);
116 s
->last_free
= free_out
- block_size
;
118 if (s
->play_pos
== 0) {
119 s
->status
|= MP_AUDIO_TX_HALF
;
120 s
->play_pos
= block_size
;
122 s
->status
|= MP_AUDIO_TX_FULL
;
126 if (s
->status
& s
->irq_enable
) {
127 qemu_irq_raise(s
->irq
);
131 static void mv88w8618_audio_clock_update(mv88w8618_audio_state
*s
)
135 if (s
->playback_mode
& MP_AUDIO_CLOCK_24MHZ
) {
136 rate
= 24576000 / 64; /* 24.576MHz */
138 rate
= 11289600 / 64; /* 11.2896MHz */
140 rate
/= ((s
->clock_div
>> 8) & 0xff) + 1;
142 wm8750_set_bclk_in(s
->wm
, rate
);
145 static uint64_t mv88w8618_audio_read(void *opaque
, hwaddr offset
,
148 mv88w8618_audio_state
*s
= opaque
;
151 case MP_AUDIO_PLAYBACK_MODE
:
152 return s
->playback_mode
;
154 case MP_AUDIO_CLOCK_DIV
:
157 case MP_AUDIO_IRQ_STATUS
:
160 case MP_AUDIO_IRQ_ENABLE
:
161 return s
->irq_enable
;
163 case MP_AUDIO_TX_STATUS
:
164 return s
->play_pos
>> 2;
171 static void mv88w8618_audio_write(void *opaque
, hwaddr offset
,
172 uint64_t value
, unsigned size
)
174 mv88w8618_audio_state
*s
= opaque
;
177 case MP_AUDIO_PLAYBACK_MODE
:
178 if (value
& MP_AUDIO_PLAYBACK_EN
&&
179 !(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
)) {
184 s
->playback_mode
= value
;
185 mv88w8618_audio_clock_update(s
);
188 case MP_AUDIO_CLOCK_DIV
:
189 s
->clock_div
= value
;
192 mv88w8618_audio_clock_update(s
);
195 case MP_AUDIO_IRQ_STATUS
:
199 case MP_AUDIO_IRQ_ENABLE
:
200 s
->irq_enable
= value
;
201 if (s
->status
& s
->irq_enable
) {
202 qemu_irq_raise(s
->irq
);
206 case MP_AUDIO_TX_START_LO
:
207 s
->phys_buf
= (s
->phys_buf
& 0xFFFF0000) | (value
& 0xFFFF);
208 s
->target_buffer
= s
->phys_buf
;
213 case MP_AUDIO_TX_THRESHOLD
:
214 s
->threshold
= (value
+ 1) * 4;
217 case MP_AUDIO_TX_START_HI
:
218 s
->phys_buf
= (s
->phys_buf
& 0xFFFF) | (value
<< 16);
219 s
->target_buffer
= s
->phys_buf
;
226 static void mv88w8618_audio_reset(DeviceState
*d
)
228 mv88w8618_audio_state
*s
= MV88W8618_AUDIO(d
);
230 s
->playback_mode
= 0;
238 static const MemoryRegionOps mv88w8618_audio_ops
= {
239 .read
= mv88w8618_audio_read
,
240 .write
= mv88w8618_audio_write
,
241 .endianness
= DEVICE_NATIVE_ENDIAN
,
244 static void mv88w8618_audio_init(Object
*obj
)
246 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
247 mv88w8618_audio_state
*s
= MV88W8618_AUDIO(dev
);
249 sysbus_init_irq(dev
, &s
->irq
);
251 memory_region_init_io(&s
->iomem
, obj
, &mv88w8618_audio_ops
, s
,
252 "audio", MP_AUDIO_SIZE
);
253 sysbus_init_mmio(dev
, &s
->iomem
);
256 static void mv88w8618_audio_realize(DeviceState
*dev
, Error
**errp
)
258 mv88w8618_audio_state
*s
= MV88W8618_AUDIO(dev
);
260 wm8750_data_req_set(s
->wm
, mv88w8618_audio_callback
, s
);
263 static const VMStateDescription mv88w8618_audio_vmsd
= {
264 .name
= "mv88w8618_audio",
266 .minimum_version_id
= 1,
267 .fields
= (VMStateField
[]) {
268 VMSTATE_UINT32(playback_mode
, mv88w8618_audio_state
),
269 VMSTATE_UINT32(status
, mv88w8618_audio_state
),
270 VMSTATE_UINT32(irq_enable
, mv88w8618_audio_state
),
271 VMSTATE_UINT32(phys_buf
, mv88w8618_audio_state
),
272 VMSTATE_UINT32(target_buffer
, mv88w8618_audio_state
),
273 VMSTATE_UINT32(threshold
, mv88w8618_audio_state
),
274 VMSTATE_UINT32(play_pos
, mv88w8618_audio_state
),
275 VMSTATE_UINT32(last_free
, mv88w8618_audio_state
),
276 VMSTATE_UINT32(clock_div
, mv88w8618_audio_state
),
277 VMSTATE_END_OF_LIST()
281 static Property mv88w8618_audio_properties
[] = {
282 DEFINE_PROP_PTR("wm8750", mv88w8618_audio_state
, wm
),
286 static void mv88w8618_audio_class_init(ObjectClass
*klass
, void *data
)
288 DeviceClass
*dc
= DEVICE_CLASS(klass
);
290 dc
->realize
= mv88w8618_audio_realize
;
291 dc
->reset
= mv88w8618_audio_reset
;
292 dc
->vmsd
= &mv88w8618_audio_vmsd
;
293 dc
->props
= mv88w8618_audio_properties
;
294 /* Reason: pointer property "wm8750" */
295 dc
->user_creatable
= false;
298 static const TypeInfo mv88w8618_audio_info
= {
299 .name
= TYPE_MV88W8618_AUDIO
,
300 .parent
= TYPE_SYS_BUS_DEVICE
,
301 .instance_size
= sizeof(mv88w8618_audio_state
),
302 .instance_init
= mv88w8618_audio_init
,
303 .class_init
= mv88w8618_audio_class_init
,
306 static void mv88w8618_register_types(void)
308 type_register_static(&mv88w8618_audio_info
);
311 type_init(mv88w8618_register_types
)