1 /* $NetBSD: emuxki.c,v 1.79 2024/06/08 21:02:29 andvar Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yannick Montulet, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * EMU10K1 single voice driver
34 * o. only 1 voice playback, 1 recording
35 * o. only s16le 2ch 48k
36 * This makes it simple to control buffers and interrupts
37 * while satisfying playback and recording quality.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: emuxki.c,v 1.79 2024/06/08 21:02:29 andvar Exp $");
42
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/module.h>
46 #include <sys/errno.h>
47 #include <sys/systm.h>
48 #include <sys/audioio.h>
49 #include <sys/mutex.h>
50 #include <sys/kmem.h>
51 #include <sys/fcntl.h>
52
53 #include <sys/bus.h>
54 #include <sys/intr.h>
55
56 #include <dev/pci/emuxkireg.h>
57 #include <dev/pci/emuxkivar.h>
58 #include <dev/pci/emuxki_boards.h>
59
60 /* #define EMUXKI_DEBUG 1 */
61 #ifdef EMUXKI_DEBUG
62 #define emudebug EMUXKI_DEBUG
63 # define DPRINTF(fmt...) do { if (emudebug) printf(fmt); } while (0)
64 # define DPRINTFN(n,fmt...) do { if (emudebug>=(n)) printf(fmt); } while (0)
65 #else
66 # define DPRINTF(fmt...) __nothing
67 # define DPRINTFN(n,fmt...) __nothing
68 #endif
69
70 /*
71 * PCI
72 * Note: emuxki's page table entry uses only 31bit addressing.
73 * (Maybe, later chip has 32bit mode, but it isn't used now.)
74 */
75
76 #define EMU_PCI_CBIO (0x10)
77
78 /* blackmagic */
79 #define X1(x) ((sc->sc_type & EMUXKI_AUDIGY) ? EMU_A_##x : EMU_##x)
80 #define X2(x, y) ((sc->sc_type & EMUXKI_AUDIGY) \
81 ? EMU_A_##x(EMU_A_##y) : EMU_##x(EMU_##y))
82 #define EMU_A_DSP_FX EMU_DSP_FX
83 #define EMU_A_DSP_IN_AC97 EMU_DSP_IN_AC97
84
85 /* prototypes */
86 static struct dmamem *dmamem_alloc(struct emuxki_softc *, size_t);
87 static void dmamem_free(struct dmamem *);
88 static void dmamem_sync(struct dmamem *, int);
89 static uint8_t emuxki_readio_1(struct emuxki_softc *, int) __unused;
90 static uint16_t emuxki_readio_2(struct emuxki_softc *, int);
91 static uint32_t emuxki_readio_4(struct emuxki_softc *, int);
92 static void emuxki_writeio_1(struct emuxki_softc *, int, uint8_t);
93 static void emuxki_writeio_2(struct emuxki_softc *, int, uint16_t);
94 static void emuxki_writeio_4(struct emuxki_softc *, int, uint32_t);
95 static uint32_t emuxki_readptr(struct emuxki_softc *, int, int, int);
96 static void emuxki_writeptr(struct emuxki_softc *, int, int, int, uint32_t);
97 static uint32_t emuxki_read(struct emuxki_softc *, int, int);
98 static void emuxki_write(struct emuxki_softc *, int, int, uint32_t);
99 static int emuxki_match(device_t, cfdata_t, void *);
100 static void emuxki_attach(device_t, device_t, void *);
101 static int emuxki_detach(device_t, int);
102 static int emuxki_init(struct emuxki_softc *);
103 static void emuxki_dsp_addop(struct emuxki_softc *, uint16_t *, uint8_t,
104 uint16_t, uint16_t, uint16_t, uint16_t);
105 static void emuxki_initfx(struct emuxki_softc *);
106 static void emuxki_play_start(struct emuxki_softc *, int, uint32_t,
107 uint32_t);
108 static void emuxki_play_stop(struct emuxki_softc *, int);
109
110 static int emuxki_query_format(void *, audio_format_query_t *);
111 static int emuxki_set_format(void *, int,
112 const audio_params_t *, const audio_params_t *,
113 audio_filter_reg_t *, audio_filter_reg_t *);
114 static int emuxki_halt_output(void *);
115 static int emuxki_halt_input(void *);
116 static int emuxki_intr(void *);
117 static int emuxki_getdev(void *, struct audio_device *);
118 static int emuxki_set_port(void *, mixer_ctrl_t *);
119 static int emuxki_get_port(void *, mixer_ctrl_t *);
120 static int emuxki_query_devinfo(void *, mixer_devinfo_t *);
121 static void *emuxki_allocm(void *, int, size_t);
122 static void emuxki_freem(void *, void *, size_t);
123 static int emuxki_round_blocksize(void *, int, int,
124 const audio_params_t *);
125 static size_t emuxki_round_buffersize(void *, int, size_t);
126 static int emuxki_get_props(void *);
127 static int emuxki_trigger_output(void *, void *, void *, int,
128 void (*)(void *), void *, const audio_params_t *);
129 static int emuxki_trigger_input(void *, void *, void *, int,
130 void (*)(void *), void *, const audio_params_t *);
131 static void emuxki_get_locks(void *, kmutex_t **, kmutex_t **);
132
133 static int emuxki_ac97_init(struct emuxki_softc *);
134 static int emuxki_ac97_attach(void *, struct ac97_codec_if *);
135 static int emuxki_ac97_read(void *, uint8_t, uint16_t *);
136 static int emuxki_ac97_write(void *, uint8_t, uint16_t);
137 static int emuxki_ac97_reset(void *);
138 static enum ac97_host_flags emuxki_ac97_flags(void *);
139
140
141 CFATTACH_DECL_NEW(emuxki, sizeof(struct emuxki_softc),
142 emuxki_match, emuxki_attach, emuxki_detach, NULL);
143
144 static const struct audio_hw_if emuxki_hw_if = {
145 .query_format = emuxki_query_format,
146 .set_format = emuxki_set_format,
147 .round_blocksize = emuxki_round_blocksize,
148 .halt_output = emuxki_halt_output,
149 .halt_input = emuxki_halt_input,
150 .getdev = emuxki_getdev,
151 .set_port = emuxki_set_port,
152 .get_port = emuxki_get_port,
153 .query_devinfo = emuxki_query_devinfo,
154 .allocm = emuxki_allocm,
155 .freem = emuxki_freem,
156 .round_buffersize = emuxki_round_buffersize,
157 .get_props = emuxki_get_props,
158 .trigger_output = emuxki_trigger_output,
159 .trigger_input = emuxki_trigger_input,
160 .get_locks = emuxki_get_locks,
161 };
162
163 static const struct audio_format emuxki_formats[] = {
164 {
165 .mode = AUMODE_PLAY | AUMODE_RECORD,
166 .encoding = AUDIO_ENCODING_SLINEAR_LE,
167 .validbits = 16,
168 .precision = 16,
169 .channels = 2,
170 .channel_mask = AUFMT_STEREO,
171 .frequency_type = 1,
172 .frequency = { 48000 },
173 }
174 };
175 #define EMUXKI_NFORMATS __arraycount(emuxki_formats)
176
177 /*
178 * dma memory
179 */
180
181 static struct dmamem *
dmamem_alloc(struct emuxki_softc * sc,size_t size)182 dmamem_alloc(struct emuxki_softc *sc, size_t size)
183 {
184 struct dmamem *mem;
185
186 KASSERT(!mutex_owned(&sc->sc_intr_lock));
187
188 /* Allocate memory for structure */
189 mem = kmem_alloc(sizeof(*mem), KM_SLEEP);
190 mem->dmat = sc->sc_dmat;
191 mem->size = size;
192 mem->align = EMU_DMA_ALIGN;
193 mem->nsegs = EMU_DMA_NSEGS;
194 mem->bound = 0;
195
196 mem->segs = kmem_alloc(mem->nsegs * sizeof(*(mem->segs)), KM_SLEEP);
197
198 if (bus_dmamem_alloc(mem->dmat, mem->size, mem->align, mem->bound,
199 mem->segs, mem->nsegs, &mem->rsegs, BUS_DMA_WAITOK)) {
200 device_printf(sc->sc_dev,
201 "%s bus_dmamem_alloc failed\n", __func__);
202 goto memfree;
203 }
204
205 if (bus_dmamem_map(mem->dmat, mem->segs, mem->nsegs, mem->size,
206 &mem->kaddr, BUS_DMA_WAITOK | BUS_DMA_COHERENT)) {
207 device_printf(sc->sc_dev,
208 "%s bus_dmamem_map failed\n", __func__);
209 goto free;
210 }
211
212 if (bus_dmamap_create(mem->dmat, mem->size, mem->nsegs, mem->size,
213 mem->bound, BUS_DMA_WAITOK, &mem->map)) {
214 device_printf(sc->sc_dev,
215 "%s bus_dmamap_create failed\n", __func__);
216 goto unmap;
217 }
218
219 if (bus_dmamap_load(mem->dmat, mem->map, mem->kaddr,
220 mem->size, NULL, BUS_DMA_WAITOK)) {
221 device_printf(sc->sc_dev,
222 "%s bus_dmamap_load failed\n", __func__);
223 goto destroy;
224 }
225
226 return mem;
227
228 destroy:
229 bus_dmamap_destroy(mem->dmat, mem->map);
230 unmap:
231 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
232 free:
233 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
234 memfree:
235 kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
236 kmem_free(mem, sizeof(*mem));
237
238 return NULL;
239 }
240
241 static void
dmamem_free(struct dmamem * mem)242 dmamem_free(struct dmamem *mem)
243 {
244
245 bus_dmamap_unload(mem->dmat, mem->map);
246 bus_dmamap_destroy(mem->dmat, mem->map);
247 bus_dmamem_unmap(mem->dmat, mem->kaddr, mem->size);
248 bus_dmamem_free(mem->dmat, mem->segs, mem->nsegs);
249
250 kmem_free(mem->segs, mem->nsegs * sizeof(*(mem->segs)));
251 kmem_free(mem, sizeof(*mem));
252 }
253
254 static void
dmamem_sync(struct dmamem * mem,int ops)255 dmamem_sync(struct dmamem *mem, int ops)
256 {
257
258 bus_dmamap_sync(mem->dmat, mem->map, 0, mem->size, ops);
259 }
260
261
262 /*
263 * I/O register access
264 */
265
266 static uint8_t
emuxki_readio_1(struct emuxki_softc * sc,int addr)267 emuxki_readio_1(struct emuxki_softc *sc, int addr)
268 {
269
270 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr);
271 }
272
273 static void
emuxki_writeio_1(struct emuxki_softc * sc,int addr,uint8_t data)274 emuxki_writeio_1(struct emuxki_softc *sc, int addr, uint8_t data)
275 {
276
277 bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
278 }
279
280 static uint16_t
emuxki_readio_2(struct emuxki_softc * sc,int addr)281 emuxki_readio_2(struct emuxki_softc *sc, int addr)
282 {
283
284 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, addr);
285 }
286
287 static void
emuxki_writeio_2(struct emuxki_softc * sc,int addr,uint16_t data)288 emuxki_writeio_2(struct emuxki_softc *sc, int addr, uint16_t data)
289 {
290
291 bus_space_write_2(sc->sc_iot, sc->sc_ioh, addr, data);
292 }
293
294 static uint32_t
emuxki_readio_4(struct emuxki_softc * sc,int addr)295 emuxki_readio_4(struct emuxki_softc *sc, int addr)
296 {
297
298 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
299 }
300
301 static void
emuxki_writeio_4(struct emuxki_softc * sc,int addr,uint32_t data)302 emuxki_writeio_4(struct emuxki_softc *sc, int addr, uint32_t data)
303 {
304
305 bus_space_write_4(sc->sc_iot, sc->sc_ioh, addr, data);
306 }
307
308 static uint32_t
emuxki_readptr(struct emuxki_softc * sc,int aptr,int dptr,int addr)309 emuxki_readptr(struct emuxki_softc *sc, int aptr, int dptr, int addr)
310 {
311 uint32_t data;
312
313 mutex_spin_enter(&sc->sc_index_lock);
314 emuxki_writeio_4(sc, aptr, addr);
315 data = emuxki_readio_4(sc, dptr);
316 mutex_spin_exit(&sc->sc_index_lock);
317 return data;
318 }
319
320 static void
emuxki_writeptr(struct emuxki_softc * sc,int aptr,int dptr,int addr,uint32_t data)321 emuxki_writeptr(struct emuxki_softc *sc, int aptr, int dptr, int addr,
322 uint32_t data)
323 {
324
325 mutex_spin_enter(&sc->sc_index_lock);
326 emuxki_writeio_4(sc, aptr, addr);
327 emuxki_writeio_4(sc, dptr, data);
328 mutex_spin_exit(&sc->sc_index_lock);
329 }
330
331 static uint32_t
emuxki_read(struct emuxki_softc * sc,int ch,int addr)332 emuxki_read(struct emuxki_softc *sc, int ch, int addr)
333 {
334
335 /* Original HENTAI addressing is never supported. */
336 KASSERT((addr & 0xff000000) == 0);
337
338 return emuxki_readptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch);
339 }
340
341 static void
emuxki_write(struct emuxki_softc * sc,int ch,int addr,uint32_t data)342 emuxki_write(struct emuxki_softc *sc, int ch, int addr, uint32_t data)
343 {
344
345 /* Original HENTAI addressing is never supported. */
346 KASSERT((addr & 0xff000000) == 0);
347
348 emuxki_writeptr(sc, EMU_PTR, EMU_DATA, (addr << 16) + ch, data);
349 }
350
351 /*
352 * MD driver
353 */
354
355 static int
emuxki_match(device_t parent,cfdata_t match,void * aux)356 emuxki_match(device_t parent, cfdata_t match, void *aux)
357 {
358 struct pci_attach_args *pa;
359 pcireg_t reg;
360
361 pa = aux;
362
363 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
364 if (emuxki_board_lookup(PCI_VENDOR(pa->pa_id),
365 PCI_PRODUCT(pa->pa_id), reg,
366 PCI_REVISION(pa->pa_class)) != NULL)
367 return 1;
368
369 return 0;
370 }
371
372 static void
emuxki_attach(device_t parent,device_t self,void * aux)373 emuxki_attach(device_t parent, device_t self, void *aux)
374 {
375 struct emuxki_softc *sc;
376 struct pci_attach_args *pa;
377 const struct emuxki_board *sb;
378 pci_intr_handle_t ih;
379 const char *intrstr;
380 char intrbuf[PCI_INTRSTR_LEN];
381 pcireg_t reg;
382
383 sc = device_private(self);
384 sc->sc_dev = self;
385 pa = aux;
386
387 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
388 sb = emuxki_board_lookup(PCI_VENDOR(pa->pa_id),
389 PCI_PRODUCT(pa->pa_id), reg,
390 PCI_REVISION(pa->pa_class));
391 KASSERT(sb != NULL);
392
393 pci_aprint_devinfo(pa, "Audio controller");
394 aprint_normal_dev(self, "%s [%s]\n", sb->sb_name, sb->sb_board);
395 DPRINTF("dmat=%p\n", (char *)pa->pa_dmat);
396
397 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
398 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
399 mutex_init(&sc->sc_index_lock, MUTEX_DEFAULT, IPL_AUDIO);
400
401 sc->sc_pc = pa->pa_pc;
402
403 /* EMU10K1 can only address 31 bits (2GB) */
404 if (bus_dmatag_subregion(pa->pa_dmat, 0, ((uint32_t)1 << 31) - 1,
405 &(sc->sc_dmat), BUS_DMA_NOWAIT) != 0) {
406 aprint_error_dev(self,
407 "WARNING: failed to restrict dma range,"
408 " falling back to parent bus dma range\n");
409 sc->sc_dmat = pa->pa_dmat;
410 }
411
412 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
413 reg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE |
414 PCI_COMMAND_MEM_ENABLE;
415 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
416
417 if (pci_mapreg_map(pa, EMU_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
418 &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
419 aprint_error(": can't map iospace\n");
420 return;
421 }
422
423 if (pci_intr_map(pa, &ih)) {
424 aprint_error_dev(self, "couldn't map interrupt\n");
425 goto unmap;
426 }
427
428 intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
429 sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_AUDIO,
430 emuxki_intr, sc, device_xname(self));
431 if (sc->sc_ih == NULL) {
432 aprint_error_dev(self, "couldn't establish interrupt");
433 if (intrstr != NULL)
434 aprint_error(" at %s", intrstr);
435 aprint_error("\n");
436 goto unmap;
437 }
438 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
439
440 /* XXX it's unknown whether APS is made from Audigy as well */
441 sc->sc_type = sb->sb_flags;
442 if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
443 strlcpy(sc->sc_audv.name, "Audigy2+CA0108",
444 sizeof(sc->sc_audv.name));
445 } else if (sc->sc_type & EMUXKI_AUDIGY2) {
446 strlcpy(sc->sc_audv.name, "Audigy2", sizeof(sc->sc_audv.name));
447 } else if (sc->sc_type & EMUXKI_AUDIGY) {
448 strlcpy(sc->sc_audv.name, "Audigy", sizeof(sc->sc_audv.name));
449 } else if (sc->sc_type & EMUXKI_APS) {
450 strlcpy(sc->sc_audv.name, "E-mu APS", sizeof(sc->sc_audv.name));
451 } else {
452 strlcpy(sc->sc_audv.name, "SB Live!", sizeof(sc->sc_audv.name));
453 }
454 snprintf(sc->sc_audv.version, sizeof(sc->sc_audv.version), "0x%02x",
455 PCI_REVISION(pa->pa_class));
456 strlcpy(sc->sc_audv.config, "emuxki", sizeof(sc->sc_audv.config));
457
458 if (emuxki_init(sc)) {
459 aprint_error("emuxki_init error\n");
460 goto intrdis;
461 }
462 if (emuxki_ac97_init(sc)) {
463 aprint_error("emuxki_ac97_init error\n");
464 goto intrdis;
465 }
466
467 sc->sc_audev = audio_attach_mi(&emuxki_hw_if, sc, self);
468 if (sc->sc_audev == NULL) {
469 aprint_error("audio_attach_mi error\n");
470 goto intrdis;
471 }
472
473 return;
474
475 intrdis:
476 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
477 unmap:
478 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
479 return;
480 }
481
482 static int
emuxki_detach(device_t self,int flags)483 emuxki_detach(device_t self, int flags)
484 {
485 struct emuxki_softc *sc = device_private(self);
486 int error;
487
488 error = config_detach_children(self, flags);
489 if (error)
490 return error;
491
492 /* All voices should be stopped now but add some code here if not */
493 emuxki_writeio_4(sc, EMU_HCFG,
494 EMU_HCFG_LOCKSOUNDCACHE |
495 EMU_HCFG_LOCKTANKCACHE_MASK |
496 EMU_HCFG_MUTEBUTTONENABLE);
497 emuxki_writeio_4(sc, EMU_INTE, 0);
498
499 /* Disable any Channels interrupts */
500 emuxki_write(sc, 0, EMU_CLIEL, 0);
501 emuxki_write(sc, 0, EMU_CLIEH, 0);
502 emuxki_write(sc, 0, EMU_SOLEL, 0);
503 emuxki_write(sc, 0, EMU_SOLEH, 0);
504
505 /* stop DSP */
506 emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
507
508 dmamem_free(sc->ptb);
509
510 pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
511 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
512
513 mutex_destroy(&sc->sc_lock);
514 mutex_destroy(&sc->sc_intr_lock);
515 mutex_destroy(&sc->sc_index_lock);
516
517 return 0;
518 }
519
520 static int
emuxki_init(struct emuxki_softc * sc)521 emuxki_init(struct emuxki_softc *sc)
522 {
523 int i;
524 uint32_t spcs;
525 uint32_t hcfg;
526
527 /* clear AUDIO bit */
528 emuxki_writeio_4(sc, EMU_HCFG,
529 EMU_HCFG_LOCKSOUNDCACHE |
530 EMU_HCFG_LOCKTANKCACHE_MASK |
531 EMU_HCFG_MUTEBUTTONENABLE);
532
533 /* mask interrupt without PCIERR */
534 emuxki_writeio_4(sc, EMU_INTE,
535 EMU_INTE_SAMPLERATER | /* always on this bit */
536 EMU_INTE_PCIERRENABLE);
537
538 /* disable all channel interrupt */
539 emuxki_write(sc, 0, EMU_CLIEL, 0);
540 emuxki_write(sc, 0, EMU_CLIEH, 0);
541 emuxki_write(sc, 0, EMU_SOLEL, 0);
542 emuxki_write(sc, 0, EMU_SOLEH, 0);
543
544 /* Set recording buffers sizes to zero */
545 emuxki_write(sc, 0, EMU_MICBS, EMU_RECBS_BUFSIZE_NONE);
546 emuxki_write(sc, 0, EMU_MICBA, 0);
547 emuxki_write(sc, 0, EMU_FXBS, EMU_RECBS_BUFSIZE_NONE);
548 emuxki_write(sc, 0, EMU_FXBA, 0);
549 emuxki_write(sc, 0, EMU_ADCBS, EMU_RECBS_BUFSIZE_NONE);
550 emuxki_write(sc, 0, EMU_ADCBA, 0);
551
552 if(sc->sc_type & EMUXKI_AUDIGY) {
553 emuxki_write(sc, 0, EMU_SPBYPASS, EMU_SPBYPASS_24_BITS);
554 emuxki_write(sc, 0, EMU_AC97SLOT,
555 EMU_AC97SLOT_CENTER | EMU_AC97SLOT_LFE);
556 }
557
558 /* Initialize all channels to stopped and no effects */
559 for (i = 0; i < EMU_NUMCHAN; i++) {
560 emuxki_write(sc, i, EMU_CHAN_DCYSUSV, 0x7f7f);
561 emuxki_write(sc, i, EMU_CHAN_IP, EMU_CHAN_IP_UNITY);
562 emuxki_write(sc, i, EMU_CHAN_VTFT, 0xffff);
563 emuxki_write(sc, i, EMU_CHAN_CVCF, 0xffff);
564 emuxki_write(sc, i, EMU_CHAN_PTRX, 0);
565 emuxki_write(sc, i, EMU_CHAN_CPF, 0);
566 emuxki_write(sc, i, EMU_CHAN_CCR, 0);
567 emuxki_write(sc, i, EMU_CHAN_PSST, 0);
568 emuxki_write(sc, i, EMU_CHAN_DSL, 0);
569 emuxki_write(sc, i, EMU_CHAN_CCCA, EMU_CHAN_CCCA_INTERPROM_1);
570 emuxki_write(sc, i, EMU_CHAN_Z1, 0);
571 emuxki_write(sc, i, EMU_CHAN_Z2, 0);
572 emuxki_write(sc, i, EMU_CHAN_MAPA, 0xffffffff);
573 emuxki_write(sc, i, EMU_CHAN_MAPB, 0xffffffff);
574 emuxki_write(sc, i, EMU_CHAN_FXRT, 0x32100000);
575 emuxki_write(sc, i, EMU_CHAN_ATKHLDM, 0);
576 emuxki_write(sc, i, EMU_CHAN_DCYSUSM, 0);
577 emuxki_write(sc, i, EMU_CHAN_IFATN, 0xffff);
578 emuxki_write(sc, i, EMU_CHAN_PEFE, 0x007f);
579 emuxki_write(sc, i, EMU_CHAN_FMMOD, 0);
580 emuxki_write(sc, i, EMU_CHAN_TREMFRQ, 0);
581 emuxki_write(sc, i, EMU_CHAN_FM2FRQ2, 0);
582 emuxki_write(sc, i, EMU_CHAN_TEMPENV, 0);
583
584 /* these are last so OFF prevents writing */
585 emuxki_write(sc, i, EMU_CHAN_LFOVAL2, 0x8000);
586 emuxki_write(sc, i, EMU_CHAN_LFOVAL1, 0x8000);
587 emuxki_write(sc, i, EMU_CHAN_ATKHLDV, 0x7f7f);
588 emuxki_write(sc, i, EMU_CHAN_ENVVOL, 0);
589 emuxki_write(sc, i, EMU_CHAN_ENVVAL, 0x8000);
590 }
591
592 /* set digital outputs format */
593 spcs = EMU_SPCS_CLKACCY_1000PPM |
594 EMU_SPCS_SAMPLERATE_48 |
595 EMU_SPCS_CHANNELNUM_LEFT |
596 EMU_SPCS_SOURCENUM_UNSPEC |
597 EMU_SPCS_GENERATIONSTATUS |
598 0x00001200 /* Cat code. */ |
599 0x00000000 /* IEC-958 Mode */ |
600 EMU_SPCS_EMPHASIS_NONE |
601 EMU_SPCS_COPYRIGHT;
602 emuxki_write(sc, 0, EMU_SPCS0, spcs);
603 emuxki_write(sc, 0, EMU_SPCS1, spcs);
604 emuxki_write(sc, 0, EMU_SPCS2, spcs);
605
606 if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
607 /* Setup SRCMulti_I2S SamplingRate */
608 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
609 emuxki_read(sc, 0, EMU_A2_SPDIF_SAMPLERATE) & 0xfffff1ff);
610
611 /* Setup SRCSel (Enable SPDIF, I2S SRCMulti) */
612 emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
613 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
614
615 /* Setup SRCMulti Input Audio Enable */
616 emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
617 0x7b0000, 0xff000000);
618
619 /* Setup SPDIF Out Audio Enable
620 * The Audigy 2 Value has a separate SPDIF out,
621 * so no need for a mixer switch */
622 emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA,
623 0x7a0000, 0xff000000);
624 emuxki_writeio_4(sc, EMU_A_IOCFG,
625 emuxki_readio_4(sc, EMU_A_IOCFG) & ~0x8); /* clear bit 3 */
626 } else if (sc->sc_type & EMUXKI_AUDIGY2) {
627 emuxki_write(sc, 0, EMU_A2_SPDIF_SAMPLERATE,
628 EMU_A2_SPDIF_UNKNOWN);
629
630 emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCSEL,
631 EMU_A2_SRCSEL_ENABLE_SPDIF | EMU_A2_SRCSEL_ENABLE_SRCMULTI);
632
633 emuxki_writeptr(sc, EMU_A2_PTR, EMU_A2_DATA, EMU_A2_SRCMULTI,
634 EMU_A2_SRCMULTI_ENABLE_INPUT);
635 }
636
637 /* page table */
638 sc->ptb = dmamem_alloc(sc, EMU_MAXPTE * sizeof(uint32_t));
639 if (sc->ptb == NULL) {
640 device_printf(sc->sc_dev, "ptb allocation error\n");
641 return ENOMEM;
642 }
643 emuxki_write(sc, 0, EMU_PTB, DMAADDR(sc->ptb));
644
645 emuxki_write(sc, 0, EMU_TCBS, 0); /* This means 16K TCB */
646 emuxki_write(sc, 0, EMU_TCB, 0); /* No TCB use for now */
647
648 /* Let's play with sound processor */
649 emuxki_initfx(sc);
650
651 /* enable interrupt */
652 emuxki_writeio_4(sc, EMU_INTE,
653 emuxki_readio_4(sc, EMU_INTE) |
654 EMU_INTE_VOLINCRENABLE |
655 EMU_INTE_VOLDECRENABLE |
656 EMU_INTE_MUTEENABLE);
657
658 if (sc->sc_type & EMUXKI_AUDIGY2_CA0108) {
659 emuxki_writeio_4(sc, EMU_A_IOCFG,
660 0x0060 | emuxki_readio_4(sc, EMU_A_IOCFG));
661 } else if (sc->sc_type & EMUXKI_AUDIGY2) {
662 emuxki_writeio_4(sc, EMU_A_IOCFG,
663 EMU_A_IOCFG_GPOUT0 | emuxki_readio_4(sc, EMU_A_IOCFG));
664 }
665
666 /* enable AUDIO bit */
667 hcfg = EMU_HCFG_AUDIOENABLE | EMU_HCFG_AUTOMUTE;
668
669 if (sc->sc_type & EMUXKI_AUDIGY2) {
670 hcfg |= EMU_HCFG_AC3ENABLE_CDSPDIF |
671 EMU_HCFG_AC3ENABLE_GPSPDIF;
672 } else if (sc->sc_type & EMUXKI_AUDIGY) {
673 } else {
674 hcfg |= EMU_HCFG_LOCKTANKCACHE_MASK;
675 }
676 /* joystick not supported now */
677 emuxki_writeio_4(sc, EMU_HCFG, hcfg);
678
679 return 0;
680 }
681
682 /*
683 * dsp programming
684 */
685
686 static void
emuxki_dsp_addop(struct emuxki_softc * sc,uint16_t * pc,uint8_t op,uint16_t r,uint16_t a,uint16_t x,uint16_t y)687 emuxki_dsp_addop(struct emuxki_softc *sc, uint16_t *pc, uint8_t op,
688 uint16_t r, uint16_t a, uint16_t x, uint16_t y)
689 {
690 uint32_t loword;
691 uint32_t hiword;
692 int reg;
693
694 if (sc->sc_type & EMUXKI_AUDIGY) {
695 reg = EMU_A_MICROCODEBASE;
696 loword = (x << 12) & EMU_A_DSP_LOWORD_OPX_MASK;
697 loword |= y & EMU_A_DSP_LOWORD_OPY_MASK;
698 hiword = (op << 24) & EMU_A_DSP_HIWORD_OPCODE_MASK;
699 hiword |= (r << 12) & EMU_A_DSP_HIWORD_RESULT_MASK;
700 hiword |= a & EMU_A_DSP_HIWORD_OPA_MASK;
701 } else {
702 reg = EMU_MICROCODEBASE;
703 loword = (x << 10) & EMU_DSP_LOWORD_OPX_MASK;
704 loword |= y & EMU_DSP_LOWORD_OPY_MASK;
705 hiword = (op << 20) & EMU_DSP_HIWORD_OPCODE_MASK;
706 hiword |= (r << 10) & EMU_DSP_HIWORD_RESULT_MASK;
707 hiword |= a & EMU_DSP_HIWORD_OPA_MASK;
708 }
709
710 reg += (*pc) * 2;
711 /* must ordering; lo, hi */
712 emuxki_write(sc, 0, reg, loword);
713 emuxki_write(sc, 0, reg + 1, hiword);
714
715 (*pc)++;
716 }
717
718 static void
emuxki_initfx(struct emuxki_softc * sc)719 emuxki_initfx(struct emuxki_softc *sc)
720 {
721 uint16_t pc;
722
723 /* Set all GPRs to 0 */
724 for (pc = 0; pc < 256; pc++)
725 emuxki_write(sc, 0, EMU_DSP_GPR(pc), 0);
726 for (pc = 0; pc < 160; pc++) {
727 emuxki_write(sc, 0, EMU_TANKMEMDATAREGBASE + pc, 0);
728 emuxki_write(sc, 0, EMU_TANKMEMADDRREGBASE + pc, 0);
729 }
730
731 /* stop DSP, single step mode */
732 emuxki_write(sc, 0, X1(DBG), X1(DBG_SINGLE_STEP));
733
734 /* XXX: delay (48kHz equiv. 21us) if needed */
735
736 /* start DSP programming */
737 pc = 0;
738
739 /* OUT[L/R] = 0 + FX[L/R] * 1 */
740 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
741 X2(DSP_OUTL, DSP_OUT_A_FRONT),
742 X1(DSP_CST(0)),
743 X1(DSP_FX(0)),
744 X1(DSP_CST(1)));
745 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
746 X2(DSP_OUTR, DSP_OUT_A_FRONT),
747 X1(DSP_CST(0)),
748 X1(DSP_FX(1)),
749 X1(DSP_CST(1)));
750 #if 0
751 /* XXX: rear feature??? */
752 /* Rear OUT[L/R] = 0 + FX[L/R] * 1 */
753 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
754 X2(DSP_OUTL, DSP_OUT_A_REAR),
755 X1(DSP_CST(0)),
756 X1(DSP_FX(0)),
757 X1(DSP_CST(1)));
758 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_MACINTS,
759 X2(DSP_OUTR, DSP_OUT_A_REAR),
760 X1(DSP_CST(0)),
761 X1(DSP_FX(1)),
762 X1(DSP_CST(1)));
763 #endif
764 /* ADC recording[L/R] = AC97 In[L/R] */
765 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
766 X2(DSP_OUTL, DSP_OUT_ADC),
767 X2(DSP_INL, DSP_IN_AC97),
768 X1(DSP_CST(0)),
769 X1(DSP_CST(0)));
770 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
771 X2(DSP_OUTR, DSP_OUT_ADC),
772 X2(DSP_INR, DSP_IN_AC97),
773 X1(DSP_CST(0)),
774 X1(DSP_CST(0)));
775
776 /* fill NOP the rest of the microcode */
777 while (pc < 512) {
778 emuxki_dsp_addop(sc, &pc, EMU_DSP_OP_ACC3,
779 X1(DSP_CST(0)),
780 X1(DSP_CST(0)),
781 X1(DSP_CST(0)),
782 X1(DSP_CST(0)));
783 }
784
785 /* clear single step flag, run DSP */
786 emuxki_write(sc, 0, X1(DBG), 0);
787 }
788
789 /*
790 * operations
791 */
792
793 static void
emuxki_play_start(struct emuxki_softc * sc,int ch,uint32_t start,uint32_t end)794 emuxki_play_start(struct emuxki_softc *sc, int ch, uint32_t start, uint32_t end)
795 {
796 uint32_t pitch;
797 uint32_t volume;
798
799 /* 48kHz:16384 = 128/375 */
800 pitch = sc->play.sample_rate * 128 / 375;
801 volume = 32767;
802
803 emuxki_write(sc, ch, EMU_CHAN_DSL,
804 (0 << 24) | /* send amount D = 0 */
805 end);
806
807 emuxki_write(sc, ch, EMU_CHAN_PSST,
808 (0 << 24) | /* send amount C = 0 */
809 start);
810
811 emuxki_write(sc, ch, EMU_CHAN_VTFT,
812 (volume << 16) |
813 (0xffff)); /* cutoff filter = none */
814
815 emuxki_write(sc, ch, EMU_CHAN_CVCF,
816 (volume << 16) |
817 (0xffff)); /* cutoff filter = none */
818
819 emuxki_write(sc, ch, EMU_CHAN_PTRX,
820 (pitch << 16) |
821 ((ch == 0 ? 0x7f : 0) << 8) | /* send amount A = 255,0(L) */
822 ((ch == 0 ? 0 : 0x7f))); /* send amount B = 0,255(R) */
823
824 /* set the pitch to start */
825 emuxki_write(sc, ch, EMU_CHAN_CPF,
826 (pitch << 16) |
827 EMU_CHAN_CPF_STEREO_MASK); /* stereo only */
828 }
829
830 static void
emuxki_play_stop(struct emuxki_softc * sc,int ch)831 emuxki_play_stop(struct emuxki_softc *sc, int ch)
832 {
833
834 /* pitch = 0 to stop playing */
835 emuxki_write(sc, ch, EMU_CHAN_CPF, EMU_CHAN_CPF_STOP_MASK);
836 /* volume = 0 */
837 emuxki_write(sc, ch, EMU_CHAN_CVCF, 0);
838 }
839
840 static void
emuxki_timer_start(struct emuxki_softc * sc)841 emuxki_timer_start(struct emuxki_softc *sc)
842 {
843 uint32_t timer;
844
845 /* frame count of half PTE at 16bit, 2ch, 48kHz */
846 timer = EMU_PTESIZE / 4 / 2;
847
848 /* EMU_TIMER is 16bit register */
849 emuxki_writeio_2(sc, EMU_TIMER, timer);
850 emuxki_writeio_4(sc, EMU_INTE,
851 emuxki_readio_4(sc, EMU_INTE) |
852 EMU_INTE_INTERTIMERENB);
853 DPRINTF("timer start\n");
854 }
855
856 static void
emuxki_timer_stop(struct emuxki_softc * sc)857 emuxki_timer_stop(struct emuxki_softc *sc)
858 {
859
860 emuxki_writeio_4(sc, EMU_INTE,
861 emuxki_readio_4(sc, EMU_INTE) &
862 ~EMU_INTE_INTERTIMERENB);
863 /* EMU_TIMER is 16bit register */
864 emuxki_writeio_2(sc, EMU_TIMER, 0);
865 DPRINTF("timer stop\n");
866 }
867
868 /*
869 * audio interface
870 */
871
872 static int
emuxki_query_format(void * hdl,audio_format_query_t * afp)873 emuxki_query_format(void *hdl, audio_format_query_t *afp)
874 {
875
876 return audio_query_format(emuxki_formats, EMUXKI_NFORMATS, afp);
877 }
878
879 static int
emuxki_set_format(void * hdl,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)880 emuxki_set_format(void *hdl, int setmode,
881 const audio_params_t *play, const audio_params_t *rec,
882 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
883 {
884 struct emuxki_softc *sc = hdl;
885
886 if ((setmode & AUMODE_PLAY))
887 sc->play = *play;
888 if ((setmode & AUMODE_RECORD))
889 sc->rec = *rec;
890 return 0;
891 }
892
893 static int
emuxki_halt_output(void * hdl)894 emuxki_halt_output(void *hdl)
895 {
896 struct emuxki_softc *sc = hdl;
897
898 emuxki_timer_stop(sc);
899 emuxki_play_stop(sc, 0);
900 emuxki_play_stop(sc, 1);
901 return 0;
902 }
903
904 static int
emuxki_halt_input(void * hdl)905 emuxki_halt_input(void *hdl)
906 {
907 struct emuxki_softc *sc = hdl;
908
909 /* stop ADC */
910 emuxki_write(sc, 0, EMU_ADCCR, 0);
911
912 /* disable interrupt */
913 emuxki_writeio_4(sc, EMU_INTE,
914 emuxki_readio_4(sc, EMU_INTE) &
915 ~EMU_INTE_ADCBUFENABLE);
916
917 return 0;
918 }
919
920 static int
emuxki_intr(void * hdl)921 emuxki_intr(void *hdl)
922 {
923 struct emuxki_softc *sc = hdl;
924 uint32_t ipr;
925 uint32_t curaddr;
926 int handled = 0;
927
928 mutex_spin_enter(&sc->sc_intr_lock);
929
930 ipr = emuxki_readio_4(sc, EMU_IPR);
931 DPRINTFN(3, "emuxki: ipr=%08x\n", ipr);
932 if (sc->pintr && (ipr & EMU_IPR_INTERVALTIMER)) {
933 /* read ch 0 */
934 curaddr = emuxki_read(sc, 0, EMU_CHAN_CCCA) &
935 EMU_CHAN_CCCA_CURRADDR_MASK;
936 DPRINTFN(3, "curaddr=%08x\n", curaddr);
937 curaddr *= sc->pframesize;
938
939 if (curaddr < sc->poffset)
940 curaddr += sc->plength;
941 if (curaddr >= sc->poffset + sc->pblksize) {
942 dmamem_sync(sc->pmem, BUS_DMASYNC_POSTWRITE);
943 sc->pintr(sc->pintrarg);
944 sc->poffset += sc->pblksize;
945 if (sc->poffset >= sc->plength) {
946 sc->poffset -= sc->plength;
947 }
948 dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
949 }
950 handled = 1;
951 }
952
953 if (sc->rintr &&
954 (ipr & (EMU_IPR_ADCBUFHALFFULL | EMU_IPR_ADCBUFFULL))) {
955 char *src;
956 char *dst;
957
958 /* Record DMA buffer has just 2 blocks */
959 src = KERNADDR(sc->rmem);
960 if (ipr & EMU_IPR_ADCBUFFULL) {
961 /* 2nd block */
962 src += EMU_REC_DMABLKSIZE;
963 }
964 dst = (char *)sc->rptr + sc->rcurrent;
965
966 dmamem_sync(sc->rmem, BUS_DMASYNC_POSTREAD);
967 memcpy(dst, src, EMU_REC_DMABLKSIZE);
968 /* for next trans */
969 dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
970 sc->rcurrent += EMU_REC_DMABLKSIZE;
971
972 if (sc->rcurrent >= sc->roffset + sc->rblksize) {
973 sc->rintr(sc->rintrarg);
974 sc->roffset += sc->rblksize;
975 if (sc->roffset >= sc->rlength) {
976 sc->roffset = 0;
977 sc->rcurrent = 0;
978 }
979 }
980
981 handled = 1;
982 }
983
984 #if defined(EMUXKI_DEBUG)
985 if (!handled) {
986 char buf[1024];
987 snprintb(buf, sizeof(buf),
988 "\20"
989 "\x19""RATETRCHANGE"
990 "\x18""FXDSP"
991 "\x17""FORCEINT"
992 "\x16""PCIERROR"
993 "\x15""VOLINCR"
994 "\x14""VOLDECR"
995 "\x13""MUTE"
996 "\x12""MICBUFFULL"
997 "\x11""MICBUFHALFFULL"
998 "\x10""ADCBUFFULL"
999 "\x0f""ADCBUFHALFFULL"
1000 "\x0e""EFXBUFFULL"
1001 "\x0d""EFXBUFHALFFULL"
1002 "\x0c""GPSPDIFSTCHANGE"
1003 "\x0b""CDROMSTCHANGE"
1004 /* INTERVALTIMER */
1005 "\x09""MIDITRANSBUFE"
1006 "\x08""MIDIRECVBUFE"
1007 "\x07""CHANNELLOOP"
1008 , ipr);
1009 DPRINTF("unexpected intr: %s\n", buf);
1010
1011 /* for debugging (must not handle if !DEBUG) */
1012 handled = 1;
1013 }
1014 #endif
1015
1016 /* Reset interrupt bit */
1017 emuxki_writeio_4(sc, EMU_IPR, ipr);
1018
1019 mutex_spin_exit(&sc->sc_intr_lock);
1020
1021 /* Interrupt handler must return !=0 if handled */
1022 return handled;
1023 }
1024
1025 static int
emuxki_getdev(void * hdl,struct audio_device * dev)1026 emuxki_getdev(void *hdl, struct audio_device *dev)
1027 {
1028 struct emuxki_softc *sc = hdl;
1029
1030 *dev = sc->sc_audv;
1031 return 0;
1032 }
1033
1034 static int
emuxki_set_port(void * hdl,mixer_ctrl_t * mctl)1035 emuxki_set_port(void *hdl, mixer_ctrl_t *mctl)
1036 {
1037 struct emuxki_softc *sc = hdl;
1038
1039 return sc->codecif->vtbl->mixer_set_port(sc->codecif, mctl);
1040 }
1041
1042 static int
emuxki_get_port(void * hdl,mixer_ctrl_t * mctl)1043 emuxki_get_port(void *hdl, mixer_ctrl_t *mctl)
1044 {
1045 struct emuxki_softc *sc = hdl;
1046
1047 return sc->codecif->vtbl->mixer_get_port(sc->codecif, mctl);
1048 }
1049
1050 static int
emuxki_query_devinfo(void * hdl,mixer_devinfo_t * minfo)1051 emuxki_query_devinfo(void *hdl, mixer_devinfo_t *minfo)
1052 {
1053 struct emuxki_softc *sc = hdl;
1054
1055 return sc->codecif->vtbl->query_devinfo(sc->codecif, minfo);
1056 }
1057
1058 static void *
emuxki_allocm(void * hdl,int direction,size_t size)1059 emuxki_allocm(void *hdl, int direction, size_t size)
1060 {
1061 struct emuxki_softc *sc = hdl;
1062
1063 if (direction == AUMODE_PLAY) {
1064 if (sc->pmem) {
1065 panic("pmem already allocated\n");
1066 return NULL;
1067 }
1068 sc->pmem = dmamem_alloc(sc, size);
1069 return KERNADDR(sc->pmem);
1070 } else {
1071 /* rmem is fixed size internal DMA buffer */
1072 if (sc->rmem) {
1073 panic("rmem already allocated\n");
1074 return NULL;
1075 }
1076 /* rmem fixed size */
1077 sc->rmem = dmamem_alloc(sc, EMU_REC_DMASIZE);
1078
1079 /* recording MI buffer is normal kmem, software trans. */
1080 sc->rptr = kmem_alloc(size, KM_SLEEP);
1081 return sc->rptr;
1082 }
1083 }
1084
1085 static void
emuxki_freem(void * hdl,void * ptr,size_t size)1086 emuxki_freem(void *hdl, void *ptr, size_t size)
1087 {
1088 struct emuxki_softc *sc = hdl;
1089
1090 if (sc->pmem && ptr == KERNADDR(sc->pmem)) {
1091 dmamem_free(sc->pmem);
1092 sc->pmem = NULL;
1093 }
1094 if (sc->rmem && ptr == sc->rptr) {
1095 dmamem_free(sc->rmem);
1096 sc->rmem = NULL;
1097 kmem_free(sc->rptr, size);
1098 sc->rptr = NULL;
1099 }
1100 }
1101
1102 /*
1103 * blocksize rounding to EMU_PTESIZE. It is for easy to drive.
1104 */
1105 static int
emuxki_round_blocksize(void * hdl,int blksize,int mode,const audio_params_t * param)1106 emuxki_round_blocksize(void *hdl, int blksize,
1107 int mode, const audio_params_t* param)
1108 {
1109
1110 /*
1111 * This is not necessary for recording, but symmetric for easy.
1112 * For recording buffer/block size requirements of hardware,
1113 * see EMU_RECBS_BUFSIZE_*
1114 */
1115 if (blksize < EMU_PTESIZE)
1116 blksize = EMU_PTESIZE;
1117 return rounddown(blksize, EMU_PTESIZE);
1118 }
1119
1120 static size_t
emuxki_round_buffersize(void * hdl,int direction,size_t bsize)1121 emuxki_round_buffersize(void *hdl, int direction, size_t bsize)
1122 {
1123
1124 /* This is not necessary for recording, but symmetric for easy */
1125 if (bsize < EMU_MINPTE * EMU_PTESIZE) {
1126 bsize = EMU_MINPTE * EMU_PTESIZE;
1127 } else if (bsize > EMU_MAXPTE * EMU_PTESIZE) {
1128 bsize = EMU_MAXPTE * EMU_PTESIZE;
1129 }
1130 return roundup(bsize, EMU_PTESIZE);
1131 }
1132
1133 static int
emuxki_get_props(void * hdl)1134 emuxki_get_props(void *hdl)
1135 {
1136
1137 return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
1138 AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1139 }
1140
1141 static int
emuxki_trigger_output(void * hdl,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * params)1142 emuxki_trigger_output(void *hdl, void *start, void *end, int blksize,
1143 void (*intr)(void *), void *arg, const audio_params_t *params)
1144 {
1145 struct emuxki_softc *sc = hdl;
1146 int npage;
1147 uint32_t *kptb;
1148 bus_addr_t dpmem;
1149 int i;
1150 uint32_t hwstart;
1151 uint32_t hwend;
1152
1153 if (sc->pmem == NULL)
1154 panic("pmem == NULL\n");
1155 if (start != KERNADDR(sc->pmem))
1156 panic("start != KERNADDR(sc->pmem)\n");
1157
1158 sc->pframesize = 4; /* channels * bit / 8 = 2*16/8=4 */
1159 sc->pblksize = blksize;
1160 sc->plength = (char *)end - (char *)start;
1161 sc->poffset = 0;
1162 npage = roundup(sc->plength, EMU_PTESIZE);
1163
1164 kptb = KERNADDR(sc->ptb);
1165 dpmem = DMAADDR(sc->pmem);
1166 for (i = 0; i < npage; i++) {
1167 kptb[i] = htole32(dpmem << 1);
1168 dpmem += EMU_PTESIZE;
1169 }
1170 dmamem_sync(sc->ptb, BUS_DMASYNC_PREWRITE);
1171
1172 hwstart = 0;
1173 hwend = hwstart + sc->plength / sc->pframesize;
1174
1175 sc->pintr = intr;
1176 sc->pintrarg = arg;
1177
1178 dmamem_sync(sc->pmem, BUS_DMASYNC_PREWRITE);
1179
1180 emuxki_play_start(sc, 0, hwstart, hwend);
1181 emuxki_play_start(sc, 1, hwstart, hwend);
1182
1183 emuxki_timer_start(sc);
1184
1185 return 0;
1186 }
1187
1188 /*
1189 * Recording uses temporary buffer. Because it can use ADC_HALF/FULL
1190 * interrupts and this method doesn't conflict with playback.
1191 */
1192
1193 static int
emuxki_trigger_input(void * hdl,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * params)1194 emuxki_trigger_input(void *hdl, void *start, void *end, int blksize,
1195 void (*intr)(void *), void *arg, const audio_params_t *params)
1196 {
1197 struct emuxki_softc *sc = hdl;
1198
1199 if (sc->rmem == NULL)
1200 panic("rmem == NULL\n");
1201 if (start != sc->rptr)
1202 panic("start != sc->rptr\n");
1203
1204 sc->rframesize = 4; /* channels * bit / 8 = 2*16/8=4 */
1205 sc->rblksize = blksize;
1206 sc->rlength = (char *)end - (char *)start;
1207 sc->roffset = 0;
1208 sc->rcurrent = 0;
1209
1210 sc->rintr = intr;
1211 sc->rintrarg = arg;
1212
1213 /*
1214 * Memo:
1215 * recording source is selected by AC97
1216 * AC97 input source routes to ADC by FX(DSP)
1217 *
1218 * Must keep following sequence order
1219 */
1220
1221 /* first, stop ADC */
1222 emuxki_write(sc, 0, EMU_ADCCR, 0);
1223 emuxki_write(sc, 0, EMU_ADCBA, 0);
1224 emuxki_write(sc, 0, EMU_ADCBS, 0);
1225
1226 dmamem_sync(sc->rmem, BUS_DMASYNC_PREREAD);
1227
1228 /* ADC interrupt enable */
1229 emuxki_writeio_4(sc, EMU_INTE,
1230 emuxki_readio_4(sc, EMU_INTE) |
1231 EMU_INTE_ADCBUFENABLE);
1232
1233 /* ADC Enable */
1234 /* stereo, 48kHz, enable */
1235 emuxki_write(sc, 0, EMU_ADCCR,
1236 X1(ADCCR_LCHANENABLE) | X1(ADCCR_RCHANENABLE));
1237
1238 /* ADC buffer address */
1239 emuxki_write(sc, 0, X1(ADCIDX), 0);
1240 emuxki_write(sc, 0, EMU_ADCBA, DMAADDR(sc->rmem));
1241
1242 /* ADC buffer size, to start */
1243 emuxki_write(sc, 0, EMU_ADCBS, EMU_REC_BUFSIZE_RECBS);
1244
1245 return 0;
1246 }
1247
1248 static void
emuxki_get_locks(void * hdl,kmutex_t ** intr,kmutex_t ** proc)1249 emuxki_get_locks(void *hdl, kmutex_t **intr, kmutex_t **proc)
1250 {
1251 struct emuxki_softc *sc = hdl;
1252
1253 *intr = &sc->sc_intr_lock;
1254 *proc = &sc->sc_lock;
1255 }
1256
1257 /*
1258 * AC97
1259 */
1260
1261 static int
emuxki_ac97_init(struct emuxki_softc * sc)1262 emuxki_ac97_init(struct emuxki_softc *sc)
1263 {
1264
1265 sc->hostif.arg = sc;
1266 sc->hostif.attach = emuxki_ac97_attach;
1267 sc->hostif.read = emuxki_ac97_read;
1268 sc->hostif.write = emuxki_ac97_write;
1269 sc->hostif.reset = emuxki_ac97_reset;
1270 sc->hostif.flags = emuxki_ac97_flags;
1271 return ac97_attach(&sc->hostif, sc->sc_dev, &sc->sc_lock);
1272 }
1273
1274 /*
1275 * AC97 callbacks
1276 */
1277
1278 static int
emuxki_ac97_attach(void * hdl,struct ac97_codec_if * codecif)1279 emuxki_ac97_attach(void *hdl, struct ac97_codec_if *codecif)
1280 {
1281 struct emuxki_softc *sc = hdl;
1282
1283 sc->codecif = codecif;
1284 return 0;
1285 }
1286
1287 static int
emuxki_ac97_read(void * hdl,uint8_t reg,uint16_t * val)1288 emuxki_ac97_read(void *hdl, uint8_t reg, uint16_t *val)
1289 {
1290 struct emuxki_softc *sc = hdl;
1291
1292 mutex_spin_enter(&sc->sc_index_lock);
1293 emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1294 *val = emuxki_readio_2(sc, EMU_AC97DATA);
1295 mutex_spin_exit(&sc->sc_index_lock);
1296
1297 return 0;
1298 }
1299
1300 static int
emuxki_ac97_write(void * hdl,uint8_t reg,uint16_t val)1301 emuxki_ac97_write(void *hdl, uint8_t reg, uint16_t val)
1302 {
1303 struct emuxki_softc *sc = hdl;
1304
1305 mutex_spin_enter(&sc->sc_index_lock);
1306 emuxki_writeio_1(sc, EMU_AC97ADDR, reg);
1307 emuxki_writeio_2(sc, EMU_AC97DATA, val);
1308 mutex_spin_exit(&sc->sc_index_lock);
1309
1310 return 0;
1311 }
1312
1313 static int
emuxki_ac97_reset(void * hdl)1314 emuxki_ac97_reset(void *hdl)
1315 {
1316
1317 return 0;
1318 }
1319
1320 static enum ac97_host_flags
emuxki_ac97_flags(void * hdl)1321 emuxki_ac97_flags(void *hdl)
1322 {
1323
1324 return AC97_HOST_SWAPPED_CHANNELS;
1325 }
1326
1327 MODULE(MODULE_CLASS_DRIVER, emuxki, "pci,audio");
1328
1329 #ifdef _MODULE
1330 #include "ioconf.c"
1331 #endif
1332
1333 static int
emuxki_modcmd(modcmd_t cmd,void * opaque)1334 emuxki_modcmd(modcmd_t cmd, void *opaque)
1335 {
1336 int error = 0;
1337
1338 switch (cmd) {
1339 case MODULE_CMD_INIT:
1340 #ifdef _MODULE
1341 error = config_init_component(cfdriver_ioconf_emuxki,
1342 cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1343 #endif
1344 return error;
1345 case MODULE_CMD_FINI:
1346 #ifdef _MODULE
1347 error = config_fini_component(cfdriver_ioconf_emuxki,
1348 cfattach_ioconf_emuxki, cfdata_ioconf_emuxki);
1349 #endif
1350 return error;
1351 default:
1352 return ENOTTY;
1353 }
1354 }
1355