2006-12-18 01:52:21 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Michael Sevakis
|
|
|
|
*
|
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.
|
2006-12-18 01:52:21 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "audio.h"
|
2007-03-11 06:21:43 +00:00
|
|
|
#include "sound.h"
|
2008-04-28 10:22:05 +00:00
|
|
|
#include "pcm.h"
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-01-20 12:59:23 +00:00
|
|
|
#ifdef HAVE_WM8751
|
|
|
|
#define MROBE100_44100HZ (0x40|(0x11 << 1)|1)
|
|
|
|
#endif
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/** DMA **/
|
|
|
|
|
2007-10-16 04:19:18 +00:00
|
|
|
#ifdef CPU_PP502x
|
2007-10-07 06:10:08 +00:00
|
|
|
/* 16-bit, L-R packed into 32 bits with left in the least significant halfword */
|
|
|
|
#define SAMPLE_SIZE 16
|
2007-10-06 22:27:27 +00:00
|
|
|
#else
|
2007-10-07 06:10:08 +00:00
|
|
|
/* 32-bit, one left 32-bit sample followed by one right 32-bit sample */
|
|
|
|
#define SAMPLE_SIZE 32
|
2006-12-18 02:37:35 +00:00
|
|
|
#endif
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +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. */
|
|
|
|
#if SAMPLE_SIZE == 16
|
|
|
|
uint32_t *p;
|
|
|
|
#elif SAMPLE_SIZE == 32
|
|
|
|
uint16_t *p;
|
2006-12-18 01:52:21 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
size_t size;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
unsigned core;
|
|
|
|
#endif
|
|
|
|
int locked;
|
|
|
|
int state;
|
2007-10-09 00:09:28 +00:00
|
|
|
};
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
extern void *fiq_function;
|
|
|
|
|
|
|
|
/* Dispatch to the proper handler and leave the main vector table alone */
|
|
|
|
void fiq_handler(void) ICODE_ATTR __attribute__((naked));
|
|
|
|
void fiq_handler(void)
|
|
|
|
{
|
|
|
|
asm volatile (
|
|
|
|
"ldr pc, [pc, #-4] \n"
|
|
|
|
"fiq_function: \n"
|
|
|
|
".word 0 \n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Get simultaneous recording and playback to work. Just needs some tweaking */
|
2006-12-18 01:52:21 +00:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
** Playback DMA transfer
|
|
|
|
**/
|
2008-04-06 04:34:57 +00:00
|
|
|
struct dma_data dma_play_data SHAREDBSS_ATTR =
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
|
|
|
/* Initialize to a locked, stopped state */
|
|
|
|
.p = NULL,
|
|
|
|
.size = 0,
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
.core = 0x00,
|
|
|
|
#endif
|
|
|
|
.locked = 0,
|
|
|
|
.state = 0
|
|
|
|
};
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-04-06 04:34:57 +00:00
|
|
|
static unsigned long pcm_freq SHAREDDATA_ATTR = HW_SAMPR_DEFAULT; /* 44.1 is default */
|
2008-01-20 12:59:23 +00:00
|
|
|
#ifdef HAVE_WM8751
|
|
|
|
/* Samplerate control for audio codec */
|
|
|
|
static int sr_ctrl = MROBE100_44100HZ;
|
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
void pcm_set_frequency(unsigned int frequency)
|
2008-09-29 20:50:26 +00:00
|
|
|
{
|
2008-09-30 20:26:25 +00:00
|
|
|
#if defined(HAVE_WM8731) || defined(HAVE_WM8721)
|
2008-09-29 20:50:26 +00:00
|
|
|
pcm_freq = frequency;
|
2008-09-28 23:25:07 +00:00
|
|
|
#else
|
2008-09-29 20:50:26 +00:00
|
|
|
(void)frequency;
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_freq = HW_SAMPR_DEFAULT;
|
2008-09-28 23:25:07 +00:00
|
|
|
#endif
|
2008-01-20 12:59:23 +00:00
|
|
|
#ifdef HAVE_WM8751
|
|
|
|
sr_ctrl = MROBE100_44100HZ;
|
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_apply_settings(void)
|
|
|
|
{
|
2008-01-20 12:59:23 +00:00
|
|
|
#ifdef HAVE_WM8751
|
|
|
|
audiohw_set_frequency(sr_ctrl);
|
|
|
|
#endif
|
2008-09-28 23:25:07 +00:00
|
|
|
|
2008-09-30 20:26:25 +00:00
|
|
|
#if defined(HAVE_WM8731) || defined(HAVE_WM8721)
|
2008-09-29 20:50:26 +00:00
|
|
|
audiohw_set_sample_rate(pcm_freq);
|
2008-09-28 23:25:07 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_curr_sampr = pcm_freq;
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-04-18 18:52:11 +00:00
|
|
|
/* ASM optimised FIQ handler. Checks for the minimum allowed loop cycles by
|
|
|
|
* evalutation of free IISFIFO-slots against available source buffer words.
|
|
|
|
* Through this it is possible to move the check for IIS_TX_FREE_COUNT outside
|
|
|
|
* the loop and do some further optimization. Right after the loops (source
|
|
|
|
* buffer -> IISFIFO) are done we need to check whether we have to exit FIQ
|
|
|
|
* handler (this must be done, if all free FIFO slots were filled) or we will
|
|
|
|
* have to get some new source data. Important information kept from former
|
|
|
|
* ASM implementation (not used anymore): GCC fails to make use of the fact
|
|
|
|
* that FIQ mode has registers r8-r14 banked, and so does not need to be saved.
|
|
|
|
* This routine uses only these registers, and so will never touch the stack
|
|
|
|
* unless it actually needs to do so when calling pcm_callback_for_more.
|
|
|
|
* C version is still included below for reference and testing.
|
2006-12-18 01:52:21 +00:00
|
|
|
*/
|
2007-07-02 05:16:40 +00:00
|
|
|
#if 1
|
2007-10-06 22:27:27 +00:00
|
|
|
void fiq_playback(void) ICODE_ATTR __attribute__((naked));
|
|
|
|
void fiq_playback(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* r10 contains IISCONFIG address (set in crt0.S to minimise code in actual
|
2006-12-18 01:52:21 +00:00
|
|
|
* 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.
|
2007-10-06 22:27:27 +00:00
|
|
|
* r10 + 0x40 is IISFIFO_WR, and r10 + 0x0c is IISFIFO_CFG.
|
|
|
|
* r8 and r9 contains local copies of p and size respectively.
|
2008-04-13 17:03:24 +00:00
|
|
|
* r0-r3 and r12 is a working register.
|
2006-12-18 01:52:21 +00:00
|
|
|
*/
|
|
|
|
asm volatile (
|
2008-04-13 17:03:24 +00:00
|
|
|
"stmfd sp!, { r0-r3, lr } \n" /* stack scratch regs and lr */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2006-12-18 01:52:21 +00:00
|
|
|
#if CONFIG_CPU == PP5002
|
2007-10-06 22:27:27 +00:00
|
|
|
"ldr r12, =0xcf001040 \n" /* Some magic from iPodLinux */
|
|
|
|
"ldr r12, [r12] \n"
|
2006-12-18 01:52:21 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
"ldmia r11, { r8-r9 } \n" /* r8 = p, r9 = size */
|
|
|
|
"cmp r9, #0 \n" /* is size 0? */
|
|
|
|
"beq .more_data \n" /* if so, ask pcmbuf for more data */
|
2008-04-13 17:03:24 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#if SAMPLE_SIZE == 16
|
2008-04-13 17:03:24 +00:00
|
|
|
".check_fifo: \n"
|
|
|
|
"ldr r0, [r10, %[cfg]] \n" /* read IISFIFO_CFG to check FIFO status */
|
|
|
|
"and r0, r0, %[mask] \n" /* r0 = IIS_TX_FREE_COUNT << 16 (PP502x) */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
|
|
|
"mov r1, r0, lsr #16 \n" /* number of free FIFO slots */
|
2008-04-13 17:03:24 +00:00
|
|
|
"cmp r1, r9, lsr #2 \n" /* number of words from source */
|
|
|
|
"movgt r1, r9, lsr #2 \n" /* r1 = amount of allowed loops */
|
|
|
|
"sub r9, r9, r1, lsl #2 \n" /* r1 words will be written in following loop */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2008-04-13 17:03:24 +00:00
|
|
|
"subs r1, r1, #2 \n"
|
|
|
|
".fifo_loop_2: \n"
|
|
|
|
"ldmgeia r8!, {r2, r12} \n" /* load four samples */
|
|
|
|
"strge r2 , [r10, %[wr]] \n" /* write sample 0-1 to IISFIFO_WR */
|
|
|
|
"strge r12, [r10, %[wr]] \n" /* write sample 2-3 to IISFIFO_WR */
|
|
|
|
"subges r1, r1, #2 \n" /* one more loop? */
|
|
|
|
"bge .fifo_loop_2 \n" /* yes, continue */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2008-04-13 17:03:24 +00:00
|
|
|
"tst r1, #1 \n" /* two samples (one word) left? */
|
|
|
|
"ldrne r12, [r8], #4 \n" /* load two samples */
|
|
|
|
"strne r12, [r10, %[wr]] \n" /* write sample 0-1 to IISFIFO_WR */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2008-04-13 17:03:24 +00:00
|
|
|
"cmp r9, #0 \n" /* either FIFO is full or source buffer is empty */
|
|
|
|
"bgt .exit \n" /* if source buffer is not empty, FIFO must be full */
|
2007-10-06 22:27:27 +00:00
|
|
|
#elif SAMPLE_SIZE == 32
|
2008-09-29 20:50:26 +00:00
|
|
|
".check_fifo: \n"
|
2008-04-13 17:03:24 +00:00
|
|
|
"ldr r0, [r10, %[cfg]] \n" /* read IISFIFO_CFG to check FIFO status */
|
|
|
|
"and r0, r0, %[mask] \n" /* r0 = IIS_TX_FREE_COUNT << 23 (PP5002) */
|
2008-04-18 18:52:11 +00:00
|
|
|
|
|
|
|
"movs r1, r0, lsr #24 \n" /* number of free pairs of FIFO slots */
|
|
|
|
"beq .exit \n" /* no complete pair? -> exit */
|
2008-04-13 17:03:24 +00:00
|
|
|
"cmp r1, r9, lsr #2 \n" /* number of words from source */
|
|
|
|
"movgt r1, r9, lsr #2 \n" /* r1 = amount of allowed loops */
|
|
|
|
"sub r9, r9, r1, lsl #2 \n" /* r1 words will be written in following loop */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2008-04-13 17:03:24 +00:00
|
|
|
".fifo_loop: \n"
|
2007-10-07 06:10:08 +00:00
|
|
|
"ldr r12, [r8], #4 \n" /* load two samples */
|
2008-04-13 17:03:24 +00:00
|
|
|
"mov r2 , r12, lsl #16 \n" /* put left sample at the top bits */
|
|
|
|
"str r2 , [r10, %[wr]] \n" /* write top sample to IISFIFO_WR */
|
|
|
|
"str r12, [r10, %[wr]] \n" /* write low sample to IISFIFO_WR*/
|
|
|
|
"subs r1, r1, #1 \n" /* one more loop? */
|
|
|
|
"bgt .fifo_loop \n" /* yes, continue */
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2008-04-13 17:03:24 +00:00
|
|
|
"cmp r9, #0 \n" /* either FIFO is full or source buffer is empty */
|
|
|
|
"bgt .exit \n" /* if source buffer is not empty, FIFO must be full */
|
2006-12-18 01:52:21 +00:00
|
|
|
#endif
|
2008-09-29 20:50:26 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
".more_data: \n"
|
|
|
|
"ldr r2, =pcm_callback_for_more \n"
|
|
|
|
"ldr r2, [r2] \n" /* get callback address */
|
|
|
|
"cmp r2, #0 \n" /* check for null pointer */
|
|
|
|
"stmneia r11, { r8-r9 } \n" /* save internal copies of variables back */
|
|
|
|
"movne r0, r11 \n" /* r0 = &p */
|
|
|
|
"addne r1, r11, #4 \n" /* r1 = &size */
|
|
|
|
"movne lr, pc \n" /* call pcm_callback_for_more */
|
|
|
|
"bxne r2 \n"
|
|
|
|
"ldmia r11, { r8-r9 } \n" /* reload p and size */
|
|
|
|
"cmp r9, #0 \n" /* did we actually get more data? */
|
2008-04-13 17:03:24 +00:00
|
|
|
"bne .check_fifo \n"
|
2007-10-06 22:27:27 +00:00
|
|
|
"ldr r12, =pcm_play_dma_stop \n"
|
|
|
|
"mov lr, pc \n"
|
|
|
|
"bx r12 \n"
|
|
|
|
"ldr r12, =pcm_play_dma_stopped_callback \n"
|
|
|
|
"mov lr, pc \n"
|
|
|
|
"bx r12 \n"
|
2008-04-13 17:03:24 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
".exit: \n" /* (r8=0 if stopping, look above) */
|
|
|
|
"stmia r11, { r8-r9 } \n" /* save p and size */
|
2008-04-13 17:03:24 +00:00
|
|
|
"ldmfd sp!, { r0-r3, lr } \n"
|
2007-10-06 22:27:27 +00:00
|
|
|
"subs pc, lr, #4 \n" /* FIQ specific return sequence */
|
|
|
|
".ltorg \n"
|
|
|
|
: /* These must only be integers! No regs */
|
2008-04-13 17:03:24 +00:00
|
|
|
: [mask]"i"(IIS_TX_FREE_MASK),
|
2007-10-06 22:27:27 +00:00
|
|
|
[cfg]"i"((int)&IISFIFO_CFG - (int)&IISCONFIG),
|
|
|
|
[wr]"i"((int)&IISFIFO_WR - (int)&IISCONFIG)
|
2006-12-18 01:52:21 +00:00
|
|
|
);
|
|
|
|
}
|
2007-07-02 05:16:40 +00:00
|
|
|
#else /* C version for reference */
|
2007-10-06 22:27:27 +00:00
|
|
|
void fiq_playback(void) __attribute__((interrupt ("FIQ"))) ICODE_ATTR;
|
|
|
|
/* NOTE: direct stack use forbidden by GCC stack handling bug for FIQ */
|
|
|
|
void fiq_playback(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
register pcm_more_callback_type get_more;
|
|
|
|
|
|
|
|
#if CONFIG_CPU == PP5002
|
2006-12-18 01:52:21 +00:00
|
|
|
inl(0xcf001040);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
do {
|
2007-10-06 22:27:27 +00:00
|
|
|
while (dma_play_data.size > 0) {
|
|
|
|
if (IIS_TX_FREE_COUNT < 2) {
|
2006-12-18 01:52:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
#if SAMPLE_SIZE == 16
|
2007-10-07 20:14:32 +00:00
|
|
|
IISFIFO_WR = *dma_play_data.p++;
|
2007-10-06 22:27:27 +00:00
|
|
|
#elif SAMPLE_SIZE == 32
|
|
|
|
IISFIFO_WR = *dma_play_data.p++ << 16;
|
|
|
|
IISFIFO_WR = *dma_play_data.p++ << 16;
|
2007-05-22 15:34:24 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
dma_play_data.size -= 4;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* p is empty, get some more data */
|
2007-10-06 22:27:27 +00:00
|
|
|
get_more = pcm_callback_for_more;
|
|
|
|
if (get_more) {
|
|
|
|
get_more((unsigned char**)&dma_play_data.p,
|
|
|
|
&dma_play_data.size);
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
} while (dma_play_data.size);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* No more data, so disable the FIFO/interrupt */
|
2006-12-18 01:52:21 +00:00
|
|
|
pcm_play_dma_stop();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_play_dma_stopped_callback();
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-07-02 05:16:40 +00:00
|
|
|
#endif /* ASM / C selection */
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* For the locks, FIQ must be disabled because the handler manipulates
|
|
|
|
IISCONFIG and the operation is not atomic - dual core support
|
|
|
|
will require other measures */
|
|
|
|
void pcm_play_lock(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2008-03-26 01:50:41 +00:00
|
|
|
int status = disable_fiq_save();
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (++dma_play_data.locked == 1) {
|
|
|
|
IIS_IRQTX_REG &= ~IIS_IRQTX;
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_fiq(status);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_unlock(void)
|
|
|
|
{
|
2008-03-26 01:50:41 +00:00
|
|
|
int status = disable_fiq_save();
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (--dma_play_data.locked == 0 && dma_play_data.state != 0) {
|
|
|
|
IIS_IRQTX_REG |= IIS_IRQTX;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_fiq(status);
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
static void play_start_pcm(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
fiq_function = fiq_playback;
|
|
|
|
pcm_apply_settings();
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
IISCONFIG &= ~IIS_TXFIFOEN; /* Stop transmitting */
|
|
|
|
dma_play_data.state = 1;
|
|
|
|
|
|
|
|
/* Fill the FIFO or start when data is used up */
|
|
|
|
while (1) {
|
|
|
|
if (IIS_TX_FREE_COUNT < 2 || dma_play_data.size == 0) {
|
|
|
|
IISCONFIG |= IIS_TXFIFOEN; /* Start transmitting */
|
|
|
|
return;
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#if SAMPLE_SIZE == 16
|
|
|
|
IISFIFO_WR = *dma_play_data.p++;
|
|
|
|
#elif SAMPLE_SIZE == 32
|
|
|
|
IISFIFO_WR = *dma_play_data.p++ << 16;
|
|
|
|
IISFIFO_WR = *dma_play_data.p++ << 16;
|
|
|
|
#endif
|
|
|
|
dma_play_data.size -= 4;
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
static void play_stop_pcm(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-02 07:48:50 +00:00
|
|
|
/* Disable TX interrupt */
|
2007-10-06 22:27:27 +00:00
|
|
|
IIS_IRQTX_REG &= ~IIS_IRQTX;
|
|
|
|
dma_play_data.state = 0;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_start(const void *addr, size_t size)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-07 20:14:32 +00:00
|
|
|
dma_play_data.p = (void *)(((uintptr_t)addr + 2) & ~3);
|
|
|
|
dma_play_data.size = (size & ~3);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#if NUM_CORES > 1
|
|
|
|
/* This will become more important later - and different ! */
|
|
|
|
dma_play_data.core = processor_id(); /* save initiating core */
|
2006-12-18 01:52:21 +00:00
|
|
|
#endif
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
CPU_INT_PRIORITY |= IIS_MASK; /* FIQ priority for I2S */
|
|
|
|
CPU_INT_EN = IIS_MASK;
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
play_start_pcm();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stops the DMA transfer and interrupt */
|
|
|
|
void pcm_play_dma_stop(void)
|
|
|
|
{
|
|
|
|
play_stop_pcm();
|
|
|
|
dma_play_data.size = 0;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
dma_play_data.core = 0; /* no core in control */
|
2007-05-22 15:34:24 +00:00
|
|
|
#endif
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_pause(bool pause)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
if (pause) {
|
|
|
|
play_stop_pcm();
|
|
|
|
} else {
|
|
|
|
play_start_pcm();
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t pcm_get_bytes_waiting(void)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
return dma_play_data.size & ~3;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_init(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_set_frequency(SAMPR_44);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
|
|
|
/* Initialize default register values. */
|
|
|
|
audiohw_init();
|
2007-03-11 17:38:08 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
dma_play_data.size = 0;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
dma_play_data.core = 0; /* no core in control */
|
2007-10-02 07:48:50 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
IISCONFIG |= IIS_TXFIFOEN;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-03-11 06:21:43 +00:00
|
|
|
|
|
|
|
void pcm_postinit(void)
|
|
|
|
{
|
|
|
|
audiohw_postinit();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_apply_settings();
|
|
|
|
}
|
|
|
|
|
|
|
|
const void * pcm_play_dma_get_peak_buffer(int *count)
|
|
|
|
{
|
|
|
|
unsigned long addr = (unsigned long)dma_play_data.p;
|
|
|
|
size_t cnt = dma_play_data.size;
|
|
|
|
*count = cnt >> 2;
|
|
|
|
return (void *)((addr + 2) & ~3);
|
2007-03-11 06:21:43 +00:00
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
** Recording DMA transfer
|
|
|
|
**/
|
2006-12-18 02:37:35 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
2007-10-06 22:27:27 +00:00
|
|
|
/* PCM recording interrupt routine lockout */
|
2008-04-06 04:34:57 +00:00
|
|
|
static struct dma_data dma_rec_data SHAREDBSS_ATTR =
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
|
|
|
/* Initialize to a locked, stopped state */
|
|
|
|
.p = NULL,
|
|
|
|
.size = 0,
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
.core = 0x00,
|
|
|
|
#endif
|
|
|
|
.locked = 0,
|
|
|
|
.state = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* For the locks, FIQ must be disabled because the handler manipulates
|
|
|
|
IISCONFIG and the operation is not atomic - dual core support
|
|
|
|
will require other measures */
|
|
|
|
void pcm_rec_lock(void)
|
|
|
|
{
|
2008-03-26 01:50:41 +00:00
|
|
|
int status = disable_fiq_save();
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (++dma_rec_data.locked == 1)
|
|
|
|
IIS_IRQRX_REG &= ~IIS_IRQRX;
|
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_fiq(status);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_unlock(void)
|
2007-06-05 07:03:30 +00:00
|
|
|
{
|
2008-03-26 01:50:41 +00:00
|
|
|
int status = disable_fiq_save();
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
if (--dma_rec_data.locked == 0 && dma_rec_data.state != 0)
|
|
|
|
IIS_IRQRX_REG |= IIS_IRQRX;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_fiq(status);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: direct stack use forbidden by GCC stack handling bug for FIQ */
|
|
|
|
void fiq_record(void) ICODE_ATTR __attribute__((interrupt ("FIQ")));
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#if defined(SANSA_C200) || defined(SANSA_E200)
|
|
|
|
void fiq_record(void)
|
|
|
|
{
|
|
|
|
register pcm_more_callback_type2 more_ready;
|
|
|
|
register int32_t value;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
|
|
|
if (audio_channels == 2) {
|
|
|
|
/* RX is stereo */
|
2007-10-06 22:27:27 +00:00
|
|
|
while (dma_rec_data.size > 0) {
|
|
|
|
if (IIS_RX_FULL_COUNT < 2) {
|
|
|
|
return;
|
2007-06-05 07:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Discard every other sample since ADC clock is 1/2 LRCK */
|
2007-10-06 22:27:27 +00:00
|
|
|
value = IISFIFO_RD;
|
|
|
|
IISFIFO_RD;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
*dma_rec_data.p++ = value;
|
|
|
|
dma_rec_data.size -= 4;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
|
|
|
/* TODO: Figure out how to do IIS loopback */
|
|
|
|
if (audio_output_source != AUDIO_SRC_PLAYBACK) {
|
2007-10-06 22:27:27 +00:00
|
|
|
if (IIS_TX_FREE_COUNT >= 16) {
|
2007-10-04 12:55:46 +00:00
|
|
|
/* Resync the output FIFO - it ran dry */
|
|
|
|
IISFIFO_WR = 0;
|
|
|
|
IISFIFO_WR = 0;
|
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
IISFIFO_WR = value;
|
|
|
|
IISFIFO_WR = value;
|
2007-06-05 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* RX is left channel mono */
|
2007-10-06 22:27:27 +00:00
|
|
|
while (dma_rec_data.size > 0) {
|
|
|
|
if (IIS_RX_FULL_COUNT < 2) {
|
|
|
|
return;
|
2007-06-05 07:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Discard every other sample since ADC clock is 1/2 LRCK */
|
2007-10-06 22:27:27 +00:00
|
|
|
value = IISFIFO_RD;
|
|
|
|
IISFIFO_RD;
|
|
|
|
|
|
|
|
value = (uint16_t)value | (value << 16);
|
|
|
|
|
|
|
|
*dma_rec_data.p++ = value;
|
|
|
|
dma_rec_data.size -= 4;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
|
|
|
if (audio_output_source != AUDIO_SRC_PLAYBACK) {
|
2007-10-06 22:27:27 +00:00
|
|
|
if (IIS_TX_FREE_COUNT >= 16) {
|
2007-10-04 12:55:46 +00:00
|
|
|
/* Resync the output FIFO - it ran dry */
|
|
|
|
IISFIFO_WR = 0;
|
|
|
|
IISFIFO_WR = 0;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
value = *((int32_t *)dma_rec_data.p - 1);
|
|
|
|
IISFIFO_WR = value;
|
|
|
|
IISFIFO_WR = value;
|
2007-06-05 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
more_ready = pcm_callback_more_ready;
|
|
|
|
|
|
|
|
if (more_ready == NULL || more_ready(0) < 0) {
|
|
|
|
/* Finished recording */
|
|
|
|
pcm_rec_dma_stop();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_dma_stopped_callback();
|
2007-06-05 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2006-12-18 01:52:21 +00:00
|
|
|
void fiq_record(void)
|
|
|
|
{
|
2007-10-02 07:48:50 +00:00
|
|
|
register pcm_more_callback_type2 more_ready;
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
while (dma_rec_data.size > 0) {
|
|
|
|
if (IIS_RX_FULL_COUNT < 2) {
|
|
|
|
return;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#if SAMPLE_SIZE == 16
|
|
|
|
*dma_rec_data.p++ = IISFIFO_RD;
|
|
|
|
#elif SAMPLE_SIZE == 32
|
|
|
|
*dma_rec_data.p++ = IISFIFO_RD >> 16;
|
|
|
|
*dma_rec_data.p++ = IISFIFO_RD >> 16;
|
|
|
|
#endif
|
|
|
|
dma_rec_data.size -= 4;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2007-03-11 17:38:08 +00:00
|
|
|
|
2006-12-18 01:52:21 +00:00
|
|
|
more_ready = pcm_callback_more_ready;
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (more_ready == NULL || more_ready(0) < 0) {
|
|
|
|
/* Finished recording */
|
|
|
|
pcm_rec_dma_stop();
|
|
|
|
pcm_rec_dma_stopped_callback();
|
|
|
|
}
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#endif /* SANSA_E200 */
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2006-12-18 01:52:21 +00:00
|
|
|
/* Continue transferring data in */
|
|
|
|
void pcm_record_more(void *start, size_t size)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_peak_addr = start; /* Start peaking at dest */
|
|
|
|
dma_rec_data.p = start; /* Start of RX buffer */
|
|
|
|
dma_rec_data.size = size; /* Bytes to transfer */
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_dma_stop(void)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* disable interrupt */
|
|
|
|
IIS_IRQRX_REG &= ~IIS_IRQRX;
|
2007-03-11 17:38:08 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
dma_rec_data.state = 0;
|
|
|
|
dma_rec_data.size = 0;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
dma_rec_data.core = 0x00;
|
|
|
|
#endif
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* disable fifo */
|
|
|
|
IISCONFIG &= ~IIS_RXFIFOEN;
|
|
|
|
IISFIFO_CFG |= IIS_RXCLR;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_rec_dma_start(void *addr, size_t size)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_dma_stop();
|
2007-03-11 17:38:08 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_peak_addr = addr;
|
|
|
|
dma_rec_data.p = addr;
|
|
|
|
dma_rec_data.size = size;
|
|
|
|
#if NUM_CORES > 1
|
|
|
|
/* This will become more important later - and different ! */
|
|
|
|
dma_rec_data.core = processor_id(); /* save initiating core */
|
2007-06-05 07:03:30 +00:00
|
|
|
#endif
|
2007-10-06 22:27:27 +00:00
|
|
|
/* setup FIQ handler */
|
|
|
|
fiq_function = fiq_record;
|
2007-06-05 07:03:30 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* interrupt on full fifo, enable record fifo interrupt */
|
|
|
|
dma_rec_data.state = 1;
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* enable RX FIFO */
|
|
|
|
IISCONFIG |= IIS_RXFIFOEN;
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* enable IIS interrupt as FIQ */
|
|
|
|
CPU_INT_PRIORITY |= IIS_MASK;
|
|
|
|
CPU_INT_EN = IIS_MASK;
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_rec_dma_close(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
|
|
|
pcm_rec_dma_stop();
|
2008-09-10 23:50:26 +00:00
|
|
|
|
|
|
|
#if defined(IPOD_COLOR) || defined (IPOD_4G)
|
|
|
|
/* The usual magic from IPL - I'm guessing this configures the headphone
|
|
|
|
socket to be input or output - in this case, output. */
|
|
|
|
GPIO_SET_BITWISE(GPIOI_OUTPUT_VAL, 0x40);
|
|
|
|
GPIO_SET_BITWISE(GPIOA_OUTPUT_VAL, 0x04);
|
|
|
|
#endif
|
2006-12-18 01:52:21 +00:00
|
|
|
} /* pcm_close_recording */
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_rec_dma_init(void)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
|
|
|
#if defined(IPOD_COLOR) || defined (IPOD_4G)
|
|
|
|
/* The usual magic from IPL - I'm guessing this configures the headphone
|
|
|
|
socket to be input or output - in this case, input. */
|
2008-09-10 23:50:26 +00:00
|
|
|
GPIO_CLEAR_BITWISE(GPIOI_OUTPUT_VAL, 0x40);
|
|
|
|
GPIO_CLEAR_BITWISE(GPIOA_OUTPUT_VAL, 0x04);
|
2006-12-18 01:52:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pcm_rec_dma_stop();
|
|
|
|
} /* pcm_init */
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
const void * pcm_rec_dma_get_peak_buffer(int *count)
|
2006-12-18 01:52:21 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
unsigned long addr = (unsigned long)pcm_rec_peak_addr;
|
|
|
|
unsigned long end = (unsigned long)dma_rec_data.p;
|
|
|
|
*count = (end >> 2) - (addr >> 2);
|
|
|
|
return (void *)(addr & ~3);
|
|
|
|
} /* pcm_rec_dma_get_peak_buffer */
|
2007-06-05 07:03:30 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_RECORDING */
|