FS#11417 by Joe Balough: fix audio/tuner on philips hdd6330
[kugel-rb.git] / firmware / target / arm / pnx0101 / pcm-pnx0101.c
blob9d4ffbd773e48163c890bd02853a46d877eb8a36
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Tomek Malesinski
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"
25 #define DMA_BUF_SAMPLES 0x100
27 short __attribute__((section(".dmabuf"))) dma_buf_left[DMA_BUF_SAMPLES];
28 short __attribute__((section(".dmabuf"))) dma_buf_right[DMA_BUF_SAMPLES];
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 p = (unsigned short*)addr;
44 p_size = size;
47 void pcm_play_dma_stop(void)
51 void pcm_play_dma_pause(bool pause)
53 (void)pause;
56 static inline void fill_dma_buf(int offset)
58 short *l, *r, *lend;
60 l = dma_buf_left + offset;
61 lend = l + DMA_BUF_SAMPLES / 2;
62 r = dma_buf_right + offset;
64 if (pcm_playing && !pcm_paused)
68 int count;
69 unsigned short *tmp_p;
70 count = MIN(p_size / 4, (size_t)(lend - l));
71 tmp_p = p;
72 p_size -= count * 4;
74 if ((int)l & 3)
76 *l++ = *tmp_p++;
77 *r++ = *tmp_p++;
78 count--;
80 while (count >= 4)
82 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
83 "and r4, r0, %3\n\t"
84 "orr r4, r4, r1, lsl #16\n\t"
85 "and r5, r2, %3\n\t"
86 "orr r5, r5, r3, lsl #16\n\t"
87 "stmia %1!, {r4, r5}\n\t"
88 "bic r4, r1, %3\n\t"
89 "orr r4, r4, r0, lsr #16\n\t"
90 "bic r5, r3, %3\n\t"
91 "orr r5, r5, r2, lsr #16\n\t"
92 "stmia %2!, {r4, r5}"
93 : "+r" (tmp_p), "+r" (l), "+r" (r)
94 : "r" (0xffff)
95 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
96 count -= 4;
98 while (count > 0)
100 *l++ = *tmp_p++;
101 *r++ = *tmp_p++;
102 count--;
104 p = tmp_p;
105 if (l >= lend)
106 return;
108 pcm_play_get_more_callback((void**)&p, &p_size);
110 while (p_size);
113 if (l < lend)
115 memset(l, 0, sizeof(short) * (lend - l));
116 memset(r, 0, sizeof(short) * (lend - l));
120 static void audio_irq(void)
122 unsigned long st = DMAINTSTAT & ~DMAINTEN;
123 int i;
124 for (i = 0; i < 2; i++)
125 if (st & (1 << i))
127 fill_dma_buf((i == 1) ? 0 : DMA_BUF_SAMPLES / 2);
128 DMAINTSTAT = 1 << i;
132 unsigned long physical_address(void *p)
134 unsigned long adr = (unsigned long)p;
135 return (MMUBLOCK((adr >> 21) & 0xf) << 21) | (adr & ((1 << 21) - 1));
138 void pcm_init(void)
140 int i;
142 memset(dma_buf_left, 0, sizeof(dma_buf_left));
143 memset(dma_buf_right, 0, sizeof(dma_buf_right));
145 for (i = 0; i < 8; i++)
147 DMASRC(i) = 0;
148 DMADEST(i) = 0;
149 DMALEN(i) = 0x1ffff;
150 DMAR0C(i) = 0;
151 DMAR10(i) = 0;
152 DMAR1C(i) = 0;
155 DMAINTSTAT = 0xc000ffff;
156 DMAINTEN = 0xc000ffff;
158 DMASRC(0) = physical_address(dma_buf_left);
159 DMADEST(0) = 0x80200280;
160 DMALEN(0) = 0xff;
161 DMAR1C(0) = 0;
162 DMAR0C(0) = 0x40408;
164 DMASRC(1) = physical_address(dma_buf_right);
165 DMADEST(1) = 0x80200284;
166 DMALEN(1) = 0xff;
167 DMAR1C(1) = 0;
168 DMAR0C(1) = 0x40409;
170 irq_set_int_handler(0x1b, audio_irq);
171 irq_enable_int(0x1b);
173 DMAINTSTAT = 1;
174 DMAINTSTAT = 2;
175 DMAINTEN &= ~3;
176 DMAR10(0) |= 1;
177 DMAR10(1) |= 1;
180 void pcm_postinit(void)
182 audiohw_postinit();
185 void pcm_dma_apply_settings(void)
189 size_t pcm_get_bytes_waiting(void)
191 return p_size & ~3;
194 const void * pcm_play_dma_get_peak_buffer(int *count)
196 unsigned long addr = (unsigned long)p;
197 size_t cnt = p_size;
198 *count = cnt >> 2;
199 return (void *)((addr + 2) & ~3);