2006-12-29 02:49:12 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Michael Sevakis
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-05-02 22:33:24 +00:00
|
|
|
#include <stdlib.h>
|
2006-12-29 02:49:12 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "logf.h"
|
|
|
|
#include "audio.h"
|
2007-03-11 06:21:43 +00:00
|
|
|
#include "sound.h"
|
2006-12-29 02:49:12 +00:00
|
|
|
#include "file.h"
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* All exact rates for 16.9344MHz clock */
|
2007-05-02 22:33:24 +00:00
|
|
|
#define GIGABEAT_11025HZ (0x19 << 1)
|
|
|
|
#define GIGABEAT_22050HZ (0x1b << 1)
|
|
|
|
#define GIGABEAT_44100HZ (0x11 << 1)
|
|
|
|
#define GIGABEAT_88200HZ (0x1f << 1)
|
2007-01-18 13:48:06 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* PCM interrupt routine lockout */
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
int locked;
|
|
|
|
unsigned long state;
|
|
|
|
} dma_play_lock =
|
|
|
|
{
|
|
|
|
.locked = 0,
|
2008-03-31 01:29:50 +00:00
|
|
|
.state = 0,
|
2007-10-06 22:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Last samplerate set by pcm_set_frequency */
|
|
|
|
static unsigned long pcm_freq = 0; /* 44.1 is default */
|
|
|
|
/* Samplerate control for audio codec */
|
2007-05-03 18:08:00 +00:00
|
|
|
static int sr_ctrl = 0;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
#define FIFO_COUNT ((IISFCON >> 6) & 0x3F)
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
/* Setup for the DMA controller */
|
|
|
|
#define DMA_CONTROL_SETUP ((1<<31) | (1<<29) | (1<<23) | (1<<22) | (1<<20))
|
|
|
|
|
|
|
|
/* DMA count has hit zero - no more data */
|
|
|
|
/* Get more data from the callback and top off the FIFO */
|
2007-10-06 22:27:27 +00:00
|
|
|
void fiq_handler(void) __attribute__((interrupt ("FIQ")));
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-05-02 22:33:24 +00:00
|
|
|
static void _pcm_apply_settings(void)
|
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
if (pcm_freq != pcm_curr_sampr)
|
2006-12-29 02:49:12 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_curr_sampr = pcm_freq;
|
2007-05-02 22:33:24 +00:00
|
|
|
audiohw_set_frequency(sr_ctrl);
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 22:33:24 +00:00
|
|
|
void pcm_apply_settings(void)
|
|
|
|
{
|
2008-03-26 01:50:41 +00:00
|
|
|
int status = disable_fiq_save();
|
2007-05-02 22:33:24 +00:00
|
|
|
_pcm_apply_settings();
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_fiq(status);
|
2007-05-02 22:33:24 +00:00
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2008-03-31 01:29:50 +00:00
|
|
|
/* Mask the DMA interrupt */
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_lock(void)
|
|
|
|
{
|
|
|
|
if (++dma_play_lock.locked == 1)
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regset(&INTMSK, DMA2_MASK);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2008-03-31 01:29:50 +00:00
|
|
|
/* Unmask the DMA interrupt if enabled */
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_unlock(void)
|
2006-12-29 02:49:12 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
if (--dma_play_lock.locked == 0)
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regclr(&INTMSK, dma_play_lock.state);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
2007-01-04 11:36:25 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_init(void)
|
|
|
|
{
|
2007-01-16 03:36:32 +00:00
|
|
|
pcm_set_frequency(SAMPR_44);
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2008-05-10 19:43:23 +00:00
|
|
|
/* There seem to be problems when changing the IIS interface configuration
|
|
|
|
* when a clock is not present.
|
|
|
|
*/
|
|
|
|
s3c_regset(&CLKCON, 1<<17);
|
|
|
|
/* slave, transmit mode, 16 bit samples - MCLK 384fs - use 16.9344Mhz -
|
|
|
|
BCLK 32fs */
|
|
|
|
IISMOD = (1<<9) | (1<<8) | (2<<6) | (1<<3) | (1<<2) | (1<<0);
|
2007-06-24 20:41:27 +00:00
|
|
|
|
2008-05-10 19:43:23 +00:00
|
|
|
/* RX,TX off,on */
|
2007-10-06 22:27:27 +00:00
|
|
|
IISCON |= (1<<3) | (1<<2);
|
|
|
|
|
2008-05-10 19:43:23 +00:00
|
|
|
s3c_regclr(&CLKCON, 1<<17);
|
|
|
|
|
2007-05-03 18:08:00 +00:00
|
|
|
audiohw_init();
|
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
/* init GPIO */
|
|
|
|
GPCCON = (GPCCON & ~(3<<14)) | (1<<14);
|
2007-10-06 22:27:27 +00:00
|
|
|
GPCDAT |= (1<<7);
|
|
|
|
/* GPE4=I2SDO, GPE3=I2SDI, GPE2=CDCLK, GPE1=I2SSCLK, GPE0=I2SLRCK */
|
|
|
|
GPECON = (GPECON & ~0x3ff) | 0x2aa;
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-01-16 03:36:32 +00:00
|
|
|
/* Do not service DMA requests, yet */
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
/* clear any pending int and mask it */
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regset(&INTMSK, DMA2_MASK);
|
|
|
|
SRCPND = DMA2_MASK;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
/* connect to FIQ */
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regset(&INTMOD, DMA2_MASK);
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
|
|
|
|
2007-03-11 06:21:43 +00:00
|
|
|
void pcm_postinit(void)
|
|
|
|
{
|
|
|
|
audiohw_postinit();
|
2007-05-03 18:08:00 +00:00
|
|
|
pcm_apply_settings();
|
2007-03-11 06:21:43 +00:00
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Connect the DMA and start filling the FIFO */
|
|
|
|
static void play_start_pcm(void)
|
|
|
|
{
|
|
|
|
/* clear pending DMA interrupt */
|
2008-03-31 01:29:50 +00:00
|
|
|
SRCPND = DMA2_MASK;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
_pcm_apply_settings();
|
|
|
|
|
|
|
|
/* Flush any pending writes */
|
|
|
|
clean_dcache_range((void*)DISRC2, (DCON2 & 0xFFFFF) * 2);
|
|
|
|
|
|
|
|
/* unmask DMA interrupt when unlocking */
|
2008-03-31 01:29:50 +00:00
|
|
|
dma_play_lock.state = DMA2_MASK;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
/* turn on the request */
|
|
|
|
IISCON |= (1<<5);
|
|
|
|
|
|
|
|
/* Activate the channel */
|
|
|
|
DMASKTRIG2 = 0x2;
|
|
|
|
|
|
|
|
/* turn off the idle */
|
|
|
|
IISCON &= ~(1<<3);
|
|
|
|
|
|
|
|
/* start the IIS */
|
|
|
|
IISCON |= (1<<0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disconnect the DMA and wait for the FIFO to clear */
|
|
|
|
static void play_stop_pcm(void)
|
2006-12-29 02:49:12 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Mask DMA interrupt */
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regset(&INTMSK, DMA2_MASK);
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
/* De-Activate the DMA channel */
|
|
|
|
DMASKTRIG2 = 0x4;
|
|
|
|
|
|
|
|
/* are we playing? wait for the chunk to finish */
|
|
|
|
if (dma_play_lock.state != 0)
|
|
|
|
{
|
|
|
|
/* wait for the FIFO to empty and DMA to stop */
|
|
|
|
while ((IISCON & (1<<7)) || (DMASKTRIG2 & 0x2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep interrupt masked when unlocking */
|
|
|
|
dma_play_lock.state = 0;
|
|
|
|
|
|
|
|
/* turn off the request */
|
|
|
|
IISCON &= ~(1<<5);
|
2007-05-03 12:39:36 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* turn on the idle */
|
|
|
|
IISCON |= (1<<3);
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* stop the IIS */
|
|
|
|
IISCON &= ~(1<<0);
|
|
|
|
}
|
2007-05-02 22:33:24 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_start(const void *addr, size_t size)
|
|
|
|
{
|
2006-12-29 02:49:12 +00:00
|
|
|
/* Enable the IIS clock */
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regset(&CLKCON, 1<<17);
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* stop any DMA in progress - idle IIS */
|
|
|
|
play_stop_pcm();
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
/* connect DMA to the FIFO and enable the FIFO */
|
|
|
|
IISFCON = (1<<15) | (1<<13);
|
|
|
|
|
|
|
|
/* set DMA dest */
|
2007-10-06 22:27:27 +00:00
|
|
|
DIDST2 = (unsigned int)&IISFIFO;
|
2006-12-29 02:49:12 +00:00
|
|
|
|
|
|
|
/* IIS is on the APB bus, INT when TC reaches 0, fixed dest addr */
|
|
|
|
DIDSTC2 = 0x03;
|
2007-01-04 11:36:25 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* set DMA source and options */
|
|
|
|
DISRC2 = (unsigned int)addr + 0x30000000;
|
2006-12-29 02:49:12 +00:00
|
|
|
/* How many transfers to make - we transfer half-word at a time = 2 bytes */
|
|
|
|
/* DMA control: CURR_TC int, single service mode, I2SSDO int, HW trig */
|
|
|
|
/* no auto-reload, half-word (16bit) */
|
2007-05-03 12:39:36 +00:00
|
|
|
DCON2 = DMA_CONTROL_SETUP | (size / 2);
|
2006-12-29 02:49:12 +00:00
|
|
|
DISRCC2 = 0x00; /* memory is on AHB bus, increment addresses */
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
play_start_pcm();
|
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Promptly stop DMA transfers and stop IIS */
|
|
|
|
void pcm_play_dma_stop(void)
|
|
|
|
{
|
|
|
|
play_stop_pcm();
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Disconnect the IIS clock */
|
2008-03-31 01:29:50 +00:00
|
|
|
s3c_regclr(&CLKCON, 1<<17);
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_dma_pause(bool pause)
|
2006-12-29 02:49:12 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
if (pause)
|
2007-01-16 03:36:32 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* pause playback on current buffer */
|
|
|
|
play_stop_pcm();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* restart playback on current buffer */
|
|
|
|
/* make sure we're aligned on left channel - skip any right
|
|
|
|
channel sample left waiting */
|
|
|
|
DISRC2 = (DCSRC2 + 2) & ~0x3;
|
|
|
|
DCON2 = DMA_CONTROL_SETUP | (DSTAT2 & 0xFFFFE);
|
|
|
|
play_start_pcm();
|
2007-01-16 03:36:32 +00:00
|
|
|
}
|
2007-05-02 22:33:24 +00:00
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-05-03 12:39:36 +00:00
|
|
|
void fiq_handler(void)
|
2007-05-02 22:33:24 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
static unsigned char *start;
|
|
|
|
static size_t size;
|
2007-05-03 12:39:36 +00:00
|
|
|
register pcm_more_callback_type get_more; /* No stack for this */
|
|
|
|
|
2007-05-02 22:33:24 +00:00
|
|
|
/* clear any pending interrupt */
|
2008-03-31 01:29:50 +00:00
|
|
|
SRCPND = DMA2_MASK;
|
2007-05-02 22:33:24 +00:00
|
|
|
|
|
|
|
/* Buffer empty. Try to get more. */
|
2007-05-03 12:39:36 +00:00
|
|
|
get_more = pcm_callback_for_more;
|
2007-10-06 22:27:27 +00:00
|
|
|
size = 0;
|
2007-05-02 22:33:24 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (get_more == NULL || (get_more(&start, &size), size == 0))
|
2007-05-02 22:33:24 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Callback missing or no more DMA to do */
|
|
|
|
pcm_play_dma_stop();
|
|
|
|
pcm_play_dma_stopped_callback();
|
2007-05-02 22:33:24 +00:00
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
else
|
2007-06-24 20:41:27 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Flush any pending cache writes */
|
|
|
|
clean_dcache_range(start, size);
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* set the new DMA values */
|
|
|
|
DCON2 = DMA_CONTROL_SETUP | (size >> 1);
|
|
|
|
DISRC2 = (unsigned int)start + 0x30000000;
|
2006-12-29 02:49:12 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Re-Activate the channel */
|
|
|
|
DMASKTRIG2 = 0x2;
|
2007-06-24 20:41:27 +00:00
|
|
|
}
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_set_frequency(unsigned int frequency)
|
|
|
|
{
|
|
|
|
switch(frequency)
|
|
|
|
{
|
|
|
|
case SAMPR_11:
|
2007-01-18 13:48:06 +00:00
|
|
|
sr_ctrl = GIGABEAT_11025HZ;
|
|
|
|
break;
|
2006-12-29 02:49:12 +00:00
|
|
|
case SAMPR_22:
|
2007-01-18 13:48:06 +00:00
|
|
|
sr_ctrl = GIGABEAT_22050HZ;
|
|
|
|
break;
|
2006-12-29 02:49:12 +00:00
|
|
|
default:
|
2007-01-18 13:48:06 +00:00
|
|
|
frequency = SAMPR_44;
|
2006-12-29 02:49:12 +00:00
|
|
|
case SAMPR_44:
|
2007-01-18 13:48:06 +00:00
|
|
|
sr_ctrl = GIGABEAT_44100HZ;
|
|
|
|
break;
|
2006-12-29 02:49:12 +00:00
|
|
|
case SAMPR_88:
|
2007-01-18 13:48:06 +00:00
|
|
|
sr_ctrl = GIGABEAT_88200HZ;
|
|
|
|
break;
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
2007-01-18 13:48:06 +00:00
|
|
|
|
2006-12-29 02:49:12 +00:00
|
|
|
pcm_freq = frequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pcm_get_bytes_waiting(void)
|
|
|
|
{
|
2007-06-24 20:41:27 +00:00
|
|
|
/* lie a little and only return full pairs */
|
|
|
|
return (DSTAT2 & 0xFFFFE) * 2;
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
const void * pcm_play_dma_get_peak_buffer(int *count)
|
2006-12-29 02:49:12 +00:00
|
|
|
{
|
2007-10-06 22:27:27 +00:00
|
|
|
unsigned long addr = DCSRC2;
|
|
|
|
int cnt = DSTAT2;
|
|
|
|
*count = (cnt & 0xFFFFF) >> 1;
|
|
|
|
return (void *)((addr + 2) & ~3);
|
2006-12-29 02:49:12 +00:00
|
|
|
}
|