gcscaudio.c revision 1.9 1 /* $NetBSD: gcscaudio.c,v 1.9 2011/11/24 03:35:59 mrg 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.9 2011/11/24 03:35:59 mrg 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 if (AC97_IS_4CH(sc->codec_if))
332 gcscaudio_append_formats(sc, &gcscaudio_formats_4ch);
333 if (AC97_IS_6CH(sc->codec_if))
334 gcscaudio_append_formats(sc, &gcscaudio_formats_6ch);
335 if (AC97_IS_FIXED_RATE(sc->codec_if)) {
336 for (i = 0; i < sc->sc_nformats; i++) {
337 sc->sc_formats[i].frequency_type = 1;
338 sc->sc_formats[i].frequency[0] = 48000;
339 }
340 }
341
342 if ((rc = auconv_create_encodings(sc->sc_formats, sc->sc_nformats,
343 &sc->sc_encodings)) != 0) {
344 aprint_error_dev(self,
345 "auconv_create_encoding: error=%d\n", rc);
346 goto attach_failure_codec;
347 }
348
349 audio_attach_mi(&gcscaudio_hw_if, sc, &sc->sc_dev);
350 sc->codec_if->vtbl->unlock(sc->codec_if);
351 return;
352
353 attach_failure_codec:
354 sc->codec_if->vtbl->detach(sc->codec_if);
355 attach_failure_intr:
356 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
357 attach_failure_unmap:
358 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
359 return;
360 }
361
362 static int
363 gcscaudio_attach_codec(void *arg, struct ac97_codec_if *codec_if)
364 {
365 struct gcscaudio_softc *sc;
366
367 sc = (struct gcscaudio_softc *)arg;
368 sc->codec_if = codec_if;
369 return 0;
370 }
371
372 static int
373 gcscaudio_reset_codec(void *arg)
374 {
375 struct gcscaudio_softc *sc;
376 sc = (struct gcscaudio_softc *)arg;
377
378 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
379 ACC_CODEC_CNTL_LNK_WRM_RST |
380 ACC_CODEC_CNTL_CMD_NEW);
381
382 if (gcscaudio_wait_ready_codec(sc, "reset timeout\n"))
383 return 1;
384
385 return 0;
386 }
387
388 static void
389 gcscaudio_spdif_event_codec(void *arg, bool flag)
390 {
391 struct gcscaudio_softc *sc;
392
393 sc = (struct gcscaudio_softc *)arg;
394 sc->sc_spdif = flag;
395 }
396
397 static int
398 gcscaudio_wait_ready_codec(struct gcscaudio_softc *sc, const char *timeout_msg)
399 {
400 int i;
401
402 #define GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT 500
403 for (i = GCSCAUDIO_WAIT_READY_CODEC_TIMEOUT; (i >= 0) &&
404 (bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL) &
405 ACC_CODEC_CNTL_CMD_NEW); i--)
406 delay(1);
407
408 if (i < 0) {
409 aprint_error_dev(&sc->sc_dev, "%s", timeout_msg);
410 return 1;
411 }
412
413 return 0;
414 }
415
416 static int
417 gcscaudio_write_codec(void *arg, uint8_t reg, uint16_t val)
418 {
419 struct gcscaudio_softc *sc;
420
421 sc = (struct gcscaudio_softc *)arg;
422
423 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
424 ACC_CODEC_CNTL_WRITE_CMD |
425 ACC_CODEC_CNTL_CMD_NEW |
426 ACC_CODEC_REG2ADDR(reg) |
427 (val & ACC_CODEC_CNTL_CMD_DATA_MASK));
428
429 if (gcscaudio_wait_ready_codec(sc, "codec write timeout\n"))
430 return 1;
431
432 #ifdef GCSCAUDIO_CODEC_DEBUG
433 aprint_error_dev(&sc->sc_dev, "codec write: reg=0x%02x, val=0x%04x\n",
434 reg, val);
435 #endif
436
437 return 0;
438 }
439
440 static int
441 gcscaudio_read_codec(void *arg, uint8_t reg, uint16_t *val)
442 {
443 struct gcscaudio_softc *sc;
444 uint32_t v;
445 int i;
446
447 sc = (struct gcscaudio_softc *)arg;
448 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_CNTL,
449 ACC_CODEC_CNTL_READ_CMD | ACC_CODEC_CNTL_CMD_NEW |
450 ACC_CODEC_REG2ADDR(reg));
451
452 if (gcscaudio_wait_ready_codec(sc, "codec write timeout for reading"))
453 return 1;
454
455 #define GCSCAUDIO_READ_CODEC_TIMEOUT 50
456 for (i = GCSCAUDIO_READ_CODEC_TIMEOUT; i >= 0; i--) {
457 v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ACC_CODEC_STATUS);
458 if ((v & ACC_CODEC_STATUS_STS_NEW) &&
459 (ACC_CODEC_ADDR2REG(v) == reg))
460 break;
461
462 delay(10);
463 }
464
465 if (i < 0) {
466 aprint_error_dev(&sc->sc_dev, "codec read timeout\n");
467 return 1;
468 }
469
470 #ifdef GCSCAUDIO_CODEC_DEBUG
471 aprint_error_dev(&sc->sc_dev, "codec read: reg=0x%02x, val=0x%04x\n",
472 reg, v & ACC_CODEC_STATUS_STS_DATA_MASK);
473 #endif
474
475 *val = v;
476 return 0;
477 }
478
479 static int
480 gcscaudio_open(void *arg, int flags)
481 {
482 struct gcscaudio_softc *sc;
483
484 sc = (struct gcscaudio_softc *)arg;
485 sc->codec_if->vtbl->lock(sc->codec_if);
486 return 0;
487 }
488
489 static void
490 gcscaudio_close(void *arg)
491 {
492 struct gcscaudio_softc *sc;
493
494 sc = (struct gcscaudio_softc *)arg;
495 sc->codec_if->vtbl->unlock(sc->codec_if);
496 }
497
498 static int
499 gcscaudio_query_encoding(void *arg, struct audio_encoding *fp)
500 {
501 struct gcscaudio_softc *sc;
502
503 sc = (struct gcscaudio_softc *)arg;
504 return auconv_query_encoding(sc->sc_encodings, fp);
505 }
506
507 static int
508 gcscaudio_set_params_ch(struct gcscaudio_softc *sc,
509 struct gcscaudio_softc_ch *ch, int mode,
510 audio_params_t *p, stream_filter_list_t *fil)
511 {
512 int error, idx;
513
514 if ((p->sample_rate < 8000) || (p->sample_rate > 48000))
515 return EINVAL;
516
517 if (p->precision != 8 && p->precision != 16)
518 return EINVAL;
519
520 if ((idx = auconv_set_converter(sc->sc_formats, sc->sc_nformats,
521 mode, p, TRUE, fil)) < 0)
522 return EINVAL;
523
524 if (fil->req_size > 0)
525 p = &fil->filters[0].param;
526
527 if (mode == AUMODE_PLAY) {
528 if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
529 /* setup rate of DAC/ADC */
530 if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
531 AC97_REG_PCM_LR_ADC_RATE, &p->sample_rate)) != 0)
532 return error;
533
534 /* additional rate of DAC for Surround */
535 if ((p->channels >= 4) &&
536 (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
537 AC97_REG_PCM_SURR_DAC_RATE, &p->sample_rate)) != 0)
538 return error;
539
540 /* additional rate of DAC for LowFrequencyEffect */
541 if ((p->channels == 6) &&
542 (error = sc->codec_if->vtbl->set_rate(sc->codec_if,
543 AC97_REG_PCM_LFE_DAC_RATE, &p->sample_rate)) != 0)
544 return error;
545 }
546 }
547
548 if (mode == AUMODE_RECORD) {
549 if (!AC97_IS_FIXED_RATE(sc->codec_if)) {
550 /* setup rate of DAC/ADC */
551 if ((error = sc->codec_if->vtbl->set_rate(sc->codec_if,
552 AC97_REG_PCM_FRONT_DAC_RATE, &p->sample_rate)) != 0)
553 return error;
554 }
555 }
556
557 ch->ch_params = *p;
558 return 0;
559 }
560
561 static int
562 gcscaudio_set_params(void *arg, int setmode, int usemode,
563 audio_params_t *play, audio_params_t *rec,
564 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
565 {
566 struct gcscaudio_softc *sc;
567 int error;
568
569 sc = (struct gcscaudio_softc *)arg;
570
571 if (setmode & AUMODE_PLAY) {
572 if ((error = gcscaudio_set_params_ch(sc, &sc->sc_play,
573 AUMODE_PLAY, play, pfil)) != 0)
574 return error;
575 }
576 if (setmode & AUMODE_RECORD) {
577 if ((error = gcscaudio_set_params_ch(sc, &sc->sc_rec,
578 AUMODE_RECORD, rec, rfil)) != 0)
579 return error;
580 }
581
582 return 0;
583 }
584
585 static int
586 gcscaudio_round_blocksize(void *arg, int blk, int mode,
587 const audio_params_t *param)
588 {
589 blk &= -4;
590 if (blk > GCSCAUDIO_PRD_SIZE_MAX)
591 blk = GCSCAUDIO_PRD_SIZE_MAX;
592
593 return blk;
594 }
595
596 static int
597 gcscaudio_halt_output(void *arg)
598 {
599 struct gcscaudio_softc *sc;
600
601 sc = (struct gcscaudio_softc *)arg;
602 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
603 ACC_BMx_CMD_BM_CTL_DISABLE);
604 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
605 ACC_BMx_CMD_BM_CTL_DISABLE);
606 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
607 ACC_BMx_CMD_BM_CTL_DISABLE);
608 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
609 ACC_BMx_CMD_BM_CTL_DISABLE);
610 sc->sc_play.ch_intr = NULL;
611
612 /* channel splitter */
613 sc->sc_mch_splitter = NULL;
614 if (sc->sc_mch_split_buf)
615 gcscaudio_free(sc, sc->sc_mch_split_buf, sc->sc_mch_split_size);
616 sc->sc_mch_split_buf = NULL;
617
618 return 0;
619 }
620
621 static int
622 gcscaudio_halt_input(void *arg)
623 {
624 struct gcscaudio_softc *sc;
625
626 sc = (struct gcscaudio_softc *)arg;
627 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
628 ACC_BMx_CMD_BM_CTL_DISABLE);
629 sc->sc_rec.ch_intr = NULL;
630 return 0;
631 }
632
633 static int
634 gcscaudio_getdev(void *addr, struct audio_device *retp)
635 {
636 *retp = gcscaudio_device;
637 return 0;
638 }
639
640 static int
641 gcscaudio_set_port(void *addr, mixer_ctrl_t *cp)
642 {
643 struct gcscaudio_softc *sc;
644
645 sc = addr;
646 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
647 }
648
649 static int
650 gcscaudio_get_port(void *addr, mixer_ctrl_t *cp)
651 {
652 struct gcscaudio_softc *sc;
653
654 sc = addr;
655 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
656 }
657
658 static int
659 gcscaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
660 {
661 struct gcscaudio_softc *sc;
662
663 sc = addr;
664 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
665 }
666
667 static void *
668 gcscaudio_malloc(void *arg, int direction, size_t size)
669 {
670 struct gcscaudio_softc *sc;
671 struct gcscaudio_dma *p;
672 int error;
673
674 sc = (struct gcscaudio_softc *)arg;
675
676 p = kmem_alloc(sizeof(*p), KM_SLEEP);
677 if (p == NULL)
678 return NULL;
679 p->size = size;
680
681 error = gcscaudio_allocate_dma(sc, size, &p->addr,
682 p->segs, sizeof(p->segs)/sizeof(p->segs[0]), &p->nseg, &p->map);
683 if (error) {
684 kmem_free(p, sizeof(*p));
685 return NULL;
686 }
687
688 LIST_INSERT_HEAD(&sc->sc_dmalist, p, list);
689 return p->addr;
690 }
691
692 static void
693 gcscaudio_free(void *arg, void *ptr, size_t size)
694 {
695 struct gcscaudio_softc *sc;
696 struct gcscaudio_dma *p;
697
698 sc = (struct gcscaudio_softc *)arg;
699
700 LIST_FOREACH(p, &sc->sc_dmalist, list) {
701 if (p->addr == ptr) {
702 bus_dmamap_unload(sc->sc_dmat, p->map);
703 bus_dmamap_destroy(sc->sc_dmat, p->map);
704 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
705 bus_dmamem_free(sc->sc_dmat, p->segs, p->nseg);
706
707 LIST_REMOVE(p, list);
708 kmem_free(p, sizeof(*p));
709 break;
710 }
711 }
712 }
713
714 static paddr_t
715 gcscaudio_mappage(void *arg, void *mem, off_t off, int prot)
716 {
717 struct gcscaudio_softc *sc;
718 struct gcscaudio_dma *p;
719
720 if (off < 0)
721 return -1;
722
723 sc = (struct gcscaudio_softc *)arg;
724 LIST_FOREACH(p, &sc->sc_dmalist, list) {
725 if (p->addr == mem) {
726 return bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nseg,
727 off, prot, BUS_DMA_WAITOK);
728 }
729 }
730
731 return -1;
732 }
733
734 static size_t
735 gcscaudio_round_buffersize(void *addr, int direction, size_t size)
736 {
737 if (size > GCSCAUDIO_BUFSIZE_MAX)
738 size = GCSCAUDIO_BUFSIZE_MAX;
739
740 return size;
741 }
742
743 static int
744 gcscaudio_get_props(void *addr)
745 {
746 struct gcscaudio_softc *sc;
747 int props;
748
749 sc = (struct gcscaudio_softc *)addr;
750 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
751 /*
752 * Even if the codec is fixed-rate, set_param() succeeds for any sample
753 * rate because of aurateconv. Applications can't know what rate the
754 * device can process in the case of mmap().
755 */
756 if (!AC97_IS_FIXED_RATE(sc->codec_if))
757 props |= AUDIO_PROP_MMAP;
758 return props;
759 }
760
761 static int
762 build_prdtables(struct gcscaudio_softc *sc, int prdidx,
763 void *addr, size_t size, int blksize, int blklen, int blkoff)
764 {
765 struct gcscaudio_dma *p;
766 struct acc_prd *prdp;
767 bus_addr_t paddr;
768 int i;
769
770 /* get physical address of start */
771 paddr = (bus_addr_t)0;
772 LIST_FOREACH(p, &sc->sc_dmalist, list) {
773 if (p->addr == addr) {
774 paddr = p->map->dm_segs[0].ds_addr;
775 break;
776 }
777 }
778 if (!paddr) {
779 aprint_error_dev(&sc->sc_dev,
780 "bad addr %p\n", addr);
781 return EINVAL;
782 }
783
784 #define PRDADDR(prdidx,idx) \
785 (sc->sc_prd.p_prdmap->dm_segs[0].ds_addr) + sizeof(struct acc_prd) * \
786 (((prdidx) * GCSCAUDIO_NPRDTABLE) + (idx))
787
788 /*
789 * build PRD table
790 * prdtbl[] = <PRD0>, <PRD1>, <PRD2>, ..., <PRDn>, <jmp to PRD0>
791 */
792 prdp = sc->sc_prd.p_prdtables->prdtbl[prdidx];
793 for (i = 0; size > 0; size -= blksize, i++) {
794 prdp[i].address = paddr + blksize * i + blkoff;
795 prdp[i].ctrlsize =
796 (size < blklen ? size : blklen) | ACC_BMx_PRD_CTRL_EOP;
797 }
798 prdp[i].address = PRDADDR(prdidx, 0);
799 prdp[i].ctrlsize = ACC_BMx_PRD_CTRL_JMP;
800
801 bus_dmamap_sync(sc->sc_dmat, sc->sc_prd.p_prdmap, 0,
802 sizeof(struct acc_prd) * i, BUS_DMASYNC_PREWRITE);
803
804 return 0;
805 }
806
807 static void
808 split_buffer_4ch(void *dst, void *src, int size, int blksize)
809 {
810 int left, i;
811 uint16_t *s, *d;
812
813 /*
814 * src[blk0]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
815 * src[blk1]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
816 * src[blk2]: L,R,SL,SR,L,R,SL,SR,L,R,SL,SR,....
817 * :
818 *
819 * rearrange to
820 *
821 * src[blk0]: L,R,L,R,L,R,L,R,..
822 * src[blk1]: L,R,L,R,L,R,L,R,..
823 * src[blk2]: L,R,L,R,L,R,L,R,..
824 * :
825 * dst[blk0]: SL,SR,SL,SR,SL,SR,SL,SR,..
826 * dst[blk1]: SL,SR,SL,SR,SL,SR,SL,SR,..
827 * dst[blk2]: SL,SR,SL,SR,SL,SR,SL,SR,..
828 * :
829 */
830 for (left = size; left > 0; left -= blksize) {
831 s = (uint16_t *)src;
832 d = (uint16_t *)dst;
833 for (i = 0; i < blksize / sizeof(uint16_t) / 4; i++) {
834 /* L,R,SL,SR -> SL,SR */
835 s++;
836 s++;
837 *d++ = *s++;
838 *d++ = *s++;
839 }
840
841 s = (uint16_t *)src;
842 d = (uint16_t *)src;
843 for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
844 /* L,R,SL,SR -> L,R */
845 *d++ = *s++;
846 *d++ = *s++;
847 s++;
848 s++;
849 }
850
851 src = (char *)src + blksize;
852 dst = (char *)dst + blksize;
853 }
854 }
855
856 static void
857 split_buffer_6ch(void *dst, void *src, int size, int blksize)
858 {
859 int left, i;
860 uint16_t *s, *d, *dc, *dl;
861
862 /*
863 * by default, treat as WAV style 5.1ch order
864 * 5.1ch(WAV): L R C LFE SL SR
865 * 5.1ch(AAC): C L R SL SR LFE
866 * :
867 */
868
869 /*
870 * src[blk0]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
871 * src[blk1]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
872 * src[blk2]: L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
873 * :
874 * src[N-1] : L,R,C,LFE,SL,SR,L,R,C,LFE,SL,SR,...
875 *
876 * rearrange to
877 *
878 * src[blk0]: L,R,L,R,..
879 * src[blk1]: L,R,L,R,..
880 * src[blk2]: L,R,L,R,..
881 * :
882 *
883 * dst[blk0]: SL,SR,SL,SR,..
884 * dst[blk1]: SL,SR,SL,SR,..
885 * dst[blk2]: SL,SR,SL,SR,..
886 * :
887 *
888 * dst[N/2+0]: C,C,C,..
889 * dst[N/2+1]: C,C,C,..
890 * :
891 *
892 * dst[N/2+N/4+0]: LFE,LFE,LFE,..
893 * dst[N/2+N/4+1]: LFE,LFE,LFE,..
894 * :
895 */
896
897 for (left = size; left > 0; left -= blksize) {
898 s = (uint16_t *)src;
899 d = (uint16_t *)dst;
900 dc = (uint16_t *)((char *)dst + blksize / 2);
901 dl = (uint16_t *)((char *)dst + blksize / 2 + blksize / 4);
902 for (i = 0; i < blksize / sizeof(uint16_t) / 6; i++) {
903 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
904 /*
905 * AAC: [C,L,R,SL,SR,LFE]
906 * => [SL,SR]
907 * => [C]
908 * => [LFE]
909 */
910 *dc++ = s[0]; /* C */
911 *dl++ = s[5]; /* LFE */
912 *d++ = s[3]; /* SL */
913 *d++ = s[4]; /* SR */
914 #else
915 /*
916 * WAV: [L,R,C,LFE,SL,SR]
917 * => [SL,SR]
918 * => [C]
919 * => [LFE]
920 */
921 *dc++ = s[2]; /* C */
922 *dl++ = s[3]; /* LFE */
923 *d++ = s[4]; /* SL */
924 *d++ = s[5]; /* SR */
925 #endif
926 s += 6;
927 }
928
929 s = (uint16_t *)src;
930 d = (uint16_t *)src;
931 for (i = 0; i < blksize / sizeof(uint16_t) / 2 / 2; i++) {
932 #ifdef GCSCAUDIO_5_1CH_AAC_ORDER
933 /* AAC: [C,L,R,SL,SR,LFE] => [L,R] */
934 *d++ = s[1];
935 *d++ = s[2];
936 #else
937 /* WAV: [L,R,C,LFE,SL,SR] => [L,R] */
938 *d++ = s[0];
939 *d++ = s[1];
940 #endif
941 s += 6;
942 }
943
944 src = (char *)src + blksize;
945 dst = (char *)dst + blksize;
946 }
947 }
948
949 static void
950 channel_splitter(struct gcscaudio_softc *sc)
951 {
952 int splitsize, left;
953 void *src, *dst;
954
955 if (sc->sc_mch_splitter == NULL)
956 return;
957
958 left = sc->sc_mch_split_size - sc->sc_mch_split_off;
959 splitsize = sc->sc_mch_split_blksize;
960 if (left < splitsize)
961 splitsize = left;
962
963 src = (char *)sc->sc_mch_split_start + sc->sc_mch_split_off;
964 dst = (char *)sc->sc_mch_split_buf + sc->sc_mch_split_off;
965
966 sc->sc_mch_splitter(dst, src, splitsize, sc->sc_mch_split_blksize);
967
968 sc->sc_mch_split_off += sc->sc_mch_split_blksize;
969 if (sc->sc_mch_split_off >= sc->sc_mch_split_size)
970 sc->sc_mch_split_off = 0;
971 }
972
973 static int
974 gcscaudio_trigger_output(void *addr, void *start, void *end, int blksize,
975 void (*intr)(void *), void *arg,
976 const audio_params_t *param)
977 {
978 struct gcscaudio_softc *sc;
979 size_t size;
980
981 sc = (struct gcscaudio_softc *)addr;
982 sc->sc_play.ch_intr = intr;
983 sc->sc_play.ch_intr_arg = arg;
984 size = (char *)end - (char *)start;
985
986 switch (sc->sc_play.ch_params.channels) {
987 case 2:
988 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
989 blksize, 0))
990 return EINVAL;
991
992 if (!AC97_IS_4CH(sc->codec_if)) {
993 /*
994 * output 2ch PCM to FRONT.LR(BM0)
995 *
996 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
997 *
998 */
999 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1000 PRDADDR(PRD_TABLE_FRONT, 0));
1001
1002 /* start DMA transfer */
1003 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1004 ACC_BMx_CMD_WRITE |
1005 ACC_BMx_CMD_BYTE_ORD_EL |
1006 ACC_BMx_CMD_BM_CTL_ENABLE);
1007 } else {
1008 /*
1009 * output same PCM to FRONT.LR(BM0) and SURROUND.LR(BM6).
1010 * CENTER(BM4) and LFE(BM7) doesn't sound.
1011 *
1012 * 2ch: L,R,L,R,L,R,L,R,... => BM0: L,R,L,R,L,R,L,R,...
1013 * BM6: (same of BM0)
1014 * BM4: none
1015 * BM7: none
1016 */
1017 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1018 PRDADDR(PRD_TABLE_FRONT, 0));
1019 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1020 PRDADDR(PRD_TABLE_FRONT, 0));
1021
1022 /* start DMA transfer */
1023 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1024 ACC_BMx_CMD_WRITE |
1025 ACC_BMx_CMD_BYTE_ORD_EL |
1026 ACC_BMx_CMD_BM_CTL_ENABLE);
1027 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1028 ACC_BMx_CMD_WRITE |
1029 ACC_BMx_CMD_BYTE_ORD_EL |
1030 ACC_BMx_CMD_BM_CTL_ENABLE);
1031 }
1032 break;
1033 case 4:
1034 /*
1035 * output 4ch PCM split to FRONT.LR(BM0) and SURROUND.LR(BM6).
1036 * CENTER(BM4) and LFE(BM7) doesn't sound.
1037 *
1038 * rearrange ordered channel to continuous per channel
1039 *
1040 * 4ch: L,R,SL,SR,L,R,SL,SR,... => BM0: L,R,L,R,...
1041 * BM6: SL,SR,SL,SR,...
1042 * BM4: none
1043 * BM7: none
1044 */
1045 if (sc->sc_mch_split_buf)
1046 gcscaudio_free(sc, sc->sc_mch_split_buf,
1047 sc->sc_mch_split_size);
1048
1049 if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
1050 size)) == NULL)
1051 return ENOMEM;
1052
1053 /*
1054 * 1st and 2nd blocks are split immediately.
1055 * Other blocks will be split synchronous with intr.
1056 */
1057 split_buffer_4ch(sc->sc_mch_split_buf, start, blksize * 2,
1058 blksize);
1059
1060 sc->sc_mch_split_start = start;
1061 sc->sc_mch_split_size = size;
1062 sc->sc_mch_split_blksize = blksize;
1063 sc->sc_mch_split_off = (blksize * 2) % size;
1064 sc->sc_mch_splitter = split_buffer_4ch; /* split function */
1065
1066 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1067 blksize / 2, 0))
1068 return EINVAL;
1069 if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1070 size, blksize, blksize / 2, 0))
1071 return EINVAL;
1072
1073 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1074 PRDADDR(PRD_TABLE_FRONT, 0));
1075 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1076 PRDADDR(PRD_TABLE_SURR, 0));
1077
1078 /* start DMA transfer */
1079 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1080 ACC_BMx_CMD_WRITE |
1081 ACC_BMx_CMD_BYTE_ORD_EL |
1082 ACC_BMx_CMD_BM_CTL_ENABLE);
1083 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1084 ACC_BMx_CMD_WRITE |
1085 ACC_BMx_CMD_BYTE_ORD_EL |
1086 ACC_BMx_CMD_BM_CTL_ENABLE);
1087 break;
1088 case 6:
1089 /*
1090 * output 6ch PCM split to
1091 * FRONT.LR(BM0), SURROUND.LR(BM6), CENTER(BM4) and LFE(BM7)
1092 *
1093 * rearrange ordered channel to continuous per channel
1094 *
1095 * 5.1ch: L,R,C,LFE,SL,SR,... => BM0: L,R,...
1096 * BM4: C,...
1097 * BM6: SL,SR,...
1098 * BM7: LFE,...
1099 *
1100 */
1101 if (sc->sc_mch_split_buf)
1102 gcscaudio_free(sc, sc->sc_mch_split_buf,
1103 sc->sc_mch_split_size);
1104
1105 if ((sc->sc_mch_split_buf = gcscaudio_malloc(sc, AUMODE_PLAY,
1106 size)) == NULL)
1107 return ENOMEM;
1108
1109 /*
1110 * 1st and 2nd blocks are split immediately.
1111 * Other block will be split synchronous with intr.
1112 */
1113 split_buffer_6ch(sc->sc_mch_split_buf, start, blksize * 2,
1114 blksize);
1115
1116 sc->sc_mch_split_start = start;
1117 sc->sc_mch_split_size = size;
1118 sc->sc_mch_split_blksize = blksize;
1119 sc->sc_mch_split_off = (blksize * 2) % size;
1120 sc->sc_mch_splitter = split_buffer_6ch; /* split function */
1121
1122 if (build_prdtables(sc, PRD_TABLE_FRONT, start, size, blksize,
1123 blksize / 3, 0))
1124 return EINVAL;
1125 if (build_prdtables(sc, PRD_TABLE_CENTER, sc->sc_mch_split_buf,
1126 size, blksize, blksize / 3, blksize / 2))
1127 return EINVAL;
1128 if (build_prdtables(sc, PRD_TABLE_SURR, sc->sc_mch_split_buf,
1129 size, blksize, blksize / 3, 0))
1130 return EINVAL;
1131 if (build_prdtables(sc, PRD_TABLE_LFE, sc->sc_mch_split_buf,
1132 size, blksize, blksize / 3, blksize / 2 + blksize / 4))
1133 return EINVAL;
1134
1135 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM0_PRD,
1136 PRDADDR(PRD_TABLE_FRONT, 0));
1137 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM4_PRD,
1138 PRDADDR(PRD_TABLE_CENTER, 0));
1139 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM6_PRD,
1140 PRDADDR(PRD_TABLE_SURR, 0));
1141 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM7_PRD,
1142 PRDADDR(PRD_TABLE_LFE, 0));
1143
1144 /* start DMA transfer */
1145 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_CMD,
1146 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1147 ACC_BMx_CMD_BM_CTL_ENABLE);
1148 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_CMD,
1149 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1150 ACC_BMx_CMD_BM_CTL_ENABLE);
1151 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_CMD,
1152 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1153 ACC_BMx_CMD_BM_CTL_ENABLE);
1154 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_CMD,
1155 ACC_BMx_CMD_WRITE | ACC_BMx_CMD_BYTE_ORD_EL |
1156 ACC_BMx_CMD_BM_CTL_ENABLE);
1157 break;
1158 }
1159
1160 return 0;
1161 }
1162
1163 static int
1164 gcscaudio_trigger_input(void *addr, void *start, void *end, int blksize,
1165 void (*intr)(void *), void *arg,
1166 const audio_params_t *param)
1167 {
1168 struct gcscaudio_softc *sc;
1169 size_t size;
1170
1171 sc = (struct gcscaudio_softc *)addr;
1172 sc->sc_rec.ch_intr = intr;
1173 sc->sc_rec.ch_intr_arg = arg;
1174 size = (char *)end - (char *)start;
1175
1176 if (build_prdtables(sc, PRD_TABLE_REC, start, size, blksize, blksize, 0))
1177 return EINVAL;
1178
1179 bus_space_write_4(sc->sc_iot, sc->sc_ioh, ACC_BM1_PRD,
1180 PRDADDR(PRD_TABLE_REC, 0));
1181
1182 /* start transfer */
1183 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_CMD,
1184 ACC_BMx_CMD_READ |
1185 ACC_BMx_CMD_BYTE_ORD_EL |
1186 ACC_BMx_CMD_BM_CTL_ENABLE);
1187
1188 return 0;
1189 }
1190
1191 static void
1192 gcscaudio_get_locks(void *arg, kmutex_t **intr, kmutex_t **thread)
1193 {
1194 struct gcscaudio_softc *sc;
1195
1196 sc = (struct gcscaudio_softc *)arg;
1197
1198 *intr = &sc->sc_intr_lock;
1199 *thread = &sc->sc_lock;
1200 }
1201
1202 static int
1203 gcscaudio_intr(void *arg)
1204 {
1205 struct gcscaudio_softc *sc;
1206 uint16_t intr;
1207 uint8_t bmstat;
1208 int nintr;
1209
1210 nintr = 0;
1211 sc = (struct gcscaudio_softc *)arg;
1212
1213 mutex_spin_enter(&sc->sc_intr_lock);
1214
1215 intr = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ACC_IRQ_STATUS);
1216 if (intr == 0)
1217 goto done;
1218
1219 /* Front output */
1220 if (intr & ACC_IRQ_STATUS_BM0_IRQ_STS) {
1221 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM0_STATUS);
1222 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1223 aprint_normal_dev(&sc->sc_dev, "BM0: Bus Master Error\n");
1224 if (!(bmstat & ACC_BMx_STATUS_EOP))
1225 aprint_normal_dev(&sc->sc_dev, "BM0: NO End of Page?\n");
1226
1227 if (sc->sc_play.ch_intr) {
1228 sc->sc_play.ch_intr(sc->sc_play.ch_intr_arg);
1229 channel_splitter(sc);
1230 }
1231 nintr++;
1232 }
1233
1234 /* Center output */
1235 if (intr & ACC_IRQ_STATUS_BM4_IRQ_STS) {
1236 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM4_STATUS);
1237 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1238 aprint_normal_dev(&sc->sc_dev, "BM4: Bus Master Error\n");
1239 if (!(bmstat & ACC_BMx_STATUS_EOP))
1240 aprint_normal_dev(&sc->sc_dev, "BM4: NO End of Page?\n");
1241
1242 nintr++;
1243 }
1244
1245 /* Surround output */
1246 if (intr & ACC_IRQ_STATUS_BM6_IRQ_STS) {
1247 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM6_STATUS);
1248 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1249 aprint_normal_dev(&sc->sc_dev, "BM6: Bus Master Error\n");
1250 if (!(bmstat & ACC_BMx_STATUS_EOP))
1251 aprint_normal_dev(&sc->sc_dev, "BM6: NO End of Page?\n");
1252
1253 nintr++;
1254 }
1255
1256 /* LowFrequencyEffect output */
1257 if (intr & ACC_IRQ_STATUS_BM7_IRQ_STS) {
1258 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM7_STATUS);
1259 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1260 aprint_normal_dev(&sc->sc_dev, "BM7: Bus Master Error\n");
1261 if (!(bmstat & ACC_BMx_STATUS_EOP))
1262 aprint_normal_dev(&sc->sc_dev, "BM7: NO End of Page?\n");
1263
1264 nintr++;
1265 }
1266
1267 /* record */
1268 if (intr & ACC_IRQ_STATUS_BM1_IRQ_STS) {
1269 bmstat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ACC_BM1_STATUS);
1270 if (bmstat & ACC_BMx_STATUS_BM_EOP_ERR)
1271 aprint_normal_dev(&sc->sc_dev, "BM1: Bus Master Error\n");
1272 if (!(bmstat & ACC_BMx_STATUS_EOP))
1273 aprint_normal_dev(&sc->sc_dev, "BM1: NO End of Page?\n");
1274
1275 if (sc->sc_rec.ch_intr) {
1276 sc->sc_rec.ch_intr(sc->sc_rec.ch_intr_arg);
1277 }
1278 nintr++;
1279 }
1280
1281 #ifdef GCSCAUDIO_DEBUG
1282 if (intr & ACC_IRQ_STATUS_IRQ_STS)
1283 aprint_normal_dev(&sc->sc_dev, "Codec GPIO IRQ Status\n");
1284 if (intr & ACC_IRQ_STATUS_WU_IRQ_STS)
1285 aprint_normal_dev(&sc->sc_dev, "Codec GPIO Wakeup IRQ Status\n");
1286 if (intr & ACC_IRQ_STATUS_BM2_IRQ_STS)
1287 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 2 IRQ Status\n");
1288 if (intr & ACC_IRQ_STATUS_BM3_IRQ_STS)
1289 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 3 IRQ Status\n");
1290 if (intr & ACC_IRQ_STATUS_BM5_IRQ_STS)
1291 aprint_normal_dev(&sc->sc_dev, "Audio Bus Master 5 IRQ Status\n");
1292 #endif
1293
1294 done:
1295 mutex_spin_exit(&sc->sc_intr_lock);
1296
1297 return nintr ? 1 : 0;
1298 }
1299
1300 static bool
1301 gcscaudio_resume(device_t dv, const pmf_qual_t *qual)
1302 {
1303 struct gcscaudio_softc *sc = device_private(dv);
1304
1305 gcscaudio_reset_codec(sc);
1306 DELAY(1000);
1307 (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1308
1309 return true;
1310 }
1311
1312 static int
1313 gcscaudio_allocate_dma(struct gcscaudio_softc *sc, size_t size, void **addrp,
1314 bus_dma_segment_t *seglist, int nseg, int *rsegp,
1315 bus_dmamap_t *mapp)
1316 {
1317 int error;
1318
1319 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seglist,
1320 nseg, rsegp, BUS_DMA_WAITOK)) != 0) {
1321 aprint_error_dev(&sc->sc_dev,
1322 "unable to allocate DMA buffer, error=%d\n", error);
1323 goto fail_alloc;
1324 }
1325
1326 if ((error = bus_dmamem_map(sc->sc_dmat, seglist, nseg, size, addrp,
1327 BUS_DMA_WAITOK | BUS_DMA_COHERENT)) != 0) {
1328 aprint_error_dev(&sc->sc_dev,
1329 "unable to map DMA buffer, error=%d\n",
1330 error);
1331 goto fail_map;
1332 }
1333
1334 if ((error = bus_dmamap_create(sc->sc_dmat, size, nseg, size, 0,
1335 BUS_DMA_WAITOK, mapp)) != 0) {
1336 aprint_error_dev(&sc->sc_dev,
1337 "unable to create DMA map, error=%d\n", error);
1338 goto fail_create;
1339 }
1340
1341 if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
1342 BUS_DMA_WAITOK)) != 0) {
1343 aprint_error_dev(&sc->sc_dev,
1344 "unable to load DMA map, error=%d\n", error);
1345 goto fail_load;
1346 }
1347
1348 return 0;
1349
1350 fail_load:
1351 bus_dmamap_destroy(sc->sc_dmat, *mapp);
1352 fail_create:
1353 bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
1354 fail_map:
1355 bus_dmamem_free(sc->sc_dmat, seglist, nseg);
1356 fail_alloc:
1357 return error;
1358 }
1359