1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2006 by Michael Sevakis
* Copyright (C) 2008 by Rob Purchase
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdlib.h>
#include "system.h"
#include "kernel.h"
#include "logf.h"
#include "audio.h"
#include "sound.h"
#include "pcm.h"
struct dma_data
{
/* NOTE: The order of size and p is important if you use assembler
optimised fiq handler, so don't change it. */
uint16_t *p;
size_t size;
#if NUM_CORES > 1
unsigned core;
#endif
int locked;
int state;
};
/****************************************************************************
** Playback DMA transfer
**/
struct dma_data dma_play_data SHAREDBSS_ATTR =
{
/* Initialize to a locked, stopped state */
.p = NULL,
.size = 0,
#if NUM_CORES > 1
.core = 0x00,
#endif
.locked = 0,
.state = 0
};
void pcm_postinit(void)
{
audiohw_postinit();
}
const void * pcm_play_dma_get_peak_buffer(int *count)
{
unsigned long addr = (unsigned long)dma_play_data.p;
size_t cnt = dma_play_data.size;
*count = cnt >> 2;
return (void *)((addr + 2) & ~3);
}
void pcm_play_dma_init(void)
{
DAVC = 0x0; /* Digital Volume = max */
#ifdef COWON_D2
/* Set DAI clock divided from PLL0 (192MHz).
The best approximation of 256*44.1kHz is 11.291MHz. */
BCLKCTR &= ~DEV_DAI;
PCLK_DAI = (1<<28) | 61682; /* DCO mode */
BCLKCTR |= DEV_DAI;
/* Enable DAI block in Master mode, 256fs->32fs, 16bit LSB */
DAMR = 0x3c8e80;
#elif defined(IAUDIO_7)
BCLKCTR &= ~DEV_DAI;
PCLK_DAI = (0x800a << 16) | (PCLK_DAI & 0xffff);
BCLKCTR |= DEV_DAI;
/* Master mode, 256->64fs, 16bit LSB*/
DAMR = 0x3cce20;
#elif defined(LOGIK_DAX)
/* TODO */
#elif defined(SANSA_M200)
/* TODO */
#elif defined(SANSA_C100)
/* TODO */
#else
#error "Target isn't supported"
#endif
/* Set DAI interrupts as FIQs */
IRQSEL = ~(DAI_RX_IRQ_MASK | DAI_TX_IRQ_MASK);
/* Initialize default register values. */
audiohw_init();
dma_play_data.size = 0;
#if NUM_CORES > 1
dma_play_data.core = 0; /* no core in control */
#endif
}
void pcm_dma_apply_settings(void)
{
}
static void play_start_pcm(void)
{
DAMR &= ~(1<<14); /* disable tx */
dma_play_data.state = 1;
if (dma_play_data.size >= 16)
{
DADO_L(0) = *dma_play_data.p++;
DADO_R(0) = *dma_play_data.p++;
DADO_L(1) = *dma_play_data.p++;
DADO_R(1) = *dma_play_data.p++;
DADO_L(2) = *dma_play_data.p++;
DADO_R(2) = *dma_play_data.p++;
DADO_L(3) = *dma_play_data.p++;
DADO_R(3) = *dma_play_data.p++;
dma_play_data.size -= 16;
}
DAMR |= (1<<14); /* enable tx */
}
static void play_stop_pcm(void)
{
DAMR &= ~(1<<14); /* disable tx */
dma_play_data.state = 0;
}
void pcm_play_dma_start(const void *addr, size_t size)
{
dma_play_data.p = (void *)(((uintptr_t)addr + 2) & ~3);
dma_play_data.size = (size & ~3);
#if NUM_CORES > 1
/* This will become more important later - and different ! */
dma_play_data.core = processor_id(); /* save initiating core */
#endif
IEN |= DAI_TX_IRQ_MASK;
play_start_pcm();
}
void pcm_play_dma_stop(void)
{
play_stop_pcm();
dma_play_data.size = 0;
#if NUM_CORES > 1
dma_play_data.core = 0; /* no core in control */
#endif
}
void pcm_play_lock(void)
{
int status = disable_fiq_save();
if (++dma_play_data.locked == 1)
{
IEN &= ~DAI_TX_IRQ_MASK;
}
restore_fiq(status);
}
void pcm_play_unlock(void)
{
int status = disable_fiq_save();
if (--dma_play_data.locked == 0 && dma_play_data.state != 0)
{
IEN |= DAI_TX_IRQ_MASK;
}
restore_fiq(status);
}
void pcm_play_dma_pause(bool pause)
{
if (pause) {
play_stop_pcm();
} else {
play_start_pcm();
}
}
size_t pcm_get_bytes_waiting(void)
{
return dma_play_data.size & ~3;
}
#ifdef HAVE_RECORDING
/* TODO: implement */
void pcm_rec_dma_init(void)
{
}
void pcm_rec_dma_close(void)
{
}
void pcm_rec_dma_start(void *addr, size_t size)
{
(void) addr;
(void) size;
}
void pcm_rec_dma_stop(void)
{
}
void pcm_rec_lock(void)
{
}
void pcm_rec_unlock(void)
{
}
const void * pcm_rec_dma_get_peak_buffer(int *count)
{
*count = 0;
return NULL;
}
void pcm_record_more(void *start, size_t size)
{
(void) start;
(void) size;
}
#endif
#if defined(CPU_TCC77X) || defined(CPU_TCC780X)
void fiq_handler(void) ICODE_ATTR __attribute__((naked));
void fiq_handler(void)
{
/* r10 contains DADO_L0 base address (set in crt0.S to minimise code in the
* FIQ handler. r11 contains address of p (also set in crt0.S). Most other
* addresses we need are generated by using offsets with these two.
* r8 and r9 contains local copies of p and size respectively.
* r0-r3 and r12 is a working register.
*/
asm volatile (
#if defined(CPU_TCC780X)
"mov r8, #0xc000 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
"ldr r9, =0xf3001004 \n" /* CREQ */
#elif defined(CPU_TCC77X)
"mov r8, #0x0030 \n" /* DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK */
"ldr r9, =0x80000104 \n" /* CREQ */
#endif
"str r8, [r9] \n" /* clear DAI IRQs */
"ldmia r11, { r8-r9 } \n" /* r8 = p, r9 = size */
"cmp r9, #0x10 \n" /* is size <16? */
"blt .more_data \n" /* if so, ask pcmbuf for more data */
".fill_fifo: \n"
"ldr r12, [r8], #4 \n" /* load two samples */
"str r12, [r10, #0x0] \n" /* write top sample to DADO_L0 */
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
"str r12, [r10, #0x4] \n" /* write low sample to DADO_R0*/
"ldr r12, [r8], #4 \n" /* load two samples */
"str r12, [r10, #0x8] \n" /* write top sample to DADO_L1 */
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
"str r12, [r10, #0xc] \n" /* write low sample to DADO_R1*/
"ldr r12, [r8], #4 \n" /* load two samples */
"str r12, [r10, #0x10] \n" /* write top sample to DADO_L2 */
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
"str r12, [r10, #0x14] \n" /* write low sample to DADO_R2*/
"ldr r12, [r8], #4 \n" /* load two samples */
"str r12, [r10, #0x18] \n" /* write top sample to DADO_L3 */
"mov r12, r12, lsr #16 \n" /* put right sample at the bottom */
"str r12, [r10, #0x1c] \n" /* write low sample to DADO_R3*/
"sub r9, r9, #0x10 \n" /* 4 words written */
"stmia r11, { r8-r9 } \n" /* save p and size */
".exit: \n"
"subs pc, lr, #4 \n" /* FIQ specific return sequence */
".more_data: \n"
"stmfd sp!, { r0-r3, lr } \n" /* stack scratch regs and lr */
"ldr r2, =pcm_callback_for_more \n"
"ldr r2, [r2] \n" /* get callback address */
"cmp r2, #0 \n" /* check for null pointer */
"movne r0, r11 \n" /* r0 = &p */
"addne r1, r11, #4 \n" /* r1 = &size */
"blxne r2 \n" /* call pcm_callback_for_more */
"ldmia r11, { r8-r9 } \n" /* reload p and size */
"cmp r9, #0x10 \n" /* did we actually get more data? */
"ldmgefd sp!, { r0-r3, lr } \n"
"bge .fill_fifo \n" /* yes: fill the fifo */
"ldr r12, =pcm_play_dma_stop \n"
"blx r12 \n" /* no: stop playback */
"ldr r12, =pcm_play_dma_stopped_callback \n"
"blx r12 \n"
"ldmfd sp!, { r0-r3, lr } \n"
"b .exit \n"
".ltorg \n"
);
}
#else /* C version for reference */
void fiq_handler(void) ICODE_ATTR __attribute__((naked));
void fiq_handler(void)
{
asm volatile( "stmfd sp!, {r0-r7, ip, lr} \n" /* Store context */
"sub sp, sp, #8 \n"); /* Reserve stack */
register pcm_more_callback_type get_more;
if (dma_play_data.size < 16)
{
/* p is empty, get some more data */
get_more = pcm_callback_for_more;
if (get_more)
{
get_more((unsigned char**)&dma_play_data.p,
&dma_play_data.size);
}
}
if (dma_play_data.size >= 16)
{
DADO_L(0) = *dma_play_data.p++;
DADO_R(0) = *dma_play_data.p++;
DADO_L(1) = *dma_play_data.p++;
DADO_R(1) = *dma_play_data.p++;
DADO_L(2) = *dma_play_data.p++;
DADO_R(2) = *dma_play_data.p++;
DADO_L(3) = *dma_play_data.p++;
DADO_R(3) = *dma_play_data.p++;
dma_play_data.size -= 16;
}
else
{
/* No more data, so disable the FIFO/interrupt */
pcm_play_dma_stop();
pcm_play_dma_stopped_callback();
}
/* Clear FIQ status */
CREQ = DAI_TX_IRQ_MASK | DAI_RX_IRQ_MASK;
asm volatile( "add sp, sp, #8 \n" /* Cleanup stack */
"ldmfd sp!, {r0-r7, ip, lr} \n" /* Restore context */
"subs pc, lr, #4 \n"); /* Return from FIQ */
}
#endif
/* TODO: required by wm8731 codec */
void i2s_reset(void)
{
/* DAMR = 0; */
}
|