Speed up the Fuze's LCD by a factor of ~2.1 (lcd_update()) by using 32bit transfers...
[kugel-rb.git] / firmware / target / arm / pcm-telechips.c
blobe172e622695e093790f89777e5270e59a66e7c30
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdlib.h>
23 #include "system.h"
24 #include "kernel.h"
25 #include "logf.h"
26 #include "audio.h"
27 #include "sound.h"
28 #include "pcm.h"
30 /* Just for tests enable it to play simple tone */
31 //#define PCM_TELECHIPS_TEST
33 struct dma_data
35 /* NOTE: The order of size and p is important if you use assembler
36 optimised fiq handler, so don't change it. */
37 uint16_t *p;
38 size_t size;
39 #if NUM_CORES > 1
40 unsigned core;
41 #endif
42 int locked;
43 int state;
46 /****************************************************************************
47 ** Playback DMA transfer
48 **/
49 struct dma_data dma_play_data SHAREDBSS_ATTR =
51 /* Initialize to a locked, stopped state */
52 .p = NULL,
53 .size = 0,
54 #if NUM_CORES > 1
55 .core = 0x00,
56 #endif
57 .locked = 0,
58 .state = 0
61 void pcm_postinit(void)
63 audiohw_postinit();
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;
70 *count = cnt >> 2;
71 return (void *)((addr + 2) & ~3);
74 void pcm_play_dma_init(void)
76 DAVC = 0x0; /* Digital Volume = max */
77 #ifdef COWON_D2
78 /* Set DAI clock divided from PLL0 (192MHz).
79 The best approximation of 256*44.1kHz is 11.291MHz. */
80 BCLKCTR &= ~DEV_DAI;
81 PCLK_DAI = (1<<28) | 61682; /* DCO mode */
82 BCLKCTR |= DEV_DAI;
84 /* Enable DAI block in Master mode, 256fs->32fs, 16bit LSB */
85 DAMR = 0x3c8e80;
86 #elif defined(IAUDIO_7)
87 BCLKCTR &= ~DEV_DAI;
88 PCLK_DAI = (0x800b << 16) | (PCLK_DAI & 0xffff);
89 BCLKCTR |= DEV_DAI;
90 /* Master mode, 256->64fs, 16bit LSB*/
91 DAMR = 0x3cce20;
92 #elif defined(LOGIK_DAX)
93 /* TODO */
94 #elif defined(SANSA_M200)
95 /* TODO */
96 #elif defined(SANSA_C100)
97 /* TODO */
98 #else
99 #error "Target isn't supported"
100 #endif
101 /* Set DAI interrupts as FIQs */
102 IRQSEL = ~(DAI_RX_IRQ_MASK | DAI_TX_IRQ_MASK);
104 /* Initialize default register values. */
105 audiohw_init();
107 dma_play_data.size = 0;
108 #if NUM_CORES > 1
109 dma_play_data.core = 0; /* no core in control */
110 #endif
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);
149 #if NUM_CORES > 1
150 /* This will become more important later - and different ! */
151 dma_play_data.core = processor_id(); /* save initiating core */
152 #endif
154 IEN |= DAI_TX_IRQ_MASK;
156 play_start_pcm();
159 void pcm_play_dma_stop(void)
161 play_stop_pcm();
162 dma_play_data.size = 0;
163 #if NUM_CORES > 1
164 dma_play_data.core = 0; /* no core in control */
165 #endif
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;
177 restore_fiq(status);
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;
189 restore_fiq(status);
192 void pcm_play_dma_pause(bool pause)
194 if (pause) {
195 play_stop_pcm();
196 } else {
197 play_start_pcm();
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)
218 (void) addr;
219 (void) 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)
236 *count = 0;
237 return NULL;
240 void pcm_record_more(void *start, size_t size)
242 (void) start;
243 (void) size;
245 #endif
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.
257 asm volatile (
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 */
264 #endif
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 */
270 ".fill_fifo: \n"
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 */
290 ".exit: \n"
291 "subs pc, lr, #4 \n" /* FIQ specific return sequence */
293 ".more_data: \n"
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"
308 "blx r12 \n"
309 "ldmfd sp!, { r0-r3, lr } \n"
310 "b .exit \n"
311 ".ltorg \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;
327 if (get_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;
347 else
349 /* No more data, so disable the FIFO/interrupt */
350 pcm_play_dma_stop();
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 */
361 #endif
363 /* TODO: required by wm8531 codec, why not to implement */
364 void i2s_reset(void)
366 /* DAMR = 0; */
369 #ifdef PCM_TELECHIPS_TEST
370 #include "lcd.h"
371 #include "sprintf.h"
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];
378 static int cntr = 0;
379 int i;
381 for (i = 0; i < 8; i ++) {
382 unsigned short val;
384 if (0x100 == (cntr & 0x100))
385 val = 0x0fff;
386 else
387 val = 0x0000;
388 data[i] = val;
389 cntr++;
392 *start = data;
393 *size = sizeof(data);
395 frame++;
398 void pcm_telechips_test(void)
400 static char buf[100];
401 unsigned char *data;
402 size_t size;
404 _backlight_on();
406 audiohw_preinit();
407 pcm_play_dma_init();
408 pcm_postinit();
410 audiohw_mute(false);
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);
417 while (1) {
418 int line = 0;
419 lcd_clear_display();
420 lcd_puts(0, line++, __func__);
421 snprintf(buf, sizeof(buf), "frame: %d", frame);
422 lcd_puts(0, line++, buf);
423 lcd_update();
424 sleep(1);
427 #endif