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