gcscaudio.c revision 1.11 1 /* $NetBSD: gcscaudio.c,v 1.11 2011/11/25 12:50:32 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2008 SHIMIZU Ryo <ryo (at) nerv.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: gcscaudio.c,v 1.11 2011/11/25 12:50:32 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 #include <sys/device.h>
36 #include <sys/queue.h>
37
38 #include <dev/pci/pcidevs.h>
39 #include <dev/pci/pcivar.h>
40
41 #include <sys/audioio.h>
42 #include <dev/audio_if.h>
43 #include <dev/mulaw.h>
44 #include <dev/auconv.h>
45 #include <dev/ic/ac97reg.h>
46 #include <dev/ic/ac97var.h>
47
48 #include <dev/pci/gcscaudioreg.h>
49
50
51 #define GCSCAUDIO_NPRDTABLE 256 /* including a JMP-PRD for loop */
52 #define GCSCAUDIO_PRD_SIZE_MAX 65532 /* limited by CS5536 Controller */
53 #define GCSCAUDIO_BUFSIZE_MAX (GCSCAUDIO_PRD_SIZE_MAX * (GCSCAUDIO_NPRDTABLE - 1))
54
55 struct gcscaudio_prd {
56 /* PRD table for play/rec */
57 struct gcscaudio_prdtables {
58 #define PRD_TABLE_FRONT 0
59 #define PRD_TABLE_SURR 1
60 #define PRD_TABLE_CENTER 2
61 #define PRD_TABLE_LFE 3
62 #define PRD_TABLE_REC 4
63 #define PRD_TABLE_MAX 5
64 struct acc_prd prdtbl[PRD_TABLE_MAX][GCSCAUDIO_NPRDTABLE];
65 } *p_prdtables;
66 bus_dmamap_t p_prdmap;
67 bus_dma_segment_t p_prdsegs[1];
68 int p_prdnseg;
69 };
70
71 struct gcscaudio_dma {
72 LIST_ENTRY(gcscaudio_dma) list;
73 bus_dmamap_t map;
74 void *addr;
75 size_t size;
76 bus_dma_segment_t segs[1];
77 int nseg;
78 };
79
80 struct gcscaudio_softc_ch {
81 void (*ch_intr)(void *);
82 void *ch_intr_arg;
83 struct audio_params ch_params;
84 };
85
86 struct gcscaudio_softc {
87 struct device sc_dev;
88 kmutex_t sc_lock;
89 kmutex_t sc_intr_lock;
90 pci_chipset_tag_t sc_pc;
91 pcitag_t sc_pt;
92 void *sc_ih;
93 bus_space_tag_t sc_iot;
94 bus_space_handle_t sc_ioh;
95 bus_size_t sc_ios;
96 bus_dma_tag_t sc_dmat;
97
98 /* allocated DMA buffer list */
99 LIST_HEAD(, gcscaudio_dma) sc_dmalist;
100
101 #define GCSCAUDIO_MAXFORMATS 4
102 struct audio_format sc_formats[GCSCAUDIO_MAXFORMATS];
103 int sc_nformats;
104 struct audio_encoding_set *sc_encodings;
105
106 /* AC97 codec */
107 struct ac97_host_if host_if;
108 struct ac97_codec_if *codec_if;
109
110 /* input, output channels */
111 struct gcscaudio_softc_ch sc_play;
112 struct gcscaudio_softc_ch sc_rec;
113 struct gcscaudio_prd sc_prd;
114
115 /* multi channel splitter work; {4,6}ch stream to {2,4} DMA buffers */
116 void *sc_mch_split_buf;
117 void *sc_mch_split_start;
118 int sc_mch_split_off;
119 int sc_mch_split_size;
120 int sc_mch_split_blksize;
121 void (*sc_mch_splitter)(void *, void *, int, int);
122 bool sc_spdif;
123 };
124
125 /* for cfattach */
126 static int gcscaudio_match(device_t, cfdata_t, void *);
127 static void gcscaudio_attach(device_t, device_t, void *);
128
129 /* for audio_hw_if */
130 static int gcscaudio_open(void *, int);
131 static void gcscaudio_close(void *);
132 static int gcscaudio_query_encoding(void *, struct audio_encoding *);
133 static int gcscaudio_set_params(void *, int, int, audio_params_t *,
134 audio_params_t *, stream_filter_list_t *,
135 stream_filter_list_t *);
136 static int gcscaudio_round_blocksize(void *, int, int, const audio_params_t *);
137 static int gcscaudio_halt_output(void *);
138 static int gcscaudio_halt_input(void *);
139 static int gcscaudio_getdev(void *, struct audio_device *);
140 static int gcscaudio_set_port(void *, mixer_ctrl_t *);
141 static int gcscaudio_get_port(void *, mixer_ctrl_t *);
142 static int gcscaudio_query_devinfo(void *, mixer_devinfo_t *);
143 static void *gcscaudio_malloc(void *, int, size_t);
144 static void gcscaudio_free(void *, void *, size_t);
145 static size_t gcscaudio_round_buffersize(void *, int, size_t);
146 static paddr_t gcscaudio_mappage(void *, void *, off_t, int);
147 static int gcscaudio_get_props(void *);
148 static int gcscaudio_trigger_output(void *, void *, void *, int,
149 void (*)(void *), void *,
150 const audio_params_t *);
151 static int gcscaudio_trigger_input(void *, void *, void *, int,
152 void (*)(void *), void *,
153 const audio_params_t *);
154 static void gcscaudio_get_locks(void *, kmutex_t **, kmutex_t **);
155 static bool gcscaudio_resume(device_t, const pmf_qual_t *);
156 static int gcscaudio_intr(void *);
157
158 /* for codec_if */
159 static int gcscaudio_attach_codec(void *, struct ac97_codec_if *);
160 static int gcscaudio_write_codec(void *, uint8_t, uint16_t);
161 static int gcscaudio_read_codec(void *, uint8_t, uint16_t *);
162 static int gcscaudio_reset_codec(void *);
163 static void gcscaudio_spdif_event_codec(void *, bool);
164
165 /* misc */
166 static int gcscaudio_append_formats(struct gcscaudio_softc *,
167 const struct audio_format *);
168 static int gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *);
169 static int gcscaudio_set_params_ch(struct gcscaudio_softc *,
170 struct gcscaudio_softc_ch *, int,
171 audio_params_t *, stream_filter_list_t *);
172 static int gcscaudio_allocate_dma(struct gcscaudio_softc *, size_t, void **,
173 bus_dma_segment_t *, int, int *,
174 bus_dmamap_t *);
175
176
177 CFATTACH_DECL(gcscaudio, sizeof (struct gcscaudio_softc),
178 gcscaudio_match, gcscaudio_attach, NULL, NULL);
179
180
181 static struct audio_device gcscaudio_device = {
182 "AMD Geode CS5536",
183 "",
184 "gcscaudio"
185 };
186
187 static const struct audio_hw_if gcscaudio_hw_if = {
188 .open = gcscaudio_open,
189 .close = gcscaudio_close,
190 .drain = NULL,
191 .query_encoding = gcscaudio_query_encoding,
192 .set_params = gcscaudio_set_params,
193 .round_blocksize = gcscaudio_round_blocksize,
194 .commit_settings = NULL,
195 .init_output = NULL,
196 .init_input = NULL,
197 .start_output = NULL,
198 .start_input = NULL,
199 .halt_output = gcscaudio_halt_output,
200 .halt_input = gcscaudio_halt_input,
201 .speaker_ctl = NULL,
202 .getdev = gcscaudio_getdev,
203 .setfd = NULL,
204 .set_port = gcscaudio_set_port,
205 .get_port = gcscaudio_get_port,
206 .query_devinfo = gcscaudio_query_devinfo,
207 .allocm = gcscaudio_malloc,
208 .freem = gcscaudio_free,
209 .round_buffersize = gcscaudio_round_buffersize,
210 .mappage = gcscaudio_mappage,
211 .get_props = gcscaudio_get_props,
212 .trigger_output = gcscaudio_trigger_output,
213 .trigger_input = gcscaudio_trigger_input,
214 .dev_ioctl = NULL,
215 .get_locks = gcscaudio_get_locks,
216 };
217
218 static const struct audio_format gcscaudio_formats_2ch = {
219 NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
220 2, AUFMT_STEREO, 0, {8000, 48000}
221 };
222
223 static const struct audio_format gcscaudio_formats_4ch = {
224 NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
225 4, AUFMT_SURROUND4, 0, {8000, 48000}
226 };
227
228 static const struct audio_format gcscaudio_formats_6ch = {
229 NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
230 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}
231 };
232
233 static int
234 gcscaudio_match(device_t parent, cfdata_t match, void *aux)
235 {
236 struct pci_attach_args *pa;
237
238 pa = (struct pci_attach_args *)aux;
239 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) &&
240 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_CS5536_AUDIO))
241 return 1;
242
243 return 0;
244 }
245
246 static int
247 gcscaudio_append_formats(struct gcscaudio_softc *sc,
248 const struct audio_format *format)
249 {
250 if (sc->sc_nformats >= GCSCAUDIO_MAXFORMATS) {
251 aprint_error_dev(&sc->sc_dev, "too many formats\n");
252 return EINVAL;
253 }
254 sc->sc_formats[sc->sc_nformats++] = *format;
255 return 0;
256 }
257
258 static void
259 gcscaudio_attach(device_t parent, device_t self, void *aux)
260 {
261 struct gcscaudio_softc *sc;
262 struct pci_attach_args *pa;
263 const char *intrstr;
264 pci_intr_handle_t ih;
265 int rc, i;
266
267 sc = device_private(self);
268
269 aprint_naive(": Audio controller\n");
270
271 pa = aux;
272 sc->sc_pc = pa->pa_pc;
273 sc->sc_pt = pa->pa_tag;
274 sc->sc_dmat = pa->pa_dmat;
275 LIST_INIT(&sc->sc_dmalist);
276 sc->sc_mch_split_buf = NULL;
277 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
278 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
279
280 aprint_normal(": AMD Geode CS5536 Audio\n");
281
282 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
283 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)) {
284 aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
285 return;
286 }
287
288 if (pci_intr_map(pa, &ih)) {
289 aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
290 goto attach_failure_unmap;
291 }
292 intrstr = pci_intr_string(sc->sc_pc, ih);
293
294 sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_AUDIO,
295 gcscaudio_intr, sc);
296 if (sc->sc_ih == NULL) {
297 aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
298 if (intrstr != NULL)
299 aprint_error(" at %s", intrstr);
300 aprint_error("\n");
301 goto attach_failure_unmap;
302 }
303
304 aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
305
306
307 if (gcscaudio_allocate_dma(sc, sizeof(*sc->sc_prd.p_prdtables),
308 (void **)&(sc->sc_prd.p_prdtables), sc->sc_prd.p_prdsegs, 1,
309 &(sc->sc_prd.p_prdnseg), &(sc->sc_prd.p_prdmap)) != 0)
310 goto attach_failure_intr;
311
312 sc->host_if.arg = sc;
313 sc->host_if.attach = gcscaudio_attach_codec;
314 sc->host_if.read = gcscaudio_read_codec;
315 sc->host_if.write = gcscaudio_write_codec;
316 sc->host_if.reset = gcscaudio_reset_codec;
317 sc->host_if.spdif_event = gcscaudio_spdif_event_codec;
318
319 if ((rc = ac97_attach(&sc->host_if, self, &sc->sc_lock)) != 0) {
320 aprint_error_dev(&sc->sc_dev,
321 "can't attach codec (error=%d)\n", rc);
322 goto attach_failure_intr;
323 }
324
325 if (!pmf_device_register(self, NULL, gcscaudio_resume))
326 aprint_error_dev(self, "couldn't establish power handler\n");
327
328
329 sc->sc_nformats = 0;
330 gcscaudio_append_formats(sc, &gcscaudio_formats_2ch);
331
332 mutex_enter(&sc->sc_lock);
333 if (AC97_IS_4CH(sc->codec_if))
334 gcscaudio_append_formats(sc, &gcscaudio_formats_4ch);
335 if (AC97_IS_6CH(sc->codec_if))
336 gcscaudio_append_formats(sc, &gcscaudio_formats_6ch);
337 if (AC97_IS_FIXED_RATE(sc->codec_if)) {
338 for (i = 0; i < sc->sc_nformats; i++) {
339 sc->sc_formats[i].frequency_type = 1;
340 sc->sc_formats[i].frequency[0] = 48000;
341 }
342 }
343 mutex_exit(&sc->sc_lock);
344
345 if ((rc = auconv_create_encodings(sc->sc_formats, sc->sc_nformats,
346 &sc->sc_encodings)) != 0) {
347 aprint_error_dev(self,
348 "auconv_create_encoding: error=%d\n", rc);
349 goto attach_failure_codec;
350 }
351
352 audio_attach_mi(&gcscaudio_hw_if, sc, &sc->sc_dev);
353 return;
354
355 attach_failure_codec:
356 sc->codec_if->vtbl->detach(sc->codec_if);
357 attach_failure_intr:
358 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
359 attach_failure_unmap:
360 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
361 return;
362 }
363
364 static int
365 gcscaudio_attach_codec(void *arg, struct ac97_codec_if *codec_if)
366 {
367 struct gcscaudio_softc *sc;
368
369 sc = (struct gcscaudio_softc *)arg;
370 sc->codec_if = codec_if;
371 return 0;
372 }
373
374 static int
375 gcscaudio_reset_codec(void *arg)
376 {
377 struct gcscaudio_softc *sc;
378 sc = (struct gcscaudio_softc *)arg;
379
380 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
381 ACC_CODEC_CNTL_LNK_WRM_RST |
382 ACC_CODEC_CNTL_CMD_NEW);
383
384 if (gcscaudio_wait_ready_codec(sc, "reset timeout\n"))
385 return 1;
386
387 return 0;
388 }
389
390 static void
391 gcscaudio_spdif_event_codec(void *arg, bool flag)
392 {
393 struct gcscaudio_softc *sc;
394
395 sc = (struct gcscaudio_softc *)arg;
396 sc->sc_spdif = flag;
397 }
398
399 static int
400 gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *timeout_msg)
401 {
402 int i;
403
404 #define GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT 500
405 for (i = GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT; (i >= 0) &&
406 (bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL) &
407 ACC_CODEC_CNTL_CMD_NEW); i--)
408 delay(1);
409
410 if (i < 0) {
411 aprint_error_dev(&sc->sc_dev, "%s", timeout_msg);
412 return 1;
413 }
414
415 return 0;
416 }
417
418 static int
419 gcscaudio_write_codec(void *arg, uint8_t reg, uint16_t val)
420 {
421 struct gcscaudio_softc *sc;
422
423 sc = (struct gcscaudio_softc *)arg;
424
425 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
426 ACC_CODEC_CNTL_WRITE_CMD |
427 ACC_CODEC_CNTL_CMD_NEW |
428 ACC_CODEC_REG2ADDR(reg) |
429 (val & ACC_CODEC_CNTL_CMD_DATA_MASK));
430
431 if (gcscaudio_wait_ready_codec(sc, "codec write timeout\n"))
432 return 1;
433
434 #ifdef GCSCAUDIO_CODEC_DEBUG
435 aprint_error_dev(&sc->sc_dev, "codec write: reg=0x%02x, val=0x%04x\n",
436 reg, val);
437 #endif
438
439 return 0;
440 }
441
442 static int
443 gcscaudio_read_codec(void *arg, uint8_t reg, uint16_t *val)
444 {
445 struct gcscaudio_softc *sc;
446 uint32_t v;
447 int i;
448
449 sc = (struct gcscaudio_softc *)arg;
450 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
451 ACC_CODEC_CNTL_READ_CMD | ACC_CODEC_CNTL_CMD_NEW |
452 ACC_CODEC_REG2ADDR(reg));
453
454 if (gcscaudio_wait_ready_codec(sc, "codec write timeout for reading"))
455 return 1;
456
457 #define GCSCAUDIO_READ_CODEC_TIMEOUT 50
458 for (i = GCSCAUDIO_READ_CODEC_TIMEOUT; i >= 0; i--) {
459 v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_STATUS);
460 if ((v & ACC_CODEC_STATUS_STS_NEW) &&
461 (ACC_CODEC_ADDR2REG(v) == reg))
462 break;
463
464 delay(10);
465 }
466
467 if (i < 0) {
468 aprint_error_dev(&sc->sc_dev, "codec read timeout\n");
469 return 1;
470 }
471
472 #ifdef GCSCAUDIO_CODEC_DEBUG
473 aprint_error_dev(&sc->sc_dev, "codec read: reg=0x%02x, val=0x%04x\n",
474 reg, v & ACC_CODEC_STATUS_STS_DATA_MASK);
475 #endif
476
477 *val = v;
478 return 0;
479 }
480
481 static int
482 gcscaudio_open(void *arg, int flags)
483 {
484 struct gcscaudio_softc *sc;
485
486 sc = (struct gcscaudio_softc *)arg;
487 sc->codec_if->vtbl->lock(sc->codec_if);
488 return 0;
489 }
490
491 static void
492 gcscaudio_close(void *arg)
493 {
494 struct gcscaudio_softc *sc;
495
496 sc = (struct gcscaudio_softc *)arg;
497 sc->codec_if->vtbl->unlock(sc->codec_if);
498 }
499
500 static int
501 gcscaudio_query_encoding(void *arg, struct audio_encoding *fp)
502 {
503 struct gcscaudio_softc *sc;
504
505 sc = (struct gcscaudio_softc *)arg;
506 return auconv_query_encoding(sc->sc_encodings, fp);
507 }
508
509 static int
510 gcscaudio_set_params_ch(struct gcscaudio_softc *sc,
511 struct gcscaudio_softc_ch *ch, int mode,
512 audio_params_t *p, stream_filter_list_t *fil)
513 {
514 int error, idx;
515
516 if ((p->sample_rate < 8000) || (p->sample_rate > 48000))
517 return EINVAL;
518
519 if (p->precision != 8 && p->precision != 16)
520 return EINVAL;
521
522 if ((idx = auconv_set_converter(sc->sc_formats, sc->sc_nformats,
523 mode, p, TRUE, fil)) < 0)
524 return EINVAL;
525
526 if (fil->req_size > 0)
527 p = &fil->filters[0].param;
528
529 if (mode == AUMODE_PLAY) {
530 if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
531 /* setup rate of DAC/ADC */
532 if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
533 AC97_REG_PCM_LR_ADC_RATE, &p->sample_rate)) != 0)
534 return error;
535
536 /* additional rate of DAC for Surround */
537 if ((p->channels >= 4) &&
538 (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
539 AC97_REG_PCM_SURR_DAC_RATE, &p->sample_rate)) != 0)
540 return error;
541
542 /* additional rate of DAC for LowFrequencyEffect */
543 if ((p->channels == 6) &&
544 (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
545 AC97_REG_PCM_LFE_DAC_RATE, &p->sample_rate)) != 0)
546 return error;
547 }
548 }
549
550 if (mode == AUMODE_RECORD) {
551 if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
552 /* setup rate of DAC/ADC */
553 if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
554 AC97_REG_PCM_FRONT_DAC_RATE, &p->sample_rate)) != 0)
555 return error;
556 }
557 }
558
559 ch->ch_params = *p;
560 return 0;
561 }
562
563 static int
564 gcscaudio_set_params(void *arg, int setmode, int usemode,
565 audio_params_t *play, audio_params_t *rec,
566 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
567 {
568 struct gcscaudio_softc *sc;
569 int error;
570
571 sc = (struct gcscaudio_softc *)arg;
572
573 if (setmode & AUMODE_PLAY) {
574 if ((error = gcscaudio_set_params_ch(sc, &sc->sc_play,
575 AUMODE_PLAY, play, pfil)) != 0)
576 return error;
577 }
578 if (setmode & AUMODE_RECORD) {
579 if ((error = gcscaudio_set_params_ch(sc, &sc->sc_rec,
580 AUMODE_RECORD, rec, rfil)) != 0)
581 return error;
582 }
583
584 return 0;
585 }
586
587 static int
588 gcscaudio_round_blocksize(void *arg, int blk, int mode,
589 const audio_params_t *param)
590 {
591 blk &= -4;
592 if (blk > GCSCAUDIO_PRD_SIZE_MAX)
593 blk = GCSCAUDIO_PRD_SIZE_MAX;
594
595 return blk;
596 }
597
598 static int
599 gcscaudio_halt_output(void *arg)
600 {
601 struct gcscaudio_softc *sc;
602
603 sc = (struct gcscaudio_softc *)arg;
604 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
605 ACC_BMx_CMD_BM_CTL_DISABLE);
606 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
607 ACC_BMx_CMD_BM_CTL_DISABLE);
608 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
609 ACC_BMx_CMD_BM_CTL_DISABLE);
610 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
611 ACC_BMx_CMD_BM_CTL_DISABLE);
612 sc->sc_play.ch_intr = NULL;
613
614 /* channel splitter */
615 sc->sc_mch_splitter = NULL;
616 if (sc->sc_mch_split_buf)
617 gcscaudio_free(sc, sc->sc_mch_split_buf, sc->sc_mch_split_size);
618 sc->sc_mch_split_buf = NULL;
619
620 return 0;
621 }
622
623 static int
624 gcscaudio_halt_input(void *arg)
625 {
626 struct gcscaudio_softc *sc;
627
628 sc = (struct gcscaudio_softc *)arg;
629 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
630 ACC_BMx_CMD_BM_CTL_DISABLE);
631 sc->sc_rec.ch_intr = NULL;
632 return 0;
633 }
634
635 static int
636 gcscaudio_getdev(void *addr, struct audio_device *retp)
637 {
638 *retp = gcscaudio_device;
639 return 0;
640 }
641
642 static int
643 gcscaudio_set_port(void *addr, mixer_ctrl_t *cp)
644 {
645 struct gcscaudio_softc *sc;
646
647 sc = addr;
648 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
649 }
650
651 static int
652 gcscaudio_get_port(void *addr, mixer_ctrl_t *cp)
653 {
654 struct gcscaudio_softc *sc;
655
656 sc = addr;
657 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
658 }
659
660 static int
661 gcscaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
662 {
663 struct gcscaudio_softc *sc;
664
665 sc = addr;
666 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
667 }
668
669 static void *
670 gcscaudio_malloc(void *arg, int direction, size_t size)
671 {
672 struct gcscaudio_softc *sc;
673 struct gcscaudio_dma *p;
674 int error;
675
676 sc = (struct gcscaudio_softc *)arg;
677
678 p = kmem_alloc(sizeof(*p), KM_SLEEP);
679 if (p == NULL)
680 return NULL;
681 p->size = size;
682
683 error = gcscaudio_allocate_dma(sc, size, &p->addr,
684 p->segs, sizeof(p->segs)/sizeof(p->segs[0]), &p->nseg, &p->map);
685 if (error) {
686 kmem_free(p, sizeof(*p));
687 return NULL;
688 }
689
690 LIST_INSERT_HEAD(&sc->sc_dmalist, p, list);
691 return p->addr;
692 }
693
694 static void
695 gcscaudio_free(void *arg, void *ptr, size_t size)
696 {
697 struct gcscaudio_softc *sc;
698 struct gcscaudio_dma *p;
699
700 sc = (struct gcscaudio_softc *)arg;
701
702 LIST_FOREACH(p, &sc->sc_dmalist, list) {
703 if (p->addr == ptr) {
704 bus_dmamap_unload(sc->sc_dmat, p->map);
705 bus_dmamap_destroy(sc->sc_dmat, p->map);
706 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
707 bus_dmamem_free(sc->sc_dmat, p->segs, p->nseg);
708
709 LIST_REMOVE(p, list);
710 kmem_free(p, sizeof(*p));
711 break;
712 }
713 }
714 }
715
716 static paddr_t
717 gcscaudio_mappage(void *arg, void *mem, off_t off, int prot)
718 {
719 struct gcscaudio_softc *sc;
720 struct gcscaudio_dma *p;
721
722 if (off < 0)
723 return -1;
724
725 sc = (struct gcscaudio_softc *)arg;
726 LIST_FOREACH(p, &sc->sc_dmalist, list) {
727 if (p->addr == mem) {
728 return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nseg,
729 off, prot, BUS_DMA_WAITOK);
730 }
731 }
732
733 return -1;
734 }
735
736 static size_t
737 gcscaudio_round_buffersize(void *addr, int direction, size_t size)
738 {
739 if (size > GCSCAUDIO_BUFSIZE_MAX)
740 size = GCSCAUDIO_BUFSIZE_MAX;
741
742 return size;
743 }
744
745 static int
746 gcscaudio_get_props(void *addr)
747 {
748 struct gcscaudio_softc *sc;
749 int props;
750
751 sc = (struct gcscaudio_softc *)addr;
752 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
753 /*
754 * Even if the codec is fixed-rate, set_param() succeeds for any sample
755 * rate because of aurateconv. Applications can't know what rate the
756 * device can process in the case of mmap().
757 */
758 if (!AC97_IS_FIXED_RATE(sc->codec_if))
759 props |= AUDIO_PROP_MMAP;
760 return props;
761 }
762
763 static int
764 build_prdtables(struct gcscaudio_softc *sc, int prdidx,
765 void *addr, size_t size, int blksize, int blklen, int blkoff)
766 {
767 struct gcscaudio_dma *p;
768 struct acc_prd *prdp;
769 bus_addr_t paddr;
770 int i;
771
772 /* get physical address of start */
773 paddr = (bus_addr_t)0;
774 LIST_FOREACH(p, &sc->sc_dmalist, list) {
775 if (p->addr == addr) {
776 paddr = p->map->dm_segs[0].ds_addr;
777 break;
778 }
779 }
780 if (!paddr) {
781 aprint_error_dev(&sc->sc_dev,
782 "bad addr %p\n", addr);
783 return EINVAL;
784 }
785
786 #define PRDADDR(prdidx,idx) \
787 (sc->sc_prd.p_prdmap->dm_segs[0].ds_addr) + sizeof(struct acc_prd) * \
788 (((prdidx) * GCSCAUDIO_NPRDTABLE) + (idx))
789
790 /*
791 * build PRD table
792 * prdtbl[] = <PRD0>, <PRD1>, <PRD2>, ..., <PRDn>, <jmp to PRD0>
793 */
794 prdp = sc->sc_prd.p_prdtables->prdtbl[prdidx];
795 for (i = 0; size > 0; size -= blksize, i++) {
796 prdp[i].address = paddr + blksize * i + blkoff;
797 prdp[i].ctrlsize =
798 (size < blklen ? size : blklen) | ACC_BMx_PRD_CTRL_EOP;
799 }
800 prdp[i].address = PRDADDR(prdidx, 0);
801 prdp[i].ctrlsize = ACC_BMx_PRD_CTRL_JMP;
802
803 bus_dmamap_sync(sc->sc_dmat, sc->sc_prd.p_prdmap, 0,
804 sizeof(struct acc_prd) * i, BUS_DMASYNC_PREWRITE);
805
806 return 0;
807 }
808
809 static void
810 split_buffer_4ch(void *dst, void *src, int size, int blksize)
811 {
812 int left, i;
813 uint16_t *s, *d;
814
815 /*
816 * src[blk0]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
817 * src[blk1]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
818 * src[blk2]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
819 * :
820 *
821 * rearrange to
822 *
823 * src[blk0]: L,R,L,R,L,R,L,R,..
824 * src[blk1]: L,R,L,R,L,R,L,R,..
825 * src[blk2]: L,R,L,R,L,R,L,R,..
826 * :
827 * dst[blk0]: SL,SR,SL,SR,SL,SR,SL,SR,..
828 * dst[blk1]: SL,SR,SL,SR,SL,SR,SL,SR,..
829 * dst[blk2]: SL,SR,SL,SR,SL,SR,SL,SR,..
830 * :
831 */
832 for (left = size; left > 0; left -= blksize) {
833 s = (uint16_t *)src;
834 d = (uint16_t *)dst;
835 for (i = 0; i < blksize / sizeof(uint16_t) / 4; i++) {
836 /* L,R,SL,SR -> SL,SR */
837 s++;
838 s++;
839 *d++ = *s++;
840 *d++ = *s++;
841 }
842
843 s = (uint16_t *)src;
844 d = (uint16_t *)src;
845 for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
846 /* L,R,SL,SR -> L,R */
847 *d++ = *s++;
848 *d++ = *s++;
849 s++;
850 s++;
851 }
852
853 src = (char *)src + blksize;
854 dst = (char *)dst + blksize;
855 }
856 }
857
858 static void
859 split_buffer_6ch(void *dst, void *src, int size, int blksize)
860 {
861 int left, i;
862 uint16_t *s, *d, *dc, *dl;
863
864 /*
865 * by default, treat as WAV style 5.1ch order
866 * 5.1ch(WAV): L R C LFE SL SR
867 * 5.1ch(AAC): C L R SL SR LFE
868 * :
869 */
870
871 /*
872 * src[blk0]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
873 * src[blk1]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
874 * src[blk2]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
875 * :
876 * src[N-1] : L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
877 *
878 * rearrange to
879 *
880 * src[blk0]: L,R,L,R,..
881 * src[blk1]: L,R,L,R,..
882 * src[blk2]: L,R,L,R,..
883 * :
884 *
885 * dst[blk0]: SL,SR,SL,SR,..
886 * dst[blk1]: SL,SR,SL,SR,..
887 * dst[blk2]: SL,SR,SL,SR,..
888 * :
889 *
890 * dst[N/2+0]: C,C,C,..
891 * dst[N/2+1]: C,C,C,..
892 * :
893 *
894 * dst[N/2+N/4+0]: LFE,LFE,LFE,..
895 * dst[N/2+N/4+1]: LFE,LFE,LFE,..
896 * :
897 */
898
899 for (left = size; left > 0; left -= blksize) {
900 s = (uint16_t *)src;
901 d = (uint16_t *)dst;
902 dc = (uint16_t *)((char *)dst + blksize / 2);
903 dl = (uint16_t *)((char *)dst + blksize / 2 + blksize / 4);
904 for (i = 0; i < blksize / sizeof(uint16_t) / 6; i++) {
905 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
906 /*
907 * AAC: [C,L,R,SL,SR,LFE]
908 * => [SL,SR]
909 * => [C]
910 * => [LFE]
911 */
912 *dc++ = s[0]; /* C */
913 *dl++ = s[5]; /* LFE */
914 *d++ = s[3]; /* SL */
915 *d++ = s[4]; /* SR */
916 #else
917 /*
918 * WAV: [L,R,C,LFE,SL,SR]
919 * => [SL,SR]
920 * => [C]
921 * => [LFE]
922 */
923 *dc++ = s[2]; /* C */
924 *dl++ = s[3]; /* LFE */
925 *d++ = s[4]; /* SL */
926 *d++ = s[5]; /* SR */
927 #endif
928 s += 6;
929 }
930
931 s = (uint16_t *)src;
932 d = (uint16_t *)src;
933 for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
934 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
935 /* AAC: [C,L,R,SL,SR,LFE] => [L,R] */
936 *d++ = s[1];
937 *d++ = s[2];
938 #else
939 /* WAV: [L,R,C,LFE,SL,SR] => [L,R] */
940 *d++ = s[0];
941 *d++ = s[1];
942 #endif
943 s += 6;
944 }
945
946 src = (char *)src + blksize;
947 dst = (char *)dst + blksize;
948 }
949 }
950
951 static void
952 channel_splitter(struct gcscaudio_softc *sc)
953 {
954 int splitsize, left;
955 void *src, *dst;
956
957 if (sc->sc_mch_splitter == NULL)
958 return;
959
960 left = sc->sc_mch_split_size - sc->sc_mch_split_off;
961 splitsize = sc->sc_mch_split_blksize;
962 if (left < splitsize)
963 splitsize = left;
964
965 src = (char *)sc->sc_mch_split_start + sc->sc_mch_split_off;
966 dst = (char *)sc->sc_mch_split_buf + sc->sc_mch_split_off;
967
968 sc->sc_mch_splitter(dst, src, splitsize, sc->sc_mch_split_blksize);
969
970 sc->sc_mch_split_off += sc->sc_mch_split_blksize;
971 if (sc->sc_mch_split_off >= sc->sc_mch_split_size)
972 sc->sc_mch_split_off = 0;
973 }
974
975 static int
976 gcscaudio_trigger_output(void *addr, void *start, void *end, int blksize,
977 void (*intr)(void *), void *arg,
978 const audio_params_t *param)
979 {
980 struct gcscaudio_softc *sc;
981 size_t size;
982
983 sc = (struct gcscaudio_softc *)addr;
984 sc->sc_play.ch_intr = intr;
985 sc->sc_play.ch_intr_arg = arg;
986 size = (char *)end - (char *)start;
987
988 switch (sc->sc_play.ch_params.channels) {
989 case 2:
990 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
991 blksize, 0))
992 return EINVAL;
993
994 if (!AC97_IS_4CH(sc->codec_if)) {
995 /*
996 * output 2ch PCM to FRONT.LR(BM0)
997 *
998 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
999 *
1000 */
1001 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1002 PRDADDR(PRD_TABLE_FRONT, 0));
1003
1004 /* start DMA transfer */
1005 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1006 ACC_BMx_CMD_WRITE |
1007 ACC_BMx_CMD_BYTE_ORD_EL |
1008 ACC_BMx_CMD_BM_CTL_ENABLE);
1009 } else {
1010 /*
1011 * output same PCM to FRONT.LR(BM0) and SURROUND.LR(BM6).
1012 * CENTER(BM4) and LFE(BM7) doesn't sound.
1013 *
1014 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
1015 * BM6: (same of BM0)
1016 * BM4: none
1017 * BM7: none
1018 */
1019 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1020 PRDADDR(PRD_TABLE_FRONT, 0));
1021 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1022 PRDADDR(PRD_TABLE_FRONT, 0));
1023
1024 /* start DMA transfer */
1025 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1026 ACC_BMx_CMD_WRITE |
1027 ACC_BMx_CMD_BYTE_ORD_EL |
1028 ACC_BMx_CMD_BM_CTL_ENABLE);
1029 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1030 ACC_BMx_CMD_WRITE |
1031 ACC_BMx_CMD_BYTE_ORD_EL |
1032 ACC_BMx_CMD_BM_CTL_ENABLE);
1033 }
1034 break;
1035 case 4:
1036 /*
1037 * output 4ch PCM split to FRONT.LR(BM0) and SURROUND.LR(BM6).
1038 * CENTER(BM4) and LFE(BM7) doesn't sound.
1039 *
1040 * rearrange ordered channel to continuous per channel
1041 *
1042 * 4ch: L,R,SL,SR,L,R,SL,SR,... => BM0: L,R,L,R,...
1043 * BM6: SL,SR,SL,SR,...
1044 * BM4: none
1045 * BM7: none
1046 */
1047 if (sc->sc_mch_split_buf)
1048 gcscaudio_free(sc, sc->sc_mch_split_buf,
1049 sc->sc_mch_split_size);
1050
1051 if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
1052 size)) == NULL)
1053 return ENOMEM;
1054
1055 /*
1056 * 1st and 2nd blocks are split immediately.
1057 * Other blocks will be split synchronous with intr.
1058 */
1059 split_buffer_4ch(sc->sc_mch_split_buf, start, blksize * 2,
1060 blksize);
1061
1062 sc->sc_mch_split_start = start;
1063 sc->sc_mch_split_size = size;
1064 sc->sc_mch_split_blksize = blksize;
1065 sc->sc_mch_split_off = (blksize * 2) % size;
1066 sc->sc_mch_splitter = split_buffer_4ch; /* split function */
1067
1068 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1069 blksize / 2, 0))
1070 return EINVAL;
1071 if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1072 size, blksize, blksize / 2, 0))
1073 return EINVAL;
1074
1075 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1076 PRDADDR(PRD_TABLE_FRONT, 0));
1077 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1078 PRDADDR(PRD_TABLE_SURR, 0));
1079
1080 /* start DMA transfer */
1081 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1082 ACC_BMx_CMD_WRITE |
1083 ACC_BMx_CMD_BYTE_ORD_EL |
1084 ACC_BMx_CMD_BM_CTL_ENABLE);
1085 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1086 ACC_BMx_CMD_WRITE |
1087 ACC_BMx_CMD_BYTE_ORD_EL |
1088 ACC_BMx_CMD_BM_CTL_ENABLE);
1089 break;
1090 case 6:
1091 /*
1092 * output 6ch PCM split to
1093 * FRONT.LR(BM0), SURROUND.LR(BM6), CENTER(BM4) and LFE(BM7)
1094 *
1095 * rearrange ordered channel to continuous per channel
1096 *
1097 * 5.1ch: L,R,C,LFE,SL,SR,... => BM0: L,R,...
1098 * BM4: C,...
1099 * BM6: SL,SR,...
1100 * BM7: LFE,...
1101 *
1102 */
1103 if (sc->sc_mch_split_buf)
1104 gcscaudio_free(sc, sc->sc_mch_split_buf,
1105 sc->sc_mch_split_size);
1106
1107 if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
1108 size)) == NULL)
1109 return ENOMEM;
1110
1111 /*
1112 * 1st and 2nd blocks are split immediately.
1113 * Other block will be split synchronous with intr.
1114 */
1115 split_buffer_6ch(sc->sc_mch_split_buf, start, blksize * 2,
1116 blksize);
1117
1118 sc->sc_mch_split_start = start;
1119 sc->sc_mch_split_size = size;
1120 sc->sc_mch_split_blksize = blksize;
1121 sc->sc_mch_split_off = (blksize * 2) % size;
1122 sc->sc_mch_splitter = split_buffer_6ch; /* split function */
1123
1124 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1125 blksize / 3, 0))
1126 return EINVAL;
1127 if (build_prdtables(sc, PRD_TABLE_CENTER, sc->sc_mch_split_buf,
1128 size, blksize, blksize / 3, blksize / 2))
1129 return EINVAL;
1130 if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1131 size, blksize, blksize / 3, 0))
1132 return EINVAL;
1133 if (build_prdtables(sc, PRD_TABLE_LFE, sc->sc_mch_split_buf,
1134 size, blksize, blksize / 3, blksize / 2 + blksize / 4))
1135 return EINVAL;
1136
1137 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1138 PRDADDR(PRD_TABLE_FRONT, 0));
1139 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_PRD,
1140 PRDADDR(PRD_TABLE_CENTER, 0));
1141 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1142 PRDADDR(PRD_TABLE_SURR, 0));
1143 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_PRD,
1144 PRDADDR(PRD_TABLE_LFE, 0));
1145
1146 /* start DMA transfer */
1147 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1148 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1149 ACC_BMx_CMD_BM_CTL_ENABLE);
1150 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
1151 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1152 ACC_BMx_CMD_BM_CTL_ENABLE);
1153 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1154 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1155 ACC_BMx_CMD_BM_CTL_ENABLE);
1156 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
1157 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1158 ACC_BMx_CMD_BM_CTL_ENABLE);
1159 break;
1160 }
1161
1162 return 0;
1163 }
1164
1165 static int
1166 gcscaudio_trigger_input(void *addr, void *start, void *end, int blksize,
1167 void (*intr)(void *), void *arg,
1168 const audio_params_t *param)
1169 {
1170 struct gcscaudio_softc *sc;
1171 size_t size;
1172
1173 sc = (struct gcscaudio_softc *)addr;
1174 sc->sc_rec.ch_intr = intr;
1175 sc->sc_rec.ch_intr_arg = arg;
1176 size = (char *)end - (char *)start;
1177
1178 if (build_prdtables(sc, PRD_TABLE_REC, start, size, blksize, blksize, 0))
1179 return EINVAL;
1180
1181 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_PRD,
1182 PRDADDR(PRD_TABLE_REC, 0));
1183
1184 /* start transfer */
1185 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
1186 ACC_BMx_CMD_READ |
1187 ACC_BMx_CMD_BYTE_ORD_EL |
1188 ACC_BMx_CMD_BM_CTL_ENABLE);
1189
1190 return 0;
1191 }
1192
1193 static void
1194 gcscaudio_get_locks(void *arg, kmutex_t **intr, kmutex_t **thread)
1195 {
1196 struct gcscaudio_softc *sc;
1197
1198 sc = (struct gcscaudio_softc *)arg;
1199
1200 *intr = &sc->sc_intr_lock;
1201 *thread = &sc->sc_lock;
1202 }
1203
1204 static int
1205 gcscaudio_intr(void *arg)
1206 {
1207 struct gcscaudio_softc *sc;
1208 uint16_t intr;
1209 uint8_t bmstat;
1210 int nintr;
1211
1212 nintr = 0;
1213 sc = (struct gcscaudio_softc *)arg;
1214
1215 mutex_spin_enter(&sc->sc_intr_lock);
1216
1217 intr = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ACC_IRQ_STATUS);
1218 if (intr == 0)
1219 goto done;
1220
1221 /* Front output */
1222 if (intr & ACC_IRQ_STATUS_BM0_IRQ_STS) {
1223 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_STATUS);
1224 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1225 aprint_normal_dev(&sc->sc_dev, "BM0: Bus Master Error\n");
1226 if (!(bmstat & ACC_BMx_STATUS_EOP))
1227 aprint_normal_dev(&sc->sc_dev, "BM0: NO End of Page?\n");
1228
1229 if (sc->sc_play.ch_intr) {
1230 sc->sc_play.ch_intr(sc->sc_play.ch_intr_arg);
1231 channel_splitter(sc);
1232 }
1233 nintr++;
1234 }
1235
1236 /* Center output */
1237 if (intr & ACC_IRQ_STATUS_BM4_IRQ_STS) {
1238 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_STATUS);
1239 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1240 aprint_normal_dev(&sc->sc_dev, "BM4: Bus Master Error\n");
1241 if (!(bmstat & ACC_BMx_STATUS_EOP))
1242 aprint_normal_dev(&sc->sc_dev, "BM4: NO End of Page?\n");
1243
1244 nintr++;
1245 }
1246
1247 /* Surround output */
1248 if (intr & ACC_IRQ_STATUS_BM6_IRQ_STS) {
1249 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_STATUS);
1250 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1251 aprint_normal_dev(&sc->sc_dev, "BM6: Bus Master Error\n");
1252 if (!(bmstat & ACC_BMx_STATUS_EOP))
1253 aprint_normal_dev(&sc->sc_dev, "BM6: NO End of Page?\n");
1254
1255 nintr++;
1256 }
1257
1258 /* LowFrequencyEffect output */
1259 if (intr & ACC_IRQ_STATUS_BM7_IRQ_STS) {
1260 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_STATUS);
1261 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1262 aprint_normal_dev(&sc->sc_dev, "BM7: Bus Master Error\n");
1263 if (!(bmstat & ACC_BMx_STATUS_EOP))
1264 aprint_normal_dev(&sc->sc_dev, "BM7: NO End of Page?\n");
1265
1266 nintr++;
1267 }
1268
1269 /* record */
1270 if (intr & ACC_IRQ_STATUS_BM1_IRQ_STS) {
1271 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_STATUS);
1272 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1273 aprint_normal_dev(&sc->sc_dev, "BM1: Bus Master Error\n");
1274 if (!(bmstat & ACC_BMx_STATUS_EOP))
1275 aprint_normal_dev(&sc->sc_dev, "BM1: NO End of Page?\n");
1276
1277 if (sc->sc_rec.ch_intr) {
1278 sc->sc_rec.ch_intr(sc->sc_rec.ch_intr_arg);
1279 }
1280 nintr++;
1281 }
1282
1283 #ifdef GCSCAUDIO_DEBUG
1284 if (intr & ACC_IRQ_STATUS_IRQ_STS)
1285 aprint_normal_dev(&sc->sc_dev, "Codec GPIO IRQ Status\n");
1286 if (intr & ACC_IRQ_STATUS_WU_IRQ_STS)
1287 aprint_normal_dev(&sc->sc_dev, "Codec GPIO Wakeup IRQ Status\n");
1288 if (intr & ACC_IRQ_STATUS_BM2_IRQ_STS)
1289 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 2 IRQ Status\n");
1290 if (intr & ACC_IRQ_STATUS_BM3_IRQ_STS)
1291 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 3 IRQ Status\n");
1292 if (intr & ACC_IRQ_STATUS_BM5_IRQ_STS)
1293 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 5 IRQ Status\n");
1294 #endif
1295
1296 done:
1297 mutex_spin_exit(&sc->sc_intr_lock);
1298
1299 return nintr ? 1 : 0;
1300 }
1301
1302 static bool
1303 gcscaudio_resume(device_t dv, const pmf_qual_t *qual)
1304 {
1305 struct gcscaudio_softc *sc = device_private(dv);
1306
1307 gcscaudio_reset_codec(sc);
1308 DELAY(1000);
1309 (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1310
1311 return true;
1312 }
1313
1314 static int
1315 gcscaudio_allocate_dma(struct gcscaudio_softc *sc, size_t size, void **addrp,
1316 bus_dma_segment_t *seglist, int nseg, int *rsegp,
1317 bus_dmamap_t *mapp)
1318 {
1319 int error;
1320
1321 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seglist,
1322 nseg, rsegp, BUS_DMA_WAITOK)) != 0) {
1323 aprint_error_dev(&sc->sc_dev,
1324 "unable to allocate DMA buffer, error=%d\n", error);
1325 goto fail_alloc;
1326 }
1327
1328 if ((error = bus_dmamem_map(sc->sc_dmat, seglist, nseg, size, addrp,
1329 BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
1330 aprint_error_dev(&sc->sc_dev,
1331 "unable to map DMA buffer, error=%d\n",
1332 error);
1333 goto fail_map;
1334 }
1335
1336 if ((error = bus_dmamap_create(sc->sc_dmat, size, nseg, size, 0,
1337 BUS_DMA_WAITOK, mapp)) != 0) {
1338 aprint_error_dev(&sc->sc_dev,
1339 "unable to create DMA map, error=%d\n", error);
1340 goto fail_create;
1341 }
1342
1343 if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
1344 BUS_DMA_WAITOK)) != 0) {
1345 aprint_error_dev(&sc->sc_dev,
1346 "unable to load DMA map, error=%d\n", error);
1347 goto fail_load;
1348 }
1349
1350 return 0;
1351
1352 fail_load:
1353 bus_dmamap_destroy(sc->sc_dmat, *mapp);
1354 fail_create:
1355 bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
1356 fail_map:
1357 bus_dmamem_free(sc->sc_dmat, seglist, nseg);
1358 fail_alloc:
1359 return error;
1360 }
1361