iPod Classic: use new PL080 DMA driver

This patch uses the new pl080 DMA driver for I2S playback and LCD
update. I have tried to be as fiel as possible to the current
behaviour, algorithms and configurations are the same, but using
the new driver. Other modifications:

Playback:

 - CHUNK_SIZE is decreased from 42988 to 8188 bytes, it does not
   affect normal playback (block size 1024), was tested using
   metronome (block size 46080). This change is needed because the
   new code commits d-cache range instead of commiting the whole
   d-cache, maximum time spent commiting the range should be
   limited, CHUNK_SIZE can be decreased even more if necessary.

 - pcm_play_dma_start() calls pcm_play_dma_stop() to stop the
   channel when it is running (metronome replays the tick sound
   without stopping the channel).

 - pcm_play_dma_get_peak_buffer(): same as actual SVN function but
   returns samples count instead of bytes count.
   TODO: AFAIK, actually this function is not used in RB. Not tested,
   but probably this function will fail because it returns pointers
   to the internal double buffer.

LCD update:

 - suppresses lcd_wakeup semaphore and uses yield()

Change-Id: I79b8aa47a941e0dd91847150618f3f7f676c26ef
This commit is contained in:
Cástor Muñoz 2014-12-06 18:37:09 +01:00
parent d6ee2c9eaf
commit 67b4e7f958
9 changed files with 336 additions and 343 deletions

View file

