1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright © 2008 Rafaël Carré
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "dma-target.h"
27 #include "clock-target.h"
33 #define MAX_TRANSFER (4*((1<<11)-1)) /* maximum data we can transfer via DMA
34 * i.e. 32 bits at once (size of I2SO_DATA)
35 * and the number of 32bits words has to
36 * fit in 11 bits of DMA register */
38 static unsigned char *dma_start_addr
;
39 static size_t dma_size
; /* in 4*32 bits */
40 static void dma_callback(void);
41 static int locked
= 0;
43 /* Mask the DMA interrupt */
44 void pcm_play_lock(void)
47 VIC_INT_EN_CLEAR
= INTERRUPT_DMAC
;
50 /* Unmask the DMA interrupt if enabled */
51 void pcm_play_unlock(void)
54 VIC_INT_ENABLE
|= INTERRUPT_DMAC
;
57 static void play_start_pcm(void)
59 const unsigned char* addr
= dma_start_addr
;
60 size_t size
= dma_size
;
61 if(size
> MAX_TRANSFER
)
64 if((unsigned int)dma_start_addr
& 3)
65 panicf("unaligned pointer!");
68 dma_start_addr
+= size
;
70 CGU_PERI
|= CGU_I2SOUT_APB_CLOCK_ENABLE
;
73 clean_dcache_range((void*)addr
, size
); /* force write back */
74 dma_enable_channel(1, (void*)addr
, (void*)I2SOUT_DATA
, DMA_PERI_I2SOUT
,
75 DMAC_FLOWCTRL_DMAC_MEM_TO_PERI
, true, false, size
>> 2, DMA_S1
,
79 static void dma_callback(void)
83 register pcm_more_callback_type get_more
= pcm_callback_for_more
;
85 get_more(&dma_start_addr
, &dma_size
);
91 pcm_play_dma_stopped_callback();
97 void pcm_play_dma_start(const void *addr
, size_t size
)
100 dma_start_addr
= (unsigned char*)addr
;
107 void pcm_play_dma_stop(void)
109 dma_disable_channel(1);
114 CGU_PERI
&= ~CGU_I2SOUT_APB_CLOCK_ENABLE
;
115 CGU_AUDIO
&= ~(1<<11);
118 void pcm_play_dma_pause(bool pause
)
121 dma_disable_channel(1);
126 void pcm_play_dma_init(void)
128 CGU_PERI
|= CGU_I2SOUT_APB_CLOCK_ENABLE
;
130 /* clock source PLLA, minimal frequency */
131 CGU_AUDIO
|= (511<<2) | (1<<0);
133 I2SOUT_CONTROL
|= (1<<6) ; /* enable dma */
134 I2SOUT_CONTROL
|= (1<<3) ; /* stereo */
135 I2SOUT_CONTROL
&= ~(1<<2); /* 16 bit samples */
140 void pcm_postinit(void)
145 void pcm_dma_apply_settings(void)
147 unsigned long frequency
= pcm_sampr
;
149 const int divider
= (((AS3525_PLLA_FREQ
/128) + (frequency
/2)) / frequency
) - 1;
150 if(divider
< 0 || divider
> 511)
151 panicf("unsupported frequency %ld", frequency
);
153 CGU_AUDIO
&= ~(((511 ^ divider
) << 2) /* I2SOUT */
154 /*| ((511 ^ divider) << 14) */ /* I2SIN */
158 size_t pcm_get_bytes_waiting(void)
163 const void * pcm_play_dma_get_peak_buffer(int *count
)
165 *count
= dma_size
>> 2;
166 return (const void*)dma_start_addr
;
169 #ifdef HAVE_PCM_DMA_ADDRESS
170 void * pcm_dma_addr(void *addr
)
173 addr
= UNCACHED_ADDR(addr
);
179 /****************************************************************************
180 ** Recording DMA transfer
182 #ifdef HAVE_RECORDING
183 void pcm_rec_lock(void)
187 void pcm_rec_unlock(void)
191 void pcm_record_more(void *start
, size_t size
)
197 void pcm_rec_dma_stop(void)
201 void pcm_rec_dma_start(void *addr
, size_t size
)
207 void pcm_rec_dma_close(void)
212 void pcm_rec_dma_init(void)
217 const void * pcm_rec_dma_get_peak_buffer(int *count
)
222 #endif /* HAVE_RECORDING */