Move pcm doublebuffer to IRAM on nano 2G.
[kugel-rb.git] / firmware / target / arm / s5l8700 / pcm-s5l8700.c
blob08086c37d8c44b560a45cbaac4559a102c8fdd54
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright © 2009 Bertrik Sikken
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 <string.h>
23 #include "config.h"
24 #include "system.h"
25 #include "audio.h"
26 #include "s5l8700.h"
27 #include "panic.h"
28 #include "audiohw.h"
29 #include "pcm.h"
30 #include "pcm_sampr.h"
31 #include "dma-target.h"
32 #include "mmu-arm.h"
34 /* Driver for the IIS/PCM part of the s5l8700 using DMA
36 Notes:
37 - pcm_play_dma_pause is untested, not sure if implemented the right way
38 - pcm_play_dma_stop is untested, not sure if implemented the right way
39 - recording is not implemented
42 static volatile int locked = 0;
43 static const int zerosample = 0;
44 static unsigned char dblbuf[1024] IBSS_ATTR;
45 static const unsigned char* queuedbuf;
46 static size_t queuedsize;
47 static const unsigned char* nextbuf;
48 static size_t nextsize;
50 static const struct div_entry {
51 int pdiv, mdiv, sdiv, cdiv;
52 } div_table[HW_NUM_FREQ] = {
53 #ifdef IPOD_NANO2G
54 [HW_FREQ_11] = { 0, 41, 3, 8},
55 [HW_FREQ_22] = { 0, 41, 3, 4},
56 [HW_FREQ_44] = { 0, 41, 3, 2},
57 [HW_FREQ_88] = { 0, 41, 3, 1},
58 [HW_FREQ_8 ] = { 0, 2, 1, 9},
59 [HW_FREQ_16] = { 0, 2, 0, 9},
60 [HW_FREQ_32] = { 2, 2, 0, 9},
61 [HW_FREQ_64] = { 6, 2, 0, 9},
62 [HW_FREQ_12] = { 0, 2, 2, 3},
63 [HW_FREQ_24] = { 0, 2, 1, 3},
64 [HW_FREQ_48] = { 0, 2, 0, 3},
65 [HW_FREQ_96] = { 2, 2, 0, 3},
66 #else
67 /* table of recommended PLL/MCLK dividers for mode 256Fs from the datasheet */
68 [HW_FREQ_11] = { 26, 189, 3, 8},
69 [HW_FREQ_22] = { 50, 98, 2, 8},
70 [HW_FREQ_44] = { 37, 151, 1, 9},
71 [HW_FREQ_88] = { 50, 98, 1, 4},
72 #if 0 /* disabled because the codec driver does not support it (yet) */
73 [HW_FREQ_8 ] = { 28, 192, 3, 12},
74 [HW_FREQ_16] = { 28, 192, 3, 6},
75 [HW_FREQ_32] = { 28, 192, 2, 6},
76 [HW_FREQ_12] = { 28, 192, 3, 8},
77 [HW_FREQ_24] = { 28, 192, 2, 8},
78 [HW_FREQ_48] = { 28, 192, 2, 4},
79 [HW_FREQ_96] = { 28, 192, 1, 4},
80 #endif
81 #endif
84 /* Mask the DMA interrupt */
85 void pcm_play_lock(void)
87 if (locked++ == 0) {
88 INTMSK &= ~(1 << 10);
92 /* Unmask the DMA interrupt if enabled */
93 void pcm_play_unlock(void)
95 if (--locked == 0) {
96 INTMSK |= (1 << 10);
100 void INT_DMA(void) ICODE_ATTR;
101 void INT_DMA(void)
103 DMACOM0 = 7;
104 while (!(DMACON0 & (1 << 18)))
106 if (queuedsize)
108 memcpy(dblbuf, queuedbuf, queuedsize);
109 DMABASE0 = (unsigned int)dblbuf;
110 DMATCNT0 = queuedsize / 2 - 1;
111 queuedsize = 0;
113 else
115 if (!nextsize) pcm_play_get_more_callback((void**)&nextbuf, &nextsize);
116 if (!nextsize) break;
117 queuedsize = MIN(sizeof(dblbuf), nextsize / 2);
118 nextsize -= queuedsize;
119 queuedbuf = nextbuf + nextsize;
120 DMABASE0 = (unsigned int)nextbuf;
121 DMATCNT0 = nextsize / 2 - 1;
122 nextsize = 0;
124 clean_dcache();
125 DMACOM0 = 4;
126 DMACOM0 = 7;
130 void pcm_play_dma_start(const void* addr, size_t size)
132 /* DMA channel on */
133 nextbuf = (const unsigned char*)addr;
134 nextsize = size;
135 queuedsize = 0;
136 DMABASE0 = (unsigned int)(&zerosample);
137 DMATCNT0 = 0;
138 DMACOM0 = 4;
140 /* IIS Tx clock on */
141 I2SCLKCON = (1 << 0); /* 1 = power on */
143 /* IIS Tx on */
144 I2STXCOM = (1 << 3) | /* 1 = transmit mode on */
145 (1 << 2) | /* 1 = I2S interface enable */
146 (1 << 1) | /* 1 = DMA request enable */
147 (0 << 0); /* 0 = LRCK on */
150 void pcm_play_dma_stop(void)
152 /* DMA channel off */
153 DMACOM0 = 5;
155 /* IIS Tx off */
156 I2STXCOM = (1 << 3) | /* 1 = transmit mode on */
157 (0 << 2) | /* 1 = I2S interface enable */
158 (1 << 1) | /* 1 = DMA request enable */
159 (0 << 0); /* 0 = LRCK on */
162 /* pause playback by disabling the I2S interface */
163 void pcm_play_dma_pause(bool pause)
165 if (pause) {
166 I2STXCOM |= (1 << 0); /* LRCK off */
168 else {
169 I2STXCOM &= ~(1 << 0); /* LRCK on */
173 static void pcm_dma_set_freq(enum hw_freq_indexes idx)
175 struct div_entry div = div_table[idx];
177 /* configure PLL1 and MCLK for the desired sample rate */
178 PLL1PMS = (div.pdiv << 16) |
179 (div.mdiv << 8) |
180 (div.sdiv << 0);
181 PLL1LCNT = 280; /* 150 microseconds */
183 /* enable PLL1 and wait for lock */
184 PLLCON |= 1 << 1;
185 while ((PLLLOCK & (1 << 1)) == 0);
187 /* configure MCLK */
188 CLKCON = (CLKCON & ~(0xFF)) |
189 (0 << 7) | /* MCLK_MASK */
190 (2 << 5) | /* MCLK_SEL = PLL1 */
191 (1 << 4) | /* MCLK_DIV_ON */
192 (div.cdiv - 1); /* MCLK_DIV_VAL */
195 void pcm_play_dma_init(void)
197 /* configure IIS pins */
198 #ifdef IPOD_NANO2G
199 PCON5 = (PCON5 & ~(0xFFFF0000)) | 0x77720000;
200 PCON6 = (PCON6 & ~(0x0F000000)) | 0x02000000;
201 #else
202 PCON7 = (PCON7 & ~(0x0FFFFF00)) | 0x02222200;
203 #endif
205 /* configure DMA channel */
206 DMACON0 = (0 << 30) | /* DEVSEL */
207 (1 << 29) | /* DIR */
208 (0 << 24) | /* SCHCNT */
209 (1 << 22) | /* DSIZE */
210 (0 << 19) | /* BLEN */
211 (0 << 18) | /* RELOAD */
212 (0 << 17) | /* HCOMINT */
213 (1 << 16) | /* WCOMINT */
214 (0 << 0); /* OFFSET */
216 /* Enable the DMA IRQ */
217 INTMSK |= (1 << 10);
219 /* setup PLL */
220 pcm_dma_set_freq(HW_FREQ_44);
222 /* enable clock to the IIS module */
223 PWRCON &= ~(1 << 6);
225 /* configure IIS core */
226 #ifdef IPOD_NANO2G
227 I2STXCON = (1 << 20) | /* undocumented */
228 (0 << 16) | /* burst length */
229 (0 << 15) | /* 0 = falling edge */
230 (0 << 13) | /* 0 = basic I2S format */
231 (0 << 12) | /* 0 = MSB first */
232 (0 << 11) | /* 0 = left channel for low polarity */
233 (3 << 8) | /* MCLK divider */
234 (0 << 5) | /* 0 = 16-bit */
235 (2 << 3) | /* bit clock per frame */
236 (1 << 0); /* channel index */
237 #else
238 I2STXCON = (DMA_IISOUT_BLEN << 16) | /* burst length */
239 (0 << 15) | /* 0 = falling edge */
240 (0 << 13) | /* 0 = basic I2S format */
241 (0 << 12) | /* 0 = MSB first */
242 (0 << 11) | /* 0 = left channel for low polarity */
243 (3 << 8) | /* MCLK divider */
244 (0 << 5) | /* 0 = 16-bit */
245 (0 << 3) | /* bit clock per frame */
246 (1 << 0); /* channel index */
247 #endif
249 audiohw_preinit();
252 void pcm_postinit(void)
254 audiohw_postinit();
257 /* set the configured PCM frequency */
258 void pcm_dma_apply_settings(void)
260 pcm_dma_set_freq(pcm_fsel);
263 size_t pcm_get_bytes_waiting(void)
265 return (nextsize + DMACTCNT0 + 2) << 1;
268 const void * pcm_play_dma_get_peak_buffer(int *count)
270 *count = DMACTCNT0 >> 1;
271 return (void *)(((DMACADDR0 + 2) & ~3) | 0x40000000);
274 #ifdef HAVE_PCM_DMA_ADDRESS
275 void * pcm_dma_addr(void *addr)
277 if (addr != NULL)
278 addr = (void*)((uintptr_t)addr | 0x40000000);
279 return addr;
281 #endif
284 /****************************************************************************
285 ** Recording DMA transfer
287 #ifdef HAVE_RECORDING
288 void pcm_rec_lock(void)
292 void pcm_rec_unlock(void)
296 void pcm_rec_dma_stop(void)
300 void pcm_rec_dma_start(void *addr, size_t size)
302 (void)addr;
303 (void)size;
306 void pcm_rec_dma_close(void)
311 void pcm_rec_dma_init(void)
316 const void * pcm_rec_dma_get_peak_buffer(void)
318 return NULL;
321 #endif /* HAVE_RECORDING */