1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Michael Sevakis
11 * Copyright (C) 2008 by Rob Purchase
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
30 /* Just for tests enable it to play simple tone */
31 //#define PCM_TELECHIPS_TEST
35 /* NOTE: The order of size and p is important if you use assembler
36 optimised fiq handler, so don't change it. */
46 /****************************************************************************
47 ** Playback DMA transfer
49 struct dma_data dma_play_data SHAREDBSS_ATTR
=
51 /* Initialize to a locked, stopped state */
61 void pcm_postinit(void)
66 const void * pcm_play_dma_get_peak_buffer(int *count
)
68 unsigned long addr
= (unsigned long)dma_play_data
.p
;
69 size_t cnt
= dma_play_data
.size
;
71 return (void *)((addr
+ 2) & ~3);
74 void pcm_play_dma_init(void)
76 DAVC
= 0x0; /* Digital Volume = max */
78 /* Set DAI clock divided from PLL0 (192MHz).
79 The best approximation of 256*44.1kHz is 11.291MHz. */
81 PCLK_DAI
= (1<<28) | 61682; /* DCO mode */
84 /* Enable DAI block in Master mode, 256fs->32fs, 16bit LSB */
86 #elif defined(IAUDIO_7)
88 PCLK_DAI
= (0x800b << 16) | (PCLK_DAI
& 0xffff);
90 /* Master mode, 256->64fs, 16bit LSB*/
92 #elif defined(LOGIK_DAX)
94 #elif defined(SANSA_M200)
96 #elif defined(SANSA_C100)
99 #error "Target isn't supported"
101 /* Set DAI interrupts as FIQs */
102 IRQSEL
= ~(DAI_RX_IRQ_MASK
| DAI_TX_IRQ_MASK
);
104 /* Initialize default register values. */
107 dma_play_data
.size
= 0;
109 dma_play_data
.core
= 0; /* no core in control */
113 void pcm_dma_apply_settings(void)
117 static void play_start_pcm(void)
119 DAMR
&= ~(1<<14); /* disable tx */
120 dma_play_data
.state
= 1;
122 if (dma_play_data
.size
>= 16)
124 DADO_L(0) = *dma_play_data
.p
++;
125 DADO_R(0) = *dma_play_data
.p
++;
126 DADO_L(1) = *dma_play_data
.p
++;
127 DADO_R(1) = *dma_play_data
.p
++;
128 DADO_L(2) = *dma_play_data
.p
++;
129 DADO_R(2) = *dma_play_data
.p
++;
130 DADO_L(3) = *dma_play_data
.p
++;
131 DADO_R(3) = *dma_play_data
.p
++;
132 dma_play_data
.size
-= 16;
135 DAMR
|= (1<<14); /* enable tx */
138 static void play_stop_pcm(void)
140 DAMR
&= ~(1<<14); /* disable tx */
141 dma_play_data
.state
= 0;
144 void pcm_play_dma_start(const void *addr
, size_t size
)
146 dma_play_data
.p
= (void *)(((uintptr_t)addr
+ 2) & ~3);
147 dma_play_data
.size
= (size
& ~3);
150 /* This will become more important later - and different ! */
151 dma_play_data
.core
= processor_id(); /* save initiating core */
154 IEN
|= DAI_TX_IRQ_MASK
;
159 void pcm_play_dma_stop(void)
162 dma_play_data
.size
= 0;
164 dma_play_data
.core
= 0; /* no core in control */
168 void pcm_play_lock(void)
170 int status
= disable_fiq_save();
172 if (++dma_play_data
.locked
== 1)
174 IEN
&= ~DAI_TX_IRQ_MASK
;
180 void pcm_play_unlock(void)
182 int status
= disable_fiq_save();
184 if (--dma_play_data
.locked
== 0 && dma_play_data
.state
!= 0)
186 IEN
|= DAI_TX_IRQ_MASK
;
192 void pcm_play_dma_pause(bool pause
)
201 size_t pcm_get_bytes_waiting(void)
203 return dma_play_data
.size
& ~3;
206 #ifdef HAVE_RECORDING
207 /* TODO: implement */
208 void pcm_rec_dma_init(void)
212 void pcm_rec_dma_close(void)
216 void pcm_rec_dma_start(void *addr
, size_t size
)
222 void pcm_rec_dma_stop(void)
226 void pcm_rec_lock(void)
230 void pcm_rec_unlock(void)
234 const void * pcm_rec_dma_get_peak_buffer(int *count
)
240 void pcm_record_more(void *start
, size_t size
)
247 #if defined(CPU_TCC77X) || defined(CPU_TCC780X)
248 void fiq_handler(void) ICODE_ATTR
__attribute__((naked
));
249 void fiq_handler(void)
251 /* r10 contains DADO_L0 base address (set in crt0.S to minimise code in the
252 * FIQ handler. r11 contains address of p (also set in crt0.S). Most other
253 * addresses we need are generated by using offsets with these two.
254 * r8 and r9 contains local copies of p and size respectively.
255 * r0-r3 and r12 is a working register.
258 #if defined(CPU_TCC780X)
259 "mov r8, #0xc000 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
260 "ldr r9, =0xf3001004 \n" /* CREQ */
261 #elif defined(CPU_TCC77X)
262 "mov r8, #0x0030 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
263 "ldr r9, =0x80000104 \n" /* CREQ */
265 "str r8, [r9] \n" /* clear DAI IRQs */
266 "ldmia r11, { r8-r9 } \n" /* r8 = p, r9 = size */
267 "cmp r9, #0x10 \n" /* is size <16? */
268 "blt .more_data \n" /* if so, ask pcmbuf for more data */
271 "ldr r12, [r8], #4 \n" /* load two samples */
272 "str r12, [r10, #0x0] \n" /* write top sample to DADO_L0 */
273 "mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
274 "str r12, [r10, #0x4] \n" /* write low sample to DADO_R0*/
275 "ldr r12, [r8], #4 \n" /* load two samples */
276 "str r12, [r10, #0x8] \n" /* write top sample to DADO_L1 */
277 "mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
278 "str r12, [r10, #0xc] \n" /* write low sample to DADO_R1*/
279 "ldr r12, [r8], #4 \n" /* load two samples */
280 "str r12, [r10, #0x10] \n" /* write top sample to DADO_L2 */
281 "mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
282 "str r12, [r10, #0x14] \n" /* write low sample to DADO_R2*/
283 "ldr r12, [r8], #4 \n" /* load two samples */
284 "str r12, [r10, #0x18] \n" /* write top sample to DADO_L3 */
285 "mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
286 "str r12, [r10, #0x1c] \n" /* write low sample to DADO_R3*/
287 "sub r9, r9, #0x10 \n" /* 4 words written */
288 "stmia r11, { r8-r9 } \n" /* save p and size */
291 "subs pc, lr, #4 \n" /* FIQ specific return sequence */
294 "stmfd sp!, { r0-r3, lr } \n" /* stack scratch regs and lr */
295 "ldr r2, =pcm_callback_for_more \n"
296 "ldr r2, [r2] \n" /* get callback address */
297 "cmp r2, #0 \n" /* check for null pointer */
298 "movne r0, r11 \n" /* r0 = &p */
299 "addne r1, r11, #4 \n" /* r1 = &size */
300 "blxne r2 \n" /* call pcm_callback_for_more */
301 "ldmia r11, { r8-r9 } \n" /* reload p and size */
302 "cmp r9, #0x10 \n" /* did we actually get more data? */
303 "ldmgefd sp!, { r0-r3, lr } \n"
304 "bge .fill_fifo \n" /* yes: fill the fifo */
305 "ldr r12, =pcm_play_dma_stop \n"
306 "blx r12 \n" /* no: stop playback */
307 "ldr r12, =pcm_play_dma_stopped_callback \n"
309 "ldmfd sp!, { r0-r3, lr } \n"
314 #else /* C version for reference */
315 void fiq_handler(void) ICODE_ATTR
__attribute__((naked
));
316 void fiq_handler(void)
318 asm volatile( "stmfd sp!, {r0-r7, ip, lr} \n" /* Store context */
319 "sub sp, sp, #8 \n"); /* Reserve stack */
321 register pcm_more_callback_type get_more
;
323 if (dma_play_data
.size
< 16)
325 /* p is empty, get some more data */
326 get_more
= pcm_callback_for_more
;
329 get_more((unsigned char**)&dma_play_data
.p
,
330 &dma_play_data
.size
);
334 if (dma_play_data
.size
>= 16)
336 DADO_L(0) = *dma_play_data
.p
++;
337 DADO_R(0) = *dma_play_data
.p
++;
338 DADO_L(1) = *dma_play_data
.p
++;
339 DADO_R(1) = *dma_play_data
.p
++;
340 DADO_L(2) = *dma_play_data
.p
++;
341 DADO_R(2) = *dma_play_data
.p
++;
342 DADO_L(3) = *dma_play_data
.p
++;
343 DADO_R(3) = *dma_play_data
.p
++;
345 dma_play_data
.size
-= 16;
349 /* No more data, so disable the FIFO/interrupt */
351 pcm_play_dma_stopped_callback();
354 /* Clear FIQ status */
355 CREQ
= DAI_TX_IRQ_MASK
| DAI_RX_IRQ_MASK
;
357 asm volatile( "add sp, sp, #8 \n" /* Cleanup stack */
358 "ldmfd sp!, {r0-r7, ip, lr} \n" /* Restore context */
359 "subs pc, lr, #4 \n"); /* Return from FIQ */
363 /* TODO: required by wm8531 codec, why not to implement */
369 #ifdef PCM_TELECHIPS_TEST
372 #include "backlight-target.h"
374 static int frame
= 0;
375 static void test_callback_for_more(unsigned char **start
, size_t *size
)
377 static unsigned short data
[8];
381 for (i
= 0; i
< 8; i
++) {
384 if (0x100 == (cntr
& 0x100))
393 *size
= sizeof(data
);
398 void pcm_telechips_test(void)
400 static char buf
[100];
411 audiohw_set_master_vol(0x7f, 0x7f);
413 pcm_callback_for_more
= test_callback_for_more
;
414 test_callback_for_more(&data
, &size
);
415 pcm_play_dma_start(data
, size
);
420 lcd_puts(0, line
++, __func__
);
421 snprintf(buf
, sizeof(buf
), "frame: %d", frame
);
422 lcd_puts(0, line
++, buf
);