2009-08-23 17:44:15 +04:00
|
|
|
/*
|
|
|
|
* Marvell 88w8618 audio emulation extracted from
|
|
|
|
* Marvell MV88w8618 / Freecom MusicPal emulation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Jan Kiszka
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the GNU GPL v2.
|
2012-01-13 20:44:23 +04:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-08-23 17:44:15 +04:00
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-01-18 20:33:52 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2017-09-19 15:30:53 +03:00
|
|
|
#include "hw/audio/wm8750.h"
|
2009-08-23 17:44:15 +04:00
|
|
|
#include "audio/audio.h"
|
2018-10-22 10:40:49 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2009-08-23 17:44:15 +04:00
|
|
|
|
|
|
|
#define MP_AUDIO_SIZE 0x00001000
|
|
|
|
|
|
|
|
/* Audio register offsets */
|
|
|
|
#define MP_AUDIO_PLAYBACK_MODE 0x00
|
|
|
|
#define MP_AUDIO_CLOCK_DIV 0x18
|
|
|
|
#define MP_AUDIO_IRQ_STATUS 0x20
|
|
|
|
#define MP_AUDIO_IRQ_ENABLE 0x24
|
|
|
|
#define MP_AUDIO_TX_START_LO 0x28
|
|
|
|
#define MP_AUDIO_TX_THRESHOLD 0x2C
|
|
|
|
#define MP_AUDIO_TX_STATUS 0x38
|
|
|
|
#define MP_AUDIO_TX_START_HI 0x40
|
|
|
|
|
|
|
|
/* Status register and IRQ enable bits */
|
|
|
|
#define MP_AUDIO_TX_HALF (1 << 6)
|
|
|
|
#define MP_AUDIO_TX_FULL (1 << 7)
|
|
|
|
|
|
|
|
/* Playback mode bits */
|
|
|
|
#define MP_AUDIO_16BIT_SAMPLE (1 << 0)
|
|
|
|
#define MP_AUDIO_PLAYBACK_EN (1 << 7)
|
|
|
|
#define MP_AUDIO_CLOCK_24MHZ (1 << 9)
|
|
|
|
#define MP_AUDIO_MONO (1 << 14)
|
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(mv88w8618_audio_state, MV88W8618_AUDIO)
|
2013-07-24 12:10:44 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct mv88w8618_audio_state {
|
2013-07-24 12:10:44 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-10-25 14:32:35 +04:00
|
|
|
MemoryRegion iomem;
|
2009-08-23 17:44:15 +04:00
|
|
|
qemu_irq irq;
|
|
|
|
uint32_t playback_mode;
|
|
|
|
uint32_t status;
|
|
|
|
uint32_t irq_enable;
|
2009-09-18 22:51:23 +04:00
|
|
|
uint32_t phys_buf;
|
2009-08-23 17:44:15 +04:00
|
|
|
uint32_t target_buffer;
|
2009-09-18 22:51:23 +04:00
|
|
|
uint32_t threshold;
|
|
|
|
uint32_t play_pos;
|
|
|
|
uint32_t last_free;
|
2009-08-23 17:44:15 +04:00
|
|
|
uint32_t clock_div;
|
2011-12-07 05:32:44 +04:00
|
|
|
void *wm;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2009-08-23 17:44:15 +04:00
|
|
|
|
|
|
|
static void mv88w8618_audio_callback(void *opaque, int free_out, int free_in)
|
|
|
|
{
|
|
|
|
mv88w8618_audio_state *s = opaque;
|
|
|
|
int16_t *codec_buffer;
|
|
|
|
int8_t buf[4096];
|
|
|
|
int8_t *mem_buffer;
|
|
|
|
int pos, block_size;
|
|
|
|
|
2009-09-18 22:51:23 +04:00
|
|
|
if (!(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) {
|
2009-08-23 17:44:15 +04:00
|
|
|
return;
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
|
|
|
if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) {
|
2009-08-23 17:44:15 +04:00
|
|
|
free_out <<= 1;
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
|
|
|
if (!(s->playback_mode & MP_AUDIO_MONO)) {
|
2009-08-23 17:44:15 +04:00
|
|
|
free_out <<= 1;
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
block_size = s->threshold / 2;
|
2009-09-18 22:51:23 +04:00
|
|
|
if (free_out - s->last_free < block_size) {
|
2009-08-23 17:44:15 +04:00
|
|
|
return;
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
|
|
|
if (block_size > 4096) {
|
2009-08-23 17:44:15 +04:00
|
|
|
return;
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2013-04-12 22:53:58 +04:00
|
|
|
cpu_physical_memory_read(s->target_buffer + s->play_pos, buf, block_size);
|
2009-08-23 17:44:15 +04:00
|
|
|
mem_buffer = buf;
|
|
|
|
if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) {
|
|
|
|
if (s->playback_mode & MP_AUDIO_MONO) {
|
|
|
|
codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
|
|
|
|
for (pos = 0; pos < block_size; pos += 2) {
|
|
|
|
*codec_buffer++ = *(int16_t *)mem_buffer;
|
|
|
|
*codec_buffer++ = *(int16_t *)mem_buffer;
|
|
|
|
mem_buffer += 2;
|
|
|
|
}
|
2009-09-18 22:51:23 +04:00
|
|
|
} else {
|
2009-08-23 17:44:15 +04:00
|
|
|
memcpy(wm8750_dac_buffer(s->wm, block_size >> 2),
|
|
|
|
(uint32_t *)mem_buffer, block_size);
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
} else {
|
|
|
|
if (s->playback_mode & MP_AUDIO_MONO) {
|
|
|
|
codec_buffer = wm8750_dac_buffer(s->wm, block_size);
|
|
|
|
for (pos = 0; pos < block_size; pos++) {
|
|
|
|
*codec_buffer++ = cpu_to_le16(256 * *mem_buffer);
|
|
|
|
*codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1);
|
|
|
|
for (pos = 0; pos < block_size; pos += 2) {
|
|
|
|
*codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
|
|
|
|
*codec_buffer++ = cpu_to_le16(256 * *mem_buffer++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wm8750_dac_commit(s->wm);
|
|
|
|
|
|
|
|
s->last_free = free_out - block_size;
|
|
|
|
|
|
|
|
if (s->play_pos == 0) {
|
|
|
|
s->status |= MP_AUDIO_TX_HALF;
|
|
|
|
s->play_pos = block_size;
|
|
|
|
} else {
|
|
|
|
s->status |= MP_AUDIO_TX_FULL;
|
|
|
|
s->play_pos = 0;
|
|
|
|
}
|
|
|
|
|
2009-09-18 22:51:23 +04:00
|
|
|
if (s->status & s->irq_enable) {
|
2009-08-23 17:44:15 +04:00
|
|
|
qemu_irq_raise(s->irq);
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mv88w8618_audio_clock_update(mv88w8618_audio_state *s)
|
|
|
|
{
|
|
|
|
int rate;
|
|
|
|
|
2009-09-18 22:51:23 +04:00
|
|
|
if (s->playback_mode & MP_AUDIO_CLOCK_24MHZ) {
|
2009-08-23 17:44:15 +04:00
|
|
|
rate = 24576000 / 64; /* 24.576MHz */
|
2009-09-18 22:51:23 +04:00
|
|
|
} else {
|
2009-08-23 17:44:15 +04:00
|
|
|
rate = 11289600 / 64; /* 11.2896MHz */
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
rate /= ((s->clock_div >> 8) & 0xff) + 1;
|
|
|
|
|
|
|
|
wm8750_set_bclk_in(s->wm, rate);
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t mv88w8618_audio_read(void *opaque, hwaddr offset,
|
2011-10-25 14:32:35 +04:00
|
|
|
unsigned size)
|
2009-08-23 17:44:15 +04:00
|
|
|
{
|
|
|
|
mv88w8618_audio_state *s = opaque;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case MP_AUDIO_PLAYBACK_MODE:
|
|
|
|
return s->playback_mode;
|
|
|
|
|
|
|
|
case MP_AUDIO_CLOCK_DIV:
|
|
|
|
return s->clock_div;
|
|
|
|
|
|
|
|
case MP_AUDIO_IRQ_STATUS:
|
|
|
|
return s->status;
|
|
|
|
|
|
|
|
case MP_AUDIO_IRQ_ENABLE:
|
|
|
|
return s->irq_enable;
|
|
|
|
|
|
|
|
case MP_AUDIO_TX_STATUS:
|
|
|
|
return s->play_pos >> 2;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void mv88w8618_audio_write(void *opaque, hwaddr offset,
|
2011-10-25 14:32:35 +04:00
|
|
|
uint64_t value, unsigned size)
|
2009-08-23 17:44:15 +04:00
|
|
|
{
|
|
|
|
mv88w8618_audio_state *s = opaque;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case MP_AUDIO_PLAYBACK_MODE:
|
|
|
|
if (value & MP_AUDIO_PLAYBACK_EN &&
|
|
|
|
!(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) {
|
|
|
|
s->status = 0;
|
|
|
|
s->last_free = 0;
|
|
|
|
s->play_pos = 0;
|
|
|
|
}
|
|
|
|
s->playback_mode = value;
|
|
|
|
mv88w8618_audio_clock_update(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_CLOCK_DIV:
|
|
|
|
s->clock_div = value;
|
|
|
|
s->last_free = 0;
|
|
|
|
s->play_pos = 0;
|
|
|
|
mv88w8618_audio_clock_update(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_IRQ_STATUS:
|
|
|
|
s->status &= ~value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_IRQ_ENABLE:
|
|
|
|
s->irq_enable = value;
|
2009-09-18 22:51:23 +04:00
|
|
|
if (s->status & s->irq_enable) {
|
2009-08-23 17:44:15 +04:00
|
|
|
qemu_irq_raise(s->irq);
|
2009-09-18 22:51:23 +04:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_TX_START_LO:
|
|
|
|
s->phys_buf = (s->phys_buf & 0xFFFF0000) | (value & 0xFFFF);
|
|
|
|
s->target_buffer = s->phys_buf;
|
|
|
|
s->play_pos = 0;
|
|
|
|
s->last_free = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_TX_THRESHOLD:
|
|
|
|
s->threshold = (value + 1) * 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_AUDIO_TX_START_HI:
|
|
|
|
s->phys_buf = (s->phys_buf & 0xFFFF) | (value << 16);
|
|
|
|
s->target_buffer = s->phys_buf;
|
|
|
|
s->play_pos = 0;
|
|
|
|
s->last_free = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 22:51:23 +04:00
|
|
|
static void mv88w8618_audio_reset(DeviceState *d)
|
2009-08-23 17:44:15 +04:00
|
|
|
{
|
2013-07-24 12:10:44 +04:00
|
|
|
mv88w8618_audio_state *s = MV88W8618_AUDIO(d);
|
2009-08-23 17:44:15 +04:00
|
|
|
|
|
|
|
s->playback_mode = 0;
|
|
|
|
s->status = 0;
|
|
|
|
s->irq_enable = 0;
|
2009-09-18 22:51:23 +04:00
|
|
|
s->clock_div = 0;
|
|
|
|
s->threshold = 0;
|
|
|
|
s->phys_buf = 0;
|
2009-08-23 17:44:15 +04:00
|
|
|
}
|
|
|
|
|
2011-10-25 14:32:35 +04:00
|
|
|
static const MemoryRegionOps mv88w8618_audio_ops = {
|
|
|
|
.read = mv88w8618_audio_read,
|
|
|
|
.write = mv88w8618_audio_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2009-08-23 17:44:15 +04:00
|
|
|
};
|
|
|
|
|
2016-12-31 04:17:19 +03:00
|
|
|
static void mv88w8618_audio_init(Object *obj)
|
2009-08-23 17:44:15 +04:00
|
|
|
{
|
2016-12-31 04:17:19 +03:00
|
|
|
SysBusDevice *dev = SYS_BUS_DEVICE(obj);
|
2013-07-24 12:10:44 +04:00
|
|
|
mv88w8618_audio_state *s = MV88W8618_AUDIO(dev);
|
2009-08-23 17:44:15 +04:00
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->irq);
|
|
|
|
|
2016-12-31 04:17:19 +03:00
|
|
|
memory_region_init_io(&s->iomem, obj, &mv88w8618_audio_ops, s,
|
2011-10-25 14:32:35 +04:00
|
|
|
"audio", MP_AUDIO_SIZE);
|
2011-11-27 13:38:10 +04:00
|
|
|
sysbus_init_mmio(dev, &s->iomem);
|
2018-10-22 10:40:49 +03:00
|
|
|
|
|
|
|
object_property_add_link(OBJECT(dev), "wm8750", TYPE_WM8750,
|
|
|
|
(Object **) &s->wm,
|
|
|
|
qdev_prop_allow_set_link_before_realize,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 18:29:22 +03:00
|
|
|
0);
|
2016-12-31 04:17:19 +03:00
|
|
|
}
|
2009-08-23 17:44:15 +04:00
|
|
|
|
2016-12-31 04:17:19 +03:00
|
|
|
static void mv88w8618_audio_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
mv88w8618_audio_state *s = MV88W8618_AUDIO(dev);
|
|
|
|
|
|
|
|
wm8750_data_req_set(s->wm, mv88w8618_audio_callback, s);
|
2009-08-23 17:44:15 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 22:51:23 +04:00
|
|
|
static const VMStateDescription mv88w8618_audio_vmsd = {
|
|
|
|
.name = "mv88w8618_audio",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 06:16:04 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2009-09-18 22:51:23 +04:00
|
|
|
VMSTATE_UINT32(playback_mode, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(status, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(irq_enable, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(phys_buf, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(target_buffer, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(threshold, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(play_pos, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(last_free, mv88w8618_audio_state),
|
|
|
|
VMSTATE_UINT32(clock_div, mv88w8618_audio_state),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void mv88w8618_audio_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2016-12-31 04:17:19 +03:00
|
|
|
dc->realize = mv88w8618_audio_realize;
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, mv88w8618_audio_reset);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &mv88w8618_audio_vmsd;
|
2017-05-03 23:35:44 +03:00
|
|
|
dc->user_creatable = false;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo mv88w8618_audio_info = {
|
2013-07-24 12:10:44 +04:00
|
|
|
.name = TYPE_MV88W8618_AUDIO,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(mv88w8618_audio_state),
|
2016-12-31 04:17:19 +03:00
|
|
|
.instance_init = mv88w8618_audio_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = mv88w8618_audio_class_init,
|
2009-08-23 17:44:15 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void mv88w8618_register_types(void)
|
2009-08-23 17:44:15 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&mv88w8618_audio_info);
|
2009-08-23 17:44:15 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(mv88w8618_register_types)
|