FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / target / arm / as3525 / pcm-as3525.c
blob4f9e18523d8d92f0c5161a2478b350fc2df3abc9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "system.h"
22 #include "audio.h"
23 #include "string.h"
24 #include "as3525.h"
25 #include "pl081.h"
26 #include "dma-target.h"
27 #include "clock-target.h"
28 #include "panic.h"
29 #include "as3514.h"
30 #include "audiohw.h"
31 #include "mmu-arm.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)
46 if(++locked == 1)
47 VIC_INT_EN_CLEAR = INTERRUPT_DMAC;
50 /* Unmask the DMA interrupt if enabled */
51 void pcm_play_unlock(void)
53 if(--locked == 0)
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)
62 size = MAX_TRANSFER;
64 if((unsigned int)dma_start_addr & 3)
65 panicf("unaligned pointer!");
67 dma_size -= size;
68 dma_start_addr += size;
70 CGU_PERI |= CGU_I2SOUT_APB_CLOCK_ENABLE;
71 CGU_AUDIO |= (1<<11);
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,
76 dma_callback);
79 static void dma_callback(void)
81 if(!dma_size)
83 register pcm_more_callback_type get_more = pcm_callback_for_more;
84 if(get_more)
85 get_more(&dma_start_addr, &dma_size);
88 if(!dma_size)
90 pcm_play_dma_stop();
91 pcm_play_dma_stopped_callback();
93 else
94 play_start_pcm();
97 void pcm_play_dma_start(const void *addr, size_t size)
99 dma_size = size;
100 dma_start_addr = (unsigned char*)addr;
102 dma_retain();
104 play_start_pcm();
107 void pcm_play_dma_stop(void)
109 dma_disable_channel(1);
110 dma_size = 0;
112 dma_release();
114 CGU_PERI &= ~CGU_I2SOUT_APB_CLOCK_ENABLE;
115 CGU_AUDIO &= ~(1<<11);
118 void pcm_play_dma_pause(bool pause)
120 if(pause)
121 dma_disable_channel(1);
122 else
123 play_start_pcm();
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 */
137 audiohw_preinit();
140 void pcm_postinit(void)
142 audiohw_postinit();
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)
160 return dma_size;
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)
172 if (addr != NULL)
173 addr = UNCACHED_ADDR(addr);
174 return addr;
176 #endif
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)
193 (void)start;
194 (void)size;
197 void pcm_rec_dma_stop(void)
201 void pcm_rec_dma_start(void *addr, size_t size)
203 (void)addr;
204 (void)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)
219 (void)count;
222 #endif /* HAVE_RECORDING */