Unify PCM interface just above the hardware driver level for all targets including...
[kugel-rb.git] / firmware / target / arm / pnx0101 / pcm-pnx0101.c
blobadfc752e8eccacea5ab2aa5bf6f6ccb3e14afe1a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Tomek Malesinski
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "system.h"
20 #include "audio.h"
21 #include "string.h"
23 #define DMA_BUF_SAMPLES 0x100
25 short __attribute__((section(".dmabuf"))) dma_buf_left[DMA_BUF_SAMPLES];
26 short __attribute__((section(".dmabuf"))) dma_buf_right[DMA_BUF_SAMPLES];
28 static int pcm_freq = HW_SAMPR_DEFAULT; /* 44.1 is default */
30 unsigned short* p IBSS_ATTR;
31 size_t p_size IBSS_ATTR;
33 void pcm_play_lock(void)
37 void pcm_play_unlock(void)
41 void pcm_play_dma_start(const void *addr, size_t size)
43 pcm_apply_settings();
45 p = (unsigned short*)addr;
46 p_size = size;
49 void pcm_play_dma_stop(void)
53 void pcm_play_dma_pause(bool pause)
55 if (!pause)
56 pcm_apply_settings();
59 static inline void fill_dma_buf(int offset)
61 short *l, *r, *lend;
63 l = dma_buf_left + offset;
64 lend = l + DMA_BUF_SAMPLES / 2;
65 r = dma_buf_right + offset;
67 if (pcm_playing && !pcm_paused)
71 int count;
72 unsigned short *tmp_p;
73 count = MIN(p_size / 4, (size_t)(lend - l));
74 tmp_p = p;
75 p_size -= count * 4;
77 if ((int)l & 3)
79 *l++ = *tmp_p++;
80 *r++ = *tmp_p++;
81 count--;
83 while (count >= 4)
85 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
86 "and r4, r0, %3\n\t"
87 "orr r4, r4, r1, lsl #16\n\t"
88 "and r5, r2, %3\n\t"
89 "orr r5, r5, r3, lsl #16\n\t"
90 "stmia %1!, {r4, r5}\n\t"
91 "bic r4, r1, %3\n\t"
92 "orr r4, r4, r0, lsr #16\n\t"
93 "bic r5, r3, %3\n\t"
94 "orr r5, r5, r2, lsr #16\n\t"
95 "stmia %2!, {r4, r5}"
96 : "+r" (tmp_p), "+r" (l), "+r" (r)
97 : "r" (0xffff)
98 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
99 count -= 4;
101 while (count > 0)
103 *l++ = *tmp_p++;
104 *r++ = *tmp_p++;
105 count--;
107 p = tmp_p;
108 if (l >= lend)
109 return;
110 else if (pcm_callback_for_more)
111 pcm_callback_for_more((unsigned char**)&p,
112 &p_size);
114 while (p_size);
116 pcm_play_dma_stopped_callback();
119 if (l < lend)
121 memset(l, 0, sizeof(short) * (lend - l));
122 memset(r, 0, sizeof(short) * (lend - l));
126 static void audio_irq(void)
128 unsigned long st = DMAINTSTAT & ~DMAINTEN;
129 int i;
130 for (i = 0; i < 2; i++)
131 if (st & (1 << i))
133 fill_dma_buf((i == 1) ? 0 : DMA_BUF_SAMPLES / 2);
134 DMAINTSTAT = 1 << i;
138 unsigned long physical_address(void *p)
140 unsigned long adr = (unsigned long)p;
141 return (MMUBLOCK((adr >> 21) & 0xf) << 21) | (adr & ((1 << 21) - 1));
144 void pcm_init(void)
146 int i;
148 pcm_set_frequency(HW_SAMPR_DEFAULT);
150 memset(dma_buf_left, 0, sizeof(dma_buf_left));
151 memset(dma_buf_right, 0, sizeof(dma_buf_right));
153 for (i = 0; i < 8; i++)
155 DMASRC(i) = 0;
156 DMADEST(i) = 0;
157 DMALEN(i) = 0x1ffff;
158 DMAR0C(i) = 0;
159 DMAR10(i) = 0;
160 DMAR1C(i) = 0;
163 DMAINTSTAT = 0xc000ffff;
164 DMAINTEN = 0xc000ffff;
166 DMASRC(0) = physical_address(dma_buf_left);
167 DMADEST(0) = 0x80200280;
168 DMALEN(0) = 0xff;
169 DMAR1C(0) = 0;
170 DMAR0C(0) = 0x40408;
172 DMASRC(1) = physical_address(dma_buf_right);
173 DMADEST(1) = 0x80200284;
174 DMALEN(1) = 0xff;
175 DMAR1C(1) = 0;
176 DMAR0C(1) = 0x40409;
178 irq_set_int_handler(0x1b, audio_irq);
179 irq_enable_int(0x1b);
181 DMAINTSTAT = 1;
182 DMAINTSTAT = 2;
183 DMAINTEN &= ~3;
184 DMAR10(0) |= 1;
185 DMAR10(1) |= 1;
188 void pcm_postinit(void)
190 audiohw_postinit();
191 pcm_apply_settings();
194 void pcm_set_frequency(unsigned int frequency)
196 (void)frequency;
197 pcm_freq = HW_SAMPR_DEFAULT;
200 void pcm_apply_settings(void)
202 pcm_curr_sampr = pcm_freq;
205 size_t pcm_get_bytes_waiting(void)
207 return p_size & ~3;
210 const void * pcm_play_dma_get_peak_buffer(int *count)
212 unsigned long addr = (unsigned long)p;
213 size_t cnt = p_size;
214 *count = cnt >> 2;
215 return (void *)((addr + 2) & ~3);