-
Notifications
You must be signed in to change notification settings - Fork 41
/
snd.c
347 lines (280 loc) · 7.61 KB
/
snd.c
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
/*
* Milkymist SoC (Software)
* Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
*
* 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, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <irq.h>
#include <system.h>
#include <hw/ac97.h>
#include <hw/interrupts.h>
#include <hw/sysctl.h>
#include <hw/capabilities.h>
#include <hal/snd.h>
/****************************************************************/
/* GENERAL */
/****************************************************************/
/*
* We use interrupts and busy wait on those variables
* (which end up in the data cache) rather than
* busy waiting on the CSR, which would use bus bandwidth
* for nothing.
*/
static volatile int snd_cr_request;
static volatile int snd_cr_reply;
void snd_init()
{
unsigned int codec_id;
unsigned int mask;
if(!(CSR_CAPABILITIES & CAP_AC97)) {
printf("SND: not supported by SoC, giving up.\n");
return;
}
snd_cr_request = 0;
snd_cr_reply = 0;
/* Reset AC'97 controller */
CSR_AC97_CRCTL = 0;
CSR_AC97_DCTL = 0;
CSR_AC97_UCTL = 0;
irq_ack(IRQ_AC97CRREQUEST|IRQ_AC97CRREPLY|IRQ_AC97DMAR|IRQ_AC97DMAW);
mask = irq_getmask();
mask |= IRQ_AC97CRREQUEST|IRQ_AC97CRREPLY|IRQ_AC97DMAR|IRQ_AC97DMAW;
irq_setmask(mask);
codec_id = snd_ac97_read(0x00);
if(codec_id == 0x0d50)
printf("SND: found LM4550 AC'97 codec\n");
else if(codec_id == 0x6150)
printf("SND: found WM9707 AC'97 codec\n");
else
printf("SND: warning, unknown codec found (ID:%04x)\n", codec_id);
/* Unmute and set volumes */
/* TODO: API for this */
snd_ac97_write(0x02, 0x0000); /* master volume */
snd_ac97_write(0x04, 0x0f0f); /* headphones volume */
snd_ac97_write(0x18, 0x0000); /* PCM out volume */
snd_ac97_write(0x1c, 0x0f0f); /* record gain */
snd_ac97_write(0x0e, 0x0000); /* mic volume: max */
snd_ac97_write(0x10, 0x0000); /* line in volume: max */
snd_ac97_write(0x1a, 0x0505); /* record select: stero mix */
snd_play_empty();
snd_record_empty();
printf("SND: initialization complete\n");
}
void snd_isr_crrequest()
{
snd_cr_request = 1;
irq_ack(IRQ_AC97CRREQUEST);
}
void snd_isr_crreply()
{
snd_cr_reply = 1;
irq_ack(IRQ_AC97CRREPLY);
}
unsigned int snd_ac97_read(unsigned int addr)
{
CSR_AC97_CRADDR = addr;
snd_cr_reply = 0;
CSR_AC97_CRCTL = AC97_CRCTL_RQEN;
while(!snd_cr_reply);
return CSR_AC97_CRDATAIN;
}
void snd_ac97_write(unsigned int addr, unsigned int value)
{
CSR_AC97_CRADDR = addr;
CSR_AC97_CRDATAOUT = value;
snd_cr_request = 0;
CSR_AC97_CRCTL = AC97_CRCTL_RQEN|AC97_CRCTL_WRITE;
while(!snd_cr_request);
}
/****************************************************************/
/* PLAYING */
/****************************************************************/
static snd_callback play_callback;
static void *play_user;
static unsigned int play_nbytes;
#define PLAY_BUFQ_SIZE 4 /* < must be a power of 2 */
#define PLAY_BUFQ_MASK (PLAY_BUFQ_SIZE-1)
static short *play_queue[PLAY_BUFQ_SIZE];
static unsigned int play_produce;
static unsigned int play_consume;
static unsigned int play_level;
static int play_underrun;
static void play_start(short *buffer)
{
CSR_AC97_DADDRESS = (unsigned int)buffer;
CSR_AC97_DREMAINING = play_nbytes;
CSR_AC97_DCTL = AC97_SCTL_EN;
}
void snd_isr_dmar()
{
/* NB. the callback can give us buffers by calling snd_play_refill() */
play_callback(play_queue[play_consume], play_user);
play_consume = (play_consume + 1) & PLAY_BUFQ_MASK;
play_level--;
irq_ack(IRQ_AC97DMAR);
if(play_level > 0)
play_start(play_queue[play_consume]);
else {
printf("SND: playing underrun\n");
play_underrun = 1;
}
}
void snd_play_empty()
{
play_produce = 0;
play_consume = 0;
play_level = 0;
play_underrun = 0;
}
int snd_play_refill(short *buffer)
{
unsigned int oldmask;
oldmask = irq_getmask();
irq_setmask(0);
if(play_level >= PLAY_BUFQ_SIZE) {
irq_setmask(oldmask);
printf("SND: play bufq overflow\n");
return 0;
}
play_queue[play_produce] = buffer;
play_produce = (play_produce + 1) & PLAY_BUFQ_MASK;
play_level++;
if(play_underrun) {
play_underrun = 0;
play_start(buffer);
}
irq_setmask(oldmask);
return 1;
}
void snd_play_start(snd_callback callback, unsigned int nsamples, void *user)
{
if(snd_play_active()) {
printf("SND: snd_play_start called while already playing\n");
return;
}
play_callback = callback;
play_user = user;
play_nbytes = nsamples*4;
if(play_level > 0)
play_start(play_queue[play_consume]);
else {
printf("SND: playing underrun\n");
play_underrun = 1;
}
}
void snd_play_stop()
{
unsigned int oldmask;
oldmask = irq_getmask();
irq_setmask(0);
CSR_AC97_DCTL = 0;
play_underrun = 0;
irq_ack(IRQ_AC97DMAR);
irq_setmask(oldmask);
}
int snd_play_active()
{
return(CSR_AC97_DCTL & AC97_SCTL_EN);
}
/****************************************************************/
/* RECORDING */
/****************************************************************/
static snd_callback record_callback;
static void *record_user;
static unsigned int record_nbytes;
#define RECORD_BUFQ_SIZE 4 /* < must be a power of 2 */
#define RECORD_BUFQ_MASK (RECORD_BUFQ_SIZE-1)
static short *record_queue[RECORD_BUFQ_SIZE];
static unsigned int record_produce;
static unsigned int record_consume;
static unsigned int record_level;
static int record_overrun;
static void record_start(short *buffer)
{
CSR_AC97_UADDRESS = (unsigned int)buffer;
CSR_AC97_UREMAINING = record_nbytes;
CSR_AC97_UCTL = AC97_SCTL_EN;
}
void snd_isr_dmaw()
{
flush_cpu_dcache();
if(record_level == 0) {
printf("SND: stray DMAW irq\n");
return;
}
/* NB. the callback can give us buffers by calling snd_record_refill() */
record_callback(record_queue[record_consume], record_user);
record_consume = (record_consume + 1) & RECORD_BUFQ_MASK;
record_level--;
irq_ack(IRQ_AC97DMAW);
if(record_level > 0)
record_start(record_queue[record_consume]);
else
record_overrun = 1;
}
void snd_record_empty()
{
record_produce = 0;
record_consume = 0;
record_level = 0;
record_overrun = 0;
}
int snd_record_refill(short *buffer)
{
unsigned int oldmask;
oldmask = irq_getmask();
irq_setmask(0);
if(record_level >= RECORD_BUFQ_SIZE) {
irq_setmask(oldmask);
printf("SND: record bufq overflow\n");
return 0;
}
record_queue[record_produce] = buffer;
record_produce = (record_produce + 1) & RECORD_BUFQ_MASK;
record_level++;
if(record_overrun) {
record_overrun = 0;
record_start(buffer);
}
irq_setmask(oldmask);
return 1;
}
void snd_record_start(snd_callback callback, unsigned int nsamples, void *user)
{
if(snd_record_active()) {
printf("SND: snd_record_start called while already recording\n");
return;
}
record_callback = callback;
record_user = user;
record_nbytes = nsamples*4;
if(record_level > 0)
record_start(record_queue[record_consume]);
else
record_overrun = 1;
}
void snd_record_stop()
{
unsigned int oldmask;
oldmask = irq_getmask();
irq_setmask(0);
CSR_AC97_UCTL = 0;
record_overrun = 0;
irq_ack(IRQ_AC97DMAW);
irq_setmask(oldmask);
}
int snd_record_active()
{
return(CSR_AC97_UCTL & AC97_SCTL_EN);
}