Add AACI audio playback support to the ARM Versatile/PB platform
PATCH v2 1/1 Add AACI audio playback support to the ARM Versatile/PB platform
The ACLink bus has been removed.
The PL041/AACI driver now directly control the LM4549 codec driver.
The AC97 emulation in ac97.c has not been reused. Making the current implementation sharable is not trivial and could bring regression to other platforms.
Versatile/PB test build:
linux-2.6.38.5
buildroot-2010.11
alsa-lib-1.0.22
alsa-utils-1.0.22
mpg123-0.66
Qemu host: Ubuntu 10.04 in Vmware/OS X
Playback tested successfully with aplay and mpg123.
--- Makefile.target | 1 + hw/lm4549.c | 307 +++++++++++++++++++++++++++++++++++++++++ hw/lm4549.h | 45 ++++++ hw/pl041.c | 406 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/pl041.h | 125 +++++++++++++++++ hw/pl041.hx | 61 ++++++++ hw/versatilepb.c | 3 + 7 files changed, 948 insertions(+), 0 deletions(-) create mode 100644 hw/lm4549.c create mode 100644 hw/lm4549.h create mode 100644 hw/pl041.c create mode 100644 hw/pl041.h create mode 100644 hw/pl041.hx
Makefile.target +1 -0
@@ -354,6 +354,7 @@ obj-arm-y += syborg_virtio.o obj-arm-y += vexpress.o obj-arm-y += strongarm.o obj-arm-y += collie.o +obj-arm-y += pl041.o lm4549.o obj-sh4-y = shix.o r2d.o sh7750.o sh7750_regnames.o tc58128.o obj-sh4-y += sh_timer.o sh_serial.o sh_intc.o sh_pci.o sm501.o
hw/lm4549.c new file +307 -0
307 lines not on this page · raw message
hw/lm4549.h new file +45 -0
45 lines not on this page · raw message
hw/pl041.c new file +406 -0
406 lines not on this page · raw message
hw/pl041.h new file +125 -0
125 lines not on this page · raw message
hw/pl041.hx new file +61 -0
61 lines not on this page · raw message
hw/versatilepb.c +3 -0
@@ -258,6 +258,9 @@ static void versatile_init(ram_addr_t ram_size,/* Add PL031 Real Time Clock. */ sysbus_create_simple ("pl031" ,0x101e8000 , pic[10 ]); +/* Add PL041 AACI Interface to the LM4549 codec */ + dev =sysbus_create_varargs ("pl041" ,0x10004000 , sic[24 ]); +/* Memory map for Versatile/PB: */ /* 0x10000000 System registers. */ /* 0x10001000 PCI controller config registers. */
--
1.7.0.4
Quoted 14 lines · Mathieu Sonet
The ACLink bus has been removed.
The PL041/AACI driver now directly control the LM4549 codec driver.The AC97 emulation in
ac97.chas not been reused. Making the current implementation sharable is not trivial and could bring regression to other platforms.Versatile/PB test build:
linux-2.6.38.5
buildroot-2010.11
alsa-lib-1.0.22
alsa-utils-1.0.22
mpg123-0.66
Quoted 4 lines
Qemu host: Ubuntu 10.04 in Vmware/OS X
Playback tested successfully with aplay and mpg123.
Once again, no objections.
[..snip..]
Signature · 1 line
mailto:[email protected]
On 13 May 2011 23:08, Mathieu Sonet <[email protected]> wrote:
[PL041/AACI patches]
I apologise for the exceedingly delayed review here.
So to start with, I tested this patch with the vexpress-a9 board (by adding the one line to add a pl041 to it) and it works, which is really cool.
A minor note on patch formatting: since the bit of the email up to the '---' goes into the git commit history, it should be a description of this version of the patch rather than a list of the differences to the previous version. (I usually put the "v1->v2" summary after the '---' before the diffstat.)
Moving on to the code review...
Quoted 1 line
+/*** Functions ***/
I'm not a fan of comments which state the obvious.
hw/lm4549.c line 65
Quoted 3 lines
hw/lm4549.c new file +3
-0+ +static void lm4549_store_init (lm4549_state *s,uint32_t offset,uint32_t value, + uint32_t is_read_only)
Making value a uint16_t would be consistent with lm4549_store and _load.
hw/lm4549.c line 71
Quoted 6 lines
hw/lm4549.c new file +6
-0+{ + lm4549_registers *r = &s->codec_regs; + + if (offset > 128) { + DPRINTF("store_init: Out of bound offset 0x%x\n", (int)offset); + }
You need a return here, otherwise we blunder on and access the array out of bounds. (Or you could just assert(), since we only use this function internally.)
Quoted 2 lines
+ r->data[offset].value = value &
0xFFFF;
+ r->data[offset].default_value = value &0xFFFF;
These "& 0xFFFF" aren't necessary.
Quoted 1 line
+static void
lm4549_store(lm4549_state *s, uint32_t offset, uint16_t value)
To be honest I'm not entirely convinced of the necessity of abstracting out the register file into these _load/_store/_reset functions rather than just having lm4549_read and _write access the array directly. (eg since there are only 3 read-only registers it would be simpler just to have lm4549_write's switch statement have:
case LM4549_Extended_Audio_ID:
case LM4549_Vendor_ID1:
case LM4549_Vendor_ID2:
/* Read-only registers */
break;rather than having all the machinery for the read_only field in the lm4549_registers struct. Similarly if you just handle the reset values in a reset function then your register array is just a simple uint16_t registers[128].)
hw/lm4549.c line 94
Quoted 6 lines
hw/lm4549.c new file +6
-0+{ + lm4549_registers *r = &s->codec_regs; + + if (offset > 128) { + DPRINTF("store: Out of bound offset 0x%x\n", (int)offset); + }
Needs a return again. (Also the case in lm4549_load.)
Quoted 1 line
+ r->data[offset].value = value &
0xFFFF;
Unnecessary mask.
Quoted 4 lines
+ if (
written_samples==s->buffer_level) {
+ s->buffer_level = 0;
+ } else {
+ s->buffer_level -=written_samples;
Quoted 8 lines
+
+ if (s->buffer_level> 0) {
+ /* Move the data back to the start of the buffer */
+ for (i = 0; i <s->buffer_level; i++) {
+ s->buffer[i] = s->buffer[i +written_samples];
+ }
+ }
+ }
You can just delete the if () { ... } part here, because if written_samples == s->buffer_level then the else {} clause is equivalent in effect to s->buffer_level = 0. [In fact "(s->buffer_level > 0)" can only be true if written_samples != s->buffer_level, because this is all unsigned arithmetic.]
Quoted 39 lines
hw/pl041.c new file +33
-0@@ -0,0 +1,406 @@ +/* +* Arm PrimeCell PL041 Advanced Audio Codec Interface +* +* Copyright (c) 2011 +* Written by Mathieu Sonet - www.elasticsheep.com +* +* This code is licenced under the GPL. +* +* ***************************************************************** +* +* This driver emulates the ARM AACI interface +* connected to a LM4549 codec. +* +*/ + +#include "sysbus.h" + +#include "pl041.h" +#include "lm4549.h" + +/*** Debug macros ***/ + +/* #define PL041_DEBUG_LEVEL 1 */ + +#if defined(PL041_DEBUG_LEVEL ) && (PL041_DEBUG_LEVEL >=1 ) +#define DBG_L1 (fmt, ...) \ +do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0) +#else +#define DBG_L1 (fmt, ...) \ +do { } while (0) +#endif + +#if defined(PL041_DEBUG_LEVEL ) && (PL041_DEBUG_LEVEL >=2 )
hw/pl041.c line 43
Quoted 10 lines
hw/pl041.c new file +10
-0+#define DBG_L2 (fmt, ...) \ +do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0) +#else +#define DBG_L2 (fmt, ...) \ +do { } while (0) +#endif + +/*** Constants ***/ + +#define FIFO_DEPTH 16
The FIFOs in the pl041 in the various ARM dev boards are actually larger than the stock pl041 FIFO. Possibly FIFO size should be a qdev property (does changing it have a (good or bad) effect on the model pl041's latency or tendency to dropouts?).
hw/pl041.c line 54
Quoted 10 lines
hw/pl041.c new file +10
-0+#define SLOT1_RW (1 << 19) + +/*** Types ***/ + +typedef struct { + uint32_t size; + uint32_t half; + uint32_t level; + uint32_t data[FIFO_DEPTH]; +}pl041_fifo ;
FIFO size is a property of the pl041, not per-FIFO, so it doesn't belong in this struct. Also, there's not much point in just caching size / 2 as 'half'.
hw/pl041.c line 62
Quoted 8 lines
hw/pl041.c new file +8
-0+ +typedef struct { + SysBusDevice busdev; + qemu_irq irq; + pl041_regfile regs; + pl041_fifo fifo1; + lm4549_state codec; +}pl041_state ;
...this is implicitly modelling the versatile/vexpress modified PL041 which only has one channel -- stock PL041 has four channels with a fifo each.
hw/pl041.c line 68
Quoted 5 lines
hw/pl041.c new file +5
-0+/*** Globals ***/ + +static const unsigned char pl041_id[8 ] = { + 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 +};
The modified PL041s in the versatilepb, vexpress etc have slightly different ID values (the FIFO depth and number of channels are encoded in one of the bytes).
hw/pl041.c line 81
Quoted 12 lines
hw/pl041.c new file +12
-0+#if defined(PL041_DEBUG_LEVEL ) +#define REGISTER (name, offset) #name, +static const char *pl041_regs_name[] = { + #include "pl041.hx" +}; +#undef REGISTER +#endif + +/*** Functions ***/ + +#if defined(PL041_DEBUG_LEVEL ) +static const char *get_reg_name (target_phys_addr_t offset)
Minor point but "pl041_reg_name()" would be a better name I think.
hw/pl041.c line 83
Quoted 2 lines
hw/pl041.c new file +2
-0+{ + if (offset <= 0xAC) {
Use sizeof() rather than a magic constant. Returning "unknown" if the pl041_regs_name[] array entry is NULL would also be more robust since it means we don't rely on having a REGISTER() entry for each offset.
Quoted 5 lines
hw/pl041.c new file +5
-0+ return pl041_regs_name[offset >> 2]; + } + + return "unknown"; +}
hw/pl041.c line 98
Quoted 10 lines
hw/pl041.c new file +10
-0+#endif + +static void pl041_fifo1_push (pl041_state *s,uint32_t value) +{ + pl041_fifo *f = &s->fifo1; + + /* Check the FIFO level */ + if (f->level >= f->size) { + hw_error("fifo1 push: overrun\n"); + }
Calling hw_error() on a FIFO overrun looks wrong. hw_error() will make qemu call abort() so as a general rule a device model should not use it for any condition the guest can provoke.
Quoted 25 lines
hw/pl041.c new file +25
-0+static void pl041_isr1_update (pl041_state *s) +{ + uint32_t mask = 0; + + /* Update ISR1 */ + if (s->regs.sr1 & TXUNDERRUN) { + s->regs.isr1 |= URINTR; + } else { + s->regs.isr1 &= ~URINTR; + } + + if (s->regs.sr1 & TXHE) { + s->regs.isr1 |= TXINTR; + } else { + s->regs.isr1 &= ~TXINTR; + } + + if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) { + s->regs.isr1 |= TXCINTR; + } else { + s->regs.isr1 &= ~TXCINTR; + } + + /* Set the irq mask */ + if (s->regs.ie1 & TXUIE) {
hw/pl041.c line 205
Quoted 10 lines
hw/pl041.c new file +10
-0+ mask |= URINTR; + } + + if (s->regs.ie1 & TXIE) { + mask |= TXINTR; + } + + if (s->regs.ie1 & TXCIE) { + mask |= TXCINTR; + }
The IEx mask bits are deliberately arranged to match the ISRx bits, so you don't need to construct the mask like this, you can just directly use s->regs.ie1 as your mask.
Quoted 7 lines
hw/pl041.c new file +6
-0+ /* Update the irq state */ + qemu_set_irq(s->irq, ((s->regs.isr1 & mask) > 0) ? 1 : 0); + DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n", + s->regs.sr1, s->regs.isr1, s->regs.isr1 & mask); +} +static uint32_t pl041_read (void *opaque,target_phys_addr_t offset)
hw/pl041.c line 232
Quoted 10 lines
hw/pl041.c new file +10
-0+{ + pl041_state *s = (pl041_state *)opaque; + int value; + + if (offset >= 0xfe0 && offset < 0x1000) { + DBG_L1("pl041_read [0x%08x]\n", offset); + return pl041_id[(offset - 0xfe0) >> 2]; + } + + if (offset < 0x110) {
Again, use sizeof().
Quoted 3 lines
+ value = *((uint32_t *)&s->regs + (offset >> 2));
+ } else {
+ hw_error("pl041_read: Bad offset %x\n", (int)offset);
hw_error() here is definitely wrong. I'd suggest printing a message if debugging is enabled and otherwise just read as zero/writes ignored.
Quoted 7 lines
+ }
+
+ switch (offset) {
+ casePL041_allints:
+ value =s->regs.isr1&0x7F;
+ break;
+ }
There's quite a lot of PL041 functionality unimplemented here; I'm not going to ask you to implement it all, but a comment at the top of the file somewhere giving a brief summary of what isn't implemented would be good.
Quoted 18 lines
hw/pl041.c new file +18
-0+static int pl041_init (SysBusDevice *dev) +{ + pl041_state *s = FROM_SYSBUS(pl041_state, dev); + int iomemtype; + + DBG_L1("pl041_init\n"); + + /* Connect the device to the sysbus */ + iomemtype = cpu_register_io_memory(pl041_readfn, + pl041_writefn, + s, + DEVICE_NATIVE_ENDIAN); + sysbus_init_mmio(dev, 0x1000, iomemtype); + sysbus_init_irq(dev, &s->irq); + + /* Reset the device */ + pl041_reset(s); +
hw/pl041.c line 404
Quoted 10 lines
hw/pl041.c new file +10
-0+ /* Init the codec */ + lm4549_init(&s->codec, &pl041_request_data, (void *)s); + + return 0; +} + +static void pl041_register_devices (void ) +{ + sysbus_register_dev("pl041", sizeof(pl041_state), pl041_init); +}
This is missing save/load support, for which you need:
(1) use sysbus_register_withprop() and pass it a SysBusDeviceInfo
(2) write a VMStateDescription and pass it as the .qdev.vmsd field of your SysBusDeviceInfo
(When you do this you can then pass your reset routine as .qdev.reset and you don't need to call it in your pl041_init() function. Watch out that the .reset routine takes a DeviceState*.)
You can see an example of this in hw/pl190.c.
hw/pl041.h line 2
Quoted 10 lines
hw/pl041.c new file +2
-0+ +device_init (pl041_register_devices)hw/pl041.h new file +2
-0@@ -0,0 +1,125 @@ +/* +* Arm PrimeCell PL041 Advanced Audio Codec Interface
This header is only used by hw/pl041.c so you might as well just put all its contents in the .c file. (If you do leave it as a separate header it needs #ifndef HW_PL041_H guards.)
-- PMM