@ -31,104 +31,129 @@
#include "pcm_sampr.h"
#include "mmu-arm.h"
#include "pcm-target.h"
#include "dma-s5l8702.h"
/* DMA configuration */
/* 3 DMA tasks needed, one chunk task and two dblbuf tasks */
#define DMA_PLAY_TSKBUF_SZ 4 /* N tasks, MUST be pow2 */
#define DMA_PLAY_LLIBUF_SZ 4 /* N LLIs, MUST be pow2 */
static struct dmac_tsk dma_play_tskbuf[DMA_PLAY_TSKBUF_SZ];
static struct dmac_lli volatile \
dma_play_llibuf[DMA_PLAY_LLIBUF_SZ] CACHEALIGN_ATTR;
static void dma_play_callback(void *data) ICODE_ATTR;
static struct dmac_ch dma_play_ch = {
.dmac = &s5l8702_dmac0,
.prio = DMAC_CH_PRIO(2),
.cb_fn = dma_play_callback,
.tskbuf = dma_play_tskbuf,
.tskbuf_mask = DMA_PLAY_TSKBUF_SZ - 1,
.queue_mode = QUEUE_LINK,
.llibuf = dma_play_llibuf,
.llibuf_mask = DMA_PLAY_LLIBUF_SZ - 1,
.llibuf_bus = DMAC_MASTER_AHB1,
};
static struct dmac_ch_cfg dma_play_ch_cfg = {
.srcperi = S5L8702_DMAC0_PERI_MEM,
.dstperi = S5L8702_DMAC0_PERI_IIS0_TX,
.sbsize = DMACCxCONTROL_BSIZE_8,
.dbsize = DMACCxCONTROL_BSIZE_4,
.swidth = DMACCxCONTROL_WIDTH_16,
.dwidth = DMACCxCONTROL_WIDTH_16,
.sbus = DMAC_MASTER_AHB1,
.dbus = DMAC_MASTER_AHB1,
.sinc = DMACCxCONTROL_INC_ENABLE,
.dinc = DMACCxCONTROL_INC_DISABLE,
.prot = DMAC_PROT_CACH | DMAC_PROT_BUFF | DMAC_PROT_PRIV,
/* align LLI transfers to L-R pairs (samples) */
.lli_xfer_max_count = DMAC_LLI_MAX_COUNT & ~1,
};
#define LLI_MAX_BYTES 8188 /* lli_xfer_max_count << swidth */
/* Use all available LLIs for chunk */
/*#define CHUNK_MAX_BYTES (LLI_MAX_BYTES * (DMA_PLAY_LLIBUF_SZ - 2))*/
#define CHUNK_MAX_BYTES (LLI_MAX_BYTES * 1)
#define WATERMARK_BYTES (PCM_WATERMARK * 4)
static volatile int locked = 0;
static const int zerosample = 0;
static unsigned char dblbuf[2][PCM_WATERMARK * 4];
static unsigned char dblbuf[2][WATERMARK_BYTES] CACHEALIGN_ATTR;
static int active_dblbuf;
struct dma_lli pcm_lli[PCM_LLICOUNT] __attribute__((aligned(16)));
static struct dma_lli* lastlli;
static const void* dataptr;
size_t pcm_remaining;
size_t pcm_chunksize;
/* Mask the DMA interrupt */
void pcm_play_lock(void)
{
if (locked++ == 0) {
//TODO: Urgh, I don't like that at all...
VIC0INTENCLEAR = 1 << IRQ_DMAC0;
}
if (locked++ == 0)
dmac_ch_lock_int(&dma_play_ch);
}
/* Unmask the DMA interrupt if enabled */
void pcm_play_unlock(void)
{
if (--locked == 0) {
VIC0INTENABLE = 1 << IRQ_DMAC0;
}
if (--locked == 0)
dmac_ch_unlock_int(&dma_play_ch);
}
void INT_DMAC0C0(void) ICODE_ATTR;
void INT_DMAC0C0(void)
static inline void play_queue_dma(void *addr, size_t size, void *cb_data)
{
DMAC0INTTCCLR = 1;
commit_dcache_range(addr, size);
dmac_ch_queue(&dma_play_ch, addr,
(void*)S5L8702_DADDR_PERI_IIS0_TX, size, cb_data);
}
static void dma_play_callback(void *cb_data)
{
if (!cb_data)
return; /* dblbuf callback entered, nothing to do */
const void *dataptr = cb_data;
if (!pcm_remaining)
{
pcm_play_dma_complete_callback(PCM_DMAST_OK, &dataptr, &pcm_remaining);
pcm_chunksize = pcm_remaining;
}
if (!pcm_remaining)
{
pcm_lli->nextlli = NULL;
pcm_lli->control = 0x7524a000;
commit_dcache();
return;
}
uint32_t lastsize = MIN(PCM_WATERMARK * 4, pcm_remaining / 2 + 1) & ~1;
if (!pcm_play_dma_complete_callback(
PCM_DMAST_OK, &dataptr, &pcm_remaining))
return;
uint32_t lastsize = MIN(WATERMARK_BYTES, pcm_remaining >> 1);
pcm_remaining -= lastsize;
if (pcm_remaining) lastlli = &pcm_lli[ARRAYLEN(pcm_lli) - 1];
else lastlli = pcm_lli;
uint32_t chunksize = MIN(PCM_CHUNKSIZE * 4 - lastsize, pcm_remaining);
if (pcm_remaining > chunksize && chunksize > pcm_remaining - PCM_WATERMARK * 8)
chunksize = pcm_remaining - PCM_WATERMARK * 8;
uint32_t chunksize = MIN(CHUNK_MAX_BYTES, pcm_remaining);
/* last chunk should be at least 2*WATERMARK_BYTES in size */
if ((pcm_remaining > chunksize) &&
(pcm_remaining < chunksize + WATERMARK_BYTES * 2))
chunksize = pcm_remaining - WATERMARK_BYTES * 2;
pcm_remaining -= chunksize;
bool last = !chunksize;
int i = 0;
while (chunksize)
{
uint32_t thislli = MIN(PCM_LLIMAX * 4, chunksize);
chunksize -= thislli;
pcm_lli[i].srcaddr = (void*)dataptr;
pcm_lli[i].dstaddr = (void*)((int)&I2STXDB0);
pcm_lli[i].nextlli = chunksize ? &pcm_lli[i + 1] : lastlli;
pcm_lli[i].control = (chunksize ? 0x7524a000 : 0xf524a000) | (thislli / 2);
dataptr += thislli;
i++;
}
if (!pcm_remaining)
{
memcpy(dblbuf[active_dblbuf], dataptr, lastsize);
lastlli->srcaddr = dblbuf[active_dblbuf];
active_dblbuf ^= 1;
}
else lastlli->srcaddr = dataptr;
lastlli->dstaddr = (void*)((int)&I2STXDB0);
lastlli->nextlli = last ? NULL : pcm_lli;
lastlli->control = (last ? 0xf524a000 : 0x7524a000) | (lastsize / 2);
dataptr += lastsize;
commit_dcache();
if (!(DMAC0C0CONFIG & 1) && (pcm_lli[0].control & 0xfff))
{
DMAC0C0LLI = pcm_lli[0];
DMAC0C0CONFIG = 0x8a81;
}
else DMAC0C0NEXTLLI = pcm_lli;
/* first part */
play_queue_dma((void*)dataptr, chunksize,
(void*)dataptr + chunksize + lastsize); /* cb_data */
/* second part */
memcpy(dblbuf[active_dblbuf], dataptr + chunksize, lastsize);
play_queue_dma(dblbuf[active_dblbuf], lastsize, NULL);
active_dblbuf ^= 1;
pcm_play_dma_status_callback(PCM_DMAST_STARTED);
}
void pcm_play_dma_start(const void* addr, size_t size)
{
dataptr = addr;
pcm_play_dma_stop();
pcm_remaining = size;
I2STXCOM = 0xe;
INT_DMAC0C0();
dma_play_callback((void*)addr);
}
void pcm_play_dma_stop(void)
{
DMAC0C0CONFIG = 0x8a80;
dmac_ch_stop(&dma_play_ch);
I2STXCOM = 0xa;
}
@ -184,9 +209,11 @@ void pcm_play_dma_init(void)
{
PWRCON(0) &= ~(1 << 4);
PWRCON(1) &= ~(1 << 7);
dmac_ch_init(&dma_play_ch, &dma_play_ch_cfg);
I2STXCON = 0xb100019;
I2SCLKCON = 1;
VIC0INTENABLE = 1 << IRQ_DMAC0;
audiohw_preinit();
pcm_dma_apply_settings();
@ -199,21 +226,16 @@ void pcm_play_dma_postinit(void)
size_t pcm_get_bytes_waiting(void)
{
int bytes = pcm_remaining;
const struct dma_lli* lli = (const struct dma_lli*)((int)&DMAC0C0LLI);
while (lli)
{
bytes += (lli->control & 0xfff) * 2;
if (lli == lastlli) break;
lli = lli->nextlli;
}
return bytes;
size_t total_bytes;
dmac_ch_get_info(&dma_play_ch, NULL, &total_bytes);
return total_bytes;
}
const void* pcm_play_dma_get_peak_buffer(int *count)
{
*count = (DMAC0C0LLI.control & 0xfff) * 2;
return (void*)(((uint32_t)DMAC0C0LLI.srcaddr) & ~3);
void *addr = dmac_ch_get_info(&dma_play_ch, count, NULL);
*count >>= 2; /* bytes to samples */
return addr; /* aligned to dest burst */
}
#ifdef HAVE_PCM_DMA_ADDRESS