2008-01-14 22:04:48 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-06-22 18:48:22 +00:00
|
|
|
* Copyright (C) 2006 by Michael Sevakis
|
|
|
|
* Copyright (C) 2008 by Rob Purchase
|
2008-01-14 22:04:48 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2008-01-14 22:04:48 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-06-22 18:48:22 +00:00
|
|
|
#include <stdlib.h>
|
2008-01-14 22:04:48 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "sound.h"
|
2010-08-01 09:33:29 +00:00
|
|
|
#include "i2s.h"
|
2008-06-22 18:48:22 +00:00
|
|
|
#include "pcm.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
#include "pcm-internal.h"
|
2008-06-22 18:48:22 +00:00
|
|
|
|
|
|
|
struct dma_data
|
|
|
|
{
|
|
|
|
/* NOTE: The order of size and p is important if you use assembler
|
|
|
|
optimised fiq handler, so don't change it. */
|
|
|
|
uint16_t *p;
|
|
|
|
size_t size;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
unsigned core;
|
|
|
|
#endif
|
|
|
|
int locked;
|
|
|
|
int state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
** Playback DMA transfer
|
|
|
|
**/
|
|
|
|
struct dma_data dma_play_data SHAREDBSS_ATTR =
|
|
|
|
{
|
|
|
|
/* Initialize to a locked, stopped state */
|
|
|
|
.p = NULL,
|
|
|
|
.size = 0,
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
.core = 0x00,
|
|
|
|
#endif
|
|
|
|
.locked = 0,
|
|
|
|
.state = 0
|
|
|
|
};
|
|
|
|
|
2008-01-14 22:04:48 +00:00
|
|
|
void pcm_postinit(void)
|
|
|
|
{
|
2008-11-26 14:52:31 +00:00
|
|
|
audiohw_postinit();
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const void * pcm_play_dma_get_peak_buffer(int *count)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
unsigned long addr = (unsigned long)dma_play_data.p;
|
|
|
|
size_t cnt = dma_play_data.size;
|
|
|
|
*count = cnt >> 2;
|
|
|
|
return (void *)((addr + 2) & ~3);
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_init(void)
|
|
|
|
{
|
2008-09-06 17:50:59 +00:00
|
|
|
DAVC = 0x0; /* Digital Volume = max */
|
|
|
|
#ifdef COWON_D2
|
2008-06-22 18:48:22 +00:00
|
|
|
/* Set DAI clock divided from PLL0 (192MHz).
|
|
|
|
The best approximation of 256*44.1kHz is 11.291MHz. */
|
|
|
|
BCLKCTR &= ~DEV_DAI;
|
|
|
|
PCLK_DAI = (1<<28) | 61682; /* DCO mode */
|
|
|
|
BCLKCTR |= DEV_DAI;
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
/* Enable DAI block in Master mode, 256fs->32fs, 16bit LSB */
|
|
|
|
DAMR = 0x3c8e80;
|
2008-09-06 17:50:59 +00:00
|
|
|
#elif defined(IAUDIO_7)
|
|
|
|
BCLKCTR &= ~DEV_DAI;
|
2009-09-20 19:53:15 +00:00
|
|
|
PCLK_DAI = (0x800a << 16) | (PCLK_DAI & 0xffff);
|
2008-09-06 17:50:59 +00:00
|
|
|
BCLKCTR |= DEV_DAI;
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-09-06 17:50:59 +00:00
|
|
|
/* Master mode, 256->64fs, 16bit LSB*/
|
|
|
|
DAMR = 0x3cce20;
|
2008-09-22 19:15:18 +00:00
|
|
|
#elif defined(LOGIK_DAX)
|
|
|
|
/* TODO */
|
|
|
|
#elif defined(SANSA_M200)
|
|
|
|
/* TODO */
|
2009-06-01 12:37:25 +00:00
|
|
|
#elif defined(SANSA_C100)
|
2009-09-20 19:53:15 +00:00
|
|
|
/* TODO */
|
2008-09-06 17:50:59 +00:00
|
|
|
#else
|
|
|
|
#error "Target isn't supported"
|
|
|
|
#endif
|
2008-06-22 18:48:22 +00:00
|
|
|
/* Set DAI interrupts as FIQs */
|
|
|
|
IRQSEL = ~(DAI_RX_IRQ_MASK | DAI_TX_IRQ_MASK);
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
/* Initialize default register values. */
|
|
|
|
audiohw_init();
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
dma_play_data.size = 0;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
dma_play_data.core = 0; /* no core in control */
|
|
|
|
#endif
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-12 11:01:07 +00:00
|
|
|
void pcm_dma_apply_settings(void)
|
2008-01-14 22:04:48 +00:00
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void play_start_pcm(void)
|
|
|
|
{
|
|
|
|
DAMR &= ~(1<<14); /* disable tx */
|
|
|
|
dma_play_data.state = 1;
|
|
|
|
|
|
|
|
if (dma_play_data.size >= 16)
|
|
|
|
{
|
|
|
|
DADO_L(0) = *dma_play_data.p++;
|
|
|
|
DADO_R(0) = *dma_play_data.p++;
|
|
|
|
DADO_L(1) = *dma_play_data.p++;
|
|
|
|
DADO_R(1) = *dma_play_data.p++;
|
|
|
|
DADO_L(2) = *dma_play_data.p++;
|
|
|
|
DADO_R(2) = *dma_play_data.p++;
|
|
|
|
DADO_L(3) = *dma_play_data.p++;
|
|
|
|
DADO_R(3) = *dma_play_data.p++;
|
|
|
|
dma_play_data.size -= 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
DAMR |= (1<<14); /* enable tx */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void play_stop_pcm(void)
|
|
|
|
{
|
|
|
|
DAMR &= ~(1<<14); /* disable tx */
|
|
|
|
dma_play_data.state = 0;
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_start(const void *addr, size_t size)
|
|
|
|
{
|
2010-05-12 14:35:47 +00:00
|
|
|
dma_play_data.p = (uint16_t*)addr;
|
2010-05-12 14:05:36 +00:00
|
|
|
dma_play_data.size = size;
|
2008-06-22 18:48:22 +00:00
|
|
|
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
/* This will become more important later - and different ! */
|
|
|
|
dma_play_data.core = processor_id(); /* save initiating core */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IEN |= DAI_TX_IRQ_MASK;
|
|
|
|
|
|
|
|
play_start_pcm();
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_stop(void)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
play_stop_pcm();
|
|
|
|
dma_play_data.size = 0;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
dma_play_data.core = 0; /* no core in control */
|
|
|
|
#endif
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_lock(void)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
int status = disable_fiq_save();
|
|
|
|
|
|
|
|
if (++dma_play_data.locked == 1)
|
|
|
|
{
|
|
|
|
IEN &= ~DAI_TX_IRQ_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_fiq(status);
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_unlock(void)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
int status = disable_fiq_save();
|
|
|
|
|
|
|
|
if (--dma_play_data.locked == 0 && dma_play_data.state != 0)
|
|
|
|
{
|
|
|
|
IEN |= DAI_TX_IRQ_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_fiq(status);
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_pause(bool pause)
|
|
|
|
{
|
2008-09-16 08:09:44 +00:00
|
|
|
if (pause) {
|
|
|
|
play_stop_pcm();
|
|
|
|
} else {
|
|
|
|
play_start_pcm();
|
|
|
|
}
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t pcm_get_bytes_waiting(void)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
return dma_play_data.size & ~3;
|
2008-01-14 22:04:48 +00:00
|
|
|
}
|
2008-04-21 20:16:18 +00:00
|
|
|
|
2008-09-06 17:50:59 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
/* TODO: implement */
|
|
|
|
void pcm_rec_dma_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_dma_close(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_dma_start(void *addr, size_t size)
|
|
|
|
{
|
|
|
|
(void) addr;
|
|
|
|
(void) size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_dma_stop(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_lock(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_unlock(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-12 14:05:36 +00:00
|
|
|
const void * pcm_rec_dma_get_peak_buffer(void)
|
2008-09-06 17:50:59 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-16 08:09:44 +00:00
|
|
|
#if defined(CPU_TCC77X) || defined(CPU_TCC780X)
|
2008-06-22 18:48:22 +00:00
|
|
|
void fiq_handler(void) ICODE_ATTR __attribute__((naked));
|
2008-04-21 20:16:18 +00:00
|
|
|
void fiq_handler(void)
|
|
|
|
{
|
2008-06-22 18:48:22 +00:00
|
|
|
/* r10 contains DADO_L0 base address (set in crt0.S to minimise code in the
|
|
|
|
* FIQ handler. r11 contains address of p (also set in crt0.S). Most other
|
|
|
|
* addresses we need are generated by using offsets with these two.
|
|
|
|
* r8 and r9 contains local copies of p and size respectively.
|
|
|
|
* r0-r3 and r12 is a working register.
|
|
|
|
*/
|
2009-09-20 19:53:15 +00:00
|
|
|
asm volatile (
|
2011-06-29 06:37:04 +00:00
|
|
|
"stmfd sp!, { r0-r4, lr } \n" /* stack scratch regs and lr */
|
|
|
|
"mov r4, #0 \n" /* Was the callback called? */
|
2008-09-16 08:09:44 +00:00
|
|
|
#if defined(CPU_TCC780X)
|
2008-06-27 12:39:03 +00:00
|
|
|
"mov r8, #0xc000 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
|
|
|
|
"ldr r9, =0xf3001004 \n" /* CREQ */
|
2008-09-16 08:09:44 +00:00
|
|
|
#elif defined(CPU_TCC77X)
|
|
|
|
"mov r8, #0x0030 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
|
|
|
|
"ldr r9, =0x80000104 \n" /* CREQ */
|
|
|
|
#endif
|
2008-06-27 12:39:03 +00:00
|
|
|
"str r8, [r9] \n" /* clear DAI IRQs */
|
2008-06-22 18:48:22 +00:00
|
|
|
"ldmia r11, { r8-r9 } \n" /* r8 = p, r9 = size */
|
|
|
|
"cmp r9, #0x10 \n" /* is size <16? */
|
|
|
|
"blt .more_data \n" /* if so, ask pcmbuf for more data */
|
|
|
|
|
|
|
|
".fill_fifo: \n"
|
|
|
|
"ldr r12, [r8], #4 \n" /* load two samples */
|
|
|
|
"str r12, [r10, #0x0] \n" /* write top sample to DADO_L0 */
|
|
|
|
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
|
|
|
|
"str r12, [r10, #0x4] \n" /* write low sample to DADO_R0*/
|
|
|
|
"ldr r12, [r8], #4 \n" /* load two samples */
|
|
|
|
"str r12, [r10, #0x8] \n" /* write top sample to DADO_L1 */
|
|
|
|
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
|
|
|
|
"str r12, [r10, #0xc] \n" /* write low sample to DADO_R1*/
|
|
|
|
"ldr r12, [r8], #4 \n" /* load two samples */
|
|
|
|
"str r12, [r10, #0x10] \n" /* write top sample to DADO_L2 */
|
|
|
|
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
|
|
|
|
"str r12, [r10, #0x14] \n" /* write low sample to DADO_R2*/
|
|
|
|
"ldr r12, [r8], #4 \n" /* load two samples */
|
|
|
|
"str r12, [r10, #0x18] \n" /* write top sample to DADO_L3 */
|
|
|
|
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
|
|
|
|
"str r12, [r10, #0x1c] \n" /* write low sample to DADO_R3*/
|
|
|
|
"sub r9, r9, #0x10 \n" /* 4 words written */
|
|
|
|
"stmia r11, { r8-r9 } \n" /* save p and size */
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
"cmp r4, #0 \n" /* Callback called? */
|
|
|
|
"beq .exit \n"
|
|
|
|
/* "mov r4, #0 \n" If get_more could be called multiple times! */
|
|
|
|
"ldr r2, =pcm_play_dma_started\n"
|
|
|
|
"ldr r2, [r2] \n"
|
|
|
|
"cmp r2, #0 \n"
|
|
|
|
"blxne r2 \n"
|
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
".exit: \n"
|
2011-06-29 06:37:04 +00:00
|
|
|
"ldmfd sp!, { r0-r4, lr } \n"
|
2008-06-22 18:48:22 +00:00
|
|
|
"subs pc, lr, #4 \n" /* FIQ specific return sequence */
|
|
|
|
|
|
|
|
".more_data: \n"
|
2011-06-29 06:37:04 +00:00
|
|
|
"mov r4, #1 \n" /* Remember we got more data in this FIQ */
|
2010-05-24 16:42:32 +00:00
|
|
|
"ldr r2, =pcm_play_get_more_callback \n"
|
|
|
|
"mov r0, r11 \n" /* r0 = &p */
|
|
|
|
"add r1, r11, #4 \n" /* r1 = &size */
|
|
|
|
"blx r2 \n" /* call pcm_play_get_more_callback */
|
|
|
|
"ldmia r11, { r8-r9 } \n" /* load new p and size */
|
|
|
|
"cmp r9, #0x10 \n" /* did we actually get enough data? */
|
|
|
|
"bpl .fill_fifo \n" /* not stop and enough? refill */
|
2008-06-22 18:48:22 +00:00
|
|
|
"b .exit \n"
|
|
|
|
".ltorg \n"
|
2008-04-21 20:16:18 +00:00
|
|
|
);
|
|
|
|
}
|
2008-06-22 18:48:22 +00:00
|
|
|
#else /* C version for reference */
|
2011-06-29 06:37:04 +00:00
|
|
|
void fiq_handler(void) ICODE_ATTR;
|
2008-06-22 18:48:22 +00:00
|
|
|
void fiq_handler(void)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
register bool new_buffer = false;
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
if (dma_play_data.size < 16)
|
|
|
|
{
|
|
|
|
/* p is empty, get some more data */
|
2011-06-29 06:37:04 +00:00
|
|
|
new_buffer = true;
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_get_more_callback((void**)&dma_play_data.p,
|
|
|
|
&dma_play_data.size);
|
2008-06-22 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dma_play_data.size >= 16)
|
|
|
|
{
|
|
|
|
DADO_L(0) = *dma_play_data.p++;
|
|
|
|
DADO_R(0) = *dma_play_data.p++;
|
|
|
|
DADO_L(1) = *dma_play_data.p++;
|
|
|
|
DADO_R(1) = *dma_play_data.p++;
|
|
|
|
DADO_L(2) = *dma_play_data.p++;
|
|
|
|
DADO_R(2) = *dma_play_data.p++;
|
|
|
|
DADO_L(3) = *dma_play_data.p++;
|
|
|
|
DADO_R(3) = *dma_play_data.p++;
|
|
|
|
|
|
|
|
dma_play_data.size -= 16;
|
|
|
|
}
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2008-06-22 18:48:22 +00:00
|
|
|
/* Clear FIQ status */
|
|
|
|
CREQ = DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK;
|
2009-09-20 19:53:15 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (new_buffer)
|
|
|
|
pcm_play_dma_started_callback();
|
2008-06-22 18:48:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
2008-09-06 17:50:59 +00:00
|
|
|
|
2009-09-20 19:53:15 +00:00
|
|
|
/* TODO: required by wm8731 codec */
|
2008-09-06 17:50:59 +00:00
|
|
|
void i2s_reset(void)
|
|
|
|
{
|
2009-09-20 19:53:15 +00:00
|
|
|
/* DAMR = 0; */
|
2008-09-06 17:50:59 +00:00
|
|
|
}
|