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 "hw/sysbus.h"
14 #include "hw/i2c/i2c.h"
15 #include "audio/audio.h"
17 #define MP_AUDIO_SIZE 0x00001000
19 /* Audio register offsets */
20 #define MP_AUDIO_PLAYBACK_MODE 0x00
21 #define MP_AUDIO_CLOCK_DIV 0x18
22 #define MP_AUDIO_IRQ_STATUS 0x20
23 #define MP_AUDIO_IRQ_ENABLE 0x24
24 #define MP_AUDIO_TX_START_LO 0x28
25 #define MP_AUDIO_TX_THRESHOLD 0x2C
26 #define MP_AUDIO_TX_STATUS 0x38
27 #define MP_AUDIO_TX_START_HI 0x40
29 /* Status register and IRQ enable bits */
30 #define MP_AUDIO_TX_HALF (1 << 6)
31 #define MP_AUDIO_TX_FULL (1 << 7)
33 /* Playback mode bits */
34 #define MP_AUDIO_16BIT_SAMPLE (1 << 0)
35 #define MP_AUDIO_PLAYBACK_EN (1 << 7)
36 #define MP_AUDIO_CLOCK_24MHZ (1 << 9)
37 #define MP_AUDIO_MONO (1 << 14)
39 #define TYPE_MV88W8618_AUDIO "mv88w8618_audio"
40 #define MV88W8618_AUDIO(obj) \
41 OBJECT_CHECK(mv88w8618_audio_state, (obj), TYPE_MV88W8618_AUDIO)
43 typedef struct mv88w8618_audio_state
{
44 SysBusDevice parent_obj
;
48 uint32_t playback_mode
;
52 uint32_t target_buffer
;
58 } mv88w8618_audio_state
;
60 static void mv88w8618_audio_callback(void *opaque
, int free_out
, int free_in
)
62 mv88w8618_audio_state
*s
= opaque
;
63 int16_t *codec_buffer
;
68 if (!(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
)) {
71 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
) {
74 if (!(s
->playback_mode
& MP_AUDIO_MONO
)) {
77 block_size
= s
->threshold
/ 2;
78 if (free_out
- s
->last_free
< block_size
) {
81 if (block_size
> 4096) {
84 cpu_physical_memory_read(s
->target_buffer
+ s
->play_pos
, buf
, block_size
);
86 if (s
->playback_mode
& MP_AUDIO_16BIT_SAMPLE
) {
87 if (s
->playback_mode
& MP_AUDIO_MONO
) {
88 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
89 for (pos
= 0; pos
< block_size
; pos
+= 2) {
90 *codec_buffer
++ = *(int16_t *)mem_buffer
;
91 *codec_buffer
++ = *(int16_t *)mem_buffer
;
95 memcpy(wm8750_dac_buffer(s
->wm
, block_size
>> 2),
96 (uint32_t *)mem_buffer
, block_size
);
99 if (s
->playback_mode
& MP_AUDIO_MONO
) {
100 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
);
101 for (pos
= 0; pos
< block_size
; pos
++) {
102 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
);
103 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
106 codec_buffer
= wm8750_dac_buffer(s
->wm
, block_size
>> 1);
107 for (pos
= 0; pos
< block_size
; pos
+= 2) {
108 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
109 *codec_buffer
++ = cpu_to_le16(256 * *mem_buffer
++);
113 wm8750_dac_commit(s
->wm
);
115 s
->last_free
= free_out
- block_size
;
117 if (s
->play_pos
== 0) {
118 s
->status
|= MP_AUDIO_TX_HALF
;
119 s
->play_pos
= block_size
;
121 s
->status
|= MP_AUDIO_TX_FULL
;
125 if (s
->status
& s
->irq_enable
) {
126 qemu_irq_raise(s
->irq
);
130 static void mv88w8618_audio_clock_update(mv88w8618_audio_state
*s
)
134 if (s
->playback_mode
& MP_AUDIO_CLOCK_24MHZ
) {
135 rate
= 24576000 / 64; /* 24.576MHz */
137 rate
= 11289600 / 64; /* 11.2896MHz */
139 rate
/= ((s
->clock_div
>> 8) & 0xff) + 1;
141 wm8750_set_bclk_in(s
->wm
, rate
);
144 static uint64_t mv88w8618_audio_read(void *opaque
, hwaddr offset
,
147 mv88w8618_audio_state
*s
= opaque
;
150 case MP_AUDIO_PLAYBACK_MODE
:
151 return s
->playback_mode
;
153 case MP_AUDIO_CLOCK_DIV
:
156 case MP_AUDIO_IRQ_STATUS
:
159 case MP_AUDIO_IRQ_ENABLE
:
160 return s
->irq_enable
;
162 case MP_AUDIO_TX_STATUS
:
163 return s
->play_pos
>> 2;
170 static void mv88w8618_audio_write(void *opaque
, hwaddr offset
,
171 uint64_t value
, unsigned size
)
173 mv88w8618_audio_state
*s
= opaque
;
176 case MP_AUDIO_PLAYBACK_MODE
:
177 if (value
& MP_AUDIO_PLAYBACK_EN
&&
178 !(s
->playback_mode
& MP_AUDIO_PLAYBACK_EN
)) {
183 s
->playback_mode
= value
;
184 mv88w8618_audio_clock_update(s
);
187 case MP_AUDIO_CLOCK_DIV
:
188 s
->clock_div
= value
;
191 mv88w8618_audio_clock_update(s
);
194 case MP_AUDIO_IRQ_STATUS
:
198 case MP_AUDIO_IRQ_ENABLE
:
199 s
->irq_enable
= value
;
200 if (s
->status
& s
->irq_enable
) {
201 qemu_irq_raise(s
->irq
);
205 case MP_AUDIO_TX_START_LO
:
206 s
->phys_buf
= (s
->phys_buf
& 0xFFFF0000) | (value
& 0xFFFF);
207 s
->target_buffer
= s
->phys_buf
;
212 case MP_AUDIO_TX_THRESHOLD
:
213 s
->threshold
= (value
+ 1) * 4;
216 case MP_AUDIO_TX_START_HI
:
217 s
->phys_buf
= (s
->phys_buf
& 0xFFFF) | (value
<< 16);
218 s
->target_buffer
= s
->phys_buf
;
225 static void mv88w8618_audio_reset(DeviceState
*d
)
227 mv88w8618_audio_state
*s
= MV88W8618_AUDIO(d
);
229 s
->playback_mode
= 0;
237 static const MemoryRegionOps mv88w8618_audio_ops
= {
238 .read
= mv88w8618_audio_read
,
239 .write
= mv88w8618_audio_write
,
240 .endianness
= DEVICE_NATIVE_ENDIAN
,
243 static int mv88w8618_audio_init(SysBusDevice
*dev
)
245 mv88w8618_audio_state
*s
= MV88W8618_AUDIO(dev
);
247 sysbus_init_irq(dev
, &s
->irq
);
249 wm8750_data_req_set(s
->wm
, mv88w8618_audio_callback
, s
);
251 memory_region_init_io(&s
->iomem
, OBJECT(s
), &mv88w8618_audio_ops
, s
,
252 "audio", MP_AUDIO_SIZE
);
253 sysbus_init_mmio(dev
, &s
->iomem
);
258 static const VMStateDescription mv88w8618_audio_vmsd
= {
259 .name
= "mv88w8618_audio",
261 .minimum_version_id
= 1,
262 .fields
= (VMStateField
[]) {
263 VMSTATE_UINT32(playback_mode
, mv88w8618_audio_state
),
264 VMSTATE_UINT32(status
, mv88w8618_audio_state
),
265 VMSTATE_UINT32(irq_enable
, mv88w8618_audio_state
),
266 VMSTATE_UINT32(phys_buf
, mv88w8618_audio_state
),
267 VMSTATE_UINT32(target_buffer
, mv88w8618_audio_state
),
268 VMSTATE_UINT32(threshold
, mv88w8618_audio_state
),
269 VMSTATE_UINT32(play_pos
, mv88w8618_audio_state
),
270 VMSTATE_UINT32(last_free
, mv88w8618_audio_state
),
271 VMSTATE_UINT32(clock_div
, mv88w8618_audio_state
),
272 VMSTATE_END_OF_LIST()
276 static Property mv88w8618_audio_properties
[] = {
277 DEFINE_PROP_PTR("wm8750", mv88w8618_audio_state
, wm
),
281 static void mv88w8618_audio_class_init(ObjectClass
*klass
, void *data
)
283 DeviceClass
*dc
= DEVICE_CLASS(klass
);
284 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
286 k
->init
= mv88w8618_audio_init
;
287 dc
->reset
= mv88w8618_audio_reset
;
288 dc
->vmsd
= &mv88w8618_audio_vmsd
;
289 dc
->props
= mv88w8618_audio_properties
;
290 /* Reason: pointer property "wm8750" */
291 dc
->cannot_instantiate_with_device_add_yet
= true;
294 static const TypeInfo mv88w8618_audio_info
= {
295 .name
= TYPE_MV88W8618_AUDIO
,
296 .parent
= TYPE_SYS_BUS_DEVICE
,
297 .instance_size
= sizeof(mv88w8618_audio_state
),
298 .class_init
= mv88w8618_audio_class_init
,
301 static void mv88w8618_register_types(void)
303 type_register_static(&mv88w8618_audio_info
);
306 type_init(mv88w8618_register_types
)