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